xref: /linux/net/ipv4/udp.c (revision ad0ea1989cc4d5905941d0a9e62c63ad6d859cef)
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  */
359c125e80bSCraig Gallek 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,
4961134158bSCraig Gallek 		struct udp_hslot *hslot2, unsigned int slot2,
4971134158bSCraig 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;
502ed0dfffdSEric Dumazet 	bool select_ok = true;
503ba418fa3STom Herbert 	u32 hash = 0;
5045051ebd2SEric Dumazet 
5055051ebd2SEric Dumazet begin:
5065051ebd2SEric Dumazet 	result = NULL;
507ba418fa3STom Herbert 	badness = 0;
5085051ebd2SEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
5095051ebd2SEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
5105051ebd2SEric Dumazet 				      daddr, hnum, dif);
5115051ebd2SEric Dumazet 		if (score > badness) {
5125051ebd2SEric Dumazet 			result = sk;
5135051ebd2SEric Dumazet 			badness = score;
514ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
515ba418fa3STom Herbert 			if (reuseport) {
51665cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
5177c0cadc6SEric Dumazet 						   saddr, sport);
518ed0dfffdSEric Dumazet 				if (select_ok) {
519ed0dfffdSEric Dumazet 					struct sock *sk2;
520ed0dfffdSEric Dumazet 
5211134158bSCraig Gallek 					sk2 = reuseport_select_sock(sk, hash, skb,
5221134158bSCraig Gallek 							sizeof(struct udphdr));
523e32ea7e7SCraig Gallek 					if (sk2) {
524e32ea7e7SCraig Gallek 						result = sk2;
525ed0dfffdSEric Dumazet 						select_ok = false;
526e32ea7e7SCraig Gallek 						goto found;
527e32ea7e7SCraig Gallek 					}
528ed0dfffdSEric Dumazet 				}
529ba418fa3STom Herbert 				matches = 1;
530ba418fa3STom Herbert 			}
531ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
532ba418fa3STom Herbert 			matches++;
5338fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
534ba418fa3STom Herbert 				result = sk;
535ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
5365051ebd2SEric Dumazet 		}
5375051ebd2SEric Dumazet 	}
5385051ebd2SEric Dumazet 	/*
5395051ebd2SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
5405051ebd2SEric Dumazet 	 * not the expected one, we must restart lookup.
5415051ebd2SEric Dumazet 	 * We probably met an item that was moved to another chain.
5425051ebd2SEric Dumazet 	 */
5435051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot2)
5445051ebd2SEric Dumazet 		goto begin;
5455051ebd2SEric Dumazet 	if (result) {
546e32ea7e7SCraig Gallek found:
547c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
5485051ebd2SEric Dumazet 			result = NULL;
5495051ebd2SEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
5505051ebd2SEric Dumazet 				  daddr, hnum, dif) < badness)) {
5515051ebd2SEric Dumazet 			sock_put(result);
5525051ebd2SEric Dumazet 			goto begin;
5535051ebd2SEric Dumazet 		}
5545051ebd2SEric Dumazet 	}
5555051ebd2SEric Dumazet 	return result;
5565051ebd2SEric Dumazet }
5575051ebd2SEric Dumazet 
558db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
559db8dac20SDavid S. Miller  * harder than this. -DaveM
560db8dac20SDavid S. Miller  */
561fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
562db8dac20SDavid S. Miller 		__be16 sport, __be32 daddr, __be16 dport,
563538950a1SCraig Gallek 		int dif, struct udp_table *udptable, struct sk_buff *skb)
564db8dac20SDavid S. Miller {
565271b72c7SEric Dumazet 	struct sock *sk, *result;
56688ab1932SEric Dumazet 	struct hlist_nulls_node *node;
567db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(dport);
5685051ebd2SEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
5695051ebd2SEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
570ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
571ed0dfffdSEric Dumazet 	bool select_ok = true;
572ba418fa3STom Herbert 	u32 hash = 0;
573db8dac20SDavid S. Miller 
574271b72c7SEric Dumazet 	rcu_read_lock();
5755051ebd2SEric Dumazet 	if (hslot->count > 10) {
5765051ebd2SEric Dumazet 		hash2 = udp4_portaddr_hash(net, daddr, hnum);
5775051ebd2SEric Dumazet 		slot2 = hash2 & udptable->mask;
5785051ebd2SEric Dumazet 		hslot2 = &udptable->hash2[slot2];
5795051ebd2SEric Dumazet 		if (hslot->count < hslot2->count)
5805051ebd2SEric Dumazet 			goto begin;
5815051ebd2SEric Dumazet 
5825051ebd2SEric Dumazet 		result = udp4_lib_lookup2(net, saddr, sport,
5835051ebd2SEric Dumazet 					  daddr, hnum, dif,
5841134158bSCraig Gallek 					  hslot2, slot2, skb);
5855051ebd2SEric Dumazet 		if (!result) {
5860eae88f3SEric Dumazet 			hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
5875051ebd2SEric Dumazet 			slot2 = hash2 & udptable->mask;
5885051ebd2SEric Dumazet 			hslot2 = &udptable->hash2[slot2];
5895051ebd2SEric Dumazet 			if (hslot->count < hslot2->count)
5905051ebd2SEric Dumazet 				goto begin;
5915051ebd2SEric Dumazet 
5921223c67cSJorge Boncompte [DTI2] 			result = udp4_lib_lookup2(net, saddr, sport,
5930eae88f3SEric Dumazet 						  htonl(INADDR_ANY), hnum, dif,
5941134158bSCraig Gallek 						  hslot2, slot2, skb);
5955051ebd2SEric Dumazet 		}
5965051ebd2SEric Dumazet 		rcu_read_unlock();
5975051ebd2SEric Dumazet 		return result;
5985051ebd2SEric Dumazet 	}
599271b72c7SEric Dumazet begin:
600271b72c7SEric Dumazet 	result = NULL;
601ba418fa3STom Herbert 	badness = 0;
60288ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
603645ca708SEric Dumazet 		score = compute_score(sk, net, saddr, hnum, sport,
604645ca708SEric Dumazet 				      daddr, dport, dif);
605645ca708SEric Dumazet 		if (score > badness) {
606db8dac20SDavid S. Miller 			result = sk;
607db8dac20SDavid S. Miller 			badness = score;
608ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
609ba418fa3STom Herbert 			if (reuseport) {
61065cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
6117c0cadc6SEric Dumazet 						   saddr, sport);
612ed0dfffdSEric Dumazet 				if (select_ok) {
613ed0dfffdSEric Dumazet 					struct sock *sk2;
614ed0dfffdSEric Dumazet 
615538950a1SCraig Gallek 					sk2 = reuseport_select_sock(sk, hash, skb,
616538950a1SCraig Gallek 							sizeof(struct udphdr));
617e32ea7e7SCraig Gallek 					if (sk2) {
618e32ea7e7SCraig Gallek 						result = sk2;
619ed0dfffdSEric Dumazet 						select_ok = false;
620e32ea7e7SCraig Gallek 						goto found;
621e32ea7e7SCraig Gallek 					}
622ed0dfffdSEric Dumazet 				}
623ba418fa3STom Herbert 				matches = 1;
624ba418fa3STom Herbert 			}
625ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
626ba418fa3STom Herbert 			matches++;
6278fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
628ba418fa3STom Herbert 				result = sk;
629ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
630db8dac20SDavid S. Miller 		}
631db8dac20SDavid S. Miller 	}
63288ab1932SEric Dumazet 	/*
63388ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
63488ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
63588ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
63688ab1932SEric Dumazet 	 */
6375051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot)
63888ab1932SEric Dumazet 		goto begin;
63988ab1932SEric Dumazet 
640271b72c7SEric Dumazet 	if (result) {
641e32ea7e7SCraig Gallek found:
642c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
643271b72c7SEric Dumazet 			result = NULL;
644271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, saddr, hnum, sport,
645271b72c7SEric Dumazet 				  daddr, dport, dif) < badness)) {
646271b72c7SEric Dumazet 			sock_put(result);
647271b72c7SEric Dumazet 			goto begin;
648271b72c7SEric Dumazet 		}
649271b72c7SEric Dumazet 	}
650271b72c7SEric Dumazet 	rcu_read_unlock();
651db8dac20SDavid S. Miller 	return result;
652db8dac20SDavid S. Miller }
653fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
654db8dac20SDavid S. Miller 
655607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
656607c4aafSKOVACS Krisztian 						 __be16 sport, __be16 dport,
657645ca708SEric Dumazet 						 struct udp_table *udptable)
658607c4aafSKOVACS Krisztian {
659607c4aafSKOVACS Krisztian 	const struct iphdr *iph = ip_hdr(skb);
660607c4aafSKOVACS Krisztian 
661adf30907SEric Dumazet 	return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
662607c4aafSKOVACS Krisztian 				 iph->daddr, dport, inet_iif(skb),
663538950a1SCraig Gallek 				 udptable, skb);
664607c4aafSKOVACS Krisztian }
665607c4aafSKOVACS Krisztian 
666bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
667bcd41303SKOVACS Krisztian 			     __be32 daddr, __be16 dport, int dif)
668bcd41303SKOVACS Krisztian {
669e32ea7e7SCraig Gallek 	return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif,
670538950a1SCraig Gallek 				 &udp_table, NULL);
671bcd41303SKOVACS Krisztian }
672bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup);
673bcd41303SKOVACS Krisztian 
674421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
675421b3885SShawn Bohrer 				       __be16 loc_port, __be32 loc_addr,
676421b3885SShawn Bohrer 				       __be16 rmt_port, __be32 rmt_addr,
677421b3885SShawn Bohrer 				       int dif, unsigned short hnum)
678421b3885SShawn Bohrer {
679421b3885SShawn Bohrer 	struct inet_sock *inet = inet_sk(sk);
680421b3885SShawn Bohrer 
681421b3885SShawn Bohrer 	if (!net_eq(sock_net(sk), net) ||
682421b3885SShawn Bohrer 	    udp_sk(sk)->udp_port_hash != hnum ||
683421b3885SShawn Bohrer 	    (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
684421b3885SShawn Bohrer 	    (inet->inet_dport != rmt_port && inet->inet_dport) ||
685421b3885SShawn Bohrer 	    (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
686421b3885SShawn Bohrer 	    ipv6_only_sock(sk) ||
687421b3885SShawn Bohrer 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
688421b3885SShawn Bohrer 		return false;
689421b3885SShawn Bohrer 	if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif))
690421b3885SShawn Bohrer 		return false;
691421b3885SShawn Bohrer 	return true;
692421b3885SShawn Bohrer }
693421b3885SShawn Bohrer 
694db8dac20SDavid S. Miller /*
695db8dac20SDavid S. Miller  * This routine is called by the ICMP module when it gets some
696db8dac20SDavid S. Miller  * sort of error condition.  If err < 0 then the socket should
697db8dac20SDavid S. Miller  * be closed and the error returned to the user.  If err > 0
698db8dac20SDavid S. Miller  * it's just the icmp type << 8 | icmp code.
699db8dac20SDavid S. Miller  * Header points to the ip header of the error packet. We move
700db8dac20SDavid S. Miller  * on past this. Then (as it used to claim before adjustment)
701db8dac20SDavid S. Miller  * header points to the first 8 bytes of the udp header.  We need
702db8dac20SDavid S. Miller  * to find the appropriate port.
703db8dac20SDavid S. Miller  */
704db8dac20SDavid S. Miller 
705645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
706db8dac20SDavid S. Miller {
707db8dac20SDavid S. Miller 	struct inet_sock *inet;
708b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *)skb->data;
709db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
710db8dac20SDavid S. Miller 	const int type = icmp_hdr(skb)->type;
711db8dac20SDavid S. Miller 	const int code = icmp_hdr(skb)->code;
712db8dac20SDavid S. Miller 	struct sock *sk;
713db8dac20SDavid S. Miller 	int harderr;
714db8dac20SDavid S. Miller 	int err;
715fd54d716SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
716db8dac20SDavid S. Miller 
717fd54d716SPavel Emelyanov 	sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
718538950a1SCraig Gallek 			iph->saddr, uh->source, skb->dev->ifindex, udptable,
719538950a1SCraig Gallek 			NULL);
72051456b29SIan Morris 	if (!sk) {
721dcfc23caSPavel Emelyanov 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
722db8dac20SDavid S. Miller 		return;	/* No socket for error */
723db8dac20SDavid S. Miller 	}
724db8dac20SDavid S. Miller 
725db8dac20SDavid S. Miller 	err = 0;
726db8dac20SDavid S. Miller 	harderr = 0;
727db8dac20SDavid S. Miller 	inet = inet_sk(sk);
728db8dac20SDavid S. Miller 
729db8dac20SDavid S. Miller 	switch (type) {
730db8dac20SDavid S. Miller 	default:
731db8dac20SDavid S. Miller 	case ICMP_TIME_EXCEEDED:
732db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
733db8dac20SDavid S. Miller 		break;
734db8dac20SDavid S. Miller 	case ICMP_SOURCE_QUENCH:
735db8dac20SDavid S. Miller 		goto out;
736db8dac20SDavid S. Miller 	case ICMP_PARAMETERPROB:
737db8dac20SDavid S. Miller 		err = EPROTO;
738db8dac20SDavid S. Miller 		harderr = 1;
739db8dac20SDavid S. Miller 		break;
740db8dac20SDavid S. Miller 	case ICMP_DEST_UNREACH:
741db8dac20SDavid S. Miller 		if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
74236393395SDavid S. Miller 			ipv4_sk_update_pmtu(skb, sk, info);
743db8dac20SDavid S. Miller 			if (inet->pmtudisc != IP_PMTUDISC_DONT) {
744db8dac20SDavid S. Miller 				err = EMSGSIZE;
745db8dac20SDavid S. Miller 				harderr = 1;
746db8dac20SDavid S. Miller 				break;
747db8dac20SDavid S. Miller 			}
748db8dac20SDavid S. Miller 			goto out;
749db8dac20SDavid S. Miller 		}
750db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
751db8dac20SDavid S. Miller 		if (code <= NR_ICMP_UNREACH) {
752db8dac20SDavid S. Miller 			harderr = icmp_err_convert[code].fatal;
753db8dac20SDavid S. Miller 			err = icmp_err_convert[code].errno;
754db8dac20SDavid S. Miller 		}
755db8dac20SDavid S. Miller 		break;
75655be7a9cSDavid S. Miller 	case ICMP_REDIRECT:
75755be7a9cSDavid S. Miller 		ipv4_sk_redirect(skb, sk);
7581a462d18SDuan Jiong 		goto out;
759db8dac20SDavid S. Miller 	}
760db8dac20SDavid S. Miller 
761db8dac20SDavid S. Miller 	/*
762db8dac20SDavid S. Miller 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
763db8dac20SDavid S. Miller 	 *	4.1.3.3.
764db8dac20SDavid S. Miller 	 */
765db8dac20SDavid S. Miller 	if (!inet->recverr) {
766db8dac20SDavid S. Miller 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
767db8dac20SDavid S. Miller 			goto out;
768b1faf566SEric Dumazet 	} else
769db8dac20SDavid S. Miller 		ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
770b1faf566SEric Dumazet 
771db8dac20SDavid S. Miller 	sk->sk_err = err;
772db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
773db8dac20SDavid S. Miller out:
774db8dac20SDavid S. Miller 	sock_put(sk);
775db8dac20SDavid S. Miller }
776db8dac20SDavid S. Miller 
777db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info)
778db8dac20SDavid S. Miller {
779645ca708SEric Dumazet 	__udp4_lib_err(skb, info, &udp_table);
780db8dac20SDavid S. Miller }
781db8dac20SDavid S. Miller 
782db8dac20SDavid S. Miller /*
783db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
784db8dac20SDavid S. Miller  */
78536d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk)
786db8dac20SDavid S. Miller {
787db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
788db8dac20SDavid S. Miller 
789db8dac20SDavid S. Miller 	if (up->pending) {
790db8dac20SDavid S. Miller 		up->len = 0;
791db8dac20SDavid S. Miller 		up->pending = 0;
792db8dac20SDavid S. Miller 		ip_flush_pending_frames(sk);
793db8dac20SDavid S. Miller 	}
794db8dac20SDavid S. Miller }
79536d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames);
796db8dac20SDavid S. Miller 
797db8dac20SDavid S. Miller /**
798f6b9664fSHerbert Xu  * 	udp4_hwcsum  -  handle outgoing HW checksumming
799db8dac20SDavid S. Miller  * 	@skb: 	sk_buff containing the filled-in UDP header
800db8dac20SDavid S. Miller  * 	        (checksum field must be zeroed out)
801f6b9664fSHerbert Xu  *	@src:	source IP address
802f6b9664fSHerbert Xu  *	@dst:	destination IP address
803db8dac20SDavid S. Miller  */
804c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
805db8dac20SDavid S. Miller {
806db8dac20SDavid S. Miller 	struct udphdr *uh = udp_hdr(skb);
807f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
808f6b9664fSHerbert Xu 	int len = skb->len - offset;
809f6b9664fSHerbert Xu 	int hlen = len;
810db8dac20SDavid S. Miller 	__wsum csum = 0;
811db8dac20SDavid S. Miller 
812ebbe495fSWANG Cong 	if (!skb_has_frag_list(skb)) {
813db8dac20SDavid S. Miller 		/*
814db8dac20SDavid S. Miller 		 * Only one fragment on the socket.
815db8dac20SDavid S. Miller 		 */
816db8dac20SDavid S. Miller 		skb->csum_start = skb_transport_header(skb) - skb->head;
817db8dac20SDavid S. Miller 		skb->csum_offset = offsetof(struct udphdr, check);
818f6b9664fSHerbert Xu 		uh->check = ~csum_tcpudp_magic(src, dst, len,
819f6b9664fSHerbert Xu 					       IPPROTO_UDP, 0);
820db8dac20SDavid S. Miller 	} else {
821ebbe495fSWANG Cong 		struct sk_buff *frags;
822ebbe495fSWANG Cong 
823db8dac20SDavid S. Miller 		/*
824db8dac20SDavid S. Miller 		 * HW-checksum won't work as there are two or more
825db8dac20SDavid S. Miller 		 * fragments on the socket so that all csums of sk_buffs
826db8dac20SDavid S. Miller 		 * should be together
827db8dac20SDavid S. Miller 		 */
828ebbe495fSWANG Cong 		skb_walk_frags(skb, frags) {
829f6b9664fSHerbert Xu 			csum = csum_add(csum, frags->csum);
830f6b9664fSHerbert Xu 			hlen -= frags->len;
831ebbe495fSWANG Cong 		}
832db8dac20SDavid S. Miller 
833f6b9664fSHerbert Xu 		csum = skb_checksum(skb, offset, hlen, csum);
834db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
835db8dac20SDavid S. Miller 
836db8dac20SDavid S. Miller 		uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
837db8dac20SDavid S. Miller 		if (uh->check == 0)
838db8dac20SDavid S. Miller 			uh->check = CSUM_MANGLED_0;
839db8dac20SDavid S. Miller 	}
840db8dac20SDavid S. Miller }
841c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum);
842db8dac20SDavid S. Miller 
843af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended
844af5fcba7STom Herbert  * for the simple case like when setting the checksum for a UDP tunnel.
845af5fcba7STom Herbert  */
846af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb,
847af5fcba7STom Herbert 		  __be32 saddr, __be32 daddr, int len)
848af5fcba7STom Herbert {
849af5fcba7STom Herbert 	struct udphdr *uh = udp_hdr(skb);
850af5fcba7STom Herbert 
851179bc67fSEdward Cree 	if (nocheck) {
852af5fcba7STom Herbert 		uh->check = 0;
853179bc67fSEdward Cree 	} else if (skb_is_gso(skb)) {
854af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
855179bc67fSEdward Cree 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
856179bc67fSEdward Cree 		uh->check = 0;
857179bc67fSEdward Cree 		uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb));
858179bc67fSEdward Cree 		if (uh->check == 0)
859179bc67fSEdward Cree 			uh->check = CSUM_MANGLED_0;
860d75f1306SEdward Cree 	} else {
861af5fcba7STom Herbert 		skb->ip_summed = CHECKSUM_PARTIAL;
862af5fcba7STom Herbert 		skb->csum_start = skb_transport_header(skb) - skb->head;
863af5fcba7STom Herbert 		skb->csum_offset = offsetof(struct udphdr, check);
864af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
865af5fcba7STom Herbert 	}
866af5fcba7STom Herbert }
867af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum);
868af5fcba7STom Herbert 
86979ab0531SDavid S. Miller static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4)
870f6b9664fSHerbert Xu {
871f6b9664fSHerbert Xu 	struct sock *sk = skb->sk;
872f6b9664fSHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
873f6b9664fSHerbert Xu 	struct udphdr *uh;
874f6b9664fSHerbert Xu 	int err = 0;
875f6b9664fSHerbert Xu 	int is_udplite = IS_UDPLITE(sk);
876f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
877f6b9664fSHerbert Xu 	int len = skb->len - offset;
878f6b9664fSHerbert Xu 	__wsum csum = 0;
879f6b9664fSHerbert Xu 
880f6b9664fSHerbert Xu 	/*
881f6b9664fSHerbert Xu 	 * Create a UDP header
882f6b9664fSHerbert Xu 	 */
883f6b9664fSHerbert Xu 	uh = udp_hdr(skb);
884f6b9664fSHerbert Xu 	uh->source = inet->inet_sport;
88579ab0531SDavid S. Miller 	uh->dest = fl4->fl4_dport;
886f6b9664fSHerbert Xu 	uh->len = htons(len);
887f6b9664fSHerbert Xu 	uh->check = 0;
888f6b9664fSHerbert Xu 
889f6b9664fSHerbert Xu 	if (is_udplite)  				 /*     UDP-Lite      */
890f6b9664fSHerbert Xu 		csum = udplite_csum(skb);
891f6b9664fSHerbert Xu 
89228448b80STom Herbert 	else if (sk->sk_no_check_tx) {   /* UDP csum disabled */
893f6b9664fSHerbert Xu 
894f6b9664fSHerbert Xu 		skb->ip_summed = CHECKSUM_NONE;
895f6b9664fSHerbert Xu 		goto send;
896f6b9664fSHerbert Xu 
897f6b9664fSHerbert Xu 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
898f6b9664fSHerbert Xu 
89979ab0531SDavid S. Miller 		udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
900f6b9664fSHerbert Xu 		goto send;
901f6b9664fSHerbert Xu 
902f6b9664fSHerbert Xu 	} else
903f6b9664fSHerbert Xu 		csum = udp_csum(skb);
904f6b9664fSHerbert Xu 
905f6b9664fSHerbert Xu 	/* add protocol-dependent pseudo-header */
90679ab0531SDavid S. Miller 	uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
907f6b9664fSHerbert Xu 				      sk->sk_protocol, csum);
908f6b9664fSHerbert Xu 	if (uh->check == 0)
909f6b9664fSHerbert Xu 		uh->check = CSUM_MANGLED_0;
910f6b9664fSHerbert Xu 
911f6b9664fSHerbert Xu send:
912b5ec8eeaSEric Dumazet 	err = ip_send_skb(sock_net(sk), skb);
913f6b9664fSHerbert Xu 	if (err) {
914f6b9664fSHerbert Xu 		if (err == -ENOBUFS && !inet->recverr) {
915f6b9664fSHerbert Xu 			UDP_INC_STATS_USER(sock_net(sk),
916f6b9664fSHerbert Xu 					   UDP_MIB_SNDBUFERRORS, is_udplite);
917f6b9664fSHerbert Xu 			err = 0;
918f6b9664fSHerbert Xu 		}
919f6b9664fSHerbert Xu 	} else
920f6b9664fSHerbert Xu 		UDP_INC_STATS_USER(sock_net(sk),
921f6b9664fSHerbert Xu 				   UDP_MIB_OUTDATAGRAMS, is_udplite);
922f6b9664fSHerbert Xu 	return err;
923f6b9664fSHerbert Xu }
924f6b9664fSHerbert Xu 
925db8dac20SDavid S. Miller /*
926db8dac20SDavid S. Miller  * Push out all pending data as one UDP datagram. Socket is locked.
927db8dac20SDavid S. Miller  */
9288822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk)
929db8dac20SDavid S. Miller {
930db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
931db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
932b6f21b26SDavid S. Miller 	struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
933db8dac20SDavid S. Miller 	struct sk_buff *skb;
934db8dac20SDavid S. Miller 	int err = 0;
935db8dac20SDavid S. Miller 
93677968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
937f6b9664fSHerbert Xu 	if (!skb)
938db8dac20SDavid S. Miller 		goto out;
939db8dac20SDavid S. Miller 
94079ab0531SDavid S. Miller 	err = udp_send_skb(skb, fl4);
941db8dac20SDavid S. Miller 
942db8dac20SDavid S. Miller out:
943db8dac20SDavid S. Miller 	up->len = 0;
944db8dac20SDavid S. Miller 	up->pending = 0;
945db8dac20SDavid S. Miller 	return err;
946db8dac20SDavid S. Miller }
9478822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames);
948db8dac20SDavid S. Miller 
9491b784140SYing Xue int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
950db8dac20SDavid S. Miller {
951db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
952db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
953e474995fSDavid S. Miller 	struct flowi4 fl4_stack;
954b6f21b26SDavid S. Miller 	struct flowi4 *fl4;
955db8dac20SDavid S. Miller 	int ulen = len;
956db8dac20SDavid S. Miller 	struct ipcm_cookie ipc;
957db8dac20SDavid S. Miller 	struct rtable *rt = NULL;
958db8dac20SDavid S. Miller 	int free = 0;
959db8dac20SDavid S. Miller 	int connected = 0;
960db8dac20SDavid S. Miller 	__be32 daddr, faddr, saddr;
961db8dac20SDavid S. Miller 	__be16 dport;
962db8dac20SDavid S. Miller 	u8  tos;
963db8dac20SDavid S. Miller 	int err, is_udplite = IS_UDPLITE(sk);
964db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
965db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
966903ab86dSHerbert Xu 	struct sk_buff *skb;
967f6d8bd05SEric Dumazet 	struct ip_options_data opt_copy;
968db8dac20SDavid S. Miller 
969db8dac20SDavid S. Miller 	if (len > 0xFFFF)
970db8dac20SDavid S. Miller 		return -EMSGSIZE;
971db8dac20SDavid S. Miller 
972db8dac20SDavid S. Miller 	/*
973db8dac20SDavid S. Miller 	 *	Check the flags.
974db8dac20SDavid S. Miller 	 */
975db8dac20SDavid S. Miller 
976db8dac20SDavid S. Miller 	if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
977db8dac20SDavid S. Miller 		return -EOPNOTSUPP;
978db8dac20SDavid S. Miller 
979db8dac20SDavid S. Miller 	ipc.opt = NULL;
9802244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
981aa661581SFrancesco Fusco 	ipc.ttl = 0;
982aa661581SFrancesco Fusco 	ipc.tos = -1;
983db8dac20SDavid S. Miller 
984903ab86dSHerbert Xu 	getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
985903ab86dSHerbert Xu 
986f5fca608SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
987db8dac20SDavid S. Miller 	if (up->pending) {
988db8dac20SDavid S. Miller 		/*
989db8dac20SDavid S. Miller 		 * There are pending frames.
990db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
991db8dac20SDavid S. Miller 		 */
992db8dac20SDavid S. Miller 		lock_sock(sk);
993db8dac20SDavid S. Miller 		if (likely(up->pending)) {
994db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET)) {
995db8dac20SDavid S. Miller 				release_sock(sk);
996db8dac20SDavid S. Miller 				return -EINVAL;
997db8dac20SDavid S. Miller 			}
998db8dac20SDavid S. Miller 			goto do_append_data;
999db8dac20SDavid S. Miller 		}
1000db8dac20SDavid S. Miller 		release_sock(sk);
1001db8dac20SDavid S. Miller 	}
1002db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1003db8dac20SDavid S. Miller 
1004db8dac20SDavid S. Miller 	/*
1005db8dac20SDavid S. Miller 	 *	Get and verify the address.
1006db8dac20SDavid S. Miller 	 */
1007db8dac20SDavid S. Miller 	if (msg->msg_name) {
1008342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
1009db8dac20SDavid S. Miller 		if (msg->msg_namelen < sizeof(*usin))
1010db8dac20SDavid S. Miller 			return -EINVAL;
1011db8dac20SDavid S. Miller 		if (usin->sin_family != AF_INET) {
1012db8dac20SDavid S. Miller 			if (usin->sin_family != AF_UNSPEC)
1013db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1014db8dac20SDavid S. Miller 		}
1015db8dac20SDavid S. Miller 
1016db8dac20SDavid S. Miller 		daddr = usin->sin_addr.s_addr;
1017db8dac20SDavid S. Miller 		dport = usin->sin_port;
1018db8dac20SDavid S. Miller 		if (dport == 0)
1019db8dac20SDavid S. Miller 			return -EINVAL;
1020db8dac20SDavid S. Miller 	} else {
1021db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1022db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1023c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
1024c720c7e8SEric Dumazet 		dport = inet->inet_dport;
1025db8dac20SDavid S. Miller 		/* Open fast path for connected socket.
1026db8dac20SDavid S. Miller 		   Route will not be used, if at least one option is set.
1027db8dac20SDavid S. Miller 		 */
1028db8dac20SDavid S. Miller 		connected = 1;
1029db8dac20SDavid S. Miller 	}
1030c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
1031db8dac20SDavid S. Miller 
1032db8dac20SDavid S. Miller 	ipc.oif = sk->sk_bound_dev_if;
1033bf84a010SDaniel Borkmann 
1034bf84a010SDaniel Borkmann 	sock_tx_timestamp(sk, &ipc.tx_flags);
1035bf84a010SDaniel Borkmann 
1036db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1037c8e6ad08SHannes Frederic Sowa 		err = ip_cmsg_send(sock_net(sk), msg, &ipc,
1038c8e6ad08SHannes Frederic Sowa 				   sk->sk_family == AF_INET6);
103991948309SEric Dumazet 		if (unlikely(err)) {
104091948309SEric Dumazet 			kfree(ipc.opt);
1041db8dac20SDavid S. Miller 			return err;
104291948309SEric Dumazet 		}
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 
1100b5bdacf3SDavid Ahern 		if (!saddr && ipc.oif) {
1101b5bdacf3SDavid Ahern 			err = l3mdev_get_saddr(net, ipc.oif, fl4);
1102b5bdacf3SDavid Ahern 			if (err < 0)
1103b5bdacf3SDavid Ahern 				goto out;
1104b5bdacf3SDavid Ahern 		}
11058cbb512cSDavid Ahern 
1106e474995fSDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
1107e474995fSDavid S. Miller 		rt = ip_route_output_flow(net, fl4, sk);
1108b23dd4feSDavid S. Miller 		if (IS_ERR(rt)) {
1109b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
111006dc94b1SDavid S. Miller 			rt = NULL;
1111db8dac20SDavid S. Miller 			if (err == -ENETUNREACH)
1112f1d8cba6SEric Dumazet 				IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
1113db8dac20SDavid S. Miller 			goto out;
1114db8dac20SDavid S. Miller 		}
1115db8dac20SDavid S. Miller 
1116db8dac20SDavid S. Miller 		err = -EACCES;
1117db8dac20SDavid S. Miller 		if ((rt->rt_flags & RTCF_BROADCAST) &&
1118db8dac20SDavid S. Miller 		    !sock_flag(sk, SOCK_BROADCAST))
1119db8dac20SDavid S. Miller 			goto out;
1120db8dac20SDavid S. Miller 		if (connected)
1121d8d1f30bSChangli Gao 			sk_dst_set(sk, dst_clone(&rt->dst));
1122db8dac20SDavid S. Miller 	}
1123db8dac20SDavid S. Miller 
1124db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1125db8dac20SDavid S. Miller 		goto do_confirm;
1126db8dac20SDavid S. Miller back_from_confirm:
1127db8dac20SDavid S. Miller 
1128e474995fSDavid S. Miller 	saddr = fl4->saddr;
1129db8dac20SDavid S. Miller 	if (!ipc.addr)
1130e474995fSDavid S. Miller 		daddr = ipc.addr = fl4->daddr;
1131db8dac20SDavid S. Miller 
1132903ab86dSHerbert Xu 	/* Lockless fast path for the non-corking case. */
1133903ab86dSHerbert Xu 	if (!corkreq) {
1134f69e6d13SAl Viro 		skb = ip_make_skb(sk, fl4, getfrag, msg, ulen,
1135903ab86dSHerbert Xu 				  sizeof(struct udphdr), &ipc, &rt,
1136903ab86dSHerbert Xu 				  msg->msg_flags);
1137903ab86dSHerbert Xu 		err = PTR_ERR(skb);
113850c3a487SYOSHIFUJI Hideaki / 吉藤英明 		if (!IS_ERR_OR_NULL(skb))
113979ab0531SDavid S. Miller 			err = udp_send_skb(skb, fl4);
1140903ab86dSHerbert Xu 		goto out;
1141903ab86dSHerbert Xu 	}
1142903ab86dSHerbert Xu 
1143db8dac20SDavid S. Miller 	lock_sock(sk);
1144db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1145db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1146db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1147db8dac20SDavid S. Miller 		release_sock(sk);
1148db8dac20SDavid S. Miller 
1149ba7a46f1SJoe Perches 		net_dbg_ratelimited("cork app bug 2\n");
1150db8dac20SDavid S. Miller 		err = -EINVAL;
1151db8dac20SDavid S. Miller 		goto out;
1152db8dac20SDavid S. Miller 	}
1153db8dac20SDavid S. Miller 	/*
1154db8dac20SDavid S. Miller 	 *	Now cork the socket to pend data.
1155db8dac20SDavid S. Miller 	 */
1156b6f21b26SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
1157b6f21b26SDavid S. Miller 	fl4->daddr = daddr;
1158b6f21b26SDavid S. Miller 	fl4->saddr = saddr;
11599cce96dfSDavid S. Miller 	fl4->fl4_dport = dport;
11609cce96dfSDavid S. Miller 	fl4->fl4_sport = inet->inet_sport;
1161db8dac20SDavid S. Miller 	up->pending = AF_INET;
1162db8dac20SDavid S. Miller 
1163db8dac20SDavid S. Miller do_append_data:
1164db8dac20SDavid S. Miller 	up->len += ulen;
1165f69e6d13SAl Viro 	err = ip_append_data(sk, fl4, getfrag, msg, ulen,
11662e77d89bSEric Dumazet 			     sizeof(struct udphdr), &ipc, &rt,
1167db8dac20SDavid S. Miller 			     corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1168db8dac20SDavid S. Miller 	if (err)
1169db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1170db8dac20SDavid S. Miller 	else if (!corkreq)
1171db8dac20SDavid S. Miller 		err = udp_push_pending_frames(sk);
1172db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1173db8dac20SDavid S. Miller 		up->pending = 0;
1174db8dac20SDavid S. Miller 	release_sock(sk);
1175db8dac20SDavid S. Miller 
1176db8dac20SDavid S. Miller out:
1177db8dac20SDavid S. Miller 	ip_rt_put(rt);
1178db8dac20SDavid S. Miller 	if (free)
1179db8dac20SDavid S. Miller 		kfree(ipc.opt);
1180db8dac20SDavid S. Miller 	if (!err)
1181db8dac20SDavid S. Miller 		return len;
1182db8dac20SDavid S. Miller 	/*
1183db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1184db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1185db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1186db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1187db8dac20SDavid S. Miller 	 * seems like overkill.
1188db8dac20SDavid S. Miller 	 */
1189db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1190629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1191629ca23cSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1192db8dac20SDavid S. Miller 	}
1193db8dac20SDavid S. Miller 	return err;
1194db8dac20SDavid S. Miller 
1195db8dac20SDavid S. Miller do_confirm:
1196d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
1197db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1198db8dac20SDavid S. Miller 		goto back_from_confirm;
1199db8dac20SDavid S. Miller 	err = 0;
1200db8dac20SDavid S. Miller 	goto out;
1201db8dac20SDavid S. Miller }
1202c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg);
1203db8dac20SDavid S. Miller 
1204db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset,
1205db8dac20SDavid S. Miller 		 size_t size, int flags)
1206db8dac20SDavid S. Miller {
1207f5fca608SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1208db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1209db8dac20SDavid S. Miller 	int ret;
1210db8dac20SDavid S. Miller 
1211d3f7d56aSShawn Landden 	if (flags & MSG_SENDPAGE_NOTLAST)
1212d3f7d56aSShawn Landden 		flags |= MSG_MORE;
1213d3f7d56aSShawn Landden 
1214db8dac20SDavid S. Miller 	if (!up->pending) {
1215db8dac20SDavid S. Miller 		struct msghdr msg = {	.msg_flags = flags|MSG_MORE };
1216db8dac20SDavid S. Miller 
1217db8dac20SDavid S. Miller 		/* Call udp_sendmsg to specify destination address which
1218db8dac20SDavid S. Miller 		 * sendpage interface can't pass.
1219db8dac20SDavid S. Miller 		 * This will succeed only when the socket is connected.
1220db8dac20SDavid S. Miller 		 */
12211b784140SYing Xue 		ret = udp_sendmsg(sk, &msg, 0);
1222db8dac20SDavid S. Miller 		if (ret < 0)
1223db8dac20SDavid S. Miller 			return ret;
1224db8dac20SDavid S. Miller 	}
1225db8dac20SDavid S. Miller 
1226db8dac20SDavid S. Miller 	lock_sock(sk);
1227db8dac20SDavid S. Miller 
1228db8dac20SDavid S. Miller 	if (unlikely(!up->pending)) {
1229db8dac20SDavid S. Miller 		release_sock(sk);
1230db8dac20SDavid S. Miller 
1231ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 3\n");
1232db8dac20SDavid S. Miller 		return -EINVAL;
1233db8dac20SDavid S. Miller 	}
1234db8dac20SDavid S. Miller 
1235f5fca608SDavid S. Miller 	ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
1236f5fca608SDavid S. Miller 			     page, offset, size, flags);
1237db8dac20SDavid S. Miller 	if (ret == -EOPNOTSUPP) {
1238db8dac20SDavid S. Miller 		release_sock(sk);
1239db8dac20SDavid S. Miller 		return sock_no_sendpage(sk->sk_socket, page, offset,
1240db8dac20SDavid S. Miller 					size, flags);
1241db8dac20SDavid S. Miller 	}
1242db8dac20SDavid S. Miller 	if (ret < 0) {
1243db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1244db8dac20SDavid S. Miller 		goto out;
1245db8dac20SDavid S. Miller 	}
1246db8dac20SDavid S. Miller 
1247db8dac20SDavid S. Miller 	up->len += size;
1248db8dac20SDavid S. Miller 	if (!(up->corkflag || (flags&MSG_MORE)))
1249db8dac20SDavid S. Miller 		ret = udp_push_pending_frames(sk);
1250db8dac20SDavid S. Miller 	if (!ret)
1251db8dac20SDavid S. Miller 		ret = size;
1252db8dac20SDavid S. Miller out:
1253db8dac20SDavid S. Miller 	release_sock(sk);
1254db8dac20SDavid S. Miller 	return ret;
1255db8dac20SDavid S. Miller }
1256db8dac20SDavid S. Miller 
125785584672SEric Dumazet /**
125885584672SEric Dumazet  *	first_packet_length	- return length of first packet in receive queue
125985584672SEric Dumazet  *	@sk: socket
126085584672SEric Dumazet  *
126185584672SEric Dumazet  *	Drops all bad checksum frames, until a valid one is found.
126285584672SEric Dumazet  *	Returns the length of found skb, or 0 if none is found.
126385584672SEric Dumazet  */
126485584672SEric Dumazet static unsigned int first_packet_length(struct sock *sk)
126585584672SEric Dumazet {
126685584672SEric Dumazet 	struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
126785584672SEric Dumazet 	struct sk_buff *skb;
126885584672SEric Dumazet 	unsigned int res;
126985584672SEric Dumazet 
127085584672SEric Dumazet 	__skb_queue_head_init(&list_kill);
127185584672SEric Dumazet 
127285584672SEric Dumazet 	spin_lock_bh(&rcvq->lock);
127385584672SEric Dumazet 	while ((skb = skb_peek(rcvq)) != NULL &&
127485584672SEric Dumazet 		udp_lib_checksum_complete(skb)) {
12756a5dc9e5SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS,
12766a5dc9e5SEric Dumazet 				 IS_UDPLITE(sk));
127785584672SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
127885584672SEric Dumazet 				 IS_UDPLITE(sk));
12798edf19c2SEric Dumazet 		atomic_inc(&sk->sk_drops);
128085584672SEric Dumazet 		__skb_unlink(skb, rcvq);
128185584672SEric Dumazet 		__skb_queue_tail(&list_kill, skb);
128285584672SEric Dumazet 	}
128385584672SEric Dumazet 	res = skb ? skb->len : 0;
128485584672SEric Dumazet 	spin_unlock_bh(&rcvq->lock);
128585584672SEric Dumazet 
128685584672SEric Dumazet 	if (!skb_queue_empty(&list_kill)) {
12878a74ad60SEric Dumazet 		bool slow = lock_sock_fast(sk);
12888a74ad60SEric Dumazet 
128985584672SEric Dumazet 		__skb_queue_purge(&list_kill);
129085584672SEric Dumazet 		sk_mem_reclaim_partial(sk);
12918a74ad60SEric Dumazet 		unlock_sock_fast(sk, slow);
129285584672SEric Dumazet 	}
129385584672SEric Dumazet 	return res;
129485584672SEric Dumazet }
129585584672SEric Dumazet 
12961da177e4SLinus Torvalds /*
12971da177e4SLinus Torvalds  *	IOCTL requests applicable to the UDP protocol
12981da177e4SLinus Torvalds  */
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
13011da177e4SLinus Torvalds {
13026516c655SStephen Hemminger 	switch (cmd) {
13031da177e4SLinus Torvalds 	case SIOCOUTQ:
13041da177e4SLinus Torvalds 	{
130531e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
130631e6d363SEric Dumazet 
13071da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
13081da177e4SLinus Torvalds 	}
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	case SIOCINQ:
13111da177e4SLinus Torvalds 	{
131285584672SEric Dumazet 		unsigned int amount = first_packet_length(sk);
13131da177e4SLinus Torvalds 
131485584672SEric Dumazet 		if (amount)
13151da177e4SLinus Torvalds 			/*
13161da177e4SLinus Torvalds 			 * We will only return the amount
13171da177e4SLinus Torvalds 			 * of this packet since that is all
13181da177e4SLinus Torvalds 			 * that will be read.
13191da177e4SLinus Torvalds 			 */
132085584672SEric Dumazet 			amount -= sizeof(struct udphdr);
132185584672SEric Dumazet 
13221da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	default:
13261da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
13271da177e4SLinus Torvalds 	}
13286516c655SStephen Hemminger 
13296516c655SStephen Hemminger 	return 0;
13301da177e4SLinus Torvalds }
1331c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl);
13321da177e4SLinus Torvalds 
1333db8dac20SDavid S. Miller /*
1334db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
1335db8dac20SDavid S. Miller  * 	return it, otherwise we block.
1336db8dac20SDavid S. Miller  */
1337db8dac20SDavid S. Miller 
13381b784140SYing Xue int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
13391b784140SYing Xue 		int flags, int *addr_len)
1340db8dac20SDavid S. Miller {
1341db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1342342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
1343db8dac20SDavid S. Miller 	struct sk_buff *skb;
134459c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
13453f518bf7SPavel Emelyanov 	int peeked, off = 0;
1346db8dac20SDavid S. Miller 	int err;
1347db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1348197c949eSEric Dumazet 	bool checksum_valid = false;
13498a74ad60SEric Dumazet 	bool slow;
1350db8dac20SDavid S. Miller 
1351db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
135285fbaa75SHannes Frederic Sowa 		return ip_recv_error(sk, msg, len, addr_len);
1353db8dac20SDavid S. Miller 
1354db8dac20SDavid S. Miller try_again:
1355db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
13563f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
1357db8dac20SDavid S. Miller 	if (!skb)
1358db8dac20SDavid S. Miller 		goto out;
1359db8dac20SDavid S. Miller 
1360db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
136159c2cdaeSDavid S. Miller 	copied = len;
136259c2cdaeSDavid S. Miller 	if (copied > ulen)
136359c2cdaeSDavid S. Miller 		copied = ulen;
136459c2cdaeSDavid S. Miller 	else if (copied < ulen)
1365db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
1366db8dac20SDavid S. Miller 
1367db8dac20SDavid S. Miller 	/*
1368db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
1369db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
1370db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
1371db8dac20SDavid S. Miller 	 */
1372db8dac20SDavid S. Miller 
137359c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
1374197c949eSEric Dumazet 		checksum_valid = !udp_lib_checksum_complete(skb);
1375197c949eSEric Dumazet 		if (!checksum_valid)
1376db8dac20SDavid S. Miller 			goto csum_copy_err;
1377db8dac20SDavid S. Miller 	}
1378db8dac20SDavid S. Miller 
1379197c949eSEric Dumazet 	if (checksum_valid || skb_csum_unnecessary(skb))
138051f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, sizeof(struct udphdr),
138151f3d02bSDavid S. Miller 					    msg, copied);
1382db8dac20SDavid S. Miller 	else {
1383227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, sizeof(struct udphdr),
1384227158dbSAl Viro 						     msg);
1385db8dac20SDavid S. Miller 
1386db8dac20SDavid S. Miller 		if (err == -EINVAL)
1387db8dac20SDavid S. Miller 			goto csum_copy_err;
1388db8dac20SDavid S. Miller 	}
1389db8dac20SDavid S. Miller 
139022911fc5SEric Dumazet 	if (unlikely(err)) {
139122911fc5SEric Dumazet 		trace_kfree_skb(skb, udp_recvmsg);
1392979402b1SEric Dumazet 		if (!peeked) {
1393979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
1394979402b1SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
1395979402b1SEric Dumazet 					   UDP_MIB_INERRORS, is_udplite);
1396979402b1SEric Dumazet 		}
1397db8dac20SDavid S. Miller 		goto out_free;
139822911fc5SEric Dumazet 	}
1399db8dac20SDavid S. Miller 
1400db8dac20SDavid S. Miller 	if (!peeked)
1401629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1402629ca23cSPavel Emelyanov 				UDP_MIB_INDATAGRAMS, is_udplite);
1403db8dac20SDavid S. Miller 
14043b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
1405db8dac20SDavid S. Miller 
1406db8dac20SDavid S. Miller 	/* Copy the address. */
1407c482c568SEric Dumazet 	if (sin) {
1408db8dac20SDavid S. Miller 		sin->sin_family = AF_INET;
1409db8dac20SDavid S. Miller 		sin->sin_port = udp_hdr(skb)->source;
1410db8dac20SDavid S. Miller 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1411db8dac20SDavid S. Miller 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1412bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin);
1413db8dac20SDavid S. Miller 	}
1414db8dac20SDavid S. Miller 	if (inet->cmsg_flags)
1415ad6f939aSTom Herbert 		ip_cmsg_recv_offset(msg, skb, sizeof(struct udphdr));
1416db8dac20SDavid S. Miller 
141759c2cdaeSDavid S. Miller 	err = copied;
1418db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
1419db8dac20SDavid S. Miller 		err = ulen;
1420db8dac20SDavid S. Miller 
1421db8dac20SDavid S. Miller out_free:
14229d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
1423db8dac20SDavid S. Miller out:
1424db8dac20SDavid S. Miller 	return err;
1425db8dac20SDavid S. Miller 
1426db8dac20SDavid S. Miller csum_copy_err:
14278a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
14286a5dc9e5SEric Dumazet 	if (!skb_kill_datagram(sk, skb, flags)) {
14296a5dc9e5SEric Dumazet 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1430629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
14316a5dc9e5SEric Dumazet 	}
14328a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
1433db8dac20SDavid S. Miller 
1434beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
1435beb39db5SEric Dumazet 	cond_resched();
14369cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
1437db8dac20SDavid S. Miller 	goto try_again;
1438db8dac20SDavid S. Miller }
1439db8dac20SDavid S. Miller 
14401da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags)
14411da177e4SLinus Torvalds {
14421da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
14431da177e4SLinus Torvalds 	/*
14441da177e4SLinus Torvalds 	 *	1003.1g - break association.
14451da177e4SLinus Torvalds 	 */
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
1448c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
1449c720c7e8SEric Dumazet 	inet->inet_dport = 0;
1450bdeab991STom Herbert 	sock_rps_reset_rxhash(sk);
14511da177e4SLinus Torvalds 	sk->sk_bound_dev_if = 0;
14521da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
14531da177e4SLinus Torvalds 		inet_reset_saddr(sk);
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
14561da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1457c720c7e8SEric Dumazet 		inet->inet_sport = 0;
14581da177e4SLinus Torvalds 	}
14591da177e4SLinus Torvalds 	sk_dst_reset(sk);
14601da177e4SLinus Torvalds 	return 0;
14611da177e4SLinus Torvalds }
1462c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect);
14631da177e4SLinus Torvalds 
1464645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk)
1465645ca708SEric Dumazet {
1466723b4610SEric Dumazet 	if (sk_hashed(sk)) {
1467645ca708SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1468512615b6SEric Dumazet 		struct udp_hslot *hslot, *hslot2;
1469512615b6SEric Dumazet 
1470512615b6SEric Dumazet 		hslot  = udp_hashslot(udptable, sock_net(sk),
1471d4cada4aSEric Dumazet 				      udp_sk(sk)->udp_port_hash);
1472512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1473645ca708SEric Dumazet 
1474c8db3fecSEric Dumazet 		spin_lock_bh(&hslot->lock);
1475e32ea7e7SCraig Gallek 		if (rcu_access_pointer(sk->sk_reuseport_cb))
1476e32ea7e7SCraig Gallek 			reuseport_detach_sock(sk);
147788ab1932SEric Dumazet 		if (sk_nulls_del_node_init_rcu(sk)) {
1478fdcc8aa9SEric Dumazet 			hslot->count--;
1479c720c7e8SEric Dumazet 			inet_sk(sk)->inet_num = 0;
1480645ca708SEric Dumazet 			sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1481512615b6SEric Dumazet 
1482512615b6SEric Dumazet 			spin_lock(&hslot2->lock);
1483512615b6SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1484512615b6SEric Dumazet 			hslot2->count--;
1485512615b6SEric Dumazet 			spin_unlock(&hslot2->lock);
1486645ca708SEric Dumazet 		}
1487c8db3fecSEric Dumazet 		spin_unlock_bh(&hslot->lock);
1488645ca708SEric Dumazet 	}
1489723b4610SEric Dumazet }
1490645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash);
1491645ca708SEric Dumazet 
1492719f8358SEric Dumazet /*
1493719f8358SEric Dumazet  * inet_rcv_saddr was changed, we must rehash secondary hash
1494719f8358SEric Dumazet  */
1495719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash)
1496719f8358SEric Dumazet {
1497719f8358SEric Dumazet 	if (sk_hashed(sk)) {
1498719f8358SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1499719f8358SEric Dumazet 		struct udp_hslot *hslot, *hslot2, *nhslot2;
1500719f8358SEric Dumazet 
1501719f8358SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1502719f8358SEric Dumazet 		nhslot2 = udp_hashslot2(udptable, newhash);
1503719f8358SEric Dumazet 		udp_sk(sk)->udp_portaddr_hash = newhash;
1504e32ea7e7SCraig Gallek 
1505e32ea7e7SCraig Gallek 		if (hslot2 != nhslot2 ||
1506e32ea7e7SCraig Gallek 		    rcu_access_pointer(sk->sk_reuseport_cb)) {
1507719f8358SEric Dumazet 			hslot = udp_hashslot(udptable, sock_net(sk),
1508719f8358SEric Dumazet 					     udp_sk(sk)->udp_port_hash);
1509719f8358SEric Dumazet 			/* we must lock primary chain too */
1510719f8358SEric Dumazet 			spin_lock_bh(&hslot->lock);
1511e32ea7e7SCraig Gallek 			if (rcu_access_pointer(sk->sk_reuseport_cb))
1512e32ea7e7SCraig Gallek 				reuseport_detach_sock(sk);
1513719f8358SEric Dumazet 
1514e32ea7e7SCraig Gallek 			if (hslot2 != nhslot2) {
1515719f8358SEric Dumazet 				spin_lock(&hslot2->lock);
1516719f8358SEric Dumazet 				hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1517719f8358SEric Dumazet 				hslot2->count--;
1518719f8358SEric Dumazet 				spin_unlock(&hslot2->lock);
1519719f8358SEric Dumazet 
1520719f8358SEric Dumazet 				spin_lock(&nhslot2->lock);
1521719f8358SEric Dumazet 				hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
1522719f8358SEric Dumazet 							 &nhslot2->head);
1523719f8358SEric Dumazet 				nhslot2->count++;
1524719f8358SEric Dumazet 				spin_unlock(&nhslot2->lock);
1525e32ea7e7SCraig Gallek 			}
1526719f8358SEric Dumazet 
1527719f8358SEric Dumazet 			spin_unlock_bh(&hslot->lock);
1528719f8358SEric Dumazet 		}
1529719f8358SEric Dumazet 	}
1530719f8358SEric Dumazet }
1531719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash);
1532719f8358SEric Dumazet 
1533719f8358SEric Dumazet static void udp_v4_rehash(struct sock *sk)
1534719f8358SEric Dumazet {
1535719f8358SEric Dumazet 	u16 new_hash = udp4_portaddr_hash(sock_net(sk),
1536719f8358SEric Dumazet 					  inet_sk(sk)->inet_rcv_saddr,
1537719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
1538719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
1539719f8358SEric Dumazet }
1540719f8358SEric Dumazet 
154193821778SHerbert Xu static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
154293821778SHerbert Xu {
1543fec5e652STom Herbert 	int rc;
154493821778SHerbert Xu 
1545005ec974SShawn Bohrer 	if (inet_sk(sk)->inet_daddr) {
1546bdeab991STom Herbert 		sock_rps_save_rxhash(sk, skb);
1547005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
15482c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
1549005ec974SShawn Bohrer 	}
1550fec5e652STom Herbert 
1551d826eb14SEric Dumazet 	rc = sock_queue_rcv_skb(sk, skb);
1552766e9037SEric Dumazet 	if (rc < 0) {
1553766e9037SEric Dumazet 		int is_udplite = IS_UDPLITE(sk);
1554766e9037SEric Dumazet 
155593821778SHerbert Xu 		/* Note that an ENOMEM error is charged twice */
1556766e9037SEric Dumazet 		if (rc == -ENOMEM)
155793821778SHerbert Xu 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
155893821778SHerbert Xu 					 is_udplite);
1559766e9037SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1560766e9037SEric Dumazet 		kfree_skb(skb);
1561296f7ea7SSatoru Moriya 		trace_udp_fail_queue_rcv_skb(rc, sk);
1562766e9037SEric Dumazet 		return -1;
156393821778SHerbert Xu 	}
156493821778SHerbert Xu 
156593821778SHerbert Xu 	return 0;
156693821778SHerbert Xu 
156793821778SHerbert Xu }
156893821778SHerbert Xu 
1569447167bfSEric Dumazet static struct static_key udp_encap_needed __read_mostly;
1570447167bfSEric Dumazet void udp_encap_enable(void)
1571447167bfSEric Dumazet {
1572447167bfSEric Dumazet 	if (!static_key_enabled(&udp_encap_needed))
1573447167bfSEric Dumazet 		static_key_slow_inc(&udp_encap_needed);
1574447167bfSEric Dumazet }
1575447167bfSEric Dumazet EXPORT_SYMBOL(udp_encap_enable);
1576447167bfSEric Dumazet 
1577db8dac20SDavid S. Miller /* returns:
1578db8dac20SDavid S. Miller  *  -1: error
1579db8dac20SDavid S. Miller  *   0: success
1580db8dac20SDavid S. Miller  *  >0: "udp encap" protocol resubmission
1581db8dac20SDavid S. Miller  *
1582db8dac20SDavid S. Miller  * Note that in the success and error cases, the skb is assumed to
1583db8dac20SDavid S. Miller  * have either been requeued or freed.
1584db8dac20SDavid S. Miller  */
1585db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1586db8dac20SDavid S. Miller {
1587db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1588db8dac20SDavid S. Miller 	int rc;
1589db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1590db8dac20SDavid S. Miller 
1591db8dac20SDavid S. Miller 	/*
1592db8dac20SDavid S. Miller 	 *	Charge it to the socket, dropping if the queue is full.
1593db8dac20SDavid S. Miller 	 */
1594db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1595db8dac20SDavid S. Miller 		goto drop;
1596db8dac20SDavid S. Miller 	nf_reset(skb);
1597db8dac20SDavid S. Miller 
1598447167bfSEric Dumazet 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
15990ad92ad0SEric Dumazet 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
16000ad92ad0SEric Dumazet 
1601db8dac20SDavid S. Miller 		/*
1602db8dac20SDavid S. Miller 		 * This is an encapsulation socket so pass the skb to
1603db8dac20SDavid S. Miller 		 * the socket's udp_encap_rcv() hook. Otherwise, just
1604db8dac20SDavid S. Miller 		 * fall through and pass this up the UDP socket.
1605db8dac20SDavid S. Miller 		 * up->encap_rcv() returns the following value:
1606db8dac20SDavid S. Miller 		 * =0 if skb was successfully passed to the encap
1607db8dac20SDavid S. Miller 		 *    handler or was discarded by it.
1608db8dac20SDavid S. Miller 		 * >0 if skb should be passed on to UDP.
1609db8dac20SDavid S. Miller 		 * <0 if skb should be resubmitted as proto -N
1610db8dac20SDavid S. Miller 		 */
1611db8dac20SDavid S. Miller 
1612db8dac20SDavid S. Miller 		/* if we're overly short, let UDP handle it */
16130ad92ad0SEric Dumazet 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
161400db4124SIan Morris 		if (skb->len > sizeof(struct udphdr) && encap_rcv) {
1615db8dac20SDavid S. Miller 			int ret;
1616db8dac20SDavid S. Miller 
16170a80966bSTom Herbert 			/* Verify checksum before giving to encap */
16180a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
16190a80966bSTom Herbert 				goto csum_error;
16200a80966bSTom Herbert 
16210ad92ad0SEric Dumazet 			ret = encap_rcv(sk, skb);
1622db8dac20SDavid S. Miller 			if (ret <= 0) {
16230283328eSPavel Emelyanov 				UDP_INC_STATS_BH(sock_net(sk),
16240283328eSPavel Emelyanov 						 UDP_MIB_INDATAGRAMS,
1625db8dac20SDavid S. Miller 						 is_udplite);
1626db8dac20SDavid S. Miller 				return -ret;
1627db8dac20SDavid S. Miller 			}
1628db8dac20SDavid S. Miller 		}
1629db8dac20SDavid S. Miller 
1630db8dac20SDavid S. Miller 		/* FALLTHROUGH -- it's a UDP Packet */
1631db8dac20SDavid S. Miller 	}
1632db8dac20SDavid S. Miller 
1633db8dac20SDavid S. Miller 	/*
1634db8dac20SDavid S. Miller 	 * 	UDP-Lite specific tests, ignored on UDP sockets
1635db8dac20SDavid S. Miller 	 */
1636db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
1637db8dac20SDavid S. Miller 
1638db8dac20SDavid S. Miller 		/*
1639db8dac20SDavid S. Miller 		 * MIB statistics other than incrementing the error count are
1640db8dac20SDavid S. Miller 		 * disabled for the following two types of errors: these depend
1641db8dac20SDavid S. Miller 		 * on the application settings, not on the functioning of the
1642db8dac20SDavid S. Miller 		 * protocol stack as such.
1643db8dac20SDavid S. Miller 		 *
1644db8dac20SDavid S. Miller 		 * RFC 3828 here recommends (sec 3.3): "There should also be a
1645db8dac20SDavid S. Miller 		 * way ... to ... at least let the receiving application block
1646db8dac20SDavid S. Miller 		 * delivery of packets with coverage values less than a value
1647db8dac20SDavid S. Miller 		 * provided by the application."
1648db8dac20SDavid S. Miller 		 */
1649db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
1650ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n",
1651db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
1652db8dac20SDavid S. Miller 			goto drop;
1653db8dac20SDavid S. Miller 		}
1654db8dac20SDavid S. Miller 		/* The next case involves violating the min. coverage requested
1655db8dac20SDavid S. Miller 		 * by the receiver. This is subtle: if receiver wants x and x is
1656db8dac20SDavid S. Miller 		 * greater than the buffersize/MTU then receiver will complain
1657db8dac20SDavid S. Miller 		 * that it wants x while sender emits packets of smaller size y.
1658db8dac20SDavid S. Miller 		 * Therefore the above ...()->partial_cov statement is essential.
1659db8dac20SDavid S. Miller 		 */
1660db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
1661ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n",
1662db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
1663db8dac20SDavid S. Miller 			goto drop;
1664db8dac20SDavid S. Miller 		}
1665db8dac20SDavid S. Miller 	}
1666db8dac20SDavid S. Miller 
166733d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
166833d480ceSEric Dumazet 	    udp_lib_checksum_complete(skb))
16696a5dc9e5SEric Dumazet 		goto csum_error;
1670db8dac20SDavid S. Miller 
1671274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
16723e215c8dSJames M Leddy 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
16733e215c8dSJames M Leddy 				 is_udplite);
1674c377411fSEric Dumazet 		goto drop;
16753e215c8dSJames M Leddy 	}
1676c377411fSEric Dumazet 
167793821778SHerbert Xu 	rc = 0;
1678db8dac20SDavid S. Miller 
1679fbf8866dSShawn Bohrer 	ipv4_pktinfo_prepare(sk, skb);
168093821778SHerbert Xu 	bh_lock_sock(sk);
168193821778SHerbert Xu 	if (!sock_owned_by_user(sk))
168293821778SHerbert Xu 		rc = __udp_queue_rcv_skb(sk, skb);
1683f545a38fSEric Dumazet 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
168455349790SZhu Yi 		bh_unlock_sock(sk);
168555349790SZhu Yi 		goto drop;
168655349790SZhu Yi 	}
168793821778SHerbert Xu 	bh_unlock_sock(sk);
168893821778SHerbert Xu 
168993821778SHerbert Xu 	return rc;
1690db8dac20SDavid S. Miller 
16916a5dc9e5SEric Dumazet csum_error:
16926a5dc9e5SEric Dumazet 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1693db8dac20SDavid S. Miller drop:
16940283328eSPavel Emelyanov 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
16958edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
1696db8dac20SDavid S. Miller 	kfree_skb(skb);
1697db8dac20SDavid S. Miller 	return -1;
1698db8dac20SDavid S. Miller }
1699db8dac20SDavid S. Miller 
17001240d137SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
17011240d137SEric Dumazet 			struct sk_buff *skb, unsigned int final)
17021240d137SEric Dumazet {
17031240d137SEric Dumazet 	unsigned int i;
17041240d137SEric Dumazet 	struct sk_buff *skb1 = NULL;
1705f6b8f32cSEric Dumazet 	struct sock *sk;
17061240d137SEric Dumazet 
17071240d137SEric Dumazet 	for (i = 0; i < count; i++) {
1708f6b8f32cSEric Dumazet 		sk = stack[i];
170951456b29SIan Morris 		if (likely(!skb1))
17101240d137SEric Dumazet 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
17111240d137SEric Dumazet 
1712f6b8f32cSEric Dumazet 		if (!skb1) {
1713f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
1714f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
1715f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1716f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
1717f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1718f6b8f32cSEric Dumazet 		}
1719f6b8f32cSEric Dumazet 
1720f6b8f32cSEric Dumazet 		if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0)
17211240d137SEric Dumazet 			skb1 = NULL;
17222dc41cffSDavid Held 
17232dc41cffSDavid Held 		sock_put(sk);
17241240d137SEric Dumazet 	}
17251240d137SEric Dumazet 	if (unlikely(skb1))
17261240d137SEric Dumazet 		kfree_skb(skb1);
17271240d137SEric Dumazet }
17281240d137SEric Dumazet 
172997502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock
1730e47eb5dfSEric Dumazet  * For UDP, we use xchg() to guard against concurrent changes.
173197502231SEric Dumazet  */
173297502231SEric Dumazet static void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
1733421b3885SShawn Bohrer {
173497502231SEric Dumazet 	struct dst_entry *old;
1735421b3885SShawn Bohrer 
1736421b3885SShawn Bohrer 	dst_hold(dst);
1737e47eb5dfSEric Dumazet 	old = xchg(&sk->sk_rx_dst, dst);
173897502231SEric Dumazet 	dst_release(old);
173997502231SEric Dumazet }
1740421b3885SShawn Bohrer 
1741db8dac20SDavid S. Miller /*
1742db8dac20SDavid S. Miller  *	Multicasts and broadcasts go to each listener.
1743db8dac20SDavid S. Miller  *
17441240d137SEric Dumazet  *	Note: called only from the BH handler context.
1745db8dac20SDavid S. Miller  */
1746e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
1747db8dac20SDavid S. Miller 				    struct udphdr  *uh,
1748db8dac20SDavid S. Miller 				    __be32 saddr, __be32 daddr,
174936cbb245SRick Jones 				    struct udp_table *udptable,
175036cbb245SRick Jones 				    int proto)
1751db8dac20SDavid S. Miller {
17521240d137SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
17535cf3d461SDavid Held 	struct hlist_nulls_node *node;
17545cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
17555cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
17565cf3d461SDavid Held 	int dif = skb->dev->ifindex;
17572dc41cffSDavid Held 	unsigned int count = 0, offset = offsetof(typeof(*sk), sk_nulls_node);
17582dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
175936cbb245SRick Jones 	bool inner_flushed = false;
17602dc41cffSDavid Held 
17612dc41cffSDavid Held 	if (use_hash2) {
17622dc41cffSDavid Held 		hash2_any = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum) &
17632dc41cffSDavid Held 			    udp_table.mask;
17642dc41cffSDavid Held 		hash2 = udp4_portaddr_hash(net, daddr, hnum) & udp_table.mask;
17652dc41cffSDavid Held start_lookup:
17662dc41cffSDavid Held 		hslot = &udp_table.hash2[hash2];
17672dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
17682dc41cffSDavid Held 	}
1769db8dac20SDavid S. Miller 
1770645ca708SEric Dumazet 	spin_lock(&hslot->lock);
17712dc41cffSDavid Held 	sk_nulls_for_each_entry_offset(sk, node, &hslot->head, offset) {
17725cf3d461SDavid Held 		if (__udp_is_mcast_sock(net, sk,
17735cf3d461SDavid Held 					uh->dest, daddr,
17745cf3d461SDavid Held 					uh->source, saddr,
17755cf3d461SDavid Held 					dif, hnum)) {
17761240d137SEric Dumazet 			if (unlikely(count == ARRAY_SIZE(stack))) {
17771240d137SEric Dumazet 				flush_stack(stack, count, skb, ~0);
177836cbb245SRick Jones 				inner_flushed = true;
17791240d137SEric Dumazet 				count = 0;
1780db8dac20SDavid S. Miller 			}
17815cf3d461SDavid Held 			stack[count++] = sk;
17822dc41cffSDavid Held 			sock_hold(sk);
17835cf3d461SDavid Held 		}
17841240d137SEric Dumazet 	}
17851240d137SEric Dumazet 
1786645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
17871240d137SEric Dumazet 
17882dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
17892dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
17902dc41cffSDavid Held 		hash2 = hash2_any;
17912dc41cffSDavid Held 		goto start_lookup;
17922dc41cffSDavid Held 	}
17932dc41cffSDavid Held 
17941240d137SEric Dumazet 	/*
17951240d137SEric Dumazet 	 * do the slow work with no lock held
17961240d137SEric Dumazet 	 */
17971240d137SEric Dumazet 	if (count) {
17981240d137SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
17991240d137SEric Dumazet 	} else {
180036cbb245SRick Jones 		if (!inner_flushed)
180136cbb245SRick Jones 			UDP_INC_STATS_BH(net, UDP_MIB_IGNOREDMULTI,
180236cbb245SRick Jones 					 proto == IPPROTO_UDPLITE);
180336cbb245SRick Jones 		consume_skb(skb);
18041240d137SEric Dumazet 	}
1805db8dac20SDavid S. Miller 	return 0;
1806db8dac20SDavid S. Miller }
1807db8dac20SDavid S. Miller 
1808db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success),
1809db8dac20SDavid S. Miller  * CHECKSUM_UNNECESSARY means, that no more checks are required.
1810db8dac20SDavid S. Miller  * Otherwise, csum completion requires chacksumming packet body,
1811db8dac20SDavid S. Miller  * including udp header and folding it to skb->csum.
1812db8dac20SDavid S. Miller  */
1813db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1814db8dac20SDavid S. Miller 				 int proto)
1815db8dac20SDavid S. Miller {
1816db8dac20SDavid S. Miller 	int err;
1817db8dac20SDavid S. Miller 
1818db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
1819db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
1820db8dac20SDavid S. Miller 
1821db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
1822db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
1823db8dac20SDavid S. Miller 		if (err)
1824db8dac20SDavid S. Miller 			return err;
1825db8dac20SDavid S. Miller 	}
1826db8dac20SDavid S. Miller 
1827ed70fcfcSTom Herbert 	return skb_checksum_init_zero_check(skb, proto, uh->check,
1828ed70fcfcSTom Herbert 					    inet_compute_pseudo);
1829db8dac20SDavid S. Miller }
1830db8dac20SDavid S. Miller 
1831db8dac20SDavid S. Miller /*
1832db8dac20SDavid S. Miller  *	All we need to do is get the socket, and then do a checksum.
1833db8dac20SDavid S. Miller  */
1834db8dac20SDavid S. Miller 
1835645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1836db8dac20SDavid S. Miller 		   int proto)
1837db8dac20SDavid S. Miller {
1838db8dac20SDavid S. Miller 	struct sock *sk;
18397b5e56f9SJesper Dangaard Brouer 	struct udphdr *uh;
1840db8dac20SDavid S. Miller 	unsigned short ulen;
1841adf30907SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
18422783ef23SJesper Dangaard Brouer 	__be32 saddr, daddr;
18430283328eSPavel Emelyanov 	struct net *net = dev_net(skb->dev);
1844db8dac20SDavid S. Miller 
1845db8dac20SDavid S. Miller 	/*
1846db8dac20SDavid S. Miller 	 *  Validate the packet.
1847db8dac20SDavid S. Miller 	 */
1848db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1849db8dac20SDavid S. Miller 		goto drop;		/* No space for header. */
1850db8dac20SDavid S. Miller 
18517b5e56f9SJesper Dangaard Brouer 	uh   = udp_hdr(skb);
1852db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
1853ccc2d97cSBjørn Mork 	saddr = ip_hdr(skb)->saddr;
1854ccc2d97cSBjørn Mork 	daddr = ip_hdr(skb)->daddr;
1855ccc2d97cSBjørn Mork 
1856db8dac20SDavid S. Miller 	if (ulen > skb->len)
1857db8dac20SDavid S. Miller 		goto short_packet;
1858db8dac20SDavid S. Miller 
1859db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
1860db8dac20SDavid S. Miller 		/* UDP validates ulen. */
1861db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1862db8dac20SDavid S. Miller 			goto short_packet;
1863db8dac20SDavid S. Miller 		uh = udp_hdr(skb);
1864db8dac20SDavid S. Miller 	}
1865db8dac20SDavid S. Miller 
1866db8dac20SDavid S. Miller 	if (udp4_csum_init(skb, uh, proto))
1867db8dac20SDavid S. Miller 		goto csum_error;
1868db8dac20SDavid S. Miller 
18698afdd99aSEric Dumazet 	sk = skb_steal_sock(skb);
18708afdd99aSEric Dumazet 	if (sk) {
187197502231SEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
1872421b3885SShawn Bohrer 		int ret;
1873421b3885SShawn Bohrer 
187497502231SEric Dumazet 		if (unlikely(sk->sk_rx_dst != dst))
187597502231SEric Dumazet 			udp_sk_rx_dst_set(sk, dst);
1876421b3885SShawn Bohrer 
1877421b3885SShawn Bohrer 		ret = udp_queue_rcv_skb(sk, skb);
18788afdd99aSEric Dumazet 		sock_put(sk);
1879421b3885SShawn Bohrer 		/* a return value > 0 means to resubmit the input, but
1880421b3885SShawn Bohrer 		 * it wants the return to be -protocol, or 0
1881421b3885SShawn Bohrer 		 */
1882421b3885SShawn Bohrer 		if (ret > 0)
1883421b3885SShawn Bohrer 			return -ret;
1884421b3885SShawn Bohrer 		return 0;
1885c18450a5SFabian Frederick 	}
1886c18450a5SFabian Frederick 
1887db8dac20SDavid S. Miller 	if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1888e3163493SPavel Emelyanov 		return __udp4_lib_mcast_deliver(net, skb, uh,
188936cbb245SRick Jones 						saddr, daddr, udptable, proto);
1890db8dac20SDavid S. Miller 
1891607c4aafSKOVACS Krisztian 	sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
189200db4124SIan Morris 	if (sk) {
1893a5b50476SEliezer Tamir 		int ret;
1894a5b50476SEliezer Tamir 
1895224d019cSTom Herbert 		if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
18962abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
18972abb7cdcSTom Herbert 						 inet_compute_pseudo);
18982abb7cdcSTom Herbert 
1899a5b50476SEliezer Tamir 		ret = udp_queue_rcv_skb(sk, skb);
1900db8dac20SDavid S. Miller 		sock_put(sk);
1901db8dac20SDavid S. Miller 
1902db8dac20SDavid S. Miller 		/* a return value > 0 means to resubmit the input, but
1903db8dac20SDavid S. Miller 		 * it wants the return to be -protocol, or 0
1904db8dac20SDavid S. Miller 		 */
1905db8dac20SDavid S. Miller 		if (ret > 0)
1906db8dac20SDavid S. Miller 			return -ret;
1907db8dac20SDavid S. Miller 		return 0;
1908db8dac20SDavid S. Miller 	}
1909db8dac20SDavid S. Miller 
1910db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1911db8dac20SDavid S. Miller 		goto drop;
1912db8dac20SDavid S. Miller 	nf_reset(skb);
1913db8dac20SDavid S. Miller 
1914db8dac20SDavid S. Miller 	/* No socket. Drop packet silently, if checksum is wrong */
1915db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
1916db8dac20SDavid S. Miller 		goto csum_error;
1917db8dac20SDavid S. Miller 
19180283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
1919db8dac20SDavid S. Miller 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1920db8dac20SDavid S. Miller 
1921db8dac20SDavid S. Miller 	/*
1922db8dac20SDavid S. Miller 	 * Hmm.  We got an UDP packet to a port to which we
1923db8dac20SDavid S. Miller 	 * don't wanna listen.  Ignore it.
1924db8dac20SDavid S. Miller 	 */
1925db8dac20SDavid S. Miller 	kfree_skb(skb);
1926db8dac20SDavid S. Miller 	return 0;
1927db8dac20SDavid S. Miller 
1928db8dac20SDavid S. Miller short_packet:
1929ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
1930afd46503SJoe Perches 			    proto == IPPROTO_UDPLITE ? "Lite" : "",
1931afd46503SJoe Perches 			    &saddr, ntohs(uh->source),
1932afd46503SJoe Perches 			    ulen, skb->len,
1933afd46503SJoe Perches 			    &daddr, ntohs(uh->dest));
1934db8dac20SDavid S. Miller 	goto drop;
1935db8dac20SDavid S. Miller 
1936db8dac20SDavid S. Miller csum_error:
1937db8dac20SDavid S. Miller 	/*
1938db8dac20SDavid S. Miller 	 * RFC1122: OK.  Discards the bad packet silently (as far as
1939db8dac20SDavid S. Miller 	 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1940db8dac20SDavid S. Miller 	 */
1941ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
1942afd46503SJoe Perches 			    proto == IPPROTO_UDPLITE ? "Lite" : "",
1943afd46503SJoe Perches 			    &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
1944db8dac20SDavid S. Miller 			    ulen);
19456a5dc9e5SEric Dumazet 	UDP_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1946db8dac20SDavid S. Miller drop:
19470283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1948db8dac20SDavid S. Miller 	kfree_skb(skb);
1949db8dac20SDavid S. Miller 	return 0;
1950db8dac20SDavid S. Miller }
1951db8dac20SDavid S. Miller 
1952421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket.
1953421b3885SShawn Bohrer  * If more than one socket found returns NULL
1954421b3885SShawn Bohrer  */
1955421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
1956421b3885SShawn Bohrer 						  __be16 loc_port, __be32 loc_addr,
1957421b3885SShawn Bohrer 						  __be16 rmt_port, __be32 rmt_addr,
1958421b3885SShawn Bohrer 						  int dif)
1959421b3885SShawn Bohrer {
1960421b3885SShawn Bohrer 	struct sock *sk, *result;
1961421b3885SShawn Bohrer 	struct hlist_nulls_node *node;
1962421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
1963421b3885SShawn Bohrer 	unsigned int count, slot = udp_hashfn(net, hnum, udp_table.mask);
1964421b3885SShawn Bohrer 	struct udp_hslot *hslot = &udp_table.hash[slot];
1965421b3885SShawn Bohrer 
196663c6f81cSEric Dumazet 	/* Do not bother scanning a too big list */
196763c6f81cSEric Dumazet 	if (hslot->count > 10)
196863c6f81cSEric Dumazet 		return NULL;
196963c6f81cSEric Dumazet 
1970421b3885SShawn Bohrer 	rcu_read_lock();
1971421b3885SShawn Bohrer begin:
1972421b3885SShawn Bohrer 	count = 0;
1973421b3885SShawn Bohrer 	result = NULL;
1974421b3885SShawn Bohrer 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
1975421b3885SShawn Bohrer 		if (__udp_is_mcast_sock(net, sk,
1976421b3885SShawn Bohrer 					loc_port, loc_addr,
1977421b3885SShawn Bohrer 					rmt_port, rmt_addr,
1978421b3885SShawn Bohrer 					dif, hnum)) {
1979421b3885SShawn Bohrer 			result = sk;
1980421b3885SShawn Bohrer 			++count;
1981421b3885SShawn Bohrer 		}
1982421b3885SShawn Bohrer 	}
1983421b3885SShawn Bohrer 	/*
1984421b3885SShawn Bohrer 	 * if the nulls value we got at the end of this lookup is
1985421b3885SShawn Bohrer 	 * not the expected one, we must restart lookup.
1986421b3885SShawn Bohrer 	 * We probably met an item that was moved to another chain.
1987421b3885SShawn Bohrer 	 */
1988421b3885SShawn Bohrer 	if (get_nulls_value(node) != slot)
1989421b3885SShawn Bohrer 		goto begin;
1990421b3885SShawn Bohrer 
1991421b3885SShawn Bohrer 	if (result) {
1992421b3885SShawn Bohrer 		if (count != 1 ||
1993421b3885SShawn Bohrer 		    unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
1994421b3885SShawn Bohrer 			result = NULL;
1995f69b923aSEric Dumazet 		else if (unlikely(!__udp_is_mcast_sock(net, result,
1996421b3885SShawn Bohrer 						       loc_port, loc_addr,
1997421b3885SShawn Bohrer 						       rmt_port, rmt_addr,
1998421b3885SShawn Bohrer 						       dif, hnum))) {
1999421b3885SShawn Bohrer 			sock_put(result);
2000421b3885SShawn Bohrer 			result = NULL;
2001421b3885SShawn Bohrer 		}
2002421b3885SShawn Bohrer 	}
2003421b3885SShawn Bohrer 	rcu_read_unlock();
2004421b3885SShawn Bohrer 	return result;
2005421b3885SShawn Bohrer }
2006421b3885SShawn Bohrer 
2007421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can
2008421b3885SShawn Bohrer  * break forwarding setups.  The chains here can be long so only check
2009421b3885SShawn Bohrer  * if the first socket is an exact match and if not move on.
2010421b3885SShawn Bohrer  */
2011421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net,
2012421b3885SShawn Bohrer 					    __be16 loc_port, __be32 loc_addr,
2013421b3885SShawn Bohrer 					    __be16 rmt_port, __be32 rmt_addr,
2014421b3885SShawn Bohrer 					    int dif)
2015421b3885SShawn Bohrer {
2016421b3885SShawn Bohrer 	struct sock *sk, *result;
2017421b3885SShawn Bohrer 	struct hlist_nulls_node *node;
2018421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
2019421b3885SShawn Bohrer 	unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum);
2020421b3885SShawn Bohrer 	unsigned int slot2 = hash2 & udp_table.mask;
2021421b3885SShawn Bohrer 	struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
2022c7228317SJoe Perches 	INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
2023421b3885SShawn Bohrer 	const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
2024421b3885SShawn Bohrer 
2025421b3885SShawn Bohrer 	rcu_read_lock();
2026421b3885SShawn Bohrer 	result = NULL;
2027421b3885SShawn Bohrer 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
2028421b3885SShawn Bohrer 		if (INET_MATCH(sk, net, acookie,
2029421b3885SShawn Bohrer 			       rmt_addr, loc_addr, ports, dif))
2030421b3885SShawn Bohrer 			result = sk;
2031421b3885SShawn Bohrer 		/* Only check first socket in chain */
2032421b3885SShawn Bohrer 		break;
2033421b3885SShawn Bohrer 	}
2034421b3885SShawn Bohrer 
2035421b3885SShawn Bohrer 	if (result) {
2036421b3885SShawn Bohrer 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
2037421b3885SShawn Bohrer 			result = NULL;
2038421b3885SShawn Bohrer 		else if (unlikely(!INET_MATCH(sk, net, acookie,
2039421b3885SShawn Bohrer 					      rmt_addr, loc_addr,
2040421b3885SShawn Bohrer 					      ports, dif))) {
2041421b3885SShawn Bohrer 			sock_put(result);
2042421b3885SShawn Bohrer 			result = NULL;
2043421b3885SShawn Bohrer 		}
2044421b3885SShawn Bohrer 	}
2045421b3885SShawn Bohrer 	rcu_read_unlock();
2046421b3885SShawn Bohrer 	return result;
2047421b3885SShawn Bohrer }
2048421b3885SShawn Bohrer 
2049421b3885SShawn Bohrer void udp_v4_early_demux(struct sk_buff *skb)
2050421b3885SShawn Bohrer {
2051610438b7SEric Dumazet 	struct net *net = dev_net(skb->dev);
2052610438b7SEric Dumazet 	const struct iphdr *iph;
2053610438b7SEric Dumazet 	const struct udphdr *uh;
2054421b3885SShawn Bohrer 	struct sock *sk;
2055421b3885SShawn Bohrer 	struct dst_entry *dst;
2056421b3885SShawn Bohrer 	int dif = skb->dev->ifindex;
20576e540309SShawn Bohrer 	int ours;
2058421b3885SShawn Bohrer 
2059421b3885SShawn Bohrer 	/* validate the packet */
2060421b3885SShawn Bohrer 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
2061421b3885SShawn Bohrer 		return;
2062421b3885SShawn Bohrer 
2063610438b7SEric Dumazet 	iph = ip_hdr(skb);
2064610438b7SEric Dumazet 	uh = udp_hdr(skb);
2065610438b7SEric Dumazet 
2066421b3885SShawn Bohrer 	if (skb->pkt_type == PACKET_BROADCAST ||
20676e540309SShawn Bohrer 	    skb->pkt_type == PACKET_MULTICAST) {
20686e540309SShawn Bohrer 		struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
20696e540309SShawn Bohrer 
20706e540309SShawn Bohrer 		if (!in_dev)
20716e540309SShawn Bohrer 			return;
20726e540309SShawn Bohrer 
2073*ad0ea198SPaolo Abeni 		/* we are supposed to accept bcast packets */
2074*ad0ea198SPaolo Abeni 		if (skb->pkt_type == PACKET_MULTICAST) {
20756e540309SShawn Bohrer 			ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr,
20766e540309SShawn Bohrer 					       iph->protocol);
20776e540309SShawn Bohrer 			if (!ours)
20786e540309SShawn Bohrer 				return;
2079*ad0ea198SPaolo Abeni 		}
2080*ad0ea198SPaolo Abeni 
2081421b3885SShawn Bohrer 		sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
2082421b3885SShawn Bohrer 						   uh->source, iph->saddr, dif);
20836e540309SShawn Bohrer 	} else if (skb->pkt_type == PACKET_HOST) {
2084421b3885SShawn Bohrer 		sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
2085421b3885SShawn Bohrer 					     uh->source, iph->saddr, dif);
20866e540309SShawn Bohrer 	} else {
2087421b3885SShawn Bohrer 		return;
20886e540309SShawn Bohrer 	}
2089421b3885SShawn Bohrer 
2090421b3885SShawn Bohrer 	if (!sk)
2091421b3885SShawn Bohrer 		return;
2092421b3885SShawn Bohrer 
2093421b3885SShawn Bohrer 	skb->sk = sk;
209482eabd9eSAlexander Duyck 	skb->destructor = sock_efree;
209510e2eb87SEric Dumazet 	dst = READ_ONCE(sk->sk_rx_dst);
2096421b3885SShawn Bohrer 
2097421b3885SShawn Bohrer 	if (dst)
2098421b3885SShawn Bohrer 		dst = dst_check(dst, 0);
209910e2eb87SEric Dumazet 	if (dst) {
210010e2eb87SEric Dumazet 		/* DST_NOCACHE can not be used without taking a reference */
210110e2eb87SEric Dumazet 		if (dst->flags & DST_NOCACHE) {
210210e2eb87SEric Dumazet 			if (likely(atomic_inc_not_zero(&dst->__refcnt)))
210310e2eb87SEric Dumazet 				skb_dst_set(skb, dst);
210410e2eb87SEric Dumazet 		} else {
2105421b3885SShawn Bohrer 			skb_dst_set_noref(skb, dst);
2106421b3885SShawn Bohrer 		}
210710e2eb87SEric Dumazet 	}
210810e2eb87SEric Dumazet }
2109421b3885SShawn Bohrer 
2110db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb)
2111db8dac20SDavid S. Miller {
2112645ca708SEric Dumazet 	return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
2113db8dac20SDavid S. Miller }
2114db8dac20SDavid S. Miller 
21157d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk)
2116db8dac20SDavid S. Miller {
211744046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
21188a74ad60SEric Dumazet 	bool slow = lock_sock_fast(sk);
2119db8dac20SDavid S. Miller 	udp_flush_pending_frames(sk);
21208a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
212144046a59STom Parkin 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
212244046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
212344046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
212444046a59STom Parkin 		if (encap_destroy)
212544046a59STom Parkin 			encap_destroy(sk);
212644046a59STom Parkin 	}
2127db8dac20SDavid S. Miller }
2128db8dac20SDavid S. Miller 
21291da177e4SLinus Torvalds /*
21301da177e4SLinus Torvalds  *	Socket option code for UDP
21311da177e4SLinus Torvalds  */
21324c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname,
2133b7058842SDavid S. Miller 		       char __user *optval, unsigned int optlen,
21344c0a6cb0SGerrit Renker 		       int (*push_pending_frames)(struct sock *))
21351da177e4SLinus Torvalds {
21361da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
21371c19448cSTom Herbert 	int val, valbool;
21381da177e4SLinus Torvalds 	int err = 0;
2139b2bf1e26SWang Chen 	int is_udplite = IS_UDPLITE(sk);
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds 	if (optlen < sizeof(int))
21421da177e4SLinus Torvalds 		return -EINVAL;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
21451da177e4SLinus Torvalds 		return -EFAULT;
21461da177e4SLinus Torvalds 
21471c19448cSTom Herbert 	valbool = val ? 1 : 0;
21481c19448cSTom Herbert 
21491da177e4SLinus Torvalds 	switch (optname) {
21501da177e4SLinus Torvalds 	case UDP_CORK:
21511da177e4SLinus Torvalds 		if (val != 0) {
21521da177e4SLinus Torvalds 			up->corkflag = 1;
21531da177e4SLinus Torvalds 		} else {
21541da177e4SLinus Torvalds 			up->corkflag = 0;
21551da177e4SLinus Torvalds 			lock_sock(sk);
21564243cdc2SJoe Perches 			push_pending_frames(sk);
21571da177e4SLinus Torvalds 			release_sock(sk);
21581da177e4SLinus Torvalds 		}
21591da177e4SLinus Torvalds 		break;
21601da177e4SLinus Torvalds 
21611da177e4SLinus Torvalds 	case UDP_ENCAP:
21621da177e4SLinus Torvalds 		switch (val) {
21631da177e4SLinus Torvalds 		case 0:
21641da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP:
21651da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP_NON_IKE:
2166067b207bSJames Chapman 			up->encap_rcv = xfrm4_udp_encap_rcv;
2167067b207bSJames Chapman 			/* FALLTHROUGH */
2168342f0234SJames Chapman 		case UDP_ENCAP_L2TPINUDP:
21691da177e4SLinus Torvalds 			up->encap_type = val;
2170447167bfSEric Dumazet 			udp_encap_enable();
21711da177e4SLinus Torvalds 			break;
21721da177e4SLinus Torvalds 		default:
21731da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
21741da177e4SLinus Torvalds 			break;
21751da177e4SLinus Torvalds 		}
21761da177e4SLinus Torvalds 		break;
21771da177e4SLinus Torvalds 
21781c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
21791c19448cSTom Herbert 		up->no_check6_tx = valbool;
21801c19448cSTom Herbert 		break;
21811c19448cSTom Herbert 
21821c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
21831c19448cSTom Herbert 		up->no_check6_rx = valbool;
21841c19448cSTom Herbert 		break;
21851c19448cSTom Herbert 
2186ba4e58ecSGerrit Renker 	/*
2187ba4e58ecSGerrit Renker 	 * 	UDP-Lite's partial checksum coverage (RFC 3828).
2188ba4e58ecSGerrit Renker 	 */
2189ba4e58ecSGerrit Renker 	/* The sender sets actual checksum coverage length via this option.
2190ba4e58ecSGerrit Renker 	 * The case coverage > packet length is handled by send module. */
2191ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2192b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2193ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2194ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
2195ba4e58ecSGerrit Renker 			val = 8;
21964be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
21974be929beSAlexey Dobriyan 			val = USHRT_MAX;
2198ba4e58ecSGerrit Renker 		up->pcslen = val;
2199ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_SEND_CC;
2200ba4e58ecSGerrit Renker 		break;
2201ba4e58ecSGerrit Renker 
2202ba4e58ecSGerrit Renker 	/* The receiver specifies a minimum checksum coverage value. To make
2203ba4e58ecSGerrit Renker 	 * sense, this should be set to at least 8 (as done below). If zero is
2204ba4e58ecSGerrit Renker 	 * used, this again means full checksum coverage.                     */
2205ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2206b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2207ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2208ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Avoid silly minimal values.       */
2209ba4e58ecSGerrit Renker 			val = 8;
22104be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
22114be929beSAlexey Dobriyan 			val = USHRT_MAX;
2212ba4e58ecSGerrit Renker 		up->pcrlen = val;
2213ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_RECV_CC;
2214ba4e58ecSGerrit Renker 		break;
2215ba4e58ecSGerrit Renker 
22161da177e4SLinus Torvalds 	default:
22171da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
22181da177e4SLinus Torvalds 		break;
22196516c655SStephen Hemminger 	}
22201da177e4SLinus Torvalds 
22211da177e4SLinus Torvalds 	return err;
22221da177e4SLinus Torvalds }
2223c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt);
22241da177e4SLinus Torvalds 
2225db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname,
2226b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
2227db8dac20SDavid S. Miller {
2228db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2229db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2230db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2231db8dac20SDavid S. Miller 	return ip_setsockopt(sk, level, optname, optval, optlen);
2232db8dac20SDavid S. Miller }
2233db8dac20SDavid S. Miller 
2234db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2235db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname,
2236b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
2237db8dac20SDavid S. Miller {
2238db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2239db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2240db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2241db8dac20SDavid S. Miller 	return compat_ip_setsockopt(sk, level, optname, optval, optlen);
2242db8dac20SDavid S. Miller }
2243db8dac20SDavid S. Miller #endif
2244db8dac20SDavid S. Miller 
22454c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname,
22461da177e4SLinus Torvalds 		       char __user *optval, int __user *optlen)
22471da177e4SLinus Torvalds {
22481da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
22491da177e4SLinus Torvalds 	int val, len;
22501da177e4SLinus Torvalds 
22511da177e4SLinus Torvalds 	if (get_user(len, optlen))
22521da177e4SLinus Torvalds 		return -EFAULT;
22531da177e4SLinus Torvalds 
22541da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 	if (len < 0)
22571da177e4SLinus Torvalds 		return -EINVAL;
22581da177e4SLinus Torvalds 
22591da177e4SLinus Torvalds 	switch (optname) {
22601da177e4SLinus Torvalds 	case UDP_CORK:
22611da177e4SLinus Torvalds 		val = up->corkflag;
22621da177e4SLinus Torvalds 		break;
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds 	case UDP_ENCAP:
22651da177e4SLinus Torvalds 		val = up->encap_type;
22661da177e4SLinus Torvalds 		break;
22671da177e4SLinus Torvalds 
22681c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
22691c19448cSTom Herbert 		val = up->no_check6_tx;
22701c19448cSTom Herbert 		break;
22711c19448cSTom Herbert 
22721c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
22731c19448cSTom Herbert 		val = up->no_check6_rx;
22741c19448cSTom Herbert 		break;
22751c19448cSTom Herbert 
2276ba4e58ecSGerrit Renker 	/* The following two cannot be changed on UDP sockets, the return is
2277ba4e58ecSGerrit Renker 	 * always 0 (which corresponds to the full checksum coverage of UDP). */
2278ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2279ba4e58ecSGerrit Renker 		val = up->pcslen;
2280ba4e58ecSGerrit Renker 		break;
2281ba4e58ecSGerrit Renker 
2282ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2283ba4e58ecSGerrit Renker 		val = up->pcrlen;
2284ba4e58ecSGerrit Renker 		break;
2285ba4e58ecSGerrit Renker 
22861da177e4SLinus Torvalds 	default:
22871da177e4SLinus Torvalds 		return -ENOPROTOOPT;
22886516c655SStephen Hemminger 	}
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds 	if (put_user(len, optlen))
22911da177e4SLinus Torvalds 		return -EFAULT;
22921da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
22931da177e4SLinus Torvalds 		return -EFAULT;
22941da177e4SLinus Torvalds 	return 0;
22951da177e4SLinus Torvalds }
2296c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt);
22971da177e4SLinus Torvalds 
2298db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname,
2299db8dac20SDavid S. Miller 		   char __user *optval, int __user *optlen)
2300db8dac20SDavid S. Miller {
2301db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2302db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2303db8dac20SDavid S. Miller 	return ip_getsockopt(sk, level, optname, optval, optlen);
2304db8dac20SDavid S. Miller }
2305db8dac20SDavid S. Miller 
2306db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2307db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname,
2308db8dac20SDavid S. Miller 				 char __user *optval, int __user *optlen)
2309db8dac20SDavid S. Miller {
2310db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2311db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2312db8dac20SDavid S. Miller 	return compat_ip_getsockopt(sk, level, optname, optval, optlen);
2313db8dac20SDavid S. Miller }
2314db8dac20SDavid S. Miller #endif
23151da177e4SLinus Torvalds /**
23161da177e4SLinus Torvalds  * 	udp_poll - wait for a UDP event.
23171da177e4SLinus Torvalds  *	@file - file struct
23181da177e4SLinus Torvalds  *	@sock - socket
23191da177e4SLinus Torvalds  *	@wait - poll table
23201da177e4SLinus Torvalds  *
23211da177e4SLinus Torvalds  *	This is same as datagram poll, except for the special case of
23221da177e4SLinus Torvalds  *	blocking sockets. If application is using a blocking fd
23231da177e4SLinus Torvalds  *	and a packet with checksum error is in the queue;
23241da177e4SLinus Torvalds  *	then it could get return from select indicating data available
23251da177e4SLinus Torvalds  *	but then block when reading it. Add special case code
23261da177e4SLinus Torvalds  *	to work around these arguably broken applications.
23271da177e4SLinus Torvalds  */
23281da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
23291da177e4SLinus Torvalds {
23301da177e4SLinus Torvalds 	unsigned int mask = datagram_poll(file, sock, wait);
23311da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
23321da177e4SLinus Torvalds 
2333c3f1dbafSDavid Majnemer 	sock_rps_record_flow(sk);
2334c3f1dbafSDavid Majnemer 
23351da177e4SLinus Torvalds 	/* Check for false positives due to checksum errors */
233685584672SEric Dumazet 	if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
233785584672SEric Dumazet 	    !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
23381da177e4SLinus Torvalds 		mask &= ~(POLLIN | POLLRDNORM);
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	return mask;
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds }
2343c482c568SEric Dumazet EXPORT_SYMBOL(udp_poll);
23441da177e4SLinus Torvalds 
2345db8dac20SDavid S. Miller struct proto udp_prot = {
2346db8dac20SDavid S. Miller 	.name		   = "UDP",
2347db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
2348db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
2349db8dac20SDavid S. Miller 	.connect	   = ip4_datagram_connect,
2350db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
2351db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
2352db8dac20SDavid S. Miller 	.destroy	   = udp_destroy_sock,
2353db8dac20SDavid S. Miller 	.setsockopt	   = udp_setsockopt,
2354db8dac20SDavid S. Miller 	.getsockopt	   = udp_getsockopt,
2355db8dac20SDavid S. Miller 	.sendmsg	   = udp_sendmsg,
2356db8dac20SDavid S. Miller 	.recvmsg	   = udp_recvmsg,
2357db8dac20SDavid S. Miller 	.sendpage	   = udp_sendpage,
235893821778SHerbert Xu 	.backlog_rcv	   = __udp_queue_rcv_skb,
23598141ed9fSSteffen Klassert 	.release_cb	   = ip4_datagram_release_cb,
2360db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
2361db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
2362719f8358SEric Dumazet 	.rehash		   = udp_v4_rehash,
2363db8dac20SDavid S. Miller 	.get_port	   = udp_v4_get_port,
2364db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
2365db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
2366db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
2367db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
2368db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp_sock),
2369271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
2370645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
2371db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2372db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udp_setsockopt,
2373db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udp_getsockopt,
2374db8dac20SDavid S. Miller #endif
2375fcbdf09dSOctavian Purdila 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
2376db8dac20SDavid S. Miller };
2377c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot);
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
23801da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
23811da177e4SLinus Torvalds 
2382645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start)
23831da177e4SLinus Torvalds {
23841da177e4SLinus Torvalds 	struct sock *sk;
23851da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
23866f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
23871da177e4SLinus Torvalds 
2388f86dcc5aSEric Dumazet 	for (state->bucket = start; state->bucket <= state->udp_table->mask;
2389f86dcc5aSEric Dumazet 	     ++state->bucket) {
239088ab1932SEric Dumazet 		struct hlist_nulls_node *node;
2391645ca708SEric Dumazet 		struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
2392f86dcc5aSEric Dumazet 
2393f86dcc5aSEric Dumazet 		if (hlist_nulls_empty(&hslot->head))
2394f86dcc5aSEric Dumazet 			continue;
2395f86dcc5aSEric Dumazet 
2396645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
239788ab1932SEric Dumazet 		sk_nulls_for_each(sk, node, &hslot->head) {
2398878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
2399a91275efSDaniel Lezcano 				continue;
24001da177e4SLinus Torvalds 			if (sk->sk_family == state->family)
24011da177e4SLinus Torvalds 				goto found;
24021da177e4SLinus Torvalds 		}
2403645ca708SEric Dumazet 		spin_unlock_bh(&hslot->lock);
24041da177e4SLinus Torvalds 	}
24051da177e4SLinus Torvalds 	sk = NULL;
24061da177e4SLinus Torvalds found:
24071da177e4SLinus Torvalds 	return sk;
24081da177e4SLinus Torvalds }
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
24111da177e4SLinus Torvalds {
24121da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
24136f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	do {
241688ab1932SEric Dumazet 		sk = sk_nulls_next(sk);
2417878628fbSYOSHIFUJI Hideaki 	} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
24181da177e4SLinus Torvalds 
2419645ca708SEric Dumazet 	if (!sk) {
2420f86dcc5aSEric Dumazet 		if (state->bucket <= state->udp_table->mask)
2421f52b5054SEric Dumazet 			spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
2422645ca708SEric Dumazet 		return udp_get_first(seq, state->bucket + 1);
24231da177e4SLinus Torvalds 	}
24241da177e4SLinus Torvalds 	return sk;
24251da177e4SLinus Torvalds }
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
24281da177e4SLinus Torvalds {
2429645ca708SEric Dumazet 	struct sock *sk = udp_get_first(seq, 0);
24301da177e4SLinus Torvalds 
24311da177e4SLinus Torvalds 	if (sk)
24321da177e4SLinus Torvalds 		while (pos && (sk = udp_get_next(seq, sk)) != NULL)
24331da177e4SLinus Torvalds 			--pos;
24341da177e4SLinus Torvalds 	return pos ? NULL : sk;
24351da177e4SLinus Torvalds }
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
24381da177e4SLinus Torvalds {
243930842f29SVitaly Mayatskikh 	struct udp_iter_state *state = seq->private;
2440f86dcc5aSEric Dumazet 	state->bucket = MAX_UDP_PORTS;
244130842f29SVitaly Mayatskikh 
2442b50660f1SYOSHIFUJI Hideaki 	return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
24431da177e4SLinus Torvalds }
24441da177e4SLinus Torvalds 
24451da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
24461da177e4SLinus Torvalds {
24471da177e4SLinus Torvalds 	struct sock *sk;
24481da177e4SLinus Torvalds 
2449b50660f1SYOSHIFUJI Hideaki 	if (v == SEQ_START_TOKEN)
24501da177e4SLinus Torvalds 		sk = udp_get_idx(seq, 0);
24511da177e4SLinus Torvalds 	else
24521da177e4SLinus Torvalds 		sk = udp_get_next(seq, v);
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	++*pos;
24551da177e4SLinus Torvalds 	return sk;
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v)
24591da177e4SLinus Torvalds {
2460645ca708SEric Dumazet 	struct udp_iter_state *state = seq->private;
2461645ca708SEric Dumazet 
2462f86dcc5aSEric Dumazet 	if (state->bucket <= state->udp_table->mask)
2463645ca708SEric Dumazet 		spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
24641da177e4SLinus Torvalds }
24651da177e4SLinus Torvalds 
246673cb88ecSArjan van de Ven int udp_seq_open(struct inode *inode, struct file *file)
24671da177e4SLinus Torvalds {
2468d9dda78bSAl Viro 	struct udp_seq_afinfo *afinfo = PDE_DATA(inode);
2469a2be75c1SDenis V. Lunev 	struct udp_iter_state *s;
2470a2be75c1SDenis V. Lunev 	int err;
24711da177e4SLinus Torvalds 
2472a2be75c1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
2473a2be75c1SDenis V. Lunev 			   sizeof(struct udp_iter_state));
2474a2be75c1SDenis V. Lunev 	if (err < 0)
2475a2be75c1SDenis V. Lunev 		return err;
2476a91275efSDaniel Lezcano 
2477a2be75c1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
24781da177e4SLinus Torvalds 	s->family		= afinfo->family;
2479645ca708SEric Dumazet 	s->udp_table		= afinfo->udp_table;
2480a2be75c1SDenis V. Lunev 	return err;
2481a91275efSDaniel Lezcano }
248273cb88ecSArjan van de Ven EXPORT_SYMBOL(udp_seq_open);
2483a91275efSDaniel Lezcano 
24841da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
24850c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
24861da177e4SLinus Torvalds {
24871da177e4SLinus Torvalds 	struct proc_dir_entry *p;
24881da177e4SLinus Torvalds 	int rc = 0;
24891da177e4SLinus Torvalds 
2490dda61925SDenis V. Lunev 	afinfo->seq_ops.start		= udp_seq_start;
2491dda61925SDenis V. Lunev 	afinfo->seq_ops.next		= udp_seq_next;
2492dda61925SDenis V. Lunev 	afinfo->seq_ops.stop		= udp_seq_stop;
2493dda61925SDenis V. Lunev 
249484841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
249573cb88ecSArjan van de Ven 			     afinfo->seq_fops, afinfo);
249684841c3cSDenis V. Lunev 	if (!p)
24971da177e4SLinus Torvalds 		rc = -ENOMEM;
24981da177e4SLinus Torvalds 	return rc;
24991da177e4SLinus Torvalds }
2500c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_register);
25011da177e4SLinus Torvalds 
25020c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
25031da177e4SLinus Torvalds {
2504ece31ffdSGao feng 	remove_proc_entry(afinfo->name, net->proc_net);
25051da177e4SLinus Torvalds }
2506c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_unregister);
2507db8dac20SDavid S. Miller 
2508db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
25095e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2510652586dfSTetsuo Handa 		int bucket)
2511db8dac20SDavid S. Miller {
2512db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
2513c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2514c720c7e8SEric Dumazet 	__be32 src  = inet->inet_rcv_saddr;
2515c720c7e8SEric Dumazet 	__u16 destp	  = ntohs(inet->inet_dport);
2516c720c7e8SEric Dumazet 	__u16 srcp	  = ntohs(inet->inet_sport);
2517db8dac20SDavid S. Miller 
2518f86dcc5aSEric Dumazet 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2519652586dfSTetsuo Handa 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
2520db8dac20SDavid S. Miller 		bucket, src, srcp, dest, destp, sp->sk_state,
252131e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
252231e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
2523a7cb5a49SEric W. Biederman 		0, 0L, 0,
2524a7cb5a49SEric W. Biederman 		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
2525a7cb5a49SEric W. Biederman 		0, sock_i_ino(sp),
2526cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_refcnt), sp,
2527652586dfSTetsuo Handa 		atomic_read(&sp->sk_drops));
2528db8dac20SDavid S. Miller }
2529db8dac20SDavid S. Miller 
2530db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v)
2531db8dac20SDavid S. Miller {
2532652586dfSTetsuo Handa 	seq_setwidth(seq, 127);
2533db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
2534652586dfSTetsuo Handa 		seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
2535db8dac20SDavid S. Miller 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
2536cb61cb9bSEric Dumazet 			   "inode ref pointer drops");
2537db8dac20SDavid S. Miller 	else {
2538db8dac20SDavid S. Miller 		struct udp_iter_state *state = seq->private;
2539db8dac20SDavid S. Miller 
2540652586dfSTetsuo Handa 		udp4_format_sock(v, seq, state->bucket);
2541db8dac20SDavid S. Miller 	}
2542652586dfSTetsuo Handa 	seq_pad(seq, '\n');
2543db8dac20SDavid S. Miller 	return 0;
2544db8dac20SDavid S. Miller }
2545db8dac20SDavid S. Miller 
254673cb88ecSArjan van de Ven static const struct file_operations udp_afinfo_seq_fops = {
254773cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
254873cb88ecSArjan van de Ven 	.open     = udp_seq_open,
254973cb88ecSArjan van de Ven 	.read     = seq_read,
255073cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
255173cb88ecSArjan van de Ven 	.release  = seq_release_net
255273cb88ecSArjan van de Ven };
255373cb88ecSArjan van de Ven 
2554db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
2555db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = {
2556db8dac20SDavid S. Miller 	.name		= "udp",
2557db8dac20SDavid S. Miller 	.family		= AF_INET,
2558645ca708SEric Dumazet 	.udp_table	= &udp_table,
255973cb88ecSArjan van de Ven 	.seq_fops	= &udp_afinfo_seq_fops,
2560dda61925SDenis V. Lunev 	.seq_ops	= {
2561dda61925SDenis V. Lunev 		.show		= udp4_seq_show,
2562dda61925SDenis V. Lunev 	},
2563db8dac20SDavid S. Miller };
2564db8dac20SDavid S. Miller 
25652c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net)
256615439febSPavel Emelyanov {
256715439febSPavel Emelyanov 	return udp_proc_register(net, &udp4_seq_afinfo);
256815439febSPavel Emelyanov }
256915439febSPavel Emelyanov 
25702c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net)
257115439febSPavel Emelyanov {
257215439febSPavel Emelyanov 	udp_proc_unregister(net, &udp4_seq_afinfo);
257315439febSPavel Emelyanov }
257415439febSPavel Emelyanov 
257515439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = {
257615439febSPavel Emelyanov 	.init = udp4_proc_init_net,
257715439febSPavel Emelyanov 	.exit = udp4_proc_exit_net,
257815439febSPavel Emelyanov };
257915439febSPavel Emelyanov 
2580db8dac20SDavid S. Miller int __init udp4_proc_init(void)
2581db8dac20SDavid S. Miller {
258215439febSPavel Emelyanov 	return register_pernet_subsys(&udp4_net_ops);
2583db8dac20SDavid S. Miller }
2584db8dac20SDavid S. Miller 
2585db8dac20SDavid S. Miller void udp4_proc_exit(void)
2586db8dac20SDavid S. Miller {
258715439febSPavel Emelyanov 	unregister_pernet_subsys(&udp4_net_ops);
2588db8dac20SDavid S. Miller }
25891da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
25901da177e4SLinus Torvalds 
2591f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries;
2592f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str)
2593645ca708SEric Dumazet {
2594413c27d8SEldad Zack 	ssize_t ret;
2595413c27d8SEldad Zack 
2596f86dcc5aSEric Dumazet 	if (!str)
2597f86dcc5aSEric Dumazet 		return 0;
2598413c27d8SEldad Zack 
2599413c27d8SEldad Zack 	ret = kstrtoul(str, 0, &uhash_entries);
2600413c27d8SEldad Zack 	if (ret)
2601413c27d8SEldad Zack 		return 0;
2602413c27d8SEldad Zack 
2603f86dcc5aSEric Dumazet 	if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
2604f86dcc5aSEric Dumazet 		uhash_entries = UDP_HTABLE_SIZE_MIN;
2605f86dcc5aSEric Dumazet 	return 1;
2606f86dcc5aSEric Dumazet }
2607f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries);
2608645ca708SEric Dumazet 
2609f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name)
2610f86dcc5aSEric Dumazet {
2611f86dcc5aSEric Dumazet 	unsigned int i;
2612f86dcc5aSEric Dumazet 
2613f86dcc5aSEric Dumazet 	table->hash = alloc_large_system_hash(name,
2614512615b6SEric Dumazet 					      2 * sizeof(struct udp_hslot),
2615f86dcc5aSEric Dumazet 					      uhash_entries,
2616f86dcc5aSEric Dumazet 					      21, /* one slot per 2 MB */
2617f86dcc5aSEric Dumazet 					      0,
2618f86dcc5aSEric Dumazet 					      &table->log,
2619f86dcc5aSEric Dumazet 					      &table->mask,
262031fe62b9STim Bird 					      UDP_HTABLE_SIZE_MIN,
2621f86dcc5aSEric Dumazet 					      64 * 1024);
262231fe62b9STim Bird 
2623512615b6SEric Dumazet 	table->hash2 = table->hash + (table->mask + 1);
2624f86dcc5aSEric Dumazet 	for (i = 0; i <= table->mask; i++) {
262588ab1932SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
2626fdcc8aa9SEric Dumazet 		table->hash[i].count = 0;
2627645ca708SEric Dumazet 		spin_lock_init(&table->hash[i].lock);
2628645ca708SEric Dumazet 	}
2629512615b6SEric Dumazet 	for (i = 0; i <= table->mask; i++) {
2630512615b6SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
2631512615b6SEric Dumazet 		table->hash2[i].count = 0;
2632512615b6SEric Dumazet 		spin_lock_init(&table->hash2[i].lock);
2633512615b6SEric Dumazet 	}
2634645ca708SEric Dumazet }
2635645ca708SEric Dumazet 
2636723b8e46STom Herbert u32 udp_flow_hashrnd(void)
2637723b8e46STom Herbert {
2638723b8e46STom Herbert 	static u32 hashrnd __read_mostly;
2639723b8e46STom Herbert 
2640723b8e46STom Herbert 	net_get_random_once(&hashrnd, sizeof(hashrnd));
2641723b8e46STom Herbert 
2642723b8e46STom Herbert 	return hashrnd;
2643723b8e46STom Herbert }
2644723b8e46STom Herbert EXPORT_SYMBOL(udp_flow_hashrnd);
2645723b8e46STom Herbert 
264695766fffSHideo Aoki void __init udp_init(void)
264795766fffSHideo Aoki {
2648f03d78dbSEric Dumazet 	unsigned long limit;
264995766fffSHideo Aoki 
2650f86dcc5aSEric Dumazet 	udp_table_init(&udp_table, "UDP");
2651f03d78dbSEric Dumazet 	limit = nr_free_buffer_pages() / 8;
265295766fffSHideo Aoki 	limit = max(limit, 128UL);
265395766fffSHideo Aoki 	sysctl_udp_mem[0] = limit / 4 * 3;
265495766fffSHideo Aoki 	sysctl_udp_mem[1] = limit;
265595766fffSHideo Aoki 	sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
265695766fffSHideo Aoki 
265795766fffSHideo Aoki 	sysctl_udp_rmem_min = SK_MEM_QUANTUM;
265895766fffSHideo Aoki 	sysctl_udp_wmem_min = SK_MEM_QUANTUM;
265995766fffSHideo Aoki }
2660