xref: /openbsd-src/share/man/man4/ip.4 (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1.\"	$OpenBSD: ip.4,v 1.33 2011/09/08 16:43:56 giovanni Exp $
2.\"	$NetBSD: ip.4,v 1.3 1994/11/30 16:22:19 jtc Exp $
3.\"
4.\" Copyright (c) 1983, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)ip.4	8.2 (Berkeley) 11/30/93
32.\"
33.Dd $Mdocdate: September 8 2011 $
34.Dt IP 4
35.Os
36.Sh NAME
37.Nm ip
38.Nd Internet Protocol
39.Sh SYNOPSIS
40.Fd #include <sys/types.h>
41.Fd #include <sys/socket.h>
42.Fd #include <netinet/in.h>
43.Ft int
44.Fn socket AF_INET SOCK_RAW proto
45.Sh DESCRIPTION
46.Tn IP
47is the network layer protocol used
48by the Internet protocol family.
49Options may be set at the
50.Tn IP
51level
52when using higher-level protocols that are based on
53.Tn IP
54(such as
55.Tn TCP
56and
57.Tn UDP ) .
58It may also be accessed
59through a
60.Dq raw socket
61when developing new protocols, or
62special-purpose applications.
63.Pp
64There are several
65.Tn IP-level
66.Xr setsockopt 2 Ns / Ns Xr getsockopt 2
67options.
68.Dv IP_OPTIONS
69may be used to provide
70.Tn IP
71options to be transmitted in the
72.Tn IP
73header of each outgoing packet
74or to examine the header options on incoming packets.
75.Tn IP
76options may be used with any socket type in the Internet family.
77The format of
78.Tn IP
79options to be sent is that specified by the
80.Tn IP
81protocol specification (RFC 791), with one exception:
82the list of addresses for Source Route options must include the first-hop
83gateway at the beginning of the list of gateways.
84The first-hop gateway address will be extracted from the option list
85and the size adjusted accordingly before use.
86To disable previously specified options,
87use a zero-length buffer:
88.Bd -literal -offset indent
89setsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0);
90.Ed
91.Pp
92.Dv IP_TOS
93and
94.Dv IP_TTL
95may be used to set the type-of-service and time-to-live
96fields in the
97.Tn IP
98header for
99.Dv SOCK_STREAM
100and
101.Dv SOCK_DGRAM
102sockets.
103For example,
104.Bd -literal -offset indent
105int tos = IPTOS_LOWDELAY;       /* see <netinet/ip.h> */
106setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
107
108int ttl = 60;                   /* max = 255 */
109setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
110.Ed
111.Pp
112If the
113.Dv IP_RECVDSTADDR
114option is enabled on a
115.Dv SOCK_DGRAM
116socket,
117the
118.Xr recvmsg 2
119call will return the destination
120.Tn IP
121address for a
122.Tn UDP
123datagram.
124The
125.Va msg_control
126field in the
127.Vt msghdr
128structure points to a buffer that contains a
129.Vt cmsghdr
130structure followed by the
131.Tn IP
132address.
133The
134.Vt cmsghdr
135fields have the following values:
136.Bd -literal -offset indent
137cmsg_len = CMSG_LEN(sizeof(struct in_addr))
138cmsg_level = IPPROTO_IP
139cmsg_type = IP_RECVDSTADDR
140.Ed
141.Pp
142If the
143.Dv IP_RECVDSTPORT
144option is enabled on a
145.Dv SOCK_DGRAM
146socket,
147the
148.Xr recvmsg 2
149call will return the destination
150port for a
151.Tn UDP
152datagram.
153The
154.Va msg_control
155field in the
156.Vt msghdr
157structure points to a buffer that contains a
158.Vt cmsghdr
159structure followed by the port in 16-bit network byte order.
160The
161.Vt cmsghdr
162fields have the following values:
163.Bd -literal -offset indent
164cmsg_len = CMSG_LEN(sizeof(u_int16_t))
165cmsg_level = IPPROTO_IP
166cmsg_type = IP_RECVDSTPORT
167.Ed
168.Pp
169If the
170.Dv IP_RECVTTL
171option is enabled on a
172.Dv SOCK_DGRAM
173or
174.Dv SOCK_RAW
175socket, the
176.Xr recvmsg 2
177call will return the
178.Tn TTL
179of the received datagram.
180The
181.Va msg_control
182field in the
183.Vt msghdr
184structure points to a buffer that contains a
185.Vt cmsghdr
186structure followed by the
187.Tn TTL
188value.
189The
190.Vt cmsghdr
191fields have the following values:
192.Bd -literal -offset indent
193cmsg_len = CMSG_LEN(sizeof(u_int8_t))
194cmsg_level = IPPROTO_IP
195cmsg_type = IP_RECVTTL
196.Ed
197.Pp
198The
199.Dv IP_MINTTL
200option may be used on
201.Dv SOCK_STREAM
202sockets to discard packets with a TTL lower than the option value.
203This can be used to implement the
204.Em Generalized TTL Security Mechanism (GTSM)
205according to RFC 3682.
206To discard all packets with a TTL lower than 255:
207.Bd -literal -offset indent
208int minttl = 255;
209setsockopt(s, IPPROTO_IP, IP_MINTTL, &minttl, sizeof(minttl));
210.Ed
211.Pp
212The
213.Dv IP_PORTRANGE
214option causes the default allocation policy for when the kernel is asked
215to choose a free port number.
216Three choices are available:
217.Pp
218.Bl -tag -width IP_PORTRANGE_DEFAULT -compact -offset indent
219.It Dv IP_PORTRANGE_DEFAULT
220The regular range of non-reserved ports.
221.It Dv IP_PORTRANGE_HIGH
222A high range, for fun.
223.It Dv IP_PORTRANGE_LOW
224Reserved ports; between 600 and 1023.
225.El
226.Pp
227If the
228.Dv IP_RECVRTABLE
229option is enabled on a
230.Dv SOCK_DGRAM
231socket,
232the
233.Xr recvmsg 2
234call will return the source routing domain for a
235.Tn UDP
236datagram.
237The
238.Va msg_control
239field in the
240.Vt msghdr
241structure points to a buffer that contains a
242.Vt cmsghdr
243structure followed by the routing table ID.
244The
245.Vt cmsghdr
246fields have the following values:
247.Bd -literal -offset indent
248cmsg_len = CMSG_LEN(sizeof(u_int))
249cmsg_level = IPPROTO_IP
250cmsg_type = IP_RECVRTABLE
251.Ed
252.Ss "Multicast Options"
253.Tn IP
254multicasting is supported only on
255.Dv AF_INET
256sockets of type
257.Dv SOCK_DGRAM
258and
259.Dv SOCK_RAW ,
260and only on networks where the interface
261driver supports multicasting.
262.Pp
263The
264.Dv IP_MULTICAST_TTL
265option changes the time-to-live (TTL)
266for outgoing multicast datagrams
267in order to control the scope of the multicasts:
268.Bd -literal -offset indent
269u_char ttl;	/* range: 0 to 255, default = 1 */
270setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
271.Ed
272.Pp
273Datagrams with a TTL of 1 are not forwarded beyond the local network.
274Multicast datagrams with a TTL of 0 will not be transmitted on any network,
275but may be delivered locally if the sending host belongs to the destination
276group and if multicast loopback has not been disabled on the sending socket
277(see below).
278Multicast datagrams with TTL greater than 1 may be forwarded
279to other networks if a multicast router is attached to the local network.
280.Pp
281For hosts with multiple interfaces, each multicast transmission is
282sent from the primary network interface.
283The
284.Dv IP_MULTICAST_IF
285option overrides the default for
286subsequent transmissions from a given socket:
287.Bd -literal -offset indent
288struct in_addr addr;
289setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
290.Ed
291.Pp
292where
293.Va addr
294is the local
295.Tn IP
296address of the desired interface or
297.Dv INADDR_ANY
298to specify the default interface.
299An interface's local IP address and multicast capability can
300be obtained via the
301.Dv SIOCGIFCONF
302and
303.Dv SIOCGIFFLAGS
304.Xr ioctl 2 Ns 's .
305Normal applications should not need to use this option.
306.Pp
307If a multicast datagram is sent to a group to which the sending host itself
308belongs (on the outgoing interface), a copy of the datagram is, by default,
309looped back by the IP layer for local delivery.
310The
311.Dv IP_MULTICAST_LOOP
312option gives the sender explicit control
313over whether or not subsequent datagrams are looped back:
314.Bd -literal -offset indent
315u_char loop;	/* 0 = disable, 1 = enable (default) */
316setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
317.Ed
318.Pp
319This option
320improves performance for applications that may have no more than one
321instance on a single host (such as a router daemon), by eliminating
322the overhead of receiving their own transmissions.
323It should generally not
324be used by applications for which there may be more than one instance on a
325single host (such as a conferencing program) or for which the sender does
326not belong to the destination group (such as a time querying program).
327.Pp
328A multicast datagram sent with an initial TTL greater than 1 may be delivered
329to the sending host on a different interface from that on which it was sent,
330if the host belongs to the destination group on that other interface.
331The loopback control option has no effect on such delivery.
332.Pp
333A host must become a member of a multicast group before it can receive
334datagrams sent to the group.
335To join a multicast group, use the
336.Dv IP_ADD_MEMBERSHIP
337option:
338.Bd -literal -offset indent
339struct ip_mreq mreq;
340setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
341.Ed
342.Pp
343where
344.Fa mreq
345is the following structure:
346.Bd -literal -offset indent
347struct ip_mreq {
348    struct in_addr imr_multiaddr; /* multicast group to join */
349    struct in_addr imr_interface; /* interface to join on */
350}
351.Ed
352.Pp
353.Va imr_interface
354should
355be
356.Dv INADDR_ANY
357to choose the default multicast interface,
358or the
359.Tn IP
360address of a particular multicast-capable interface if
361the host is multihomed.
362Membership is associated with a single interface;
363programs running on multihomed hosts may need to
364join the same group on more than one interface.
365Up to
366.Dv IP_MAX_MEMBERSHIPS
367(currently 4095) memberships may be added on a
368single socket.
369.Pp
370To drop a membership, use:
371.Bd -literal -offset indent
372struct ip_mreq mreq;
373setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
374.Ed
375.Pp
376where
377.Fa mreq
378contains the same values as used to add the membership.
379Memberships are dropped when the socket is closed or the process exits.
380.\"-----------------------
381.Ss "Raw IP Sockets"
382Raw
383.Tn IP
384sockets are connectionless,
385and are normally used with the
386.Xr sendto 2
387and
388.Xr recvfrom 2
389calls, though the
390.Xr connect 2
391call may also be used to fix the destination for future
392packets (in which case the
393.Xr read 2
394or
395.Xr recv 2
396and
397.Xr write 2
398or
399.Xr send 2
400system calls may be used).
401.Pp
402If
403.Fa proto
404is 0, the default protocol
405.Dv IPPROTO_RAW
406is used for outgoing
407packets, and only incoming packets destined for that protocol
408are received.
409If
410.Fa proto
411is non-zero, that protocol number will be used on outgoing packets
412and to filter incoming packets.
413.Pp
414Outgoing packets automatically have an
415.Tn IP
416header prepended to
417them (based on the destination address and the protocol
418number the socket is created with),
419unless the
420.Dv IP_HDRINCL
421option has been set.
422Incoming packets are received with
423.Tn IP
424header and options intact.
425.Pp
426.Dv IP_HDRINCL
427indicates the complete IP header is included with the data
428and may be used only with the
429.Dv SOCK_RAW
430type.
431.Bd -literal -offset indent
432#include <netinet/ip.h>
433
434int hincl = 1;                  /* 1 = on, 0 = off */
435setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
436.Ed
437.Pp
438Unlike previous
439.Bx
440releases, the program must set all
441the fields of the IP header, including the following:
442.Bd -literal -offset indent
443ip->ip_v = IPVERSION;
444ip->ip_hl = hlen >> 2;
445ip->ip_id = 0;  /* 0 means kernel set appropriate value */
446ip->ip_off = htons(offset);
447ip->ip_len = htons(len);
448.Ed
449.Pp
450Additionally note that starting with
451.Ox 2.1 ,
452the
453.Va ip_off
454and
455.Va ip_len
456fields are in network byte order.
457If the header source address is set to
458.Dv INADDR_ANY ,
459the kernel will choose an appropriate address.
460.Sh DIAGNOSTICS
461A socket operation may fail with one of the following errors returned:
462.Bl -tag -width [EADDRNOTAVAIL]
463.It Bq Er EISCONN
464when trying to establish a connection on a socket which
465already has one, or when trying to send a datagram with the destination
466address specified and the socket is already connected;
467.It Bq Er ENOTCONN
468when trying to send a datagram, but
469no destination address is specified, and the socket hasn't been
470connected;
471.It Bq Er ENOBUFS
472when the system runs out of memory for
473an internal data structure;
474.It Bq Er EADDRNOTAVAIL
475when an attempt is made to create a
476socket with a network address for which no network interface
477exists.
478.It Bq Er EACCES
479when an attempt is made to create
480a raw IP socket by a non-privileged process.
481.El
482.Pp
483The following errors specific to
484.Tn IP
485may occur when setting or getting
486.Tn IP
487options:
488.Bl -tag -width EADDRNOTAVAILxx
489.It Bq Er EINVAL
490An unknown socket option name was given.
491.It Bq Er EINVAL
492The IP option field was improperly formed;
493an option field was shorter than the minimum value
494or longer than the option buffer provided.
495.El
496.Sh SEE ALSO
497.Xr getsockopt 2 ,
498.Xr ioctl 2 ,
499.Xr recv 2 ,
500.Xr send 2 ,
501.Xr icmp 4 ,
502.Xr inet 4 ,
503.Xr netintro 4
504.Sh HISTORY
505The
506.Nm
507protocol appeared in
508.Bx 4.2 .
509