]>
Commit | Line | Data |
---|---|---|
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 |
44 | ip \- 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 |
58 | Linux implements the Internet Protocol, version 4, | |
59 | described in RFC\ 791 and RFC\ 1122. | |
60 | .B ip | |
119a1824 | 61 | contains a level 2 multicasting implementation conforming to RFC\ 1112. |
77117f4f | 62 | It also contains an IP router including a packet filter. |
77117f4f | 63 | .PP |
02f63637 | 64 | The programming interface is BSD-sockets compatible. |
77117f4f MK |
65 | For more information on sockets, see |
66 | .BR socket (7). | |
67 | .PP | |
38fdd3bb MK |
68 | An IP socket is created using |
69 | .BR socket (2): | |
5711c04f | 70 | .PP |
1ae6b2c7 AC |
71 | .in +4n |
72 | .EX | |
73 | socket(AF_INET, socket_type, protocol); | |
74 | .EE | |
75 | .in | |
5711c04f | 76 | .PP |
d8833593 | 77 | Valid socket types include |
77117f4f | 78 | .B SOCK_STREAM |
d8833593 | 79 | to open a stream socket, |
77117f4f | 80 | .B SOCK_DGRAM |
d8833593 | 81 | to open a datagram socket, and |
77117f4f MK |
82 | .B SOCK_RAW |
83 | to open a | |
84 | .BR raw (7) | |
85 | socket to access the IP protocol directly. | |
d8833593 | 86 | .PP |
77117f4f MK |
87 | .I protocol |
88 | is the IP protocol in the IP header to be received or sent. | |
d8833593 | 89 | Valid values for |
77117f4f | 90 | .I protocol |
d8833593 MK |
91 | include: |
92 | .IP \(bu 2 | |
93 | 0 and | |
77117f4f | 94 | .B IPPROTO_TCP |
d8833593 MK |
95 | for |
96 | .BR tcp (7) | |
97 | stream sockets; | |
98 | .IP \(bu | |
99 | 0 and | |
77117f4f | 100 | .B IPPROTO_UDP |
d8833593 MK |
101 | for |
102 | .BR udp (7) | |
103 | datagram sockets; | |
104 | .IP \(bu | |
105 | .B IPPROTO_SCTP | |
106 | for | |
107 | .BR sctp (7) | |
108 | stream sockets; and | |
109 | .IP \(bu | |
110 | .B IPPROTO_UDPLITE | |
111 | for | |
112 | .BR udplite (7) | |
113 | datagram sockets. | |
114 | .PP | |
77117f4f MK |
115 | For |
116 | .B SOCK_RAW | |
119a1824 MK |
117 | you may specify a valid IANA IP protocol defined in |
118 | RFC\ 1700 assigned numbers. | |
77117f4f | 119 | .PP |
77117f4f MK |
120 | When a process wants to receive new incoming packets or connections, it |
121 | should bind a socket to a local interface address using | |
122 | .BR bind (2). | |
0a03fceb FL |
123 | In this case, only one IP socket may be bound to any given local |
124 | (address, port) pair. | |
77117f4f MK |
125 | When |
126 | .B INADDR_ANY | |
f971627f | 127 | is specified in the bind call, the socket will be bound to |
77117f4f MK |
128 | .I all |
129 | local interfaces. | |
130 | When | |
131 | .BR listen (2) | |
0a03fceb FL |
132 | is called on an unbound socket, the socket is automatically bound |
133 | to a random free port with the local address set to | |
134 | .BR INADDR_ANY . | |
135 | When | |
77117f4f | 136 | .BR connect (2) |
0a03fceb | 137 | is called on an unbound socket, the socket is automatically bound |
c534b25d | 138 | to a random free port or to a usable shared port with the local address |
0a03fceb | 139 | set to |
77117f4f | 140 | .BR INADDR_ANY . |
5711c04f | 141 | .PP |
77117f4f | 142 | A TCP local socket address that has been bound is unavailable for |
119a1824 | 143 | some time after closing, unless the |
77117f4f MK |
144 | .B SO_REUSEADDR |
145 | flag has been set. | |
119a1824 | 146 | Care should be taken when using this flag as it makes TCP less reliable. |
c634028a | 147 | .SS Address format |
77117f4f MK |
148 | An IP socket address is defined as a combination of an IP interface |
149 | address and a 16-bit port number. | |
150 | The basic IP protocol does not supply port numbers, they | |
151 | are implemented by higher level protocols like | |
152 | .BR udp (7) | |
153 | and | |
154 | .BR tcp (7). | |
155 | On raw sockets | |
156 | .I sin_port | |
157 | is set to the IP protocol. | |
158 | .PP | |
159 | .in +4n | |
b8302363 | 160 | .EX |
77117f4f MK |
161 | struct 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 |
168 | struct 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 | |
175 | is always set to | |
176 | .BR AF_INET . | |
177 | This is required; in Linux 2.2 most networking functions return | |
178 | .B EINVAL | |
179 | when this setting is missing. | |
180 | .I sin_port | |
181 | contains the port in network byte order. | |
182 | The port numbers below 1024 are called | |
1ae6b2c7 | 183 | .I privileged ports |
6eb334b2 MK |
184 | (or sometimes: |
185 | .IR "reserved ports" ). | |
ed1ba8a5 MK |
186 | Only a privileged process |
187 | (on Linux: a process that has the | |
77117f4f | 188 | .B CAP_NET_BIND_SERVICE |
ed1ba8a5 | 189 | capability in the user namespace governing its network namespace) may |
77117f4f MK |
190 | .BR bind (2) |
191 | to these sockets. | |
192 | Note that the raw IPv4 protocol as such has no concept of a | |
33a0ccb2 | 193 | port, they are implemented only by higher protocols like |
77117f4f MK |
194 | .BR tcp (7) |
195 | and | |
196 | .BR udp (7). | |
197 | .PP | |
198 | .I sin_addr | |
199 | is the IP host address. | |
200 | The | |
201 | .I s_addr | |
202 | member of | |
203 | .I struct in_addr | |
204 | contains the host interface address in network byte order. | |
205 | .I in_addr | |
e89c8911 | 206 | should be assigned one of the |
1ae6b2c7 | 207 | .B INADDR_* |
c0a0e532 RBP |
208 | values |
209 | (e.g., | |
5d0ea688 | 210 | .BR INADDR_LOOPBACK ) |
c0a0e532 RBP |
211 | using |
212 | .BR htonl (3) | |
77117f4f MK |
213 | or set using the |
214 | .BR inet_aton (3), | |
215 | .BR inet_addr (3), | |
216 | .BR inet_makeaddr (3) | |
217 | library functions or directly with the name resolver (see | |
218 | .BR gethostbyname (3)). | |
5711c04f | 219 | .PP |
173fa792 | 220 | IPv4 addresses are divided into unicast, broadcast, |
77117f4f MK |
221 | and multicast addresses. |
222 | Unicast addresses specify a single interface of a host, | |
173fa792 | 223 | broadcast addresses specify all hosts on a network, and multicast |
77117f4f | 224 | addresses address all hosts in a multicast group. |
33a0ccb2 | 225 | Datagrams to broadcast addresses can be sent or received only when the |
77117f4f MK |
226 | .B SO_BROADCAST |
227 | socket flag is set. | |
33a0ccb2 MK |
228 | In the current implementation, connection-oriented sockets are allowed |
229 | to use only unicast addresses. | |
77117f4f | 230 | .\" Leave a loophole for XTP @) |
5711c04f | 231 | .PP |
77117f4f MK |
232 | Note that the address and the port are always stored in |
233 | network byte order. | |
234 | In particular, this means that you need to call | |
235 | .BR htons (3) | |
236 | on the number that is assigned to a port. | |
237 | All address/port manipulation | |
238 | functions in the standard library work in network byte order. | |
5711c04f | 239 | .PP |
77117f4f MK |
240 | There are several special addresses: |
241 | .B INADDR_LOOPBACK | |
242 | (127.0.0.1) | |
243 | always refers to the local host via the loopback device; | |
244 | .B INADDR_ANY | |
245 | (0.0.0.0) | |
246 | means any address for binding; | |
247 | .B INADDR_BROADCAST | |
248 | (255.255.255.255) | |
249 | means any host and has the same effect on bind as | |
250 | .B INADDR_ANY | |
251 | for historical reasons. | |
c634028a | 252 | .SS Socket options |
77117f4f MK |
253 | IP supports some protocol-specific socket options that can be set with |
254 | .BR setsockopt (2) | |
255 | and read with | |
256 | .BR getsockopt (2). | |
257 | The socket option level for IP is | |
258 | .BR IPPROTO_IP . | |
259 | .\" or SOL_IP on Linux | |
260 | A boolean integer flag is zero when it is false, otherwise true. | |
5711c04f | 261 | .PP |
651b8a82 MK |
262 | When an invalid socket option is specified, |
263 | .BR getsockopt (2) | |
264 | and | |
265 | .BR setsockopt (2) | |
266 | fail with the error | |
267 | .BR ENOPROTOOPT . | |
77117f4f | 268 | .TP |