xref: /linux/net/ipv4/af_inet.c (revision 55fbedcec622a943cb09a969aef1ddcba87ee40a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		PF_INET protocol family socket handler.
8  *
9  * Authors:	Ross Biro
10  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11  *		Florian La Roche, <flla@stud.uni-sb.de>
12  *		Alan Cox, <A.Cox@swansea.ac.uk>
13  *
14  * Changes (see also sock.c)
15  *
16  *		piggy,
17  *		Karl Knutson	:	Socket protocol table
18  *		A.N.Kuznetsov	:	Socket death error in accept().
19  *		John Richardson :	Fix non blocking error in connect()
20  *					so sockets that fail to connect
21  *					don't return -EINPROGRESS.
22  *		Alan Cox	:	Asynchronous I/O support
23  *		Alan Cox	:	Keep correct socket pointer on sock
24  *					structures
25  *					when accept() ed
26  *		Alan Cox	:	Semantics of SO_LINGER aren't state
27  *					moved to close when you look carefully.
28  *					With this fixed and the accept bug fixed
29  *					some RPC stuff seems happier.
30  *		Niibe Yutaka	:	4.4BSD style write async I/O
31  *		Alan Cox,
32  *		Tony Gale 	:	Fixed reuse semantics.
33  *		Alan Cox	:	bind() shouldn't abort existing but dead
34  *					sockets. Stops FTP netin:.. I hope.
35  *		Alan Cox	:	bind() works correctly for RAW sockets.
36  *					Note that FreeBSD at least was broken
37  *					in this respect so be careful with
38  *					compatibility tests...
39  *		Alan Cox	:	routing cache support
40  *		Alan Cox	:	memzero the socket structure for
41  *					compactness.
42  *		Matt Day	:	nonblock connect error handler
43  *		Alan Cox	:	Allow large numbers of pending sockets
44  *					(eg for big web sites), but only if
45  *					specifically application requested.
46  *		Alan Cox	:	New buffering throughout IP. Used
47  *					dumbly.
48  *		Alan Cox	:	New buffering now used smartly.
49  *		Alan Cox	:	BSD rather than common sense
50  *					interpretation of listen.
51  *		Germano Caronni	:	Assorted small races.
52  *		Alan Cox	:	sendmsg/recvmsg basic support.
53  *		Alan Cox	:	Only sendmsg/recvmsg now supported.
54  *		Alan Cox	:	Locked down bind (see security list).
55  *		Alan Cox	:	Loosened bind a little.
56  *		Mike McLagan	:	ADD/DEL DLCI Ioctls
57  *	Willy Konynenberg	:	Transparent proxying support.
58  *		David S. Miller	:	New socket lookup architecture.
59  *					Some other random speedups.
60  *		Cyrus Durgin	:	Cleaned up file for kmod hacks.
61  *		Andi Kleen	:	Fix inet_stream_connect TCP race.
62  */
63 
64 #define pr_fmt(fmt) "IPv4: " fmt
65 
66 #include <linux/err.h>
67 #include <linux/errno.h>
68 #include <linux/types.h>
69 #include <linux/socket.h>
70 #include <linux/in.h>
71 #include <linux/kernel.h>
72 #include <linux/kmod.h>
73 #include <linux/sched.h>
74 #include <linux/timer.h>
75 #include <linux/string.h>
76 #include <linux/sockios.h>
77 #include <linux/net.h>
78 #include <linux/capability.h>
79 #include <linux/fcntl.h>
80 #include <linux/mm.h>
81 #include <linux/interrupt.h>
82 #include <linux/stat.h>
83 #include <linux/init.h>
84 #include <linux/poll.h>
85 #include <linux/netfilter_ipv4.h>
86 #include <linux/random.h>
87 #include <linux/slab.h>
88 
89 #include <linux/uaccess.h>
90 
91 #include <linux/inet.h>
92 #include <linux/igmp.h>
93 #include <linux/inetdevice.h>
94 #include <linux/netdevice.h>
95 #include <net/checksum.h>
96 #include <net/ip.h>
97 #include <net/protocol.h>
98 #include <net/arp.h>
99 #include <net/route.h>
100 #include <net/ip_fib.h>
101 #include <net/inet_connection_sock.h>
102 #include <net/gro.h>
103 #include <net/gso.h>
104 #include <net/tcp.h>
105 #include <net/psp.h>
106 #include <net/udp.h>
107 #include <net/ping.h>
108 #include <linux/skbuff.h>
109 #include <net/sock.h>
110 #include <net/raw.h>
111 #include <net/icmp.h>
112 #include <net/inet_common.h>
113 #include <net/ip_tunnels.h>
114 #include <net/xfrm.h>
115 #include <net/net_namespace.h>
116 #include <net/secure_seq.h>
117 #ifdef CONFIG_IP_MROUTE
118 #include <linux/mroute.h>
119 #endif
120 #include <net/l3mdev.h>
121 #include <net/compat.h>
122 #include <net/rps.h>
123 
124 #include <trace/events/sock.h>
125 
126 /* Keep the definition of IPv6 disable here for now, to avoid annoying linker
127  * issues in case IPv6=m
128  */
129 int disable_ipv6_mod;
130 EXPORT_SYMBOL(disable_ipv6_mod);
131 
132 /* The inetsw table contains everything that inet_create needs to
133  * build a new socket.
134  */
135 static struct list_head inetsw[SOCK_MAX];
136 static DEFINE_SPINLOCK(inetsw_lock);
137 
138 /* New destruction routine */
139 
140 void inet_sock_destruct(struct sock *sk)
141 {
142 	struct inet_sock *inet = inet_sk(sk);
143 
144 	__skb_queue_purge(&sk->sk_receive_queue);
145 	__skb_queue_purge(&sk->sk_error_queue);
146 
147 	sk_mem_reclaim_final(sk);
148 
149 	if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
150 		pr_err("Attempt to release TCP socket in state %d %p\n",
151 		       sk->sk_state, sk);
152 		return;
153 	}
154 	if (!sock_flag(sk, SOCK_DEAD)) {
155 		pr_err("Attempt to release alive inet socket %p\n", sk);
156 		return;
157 	}
158 
159 	WARN_ON_ONCE(atomic_read(&sk->sk_rmem_alloc));
160 	WARN_ON_ONCE(refcount_read(&sk->sk_wmem_alloc));
161 	WARN_ON_ONCE(sk->sk_wmem_queued);
162 	WARN_ON_ONCE(sk->sk_forward_alloc);
163 
164 	kfree_rcu(rcu_dereference_protected(inet->inet_opt, 1), rcu);
165 	dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1));
166 	dst_release(rcu_dereference_protected(sk->sk_rx_dst, 1));
167 	psp_sk_assoc_free(sk);
168 }
169 EXPORT_SYMBOL(inet_sock_destruct);
170 
171 /*
172  *	The routines beyond this point handle the behaviour of an AF_INET
173  *	socket object. Mostly it punts to the subprotocols of IP to do
174  *	the work.
175  */
176 
177 /*
178  *	Automatically bind an unbound socket.
179  */
180 
181 static int inet_autobind(struct sock *sk)
182 {
183 	struct inet_sock *inet;
184 	/* We may need to bind the socket. */
185 	lock_sock(sk);
186 	inet = inet_sk(sk);
187 	if (!inet->inet_num) {
188 		if (sk->sk_prot->get_port(sk, 0)) {
189 			release_sock(sk);
190 			return -EAGAIN;
191 		}
192 		inet->inet_sport = htons(inet->inet_num);
193 	}
194 	release_sock(sk);
195 	return 0;
196 }
197 
198 int __inet_listen_sk(struct sock *sk, int backlog)
199 {
200 	unsigned char old_state = sk->sk_state;
201 	int err, tcp_fastopen;
202 
203 	if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
204 		return -EINVAL;
205 
206 	WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
207 	/* Really, if the socket is already in listen state
208 	 * we can only allow the backlog to be adjusted.
209 	 */
210 	if (old_state != TCP_LISTEN) {
211 		/* Enable TFO w/o requiring TCP_FASTOPEN socket option.
212 		 * Note that only TCP sockets (SOCK_STREAM) will reach here.
213 		 * Also fastopen backlog may already been set via the option
214 		 * because the socket was in TCP_LISTEN state previously but
215 		 * was shutdown() rather than close().
216 		 */
217 		tcp_fastopen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fastopen);
218 		if ((tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
219 		    (tcp_fastopen & TFO_SERVER_ENABLE) &&
220 		    !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
221 			fastopen_queue_tune(sk, backlog);
222 			tcp_fastopen_init_key_once(sock_net(sk));
223 		}
224 
225 		err = inet_csk_listen_start(sk);
226 		if (err)
227 			return err;
228 
229 		tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB);
230 		bpf_tcp_ops_call(listen, sk);
231 	}
232 	return 0;
233 }
234 
235 /*
236  *	Move a socket into listening state.
237  */
238 int inet_listen(struct socket *sock, int backlog)
239 {
240 	struct sock *sk = sock->sk;
241 	int err = -EINVAL;
242 
243 	lock_sock(sk);
244 
245 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
246 		goto out;
247 
248 	err = __inet_listen_sk(sk, backlog);
249 
250 out:
251 	release_sock(sk);
252 	return err;
253 }
254 EXPORT_SYMBOL(inet_listen);
255 
256 /*
257  *	Create an inet socket.
258  */
259 
260 static int inet_create(struct net *net, struct socket *sock, int protocol,
261 		       int kern)
262 {
263 	struct sock *sk;
264 	struct inet_protosw *answer;
265 	struct inet_sock *inet;
266 	struct proto *answer_prot;
267 	unsigned char answer_flags;
268 	int try_loading_module = 0;
269 	int err;
270 
271 	if (protocol < 0 || protocol >= IPPROTO_MAX)
272 		return -EINVAL;
273 
274 	sock->state = SS_UNCONNECTED;
275 
276 	/* Look for the requested type/protocol pair. */
277 lookup_protocol:
278 	err = -ESOCKTNOSUPPORT;
279 	rcu_read_lock();
280 	list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
281 
282 		err = 0;
283 		/* Check the non-wild match. */
284 		if (protocol == answer->protocol) {
285 			if (protocol != IPPROTO_IP)
286 				break;
287 		} else {
288 			/* Check for the two wild cases. */
289 			if (IPPROTO_IP == protocol) {
290 				protocol = answer->protocol;
291 				break;
292 			}
293 			if (IPPROTO_IP == answer->protocol)
294 				break;
295 		}
296 		err = -EPROTONOSUPPORT;
297 	}
298 
299 	if (unlikely(err)) {
300 		if (try_loading_module < 2) {
301 			rcu_read_unlock();
302 			/*
303 			 * Be more specific, e.g. net-pf-2-proto-132-type-1
304 			 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
305 			 */
306 			if (++try_loading_module == 1)
307 				request_module("net-pf-%d-proto-%d-type-%d",
308 					       PF_INET, protocol, sock->type);
309 			/*
310 			 * Fall back to generic, e.g. net-pf-2-proto-132
311 			 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
312 			 */
313 			else
314 				request_module("net-pf-%d-proto-%d",
315 					       PF_INET, protocol);
316 			goto lookup_protocol;
317 		} else
318 			goto out_rcu_unlock;
319 	}
320 
321 	err = -EPERM;
322 	if (sock->type == SOCK_RAW && !kern &&
323 	    !ns_capable(net->user_ns, CAP_NET_RAW))
324 		goto out_rcu_unlock;
325 
326 	sock->ops = answer->ops;
327 	answer_prot = answer->prot;
328 	answer_flags = answer->flags;
329 	rcu_read_unlock();
330 
331 	WARN_ON(!answer_prot->slab);
332 
333 	err = -ENOMEM;
334 	sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot, kern);
335 	if (!sk)
336 		goto out;
337 
338 	err = 0;
339 	if (INET_PROTOSW_REUSE & answer_flags)
340 		sk->sk_reuse = SK_CAN_REUSE;
341 
342 	if (INET_PROTOSW_ICSK & answer_flags)
343 		inet_init_csk_locks(sk);
344 
345 	inet = inet_sk(sk);
346 	inet_assign_bit(IS_ICSK, sk, INET_PROTOSW_ICSK & answer_flags);
347 
348 	inet_clear_bit(NODEFRAG, sk);
349 
350 	if (SOCK_RAW == sock->type) {
351 		inet->inet_num = protocol;
352 		if (IPPROTO_RAW == protocol)
353 			inet_set_bit(HDRINCL, sk);
354 	}
355 
356 	if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc))
357 		inet->pmtudisc = IP_PMTUDISC_DONT;
358 	else
359 		inet->pmtudisc = IP_PMTUDISC_WANT;
360 
361 	atomic_set(&inet->inet_id, 0);
362 
363 	sock_init_data(sock, sk);
364 
365 	sk->sk_destruct	   = inet_sock_destruct;
366 	sk->sk_protocol	   = protocol;
367 	sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
368 	sk->sk_txrehash = READ_ONCE(net->core.sysctl_txrehash);
369 
370 	inet->uc_ttl	= -1;
371 	inet_set_bit(MC_LOOP, sk);
372 	inet->mc_ttl	= 1;
373 	inet_set_bit(MC_ALL, sk);
374 	inet->mc_index	= 0;
375 	inet->mc_list	= NULL;
376 	inet->rcv_tos	= 0;
377 
378 	if (inet->inet_num) {
379 		/* It assumes that any protocol which allows
380 		 * the user to assign a number at socket
381 		 * creation time automatically
382 		 * shares.
383 		 */
384 		inet->inet_sport = htons(inet->inet_num);
385 		/* Add to protocol hash chains. */
386 		err = sk->sk_prot->hash(sk);
387 		if (err)
388 			goto out_sk_release;
389 	}
390 
391 	if (sk->sk_prot->init) {
392 		err = sk->sk_prot->init(sk);
393 		if (err)
394 			goto out_sk_release;
395 	}
396 
397 	if (!kern) {
398 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
399 		if (err)
400 			goto out_sk_release;
401 	}
402 out:
403 	return err;
404 out_rcu_unlock:
405 	rcu_read_unlock();
406 	goto out;
407 out_sk_release:
408 	sk_common_release(sk);
409 	sock->sk = NULL;
410 	goto out;
411 }
412 
413 
414 /*
415  *	The peer socket should always be NULL (or else). When we call this
416  *	function we are destroying the object and from then on nobody
417  *	should refer to it.
418  */
419 int inet_release(struct socket *sock)
420 {
421 	struct sock *sk = sock->sk;
422 
423 	if (sk) {
424 		long timeout;
425 
426 		if (!sk->sk_kern_sock)
427 			BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk);
428 
429 		/* Applications forget to leave groups before exiting */
430 		ip_mc_drop_socket(sk);
431 
432 		/* If linger is set, we don't return until the close
433 		 * is complete.  Otherwise we return immediately. The
434 		 * actually closing is done the same either way.
435 		 *
436 		 * If the close is due to the process exiting, we never
437 		 * linger..
438 		 */
439 		timeout = 0;
440 		if (sock_flag(sk, SOCK_LINGER) &&
441 		    !(current->flags & PF_EXITING))
442 			timeout = sk->sk_lingertime;
443 		sk->sk_prot->close(sk, timeout);
444 		sock->sk = NULL;
445 	}
446 	return 0;
447 }
448 EXPORT_SYMBOL(inet_release);
449 
450 int inet_bind_sk(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
451 {
452 	u32 flags = BIND_WITH_LOCK;
453 	int err;
454 
455 	/* If the socket has its own bind function then use it. (RAW) */
456 	if (sk->sk_prot->bind) {
457 		return sk->sk_prot->bind(sk, uaddr, addr_len);
458 	}
459 	if (addr_len < sizeof(struct sockaddr_in))
460 		return -EINVAL;
461 
462 	/* BPF prog is run before any checks are done so that if the prog
463 	 * changes context in a wrong way it will be caught.
464 	 */
465 	err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, &addr_len,
466 						 CGROUP_INET4_BIND, &flags);
467 	if (err)
468 		return err;
469 
470 	return __inet_bind(sk, uaddr, addr_len, flags);
471 }
472 
473 int inet_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
474 {
475 	return inet_bind_sk(sock->sk, uaddr, addr_len);
476 }
477 EXPORT_SYMBOL(inet_bind);
478 
479 int __inet_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
480 		u32 flags)
481 {
482 	struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
483 	struct inet_sock *inet = inet_sk(sk);
484 	struct net *net = sock_net(sk);
485 	unsigned short snum;
486 	int chk_addr_ret;
487 	u32 tb_id = RT_TABLE_LOCAL;
488 	int err;
489 
490 	if (addr->sin_family != AF_INET) {
491 		/* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
492 		 * only if s_addr is INADDR_ANY.
493 		 */
494 		err = -EAFNOSUPPORT;
495 		if (addr->sin_family != AF_UNSPEC ||
496 		    addr->sin_addr.s_addr != htonl(INADDR_ANY))
497 			goto out;
498 	}
499 
500 	tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id;
501 	chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
502 
503 	/* Not specified by any standard per-se, however it breaks too
504 	 * many applications when removed.  It is unfortunate since
505 	 * allowing applications to make a non-local bind solves
506 	 * several problems with systems using dynamic addressing.
507 	 * (ie. your servers still start up even if your ISDN link
508 	 *  is temporarily down)
509 	 */
510 	err = -EADDRNOTAVAIL;
511 	if (!inet_addr_valid_or_nonlocal(net, inet, addr->sin_addr.s_addr,
512 	                                 chk_addr_ret))
513 		goto out;
514 
515 	snum = ntohs(addr->sin_port);
516 	err = -EACCES;
517 	if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) &&
518 	    snum && inet_port_requires_bind_service(net, snum) &&
519 	    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
520 		goto out;
521 
522 	/*      We keep a pair of addresses. rcv_saddr is the one
523 	 *      used by hash lookups, and saddr is used for transmit.
524 	 *
525 	 *      In the BSD API these are the same except where it
526 	 *      would be illegal to use them (multicast/broadcast) in
527 	 *      which case the sending device address is used.
528 	 */
529 	if (flags & BIND_WITH_LOCK)
530 		lock_sock(sk);
531 
532 	/* Check these errors (active socket, double bind). */
533 	err = -EINVAL;
534 	if (sk->sk_state != TCP_CLOSE || inet->inet_num)
535 		goto out_release_sock;
536 
537 	inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
538 	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
539 		inet->inet_saddr = 0;  /* Use device */
540 
541 	/* Make sure we are allowed to bind here. */
542 	if (snum || !(inet_test_bit(BIND_ADDRESS_NO_PORT, sk) ||
543 		      (flags & BIND_FORCE_ADDRESS_NO_PORT))) {
544 		err = sk->sk_prot->get_port(sk, snum);
545 		if (err) {
546 			inet->inet_saddr = inet->inet_rcv_saddr = 0;
547 			goto out_release_sock;
548 		}
549 		if (!(flags & BIND_FROM_BPF)) {
550 			err = BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk);
551 			if (err) {
552 				inet->inet_saddr = inet->inet_rcv_saddr = 0;
553 				if (sk->sk_prot->put_port)
554 					sk->sk_prot->put_port(sk);
555 				goto out_release_sock;
556 			}
557 		}
558 	}
559 
560 	if (inet->inet_rcv_saddr)
561 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
562 	if (snum)
563 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
564 	inet->inet_sport = htons(inet->inet_num);
565 	inet->inet_daddr = 0;
566 	inet->inet_dport = 0;
567 	sk_dst_reset(sk);
568 	err = 0;
569 out_release_sock:
570 	if (flags & BIND_WITH_LOCK)
571 		release_sock(sk);
572 out:
573 	return err;
574 }
575 
576 int inet_dgram_connect(struct socket *sock, struct sockaddr_unsized *uaddr,
577 		       int addr_len, int flags)
578 {
579 	struct sock *sk = sock->sk;
580 	const struct proto *prot;
581 	int err;
582 
583 	if (addr_len < sizeof(uaddr->sa_family))
584 		return -EINVAL;
585 
586 	prot = READ_ONCE(sk->sk_prot);
587 
588 	if (uaddr->sa_family == AF_UNSPEC)
589 		return prot->disconnect(sk, flags);
590 
591 	if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) {
592 		err = prot->pre_connect(sk, uaddr, addr_len);
593 		if (err)
594 			return err;
595 	}
596 
597 	if (data_race(!inet_sk(sk)->inet_num) && inet_autobind(sk))
598 		return -EAGAIN;
599 	return prot->connect(sk, uaddr, addr_len);
600 }
601 EXPORT_SYMBOL(inet_dgram_connect);
602 
603 static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias)
604 {
605 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
606 
607 	add_wait_queue(sk_sleep(sk), &wait);
608 	sk->sk_write_pending += writebias;
609 
610 	/* Basic assumption: if someone sets sk->sk_err, he _must_
611 	 * change state of the socket from TCP_SYN_*.
612 	 * Connect() does not allow to get error notifications
613 	 * without closing the socket.
614 	 */
615 	while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
616 		release_sock(sk);
617 		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
618 		lock_sock(sk);
619 		if (signal_pending(current) || !timeo)
620 			break;
621 	}
622 	remove_wait_queue(sk_sleep(sk), &wait);
623 	sk->sk_write_pending -= writebias;
624 	return timeo;
625 }
626 
627 /*
628  *	Connect to a remote host. There is regrettably still a little
629  *	TCP 'magic' in here.
630  */
631 int __inet_stream_connect(struct socket *sock, struct sockaddr_unsized *uaddr,
632 			  int addr_len, int flags, int is_sendmsg)
633 {
634 	struct sock *sk = sock->sk;
635 	int err;
636 	long timeo;
637 
638 	/*
639 	 * uaddr can be NULL and addr_len can be 0 if:
640 	 * sk is a TCP fastopen active socket and
641 	 * TCP_FASTOPEN_CONNECT sockopt is set and
642 	 * we already have a valid cookie for this socket.
643 	 * In this case, user can call write() after connect().
644 	 * write() will invoke tcp_sendmsg_fastopen() which calls
645 	 * __inet_stream_connect().
646 	 */
647 	if (uaddr) {
648 		if (addr_len < sizeof(uaddr->sa_family))
649 			return -EINVAL;
650 
651 		if (uaddr->sa_family == AF_UNSPEC) {
652 			sk->sk_disconnects++;
653 			err = sk->sk_prot->disconnect(sk, flags);
654 			sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
655 			goto out;
656 		}
657 	}
658 
659 	switch (sock->state) {
660 	default:
661 		err = -EINVAL;
662 		goto out;
663 	case SS_CONNECTED:
664 		err = -EISCONN;
665 		goto out;
666 	case SS_CONNECTING:
667 		if (inet_test_bit(DEFER_CONNECT, sk))
668 			err = is_sendmsg ? -EINPROGRESS : -EISCONN;
669 		else
670 			err = -EALREADY;
671 		/* Fall out of switch with err, set for this state */
672 		break;
673 	case SS_UNCONNECTED:
674 		err = -EISCONN;
675 		if (sk->sk_state != TCP_CLOSE)
676 			goto out;
677 
678 		if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) {
679 			err = sk->sk_prot->pre_connect(sk, uaddr, addr_len);
680 			if (err)
681 				goto out;
682 		}
683 
684 		err = sk->sk_prot->connect(sk, uaddr, addr_len);
685 		if (err < 0)
686 			goto out;
687 
688 		sock->state = SS_CONNECTING;
689 
690 		if (!err && inet_test_bit(DEFER_CONNECT, sk))
691 			goto out;
692 
693 		/* Just entered SS_CONNECTING state; the only
694 		 * difference is that return value in non-blocking
695 		 * case is EINPROGRESS, rather than EALREADY.
696 		 */
697 		err = -EINPROGRESS;
698 		break;
699 	}
700 
701 	timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
702 
703 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
704 		int writebias = (sk->sk_protocol == IPPROTO_TCP) &&
705 				tcp_sk(sk)->fastopen_req &&
706 				tcp_sk(sk)->fastopen_req->data ? 1 : 0;
707 		int dis = sk->sk_disconnects;
708 
709 		/* Error code is set above */
710 		if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
711 			goto out;
712 
713 		err = sock_intr_errno(timeo);
714 		if (signal_pending(current))
715 			goto out;
716 
717 		if (dis != sk->sk_disconnects) {
718 			err = -EPIPE;
719 			goto out;
720 		}
721 	}
722 
723 	/* Connection was closed by RST, timeout, ICMP error
724 	 * or another process disconnected us.
725 	 */
726 	if (sk->sk_state == TCP_CLOSE)
727 		goto sock_error;
728 
729 	/* sk->sk_err may be not zero now, if RECVERR was ordered by user
730 	 * and error was received after socket entered established state.
731 	 * Hence, it is handled normally after connect() return successfully.
732 	 */
733 
734 	sock->state = SS_CONNECTED;
735 	err = 0;
736 out:
737 	return err;
738 
739 sock_error:
740 	err = sock_error(sk) ? : -ECONNABORTED;
741 	sock->state = SS_UNCONNECTED;
742 	sk->sk_disconnects++;
743 	if (sk->sk_prot->disconnect(sk, flags))
744 		sock->state = SS_DISCONNECTING;
745 	goto out;
746 }
747 EXPORT_SYMBOL(__inet_stream_connect);
748 
749 int inet_stream_connect(struct socket *sock, struct sockaddr_unsized *uaddr,
750 			int addr_len, int flags)
751 {
752 	int err;
753 
754 	lock_sock(sock->sk);
755 	err = __inet_stream_connect(sock, uaddr, addr_len, flags, 0);
756 	release_sock(sock->sk);
757 	return err;
758 }
759 EXPORT_SYMBOL(inet_stream_connect);
760 
761 void __inet_accept(struct socket *sock, struct socket *newsock, struct sock *newsk)
762 {
763 	if (mem_cgroup_sockets_enabled) {
764 		mem_cgroup_sk_alloc(newsk);
765 		__sk_charge(newsk, GFP_KERNEL);
766 	}
767 
768 	sock_rps_record_flow(newsk);
769 	WARN_ON(!((1 << newsk->sk_state) &
770 		  (TCPF_ESTABLISHED | TCPF_SYN_RECV |
771 		   TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 |
772 		   TCPF_CLOSING | TCPF_CLOSE_WAIT |
773 		   TCPF_CLOSE)));
774 
775 	if (test_bit(SOCK_SUPPORT_ZC, &sock->flags))
776 		set_bit(SOCK_SUPPORT_ZC, &newsock->flags);
777 	sock_graft(newsk, newsock);
778 
779 	newsock->state = SS_CONNECTED;
780 }
781 EXPORT_SYMBOL_GPL(__inet_accept);
782 
783 /*
784  *	Accept a pending connection. The TCP layer now gives BSD semantics.
785  */
786 
787 int inet_accept(struct socket *sock, struct socket *newsock,
788 		struct proto_accept_arg *arg)
789 {
790 	struct sock *sk1 = sock->sk, *sk2;
791 
792 	arg->err = -EINVAL;
793 	sk2 = READ_ONCE(sk1->sk_prot)->accept(sk1, arg);
794 	if (!sk2)
795 		return arg->err;
796 
797 	lock_sock(sk2);
798 	__inet_accept(sock, newsock, sk2);
799 	release_sock(sk2);
800 	return 0;
801 }
802 EXPORT_SYMBOL(inet_accept);
803 
804 /*
805  *	This does both peername and sockname.
806  */
807 int inet_getname(struct socket *sock, struct sockaddr *uaddr,
808 		 int peer)
809 {
810 	struct sock *sk		= sock->sk;
811 	struct inet_sock *inet	= inet_sk(sk);
812 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
813 	int sin_addr_len = sizeof(*sin);
814 
815 	sin->sin_family = AF_INET;
816 	lock_sock(sk);
817 	if (peer) {
818 		if (!inet->inet_dport ||
819 		    (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
820 		     peer == 1)) {
821 			release_sock(sk);
822 			return -ENOTCONN;
823 		}
824 		sin->sin_port = inet->inet_dport;
825 		sin->sin_addr.s_addr = inet->inet_daddr;
826 		BPF_CGROUP_RUN_SA_PROG(sk, sin, &sin_addr_len,
827 				       CGROUP_INET4_GETPEERNAME);
828 	} else {
829 		__be32 addr = inet->inet_rcv_saddr;
830 		if (!addr)
831 			addr = inet->inet_saddr;
832 		sin->sin_port = inet->inet_sport;
833 		sin->sin_addr.s_addr = addr;
834 		BPF_CGROUP_RUN_SA_PROG(sk, sin, &sin_addr_len,
835 				       CGROUP_INET4_GETSOCKNAME);
836 	}
837 	release_sock(sk);
838 	memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
839 	return sin_addr_len;
840 }
841 EXPORT_SYMBOL(inet_getname);
842 
843 int inet_send_prepare(struct sock *sk)
844 {
845 	sock_rps_record_flow(sk);
846 
847 	/* We may need to bind the socket. */
848 	if (data_race(!inet_sk(sk)->inet_num) && !sk->sk_prot->no_autobind &&
849 	    inet_autobind(sk))
850 		return -EAGAIN;
851 
852 	return 0;
853 }
854 
855 int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
856 {
857 	struct sock *sk = sock->sk;
858 	const struct proto *prot;
859 
860 	if (unlikely(inet_send_prepare(sk)))
861 		return -EAGAIN;
862 
863 	prot = READ_ONCE(sk->sk_prot);
864 	return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udp_sendmsg,
865 			       sk, msg, size);
866 }
867 EXPORT_SYMBOL(inet_sendmsg);
868 
869 void inet_splice_eof(struct socket *sock)
870 {
871 	const struct proto *prot;
872 	struct sock *sk = sock->sk;
873 
874 	if (unlikely(inet_send_prepare(sk)))
875 		return;
876 
877 	prot = READ_ONCE(sk->sk_prot);
878 	if (prot->splice_eof)
879 		prot->splice_eof(sock);
880 }
881 
882 int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
883 		 int flags)
884 {
885 	struct sock *sk = sock->sk;
886 	const struct proto *prot;
887 
888 	if (likely(!(flags & MSG_ERRQUEUE)))
889 		sock_rps_record_flow(sk);
890 
891 	prot = READ_ONCE(sk->sk_prot);
892 	return INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udp_recvmsg,
893 			       sk, msg, size, flags);
894 }
895 EXPORT_SYMBOL(inet_recvmsg);
896 
897 int inet_shutdown(struct socket *sock, int how)
898 {
899 	struct sock *sk = sock->sk;
900 	int err = 0;
901 
902 	/* This should really check to make sure
903 	 * the socket is a TCP socket. (WHY AC...)
904 	 */
905 	how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
906 		       1->2 bit 2 snds.
907 		       2->3 */
908 	if ((how & ~SHUTDOWN_MASK) || !how)	/* MAXINT->0 */
909 		return -EINVAL;
910 
911 	lock_sock(sk);
912 	if (sock->state == SS_CONNECTING) {
913 		if ((1 << sk->sk_state) &
914 		    (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
915 			sock->state = SS_DISCONNECTING;
916 		else
917 			sock->state = SS_CONNECTED;
918 	}
919 
920 	switch (sk->sk_state) {
921 	case TCP_CLOSE:
922 		err = -ENOTCONN;
923 		/* Hack to wake up other listeners, who can poll for
924 		   EPOLLHUP, even on eg. unconnected UDP sockets -- RR */
925 		fallthrough;
926 	default:
927 		WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | how);
928 		if (sk->sk_prot->shutdown)
929 			sk->sk_prot->shutdown(sk, how);
930 		break;
931 
932 	/* Remaining two branches are temporary solution for missing
933 	 * close() in multithreaded environment. It is _not_ a good idea,
934 	 * but we have no choice until close() is repaired at VFS level.
935 	 */
936 	case TCP_LISTEN:
937 		if (!(how & RCV_SHUTDOWN))
938 			break;
939 		fallthrough;
940 	case TCP_SYN_SENT:
941 		err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
942 		sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
943 		break;
944 	}
945 
946 	/* Wake up anyone sleeping in poll. */
947 	sk->sk_state_change(sk);
948 	release_sock(sk);
949 	return err;
950 }
951 EXPORT_SYMBOL(inet_shutdown);
952 
953 /*
954  *	ioctl() calls you can issue on an INET socket. Most of these are
955  *	device configuration and stuff and very rarely used. Some ioctls
956  *	pass on to the socket itself.
957  *
958  *	NOTE: I like the idea of a module for the config stuff. ie ifconfig
959  *	loads the devconfigure module does its configuring and unloads it.
960  *	There's a good 20K of config code hanging around the kernel.
961  */
962 
963 int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
964 {
965 	struct sock *sk = sock->sk;
966 	int err = 0;
967 	struct net *net = sock_net(sk);
968 	void __user *p = (void __user *)arg;
969 	struct ifreq ifr;
970 	struct rtentry rt;
971 
972 	switch (cmd) {
973 	case SIOCADDRT:
974 	case SIOCDELRT:
975 		if (copy_from_user(&rt, p, sizeof(struct rtentry)))
976 			return -EFAULT;
977 		err = ip_rt_ioctl(net, cmd, &rt);
978 		break;
979 	case SIOCRTMSG:
980 		err = -EINVAL;
981 		break;
982 	case SIOCDARP:
983 	case SIOCGARP:
984 	case SIOCSARP:
985 		err = arp_ioctl(net, cmd, (void __user *)arg);
986 		break;
987 	case SIOCGIFADDR:
988 	case SIOCGIFBRDADDR:
989 	case SIOCGIFNETMASK:
990 	case SIOCGIFDSTADDR:
991 	case SIOCGIFPFLAGS:
992 		if (get_user_ifreq(&ifr, NULL, p))
993 			return -EFAULT;
994 		err = devinet_ioctl(net, cmd, &ifr);
995 		if (!err && put_user_ifreq(&ifr, p))
996 			err = -EFAULT;
997 		break;
998 
999 	case SIOCSIFADDR:
1000 	case SIOCSIFBRDADDR:
1001 	case SIOCSIFNETMASK:
1002 	case SIOCSIFDSTADDR:
1003 	case SIOCSIFPFLAGS:
1004 	case SIOCSIFFLAGS:
1005 		if (get_user_ifreq(&ifr, NULL, p))
1006 			return -EFAULT;
1007 		err = devinet_ioctl(net, cmd, &ifr);
1008 		break;
1009 	default:
1010 		if (sk->sk_prot->ioctl)
1011 			err = sk_ioctl(sk, cmd, (void __user *)arg);
1012 		else
1013 			err = -ENOIOCTLCMD;
1014 		break;
1015 	}
1016 	return err;
1017 }
1018 EXPORT_SYMBOL(inet_ioctl);
1019 
1020 #ifdef CONFIG_COMPAT
1021 static int inet_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
1022 		struct compat_rtentry __user *ur)
1023 {
1024 	compat_uptr_t rtdev;
1025 	struct rtentry rt;
1026 
1027 	if (copy_from_user(&rt.rt_dst, &ur->rt_dst,
1028 			3 * sizeof(struct sockaddr)) ||
1029 	    get_user(rt.rt_flags, &ur->rt_flags) ||
1030 	    get_user(rt.rt_metric, &ur->rt_metric) ||
1031 	    get_user(rt.rt_mtu, &ur->rt_mtu) ||
1032 	    get_user(rt.rt_window, &ur->rt_window) ||
1033 	    get_user(rt.rt_irtt, &ur->rt_irtt) ||
1034 	    get_user(rtdev, &ur->rt_dev))
1035 		return -EFAULT;
1036 
1037 	rt.rt_dev = compat_ptr(rtdev);
1038 	return ip_rt_ioctl(sock_net(sk), cmd, &rt);
1039 }
1040 
1041 static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1042 {
1043 	void __user *argp = compat_ptr(arg);
1044 	struct sock *sk = sock->sk;
1045 
1046 	switch (cmd) {
1047 	case SIOCADDRT:
1048 	case SIOCDELRT:
1049 		return inet_compat_routing_ioctl(sk, cmd, argp);
1050 	default:
1051 		if (!sk->sk_prot->compat_ioctl)
1052 			return -ENOIOCTLCMD;
1053 		return sk->sk_prot->compat_ioctl(sk, cmd, arg);
1054 	}
1055 }
1056 #endif /* CONFIG_COMPAT */
1057 
1058 const struct proto_ops inet_stream_ops = {
1059 	.family		   = PF_INET,
1060 	.owner		   = THIS_MODULE,
1061 	.release	   = inet_release,
1062 	.bind		   = inet_bind,
1063 	.connect	   = inet_stream_connect,
1064 	.socketpair	   = sock_no_socketpair,
1065 	.accept		   = inet_accept,
1066 	.getname	   = inet_getname,
1067 	.poll		   = tcp_poll,
1068 	.ioctl		   = inet_ioctl,
1069 	.gettstamp	   = sock_gettstamp,
1070 	.listen		   = inet_listen,
1071 	.shutdown	   = inet_shutdown,
1072 	.setsockopt	   = sock_common_setsockopt,
1073 	.getsockopt	   = sock_common_getsockopt,
1074 	.sendmsg	   = inet_sendmsg,
1075 	.recvmsg	   = inet_recvmsg,
1076 #ifdef CONFIG_MMU
1077 	.mmap		   = tcp_mmap,
1078 #endif
1079 	.splice_eof	   = inet_splice_eof,
1080 	.splice_read	   = tcp_splice_read,
1081 	.set_peek_off      = sk_set_peek_off,
1082 	.read_sock	   = tcp_read_sock,
1083 	.read_skb	   = tcp_read_skb,
1084 	.sendmsg_locked    = tcp_sendmsg_locked,
1085 	.peek_len	   = tcp_peek_len,
1086 #ifdef CONFIG_COMPAT
1087 	.compat_ioctl	   = inet_compat_ioctl,
1088 #endif
1089 	.set_rcvlowat	   = tcp_set_rcvlowat,
1090 	.set_rcvbuf	   = tcp_set_rcvbuf,
1091 };
1092 EXPORT_SYMBOL(inet_stream_ops);
1093 
1094 const struct proto_ops inet_dgram_ops = {
1095 	.family		   = PF_INET,
1096 	.owner		   = THIS_MODULE,
1097 	.release	   = inet_release,
1098 	.bind		   = inet_bind,
1099 	.connect	   = inet_dgram_connect,
1100 	.socketpair	   = sock_no_socketpair,
1101 	.accept		   = sock_no_accept,
1102 	.getname	   = inet_getname,
1103 	.poll		   = udp_poll,
1104 	.ioctl		   = inet_ioctl,
1105 	.gettstamp	   = sock_gettstamp,
1106 	.listen		   = sock_no_listen,
1107 	.shutdown	   = inet_shutdown,
1108 	.setsockopt	   = sock_common_setsockopt,
1109 	.getsockopt	   = sock_common_getsockopt,
1110 	.sendmsg	   = inet_sendmsg,
1111 	.read_skb	   = udp_read_skb,
1112 	.recvmsg	   = inet_recvmsg,
1113 	.mmap		   = sock_no_mmap,
1114 	.splice_eof	   = inet_splice_eof,
1115 	.set_peek_off	   = udp_set_peek_off,
1116 #ifdef CONFIG_COMPAT
1117 	.compat_ioctl	   = inet_compat_ioctl,
1118 #endif
1119 };
1120 EXPORT_SYMBOL(inet_dgram_ops);
1121 
1122 /*
1123  * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
1124  * udp_poll
1125  */
1126 static const struct proto_ops inet_sockraw_ops = {
1127 	.family		   = PF_INET,
1128 	.owner		   = THIS_MODULE,
1129 	.release	   = inet_release,
1130 	.bind		   = inet_bind,
1131 	.connect	   = inet_dgram_connect,
1132 	.socketpair	   = sock_no_socketpair,
1133 	.accept		   = sock_no_accept,
1134 	.getname	   = inet_getname,
1135 	.poll		   = datagram_poll,
1136 	.ioctl		   = inet_ioctl,
1137 	.gettstamp	   = sock_gettstamp,
1138 	.listen		   = sock_no_listen,
1139 	.shutdown	   = inet_shutdown,
1140 	.setsockopt	   = sock_common_setsockopt,
1141 	.getsockopt	   = sock_common_getsockopt,
1142 	.sendmsg	   = inet_sendmsg,
1143 	.recvmsg	   = inet_recvmsg,
1144 	.mmap		   = sock_no_mmap,
1145 	.splice_eof	   = inet_splice_eof,
1146 #ifdef CONFIG_COMPAT
1147 	.compat_ioctl	   = inet_compat_ioctl,
1148 #endif
1149 };
1150 
1151 static const struct net_proto_family inet_family_ops = {
1152 	.family = PF_INET,
1153 	.create = inet_create,
1154 	.owner	= THIS_MODULE,
1155 };
1156 
1157 /* Upon startup we insert all the elements in inetsw_array[] into
1158  * the linked list inetsw.
1159  */
1160 static struct inet_protosw inetsw_array[] =
1161 {
1162 	{
1163 		.type =       SOCK_STREAM,
1164 		.protocol =   IPPROTO_TCP,
1165 		.prot =       &tcp_prot,
1166 		.ops =        &inet_stream_ops,
1167 		.flags =      INET_PROTOSW_PERMANENT |
1168 			      INET_PROTOSW_ICSK,
1169 	},
1170 
1171 	{
1172 		.type =       SOCK_DGRAM,
1173 		.protocol =   IPPROTO_UDP,
1174 		.prot =       &udp_prot,
1175 		.ops =        &inet_dgram_ops,
1176 		.flags =      INET_PROTOSW_PERMANENT,
1177        },
1178 
1179        {
1180 		.type =       SOCK_DGRAM,
1181 		.protocol =   IPPROTO_ICMP,
1182 		.prot =       &ping_prot,
1183 		.ops =        &inet_sockraw_ops,
1184 		.flags =      INET_PROTOSW_REUSE,
1185        },
1186 
1187        {
1188 	       .type =       SOCK_RAW,
1189 	       .protocol =   IPPROTO_IP,	/* wild card */
1190 	       .prot =       &raw_prot,
1191 	       .ops =        &inet_sockraw_ops,
1192 	       .flags =      INET_PROTOSW_REUSE,
1193        }
1194 };
1195 
1196 #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
1197 
1198 void inet_register_protosw(struct inet_protosw *p)
1199 {
1200 	struct list_head *lh;
1201 	struct inet_protosw *answer;
1202 	int protocol = p->protocol;
1203 	struct list_head *last_perm;
1204 
1205 	spin_lock_bh(&inetsw_lock);
1206 
1207 	if (p->type >= SOCK_MAX)
1208 		goto out_illegal;
1209 
1210 	/* If we are trying to override a permanent protocol, bail. */
1211 	last_perm = &inetsw[p->type];
1212 	list_for_each(lh, &inetsw[p->type]) {
1213 		answer = list_entry(lh, struct inet_protosw, list);
1214 		/* Check only the non-wild match. */
1215 		if ((INET_PROTOSW_PERMANENT & answer->flags) == 0)
1216 			break;
1217 		if (protocol == answer->protocol)
1218 			goto out_permanent;
1219 		last_perm = lh;
1220 	}
1221 
1222 	/* Add the new entry after the last permanent entry if any, so that
1223 	 * the new entry does not override a permanent entry when matched with
1224 	 * a wild-card protocol. But it is allowed to override any existing
1225 	 * non-permanent entry.  This means that when we remove this entry, the
1226 	 * system automatically returns to the old behavior.
1227 	 */
1228 	list_add_rcu(&p->list, last_perm);
1229 out:
1230 	spin_unlock_bh(&inetsw_lock);
1231 
1232 	return;
1233 
1234 out_permanent:
1235 	pr_err("Attempt to override permanent protocol %d\n", protocol);
1236 	goto out;
1237 
1238 out_illegal:
1239 	pr_err("Ignoring attempt to register invalid socket type %d\n",
1240 	       p->type);
1241 	goto out;
1242 }
1243 EXPORT_SYMBOL(inet_register_protosw);
1244 
1245 void inet_unregister_protosw(struct inet_protosw *p)
1246 {
1247 	if (INET_PROTOSW_PERMANENT & p->flags) {
1248 		pr_err("Attempt to unregister permanent protocol %d\n",
1249 		       p->protocol);
1250 	} else {
1251 		spin_lock_bh(&inetsw_lock);
1252 		list_del_rcu(&p->list);
1253 		spin_unlock_bh(&inetsw_lock);
1254 
1255 		synchronize_net();
1256 	}
1257 }
1258 EXPORT_SYMBOL(inet_unregister_protosw);
1259 
1260 static int inet_sk_reselect_saddr(struct sock *sk)
1261 {
1262 	struct inet_sock *inet = inet_sk(sk);
1263 	__be32 old_saddr = inet->inet_saddr;
1264 	__be32 daddr = inet->inet_daddr;
1265 	struct flowi4 *fl4;
1266 	struct rtable *rt;
1267 	__be32 new_saddr;
1268 	struct ip_options_rcu *inet_opt;
1269 	int err;
1270 
1271 	inet_opt = rcu_dereference_protected(inet->inet_opt,
1272 					     lockdep_sock_is_held(sk));
1273 	if (inet_opt && inet_opt->opt.srr)
1274 		daddr = inet_opt->opt.faddr;
1275 
1276 	/* Query new route. */
1277 	fl4 = &inet->cork.fl.u.ip4;
1278 	rt = ip_route_connect(fl4, daddr, 0, sk->sk_bound_dev_if,
1279 			      sk->sk_protocol, inet->inet_sport,
1280 			      inet->inet_dport, sk);
1281 	if (IS_ERR(rt))
1282 		return PTR_ERR(rt);
1283 
1284 	new_saddr = fl4->saddr;
1285 
1286 	if (new_saddr == old_saddr) {
1287 		sk_setup_caps(sk, &rt->dst);
1288 		return 0;
1289 	}
1290 
1291 	err = inet_bhash2_update_saddr(sk, &new_saddr, AF_INET);
1292 	if (err) {
1293 		ip_rt_put(rt);
1294 		return err;
1295 	}
1296 
1297 	sk_setup_caps(sk, &rt->dst);
1298 
1299 	if (READ_ONCE(sock_net(sk)->ipv4.sysctl_ip_dynaddr) > 1) {
1300 		pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
1301 			__func__, &old_saddr, &new_saddr);
1302 	}
1303 
1304 	/*
1305 	 * XXX The only one ugly spot where we need to
1306 	 * XXX really change the sockets identity after
1307 	 * XXX it has entered the hashes. -DaveM
1308 	 *
1309 	 * Besides that, it does not check for connection
1310 	 * uniqueness. Wait for troubles.
1311 	 */
1312 	return __sk_prot_rehash(sk);
1313 }
1314 
1315 int inet_sk_rebuild_header(struct sock *sk)
1316 {
1317 	struct rtable *rt = dst_rtable(__sk_dst_check(sk, 0));
1318 	struct inet_sock *inet = inet_sk(sk);
1319 	struct flowi4 *fl4;
1320 	int err;
1321 
1322 	/* Route is OK, nothing to do. */
1323 	if (rt)
1324 		return 0;
1325 
1326 	/* Reroute. */
1327 	fl4 = &inet->cork.fl.u.ip4;
1328 	inet_sk_init_flowi4(inet, fl4);
1329 	rt = ip_route_output_flow(sock_net(sk), fl4, sk);
1330 	if (!IS_ERR(rt)) {
1331 		err = 0;
1332 		sk_setup_caps(sk, &rt->dst);
1333 	} else {
1334 		err = PTR_ERR(rt);
1335 
1336 		/* Routing failed... */
1337 		sk->sk_route_caps = 0;
1338 
1339 		if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_ip_dynaddr) ||
1340 		    sk->sk_state != TCP_SYN_SENT ||
1341 		    (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1342 		    (err = inet_sk_reselect_saddr(sk)) != 0)
1343 			WRITE_ONCE(sk->sk_err_soft, -err);
1344 	}
1345 
1346 	return err;
1347 }
1348 
1349 void inet_sk_set_state(struct sock *sk, int state)
1350 {
1351 	trace_inet_sock_set_state(sk, sk->sk_state, state);
1352 	sk->sk_state = state;
1353 }
1354 EXPORT_SYMBOL(inet_sk_set_state);
1355 
1356 void inet_sk_state_store(struct sock *sk, int newstate)
1357 {
1358 	trace_inet_sock_set_state(sk, sk->sk_state, newstate);
1359 	smp_store_release(&sk->sk_state, newstate);
1360 }
1361 
1362 struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1363 				 netdev_features_t features)
1364 {
1365 	bool udpfrag = false, fixedid = false, gso_partial, encap;
1366 	struct sk_buff *segs = ERR_PTR(-EINVAL);
1367 	const struct net_offload *ops;
1368 	unsigned int offset = 0;
1369 	struct iphdr *iph;
1370 	int proto, tot_len;
1371 	int nhoff;
1372 	int ihl;
1373 	int id;
1374 
1375 	skb_reset_network_header(skb);
1376 	if (unlikely(gso_recursion_inc_test(skb,
1377 					   IP_TUNNEL_RECURSION_LIMIT)))
1378 		goto out;
1379 	nhoff = skb_network_header(skb) - skb_mac_header(skb);
1380 	if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1381 		goto out;
1382 
1383 	iph = ip_hdr(skb);
1384 	ihl = iph->ihl * 4;
1385 	if (ihl < sizeof(*iph))
1386 		goto out;
1387 
1388 	id = ntohs(iph->id);
1389 	proto = iph->protocol;
1390 
1391 	/* Warning: after this point, iph might be no longer valid */
1392 	if (unlikely(!pskb_may_pull(skb, ihl)))
1393 		goto out;
1394 	__skb_pull(skb, ihl);
1395 
1396 	encap = SKB_GSO_CB(skb)->encap_level > 0;
1397 	if (encap)
1398 		features &= skb->dev->hw_enc_features;
1399 	SKB_GSO_CB(skb)->encap_level += ihl;
1400 
1401 	skb_reset_transport_header(skb);
1402 
1403 	segs = ERR_PTR(-EPROTONOSUPPORT);
1404 
1405 	fixedid = !!(skb_shinfo(skb)->gso_type & (SKB_GSO_TCP_FIXEDID << encap));
1406 
1407 	if (!skb->encapsulation || encap)
1408 		udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
1409 
1410 	ops = rcu_dereference(inet_offloads[proto]);
1411 	if (likely(ops && ops->callbacks.gso_segment)) {
1412 		segs = ops->callbacks.gso_segment(skb, features);
1413 		if (!segs)
1414 			skb->network_header = skb_mac_header(skb) + nhoff - skb->head;
1415 	}
1416 
1417 	if (IS_ERR_OR_NULL(segs))
1418 		goto out;
1419 
1420 	gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
1421 
1422 	skb = segs;
1423 	do {
1424 		iph = (struct iphdr *)(skb_mac_header(skb) + nhoff);
1425 		if (udpfrag) {
1426 			iph->frag_off = htons(offset >> 3);
1427 			if (skb->next)
1428 				iph->frag_off |= htons(IP_MF);
1429 			offset += skb->len - nhoff - ihl;
1430 			tot_len = skb->len - nhoff;
1431 		} else if (skb_is_gso(skb)) {
1432 			if (!fixedid) {
1433 				iph->id = htons(id);
1434 				id += skb_shinfo(skb)->gso_segs;
1435 			}
1436 
1437 			if (gso_partial)
1438 				tot_len = skb_shinfo(skb)->gso_size +
1439 					  SKB_GSO_CB(skb)->data_offset +
1440 					  skb->head - (unsigned char *)iph;
1441 			else
1442 				tot_len = skb->len - nhoff;
1443 		} else {
1444 			if (!fixedid)
1445 				iph->id = htons(id++);
1446 			tot_len = skb->len - nhoff;
1447 		}
1448 		iph->tot_len = htons(tot_len);
1449 		ip_send_check(iph);
1450 		if (encap)
1451 			skb_reset_inner_headers(skb);
1452 		skb->network_header = (u8 *)iph - skb->head;
1453 		skb_reset_mac_len(skb);
1454 	} while ((skb = skb->next));
1455 
1456 out:
1457 	return segs;
1458 }
1459 
1460 static struct sk_buff *ipip_gso_segment(struct sk_buff *skb,
1461 					netdev_features_t features)
1462 {
1463 	if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP4))
1464 		return ERR_PTR(-EINVAL);
1465 
1466 	return inet_gso_segment(skb, features);
1467 }
1468 
1469 struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb)
1470 {
1471 	const struct net_offload *ops;
1472 	struct sk_buff *pp = NULL;
1473 	const struct iphdr *iph;
1474 	struct sk_buff *p;
1475 	unsigned int hlen;
1476 	unsigned int off;
1477 	int flush = 1;
1478 	int proto;
1479 
1480 	off = skb_gro_offset(skb);
1481 	hlen = off + sizeof(*iph);
1482 	iph = skb_gro_header(skb, hlen, off);
1483 	if (unlikely(!iph))
1484 		goto out;
1485 
1486 	proto = iph->protocol;
1487 
1488 	ops = rcu_dereference(inet_offloads[proto]);
1489 	if (!ops || !ops->callbacks.gro_receive)
1490 		goto out;
1491 
1492 	if (*(u8 *)iph != 0x45)
1493 		goto out;
1494 
1495 	if (ip_is_fragment(iph))
1496 		goto out;
1497 
1498 	if (unlikely(ip_fast_csum((u8 *)iph, 5)))
1499 		goto out;
1500 
1501 	NAPI_GRO_CB(skb)->proto = proto;
1502 	flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (ntohl(*(__be32 *)&iph->id) & ~IP_DF));
1503 
1504 	list_for_each_entry(p, head, list) {
1505 		struct iphdr *iph2;
1506 
1507 		if (!NAPI_GRO_CB(p)->same_flow)
1508 			continue;
1509 
1510 		iph2 = (struct iphdr *)(p->data + off);
1511 		/* The above works because, with the exception of the top
1512 		 * (inner most) layer, we only aggregate pkts with the same
1513 		 * hdr length so all the hdrs we'll need to verify will start
1514 		 * at the same offset.
1515 		 */
1516 		if ((iph->protocol ^ iph2->protocol) |
1517 		    ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
1518 		    ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
1519 			NAPI_GRO_CB(p)->same_flow = 0;
1520 			continue;
1521 		}
1522 	}
1523 
1524 	NAPI_GRO_CB(skb)->flush |= flush;
1525 	NAPI_GRO_CB(skb)->network_offsets[NAPI_GRO_CB(skb)->encap_mark] = off;
1526 
1527 	/* Note : No need to call skb_gro_postpull_rcsum() here,
1528 	 * as we already checked checksum over ipv4 header was 0
1529 	 */
1530 	skb_gro_pull(skb, sizeof(*iph));
1531 	skb_set_transport_header(skb, skb_gro_offset(skb));
1532 
1533 	pp = indirect_call_gro_receive(tcp4_gro_receive, udp4_gro_receive,
1534 				       ops->callbacks.gro_receive, head, skb);
1535 
1536 out:
1537 	skb_gro_flush_final(skb, pp, flush);
1538 
1539 	return pp;
1540 }
1541 EXPORT_INDIRECT_CALLABLE(inet_gro_receive);
1542 
1543 static struct sk_buff *ipip_gro_receive(struct list_head *head,
1544 					struct sk_buff *skb)
1545 {
1546 	if (NAPI_GRO_CB(skb)->encap_mark) {
1547 		NAPI_GRO_CB(skb)->flush = 1;
1548 		return NULL;
1549 	}
1550 
1551 	NAPI_GRO_CB(skb)->encap_mark = 1;
1552 
1553 	return inet_gro_receive(head, skb);
1554 }
1555 
1556 #define SECONDS_PER_DAY	86400
1557 
1558 /* inet_current_timestamp - Return IP network timestamp
1559  *
1560  * Return milliseconds since midnight in network byte order.
1561  */
1562 __be32 inet_current_timestamp(void)
1563 {
1564 	u32 secs;
1565 	u32 msecs;
1566 	struct timespec64 ts;
1567 
1568 	ktime_get_real_ts64(&ts);
1569 
1570 	/* Get secs since midnight. */
1571 	(void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs);
1572 	/* Convert to msecs. */
1573 	msecs = secs * MSEC_PER_SEC;
1574 	/* Convert nsec to msec. */
1575 	msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC;
1576 
1577 	/* Convert to network byte order. */
1578 	return htonl(msecs);
1579 }
1580 
1581 int inet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1582 {
1583 	unsigned int family = READ_ONCE(sk->sk_family);
1584 
1585 	if (family == AF_INET)
1586 		return ip_recv_error(sk, msg, len);
1587 #if IS_ENABLED(CONFIG_IPV6)
1588 	if (family == AF_INET6)
1589 		return pingv6_ops.ipv6_recv_error(sk, msg, len);
1590 #endif
1591 	return -EINVAL;
1592 }
1593 EXPORT_SYMBOL(inet_recv_error);
1594 
1595 int inet_gro_complete(struct sk_buff *skb, int nhoff)
1596 {
1597 	struct iphdr *iph = (struct iphdr *)(skb->data + nhoff);
1598 	const struct net_offload *ops;
1599 	__be16 totlen = iph->tot_len;
1600 	int proto = iph->protocol;
1601 	int err = -ENOSYS;
1602 
1603 	if (skb->encapsulation) {
1604 		skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IP));
1605 		skb_set_inner_network_header(skb, nhoff);
1606 	}
1607 
1608 	iph_set_totlen(iph, skb->len - nhoff);
1609 	csum_replace2(&iph->check, totlen, iph->tot_len);
1610 
1611 	ops = rcu_dereference(inet_offloads[proto]);
1612 	if (WARN_ON(!ops || !ops->callbacks.gro_complete))
1613 		goto out;
1614 
1615 	/* Only need to add sizeof(*iph) to get to the next hdr below
1616 	 * because any hdr with option will have been flushed in
1617 	 * inet_gro_receive().
1618 	 */
1619 	err = INDIRECT_CALL_2(ops->callbacks.gro_complete,
1620 			      tcp4_gro_complete, udp4_gro_complete,
1621 			      skb, nhoff + sizeof(*iph));
1622 
1623 out:
1624 	return err;
1625 }
1626 EXPORT_INDIRECT_CALLABLE(inet_gro_complete);
1627 
1628 static int ipip_gro_complete(struct sk_buff *skb, int nhoff)
1629 {
1630 	skb->encapsulation = 1;
1631 	skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
1632 	return inet_gro_complete(skb, nhoff);
1633 }
1634 
1635 int inet_ctl_sock_create(struct sock **sk, unsigned short family,
1636 			 unsigned short type, unsigned char protocol,
1637 			 struct net *net)
1638 {
1639 	struct socket *sock;
1640 	int rc = sock_create_kern(net, family, type, protocol, &sock);
1641 
1642 	if (rc == 0) {
1643 		*sk = sock->sk;
1644 		(*sk)->sk_allocation = GFP_ATOMIC;
1645 		(*sk)->sk_use_task_frag = false;
1646 		/*
1647 		 * Unhash it so that IP input processing does not even see it,
1648 		 * we do not wish this socket to see incoming packets.
1649 		 */
1650 		(*sk)->sk_prot->unhash(*sk);
1651 	}
1652 	return rc;
1653 }
1654 EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1655 
1656 unsigned long snmp_fold_field(void __percpu *mib, int offt)
1657 {
1658 	unsigned long res = 0;
1659 	int i;
1660 
1661 	for_each_possible_cpu(i)
1662 		res += snmp_get_cpu_field(mib, i, offt);
1663 	return res;
1664 }
1665 
1666 #if BITS_PER_LONG==32
1667 
1668 u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offt,
1669 			 size_t syncp_offset)
1670 {
1671 	void *bhptr;
1672 	struct u64_stats_sync *syncp;
1673 	u64 v;
1674 	unsigned int start;
1675 
1676 	bhptr = per_cpu_ptr(mib, cpu);
1677 	syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
1678 	do {
1679 		start = u64_stats_fetch_begin(syncp);
1680 		v = *(((u64 *)bhptr) + offt);
1681 	} while (u64_stats_fetch_retry(syncp, start));
1682 
1683 	return v;
1684 }
1685 
1686 u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_offset)
1687 {
1688 	u64 res = 0;
1689 	int cpu;
1690 
1691 	for_each_possible_cpu(cpu) {
1692 		res += snmp_get_cpu_field64(mib, cpu, offt, syncp_offset);
1693 	}
1694 	return res;
1695 }
1696 #endif
1697 
1698 #ifdef CONFIG_IP_MULTICAST
1699 static const struct net_protocol igmp_protocol = {
1700 	.handler =	igmp_rcv,
1701 };
1702 #endif
1703 
1704 static const struct net_protocol icmp_protocol = {
1705 	.handler =	icmp_rcv,
1706 	.err_handler =	icmp_err,
1707 	.no_policy =	1,
1708 };
1709 
1710 static __net_init int ipv4_mib_init_net(struct net *net)
1711 {
1712 	int i;
1713 
1714 	net->mib.tcp_statistics = alloc_percpu(struct tcp_mib);
1715 	if (!net->mib.tcp_statistics)
1716 		goto err_tcp_mib;
1717 	net->mib.ip_statistics = alloc_percpu(struct ipstats_mib);
1718 	if (!net->mib.ip_statistics)
1719 		goto err_ip_mib;
1720 
1721 	for_each_possible_cpu(i) {
1722 		struct ipstats_mib *af_inet_stats;
1723 		af_inet_stats = per_cpu_ptr(net->mib.ip_statistics, i);
1724 		u64_stats_init(&af_inet_stats->syncp);
1725 	}
1726 
1727 	net->mib.net_statistics = alloc_percpu(struct linux_mib);
1728 	if (!net->mib.net_statistics)
1729 		goto err_net_mib;
1730 	net->mib.udp_statistics = alloc_percpu(struct udp_mib);
1731 	if (!net->mib.udp_statistics)
1732 		goto err_udp_mib;
1733 	net->mib.icmp_statistics = alloc_percpu(struct icmp_mib);
1734 	if (!net->mib.icmp_statistics)
1735 		goto err_icmp_mib;
1736 	net->mib.icmpmsg_statistics = kzalloc_obj(struct icmpmsg_mib);
1737 	if (!net->mib.icmpmsg_statistics)
1738 		goto err_icmpmsg_mib;
1739 
1740 	tcp_mib_init(net);
1741 	return 0;
1742 
1743 err_icmpmsg_mib:
1744 	free_percpu(net->mib.icmp_statistics);
1745 err_icmp_mib:
1746 	free_percpu(net->mib.udp_statistics);
1747 err_udp_mib:
1748 	free_percpu(net->mib.net_statistics);
1749 err_net_mib:
1750 	free_percpu(net->mib.ip_statistics);
1751 err_ip_mib:
1752 	free_percpu(net->mib.tcp_statistics);
1753 err_tcp_mib:
1754 	return -ENOMEM;
1755 }
1756 
1757 static __net_exit void ipv4_mib_exit_net(struct net *net)
1758 {
1759 	kfree(net->mib.icmpmsg_statistics);
1760 	free_percpu(net->mib.icmp_statistics);
1761 	free_percpu(net->mib.udp_statistics);
1762 	free_percpu(net->mib.net_statistics);
1763 	free_percpu(net->mib.ip_statistics);
1764 	free_percpu(net->mib.tcp_statistics);
1765 #ifdef CONFIG_MPTCP
1766 	/* allocated on demand, see mptcp_init_sock() */
1767 	free_percpu(net->mib.mptcp_statistics);
1768 #endif
1769 }
1770 
1771 static __net_initdata struct pernet_operations ipv4_mib_ops = {
1772 	.init = ipv4_mib_init_net,
1773 	.exit = ipv4_mib_exit_net,
1774 };
1775 
1776 static int __init init_ipv4_mibs(void)
1777 {
1778 	return register_pernet_subsys(&ipv4_mib_ops);
1779 }
1780 
1781 static __net_init int inet_init_net(struct net *net)
1782 {
1783 	/*
1784 	 * Set defaults for local port range
1785 	 */
1786 	net->ipv4.ip_local_ports.range = 60999u << 16 | 32768u;
1787 
1788 	seqlock_init(&net->ipv4.ping_group_range.lock);
1789 	/*
1790 	 * Sane defaults - nobody may create ping sockets.
1791 	 * Boot scripts should set this to distro-specific group.
1792 	 */
1793 	net->ipv4.ping_group_range.range[0] = make_kgid(&init_user_ns, 1);
1794 	net->ipv4.ping_group_range.range[1] = make_kgid(&init_user_ns, 0);
1795 
1796 	/* Default values for sysctl-controlled parameters.
1797 	 * We set them here, in case sysctl is not compiled.
1798 	 */
1799 	net->ipv4.sysctl_ip_default_ttl = IPDEFTTL;
1800 	net->ipv4.sysctl_ip_fwd_update_priority = 1;
1801 	net->ipv4.sysctl_ip_dynaddr = 0;
1802 	net->ipv4.sysctl_ip_early_demux = 1;
1803 	net->ipv4.sysctl_udp_early_demux = 1;
1804 	net->ipv4.sysctl_tcp_early_demux = 1;
1805 	net->ipv4.sysctl_nexthop_compat_mode = 1;
1806 #ifdef CONFIG_SYSCTL
1807 	net->ipv4.sysctl_ip_prot_sock = PROT_SOCK;
1808 #endif
1809 
1810 	/* Some igmp sysctl, whose values are always used */
1811 	net->ipv4.sysctl_igmp_max_memberships = 20;
1812 	net->ipv4.sysctl_igmp_max_msf = 10;
1813 	/* IGMP reports for link-local multicast groups are enabled by default */
1814 	net->ipv4.sysctl_igmp_llm_reports = 1;
1815 	net->ipv4.sysctl_igmp_qrv = 2;
1816 
1817 	net->ipv4.sysctl_fib_notify_on_flag_change = 0;
1818 
1819 	return 0;
1820 }
1821 
1822 static __net_initdata struct pernet_operations af_inet_ops = {
1823 	.init = inet_init_net,
1824 };
1825 
1826 static int __init init_inet_pernet_ops(void)
1827 {
1828 	return register_pernet_subsys(&af_inet_ops);
1829 }
1830 
1831 static int ipv4_proc_init(void);
1832 
1833 /*
1834  *	IP protocol layer initialiser
1835  */
1836 
1837 
1838 static const struct net_offload ipip_offload = {
1839 	.callbacks = {
1840 		.gso_segment	= ipip_gso_segment,
1841 		.gro_receive	= ipip_gro_receive,
1842 		.gro_complete	= ipip_gro_complete,
1843 	},
1844 };
1845 
1846 static int __init ipip_offload_init(void)
1847 {
1848 	return inet_add_offload(&ipip_offload, IPPROTO_IPIP);
1849 }
1850 
1851 static int __init ipv4_offload_init(void)
1852 {
1853 	/*
1854 	 * Add offloads
1855 	 */
1856 	if (udpv4_offload_init() < 0)
1857 		pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
1858 	if (tcpv4_offload_init() < 0)
1859 		pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
1860 	if (ipip_offload_init() < 0)
1861 		pr_crit("%s: Cannot add IPIP protocol offload\n", __func__);
1862 
1863 	net_hotdata.ip_packet_offload = (struct packet_offload) {
1864 		.type = cpu_to_be16(ETH_P_IP),
1865 		.callbacks = {
1866 			.gso_segment = inet_gso_segment,
1867 			.gro_receive = inet_gro_receive,
1868 			.gro_complete = inet_gro_complete,
1869 		},
1870 	};
1871 	dev_add_offload(&net_hotdata.ip_packet_offload);
1872 	return 0;
1873 }
1874 
1875 fs_initcall(ipv4_offload_init);
1876 
1877 static struct packet_type ip_packet_type __read_mostly = {
1878 	.type = cpu_to_be16(ETH_P_IP),
1879 	.func = ip_rcv,
1880 	.list_func = ip_list_rcv,
1881 };
1882 
1883 static int __init inet_init(void)
1884 {
1885 	struct inet_protosw *q;
1886 	struct list_head *r;
1887 	int rc;
1888 
1889 	sock_skb_cb_check_size(sizeof(struct inet_skb_parm));
1890 
1891 	raw_hashinfo_init(&raw_v4_hashinfo);
1892 
1893 	rc = proto_register(&tcp_prot, 1);
1894 	if (rc)
1895 		goto out;
1896 
1897 	rc = proto_register(&udp_prot, 1);
1898 	if (rc)
1899 		goto out_unregister_tcp_proto;
1900 
1901 	rc = proto_register(&raw_prot, 1);
1902 	if (rc)
1903 		goto out_unregister_udp_proto;
1904 
1905 	rc = proto_register(&ping_prot, 1);
1906 	if (rc)
1907 		goto out_unregister_raw_proto;
1908 
1909 	/*
1910 	 *	Tell SOCKET that we are alive...
1911 	 */
1912 
1913 	(void)sock_register(&inet_family_ops);
1914 
1915 #ifdef CONFIG_SYSCTL
1916 	ip_static_sysctl_init();
1917 #endif
1918 
1919 	/*
1920 	 *	Add all the base protocols.
1921 	 */
1922 
1923 	if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1924 		pr_crit("%s: Cannot add ICMP protocol\n", __func__);
1925 
1926 	net_hotdata.udp_protocol = (struct net_protocol) {
1927 		.handler =	udp_rcv,
1928 		.err_handler =	udp_err,
1929 		.no_policy =	1,
1930 	};
1931 	if (inet_add_protocol(&net_hotdata.udp_protocol, IPPROTO_UDP) < 0)
1932 		pr_crit("%s: Cannot add UDP protocol\n", __func__);
1933 
1934 	net_hotdata.tcp_protocol = (struct net_protocol) {
1935 		.handler	=	tcp_v4_rcv,
1936 		.err_handler	=	tcp_v4_err,
1937 		.no_policy	=	1,
1938 		.icmp_strict_tag_validation = 1,
1939 	};
1940 	if (inet_add_protocol(&net_hotdata.tcp_protocol, IPPROTO_TCP) < 0)
1941 		pr_crit("%s: Cannot add TCP protocol\n", __func__);
1942 #ifdef CONFIG_IP_MULTICAST
1943 	if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1944 		pr_crit("%s: Cannot add IGMP protocol\n", __func__);
1945 #endif
1946 
1947 	/* Register the socket-side information for inet_create. */
1948 	for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1949 		INIT_LIST_HEAD(r);
1950 
1951 	for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1952 		inet_register_protosw(q);
1953 
1954 	/*
1955 	 *	Set the ARP module up
1956 	 */
1957 
1958 	arp_init();
1959 
1960 	/*
1961 	 *	Set the IP module up
1962 	 */
1963 
1964 	ip_init();
1965 
1966 	/* Initialise per-cpu ipv4 mibs */
1967 	if (init_ipv4_mibs())
1968 		panic("%s: Cannot init ipv4 mibs\n", __func__);
1969 
1970 	/* Setup TCP slab cache for open requests. */
1971 	tcp_init();
1972 
1973 	/* Setup UDP memory threshold */
1974 	udp_init();
1975 
1976 	raw_init();
1977 
1978 	ping_init();
1979 
1980 	/*
1981 	 *	Set the ICMP layer up
1982 	 */
1983 
1984 	if (icmp_init() < 0)
1985 		panic("Failed to create the ICMP control socket.\n");
1986 
1987 	/*
1988 	 *	Initialise the multicast router
1989 	 */
1990 #if defined(CONFIG_IP_MROUTE)
1991 	if (ip_mr_init())
1992 		pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
1993 #endif
1994 
1995 	if (init_inet_pernet_ops())
1996 		pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__);
1997 
1998 	ipv4_proc_init();
1999 
2000 	ipfrag_init();
2001 
2002 	dev_add_pack(&ip_packet_type);
2003 
2004 	ip_tunnel_core_init();
2005 
2006 	rc = 0;
2007 out:
2008 	return rc;
2009 out_unregister_raw_proto:
2010 	proto_unregister(&raw_prot);
2011 out_unregister_udp_proto:
2012 	proto_unregister(&udp_prot);
2013 out_unregister_tcp_proto:
2014 	proto_unregister(&tcp_prot);
2015 	goto out;
2016 }
2017 
2018 fs_initcall(inet_init);
2019 
2020 /* ------------------------------------------------------------------------ */
2021 
2022 #ifdef CONFIG_PROC_FS
2023 static int __init ipv4_proc_init(void)
2024 {
2025 	int rc = 0;
2026 
2027 	if (raw_proc_init())
2028 		goto out_raw;
2029 	if (tcp4_proc_init())
2030 		goto out_tcp;
2031 	if (udp4_proc_init())
2032 		goto out_udp;
2033 	if (ping_proc_init())
2034 		goto out_ping;
2035 	if (ip_misc_proc_init())
2036 		goto out_misc;
2037 out:
2038 	return rc;
2039 out_misc:
2040 	ping_proc_exit();
2041 out_ping:
2042 	udp4_proc_exit();
2043 out_udp:
2044 	tcp4_proc_exit();
2045 out_tcp:
2046 	raw_proc_exit();
2047 out_raw:
2048 	rc = -ENOMEM;
2049 	goto out;
2050 }
2051 
2052 #else /* CONFIG_PROC_FS */
2053 static int __init ipv4_proc_init(void)
2054 {
2055 	return 0;
2056 }
2057 #endif /* CONFIG_PROC_FS */
2058