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