]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/ipv4/af_inet.c
be2net: FLR must be first cmd issued to Lancer FW
[mirror_ubuntu-hirsute-kernel.git] / net / ipv4 / af_inet.c
CommitLineData
1da177e4
LT
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * PF_INET protocol family socket handler.
7 *
02c30a84 8 * Authors: Ross Biro
1da177e4
LT
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Florian La Roche, <flla@stud.uni-sb.de>
11 * Alan Cox, <A.Cox@swansea.ac.uk>
12 *
13 * Changes (see also sock.c)
14 *
15 * piggy,
16 * Karl Knutson : Socket protocol table
17 * A.N.Kuznetsov : Socket death error in accept().
18 * John Richardson : Fix non blocking error in connect()
19 * so sockets that fail to connect
20 * don't return -EINPROGRESS.
21 * Alan Cox : Asynchronous I/O support
22 * Alan Cox : Keep correct socket pointer on sock
23 * structures
24 * when accept() ed
25 * Alan Cox : Semantics of SO_LINGER aren't state
26 * moved to close when you look carefully.
27 * With this fixed and the accept bug fixed
28 * some RPC stuff seems happier.
29 * Niibe Yutaka : 4.4BSD style write async I/O
30 * Alan Cox,
31 * Tony Gale : Fixed reuse semantics.
32 * Alan Cox : bind() shouldn't abort existing but dead
33 * sockets. Stops FTP netin:.. I hope.
34 * Alan Cox : bind() works correctly for RAW sockets.
35 * Note that FreeBSD at least was broken
36 * in this respect so be careful with
37 * compatibility tests...
38 * Alan Cox : routing cache support
39 * Alan Cox : memzero the socket structure for
40 * compactness.
41 * Matt Day : nonblock connect error handler
42 * Alan Cox : Allow large numbers of pending sockets
43 * (eg for big web sites), but only if
44 * specifically application requested.
45 * Alan Cox : New buffering throughout IP. Used
46 * dumbly.
47 * Alan Cox : New buffering now used smartly.
48 * Alan Cox : BSD rather than common sense
49 * interpretation of listen.
50 * Germano Caronni : Assorted small races.
51 * Alan Cox : sendmsg/recvmsg basic support.
52 * Alan Cox : Only sendmsg/recvmsg now supported.
53 * Alan Cox : Locked down bind (see security list).
54 * Alan Cox : Loosened bind a little.
55 * Mike McLagan : ADD/DEL DLCI Ioctls
56 * Willy Konynenberg : Transparent proxying support.
57 * David S. Miller : New socket lookup architecture.
58 * Some other random speedups.
59 * Cyrus Durgin : Cleaned up file for kmod hacks.
60 * Andi Kleen : Fix inet_stream_connect TCP race.
61 *
62 * This program is free software; you can redistribute it and/or
63 * modify it under the terms of the GNU General Public License
64 * as published by the Free Software Foundation; either version
65 * 2 of the License, or (at your option) any later version.
66 */
67
afd46503
JP
68#define pr_fmt(fmt) "IPv4: " fmt
69
f4c50d99 70#include <linux/err.h>
1da177e4
LT
71#include <linux/errno.h>
72#include <linux/types.h>
73#include <linux/socket.h>
74#include <linux/in.h>
75#include <linux/kernel.h>
1da177e4
LT
76#include <linux/module.h>
77#include <linux/sched.h>
78#include <linux/timer.h>
79#include <linux/string.h>
80#include <linux/sockios.h>
81#include <linux/net.h>
4fc268d2 82#include <linux/capability.h>
1da177e4
LT
83#include <linux/fcntl.h>
84#include <linux/mm.h>
85#include <linux/interrupt.h>
86#include <linux/stat.h>
87#include <linux/init.h>
88#include <linux/poll.h>
89#include <linux/netfilter_ipv4.h>
b3da2cf3 90#include <linux/random.h>
5a0e3ad6 91#include <linux/slab.h>
1da177e4
LT
92
93#include <asm/uaccess.h>
1da177e4 94
1da177e4
LT
95#include <linux/inet.h>
96#include <linux/igmp.h>
14c85021 97#include <linux/inetdevice.h>
1da177e4 98#include <linux/netdevice.h>
73cc19f1 99#include <net/checksum.h>
1da177e4
LT
100#include <net/ip.h>
101#include <net/protocol.h>
102#include <net/arp.h>
103#include <net/route.h>
104#include <net/ip_fib.h>
295f7324 105#include <net/inet_connection_sock.h>
1da177e4
LT
106#include <net/tcp.h>
107#include <net/udp.h>
ba4e58ec 108#include <net/udplite.h>
c319b4d7 109#include <net/ping.h>
1da177e4
LT
110#include <linux/skbuff.h>
111#include <net/sock.h>
112#include <net/raw.h>
113#include <net/icmp.h>
1da177e4
LT
114#include <net/inet_common.h>
115#include <net/xfrm.h>
9b4661bd 116#include <net/net_namespace.h>
1da177e4
LT
117#ifdef CONFIG_IP_MROUTE
118#include <linux/mroute.h>
119#endif
120
1da177e4
LT
121
122/* The inetsw table contains everything that inet_create needs to
123 * build a new socket.
124 */
125static struct list_head inetsw[SOCK_MAX];
126static DEFINE_SPINLOCK(inetsw_lock);
127
9ba63979 128struct ipv4_config ipv4_config;
9ba63979
PE
129EXPORT_SYMBOL(ipv4_config);
130
1da177e4
LT
131/* New destruction routine */
132
133void inet_sock_destruct(struct sock *sk)
134{
135 struct inet_sock *inet = inet_sk(sk);
136
137 __skb_queue_purge(&sk->sk_receive_queue);
138 __skb_queue_purge(&sk->sk_error_queue);
139
95766fff
HA
140 sk_mem_reclaim(sk);
141
1da177e4 142 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
3d1427f8 143 pr_err("Attempt to release TCP socket in state %d %p\n",
1da177e4
LT
144 sk->sk_state, sk);
145 return;
146 }
147 if (!sock_flag(sk, SOCK_DEAD)) {
3d1427f8 148 pr_err("Attempt to release alive inet socket %p\n", sk);
1da177e4
LT
149 return;
150 }
151
547b792c
IJ
152 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
153 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
154 WARN_ON(sk->sk_wmem_queued);
155 WARN_ON(sk->sk_forward_alloc);
1da177e4 156
f6d8bd05 157 kfree(rcu_dereference_protected(inet->inet_opt, 1));
b6c6712a 158 dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
41063e9d 159 dst_release(sk->sk_rx_dst);
e6848976 160 sk_refcnt_debug_dec(sk);
1da177e4 161}
3d1427f8 162EXPORT_SYMBOL(inet_sock_destruct);
1da177e4
LT
163
164/*
165 * The routines beyond this point handle the behaviour of an AF_INET
166 * socket object. Mostly it punts to the subprotocols of IP to do
167 * the work.
168 */
169
170/*
171 * Automatically bind an unbound socket.
172 */
173
174static int inet_autobind(struct sock *sk)
175{
176 struct inet_sock *inet;
177 /* We may need to bind the socket. */
178 lock_sock(sk);
179 inet = inet_sk(sk);
c720c7e8 180 if (!inet->inet_num) {
1da177e4
LT
181 if (sk->sk_prot->get_port(sk, 0)) {
182 release_sock(sk);
183 return -EAGAIN;
184 }
c720c7e8 185 inet->inet_sport = htons(inet->inet_num);
1da177e4
LT
186 }
187 release_sock(sk);
188 return 0;
189}
190
191/*
192 * Move a socket into listening state.
193 */
194int inet_listen(struct socket *sock, int backlog)
195{
196 struct sock *sk = sock->sk;
197 unsigned char old_state;
198 int err;
199
200 lock_sock(sk);
201
202 err = -EINVAL;
203 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
204 goto out;
205
206 old_state = sk->sk_state;
207 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
208 goto out;
209
210 /* Really, if the socket is already in listen state
211 * we can only allow the backlog to be adjusted.
212 */
213 if (old_state != TCP_LISTEN) {
8336886f
JC
214 /* Check special setups for testing purpose to enable TFO w/o
215 * requiring TCP_FASTOPEN sockopt.
216 * Note that only TCP sockets (SOCK_STREAM) will reach here.
217 * Also fastopenq may already been allocated because this
218 * socket was in TCP_LISTEN state previously but was
219 * shutdown() (rather than close()).
220 */
221 if ((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) != 0 &&
222 inet_csk(sk)->icsk_accept_queue.fastopenq == NULL) {
223 if ((sysctl_tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) != 0)
224 err = fastopen_init_queue(sk, backlog);
225 else if ((sysctl_tcp_fastopen &
226 TFO_SERVER_WO_SOCKOPT2) != 0)
227 err = fastopen_init_queue(sk,
228 ((uint)sysctl_tcp_fastopen) >> 16);
229 else
230 err = 0;
231 if (err)
232 goto out;
233 }
72a3effa 234 err = inet_csk_listen_start(sk, backlog);
1da177e4
LT
235 if (err)
236 goto out;
237 }
238 sk->sk_max_ack_backlog = backlog;
239 err = 0;
240
241out:
242 release_sock(sk);
243 return err;
244}
3d1427f8 245EXPORT_SYMBOL(inet_listen);
1da177e4 246
be776281 247u32 inet_ehash_secret __read_mostly;
b3da2cf3
DM
248EXPORT_SYMBOL(inet_ehash_secret);
249
08dcdbf6
ED
250u32 ipv6_hash_secret __read_mostly;
251EXPORT_SYMBOL(ipv6_hash_secret);
252
be776281 253/*
08dcdbf6
ED
254 * inet_ehash_secret must be set exactly once, and to a non nul value
255 * ipv6_hash_secret must be set exactly once.
be776281 256 */
b3da2cf3
DM
257void build_ehash_secret(void)
258{
be776281 259 u32 rnd;
49e8ab03 260
be776281
ED
261 do {
262 get_random_bytes(&rnd, sizeof(rnd));
263 } while (rnd == 0);
49e8ab03 264
08dcdbf6
ED
265 if (cmpxchg(&inet_ehash_secret, 0, rnd) == 0)
266 get_random_bytes(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
b3da2cf3
DM
267}
268EXPORT_SYMBOL(build_ehash_secret);
269
1da177e4
LT
270/*
271 * Create an inet socket.
272 */
273
3f378b68
EP
274static int inet_create(struct net *net, struct socket *sock, int protocol,
275 int kern)
1da177e4
LT
276{
277 struct sock *sk;
1da177e4
LT
278 struct inet_protosw *answer;
279 struct inet_sock *inet;
280 struct proto *answer_prot;
281 unsigned char answer_flags;
282 char answer_no_check;
bb97d31f 283 int try_loading_module = 0;
86c8f9d1 284 int err;
1da177e4 285
04f258ce
ED
286 if (unlikely(!inet_ehash_secret))
287 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
288 build_ehash_secret();
b3da2cf3 289
1da177e4
LT
290 sock->state = SS_UNCONNECTED;
291
292 /* Look for the requested type/protocol pair. */
bb97d31f 293lookup_protocol:
86c8f9d1 294 err = -ESOCKTNOSUPPORT;
1da177e4 295 rcu_read_lock();
696adfe8 296 list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
1da177e4 297
696adfe8 298 err = 0;
1da177e4
LT
299 /* Check the non-wild match. */
300 if (protocol == answer->protocol) {
301 if (protocol != IPPROTO_IP)
302 break;
303 } else {
304 /* Check for the two wild cases. */
305 if (IPPROTO_IP == protocol) {
306 protocol = answer->protocol;
307 break;
308 }
309 if (IPPROTO_IP == answer->protocol)
310 break;
311 }
86c8f9d1 312 err = -EPROTONOSUPPORT;
1da177e4
LT
313 }
314
696adfe8 315 if (unlikely(err)) {
bb97d31f
ACM
316 if (try_loading_module < 2) {
317 rcu_read_unlock();
318 /*
319 * Be more specific, e.g. net-pf-2-proto-132-type-1
320 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
321 */
322 if (++try_loading_module == 1)
323 request_module("net-pf-%d-proto-%d-type-%d",
324 PF_INET, protocol, sock->type);
325 /*
326 * Fall back to generic, e.g. net-pf-2-proto-132
327 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
328 */
329 else
330 request_module("net-pf-%d-proto-%d",
331 PF_INET, protocol);
332 goto lookup_protocol;
333 } else
334 goto out_rcu_unlock;
335 }
336
1da177e4 337 err = -EPERM;
52e804c6
EB
338 if (sock->type == SOCK_RAW && !kern &&
339 !ns_capable(net->user_ns, CAP_NET_RAW))
1da177e4 340 goto out_rcu_unlock;
1da177e4
LT
341
342 sock->ops = answer->ops;
343 answer_prot = answer->prot;
344 answer_no_check = answer->no_check;
345 answer_flags = answer->flags;
346 rcu_read_unlock();
347
547b792c 348 WARN_ON(answer_prot->slab == NULL);
1da177e4
LT
349
350 err = -ENOBUFS;
6257ff21 351 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
1da177e4
LT
352 if (sk == NULL)
353 goto out;
354
355 err = 0;
356 sk->sk_no_check = answer_no_check;
357 if (INET_PROTOSW_REUSE & answer_flags)
4a17fd52 358 sk->sk_reuse = SK_CAN_REUSE;
1da177e4
LT
359
360 inet = inet_sk(sk);
469de9b9 361 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1da177e4 362
7b2ff18e
JO
363 inet->nodefrag = 0;
364
1da177e4 365 if (SOCK_RAW == sock->type) {
c720c7e8 366 inet->inet_num = protocol;
1da177e4
LT
367 if (IPPROTO_RAW == protocol)
368 inet->hdrincl = 1;
369 }
370
371 if (ipv4_config.no_pmtu_disc)
372 inet->pmtudisc = IP_PMTUDISC_DONT;
373 else
374 inet->pmtudisc = IP_PMTUDISC_WANT;
375
c720c7e8 376 inet->inet_id = 0;
1da177e4
LT
377
378 sock_init_data(sock, sk);
379
380 sk->sk_destruct = inet_sock_destruct;
1da177e4
LT
381 sk->sk_protocol = protocol;
382 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
383
384 inet->uc_ttl = -1;
385 inet->mc_loop = 1;
386 inet->mc_ttl = 1;
f771bef9 387 inet->mc_all = 1;
1da177e4
LT
388 inet->mc_index = 0;
389 inet->mc_list = NULL;
4c507d28 390 inet->rcv_tos = 0;
1da177e4 391
e6848976 392 sk_refcnt_debug_inc(sk);
1da177e4 393
c720c7e8 394 if (inet->inet_num) {
1da177e4
LT
395 /* It assumes that any protocol which allows
396 * the user to assign a number at socket
397 * creation time automatically
398 * shares.
399 */
c720c7e8 400 inet->inet_sport = htons(inet->inet_num);
1da177e4
LT
401 /* Add to protocol hash chains. */
402 sk->sk_prot->hash(sk);
403 }
404
405 if (sk->sk_prot->init) {
406 err = sk->sk_prot->init(sk);
407 if (err)
408 sk_common_release(sk);
409 }
410out:
411 return err;
412out_rcu_unlock:
413 rcu_read_unlock();
414 goto out;
415}
416
417
418/*
419 * The peer socket should always be NULL (or else). When we call this
420 * function we are destroying the object and from then on nobody
421 * should refer to it.
422 */
423int inet_release(struct socket *sock)
424{
425 struct sock *sk = sock->sk;
426
427 if (sk) {
428 long timeout;
429
c58dc01b 430 sock_rps_reset_flow(sk);
fec5e652 431
1da177e4
LT
432 /* Applications forget to leave groups before exiting */
433 ip_mc_drop_socket(sk);
434
435 /* If linger is set, we don't return until the close
436 * is complete. Otherwise we return immediately. The
437 * actually closing is done the same either way.
438 *
439 * If the close is due to the process exiting, we never
440 * linger..
441 */
442 timeout = 0;
443 if (sock_flag(sk, SOCK_LINGER) &&
444 !(current->flags & PF_EXITING))
445 timeout = sk->sk_lingertime;
446 sock->sk = NULL;
447 sk->sk_prot->close(sk, timeout);
448 }
449 return 0;
450}
3d1427f8 451EXPORT_SYMBOL(inet_release);
1da177e4
LT
452
453/* It is off by default, see below. */
ab32ea5d 454int sysctl_ip_nonlocal_bind __read_mostly;
3d1427f8 455EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);
1da177e4
LT
456
457int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
458{
459 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
460 struct sock *sk = sock->sk;
461 struct inet_sock *inet = inet_sk(sk);
3594698a 462 struct net *net = sock_net(sk);
1da177e4
LT
463 unsigned short snum;
464 int chk_addr_ret;
465 int err;
466
467 /* If the socket has its own bind function then use it. (RAW) */
468 if (sk->sk_prot->bind) {
469 err = sk->sk_prot->bind(sk, uaddr, addr_len);
470 goto out;
471 }
472 err = -EINVAL;
473 if (addr_len < sizeof(struct sockaddr_in))
474 goto out;
475
c349a528 476 if (addr->sin_family != AF_INET) {
29c486df
ED
477 /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
478 * only if s_addr is INADDR_ANY.
479 */
c349a528 480 err = -EAFNOSUPPORT;
29c486df
ED
481 if (addr->sin_family != AF_UNSPEC ||
482 addr->sin_addr.s_addr != htonl(INADDR_ANY))
483 goto out;
c349a528 484 }
d0733d2e 485
3594698a 486 chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
1da177e4
LT
487
488 /* Not specified by any standard per-se, however it breaks too
489 * many applications when removed. It is unfortunate since
490 * allowing applications to make a non-local bind solves
491 * several problems with systems using dynamic addressing.
492 * (ie. your servers still start up even if your ISDN link
493 * is temporarily down)
494 */
495 err = -EADDRNOTAVAIL;
496 if (!sysctl_ip_nonlocal_bind &&
b9fb1506 497 !(inet->freebind || inet->transparent) &&
e6f1cebf 498 addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
1da177e4
LT
499 chk_addr_ret != RTN_LOCAL &&
500 chk_addr_ret != RTN_MULTICAST &&
501 chk_addr_ret != RTN_BROADCAST)
502 goto out;
503
504 snum = ntohs(addr->sin_port);
505 err = -EACCES;
3594698a
EB
506 if (snum && snum < PROT_SOCK &&
507 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1da177e4
LT
508 goto out;
509
510 /* We keep a pair of addresses. rcv_saddr is the one
511 * used by hash lookups, and saddr is used for transmit.
512 *
513 * In the BSD API these are the same except where it
514 * would be illegal to use them (multicast/broadcast) in
515 * which case the sending device address is used.
516 */
517 lock_sock(sk);
518
519 /* Check these errors (active socket, double bind). */
520 err = -EINVAL;
c720c7e8 521 if (sk->sk_state != TCP_CLOSE || inet->inet_num)
1da177e4
LT
522 goto out_release_sock;
523
c720c7e8 524 inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
1da177e4 525 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
c720c7e8 526 inet->inet_saddr = 0; /* Use device */
1da177e4
LT
527
528 /* Make sure we are allowed to bind here. */
529 if (sk->sk_prot->get_port(sk, snum)) {
c720c7e8 530 inet->inet_saddr = inet->inet_rcv_saddr = 0;
1da177e4
LT
531 err = -EADDRINUSE;
532 goto out_release_sock;
533 }
534
c720c7e8 535 if (inet->inet_rcv_saddr)
1da177e4
LT
536 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
537 if (snum)
538 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
c720c7e8
ED
539 inet->inet_sport = htons(inet->inet_num);
540 inet->inet_daddr = 0;
541 inet->inet_dport = 0;
1da177e4
LT
542 sk_dst_reset(sk);
543 err = 0;
544out_release_sock:
545 release_sock(sk);
546out:
547 return err;
548}
3d1427f8 549EXPORT_SYMBOL(inet_bind);
1da177e4 550
5e73ea1a 551int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr,
1da177e4
LT
552 int addr_len, int flags)
553{
554 struct sock *sk = sock->sk;
555
6503d961
CG
556 if (addr_len < sizeof(uaddr->sa_family))
557 return -EINVAL;
1da177e4
LT
558 if (uaddr->sa_family == AF_UNSPEC)
559 return sk->sk_prot->disconnect(sk, flags);
560
c720c7e8 561 if (!inet_sk(sk)->inet_num && inet_autobind(sk))
1da177e4 562 return -EAGAIN;
e3192690 563 return sk->sk_prot->connect(sk, uaddr, addr_len);
1da177e4 564}
3d1427f8 565EXPORT_SYMBOL(inet_dgram_connect);
1da177e4 566
783237e8 567static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias)
1da177e4
LT
568{
569 DEFINE_WAIT(wait);
570
aa395145 571 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
783237e8 572 sk->sk_write_pending += writebias;
1da177e4
LT
573
574 /* Basic assumption: if someone sets sk->sk_err, he _must_
575 * change state of the socket from TCP_SYN_*.
576 * Connect() does not allow to get error notifications
577 * without closing the socket.
578 */
579 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
580 release_sock(sk);
581 timeo = schedule_timeout(timeo);
582 lock_sock(sk);
583 if (signal_pending(current) || !timeo)
584 break;
aa395145 585 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1da177e4 586 }
aa395145 587 finish_wait(sk_sleep(sk), &wait);
783237e8 588 sk->sk_write_pending -= writebias;
1da177e4
LT
589 return timeo;
590}
591
592/*
593 * Connect to a remote host. There is regrettably still a little
594 * TCP 'magic' in here.
595 */
cf60af03
YC
596int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
597 int addr_len, int flags)
1da177e4
LT
598{
599 struct sock *sk = sock->sk;
600 int err;
601 long timeo;
602
6503d961
CG
603 if (addr_len < sizeof(uaddr->sa_family))
604 return -EINVAL;
605
1da177e4
LT
606 if (uaddr->sa_family == AF_UNSPEC) {
607 err = sk->sk_prot->disconnect(sk, flags);
608 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
609 goto out;
610 }
611
612 switch (sock->state) {
613 default:
614 err = -EINVAL;
615 goto out;
616 case SS_CONNECTED:
617 err = -EISCONN;
618 goto out;
619 case SS_CONNECTING:
620 err = -EALREADY;
621 /* Fall out of switch with err, set for this state */
622 break;
623 case SS_UNCONNECTED:
624 err = -EISCONN;
625 if (sk->sk_state != TCP_CLOSE)
626 goto out;
627
628 err = sk->sk_prot->connect(sk, uaddr, addr_len);
629 if (err < 0)
630 goto out;
631
e905a9ed 632 sock->state = SS_CONNECTING;
1da177e4
LT
633
634 /* Just entered SS_CONNECTING state; the only
635 * difference is that return value in non-blocking
636 * case is EINPROGRESS, rather than EALREADY.
637 */
638 err = -EINPROGRESS;
639 break;
640 }
641
642 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
643
644 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
783237e8
YC
645 int writebias = (sk->sk_protocol == IPPROTO_TCP) &&
646 tcp_sk(sk)->fastopen_req &&
647 tcp_sk(sk)->fastopen_req->data ? 1 : 0;
648
1da177e4 649 /* Error code is set above */
783237e8 650 if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
1da177e4
LT
651 goto out;
652
653 err = sock_intr_errno(timeo);
654 if (signal_pending(current))
655 goto out;
656 }
657
658 /* Connection was closed by RST, timeout, ICMP error
659 * or another process disconnected us.
660 */
661 if (sk->sk_state == TCP_CLOSE)
662 goto sock_error;
663
664 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
665 * and error was received after socket entered established state.
666 * Hence, it is handled normally after connect() return successfully.
667 */
668
669 sock->state = SS_CONNECTED;
670 err = 0;
671out:
1da177e4
LT
672 return err;
673
674sock_error:
675 err = sock_error(sk) ? : -ECONNABORTED;
676 sock->state = SS_UNCONNECTED;
677 if (sk->sk_prot->disconnect(sk, flags))
678 sock->state = SS_DISCONNECTING;
679 goto out;
680}
cf60af03
YC
681EXPORT_SYMBOL(__inet_stream_connect);
682
683int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
684 int addr_len, int flags)
685{
686 int err;
687
688 lock_sock(sock->sk);
689 err = __inet_stream_connect(sock, uaddr, addr_len, flags);
690 release_sock(sock->sk);
691 return err;
692}
3d1427f8 693EXPORT_SYMBOL(inet_stream_connect);
1da177e4
LT
694
695/*
696 * Accept a pending connection. The TCP layer now gives BSD semantics.
697 */
698
699int inet_accept(struct socket *sock, struct socket *newsock, int flags)
700{
701 struct sock *sk1 = sock->sk;
702 int err = -EINVAL;
703 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
704
705 if (!sk2)
706 goto do_err;
707
708 lock_sock(sk2);
709
1eddcead 710 sock_rps_record_flow(sk2);
547b792c 711 WARN_ON(!((1 << sk2->sk_state) &
8336886f
JC
712 (TCPF_ESTABLISHED | TCPF_SYN_RECV |
713 TCPF_CLOSE_WAIT | TCPF_CLOSE)));
1da177e4
LT
714
715 sock_graft(sk2, newsock);
716
717 newsock->state = SS_CONNECTED;
718 err = 0;
719 release_sock(sk2);
720do_err:
721 return err;
722}
3d1427f8 723EXPORT_SYMBOL(inet_accept);
1da177e4
LT
724
725
726/*
727 * This does both peername and sockname.
728 */
729int inet_getname(struct socket *sock, struct sockaddr *uaddr,
730 int *uaddr_len, int peer)
731{
732 struct sock *sk = sock->sk;
733 struct inet_sock *inet = inet_sk(sk);
38bfd8f5 734 DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
1da177e4
LT
735
736 sin->sin_family = AF_INET;
737 if (peer) {
c720c7e8 738 if (!inet->inet_dport ||
1da177e4
LT
739 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
740 peer == 1))
741 return -ENOTCONN;
c720c7e8
ED
742 sin->sin_port = inet->inet_dport;
743 sin->sin_addr.s_addr = inet->inet_daddr;
1da177e4 744 } else {
c720c7e8 745 __be32 addr = inet->inet_rcv_saddr;
1da177e4 746 if (!addr)
c720c7e8
ED
747 addr = inet->inet_saddr;
748 sin->sin_port = inet->inet_sport;
1da177e4
LT
749 sin->sin_addr.s_addr = addr;
750 }
751 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
752 *uaddr_len = sizeof(*sin);
753 return 0;
754}
3d1427f8 755EXPORT_SYMBOL(inet_getname);
1da177e4
LT
756
757int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
758 size_t size)
759{
760 struct sock *sk = sock->sk;
761
c58dc01b 762 sock_rps_record_flow(sk);
fec5e652 763
1da177e4 764 /* We may need to bind the socket. */
7ba42910
CG
765 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
766 inet_autobind(sk))
1da177e4
LT
767 return -EAGAIN;
768
769 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
770}
3d1427f8 771EXPORT_SYMBOL(inet_sendmsg);
1da177e4 772
7ba42910
CG
773ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
774 size_t size, int flags)
1da177e4
LT
775{
776 struct sock *sk = sock->sk;
777
c58dc01b 778 sock_rps_record_flow(sk);
fec5e652 779
1da177e4 780 /* We may need to bind the socket. */
7ba42910
CG
781 if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
782 inet_autobind(sk))
1da177e4
LT
783 return -EAGAIN;
784
785 if (sk->sk_prot->sendpage)
786 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
787 return sock_no_sendpage(sock, page, offset, size, flags);
788}
7ba42910 789EXPORT_SYMBOL(inet_sendpage);
1da177e4 790
fec5e652
TH
791int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
792 size_t size, int flags)
793{
794 struct sock *sk = sock->sk;
795 int addr_len = 0;
796 int err;
797
c58dc01b 798 sock_rps_record_flow(sk);
fec5e652
TH
799
800 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
801 flags & ~MSG_DONTWAIT, &addr_len);
802 if (err >= 0)
803 msg->msg_namelen = addr_len;
804 return err;
805}
806EXPORT_SYMBOL(inet_recvmsg);
1da177e4
LT
807
808int inet_shutdown(struct socket *sock, int how)
809{
810 struct sock *sk = sock->sk;
811 int err = 0;
812
813 /* This should really check to make sure
814 * the socket is a TCP socket. (WHY AC...)
815 */
816 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
817 1->2 bit 2 snds.
818 2->3 */
819 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
820 return -EINVAL;
821
822 lock_sock(sk);
823 if (sock->state == SS_CONNECTING) {
824 if ((1 << sk->sk_state) &
825 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
826 sock->state = SS_DISCONNECTING;
827 else
828 sock->state = SS_CONNECTED;
829 }
830
831 switch (sk->sk_state) {
832 case TCP_CLOSE:
833 err = -ENOTCONN;
834 /* Hack to wake up other listeners, who can poll for
835 POLLHUP, even on eg. unconnected UDP sockets -- RR */
836 default:
837 sk->sk_shutdown |= how;
838 if (sk->sk_prot->shutdown)
839 sk->sk_prot->shutdown(sk, how);
840 break;
841
842 /* Remaining two branches are temporary solution for missing
843 * close() in multithreaded environment. It is _not_ a good idea,
844 * but we have no choice until close() is repaired at VFS level.
845 */
846 case TCP_LISTEN:
847 if (!(how & RCV_SHUTDOWN))
848 break;
849 /* Fall through */
850 case TCP_SYN_SENT:
851 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
852 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
853 break;
854 }
855
856 /* Wake up anyone sleeping in poll. */
857 sk->sk_state_change(sk);
858 release_sock(sk);
859 return err;
860}
3d1427f8 861EXPORT_SYMBOL(inet_shutdown);
1da177e4
LT
862
863/*
864 * ioctl() calls you can issue on an INET socket. Most of these are
865 * device configuration and stuff and very rarely used. Some ioctls
866 * pass on to the socket itself.
867 *
868 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
869 * loads the devconfigure module does its configuring and unloads it.
870 * There's a good 20K of config code hanging around the kernel.
871 */
872
873int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
874{
875 struct sock *sk = sock->sk;
876 int err = 0;
3b1e0a65 877 struct net *net = sock_net(sk);
1da177e4
LT
878
879 switch (cmd) {
3d1427f8
ED
880 case SIOCGSTAMP:
881 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
882 break;
883 case SIOCGSTAMPNS:
884 err = sock_get_timestampns(sk, (struct timespec __user *)arg);
885 break;
886 case SIOCADDRT:
887 case SIOCDELRT:
888 case SIOCRTMSG:
889 err = ip_rt_ioctl(net, cmd, (void __user *)arg);
890 break;
891 case SIOCDARP:
892 case SIOCGARP:
893 case SIOCSARP:
894 err = arp_ioctl(net, cmd, (void __user *)arg);
895 break;
896 case SIOCGIFADDR:
897 case SIOCSIFADDR:
898 case SIOCGIFBRDADDR:
899 case SIOCSIFBRDADDR:
900 case SIOCGIFNETMASK:
901 case SIOCSIFNETMASK:
902 case SIOCGIFDSTADDR:
903 case SIOCSIFDSTADDR:
904 case SIOCSIFPFLAGS:
905 case SIOCGIFPFLAGS:
906 case SIOCSIFFLAGS:
907 err = devinet_ioctl(net, cmd, (void __user *)arg);
908 break;
909 default:
910 if (sk->sk_prot->ioctl)
911 err = sk->sk_prot->ioctl(sk, cmd, arg);
912 else
913 err = -ENOIOCTLCMD;
914 break;
1da177e4
LT
915 }
916 return err;
917}
3d1427f8 918EXPORT_SYMBOL(inet_ioctl);
1da177e4 919
709b46e8 920#ifdef CONFIG_COMPAT
686dc6b6 921static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
709b46e8
EB
922{
923 struct sock *sk = sock->sk;
924 int err = -ENOIOCTLCMD;
925
926 if (sk->sk_prot->compat_ioctl)
927 err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
928
929 return err;
930}
931#endif
932
90ddc4f0 933const struct proto_ops inet_stream_ops = {
543d9cfe
ACM
934 .family = PF_INET,
935 .owner = THIS_MODULE,
936 .release = inet_release,
937 .bind = inet_bind,
938 .connect = inet_stream_connect,
939 .socketpair = sock_no_socketpair,
940 .accept = inet_accept,
941 .getname = inet_getname,
942 .poll = tcp_poll,
943 .ioctl = inet_ioctl,
944 .listen = inet_listen,
945 .shutdown = inet_shutdown,
946 .setsockopt = sock_common_setsockopt,
947 .getsockopt = sock_common_getsockopt,
7ba42910 948 .sendmsg = inet_sendmsg,
fec5e652 949 .recvmsg = inet_recvmsg,
543d9cfe 950 .mmap = sock_no_mmap,
7ba42910 951 .sendpage = inet_sendpage,
9c55e01c 952 .splice_read = tcp_splice_read,
3fdadf7d 953#ifdef CONFIG_COMPAT
543d9cfe
ACM
954 .compat_setsockopt = compat_sock_common_setsockopt,
955 .compat_getsockopt = compat_sock_common_getsockopt,
709b46e8 956 .compat_ioctl = inet_compat_ioctl,
3fdadf7d 957#endif
1da177e4 958};
3d1427f8 959EXPORT_SYMBOL(inet_stream_ops);
1da177e4 960
90ddc4f0 961const struct proto_ops inet_dgram_ops = {
543d9cfe
ACM
962 .family = PF_INET,
963 .owner = THIS_MODULE,
964 .release = inet_release,
965 .bind = inet_bind,
966 .connect = inet_dgram_connect,
967 .socketpair = sock_no_socketpair,
968 .accept = sock_no_accept,
969 .getname = inet_getname,
970 .poll = udp_poll,
971 .ioctl = inet_ioctl,
972 .listen = sock_no_listen,
973 .shutdown = inet_shutdown,
974 .setsockopt = sock_common_setsockopt,
975 .getsockopt = sock_common_getsockopt,
976 .sendmsg = inet_sendmsg,
fec5e652 977 .recvmsg = inet_recvmsg,
543d9cfe
ACM
978 .mmap = sock_no_mmap,
979 .sendpage = inet_sendpage,
3fdadf7d 980#ifdef CONFIG_COMPAT
543d9cfe
ACM
981 .compat_setsockopt = compat_sock_common_setsockopt,
982 .compat_getsockopt = compat_sock_common_getsockopt,
709b46e8 983 .compat_ioctl = inet_compat_ioctl,
3fdadf7d 984#endif
1da177e4 985};
3d1427f8 986EXPORT_SYMBOL(inet_dgram_ops);
1da177e4
LT
987
988/*
989 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
990 * udp_poll
991 */
90ddc4f0 992static const struct proto_ops inet_sockraw_ops = {
543d9cfe
ACM
993 .family = PF_INET,
994 .owner = THIS_MODULE,
995 .release = inet_release,
996 .bind = inet_bind,
997 .connect = inet_dgram_connect,
998 .socketpair = sock_no_socketpair,
999 .accept = sock_no_accept,
1000 .getname = inet_getname,
1001 .poll = datagram_poll,
1002 .ioctl = inet_ioctl,
1003 .listen = sock_no_listen,
1004 .shutdown = inet_shutdown,
1005 .setsockopt = sock_common_setsockopt,
1006 .getsockopt = sock_common_getsockopt,
1007 .sendmsg = inet_sendmsg,
fec5e652 1008 .recvmsg = inet_recvmsg,
543d9cfe
ACM
1009 .mmap = sock_no_mmap,
1010 .sendpage = inet_sendpage,
3fdadf7d 1011#ifdef CONFIG_COMPAT
543d9cfe
ACM
1012 .compat_setsockopt = compat_sock_common_setsockopt,
1013 .compat_getsockopt = compat_sock_common_getsockopt,
709b46e8 1014 .compat_ioctl = inet_compat_ioctl,
3fdadf7d 1015#endif
1da177e4
LT
1016};
1017
ec1b4cf7 1018static const struct net_proto_family inet_family_ops = {
1da177e4
LT
1019 .family = PF_INET,
1020 .create = inet_create,
1021 .owner = THIS_MODULE,
1022};
1023
1da177e4
LT
1024/* Upon startup we insert all the elements in inetsw_array[] into
1025 * the linked list inetsw.
1026 */
1027static struct inet_protosw inetsw_array[] =
1028{
e905a9ed
YH
1029 {
1030 .type = SOCK_STREAM,
1031 .protocol = IPPROTO_TCP,
1032 .prot = &tcp_prot,
1033 .ops = &inet_stream_ops,
e905a9ed
YH
1034 .no_check = 0,
1035 .flags = INET_PROTOSW_PERMANENT |
d83d8461 1036 INET_PROTOSW_ICSK,
e905a9ed
YH
1037 },
1038
1039 {
1040 .type = SOCK_DGRAM,
1041 .protocol = IPPROTO_UDP,
1042 .prot = &udp_prot,
1043 .ops = &inet_dgram_ops,
e905a9ed
YH
1044 .no_check = UDP_CSUM_DEFAULT,
1045 .flags = INET_PROTOSW_PERMANENT,
1da177e4 1046 },
e905a9ed 1047
c319b4d7
VK
1048 {
1049 .type = SOCK_DGRAM,
1050 .protocol = IPPROTO_ICMP,
1051 .prot = &ping_prot,
1052 .ops = &inet_dgram_ops,
1053 .no_check = UDP_CSUM_DEFAULT,
1054 .flags = INET_PROTOSW_REUSE,
1055 },
1da177e4
LT
1056
1057 {
e905a9ed
YH
1058 .type = SOCK_RAW,
1059 .protocol = IPPROTO_IP, /* wild card */
1060 .prot = &raw_prot,
1061 .ops = &inet_sockraw_ops,
e905a9ed
YH
1062 .no_check = UDP_CSUM_DEFAULT,
1063 .flags = INET_PROTOSW_REUSE,
1da177e4
LT
1064 }
1065};
1066
c40f6fff 1067#define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
1da177e4
LT
1068
1069void inet_register_protosw(struct inet_protosw *p)
1070{
1071 struct list_head *lh;
1072 struct inet_protosw *answer;
1073 int protocol = p->protocol;
1074 struct list_head *last_perm;
1075
1076 spin_lock_bh(&inetsw_lock);
1077
1078 if (p->type >= SOCK_MAX)
1079 goto out_illegal;
1080
1081 /* If we are trying to override a permanent protocol, bail. */
1082 answer = NULL;
1083 last_perm = &inetsw[p->type];
1084 list_for_each(lh, &inetsw[p->type]) {
1085 answer = list_entry(lh, struct inet_protosw, list);
1086
1087 /* Check only the non-wild match. */
1088 if (INET_PROTOSW_PERMANENT & answer->flags) {
1089 if (protocol == answer->protocol)
1090 break;
1091 last_perm = lh;
1092 }
1093
1094 answer = NULL;
1095 }
1096 if (answer)
1097 goto out_permanent;
1098
1099 /* Add the new entry after the last permanent entry if any, so that
1100 * the new entry does not override a permanent entry when matched with
1101 * a wild-card protocol. But it is allowed to override any existing
e905a9ed 1102 * non-permanent entry. This means that when we remove this entry, the
1da177e4
LT
1103 * system automatically returns to the old behavior.
1104 */
1105 list_add_rcu(&p->list, last_perm);
1106out:
1107 spin_unlock_bh(&inetsw_lock);
1108
1da177e4
LT
1109 return;
1110
1111out_permanent:
058bd4d2 1112 pr_err("Attempt to override permanent protocol %d\n", protocol);
1da177e4
LT
1113 goto out;
1114
1115out_illegal:
058bd4d2 1116 pr_err("Ignoring attempt to register invalid socket type %d\n",
1da177e4
LT
1117 p->type);
1118 goto out;
1119}
3d1427f8 1120EXPORT_SYMBOL(inet_register_protosw);
1da177e4
LT
1121
1122void inet_unregister_protosw(struct inet_protosw *p)
1123{
1124 if (INET_PROTOSW_PERMANENT & p->flags) {
058bd4d2 1125 pr_err("Attempt to unregister permanent protocol %d\n",
1da177e4
LT
1126 p->protocol);
1127 } else {
1128 spin_lock_bh(&inetsw_lock);
1129 list_del_rcu(&p->list);
1130 spin_unlock_bh(&inetsw_lock);
1131
1132 synchronize_net();
1133 }
1134}
3d1427f8 1135EXPORT_SYMBOL(inet_unregister_protosw);
1da177e4 1136
32519f11
ACM
1137/*
1138 * Shall we try to damage output packets if routing dev changes?
1139 */
1140
ab32ea5d 1141int sysctl_ip_dynaddr __read_mostly;
32519f11
ACM
1142
1143static int inet_sk_reselect_saddr(struct sock *sk)
1144{
1145 struct inet_sock *inet = inet_sk(sk);
c720c7e8 1146 __be32 old_saddr = inet->inet_saddr;
c720c7e8 1147 __be32 daddr = inet->inet_daddr;
6e869138 1148 struct flowi4 *fl4;
b23dd4fe
DM
1149 struct rtable *rt;
1150 __be32 new_saddr;
f6d8bd05 1151 struct ip_options_rcu *inet_opt;
32519f11 1152
f6d8bd05
ED
1153 inet_opt = rcu_dereference_protected(inet->inet_opt,
1154 sock_owned_by_user(sk));
1155 if (inet_opt && inet_opt->opt.srr)
1156 daddr = inet_opt->opt.faddr;
32519f11
ACM
1157
1158 /* Query new route. */
6e869138
DM
1159 fl4 = &inet->cork.fl.u.ip4;
1160 rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
b23dd4fe
DM
1161 sk->sk_bound_dev_if, sk->sk_protocol,
1162 inet->inet_sport, inet->inet_dport, sk, false);
1163 if (IS_ERR(rt))
1164 return PTR_ERR(rt);
32519f11 1165
d8d1f30b 1166 sk_setup_caps(sk, &rt->dst);
32519f11 1167
6e869138 1168 new_saddr = fl4->saddr;
32519f11
ACM
1169
1170 if (new_saddr == old_saddr)
1171 return 0;
1172
1173 if (sysctl_ip_dynaddr > 1) {
058bd4d2
JP
1174 pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
1175 __func__, &old_saddr, &new_saddr);
32519f11
ACM
1176 }
1177
c720c7e8 1178 inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
32519f11
ACM
1179
1180 /*
1181 * XXX The only one ugly spot where we need to
1182 * XXX really change the sockets identity after
1183 * XXX it has entered the hashes. -DaveM
1184 *
1185 * Besides that, it does not check for connection
1186 * uniqueness. Wait for troubles.
1187 */
1188 __sk_prot_rehash(sk);
1189 return 0;
1190}
1191
1192int inet_sk_rebuild_header(struct sock *sk)
1193{
1194 struct inet_sock *inet = inet_sk(sk);
1195 struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
3ca3c68e 1196 __be32 daddr;
f6d8bd05 1197 struct ip_options_rcu *inet_opt;
6e869138 1198 struct flowi4 *fl4;
32519f11
ACM
1199 int err;
1200
1201 /* Route is OK, nothing to do. */
1202 if (rt)
1203 return 0;
1204
1205 /* Reroute. */
f6d8bd05
ED
1206 rcu_read_lock();
1207 inet_opt = rcu_dereference(inet->inet_opt);
c720c7e8 1208 daddr = inet->inet_daddr;
f6d8bd05
ED
1209 if (inet_opt && inet_opt->opt.srr)
1210 daddr = inet_opt->opt.faddr;
1211 rcu_read_unlock();
6e869138
DM
1212 fl4 = &inet->cork.fl.u.ip4;
1213 rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
78fbfd8a
DM
1214 inet->inet_dport, inet->inet_sport,
1215 sk->sk_protocol, RT_CONN_FLAGS(sk),
1216 sk->sk_bound_dev_if);
b23dd4fe
DM
1217 if (!IS_ERR(rt)) {
1218 err = 0;
d8d1f30b 1219 sk_setup_caps(sk, &rt->dst);
b23dd4fe
DM
1220 } else {
1221 err = PTR_ERR(rt);
1222
32519f11
ACM
1223 /* Routing failed... */
1224 sk->sk_route_caps = 0;
1225 /*
1226 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1227 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1228 */
1229 if (!sysctl_ip_dynaddr ||
1230 sk->sk_state != TCP_SYN_SENT ||
1231 (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1232 (err = inet_sk_reselect_saddr(sk)) != 0)
1233 sk->sk_err_soft = -err;
1234 }
1235
1236 return err;
1237}
32519f11
ACM
1238EXPORT_SYMBOL(inet_sk_rebuild_header);
1239
a430a43d
HX
1240static int inet_gso_send_check(struct sk_buff *skb)
1241{
bca49f84 1242 const struct net_offload *ops;
f9242b6b 1243 const struct iphdr *iph;
a430a43d
HX
1244 int proto;
1245 int ihl;
1246 int err = -EINVAL;
1247
1248 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1249 goto out;
1250
eddc9ec5 1251 iph = ip_hdr(skb);
a430a43d
HX
1252 ihl = iph->ihl * 4;
1253 if (ihl < sizeof(*iph))
1254 goto out;
1255
1256 if (unlikely(!pskb_may_pull(skb, ihl)))
1257 goto out;
1258
badff6d0
ACM
1259 __skb_pull(skb, ihl);
1260 skb_reset_transport_header(skb);
eddc9ec5 1261 iph = ip_hdr(skb);
f9242b6b 1262 proto = iph->protocol;
a430a43d
HX
1263 err = -EPROTONOSUPPORT;
1264
1265 rcu_read_lock();
bca49f84 1266 ops = rcu_dereference(inet_offloads[proto]);
f191a1d1
VY
1267 if (likely(ops && ops->callbacks.gso_send_check))
1268 err = ops->callbacks.gso_send_check(skb);
a430a43d
HX
1269 rcu_read_unlock();
1270
1271out:
1272 return err;
1273}
1274
c8f44aff
MM
1275static struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1276 netdev_features_t features)
f4c50d99
HX
1277{
1278 struct sk_buff *segs = ERR_PTR(-EINVAL);
bca49f84 1279 const struct net_offload *ops;
f9242b6b 1280 struct iphdr *iph;
f4c50d99
HX
1281 int proto;
1282 int ihl;
1283 int id;
d7ca4cc0 1284 unsigned int offset = 0;
73136267 1285 bool tunnel;
f4c50d99 1286
bbcf467d
HX
1287 if (unlikely(skb_shinfo(skb)->gso_type &
1288 ~(SKB_GSO_TCPV4 |
1289 SKB_GSO_UDP |
1290 SKB_GSO_DODGY |
1291 SKB_GSO_TCP_ECN |
68c33163 1292 SKB_GSO_GRE |
73136267 1293 SKB_GSO_UDP_TUNNEL |
bbcf467d
HX
1294 0)))
1295 goto out;
1296
1297 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
f4c50d99
HX
1298 goto out;
1299
eddc9ec5 1300 iph = ip_hdr(skb);
f4c50d99
HX
1301 ihl = iph->ihl * 4;
1302 if (ihl < sizeof(*iph))
1303 goto out;
1304
bbcf467d 1305 if (unlikely(!pskb_may_pull(skb, ihl)))
f4c50d99
HX
1306 goto out;
1307
73136267
PS
1308 tunnel = !!skb->encapsulation;
1309
badff6d0
ACM
1310 __skb_pull(skb, ihl);
1311 skb_reset_transport_header(skb);
eddc9ec5 1312 iph = ip_hdr(skb);
f4c50d99 1313 id = ntohs(iph->id);
f9242b6b 1314 proto = iph->protocol;
f4c50d99
HX
1315 segs = ERR_PTR(-EPROTONOSUPPORT);
1316
1317 rcu_read_lock();
bca49f84 1318 ops = rcu_dereference(inet_offloads[proto]);
f191a1d1
VY
1319 if (likely(ops && ops->callbacks.gso_segment))
1320 segs = ops->callbacks.gso_segment(skb, features);
f4c50d99
HX
1321 rcu_read_unlock();
1322
50c3a487 1323 if (IS_ERR_OR_NULL(segs))
f4c50d99
HX
1324 goto out;
1325
1326 skb = segs;
1327 do {
eddc9ec5 1328 iph = ip_hdr(skb);
73136267 1329 if (!tunnel && proto == IPPROTO_UDP) {
d7ca4cc0
SS
1330 iph->id = htons(id);
1331 iph->frag_off = htons(offset >> 3);
1332 if (skb->next != NULL)
1333 iph->frag_off |= htons(IP_MF);
1334 offset += (skb->len - skb->mac_len - iph->ihl * 4);
490ab081 1335 } else {
25c7704d 1336 iph->id = htons(id++);
490ab081 1337 }
f4c50d99
HX
1338 iph->tot_len = htons(skb->len - skb->mac_len);
1339 iph->check = 0;
d56f90a7 1340 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
f4c50d99
HX
1341 } while ((skb = skb->next));
1342
1343out:
1344 return segs;
1345}
1346
73cc19f1
HX
1347static struct sk_buff **inet_gro_receive(struct sk_buff **head,
1348 struct sk_buff *skb)
1349{
bca49f84 1350 const struct net_offload *ops;
73cc19f1
HX
1351 struct sk_buff **pp = NULL;
1352 struct sk_buff *p;
b71d1d42 1353 const struct iphdr *iph;
a5b1cf28
HX
1354 unsigned int hlen;
1355 unsigned int off;
1075f3f6 1356 unsigned int id;
73cc19f1
HX
1357 int flush = 1;
1358 int proto;
73cc19f1 1359
a5b1cf28
HX
1360 off = skb_gro_offset(skb);
1361 hlen = off + sizeof(*iph);
1362 iph = skb_gro_header_fast(skb, off);
1363 if (skb_gro_header_hard(skb, hlen)) {
1364 iph = skb_gro_header_slow(skb, hlen, off);
1365 if (unlikely(!iph))
1366 goto out;
1367 }
73cc19f1 1368
f9242b6b 1369 proto = iph->protocol;
73cc19f1
HX
1370
1371 rcu_read_lock();
bca49f84 1372 ops = rcu_dereference(inet_offloads[proto]);
f191a1d1 1373 if (!ops || !ops->callbacks.gro_receive)
73cc19f1
HX
1374 goto out_unlock;
1375
a5ad24be 1376 if (*(u8 *)iph != 0x45)
73cc19f1
HX
1377 goto out_unlock;
1378
a9e050f4 1379 if (unlikely(ip_fast_csum((u8 *)iph, 5)))
73cc19f1
HX
1380 goto out_unlock;
1381
0eae88f3
ED
1382 id = ntohl(*(__be32 *)&iph->id);
1383 flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id ^ IP_DF));
1075f3f6 1384 id >>= 16;
73cc19f1
HX
1385
1386 for (p = *head; p; p = p->next) {
1387 struct iphdr *iph2;
1388
1389 if (!NAPI_GRO_CB(p)->same_flow)
1390 continue;
1391
1392 iph2 = ip_hdr(p);
1393
a5ad24be 1394 if ((iph->protocol ^ iph2->protocol) |
0eae88f3
ED
1395 ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
1396 ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
73cc19f1
HX
1397 NAPI_GRO_CB(p)->same_flow = 0;
1398 continue;
1399 }
1400
1401 /* All fields must match except length and checksum. */
1402 NAPI_GRO_CB(p)->flush |=
a5ad24be 1403 (iph->ttl ^ iph2->ttl) |
a9e050f4 1404 (iph->tos ^ iph2->tos) |
a5ad24be 1405 ((u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) ^ id);
73cc19f1
HX
1406
1407 NAPI_GRO_CB(p)->flush |= flush;
1408 }
1409
1410 NAPI_GRO_CB(skb)->flush |= flush;
86911732
HX
1411 skb_gro_pull(skb, sizeof(*iph));
1412 skb_set_transport_header(skb, skb_gro_offset(skb));
73cc19f1 1413
f191a1d1 1414 pp = ops->callbacks.gro_receive(head, skb);
73cc19f1
HX
1415
1416out_unlock:
1417 rcu_read_unlock();
1418
1419out:
1420 NAPI_GRO_CB(skb)->flush |= flush;
1421
1422 return pp;
1423}
1424
1425static int inet_gro_complete(struct sk_buff *skb)
1426{
f9242b6b 1427 __be16 newlen = htons(skb->len - skb_network_offset(skb));
73cc19f1 1428 struct iphdr *iph = ip_hdr(skb);
bca49f84 1429 const struct net_offload *ops;
f9242b6b 1430 int proto = iph->protocol;
73cc19f1 1431 int err = -ENOSYS;
73cc19f1
HX
1432
1433 csum_replace2(&iph->check, iph->tot_len, newlen);
1434 iph->tot_len = newlen;
1435
1436 rcu_read_lock();
bca49f84 1437 ops = rcu_dereference(inet_offloads[proto]);
f191a1d1 1438 if (WARN_ON(!ops || !ops->callbacks.gro_complete))
73cc19f1
HX
1439 goto out_unlock;
1440
f191a1d1 1441 err = ops->callbacks.gro_complete(skb);
73cc19f1
HX
1442
1443out_unlock:
1444 rcu_read_unlock();
1445
1446 return err;
1447}
1448
eee4fe4d 1449int inet_ctl_sock_create(struct sock **sk, unsigned short family,
5677242f
DL
1450 unsigned short type, unsigned char protocol,
1451 struct net *net)
3d58b5fa 1452{
eee4fe4d
DL
1453 struct socket *sock;
1454 int rc = sock_create_kern(family, type, protocol, &sock);
3d58b5fa
DL
1455
1456 if (rc == 0) {
eee4fe4d
DL
1457 *sk = sock->sk;
1458 (*sk)->sk_allocation = GFP_ATOMIC;
3d58b5fa
DL
1459 /*
1460 * Unhash it so that IP input processing does not even see it,
1461 * we do not wish this socket to see incoming packets.
1462 */
eee4fe4d 1463 (*sk)->sk_prot->unhash(*sk);
5677242f
DL
1464
1465 sk_change_net(*sk, net);
3d58b5fa
DL
1466 }
1467 return rc;
1468}
3d58b5fa
DL
1469EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1470
7d720c3e 1471unsigned long snmp_fold_field(void __percpu *mib[], int offt)
5e0f0435
HX
1472{
1473 unsigned long res = 0;
8f0ea0fe 1474 int i, j;
5e0f0435
HX
1475
1476 for_each_possible_cpu(i) {
8f0ea0fe
ED
1477 for (j = 0; j < SNMP_ARRAY_SZ; j++)
1478 res += *(((unsigned long *) per_cpu_ptr(mib[j], i)) + offt);
5e0f0435
HX
1479 }
1480 return res;
1481}
1482EXPORT_SYMBOL_GPL(snmp_fold_field);
1483
4ce3c183
ED
1484#if BITS_PER_LONG==32
1485
1486u64 snmp_fold_field64(void __percpu *mib[], int offt, size_t syncp_offset)
1487{
1488 u64 res = 0;
1489 int cpu;
1490
1491 for_each_possible_cpu(cpu) {
8f0ea0fe 1492 void *bhptr;
4ce3c183 1493 struct u64_stats_sync *syncp;
8f0ea0fe 1494 u64 v;
4ce3c183
ED
1495 unsigned int start;
1496
8f0ea0fe 1497 bhptr = per_cpu_ptr(mib[0], cpu);
4ce3c183
ED
1498 syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
1499 do {
1500 start = u64_stats_fetch_begin_bh(syncp);
8f0ea0fe 1501 v = *(((u64 *) bhptr) + offt);
4ce3c183
ED
1502 } while (u64_stats_fetch_retry_bh(syncp, start));
1503
8f0ea0fe 1504 res += v;
4ce3c183
ED
1505 }
1506 return res;
1507}
1508EXPORT_SYMBOL_GPL(snmp_fold_field64);
1509#endif
1510
1823e4c8 1511int snmp_mib_init(void __percpu *ptr[2], size_t mibsize, size_t align)
5e0f0435
HX
1512{
1513 BUG_ON(ptr == NULL);
1823e4c8 1514 ptr[0] = __alloc_percpu(mibsize, align);
5e0f0435 1515 if (!ptr[0])
8f0ea0fe
ED
1516 return -ENOMEM;
1517#if SNMP_ARRAY_SZ == 2
1823e4c8 1518 ptr[1] = __alloc_percpu(mibsize, align);
8f0ea0fe
ED
1519 if (!ptr[1]) {
1520 free_percpu(ptr[0]);
1521 ptr[0] = NULL;
1522 return -ENOMEM;
1523 }
1524#endif
5e0f0435 1525 return 0;
5e0f0435
HX
1526}
1527EXPORT_SYMBOL_GPL(snmp_mib_init);
1528
8f0ea0fe 1529void snmp_mib_free(void __percpu *ptr[SNMP_ARRAY_SZ])
5e0f0435 1530{
8f0ea0fe
ED
1531 int i;
1532
5e0f0435 1533 BUG_ON(ptr == NULL);
8f0ea0fe
ED
1534 for (i = 0; i < SNMP_ARRAY_SZ; i++) {
1535 free_percpu(ptr[i]);
1536 ptr[i] = NULL;
1537 }
5e0f0435
HX
1538}
1539EXPORT_SYMBOL_GPL(snmp_mib_free);
1540
1da177e4 1541#ifdef CONFIG_IP_MULTICAST
32613090 1542static const struct net_protocol igmp_protocol = {
1da177e4 1543 .handler = igmp_rcv,
b4ee07df 1544 .netns_ok = 1,
1da177e4
LT
1545};
1546#endif
1547
32613090 1548static const struct net_protocol tcp_protocol = {
41063e9d
DM
1549 .early_demux = tcp_v4_early_demux,
1550 .handler = tcp_v4_rcv,
1551 .err_handler = tcp_v4_err,
41063e9d
DM
1552 .no_policy = 1,
1553 .netns_ok = 1,
1da177e4
LT
1554};
1555
de27d001 1556static const struct net_offload tcp_offload = {
f191a1d1
VY
1557 .callbacks = {
1558 .gso_send_check = tcp_v4_gso_send_check,
1559 .gso_segment = tcp_tso_segment,
1560 .gro_receive = tcp4_gro_receive,
1561 .gro_complete = tcp4_gro_complete,
1562 },
de27d001
VY
1563};
1564
32613090 1565static const struct net_protocol udp_protocol = {
1da177e4
LT
1566 .handler = udp_rcv,
1567 .err_handler = udp_err,
1568 .no_policy = 1,
92f1fecb 1569 .netns_ok = 1,
1da177e4
LT
1570};
1571
de27d001 1572static const struct net_offload udp_offload = {
f191a1d1
VY
1573 .callbacks = {
1574 .gso_send_check = udp4_ufo_send_check,
1575 .gso_segment = udp4_ufo_fragment,
1576 },
de27d001
VY
1577};
1578
32613090 1579static const struct net_protocol icmp_protocol = {
1da177e4 1580 .handler = icmp_rcv,
5b052042 1581 .err_handler = icmp_err,
8b7817f3 1582 .no_policy = 1,
92f1fecb 1583 .netns_ok = 1,
1da177e4
LT
1584};
1585
9b4661bd
PE
1586static __net_init int ipv4_mib_init_net(struct net *net)
1587{
7d720c3e 1588 if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics,
1823e4c8
ED
1589 sizeof(struct tcp_mib),
1590 __alignof__(struct tcp_mib)) < 0)
57ef42d5 1591 goto err_tcp_mib;
7d720c3e 1592 if (snmp_mib_init((void __percpu **)net->mib.ip_statistics,
1823e4c8
ED
1593 sizeof(struct ipstats_mib),
1594 __alignof__(struct ipstats_mib)) < 0)
a20f5799 1595 goto err_ip_mib;
7d720c3e 1596 if (snmp_mib_init((void __percpu **)net->mib.net_statistics,
1823e4c8
ED
1597 sizeof(struct linux_mib),
1598 __alignof__(struct linux_mib)) < 0)
61a7e260 1599 goto err_net_mib;
7d720c3e 1600 if (snmp_mib_init((void __percpu **)net->mib.udp_statistics,
1823e4c8
ED
1601 sizeof(struct udp_mib),
1602 __alignof__(struct udp_mib)) < 0)
2f275f91 1603 goto err_udp_mib;
7d720c3e 1604 if (snmp_mib_init((void __percpu **)net->mib.udplite_statistics,
1823e4c8
ED
1605 sizeof(struct udp_mib),
1606 __alignof__(struct udp_mib)) < 0)
386019d3 1607 goto err_udplite_mib;
7d720c3e 1608 if (snmp_mib_init((void __percpu **)net->mib.icmp_statistics,
1823e4c8
ED
1609 sizeof(struct icmp_mib),
1610 __alignof__(struct icmp_mib)) < 0)
b60538a0 1611 goto err_icmp_mib;
acb32ba3
ED
1612 net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib),
1613 GFP_KERNEL);
1614 if (!net->mib.icmpmsg_statistics)
923c6586 1615 goto err_icmpmsg_mib;
57ef42d5
PE
1616
1617 tcp_mib_init(net);
9b4661bd 1618 return 0;
57ef42d5 1619
923c6586 1620err_icmpmsg_mib:
7d720c3e 1621 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
b60538a0 1622err_icmp_mib:
7d720c3e 1623 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
386019d3 1624err_udplite_mib:
7d720c3e 1625 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
2f275f91 1626err_udp_mib:
7d720c3e 1627 snmp_mib_free((void __percpu **)net->mib.net_statistics);
61a7e260 1628err_net_mib:
7d720c3e 1629 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
a20f5799 1630err_ip_mib:
7d720c3e 1631 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
57ef42d5
PE
1632err_tcp_mib:
1633 return -ENOMEM;
9b4661bd
PE
1634}
1635
1636static __net_exit void ipv4_mib_exit_net(struct net *net)
1637{
acb32ba3 1638 kfree(net->mib.icmpmsg_statistics);
7d720c3e
TH
1639 snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
1640 snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
1641 snmp_mib_free((void __percpu **)net->mib.udp_statistics);
1642 snmp_mib_free((void __percpu **)net->mib.net_statistics);
1643 snmp_mib_free((void __percpu **)net->mib.ip_statistics);
1644 snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
9b4661bd
PE
1645}
1646
1647static __net_initdata struct pernet_operations ipv4_mib_ops = {
1648 .init = ipv4_mib_init_net,
1649 .exit = ipv4_mib_exit_net,
1650};
1651
1da177e4
LT
1652static int __init init_ipv4_mibs(void)
1653{
d89cbbb1 1654 return register_pernet_subsys(&ipv4_mib_ops);
1da177e4
LT
1655}
1656
1657static int ipv4_proc_init(void);
1da177e4 1658
30e224d7
HX
1659/*
1660 * IP protocol layer initialiser
1661 */
1662
22061d80
VY
1663static struct packet_offload ip_packet_offload __read_mostly = {
1664 .type = cpu_to_be16(ETH_P_IP),
f191a1d1
VY
1665 .callbacks = {
1666 .gso_send_check = inet_gso_send_check,
1667 .gso_segment = inet_gso_segment,
1668 .gro_receive = inet_gro_receive,
1669 .gro_complete = inet_gro_complete,
1670 },
30e224d7
HX
1671};
1672
808a8f88
VY
1673static int __init ipv4_offload_init(void)
1674{
1675 /*
1676 * Add offloads
1677 */
1678 if (inet_add_offload(&udp_offload, IPPROTO_UDP) < 0)
1679 pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
1680 if (inet_add_offload(&tcp_offload, IPPROTO_TCP) < 0)
1681 pr_crit("%s: Cannot add TCP protocol offlaod\n", __func__);
1682
1683 dev_add_offload(&ip_packet_offload);
1684 return 0;
1685}
1686
1687fs_initcall(ipv4_offload_init);
1688
1689static struct packet_type ip_packet_type __read_mostly = {
1690 .type = cpu_to_be16(ETH_P_IP),
1691 .func = ip_rcv,
1692};
1693
1da177e4
LT
1694static int __init inet_init(void)
1695{
1da177e4
LT
1696 struct inet_protosw *q;
1697 struct list_head *r;
1698 int rc = -EINVAL;
1699
95c7e0e4 1700 BUILD_BUG_ON(sizeof(struct inet_skb_parm) > FIELD_SIZEOF(struct sk_buff, cb));
1da177e4 1701
e3826f1e
AW
1702 sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
1703 if (!sysctl_local_reserved_ports)
1704 goto out;
1705
1da177e4
LT
1706 rc = proto_register(&tcp_prot, 1);
1707 if (rc)
e3826f1e 1708 goto out_free_reserved_ports;
1da177e4
LT
1709
1710 rc = proto_register(&udp_prot, 1);
1711 if (rc)
1712 goto out_unregister_tcp_proto;
1713
1714 rc = proto_register(&raw_prot, 1);
1715 if (rc)
1716 goto out_unregister_udp_proto;
1717
c319b4d7
VK
1718 rc = proto_register(&ping_prot, 1);
1719 if (rc)
1720 goto out_unregister_raw_proto;
1721
1da177e4 1722 /*
e905a9ed 1723 * Tell SOCKET that we are alive...
1da177e4
LT
1724 */
1725
e905a9ed 1726 (void)sock_register(&inet_family_ops);
1da177e4 1727
bd7b1533
AV
1728#ifdef CONFIG_SYSCTL
1729 ip_static_sysctl_init();
1730#endif
1731
3dc43e3e
GC
1732 tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
1733
1da177e4
LT
1734 /*
1735 * Add all the base protocols.
1736 */
1737
1738 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
058bd4d2 1739 pr_crit("%s: Cannot add ICMP protocol\n", __func__);
1da177e4 1740 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
058bd4d2 1741 pr_crit("%s: Cannot add UDP protocol\n", __func__);
1da177e4 1742 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
058bd4d2 1743 pr_crit("%s: Cannot add TCP protocol\n", __func__);
1da177e4
LT
1744#ifdef CONFIG_IP_MULTICAST
1745 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
058bd4d2 1746 pr_crit("%s: Cannot add IGMP protocol\n", __func__);
1da177e4
LT
1747#endif
1748
1749 /* Register the socket-side information for inet_create. */
1750 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1751 INIT_LIST_HEAD(r);
1752
1753 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1754 inet_register_protosw(q);
1755
1756 /*
1757 * Set the ARP module up
1758 */
1759
1760 arp_init();
1761
e905a9ed
YH
1762 /*
1763 * Set the IP module up
1764 */
1da177e4
LT
1765
1766 ip_init();
1767
9b0f976f 1768 tcp_v4_init();
1da177e4
LT
1769
1770 /* Setup TCP slab cache for open requests. */
1771 tcp_init();
1772
95766fff
HA
1773 /* Setup UDP memory threshold */
1774 udp_init();
1775
ba4e58ec
GR
1776 /* Add UDP-Lite (RFC 3828) */
1777 udplite4_register();
1da177e4 1778
c319b4d7
VK
1779 ping_init();
1780
1da177e4
LT
1781 /*
1782 * Set the ICMP layer up
1783 */
1784
a5710d65
DL
1785 if (icmp_init() < 0)
1786 panic("Failed to create the ICMP control socket.\n");
1da177e4
LT
1787
1788 /*
1789 * Initialise the multicast router
1790 */
1791#if defined(CONFIG_IP_MROUTE)
03d2f897 1792 if (ip_mr_init())
058bd4d2 1793 pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
1da177e4
LT
1794#endif
1795 /*
1796 * Initialise per-cpu ipv4 mibs
e905a9ed 1797 */
1da177e4 1798
132adf54 1799 if (init_ipv4_mibs())
058bd4d2 1800 pr_crit("%s: Cannot init ipv4 mibs\n", __func__);
e905a9ed 1801
1da177e4
LT
1802 ipv4_proc_init();
1803
1804 ipfrag_init();
1805
30e224d7
HX
1806 dev_add_pack(&ip_packet_type);
1807
1da177e4
LT
1808 rc = 0;
1809out:
1810 return rc;
c319b4d7
VK
1811out_unregister_raw_proto:
1812 proto_unregister(&raw_prot);
1da177e4
LT
1813out_unregister_udp_proto:
1814 proto_unregister(&udp_prot);
321efff7
OK
1815out_unregister_tcp_proto:
1816 proto_unregister(&tcp_prot);
e3826f1e
AW
1817out_free_reserved_ports:
1818 kfree(sysctl_local_reserved_ports);
1da177e4
LT
1819 goto out;
1820}
1821
a536e077 1822fs_initcall(inet_init);
1da177e4
LT
1823
1824/* ------------------------------------------------------------------------ */
1825
1826#ifdef CONFIG_PROC_FS
1da177e4
LT
1827static int __init ipv4_proc_init(void)
1828{
1829 int rc = 0;
1830
1831 if (raw_proc_init())
1832 goto out_raw;
1833 if (tcp4_proc_init())
1834 goto out_tcp;
1835 if (udp4_proc_init())
1836 goto out_udp;
c319b4d7
VK
1837 if (ping_proc_init())
1838 goto out_ping;
1da177e4
LT
1839 if (ip_misc_proc_init())
1840 goto out_misc;
1841out:
1842 return rc;
1843out_misc:
c319b4d7
VK
1844 ping_proc_exit();
1845out_ping:
1da177e4
LT
1846 udp4_proc_exit();
1847out_udp:
1848 tcp4_proc_exit();
1849out_tcp:
1850 raw_proc_exit();
1851out_raw:
1852 rc = -ENOMEM;
1853 goto out;
1854}
1855
1856#else /* CONFIG_PROC_FS */
1857static int __init ipv4_proc_init(void)
1858{
1859 return 0;
1860}
1861#endif /* CONFIG_PROC_FS */
1862
1863MODULE_ALIAS_NETPROTO(PF_INET);
1864