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