]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/ip.7
Many pages: Fix style issues reported by `make lint-groff`
[thirdparty/man-pages.git] / man7 / ip.7
CommitLineData
77117f4f 1.\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2297bf0e 2.\"
00acdba1 3.\" %%%LICENSE_START(VERBATIM_ONE_PARA)
77117f4f
MK
4.\" Permission is granted to distribute possibly modified copies
5.\" of this page provided the header is included verbatim,
6.\" and in case of nontrivial modification author and date
7.\" of the modification is added to the header.
8ff7380d 8.\" %%%LICENSE_END
6a717e5e 9.\"
77117f4f 10.\" $Id: ip.7,v 1.19 2000/12/20 18:10:31 ak Exp $
3ac83a00 11.\"
bea08fec 12.\" FIXME The following socket options are yet to be documented
2c596bf5 13.\"
f645fea8
MK
14.\" IP_XFRM_POLICY (2.5.48)
15.\" Needs CAP_NET_ADMIN
2c596bf5 16.\"
f645fea8
MK
17.\" IP_IPSEC_POLICY (2.5.47)
18.\" Needs CAP_NET_ADMIN
2c596bf5 19.\"
f645fea8
MK
20.\" IP_MINTTL (2.6.34)
21.\" commit d218d11133d888f9745802146a50255a4781d37a
22.\" Author: Stephen Hemminger <shemminger@vyatta.com>
2c596bf5 23.\"
f645fea8 24.\" MCAST_JOIN_GROUP (2.4.22 / 2.6)
2c596bf5 25.\"
f645fea8 26.\" MCAST_BLOCK_SOURCE (2.4.22 / 2.6)
2c596bf5 27.\"
f645fea8 28.\" MCAST_UNBLOCK_SOURCE (2.4.22 / 2.6)
2c596bf5 29.\"
f645fea8 30.\" MCAST_LEAVE_GROUP (2.4.22 / 2.6)
2c596bf5 31.\"
f645fea8 32.\" MCAST_JOIN_SOURCE_GROUP (2.4.22 / 2.6)
2c596bf5 33.\"
f645fea8 34.\" MCAST_LEAVE_SOURCE_GROUP (2.4.22 / 2.6)
2c596bf5 35.\"
f645fea8 36.\" MCAST_MSFILTER (2.4.22 / 2.6)
2c596bf5 37.\"
f645fea8
MK
38.\" IP_UNICAST_IF (3.4)
39.\" commit 76e21053b5bf33a07c76f99d27a74238310e3c71
40.\" Author: Erich E. Hoover <ehoover@mines.edu>
3ac83a00 41.\"
1d767b55 42.TH IP 7 2021-03-22 "Linux" "Linux Programmer's Manual"
77117f4f
MK
43.SH NAME
44ip \- Linux IPv4 protocol implementation
45.SH SYNOPSIS
c7db92b9 46.nf
77117f4f 47.B #include <sys/socket.h>
77117f4f
MK
48.\" .B #include <net/netinet.h> -- does not exist anymore
49.\" .B #include <linux/errqueue.h> -- never include <linux/foo.h>
50.B #include <netinet/in.h>
77117f4f 51.B #include <netinet/ip.h> \fR/* superset of previous */
68e4db0a 52.PP
d4c8c97c 53.IB tcp_socket " = socket(AF_INET, SOCK_STREAM, 0);"
d4c8c97c 54.IB udp_socket " = socket(AF_INET, SOCK_DGRAM, 0);"
d4c8c97c 55.IB raw_socket " = socket(AF_INET, SOCK_RAW, " protocol ");"
c7db92b9 56.fi
77117f4f
MK
57.SH DESCRIPTION
58Linux implements the Internet Protocol, version 4,
59described in RFC\ 791 and RFC\ 1122.
60.B ip
119a1824 61contains a level 2 multicasting implementation conforming to RFC\ 1112.
77117f4f 62It also contains an IP router including a packet filter.
77117f4f 63.PP
02f63637 64The programming interface is BSD-sockets compatible.
77117f4f
MK
65For more information on sockets, see
66.BR socket (7).
67.PP
38fdd3bb
MK
68An IP socket is created using
69.BR socket (2):
5711c04f 70.PP
1ae6b2c7
AC
71.in +4n
72.EX
73socket(AF_INET, socket_type, protocol);
74.EE
75.in
5711c04f 76.PP
d8833593 77Valid socket types include
77117f4f 78.B SOCK_STREAM
d8833593 79to open a stream socket,
77117f4f 80.B SOCK_DGRAM
d8833593 81to open a datagram socket, and
77117f4f
MK
82.B SOCK_RAW
83to open a
84.BR raw (7)
85socket to access the IP protocol directly.
d8833593 86.PP
77117f4f
MK
87.I protocol
88is the IP protocol in the IP header to be received or sent.
d8833593 89Valid values for
77117f4f 90.I protocol
d8833593
MK
91include:
92.IP \(bu 2
930 and
77117f4f 94.B IPPROTO_TCP
d8833593
MK
95for
96.BR tcp (7)
97stream sockets;
98.IP \(bu
990 and
77117f4f 100.B IPPROTO_UDP
d8833593
MK
101for
102.BR udp (7)
103datagram sockets;
104.IP \(bu
105.B IPPROTO_SCTP
106for
107.BR sctp (7)
108stream sockets; and
109.IP \(bu
110.B IPPROTO_UDPLITE
111for
112.BR udplite (7)
113datagram sockets.
114.PP
77117f4f
MK
115For
116.B SOCK_RAW
119a1824
MK
117you may specify a valid IANA IP protocol defined in
118RFC\ 1700 assigned numbers.
77117f4f 119.PP
77117f4f
MK
120When a process wants to receive new incoming packets or connections, it
121should bind a socket to a local interface address using
122.BR bind (2).
0a03fceb
FL
123In this case, only one IP socket may be bound to any given local
124(address, port) pair.
77117f4f
MK
125When
126.B INADDR_ANY
f971627f 127is specified in the bind call, the socket will be bound to
77117f4f
MK
128.I all
129local interfaces.
130When
131.BR listen (2)
0a03fceb
FL
132is called on an unbound socket, the socket is automatically bound
133to a random free port with the local address set to
134.BR INADDR_ANY .
135When
77117f4f 136.BR connect (2)
0a03fceb 137is called on an unbound socket, the socket is automatically bound
c534b25d 138to a random free port or to a usable shared port with the local address
0a03fceb 139set to
77117f4f 140.BR INADDR_ANY .
5711c04f 141.PP
77117f4f 142A TCP local socket address that has been bound is unavailable for
119a1824 143some time after closing, unless the
77117f4f
MK
144.B SO_REUSEADDR
145flag has been set.
119a1824 146Care should be taken when using this flag as it makes TCP less reliable.
c634028a 147.SS Address format
77117f4f
MK
148An IP socket address is defined as a combination of an IP interface
149address and a 16-bit port number.
150The basic IP protocol does not supply port numbers, they
151are implemented by higher level protocols like
152.BR udp (7)
153and
154.BR tcp (7).
155On raw sockets
156.I sin_port
157is set to the IP protocol.
158.PP
159.in +4n
b8302363 160.EX
77117f4f
MK
161struct sockaddr_in {
162 sa_family_t sin_family; /* address family: AF_INET */
4fa50d72 163 in_port_t sin_port; /* port in network byte order */
77117f4f
MK
164 struct in_addr sin_addr; /* internet address */
165};
166
c2e81ff9 167/* Internet address */
77117f4f
MK
168struct in_addr {
169 uint32_t s_addr; /* address in network byte order */
170};
b8302363 171.EE
77117f4f
MK
172.in
173.PP
174.I sin_family
175is always set to
176.BR AF_INET .
177This is required; in Linux 2.2 most networking functions return
178.B EINVAL
179when this setting is missing.
180.I sin_port
181contains the port in network byte order.
182The port numbers below 1024 are called
1ae6b2c7 183.I privileged ports
6eb334b2
MK
184(or sometimes:
185.IR "reserved ports" ).
ed1ba8a5
MK
186Only a privileged process
187(on Linux: a process that has the
77117f4f 188.B CAP_NET_BIND_SERVICE
ed1ba8a5 189capability in the user namespace governing its network namespace) may
77117f4f
MK
190.BR bind (2)
191to these sockets.
192Note that the raw IPv4 protocol as such has no concept of a
33a0ccb2 193port, they are implemented only by higher protocols like
77117f4f
MK
194.BR tcp (7)
195and
196.BR udp (7).
197.PP
198.I sin_addr
199is the IP host address.
200The
201.I s_addr
202member of
203.I struct in_addr
204contains the host interface address in network byte order.
205.I in_addr
e89c8911 206should be assigned one of the
1ae6b2c7 207.B INADDR_*
c0a0e532
RBP
208values
209(e.g.,
5d0ea688 210.BR INADDR_LOOPBACK )
c0a0e532
RBP
211using
212.BR htonl (3)
77117f4f
MK
213or set using the
214.BR inet_aton (3),
215.BR inet_addr (3),
216.BR inet_makeaddr (3)
217library functions or directly with the name resolver (see
218.BR gethostbyname (3)).
5711c04f 219.PP
173fa792 220IPv4 addresses are divided into unicast, broadcast,
77117f4f
MK
221and multicast addresses.
222Unicast addresses specify a single interface of a host,
173fa792 223broadcast addresses specify all hosts on a network, and multicast
77117f4f 224addresses address all hosts in a multicast group.
33a0ccb2 225Datagrams to broadcast addresses can be sent or received only when the
77117f4f
MK
226.B SO_BROADCAST
227socket flag is set.
33a0ccb2
MK
228In the current implementation, connection-oriented sockets are allowed
229to use only unicast addresses.
77117f4f 230.\" Leave a loophole for XTP @)
5711c04f 231.PP
77117f4f
MK
232Note that the address and the port are always stored in
233network byte order.
234In particular, this means that you need to call
235.BR htons (3)
236on the number that is assigned to a port.
237All address/port manipulation
238functions in the standard library work in network byte order.
5711c04f 239.PP
77117f4f
MK
240There are several special addresses:
241.B INADDR_LOOPBACK
242(127.0.0.1)
243always refers to the local host via the loopback device;
244.B INADDR_ANY
245(0.0.0.0)
246means any address for binding;
247.B INADDR_BROADCAST
248(255.255.255.255)
249means any host and has the same effect on bind as
250.B INADDR_ANY
251for historical reasons.
c634028a 252.SS Socket options
77117f4f
MK
253IP supports some protocol-specific socket options that can be set with
254.BR setsockopt (2)
255and read with
256.BR getsockopt (2).
257The socket option level for IP is
258.BR IPPROTO_IP .
259.\" or SOL_IP on Linux
260A boolean integer flag is zero when it is false, otherwise true.
5711c04f 261.PP
651b8a82
MK
262When an invalid socket option is specified,
263.BR getsockopt (2)
264and
265.BR setsockopt (2)
266fail with the error
267.BR ENOPROTOOPT .
77117f4f 268.TP