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 67and 68.Xr getsockopt 2 69options. 70.Dv IP_OPTIONS 71may be used to provide 72.Tn IP 73options to be transmitted in the 74.Tn IP 75header of each outgoing packet 76or to examine the header options on incoming packets. 77.Tn IP 78options may be used with any socket type in the Internet family. 79The format of 80.Tn IP 81options to be sent is that specified by the 82.Tn IP 83protocol specification (RFC-791), with one exception: 84the list of addresses for Source Route options must include the first-hop 85gateway at the beginning of the list of gateways. 86The first-hop gateway address will be extracted from the option list 87and the size adjusted accordingly before use. 88To disable previously specified options, 89use a zero-length buffer: 90.Bd -literal 91setsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0); 92.Ed 93.Pp 94.Dv IP_TOS 95and 96.Dv IP_TTL 97may be used to set the type-of-service and time-to-live 98fields in the 99.Tn IP 100header for 101.Dv SOCK_STREAM 102and 103.Dv SOCK_DGRAM 104sockets. For example, 105.Bd -literal 106int tos = IPTOS_LOWDELAY; /* see <netinet/in.h> */ 107setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); 108 109int ttl = 60; /* max = 255 */ 110setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); 111.Ed 112.Pp 113If the 114.Dv IP_RECVDSTADDR 115option is enabled on a 116.Dv SOCK_DGRAM 117socket, 118the 119.Xr recvmsg 2 120call will return the destination 121.Tn IP 122address for a 123.Tn UDP 124datagram. 125The msg_control field in the msghdr structure points to a buffer 126that contains a cmsghdr structure followed by the 127.Tn IP 128address. 129The cmsghdr fields have the following values: 130.Bd -literal 131cmsg_len = sizeof(struct in_addr) 132cmsg_level = IPPROTO_IP 133cmsg_type = IP_RECVDSTADDR 134.Ed 135.Pp 136.Dv IP_PORTRANGE 137may be used to set the port range used for selecting a local port number 138on a socket with an unspecified (zero) port number. It has the following 139possible values: 140.Bl -tag -width IP_PORTRANGE_DEFAULT 141.It Dv IP_PORTRANGE_DEFAULT 142use the default range of values, normally 143.Dv IPPORT_RESERVED 144through 145.Dv IPPORT_RESERVED . 146This is adjustable through the sysctl setting: 147.Nm net.inet.ip.portrange.first 148and 149.Nm net.inet.ip.portrange.last . 150.It Dv IP_PORTRANGE_HIGH 151use a high range of values, normally 152.Dv IPPORT_HIFIRSTAUTO 153and 154.Dv IPPORT_HILASTAUTO . 155This is adjustable through the sysctl setting: 156.Nm net.inet.ip.portrange.hifirst 157and 158.Nm net.inet.ip.portrange.hilast . 159.It Dv IP_PORTRANGE_LOW 160use a low range of ports, which are normally restricted to 161privileged processes on 162.Ux 163systems. The range is normally from 164.Dv IPPORT_RESERVED 165down to 166.Li 1 167in descending order. This range is not sysctl configurable. 168.El 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 2 301and 302.Xr recvfrom 2 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