aboutsummaryrefslogtreecommitdiffstats
path: root/man/man7/ip.7
blob: 60081cbea041b70920c2d033fdb4b3706e247772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: Linux-man-pages-1-para
.\"
.TH ip 7 (date) "Linux man-pages (unreleased)"
.SH NAME
ip \- Linux IPv4 protocol implementation
.SH SYNOPSIS
.nf
.B #include <sys/socket.h>
.\" .B #include <net/netinet.h> -- does not exist anymore
.\" .B #include <linux/errqueue.h> -- never include <linux/foo.h>
.B #include <netinet/in.h>
.BR "#include <netinet/ip.h>" "  /* superset of previous */"
.P
.IB tcp_socket " = socket(AF_INET, SOCK_STREAM, 0);"
.IB udp_socket " = socket(AF_INET, SOCK_DGRAM, 0);"
.IB raw_socket " = socket(AF_INET, SOCK_RAW, " protocol ");"
.fi
.SH DESCRIPTION
Linux implements the Internet Protocol, version 4,
described in RFC\ 791 and RFC\ 1122.
.B ip
contains a level 2 multicasting implementation conforming to RFC\ 1112.
It also contains an IP router including a packet filter.
.P
The programming interface is BSD-sockets compatible.
For more information on sockets, see
.BR socket (7).
.P
An IP socket is created using
.BR socket (2):
.P
.in +4n
.EX
socket(AF_INET, socket_type, protocol);
.EE
.in
.P
Valid socket types include
.B SOCK_STREAM
to open a stream socket,
.B SOCK_DGRAM
to open a datagram socket, and
.B SOCK_RAW
to open a
.BR raw (7)
socket to access the IP protocol directly.
.P
.I protocol
is the IP protocol in the IP header to be received or sent.
Valid values for
.I protocol
include:
.IP \[bu] 3
0 and
.B IPPROTO_TCP
for
.BR tcp (7)
stream sockets;
.IP \[bu]
0 and
.B IPPROTO_UDP
for
.BR udp (7)
datagram sockets;
.IP \[bu]
.B IPPROTO_SCTP
for
.BR sctp (7)
stream sockets;
and
.IP \[bu]
.B IPPROTO_UDPLITE
for
.BR udplite (7)
datagram sockets.
.P
For
.B SOCK_RAW
you may specify a valid IANA IP protocol defined in
RFC\ 1700 assigned numbers.
.P
When a process wants to receive new incoming packets or connections, it
should bind a socket to a local interface address using
.BR bind (2).
In this case, only one IP socket may be bound to any given local
(address, port) pair.
When
.B INADDR_ANY
is specified in the bind call, the socket will be bound to
.I all
local interfaces.
When
.BR listen (2)
is called on an unbound socket, the socket is automatically bound
to a random free port with the local address set to
.BR INADDR_ANY .
When
.BR connect (2)
is called on an unbound socket, the socket is automatically bound
to a random free port or to a usable shared port with the local address
set to
.BR INADDR_ANY .
.P
A TCP local socket address that has been bound is unavailable for
some time after closing, unless the
.B SO_REUSEADDR
flag has been set.
Care should be taken when using this flag as it makes TCP less reliable.
.SS Address format
An IP socket address is defined as a combination of an IP interface
address and a 16-bit port number.
The basic IP protocol does not supply port numbers, they
are implemented by higher level protocols like
.BR udp (7)
and
.BR tcp (7).
On raw sockets
.I .sin_port
is set to the IP protocol.
.P
See
.BR sockaddr_in (3type).
.P
.I .sin_family
is always set to
.BR AF_INET .
This is required; in Linux 2.2 most networking functions return
.B EINVAL
when this setting is missing.
.I .sin_port
contains the port in network byte order.
The port numbers below 1024 are called
.I privileged ports
(or sometimes:
.IR "reserved ports" ).
Only a privileged process
(on Linux: a process that has the
.B CAP_NET_BIND_SERVICE
capability in the user namespace governing its network namespace) may
.BR bind (2)
to these sockets.
Note that the raw IPv4 protocol as such has no concept of a
port, they are implemented only by higher protocols like
.BR tcp (7)
and
.BR udp (7).
.P
.I .sin_addr
is the IP host address.
The
.I .s_addr
member of the
.BR in_addr (3type)
structure
contains the host interface address in network byte order.
.BR in_addr (3type)
should be assigned one of the
.B INADDR_*
values
(e.g.,
.BR INADDR_LOOPBACK )
using
.BR htonl (3)
or set using the
.BR inet_aton (3),
.BR inet_addr (3),
.BR inet_makeaddr (3)
library functions or directly with the name resolver (see
.BR gethostbyname (3)).
.P
IPv4 addresses are divided into unicast, broadcast,
and multicast addresses.
Unicast addresses specify a single interface of a host,
broadcast addresses specify all hosts on a network, and multicast
addresses address all hosts in a multicast group.
Datagrams to broadcast addresses can be sent or received only when the
.B SO_BROADCAST
socket flag is set.
In the current implementation, connection-oriented sockets are allowed
to use only unicast addresses.
.\" Leave a loophole for XTP @)
.P
Note that the address and the port are always stored in
network byte order.
In particular, this means that you need to call
.BR htons (3)
on the number that is assigned to a port.
All address/port manipulation
functions in the standard library work in network byte order.
.SS Special and reserved addresses
There are several special addresses:
.TP
.BR INADDR_LOOPBACK " (127.0.0.1)"
always refers to the local host via the loopback device;
.TP
.BR INADDR_ANY " (0.0.0.0)"
means any address for socket binding;
.TP
.BR INADDR_BROADCAST " (255.255.255.255)"
has the same effect on
.BR bind (2)
as
.B INADDR_ANY
for historical reasons.
A packet addressed to
.B INADDR_BROADCAST
through a socket which has
.B SO_BROADCAST
set will be broadcast to all hosts on the local network segment,
as long as the link is broadcast-capable.
.TP
Highest-numbered address
.TQ
Lowest-numbered address
On any locally-attached non-point-to-point IP subnet
with a link type that supports broadcasts,
the highest-numbered address
(e.g., the .255 address on a subnet with netmask 255.255.255.0)
is designated as a broadcast address.
It cannot usefully be assigned to an individual interface,
and can only be addressed with a socket on which the
.B SO_BROADCAST
option has been set.
Internet standards have historically
also reserved the lowest-numbered address
(e.g., the .0 address on a subnet with netmask 255.255.255.0)
for broadcast, though they call it "obsolete" for this purpose.
(Some sources also refer to this as the "network address.")
Since Linux 5.14,
.\" commit 58fee5fc83658aaacf60246aeab738946a9ba516
it is treated as an ordinary unicast address
and can be assigned to an interface.
.P
Internet standards have traditionally also reserved various addresses
for particular uses, though Linux no longer treats
some of these specially.
.TP
[0.0.0.1, 0.255.255.255]
.TQ
[240.0.0.0, 255.255.255.254]
Addresses in these ranges (0/8 and 240/4) are reserved globally.
Since Linux 5.3
.\" commit 96125bf9985a75db00496dd2bc9249b777d2b19b
and Linux 2.6.25,
.\" commit 1e637c74b0f84eaca02b914c0b8c6f67276e9697
respectively,
the 0/8 and 240/4 addresses, other than
.B INADDR_ANY
and
.BR INADDR_BROADCAST ,
are treated as ordinary unicast addresses.
Systems that follow the traditional behaviors may not
interoperate with these historically reserved addresses.
.TP
[127.0.0.1, 127.255.255.254]
Addresses in this range (127/8) are treated as loopback addresses
akin to the standardized local loopback address
.B INADDR_LOOPBACK
(127.0.0.1);
.TP
[224.0.0.0, 239.255.255.255]
Addresses in this range (224/4) are dedicated to multicast use.
.SS Socket options
See
.BR IPPROTO_IP (2const).
.SS /proc interfaces
See
.BR proc_sys_net_ipv4 (5).
.SS Ioctls
All ioctls described in
.BR socket (7)
apply to
.BR ip .
.P
Ioctls to configure generic device parameters are described in
.BR netdevice (7).
.\" FIXME Add a discussion of multicasting
.SH ERRORS
.\" FIXME document all errors.
.\"     We should really fix the kernels to give more uniform
.\"     error returns (ENOMEM vs ENOBUFS, EPERM vs EACCES etc.)
.TP
.B EACCES
The user tried to execute an operation without the necessary permissions.
These include:
sending a packet to a broadcast address without having the
.B SO_BROADCAST
flag set;
sending a packet via a
.I prohibit
route;
modifying firewall settings without superuser privileges (the
.B CAP_NET_ADMIN
capability);
binding to a privileged port without superuser privileges (the
.B CAP_NET_BIND_SERVICE
capability).
.TP
.B EADDRINUSE
Tried to bind to an address already in use.
.TP
.B EADDRNOTAVAIL
A nonexistent interface was requested or the requested source
address was not local.
.TP
.B EAGAIN
Operation on a nonblocking socket would block.
.TP
.B EALREADY
A connection operation on a nonblocking socket is already in progress.
.TP
.B ECONNABORTED
A connection was closed during an
.BR accept (2).
.TP
.B EHOSTUNREACH
No valid routing table entry matches the destination address.
This error can be caused by an ICMP message from a remote router or
for the local routing table.
.TP
.B EINVAL
Invalid argument passed.
For send operations this can be caused by sending to a
.I blackhole
route.
.TP
.B EISCONN
.BR connect (2)
was called on an already connected socket.
.TP
.B EMSGSIZE
Datagram is bigger than an MTU on the path and it cannot be fragmented.
.TP
.B ENOBUFS
.TQ
.B ENOMEM
Not enough free memory.
This often means that the memory allocation is limited by the socket
buffer limits, not by the system memory, but this is not 100% consistent.
.TP
.B ENOENT
.B SIOCGSTAMP
was called on a socket where no packet arrived.
.TP
.B ENOPKG
A kernel subsystem was not configured.
.TP
.B ENOPROTOOPT
.TQ
.B EOPNOTSUPP
Invalid socket option passed.
.TP
.B ENOTCONN
The operation is defined only on a connected socket, but the socket wasn't
connected.
.TP
.B EPERM
User doesn't have permission to set high priority, change configuration,
or send signals to the requested process or group.
.TP
.B EPIPE
The connection was unexpectedly closed or shut down by the other end.
.TP
.B ESOCKTNOSUPPORT
The socket is not configured or an unknown socket type was requested.
.P
Other errors may be generated by the overlaying protocols;
see
.BR tcp (7),
.BR raw (7),
.BR udp (7),
and
.BR socket (7).
.SH NOTES
Be very careful with the
.B SO_BROADCAST
option \- it is not privileged in Linux.
It is easy to overload the network
with careless broadcasts.
For new application protocols
it is better to use a multicast group instead of broadcasting.
Broadcasting is discouraged.
See RFC 6762 for an example of a protocol (mDNS)
using the more modern multicast approach
to communicating with an open-ended
group of hosts on the local network.
.P
Using the
.B SOL_IP
socket options level isn't portable;
BSD-based stacks use the
.B IPPROTO_IP
level.
.P
.B INADDR_ANY
(0.0.0.0) and
.B INADDR_BROADCAST
(255.255.255.255) are byte-order-neutral.
This means
.BR htonl (3)
has no effect on them.
.SS Compatibility
For compatibility with Linux 2.0, the obsolete
.BI "socket(AF_INET, SOCK_PACKET, " protocol )
syntax is still supported to open a
.BR packet (7)
socket.
This is deprecated and should be replaced by
.BI "socket(AF_PACKET, SOCK_RAW, " protocol )
instead.
The main difference is the new
.I sockaddr_ll
address structure for generic link layer information instead of the old
.BR sockaddr_pkt .
.SH BUGS
There are too many inconsistent error values.
.P
The error used to diagnose exhaustion of the ephemeral port range differs
across the various system calls
.RB ( connect (2),
.BR bind (2),
.BR listen (2),
.BR sendto (2))
that can assign ephemeral ports.
.P
The ioctls to configure IP-specific interface options and ARP tables are
not described.
.\" .P
.\" Some versions of glibc forget to declare
.\" .IR in_pktinfo .
.\" Workaround currently is to copy it into your program from this man page.
.P
Receiving the original destination address with
.B MSG_ERRQUEUE
in
.I msg_name
by
.BR recvmsg (2)
does not work in some Linux 2.2 kernels.
.\" .SH AUTHORS
.\" This man page was written by Andi Kleen.
.SH SEE ALSO
.BR IPPROTO_IP (2const),
.BR recvmsg (2),
.BR sendmsg (2),
.BR byteorder (3),
.BR capabilities (7),
.BR icmp (7),
.BR ipv6 (7),
.BR netdevice (7),
.BR netlink (7),
.BR raw (7),
.BR socket (7),
.BR tcp (7),
.BR udp (7),
.BR ip (8)
.P
The kernel source file
.IR Documentation/networking/ip\-sysctl.txt .
.P
RFC\ 791 for the original IP specification.
RFC\ 1122 for the IPv4 host requirements.
RFC\ 1812 for the IPv4 router requirements.