xref: /linux/net/ipv4/udp.c (revision 60d9b03141243589dacd3136f3fcb4e6976df954)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		The User Datagram Protocol (UDP).
71da177e4SLinus Torvalds  *
802c30a84SJesper Juhl  * Authors:	Ross Biro
91da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
101da177e4SLinus Torvalds  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
11113aa838SAlan Cox  *		Alan Cox, <alan@lxorguk.ukuu.org.uk>
121da177e4SLinus Torvalds  *		Hirokazu Takahashi, <taka@valinux.co.jp>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * Fixes:
151da177e4SLinus Torvalds  *		Alan Cox	:	verify_area() calls
161da177e4SLinus Torvalds  *		Alan Cox	: 	stopped close while in use off icmp
171da177e4SLinus Torvalds  *					messages. Not a fix but a botch that
181da177e4SLinus Torvalds  *					for udp at least is 'valid'.
191da177e4SLinus Torvalds  *		Alan Cox	:	Fixed icmp handling properly
201da177e4SLinus Torvalds  *		Alan Cox	: 	Correct error for oversized datagrams
211da177e4SLinus Torvalds  *		Alan Cox	:	Tidied select() semantics.
221da177e4SLinus Torvalds  *		Alan Cox	:	udp_err() fixed properly, also now
231da177e4SLinus Torvalds  *					select and read wake correctly on errors
241da177e4SLinus Torvalds  *		Alan Cox	:	udp_send verify_area moved to avoid mem leak
251da177e4SLinus Torvalds  *		Alan Cox	:	UDP can count its memory
261da177e4SLinus Torvalds  *		Alan Cox	:	send to an unknown connection causes
271da177e4SLinus Torvalds  *					an ECONNREFUSED off the icmp, but
281da177e4SLinus Torvalds  *					does NOT close.
291da177e4SLinus Torvalds  *		Alan Cox	:	Switched to new sk_buff handlers. No more backlog!
301da177e4SLinus Torvalds  *		Alan Cox	:	Using generic datagram code. Even smaller and the PEEK
311da177e4SLinus Torvalds  *					bug no longer crashes it.
321da177e4SLinus Torvalds  *		Fred Van Kempen	: 	Net2e support for sk->broadcast.
331da177e4SLinus Torvalds  *		Alan Cox	:	Uses skb_free_datagram
341da177e4SLinus Torvalds  *		Alan Cox	:	Added get/set sockopt support.
351da177e4SLinus Torvalds  *		Alan Cox	:	Broadcasting without option set returns EACCES.
361da177e4SLinus Torvalds  *		Alan Cox	:	No wakeup calls. Instead we now use the callbacks.
371da177e4SLinus Torvalds  *		Alan Cox	:	Use ip_tos and ip_ttl
381da177e4SLinus Torvalds  *		Alan Cox	:	SNMP Mibs
391da177e4SLinus Torvalds  *		Alan Cox	:	MSG_DONTROUTE, and 0.0.0.0 support.
401da177e4SLinus Torvalds  *		Matt Dillon	:	UDP length checks.
411da177e4SLinus Torvalds  *		Alan Cox	:	Smarter af_inet used properly.
421da177e4SLinus Torvalds  *		Alan Cox	:	Use new kernel side addressing.
431da177e4SLinus Torvalds  *		Alan Cox	:	Incorrect return on truncated datagram receive.
441da177e4SLinus Torvalds  *	Arnt Gulbrandsen 	:	New udp_send and stuff
451da177e4SLinus Torvalds  *		Alan Cox	:	Cache last socket
461da177e4SLinus Torvalds  *		Alan Cox	:	Route cache
471da177e4SLinus Torvalds  *		Jon Peatfield	:	Minor efficiency fix to sendto().
481da177e4SLinus Torvalds  *		Mike Shaver	:	RFC1122 checks.
491da177e4SLinus Torvalds  *		Alan Cox	:	Nonblocking error fix.
501da177e4SLinus Torvalds  *	Willy Konynenberg	:	Transparent proxying support.
511da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
521da177e4SLinus Torvalds  *		David S. Miller	:	New socket lookup architecture.
531da177e4SLinus Torvalds  *					Last socket cache retained as it
541da177e4SLinus Torvalds  *					does have a high hit rate.
551da177e4SLinus Torvalds  *		Olaf Kirch	:	Don't linearise iovec on sendmsg.
561da177e4SLinus Torvalds  *		Andi Kleen	:	Some cleanups, cache destination entry
571da177e4SLinus Torvalds  *					for connect.
581da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
591da177e4SLinus Torvalds  *		Melvin Smith	:	Check msg_name not msg_namelen in sendto(),
601da177e4SLinus Torvalds  *					return ENOTCONN for unconnected sockets (POSIX)
611da177e4SLinus Torvalds  *		Janos Farkas	:	don't deliver multi/broadcasts to a different
621da177e4SLinus Torvalds  *					bound-to-device socket
631da177e4SLinus Torvalds  *	Hirokazu Takahashi	:	HW checksumming for outgoing UDP
641da177e4SLinus Torvalds  *					datagrams.
651da177e4SLinus Torvalds  *	Hirokazu Takahashi	:	sendfile() on UDP works now.
661da177e4SLinus Torvalds  *		Arnaldo C. Melo :	convert /proc/net/udp to seq_file
671da177e4SLinus Torvalds  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
681da177e4SLinus Torvalds  *	Alexey Kuznetsov:		allow both IPv4 and IPv6 sockets to bind
691da177e4SLinus Torvalds  *					a single port at the same time.
701da177e4SLinus Torvalds  *	Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
71342f0234SJames Chapman  *	James Chapman		:	Add L2TP encapsulation type.
721da177e4SLinus Torvalds  *
731da177e4SLinus Torvalds  *
741da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
751da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
761da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
771da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
781da177e4SLinus Torvalds  */
791da177e4SLinus Torvalds 
80afd46503SJoe Perches #define pr_fmt(fmt) "UDP: " fmt
81afd46503SJoe Perches 
827c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
831da177e4SLinus Torvalds #include <asm/ioctls.h>
8495766fffSHideo Aoki #include <linux/bootmem.h>
858203efb3SEric Dumazet #include <linux/highmem.h>
868203efb3SEric Dumazet #include <linux/swap.h>
871da177e4SLinus Torvalds #include <linux/types.h>
881da177e4SLinus Torvalds #include <linux/fcntl.h>
891da177e4SLinus Torvalds #include <linux/module.h>
901da177e4SLinus Torvalds #include <linux/socket.h>
911da177e4SLinus Torvalds #include <linux/sockios.h>
9214c85021SArnaldo Carvalho de Melo #include <linux/igmp.h>
936e540309SShawn Bohrer #include <linux/inetdevice.h>
941da177e4SLinus Torvalds #include <linux/in.h>
951da177e4SLinus Torvalds #include <linux/errno.h>
961da177e4SLinus Torvalds #include <linux/timer.h>
971da177e4SLinus Torvalds #include <linux/mm.h>
981da177e4SLinus Torvalds #include <linux/inet.h>
991da177e4SLinus Torvalds #include <linux/netdevice.h>
1005a0e3ad6STejun Heo #include <linux/slab.h>
101c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
1021da177e4SLinus Torvalds #include <linux/skbuff.h>
1031da177e4SLinus Torvalds #include <linux/proc_fs.h>
1041da177e4SLinus Torvalds #include <linux/seq_file.h>
105457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1061da177e4SLinus Torvalds #include <net/icmp.h>
107421b3885SShawn Bohrer #include <net/inet_hashtables.h>
1081da177e4SLinus Torvalds #include <net/route.h>
1091da177e4SLinus Torvalds #include <net/checksum.h>
1101da177e4SLinus Torvalds #include <net/xfrm.h>
111296f7ea7SSatoru Moriya #include <trace/events/udp.h>
112447167bfSEric Dumazet #include <linux/static_key.h>
11322911fc5SEric Dumazet #include <trace/events/skb.h>
114076bb0c8SEliezer Tamir #include <net/busy_poll.h>
115ba4e58ecSGerrit Renker #include "udp_impl.h"
116e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
117217375a0SEric Dumazet #include <net/addrconf.h>
1181da177e4SLinus Torvalds 
119f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly;
120645ca708SEric Dumazet EXPORT_SYMBOL(udp_table);
1211da177e4SLinus Torvalds 
1228d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly;
12395766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem);
124c482c568SEric Dumazet 
125c482c568SEric Dumazet int sysctl_udp_rmem_min __read_mostly;
12695766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min);
127c482c568SEric Dumazet 
128c482c568SEric Dumazet int sysctl_udp_wmem_min __read_mostly;
12995766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min);
13095766fffSHideo Aoki 
1318d987e5cSEric Dumazet atomic_long_t udp_memory_allocated;
13295766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated);
13395766fffSHideo Aoki 
134f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536
135f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
13698322f22SEric Dumazet 
13763a6fff3SRobert Shearman /* IPCB reference means this can not be used from early demux */
13863a6fff3SRobert Shearman static bool udp_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
13963a6fff3SRobert Shearman {
14063a6fff3SRobert Shearman #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
14163a6fff3SRobert Shearman 	if (!net->ipv4.sysctl_udp_l3mdev_accept &&
14263a6fff3SRobert Shearman 	    skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
14363a6fff3SRobert Shearman 		return true;
14463a6fff3SRobert Shearman #endif
14563a6fff3SRobert Shearman 	return false;
14663a6fff3SRobert Shearman }
14763a6fff3SRobert Shearman 
148f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num,
149645ca708SEric Dumazet 			       const struct udp_hslot *hslot,
15098322f22SEric Dumazet 			       unsigned long *bitmap,
151fe38d2a1SJosef Bacik 			       struct sock *sk, unsigned int log)
15225030a7fSGerrit Renker {
153f24d43c0SEric Dumazet 	struct sock *sk2;
154ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
15525030a7fSGerrit Renker 
156ca065d0cSEric Dumazet 	sk_for_each(sk2, &hslot->head) {
157f24d43c0SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
158f24d43c0SEric Dumazet 		    sk2 != sk &&
159d4cada4aSEric Dumazet 		    (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
160f24d43c0SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1619d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1629d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
163fe38d2a1SJosef Bacik 		    inet_rcv_saddr_equal(sk, sk2, true)) {
164df560056SEric Garver 			if (sk2->sk_reuseport && sk->sk_reuseport &&
165df560056SEric Garver 			    !rcu_access_pointer(sk->sk_reuseport_cb) &&
166df560056SEric Garver 			    uid_eq(uid, sock_i_uid(sk2))) {
167df560056SEric Garver 				if (!bitmap)
168df560056SEric Garver 					return 0;
169df560056SEric Garver 			} else {
1704243cdc2SJoe Perches 				if (!bitmap)
171fc038410SDavid S. Miller 					return 1;
172df560056SEric Garver 				__set_bit(udp_sk(sk2)->udp_port_hash >> log,
173df560056SEric Garver 					  bitmap);
174df560056SEric Garver 			}
1754243cdc2SJoe Perches 		}
17698322f22SEric Dumazet 	}
17725030a7fSGerrit Renker 	return 0;
17825030a7fSGerrit Renker }
17925030a7fSGerrit Renker 
18030fff923SEric Dumazet /*
18130fff923SEric Dumazet  * Note: we still hold spinlock of primary hash chain, so no other writer
18230fff923SEric Dumazet  * can insert/delete a socket with local_port == num
18330fff923SEric Dumazet  */
18430fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num,
18530fff923SEric Dumazet 				struct udp_hslot *hslot2,
186fe38d2a1SJosef Bacik 				struct sock *sk)
18730fff923SEric Dumazet {
18830fff923SEric Dumazet 	struct sock *sk2;
189ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
19030fff923SEric Dumazet 	int res = 0;
19130fff923SEric Dumazet 
19230fff923SEric Dumazet 	spin_lock(&hslot2->lock);
193ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry(sk2, &hslot2->head) {
19430fff923SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
19530fff923SEric Dumazet 		    sk2 != sk &&
19630fff923SEric Dumazet 		    (udp_sk(sk2)->udp_port_hash == num) &&
19730fff923SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1989d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1999d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
200fe38d2a1SJosef Bacik 		    inet_rcv_saddr_equal(sk, sk2, true)) {
201df560056SEric Garver 			if (sk2->sk_reuseport && sk->sk_reuseport &&
202df560056SEric Garver 			    !rcu_access_pointer(sk->sk_reuseport_cb) &&
203df560056SEric Garver 			    uid_eq(uid, sock_i_uid(sk2))) {
204df560056SEric Garver 				res = 0;
205df560056SEric Garver 			} else {
20630fff923SEric Dumazet 				res = 1;
207df560056SEric Garver 			}
20830fff923SEric Dumazet 			break;
20930fff923SEric Dumazet 		}
2104243cdc2SJoe Perches 	}
21130fff923SEric Dumazet 	spin_unlock(&hslot2->lock);
21230fff923SEric Dumazet 	return res;
21330fff923SEric Dumazet }
21430fff923SEric Dumazet 
215fe38d2a1SJosef Bacik static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot)
216e32ea7e7SCraig Gallek {
217e32ea7e7SCraig Gallek 	struct net *net = sock_net(sk);
218e32ea7e7SCraig Gallek 	kuid_t uid = sock_i_uid(sk);
219e32ea7e7SCraig Gallek 	struct sock *sk2;
220e32ea7e7SCraig Gallek 
221ca065d0cSEric Dumazet 	sk_for_each(sk2, &hslot->head) {
222e32ea7e7SCraig Gallek 		if (net_eq(sock_net(sk2), net) &&
223e32ea7e7SCraig Gallek 		    sk2 != sk &&
224e32ea7e7SCraig Gallek 		    sk2->sk_family == sk->sk_family &&
225e32ea7e7SCraig Gallek 		    ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
226e32ea7e7SCraig Gallek 		    (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) &&
227e32ea7e7SCraig Gallek 		    (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
228e32ea7e7SCraig Gallek 		    sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
229fe38d2a1SJosef Bacik 		    inet_rcv_saddr_equal(sk, sk2, false)) {
230e32ea7e7SCraig Gallek 			return reuseport_add_sock(sk, sk2);
231e32ea7e7SCraig Gallek 		}
232e32ea7e7SCraig Gallek 	}
233e32ea7e7SCraig Gallek 
234e32ea7e7SCraig Gallek 	/* Initial allocation may have already happened via setsockopt */
235e32ea7e7SCraig Gallek 	if (!rcu_access_pointer(sk->sk_reuseport_cb))
236e32ea7e7SCraig Gallek 		return reuseport_alloc(sk);
237e32ea7e7SCraig Gallek 	return 0;
238e32ea7e7SCraig Gallek }
239e32ea7e7SCraig Gallek 
24025030a7fSGerrit Renker /**
2416ba5a3c5SPavel Emelyanov  *  udp_lib_get_port  -  UDP/-Lite port lookup for IPv4 and IPv6
24225030a7fSGerrit Renker  *
24325030a7fSGerrit Renker  *  @sk:          socket struct in question
24425030a7fSGerrit Renker  *  @snum:        port number to look up
24525985edcSLucas De Marchi  *  @hash2_nulladdr: AF-dependent hash value in secondary hash chains,
24630fff923SEric Dumazet  *                   with NULL address
24725030a7fSGerrit Renker  */
2486ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum,
24930fff923SEric Dumazet 		     unsigned int hash2_nulladdr)
2501da177e4SLinus Torvalds {
251512615b6SEric Dumazet 	struct udp_hslot *hslot, *hslot2;
252645ca708SEric Dumazet 	struct udp_table *udptable = sk->sk_prot->h.udp_table;
25325030a7fSGerrit Renker 	int    error = 1;
2543b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2551da177e4SLinus Torvalds 
25632c1da70SStephen Hemminger 	if (!snum) {
2579088c560SEric Dumazet 		int low, high, remaining;
25895c96174SEric Dumazet 		unsigned int rand;
25998322f22SEric Dumazet 		unsigned short first, last;
26098322f22SEric Dumazet 		DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
2611da177e4SLinus Torvalds 
2620bbf87d8SEric W. Biederman 		inet_get_local_port_range(net, &low, &high);
263a25de534SAnton Arapov 		remaining = (high - low) + 1;
264227b60f5SStephen Hemminger 
26563862b5bSAruna-Hewapathirane 		rand = prandom_u32();
2668fc54f68SDaniel Borkmann 		first = reciprocal_scale(rand, remaining) + low;
26798322f22SEric Dumazet 		/*
26898322f22SEric Dumazet 		 * force rand to be an odd multiple of UDP_HTABLE_SIZE
26998322f22SEric Dumazet 		 */
270f86dcc5aSEric Dumazet 		rand = (rand | 1) * (udptable->mask + 1);
2715781b235SEric Dumazet 		last = first + udptable->mask + 1;
2725781b235SEric Dumazet 		do {
273f86dcc5aSEric Dumazet 			hslot = udp_hashslot(udptable, net, first);
27498322f22SEric Dumazet 			bitmap_zero(bitmap, PORTS_PER_CHAIN);
275645ca708SEric Dumazet 			spin_lock_bh(&hslot->lock);
27698322f22SEric Dumazet 			udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
277fe38d2a1SJosef Bacik 					    udptable->log);
27898322f22SEric Dumazet 
27998322f22SEric Dumazet 			snum = first;
28098322f22SEric Dumazet 			/*
28198322f22SEric Dumazet 			 * Iterate on all possible values of snum for this hash.
28298322f22SEric Dumazet 			 * Using steps of an odd multiple of UDP_HTABLE_SIZE
28398322f22SEric Dumazet 			 * give us randomization and full range coverage.
28498322f22SEric Dumazet 			 */
2859088c560SEric Dumazet 			do {
28698322f22SEric Dumazet 				if (low <= snum && snum <= high &&
287e3826f1eSAmerigo Wang 				    !test_bit(snum >> udptable->log, bitmap) &&
288122ff243SWANG Cong 				    !inet_is_local_reserved_port(net, snum))
28998322f22SEric Dumazet 					goto found;
29098322f22SEric Dumazet 				snum += rand;
29198322f22SEric Dumazet 			} while (snum != first);
29298322f22SEric Dumazet 			spin_unlock_bh(&hslot->lock);
293df560056SEric Garver 			cond_resched();
2945781b235SEric Dumazet 		} while (++first != last);
29598322f22SEric Dumazet 		goto fail;
296645ca708SEric Dumazet 	} else {
297f86dcc5aSEric Dumazet 		hslot = udp_hashslot(udptable, net, snum);
298645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
29930fff923SEric Dumazet 		if (hslot->count > 10) {
30030fff923SEric Dumazet 			int exist;
30130fff923SEric Dumazet 			unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
30230fff923SEric Dumazet 
30330fff923SEric Dumazet 			slot2          &= udptable->mask;
30430fff923SEric Dumazet 			hash2_nulladdr &= udptable->mask;
30530fff923SEric Dumazet 
30630fff923SEric Dumazet 			hslot2 = udp_hashslot2(udptable, slot2);
30730fff923SEric Dumazet 			if (hslot->count < hslot2->count)
30830fff923SEric Dumazet 				goto scan_primary_hash;
30930fff923SEric Dumazet 
310fe38d2a1SJosef Bacik 			exist = udp_lib_lport_inuse2(net, snum, hslot2, sk);
31130fff923SEric Dumazet 			if (!exist && (hash2_nulladdr != slot2)) {
31230fff923SEric Dumazet 				hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
31330fff923SEric Dumazet 				exist = udp_lib_lport_inuse2(net, snum, hslot2,
314fe38d2a1SJosef Bacik 							     sk);
31530fff923SEric Dumazet 			}
31630fff923SEric Dumazet 			if (exist)
31730fff923SEric Dumazet 				goto fail_unlock;
31830fff923SEric Dumazet 			else
31930fff923SEric Dumazet 				goto found;
32030fff923SEric Dumazet 		}
32130fff923SEric Dumazet scan_primary_hash:
322fe38d2a1SJosef Bacik 		if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0))
323645ca708SEric Dumazet 			goto fail_unlock;
324645ca708SEric Dumazet 	}
32598322f22SEric Dumazet found:
326c720c7e8SEric Dumazet 	inet_sk(sk)->inet_num = snum;
327d4cada4aSEric Dumazet 	udp_sk(sk)->udp_port_hash = snum;
328d4cada4aSEric Dumazet 	udp_sk(sk)->udp_portaddr_hash ^= snum;
3291da177e4SLinus Torvalds 	if (sk_unhashed(sk)) {
330e32ea7e7SCraig Gallek 		if (sk->sk_reuseport &&
331fe38d2a1SJosef Bacik 		    udp_reuseport_add_sock(sk, hslot)) {
332e32ea7e7SCraig Gallek 			inet_sk(sk)->inet_num = 0;
333e32ea7e7SCraig Gallek 			udp_sk(sk)->udp_port_hash = 0;
334e32ea7e7SCraig Gallek 			udp_sk(sk)->udp_portaddr_hash ^= snum;
335e32ea7e7SCraig Gallek 			goto fail_unlock;
336e32ea7e7SCraig Gallek 		}
337e32ea7e7SCraig Gallek 
338ca065d0cSEric Dumazet 		sk_add_node_rcu(sk, &hslot->head);
339fdcc8aa9SEric Dumazet 		hslot->count++;
340c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
341512615b6SEric Dumazet 
342512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
343512615b6SEric Dumazet 		spin_lock(&hslot2->lock);
344d894ba18SCraig Gallek 		if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
345d894ba18SCraig Gallek 		    sk->sk_family == AF_INET6)
3461602f49bSDavid S. Miller 			hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
347d894ba18SCraig Gallek 					   &hslot2->head);
348d894ba18SCraig Gallek 		else
349ca065d0cSEric Dumazet 			hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
350512615b6SEric Dumazet 					   &hslot2->head);
351512615b6SEric Dumazet 		hslot2->count++;
352512615b6SEric Dumazet 		spin_unlock(&hslot2->lock);
3531da177e4SLinus Torvalds 	}
354ca065d0cSEric Dumazet 	sock_set_flag(sk, SOCK_RCU_FREE);
35525030a7fSGerrit Renker 	error = 0;
356645ca708SEric Dumazet fail_unlock:
357645ca708SEric Dumazet 	spin_unlock_bh(&hslot->lock);
3581da177e4SLinus Torvalds fail:
35925030a7fSGerrit Renker 	return error;
3601da177e4SLinus Torvalds }
361c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port);
3621da177e4SLinus Torvalds 
3636eada011SEric Dumazet static u32 udp4_portaddr_hash(const struct net *net, __be32 saddr,
364d4cada4aSEric Dumazet 			      unsigned int port)
365d4cada4aSEric Dumazet {
3660eae88f3SEric Dumazet 	return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
367d4cada4aSEric Dumazet }
368d4cada4aSEric Dumazet 
3696ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum)
370db8dac20SDavid S. Miller {
37130fff923SEric Dumazet 	unsigned int hash2_nulladdr =
3720eae88f3SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
37330fff923SEric Dumazet 	unsigned int hash2_partial =
37430fff923SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
37530fff923SEric Dumazet 
376d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
37730fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
378fe38d2a1SJosef Bacik 	return udp_lib_get_port(sk, snum, hash2_nulladdr);
379db8dac20SDavid S. Miller }
380db8dac20SDavid S. Miller 
381d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net,
382d1e37288SSu, Xuemin 			 __be32 saddr, __be16 sport,
383fb74c277SDavid Ahern 			 __be32 daddr, unsigned short hnum,
384fb74c277SDavid Ahern 			 int dif, int sdif, bool exact_dif)
385645ca708SEric Dumazet {
38660c04aecSJoe Perches 	int score;
38760c04aecSJoe Perches 	struct inet_sock *inet;
388645ca708SEric Dumazet 
38960c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
39060c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
39160c04aecSJoe Perches 	    ipv6_only_sock(sk))
39260c04aecSJoe Perches 		return -1;
393645ca708SEric Dumazet 
39460c04aecSJoe Perches 	score = (sk->sk_family == PF_INET) ? 2 : 1;
39560c04aecSJoe Perches 	inet = inet_sk(sk);
39660c04aecSJoe Perches 
397c720c7e8SEric Dumazet 	if (inet->inet_rcv_saddr) {
398c720c7e8SEric Dumazet 		if (inet->inet_rcv_saddr != daddr)
399645ca708SEric Dumazet 			return -1;
400ba418fa3STom Herbert 		score += 4;
401645ca708SEric Dumazet 	}
40260c04aecSJoe Perches 
403c720c7e8SEric Dumazet 	if (inet->inet_daddr) {
404c720c7e8SEric Dumazet 		if (inet->inet_daddr != saddr)
405645ca708SEric Dumazet 			return -1;
406ba418fa3STom Herbert 		score += 4;
407645ca708SEric Dumazet 	}
40860c04aecSJoe Perches 
409c720c7e8SEric Dumazet 	if (inet->inet_dport) {
410c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
411645ca708SEric Dumazet 			return -1;
412ba418fa3STom Herbert 		score += 4;
413645ca708SEric Dumazet 	}
41460c04aecSJoe Perches 
41563a6fff3SRobert Shearman 	if (sk->sk_bound_dev_if || exact_dif) {
416fb74c277SDavid Ahern 		bool dev_match = (sk->sk_bound_dev_if == dif ||
417fb74c277SDavid Ahern 				  sk->sk_bound_dev_if == sdif);
418fb74c277SDavid Ahern 
419fb74c277SDavid Ahern 		if (exact_dif && !dev_match)
420645ca708SEric Dumazet 			return -1;
421fb74c277SDavid Ahern 		if (sk->sk_bound_dev_if && dev_match)
422ba418fa3STom Herbert 			score += 4;
423645ca708SEric Dumazet 	}
424fb74c277SDavid Ahern 
42570da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
42670da268bSEric Dumazet 		score++;
427645ca708SEric Dumazet 	return score;
428645ca708SEric Dumazet }
429645ca708SEric Dumazet 
4306eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
43165cd8033SHannes Frederic Sowa 		       const __u16 lport, const __be32 faddr,
43265cd8033SHannes Frederic Sowa 		       const __be16 fport)
43365cd8033SHannes Frederic Sowa {
4341bbdceefSHannes Frederic Sowa 	static u32 udp_ehash_secret __read_mostly;
4351bbdceefSHannes Frederic Sowa 
4361bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret));
4371bbdceefSHannes Frederic Sowa 
43865cd8033SHannes Frederic Sowa 	return __inet_ehashfn(laddr, lport, faddr, fport,
4391bbdceefSHannes Frederic Sowa 			      udp_ehash_secret + net_hash_mix(net));
44065cd8033SHannes Frederic Sowa }
44165cd8033SHannes Frederic Sowa 
442d1e37288SSu, Xuemin /* called with rcu_read_lock() */
4435051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net,
4445051ebd2SEric Dumazet 				     __be32 saddr, __be16 sport,
445fb74c277SDavid Ahern 				     __be32 daddr, unsigned int hnum,
446fb74c277SDavid Ahern 				     int dif, int sdif, bool exact_dif,
447d1e37288SSu, Xuemin 				     struct udp_hslot *hslot2,
4481134158bSCraig Gallek 				     struct sk_buff *skb)
4495051ebd2SEric Dumazet {
4505051ebd2SEric Dumazet 	struct sock *sk, *result;
451ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
452ba418fa3STom Herbert 	u32 hash = 0;
4535051ebd2SEric Dumazet 
4545051ebd2SEric Dumazet 	result = NULL;
455ba418fa3STom Herbert 	badness = 0;
456ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
457d1e37288SSu, Xuemin 		score = compute_score(sk, net, saddr, sport,
458fb74c277SDavid Ahern 				      daddr, hnum, dif, sdif, exact_dif);
4595051ebd2SEric Dumazet 		if (score > badness) {
460ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
461ba418fa3STom Herbert 			if (reuseport) {
46265cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
4637c0cadc6SEric Dumazet 						   saddr, sport);
464ca065d0cSEric Dumazet 				result = reuseport_select_sock(sk, hash, skb,
4651134158bSCraig Gallek 							sizeof(struct udphdr));
466ca065d0cSEric Dumazet 				if (result)
467ca065d0cSEric Dumazet 					return result;
468ba418fa3STom Herbert 				matches = 1;
469ba418fa3STom Herbert 			}
470ca065d0cSEric Dumazet 			badness = score;
471ca065d0cSEric Dumazet 			result = sk;
472ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
473ba418fa3STom Herbert 			matches++;
4748fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
475ba418fa3STom Herbert 				result = sk;
476ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
4775051ebd2SEric Dumazet 		}
4785051ebd2SEric Dumazet 	}
4795051ebd2SEric Dumazet 	return result;
4805051ebd2SEric Dumazet }
4815051ebd2SEric Dumazet 
482db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
483db8dac20SDavid S. Miller  * harder than this. -DaveM
484db8dac20SDavid S. Miller  */
485fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
486fb74c277SDavid Ahern 		__be16 sport, __be32 daddr, __be16 dport, int dif,
487fb74c277SDavid Ahern 		int sdif, struct udp_table *udptable, struct sk_buff *skb)
488db8dac20SDavid S. Miller {
489271b72c7SEric Dumazet 	struct sock *sk, *result;
490db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(dport);
4915051ebd2SEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
4925051ebd2SEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
49363a6fff3SRobert Shearman 	bool exact_dif = udp_lib_exact_dif_match(net, skb);
494ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
495ba418fa3STom Herbert 	u32 hash = 0;
496db8dac20SDavid S. Miller 
4975051ebd2SEric Dumazet 	if (hslot->count > 10) {
4985051ebd2SEric Dumazet 		hash2 = udp4_portaddr_hash(net, daddr, hnum);
4995051ebd2SEric Dumazet 		slot2 = hash2 & udptable->mask;
5005051ebd2SEric Dumazet 		hslot2 = &udptable->hash2[slot2];
5015051ebd2SEric Dumazet 		if (hslot->count < hslot2->count)
5025051ebd2SEric Dumazet 			goto begin;
5035051ebd2SEric Dumazet 
5045051ebd2SEric Dumazet 		result = udp4_lib_lookup2(net, saddr, sport,
505fb74c277SDavid Ahern 					  daddr, hnum, dif, sdif,
50663a6fff3SRobert Shearman 					  exact_dif, hslot2, skb);
5075051ebd2SEric Dumazet 		if (!result) {
508d1e37288SSu, Xuemin 			unsigned int old_slot2 = slot2;
5090eae88f3SEric Dumazet 			hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
5105051ebd2SEric Dumazet 			slot2 = hash2 & udptable->mask;
511d1e37288SSu, Xuemin 			/* avoid searching the same slot again. */
512d1e37288SSu, Xuemin 			if (unlikely(slot2 == old_slot2))
513d1e37288SSu, Xuemin 				return result;
514d1e37288SSu, Xuemin 
5155051ebd2SEric Dumazet 			hslot2 = &udptable->hash2[slot2];
5165051ebd2SEric Dumazet 			if (hslot->count < hslot2->count)
5175051ebd2SEric Dumazet 				goto begin;
5185051ebd2SEric Dumazet 
5191223c67cSJorge Boncompte [DTI2] 			result = udp4_lib_lookup2(net, saddr, sport,
520fb74c277SDavid Ahern 						  daddr, hnum, dif, sdif,
52163a6fff3SRobert Shearman 						  exact_dif, hslot2, skb);
5225051ebd2SEric Dumazet 		}
5235051ebd2SEric Dumazet 		return result;
5245051ebd2SEric Dumazet 	}
525271b72c7SEric Dumazet begin:
526271b72c7SEric Dumazet 	result = NULL;
527ba418fa3STom Herbert 	badness = 0;
528ca065d0cSEric Dumazet 	sk_for_each_rcu(sk, &hslot->head) {
529d1e37288SSu, Xuemin 		score = compute_score(sk, net, saddr, sport,
530fb74c277SDavid Ahern 				      daddr, hnum, dif, sdif, exact_dif);
531645ca708SEric Dumazet 		if (score > badness) {
532ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
533ba418fa3STom Herbert 			if (reuseport) {
53465cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
5357c0cadc6SEric Dumazet 						   saddr, sport);
536ca065d0cSEric Dumazet 				result = reuseport_select_sock(sk, hash, skb,
537538950a1SCraig Gallek 							sizeof(struct udphdr));
538ca065d0cSEric Dumazet 				if (result)
539ca065d0cSEric Dumazet 					return result;
540ba418fa3STom Herbert 				matches = 1;
541ba418fa3STom Herbert 			}
542ca065d0cSEric Dumazet 			result = sk;
543ca065d0cSEric Dumazet 			badness = score;
544ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
545ba418fa3STom Herbert 			matches++;
5468fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
547ba418fa3STom Herbert 				result = sk;
548ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
549db8dac20SDavid S. Miller 		}
550db8dac20SDavid S. Miller 	}
551db8dac20SDavid S. Miller 	return result;
552db8dac20SDavid S. Miller }
553fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
554db8dac20SDavid S. Miller 
555607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
556607c4aafSKOVACS Krisztian 						 __be16 sport, __be16 dport,
557645ca708SEric Dumazet 						 struct udp_table *udptable)
558607c4aafSKOVACS Krisztian {
559607c4aafSKOVACS Krisztian 	const struct iphdr *iph = ip_hdr(skb);
560607c4aafSKOVACS Krisztian 
561ed7cbbceSAlexander Duyck 	return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
562607c4aafSKOVACS Krisztian 				 iph->daddr, dport, inet_iif(skb),
563fb74c277SDavid Ahern 				 inet_sdif(skb), udptable, skb);
564607c4aafSKOVACS Krisztian }
565607c4aafSKOVACS Krisztian 
56663058308STom Herbert struct sock *udp4_lib_lookup_skb(struct sk_buff *skb,
56763058308STom Herbert 				 __be16 sport, __be16 dport)
56863058308STom Herbert {
569ed7cbbceSAlexander Duyck 	return __udp4_lib_lookup_skb(skb, sport, dport, &udp_table);
57063058308STom Herbert }
57163058308STom Herbert EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb);
57263058308STom Herbert 
573ca065d0cSEric Dumazet /* Must be called under rcu_read_lock().
574ca065d0cSEric Dumazet  * Does increment socket refcount.
575ca065d0cSEric Dumazet  */
576ca065d0cSEric Dumazet #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \
57730f58158SArnd Bergmann     IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY) || \
57830f58158SArnd Bergmann     IS_ENABLED(CONFIG_NF_SOCKET_IPV4)
579bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
580bcd41303SKOVACS Krisztian 			     __be32 daddr, __be16 dport, int dif)
581bcd41303SKOVACS Krisztian {
582ca065d0cSEric Dumazet 	struct sock *sk;
583ca065d0cSEric Dumazet 
584ca065d0cSEric Dumazet 	sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport,
585fb74c277SDavid Ahern 			       dif, 0, &udp_table, NULL);
58641c6d650SReshetova, Elena 	if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
587ca065d0cSEric Dumazet 		sk = NULL;
588ca065d0cSEric Dumazet 	return sk;
589bcd41303SKOVACS Krisztian }
590bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup);
591ca065d0cSEric Dumazet #endif
592bcd41303SKOVACS Krisztian 
593421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
594421b3885SShawn Bohrer 				       __be16 loc_port, __be32 loc_addr,
595421b3885SShawn Bohrer 				       __be16 rmt_port, __be32 rmt_addr,
596fb74c277SDavid Ahern 				       int dif, int sdif, unsigned short hnum)
597421b3885SShawn Bohrer {
598421b3885SShawn Bohrer 	struct inet_sock *inet = inet_sk(sk);
599421b3885SShawn Bohrer 
600421b3885SShawn Bohrer 	if (!net_eq(sock_net(sk), net) ||
601421b3885SShawn Bohrer 	    udp_sk(sk)->udp_port_hash != hnum ||
602421b3885SShawn Bohrer 	    (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
603421b3885SShawn Bohrer 	    (inet->inet_dport != rmt_port && inet->inet_dport) ||
604421b3885SShawn Bohrer 	    (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
605421b3885SShawn Bohrer 	    ipv6_only_sock(sk) ||
606fb74c277SDavid Ahern 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif &&
607fb74c277SDavid Ahern 	     sk->sk_bound_dev_if != sdif))
608421b3885SShawn Bohrer 		return false;
609*60d9b031SDavid Ahern 	if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif))
610421b3885SShawn Bohrer 		return false;
611421b3885SShawn Bohrer 	return true;
612421b3885SShawn Bohrer }
613421b3885SShawn Bohrer 
614db8dac20SDavid S. Miller /*
615db8dac20SDavid S. Miller  * This routine is called by the ICMP module when it gets some
616db8dac20SDavid S. Miller  * sort of error condition.  If err < 0 then the socket should
617db8dac20SDavid S. Miller  * be closed and the error returned to the user.  If err > 0
618db8dac20SDavid S. Miller  * it's just the icmp type << 8 | icmp code.
619db8dac20SDavid S. Miller  * Header points to the ip header of the error packet. We move
620db8dac20SDavid S. Miller  * on past this. Then (as it used to claim before adjustment)
621db8dac20SDavid S. Miller  * header points to the first 8 bytes of the udp header.  We need
622db8dac20SDavid S. Miller  * to find the appropriate port.
623db8dac20SDavid S. Miller  */
624db8dac20SDavid S. Miller 
625645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
626db8dac20SDavid S. Miller {
627db8dac20SDavid S. Miller 	struct inet_sock *inet;
628b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *)skb->data;
629db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
630db8dac20SDavid S. Miller 	const int type = icmp_hdr(skb)->type;
631db8dac20SDavid S. Miller 	const int code = icmp_hdr(skb)->code;
632db8dac20SDavid S. Miller 	struct sock *sk;
633db8dac20SDavid S. Miller 	int harderr;
634db8dac20SDavid S. Miller 	int err;
635fd54d716SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
636db8dac20SDavid S. Miller 
637fd54d716SPavel Emelyanov 	sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
638fb74c277SDavid Ahern 			       iph->saddr, uh->source, skb->dev->ifindex, 0,
639fb74c277SDavid Ahern 			       udptable, NULL);
64051456b29SIan Morris 	if (!sk) {
6415d3848bcSEric Dumazet 		__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
642db8dac20SDavid S. Miller 		return;	/* No socket for error */
643db8dac20SDavid S. Miller 	}
644db8dac20SDavid S. Miller 
645db8dac20SDavid S. Miller 	err = 0;
646db8dac20SDavid S. Miller 	harderr = 0;
647db8dac20SDavid S. Miller 	inet = inet_sk(sk);
648db8dac20SDavid S. Miller 
649db8dac20SDavid S. Miller 	switch (type) {
650db8dac20SDavid S. Miller 	default:
651db8dac20SDavid S. Miller 	case ICMP_TIME_EXCEEDED:
652db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
653db8dac20SDavid S. Miller 		break;
654db8dac20SDavid S. Miller 	case ICMP_SOURCE_QUENCH:
655db8dac20SDavid S. Miller 		goto out;
656db8dac20SDavid S. Miller 	case ICMP_PARAMETERPROB:
657db8dac20SDavid S. Miller 		err = EPROTO;
658db8dac20SDavid S. Miller 		harderr = 1;
659db8dac20SDavid S. Miller 		break;
660db8dac20SDavid S. Miller 	case ICMP_DEST_UNREACH:
661db8dac20SDavid S. Miller 		if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
66236393395SDavid S. Miller 			ipv4_sk_update_pmtu(skb, sk, info);
663db8dac20SDavid S. Miller 			if (inet->pmtudisc != IP_PMTUDISC_DONT) {
664db8dac20SDavid S. Miller 				err = EMSGSIZE;
665db8dac20SDavid S. Miller 				harderr = 1;
666db8dac20SDavid S. Miller 				break;
667db8dac20SDavid S. Miller 			}
668db8dac20SDavid S. Miller 			goto out;
669db8dac20SDavid S. Miller 		}
670db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
671db8dac20SDavid S. Miller 		if (code <= NR_ICMP_UNREACH) {
672db8dac20SDavid S. Miller 			harderr = icmp_err_convert[code].fatal;
673db8dac20SDavid S. Miller 			err = icmp_err_convert[code].errno;
674db8dac20SDavid S. Miller 		}
675db8dac20SDavid S. Miller 		break;
67655be7a9cSDavid S. Miller 	case ICMP_REDIRECT:
67755be7a9cSDavid S. Miller 		ipv4_sk_redirect(skb, sk);
6781a462d18SDuan Jiong 		goto out;
679db8dac20SDavid S. Miller 	}
680db8dac20SDavid S. Miller 
681db8dac20SDavid S. Miller 	/*
682db8dac20SDavid S. Miller 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
683db8dac20SDavid S. Miller 	 *	4.1.3.3.
684db8dac20SDavid S. Miller 	 */
685db8dac20SDavid S. Miller 	if (!inet->recverr) {
686db8dac20SDavid S. Miller 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
687db8dac20SDavid S. Miller 			goto out;
688b1faf566SEric Dumazet 	} else
689db8dac20SDavid S. Miller 		ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
690b1faf566SEric Dumazet 
691db8dac20SDavid S. Miller 	sk->sk_err = err;
692db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
693db8dac20SDavid S. Miller out:
694ca065d0cSEric Dumazet 	return;
695db8dac20SDavid S. Miller }
696db8dac20SDavid S. Miller 
697db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info)
698db8dac20SDavid S. Miller {
699645ca708SEric Dumazet 	__udp4_lib_err(skb, info, &udp_table);
700db8dac20SDavid S. Miller }
701db8dac20SDavid S. Miller 
702db8dac20SDavid S. Miller /*
703db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
704db8dac20SDavid S. Miller  */
70536d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk)
706db8dac20SDavid S. Miller {
707db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
708db8dac20SDavid S. Miller 
709db8dac20SDavid S. Miller 	if (up->pending) {
710db8dac20SDavid S. Miller 		up->len = 0;
711db8dac20SDavid S. Miller 		up->pending = 0;
712db8dac20SDavid S. Miller 		ip_flush_pending_frames(sk);
713db8dac20SDavid S. Miller 	}
714db8dac20SDavid S. Miller }
71536d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames);
716db8dac20SDavid S. Miller 
717db8dac20SDavid S. Miller /**
718f6b9664fSHerbert Xu  * 	udp4_hwcsum  -  handle outgoing HW checksumming
719db8dac20SDavid S. Miller  * 	@skb: 	sk_buff containing the filled-in UDP header
720db8dac20SDavid S. Miller  * 	        (checksum field must be zeroed out)
721f6b9664fSHerbert Xu  *	@src:	source IP address
722f6b9664fSHerbert Xu  *	@dst:	destination IP address
723db8dac20SDavid S. Miller  */
724c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
725db8dac20SDavid S. Miller {
726db8dac20SDavid S. Miller 	struct udphdr *uh = udp_hdr(skb);
727f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
728f6b9664fSHerbert Xu 	int len = skb->len - offset;
729f6b9664fSHerbert Xu 	int hlen = len;
730db8dac20SDavid S. Miller 	__wsum csum = 0;
731db8dac20SDavid S. Miller 
732ebbe495fSWANG Cong 	if (!skb_has_frag_list(skb)) {
733db8dac20SDavid S. Miller 		/*
734db8dac20SDavid S. Miller 		 * Only one fragment on the socket.
735db8dac20SDavid S. Miller 		 */
736db8dac20SDavid S. Miller 		skb->csum_start = skb_transport_header(skb) - skb->head;
737db8dac20SDavid S. Miller 		skb->csum_offset = offsetof(struct udphdr, check);
738f6b9664fSHerbert Xu 		uh->check = ~csum_tcpudp_magic(src, dst, len,
739f6b9664fSHerbert Xu 					       IPPROTO_UDP, 0);
740db8dac20SDavid S. Miller 	} else {
741ebbe495fSWANG Cong 		struct sk_buff *frags;
742ebbe495fSWANG Cong 
743db8dac20SDavid S. Miller 		/*
744db8dac20SDavid S. Miller 		 * HW-checksum won't work as there are two or more
745db8dac20SDavid S. Miller 		 * fragments on the socket so that all csums of sk_buffs
746db8dac20SDavid S. Miller 		 * should be together
747db8dac20SDavid S. Miller 		 */
748ebbe495fSWANG Cong 		skb_walk_frags(skb, frags) {
749f6b9664fSHerbert Xu 			csum = csum_add(csum, frags->csum);
750f6b9664fSHerbert Xu 			hlen -= frags->len;
751ebbe495fSWANG Cong 		}
752db8dac20SDavid S. Miller 
753f6b9664fSHerbert Xu 		csum = skb_checksum(skb, offset, hlen, csum);
754db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
755db8dac20SDavid S. Miller 
756db8dac20SDavid S. Miller 		uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
757db8dac20SDavid S. Miller 		if (uh->check == 0)
758db8dac20SDavid S. Miller 			uh->check = CSUM_MANGLED_0;
759db8dac20SDavid S. Miller 	}
760db8dac20SDavid S. Miller }
761c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum);
762db8dac20SDavid S. Miller 
763af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended
764af5fcba7STom Herbert  * for the simple case like when setting the checksum for a UDP tunnel.
765af5fcba7STom Herbert  */
766af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb,
767af5fcba7STom Herbert 		  __be32 saddr, __be32 daddr, int len)
768af5fcba7STom Herbert {
769af5fcba7STom Herbert 	struct udphdr *uh = udp_hdr(skb);
770af5fcba7STom Herbert 
771179bc67fSEdward Cree 	if (nocheck) {
772af5fcba7STom Herbert 		uh->check = 0;
773179bc67fSEdward Cree 	} else if (skb_is_gso(skb)) {
774af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
775179bc67fSEdward Cree 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
776179bc67fSEdward Cree 		uh->check = 0;
777179bc67fSEdward Cree 		uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb));
778179bc67fSEdward Cree 		if (uh->check == 0)
779179bc67fSEdward Cree 			uh->check = CSUM_MANGLED_0;
780d75f1306SEdward Cree 	} else {
781af5fcba7STom Herbert 		skb->ip_summed = CHECKSUM_PARTIAL;
782af5fcba7STom Herbert 		skb->csum_start = skb_transport_header(skb) - skb->head;
783af5fcba7STom Herbert 		skb->csum_offset = offsetof(struct udphdr, check);
784af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
785af5fcba7STom Herbert 	}
786af5fcba7STom Herbert }
787af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum);
788af5fcba7STom Herbert 
78979ab0531SDavid S. Miller static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4)
790f6b9664fSHerbert Xu {
791f6b9664fSHerbert Xu 	struct sock *sk = skb->sk;
792f6b9664fSHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
793f6b9664fSHerbert Xu 	struct udphdr *uh;
794f6b9664fSHerbert Xu 	int err = 0;
795f6b9664fSHerbert Xu 	int is_udplite = IS_UDPLITE(sk);
796f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
797f6b9664fSHerbert Xu 	int len = skb->len - offset;
798f6b9664fSHerbert Xu 	__wsum csum = 0;
799f6b9664fSHerbert Xu 
800f6b9664fSHerbert Xu 	/*
801f6b9664fSHerbert Xu 	 * Create a UDP header
802f6b9664fSHerbert Xu 	 */
803f6b9664fSHerbert Xu 	uh = udp_hdr(skb);
804f6b9664fSHerbert Xu 	uh->source = inet->inet_sport;
80579ab0531SDavid S. Miller 	uh->dest = fl4->fl4_dport;
806f6b9664fSHerbert Xu 	uh->len = htons(len);
807f6b9664fSHerbert Xu 	uh->check = 0;
808f6b9664fSHerbert Xu 
809f6b9664fSHerbert Xu 	if (is_udplite)  				 /*     UDP-Lite      */
810f6b9664fSHerbert Xu 		csum = udplite_csum(skb);
811f6b9664fSHerbert Xu 
81228448b80STom Herbert 	else if (sk->sk_no_check_tx) {   /* UDP csum disabled */
813f6b9664fSHerbert Xu 
814f6b9664fSHerbert Xu 		skb->ip_summed = CHECKSUM_NONE;
815f6b9664fSHerbert Xu 		goto send;
816f6b9664fSHerbert Xu 
817f6b9664fSHerbert Xu 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
818f6b9664fSHerbert Xu 
81979ab0531SDavid S. Miller 		udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
820f6b9664fSHerbert Xu 		goto send;
821f6b9664fSHerbert Xu 
822f6b9664fSHerbert Xu 	} else
823f6b9664fSHerbert Xu 		csum = udp_csum(skb);
824f6b9664fSHerbert Xu 
825f6b9664fSHerbert Xu 	/* add protocol-dependent pseudo-header */
82679ab0531SDavid S. Miller 	uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
827f6b9664fSHerbert Xu 				      sk->sk_protocol, csum);
828f6b9664fSHerbert Xu 	if (uh->check == 0)
829f6b9664fSHerbert Xu 		uh->check = CSUM_MANGLED_0;
830f6b9664fSHerbert Xu 
831f6b9664fSHerbert Xu send:
832b5ec8eeaSEric Dumazet 	err = ip_send_skb(sock_net(sk), skb);
833f6b9664fSHerbert Xu 	if (err) {
834f6b9664fSHerbert Xu 		if (err == -ENOBUFS && !inet->recverr) {
8356aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk),
836f6b9664fSHerbert Xu 				      UDP_MIB_SNDBUFERRORS, is_udplite);
837f6b9664fSHerbert Xu 			err = 0;
838f6b9664fSHerbert Xu 		}
839f6b9664fSHerbert Xu 	} else
8406aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk),
841f6b9664fSHerbert Xu 			      UDP_MIB_OUTDATAGRAMS, is_udplite);
842f6b9664fSHerbert Xu 	return err;
843f6b9664fSHerbert Xu }
844f6b9664fSHerbert Xu 
845db8dac20SDavid S. Miller /*
846db8dac20SDavid S. Miller  * Push out all pending data as one UDP datagram. Socket is locked.
847db8dac20SDavid S. Miller  */
8488822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk)
849db8dac20SDavid S. Miller {
850db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
851db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
852b6f21b26SDavid S. Miller 	struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
853db8dac20SDavid S. Miller 	struct sk_buff *skb;
854db8dac20SDavid S. Miller 	int err = 0;
855db8dac20SDavid S. Miller 
85677968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
857f6b9664fSHerbert Xu 	if (!skb)
858db8dac20SDavid S. Miller 		goto out;
859db8dac20SDavid S. Miller 
86079ab0531SDavid S. Miller 	err = udp_send_skb(skb, fl4);
861db8dac20SDavid S. Miller 
862db8dac20SDavid S. Miller out:
863db8dac20SDavid S. Miller 	up->len = 0;
864db8dac20SDavid S. Miller 	up->pending = 0;
865db8dac20SDavid S. Miller 	return err;
866db8dac20SDavid S. Miller }
8678822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames);
868db8dac20SDavid S. Miller 
8691b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
870db8dac20SDavid S. Miller {
871db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
872db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
873e474995fSDavid S. Miller 	struct flowi4 fl4_stack;
874b6f21b26SDavid S. Miller 	struct flowi4 *fl4;
875db8dac20SDavid S. Miller 	int ulen = len;
876db8dac20SDavid S. Miller 	struct ipcm_cookie ipc;
877db8dac20SDavid S. Miller 	struct rtable *rt = NULL;
878db8dac20SDavid S. Miller 	int free = 0;
879db8dac20SDavid S. Miller 	int connected = 0;
880db8dac20SDavid S. Miller 	__be32 daddr, faddr, saddr;
881db8dac20SDavid S. Miller 	__be16 dport;
882db8dac20SDavid S. Miller 	u8  tos;
883db8dac20SDavid S. Miller 	int err, is_udplite = IS_UDPLITE(sk);
884db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
885db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
886903ab86dSHerbert Xu 	struct sk_buff *skb;
887f6d8bd05SEric Dumazet 	struct ip_options_data opt_copy;
888db8dac20SDavid S. Miller 
889db8dac20SDavid S. Miller 	if (len > 0xFFFF)
890db8dac20SDavid S. Miller 		return -EMSGSIZE;
891db8dac20SDavid S. Miller 
892db8dac20SDavid S. Miller 	/*
893db8dac20SDavid S. Miller 	 *	Check the flags.
894db8dac20SDavid S. Miller 	 */
895db8dac20SDavid S. Miller 
896db8dac20SDavid S. Miller 	if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
897db8dac20SDavid S. Miller 		return -EOPNOTSUPP;
898db8dac20SDavid S. Miller 
899db8dac20SDavid S. Miller 	ipc.opt = NULL;
9002244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
901aa661581SFrancesco Fusco 	ipc.ttl = 0;
902aa661581SFrancesco Fusco 	ipc.tos = -1;
903db8dac20SDavid S. Miller 
904903ab86dSHerbert Xu 	getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
905903ab86dSHerbert Xu 
906f5fca608SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
907db8dac20SDavid S. Miller 	if (up->pending) {
908db8dac20SDavid S. Miller 		/*
909db8dac20SDavid S. Miller 		 * There are pending frames.
910db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
911db8dac20SDavid S. Miller 		 */
912db8dac20SDavid S. Miller 		lock_sock(sk);
913db8dac20SDavid S. Miller 		if (likely(up->pending)) {
914db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET)) {
915db8dac20SDavid S. Miller 				release_sock(sk);
916db8dac20SDavid S. Miller 				return -EINVAL;
917db8dac20SDavid S. Miller 			}
918db8dac20SDavid S. Miller 			goto do_append_data;
919db8dac20SDavid S. Miller 		}
920db8dac20SDavid S. Miller 		release_sock(sk);
921db8dac20SDavid S. Miller 	}
922db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
923db8dac20SDavid S. Miller 
924db8dac20SDavid S. Miller 	/*
925db8dac20SDavid S. Miller 	 *	Get and verify the address.
926db8dac20SDavid S. Miller 	 */
927db8dac20SDavid S. Miller 	if (msg->msg_name) {
928342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
929db8dac20SDavid S. Miller 		if (msg->msg_namelen < sizeof(*usin))
930db8dac20SDavid S. Miller 			return -EINVAL;
931db8dac20SDavid S. Miller 		if (usin->sin_family != AF_INET) {
932db8dac20SDavid S. Miller 			if (usin->sin_family != AF_UNSPEC)
933db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
934db8dac20SDavid S. Miller 		}
935db8dac20SDavid S. Miller 
936db8dac20SDavid S. Miller 		daddr = usin->sin_addr.s_addr;
937db8dac20SDavid S. Miller 		dport = usin->sin_port;
938db8dac20SDavid S. Miller 		if (dport == 0)
939db8dac20SDavid S. Miller 			return -EINVAL;
940db8dac20SDavid S. Miller 	} else {
941db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
942db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
943c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
944c720c7e8SEric Dumazet 		dport = inet->inet_dport;
945db8dac20SDavid S. Miller 		/* Open fast path for connected socket.
946db8dac20SDavid S. Miller 		   Route will not be used, if at least one option is set.
947db8dac20SDavid S. Miller 		 */
948db8dac20SDavid S. Miller 		connected = 1;
949db8dac20SDavid S. Miller 	}
950c14ac945SSoheil Hassas Yeganeh 
951c14ac945SSoheil Hassas Yeganeh 	ipc.sockc.tsflags = sk->sk_tsflags;
952c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
953db8dac20SDavid S. Miller 	ipc.oif = sk->sk_bound_dev_if;
954bf84a010SDaniel Borkmann 
955db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
95624025c46SSoheil Hassas Yeganeh 		err = ip_cmsg_send(sk, msg, &ipc, sk->sk_family == AF_INET6);
95791948309SEric Dumazet 		if (unlikely(err)) {
95891948309SEric Dumazet 			kfree(ipc.opt);
959db8dac20SDavid S. Miller 			return err;
96091948309SEric Dumazet 		}
961db8dac20SDavid S. Miller 		if (ipc.opt)
962db8dac20SDavid S. Miller 			free = 1;
963db8dac20SDavid S. Miller 		connected = 0;
964db8dac20SDavid S. Miller 	}
965f6d8bd05SEric Dumazet 	if (!ipc.opt) {
966f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
967f6d8bd05SEric Dumazet 
968f6d8bd05SEric Dumazet 		rcu_read_lock();
969f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference(inet->inet_opt);
970f6d8bd05SEric Dumazet 		if (inet_opt) {
971f6d8bd05SEric Dumazet 			memcpy(&opt_copy, inet_opt,
972f6d8bd05SEric Dumazet 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
973f6d8bd05SEric Dumazet 			ipc.opt = &opt_copy.opt;
974f6d8bd05SEric Dumazet 		}
975f6d8bd05SEric Dumazet 		rcu_read_unlock();
976f6d8bd05SEric Dumazet 	}
977db8dac20SDavid S. Miller 
978db8dac20SDavid S. Miller 	saddr = ipc.addr;
979db8dac20SDavid S. Miller 	ipc.addr = faddr = daddr;
980db8dac20SDavid S. Miller 
981c14ac945SSoheil Hassas Yeganeh 	sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags);
982c14ac945SSoheil Hassas Yeganeh 
983f6d8bd05SEric Dumazet 	if (ipc.opt && ipc.opt->opt.srr) {
984db8dac20SDavid S. Miller 		if (!daddr)
985db8dac20SDavid S. Miller 			return -EINVAL;
986f6d8bd05SEric Dumazet 		faddr = ipc.opt->opt.faddr;
987db8dac20SDavid S. Miller 		connected = 0;
988db8dac20SDavid S. Miller 	}
989aa661581SFrancesco Fusco 	tos = get_rttos(&ipc, inet);
990db8dac20SDavid S. Miller 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
991db8dac20SDavid S. Miller 	    (msg->msg_flags & MSG_DONTROUTE) ||
992f6d8bd05SEric Dumazet 	    (ipc.opt && ipc.opt->opt.is_strictroute)) {
993db8dac20SDavid S. Miller 		tos |= RTO_ONLINK;
994db8dac20SDavid S. Miller 		connected = 0;
995db8dac20SDavid S. Miller 	}
996db8dac20SDavid S. Miller 
997db8dac20SDavid S. Miller 	if (ipv4_is_multicast(daddr)) {
998db8dac20SDavid S. Miller 		if (!ipc.oif)
999db8dac20SDavid S. Miller 			ipc.oif = inet->mc_index;
1000db8dac20SDavid S. Miller 		if (!saddr)
1001db8dac20SDavid S. Miller 			saddr = inet->mc_addr;
1002db8dac20SDavid S. Miller 		connected = 0;
100376e21053SErich E. Hoover 	} else if (!ipc.oif)
100476e21053SErich E. Hoover 		ipc.oif = inet->uc_index;
1005db8dac20SDavid S. Miller 
1006db8dac20SDavid S. Miller 	if (connected)
1007db8dac20SDavid S. Miller 		rt = (struct rtable *)sk_dst_check(sk, 0);
1008db8dac20SDavid S. Miller 
100951456b29SIan Morris 	if (!rt) {
101084a3aa00SPavel Emelyanov 		struct net *net = sock_net(sk);
10119a24abfaSDavid Ahern 		__u8 flow_flags = inet_sk_flowi_flags(sk);
101284a3aa00SPavel Emelyanov 
1013e474995fSDavid S. Miller 		fl4 = &fl4_stack;
10149a24abfaSDavid Ahern 
10159a24abfaSDavid Ahern 		flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
10169a24abfaSDavid Ahern 				   RT_SCOPE_UNIVERSE, sk->sk_protocol,
10179a24abfaSDavid Ahern 				   flow_flags,
1018e2d118a1SLorenzo Colitti 				   faddr, saddr, dport, inet->inet_sport,
1019e2d118a1SLorenzo Colitti 				   sk->sk_uid);
1020c0951cbcSDavid S. Miller 
1021e474995fSDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
1022e474995fSDavid S. Miller 		rt = ip_route_output_flow(net, fl4, sk);
1023b23dd4feSDavid S. Miller 		if (IS_ERR(rt)) {
1024b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
102506dc94b1SDavid S. Miller 			rt = NULL;
1026db8dac20SDavid S. Miller 			if (err == -ENETUNREACH)
1027f1d8cba6SEric Dumazet 				IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
1028db8dac20SDavid S. Miller 			goto out;
1029db8dac20SDavid S. Miller 		}
1030db8dac20SDavid S. Miller 
1031db8dac20SDavid S. Miller 		err = -EACCES;
1032db8dac20SDavid S. Miller 		if ((rt->rt_flags & RTCF_BROADCAST) &&
1033db8dac20SDavid S. Miller 		    !sock_flag(sk, SOCK_BROADCAST))
1034db8dac20SDavid S. Miller 			goto out;
1035db8dac20SDavid S. Miller 		if (connected)
1036d8d1f30bSChangli Gao 			sk_dst_set(sk, dst_clone(&rt->dst));
1037db8dac20SDavid S. Miller 	}
1038db8dac20SDavid S. Miller 
1039db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1040db8dac20SDavid S. Miller 		goto do_confirm;
1041db8dac20SDavid S. Miller back_from_confirm:
1042db8dac20SDavid S. Miller 
1043e474995fSDavid S. Miller 	saddr = fl4->saddr;
1044db8dac20SDavid S. Miller 	if (!ipc.addr)
1045e474995fSDavid S. Miller 		daddr = ipc.addr = fl4->daddr;
1046db8dac20SDavid S. Miller 
1047903ab86dSHerbert Xu 	/* Lockless fast path for the non-corking case. */
1048903ab86dSHerbert Xu 	if (!corkreq) {
1049f69e6d13SAl Viro 		skb = ip_make_skb(sk, fl4, getfrag, msg, ulen,
1050903ab86dSHerbert Xu 				  sizeof(struct udphdr), &ipc, &rt,
1051903ab86dSHerbert Xu 				  msg->msg_flags);
1052903ab86dSHerbert Xu 		err = PTR_ERR(skb);
105350c3a487SYOSHIFUJI Hideaki / 吉藤英明 		if (!IS_ERR_OR_NULL(skb))
105479ab0531SDavid S. Miller 			err = udp_send_skb(skb, fl4);
1055903ab86dSHerbert Xu 		goto out;
1056903ab86dSHerbert Xu 	}
1057903ab86dSHerbert Xu 
1058db8dac20SDavid S. Miller 	lock_sock(sk);
1059db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1060db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1061db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1062db8dac20SDavid S. Miller 		release_sock(sk);
1063db8dac20SDavid S. Miller 
1064ba7a46f1SJoe Perches 		net_dbg_ratelimited("cork app bug 2\n");
1065db8dac20SDavid S. Miller 		err = -EINVAL;
1066db8dac20SDavid S. Miller 		goto out;
1067db8dac20SDavid S. Miller 	}
1068db8dac20SDavid S. Miller 	/*
1069db8dac20SDavid S. Miller 	 *	Now cork the socket to pend data.
1070db8dac20SDavid S. Miller 	 */
1071b6f21b26SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
1072b6f21b26SDavid S. Miller 	fl4->daddr = daddr;
1073b6f21b26SDavid S. Miller 	fl4->saddr = saddr;
10749cce96dfSDavid S. Miller 	fl4->fl4_dport = dport;
10759cce96dfSDavid S. Miller 	fl4->fl4_sport = inet->inet_sport;
1076db8dac20SDavid S. Miller 	up->pending = AF_INET;
1077db8dac20SDavid S. Miller 
1078db8dac20SDavid S. Miller do_append_data:
1079db8dac20SDavid S. Miller 	up->len += ulen;
1080f69e6d13SAl Viro 	err = ip_append_data(sk, fl4, getfrag, msg, ulen,
10812e77d89bSEric Dumazet 			     sizeof(struct udphdr), &ipc, &rt,
1082db8dac20SDavid S. Miller 			     corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1083db8dac20SDavid S. Miller 	if (err)
1084db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1085db8dac20SDavid S. Miller 	else if (!corkreq)
1086db8dac20SDavid S. Miller 		err = udp_push_pending_frames(sk);
1087db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1088db8dac20SDavid S. Miller 		up->pending = 0;
1089db8dac20SDavid S. Miller 	release_sock(sk);
1090db8dac20SDavid S. Miller 
1091db8dac20SDavid S. Miller out:
1092db8dac20SDavid S. Miller 	ip_rt_put(rt);
1093db8dac20SDavid S. Miller 	if (free)
1094db8dac20SDavid S. Miller 		kfree(ipc.opt);
1095db8dac20SDavid S. Miller 	if (!err)
1096db8dac20SDavid S. Miller 		return len;
1097db8dac20SDavid S. Miller 	/*
1098db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1099db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1100db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1101db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1102db8dac20SDavid S. Miller 	 * seems like overkill.
1103db8dac20SDavid S. Miller 	 */
1104db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
11056aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk),
1106629ca23cSPavel Emelyanov 			      UDP_MIB_SNDBUFERRORS, is_udplite);
1107db8dac20SDavid S. Miller 	}
1108db8dac20SDavid S. Miller 	return err;
1109db8dac20SDavid S. Miller 
1110db8dac20SDavid S. Miller do_confirm:
11110dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
11120dec879fSJulian Anastasov 		dst_confirm_neigh(&rt->dst, &fl4->daddr);
1113db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1114db8dac20SDavid S. Miller 		goto back_from_confirm;
1115db8dac20SDavid S. Miller 	err = 0;
1116db8dac20SDavid S. Miller 	goto out;
1117db8dac20SDavid S. Miller }
1118c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg);
1119db8dac20SDavid S. Miller 
1120db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset,
1121db8dac20SDavid S. Miller 		 size_t size, int flags)
1122db8dac20SDavid S. Miller {
1123f5fca608SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1124db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1125db8dac20SDavid S. Miller 	int ret;
1126db8dac20SDavid S. Miller 
1127d3f7d56aSShawn Landden 	if (flags & MSG_SENDPAGE_NOTLAST)
1128d3f7d56aSShawn Landden 		flags |= MSG_MORE;
1129d3f7d56aSShawn Landden 
1130db8dac20SDavid S. Miller 	if (!up->pending) {
1131db8dac20SDavid S. Miller 		struct msghdr msg = {	.msg_flags = flags|MSG_MORE };
1132db8dac20SDavid S. Miller 
1133db8dac20SDavid S. Miller 		/* Call udp_sendmsg to specify destination address which
1134db8dac20SDavid S. Miller 		 * sendpage interface can't pass.
1135db8dac20SDavid S. Miller 		 * This will succeed only when the socket is connected.
1136db8dac20SDavid S. Miller 		 */
11371b784140SYing Xue 		ret = udp_sendmsg(sk, &msg, 0);
1138db8dac20SDavid S. Miller 		if (ret < 0)
1139db8dac20SDavid S. Miller 			return ret;
1140db8dac20SDavid S. Miller 	}
1141db8dac20SDavid S. Miller 
1142db8dac20SDavid S. Miller 	lock_sock(sk);
1143db8dac20SDavid S. Miller 
1144db8dac20SDavid S. Miller 	if (unlikely(!up->pending)) {
1145db8dac20SDavid S. Miller 		release_sock(sk);
1146db8dac20SDavid S. Miller 
1147ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 3\n");
1148db8dac20SDavid S. Miller 		return -EINVAL;
1149db8dac20SDavid S. Miller 	}
1150db8dac20SDavid S. Miller 
1151f5fca608SDavid S. Miller 	ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
1152f5fca608SDavid S. Miller 			     page, offset, size, flags);
1153db8dac20SDavid S. Miller 	if (ret == -EOPNOTSUPP) {
1154db8dac20SDavid S. Miller 		release_sock(sk);
1155db8dac20SDavid S. Miller 		return sock_no_sendpage(sk->sk_socket, page, offset,
1156db8dac20SDavid S. Miller 					size, flags);
1157db8dac20SDavid S. Miller 	}
1158db8dac20SDavid S. Miller 	if (ret < 0) {
1159db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1160db8dac20SDavid S. Miller 		goto out;
1161db8dac20SDavid S. Miller 	}
1162db8dac20SDavid S. Miller 
1163db8dac20SDavid S. Miller 	up->len += size;
1164db8dac20SDavid S. Miller 	if (!(up->corkflag || (flags&MSG_MORE)))
1165db8dac20SDavid S. Miller 		ret = udp_push_pending_frames(sk);
1166db8dac20SDavid S. Miller 	if (!ret)
1167db8dac20SDavid S. Miller 		ret = size;
1168db8dac20SDavid S. Miller out:
1169db8dac20SDavid S. Miller 	release_sock(sk);
1170db8dac20SDavid S. Miller 	return ret;
1171db8dac20SDavid S. Miller }
1172db8dac20SDavid S. Miller 
1173dce4551cSPaolo Abeni #define UDP_SKB_IS_STATELESS 0x80000000
1174dce4551cSPaolo Abeni 
1175b65ac446SPaolo Abeni static void udp_set_dev_scratch(struct sk_buff *skb)
1176b65ac446SPaolo Abeni {
1177dce4551cSPaolo Abeni 	struct udp_dev_scratch *scratch = udp_skb_scratch(skb);
1178b65ac446SPaolo Abeni 
1179b65ac446SPaolo Abeni 	BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long));
1180dce4551cSPaolo Abeni 	scratch->_tsize_state = skb->truesize;
1181dce4551cSPaolo Abeni #if BITS_PER_LONG == 64
1182b65ac446SPaolo Abeni 	scratch->len = skb->len;
1183b65ac446SPaolo Abeni 	scratch->csum_unnecessary = !!skb_csum_unnecessary(skb);
1184b65ac446SPaolo Abeni 	scratch->is_linear = !skb_is_nonlinear(skb);
1185b65ac446SPaolo Abeni #endif
11863bdefdf9SPaolo Abeni 	/* all head states execept sp (dst, sk, nf) are always cleared by
11873bdefdf9SPaolo Abeni 	 * udp_rcv() and we need to preserve secpath, if present, to eventually
11883bdefdf9SPaolo Abeni 	 * process IP_CMSG_PASSSEC at recvmsg() time
11893bdefdf9SPaolo Abeni 	 */
11903bdefdf9SPaolo Abeni 	if (likely(!skb_sec_path(skb)))
1191dce4551cSPaolo Abeni 		scratch->_tsize_state |= UDP_SKB_IS_STATELESS;
1192dce4551cSPaolo Abeni }
1193dce4551cSPaolo Abeni 
1194dce4551cSPaolo Abeni static int udp_skb_truesize(struct sk_buff *skb)
1195dce4551cSPaolo Abeni {
1196dce4551cSPaolo Abeni 	return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS;
1197dce4551cSPaolo Abeni }
1198dce4551cSPaolo Abeni 
1199dce4551cSPaolo Abeni static bool udp_skb_has_head_state(struct sk_buff *skb)
1200dce4551cSPaolo Abeni {
1201dce4551cSPaolo Abeni 	return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS);
1202dce4551cSPaolo Abeni }
1203b65ac446SPaolo Abeni 
12047c13f97fSPaolo Abeni /* fully reclaim rmem/fwd memory allocated for skb */
12056dfb4367SPaolo Abeni static void udp_rmem_release(struct sock *sk, int size, int partial,
12066dfb4367SPaolo Abeni 			     bool rx_queue_lock_held)
1207f970bd9eSPaolo Abeni {
12086b229cf7SEric Dumazet 	struct udp_sock *up = udp_sk(sk);
12092276f58aSPaolo Abeni 	struct sk_buff_head *sk_queue;
1210f970bd9eSPaolo Abeni 	int amt;
1211f970bd9eSPaolo Abeni 
12126b229cf7SEric Dumazet 	if (likely(partial)) {
12136b229cf7SEric Dumazet 		up->forward_deficit += size;
12146b229cf7SEric Dumazet 		size = up->forward_deficit;
12156b229cf7SEric Dumazet 		if (size < (sk->sk_rcvbuf >> 2) &&
12162276f58aSPaolo Abeni 		    !skb_queue_empty(&up->reader_queue))
12176b229cf7SEric Dumazet 			return;
12186b229cf7SEric Dumazet 	} else {
12196b229cf7SEric Dumazet 		size += up->forward_deficit;
12206b229cf7SEric Dumazet 	}
12216b229cf7SEric Dumazet 	up->forward_deficit = 0;
12226b229cf7SEric Dumazet 
12236dfb4367SPaolo Abeni 	/* acquire the sk_receive_queue for fwd allocated memory scheduling,
12246dfb4367SPaolo Abeni 	 * if the called don't held it already
12256dfb4367SPaolo Abeni 	 */
12262276f58aSPaolo Abeni 	sk_queue = &sk->sk_receive_queue;
12276dfb4367SPaolo Abeni 	if (!rx_queue_lock_held)
12282276f58aSPaolo Abeni 		spin_lock(&sk_queue->lock);
12292276f58aSPaolo Abeni 
12306dfb4367SPaolo Abeni 
1231f970bd9eSPaolo Abeni 	sk->sk_forward_alloc += size;
1232f970bd9eSPaolo Abeni 	amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1);
1233f970bd9eSPaolo Abeni 	sk->sk_forward_alloc -= amt;
1234f970bd9eSPaolo Abeni 
1235f970bd9eSPaolo Abeni 	if (amt)
1236f970bd9eSPaolo Abeni 		__sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT);
123702ab0d13SEric Dumazet 
123802ab0d13SEric Dumazet 	atomic_sub(size, &sk->sk_rmem_alloc);
12392276f58aSPaolo Abeni 
12402276f58aSPaolo Abeni 	/* this can save us from acquiring the rx queue lock on next receive */
12412276f58aSPaolo Abeni 	skb_queue_splice_tail_init(sk_queue, &up->reader_queue);
12422276f58aSPaolo Abeni 
12436dfb4367SPaolo Abeni 	if (!rx_queue_lock_held)
12442276f58aSPaolo Abeni 		spin_unlock(&sk_queue->lock);
1245f970bd9eSPaolo Abeni }
1246f970bd9eSPaolo Abeni 
12472276f58aSPaolo Abeni /* Note: called with reader_queue.lock held.
1248c84d9490SEric Dumazet  * Instead of using skb->truesize here, find a copy of it in skb->dev_scratch
1249c84d9490SEric Dumazet  * This avoids a cache line miss while receive_queue lock is held.
1250c84d9490SEric Dumazet  * Look at __udp_enqueue_schedule_skb() to find where this copy is done.
1251c84d9490SEric Dumazet  */
12527c13f97fSPaolo Abeni void udp_skb_destructor(struct sock *sk, struct sk_buff *skb)
1253f970bd9eSPaolo Abeni {
1254b65ac446SPaolo Abeni 	prefetch(&skb->data);
1255b65ac446SPaolo Abeni 	udp_rmem_release(sk, udp_skb_truesize(skb), 1, false);
1256f970bd9eSPaolo Abeni }
12577c13f97fSPaolo Abeni EXPORT_SYMBOL(udp_skb_destructor);
1258f970bd9eSPaolo Abeni 
12596dfb4367SPaolo Abeni /* as above, but the caller held the rx queue lock, too */
126064f5102dSColin Ian King static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb)
12616dfb4367SPaolo Abeni {
1262b65ac446SPaolo Abeni 	prefetch(&skb->data);
1263b65ac446SPaolo Abeni 	udp_rmem_release(sk, udp_skb_truesize(skb), 1, true);
12646dfb4367SPaolo Abeni }
12656dfb4367SPaolo Abeni 
12664b272750SEric Dumazet /* Idea of busylocks is to let producers grab an extra spinlock
12674b272750SEric Dumazet  * to relieve pressure on the receive_queue spinlock shared by consumer.
12684b272750SEric Dumazet  * Under flood, this means that only one producer can be in line
12694b272750SEric Dumazet  * trying to acquire the receive_queue spinlock.
12704b272750SEric Dumazet  * These busylock can be allocated on a per cpu manner, instead of a
12714b272750SEric Dumazet  * per socket one (that would consume a cache line per socket)
12724b272750SEric Dumazet  */
12734b272750SEric Dumazet static int udp_busylocks_log __read_mostly;
12744b272750SEric Dumazet static spinlock_t *udp_busylocks __read_mostly;
12754b272750SEric Dumazet 
12764b272750SEric Dumazet static spinlock_t *busylock_acquire(void *ptr)
12774b272750SEric Dumazet {
12784b272750SEric Dumazet 	spinlock_t *busy;
12794b272750SEric Dumazet 
12804b272750SEric Dumazet 	busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log);
12814b272750SEric Dumazet 	spin_lock(busy);
12824b272750SEric Dumazet 	return busy;
12834b272750SEric Dumazet }
12844b272750SEric Dumazet 
12854b272750SEric Dumazet static void busylock_release(spinlock_t *busy)
12864b272750SEric Dumazet {
12874b272750SEric Dumazet 	if (busy)
12884b272750SEric Dumazet 		spin_unlock(busy);
12894b272750SEric Dumazet }
12904b272750SEric Dumazet 
1291f970bd9eSPaolo Abeni int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
1292f970bd9eSPaolo Abeni {
1293f970bd9eSPaolo Abeni 	struct sk_buff_head *list = &sk->sk_receive_queue;
1294f970bd9eSPaolo Abeni 	int rmem, delta, amt, err = -ENOMEM;
12954b272750SEric Dumazet 	spinlock_t *busy = NULL;
1296c8c8b127SEric Dumazet 	int size;
1297f970bd9eSPaolo Abeni 
1298f970bd9eSPaolo Abeni 	/* try to avoid the costly atomic add/sub pair when the receive
1299f970bd9eSPaolo Abeni 	 * queue is full; always allow at least a packet
1300f970bd9eSPaolo Abeni 	 */
1301f970bd9eSPaolo Abeni 	rmem = atomic_read(&sk->sk_rmem_alloc);
1302363dc73aSPaolo Abeni 	if (rmem > sk->sk_rcvbuf)
1303f970bd9eSPaolo Abeni 		goto drop;
1304f970bd9eSPaolo Abeni 
1305c8c8b127SEric Dumazet 	/* Under mem pressure, it might be helpful to help udp_recvmsg()
1306c8c8b127SEric Dumazet 	 * having linear skbs :
1307c8c8b127SEric Dumazet 	 * - Reduce memory overhead and thus increase receive queue capacity
1308c8c8b127SEric Dumazet 	 * - Less cache line misses at copyout() time
1309c8c8b127SEric Dumazet 	 * - Less work at consume_skb() (less alien page frag freeing)
1310c8c8b127SEric Dumazet 	 */
13114b272750SEric Dumazet 	if (rmem > (sk->sk_rcvbuf >> 1)) {
1312c8c8b127SEric Dumazet 		skb_condense(skb);
13134b272750SEric Dumazet 
13144b272750SEric Dumazet 		busy = busylock_acquire(sk);
13154b272750SEric Dumazet 	}
1316c8c8b127SEric Dumazet 	size = skb->truesize;
1317b65ac446SPaolo Abeni 	udp_set_dev_scratch(skb);
1318c8c8b127SEric Dumazet 
1319f970bd9eSPaolo Abeni 	/* we drop only if the receive buf is full and the receive
1320f970bd9eSPaolo Abeni 	 * queue contains some other skb
1321f970bd9eSPaolo Abeni 	 */
1322f970bd9eSPaolo Abeni 	rmem = atomic_add_return(size, &sk->sk_rmem_alloc);
1323363dc73aSPaolo Abeni 	if (rmem > (size + sk->sk_rcvbuf))
1324f970bd9eSPaolo Abeni 		goto uncharge_drop;
1325f970bd9eSPaolo Abeni 
1326f970bd9eSPaolo Abeni 	spin_lock(&list->lock);
1327f970bd9eSPaolo Abeni 	if (size >= sk->sk_forward_alloc) {
1328f970bd9eSPaolo Abeni 		amt = sk_mem_pages(size);
1329f970bd9eSPaolo Abeni 		delta = amt << SK_MEM_QUANTUM_SHIFT;
1330f970bd9eSPaolo Abeni 		if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) {
1331f970bd9eSPaolo Abeni 			err = -ENOBUFS;
1332f970bd9eSPaolo Abeni 			spin_unlock(&list->lock);
1333f970bd9eSPaolo Abeni 			goto uncharge_drop;
1334f970bd9eSPaolo Abeni 		}
1335f970bd9eSPaolo Abeni 
1336f970bd9eSPaolo Abeni 		sk->sk_forward_alloc += delta;
1337f970bd9eSPaolo Abeni 	}
1338f970bd9eSPaolo Abeni 
1339f970bd9eSPaolo Abeni 	sk->sk_forward_alloc -= size;
1340f970bd9eSPaolo Abeni 
13417c13f97fSPaolo Abeni 	/* no need to setup a destructor, we will explicitly release the
13427c13f97fSPaolo Abeni 	 * forward allocated memory on dequeue
13437c13f97fSPaolo Abeni 	 */
1344f970bd9eSPaolo Abeni 	sock_skb_set_dropcount(sk, skb);
1345f970bd9eSPaolo Abeni 
1346f970bd9eSPaolo Abeni 	__skb_queue_tail(list, skb);
1347f970bd9eSPaolo Abeni 	spin_unlock(&list->lock);
1348f970bd9eSPaolo Abeni 
1349f970bd9eSPaolo Abeni 	if (!sock_flag(sk, SOCK_DEAD))
1350f970bd9eSPaolo Abeni 		sk->sk_data_ready(sk);
1351f970bd9eSPaolo Abeni 
13524b272750SEric Dumazet 	busylock_release(busy);
1353f970bd9eSPaolo Abeni 	return 0;
1354f970bd9eSPaolo Abeni 
1355f970bd9eSPaolo Abeni uncharge_drop:
1356f970bd9eSPaolo Abeni 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
1357f970bd9eSPaolo Abeni 
1358f970bd9eSPaolo Abeni drop:
1359f970bd9eSPaolo Abeni 	atomic_inc(&sk->sk_drops);
13604b272750SEric Dumazet 	busylock_release(busy);
1361f970bd9eSPaolo Abeni 	return err;
1362f970bd9eSPaolo Abeni }
1363f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb);
1364f970bd9eSPaolo Abeni 
1365c915fe13SPaolo Abeni void udp_destruct_sock(struct sock *sk)
1366f970bd9eSPaolo Abeni {
1367f970bd9eSPaolo Abeni 	/* reclaim completely the forward allocated memory */
13682276f58aSPaolo Abeni 	struct udp_sock *up = udp_sk(sk);
13697c13f97fSPaolo Abeni 	unsigned int total = 0;
13707c13f97fSPaolo Abeni 	struct sk_buff *skb;
13717c13f97fSPaolo Abeni 
13722276f58aSPaolo Abeni 	skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue);
13732276f58aSPaolo Abeni 	while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) {
13747c13f97fSPaolo Abeni 		total += skb->truesize;
13757c13f97fSPaolo Abeni 		kfree_skb(skb);
13767c13f97fSPaolo Abeni 	}
13776dfb4367SPaolo Abeni 	udp_rmem_release(sk, total, 0, true);
13787c13f97fSPaolo Abeni 
1379f970bd9eSPaolo Abeni 	inet_sock_destruct(sk);
1380f970bd9eSPaolo Abeni }
1381c915fe13SPaolo Abeni EXPORT_SYMBOL_GPL(udp_destruct_sock);
1382f970bd9eSPaolo Abeni 
1383f970bd9eSPaolo Abeni int udp_init_sock(struct sock *sk)
1384f970bd9eSPaolo Abeni {
13852276f58aSPaolo Abeni 	skb_queue_head_init(&udp_sk(sk)->reader_queue);
1386f970bd9eSPaolo Abeni 	sk->sk_destruct = udp_destruct_sock;
1387f970bd9eSPaolo Abeni 	return 0;
1388f970bd9eSPaolo Abeni }
1389f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(udp_init_sock);
1390f970bd9eSPaolo Abeni 
1391f970bd9eSPaolo Abeni void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len)
1392f970bd9eSPaolo Abeni {
1393f970bd9eSPaolo Abeni 	if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) {
1394f970bd9eSPaolo Abeni 		bool slow = lock_sock_fast(sk);
1395f970bd9eSPaolo Abeni 
1396f970bd9eSPaolo Abeni 		sk_peek_offset_bwd(sk, len);
1397f970bd9eSPaolo Abeni 		unlock_sock_fast(sk, slow);
1398f970bd9eSPaolo Abeni 	}
13990a463c78SPaolo Abeni 
1400dce4551cSPaolo Abeni 	/* In the more common cases we cleared the head states previously,
1401dce4551cSPaolo Abeni 	 * see __udp_queue_rcv_skb().
14020ddf3fb2SPaolo Abeni 	 */
1403dce4551cSPaolo Abeni 	if (unlikely(udp_skb_has_head_state(skb)))
14040ddf3fb2SPaolo Abeni 		skb_release_head_state(skb);
14050a463c78SPaolo Abeni 	consume_stateless_skb(skb);
1406f970bd9eSPaolo Abeni }
1407f970bd9eSPaolo Abeni EXPORT_SYMBOL_GPL(skb_consume_udp);
1408f970bd9eSPaolo Abeni 
14092276f58aSPaolo Abeni static struct sk_buff *__first_packet_length(struct sock *sk,
14102276f58aSPaolo Abeni 					     struct sk_buff_head *rcvq,
14112276f58aSPaolo Abeni 					     int *total)
14122276f58aSPaolo Abeni {
14132276f58aSPaolo Abeni 	struct sk_buff *skb;
14142276f58aSPaolo Abeni 
14159bd780f5SPaolo Abeni 	while ((skb = skb_peek(rcvq)) != NULL) {
14169bd780f5SPaolo Abeni 		if (udp_lib_checksum_complete(skb)) {
14172276f58aSPaolo Abeni 			__UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
14182276f58aSPaolo Abeni 					IS_UDPLITE(sk));
14192276f58aSPaolo Abeni 			__UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
14202276f58aSPaolo Abeni 					IS_UDPLITE(sk));
14212276f58aSPaolo Abeni 			atomic_inc(&sk->sk_drops);
14222276f58aSPaolo Abeni 			__skb_unlink(skb, rcvq);
14232276f58aSPaolo Abeni 			*total += skb->truesize;
14242276f58aSPaolo Abeni 			kfree_skb(skb);
14259bd780f5SPaolo Abeni 		} else {
14269bd780f5SPaolo Abeni 			/* the csum related bits could be changed, refresh
14279bd780f5SPaolo Abeni 			 * the scratch area
14289bd780f5SPaolo Abeni 			 */
14299bd780f5SPaolo Abeni 			udp_set_dev_scratch(skb);
14309bd780f5SPaolo Abeni 			break;
14319bd780f5SPaolo Abeni 		}
14322276f58aSPaolo Abeni 	}
14332276f58aSPaolo Abeni 	return skb;
14342276f58aSPaolo Abeni }
14352276f58aSPaolo Abeni 
143685584672SEric Dumazet /**
143785584672SEric Dumazet  *	first_packet_length	- return length of first packet in receive queue
143885584672SEric Dumazet  *	@sk: socket
143985584672SEric Dumazet  *
144085584672SEric Dumazet  *	Drops all bad checksum frames, until a valid one is found.
1441e83c6744SEric Dumazet  *	Returns the length of found skb, or -1 if none is found.
144285584672SEric Dumazet  */
1443e83c6744SEric Dumazet static int first_packet_length(struct sock *sk)
144485584672SEric Dumazet {
14452276f58aSPaolo Abeni 	struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue;
14462276f58aSPaolo Abeni 	struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
144785584672SEric Dumazet 	struct sk_buff *skb;
14487c13f97fSPaolo Abeni 	int total = 0;
1449e83c6744SEric Dumazet 	int res;
145085584672SEric Dumazet 
145185584672SEric Dumazet 	spin_lock_bh(&rcvq->lock);
14522276f58aSPaolo Abeni 	skb = __first_packet_length(sk, rcvq, &total);
14532276f58aSPaolo Abeni 	if (!skb && !skb_queue_empty(sk_queue)) {
14542276f58aSPaolo Abeni 		spin_lock(&sk_queue->lock);
14552276f58aSPaolo Abeni 		skb_queue_splice_tail_init(sk_queue, rcvq);
14562276f58aSPaolo Abeni 		spin_unlock(&sk_queue->lock);
14572276f58aSPaolo Abeni 
14582276f58aSPaolo Abeni 		skb = __first_packet_length(sk, rcvq, &total);
145985584672SEric Dumazet 	}
1460e83c6744SEric Dumazet 	res = skb ? skb->len : -1;
14617c13f97fSPaolo Abeni 	if (total)
14626dfb4367SPaolo Abeni 		udp_rmem_release(sk, total, 1, false);
146385584672SEric Dumazet 	spin_unlock_bh(&rcvq->lock);
146485584672SEric Dumazet 	return res;
146585584672SEric Dumazet }
146685584672SEric Dumazet 
14671da177e4SLinus Torvalds /*
14681da177e4SLinus Torvalds  *	IOCTL requests applicable to the UDP protocol
14691da177e4SLinus Torvalds  */
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
14721da177e4SLinus Torvalds {
14736516c655SStephen Hemminger 	switch (cmd) {
14741da177e4SLinus Torvalds 	case SIOCOUTQ:
14751da177e4SLinus Torvalds 	{
147631e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
147731e6d363SEric Dumazet 
14781da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
14791da177e4SLinus Torvalds 	}
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	case SIOCINQ:
14821da177e4SLinus Torvalds 	{
1483e83c6744SEric Dumazet 		int amount = max_t(int, 0, first_packet_length(sk));
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
14861da177e4SLinus Torvalds 	}
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 	default:
14891da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
14901da177e4SLinus Torvalds 	}
14916516c655SStephen Hemminger 
14926516c655SStephen Hemminger 	return 0;
14931da177e4SLinus Torvalds }
1494c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl);
14951da177e4SLinus Torvalds 
14962276f58aSPaolo Abeni struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags,
14972276f58aSPaolo Abeni 			       int noblock, int *peeked, int *off, int *err)
14982276f58aSPaolo Abeni {
14992276f58aSPaolo Abeni 	struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
15002276f58aSPaolo Abeni 	struct sk_buff_head *queue;
15012276f58aSPaolo Abeni 	struct sk_buff *last;
15022276f58aSPaolo Abeni 	long timeo;
15032276f58aSPaolo Abeni 	int error;
15042276f58aSPaolo Abeni 
15052276f58aSPaolo Abeni 	queue = &udp_sk(sk)->reader_queue;
15062276f58aSPaolo Abeni 	flags |= noblock ? MSG_DONTWAIT : 0;
15072276f58aSPaolo Abeni 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
15082276f58aSPaolo Abeni 	do {
15092276f58aSPaolo Abeni 		struct sk_buff *skb;
15102276f58aSPaolo Abeni 
15112276f58aSPaolo Abeni 		error = sock_error(sk);
15122276f58aSPaolo Abeni 		if (error)
15132276f58aSPaolo Abeni 			break;
15142276f58aSPaolo Abeni 
15152276f58aSPaolo Abeni 		error = -EAGAIN;
15162276f58aSPaolo Abeni 		*peeked = 0;
15172276f58aSPaolo Abeni 		do {
15182276f58aSPaolo Abeni 			spin_lock_bh(&queue->lock);
15192276f58aSPaolo Abeni 			skb = __skb_try_recv_from_queue(sk, queue, flags,
15202276f58aSPaolo Abeni 							udp_skb_destructor,
1521de321ed3SAndrey Vagin 							peeked, off, err,
15222276f58aSPaolo Abeni 							&last);
15232276f58aSPaolo Abeni 			if (skb) {
15242276f58aSPaolo Abeni 				spin_unlock_bh(&queue->lock);
15252276f58aSPaolo Abeni 				return skb;
15262276f58aSPaolo Abeni 			}
15272276f58aSPaolo Abeni 
15282276f58aSPaolo Abeni 			if (skb_queue_empty(sk_queue)) {
15292276f58aSPaolo Abeni 				spin_unlock_bh(&queue->lock);
15302276f58aSPaolo Abeni 				goto busy_check;
15312276f58aSPaolo Abeni 			}
15322276f58aSPaolo Abeni 
15336dfb4367SPaolo Abeni 			/* refill the reader queue and walk it again
15346dfb4367SPaolo Abeni 			 * keep both queues locked to avoid re-acquiring
15356dfb4367SPaolo Abeni 			 * the sk_receive_queue lock if fwd memory scheduling
15366dfb4367SPaolo Abeni 			 * is needed.
15376dfb4367SPaolo Abeni 			 */
15382276f58aSPaolo Abeni 			spin_lock(&sk_queue->lock);
15392276f58aSPaolo Abeni 			skb_queue_splice_tail_init(sk_queue, queue);
15402276f58aSPaolo Abeni 
15412276f58aSPaolo Abeni 			skb = __skb_try_recv_from_queue(sk, queue, flags,
15426dfb4367SPaolo Abeni 							udp_skb_dtor_locked,
1543de321ed3SAndrey Vagin 							peeked, off, err,
15442276f58aSPaolo Abeni 							&last);
15456dfb4367SPaolo Abeni 			spin_unlock(&sk_queue->lock);
15462276f58aSPaolo Abeni 			spin_unlock_bh(&queue->lock);
1547de321ed3SAndrey Vagin 			if (skb)
15482276f58aSPaolo Abeni 				return skb;
15492276f58aSPaolo Abeni 
15502276f58aSPaolo Abeni busy_check:
15512276f58aSPaolo Abeni 			if (!sk_can_busy_loop(sk))
15522276f58aSPaolo Abeni 				break;
15532276f58aSPaolo Abeni 
15542276f58aSPaolo Abeni 			sk_busy_loop(sk, flags & MSG_DONTWAIT);
15552276f58aSPaolo Abeni 		} while (!skb_queue_empty(sk_queue));
15562276f58aSPaolo Abeni 
15572276f58aSPaolo Abeni 		/* sk_queue is empty, reader_queue may contain peeked packets */
15582276f58aSPaolo Abeni 	} while (timeo &&
15592276f58aSPaolo Abeni 		 !__skb_wait_for_more_packets(sk, &error, &timeo,
15602276f58aSPaolo Abeni 					      (struct sk_buff *)sk_queue));
15612276f58aSPaolo Abeni 
15622276f58aSPaolo Abeni 	*err = error;
15632276f58aSPaolo Abeni 	return NULL;
15642276f58aSPaolo Abeni }
15652276f58aSPaolo Abeni EXPORT_SYMBOL_GPL(__skb_recv_udp);
15662276f58aSPaolo Abeni 
1567db8dac20SDavid S. Miller /*
1568db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
1569db8dac20SDavid S. Miller  * 	return it, otherwise we block.
1570db8dac20SDavid S. Miller  */
1571db8dac20SDavid S. Miller 
15721b784140SYing Xue int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
15731b784140SYing Xue 		int flags, int *addr_len)
1574db8dac20SDavid S. Miller {
1575db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1576342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
1577db8dac20SDavid S. Miller 	struct sk_buff *skb;
157859c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
1579627d2d6bSsamanthakumar 	int peeked, peeking, off;
1580db8dac20SDavid S. Miller 	int err;
1581db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1582197c949eSEric Dumazet 	bool checksum_valid = false;
1583db8dac20SDavid S. Miller 
1584db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
158585fbaa75SHannes Frederic Sowa 		return ip_recv_error(sk, msg, len, addr_len);
1586db8dac20SDavid S. Miller 
1587db8dac20SDavid S. Miller try_again:
1588627d2d6bSsamanthakumar 	peeking = off = sk_peek_offset(sk, flags);
15897c13f97fSPaolo Abeni 	skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err);
1590db8dac20SDavid S. Miller 	if (!skb)
1591627d2d6bSsamanthakumar 		return err;
1592db8dac20SDavid S. Miller 
1593b65ac446SPaolo Abeni 	ulen = udp_skb_len(skb);
159459c2cdaeSDavid S. Miller 	copied = len;
1595627d2d6bSsamanthakumar 	if (copied > ulen - off)
1596627d2d6bSsamanthakumar 		copied = ulen - off;
159759c2cdaeSDavid S. Miller 	else if (copied < ulen)
1598db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
1599db8dac20SDavid S. Miller 
1600db8dac20SDavid S. Miller 	/*
1601db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
1602db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
1603db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
1604db8dac20SDavid S. Miller 	 */
1605db8dac20SDavid S. Miller 
1606d21dbdfeSEric Dumazet 	if (copied < ulen || peeking ||
1607d21dbdfeSEric Dumazet 	    (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
1608b65ac446SPaolo Abeni 		checksum_valid = udp_skb_csum_unnecessary(skb) ||
1609b65ac446SPaolo Abeni 				!__udp_lib_checksum_complete(skb);
1610197c949eSEric Dumazet 		if (!checksum_valid)
1611db8dac20SDavid S. Miller 			goto csum_copy_err;
1612db8dac20SDavid S. Miller 	}
1613db8dac20SDavid S. Miller 
1614b65ac446SPaolo Abeni 	if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
1615b65ac446SPaolo Abeni 		if (udp_skb_is_linear(skb))
1616b65ac446SPaolo Abeni 			err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
1617b65ac446SPaolo Abeni 		else
1618627d2d6bSsamanthakumar 			err = skb_copy_datagram_msg(skb, off, msg, copied);
1619b65ac446SPaolo Abeni 	} else {
1620627d2d6bSsamanthakumar 		err = skb_copy_and_csum_datagram_msg(skb, off, msg);
1621db8dac20SDavid S. Miller 
1622db8dac20SDavid S. Miller 		if (err == -EINVAL)
1623db8dac20SDavid S. Miller 			goto csum_copy_err;
1624db8dac20SDavid S. Miller 	}
1625db8dac20SDavid S. Miller 
162622911fc5SEric Dumazet 	if (unlikely(err)) {
1627979402b1SEric Dumazet 		if (!peeked) {
1628979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
16296aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk),
1630979402b1SEric Dumazet 				      UDP_MIB_INERRORS, is_udplite);
1631979402b1SEric Dumazet 		}
1632850cbaddSPaolo Abeni 		kfree_skb(skb);
1633627d2d6bSsamanthakumar 		return err;
163422911fc5SEric Dumazet 	}
1635db8dac20SDavid S. Miller 
1636db8dac20SDavid S. Miller 	if (!peeked)
16376aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk),
1638629ca23cSPavel Emelyanov 			      UDP_MIB_INDATAGRAMS, is_udplite);
1639db8dac20SDavid S. Miller 
16403b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
1641db8dac20SDavid S. Miller 
1642db8dac20SDavid S. Miller 	/* Copy the address. */
1643c482c568SEric Dumazet 	if (sin) {
1644db8dac20SDavid S. Miller 		sin->sin_family = AF_INET;
1645db8dac20SDavid S. Miller 		sin->sin_port = udp_hdr(skb)->source;
1646db8dac20SDavid S. Miller 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1647db8dac20SDavid S. Miller 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1648bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin);
1649db8dac20SDavid S. Miller 	}
1650db8dac20SDavid S. Miller 	if (inet->cmsg_flags)
1651ad959036SPaolo Abeni 		ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off);
1652db8dac20SDavid S. Miller 
165359c2cdaeSDavid S. Miller 	err = copied;
1654db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
1655db8dac20SDavid S. Miller 		err = ulen;
1656db8dac20SDavid S. Miller 
1657850cbaddSPaolo Abeni 	skb_consume_udp(sk, skb, peeking ? -err : err);
1658db8dac20SDavid S. Miller 	return err;
1659db8dac20SDavid S. Miller 
1660db8dac20SDavid S. Miller csum_copy_err:
16612276f58aSPaolo Abeni 	if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
16622276f58aSPaolo Abeni 				 udp_skb_destructor)) {
16636aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
16646aef70a8SEric Dumazet 		UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
16656a5dc9e5SEric Dumazet 	}
1666850cbaddSPaolo Abeni 	kfree_skb(skb);
1667db8dac20SDavid S. Miller 
1668beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
1669beb39db5SEric Dumazet 	cond_resched();
16709cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
1671db8dac20SDavid S. Miller 	goto try_again;
1672db8dac20SDavid S. Miller }
1673db8dac20SDavid S. Miller 
1674286c72deSEric Dumazet int __udp_disconnect(struct sock *sk, int flags)
16751da177e4SLinus Torvalds {
16761da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
16771da177e4SLinus Torvalds 	/*
16781da177e4SLinus Torvalds 	 *	1003.1g - break association.
16791da177e4SLinus Torvalds 	 */
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
1682c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
1683c720c7e8SEric Dumazet 	inet->inet_dport = 0;
1684bdeab991STom Herbert 	sock_rps_reset_rxhash(sk);
16851da177e4SLinus Torvalds 	sk->sk_bound_dev_if = 0;
16861da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
16871da177e4SLinus Torvalds 		inet_reset_saddr(sk);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
16901da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1691c720c7e8SEric Dumazet 		inet->inet_sport = 0;
16921da177e4SLinus Torvalds 	}
16931da177e4SLinus Torvalds 	sk_dst_reset(sk);
16941da177e4SLinus Torvalds 	return 0;
16951da177e4SLinus Torvalds }
1696286c72deSEric Dumazet EXPORT_SYMBOL(__udp_disconnect);
1697286c72deSEric Dumazet 
1698286c72deSEric Dumazet int udp_disconnect(struct sock *sk, int flags)
1699286c72deSEric Dumazet {
1700286c72deSEric Dumazet 	lock_sock(sk);
1701286c72deSEric Dumazet 	__udp_disconnect(sk, flags);
1702286c72deSEric Dumazet 	release_sock(sk);
1703286c72deSEric Dumazet 	return 0;
1704286c72deSEric Dumazet }
1705c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect);
17061da177e4SLinus Torvalds 
1707645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk)
1708645ca708SEric Dumazet {
1709723b4610SEric Dumazet 	if (sk_hashed(sk)) {
1710645ca708SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1711512615b6SEric Dumazet 		struct udp_hslot *hslot, *hslot2;
1712512615b6SEric Dumazet 
1713512615b6SEric Dumazet 		hslot  = udp_hashslot(udptable, sock_net(sk),
1714d4cada4aSEric Dumazet 				      udp_sk(sk)->udp_port_hash);
1715512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1716645ca708SEric Dumazet 
1717c8db3fecSEric Dumazet 		spin_lock_bh(&hslot->lock);
1718e32ea7e7SCraig Gallek 		if (rcu_access_pointer(sk->sk_reuseport_cb))
1719e32ea7e7SCraig Gallek 			reuseport_detach_sock(sk);
1720ca065d0cSEric Dumazet 		if (sk_del_node_init_rcu(sk)) {
1721fdcc8aa9SEric Dumazet 			hslot->count--;
1722c720c7e8SEric Dumazet 			inet_sk(sk)->inet_num = 0;
1723645ca708SEric Dumazet 			sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1724512615b6SEric Dumazet 
1725512615b6SEric Dumazet 			spin_lock(&hslot2->lock);
1726ca065d0cSEric Dumazet 			hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1727512615b6SEric Dumazet 			hslot2->count--;
1728512615b6SEric Dumazet 			spin_unlock(&hslot2->lock);
1729645ca708SEric Dumazet 		}
1730c8db3fecSEric Dumazet 		spin_unlock_bh(&hslot->lock);
1731645ca708SEric Dumazet 	}
1732723b4610SEric Dumazet }
1733645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash);
1734645ca708SEric Dumazet 
1735719f8358SEric Dumazet /*
1736719f8358SEric Dumazet  * inet_rcv_saddr was changed, we must rehash secondary hash
1737719f8358SEric Dumazet  */
1738719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash)
1739719f8358SEric Dumazet {
1740719f8358SEric Dumazet 	if (sk_hashed(sk)) {
1741719f8358SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1742719f8358SEric Dumazet 		struct udp_hslot *hslot, *hslot2, *nhslot2;
1743719f8358SEric Dumazet 
1744719f8358SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1745719f8358SEric Dumazet 		nhslot2 = udp_hashslot2(udptable, newhash);
1746719f8358SEric Dumazet 		udp_sk(sk)->udp_portaddr_hash = newhash;
1747e32ea7e7SCraig Gallek 
1748e32ea7e7SCraig Gallek 		if (hslot2 != nhslot2 ||
1749e32ea7e7SCraig Gallek 		    rcu_access_pointer(sk->sk_reuseport_cb)) {
1750719f8358SEric Dumazet 			hslot = udp_hashslot(udptable, sock_net(sk),
1751719f8358SEric Dumazet 					     udp_sk(sk)->udp_port_hash);
1752719f8358SEric Dumazet 			/* we must lock primary chain too */
1753719f8358SEric Dumazet 			spin_lock_bh(&hslot->lock);
1754e32ea7e7SCraig Gallek 			if (rcu_access_pointer(sk->sk_reuseport_cb))
1755e32ea7e7SCraig Gallek 				reuseport_detach_sock(sk);
1756719f8358SEric Dumazet 
1757e32ea7e7SCraig Gallek 			if (hslot2 != nhslot2) {
1758719f8358SEric Dumazet 				spin_lock(&hslot2->lock);
1759ca065d0cSEric Dumazet 				hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1760719f8358SEric Dumazet 				hslot2->count--;
1761719f8358SEric Dumazet 				spin_unlock(&hslot2->lock);
1762719f8358SEric Dumazet 
1763719f8358SEric Dumazet 				spin_lock(&nhslot2->lock);
1764ca065d0cSEric Dumazet 				hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
1765719f8358SEric Dumazet 							 &nhslot2->head);
1766719f8358SEric Dumazet 				nhslot2->count++;
1767719f8358SEric Dumazet 				spin_unlock(&nhslot2->lock);
1768e32ea7e7SCraig Gallek 			}
1769719f8358SEric Dumazet 
1770719f8358SEric Dumazet 			spin_unlock_bh(&hslot->lock);
1771719f8358SEric Dumazet 		}
1772719f8358SEric Dumazet 	}
1773719f8358SEric Dumazet }
1774719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash);
1775719f8358SEric Dumazet 
1776719f8358SEric Dumazet static void udp_v4_rehash(struct sock *sk)
1777719f8358SEric Dumazet {
1778719f8358SEric Dumazet 	u16 new_hash = udp4_portaddr_hash(sock_net(sk),
1779719f8358SEric Dumazet 					  inet_sk(sk)->inet_rcv_saddr,
1780719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
1781719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
1782719f8358SEric Dumazet }
1783719f8358SEric Dumazet 
1784a3f96c47SPaolo Abeni static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
178593821778SHerbert Xu {
1786fec5e652STom Herbert 	int rc;
178793821778SHerbert Xu 
1788005ec974SShawn Bohrer 	if (inet_sk(sk)->inet_daddr) {
1789bdeab991STom Herbert 		sock_rps_save_rxhash(sk, skb);
1790005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
17912c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
1792e68b6e50SEric Dumazet 	} else {
1793e68b6e50SEric Dumazet 		sk_mark_napi_id_once(sk, skb);
1794005ec974SShawn Bohrer 	}
1795fec5e652STom Herbert 
1796850cbaddSPaolo Abeni 	rc = __udp_enqueue_schedule_skb(sk, skb);
1797766e9037SEric Dumazet 	if (rc < 0) {
1798766e9037SEric Dumazet 		int is_udplite = IS_UDPLITE(sk);
1799766e9037SEric Dumazet 
180093821778SHerbert Xu 		/* Note that an ENOMEM error is charged twice */
1801766e9037SEric Dumazet 		if (rc == -ENOMEM)
1802e61da9e2SEric Dumazet 			UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS,
180393821778SHerbert Xu 					is_udplite);
1804e61da9e2SEric Dumazet 		UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1805766e9037SEric Dumazet 		kfree_skb(skb);
1806296f7ea7SSatoru Moriya 		trace_udp_fail_queue_rcv_skb(rc, sk);
1807766e9037SEric Dumazet 		return -1;
180893821778SHerbert Xu 	}
180993821778SHerbert Xu 
181093821778SHerbert Xu 	return 0;
181193821778SHerbert Xu }
181293821778SHerbert Xu 
1813447167bfSEric Dumazet static struct static_key udp_encap_needed __read_mostly;
1814447167bfSEric Dumazet void udp_encap_enable(void)
1815447167bfSEric Dumazet {
1816447167bfSEric Dumazet 	if (!static_key_enabled(&udp_encap_needed))
1817447167bfSEric Dumazet 		static_key_slow_inc(&udp_encap_needed);
1818447167bfSEric Dumazet }
1819447167bfSEric Dumazet EXPORT_SYMBOL(udp_encap_enable);
1820447167bfSEric Dumazet 
1821db8dac20SDavid S. Miller /* returns:
1822db8dac20SDavid S. Miller  *  -1: error
1823db8dac20SDavid S. Miller  *   0: success
1824db8dac20SDavid S. Miller  *  >0: "udp encap" protocol resubmission
1825db8dac20SDavid S. Miller  *
1826db8dac20SDavid S. Miller  * Note that in the success and error cases, the skb is assumed to
1827db8dac20SDavid S. Miller  * have either been requeued or freed.
1828db8dac20SDavid S. Miller  */
1829a3f96c47SPaolo Abeni static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1830db8dac20SDavid S. Miller {
1831db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1832db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1833db8dac20SDavid S. Miller 
1834db8dac20SDavid S. Miller 	/*
1835db8dac20SDavid S. Miller 	 *	Charge it to the socket, dropping if the queue is full.
1836db8dac20SDavid S. Miller 	 */
1837db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1838db8dac20SDavid S. Miller 		goto drop;
1839db8dac20SDavid S. Miller 	nf_reset(skb);
1840db8dac20SDavid S. Miller 
1841447167bfSEric Dumazet 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
18420ad92ad0SEric Dumazet 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
18430ad92ad0SEric Dumazet 
1844db8dac20SDavid S. Miller 		/*
1845db8dac20SDavid S. Miller 		 * This is an encapsulation socket so pass the skb to
1846db8dac20SDavid S. Miller 		 * the socket's udp_encap_rcv() hook. Otherwise, just
1847db8dac20SDavid S. Miller 		 * fall through and pass this up the UDP socket.
1848db8dac20SDavid S. Miller 		 * up->encap_rcv() returns the following value:
1849db8dac20SDavid S. Miller 		 * =0 if skb was successfully passed to the encap
1850db8dac20SDavid S. Miller 		 *    handler or was discarded by it.
1851db8dac20SDavid S. Miller 		 * >0 if skb should be passed on to UDP.
1852db8dac20SDavid S. Miller 		 * <0 if skb should be resubmitted as proto -N
1853db8dac20SDavid S. Miller 		 */
1854db8dac20SDavid S. Miller 
1855db8dac20SDavid S. Miller 		/* if we're overly short, let UDP handle it */
18560ad92ad0SEric Dumazet 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
1857e5aed006SHannes Frederic Sowa 		if (encap_rcv) {
1858db8dac20SDavid S. Miller 			int ret;
1859db8dac20SDavid S. Miller 
18600a80966bSTom Herbert 			/* Verify checksum before giving to encap */
18610a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
18620a80966bSTom Herbert 				goto csum_error;
18630a80966bSTom Herbert 
18640ad92ad0SEric Dumazet 			ret = encap_rcv(sk, skb);
1865db8dac20SDavid S. Miller 			if (ret <= 0) {
186602c22347SEric Dumazet 				__UDP_INC_STATS(sock_net(sk),
18670283328eSPavel Emelyanov 						UDP_MIB_INDATAGRAMS,
1868db8dac20SDavid S. Miller 						is_udplite);
1869db8dac20SDavid S. Miller 				return -ret;
1870db8dac20SDavid S. Miller 			}
1871db8dac20SDavid S. Miller 		}
1872db8dac20SDavid S. Miller 
1873db8dac20SDavid S. Miller 		/* FALLTHROUGH -- it's a UDP Packet */
1874db8dac20SDavid S. Miller 	}
1875db8dac20SDavid S. Miller 
1876db8dac20SDavid S. Miller 	/*
1877db8dac20SDavid S. Miller 	 * 	UDP-Lite specific tests, ignored on UDP sockets
1878db8dac20SDavid S. Miller 	 */
1879db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
1880db8dac20SDavid S. Miller 
1881db8dac20SDavid S. Miller 		/*
1882db8dac20SDavid S. Miller 		 * MIB statistics other than incrementing the error count are
1883db8dac20SDavid S. Miller 		 * disabled for the following two types of errors: these depend
1884db8dac20SDavid S. Miller 		 * on the application settings, not on the functioning of the
1885db8dac20SDavid S. Miller 		 * protocol stack as such.
1886db8dac20SDavid S. Miller 		 *
1887db8dac20SDavid S. Miller 		 * RFC 3828 here recommends (sec 3.3): "There should also be a
1888db8dac20SDavid S. Miller 		 * way ... to ... at least let the receiving application block
1889db8dac20SDavid S. Miller 		 * delivery of packets with coverage values less than a value
1890db8dac20SDavid S. Miller 		 * provided by the application."
1891db8dac20SDavid S. Miller 		 */
1892db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
1893ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n",
1894db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
1895db8dac20SDavid S. Miller 			goto drop;
1896db8dac20SDavid S. Miller 		}
1897db8dac20SDavid S. Miller 		/* The next case involves violating the min. coverage requested
1898db8dac20SDavid S. Miller 		 * by the receiver. This is subtle: if receiver wants x and x is
1899db8dac20SDavid S. Miller 		 * greater than the buffersize/MTU then receiver will complain
1900db8dac20SDavid S. Miller 		 * that it wants x while sender emits packets of smaller size y.
1901db8dac20SDavid S. Miller 		 * Therefore the above ...()->partial_cov statement is essential.
1902db8dac20SDavid S. Miller 		 */
1903db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
1904ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n",
1905db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
1906db8dac20SDavid S. Miller 			goto drop;
1907db8dac20SDavid S. Miller 		}
1908db8dac20SDavid S. Miller 	}
1909db8dac20SDavid S. Miller 
1910dd99e425SPaolo Abeni 	prefetch(&sk->sk_rmem_alloc);
1911ce25d66aSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
1912ce25d66aSEric Dumazet 	    udp_lib_checksum_complete(skb))
19136a5dc9e5SEric Dumazet 			goto csum_error;
1914ce25d66aSEric Dumazet 
1915ba66bbe5SDaniel Borkmann 	if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
1916a6127697SMichal Kubeček 		goto drop;
1917db8dac20SDavid S. Miller 
1918e6afc8acSsamanthakumar 	udp_csum_pull_header(skb);
1919db8dac20SDavid S. Miller 
1920fbf8866dSShawn Bohrer 	ipv4_pktinfo_prepare(sk, skb);
1921850cbaddSPaolo Abeni 	return __udp_queue_rcv_skb(sk, skb);
1922db8dac20SDavid S. Miller 
19236a5dc9e5SEric Dumazet csum_error:
192402c22347SEric Dumazet 	__UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1925db8dac20SDavid S. Miller drop:
192602c22347SEric Dumazet 	__UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
19278edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
1928db8dac20SDavid S. Miller 	kfree_skb(skb);
1929db8dac20SDavid S. Miller 	return -1;
1930db8dac20SDavid S. Miller }
1931db8dac20SDavid S. Miller 
193297502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock
1933e47eb5dfSEric Dumazet  * For UDP, we use xchg() to guard against concurrent changes.
193497502231SEric Dumazet  */
1935c9f2c1aeSPaolo Abeni void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
1936421b3885SShawn Bohrer {
193797502231SEric Dumazet 	struct dst_entry *old;
1938421b3885SShawn Bohrer 
1939d24406c8SWei Wang 	if (dst_hold_safe(dst)) {
1940e47eb5dfSEric Dumazet 		old = xchg(&sk->sk_rx_dst, dst);
194197502231SEric Dumazet 		dst_release(old);
194297502231SEric Dumazet 	}
1943d24406c8SWei Wang }
1944c9f2c1aeSPaolo Abeni EXPORT_SYMBOL(udp_sk_rx_dst_set);
1945421b3885SShawn Bohrer 
1946db8dac20SDavid S. Miller /*
1947db8dac20SDavid S. Miller  *	Multicasts and broadcasts go to each listener.
1948db8dac20SDavid S. Miller  *
19491240d137SEric Dumazet  *	Note: called only from the BH handler context.
1950db8dac20SDavid S. Miller  */
1951e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
1952db8dac20SDavid S. Miller 				    struct udphdr  *uh,
1953db8dac20SDavid S. Miller 				    __be32 saddr, __be32 daddr,
195436cbb245SRick Jones 				    struct udp_table *udptable,
195536cbb245SRick Jones 				    int proto)
1956db8dac20SDavid S. Miller {
1957ca065d0cSEric Dumazet 	struct sock *sk, *first = NULL;
19585cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
19595cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
19602dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
1961ca065d0cSEric Dumazet 	unsigned int offset = offsetof(typeof(*sk), sk_node);
1962ca065d0cSEric Dumazet 	int dif = skb->dev->ifindex;
1963fb74c277SDavid Ahern 	int sdif = inet_sdif(skb);
1964ca065d0cSEric Dumazet 	struct hlist_node *node;
1965ca065d0cSEric Dumazet 	struct sk_buff *nskb;
19662dc41cffSDavid Held 
19672dc41cffSDavid Held 	if (use_hash2) {
19682dc41cffSDavid Held 		hash2_any = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum) &
196973e2d5e3SPablo Neira 			    udptable->mask;
197073e2d5e3SPablo Neira 		hash2 = udp4_portaddr_hash(net, daddr, hnum) & udptable->mask;
19712dc41cffSDavid Held start_lookup:
197273e2d5e3SPablo Neira 		hslot = &udptable->hash2[hash2];
19732dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
19742dc41cffSDavid Held 	}
1975db8dac20SDavid S. Miller 
1976ca065d0cSEric Dumazet 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
1977ca065d0cSEric Dumazet 		if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr,
1978fb74c277SDavid Ahern 					 uh->source, saddr, dif, sdif, hnum))
1979ca065d0cSEric Dumazet 			continue;
19801240d137SEric Dumazet 
1981ca065d0cSEric Dumazet 		if (!first) {
1982ca065d0cSEric Dumazet 			first = sk;
1983ca065d0cSEric Dumazet 			continue;
1984ca065d0cSEric Dumazet 		}
1985ca065d0cSEric Dumazet 		nskb = skb_clone(skb, GFP_ATOMIC);
1986ca065d0cSEric Dumazet 
1987ca065d0cSEric Dumazet 		if (unlikely(!nskb)) {
1988ca065d0cSEric Dumazet 			atomic_inc(&sk->sk_drops);
198902c22347SEric Dumazet 			__UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
1990ca065d0cSEric Dumazet 					IS_UDPLITE(sk));
199102c22347SEric Dumazet 			__UDP_INC_STATS(net, UDP_MIB_INERRORS,
1992ca065d0cSEric Dumazet 					IS_UDPLITE(sk));
1993ca065d0cSEric Dumazet 			continue;
1994ca065d0cSEric Dumazet 		}
1995ca065d0cSEric Dumazet 		if (udp_queue_rcv_skb(sk, nskb) > 0)
1996ca065d0cSEric Dumazet 			consume_skb(nskb);
1997ca065d0cSEric Dumazet 	}
19981240d137SEric Dumazet 
19992dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
20002dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
20012dc41cffSDavid Held 		hash2 = hash2_any;
20022dc41cffSDavid Held 		goto start_lookup;
20032dc41cffSDavid Held 	}
20042dc41cffSDavid Held 
2005ca065d0cSEric Dumazet 	if (first) {
2006ca065d0cSEric Dumazet 		if (udp_queue_rcv_skb(first, skb) > 0)
2007ca065d0cSEric Dumazet 			consume_skb(skb);
20081240d137SEric Dumazet 	} else {
2009ca065d0cSEric Dumazet 		kfree_skb(skb);
201002c22347SEric Dumazet 		__UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
201136cbb245SRick Jones 				proto == IPPROTO_UDPLITE);
20121240d137SEric Dumazet 	}
2013db8dac20SDavid S. Miller 	return 0;
2014db8dac20SDavid S. Miller }
2015db8dac20SDavid S. Miller 
2016db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success),
2017db8dac20SDavid S. Miller  * CHECKSUM_UNNECESSARY means, that no more checks are required.
2018db8dac20SDavid S. Miller  * Otherwise, csum completion requires chacksumming packet body,
2019db8dac20SDavid S. Miller  * including udp header and folding it to skb->csum.
2020db8dac20SDavid S. Miller  */
2021db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
2022db8dac20SDavid S. Miller 				 int proto)
2023db8dac20SDavid S. Miller {
2024db8dac20SDavid S. Miller 	int err;
2025db8dac20SDavid S. Miller 
2026db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
2027db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
2028db8dac20SDavid S. Miller 
2029db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
2030db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
2031db8dac20SDavid S. Miller 		if (err)
2032db8dac20SDavid S. Miller 			return err;
2033db8dac20SDavid S. Miller 	}
2034db8dac20SDavid S. Miller 
2035b46d9f62SHannes Frederic Sowa 	/* Note, we are only interested in != 0 or == 0, thus the
2036b46d9f62SHannes Frederic Sowa 	 * force to int.
2037b46d9f62SHannes Frederic Sowa 	 */
2038b46d9f62SHannes Frederic Sowa 	return (__force int)skb_checksum_init_zero_check(skb, proto, uh->check,
2039ed70fcfcSTom Herbert 							 inet_compute_pseudo);
2040db8dac20SDavid S. Miller }
2041db8dac20SDavid S. Miller 
2042db8dac20SDavid S. Miller /*
2043db8dac20SDavid S. Miller  *	All we need to do is get the socket, and then do a checksum.
2044db8dac20SDavid S. Miller  */
2045db8dac20SDavid S. Miller 
2046645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
2047db8dac20SDavid S. Miller 		   int proto)
2048db8dac20SDavid S. Miller {
2049db8dac20SDavid S. Miller 	struct sock *sk;
20507b5e56f9SJesper Dangaard Brouer 	struct udphdr *uh;
2051db8dac20SDavid S. Miller 	unsigned short ulen;
2052adf30907SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
20532783ef23SJesper Dangaard Brouer 	__be32 saddr, daddr;
20540283328eSPavel Emelyanov 	struct net *net = dev_net(skb->dev);
2055db8dac20SDavid S. Miller 
2056db8dac20SDavid S. Miller 	/*
2057db8dac20SDavid S. Miller 	 *  Validate the packet.
2058db8dac20SDavid S. Miller 	 */
2059db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
2060db8dac20SDavid S. Miller 		goto drop;		/* No space for header. */
2061db8dac20SDavid S. Miller 
20627b5e56f9SJesper Dangaard Brouer 	uh   = udp_hdr(skb);
2063db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
2064ccc2d97cSBjørn Mork 	saddr = ip_hdr(skb)->saddr;
2065ccc2d97cSBjørn Mork 	daddr = ip_hdr(skb)->daddr;
2066ccc2d97cSBjørn Mork 
2067db8dac20SDavid S. Miller 	if (ulen > skb->len)
2068db8dac20SDavid S. Miller 		goto short_packet;
2069db8dac20SDavid S. Miller 
2070db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
2071db8dac20SDavid S. Miller 		/* UDP validates ulen. */
2072db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
2073db8dac20SDavid S. Miller 			goto short_packet;
2074db8dac20SDavid S. Miller 		uh = udp_hdr(skb);
2075db8dac20SDavid S. Miller 	}
2076db8dac20SDavid S. Miller 
2077db8dac20SDavid S. Miller 	if (udp4_csum_init(skb, uh, proto))
2078db8dac20SDavid S. Miller 		goto csum_error;
2079db8dac20SDavid S. Miller 
20808afdd99aSEric Dumazet 	sk = skb_steal_sock(skb);
20818afdd99aSEric Dumazet 	if (sk) {
208297502231SEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
2083421b3885SShawn Bohrer 		int ret;
2084421b3885SShawn Bohrer 
208597502231SEric Dumazet 		if (unlikely(sk->sk_rx_dst != dst))
208697502231SEric Dumazet 			udp_sk_rx_dst_set(sk, dst);
2087421b3885SShawn Bohrer 
2088421b3885SShawn Bohrer 		ret = udp_queue_rcv_skb(sk, skb);
20898afdd99aSEric Dumazet 		sock_put(sk);
2090421b3885SShawn Bohrer 		/* a return value > 0 means to resubmit the input, but
2091421b3885SShawn Bohrer 		 * it wants the return to be -protocol, or 0
2092421b3885SShawn Bohrer 		 */
2093421b3885SShawn Bohrer 		if (ret > 0)
2094421b3885SShawn Bohrer 			return -ret;
2095421b3885SShawn Bohrer 		return 0;
2096c18450a5SFabian Frederick 	}
2097c18450a5SFabian Frederick 
2098db8dac20SDavid S. Miller 	if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
2099e3163493SPavel Emelyanov 		return __udp4_lib_mcast_deliver(net, skb, uh,
210036cbb245SRick Jones 						saddr, daddr, udptable, proto);
2101db8dac20SDavid S. Miller 
2102607c4aafSKOVACS Krisztian 	sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
210300db4124SIan Morris 	if (sk) {
2104a5b50476SEliezer Tamir 		int ret;
2105a5b50476SEliezer Tamir 
2106224d019cSTom Herbert 		if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
21072abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
21082abb7cdcSTom Herbert 						 inet_compute_pseudo);
21092abb7cdcSTom Herbert 
2110a5b50476SEliezer Tamir 		ret = udp_queue_rcv_skb(sk, skb);
2111db8dac20SDavid S. Miller 
2112db8dac20SDavid S. Miller 		/* a return value > 0 means to resubmit the input, but
2113db8dac20SDavid S. Miller 		 * it wants the return to be -protocol, or 0
2114db8dac20SDavid S. Miller 		 */
2115db8dac20SDavid S. Miller 		if (ret > 0)
2116db8dac20SDavid S. Miller 			return -ret;
2117db8dac20SDavid S. Miller 		return 0;
2118db8dac20SDavid S. Miller 	}
2119db8dac20SDavid S. Miller 
2120db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
2121db8dac20SDavid S. Miller 		goto drop;
2122db8dac20SDavid S. Miller 	nf_reset(skb);
2123db8dac20SDavid S. Miller 
2124db8dac20SDavid S. Miller 	/* No socket. Drop packet silently, if checksum is wrong */
2125db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
2126db8dac20SDavid S. Miller 		goto csum_error;
2127db8dac20SDavid S. Miller 
212802c22347SEric Dumazet 	__UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
2129db8dac20SDavid S. Miller 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
2130db8dac20SDavid S. Miller 
2131db8dac20SDavid S. Miller 	/*
2132db8dac20SDavid S. Miller 	 * Hmm.  We got an UDP packet to a port to which we
2133db8dac20SDavid S. Miller 	 * don't wanna listen.  Ignore it.
2134db8dac20SDavid S. Miller 	 */
2135db8dac20SDavid S. Miller 	kfree_skb(skb);
2136db8dac20SDavid S. Miller 	return 0;
2137db8dac20SDavid S. Miller 
2138db8dac20SDavid S. Miller short_packet:
2139ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
2140afd46503SJoe Perches 			    proto == IPPROTO_UDPLITE ? "Lite" : "",
2141afd46503SJoe Perches 			    &saddr, ntohs(uh->source),
2142afd46503SJoe Perches 			    ulen, skb->len,
2143afd46503SJoe Perches 			    &daddr, ntohs(uh->dest));
2144db8dac20SDavid S. Miller 	goto drop;
2145db8dac20SDavid S. Miller 
2146db8dac20SDavid S. Miller csum_error:
2147db8dac20SDavid S. Miller 	/*
2148db8dac20SDavid S. Miller 	 * RFC1122: OK.  Discards the bad packet silently (as far as
2149db8dac20SDavid S. Miller 	 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
2150db8dac20SDavid S. Miller 	 */
2151ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
2152afd46503SJoe Perches 			    proto == IPPROTO_UDPLITE ? "Lite" : "",
2153afd46503SJoe Perches 			    &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
2154db8dac20SDavid S. Miller 			    ulen);
215502c22347SEric Dumazet 	__UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
2156db8dac20SDavid S. Miller drop:
215702c22347SEric Dumazet 	__UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
2158db8dac20SDavid S. Miller 	kfree_skb(skb);
2159db8dac20SDavid S. Miller 	return 0;
2160db8dac20SDavid S. Miller }
2161db8dac20SDavid S. Miller 
2162421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket.
2163421b3885SShawn Bohrer  * If more than one socket found returns NULL
2164421b3885SShawn Bohrer  */
2165421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
2166421b3885SShawn Bohrer 						  __be16 loc_port, __be32 loc_addr,
2167421b3885SShawn Bohrer 						  __be16 rmt_port, __be32 rmt_addr,
2168fb74c277SDavid Ahern 						  int dif, int sdif)
2169421b3885SShawn Bohrer {
2170421b3885SShawn Bohrer 	struct sock *sk, *result;
2171421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
2172ca065d0cSEric Dumazet 	unsigned int slot = udp_hashfn(net, hnum, udp_table.mask);
2173421b3885SShawn Bohrer 	struct udp_hslot *hslot = &udp_table.hash[slot];
2174421b3885SShawn Bohrer 
217563c6f81cSEric Dumazet 	/* Do not bother scanning a too big list */
217663c6f81cSEric Dumazet 	if (hslot->count > 10)
217763c6f81cSEric Dumazet 		return NULL;
217863c6f81cSEric Dumazet 
2179421b3885SShawn Bohrer 	result = NULL;
2180ca065d0cSEric Dumazet 	sk_for_each_rcu(sk, &hslot->head) {
2181ca065d0cSEric Dumazet 		if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr,
2182fb74c277SDavid Ahern 					rmt_port, rmt_addr, dif, sdif, hnum)) {
2183ca065d0cSEric Dumazet 			if (result)
2184ca065d0cSEric Dumazet 				return NULL;
2185421b3885SShawn Bohrer 			result = sk;
2186421b3885SShawn Bohrer 		}
2187421b3885SShawn Bohrer 	}
2188421b3885SShawn Bohrer 
2189421b3885SShawn Bohrer 	return result;
2190421b3885SShawn Bohrer }
2191421b3885SShawn Bohrer 
2192421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can
2193421b3885SShawn Bohrer  * break forwarding setups.  The chains here can be long so only check
2194421b3885SShawn Bohrer  * if the first socket is an exact match and if not move on.
2195421b3885SShawn Bohrer  */
2196421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net,
2197421b3885SShawn Bohrer 					    __be16 loc_port, __be32 loc_addr,
2198421b3885SShawn Bohrer 					    __be16 rmt_port, __be32 rmt_addr,
21993fa6f616SDavid Ahern 					    int dif, int sdif)
2200421b3885SShawn Bohrer {
2201421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
2202421b3885SShawn Bohrer 	unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum);
2203421b3885SShawn Bohrer 	unsigned int slot2 = hash2 & udp_table.mask;
2204421b3885SShawn Bohrer 	struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
2205c7228317SJoe Perches 	INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
2206421b3885SShawn Bohrer 	const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
2207ca065d0cSEric Dumazet 	struct sock *sk;
2208421b3885SShawn Bohrer 
2209ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
2210ca065d0cSEric Dumazet 		if (INET_MATCH(sk, net, acookie, rmt_addr,
22113fa6f616SDavid Ahern 			       loc_addr, ports, dif, sdif))
2212ca065d0cSEric Dumazet 			return sk;
2213421b3885SShawn Bohrer 		/* Only check first socket in chain */
2214421b3885SShawn Bohrer 		break;
2215421b3885SShawn Bohrer 	}
2216ca065d0cSEric Dumazet 	return NULL;
2217421b3885SShawn Bohrer }
2218421b3885SShawn Bohrer 
2219421b3885SShawn Bohrer void udp_v4_early_demux(struct sk_buff *skb)
2220421b3885SShawn Bohrer {
2221610438b7SEric Dumazet 	struct net *net = dev_net(skb->dev);
2222610438b7SEric Dumazet 	const struct iphdr *iph;
2223610438b7SEric Dumazet 	const struct udphdr *uh;
2224ca065d0cSEric Dumazet 	struct sock *sk = NULL;
2225421b3885SShawn Bohrer 	struct dst_entry *dst;
2226421b3885SShawn Bohrer 	int dif = skb->dev->ifindex;
2227fb74c277SDavid Ahern 	int sdif = inet_sdif(skb);
22286e540309SShawn Bohrer 	int ours;
2229421b3885SShawn Bohrer 
2230421b3885SShawn Bohrer 	/* validate the packet */
2231421b3885SShawn Bohrer 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
2232421b3885SShawn Bohrer 		return;
2233421b3885SShawn Bohrer 
2234610438b7SEric Dumazet 	iph = ip_hdr(skb);
2235610438b7SEric Dumazet 	uh = udp_hdr(skb);
2236610438b7SEric Dumazet 
2237421b3885SShawn Bohrer 	if (skb->pkt_type == PACKET_BROADCAST ||
22386e540309SShawn Bohrer 	    skb->pkt_type == PACKET_MULTICAST) {
22396e540309SShawn Bohrer 		struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
22406e540309SShawn Bohrer 
22416e540309SShawn Bohrer 		if (!in_dev)
22426e540309SShawn Bohrer 			return;
22436e540309SShawn Bohrer 
2244ad0ea198SPaolo Abeni 		/* we are supposed to accept bcast packets */
2245ad0ea198SPaolo Abeni 		if (skb->pkt_type == PACKET_MULTICAST) {
22466e540309SShawn Bohrer 			ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr,
22476e540309SShawn Bohrer 					       iph->protocol);
22486e540309SShawn Bohrer 			if (!ours)
22496e540309SShawn Bohrer 				return;
2250ad0ea198SPaolo Abeni 		}
2251ad0ea198SPaolo Abeni 
2252421b3885SShawn Bohrer 		sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
2253fb74c277SDavid Ahern 						   uh->source, iph->saddr,
2254fb74c277SDavid Ahern 						   dif, sdif);
22556e540309SShawn Bohrer 	} else if (skb->pkt_type == PACKET_HOST) {
2256421b3885SShawn Bohrer 		sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
22573fa6f616SDavid Ahern 					     uh->source, iph->saddr, dif, sdif);
22586e540309SShawn Bohrer 	}
2259421b3885SShawn Bohrer 
226041c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
2261421b3885SShawn Bohrer 		return;
2262421b3885SShawn Bohrer 
2263421b3885SShawn Bohrer 	skb->sk = sk;
226482eabd9eSAlexander Duyck 	skb->destructor = sock_efree;
226510e2eb87SEric Dumazet 	dst = READ_ONCE(sk->sk_rx_dst);
2266421b3885SShawn Bohrer 
2267421b3885SShawn Bohrer 	if (dst)
2268421b3885SShawn Bohrer 		dst = dst_check(dst, 0);
226910e2eb87SEric Dumazet 	if (dst) {
2270d24406c8SWei Wang 		/* set noref for now.
2271d24406c8SWei Wang 		 * any place which wants to hold dst has to call
2272d24406c8SWei Wang 		 * dst_hold_safe()
2273d24406c8SWei Wang 		 */
2274421b3885SShawn Bohrer 		skb_dst_set_noref(skb, dst);
2275421b3885SShawn Bohrer 	}
227610e2eb87SEric Dumazet }
2277421b3885SShawn Bohrer 
2278db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb)
2279db8dac20SDavid S. Miller {
2280645ca708SEric Dumazet 	return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
2281db8dac20SDavid S. Miller }
2282db8dac20SDavid S. Miller 
22837d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk)
2284db8dac20SDavid S. Miller {
228544046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
22868a74ad60SEric Dumazet 	bool slow = lock_sock_fast(sk);
2287db8dac20SDavid S. Miller 	udp_flush_pending_frames(sk);
22888a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
228944046a59STom Parkin 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
229044046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
229144046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
229244046a59STom Parkin 		if (encap_destroy)
229344046a59STom Parkin 			encap_destroy(sk);
229444046a59STom Parkin 	}
2295db8dac20SDavid S. Miller }
2296db8dac20SDavid S. Miller 
22971da177e4SLinus Torvalds /*
22981da177e4SLinus Torvalds  *	Socket option code for UDP
22991da177e4SLinus Torvalds  */
23004c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname,
2301b7058842SDavid S. Miller 		       char __user *optval, unsigned int optlen,
23024c0a6cb0SGerrit Renker 		       int (*push_pending_frames)(struct sock *))
23031da177e4SLinus Torvalds {
23041da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
23051c19448cSTom Herbert 	int val, valbool;
23061da177e4SLinus Torvalds 	int err = 0;
2307b2bf1e26SWang Chen 	int is_udplite = IS_UDPLITE(sk);
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	if (optlen < sizeof(int))
23101da177e4SLinus Torvalds 		return -EINVAL;
23111da177e4SLinus Torvalds 
23121da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
23131da177e4SLinus Torvalds 		return -EFAULT;
23141da177e4SLinus Torvalds 
23151c19448cSTom Herbert 	valbool = val ? 1 : 0;
23161c19448cSTom Herbert 
23171da177e4SLinus Torvalds 	switch (optname) {
23181da177e4SLinus Torvalds 	case UDP_CORK:
23191da177e4SLinus Torvalds 		if (val != 0) {
23201da177e4SLinus Torvalds 			up->corkflag = 1;
23211da177e4SLinus Torvalds 		} else {
23221da177e4SLinus Torvalds 			up->corkflag = 0;
23231da177e4SLinus Torvalds 			lock_sock(sk);
23244243cdc2SJoe Perches 			push_pending_frames(sk);
23251da177e4SLinus Torvalds 			release_sock(sk);
23261da177e4SLinus Torvalds 		}
23271da177e4SLinus Torvalds 		break;
23281da177e4SLinus Torvalds 
23291da177e4SLinus Torvalds 	case UDP_ENCAP:
23301da177e4SLinus Torvalds 		switch (val) {
23311da177e4SLinus Torvalds 		case 0:
23321da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP:
23331da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP_NON_IKE:
2334067b207bSJames Chapman 			up->encap_rcv = xfrm4_udp_encap_rcv;
2335067b207bSJames Chapman 			/* FALLTHROUGH */
2336342f0234SJames Chapman 		case UDP_ENCAP_L2TPINUDP:
23371da177e4SLinus Torvalds 			up->encap_type = val;
2338447167bfSEric Dumazet 			udp_encap_enable();
23391da177e4SLinus Torvalds 			break;
23401da177e4SLinus Torvalds 		default:
23411da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
23421da177e4SLinus Torvalds 			break;
23431da177e4SLinus Torvalds 		}
23441da177e4SLinus Torvalds 		break;
23451da177e4SLinus Torvalds 
23461c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
23471c19448cSTom Herbert 		up->no_check6_tx = valbool;
23481c19448cSTom Herbert 		break;
23491c19448cSTom Herbert 
23501c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
23511c19448cSTom Herbert 		up->no_check6_rx = valbool;
23521c19448cSTom Herbert 		break;
23531c19448cSTom Herbert 
2354ba4e58ecSGerrit Renker 	/*
2355ba4e58ecSGerrit Renker 	 * 	UDP-Lite's partial checksum coverage (RFC 3828).
2356ba4e58ecSGerrit Renker 	 */
2357ba4e58ecSGerrit Renker 	/* The sender sets actual checksum coverage length via this option.
2358ba4e58ecSGerrit Renker 	 * The case coverage > packet length is handled by send module. */
2359ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2360b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2361ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2362ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
2363ba4e58ecSGerrit Renker 			val = 8;
23644be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
23654be929beSAlexey Dobriyan 			val = USHRT_MAX;
2366ba4e58ecSGerrit Renker 		up->pcslen = val;
2367ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_SEND_CC;
2368ba4e58ecSGerrit Renker 		break;
2369ba4e58ecSGerrit Renker 
2370ba4e58ecSGerrit Renker 	/* The receiver specifies a minimum checksum coverage value. To make
2371ba4e58ecSGerrit Renker 	 * sense, this should be set to at least 8 (as done below). If zero is
2372ba4e58ecSGerrit Renker 	 * used, this again means full checksum coverage.                     */
2373ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2374b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2375ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2376ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Avoid silly minimal values.       */
2377ba4e58ecSGerrit Renker 			val = 8;
23784be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
23794be929beSAlexey Dobriyan 			val = USHRT_MAX;
2380ba4e58ecSGerrit Renker 		up->pcrlen = val;
2381ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_RECV_CC;
2382ba4e58ecSGerrit Renker 		break;
2383ba4e58ecSGerrit Renker 
23841da177e4SLinus Torvalds 	default:
23851da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
23861da177e4SLinus Torvalds 		break;
23876516c655SStephen Hemminger 	}
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds 	return err;
23901da177e4SLinus Torvalds }
2391c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt);
23921da177e4SLinus Torvalds 
2393db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname,
2394b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
2395db8dac20SDavid S. Miller {
2396db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2397db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2398db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2399db8dac20SDavid S. Miller 	return ip_setsockopt(sk, level, optname, optval, optlen);
2400db8dac20SDavid S. Miller }
2401db8dac20SDavid S. Miller 
2402db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2403db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname,
2404b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
2405db8dac20SDavid S. Miller {
2406db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2407db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2408db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2409db8dac20SDavid S. Miller 	return compat_ip_setsockopt(sk, level, optname, optval, optlen);
2410db8dac20SDavid S. Miller }
2411db8dac20SDavid S. Miller #endif
2412db8dac20SDavid S. Miller 
24134c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname,
24141da177e4SLinus Torvalds 		       char __user *optval, int __user *optlen)
24151da177e4SLinus Torvalds {
24161da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
24171da177e4SLinus Torvalds 	int val, len;
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 	if (get_user(len, optlen))
24201da177e4SLinus Torvalds 		return -EFAULT;
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
24231da177e4SLinus Torvalds 
24241da177e4SLinus Torvalds 	if (len < 0)
24251da177e4SLinus Torvalds 		return -EINVAL;
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds 	switch (optname) {
24281da177e4SLinus Torvalds 	case UDP_CORK:
24291da177e4SLinus Torvalds 		val = up->corkflag;
24301da177e4SLinus Torvalds 		break;
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 	case UDP_ENCAP:
24331da177e4SLinus Torvalds 		val = up->encap_type;
24341da177e4SLinus Torvalds 		break;
24351da177e4SLinus Torvalds 
24361c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
24371c19448cSTom Herbert 		val = up->no_check6_tx;
24381c19448cSTom Herbert 		break;
24391c19448cSTom Herbert 
24401c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
24411c19448cSTom Herbert 		val = up->no_check6_rx;
24421c19448cSTom Herbert 		break;
24431c19448cSTom Herbert 
2444ba4e58ecSGerrit Renker 	/* The following two cannot be changed on UDP sockets, the return is
2445ba4e58ecSGerrit Renker 	 * always 0 (which corresponds to the full checksum coverage of UDP). */
2446ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2447ba4e58ecSGerrit Renker 		val = up->pcslen;
2448ba4e58ecSGerrit Renker 		break;
2449ba4e58ecSGerrit Renker 
2450ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2451ba4e58ecSGerrit Renker 		val = up->pcrlen;
2452ba4e58ecSGerrit Renker 		break;
2453ba4e58ecSGerrit Renker 
24541da177e4SLinus Torvalds 	default:
24551da177e4SLinus Torvalds 		return -ENOPROTOOPT;
24566516c655SStephen Hemminger 	}
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds 	if (put_user(len, optlen))
24591da177e4SLinus Torvalds 		return -EFAULT;
24601da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
24611da177e4SLinus Torvalds 		return -EFAULT;
24621da177e4SLinus Torvalds 	return 0;
24631da177e4SLinus Torvalds }
2464c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt);
24651da177e4SLinus Torvalds 
2466db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname,
2467db8dac20SDavid S. Miller 		   char __user *optval, int __user *optlen)
2468db8dac20SDavid S. Miller {
2469db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2470db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2471db8dac20SDavid S. Miller 	return ip_getsockopt(sk, level, optname, optval, optlen);
2472db8dac20SDavid S. Miller }
2473db8dac20SDavid S. Miller 
2474db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2475db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname,
2476db8dac20SDavid S. Miller 				 char __user *optval, int __user *optlen)
2477db8dac20SDavid S. Miller {
2478db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2479db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2480db8dac20SDavid S. Miller 	return compat_ip_getsockopt(sk, level, optname, optval, optlen);
2481db8dac20SDavid S. Miller }
2482db8dac20SDavid S. Miller #endif
24831da177e4SLinus Torvalds /**
24841da177e4SLinus Torvalds  * 	udp_poll - wait for a UDP event.
24851da177e4SLinus Torvalds  *	@file - file struct
24861da177e4SLinus Torvalds  *	@sock - socket
24871da177e4SLinus Torvalds  *	@wait - poll table
24881da177e4SLinus Torvalds  *
24891da177e4SLinus Torvalds  *	This is same as datagram poll, except for the special case of
24901da177e4SLinus Torvalds  *	blocking sockets. If application is using a blocking fd
24911da177e4SLinus Torvalds  *	and a packet with checksum error is in the queue;
24921da177e4SLinus Torvalds  *	then it could get return from select indicating data available
24931da177e4SLinus Torvalds  *	but then block when reading it. Add special case code
24941da177e4SLinus Torvalds  *	to work around these arguably broken applications.
24951da177e4SLinus Torvalds  */
24961da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
24971da177e4SLinus Torvalds {
24981da177e4SLinus Torvalds 	unsigned int mask = datagram_poll(file, sock, wait);
24991da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
25001da177e4SLinus Torvalds 
25012276f58aSPaolo Abeni 	if (!skb_queue_empty(&udp_sk(sk)->reader_queue))
25022276f58aSPaolo Abeni 		mask |= POLLIN | POLLRDNORM;
25032276f58aSPaolo Abeni 
2504c3f1dbafSDavid Majnemer 	sock_rps_record_flow(sk);
2505c3f1dbafSDavid Majnemer 
25061da177e4SLinus Torvalds 	/* Check for false positives due to checksum errors */
250785584672SEric Dumazet 	if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
2508e83c6744SEric Dumazet 	    !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1)
25091da177e4SLinus Torvalds 		mask &= ~(POLLIN | POLLRDNORM);
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 	return mask;
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds }
2514c482c568SEric Dumazet EXPORT_SYMBOL(udp_poll);
25151da177e4SLinus Torvalds 
25165d77dca8SDavid Ahern int udp_abort(struct sock *sk, int err)
25175d77dca8SDavid Ahern {
25185d77dca8SDavid Ahern 	lock_sock(sk);
25195d77dca8SDavid Ahern 
25205d77dca8SDavid Ahern 	sk->sk_err = err;
25215d77dca8SDavid Ahern 	sk->sk_error_report(sk);
2522286c72deSEric Dumazet 	__udp_disconnect(sk, 0);
25235d77dca8SDavid Ahern 
25245d77dca8SDavid Ahern 	release_sock(sk);
25255d77dca8SDavid Ahern 
25265d77dca8SDavid Ahern 	return 0;
25275d77dca8SDavid Ahern }
25285d77dca8SDavid Ahern EXPORT_SYMBOL_GPL(udp_abort);
25295d77dca8SDavid Ahern 
2530db8dac20SDavid S. Miller struct proto udp_prot = {
2531db8dac20SDavid S. Miller 	.name		   = "UDP",
2532db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
2533db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
2534db8dac20SDavid S. Miller 	.connect	   = ip4_datagram_connect,
2535db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
2536db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
2537850cbaddSPaolo Abeni 	.init		   = udp_init_sock,
2538db8dac20SDavid S. Miller 	.destroy	   = udp_destroy_sock,
2539db8dac20SDavid S. Miller 	.setsockopt	   = udp_setsockopt,
2540db8dac20SDavid S. Miller 	.getsockopt	   = udp_getsockopt,
2541db8dac20SDavid S. Miller 	.sendmsg	   = udp_sendmsg,
2542db8dac20SDavid S. Miller 	.recvmsg	   = udp_recvmsg,
2543db8dac20SDavid S. Miller 	.sendpage	   = udp_sendpage,
25448141ed9fSSteffen Klassert 	.release_cb	   = ip4_datagram_release_cb,
2545db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
2546db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
2547719f8358SEric Dumazet 	.rehash		   = udp_v4_rehash,
2548db8dac20SDavid S. Miller 	.get_port	   = udp_v4_get_port,
2549db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
2550db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
2551db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
2552db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
2553db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp_sock),
2554645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
2555db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2556db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udp_setsockopt,
2557db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udp_getsockopt,
2558db8dac20SDavid S. Miller #endif
25595d77dca8SDavid Ahern 	.diag_destroy	   = udp_abort,
2560db8dac20SDavid S. Miller };
2561c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot);
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
25641da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
25651da177e4SLinus Torvalds 
2566645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start)
25671da177e4SLinus Torvalds {
25681da177e4SLinus Torvalds 	struct sock *sk;
25691da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
25706f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
25711da177e4SLinus Torvalds 
2572f86dcc5aSEric Dumazet 	for (state->bucket = start; state->bucket <= state->udp_table->mask;
2573f86dcc5aSEric Dumazet 	     ++state->bucket) {
2574645ca708SEric Dumazet 		struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
2575f86dcc5aSEric Dumazet 
2576ca065d0cSEric Dumazet 		if (hlist_empty(&hslot->head))
2577f86dcc5aSEric Dumazet 			continue;
2578f86dcc5aSEric Dumazet 
2579645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
2580ca065d0cSEric Dumazet 		sk_for_each(sk, &hslot->head) {
2581878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
2582a91275efSDaniel Lezcano 				continue;
25831da177e4SLinus Torvalds 			if (sk->sk_family == state->family)
25841da177e4SLinus Torvalds 				goto found;
25851da177e4SLinus Torvalds 		}
2586645ca708SEric Dumazet 		spin_unlock_bh(&hslot->lock);
25871da177e4SLinus Torvalds 	}
25881da177e4SLinus Torvalds 	sk = NULL;
25891da177e4SLinus Torvalds found:
25901da177e4SLinus Torvalds 	return sk;
25911da177e4SLinus Torvalds }
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
25941da177e4SLinus Torvalds {
25951da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
25966f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
25971da177e4SLinus Torvalds 
25981da177e4SLinus Torvalds 	do {
2599ca065d0cSEric Dumazet 		sk = sk_next(sk);
2600878628fbSYOSHIFUJI Hideaki 	} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
26011da177e4SLinus Torvalds 
2602645ca708SEric Dumazet 	if (!sk) {
2603f86dcc5aSEric Dumazet 		if (state->bucket <= state->udp_table->mask)
2604f52b5054SEric Dumazet 			spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
2605645ca708SEric Dumazet 		return udp_get_first(seq, state->bucket + 1);
26061da177e4SLinus Torvalds 	}
26071da177e4SLinus Torvalds 	return sk;
26081da177e4SLinus Torvalds }
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
26111da177e4SLinus Torvalds {
2612645ca708SEric Dumazet 	struct sock *sk = udp_get_first(seq, 0);
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	if (sk)
26151da177e4SLinus Torvalds 		while (pos && (sk = udp_get_next(seq, sk)) != NULL)
26161da177e4SLinus Torvalds 			--pos;
26171da177e4SLinus Torvalds 	return pos ? NULL : sk;
26181da177e4SLinus Torvalds }
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
26211da177e4SLinus Torvalds {
262230842f29SVitaly Mayatskikh 	struct udp_iter_state *state = seq->private;
2623f86dcc5aSEric Dumazet 	state->bucket = MAX_UDP_PORTS;
262430842f29SVitaly Mayatskikh 
2625b50660f1SYOSHIFUJI Hideaki 	return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
26261da177e4SLinus Torvalds }
26271da177e4SLinus Torvalds 
26281da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
26291da177e4SLinus Torvalds {
26301da177e4SLinus Torvalds 	struct sock *sk;
26311da177e4SLinus Torvalds 
2632b50660f1SYOSHIFUJI Hideaki 	if (v == SEQ_START_TOKEN)
26331da177e4SLinus Torvalds 		sk = udp_get_idx(seq, 0);
26341da177e4SLinus Torvalds 	else
26351da177e4SLinus Torvalds 		sk = udp_get_next(seq, v);
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds 	++*pos;
26381da177e4SLinus Torvalds 	return sk;
26391da177e4SLinus Torvalds }
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v)
26421da177e4SLinus Torvalds {
2643645ca708SEric Dumazet 	struct udp_iter_state *state = seq->private;
2644645ca708SEric Dumazet 
2645f86dcc5aSEric Dumazet 	if (state->bucket <= state->udp_table->mask)
2646645ca708SEric Dumazet 		spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
26471da177e4SLinus Torvalds }
26481da177e4SLinus Torvalds 
264973cb88ecSArjan van de Ven int udp_seq_open(struct inode *inode, struct file *file)
26501da177e4SLinus Torvalds {
2651d9dda78bSAl Viro 	struct udp_seq_afinfo *afinfo = PDE_DATA(inode);
2652a2be75c1SDenis V. Lunev 	struct udp_iter_state *s;
2653a2be75c1SDenis V. Lunev 	int err;
26541da177e4SLinus Torvalds 
2655a2be75c1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
2656a2be75c1SDenis V. Lunev 			   sizeof(struct udp_iter_state));
2657a2be75c1SDenis V. Lunev 	if (err < 0)
2658a2be75c1SDenis V. Lunev 		return err;
2659a91275efSDaniel Lezcano 
2660a2be75c1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
26611da177e4SLinus Torvalds 	s->family		= afinfo->family;
2662645ca708SEric Dumazet 	s->udp_table		= afinfo->udp_table;
2663a2be75c1SDenis V. Lunev 	return err;
2664a91275efSDaniel Lezcano }
266573cb88ecSArjan van de Ven EXPORT_SYMBOL(udp_seq_open);
2666a91275efSDaniel Lezcano 
26671da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
26680c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
26691da177e4SLinus Torvalds {
26701da177e4SLinus Torvalds 	struct proc_dir_entry *p;
26711da177e4SLinus Torvalds 	int rc = 0;
26721da177e4SLinus Torvalds 
2673dda61925SDenis V. Lunev 	afinfo->seq_ops.start		= udp_seq_start;
2674dda61925SDenis V. Lunev 	afinfo->seq_ops.next		= udp_seq_next;
2675dda61925SDenis V. Lunev 	afinfo->seq_ops.stop		= udp_seq_stop;
2676dda61925SDenis V. Lunev 
267784841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
267873cb88ecSArjan van de Ven 			     afinfo->seq_fops, afinfo);
267984841c3cSDenis V. Lunev 	if (!p)
26801da177e4SLinus Torvalds 		rc = -ENOMEM;
26811da177e4SLinus Torvalds 	return rc;
26821da177e4SLinus Torvalds }
2683c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_register);
26841da177e4SLinus Torvalds 
26850c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
26861da177e4SLinus Torvalds {
2687ece31ffdSGao feng 	remove_proc_entry(afinfo->name, net->proc_net);
26881da177e4SLinus Torvalds }
2689c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_unregister);
2690db8dac20SDavid S. Miller 
2691db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
26925e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2693652586dfSTetsuo Handa 		int bucket)
2694db8dac20SDavid S. Miller {
2695db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
2696c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2697c720c7e8SEric Dumazet 	__be32 src  = inet->inet_rcv_saddr;
2698c720c7e8SEric Dumazet 	__u16 destp	  = ntohs(inet->inet_dport);
2699c720c7e8SEric Dumazet 	__u16 srcp	  = ntohs(inet->inet_sport);
2700db8dac20SDavid S. Miller 
2701f86dcc5aSEric Dumazet 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2702652586dfSTetsuo Handa 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
2703db8dac20SDavid S. Miller 		bucket, src, srcp, dest, destp, sp->sk_state,
270431e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
270531e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
2706a7cb5a49SEric W. Biederman 		0, 0L, 0,
2707a7cb5a49SEric W. Biederman 		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
2708a7cb5a49SEric W. Biederman 		0, sock_i_ino(sp),
270941c6d650SReshetova, Elena 		refcount_read(&sp->sk_refcnt), sp,
2710652586dfSTetsuo Handa 		atomic_read(&sp->sk_drops));
2711db8dac20SDavid S. Miller }
2712db8dac20SDavid S. Miller 
2713db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v)
2714db8dac20SDavid S. Miller {
2715652586dfSTetsuo Handa 	seq_setwidth(seq, 127);
2716db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
2717652586dfSTetsuo Handa 		seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
2718db8dac20SDavid S. Miller 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
2719cb61cb9bSEric Dumazet 			   "inode ref pointer drops");
2720db8dac20SDavid S. Miller 	else {
2721db8dac20SDavid S. Miller 		struct udp_iter_state *state = seq->private;
2722db8dac20SDavid S. Miller 
2723652586dfSTetsuo Handa 		udp4_format_sock(v, seq, state->bucket);
2724db8dac20SDavid S. Miller 	}
2725652586dfSTetsuo Handa 	seq_pad(seq, '\n');
2726db8dac20SDavid S. Miller 	return 0;
2727db8dac20SDavid S. Miller }
2728db8dac20SDavid S. Miller 
272973cb88ecSArjan van de Ven static const struct file_operations udp_afinfo_seq_fops = {
273073cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
273173cb88ecSArjan van de Ven 	.open     = udp_seq_open,
273273cb88ecSArjan van de Ven 	.read     = seq_read,
273373cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
273473cb88ecSArjan van de Ven 	.release  = seq_release_net
273573cb88ecSArjan van de Ven };
273673cb88ecSArjan van de Ven 
2737db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
2738db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = {
2739db8dac20SDavid S. Miller 	.name		= "udp",
2740db8dac20SDavid S. Miller 	.family		= AF_INET,
2741645ca708SEric Dumazet 	.udp_table	= &udp_table,
274273cb88ecSArjan van de Ven 	.seq_fops	= &udp_afinfo_seq_fops,
2743dda61925SDenis V. Lunev 	.seq_ops	= {
2744dda61925SDenis V. Lunev 		.show		= udp4_seq_show,
2745dda61925SDenis V. Lunev 	},
2746db8dac20SDavid S. Miller };
2747db8dac20SDavid S. Miller 
27482c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net)
274915439febSPavel Emelyanov {
275015439febSPavel Emelyanov 	return udp_proc_register(net, &udp4_seq_afinfo);
275115439febSPavel Emelyanov }
275215439febSPavel Emelyanov 
27532c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net)
275415439febSPavel Emelyanov {
275515439febSPavel Emelyanov 	udp_proc_unregister(net, &udp4_seq_afinfo);
275615439febSPavel Emelyanov }
275715439febSPavel Emelyanov 
275815439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = {
275915439febSPavel Emelyanov 	.init = udp4_proc_init_net,
276015439febSPavel Emelyanov 	.exit = udp4_proc_exit_net,
276115439febSPavel Emelyanov };
276215439febSPavel Emelyanov 
2763db8dac20SDavid S. Miller int __init udp4_proc_init(void)
2764db8dac20SDavid S. Miller {
276515439febSPavel Emelyanov 	return register_pernet_subsys(&udp4_net_ops);
2766db8dac20SDavid S. Miller }
2767db8dac20SDavid S. Miller 
2768db8dac20SDavid S. Miller void udp4_proc_exit(void)
2769db8dac20SDavid S. Miller {
277015439febSPavel Emelyanov 	unregister_pernet_subsys(&udp4_net_ops);
2771db8dac20SDavid S. Miller }
27721da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
27731da177e4SLinus Torvalds 
2774f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries;
2775f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str)
2776645ca708SEric Dumazet {
2777413c27d8SEldad Zack 	ssize_t ret;
2778413c27d8SEldad Zack 
2779f86dcc5aSEric Dumazet 	if (!str)
2780f86dcc5aSEric Dumazet 		return 0;
2781413c27d8SEldad Zack 
2782413c27d8SEldad Zack 	ret = kstrtoul(str, 0, &uhash_entries);
2783413c27d8SEldad Zack 	if (ret)
2784413c27d8SEldad Zack 		return 0;
2785413c27d8SEldad Zack 
2786f86dcc5aSEric Dumazet 	if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
2787f86dcc5aSEric Dumazet 		uhash_entries = UDP_HTABLE_SIZE_MIN;
2788f86dcc5aSEric Dumazet 	return 1;
2789f86dcc5aSEric Dumazet }
2790f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries);
2791645ca708SEric Dumazet 
2792f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name)
2793f86dcc5aSEric Dumazet {
2794f86dcc5aSEric Dumazet 	unsigned int i;
2795f86dcc5aSEric Dumazet 
2796f86dcc5aSEric Dumazet 	table->hash = alloc_large_system_hash(name,
2797512615b6SEric Dumazet 					      2 * sizeof(struct udp_hslot),
2798f86dcc5aSEric Dumazet 					      uhash_entries,
2799f86dcc5aSEric Dumazet 					      21, /* one slot per 2 MB */
2800f86dcc5aSEric Dumazet 					      0,
2801f86dcc5aSEric Dumazet 					      &table->log,
2802f86dcc5aSEric Dumazet 					      &table->mask,
280331fe62b9STim Bird 					      UDP_HTABLE_SIZE_MIN,
2804f86dcc5aSEric Dumazet 					      64 * 1024);
280531fe62b9STim Bird 
2806512615b6SEric Dumazet 	table->hash2 = table->hash + (table->mask + 1);
2807f86dcc5aSEric Dumazet 	for (i = 0; i <= table->mask; i++) {
2808ca065d0cSEric Dumazet 		INIT_HLIST_HEAD(&table->hash[i].head);
2809fdcc8aa9SEric Dumazet 		table->hash[i].count = 0;
2810645ca708SEric Dumazet 		spin_lock_init(&table->hash[i].lock);
2811645ca708SEric Dumazet 	}
2812512615b6SEric Dumazet 	for (i = 0; i <= table->mask; i++) {
2813ca065d0cSEric Dumazet 		INIT_HLIST_HEAD(&table->hash2[i].head);
2814512615b6SEric Dumazet 		table->hash2[i].count = 0;
2815512615b6SEric Dumazet 		spin_lock_init(&table->hash2[i].lock);
2816512615b6SEric Dumazet 	}
2817645ca708SEric Dumazet }
2818645ca708SEric Dumazet 
2819723b8e46STom Herbert u32 udp_flow_hashrnd(void)
2820723b8e46STom Herbert {
2821723b8e46STom Herbert 	static u32 hashrnd __read_mostly;
2822723b8e46STom Herbert 
2823723b8e46STom Herbert 	net_get_random_once(&hashrnd, sizeof(hashrnd));
2824723b8e46STom Herbert 
2825723b8e46STom Herbert 	return hashrnd;
2826723b8e46STom Herbert }
2827723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd);
2828723b8e46STom Herbert 
282995766fffSHideo Aoki void __init udp_init(void)
283095766fffSHideo Aoki {
2831f03d78dbSEric Dumazet 	unsigned long limit;
28324b272750SEric Dumazet 	unsigned int i;
283395766fffSHideo Aoki 
2834f86dcc5aSEric Dumazet 	udp_table_init(&udp_table, "UDP");
2835f03d78dbSEric Dumazet 	limit = nr_free_buffer_pages() / 8;
283695766fffSHideo Aoki 	limit = max(limit, 128UL);
283795766fffSHideo Aoki 	sysctl_udp_mem[0] = limit / 4 * 3;
283895766fffSHideo Aoki 	sysctl_udp_mem[1] = limit;
283995766fffSHideo Aoki 	sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
284095766fffSHideo Aoki 
284195766fffSHideo Aoki 	sysctl_udp_rmem_min = SK_MEM_QUANTUM;
284295766fffSHideo Aoki 	sysctl_udp_wmem_min = SK_MEM_QUANTUM;
28434b272750SEric Dumazet 
28444b272750SEric Dumazet 	/* 16 spinlocks per cpu */
28454b272750SEric Dumazet 	udp_busylocks_log = ilog2(nr_cpu_ids) + 4;
28464b272750SEric Dumazet 	udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log,
28474b272750SEric Dumazet 				GFP_KERNEL);
28484b272750SEric Dumazet 	if (!udp_busylocks)
28494b272750SEric Dumazet 		panic("UDP: failed to alloc udp_busylocks\n");
28504b272750SEric Dumazet 	for (i = 0; i < (1U << udp_busylocks_log); i++)
28514b272750SEric Dumazet 		spin_lock_init(udp_busylocks + i);
285295766fffSHideo Aoki }
2853