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