xref: /freebsd/share/man/man4/ip.4 (revision 3ef51c5fb9163f2aafb1c14729e06a8bf0c4d113)
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 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.Tn IP
396multicasting is supported only on
397.Dv AF_INET
398sockets of type
399.Dv SOCK_DGRAM
400and
401.Dv SOCK_RAW ,
402and only on networks where the interface
403driver supports multicasting.
404.Pp
405The
406.Dv IP_MULTICAST_TTL
407option changes the time-to-live (TTL)
408for outgoing multicast datagrams
409in order to control the scope of the multicasts:
410.Bd -literal
411u_char ttl;	/* range: 0 to 255, default = 1 */
412setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
413.Ed
414.Pp
415Datagrams with a TTL of 1 are not forwarded beyond the local network.
416Multicast datagrams with a TTL of 0 will not be transmitted on any network,
417but may be delivered locally if the sending host belongs to the destination
418group and if multicast loopback has not been disabled on the sending socket
419(see below).
420Multicast datagrams with TTL greater than 1 may be forwarded
421to other networks if a multicast router is attached to the local network.
422.Pp
423For hosts with multiple interfaces, where an interface has not
424been specified for a multicast group membership,
425each multicast transmission is sent from the primary network interface.
426The
427.Dv IP_MULTICAST_IF
428option overrides the default for
429subsequent transmissions from a given socket:
430.Bd -literal
431struct in_addr addr;
432setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
433.Ed
434.Pp
435where "addr" is the local
436.Tn IP
437address of the desired interface or
438.Dv INADDR_ANY
439to specify the default interface.
440.Pp
441To specify an interface by index, an instance of
442.Vt ip_mreqn
443may be passed instead.
444The
445.Vt imr_ifindex
446member should be set to the index of the desired interface,
447or 0 to specify the default interface.
448The kernel differentiates between these two structures by their size.
449.Pp
450The use of
451.Vt IP_MULTICAST_IF
452is
453.Em not recommended ,
454as multicast memberships are scoped to each
455individual interface.
456It is supported for legacy use only by applications,
457such as routing daemons, which expect to
458be able to transmit link-local IPv4 multicast datagrams (224.0.0.0/24)
459on multiple interfaces,
460without requesting an individual membership for each interface.
461.Pp
462.\"
463An interface's local IP address and multicast capability can
464be obtained via the
465.Dv SIOCGIFCONF
466and
467.Dv SIOCGIFFLAGS
468ioctls.
469Normal applications should not need to use this option.
470.Pp
471If a multicast datagram is sent to a group to which the sending host itself
472belongs (on the outgoing interface), a copy of the datagram is, by default,
473looped back by the IP layer for local delivery.
474The
475.Dv IP_MULTICAST_LOOP
476option gives the sender explicit control
477over whether or not subsequent datagrams are looped back:
478.Bd -literal
479u_char loop;	/* 0 = disable, 1 = enable (default) */
480setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
481.Ed
482.Pp
483This option
484improves performance for applications that may have no more than one
485instance on a single host (such as a routing daemon), by eliminating
486the overhead of receiving their own transmissions.
487It should generally not
488be used by applications for which there may be more than one instance on a
489single host (such as a conferencing program) or for which the sender does
490not belong to the destination group (such as a time querying program).
491.Pp
492The sysctl setting
493.Va net.inet.ip.mcast.loop
494controls the default setting of the
495.Dv IP_MULTICAST_LOOP
496socket option for new sockets.
497.Pp
498A multicast datagram sent with an initial TTL greater than 1 may be delivered
499to the sending host on a different interface from that on which it was sent,
500if the host belongs to the destination group on that other interface.
501The loopback control option has no effect on such delivery.
502.Pp
503A host must become a member of a multicast group before it can receive
504datagrams sent to the group.
505To join a multicast group, use the
506.Dv IP_ADD_MEMBERSHIP
507option:
508.Bd -literal
509struct ip_mreq mreq;
510setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
511.Ed
512.Pp
513where
514.Fa mreq
515is the following structure:
516.Bd -literal
517struct ip_mreq {
518    struct in_addr imr_multiaddr; /* IP multicast address of group */
519    struct in_addr imr_interface; /* local IP address of interface */
520}
521.Ed
522.Pp
523.Va imr_interface
524should be set to the
525.Tn IP
526address of a particular multicast-capable interface if
527the host is multihomed.
528It may be set to
529.Dv INADDR_ANY
530to choose the default interface, although this is not recommended;
531this is considered to be the first interface corresponding
532to the default route.
533Otherwise, the first multicast-capable interface
534configured in the system will be used.
535.Pp
536Prior to
537.Fx 7.0 ,
538if the
539.Va imr_interface
540member is within the network range
541.Li 0.0.0.0/8 ,
542it is treated as an interface index in the system interface MIB,
543as per the RIP Version 2 MIB Extension (RFC-1724).
544In versions of
545.Fx
546since 7.0, this behavior is no longer supported.
547Developers should
548instead use the RFC 3678 multicast source filter APIs; in particular,
549.Dv MCAST_JOIN_GROUP .
550.Pp
551Up to
552.Dv IP_MAX_MEMBERSHIPS
553memberships may be added on a single socket.
554Membership is associated with a single interface;
555programs running on multihomed hosts may need to
556join the same group on more than one interface.
557.Pp
558To drop a membership, use:
559.Bd -literal
560struct ip_mreq mreq;
561setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
562.Ed
563.Pp
564where
565.Fa mreq
566contains the same values as used to add the membership.
567Memberships are dropped when the socket is closed or the process exits.
568.\" TODO: Update this piece when IPv4 source-address selection is implemented.
569.Pp
570The IGMP protocol uses the primary IP address of the interface
571as its identifier for group membership.
572This is the first IP address configured on the interface.
573If this address is removed or changed, the results are
574undefined, as the IGMP membership state will then be inconsistent.
575If multiple IP aliases are configured on the same interface,
576they will be ignored.
577.Pp
578This shortcoming was addressed in IPv6; MLDv2 requires
579that the unique link-local address for an interface is
580used to identify an MLDv2 listener.
581.Ss "Source-Specific Multicast Options"
582Since
583.Fx 8.0 ,
584the use of Source-Specific Multicast (SSM) is supported.
585These extensions require an IGMPv3 multicast router in order to
586make best use of them.
587If a legacy multicast router is present on the link,
588.Fx
589will simply downgrade to the version of IGMP spoken by the router,
590and the benefits of source filtering on the upstream link
591will not be present, although the kernel will continue to
592squelch transmissions from blocked sources.
593.Pp
594Each group membership on a socket now has a filter mode:
595.Bl -tag -width MCAST_EXCLUDE
596.It Dv MCAST_EXCLUDE
597Datagrams sent to this group are accepted,
598unless the source is in a list of blocked source addresses.
599.It Dv MCAST_INCLUDE
600Datagrams sent to this group are accepted
601only if the source is in a list of accepted source addresses.
602.El
603.Pp
604Groups joined using the legacy
605.Dv IP_ADD_MEMBERSHIP
606option are placed in exclusive-mode,
607and are able to request that certain sources are blocked or allowed.
608This is known as the
609.Em delta-based API .
610.Pp
611To block a multicast source on an existing group membership:
612.Bd -literal
613struct ip_mreq_source mreqs;
614setsockopt(s, IPPROTO_IP, IP_BLOCK_SOURCE, &mreqs, sizeof(mreqs));
615.Ed
616.Pp
617where
618.Fa mreqs
619is the following structure:
620.Bd -literal
621struct ip_mreq_source {
622    struct in_addr imr_multiaddr; /* IP multicast address of group */
623    struct in_addr imr_sourceaddr; /* IP address of source */
624    struct in_addr imr_interface; /* local IP address of interface */
625}
626.Ed
627.Va imr_sourceaddr
628should be set to the address of the source to be blocked.
629.Pp
630To unblock a multicast source on an existing group:
631.Bd -literal
632struct ip_mreq_source mreqs;
633setsockopt(s, IPPROTO_IP, IP_UNBLOCK_SOURCE, &mreqs, sizeof(mreqs));
634.Ed
635.Pp
636The
637.Dv IP_BLOCK_SOURCE
638and
639.Dv IP_UNBLOCK_SOURCE
640options are
641.Em not permitted
642for inclusive-mode group memberships.
643.Pp
644To join a multicast group in
645.Dv MCAST_INCLUDE
646mode with a single source,
647or add another source to an existing inclusive-mode membership:
648.Bd -literal
649struct ip_mreq_source mreqs;
650setsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
651.Ed
652.Pp
653To leave a single source from an existing group in inclusive mode:
654.Bd -literal
655struct ip_mreq_source mreqs;
656setsockopt(s, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
657.Ed
658If this is the last accepted source for the group, the membership
659will be dropped.
660.Pp
661The
662.Dv IP_ADD_SOURCE_MEMBERSHIP
663and
664.Dv IP_DROP_SOURCE_MEMBERSHIP
665options are
666.Em not accepted
667for exclusive-mode group memberships.
668However, both exclusive and inclusive mode memberships
669support the use of the
670.Em full-state API
671documented in RFC 3678.
672For management of source filter lists using this API,
673please refer to
674.Xr sourcefilter 3 .
675.Pp
676The sysctl settings
677.Va net.inet.ip.mcast.maxsocksrc
678and
679.Va net.inet.ip.mcast.maxgrpsrc
680are used to specify an upper limit on the number of per-socket and per-group
681source filter entries which the kernel may allocate.
682.\"-----------------------
683.Ss "Raw IP Sockets"
684Raw
685.Tn IP
686sockets are connectionless,
687and are normally used with the
688.Xr sendto 2
689and
690.Xr recvfrom 2
691calls, though the
692.Xr connect 2
693call may also be used to fix the destination for future
694packets (in which case the
695.Xr read 2
696or
697.Xr recv 2
698and
699.Xr write 2
700or
701.Xr send 2
702system calls may be used).
703.Pp
704If
705.Fa proto
706is 0, the default protocol
707.Dv IPPROTO_RAW
708is used for outgoing
709packets, and only incoming packets destined for that protocol
710are received.
711If
712.Fa proto
713is non-zero, that protocol number will be used on outgoing packets
714and to filter incoming packets.
715.Pp
716Outgoing packets automatically have an
717.Tn IP
718header prepended to
719them (based on the destination address and the protocol
720number the socket is created with),
721unless the
722.Dv IP_HDRINCL
723option has been set.
724Incoming packets are received with
725.Tn IP
726header and options intact.
727.Pp
728.Dv IP_HDRINCL
729indicates the complete IP header is included with the data
730and may be used only with the
731.Dv SOCK_RAW
732type.
733.Bd -literal
734#include <netinet/in_systm.h>
735#include <netinet/ip.h>
736
737int hincl = 1;                  /* 1 = on, 0 = off */
738setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
739.Ed
740.Pp
741Unlike previous
742.Bx
743releases, the program must set all
744the fields of the IP header, including the following:
745.Bd -literal
746ip->ip_v = IPVERSION;
747ip->ip_hl = hlen >> 2;
748ip->ip_id = 0;  /* 0 means kernel set appropriate value */
749ip->ip_off = offset;
750.Ed
751.Pp
752The
753.Va ip_len
754and
755.Va ip_off
756fields
757.Em must
758be provided in host byte order.
759All other fields must be provided in network byte order.
760See
761.Xr byteorder 3
762for more information on network byte order.
763If the
764.Va ip_id
765field is set to 0 then the kernel will choose an
766appropriate value.
767If the header source address is set to
768.Dv INADDR_ANY ,
769the kernel will choose an appropriate address.
770.Sh ERRORS
771A socket operation may fail with one of the following errors returned:
772.Bl -tag -width Er
773.It Bq Er EISCONN
774when trying to establish a connection on a socket which
775already has one, or when trying to send a datagram with the destination
776address specified and the socket is already connected;
777.It Bq Er ENOTCONN
778when trying to send a datagram, but
779no destination address is specified, and the socket has not been
780connected;
781.It Bq Er ENOBUFS
782when the system runs out of memory for
783an internal data structure;
784.It Bq Er EADDRNOTAVAIL
785when an attempt is made to create a
786socket with a network address for which no network interface
787exists.
788.It Bq Er EACCES
789when an attempt is made to create
790a raw IP socket by a non-privileged process.
791.El
792.Pp
793The following errors specific to
794.Tn IP
795may occur when setting or getting
796.Tn IP
797options:
798.Bl -tag -width Er
799.It Bq Er EINVAL
800An unknown socket option name was given.
801.It Bq Er EINVAL
802The IP option field was improperly formed;
803an option field was shorter than the minimum value
804or longer than the option buffer provided.
805.El
806.Pp
807The following errors may occur when attempting to send
808.Tn IP
809datagrams via a
810.Dq raw socket
811with the
812.Dv IP_HDRINCL
813option set:
814.Bl -tag -width Er
815.It Bq Er EINVAL
816The user-supplied
817.Va ip_len
818field was not equal to the length of the datagram written to the socket.
819.El
820.Sh SEE ALSO
821.Xr getsockopt 2 ,
822.Xr recv 2 ,
823.Xr send 2 ,
824.Xr byteorder 3 ,
825.Xr icmp 4 ,
826.Xr igmp 4 ,
827.Xr inet 4 ,
828.Xr intro 4 ,
829.Xr multicast 4 ,
830.Xr sourcefilter 3
831.Rs
832.%A D. Thaler
833.%A B. Fenner
834.%A B. Quinn
835.%T "Socket Interface Extensions for Multicast Source Filters"
836.%N RFC 3678
837.%D Jan 2004
838.Re
839.Sh HISTORY
840The
841.Nm
842protocol appeared in
843.Bx 4.2 .
844The
845.Vt ip_mreqn
846structure appeared in
847.Tn Linux 2.4 .
848.Sh BUGS
849Before
850.Fx 10.0
851packets received on raw IP sockets had the
852.Va ip_hl
853subtracted from the
854.Va ip_len field.
855