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