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