xref: /netbsd-src/share/man/man4/ip.4 (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1.\"	$NetBSD: ip.4,v 1.5 1998/01/07 23:39:49 lukem Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)ip.4	8.2 (Berkeley) 11/30/93
35.\"
36.Dd November 30, 1993
37.Dt IP 4
38.Os BSD 4.2
39.Sh NAME
40.Nm ip
41.Nd Internet Protocol
42.Sh SYNOPSIS
43.Fd #include <sys/socket.h>
44.Fd #include <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 / Ns
69.Xr getsockopt 2
70options.
71.Dv IP_OPTIONS
72may be used to provide
73.Tn IP
74options to be transmitted in the
75.Tn IP
76header of each outgoing packet
77or to examine the header options on incoming packets.
78.Tn IP
79options may be used with any socket type in the Internet family.
80The format of
81.Tn IP
82options to be sent is that specified by the
83.Tn IP
84protocol specification (RFC-791), with one exception:
85the list of addresses for Source Route options must include the first-hop
86gateway at the beginning of the list of gateways.
87The first-hop gateway address will be extracted from the option list
88and the size adjusted accordingly before use.
89To disable previously specified options,
90use a zero-length buffer:
91.Bd -literal
92setsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0);
93.Ed
94.Pp
95.Dv IP_TOS
96and
97.Dv IP_TTL
98may be used to set the type-of-service and time-to-live
99fields in the
100.Tn IP
101header for
102.Dv SOCK_STREAM
103and
104.Dv SOCK_DGRAM
105sockets. For example,
106.Bd -literal
107int tos = IPTOS_LOWDELAY;       /* see <netinet/in.h> */
108setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
109
110int ttl = 60;                   /* max = 255 */
111setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
112.Ed
113.Pp
114.Dv IP_PORTRANGE
115controls how ephemeral ports are allocated for
116.Dv SOCK_STREAM
117and
118.Dv SOCK_DGRAM
119sockets. For example,
120.Bd -literal
121int range = IP_PORTRANGE_LOW;       /* see <netinet/in.h> */
122setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &range, sizeof(range));
123.Ed
124.Pp
125If the
126.Dv IP_RECVDSTADDR
127option is enabled on a
128.Dv SOCK_DGRAM
129or
130.Dv SOCK_RAW
131socket,
132the
133.Xr recvmsg
134call will return the destination
135.Tn IP
136address for a
137.Tn UDP
138datagram.
139The msg_control field in the msghdr structure points to a buffer
140that contains a cmsghdr structure followed by the
141.Tn IP
142address.
143The cmsghdr fields have the following values:
144.Bd -literal
145cmsg_len = sizeof(struct in_addr)
146cmsg_level = IPPROTO_IP
147cmsg_type = IP_RECVDSTADDR
148.Ed
149.Pp
150If the
151.Dv IP_RECVIF
152option is enabled on a
153.Dv SOCK_DGRAM
154or
155.Dv SOCK_RAW
156socket,
157the
158.Xr recvmsg
159call will return a struct sockaddr_dl corresponding to
160the interface on which the packet was received.
161the msg_control field in the msghdr structure points to a buffer
162that contains a cmsghdr structure followed by the struct sockaddr_dl.
163The cmsghdr fields have the following values:
164.Bd -literal
165cmsg_len = sizeof(struct sockaddr_dl)
166cmsg_level = IPPROTO_IP
167cmsg_type = IP_RECVIF
168.Ed
169.Ss "Multicast Options"
170.Pp
171.Tn IP
172multicasting is supported only on
173.Dv AF_INET
174sockets of type
175.Dv SOCK_DGRAM
176and
177.Dv SOCK_RAW,
178and only on networks where the interface
179driver supports multicasting.
180.Pp
181The
182.Dv IP_MULTICAST_TTL
183option changes the time-to-live (TTL)
184for outgoing multicast datagrams
185in order to control the scope of the multicasts:
186.Bd -literal
187u_char ttl;	/* range: 0 to 255, default = 1 */
188setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
189.Ed
190.sp
191Datagrams with a TTL of 1 are not forwarded beyond the local network.
192Multicast datagrams with a TTL of 0 will not be transmitted on any network,
193but may be delivered locally if the sending host belongs to the destination
194group and if multicast loopback has not been disabled on the sending socket
195(see below).  Multicast datagrams with TTL greater than 1 may be forwarded
196to other networks if a multicast router is attached to the local network.
197.Pp
198For hosts with multiple interfaces, each multicast transmission is
199sent from the primary network interface.
200The
201.Dv IP_MULTICAST_IF
202option overrides the default for
203subsequent transmissions from a given socket:
204.Bd -literal
205struct in_addr addr;
206setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
207.Ed
208.sp
209where "addr" is the local
210.Tn IP
211address of the desired interface or
212.Dv INADDR_ANY
213to specify the default interface.
214An interface's local IP address and multicast capability can
215be obtained via the
216.Dv SIOCGIFCONF
217and
218.Dv SIOCGIFFLAGS
219ioctls.
220Normal applications should not need to use this option.
221.Pp
222If a multicast datagram is sent to a group to which the sending host itself
223belongs (on the outgoing interface), a copy of the datagram is, by default,
224looped back by the IP layer for local delivery.
225The
226.Dv IP_MULTICAST_LOOP
227option gives the sender explicit control
228over whether or not subsequent datagrams are looped back:
229.Bd -literal
230u_char loop;	/* 0 = disable, 1 = enable (default) */
231setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
232.Ed
233.sp
234This option
235improves performance for applications that may have no more than one
236instance on a single host (such as a router demon), by eliminating
237the overhead of receiving their own transmissions.  It should generally not
238be used by applications for which there may be more than one instance on a
239single host (such as a conferencing program) or for which the sender does
240not belong to the destination group (such as a time querying program).
241.Pp
242A multicast datagram sent with an initial TTL greater than 1 may be delivered
243to the sending host on a different interface from that on which it was sent,
244if the host belongs to the destination group on that other interface.  The
245loopback control option has no effect on such delivery.
246.Pp
247A host must become a member of a multicast group before it can receive
248datagrams sent to the group.  To join a multicast group, use the
249.Dv IP_ADD_MEMBERSHIP
250option:
251.Bd -literal
252struct ip_mreq mreq;
253setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
254.Ed
255.sp
256where
257.Fa mreq
258is the following structure:
259.Bd -literal
260struct ip_mreq {
261    struct in_addr imr_multiaddr; /* multicast group to join */
262    struct in_addr imr_interface; /* interface to join on */
263}
264.Ed
265.sp
266.Dv imr_interface
267should
268be
269.Dv INADDR_ANY
270to choose the default multicast interface,
271or the
272.Tn IP
273address of a particular multicast-capable interface if
274the host is multihomed.
275Membership is associated with a single interface;
276programs running on multihomed hosts may need to
277join the same group on more than one interface.
278Up to
279.Dv IP_MAX_MEMBERSHIPS
280(currently 20) memberships may be added on a
281single socket.
282.Pp
283To drop a membership, use:
284.Bd -literal
285struct ip_mreq mreq;
286setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
287.Ed
288.sp
289where
290.Fa mreq
291contains the same values as used to add the membership.
292Memberships are dropped when the socket is closed or the process exits.
293.\"-----------------------
294.Ss "Raw IP Sockets"
295.Pp
296Raw
297.Tn IP
298sockets are connectionless,
299and are normally used with the
300.Xr sendto
301and
302.Xr recvfrom
303calls, though the
304.Xr connect 2
305call may also be used to fix the destination for future
306packets (in which case the
307.Xr read 2
308or
309.Xr recv 2
310and
311.Xr write 2
312or
313.Xr send 2
314system calls may be used).
315.Pp
316If
317.Fa proto
318is 0, the default protocol
319.Dv IPPROTO_RAW
320is used for outgoing
321packets, and only incoming packets destined for that protocol
322are received.
323If
324.Fa proto
325is non-zero, that protocol number will be used on outgoing packets
326and to filter incoming packets.
327.Pp
328Outgoing packets automatically have an
329.Tn IP
330header prepended to
331them (based on the destination address and the protocol
332number the socket is created with),
333unless the
334.Dv IP_HDRINCL
335option has been set.
336Incoming packets are received with
337.Tn IP
338header and options intact.
339.Pp
340.Dv IP_HDRINCL
341indicates the complete IP header is included with the data
342and may be used only with the
343.Dv SOCK_RAW
344type.
345.Bd -literal
346#include <netinet/ip.h>
347
348int hincl = 1;                  /* 1 = on, 0 = off */
349setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
350.Ed
351.sp
352Unlike previous
353.Tn BSD
354releases, the program must set all
355the fields of the IP header, including the following:
356.Bd -literal
357ip->ip_v = IPVERSION;
358ip->ip_hl = hlen >> 2;
359ip->ip_id = 0;  /* 0 means kernel set appropriate value */
360ip->ip_off = offset;
361.Ed
362.sp .5
363If the header source address is set to
364.Dv INADDR_ANY,
365the kernel will choose an appropriate address.
366.Sh DIAGNOSTICS
367A socket operation may fail with one of the following errors returned:
368.Bl -tag -width [EADDRNOTAVAIL]
369.It Bq Er EISCONN
370when trying to establish a connection on a socket which
371already has one, or when trying to send a datagram with the destination
372address specified and the socket is already connected;
373.It Bq Er ENOTCONN
374when trying to send a datagram, but
375no destination address is specified, and the socket hasn't been
376connected;
377.It Bq Er ENOBUFS
378when the system runs out of memory for
379an internal data structure;
380.It Bq Er EADDRNOTAVAIL
381when an attempt is made to create a
382socket with a network address for which no network interface
383exists.
384.It Bq Er EACESS
385when an attempt is made to create
386a raw IP socket by a non-privileged process.
387.El
388.Pp
389The following errors specific to
390.Tn IP
391may occur when setting or getting
392.Tn IP
393options:
394.Bl -tag -width EADDRNOTAVAILxx
395.It Bq Er EINVAL
396An unknown socket option name was given.
397.It Bq Er EINVAL
398The IP option field was improperly formed;
399an option field was shorter than the minimum value
400or longer than the option buffer provided.
401.El
402.Sh SEE ALSO
403.Xr getsockopt 2 ,
404.Xr send 2 ,
405.Xr recv 2 ,
406.Xr intro 4 ,
407.Xr icmp 4 ,
408.Xr inet 4
409.Sh HISTORY
410The
411.Nm
412protocol appeared in
413.Bx 4.2 .
414