xref: /linux/net/ipv4/udp.c (revision 2abb7cdc0dc84e99b76ef983a1ae1978922aa9b3)
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>
931da177e4SLinus Torvalds #include <linux/in.h>
941da177e4SLinus Torvalds #include <linux/errno.h>
951da177e4SLinus Torvalds #include <linux/timer.h>
961da177e4SLinus Torvalds #include <linux/mm.h>
971da177e4SLinus Torvalds #include <linux/inet.h>
981da177e4SLinus Torvalds #include <linux/netdevice.h>
995a0e3ad6STejun Heo #include <linux/slab.h>
100c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
1011da177e4SLinus Torvalds #include <linux/skbuff.h>
10257c67ff4STom Herbert #include <linux/netdevice.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"
1161da177e4SLinus Torvalds 
117f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly;
118645ca708SEric Dumazet EXPORT_SYMBOL(udp_table);
1191da177e4SLinus Torvalds 
1208d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly;
12195766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem);
122c482c568SEric Dumazet 
123c482c568SEric Dumazet int sysctl_udp_rmem_min __read_mostly;
12495766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min);
125c482c568SEric Dumazet 
126c482c568SEric Dumazet int sysctl_udp_wmem_min __read_mostly;
12795766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min);
12895766fffSHideo Aoki 
1298d987e5cSEric Dumazet atomic_long_t udp_memory_allocated;
13095766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated);
13195766fffSHideo Aoki 
132f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536
133f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
13498322f22SEric Dumazet 
135f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num,
136645ca708SEric Dumazet 			       const struct udp_hslot *hslot,
13798322f22SEric Dumazet 			       unsigned long *bitmap,
138f24d43c0SEric Dumazet 			       struct sock *sk,
139f24d43c0SEric Dumazet 			       int (*saddr_comp)(const struct sock *sk1,
140f86dcc5aSEric Dumazet 						 const struct sock *sk2),
141f86dcc5aSEric Dumazet 			       unsigned int log)
14225030a7fSGerrit Renker {
143f24d43c0SEric Dumazet 	struct sock *sk2;
14488ab1932SEric Dumazet 	struct hlist_nulls_node *node;
145ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
14625030a7fSGerrit Renker 
14788ab1932SEric Dumazet 	sk_nulls_for_each(sk2, node, &hslot->head)
148f24d43c0SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
149f24d43c0SEric Dumazet 		    sk2 != sk &&
150d4cada4aSEric Dumazet 		    (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
151f24d43c0SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1529d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1539d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
154ba418fa3STom Herbert 		    (!sk2->sk_reuseport || !sk->sk_reuseport ||
155ba418fa3STom Herbert 		      !uid_eq(uid, sock_i_uid(sk2))) &&
15698322f22SEric Dumazet 		    (*saddr_comp)(sk, sk2)) {
15798322f22SEric Dumazet 			if (bitmap)
158d4cada4aSEric Dumazet 				__set_bit(udp_sk(sk2)->udp_port_hash >> log,
159d4cada4aSEric Dumazet 					  bitmap);
16098322f22SEric Dumazet 			else
161fc038410SDavid S. Miller 				return 1;
16298322f22SEric Dumazet 		}
16325030a7fSGerrit Renker 	return 0;
16425030a7fSGerrit Renker }
16525030a7fSGerrit Renker 
16630fff923SEric Dumazet /*
16730fff923SEric Dumazet  * Note: we still hold spinlock of primary hash chain, so no other writer
16830fff923SEric Dumazet  * can insert/delete a socket with local_port == num
16930fff923SEric Dumazet  */
17030fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num,
17130fff923SEric Dumazet 			       struct udp_hslot *hslot2,
17230fff923SEric Dumazet 			       struct sock *sk,
17330fff923SEric Dumazet 			       int (*saddr_comp)(const struct sock *sk1,
17430fff923SEric Dumazet 						 const struct sock *sk2))
17530fff923SEric Dumazet {
17630fff923SEric Dumazet 	struct sock *sk2;
17730fff923SEric Dumazet 	struct hlist_nulls_node *node;
178ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
17930fff923SEric Dumazet 	int res = 0;
18030fff923SEric Dumazet 
18130fff923SEric Dumazet 	spin_lock(&hslot2->lock);
18230fff923SEric Dumazet 	udp_portaddr_for_each_entry(sk2, node, &hslot2->head)
18330fff923SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
18430fff923SEric Dumazet 		    sk2 != sk &&
18530fff923SEric Dumazet 		    (udp_sk(sk2)->udp_port_hash == num) &&
18630fff923SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1879d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1889d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
189ba418fa3STom Herbert 		    (!sk2->sk_reuseport || !sk->sk_reuseport ||
190ba418fa3STom Herbert 		      !uid_eq(uid, sock_i_uid(sk2))) &&
19130fff923SEric Dumazet 		    (*saddr_comp)(sk, sk2)) {
19230fff923SEric Dumazet 			res = 1;
19330fff923SEric Dumazet 			break;
19430fff923SEric Dumazet 		}
19530fff923SEric Dumazet 	spin_unlock(&hslot2->lock);
19630fff923SEric Dumazet 	return res;
19730fff923SEric Dumazet }
19830fff923SEric Dumazet 
19925030a7fSGerrit Renker /**
2006ba5a3c5SPavel Emelyanov  *  udp_lib_get_port  -  UDP/-Lite port lookup for IPv4 and IPv6
20125030a7fSGerrit Renker  *
20225030a7fSGerrit Renker  *  @sk:          socket struct in question
20325030a7fSGerrit Renker  *  @snum:        port number to look up
204df2bc459SDavid S. Miller  *  @saddr_comp:  AF-dependent comparison of bound local IP addresses
20525985edcSLucas De Marchi  *  @hash2_nulladdr: AF-dependent hash value in secondary hash chains,
20630fff923SEric Dumazet  *                   with NULL address
20725030a7fSGerrit Renker  */
2086ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum,
209df2bc459SDavid S. Miller 		       int (*saddr_comp)(const struct sock *sk1,
21030fff923SEric Dumazet 					 const struct sock *sk2),
21130fff923SEric Dumazet 		     unsigned int hash2_nulladdr)
2121da177e4SLinus Torvalds {
213512615b6SEric Dumazet 	struct udp_hslot *hslot, *hslot2;
214645ca708SEric Dumazet 	struct udp_table *udptable = sk->sk_prot->h.udp_table;
21525030a7fSGerrit Renker 	int    error = 1;
2163b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2171da177e4SLinus Torvalds 
21832c1da70SStephen Hemminger 	if (!snum) {
2199088c560SEric Dumazet 		int low, high, remaining;
22095c96174SEric Dumazet 		unsigned int rand;
22198322f22SEric Dumazet 		unsigned short first, last;
22298322f22SEric Dumazet 		DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
2231da177e4SLinus Torvalds 
2240bbf87d8SEric W. Biederman 		inet_get_local_port_range(net, &low, &high);
225a25de534SAnton Arapov 		remaining = (high - low) + 1;
226227b60f5SStephen Hemminger 
22763862b5bSAruna-Hewapathirane 		rand = prandom_u32();
2288fc54f68SDaniel Borkmann 		first = reciprocal_scale(rand, remaining) + low;
22998322f22SEric Dumazet 		/*
23098322f22SEric Dumazet 		 * force rand to be an odd multiple of UDP_HTABLE_SIZE
23198322f22SEric Dumazet 		 */
232f86dcc5aSEric Dumazet 		rand = (rand | 1) * (udptable->mask + 1);
2335781b235SEric Dumazet 		last = first + udptable->mask + 1;
2345781b235SEric Dumazet 		do {
235f86dcc5aSEric Dumazet 			hslot = udp_hashslot(udptable, net, first);
23698322f22SEric Dumazet 			bitmap_zero(bitmap, PORTS_PER_CHAIN);
237645ca708SEric Dumazet 			spin_lock_bh(&hslot->lock);
23898322f22SEric Dumazet 			udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
239f86dcc5aSEric Dumazet 					    saddr_comp, udptable->log);
24098322f22SEric Dumazet 
24198322f22SEric Dumazet 			snum = first;
24298322f22SEric Dumazet 			/*
24398322f22SEric Dumazet 			 * Iterate on all possible values of snum for this hash.
24498322f22SEric Dumazet 			 * Using steps of an odd multiple of UDP_HTABLE_SIZE
24598322f22SEric Dumazet 			 * give us randomization and full range coverage.
24698322f22SEric Dumazet 			 */
2479088c560SEric Dumazet 			do {
24898322f22SEric Dumazet 				if (low <= snum && snum <= high &&
249e3826f1eSAmerigo Wang 				    !test_bit(snum >> udptable->log, bitmap) &&
250122ff243SWANG Cong 				    !inet_is_local_reserved_port(net, snum))
25198322f22SEric Dumazet 					goto found;
25298322f22SEric Dumazet 				snum += rand;
25398322f22SEric Dumazet 			} while (snum != first);
25498322f22SEric Dumazet 			spin_unlock_bh(&hslot->lock);
2555781b235SEric Dumazet 		} while (++first != last);
25698322f22SEric Dumazet 		goto fail;
257645ca708SEric Dumazet 	} else {
258f86dcc5aSEric Dumazet 		hslot = udp_hashslot(udptable, net, snum);
259645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
26030fff923SEric Dumazet 		if (hslot->count > 10) {
26130fff923SEric Dumazet 			int exist;
26230fff923SEric Dumazet 			unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
26330fff923SEric Dumazet 
26430fff923SEric Dumazet 			slot2          &= udptable->mask;
26530fff923SEric Dumazet 			hash2_nulladdr &= udptable->mask;
26630fff923SEric Dumazet 
26730fff923SEric Dumazet 			hslot2 = udp_hashslot2(udptable, slot2);
26830fff923SEric Dumazet 			if (hslot->count < hslot2->count)
26930fff923SEric Dumazet 				goto scan_primary_hash;
27030fff923SEric Dumazet 
27130fff923SEric Dumazet 			exist = udp_lib_lport_inuse2(net, snum, hslot2,
27230fff923SEric Dumazet 						     sk, saddr_comp);
27330fff923SEric Dumazet 			if (!exist && (hash2_nulladdr != slot2)) {
27430fff923SEric Dumazet 				hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
27530fff923SEric Dumazet 				exist = udp_lib_lport_inuse2(net, snum, hslot2,
27630fff923SEric Dumazet 							     sk, saddr_comp);
27730fff923SEric Dumazet 			}
27830fff923SEric Dumazet 			if (exist)
27930fff923SEric Dumazet 				goto fail_unlock;
28030fff923SEric Dumazet 			else
28130fff923SEric Dumazet 				goto found;
28230fff923SEric Dumazet 		}
28330fff923SEric Dumazet scan_primary_hash:
284f86dcc5aSEric Dumazet 		if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
285f86dcc5aSEric Dumazet 					saddr_comp, 0))
286645ca708SEric Dumazet 			goto fail_unlock;
287645ca708SEric Dumazet 	}
28898322f22SEric Dumazet found:
289c720c7e8SEric Dumazet 	inet_sk(sk)->inet_num = snum;
290d4cada4aSEric Dumazet 	udp_sk(sk)->udp_port_hash = snum;
291d4cada4aSEric Dumazet 	udp_sk(sk)->udp_portaddr_hash ^= snum;
2921da177e4SLinus Torvalds 	if (sk_unhashed(sk)) {
29388ab1932SEric Dumazet 		sk_nulls_add_node_rcu(sk, &hslot->head);
294fdcc8aa9SEric Dumazet 		hslot->count++;
295c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
296512615b6SEric Dumazet 
297512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
298512615b6SEric Dumazet 		spin_lock(&hslot2->lock);
299512615b6SEric Dumazet 		hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
300512615b6SEric Dumazet 					 &hslot2->head);
301512615b6SEric Dumazet 		hslot2->count++;
302512615b6SEric Dumazet 		spin_unlock(&hslot2->lock);
3031da177e4SLinus Torvalds 	}
30425030a7fSGerrit Renker 	error = 0;
305645ca708SEric Dumazet fail_unlock:
306645ca708SEric Dumazet 	spin_unlock_bh(&hslot->lock);
3071da177e4SLinus Torvalds fail:
30825030a7fSGerrit Renker 	return error;
3091da177e4SLinus Torvalds }
310c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port);
3111da177e4SLinus Torvalds 
312499923c7SVlad Yasevich static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
313db8dac20SDavid S. Miller {
314db8dac20SDavid S. Miller 	struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
315db8dac20SDavid S. Miller 
316db8dac20SDavid S. Miller 	return 	(!ipv6_only_sock(sk2)  &&
317c720c7e8SEric Dumazet 		 (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr ||
318c720c7e8SEric Dumazet 		   inet1->inet_rcv_saddr == inet2->inet_rcv_saddr));
319db8dac20SDavid S. Miller }
320db8dac20SDavid S. Miller 
321d4cada4aSEric Dumazet static unsigned int udp4_portaddr_hash(struct net *net, __be32 saddr,
322d4cada4aSEric Dumazet 				       unsigned int port)
323d4cada4aSEric Dumazet {
3240eae88f3SEric Dumazet 	return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
325d4cada4aSEric Dumazet }
326d4cada4aSEric Dumazet 
3276ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum)
328db8dac20SDavid S. Miller {
32930fff923SEric Dumazet 	unsigned int hash2_nulladdr =
3300eae88f3SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
33130fff923SEric Dumazet 	unsigned int hash2_partial =
33230fff923SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
33330fff923SEric Dumazet 
334d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
33530fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
33630fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr);
337db8dac20SDavid S. Miller }
338db8dac20SDavid S. Miller 
339645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
340645ca708SEric Dumazet 			 unsigned short hnum,
341645ca708SEric Dumazet 			 __be16 sport, __be32 daddr, __be16 dport, int dif)
342645ca708SEric Dumazet {
343645ca708SEric Dumazet 	int score = -1;
344645ca708SEric Dumazet 
345d4cada4aSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
346645ca708SEric Dumazet 			!ipv6_only_sock(sk)) {
347645ca708SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
348645ca708SEric Dumazet 
349ba418fa3STom Herbert 		score = (sk->sk_family == PF_INET ? 2 : 1);
350c720c7e8SEric Dumazet 		if (inet->inet_rcv_saddr) {
351c720c7e8SEric Dumazet 			if (inet->inet_rcv_saddr != daddr)
352645ca708SEric Dumazet 				return -1;
353ba418fa3STom Herbert 			score += 4;
354645ca708SEric Dumazet 		}
355c720c7e8SEric Dumazet 		if (inet->inet_daddr) {
356c720c7e8SEric Dumazet 			if (inet->inet_daddr != saddr)
357645ca708SEric Dumazet 				return -1;
358ba418fa3STom Herbert 			score += 4;
359645ca708SEric Dumazet 		}
360c720c7e8SEric Dumazet 		if (inet->inet_dport) {
361c720c7e8SEric Dumazet 			if (inet->inet_dport != sport)
362645ca708SEric Dumazet 				return -1;
363ba418fa3STom Herbert 			score += 4;
364645ca708SEric Dumazet 		}
365645ca708SEric Dumazet 		if (sk->sk_bound_dev_if) {
366645ca708SEric Dumazet 			if (sk->sk_bound_dev_if != dif)
367645ca708SEric Dumazet 				return -1;
368ba418fa3STom Herbert 			score += 4;
369645ca708SEric Dumazet 		}
370645ca708SEric Dumazet 	}
371645ca708SEric Dumazet 	return score;
372645ca708SEric Dumazet }
373645ca708SEric Dumazet 
3745051ebd2SEric Dumazet /*
3755051ebd2SEric Dumazet  * In this second variant, we check (daddr, dport) matches (inet_rcv_sadd, inet_num)
3765051ebd2SEric Dumazet  */
3775051ebd2SEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
3785051ebd2SEric Dumazet 				 __be32 saddr, __be16 sport,
3795051ebd2SEric Dumazet 				 __be32 daddr, unsigned int hnum, int dif)
3805051ebd2SEric Dumazet {
3815051ebd2SEric Dumazet 	int score = -1;
3825051ebd2SEric Dumazet 
3835051ebd2SEric Dumazet 	if (net_eq(sock_net(sk), net) && !ipv6_only_sock(sk)) {
3845051ebd2SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
3855051ebd2SEric Dumazet 
3865051ebd2SEric Dumazet 		if (inet->inet_rcv_saddr != daddr)
3875051ebd2SEric Dumazet 			return -1;
3885051ebd2SEric Dumazet 		if (inet->inet_num != hnum)
3895051ebd2SEric Dumazet 			return -1;
3905051ebd2SEric Dumazet 
391ba418fa3STom Herbert 		score = (sk->sk_family == PF_INET ? 2 : 1);
3925051ebd2SEric Dumazet 		if (inet->inet_daddr) {
3935051ebd2SEric Dumazet 			if (inet->inet_daddr != saddr)
3945051ebd2SEric Dumazet 				return -1;
395ba418fa3STom Herbert 			score += 4;
3965051ebd2SEric Dumazet 		}
3975051ebd2SEric Dumazet 		if (inet->inet_dport) {
3985051ebd2SEric Dumazet 			if (inet->inet_dport != sport)
3995051ebd2SEric Dumazet 				return -1;
400ba418fa3STom Herbert 			score += 4;
4015051ebd2SEric Dumazet 		}
4025051ebd2SEric Dumazet 		if (sk->sk_bound_dev_if) {
4035051ebd2SEric Dumazet 			if (sk->sk_bound_dev_if != dif)
4045051ebd2SEric Dumazet 				return -1;
405ba418fa3STom Herbert 			score += 4;
4065051ebd2SEric Dumazet 		}
4075051ebd2SEric Dumazet 	}
4085051ebd2SEric Dumazet 	return score;
4095051ebd2SEric Dumazet }
4105051ebd2SEric Dumazet 
41165cd8033SHannes Frederic Sowa static unsigned int udp_ehashfn(struct net *net, const __be32 laddr,
41265cd8033SHannes Frederic Sowa 				 const __u16 lport, const __be32 faddr,
41365cd8033SHannes Frederic Sowa 				 const __be16 fport)
41465cd8033SHannes Frederic Sowa {
4151bbdceefSHannes Frederic Sowa 	static u32 udp_ehash_secret __read_mostly;
4161bbdceefSHannes Frederic Sowa 
4171bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret));
4181bbdceefSHannes Frederic Sowa 
41965cd8033SHannes Frederic Sowa 	return __inet_ehashfn(laddr, lport, faddr, fport,
4201bbdceefSHannes Frederic Sowa 			      udp_ehash_secret + net_hash_mix(net));
42165cd8033SHannes Frederic Sowa }
42265cd8033SHannes Frederic Sowa 
4235051ebd2SEric Dumazet 
4245051ebd2SEric Dumazet /* called with read_rcu_lock() */
4255051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net,
4265051ebd2SEric Dumazet 		__be32 saddr, __be16 sport,
4275051ebd2SEric Dumazet 		__be32 daddr, unsigned int hnum, int dif,
4285051ebd2SEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
4295051ebd2SEric Dumazet {
4305051ebd2SEric Dumazet 	struct sock *sk, *result;
4315051ebd2SEric Dumazet 	struct hlist_nulls_node *node;
432ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
433ba418fa3STom Herbert 	u32 hash = 0;
4345051ebd2SEric Dumazet 
4355051ebd2SEric Dumazet begin:
4365051ebd2SEric Dumazet 	result = NULL;
437ba418fa3STom Herbert 	badness = 0;
4385051ebd2SEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
4395051ebd2SEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
4405051ebd2SEric Dumazet 				      daddr, hnum, dif);
4415051ebd2SEric Dumazet 		if (score > badness) {
4425051ebd2SEric Dumazet 			result = sk;
4435051ebd2SEric Dumazet 			badness = score;
444ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
445ba418fa3STom Herbert 			if (reuseport) {
44665cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
4477c0cadc6SEric Dumazet 						   saddr, sport);
448ba418fa3STom Herbert 				matches = 1;
449ba418fa3STom Herbert 			}
450ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
451ba418fa3STom Herbert 			matches++;
4528fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
453ba418fa3STom Herbert 				result = sk;
454ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
4555051ebd2SEric Dumazet 		}
4565051ebd2SEric Dumazet 	}
4575051ebd2SEric Dumazet 	/*
4585051ebd2SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
4595051ebd2SEric Dumazet 	 * not the expected one, we must restart lookup.
4605051ebd2SEric Dumazet 	 * We probably met an item that was moved to another chain.
4615051ebd2SEric Dumazet 	 */
4625051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot2)
4635051ebd2SEric Dumazet 		goto begin;
4645051ebd2SEric Dumazet 	if (result) {
465c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
4665051ebd2SEric Dumazet 			result = NULL;
4675051ebd2SEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
4685051ebd2SEric Dumazet 				  daddr, hnum, dif) < badness)) {
4695051ebd2SEric Dumazet 			sock_put(result);
4705051ebd2SEric Dumazet 			goto begin;
4715051ebd2SEric Dumazet 		}
4725051ebd2SEric Dumazet 	}
4735051ebd2SEric Dumazet 	return result;
4745051ebd2SEric Dumazet }
4755051ebd2SEric Dumazet 
476db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
477db8dac20SDavid S. Miller  * harder than this. -DaveM
478db8dac20SDavid S. Miller  */
479fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
480db8dac20SDavid S. Miller 		__be16 sport, __be32 daddr, __be16 dport,
481645ca708SEric Dumazet 		int dif, struct udp_table *udptable)
482db8dac20SDavid S. Miller {
483271b72c7SEric Dumazet 	struct sock *sk, *result;
48488ab1932SEric Dumazet 	struct hlist_nulls_node *node;
485db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(dport);
4865051ebd2SEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
4875051ebd2SEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
488ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
489ba418fa3STom Herbert 	u32 hash = 0;
490db8dac20SDavid S. Miller 
491271b72c7SEric Dumazet 	rcu_read_lock();
4925051ebd2SEric Dumazet 	if (hslot->count > 10) {
4935051ebd2SEric Dumazet 		hash2 = udp4_portaddr_hash(net, daddr, hnum);
4945051ebd2SEric Dumazet 		slot2 = hash2 & udptable->mask;
4955051ebd2SEric Dumazet 		hslot2 = &udptable->hash2[slot2];
4965051ebd2SEric Dumazet 		if (hslot->count < hslot2->count)
4975051ebd2SEric Dumazet 			goto begin;
4985051ebd2SEric Dumazet 
4995051ebd2SEric Dumazet 		result = udp4_lib_lookup2(net, saddr, sport,
5005051ebd2SEric Dumazet 					  daddr, hnum, dif,
5015051ebd2SEric Dumazet 					  hslot2, slot2);
5025051ebd2SEric Dumazet 		if (!result) {
5030eae88f3SEric Dumazet 			hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
5045051ebd2SEric Dumazet 			slot2 = hash2 & udptable->mask;
5055051ebd2SEric Dumazet 			hslot2 = &udptable->hash2[slot2];
5065051ebd2SEric Dumazet 			if (hslot->count < hslot2->count)
5075051ebd2SEric Dumazet 				goto begin;
5085051ebd2SEric Dumazet 
5091223c67cSJorge Boncompte [DTI2] 			result = udp4_lib_lookup2(net, saddr, sport,
5100eae88f3SEric Dumazet 						  htonl(INADDR_ANY), hnum, dif,
5115051ebd2SEric Dumazet 						  hslot2, slot2);
5125051ebd2SEric Dumazet 		}
5135051ebd2SEric Dumazet 		rcu_read_unlock();
5145051ebd2SEric Dumazet 		return result;
5155051ebd2SEric Dumazet 	}
516271b72c7SEric Dumazet begin:
517271b72c7SEric Dumazet 	result = NULL;
518ba418fa3STom Herbert 	badness = 0;
51988ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
520645ca708SEric Dumazet 		score = compute_score(sk, net, saddr, hnum, sport,
521645ca708SEric Dumazet 				      daddr, dport, dif);
522645ca708SEric Dumazet 		if (score > badness) {
523db8dac20SDavid S. Miller 			result = sk;
524db8dac20SDavid S. Miller 			badness = score;
525ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
526ba418fa3STom Herbert 			if (reuseport) {
52765cd8033SHannes Frederic Sowa 				hash = udp_ehashfn(net, daddr, hnum,
5287c0cadc6SEric Dumazet 						   saddr, sport);
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);
536db8dac20SDavid S. Miller 		}
537db8dac20SDavid S. Miller 	}
53888ab1932SEric Dumazet 	/*
53988ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
54088ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
54188ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
54288ab1932SEric Dumazet 	 */
5435051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot)
54488ab1932SEric Dumazet 		goto begin;
54588ab1932SEric Dumazet 
546271b72c7SEric Dumazet 	if (result) {
547c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
548271b72c7SEric Dumazet 			result = NULL;
549271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, saddr, hnum, sport,
550271b72c7SEric Dumazet 				  daddr, dport, dif) < badness)) {
551271b72c7SEric Dumazet 			sock_put(result);
552271b72c7SEric Dumazet 			goto begin;
553271b72c7SEric Dumazet 		}
554271b72c7SEric Dumazet 	}
555271b72c7SEric Dumazet 	rcu_read_unlock();
556db8dac20SDavid S. Miller 	return result;
557db8dac20SDavid S. Miller }
558fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
559db8dac20SDavid S. Miller 
560607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
561607c4aafSKOVACS Krisztian 						 __be16 sport, __be16 dport,
562645ca708SEric Dumazet 						 struct udp_table *udptable)
563607c4aafSKOVACS Krisztian {
564607c4aafSKOVACS Krisztian 	const struct iphdr *iph = ip_hdr(skb);
565607c4aafSKOVACS Krisztian 
566adf30907SEric Dumazet 	return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
567607c4aafSKOVACS Krisztian 				 iph->daddr, dport, inet_iif(skb),
568607c4aafSKOVACS Krisztian 				 udptable);
569607c4aafSKOVACS Krisztian }
570607c4aafSKOVACS Krisztian 
571bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
572bcd41303SKOVACS Krisztian 			     __be32 daddr, __be16 dport, int dif)
573bcd41303SKOVACS Krisztian {
574645ca708SEric Dumazet 	return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
575bcd41303SKOVACS Krisztian }
576bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup);
577bcd41303SKOVACS Krisztian 
578421b3885SShawn Bohrer static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
579421b3885SShawn Bohrer 				       __be16 loc_port, __be32 loc_addr,
580421b3885SShawn Bohrer 				       __be16 rmt_port, __be32 rmt_addr,
581421b3885SShawn Bohrer 				       int dif, unsigned short hnum)
582421b3885SShawn Bohrer {
583421b3885SShawn Bohrer 	struct inet_sock *inet = inet_sk(sk);
584421b3885SShawn Bohrer 
585421b3885SShawn Bohrer 	if (!net_eq(sock_net(sk), net) ||
586421b3885SShawn Bohrer 	    udp_sk(sk)->udp_port_hash != hnum ||
587421b3885SShawn Bohrer 	    (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
588421b3885SShawn Bohrer 	    (inet->inet_dport != rmt_port && inet->inet_dport) ||
589421b3885SShawn Bohrer 	    (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
590421b3885SShawn Bohrer 	    ipv6_only_sock(sk) ||
591421b3885SShawn Bohrer 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
592421b3885SShawn Bohrer 		return false;
593421b3885SShawn Bohrer 	if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif))
594421b3885SShawn Bohrer 		return false;
595421b3885SShawn Bohrer 	return true;
596421b3885SShawn Bohrer }
597421b3885SShawn Bohrer 
598db8dac20SDavid S. Miller /*
599db8dac20SDavid S. Miller  * This routine is called by the ICMP module when it gets some
600db8dac20SDavid S. Miller  * sort of error condition.  If err < 0 then the socket should
601db8dac20SDavid S. Miller  * be closed and the error returned to the user.  If err > 0
602db8dac20SDavid S. Miller  * it's just the icmp type << 8 | icmp code.
603db8dac20SDavid S. Miller  * Header points to the ip header of the error packet. We move
604db8dac20SDavid S. Miller  * on past this. Then (as it used to claim before adjustment)
605db8dac20SDavid S. Miller  * header points to the first 8 bytes of the udp header.  We need
606db8dac20SDavid S. Miller  * to find the appropriate port.
607db8dac20SDavid S. Miller  */
608db8dac20SDavid S. Miller 
609645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
610db8dac20SDavid S. Miller {
611db8dac20SDavid S. Miller 	struct inet_sock *inet;
612b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *)skb->data;
613db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
614db8dac20SDavid S. Miller 	const int type = icmp_hdr(skb)->type;
615db8dac20SDavid S. Miller 	const int code = icmp_hdr(skb)->code;
616db8dac20SDavid S. Miller 	struct sock *sk;
617db8dac20SDavid S. Miller 	int harderr;
618db8dac20SDavid S. Miller 	int err;
619fd54d716SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
620db8dac20SDavid S. Miller 
621fd54d716SPavel Emelyanov 	sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
622db8dac20SDavid S. Miller 			iph->saddr, uh->source, skb->dev->ifindex, udptable);
623db8dac20SDavid S. Miller 	if (sk == NULL) {
624dcfc23caSPavel Emelyanov 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
625db8dac20SDavid S. Miller 		return;	/* No socket for error */
626db8dac20SDavid S. Miller 	}
627db8dac20SDavid S. Miller 
628db8dac20SDavid S. Miller 	err = 0;
629db8dac20SDavid S. Miller 	harderr = 0;
630db8dac20SDavid S. Miller 	inet = inet_sk(sk);
631db8dac20SDavid S. Miller 
632db8dac20SDavid S. Miller 	switch (type) {
633db8dac20SDavid S. Miller 	default:
634db8dac20SDavid S. Miller 	case ICMP_TIME_EXCEEDED:
635db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
636db8dac20SDavid S. Miller 		break;
637db8dac20SDavid S. Miller 	case ICMP_SOURCE_QUENCH:
638db8dac20SDavid S. Miller 		goto out;
639db8dac20SDavid S. Miller 	case ICMP_PARAMETERPROB:
640db8dac20SDavid S. Miller 		err = EPROTO;
641db8dac20SDavid S. Miller 		harderr = 1;
642db8dac20SDavid S. Miller 		break;
643db8dac20SDavid S. Miller 	case ICMP_DEST_UNREACH:
644db8dac20SDavid S. Miller 		if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
64536393395SDavid S. Miller 			ipv4_sk_update_pmtu(skb, sk, info);
646db8dac20SDavid S. Miller 			if (inet->pmtudisc != IP_PMTUDISC_DONT) {
647db8dac20SDavid S. Miller 				err = EMSGSIZE;
648db8dac20SDavid S. Miller 				harderr = 1;
649db8dac20SDavid S. Miller 				break;
650db8dac20SDavid S. Miller 			}
651db8dac20SDavid S. Miller 			goto out;
652db8dac20SDavid S. Miller 		}
653db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
654db8dac20SDavid S. Miller 		if (code <= NR_ICMP_UNREACH) {
655db8dac20SDavid S. Miller 			harderr = icmp_err_convert[code].fatal;
656db8dac20SDavid S. Miller 			err = icmp_err_convert[code].errno;
657db8dac20SDavid S. Miller 		}
658db8dac20SDavid S. Miller 		break;
65955be7a9cSDavid S. Miller 	case ICMP_REDIRECT:
66055be7a9cSDavid S. Miller 		ipv4_sk_redirect(skb, sk);
6611a462d18SDuan Jiong 		goto out;
662db8dac20SDavid S. Miller 	}
663db8dac20SDavid S. Miller 
664db8dac20SDavid S. Miller 	/*
665db8dac20SDavid S. Miller 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
666db8dac20SDavid S. Miller 	 *	4.1.3.3.
667db8dac20SDavid S. Miller 	 */
668db8dac20SDavid S. Miller 	if (!inet->recverr) {
669db8dac20SDavid S. Miller 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
670db8dac20SDavid S. Miller 			goto out;
671b1faf566SEric Dumazet 	} else
672db8dac20SDavid S. Miller 		ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
673b1faf566SEric Dumazet 
674db8dac20SDavid S. Miller 	sk->sk_err = err;
675db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
676db8dac20SDavid S. Miller out:
677db8dac20SDavid S. Miller 	sock_put(sk);
678db8dac20SDavid S. Miller }
679db8dac20SDavid S. Miller 
680db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info)
681db8dac20SDavid S. Miller {
682645ca708SEric Dumazet 	__udp4_lib_err(skb, info, &udp_table);
683db8dac20SDavid S. Miller }
684db8dac20SDavid S. Miller 
685db8dac20SDavid S. Miller /*
686db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
687db8dac20SDavid S. Miller  */
68836d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk)
689db8dac20SDavid S. Miller {
690db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
691db8dac20SDavid S. Miller 
692db8dac20SDavid S. Miller 	if (up->pending) {
693db8dac20SDavid S. Miller 		up->len = 0;
694db8dac20SDavid S. Miller 		up->pending = 0;
695db8dac20SDavid S. Miller 		ip_flush_pending_frames(sk);
696db8dac20SDavid S. Miller 	}
697db8dac20SDavid S. Miller }
69836d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames);
699db8dac20SDavid S. Miller 
700db8dac20SDavid S. Miller /**
701f6b9664fSHerbert Xu  * 	udp4_hwcsum  -  handle outgoing HW checksumming
702db8dac20SDavid S. Miller  * 	@skb: 	sk_buff containing the filled-in UDP header
703db8dac20SDavid S. Miller  * 	        (checksum field must be zeroed out)
704f6b9664fSHerbert Xu  *	@src:	source IP address
705f6b9664fSHerbert Xu  *	@dst:	destination IP address
706db8dac20SDavid S. Miller  */
707c26bf4a5SThomas Graf void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
708db8dac20SDavid S. Miller {
709db8dac20SDavid S. Miller 	struct udphdr *uh = udp_hdr(skb);
710f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
711f6b9664fSHerbert Xu 	int len = skb->len - offset;
712f6b9664fSHerbert Xu 	int hlen = len;
713db8dac20SDavid S. Miller 	__wsum csum = 0;
714db8dac20SDavid S. Miller 
715ebbe495fSWANG Cong 	if (!skb_has_frag_list(skb)) {
716db8dac20SDavid S. Miller 		/*
717db8dac20SDavid S. Miller 		 * Only one fragment on the socket.
718db8dac20SDavid S. Miller 		 */
719db8dac20SDavid S. Miller 		skb->csum_start = skb_transport_header(skb) - skb->head;
720db8dac20SDavid S. Miller 		skb->csum_offset = offsetof(struct udphdr, check);
721f6b9664fSHerbert Xu 		uh->check = ~csum_tcpudp_magic(src, dst, len,
722f6b9664fSHerbert Xu 					       IPPROTO_UDP, 0);
723db8dac20SDavid S. Miller 	} else {
724ebbe495fSWANG Cong 		struct sk_buff *frags;
725ebbe495fSWANG Cong 
726db8dac20SDavid S. Miller 		/*
727db8dac20SDavid S. Miller 		 * HW-checksum won't work as there are two or more
728db8dac20SDavid S. Miller 		 * fragments on the socket so that all csums of sk_buffs
729db8dac20SDavid S. Miller 		 * should be together
730db8dac20SDavid S. Miller 		 */
731ebbe495fSWANG Cong 		skb_walk_frags(skb, frags) {
732f6b9664fSHerbert Xu 			csum = csum_add(csum, frags->csum);
733f6b9664fSHerbert Xu 			hlen -= frags->len;
734ebbe495fSWANG Cong 		}
735db8dac20SDavid S. Miller 
736f6b9664fSHerbert Xu 		csum = skb_checksum(skb, offset, hlen, csum);
737db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
738db8dac20SDavid S. Miller 
739db8dac20SDavid S. Miller 		uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
740db8dac20SDavid S. Miller 		if (uh->check == 0)
741db8dac20SDavid S. Miller 			uh->check = CSUM_MANGLED_0;
742db8dac20SDavid S. Miller 	}
743db8dac20SDavid S. Miller }
744c26bf4a5SThomas Graf EXPORT_SYMBOL_GPL(udp4_hwcsum);
745db8dac20SDavid S. Miller 
746af5fcba7STom Herbert /* Function to set UDP checksum for an IPv4 UDP packet. This is intended
747af5fcba7STom Herbert  * for the simple case like when setting the checksum for a UDP tunnel.
748af5fcba7STom Herbert  */
749af5fcba7STom Herbert void udp_set_csum(bool nocheck, struct sk_buff *skb,
750af5fcba7STom Herbert 		  __be32 saddr, __be32 daddr, int len)
751af5fcba7STom Herbert {
752af5fcba7STom Herbert 	struct udphdr *uh = udp_hdr(skb);
753af5fcba7STom Herbert 
754af5fcba7STom Herbert 	if (nocheck)
755af5fcba7STom Herbert 		uh->check = 0;
756af5fcba7STom Herbert 	else if (skb_is_gso(skb))
757af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
758af5fcba7STom Herbert 	else if (skb_dst(skb) && skb_dst(skb)->dev &&
759af5fcba7STom Herbert 		 (skb_dst(skb)->dev->features & NETIF_F_V4_CSUM)) {
760af5fcba7STom Herbert 
761af5fcba7STom Herbert 		BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL);
762af5fcba7STom Herbert 
763af5fcba7STom Herbert 		skb->ip_summed = CHECKSUM_PARTIAL;
764af5fcba7STom Herbert 		skb->csum_start = skb_transport_header(skb) - skb->head;
765af5fcba7STom Herbert 		skb->csum_offset = offsetof(struct udphdr, check);
766af5fcba7STom Herbert 		uh->check = ~udp_v4_check(len, saddr, daddr, 0);
767af5fcba7STom Herbert 	} else {
768af5fcba7STom Herbert 		__wsum csum;
769af5fcba7STom Herbert 
770af5fcba7STom Herbert 		BUG_ON(skb->ip_summed == CHECKSUM_PARTIAL);
771af5fcba7STom Herbert 
772af5fcba7STom Herbert 		uh->check = 0;
773af5fcba7STom Herbert 		csum = skb_checksum(skb, 0, len, 0);
774af5fcba7STom Herbert 		uh->check = udp_v4_check(len, saddr, daddr, csum);
775af5fcba7STom Herbert 		if (uh->check == 0)
776af5fcba7STom Herbert 			uh->check = CSUM_MANGLED_0;
777af5fcba7STom Herbert 
778af5fcba7STom Herbert 		skb->ip_summed = CHECKSUM_UNNECESSARY;
779af5fcba7STom Herbert 	}
780af5fcba7STom Herbert }
781af5fcba7STom Herbert EXPORT_SYMBOL(udp_set_csum);
782af5fcba7STom Herbert 
78379ab0531SDavid S. Miller static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4)
784f6b9664fSHerbert Xu {
785f6b9664fSHerbert Xu 	struct sock *sk = skb->sk;
786f6b9664fSHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
787f6b9664fSHerbert Xu 	struct udphdr *uh;
788f6b9664fSHerbert Xu 	int err = 0;
789f6b9664fSHerbert Xu 	int is_udplite = IS_UDPLITE(sk);
790f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
791f6b9664fSHerbert Xu 	int len = skb->len - offset;
792f6b9664fSHerbert Xu 	__wsum csum = 0;
793f6b9664fSHerbert Xu 
794f6b9664fSHerbert Xu 	/*
795f6b9664fSHerbert Xu 	 * Create a UDP header
796f6b9664fSHerbert Xu 	 */
797f6b9664fSHerbert Xu 	uh = udp_hdr(skb);
798f6b9664fSHerbert Xu 	uh->source = inet->inet_sport;
79979ab0531SDavid S. Miller 	uh->dest = fl4->fl4_dport;
800f6b9664fSHerbert Xu 	uh->len = htons(len);
801f6b9664fSHerbert Xu 	uh->check = 0;
802f6b9664fSHerbert Xu 
803f6b9664fSHerbert Xu 	if (is_udplite)  				 /*     UDP-Lite      */
804f6b9664fSHerbert Xu 		csum = udplite_csum(skb);
805f6b9664fSHerbert Xu 
80628448b80STom Herbert 	else if (sk->sk_no_check_tx) {   /* UDP csum disabled */
807f6b9664fSHerbert Xu 
808f6b9664fSHerbert Xu 		skb->ip_summed = CHECKSUM_NONE;
809f6b9664fSHerbert Xu 		goto send;
810f6b9664fSHerbert Xu 
811f6b9664fSHerbert Xu 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
812f6b9664fSHerbert Xu 
81379ab0531SDavid S. Miller 		udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
814f6b9664fSHerbert Xu 		goto send;
815f6b9664fSHerbert Xu 
816f6b9664fSHerbert Xu 	} else
817f6b9664fSHerbert Xu 		csum = udp_csum(skb);
818f6b9664fSHerbert Xu 
819f6b9664fSHerbert Xu 	/* add protocol-dependent pseudo-header */
82079ab0531SDavid S. Miller 	uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
821f6b9664fSHerbert Xu 				      sk->sk_protocol, csum);
822f6b9664fSHerbert Xu 	if (uh->check == 0)
823f6b9664fSHerbert Xu 		uh->check = CSUM_MANGLED_0;
824f6b9664fSHerbert Xu 
825f6b9664fSHerbert Xu send:
826b5ec8eeaSEric Dumazet 	err = ip_send_skb(sock_net(sk), skb);
827f6b9664fSHerbert Xu 	if (err) {
828f6b9664fSHerbert Xu 		if (err == -ENOBUFS && !inet->recverr) {
829f6b9664fSHerbert Xu 			UDP_INC_STATS_USER(sock_net(sk),
830f6b9664fSHerbert Xu 					   UDP_MIB_SNDBUFERRORS, is_udplite);
831f6b9664fSHerbert Xu 			err = 0;
832f6b9664fSHerbert Xu 		}
833f6b9664fSHerbert Xu 	} else
834f6b9664fSHerbert Xu 		UDP_INC_STATS_USER(sock_net(sk),
835f6b9664fSHerbert Xu 				   UDP_MIB_OUTDATAGRAMS, is_udplite);
836f6b9664fSHerbert Xu 	return err;
837f6b9664fSHerbert Xu }
838f6b9664fSHerbert Xu 
839db8dac20SDavid S. Miller /*
840db8dac20SDavid S. Miller  * Push out all pending data as one UDP datagram. Socket is locked.
841db8dac20SDavid S. Miller  */
8428822b64aSHannes Frederic Sowa int udp_push_pending_frames(struct sock *sk)
843db8dac20SDavid S. Miller {
844db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
845db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
846b6f21b26SDavid S. Miller 	struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
847db8dac20SDavid S. Miller 	struct sk_buff *skb;
848db8dac20SDavid S. Miller 	int err = 0;
849db8dac20SDavid S. Miller 
85077968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
851f6b9664fSHerbert Xu 	if (!skb)
852db8dac20SDavid S. Miller 		goto out;
853db8dac20SDavid S. Miller 
85479ab0531SDavid S. Miller 	err = udp_send_skb(skb, fl4);
855db8dac20SDavid S. Miller 
856db8dac20SDavid S. Miller out:
857db8dac20SDavid S. Miller 	up->len = 0;
858db8dac20SDavid S. Miller 	up->pending = 0;
859db8dac20SDavid S. Miller 	return err;
860db8dac20SDavid S. Miller }
8618822b64aSHannes Frederic Sowa EXPORT_SYMBOL(udp_push_pending_frames);
862db8dac20SDavid S. Miller 
863db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
864db8dac20SDavid S. Miller 		size_t len)
865db8dac20SDavid S. Miller {
866db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
867db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
868e474995fSDavid S. Miller 	struct flowi4 fl4_stack;
869b6f21b26SDavid S. Miller 	struct flowi4 *fl4;
870db8dac20SDavid S. Miller 	int ulen = len;
871db8dac20SDavid S. Miller 	struct ipcm_cookie ipc;
872db8dac20SDavid S. Miller 	struct rtable *rt = NULL;
873db8dac20SDavid S. Miller 	int free = 0;
874db8dac20SDavid S. Miller 	int connected = 0;
875db8dac20SDavid S. Miller 	__be32 daddr, faddr, saddr;
876db8dac20SDavid S. Miller 	__be16 dport;
877db8dac20SDavid S. Miller 	u8  tos;
878db8dac20SDavid S. Miller 	int err, is_udplite = IS_UDPLITE(sk);
879db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
880db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
881903ab86dSHerbert Xu 	struct sk_buff *skb;
882f6d8bd05SEric Dumazet 	struct ip_options_data opt_copy;
883db8dac20SDavid S. Miller 
884db8dac20SDavid S. Miller 	if (len > 0xFFFF)
885db8dac20SDavid S. Miller 		return -EMSGSIZE;
886db8dac20SDavid S. Miller 
887db8dac20SDavid S. Miller 	/*
888db8dac20SDavid S. Miller 	 *	Check the flags.
889db8dac20SDavid S. Miller 	 */
890db8dac20SDavid S. Miller 
891db8dac20SDavid S. Miller 	if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
892db8dac20SDavid S. Miller 		return -EOPNOTSUPP;
893db8dac20SDavid S. Miller 
894db8dac20SDavid S. Miller 	ipc.opt = NULL;
8952244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
896aa661581SFrancesco Fusco 	ipc.ttl = 0;
897aa661581SFrancesco Fusco 	ipc.tos = -1;
898db8dac20SDavid S. Miller 
899903ab86dSHerbert Xu 	getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
900903ab86dSHerbert Xu 
901f5fca608SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
902db8dac20SDavid S. Miller 	if (up->pending) {
903db8dac20SDavid S. Miller 		/*
904db8dac20SDavid S. Miller 		 * There are pending frames.
905db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
906db8dac20SDavid S. Miller 		 */
907db8dac20SDavid S. Miller 		lock_sock(sk);
908db8dac20SDavid S. Miller 		if (likely(up->pending)) {
909db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET)) {
910db8dac20SDavid S. Miller 				release_sock(sk);
911db8dac20SDavid S. Miller 				return -EINVAL;
912db8dac20SDavid S. Miller 			}
913db8dac20SDavid S. Miller 			goto do_append_data;
914db8dac20SDavid S. Miller 		}
915db8dac20SDavid S. Miller 		release_sock(sk);
916db8dac20SDavid S. Miller 	}
917db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
918db8dac20SDavid S. Miller 
919db8dac20SDavid S. Miller 	/*
920db8dac20SDavid S. Miller 	 *	Get and verify the address.
921db8dac20SDavid S. Miller 	 */
922db8dac20SDavid S. Miller 	if (msg->msg_name) {
923342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
924db8dac20SDavid S. Miller 		if (msg->msg_namelen < sizeof(*usin))
925db8dac20SDavid S. Miller 			return -EINVAL;
926db8dac20SDavid S. Miller 		if (usin->sin_family != AF_INET) {
927db8dac20SDavid S. Miller 			if (usin->sin_family != AF_UNSPEC)
928db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
929db8dac20SDavid S. Miller 		}
930db8dac20SDavid S. Miller 
931db8dac20SDavid S. Miller 		daddr = usin->sin_addr.s_addr;
932db8dac20SDavid S. Miller 		dport = usin->sin_port;
933db8dac20SDavid S. Miller 		if (dport == 0)
934db8dac20SDavid S. Miller 			return -EINVAL;
935db8dac20SDavid S. Miller 	} else {
936db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
937db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
938c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
939c720c7e8SEric Dumazet 		dport = inet->inet_dport;
940db8dac20SDavid S. Miller 		/* Open fast path for connected socket.
941db8dac20SDavid S. Miller 		   Route will not be used, if at least one option is set.
942db8dac20SDavid S. Miller 		 */
943db8dac20SDavid S. Miller 		connected = 1;
944db8dac20SDavid S. Miller 	}
945c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
946db8dac20SDavid S. Miller 
947db8dac20SDavid S. Miller 	ipc.oif = sk->sk_bound_dev_if;
948bf84a010SDaniel Borkmann 
949bf84a010SDaniel Borkmann 	sock_tx_timestamp(sk, &ipc.tx_flags);
950bf84a010SDaniel Borkmann 
951db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
952c8e6ad08SHannes Frederic Sowa 		err = ip_cmsg_send(sock_net(sk), msg, &ipc,
953c8e6ad08SHannes Frederic Sowa 				   sk->sk_family == AF_INET6);
954db8dac20SDavid S. Miller 		if (err)
955db8dac20SDavid S. Miller 			return err;
956db8dac20SDavid S. Miller 		if (ipc.opt)
957db8dac20SDavid S. Miller 			free = 1;
958db8dac20SDavid S. Miller 		connected = 0;
959db8dac20SDavid S. Miller 	}
960f6d8bd05SEric Dumazet 	if (!ipc.opt) {
961f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
962f6d8bd05SEric Dumazet 
963f6d8bd05SEric Dumazet 		rcu_read_lock();
964f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference(inet->inet_opt);
965f6d8bd05SEric Dumazet 		if (inet_opt) {
966f6d8bd05SEric Dumazet 			memcpy(&opt_copy, inet_opt,
967f6d8bd05SEric Dumazet 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
968f6d8bd05SEric Dumazet 			ipc.opt = &opt_copy.opt;
969f6d8bd05SEric Dumazet 		}
970f6d8bd05SEric Dumazet 		rcu_read_unlock();
971f6d8bd05SEric Dumazet 	}
972db8dac20SDavid S. Miller 
973db8dac20SDavid S. Miller 	saddr = ipc.addr;
974db8dac20SDavid S. Miller 	ipc.addr = faddr = daddr;
975db8dac20SDavid S. Miller 
976f6d8bd05SEric Dumazet 	if (ipc.opt && ipc.opt->opt.srr) {
977db8dac20SDavid S. Miller 		if (!daddr)
978db8dac20SDavid S. Miller 			return -EINVAL;
979f6d8bd05SEric Dumazet 		faddr = ipc.opt->opt.faddr;
980db8dac20SDavid S. Miller 		connected = 0;
981db8dac20SDavid S. Miller 	}
982aa661581SFrancesco Fusco 	tos = get_rttos(&ipc, inet);
983db8dac20SDavid S. Miller 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
984db8dac20SDavid S. Miller 	    (msg->msg_flags & MSG_DONTROUTE) ||
985f6d8bd05SEric Dumazet 	    (ipc.opt && ipc.opt->opt.is_strictroute)) {
986db8dac20SDavid S. Miller 		tos |= RTO_ONLINK;
987db8dac20SDavid S. Miller 		connected = 0;
988db8dac20SDavid S. Miller 	}
989db8dac20SDavid S. Miller 
990db8dac20SDavid S. Miller 	if (ipv4_is_multicast(daddr)) {
991db8dac20SDavid S. Miller 		if (!ipc.oif)
992db8dac20SDavid S. Miller 			ipc.oif = inet->mc_index;
993db8dac20SDavid S. Miller 		if (!saddr)
994db8dac20SDavid S. Miller 			saddr = inet->mc_addr;
995db8dac20SDavid S. Miller 		connected = 0;
99676e21053SErich E. Hoover 	} else if (!ipc.oif)
99776e21053SErich E. Hoover 		ipc.oif = inet->uc_index;
998db8dac20SDavid S. Miller 
999db8dac20SDavid S. Miller 	if (connected)
1000db8dac20SDavid S. Miller 		rt = (struct rtable *)sk_dst_check(sk, 0);
1001db8dac20SDavid S. Miller 
1002db8dac20SDavid S. Miller 	if (rt == NULL) {
100384a3aa00SPavel Emelyanov 		struct net *net = sock_net(sk);
100484a3aa00SPavel Emelyanov 
1005e474995fSDavid S. Miller 		fl4 = &fl4_stack;
1006e474995fSDavid S. Miller 		flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
1007c0951cbcSDavid S. Miller 				   RT_SCOPE_UNIVERSE, sk->sk_protocol,
10080e0d44abSSteffen Klassert 				   inet_sk_flowi_flags(sk),
1009c0951cbcSDavid S. Miller 				   faddr, saddr, dport, inet->inet_sport);
1010c0951cbcSDavid S. Miller 
1011e474995fSDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
1012e474995fSDavid S. Miller 		rt = ip_route_output_flow(net, fl4, sk);
1013b23dd4feSDavid S. Miller 		if (IS_ERR(rt)) {
1014b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
101506dc94b1SDavid S. Miller 			rt = NULL;
1016db8dac20SDavid S. Miller 			if (err == -ENETUNREACH)
1017f1d8cba6SEric Dumazet 				IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
1018db8dac20SDavid S. Miller 			goto out;
1019db8dac20SDavid S. Miller 		}
1020db8dac20SDavid S. Miller 
1021db8dac20SDavid S. Miller 		err = -EACCES;
1022db8dac20SDavid S. Miller 		if ((rt->rt_flags & RTCF_BROADCAST) &&
1023db8dac20SDavid S. Miller 		    !sock_flag(sk, SOCK_BROADCAST))
1024db8dac20SDavid S. Miller 			goto out;
1025db8dac20SDavid S. Miller 		if (connected)
1026d8d1f30bSChangli Gao 			sk_dst_set(sk, dst_clone(&rt->dst));
1027db8dac20SDavid S. Miller 	}
1028db8dac20SDavid S. Miller 
1029db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1030db8dac20SDavid S. Miller 		goto do_confirm;
1031db8dac20SDavid S. Miller back_from_confirm:
1032db8dac20SDavid S. Miller 
1033e474995fSDavid S. Miller 	saddr = fl4->saddr;
1034db8dac20SDavid S. Miller 	if (!ipc.addr)
1035e474995fSDavid S. Miller 		daddr = ipc.addr = fl4->daddr;
1036db8dac20SDavid S. Miller 
1037903ab86dSHerbert Xu 	/* Lockless fast path for the non-corking case. */
1038903ab86dSHerbert Xu 	if (!corkreq) {
103977968b78SDavid S. Miller 		skb = ip_make_skb(sk, fl4, getfrag, msg->msg_iov, ulen,
1040903ab86dSHerbert Xu 				  sizeof(struct udphdr), &ipc, &rt,
1041903ab86dSHerbert Xu 				  msg->msg_flags);
1042903ab86dSHerbert Xu 		err = PTR_ERR(skb);
104350c3a487SYOSHIFUJI Hideaki / 吉藤英明 		if (!IS_ERR_OR_NULL(skb))
104479ab0531SDavid S. Miller 			err = udp_send_skb(skb, fl4);
1045903ab86dSHerbert Xu 		goto out;
1046903ab86dSHerbert Xu 	}
1047903ab86dSHerbert Xu 
1048db8dac20SDavid S. Miller 	lock_sock(sk);
1049db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1050db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1051db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1052db8dac20SDavid S. Miller 		release_sock(sk);
1053db8dac20SDavid S. Miller 
1054afd46503SJoe Perches 		LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("cork app bug 2\n"));
1055db8dac20SDavid S. Miller 		err = -EINVAL;
1056db8dac20SDavid S. Miller 		goto out;
1057db8dac20SDavid S. Miller 	}
1058db8dac20SDavid S. Miller 	/*
1059db8dac20SDavid S. Miller 	 *	Now cork the socket to pend data.
1060db8dac20SDavid S. Miller 	 */
1061b6f21b26SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
1062b6f21b26SDavid S. Miller 	fl4->daddr = daddr;
1063b6f21b26SDavid S. Miller 	fl4->saddr = saddr;
10649cce96dfSDavid S. Miller 	fl4->fl4_dport = dport;
10659cce96dfSDavid S. Miller 	fl4->fl4_sport = inet->inet_sport;
1066db8dac20SDavid S. Miller 	up->pending = AF_INET;
1067db8dac20SDavid S. Miller 
1068db8dac20SDavid S. Miller do_append_data:
1069db8dac20SDavid S. Miller 	up->len += ulen;
1070f5fca608SDavid S. Miller 	err = ip_append_data(sk, fl4, getfrag, msg->msg_iov, ulen,
10712e77d89bSEric Dumazet 			     sizeof(struct udphdr), &ipc, &rt,
1072db8dac20SDavid S. Miller 			     corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1073db8dac20SDavid S. Miller 	if (err)
1074db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1075db8dac20SDavid S. Miller 	else if (!corkreq)
1076db8dac20SDavid S. Miller 		err = udp_push_pending_frames(sk);
1077db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1078db8dac20SDavid S. Miller 		up->pending = 0;
1079db8dac20SDavid S. Miller 	release_sock(sk);
1080db8dac20SDavid S. Miller 
1081db8dac20SDavid S. Miller out:
1082db8dac20SDavid S. Miller 	ip_rt_put(rt);
1083db8dac20SDavid S. Miller 	if (free)
1084db8dac20SDavid S. Miller 		kfree(ipc.opt);
1085db8dac20SDavid S. Miller 	if (!err)
1086db8dac20SDavid S. Miller 		return len;
1087db8dac20SDavid S. Miller 	/*
1088db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1089db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1090db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1091db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1092db8dac20SDavid S. Miller 	 * seems like overkill.
1093db8dac20SDavid S. Miller 	 */
1094db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1095629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1096629ca23cSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1097db8dac20SDavid S. Miller 	}
1098db8dac20SDavid S. Miller 	return err;
1099db8dac20SDavid S. Miller 
1100db8dac20SDavid S. Miller do_confirm:
1101d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
1102db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1103db8dac20SDavid S. Miller 		goto back_from_confirm;
1104db8dac20SDavid S. Miller 	err = 0;
1105db8dac20SDavid S. Miller 	goto out;
1106db8dac20SDavid S. Miller }
1107c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg);
1108db8dac20SDavid S. Miller 
1109db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset,
1110db8dac20SDavid S. Miller 		 size_t size, int flags)
1111db8dac20SDavid S. Miller {
1112f5fca608SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1113db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1114db8dac20SDavid S. Miller 	int ret;
1115db8dac20SDavid S. Miller 
1116d3f7d56aSShawn Landden 	if (flags & MSG_SENDPAGE_NOTLAST)
1117d3f7d56aSShawn Landden 		flags |= MSG_MORE;
1118d3f7d56aSShawn Landden 
1119db8dac20SDavid S. Miller 	if (!up->pending) {
1120db8dac20SDavid S. Miller 		struct msghdr msg = {	.msg_flags = flags|MSG_MORE };
1121db8dac20SDavid S. Miller 
1122db8dac20SDavid S. Miller 		/* Call udp_sendmsg to specify destination address which
1123db8dac20SDavid S. Miller 		 * sendpage interface can't pass.
1124db8dac20SDavid S. Miller 		 * This will succeed only when the socket is connected.
1125db8dac20SDavid S. Miller 		 */
1126db8dac20SDavid S. Miller 		ret = udp_sendmsg(NULL, sk, &msg, 0);
1127db8dac20SDavid S. Miller 		if (ret < 0)
1128db8dac20SDavid S. Miller 			return ret;
1129db8dac20SDavid S. Miller 	}
1130db8dac20SDavid S. Miller 
1131db8dac20SDavid S. Miller 	lock_sock(sk);
1132db8dac20SDavid S. Miller 
1133db8dac20SDavid S. Miller 	if (unlikely(!up->pending)) {
1134db8dac20SDavid S. Miller 		release_sock(sk);
1135db8dac20SDavid S. Miller 
1136afd46503SJoe Perches 		LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("udp cork app bug 3\n"));
1137db8dac20SDavid S. Miller 		return -EINVAL;
1138db8dac20SDavid S. Miller 	}
1139db8dac20SDavid S. Miller 
1140f5fca608SDavid S. Miller 	ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
1141f5fca608SDavid S. Miller 			     page, offset, size, flags);
1142db8dac20SDavid S. Miller 	if (ret == -EOPNOTSUPP) {
1143db8dac20SDavid S. Miller 		release_sock(sk);
1144db8dac20SDavid S. Miller 		return sock_no_sendpage(sk->sk_socket, page, offset,
1145db8dac20SDavid S. Miller 					size, flags);
1146db8dac20SDavid S. Miller 	}
1147db8dac20SDavid S. Miller 	if (ret < 0) {
1148db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1149db8dac20SDavid S. Miller 		goto out;
1150db8dac20SDavid S. Miller 	}
1151db8dac20SDavid S. Miller 
1152db8dac20SDavid S. Miller 	up->len += size;
1153db8dac20SDavid S. Miller 	if (!(up->corkflag || (flags&MSG_MORE)))
1154db8dac20SDavid S. Miller 		ret = udp_push_pending_frames(sk);
1155db8dac20SDavid S. Miller 	if (!ret)
1156db8dac20SDavid S. Miller 		ret = size;
1157db8dac20SDavid S. Miller out:
1158db8dac20SDavid S. Miller 	release_sock(sk);
1159db8dac20SDavid S. Miller 	return ret;
1160db8dac20SDavid S. Miller }
1161db8dac20SDavid S. Miller 
116285584672SEric Dumazet 
116385584672SEric Dumazet /**
116485584672SEric Dumazet  *	first_packet_length	- return length of first packet in receive queue
116585584672SEric Dumazet  *	@sk: socket
116685584672SEric Dumazet  *
116785584672SEric Dumazet  *	Drops all bad checksum frames, until a valid one is found.
116885584672SEric Dumazet  *	Returns the length of found skb, or 0 if none is found.
116985584672SEric Dumazet  */
117085584672SEric Dumazet static unsigned int first_packet_length(struct sock *sk)
117185584672SEric Dumazet {
117285584672SEric Dumazet 	struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
117385584672SEric Dumazet 	struct sk_buff *skb;
117485584672SEric Dumazet 	unsigned int res;
117585584672SEric Dumazet 
117685584672SEric Dumazet 	__skb_queue_head_init(&list_kill);
117785584672SEric Dumazet 
117885584672SEric Dumazet 	spin_lock_bh(&rcvq->lock);
117985584672SEric Dumazet 	while ((skb = skb_peek(rcvq)) != NULL &&
118085584672SEric Dumazet 		udp_lib_checksum_complete(skb)) {
11816a5dc9e5SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS,
11826a5dc9e5SEric Dumazet 				 IS_UDPLITE(sk));
118385584672SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
118485584672SEric Dumazet 				 IS_UDPLITE(sk));
11858edf19c2SEric Dumazet 		atomic_inc(&sk->sk_drops);
118685584672SEric Dumazet 		__skb_unlink(skb, rcvq);
118785584672SEric Dumazet 		__skb_queue_tail(&list_kill, skb);
118885584672SEric Dumazet 	}
118985584672SEric Dumazet 	res = skb ? skb->len : 0;
119085584672SEric Dumazet 	spin_unlock_bh(&rcvq->lock);
119185584672SEric Dumazet 
119285584672SEric Dumazet 	if (!skb_queue_empty(&list_kill)) {
11938a74ad60SEric Dumazet 		bool slow = lock_sock_fast(sk);
11948a74ad60SEric Dumazet 
119585584672SEric Dumazet 		__skb_queue_purge(&list_kill);
119685584672SEric Dumazet 		sk_mem_reclaim_partial(sk);
11978a74ad60SEric Dumazet 		unlock_sock_fast(sk, slow);
119885584672SEric Dumazet 	}
119985584672SEric Dumazet 	return res;
120085584672SEric Dumazet }
120185584672SEric Dumazet 
12021da177e4SLinus Torvalds /*
12031da177e4SLinus Torvalds  *	IOCTL requests applicable to the UDP protocol
12041da177e4SLinus Torvalds  */
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
12071da177e4SLinus Torvalds {
12086516c655SStephen Hemminger 	switch (cmd) {
12091da177e4SLinus Torvalds 	case SIOCOUTQ:
12101da177e4SLinus Torvalds 	{
121131e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
121231e6d363SEric Dumazet 
12131da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	case SIOCINQ:
12171da177e4SLinus Torvalds 	{
121885584672SEric Dumazet 		unsigned int amount = first_packet_length(sk);
12191da177e4SLinus Torvalds 
122085584672SEric Dumazet 		if (amount)
12211da177e4SLinus Torvalds 			/*
12221da177e4SLinus Torvalds 			 * We will only return the amount
12231da177e4SLinus Torvalds 			 * of this packet since that is all
12241da177e4SLinus Torvalds 			 * that will be read.
12251da177e4SLinus Torvalds 			 */
122685584672SEric Dumazet 			amount -= sizeof(struct udphdr);
122785584672SEric Dumazet 
12281da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	default:
12321da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
12331da177e4SLinus Torvalds 	}
12346516c655SStephen Hemminger 
12356516c655SStephen Hemminger 	return 0;
12361da177e4SLinus Torvalds }
1237c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl);
12381da177e4SLinus Torvalds 
1239db8dac20SDavid S. Miller /*
1240db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
1241db8dac20SDavid S. Miller  * 	return it, otherwise we block.
1242db8dac20SDavid S. Miller  */
1243db8dac20SDavid S. Miller 
1244db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1245db8dac20SDavid S. Miller 		size_t len, int noblock, int flags, int *addr_len)
1246db8dac20SDavid S. Miller {
1247db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1248342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
1249db8dac20SDavid S. Miller 	struct sk_buff *skb;
125059c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
12513f518bf7SPavel Emelyanov 	int peeked, off = 0;
1252db8dac20SDavid S. Miller 	int err;
1253db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
12548a74ad60SEric Dumazet 	bool slow;
1255db8dac20SDavid S. Miller 
1256db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
125785fbaa75SHannes Frederic Sowa 		return ip_recv_error(sk, msg, len, addr_len);
1258db8dac20SDavid S. Miller 
1259db8dac20SDavid S. Miller try_again:
1260db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
12613f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
1262db8dac20SDavid S. Miller 	if (!skb)
1263db8dac20SDavid S. Miller 		goto out;
1264db8dac20SDavid S. Miller 
1265db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
126659c2cdaeSDavid S. Miller 	copied = len;
126759c2cdaeSDavid S. Miller 	if (copied > ulen)
126859c2cdaeSDavid S. Miller 		copied = ulen;
126959c2cdaeSDavid S. Miller 	else if (copied < ulen)
1270db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
1271db8dac20SDavid S. Miller 
1272db8dac20SDavid S. Miller 	/*
1273db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
1274db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
1275db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
1276db8dac20SDavid S. Miller 	 */
1277db8dac20SDavid S. Miller 
127859c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
1279db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
1280db8dac20SDavid S. Miller 			goto csum_copy_err;
1281db8dac20SDavid S. Miller 	}
1282db8dac20SDavid S. Miller 
1283db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
1284db8dac20SDavid S. Miller 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
128559c2cdaeSDavid S. Miller 					      msg->msg_iov, copied);
1286db8dac20SDavid S. Miller 	else {
1287c482c568SEric Dumazet 		err = skb_copy_and_csum_datagram_iovec(skb,
1288c482c568SEric Dumazet 						       sizeof(struct udphdr),
1289c482c568SEric Dumazet 						       msg->msg_iov);
1290db8dac20SDavid S. Miller 
1291db8dac20SDavid S. Miller 		if (err == -EINVAL)
1292db8dac20SDavid S. Miller 			goto csum_copy_err;
1293db8dac20SDavid S. Miller 	}
1294db8dac20SDavid S. Miller 
129522911fc5SEric Dumazet 	if (unlikely(err)) {
129622911fc5SEric Dumazet 		trace_kfree_skb(skb, udp_recvmsg);
1297979402b1SEric Dumazet 		if (!peeked) {
1298979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
1299979402b1SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
1300979402b1SEric Dumazet 					   UDP_MIB_INERRORS, is_udplite);
1301979402b1SEric Dumazet 		}
1302db8dac20SDavid S. Miller 		goto out_free;
130322911fc5SEric Dumazet 	}
1304db8dac20SDavid S. Miller 
1305db8dac20SDavid S. Miller 	if (!peeked)
1306629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1307629ca23cSPavel Emelyanov 				UDP_MIB_INDATAGRAMS, is_udplite);
1308db8dac20SDavid S. Miller 
13093b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
1310db8dac20SDavid S. Miller 
1311db8dac20SDavid S. Miller 	/* Copy the address. */
1312c482c568SEric Dumazet 	if (sin) {
1313db8dac20SDavid S. Miller 		sin->sin_family = AF_INET;
1314db8dac20SDavid S. Miller 		sin->sin_port = udp_hdr(skb)->source;
1315db8dac20SDavid S. Miller 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1316db8dac20SDavid S. Miller 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1317bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin);
1318db8dac20SDavid S. Miller 	}
1319db8dac20SDavid S. Miller 	if (inet->cmsg_flags)
1320db8dac20SDavid S. Miller 		ip_cmsg_recv(msg, skb);
1321db8dac20SDavid S. Miller 
132259c2cdaeSDavid S. Miller 	err = copied;
1323db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
1324db8dac20SDavid S. Miller 		err = ulen;
1325db8dac20SDavid S. Miller 
1326db8dac20SDavid S. Miller out_free:
13279d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
1328db8dac20SDavid S. Miller out:
1329db8dac20SDavid S. Miller 	return err;
1330db8dac20SDavid S. Miller 
1331db8dac20SDavid S. Miller csum_copy_err:
13328a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
13336a5dc9e5SEric Dumazet 	if (!skb_kill_datagram(sk, skb, flags)) {
13346a5dc9e5SEric Dumazet 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1335629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
13366a5dc9e5SEric Dumazet 	}
13378a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
1338db8dac20SDavid S. Miller 
1339db8dac20SDavid S. Miller 	if (noblock)
1340db8dac20SDavid S. Miller 		return -EAGAIN;
13419cfaa8deSXufeng Zhang 
13429cfaa8deSXufeng Zhang 	/* starting over for a new packet */
13439cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
1344db8dac20SDavid S. Miller 	goto try_again;
1345db8dac20SDavid S. Miller }
1346db8dac20SDavid S. Miller 
1347db8dac20SDavid S. Miller 
13481da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags)
13491da177e4SLinus Torvalds {
13501da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
13511da177e4SLinus Torvalds 	/*
13521da177e4SLinus Torvalds 	 *	1003.1g - break association.
13531da177e4SLinus Torvalds 	 */
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
1356c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
1357c720c7e8SEric Dumazet 	inet->inet_dport = 0;
1358bdeab991STom Herbert 	sock_rps_reset_rxhash(sk);
13591da177e4SLinus Torvalds 	sk->sk_bound_dev_if = 0;
13601da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
13611da177e4SLinus Torvalds 		inet_reset_saddr(sk);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
13641da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1365c720c7e8SEric Dumazet 		inet->inet_sport = 0;
13661da177e4SLinus Torvalds 	}
13671da177e4SLinus Torvalds 	sk_dst_reset(sk);
13681da177e4SLinus Torvalds 	return 0;
13691da177e4SLinus Torvalds }
1370c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect);
13711da177e4SLinus Torvalds 
1372645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk)
1373645ca708SEric Dumazet {
1374723b4610SEric Dumazet 	if (sk_hashed(sk)) {
1375645ca708SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1376512615b6SEric Dumazet 		struct udp_hslot *hslot, *hslot2;
1377512615b6SEric Dumazet 
1378512615b6SEric Dumazet 		hslot  = udp_hashslot(udptable, sock_net(sk),
1379d4cada4aSEric Dumazet 				      udp_sk(sk)->udp_port_hash);
1380512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1381645ca708SEric Dumazet 
1382c8db3fecSEric Dumazet 		spin_lock_bh(&hslot->lock);
138388ab1932SEric Dumazet 		if (sk_nulls_del_node_init_rcu(sk)) {
1384fdcc8aa9SEric Dumazet 			hslot->count--;
1385c720c7e8SEric Dumazet 			inet_sk(sk)->inet_num = 0;
1386645ca708SEric Dumazet 			sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1387512615b6SEric Dumazet 
1388512615b6SEric Dumazet 			spin_lock(&hslot2->lock);
1389512615b6SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1390512615b6SEric Dumazet 			hslot2->count--;
1391512615b6SEric Dumazet 			spin_unlock(&hslot2->lock);
1392645ca708SEric Dumazet 		}
1393c8db3fecSEric Dumazet 		spin_unlock_bh(&hslot->lock);
1394645ca708SEric Dumazet 	}
1395723b4610SEric Dumazet }
1396645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash);
1397645ca708SEric Dumazet 
1398719f8358SEric Dumazet /*
1399719f8358SEric Dumazet  * inet_rcv_saddr was changed, we must rehash secondary hash
1400719f8358SEric Dumazet  */
1401719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash)
1402719f8358SEric Dumazet {
1403719f8358SEric Dumazet 	if (sk_hashed(sk)) {
1404719f8358SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1405719f8358SEric Dumazet 		struct udp_hslot *hslot, *hslot2, *nhslot2;
1406719f8358SEric Dumazet 
1407719f8358SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1408719f8358SEric Dumazet 		nhslot2 = udp_hashslot2(udptable, newhash);
1409719f8358SEric Dumazet 		udp_sk(sk)->udp_portaddr_hash = newhash;
1410719f8358SEric Dumazet 		if (hslot2 != nhslot2) {
1411719f8358SEric Dumazet 			hslot = udp_hashslot(udptable, sock_net(sk),
1412719f8358SEric Dumazet 					     udp_sk(sk)->udp_port_hash);
1413719f8358SEric Dumazet 			/* we must lock primary chain too */
1414719f8358SEric Dumazet 			spin_lock_bh(&hslot->lock);
1415719f8358SEric Dumazet 
1416719f8358SEric Dumazet 			spin_lock(&hslot2->lock);
1417719f8358SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1418719f8358SEric Dumazet 			hslot2->count--;
1419719f8358SEric Dumazet 			spin_unlock(&hslot2->lock);
1420719f8358SEric Dumazet 
1421719f8358SEric Dumazet 			spin_lock(&nhslot2->lock);
1422719f8358SEric Dumazet 			hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
1423719f8358SEric Dumazet 						 &nhslot2->head);
1424719f8358SEric Dumazet 			nhslot2->count++;
1425719f8358SEric Dumazet 			spin_unlock(&nhslot2->lock);
1426719f8358SEric Dumazet 
1427719f8358SEric Dumazet 			spin_unlock_bh(&hslot->lock);
1428719f8358SEric Dumazet 		}
1429719f8358SEric Dumazet 	}
1430719f8358SEric Dumazet }
1431719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash);
1432719f8358SEric Dumazet 
1433719f8358SEric Dumazet static void udp_v4_rehash(struct sock *sk)
1434719f8358SEric Dumazet {
1435719f8358SEric Dumazet 	u16 new_hash = udp4_portaddr_hash(sock_net(sk),
1436719f8358SEric Dumazet 					  inet_sk(sk)->inet_rcv_saddr,
1437719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
1438719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
1439719f8358SEric Dumazet }
1440719f8358SEric Dumazet 
144193821778SHerbert Xu static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
144293821778SHerbert Xu {
1443fec5e652STom Herbert 	int rc;
144493821778SHerbert Xu 
1445005ec974SShawn Bohrer 	if (inet_sk(sk)->inet_daddr) {
1446bdeab991STom Herbert 		sock_rps_save_rxhash(sk, skb);
1447005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
1448005ec974SShawn Bohrer 	}
1449fec5e652STom Herbert 
1450d826eb14SEric Dumazet 	rc = sock_queue_rcv_skb(sk, skb);
1451766e9037SEric Dumazet 	if (rc < 0) {
1452766e9037SEric Dumazet 		int is_udplite = IS_UDPLITE(sk);
1453766e9037SEric Dumazet 
145493821778SHerbert Xu 		/* Note that an ENOMEM error is charged twice */
1455766e9037SEric Dumazet 		if (rc == -ENOMEM)
145693821778SHerbert Xu 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
145793821778SHerbert Xu 					 is_udplite);
1458766e9037SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1459766e9037SEric Dumazet 		kfree_skb(skb);
1460296f7ea7SSatoru Moriya 		trace_udp_fail_queue_rcv_skb(rc, sk);
1461766e9037SEric Dumazet 		return -1;
146293821778SHerbert Xu 	}
146393821778SHerbert Xu 
146493821778SHerbert Xu 	return 0;
146593821778SHerbert Xu 
146693821778SHerbert Xu }
146793821778SHerbert Xu 
1468447167bfSEric Dumazet static struct static_key udp_encap_needed __read_mostly;
1469447167bfSEric Dumazet void udp_encap_enable(void)
1470447167bfSEric Dumazet {
1471447167bfSEric Dumazet 	if (!static_key_enabled(&udp_encap_needed))
1472447167bfSEric Dumazet 		static_key_slow_inc(&udp_encap_needed);
1473447167bfSEric Dumazet }
1474447167bfSEric Dumazet EXPORT_SYMBOL(udp_encap_enable);
1475447167bfSEric Dumazet 
1476db8dac20SDavid S. Miller /* returns:
1477db8dac20SDavid S. Miller  *  -1: error
1478db8dac20SDavid S. Miller  *   0: success
1479db8dac20SDavid S. Miller  *  >0: "udp encap" protocol resubmission
1480db8dac20SDavid S. Miller  *
1481db8dac20SDavid S. Miller  * Note that in the success and error cases, the skb is assumed to
1482db8dac20SDavid S. Miller  * have either been requeued or freed.
1483db8dac20SDavid S. Miller  */
1484db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1485db8dac20SDavid S. Miller {
1486db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1487db8dac20SDavid S. Miller 	int rc;
1488db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1489db8dac20SDavid S. Miller 
1490db8dac20SDavid S. Miller 	/*
1491db8dac20SDavid S. Miller 	 *	Charge it to the socket, dropping if the queue is full.
1492db8dac20SDavid S. Miller 	 */
1493db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1494db8dac20SDavid S. Miller 		goto drop;
1495db8dac20SDavid S. Miller 	nf_reset(skb);
1496db8dac20SDavid S. Miller 
1497447167bfSEric Dumazet 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
14980ad92ad0SEric Dumazet 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
14990ad92ad0SEric Dumazet 
1500db8dac20SDavid S. Miller 		/*
1501db8dac20SDavid S. Miller 		 * This is an encapsulation socket so pass the skb to
1502db8dac20SDavid S. Miller 		 * the socket's udp_encap_rcv() hook. Otherwise, just
1503db8dac20SDavid S. Miller 		 * fall through and pass this up the UDP socket.
1504db8dac20SDavid S. Miller 		 * up->encap_rcv() returns the following value:
1505db8dac20SDavid S. Miller 		 * =0 if skb was successfully passed to the encap
1506db8dac20SDavid S. Miller 		 *    handler or was discarded by it.
1507db8dac20SDavid S. Miller 		 * >0 if skb should be passed on to UDP.
1508db8dac20SDavid S. Miller 		 * <0 if skb should be resubmitted as proto -N
1509db8dac20SDavid S. Miller 		 */
1510db8dac20SDavid S. Miller 
1511db8dac20SDavid S. Miller 		/* if we're overly short, let UDP handle it */
15120ad92ad0SEric Dumazet 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
15130ad92ad0SEric Dumazet 		if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) {
1514db8dac20SDavid S. Miller 			int ret;
1515db8dac20SDavid S. Miller 
15160a80966bSTom Herbert 			/* Verify checksum before giving to encap */
15170a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
15180a80966bSTom Herbert 				goto csum_error;
15190a80966bSTom Herbert 
15200ad92ad0SEric Dumazet 			ret = encap_rcv(sk, skb);
1521db8dac20SDavid S. Miller 			if (ret <= 0) {
15220283328eSPavel Emelyanov 				UDP_INC_STATS_BH(sock_net(sk),
15230283328eSPavel Emelyanov 						 UDP_MIB_INDATAGRAMS,
1524db8dac20SDavid S. Miller 						 is_udplite);
1525db8dac20SDavid S. Miller 				return -ret;
1526db8dac20SDavid S. Miller 			}
1527db8dac20SDavid S. Miller 		}
1528db8dac20SDavid S. Miller 
1529db8dac20SDavid S. Miller 		/* FALLTHROUGH -- it's a UDP Packet */
1530db8dac20SDavid S. Miller 	}
1531db8dac20SDavid S. Miller 
1532db8dac20SDavid S. Miller 	/*
1533db8dac20SDavid S. Miller 	 * 	UDP-Lite specific tests, ignored on UDP sockets
1534db8dac20SDavid S. Miller 	 */
1535db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
1536db8dac20SDavid S. Miller 
1537db8dac20SDavid S. Miller 		/*
1538db8dac20SDavid S. Miller 		 * MIB statistics other than incrementing the error count are
1539db8dac20SDavid S. Miller 		 * disabled for the following two types of errors: these depend
1540db8dac20SDavid S. Miller 		 * on the application settings, not on the functioning of the
1541db8dac20SDavid S. Miller 		 * protocol stack as such.
1542db8dac20SDavid S. Miller 		 *
1543db8dac20SDavid S. Miller 		 * RFC 3828 here recommends (sec 3.3): "There should also be a
1544db8dac20SDavid S. Miller 		 * way ... to ... at least let the receiving application block
1545db8dac20SDavid S. Miller 		 * delivery of packets with coverage values less than a value
1546db8dac20SDavid S. Miller 		 * provided by the application."
1547db8dac20SDavid S. Miller 		 */
1548db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
1549afd46503SJoe Perches 			LIMIT_NETDEBUG(KERN_WARNING "UDPLite: partial coverage %d while full coverage %d requested\n",
1550db8dac20SDavid S. Miller 				       UDP_SKB_CB(skb)->cscov, skb->len);
1551db8dac20SDavid S. Miller 			goto drop;
1552db8dac20SDavid S. Miller 		}
1553db8dac20SDavid S. Miller 		/* The next case involves violating the min. coverage requested
1554db8dac20SDavid S. Miller 		 * by the receiver. This is subtle: if receiver wants x and x is
1555db8dac20SDavid S. Miller 		 * greater than the buffersize/MTU then receiver will complain
1556db8dac20SDavid S. Miller 		 * that it wants x while sender emits packets of smaller size y.
1557db8dac20SDavid S. Miller 		 * Therefore the above ...()->partial_cov statement is essential.
1558db8dac20SDavid S. Miller 		 */
1559db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
1560afd46503SJoe Perches 			LIMIT_NETDEBUG(KERN_WARNING "UDPLite: coverage %d too small, need min %d\n",
1561db8dac20SDavid S. Miller 				       UDP_SKB_CB(skb)->cscov, up->pcrlen);
1562db8dac20SDavid S. Miller 			goto drop;
1563db8dac20SDavid S. Miller 		}
1564db8dac20SDavid S. Miller 	}
1565db8dac20SDavid S. Miller 
156633d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
156733d480ceSEric Dumazet 	    udp_lib_checksum_complete(skb))
15686a5dc9e5SEric Dumazet 		goto csum_error;
1569db8dac20SDavid S. Miller 
1570c377411fSEric Dumazet 
1571274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
15723e215c8dSJames M Leddy 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
15733e215c8dSJames M Leddy 				 is_udplite);
1574c377411fSEric Dumazet 		goto drop;
15753e215c8dSJames M Leddy 	}
1576c377411fSEric Dumazet 
157793821778SHerbert Xu 	rc = 0;
1578db8dac20SDavid S. Miller 
1579fbf8866dSShawn Bohrer 	ipv4_pktinfo_prepare(sk, skb);
158093821778SHerbert Xu 	bh_lock_sock(sk);
158193821778SHerbert Xu 	if (!sock_owned_by_user(sk))
158293821778SHerbert Xu 		rc = __udp_queue_rcv_skb(sk, skb);
1583f545a38fSEric Dumazet 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
158455349790SZhu Yi 		bh_unlock_sock(sk);
158555349790SZhu Yi 		goto drop;
158655349790SZhu Yi 	}
158793821778SHerbert Xu 	bh_unlock_sock(sk);
158893821778SHerbert Xu 
158993821778SHerbert Xu 	return rc;
1590db8dac20SDavid S. Miller 
15916a5dc9e5SEric Dumazet csum_error:
15926a5dc9e5SEric Dumazet 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1593db8dac20SDavid S. Miller drop:
15940283328eSPavel Emelyanov 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
15958edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
1596db8dac20SDavid S. Miller 	kfree_skb(skb);
1597db8dac20SDavid S. Miller 	return -1;
1598db8dac20SDavid S. Miller }
1599db8dac20SDavid S. Miller 
16001240d137SEric Dumazet 
16011240d137SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
16021240d137SEric Dumazet 			struct sk_buff *skb, unsigned int final)
16031240d137SEric Dumazet {
16041240d137SEric Dumazet 	unsigned int i;
16051240d137SEric Dumazet 	struct sk_buff *skb1 = NULL;
1606f6b8f32cSEric Dumazet 	struct sock *sk;
16071240d137SEric Dumazet 
16081240d137SEric Dumazet 	for (i = 0; i < count; i++) {
1609f6b8f32cSEric Dumazet 		sk = stack[i];
16101240d137SEric Dumazet 		if (likely(skb1 == NULL))
16111240d137SEric Dumazet 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
16121240d137SEric Dumazet 
1613f6b8f32cSEric Dumazet 		if (!skb1) {
1614f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
1615f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
1616f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1617f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
1618f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1619f6b8f32cSEric Dumazet 		}
1620f6b8f32cSEric Dumazet 
1621f6b8f32cSEric Dumazet 		if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0)
16221240d137SEric Dumazet 			skb1 = NULL;
16232dc41cffSDavid Held 
16242dc41cffSDavid Held 		sock_put(sk);
16251240d137SEric Dumazet 	}
16261240d137SEric Dumazet 	if (unlikely(skb1))
16271240d137SEric Dumazet 		kfree_skb(skb1);
16281240d137SEric Dumazet }
16291240d137SEric Dumazet 
163097502231SEric Dumazet /* For TCP sockets, sk_rx_dst is protected by socket lock
1631e47eb5dfSEric Dumazet  * For UDP, we use xchg() to guard against concurrent changes.
163297502231SEric Dumazet  */
163397502231SEric Dumazet static void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
1634421b3885SShawn Bohrer {
163597502231SEric Dumazet 	struct dst_entry *old;
1636421b3885SShawn Bohrer 
1637421b3885SShawn Bohrer 	dst_hold(dst);
1638e47eb5dfSEric Dumazet 	old = xchg(&sk->sk_rx_dst, dst);
163997502231SEric Dumazet 	dst_release(old);
164097502231SEric Dumazet }
1641421b3885SShawn Bohrer 
1642db8dac20SDavid S. Miller /*
1643db8dac20SDavid S. Miller  *	Multicasts and broadcasts go to each listener.
1644db8dac20SDavid S. Miller  *
16451240d137SEric Dumazet  *	Note: called only from the BH handler context.
1646db8dac20SDavid S. Miller  */
1647e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
1648db8dac20SDavid S. Miller 				    struct udphdr  *uh,
1649db8dac20SDavid S. Miller 				    __be32 saddr, __be32 daddr,
1650645ca708SEric Dumazet 				    struct udp_table *udptable)
1651db8dac20SDavid S. Miller {
16521240d137SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
16535cf3d461SDavid Held 	struct hlist_nulls_node *node;
16545cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
16555cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
16565cf3d461SDavid Held 	int dif = skb->dev->ifindex;
16572dc41cffSDavid Held 	unsigned int count = 0, offset = offsetof(typeof(*sk), sk_nulls_node);
16582dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
16592dc41cffSDavid Held 
16602dc41cffSDavid Held 	if (use_hash2) {
16612dc41cffSDavid Held 		hash2_any = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum) &
16622dc41cffSDavid Held 			    udp_table.mask;
16632dc41cffSDavid Held 		hash2 = udp4_portaddr_hash(net, daddr, hnum) & udp_table.mask;
16642dc41cffSDavid Held start_lookup:
16652dc41cffSDavid Held 		hslot = &udp_table.hash2[hash2];
16662dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
16672dc41cffSDavid Held 	}
1668db8dac20SDavid S. Miller 
1669645ca708SEric Dumazet 	spin_lock(&hslot->lock);
16702dc41cffSDavid Held 	sk_nulls_for_each_entry_offset(sk, node, &hslot->head, offset) {
16715cf3d461SDavid Held 		if (__udp_is_mcast_sock(net, sk,
16725cf3d461SDavid Held 					uh->dest, daddr,
16735cf3d461SDavid Held 					uh->source, saddr,
16745cf3d461SDavid Held 					dif, hnum)) {
16751240d137SEric Dumazet 			if (unlikely(count == ARRAY_SIZE(stack))) {
16761240d137SEric Dumazet 				flush_stack(stack, count, skb, ~0);
16771240d137SEric Dumazet 				count = 0;
1678db8dac20SDavid S. Miller 			}
16795cf3d461SDavid Held 			stack[count++] = sk;
16802dc41cffSDavid Held 			sock_hold(sk);
16815cf3d461SDavid Held 		}
16821240d137SEric Dumazet 	}
16831240d137SEric Dumazet 
1684645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
16851240d137SEric Dumazet 
16862dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
16872dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
16882dc41cffSDavid Held 		hash2 = hash2_any;
16892dc41cffSDavid Held 		goto start_lookup;
16902dc41cffSDavid Held 	}
16912dc41cffSDavid Held 
16921240d137SEric Dumazet 	/*
16931240d137SEric Dumazet 	 * do the slow work with no lock held
16941240d137SEric Dumazet 	 */
16951240d137SEric Dumazet 	if (count) {
16961240d137SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
16971240d137SEric Dumazet 	} else {
16981240d137SEric Dumazet 		kfree_skb(skb);
16991240d137SEric Dumazet 	}
1700db8dac20SDavid S. Miller 	return 0;
1701db8dac20SDavid S. Miller }
1702db8dac20SDavid S. Miller 
1703db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success),
1704db8dac20SDavid S. Miller  * CHECKSUM_UNNECESSARY means, that no more checks are required.
1705db8dac20SDavid S. Miller  * Otherwise, csum completion requires chacksumming packet body,
1706db8dac20SDavid S. Miller  * including udp header and folding it to skb->csum.
1707db8dac20SDavid S. Miller  */
1708db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1709db8dac20SDavid S. Miller 				 int proto)
1710db8dac20SDavid S. Miller {
1711db8dac20SDavid S. Miller 	int err;
1712db8dac20SDavid S. Miller 
1713db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
1714db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
1715db8dac20SDavid S. Miller 
1716db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
1717db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
1718db8dac20SDavid S. Miller 		if (err)
1719db8dac20SDavid S. Miller 			return err;
1720db8dac20SDavid S. Miller 	}
1721db8dac20SDavid S. Miller 
1722ed70fcfcSTom Herbert 	return skb_checksum_init_zero_check(skb, proto, uh->check,
1723ed70fcfcSTom Herbert 					    inet_compute_pseudo);
1724db8dac20SDavid S. Miller }
1725db8dac20SDavid S. Miller 
1726db8dac20SDavid S. Miller /*
1727db8dac20SDavid S. Miller  *	All we need to do is get the socket, and then do a checksum.
1728db8dac20SDavid S. Miller  */
1729db8dac20SDavid S. Miller 
1730645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1731db8dac20SDavid S. Miller 		   int proto)
1732db8dac20SDavid S. Miller {
1733db8dac20SDavid S. Miller 	struct sock *sk;
17347b5e56f9SJesper Dangaard Brouer 	struct udphdr *uh;
1735db8dac20SDavid S. Miller 	unsigned short ulen;
1736adf30907SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
17372783ef23SJesper Dangaard Brouer 	__be32 saddr, daddr;
17380283328eSPavel Emelyanov 	struct net *net = dev_net(skb->dev);
1739db8dac20SDavid S. Miller 
1740db8dac20SDavid S. Miller 	/*
1741db8dac20SDavid S. Miller 	 *  Validate the packet.
1742db8dac20SDavid S. Miller 	 */
1743db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1744db8dac20SDavid S. Miller 		goto drop;		/* No space for header. */
1745db8dac20SDavid S. Miller 
17467b5e56f9SJesper Dangaard Brouer 	uh   = udp_hdr(skb);
1747db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
1748ccc2d97cSBjørn Mork 	saddr = ip_hdr(skb)->saddr;
1749ccc2d97cSBjørn Mork 	daddr = ip_hdr(skb)->daddr;
1750ccc2d97cSBjørn Mork 
1751db8dac20SDavid S. Miller 	if (ulen > skb->len)
1752db8dac20SDavid S. Miller 		goto short_packet;
1753db8dac20SDavid S. Miller 
1754db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
1755db8dac20SDavid S. Miller 		/* UDP validates ulen. */
1756db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1757db8dac20SDavid S. Miller 			goto short_packet;
1758db8dac20SDavid S. Miller 		uh = udp_hdr(skb);
1759db8dac20SDavid S. Miller 	}
1760db8dac20SDavid S. Miller 
1761db8dac20SDavid S. Miller 	if (udp4_csum_init(skb, uh, proto))
1762db8dac20SDavid S. Miller 		goto csum_error;
1763db8dac20SDavid S. Miller 
17648afdd99aSEric Dumazet 	sk = skb_steal_sock(skb);
17658afdd99aSEric Dumazet 	if (sk) {
176697502231SEric Dumazet 		struct dst_entry *dst = skb_dst(skb);
1767421b3885SShawn Bohrer 		int ret;
1768421b3885SShawn Bohrer 
176997502231SEric Dumazet 		if (unlikely(sk->sk_rx_dst != dst))
177097502231SEric Dumazet 			udp_sk_rx_dst_set(sk, dst);
1771421b3885SShawn Bohrer 
1772421b3885SShawn Bohrer 		ret = udp_queue_rcv_skb(sk, skb);
17738afdd99aSEric Dumazet 		sock_put(sk);
1774421b3885SShawn Bohrer 		/* a return value > 0 means to resubmit the input, but
1775421b3885SShawn Bohrer 		 * it wants the return to be -protocol, or 0
1776421b3885SShawn Bohrer 		 */
1777421b3885SShawn Bohrer 		if (ret > 0)
1778421b3885SShawn Bohrer 			return -ret;
1779421b3885SShawn Bohrer 		return 0;
1780421b3885SShawn Bohrer 	} else {
1781db8dac20SDavid S. Miller 		if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1782e3163493SPavel Emelyanov 			return __udp4_lib_mcast_deliver(net, skb, uh,
1783e3163493SPavel Emelyanov 					saddr, daddr, udptable);
1784db8dac20SDavid S. Miller 
1785607c4aafSKOVACS Krisztian 		sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1786421b3885SShawn Bohrer 	}
1787db8dac20SDavid S. Miller 
1788db8dac20SDavid S. Miller 	if (sk != NULL) {
1789a5b50476SEliezer Tamir 		int ret;
1790a5b50476SEliezer Tamir 
1791*2abb7cdcSTom Herbert 		if (udp_sk(sk)->convert_csum && uh->check && !IS_UDPLITE(sk))
1792*2abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
1793*2abb7cdcSTom Herbert 						 inet_compute_pseudo);
1794*2abb7cdcSTom Herbert 
1795a5b50476SEliezer Tamir 		ret = udp_queue_rcv_skb(sk, skb);
1796db8dac20SDavid S. Miller 		sock_put(sk);
1797db8dac20SDavid S. Miller 
1798db8dac20SDavid S. Miller 		/* a return value > 0 means to resubmit the input, but
1799db8dac20SDavid S. Miller 		 * it wants the return to be -protocol, or 0
1800db8dac20SDavid S. Miller 		 */
1801db8dac20SDavid S. Miller 		if (ret > 0)
1802db8dac20SDavid S. Miller 			return -ret;
1803db8dac20SDavid S. Miller 		return 0;
1804db8dac20SDavid S. Miller 	}
1805db8dac20SDavid S. Miller 
1806db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1807db8dac20SDavid S. Miller 		goto drop;
1808db8dac20SDavid S. Miller 	nf_reset(skb);
1809db8dac20SDavid S. Miller 
1810db8dac20SDavid S. Miller 	/* No socket. Drop packet silently, if checksum is wrong */
1811db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
1812db8dac20SDavid S. Miller 		goto csum_error;
1813db8dac20SDavid S. Miller 
18140283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
1815db8dac20SDavid S. Miller 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1816db8dac20SDavid S. Miller 
1817db8dac20SDavid S. Miller 	/*
1818db8dac20SDavid S. Miller 	 * Hmm.  We got an UDP packet to a port to which we
1819db8dac20SDavid S. Miller 	 * don't wanna listen.  Ignore it.
1820db8dac20SDavid S. Miller 	 */
1821db8dac20SDavid S. Miller 	kfree_skb(skb);
1822db8dac20SDavid S. Miller 	return 0;
1823db8dac20SDavid S. Miller 
1824db8dac20SDavid S. Miller short_packet:
1825673d57e7SHarvey Harrison 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
1826afd46503SJoe Perches 		       proto == IPPROTO_UDPLITE ? "Lite" : "",
1827afd46503SJoe Perches 		       &saddr, ntohs(uh->source),
1828afd46503SJoe Perches 		       ulen, skb->len,
1829afd46503SJoe Perches 		       &daddr, ntohs(uh->dest));
1830db8dac20SDavid S. Miller 	goto drop;
1831db8dac20SDavid S. Miller 
1832db8dac20SDavid S. Miller csum_error:
1833db8dac20SDavid S. Miller 	/*
1834db8dac20SDavid S. Miller 	 * RFC1122: OK.  Discards the bad packet silently (as far as
1835db8dac20SDavid S. Miller 	 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1836db8dac20SDavid S. Miller 	 */
1837673d57e7SHarvey Harrison 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
1838afd46503SJoe Perches 		       proto == IPPROTO_UDPLITE ? "Lite" : "",
1839afd46503SJoe Perches 		       &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
1840db8dac20SDavid S. Miller 		       ulen);
18416a5dc9e5SEric Dumazet 	UDP_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1842db8dac20SDavid S. Miller drop:
18430283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1844db8dac20SDavid S. Miller 	kfree_skb(skb);
1845db8dac20SDavid S. Miller 	return 0;
1846db8dac20SDavid S. Miller }
1847db8dac20SDavid S. Miller 
1848421b3885SShawn Bohrer /* We can only early demux multicast if there is a single matching socket.
1849421b3885SShawn Bohrer  * If more than one socket found returns NULL
1850421b3885SShawn Bohrer  */
1851421b3885SShawn Bohrer static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
1852421b3885SShawn Bohrer 						  __be16 loc_port, __be32 loc_addr,
1853421b3885SShawn Bohrer 						  __be16 rmt_port, __be32 rmt_addr,
1854421b3885SShawn Bohrer 						  int dif)
1855421b3885SShawn Bohrer {
1856421b3885SShawn Bohrer 	struct sock *sk, *result;
1857421b3885SShawn Bohrer 	struct hlist_nulls_node *node;
1858421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
1859421b3885SShawn Bohrer 	unsigned int count, slot = udp_hashfn(net, hnum, udp_table.mask);
1860421b3885SShawn Bohrer 	struct udp_hslot *hslot = &udp_table.hash[slot];
1861421b3885SShawn Bohrer 
186263c6f81cSEric Dumazet 	/* Do not bother scanning a too big list */
186363c6f81cSEric Dumazet 	if (hslot->count > 10)
186463c6f81cSEric Dumazet 		return NULL;
186563c6f81cSEric Dumazet 
1866421b3885SShawn Bohrer 	rcu_read_lock();
1867421b3885SShawn Bohrer begin:
1868421b3885SShawn Bohrer 	count = 0;
1869421b3885SShawn Bohrer 	result = NULL;
1870421b3885SShawn Bohrer 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
1871421b3885SShawn Bohrer 		if (__udp_is_mcast_sock(net, sk,
1872421b3885SShawn Bohrer 					loc_port, loc_addr,
1873421b3885SShawn Bohrer 					rmt_port, rmt_addr,
1874421b3885SShawn Bohrer 					dif, hnum)) {
1875421b3885SShawn Bohrer 			result = sk;
1876421b3885SShawn Bohrer 			++count;
1877421b3885SShawn Bohrer 		}
1878421b3885SShawn Bohrer 	}
1879421b3885SShawn Bohrer 	/*
1880421b3885SShawn Bohrer 	 * if the nulls value we got at the end of this lookup is
1881421b3885SShawn Bohrer 	 * not the expected one, we must restart lookup.
1882421b3885SShawn Bohrer 	 * We probably met an item that was moved to another chain.
1883421b3885SShawn Bohrer 	 */
1884421b3885SShawn Bohrer 	if (get_nulls_value(node) != slot)
1885421b3885SShawn Bohrer 		goto begin;
1886421b3885SShawn Bohrer 
1887421b3885SShawn Bohrer 	if (result) {
1888421b3885SShawn Bohrer 		if (count != 1 ||
1889421b3885SShawn Bohrer 		    unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
1890421b3885SShawn Bohrer 			result = NULL;
1891f69b923aSEric Dumazet 		else if (unlikely(!__udp_is_mcast_sock(net, result,
1892421b3885SShawn Bohrer 						       loc_port, loc_addr,
1893421b3885SShawn Bohrer 						       rmt_port, rmt_addr,
1894421b3885SShawn Bohrer 						       dif, hnum))) {
1895421b3885SShawn Bohrer 			sock_put(result);
1896421b3885SShawn Bohrer 			result = NULL;
1897421b3885SShawn Bohrer 		}
1898421b3885SShawn Bohrer 	}
1899421b3885SShawn Bohrer 	rcu_read_unlock();
1900421b3885SShawn Bohrer 	return result;
1901421b3885SShawn Bohrer }
1902421b3885SShawn Bohrer 
1903421b3885SShawn Bohrer /* For unicast we should only early demux connected sockets or we can
1904421b3885SShawn Bohrer  * break forwarding setups.  The chains here can be long so only check
1905421b3885SShawn Bohrer  * if the first socket is an exact match and if not move on.
1906421b3885SShawn Bohrer  */
1907421b3885SShawn Bohrer static struct sock *__udp4_lib_demux_lookup(struct net *net,
1908421b3885SShawn Bohrer 					    __be16 loc_port, __be32 loc_addr,
1909421b3885SShawn Bohrer 					    __be16 rmt_port, __be32 rmt_addr,
1910421b3885SShawn Bohrer 					    int dif)
1911421b3885SShawn Bohrer {
1912421b3885SShawn Bohrer 	struct sock *sk, *result;
1913421b3885SShawn Bohrer 	struct hlist_nulls_node *node;
1914421b3885SShawn Bohrer 	unsigned short hnum = ntohs(loc_port);
1915421b3885SShawn Bohrer 	unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum);
1916421b3885SShawn Bohrer 	unsigned int slot2 = hash2 & udp_table.mask;
1917421b3885SShawn Bohrer 	struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
1918c7228317SJoe Perches 	INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
1919421b3885SShawn Bohrer 	const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
1920421b3885SShawn Bohrer 
1921421b3885SShawn Bohrer 	rcu_read_lock();
1922421b3885SShawn Bohrer 	result = NULL;
1923421b3885SShawn Bohrer 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
1924421b3885SShawn Bohrer 		if (INET_MATCH(sk, net, acookie,
1925421b3885SShawn Bohrer 			       rmt_addr, loc_addr, ports, dif))
1926421b3885SShawn Bohrer 			result = sk;
1927421b3885SShawn Bohrer 		/* Only check first socket in chain */
1928421b3885SShawn Bohrer 		break;
1929421b3885SShawn Bohrer 	}
1930421b3885SShawn Bohrer 
1931421b3885SShawn Bohrer 	if (result) {
1932421b3885SShawn Bohrer 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
1933421b3885SShawn Bohrer 			result = NULL;
1934421b3885SShawn Bohrer 		else if (unlikely(!INET_MATCH(sk, net, acookie,
1935421b3885SShawn Bohrer 					      rmt_addr, loc_addr,
1936421b3885SShawn Bohrer 					      ports, dif))) {
1937421b3885SShawn Bohrer 			sock_put(result);
1938421b3885SShawn Bohrer 			result = NULL;
1939421b3885SShawn Bohrer 		}
1940421b3885SShawn Bohrer 	}
1941421b3885SShawn Bohrer 	rcu_read_unlock();
1942421b3885SShawn Bohrer 	return result;
1943421b3885SShawn Bohrer }
1944421b3885SShawn Bohrer 
1945421b3885SShawn Bohrer void udp_v4_early_demux(struct sk_buff *skb)
1946421b3885SShawn Bohrer {
1947610438b7SEric Dumazet 	struct net *net = dev_net(skb->dev);
1948610438b7SEric Dumazet 	const struct iphdr *iph;
1949610438b7SEric Dumazet 	const struct udphdr *uh;
1950421b3885SShawn Bohrer 	struct sock *sk;
1951421b3885SShawn Bohrer 	struct dst_entry *dst;
1952421b3885SShawn Bohrer 	int dif = skb->dev->ifindex;
1953421b3885SShawn Bohrer 
1954421b3885SShawn Bohrer 	/* validate the packet */
1955421b3885SShawn Bohrer 	if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
1956421b3885SShawn Bohrer 		return;
1957421b3885SShawn Bohrer 
1958610438b7SEric Dumazet 	iph = ip_hdr(skb);
1959610438b7SEric Dumazet 	uh = udp_hdr(skb);
1960610438b7SEric Dumazet 
1961421b3885SShawn Bohrer 	if (skb->pkt_type == PACKET_BROADCAST ||
1962421b3885SShawn Bohrer 	    skb->pkt_type == PACKET_MULTICAST)
1963421b3885SShawn Bohrer 		sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
1964421b3885SShawn Bohrer 						   uh->source, iph->saddr, dif);
1965421b3885SShawn Bohrer 	else if (skb->pkt_type == PACKET_HOST)
1966421b3885SShawn Bohrer 		sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
1967421b3885SShawn Bohrer 					     uh->source, iph->saddr, dif);
1968421b3885SShawn Bohrer 	else
1969421b3885SShawn Bohrer 		return;
1970421b3885SShawn Bohrer 
1971421b3885SShawn Bohrer 	if (!sk)
1972421b3885SShawn Bohrer 		return;
1973421b3885SShawn Bohrer 
1974421b3885SShawn Bohrer 	skb->sk = sk;
1975421b3885SShawn Bohrer 	skb->destructor = sock_edemux;
1976421b3885SShawn Bohrer 	dst = sk->sk_rx_dst;
1977421b3885SShawn Bohrer 
1978421b3885SShawn Bohrer 	if (dst)
1979421b3885SShawn Bohrer 		dst = dst_check(dst, 0);
1980421b3885SShawn Bohrer 	if (dst)
1981421b3885SShawn Bohrer 		skb_dst_set_noref(skb, dst);
1982421b3885SShawn Bohrer }
1983421b3885SShawn Bohrer 
1984db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb)
1985db8dac20SDavid S. Miller {
1986645ca708SEric Dumazet 	return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1987db8dac20SDavid S. Miller }
1988db8dac20SDavid S. Miller 
19897d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk)
1990db8dac20SDavid S. Miller {
199144046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
19928a74ad60SEric Dumazet 	bool slow = lock_sock_fast(sk);
1993db8dac20SDavid S. Miller 	udp_flush_pending_frames(sk);
19948a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
199544046a59STom Parkin 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
199644046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
199744046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
199844046a59STom Parkin 		if (encap_destroy)
199944046a59STom Parkin 			encap_destroy(sk);
200044046a59STom Parkin 	}
2001db8dac20SDavid S. Miller }
2002db8dac20SDavid S. Miller 
20031da177e4SLinus Torvalds /*
20041da177e4SLinus Torvalds  *	Socket option code for UDP
20051da177e4SLinus Torvalds  */
20064c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname,
2007b7058842SDavid S. Miller 		       char __user *optval, unsigned int optlen,
20084c0a6cb0SGerrit Renker 		       int (*push_pending_frames)(struct sock *))
20091da177e4SLinus Torvalds {
20101da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
20111c19448cSTom Herbert 	int val, valbool;
20121da177e4SLinus Torvalds 	int err = 0;
2013b2bf1e26SWang Chen 	int is_udplite = IS_UDPLITE(sk);
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	if (optlen < sizeof(int))
20161da177e4SLinus Torvalds 		return -EINVAL;
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
20191da177e4SLinus Torvalds 		return -EFAULT;
20201da177e4SLinus Torvalds 
20211c19448cSTom Herbert 	valbool = val ? 1 : 0;
20221c19448cSTom Herbert 
20231da177e4SLinus Torvalds 	switch (optname) {
20241da177e4SLinus Torvalds 	case UDP_CORK:
20251da177e4SLinus Torvalds 		if (val != 0) {
20261da177e4SLinus Torvalds 			up->corkflag = 1;
20271da177e4SLinus Torvalds 		} else {
20281da177e4SLinus Torvalds 			up->corkflag = 0;
20291da177e4SLinus Torvalds 			lock_sock(sk);
20304c0a6cb0SGerrit Renker 			(*push_pending_frames)(sk);
20311da177e4SLinus Torvalds 			release_sock(sk);
20321da177e4SLinus Torvalds 		}
20331da177e4SLinus Torvalds 		break;
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 	case UDP_ENCAP:
20361da177e4SLinus Torvalds 		switch (val) {
20371da177e4SLinus Torvalds 		case 0:
20381da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP:
20391da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP_NON_IKE:
2040067b207bSJames Chapman 			up->encap_rcv = xfrm4_udp_encap_rcv;
2041067b207bSJames Chapman 			/* FALLTHROUGH */
2042342f0234SJames Chapman 		case UDP_ENCAP_L2TPINUDP:
20431da177e4SLinus Torvalds 			up->encap_type = val;
2044447167bfSEric Dumazet 			udp_encap_enable();
20451da177e4SLinus Torvalds 			break;
20461da177e4SLinus Torvalds 		default:
20471da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
20481da177e4SLinus Torvalds 			break;
20491da177e4SLinus Torvalds 		}
20501da177e4SLinus Torvalds 		break;
20511da177e4SLinus Torvalds 
20521c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
20531c19448cSTom Herbert 		up->no_check6_tx = valbool;
20541c19448cSTom Herbert 		break;
20551c19448cSTom Herbert 
20561c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
20571c19448cSTom Herbert 		up->no_check6_rx = valbool;
20581c19448cSTom Herbert 		break;
20591c19448cSTom Herbert 
2060ba4e58ecSGerrit Renker 	/*
2061ba4e58ecSGerrit Renker 	 * 	UDP-Lite's partial checksum coverage (RFC 3828).
2062ba4e58ecSGerrit Renker 	 */
2063ba4e58ecSGerrit Renker 	/* The sender sets actual checksum coverage length via this option.
2064ba4e58ecSGerrit Renker 	 * The case coverage > packet length is handled by send module. */
2065ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2066b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2067ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2068ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
2069ba4e58ecSGerrit Renker 			val = 8;
20704be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
20714be929beSAlexey Dobriyan 			val = USHRT_MAX;
2072ba4e58ecSGerrit Renker 		up->pcslen = val;
2073ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_SEND_CC;
2074ba4e58ecSGerrit Renker 		break;
2075ba4e58ecSGerrit Renker 
2076ba4e58ecSGerrit Renker 	/* The receiver specifies a minimum checksum coverage value. To make
2077ba4e58ecSGerrit Renker 	 * sense, this should be set to at least 8 (as done below). If zero is
2078ba4e58ecSGerrit Renker 	 * used, this again means full checksum coverage.                     */
2079ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2080b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
2081ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
2082ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Avoid silly minimal values.       */
2083ba4e58ecSGerrit Renker 			val = 8;
20844be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
20854be929beSAlexey Dobriyan 			val = USHRT_MAX;
2086ba4e58ecSGerrit Renker 		up->pcrlen = val;
2087ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_RECV_CC;
2088ba4e58ecSGerrit Renker 		break;
2089ba4e58ecSGerrit Renker 
20901da177e4SLinus Torvalds 	default:
20911da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
20921da177e4SLinus Torvalds 		break;
20936516c655SStephen Hemminger 	}
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	return err;
20961da177e4SLinus Torvalds }
2097c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt);
20981da177e4SLinus Torvalds 
2099db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname,
2100b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
2101db8dac20SDavid S. Miller {
2102db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2103db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2104db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2105db8dac20SDavid S. Miller 	return ip_setsockopt(sk, level, optname, optval, optlen);
2106db8dac20SDavid S. Miller }
2107db8dac20SDavid S. Miller 
2108db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2109db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname,
2110b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
2111db8dac20SDavid S. Miller {
2112db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2113db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2114db8dac20SDavid S. Miller 					  udp_push_pending_frames);
2115db8dac20SDavid S. Miller 	return compat_ip_setsockopt(sk, level, optname, optval, optlen);
2116db8dac20SDavid S. Miller }
2117db8dac20SDavid S. Miller #endif
2118db8dac20SDavid S. Miller 
21194c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname,
21201da177e4SLinus Torvalds 		       char __user *optval, int __user *optlen)
21211da177e4SLinus Torvalds {
21221da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
21231da177e4SLinus Torvalds 	int val, len;
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds 	if (get_user(len, optlen))
21261da177e4SLinus Torvalds 		return -EFAULT;
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds 	if (len < 0)
21311da177e4SLinus Torvalds 		return -EINVAL;
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds 	switch (optname) {
21341da177e4SLinus Torvalds 	case UDP_CORK:
21351da177e4SLinus Torvalds 		val = up->corkflag;
21361da177e4SLinus Torvalds 		break;
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 	case UDP_ENCAP:
21391da177e4SLinus Torvalds 		val = up->encap_type;
21401da177e4SLinus Torvalds 		break;
21411da177e4SLinus Torvalds 
21421c19448cSTom Herbert 	case UDP_NO_CHECK6_TX:
21431c19448cSTom Herbert 		val = up->no_check6_tx;
21441c19448cSTom Herbert 		break;
21451c19448cSTom Herbert 
21461c19448cSTom Herbert 	case UDP_NO_CHECK6_RX:
21471c19448cSTom Herbert 		val = up->no_check6_rx;
21481c19448cSTom Herbert 		break;
21491c19448cSTom Herbert 
2150ba4e58ecSGerrit Renker 	/* The following two cannot be changed on UDP sockets, the return is
2151ba4e58ecSGerrit Renker 	 * always 0 (which corresponds to the full checksum coverage of UDP). */
2152ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
2153ba4e58ecSGerrit Renker 		val = up->pcslen;
2154ba4e58ecSGerrit Renker 		break;
2155ba4e58ecSGerrit Renker 
2156ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
2157ba4e58ecSGerrit Renker 		val = up->pcrlen;
2158ba4e58ecSGerrit Renker 		break;
2159ba4e58ecSGerrit Renker 
21601da177e4SLinus Torvalds 	default:
21611da177e4SLinus Torvalds 		return -ENOPROTOOPT;
21626516c655SStephen Hemminger 	}
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds 	if (put_user(len, optlen))
21651da177e4SLinus Torvalds 		return -EFAULT;
21661da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
21671da177e4SLinus Torvalds 		return -EFAULT;
21681da177e4SLinus Torvalds 	return 0;
21691da177e4SLinus Torvalds }
2170c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt);
21711da177e4SLinus Torvalds 
2172db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname,
2173db8dac20SDavid S. Miller 		   char __user *optval, int __user *optlen)
2174db8dac20SDavid S. Miller {
2175db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2176db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2177db8dac20SDavid S. Miller 	return ip_getsockopt(sk, level, optname, optval, optlen);
2178db8dac20SDavid S. Miller }
2179db8dac20SDavid S. Miller 
2180db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2181db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname,
2182db8dac20SDavid S. Miller 				 char __user *optval, int __user *optlen)
2183db8dac20SDavid S. Miller {
2184db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
2185db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2186db8dac20SDavid S. Miller 	return compat_ip_getsockopt(sk, level, optname, optval, optlen);
2187db8dac20SDavid S. Miller }
2188db8dac20SDavid S. Miller #endif
21891da177e4SLinus Torvalds /**
21901da177e4SLinus Torvalds  * 	udp_poll - wait for a UDP event.
21911da177e4SLinus Torvalds  *	@file - file struct
21921da177e4SLinus Torvalds  *	@sock - socket
21931da177e4SLinus Torvalds  *	@wait - poll table
21941da177e4SLinus Torvalds  *
21951da177e4SLinus Torvalds  *	This is same as datagram poll, except for the special case of
21961da177e4SLinus Torvalds  *	blocking sockets. If application is using a blocking fd
21971da177e4SLinus Torvalds  *	and a packet with checksum error is in the queue;
21981da177e4SLinus Torvalds  *	then it could get return from select indicating data available
21991da177e4SLinus Torvalds  *	but then block when reading it. Add special case code
22001da177e4SLinus Torvalds  *	to work around these arguably broken applications.
22011da177e4SLinus Torvalds  */
22021da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
22031da177e4SLinus Torvalds {
22041da177e4SLinus Torvalds 	unsigned int mask = datagram_poll(file, sock, wait);
22051da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
22061da177e4SLinus Torvalds 
2207c3f1dbafSDavid Majnemer 	sock_rps_record_flow(sk);
2208c3f1dbafSDavid Majnemer 
22091da177e4SLinus Torvalds 	/* Check for false positives due to checksum errors */
221085584672SEric Dumazet 	if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
221185584672SEric Dumazet 	    !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
22121da177e4SLinus Torvalds 		mask &= ~(POLLIN | POLLRDNORM);
22131da177e4SLinus Torvalds 
22141da177e4SLinus Torvalds 	return mask;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds }
2217c482c568SEric Dumazet EXPORT_SYMBOL(udp_poll);
22181da177e4SLinus Torvalds 
2219db8dac20SDavid S. Miller struct proto udp_prot = {
2220db8dac20SDavid S. Miller 	.name		   = "UDP",
2221db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
2222db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
2223db8dac20SDavid S. Miller 	.connect	   = ip4_datagram_connect,
2224db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
2225db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
2226db8dac20SDavid S. Miller 	.destroy	   = udp_destroy_sock,
2227db8dac20SDavid S. Miller 	.setsockopt	   = udp_setsockopt,
2228db8dac20SDavid S. Miller 	.getsockopt	   = udp_getsockopt,
2229db8dac20SDavid S. Miller 	.sendmsg	   = udp_sendmsg,
2230db8dac20SDavid S. Miller 	.recvmsg	   = udp_recvmsg,
2231db8dac20SDavid S. Miller 	.sendpage	   = udp_sendpage,
223293821778SHerbert Xu 	.backlog_rcv	   = __udp_queue_rcv_skb,
22338141ed9fSSteffen Klassert 	.release_cb	   = ip4_datagram_release_cb,
2234db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
2235db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
2236719f8358SEric Dumazet 	.rehash		   = udp_v4_rehash,
2237db8dac20SDavid S. Miller 	.get_port	   = udp_v4_get_port,
2238db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
2239db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
2240db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
2241db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
2242db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp_sock),
2243271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
2244645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
2245db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2246db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udp_setsockopt,
2247db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udp_getsockopt,
2248db8dac20SDavid S. Miller #endif
2249fcbdf09dSOctavian Purdila 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
2250db8dac20SDavid S. Miller };
2251c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot);
22521da177e4SLinus Torvalds 
22531da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
22541da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
22551da177e4SLinus Torvalds 
2256645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start)
22571da177e4SLinus Torvalds {
22581da177e4SLinus Torvalds 	struct sock *sk;
22591da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
22606f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
22611da177e4SLinus Torvalds 
2262f86dcc5aSEric Dumazet 	for (state->bucket = start; state->bucket <= state->udp_table->mask;
2263f86dcc5aSEric Dumazet 	     ++state->bucket) {
226488ab1932SEric Dumazet 		struct hlist_nulls_node *node;
2265645ca708SEric Dumazet 		struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
2266f86dcc5aSEric Dumazet 
2267f86dcc5aSEric Dumazet 		if (hlist_nulls_empty(&hslot->head))
2268f86dcc5aSEric Dumazet 			continue;
2269f86dcc5aSEric Dumazet 
2270645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
227188ab1932SEric Dumazet 		sk_nulls_for_each(sk, node, &hslot->head) {
2272878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
2273a91275efSDaniel Lezcano 				continue;
22741da177e4SLinus Torvalds 			if (sk->sk_family == state->family)
22751da177e4SLinus Torvalds 				goto found;
22761da177e4SLinus Torvalds 		}
2277645ca708SEric Dumazet 		spin_unlock_bh(&hslot->lock);
22781da177e4SLinus Torvalds 	}
22791da177e4SLinus Torvalds 	sk = NULL;
22801da177e4SLinus Torvalds found:
22811da177e4SLinus Torvalds 	return sk;
22821da177e4SLinus Torvalds }
22831da177e4SLinus Torvalds 
22841da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
22851da177e4SLinus Torvalds {
22861da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
22876f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds 	do {
229088ab1932SEric Dumazet 		sk = sk_nulls_next(sk);
2291878628fbSYOSHIFUJI Hideaki 	} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
22921da177e4SLinus Torvalds 
2293645ca708SEric Dumazet 	if (!sk) {
2294f86dcc5aSEric Dumazet 		if (state->bucket <= state->udp_table->mask)
2295f52b5054SEric Dumazet 			spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
2296645ca708SEric Dumazet 		return udp_get_first(seq, state->bucket + 1);
22971da177e4SLinus Torvalds 	}
22981da177e4SLinus Torvalds 	return sk;
22991da177e4SLinus Torvalds }
23001da177e4SLinus Torvalds 
23011da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
23021da177e4SLinus Torvalds {
2303645ca708SEric Dumazet 	struct sock *sk = udp_get_first(seq, 0);
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	if (sk)
23061da177e4SLinus Torvalds 		while (pos && (sk = udp_get_next(seq, sk)) != NULL)
23071da177e4SLinus Torvalds 			--pos;
23081da177e4SLinus Torvalds 	return pos ? NULL : sk;
23091da177e4SLinus Torvalds }
23101da177e4SLinus Torvalds 
23111da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
23121da177e4SLinus Torvalds {
231330842f29SVitaly Mayatskikh 	struct udp_iter_state *state = seq->private;
2314f86dcc5aSEric Dumazet 	state->bucket = MAX_UDP_PORTS;
231530842f29SVitaly Mayatskikh 
2316b50660f1SYOSHIFUJI Hideaki 	return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
23171da177e4SLinus Torvalds }
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
23201da177e4SLinus Torvalds {
23211da177e4SLinus Torvalds 	struct sock *sk;
23221da177e4SLinus Torvalds 
2323b50660f1SYOSHIFUJI Hideaki 	if (v == SEQ_START_TOKEN)
23241da177e4SLinus Torvalds 		sk = udp_get_idx(seq, 0);
23251da177e4SLinus Torvalds 	else
23261da177e4SLinus Torvalds 		sk = udp_get_next(seq, v);
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	++*pos;
23291da177e4SLinus Torvalds 	return sk;
23301da177e4SLinus Torvalds }
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v)
23331da177e4SLinus Torvalds {
2334645ca708SEric Dumazet 	struct udp_iter_state *state = seq->private;
2335645ca708SEric Dumazet 
2336f86dcc5aSEric Dumazet 	if (state->bucket <= state->udp_table->mask)
2337645ca708SEric Dumazet 		spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
23381da177e4SLinus Torvalds }
23391da177e4SLinus Torvalds 
234073cb88ecSArjan van de Ven int udp_seq_open(struct inode *inode, struct file *file)
23411da177e4SLinus Torvalds {
2342d9dda78bSAl Viro 	struct udp_seq_afinfo *afinfo = PDE_DATA(inode);
2343a2be75c1SDenis V. Lunev 	struct udp_iter_state *s;
2344a2be75c1SDenis V. Lunev 	int err;
23451da177e4SLinus Torvalds 
2346a2be75c1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
2347a2be75c1SDenis V. Lunev 			   sizeof(struct udp_iter_state));
2348a2be75c1SDenis V. Lunev 	if (err < 0)
2349a2be75c1SDenis V. Lunev 		return err;
2350a91275efSDaniel Lezcano 
2351a2be75c1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
23521da177e4SLinus Torvalds 	s->family		= afinfo->family;
2353645ca708SEric Dumazet 	s->udp_table		= afinfo->udp_table;
2354a2be75c1SDenis V. Lunev 	return err;
2355a91275efSDaniel Lezcano }
235673cb88ecSArjan van de Ven EXPORT_SYMBOL(udp_seq_open);
2357a91275efSDaniel Lezcano 
23581da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
23590c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
23601da177e4SLinus Torvalds {
23611da177e4SLinus Torvalds 	struct proc_dir_entry *p;
23621da177e4SLinus Torvalds 	int rc = 0;
23631da177e4SLinus Torvalds 
2364dda61925SDenis V. Lunev 	afinfo->seq_ops.start		= udp_seq_start;
2365dda61925SDenis V. Lunev 	afinfo->seq_ops.next		= udp_seq_next;
2366dda61925SDenis V. Lunev 	afinfo->seq_ops.stop		= udp_seq_stop;
2367dda61925SDenis V. Lunev 
236884841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
236973cb88ecSArjan van de Ven 			     afinfo->seq_fops, afinfo);
237084841c3cSDenis V. Lunev 	if (!p)
23711da177e4SLinus Torvalds 		rc = -ENOMEM;
23721da177e4SLinus Torvalds 	return rc;
23731da177e4SLinus Torvalds }
2374c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_register);
23751da177e4SLinus Torvalds 
23760c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
23771da177e4SLinus Torvalds {
2378ece31ffdSGao feng 	remove_proc_entry(afinfo->name, net->proc_net);
23791da177e4SLinus Torvalds }
2380c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_unregister);
2381db8dac20SDavid S. Miller 
2382db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
23835e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2384652586dfSTetsuo Handa 		int bucket)
2385db8dac20SDavid S. Miller {
2386db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
2387c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2388c720c7e8SEric Dumazet 	__be32 src  = inet->inet_rcv_saddr;
2389c720c7e8SEric Dumazet 	__u16 destp	  = ntohs(inet->inet_dport);
2390c720c7e8SEric Dumazet 	__u16 srcp	  = ntohs(inet->inet_sport);
2391db8dac20SDavid S. Miller 
2392f86dcc5aSEric Dumazet 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2393652586dfSTetsuo Handa 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
2394db8dac20SDavid S. Miller 		bucket, src, srcp, dest, destp, sp->sk_state,
239531e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
239631e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
2397a7cb5a49SEric W. Biederman 		0, 0L, 0,
2398a7cb5a49SEric W. Biederman 		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
2399a7cb5a49SEric W. Biederman 		0, sock_i_ino(sp),
2400cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_refcnt), sp,
2401652586dfSTetsuo Handa 		atomic_read(&sp->sk_drops));
2402db8dac20SDavid S. Miller }
2403db8dac20SDavid S. Miller 
2404db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v)
2405db8dac20SDavid S. Miller {
2406652586dfSTetsuo Handa 	seq_setwidth(seq, 127);
2407db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
2408652586dfSTetsuo Handa 		seq_puts(seq, "  sl  local_address rem_address   st tx_queue "
2409db8dac20SDavid S. Miller 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
2410cb61cb9bSEric Dumazet 			   "inode ref pointer drops");
2411db8dac20SDavid S. Miller 	else {
2412db8dac20SDavid S. Miller 		struct udp_iter_state *state = seq->private;
2413db8dac20SDavid S. Miller 
2414652586dfSTetsuo Handa 		udp4_format_sock(v, seq, state->bucket);
2415db8dac20SDavid S. Miller 	}
2416652586dfSTetsuo Handa 	seq_pad(seq, '\n');
2417db8dac20SDavid S. Miller 	return 0;
2418db8dac20SDavid S. Miller }
2419db8dac20SDavid S. Miller 
242073cb88ecSArjan van de Ven static const struct file_operations udp_afinfo_seq_fops = {
242173cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
242273cb88ecSArjan van de Ven 	.open     = udp_seq_open,
242373cb88ecSArjan van de Ven 	.read     = seq_read,
242473cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
242573cb88ecSArjan van de Ven 	.release  = seq_release_net
242673cb88ecSArjan van de Ven };
242773cb88ecSArjan van de Ven 
2428db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
2429db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = {
2430db8dac20SDavid S. Miller 	.name		= "udp",
2431db8dac20SDavid S. Miller 	.family		= AF_INET,
2432645ca708SEric Dumazet 	.udp_table	= &udp_table,
243373cb88ecSArjan van de Ven 	.seq_fops	= &udp_afinfo_seq_fops,
2434dda61925SDenis V. Lunev 	.seq_ops	= {
2435dda61925SDenis V. Lunev 		.show		= udp4_seq_show,
2436dda61925SDenis V. Lunev 	},
2437db8dac20SDavid S. Miller };
2438db8dac20SDavid S. Miller 
24392c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net)
244015439febSPavel Emelyanov {
244115439febSPavel Emelyanov 	return udp_proc_register(net, &udp4_seq_afinfo);
244215439febSPavel Emelyanov }
244315439febSPavel Emelyanov 
24442c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net)
244515439febSPavel Emelyanov {
244615439febSPavel Emelyanov 	udp_proc_unregister(net, &udp4_seq_afinfo);
244715439febSPavel Emelyanov }
244815439febSPavel Emelyanov 
244915439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = {
245015439febSPavel Emelyanov 	.init = udp4_proc_init_net,
245115439febSPavel Emelyanov 	.exit = udp4_proc_exit_net,
245215439febSPavel Emelyanov };
245315439febSPavel Emelyanov 
2454db8dac20SDavid S. Miller int __init udp4_proc_init(void)
2455db8dac20SDavid S. Miller {
245615439febSPavel Emelyanov 	return register_pernet_subsys(&udp4_net_ops);
2457db8dac20SDavid S. Miller }
2458db8dac20SDavid S. Miller 
2459db8dac20SDavid S. Miller void udp4_proc_exit(void)
2460db8dac20SDavid S. Miller {
246115439febSPavel Emelyanov 	unregister_pernet_subsys(&udp4_net_ops);
2462db8dac20SDavid S. Miller }
24631da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
24641da177e4SLinus Torvalds 
2465f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries;
2466f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str)
2467645ca708SEric Dumazet {
2468413c27d8SEldad Zack 	ssize_t ret;
2469413c27d8SEldad Zack 
2470f86dcc5aSEric Dumazet 	if (!str)
2471f86dcc5aSEric Dumazet 		return 0;
2472413c27d8SEldad Zack 
2473413c27d8SEldad Zack 	ret = kstrtoul(str, 0, &uhash_entries);
2474413c27d8SEldad Zack 	if (ret)
2475413c27d8SEldad Zack 		return 0;
2476413c27d8SEldad Zack 
2477f86dcc5aSEric Dumazet 	if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
2478f86dcc5aSEric Dumazet 		uhash_entries = UDP_HTABLE_SIZE_MIN;
2479f86dcc5aSEric Dumazet 	return 1;
2480f86dcc5aSEric Dumazet }
2481f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries);
2482645ca708SEric Dumazet 
2483f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name)
2484f86dcc5aSEric Dumazet {
2485f86dcc5aSEric Dumazet 	unsigned int i;
2486f86dcc5aSEric Dumazet 
2487f86dcc5aSEric Dumazet 	table->hash = alloc_large_system_hash(name,
2488512615b6SEric Dumazet 					      2 * sizeof(struct udp_hslot),
2489f86dcc5aSEric Dumazet 					      uhash_entries,
2490f86dcc5aSEric Dumazet 					      21, /* one slot per 2 MB */
2491f86dcc5aSEric Dumazet 					      0,
2492f86dcc5aSEric Dumazet 					      &table->log,
2493f86dcc5aSEric Dumazet 					      &table->mask,
249431fe62b9STim Bird 					      UDP_HTABLE_SIZE_MIN,
2495f86dcc5aSEric Dumazet 					      64 * 1024);
249631fe62b9STim Bird 
2497512615b6SEric Dumazet 	table->hash2 = table->hash + (table->mask + 1);
2498f86dcc5aSEric Dumazet 	for (i = 0; i <= table->mask; i++) {
249988ab1932SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
2500fdcc8aa9SEric Dumazet 		table->hash[i].count = 0;
2501645ca708SEric Dumazet 		spin_lock_init(&table->hash[i].lock);
2502645ca708SEric Dumazet 	}
2503512615b6SEric Dumazet 	for (i = 0; i <= table->mask; i++) {
2504512615b6SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
2505512615b6SEric Dumazet 		table->hash2[i].count = 0;
2506512615b6SEric Dumazet 		spin_lock_init(&table->hash2[i].lock);
2507512615b6SEric Dumazet 	}
2508645ca708SEric Dumazet }
2509645ca708SEric Dumazet 
251095766fffSHideo Aoki void __init udp_init(void)
251195766fffSHideo Aoki {
2512f03d78dbSEric Dumazet 	unsigned long limit;
251395766fffSHideo Aoki 
2514f86dcc5aSEric Dumazet 	udp_table_init(&udp_table, "UDP");
2515f03d78dbSEric Dumazet 	limit = nr_free_buffer_pages() / 8;
251695766fffSHideo Aoki 	limit = max(limit, 128UL);
251795766fffSHideo Aoki 	sysctl_udp_mem[0] = limit / 4 * 3;
251895766fffSHideo Aoki 	sysctl_udp_mem[1] = limit;
251995766fffSHideo Aoki 	sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
252095766fffSHideo Aoki 
252195766fffSHideo Aoki 	sysctl_udp_rmem_min = SK_MEM_QUANTUM;
252295766fffSHideo Aoki 	sysctl_udp_wmem_min = SK_MEM_QUANTUM;
252395766fffSHideo Aoki }
2524