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