/share/man/man4/ip.4

https://bitbucket.org/freebsd/freebsd-head/ · Forth · 897 lines · 894 code · 3 blank · 0 comment · 34 complexity · 7555048470857a1ac6a1444bccd55675 MD5 · raw file

  1. .\" Copyright (c) 1983, 1991, 1993
  2. .\" The Regents of the University of California. All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  7. .\" 1. Redistributions of source code must retain the above copyright
  8. .\" notice, this list of conditions and the following disclaimer.
  9. .\" 2. Redistributions in binary form must reproduce the above copyright
  10. .\" notice, this list of conditions and the following disclaimer in the
  11. .\" documentation and/or other materials provided with the distribution.
  12. .\" 3. All advertising materials mentioning features or use of this software
  13. .\" must display the following acknowledgement:
  14. .\" This product includes software developed by the University of
  15. .\" California, Berkeley and its contributors.
  16. .\" 4. Neither the name of the University nor the names of its contributors
  17. .\" may be used to endorse or promote products derived from this software
  18. .\" without specific prior written permission.
  19. .\"
  20. .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. .\" SUCH DAMAGE.
  31. .\"
  32. .\" @(#)ip.4 8.2 (Berkeley) 11/30/93
  33. .\" $FreeBSD$
  34. .\"
  35. .Dd October 12, 2012
  36. .Dt IP 4
  37. .Os
  38. .Sh NAME
  39. .Nm ip
  40. .Nd Internet Protocol
  41. .Sh SYNOPSIS
  42. .In sys/types.h
  43. .In sys/socket.h
  44. .In netinet/in.h
  45. .Ft int
  46. .Fn socket AF_INET SOCK_RAW proto
  47. .Sh DESCRIPTION
  48. .Tn IP
  49. is the transport layer protocol used
  50. by the Internet protocol family.
  51. Options may be set at the
  52. .Tn IP
  53. level
  54. when using higher-level protocols that are based on
  55. .Tn IP
  56. (such as
  57. .Tn TCP
  58. and
  59. .Tn UDP ) .
  60. It may also be accessed
  61. through a
  62. .Dq raw socket
  63. when developing new protocols, or
  64. special-purpose applications.
  65. .Pp
  66. There are several
  67. .Tn IP-level
  68. .Xr setsockopt 2
  69. and
  70. .Xr getsockopt 2
  71. options.
  72. .Dv IP_OPTIONS
  73. may be used to provide
  74. .Tn IP
  75. options to be transmitted in the
  76. .Tn IP
  77. header of each outgoing packet
  78. or to examine the header options on incoming packets.
  79. .Tn IP
  80. options may be used with any socket type in the Internet family.
  81. The format of
  82. .Tn IP
  83. options to be sent is that specified by the
  84. .Tn IP
  85. protocol specification (RFC-791), with one exception:
  86. the list of addresses for Source Route options must include the first-hop
  87. gateway at the beginning of the list of gateways.
  88. The first-hop gateway address will be extracted from the option list
  89. and the size adjusted accordingly before use.
  90. To disable previously specified options,
  91. use a zero-length buffer:
  92. .Bd -literal
  93. setsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0);
  94. .Ed
  95. .Pp
  96. .Dv IP_TOS
  97. and
  98. .Dv IP_TTL
  99. may be used to set the type-of-service and time-to-live
  100. fields in the
  101. .Tn IP
  102. header for
  103. .Dv SOCK_STREAM , SOCK_DGRAM ,
  104. and certain types of
  105. .Dv SOCK_RAW
  106. sockets.
  107. For example,
  108. .Bd -literal
  109. int tos = IPTOS_LOWDELAY; /* see <netinet/ip.h> */
  110. setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
  111. int ttl = 60; /* max = 255 */
  112. setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
  113. .Ed
  114. .Pp
  115. .Dv IP_MINTTL
  116. may be used to set the minimum acceptable TTL a packet must have when
  117. received on a socket.
  118. All packets with a lower TTL are silently dropped.
  119. This option is only really useful when set to 255, preventing packets
  120. from outside the directly connected networks reaching local listeners
  121. on sockets.
  122. .Pp
  123. .Dv IP_DONTFRAG
  124. may be used to set the Don't Fragment flag on IP packets.
  125. Currently this option is respected only on
  126. .Xr udp 4
  127. and raw
  128. .Xr ip 4
  129. sockets, unless the
  130. .Dv IP_HDRINCL
  131. option has been set.
  132. On
  133. .Xr tcp 4
  134. sockets, the Don't Fragment flag is controlled by the Path
  135. MTU Discovery option.
  136. Sending a packet larger than the MTU size of the egress interface,
  137. determined by the destination address, returns an
  138. .Er EMSGSIZE
  139. error.
  140. .Pp
  141. If the
  142. .Dv IP_RECVDSTADDR
  143. option is enabled on a
  144. .Dv SOCK_DGRAM
  145. socket,
  146. the
  147. .Xr recvmsg 2
  148. call will return the destination
  149. .Tn IP
  150. address for a
  151. .Tn UDP
  152. datagram.
  153. The
  154. .Vt msg_control
  155. field in the
  156. .Vt msghdr
  157. structure points to a buffer
  158. that contains a
  159. .Vt cmsghdr
  160. structure followed by the
  161. .Tn IP
  162. address.
  163. The
  164. .Vt cmsghdr
  165. fields have the following values:
  166. .Bd -literal
  167. cmsg_len = CMSG_LEN(sizeof(struct in_addr))
  168. cmsg_level = IPPROTO_IP
  169. cmsg_type = IP_RECVDSTADDR
  170. .Ed
  171. .Pp
  172. The source address to be used for outgoing
  173. .Tn UDP
  174. datagrams on a socket can be specified as ancillary data with a type code of
  175. .Dv IP_SENDSRCADDR .
  176. The msg_control field in the msghdr structure should point to a buffer
  177. that contains a
  178. .Vt cmsghdr
  179. structure followed by the
  180. .Tn IP
  181. address.
  182. The cmsghdr fields should have the following values:
  183. .Bd -literal
  184. cmsg_len = CMSG_LEN(sizeof(struct in_addr))
  185. cmsg_level = IPPROTO_IP
  186. cmsg_type = IP_SENDSRCADDR
  187. .Ed
  188. .Pp
  189. The socket should be either bound to
  190. .Dv INADDR_ANY
  191. and a local port, and the address supplied with
  192. .Dv IP_SENDSRCADDR
  193. should't be
  194. .Dv INADDR_ANY ,
  195. or the socket should be bound to a local address and the address supplied with
  196. .Dv IP_SENDSRCADDR
  197. should be
  198. .Dv INADDR_ANY .
  199. In the latter case bound address is overriden via generic source address
  200. selection logic, which would choose IP address of interface closest to
  201. destination.
  202. .Pp
  203. For convenience,
  204. .Dv IP_SENDSRCADDR
  205. is defined to have the same value as
  206. .Dv IP_RECVDSTADDR ,
  207. so the
  208. .Dv IP_RECVDSTADDR
  209. control message from
  210. .Xr recvmsg 2
  211. can be used directly as a control message for
  212. .Xr sendmsg 2 .
  213. .\"
  214. .Pp
  215. If the
  216. .Dv IP_ONESBCAST
  217. option is enabled on a
  218. .Dv SOCK_DGRAM
  219. or a
  220. .Dv SOCK_RAW
  221. socket, the destination address of outgoing
  222. broadcast datagrams on that socket will be forced
  223. to the undirected broadcast address,
  224. .Dv INADDR_BROADCAST ,
  225. before transmission.
  226. This is in contrast to the default behavior of the
  227. system, which is to transmit undirected broadcasts
  228. via the first network interface with the
  229. .Dv IFF_BROADCAST
  230. flag set.
  231. .Pp
  232. This option allows applications to choose which
  233. interface is used to transmit an undirected broadcast
  234. datagram.
  235. For example, the following code would force an
  236. undirected broadcast to be transmitted via the interface
  237. configured with the broadcast address 192.168.2.255:
  238. .Bd -literal
  239. char msg[512];
  240. struct sockaddr_in sin;
  241. int onesbcast = 1; /* 0 = disable (default), 1 = enable */
  242. setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &onesbcast, sizeof(onesbcast));
  243. sin.sin_addr.s_addr = inet_addr("192.168.2.255");
  244. sin.sin_port = htons(1234);
  245. sendto(s, msg, sizeof(msg), 0, &sin, sizeof(sin));
  246. .Ed
  247. .Pp
  248. It is the application's responsibility to set the
  249. .Dv IP_TTL
  250. option
  251. to an appropriate value in order to prevent broadcast storms.
  252. The application must have sufficient credentials to set the
  253. .Dv SO_BROADCAST
  254. socket level option, otherwise the
  255. .Dv IP_ONESBCAST
  256. option has no effect.
  257. .Pp
  258. If the
  259. .Dv IP_BINDANY
  260. option is enabled on a
  261. .Dv SOCK_STREAM ,
  262. .Dv SOCK_DGRAM
  263. or a
  264. .Dv SOCK_RAW
  265. socket, one can
  266. .Xr bind 2
  267. to any address, even one not bound to any available network interface in the
  268. system.
  269. This functionality (in conjunction with special firewall rules) can be used for
  270. implementing a transparent proxy.
  271. The
  272. .Dv PRIV_NETINET_BINDANY
  273. privilege is needed to set this option.
  274. .Pp
  275. If the
  276. .Dv IP_RECVTTL
  277. option is enabled on a
  278. .Dv SOCK_DGRAM
  279. socket, the
  280. .Xr recvmsg 2
  281. call will return the
  282. .Tn IP
  283. .Tn TTL
  284. (time to live) field for a
  285. .Tn UDP
  286. datagram.
  287. The msg_control field in the msghdr structure points to a buffer
  288. that contains a cmsghdr structure followed by the
  289. .Tn TTL .
  290. The cmsghdr fields have the following values:
  291. .Bd -literal
  292. cmsg_len = CMSG_LEN(sizeof(u_char))
  293. cmsg_level = IPPROTO_IP
  294. cmsg_type = IP_RECVTTL
  295. .Ed
  296. .\"
  297. .Pp
  298. If the
  299. .Dv IP_RECVTOS
  300. option is enabled on a
  301. .Dv SOCK_DGRAM
  302. socket, the
  303. .Xr recvmsg 2
  304. call will return the
  305. .Tn IP
  306. .Tn TOS
  307. (type of service) field for a
  308. .Tn UDP
  309. datagram.
  310. The msg_control field in the msghdr structure points to a buffer
  311. that contains a cmsghdr structure followed by the
  312. .Tn TOS .
  313. The cmsghdr fields have the following values:
  314. .Bd -literal
  315. cmsg_len = CMSG_LEN(sizeof(u_char))
  316. cmsg_level = IPPROTO_IP
  317. cmsg_type = IP_RECVTOS
  318. .Ed
  319. .\"
  320. .Pp
  321. If the
  322. .Dv IP_RECVIF
  323. option is enabled on a
  324. .Dv SOCK_DGRAM
  325. socket, the
  326. .Xr recvmsg 2
  327. call returns a
  328. .Vt "struct sockaddr_dl"
  329. corresponding to the interface on which the
  330. packet was received.
  331. The
  332. .Va msg_control
  333. field in the
  334. .Vt msghdr
  335. structure points to a buffer that contains a
  336. .Vt cmsghdr
  337. structure followed by the
  338. .Vt "struct sockaddr_dl" .
  339. The
  340. .Vt cmsghdr
  341. fields have the following values:
  342. .Bd -literal
  343. cmsg_len = CMSG_LEN(sizeof(struct sockaddr_dl))
  344. cmsg_level = IPPROTO_IP
  345. cmsg_type = IP_RECVIF
  346. .Ed
  347. .Pp
  348. .Dv IP_PORTRANGE
  349. may be used to set the port range used for selecting a local port number
  350. on a socket with an unspecified (zero) port number.
  351. It has the following
  352. possible values:
  353. .Bl -tag -width IP_PORTRANGE_DEFAULT
  354. .It Dv IP_PORTRANGE_DEFAULT
  355. use the default range of values, normally
  356. .Dv IPPORT_HIFIRSTAUTO
  357. through
  358. .Dv IPPORT_HILASTAUTO .
  359. This is adjustable through the sysctl setting:
  360. .Va net.inet.ip.portrange.first
  361. and
  362. .Va net.inet.ip.portrange.last .
  363. .It Dv IP_PORTRANGE_HIGH
  364. use a high range of values, normally
  365. .Dv IPPORT_HIFIRSTAUTO
  366. and
  367. .Dv IPPORT_HILASTAUTO .
  368. This is adjustable through the sysctl setting:
  369. .Va net.inet.ip.portrange.hifirst
  370. and
  371. .Va net.inet.ip.portrange.hilast .
  372. .It Dv IP_PORTRANGE_LOW
  373. use a low range of ports, which are normally restricted to
  374. privileged processes on
  375. .Ux
  376. systems.
  377. The range is normally from
  378. .Dv IPPORT_RESERVED
  379. \- 1 down to
  380. .Li IPPORT_RESERVEDSTART
  381. in descending order.
  382. This is adjustable through the sysctl setting:
  383. .Va net.inet.ip.portrange.lowfirst
  384. and
  385. .Va net.inet.ip.portrange.lowlast .
  386. .El
  387. .Pp
  388. The range of privileged ports which only may be opened by
  389. root-owned processes may be modified by the
  390. .Va net.inet.ip.portrange.reservedlow
  391. and
  392. .Va net.inet.ip.portrange.reservedhigh
  393. sysctl settings.
  394. The values default to the traditional range,
  395. 0 through
  396. .Dv IPPORT_RESERVED
  397. \- 1
  398. (0 through 1023), respectively.
  399. Note that these settings do not affect and are not accounted for in the
  400. use or calculation of the other
  401. .Va net.inet.ip.portrange
  402. values above.
  403. Changing these values departs from
  404. .Ux
  405. tradition and has security
  406. consequences that the administrator should carefully evaluate before
  407. modifying these settings.
  408. .Pp
  409. Ports are allocated at random within the specified port range in order
  410. to increase the difficulty of random spoofing attacks.
  411. In scenarios such as benchmarking, this behavior may be undesirable.
  412. In these cases,
  413. .Va net.inet.ip.portrange.randomized
  414. can be used to toggle randomization off.
  415. If more than
  416. .Va net.inet.ip.portrange.randomcps
  417. ports have been allocated in the last second, then return to sequential
  418. port allocation.
  419. Return to random allocation only once the current port allocation rate
  420. drops below
  421. .Va net.inet.ip.portrange.randomcps
  422. for at least
  423. .Va net.inet.ip.portrange.randomtime
  424. seconds.
  425. The default values for
  426. .Va net.inet.ip.portrange.randomcps
  427. and
  428. .Va net.inet.ip.portrange.randomtime
  429. are 10 port allocations per second and 45 seconds correspondingly.
  430. .Ss "Multicast Options"
  431. .Tn IP
  432. multicasting is supported only on
  433. .Dv AF_INET
  434. sockets of type
  435. .Dv SOCK_DGRAM
  436. and
  437. .Dv SOCK_RAW ,
  438. and only on networks where the interface
  439. driver supports multicasting.
  440. .Pp
  441. The
  442. .Dv IP_MULTICAST_TTL
  443. option changes the time-to-live (TTL)
  444. for outgoing multicast datagrams
  445. in order to control the scope of the multicasts:
  446. .Bd -literal
  447. u_char ttl; /* range: 0 to 255, default = 1 */
  448. setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  449. .Ed
  450. .Pp
  451. Datagrams with a TTL of 1 are not forwarded beyond the local network.
  452. Multicast datagrams with a TTL of 0 will not be transmitted on any network,
  453. but may be delivered locally if the sending host belongs to the destination
  454. group and if multicast loopback has not been disabled on the sending socket
  455. (see below).
  456. Multicast datagrams with TTL greater than 1 may be forwarded
  457. to other networks if a multicast router is attached to the local network.
  458. .Pp
  459. For hosts with multiple interfaces, where an interface has not
  460. been specified for a multicast group membership,
  461. each multicast transmission is sent from the primary network interface.
  462. The
  463. .Dv IP_MULTICAST_IF
  464. option overrides the default for
  465. subsequent transmissions from a given socket:
  466. .Bd -literal
  467. struct in_addr addr;
  468. setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
  469. .Ed
  470. .Pp
  471. where "addr" is the local
  472. .Tn IP
  473. address of the desired interface or
  474. .Dv INADDR_ANY
  475. to specify the default interface.
  476. .Pp
  477. To specify an interface by index, an instance of
  478. .Vt ip_mreqn
  479. may be passed instead.
  480. The
  481. .Vt imr_ifindex
  482. member should be set to the index of the desired interface,
  483. or 0 to specify the default interface.
  484. The kernel differentiates between these two structures by their size.
  485. .Pp
  486. The use of
  487. .Vt IP_MULTICAST_IF
  488. is
  489. .Em not recommended ,
  490. as multicast memberships are scoped to each
  491. individual interface.
  492. It is supported for legacy use only by applications,
  493. such as routing daemons, which expect to
  494. be able to transmit link-local IPv4 multicast datagrams (224.0.0.0/24)
  495. on multiple interfaces,
  496. without requesting an individual membership for each interface.
  497. .Pp
  498. .\"
  499. An interface's local IP address and multicast capability can
  500. be obtained via the
  501. .Dv SIOCGIFCONF
  502. and
  503. .Dv SIOCGIFFLAGS
  504. ioctls.
  505. Normal applications should not need to use this option.
  506. .Pp
  507. If a multicast datagram is sent to a group to which the sending host itself
  508. belongs (on the outgoing interface), a copy of the datagram is, by default,
  509. looped back by the IP layer for local delivery.
  510. The
  511. .Dv IP_MULTICAST_LOOP
  512. option gives the sender explicit control
  513. over whether or not subsequent datagrams are looped back:
  514. .Bd -literal
  515. u_char loop; /* 0 = disable, 1 = enable (default) */
  516. setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
  517. .Ed
  518. .Pp
  519. This option
  520. improves performance for applications that may have no more than one
  521. instance on a single host (such as a routing daemon), by eliminating
  522. the overhead of receiving their own transmissions.
  523. It should generally not
  524. be used by applications for which there may be more than one instance on a
  525. single host (such as a conferencing program) or for which the sender does
  526. not belong to the destination group (such as a time querying program).
  527. .Pp
  528. The sysctl setting
  529. .Va net.inet.ip.mcast.loop
  530. controls the default setting of the
  531. .Dv IP_MULTICAST_LOOP
  532. socket option for new sockets.
  533. .Pp
  534. A multicast datagram sent with an initial TTL greater than 1 may be delivered
  535. to the sending host on a different interface from that on which it was sent,
  536. if the host belongs to the destination group on that other interface.
  537. The loopback control option has no effect on such delivery.
  538. .Pp
  539. A host must become a member of a multicast group before it can receive
  540. datagrams sent to the group.
  541. To join a multicast group, use the
  542. .Dv IP_ADD_MEMBERSHIP
  543. option:
  544. .Bd -literal
  545. struct ip_mreq mreq;
  546. setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
  547. .Ed
  548. .Pp
  549. where
  550. .Fa mreq
  551. is the following structure:
  552. .Bd -literal
  553. struct ip_mreq {
  554. struct in_addr imr_multiaddr; /* IP multicast address of group */
  555. struct in_addr imr_interface; /* local IP address of interface */
  556. }
  557. .Ed
  558. .Pp
  559. .Va imr_interface
  560. should be set to the
  561. .Tn IP
  562. address of a particular multicast-capable interface if
  563. the host is multihomed.
  564. It may be set to
  565. .Dv INADDR_ANY
  566. to choose the default interface, although this is not recommended;
  567. this is considered to be the first interface corresponding
  568. to the default route.
  569. Otherwise, the first multicast-capable interface
  570. configured in the system will be used.
  571. .Pp
  572. Prior to
  573. .Fx 7.0 ,
  574. if the
  575. .Va imr_interface
  576. member is within the network range
  577. .Li 0.0.0.0/8 ,
  578. it is treated as an interface index in the system interface MIB,
  579. as per the RIP Version 2 MIB Extension (RFC-1724).
  580. In versions of
  581. .Fx
  582. since 7.0, this behavior is no longer supported.
  583. Developers should
  584. instead use the RFC 3678 multicast source filter APIs; in particular,
  585. .Dv MCAST_JOIN_GROUP .
  586. .Pp
  587. Up to
  588. .Dv IP_MAX_MEMBERSHIPS
  589. memberships may be added on a single socket.
  590. Membership is associated with a single interface;
  591. programs running on multihomed hosts may need to
  592. join the same group on more than one interface.
  593. .Pp
  594. To drop a membership, use:
  595. .Bd -literal
  596. struct ip_mreq mreq;
  597. setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
  598. .Ed
  599. .Pp
  600. where
  601. .Fa mreq
  602. contains the same values as used to add the membership.
  603. Memberships are dropped when the socket is closed or the process exits.
  604. .\" TODO: Update this piece when IPv4 source-address selection is implemented.
  605. .Pp
  606. The IGMP protocol uses the primary IP address of the interface
  607. as its identifier for group membership.
  608. This is the first IP address configured on the interface.
  609. If this address is removed or changed, the results are
  610. undefined, as the IGMP membership state will then be inconsistent.
  611. If multiple IP aliases are configured on the same interface,
  612. they will be ignored.
  613. .Pp
  614. This shortcoming was addressed in IPv6; MLDv2 requires
  615. that the unique link-local address for an interface is
  616. used to identify an MLDv2 listener.
  617. .Ss "Source-Specific Multicast Options"
  618. Since
  619. .Fx 8.0 ,
  620. the use of Source-Specific Multicast (SSM) is supported.
  621. These extensions require an IGMPv3 multicast router in order to
  622. make best use of them.
  623. If a legacy multicast router is present on the link,
  624. .Fx
  625. will simply downgrade to the version of IGMP spoken by the router,
  626. and the benefits of source filtering on the upstream link
  627. will not be present, although the kernel will continue to
  628. squelch transmissions from blocked sources.
  629. .Pp
  630. Each group membership on a socket now has a filter mode:
  631. .Bl -tag -width MCAST_EXCLUDE
  632. .It Dv MCAST_EXCLUDE
  633. Datagrams sent to this group are accepted,
  634. unless the source is in a list of blocked source addresses.
  635. .It Dv MCAST_INCLUDE
  636. Datagrams sent to this group are accepted
  637. only if the source is in a list of accepted source addresses.
  638. .El
  639. .Pp
  640. Groups joined using the legacy
  641. .Dv IP_ADD_MEMBERSHIP
  642. option are placed in exclusive-mode,
  643. and are able to request that certain sources are blocked or allowed.
  644. This is known as the
  645. .Em delta-based API .
  646. .Pp
  647. To block a multicast source on an existing group membership:
  648. .Bd -literal
  649. struct ip_mreq_source mreqs;
  650. setsockopt(s, IPPROTO_IP, IP_BLOCK_SOURCE, &mreqs, sizeof(mreqs));
  651. .Ed
  652. .Pp
  653. where
  654. .Fa mreqs
  655. is the following structure:
  656. .Bd -literal
  657. struct ip_mreq_source {
  658. struct in_addr imr_multiaddr; /* IP multicast address of group */
  659. struct in_addr imr_sourceaddr; /* IP address of source */
  660. struct in_addr imr_interface; /* local IP address of interface */
  661. }
  662. .Ed
  663. .Va imr_sourceaddr
  664. should be set to the address of the source to be blocked.
  665. .Pp
  666. To unblock a multicast source on an existing group:
  667. .Bd -literal
  668. struct ip_mreq_source mreqs;
  669. setsockopt(s, IPPROTO_IP, IP_UNBLOCK_SOURCE, &mreqs, sizeof(mreqs));
  670. .Ed
  671. .Pp
  672. The
  673. .Dv IP_BLOCK_SOURCE
  674. and
  675. .Dv IP_UNBLOCK_SOURCE
  676. options are
  677. .Em not permitted
  678. for inclusive-mode group memberships.
  679. .Pp
  680. To join a multicast group in
  681. .Dv MCAST_INCLUDE
  682. mode with a single source,
  683. or add another source to an existing inclusive-mode membership:
  684. .Bd -literal
  685. struct ip_mreq_source mreqs;
  686. setsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
  687. .Ed
  688. .Pp
  689. To leave a single source from an existing group in inclusive mode:
  690. .Bd -literal
  691. struct ip_mreq_source mreqs;
  692. setsockopt(s, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
  693. .Ed
  694. If this is the last accepted source for the group, the membership
  695. will be dropped.
  696. .Pp
  697. The
  698. .Dv IP_ADD_SOURCE_MEMBERSHIP
  699. and
  700. .Dv IP_DROP_SOURCE_MEMBERSHIP
  701. options are
  702. .Em not accepted
  703. for exclusive-mode group memberships.
  704. However, both exclusive and inclusive mode memberships
  705. support the use of the
  706. .Em full-state API
  707. documented in RFC 3678.
  708. For management of source filter lists using this API,
  709. please refer to
  710. .Xr sourcefilter 3 .
  711. .Pp
  712. The sysctl settings
  713. .Va net.inet.ip.mcast.maxsocksrc
  714. and
  715. .Va net.inet.ip.mcast.maxgrpsrc
  716. are used to specify an upper limit on the number of per-socket and per-group
  717. source filter entries which the kernel may allocate.
  718. .\"-----------------------
  719. .Ss "Raw IP Sockets"
  720. Raw
  721. .Tn IP
  722. sockets are connectionless,
  723. and are normally used with the
  724. .Xr sendto 2
  725. and
  726. .Xr recvfrom 2
  727. calls, though the
  728. .Xr connect 2
  729. call may also be used to fix the destination for future
  730. packets (in which case the
  731. .Xr read 2
  732. or
  733. .Xr recv 2
  734. and
  735. .Xr write 2
  736. or
  737. .Xr send 2
  738. system calls may be used).
  739. .Pp
  740. If
  741. .Fa proto
  742. is 0, the default protocol
  743. .Dv IPPROTO_RAW
  744. is used for outgoing
  745. packets, and only incoming packets destined for that protocol
  746. are received.
  747. If
  748. .Fa proto
  749. is non-zero, that protocol number will be used on outgoing packets
  750. and to filter incoming packets.
  751. .Pp
  752. Outgoing packets automatically have an
  753. .Tn IP
  754. header prepended to
  755. them (based on the destination address and the protocol
  756. number the socket is created with),
  757. unless the
  758. .Dv IP_HDRINCL
  759. option has been set.
  760. Incoming packets are received with
  761. .Tn IP
  762. header and options intact, except for
  763. .Va ip_len
  764. and
  765. .Va ip_off
  766. fields converted to host byte order.
  767. .Pp
  768. .Dv IP_HDRINCL
  769. indicates the complete IP header is included with the data
  770. and may be used only with the
  771. .Dv SOCK_RAW
  772. type.
  773. .Bd -literal
  774. #include <netinet/in_systm.h>
  775. #include <netinet/ip.h>
  776. int hincl = 1; /* 1 = on, 0 = off */
  777. setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
  778. .Ed
  779. .Pp
  780. Unlike previous
  781. .Bx
  782. releases, the program must set all
  783. the fields of the IP header, including the following:
  784. .Bd -literal
  785. ip->ip_v = IPVERSION;
  786. ip->ip_hl = hlen >> 2;
  787. ip->ip_id = 0; /* 0 means kernel set appropriate value */
  788. ip->ip_off = offset;
  789. .Ed
  790. .Pp
  791. The
  792. .Va ip_len
  793. and
  794. .Va ip_off
  795. fields
  796. .Em must
  797. be provided in host byte order.
  798. All other fields must be provided in network byte order.
  799. See
  800. .Xr byteorder 3
  801. for more information on network byte order.
  802. If the
  803. .Va ip_id
  804. field is set to 0 then the kernel will choose an
  805. appropriate value.
  806. If the header source address is set to
  807. .Dv INADDR_ANY ,
  808. the kernel will choose an appropriate address.
  809. .Sh ERRORS
  810. A socket operation may fail with one of the following errors returned:
  811. .Bl -tag -width Er
  812. .It Bq Er EISCONN
  813. when trying to establish a connection on a socket which
  814. already has one, or when trying to send a datagram with the destination
  815. address specified and the socket is already connected;
  816. .It Bq Er ENOTCONN
  817. when trying to send a datagram, but
  818. no destination address is specified, and the socket has not been
  819. connected;
  820. .It Bq Er ENOBUFS
  821. when the system runs out of memory for
  822. an internal data structure;
  823. .It Bq Er EADDRNOTAVAIL
  824. when an attempt is made to create a
  825. socket with a network address for which no network interface
  826. exists.
  827. .It Bq Er EACCES
  828. when an attempt is made to create
  829. a raw IP socket by a non-privileged process.
  830. .El
  831. .Pp
  832. The following errors specific to
  833. .Tn IP
  834. may occur when setting or getting
  835. .Tn IP
  836. options:
  837. .Bl -tag -width Er
  838. .It Bq Er EINVAL
  839. An unknown socket option name was given.
  840. .It Bq Er EINVAL
  841. The IP option field was improperly formed;
  842. an option field was shorter than the minimum value
  843. or longer than the option buffer provided.
  844. .El
  845. .Pp
  846. The following errors may occur when attempting to send
  847. .Tn IP
  848. datagrams via a
  849. .Dq raw socket
  850. with the
  851. .Dv IP_HDRINCL
  852. option set:
  853. .Bl -tag -width Er
  854. .It Bq Er EINVAL
  855. The user-supplied
  856. .Va ip_len
  857. field was not equal to the length of the datagram written to the socket.
  858. .El
  859. .Sh SEE ALSO
  860. .Xr getsockopt 2 ,
  861. .Xr recv 2 ,
  862. .Xr send 2 ,
  863. .Xr byteorder 3 ,
  864. .Xr icmp 4 ,
  865. .Xr igmp 4 ,
  866. .Xr inet 4 ,
  867. .Xr intro 4 ,
  868. .Xr multicast 4 ,
  869. .Xr sourcefilter 3
  870. .Rs
  871. .%A D. Thaler
  872. .%A B. Fenner
  873. .%A B. Quinn
  874. .%T "Socket Interface Extensions for Multicast Source Filters"
  875. .%N RFC 3678
  876. .%D Jan 2004
  877. .Re
  878. .Sh HISTORY
  879. The
  880. .Nm
  881. protocol appeared in
  882. .Bx 4.2 .
  883. The
  884. .Vt ip_mreqn
  885. structure appeared in
  886. .Tn Linux 2.4 .
  887. .Sh BUGS
  888. Before
  889. .Fx 10.0
  890. packets received on raw IP sockets had the
  891. .Va ip_hl
  892. subtracted from the
  893. .Va ip_len
  894. field.