147675Scael.\" Copyright (c) 1983, 1991 The Regents of the University of California. 236752Sbostic.\" All rights reserved. 320728Smckusick.\" 443581Strent.\" %sccs.include.redist.man% 520728Smckusick.\" 6*60006Sandrew.\" @(#)ip.4 6.6 (Berkeley) 05/13/93 736752Sbostic.\" 847675Scael.Dd 947675Scael.Dt IP 4 1047675Scael.Os BSD 4.2 1147675Scael.Sh NAME 1247675Scael.Nm ip 1347675Scael.Nd Internet Protocol 1447675Scael.Sh SYNOPSIS 1547675Scael.Fd #include <sys/socket.h> 1647675Scael.Fd #include <netinet/in.h> 1747675Scael.Ft int 1847675Scael.Fn socket AF_INET SOCK_RAW proto 1947675Scael.Sh DESCRIPTION 2047675Scael.Tn IP 2147675Scaelis the transport layer protocol used 2228252Skarelsby the Internet protocol family. 2347675ScaelOptions may be set at the 2447675Scael.Tn IP 2547675Scaellevel 2647675Scaelwhen using higher-level protocols that are based on 2747675Scael.Tn IP 2847675Scael(such as 2947675Scael.Tn TCP 3047675Scaeland 3147675Scael.Tn UDP ) . 3228252SkarelsIt may also be accessed 3347675Scaelthrough a 3447675Scael.Dq raw socket 3547675Scaelwhen developing new protocols, or 36*60006Sandrewspecial-purpose applications. 3747675Scael.Pp 38*60006SandrewThere are several 39*60006Sandrew.Tn IP -level 40*60006Sandrew.Xr setsockopt 2 / Ns 41*60006Sandrew.Xr getsockopt 2 42*60006Sandrewoptions. 43*60006Sandrew.Dv IP_OPTIONS 44*60006Sandrewmay be used to provide 4547675Scael.Tn IP 4647675Scaeloptions to be transmitted in the 4747675Scael.Tn IP 48*60006Sandrewheader of each outgoing packet 49*60006Sandrewor to examine the header options on incoming packets. 50*60006Sandrew.Tn IP 51*60006Sandrewoptions may be used with any socket type in the Internet family. 5247675ScaelThe format of 5347675Scael.Tn IP 5447675Scaeloptions to be sent is that specified by the 55*60006Sandrew.Tn IP 56*60006Sandrewprotocol specification (RFC-791), with one exception: 5728252Skarelsthe list of addresses for Source Route options must include the first-hop 5828252Skarelsgateway at the beginning of the list of gateways. 5928252SkarelsThe first-hop gateway address will be extracted from the option list 6028252Skarelsand the size adjusted accordingly before use. 61*60006SandrewTo disable previously specified options, 62*60006Sandrewuse a zero-length buffer: 63*60006Sandrew.Bd -literal 64*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0); 65*60006Sandrew.Ed 6647675Scael.Pp 67*60006Sandrew.Dv IP_TOS 68*60006Sandrewand 69*60006Sandrew.Dv IP_TTL 70*60006Sandrewmay be used to set the type-of-service and time-to-live 71*60006Sandrewfields in the 72*60006Sandrew.Tn IP 73*60006Sandrewheader for 74*60006Sandrew.Dv SOCK_STREAM 75*60006Sandrewand 76*60006Sandrew.Dv SOCK_DGRAM 77*60006Sandrewsockets. For example, 78*60006Sandrew.Bd -literal 79*60006Sandrewint tos = IPTOS_LOWDELAY; /* see <netinet/in.h> */ 80*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); 81*60006Sandrew 82*60006Sandrewint ttl = 60; /* max = 255 */ 83*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); 84*60006Sandrew.Ed 85*60006Sandrew.Pp 86*60006SandrewIf the 87*60006Sandrew.Dv IP_RECVDSTADDR 88*60006Sandrewoption is enabled on a 89*60006Sandrew.Dv SOCK_DGRAM 90*60006Sandrewsocket, 91*60006Sandrewthe 92*60006Sandrew.Xr recvmsg 93*60006Sandrewcall will return the destination 94*60006Sandrew.Tn IP 95*60006Sandrewaddress for a 96*60006Sandrew.Tn UDP 97*60006Sandrewdatagram. 98*60006SandrewThe msg_control field in the msghdr structure points to a buffer 99*60006Sandrewthat contains a cmsghdr structure followed by the 100*60006Sandrew.Tn IP 101*60006Sandrewaddress. 102*60006SandrewThe cmsghdr fields have the following values: 103*60006Sandrew.Bd -literal 104*60006Sandrewcmsg_len = sizeof(struct in_addr) 105*60006Sandrewcmsg_level = IPPROTO_IP 106*60006Sandrewcmsg_type = IP_RECVDSTADDR 107*60006Sandrew.Ed 108*60006Sandrew.Ss "Multicast Options" 109*60006Sandrew.Pp 110*60006Sandrew.Tn IP 111*60006Sandrewmulticasting is supported only on 112*60006Sandrew.Dv AF_INET 113*60006Sandrewsockets of type 114*60006Sandrew.Dv SOCK_DGRAM 115*60006Sandrewand 116*60006Sandrew.Dv SOCK_RAW, 117*60006Sandrewand only on networks where the interface 118*60006Sandrewdriver supports multicasting. 119*60006Sandrew.Pp 120*60006SandrewThe 121*60006Sandrew.Dv IP_MULTICAST_TTL 122*60006Sandrewoption changes the time-to-live (TTL) 123*60006Sandrewfor outgoing multicast datagrams 124*60006Sandrewin order to control the scope of the multicasts: 125*60006Sandrew.Bd -literal 126*60006Sandrewu_char ttl; /* range: 0 to 255, default = 1 */ 127*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); 128*60006Sandrew.Ed 129*60006Sandrew.sp 130*60006SandrewDatagrams with a TTL of 1 are not forwarded beyond the local network. 131*60006SandrewMulticast datagrams with a TTL of 0 will not be transmitted on any network, 132*60006Sandrewbut may be delivered locally if the sending host belongs to the destination 133*60006Sandrewgroup and if multicast loopback has not been disabled on the sending socket 134*60006Sandrew(see below). Multicast datagrams with TTL greater than 1 may be forwarded 135*60006Sandrewto other networks if a multicast router is attached to the local network. 136*60006Sandrew.Pp 137*60006SandrewFor hosts with multiple interfaces, each multicast transmission is 138*60006Sandrewsent from the primary network interface. 139*60006SandrewThe 140*60006Sandrew.Dv IP_MULTICAST_IF 141*60006Sandrewoption overrides the default for 142*60006Sandrewsubsequent transmissions from a given socket: 143*60006Sandrew.Bd -literal 144*60006Sandrewstruct in_addr addr; 145*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr)); 146*60006Sandrew.Ed 147*60006Sandrew.sp 148*60006Sandrewwhere "addr" is the local 149*60006Sandrew.Tn IP 150*60006Sandrewaddress of the desired interface or 151*60006Sandrew.Dv INADDR_ANY 152*60006Sandrewto specify the default interface. 153*60006SandrewAn interface's local IP address and multicast capability can 154*60006Sandrewbe obtained via the 155*60006Sandrew.Dv SIOCGIFCONF 156*60006Sandrewand 157*60006Sandrew.Dv SIOCGIFFLAGS 158*60006Sandrewioctls. 159*60006SandrewNormal applications should not need to use this option. 160*60006Sandrew.Pp 161*60006SandrewIf a multicast datagram is sent to a group to which the sending host itself 162*60006Sandrewbelongs (on the outgoing interface), a copy of the datagram is, by default, 163*60006Sandrewlooped back by the IP layer for local delivery. 164*60006SandrewThe 165*60006Sandrew.Dv IP_MULTICAST_LOOP 166*60006Sandrewoption gives the sender explicit control 167*60006Sandrewover whether or not subsequent datagrams are looped back: 168*60006Sandrew.Bd -literal 169*60006Sandrewu_char loop; /* 0 = disable, 1 = enable (default) */ 170*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); 171*60006Sandrew.Ed 172*60006Sandrew.sp 173*60006SandrewThis option 174*60006Sandrewimproves performance for applications that may have no more than one 175*60006Sandrewinstance on a single host (such as a router demon), by eliminating 176*60006Sandrewthe overhead of receiving their own transmissions. It should generally not 177*60006Sandrewbe used by applications for which there may be more than one instance on a 178*60006Sandrewsingle host (such as a conferencing program) or for which the sender does 179*60006Sandrewnot belong to the destination group (such as a time querying program). 180*60006Sandrew.Pp 181*60006SandrewA multicast datagram sent with an initial TTL greater than 1 may be delivered 182*60006Sandrewto the sending host on a different interface from that on which it was sent, 183*60006Sandrewif the host belongs to the destination group on that other interface. The 184*60006Sandrewloopback control option has no effect on such delivery. 185*60006Sandrew.Pp 186*60006SandrewA host must become a member of a multicast group before it can receive 187*60006Sandrewdatagrams sent to the group. To join a multicast group, use the 188*60006Sandrew.Dv IP_ADD_MEMBERSHIP 189*60006Sandrewoption: 190*60006Sandrew.Bd -literal 191*60006Sandrewstruct ip_mreq mreq; 192*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); 193*60006Sandrew.Ed 194*60006Sandrew.sp 195*60006Sandrewwhere 196*60006Sandrew.Fa mreq 197*60006Sandrewis the following structure: 198*60006Sandrew.Bd -literal 199*60006Sandrewstruct ip_mreq { 200*60006Sandrew struct in_addr imr_multiaddr; /* multicast group to join */ 201*60006Sandrew struct in_addr imr_interface; /* interface to join on */ 202*60006Sandrew} 203*60006Sandrew.Ed 204*60006Sandrew.sp 205*60006Sandrew.Dv imr_interface 206*60006Sandrewshould 207*60006Sandrewbe 208*60006Sandrew.Dv INADDR_ANY 209*60006Sandrewto choose the default multicast interface, 210*60006Sandrewor the 211*60006Sandrew.Tn IP 212*60006Sandrewaddress of a particular multicast-capable interface if 213*60006Sandrewthe host is multihomed. 214*60006SandrewMembership is associated with a single interface; 215*60006Sandrewprograms running on multihomed hosts may need to to 216*60006Sandrewjoin the same group on more than one interface. 217*60006SandrewUp to 218*60006Sandrew.Dv IP_MAX_MEMBERSHIPS 219*60006Sandrew(currently 20) memberships may be added on a 220*60006Sandrewsingle socket. 221*60006Sandrew.Pp 222*60006SandrewTo drop a membership, use: 223*60006Sandrew.Bd -literal 224*60006Sandrewstruct ip_mreq mreq; 225*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); 226*60006Sandrew.Ed 227*60006Sandrew.sp 228*60006Sandrewwhere 229*60006Sandrew.Fa mreq 230*60006Sandrewcontains the same values as used to add the membership. 231*60006SandrewMemberships are dropped when the socket is closed or the process exits. 232*60006Sandrew.\"----------------------- 233*60006Sandrew.Ss "Raw IP Sockets" 234*60006Sandrew.Pp 23547675ScaelRaw 23647675Scael.Tn IP 23747675Scaelsockets are connectionless, 23820728Smckusickand are normally used with the 23947675Scael.Xr sendto 24020728Smckusickand 24147675Scael.Xr recvfrom 24220728Smckusickcalls, though the 24347675Scael.Xr connect 2 24420728Smckusickcall may also be used to fix the destination for future 24520728Smckusickpackets (in which case the 24647675Scael.Xr read 2 24720728Smckusickor 24847675Scael.Xr recv 2 24920728Smckusickand 25047675Scael.Xr write 2 25120728Smckusickor 25247675Scael.Xr send 2 25320728Smckusicksystem calls may be used). 25447675Scael.Pp 25528252SkarelsIf 25647675Scael.Fa proto 25747675Scaelis 0, the default protocol 25847675Scael.Dv IPPROTO_RAW 25947675Scaelis used for outgoing 26028252Skarelspackets, and only incoming packets destined for that protocol 26128252Skarelsare received. 26228252SkarelsIf 26347675Scael.Fa proto 26428252Skarelsis non-zero, that protocol number will be used on outgoing packets 26528252Skarelsand to filter incoming packets. 26647675Scael.Pp 26747675ScaelOutgoing packets automatically have an 26847675Scael.Tn IP 26947675Scaelheader prepended to 27020728Smckusickthem (based on the destination address and the protocol 271*60006Sandrewnumber the socket is created with), 272*60006Sandrewunless the 273*60006Sandrew.Dv IP_HDRINCL 274*60006Sandrewoption has been set. 27547675ScaelIncoming packets are received with 27647675Scael.Tn IP 27747675Scaelheader and options intact. 278*60006Sandrew.Pp 279*60006Sandrew.Dv IP_HDRINCL 280*60006Sandrewindicates the complete IP header is included with the data 281*60006Sandrewand may be used only with the 282*60006Sandrew.Dv SOCK_RAW 283*60006Sandrewtype. 284*60006Sandrew.Bd -literal 285*60006Sandrew#include <netinet/ip.h> 286*60006Sandrew 287*60006Sandrewint hincl = 1; /* 1 = on, 0 = off */ 288*60006Sandrewsetsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)); 289*60006Sandrew.Ed 290*60006Sandrew.sp 291*60006SandrewUnlike previous 292*60006Sandrew.Tn BSD 293*60006Sandrewreleases, the program must set all 294*60006Sandrewthe fields of the IP header, including the following: 295*60006Sandrew.Bd -literal 296*60006Sandrewip->ip_v = IPVERSION; 297*60006Sandrewip->ip_hl = hlen >> 2; 298*60006Sandrewip->ip_id = 0; /* 0 means kernel set appropriate value */ 299*60006Sandrewip->ip_off = offset; 300*60006Sandrew.Ed 301*60006Sandrew.sp .5 302*60006SandrewIf the header source address is set to 303*60006Sandrew.Dv INADDR_ANY, 304*60006Sandrewthe kernel will choose an appropriate address. 30547675Scael.Sh DIAGNOSTICS 30620728SmckusickA socket operation may fail with one of the following errors returned: 30747675Scael.Bl -tag -width [EADDRNOTAVAIL] 30847675Scael.It Bq Er EISCONN 30920728Smckusickwhen trying to establish a connection on a socket which 31020728Smckusickalready has one, or when trying to send a datagram with the destination 31120728Smckusickaddress specified and the socket is already connected; 31247675Scael.It Bq Er ENOTCONN 31320728Smckusickwhen trying to send a datagram, but 31420728Smckusickno destination address is specified, and the socket hasn't been 31520728Smckusickconnected; 31647675Scael.It Bq Er ENOBUFS 31720728Smckusickwhen the system runs out of memory for 31820728Smckusickan internal data structure; 31947675Scael.It Bq Er EADDRNOTAVAIL 32020728Smckusickwhen an attempt is made to create a 32120728Smckusicksocket with a network address for which no network interface 32220728Smckusickexists. 323*60006Sandrew.It Bq Er EACESS 324*60006Sandrewwhen an attempt is made to create 325*60006Sandrewa raw IP socket by a non-privileged process. 32647675Scael.El 32747675Scael.Pp 32847675ScaelThe following errors specific to 32947675Scael.Tn IP 33047675Scaelmay occur when setting or getting 33147675Scael.Tn IP 33247675Scaeloptions: 33347675Scael.Bl -tag -width EADDRNOTAVAILxx 33447675Scael.It Bq Er EINVAL 33528252SkarelsAn unknown socket option name was given. 33647675Scael.It Bq Er EINVAL 33728252SkarelsThe IP option field was improperly formed; 33828252Skarelsan option field was shorter than the minimum value 33928252Skarelsor longer than the option buffer provided. 34047675Scael.El 34147675Scael.Sh SEE ALSO 34247675Scael.Xr getsockopt 2 , 34347675Scael.Xr send 2 , 34447675Scael.Xr recv 2 , 34547675Scael.Xr intro 4 , 34647675Scael.Xr icmp 4 , 34747675Scael.Xr inet 4 34847675Scael.Sh HISTORY 34947675ScaelThe 35047675Scael.Nm 35147675Scaelprotocol appeared in 35247675Scael.Bx 4.2 . 353