xref: /openbmc/linux/net/ipv4/af_inet.c (revision 6f24080e)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		PF_INET protocol family socket handler.
81da177e4SLinus Torvalds  *
902c30a84SJesper Juhl  * Authors:	Ross Biro
101da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
111da177e4SLinus Torvalds  *		Florian La Roche, <flla@stud.uni-sb.de>
121da177e4SLinus Torvalds  *		Alan Cox, <A.Cox@swansea.ac.uk>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Changes (see also sock.c)
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *		piggy,
171da177e4SLinus Torvalds  *		Karl Knutson	:	Socket protocol table
181da177e4SLinus Torvalds  *		A.N.Kuznetsov	:	Socket death error in accept().
191da177e4SLinus Torvalds  *		John Richardson :	Fix non blocking error in connect()
201da177e4SLinus Torvalds  *					so sockets that fail to connect
211da177e4SLinus Torvalds  *					don't return -EINPROGRESS.
221da177e4SLinus Torvalds  *		Alan Cox	:	Asynchronous I/O support
231da177e4SLinus Torvalds  *		Alan Cox	:	Keep correct socket pointer on sock
241da177e4SLinus Torvalds  *					structures
251da177e4SLinus Torvalds  *					when accept() ed
261da177e4SLinus Torvalds  *		Alan Cox	:	Semantics of SO_LINGER aren't state
271da177e4SLinus Torvalds  *					moved to close when you look carefully.
281da177e4SLinus Torvalds  *					With this fixed and the accept bug fixed
291da177e4SLinus Torvalds  *					some RPC stuff seems happier.
301da177e4SLinus Torvalds  *		Niibe Yutaka	:	4.4BSD style write async I/O
311da177e4SLinus Torvalds  *		Alan Cox,
321da177e4SLinus Torvalds  *		Tony Gale 	:	Fixed reuse semantics.
331da177e4SLinus Torvalds  *		Alan Cox	:	bind() shouldn't abort existing but dead
341da177e4SLinus Torvalds  *					sockets. Stops FTP netin:.. I hope.
351da177e4SLinus Torvalds  *		Alan Cox	:	bind() works correctly for RAW sockets.
361da177e4SLinus Torvalds  *					Note that FreeBSD at least was broken
371da177e4SLinus Torvalds  *					in this respect so be careful with
381da177e4SLinus Torvalds  *					compatibility tests...
391da177e4SLinus Torvalds  *		Alan Cox	:	routing cache support
401da177e4SLinus Torvalds  *		Alan Cox	:	memzero the socket structure for
411da177e4SLinus Torvalds  *					compactness.
421da177e4SLinus Torvalds  *		Matt Day	:	nonblock connect error handler
431da177e4SLinus Torvalds  *		Alan Cox	:	Allow large numbers of pending sockets
441da177e4SLinus Torvalds  *					(eg for big web sites), but only if
451da177e4SLinus Torvalds  *					specifically application requested.
461da177e4SLinus Torvalds  *		Alan Cox	:	New buffering throughout IP. Used
471da177e4SLinus Torvalds  *					dumbly.
481da177e4SLinus Torvalds  *		Alan Cox	:	New buffering now used smartly.
491da177e4SLinus Torvalds  *		Alan Cox	:	BSD rather than common sense
501da177e4SLinus Torvalds  *					interpretation of listen.
511da177e4SLinus Torvalds  *		Germano Caronni	:	Assorted small races.
521da177e4SLinus Torvalds  *		Alan Cox	:	sendmsg/recvmsg basic support.
531da177e4SLinus Torvalds  *		Alan Cox	:	Only sendmsg/recvmsg now supported.
541da177e4SLinus Torvalds  *		Alan Cox	:	Locked down bind (see security list).
551da177e4SLinus Torvalds  *		Alan Cox	:	Loosened bind a little.
561da177e4SLinus Torvalds  *		Mike McLagan	:	ADD/DEL DLCI Ioctls
571da177e4SLinus Torvalds  *	Willy Konynenberg	:	Transparent proxying support.
581da177e4SLinus Torvalds  *		David S. Miller	:	New socket lookup architecture.
591da177e4SLinus Torvalds  *					Some other random speedups.
601da177e4SLinus Torvalds  *		Cyrus Durgin	:	Cleaned up file for kmod hacks.
611da177e4SLinus Torvalds  *		Andi Kleen	:	Fix inet_stream_connect TCP race.
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds 
64afd46503SJoe Perches #define pr_fmt(fmt) "IPv4: " fmt
65afd46503SJoe Perches 
66f4c50d99SHerbert Xu #include <linux/err.h>
671da177e4SLinus Torvalds #include <linux/errno.h>
681da177e4SLinus Torvalds #include <linux/types.h>
691da177e4SLinus Torvalds #include <linux/socket.h>
701da177e4SLinus Torvalds #include <linux/in.h>
711da177e4SLinus Torvalds #include <linux/kernel.h>
72d3fc0353SPaul Gortmaker #include <linux/kmod.h>
731da177e4SLinus Torvalds #include <linux/sched.h>
741da177e4SLinus Torvalds #include <linux/timer.h>
751da177e4SLinus Torvalds #include <linux/string.h>
761da177e4SLinus Torvalds #include <linux/sockios.h>
771da177e4SLinus Torvalds #include <linux/net.h>
784fc268d2SRandy Dunlap #include <linux/capability.h>
791da177e4SLinus Torvalds #include <linux/fcntl.h>
801da177e4SLinus Torvalds #include <linux/mm.h>
811da177e4SLinus Torvalds #include <linux/interrupt.h>
821da177e4SLinus Torvalds #include <linux/stat.h>
831da177e4SLinus Torvalds #include <linux/init.h>
841da177e4SLinus Torvalds #include <linux/poll.h>
851da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
86b3da2cf3SDavid S. Miller #include <linux/random.h>
875a0e3ad6STejun Heo #include <linux/slab.h>
881da177e4SLinus Torvalds 
897c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #include <linux/inet.h>
921da177e4SLinus Torvalds #include <linux/igmp.h>
9314c85021SArnaldo Carvalho de Melo #include <linux/inetdevice.h>
941da177e4SLinus Torvalds #include <linux/netdevice.h>
9573cc19f1SHerbert Xu #include <net/checksum.h>
961da177e4SLinus Torvalds #include <net/ip.h>
971da177e4SLinus Torvalds #include <net/protocol.h>
981da177e4SLinus Torvalds #include <net/arp.h>
991da177e4SLinus Torvalds #include <net/route.h>
1001da177e4SLinus Torvalds #include <net/ip_fib.h>
101295f7324SArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
1021da177e4SLinus Torvalds #include <net/tcp.h>
1031da177e4SLinus Torvalds #include <net/udp.h>
104ba4e58ecSGerrit Renker #include <net/udplite.h>
105c319b4d7SVasiliy Kulikov #include <net/ping.h>
1061da177e4SLinus Torvalds #include <linux/skbuff.h>
1071da177e4SLinus Torvalds #include <net/sock.h>
1081da177e4SLinus Torvalds #include <net/raw.h>
1091da177e4SLinus Torvalds #include <net/icmp.h>
1101da177e4SLinus Torvalds #include <net/inet_common.h>
111045a0fa0SThomas Graf #include <net/ip_tunnels.h>
1121da177e4SLinus Torvalds #include <net/xfrm.h>
1139b4661bdSPavel Emelyanov #include <net/net_namespace.h>
114aebda156SEric Dumazet #include <net/secure_seq.h>
1151da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
1161da177e4SLinus Torvalds #include <linux/mroute.h>
1171da177e4SLinus Torvalds #endif
1183236b004SDavid Ahern #include <net/l3mdev.h>
1191da177e4SLinus Torvalds 
120563e0bb0SYafang Shao #include <trace/events/sock.h>
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /* The inetsw table contains everything that inet_create needs to
1231da177e4SLinus Torvalds  * build a new socket.
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds static struct list_head inetsw[SOCK_MAX];
1261da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw_lock);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* New destruction routine */
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds void inet_sock_destruct(struct sock *sk)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	__skb_queue_purge(&sk->sk_receive_queue);
1358b27dae5SEric Dumazet 	if (sk->sk_rx_skb_cache) {
1368b27dae5SEric Dumazet 		__kfree_skb(sk->sk_rx_skb_cache);
1378b27dae5SEric Dumazet 		sk->sk_rx_skb_cache = NULL;
1388b27dae5SEric Dumazet 	}
1391da177e4SLinus Torvalds 	__skb_queue_purge(&sk->sk_error_queue);
1401da177e4SLinus Torvalds 
14195766fffSHideo Aoki 	sk_mem_reclaim(sk);
14295766fffSHideo Aoki 
1431da177e4SLinus Torvalds 	if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
1443d1427f8SEric Dumazet 		pr_err("Attempt to release TCP socket in state %d %p\n",
1451da177e4SLinus Torvalds 		       sk->sk_state, sk);
1461da177e4SLinus Torvalds 		return;
1471da177e4SLinus Torvalds 	}
1481da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD)) {
1493d1427f8SEric Dumazet 		pr_err("Attempt to release alive inet socket %p\n", sk);
1501da177e4SLinus Torvalds 		return;
1511da177e4SLinus Torvalds 	}
1521da177e4SLinus Torvalds 
153547b792cSIlpo Järvinen 	WARN_ON(atomic_read(&sk->sk_rmem_alloc));
15414afee4bSReshetova, Elena 	WARN_ON(refcount_read(&sk->sk_wmem_alloc));
155547b792cSIlpo Järvinen 	WARN_ON(sk->sk_wmem_queued);
156547b792cSIlpo Järvinen 	WARN_ON(sk->sk_forward_alloc);
1571da177e4SLinus Torvalds 
158f6d8bd05SEric Dumazet 	kfree(rcu_dereference_protected(inet->inet_opt, 1));
1595869b8faSXin Long 	dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1));
16041063e9dSDavid S. Miller 	dst_release(sk->sk_rx_dst);
161e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_dec(sk);
1621da177e4SLinus Torvalds }
1633d1427f8SEric Dumazet EXPORT_SYMBOL(inet_sock_destruct);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds /*
1661da177e4SLinus Torvalds  *	The routines beyond this point handle the behaviour of an AF_INET
1671da177e4SLinus Torvalds  *	socket object. Mostly it punts to the subprotocols of IP to do
1681da177e4SLinus Torvalds  *	the work.
1691da177e4SLinus Torvalds  */
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /*
1721da177e4SLinus Torvalds  *	Automatically bind an unbound socket.
1731da177e4SLinus Torvalds  */
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds static int inet_autobind(struct sock *sk)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct inet_sock *inet;
1781da177e4SLinus Torvalds 	/* We may need to bind the socket. */
1791da177e4SLinus Torvalds 	lock_sock(sk);
1801da177e4SLinus Torvalds 	inet = inet_sk(sk);
181c720c7e8SEric Dumazet 	if (!inet->inet_num) {
1821da177e4SLinus Torvalds 		if (sk->sk_prot->get_port(sk, 0)) {
1831da177e4SLinus Torvalds 			release_sock(sk);
1841da177e4SLinus Torvalds 			return -EAGAIN;
1851da177e4SLinus Torvalds 		}
186c720c7e8SEric Dumazet 		inet->inet_sport = htons(inet->inet_num);
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 	release_sock(sk);
1891da177e4SLinus Torvalds 	return 0;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds  *	Move a socket into listening state.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds int inet_listen(struct socket *sock, int backlog)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
1981da177e4SLinus Torvalds 	unsigned char old_state;
199e1cfcbe8SHaishuang Yan 	int err, tcp_fastopen;
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	lock_sock(sk);
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	err = -EINVAL;
2041da177e4SLinus Torvalds 	if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
2051da177e4SLinus Torvalds 		goto out;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	old_state = sk->sk_state;
2081da177e4SLinus Torvalds 	if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
2091da177e4SLinus Torvalds 		goto out;
2101da177e4SLinus Torvalds 
2111295e2cfSYafang Shao 	sk->sk_max_ack_backlog = backlog;
2121da177e4SLinus Torvalds 	/* Really, if the socket is already in listen state
2131da177e4SLinus Torvalds 	 * we can only allow the backlog to be adjusted.
2141da177e4SLinus Torvalds 	 */
2151da177e4SLinus Torvalds 	if (old_state != TCP_LISTEN) {
216cebc5cbaSYuchung Cheng 		/* Enable TFO w/o requiring TCP_FASTOPEN socket option.
2178336886fSJerry Chu 		 * Note that only TCP sockets (SOCK_STREAM) will reach here.
218cebc5cbaSYuchung Cheng 		 * Also fastopen backlog may already been set via the option
219cebc5cbaSYuchung Cheng 		 * because the socket was in TCP_LISTEN state previously but
220cebc5cbaSYuchung Cheng 		 * was shutdown() rather than close().
2218336886fSJerry Chu 		 */
222e1cfcbe8SHaishuang Yan 		tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
223e1cfcbe8SHaishuang Yan 		if ((tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
224e1cfcbe8SHaishuang Yan 		    (tcp_fastopen & TFO_SERVER_ENABLE) &&
2250536fcc0SEric Dumazet 		    !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
2260536fcc0SEric Dumazet 			fastopen_queue_tune(sk, backlog);
22743713848SHaishuang Yan 			tcp_fastopen_init_key_once(sock_net(sk));
2288336886fSJerry Chu 		}
229cebc5cbaSYuchung Cheng 
23072a3effaSEric Dumazet 		err = inet_csk_listen_start(sk, backlog);
2311da177e4SLinus Torvalds 		if (err)
2321da177e4SLinus Torvalds 			goto out;
233f333ee0cSAndrey Ignatov 		tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL);
2341da177e4SLinus Torvalds 	}
2351da177e4SLinus Torvalds 	err = 0;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds out:
2381da177e4SLinus Torvalds 	release_sock(sk);
2391da177e4SLinus Torvalds 	return err;
2401da177e4SLinus Torvalds }
2413d1427f8SEric Dumazet EXPORT_SYMBOL(inet_listen);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /*
2441da177e4SLinus Torvalds  *	Create an inet socket.
2451da177e4SLinus Torvalds  */
2461da177e4SLinus Torvalds 
2473f378b68SEric Paris static int inet_create(struct net *net, struct socket *sock, int protocol,
2483f378b68SEric Paris 		       int kern)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	struct sock *sk;
2511da177e4SLinus Torvalds 	struct inet_protosw *answer;
2521da177e4SLinus Torvalds 	struct inet_sock *inet;
2531da177e4SLinus Torvalds 	struct proto *answer_prot;
2541da177e4SLinus Torvalds 	unsigned char answer_flags;
255bb97d31fSArnaldo Carvalho de Melo 	int try_loading_module = 0;
25686c8f9d1SHerbert Xu 	int err;
2571da177e4SLinus Torvalds 
25879462ad0SHannes Frederic Sowa 	if (protocol < 0 || protocol >= IPPROTO_MAX)
25979462ad0SHannes Frederic Sowa 		return -EINVAL;
26079462ad0SHannes Frederic Sowa 
2611da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	/* Look for the requested type/protocol pair. */
264bb97d31fSArnaldo Carvalho de Melo lookup_protocol:
26586c8f9d1SHerbert Xu 	err = -ESOCKTNOSUPPORT;
2661da177e4SLinus Torvalds 	rcu_read_lock();
267696adfe8SPaul E. McKenney 	list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
2681da177e4SLinus Torvalds 
269696adfe8SPaul E. McKenney 		err = 0;
2701da177e4SLinus Torvalds 		/* Check the non-wild match. */
2711da177e4SLinus Torvalds 		if (protocol == answer->protocol) {
2721da177e4SLinus Torvalds 			if (protocol != IPPROTO_IP)
2731da177e4SLinus Torvalds 				break;
2741da177e4SLinus Torvalds 		} else {
2751da177e4SLinus Torvalds 			/* Check for the two wild cases. */
2761da177e4SLinus Torvalds 			if (IPPROTO_IP == protocol) {
2771da177e4SLinus Torvalds 				protocol = answer->protocol;
2781da177e4SLinus Torvalds 				break;
2791da177e4SLinus Torvalds 			}
2801da177e4SLinus Torvalds 			if (IPPROTO_IP == answer->protocol)
2811da177e4SLinus Torvalds 				break;
2821da177e4SLinus Torvalds 		}
28386c8f9d1SHerbert Xu 		err = -EPROTONOSUPPORT;
2841da177e4SLinus Torvalds 	}
2851da177e4SLinus Torvalds 
286696adfe8SPaul E. McKenney 	if (unlikely(err)) {
287bb97d31fSArnaldo Carvalho de Melo 		if (try_loading_module < 2) {
288bb97d31fSArnaldo Carvalho de Melo 			rcu_read_unlock();
289bb97d31fSArnaldo Carvalho de Melo 			/*
290bb97d31fSArnaldo Carvalho de Melo 			 * Be more specific, e.g. net-pf-2-proto-132-type-1
291bb97d31fSArnaldo Carvalho de Melo 			 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
292bb97d31fSArnaldo Carvalho de Melo 			 */
293bb97d31fSArnaldo Carvalho de Melo 			if (++try_loading_module == 1)
294bb97d31fSArnaldo Carvalho de Melo 				request_module("net-pf-%d-proto-%d-type-%d",
295bb97d31fSArnaldo Carvalho de Melo 					       PF_INET, protocol, sock->type);
296bb97d31fSArnaldo Carvalho de Melo 			/*
297bb97d31fSArnaldo Carvalho de Melo 			 * Fall back to generic, e.g. net-pf-2-proto-132
298bb97d31fSArnaldo Carvalho de Melo 			 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
299bb97d31fSArnaldo Carvalho de Melo 			 */
300bb97d31fSArnaldo Carvalho de Melo 			else
301bb97d31fSArnaldo Carvalho de Melo 				request_module("net-pf-%d-proto-%d",
302bb97d31fSArnaldo Carvalho de Melo 					       PF_INET, protocol);
303bb97d31fSArnaldo Carvalho de Melo 			goto lookup_protocol;
304bb97d31fSArnaldo Carvalho de Melo 		} else
3051da177e4SLinus Torvalds 			goto out_rcu_unlock;
306bb97d31fSArnaldo Carvalho de Melo 	}
307bb97d31fSArnaldo Carvalho de Melo 
3081da177e4SLinus Torvalds 	err = -EPERM;
30952e804c6SEric W. Biederman 	if (sock->type == SOCK_RAW && !kern &&
31052e804c6SEric W. Biederman 	    !ns_capable(net->user_ns, CAP_NET_RAW))
3111da177e4SLinus Torvalds 		goto out_rcu_unlock;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	sock->ops = answer->ops;
3141da177e4SLinus Torvalds 	answer_prot = answer->prot;
3151da177e4SLinus Torvalds 	answer_flags = answer->flags;
3161da177e4SLinus Torvalds 	rcu_read_unlock();
3171da177e4SLinus Torvalds 
31851456b29SIan Morris 	WARN_ON(!answer_prot->slab);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	err = -ENOBUFS;
32111aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot, kern);
32251456b29SIan Morris 	if (!sk)
3231da177e4SLinus Torvalds 		goto out;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	err = 0;
3261da177e4SLinus Torvalds 	if (INET_PROTOSW_REUSE & answer_flags)
3274a17fd52SPavel Emelyanov 		sk->sk_reuse = SK_CAN_REUSE;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	inet = inet_sk(sk);
330469de9b9SPaul Moore 	inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
3311da177e4SLinus Torvalds 
3327b2ff18eSJiri Olsa 	inet->nodefrag = 0;
3337b2ff18eSJiri Olsa 
3341da177e4SLinus Torvalds 	if (SOCK_RAW == sock->type) {
335c720c7e8SEric Dumazet 		inet->inet_num = protocol;
3361da177e4SLinus Torvalds 		if (IPPROTO_RAW == protocol)
3371da177e4SLinus Torvalds 			inet->hdrincl = 1;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 
340974eda11SHannes Frederic Sowa 	if (net->ipv4.sysctl_ip_no_pmtu_disc)
3411da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_DONT;
3421da177e4SLinus Torvalds 	else
3431da177e4SLinus Torvalds 		inet->pmtudisc = IP_PMTUDISC_WANT;
3441da177e4SLinus Torvalds 
345c720c7e8SEric Dumazet 	inet->inet_id = 0;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	sock_init_data(sock, sk);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	sk->sk_destruct	   = inet_sock_destruct;
3501da177e4SLinus Torvalds 	sk->sk_protocol	   = protocol;
3511da177e4SLinus Torvalds 	sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	inet->uc_ttl	= -1;
3541da177e4SLinus Torvalds 	inet->mc_loop	= 1;
3551da177e4SLinus Torvalds 	inet->mc_ttl	= 1;
356f771bef9SNivedita Singhvi 	inet->mc_all	= 1;
3571da177e4SLinus Torvalds 	inet->mc_index	= 0;
3581da177e4SLinus Torvalds 	inet->mc_list	= NULL;
3594c507d28SJiri Benc 	inet->rcv_tos	= 0;
3601da177e4SLinus Torvalds 
361e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(sk);
3621da177e4SLinus Torvalds 
363c720c7e8SEric Dumazet 	if (inet->inet_num) {
3641da177e4SLinus Torvalds 		/* It assumes that any protocol which allows
3651da177e4SLinus Torvalds 		 * the user to assign a number at socket
3661da177e4SLinus Torvalds 		 * creation time automatically
3671da177e4SLinus Torvalds 		 * shares.
3681da177e4SLinus Torvalds 		 */
369c720c7e8SEric Dumazet 		inet->inet_sport = htons(inet->inet_num);
3701da177e4SLinus Torvalds 		/* Add to protocol hash chains. */
371086c653fSCraig Gallek 		err = sk->sk_prot->hash(sk);
372086c653fSCraig Gallek 		if (err) {
373086c653fSCraig Gallek 			sk_common_release(sk);
374086c653fSCraig Gallek 			goto out;
375086c653fSCraig Gallek 		}
3761da177e4SLinus Torvalds 	}
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if (sk->sk_prot->init) {
3791da177e4SLinus Torvalds 		err = sk->sk_prot->init(sk);
38061023658SDavid Ahern 		if (err) {
3811da177e4SLinus Torvalds 			sk_common_release(sk);
38261023658SDavid Ahern 			goto out;
38361023658SDavid Ahern 		}
38461023658SDavid Ahern 	}
38561023658SDavid Ahern 
38661023658SDavid Ahern 	if (!kern) {
38761023658SDavid Ahern 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
38861023658SDavid Ahern 		if (err) {
38961023658SDavid Ahern 			sk_common_release(sk);
39061023658SDavid Ahern 			goto out;
39161023658SDavid Ahern 		}
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds out:
3941da177e4SLinus Torvalds 	return err;
3951da177e4SLinus Torvalds out_rcu_unlock:
3961da177e4SLinus Torvalds 	rcu_read_unlock();
3971da177e4SLinus Torvalds 	goto out;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /*
4021da177e4SLinus Torvalds  *	The peer socket should always be NULL (or else). When we call this
4031da177e4SLinus Torvalds  *	function we are destroying the object and from then on nobody
4041da177e4SLinus Torvalds  *	should refer to it.
4051da177e4SLinus Torvalds  */
4061da177e4SLinus Torvalds int inet_release(struct socket *sock)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (sk) {
4111da177e4SLinus Torvalds 		long timeout;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		/* Applications forget to leave groups before exiting */
4141da177e4SLinus Torvalds 		ip_mc_drop_socket(sk);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 		/* If linger is set, we don't return until the close
4171da177e4SLinus Torvalds 		 * is complete.  Otherwise we return immediately. The
4181da177e4SLinus Torvalds 		 * actually closing is done the same either way.
4191da177e4SLinus Torvalds 		 *
4201da177e4SLinus Torvalds 		 * If the close is due to the process exiting, we never
4211da177e4SLinus Torvalds 		 * linger..
4221da177e4SLinus Torvalds 		 */
4231da177e4SLinus Torvalds 		timeout = 0;
4241da177e4SLinus Torvalds 		if (sock_flag(sk, SOCK_LINGER) &&
4251da177e4SLinus Torvalds 		    !(current->flags & PF_EXITING))
4261da177e4SLinus Torvalds 			timeout = sk->sk_lingertime;
4271da177e4SLinus Torvalds 		sk->sk_prot->close(sk, timeout);
4282b81f816SJakub Kicinski 		sock->sk = NULL;
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds 	return 0;
4311da177e4SLinus Torvalds }
4323d1427f8SEric Dumazet EXPORT_SYMBOL(inet_release);
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
4351da177e4SLinus Torvalds {
4361da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4371da177e4SLinus Torvalds 	int err;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	/* If the socket has its own bind function then use it. (RAW) */
4401da177e4SLinus Torvalds 	if (sk->sk_prot->bind) {
4413679d585SAndrey Ignatov 		return sk->sk_prot->bind(sk, uaddr, addr_len);
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 	if (addr_len < sizeof(struct sockaddr_in))
4443679d585SAndrey Ignatov 		return -EINVAL;
4451da177e4SLinus Torvalds 
4464fbac77dSAndrey Ignatov 	/* BPF prog is run before any checks are done so that if the prog
4474fbac77dSAndrey Ignatov 	 * changes context in a wrong way it will be caught.
4484fbac77dSAndrey Ignatov 	 */
4494fbac77dSAndrey Ignatov 	err = BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr);
4504fbac77dSAndrey Ignatov 	if (err)
4513679d585SAndrey Ignatov 		return err;
4523679d585SAndrey Ignatov 
4533679d585SAndrey Ignatov 	return __inet_bind(sk, uaddr, addr_len, false, true);
4543679d585SAndrey Ignatov }
4553679d585SAndrey Ignatov EXPORT_SYMBOL(inet_bind);
4563679d585SAndrey Ignatov 
4573679d585SAndrey Ignatov int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
4583679d585SAndrey Ignatov 		bool force_bind_address_no_port, bool with_lock)
4593679d585SAndrey Ignatov {
4603679d585SAndrey Ignatov 	struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
4611da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4621da177e4SLinus Torvalds 	struct net *net = sock_net(sk);
4631da177e4SLinus Torvalds 	unsigned short snum;
4641da177e4SLinus Torvalds 	int chk_addr_ret;
4651da177e4SLinus Torvalds 	u32 tb_id = RT_TABLE_LOCAL;
4661da177e4SLinus Torvalds 	int err;
4671da177e4SLinus Torvalds 
468c349a528SMarcus Meissner 	if (addr->sin_family != AF_INET) {
46929c486dfSEric Dumazet 		/* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
47029c486dfSEric Dumazet 		 * only if s_addr is INADDR_ANY.
47129c486dfSEric Dumazet 		 */
472c349a528SMarcus Meissner 		err = -EAFNOSUPPORT;
47329c486dfSEric Dumazet 		if (addr->sin_family != AF_UNSPEC ||
47429c486dfSEric Dumazet 		    addr->sin_addr.s_addr != htonl(INADDR_ANY))
475d0733d2eSMarcus Meissner 			goto out;
476c349a528SMarcus Meissner 	}
477d0733d2eSMarcus Meissner 
4783236b004SDavid Ahern 	tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id;
47930bbaa19SDavid Ahern 	chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/* Not specified by any standard per-se, however it breaks too
4821da177e4SLinus Torvalds 	 * many applications when removed.  It is unfortunate since
4831da177e4SLinus Torvalds 	 * allowing applications to make a non-local bind solves
4841da177e4SLinus Torvalds 	 * several problems with systems using dynamic addressing.
4851da177e4SLinus Torvalds 	 * (ie. your servers still start up even if your ISDN link
4861da177e4SLinus Torvalds 	 *  is temporarily down)
4871da177e4SLinus Torvalds 	 */
4881da177e4SLinus Torvalds 	err = -EADDRNOTAVAIL;
48983ba4645SVincent Bernat 	if (!inet_can_nonlocal_bind(net, inet) &&
490e6f1cebfSAl Viro 	    addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
4911da177e4SLinus Torvalds 	    chk_addr_ret != RTN_LOCAL &&
4921da177e4SLinus Torvalds 	    chk_addr_ret != RTN_MULTICAST &&
4931da177e4SLinus Torvalds 	    chk_addr_ret != RTN_BROADCAST)
4941da177e4SLinus Torvalds 		goto out;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	snum = ntohs(addr->sin_port);
4971da177e4SLinus Torvalds 	err = -EACCES;
4984548b683SKrister Johansen 	if (snum && snum < inet_prot_sock(net) &&
4993594698aSEric W. Biederman 	    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
5001da177e4SLinus Torvalds 		goto out;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/*      We keep a pair of addresses. rcv_saddr is the one
5031da177e4SLinus Torvalds 	 *      used by hash lookups, and saddr is used for transmit.
5041da177e4SLinus Torvalds 	 *
5051da177e4SLinus Torvalds 	 *      In the BSD API these are the same except where it
5061da177e4SLinus Torvalds 	 *      would be illegal to use them (multicast/broadcast) in
5071da177e4SLinus Torvalds 	 *      which case the sending device address is used.
5081da177e4SLinus Torvalds 	 */
5093679d585SAndrey Ignatov 	if (with_lock)
5101da177e4SLinus Torvalds 		lock_sock(sk);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	/* Check these errors (active socket, double bind). */
5131da177e4SLinus Torvalds 	err = -EINVAL;
514c720c7e8SEric Dumazet 	if (sk->sk_state != TCP_CLOSE || inet->inet_num)
5151da177e4SLinus Torvalds 		goto out_release_sock;
5161da177e4SLinus Torvalds 
517c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
5181da177e4SLinus Torvalds 	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
519c720c7e8SEric Dumazet 		inet->inet_saddr = 0;  /* Use device */
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	/* Make sure we are allowed to bind here. */
522aac3fc32SAndrey Ignatov 	if (snum || !(inet->bind_address_no_port ||
523aac3fc32SAndrey Ignatov 		      force_bind_address_no_port)) {
524aac3fc32SAndrey Ignatov 		if (sk->sk_prot->get_port(sk, snum)) {
525c720c7e8SEric Dumazet 			inet->inet_saddr = inet->inet_rcv_saddr = 0;
5261da177e4SLinus Torvalds 			err = -EADDRINUSE;
5271da177e4SLinus Torvalds 			goto out_release_sock;
5281da177e4SLinus Torvalds 		}
529aac3fc32SAndrey Ignatov 		err = BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk);
530aac3fc32SAndrey Ignatov 		if (err) {
531aac3fc32SAndrey Ignatov 			inet->inet_saddr = inet->inet_rcv_saddr = 0;
532aac3fc32SAndrey Ignatov 			goto out_release_sock;
533aac3fc32SAndrey Ignatov 		}
534aac3fc32SAndrey Ignatov 	}
5351da177e4SLinus Torvalds 
536c720c7e8SEric Dumazet 	if (inet->inet_rcv_saddr)
5371da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
5381da177e4SLinus Torvalds 	if (snum)
5391da177e4SLinus Torvalds 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
540c720c7e8SEric Dumazet 	inet->inet_sport = htons(inet->inet_num);
541c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
542c720c7e8SEric Dumazet 	inet->inet_dport = 0;
5431da177e4SLinus Torvalds 	sk_dst_reset(sk);
5441da177e4SLinus Torvalds 	err = 0;
5451da177e4SLinus Torvalds out_release_sock:
5463679d585SAndrey Ignatov 	if (with_lock)
5471da177e4SLinus Torvalds 		release_sock(sk);
5481da177e4SLinus Torvalds out:
5491da177e4SLinus Torvalds 	return err;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr,
5531da177e4SLinus Torvalds 		       int addr_len, int flags)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
556d74bad4eSAndrey Ignatov 	int err;
5571da177e4SLinus Torvalds 
5586503d961SChangli Gao 	if (addr_len < sizeof(uaddr->sa_family))
5596503d961SChangli Gao 		return -EINVAL;
5601da177e4SLinus Torvalds 	if (uaddr->sa_family == AF_UNSPEC)
5611da177e4SLinus Torvalds 		return sk->sk_prot->disconnect(sk, flags);
5621da177e4SLinus Torvalds 
563d74bad4eSAndrey Ignatov 	if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) {
564d74bad4eSAndrey Ignatov 		err = sk->sk_prot->pre_connect(sk, uaddr, addr_len);
565d74bad4eSAndrey Ignatov 		if (err)
566d74bad4eSAndrey Ignatov 			return err;
567d74bad4eSAndrey Ignatov 	}
568d74bad4eSAndrey Ignatov 
569c720c7e8SEric Dumazet 	if (!inet_sk(sk)->inet_num && inet_autobind(sk))
5701da177e4SLinus Torvalds 		return -EAGAIN;
571e3192690SJoe Perches 	return sk->sk_prot->connect(sk, uaddr, addr_len);
5721da177e4SLinus Torvalds }
5733d1427f8SEric Dumazet EXPORT_SYMBOL(inet_dgram_connect);
5741da177e4SLinus Torvalds 
575783237e8SYuchung Cheng static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias)
5761da177e4SLinus Torvalds {
57714135f30SWANG Cong 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
5781da177e4SLinus Torvalds 
57914135f30SWANG Cong 	add_wait_queue(sk_sleep(sk), &wait);
580783237e8SYuchung Cheng 	sk->sk_write_pending += writebias;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	/* Basic assumption: if someone sets sk->sk_err, he _must_
5831da177e4SLinus Torvalds 	 * change state of the socket from TCP_SYN_*.
5841da177e4SLinus Torvalds 	 * Connect() does not allow to get error notifications
5851da177e4SLinus Torvalds 	 * without closing the socket.
5861da177e4SLinus Torvalds 	 */
5871da177e4SLinus Torvalds 	while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
5881da177e4SLinus Torvalds 		release_sock(sk);
58914135f30SWANG Cong 		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
5901da177e4SLinus Torvalds 		lock_sock(sk);
5911da177e4SLinus Torvalds 		if (signal_pending(current) || !timeo)
5921da177e4SLinus Torvalds 			break;
5931da177e4SLinus Torvalds 	}
59414135f30SWANG Cong 	remove_wait_queue(sk_sleep(sk), &wait);
595783237e8SYuchung Cheng 	sk->sk_write_pending -= writebias;
5961da177e4SLinus Torvalds 	return timeo;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds /*
6001da177e4SLinus Torvalds  *	Connect to a remote host. There is regrettably still a little
6011da177e4SLinus Torvalds  *	TCP 'magic' in here.
6021da177e4SLinus Torvalds  */
603cf60af03SYuchung Cheng int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
6043979ad7eSWilly Tarreau 			  int addr_len, int flags, int is_sendmsg)
6051da177e4SLinus Torvalds {
6061da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6071da177e4SLinus Torvalds 	int err;
6081da177e4SLinus Torvalds 	long timeo;
6091da177e4SLinus Torvalds 
61019f6d3f3SWei Wang 	/*
61119f6d3f3SWei Wang 	 * uaddr can be NULL and addr_len can be 0 if:
61219f6d3f3SWei Wang 	 * sk is a TCP fastopen active socket and
61319f6d3f3SWei Wang 	 * TCP_FASTOPEN_CONNECT sockopt is set and
61419f6d3f3SWei Wang 	 * we already have a valid cookie for this socket.
61519f6d3f3SWei Wang 	 * In this case, user can call write() after connect().
61619f6d3f3SWei Wang 	 * write() will invoke tcp_sendmsg_fastopen() which calls
61719f6d3f3SWei Wang 	 * __inet_stream_connect().
61819f6d3f3SWei Wang 	 */
61919f6d3f3SWei Wang 	if (uaddr) {
6206503d961SChangli Gao 		if (addr_len < sizeof(uaddr->sa_family))
6216503d961SChangli Gao 			return -EINVAL;
6226503d961SChangli Gao 
6231da177e4SLinus Torvalds 		if (uaddr->sa_family == AF_UNSPEC) {
6241da177e4SLinus Torvalds 			err = sk->sk_prot->disconnect(sk, flags);
6251da177e4SLinus Torvalds 			sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
6261da177e4SLinus Torvalds 			goto out;
6271da177e4SLinus Torvalds 		}
62819f6d3f3SWei Wang 	}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	switch (sock->state) {
6311da177e4SLinus Torvalds 	default:
6321da177e4SLinus Torvalds 		err = -EINVAL;
6331da177e4SLinus Torvalds 		goto out;
6341da177e4SLinus Torvalds 	case SS_CONNECTED:
6351da177e4SLinus Torvalds 		err = -EISCONN;
6361da177e4SLinus Torvalds 		goto out;
6371da177e4SLinus Torvalds 	case SS_CONNECTING:
63819f6d3f3SWei Wang 		if (inet_sk(sk)->defer_connect)
6393979ad7eSWilly Tarreau 			err = is_sendmsg ? -EINPROGRESS : -EISCONN;
64019f6d3f3SWei Wang 		else
6411da177e4SLinus Torvalds 			err = -EALREADY;
6421da177e4SLinus Torvalds 		/* Fall out of switch with err, set for this state */
6431da177e4SLinus Torvalds 		break;
6441da177e4SLinus Torvalds 	case SS_UNCONNECTED:
6451da177e4SLinus Torvalds 		err = -EISCONN;
6461da177e4SLinus Torvalds 		if (sk->sk_state != TCP_CLOSE)
6471da177e4SLinus Torvalds 			goto out;
6481da177e4SLinus Torvalds 
649d74bad4eSAndrey Ignatov 		if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) {
650d74bad4eSAndrey Ignatov 			err = sk->sk_prot->pre_connect(sk, uaddr, addr_len);
651d74bad4eSAndrey Ignatov 			if (err)
652d74bad4eSAndrey Ignatov 				goto out;
653d74bad4eSAndrey Ignatov 		}
654d74bad4eSAndrey Ignatov 
6551da177e4SLinus Torvalds 		err = sk->sk_prot->connect(sk, uaddr, addr_len);
6561da177e4SLinus Torvalds 		if (err < 0)
6571da177e4SLinus Torvalds 			goto out;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 		sock->state = SS_CONNECTING;
6601da177e4SLinus Torvalds 
66119f6d3f3SWei Wang 		if (!err && inet_sk(sk)->defer_connect)
66219f6d3f3SWei Wang 			goto out;
66319f6d3f3SWei Wang 
6641da177e4SLinus Torvalds 		/* Just entered SS_CONNECTING state; the only
6651da177e4SLinus Torvalds 		 * difference is that return value in non-blocking
6661da177e4SLinus Torvalds 		 * case is EINPROGRESS, rather than EALREADY.
6671da177e4SLinus Torvalds 		 */
6681da177e4SLinus Torvalds 		err = -EINPROGRESS;
6691da177e4SLinus Torvalds 		break;
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
675783237e8SYuchung Cheng 		int writebias = (sk->sk_protocol == IPPROTO_TCP) &&
676783237e8SYuchung Cheng 				tcp_sk(sk)->fastopen_req &&
677783237e8SYuchung Cheng 				tcp_sk(sk)->fastopen_req->data ? 1 : 0;
678783237e8SYuchung Cheng 
6791da177e4SLinus Torvalds 		/* Error code is set above */
680783237e8SYuchung Cheng 		if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
6811da177e4SLinus Torvalds 			goto out;
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 		err = sock_intr_errno(timeo);
6841da177e4SLinus Torvalds 		if (signal_pending(current))
6851da177e4SLinus Torvalds 			goto out;
6861da177e4SLinus Torvalds 	}
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	/* Connection was closed by RST, timeout, ICMP error
6891da177e4SLinus Torvalds 	 * or another process disconnected us.
6901da177e4SLinus Torvalds 	 */
6911da177e4SLinus Torvalds 	if (sk->sk_state == TCP_CLOSE)
6921da177e4SLinus Torvalds 		goto sock_error;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	/* sk->sk_err may be not zero now, if RECVERR was ordered by user
6951da177e4SLinus Torvalds 	 * and error was received after socket entered established state.
6961da177e4SLinus Torvalds 	 * Hence, it is handled normally after connect() return successfully.
6971da177e4SLinus Torvalds 	 */
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	sock->state = SS_CONNECTED;
7001da177e4SLinus Torvalds 	err = 0;
7011da177e4SLinus Torvalds out:
7021da177e4SLinus Torvalds 	return err;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds sock_error:
7051da177e4SLinus Torvalds 	err = sock_error(sk) ? : -ECONNABORTED;
7061da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
7071da177e4SLinus Torvalds 	if (sk->sk_prot->disconnect(sk, flags))
7081da177e4SLinus Torvalds 		sock->state = SS_DISCONNECTING;
7091da177e4SLinus Torvalds 	goto out;
7101da177e4SLinus Torvalds }
711cf60af03SYuchung Cheng EXPORT_SYMBOL(__inet_stream_connect);
712cf60af03SYuchung Cheng 
713cf60af03SYuchung Cheng int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
714cf60af03SYuchung Cheng 			int addr_len, int flags)
715cf60af03SYuchung Cheng {
716cf60af03SYuchung Cheng 	int err;
717cf60af03SYuchung Cheng 
718cf60af03SYuchung Cheng 	lock_sock(sock->sk);
7193979ad7eSWilly Tarreau 	err = __inet_stream_connect(sock, uaddr, addr_len, flags, 0);
720cf60af03SYuchung Cheng 	release_sock(sock->sk);
721cf60af03SYuchung Cheng 	return err;
722cf60af03SYuchung Cheng }
7233d1427f8SEric Dumazet EXPORT_SYMBOL(inet_stream_connect);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds /*
7261da177e4SLinus Torvalds  *	Accept a pending connection. The TCP layer now gives BSD semantics.
7271da177e4SLinus Torvalds  */
7281da177e4SLinus Torvalds 
729cdfbabfbSDavid Howells int inet_accept(struct socket *sock, struct socket *newsock, int flags,
730cdfbabfbSDavid Howells 		bool kern)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	struct sock *sk1 = sock->sk;
7331da177e4SLinus Torvalds 	int err = -EINVAL;
734cdfbabfbSDavid Howells 	struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err, kern);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	if (!sk2)
7371da177e4SLinus Torvalds 		goto do_err;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	lock_sock(sk2);
7401da177e4SLinus Torvalds 
7411eddceadSEric Dumazet 	sock_rps_record_flow(sk2);
742547b792cSIlpo Järvinen 	WARN_ON(!((1 << sk2->sk_state) &
7438336886fSJerry Chu 		  (TCPF_ESTABLISHED | TCPF_SYN_RECV |
7448336886fSJerry Chu 		  TCPF_CLOSE_WAIT | TCPF_CLOSE)));
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	sock_graft(sk2, newsock);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	newsock->state = SS_CONNECTED;
7491da177e4SLinus Torvalds 	err = 0;
7501da177e4SLinus Torvalds 	release_sock(sk2);
7511da177e4SLinus Torvalds do_err:
7521da177e4SLinus Torvalds 	return err;
7531da177e4SLinus Torvalds }
7543d1427f8SEric Dumazet EXPORT_SYMBOL(inet_accept);
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds /*
7581da177e4SLinus Torvalds  *	This does both peername and sockname.
7591da177e4SLinus Torvalds  */
7601da177e4SLinus Torvalds int inet_getname(struct socket *sock, struct sockaddr *uaddr,
7619b2c45d4SDenys Vlasenko 			int peer)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	struct sock *sk		= sock->sk;
7641da177e4SLinus Torvalds 	struct inet_sock *inet	= inet_sk(sk);
76538bfd8f5SCyrill Gorcunov 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	sin->sin_family = AF_INET;
7681da177e4SLinus Torvalds 	if (peer) {
769c720c7e8SEric Dumazet 		if (!inet->inet_dport ||
7701da177e4SLinus Torvalds 		    (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
7711da177e4SLinus Torvalds 		     peer == 1))
7721da177e4SLinus Torvalds 			return -ENOTCONN;
773c720c7e8SEric Dumazet 		sin->sin_port = inet->inet_dport;
774c720c7e8SEric Dumazet 		sin->sin_addr.s_addr = inet->inet_daddr;
7751da177e4SLinus Torvalds 	} else {
776c720c7e8SEric Dumazet 		__be32 addr = inet->inet_rcv_saddr;
7771da177e4SLinus Torvalds 		if (!addr)
778c720c7e8SEric Dumazet 			addr = inet->inet_saddr;
779c720c7e8SEric Dumazet 		sin->sin_port = inet->inet_sport;
7801da177e4SLinus Torvalds 		sin->sin_addr.s_addr = addr;
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 	memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
7839b2c45d4SDenys Vlasenko 	return sizeof(*sin);
7841da177e4SLinus Torvalds }
7853d1427f8SEric Dumazet EXPORT_SYMBOL(inet_getname);
7861da177e4SLinus Torvalds 
787e4730936SPaolo Abeni int inet_send_prepare(struct sock *sk)
7881da177e4SLinus Torvalds {
789c58dc01bSDavid S. Miller 	sock_rps_record_flow(sk);
790fec5e652STom Herbert 
7911da177e4SLinus Torvalds 	/* We may need to bind the socket. */
7927ba42910SChangli Gao 	if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
7937ba42910SChangli Gao 	    inet_autobind(sk))
7941da177e4SLinus Torvalds 		return -EAGAIN;
7951da177e4SLinus Torvalds 
796e4730936SPaolo Abeni 	return 0;
797e4730936SPaolo Abeni }
798e4730936SPaolo Abeni EXPORT_SYMBOL_GPL(inet_send_prepare);
799e4730936SPaolo Abeni 
800e4730936SPaolo Abeni int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
801e4730936SPaolo Abeni {
802e4730936SPaolo Abeni 	struct sock *sk = sock->sk;
803e4730936SPaolo Abeni 
804e4730936SPaolo Abeni 	if (unlikely(inet_send_prepare(sk)))
805e4730936SPaolo Abeni 		return -EAGAIN;
806e4730936SPaolo Abeni 
8076f24080eSPaolo Abeni 	return INDIRECT_CALL_2(sk->sk_prot->sendmsg, tcp_sendmsg, udp_sendmsg,
8086f24080eSPaolo Abeni 			       sk, msg, size);
8091da177e4SLinus Torvalds }
8103d1427f8SEric Dumazet EXPORT_SYMBOL(inet_sendmsg);
8111da177e4SLinus Torvalds 
8127ba42910SChangli Gao ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
8133d1427f8SEric Dumazet 		      size_t size, int flags)
8141da177e4SLinus Torvalds {
8151da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8161da177e4SLinus Torvalds 
817e4730936SPaolo Abeni 	if (unlikely(inet_send_prepare(sk)))
8181da177e4SLinus Torvalds 		return -EAGAIN;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	if (sk->sk_prot->sendpage)
8211da177e4SLinus Torvalds 		return sk->sk_prot->sendpage(sk, page, offset, size, flags);
8221da177e4SLinus Torvalds 	return sock_no_sendpage(sock, page, offset, size, flags);
8231da177e4SLinus Torvalds }
8247ba42910SChangli Gao EXPORT_SYMBOL(inet_sendpage);
8251da177e4SLinus Torvalds 
8266f24080eSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udp_recvmsg(struct sock *, struct msghdr *,
8276f24080eSPaolo Abeni 					  size_t, int, int, int *));
8281b784140SYing Xue int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
8291b784140SYing Xue 		 int flags)
830fec5e652STom Herbert {
831fec5e652STom Herbert 	struct sock *sk = sock->sk;
832fec5e652STom Herbert 	int addr_len = 0;
833fec5e652STom Herbert 	int err;
834fec5e652STom Herbert 
835e3f2c4a3SSoheil Hassas Yeganeh 	if (likely(!(flags & MSG_ERRQUEUE)))
836c58dc01bSDavid S. Miller 		sock_rps_record_flow(sk);
837fec5e652STom Herbert 
8386f24080eSPaolo Abeni 	err = INDIRECT_CALL_2(sk->sk_prot->recvmsg, tcp_recvmsg, udp_recvmsg,
8396f24080eSPaolo Abeni 			      sk, msg, size, flags & MSG_DONTWAIT,
840fec5e652STom Herbert 			      flags & ~MSG_DONTWAIT, &addr_len);
841fec5e652STom Herbert 	if (err >= 0)
842fec5e652STom Herbert 		msg->msg_namelen = addr_len;
843fec5e652STom Herbert 	return err;
844fec5e652STom Herbert }
845fec5e652STom Herbert EXPORT_SYMBOL(inet_recvmsg);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds int inet_shutdown(struct socket *sock, int how)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8501da177e4SLinus Torvalds 	int err = 0;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	/* This should really check to make sure
8531da177e4SLinus Torvalds 	 * the socket is a TCP socket. (WHY AC...)
8541da177e4SLinus Torvalds 	 */
8551da177e4SLinus Torvalds 	how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
8561da177e4SLinus Torvalds 		       1->2 bit 2 snds.
8571da177e4SLinus Torvalds 		       2->3 */
8581da177e4SLinus Torvalds 	if ((how & ~SHUTDOWN_MASK) || !how)	/* MAXINT->0 */
8591da177e4SLinus Torvalds 		return -EINVAL;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	lock_sock(sk);
8621da177e4SLinus Torvalds 	if (sock->state == SS_CONNECTING) {
8631da177e4SLinus Torvalds 		if ((1 << sk->sk_state) &
8641da177e4SLinus Torvalds 		    (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
8651da177e4SLinus Torvalds 			sock->state = SS_DISCONNECTING;
8661da177e4SLinus Torvalds 		else
8671da177e4SLinus Torvalds 			sock->state = SS_CONNECTED;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	switch (sk->sk_state) {
8711da177e4SLinus Torvalds 	case TCP_CLOSE:
8721da177e4SLinus Torvalds 		err = -ENOTCONN;
8731da177e4SLinus Torvalds 		/* Hack to wake up other listeners, who can poll for
874a9a08845SLinus Torvalds 		   EPOLLHUP, even on eg. unconnected UDP sockets -- RR */
875fcfd6dfaSGustavo A. R. Silva 		/* fall through */
8761da177e4SLinus Torvalds 	default:
8771da177e4SLinus Torvalds 		sk->sk_shutdown |= how;
8781da177e4SLinus Torvalds 		if (sk->sk_prot->shutdown)
8791da177e4SLinus Torvalds 			sk->sk_prot->shutdown(sk, how);
8801da177e4SLinus Torvalds 		break;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	/* Remaining two branches are temporary solution for missing
8831da177e4SLinus Torvalds 	 * close() in multithreaded environment. It is _not_ a good idea,
8841da177e4SLinus Torvalds 	 * but we have no choice until close() is repaired at VFS level.
8851da177e4SLinus Torvalds 	 */
8861da177e4SLinus Torvalds 	case TCP_LISTEN:
8871da177e4SLinus Torvalds 		if (!(how & RCV_SHUTDOWN))
8881da177e4SLinus Torvalds 			break;
889fcfd6dfaSGustavo A. R. Silva 		/* fall through */
8901da177e4SLinus Torvalds 	case TCP_SYN_SENT:
8911da177e4SLinus Torvalds 		err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
8921da177e4SLinus Torvalds 		sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
8931da177e4SLinus Torvalds 		break;
8941da177e4SLinus Torvalds 	}
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	/* Wake up anyone sleeping in poll. */
8971da177e4SLinus Torvalds 	sk->sk_state_change(sk);
8981da177e4SLinus Torvalds 	release_sock(sk);
8991da177e4SLinus Torvalds 	return err;
9001da177e4SLinus Torvalds }
9013d1427f8SEric Dumazet EXPORT_SYMBOL(inet_shutdown);
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds /*
9041da177e4SLinus Torvalds  *	ioctl() calls you can issue on an INET socket. Most of these are
9051da177e4SLinus Torvalds  *	device configuration and stuff and very rarely used. Some ioctls
9061da177e4SLinus Torvalds  *	pass on to the socket itself.
9071da177e4SLinus Torvalds  *
9081da177e4SLinus Torvalds  *	NOTE: I like the idea of a module for the config stuff. ie ifconfig
9091da177e4SLinus Torvalds  *	loads the devconfigure module does its configuring and unloads it.
9101da177e4SLinus Torvalds  *	There's a good 20K of config code hanging around the kernel.
9111da177e4SLinus Torvalds  */
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
9141da177e4SLinus Torvalds {
9151da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
9161da177e4SLinus Torvalds 	int err = 0;
9173b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
91803aef17bSAl Viro 	void __user *p = (void __user *)arg;
91903aef17bSAl Viro 	struct ifreq ifr;
920ca25c300SAl Viro 	struct rtentry rt;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	switch (cmd) {
9231da177e4SLinus Torvalds 	case SIOCADDRT:
9241da177e4SLinus Torvalds 	case SIOCDELRT:
925ca25c300SAl Viro 		if (copy_from_user(&rt, p, sizeof(struct rtentry)))
926ca25c300SAl Viro 			return -EFAULT;
927ca25c300SAl Viro 		err = ip_rt_ioctl(net, cmd, &rt);
928ca25c300SAl Viro 		break;
9291da177e4SLinus Torvalds 	case SIOCRTMSG:
930ca25c300SAl Viro 		err = -EINVAL;
9311da177e4SLinus Torvalds 		break;
9321da177e4SLinus Torvalds 	case SIOCDARP:
9331da177e4SLinus Torvalds 	case SIOCGARP:
9341da177e4SLinus Torvalds 	case SIOCSARP:
935e5b13cb1SDenis V. Lunev 		err = arp_ioctl(net, cmd, (void __user *)arg);
9361da177e4SLinus Torvalds 		break;
9371da177e4SLinus Torvalds 	case SIOCGIFADDR:
9381da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:
9391da177e4SLinus Torvalds 	case SIOCGIFNETMASK:
9401da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:
94103aef17bSAl Viro 	case SIOCGIFPFLAGS:
94203aef17bSAl Viro 		if (copy_from_user(&ifr, p, sizeof(struct ifreq)))
94303aef17bSAl Viro 			return -EFAULT;
94403aef17bSAl Viro 		err = devinet_ioctl(net, cmd, &ifr);
94503aef17bSAl Viro 		if (!err && copy_to_user(p, &ifr, sizeof(struct ifreq)))
94603aef17bSAl Viro 			err = -EFAULT;
94703aef17bSAl Viro 		break;
94803aef17bSAl Viro 
94903aef17bSAl Viro 	case SIOCSIFADDR:
95003aef17bSAl Viro 	case SIOCSIFBRDADDR:
95103aef17bSAl Viro 	case SIOCSIFNETMASK:
9521da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
9531da177e4SLinus Torvalds 	case SIOCSIFPFLAGS:
9541da177e4SLinus Torvalds 	case SIOCSIFFLAGS:
95503aef17bSAl Viro 		if (copy_from_user(&ifr, p, sizeof(struct ifreq)))
95603aef17bSAl Viro 			return -EFAULT;
95703aef17bSAl Viro 		err = devinet_ioctl(net, cmd, &ifr);
9581da177e4SLinus Torvalds 		break;
9591da177e4SLinus Torvalds 	default:
960b5e5fa5eSChristoph Hellwig 		if (sk->sk_prot->ioctl)
961b5e5fa5eSChristoph Hellwig 			err = sk->sk_prot->ioctl(sk, cmd, arg);
962b5e5fa5eSChristoph Hellwig 		else
963b5e5fa5eSChristoph Hellwig 			err = -ENOIOCTLCMD;
9641da177e4SLinus Torvalds 		break;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 	return err;
9671da177e4SLinus Torvalds }
9683d1427f8SEric Dumazet EXPORT_SYMBOL(inet_ioctl);
9691da177e4SLinus Torvalds 
970709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT
971686dc6b6SGerrit Renker static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
972709b46e8SEric W. Biederman {
973709b46e8SEric W. Biederman 	struct sock *sk = sock->sk;
974709b46e8SEric W. Biederman 	int err = -ENOIOCTLCMD;
975709b46e8SEric W. Biederman 
976709b46e8SEric W. Biederman 	if (sk->sk_prot->compat_ioctl)
977709b46e8SEric W. Biederman 		err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
978709b46e8SEric W. Biederman 
979709b46e8SEric W. Biederman 	return err;
980709b46e8SEric W. Biederman }
981709b46e8SEric W. Biederman #endif
982709b46e8SEric W. Biederman 
98390ddc4f0SEric Dumazet const struct proto_ops inet_stream_ops = {
9841da177e4SLinus Torvalds 	.family		   = PF_INET,
9851da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
9861da177e4SLinus Torvalds 	.release	   = inet_release,
9871da177e4SLinus Torvalds 	.bind		   = inet_bind,
9881da177e4SLinus Torvalds 	.connect	   = inet_stream_connect,
9891da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,
9901da177e4SLinus Torvalds 	.accept		   = inet_accept,
9911da177e4SLinus Torvalds 	.getname	   = inet_getname,
992a11e1d43SLinus Torvalds 	.poll		   = tcp_poll,
9931da177e4SLinus Torvalds 	.ioctl		   = inet_ioctl,
994c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
9951da177e4SLinus Torvalds 	.listen		   = inet_listen,
9961da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,
9971da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,
9981da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,
9997ba42910SChangli Gao 	.sendmsg	   = inet_sendmsg,
1000fec5e652STom Herbert 	.recvmsg	   = inet_recvmsg,
100105255b82SEric Dumazet #ifdef CONFIG_MMU
100293ab6cc6SEric Dumazet 	.mmap		   = tcp_mmap,
100305255b82SEric Dumazet #endif
10047ba42910SChangli Gao 	.sendpage	   = inet_sendpage,
10059c55e01cSJens Axboe 	.splice_read	   = tcp_splice_read,
100632035585STom Herbert 	.read_sock	   = tcp_read_sock,
1007306b13ebSTom Herbert 	.sendmsg_locked    = tcp_sendmsg_locked,
1008306b13ebSTom Herbert 	.sendpage_locked   = tcp_sendpage_locked,
100932035585STom Herbert 	.peek_len	   = tcp_peek_len,
10103fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
10113fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_sock_common_setsockopt,
10123fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_sock_common_getsockopt,
1013709b46e8SEric W. Biederman 	.compat_ioctl	   = inet_compat_ioctl,
10143fdadf7dSDmitry Mishin #endif
1015d1361840SEric Dumazet 	.set_rcvlowat	   = tcp_set_rcvlowat,
10161da177e4SLinus Torvalds };
10173d1427f8SEric Dumazet EXPORT_SYMBOL(inet_stream_ops);
10181da177e4SLinus Torvalds 
101990ddc4f0SEric Dumazet const struct proto_ops inet_dgram_ops = {
10201da177e4SLinus Torvalds 	.family		   = PF_INET,
10211da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
10221da177e4SLinus Torvalds 	.release	   = inet_release,
10231da177e4SLinus Torvalds 	.bind		   = inet_bind,
10241da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,
10251da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,
10261da177e4SLinus Torvalds 	.accept		   = sock_no_accept,
10271da177e4SLinus Torvalds 	.getname	   = inet_getname,
1028a11e1d43SLinus Torvalds 	.poll		   = udp_poll,
10291da177e4SLinus Torvalds 	.ioctl		   = inet_ioctl,
1030c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
10311da177e4SLinus Torvalds 	.listen		   = sock_no_listen,
10321da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,
10331da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,
10341da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,
10351da177e4SLinus Torvalds 	.sendmsg	   = inet_sendmsg,
1036fec5e652STom Herbert 	.recvmsg	   = inet_recvmsg,
10371da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
10381da177e4SLinus Torvalds 	.sendpage	   = inet_sendpage,
1039627d2d6bSsamanthakumar 	.set_peek_off	   = sk_set_peek_off,
1040543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
1041543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_sock_common_setsockopt,
1042543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_sock_common_getsockopt,
1043709b46e8SEric W. Biederman 	.compat_ioctl	   = inet_compat_ioctl,
1044543d9cfeSArnaldo Carvalho de Melo #endif
10451da177e4SLinus Torvalds };
10463d1427f8SEric Dumazet EXPORT_SYMBOL(inet_dgram_ops);
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds /*
10491da177e4SLinus Torvalds  * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
1050a11e1d43SLinus Torvalds  * udp_poll
10511da177e4SLinus Torvalds  */
105290ddc4f0SEric Dumazet static const struct proto_ops inet_sockraw_ops = {
10531da177e4SLinus Torvalds 	.family		   = PF_INET,
10541da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
10551da177e4SLinus Torvalds 	.release	   = inet_release,
10561da177e4SLinus Torvalds 	.bind		   = inet_bind,
10571da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,
10581da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,
10591da177e4SLinus Torvalds 	.accept		   = sock_no_accept,
10601da177e4SLinus Torvalds 	.getname	   = inet_getname,
1061a11e1d43SLinus Torvalds 	.poll		   = datagram_poll,
10621da177e4SLinus Torvalds 	.ioctl		   = inet_ioctl,
1063c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
10641da177e4SLinus Torvalds 	.listen		   = sock_no_listen,
10651da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,
10661da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,
10671da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,
10681da177e4SLinus Torvalds 	.sendmsg	   = inet_sendmsg,
1069fec5e652STom Herbert 	.recvmsg	   = inet_recvmsg,
10701da177e4SLinus Torvalds 	.mmap		   = sock_no_mmap,
10711da177e4SLinus Torvalds 	.sendpage	   = inet_sendpage,
1072543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
1073543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_sock_common_setsockopt,
1074543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_sock_common_getsockopt,
1075709b46e8SEric W. Biederman 	.compat_ioctl	   = inet_compat_ioctl,
1076543d9cfeSArnaldo Carvalho de Melo #endif
10771da177e4SLinus Torvalds };
10781da177e4SLinus Torvalds 
1079ec1b4cf7SStephen Hemminger static const struct net_proto_family inet_family_ops = {
10801da177e4SLinus Torvalds 	.family = PF_INET,
10811da177e4SLinus Torvalds 	.create = inet_create,
10821da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
10831da177e4SLinus Torvalds };
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds /* Upon startup we insert all the elements in inetsw_array[] into
10861da177e4SLinus Torvalds  * the linked list inetsw.
10871da177e4SLinus Torvalds  */
10881da177e4SLinus Torvalds static struct inet_protosw inetsw_array[] =
10891da177e4SLinus Torvalds {
10901da177e4SLinus Torvalds 	{
10911da177e4SLinus Torvalds 		.type =       SOCK_STREAM,
10921da177e4SLinus Torvalds 		.protocol =   IPPROTO_TCP,
10931da177e4SLinus Torvalds 		.prot =       &tcp_prot,
10941da177e4SLinus Torvalds 		.ops =        &inet_stream_ops,
1095d83d8461SArnaldo Carvalho de Melo 		.flags =      INET_PROTOSW_PERMANENT |
1096d83d8461SArnaldo Carvalho de Melo 			      INET_PROTOSW_ICSK,
10971da177e4SLinus Torvalds 	},
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	{
11001da177e4SLinus Torvalds 		.type =       SOCK_DGRAM,
11011da177e4SLinus Torvalds 		.protocol =   IPPROTO_UDP,
11021da177e4SLinus Torvalds 		.prot =       &udp_prot,
11031da177e4SLinus Torvalds 		.ops =        &inet_dgram_ops,
11041da177e4SLinus Torvalds 		.flags =      INET_PROTOSW_PERMANENT,
11051da177e4SLinus Torvalds        },
11061da177e4SLinus Torvalds 
1107c319b4d7SVasiliy Kulikov        {
1108c319b4d7SVasiliy Kulikov 		.type =       SOCK_DGRAM,
1109c319b4d7SVasiliy Kulikov 		.protocol =   IPPROTO_ICMP,
1110c319b4d7SVasiliy Kulikov 		.prot =       &ping_prot,
111177d4b1d3SEric Dumazet 		.ops =        &inet_sockraw_ops,
1112c319b4d7SVasiliy Kulikov 		.flags =      INET_PROTOSW_REUSE,
1113c319b4d7SVasiliy Kulikov        },
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds        {
11161da177e4SLinus Torvalds 	       .type =       SOCK_RAW,
11171da177e4SLinus Torvalds 	       .protocol =   IPPROTO_IP,	/* wild card */
11181da177e4SLinus Torvalds 	       .prot =       &raw_prot,
11191da177e4SLinus Torvalds 	       .ops =        &inet_sockraw_ops,
11201da177e4SLinus Torvalds 	       .flags =      INET_PROTOSW_REUSE,
11211da177e4SLinus Torvalds        }
11221da177e4SLinus Torvalds };
11231da177e4SLinus Torvalds 
1124c40f6fffSDenis Cheng #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds void inet_register_protosw(struct inet_protosw *p)
11271da177e4SLinus Torvalds {
11281da177e4SLinus Torvalds 	struct list_head *lh;
11291da177e4SLinus Torvalds 	struct inet_protosw *answer;
11301da177e4SLinus Torvalds 	int protocol = p->protocol;
11311da177e4SLinus Torvalds 	struct list_head *last_perm;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 	spin_lock_bh(&inetsw_lock);
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	if (p->type >= SOCK_MAX)
11361da177e4SLinus Torvalds 		goto out_illegal;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	/* If we are trying to override a permanent protocol, bail. */
11391da177e4SLinus Torvalds 	last_perm = &inetsw[p->type];
11401da177e4SLinus Torvalds 	list_for_each(lh, &inetsw[p->type]) {
11411da177e4SLinus Torvalds 		answer = list_entry(lh, struct inet_protosw, list);
11421da177e4SLinus Torvalds 		/* Check only the non-wild match. */
1143f6c53334SJunwei Zhang 		if ((INET_PROTOSW_PERMANENT & answer->flags) == 0)
11441da177e4SLinus Torvalds 			break;
1145f6c53334SJunwei Zhang 		if (protocol == answer->protocol)
1146f6c53334SJunwei Zhang 			goto out_permanent;
11471da177e4SLinus Torvalds 		last_perm = lh;
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	/* Add the new entry after the last permanent entry if any, so that
11511da177e4SLinus Torvalds 	 * the new entry does not override a permanent entry when matched with
11521da177e4SLinus Torvalds 	 * a wild-card protocol. But it is allowed to override any existing
11531da177e4SLinus Torvalds 	 * non-permanent entry.  This means that when we remove this entry, the
11541da177e4SLinus Torvalds 	 * system automatically returns to the old behavior.
11551da177e4SLinus Torvalds 	 */
11561da177e4SLinus Torvalds 	list_add_rcu(&p->list, last_perm);
11571da177e4SLinus Torvalds out:
11581da177e4SLinus Torvalds 	spin_unlock_bh(&inetsw_lock);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	return;
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds out_permanent:
1163058bd4d2SJoe Perches 	pr_err("Attempt to override permanent protocol %d\n", protocol);
11641da177e4SLinus Torvalds 	goto out;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds out_illegal:
1167058bd4d2SJoe Perches 	pr_err("Ignoring attempt to register invalid socket type %d\n",
11681da177e4SLinus Torvalds 	       p->type);
11691da177e4SLinus Torvalds 	goto out;
11701da177e4SLinus Torvalds }
11713d1427f8SEric Dumazet EXPORT_SYMBOL(inet_register_protosw);
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds void inet_unregister_protosw(struct inet_protosw *p)
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds 	if (INET_PROTOSW_PERMANENT & p->flags) {
1176058bd4d2SJoe Perches 		pr_err("Attempt to unregister permanent protocol %d\n",
11771da177e4SLinus Torvalds 		       p->protocol);
11781da177e4SLinus Torvalds 	} else {
11791da177e4SLinus Torvalds 		spin_lock_bh(&inetsw_lock);
11801da177e4SLinus Torvalds 		list_del_rcu(&p->list);
11811da177e4SLinus Torvalds 		spin_unlock_bh(&inetsw_lock);
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 		synchronize_net();
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds }
11863d1427f8SEric Dumazet EXPORT_SYMBOL(inet_unregister_protosw);
11871da177e4SLinus Torvalds 
118832519f11SArnaldo Carvalho de Melo static int inet_sk_reselect_saddr(struct sock *sk)
118932519f11SArnaldo Carvalho de Melo {
119032519f11SArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
1191c720c7e8SEric Dumazet 	__be32 old_saddr = inet->inet_saddr;
1192c720c7e8SEric Dumazet 	__be32 daddr = inet->inet_daddr;
11936e869138SDavid S. Miller 	struct flowi4 *fl4;
1194b23dd4feSDavid S. Miller 	struct rtable *rt;
1195b23dd4feSDavid S. Miller 	__be32 new_saddr;
1196f6d8bd05SEric Dumazet 	struct ip_options_rcu *inet_opt;
119732519f11SArnaldo Carvalho de Melo 
1198f6d8bd05SEric Dumazet 	inet_opt = rcu_dereference_protected(inet->inet_opt,
11991e1d04e6SHannes Frederic Sowa 					     lockdep_sock_is_held(sk));
1200f6d8bd05SEric Dumazet 	if (inet_opt && inet_opt->opt.srr)
1201f6d8bd05SEric Dumazet 		daddr = inet_opt->opt.faddr;
120232519f11SArnaldo Carvalho de Melo 
120332519f11SArnaldo Carvalho de Melo 	/* Query new route. */
12046e869138SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
12056e869138SDavid S. Miller 	rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
1206b23dd4feSDavid S. Miller 			      sk->sk_bound_dev_if, sk->sk_protocol,
12070e0d44abSSteffen Klassert 			      inet->inet_sport, inet->inet_dport, sk);
1208b23dd4feSDavid S. Miller 	if (IS_ERR(rt))
1209b23dd4feSDavid S. Miller 		return PTR_ERR(rt);
121032519f11SArnaldo Carvalho de Melo 
1211d8d1f30bSChangli Gao 	sk_setup_caps(sk, &rt->dst);
121232519f11SArnaldo Carvalho de Melo 
12136e869138SDavid S. Miller 	new_saddr = fl4->saddr;
121432519f11SArnaldo Carvalho de Melo 
121532519f11SArnaldo Carvalho de Melo 	if (new_saddr == old_saddr)
121632519f11SArnaldo Carvalho de Melo 		return 0;
121732519f11SArnaldo Carvalho de Melo 
1218287b7f38SNikolay Borisov 	if (sock_net(sk)->ipv4.sysctl_ip_dynaddr > 1) {
1219058bd4d2SJoe Perches 		pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
1220673d57e7SHarvey Harrison 			__func__, &old_saddr, &new_saddr);
122132519f11SArnaldo Carvalho de Melo 	}
122232519f11SArnaldo Carvalho de Melo 
1223c720c7e8SEric Dumazet 	inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
122432519f11SArnaldo Carvalho de Melo 
122532519f11SArnaldo Carvalho de Melo 	/*
122632519f11SArnaldo Carvalho de Melo 	 * XXX The only one ugly spot where we need to
122732519f11SArnaldo Carvalho de Melo 	 * XXX really change the sockets identity after
122832519f11SArnaldo Carvalho de Melo 	 * XXX it has entered the hashes. -DaveM
122932519f11SArnaldo Carvalho de Melo 	 *
123032519f11SArnaldo Carvalho de Melo 	 * Besides that, it does not check for connection
123132519f11SArnaldo Carvalho de Melo 	 * uniqueness. Wait for troubles.
123232519f11SArnaldo Carvalho de Melo 	 */
1233086c653fSCraig Gallek 	return __sk_prot_rehash(sk);
123432519f11SArnaldo Carvalho de Melo }
123532519f11SArnaldo Carvalho de Melo 
123632519f11SArnaldo Carvalho de Melo int inet_sk_rebuild_header(struct sock *sk)
123732519f11SArnaldo Carvalho de Melo {
123832519f11SArnaldo Carvalho de Melo 	struct inet_sock *inet = inet_sk(sk);
123932519f11SArnaldo Carvalho de Melo 	struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
12403ca3c68eSAl Viro 	__be32 daddr;
1241f6d8bd05SEric Dumazet 	struct ip_options_rcu *inet_opt;
12426e869138SDavid S. Miller 	struct flowi4 *fl4;
124332519f11SArnaldo Carvalho de Melo 	int err;
124432519f11SArnaldo Carvalho de Melo 
124532519f11SArnaldo Carvalho de Melo 	/* Route is OK, nothing to do. */
124632519f11SArnaldo Carvalho de Melo 	if (rt)
124732519f11SArnaldo Carvalho de Melo 		return 0;
124832519f11SArnaldo Carvalho de Melo 
124932519f11SArnaldo Carvalho de Melo 	/* Reroute. */
1250f6d8bd05SEric Dumazet 	rcu_read_lock();
1251f6d8bd05SEric Dumazet 	inet_opt = rcu_dereference(inet->inet_opt);
1252c720c7e8SEric Dumazet 	daddr = inet->inet_daddr;
1253f6d8bd05SEric Dumazet 	if (inet_opt && inet_opt->opt.srr)
1254f6d8bd05SEric Dumazet 		daddr = inet_opt->opt.faddr;
1255f6d8bd05SEric Dumazet 	rcu_read_unlock();
12566e869138SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
12576e869138SDavid S. Miller 	rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
125878fbfd8aSDavid S. Miller 				   inet->inet_dport, inet->inet_sport,
125978fbfd8aSDavid S. Miller 				   sk->sk_protocol, RT_CONN_FLAGS(sk),
126078fbfd8aSDavid S. Miller 				   sk->sk_bound_dev_if);
1261b23dd4feSDavid S. Miller 	if (!IS_ERR(rt)) {
1262b23dd4feSDavid S. Miller 		err = 0;
1263d8d1f30bSChangli Gao 		sk_setup_caps(sk, &rt->dst);
1264b23dd4feSDavid S. Miller 	} else {
1265b23dd4feSDavid S. Miller 		err = PTR_ERR(rt);
1266b23dd4feSDavid S. Miller 
126732519f11SArnaldo Carvalho de Melo 		/* Routing failed... */
126832519f11SArnaldo Carvalho de Melo 		sk->sk_route_caps = 0;
126932519f11SArnaldo Carvalho de Melo 		/*
127032519f11SArnaldo Carvalho de Melo 		 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
127132519f11SArnaldo Carvalho de Melo 		 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
127232519f11SArnaldo Carvalho de Melo 		 */
1273287b7f38SNikolay Borisov 		if (!sock_net(sk)->ipv4.sysctl_ip_dynaddr ||
127432519f11SArnaldo Carvalho de Melo 		    sk->sk_state != TCP_SYN_SENT ||
127532519f11SArnaldo Carvalho de Melo 		    (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
127632519f11SArnaldo Carvalho de Melo 		    (err = inet_sk_reselect_saddr(sk)) != 0)
127732519f11SArnaldo Carvalho de Melo 			sk->sk_err_soft = -err;
127832519f11SArnaldo Carvalho de Melo 	}
127932519f11SArnaldo Carvalho de Melo 
128032519f11SArnaldo Carvalho de Melo 	return err;
128132519f11SArnaldo Carvalho de Melo }
128232519f11SArnaldo Carvalho de Melo EXPORT_SYMBOL(inet_sk_rebuild_header);
128332519f11SArnaldo Carvalho de Melo 
1284563e0bb0SYafang Shao void inet_sk_set_state(struct sock *sk, int state)
1285563e0bb0SYafang Shao {
1286563e0bb0SYafang Shao 	trace_inet_sock_set_state(sk, sk->sk_state, state);
1287563e0bb0SYafang Shao 	sk->sk_state = state;
1288563e0bb0SYafang Shao }
1289563e0bb0SYafang Shao EXPORT_SYMBOL(inet_sk_set_state);
1290563e0bb0SYafang Shao 
1291563e0bb0SYafang Shao void inet_sk_state_store(struct sock *sk, int newstate)
1292563e0bb0SYafang Shao {
1293563e0bb0SYafang Shao 	trace_inet_sock_set_state(sk, sk->sk_state, newstate);
1294563e0bb0SYafang Shao 	smp_store_release(&sk->sk_state, newstate);
1295563e0bb0SYafang Shao }
1296563e0bb0SYafang Shao 
1297b8921ca8STom Herbert struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1298c8f44affSMichał Mirosław 				 netdev_features_t features)
1299f4c50d99SHerbert Xu {
13000c19f846SWillem de Bruijn 	bool udpfrag = false, fixedid = false, gso_partial, encap;
1301f4c50d99SHerbert Xu 	struct sk_buff *segs = ERR_PTR(-EINVAL);
1302bca49f84SVlad Yasevich 	const struct net_offload *ops;
13030c19f846SWillem de Bruijn 	unsigned int offset = 0;
1304f9242b6bSDavid S. Miller 	struct iphdr *iph;
1305802ab55aSAlexander Duyck 	int proto, tot_len;
13063347c960SEric Dumazet 	int nhoff;
1307f4c50d99SHerbert Xu 	int ihl;
1308f4c50d99SHerbert Xu 	int id;
1309f4c50d99SHerbert Xu 
13103347c960SEric Dumazet 	skb_reset_network_header(skb);
13113347c960SEric Dumazet 	nhoff = skb_network_header(skb) - skb_mac_header(skb);
1312bbcf467dSHerbert Xu 	if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1313f4c50d99SHerbert Xu 		goto out;
1314f4c50d99SHerbert Xu 
1315eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
1316f4c50d99SHerbert Xu 	ihl = iph->ihl * 4;
1317f4c50d99SHerbert Xu 	if (ihl < sizeof(*iph))
1318f4c50d99SHerbert Xu 		goto out;
1319f4c50d99SHerbert Xu 
132047d27aadSEric Dumazet 	id = ntohs(iph->id);
132147d27aadSEric Dumazet 	proto = iph->protocol;
132247d27aadSEric Dumazet 
132347d27aadSEric Dumazet 	/* Warning: after this point, iph might be no longer valid */
1324bbcf467dSHerbert Xu 	if (unlikely(!pskb_may_pull(skb, ihl)))
1325f4c50d99SHerbert Xu 		goto out;
132647d27aadSEric Dumazet 	__skb_pull(skb, ihl);
1327f4c50d99SHerbert Xu 
13288c3a897bSEric Dumazet 	encap = SKB_GSO_CB(skb)->encap_level > 0;
13298c3a897bSEric Dumazet 	if (encap)
13301e16aa3dSFlorian Westphal 		features &= skb->dev->hw_enc_features;
13313347c960SEric Dumazet 	SKB_GSO_CB(skb)->encap_level += ihl;
133273136267SPravin B Shelar 
1333badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
133447d27aadSEric Dumazet 
1335f4c50d99SHerbert Xu 	segs = ERR_PTR(-EPROTONOSUPPORT);
1336f4c50d99SHerbert Xu 
1337cbc53e08SAlexander Duyck 	if (!skb->encapsulation || encap) {
13380c19f846SWillem de Bruijn 		udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
1339cbc53e08SAlexander Duyck 		fixedid = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID);
1340cbc53e08SAlexander Duyck 
1341cbc53e08SAlexander Duyck 		/* fixed ID is invalid if DF bit is not set */
1342a5108878SArnaldo Carvalho de Melo 		if (fixedid && !(ip_hdr(skb)->frag_off & htons(IP_DF)))
1343cbc53e08SAlexander Duyck 			goto out;
1344cbc53e08SAlexander Duyck 	}
1345dcd60771SEric Dumazet 
1346bca49f84SVlad Yasevich 	ops = rcu_dereference(inet_offloads[proto]);
1347f191a1d1SVlad Yasevich 	if (likely(ops && ops->callbacks.gso_segment))
1348f191a1d1SVlad Yasevich 		segs = ops->callbacks.gso_segment(skb, features);
1349f4c50d99SHerbert Xu 
135050c3a487SYOSHIFUJI Hideaki / 吉藤英明 	if (IS_ERR_OR_NULL(segs))
1351f4c50d99SHerbert Xu 		goto out;
1352f4c50d99SHerbert Xu 
135307b26c94SSteffen Klassert 	gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
135407b26c94SSteffen Klassert 
1355f4c50d99SHerbert Xu 	skb = segs;
1356f4c50d99SHerbert Xu 	do {
13573347c960SEric Dumazet 		iph = (struct iphdr *)(skb_mac_header(skb) + nhoff);
13580c19f846SWillem de Bruijn 		if (udpfrag) {
13590c19f846SWillem de Bruijn 			iph->frag_off = htons(offset >> 3);
13600c19f846SWillem de Bruijn 			if (skb->next)
13610c19f846SWillem de Bruijn 				iph->frag_off |= htons(IP_MF);
13620c19f846SWillem de Bruijn 			offset += skb->len - nhoff - ihl;
13630c19f846SWillem de Bruijn 			tot_len = skb->len - nhoff;
13640c19f846SWillem de Bruijn 		} else if (skb_is_gso(skb)) {
1365802ab55aSAlexander Duyck 			if (!fixedid) {
1366802ab55aSAlexander Duyck 				iph->id = htons(id);
1367802ab55aSAlexander Duyck 				id += skb_shinfo(skb)->gso_segs;
1368490ab081SPravin B Shelar 			}
136907b26c94SSteffen Klassert 
137007b26c94SSteffen Klassert 			if (gso_partial)
1371802ab55aSAlexander Duyck 				tot_len = skb_shinfo(skb)->gso_size +
1372802ab55aSAlexander Duyck 					  SKB_GSO_CB(skb)->data_offset +
1373802ab55aSAlexander Duyck 					  skb->head - (unsigned char *)iph;
137407b26c94SSteffen Klassert 			else
137507b26c94SSteffen Klassert 				tot_len = skb->len - nhoff;
1376802ab55aSAlexander Duyck 		} else {
1377802ab55aSAlexander Duyck 			if (!fixedid)
1378802ab55aSAlexander Duyck 				iph->id = htons(id++);
1379802ab55aSAlexander Duyck 			tot_len = skb->len - nhoff;
1380802ab55aSAlexander Duyck 		}
1381802ab55aSAlexander Duyck 		iph->tot_len = htons(tot_len);
138247d27aadSEric Dumazet 		ip_send_check(iph);
13838c3a897bSEric Dumazet 		if (encap)
13843347c960SEric Dumazet 			skb_reset_inner_headers(skb);
13853347c960SEric Dumazet 		skb->network_header = (u8 *)iph - skb->head;
1386c56cae23SToke Høiland-Jørgensen 		skb_reset_mac_len(skb);
1387f4c50d99SHerbert Xu 	} while ((skb = skb->next));
1388f4c50d99SHerbert Xu 
1389f4c50d99SHerbert Xu out:
1390f4c50d99SHerbert Xu 	return segs;
1391f4c50d99SHerbert Xu }
1392b8921ca8STom Herbert EXPORT_SYMBOL(inet_gso_segment);
1393f4c50d99SHerbert Xu 
1394418e897eSWillem de Bruijn static struct sk_buff *ipip_gso_segment(struct sk_buff *skb,
1395418e897eSWillem de Bruijn 					netdev_features_t features)
1396418e897eSWillem de Bruijn {
1397418e897eSWillem de Bruijn 	if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP4))
1398418e897eSWillem de Bruijn 		return ERR_PTR(-EINVAL);
1399418e897eSWillem de Bruijn 
1400418e897eSWillem de Bruijn 	return inet_gso_segment(skb, features);
1401418e897eSWillem de Bruijn }
1402418e897eSWillem de Bruijn 
1403028e0a47SPaolo Abeni INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp4_gro_receive(struct list_head *,
1404028e0a47SPaolo Abeni 							   struct sk_buff *));
1405028e0a47SPaolo Abeni INDIRECT_CALLABLE_DECLARE(struct sk_buff *udp4_gro_receive(struct list_head *,
1406028e0a47SPaolo Abeni 							   struct sk_buff *));
1407d4546c25SDavid Miller struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb)
140873cc19f1SHerbert Xu {
1409bca49f84SVlad Yasevich 	const struct net_offload *ops;
1410d4546c25SDavid Miller 	struct sk_buff *pp = NULL;
1411b71d1d42SEric Dumazet 	const struct iphdr *iph;
1412d4546c25SDavid Miller 	struct sk_buff *p;
1413a5b1cf28SHerbert Xu 	unsigned int hlen;
1414a5b1cf28SHerbert Xu 	unsigned int off;
14151075f3f6SHerbert Xu 	unsigned int id;
141673cc19f1SHerbert Xu 	int flush = 1;
141773cc19f1SHerbert Xu 	int proto;
141873cc19f1SHerbert Xu 
1419a5b1cf28SHerbert Xu 	off = skb_gro_offset(skb);
1420a5b1cf28SHerbert Xu 	hlen = off + sizeof(*iph);
1421a5b1cf28SHerbert Xu 	iph = skb_gro_header_fast(skb, off);
1422a5b1cf28SHerbert Xu 	if (skb_gro_header_hard(skb, hlen)) {
1423a5b1cf28SHerbert Xu 		iph = skb_gro_header_slow(skb, hlen, off);
142486911732SHerbert Xu 		if (unlikely(!iph))
142573cc19f1SHerbert Xu 			goto out;
1426a5b1cf28SHerbert Xu 	}
142773cc19f1SHerbert Xu 
1428f9242b6bSDavid S. Miller 	proto = iph->protocol;
142973cc19f1SHerbert Xu 
143073cc19f1SHerbert Xu 	rcu_read_lock();
1431bca49f84SVlad Yasevich 	ops = rcu_dereference(inet_offloads[proto]);
1432f191a1d1SVlad Yasevich 	if (!ops || !ops->callbacks.gro_receive)
143373cc19f1SHerbert Xu 		goto out_unlock;
143473cc19f1SHerbert Xu 
1435a5ad24beSHerbert Xu 	if (*(u8 *)iph != 0x45)
143673cc19f1SHerbert Xu 		goto out_unlock;
143773cc19f1SHerbert Xu 
14389b83e031SSteffen Klassert 	if (ip_is_fragment(iph))
14399b83e031SSteffen Klassert 		goto out_unlock;
14409b83e031SSteffen Klassert 
1441a9e050f4SEric Dumazet 	if (unlikely(ip_fast_csum((u8 *)iph, 5)))
144273cc19f1SHerbert Xu 		goto out_unlock;
144373cc19f1SHerbert Xu 
14440eae88f3SEric Dumazet 	id = ntohl(*(__be32 *)&iph->id);
1445db8caf3dSEric Dumazet 	flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF));
14461075f3f6SHerbert Xu 	id >>= 16;
144773cc19f1SHerbert Xu 
1448d4546c25SDavid Miller 	list_for_each_entry(p, head, list) {
144973cc19f1SHerbert Xu 		struct iphdr *iph2;
14501530545eSAlexander Duyck 		u16 flush_id;
145173cc19f1SHerbert Xu 
145273cc19f1SHerbert Xu 		if (!NAPI_GRO_CB(p)->same_flow)
145373cc19f1SHerbert Xu 			continue;
145473cc19f1SHerbert Xu 
1455299603e8SJerry Chu 		iph2 = (struct iphdr *)(p->data + off);
1456299603e8SJerry Chu 		/* The above works because, with the exception of the top
1457299603e8SJerry Chu 		 * (inner most) layer, we only aggregate pkts with the same
1458299603e8SJerry Chu 		 * hdr length so all the hdrs we'll need to verify will start
1459299603e8SJerry Chu 		 * at the same offset.
1460299603e8SJerry Chu 		 */
1461a5ad24beSHerbert Xu 		if ((iph->protocol ^ iph2->protocol) |
14620eae88f3SEric Dumazet 		    ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
14630eae88f3SEric Dumazet 		    ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
146473cc19f1SHerbert Xu 			NAPI_GRO_CB(p)->same_flow = 0;
146573cc19f1SHerbert Xu 			continue;
146673cc19f1SHerbert Xu 		}
146773cc19f1SHerbert Xu 
146873cc19f1SHerbert Xu 		/* All fields must match except length and checksum. */
146973cc19f1SHerbert Xu 		NAPI_GRO_CB(p)->flush |=
1470a5ad24beSHerbert Xu 			(iph->ttl ^ iph2->ttl) |
1471a9e050f4SEric Dumazet 			(iph->tos ^ iph2->tos) |
1472bf5a755fSJerry Chu 			((iph->frag_off ^ iph2->frag_off) & htons(IP_DF));
147373cc19f1SHerbert Xu 
147473cc19f1SHerbert Xu 		NAPI_GRO_CB(p)->flush |= flush;
14751530545eSAlexander Duyck 
14761530545eSAlexander Duyck 		/* We need to store of the IP ID check to be included later
14771530545eSAlexander Duyck 		 * when we can verify that this packet does in fact belong
14781530545eSAlexander Duyck 		 * to a given flow.
14791530545eSAlexander Duyck 		 */
14801530545eSAlexander Duyck 		flush_id = (u16)(id - ntohs(iph2->id));
14811530545eSAlexander Duyck 
14821530545eSAlexander Duyck 		/* This bit of code makes it much easier for us to identify
14831530545eSAlexander Duyck 		 * the cases where we are doing atomic vs non-atomic IP ID
14841530545eSAlexander Duyck 		 * checks.  Specifically an atomic check can return IP ID
14851530545eSAlexander Duyck 		 * values 0 - 0xFFFF, while a non-atomic check can only
14861530545eSAlexander Duyck 		 * return 0 or 0xFFFF.
14871530545eSAlexander Duyck 		 */
14881530545eSAlexander Duyck 		if (!NAPI_GRO_CB(p)->is_atomic ||
14891530545eSAlexander Duyck 		    !(iph->frag_off & htons(IP_DF))) {
14901530545eSAlexander Duyck 			flush_id ^= NAPI_GRO_CB(p)->count;
14911530545eSAlexander Duyck 			flush_id = flush_id ? 0xFFFF : 0;
149273cc19f1SHerbert Xu 		}
149373cc19f1SHerbert Xu 
14941530545eSAlexander Duyck 		/* If the previous IP ID value was based on an atomic
14951530545eSAlexander Duyck 		 * datagram we can overwrite the value and ignore it.
14961530545eSAlexander Duyck 		 */
14971530545eSAlexander Duyck 		if (NAPI_GRO_CB(skb)->is_atomic)
14981530545eSAlexander Duyck 			NAPI_GRO_CB(p)->flush_id = flush_id;
14991530545eSAlexander Duyck 		else
15001530545eSAlexander Duyck 			NAPI_GRO_CB(p)->flush_id |= flush_id;
15011530545eSAlexander Duyck 	}
15021530545eSAlexander Duyck 
15031530545eSAlexander Duyck 	NAPI_GRO_CB(skb)->is_atomic = !!(iph->frag_off & htons(IP_DF));
150473cc19f1SHerbert Xu 	NAPI_GRO_CB(skb)->flush |= flush;
1505299603e8SJerry Chu 	skb_set_network_header(skb, off);
1506299603e8SJerry Chu 	/* The above will be needed by the transport layer if there is one
1507299603e8SJerry Chu 	 * immediately following this IP hdr.
1508299603e8SJerry Chu 	 */
1509299603e8SJerry Chu 
15102c804d0fSEric Dumazet 	/* Note : No need to call skb_gro_postpull_rcsum() here,
15112c804d0fSEric Dumazet 	 * as we already checked checksum over ipv4 header was 0
15122c804d0fSEric Dumazet 	 */
151386911732SHerbert Xu 	skb_gro_pull(skb, sizeof(*iph));
151486911732SHerbert Xu 	skb_set_transport_header(skb, skb_gro_offset(skb));
151573cc19f1SHerbert Xu 
1516028e0a47SPaolo Abeni 	pp = indirect_call_gro_receive(tcp4_gro_receive, udp4_gro_receive,
1517028e0a47SPaolo Abeni 				       ops->callbacks.gro_receive, head, skb);
151873cc19f1SHerbert Xu 
151973cc19f1SHerbert Xu out_unlock:
152073cc19f1SHerbert Xu 	rcu_read_unlock();
152173cc19f1SHerbert Xu 
152273cc19f1SHerbert Xu out:
15235f114163SSteffen Klassert 	skb_gro_flush_final(skb, pp, flush);
152473cc19f1SHerbert Xu 
152573cc19f1SHerbert Xu 	return pp;
152673cc19f1SHerbert Xu }
1527b8921ca8STom Herbert EXPORT_SYMBOL(inet_gro_receive);
152873cc19f1SHerbert Xu 
1529d4546c25SDavid Miller static struct sk_buff *ipip_gro_receive(struct list_head *head,
1530fac8e0f5SJesse Gross 					struct sk_buff *skb)
1531fac8e0f5SJesse Gross {
1532fac8e0f5SJesse Gross 	if (NAPI_GRO_CB(skb)->encap_mark) {
1533fac8e0f5SJesse Gross 		NAPI_GRO_CB(skb)->flush = 1;
1534fac8e0f5SJesse Gross 		return NULL;
1535fac8e0f5SJesse Gross 	}
1536fac8e0f5SJesse Gross 
1537fac8e0f5SJesse Gross 	NAPI_GRO_CB(skb)->encap_mark = 1;
1538fac8e0f5SJesse Gross 
1539fac8e0f5SJesse Gross 	return inet_gro_receive(head, skb);
1540fac8e0f5SJesse Gross }
1541fac8e0f5SJesse Gross 
1542822c8685SDeepa Dinamani #define SECONDS_PER_DAY	86400
1543822c8685SDeepa Dinamani 
1544822c8685SDeepa Dinamani /* inet_current_timestamp - Return IP network timestamp
1545822c8685SDeepa Dinamani  *
1546822c8685SDeepa Dinamani  * Return milliseconds since midnight in network byte order.
1547822c8685SDeepa Dinamani  */
1548822c8685SDeepa Dinamani __be32 inet_current_timestamp(void)
1549822c8685SDeepa Dinamani {
1550822c8685SDeepa Dinamani 	u32 secs;
1551822c8685SDeepa Dinamani 	u32 msecs;
1552822c8685SDeepa Dinamani 	struct timespec64 ts;
1553822c8685SDeepa Dinamani 
1554822c8685SDeepa Dinamani 	ktime_get_real_ts64(&ts);
1555822c8685SDeepa Dinamani 
1556822c8685SDeepa Dinamani 	/* Get secs since midnight. */
1557822c8685SDeepa Dinamani 	(void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs);
1558822c8685SDeepa Dinamani 	/* Convert to msecs. */
1559822c8685SDeepa Dinamani 	msecs = secs * MSEC_PER_SEC;
1560822c8685SDeepa Dinamani 	/* Convert nsec to msec. */
1561822c8685SDeepa Dinamani 	msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC;
1562822c8685SDeepa Dinamani 
1563822c8685SDeepa Dinamani 	/* Convert to network byte order. */
15643ba9d300SDeepa Dinamani 	return htonl(msecs);
1565822c8685SDeepa Dinamani }
1566822c8685SDeepa Dinamani EXPORT_SYMBOL(inet_current_timestamp);
1567822c8685SDeepa Dinamani 
1568f4713a3dSWillem de Bruijn int inet_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
1569f4713a3dSWillem de Bruijn {
1570f4713a3dSWillem de Bruijn 	if (sk->sk_family == AF_INET)
1571f4713a3dSWillem de Bruijn 		return ip_recv_error(sk, msg, len, addr_len);
1572f4713a3dSWillem de Bruijn #if IS_ENABLED(CONFIG_IPV6)
1573f4713a3dSWillem de Bruijn 	if (sk->sk_family == AF_INET6)
1574f4713a3dSWillem de Bruijn 		return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len);
1575f4713a3dSWillem de Bruijn #endif
1576f4713a3dSWillem de Bruijn 	return -EINVAL;
1577f4713a3dSWillem de Bruijn }
1578f4713a3dSWillem de Bruijn 
1579028e0a47SPaolo Abeni INDIRECT_CALLABLE_DECLARE(int tcp4_gro_complete(struct sk_buff *, int));
1580028e0a47SPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udp4_gro_complete(struct sk_buff *, int));
1581b8921ca8STom Herbert int inet_gro_complete(struct sk_buff *skb, int nhoff)
158273cc19f1SHerbert Xu {
1583299603e8SJerry Chu 	__be16 newlen = htons(skb->len - nhoff);
1584299603e8SJerry Chu 	struct iphdr *iph = (struct iphdr *)(skb->data + nhoff);
1585bca49f84SVlad Yasevich 	const struct net_offload *ops;
1586f9242b6bSDavid S. Miller 	int proto = iph->protocol;
1587f9242b6bSDavid S. Miller 	int err = -ENOSYS;
158873cc19f1SHerbert Xu 
1589294acf1cSPaolo Abeni 	if (skb->encapsulation) {
1590294acf1cSPaolo Abeni 		skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IP));
1591c3caf119SJerry Chu 		skb_set_inner_network_header(skb, nhoff);
1592294acf1cSPaolo Abeni 	}
1593c3caf119SJerry Chu 
159473cc19f1SHerbert Xu 	csum_replace2(&iph->check, iph->tot_len, newlen);
159573cc19f1SHerbert Xu 	iph->tot_len = newlen;
159673cc19f1SHerbert Xu 
159773cc19f1SHerbert Xu 	rcu_read_lock();
1598bca49f84SVlad Yasevich 	ops = rcu_dereference(inet_offloads[proto]);
1599f191a1d1SVlad Yasevich 	if (WARN_ON(!ops || !ops->callbacks.gro_complete))
160073cc19f1SHerbert Xu 		goto out_unlock;
160173cc19f1SHerbert Xu 
1602299603e8SJerry Chu 	/* Only need to add sizeof(*iph) to get to the next hdr below
1603299603e8SJerry Chu 	 * because any hdr with option will have been flushed in
1604299603e8SJerry Chu 	 * inet_gro_receive().
1605299603e8SJerry Chu 	 */
1606028e0a47SPaolo Abeni 	err = INDIRECT_CALL_2(ops->callbacks.gro_complete,
1607028e0a47SPaolo Abeni 			      tcp4_gro_complete, udp4_gro_complete,
1608028e0a47SPaolo Abeni 			      skb, nhoff + sizeof(*iph));
160973cc19f1SHerbert Xu 
161073cc19f1SHerbert Xu out_unlock:
161173cc19f1SHerbert Xu 	rcu_read_unlock();
161273cc19f1SHerbert Xu 
161373cc19f1SHerbert Xu 	return err;
161473cc19f1SHerbert Xu }
1615b8921ca8STom Herbert EXPORT_SYMBOL(inet_gro_complete);
161673cc19f1SHerbert Xu 
1617b8cba75bSJesse Gross static int ipip_gro_complete(struct sk_buff *skb, int nhoff)
1618b8cba75bSJesse Gross {
1619b8cba75bSJesse Gross 	skb->encapsulation = 1;
16207e13318dSTom Herbert 	skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
1621b8cba75bSJesse Gross 	return inet_gro_complete(skb, nhoff);
1622b8cba75bSJesse Gross }
1623b8cba75bSJesse Gross 
1624eee4fe4dSDenis V. Lunev int inet_ctl_sock_create(struct sock **sk, unsigned short family,
16255677242fSDenis V. Lunev 			 unsigned short type, unsigned char protocol,
16265677242fSDenis V. Lunev 			 struct net *net)
16273d58b5faSDenis V. Lunev {
1628eee4fe4dSDenis V. Lunev 	struct socket *sock;
162926abe143SEric W. Biederman 	int rc = sock_create_kern(net, family, type, protocol, &sock);
16303d58b5faSDenis V. Lunev 
16313d58b5faSDenis V. Lunev 	if (rc == 0) {
1632eee4fe4dSDenis V. Lunev 		*sk = sock->sk;
1633eee4fe4dSDenis V. Lunev 		(*sk)->sk_allocation = GFP_ATOMIC;
16343d58b5faSDenis V. Lunev 		/*
16353d58b5faSDenis V. Lunev 		 * Unhash it so that IP input processing does not even see it,
16363d58b5faSDenis V. Lunev 		 * we do not wish this socket to see incoming packets.
16373d58b5faSDenis V. Lunev 		 */
1638eee4fe4dSDenis V. Lunev 		(*sk)->sk_prot->unhash(*sk);
16393d58b5faSDenis V. Lunev 	}
16403d58b5faSDenis V. Lunev 	return rc;
16413d58b5faSDenis V. Lunev }
16423d58b5faSDenis V. Lunev EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
16433d58b5faSDenis V. Lunev 
1644c4c6bc31SRaghavendra K T u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt)
1645c4c6bc31SRaghavendra K T {
1646c4c6bc31SRaghavendra K T 	return  *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt);
1647c4c6bc31SRaghavendra K T }
1648c4c6bc31SRaghavendra K T EXPORT_SYMBOL_GPL(snmp_get_cpu_field);
1649c4c6bc31SRaghavendra K T 
1650698365faSWANG Cong unsigned long snmp_fold_field(void __percpu *mib, int offt)
16515e0f0435SHerbert Xu {
16525e0f0435SHerbert Xu 	unsigned long res = 0;
1653698365faSWANG Cong 	int i;
16545e0f0435SHerbert Xu 
1655698365faSWANG Cong 	for_each_possible_cpu(i)
1656c4c6bc31SRaghavendra K T 		res += snmp_get_cpu_field(mib, i, offt);
16575e0f0435SHerbert Xu 	return res;
16585e0f0435SHerbert Xu }
16595e0f0435SHerbert Xu EXPORT_SYMBOL_GPL(snmp_fold_field);
16605e0f0435SHerbert Xu 
16614ce3c183SEric Dumazet #if BITS_PER_LONG==32
16624ce3c183SEric Dumazet 
166380ec1927SDavid S. Miller u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offt,
1664c4c6bc31SRaghavendra K T 			 size_t syncp_offset)
16654ce3c183SEric Dumazet {
16668f0ea0feSEric Dumazet 	void *bhptr;
16674ce3c183SEric Dumazet 	struct u64_stats_sync *syncp;
16688f0ea0feSEric Dumazet 	u64 v;
16694ce3c183SEric Dumazet 	unsigned int start;
16704ce3c183SEric Dumazet 
1671698365faSWANG Cong 	bhptr = per_cpu_ptr(mib, cpu);
16724ce3c183SEric Dumazet 	syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
16734ce3c183SEric Dumazet 	do {
167457a7744eSEric W. Biederman 		start = u64_stats_fetch_begin_irq(syncp);
16758f0ea0feSEric Dumazet 		v = *(((u64 *)bhptr) + offt);
167657a7744eSEric W. Biederman 	} while (u64_stats_fetch_retry_irq(syncp, start));
16774ce3c183SEric Dumazet 
1678c4c6bc31SRaghavendra K T 	return v;
1679c4c6bc31SRaghavendra K T }
1680c4c6bc31SRaghavendra K T EXPORT_SYMBOL_GPL(snmp_get_cpu_field64);
1681c4c6bc31SRaghavendra K T 
1682c4c6bc31SRaghavendra K T u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_offset)
1683c4c6bc31SRaghavendra K T {
1684c4c6bc31SRaghavendra K T 	u64 res = 0;
1685c4c6bc31SRaghavendra K T 	int cpu;
1686c4c6bc31SRaghavendra K T 
1687c4c6bc31SRaghavendra K T 	for_each_possible_cpu(cpu) {
1688d1bfc625SMadalin Bucur 		res += snmp_get_cpu_field64(mib, cpu, offt, syncp_offset);
16894ce3c183SEric Dumazet 	}
16904ce3c183SEric Dumazet 	return res;
16914ce3c183SEric Dumazet }
16924ce3c183SEric Dumazet EXPORT_SYMBOL_GPL(snmp_fold_field64);
16934ce3c183SEric Dumazet #endif
16944ce3c183SEric Dumazet 
16951da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
169632613090SAlexey Dobriyan static const struct net_protocol igmp_protocol = {
16971da177e4SLinus Torvalds 	.handler =	igmp_rcv,
1698b4ee07dfSAlexey Dobriyan 	.netns_ok =	1,
16991da177e4SLinus Torvalds };
17001da177e4SLinus Torvalds #endif
17011da177e4SLinus Torvalds 
1702a8e3bb34SDavid Ahern /* thinking of making this const? Don't.
1703a8e3bb34SDavid Ahern  * early_demux can change based on sysctl.
1704a8e3bb34SDavid Ahern  */
170503157937SDavid Ahern static struct net_protocol tcp_protocol = {
170641063e9dSDavid S. Miller 	.early_demux	=	tcp_v4_early_demux,
1707dddb64bcSsubashab@codeaurora.org 	.early_demux_handler =  tcp_v4_early_demux,
17081da177e4SLinus Torvalds 	.handler	=	tcp_v4_rcv,
17091da177e4SLinus Torvalds 	.err_handler	=	tcp_v4_err,
17101da177e4SLinus Torvalds 	.no_policy	=	1,
171192f1fecbSDenis V. Lunev 	.netns_ok	=	1,
17128ed1dc44SHannes Frederic Sowa 	.icmp_strict_tag_validation = 1,
17131da177e4SLinus Torvalds };
17141da177e4SLinus Torvalds 
1715a8e3bb34SDavid Ahern /* thinking of making this const? Don't.
1716a8e3bb34SDavid Ahern  * early_demux can change based on sysctl.
1717a8e3bb34SDavid Ahern  */
171803157937SDavid Ahern static struct net_protocol udp_protocol = {
1719421b3885SShawn Bohrer 	.early_demux =	udp_v4_early_demux,
1720dddb64bcSsubashab@codeaurora.org 	.early_demux_handler =	udp_v4_early_demux,
17211da177e4SLinus Torvalds 	.handler =	udp_rcv,
17221da177e4SLinus Torvalds 	.err_handler =	udp_err,
17231da177e4SLinus Torvalds 	.no_policy =	1,
172492f1fecbSDenis V. Lunev 	.netns_ok =	1,
17251da177e4SLinus Torvalds };
17261da177e4SLinus Torvalds 
172732613090SAlexey Dobriyan static const struct net_protocol icmp_protocol = {
17281da177e4SLinus Torvalds 	.handler =	icmp_rcv,
17295b052042SLi Wei 	.err_handler =	icmp_err,
17308b7817f3SHerbert Xu 	.no_policy =	1,
173192f1fecbSDenis V. Lunev 	.netns_ok =	1,
17321da177e4SLinus Torvalds };
17331da177e4SLinus Torvalds 
17349b4661bdSPavel Emelyanov static __net_init int ipv4_mib_init_net(struct net *net)
17359b4661bdSPavel Emelyanov {
1736827da44cSJohn Stultz 	int i;
1737827da44cSJohn Stultz 
1738698365faSWANG Cong 	net->mib.tcp_statistics = alloc_percpu(struct tcp_mib);
1739698365faSWANG Cong 	if (!net->mib.tcp_statistics)
174057ef42d5SPavel Emelyanov 		goto err_tcp_mib;
1741698365faSWANG Cong 	net->mib.ip_statistics = alloc_percpu(struct ipstats_mib);
1742698365faSWANG Cong 	if (!net->mib.ip_statistics)
1743a20f5799SPavel Emelyanov 		goto err_ip_mib;
1744827da44cSJohn Stultz 
1745827da44cSJohn Stultz 	for_each_possible_cpu(i) {
1746827da44cSJohn Stultz 		struct ipstats_mib *af_inet_stats;
1747698365faSWANG Cong 		af_inet_stats = per_cpu_ptr(net->mib.ip_statistics, i);
1748827da44cSJohn Stultz 		u64_stats_init(&af_inet_stats->syncp);
1749827da44cSJohn Stultz 	}
1750827da44cSJohn Stultz 
1751698365faSWANG Cong 	net->mib.net_statistics = alloc_percpu(struct linux_mib);
1752698365faSWANG Cong 	if (!net->mib.net_statistics)
175361a7e260SPavel Emelyanov 		goto err_net_mib;
1754698365faSWANG Cong 	net->mib.udp_statistics = alloc_percpu(struct udp_mib);
1755698365faSWANG Cong 	if (!net->mib.udp_statistics)
17562f275f91SPavel Emelyanov 		goto err_udp_mib;
1757698365faSWANG Cong 	net->mib.udplite_statistics = alloc_percpu(struct udp_mib);
1758698365faSWANG Cong 	if (!net->mib.udplite_statistics)
1759386019d3SPavel Emelyanov 		goto err_udplite_mib;
1760698365faSWANG Cong 	net->mib.icmp_statistics = alloc_percpu(struct icmp_mib);
1761698365faSWANG Cong 	if (!net->mib.icmp_statistics)
1762b60538a0SPavel Emelyanov 		goto err_icmp_mib;
1763acb32ba3SEric Dumazet 	net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib),
1764acb32ba3SEric Dumazet 					      GFP_KERNEL);
1765acb32ba3SEric Dumazet 	if (!net->mib.icmpmsg_statistics)
1766923c6586SPavel Emelyanov 		goto err_icmpmsg_mib;
176757ef42d5SPavel Emelyanov 
176857ef42d5SPavel Emelyanov 	tcp_mib_init(net);
17699b4661bdSPavel Emelyanov 	return 0;
177057ef42d5SPavel Emelyanov 
1771923c6586SPavel Emelyanov err_icmpmsg_mib:
1772698365faSWANG Cong 	free_percpu(net->mib.icmp_statistics);
1773b60538a0SPavel Emelyanov err_icmp_mib:
1774698365faSWANG Cong 	free_percpu(net->mib.udplite_statistics);
1775386019d3SPavel Emelyanov err_udplite_mib:
1776698365faSWANG Cong 	free_percpu(net->mib.udp_statistics);
17772f275f91SPavel Emelyanov err_udp_mib:
1778698365faSWANG Cong 	free_percpu(net->mib.net_statistics);
177961a7e260SPavel Emelyanov err_net_mib:
1780698365faSWANG Cong 	free_percpu(net->mib.ip_statistics);
1781a20f5799SPavel Emelyanov err_ip_mib:
1782698365faSWANG Cong 	free_percpu(net->mib.tcp_statistics);
178357ef42d5SPavel Emelyanov err_tcp_mib:
178457ef42d5SPavel Emelyanov 	return -ENOMEM;
17859b4661bdSPavel Emelyanov }
17869b4661bdSPavel Emelyanov 
17879b4661bdSPavel Emelyanov static __net_exit void ipv4_mib_exit_net(struct net *net)
17889b4661bdSPavel Emelyanov {
1789acb32ba3SEric Dumazet 	kfree(net->mib.icmpmsg_statistics);
1790698365faSWANG Cong 	free_percpu(net->mib.icmp_statistics);
1791698365faSWANG Cong 	free_percpu(net->mib.udplite_statistics);
1792698365faSWANG Cong 	free_percpu(net->mib.udp_statistics);
1793698365faSWANG Cong 	free_percpu(net->mib.net_statistics);
1794698365faSWANG Cong 	free_percpu(net->mib.ip_statistics);
1795698365faSWANG Cong 	free_percpu(net->mib.tcp_statistics);
17969b4661bdSPavel Emelyanov }
17979b4661bdSPavel Emelyanov 
17989b4661bdSPavel Emelyanov static __net_initdata struct pernet_operations ipv4_mib_ops = {
17999b4661bdSPavel Emelyanov 	.init = ipv4_mib_init_net,
18009b4661bdSPavel Emelyanov 	.exit = ipv4_mib_exit_net,
18019b4661bdSPavel Emelyanov };
18029b4661bdSPavel Emelyanov 
18031da177e4SLinus Torvalds static int __init init_ipv4_mibs(void)
18041da177e4SLinus Torvalds {
1805d89cbbb1SPavel Emelyanov 	return register_pernet_subsys(&ipv4_mib_ops);
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds 
1808c9d8f1a6SCong Wang static __net_init int inet_init_net(struct net *net)
1809c9d8f1a6SCong Wang {
1810c9d8f1a6SCong Wang 	/*
1811c9d8f1a6SCong Wang 	 * Set defaults for local port range
1812c9d8f1a6SCong Wang 	 */
1813c9d8f1a6SCong Wang 	seqlock_init(&net->ipv4.ip_local_ports.lock);
1814c9d8f1a6SCong Wang 	net->ipv4.ip_local_ports.range[0] =  32768;
181507f4c900SEric Dumazet 	net->ipv4.ip_local_ports.range[1] =  60999;
1816ba6b918aSCong Wang 
1817ba6b918aSCong Wang 	seqlock_init(&net->ipv4.ping_group_range.lock);
1818ba6b918aSCong Wang 	/*
1819ba6b918aSCong Wang 	 * Sane defaults - nobody may create ping sockets.
1820ba6b918aSCong Wang 	 * Boot scripts should set this to distro-specific group.
1821ba6b918aSCong Wang 	 */
1822ba6b918aSCong Wang 	net->ipv4.ping_group_range.range[0] = make_kgid(&init_user_ns, 1);
1823ba6b918aSCong Wang 	net->ipv4.ping_group_range.range[1] = make_kgid(&init_user_ns, 0);
1824049bbf58SEzequiel Garcia 
1825049bbf58SEzequiel Garcia 	/* Default values for sysctl-controlled parameters.
1826049bbf58SEzequiel Garcia 	 * We set them here, in case sysctl is not compiled.
1827049bbf58SEzequiel Garcia 	 */
1828049bbf58SEzequiel Garcia 	net->ipv4.sysctl_ip_default_ttl = IPDEFTTL;
1829432e05d3SPetr Machata 	net->ipv4.sysctl_ip_fwd_update_priority = 1;
1830049bbf58SEzequiel Garcia 	net->ipv4.sysctl_ip_dynaddr = 0;
1831049bbf58SEzequiel Garcia 	net->ipv4.sysctl_ip_early_demux = 1;
1832dddb64bcSsubashab@codeaurora.org 	net->ipv4.sysctl_udp_early_demux = 1;
1833dddb64bcSsubashab@codeaurora.org 	net->ipv4.sysctl_tcp_early_demux = 1;
18344548b683SKrister Johansen #ifdef CONFIG_SYSCTL
18354548b683SKrister Johansen 	net->ipv4.sysctl_ip_prot_sock = PROT_SOCK;
18364548b683SKrister Johansen #endif
1837049bbf58SEzequiel Garcia 
18381714020eSNikolay Borisov 	/* Some igmp sysctl, whose values are always used */
18391714020eSNikolay Borisov 	net->ipv4.sysctl_igmp_max_memberships = 20;
18401714020eSNikolay Borisov 	net->ipv4.sysctl_igmp_max_msf = 10;
18411714020eSNikolay Borisov 	/* IGMP reports for link-local multicast groups are enabled by default */
18421714020eSNikolay Borisov 	net->ipv4.sysctl_igmp_llm_reports = 1;
18431714020eSNikolay Borisov 	net->ipv4.sysctl_igmp_qrv = 2;
18441714020eSNikolay Borisov 
1845c9d8f1a6SCong Wang 	return 0;
1846c9d8f1a6SCong Wang }
1847c9d8f1a6SCong Wang 
1848c9d8f1a6SCong Wang static __net_exit void inet_exit_net(struct net *net)
1849c9d8f1a6SCong Wang {
1850c9d8f1a6SCong Wang }
1851c9d8f1a6SCong Wang 
1852c9d8f1a6SCong Wang static __net_initdata struct pernet_operations af_inet_ops = {
1853c9d8f1a6SCong Wang 	.init = inet_init_net,
1854c9d8f1a6SCong Wang 	.exit = inet_exit_net,
1855c9d8f1a6SCong Wang };
1856c9d8f1a6SCong Wang 
1857c9d8f1a6SCong Wang static int __init init_inet_pernet_ops(void)
1858c9d8f1a6SCong Wang {
1859c9d8f1a6SCong Wang 	return register_pernet_subsys(&af_inet_ops);
1860c9d8f1a6SCong Wang }
1861c9d8f1a6SCong Wang 
18621da177e4SLinus Torvalds static int ipv4_proc_init(void);
18631da177e4SLinus Torvalds 
186430e224d7SHerbert Xu /*
186530e224d7SHerbert Xu  *	IP protocol layer initialiser
186630e224d7SHerbert Xu  */
186730e224d7SHerbert Xu 
186822061d80SVlad Yasevich static struct packet_offload ip_packet_offload __read_mostly = {
186922061d80SVlad Yasevich 	.type = cpu_to_be16(ETH_P_IP),
1870f191a1d1SVlad Yasevich 	.callbacks = {
1871f4c50d99SHerbert Xu 		.gso_segment = inet_gso_segment,
187273cc19f1SHerbert Xu 		.gro_receive = inet_gro_receive,
187373cc19f1SHerbert Xu 		.gro_complete = inet_gro_complete,
1874f191a1d1SVlad Yasevich 	},
187530e224d7SHerbert Xu };
187630e224d7SHerbert Xu 
1877cb32f511SEric Dumazet static const struct net_offload ipip_offload = {
1878cb32f511SEric Dumazet 	.callbacks = {
1879418e897eSWillem de Bruijn 		.gso_segment	= ipip_gso_segment,
1880fac8e0f5SJesse Gross 		.gro_receive	= ipip_gro_receive,
1881b8cba75bSJesse Gross 		.gro_complete	= ipip_gro_complete,
1882cb32f511SEric Dumazet 	},
1883cb32f511SEric Dumazet };
1884cb32f511SEric Dumazet 
188593b1b31fSTonghao Zhang static int __init ipip_offload_init(void)
188693b1b31fSTonghao Zhang {
188793b1b31fSTonghao Zhang 	return inet_add_offload(&ipip_offload, IPPROTO_IPIP);
188893b1b31fSTonghao Zhang }
188993b1b31fSTonghao Zhang 
1890808a8f88SVlad Yasevich static int __init ipv4_offload_init(void)
1891808a8f88SVlad Yasevich {
1892808a8f88SVlad Yasevich 	/*
1893808a8f88SVlad Yasevich 	 * Add offloads
1894808a8f88SVlad Yasevich 	 */
1895da5bab07SDaniel Borkmann 	if (udpv4_offload_init() < 0)
1896808a8f88SVlad Yasevich 		pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
189728850dc7SDaniel Borkmann 	if (tcpv4_offload_init() < 0)
189828850dc7SDaniel Borkmann 		pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
189993b1b31fSTonghao Zhang 	if (ipip_offload_init() < 0)
190093b1b31fSTonghao Zhang 		pr_crit("%s: Cannot add IPIP protocol offload\n", __func__);
1901808a8f88SVlad Yasevich 
1902808a8f88SVlad Yasevich 	dev_add_offload(&ip_packet_offload);
1903808a8f88SVlad Yasevich 	return 0;
1904808a8f88SVlad Yasevich }
1905808a8f88SVlad Yasevich 
1906808a8f88SVlad Yasevich fs_initcall(ipv4_offload_init);
1907808a8f88SVlad Yasevich 
1908808a8f88SVlad Yasevich static struct packet_type ip_packet_type __read_mostly = {
1909808a8f88SVlad Yasevich 	.type = cpu_to_be16(ETH_P_IP),
1910808a8f88SVlad Yasevich 	.func = ip_rcv,
191117266ee9SEdward Cree 	.list_func = ip_list_rcv,
1912808a8f88SVlad Yasevich };
1913808a8f88SVlad Yasevich 
19141da177e4SLinus Torvalds static int __init inet_init(void)
19151da177e4SLinus Torvalds {
19161da177e4SLinus Torvalds 	struct inet_protosw *q;
19171da177e4SLinus Torvalds 	struct list_head *r;
19181da177e4SLinus Torvalds 	int rc = -EINVAL;
19191da177e4SLinus Torvalds 
1920b4772ef8SEyal Birger 	sock_skb_cb_check_size(sizeof(struct inet_skb_parm));
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	rc = proto_register(&tcp_prot, 1);
19231da177e4SLinus Torvalds 	if (rc)
1924122ff243SWANG Cong 		goto out;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	rc = proto_register(&udp_prot, 1);
19271da177e4SLinus Torvalds 	if (rc)
19281da177e4SLinus Torvalds 		goto out_unregister_tcp_proto;
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds 	rc = proto_register(&raw_prot, 1);
19311da177e4SLinus Torvalds 	if (rc)
19321da177e4SLinus Torvalds 		goto out_unregister_udp_proto;
19331da177e4SLinus Torvalds 
1934c319b4d7SVasiliy Kulikov 	rc = proto_register(&ping_prot, 1);
1935c319b4d7SVasiliy Kulikov 	if (rc)
1936c319b4d7SVasiliy Kulikov 		goto out_unregister_raw_proto;
1937c319b4d7SVasiliy Kulikov 
19381da177e4SLinus Torvalds 	/*
19391da177e4SLinus Torvalds 	 *	Tell SOCKET that we are alive...
19401da177e4SLinus Torvalds 	 */
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 	(void)sock_register(&inet_family_ops);
19431da177e4SLinus Torvalds 
1944bd7b1533SAl Viro #ifdef CONFIG_SYSCTL
1945bd7b1533SAl Viro 	ip_static_sysctl_init();
1946bd7b1533SAl Viro #endif
1947bd7b1533SAl Viro 
19481da177e4SLinus Torvalds 	/*
19491da177e4SLinus Torvalds 	 *	Add all the base protocols.
19501da177e4SLinus Torvalds 	 */
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 	if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1953058bd4d2SJoe Perches 		pr_crit("%s: Cannot add ICMP protocol\n", __func__);
19541da177e4SLinus Torvalds 	if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1955058bd4d2SJoe Perches 		pr_crit("%s: Cannot add UDP protocol\n", __func__);
19561da177e4SLinus Torvalds 	if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1957058bd4d2SJoe Perches 		pr_crit("%s: Cannot add TCP protocol\n", __func__);
19581da177e4SLinus Torvalds #ifdef CONFIG_IP_MULTICAST
19591da177e4SLinus Torvalds 	if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1960058bd4d2SJoe Perches 		pr_crit("%s: Cannot add IGMP protocol\n", __func__);
19611da177e4SLinus Torvalds #endif
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	/* Register the socket-side information for inet_create. */
19641da177e4SLinus Torvalds 	for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
19651da177e4SLinus Torvalds 		INIT_LIST_HEAD(r);
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
19681da177e4SLinus Torvalds 		inet_register_protosw(q);
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	/*
19711da177e4SLinus Torvalds 	 *	Set the ARP module up
19721da177e4SLinus Torvalds 	 */
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	arp_init();
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	/*
19771da177e4SLinus Torvalds 	 *	Set the IP module up
19781da177e4SLinus Torvalds 	 */
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds 	ip_init();
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	/* Setup TCP slab cache for open requests. */
19831da177e4SLinus Torvalds 	tcp_init();
19841da177e4SLinus Torvalds 
198595766fffSHideo Aoki 	/* Setup UDP memory threshold */
198695766fffSHideo Aoki 	udp_init();
198795766fffSHideo Aoki 
1988ba4e58ecSGerrit Renker 	/* Add UDP-Lite (RFC 3828) */
1989ba4e58ecSGerrit Renker 	udplite4_register();
19901da177e4SLinus Torvalds 
19916897445fSMike Manning 	raw_init();
19926897445fSMike Manning 
1993c319b4d7SVasiliy Kulikov 	ping_init();
1994c319b4d7SVasiliy Kulikov 
19951da177e4SLinus Torvalds 	/*
19961da177e4SLinus Torvalds 	 *	Set the ICMP layer up
19971da177e4SLinus Torvalds 	 */
19981da177e4SLinus Torvalds 
1999a5710d65SDenis V. Lunev 	if (icmp_init() < 0)
2000a5710d65SDenis V. Lunev 		panic("Failed to create the ICMP control socket.\n");
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	/*
20031da177e4SLinus Torvalds 	 *	Initialise the multicast router
20041da177e4SLinus Torvalds 	 */
20051da177e4SLinus Torvalds #if defined(CONFIG_IP_MROUTE)
200603d2f897SWang Chen 	if (ip_mr_init())
2007058bd4d2SJoe Perches 		pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
20081da177e4SLinus Torvalds #endif
2009c9d8f1a6SCong Wang 
2010c9d8f1a6SCong Wang 	if (init_inet_pernet_ops())
2011c9d8f1a6SCong Wang 		pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__);
20121da177e4SLinus Torvalds 	/*
20131da177e4SLinus Torvalds 	 *	Initialise per-cpu ipv4 mibs
20141da177e4SLinus Torvalds 	 */
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 	if (init_ipv4_mibs())
2017058bd4d2SJoe Perches 		pr_crit("%s: Cannot init ipv4 mibs\n", __func__);
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds 	ipv4_proc_init();
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	ipfrag_init();
20221da177e4SLinus Torvalds 
202330e224d7SHerbert Xu 	dev_add_pack(&ip_packet_type);
202430e224d7SHerbert Xu 
2025045a0fa0SThomas Graf 	ip_tunnel_core_init();
2026045a0fa0SThomas Graf 
20271da177e4SLinus Torvalds 	rc = 0;
20281da177e4SLinus Torvalds out:
20291da177e4SLinus Torvalds 	return rc;
2030c319b4d7SVasiliy Kulikov out_unregister_raw_proto:
2031c319b4d7SVasiliy Kulikov 	proto_unregister(&raw_prot);
20321da177e4SLinus Torvalds out_unregister_udp_proto:
20331da177e4SLinus Torvalds 	proto_unregister(&udp_prot);
2034321efff7SOlaf Kirch out_unregister_tcp_proto:
2035321efff7SOlaf Kirch 	proto_unregister(&tcp_prot);
20361da177e4SLinus Torvalds 	goto out;
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
2039a536e077SHeiko Carstens fs_initcall(inet_init);
20401da177e4SLinus Torvalds 
20411da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
20441da177e4SLinus Torvalds static int __init ipv4_proc_init(void)
20451da177e4SLinus Torvalds {
20461da177e4SLinus Torvalds 	int rc = 0;
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	if (raw_proc_init())
20491da177e4SLinus Torvalds 		goto out_raw;
20501da177e4SLinus Torvalds 	if (tcp4_proc_init())
20511da177e4SLinus Torvalds 		goto out_tcp;
20521da177e4SLinus Torvalds 	if (udp4_proc_init())
20531da177e4SLinus Torvalds 		goto out_udp;
2054c319b4d7SVasiliy Kulikov 	if (ping_proc_init())
2055c319b4d7SVasiliy Kulikov 		goto out_ping;
20561da177e4SLinus Torvalds 	if (ip_misc_proc_init())
20571da177e4SLinus Torvalds 		goto out_misc;
20581da177e4SLinus Torvalds out:
20591da177e4SLinus Torvalds 	return rc;
20601da177e4SLinus Torvalds out_misc:
2061c319b4d7SVasiliy Kulikov 	ping_proc_exit();
2062c319b4d7SVasiliy Kulikov out_ping:
20631da177e4SLinus Torvalds 	udp4_proc_exit();
20641da177e4SLinus Torvalds out_udp:
20651da177e4SLinus Torvalds 	tcp4_proc_exit();
20661da177e4SLinus Torvalds out_tcp:
20671da177e4SLinus Torvalds 	raw_proc_exit();
20681da177e4SLinus Torvalds out_raw:
20691da177e4SLinus Torvalds 	rc = -ENOMEM;
20701da177e4SLinus Torvalds 	goto out;
20711da177e4SLinus Torvalds }
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds #else /* CONFIG_PROC_FS */
20741da177e4SLinus Torvalds static int __init ipv4_proc_init(void)
20751da177e4SLinus Torvalds {
20761da177e4SLinus Torvalds 	return 0;
20771da177e4SLinus Torvalds }
20781da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
2079