xref: /linux/net/ipv4/udp.c (revision 1a186c14cedbf3364634f2dfa9302ed31b8fab19)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		The User Datagram Protocol (UDP).
81da177e4SLinus Torvalds  *
902c30a84SJesper Juhl  * Authors:	Ross Biro
101da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
111da177e4SLinus Torvalds  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
12113aa838SAlan Cox  *		Alan Cox, <alan@lxorguk.ukuu.org.uk>
131da177e4SLinus Torvalds  *		Hirokazu Takahashi, <taka@valinux.co.jp>
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Fixes:
161da177e4SLinus Torvalds  *		Alan Cox	:	verify_area() calls
171da177e4SLinus Torvalds  *		Alan Cox	: 	stopped close while in use off icmp
181da177e4SLinus Torvalds  *					messages. Not a fix but a botch that
191da177e4SLinus Torvalds  *					for udp at least is 'valid'.
201da177e4SLinus Torvalds  *		Alan Cox	:	Fixed icmp handling properly
211da177e4SLinus Torvalds  *		Alan Cox	: 	Correct error for oversized datagrams
221da177e4SLinus Torvalds  *		Alan Cox	:	Tidied select() semantics.
231da177e4SLinus Torvalds  *		Alan Cox	:	udp_err() fixed properly, also now
241da177e4SLinus Torvalds  *					select and read wake correctly on errors
251da177e4SLinus Torvalds  *		Alan Cox	:	udp_send verify_area moved to avoid mem leak
261da177e4SLinus Torvalds  *		Alan Cox	:	UDP can count its memory
271da177e4SLinus Torvalds  *		Alan Cox	:	send to an unknown connection causes
281da177e4SLinus Torvalds  *					an ECONNREFUSED off the icmp, but
291da177e4SLinus Torvalds  *					does NOT close.
301da177e4SLinus Torvalds  *		Alan Cox	:	Switched to new sk_buff handlers. No more backlog!
311da177e4SLinus Torvalds  *		Alan Cox	:	Using generic datagram code. Even smaller and the PEEK
321da177e4SLinus Torvalds  *					bug no longer crashes it.
331da177e4SLinus Torvalds  *		Fred Van Kempen	: 	Net2e support for sk->broadcast.
341da177e4SLinus Torvalds  *		Alan Cox	:	Uses skb_free_datagram
351da177e4SLinus Torvalds  *		Alan Cox	:	Added get/set sockopt support.
361da177e4SLinus Torvalds  *		Alan Cox	:	Broadcasting without option set returns EACCES.
371da177e4SLinus Torvalds  *		Alan Cox	:	No wakeup calls. Instead we now use the callbacks.
381da177e4SLinus Torvalds  *		Alan Cox	:	Use ip_tos and ip_ttl
391da177e4SLinus Torvalds  *		Alan Cox	:	SNMP Mibs
401da177e4SLinus Torvalds  *		Alan Cox	:	MSG_DONTROUTE, and 0.0.0.0 support.
411da177e4SLinus Torvalds  *		Matt Dillon	:	UDP length checks.
421da177e4SLinus Torvalds  *		Alan Cox	:	Smarter af_inet used properly.
431da177e4SLinus Torvalds  *		Alan Cox	:	Use new kernel side addressing.
441da177e4SLinus Torvalds  *		Alan Cox	:	Incorrect return on truncated datagram receive.
451da177e4SLinus Torvalds  *	Arnt Gulbrandsen 	:	New udp_send and stuff
461da177e4SLinus Torvalds  *		Alan Cox	:	Cache last socket
471da177e4SLinus Torvalds  *		Alan Cox	:	Route cache
481da177e4SLinus Torvalds  *		Jon Peatfield	:	Minor efficiency fix to sendto().
491da177e4SLinus Torvalds  *		Mike Shaver	:	RFC1122 checks.
501da177e4SLinus Torvalds  *		Alan Cox	:	Nonblocking error fix.
511da177e4SLinus Torvalds  *	Willy Konynenberg	:	Transparent proxying support.
521da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
531da177e4SLinus Torvalds  *		David S. Miller	:	New socket lookup architecture.
541da177e4SLinus Torvalds  *					Last socket cache retained as it
551da177e4SLinus Torvalds  *					does have a high hit rate.
561da177e4SLinus Torvalds  *		Olaf Kirch	:	Don't linearise iovec on sendmsg.
571da177e4SLinus Torvalds  *		Andi Kleen	:	Some cleanups, cache destination entry
581da177e4SLinus Torvalds  *					for connect.
591da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
601da177e4SLinus Torvalds  *		Melvin Smith	:	Check msg_name not msg_namelen in sendto(),
611da177e4SLinus Torvalds  *					return ENOTCONN for unconnected sockets (POSIX)
621da177e4SLinus Torvalds  *		Janos Farkas	:	don't deliver multi/broadcasts to a different
631da177e4SLinus Torvalds  *					bound-to-device socket
641da177e4SLinus Torvalds  *	Hirokazu Takahashi	:	HW checksumming for outgoing UDP
651da177e4SLinus Torvalds  *					datagrams.
661da177e4SLinus Torvalds  *	Hirokazu Takahashi	:	sendfile() on UDP works now.
671da177e4SLinus Torvalds  *		Arnaldo C. Melo :	convert /proc/net/udp to seq_file
681da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
691da177e4SLinus Torvalds  *	Alexey Kuznetsov:		allow both IPv4 and IPv6 sockets to bind
701da177e4SLinus Torvalds  *					a single port at the same time.
711da177e4SLinus Torvalds  *	Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
72342f0234SJames Chapman  *	James Chapman		:	Add L2TP encapsulation type.
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds 
75afd46503SJoe Perches #define pr_fmt(fmt) "UDP: " fmt
76afd46503SJoe Perches 
777c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
781da177e4SLinus Torvalds #include <asm/ioctls.h>
7957c8a661SMike Rapoport #include <linux/memblock.h>
808203efb3SEric Dumazet #include <linux/highmem.h>
818203efb3SEric Dumazet #include <linux/swap.h>
821da177e4SLinus Torvalds #include <linux/types.h>
831da177e4SLinus Torvalds #include <linux/fcntl.h>
841da177e4SLinus Torvalds #include <linux/module.h>
851da177e4SLinus Torvalds #include <linux/socket.h>
861da177e4SLinus Torvalds #include <linux/sockios.h>
8714c85021SArnaldo Carvalho de Melo #include <linux/igmp.h>
886e540309SShawn Bohrer #include <linux/inetdevice.h>
891da177e4SLinus Torvalds #include <linux/in.h>
901da177e4SLinus Torvalds #include <linux/errno.h>
911da177e4SLinus Torvalds #include <linux/timer.h>
921da177e4SLinus Torvalds #include <linux/mm.h>
931da177e4SLinus Torvalds #include <linux/inet.h>
941da177e4SLinus Torvalds #include <linux/netdevice.h>
955a0e3ad6STejun Heo #include <linux/slab.h>
96c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
971da177e4SLinus Torvalds #include <linux/skbuff.h>
981da177e4SLinus Torvalds #include <linux/proc_fs.h>
991da177e4SLinus Torvalds #include <linux/seq_file.h>
100457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1011da177e4SLinus Torvalds #include <net/icmp.h>
102421b3885SShawn Bohrer #include <net/inet_hashtables.h>
103e7cc0824SStefano Brivio #include <net/ip_tunnels.h>
1041da177e4SLinus Torvalds #include <net/route.h>
1051da177e4SLinus Torvalds #include <net/checksum.h>
1061da177e4SLinus Torvalds #include <net/xfrm.h>
107296f7ea7SSatoru Moriya #include <trace/events/udp.h>
108447167bfSEric Dumazet #include <linux/static_key.h>
10922911fc5SEric Dumazet #include <trace/events/skb.h>
110076bb0c8SEliezer Tamir #include <net/busy_poll.h>
111ba4e58ecSGerrit Renker #include "udp_impl.h"
112e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
113217375a0SEric Dumazet #include <net/addrconf.h>
11460fb9567SPaolo Abeni #include <net/udp_tunnel.h>
1151da177e4SLinus Torvalds 
116f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly;
117645ca708SEric Dumazet EXPORT_SYMBOL(udp_table);
1181da177e4SLinus Torvalds 
1198d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly;
12095766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem);
121c482c568SEric Dumazet 
1228d987e5cSEric Dumazet atomic_long_t udp_memory_allocated;
12395766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated);
12495766fffSHideo Aoki 
125f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536
126f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
12798322f22SEric Dumazet 
128f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num,
129645ca708SEric Dumazet 			       const struct udp_hslot *hslot,
13098322f22SEric Dumazet 			       unsigned long *bitmap,
131fe38d2a1SJosef Bacik 			       struct sock *sk, unsigned int log)
13225030a7fSGerrit Renker {
133f24d43c0SEric Dumazet 	struct sock *sk2;
134ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
13525030a7fSGerrit Renker 
136ca065d0cSEric Dumazet 	sk_for_each(sk2, &hslot->head) {
137f24d43c0SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
138f24d43c0SEric Dumazet 		    sk2 != sk &&
139d4cada4aSEric Dumazet 		    (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
140f24d43c0SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1419d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1429d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
143fe38d2a1SJosef Bacik 		    inet_rcv_saddr_equal(sk, sk2, true)) {
144df560056SEric Garver 			if (sk2->sk_reuseport && sk->sk_reuseport &&
145df560056SEric Garver 			    !rcu_access_pointer(sk->sk_reuseport_cb) &&
146df560056SEric Garver 			    uid_eq(uid, sock_i_uid(sk2))) {
147df560056SEric Garver 				if (!bitmap)
148df560056SEric Garver 					return 0;
149df560056SEric Garver 			} else {
1504243cdc2SJoe Perches 				if (!bitmap)
151fc038410SDavid S. Miller 					return 1;
152df560056SEric Garver 				__set_bit(udp_sk(sk2)->udp_port_hash >> log,
153df560056SEric Garver 					  bitmap);
154df560056SEric Garver 			}
1554243cdc2SJoe Perches 		}
15698322f22SEric Dumazet 	}
15725030a7fSGerrit Renker 	return 0;
15825030a7fSGerrit Renker }
15925030a7fSGerrit Renker 
16030fff923SEric Dumazet /*
16130fff923SEric Dumazet  * Note: we still hold spinlock of primary hash chain, so no other writer
16230fff923SEric Dumazet  * can insert/delete a socket with local_port == num
16330fff923SEric Dumazet  */
16430fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num,
16530fff923SEric Dumazet 				struct udp_hslot *hslot2,
166fe38d2a1SJosef Bacik 				struct sock *sk)
16730fff923SEric Dumazet {
16830fff923SEric Dumazet 	struct sock *sk2;
169ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
17030fff923SEric Dumazet 	int res = 0;
17130fff923SEric Dumazet 
17230fff923SEric Dumazet 	spin_lock(&hslot2->lock);
173ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry(sk2, &hslot2->head) {
17430fff923SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
17530fff923SEric Dumazet 		    sk2 != sk &&
17630fff923SEric Dumazet 		    (udp_sk(sk2)->udp_port_hash == num) &&
17730fff923SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1789d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1799d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
180fe38d2a1SJosef Bacik 		    inet_rcv_saddr_equal(sk, sk2, true)) {
181df560056SEric Garver 			if (sk2->sk_reuseport && sk->sk_reuseport &&
182df560056SEric Garver 			    !rcu_access_pointer(sk->sk_reuseport_cb) &&
183df560056SEric Garver 			    uid_eq(uid, sock_i_uid(sk2))) {
184df560056SEric Garver 				res = 0;
185df560056SEric Garver 			} else {
18630fff923SEric Dumazet 				res = 1;
187df560056SEric Garver 			}
18830fff923SEric Dumazet 			break;
18930fff923SEric Dumazet 		}
1904243cdc2SJoe Perches 	}
19130fff923SEric Dumazet 	spin_unlock(&hslot2->lock);
19230fff923SEric Dumazet 	return res;
19330fff923SEric Dumazet }
19430fff923SEric Dumazet 
195fe38d2a1SJosef Bacik static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot)
196e32ea7e7SCraig Gallek {
197e32ea7e7SCraig Gallek 	struct net *net = sock_net(sk);
198e32ea7e7SCraig Gallek 	kuid_t uid = sock_i_uid(sk);
199e32ea7e7SCraig Gallek 	struct sock *sk2;
200e32ea7e7SCraig Gallek 
201ca065d0cSEric Dumazet 	sk_for_each(sk2, &hslot->head) {
202e32ea7e7SCraig Gallek 		if (net_eq(sock_net(sk2), net) &&
203e32ea7e7SCraig Gallek 		    sk2 != sk &&
204e32ea7e7SCraig Gallek 		    sk2->sk_family == sk->sk_family &&
205e32ea7e7SCraig Gallek 		    ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
206e32ea7e7SCraig Gallek 		    (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) &&
207e32ea7e7SCraig Gallek 		    (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
208e32ea7e7SCraig Gallek 		    sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
209fe38d2a1SJosef Bacik 		    inet_rcv_saddr_equal(sk, sk2, false)) {
2102dbb9b9eSMartin KaFai Lau 			return reuseport_add_sock(sk, sk2,
2112dbb9b9eSMartin KaFai Lau 						  inet_rcv_saddr_any(sk));
212e32ea7e7SCraig Gallek 		}
213e32ea7e7SCraig Gallek 	}
214e32ea7e7SCraig Gallek 
2152dbb9b9eSMartin KaFai Lau 	return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
216e32ea7e7SCraig Gallek }
217e32ea7e7SCraig Gallek 
21825030a7fSGerrit Renker /**
2196ba5a3c5SPavel Emelyanov  *  udp_lib_get_port  -  UDP/-Lite port lookup for IPv4 and IPv6
22025030a7fSGerrit Renker  *
22125030a7fSGerrit Renker  *  @sk:          socket struct in question
22225030a7fSGerrit Renker  *  @snum:        port number to look up
22325985edcSLucas De Marchi  *  @hash2_nulladdr: AF-dependent hash value in secondary hash chains,
22430fff923SEric Dumazet  *                   with NULL address
22525030a7fSGerrit Renker  */
2266ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum,
22730fff923SEric Dumazet 		     unsigned int hash2_nulladdr)
2281da177e4SLinus Torvalds {
229512615b6SEric Dumazet 	struct udp_hslot *hslot, *hslot2;
230645ca708SEric Dumazet 	struct udp_table *udptable = sk->sk_prot->h.udp_table;
23125030a7fSGerrit Renker 	int    error = 1;
2323b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2331da177e4SLinus Torvalds 
23432c1da70SStephen Hemminger 	if (!snum) {
2359088c560SEric Dumazet 		int low, high, remaining;
23695c96174SEric Dumazet 		unsigned int rand;
23798322f22SEric Dumazet 		unsigned short first, last;
23898322f22SEric Dumazet 		DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
2391da177e4SLinus Torvalds 
2400bbf87d8SEric W. Biederman 		inet_get_local_port_range(net, &low, &high);
241a25de534SAnton Arapov 		remaining = (high - low) + 1;
242227b60f5SStephen Hemminger 
24363862b5bSAruna-Hewapathirane 		rand = prandom_u32();
2448fc54f68SDaniel Borkmann 		first = reciprocal_scale(rand, remaining) + low;
24598322f22SEric Dumazet 		/*
24698322f22SEric Dumazet 		 * force rand to be an odd multiple of UDP_HTABLE_SIZE
24798322f22SEric Dumazet 		 */
248f86dcc5aSEric Dumazet 		rand = (rand | 1) * (udptable->mask + 1);
2495781b235SEric Dumazet 		last = first + udptable->mask + 1;
2505781b235SEric Dumazet 		do {
251f86dcc5aSEric Dumazet 			hslot = udp_hashslot(udptable, net, first);
25298322f22SEric Dumazet 			bitmap_zero(bitmap, PORTS_PER_CHAIN);
253645ca708SEric Dumazet 			spin_lock_bh(&hslot->lock);
25498322f22SEric Dumazet 			udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
255fe38d2a1SJosef Bacik 					    udptable->log);
25698322f22SEric Dumazet 
25798322f22SEric Dumazet 			snum = first;
25898322f22SEric Dumazet 			/*
25998322f22SEric Dumazet 			 * Iterate on all possible values of snum for this hash.
26098322f22SEric Dumazet 			 * Using steps of an odd multiple of UDP_HTABLE_SIZE
26198322f22SEric Dumazet 			 * give us randomization and full range coverage.
26298322f22SEric Dumazet 			 */
2639088c560SEric Dumazet 			do {
26498322f22SEric Dumazet 				if (low <= snum && snum <= high &&
265e3826f1eSAmerigo Wang 				    !test_bit(snum >> udptable->log, bitmap) &&
266122ff243SWANG Cong 				    !inet_is_local_reserved_port(net, snum))
26798322f22SEric Dumazet 					goto found;
26898322f22SEric Dumazet 				snum += rand;
26998322f22SEric Dumazet 			} while (snum != first);
27098322f22SEric Dumazet 			spin_unlock_bh(&hslot->lock);
271df560056SEric Garver 			cond_resched();
2725781b235SEric Dumazet 		} while (++first != last);
27398322f22SEric Dumazet 		goto fail;
274645ca708SEric Dumazet 	} else {
275f86dcc5aSEric Dumazet 		hslot = udp_hashslot(udptable, net, snum);
276645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
27730fff923SEric Dumazet 		if (hslot->count > 10) {
27830fff923SEric Dumazet 			int exist;
27930fff923SEric Dumazet 			unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
28030fff923SEric Dumazet 
28130fff923SEric Dumazet 			slot2          &= udptable->mask;
28230fff923SEric Dumazet 			hash2_nulladdr &= udptable->mask;
28330fff923SEric Dumazet 
28430fff923SEric Dumazet 			hslot2 = udp_hashslot2(udptable, slot2);
28530fff923SEric Dumazet 			if (hslot->count < hslot2->count)
28630fff923SEric Dumazet 				goto scan_primary_hash;
28730fff923SEric Dumazet 
288fe38d2a1SJosef Bacik 			exist = udp_lib_lport_inuse2(net, snum, hslot2, sk);
28930fff923SEric Dumazet 			if (!exist && (hash2_nulladdr != slot2)) {
29030fff923SEric Dumazet 				hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
29130fff923SEric Dumazet 				exist = udp_lib_lport_inuse2(net, snum, hslot2,
292fe38d2a1SJosef Bacik 							     sk);
29330fff923SEric Dumazet 			}
29430fff923SEric Dumazet 			if (exist)
29530fff923SEric Dumazet 				goto fail_unlock;
29630fff923SEric Dumazet 			else
29730fff923SEric Dumazet 				goto found;
29830fff923SEric Dumazet 		}
29930fff923SEric Dumazet scan_primary_hash:
300fe38d2a1SJosef Bacik 		if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0))
301645ca708SEric Dumazet 			goto fail_unlock;
302645ca708SEric Dumazet 	}
30398322f22SEric Dumazet found:
304c720c7e8SEric Dumazet 	inet_sk(sk)->inet_num = snum;
305d4cada4aSEric Dumazet 	udp_sk(sk)->udp_port_hash = snum;
306d4cada4aSEric Dumazet 	udp_sk(sk)->udp_portaddr_hash ^= snum;
3071da177e4SLinus Torvalds 	if (sk_unhashed(sk)) {
308e32ea7e7SCraig Gallek 		if (sk->sk_reuseport &&
309fe38d2a1SJosef Bacik 		    udp_reuseport_add_sock(sk, hslot)) {
310e32ea7e7SCraig Gallek 			inet_sk(sk)->inet_num = 0;
311e32ea7e7SCraig Gallek 			udp_sk(sk)->udp_port_hash = 0;
312e32ea7e7SCraig Gallek 			udp_sk(sk)->udp_portaddr_hash ^= snum;
313e32ea7e7SCraig Gallek 			goto fail_unlock;
314e32ea7e7SCraig Gallek 		}
315e32ea7e7SCraig Gallek 
316ca065d0cSEric Dumazet 		sk_add_node_rcu(sk, &hslot->head);
317fdcc8aa9SEric Dumazet 		hslot->count++;
318c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
319512615b6SEric Dumazet 
320512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
321512615b6SEric Dumazet 		spin_lock(&hslot2->lock);
322d894ba18SCraig Gallek 		if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
323d894ba18SCraig Gallek 		    sk->sk_family == AF_INET6)
3241602f49bSDavid S. Miller 			hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
325d894ba18SCraig Gallek 					   &hslot2->head);
326d894ba18SCraig Gallek 		else
327ca065d0cSEric Dumazet 			hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
328512615b6SEric Dumazet 					   &hslot2->head);
329512615b6SEric Dumazet 		hslot2->count++;
330512615b6SEric Dumazet 		spin_unlock(&hslot2->lock);
3311da177e4SLinus Torvalds 	}
332ca065d0cSEric Dumazet 	sock_set_flag(sk, SOCK_RCU_FREE);
33325030a7fSGerrit Renker 	error = 0;
334645ca708SEric Dumazet fail_unlock:
335645ca708SEric Dumazet 	spin_unlock_bh(&hslot->lock);
3361da177e4SLinus Torvalds fail:
33725030a7fSGerrit Renker 	return error;
3381da177e4SLinus Torvalds }
339c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port);
3401da177e4SLinus Torvalds 
3416ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum)
342db8dac20SDavid S. Miller {
34330fff923SEric Dumazet 	unsigned int hash2_nulladdr =
344f0b1e64cSMartin KaFai Lau 		ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
34530fff923SEric Dumazet 	unsigned int hash2_partial =
346f0b1e64cSMartin KaFai Lau 		ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
34730fff923SEric Dumazet 
348d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
34930fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
350fe38d2a1SJosef Bacik 	return udp_lib_get_port(sk, snum, hash2_nulladdr);
351db8dac20SDavid S. Miller }
352db8dac20SDavid S. Miller 
353d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net,
354d1e37288SSu, Xuemin 			 __be32 saddr, __be16 sport,
355fb74c277SDavid Ahern 			 __be32 daddr, unsigned short hnum,
35673545373STim Beale 			 int dif, int sdif)
357645ca708SEric Dumazet {
35860c04aecSJoe Perches 	int score;
35960c04aecSJoe Perches 	struct inet_sock *inet;
3606da5b0f0SMike Manning 	bool dev_match;
361645ca708SEric Dumazet 
36260c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
36360c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
36460c04aecSJoe Perches 	    ipv6_only_sock(sk))
36560c04aecSJoe Perches 		return -1;
366645ca708SEric Dumazet 
3674cdeeee9SPeter Oskolkov 	if (sk->sk_rcv_saddr != daddr)
368645ca708SEric Dumazet 		return -1;
36960c04aecSJoe Perches 
3704cdeeee9SPeter Oskolkov 	score = (sk->sk_family == PF_INET) ? 2 : 1;
3714cdeeee9SPeter Oskolkov 
3724cdeeee9SPeter Oskolkov 	inet = inet_sk(sk);
373c720c7e8SEric Dumazet 	if (inet->inet_daddr) {
374c720c7e8SEric Dumazet 		if (inet->inet_daddr != saddr)
375645ca708SEric Dumazet 			return -1;
376ba418fa3STom Herbert 		score += 4;
377645ca708SEric Dumazet 	}
37860c04aecSJoe Perches 
379c720c7e8SEric Dumazet 	if (inet->inet_dport) {
380c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
381645ca708SEric Dumazet 			return -1;
382ba418fa3STom Herbert 		score += 4;
383645ca708SEric Dumazet 	}
38460c04aecSJoe Perches 
3856da5b0f0SMike Manning 	dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if,
3866da5b0f0SMike Manning 					dif, sdif);
38769678bcdSPaolo Abeni 	if (!dev_match)
388645ca708SEric Dumazet 		return -1;
389ba418fa3STom Herbert 	score += 4;
390fb74c277SDavid Ahern 
3917170a977SEric Dumazet 	if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
39270da268bSEric Dumazet 		score++;
393645ca708SEric Dumazet 	return score;
394645ca708SEric Dumazet }
395645ca708SEric Dumazet 
3966eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
39765cd8033SHannes Frederic Sowa 		       const __u16 lport, const __be32 faddr,
39865cd8033SHannes Frederic Sowa 		       const __be16 fport)
39965cd8033SHannes Frederic Sowa {
4001bbdceefSHannes Frederic Sowa 	static u32 udp_ehash_secret __read_mostly;
4011bbdceefSHannes Frederic Sowa 
4021bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret));
4031bbdceefSHannes Frederic Sowa 
40465cd8033SHannes Frederic Sowa 	return __inet_ehashfn(laddr, lport, faddr, fport,
4051bbdceefSHannes Frederic Sowa 			      udp_ehash_secret + net_hash_mix(net));
40665cd8033SHannes Frederic Sowa }
40765cd8033SHannes Frederic Sowa 
408d1e37288SSu, Xuemin /* called with rcu_read_lock() */
4095051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net,
4105051ebd2SEric Dumazet 				     __be32 saddr, __be16 sport,
411fb74c277SDavid Ahern 				     __be32 daddr, unsigned int hnum,
41273545373STim Beale 				     int dif, int sdif,
413d1e37288SSu, Xuemin 				     struct udp_hslot *hslot2,
4141134158bSCraig Gallek 				     struct sk_buff *skb)
4155051ebd2SEric Dumazet {
4165051ebd2SEric Dumazet 	struct sock *sk, *result;
417e94a62f5SPaolo Abeni 	int score, badness;
418ba418fa3STom Herbert 	u32 hash = 0;
4195051ebd2SEric Dumazet 
4205051ebd2SEric Dumazet 	result = NULL;
421ba418fa3STom Herbert 	badness = 0;
422ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
423d1e37288SSu, Xuemin 		score = compute_score(sk, net, saddr, sport,
42473545373STim Beale 				      daddr, hnum, dif, sdif);
4255051ebd2SEric Dumazet 		if (score > badness) {
426acdcecc6SWillem de Bruijn 			if (sk->sk_reuseport &&
427acdcecc6SWillem de Bruijn 			    sk->sk_state != TCP_ESTABLISHED) {
42865cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
4297c0cadc6SEric Dumazet 						   saddr, sport);
430ca065d0cSEric Dumazet 				result = reuseport_select_sock(sk, hash, skb,
4311134158bSCraig Gallek 							sizeof(struct udphdr));
432acdcecc6SWillem de Bruijn 				if (result && !reuseport_has_conns(sk, false))
433ca065d0cSEric Dumazet 					return result;
434ba418fa3STom Herbert 			}
435ca065d0cSEric Dumazet 			badness = score;
436ca065d0cSEric Dumazet 			result = sk;
4375051ebd2SEric Dumazet 		}
4385051ebd2SEric Dumazet 	}
4395051ebd2SEric Dumazet 	return result;
4405051ebd2SEric Dumazet }
4415051ebd2SEric Dumazet 
442db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
443db8dac20SDavid S. Miller  * harder than this. -DaveM
444db8dac20SDavid S. Miller  */
445fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
446fb74c277SDavid Ahern 		__be16 sport, __be32 daddr, __be16 dport, int dif,
447fb74c277SDavid Ahern 		int sdif, struct udp_table *udptable, struct sk_buff *skb)
448db8dac20SDavid S. Miller {
4494cdeeee9SPeter Oskolkov 	struct sock *result;
450db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(dport);
4514cdeeee9SPeter Oskolkov 	unsigned int hash2, slot2;
4524cdeeee9SPeter Oskolkov 	struct udp_hslot *hslot2;
453db8dac20SDavid S. Miller 
454f0b1e64cSMartin KaFai Lau 	hash2 = ipv4_portaddr_hash(net, daddr, hnum);
4555051ebd2SEric Dumazet 	slot2 = hash2 & udptable->mask;
4565051ebd2SEric Dumazet 	hslot2 = &udptable->hash2[slot2];
4575051ebd2SEric Dumazet 
4585051ebd2SEric Dumazet 	result = udp4_lib_lookup2(net, saddr, sport,
459fb74c277SDavid Ahern 				  daddr, hnum, dif, sdif,
46073545373STim Beale 				  hslot2, skb);
4615051ebd2SEric Dumazet 	if (!result) {
462f0b1e64cSMartin KaFai Lau 		hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
4635051ebd2SEric Dumazet 		slot2 = hash2 & udptable->mask;
4645051ebd2SEric Dumazet 		hslot2 = &udptable->hash2[slot2];
4655051ebd2SEric Dumazet 
4661223c67cSJorge Boncompte [DTI2] 		result = udp4_lib_lookup2(net, saddr, sport,
4674cdeeee9SPeter Oskolkov 					  htonl(INADDR_ANY), hnum, dif, sdif,
46873545373STim Beale 					  hslot2, skb);
4695051ebd2SEric Dumazet 	}
47088e235b8SEnrico Weigelt 	if (IS_ERR(result))
4718217ca65SMartin KaFai Lau 		return NULL;
4725051ebd2SEric Dumazet 	return result;
4735051ebd2SEric Dumazet }
474fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
475db8dac20SDavid S. Miller 
476607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
477607c4aafSKOVACS Krisztian 						 __be16 sport, __be16 dport,
478645ca708SEric Dumazet 						 struct udp_table *udptable)
479607c4aafSKOVACS Krisztian {
480607c4aafSKOVACS Krisztian 	const struct iphdr *iph = ip_hdr(skb);
481607c4aafSKOVACS Krisztian 
482ed7cbbceSAlexander Duyck 	return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
483607c4aafSKOVACS Krisztian 				 iph->daddr, dport, inet_iif(skb),
484fb74c277SDavid Ahern 				 inet_sdif(skb), udptable, skb);
485607c4aafSKOVACS Krisztian }
486607c4aafSKOVACS Krisztian 
48763058308STom Herbert struct sock *udp4_lib_lookup_skb(struct sk_buff *skb,
48863058308STom Herbert 				 __be16 sport, __be16 dport)
48963058308STom Herbert {
490257a525fSMartin KaFai Lau 	const struct iphdr *iph = ip_hdr(skb);
491257a525fSMartin KaFai Lau 
492257a525fSMartin KaFai Lau 	return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
493257a525fSMartin KaFai Lau 				 iph->daddr, dport, inet_iif(skb),
494257a525fSMartin KaFai Lau 				 inet_sdif(skb), &udp_table, NULL);
49563058308STom Herbert }
49663058308STom Herbert EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb);
49763058308STom Herbert 
498ca065d0cSEric Dumazet /* Must be called under rcu_read_lock().
499ca065d0cSEric Dumazet  * Does increment socket refcount.
500ca065d0cSEric Dumazet  */
5016e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4)
502bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
503bcd41303SKOVACS Krisztian 			     __be32 daddr, __be16 dport, int dif)
504bcd41303SKOVACS Krisztian {
505ca065d0cSEric Dumazet 	struct sock *sk;
506ca065d0cSEric Dumazet 
507ca065d0cSEric Dumazet 	sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport,
508fb74c277SDavid Ahern 			       dif, 0, &udp_table, NULL);
50941c6d650SReshetova, Elena 	if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
510ca065d0cSEric Dumazet 		sk = NULL;
511ca065d0cSEric Dumazet 	return sk;
512bcd41303SKOVACS Krisztian }
513bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup);
514ca065d0cSEric Dumazet #endif
515bcd41303SKOVACS Krisztian 
516421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
517421b3885SShawn Bohrer 				       __be16 loc_port, __be32 loc_addr,
518421b3885SShawn Bohrer 				       __be16 rmt_port, __be32 rmt_addr,
519fb74c277SDavid Ahern 				       int dif, int sdif, unsigned short hnum)
520421b3885SShawn Bohrer {
521421b3885SShawn Bohrer 	struct inet_sock *inet = inet_sk(sk);
522421b3885SShawn Bohrer 
523421b3885SShawn Bohrer 	if (!net_eq(sock_net(sk), net) ||
524421b3885SShawn Bohrer 	    udp_sk(sk)->udp_port_hash != hnum ||
525421b3885SShawn Bohrer 	    (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
526421b3885SShawn Bohrer 	    (inet->inet_dport != rmt_port && inet->inet_dport) ||
527421b3885SShawn Bohrer 	    (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
528421b3885SShawn Bohrer 	    ipv6_only_sock(sk) ||
52982ba25c6STim Beale 	    !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
530421b3885SShawn Bohrer 		return false;
53160d9b031SDavid Ahern 	if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif))
532421b3885SShawn Bohrer 		return false;
533421b3885SShawn Bohrer 	return true;
534421b3885SShawn Bohrer }
535421b3885SShawn Bohrer 
536a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key);
537a36e185eSStefano Brivio void udp_encap_enable(void)
538a36e185eSStefano Brivio {
5399c480601SPaolo Abeni 	static_branch_inc(&udp_encap_needed_key);
540a36e185eSStefano Brivio }
541a36e185eSStefano Brivio EXPORT_SYMBOL(udp_encap_enable);
542a36e185eSStefano Brivio 
543e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
544e7cc0824SStefano Brivio  * through error handlers in encapsulations looking for a match.
545e7cc0824SStefano Brivio  */
546e7cc0824SStefano Brivio static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info)
547e7cc0824SStefano Brivio {
548e7cc0824SStefano Brivio 	int i;
549e7cc0824SStefano Brivio 
550e7cc0824SStefano Brivio 	for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
551e7cc0824SStefano Brivio 		int (*handler)(struct sk_buff *skb, u32 info);
55292b95364SPaolo Abeni 		const struct ip_tunnel_encap_ops *encap;
553e7cc0824SStefano Brivio 
55492b95364SPaolo Abeni 		encap = rcu_dereference(iptun_encaps[i]);
55592b95364SPaolo Abeni 		if (!encap)
556e7cc0824SStefano Brivio 			continue;
55792b95364SPaolo Abeni 		handler = encap->err_handler;
558e7cc0824SStefano Brivio 		if (handler && !handler(skb, info))
559e7cc0824SStefano Brivio 			return 0;
560e7cc0824SStefano Brivio 	}
561e7cc0824SStefano Brivio 
562e7cc0824SStefano Brivio 	return -ENOENT;
563e7cc0824SStefano Brivio }
564e7cc0824SStefano Brivio 
565a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without
566a36e185eSStefano Brivio  * reversing source and destination port: this will match tunnels that force the
567a36e185eSStefano Brivio  * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
568a36e185eSStefano Brivio  * lwtunnels might actually break this assumption by being configured with
569a36e185eSStefano Brivio  * different destination ports on endpoints, in this case we won't be able to
570a36e185eSStefano Brivio  * trace ICMP messages back to them.
571a36e185eSStefano Brivio  *
572e7cc0824SStefano Brivio  * If this doesn't match any socket, probe tunnels with arbitrary destination
573e7cc0824SStefano Brivio  * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
574e7cc0824SStefano Brivio  * we've sent packets to won't necessarily match the local destination port.
575e7cc0824SStefano Brivio  *
576a36e185eSStefano Brivio  * Then ask the tunnel implementation to match the error against a valid
577a36e185eSStefano Brivio  * association.
578a36e185eSStefano Brivio  *
579e7cc0824SStefano Brivio  * Return an error if we can't find a match, the socket if we need further
580e7cc0824SStefano Brivio  * processing, zero otherwise.
581a36e185eSStefano Brivio  */
582a36e185eSStefano Brivio static struct sock *__udp4_lib_err_encap(struct net *net,
583a36e185eSStefano Brivio 					 const struct iphdr *iph,
584a36e185eSStefano Brivio 					 struct udphdr *uh,
585a36e185eSStefano Brivio 					 struct udp_table *udptable,
586e7cc0824SStefano Brivio 					 struct sk_buff *skb, u32 info)
587a36e185eSStefano Brivio {
588a36e185eSStefano Brivio 	int network_offset, transport_offset;
589a36e185eSStefano Brivio 	struct sock *sk;
590a36e185eSStefano Brivio 
591a36e185eSStefano Brivio 	network_offset = skb_network_offset(skb);
592a36e185eSStefano Brivio 	transport_offset = skb_transport_offset(skb);
593a36e185eSStefano Brivio 
594a36e185eSStefano Brivio 	/* Network header needs to point to the outer IPv4 header inside ICMP */
595a36e185eSStefano Brivio 	skb_reset_network_header(skb);
596a36e185eSStefano Brivio 
597a36e185eSStefano Brivio 	/* Transport header needs to point to the UDP header */
598a36e185eSStefano Brivio 	skb_set_transport_header(skb, iph->ihl << 2);
599a36e185eSStefano Brivio 
600e7cc0824SStefano Brivio 	sk = __udp4_lib_lookup(net, iph->daddr, uh->source,
601e7cc0824SStefano Brivio 			       iph->saddr, uh->dest, skb->dev->ifindex, 0,
602e7cc0824SStefano Brivio 			       udptable, NULL);
603e7cc0824SStefano Brivio 	if (sk) {
604e7cc0824SStefano Brivio 		int (*lookup)(struct sock *sk, struct sk_buff *skb);
605e7cc0824SStefano Brivio 		struct udp_sock *up = udp_sk(sk);
606e7cc0824SStefano Brivio 
607a36e185eSStefano Brivio 		lookup = READ_ONCE(up->encap_err_lookup);
608a36e185eSStefano Brivio 		if (!lookup || lookup(sk, skb))
609a36e185eSStefano Brivio 			sk = NULL;
610e7cc0824SStefano Brivio 	}
611e7cc0824SStefano Brivio 
612e7cc0824SStefano Brivio 	if (!sk)
613e7cc0824SStefano Brivio 		sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info));
614a36e185eSStefano Brivio 
615a36e185eSStefano Brivio 	skb_set_transport_header(skb, transport_offset);
616a36e185eSStefano Brivio 	skb_set_network_header(skb, network_offset);
617a36e185eSStefano Brivio 
618a36e185eSStefano Brivio 	return sk;
619a36e185eSStefano Brivio }
620a36e185eSStefano Brivio 
621db8dac20SDavid S. Miller /*
622db8dac20SDavid S. Miller  * This routine is called by the ICMP module when it gets some
623db8dac20SDavid S. Miller  * sort of error condition.  If err < 0 then the socket should
624db8dac20SDavid S. Miller  * be closed and the error returned to the user.  If err > 0
625db8dac20SDavid S. Miller  * it's just the icmp type << 8 | icmp code.
626db8dac20SDavid S. Miller  * Header points to the ip header of the error packet. We move
627db8dac20SDavid S. Miller  * on past this. Then (as it used to claim before adjustment)
628db8dac20SDavid S. Miller  * header points to the first 8 bytes of the udp header.  We need
629db8dac20SDavid S. Miller  * to find the appropriate port.
630db8dac20SDavid S. Miller  */
631db8dac20SDavid S. Miller 
63232bbd879SStefano Brivio int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
633db8dac20SDavid S. Miller {
634db8dac20SDavid S. Miller 	struct inet_sock *inet;
635b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *)skb->data;
636db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
637db8dac20SDavid S. Miller 	const int type = icmp_hdr(skb)->type;
638db8dac20SDavid S. Miller 	const int code = icmp_hdr(skb)->code;
639a36e185eSStefano Brivio 	bool tunnel = false;
640db8dac20SDavid S. Miller 	struct sock *sk;
641db8dac20SDavid S. Miller 	int harderr;
642db8dac20SDavid S. Miller 	int err;
643fd54d716SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
644db8dac20SDavid S. Miller 
645fd54d716SPavel Emelyanov 	sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
646f64bf6b8SMike Manning 			       iph->saddr, uh->source, skb->dev->ifindex,
647f64bf6b8SMike Manning 			       inet_sdif(skb), udptable, NULL);
64851456b29SIan Morris 	if (!sk) {
649a36e185eSStefano Brivio 		/* No socket for error: try tunnels before discarding */
650e7cc0824SStefano Brivio 		sk = ERR_PTR(-ENOENT);
651e7cc0824SStefano Brivio 		if (static_branch_unlikely(&udp_encap_needed_key)) {
652e7cc0824SStefano Brivio 			sk = __udp4_lib_err_encap(net, iph, uh, udptable, skb,
653e7cc0824SStefano Brivio 						  info);
654e7cc0824SStefano Brivio 			if (!sk)
655e7cc0824SStefano Brivio 				return 0;
656a36e185eSStefano Brivio 		}
657e7cc0824SStefano Brivio 
658e7cc0824SStefano Brivio 		if (IS_ERR(sk)) {
659e7cc0824SStefano Brivio 			__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
660e7cc0824SStefano Brivio 			return PTR_ERR(sk);
661e7cc0824SStefano Brivio 		}
662e7cc0824SStefano Brivio 
663a36e185eSStefano Brivio 		tunnel = true;
664db8dac20SDavid S. Miller 	}
665db8dac20SDavid S. Miller 
666db8dac20SDavid S. Miller 	err = 0;
667db8dac20SDavid S. Miller 	harderr = 0;
668db8dac20SDavid S. Miller 	inet = inet_sk(sk);
669db8dac20SDavid S. Miller 
670db8dac20SDavid S. Miller 	switch (type) {
671db8dac20SDavid S. Miller 	default:
672db8dac20SDavid S. Miller 	case ICMP_TIME_EXCEEDED:
673db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
674db8dac20SDavid S. Miller 		break;
675db8dac20SDavid S. Miller 	case ICMP_SOURCE_QUENCH:
676db8dac20SDavid S. Miller 		goto out;
677db8dac20SDavid S. Miller 	case ICMP_PARAMETERPROB:
678db8dac20SDavid S. Miller 		err = EPROTO;
679db8dac20SDavid S. Miller 		harderr = 1;
680db8dac20SDavid S. Miller 		break;
681db8dac20SDavid S. Miller 	case ICMP_DEST_UNREACH:
682db8dac20SDavid S. Miller 		if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
68336393395SDavid S. Miller 			ipv4_sk_update_pmtu(skb, sk, info);
684db8dac20SDavid S. Miller 			if (inet->pmtudisc != IP_PMTUDISC_DONT) {
685db8dac20SDavid S. Miller 				err = EMSGSIZE;
686db8dac20SDavid S. Miller 				harderr = 1;
687db8dac20SDavid S. Miller 				break;
688db8dac20SDavid S. Miller 			}
689db8dac20SDavid S. Miller 			goto out;
690db8dac20SDavid S. Miller 		}
691db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
692db8dac20SDavid S. Miller 		if (code <= NR_ICMP_UNREACH) {
693db8dac20SDavid S. Miller 			harderr = icmp_err_convert[code].fatal;
694db8dac20SDavid S. Miller 			err = icmp_err_convert[code].errno;
695db8dac20SDavid S. Miller 		}
696db8dac20SDavid S. Miller 		break;
69755be7a9cSDavid S. Miller 	case ICMP_REDIRECT:
69855be7a9cSDavid S. Miller 		ipv4_sk_redirect(skb, sk);
6991a462d18SDuan Jiong 		goto out;
700db8dac20SDavid S. Miller 	}
701db8dac20SDavid S. Miller 
702db8dac20SDavid S. Miller 	/*
703db8dac20SDavid S. Miller 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
704db8dac20SDavid S. Miller 	 *	4.1.3.3.
705db8dac20SDavid S. Miller 	 */
706a36e185eSStefano Brivio 	if (tunnel) {
707a36e185eSStefano Brivio 		/* ...not for tunnels though: we don't have a sending socket */
708a36e185eSStefano Brivio 		goto out;
709a36e185eSStefano Brivio 	}
710db8dac20SDavid S. Miller 	if (!inet->recverr) {
711db8dac20SDavid S. Miller 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
712db8dac20SDavid S. Miller 			goto out;
713b1faf566SEric Dumazet 	} else
714db8dac20SDavid S. Miller 		ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
715b1faf566SEric Dumazet 
716db8dac20SDavid S. Miller 	sk->sk_err = err;
717db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
718db8dac20SDavid S. Miller out:
71932bbd879SStefano Brivio 	return 0;
720db8dac20SDavid S. Miller }
721db8dac20SDavid S. Miller 
72232bbd879SStefano Brivio int udp_err(struct sk_buff *skb, u32 info)
723db8dac20SDavid S. Miller {
72432bbd879SStefano Brivio 	return __udp4_lib_err(skb, info, &udp_table);
725db8dac20SDavid S. Miller }
726db8dac20SDavid S. Miller 
727db8dac20SDavid S. Miller /*
728db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
729db8dac20SDavid S. Miller  */
73036d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk)
731db8dac20SDavid S. Miller {
732db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
733db8dac20SDavid S. Miller 
734db8dac20SDavid S. Miller 	if (up->pending) {
735db8dac20SDavid S. Miller 		up->len = 0;
736db8dac20SDavid S. Miller 		up->pending = 0;
737db8dac20SDavid S. Miller 		ip_flush_pending_frames(sk);
738db8dac20SDavid S. Miller 	}
739db8dac20SDavid S. Miller }
74036d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames);
741db8dac20SDavid S. Miller 
742db8dac20SDavid S. Miller /**
743f6b9664fSHerbert Xu  * 	udp4_hwcsum  -  handle outgoing HW checksumming
744db8dac20SDavid S. Miller  * 	@skb: 	sk_buff containing the filled-in UDP header
745db8dac20SDavid S. Miller  * 	        (checksum field must be zeroed out)
746f6b9664fSHerbert Xu  *	@src:	source IP address
747f6b9664fSHerbert Xu  *	@dst:	destination IP address
748db8dac20SDavid S. Miller  */
749c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
750db8dac20SDavid S. Miller {
751db8dac20SDavid S. Miller 	struct udphdr *uh = udp_hdr(skb);
752f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
753f6b9664fSHerbert Xu 	int len = skb->len - offset;
754f6b9664fSHerbert Xu 	int hlen = len;
755db8dac20SDavid S. Miller 	__wsum csum = 0;
756db8dac20SDavid S. Miller 
757ebbe495fSWANG Cong 	if (!skb_has_frag_list(skb)) {
758db8dac20SDavid S. Miller 		/*
759db8dac20SDavid S. Miller 		 * Only one fragment on the socket.
760db8dac20SDavid S. Miller 		 */
761db8dac20SDavid S. Miller 		skb->csum_start = skb_transport_header(skb) - skb->head;
762db8dac20SDavid S. Miller 		skb->csum_offset = offsetof(struct udphdr, check);
763f6b9664fSHerbert Xu 		uh->check = ~csum_tcpudp_magic(src, dst, len,
764f6b9664fSHerbert Xu 					       IPPROTO_UDP, 0);
765db8dac20SDavid S. Miller 	} else {
766ebbe495fSWANG Cong 		struct sk_buff *frags;
767ebbe495fSWANG Cong 
768db8dac20SDavid S. Miller 		/*
769db8dac20SDavid S. Miller 		 * HW-checksum won't work as there are two or more
770db8dac20SDavid S. Miller 		 * fragments on the socket so that all csums of sk_buffs
771db8dac20SDavid S. Miller 		 * should be together
772db8dac20SDavid S. Miller 		 */
773ebbe495fSWANG Cong 		skb_walk_frags(skb, frags) {
774f6b9664fSHerbert Xu 			csum = csum_add(csum, frags->csum);
775f6b9664fSHerbert Xu 			hlen -= frags->len;
776ebbe495fSWANG Cong 		}
777db8dac20SDavid S. Miller 
778f6b9664fSHerbert Xu 		csum = skb_checksum(skb, offset, hlen, csum);
779db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
780db8dac20SDavid S. Miller 
781db8dac20SDavid S. Miller 		uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
782db8dac20SDavid S. Miller 		if (uh->check == 0)
783db8dac20SDavid S. Miller 			uh->check = CSUM_MANGLED_0;
784db8dac20SDavid S. Miller 	}
785db8dac20SDavid S. Miller }
786c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum);
787db8dac20SDavid S. Miller 
788af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended
789af5fcba7STom Herbert  * for the simple case like when setting the checksum for a UDP tunnel.
790af5fcba7STom Herbert  */
791af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb,
792af5fcba7STom Herbert 		  __be32 saddr, __be32 daddr, int len)
793af5fcba7STom Herbert {
794af5fcba7STom Herbert 	struct udphdr *uh = udp_hdr(skb);
795af5fcba7STom Herbert 
796179bc67fSEdward Cree 	if (nocheck) {
797af5fcba7STom Herbert 		uh->check = 0;
798179bc67fSEdward Cree 	} else if (skb_is_gso(skb)) {
799af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
800179bc67fSEdward Cree 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
801179bc67fSEdward Cree 		uh->check = 0;
802179bc67fSEdward Cree 		uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb));
803179bc67fSEdward Cree 		if (uh->check == 0)
804179bc67fSEdward Cree 			uh->check = CSUM_MANGLED_0;
805d75f1306SEdward Cree 	} else {
806af5fcba7STom Herbert 		skb->ip_summed = CHECKSUM_PARTIAL;
807af5fcba7STom Herbert 		skb->csum_start = skb_transport_header(skb) - skb->head;
808af5fcba7STom Herbert 		skb->csum_offset = offsetof(struct udphdr, check);
809af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
810af5fcba7STom Herbert 	}
811af5fcba7STom Herbert }
812af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum);
813af5fcba7STom Herbert 
814bec1f6f6SWillem de Bruijn static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4,
815bec1f6f6SWillem de Bruijn 			struct inet_cork *cork)
816f6b9664fSHerbert Xu {
817f6b9664fSHerbert Xu 	struct sock *sk = skb->sk;
818f6b9664fSHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
819f6b9664fSHerbert Xu 	struct udphdr *uh;
820f6b9664fSHerbert Xu 	int err = 0;
821f6b9664fSHerbert Xu 	int is_udplite = IS_UDPLITE(sk);
822f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
823f6b9664fSHerbert Xu 	int len = skb->len - offset;
8244094871dSJosh Hunt 	int datalen = len - sizeof(*uh);
825f6b9664fSHerbert Xu 	__wsum csum = 0;
826f6b9664fSHerbert Xu 
827f6b9664fSHerbert Xu 	/*
828f6b9664fSHerbert Xu 	 * Create a UDP header
829f6b9664fSHerbert Xu 	 */
830f6b9664fSHerbert Xu 	uh = udp_hdr(skb);
831f6b9664fSHerbert Xu 	uh->source = inet->inet_sport;
83279ab0531SDavid S. Miller 	uh->dest = fl4->fl4_dport;
833f6b9664fSHerbert Xu 	uh->len = htons(len);
834f6b9664fSHerbert Xu 	uh->check = 0;
835f6b9664fSHerbert Xu 
836bec1f6f6SWillem de Bruijn 	if (cork->gso_size) {
837bec1f6f6SWillem de Bruijn 		const int hlen = skb_network_header_len(skb) +
838bec1f6f6SWillem de Bruijn 				 sizeof(struct udphdr);
839bec1f6f6SWillem de Bruijn 
8400f149c9fSWillem de Bruijn 		if (hlen + cork->gso_size > cork->fragsize) {
8410f149c9fSWillem de Bruijn 			kfree_skb(skb);
842bec1f6f6SWillem de Bruijn 			return -EINVAL;
8430f149c9fSWillem de Bruijn 		}
8440f149c9fSWillem de Bruijn 		if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) {
8450f149c9fSWillem de Bruijn 			kfree_skb(skb);
846bec1f6f6SWillem de Bruijn 			return -EINVAL;
8470f149c9fSWillem de Bruijn 		}
8480f149c9fSWillem de Bruijn 		if (sk->sk_no_check_tx) {
8490f149c9fSWillem de Bruijn 			kfree_skb(skb);
850a8c744a8SWillem de Bruijn 			return -EINVAL;
8510f149c9fSWillem de Bruijn 		}
852ff06342cSWillem de Bruijn 		if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
8530f149c9fSWillem de Bruijn 		    dst_xfrm(skb_dst(skb))) {
8540f149c9fSWillem de Bruijn 			kfree_skb(skb);
855bec1f6f6SWillem de Bruijn 			return -EIO;
8560f149c9fSWillem de Bruijn 		}
857bec1f6f6SWillem de Bruijn 
8584094871dSJosh Hunt 		if (datalen > cork->gso_size) {
859bec1f6f6SWillem de Bruijn 			skb_shinfo(skb)->gso_size = cork->gso_size;
860bec1f6f6SWillem de Bruijn 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
8614094871dSJosh Hunt 			skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
862dfec0ee2SAlexander Duyck 								 cork->gso_size);
8634094871dSJosh Hunt 		}
864a8c744a8SWillem de Bruijn 		goto csum_partial;
865bec1f6f6SWillem de Bruijn 	}
866bec1f6f6SWillem de Bruijn 
867f6b9664fSHerbert Xu 	if (is_udplite)  				 /*     UDP-Lite      */
868f6b9664fSHerbert Xu 		csum = udplite_csum(skb);
869f6b9664fSHerbert Xu 
870ab2fb7e3SWillem de Bruijn 	else if (sk->sk_no_check_tx) {			 /* UDP csum off */
871f6b9664fSHerbert Xu 
872f6b9664fSHerbert Xu 		skb->ip_summed = CHECKSUM_NONE;
873f6b9664fSHerbert Xu 		goto send;
874f6b9664fSHerbert Xu 
875f6b9664fSHerbert Xu 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
876a8c744a8SWillem de Bruijn csum_partial:
877f6b9664fSHerbert Xu 
87879ab0531SDavid S. Miller 		udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
879f6b9664fSHerbert Xu 		goto send;
880f6b9664fSHerbert Xu 
881f6b9664fSHerbert Xu 	} else
882f6b9664fSHerbert Xu 		csum = udp_csum(skb);
883f6b9664fSHerbert Xu 
884f6b9664fSHerbert Xu 	/* add protocol-dependent pseudo-header */
88579ab0531SDavid S. Miller 	uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
886f6b9664fSHerbert Xu 				      sk->sk_protocol, csum);
887f6b9664fSHerbert Xu 	if (uh->check == 0)
888f6b9664fSHerbert Xu 		uh->check = CSUM_MANGLED_0;
889f6b9664fSHerbert Xu 
890f6b9664fSHerbert Xu send:
891b5ec8eeaSEric Dumazet 	err = ip_send_skb(sock_net(sk), skb);
892f6b9664fSHerbert Xu 	if (err) {
893f6b9664fSHerbert Xu 		if (err == -ENOBUFS && !inet->recverr) {
8946aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk),
895f6b9664fSHerbert Xu 				      UDP_MIB_SNDBUFERRORS, is_udplite);
896f6b9664fSHerbert Xu 			err = 0;
897f6b9664fSHerbert Xu 		}
898f6b9664fSHerbert Xu 	} else
8996aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk),
900f6b9664fSHerbert Xu 			      UDP_MIB_OUTDATAGRAMS, is_udplite);
901f6b9664fSHerbert Xu 	return err;
902f6b9664fSHerbert Xu }
903f6b9664fSHerbert Xu 
904db8dac20SDavid S. Miller /*
905db8dac20SDavid S. Miller  * Push out all pending data as one UDP datagram. Socket is locked.
906db8dac20SDavid S. Miller  */
9078822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk)
908db8dac20SDavid S. Miller {
909db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
910db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
911b6f21b26SDavid S. Miller 	struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
912db8dac20SDavid S. Miller 	struct sk_buff *skb;
913db8dac20SDavid S. Miller 	int err = 0;
914db8dac20SDavid S. Miller 
91577968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
916f6b9664fSHerbert Xu 	if (!skb)
917db8dac20SDavid S. Miller 		goto out;
918db8dac20SDavid S. Miller 
919bec1f6f6SWillem de Bruijn 	err = udp_send_skb(skb, fl4, &inet->cork.base);
920db8dac20SDavid S. Miller 
921db8dac20SDavid S. Miller out:
922db8dac20SDavid S. Miller 	up->len = 0;
923db8dac20SDavid S. Miller 	up->pending = 0;
924db8dac20SDavid S. Miller 	return err;
925db8dac20SDavid S. Miller }
9268822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames);
927db8dac20SDavid S. Miller 
9282e8de857SWillem de Bruijn static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size)
9292e8de857SWillem de Bruijn {
9302e8de857SWillem de Bruijn 	switch (cmsg->cmsg_type) {
9312e8de857SWillem de Bruijn 	case UDP_SEGMENT:
9322e8de857SWillem de Bruijn 		if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16)))
9332e8de857SWillem de Bruijn 			return -EINVAL;
9342e8de857SWillem de Bruijn 		*gso_size = *(__u16 *)CMSG_DATA(cmsg);
9352e8de857SWillem de Bruijn 		return 0;
9362e8de857SWillem de Bruijn 	default:
9372e8de857SWillem de Bruijn 		return -EINVAL;
9382e8de857SWillem de Bruijn 	}
9392e8de857SWillem de Bruijn }
9402e8de857SWillem de Bruijn 
9412e8de857SWillem de Bruijn int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size)
9422e8de857SWillem de Bruijn {
9432e8de857SWillem de Bruijn 	struct cmsghdr *cmsg;
9442e8de857SWillem de Bruijn 	bool need_ip = false;
9452e8de857SWillem de Bruijn 	int err;
9462e8de857SWillem de Bruijn 
9472e8de857SWillem de Bruijn 	for_each_cmsghdr(cmsg, msg) {
9482e8de857SWillem de Bruijn 		if (!CMSG_OK(msg, cmsg))
9492e8de857SWillem de Bruijn 			return -EINVAL;
9502e8de857SWillem de Bruijn 
9512e8de857SWillem de Bruijn 		if (cmsg->cmsg_level != SOL_UDP) {
9522e8de857SWillem de Bruijn 			need_ip = true;
9532e8de857SWillem de Bruijn 			continue;
9542e8de857SWillem de Bruijn 		}
9552e8de857SWillem de Bruijn 
9562e8de857SWillem de Bruijn 		err = __udp_cmsg_send(cmsg, gso_size);
9572e8de857SWillem de Bruijn 		if (err)
9582e8de857SWillem de Bruijn 			return err;
9592e8de857SWillem de Bruijn 	}
9602e8de857SWillem de Bruijn 
9612e8de857SWillem de Bruijn 	return need_ip;
9622e8de857SWillem de Bruijn }
9632e8de857SWillem de Bruijn EXPORT_SYMBOL_GPL(udp_cmsg_send);
9642e8de857SWillem de Bruijn 
9651b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
966db8dac20SDavid S. Miller {
967db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
968db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
9691cedee13SAndrey Ignatov 	DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
970e474995fSDavid S. Miller 	struct flowi4 fl4_stack;
971b6f21b26SDavid S. Miller 	struct flowi4 *fl4;
972db8dac20SDavid S. Miller 	int ulen = len;
973db8dac20SDavid S. Miller 	struct ipcm_cookie ipc;
974db8dac20SDavid S. Miller 	struct rtable *rt = NULL;
975db8dac20SDavid S. Miller 	int free = 0;
976db8dac20SDavid S. Miller 	int connected = 0;
977db8dac20SDavid S. Miller 	__be32 daddr, faddr, saddr;
978db8dac20SDavid S. Miller 	__be16 dport;
979db8dac20SDavid S. Miller 	u8  tos;
980db8dac20SDavid S. Miller 	int err, is_udplite = IS_UDPLITE(sk);
981db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
982db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
983903ab86dSHerbert Xu 	struct sk_buff *skb;
984f6d8bd05SEric Dumazet 	struct ip_options_data opt_copy;
985db8dac20SDavid S. Miller 
986db8dac20SDavid S. Miller 	if (len > 0xFFFF)
987db8dac20SDavid S. Miller 		return -EMSGSIZE;
988db8dac20SDavid S. Miller 
989db8dac20SDavid S. Miller 	/*
990db8dac20SDavid S. Miller 	 *	Check the flags.
991db8dac20SDavid S. Miller 	 */
992db8dac20SDavid S. Miller 
993db8dac20SDavid S. Miller 	if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
994db8dac20SDavid S. Miller 		return -EOPNOTSUPP;
995db8dac20SDavid S. Miller 
996903ab86dSHerbert Xu 	getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
997903ab86dSHerbert Xu 
998f5fca608SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
999db8dac20SDavid S. Miller 	if (up->pending) {
1000db8dac20SDavid S. Miller 		/*
1001db8dac20SDavid S. Miller 		 * There are pending frames.
1002db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1003db8dac20SDavid S. Miller 		 */
1004db8dac20SDavid S. Miller 		lock_sock(sk);
1005db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1006db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET)) {
1007db8dac20SDavid S. Miller 				release_sock(sk);
1008db8dac20SDavid S. Miller 				return -EINVAL;
1009db8dac20SDavid S. Miller 			}
1010db8dac20SDavid S. Miller 			goto do_append_data;
1011db8dac20SDavid S. Miller 		}
1012db8dac20SDavid S. Miller 		release_sock(sk);
1013db8dac20SDavid S. Miller 	}
1014db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1015db8dac20SDavid S. Miller 
1016db8dac20SDavid S. Miller 	/*
1017db8dac20SDavid S. Miller 	 *	Get and verify the address.
1018db8dac20SDavid S. Miller 	 */
10191cedee13SAndrey Ignatov 	if (usin) {
1020db8dac20SDavid S. Miller 		if (msg->msg_namelen < sizeof(*usin))
1021db8dac20SDavid S. Miller 			return -EINVAL;
1022db8dac20SDavid S. Miller 		if (usin->sin_family != AF_INET) {
1023db8dac20SDavid S. Miller 			if (usin->sin_family != AF_UNSPEC)
1024db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1025db8dac20SDavid S. Miller 		}
1026db8dac20SDavid S. Miller 
1027db8dac20SDavid S. Miller 		daddr = usin->sin_addr.s_addr;
1028db8dac20SDavid S. Miller 		dport = usin->sin_port;
1029db8dac20SDavid S. Miller 		if (dport == 0)
1030db8dac20SDavid S. Miller 			return -EINVAL;
1031db8dac20SDavid S. Miller 	} else {
1032db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1033db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1034c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
1035c720c7e8SEric Dumazet 		dport = inet->inet_dport;
1036db8dac20SDavid S. Miller 		/* Open fast path for connected socket.
1037db8dac20SDavid S. Miller 		   Route will not be used, if at least one option is set.
1038db8dac20SDavid S. Miller 		 */
1039db8dac20SDavid S. Miller 		connected = 1;
1040db8dac20SDavid S. Miller 	}
1041c14ac945SSoheil Hassas Yeganeh 
104235178206SWillem de Bruijn 	ipcm_init_sk(&ipc, inet);
1043bec1f6f6SWillem de Bruijn 	ipc.gso_size = up->gso_size;
1044bf84a010SDaniel Borkmann 
1045db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
10462e8de857SWillem de Bruijn 		err = udp_cmsg_send(sk, msg, &ipc.gso_size);
10472e8de857SWillem de Bruijn 		if (err > 0)
10482e8de857SWillem de Bruijn 			err = ip_cmsg_send(sk, msg, &ipc,
10492e8de857SWillem de Bruijn 					   sk->sk_family == AF_INET6);
10502e8de857SWillem de Bruijn 		if (unlikely(err < 0)) {
105191948309SEric Dumazet 			kfree(ipc.opt);
1052db8dac20SDavid S. Miller 			return err;
105391948309SEric Dumazet 		}
1054db8dac20SDavid S. Miller 		if (ipc.opt)
1055db8dac20SDavid S. Miller 			free = 1;
1056db8dac20SDavid S. Miller 		connected = 0;
1057db8dac20SDavid S. Miller 	}
1058f6d8bd05SEric Dumazet 	if (!ipc.opt) {
1059f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
1060f6d8bd05SEric Dumazet 
1061f6d8bd05SEric Dumazet 		rcu_read_lock();
1062f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference(inet->inet_opt);
1063f6d8bd05SEric Dumazet 		if (inet_opt) {
1064f6d8bd05SEric Dumazet 			memcpy(&opt_copy, inet_opt,
1065f6d8bd05SEric Dumazet 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
1066f6d8bd05SEric Dumazet 			ipc.opt = &opt_copy.opt;
1067f6d8bd05SEric Dumazet 		}
1068f6d8bd05SEric Dumazet 		rcu_read_unlock();
1069f6d8bd05SEric Dumazet 	}
1070db8dac20SDavid S. Miller 
10711cedee13SAndrey Ignatov 	if (cgroup_bpf_enabled && !connected) {
10721cedee13SAndrey Ignatov 		err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk,
10731cedee13SAndrey Ignatov 					    (struct sockaddr *)usin, &ipc.addr);
10741cedee13SAndrey Ignatov 		if (err)
10751cedee13SAndrey Ignatov 			goto out_free;
10761cedee13SAndrey Ignatov 		if (usin) {
10771cedee13SAndrey Ignatov 			if (usin->sin_port == 0) {
10781cedee13SAndrey Ignatov 				/* BPF program set invalid port. Reject it. */
10791cedee13SAndrey Ignatov 				err = -EINVAL;
10801cedee13SAndrey Ignatov 				goto out_free;
10811cedee13SAndrey Ignatov 			}
10821cedee13SAndrey Ignatov 			daddr = usin->sin_addr.s_addr;
10831cedee13SAndrey Ignatov 			dport = usin->sin_port;
10841cedee13SAndrey Ignatov 		}
10851cedee13SAndrey Ignatov 	}
10861cedee13SAndrey Ignatov 
1087db8dac20SDavid S. Miller 	saddr = ipc.addr;
1088db8dac20SDavid S. Miller 	ipc.addr = faddr = daddr;
1089db8dac20SDavid S. Miller 
1090f6d8bd05SEric Dumazet 	if (ipc.opt && ipc.opt->opt.srr) {
10911b97013bSAndrey Ignatov 		if (!daddr) {
10921b97013bSAndrey Ignatov 			err = -EINVAL;
10931b97013bSAndrey Ignatov 			goto out_free;
10941b97013bSAndrey Ignatov 		}
1095f6d8bd05SEric Dumazet 		faddr = ipc.opt->opt.faddr;
1096db8dac20SDavid S. Miller 		connected = 0;
1097db8dac20SDavid S. Miller 	}
1098aa661581SFrancesco Fusco 	tos = get_rttos(&ipc, inet);
1099db8dac20SDavid S. Miller 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
1100db8dac20SDavid S. Miller 	    (msg->msg_flags & MSG_DONTROUTE) ||
1101f6d8bd05SEric Dumazet 	    (ipc.opt && ipc.opt->opt.is_strictroute)) {
1102db8dac20SDavid S. Miller 		tos |= RTO_ONLINK;
1103db8dac20SDavid S. Miller 		connected = 0;
1104db8dac20SDavid S. Miller 	}
1105db8dac20SDavid S. Miller 
1106db8dac20SDavid S. Miller 	if (ipv4_is_multicast(daddr)) {
1107854da991SRobert Shearman 		if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
1108db8dac20SDavid S. Miller 			ipc.oif = inet->mc_index;
1109db8dac20SDavid S. Miller 		if (!saddr)
1110db8dac20SDavid S. Miller 			saddr = inet->mc_addr;
1111db8dac20SDavid S. Miller 		connected = 0;
11129515a2e0SDavid Ahern 	} else if (!ipc.oif) {
111376e21053SErich E. Hoover 		ipc.oif = inet->uc_index;
11149515a2e0SDavid Ahern 	} else if (ipv4_is_lbcast(daddr) && inet->uc_index) {
11159515a2e0SDavid Ahern 		/* oif is set, packet is to local broadcast and
11169515a2e0SDavid Ahern 		 * and uc_index is set. oif is most likely set
11179515a2e0SDavid Ahern 		 * by sk_bound_dev_if. If uc_index != oif check if the
11189515a2e0SDavid Ahern 		 * oif is an L3 master and uc_index is an L3 slave.
11199515a2e0SDavid Ahern 		 * If so, we want to allow the send using the uc_index.
11209515a2e0SDavid Ahern 		 */
11219515a2e0SDavid Ahern 		if (ipc.oif != inet->uc_index &&
11229515a2e0SDavid Ahern 		    ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk),
11239515a2e0SDavid Ahern 							      inet->uc_index)) {
11249515a2e0SDavid Ahern 			ipc.oif = inet->uc_index;
11259515a2e0SDavid Ahern 		}
11269515a2e0SDavid Ahern 	}
1127db8dac20SDavid S. Miller 
1128db8dac20SDavid S. Miller 	if (connected)
1129db8dac20SDavid S. Miller 		rt = (struct rtable *)sk_dst_check(sk, 0);
1130db8dac20SDavid S. Miller 
113151456b29SIan Morris 	if (!rt) {
113284a3aa00SPavel Emelyanov 		struct net *net = sock_net(sk);
11339a24abfaSDavid Ahern 		__u8 flow_flags = inet_sk_flowi_flags(sk);
113484a3aa00SPavel Emelyanov 
1135e474995fSDavid S. Miller 		fl4 = &fl4_stack;
11369a24abfaSDavid Ahern 
1137c6af0c22SWillem de Bruijn 		flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos,
11389a24abfaSDavid Ahern 				   RT_SCOPE_UNIVERSE, sk->sk_protocol,
11399a24abfaSDavid Ahern 				   flow_flags,
1140e2d118a1SLorenzo Colitti 				   faddr, saddr, dport, inet->inet_sport,
1141e2d118a1SLorenzo Colitti 				   sk->sk_uid);
1142c0951cbcSDavid S. Miller 
1143e474995fSDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
1144e474995fSDavid S. Miller 		rt = ip_route_output_flow(net, fl4, sk);
1145b23dd4feSDavid S. Miller 		if (IS_ERR(rt)) {
1146b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
114706dc94b1SDavid S. Miller 			rt = NULL;
1148db8dac20SDavid S. Miller 			if (err == -ENETUNREACH)
1149f1d8cba6SEric Dumazet 				IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
1150db8dac20SDavid S. Miller 			goto out;
1151db8dac20SDavid S. Miller 		}
1152db8dac20SDavid S. Miller 
1153db8dac20SDavid S. Miller 		err = -EACCES;
1154db8dac20SDavid S. Miller 		if ((rt->rt_flags & RTCF_BROADCAST) &&
1155db8dac20SDavid S. Miller 		    !sock_flag(sk, SOCK_BROADCAST))
1156db8dac20SDavid S. Miller 			goto out;
1157db8dac20SDavid S. Miller 		if (connected)
1158d8d1f30bSChangli Gao 			sk_dst_set(sk, dst_clone(&rt->dst));
1159db8dac20SDavid S. Miller 	}
1160db8dac20SDavid S. Miller 
1161db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1162db8dac20SDavid S. Miller 		goto do_confirm;
1163db8dac20SDavid S. Miller back_from_confirm:
1164db8dac20SDavid S. Miller 
1165e474995fSDavid S. Miller 	saddr = fl4->saddr;
1166db8dac20SDavid S. Miller 	if (!ipc.addr)
1167e474995fSDavid S. Miller 		daddr = ipc.addr = fl4->daddr;
1168db8dac20SDavid S. Miller 
1169903ab86dSHerbert Xu 	/* Lockless fast path for the non-corking case. */
1170903ab86dSHerbert Xu 	if (!corkreq) {
11711cd7884dSWillem de Bruijn 		struct inet_cork cork;
11721cd7884dSWillem de Bruijn 
1173f69e6d13SAl Viro 		skb = ip_make_skb(sk, fl4, getfrag, msg, ulen,
1174903ab86dSHerbert Xu 				  sizeof(struct udphdr), &ipc, &rt,
11751cd7884dSWillem de Bruijn 				  &cork, msg->msg_flags);
1176903ab86dSHerbert Xu 		err = PTR_ERR(skb);
117750c3a487SYOSHIFUJI Hideaki / 吉藤英明 		if (!IS_ERR_OR_NULL(skb))
1178bec1f6f6SWillem de Bruijn 			err = udp_send_skb(skb, fl4, &cork);
1179903ab86dSHerbert Xu 		goto out;
1180903ab86dSHerbert Xu 	}
1181903ab86dSHerbert Xu 
1182db8dac20SDavid S. Miller 	lock_sock(sk);
1183db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1184db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1185db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1186db8dac20SDavid S. Miller 		release_sock(sk);
1187db8dac20SDavid S. Miller 
1188197df02cSMatteo Croce 		net_dbg_ratelimited("socket already corked\n");
1189db8dac20SDavid S. Miller 		err = -EINVAL;
1190db8dac20SDavid S. Miller 		goto out;
1191db8dac20SDavid S. Miller 	}
1192db8dac20SDavid S. Miller 	/*
1193db8dac20SDavid S. Miller 	 *	Now cork the socket to pend data.
1194db8dac20SDavid S. Miller 	 */
1195b6f21b26SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
1196b6f21b26SDavid S. Miller 	fl4->daddr = daddr;
1197b6f21b26SDavid S. Miller 	fl4->saddr = saddr;
11989cce96dfSDavid S. Miller 	fl4->fl4_dport = dport;
11999cce96dfSDavid S. Miller 	fl4->fl4_sport = inet->inet_sport;
1200db8dac20SDavid S. Miller 	up->pending = AF_INET;
1201db8dac20SDavid S. Miller 
1202db8dac20SDavid S. Miller do_append_data:
1203db8dac20SDavid S. Miller 	up->len += ulen;
1204f69e6d13SAl Viro 	err = ip_append_data(sk, fl4, getfrag, msg, ulen,
12052e77d89bSEric Dumazet 			     sizeof(struct udphdr), &ipc, &rt,
1206db8dac20SDavid S. Miller 			     corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1207db8dac20SDavid S. Miller 	if (err)
1208db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1209db8dac20SDavid S. Miller 	else if (!corkreq)
1210db8dac20SDavid S. Miller 		err = udp_push_pending_frames(sk);
1211db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1212db8dac20SDavid S. Miller 		up->pending = 0;
1213db8dac20SDavid S. Miller 	release_sock(sk);
1214db8dac20SDavid S. Miller 
1215db8dac20SDavid S. Miller out:
1216db8dac20SDavid S. Miller 	ip_rt_put(rt);
12171b97013bSAndrey Ignatov out_free:
1218db8dac20SDavid S. Miller 	if (free)
1219db8dac20SDavid S. Miller 		kfree(ipc.opt);
1220db8dac20SDavid S. Miller 	if (!err)
1221db8dac20SDavid S. Miller 		return len;
1222db8dac20SDavid S. Miller 	/*
1223db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1224db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1225db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1226db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1227db8dac20SDavid S. Miller 	 * seems like overkill.
1228db8dac20SDavid S. Miller 	 */
1229db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
12306aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk),
1231629ca23cSPavel Emelyanov 			      UDP_MIB_SNDBUFERRORS, is_udplite);
1232db8dac20SDavid S. Miller 	}
1233db8dac20SDavid S. Miller 	return err;
1234db8dac20SDavid S. Miller 
1235db8dac20SDavid S. Miller do_confirm:
12360dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
12370dec879fSJulian Anastasov 		dst_confirm_neigh(&rt->dst, &fl4->daddr);
1238db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1239db8dac20SDavid S. Miller 		goto back_from_confirm;
1240db8dac20SDavid S. Miller 	err = 0;
1241db8dac20SDavid S. Miller 	goto out;
1242db8dac20SDavid S. Miller }
1243c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg);
1244db8dac20SDavid S. Miller 
1245db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset,
1246db8dac20SDavid S. Miller 		 size_t size, int flags)
1247db8dac20SDavid S. Miller {
1248f5fca608SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1249db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1250db8dac20SDavid S. Miller 	int ret;
1251db8dac20SDavid S. Miller 
1252d3f7d56aSShawn Landden 	if (flags & MSG_SENDPAGE_NOTLAST)
1253d3f7d56aSShawn Landden 		flags |= MSG_MORE;
1254d3f7d56aSShawn Landden 
1255db8dac20SDavid S. Miller 	if (!up->pending) {
1256db8dac20SDavid S. Miller 		struct msghdr msg = {	.msg_flags = flags|MSG_MORE };
1257db8dac20SDavid S. Miller 
1258db8dac20SDavid S. Miller 		/* Call udp_sendmsg to specify destination address which
1259db8dac20SDavid S. Miller 		 * sendpage interface can't pass.
1260db8dac20SDavid S. Miller 		 * This will succeed only when the socket is connected.
1261db8dac20SDavid S. Miller 		 */
12621b784140SYing Xue 		ret = udp_sendmsg(sk, &msg, 0);
1263db8dac20SDavid S. Miller 		if (ret < 0)
1264db8dac20SDavid S. Miller 			return ret;
1265db8dac20SDavid S. Miller 	}
1266db8dac20SDavid S. Miller 
1267db8dac20SDavid S. Miller 	lock_sock(sk);
1268db8dac20SDavid S. Miller 
1269db8dac20SDavid S. Miller 	if (unlikely(!up->pending)) {
1270db8dac20SDavid S. Miller 		release_sock(sk);
1271db8dac20SDavid S. Miller 
1272197df02cSMatteo Croce 		net_dbg_ratelimited("cork failed\n");
1273db8dac20SDavid S. Miller 		return -EINVAL;
1274db8dac20SDavid S. Miller 	}
1275db8dac20SDavid S. Miller 
1276f5fca608SDavid S. Miller 	ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
1277f5fca608SDavid S. Miller 			     page, offset, size, flags);
1278db8dac20SDavid S. Miller 	if (ret == -EOPNOTSUPP) {
1279db8dac20SDavid S. Miller 		release_sock(sk);
1280db8dac20SDavid S. Miller 		return sock_no_sendpage(sk->sk_socket, page, offset,
1281db8dac20SDavid S. Miller 					size, flags);
1282db8dac20SDavid S. Miller 	}
1283db8dac20SDavid S. Miller 	if (ret < 0) {
1284db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1285db8dac20SDavid S. Miller 		goto out;
1286db8dac20SDavid S. Miller 	}
1287db8dac20SDavid S. Miller 
1288db8dac20SDavid S. Miller 	up->len += size;
1289db8dac20SDavid S. Miller 	if (!(up->corkflag || (flags&MSG_MORE)))
1290db8dac20SDavid S. Miller 		ret = udp_push_pending_frames(sk);
1291db8dac20SDavid S. Miller 	if (!ret)
1292db8dac20SDavid S. Miller 		ret = size;
1293db8dac20SDavid S. Miller out:
1294db8dac20SDavid S. Miller 	release_sock(sk);
1295db8dac20SDavid S. Miller 	return ret;
1296db8dac20SDavid S. Miller }
1297db8dac20SDavid S. Miller 
1298dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000
1299dce4551cSPaolo Abeni 
1300677bf08cSFlorian Westphal /* all head states (dst, sk, nf conntrack) except skb extensions are
1301677bf08cSFlorian Westphal  * cleared by udp_rcv().
1302677bf08cSFlorian Westphal  *
1303677bf08cSFlorian Westphal  * We need to preserve secpath, if present, to eventually process
1304677bf08cSFlorian Westphal  * IP_CMSG_PASSSEC at recvmsg() time.
1305677bf08cSFlorian Westphal  *
1306677bf08cSFlorian Westphal  * Other extensions can be cleared.
1307677bf08cSFlorian Westphal  */
1308677bf08cSFlorian Westphal static bool udp_try_make_stateless(struct sk_buff *skb)
1309677bf08cSFlorian Westphal {
1310677bf08cSFlorian Westphal 	if (!skb_has_extensions(skb))
1311677bf08cSFlorian Westphal 		return true;
1312677bf08cSFlorian Westphal 
1313677bf08cSFlorian Westphal 	if (!secpath_exists(skb)) {
1314677bf08cSFlorian Westphal 		skb_ext_reset(skb);
1315677bf08cSFlorian Westphal 		return true;
1316677bf08cSFlorian Westphal 	}
1317677bf08cSFlorian Westphal 
1318677bf08cSFlorian Westphal 	return false;
1319677bf08cSFlorian Westphal }
1320677bf08cSFlorian Westphal 
1321b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb)
1322b65ac446SPaolo Abeni {
1323dce4551cSPaolo Abeni 	struct udp_dev_scratch *scratch = udp_skb_scratch(skb);
1324b65ac446SPaolo Abeni 
1325b65ac446SPaolo Abeni 	BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long));
1326dce4551cSPaolo Abeni 	scratch->_tsize_state = skb->truesize;
1327dce4551cSPaolo Abeni #if BITS_PER_LONG == 64
1328b65ac446SPaolo Abeni 	scratch->len = skb->len;
1329b65ac446SPaolo Abeni 	scratch->csum_unnecessary = !!skb_csum_unnecessary(skb);
1330b65ac446SPaolo Abeni 	scratch->is_linear = !skb_is_nonlinear(skb);
1331b65ac446SPaolo Abeni #endif
1332677bf08cSFlorian Westphal 	if (udp_try_make_stateless(skb))
1333dce4551cSPaolo Abeni 		scratch->_tsize_state |= UDP_SKB_IS_STATELESS;
1334dce4551cSPaolo Abeni }
1335dce4551cSPaolo Abeni 
1336a793183cSEric Dumazet static void udp_skb_csum_unnecessary_set(struct sk_buff *skb)
1337a793183cSEric Dumazet {
1338a793183cSEric Dumazet 	/* We come here after udp_lib_checksum_complete() returned 0.
1339a793183cSEric Dumazet 	 * This means that __skb_checksum_complete() might have
1340a793183cSEric Dumazet 	 * set skb->csum_valid to 1.
1341a793183cSEric Dumazet 	 * On 64bit platforms, we can set csum_unnecessary
1342a793183cSEric Dumazet 	 * to true, but only if the skb is not shared.
1343a793183cSEric Dumazet 	 */
1344a793183cSEric Dumazet #if BITS_PER_LONG == 64
1345a793183cSEric Dumazet 	if (!skb_shared(skb))
1346a793183cSEric Dumazet 		udp_skb_scratch(skb)->csum_unnecessary = true;
1347a793183cSEric Dumazet #endif
1348a793183cSEric Dumazet }
1349a793183cSEric Dumazet 
1350dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb)
1351dce4551cSPaolo Abeni {
1352dce4551cSPaolo Abeni 	return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS;
1353dce4551cSPaolo Abeni }
1354dce4551cSPaolo Abeni 
1355dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb)
1356dce4551cSPaolo Abeni {
1357dce4551cSPaolo Abeni 	return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS);
1358dce4551cSPaolo Abeni }
1359b65ac446SPaolo Abeni 
13607c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */
13616dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial,
13626dfb4367SPaolo Abeni 			     bool rx_queue_lock_held)
1363f970bd9eSPaolo Abeni {
13646b229cf7SEric Dumazet 	struct udp_sock *up = udp_sk(sk);
13652276f58aSPaolo Abeni 	struct sk_buff_head *sk_queue;
1366f970bd9eSPaolo Abeni 	int amt;
1367f970bd9eSPaolo Abeni 
13686b229cf7SEric Dumazet 	if (likely(partial)) {
13696b229cf7SEric Dumazet 		up->forward_deficit += size;
13706b229cf7SEric Dumazet 		size = up->forward_deficit;
13710d4a6608SPaolo Abeni 		if (size < (sk->sk_rcvbuf >> 2))
13726b229cf7SEric Dumazet 			return;
13736b229cf7SEric Dumazet 	} else {
13746b229cf7SEric Dumazet 		size += up->forward_deficit;
13756b229cf7SEric Dumazet 	}
13766b229cf7SEric Dumazet 	up->forward_deficit = 0;
13776b229cf7SEric Dumazet 
13786dfb4367SPaolo Abeni 	/* acquire the sk_receive_queue for fwd allocated memory scheduling,
13796dfb4367SPaolo Abeni 	 * if the called don't held it already
13806dfb4367SPaolo Abeni 	 */
13812276f58aSPaolo Abeni 	sk_queue = &sk->sk_receive_queue;
13826dfb4367SPaolo Abeni 	if (!rx_queue_lock_held)
13832276f58aSPaolo Abeni 		spin_lock(&sk_queue->lock);
13842276f58aSPaolo Abeni 
13856dfb4367SPaolo Abeni 
1386f970bd9eSPaolo Abeni 	sk->sk_forward_alloc += size;
1387f970bd9eSPaolo Abeni 	amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1);
1388f970bd9eSPaolo Abeni 	sk->sk_forward_alloc -= amt;
1389f970bd9eSPaolo Abeni 
1390f970bd9eSPaolo Abeni 	if (amt)
1391f970bd9eSPaolo Abeni 		__sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT);
139202ab0d13SEric Dumazet 
139302ab0d13SEric Dumazet 	atomic_sub(size, &sk->sk_rmem_alloc);
13942276f58aSPaolo Abeni 
13952276f58aSPaolo Abeni 	/* this can save us from acquiring the rx queue lock on next receive */
13962276f58aSPaolo Abeni 	skb_queue_splice_tail_init(sk_queue, &up->reader_queue);
13972276f58aSPaolo Abeni 
13986dfb4367SPaolo Abeni 	if (!rx_queue_lock_held)
13992276f58aSPaolo Abeni 		spin_unlock(&sk_queue->lock);
1400f970bd9eSPaolo Abeni }
1401f970bd9eSPaolo Abeni 
14022276f58aSPaolo Abeni /* Note: called with reader_queue.lock held.
1403c84d9490SEric Dumazet  * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch
1404c84d9490SEric Dumazet  * This avoids a cache line miss while receive_queue lock is held.
1405c84d9490SEric Dumazet  * Look at __udp_enqueue_schedule_skb() to find where this copy is done.
1406c84d9490SEric Dumazet  */
14077c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb)
1408f970bd9eSPaolo Abeni {
1409b65ac446SPaolo Abeni 	prefetch(&skb->data);
1410b65ac446SPaolo Abeni 	udp_rmem_release(sk, udp_skb_truesize(skb), 1, false);
1411f970bd9eSPaolo Abeni }
14127c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor);
1413f970bd9eSPaolo Abeni 
14146dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */
141564f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb)
14166dfb4367SPaolo Abeni {
1417b65ac446SPaolo Abeni 	prefetch(&skb->data);
1418b65ac446SPaolo Abeni 	udp_rmem_release(sk, udp_skb_truesize(skb), 1, true);
14196dfb4367SPaolo Abeni }
14206dfb4367SPaolo Abeni 
14214b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock
14224b272750SEric Dumazet  * to relieve pressure on the receive_queue spinlock shared by consumer.
14234b272750SEric Dumazet  * Under flood, this means that only one producer can be in line
14244b272750SEric Dumazet  * trying to acquire the receive_queue spinlock.
14254b272750SEric Dumazet  * These busylock can be allocated on a per cpu manner, instead of a
14264b272750SEric Dumazet  * per socket one (that would consume a cache line per socket)
14274b272750SEric Dumazet  */
14284b272750SEric Dumazet static int udp_busylocks_log __read_mostly;
14294b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly;
14304b272750SEric Dumazet 
14314b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr)
14324b272750SEric Dumazet {
14334b272750SEric Dumazet 	spinlock_t *busy;
14344b272750SEric Dumazet 
14354b272750SEric Dumazet 	busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log);
14364b272750SEric Dumazet 	spin_lock(busy);
14374b272750SEric Dumazet 	return busy;
14384b272750SEric Dumazet }
14394b272750SEric Dumazet 
14404b272750SEric Dumazet static void busylock_release(spinlock_t *busy)
14414b272750SEric Dumazet {
14424b272750SEric Dumazet 	if (busy)
14434b272750SEric Dumazet 		spin_unlock(busy);
14444b272750SEric Dumazet }
14454b272750SEric Dumazet 
1446f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
1447f970bd9eSPaolo Abeni {
1448f970bd9eSPaolo Abeni 	struct sk_buff_head *list = &sk->sk_receive_queue;
1449f970bd9eSPaolo Abeni 	int rmem, delta, amt, err = -ENOMEM;
14504b272750SEric Dumazet 	spinlock_t *busy = NULL;
1451c8c8b127SEric Dumazet 	int size;
1452f970bd9eSPaolo Abeni 
1453f970bd9eSPaolo Abeni 	/* try to avoid the costly atomic add/sub pair when the receive
1454f970bd9eSPaolo Abeni 	 * queue is full; always allow at least a packet
1455f970bd9eSPaolo Abeni 	 */
1456f970bd9eSPaolo Abeni 	rmem = atomic_read(&sk->sk_rmem_alloc);
1457363dc73aSPaolo Abeni 	if (rmem > sk->sk_rcvbuf)
1458f970bd9eSPaolo Abeni 		goto drop;
1459f970bd9eSPaolo Abeni 
1460c8c8b127SEric Dumazet 	/* Under mem pressure, it might be helpful to help udp_recvmsg()
1461c8c8b127SEric Dumazet 	 * having linear skbs :
1462c8c8b127SEric Dumazet 	 * - Reduce memory overhead and thus increase receive queue capacity
1463c8c8b127SEric Dumazet 	 * - Less cache line misses at copyout() time
1464c8c8b127SEric Dumazet 	 * - Less work at consume_skb() (less alien page frag freeing)
1465c8c8b127SEric Dumazet 	 */
14664b272750SEric Dumazet 	if (rmem > (sk->sk_rcvbuf >> 1)) {
1467c8c8b127SEric Dumazet 		skb_condense(skb);
14684b272750SEric Dumazet 
14694b272750SEric Dumazet 		busy = busylock_acquire(sk);
14704b272750SEric Dumazet 	}
1471c8c8b127SEric Dumazet 	size = skb->truesize;
1472b65ac446SPaolo Abeni 	udp_set_dev_scratch(skb);
1473c8c8b127SEric Dumazet 
1474f970bd9eSPaolo Abeni 	/* we drop only if the receive buf is full and the receive
1475f970bd9eSPaolo Abeni 	 * queue contains some other skb
1476f970bd9eSPaolo Abeni 	 */
1477f970bd9eSPaolo Abeni 	rmem = atomic_add_return(size, &sk->sk_rmem_alloc);
1478feed8a4fSAntonio Messina 	if (rmem > (size + (unsigned int)sk->sk_rcvbuf))
1479f970bd9eSPaolo Abeni 		goto uncharge_drop;
1480f970bd9eSPaolo Abeni 
1481f970bd9eSPaolo Abeni 	spin_lock(&list->lock);
1482f970bd9eSPaolo Abeni 	if (size >= sk->sk_forward_alloc) {
1483f970bd9eSPaolo Abeni 		amt = sk_mem_pages(size);
1484f970bd9eSPaolo Abeni 		delta = amt << SK_MEM_QUANTUM_SHIFT;
1485f970bd9eSPaolo Abeni 		if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) {
1486f970bd9eSPaolo Abeni 			err = -ENOBUFS;
1487f970bd9eSPaolo Abeni 			spin_unlock(&list->lock);
1488f970bd9eSPaolo Abeni 			goto uncharge_drop;
1489f970bd9eSPaolo Abeni 		}
1490f970bd9eSPaolo Abeni 
1491f970bd9eSPaolo Abeni 		sk->sk_forward_alloc += delta;
1492f970bd9eSPaolo Abeni 	}
1493f970bd9eSPaolo Abeni 
1494f970bd9eSPaolo Abeni 	sk->sk_forward_alloc -= size;
1495f970bd9eSPaolo Abeni 
14967c13f97fSPaolo Abeni 	/* no need to setup a destructor, we will explicitly release the
14977c13f97fSPaolo Abeni 	 * forward allocated memory on dequeue
14987c13f97fSPaolo Abeni 	 */
1499f970bd9eSPaolo Abeni 	sock_skb_set_dropcount(sk, skb);
1500f970bd9eSPaolo Abeni 
1501f970bd9eSPaolo Abeni 	__skb_queue_tail(list, skb);
1502f970bd9eSPaolo Abeni 	spin_unlock(&list->lock);
1503f970bd9eSPaolo Abeni 
1504f970bd9eSPaolo Abeni 	if (!sock_flag(sk, SOCK_DEAD))
1505f970bd9eSPaolo Abeni 		sk->sk_data_ready(sk);
1506f970bd9eSPaolo Abeni 
15074b272750SEric Dumazet 	busylock_release(busy);
1508f970bd9eSPaolo Abeni 	return 0;
1509f970bd9eSPaolo Abeni 
1510f970bd9eSPaolo Abeni uncharge_drop:
1511f970bd9eSPaolo Abeni 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
1512f970bd9eSPaolo Abeni 
1513f970bd9eSPaolo Abeni drop:
1514f970bd9eSPaolo Abeni 	atomic_inc(&sk->sk_drops);
15154b272750SEric Dumazet 	busylock_release(busy);
1516f970bd9eSPaolo Abeni 	return err;
1517f970bd9eSPaolo Abeni }
1518f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb);
1519f970bd9eSPaolo Abeni 
1520c915fe13SPaolo Abeni void udp_destruct_sock(struct sock *sk)
1521f970bd9eSPaolo Abeni {
1522f970bd9eSPaolo Abeni 	/* reclaim completely the forward allocated memory */
15232276f58aSPaolo Abeni 	struct udp_sock *up = udp_sk(sk);
15247c13f97fSPaolo Abeni 	unsigned int total = 0;
15257c13f97fSPaolo Abeni 	struct sk_buff *skb;
15267c13f97fSPaolo Abeni 
15272276f58aSPaolo Abeni 	skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue);
15282276f58aSPaolo Abeni 	while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) {
15297c13f97fSPaolo Abeni 		total += skb->truesize;
15307c13f97fSPaolo Abeni 		kfree_skb(skb);
15317c13f97fSPaolo Abeni 	}
15326dfb4367SPaolo Abeni 	udp_rmem_release(sk, total, 0, true);
15337c13f97fSPaolo Abeni 
1534f970bd9eSPaolo Abeni 	inet_sock_destruct(sk);
1535f970bd9eSPaolo Abeni }
1536c915fe13SPaolo Abeni EXPORT_SYMBOL_GPL(udp_destruct_sock);
1537f970bd9eSPaolo Abeni 
1538f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk)
1539f970bd9eSPaolo Abeni {
15402276f58aSPaolo Abeni 	skb_queue_head_init(&udp_sk(sk)->reader_queue);
1541f970bd9eSPaolo Abeni 	sk->sk_destruct = udp_destruct_sock;
1542f970bd9eSPaolo Abeni 	return 0;
1543f970bd9eSPaolo Abeni }
1544f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(udp_init_sock);
1545f970bd9eSPaolo Abeni 
1546f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len)
1547f970bd9eSPaolo Abeni {
1548f970bd9eSPaolo Abeni 	if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) {
1549f970bd9eSPaolo Abeni 		bool slow = lock_sock_fast(sk);
1550f970bd9eSPaolo Abeni 
1551f970bd9eSPaolo Abeni 		sk_peek_offset_bwd(sk, len);
1552f970bd9eSPaolo Abeni 		unlock_sock_fast(sk, slow);
1553f970bd9eSPaolo Abeni 	}
15540a463c78SPaolo Abeni 
1555ca2c1418SPaolo Abeni 	if (!skb_unref(skb))
1556ca2c1418SPaolo Abeni 		return;
1557ca2c1418SPaolo Abeni 
1558dce4551cSPaolo Abeni 	/* In the more common cases we cleared the head states previously,
1559dce4551cSPaolo Abeni 	 * see __udp_queue_rcv_skb().
15600ddf3fb2SPaolo Abeni 	 */
1561dce4551cSPaolo Abeni 	if (unlikely(udp_skb_has_head_state(skb)))
15620ddf3fb2SPaolo Abeni 		skb_release_head_state(skb);
1563ca2c1418SPaolo Abeni 	__consume_stateless_skb(skb);
1564f970bd9eSPaolo Abeni }
1565f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp);
1566f970bd9eSPaolo Abeni 
15672276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk,
15682276f58aSPaolo Abeni 					     struct sk_buff_head *rcvq,
15692276f58aSPaolo Abeni 					     int *total)
15702276f58aSPaolo Abeni {
15712276f58aSPaolo Abeni 	struct sk_buff *skb;
15722276f58aSPaolo Abeni 
15739bd780f5SPaolo Abeni 	while ((skb = skb_peek(rcvq)) != NULL) {
15749bd780f5SPaolo Abeni 		if (udp_lib_checksum_complete(skb)) {
15752276f58aSPaolo Abeni 			__UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
15762276f58aSPaolo Abeni 					IS_UDPLITE(sk));
15772276f58aSPaolo Abeni 			__UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
15782276f58aSPaolo Abeni 					IS_UDPLITE(sk));
15792276f58aSPaolo Abeni 			atomic_inc(&sk->sk_drops);
15802276f58aSPaolo Abeni 			__skb_unlink(skb, rcvq);
15812276f58aSPaolo Abeni 			*total += skb->truesize;
15822276f58aSPaolo Abeni 			kfree_skb(skb);
15839bd780f5SPaolo Abeni 		} else {
1584a793183cSEric Dumazet 			udp_skb_csum_unnecessary_set(skb);
15859bd780f5SPaolo Abeni 			break;
15869bd780f5SPaolo Abeni 		}
15872276f58aSPaolo Abeni 	}
15882276f58aSPaolo Abeni 	return skb;
15892276f58aSPaolo Abeni }
15902276f58aSPaolo Abeni 
159185584672SEric Dumazet /**
159285584672SEric Dumazet  *	first_packet_length	- return length of first packet in receive queue
159385584672SEric Dumazet  *	@sk: socket
159485584672SEric Dumazet  *
159585584672SEric Dumazet  *	Drops all bad checksum frames, until a valid one is found.
1596e83c6744SEric Dumazet  *	Returns the length of found skb, or -1 if none is found.
159785584672SEric Dumazet  */
1598e83c6744SEric Dumazet static int first_packet_length(struct sock *sk)
159985584672SEric Dumazet {
16002276f58aSPaolo Abeni 	struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue;
16012276f58aSPaolo Abeni 	struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
160285584672SEric Dumazet 	struct sk_buff *skb;
16037c13f97fSPaolo Abeni 	int total = 0;
1604e83c6744SEric Dumazet 	int res;
160585584672SEric Dumazet 
160685584672SEric Dumazet 	spin_lock_bh(&rcvq->lock);
16072276f58aSPaolo Abeni 	skb = __first_packet_length(sk, rcvq, &total);
1608137a0dbeSEric Dumazet 	if (!skb && !skb_queue_empty_lockless(sk_queue)) {
16092276f58aSPaolo Abeni 		spin_lock(&sk_queue->lock);
16102276f58aSPaolo Abeni 		skb_queue_splice_tail_init(sk_queue, rcvq);
16112276f58aSPaolo Abeni 		spin_unlock(&sk_queue->lock);
16122276f58aSPaolo Abeni 
16132276f58aSPaolo Abeni 		skb = __first_packet_length(sk, rcvq, &total);
161485584672SEric Dumazet 	}
1615e83c6744SEric Dumazet 	res = skb ? skb->len : -1;
16167c13f97fSPaolo Abeni 	if (total)
16176dfb4367SPaolo Abeni 		udp_rmem_release(sk, total, 1, false);
161885584672SEric Dumazet 	spin_unlock_bh(&rcvq->lock);
161985584672SEric Dumazet 	return res;
162085584672SEric Dumazet }
162185584672SEric Dumazet 
16221da177e4SLinus Torvalds /*
16231da177e4SLinus Torvalds  *	IOCTL requests applicable to the UDP protocol
16241da177e4SLinus Torvalds  */
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
16271da177e4SLinus Torvalds {
16286516c655SStephen Hemminger 	switch (cmd) {
16291da177e4SLinus Torvalds 	case SIOCOUTQ:
16301da177e4SLinus Torvalds 	{
163131e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
163231e6d363SEric Dumazet 
16331da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
16341da177e4SLinus Torvalds 	}
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	case SIOCINQ:
16371da177e4SLinus Torvalds 	{
1638e83c6744SEric Dumazet 		int amount = max_t(int, 0, first_packet_length(sk));
16391da177e4SLinus Torvalds 
16401da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
16411da177e4SLinus Torvalds 	}
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	default:
16441da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
16451da177e4SLinus Torvalds 	}
16466516c655SStephen Hemminger 
16476516c655SStephen Hemminger 	return 0;
16481da177e4SLinus Torvalds }
1649c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl);
16501da177e4SLinus Torvalds 
16512276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags,
1652fd69c399SPaolo Abeni 			       int noblock, int *off, int *err)
16532276f58aSPaolo Abeni {
16542276f58aSPaolo Abeni 	struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
16552276f58aSPaolo Abeni 	struct sk_buff_head *queue;
16562276f58aSPaolo Abeni 	struct sk_buff *last;
16572276f58aSPaolo Abeni 	long timeo;
16582276f58aSPaolo Abeni 	int error;
16592276f58aSPaolo Abeni 
16602276f58aSPaolo Abeni 	queue = &udp_sk(sk)->reader_queue;
16612276f58aSPaolo Abeni 	flags |= noblock ? MSG_DONTWAIT : 0;
16622276f58aSPaolo Abeni 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
16632276f58aSPaolo Abeni 	do {
16642276f58aSPaolo Abeni 		struct sk_buff *skb;
16652276f58aSPaolo Abeni 
16662276f58aSPaolo Abeni 		error = sock_error(sk);
16672276f58aSPaolo Abeni 		if (error)
16682276f58aSPaolo Abeni 			break;
16692276f58aSPaolo Abeni 
16702276f58aSPaolo Abeni 		error = -EAGAIN;
16712276f58aSPaolo Abeni 		do {
16722276f58aSPaolo Abeni 			spin_lock_bh(&queue->lock);
16732276f58aSPaolo Abeni 			skb = __skb_try_recv_from_queue(sk, queue, flags,
16742276f58aSPaolo Abeni 							udp_skb_destructor,
1675fd69c399SPaolo Abeni 							off, err, &last);
16762276f58aSPaolo Abeni 			if (skb) {
16772276f58aSPaolo Abeni 				spin_unlock_bh(&queue->lock);
16782276f58aSPaolo Abeni 				return skb;
16792276f58aSPaolo Abeni 			}
16802276f58aSPaolo Abeni 
1681137a0dbeSEric Dumazet 			if (skb_queue_empty_lockless(sk_queue)) {
16822276f58aSPaolo Abeni 				spin_unlock_bh(&queue->lock);
16832276f58aSPaolo Abeni 				goto busy_check;
16842276f58aSPaolo Abeni 			}
16852276f58aSPaolo Abeni 
16866dfb4367SPaolo Abeni 			/* refill the reader queue and walk it again
16876dfb4367SPaolo Abeni 			 * keep both queues locked to avoid re-acquiring
16886dfb4367SPaolo Abeni 			 * the sk_receive_queue lock if fwd memory scheduling
16896dfb4367SPaolo Abeni 			 * is needed.
16906dfb4367SPaolo Abeni 			 */
16912276f58aSPaolo Abeni 			spin_lock(&sk_queue->lock);
16922276f58aSPaolo Abeni 			skb_queue_splice_tail_init(sk_queue, queue);
16932276f58aSPaolo Abeni 
16942276f58aSPaolo Abeni 			skb = __skb_try_recv_from_queue(sk, queue, flags,
16956dfb4367SPaolo Abeni 							udp_skb_dtor_locked,
1696fd69c399SPaolo Abeni 							off, err, &last);
16976dfb4367SPaolo Abeni 			spin_unlock(&sk_queue->lock);
16982276f58aSPaolo Abeni 			spin_unlock_bh(&queue->lock);
1699de321ed3SAndrey Vagin 			if (skb)
17002276f58aSPaolo Abeni 				return skb;
17012276f58aSPaolo Abeni 
17022276f58aSPaolo Abeni busy_check:
17032276f58aSPaolo Abeni 			if (!sk_can_busy_loop(sk))
17042276f58aSPaolo Abeni 				break;
17052276f58aSPaolo Abeni 
17062276f58aSPaolo Abeni 			sk_busy_loop(sk, flags & MSG_DONTWAIT);
1707137a0dbeSEric Dumazet 		} while (!skb_queue_empty_lockless(sk_queue));
17082276f58aSPaolo Abeni 
17092276f58aSPaolo Abeni 		/* sk_queue is empty, reader_queue may contain peeked packets */
17102276f58aSPaolo Abeni 	} while (timeo &&
17112276f58aSPaolo Abeni 		 !__skb_wait_for_more_packets(sk, &error, &timeo,
17122276f58aSPaolo Abeni 					      (struct sk_buff *)sk_queue));
17132276f58aSPaolo Abeni 
17142276f58aSPaolo Abeni 	*err = error;
17152276f58aSPaolo Abeni 	return NULL;
17162276f58aSPaolo Abeni }
17177e823644SJiri Kosina EXPORT_SYMBOL(__skb_recv_udp);
17182276f58aSPaolo Abeni 
1719db8dac20SDavid S. Miller /*
1720db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
1721db8dac20SDavid S. Miller  * 	return it, otherwise we block.
1722db8dac20SDavid S. Miller  */
1723db8dac20SDavid S. Miller 
17241b784140SYing Xue int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
17251b784140SYing Xue 		int flags, int *addr_len)
1726db8dac20SDavid S. Miller {
1727db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1728342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
1729db8dac20SDavid S. Miller 	struct sk_buff *skb;
173059c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
1731fd69c399SPaolo Abeni 	int off, err, peeking = flags & MSG_PEEK;
1732db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1733197c949eSEric Dumazet 	bool checksum_valid = false;
1734db8dac20SDavid S. Miller 
1735db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
173685fbaa75SHannes Frederic Sowa 		return ip_recv_error(sk, msg, len, addr_len);
1737db8dac20SDavid S. Miller 
1738db8dac20SDavid S. Miller try_again:
1739a0917e0bSMatthew Dawson 	off = sk_peek_offset(sk, flags);
1740fd69c399SPaolo Abeni 	skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
1741db8dac20SDavid S. Miller 	if (!skb)
1742627d2d6bSsamanthakumar 		return err;
1743db8dac20SDavid S. Miller 
1744b65ac446SPaolo Abeni 	ulen = udp_skb_len(skb);
174559c2cdaeSDavid S. Miller 	copied = len;
1746627d2d6bSsamanthakumar 	if (copied > ulen - off)
1747627d2d6bSsamanthakumar 		copied = ulen - off;
174859c2cdaeSDavid S. Miller 	else if (copied < ulen)
1749db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
1750db8dac20SDavid S. Miller 
1751db8dac20SDavid S. Miller 	/*
1752db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
1753db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
1754db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
1755db8dac20SDavid S. Miller 	 */
1756db8dac20SDavid S. Miller 
1757d21dbdfeSEric Dumazet 	if (copied < ulen || peeking ||
1758d21dbdfeSEric Dumazet 	    (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
1759b65ac446SPaolo Abeni 		checksum_valid = udp_skb_csum_unnecessary(skb) ||
1760b65ac446SPaolo Abeni 				!__udp_lib_checksum_complete(skb);
1761197c949eSEric Dumazet 		if (!checksum_valid)
1762db8dac20SDavid S. Miller 			goto csum_copy_err;
1763db8dac20SDavid S. Miller 	}
1764db8dac20SDavid S. Miller 
1765b65ac446SPaolo Abeni 	if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
1766b65ac446SPaolo Abeni 		if (udp_skb_is_linear(skb))
1767b65ac446SPaolo Abeni 			err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
1768b65ac446SPaolo Abeni 		else
1769627d2d6bSsamanthakumar 			err = skb_copy_datagram_msg(skb, off, msg, copied);
1770b65ac446SPaolo Abeni 	} else {
1771627d2d6bSsamanthakumar 		err = skb_copy_and_csum_datagram_msg(skb, off, msg);
1772db8dac20SDavid S. Miller 
1773db8dac20SDavid S. Miller 		if (err == -EINVAL)
1774db8dac20SDavid S. Miller 			goto csum_copy_err;
1775db8dac20SDavid S. Miller 	}
1776db8dac20SDavid S. Miller 
177722911fc5SEric Dumazet 	if (unlikely(err)) {
1778fd69c399SPaolo Abeni 		if (!peeking) {
1779979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
17806aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk),
1781979402b1SEric Dumazet 				      UDP_MIB_INERRORS, is_udplite);
1782979402b1SEric Dumazet 		}
1783850cbaddSPaolo Abeni 		kfree_skb(skb);
1784627d2d6bSsamanthakumar 		return err;
178522911fc5SEric Dumazet 	}
1786db8dac20SDavid S. Miller 
1787fd69c399SPaolo Abeni 	if (!peeking)
17886aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk),
1789629ca23cSPavel Emelyanov 			      UDP_MIB_INDATAGRAMS, is_udplite);
1790db8dac20SDavid S. Miller 
17913b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
1792db8dac20SDavid S. Miller 
1793db8dac20SDavid S. Miller 	/* Copy the address. */
1794c482c568SEric Dumazet 	if (sin) {
1795db8dac20SDavid S. Miller 		sin->sin_family = AF_INET;
1796db8dac20SDavid S. Miller 		sin->sin_port = udp_hdr(skb)->source;
1797db8dac20SDavid S. Miller 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1798db8dac20SDavid S. Miller 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1799bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin);
1800983695faSDaniel Borkmann 
1801983695faSDaniel Borkmann 		if (cgroup_bpf_enabled)
1802983695faSDaniel Borkmann 			BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk,
1803983695faSDaniel Borkmann 							(struct sockaddr *)sin);
1804db8dac20SDavid S. Miller 	}
1805bcd1665eSPaolo Abeni 
1806bcd1665eSPaolo Abeni 	if (udp_sk(sk)->gro_enabled)
1807bcd1665eSPaolo Abeni 		udp_cmsg_recv(msg, sk, skb);
1808bcd1665eSPaolo Abeni 
1809db8dac20SDavid S. Miller 	if (inet->cmsg_flags)
1810ad959036SPaolo Abeni 		ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off);
1811db8dac20SDavid S. Miller 
181259c2cdaeSDavid S. Miller 	err = copied;
1813db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
1814db8dac20SDavid S. Miller 		err = ulen;
1815db8dac20SDavid S. Miller 
1816850cbaddSPaolo Abeni 	skb_consume_udp(sk, skb, peeking ? -err : err);
1817db8dac20SDavid S. Miller 	return err;
1818db8dac20SDavid S. Miller 
1819db8dac20SDavid S. Miller csum_copy_err:
18202276f58aSPaolo Abeni 	if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
18212276f58aSPaolo Abeni 				 udp_skb_destructor)) {
18226aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
18236aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
18246a5dc9e5SEric Dumazet 	}
1825850cbaddSPaolo Abeni 	kfree_skb(skb);
1826db8dac20SDavid S. Miller 
1827beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
1828beb39db5SEric Dumazet 	cond_resched();
18299cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
1830db8dac20SDavid S. Miller 	goto try_again;
1831db8dac20SDavid S. Miller }
1832db8dac20SDavid S. Miller 
1833d74bad4eSAndrey Ignatov int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
1834d74bad4eSAndrey Ignatov {
1835d74bad4eSAndrey Ignatov 	/* This check is replicated from __ip4_datagram_connect() and
1836d74bad4eSAndrey Ignatov 	 * intended to prevent BPF program called below from accessing bytes
1837d74bad4eSAndrey Ignatov 	 * that are out of the bound specified by user in addr_len.
1838d74bad4eSAndrey Ignatov 	 */
1839d74bad4eSAndrey Ignatov 	if (addr_len < sizeof(struct sockaddr_in))
1840d74bad4eSAndrey Ignatov 		return -EINVAL;
1841d74bad4eSAndrey Ignatov 
1842d74bad4eSAndrey Ignatov 	return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr);
1843d74bad4eSAndrey Ignatov }
1844d74bad4eSAndrey Ignatov EXPORT_SYMBOL(udp_pre_connect);
1845d74bad4eSAndrey Ignatov 
1846286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags)
18471da177e4SLinus Torvalds {
18481da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
18491da177e4SLinus Torvalds 	/*
18501da177e4SLinus Torvalds 	 *	1003.1g - break association.
18511da177e4SLinus Torvalds 	 */
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
1854c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
1855c720c7e8SEric Dumazet 	inet->inet_dport = 0;
1856bdeab991STom Herbert 	sock_rps_reset_rxhash(sk);
18571da177e4SLinus Torvalds 	sk->sk_bound_dev_if = 0;
18581da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
18591da177e4SLinus Torvalds 		inet_reset_saddr(sk);
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
18621da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1863c720c7e8SEric Dumazet 		inet->inet_sport = 0;
18641da177e4SLinus Torvalds 	}
18651da177e4SLinus Torvalds 	sk_dst_reset(sk);
18661da177e4SLinus Torvalds 	return 0;
18671da177e4SLinus Torvalds }
1868286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect);
1869286c72deSEric Dumazet 
1870286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags)
1871286c72deSEric Dumazet {
1872286c72deSEric Dumazet 	lock_sock(sk);
1873286c72deSEric Dumazet 	__udp_disconnect(sk, flags);
1874286c72deSEric Dumazet 	release_sock(sk);
1875286c72deSEric Dumazet 	return 0;
1876286c72deSEric Dumazet }
1877c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect);
18781da177e4SLinus Torvalds 
1879645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk)
1880645ca708SEric Dumazet {
1881723b4610SEric Dumazet 	if (sk_hashed(sk)) {
1882645ca708SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1883512615b6SEric Dumazet 		struct udp_hslot *hslot, *hslot2;
1884512615b6SEric Dumazet 
1885512615b6SEric Dumazet 		hslot  = udp_hashslot(udptable, sock_net(sk),
1886d4cada4aSEric Dumazet 				      udp_sk(sk)->udp_port_hash);
1887512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1888645ca708SEric Dumazet 
1889c8db3fecSEric Dumazet 		spin_lock_bh(&hslot->lock);
1890e32ea7e7SCraig Gallek 		if (rcu_access_pointer(sk->sk_reuseport_cb))
1891e32ea7e7SCraig Gallek 			reuseport_detach_sock(sk);
1892ca065d0cSEric Dumazet 		if (sk_del_node_init_rcu(sk)) {
1893fdcc8aa9SEric Dumazet 			hslot->count--;
1894c720c7e8SEric Dumazet 			inet_sk(sk)->inet_num = 0;
1895645ca708SEric Dumazet 			sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1896512615b6SEric Dumazet 
1897512615b6SEric Dumazet 			spin_lock(&hslot2->lock);
1898ca065d0cSEric Dumazet 			hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1899512615b6SEric Dumazet 			hslot2->count--;
1900512615b6SEric Dumazet 			spin_unlock(&hslot2->lock);
1901645ca708SEric Dumazet 		}
1902c8db3fecSEric Dumazet 		spin_unlock_bh(&hslot->lock);
1903645ca708SEric Dumazet 	}
1904723b4610SEric Dumazet }
1905645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash);
1906645ca708SEric Dumazet 
1907719f8358SEric Dumazet /*
1908719f8358SEric Dumazet  * inet_rcv_saddr was changed, we must rehash secondary hash
1909719f8358SEric Dumazet  */
1910719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash)
1911719f8358SEric Dumazet {
1912719f8358SEric Dumazet 	if (sk_hashed(sk)) {
1913719f8358SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1914719f8358SEric Dumazet 		struct udp_hslot *hslot, *hslot2, *nhslot2;
1915719f8358SEric Dumazet 
1916719f8358SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1917719f8358SEric Dumazet 		nhslot2 = udp_hashslot2(udptable, newhash);
1918719f8358SEric Dumazet 		udp_sk(sk)->udp_portaddr_hash = newhash;
1919e32ea7e7SCraig Gallek 
1920e32ea7e7SCraig Gallek 		if (hslot2 != nhslot2 ||
1921e32ea7e7SCraig Gallek 		    rcu_access_pointer(sk->sk_reuseport_cb)) {
1922719f8358SEric Dumazet 			hslot = udp_hashslot(udptable, sock_net(sk),
1923719f8358SEric Dumazet 					     udp_sk(sk)->udp_port_hash);
1924719f8358SEric Dumazet 			/* we must lock primary chain too */
1925719f8358SEric Dumazet 			spin_lock_bh(&hslot->lock);
1926e32ea7e7SCraig Gallek 			if (rcu_access_pointer(sk->sk_reuseport_cb))
1927e32ea7e7SCraig Gallek 				reuseport_detach_sock(sk);
1928719f8358SEric Dumazet 
1929e32ea7e7SCraig Gallek 			if (hslot2 != nhslot2) {
1930719f8358SEric Dumazet 				spin_lock(&hslot2->lock);
1931ca065d0cSEric Dumazet 				hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1932719f8358SEric Dumazet 				hslot2->count--;
1933719f8358SEric Dumazet 				spin_unlock(&hslot2->lock);
1934719f8358SEric Dumazet 
1935719f8358SEric Dumazet 				spin_lock(&nhslot2->lock);
1936ca065d0cSEric Dumazet 				hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
1937719f8358SEric Dumazet 							 &nhslot2->head);
1938719f8358SEric Dumazet 				nhslot2->count++;
1939719f8358SEric Dumazet 				spin_unlock(&nhslot2->lock);
1940e32ea7e7SCraig Gallek 			}
1941719f8358SEric Dumazet 
1942719f8358SEric Dumazet 			spin_unlock_bh(&hslot->lock);
1943719f8358SEric Dumazet 		}
1944719f8358SEric Dumazet 	}
1945719f8358SEric Dumazet }
1946719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash);
1947719f8358SEric Dumazet 
19488f6b5392SAlexey Kodanev void udp_v4_rehash(struct sock *sk)
1949719f8358SEric Dumazet {
1950f0b1e64cSMartin KaFai Lau 	u16 new_hash = ipv4_portaddr_hash(sock_net(sk),
1951719f8358SEric Dumazet 					  inet_sk(sk)->inet_rcv_saddr,
1952719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
1953719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
1954719f8358SEric Dumazet }
1955719f8358SEric Dumazet 
1956a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
195793821778SHerbert Xu {
1958fec5e652STom Herbert 	int rc;
195993821778SHerbert Xu 
1960005ec974SShawn Bohrer 	if (inet_sk(sk)->inet_daddr) {
1961bdeab991STom Herbert 		sock_rps_save_rxhash(sk, skb);
1962005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
19632c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
1964e68b6e50SEric Dumazet 	} else {
1965e68b6e50SEric Dumazet 		sk_mark_napi_id_once(sk, skb);
1966005ec974SShawn Bohrer 	}
1967fec5e652STom Herbert 
1968850cbaddSPaolo Abeni 	rc = __udp_enqueue_schedule_skb(sk, skb);
1969766e9037SEric Dumazet 	if (rc < 0) {
1970766e9037SEric Dumazet 		int is_udplite = IS_UDPLITE(sk);
1971766e9037SEric Dumazet 
197293821778SHerbert Xu 		/* Note that an ENOMEM error is charged twice */
1973766e9037SEric Dumazet 		if (rc == -ENOMEM)
1974e61da9e2SEric Dumazet 			UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS,
197593821778SHerbert Xu 					is_udplite);
1976e61da9e2SEric Dumazet 		UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1977766e9037SEric Dumazet 		kfree_skb(skb);
1978296f7ea7SSatoru Moriya 		trace_udp_fail_queue_rcv_skb(rc, sk);
1979766e9037SEric Dumazet 		return -1;
198093821778SHerbert Xu 	}
198193821778SHerbert Xu 
198293821778SHerbert Xu 	return 0;
198393821778SHerbert Xu }
198493821778SHerbert Xu 
1985db8dac20SDavid S. Miller /* returns:
1986db8dac20SDavid S. Miller  *  -1: error
1987db8dac20SDavid S. Miller  *   0: success
1988db8dac20SDavid S. Miller  *  >0: "udp encap" protocol resubmission
1989db8dac20SDavid S. Miller  *
1990db8dac20SDavid S. Miller  * Note that in the success and error cases, the skb is assumed to
1991db8dac20SDavid S. Miller  * have either been requeued or freed.
1992db8dac20SDavid S. Miller  */
1993cf329aa4SPaolo Abeni static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
1994db8dac20SDavid S. Miller {
1995db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1996db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1997db8dac20SDavid S. Miller 
1998db8dac20SDavid S. Miller 	/*
1999db8dac20SDavid S. Miller 	 *	Charge it to the socket, dropping if the queue is full.
2000db8dac20SDavid S. Miller 	 */
2001db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
2002db8dac20SDavid S. Miller 		goto drop;
2003895b5c9fSFlorian Westphal 	nf_reset_ct(skb);
2004db8dac20SDavid S. Miller 
200588ab3108SDavidlohr Bueso 	if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) {
20060ad92ad0SEric Dumazet 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
20070ad92ad0SEric Dumazet 
2008db8dac20SDavid S. Miller 		/*
2009db8dac20SDavid S. Miller 		 * This is an encapsulation socket so pass the skb to
2010db8dac20SDavid S. Miller 		 * the socket's udp_encap_rcv() hook. Otherwise, just
2011db8dac20SDavid S. Miller 		 * fall through and pass this up the UDP socket.
2012db8dac20SDavid S. Miller 		 * up->encap_rcv() returns the following value:
2013db8dac20SDavid S. Miller 		 * =0 if skb was successfully passed to the encap
2014db8dac20SDavid S. Miller 		 *    handler or was discarded by it.
2015db8dac20SDavid S. Miller 		 * >0 if skb should be passed on to UDP.
2016db8dac20SDavid S. Miller 		 * <0 if skb should be resubmitted as proto -N
2017db8dac20SDavid S. Miller 		 */
2018db8dac20SDavid S. Miller 
2019db8dac20SDavid S. Miller 		/* if we're overly short, let UDP handle it */
20206aa7de05SMark Rutland 		encap_rcv = READ_ONCE(up->encap_rcv);
2021e5aed006SHannes Frederic Sowa 		if (encap_rcv) {
2022db8dac20SDavid S. Miller 			int ret;
2023db8dac20SDavid S. Miller 
20240a80966bSTom Herbert 			/* Verify checksum before giving to encap */
20250a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
20260a80966bSTom Herbert 				goto csum_error;
20270a80966bSTom Herbert 
20280ad92ad0SEric Dumazet 			ret = encap_rcv(sk, skb);
2029db8dac20SDavid S. Miller 			if (ret <= 0) {
203002c22347SEric Dumazet 				__UDP_INC_STATS(sock_net(sk),
20310283328eSPavel Emelyanov 						UDP_MIB_INDATAGRAMS,
2032db8dac20SDavid S. Miller 						is_udplite);
2033db8dac20SDavid S. Miller 				return -ret;
2034db8dac20SDavid S. Miller 			}
2035db8dac20SDavid S. Miller 		}
2036db8dac20SDavid S. Miller 
2037db8dac20SDavid S. Miller 		/* FALLTHROUGH -- it's a UDP Packet */
2038db8dac20SDavid S. Miller 	}
2039db8dac20SDavid S. Miller 
2040db8dac20SDavid S. Miller 	/*
2041db8dac20SDavid S. Miller 	 * 	UDP-Lite specific tests, ignored on UDP sockets
2042db8dac20SDavid S. Miller 	 */
2043db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
2044db8dac20SDavid S. Miller 
2045db8dac20SDavid S. Miller 		/*
2046db8dac20SDavid S. Miller 		 * MIB statistics other than incrementing the error count are
2047db8dac20SDavid S. Miller 		 * disabled for the following two types of errors: these depend
2048db8dac20SDavid S. Miller 		 * on the application settings, not on the functioning of the
2049db8dac20SDavid S. Miller 		 * protocol stack as such.
2050db8dac20SDavid S. Miller 		 *
2051db8dac20SDavid S. Miller 		 * RFC 3828 here recommends (sec 3.3): "There should also be a
2052db8dac20SDavid S. Miller 		 * way ... to ... at least let the receiving application block
2053db8dac20SDavid S. Miller 		 * delivery of packets with coverage values less than a value
2054db8dac20SDavid S. Miller 		 * provided by the application."
2055db8dac20SDavid S. Miller 		 */
2056db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
2057ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n",
2058db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
2059db8dac20SDavid S. Miller 			goto drop;
2060db8dac20SDavid S. Miller 		}
2061db8dac20SDavid S. Miller 		/* The next case involves violating the min. coverage requested
2062db8dac20SDavid S. Miller 		 * by the receiver. This is subtle: if receiver wants x and x is
2063db8dac20SDavid S. Miller 		 * greater than the buffersize/MTU then receiver will complain
2064db8dac20SDavid S. Miller 		 * that it wants x while sender emits packets of smaller size y.
2065db8dac20SDavid S. Miller 		 * Therefore the above ...()->partial_cov statement is essential.
2066db8dac20SDavid S. Miller 		 */
2067db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
2068ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n",
2069db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
2070db8dac20SDavid S. Miller 			goto drop;
2071db8dac20SDavid S. Miller 		}
2072db8dac20SDavid S. Miller 	}
2073db8dac20SDavid S. Miller 
2074dd99e425SPaolo Abeni 	prefetch(&sk->sk_rmem_alloc);
2075ce25d66aSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
2076ce25d66aSEric Dumazet 	    udp_lib_checksum_complete(skb))
20776a5dc9e5SEric Dumazet 			goto csum_error;
2078ce25d66aSEric Dumazet 
2079ba66bbe5SDaniel Borkmann 	if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
2080a6127697SMichal Kubeček 		goto drop;
2081db8dac20SDavid S. Miller 
2082e6afc8acSsamanthakumar 	udp_csum_pull_header(skb);
2083db8dac20SDavid S. Miller 
2084fbf8866dSShawn Bohrer 	ipv4_pktinfo_prepare(sk, skb);
2085850cbaddSPaolo Abeni 	return __udp_queue_rcv_skb(sk, skb);
2086db8dac20SDavid S. Miller 
20876a5dc9e5SEric Dumazet csum_error:
208802c22347SEric Dumazet 	__UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
2089db8dac20SDavid S. Miller drop:
209002c22347SEric Dumazet 	__UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
20918edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
2092db8dac20SDavid S. Miller 	kfree_skb(skb);
2093db8dac20SDavid S. Miller 	return -1;
2094db8dac20SDavid S. Miller }
2095db8dac20SDavid S. Miller 
2096cf329aa4SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
2097cf329aa4SPaolo Abeni {
2098cf329aa4SPaolo Abeni 	struct sk_buff *next, *segs;
2099cf329aa4SPaolo Abeni 	int ret;
2100cf329aa4SPaolo Abeni 
2101cf329aa4SPaolo Abeni 	if (likely(!udp_unexpected_gso(sk, skb)))
2102cf329aa4SPaolo Abeni 		return udp_queue_rcv_one_skb(sk, skb);
2103cf329aa4SPaolo Abeni 
2104cf329aa4SPaolo Abeni 	BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_SGO_CB_OFFSET);
2105cf329aa4SPaolo Abeni 	__skb_push(skb, -skb_mac_offset(skb));
2106cf329aa4SPaolo Abeni 	segs = udp_rcv_segment(sk, skb, true);
2107*1a186c14SJason A. Donenfeld 	skb_list_walk_safe(segs, skb, next) {
2108cf329aa4SPaolo Abeni 		__skb_pull(skb, skb_transport_offset(skb));
2109cf329aa4SPaolo Abeni 		ret = udp_queue_rcv_one_skb(sk, skb);
2110cf329aa4SPaolo Abeni 		if (ret > 0)
2111cf329aa4SPaolo Abeni 			ip_protocol_deliver_rcu(dev_net(skb->dev), skb, -ret);
2112cf329aa4SPaolo Abeni 	}
2113cf329aa4SPaolo Abeni 	return 0;
2114cf329aa4SPaolo Abeni }
2115cf329aa4SPaolo Abeni 
211697502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock
2117e47eb5dfSEric Dumazet  * For UDP, we use xchg() to guard against concurrent changes.
211897502231SEric Dumazet  */
211964f0f5d1SPaolo Abeni bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
2120421b3885SShawn Bohrer {
212197502231SEric Dumazet 	struct dst_entry *old;
2122421b3885SShawn Bohrer 
2123d24406c8SWei Wang 	if (dst_hold_safe(dst)) {
2124e47eb5dfSEric Dumazet 		old = xchg(&sk->sk_rx_dst, dst);
212597502231SEric Dumazet 		dst_release(old);
212664f0f5d1SPaolo Abeni 		return old != dst;
212797502231SEric Dumazet 	}
212864f0f5d1SPaolo Abeni 	return false;
2129d24406c8SWei Wang }
2130c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set);
2131421b3885SShawn Bohrer 
2132db8dac20SDavid S. Miller /*
2133db8dac20SDavid S. Miller  *	Multicasts and broadcasts go to each listener.
2134db8dac20SDavid S. Miller  *
21351240d137SEric Dumazet  *	Note: called only from the BH handler context.
2136db8dac20SDavid S. Miller  */
2137e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
2138db8dac20SDavid S. Miller 				    struct udphdr  *uh,
2139db8dac20SDavid S. Miller 				    __be32 saddr, __be32 daddr,
214036cbb245SRick Jones 				    struct udp_table *udptable,
214136cbb245SRick Jones 				    int proto)
2142db8dac20SDavid S. Miller {
2143ca065d0cSEric Dumazet 	struct sock *sk, *first = NULL;
21445cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
21455cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
21462dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
2147ca065d0cSEric Dumazet 	unsigned int offset = offsetof(typeof(*sk), sk_node);
2148ca065d0cSEric Dumazet 	int dif = skb->dev->ifindex;
2149fb74c277SDavid Ahern 	int sdif = inet_sdif(skb);
2150ca065d0cSEric Dumazet 	struct hlist_node *node;
2151ca065d0cSEric Dumazet 	struct sk_buff *nskb;
21522dc41cffSDavid Held 
21532dc41cffSDavid Held 	if (use_hash2) {
2154f0b1e64cSMartin KaFai Lau 		hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) &
215573e2d5e3SPablo Neira 			    udptable->mask;
2156f0b1e64cSMartin KaFai Lau 		hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask;
21572dc41cffSDavid Held start_lookup:
215873e2d5e3SPablo Neira 		hslot = &udptable->hash2[hash2];
21592dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
21602dc41cffSDavid Held 	}
2161db8dac20SDavid S. Miller 
2162ca065d0cSEric Dumazet 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
2163ca065d0cSEric Dumazet 		if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr,
2164fb74c277SDavid Ahern 					 uh->source, saddr, dif, sdif, hnum))
2165ca065d0cSEric Dumazet 			continue;
21661240d137SEric Dumazet 
2167ca065d0cSEric Dumazet 		if (!first) {
2168ca065d0cSEric Dumazet 			first = sk;
2169ca065d0cSEric Dumazet 			continue;
2170ca065d0cSEric Dumazet 		}
2171ca065d0cSEric Dumazet 		nskb = skb_clone(skb, GFP_ATOMIC);
2172ca065d0cSEric Dumazet 
2173ca065d0cSEric Dumazet 		if (unlikely(!nskb)) {
2174ca065d0cSEric Dumazet 			atomic_inc(&sk->sk_drops);
217502c22347SEric Dumazet 			__UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
2176ca065d0cSEric Dumazet 					IS_UDPLITE(sk));
217702c22347SEric Dumazet 			__UDP_INC_STATS(net, UDP_MIB_INERRORS,
2178ca065d0cSEric Dumazet 					IS_UDPLITE(sk));
2179ca065d0cSEric Dumazet 			continue;
2180ca065d0cSEric Dumazet 		}
2181ca065d0cSEric Dumazet 		if (udp_queue_rcv_skb(sk, nskb) > 0)
2182ca065d0cSEric Dumazet 			consume_skb(nskb);
2183ca065d0cSEric Dumazet 	}
21841240d137SEric Dumazet 
21852dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
21862dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
21872dc41cffSDavid Held 		hash2 = hash2_any;
21882dc41cffSDavid Held 		goto start_lookup;
21892dc41cffSDavid Held 	}
21902dc41cffSDavid Held 
2191ca065d0cSEric Dumazet 	if (first) {
2192ca065d0cSEric Dumazet 		if (udp_queue_rcv_skb(first, skb) > 0)
2193ca065d0cSEric Dumazet 			consume_skb(skb);
21941240d137SEric Dumazet 	} else {
2195ca065d0cSEric Dumazet 		kfree_skb(skb);
219602c22347SEric Dumazet 		__UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
219736cbb245SRick Jones 				proto == IPPROTO_UDPLITE);
21981240d137SEric Dumazet 	}
2199db8dac20SDavid S. Miller 	return 0;
2200db8dac20SDavid S. Miller }
2201db8dac20SDavid S. Miller 
2202db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success),
2203db8dac20SDavid S. Miller  * CHECKSUM_UNNECESSARY means, that no more checks are required.
2204666a3d6eSSu Yanjun  * Otherwise, csum completion requires checksumming packet body,
2205db8dac20SDavid S. Miller  * including udp header and folding it to skb->csum.
2206db8dac20SDavid S. Miller  */
2207db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
2208db8dac20SDavid S. Miller 				 int proto)
2209db8dac20SDavid S. Miller {
2210db8dac20SDavid S. Miller 	int err;
2211db8dac20SDavid S. Miller 
2212db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
2213db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
2214db8dac20SDavid S. Miller 
2215db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
2216db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
2217db8dac20SDavid S. Miller 		if (err)
2218db8dac20SDavid S. Miller 			return err;
221915f35d49SAlexey Kodanev 
222015f35d49SAlexey Kodanev 		if (UDP_SKB_CB(skb)->partial_cov) {
222115f35d49SAlexey Kodanev 			skb->csum = inet_compute_pseudo(skb, proto);
222215f35d49SAlexey Kodanev 			return 0;
222315f35d49SAlexey Kodanev 		}
2224db8dac20SDavid S. Miller 	}
2225db8dac20SDavid S. Miller 
2226b46d9f62SHannes Frederic Sowa 	/* Note, we are only interested in != 0 or == 0, thus the
2227b46d9f62SHannes Frederic Sowa 	 * force to int.
2228b46d9f62SHannes Frederic Sowa 	 */
2229db4f1be3SSean Tranchetti 	err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check,
2230ed70fcfcSTom Herbert 							inet_compute_pseudo);
2231db4f1be3SSean Tranchetti 	if (err)
2232db4f1be3SSean Tranchetti 		return err;
2233db4f1be3SSean Tranchetti 
2234db4f1be3SSean Tranchetti 	if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) {
2235db4f1be3SSean Tranchetti 		/* If SW calculated the value, we know it's bad */
2236db4f1be3SSean Tranchetti 		if (skb->csum_complete_sw)
2237db4f1be3SSean Tranchetti 			return 1;
2238db4f1be3SSean Tranchetti 
2239db4f1be3SSean Tranchetti 		/* HW says the value is bad. Let's validate that.
2240db4f1be3SSean Tranchetti 		 * skb->csum is no longer the full packet checksum,
2241db4f1be3SSean Tranchetti 		 * so don't treat it as such.
2242db4f1be3SSean Tranchetti 		 */
2243db4f1be3SSean Tranchetti 		skb_checksum_complete_unset(skb);
2244db4f1be3SSean Tranchetti 	}
2245db4f1be3SSean Tranchetti 
2246db4f1be3SSean Tranchetti 	return 0;
2247db8dac20SDavid S. Miller }
2248db8dac20SDavid S. Miller 
22492b5a9217SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
22502b5a9217SPaolo Abeni  * return code conversion for ip layer consumption
22512b5a9217SPaolo Abeni  */
22522b5a9217SPaolo Abeni static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
22532b5a9217SPaolo Abeni 			       struct udphdr *uh)
22542b5a9217SPaolo Abeni {
22552b5a9217SPaolo Abeni 	int ret;
22562b5a9217SPaolo Abeni 
22572b5a9217SPaolo Abeni 	if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
2258e4aa33adSLi RongQing 		skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo);
22592b5a9217SPaolo Abeni 
22602b5a9217SPaolo Abeni 	ret = udp_queue_rcv_skb(sk, skb);
22612b5a9217SPaolo Abeni 
22622b5a9217SPaolo Abeni 	/* a return value > 0 means to resubmit the input, but
22632b5a9217SPaolo Abeni 	 * it wants the return to be -protocol, or 0
22642b5a9217SPaolo Abeni 	 */
22652b5a9217SPaolo Abeni 	if (ret > 0)
22662b5a9217SPaolo Abeni 		return -ret;
22672b5a9217SPaolo Abeni 	return 0;
22682b5a9217SPaolo Abeni }
22692b5a9217SPaolo Abeni 
2270db8dac20SDavid S. Miller /*
2271db8dac20SDavid S. Miller  *	All we need to do is get the socket, and then do a checksum.
2272db8dac20SDavid S. Miller  */
2273db8dac20SDavid S. Miller 
2274645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
2275db8dac20SDavid S. Miller 		   int proto)
2276db8dac20SDavid S. Miller {
2277db8dac20SDavid S. Miller 	struct sock *sk;
22787b5e56f9SJesper Dangaard Brouer 	struct udphdr *uh;
2279db8dac20SDavid S. Miller 	unsigned short ulen;
2280adf30907SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
22812783ef23SJesper Dangaard Brouer 	__be32 saddr, daddr;
22820283328eSPavel Emelyanov 	struct net *net = dev_net(skb->dev);
2283db8dac20SDavid S. Miller 
2284db8dac20SDavid S. Miller 	/*
2285db8dac20SDavid S. Miller 	 *  Validate the packet.
2286db8dac20SDavid S. Miller 	 */
2287db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
2288db8dac20SDavid S. Miller 		goto drop;		/* No space for header. */
2289db8dac20SDavid S. Miller 
22907b5e56f9SJesper Dangaard Brouer 	uh   = udp_hdr(skb);
2291db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
2292ccc2d97cSBjørn Mork 	saddr = ip_hdr(skb)->saddr;
2293ccc2d97cSBjørn Mork 	daddr = ip_hdr(skb)->daddr;
2294ccc2d97cSBjørn Mork 
2295db8dac20SDavid S. Miller 	if (ulen > skb->len)
2296db8dac20SDavid S. Miller 		goto short_packet;
2297db8dac20SDavid S. Miller 
2298db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
2299db8dac20SDavid S. Miller 		/* UDP validates ulen. */
2300db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
2301db8dac20SDavid S. Miller 			goto short_packet;
2302db8dac20SDavid S. Miller 		uh = udp_hdr(skb);
2303db8dac20SDavid S. Miller 	}
2304db8dac20SDavid S. Miller 
2305db8dac20SDavid S. Miller 	if (udp4_csum_init(skb, uh, proto))
2306db8dac20SDavid S. Miller 		goto csum_error;
2307db8dac20SDavid S. Miller 
23088afdd99aSEric Dumazet 	sk = skb_steal_sock(skb);
23098afdd99aSEric Dumazet 	if (sk) {
231097502231SEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
2311421b3885SShawn Bohrer 		int ret;
2312421b3885SShawn Bohrer 
231397502231SEric Dumazet 		if (unlikely(sk->sk_rx_dst != dst))
231497502231SEric Dumazet 			udp_sk_rx_dst_set(sk, dst);
2315421b3885SShawn Bohrer 
23162b5a9217SPaolo Abeni 		ret = udp_unicast_rcv_skb(sk, skb, uh);
23178afdd99aSEric Dumazet 		sock_put(sk);
23182b5a9217SPaolo Abeni 		return ret;
2319c18450a5SFabian Frederick 	}
2320c18450a5SFabian Frederick 
2321db8dac20SDavid S. Miller 	if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
2322e3163493SPavel Emelyanov 		return __udp4_lib_mcast_deliver(net, skb, uh,
232336cbb245SRick Jones 						saddr, daddr, udptable, proto);
2324db8dac20SDavid S. Miller 
2325607c4aafSKOVACS Krisztian 	sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
23262b5a9217SPaolo Abeni 	if (sk)
23272b5a9217SPaolo Abeni 		return udp_unicast_rcv_skb(sk, skb, uh);
2328db8dac20SDavid S. Miller 
2329db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
2330db8dac20SDavid S. Miller 		goto drop;
2331895b5c9fSFlorian Westphal 	nf_reset_ct(skb);
2332db8dac20SDavid S. Miller 
2333db8dac20SDavid S. Miller 	/* No socket. Drop packet silently, if checksum is wrong */
2334db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
2335db8dac20SDavid S. Miller 		goto csum_error;
2336db8dac20SDavid S. Miller 
233702c22347SEric Dumazet 	__UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
2338db8dac20SDavid S. Miller 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
2339db8dac20SDavid S. Miller 
2340db8dac20SDavid S. Miller 	/*
2341db8dac20SDavid S. Miller 	 * Hmm.  We got an UDP packet to a port to which we
2342db8dac20SDavid S. Miller 	 * don't wanna listen.  Ignore it.
2343db8dac20SDavid S. Miller 	 */
2344db8dac20SDavid S. Miller 	kfree_skb(skb);
2345db8dac20SDavid S. Miller 	return 0;
2346db8dac20SDavid S. Miller 
2347db8dac20SDavid S. Miller short_packet:
2348ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
2349afd46503SJoe Perches 			    proto == IPPROTO_UDPLITE ? "Lite" : "",
2350afd46503SJoe Perches 			    &saddr, ntohs(uh->source),
2351afd46503SJoe Perches 			    ulen, skb->len,
2352afd46503SJoe Perches 			    &daddr, ntohs(uh->dest));
2353db8dac20SDavid S. Miller 	goto drop;
2354db8dac20SDavid S. Miller 
2355db8dac20SDavid S. Miller csum_error:
2356db8dac20SDavid S. Miller 	/*
2357db8dac20SDavid S. Miller 	 * RFC1122: OK.  Discards the bad packet silently (as far as
2358db8dac20SDavid S. Miller 	 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
2359db8dac20SDavid S. Miller 	 */
2360ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
2361afd46503SJoe Perches 			    proto == IPPROTO_UDPLITE ? "Lite" : "",
2362afd46503SJoe Perches 			    &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
2363db8dac20SDavid S. Miller 			    ulen);
236402c22347SEric Dumazet 	__UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
2365db8dac20SDavid S. Miller drop:
236602c22347SEric Dumazet 	__UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
2367db8dac20SDavid S. Miller 	kfree_skb(skb);
2368db8dac20SDavid S. Miller 	return 0;
2369db8dac20SDavid S. Miller }
2370db8dac20SDavid S. Miller 
2371421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket.
2372421b3885SShawn Bohrer  * If more than one socket found returns NULL
2373421b3885SShawn Bohrer  */
2374421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
2375421b3885SShawn Bohrer 						  __be16 loc_port, __be32 loc_addr,
2376421b3885SShawn Bohrer 						  __be16 rmt_port, __be32 rmt_addr,
2377fb74c277SDavid Ahern 						  int dif, int sdif)
2378421b3885SShawn Bohrer {
2379421b3885SShawn Bohrer 	struct sock *sk, *result;
2380421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
2381ca065d0cSEric Dumazet 	unsigned int slot = udp_hashfn(net, hnum, udp_table.mask);
2382421b3885SShawn Bohrer 	struct udp_hslot *hslot = &udp_table.hash[slot];
2383421b3885SShawn Bohrer 
238463c6f81cSEric Dumazet 	/* Do not bother scanning a too big list */
238563c6f81cSEric Dumazet 	if (hslot->count > 10)
238663c6f81cSEric Dumazet 		return NULL;
238763c6f81cSEric Dumazet 
2388421b3885SShawn Bohrer 	result = NULL;
2389ca065d0cSEric Dumazet 	sk_for_each_rcu(sk, &hslot->head) {
2390ca065d0cSEric Dumazet 		if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr,
2391fb74c277SDavid Ahern 					rmt_port, rmt_addr, dif, sdif, hnum)) {
2392ca065d0cSEric Dumazet 			if (result)
2393ca065d0cSEric Dumazet 				return NULL;
2394421b3885SShawn Bohrer 			result = sk;
2395421b3885SShawn Bohrer 		}
2396421b3885SShawn Bohrer 	}
2397421b3885SShawn Bohrer 
2398421b3885SShawn Bohrer 	return result;
2399421b3885SShawn Bohrer }
2400421b3885SShawn Bohrer 
2401421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can
2402421b3885SShawn Bohrer  * break forwarding setups.  The chains here can be long so only check
2403421b3885SShawn Bohrer  * if the first socket is an exact match and if not move on.
2404421b3885SShawn Bohrer  */
2405421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net,
2406421b3885SShawn Bohrer 					    __be16 loc_port, __be32 loc_addr,
2407421b3885SShawn Bohrer 					    __be16 rmt_port, __be32 rmt_addr,
24083fa6f616SDavid Ahern 					    int dif, int sdif)
2409421b3885SShawn Bohrer {
2410421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
2411f0b1e64cSMartin KaFai Lau 	unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum);
2412421b3885SShawn Bohrer 	unsigned int slot2 = hash2 & udp_table.mask;
2413421b3885SShawn Bohrer 	struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
2414c7228317SJoe Perches 	INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
2415421b3885SShawn Bohrer 	const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
2416ca065d0cSEric Dumazet 	struct sock *sk;
2417421b3885SShawn Bohrer 
2418ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
2419ca065d0cSEric Dumazet 		if (INET_MATCH(sk, net, acookie, rmt_addr,
24203fa6f616SDavid Ahern 			       loc_addr, ports, dif, sdif))
2421ca065d0cSEric Dumazet 			return sk;
2422421b3885SShawn Bohrer 		/* Only check first socket in chain */
2423421b3885SShawn Bohrer 		break;
2424421b3885SShawn Bohrer 	}
2425ca065d0cSEric Dumazet 	return NULL;
2426421b3885SShawn Bohrer }
2427421b3885SShawn Bohrer 
24287487449cSPaolo Abeni int udp_v4_early_demux(struct sk_buff *skb)
2429421b3885SShawn Bohrer {
2430610438b7SEric Dumazet 	struct net *net = dev_net(skb->dev);
2431bc044e8dSPaolo Abeni 	struct in_device *in_dev = NULL;
2432610438b7SEric Dumazet 	const struct iphdr *iph;
2433610438b7SEric Dumazet 	const struct udphdr *uh;
2434ca065d0cSEric Dumazet 	struct sock *sk = NULL;
2435421b3885SShawn Bohrer 	struct dst_entry *dst;
2436421b3885SShawn Bohrer 	int dif = skb->dev->ifindex;
2437fb74c277SDavid Ahern 	int sdif = inet_sdif(skb);
24386e540309SShawn Bohrer 	int ours;
2439421b3885SShawn Bohrer 
2440421b3885SShawn Bohrer 	/* validate the packet */
2441421b3885SShawn Bohrer 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
24427487449cSPaolo Abeni 		return 0;
2443421b3885SShawn Bohrer 
2444610438b7SEric Dumazet 	iph = ip_hdr(skb);
2445610438b7SEric Dumazet 	uh = udp_hdr(skb);
2446610438b7SEric Dumazet 
2447996b44fcSPaolo Abeni 	if (skb->pkt_type == PACKET_MULTICAST) {
2448bc044e8dSPaolo Abeni 		in_dev = __in_dev_get_rcu(skb->dev);
24496e540309SShawn Bohrer 
24506e540309SShawn Bohrer 		if (!in_dev)
24517487449cSPaolo Abeni 			return 0;
24526e540309SShawn Bohrer 
24536e540309SShawn Bohrer 		ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr,
24546e540309SShawn Bohrer 				       iph->protocol);
24556e540309SShawn Bohrer 		if (!ours)
24567487449cSPaolo Abeni 			return 0;
2457ad0ea198SPaolo Abeni 
2458421b3885SShawn Bohrer 		sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
2459fb74c277SDavid Ahern 						   uh->source, iph->saddr,
2460fb74c277SDavid Ahern 						   dif, sdif);
24616e540309SShawn Bohrer 	} else if (skb->pkt_type == PACKET_HOST) {
2462421b3885SShawn Bohrer 		sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
24633fa6f616SDavid Ahern 					     uh->source, iph->saddr, dif, sdif);
24646e540309SShawn Bohrer 	}
2465421b3885SShawn Bohrer 
246641c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
24677487449cSPaolo Abeni 		return 0;
2468421b3885SShawn Bohrer 
2469421b3885SShawn Bohrer 	skb->sk = sk;
247082eabd9eSAlexander Duyck 	skb->destructor = sock_efree;
247110e2eb87SEric Dumazet 	dst = READ_ONCE(sk->sk_rx_dst);
2472421b3885SShawn Bohrer 
2473421b3885SShawn Bohrer 	if (dst)
2474421b3885SShawn Bohrer 		dst = dst_check(dst, 0);
247510e2eb87SEric Dumazet 	if (dst) {
2476bc044e8dSPaolo Abeni 		u32 itag = 0;
2477bc044e8dSPaolo Abeni 
2478d24406c8SWei Wang 		/* set noref for now.
2479d24406c8SWei Wang 		 * any place which wants to hold dst has to call
2480d24406c8SWei Wang 		 * dst_hold_safe()
2481d24406c8SWei Wang 		 */
2482421b3885SShawn Bohrer 		skb_dst_set_noref(skb, dst);
2483bc044e8dSPaolo Abeni 
2484bc044e8dSPaolo Abeni 		/* for unconnected multicast sockets we need to validate
2485bc044e8dSPaolo Abeni 		 * the source on each packet
2486bc044e8dSPaolo Abeni 		 */
2487bc044e8dSPaolo Abeni 		if (!inet_sk(sk)->inet_daddr && in_dev)
2488bc044e8dSPaolo Abeni 			return ip_mc_validate_source(skb, iph->daddr,
2489bc044e8dSPaolo Abeni 						     iph->saddr, iph->tos,
2490bc044e8dSPaolo Abeni 						     skb->dev, in_dev, &itag);
2491421b3885SShawn Bohrer 	}
24927487449cSPaolo Abeni 	return 0;
249310e2eb87SEric Dumazet }
2494421b3885SShawn Bohrer 
2495db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb)
2496db8dac20SDavid S. Miller {
2497645ca708SEric Dumazet 	return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
2498db8dac20SDavid S. Miller }
2499db8dac20SDavid S. Miller 
25007d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk)
2501db8dac20SDavid S. Miller {
250244046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
25038a74ad60SEric Dumazet 	bool slow = lock_sock_fast(sk);
2504db8dac20SDavid S. Miller 	udp_flush_pending_frames(sk);
25058a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
250660fb9567SPaolo Abeni 	if (static_branch_unlikely(&udp_encap_needed_key)) {
250760fb9567SPaolo Abeni 		if (up->encap_type) {
250844046a59STom Parkin 			void (*encap_destroy)(struct sock *sk);
25096aa7de05SMark Rutland 			encap_destroy = READ_ONCE(up->encap_destroy);
251044046a59STom Parkin 			if (encap_destroy)
251144046a59STom Parkin 				encap_destroy(sk);
251244046a59STom Parkin 		}
251360fb9567SPaolo Abeni 		if (up->encap_enabled)
25149c480601SPaolo Abeni 			static_branch_dec(&udp_encap_needed_key);
251560fb9567SPaolo Abeni 	}
2516db8dac20SDavid S. Miller }
2517db8dac20SDavid S. Miller 
25181da177e4SLinus Torvalds /*
25191da177e4SLinus Torvalds  *	Socket option code for UDP
25201da177e4SLinus Torvalds  */
25214c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname,
2522b7058842SDavid S. Miller 		       char __user *optval, unsigned int optlen,
25234c0a6cb0SGerrit Renker 		       int (*push_pending_frames)(struct sock *))
25241da177e4SLinus Torvalds {
25251da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
25261c19448cSTom Herbert 	int val, valbool;
25271da177e4SLinus Torvalds 	int err = 0;
2528b2bf1e26SWang Chen 	int is_udplite = IS_UDPLITE(sk);
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	if (optlen < sizeof(int))
25311da177e4SLinus Torvalds 		return -EINVAL;
25321da177e4SLinus Torvalds 
25331da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
25341da177e4SLinus Torvalds 		return -EFAULT;
25351da177e4SLinus Torvalds 
25361c19448cSTom Herbert 	valbool = val ? 1 : 0;
25371c19448cSTom Herbert 
25381da177e4SLinus Torvalds 	switch (optname) {
25391da177e4SLinus Torvalds 	case UDP_CORK:
25401da177e4SLinus Torvalds 		if (val != 0) {
25411da177e4SLinus Torvalds 			up->corkflag = 1;
25421da177e4SLinus Torvalds 		} else {
25431da177e4SLinus Torvalds 			up->corkflag = 0;
25441da177e4SLinus Torvalds 			lock_sock(sk);
25454243cdc2SJoe Perches 			push_pending_frames(sk);
25461da177e4SLinus Torvalds 			release_sock(sk);
25471da177e4SLinus Torvalds 		}
25481da177e4SLinus Torvalds 		break;
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds 	case UDP_ENCAP:
25511da177e4SLinus Torvalds 		switch (val) {
25521da177e4SLinus Torvalds 		case 0:
2553fd1ac07fSAlexey Dobriyan #ifdef CONFIG_XFRM
25541da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP:
25551da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP_NON_IKE:
2556067b207bSJames Chapman 			up->encap_rcv = xfrm4_udp_encap_rcv;
2557fd1ac07fSAlexey Dobriyan #endif
2558067b207bSJames Chapman 			/* FALLTHROUGH */
2559342f0234SJames Chapman 		case UDP_ENCAP_L2TPINUDP:
25601da177e4SLinus Torvalds 			up->encap_type = val;
256160fb9567SPaolo Abeni 			lock_sock(sk);
256260fb9567SPaolo Abeni 			udp_tunnel_encap_enable(sk->sk_socket);
256360fb9567SPaolo Abeni 			release_sock(sk);
25641da177e4SLinus Torvalds 			break;
25651da177e4SLinus Torvalds 		default:
25661da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
25671da177e4SLinus Torvalds 			break;
25681da177e4SLinus Torvalds 		}
25691da177e4SLinus Torvalds 		break;
25701da177e4SLinus Torvalds 
25711c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
25721c19448cSTom Herbert 		up->no_check6_tx = valbool;
25731c19448cSTom Herbert 		break;
25741c19448cSTom Herbert 
25751c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
25761c19448cSTom Herbert 		up->no_check6_rx = valbool;
25771c19448cSTom Herbert 		break;
25781c19448cSTom Herbert 
2579bec1f6f6SWillem de Bruijn 	case UDP_SEGMENT:
2580bec1f6f6SWillem de Bruijn 		if (val < 0 || val > USHRT_MAX)
2581bec1f6f6SWillem de Bruijn 			return -EINVAL;
2582bec1f6f6SWillem de Bruijn 		up->gso_size = val;
2583bec1f6f6SWillem de Bruijn 		break;
2584bec1f6f6SWillem de Bruijn 
2585e20cf8d3SPaolo Abeni 	case UDP_GRO:
2586e20cf8d3SPaolo Abeni 		lock_sock(sk);
2587e20cf8d3SPaolo Abeni 		if (valbool)
2588e20cf8d3SPaolo Abeni 			udp_tunnel_encap_enable(sk->sk_socket);
2589e20cf8d3SPaolo Abeni 		up->gro_enabled = valbool;
2590e20cf8d3SPaolo Abeni 		release_sock(sk);
2591e20cf8d3SPaolo Abeni 		break;
2592e20cf8d3SPaolo Abeni 
2593ba4e58ecSGerrit Renker 	/*
2594ba4e58ecSGerrit Renker 	 * 	UDP-Lite's partial checksum coverage (RFC 3828).
2595ba4e58ecSGerrit Renker 	 */
2596ba4e58ecSGerrit Renker 	/* The sender sets actual checksum coverage length via this option.
2597ba4e58ecSGerrit Renker 	 * The case coverage > packet length is handled by send module. */
2598ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2599b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2600ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2601ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
2602ba4e58ecSGerrit Renker 			val = 8;
26034be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
26044be929beSAlexey Dobriyan 			val = USHRT_MAX;
2605ba4e58ecSGerrit Renker 		up->pcslen = val;
2606ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_SEND_CC;
2607ba4e58ecSGerrit Renker 		break;
2608ba4e58ecSGerrit Renker 
2609ba4e58ecSGerrit Renker 	/* The receiver specifies a minimum checksum coverage value. To make
2610ba4e58ecSGerrit Renker 	 * sense, this should be set to at least 8 (as done below). If zero is
2611ba4e58ecSGerrit Renker 	 * used, this again means full checksum coverage.                     */
2612ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2613b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2614ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2615ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Avoid silly minimal values.       */
2616ba4e58ecSGerrit Renker 			val = 8;
26174be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
26184be929beSAlexey Dobriyan 			val = USHRT_MAX;
2619ba4e58ecSGerrit Renker 		up->pcrlen = val;
2620ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_RECV_CC;
2621ba4e58ecSGerrit Renker 		break;
2622ba4e58ecSGerrit Renker 
26231da177e4SLinus Torvalds 	default:
26241da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
26251da177e4SLinus Torvalds 		break;
26266516c655SStephen Hemminger 	}
26271da177e4SLinus Torvalds 
26281da177e4SLinus Torvalds 	return err;
26291da177e4SLinus Torvalds }
2630c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt);
26311da177e4SLinus Torvalds 
2632db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname,
2633b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
2634db8dac20SDavid S. Miller {
2635db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2636db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2637db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2638db8dac20SDavid S. Miller 	return ip_setsockopt(sk, level, optname, optval, optlen);
2639db8dac20SDavid S. Miller }
2640db8dac20SDavid S. Miller 
2641db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2642db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname,
2643b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
2644db8dac20SDavid S. Miller {
2645db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2646db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2647db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2648db8dac20SDavid S. Miller 	return compat_ip_setsockopt(sk, level, optname, optval, optlen);
2649db8dac20SDavid S. Miller }
2650db8dac20SDavid S. Miller #endif
2651db8dac20SDavid S. Miller 
26524c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname,
26531da177e4SLinus Torvalds 		       char __user *optval, int __user *optlen)
26541da177e4SLinus Torvalds {
26551da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
26561da177e4SLinus Torvalds 	int val, len;
26571da177e4SLinus Torvalds 
26581da177e4SLinus Torvalds 	if (get_user(len, optlen))
26591da177e4SLinus Torvalds 		return -EFAULT;
26601da177e4SLinus Torvalds 
26611da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	if (len < 0)
26641da177e4SLinus Torvalds 		return -EINVAL;
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 	switch (optname) {
26671da177e4SLinus Torvalds 	case UDP_CORK:
26681da177e4SLinus Torvalds 		val = up->corkflag;
26691da177e4SLinus Torvalds 		break;
26701da177e4SLinus Torvalds 
26711da177e4SLinus Torvalds 	case UDP_ENCAP:
26721da177e4SLinus Torvalds 		val = up->encap_type;
26731da177e4SLinus Torvalds 		break;
26741da177e4SLinus Torvalds 
26751c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
26761c19448cSTom Herbert 		val = up->no_check6_tx;
26771c19448cSTom Herbert 		break;
26781c19448cSTom Herbert 
26791c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
26801c19448cSTom Herbert 		val = up->no_check6_rx;
26811c19448cSTom Herbert 		break;
26821c19448cSTom Herbert 
2683bec1f6f6SWillem de Bruijn 	case UDP_SEGMENT:
2684bec1f6f6SWillem de Bruijn 		val = up->gso_size;
2685bec1f6f6SWillem de Bruijn 		break;
2686bec1f6f6SWillem de Bruijn 
2687ba4e58ecSGerrit Renker 	/* The following two cannot be changed on UDP sockets, the return is
2688ba4e58ecSGerrit Renker 	 * always 0 (which corresponds to the full checksum coverage of UDP). */
2689ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2690ba4e58ecSGerrit Renker 		val = up->pcslen;
2691ba4e58ecSGerrit Renker 		break;
2692ba4e58ecSGerrit Renker 
2693ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2694ba4e58ecSGerrit Renker 		val = up->pcrlen;
2695ba4e58ecSGerrit Renker 		break;
2696ba4e58ecSGerrit Renker 
26971da177e4SLinus Torvalds 	default:
26981da177e4SLinus Torvalds 		return -ENOPROTOOPT;
26996516c655SStephen Hemminger 	}
27001da177e4SLinus Torvalds 
27011da177e4SLinus Torvalds 	if (put_user(len, optlen))
27021da177e4SLinus Torvalds 		return -EFAULT;
27031da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
27041da177e4SLinus Torvalds 		return -EFAULT;
27051da177e4SLinus Torvalds 	return 0;
27061da177e4SLinus Torvalds }
2707c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt);
27081da177e4SLinus Torvalds 
2709db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname,
2710db8dac20SDavid S. Miller 		   char __user *optval, int __user *optlen)
2711db8dac20SDavid S. Miller {
2712db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2713db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2714db8dac20SDavid S. Miller 	return ip_getsockopt(sk, level, optname, optval, optlen);
2715db8dac20SDavid S. Miller }
2716db8dac20SDavid S. Miller 
2717db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2718db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname,
2719db8dac20SDavid S. Miller 				 char __user *optval, int __user *optlen)
2720db8dac20SDavid S. Miller {
2721db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2722db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2723db8dac20SDavid S. Miller 	return compat_ip_getsockopt(sk, level, optname, optval, optlen);
2724db8dac20SDavid S. Miller }
2725db8dac20SDavid S. Miller #endif
27261da177e4SLinus Torvalds /**
27271da177e4SLinus Torvalds  * 	udp_poll - wait for a UDP event.
27281da177e4SLinus Torvalds  *	@file - file struct
27291da177e4SLinus Torvalds  *	@sock - socket
2730a11e1d43SLinus Torvalds  *	@wait - poll table
27311da177e4SLinus Torvalds  *
27321da177e4SLinus Torvalds  *	This is same as datagram poll, except for the special case of
27331da177e4SLinus Torvalds  *	blocking sockets. If application is using a blocking fd
27341da177e4SLinus Torvalds  *	and a packet with checksum error is in the queue;
27351da177e4SLinus Torvalds  *	then it could get return from select indicating data available
27361da177e4SLinus Torvalds  *	but then block when reading it. Add special case code
27371da177e4SLinus Torvalds  *	to work around these arguably broken applications.
27381da177e4SLinus Torvalds  */
2739a11e1d43SLinus Torvalds __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait)
27401da177e4SLinus Torvalds {
2741a11e1d43SLinus Torvalds 	__poll_t mask = datagram_poll(file, sock, wait);
27421da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
27431da177e4SLinus Torvalds 
27443ef7cf57SEric Dumazet 	if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue))
2745a9a08845SLinus Torvalds 		mask |= EPOLLIN | EPOLLRDNORM;
27462276f58aSPaolo Abeni 
27471da177e4SLinus Torvalds 	/* Check for false positives due to checksum errors */
2748a11e1d43SLinus Torvalds 	if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
2749e83c6744SEric Dumazet 	    !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1)
2750a9a08845SLinus Torvalds 		mask &= ~(EPOLLIN | EPOLLRDNORM);
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds 	return mask;
27531da177e4SLinus Torvalds 
27541da177e4SLinus Torvalds }
2755a11e1d43SLinus Torvalds EXPORT_SYMBOL(udp_poll);
27561da177e4SLinus Torvalds 
27575d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err)
27585d77dca8SDavid Ahern {
27595d77dca8SDavid Ahern 	lock_sock(sk);
27605d77dca8SDavid Ahern 
27615d77dca8SDavid Ahern 	sk->sk_err = err;
27625d77dca8SDavid Ahern 	sk->sk_error_report(sk);
2763286c72deSEric Dumazet 	__udp_disconnect(sk, 0);
27645d77dca8SDavid Ahern 
27655d77dca8SDavid Ahern 	release_sock(sk);
27665d77dca8SDavid Ahern 
27675d77dca8SDavid Ahern 	return 0;
27685d77dca8SDavid Ahern }
27695d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort);
27705d77dca8SDavid Ahern 
2771db8dac20SDavid S. Miller struct proto udp_prot = {
2772db8dac20SDavid S. Miller 	.name			= "UDP",
2773db8dac20SDavid S. Miller 	.owner			= THIS_MODULE,
2774db8dac20SDavid S. Miller 	.close			= udp_lib_close,
2775d74bad4eSAndrey Ignatov 	.pre_connect		= udp_pre_connect,
2776db8dac20SDavid S. Miller 	.connect		= ip4_datagram_connect,
2777db8dac20SDavid S. Miller 	.disconnect		= udp_disconnect,
2778db8dac20SDavid S. Miller 	.ioctl			= udp_ioctl,
2779850cbaddSPaolo Abeni 	.init			= udp_init_sock,
2780db8dac20SDavid S. Miller 	.destroy		= udp_destroy_sock,
2781db8dac20SDavid S. Miller 	.setsockopt		= udp_setsockopt,
2782db8dac20SDavid S. Miller 	.getsockopt		= udp_getsockopt,
2783db8dac20SDavid S. Miller 	.sendmsg		= udp_sendmsg,
2784db8dac20SDavid S. Miller 	.recvmsg		= udp_recvmsg,
2785db8dac20SDavid S. Miller 	.sendpage		= udp_sendpage,
27868141ed9fSSteffen Klassert 	.release_cb		= ip4_datagram_release_cb,
2787db8dac20SDavid S. Miller 	.hash			= udp_lib_hash,
2788db8dac20SDavid S. Miller 	.unhash			= udp_lib_unhash,
2789719f8358SEric Dumazet 	.rehash			= udp_v4_rehash,
2790db8dac20SDavid S. Miller 	.get_port		= udp_v4_get_port,
2791db8dac20SDavid S. Miller 	.memory_allocated	= &udp_memory_allocated,
2792db8dac20SDavid S. Miller 	.sysctl_mem		= sysctl_udp_mem,
27931e802951STonghao Zhang 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_udp_wmem_min),
27941e802951STonghao Zhang 	.sysctl_rmem_offset	= offsetof(struct net, ipv4.sysctl_udp_rmem_min),
2795db8dac20SDavid S. Miller 	.obj_size		= sizeof(struct udp_sock),
2796645ca708SEric Dumazet 	.h.udp_table		= &udp_table,
2797db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2798db8dac20SDavid S. Miller 	.compat_setsockopt	= compat_udp_setsockopt,
2799db8dac20SDavid S. Miller 	.compat_getsockopt	= compat_udp_getsockopt,
2800db8dac20SDavid S. Miller #endif
28015d77dca8SDavid Ahern 	.diag_destroy		= udp_abort,
2802db8dac20SDavid S. Miller };
2803c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot);
28041da177e4SLinus Torvalds 
28051da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
28061da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
28071da177e4SLinus Torvalds 
2808645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start)
28091da177e4SLinus Torvalds {
28101da177e4SLinus Torvalds 	struct sock *sk;
2811a3d2599bSChristoph Hellwig 	struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
28121da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
28136f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
28141da177e4SLinus Torvalds 
2815a3d2599bSChristoph Hellwig 	for (state->bucket = start; state->bucket <= afinfo->udp_table->mask;
2816f86dcc5aSEric Dumazet 	     ++state->bucket) {
2817a3d2599bSChristoph Hellwig 		struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket];
2818f86dcc5aSEric Dumazet 
2819ca065d0cSEric Dumazet 		if (hlist_empty(&hslot->head))
2820f86dcc5aSEric Dumazet 			continue;
2821f86dcc5aSEric Dumazet 
2822645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
2823ca065d0cSEric Dumazet 		sk_for_each(sk, &hslot->head) {
2824878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
2825a91275efSDaniel Lezcano 				continue;
2826a3d2599bSChristoph Hellwig 			if (sk->sk_family == afinfo->family)
28271da177e4SLinus Torvalds 				goto found;
28281da177e4SLinus Torvalds 		}
2829645ca708SEric Dumazet 		spin_unlock_bh(&hslot->lock);
28301da177e4SLinus Torvalds 	}
28311da177e4SLinus Torvalds 	sk = NULL;
28321da177e4SLinus Torvalds found:
28331da177e4SLinus Torvalds 	return sk;
28341da177e4SLinus Torvalds }
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
28371da177e4SLinus Torvalds {
2838a3d2599bSChristoph Hellwig 	struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
28391da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
28406f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds 	do {
2843ca065d0cSEric Dumazet 		sk = sk_next(sk);
2844a3d2599bSChristoph Hellwig 	} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != afinfo->family));
28451da177e4SLinus Torvalds 
2846645ca708SEric Dumazet 	if (!sk) {
2847a3d2599bSChristoph Hellwig 		if (state->bucket <= afinfo->udp_table->mask)
2848a3d2599bSChristoph Hellwig 			spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock);
2849645ca708SEric Dumazet 		return udp_get_first(seq, state->bucket + 1);
28501da177e4SLinus Torvalds 	}
28511da177e4SLinus Torvalds 	return sk;
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
28541da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
28551da177e4SLinus Torvalds {
2856645ca708SEric Dumazet 	struct sock *sk = udp_get_first(seq, 0);
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds 	if (sk)
28591da177e4SLinus Torvalds 		while (pos && (sk = udp_get_next(seq, sk)) != NULL)
28601da177e4SLinus Torvalds 			--pos;
28611da177e4SLinus Torvalds 	return pos ? NULL : sk;
28621da177e4SLinus Torvalds }
28631da177e4SLinus Torvalds 
2864a3d2599bSChristoph Hellwig void *udp_seq_start(struct seq_file *seq, loff_t *pos)
28651da177e4SLinus Torvalds {
286630842f29SVitaly Mayatskikh 	struct udp_iter_state *state = seq->private;
2867f86dcc5aSEric Dumazet 	state->bucket = MAX_UDP_PORTS;
286830842f29SVitaly Mayatskikh 
2869b50660f1SYOSHIFUJI Hideaki 	return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
28701da177e4SLinus Torvalds }
2871a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_start);
28721da177e4SLinus Torvalds 
2873a3d2599bSChristoph Hellwig void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
28741da177e4SLinus Torvalds {
28751da177e4SLinus Torvalds 	struct sock *sk;
28761da177e4SLinus Torvalds 
2877b50660f1SYOSHIFUJI Hideaki 	if (v == SEQ_START_TOKEN)
28781da177e4SLinus Torvalds 		sk = udp_get_idx(seq, 0);
28791da177e4SLinus Torvalds 	else
28801da177e4SLinus Torvalds 		sk = udp_get_next(seq, v);
28811da177e4SLinus Torvalds 
28821da177e4SLinus Torvalds 	++*pos;
28831da177e4SLinus Torvalds 	return sk;
28841da177e4SLinus Torvalds }
2885a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_next);
28861da177e4SLinus Torvalds 
2887a3d2599bSChristoph Hellwig void udp_seq_stop(struct seq_file *seq, void *v)
28881da177e4SLinus Torvalds {
2889a3d2599bSChristoph Hellwig 	struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
2890645ca708SEric Dumazet 	struct udp_iter_state *state = seq->private;
2891645ca708SEric Dumazet 
2892a3d2599bSChristoph Hellwig 	if (state->bucket <= afinfo->udp_table->mask)
2893a3d2599bSChristoph Hellwig 		spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock);
28941da177e4SLinus Torvalds }
2895a3d2599bSChristoph Hellwig EXPORT_SYMBOL(udp_seq_stop);
2896db8dac20SDavid S. Miller 
2897db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
28985e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2899652586dfSTetsuo Handa 		int bucket)
2900db8dac20SDavid S. Miller {
2901db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
2902c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2903c720c7e8SEric Dumazet 	__be32 src  = inet->inet_rcv_saddr;
2904c720c7e8SEric Dumazet 	__u16 destp	  = ntohs(inet->inet_dport);
2905c720c7e8SEric Dumazet 	__u16 srcp	  = ntohs(inet->inet_sport);
2906db8dac20SDavid S. Miller 
2907f86dcc5aSEric Dumazet 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2908ea9a0379SPatrick Talbert 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u",
2909db8dac20SDavid S. Miller 		bucket, src, srcp, dest, destp, sp->sk_state,
291031e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
29116c206b20SPaolo Abeni 		udp_rqueue_get(sp),
2912a7cb5a49SEric W. Biederman 		0, 0L, 0,
2913a7cb5a49SEric W. Biederman 		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
2914a7cb5a49SEric W. Biederman 		0, sock_i_ino(sp),
291541c6d650SReshetova, Elena 		refcount_read(&sp->sk_refcnt), sp,
2916652586dfSTetsuo Handa 		atomic_read(&sp->sk_drops));
2917db8dac20SDavid S. Miller }
2918db8dac20SDavid S. Miller 
2919db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v)
2920db8dac20SDavid S. Miller {
2921652586dfSTetsuo Handa 	seq_setwidth(seq, 127);
2922db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
2923652586dfSTetsuo Handa 		seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
2924db8dac20SDavid S. Miller 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
2925cb61cb9bSEric Dumazet 			   "inode ref pointer drops");
2926db8dac20SDavid S. Miller 	else {
2927db8dac20SDavid S. Miller 		struct udp_iter_state *state = seq->private;
2928db8dac20SDavid S. Miller 
2929652586dfSTetsuo Handa 		udp4_format_sock(v, seq, state->bucket);
2930db8dac20SDavid S. Miller 	}
2931652586dfSTetsuo Handa 	seq_pad(seq, '\n');
2932db8dac20SDavid S. Miller 	return 0;
2933db8dac20SDavid S. Miller }
2934db8dac20SDavid S. Miller 
2935c3506372SChristoph Hellwig const struct seq_operations udp_seq_ops = {
2936a3d2599bSChristoph Hellwig 	.start		= udp_seq_start,
2937a3d2599bSChristoph Hellwig 	.next		= udp_seq_next,
2938a3d2599bSChristoph Hellwig 	.stop		= udp_seq_stop,
2939a3d2599bSChristoph Hellwig 	.show		= udp4_seq_show,
2940a3d2599bSChristoph Hellwig };
2941c3506372SChristoph Hellwig EXPORT_SYMBOL(udp_seq_ops);
294273cb88ecSArjan van de Ven 
2943db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = {
2944db8dac20SDavid S. Miller 	.family		= AF_INET,
2945645ca708SEric Dumazet 	.udp_table	= &udp_table,
2946db8dac20SDavid S. Miller };
2947db8dac20SDavid S. Miller 
29482c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net)
294915439febSPavel Emelyanov {
2950c3506372SChristoph Hellwig 	if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops,
2951c3506372SChristoph Hellwig 			sizeof(struct udp_iter_state), &udp4_seq_afinfo))
2952a3d2599bSChristoph Hellwig 		return -ENOMEM;
2953a3d2599bSChristoph Hellwig 	return 0;
295415439febSPavel Emelyanov }
295515439febSPavel Emelyanov 
29562c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net)
295715439febSPavel Emelyanov {
2958a3d2599bSChristoph Hellwig 	remove_proc_entry("udp", net->proc_net);
295915439febSPavel Emelyanov }
296015439febSPavel Emelyanov 
296115439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = {
296215439febSPavel Emelyanov 	.init = udp4_proc_init_net,
296315439febSPavel Emelyanov 	.exit = udp4_proc_exit_net,
296415439febSPavel Emelyanov };
296515439febSPavel Emelyanov 
2966db8dac20SDavid S. Miller int __init udp4_proc_init(void)
2967db8dac20SDavid S. Miller {
296815439febSPavel Emelyanov 	return register_pernet_subsys(&udp4_net_ops);
2969db8dac20SDavid S. Miller }
2970db8dac20SDavid S. Miller 
2971db8dac20SDavid S. Miller void udp4_proc_exit(void)
2972db8dac20SDavid S. Miller {
297315439febSPavel Emelyanov 	unregister_pernet_subsys(&udp4_net_ops);
2974db8dac20SDavid S. Miller }
29751da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
29761da177e4SLinus Torvalds 
2977f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries;
2978f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str)
2979645ca708SEric Dumazet {
2980413c27d8SEldad Zack 	ssize_t ret;
2981413c27d8SEldad Zack 
2982f86dcc5aSEric Dumazet 	if (!str)
2983f86dcc5aSEric Dumazet 		return 0;
2984413c27d8SEldad Zack 
2985413c27d8SEldad Zack 	ret = kstrtoul(str, 0, &uhash_entries);
2986413c27d8SEldad Zack 	if (ret)
2987413c27d8SEldad Zack 		return 0;
2988413c27d8SEldad Zack 
2989f86dcc5aSEric Dumazet 	if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
2990f86dcc5aSEric Dumazet 		uhash_entries = UDP_HTABLE_SIZE_MIN;
2991f86dcc5aSEric Dumazet 	return 1;
2992f86dcc5aSEric Dumazet }
2993f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries);
2994645ca708SEric Dumazet 
2995f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name)
2996f86dcc5aSEric Dumazet {
2997f86dcc5aSEric Dumazet 	unsigned int i;
2998f86dcc5aSEric Dumazet 
2999f86dcc5aSEric Dumazet 	table->hash = alloc_large_system_hash(name,
3000512615b6SEric Dumazet 					      2 * sizeof(struct udp_hslot),
3001f86dcc5aSEric Dumazet 					      uhash_entries,
3002f86dcc5aSEric Dumazet 					      21, /* one slot per 2 MB */
3003f86dcc5aSEric Dumazet 					      0,
3004f86dcc5aSEric Dumazet 					      &table->log,
3005f86dcc5aSEric Dumazet 					      &table->mask,
300631fe62b9STim Bird 					      UDP_HTABLE_SIZE_MIN,
3007f86dcc5aSEric Dumazet 					      64 * 1024);
300831fe62b9STim Bird 
3009512615b6SEric Dumazet 	table->hash2 = table->hash + (table->mask + 1);
3010f86dcc5aSEric Dumazet 	for (i = 0; i <= table->mask; i++) {
3011ca065d0cSEric Dumazet 		INIT_HLIST_HEAD(&table->hash[i].head);
3012fdcc8aa9SEric Dumazet 		table->hash[i].count = 0;
3013645ca708SEric Dumazet 		spin_lock_init(&table->hash[i].lock);
3014645ca708SEric Dumazet 	}
3015512615b6SEric Dumazet 	for (i = 0; i <= table->mask; i++) {
3016ca065d0cSEric Dumazet 		INIT_HLIST_HEAD(&table->hash2[i].head);
3017512615b6SEric Dumazet 		table->hash2[i].count = 0;
3018512615b6SEric Dumazet 		spin_lock_init(&table->hash2[i].lock);
3019512615b6SEric Dumazet 	}
3020645ca708SEric Dumazet }
3021645ca708SEric Dumazet 
3022723b8e46STom Herbert u32 udp_flow_hashrnd(void)
3023723b8e46STom Herbert {
3024723b8e46STom Herbert 	static u32 hashrnd __read_mostly;
3025723b8e46STom Herbert 
3026723b8e46STom Herbert 	net_get_random_once(&hashrnd, sizeof(hashrnd));
3027723b8e46STom Herbert 
3028723b8e46STom Herbert 	return hashrnd;
3029723b8e46STom Herbert }
3030723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd);
3031723b8e46STom Herbert 
30321e802951STonghao Zhang static void __udp_sysctl_init(struct net *net)
30331e802951STonghao Zhang {
30341e802951STonghao Zhang 	net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM;
30351e802951STonghao Zhang 	net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM;
30361e802951STonghao Zhang 
30371e802951STonghao Zhang #ifdef CONFIG_NET_L3_MASTER_DEV
30381e802951STonghao Zhang 	net->ipv4.sysctl_udp_l3mdev_accept = 0;
30391e802951STonghao Zhang #endif
30401e802951STonghao Zhang }
30411e802951STonghao Zhang 
30421e802951STonghao Zhang static int __net_init udp_sysctl_init(struct net *net)
30431e802951STonghao Zhang {
30441e802951STonghao Zhang 	__udp_sysctl_init(net);
30451e802951STonghao Zhang 	return 0;
30461e802951STonghao Zhang }
30471e802951STonghao Zhang 
30481e802951STonghao Zhang static struct pernet_operations __net_initdata udp_sysctl_ops = {
30491e802951STonghao Zhang 	.init	= udp_sysctl_init,
30501e802951STonghao Zhang };
30511e802951STonghao Zhang 
305295766fffSHideo Aoki void __init udp_init(void)
305395766fffSHideo Aoki {
3054f03d78dbSEric Dumazet 	unsigned long limit;
30554b272750SEric Dumazet 	unsigned int i;
305695766fffSHideo Aoki 
3057f86dcc5aSEric Dumazet 	udp_table_init(&udp_table, "UDP");
3058f03d78dbSEric Dumazet 	limit = nr_free_buffer_pages() / 8;
305995766fffSHideo Aoki 	limit = max(limit, 128UL);
306095766fffSHideo Aoki 	sysctl_udp_mem[0] = limit / 4 * 3;
306195766fffSHideo Aoki 	sysctl_udp_mem[1] = limit;
306295766fffSHideo Aoki 	sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
306395766fffSHideo Aoki 
30641e802951STonghao Zhang 	__udp_sysctl_init(&init_net);
30654b272750SEric Dumazet 
30664b272750SEric Dumazet 	/* 16 spinlocks per cpu */
30674b272750SEric Dumazet 	udp_busylocks_log = ilog2(nr_cpu_ids) + 4;
30684b272750SEric Dumazet 	udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log,
30694b272750SEric Dumazet 				GFP_KERNEL);
30704b272750SEric Dumazet 	if (!udp_busylocks)
30714b272750SEric Dumazet 		panic("UDP: failed to alloc udp_busylocks\n");
30724b272750SEric Dumazet 	for (i = 0; i < (1U << udp_busylocks_log); i++)
30734b272750SEric Dumazet 		spin_lock_init(udp_busylocks + i);
30741e802951STonghao Zhang 
30751e802951STonghao Zhang 	if (register_pernet_subsys(&udp_sysctl_ops))
30761e802951STonghao Zhang 		panic("UDP: failed to init sysctl parameters.\n");
307795766fffSHideo Aoki }
3078