1 .\" Copyright (c) 1983, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)ip.4 8.2 (Berkeley) 11/30/93
29 .\" $FreeBSD: src/share/man/man4/ip.4,v 1.13.2.9 2002/05/02 02:40:26 silby Exp $
30 .\" $DragonFly: src/share/man/man4/ip.4,v 1.7 2007/12/15 10:23:58 swildner Exp $
43 .Fn socket AF_INET SOCK_RAW proto
46 is the transport layer protocol used
47 by the Internet protocol family.
48 Options may be set at the
51 when using higher-level protocols that are based on
57 It may also be accessed
60 when developing new protocols, or
61 special-purpose applications.
70 may be used to provide
72 options to be transmitted in the
74 header of each outgoing packet
75 or to examine the header options on incoming packets.
77 options may be used with any socket type in the Internet family.
80 options to be sent is that specified by the
82 protocol specification (RFC 791), with one exception:
83 the list of addresses for Source Route options must include the first-hop
84 gateway at the beginning of the list of gateways.
85 The first-hop gateway address will be extracted from the option list
86 and the size adjusted accordingly before use.
87 To disable previously specified options,
88 use a zero-length buffer:
90 setsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0);
96 may be used to set the type-of-service and time-to-live
100 .Dv SOCK_STREAM , SOCK_DGRAM ,
106 int tos = IPTOS_LOWDELAY; /* see <netinet/ip.h> */
107 setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
109 int ttl = 60; /* max = 255 */
110 setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
114 may be used to set the minimum acceptable TTL a packet must have when
115 received on a socket.
116 All packets with a lower TTL are silently dropped.
117 Works on already connected/connecting and listening sockets for RAW/UDP/TCP.
118 It allows to implement security mechanisms described in RFC 3682 (GTSM).
122 option is enabled on a
127 call will return the destination
132 The msg_control field in the msghdr structure points to a buffer
133 that contains a cmsghdr structure followed by the
136 The cmsghdr fields have the following values:
138 cmsg_len = sizeof(struct in_addr)
139 cmsg_level = IPPROTO_IP
140 cmsg_type = IP_RECVDSTADDR
145 option is enabled on a
152 (time to live) field for a
155 The msg_control field in the msghdr structure points to a buffer
156 that contains a cmsghdr structure followed by the
158 The cmsghdr fields have the following values:
160 cmsg_len = sizeof(u_char)
161 cmsg_level = IPPROTO_IP
162 cmsg_type = IP_RECVTTL
166 may be used to set the port range used for selecting a local port number
167 on a socket with an unspecified (zero) port number.
170 .Bl -tag -width IP_PORTRANGE_DEFAULT
171 .It Dv IP_PORTRANGE_DEFAULT
172 use the default range of values, normally
175 .Dv IPPORT_USERRESERVED .
176 This is adjustable through the sysctl setting:
177 .Va net.inet.ip.portrange.first
179 .Va net.inet.ip.portrange.last .
180 .It Dv IP_PORTRANGE_HIGH
181 use a high range of values, normally
182 .Dv IPPORT_HIFIRSTAUTO
184 .Dv IPPORT_HILASTAUTO .
185 This is adjustable through the sysctl setting:
186 .Va net.inet.ip.portrange.hifirst
188 .Va net.inet.ip.portrange.hilast .
189 .It Dv IP_PORTRANGE_LOW
190 use a low range of ports, which are normally restricted to
191 privileged processes on
193 systems. The range is normally from
194 .Dv IPPORT_RESERVED - 1
196 .Li IPPORT_RESERVEDSTART
198 This is adjustable through the sysctl setting:
199 .Va net.inet.ip.portrange.lowfirst
201 .Va net.inet.ip.portrange.lowlast .
203 .Ss "Multicast Options"
205 multicasting is supported only on
211 and only on networks where the interface
212 driver supports multicasting.
216 option changes the time-to-live (TTL)
217 for outgoing multicast datagrams
218 in order to control the scope of the multicasts:
220 u_char ttl; /* range: 0 to 255, default = 1 */
221 setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
224 Datagrams with a TTL of 1 are not forwarded beyond the local network.
225 Multicast datagrams with a TTL of 0 will not be transmitted on any network,
226 but may be delivered locally if the sending host belongs to the destination
227 group and if multicast loopback has not been disabled on the sending socket
228 (see below). Multicast datagrams with TTL greater than 1 may be forwarded
229 to other networks if a multicast router is attached to the local network.
231 For hosts with multiple interfaces, each multicast transmission is
232 sent from the primary network interface.
235 option overrides the default for
236 subsequent transmissions from a given socket:
239 setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
242 where "addr" is the local
244 address of the desired interface or
246 to specify the default interface.
247 An interface's local IP address and multicast capability can
253 Normal applications should not need to use this option.
255 If a multicast datagram is sent to a group to which the sending host itself
256 belongs (on the outgoing interface), a copy of the datagram is, by default,
257 looped back by the IP layer for local delivery.
259 .Dv IP_MULTICAST_LOOP
260 option gives the sender explicit control
261 over whether or not subsequent datagrams are looped back:
263 u_char loop; /* 0 = disable, 1 = enable (default) */
264 setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
268 improves performance for applications that may have no more than one
269 instance on a single host (such as a router daemon), by eliminating
270 the overhead of receiving their own transmissions. It should generally not
271 be used by applications for which there may be more than one instance on a
272 single host (such as a conferencing program) or for which the sender does
273 not belong to the destination group (such as a time querying program).
275 A multicast datagram sent with an initial TTL greater than 1 may be delivered
276 to the sending host on a different interface from that on which it was sent,
277 if the host belongs to the destination group on that other interface. The
278 loopback control option has no effect on such delivery.
280 A host must become a member of a multicast group before it can receive
281 datagrams sent to the group. To join a multicast group, use the
282 .Dv IP_ADD_MEMBERSHIP
286 setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
291 is the following structure:
294 struct in_addr imr_multiaddr; /* IP multicast address of group */
295 struct in_addr imr_interface; /* local IP address of interface */
303 to choose the default multicast interface,
306 address of a particular multicast-capable interface if
307 the host is multihomed.
308 Membership is associated with a single interface;
309 programs running on multihomed hosts may need to
310 join the same group on more than one interface.
312 .Dv IP_MAX_MEMBERSHIPS
313 (currently 20) memberships may be added on a
316 To drop a membership, use:
319 setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
324 contains the same values as used to add the membership.
325 Memberships are dropped when the socket is closed or the process exits.
326 .\"-----------------------
330 sockets are connectionless,
331 and are normally used with the
337 call may also be used to fix the destination for future
338 packets (in which case the
346 system calls may be used).
350 is 0, the default protocol
353 packets, and only incoming packets destined for that protocol
357 is non-zero, that protocol number will be used on outgoing packets
358 and to filter incoming packets.
360 Outgoing packets automatically have an
363 them (based on the destination address and the protocol
364 number the socket is created with),
368 Incoming packets are received with
370 header and options intact.
373 indicates the complete IP header is included with the data
374 and may be used only with the
378 #include <netinet/in_systm.h>
379 #include <netinet/ip.h>
381 int hincl = 1; /* 1 = on, 0 = off */
382 setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
387 releases, the program must set all
388 the fields of the IP header, including the following:
390 ip->ip_v = IPVERSION;
391 ip->ip_hl = hlen >> 2;
392 ip->ip_id = 0; /* 0 means kernel set appropriate value */
396 If the header source address is set to
398 the kernel will choose an appropriate address.
400 A socket operation may fail with one of the following errors returned:
403 when trying to establish a connection on a socket which
404 already has one, or when trying to send a datagram with the destination
405 address specified and the socket is already connected;
407 when trying to send a datagram, but
408 no destination address is specified, and the socket hasn't been
411 when the system runs out of memory for
412 an internal data structure;
413 .It Bq Er EADDRNOTAVAIL
414 when an attempt is made to create a
415 socket with a network address for which no network interface
418 when an attempt is made to create
419 a raw IP socket by a non-privileged process.
422 The following errors specific to
424 may occur when setting or getting
427 .Bl -tag -width EADDRNOTAVAILxx
429 An unknown socket option name was given.
431 The IP option field was improperly formed;
432 an option field was shorter than the minimum value
433 or longer than the option buffer provided.