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