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