xref: /freebsd/share/man/man4/ip.4 (revision 108ad5bd51bf0c2f8ce08a0c1126918984cbf08d)
1afe61c15SRodney W. Grimes.\" Copyright (c) 1983, 1991, 1993
2afe61c15SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
3afe61c15SRodney W. Grimes.\"
4afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
5afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions
6afe61c15SRodney W. Grimes.\" are met:
7afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
8afe61c15SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
9afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
10afe61c15SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
11afe61c15SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
12afe61c15SRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
13afe61c15SRodney W. Grimes.\"    must display the following acknowledgement:
14afe61c15SRodney W. Grimes.\"	This product includes software developed by the University of
15afe61c15SRodney W. Grimes.\"	California, Berkeley and its contributors.
16afe61c15SRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
17afe61c15SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
18afe61c15SRodney W. Grimes.\"    without specific prior written permission.
19afe61c15SRodney W. Grimes.\"
20afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23afe61c15SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30afe61c15SRodney W. Grimes.\" SUCH DAMAGE.
31afe61c15SRodney W. Grimes.\"
32afe61c15SRodney W. Grimes.\"     @(#)ip.4	8.2 (Berkeley) 11/30/93
337f3dea24SPeter Wemm.\" $FreeBSD$
34afe61c15SRodney W. Grimes.\"
35108ad5bdSBruce M Simpson.Dd February 13, 2009
36afe61c15SRodney W. Grimes.Dt IP 4
378ce3f3ddSRuslan Ermilov.Os
38afe61c15SRodney W. Grimes.Sh NAME
39afe61c15SRodney W. Grimes.Nm ip
40afe61c15SRodney W. Grimes.Nd Internet Protocol
41afe61c15SRodney W. Grimes.Sh SYNOPSIS
4232eef9aeSRuslan Ermilov.In sys/types.h
4332eef9aeSRuslan Ermilov.In sys/socket.h
4432eef9aeSRuslan Ermilov.In netinet/in.h
45afe61c15SRodney W. Grimes.Ft int
46afe61c15SRodney W. Grimes.Fn socket AF_INET SOCK_RAW proto
47afe61c15SRodney W. Grimes.Sh DESCRIPTION
48afe61c15SRodney W. Grimes.Tn IP
49afe61c15SRodney W. Grimesis the transport layer protocol used
50afe61c15SRodney W. Grimesby the Internet protocol family.
51afe61c15SRodney W. GrimesOptions may be set at the
52afe61c15SRodney W. Grimes.Tn IP
53afe61c15SRodney W. Grimeslevel
54afe61c15SRodney W. Grimeswhen using higher-level protocols that are based on
55afe61c15SRodney W. Grimes.Tn IP
56afe61c15SRodney W. Grimes(such as
57afe61c15SRodney W. Grimes.Tn TCP
58afe61c15SRodney W. Grimesand
59afe61c15SRodney W. Grimes.Tn UDP ) .
60afe61c15SRodney W. GrimesIt may also be accessed
61afe61c15SRodney W. Grimesthrough a
62afe61c15SRodney W. Grimes.Dq raw socket
63afe61c15SRodney W. Grimeswhen developing new protocols, or
64afe61c15SRodney W. Grimesspecial-purpose applications.
65afe61c15SRodney W. Grimes.Pp
66afe61c15SRodney W. GrimesThere are several
67afe61c15SRodney W. Grimes.Tn IP-level
68edf0e5b3SMike Pritchard.Xr setsockopt 2
69edf0e5b3SMike Pritchardand
70afe61c15SRodney W. Grimes.Xr getsockopt 2
71afe61c15SRodney W. Grimesoptions.
72afe61c15SRodney W. Grimes.Dv IP_OPTIONS
73afe61c15SRodney W. Grimesmay be used to provide
74afe61c15SRodney W. Grimes.Tn IP
75afe61c15SRodney W. Grimesoptions to be transmitted in the
76afe61c15SRodney W. Grimes.Tn IP
77afe61c15SRodney W. Grimesheader of each outgoing packet
78afe61c15SRodney W. Grimesor to examine the header options on incoming packets.
79afe61c15SRodney W. Grimes.Tn IP
80afe61c15SRodney W. Grimesoptions may be used with any socket type in the Internet family.
81afe61c15SRodney W. GrimesThe format of
82afe61c15SRodney W. Grimes.Tn IP
83afe61c15SRodney W. Grimesoptions to be sent is that specified by the
84afe61c15SRodney W. Grimes.Tn IP
85afe61c15SRodney W. Grimesprotocol specification (RFC-791), with one exception:
86afe61c15SRodney W. Grimesthe list of addresses for Source Route options must include the first-hop
87afe61c15SRodney W. Grimesgateway at the beginning of the list of gateways.
88afe61c15SRodney W. GrimesThe first-hop gateway address will be extracted from the option list
89afe61c15SRodney W. Grimesand the size adjusted accordingly before use.
90afe61c15SRodney W. GrimesTo disable previously specified options,
91afe61c15SRodney W. Grimesuse a zero-length buffer:
92afe61c15SRodney W. Grimes.Bd -literal
93afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0);
94afe61c15SRodney W. Grimes.Ed
95afe61c15SRodney W. Grimes.Pp
96afe61c15SRodney W. Grimes.Dv IP_TOS
97afe61c15SRodney W. Grimesand
98afe61c15SRodney W. Grimes.Dv IP_TTL
99afe61c15SRodney W. Grimesmay be used to set the type-of-service and time-to-live
100afe61c15SRodney W. Grimesfields in the
101afe61c15SRodney W. Grimes.Tn IP
102afe61c15SRodney W. Grimesheader for
1038ce3f3ddSRuslan Ermilov.Dv SOCK_STREAM , SOCK_DGRAM ,
1048ce3f3ddSRuslan Ermilovand certain types of
1058ce3f3ddSRuslan Ermilov.Dv SOCK_RAW
1066d249eeeSSheldon Hearnsockets.
1076d249eeeSSheldon HearnFor example,
108afe61c15SRodney W. Grimes.Bd -literal
109a7c83731SRuslan Ermilovint tos = IPTOS_LOWDELAY;       /* see <netinet/ip.h> */
110afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
111afe61c15SRodney W. Grimes
112afe61c15SRodney W. Grimesint ttl = 60;                   /* max = 255 */
113afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
114afe61c15SRodney W. Grimes.Ed
115afe61c15SRodney W. Grimes.Pp
1165090c754SAndre Oppermann.Dv IP_MINTTL
1175090c754SAndre Oppermannmay be used to set the minimum acceptable TTL a packet must have when
1185090c754SAndre Oppermannreceived on a socket.
1195090c754SAndre OppermannAll packets with a lower TTL are silently dropped.
12078ad5421SRuslan ErmilovThis option is only really useful when set to 255, preventing packets
1215090c754SAndre Oppermannfrom outside the directly connected networks reaching local listeners
1225090c754SAndre Oppermannon sockets.
1235090c754SAndre Oppermann.Pp
124c9ebaafaSAndre Oppermann.Dv IP_DONTFRAG
125c9ebaafaSAndre Oppermannmay be used to set the Don't Fragment flag on IP packets.
126c9ebaafaSAndre OppermannCurrently this option is respected only on
127c9ebaafaSAndre Oppermann.Xr udp 4
12878ad5421SRuslan Ermilovand raw
129c9ebaafaSAndre Oppermann.Xr ip 4
13078ad5421SRuslan Ermilovsockets, unless the
13178ad5421SRuslan Ermilov.Dv IP_HDRINCL
13278ad5421SRuslan Ermilovoption has been set.
133c9ebaafaSAndre OppermannOn
134c9ebaafaSAndre Oppermann.Xr tcp 4
13578ad5421SRuslan Ermilovsockets, the Don't Fragment flag is controlled by the Path
136c9ebaafaSAndre OppermannMTU Discovery option.
137c9ebaafaSAndre OppermannSending a packet larger than the MTU size of the egress interface,
13878ad5421SRuslan Ermilovdetermined by the destination address, returns an
13978ad5421SRuslan Ermilov.Er EMSGSIZE
14078ad5421SRuslan Ermiloverror.
141c9ebaafaSAndre Oppermann.Pp
142afe61c15SRodney W. GrimesIf the
143afe61c15SRodney W. Grimes.Dv IP_RECVDSTADDR
144afe61c15SRodney W. Grimesoption is enabled on a
145afe61c15SRodney W. Grimes.Dv SOCK_DGRAM
146afe61c15SRodney W. Grimessocket,
147afe61c15SRodney W. Grimesthe
148edf0e5b3SMike Pritchard.Xr recvmsg 2
149afe61c15SRodney W. Grimescall will return the destination
150afe61c15SRodney W. Grimes.Tn IP
151afe61c15SRodney W. Grimesaddress for a
152afe61c15SRodney W. Grimes.Tn UDP
153afe61c15SRodney W. Grimesdatagram.
1548d22ff75SRuslan ErmilovThe
1558d22ff75SRuslan Ermilov.Vt msg_control
1568d22ff75SRuslan Ermilovfield in the
1578d22ff75SRuslan Ermilov.Vt msghdr
1588d22ff75SRuslan Ermilovstructure points to a buffer
1598d22ff75SRuslan Ermilovthat contains a
1608d22ff75SRuslan Ermilov.Vt cmsghdr
1618d22ff75SRuslan Ermilovstructure followed by the
162afe61c15SRodney W. Grimes.Tn IP
163afe61c15SRodney W. Grimesaddress.
1648d22ff75SRuslan ErmilovThe
1658d22ff75SRuslan Ermilov.Vt cmsghdr
1668d22ff75SRuslan Ermilovfields have the following values:
167afe61c15SRodney W. Grimes.Bd -literal
168afe61c15SRodney W. Grimescmsg_len = sizeof(struct in_addr)
169afe61c15SRodney W. Grimescmsg_level = IPPROTO_IP
170afe61c15SRodney W. Grimescmsg_type = IP_RECVDSTADDR
171afe61c15SRodney W. Grimes.Ed
1727ee32b9fSPeter Wemm.Pp
173c557ae16SIan DowseThe source address to be used for outgoing
174c557ae16SIan Dowse.Tn UDP
175c557ae16SIan Dowsedatagrams on a socket that is not bound to a specific
176c557ae16SIan Dowse.Tn IP
177c557ae16SIan Dowseaddress can be specified as ancillary data with a type code of
178c557ae16SIan Dowse.Dv IP_SENDSRCADDR .
179c557ae16SIan DowseThe msg_control field in the msghdr structure should point to a buffer
1808d22ff75SRuslan Ermilovthat contains a
1818d22ff75SRuslan Ermilov.Vt cmsghdr
1828d22ff75SRuslan Ermilovstructure followed by the
183c557ae16SIan Dowse.Tn IP
184c557ae16SIan Dowseaddress.
185c557ae16SIan DowseThe cmsghdr fields should have the following values:
186c557ae16SIan Dowse.Bd -literal
187c557ae16SIan Dowsecmsg_len = sizeof(struct in_addr)
188c557ae16SIan Dowsecmsg_level = IPPROTO_IP
189c557ae16SIan Dowsecmsg_type = IP_SENDSRCADDR
190c557ae16SIan Dowse.Ed
191c557ae16SIan Dowse.Pp
192c557ae16SIan DowseFor convenience,
193c557ae16SIan Dowse.Dv IP_SENDSRCADDR
194c557ae16SIan Dowseis defined to have the same value as
195c557ae16SIan Dowse.Dv IP_RECVDSTADDR ,
196c557ae16SIan Dowseso the
197c557ae16SIan Dowse.Dv IP_RECVDSTADDR
198c557ae16SIan Dowsecontrol message from
199c557ae16SIan Dowse.Xr recvmsg 2
200c557ae16SIan Dowsecan be used directly as a control message for
201c557ae16SIan Dowse.Xr sendmsg 2 .
20204bcf5a9SBruce M Simpson.\"
203c557ae16SIan Dowse.Pp
2043b5f87e6SMatthew N. DoddIf the
2058afa2304SBruce M Simpson.Dv IP_ONESBCAST
2068afa2304SBruce M Simpsonoption is enabled on a
2078afa2304SBruce M Simpson.Dv SOCK_DGRAM
2088afa2304SBruce M Simpsonor a
2098afa2304SBruce M Simpson.Dv SOCK_RAW
2108afa2304SBruce M Simpsonsocket, the destination address of outgoing
2118afa2304SBruce M Simpsonbroadcast datagrams on that socket will be forced
2128afa2304SBruce M Simpsonto the undirected broadcast address,
2138afa2304SBruce M Simpson.Dv INADDR_BROADCAST ,
2148afa2304SBruce M Simpsonbefore transmission.
2158afa2304SBruce M SimpsonThis is in contrast to the default behavior of the
2168afa2304SBruce M Simpsonsystem, which is to transmit undirected broadcasts
2178afa2304SBruce M Simpsonvia the first network interface with the
2188afa2304SBruce M Simpson.Dv IFF_BROADCAST flag set.
2198afa2304SBruce M Simpson.Pp
2208afa2304SBruce M SimpsonThis option allows applications to choose which
2218afa2304SBruce M Simpsoninterface is used to transmit an undirected broadcast
2228afa2304SBruce M Simpsondatagram.
2238afa2304SBruce M SimpsonFor example, the following code would force an
2248afa2304SBruce M Simpsonundirected broadcast to be transmitted via the interface
2258afa2304SBruce M Simpsonconfigured with the broadcast address 192.168.2.255:
2268afa2304SBruce M Simpson.Bd -literal
2278afa2304SBruce M Simpsonchar msg[512];
2288afa2304SBruce M Simpsonstruct sockaddr_in sin;
2298afa2304SBruce M Simpsonu_char onesbcast = 1;	/* 0 = disable (default), 1 = enable */
2308afa2304SBruce M Simpson
2318afa2304SBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_ONESBCAST, &onesbcast, sizeof(onesbcast));
2328afa2304SBruce M Simpsonsin.sin_addr.s_addr = inet_addr("192.168.2.255");
2338afa2304SBruce M Simpsonsin.sin_port = htons(1234);
2348afa2304SBruce M Simpsonsendto(s, msg, sizeof(msg), 0, &sin, sizeof(sin));
2358afa2304SBruce M Simpson.Ed
2368afa2304SBruce M Simpson.Pp
2378afa2304SBruce M SimpsonIt is the application's responsibility to set the
2388afa2304SBruce M Simpson.Dv IP_TTL option
2398afa2304SBruce M Simpsonto an appropriate value in order to prevent broadcast storms.
2408afa2304SBruce M SimpsonThe application must have sufficient credentials to set the
2418afa2304SBruce M Simpson.Dv SO_BROADCAST
2428afa2304SBruce M Simpsonsocket level option, otherwise the
2438afa2304SBruce M Simpson.Dv IP_ONESBCAST option has no effect.
2448afa2304SBruce M Simpson.Pp
2458afa2304SBruce M SimpsonIf the
2464957466bSMatthew N. Dodd.Dv IP_RECVTTL
2474957466bSMatthew N. Doddoption is enabled on a
2484957466bSMatthew N. Dodd.Dv SOCK_DGRAM
2494957466bSMatthew N. Doddsocket, the
2504957466bSMatthew N. Dodd.Xr recvmsg 2
2514957466bSMatthew N. Doddcall will return the
2524957466bSMatthew N. Dodd.Tn IP
2534957466bSMatthew N. Dodd.Tn TTL
2544957466bSMatthew N. Dodd(time to live) field for a
2554957466bSMatthew N. Dodd.Tn UDP
2564957466bSMatthew N. Dodddatagram.
2574957466bSMatthew N. DoddThe msg_control field in the msghdr structure points to a buffer
2584957466bSMatthew N. Doddthat contains a cmsghdr structure followed by the
2594957466bSMatthew N. Dodd.Tn TTL .
2604957466bSMatthew N. DoddThe cmsghdr fields have the following values:
2614957466bSMatthew N. Dodd.Bd -literal
2624957466bSMatthew N. Doddcmsg_len = sizeof(u_char)
2634957466bSMatthew N. Doddcmsg_level = IPPROTO_IP
2644957466bSMatthew N. Doddcmsg_type = IP_RECVTTL
2654957466bSMatthew N. Dodd.Ed
26604bcf5a9SBruce M Simpson.\"
2674957466bSMatthew N. Dodd.Pp
2684957466bSMatthew N. DoddIf the
2693b5f87e6SMatthew N. Dodd.Dv IP_RECVIF
2703b5f87e6SMatthew N. Doddoption is enabled on a
2713b5f87e6SMatthew N. Dodd.Dv SOCK_DGRAM
2723b5f87e6SMatthew N. Doddsocket, the
2733b5f87e6SMatthew N. Dodd.Xr recvmsg 2
2748d22ff75SRuslan Ermilovcall returns a
2758d22ff75SRuslan Ermilov.Vt "struct sockaddr_dl"
2768d22ff75SRuslan Ermilovcorresponding to the interface on which the
2778d22ff75SRuslan Ermilovpacket was received.
2788d22ff75SRuslan ErmilovThe
2798d22ff75SRuslan Ermilov.Va msg_control
2808d22ff75SRuslan Ermilovfield in the
2818d22ff75SRuslan Ermilov.Vt msghdr
2828d22ff75SRuslan Ermilovstructure points to a buffer that contains a
2838d22ff75SRuslan Ermilov.Vt cmsghdr
2848d22ff75SRuslan Ermilovstructure followed by the
2858d22ff75SRuslan Ermilov.Vt "struct sockaddr_dl" .
2868d22ff75SRuslan ErmilovThe
2878d22ff75SRuslan Ermilov.Vt cmsghdr
2888d22ff75SRuslan Ermilovfields have the following values:
2893b5f87e6SMatthew N. Dodd.Bd -literal
2903b5f87e6SMatthew N. Doddcmsg_len = sizeof(struct sockaddr_dl)
2913b5f87e6SMatthew N. Doddcmsg_level = IPPROTO_IP
2923b5f87e6SMatthew N. Doddcmsg_type = IP_RECVIF
2933b5f87e6SMatthew N. Dodd.Ed
2943b5f87e6SMatthew N. Dodd.Pp
2957ee32b9fSPeter Wemm.Dv IP_PORTRANGE
2967ee32b9fSPeter Wemmmay be used to set the port range used for selecting a local port number
2976d249eeeSSheldon Hearnon a socket with an unspecified (zero) port number.
2986d249eeeSSheldon HearnIt has the following
2997ee32b9fSPeter Wemmpossible values:
3007ee32b9fSPeter Wemm.Bl -tag -width IP_PORTRANGE_DEFAULT
3017ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_DEFAULT
3027ee32b9fSPeter Wemmuse the default range of values, normally
30374c0361aSMike Silbersack.Dv IPPORT_HIFIRSTAUTO
3047ee32b9fSPeter Wemmthrough
30574c0361aSMike Silbersack.Dv IPPORT_HILASTAUTO .
3067ee32b9fSPeter WemmThis is adjustable through the sysctl setting:
307ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.first
3087ee32b9fSPeter Wemmand
309ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.last .
3107ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_HIGH
3117ee32b9fSPeter Wemmuse a high range of values, normally
3127ee32b9fSPeter Wemm.Dv IPPORT_HIFIRSTAUTO
3137ee32b9fSPeter Wemmand
3147ee32b9fSPeter Wemm.Dv IPPORT_HILASTAUTO .
3157ee32b9fSPeter WemmThis is adjustable through the sysctl setting:
316ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.hifirst
3177ee32b9fSPeter Wemmand
318ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.hilast .
3197ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_LOW
3207ee32b9fSPeter Wemmuse a low range of ports, which are normally restricted to
3217ee32b9fSPeter Wemmprivileged processes on
3227ee32b9fSPeter Wemm.Ux
3238d22ff75SRuslan Ermilovsystems.
3248d22ff75SRuslan ErmilovThe range is normally from
325ace5be68SRuslan Ermilov.Dv IPPORT_RESERVED
326ace5be68SRuslan Ermilov\- 1 down to
3278f5e8232SBen Smithurst.Li IPPORT_RESERVEDSTART
3288f5e8232SBen Smithurstin descending order.
3298f5e8232SBen SmithurstThis is adjustable through the sysctl setting:
330ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.lowfirst
3318f5e8232SBen Smithurstand
332ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.lowlast .
3337ee32b9fSPeter Wemm.El
334718b17bbSCrist J. Clark.Pp
335718b17bbSCrist J. ClarkThe range of privileged ports which only may be opened by
336718b17bbSCrist J. Clarkroot-owned processes may be modified by the
337ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.reservedlow
338718b17bbSCrist J. Clarkand
339ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.reservedhigh
340718b17bbSCrist J. Clarksysctl settings.
341718b17bbSCrist J. ClarkThe values default to the traditional range,
342718b17bbSCrist J. Clark0 through
343ace5be68SRuslan Ermilov.Dv IPPORT_RESERVED
344ace5be68SRuslan Ermilov\- 1
345718b17bbSCrist J. Clark(0 through 1023), respectively.
346718b17bbSCrist J. ClarkNote that these settings do not affect and are not accounted for in the
347718b17bbSCrist J. Clarkuse or calculation of the other
348ace5be68SRuslan Ermilov.Va net.inet.ip.portrange
349718b17bbSCrist J. Clarkvalues above.
350ace5be68SRuslan ErmilovChanging these values departs from
351ace5be68SRuslan Ermilov.Ux
352ace5be68SRuslan Ermilovtradition and has security
353ace5be68SRuslan Ermilovconsequences that the administrator should carefully evaluate before
354718b17bbSCrist J. Clarkmodifying these settings.
355fe9892eaSMike Silbersack.Pp
356fe9892eaSMike SilbersackPorts are allocated at random within the specified port range in order
357bf7f20c2SRuslan Ermilovto increase the difficulty of random spoofing attacks.
358c48524c2SMike PritchardIn scenarios such as benchmarking, this behavior may be undesirable.
359bf7f20c2SRuslan ErmilovIn these cases,
360fe9892eaSMike Silbersack.Va net.inet.ip.portrange.randomized
361fe9892eaSMike Silbersackcan be used to toggle randomization off.
3626ee79c59SMaxim KonovalovIf more than
3636ee79c59SMaxim Konovalov.Va net.inet.ip.portrange.randomcps
3646ee79c59SMaxim Konovalovports have been allocated in the last second, then return to sequential
3656ee79c59SMaxim Konovalovport allocation.
3666ee79c59SMaxim KonovalovReturn to random allocation only once the current port allocation rate
3676ee79c59SMaxim Konovalovdrops below
3686ee79c59SMaxim Konovalov.Va net.inet.ip.portrange.randomcps
3696ee79c59SMaxim Konovalovfor at least
3706ee79c59SMaxim Konovalov.Va net.inet.ip.portrange.randomtime
3716ee79c59SMaxim Konovalovseconds.
3726ee79c59SMaxim KonovalovThe default values for
3736ee79c59SMaxim Konovalov.Va net.inet.ip.portrange.randomcps
3746ee79c59SMaxim Konovalovand
3756ee79c59SMaxim Konovalov.Va net.inet.ip.portrange.randomtime
3766ee79c59SMaxim Konovalovare 10 port allocations per second and 45 seconds correspondingly.
377afe61c15SRodney W. Grimes.Ss "Multicast Options"
378afe61c15SRodney W. Grimes.Pp
379afe61c15SRodney W. Grimes.Tn IP
380afe61c15SRodney W. Grimesmulticasting is supported only on
381afe61c15SRodney W. Grimes.Dv AF_INET
382afe61c15SRodney W. Grimessockets of type
383afe61c15SRodney W. Grimes.Dv SOCK_DGRAM
384afe61c15SRodney W. Grimesand
385afe61c15SRodney W. Grimes.Dv SOCK_RAW ,
386afe61c15SRodney W. Grimesand only on networks where the interface
387afe61c15SRodney W. Grimesdriver supports multicasting.
388afe61c15SRodney W. Grimes.Pp
389afe61c15SRodney W. GrimesThe
390afe61c15SRodney W. Grimes.Dv IP_MULTICAST_TTL
391afe61c15SRodney W. Grimesoption changes the time-to-live (TTL)
392afe61c15SRodney W. Grimesfor outgoing multicast datagrams
393afe61c15SRodney W. Grimesin order to control the scope of the multicasts:
394afe61c15SRodney W. Grimes.Bd -literal
395afe61c15SRodney W. Grimesu_char ttl;	/* range: 0 to 255, default = 1 */
396afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
397afe61c15SRodney W. Grimes.Ed
3980c3a1746SMike Pritchard.Pp
399afe61c15SRodney W. GrimesDatagrams with a TTL of 1 are not forwarded beyond the local network.
400afe61c15SRodney W. GrimesMulticast datagrams with a TTL of 0 will not be transmitted on any network,
401afe61c15SRodney W. Grimesbut may be delivered locally if the sending host belongs to the destination
402afe61c15SRodney W. Grimesgroup and if multicast loopback has not been disabled on the sending socket
4038d22ff75SRuslan Ermilov(see below).
4048d22ff75SRuslan ErmilovMulticast datagrams with TTL greater than 1 may be forwarded
405afe61c15SRodney W. Grimesto other networks if a multicast router is attached to the local network.
406afe61c15SRodney W. Grimes.Pp
407108ad5bdSBruce M SimpsonFor hosts with multiple interfaces, where an interface has not
408108ad5bdSBruce M Simpsonbeen specified for a multicast group membership,
409108ad5bdSBruce M Simpsoneach multicast transmission is sent from the primary network interface.
410afe61c15SRodney W. GrimesThe
411afe61c15SRodney W. Grimes.Dv IP_MULTICAST_IF
412afe61c15SRodney W. Grimesoption overrides the default for
413afe61c15SRodney W. Grimessubsequent transmissions from a given socket:
414afe61c15SRodney W. Grimes.Bd -literal
415afe61c15SRodney W. Grimesstruct in_addr addr;
416afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
417afe61c15SRodney W. Grimes.Ed
4180c3a1746SMike Pritchard.Pp
419afe61c15SRodney W. Grimeswhere "addr" is the local
420afe61c15SRodney W. Grimes.Tn IP
421afe61c15SRodney W. Grimesaddress of the desired interface or
422afe61c15SRodney W. Grimes.Dv INADDR_ANY
423afe61c15SRodney W. Grimesto specify the default interface.
42471498f30SBruce M Simpson.Pp
42571498f30SBruce M SimpsonTo specify an interface by index, an instance of
42671498f30SBruce M Simpson.Vt ip_mreqn
427108ad5bdSBruce M Simpsonmay be passed instead.
42871498f30SBruce M SimpsonThe
42971498f30SBruce M Simpson.Vt imr_ifindex
43071498f30SBruce M Simpsonmember should be set to the index of the desired interface,
43171498f30SBruce M Simpsonor 0 to specify the default interface.
43271498f30SBruce M SimpsonThe kernel differentiates between these two structures by their size.
433108ad5bdSBruce M Simpson.Pp
434108ad5bdSBruce M SimpsonThe use of
435108ad5bdSBruce M Simpson.Vt IP_MULTICAST_IF
436108ad5bdSBruce M Simpsonis
437108ad5bdSBruce M Simpson.Em not recommended ,
438108ad5bdSBruce M Simpsonas multicast memberships are scoped to each
439108ad5bdSBruce M Simpsonindividual interface.
440108ad5bdSBruce M SimpsonIt is supported for legacy use only by applications,
441108ad5bdSBruce M Simpsonsuch as routing daemons, which expect to
442108ad5bdSBruce M Simpsonbe able to transmit link-local IPv4 multicast datagrams (224.0.0.0/24)
443108ad5bdSBruce M Simpsonon multiple interfaces,
444108ad5bdSBruce M Simpsonwithout requesting an individual membership for each interface.
445108ad5bdSBruce M Simpson.Pp
44671498f30SBruce M Simpson.\"
447afe61c15SRodney W. GrimesAn interface's local IP address and multicast capability can
448afe61c15SRodney W. Grimesbe obtained via the
449afe61c15SRodney W. Grimes.Dv SIOCGIFCONF
450afe61c15SRodney W. Grimesand
451afe61c15SRodney W. Grimes.Dv SIOCGIFFLAGS
452afe61c15SRodney W. Grimesioctls.
453afe61c15SRodney W. GrimesNormal applications should not need to use this option.
454afe61c15SRodney W. Grimes.Pp
455afe61c15SRodney W. GrimesIf a multicast datagram is sent to a group to which the sending host itself
456afe61c15SRodney W. Grimesbelongs (on the outgoing interface), a copy of the datagram is, by default,
457afe61c15SRodney W. Grimeslooped back by the IP layer for local delivery.
458afe61c15SRodney W. GrimesThe
459afe61c15SRodney W. Grimes.Dv IP_MULTICAST_LOOP
460afe61c15SRodney W. Grimesoption gives the sender explicit control
461afe61c15SRodney W. Grimesover whether or not subsequent datagrams are looped back:
462afe61c15SRodney W. Grimes.Bd -literal
463afe61c15SRodney W. Grimesu_char loop;	/* 0 = disable, 1 = enable (default) */
464afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
465afe61c15SRodney W. Grimes.Ed
4660c3a1746SMike Pritchard.Pp
467afe61c15SRodney W. GrimesThis option
468afe61c15SRodney W. Grimesimproves performance for applications that may have no more than one
469aabbf63eSBen Smithurstinstance on a single host (such as a router daemon), by eliminating
4708d22ff75SRuslan Ermilovthe overhead of receiving their own transmissions.
4718d22ff75SRuslan ErmilovIt should generally not
472afe61c15SRodney W. Grimesbe used by applications for which there may be more than one instance on a
473afe61c15SRodney W. Grimessingle host (such as a conferencing program) or for which the sender does
474afe61c15SRodney W. Grimesnot belong to the destination group (such as a time querying program).
475afe61c15SRodney W. Grimes.Pp
476afe61c15SRodney W. GrimesA multicast datagram sent with an initial TTL greater than 1 may be delivered
477afe61c15SRodney W. Grimesto the sending host on a different interface from that on which it was sent,
4788d22ff75SRuslan Ermilovif the host belongs to the destination group on that other interface.
4798d22ff75SRuslan ErmilovThe loopback control option has no effect on such delivery.
480afe61c15SRodney W. Grimes.Pp
481afe61c15SRodney W. GrimesA host must become a member of a multicast group before it can receive
4828d22ff75SRuslan Ermilovdatagrams sent to the group.
4838d22ff75SRuslan ErmilovTo join a multicast group, use the
484afe61c15SRodney W. Grimes.Dv IP_ADD_MEMBERSHIP
485afe61c15SRodney W. Grimesoption:
486afe61c15SRodney W. Grimes.Bd -literal
487afe61c15SRodney W. Grimesstruct ip_mreq mreq;
488afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
489afe61c15SRodney W. Grimes.Ed
4900c3a1746SMike Pritchard.Pp
491afe61c15SRodney W. Grimeswhere
492afe61c15SRodney W. Grimes.Fa mreq
493afe61c15SRodney W. Grimesis the following structure:
494afe61c15SRodney W. Grimes.Bd -literal
495afe61c15SRodney W. Grimesstruct ip_mreq {
4960c3a1746SMike Pritchard    struct in_addr imr_multiaddr; /* IP multicast address of group */
4970c3a1746SMike Pritchard    struct in_addr imr_interface; /* local IP address of interface */
498afe61c15SRodney W. Grimes}
499afe61c15SRodney W. Grimes.Ed
5000c3a1746SMike Pritchard.Pp
501bf7f20c2SRuslan Ermilov.Va imr_interface
502108ad5bdSBruce M Simpsonshould be set to the
503afe61c15SRodney W. Grimes.Tn IP
504afe61c15SRodney W. Grimesaddress of a particular multicast-capable interface if
505afe61c15SRodney W. Grimesthe host is multihomed.
506108ad5bdSBruce M SimpsonIt may be set to
507108ad5bdSBruce M Simpson.Dv INADDR_ANY
508108ad5bdSBruce M Simpsonto choose the default interface, although this is not recommended;
509108ad5bdSBruce M Simpsonthis is considered to be the first interface corresponding
510108ad5bdSBruce M Simpsonto the default route.
511108ad5bdSBruce M SimpsonOtherwise, the first multicast-capable interface
512108ad5bdSBruce M Simpsonconfigured in the system will be used.
513108ad5bdSBruce M Simpson.Pp
514108ad5bdSBruce M SimpsonPrior to
515108ad5bdSBruce M Simpson.Fx 7.0 ,
51626c3ef53SBruce M Simpsonif the
517bf7f20c2SRuslan Ermilov.Va imr_interface
51826c3ef53SBruce M Simpsonmember is within the network range
51926c3ef53SBruce M Simpson.Li 0.0.0.0/8 ,
52026c3ef53SBruce M Simpsonit is treated as an interface index in the system interface MIB,
52126c3ef53SBruce M Simpsonas per the RIP Version 2 MIB Extension (RFC-1724).
522108ad5bdSBruce M SimpsonIn versions of
523108ad5bdSBruce M Simpson.Fx
524108ad5bdSBruce M Simpsonsince 7.0, this behavior is no longer supported.
525108ad5bdSBruce M SimpsonDevelopers should
526108ad5bdSBruce M Simpsoninstead use the RFC 3678 multicast source filter APIs; in particular,
527108ad5bdSBruce M Simpson.Dv MCAST_JOIN_GROUP .
52826c3ef53SBruce M Simpson.Pp
529afe61c15SRodney W. GrimesUp to
530afe61c15SRodney W. Grimes.Dv IP_MAX_MEMBERSHIPS
53124135314SBruce M Simpsonmemberships may be added on a single socket.
53204bcf5a9SBruce M SimpsonMembership is associated with a single interface;
53304bcf5a9SBruce M Simpsonprograms running on multihomed hosts may need to
53404bcf5a9SBruce M Simpsonjoin the same group on more than one interface.
53504bcf5a9SBruce M Simpson.Pp
536afe61c15SRodney W. GrimesTo drop a membership, use:
537afe61c15SRodney W. Grimes.Bd -literal
538afe61c15SRodney W. Grimesstruct ip_mreq mreq;
539afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
540afe61c15SRodney W. Grimes.Ed
5410c3a1746SMike Pritchard.Pp
542afe61c15SRodney W. Grimeswhere
543afe61c15SRodney W. Grimes.Fa mreq
544afe61c15SRodney W. Grimescontains the same values as used to add the membership.
545afe61c15SRodney W. GrimesMemberships are dropped when the socket is closed or the process exits.
546108ad5bdSBruce M Simpson.\" TODO: Update this piece when IPv4 source-address selection is implemented.
547108ad5bdSBruce M Simpson.Pp
548108ad5bdSBruce M SimpsonThe IGMP protocol uses the primary IP address of the interface
549108ad5bdSBruce M Simpsonas its identifier for group membership.
550108ad5bdSBruce M SimpsonThis is the first IP address configured on the interface.
551108ad5bdSBruce M SimpsonIf this address is removed or changed, the results are
552108ad5bdSBruce M Simpsonundefined, as the IGMP membership state will then be inconsistent.
553108ad5bdSBruce M SimpsonIf multiple IP aliases are configured on the same interface,
554108ad5bdSBruce M Simpsonthey will be ignored.
555108ad5bdSBruce M Simpson.Pp
556108ad5bdSBruce M SimpsonThis shortcoming was addressed in IPv6; MLDv2 requires
557108ad5bdSBruce M Simpsonthat the unique link-local address for an interface is
558108ad5bdSBruce M Simpsonused to identify an MLDv2 listener.
559108ad5bdSBruce M Simpson.Ss "Source-Specific Multicast Options"
560108ad5bdSBruce M SimpsonSince
561108ad5bdSBruce M Simpson.Fx 8.0 ,
562108ad5bdSBruce M Simpsonthe use of Source-Specific Multicast (SSM) is supported.
563108ad5bdSBruce M SimpsonThese extensions require an IGMPv3 multicast router in order to
564108ad5bdSBruce M Simpsonmake best use of them.
565108ad5bdSBruce M SimpsonIf a legacy multicast router is present on the link,
566108ad5bdSBruce M Simpson.Fx
567108ad5bdSBruce M Simpsonwill simply downgrade to the version of IGMP spoken by the router,
568108ad5bdSBruce M Simpsonand the benefits of source filtering on the upstream link
569108ad5bdSBruce M Simpsonwill not be present, although the kernel will continue to
570108ad5bdSBruce M Simpsonsquelch transmissions from blocked sources.
571108ad5bdSBruce M Simpson.Pp
572108ad5bdSBruce M SimpsonEach group membership on a socket now has a filter mode:
573108ad5bdSBruce M Simpson.Bl -tag -width MCAST_EXCLUDE
574108ad5bdSBruce M Simpson.It Dv MCAST_EXCLUDE
575108ad5bdSBruce M SimpsonDatagrams sent to this group are accepted,
576108ad5bdSBruce M Simpsonunless the source is in a list of blocked source addresses.
577108ad5bdSBruce M Simpson.It Dv MCAST_INCLUDE
578108ad5bdSBruce M SimpsonDatagrams sent to this group are accepted
579108ad5bdSBruce M Simpsononly if the source is in a list of accepted source addresses.
580108ad5bdSBruce M Simpson.El
581108ad5bdSBruce M Simpson.Pp
582108ad5bdSBruce M SimpsonGroups joined using the legacy
583108ad5bdSBruce M Simpson.Dv IP_ADD_MEMBERSHIP
584108ad5bdSBruce M Simpsonoption are placed in exclusive-mode,
585108ad5bdSBruce M Simpsonand are able to request that certain sources are blocked or allowed.
586108ad5bdSBruce M SimpsonThis is known as the
587108ad5bdSBruce M Simpson.Em delta-based API .
588108ad5bdSBruce M Simpson.Pp
589108ad5bdSBruce M SimpsonTo block a multicast source on an existing group membership:
590108ad5bdSBruce M Simpson.Bd -literal
591108ad5bdSBruce M Simpsonstruct ip_mreq_source mreqs;
592108ad5bdSBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_BLOCK_SOURCE, &mreqs, sizeof(mreqs));
593108ad5bdSBruce M Simpson.Ed
594108ad5bdSBruce M Simpson.Pp
595108ad5bdSBruce M Simpsonwhere
596108ad5bdSBruce M Simpson.Fa mreqs
597108ad5bdSBruce M Simpsonis the following structure:
598108ad5bdSBruce M Simpson.Bd -literal
599108ad5bdSBruce M Simpsonstruct ip_mreq_source {
600108ad5bdSBruce M Simpson    struct in_addr imr_multiaddr; /* IP multicast address of group */
601108ad5bdSBruce M Simpson    struct in_addr imr_sourceaddr; /* IP address of source */
602108ad5bdSBruce M Simpson    struct in_addr imr_interface; /* local IP address of interface */
603108ad5bdSBruce M Simpson}
604108ad5bdSBruce M Simpson.Ed
605108ad5bdSBruce M Simpson.Va imr_sourceaddr
606108ad5bdSBruce M Simpsonshould be set to the address of the source to be blocked.
607108ad5bdSBruce M Simpson.Pp
608108ad5bdSBruce M SimpsonTo unblock a multicast source on an existing group:
609108ad5bdSBruce M Simpson.Bd -literal
610108ad5bdSBruce M Simpsonstruct ip_mreq_source mreqs;
611108ad5bdSBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_UNBLOCK_SOURCE, &mreqs, sizeof(mreqs));
612108ad5bdSBruce M Simpson.Ed
613108ad5bdSBruce M Simpson.Pp
614108ad5bdSBruce M SimpsonThe
615108ad5bdSBruce M Simpson.Dv IP_BLOCK_SOURCE
616108ad5bdSBruce M Simpsonand
617108ad5bdSBruce M Simpson.Dv IP_UNBLOCK_SOURCE
618108ad5bdSBruce M Simpsonoptions are
619108ad5bdSBruce M Simpson.Em not permitted
620108ad5bdSBruce M Simpsonfor inclusive-mode group memberships.
621108ad5bdSBruce M Simpson.Pp
622108ad5bdSBruce M SimpsonTo join a multicast group in
623108ad5bdSBruce M Simpson.Dv MCAST_INCLUDE
624108ad5bdSBruce M Simpsonmode with a single source,
625108ad5bdSBruce M Simpsonor add another source to an existing inclusive-mode membership:
626108ad5bdSBruce M Simpson.Bd -literal
627108ad5bdSBruce M Simpsonstruct ip_mreq_source mreqs;
628108ad5bdSBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
629108ad5bdSBruce M Simpson.Ed
630108ad5bdSBruce M Simpson.Pp
631108ad5bdSBruce M SimpsonTo leave a single source from an existing group in inclusive mode:
632108ad5bdSBruce M Simpson.Bd -literal
633108ad5bdSBruce M Simpsonstruct ip_mreq_source mreqs;
634108ad5bdSBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
635108ad5bdSBruce M Simpson.Ed
636108ad5bdSBruce M SimpsonIf this is the last accepted source for the group, the membership
637108ad5bdSBruce M Simpsonwill be dropped.
638108ad5bdSBruce M Simpson.Pp
639108ad5bdSBruce M SimpsonThe
640108ad5bdSBruce M Simpson.Dv IP_ADD_SOURCE_MEMBERSHIP
641108ad5bdSBruce M Simpsonand
642108ad5bdSBruce M Simpson.Dv IP_DROP_SOURCE_MEMBERSHIP
643108ad5bdSBruce M Simpsonoptions are
644108ad5bdSBruce M Simpson.Em not accepted
645108ad5bdSBruce M Simpsonfor exclusive-mode group memberships.
646108ad5bdSBruce M SimpsonHowever, both exclusive and inclusive mode memberships
647108ad5bdSBruce M Simpsonsupport the use of the
648108ad5bdSBruce M Simpson.Em full-state API
649108ad5bdSBruce M Simpsondocumented in RFC 3678.
650108ad5bdSBruce M SimpsonFor management of source filter lists using this API,
651108ad5bdSBruce M Simpsonplease refer to
652108ad5bdSBruce M Simpson.Xr sourcefilter 3 .
653afe61c15SRodney W. Grimes.\"-----------------------
654afe61c15SRodney W. Grimes.Ss "Raw IP Sockets"
655afe61c15SRodney W. Grimes.Pp
656afe61c15SRodney W. GrimesRaw
657afe61c15SRodney W. Grimes.Tn IP
658afe61c15SRodney W. Grimessockets are connectionless,
659afe61c15SRodney W. Grimesand are normally used with the
660edf0e5b3SMike Pritchard.Xr sendto 2
661afe61c15SRodney W. Grimesand
662edf0e5b3SMike Pritchard.Xr recvfrom 2
663afe61c15SRodney W. Grimescalls, though the
664afe61c15SRodney W. Grimes.Xr connect 2
665afe61c15SRodney W. Grimescall may also be used to fix the destination for future
666afe61c15SRodney W. Grimespackets (in which case the
667afe61c15SRodney W. Grimes.Xr read 2
668afe61c15SRodney W. Grimesor
669afe61c15SRodney W. Grimes.Xr recv 2
670afe61c15SRodney W. Grimesand
671afe61c15SRodney W. Grimes.Xr write 2
672afe61c15SRodney W. Grimesor
673afe61c15SRodney W. Grimes.Xr send 2
674afe61c15SRodney W. Grimessystem calls may be used).
675afe61c15SRodney W. Grimes.Pp
676afe61c15SRodney W. GrimesIf
677afe61c15SRodney W. Grimes.Fa proto
678afe61c15SRodney W. Grimesis 0, the default protocol
679afe61c15SRodney W. Grimes.Dv IPPROTO_RAW
680afe61c15SRodney W. Grimesis used for outgoing
681afe61c15SRodney W. Grimespackets, and only incoming packets destined for that protocol
682afe61c15SRodney W. Grimesare received.
683afe61c15SRodney W. GrimesIf
684afe61c15SRodney W. Grimes.Fa proto
685afe61c15SRodney W. Grimesis non-zero, that protocol number will be used on outgoing packets
686afe61c15SRodney W. Grimesand to filter incoming packets.
687afe61c15SRodney W. Grimes.Pp
688afe61c15SRodney W. GrimesOutgoing packets automatically have an
689afe61c15SRodney W. Grimes.Tn IP
690afe61c15SRodney W. Grimesheader prepended to
691afe61c15SRodney W. Grimesthem (based on the destination address and the protocol
692afe61c15SRodney W. Grimesnumber the socket is created with),
693afe61c15SRodney W. Grimesunless the
694afe61c15SRodney W. Grimes.Dv IP_HDRINCL
695afe61c15SRodney W. Grimesoption has been set.
696afe61c15SRodney W. GrimesIncoming packets are received with
697afe61c15SRodney W. Grimes.Tn IP
698afe61c15SRodney W. Grimesheader and options intact.
699afe61c15SRodney W. Grimes.Pp
700afe61c15SRodney W. Grimes.Dv IP_HDRINCL
701afe61c15SRodney W. Grimesindicates the complete IP header is included with the data
702afe61c15SRodney W. Grimesand may be used only with the
703afe61c15SRodney W. Grimes.Dv SOCK_RAW
704afe61c15SRodney W. Grimestype.
705afe61c15SRodney W. Grimes.Bd -literal
706ddbd0698SBruce Evans#include <netinet/in_systm.h>
707afe61c15SRodney W. Grimes#include <netinet/ip.h>
708afe61c15SRodney W. Grimes
709afe61c15SRodney W. Grimesint hincl = 1;                  /* 1 = on, 0 = off */
710afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
711afe61c15SRodney W. Grimes.Ed
7120c3a1746SMike Pritchard.Pp
713afe61c15SRodney W. GrimesUnlike previous
7148ce3f3ddSRuslan Ermilov.Bx
715afe61c15SRodney W. Grimesreleases, the program must set all
716afe61c15SRodney W. Grimesthe fields of the IP header, including the following:
717afe61c15SRodney W. Grimes.Bd -literal
718afe61c15SRodney W. Grimesip->ip_v = IPVERSION;
719afe61c15SRodney W. Grimesip->ip_hl = hlen >> 2;
720afe61c15SRodney W. Grimesip->ip_id = 0;  /* 0 means kernel set appropriate value */
721afe61c15SRodney W. Grimesip->ip_off = offset;
722afe61c15SRodney W. Grimes.Ed
7230c3a1746SMike Pritchard.Pp
724538a4c1fSBruce M SimpsonThe
725538a4c1fSBruce M Simpson.Va ip_len
726538a4c1fSBruce M Simpsonand
727538a4c1fSBruce M Simpson.Va ip_off
728538a4c1fSBruce M Simpsonfields
729538a4c1fSBruce M Simpson.Em must
730538a4c1fSBruce M Simpsonbe provided in host byte order .
7316f5bc70aSBruce M SimpsonAll other fields must be provided in network byte order.
7326f5bc70aSBruce M SimpsonSee
7335c8e5f24SRuslan Ermilov.Xr byteorder 3
7346f5bc70aSBruce M Simpsonfor more information on network byte order.
735538a4c1fSBruce M SimpsonIf the
736538a4c1fSBruce M Simpson.Va ip_id
737538a4c1fSBruce M Simpsonfield is set to 0 then the kernel will choose an
738c48524c2SMike Pritchardappropriate value.
739afe61c15SRodney W. GrimesIf the header source address is set to
740afe61c15SRodney W. Grimes.Dv INADDR_ANY ,
741afe61c15SRodney W. Grimesthe kernel will choose an appropriate address.
742ef0180edSDima Dorfman.Sh ERRORS
743afe61c15SRodney W. GrimesA socket operation may fail with one of the following errors returned:
744ef0180edSDima Dorfman.Bl -tag -width Er
745afe61c15SRodney W. Grimes.It Bq Er EISCONN
746afe61c15SRodney W. Grimeswhen trying to establish a connection on a socket which
747afe61c15SRodney W. Grimesalready has one, or when trying to send a datagram with the destination
748afe61c15SRodney W. Grimesaddress specified and the socket is already connected;
749afe61c15SRodney W. Grimes.It Bq Er ENOTCONN
750afe61c15SRodney W. Grimeswhen trying to send a datagram, but
7510227791bSRuslan Ermilovno destination address is specified, and the socket has not been
752afe61c15SRodney W. Grimesconnected;
753afe61c15SRodney W. Grimes.It Bq Er ENOBUFS
754afe61c15SRodney W. Grimeswhen the system runs out of memory for
755afe61c15SRodney W. Grimesan internal data structure;
756afe61c15SRodney W. Grimes.It Bq Er EADDRNOTAVAIL
757afe61c15SRodney W. Grimeswhen an attempt is made to create a
758afe61c15SRodney W. Grimessocket with a network address for which no network interface
759afe61c15SRodney W. Grimesexists.
7607bdf965dSAlexander Langer.It Bq Er EACCES
761afe61c15SRodney W. Grimeswhen an attempt is made to create
762afe61c15SRodney W. Grimesa raw IP socket by a non-privileged process.
763afe61c15SRodney W. Grimes.El
764afe61c15SRodney W. Grimes.Pp
765afe61c15SRodney W. GrimesThe following errors specific to
766afe61c15SRodney W. Grimes.Tn IP
767afe61c15SRodney W. Grimesmay occur when setting or getting
768afe61c15SRodney W. Grimes.Tn IP
769afe61c15SRodney W. Grimesoptions:
7708d22ff75SRuslan Ermilov.Bl -tag -width Er
771afe61c15SRodney W. Grimes.It Bq Er EINVAL
772afe61c15SRodney W. GrimesAn unknown socket option name was given.
773afe61c15SRodney W. Grimes.It Bq Er EINVAL
774afe61c15SRodney W. GrimesThe IP option field was improperly formed;
775afe61c15SRodney W. Grimesan option field was shorter than the minimum value
776afe61c15SRodney W. Grimesor longer than the option buffer provided.
777afe61c15SRodney W. Grimes.El
7786f5bc70aSBruce M Simpson.Pp
7796f5bc70aSBruce M SimpsonThe following errors may occur when attempting to send
7806f5bc70aSBruce M Simpson.Tn IP
7816f5bc70aSBruce M Simpsondatagrams via a
7826f5bc70aSBruce M Simpson.Dq raw socket
7836f5bc70aSBruce M Simpsonwith the
784538a4c1fSBruce M Simpson.Dv IP_HDRINCL
7856f5bc70aSBruce M Simpsonoption set:
7866f5bc70aSBruce M Simpson.Bl -tag -width Er
7876f5bc70aSBruce M Simpson.It Bq Er EINVAL
788538a4c1fSBruce M SimpsonThe user-supplied
789538a4c1fSBruce M Simpson.Va ip_len
790538a4c1fSBruce M Simpsonfield was not equal to the length of the datagram written to the socket.
7916f5bc70aSBruce M Simpson.El
792afe61c15SRodney W. Grimes.Sh SEE ALSO
793afe61c15SRodney W. Grimes.Xr getsockopt 2 ,
794afe61c15SRodney W. Grimes.Xr recv 2 ,
7950b992c1dSWolfram Schneider.Xr send 2 ,
7965c8e5f24SRuslan Ermilov.Xr byteorder 3 ,
797afe61c15SRodney W. Grimes.Xr icmp 4 ,
7980b992c1dSWolfram Schneider.Xr inet 4 ,
79904bcf5a9SBruce M Simpson.Xr intro 4 ,
800108ad5bdSBruce M Simpson.Xr multicast 4 ,
801108ad5bdSBruce M Simpson.Xr sourcefilter 3
802108ad5bdSBruce M Simpson.Rs
803108ad5bdSBruce M Simpson.%A D. Thaler
804108ad5bdSBruce M Simpson.%A B. Fenner
805108ad5bdSBruce M Simpson.%A B. Quinn
806108ad5bdSBruce M Simpson.%T "Socket Interface Extensions for Multicast Source Filters"
807108ad5bdSBruce M Simpson.%N RFC 3678
808108ad5bdSBruce M Simpson.%D Jan 2004
809108ad5bdSBruce M Simpson.Re
810afe61c15SRodney W. Grimes.Sh HISTORY
811afe61c15SRodney W. GrimesThe
812afe61c15SRodney W. Grimes.Nm
813afe61c15SRodney W. Grimesprotocol appeared in
814afe61c15SRodney W. Grimes.Bx 4.2 .
81571498f30SBruce M SimpsonThe
81671498f30SBruce M Simpson.Vt ip_mreqn
81771498f30SBruce M Simpsonstructure appeared in
81871498f30SBruce M Simpson.Tn Linux 2.4 .
819