xref: /freebsd/share/man/man4/ip.4 (revision c48524c2aadd7fea1fd3136a6ec63e0eed7f944b)
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.\"
356f5bc70aSBruce M Simpson.Dd June 14, 2004
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
116afe61c15SRodney W. GrimesIf the
117afe61c15SRodney W. Grimes.Dv IP_RECVDSTADDR
118afe61c15SRodney W. Grimesoption is enabled on a
119afe61c15SRodney W. Grimes.Dv SOCK_DGRAM
120afe61c15SRodney W. Grimessocket,
121afe61c15SRodney W. Grimesthe
122edf0e5b3SMike Pritchard.Xr recvmsg 2
123afe61c15SRodney W. Grimescall will return the destination
124afe61c15SRodney W. Grimes.Tn IP
125afe61c15SRodney W. Grimesaddress for a
126afe61c15SRodney W. Grimes.Tn UDP
127afe61c15SRodney W. Grimesdatagram.
1288d22ff75SRuslan ErmilovThe
1298d22ff75SRuslan Ermilov.Vt msg_control
1308d22ff75SRuslan Ermilovfield in the
1318d22ff75SRuslan Ermilov.Vt msghdr
1328d22ff75SRuslan Ermilovstructure points to a buffer
1338d22ff75SRuslan Ermilovthat contains a
1348d22ff75SRuslan Ermilov.Vt cmsghdr
1358d22ff75SRuslan Ermilovstructure followed by the
136afe61c15SRodney W. Grimes.Tn IP
137afe61c15SRodney W. Grimesaddress.
1388d22ff75SRuslan ErmilovThe
1398d22ff75SRuslan Ermilov.Vt cmsghdr
1408d22ff75SRuslan Ermilovfields have the following values:
141afe61c15SRodney W. Grimes.Bd -literal
142afe61c15SRodney W. Grimescmsg_len = sizeof(struct in_addr)
143afe61c15SRodney W. Grimescmsg_level = IPPROTO_IP
144afe61c15SRodney W. Grimescmsg_type = IP_RECVDSTADDR
145afe61c15SRodney W. Grimes.Ed
1467ee32b9fSPeter Wemm.Pp
147c557ae16SIan DowseThe source address to be used for outgoing
148c557ae16SIan Dowse.Tn UDP
149c557ae16SIan Dowsedatagrams on a socket that is not bound to a specific
150c557ae16SIan Dowse.Tn IP
151c557ae16SIan Dowseaddress can be specified as ancillary data with a type code of
152c557ae16SIan Dowse.Dv IP_SENDSRCADDR .
153c557ae16SIan DowseThe msg_control field in the msghdr structure should point to a buffer
1548d22ff75SRuslan Ermilovthat contains a
1558d22ff75SRuslan Ermilov.Vt cmsghdr
1568d22ff75SRuslan Ermilovstructure followed by the
157c557ae16SIan Dowse.Tn IP
158c557ae16SIan Dowseaddress.
159c557ae16SIan DowseThe cmsghdr fields should have the following values:
160c557ae16SIan Dowse.Bd -literal
161c557ae16SIan Dowsecmsg_len = sizeof(struct in_addr)
162c557ae16SIan Dowsecmsg_level = IPPROTO_IP
163c557ae16SIan Dowsecmsg_type = IP_SENDSRCADDR
164c557ae16SIan Dowse.Ed
165c557ae16SIan Dowse.Pp
166c557ae16SIan DowseFor convenience,
167c557ae16SIan Dowse.Dv IP_SENDSRCADDR
168c557ae16SIan Dowseis defined to have the same value as
169c557ae16SIan Dowse.Dv IP_RECVDSTADDR ,
170c557ae16SIan Dowseso the
171c557ae16SIan Dowse.Dv IP_RECVDSTADDR
172c557ae16SIan Dowsecontrol message from
173c557ae16SIan Dowse.Xr recvmsg 2
174c557ae16SIan Dowsecan be used directly as a control message for
175c557ae16SIan Dowse.Xr sendmsg 2 .
176c557ae16SIan Dowse.Pp
1773b5f87e6SMatthew N. DoddIf the
1788afa2304SBruce M Simpson.Dv IP_ONESBCAST
1798afa2304SBruce M Simpsonoption is enabled on a
1808afa2304SBruce M Simpson.Dv SOCK_DGRAM
1818afa2304SBruce M Simpsonor a
1828afa2304SBruce M Simpson.Dv SOCK_RAW
1838afa2304SBruce M Simpsonsocket, the destination address of outgoing
1848afa2304SBruce M Simpsonbroadcast datagrams on that socket will be forced
1858afa2304SBruce M Simpsonto the undirected broadcast address,
1868afa2304SBruce M Simpson.Dv INADDR_BROADCAST ,
1878afa2304SBruce M Simpsonbefore transmission.
1888afa2304SBruce M SimpsonThis is in contrast to the default behavior of the
1898afa2304SBruce M Simpsonsystem, which is to transmit undirected broadcasts
1908afa2304SBruce M Simpsonvia the first network interface with the
1918afa2304SBruce M Simpson.Dv IFF_BROADCAST flag set.
1928afa2304SBruce M Simpson.Pp
1938afa2304SBruce M SimpsonThis option allows applications to choose which
1948afa2304SBruce M Simpsoninterface is used to transmit an undirected broadcast
1958afa2304SBruce M Simpsondatagram.
1968afa2304SBruce M SimpsonFor example, the following code would force an
1978afa2304SBruce M Simpsonundirected broadcast to be transmitted via the interface
1988afa2304SBruce M Simpsonconfigured with the broadcast address 192.168.2.255:
1998afa2304SBruce M Simpson.Bd -literal
2008afa2304SBruce M Simpsonchar msg[512];
2018afa2304SBruce M Simpsonstruct sockaddr_in sin;
2028afa2304SBruce M Simpsonu_char onesbcast = 1;	/* 0 = disable (default), 1 = enable */
2038afa2304SBruce M Simpson
2048afa2304SBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_ONESBCAST, &onesbcast, sizeof(onesbcast));
2058afa2304SBruce M Simpsonsin.sin_addr.s_addr = inet_addr("192.168.2.255");
2068afa2304SBruce M Simpsonsin.sin_port = htons(1234);
2078afa2304SBruce M Simpsonsendto(s, msg, sizeof(msg), 0, &sin, sizeof(sin));
2088afa2304SBruce M Simpson.Ed
2098afa2304SBruce M Simpson.Pp
2108afa2304SBruce M SimpsonIt is the application's responsibility to set the
2118afa2304SBruce M Simpson.Dv IP_TTL option
2128afa2304SBruce M Simpsonto an appropriate value in order to prevent broadcast storms.
2138afa2304SBruce M SimpsonThe application must have sufficient credentials to set the
2148afa2304SBruce M Simpson.Dv SO_BROADCAST
2158afa2304SBruce M Simpsonsocket level option, otherwise the
2168afa2304SBruce M Simpson.Dv IP_ONESBCAST option has no effect.
2178afa2304SBruce M Simpson.Pp
2188afa2304SBruce M SimpsonIf the
2194957466bSMatthew N. Dodd.Dv IP_RECVTTL
2204957466bSMatthew N. Doddoption is enabled on a
2214957466bSMatthew N. Dodd.Dv SOCK_DGRAM
2224957466bSMatthew N. Doddsocket, the
2234957466bSMatthew N. Dodd.Xr recvmsg 2
2244957466bSMatthew N. Doddcall will return the
2254957466bSMatthew N. Dodd.Tn IP
2264957466bSMatthew N. Dodd.Tn TTL
2274957466bSMatthew N. Dodd(time to live) field for a
2284957466bSMatthew N. Dodd.Tn UDP
2294957466bSMatthew N. Dodddatagram.
2304957466bSMatthew N. DoddThe msg_control field in the msghdr structure points to a buffer
2314957466bSMatthew N. Doddthat contains a cmsghdr structure followed by the
2324957466bSMatthew N. Dodd.Tn TTL .
2334957466bSMatthew N. DoddThe cmsghdr fields have the following values:
2344957466bSMatthew N. Dodd.Bd -literal
2354957466bSMatthew N. Doddcmsg_len = sizeof(u_char)
2364957466bSMatthew N. Doddcmsg_level = IPPROTO_IP
2374957466bSMatthew N. Doddcmsg_type = IP_RECVTTL
2384957466bSMatthew N. Dodd.Ed
2394957466bSMatthew N. Dodd.Pp
2404957466bSMatthew N. DoddIf the
2413b5f87e6SMatthew N. Dodd.Dv IP_RECVIF
2423b5f87e6SMatthew N. Doddoption is enabled on a
2433b5f87e6SMatthew N. Dodd.Dv SOCK_DGRAM
2443b5f87e6SMatthew N. Doddsocket, the
2453b5f87e6SMatthew N. Dodd.Xr recvmsg 2
2468d22ff75SRuslan Ermilovcall returns a
2478d22ff75SRuslan Ermilov.Vt "struct sockaddr_dl"
2488d22ff75SRuslan Ermilovcorresponding to the interface on which the
2498d22ff75SRuslan Ermilovpacket was received.
2508d22ff75SRuslan ErmilovThe
2518d22ff75SRuslan Ermilov.Va msg_control
2528d22ff75SRuslan Ermilovfield in the
2538d22ff75SRuslan Ermilov.Vt msghdr
2548d22ff75SRuslan Ermilovstructure points to a buffer that contains a
2558d22ff75SRuslan Ermilov.Vt cmsghdr
2568d22ff75SRuslan Ermilovstructure followed by the
2578d22ff75SRuslan Ermilov.Vt "struct sockaddr_dl" .
2588d22ff75SRuslan ErmilovThe
2598d22ff75SRuslan Ermilov.Vt cmsghdr
2608d22ff75SRuslan Ermilovfields have the following values:
2613b5f87e6SMatthew N. Dodd.Bd -literal
2623b5f87e6SMatthew N. Doddcmsg_len = sizeof(struct sockaddr_dl)
2633b5f87e6SMatthew N. Doddcmsg_level = IPPROTO_IP
2643b5f87e6SMatthew N. Doddcmsg_type = IP_RECVIF
2653b5f87e6SMatthew N. Dodd.Ed
2663b5f87e6SMatthew N. Dodd.Pp
2677ee32b9fSPeter Wemm.Dv IP_PORTRANGE
2687ee32b9fSPeter Wemmmay be used to set the port range used for selecting a local port number
2696d249eeeSSheldon Hearnon a socket with an unspecified (zero) port number.
2706d249eeeSSheldon HearnIt has the following
2717ee32b9fSPeter Wemmpossible values:
2727ee32b9fSPeter Wemm.Bl -tag -width IP_PORTRANGE_DEFAULT
2737ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_DEFAULT
2747ee32b9fSPeter Wemmuse the default range of values, normally
27574c0361aSMike Silbersack.Dv IPPORT_HIFIRSTAUTO
2767ee32b9fSPeter Wemmthrough
27774c0361aSMike Silbersack.Dv IPPORT_HILASTAUTO .
2787ee32b9fSPeter WemmThis is adjustable through the sysctl setting:
279ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.first
2807ee32b9fSPeter Wemmand
281ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.last .
2827ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_HIGH
2837ee32b9fSPeter Wemmuse a high range of values, normally
2847ee32b9fSPeter Wemm.Dv IPPORT_HIFIRSTAUTO
2857ee32b9fSPeter Wemmand
2867ee32b9fSPeter Wemm.Dv IPPORT_HILASTAUTO .
2877ee32b9fSPeter WemmThis is adjustable through the sysctl setting:
288ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.hifirst
2897ee32b9fSPeter Wemmand
290ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.hilast .
2917ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_LOW
2927ee32b9fSPeter Wemmuse a low range of ports, which are normally restricted to
2937ee32b9fSPeter Wemmprivileged processes on
2947ee32b9fSPeter Wemm.Ux
2958d22ff75SRuslan Ermilovsystems.
2968d22ff75SRuslan ErmilovThe range is normally from
297ace5be68SRuslan Ermilov.Dv IPPORT_RESERVED
298ace5be68SRuslan Ermilov\- 1 down to
2998f5e8232SBen Smithurst.Li IPPORT_RESERVEDSTART
3008f5e8232SBen Smithurstin descending order.
3018f5e8232SBen SmithurstThis is adjustable through the sysctl setting:
302ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.lowfirst
3038f5e8232SBen Smithurstand
304ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.lowlast .
3057ee32b9fSPeter Wemm.El
306718b17bbSCrist J. Clark.Pp
307718b17bbSCrist J. ClarkThe range of privileged ports which only may be opened by
308718b17bbSCrist J. Clarkroot-owned processes may be modified by the
309ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.reservedlow
310718b17bbSCrist J. Clarkand
311ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.reservedhigh
312718b17bbSCrist J. Clarksysctl settings.
313718b17bbSCrist J. ClarkThe values default to the traditional range,
314718b17bbSCrist J. Clark0 through
315ace5be68SRuslan Ermilov.Dv IPPORT_RESERVED
316ace5be68SRuslan Ermilov\- 1
317718b17bbSCrist J. Clark(0 through 1023), respectively.
318718b17bbSCrist J. ClarkNote that these settings do not affect and are not accounted for in the
319718b17bbSCrist J. Clarkuse or calculation of the other
320ace5be68SRuslan Ermilov.Va net.inet.ip.portrange
321718b17bbSCrist J. Clarkvalues above.
322ace5be68SRuslan ErmilovChanging these values departs from
323ace5be68SRuslan Ermilov.Ux
324ace5be68SRuslan Ermilovtradition and has security
325ace5be68SRuslan Ermilovconsequences that the administrator should carefully evaluate before
326718b17bbSCrist J. Clarkmodifying these settings.
327fe9892eaSMike Silbersack.Pp
328fe9892eaSMike SilbersackPorts are allocated at random within the specified port range in order
329bf7f20c2SRuslan Ermilovto increase the difficulty of random spoofing attacks.
330c48524c2SMike PritchardIn scenarios such as benchmarking, this behavior may be undesirable.
331bf7f20c2SRuslan ErmilovIn these cases,
332fe9892eaSMike Silbersack.Va net.inet.ip.portrange.randomized
333fe9892eaSMike Silbersackcan be used to toggle randomization off.
334afe61c15SRodney W. Grimes.Ss "Multicast Options"
335afe61c15SRodney W. Grimes.Pp
336afe61c15SRodney W. Grimes.Tn IP
337afe61c15SRodney W. Grimesmulticasting is supported only on
338afe61c15SRodney W. Grimes.Dv AF_INET
339afe61c15SRodney W. Grimessockets of type
340afe61c15SRodney W. Grimes.Dv SOCK_DGRAM
341afe61c15SRodney W. Grimesand
342afe61c15SRodney W. Grimes.Dv SOCK_RAW ,
343afe61c15SRodney W. Grimesand only on networks where the interface
344afe61c15SRodney W. Grimesdriver supports multicasting.
345afe61c15SRodney W. Grimes.Pp
346afe61c15SRodney W. GrimesThe
347afe61c15SRodney W. Grimes.Dv IP_MULTICAST_TTL
348afe61c15SRodney W. Grimesoption changes the time-to-live (TTL)
349afe61c15SRodney W. Grimesfor outgoing multicast datagrams
350afe61c15SRodney W. Grimesin order to control the scope of the multicasts:
351afe61c15SRodney W. Grimes.Bd -literal
352afe61c15SRodney W. Grimesu_char ttl;	/* range: 0 to 255, default = 1 */
353afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
354afe61c15SRodney W. Grimes.Ed
3550c3a1746SMike Pritchard.Pp
356afe61c15SRodney W. GrimesDatagrams with a TTL of 1 are not forwarded beyond the local network.
357afe61c15SRodney W. GrimesMulticast datagrams with a TTL of 0 will not be transmitted on any network,
358afe61c15SRodney W. Grimesbut may be delivered locally if the sending host belongs to the destination
359afe61c15SRodney W. Grimesgroup and if multicast loopback has not been disabled on the sending socket
3608d22ff75SRuslan Ermilov(see below).
3618d22ff75SRuslan ErmilovMulticast datagrams with TTL greater than 1 may be forwarded
362afe61c15SRodney W. Grimesto other networks if a multicast router is attached to the local network.
363afe61c15SRodney W. Grimes.Pp
364afe61c15SRodney W. GrimesFor hosts with multiple interfaces, each multicast transmission is
365afe61c15SRodney W. Grimessent from the primary network interface.
366afe61c15SRodney W. GrimesThe
367afe61c15SRodney W. Grimes.Dv IP_MULTICAST_IF
368afe61c15SRodney W. Grimesoption overrides the default for
369afe61c15SRodney W. Grimessubsequent transmissions from a given socket:
370afe61c15SRodney W. Grimes.Bd -literal
371afe61c15SRodney W. Grimesstruct in_addr addr;
372afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
373afe61c15SRodney W. Grimes.Ed
3740c3a1746SMike Pritchard.Pp
375afe61c15SRodney W. Grimeswhere "addr" is the local
376afe61c15SRodney W. Grimes.Tn IP
377afe61c15SRodney W. Grimesaddress of the desired interface or
378afe61c15SRodney W. Grimes.Dv INADDR_ANY
379afe61c15SRodney W. Grimesto specify the default interface.
380afe61c15SRodney W. GrimesAn interface's local IP address and multicast capability can
381afe61c15SRodney W. Grimesbe obtained via the
382afe61c15SRodney W. Grimes.Dv SIOCGIFCONF
383afe61c15SRodney W. Grimesand
384afe61c15SRodney W. Grimes.Dv SIOCGIFFLAGS
385afe61c15SRodney W. Grimesioctls.
386afe61c15SRodney W. GrimesNormal applications should not need to use this option.
387afe61c15SRodney W. Grimes.Pp
388afe61c15SRodney W. GrimesIf a multicast datagram is sent to a group to which the sending host itself
389afe61c15SRodney W. Grimesbelongs (on the outgoing interface), a copy of the datagram is, by default,
390afe61c15SRodney W. Grimeslooped back by the IP layer for local delivery.
391afe61c15SRodney W. GrimesThe
392afe61c15SRodney W. Grimes.Dv IP_MULTICAST_LOOP
393afe61c15SRodney W. Grimesoption gives the sender explicit control
394afe61c15SRodney W. Grimesover whether or not subsequent datagrams are looped back:
395afe61c15SRodney W. Grimes.Bd -literal
396afe61c15SRodney W. Grimesu_char loop;	/* 0 = disable, 1 = enable (default) */
397afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
398afe61c15SRodney W. Grimes.Ed
3990c3a1746SMike Pritchard.Pp
400afe61c15SRodney W. GrimesThis option
401afe61c15SRodney W. Grimesimproves performance for applications that may have no more than one
402aabbf63eSBen Smithurstinstance on a single host (such as a router daemon), by eliminating
4038d22ff75SRuslan Ermilovthe overhead of receiving their own transmissions.
4048d22ff75SRuslan ErmilovIt should generally not
405afe61c15SRodney W. Grimesbe used by applications for which there may be more than one instance on a
406afe61c15SRodney W. Grimessingle host (such as a conferencing program) or for which the sender does
407afe61c15SRodney W. Grimesnot belong to the destination group (such as a time querying program).
408afe61c15SRodney W. Grimes.Pp
409afe61c15SRodney W. GrimesA multicast datagram sent with an initial TTL greater than 1 may be delivered
410afe61c15SRodney W. Grimesto the sending host on a different interface from that on which it was sent,
4118d22ff75SRuslan Ermilovif the host belongs to the destination group on that other interface.
4128d22ff75SRuslan ErmilovThe loopback control option has no effect on such delivery.
413afe61c15SRodney W. Grimes.Pp
414afe61c15SRodney W. GrimesA host must become a member of a multicast group before it can receive
4158d22ff75SRuslan Ermilovdatagrams sent to the group.
4168d22ff75SRuslan ErmilovTo join a multicast group, use the
417afe61c15SRodney W. Grimes.Dv IP_ADD_MEMBERSHIP
418afe61c15SRodney W. Grimesoption:
419afe61c15SRodney W. Grimes.Bd -literal
420afe61c15SRodney W. Grimesstruct ip_mreq mreq;
421afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
422afe61c15SRodney W. Grimes.Ed
4230c3a1746SMike Pritchard.Pp
424afe61c15SRodney W. Grimeswhere
425afe61c15SRodney W. Grimes.Fa mreq
426afe61c15SRodney W. Grimesis the following structure:
427afe61c15SRodney W. Grimes.Bd -literal
428afe61c15SRodney W. Grimesstruct ip_mreq {
4290c3a1746SMike Pritchard    struct in_addr imr_multiaddr; /* IP multicast address of group */
4300c3a1746SMike Pritchard    struct in_addr imr_interface; /* local IP address of interface */
431afe61c15SRodney W. Grimes}
432afe61c15SRodney W. Grimes.Ed
4330c3a1746SMike Pritchard.Pp
434bf7f20c2SRuslan Ermilov.Va imr_interface
43526c3ef53SBruce M Simpsonshould be set to
436afe61c15SRodney W. Grimes.Dv INADDR_ANY
437afe61c15SRodney W. Grimesto choose the default multicast interface,
438afe61c15SRodney W. Grimesor the
439afe61c15SRodney W. Grimes.Tn IP
440afe61c15SRodney W. Grimesaddress of a particular multicast-capable interface if
441afe61c15SRodney W. Grimesthe host is multihomed.
44226c3ef53SBruce M SimpsonSince
44326c3ef53SBruce M Simpson.Fx 4.4 ,
44426c3ef53SBruce M Simpsonif the
445bf7f20c2SRuslan Ermilov.Va imr_interface
44626c3ef53SBruce M Simpsonmember is within the network range
44726c3ef53SBruce M Simpson.Li 0.0.0.0/8 ,
44826c3ef53SBruce M Simpsonit is treated as an interface index in the system interface MIB,
44926c3ef53SBruce M Simpsonas per the RIP Version 2 MIB Extension (RFC-1724).
45026c3ef53SBruce M Simpson.Pp
451afe61c15SRodney W. GrimesMembership is associated with a single interface;
452afe61c15SRodney W. Grimesprograms running on multihomed hosts may need to
453afe61c15SRodney W. Grimesjoin the same group on more than one interface.
454afe61c15SRodney W. GrimesUp to
455afe61c15SRodney W. Grimes.Dv IP_MAX_MEMBERSHIPS
456afe61c15SRodney W. Grimes(currently 20) memberships may be added on a
457afe61c15SRodney W. Grimessingle socket.
458afe61c15SRodney W. Grimes.Pp
459afe61c15SRodney W. GrimesTo drop a membership, use:
460afe61c15SRodney W. Grimes.Bd -literal
461afe61c15SRodney W. Grimesstruct ip_mreq mreq;
462afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
463afe61c15SRodney W. Grimes.Ed
4640c3a1746SMike Pritchard.Pp
465afe61c15SRodney W. Grimeswhere
466afe61c15SRodney W. Grimes.Fa mreq
467afe61c15SRodney W. Grimescontains the same values as used to add the membership.
468afe61c15SRodney W. GrimesMemberships are dropped when the socket is closed or the process exits.
469afe61c15SRodney W. Grimes.\"-----------------------
470afe61c15SRodney W. Grimes.Ss "Raw IP Sockets"
471afe61c15SRodney W. Grimes.Pp
472afe61c15SRodney W. GrimesRaw
473afe61c15SRodney W. Grimes.Tn IP
474afe61c15SRodney W. Grimessockets are connectionless,
475afe61c15SRodney W. Grimesand are normally used with the
476edf0e5b3SMike Pritchard.Xr sendto 2
477afe61c15SRodney W. Grimesand
478edf0e5b3SMike Pritchard.Xr recvfrom 2
479afe61c15SRodney W. Grimescalls, though the
480afe61c15SRodney W. Grimes.Xr connect 2
481afe61c15SRodney W. Grimescall may also be used to fix the destination for future
482afe61c15SRodney W. Grimespackets (in which case the
483afe61c15SRodney W. Grimes.Xr read 2
484afe61c15SRodney W. Grimesor
485afe61c15SRodney W. Grimes.Xr recv 2
486afe61c15SRodney W. Grimesand
487afe61c15SRodney W. Grimes.Xr write 2
488afe61c15SRodney W. Grimesor
489afe61c15SRodney W. Grimes.Xr send 2
490afe61c15SRodney W. Grimessystem calls may be used).
491afe61c15SRodney W. Grimes.Pp
492afe61c15SRodney W. GrimesIf
493afe61c15SRodney W. Grimes.Fa proto
494afe61c15SRodney W. Grimesis 0, the default protocol
495afe61c15SRodney W. Grimes.Dv IPPROTO_RAW
496afe61c15SRodney W. Grimesis used for outgoing
497afe61c15SRodney W. Grimespackets, and only incoming packets destined for that protocol
498afe61c15SRodney W. Grimesare received.
499afe61c15SRodney W. GrimesIf
500afe61c15SRodney W. Grimes.Fa proto
501afe61c15SRodney W. Grimesis non-zero, that protocol number will be used on outgoing packets
502afe61c15SRodney W. Grimesand to filter incoming packets.
503afe61c15SRodney W. Grimes.Pp
504afe61c15SRodney W. GrimesOutgoing packets automatically have an
505afe61c15SRodney W. Grimes.Tn IP
506afe61c15SRodney W. Grimesheader prepended to
507afe61c15SRodney W. Grimesthem (based on the destination address and the protocol
508afe61c15SRodney W. Grimesnumber the socket is created with),
509afe61c15SRodney W. Grimesunless the
510afe61c15SRodney W. Grimes.Dv IP_HDRINCL
511afe61c15SRodney W. Grimesoption has been set.
512afe61c15SRodney W. GrimesIncoming packets are received with
513afe61c15SRodney W. Grimes.Tn IP
514afe61c15SRodney W. Grimesheader and options intact.
515afe61c15SRodney W. Grimes.Pp
516afe61c15SRodney W. Grimes.Dv IP_HDRINCL
517afe61c15SRodney W. Grimesindicates the complete IP header is included with the data
518afe61c15SRodney W. Grimesand may be used only with the
519afe61c15SRodney W. Grimes.Dv SOCK_RAW
520afe61c15SRodney W. Grimestype.
521afe61c15SRodney W. Grimes.Bd -literal
522ddbd0698SBruce Evans#include <netinet/in_systm.h>
523afe61c15SRodney W. Grimes#include <netinet/ip.h>
524afe61c15SRodney W. Grimes
525afe61c15SRodney W. Grimesint hincl = 1;                  /* 1 = on, 0 = off */
526afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
527afe61c15SRodney W. Grimes.Ed
5280c3a1746SMike Pritchard.Pp
529afe61c15SRodney W. GrimesUnlike previous
5308ce3f3ddSRuslan Ermilov.Bx
531afe61c15SRodney W. Grimesreleases, the program must set all
532afe61c15SRodney W. Grimesthe fields of the IP header, including the following:
533afe61c15SRodney W. Grimes.Bd -literal
534afe61c15SRodney W. Grimesip->ip_v = IPVERSION;
535afe61c15SRodney W. Grimesip->ip_hl = hlen >> 2;
536afe61c15SRodney W. Grimesip->ip_id = 0;  /* 0 means kernel set appropriate value */
537afe61c15SRodney W. Grimesip->ip_off = offset;
538afe61c15SRodney W. Grimes.Ed
5390c3a1746SMike Pritchard.Pp
540538a4c1fSBruce M SimpsonThe
541538a4c1fSBruce M Simpson.Va ip_len
542538a4c1fSBruce M Simpsonand
543538a4c1fSBruce M Simpson.Va ip_off
544538a4c1fSBruce M Simpsonfields
545538a4c1fSBruce M Simpson.Em must
546538a4c1fSBruce M Simpsonbe provided in host byte order .
5476f5bc70aSBruce M SimpsonAll other fields must be provided in network byte order.
5486f5bc70aSBruce M SimpsonSee
5496f5bc70aSBruce M Simpson.Xr byteorder 4
5506f5bc70aSBruce M Simpsonfor more information on network byte order.
551538a4c1fSBruce M SimpsonIf the
552538a4c1fSBruce M Simpson.Va ip_id
553538a4c1fSBruce M Simpsonfield is set to 0 then the kernel will choose an
554c48524c2SMike Pritchardappropriate value.
555afe61c15SRodney W. GrimesIf the header source address is set to
556afe61c15SRodney W. Grimes.Dv INADDR_ANY ,
557afe61c15SRodney W. Grimesthe kernel will choose an appropriate address.
558ef0180edSDima Dorfman.Sh ERRORS
559afe61c15SRodney W. GrimesA socket operation may fail with one of the following errors returned:
560ef0180edSDima Dorfman.Bl -tag -width Er
561afe61c15SRodney W. Grimes.It Bq Er EISCONN
562afe61c15SRodney W. Grimeswhen trying to establish a connection on a socket which
563afe61c15SRodney W. Grimesalready has one, or when trying to send a datagram with the destination
564afe61c15SRodney W. Grimesaddress specified and the socket is already connected;
565afe61c15SRodney W. Grimes.It Bq Er ENOTCONN
566afe61c15SRodney W. Grimeswhen trying to send a datagram, but
567afe61c15SRodney W. Grimesno destination address is specified, and the socket hasn't been
568afe61c15SRodney W. Grimesconnected;
569afe61c15SRodney W. Grimes.It Bq Er ENOBUFS
570afe61c15SRodney W. Grimeswhen the system runs out of memory for
571afe61c15SRodney W. Grimesan internal data structure;
572afe61c15SRodney W. Grimes.It Bq Er EADDRNOTAVAIL
573afe61c15SRodney W. Grimeswhen an attempt is made to create a
574afe61c15SRodney W. Grimessocket with a network address for which no network interface
575afe61c15SRodney W. Grimesexists.
5767bdf965dSAlexander Langer.It Bq Er EACCES
577afe61c15SRodney W. Grimeswhen an attempt is made to create
578afe61c15SRodney W. Grimesa raw IP socket by a non-privileged process.
579afe61c15SRodney W. Grimes.El
580afe61c15SRodney W. Grimes.Pp
581afe61c15SRodney W. GrimesThe following errors specific to
582afe61c15SRodney W. Grimes.Tn IP
583afe61c15SRodney W. Grimesmay occur when setting or getting
584afe61c15SRodney W. Grimes.Tn IP
585afe61c15SRodney W. Grimesoptions:
5868d22ff75SRuslan Ermilov.Bl -tag -width Er
587afe61c15SRodney W. Grimes.It Bq Er EINVAL
588afe61c15SRodney W. GrimesAn unknown socket option name was given.
589afe61c15SRodney W. Grimes.It Bq Er EINVAL
590afe61c15SRodney W. GrimesThe IP option field was improperly formed;
591afe61c15SRodney W. Grimesan option field was shorter than the minimum value
592afe61c15SRodney W. Grimesor longer than the option buffer provided.
593afe61c15SRodney W. Grimes.El
5946f5bc70aSBruce M Simpson.Pp
5956f5bc70aSBruce M SimpsonThe following errors may occur when attempting to send
5966f5bc70aSBruce M Simpson.Tn IP
5976f5bc70aSBruce M Simpsondatagrams via a
5986f5bc70aSBruce M Simpson.Dq raw socket
5996f5bc70aSBruce M Simpsonwith the
600538a4c1fSBruce M Simpson.Dv IP_HDRINCL
6016f5bc70aSBruce M Simpsonoption set:
6026f5bc70aSBruce M Simpson.Bl -tag -width Er
6036f5bc70aSBruce M Simpson.It Bq Er EINVAL
604538a4c1fSBruce M SimpsonThe user-supplied
605538a4c1fSBruce M Simpson.Va ip_len
606538a4c1fSBruce M Simpsonfield was not equal to the length of the datagram written to the socket.
6076f5bc70aSBruce M Simpson.El
608afe61c15SRodney W. Grimes.Sh SEE ALSO
609afe61c15SRodney W. Grimes.Xr getsockopt 2 ,
610afe61c15SRodney W. Grimes.Xr recv 2 ,
6110b992c1dSWolfram Schneider.Xr send 2 ,
6126f5bc70aSBruce M Simpson.Xr byteorder 4 ,
613afe61c15SRodney W. Grimes.Xr icmp 4 ,
6140b992c1dSWolfram Schneider.Xr inet 4 ,
6150b992c1dSWolfram Schneider.Xr intro 4
616afe61c15SRodney W. Grimes.Sh HISTORY
617afe61c15SRodney W. GrimesThe
618afe61c15SRodney W. Grimes.Nm
619afe61c15SRodney W. Grimesprotocol appeared in
620afe61c15SRodney W. Grimes.Bx 4.2 .
621