xref: /linux/net/ipv4/udp.c (revision 1134158ba3d656b8dbc79a23d482129a531ba0ae)
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 
821da177e4SLinus Torvalds #include <asm/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>
1171da177e4SLinus Torvalds 
118f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly;
119645ca708SEric Dumazet EXPORT_SYMBOL(udp_table);
1201da177e4SLinus Torvalds 
1218d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly;
12295766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem);
123c482c568SEric Dumazet 
124c482c568SEric Dumazet int sysctl_udp_rmem_min __read_mostly;
12595766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min);
126c482c568SEric Dumazet 
127c482c568SEric Dumazet int sysctl_udp_wmem_min __read_mostly;
12895766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min);
12995766fffSHideo Aoki 
1308d987e5cSEric Dumazet atomic_long_t udp_memory_allocated;
13195766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated);
13295766fffSHideo Aoki 
133f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536
134f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
13598322f22SEric Dumazet 
136f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num,
137645ca708SEric Dumazet 			       const struct udp_hslot *hslot,
13898322f22SEric Dumazet 			       unsigned long *bitmap,
139f24d43c0SEric Dumazet 			       struct sock *sk,
140f24d43c0SEric Dumazet 			       int (*saddr_comp)(const struct sock *sk1,
141e32ea7e7SCraig Gallek 						 const struct sock *sk2,
142e32ea7e7SCraig Gallek 						 bool match_wildcard),
143f86dcc5aSEric Dumazet 			       unsigned int log)
14425030a7fSGerrit Renker {
145f24d43c0SEric Dumazet 	struct sock *sk2;
14688ab1932SEric Dumazet 	struct hlist_nulls_node *node;
147ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
14825030a7fSGerrit Renker 
1494243cdc2SJoe Perches 	sk_nulls_for_each(sk2, node, &hslot->head) {
150f24d43c0SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
151f24d43c0SEric Dumazet 		    sk2 != sk &&
152d4cada4aSEric Dumazet 		    (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
153f24d43c0SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1549d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1559d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
156ba418fa3STom Herbert 		    (!sk2->sk_reuseport || !sk->sk_reuseport ||
157e32ea7e7SCraig Gallek 		     rcu_access_pointer(sk->sk_reuseport_cb) ||
158ba418fa3STom Herbert 		     !uid_eq(uid, sock_i_uid(sk2))) &&
159e32ea7e7SCraig Gallek 		    saddr_comp(sk, sk2, true)) {
1604243cdc2SJoe Perches 			if (!bitmap)
161fc038410SDavid S. Miller 				return 1;
1624243cdc2SJoe Perches 			__set_bit(udp_sk(sk2)->udp_port_hash >> log, bitmap);
1634243cdc2SJoe Perches 		}
16498322f22SEric Dumazet 	}
16525030a7fSGerrit Renker 	return 0;
16625030a7fSGerrit Renker }
16725030a7fSGerrit Renker 
16830fff923SEric Dumazet /*
16930fff923SEric Dumazet  * Note: we still hold spinlock of primary hash chain, so no other writer
17030fff923SEric Dumazet  * can insert/delete a socket with local_port == num
17130fff923SEric Dumazet  */
17230fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num,
17330fff923SEric Dumazet 				struct udp_hslot *hslot2,
17430fff923SEric Dumazet 				struct sock *sk,
17530fff923SEric Dumazet 				int (*saddr_comp)(const struct sock *sk1,
176e32ea7e7SCraig Gallek 						  const struct sock *sk2,
177e32ea7e7SCraig Gallek 						  bool match_wildcard))
17830fff923SEric Dumazet {
17930fff923SEric Dumazet 	struct sock *sk2;
18030fff923SEric Dumazet 	struct hlist_nulls_node *node;
181ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
18230fff923SEric Dumazet 	int res = 0;
18330fff923SEric Dumazet 
18430fff923SEric Dumazet 	spin_lock(&hslot2->lock);
1854243cdc2SJoe Perches 	udp_portaddr_for_each_entry(sk2, node, &hslot2->head) {
18630fff923SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
18730fff923SEric Dumazet 		    sk2 != sk &&
18830fff923SEric Dumazet 		    (udp_sk(sk2)->udp_port_hash == num) &&
18930fff923SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1909d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1919d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
192ba418fa3STom Herbert 		    (!sk2->sk_reuseport || !sk->sk_reuseport ||
193e32ea7e7SCraig Gallek 		     rcu_access_pointer(sk->sk_reuseport_cb) ||
194ba418fa3STom Herbert 		     !uid_eq(uid, sock_i_uid(sk2))) &&
195e32ea7e7SCraig Gallek 		    saddr_comp(sk, sk2, true)) {
19630fff923SEric Dumazet 			res = 1;
19730fff923SEric Dumazet 			break;
19830fff923SEric Dumazet 		}
1994243cdc2SJoe Perches 	}
20030fff923SEric Dumazet 	spin_unlock(&hslot2->lock);
20130fff923SEric Dumazet 	return res;
20230fff923SEric Dumazet }
20330fff923SEric Dumazet 
204e32ea7e7SCraig Gallek static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot,
205e32ea7e7SCraig Gallek 				  int (*saddr_same)(const struct sock *sk1,
206e32ea7e7SCraig Gallek 						    const struct sock *sk2,
207e32ea7e7SCraig Gallek 						    bool match_wildcard))
208e32ea7e7SCraig Gallek {
209e32ea7e7SCraig Gallek 	struct net *net = sock_net(sk);
210e32ea7e7SCraig Gallek 	struct hlist_nulls_node *node;
211e32ea7e7SCraig Gallek 	kuid_t uid = sock_i_uid(sk);
212e32ea7e7SCraig Gallek 	struct sock *sk2;
213e32ea7e7SCraig Gallek 
214e32ea7e7SCraig Gallek 	sk_nulls_for_each(sk2, node, &hslot->head) {
215e32ea7e7SCraig Gallek 		if (net_eq(sock_net(sk2), net) &&
216e32ea7e7SCraig Gallek 		    sk2 != sk &&
217e32ea7e7SCraig Gallek 		    sk2->sk_family == sk->sk_family &&
218e32ea7e7SCraig Gallek 		    ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
219e32ea7e7SCraig Gallek 		    (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) &&
220e32ea7e7SCraig Gallek 		    (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
221e32ea7e7SCraig Gallek 		    sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
222e32ea7e7SCraig Gallek 		    (*saddr_same)(sk, sk2, false)) {
223e32ea7e7SCraig Gallek 			return reuseport_add_sock(sk, sk2);
224e32ea7e7SCraig Gallek 		}
225e32ea7e7SCraig Gallek 	}
226e32ea7e7SCraig Gallek 
227e32ea7e7SCraig Gallek 	/* Initial allocation may have already happened via setsockopt */
228e32ea7e7SCraig Gallek 	if (!rcu_access_pointer(sk->sk_reuseport_cb))
229e32ea7e7SCraig Gallek 		return reuseport_alloc(sk);
230e32ea7e7SCraig Gallek 	return 0;
231e32ea7e7SCraig Gallek }
232e32ea7e7SCraig Gallek 
23325030a7fSGerrit Renker /**
2346ba5a3c5SPavel Emelyanov  *  udp_lib_get_port  -  UDP/-Lite port lookup for IPv4 and IPv6
23525030a7fSGerrit Renker  *
23625030a7fSGerrit Renker  *  @sk:          socket struct in question
23725030a7fSGerrit Renker  *  @snum:        port number to look up
238df2bc459SDavid S. Miller  *  @saddr_comp:  AF-dependent comparison of bound local IP addresses
23925985edcSLucas De Marchi  *  @hash2_nulladdr: AF-dependent hash value in secondary hash chains,
24030fff923SEric Dumazet  *                   with NULL address
24125030a7fSGerrit Renker  */
2426ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum,
243df2bc459SDavid S. Miller 		     int (*saddr_comp)(const struct sock *sk1,
244e32ea7e7SCraig Gallek 				       const struct sock *sk2,
245e32ea7e7SCraig Gallek 				       bool match_wildcard),
24630fff923SEric Dumazet 		     unsigned int hash2_nulladdr)
2471da177e4SLinus Torvalds {
248512615b6SEric Dumazet 	struct udp_hslot *hslot, *hslot2;
249645ca708SEric Dumazet 	struct udp_table *udptable = sk->sk_prot->h.udp_table;
25025030a7fSGerrit Renker 	int    error = 1;
2513b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2521da177e4SLinus Torvalds 
25332c1da70SStephen Hemminger 	if (!snum) {
2549088c560SEric Dumazet 		int low, high, remaining;
25595c96174SEric Dumazet 		unsigned int rand;
25698322f22SEric Dumazet 		unsigned short first, last;
25798322f22SEric Dumazet 		DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
2581da177e4SLinus Torvalds 
2590bbf87d8SEric W. Biederman 		inet_get_local_port_range(net, &low, &high);
260a25de534SAnton Arapov 		remaining = (high - low) + 1;
261227b60f5SStephen Hemminger 
26263862b5bSAruna-Hewapathirane 		rand = prandom_u32();
2638fc54f68SDaniel Borkmann 		first = reciprocal_scale(rand, remaining) + low;
26498322f22SEric Dumazet 		/*
26598322f22SEric Dumazet 		 * force rand to be an odd multiple of UDP_HTABLE_SIZE
26698322f22SEric Dumazet 		 */
267f86dcc5aSEric Dumazet 		rand = (rand | 1) * (udptable->mask + 1);
2685781b235SEric Dumazet 		last = first + udptable->mask + 1;
2695781b235SEric Dumazet 		do {
270f86dcc5aSEric Dumazet 			hslot = udp_hashslot(udptable, net, first);
27198322f22SEric Dumazet 			bitmap_zero(bitmap, PORTS_PER_CHAIN);
272645ca708SEric Dumazet 			spin_lock_bh(&hslot->lock);
27398322f22SEric Dumazet 			udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
274f86dcc5aSEric Dumazet 					    saddr_comp, udptable->log);
27598322f22SEric Dumazet 
27698322f22SEric Dumazet 			snum = first;
27798322f22SEric Dumazet 			/*
27898322f22SEric Dumazet 			 * Iterate on all possible values of snum for this hash.
27998322f22SEric Dumazet 			 * Using steps of an odd multiple of UDP_HTABLE_SIZE
28098322f22SEric Dumazet 			 * give us randomization and full range coverage.
28198322f22SEric Dumazet 			 */
2829088c560SEric Dumazet 			do {
28398322f22SEric Dumazet 				if (low <= snum && snum <= high &&
284e3826f1eSAmerigo Wang 				    !test_bit(snum >> udptable->log, bitmap) &&
285122ff243SWANG Cong 				    !inet_is_local_reserved_port(net, snum))
28698322f22SEric Dumazet 					goto found;
28798322f22SEric Dumazet 				snum += rand;
28898322f22SEric Dumazet 			} while (snum != first);
28998322f22SEric Dumazet 			spin_unlock_bh(&hslot->lock);
2905781b235SEric Dumazet 		} while (++first != last);
29198322f22SEric Dumazet 		goto fail;
292645ca708SEric Dumazet 	} else {
293f86dcc5aSEric Dumazet 		hslot = udp_hashslot(udptable, net, snum);
294645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
29530fff923SEric Dumazet 		if (hslot->count > 10) {
29630fff923SEric Dumazet 			int exist;
29730fff923SEric Dumazet 			unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
29830fff923SEric Dumazet 
29930fff923SEric Dumazet 			slot2          &= udptable->mask;
30030fff923SEric Dumazet 			hash2_nulladdr &= udptable->mask;
30130fff923SEric Dumazet 
30230fff923SEric Dumazet 			hslot2 = udp_hashslot2(udptable, slot2);
30330fff923SEric Dumazet 			if (hslot->count < hslot2->count)
30430fff923SEric Dumazet 				goto scan_primary_hash;
30530fff923SEric Dumazet 
30630fff923SEric Dumazet 			exist = udp_lib_lport_inuse2(net, snum, hslot2,
30730fff923SEric Dumazet 						     sk, saddr_comp);
30830fff923SEric Dumazet 			if (!exist && (hash2_nulladdr != slot2)) {
30930fff923SEric Dumazet 				hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
31030fff923SEric Dumazet 				exist = udp_lib_lport_inuse2(net, snum, hslot2,
31130fff923SEric Dumazet 							     sk, saddr_comp);
31230fff923SEric Dumazet 			}
31330fff923SEric Dumazet 			if (exist)
31430fff923SEric Dumazet 				goto fail_unlock;
31530fff923SEric Dumazet 			else
31630fff923SEric Dumazet 				goto found;
31730fff923SEric Dumazet 		}
31830fff923SEric Dumazet scan_primary_hash:
319f86dcc5aSEric Dumazet 		if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
320f86dcc5aSEric Dumazet 					saddr_comp, 0))
321645ca708SEric Dumazet 			goto fail_unlock;
322645ca708SEric Dumazet 	}
32398322f22SEric Dumazet found:
324c720c7e8SEric Dumazet 	inet_sk(sk)->inet_num = snum;
325d4cada4aSEric Dumazet 	udp_sk(sk)->udp_port_hash = snum;
326d4cada4aSEric Dumazet 	udp_sk(sk)->udp_portaddr_hash ^= snum;
3271da177e4SLinus Torvalds 	if (sk_unhashed(sk)) {
328e32ea7e7SCraig Gallek 		if (sk->sk_reuseport &&
329e32ea7e7SCraig Gallek 		    udp_reuseport_add_sock(sk, hslot, saddr_comp)) {
330e32ea7e7SCraig Gallek 			inet_sk(sk)->inet_num = 0;
331e32ea7e7SCraig Gallek 			udp_sk(sk)->udp_port_hash = 0;
332e32ea7e7SCraig Gallek 			udp_sk(sk)->udp_portaddr_hash ^= snum;
333e32ea7e7SCraig Gallek 			goto fail_unlock;
334e32ea7e7SCraig Gallek 		}
335e32ea7e7SCraig Gallek 
33688ab1932SEric Dumazet 		sk_nulls_add_node_rcu(sk, &hslot->head);
337fdcc8aa9SEric Dumazet 		hslot->count++;
338c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
339512615b6SEric Dumazet 
340512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
341512615b6SEric Dumazet 		spin_lock(&hslot2->lock);
342512615b6SEric Dumazet 		hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
343512615b6SEric Dumazet 					 &hslot2->head);
344512615b6SEric Dumazet 		hslot2->count++;
345512615b6SEric Dumazet 		spin_unlock(&hslot2->lock);
3461da177e4SLinus Torvalds 	}
34725030a7fSGerrit Renker 	error = 0;
348645ca708SEric Dumazet fail_unlock:
349645ca708SEric Dumazet 	spin_unlock_bh(&hslot->lock);
3501da177e4SLinus Torvalds fail:
35125030a7fSGerrit Renker 	return error;
3521da177e4SLinus Torvalds }
353c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port);
3541da177e4SLinus Torvalds 
355e32ea7e7SCraig Gallek /* match_wildcard == true:  0.0.0.0 equals to any IPv4 addresses
356e32ea7e7SCraig Gallek  * match_wildcard == false: addresses must be exactly the same, i.e.
357e32ea7e7SCraig Gallek  *                          0.0.0.0 only equals to 0.0.0.0
358e32ea7e7SCraig Gallek  */
359e32ea7e7SCraig Gallek static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2,
360e32ea7e7SCraig Gallek 				bool match_wildcard)
361db8dac20SDavid S. Miller {
362db8dac20SDavid S. Miller 	struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
363db8dac20SDavid S. Miller 
364e32ea7e7SCraig Gallek 	if (!ipv6_only_sock(sk2)) {
365e32ea7e7SCraig Gallek 		if (inet1->inet_rcv_saddr == inet2->inet_rcv_saddr)
366e32ea7e7SCraig Gallek 			return 1;
367e32ea7e7SCraig Gallek 		if (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr)
368e32ea7e7SCraig Gallek 			return match_wildcard;
369e32ea7e7SCraig Gallek 	}
370e32ea7e7SCraig Gallek 	return 0;
371db8dac20SDavid S. Miller }
372db8dac20SDavid S. Miller 
3736eada011SEric Dumazet static u32 udp4_portaddr_hash(const struct net *net, __be32 saddr,
374d4cada4aSEric Dumazet 			      unsigned int port)
375d4cada4aSEric Dumazet {
3760eae88f3SEric Dumazet 	return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
377d4cada4aSEric Dumazet }
378d4cada4aSEric Dumazet 
3796ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum)
380db8dac20SDavid S. Miller {
38130fff923SEric Dumazet 	unsigned int hash2_nulladdr =
3820eae88f3SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
38330fff923SEric Dumazet 	unsigned int hash2_partial =
38430fff923SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
38530fff923SEric Dumazet 
386d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
38730fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
38830fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr);
389db8dac20SDavid S. Miller }
390db8dac20SDavid S. Miller 
39160c04aecSJoe Perches static inline int compute_score(struct sock *sk, struct net *net,
39260c04aecSJoe Perches 				__be32 saddr, unsigned short hnum, __be16 sport,
39360c04aecSJoe Perches 				__be32 daddr, __be16 dport, int dif)
394645ca708SEric Dumazet {
39560c04aecSJoe Perches 	int score;
39660c04aecSJoe Perches 	struct inet_sock *inet;
397645ca708SEric Dumazet 
39860c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
39960c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
40060c04aecSJoe Perches 	    ipv6_only_sock(sk))
40160c04aecSJoe Perches 		return -1;
402645ca708SEric Dumazet 
40360c04aecSJoe Perches 	score = (sk->sk_family == PF_INET) ? 2 : 1;
40460c04aecSJoe Perches 	inet = inet_sk(sk);
40560c04aecSJoe Perches 
406c720c7e8SEric Dumazet 	if (inet->inet_rcv_saddr) {
407c720c7e8SEric Dumazet 		if (inet->inet_rcv_saddr != daddr)
408645ca708SEric Dumazet 			return -1;
409ba418fa3STom Herbert 		score += 4;
410645ca708SEric Dumazet 	}
41160c04aecSJoe Perches 
412c720c7e8SEric Dumazet 	if (inet->inet_daddr) {
413c720c7e8SEric Dumazet 		if (inet->inet_daddr != saddr)
414645ca708SEric Dumazet 			return -1;
415ba418fa3STom Herbert 		score += 4;
416645ca708SEric Dumazet 	}
41760c04aecSJoe Perches 
418c720c7e8SEric Dumazet 	if (inet->inet_dport) {
419c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
420645ca708SEric Dumazet 			return -1;
421ba418fa3STom Herbert 		score += 4;
422645ca708SEric Dumazet 	}
42360c04aecSJoe Perches 
424645ca708SEric Dumazet 	if (sk->sk_bound_dev_if) {
425645ca708SEric Dumazet 		if (sk->sk_bound_dev_if != dif)
426645ca708SEric Dumazet 			return -1;
427ba418fa3STom Herbert 		score += 4;
428645ca708SEric Dumazet 	}
42970da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
43070da268bSEric Dumazet 		score++;
431645ca708SEric Dumazet 	return score;
432645ca708SEric Dumazet }
433645ca708SEric Dumazet 
4345051ebd2SEric Dumazet /*
4355051ebd2SEric Dumazet  * In this second variant, we check (daddr, dport) matches (inet_rcv_sadd, inet_num)
4365051ebd2SEric Dumazet  */
4375051ebd2SEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
4385051ebd2SEric Dumazet 				 __be32 saddr, __be16 sport,
4395051ebd2SEric Dumazet 				 __be32 daddr, unsigned int hnum, int dif)
4405051ebd2SEric Dumazet {
44160c04aecSJoe Perches 	int score;
44260c04aecSJoe Perches 	struct inet_sock *inet;
4435051ebd2SEric Dumazet 
44460c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
44560c04aecSJoe Perches 	    ipv6_only_sock(sk))
4465051ebd2SEric Dumazet 		return -1;
4475051ebd2SEric Dumazet 
44860c04aecSJoe Perches 	inet = inet_sk(sk);
44960c04aecSJoe Perches 
45060c04aecSJoe Perches 	if (inet->inet_rcv_saddr != daddr ||
45160c04aecSJoe Perches 	    inet->inet_num != hnum)
45260c04aecSJoe Perches 		return -1;
45360c04aecSJoe Perches 
45460c04aecSJoe Perches 	score = (sk->sk_family == PF_INET) ? 2 : 1;
45560c04aecSJoe Perches 
4565051ebd2SEric Dumazet 	if (inet->inet_daddr) {
4575051ebd2SEric Dumazet 		if (inet->inet_daddr != saddr)
4585051ebd2SEric Dumazet 			return -1;
459ba418fa3STom Herbert 		score += 4;
4605051ebd2SEric Dumazet 	}
46160c04aecSJoe Perches 
4625051ebd2SEric Dumazet 	if (inet->inet_dport) {
4635051ebd2SEric Dumazet 		if (inet->inet_dport != sport)
4645051ebd2SEric Dumazet 			return -1;
465ba418fa3STom Herbert 		score += 4;
4665051ebd2SEric Dumazet 	}
46760c04aecSJoe Perches 
4685051ebd2SEric Dumazet 	if (sk->sk_bound_dev_if) {
4695051ebd2SEric Dumazet 		if (sk->sk_bound_dev_if != dif)
4705051ebd2SEric Dumazet 			return -1;
471ba418fa3STom Herbert 		score += 4;
4725051ebd2SEric Dumazet 	}
47360c04aecSJoe Perches 
47470da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
47570da268bSEric Dumazet 		score++;
47670da268bSEric Dumazet 
4775051ebd2SEric Dumazet 	return score;
4785051ebd2SEric Dumazet }
4795051ebd2SEric Dumazet 
4806eada011SEric Dumazet static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
48165cd8033SHannes Frederic Sowa 		       const __u16 lport, const __be32 faddr,
48265cd8033SHannes Frederic Sowa 		       const __be16 fport)
48365cd8033SHannes Frederic Sowa {
4841bbdceefSHannes Frederic Sowa 	static u32 udp_ehash_secret __read_mostly;
4851bbdceefSHannes Frederic Sowa 
4861bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret));
4871bbdceefSHannes Frederic Sowa 
48865cd8033SHannes Frederic Sowa 	return __inet_ehashfn(laddr, lport, faddr, fport,
4891bbdceefSHannes Frederic Sowa 			      udp_ehash_secret + net_hash_mix(net));
49065cd8033SHannes Frederic Sowa }
49165cd8033SHannes Frederic Sowa 
4925051ebd2SEric Dumazet /* called with read_rcu_lock() */
4935051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net,
4945051ebd2SEric Dumazet 		__be32 saddr, __be16 sport,
4955051ebd2SEric Dumazet 		__be32 daddr, unsigned int hnum, int dif,
496*1134158bSCraig Gallek 		struct udp_hslot *hslot2, unsigned int slot2,
497*1134158bSCraig Gallek 		struct sk_buff *skb)
4985051ebd2SEric Dumazet {
4995051ebd2SEric Dumazet 	struct sock *sk, *result;
5005051ebd2SEric Dumazet 	struct hlist_nulls_node *node;
501ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
502ba418fa3STom Herbert 	u32 hash = 0;
5035051ebd2SEric Dumazet 
5045051ebd2SEric Dumazet begin:
5055051ebd2SEric Dumazet 	result = NULL;
506ba418fa3STom Herbert 	badness = 0;
5075051ebd2SEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
5085051ebd2SEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
5095051ebd2SEric Dumazet 				      daddr, hnum, dif);
5105051ebd2SEric Dumazet 		if (score > badness) {
5115051ebd2SEric Dumazet 			result = sk;
5125051ebd2SEric Dumazet 			badness = score;
513ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
514ba418fa3STom Herbert 			if (reuseport) {
515e32ea7e7SCraig Gallek 				struct sock *sk2;
51665cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
5177c0cadc6SEric Dumazet 						   saddr, sport);
518*1134158bSCraig Gallek 				sk2 = reuseport_select_sock(sk, hash, skb,
519*1134158bSCraig Gallek 							    sizeof(struct udphdr));
520e32ea7e7SCraig Gallek 				if (sk2) {
521e32ea7e7SCraig Gallek 					result = sk2;
522e32ea7e7SCraig Gallek 					goto found;
523e32ea7e7SCraig Gallek 				}
524ba418fa3STom Herbert 				matches = 1;
525ba418fa3STom Herbert 			}
526ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
527ba418fa3STom Herbert 			matches++;
5288fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
529ba418fa3STom Herbert 				result = sk;
530ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
5315051ebd2SEric Dumazet 		}
5325051ebd2SEric Dumazet 	}
5335051ebd2SEric Dumazet 	/*
5345051ebd2SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
5355051ebd2SEric Dumazet 	 * not the expected one, we must restart lookup.
5365051ebd2SEric Dumazet 	 * We probably met an item that was moved to another chain.
5375051ebd2SEric Dumazet 	 */
5385051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot2)
5395051ebd2SEric Dumazet 		goto begin;
5405051ebd2SEric Dumazet 	if (result) {
541e32ea7e7SCraig Gallek found:
542c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
5435051ebd2SEric Dumazet 			result = NULL;
5445051ebd2SEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
5455051ebd2SEric Dumazet 				  daddr, hnum, dif) < badness)) {
5465051ebd2SEric Dumazet 			sock_put(result);
5475051ebd2SEric Dumazet 			goto begin;
5485051ebd2SEric Dumazet 		}
5495051ebd2SEric Dumazet 	}
5505051ebd2SEric Dumazet 	return result;
5515051ebd2SEric Dumazet }
5525051ebd2SEric Dumazet 
553db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
554db8dac20SDavid S. Miller  * harder than this. -DaveM
555db8dac20SDavid S. Miller  */
556fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
557db8dac20SDavid S. Miller 		__be16 sport, __be32 daddr, __be16 dport,
558538950a1SCraig Gallek 		int dif, struct udp_table *udptable, struct sk_buff *skb)
559db8dac20SDavid S. Miller {
560271b72c7SEric Dumazet 	struct sock *sk, *result;
56188ab1932SEric Dumazet 	struct hlist_nulls_node *node;
562db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(dport);
5635051ebd2SEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
5645051ebd2SEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
565ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
566ba418fa3STom Herbert 	u32 hash = 0;
567db8dac20SDavid S. Miller 
568271b72c7SEric Dumazet 	rcu_read_lock();
5695051ebd2SEric Dumazet 	if (hslot->count > 10) {
5705051ebd2SEric Dumazet 		hash2 = udp4_portaddr_hash(net, daddr, hnum);
5715051ebd2SEric Dumazet 		slot2 = hash2 & udptable->mask;
5725051ebd2SEric Dumazet 		hslot2 = &udptable->hash2[slot2];
5735051ebd2SEric Dumazet 		if (hslot->count < hslot2->count)
5745051ebd2SEric Dumazet 			goto begin;
5755051ebd2SEric Dumazet 
5765051ebd2SEric Dumazet 		result = udp4_lib_lookup2(net, saddr, sport,
5775051ebd2SEric Dumazet 					  daddr, hnum, dif,
578*1134158bSCraig Gallek 					  hslot2, slot2, skb);
5795051ebd2SEric Dumazet 		if (!result) {
5800eae88f3SEric Dumazet 			hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
5815051ebd2SEric Dumazet 			slot2 = hash2 & udptable->mask;
5825051ebd2SEric Dumazet 			hslot2 = &udptable->hash2[slot2];
5835051ebd2SEric Dumazet 			if (hslot->count < hslot2->count)
5845051ebd2SEric Dumazet 				goto begin;
5855051ebd2SEric Dumazet 
5861223c67cSJorge Boncompte [DTI2] 			result = udp4_lib_lookup2(net, saddr, sport,
5870eae88f3SEric Dumazet 						  htonl(INADDR_ANY), hnum, dif,
588*1134158bSCraig Gallek 						  hslot2, slot2, skb);
5895051ebd2SEric Dumazet 		}
5905051ebd2SEric Dumazet 		rcu_read_unlock();
5915051ebd2SEric Dumazet 		return result;
5925051ebd2SEric Dumazet 	}
593271b72c7SEric Dumazet begin:
594271b72c7SEric Dumazet 	result = NULL;
595ba418fa3STom Herbert 	badness = 0;
59688ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
597645ca708SEric Dumazet 		score = compute_score(sk, net, saddr, hnum, sport,
598645ca708SEric Dumazet 				      daddr, dport, dif);
599645ca708SEric Dumazet 		if (score > badness) {
600db8dac20SDavid S. Miller 			result = sk;
601db8dac20SDavid S. Miller 			badness = score;
602ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
603ba418fa3STom Herbert 			if (reuseport) {
604e32ea7e7SCraig Gallek 				struct sock *sk2;
60565cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
6067c0cadc6SEric Dumazet 						   saddr, sport);
607538950a1SCraig Gallek 				sk2 = reuseport_select_sock(sk, hash, skb,
608538950a1SCraig Gallek 							sizeof(struct udphdr));
609e32ea7e7SCraig Gallek 				if (sk2) {
610e32ea7e7SCraig Gallek 					result = sk2;
611e32ea7e7SCraig Gallek 					goto found;
612e32ea7e7SCraig Gallek 				}
613ba418fa3STom Herbert 				matches = 1;
614ba418fa3STom Herbert 			}
615ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
616ba418fa3STom Herbert 			matches++;
6178fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
618ba418fa3STom Herbert 				result = sk;
619ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
620db8dac20SDavid S. Miller 		}
621db8dac20SDavid S. Miller 	}
62288ab1932SEric Dumazet 	/*
62388ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
62488ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
62588ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
62688ab1932SEric Dumazet 	 */
6275051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot)
62888ab1932SEric Dumazet 		goto begin;
62988ab1932SEric Dumazet 
630271b72c7SEric Dumazet 	if (result) {
631e32ea7e7SCraig Gallek found:
632c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
633271b72c7SEric Dumazet 			result = NULL;
634271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, saddr, hnum, sport,
635271b72c7SEric Dumazet 				  daddr, dport, dif) < badness)) {
636271b72c7SEric Dumazet 			sock_put(result);
637271b72c7SEric Dumazet 			goto begin;
638271b72c7SEric Dumazet 		}
639271b72c7SEric Dumazet 	}
640271b72c7SEric Dumazet 	rcu_read_unlock();
641db8dac20SDavid S. Miller 	return result;
642db8dac20SDavid S. Miller }
643fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
644db8dac20SDavid S. Miller 
645607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
646607c4aafSKOVACS Krisztian 						 __be16 sport, __be16 dport,
647645ca708SEric Dumazet 						 struct udp_table *udptable)
648607c4aafSKOVACS Krisztian {
649607c4aafSKOVACS Krisztian 	const struct iphdr *iph = ip_hdr(skb);
650607c4aafSKOVACS Krisztian 
651adf30907SEric Dumazet 	return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
652607c4aafSKOVACS Krisztian 				 iph->daddr, dport, inet_iif(skb),
653538950a1SCraig Gallek 				 udptable, skb);
654607c4aafSKOVACS Krisztian }
655607c4aafSKOVACS Krisztian 
656bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
657bcd41303SKOVACS Krisztian 			     __be32 daddr, __be16 dport, int dif)
658bcd41303SKOVACS Krisztian {
659e32ea7e7SCraig Gallek 	return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif,
660538950a1SCraig Gallek 				 &udp_table, NULL);
661bcd41303SKOVACS Krisztian }
662bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup);
663bcd41303SKOVACS Krisztian 
664421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
665421b3885SShawn Bohrer 				       __be16 loc_port, __be32 loc_addr,
666421b3885SShawn Bohrer 				       __be16 rmt_port, __be32 rmt_addr,
667421b3885SShawn Bohrer 				       int dif, unsigned short hnum)
668421b3885SShawn Bohrer {
669421b3885SShawn Bohrer 	struct inet_sock *inet = inet_sk(sk);
670421b3885SShawn Bohrer 
671421b3885SShawn Bohrer 	if (!net_eq(sock_net(sk), net) ||
672421b3885SShawn Bohrer 	    udp_sk(sk)->udp_port_hash != hnum ||
673421b3885SShawn Bohrer 	    (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
674421b3885SShawn Bohrer 	    (inet->inet_dport != rmt_port && inet->inet_dport) ||
675421b3885SShawn Bohrer 	    (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
676421b3885SShawn Bohrer 	    ipv6_only_sock(sk) ||
677421b3885SShawn Bohrer 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
678421b3885SShawn Bohrer 		return false;
679421b3885SShawn Bohrer 	if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif))
680421b3885SShawn Bohrer 		return false;
681421b3885SShawn Bohrer 	return true;
682421b3885SShawn Bohrer }
683421b3885SShawn Bohrer 
684db8dac20SDavid S. Miller /*
685db8dac20SDavid S. Miller  * This routine is called by the ICMP module when it gets some
686db8dac20SDavid S. Miller  * sort of error condition.  If err < 0 then the socket should
687db8dac20SDavid S. Miller  * be closed and the error returned to the user.  If err > 0
688db8dac20SDavid S. Miller  * it's just the icmp type << 8 | icmp code.
689db8dac20SDavid S. Miller  * Header points to the ip header of the error packet. We move
690db8dac20SDavid S. Miller  * on past this. Then (as it used to claim before adjustment)
691db8dac20SDavid S. Miller  * header points to the first 8 bytes of the udp header.  We need
692db8dac20SDavid S. Miller  * to find the appropriate port.
693db8dac20SDavid S. Miller  */
694db8dac20SDavid S. Miller 
695645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
696db8dac20SDavid S. Miller {
697db8dac20SDavid S. Miller 	struct inet_sock *inet;
698b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *)skb->data;
699db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
700db8dac20SDavid S. Miller 	const int type = icmp_hdr(skb)->type;
701db8dac20SDavid S. Miller 	const int code = icmp_hdr(skb)->code;
702db8dac20SDavid S. Miller 	struct sock *sk;
703db8dac20SDavid S. Miller 	int harderr;
704db8dac20SDavid S. Miller 	int err;
705fd54d716SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
706db8dac20SDavid S. Miller 
707fd54d716SPavel Emelyanov 	sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
708538950a1SCraig Gallek 			iph->saddr, uh->source, skb->dev->ifindex, udptable,
709538950a1SCraig Gallek 			NULL);
71051456b29SIan Morris 	if (!sk) {
711dcfc23caSPavel Emelyanov 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
712db8dac20SDavid S. Miller 		return;	/* No socket for error */
713db8dac20SDavid S. Miller 	}
714db8dac20SDavid S. Miller 
715db8dac20SDavid S. Miller 	err = 0;
716db8dac20SDavid S. Miller 	harderr = 0;
717db8dac20SDavid S. Miller 	inet = inet_sk(sk);
718db8dac20SDavid S. Miller 
719db8dac20SDavid S. Miller 	switch (type) {
720db8dac20SDavid S. Miller 	default:
721db8dac20SDavid S. Miller 	case ICMP_TIME_EXCEEDED:
722db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
723db8dac20SDavid S. Miller 		break;
724db8dac20SDavid S. Miller 	case ICMP_SOURCE_QUENCH:
725db8dac20SDavid S. Miller 		goto out;
726db8dac20SDavid S. Miller 	case ICMP_PARAMETERPROB:
727db8dac20SDavid S. Miller 		err = EPROTO;
728db8dac20SDavid S. Miller 		harderr = 1;
729db8dac20SDavid S. Miller 		break;
730db8dac20SDavid S. Miller 	case ICMP_DEST_UNREACH:
731db8dac20SDavid S. Miller 		if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
73236393395SDavid S. Miller 			ipv4_sk_update_pmtu(skb, sk, info);
733db8dac20SDavid S. Miller 			if (inet->pmtudisc != IP_PMTUDISC_DONT) {
734db8dac20SDavid S. Miller 				err = EMSGSIZE;
735db8dac20SDavid S. Miller 				harderr = 1;
736db8dac20SDavid S. Miller 				break;
737db8dac20SDavid S. Miller 			}
738db8dac20SDavid S. Miller 			goto out;
739db8dac20SDavid S. Miller 		}
740db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
741db8dac20SDavid S. Miller 		if (code <= NR_ICMP_UNREACH) {
742db8dac20SDavid S. Miller 			harderr = icmp_err_convert[code].fatal;
743db8dac20SDavid S. Miller 			err = icmp_err_convert[code].errno;
744db8dac20SDavid S. Miller 		}
745db8dac20SDavid S. Miller 		break;
74655be7a9cSDavid S. Miller 	case ICMP_REDIRECT:
74755be7a9cSDavid S. Miller 		ipv4_sk_redirect(skb, sk);
7481a462d18SDuan Jiong 		goto out;
749db8dac20SDavid S. Miller 	}
750db8dac20SDavid S. Miller 
751db8dac20SDavid S. Miller 	/*
752db8dac20SDavid S. Miller 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
753db8dac20SDavid S. Miller 	 *	4.1.3.3.
754db8dac20SDavid S. Miller 	 */
755db8dac20SDavid S. Miller 	if (!inet->recverr) {
756db8dac20SDavid S. Miller 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
757db8dac20SDavid S. Miller 			goto out;
758b1faf566SEric Dumazet 	} else
759db8dac20SDavid S. Miller 		ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
760b1faf566SEric Dumazet 
761db8dac20SDavid S. Miller 	sk->sk_err = err;
762db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
763db8dac20SDavid S. Miller out:
764db8dac20SDavid S. Miller 	sock_put(sk);
765db8dac20SDavid S. Miller }
766db8dac20SDavid S. Miller 
767db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info)
768db8dac20SDavid S. Miller {
769645ca708SEric Dumazet 	__udp4_lib_err(skb, info, &udp_table);
770db8dac20SDavid S. Miller }
771db8dac20SDavid S. Miller 
772db8dac20SDavid S. Miller /*
773db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
774db8dac20SDavid S. Miller  */
77536d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk)
776db8dac20SDavid S. Miller {
777db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
778db8dac20SDavid S. Miller 
779db8dac20SDavid S. Miller 	if (up->pending) {
780db8dac20SDavid S. Miller 		up->len = 0;
781db8dac20SDavid S. Miller 		up->pending = 0;
782db8dac20SDavid S. Miller 		ip_flush_pending_frames(sk);
783db8dac20SDavid S. Miller 	}
784db8dac20SDavid S. Miller }
78536d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames);
786db8dac20SDavid S. Miller 
787db8dac20SDavid S. Miller /**
788f6b9664fSHerbert Xu  * 	udp4_hwcsum  -  handle outgoing HW checksumming
789db8dac20SDavid S. Miller  * 	@skb: 	sk_buff containing the filled-in UDP header
790db8dac20SDavid S. Miller  * 	        (checksum field must be zeroed out)
791f6b9664fSHerbert Xu  *	@src:	source IP address
792f6b9664fSHerbert Xu  *	@dst:	destination IP address
793db8dac20SDavid S. Miller  */
794c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
795db8dac20SDavid S. Miller {
796db8dac20SDavid S. Miller 	struct udphdr *uh = udp_hdr(skb);
797f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
798f6b9664fSHerbert Xu 	int len = skb->len - offset;
799f6b9664fSHerbert Xu 	int hlen = len;
800db8dac20SDavid S. Miller 	__wsum csum = 0;
801db8dac20SDavid S. Miller 
802ebbe495fSWANG Cong 	if (!skb_has_frag_list(skb)) {
803db8dac20SDavid S. Miller 		/*
804db8dac20SDavid S. Miller 		 * Only one fragment on the socket.
805db8dac20SDavid S. Miller 		 */
806db8dac20SDavid S. Miller 		skb->csum_start = skb_transport_header(skb) - skb->head;
807db8dac20SDavid S. Miller 		skb->csum_offset = offsetof(struct udphdr, check);
808f6b9664fSHerbert Xu 		uh->check = ~csum_tcpudp_magic(src, dst, len,
809f6b9664fSHerbert Xu 					       IPPROTO_UDP, 0);
810db8dac20SDavid S. Miller 	} else {
811ebbe495fSWANG Cong 		struct sk_buff *frags;
812ebbe495fSWANG Cong 
813db8dac20SDavid S. Miller 		/*
814db8dac20SDavid S. Miller 		 * HW-checksum won't work as there are two or more
815db8dac20SDavid S. Miller 		 * fragments on the socket so that all csums of sk_buffs
816db8dac20SDavid S. Miller 		 * should be together
817db8dac20SDavid S. Miller 		 */
818ebbe495fSWANG Cong 		skb_walk_frags(skb, frags) {
819f6b9664fSHerbert Xu 			csum = csum_add(csum, frags->csum);
820f6b9664fSHerbert Xu 			hlen -= frags->len;
821ebbe495fSWANG Cong 		}
822db8dac20SDavid S. Miller 
823f6b9664fSHerbert Xu 		csum = skb_checksum(skb, offset, hlen, csum);
824db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
825db8dac20SDavid S. Miller 
826db8dac20SDavid S. Miller 		uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
827db8dac20SDavid S. Miller 		if (uh->check == 0)
828db8dac20SDavid S. Miller 			uh->check = CSUM_MANGLED_0;
829db8dac20SDavid S. Miller 	}
830db8dac20SDavid S. Miller }
831c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum);
832db8dac20SDavid S. Miller 
833af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended
834af5fcba7STom Herbert  * for the simple case like when setting the checksum for a UDP tunnel.
835af5fcba7STom Herbert  */
836af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb,
837af5fcba7STom Herbert 		  __be32 saddr, __be32 daddr, int len)
838af5fcba7STom Herbert {
839af5fcba7STom Herbert 	struct udphdr *uh = udp_hdr(skb);
840af5fcba7STom Herbert 
841af5fcba7STom Herbert 	if (nocheck)
842af5fcba7STom Herbert 		uh->check = 0;
843af5fcba7STom Herbert 	else if (skb_is_gso(skb))
844af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
845af5fcba7STom Herbert 	else if (skb_dst(skb) && skb_dst(skb)->dev &&
846c8cd0989STom Herbert 		 (skb_dst(skb)->dev->features &
847c8cd0989STom Herbert 		  (NETIF_F_IP_CSUM | NETIF_F_HW_CSUM))) {
848af5fcba7STom Herbert 
849af5fcba7STom Herbert 		BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL);
850af5fcba7STom Herbert 
851af5fcba7STom Herbert 		skb->ip_summed = CHECKSUM_PARTIAL;
852af5fcba7STom Herbert 		skb->csum_start = skb_transport_header(skb) - skb->head;
853af5fcba7STom Herbert 		skb->csum_offset = offsetof(struct udphdr, check);
854af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
855af5fcba7STom Herbert 	} else {
856af5fcba7STom Herbert 		__wsum csum;
857af5fcba7STom Herbert 
858af5fcba7STom Herbert 		BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL);
859af5fcba7STom Herbert 
860af5fcba7STom Herbert 		uh->check = 0;
861af5fcba7STom Herbert 		csum = skb_checksum(skb, 0, len, 0);
862af5fcba7STom Herbert 		uh->check = udp_v4_check(len, saddr, daddr, csum);
863af5fcba7STom Herbert 		if (uh->check == 0)
864af5fcba7STom Herbert 			uh->check = CSUM_MANGLED_0;
865af5fcba7STom Herbert 
866af5fcba7STom Herbert 		skb->ip_summed = CHECKSUM_UNNECESSARY;
867af5fcba7STom Herbert 	}
868af5fcba7STom Herbert }
869af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum);
870af5fcba7STom Herbert 
87179ab0531SDavid S. Miller static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4)
872f6b9664fSHerbert Xu {
873f6b9664fSHerbert Xu 	struct sock *sk = skb->sk;
874f6b9664fSHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
875f6b9664fSHerbert Xu 	struct udphdr *uh;
876f6b9664fSHerbert Xu 	int err = 0;
877f6b9664fSHerbert Xu 	int is_udplite = IS_UDPLITE(sk);
878f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
879f6b9664fSHerbert Xu 	int len = skb->len - offset;
880f6b9664fSHerbert Xu 	__wsum csum = 0;
881f6b9664fSHerbert Xu 
882f6b9664fSHerbert Xu 	/*
883f6b9664fSHerbert Xu 	 * Create a UDP header
884f6b9664fSHerbert Xu 	 */
885f6b9664fSHerbert Xu 	uh = udp_hdr(skb);
886f6b9664fSHerbert Xu 	uh->source = inet->inet_sport;
88779ab0531SDavid S. Miller 	uh->dest = fl4->fl4_dport;
888f6b9664fSHerbert Xu 	uh->len = htons(len);
889f6b9664fSHerbert Xu 	uh->check = 0;
890f6b9664fSHerbert Xu 
891f6b9664fSHerbert Xu 	if (is_udplite)  				 /*     UDP-Lite      */
892f6b9664fSHerbert Xu 		csum = udplite_csum(skb);
893f6b9664fSHerbert Xu 
89428448b80STom Herbert 	else if (sk->sk_no_check_tx) {   /* UDP csum disabled */
895f6b9664fSHerbert Xu 
896f6b9664fSHerbert Xu 		skb->ip_summed = CHECKSUM_NONE;
897f6b9664fSHerbert Xu 		goto send;
898f6b9664fSHerbert Xu 
899f6b9664fSHerbert Xu 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
900f6b9664fSHerbert Xu 
90179ab0531SDavid S. Miller 		udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
902f6b9664fSHerbert Xu 		goto send;
903f6b9664fSHerbert Xu 
904f6b9664fSHerbert Xu 	} else
905f6b9664fSHerbert Xu 		csum = udp_csum(skb);
906f6b9664fSHerbert Xu 
907f6b9664fSHerbert Xu 	/* add protocol-dependent pseudo-header */
90879ab0531SDavid S. Miller 	uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
909f6b9664fSHerbert Xu 				      sk->sk_protocol, csum);
910f6b9664fSHerbert Xu 	if (uh->check == 0)
911f6b9664fSHerbert Xu 		uh->check = CSUM_MANGLED_0;
912f6b9664fSHerbert Xu 
913f6b9664fSHerbert Xu send:
914b5ec8eeaSEric Dumazet 	err = ip_send_skb(sock_net(sk), skb);
915f6b9664fSHerbert Xu 	if (err) {
916f6b9664fSHerbert Xu 		if (err == -ENOBUFS && !inet->recverr) {
917f6b9664fSHerbert Xu 			UDP_INC_STATS_USER(sock_net(sk),
918f6b9664fSHerbert Xu 					   UDP_MIB_SNDBUFERRORS, is_udplite);
919f6b9664fSHerbert Xu 			err = 0;
920f6b9664fSHerbert Xu 		}
921f6b9664fSHerbert Xu 	} else
922f6b9664fSHerbert Xu 		UDP_INC_STATS_USER(sock_net(sk),
923f6b9664fSHerbert Xu 				   UDP_MIB_OUTDATAGRAMS, is_udplite);
924f6b9664fSHerbert Xu 	return err;
925f6b9664fSHerbert Xu }
926f6b9664fSHerbert Xu 
927db8dac20SDavid S. Miller /*
928db8dac20SDavid S. Miller  * Push out all pending data as one UDP datagram. Socket is locked.
929db8dac20SDavid S. Miller  */
9308822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk)
931db8dac20SDavid S. Miller {
932db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
933db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
934b6f21b26SDavid S. Miller 	struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
935db8dac20SDavid S. Miller 	struct sk_buff *skb;
936db8dac20SDavid S. Miller 	int err = 0;
937db8dac20SDavid S. Miller 
93877968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
939f6b9664fSHerbert Xu 	if (!skb)
940db8dac20SDavid S. Miller 		goto out;
941db8dac20SDavid S. Miller 
94279ab0531SDavid S. Miller 	err = udp_send_skb(skb, fl4);
943db8dac20SDavid S. Miller 
944db8dac20SDavid S. Miller out:
945db8dac20SDavid S. Miller 	up->len = 0;
946db8dac20SDavid S. Miller 	up->pending = 0;
947db8dac20SDavid S. Miller 	return err;
948db8dac20SDavid S. Miller }
9498822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames);
950db8dac20SDavid S. Miller 
9511b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
952db8dac20SDavid S. Miller {
953db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
954db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
955e474995fSDavid S. Miller 	struct flowi4 fl4_stack;
956b6f21b26SDavid S. Miller 	struct flowi4 *fl4;
957db8dac20SDavid S. Miller 	int ulen = len;
958db8dac20SDavid S. Miller 	struct ipcm_cookie ipc;
959db8dac20SDavid S. Miller 	struct rtable *rt = NULL;
960db8dac20SDavid S. Miller 	int free = 0;
961db8dac20SDavid S. Miller 	int connected = 0;
962db8dac20SDavid S. Miller 	__be32 daddr, faddr, saddr;
963db8dac20SDavid S. Miller 	__be16 dport;
964db8dac20SDavid S. Miller 	u8  tos;
965db8dac20SDavid S. Miller 	int err, is_udplite = IS_UDPLITE(sk);
966db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
967db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
968903ab86dSHerbert Xu 	struct sk_buff *skb;
969f6d8bd05SEric Dumazet 	struct ip_options_data opt_copy;
970db8dac20SDavid S. Miller 
971db8dac20SDavid S. Miller 	if (len > 0xFFFF)
972db8dac20SDavid S. Miller 		return -EMSGSIZE;
973db8dac20SDavid S. Miller 
974db8dac20SDavid S. Miller 	/*
975db8dac20SDavid S. Miller 	 *	Check the flags.
976db8dac20SDavid S. Miller 	 */
977db8dac20SDavid S. Miller 
978db8dac20SDavid S. Miller 	if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
979db8dac20SDavid S. Miller 		return -EOPNOTSUPP;
980db8dac20SDavid S. Miller 
981db8dac20SDavid S. Miller 	ipc.opt = NULL;
9822244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
983aa661581SFrancesco Fusco 	ipc.ttl = 0;
984aa661581SFrancesco Fusco 	ipc.tos = -1;
985db8dac20SDavid S. Miller 
986903ab86dSHerbert Xu 	getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
987903ab86dSHerbert Xu 
988f5fca608SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
989db8dac20SDavid S. Miller 	if (up->pending) {
990db8dac20SDavid S. Miller 		/*
991db8dac20SDavid S. Miller 		 * There are pending frames.
992db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
993db8dac20SDavid S. Miller 		 */
994db8dac20SDavid S. Miller 		lock_sock(sk);
995db8dac20SDavid S. Miller 		if (likely(up->pending)) {
996db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET)) {
997db8dac20SDavid S. Miller 				release_sock(sk);
998db8dac20SDavid S. Miller 				return -EINVAL;
999db8dac20SDavid S. Miller 			}
1000db8dac20SDavid S. Miller 			goto do_append_data;
1001db8dac20SDavid S. Miller 		}
1002db8dac20SDavid S. Miller 		release_sock(sk);
1003db8dac20SDavid S. Miller 	}
1004db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1005db8dac20SDavid S. Miller 
1006db8dac20SDavid S. Miller 	/*
1007db8dac20SDavid S. Miller 	 *	Get and verify the address.
1008db8dac20SDavid S. Miller 	 */
1009db8dac20SDavid S. Miller 	if (msg->msg_name) {
1010342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
1011db8dac20SDavid S. Miller 		if (msg->msg_namelen < sizeof(*usin))
1012db8dac20SDavid S. Miller 			return -EINVAL;
1013db8dac20SDavid S. Miller 		if (usin->sin_family != AF_INET) {
1014db8dac20SDavid S. Miller 			if (usin->sin_family != AF_UNSPEC)
1015db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1016db8dac20SDavid S. Miller 		}
1017db8dac20SDavid S. Miller 
1018db8dac20SDavid S. Miller 		daddr = usin->sin_addr.s_addr;
1019db8dac20SDavid S. Miller 		dport = usin->sin_port;
1020db8dac20SDavid S. Miller 		if (dport == 0)
1021db8dac20SDavid S. Miller 			return -EINVAL;
1022db8dac20SDavid S. Miller 	} else {
1023db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1024db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1025c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
1026c720c7e8SEric Dumazet 		dport = inet->inet_dport;
1027db8dac20SDavid S. Miller 		/* Open fast path for connected socket.
1028db8dac20SDavid S. Miller 		   Route will not be used, if at least one option is set.
1029db8dac20SDavid S. Miller 		 */
1030db8dac20SDavid S. Miller 		connected = 1;
1031db8dac20SDavid S. Miller 	}
1032c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
1033db8dac20SDavid S. Miller 
1034db8dac20SDavid S. Miller 	ipc.oif = sk->sk_bound_dev_if;
1035bf84a010SDaniel Borkmann 
1036bf84a010SDaniel Borkmann 	sock_tx_timestamp(sk, &ipc.tx_flags);
1037bf84a010SDaniel Borkmann 
1038db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1039c8e6ad08SHannes Frederic Sowa 		err = ip_cmsg_send(sock_net(sk), msg, &ipc,
1040c8e6ad08SHannes Frederic Sowa 				   sk->sk_family == AF_INET6);
1041db8dac20SDavid S. Miller 		if (err)
1042db8dac20SDavid S. Miller 			return err;
1043db8dac20SDavid S. Miller 		if (ipc.opt)
1044db8dac20SDavid S. Miller 			free = 1;
1045db8dac20SDavid S. Miller 		connected = 0;
1046db8dac20SDavid S. Miller 	}
1047f6d8bd05SEric Dumazet 	if (!ipc.opt) {
1048f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
1049f6d8bd05SEric Dumazet 
1050f6d8bd05SEric Dumazet 		rcu_read_lock();
1051f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference(inet->inet_opt);
1052f6d8bd05SEric Dumazet 		if (inet_opt) {
1053f6d8bd05SEric Dumazet 			memcpy(&opt_copy, inet_opt,
1054f6d8bd05SEric Dumazet 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
1055f6d8bd05SEric Dumazet 			ipc.opt = &opt_copy.opt;
1056f6d8bd05SEric Dumazet 		}
1057f6d8bd05SEric Dumazet 		rcu_read_unlock();
1058f6d8bd05SEric Dumazet 	}
1059db8dac20SDavid S. Miller 
1060db8dac20SDavid S. Miller 	saddr = ipc.addr;
1061db8dac20SDavid S. Miller 	ipc.addr = faddr = daddr;
1062db8dac20SDavid S. Miller 
1063f6d8bd05SEric Dumazet 	if (ipc.opt && ipc.opt->opt.srr) {
1064db8dac20SDavid S. Miller 		if (!daddr)
1065db8dac20SDavid S. Miller 			return -EINVAL;
1066f6d8bd05SEric Dumazet 		faddr = ipc.opt->opt.faddr;
1067db8dac20SDavid S. Miller 		connected = 0;
1068db8dac20SDavid S. Miller 	}
1069aa661581SFrancesco Fusco 	tos = get_rttos(&ipc, inet);
1070db8dac20SDavid S. Miller 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
1071db8dac20SDavid S. Miller 	    (msg->msg_flags & MSG_DONTROUTE) ||
1072f6d8bd05SEric Dumazet 	    (ipc.opt && ipc.opt->opt.is_strictroute)) {
1073db8dac20SDavid S. Miller 		tos |= RTO_ONLINK;
1074db8dac20SDavid S. Miller 		connected = 0;
1075db8dac20SDavid S. Miller 	}
1076db8dac20SDavid S. Miller 
1077db8dac20SDavid S. Miller 	if (ipv4_is_multicast(daddr)) {
1078db8dac20SDavid S. Miller 		if (!ipc.oif)
1079db8dac20SDavid S. Miller 			ipc.oif = inet->mc_index;
1080db8dac20SDavid S. Miller 		if (!saddr)
1081db8dac20SDavid S. Miller 			saddr = inet->mc_addr;
1082db8dac20SDavid S. Miller 		connected = 0;
108376e21053SErich E. Hoover 	} else if (!ipc.oif)
108476e21053SErich E. Hoover 		ipc.oif = inet->uc_index;
1085db8dac20SDavid S. Miller 
1086db8dac20SDavid S. Miller 	if (connected)
1087db8dac20SDavid S. Miller 		rt = (struct rtable *)sk_dst_check(sk, 0);
1088db8dac20SDavid S. Miller 
108951456b29SIan Morris 	if (!rt) {
109084a3aa00SPavel Emelyanov 		struct net *net = sock_net(sk);
10919a24abfaSDavid Ahern 		__u8 flow_flags = inet_sk_flowi_flags(sk);
109284a3aa00SPavel Emelyanov 
1093e474995fSDavid S. Miller 		fl4 = &fl4_stack;
10949a24abfaSDavid Ahern 
10959a24abfaSDavid Ahern 		flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
10969a24abfaSDavid Ahern 				   RT_SCOPE_UNIVERSE, sk->sk_protocol,
10979a24abfaSDavid Ahern 				   flow_flags,
1098c0951cbcSDavid S. Miller 				   faddr, saddr, dport, inet->inet_sport);
1099c0951cbcSDavid S. Miller 
11008cbb512cSDavid Ahern 		if (!saddr && ipc.oif)
11018cbb512cSDavid Ahern 			l3mdev_get_saddr(net, ipc.oif, fl4);
11028cbb512cSDavid Ahern 
1103e474995fSDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
1104e474995fSDavid S. Miller 		rt = ip_route_output_flow(net, fl4, sk);
1105b23dd4feSDavid S. Miller 		if (IS_ERR(rt)) {
1106b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
110706dc94b1SDavid S. Miller 			rt = NULL;
1108db8dac20SDavid S. Miller 			if (err == -ENETUNREACH)
1109f1d8cba6SEric Dumazet 				IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
1110db8dac20SDavid S. Miller 			goto out;
1111db8dac20SDavid S. Miller 		}
1112db8dac20SDavid S. Miller 
1113db8dac20SDavid S. Miller 		err = -EACCES;
1114db8dac20SDavid S. Miller 		if ((rt->rt_flags & RTCF_BROADCAST) &&
1115db8dac20SDavid S. Miller 		    !sock_flag(sk, SOCK_BROADCAST))
1116db8dac20SDavid S. Miller 			goto out;
1117db8dac20SDavid S. Miller 		if (connected)
1118d8d1f30bSChangli Gao 			sk_dst_set(sk, dst_clone(&rt->dst));
1119db8dac20SDavid S. Miller 	}
1120db8dac20SDavid S. Miller 
1121db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1122db8dac20SDavid S. Miller 		goto do_confirm;
1123db8dac20SDavid S. Miller back_from_confirm:
1124db8dac20SDavid S. Miller 
1125e474995fSDavid S. Miller 	saddr = fl4->saddr;
1126db8dac20SDavid S. Miller 	if (!ipc.addr)
1127e474995fSDavid S. Miller 		daddr = ipc.addr = fl4->daddr;
1128db8dac20SDavid S. Miller 
1129903ab86dSHerbert Xu 	/* Lockless fast path for the non-corking case. */
1130903ab86dSHerbert Xu 	if (!corkreq) {
1131f69e6d13SAl Viro 		skb = ip_make_skb(sk, fl4, getfrag, msg, ulen,
1132903ab86dSHerbert Xu 				  sizeof(struct udphdr), &ipc, &rt,
1133903ab86dSHerbert Xu 				  msg->msg_flags);
1134903ab86dSHerbert Xu 		err = PTR_ERR(skb);
113550c3a487SYOSHIFUJI Hideaki / 吉藤英明 		if (!IS_ERR_OR_NULL(skb))
113679ab0531SDavid S. Miller 			err = udp_send_skb(skb, fl4);
1137903ab86dSHerbert Xu 		goto out;
1138903ab86dSHerbert Xu 	}
1139903ab86dSHerbert Xu 
1140db8dac20SDavid S. Miller 	lock_sock(sk);
1141db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1142db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1143db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1144db8dac20SDavid S. Miller 		release_sock(sk);
1145db8dac20SDavid S. Miller 
1146ba7a46f1SJoe Perches 		net_dbg_ratelimited("cork app bug 2\n");
1147db8dac20SDavid S. Miller 		err = -EINVAL;
1148db8dac20SDavid S. Miller 		goto out;
1149db8dac20SDavid S. Miller 	}
1150db8dac20SDavid S. Miller 	/*
1151db8dac20SDavid S. Miller 	 *	Now cork the socket to pend data.
1152db8dac20SDavid S. Miller 	 */
1153b6f21b26SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
1154b6f21b26SDavid S. Miller 	fl4->daddr = daddr;
1155b6f21b26SDavid S. Miller 	fl4->saddr = saddr;
11569cce96dfSDavid S. Miller 	fl4->fl4_dport = dport;
11579cce96dfSDavid S. Miller 	fl4->fl4_sport = inet->inet_sport;
1158db8dac20SDavid S. Miller 	up->pending = AF_INET;
1159db8dac20SDavid S. Miller 
1160db8dac20SDavid S. Miller do_append_data:
1161db8dac20SDavid S. Miller 	up->len += ulen;
1162f69e6d13SAl Viro 	err = ip_append_data(sk, fl4, getfrag, msg, ulen,
11632e77d89bSEric Dumazet 			     sizeof(struct udphdr), &ipc, &rt,
1164db8dac20SDavid S. Miller 			     corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1165db8dac20SDavid S. Miller 	if (err)
1166db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1167db8dac20SDavid S. Miller 	else if (!corkreq)
1168db8dac20SDavid S. Miller 		err = udp_push_pending_frames(sk);
1169db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1170db8dac20SDavid S. Miller 		up->pending = 0;
1171db8dac20SDavid S. Miller 	release_sock(sk);
1172db8dac20SDavid S. Miller 
1173db8dac20SDavid S. Miller out:
1174db8dac20SDavid S. Miller 	ip_rt_put(rt);
1175db8dac20SDavid S. Miller 	if (free)
1176db8dac20SDavid S. Miller 		kfree(ipc.opt);
1177db8dac20SDavid S. Miller 	if (!err)
1178db8dac20SDavid S. Miller 		return len;
1179db8dac20SDavid S. Miller 	/*
1180db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1181db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1182db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1183db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1184db8dac20SDavid S. Miller 	 * seems like overkill.
1185db8dac20SDavid S. Miller 	 */
1186db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1187629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1188629ca23cSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1189db8dac20SDavid S. Miller 	}
1190db8dac20SDavid S. Miller 	return err;
1191db8dac20SDavid S. Miller 
1192db8dac20SDavid S. Miller do_confirm:
1193d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
1194db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1195db8dac20SDavid S. Miller 		goto back_from_confirm;
1196db8dac20SDavid S. Miller 	err = 0;
1197db8dac20SDavid S. Miller 	goto out;
1198db8dac20SDavid S. Miller }
1199c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg);
1200db8dac20SDavid S. Miller 
1201db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset,
1202db8dac20SDavid S. Miller 		 size_t size, int flags)
1203db8dac20SDavid S. Miller {
1204f5fca608SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1205db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1206db8dac20SDavid S. Miller 	int ret;
1207db8dac20SDavid S. Miller 
1208d3f7d56aSShawn Landden 	if (flags & MSG_SENDPAGE_NOTLAST)
1209d3f7d56aSShawn Landden 		flags |= MSG_MORE;
1210d3f7d56aSShawn Landden 
1211db8dac20SDavid S. Miller 	if (!up->pending) {
1212db8dac20SDavid S. Miller 		struct msghdr msg = {	.msg_flags = flags|MSG_MORE };
1213db8dac20SDavid S. Miller 
1214db8dac20SDavid S. Miller 		/* Call udp_sendmsg to specify destination address which
1215db8dac20SDavid S. Miller 		 * sendpage interface can't pass.
1216db8dac20SDavid S. Miller 		 * This will succeed only when the socket is connected.
1217db8dac20SDavid S. Miller 		 */
12181b784140SYing Xue 		ret = udp_sendmsg(sk, &msg, 0);
1219db8dac20SDavid S. Miller 		if (ret < 0)
1220db8dac20SDavid S. Miller 			return ret;
1221db8dac20SDavid S. Miller 	}
1222db8dac20SDavid S. Miller 
1223db8dac20SDavid S. Miller 	lock_sock(sk);
1224db8dac20SDavid S. Miller 
1225db8dac20SDavid S. Miller 	if (unlikely(!up->pending)) {
1226db8dac20SDavid S. Miller 		release_sock(sk);
1227db8dac20SDavid S. Miller 
1228ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 3\n");
1229db8dac20SDavid S. Miller 		return -EINVAL;
1230db8dac20SDavid S. Miller 	}
1231db8dac20SDavid S. Miller 
1232f5fca608SDavid S. Miller 	ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
1233f5fca608SDavid S. Miller 			     page, offset, size, flags);
1234db8dac20SDavid S. Miller 	if (ret == -EOPNOTSUPP) {
1235db8dac20SDavid S. Miller 		release_sock(sk);
1236db8dac20SDavid S. Miller 		return sock_no_sendpage(sk->sk_socket, page, offset,
1237db8dac20SDavid S. Miller 					size, flags);
1238db8dac20SDavid S. Miller 	}
1239db8dac20SDavid S. Miller 	if (ret < 0) {
1240db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1241db8dac20SDavid S. Miller 		goto out;
1242db8dac20SDavid S. Miller 	}
1243db8dac20SDavid S. Miller 
1244db8dac20SDavid S. Miller 	up->len += size;
1245db8dac20SDavid S. Miller 	if (!(up->corkflag || (flags&MSG_MORE)))
1246db8dac20SDavid S. Miller 		ret = udp_push_pending_frames(sk);
1247db8dac20SDavid S. Miller 	if (!ret)
1248db8dac20SDavid S. Miller 		ret = size;
1249db8dac20SDavid S. Miller out:
1250db8dac20SDavid S. Miller 	release_sock(sk);
1251db8dac20SDavid S. Miller 	return ret;
1252db8dac20SDavid S. Miller }
1253db8dac20SDavid S. Miller 
125485584672SEric Dumazet /**
125585584672SEric Dumazet  *	first_packet_length	- return length of first packet in receive queue
125685584672SEric Dumazet  *	@sk: socket
125785584672SEric Dumazet  *
125885584672SEric Dumazet  *	Drops all bad checksum frames, until a valid one is found.
125985584672SEric Dumazet  *	Returns the length of found skb, or 0 if none is found.
126085584672SEric Dumazet  */
126185584672SEric Dumazet static unsigned int first_packet_length(struct sock *sk)
126285584672SEric Dumazet {
126385584672SEric Dumazet 	struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
126485584672SEric Dumazet 	struct sk_buff *skb;
126585584672SEric Dumazet 	unsigned int res;
126685584672SEric Dumazet 
126785584672SEric Dumazet 	__skb_queue_head_init(&list_kill);
126885584672SEric Dumazet 
126985584672SEric Dumazet 	spin_lock_bh(&rcvq->lock);
127085584672SEric Dumazet 	while ((skb = skb_peek(rcvq)) != NULL &&
127185584672SEric Dumazet 		udp_lib_checksum_complete(skb)) {
12726a5dc9e5SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS,
12736a5dc9e5SEric Dumazet 				 IS_UDPLITE(sk));
127485584672SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
127585584672SEric Dumazet 				 IS_UDPLITE(sk));
12768edf19c2SEric Dumazet 		atomic_inc(&sk->sk_drops);
127785584672SEric Dumazet 		__skb_unlink(skb, rcvq);
127885584672SEric Dumazet 		__skb_queue_tail(&list_kill, skb);
127985584672SEric Dumazet 	}
128085584672SEric Dumazet 	res = skb ? skb->len : 0;
128185584672SEric Dumazet 	spin_unlock_bh(&rcvq->lock);
128285584672SEric Dumazet 
128385584672SEric Dumazet 	if (!skb_queue_empty(&list_kill)) {
12848a74ad60SEric Dumazet 		bool slow = lock_sock_fast(sk);
12858a74ad60SEric Dumazet 
128685584672SEric Dumazet 		__skb_queue_purge(&list_kill);
128785584672SEric Dumazet 		sk_mem_reclaim_partial(sk);
12888a74ad60SEric Dumazet 		unlock_sock_fast(sk, slow);
128985584672SEric Dumazet 	}
129085584672SEric Dumazet 	return res;
129185584672SEric Dumazet }
129285584672SEric Dumazet 
12931da177e4SLinus Torvalds /*
12941da177e4SLinus Torvalds  *	IOCTL requests applicable to the UDP protocol
12951da177e4SLinus Torvalds  */
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
12981da177e4SLinus Torvalds {
12996516c655SStephen Hemminger 	switch (cmd) {
13001da177e4SLinus Torvalds 	case SIOCOUTQ:
13011da177e4SLinus Torvalds 	{
130231e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
130331e6d363SEric Dumazet 
13041da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	case SIOCINQ:
13081da177e4SLinus Torvalds 	{
130985584672SEric Dumazet 		unsigned int amount = first_packet_length(sk);
13101da177e4SLinus Torvalds 
131185584672SEric Dumazet 		if (amount)
13121da177e4SLinus Torvalds 			/*
13131da177e4SLinus Torvalds 			 * We will only return the amount
13141da177e4SLinus Torvalds 			 * of this packet since that is all
13151da177e4SLinus Torvalds 			 * that will be read.
13161da177e4SLinus Torvalds 			 */
131785584672SEric Dumazet 			amount -= sizeof(struct udphdr);
131885584672SEric Dumazet 
13191da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	default:
13231da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
13241da177e4SLinus Torvalds 	}
13256516c655SStephen Hemminger 
13266516c655SStephen Hemminger 	return 0;
13271da177e4SLinus Torvalds }
1328c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl);
13291da177e4SLinus Torvalds 
1330db8dac20SDavid S. Miller /*
1331db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
1332db8dac20SDavid S. Miller  * 	return it, otherwise we block.
1333db8dac20SDavid S. Miller  */
1334db8dac20SDavid S. Miller 
13351b784140SYing Xue int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
13361b784140SYing Xue 		int flags, int *addr_len)
1337db8dac20SDavid S. Miller {
1338db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1339342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
1340db8dac20SDavid S. Miller 	struct sk_buff *skb;
134159c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
13423f518bf7SPavel Emelyanov 	int peeked, off = 0;
1343db8dac20SDavid S. Miller 	int err;
1344db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1345197c949eSEric Dumazet 	bool checksum_valid = false;
13468a74ad60SEric Dumazet 	bool slow;
1347db8dac20SDavid S. Miller 
1348db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
134985fbaa75SHannes Frederic Sowa 		return ip_recv_error(sk, msg, len, addr_len);
1350db8dac20SDavid S. Miller 
1351db8dac20SDavid S. Miller try_again:
1352db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
13533f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
1354db8dac20SDavid S. Miller 	if (!skb)
1355db8dac20SDavid S. Miller 		goto out;
1356db8dac20SDavid S. Miller 
1357db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
135859c2cdaeSDavid S. Miller 	copied = len;
135959c2cdaeSDavid S. Miller 	if (copied > ulen)
136059c2cdaeSDavid S. Miller 		copied = ulen;
136159c2cdaeSDavid S. Miller 	else if (copied < ulen)
1362db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
1363db8dac20SDavid S. Miller 
1364db8dac20SDavid S. Miller 	/*
1365db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
1366db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
1367db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
1368db8dac20SDavid S. Miller 	 */
1369db8dac20SDavid S. Miller 
137059c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
1371197c949eSEric Dumazet 		checksum_valid = !udp_lib_checksum_complete(skb);
1372197c949eSEric Dumazet 		if (!checksum_valid)
1373db8dac20SDavid S. Miller 			goto csum_copy_err;
1374db8dac20SDavid S. Miller 	}
1375db8dac20SDavid S. Miller 
1376197c949eSEric Dumazet 	if (checksum_valid || skb_csum_unnecessary(skb))
137751f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, sizeof(struct udphdr),
137851f3d02bSDavid S. Miller 					    msg, copied);
1379db8dac20SDavid S. Miller 	else {
1380227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, sizeof(struct udphdr),
1381227158dbSAl Viro 						     msg);
1382db8dac20SDavid S. Miller 
1383db8dac20SDavid S. Miller 		if (err == -EINVAL)
1384db8dac20SDavid S. Miller 			goto csum_copy_err;
1385db8dac20SDavid S. Miller 	}
1386db8dac20SDavid S. Miller 
138722911fc5SEric Dumazet 	if (unlikely(err)) {
138822911fc5SEric Dumazet 		trace_kfree_skb(skb, udp_recvmsg);
1389979402b1SEric Dumazet 		if (!peeked) {
1390979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
1391979402b1SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
1392979402b1SEric Dumazet 					   UDP_MIB_INERRORS, is_udplite);
1393979402b1SEric Dumazet 		}
1394db8dac20SDavid S. Miller 		goto out_free;
139522911fc5SEric Dumazet 	}
1396db8dac20SDavid S. Miller 
1397db8dac20SDavid S. Miller 	if (!peeked)
1398629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1399629ca23cSPavel Emelyanov 				UDP_MIB_INDATAGRAMS, is_udplite);
1400db8dac20SDavid S. Miller 
14013b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
1402db8dac20SDavid S. Miller 
1403db8dac20SDavid S. Miller 	/* Copy the address. */
1404c482c568SEric Dumazet 	if (sin) {
1405db8dac20SDavid S. Miller 		sin->sin_family = AF_INET;
1406db8dac20SDavid S. Miller 		sin->sin_port = udp_hdr(skb)->source;
1407db8dac20SDavid S. Miller 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1408db8dac20SDavid S. Miller 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1409bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin);
1410db8dac20SDavid S. Miller 	}
1411db8dac20SDavid S. Miller 	if (inet->cmsg_flags)
1412ad6f939aSTom Herbert 		ip_cmsg_recv_offset(msg, skb, sizeof(struct udphdr));
1413db8dac20SDavid S. Miller 
141459c2cdaeSDavid S. Miller 	err = copied;
1415db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
1416db8dac20SDavid S. Miller 		err = ulen;
1417db8dac20SDavid S. Miller 
1418db8dac20SDavid S. Miller out_free:
14199d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
1420db8dac20SDavid S. Miller out:
1421db8dac20SDavid S. Miller 	return err;
1422db8dac20SDavid S. Miller 
1423db8dac20SDavid S. Miller csum_copy_err:
14248a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
14256a5dc9e5SEric Dumazet 	if (!skb_kill_datagram(sk, skb, flags)) {
14266a5dc9e5SEric Dumazet 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1427629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
14286a5dc9e5SEric Dumazet 	}
14298a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
1430db8dac20SDavid S. Miller 
1431beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
1432beb39db5SEric Dumazet 	cond_resched();
14339cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
1434db8dac20SDavid S. Miller 	goto try_again;
1435db8dac20SDavid S. Miller }
1436db8dac20SDavid S. Miller 
14371da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags)
14381da177e4SLinus Torvalds {
14391da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
14401da177e4SLinus Torvalds 	/*
14411da177e4SLinus Torvalds 	 *	1003.1g - break association.
14421da177e4SLinus Torvalds 	 */
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
1445c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
1446c720c7e8SEric Dumazet 	inet->inet_dport = 0;
1447bdeab991STom Herbert 	sock_rps_reset_rxhash(sk);
14481da177e4SLinus Torvalds 	sk->sk_bound_dev_if = 0;
14491da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
14501da177e4SLinus Torvalds 		inet_reset_saddr(sk);
14511da177e4SLinus Torvalds 
14521da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
14531da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1454c720c7e8SEric Dumazet 		inet->inet_sport = 0;
14551da177e4SLinus Torvalds 	}
14561da177e4SLinus Torvalds 	sk_dst_reset(sk);
14571da177e4SLinus Torvalds 	return 0;
14581da177e4SLinus Torvalds }
1459c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect);
14601da177e4SLinus Torvalds 
1461645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk)
1462645ca708SEric Dumazet {
1463723b4610SEric Dumazet 	if (sk_hashed(sk)) {
1464645ca708SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1465512615b6SEric Dumazet 		struct udp_hslot *hslot, *hslot2;
1466512615b6SEric Dumazet 
1467512615b6SEric Dumazet 		hslot  = udp_hashslot(udptable, sock_net(sk),
1468d4cada4aSEric Dumazet 				      udp_sk(sk)->udp_port_hash);
1469512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1470645ca708SEric Dumazet 
1471c8db3fecSEric Dumazet 		spin_lock_bh(&hslot->lock);
1472e32ea7e7SCraig Gallek 		if (rcu_access_pointer(sk->sk_reuseport_cb))
1473e32ea7e7SCraig Gallek 			reuseport_detach_sock(sk);
147488ab1932SEric Dumazet 		if (sk_nulls_del_node_init_rcu(sk)) {
1475fdcc8aa9SEric Dumazet 			hslot->count--;
1476c720c7e8SEric Dumazet 			inet_sk(sk)->inet_num = 0;
1477645ca708SEric Dumazet 			sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1478512615b6SEric Dumazet 
1479512615b6SEric Dumazet 			spin_lock(&hslot2->lock);
1480512615b6SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1481512615b6SEric Dumazet 			hslot2->count--;
1482512615b6SEric Dumazet 			spin_unlock(&hslot2->lock);
1483645ca708SEric Dumazet 		}
1484c8db3fecSEric Dumazet 		spin_unlock_bh(&hslot->lock);
1485645ca708SEric Dumazet 	}
1486723b4610SEric Dumazet }
1487645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash);
1488645ca708SEric Dumazet 
1489719f8358SEric Dumazet /*
1490719f8358SEric Dumazet  * inet_rcv_saddr was changed, we must rehash secondary hash
1491719f8358SEric Dumazet  */
1492719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash)
1493719f8358SEric Dumazet {
1494719f8358SEric Dumazet 	if (sk_hashed(sk)) {
1495719f8358SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1496719f8358SEric Dumazet 		struct udp_hslot *hslot, *hslot2, *nhslot2;
1497719f8358SEric Dumazet 
1498719f8358SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1499719f8358SEric Dumazet 		nhslot2 = udp_hashslot2(udptable, newhash);
1500719f8358SEric Dumazet 		udp_sk(sk)->udp_portaddr_hash = newhash;
1501e32ea7e7SCraig Gallek 
1502e32ea7e7SCraig Gallek 		if (hslot2 != nhslot2 ||
1503e32ea7e7SCraig Gallek 		    rcu_access_pointer(sk->sk_reuseport_cb)) {
1504719f8358SEric Dumazet 			hslot = udp_hashslot(udptable, sock_net(sk),
1505719f8358SEric Dumazet 					     udp_sk(sk)->udp_port_hash);
1506719f8358SEric Dumazet 			/* we must lock primary chain too */
1507719f8358SEric Dumazet 			spin_lock_bh(&hslot->lock);
1508e32ea7e7SCraig Gallek 			if (rcu_access_pointer(sk->sk_reuseport_cb))
1509e32ea7e7SCraig Gallek 				reuseport_detach_sock(sk);
1510719f8358SEric Dumazet 
1511e32ea7e7SCraig Gallek 			if (hslot2 != nhslot2) {
1512719f8358SEric Dumazet 				spin_lock(&hslot2->lock);
1513719f8358SEric Dumazet 				hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1514719f8358SEric Dumazet 				hslot2->count--;
1515719f8358SEric Dumazet 				spin_unlock(&hslot2->lock);
1516719f8358SEric Dumazet 
1517719f8358SEric Dumazet 				spin_lock(&nhslot2->lock);
1518719f8358SEric Dumazet 				hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
1519719f8358SEric Dumazet 							 &nhslot2->head);
1520719f8358SEric Dumazet 				nhslot2->count++;
1521719f8358SEric Dumazet 				spin_unlock(&nhslot2->lock);
1522e32ea7e7SCraig Gallek 			}
1523719f8358SEric Dumazet 
1524719f8358SEric Dumazet 			spin_unlock_bh(&hslot->lock);
1525719f8358SEric Dumazet 		}
1526719f8358SEric Dumazet 	}
1527719f8358SEric Dumazet }
1528719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash);
1529719f8358SEric Dumazet 
1530719f8358SEric Dumazet static void udp_v4_rehash(struct sock *sk)
1531719f8358SEric Dumazet {
1532719f8358SEric Dumazet 	u16 new_hash = udp4_portaddr_hash(sock_net(sk),
1533719f8358SEric Dumazet 					  inet_sk(sk)->inet_rcv_saddr,
1534719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
1535719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
1536719f8358SEric Dumazet }
1537719f8358SEric Dumazet 
153893821778SHerbert Xu static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
153993821778SHerbert Xu {
1540fec5e652STom Herbert 	int rc;
154193821778SHerbert Xu 
1542005ec974SShawn Bohrer 	if (inet_sk(sk)->inet_daddr) {
1543bdeab991STom Herbert 		sock_rps_save_rxhash(sk, skb);
1544005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
15452c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
1546005ec974SShawn Bohrer 	}
1547fec5e652STom Herbert 
1548d826eb14SEric Dumazet 	rc = sock_queue_rcv_skb(sk, skb);
1549766e9037SEric Dumazet 	if (rc < 0) {
1550766e9037SEric Dumazet 		int is_udplite = IS_UDPLITE(sk);
1551766e9037SEric Dumazet 
155293821778SHerbert Xu 		/* Note that an ENOMEM error is charged twice */
1553766e9037SEric Dumazet 		if (rc == -ENOMEM)
155493821778SHerbert Xu 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
155593821778SHerbert Xu 					 is_udplite);
1556766e9037SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1557766e9037SEric Dumazet 		kfree_skb(skb);
1558296f7ea7SSatoru Moriya 		trace_udp_fail_queue_rcv_skb(rc, sk);
1559766e9037SEric Dumazet 		return -1;
156093821778SHerbert Xu 	}
156193821778SHerbert Xu 
156293821778SHerbert Xu 	return 0;
156393821778SHerbert Xu 
156493821778SHerbert Xu }
156593821778SHerbert Xu 
1566447167bfSEric Dumazet static struct static_key udp_encap_needed __read_mostly;
1567447167bfSEric Dumazet void udp_encap_enable(void)
1568447167bfSEric Dumazet {
1569447167bfSEric Dumazet 	if (!static_key_enabled(&udp_encap_needed))
1570447167bfSEric Dumazet 		static_key_slow_inc(&udp_encap_needed);
1571447167bfSEric Dumazet }
1572447167bfSEric Dumazet EXPORT_SYMBOL(udp_encap_enable);
1573447167bfSEric Dumazet 
1574db8dac20SDavid S. Miller /* returns:
1575db8dac20SDavid S. Miller  *  -1: error
1576db8dac20SDavid S. Miller  *   0: success
1577db8dac20SDavid S. Miller  *  >0: "udp encap" protocol resubmission
1578db8dac20SDavid S. Miller  *
1579db8dac20SDavid S. Miller  * Note that in the success and error cases, the skb is assumed to
1580db8dac20SDavid S. Miller  * have either been requeued or freed.
1581db8dac20SDavid S. Miller  */
1582db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1583db8dac20SDavid S. Miller {
1584db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1585db8dac20SDavid S. Miller 	int rc;
1586db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1587db8dac20SDavid S. Miller 
1588db8dac20SDavid S. Miller 	/*
1589db8dac20SDavid S. Miller 	 *	Charge it to the socket, dropping if the queue is full.
1590db8dac20SDavid S. Miller 	 */
1591db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1592db8dac20SDavid S. Miller 		goto drop;
1593db8dac20SDavid S. Miller 	nf_reset(skb);
1594db8dac20SDavid S. Miller 
1595447167bfSEric Dumazet 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
15960ad92ad0SEric Dumazet 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
15970ad92ad0SEric Dumazet 
1598db8dac20SDavid S. Miller 		/*
1599db8dac20SDavid S. Miller 		 * This is an encapsulation socket so pass the skb to
1600db8dac20SDavid S. Miller 		 * the socket's udp_encap_rcv() hook. Otherwise, just
1601db8dac20SDavid S. Miller 		 * fall through and pass this up the UDP socket.
1602db8dac20SDavid S. Miller 		 * up->encap_rcv() returns the following value:
1603db8dac20SDavid S. Miller 		 * =0 if skb was successfully passed to the encap
1604db8dac20SDavid S. Miller 		 *    handler or was discarded by it.
1605db8dac20SDavid S. Miller 		 * >0 if skb should be passed on to UDP.
1606db8dac20SDavid S. Miller 		 * <0 if skb should be resubmitted as proto -N
1607db8dac20SDavid S. Miller 		 */
1608db8dac20SDavid S. Miller 
1609db8dac20SDavid S. Miller 		/* if we're overly short, let UDP handle it */
16100ad92ad0SEric Dumazet 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
161100db4124SIan Morris 		if (skb->len > sizeof(struct udphdr) && encap_rcv) {
1612db8dac20SDavid S. Miller 			int ret;
1613db8dac20SDavid S. Miller 
16140a80966bSTom Herbert 			/* Verify checksum before giving to encap */
16150a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
16160a80966bSTom Herbert 				goto csum_error;
16170a80966bSTom Herbert 
16180ad92ad0SEric Dumazet 			ret = encap_rcv(sk, skb);
1619db8dac20SDavid S. Miller 			if (ret <= 0) {
16200283328eSPavel Emelyanov 				UDP_INC_STATS_BH(sock_net(sk),
16210283328eSPavel Emelyanov 						 UDP_MIB_INDATAGRAMS,
1622db8dac20SDavid S. Miller 						 is_udplite);
1623db8dac20SDavid S. Miller 				return -ret;
1624db8dac20SDavid S. Miller 			}
1625db8dac20SDavid S. Miller 		}
1626db8dac20SDavid S. Miller 
1627db8dac20SDavid S. Miller 		/* FALLTHROUGH -- it's a UDP Packet */
1628db8dac20SDavid S. Miller 	}
1629db8dac20SDavid S. Miller 
1630db8dac20SDavid S. Miller 	/*
1631db8dac20SDavid S. Miller 	 * 	UDP-Lite specific tests, ignored on UDP sockets
1632db8dac20SDavid S. Miller 	 */
1633db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
1634db8dac20SDavid S. Miller 
1635db8dac20SDavid S. Miller 		/*
1636db8dac20SDavid S. Miller 		 * MIB statistics other than incrementing the error count are
1637db8dac20SDavid S. Miller 		 * disabled for the following two types of errors: these depend
1638db8dac20SDavid S. Miller 		 * on the application settings, not on the functioning of the
1639db8dac20SDavid S. Miller 		 * protocol stack as such.
1640db8dac20SDavid S. Miller 		 *
1641db8dac20SDavid S. Miller 		 * RFC 3828 here recommends (sec 3.3): "There should also be a
1642db8dac20SDavid S. Miller 		 * way ... to ... at least let the receiving application block
1643db8dac20SDavid S. Miller 		 * delivery of packets with coverage values less than a value
1644db8dac20SDavid S. Miller 		 * provided by the application."
1645db8dac20SDavid S. Miller 		 */
1646db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
1647ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n",
1648db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
1649db8dac20SDavid S. Miller 			goto drop;
1650db8dac20SDavid S. Miller 		}
1651db8dac20SDavid S. Miller 		/* The next case involves violating the min. coverage requested
1652db8dac20SDavid S. Miller 		 * by the receiver. This is subtle: if receiver wants x and x is
1653db8dac20SDavid S. Miller 		 * greater than the buffersize/MTU then receiver will complain
1654db8dac20SDavid S. Miller 		 * that it wants x while sender emits packets of smaller size y.
1655db8dac20SDavid S. Miller 		 * Therefore the above ...()->partial_cov statement is essential.
1656db8dac20SDavid S. Miller 		 */
1657db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
1658ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n",
1659db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
1660db8dac20SDavid S. Miller 			goto drop;
1661db8dac20SDavid S. Miller 		}
1662db8dac20SDavid S. Miller 	}
1663db8dac20SDavid S. Miller 
166433d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
166533d480ceSEric Dumazet 	    udp_lib_checksum_complete(skb))
16666a5dc9e5SEric Dumazet 		goto csum_error;
1667db8dac20SDavid S. Miller 
1668274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
16693e215c8dSJames M Leddy 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
16703e215c8dSJames M Leddy 				 is_udplite);
1671c377411fSEric Dumazet 		goto drop;
16723e215c8dSJames M Leddy 	}
1673c377411fSEric Dumazet 
167493821778SHerbert Xu 	rc = 0;
1675db8dac20SDavid S. Miller 
1676fbf8866dSShawn Bohrer 	ipv4_pktinfo_prepare(sk, skb);
167793821778SHerbert Xu 	bh_lock_sock(sk);
167893821778SHerbert Xu 	if (!sock_owned_by_user(sk))
167993821778SHerbert Xu 		rc = __udp_queue_rcv_skb(sk, skb);
1680f545a38fSEric Dumazet 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
168155349790SZhu Yi 		bh_unlock_sock(sk);
168255349790SZhu Yi 		goto drop;
168355349790SZhu Yi 	}
168493821778SHerbert Xu 	bh_unlock_sock(sk);
168593821778SHerbert Xu 
168693821778SHerbert Xu 	return rc;
1687db8dac20SDavid S. Miller 
16886a5dc9e5SEric Dumazet csum_error:
16896a5dc9e5SEric Dumazet 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1690db8dac20SDavid S. Miller drop:
16910283328eSPavel Emelyanov 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
16928edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
1693db8dac20SDavid S. Miller 	kfree_skb(skb);
1694db8dac20SDavid S. Miller 	return -1;
1695db8dac20SDavid S. Miller }
1696db8dac20SDavid S. Miller 
16971240d137SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
16981240d137SEric Dumazet 			struct sk_buff *skb, unsigned int final)
16991240d137SEric Dumazet {
17001240d137SEric Dumazet 	unsigned int i;
17011240d137SEric Dumazet 	struct sk_buff *skb1 = NULL;
1702f6b8f32cSEric Dumazet 	struct sock *sk;
17031240d137SEric Dumazet 
17041240d137SEric Dumazet 	for (i = 0; i < count; i++) {
1705f6b8f32cSEric Dumazet 		sk = stack[i];
170651456b29SIan Morris 		if (likely(!skb1))
17071240d137SEric Dumazet 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
17081240d137SEric Dumazet 
1709f6b8f32cSEric Dumazet 		if (!skb1) {
1710f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
1711f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
1712f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1713f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
1714f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1715f6b8f32cSEric Dumazet 		}
1716f6b8f32cSEric Dumazet 
1717f6b8f32cSEric Dumazet 		if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0)
17181240d137SEric Dumazet 			skb1 = NULL;
17192dc41cffSDavid Held 
17202dc41cffSDavid Held 		sock_put(sk);
17211240d137SEric Dumazet 	}
17221240d137SEric Dumazet 	if (unlikely(skb1))
17231240d137SEric Dumazet 		kfree_skb(skb1);
17241240d137SEric Dumazet }
17251240d137SEric Dumazet 
172697502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock
1727e47eb5dfSEric Dumazet  * For UDP, we use xchg() to guard against concurrent changes.
172897502231SEric Dumazet  */
172997502231SEric Dumazet static void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
1730421b3885SShawn Bohrer {
173197502231SEric Dumazet 	struct dst_entry *old;
1732421b3885SShawn Bohrer 
1733421b3885SShawn Bohrer 	dst_hold(dst);
1734e47eb5dfSEric Dumazet 	old = xchg(&sk->sk_rx_dst, dst);
173597502231SEric Dumazet 	dst_release(old);
173697502231SEric Dumazet }
1737421b3885SShawn Bohrer 
1738db8dac20SDavid S. Miller /*
1739db8dac20SDavid S. Miller  *	Multicasts and broadcasts go to each listener.
1740db8dac20SDavid S. Miller  *
17411240d137SEric Dumazet  *	Note: called only from the BH handler context.
1742db8dac20SDavid S. Miller  */
1743e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
1744db8dac20SDavid S. Miller 				    struct udphdr  *uh,
1745db8dac20SDavid S. Miller 				    __be32 saddr, __be32 daddr,
174636cbb245SRick Jones 				    struct udp_table *udptable,
174736cbb245SRick Jones 				    int proto)
1748db8dac20SDavid S. Miller {
17491240d137SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
17505cf3d461SDavid Held 	struct hlist_nulls_node *node;
17515cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
17525cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
17535cf3d461SDavid Held 	int dif = skb->dev->ifindex;
17542dc41cffSDavid Held 	unsigned int count = 0, offset = offsetof(typeof(*sk), sk_nulls_node);
17552dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
175636cbb245SRick Jones 	bool inner_flushed = false;
17572dc41cffSDavid Held 
17582dc41cffSDavid Held 	if (use_hash2) {
17592dc41cffSDavid Held 		hash2_any = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum) &
17602dc41cffSDavid Held 			    udp_table.mask;
17612dc41cffSDavid Held 		hash2 = udp4_portaddr_hash(net, daddr, hnum) & udp_table.mask;
17622dc41cffSDavid Held start_lookup:
17632dc41cffSDavid Held 		hslot = &udp_table.hash2[hash2];
17642dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
17652dc41cffSDavid Held 	}
1766db8dac20SDavid S. Miller 
1767645ca708SEric Dumazet 	spin_lock(&hslot->lock);
17682dc41cffSDavid Held 	sk_nulls_for_each_entry_offset(sk, node, &hslot->head, offset) {
17695cf3d461SDavid Held 		if (__udp_is_mcast_sock(net, sk,
17705cf3d461SDavid Held 					uh->dest, daddr,
17715cf3d461SDavid Held 					uh->source, saddr,
17725cf3d461SDavid Held 					dif, hnum)) {
17731240d137SEric Dumazet 			if (unlikely(count == ARRAY_SIZE(stack))) {
17741240d137SEric Dumazet 				flush_stack(stack, count, skb, ~0);
177536cbb245SRick Jones 				inner_flushed = true;
17761240d137SEric Dumazet 				count = 0;
1777db8dac20SDavid S. Miller 			}
17785cf3d461SDavid Held 			stack[count++] = sk;
17792dc41cffSDavid Held 			sock_hold(sk);
17805cf3d461SDavid Held 		}
17811240d137SEric Dumazet 	}
17821240d137SEric Dumazet 
1783645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
17841240d137SEric Dumazet 
17852dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
17862dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
17872dc41cffSDavid Held 		hash2 = hash2_any;
17882dc41cffSDavid Held 		goto start_lookup;
17892dc41cffSDavid Held 	}
17902dc41cffSDavid Held 
17911240d137SEric Dumazet 	/*
17921240d137SEric Dumazet 	 * do the slow work with no lock held
17931240d137SEric Dumazet 	 */
17941240d137SEric Dumazet 	if (count) {
17951240d137SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
17961240d137SEric Dumazet 	} else {
179736cbb245SRick Jones 		if (!inner_flushed)
179836cbb245SRick Jones 			UDP_INC_STATS_BH(net, UDP_MIB_IGNOREDMULTI,
179936cbb245SRick Jones 					 proto == IPPROTO_UDPLITE);
180036cbb245SRick Jones 		consume_skb(skb);
18011240d137SEric Dumazet 	}
1802db8dac20SDavid S. Miller 	return 0;
1803db8dac20SDavid S. Miller }
1804db8dac20SDavid S. Miller 
1805db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success),
1806db8dac20SDavid S. Miller  * CHECKSUM_UNNECESSARY means, that no more checks are required.
1807db8dac20SDavid S. Miller  * Otherwise, csum completion requires chacksumming packet body,
1808db8dac20SDavid S. Miller  * including udp header and folding it to skb->csum.
1809db8dac20SDavid S. Miller  */
1810db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1811db8dac20SDavid S. Miller 				 int proto)
1812db8dac20SDavid S. Miller {
1813db8dac20SDavid S. Miller 	int err;
1814db8dac20SDavid S. Miller 
1815db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
1816db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
1817db8dac20SDavid S. Miller 
1818db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
1819db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
1820db8dac20SDavid S. Miller 		if (err)
1821db8dac20SDavid S. Miller 			return err;
1822db8dac20SDavid S. Miller 	}
1823db8dac20SDavid S. Miller 
1824ed70fcfcSTom Herbert 	return skb_checksum_init_zero_check(skb, proto, uh->check,
1825ed70fcfcSTom Herbert 					    inet_compute_pseudo);
1826db8dac20SDavid S. Miller }
1827db8dac20SDavid S. Miller 
1828db8dac20SDavid S. Miller /*
1829db8dac20SDavid S. Miller  *	All we need to do is get the socket, and then do a checksum.
1830db8dac20SDavid S. Miller  */
1831db8dac20SDavid S. Miller 
1832645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1833db8dac20SDavid S. Miller 		   int proto)
1834db8dac20SDavid S. Miller {
1835db8dac20SDavid S. Miller 	struct sock *sk;
18367b5e56f9SJesper Dangaard Brouer 	struct udphdr *uh;
1837db8dac20SDavid S. Miller 	unsigned short ulen;
1838adf30907SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
18392783ef23SJesper Dangaard Brouer 	__be32 saddr, daddr;
18400283328eSPavel Emelyanov 	struct net *net = dev_net(skb->dev);
1841db8dac20SDavid S. Miller 
1842db8dac20SDavid S. Miller 	/*
1843db8dac20SDavid S. Miller 	 *  Validate the packet.
1844db8dac20SDavid S. Miller 	 */
1845db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1846db8dac20SDavid S. Miller 		goto drop;		/* No space for header. */
1847db8dac20SDavid S. Miller 
18487b5e56f9SJesper Dangaard Brouer 	uh   = udp_hdr(skb);
1849db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
1850ccc2d97cSBjørn Mork 	saddr = ip_hdr(skb)->saddr;
1851ccc2d97cSBjørn Mork 	daddr = ip_hdr(skb)->daddr;
1852ccc2d97cSBjørn Mork 
1853db8dac20SDavid S. Miller 	if (ulen > skb->len)
1854db8dac20SDavid S. Miller 		goto short_packet;
1855db8dac20SDavid S. Miller 
1856db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
1857db8dac20SDavid S. Miller 		/* UDP validates ulen. */
1858db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1859db8dac20SDavid S. Miller 			goto short_packet;
1860db8dac20SDavid S. Miller 		uh = udp_hdr(skb);
1861db8dac20SDavid S. Miller 	}
1862db8dac20SDavid S. Miller 
1863db8dac20SDavid S. Miller 	if (udp4_csum_init(skb, uh, proto))
1864db8dac20SDavid S. Miller 		goto csum_error;
1865db8dac20SDavid S. Miller 
18668afdd99aSEric Dumazet 	sk = skb_steal_sock(skb);
18678afdd99aSEric Dumazet 	if (sk) {
186897502231SEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
1869421b3885SShawn Bohrer 		int ret;
1870421b3885SShawn Bohrer 
187197502231SEric Dumazet 		if (unlikely(sk->sk_rx_dst != dst))
187297502231SEric Dumazet 			udp_sk_rx_dst_set(sk, dst);
1873421b3885SShawn Bohrer 
1874421b3885SShawn Bohrer 		ret = udp_queue_rcv_skb(sk, skb);
18758afdd99aSEric Dumazet 		sock_put(sk);
1876421b3885SShawn Bohrer 		/* a return value > 0 means to resubmit the input, but
1877421b3885SShawn Bohrer 		 * it wants the return to be -protocol, or 0
1878421b3885SShawn Bohrer 		 */
1879421b3885SShawn Bohrer 		if (ret > 0)
1880421b3885SShawn Bohrer 			return -ret;
1881421b3885SShawn Bohrer 		return 0;
1882c18450a5SFabian Frederick 	}
1883c18450a5SFabian Frederick 
1884db8dac20SDavid S. Miller 	if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1885e3163493SPavel Emelyanov 		return __udp4_lib_mcast_deliver(net, skb, uh,
188636cbb245SRick Jones 						saddr, daddr, udptable, proto);
1887db8dac20SDavid S. Miller 
1888607c4aafSKOVACS Krisztian 	sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
188900db4124SIan Morris 	if (sk) {
1890a5b50476SEliezer Tamir 		int ret;
1891a5b50476SEliezer Tamir 
1892224d019cSTom Herbert 		if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
18932abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
18942abb7cdcSTom Herbert 						 inet_compute_pseudo);
18952abb7cdcSTom Herbert 
1896a5b50476SEliezer Tamir 		ret = udp_queue_rcv_skb(sk, skb);
1897db8dac20SDavid S. Miller 		sock_put(sk);
1898db8dac20SDavid S. Miller 
1899db8dac20SDavid S. Miller 		/* a return value > 0 means to resubmit the input, but
1900db8dac20SDavid S. Miller 		 * it wants the return to be -protocol, or 0
1901db8dac20SDavid S. Miller 		 */
1902db8dac20SDavid S. Miller 		if (ret > 0)
1903db8dac20SDavid S. Miller 			return -ret;
1904db8dac20SDavid S. Miller 		return 0;
1905db8dac20SDavid S. Miller 	}
1906db8dac20SDavid S. Miller 
1907db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1908db8dac20SDavid S. Miller 		goto drop;
1909db8dac20SDavid S. Miller 	nf_reset(skb);
1910db8dac20SDavid S. Miller 
1911db8dac20SDavid S. Miller 	/* No socket. Drop packet silently, if checksum is wrong */
1912db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
1913db8dac20SDavid S. Miller 		goto csum_error;
1914db8dac20SDavid S. Miller 
19150283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
1916db8dac20SDavid S. Miller 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1917db8dac20SDavid S. Miller 
1918db8dac20SDavid S. Miller 	/*
1919db8dac20SDavid S. Miller 	 * Hmm.  We got an UDP packet to a port to which we
1920db8dac20SDavid S. Miller 	 * don't wanna listen.  Ignore it.
1921db8dac20SDavid S. Miller 	 */
1922db8dac20SDavid S. Miller 	kfree_skb(skb);
1923db8dac20SDavid S. Miller 	return 0;
1924db8dac20SDavid S. Miller 
1925db8dac20SDavid S. Miller short_packet:
1926ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
1927afd46503SJoe Perches 			    proto == IPPROTO_UDPLITE ? "Lite" : "",
1928afd46503SJoe Perches 			    &saddr, ntohs(uh->source),
1929afd46503SJoe Perches 			    ulen, skb->len,
1930afd46503SJoe Perches 			    &daddr, ntohs(uh->dest));
1931db8dac20SDavid S. Miller 	goto drop;
1932db8dac20SDavid S. Miller 
1933db8dac20SDavid S. Miller csum_error:
1934db8dac20SDavid S. Miller 	/*
1935db8dac20SDavid S. Miller 	 * RFC1122: OK.  Discards the bad packet silently (as far as
1936db8dac20SDavid S. Miller 	 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1937db8dac20SDavid S. Miller 	 */
1938ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
1939afd46503SJoe Perches 			    proto == IPPROTO_UDPLITE ? "Lite" : "",
1940afd46503SJoe Perches 			    &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
1941db8dac20SDavid S. Miller 			    ulen);
19426a5dc9e5SEric Dumazet 	UDP_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1943db8dac20SDavid S. Miller drop:
19440283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1945db8dac20SDavid S. Miller 	kfree_skb(skb);
1946db8dac20SDavid S. Miller 	return 0;
1947db8dac20SDavid S. Miller }
1948db8dac20SDavid S. Miller 
1949421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket.
1950421b3885SShawn Bohrer  * If more than one socket found returns NULL
1951421b3885SShawn Bohrer  */
1952421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
1953421b3885SShawn Bohrer 						  __be16 loc_port, __be32 loc_addr,
1954421b3885SShawn Bohrer 						  __be16 rmt_port, __be32 rmt_addr,
1955421b3885SShawn Bohrer 						  int dif)
1956421b3885SShawn Bohrer {
1957421b3885SShawn Bohrer 	struct sock *sk, *result;
1958421b3885SShawn Bohrer 	struct hlist_nulls_node *node;
1959421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
1960421b3885SShawn Bohrer 	unsigned int count, slot = udp_hashfn(net, hnum, udp_table.mask);
1961421b3885SShawn Bohrer 	struct udp_hslot *hslot = &udp_table.hash[slot];
1962421b3885SShawn Bohrer 
196363c6f81cSEric Dumazet 	/* Do not bother scanning a too big list */
196463c6f81cSEric Dumazet 	if (hslot->count > 10)
196563c6f81cSEric Dumazet 		return NULL;
196663c6f81cSEric Dumazet 
1967421b3885SShawn Bohrer 	rcu_read_lock();
1968421b3885SShawn Bohrer begin:
1969421b3885SShawn Bohrer 	count = 0;
1970421b3885SShawn Bohrer 	result = NULL;
1971421b3885SShawn Bohrer 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
1972421b3885SShawn Bohrer 		if (__udp_is_mcast_sock(net, sk,
1973421b3885SShawn Bohrer 					loc_port, loc_addr,
1974421b3885SShawn Bohrer 					rmt_port, rmt_addr,
1975421b3885SShawn Bohrer 					dif, hnum)) {
1976421b3885SShawn Bohrer 			result = sk;
1977421b3885SShawn Bohrer 			++count;
1978421b3885SShawn Bohrer 		}
1979421b3885SShawn Bohrer 	}
1980421b3885SShawn Bohrer 	/*
1981421b3885SShawn Bohrer 	 * if the nulls value we got at the end of this lookup is
1982421b3885SShawn Bohrer 	 * not the expected one, we must restart lookup.
1983421b3885SShawn Bohrer 	 * We probably met an item that was moved to another chain.
1984421b3885SShawn Bohrer 	 */
1985421b3885SShawn Bohrer 	if (get_nulls_value(node) != slot)
1986421b3885SShawn Bohrer 		goto begin;
1987421b3885SShawn Bohrer 
1988421b3885SShawn Bohrer 	if (result) {
1989421b3885SShawn Bohrer 		if (count != 1 ||
1990421b3885SShawn Bohrer 		    unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
1991421b3885SShawn Bohrer 			result = NULL;
1992f69b923aSEric Dumazet 		else if (unlikely(!__udp_is_mcast_sock(net, result,
1993421b3885SShawn Bohrer 						       loc_port, loc_addr,
1994421b3885SShawn Bohrer 						       rmt_port, rmt_addr,
1995421b3885SShawn Bohrer 						       dif, hnum))) {
1996421b3885SShawn Bohrer 			sock_put(result);
1997421b3885SShawn Bohrer 			result = NULL;
1998421b3885SShawn Bohrer 		}
1999421b3885SShawn Bohrer 	}
2000421b3885SShawn Bohrer 	rcu_read_unlock();
2001421b3885SShawn Bohrer 	return result;
2002421b3885SShawn Bohrer }
2003421b3885SShawn Bohrer 
2004421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can
2005421b3885SShawn Bohrer  * break forwarding setups.  The chains here can be long so only check
2006421b3885SShawn Bohrer  * if the first socket is an exact match and if not move on.
2007421b3885SShawn Bohrer  */
2008421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net,
2009421b3885SShawn Bohrer 					    __be16 loc_port, __be32 loc_addr,
2010421b3885SShawn Bohrer 					    __be16 rmt_port, __be32 rmt_addr,
2011421b3885SShawn Bohrer 					    int dif)
2012421b3885SShawn Bohrer {
2013421b3885SShawn Bohrer 	struct sock *sk, *result;
2014421b3885SShawn Bohrer 	struct hlist_nulls_node *node;
2015421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
2016421b3885SShawn Bohrer 	unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum);
2017421b3885SShawn Bohrer 	unsigned int slot2 = hash2 & udp_table.mask;
2018421b3885SShawn Bohrer 	struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
2019c7228317SJoe Perches 	INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
2020421b3885SShawn Bohrer 	const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
2021421b3885SShawn Bohrer 
2022421b3885SShawn Bohrer 	rcu_read_lock();
2023421b3885SShawn Bohrer 	result = NULL;
2024421b3885SShawn Bohrer 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
2025421b3885SShawn Bohrer 		if (INET_MATCH(sk, net, acookie,
2026421b3885SShawn Bohrer 			       rmt_addr, loc_addr, ports, dif))
2027421b3885SShawn Bohrer 			result = sk;
2028421b3885SShawn Bohrer 		/* Only check first socket in chain */
2029421b3885SShawn Bohrer 		break;
2030421b3885SShawn Bohrer 	}
2031421b3885SShawn Bohrer 
2032421b3885SShawn Bohrer 	if (result) {
2033421b3885SShawn Bohrer 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
2034421b3885SShawn Bohrer 			result = NULL;
2035421b3885SShawn Bohrer 		else if (unlikely(!INET_MATCH(sk, net, acookie,
2036421b3885SShawn Bohrer 					      rmt_addr, loc_addr,
2037421b3885SShawn Bohrer 					      ports, dif))) {
2038421b3885SShawn Bohrer 			sock_put(result);
2039421b3885SShawn Bohrer 			result = NULL;
2040421b3885SShawn Bohrer 		}
2041421b3885SShawn Bohrer 	}
2042421b3885SShawn Bohrer 	rcu_read_unlock();
2043421b3885SShawn Bohrer 	return result;
2044421b3885SShawn Bohrer }
2045421b3885SShawn Bohrer 
2046421b3885SShawn Bohrer void udp_v4_early_demux(struct sk_buff *skb)
2047421b3885SShawn Bohrer {
2048610438b7SEric Dumazet 	struct net *net = dev_net(skb->dev);
2049610438b7SEric Dumazet 	const struct iphdr *iph;
2050610438b7SEric Dumazet 	const struct udphdr *uh;
2051421b3885SShawn Bohrer 	struct sock *sk;
2052421b3885SShawn Bohrer 	struct dst_entry *dst;
2053421b3885SShawn Bohrer 	int dif = skb->dev->ifindex;
20546e540309SShawn Bohrer 	int ours;
2055421b3885SShawn Bohrer 
2056421b3885SShawn Bohrer 	/* validate the packet */
2057421b3885SShawn Bohrer 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
2058421b3885SShawn Bohrer 		return;
2059421b3885SShawn Bohrer 
2060610438b7SEric Dumazet 	iph = ip_hdr(skb);
2061610438b7SEric Dumazet 	uh = udp_hdr(skb);
2062610438b7SEric Dumazet 
2063421b3885SShawn Bohrer 	if (skb->pkt_type == PACKET_BROADCAST ||
20646e540309SShawn Bohrer 	    skb->pkt_type == PACKET_MULTICAST) {
20656e540309SShawn Bohrer 		struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
20666e540309SShawn Bohrer 
20676e540309SShawn Bohrer 		if (!in_dev)
20686e540309SShawn Bohrer 			return;
20696e540309SShawn Bohrer 
20706e540309SShawn Bohrer 		ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr,
20716e540309SShawn Bohrer 				       iph->protocol);
20726e540309SShawn Bohrer 		if (!ours)
20736e540309SShawn Bohrer 			return;
2074421b3885SShawn Bohrer 		sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
2075421b3885SShawn Bohrer 						   uh->source, iph->saddr, dif);
20766e540309SShawn Bohrer 	} else if (skb->pkt_type == PACKET_HOST) {
2077421b3885SShawn Bohrer 		sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
2078421b3885SShawn Bohrer 					     uh->source, iph->saddr, dif);
20796e540309SShawn Bohrer 	} else {
2080421b3885SShawn Bohrer 		return;
20816e540309SShawn Bohrer 	}
2082421b3885SShawn Bohrer 
2083421b3885SShawn Bohrer 	if (!sk)
2084421b3885SShawn Bohrer 		return;
2085421b3885SShawn Bohrer 
2086421b3885SShawn Bohrer 	skb->sk = sk;
208782eabd9eSAlexander Duyck 	skb->destructor = sock_efree;
208810e2eb87SEric Dumazet 	dst = READ_ONCE(sk->sk_rx_dst);
2089421b3885SShawn Bohrer 
2090421b3885SShawn Bohrer 	if (dst)
2091421b3885SShawn Bohrer 		dst = dst_check(dst, 0);
209210e2eb87SEric Dumazet 	if (dst) {
209310e2eb87SEric Dumazet 		/* DST_NOCACHE can not be used without taking a reference */
209410e2eb87SEric Dumazet 		if (dst->flags & DST_NOCACHE) {
209510e2eb87SEric Dumazet 			if (likely(atomic_inc_not_zero(&dst->__refcnt)))
209610e2eb87SEric Dumazet 				skb_dst_set(skb, dst);
209710e2eb87SEric Dumazet 		} else {
2098421b3885SShawn Bohrer 			skb_dst_set_noref(skb, dst);
2099421b3885SShawn Bohrer 		}
210010e2eb87SEric Dumazet 	}
210110e2eb87SEric Dumazet }
2102421b3885SShawn Bohrer 
2103db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb)
2104db8dac20SDavid S. Miller {
2105645ca708SEric Dumazet 	return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
2106db8dac20SDavid S. Miller }
2107db8dac20SDavid S. Miller 
21087d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk)
2109db8dac20SDavid S. Miller {
211044046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
21118a74ad60SEric Dumazet 	bool slow = lock_sock_fast(sk);
2112db8dac20SDavid S. Miller 	udp_flush_pending_frames(sk);
21138a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
211444046a59STom Parkin 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
211544046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
211644046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
211744046a59STom Parkin 		if (encap_destroy)
211844046a59STom Parkin 			encap_destroy(sk);
211944046a59STom Parkin 	}
2120db8dac20SDavid S. Miller }
2121db8dac20SDavid S. Miller 
21221da177e4SLinus Torvalds /*
21231da177e4SLinus Torvalds  *	Socket option code for UDP
21241da177e4SLinus Torvalds  */
21254c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname,
2126b7058842SDavid S. Miller 		       char __user *optval, unsigned int optlen,
21274c0a6cb0SGerrit Renker 		       int (*push_pending_frames)(struct sock *))
21281da177e4SLinus Torvalds {
21291da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
21301c19448cSTom Herbert 	int val, valbool;
21311da177e4SLinus Torvalds 	int err = 0;
2132b2bf1e26SWang Chen 	int is_udplite = IS_UDPLITE(sk);
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	if (optlen < sizeof(int))
21351da177e4SLinus Torvalds 		return -EINVAL;
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
21381da177e4SLinus Torvalds 		return -EFAULT;
21391da177e4SLinus Torvalds 
21401c19448cSTom Herbert 	valbool = val ? 1 : 0;
21411c19448cSTom Herbert 
21421da177e4SLinus Torvalds 	switch (optname) {
21431da177e4SLinus Torvalds 	case UDP_CORK:
21441da177e4SLinus Torvalds 		if (val != 0) {
21451da177e4SLinus Torvalds 			up->corkflag = 1;
21461da177e4SLinus Torvalds 		} else {
21471da177e4SLinus Torvalds 			up->corkflag = 0;
21481da177e4SLinus Torvalds 			lock_sock(sk);
21494243cdc2SJoe Perches 			push_pending_frames(sk);
21501da177e4SLinus Torvalds 			release_sock(sk);
21511da177e4SLinus Torvalds 		}
21521da177e4SLinus Torvalds 		break;
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	case UDP_ENCAP:
21551da177e4SLinus Torvalds 		switch (val) {
21561da177e4SLinus Torvalds 		case 0:
21571da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP:
21581da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP_NON_IKE:
2159067b207bSJames Chapman 			up->encap_rcv = xfrm4_udp_encap_rcv;
2160067b207bSJames Chapman 			/* FALLTHROUGH */
2161342f0234SJames Chapman 		case UDP_ENCAP_L2TPINUDP:
21621da177e4SLinus Torvalds 			up->encap_type = val;
2163447167bfSEric Dumazet 			udp_encap_enable();
21641da177e4SLinus Torvalds 			break;
21651da177e4SLinus Torvalds 		default:
21661da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
21671da177e4SLinus Torvalds 			break;
21681da177e4SLinus Torvalds 		}
21691da177e4SLinus Torvalds 		break;
21701da177e4SLinus Torvalds 
21711c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
21721c19448cSTom Herbert 		up->no_check6_tx = valbool;
21731c19448cSTom Herbert 		break;
21741c19448cSTom Herbert 
21751c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
21761c19448cSTom Herbert 		up->no_check6_rx = valbool;
21771c19448cSTom Herbert 		break;
21781c19448cSTom Herbert 
2179ba4e58ecSGerrit Renker 	/*
2180ba4e58ecSGerrit Renker 	 * 	UDP-Lite's partial checksum coverage (RFC 3828).
2181ba4e58ecSGerrit Renker 	 */
2182ba4e58ecSGerrit Renker 	/* The sender sets actual checksum coverage length via this option.
2183ba4e58ecSGerrit Renker 	 * The case coverage > packet length is handled by send module. */
2184ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2185b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2186ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2187ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
2188ba4e58ecSGerrit Renker 			val = 8;
21894be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
21904be929beSAlexey Dobriyan 			val = USHRT_MAX;
2191ba4e58ecSGerrit Renker 		up->pcslen = val;
2192ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_SEND_CC;
2193ba4e58ecSGerrit Renker 		break;
2194ba4e58ecSGerrit Renker 
2195ba4e58ecSGerrit Renker 	/* The receiver specifies a minimum checksum coverage value. To make
2196ba4e58ecSGerrit Renker 	 * sense, this should be set to at least 8 (as done below). If zero is
2197ba4e58ecSGerrit Renker 	 * used, this again means full checksum coverage.                     */
2198ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2199b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2200ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2201ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Avoid silly minimal values.       */
2202ba4e58ecSGerrit Renker 			val = 8;
22034be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
22044be929beSAlexey Dobriyan 			val = USHRT_MAX;
2205ba4e58ecSGerrit Renker 		up->pcrlen = val;
2206ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_RECV_CC;
2207ba4e58ecSGerrit Renker 		break;
2208ba4e58ecSGerrit Renker 
22091da177e4SLinus Torvalds 	default:
22101da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
22111da177e4SLinus Torvalds 		break;
22126516c655SStephen Hemminger 	}
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 	return err;
22151da177e4SLinus Torvalds }
2216c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt);
22171da177e4SLinus Torvalds 
2218db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname,
2219b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
2220db8dac20SDavid S. Miller {
2221db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2222db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2223db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2224db8dac20SDavid S. Miller 	return ip_setsockopt(sk, level, optname, optval, optlen);
2225db8dac20SDavid S. Miller }
2226db8dac20SDavid S. Miller 
2227db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2228db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname,
2229b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
2230db8dac20SDavid S. Miller {
2231db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2232db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2233db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2234db8dac20SDavid S. Miller 	return compat_ip_setsockopt(sk, level, optname, optval, optlen);
2235db8dac20SDavid S. Miller }
2236db8dac20SDavid S. Miller #endif
2237db8dac20SDavid S. Miller 
22384c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname,
22391da177e4SLinus Torvalds 		       char __user *optval, int __user *optlen)
22401da177e4SLinus Torvalds {
22411da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
22421da177e4SLinus Torvalds 	int val, len;
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds 	if (get_user(len, optlen))
22451da177e4SLinus Torvalds 		return -EFAULT;
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds 	if (len < 0)
22501da177e4SLinus Torvalds 		return -EINVAL;
22511da177e4SLinus Torvalds 
22521da177e4SLinus Torvalds 	switch (optname) {
22531da177e4SLinus Torvalds 	case UDP_CORK:
22541da177e4SLinus Torvalds 		val = up->corkflag;
22551da177e4SLinus Torvalds 		break;
22561da177e4SLinus Torvalds 
22571da177e4SLinus Torvalds 	case UDP_ENCAP:
22581da177e4SLinus Torvalds 		val = up->encap_type;
22591da177e4SLinus Torvalds 		break;
22601da177e4SLinus Torvalds 
22611c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
22621c19448cSTom Herbert 		val = up->no_check6_tx;
22631c19448cSTom Herbert 		break;
22641c19448cSTom Herbert 
22651c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
22661c19448cSTom Herbert 		val = up->no_check6_rx;
22671c19448cSTom Herbert 		break;
22681c19448cSTom Herbert 
2269ba4e58ecSGerrit Renker 	/* The following two cannot be changed on UDP sockets, the return is
2270ba4e58ecSGerrit Renker 	 * always 0 (which corresponds to the full checksum coverage of UDP). */
2271ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2272ba4e58ecSGerrit Renker 		val = up->pcslen;
2273ba4e58ecSGerrit Renker 		break;
2274ba4e58ecSGerrit Renker 
2275ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2276ba4e58ecSGerrit Renker 		val = up->pcrlen;
2277ba4e58ecSGerrit Renker 		break;
2278ba4e58ecSGerrit Renker 
22791da177e4SLinus Torvalds 	default:
22801da177e4SLinus Torvalds 		return -ENOPROTOOPT;
22816516c655SStephen Hemminger 	}
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 	if (put_user(len, optlen))
22841da177e4SLinus Torvalds 		return -EFAULT;
22851da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
22861da177e4SLinus Torvalds 		return -EFAULT;
22871da177e4SLinus Torvalds 	return 0;
22881da177e4SLinus Torvalds }
2289c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt);
22901da177e4SLinus Torvalds 
2291db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname,
2292db8dac20SDavid S. Miller 		   char __user *optval, int __user *optlen)
2293db8dac20SDavid S. Miller {
2294db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2295db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2296db8dac20SDavid S. Miller 	return ip_getsockopt(sk, level, optname, optval, optlen);
2297db8dac20SDavid S. Miller }
2298db8dac20SDavid S. Miller 
2299db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2300db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname,
2301db8dac20SDavid S. Miller 				 char __user *optval, int __user *optlen)
2302db8dac20SDavid S. Miller {
2303db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2304db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2305db8dac20SDavid S. Miller 	return compat_ip_getsockopt(sk, level, optname, optval, optlen);
2306db8dac20SDavid S. Miller }
2307db8dac20SDavid S. Miller #endif
23081da177e4SLinus Torvalds /**
23091da177e4SLinus Torvalds  * 	udp_poll - wait for a UDP event.
23101da177e4SLinus Torvalds  *	@file - file struct
23111da177e4SLinus Torvalds  *	@sock - socket
23121da177e4SLinus Torvalds  *	@wait - poll table
23131da177e4SLinus Torvalds  *
23141da177e4SLinus Torvalds  *	This is same as datagram poll, except for the special case of
23151da177e4SLinus Torvalds  *	blocking sockets. If application is using a blocking fd
23161da177e4SLinus Torvalds  *	and a packet with checksum error is in the queue;
23171da177e4SLinus Torvalds  *	then it could get return from select indicating data available
23181da177e4SLinus Torvalds  *	but then block when reading it. Add special case code
23191da177e4SLinus Torvalds  *	to work around these arguably broken applications.
23201da177e4SLinus Torvalds  */
23211da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
23221da177e4SLinus Torvalds {
23231da177e4SLinus Torvalds 	unsigned int mask = datagram_poll(file, sock, wait);
23241da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
23251da177e4SLinus Torvalds 
2326c3f1dbafSDavid Majnemer 	sock_rps_record_flow(sk);
2327c3f1dbafSDavid Majnemer 
23281da177e4SLinus Torvalds 	/* Check for false positives due to checksum errors */
232985584672SEric Dumazet 	if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
233085584672SEric Dumazet 	    !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
23311da177e4SLinus Torvalds 		mask &= ~(POLLIN | POLLRDNORM);
23321da177e4SLinus Torvalds 
23331da177e4SLinus Torvalds 	return mask;
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds }
2336c482c568SEric Dumazet EXPORT_SYMBOL(udp_poll);
23371da177e4SLinus Torvalds 
2338db8dac20SDavid S. Miller struct proto udp_prot = {
2339db8dac20SDavid S. Miller 	.name		   = "UDP",
2340db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
2341db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
2342db8dac20SDavid S. Miller 	.connect	   = ip4_datagram_connect,
2343db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
2344db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
2345db8dac20SDavid S. Miller 	.destroy	   = udp_destroy_sock,
2346db8dac20SDavid S. Miller 	.setsockopt	   = udp_setsockopt,
2347db8dac20SDavid S. Miller 	.getsockopt	   = udp_getsockopt,
2348db8dac20SDavid S. Miller 	.sendmsg	   = udp_sendmsg,
2349db8dac20SDavid S. Miller 	.recvmsg	   = udp_recvmsg,
2350db8dac20SDavid S. Miller 	.sendpage	   = udp_sendpage,
235193821778SHerbert Xu 	.backlog_rcv	   = __udp_queue_rcv_skb,
23528141ed9fSSteffen Klassert 	.release_cb	   = ip4_datagram_release_cb,
2353db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
2354db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
2355719f8358SEric Dumazet 	.rehash		   = udp_v4_rehash,
2356db8dac20SDavid S. Miller 	.get_port	   = udp_v4_get_port,
2357db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
2358db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
2359db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
2360db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
2361db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp_sock),
2362271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
2363645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
2364db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2365db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udp_setsockopt,
2366db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udp_getsockopt,
2367db8dac20SDavid S. Miller #endif
2368fcbdf09dSOctavian Purdila 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
2369db8dac20SDavid S. Miller };
2370c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot);
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
23731da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
23741da177e4SLinus Torvalds 
2375645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start)
23761da177e4SLinus Torvalds {
23771da177e4SLinus Torvalds 	struct sock *sk;
23781da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
23796f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
23801da177e4SLinus Torvalds 
2381f86dcc5aSEric Dumazet 	for (state->bucket = start; state->bucket <= state->udp_table->mask;
2382f86dcc5aSEric Dumazet 	     ++state->bucket) {
238388ab1932SEric Dumazet 		struct hlist_nulls_node *node;
2384645ca708SEric Dumazet 		struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
2385f86dcc5aSEric Dumazet 
2386f86dcc5aSEric Dumazet 		if (hlist_nulls_empty(&hslot->head))
2387f86dcc5aSEric Dumazet 			continue;
2388f86dcc5aSEric Dumazet 
2389645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
239088ab1932SEric Dumazet 		sk_nulls_for_each(sk, node, &hslot->head) {
2391878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
2392a91275efSDaniel Lezcano 				continue;
23931da177e4SLinus Torvalds 			if (sk->sk_family == state->family)
23941da177e4SLinus Torvalds 				goto found;
23951da177e4SLinus Torvalds 		}
2396645ca708SEric Dumazet 		spin_unlock_bh(&hslot->lock);
23971da177e4SLinus Torvalds 	}
23981da177e4SLinus Torvalds 	sk = NULL;
23991da177e4SLinus Torvalds found:
24001da177e4SLinus Torvalds 	return sk;
24011da177e4SLinus Torvalds }
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
24041da177e4SLinus Torvalds {
24051da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
24066f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	do {
240988ab1932SEric Dumazet 		sk = sk_nulls_next(sk);
2410878628fbSYOSHIFUJI Hideaki 	} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
24111da177e4SLinus Torvalds 
2412645ca708SEric Dumazet 	if (!sk) {
2413f86dcc5aSEric Dumazet 		if (state->bucket <= state->udp_table->mask)
2414f52b5054SEric Dumazet 			spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
2415645ca708SEric Dumazet 		return udp_get_first(seq, state->bucket + 1);
24161da177e4SLinus Torvalds 	}
24171da177e4SLinus Torvalds 	return sk;
24181da177e4SLinus Torvalds }
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
24211da177e4SLinus Torvalds {
2422645ca708SEric Dumazet 	struct sock *sk = udp_get_first(seq, 0);
24231da177e4SLinus Torvalds 
24241da177e4SLinus Torvalds 	if (sk)
24251da177e4SLinus Torvalds 		while (pos && (sk = udp_get_next(seq, sk)) != NULL)
24261da177e4SLinus Torvalds 			--pos;
24271da177e4SLinus Torvalds 	return pos ? NULL : sk;
24281da177e4SLinus Torvalds }
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
24311da177e4SLinus Torvalds {
243230842f29SVitaly Mayatskikh 	struct udp_iter_state *state = seq->private;
2433f86dcc5aSEric Dumazet 	state->bucket = MAX_UDP_PORTS;
243430842f29SVitaly Mayatskikh 
2435b50660f1SYOSHIFUJI Hideaki 	return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
24361da177e4SLinus Torvalds }
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
24391da177e4SLinus Torvalds {
24401da177e4SLinus Torvalds 	struct sock *sk;
24411da177e4SLinus Torvalds 
2442b50660f1SYOSHIFUJI Hideaki 	if (v == SEQ_START_TOKEN)
24431da177e4SLinus Torvalds 		sk = udp_get_idx(seq, 0);
24441da177e4SLinus Torvalds 	else
24451da177e4SLinus Torvalds 		sk = udp_get_next(seq, v);
24461da177e4SLinus Torvalds 
24471da177e4SLinus Torvalds 	++*pos;
24481da177e4SLinus Torvalds 	return sk;
24491da177e4SLinus Torvalds }
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v)
24521da177e4SLinus Torvalds {
2453645ca708SEric Dumazet 	struct udp_iter_state *state = seq->private;
2454645ca708SEric Dumazet 
2455f86dcc5aSEric Dumazet 	if (state->bucket <= state->udp_table->mask)
2456645ca708SEric Dumazet 		spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
24571da177e4SLinus Torvalds }
24581da177e4SLinus Torvalds 
245973cb88ecSArjan van de Ven int udp_seq_open(struct inode *inode, struct file *file)
24601da177e4SLinus Torvalds {
2461d9dda78bSAl Viro 	struct udp_seq_afinfo *afinfo = PDE_DATA(inode);
2462a2be75c1SDenis V. Lunev 	struct udp_iter_state *s;
2463a2be75c1SDenis V. Lunev 	int err;
24641da177e4SLinus Torvalds 
2465a2be75c1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
2466a2be75c1SDenis V. Lunev 			   sizeof(struct udp_iter_state));
2467a2be75c1SDenis V. Lunev 	if (err < 0)
2468a2be75c1SDenis V. Lunev 		return err;
2469a91275efSDaniel Lezcano 
2470a2be75c1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
24711da177e4SLinus Torvalds 	s->family		= afinfo->family;
2472645ca708SEric Dumazet 	s->udp_table		= afinfo->udp_table;
2473a2be75c1SDenis V. Lunev 	return err;
2474a91275efSDaniel Lezcano }
247573cb88ecSArjan van de Ven EXPORT_SYMBOL(udp_seq_open);
2476a91275efSDaniel Lezcano 
24771da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
24780c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
24791da177e4SLinus Torvalds {
24801da177e4SLinus Torvalds 	struct proc_dir_entry *p;
24811da177e4SLinus Torvalds 	int rc = 0;
24821da177e4SLinus Torvalds 
2483dda61925SDenis V. Lunev 	afinfo->seq_ops.start		= udp_seq_start;
2484dda61925SDenis V. Lunev 	afinfo->seq_ops.next		= udp_seq_next;
2485dda61925SDenis V. Lunev 	afinfo->seq_ops.stop		= udp_seq_stop;
2486dda61925SDenis V. Lunev 
248784841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
248873cb88ecSArjan van de Ven 			     afinfo->seq_fops, afinfo);
248984841c3cSDenis V. Lunev 	if (!p)
24901da177e4SLinus Torvalds 		rc = -ENOMEM;
24911da177e4SLinus Torvalds 	return rc;
24921da177e4SLinus Torvalds }
2493c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_register);
24941da177e4SLinus Torvalds 
24950c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
24961da177e4SLinus Torvalds {
2497ece31ffdSGao feng 	remove_proc_entry(afinfo->name, net->proc_net);
24981da177e4SLinus Torvalds }
2499c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_unregister);
2500db8dac20SDavid S. Miller 
2501db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
25025e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2503652586dfSTetsuo Handa 		int bucket)
2504db8dac20SDavid S. Miller {
2505db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
2506c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2507c720c7e8SEric Dumazet 	__be32 src  = inet->inet_rcv_saddr;
2508c720c7e8SEric Dumazet 	__u16 destp	  = ntohs(inet->inet_dport);
2509c720c7e8SEric Dumazet 	__u16 srcp	  = ntohs(inet->inet_sport);
2510db8dac20SDavid S. Miller 
2511f86dcc5aSEric Dumazet 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2512652586dfSTetsuo Handa 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
2513db8dac20SDavid S. Miller 		bucket, src, srcp, dest, destp, sp->sk_state,
251431e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
251531e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
2516a7cb5a49SEric W. Biederman 		0, 0L, 0,
2517a7cb5a49SEric W. Biederman 		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
2518a7cb5a49SEric W. Biederman 		0, sock_i_ino(sp),
2519cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_refcnt), sp,
2520652586dfSTetsuo Handa 		atomic_read(&sp->sk_drops));
2521db8dac20SDavid S. Miller }
2522db8dac20SDavid S. Miller 
2523db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v)
2524db8dac20SDavid S. Miller {
2525652586dfSTetsuo Handa 	seq_setwidth(seq, 127);
2526db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
2527652586dfSTetsuo Handa 		seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
2528db8dac20SDavid S. Miller 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
2529cb61cb9bSEric Dumazet 			   "inode ref pointer drops");
2530db8dac20SDavid S. Miller 	else {
2531db8dac20SDavid S. Miller 		struct udp_iter_state *state = seq->private;
2532db8dac20SDavid S. Miller 
2533652586dfSTetsuo Handa 		udp4_format_sock(v, seq, state->bucket);
2534db8dac20SDavid S. Miller 	}
2535652586dfSTetsuo Handa 	seq_pad(seq, '\n');
2536db8dac20SDavid S. Miller 	return 0;
2537db8dac20SDavid S. Miller }
2538db8dac20SDavid S. Miller 
253973cb88ecSArjan van de Ven static const struct file_operations udp_afinfo_seq_fops = {
254073cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
254173cb88ecSArjan van de Ven 	.open     = udp_seq_open,
254273cb88ecSArjan van de Ven 	.read     = seq_read,
254373cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
254473cb88ecSArjan van de Ven 	.release  = seq_release_net
254573cb88ecSArjan van de Ven };
254673cb88ecSArjan van de Ven 
2547db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
2548db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = {
2549db8dac20SDavid S. Miller 	.name		= "udp",
2550db8dac20SDavid S. Miller 	.family		= AF_INET,
2551645ca708SEric Dumazet 	.udp_table	= &udp_table,
255273cb88ecSArjan van de Ven 	.seq_fops	= &udp_afinfo_seq_fops,
2553dda61925SDenis V. Lunev 	.seq_ops	= {
2554dda61925SDenis V. Lunev 		.show		= udp4_seq_show,
2555dda61925SDenis V. Lunev 	},
2556db8dac20SDavid S. Miller };
2557db8dac20SDavid S. Miller 
25582c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net)
255915439febSPavel Emelyanov {
256015439febSPavel Emelyanov 	return udp_proc_register(net, &udp4_seq_afinfo);
256115439febSPavel Emelyanov }
256215439febSPavel Emelyanov 
25632c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net)
256415439febSPavel Emelyanov {
256515439febSPavel Emelyanov 	udp_proc_unregister(net, &udp4_seq_afinfo);
256615439febSPavel Emelyanov }
256715439febSPavel Emelyanov 
256815439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = {
256915439febSPavel Emelyanov 	.init = udp4_proc_init_net,
257015439febSPavel Emelyanov 	.exit = udp4_proc_exit_net,
257115439febSPavel Emelyanov };
257215439febSPavel Emelyanov 
2573db8dac20SDavid S. Miller int __init udp4_proc_init(void)
2574db8dac20SDavid S. Miller {
257515439febSPavel Emelyanov 	return register_pernet_subsys(&udp4_net_ops);
2576db8dac20SDavid S. Miller }
2577db8dac20SDavid S. Miller 
2578db8dac20SDavid S. Miller void udp4_proc_exit(void)
2579db8dac20SDavid S. Miller {
258015439febSPavel Emelyanov 	unregister_pernet_subsys(&udp4_net_ops);
2581db8dac20SDavid S. Miller }
25821da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
25831da177e4SLinus Torvalds 
2584f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries;
2585f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str)
2586645ca708SEric Dumazet {
2587413c27d8SEldad Zack 	ssize_t ret;
2588413c27d8SEldad Zack 
2589f86dcc5aSEric Dumazet 	if (!str)
2590f86dcc5aSEric Dumazet 		return 0;
2591413c27d8SEldad Zack 
2592413c27d8SEldad Zack 	ret = kstrtoul(str, 0, &uhash_entries);
2593413c27d8SEldad Zack 	if (ret)
2594413c27d8SEldad Zack 		return 0;
2595413c27d8SEldad Zack 
2596f86dcc5aSEric Dumazet 	if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
2597f86dcc5aSEric Dumazet 		uhash_entries = UDP_HTABLE_SIZE_MIN;
2598f86dcc5aSEric Dumazet 	return 1;
2599f86dcc5aSEric Dumazet }
2600f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries);
2601645ca708SEric Dumazet 
2602f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name)
2603f86dcc5aSEric Dumazet {
2604f86dcc5aSEric Dumazet 	unsigned int i;
2605f86dcc5aSEric Dumazet 
2606f86dcc5aSEric Dumazet 	table->hash = alloc_large_system_hash(name,
2607512615b6SEric Dumazet 					      2 * sizeof(struct udp_hslot),
2608f86dcc5aSEric Dumazet 					      uhash_entries,
2609f86dcc5aSEric Dumazet 					      21, /* one slot per 2 MB */
2610f86dcc5aSEric Dumazet 					      0,
2611f86dcc5aSEric Dumazet 					      &table->log,
2612f86dcc5aSEric Dumazet 					      &table->mask,
261331fe62b9STim Bird 					      UDP_HTABLE_SIZE_MIN,
2614f86dcc5aSEric Dumazet 					      64 * 1024);
261531fe62b9STim Bird 
2616512615b6SEric Dumazet 	table->hash2 = table->hash + (table->mask + 1);
2617f86dcc5aSEric Dumazet 	for (i = 0; i <= table->mask; i++) {
261888ab1932SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
2619fdcc8aa9SEric Dumazet 		table->hash[i].count = 0;
2620645ca708SEric Dumazet 		spin_lock_init(&table->hash[i].lock);
2621645ca708SEric Dumazet 	}
2622512615b6SEric Dumazet 	for (i = 0; i <= table->mask; i++) {
2623512615b6SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
2624512615b6SEric Dumazet 		table->hash2[i].count = 0;
2625512615b6SEric Dumazet 		spin_lock_init(&table->hash2[i].lock);
2626512615b6SEric Dumazet 	}
2627645ca708SEric Dumazet }
2628645ca708SEric Dumazet 
2629723b8e46STom Herbert u32 udp_flow_hashrnd(void)
2630723b8e46STom Herbert {
2631723b8e46STom Herbert 	static u32 hashrnd __read_mostly;
2632723b8e46STom Herbert 
2633723b8e46STom Herbert 	net_get_random_once(&hashrnd, sizeof(hashrnd));
2634723b8e46STom Herbert 
2635723b8e46STom Herbert 	return hashrnd;
2636723b8e46STom Herbert }
2637723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd);
2638723b8e46STom Herbert 
263995766fffSHideo Aoki void __init udp_init(void)
264095766fffSHideo Aoki {
2641f03d78dbSEric Dumazet 	unsigned long limit;
264295766fffSHideo Aoki 
2643f86dcc5aSEric Dumazet 	udp_table_init(&udp_table, "UDP");
2644f03d78dbSEric Dumazet 	limit = nr_free_buffer_pages() / 8;
264595766fffSHideo Aoki 	limit = max(limit, 128UL);
264695766fffSHideo Aoki 	sysctl_udp_mem[0] = limit / 4 * 3;
264795766fffSHideo Aoki 	sysctl_udp_mem[1] = limit;
264895766fffSHideo Aoki 	sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
264995766fffSHideo Aoki 
265095766fffSHideo Aoki 	sysctl_udp_rmem_min = SK_MEM_QUANTUM;
265195766fffSHideo Aoki 	sysctl_udp_wmem_min = SK_MEM_QUANTUM;
265295766fffSHideo Aoki }
2653