161598Sbostic.\" Copyright (c) 1983, 1991, 1993 261598Sbostic.\" The Regents of the University of California. All rights reserved. 320728Smckusick.\" 443581Strent.\" %sccs.include.redist.man% 520728Smckusick.\" 6*64994Smckusick.\" @(#)ip.4 8.2 (Berkeley) 11/30/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 3660006Sandrewspecial-purpose applications. 3747675Scael.Pp 3860006SandrewThere are several 39*64994Smckusick.Tn IP-level 4060006Sandrew.Xr setsockopt 2 / Ns 4160006Sandrew.Xr getsockopt 2 4260006Sandrewoptions. 4360006Sandrew.Dv IP_OPTIONS 4460006Sandrewmay be used to provide 4547675Scael.Tn IP 4647675Scaeloptions to be transmitted in the 4747675Scael.Tn IP 4860006Sandrewheader of each outgoing packet 4960006Sandrewor to examine the header options on incoming packets. 5060006Sandrew.Tn IP 5160006Sandrewoptions 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 5560006Sandrew.Tn IP 5660006Sandrewprotocol 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. 6160006SandrewTo disable previously specified options, 6260006Sandrewuse a zero-length buffer: 6360006Sandrew.Bd -literal 6460006Sandrewsetsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0); 6560006Sandrew.Ed 6647675Scael.Pp 6760006Sandrew.Dv IP_TOS 6860006Sandrewand 6960006Sandrew.Dv IP_TTL 7060006Sandrewmay be used to set the type-of-service and time-to-live 7160006Sandrewfields in the 7260006Sandrew.Tn IP 7360006Sandrewheader for 7460006Sandrew.Dv SOCK_STREAM 7560006Sandrewand 7660006Sandrew.Dv SOCK_DGRAM 7760006Sandrewsockets. For example, 7860006Sandrew.Bd -literal 7960006Sandrewint tos = IPTOS_LOWDELAY; /* see <netinet/in.h> */ 8060006Sandrewsetsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); 8160006Sandrew 8260006Sandrewint ttl = 60; /* max = 255 */ 8360006Sandrewsetsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); 8460006Sandrew.Ed 8560006Sandrew.Pp 8660006SandrewIf the 8760006Sandrew.Dv IP_RECVDSTADDR 8860006Sandrewoption is enabled on a 8960006Sandrew.Dv SOCK_DGRAM 9060006Sandrewsocket, 9160006Sandrewthe 9260006Sandrew.Xr recvmsg 9360006Sandrewcall will return the destination 9460006Sandrew.Tn IP 9560006Sandrewaddress for a 9660006Sandrew.Tn UDP 9760006Sandrewdatagram. 9860006SandrewThe msg_control field in the msghdr structure points to a buffer 9960006Sandrewthat contains a cmsghdr structure followed by the 10060006Sandrew.Tn IP 10160006Sandrewaddress. 10260006SandrewThe cmsghdr fields have the following values: 10360006Sandrew.Bd -literal 10460006Sandrewcmsg_len = sizeof(struct in_addr) 10560006Sandrewcmsg_level = IPPROTO_IP 10660006Sandrewcmsg_type = IP_RECVDSTADDR 10760006Sandrew.Ed 10860006Sandrew.Ss "Multicast Options" 10960006Sandrew.Pp 11060006Sandrew.Tn IP 11160006Sandrewmulticasting is supported only on 11260006Sandrew.Dv AF_INET 11360006Sandrewsockets of type 11460006Sandrew.Dv SOCK_DGRAM 11560006Sandrewand 11660006Sandrew.Dv SOCK_RAW, 11760006Sandrewand only on networks where the interface 11860006Sandrewdriver supports multicasting. 11960006Sandrew.Pp 12060006SandrewThe 12160006Sandrew.Dv IP_MULTICAST_TTL 12260006Sandrewoption changes the time-to-live (TTL) 12360006Sandrewfor outgoing multicast datagrams 12460006Sandrewin order to control the scope of the multicasts: 12560006Sandrew.Bd -literal 12660006Sandrewu_char ttl; /* range: 0 to 255, default = 1 */ 12760006Sandrewsetsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); 12860006Sandrew.Ed 12960006Sandrew.sp 13060006SandrewDatagrams with a TTL of 1 are not forwarded beyond the local network. 13160006SandrewMulticast datagrams with a TTL of 0 will not be transmitted on any network, 13260006Sandrewbut may be delivered locally if the sending host belongs to the destination 13360006Sandrewgroup and if multicast loopback has not been disabled on the sending socket 13460006Sandrew(see below). Multicast datagrams with TTL greater than 1 may be forwarded 13560006Sandrewto other networks if a multicast router is attached to the local network. 13660006Sandrew.Pp 13760006SandrewFor hosts with multiple interfaces, each multicast transmission is 13860006Sandrewsent from the primary network interface. 13960006SandrewThe 14060006Sandrew.Dv IP_MULTICAST_IF 14160006Sandrewoption overrides the default for 14260006Sandrewsubsequent transmissions from a given socket: 14360006Sandrew.Bd -literal 14460006Sandrewstruct in_addr addr; 14560006Sandrewsetsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr)); 14660006Sandrew.Ed 14760006Sandrew.sp 14860006Sandrewwhere "addr" is the local 14960006Sandrew.Tn IP 15060006Sandrewaddress of the desired interface or 15160006Sandrew.Dv INADDR_ANY 15260006Sandrewto specify the default interface. 15360006SandrewAn interface's local IP address and multicast capability can 15460006Sandrewbe obtained via the 15560006Sandrew.Dv SIOCGIFCONF 15660006Sandrewand 15760006Sandrew.Dv SIOCGIFFLAGS 15860006Sandrewioctls. 15960006SandrewNormal applications should not need to use this option. 16060006Sandrew.Pp 16160006SandrewIf a multicast datagram is sent to a group to which the sending host itself 16260006Sandrewbelongs (on the outgoing interface), a copy of the datagram is, by default, 16360006Sandrewlooped back by the IP layer for local delivery. 16460006SandrewThe 16560006Sandrew.Dv IP_MULTICAST_LOOP 16660006Sandrewoption gives the sender explicit control 16760006Sandrewover whether or not subsequent datagrams are looped back: 16860006Sandrew.Bd -literal 16960006Sandrewu_char loop; /* 0 = disable, 1 = enable (default) */ 17060006Sandrewsetsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); 17160006Sandrew.Ed 17260006Sandrew.sp 17360006SandrewThis option 17460006Sandrewimproves performance for applications that may have no more than one 17560006Sandrewinstance on a single host (such as a router demon), by eliminating 17660006Sandrewthe overhead of receiving their own transmissions. It should generally not 17760006Sandrewbe used by applications for which there may be more than one instance on a 17860006Sandrewsingle host (such as a conferencing program) or for which the sender does 17960006Sandrewnot belong to the destination group (such as a time querying program). 18060006Sandrew.Pp 18160006SandrewA multicast datagram sent with an initial TTL greater than 1 may be delivered 18260006Sandrewto the sending host on a different interface from that on which it was sent, 18360006Sandrewif the host belongs to the destination group on that other interface. The 18460006Sandrewloopback control option has no effect on such delivery. 18560006Sandrew.Pp 18660006SandrewA host must become a member of a multicast group before it can receive 18760006Sandrewdatagrams sent to the group. To join a multicast group, use the 18860006Sandrew.Dv IP_ADD_MEMBERSHIP 18960006Sandrewoption: 19060006Sandrew.Bd -literal 19160006Sandrewstruct ip_mreq mreq; 19260006Sandrewsetsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); 19360006Sandrew.Ed 19460006Sandrew.sp 19560006Sandrewwhere 19660006Sandrew.Fa mreq 19760006Sandrewis the following structure: 19860006Sandrew.Bd -literal 19960006Sandrewstruct ip_mreq { 20060006Sandrew struct in_addr imr_multiaddr; /* multicast group to join */ 20160006Sandrew struct in_addr imr_interface; /* interface to join on */ 20260006Sandrew} 20360006Sandrew.Ed 20460006Sandrew.sp 20560006Sandrew.Dv imr_interface 20660006Sandrewshould 20760006Sandrewbe 20860006Sandrew.Dv INADDR_ANY 20960006Sandrewto choose the default multicast interface, 21060006Sandrewor the 21160006Sandrew.Tn IP 21260006Sandrewaddress of a particular multicast-capable interface if 21360006Sandrewthe host is multihomed. 21460006SandrewMembership is associated with a single interface; 215*64994Smckusickprograms running on multihomed hosts may need to 21660006Sandrewjoin the same group on more than one interface. 21760006SandrewUp to 21860006Sandrew.Dv IP_MAX_MEMBERSHIPS 21960006Sandrew(currently 20) memberships may be added on a 22060006Sandrewsingle socket. 22160006Sandrew.Pp 22260006SandrewTo drop a membership, use: 22360006Sandrew.Bd -literal 22460006Sandrewstruct ip_mreq mreq; 22560006Sandrewsetsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); 22660006Sandrew.Ed 22760006Sandrew.sp 22860006Sandrewwhere 22960006Sandrew.Fa mreq 23060006Sandrewcontains the same values as used to add the membership. 23160006SandrewMemberships are dropped when the socket is closed or the process exits. 23260006Sandrew.\"----------------------- 23360006Sandrew.Ss "Raw IP Sockets" 23460006Sandrew.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 27160006Sandrewnumber the socket is created with), 27260006Sandrewunless the 27360006Sandrew.Dv IP_HDRINCL 27460006Sandrewoption has been set. 27547675ScaelIncoming packets are received with 27647675Scael.Tn IP 27747675Scaelheader and options intact. 27860006Sandrew.Pp 27960006Sandrew.Dv IP_HDRINCL 28060006Sandrewindicates the complete IP header is included with the data 28160006Sandrewand may be used only with the 28260006Sandrew.Dv SOCK_RAW 28360006Sandrewtype. 28460006Sandrew.Bd -literal 28560006Sandrew#include <netinet/ip.h> 28660006Sandrew 28760006Sandrewint hincl = 1; /* 1 = on, 0 = off */ 28860006Sandrewsetsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)); 28960006Sandrew.Ed 29060006Sandrew.sp 29160006SandrewUnlike previous 29260006Sandrew.Tn BSD 29360006Sandrewreleases, the program must set all 29460006Sandrewthe fields of the IP header, including the following: 29560006Sandrew.Bd -literal 29660006Sandrewip->ip_v = IPVERSION; 29760006Sandrewip->ip_hl = hlen >> 2; 29860006Sandrewip->ip_id = 0; /* 0 means kernel set appropriate value */ 29960006Sandrewip->ip_off = offset; 30060006Sandrew.Ed 30160006Sandrew.sp .5 30260006SandrewIf the header source address is set to 30360006Sandrew.Dv INADDR_ANY, 30460006Sandrewthe 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. 32360006Sandrew.It Bq Er EACESS 32460006Sandrewwhen an attempt is made to create 32560006Sandrewa 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