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