1.\" $NetBSD: ip.4,v 1.10 1999/03/16 01:19:16 garbled 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 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 network layer protocol used by the Internet protocol family. 50Options may be set at the 51.Tn IP 52level when 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 through a 59.Dq raw socket 60when developing new protocols, or special-purpose applications. 61.Pp 62There are several 63.Tn IP-level 64.Xr setsockopt 2 / Ns Xr getsockopt 2 65options. 66.Dv IP_OPTIONS 67may be used to provide 68.Tn IP 69options to be transmitted in the 70.Tn IP 71header of each outgoing packet 72or to examine the header options on incoming packets. 73.Tn IP 74options may be used with any socket type in the Internet family. 75The format of 76.Tn IP 77options to be sent is that specified by the 78.Tn IP 79protocol specification (RFC-791), with one exception: 80the list of addresses for Source Route options must include the first-hop 81gateway at the beginning of the list of gateways. 82The first-hop gateway address will be extracted from the option list 83and the size adjusted accordingly before use. 84To disable previously specified options, use a zero-length buffer: 85.Bd -literal 86setsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0); 87.Ed 88.Pp 89.Dv IP_TOS 90and 91.Dv IP_TTL 92may be used to set the type-of-service and time-to-live fields in the 93.Tn IP 94header for 95.Dv SOCK_STREAM 96and 97.Dv SOCK_DGRAM 98sockets. 99For example, 100.Bd -literal 101int tos = IPTOS_LOWDELAY; /* see <netinet/in.h> */ 102setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); 103 104int ttl = 60; /* max = 255 */ 105setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); 106.Ed 107.Pp 108.Dv IP_PORTRANGE 109controls how ephemeral ports are allocated for 110.Dv SOCK_STREAM 111and 112.Dv SOCK_DGRAM 113sockets. For example, 114.Bd -literal 115int range = IP_PORTRANGE_LOW; /* see <netinet/in.h> */ 116setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &range, sizeof(range)); 117.Ed 118.Pp 119If the 120.Dv IP_RECVDSTADDR 121option is enabled on a 122.Dv SOCK_DGRAM 123or 124.Dv SOCK_RAW 125socket, 126the 127.Xr recvmsg 2 128call will return the destination 129.Tn IP 130address for a 131.Tn UDP 132datagram. 133The msg_control field in the msghdr structure points to a buffer 134that contains a cmsghdr structure followed by the 135.Tn IP 136address. 137The cmsghdr fields have the following values: 138.Bd -literal 139cmsg_len = sizeof(struct in_addr) 140cmsg_level = IPPROTO_IP 141cmsg_type = IP_RECVDSTADDR 142.Ed 143.Pp 144If the 145.Dv IP_RECVIF 146option is enabled on a 147.Dv SOCK_DGRAM 148or 149.Dv SOCK_RAW 150socket, 151the 152.Xr recvmsg 2 153call will return a struct sockaddr_dl corresponding to 154the interface on which the packet was received. 155the msg_control field in the msghdr structure points to a buffer 156that contains a cmsghdr structure followed by the struct sockaddr_dl. 157The cmsghdr fields have the following values: 158.Bd -literal 159cmsg_len = sizeof(struct sockaddr_dl) 160cmsg_level = IPPROTO_IP 161cmsg_type = IP_RECVIF 162.Ed 163.Ss "Multicast Options" 164.Pp 165.Tn IP 166multicasting is supported only on 167.Dv AF_INET 168sockets of type 169.Dv SOCK_DGRAM 170and 171.Dv SOCK_RAW, 172and only on networks where the interface driver supports multicasting. 173.Pp 174The 175.Dv IP_MULTICAST_TTL 176option changes the time-to-live (TTL) for outgoing multicast datagrams 177in order to control the scope of the multicasts: 178.Bd -literal 179u_char ttl; /* range: 0 to 255, default = 1 */ 180setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); 181.Ed 182.Pp 183Datagrams with a TTL of 1 are not forwarded beyond the local network. 184Multicast datagrams with a TTL of 0 will not be transmitted on any network, 185but may be delivered locally if the sending host belongs to the destination 186group and if multicast loopback has not been disabled on the sending socket 187(see below). 188Multicast datagrams with TTL greater than 1 may be forwarded 189to other networks if a multicast router is attached to the local network. 190.Pp 191For hosts with multiple interfaces, each multicast transmission is 192sent from the primary network interface. 193The 194.Dv IP_MULTICAST_IF 195option overrides the default for 196subsequent transmissions from a given socket: 197.Bd -literal 198struct in_addr addr; 199setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr)); 200.Ed 201.Pp 202where "addr" is the local 203.Tn IP 204address of the desired interface or 205.Dv INADDR_ANY 206to specify the default interface. 207An interface's local IP address and multicast capability can 208be obtained via the 209.Dv SIOCGIFCONF 210and 211.Dv SIOCGIFFLAGS 212ioctls. 213Normal applications should not need to use this option. 214.Pp 215If a multicast datagram is sent to a group to which the sending host itself 216belongs (on the outgoing interface), a copy of the datagram is, by default, 217looped back by the IP layer for local delivery. 218The 219.Dv IP_MULTICAST_LOOP 220option gives the sender explicit control 221over whether or not subsequent datagrams are looped back: 222.Bd -literal 223u_char loop; /* 0 = disable, 1 = enable (default) */ 224setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); 225.Ed 226.Pp 227This option 228improves performance for applications that may have no more than one 229instance on a single host (such as a router demon), by eliminating 230the overhead of receiving their own transmissions. 231It should generally not be used by applications for which there 232may be more than one instance on a single host (such as a conferencing 233program) or for which the sender does not belong to the destination 234group (such as a time querying program). 235.Pp 236A multicast datagram sent with an initial TTL greater than 1 may be delivered 237to the sending host on a different interface from that on which it was sent, 238if the host belongs to the destination group on that other interface. 239The loopback control option has no effect on such delivery. 240.Pp 241A host must become a member of a multicast group before it can receive 242datagrams sent to the group. 243To join a multicast group, use the 244.Dv IP_ADD_MEMBERSHIP 245option: 246.Bd -literal 247struct ip_mreq mreq; 248setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); 249.Ed 250.Pp 251where 252.Fa mreq 253is the following structure: 254.Bd -literal 255struct ip_mreq { 256 struct in_addr imr_multiaddr; /* multicast group to join */ 257 struct in_addr imr_interface; /* interface to join on */ 258} 259.Ed 260.Pp 261.Dv imr_interface 262should be 263.Dv INADDR_ANY 264to choose the default multicast interface, or the 265.Tn IP 266address of a particular multicast-capable interface if 267the host is multihomed. 268Membership is associated with a single interface; 269programs running on multihomed hosts may need to 270join the same group on more than one interface. 271Up to 272.Dv IP_MAX_MEMBERSHIPS 273(currently 20) memberships may be added on a single socket. 274.Pp 275To drop a membership, use: 276.Bd -literal 277struct ip_mreq mreq; 278setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); 279.Ed 280.Pp 281where 282.Fa mreq 283contains the same values as used to add the membership. 284Memberships are dropped when the socket is closed or the process exits. 285.\"----------------------- 286.Ss "Raw IP Sockets" 287.Pp 288Raw 289.Tn IP 290sockets are connectionless, and are normally used with the 291.Xr sendto 2 292and 293.Xr recvfrom 2 294calls, though the 295.Xr connect 2 296call may also be used to fix the destination for future 297packets (in which case the 298.Xr read 2 299or 300.Xr recv 2 301and 302.Xr write 2 303or 304.Xr send 2 305system calls may be used). 306.Pp 307If 308.Fa proto 309is 0, the default protocol 310.Dv IPPROTO_RAW 311is used for outgoing packets, and only incoming packets destined 312for that protocol are received. 313If 314.Fa proto 315is non-zero, that protocol number will be used on outgoing packets 316and to filter incoming packets. 317.Pp 318Outgoing packets automatically have an 319.Tn IP 320header prepended to them (based on the destination address and the 321protocol number the socket is created with), unless the 322.Dv IP_HDRINCL 323option has been set. 324Incoming packets are received with 325.Tn IP 326header and options intact. 327.Pp 328.Dv IP_HDRINCL 329indicates the complete IP header is included with the data and may 330be used only with the 331.Dv SOCK_RAW 332type. 333.Bd -literal 334#include <netinet/ip.h> 335 336int hincl = 1; /* 1 = on, 0 = off */ 337setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)); 338.Ed 339.Pp 340Unlike previous 341.Bx 342releases, the program must set all 343the fields of the IP header, including the following: 344.Bd -literal 345ip->ip_v = IPVERSION; 346ip->ip_hl = hlen >> 2; 347ip->ip_id = 0; /* 0 means kernel set appropriate value */ 348ip->ip_off = offset; 349.Ed 350.Pp 351If the header source address is set to 352.Dv INADDR_ANY, 353the kernel will choose an appropriate address. 354.Sh DIAGNOSTICS 355A socket operation may fail with one of the following errors returned: 356.Bl -tag -width [EADDRNOTAVAIL] 357.It Bq Er EISCONN 358when trying to establish a connection on a socket which already 359has one, or when trying to send a datagram with the destination 360address specified and the socket is already connected; 361.It Bq Er ENOTCONN 362when trying to send a datagram, but no destination address is 363specified, and the socket hasn't been connected; 364.It Bq Er ENOBUFS 365when the system runs out of memory for an internal data structure; 366.It Bq Er EADDRNOTAVAIL 367when an attempt is made to create a socket with a network address 368for which no network interface exists. 369.It Bq Er EACCES 370when an attempt is made to create a raw IP socket by a non-privileged process. 371.El 372.Pp 373The following errors specific to 374.Tn IP 375may occur when setting or getting 376.Tn IP 377options: 378.Bl -tag -width EADDRNOTAVAILxx 379.It Bq Er EINVAL 380An unknown socket option name was given. 381.It Bq Er EINVAL 382The IP option field was improperly formed; an option field was 383shorter than the minimum value or longer than the option buffer provided. 384.El 385.Sh SEE ALSO 386.Xr getsockopt 2 , 387.Xr send 2 , 388.Xr recv 2 , 389.Xr intro 4 , 390.Xr icmp 4 , 391.Xr inet 4 392.Rs 393.%R RFC 394.%N 791 395.%D September 1981 396.%T "Internet Protocol" 397.Re 398.Rs 399.%R RFC 400.%N 1112 401.%D August 1989 402.%T "Host Extensions for IP Multicasting" 403.Re 404.Rs 405.%R RFC 406.%N 1122 407.%D October 1989 408.%T "Requirements for Internet Hosts -- Communication Layers" 409.Re 410.Sh HISTORY 411The 412.Nm 413protocol appeared in 414.Bx 4.2 . 415