xref: /linux/net/ipv4/udp.c (revision a5b50476f77a8fcc8055c955720d05a7c2d9c532)
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>
1021da177e4SLinus Torvalds #include <linux/proc_fs.h>
1031da177e4SLinus Torvalds #include <linux/seq_file.h>
104457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1051da177e4SLinus Torvalds #include <net/icmp.h>
1061da177e4SLinus Torvalds #include <net/route.h>
1071da177e4SLinus Torvalds #include <net/checksum.h>
1081da177e4SLinus Torvalds #include <net/xfrm.h>
109296f7ea7SSatoru Moriya #include <trace/events/udp.h>
110447167bfSEric Dumazet #include <linux/static_key.h>
11122911fc5SEric Dumazet #include <trace/events/skb.h>
112*a5b50476SEliezer Tamir #include <net/ll_poll.h>
113ba4e58ecSGerrit Renker #include "udp_impl.h"
1141da177e4SLinus Torvalds 
115f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly;
116645ca708SEric Dumazet EXPORT_SYMBOL(udp_table);
1171da177e4SLinus Torvalds 
1188d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly;
11995766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem);
120c482c568SEric Dumazet 
121c482c568SEric Dumazet int sysctl_udp_rmem_min __read_mostly;
12295766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min);
123c482c568SEric Dumazet 
124c482c568SEric Dumazet int sysctl_udp_wmem_min __read_mostly;
12595766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min);
12695766fffSHideo Aoki 
1278d987e5cSEric Dumazet atomic_long_t udp_memory_allocated;
12895766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated);
12995766fffSHideo Aoki 
130f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536
131f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
13298322f22SEric Dumazet 
133f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num,
134645ca708SEric Dumazet 			       const struct udp_hslot *hslot,
13598322f22SEric Dumazet 			       unsigned long *bitmap,
136f24d43c0SEric Dumazet 			       struct sock *sk,
137f24d43c0SEric Dumazet 			       int (*saddr_comp)(const struct sock *sk1,
138f86dcc5aSEric Dumazet 						 const struct sock *sk2),
139f86dcc5aSEric Dumazet 			       unsigned int log)
14025030a7fSGerrit Renker {
141f24d43c0SEric Dumazet 	struct sock *sk2;
14288ab1932SEric Dumazet 	struct hlist_nulls_node *node;
143ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
14425030a7fSGerrit Renker 
14588ab1932SEric Dumazet 	sk_nulls_for_each(sk2, node, &hslot->head)
146f24d43c0SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
147f24d43c0SEric Dumazet 		    sk2 != sk &&
148d4cada4aSEric Dumazet 		    (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
149f24d43c0SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1509d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1519d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
152ba418fa3STom Herbert 		    (!sk2->sk_reuseport || !sk->sk_reuseport ||
153ba418fa3STom Herbert 		      !uid_eq(uid, sock_i_uid(sk2))) &&
15498322f22SEric Dumazet 		    (*saddr_comp)(sk, sk2)) {
15598322f22SEric Dumazet 			if (bitmap)
156d4cada4aSEric Dumazet 				__set_bit(udp_sk(sk2)->udp_port_hash >> log,
157d4cada4aSEric Dumazet 					  bitmap);
15898322f22SEric Dumazet 			else
159fc038410SDavid S. Miller 				return 1;
16098322f22SEric Dumazet 		}
16125030a7fSGerrit Renker 	return 0;
16225030a7fSGerrit Renker }
16325030a7fSGerrit Renker 
16430fff923SEric Dumazet /*
16530fff923SEric Dumazet  * Note: we still hold spinlock of primary hash chain, so no other writer
16630fff923SEric Dumazet  * can insert/delete a socket with local_port == num
16730fff923SEric Dumazet  */
16830fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num,
16930fff923SEric Dumazet 			       struct udp_hslot *hslot2,
17030fff923SEric Dumazet 			       struct sock *sk,
17130fff923SEric Dumazet 			       int (*saddr_comp)(const struct sock *sk1,
17230fff923SEric Dumazet 						 const struct sock *sk2))
17330fff923SEric Dumazet {
17430fff923SEric Dumazet 	struct sock *sk2;
17530fff923SEric Dumazet 	struct hlist_nulls_node *node;
176ba418fa3STom Herbert 	kuid_t uid = sock_i_uid(sk);
17730fff923SEric Dumazet 	int res = 0;
17830fff923SEric Dumazet 
17930fff923SEric Dumazet 	spin_lock(&hslot2->lock);
18030fff923SEric Dumazet 	udp_portaddr_for_each_entry(sk2, node, &hslot2->head)
18130fff923SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
18230fff923SEric Dumazet 		    sk2 != sk &&
18330fff923SEric Dumazet 		    (udp_sk(sk2)->udp_port_hash == num) &&
18430fff923SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1859d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1869d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
187ba418fa3STom Herbert 		    (!sk2->sk_reuseport || !sk->sk_reuseport ||
188ba418fa3STom Herbert 		      !uid_eq(uid, sock_i_uid(sk2))) &&
18930fff923SEric Dumazet 		    (*saddr_comp)(sk, sk2)) {
19030fff923SEric Dumazet 			res = 1;
19130fff923SEric Dumazet 			break;
19230fff923SEric Dumazet 		}
19330fff923SEric Dumazet 	spin_unlock(&hslot2->lock);
19430fff923SEric Dumazet 	return res;
19530fff923SEric Dumazet }
19630fff923SEric Dumazet 
19725030a7fSGerrit Renker /**
1986ba5a3c5SPavel Emelyanov  *  udp_lib_get_port  -  UDP/-Lite port lookup for IPv4 and IPv6
19925030a7fSGerrit Renker  *
20025030a7fSGerrit Renker  *  @sk:          socket struct in question
20125030a7fSGerrit Renker  *  @snum:        port number to look up
202df2bc459SDavid S. Miller  *  @saddr_comp:  AF-dependent comparison of bound local IP addresses
20325985edcSLucas De Marchi  *  @hash2_nulladdr: AF-dependent hash value in secondary hash chains,
20430fff923SEric Dumazet  *                   with NULL address
20525030a7fSGerrit Renker  */
2066ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum,
207df2bc459SDavid S. Miller 		       int (*saddr_comp)(const struct sock *sk1,
20830fff923SEric Dumazet 					 const struct sock *sk2),
20930fff923SEric Dumazet 		     unsigned int hash2_nulladdr)
2101da177e4SLinus Torvalds {
211512615b6SEric Dumazet 	struct udp_hslot *hslot, *hslot2;
212645ca708SEric Dumazet 	struct udp_table *udptable = sk->sk_prot->h.udp_table;
21325030a7fSGerrit Renker 	int    error = 1;
2143b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2151da177e4SLinus Torvalds 
21632c1da70SStephen Hemminger 	if (!snum) {
2179088c560SEric Dumazet 		int low, high, remaining;
21895c96174SEric Dumazet 		unsigned int rand;
21998322f22SEric Dumazet 		unsigned short first, last;
22098322f22SEric Dumazet 		DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
2211da177e4SLinus Torvalds 
222227b60f5SStephen Hemminger 		inet_get_local_port_range(&low, &high);
223a25de534SAnton Arapov 		remaining = (high - low) + 1;
224227b60f5SStephen Hemminger 
2259088c560SEric Dumazet 		rand = net_random();
22698322f22SEric Dumazet 		first = (((u64)rand * remaining) >> 32) + low;
22798322f22SEric Dumazet 		/*
22898322f22SEric Dumazet 		 * force rand to be an odd multiple of UDP_HTABLE_SIZE
22998322f22SEric Dumazet 		 */
230f86dcc5aSEric Dumazet 		rand = (rand | 1) * (udptable->mask + 1);
2315781b235SEric Dumazet 		last = first + udptable->mask + 1;
2325781b235SEric Dumazet 		do {
233f86dcc5aSEric Dumazet 			hslot = udp_hashslot(udptable, net, first);
23498322f22SEric Dumazet 			bitmap_zero(bitmap, PORTS_PER_CHAIN);
235645ca708SEric Dumazet 			spin_lock_bh(&hslot->lock);
23698322f22SEric Dumazet 			udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
237f86dcc5aSEric Dumazet 					    saddr_comp, udptable->log);
23898322f22SEric Dumazet 
23998322f22SEric Dumazet 			snum = first;
24098322f22SEric Dumazet 			/*
24198322f22SEric Dumazet 			 * Iterate on all possible values of snum for this hash.
24298322f22SEric Dumazet 			 * Using steps of an odd multiple of UDP_HTABLE_SIZE
24398322f22SEric Dumazet 			 * give us randomization and full range coverage.
24498322f22SEric Dumazet 			 */
2459088c560SEric Dumazet 			do {
24698322f22SEric Dumazet 				if (low <= snum && snum <= high &&
247e3826f1eSAmerigo Wang 				    !test_bit(snum >> udptable->log, bitmap) &&
248e3826f1eSAmerigo Wang 				    !inet_is_reserved_local_port(snum))
24998322f22SEric Dumazet 					goto found;
25098322f22SEric Dumazet 				snum += rand;
25198322f22SEric Dumazet 			} while (snum != first);
25298322f22SEric Dumazet 			spin_unlock_bh(&hslot->lock);
2535781b235SEric Dumazet 		} while (++first != last);
25498322f22SEric Dumazet 		goto fail;
255645ca708SEric Dumazet 	} else {
256f86dcc5aSEric Dumazet 		hslot = udp_hashslot(udptable, net, snum);
257645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
25830fff923SEric Dumazet 		if (hslot->count > 10) {
25930fff923SEric Dumazet 			int exist;
26030fff923SEric Dumazet 			unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
26130fff923SEric Dumazet 
26230fff923SEric Dumazet 			slot2          &= udptable->mask;
26330fff923SEric Dumazet 			hash2_nulladdr &= udptable->mask;
26430fff923SEric Dumazet 
26530fff923SEric Dumazet 			hslot2 = udp_hashslot2(udptable, slot2);
26630fff923SEric Dumazet 			if (hslot->count < hslot2->count)
26730fff923SEric Dumazet 				goto scan_primary_hash;
26830fff923SEric Dumazet 
26930fff923SEric Dumazet 			exist = udp_lib_lport_inuse2(net, snum, hslot2,
27030fff923SEric Dumazet 						     sk, saddr_comp);
27130fff923SEric Dumazet 			if (!exist && (hash2_nulladdr != slot2)) {
27230fff923SEric Dumazet 				hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
27330fff923SEric Dumazet 				exist = udp_lib_lport_inuse2(net, snum, hslot2,
27430fff923SEric Dumazet 							     sk, saddr_comp);
27530fff923SEric Dumazet 			}
27630fff923SEric Dumazet 			if (exist)
27730fff923SEric Dumazet 				goto fail_unlock;
27830fff923SEric Dumazet 			else
27930fff923SEric Dumazet 				goto found;
28030fff923SEric Dumazet 		}
28130fff923SEric Dumazet scan_primary_hash:
282f86dcc5aSEric Dumazet 		if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
283f86dcc5aSEric Dumazet 					saddr_comp, 0))
284645ca708SEric Dumazet 			goto fail_unlock;
285645ca708SEric Dumazet 	}
28698322f22SEric Dumazet found:
287c720c7e8SEric Dumazet 	inet_sk(sk)->inet_num = snum;
288d4cada4aSEric Dumazet 	udp_sk(sk)->udp_port_hash = snum;
289d4cada4aSEric Dumazet 	udp_sk(sk)->udp_portaddr_hash ^= snum;
2901da177e4SLinus Torvalds 	if (sk_unhashed(sk)) {
29188ab1932SEric Dumazet 		sk_nulls_add_node_rcu(sk, &hslot->head);
292fdcc8aa9SEric Dumazet 		hslot->count++;
293c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
294512615b6SEric Dumazet 
295512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
296512615b6SEric Dumazet 		spin_lock(&hslot2->lock);
297512615b6SEric Dumazet 		hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
298512615b6SEric Dumazet 					 &hslot2->head);
299512615b6SEric Dumazet 		hslot2->count++;
300512615b6SEric Dumazet 		spin_unlock(&hslot2->lock);
3011da177e4SLinus Torvalds 	}
30225030a7fSGerrit Renker 	error = 0;
303645ca708SEric Dumazet fail_unlock:
304645ca708SEric Dumazet 	spin_unlock_bh(&hslot->lock);
3051da177e4SLinus Torvalds fail:
30625030a7fSGerrit Renker 	return error;
3071da177e4SLinus Torvalds }
308c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port);
3091da177e4SLinus Torvalds 
310499923c7SVlad Yasevich static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
311db8dac20SDavid S. Miller {
312db8dac20SDavid S. Miller 	struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
313db8dac20SDavid S. Miller 
314db8dac20SDavid S. Miller 	return 	(!ipv6_only_sock(sk2)  &&
315c720c7e8SEric Dumazet 		 (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr ||
316c720c7e8SEric Dumazet 		   inet1->inet_rcv_saddr == inet2->inet_rcv_saddr));
317db8dac20SDavid S. Miller }
318db8dac20SDavid S. Miller 
319d4cada4aSEric Dumazet static unsigned int udp4_portaddr_hash(struct net *net, __be32 saddr,
320d4cada4aSEric Dumazet 				       unsigned int port)
321d4cada4aSEric Dumazet {
3220eae88f3SEric Dumazet 	return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
323d4cada4aSEric Dumazet }
324d4cada4aSEric Dumazet 
3256ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum)
326db8dac20SDavid S. Miller {
32730fff923SEric Dumazet 	unsigned int hash2_nulladdr =
3280eae88f3SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
32930fff923SEric Dumazet 	unsigned int hash2_partial =
33030fff923SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
33130fff923SEric Dumazet 
332d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
33330fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
33430fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr);
335db8dac20SDavid S. Miller }
336db8dac20SDavid S. Miller 
337645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
338645ca708SEric Dumazet 			 unsigned short hnum,
339645ca708SEric Dumazet 			 __be16 sport, __be32 daddr, __be16 dport, int dif)
340645ca708SEric Dumazet {
341645ca708SEric Dumazet 	int score = -1;
342645ca708SEric Dumazet 
343d4cada4aSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
344645ca708SEric Dumazet 			!ipv6_only_sock(sk)) {
345645ca708SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
346645ca708SEric Dumazet 
347ba418fa3STom Herbert 		score = (sk->sk_family == PF_INET ? 2 : 1);
348c720c7e8SEric Dumazet 		if (inet->inet_rcv_saddr) {
349c720c7e8SEric Dumazet 			if (inet->inet_rcv_saddr != daddr)
350645ca708SEric Dumazet 				return -1;
351ba418fa3STom Herbert 			score += 4;
352645ca708SEric Dumazet 		}
353c720c7e8SEric Dumazet 		if (inet->inet_daddr) {
354c720c7e8SEric Dumazet 			if (inet->inet_daddr != saddr)
355645ca708SEric Dumazet 				return -1;
356ba418fa3STom Herbert 			score += 4;
357645ca708SEric Dumazet 		}
358c720c7e8SEric Dumazet 		if (inet->inet_dport) {
359c720c7e8SEric Dumazet 			if (inet->inet_dport != sport)
360645ca708SEric Dumazet 				return -1;
361ba418fa3STom Herbert 			score += 4;
362645ca708SEric Dumazet 		}
363645ca708SEric Dumazet 		if (sk->sk_bound_dev_if) {
364645ca708SEric Dumazet 			if (sk->sk_bound_dev_if != dif)
365645ca708SEric Dumazet 				return -1;
366ba418fa3STom Herbert 			score += 4;
367645ca708SEric Dumazet 		}
368645ca708SEric Dumazet 	}
369645ca708SEric Dumazet 	return score;
370645ca708SEric Dumazet }
371645ca708SEric Dumazet 
3725051ebd2SEric Dumazet /*
3735051ebd2SEric Dumazet  * In this second variant, we check (daddr, dport) matches (inet_rcv_sadd, inet_num)
3745051ebd2SEric Dumazet  */
3755051ebd2SEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
3765051ebd2SEric Dumazet 				 __be32 saddr, __be16 sport,
3775051ebd2SEric Dumazet 				 __be32 daddr, unsigned int hnum, int dif)
3785051ebd2SEric Dumazet {
3795051ebd2SEric Dumazet 	int score = -1;
3805051ebd2SEric Dumazet 
3815051ebd2SEric Dumazet 	if (net_eq(sock_net(sk), net) && !ipv6_only_sock(sk)) {
3825051ebd2SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
3835051ebd2SEric Dumazet 
3845051ebd2SEric Dumazet 		if (inet->inet_rcv_saddr != daddr)
3855051ebd2SEric Dumazet 			return -1;
3865051ebd2SEric Dumazet 		if (inet->inet_num != hnum)
3875051ebd2SEric Dumazet 			return -1;
3885051ebd2SEric Dumazet 
389ba418fa3STom Herbert 		score = (sk->sk_family == PF_INET ? 2 : 1);
3905051ebd2SEric Dumazet 		if (inet->inet_daddr) {
3915051ebd2SEric Dumazet 			if (inet->inet_daddr != saddr)
3925051ebd2SEric Dumazet 				return -1;
393ba418fa3STom Herbert 			score += 4;
3945051ebd2SEric Dumazet 		}
3955051ebd2SEric Dumazet 		if (inet->inet_dport) {
3965051ebd2SEric Dumazet 			if (inet->inet_dport != sport)
3975051ebd2SEric Dumazet 				return -1;
398ba418fa3STom Herbert 			score += 4;
3995051ebd2SEric Dumazet 		}
4005051ebd2SEric Dumazet 		if (sk->sk_bound_dev_if) {
4015051ebd2SEric Dumazet 			if (sk->sk_bound_dev_if != dif)
4025051ebd2SEric Dumazet 				return -1;
403ba418fa3STom Herbert 			score += 4;
4045051ebd2SEric Dumazet 		}
4055051ebd2SEric Dumazet 	}
4065051ebd2SEric Dumazet 	return score;
4075051ebd2SEric Dumazet }
4085051ebd2SEric Dumazet 
4095051ebd2SEric Dumazet 
4105051ebd2SEric Dumazet /* called with read_rcu_lock() */
4115051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net,
4125051ebd2SEric Dumazet 		__be32 saddr, __be16 sport,
4135051ebd2SEric Dumazet 		__be32 daddr, unsigned int hnum, int dif,
4145051ebd2SEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
4155051ebd2SEric Dumazet {
4165051ebd2SEric Dumazet 	struct sock *sk, *result;
4175051ebd2SEric Dumazet 	struct hlist_nulls_node *node;
418ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
419ba418fa3STom Herbert 	u32 hash = 0;
4205051ebd2SEric Dumazet 
4215051ebd2SEric Dumazet begin:
4225051ebd2SEric Dumazet 	result = NULL;
423ba418fa3STom Herbert 	badness = 0;
4245051ebd2SEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
4255051ebd2SEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
4265051ebd2SEric Dumazet 				      daddr, hnum, dif);
4275051ebd2SEric Dumazet 		if (score > badness) {
4285051ebd2SEric Dumazet 			result = sk;
4295051ebd2SEric Dumazet 			badness = score;
430ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
431ba418fa3STom Herbert 			if (reuseport) {
432ba418fa3STom Herbert 				hash = inet_ehashfn(net, daddr, hnum,
433ba418fa3STom Herbert 						    saddr, htons(sport));
434ba418fa3STom Herbert 				matches = 1;
435ba418fa3STom Herbert 			}
436ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
437ba418fa3STom Herbert 			matches++;
438ba418fa3STom Herbert 			if (((u64)hash * matches) >> 32 == 0)
439ba418fa3STom Herbert 				result = sk;
440ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
4415051ebd2SEric Dumazet 		}
4425051ebd2SEric Dumazet 	}
4435051ebd2SEric Dumazet 	/*
4445051ebd2SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
4455051ebd2SEric Dumazet 	 * not the expected one, we must restart lookup.
4465051ebd2SEric Dumazet 	 * We probably met an item that was moved to another chain.
4475051ebd2SEric Dumazet 	 */
4485051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot2)
4495051ebd2SEric Dumazet 		goto begin;
4505051ebd2SEric Dumazet 	if (result) {
451c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
4525051ebd2SEric Dumazet 			result = NULL;
4535051ebd2SEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
4545051ebd2SEric Dumazet 				  daddr, hnum, dif) < badness)) {
4555051ebd2SEric Dumazet 			sock_put(result);
4565051ebd2SEric Dumazet 			goto begin;
4575051ebd2SEric Dumazet 		}
4585051ebd2SEric Dumazet 	}
4595051ebd2SEric Dumazet 	return result;
4605051ebd2SEric Dumazet }
4615051ebd2SEric Dumazet 
462db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
463db8dac20SDavid S. Miller  * harder than this. -DaveM
464db8dac20SDavid S. Miller  */
465fce82338SPavel Emelyanov struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
466db8dac20SDavid S. Miller 		__be16 sport, __be32 daddr, __be16 dport,
467645ca708SEric Dumazet 		int dif, struct udp_table *udptable)
468db8dac20SDavid S. Miller {
469271b72c7SEric Dumazet 	struct sock *sk, *result;
47088ab1932SEric Dumazet 	struct hlist_nulls_node *node;
471db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(dport);
4725051ebd2SEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
4735051ebd2SEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
474ba418fa3STom Herbert 	int score, badness, matches = 0, reuseport = 0;
475ba418fa3STom Herbert 	u32 hash = 0;
476db8dac20SDavid S. Miller 
477271b72c7SEric Dumazet 	rcu_read_lock();
4785051ebd2SEric Dumazet 	if (hslot->count > 10) {
4795051ebd2SEric Dumazet 		hash2 = udp4_portaddr_hash(net, daddr, hnum);
4805051ebd2SEric Dumazet 		slot2 = hash2 & udptable->mask;
4815051ebd2SEric Dumazet 		hslot2 = &udptable->hash2[slot2];
4825051ebd2SEric Dumazet 		if (hslot->count < hslot2->count)
4835051ebd2SEric Dumazet 			goto begin;
4845051ebd2SEric Dumazet 
4855051ebd2SEric Dumazet 		result = udp4_lib_lookup2(net, saddr, sport,
4865051ebd2SEric Dumazet 					  daddr, hnum, dif,
4875051ebd2SEric Dumazet 					  hslot2, slot2);
4885051ebd2SEric Dumazet 		if (!result) {
4890eae88f3SEric Dumazet 			hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
4905051ebd2SEric Dumazet 			slot2 = hash2 & udptable->mask;
4915051ebd2SEric Dumazet 			hslot2 = &udptable->hash2[slot2];
4925051ebd2SEric Dumazet 			if (hslot->count < hslot2->count)
4935051ebd2SEric Dumazet 				goto begin;
4945051ebd2SEric Dumazet 
4951223c67cSJorge Boncompte [DTI2] 			result = udp4_lib_lookup2(net, saddr, sport,
4960eae88f3SEric Dumazet 						  htonl(INADDR_ANY), hnum, dif,
4975051ebd2SEric Dumazet 						  hslot2, slot2);
4985051ebd2SEric Dumazet 		}
4995051ebd2SEric Dumazet 		rcu_read_unlock();
5005051ebd2SEric Dumazet 		return result;
5015051ebd2SEric Dumazet 	}
502271b72c7SEric Dumazet begin:
503271b72c7SEric Dumazet 	result = NULL;
504ba418fa3STom Herbert 	badness = 0;
50588ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
506645ca708SEric Dumazet 		score = compute_score(sk, net, saddr, hnum, sport,
507645ca708SEric Dumazet 				      daddr, dport, dif);
508645ca708SEric Dumazet 		if (score > badness) {
509db8dac20SDavid S. Miller 			result = sk;
510db8dac20SDavid S. Miller 			badness = score;
511ba418fa3STom Herbert 			reuseport = sk->sk_reuseport;
512ba418fa3STom Herbert 			if (reuseport) {
513ba418fa3STom Herbert 				hash = inet_ehashfn(net, daddr, hnum,
514ba418fa3STom Herbert 						    saddr, htons(sport));
515ba418fa3STom Herbert 				matches = 1;
516ba418fa3STom Herbert 			}
517ba418fa3STom Herbert 		} else if (score == badness && reuseport) {
518ba418fa3STom Herbert 			matches++;
519ba418fa3STom Herbert 			if (((u64)hash * matches) >> 32 == 0)
520ba418fa3STom Herbert 				result = sk;
521ba418fa3STom Herbert 			hash = next_pseudo_random32(hash);
522db8dac20SDavid S. Miller 		}
523db8dac20SDavid S. Miller 	}
52488ab1932SEric Dumazet 	/*
52588ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
52688ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
52788ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
52888ab1932SEric Dumazet 	 */
5295051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot)
53088ab1932SEric Dumazet 		goto begin;
53188ab1932SEric Dumazet 
532271b72c7SEric Dumazet 	if (result) {
533c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
534271b72c7SEric Dumazet 			result = NULL;
535271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, saddr, hnum, sport,
536271b72c7SEric Dumazet 				  daddr, dport, dif) < badness)) {
537271b72c7SEric Dumazet 			sock_put(result);
538271b72c7SEric Dumazet 			goto begin;
539271b72c7SEric Dumazet 		}
540271b72c7SEric Dumazet 	}
541271b72c7SEric Dumazet 	rcu_read_unlock();
542db8dac20SDavid S. Miller 	return result;
543db8dac20SDavid S. Miller }
544fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
545db8dac20SDavid S. Miller 
546607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
547607c4aafSKOVACS Krisztian 						 __be16 sport, __be16 dport,
548645ca708SEric Dumazet 						 struct udp_table *udptable)
549607c4aafSKOVACS Krisztian {
55023542618SKOVACS Krisztian 	struct sock *sk;
551607c4aafSKOVACS Krisztian 	const struct iphdr *iph = ip_hdr(skb);
552607c4aafSKOVACS Krisztian 
55323542618SKOVACS Krisztian 	if (unlikely(sk = skb_steal_sock(skb)))
55423542618SKOVACS Krisztian 		return sk;
55523542618SKOVACS Krisztian 	else
556adf30907SEric Dumazet 		return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
557607c4aafSKOVACS Krisztian 					 iph->daddr, dport, inet_iif(skb),
558607c4aafSKOVACS Krisztian 					 udptable);
559607c4aafSKOVACS Krisztian }
560607c4aafSKOVACS Krisztian 
561bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
562bcd41303SKOVACS Krisztian 			     __be32 daddr, __be16 dport, int dif)
563bcd41303SKOVACS Krisztian {
564645ca708SEric Dumazet 	return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
565bcd41303SKOVACS Krisztian }
566bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup);
567bcd41303SKOVACS Krisztian 
568920a4611SEric Dumazet static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
569db8dac20SDavid S. Miller 					     __be16 loc_port, __be32 loc_addr,
570db8dac20SDavid S. Miller 					     __be16 rmt_port, __be32 rmt_addr,
571db8dac20SDavid S. Miller 					     int dif)
572db8dac20SDavid S. Miller {
57388ab1932SEric Dumazet 	struct hlist_nulls_node *node;
574db8dac20SDavid S. Miller 	struct sock *s = sk;
575db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(loc_port);
576db8dac20SDavid S. Miller 
57788ab1932SEric Dumazet 	sk_nulls_for_each_from(s, node) {
578db8dac20SDavid S. Miller 		struct inet_sock *inet = inet_sk(s);
579db8dac20SDavid S. Miller 
580920a4611SEric Dumazet 		if (!net_eq(sock_net(s), net) ||
581d4cada4aSEric Dumazet 		    udp_sk(s)->udp_port_hash != hnum ||
582c720c7e8SEric Dumazet 		    (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
583c720c7e8SEric Dumazet 		    (inet->inet_dport != rmt_port && inet->inet_dport) ||
584c720c7e8SEric Dumazet 		    (inet->inet_rcv_saddr &&
585c720c7e8SEric Dumazet 		     inet->inet_rcv_saddr != loc_addr) ||
586db8dac20SDavid S. Miller 		    ipv6_only_sock(s) ||
587db8dac20SDavid S. Miller 		    (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
588db8dac20SDavid S. Miller 			continue;
589db8dac20SDavid S. Miller 		if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
590db8dac20SDavid S. Miller 			continue;
591db8dac20SDavid S. Miller 		goto found;
592db8dac20SDavid S. Miller 	}
593db8dac20SDavid S. Miller 	s = NULL;
594db8dac20SDavid S. Miller found:
595db8dac20SDavid S. Miller 	return s;
596db8dac20SDavid S. Miller }
597db8dac20SDavid S. Miller 
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);
66155be7a9cSDavid S. Miller 		break;
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  */
707f6b9664fSHerbert Xu static 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 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
711f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
712f6b9664fSHerbert Xu 	int len = skb->len - offset;
713f6b9664fSHerbert Xu 	int hlen = len;
714db8dac20SDavid S. Miller 	__wsum csum = 0;
715db8dac20SDavid S. Miller 
716f6b9664fSHerbert Xu 	if (!frags) {
717db8dac20SDavid S. Miller 		/*
718db8dac20SDavid S. Miller 		 * Only one fragment on the socket.
719db8dac20SDavid S. Miller 		 */
720db8dac20SDavid S. Miller 		skb->csum_start = skb_transport_header(skb) - skb->head;
721db8dac20SDavid S. Miller 		skb->csum_offset = offsetof(struct udphdr, check);
722f6b9664fSHerbert Xu 		uh->check = ~csum_tcpudp_magic(src, dst, len,
723f6b9664fSHerbert Xu 					       IPPROTO_UDP, 0);
724db8dac20SDavid S. Miller 	} else {
725db8dac20SDavid S. Miller 		/*
726db8dac20SDavid S. Miller 		 * HW-checksum won't work as there are two or more
727db8dac20SDavid S. Miller 		 * fragments on the socket so that all csums of sk_buffs
728db8dac20SDavid S. Miller 		 * should be together
729db8dac20SDavid S. Miller 		 */
730f6b9664fSHerbert Xu 		do {
731f6b9664fSHerbert Xu 			csum = csum_add(csum, frags->csum);
732f6b9664fSHerbert Xu 			hlen -= frags->len;
733f6b9664fSHerbert Xu 		} while ((frags = frags->next));
734db8dac20SDavid S. Miller 
735f6b9664fSHerbert Xu 		csum = skb_checksum(skb, offset, hlen, csum);
736db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
737db8dac20SDavid S. Miller 
738db8dac20SDavid S. Miller 		uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
739db8dac20SDavid S. Miller 		if (uh->check == 0)
740db8dac20SDavid S. Miller 			uh->check = CSUM_MANGLED_0;
741db8dac20SDavid S. Miller 	}
742db8dac20SDavid S. Miller }
743db8dac20SDavid S. Miller 
74479ab0531SDavid S. Miller static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4)
745f6b9664fSHerbert Xu {
746f6b9664fSHerbert Xu 	struct sock *sk = skb->sk;
747f6b9664fSHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
748f6b9664fSHerbert Xu 	struct udphdr *uh;
749f6b9664fSHerbert Xu 	int err = 0;
750f6b9664fSHerbert Xu 	int is_udplite = IS_UDPLITE(sk);
751f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
752f6b9664fSHerbert Xu 	int len = skb->len - offset;
753f6b9664fSHerbert Xu 	__wsum csum = 0;
754f6b9664fSHerbert Xu 
755f6b9664fSHerbert Xu 	/*
756f6b9664fSHerbert Xu 	 * Create a UDP header
757f6b9664fSHerbert Xu 	 */
758f6b9664fSHerbert Xu 	uh = udp_hdr(skb);
759f6b9664fSHerbert Xu 	uh->source = inet->inet_sport;
76079ab0531SDavid S. Miller 	uh->dest = fl4->fl4_dport;
761f6b9664fSHerbert Xu 	uh->len = htons(len);
762f6b9664fSHerbert Xu 	uh->check = 0;
763f6b9664fSHerbert Xu 
764f6b9664fSHerbert Xu 	if (is_udplite)  				 /*     UDP-Lite      */
765f6b9664fSHerbert Xu 		csum = udplite_csum(skb);
766f6b9664fSHerbert Xu 
767f6b9664fSHerbert Xu 	else if (sk->sk_no_check == UDP_CSUM_NOXMIT) {   /* UDP csum disabled */
768f6b9664fSHerbert Xu 
769f6b9664fSHerbert Xu 		skb->ip_summed = CHECKSUM_NONE;
770f6b9664fSHerbert Xu 		goto send;
771f6b9664fSHerbert Xu 
772f6b9664fSHerbert Xu 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
773f6b9664fSHerbert Xu 
77479ab0531SDavid S. Miller 		udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
775f6b9664fSHerbert Xu 		goto send;
776f6b9664fSHerbert Xu 
777f6b9664fSHerbert Xu 	} else
778f6b9664fSHerbert Xu 		csum = udp_csum(skb);
779f6b9664fSHerbert Xu 
780f6b9664fSHerbert Xu 	/* add protocol-dependent pseudo-header */
78179ab0531SDavid S. Miller 	uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
782f6b9664fSHerbert Xu 				      sk->sk_protocol, csum);
783f6b9664fSHerbert Xu 	if (uh->check == 0)
784f6b9664fSHerbert Xu 		uh->check = CSUM_MANGLED_0;
785f6b9664fSHerbert Xu 
786f6b9664fSHerbert Xu send:
787b5ec8eeaSEric Dumazet 	err = ip_send_skb(sock_net(sk), skb);
788f6b9664fSHerbert Xu 	if (err) {
789f6b9664fSHerbert Xu 		if (err == -ENOBUFS && !inet->recverr) {
790f6b9664fSHerbert Xu 			UDP_INC_STATS_USER(sock_net(sk),
791f6b9664fSHerbert Xu 					   UDP_MIB_SNDBUFERRORS, is_udplite);
792f6b9664fSHerbert Xu 			err = 0;
793f6b9664fSHerbert Xu 		}
794f6b9664fSHerbert Xu 	} else
795f6b9664fSHerbert Xu 		UDP_INC_STATS_USER(sock_net(sk),
796f6b9664fSHerbert Xu 				   UDP_MIB_OUTDATAGRAMS, is_udplite);
797f6b9664fSHerbert Xu 	return err;
798f6b9664fSHerbert Xu }
799f6b9664fSHerbert Xu 
800db8dac20SDavid S. Miller /*
801db8dac20SDavid S. Miller  * Push out all pending data as one UDP datagram. Socket is locked.
802db8dac20SDavid S. Miller  */
803db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk)
804db8dac20SDavid S. Miller {
805db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
806db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
807b6f21b26SDavid S. Miller 	struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
808db8dac20SDavid S. Miller 	struct sk_buff *skb;
809db8dac20SDavid S. Miller 	int err = 0;
810db8dac20SDavid S. Miller 
81177968b78SDavid S. Miller 	skb = ip_finish_skb(sk, fl4);
812f6b9664fSHerbert Xu 	if (!skb)
813db8dac20SDavid S. Miller 		goto out;
814db8dac20SDavid S. Miller 
81579ab0531SDavid S. Miller 	err = udp_send_skb(skb, fl4);
816db8dac20SDavid S. Miller 
817db8dac20SDavid S. Miller out:
818db8dac20SDavid S. Miller 	up->len = 0;
819db8dac20SDavid S. Miller 	up->pending = 0;
820db8dac20SDavid S. Miller 	return err;
821db8dac20SDavid S. Miller }
822db8dac20SDavid S. Miller 
823db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
824db8dac20SDavid S. Miller 		size_t len)
825db8dac20SDavid S. Miller {
826db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
827db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
828e474995fSDavid S. Miller 	struct flowi4 fl4_stack;
829b6f21b26SDavid S. Miller 	struct flowi4 *fl4;
830db8dac20SDavid S. Miller 	int ulen = len;
831db8dac20SDavid S. Miller 	struct ipcm_cookie ipc;
832db8dac20SDavid S. Miller 	struct rtable *rt = NULL;
833db8dac20SDavid S. Miller 	int free = 0;
834db8dac20SDavid S. Miller 	int connected = 0;
835db8dac20SDavid S. Miller 	__be32 daddr, faddr, saddr;
836db8dac20SDavid S. Miller 	__be16 dport;
837db8dac20SDavid S. Miller 	u8  tos;
838db8dac20SDavid S. Miller 	int err, is_udplite = IS_UDPLITE(sk);
839db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
840db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
841903ab86dSHerbert Xu 	struct sk_buff *skb;
842f6d8bd05SEric Dumazet 	struct ip_options_data opt_copy;
843db8dac20SDavid S. Miller 
844db8dac20SDavid S. Miller 	if (len > 0xFFFF)
845db8dac20SDavid S. Miller 		return -EMSGSIZE;
846db8dac20SDavid S. Miller 
847db8dac20SDavid S. Miller 	/*
848db8dac20SDavid S. Miller 	 *	Check the flags.
849db8dac20SDavid S. Miller 	 */
850db8dac20SDavid S. Miller 
851db8dac20SDavid S. Miller 	if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
852db8dac20SDavid S. Miller 		return -EOPNOTSUPP;
853db8dac20SDavid S. Miller 
854db8dac20SDavid S. Miller 	ipc.opt = NULL;
8552244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
856db8dac20SDavid S. Miller 
857903ab86dSHerbert Xu 	getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
858903ab86dSHerbert Xu 
859f5fca608SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
860db8dac20SDavid S. Miller 	if (up->pending) {
861db8dac20SDavid S. Miller 		/*
862db8dac20SDavid S. Miller 		 * There are pending frames.
863db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
864db8dac20SDavid S. Miller 		 */
865db8dac20SDavid S. Miller 		lock_sock(sk);
866db8dac20SDavid S. Miller 		if (likely(up->pending)) {
867db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET)) {
868db8dac20SDavid S. Miller 				release_sock(sk);
869db8dac20SDavid S. Miller 				return -EINVAL;
870db8dac20SDavid S. Miller 			}
871db8dac20SDavid S. Miller 			goto do_append_data;
872db8dac20SDavid S. Miller 		}
873db8dac20SDavid S. Miller 		release_sock(sk);
874db8dac20SDavid S. Miller 	}
875db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
876db8dac20SDavid S. Miller 
877db8dac20SDavid S. Miller 	/*
878db8dac20SDavid S. Miller 	 *	Get and verify the address.
879db8dac20SDavid S. Miller 	 */
880db8dac20SDavid S. Miller 	if (msg->msg_name) {
881db8dac20SDavid S. Miller 		struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
882db8dac20SDavid S. Miller 		if (msg->msg_namelen < sizeof(*usin))
883db8dac20SDavid S. Miller 			return -EINVAL;
884db8dac20SDavid S. Miller 		if (usin->sin_family != AF_INET) {
885db8dac20SDavid S. Miller 			if (usin->sin_family != AF_UNSPEC)
886db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
887db8dac20SDavid S. Miller 		}
888db8dac20SDavid S. Miller 
889db8dac20SDavid S. Miller 		daddr = usin->sin_addr.s_addr;
890db8dac20SDavid S. Miller 		dport = usin->sin_port;
891db8dac20SDavid S. Miller 		if (dport == 0)
892db8dac20SDavid S. Miller 			return -EINVAL;
893db8dac20SDavid S. Miller 	} else {
894db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
895db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
896c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
897c720c7e8SEric Dumazet 		dport = inet->inet_dport;
898db8dac20SDavid S. Miller 		/* Open fast path for connected socket.
899db8dac20SDavid S. Miller 		   Route will not be used, if at least one option is set.
900db8dac20SDavid S. Miller 		 */
901db8dac20SDavid S. Miller 		connected = 1;
902db8dac20SDavid S. Miller 	}
903c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
904db8dac20SDavid S. Miller 
905db8dac20SDavid S. Miller 	ipc.oif = sk->sk_bound_dev_if;
906bf84a010SDaniel Borkmann 
907bf84a010SDaniel Borkmann 	sock_tx_timestamp(sk, &ipc.tx_flags);
908bf84a010SDaniel Borkmann 
909db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
9103b1e0a65SYOSHIFUJI Hideaki 		err = ip_cmsg_send(sock_net(sk), msg, &ipc);
911db8dac20SDavid S. Miller 		if (err)
912db8dac20SDavid S. Miller 			return err;
913db8dac20SDavid S. Miller 		if (ipc.opt)
914db8dac20SDavid S. Miller 			free = 1;
915db8dac20SDavid S. Miller 		connected = 0;
916db8dac20SDavid S. Miller 	}
917f6d8bd05SEric Dumazet 	if (!ipc.opt) {
918f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
919f6d8bd05SEric Dumazet 
920f6d8bd05SEric Dumazet 		rcu_read_lock();
921f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference(inet->inet_opt);
922f6d8bd05SEric Dumazet 		if (inet_opt) {
923f6d8bd05SEric Dumazet 			memcpy(&opt_copy, inet_opt,
924f6d8bd05SEric Dumazet 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
925f6d8bd05SEric Dumazet 			ipc.opt = &opt_copy.opt;
926f6d8bd05SEric Dumazet 		}
927f6d8bd05SEric Dumazet 		rcu_read_unlock();
928f6d8bd05SEric Dumazet 	}
929db8dac20SDavid S. Miller 
930db8dac20SDavid S. Miller 	saddr = ipc.addr;
931db8dac20SDavid S. Miller 	ipc.addr = faddr = daddr;
932db8dac20SDavid S. Miller 
933f6d8bd05SEric Dumazet 	if (ipc.opt && ipc.opt->opt.srr) {
934db8dac20SDavid S. Miller 		if (!daddr)
935db8dac20SDavid S. Miller 			return -EINVAL;
936f6d8bd05SEric Dumazet 		faddr = ipc.opt->opt.faddr;
937db8dac20SDavid S. Miller 		connected = 0;
938db8dac20SDavid S. Miller 	}
939db8dac20SDavid S. Miller 	tos = RT_TOS(inet->tos);
940db8dac20SDavid S. Miller 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
941db8dac20SDavid S. Miller 	    (msg->msg_flags & MSG_DONTROUTE) ||
942f6d8bd05SEric Dumazet 	    (ipc.opt && ipc.opt->opt.is_strictroute)) {
943db8dac20SDavid S. Miller 		tos |= RTO_ONLINK;
944db8dac20SDavid S. Miller 		connected = 0;
945db8dac20SDavid S. Miller 	}
946db8dac20SDavid S. Miller 
947db8dac20SDavid S. Miller 	if (ipv4_is_multicast(daddr)) {
948db8dac20SDavid S. Miller 		if (!ipc.oif)
949db8dac20SDavid S. Miller 			ipc.oif = inet->mc_index;
950db8dac20SDavid S. Miller 		if (!saddr)
951db8dac20SDavid S. Miller 			saddr = inet->mc_addr;
952db8dac20SDavid S. Miller 		connected = 0;
95376e21053SErich E. Hoover 	} else if (!ipc.oif)
95476e21053SErich E. Hoover 		ipc.oif = inet->uc_index;
955db8dac20SDavid S. Miller 
956db8dac20SDavid S. Miller 	if (connected)
957db8dac20SDavid S. Miller 		rt = (struct rtable *)sk_dst_check(sk, 0);
958db8dac20SDavid S. Miller 
959db8dac20SDavid S. Miller 	if (rt == NULL) {
96084a3aa00SPavel Emelyanov 		struct net *net = sock_net(sk);
96184a3aa00SPavel Emelyanov 
962e474995fSDavid S. Miller 		fl4 = &fl4_stack;
963e474995fSDavid S. Miller 		flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
964c0951cbcSDavid S. Miller 				   RT_SCOPE_UNIVERSE, sk->sk_protocol,
965c0951cbcSDavid S. Miller 				   inet_sk_flowi_flags(sk)|FLOWI_FLAG_CAN_SLEEP,
966c0951cbcSDavid S. Miller 				   faddr, saddr, dport, inet->inet_sport);
967c0951cbcSDavid S. Miller 
968e474995fSDavid S. Miller 		security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
969e474995fSDavid S. Miller 		rt = ip_route_output_flow(net, fl4, sk);
970b23dd4feSDavid S. Miller 		if (IS_ERR(rt)) {
971b23dd4feSDavid S. Miller 			err = PTR_ERR(rt);
97206dc94b1SDavid S. Miller 			rt = NULL;
973db8dac20SDavid S. Miller 			if (err == -ENETUNREACH)
9747c73a6faSPavel Emelyanov 				IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
975db8dac20SDavid S. Miller 			goto out;
976db8dac20SDavid S. Miller 		}
977db8dac20SDavid S. Miller 
978db8dac20SDavid S. Miller 		err = -EACCES;
979db8dac20SDavid S. Miller 		if ((rt->rt_flags & RTCF_BROADCAST) &&
980db8dac20SDavid S. Miller 		    !sock_flag(sk, SOCK_BROADCAST))
981db8dac20SDavid S. Miller 			goto out;
982db8dac20SDavid S. Miller 		if (connected)
983d8d1f30bSChangli Gao 			sk_dst_set(sk, dst_clone(&rt->dst));
984db8dac20SDavid S. Miller 	}
985db8dac20SDavid S. Miller 
986db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
987db8dac20SDavid S. Miller 		goto do_confirm;
988db8dac20SDavid S. Miller back_from_confirm:
989db8dac20SDavid S. Miller 
990e474995fSDavid S. Miller 	saddr = fl4->saddr;
991db8dac20SDavid S. Miller 	if (!ipc.addr)
992e474995fSDavid S. Miller 		daddr = ipc.addr = fl4->daddr;
993db8dac20SDavid S. Miller 
994903ab86dSHerbert Xu 	/* Lockless fast path for the non-corking case. */
995903ab86dSHerbert Xu 	if (!corkreq) {
99677968b78SDavid S. Miller 		skb = ip_make_skb(sk, fl4, getfrag, msg->msg_iov, ulen,
997903ab86dSHerbert Xu 				  sizeof(struct udphdr), &ipc, &rt,
998903ab86dSHerbert Xu 				  msg->msg_flags);
999903ab86dSHerbert Xu 		err = PTR_ERR(skb);
100050c3a487SYOSHIFUJI Hideaki / 吉藤英明 		if (!IS_ERR_OR_NULL(skb))
100179ab0531SDavid S. Miller 			err = udp_send_skb(skb, fl4);
1002903ab86dSHerbert Xu 		goto out;
1003903ab86dSHerbert Xu 	}
1004903ab86dSHerbert Xu 
1005db8dac20SDavid S. Miller 	lock_sock(sk);
1006db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1007db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1008db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1009db8dac20SDavid S. Miller 		release_sock(sk);
1010db8dac20SDavid S. Miller 
1011afd46503SJoe Perches 		LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("cork app bug 2\n"));
1012db8dac20SDavid S. Miller 		err = -EINVAL;
1013db8dac20SDavid S. Miller 		goto out;
1014db8dac20SDavid S. Miller 	}
1015db8dac20SDavid S. Miller 	/*
1016db8dac20SDavid S. Miller 	 *	Now cork the socket to pend data.
1017db8dac20SDavid S. Miller 	 */
1018b6f21b26SDavid S. Miller 	fl4 = &inet->cork.fl.u.ip4;
1019b6f21b26SDavid S. Miller 	fl4->daddr = daddr;
1020b6f21b26SDavid S. Miller 	fl4->saddr = saddr;
10219cce96dfSDavid S. Miller 	fl4->fl4_dport = dport;
10229cce96dfSDavid S. Miller 	fl4->fl4_sport = inet->inet_sport;
1023db8dac20SDavid S. Miller 	up->pending = AF_INET;
1024db8dac20SDavid S. Miller 
1025db8dac20SDavid S. Miller do_append_data:
1026db8dac20SDavid S. Miller 	up->len += ulen;
1027f5fca608SDavid S. Miller 	err = ip_append_data(sk, fl4, getfrag, msg->msg_iov, ulen,
10282e77d89bSEric Dumazet 			     sizeof(struct udphdr), &ipc, &rt,
1029db8dac20SDavid S. Miller 			     corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1030db8dac20SDavid S. Miller 	if (err)
1031db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1032db8dac20SDavid S. Miller 	else if (!corkreq)
1033db8dac20SDavid S. Miller 		err = udp_push_pending_frames(sk);
1034db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1035db8dac20SDavid S. Miller 		up->pending = 0;
1036db8dac20SDavid S. Miller 	release_sock(sk);
1037db8dac20SDavid S. Miller 
1038db8dac20SDavid S. Miller out:
1039db8dac20SDavid S. Miller 	ip_rt_put(rt);
1040db8dac20SDavid S. Miller 	if (free)
1041db8dac20SDavid S. Miller 		kfree(ipc.opt);
1042db8dac20SDavid S. Miller 	if (!err)
1043db8dac20SDavid S. Miller 		return len;
1044db8dac20SDavid S. Miller 	/*
1045db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1046db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1047db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1048db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1049db8dac20SDavid S. Miller 	 * seems like overkill.
1050db8dac20SDavid S. Miller 	 */
1051db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1052629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1053629ca23cSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1054db8dac20SDavid S. Miller 	}
1055db8dac20SDavid S. Miller 	return err;
1056db8dac20SDavid S. Miller 
1057db8dac20SDavid S. Miller do_confirm:
1058d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
1059db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1060db8dac20SDavid S. Miller 		goto back_from_confirm;
1061db8dac20SDavid S. Miller 	err = 0;
1062db8dac20SDavid S. Miller 	goto out;
1063db8dac20SDavid S. Miller }
1064c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg);
1065db8dac20SDavid S. Miller 
1066db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset,
1067db8dac20SDavid S. Miller 		 size_t size, int flags)
1068db8dac20SDavid S. Miller {
1069f5fca608SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1070db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1071db8dac20SDavid S. Miller 	int ret;
1072db8dac20SDavid S. Miller 
1073db8dac20SDavid S. Miller 	if (!up->pending) {
1074db8dac20SDavid S. Miller 		struct msghdr msg = {	.msg_flags = flags|MSG_MORE };
1075db8dac20SDavid S. Miller 
1076db8dac20SDavid S. Miller 		/* Call udp_sendmsg to specify destination address which
1077db8dac20SDavid S. Miller 		 * sendpage interface can't pass.
1078db8dac20SDavid S. Miller 		 * This will succeed only when the socket is connected.
1079db8dac20SDavid S. Miller 		 */
1080db8dac20SDavid S. Miller 		ret = udp_sendmsg(NULL, sk, &msg, 0);
1081db8dac20SDavid S. Miller 		if (ret < 0)
1082db8dac20SDavid S. Miller 			return ret;
1083db8dac20SDavid S. Miller 	}
1084db8dac20SDavid S. Miller 
1085db8dac20SDavid S. Miller 	lock_sock(sk);
1086db8dac20SDavid S. Miller 
1087db8dac20SDavid S. Miller 	if (unlikely(!up->pending)) {
1088db8dac20SDavid S. Miller 		release_sock(sk);
1089db8dac20SDavid S. Miller 
1090afd46503SJoe Perches 		LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("udp cork app bug 3\n"));
1091db8dac20SDavid S. Miller 		return -EINVAL;
1092db8dac20SDavid S. Miller 	}
1093db8dac20SDavid S. Miller 
1094f5fca608SDavid S. Miller 	ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
1095f5fca608SDavid S. Miller 			     page, offset, size, flags);
1096db8dac20SDavid S. Miller 	if (ret == -EOPNOTSUPP) {
1097db8dac20SDavid S. Miller 		release_sock(sk);
1098db8dac20SDavid S. Miller 		return sock_no_sendpage(sk->sk_socket, page, offset,
1099db8dac20SDavid S. Miller 					size, flags);
1100db8dac20SDavid S. Miller 	}
1101db8dac20SDavid S. Miller 	if (ret < 0) {
1102db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1103db8dac20SDavid S. Miller 		goto out;
1104db8dac20SDavid S. Miller 	}
1105db8dac20SDavid S. Miller 
1106db8dac20SDavid S. Miller 	up->len += size;
1107db8dac20SDavid S. Miller 	if (!(up->corkflag || (flags&MSG_MORE)))
1108db8dac20SDavid S. Miller 		ret = udp_push_pending_frames(sk);
1109db8dac20SDavid S. Miller 	if (!ret)
1110db8dac20SDavid S. Miller 		ret = size;
1111db8dac20SDavid S. Miller out:
1112db8dac20SDavid S. Miller 	release_sock(sk);
1113db8dac20SDavid S. Miller 	return ret;
1114db8dac20SDavid S. Miller }
1115db8dac20SDavid S. Miller 
111685584672SEric Dumazet 
111785584672SEric Dumazet /**
111885584672SEric Dumazet  *	first_packet_length	- return length of first packet in receive queue
111985584672SEric Dumazet  *	@sk: socket
112085584672SEric Dumazet  *
112185584672SEric Dumazet  *	Drops all bad checksum frames, until a valid one is found.
112285584672SEric Dumazet  *	Returns the length of found skb, or 0 if none is found.
112385584672SEric Dumazet  */
112485584672SEric Dumazet static unsigned int first_packet_length(struct sock *sk)
112585584672SEric Dumazet {
112685584672SEric Dumazet 	struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
112785584672SEric Dumazet 	struct sk_buff *skb;
112885584672SEric Dumazet 	unsigned int res;
112985584672SEric Dumazet 
113085584672SEric Dumazet 	__skb_queue_head_init(&list_kill);
113185584672SEric Dumazet 
113285584672SEric Dumazet 	spin_lock_bh(&rcvq->lock);
113385584672SEric Dumazet 	while ((skb = skb_peek(rcvq)) != NULL &&
113485584672SEric Dumazet 		udp_lib_checksum_complete(skb)) {
11356a5dc9e5SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS,
11366a5dc9e5SEric Dumazet 				 IS_UDPLITE(sk));
113785584672SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
113885584672SEric Dumazet 				 IS_UDPLITE(sk));
11398edf19c2SEric Dumazet 		atomic_inc(&sk->sk_drops);
114085584672SEric Dumazet 		__skb_unlink(skb, rcvq);
114185584672SEric Dumazet 		__skb_queue_tail(&list_kill, skb);
114285584672SEric Dumazet 	}
114385584672SEric Dumazet 	res = skb ? skb->len : 0;
114485584672SEric Dumazet 	spin_unlock_bh(&rcvq->lock);
114585584672SEric Dumazet 
114685584672SEric Dumazet 	if (!skb_queue_empty(&list_kill)) {
11478a74ad60SEric Dumazet 		bool slow = lock_sock_fast(sk);
11488a74ad60SEric Dumazet 
114985584672SEric Dumazet 		__skb_queue_purge(&list_kill);
115085584672SEric Dumazet 		sk_mem_reclaim_partial(sk);
11518a74ad60SEric Dumazet 		unlock_sock_fast(sk, slow);
115285584672SEric Dumazet 	}
115385584672SEric Dumazet 	return res;
115485584672SEric Dumazet }
115585584672SEric Dumazet 
11561da177e4SLinus Torvalds /*
11571da177e4SLinus Torvalds  *	IOCTL requests applicable to the UDP protocol
11581da177e4SLinus Torvalds  */
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
11611da177e4SLinus Torvalds {
11626516c655SStephen Hemminger 	switch (cmd) {
11631da177e4SLinus Torvalds 	case SIOCOUTQ:
11641da177e4SLinus Torvalds 	{
116531e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
116631e6d363SEric Dumazet 
11671da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11681da177e4SLinus Torvalds 	}
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	case SIOCINQ:
11711da177e4SLinus Torvalds 	{
117285584672SEric Dumazet 		unsigned int amount = first_packet_length(sk);
11731da177e4SLinus Torvalds 
117485584672SEric Dumazet 		if (amount)
11751da177e4SLinus Torvalds 			/*
11761da177e4SLinus Torvalds 			 * We will only return the amount
11771da177e4SLinus Torvalds 			 * of this packet since that is all
11781da177e4SLinus Torvalds 			 * that will be read.
11791da177e4SLinus Torvalds 			 */
118085584672SEric Dumazet 			amount -= sizeof(struct udphdr);
118185584672SEric Dumazet 
11821da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11831da177e4SLinus Torvalds 	}
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	default:
11861da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
11871da177e4SLinus Torvalds 	}
11886516c655SStephen Hemminger 
11896516c655SStephen Hemminger 	return 0;
11901da177e4SLinus Torvalds }
1191c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl);
11921da177e4SLinus Torvalds 
1193db8dac20SDavid S. Miller /*
1194db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
1195db8dac20SDavid S. Miller  * 	return it, otherwise we block.
1196db8dac20SDavid S. Miller  */
1197db8dac20SDavid S. Miller 
1198db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1199db8dac20SDavid S. Miller 		size_t len, int noblock, int flags, int *addr_len)
1200db8dac20SDavid S. Miller {
1201db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1202db8dac20SDavid S. Miller 	struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
1203db8dac20SDavid S. Miller 	struct sk_buff *skb;
120459c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
12053f518bf7SPavel Emelyanov 	int peeked, off = 0;
1206db8dac20SDavid S. Miller 	int err;
1207db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
12088a74ad60SEric Dumazet 	bool slow;
1209db8dac20SDavid S. Miller 
1210db8dac20SDavid S. Miller 	/*
1211db8dac20SDavid S. Miller 	 *	Check any passed addresses
1212db8dac20SDavid S. Miller 	 */
1213db8dac20SDavid S. Miller 	if (addr_len)
1214db8dac20SDavid S. Miller 		*addr_len = sizeof(*sin);
1215db8dac20SDavid S. Miller 
1216db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
1217db8dac20SDavid S. Miller 		return ip_recv_error(sk, msg, len);
1218db8dac20SDavid S. Miller 
1219db8dac20SDavid S. Miller try_again:
1220db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
12213f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
1222db8dac20SDavid S. Miller 	if (!skb)
1223db8dac20SDavid S. Miller 		goto out;
1224db8dac20SDavid S. Miller 
1225db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
122659c2cdaeSDavid S. Miller 	copied = len;
122759c2cdaeSDavid S. Miller 	if (copied > ulen)
122859c2cdaeSDavid S. Miller 		copied = ulen;
122959c2cdaeSDavid S. Miller 	else if (copied < ulen)
1230db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
1231db8dac20SDavid S. Miller 
1232db8dac20SDavid S. Miller 	/*
1233db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
1234db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
1235db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
1236db8dac20SDavid S. Miller 	 */
1237db8dac20SDavid S. Miller 
123859c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
1239db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
1240db8dac20SDavid S. Miller 			goto csum_copy_err;
1241db8dac20SDavid S. Miller 	}
1242db8dac20SDavid S. Miller 
1243db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
1244db8dac20SDavid S. Miller 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
124559c2cdaeSDavid S. Miller 					      msg->msg_iov, copied);
1246db8dac20SDavid S. Miller 	else {
1247c482c568SEric Dumazet 		err = skb_copy_and_csum_datagram_iovec(skb,
1248c482c568SEric Dumazet 						       sizeof(struct udphdr),
1249c482c568SEric Dumazet 						       msg->msg_iov);
1250db8dac20SDavid S. Miller 
1251db8dac20SDavid S. Miller 		if (err == -EINVAL)
1252db8dac20SDavid S. Miller 			goto csum_copy_err;
1253db8dac20SDavid S. Miller 	}
1254db8dac20SDavid S. Miller 
125522911fc5SEric Dumazet 	if (unlikely(err)) {
125622911fc5SEric Dumazet 		trace_kfree_skb(skb, udp_recvmsg);
1257979402b1SEric Dumazet 		if (!peeked) {
1258979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
1259979402b1SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
1260979402b1SEric Dumazet 					   UDP_MIB_INERRORS, is_udplite);
1261979402b1SEric Dumazet 		}
1262db8dac20SDavid S. Miller 		goto out_free;
126322911fc5SEric Dumazet 	}
1264db8dac20SDavid S. Miller 
1265db8dac20SDavid S. Miller 	if (!peeked)
1266629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1267629ca23cSPavel Emelyanov 				UDP_MIB_INDATAGRAMS, is_udplite);
1268db8dac20SDavid S. Miller 
12693b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
1270db8dac20SDavid S. Miller 
1271db8dac20SDavid S. Miller 	/* Copy the address. */
1272c482c568SEric Dumazet 	if (sin) {
1273db8dac20SDavid S. Miller 		sin->sin_family = AF_INET;
1274db8dac20SDavid S. Miller 		sin->sin_port = udp_hdr(skb)->source;
1275db8dac20SDavid S. Miller 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1276db8dac20SDavid S. Miller 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1277db8dac20SDavid S. Miller 	}
1278db8dac20SDavid S. Miller 	if (inet->cmsg_flags)
1279db8dac20SDavid S. Miller 		ip_cmsg_recv(msg, skb);
1280db8dac20SDavid S. Miller 
128159c2cdaeSDavid S. Miller 	err = copied;
1282db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
1283db8dac20SDavid S. Miller 		err = ulen;
1284db8dac20SDavid S. Miller 
1285db8dac20SDavid S. Miller out_free:
12869d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
1287db8dac20SDavid S. Miller out:
1288db8dac20SDavid S. Miller 	return err;
1289db8dac20SDavid S. Miller 
1290db8dac20SDavid S. Miller csum_copy_err:
12918a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
12926a5dc9e5SEric Dumazet 	if (!skb_kill_datagram(sk, skb, flags)) {
12936a5dc9e5SEric Dumazet 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1294629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
12956a5dc9e5SEric Dumazet 	}
12968a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
1297db8dac20SDavid S. Miller 
1298db8dac20SDavid S. Miller 	if (noblock)
1299db8dac20SDavid S. Miller 		return -EAGAIN;
13009cfaa8deSXufeng Zhang 
13019cfaa8deSXufeng Zhang 	/* starting over for a new packet */
13029cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
1303db8dac20SDavid S. Miller 	goto try_again;
1304db8dac20SDavid S. Miller }
1305db8dac20SDavid S. Miller 
1306db8dac20SDavid S. Miller 
13071da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags)
13081da177e4SLinus Torvalds {
13091da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
13101da177e4SLinus Torvalds 	/*
13111da177e4SLinus Torvalds 	 *	1003.1g - break association.
13121da177e4SLinus Torvalds 	 */
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
1315c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
1316c720c7e8SEric Dumazet 	inet->inet_dport = 0;
1317bdeab991STom Herbert 	sock_rps_reset_rxhash(sk);
13181da177e4SLinus Torvalds 	sk->sk_bound_dev_if = 0;
13191da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
13201da177e4SLinus Torvalds 		inet_reset_saddr(sk);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
13231da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1324c720c7e8SEric Dumazet 		inet->inet_sport = 0;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 	sk_dst_reset(sk);
13271da177e4SLinus Torvalds 	return 0;
13281da177e4SLinus Torvalds }
1329c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect);
13301da177e4SLinus Torvalds 
1331645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk)
1332645ca708SEric Dumazet {
1333723b4610SEric Dumazet 	if (sk_hashed(sk)) {
1334645ca708SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1335512615b6SEric Dumazet 		struct udp_hslot *hslot, *hslot2;
1336512615b6SEric Dumazet 
1337512615b6SEric Dumazet 		hslot  = udp_hashslot(udptable, sock_net(sk),
1338d4cada4aSEric Dumazet 				      udp_sk(sk)->udp_port_hash);
1339512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1340645ca708SEric Dumazet 
1341c8db3fecSEric Dumazet 		spin_lock_bh(&hslot->lock);
134288ab1932SEric Dumazet 		if (sk_nulls_del_node_init_rcu(sk)) {
1343fdcc8aa9SEric Dumazet 			hslot->count--;
1344c720c7e8SEric Dumazet 			inet_sk(sk)->inet_num = 0;
1345645ca708SEric Dumazet 			sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1346512615b6SEric Dumazet 
1347512615b6SEric Dumazet 			spin_lock(&hslot2->lock);
1348512615b6SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1349512615b6SEric Dumazet 			hslot2->count--;
1350512615b6SEric Dumazet 			spin_unlock(&hslot2->lock);
1351645ca708SEric Dumazet 		}
1352c8db3fecSEric Dumazet 		spin_unlock_bh(&hslot->lock);
1353645ca708SEric Dumazet 	}
1354723b4610SEric Dumazet }
1355645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash);
1356645ca708SEric Dumazet 
1357719f8358SEric Dumazet /*
1358719f8358SEric Dumazet  * inet_rcv_saddr was changed, we must rehash secondary hash
1359719f8358SEric Dumazet  */
1360719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash)
1361719f8358SEric Dumazet {
1362719f8358SEric Dumazet 	if (sk_hashed(sk)) {
1363719f8358SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1364719f8358SEric Dumazet 		struct udp_hslot *hslot, *hslot2, *nhslot2;
1365719f8358SEric Dumazet 
1366719f8358SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1367719f8358SEric Dumazet 		nhslot2 = udp_hashslot2(udptable, newhash);
1368719f8358SEric Dumazet 		udp_sk(sk)->udp_portaddr_hash = newhash;
1369719f8358SEric Dumazet 		if (hslot2 != nhslot2) {
1370719f8358SEric Dumazet 			hslot = udp_hashslot(udptable, sock_net(sk),
1371719f8358SEric Dumazet 					     udp_sk(sk)->udp_port_hash);
1372719f8358SEric Dumazet 			/* we must lock primary chain too */
1373719f8358SEric Dumazet 			spin_lock_bh(&hslot->lock);
1374719f8358SEric Dumazet 
1375719f8358SEric Dumazet 			spin_lock(&hslot2->lock);
1376719f8358SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1377719f8358SEric Dumazet 			hslot2->count--;
1378719f8358SEric Dumazet 			spin_unlock(&hslot2->lock);
1379719f8358SEric Dumazet 
1380719f8358SEric Dumazet 			spin_lock(&nhslot2->lock);
1381719f8358SEric Dumazet 			hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
1382719f8358SEric Dumazet 						 &nhslot2->head);
1383719f8358SEric Dumazet 			nhslot2->count++;
1384719f8358SEric Dumazet 			spin_unlock(&nhslot2->lock);
1385719f8358SEric Dumazet 
1386719f8358SEric Dumazet 			spin_unlock_bh(&hslot->lock);
1387719f8358SEric Dumazet 		}
1388719f8358SEric Dumazet 	}
1389719f8358SEric Dumazet }
1390719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash);
1391719f8358SEric Dumazet 
1392719f8358SEric Dumazet static void udp_v4_rehash(struct sock *sk)
1393719f8358SEric Dumazet {
1394719f8358SEric Dumazet 	u16 new_hash = udp4_portaddr_hash(sock_net(sk),
1395719f8358SEric Dumazet 					  inet_sk(sk)->inet_rcv_saddr,
1396719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
1397719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
1398719f8358SEric Dumazet }
1399719f8358SEric Dumazet 
140093821778SHerbert Xu static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
140193821778SHerbert Xu {
1402fec5e652STom Herbert 	int rc;
140393821778SHerbert Xu 
1404fec5e652STom Herbert 	if (inet_sk(sk)->inet_daddr)
1405bdeab991STom Herbert 		sock_rps_save_rxhash(sk, skb);
1406fec5e652STom Herbert 
1407d826eb14SEric Dumazet 	rc = sock_queue_rcv_skb(sk, skb);
1408766e9037SEric Dumazet 	if (rc < 0) {
1409766e9037SEric Dumazet 		int is_udplite = IS_UDPLITE(sk);
1410766e9037SEric Dumazet 
141193821778SHerbert Xu 		/* Note that an ENOMEM error is charged twice */
1412766e9037SEric Dumazet 		if (rc == -ENOMEM)
141393821778SHerbert Xu 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
141493821778SHerbert Xu 					 is_udplite);
1415766e9037SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1416766e9037SEric Dumazet 		kfree_skb(skb);
1417296f7ea7SSatoru Moriya 		trace_udp_fail_queue_rcv_skb(rc, sk);
1418766e9037SEric Dumazet 		return -1;
141993821778SHerbert Xu 	}
142093821778SHerbert Xu 
142193821778SHerbert Xu 	return 0;
142293821778SHerbert Xu 
142393821778SHerbert Xu }
142493821778SHerbert Xu 
1425447167bfSEric Dumazet static struct static_key udp_encap_needed __read_mostly;
1426447167bfSEric Dumazet void udp_encap_enable(void)
1427447167bfSEric Dumazet {
1428447167bfSEric Dumazet 	if (!static_key_enabled(&udp_encap_needed))
1429447167bfSEric Dumazet 		static_key_slow_inc(&udp_encap_needed);
1430447167bfSEric Dumazet }
1431447167bfSEric Dumazet EXPORT_SYMBOL(udp_encap_enable);
1432447167bfSEric Dumazet 
1433db8dac20SDavid S. Miller /* returns:
1434db8dac20SDavid S. Miller  *  -1: error
1435db8dac20SDavid S. Miller  *   0: success
1436db8dac20SDavid S. Miller  *  >0: "udp encap" protocol resubmission
1437db8dac20SDavid S. Miller  *
1438db8dac20SDavid S. Miller  * Note that in the success and error cases, the skb is assumed to
1439db8dac20SDavid S. Miller  * have either been requeued or freed.
1440db8dac20SDavid S. Miller  */
1441db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1442db8dac20SDavid S. Miller {
1443db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1444db8dac20SDavid S. Miller 	int rc;
1445db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1446db8dac20SDavid S. Miller 
1447db8dac20SDavid S. Miller 	/*
1448db8dac20SDavid S. Miller 	 *	Charge it to the socket, dropping if the queue is full.
1449db8dac20SDavid S. Miller 	 */
1450db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1451db8dac20SDavid S. Miller 		goto drop;
1452db8dac20SDavid S. Miller 	nf_reset(skb);
1453db8dac20SDavid S. Miller 
1454447167bfSEric Dumazet 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
14550ad92ad0SEric Dumazet 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
14560ad92ad0SEric Dumazet 
1457db8dac20SDavid S. Miller 		/*
1458db8dac20SDavid S. Miller 		 * This is an encapsulation socket so pass the skb to
1459db8dac20SDavid S. Miller 		 * the socket's udp_encap_rcv() hook. Otherwise, just
1460db8dac20SDavid S. Miller 		 * fall through and pass this up the UDP socket.
1461db8dac20SDavid S. Miller 		 * up->encap_rcv() returns the following value:
1462db8dac20SDavid S. Miller 		 * =0 if skb was successfully passed to the encap
1463db8dac20SDavid S. Miller 		 *    handler or was discarded by it.
1464db8dac20SDavid S. Miller 		 * >0 if skb should be passed on to UDP.
1465db8dac20SDavid S. Miller 		 * <0 if skb should be resubmitted as proto -N
1466db8dac20SDavid S. Miller 		 */
1467db8dac20SDavid S. Miller 
1468db8dac20SDavid S. Miller 		/* if we're overly short, let UDP handle it */
14690ad92ad0SEric Dumazet 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
14700ad92ad0SEric Dumazet 		if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) {
1471db8dac20SDavid S. Miller 			int ret;
1472db8dac20SDavid S. Miller 
14730ad92ad0SEric Dumazet 			ret = encap_rcv(sk, skb);
1474db8dac20SDavid S. Miller 			if (ret <= 0) {
14750283328eSPavel Emelyanov 				UDP_INC_STATS_BH(sock_net(sk),
14760283328eSPavel Emelyanov 						 UDP_MIB_INDATAGRAMS,
1477db8dac20SDavid S. Miller 						 is_udplite);
1478db8dac20SDavid S. Miller 				return -ret;
1479db8dac20SDavid S. Miller 			}
1480db8dac20SDavid S. Miller 		}
1481db8dac20SDavid S. Miller 
1482db8dac20SDavid S. Miller 		/* FALLTHROUGH -- it's a UDP Packet */
1483db8dac20SDavid S. Miller 	}
1484db8dac20SDavid S. Miller 
1485db8dac20SDavid S. Miller 	/*
1486db8dac20SDavid S. Miller 	 * 	UDP-Lite specific tests, ignored on UDP sockets
1487db8dac20SDavid S. Miller 	 */
1488db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
1489db8dac20SDavid S. Miller 
1490db8dac20SDavid S. Miller 		/*
1491db8dac20SDavid S. Miller 		 * MIB statistics other than incrementing the error count are
1492db8dac20SDavid S. Miller 		 * disabled for the following two types of errors: these depend
1493db8dac20SDavid S. Miller 		 * on the application settings, not on the functioning of the
1494db8dac20SDavid S. Miller 		 * protocol stack as such.
1495db8dac20SDavid S. Miller 		 *
1496db8dac20SDavid S. Miller 		 * RFC 3828 here recommends (sec 3.3): "There should also be a
1497db8dac20SDavid S. Miller 		 * way ... to ... at least let the receiving application block
1498db8dac20SDavid S. Miller 		 * delivery of packets with coverage values less than a value
1499db8dac20SDavid S. Miller 		 * provided by the application."
1500db8dac20SDavid S. Miller 		 */
1501db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
1502afd46503SJoe Perches 			LIMIT_NETDEBUG(KERN_WARNING "UDPLite: partial coverage %d while full coverage %d requested\n",
1503db8dac20SDavid S. Miller 				       UDP_SKB_CB(skb)->cscov, skb->len);
1504db8dac20SDavid S. Miller 			goto drop;
1505db8dac20SDavid S. Miller 		}
1506db8dac20SDavid S. Miller 		/* The next case involves violating the min. coverage requested
1507db8dac20SDavid S. Miller 		 * by the receiver. This is subtle: if receiver wants x and x is
1508db8dac20SDavid S. Miller 		 * greater than the buffersize/MTU then receiver will complain
1509db8dac20SDavid S. Miller 		 * that it wants x while sender emits packets of smaller size y.
1510db8dac20SDavid S. Miller 		 * Therefore the above ...()->partial_cov statement is essential.
1511db8dac20SDavid S. Miller 		 */
1512db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
1513afd46503SJoe Perches 			LIMIT_NETDEBUG(KERN_WARNING "UDPLite: coverage %d too small, need min %d\n",
1514db8dac20SDavid S. Miller 				       UDP_SKB_CB(skb)->cscov, up->pcrlen);
1515db8dac20SDavid S. Miller 			goto drop;
1516db8dac20SDavid S. Miller 		}
1517db8dac20SDavid S. Miller 	}
1518db8dac20SDavid S. Miller 
151933d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
152033d480ceSEric Dumazet 	    udp_lib_checksum_complete(skb))
15216a5dc9e5SEric Dumazet 		goto csum_error;
1522db8dac20SDavid S. Miller 
1523c377411fSEric Dumazet 
1524f545a38fSEric Dumazet 	if (sk_rcvqueues_full(sk, skb, sk->sk_rcvbuf))
1525c377411fSEric Dumazet 		goto drop;
1526c377411fSEric Dumazet 
152793821778SHerbert Xu 	rc = 0;
1528db8dac20SDavid S. Miller 
1529d826eb14SEric Dumazet 	ipv4_pktinfo_prepare(skb);
153093821778SHerbert Xu 	bh_lock_sock(sk);
153193821778SHerbert Xu 	if (!sock_owned_by_user(sk))
153293821778SHerbert Xu 		rc = __udp_queue_rcv_skb(sk, skb);
1533f545a38fSEric Dumazet 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
153455349790SZhu Yi 		bh_unlock_sock(sk);
153555349790SZhu Yi 		goto drop;
153655349790SZhu Yi 	}
153793821778SHerbert Xu 	bh_unlock_sock(sk);
153893821778SHerbert Xu 
153993821778SHerbert Xu 	return rc;
1540db8dac20SDavid S. Miller 
15416a5dc9e5SEric Dumazet csum_error:
15426a5dc9e5SEric Dumazet 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1543db8dac20SDavid S. Miller drop:
15440283328eSPavel Emelyanov 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
15458edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
1546db8dac20SDavid S. Miller 	kfree_skb(skb);
1547db8dac20SDavid S. Miller 	return -1;
1548db8dac20SDavid S. Miller }
1549db8dac20SDavid S. Miller 
15501240d137SEric Dumazet 
15511240d137SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
15521240d137SEric Dumazet 			struct sk_buff *skb, unsigned int final)
15531240d137SEric Dumazet {
15541240d137SEric Dumazet 	unsigned int i;
15551240d137SEric Dumazet 	struct sk_buff *skb1 = NULL;
1556f6b8f32cSEric Dumazet 	struct sock *sk;
15571240d137SEric Dumazet 
15581240d137SEric Dumazet 	for (i = 0; i < count; i++) {
1559f6b8f32cSEric Dumazet 		sk = stack[i];
15601240d137SEric Dumazet 		if (likely(skb1 == NULL))
15611240d137SEric Dumazet 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
15621240d137SEric Dumazet 
1563f6b8f32cSEric Dumazet 		if (!skb1) {
1564f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
1565f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
1566f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1567f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
1568f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1569f6b8f32cSEric Dumazet 		}
1570f6b8f32cSEric Dumazet 
1571f6b8f32cSEric Dumazet 		if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0)
15721240d137SEric Dumazet 			skb1 = NULL;
15731240d137SEric Dumazet 	}
15741240d137SEric Dumazet 	if (unlikely(skb1))
15751240d137SEric Dumazet 		kfree_skb(skb1);
15761240d137SEric Dumazet }
15771240d137SEric Dumazet 
1578db8dac20SDavid S. Miller /*
1579db8dac20SDavid S. Miller  *	Multicasts and broadcasts go to each listener.
1580db8dac20SDavid S. Miller  *
15811240d137SEric Dumazet  *	Note: called only from the BH handler context.
1582db8dac20SDavid S. Miller  */
1583e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
1584db8dac20SDavid S. Miller 				    struct udphdr  *uh,
1585db8dac20SDavid S. Miller 				    __be32 saddr, __be32 daddr,
1586645ca708SEric Dumazet 				    struct udp_table *udptable)
1587db8dac20SDavid S. Miller {
15881240d137SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
1589f86dcc5aSEric Dumazet 	struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
1590db8dac20SDavid S. Miller 	int dif;
15911240d137SEric Dumazet 	unsigned int i, count = 0;
1592db8dac20SDavid S. Miller 
1593645ca708SEric Dumazet 	spin_lock(&hslot->lock);
159488ab1932SEric Dumazet 	sk = sk_nulls_head(&hslot->head);
1595db8dac20SDavid S. Miller 	dif = skb->dev->ifindex;
1596920a4611SEric Dumazet 	sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
15971240d137SEric Dumazet 	while (sk) {
15981240d137SEric Dumazet 		stack[count++] = sk;
15991240d137SEric Dumazet 		sk = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
16001240d137SEric Dumazet 				       daddr, uh->source, saddr, dif);
16011240d137SEric Dumazet 		if (unlikely(count == ARRAY_SIZE(stack))) {
16021240d137SEric Dumazet 			if (!sk)
16031240d137SEric Dumazet 				break;
16041240d137SEric Dumazet 			flush_stack(stack, count, skb, ~0);
16051240d137SEric Dumazet 			count = 0;
1606db8dac20SDavid S. Miller 		}
16071240d137SEric Dumazet 	}
16081240d137SEric Dumazet 	/*
16091240d137SEric Dumazet 	 * before releasing chain lock, we must take a reference on sockets
16101240d137SEric Dumazet 	 */
16111240d137SEric Dumazet 	for (i = 0; i < count; i++)
16121240d137SEric Dumazet 		sock_hold(stack[i]);
16131240d137SEric Dumazet 
1614645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
16151240d137SEric Dumazet 
16161240d137SEric Dumazet 	/*
16171240d137SEric Dumazet 	 * do the slow work with no lock held
16181240d137SEric Dumazet 	 */
16191240d137SEric Dumazet 	if (count) {
16201240d137SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
16211240d137SEric Dumazet 
16221240d137SEric Dumazet 		for (i = 0; i < count; i++)
16231240d137SEric Dumazet 			sock_put(stack[i]);
16241240d137SEric Dumazet 	} else {
16251240d137SEric Dumazet 		kfree_skb(skb);
16261240d137SEric Dumazet 	}
1627db8dac20SDavid S. Miller 	return 0;
1628db8dac20SDavid S. Miller }
1629db8dac20SDavid S. Miller 
1630db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success),
1631db8dac20SDavid S. Miller  * CHECKSUM_UNNECESSARY means, that no more checks are required.
1632db8dac20SDavid S. Miller  * Otherwise, csum completion requires chacksumming packet body,
1633db8dac20SDavid S. Miller  * including udp header and folding it to skb->csum.
1634db8dac20SDavid S. Miller  */
1635db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1636db8dac20SDavid S. Miller 				 int proto)
1637db8dac20SDavid S. Miller {
1638db8dac20SDavid S. Miller 	const struct iphdr *iph;
1639db8dac20SDavid S. Miller 	int err;
1640db8dac20SDavid S. Miller 
1641db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
1642db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
1643db8dac20SDavid S. Miller 
1644db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
1645db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
1646db8dac20SDavid S. Miller 		if (err)
1647db8dac20SDavid S. Miller 			return err;
1648db8dac20SDavid S. Miller 	}
1649db8dac20SDavid S. Miller 
1650db8dac20SDavid S. Miller 	iph = ip_hdr(skb);
1651db8dac20SDavid S. Miller 	if (uh->check == 0) {
1652db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1653db8dac20SDavid S. Miller 	} else if (skb->ip_summed == CHECKSUM_COMPLETE) {
1654db8dac20SDavid S. Miller 		if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
1655db8dac20SDavid S. Miller 				      proto, skb->csum))
1656db8dac20SDavid S. Miller 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1657db8dac20SDavid S. Miller 	}
1658db8dac20SDavid S. Miller 	if (!skb_csum_unnecessary(skb))
1659db8dac20SDavid S. Miller 		skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
1660db8dac20SDavid S. Miller 					       skb->len, proto, 0);
1661db8dac20SDavid S. Miller 	/* Probably, we should checksum udp header (it should be in cache
1662db8dac20SDavid S. Miller 	 * in any case) and data in tiny packets (< rx copybreak).
1663db8dac20SDavid S. Miller 	 */
1664db8dac20SDavid S. Miller 
1665db8dac20SDavid S. Miller 	return 0;
1666db8dac20SDavid S. Miller }
1667db8dac20SDavid S. Miller 
1668db8dac20SDavid S. Miller /*
1669db8dac20SDavid S. Miller  *	All we need to do is get the socket, and then do a checksum.
1670db8dac20SDavid S. Miller  */
1671db8dac20SDavid S. Miller 
1672645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1673db8dac20SDavid S. Miller 		   int proto)
1674db8dac20SDavid S. Miller {
1675db8dac20SDavid S. Miller 	struct sock *sk;
16767b5e56f9SJesper Dangaard Brouer 	struct udphdr *uh;
1677db8dac20SDavid S. Miller 	unsigned short ulen;
1678adf30907SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
16792783ef23SJesper Dangaard Brouer 	__be32 saddr, daddr;
16800283328eSPavel Emelyanov 	struct net *net = dev_net(skb->dev);
1681db8dac20SDavid S. Miller 
1682db8dac20SDavid S. Miller 	/*
1683db8dac20SDavid S. Miller 	 *  Validate the packet.
1684db8dac20SDavid S. Miller 	 */
1685db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1686db8dac20SDavid S. Miller 		goto drop;		/* No space for header. */
1687db8dac20SDavid S. Miller 
16887b5e56f9SJesper Dangaard Brouer 	uh   = udp_hdr(skb);
1689db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
1690ccc2d97cSBjørn Mork 	saddr = ip_hdr(skb)->saddr;
1691ccc2d97cSBjørn Mork 	daddr = ip_hdr(skb)->daddr;
1692ccc2d97cSBjørn Mork 
1693db8dac20SDavid S. Miller 	if (ulen > skb->len)
1694db8dac20SDavid S. Miller 		goto short_packet;
1695db8dac20SDavid S. Miller 
1696db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
1697db8dac20SDavid S. Miller 		/* UDP validates ulen. */
1698db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1699db8dac20SDavid S. Miller 			goto short_packet;
1700db8dac20SDavid S. Miller 		uh = udp_hdr(skb);
1701db8dac20SDavid S. Miller 	}
1702db8dac20SDavid S. Miller 
1703db8dac20SDavid S. Miller 	if (udp4_csum_init(skb, uh, proto))
1704db8dac20SDavid S. Miller 		goto csum_error;
1705db8dac20SDavid S. Miller 
1706db8dac20SDavid S. Miller 	if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1707e3163493SPavel Emelyanov 		return __udp4_lib_mcast_deliver(net, skb, uh,
1708e3163493SPavel Emelyanov 				saddr, daddr, udptable);
1709db8dac20SDavid S. Miller 
1710607c4aafSKOVACS Krisztian 	sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1711db8dac20SDavid S. Miller 
1712db8dac20SDavid S. Miller 	if (sk != NULL) {
1713*a5b50476SEliezer Tamir 		int ret;
1714*a5b50476SEliezer Tamir 
1715*a5b50476SEliezer Tamir 		sk_mark_ll(sk, skb);
1716*a5b50476SEliezer Tamir 		ret = udp_queue_rcv_skb(sk, skb);
1717db8dac20SDavid S. Miller 		sock_put(sk);
1718db8dac20SDavid S. Miller 
1719db8dac20SDavid S. Miller 		/* a return value > 0 means to resubmit the input, but
1720db8dac20SDavid S. Miller 		 * it wants the return to be -protocol, or 0
1721db8dac20SDavid S. Miller 		 */
1722db8dac20SDavid S. Miller 		if (ret > 0)
1723db8dac20SDavid S. Miller 			return -ret;
1724db8dac20SDavid S. Miller 		return 0;
1725db8dac20SDavid S. Miller 	}
1726db8dac20SDavid S. Miller 
1727db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1728db8dac20SDavid S. Miller 		goto drop;
1729db8dac20SDavid S. Miller 	nf_reset(skb);
1730db8dac20SDavid S. Miller 
1731db8dac20SDavid S. Miller 	/* No socket. Drop packet silently, if checksum is wrong */
1732db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
1733db8dac20SDavid S. Miller 		goto csum_error;
1734db8dac20SDavid S. Miller 
17350283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
1736db8dac20SDavid S. Miller 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1737db8dac20SDavid S. Miller 
1738db8dac20SDavid S. Miller 	/*
1739db8dac20SDavid S. Miller 	 * Hmm.  We got an UDP packet to a port to which we
1740db8dac20SDavid S. Miller 	 * don't wanna listen.  Ignore it.
1741db8dac20SDavid S. Miller 	 */
1742db8dac20SDavid S. Miller 	kfree_skb(skb);
1743db8dac20SDavid S. Miller 	return 0;
1744db8dac20SDavid S. Miller 
1745db8dac20SDavid S. Miller short_packet:
1746673d57e7SHarvey Harrison 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
1747afd46503SJoe Perches 		       proto == IPPROTO_UDPLITE ? "Lite" : "",
1748afd46503SJoe Perches 		       &saddr, ntohs(uh->source),
1749afd46503SJoe Perches 		       ulen, skb->len,
1750afd46503SJoe Perches 		       &daddr, ntohs(uh->dest));
1751db8dac20SDavid S. Miller 	goto drop;
1752db8dac20SDavid S. Miller 
1753db8dac20SDavid S. Miller csum_error:
1754db8dac20SDavid S. Miller 	/*
1755db8dac20SDavid S. Miller 	 * RFC1122: OK.  Discards the bad packet silently (as far as
1756db8dac20SDavid S. Miller 	 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1757db8dac20SDavid S. Miller 	 */
1758673d57e7SHarvey Harrison 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
1759afd46503SJoe Perches 		       proto == IPPROTO_UDPLITE ? "Lite" : "",
1760afd46503SJoe Perches 		       &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
1761db8dac20SDavid S. Miller 		       ulen);
17626a5dc9e5SEric Dumazet 	UDP_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1763db8dac20SDavid S. Miller drop:
17640283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1765db8dac20SDavid S. Miller 	kfree_skb(skb);
1766db8dac20SDavid S. Miller 	return 0;
1767db8dac20SDavid S. Miller }
1768db8dac20SDavid S. Miller 
1769db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb)
1770db8dac20SDavid S. Miller {
1771645ca708SEric Dumazet 	return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1772db8dac20SDavid S. Miller }
1773db8dac20SDavid S. Miller 
17747d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk)
1775db8dac20SDavid S. Miller {
177644046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
17778a74ad60SEric Dumazet 	bool slow = lock_sock_fast(sk);
1778db8dac20SDavid S. Miller 	udp_flush_pending_frames(sk);
17798a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
178044046a59STom Parkin 	if (static_key_false(&udp_encap_needed) && up->encap_type) {
178144046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
178244046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
178344046a59STom Parkin 		if (encap_destroy)
178444046a59STom Parkin 			encap_destroy(sk);
178544046a59STom Parkin 	}
1786db8dac20SDavid S. Miller }
1787db8dac20SDavid S. Miller 
17881da177e4SLinus Torvalds /*
17891da177e4SLinus Torvalds  *	Socket option code for UDP
17901da177e4SLinus Torvalds  */
17914c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname,
1792b7058842SDavid S. Miller 		       char __user *optval, unsigned int optlen,
17934c0a6cb0SGerrit Renker 		       int (*push_pending_frames)(struct sock *))
17941da177e4SLinus Torvalds {
17951da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
17961da177e4SLinus Torvalds 	int val;
17971da177e4SLinus Torvalds 	int err = 0;
1798b2bf1e26SWang Chen 	int is_udplite = IS_UDPLITE(sk);
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds 	if (optlen < sizeof(int))
18011da177e4SLinus Torvalds 		return -EINVAL;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
18041da177e4SLinus Torvalds 		return -EFAULT;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 	switch (optname) {
18071da177e4SLinus Torvalds 	case UDP_CORK:
18081da177e4SLinus Torvalds 		if (val != 0) {
18091da177e4SLinus Torvalds 			up->corkflag = 1;
18101da177e4SLinus Torvalds 		} else {
18111da177e4SLinus Torvalds 			up->corkflag = 0;
18121da177e4SLinus Torvalds 			lock_sock(sk);
18134c0a6cb0SGerrit Renker 			(*push_pending_frames)(sk);
18141da177e4SLinus Torvalds 			release_sock(sk);
18151da177e4SLinus Torvalds 		}
18161da177e4SLinus Torvalds 		break;
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds 	case UDP_ENCAP:
18191da177e4SLinus Torvalds 		switch (val) {
18201da177e4SLinus Torvalds 		case 0:
18211da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP:
18221da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP_NON_IKE:
1823067b207bSJames Chapman 			up->encap_rcv = xfrm4_udp_encap_rcv;
1824067b207bSJames Chapman 			/* FALLTHROUGH */
1825342f0234SJames Chapman 		case UDP_ENCAP_L2TPINUDP:
18261da177e4SLinus Torvalds 			up->encap_type = val;
1827447167bfSEric Dumazet 			udp_encap_enable();
18281da177e4SLinus Torvalds 			break;
18291da177e4SLinus Torvalds 		default:
18301da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
18311da177e4SLinus Torvalds 			break;
18321da177e4SLinus Torvalds 		}
18331da177e4SLinus Torvalds 		break;
18341da177e4SLinus Torvalds 
1835ba4e58ecSGerrit Renker 	/*
1836ba4e58ecSGerrit Renker 	 * 	UDP-Lite's partial checksum coverage (RFC 3828).
1837ba4e58ecSGerrit Renker 	 */
1838ba4e58ecSGerrit Renker 	/* The sender sets actual checksum coverage length via this option.
1839ba4e58ecSGerrit Renker 	 * The case coverage > packet length is handled by send module. */
1840ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
1841b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
1842ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
1843ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
1844ba4e58ecSGerrit Renker 			val = 8;
18454be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
18464be929beSAlexey Dobriyan 			val = USHRT_MAX;
1847ba4e58ecSGerrit Renker 		up->pcslen = val;
1848ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_SEND_CC;
1849ba4e58ecSGerrit Renker 		break;
1850ba4e58ecSGerrit Renker 
1851ba4e58ecSGerrit Renker 	/* The receiver specifies a minimum checksum coverage value. To make
1852ba4e58ecSGerrit Renker 	 * sense, this should be set to at least 8 (as done below). If zero is
1853ba4e58ecSGerrit Renker 	 * used, this again means full checksum coverage.                     */
1854ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
1855b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
1856ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
1857ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Avoid silly minimal values.       */
1858ba4e58ecSGerrit Renker 			val = 8;
18594be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
18604be929beSAlexey Dobriyan 			val = USHRT_MAX;
1861ba4e58ecSGerrit Renker 		up->pcrlen = val;
1862ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_RECV_CC;
1863ba4e58ecSGerrit Renker 		break;
1864ba4e58ecSGerrit Renker 
18651da177e4SLinus Torvalds 	default:
18661da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
18671da177e4SLinus Torvalds 		break;
18686516c655SStephen Hemminger 	}
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	return err;
18711da177e4SLinus Torvalds }
1872c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt);
18731da177e4SLinus Torvalds 
1874db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname,
1875b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
1876db8dac20SDavid S. Miller {
1877db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1878db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1879db8dac20SDavid S. Miller 					  udp_push_pending_frames);
1880db8dac20SDavid S. Miller 	return ip_setsockopt(sk, level, optname, optval, optlen);
1881db8dac20SDavid S. Miller }
1882db8dac20SDavid S. Miller 
1883db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1884db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1885b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
1886db8dac20SDavid S. Miller {
1887db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1888db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1889db8dac20SDavid S. Miller 					  udp_push_pending_frames);
1890db8dac20SDavid S. Miller 	return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1891db8dac20SDavid S. Miller }
1892db8dac20SDavid S. Miller #endif
1893db8dac20SDavid S. Miller 
18944c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname,
18951da177e4SLinus Torvalds 		       char __user *optval, int __user *optlen)
18961da177e4SLinus Torvalds {
18971da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
18981da177e4SLinus Torvalds 	int val, len;
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 	if (get_user(len, optlen))
19011da177e4SLinus Torvalds 		return -EFAULT;
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 	if (len < 0)
19061da177e4SLinus Torvalds 		return -EINVAL;
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 	switch (optname) {
19091da177e4SLinus Torvalds 	case UDP_CORK:
19101da177e4SLinus Torvalds 		val = up->corkflag;
19111da177e4SLinus Torvalds 		break;
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 	case UDP_ENCAP:
19141da177e4SLinus Torvalds 		val = up->encap_type;
19151da177e4SLinus Torvalds 		break;
19161da177e4SLinus Torvalds 
1917ba4e58ecSGerrit Renker 	/* The following two cannot be changed on UDP sockets, the return is
1918ba4e58ecSGerrit Renker 	 * always 0 (which corresponds to the full checksum coverage of UDP). */
1919ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
1920ba4e58ecSGerrit Renker 		val = up->pcslen;
1921ba4e58ecSGerrit Renker 		break;
1922ba4e58ecSGerrit Renker 
1923ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
1924ba4e58ecSGerrit Renker 		val = up->pcrlen;
1925ba4e58ecSGerrit Renker 		break;
1926ba4e58ecSGerrit Renker 
19271da177e4SLinus Torvalds 	default:
19281da177e4SLinus Torvalds 		return -ENOPROTOOPT;
19296516c655SStephen Hemminger 	}
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	if (put_user(len, optlen))
19321da177e4SLinus Torvalds 		return -EFAULT;
19331da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
19341da177e4SLinus Torvalds 		return -EFAULT;
19351da177e4SLinus Torvalds 	return 0;
19361da177e4SLinus Torvalds }
1937c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt);
19381da177e4SLinus Torvalds 
1939db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname,
1940db8dac20SDavid S. Miller 		   char __user *optval, int __user *optlen)
1941db8dac20SDavid S. Miller {
1942db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1943db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1944db8dac20SDavid S. Miller 	return ip_getsockopt(sk, level, optname, optval, optlen);
1945db8dac20SDavid S. Miller }
1946db8dac20SDavid S. Miller 
1947db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1948db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1949db8dac20SDavid S. Miller 				 char __user *optval, int __user *optlen)
1950db8dac20SDavid S. Miller {
1951db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1952db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1953db8dac20SDavid S. Miller 	return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1954db8dac20SDavid S. Miller }
1955db8dac20SDavid S. Miller #endif
19561da177e4SLinus Torvalds /**
19571da177e4SLinus Torvalds  * 	udp_poll - wait for a UDP event.
19581da177e4SLinus Torvalds  *	@file - file struct
19591da177e4SLinus Torvalds  *	@sock - socket
19601da177e4SLinus Torvalds  *	@wait - poll table
19611da177e4SLinus Torvalds  *
19621da177e4SLinus Torvalds  *	This is same as datagram poll, except for the special case of
19631da177e4SLinus Torvalds  *	blocking sockets. If application is using a blocking fd
19641da177e4SLinus Torvalds  *	and a packet with checksum error is in the queue;
19651da177e4SLinus Torvalds  *	then it could get return from select indicating data available
19661da177e4SLinus Torvalds  *	but then block when reading it. Add special case code
19671da177e4SLinus Torvalds  *	to work around these arguably broken applications.
19681da177e4SLinus Torvalds  */
19691da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	unsigned int mask = datagram_poll(file, sock, wait);
19721da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
19731da177e4SLinus Torvalds 
1974c3f1dbafSDavid Majnemer 	sock_rps_record_flow(sk);
1975c3f1dbafSDavid Majnemer 
19761da177e4SLinus Torvalds 	/* Check for false positives due to checksum errors */
197785584672SEric Dumazet 	if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
197885584672SEric Dumazet 	    !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
19791da177e4SLinus Torvalds 		mask &= ~(POLLIN | POLLRDNORM);
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	return mask;
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds }
1984c482c568SEric Dumazet EXPORT_SYMBOL(udp_poll);
19851da177e4SLinus Torvalds 
1986db8dac20SDavid S. Miller struct proto udp_prot = {
1987db8dac20SDavid S. Miller 	.name		   = "UDP",
1988db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1989db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1990db8dac20SDavid S. Miller 	.connect	   = ip4_datagram_connect,
1991db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1992db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1993db8dac20SDavid S. Miller 	.destroy	   = udp_destroy_sock,
1994db8dac20SDavid S. Miller 	.setsockopt	   = udp_setsockopt,
1995db8dac20SDavid S. Miller 	.getsockopt	   = udp_getsockopt,
1996db8dac20SDavid S. Miller 	.sendmsg	   = udp_sendmsg,
1997db8dac20SDavid S. Miller 	.recvmsg	   = udp_recvmsg,
1998db8dac20SDavid S. Miller 	.sendpage	   = udp_sendpage,
199993821778SHerbert Xu 	.backlog_rcv	   = __udp_queue_rcv_skb,
20008141ed9fSSteffen Klassert 	.release_cb	   = ip4_datagram_release_cb,
2001db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
2002db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
2003719f8358SEric Dumazet 	.rehash		   = udp_v4_rehash,
2004db8dac20SDavid S. Miller 	.get_port	   = udp_v4_get_port,
2005db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
2006db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
2007db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
2008db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
2009db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp_sock),
2010271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
2011645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
2012db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
2013db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udp_setsockopt,
2014db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udp_getsockopt,
2015db8dac20SDavid S. Miller #endif
2016fcbdf09dSOctavian Purdila 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
2017db8dac20SDavid S. Miller };
2018c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot);
20191da177e4SLinus Torvalds 
20201da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
20211da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
20221da177e4SLinus Torvalds 
2023645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start)
20241da177e4SLinus Torvalds {
20251da177e4SLinus Torvalds 	struct sock *sk;
20261da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
20276f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
20281da177e4SLinus Torvalds 
2029f86dcc5aSEric Dumazet 	for (state->bucket = start; state->bucket <= state->udp_table->mask;
2030f86dcc5aSEric Dumazet 	     ++state->bucket) {
203188ab1932SEric Dumazet 		struct hlist_nulls_node *node;
2032645ca708SEric Dumazet 		struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
2033f86dcc5aSEric Dumazet 
2034f86dcc5aSEric Dumazet 		if (hlist_nulls_empty(&hslot->head))
2035f86dcc5aSEric Dumazet 			continue;
2036f86dcc5aSEric Dumazet 
2037645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
203888ab1932SEric Dumazet 		sk_nulls_for_each(sk, node, &hslot->head) {
2039878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
2040a91275efSDaniel Lezcano 				continue;
20411da177e4SLinus Torvalds 			if (sk->sk_family == state->family)
20421da177e4SLinus Torvalds 				goto found;
20431da177e4SLinus Torvalds 		}
2044645ca708SEric Dumazet 		spin_unlock_bh(&hslot->lock);
20451da177e4SLinus Torvalds 	}
20461da177e4SLinus Torvalds 	sk = NULL;
20471da177e4SLinus Torvalds found:
20481da177e4SLinus Torvalds 	return sk;
20491da177e4SLinus Torvalds }
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
20521da177e4SLinus Torvalds {
20531da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
20546f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	do {
205788ab1932SEric Dumazet 		sk = sk_nulls_next(sk);
2058878628fbSYOSHIFUJI Hideaki 	} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
20591da177e4SLinus Torvalds 
2060645ca708SEric Dumazet 	if (!sk) {
2061f86dcc5aSEric Dumazet 		if (state->bucket <= state->udp_table->mask)
2062f52b5054SEric Dumazet 			spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
2063645ca708SEric Dumazet 		return udp_get_first(seq, state->bucket + 1);
20641da177e4SLinus Torvalds 	}
20651da177e4SLinus Torvalds 	return sk;
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
20691da177e4SLinus Torvalds {
2070645ca708SEric Dumazet 	struct sock *sk = udp_get_first(seq, 0);
20711da177e4SLinus Torvalds 
20721da177e4SLinus Torvalds 	if (sk)
20731da177e4SLinus Torvalds 		while (pos && (sk = udp_get_next(seq, sk)) != NULL)
20741da177e4SLinus Torvalds 			--pos;
20751da177e4SLinus Torvalds 	return pos ? NULL : sk;
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
20791da177e4SLinus Torvalds {
208030842f29SVitaly Mayatskikh 	struct udp_iter_state *state = seq->private;
2081f86dcc5aSEric Dumazet 	state->bucket = MAX_UDP_PORTS;
208230842f29SVitaly Mayatskikh 
2083b50660f1SYOSHIFUJI Hideaki 	return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
20871da177e4SLinus Torvalds {
20881da177e4SLinus Torvalds 	struct sock *sk;
20891da177e4SLinus Torvalds 
2090b50660f1SYOSHIFUJI Hideaki 	if (v == SEQ_START_TOKEN)
20911da177e4SLinus Torvalds 		sk = udp_get_idx(seq, 0);
20921da177e4SLinus Torvalds 	else
20931da177e4SLinus Torvalds 		sk = udp_get_next(seq, v);
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	++*pos;
20961da177e4SLinus Torvalds 	return sk;
20971da177e4SLinus Torvalds }
20981da177e4SLinus Torvalds 
20991da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v)
21001da177e4SLinus Torvalds {
2101645ca708SEric Dumazet 	struct udp_iter_state *state = seq->private;
2102645ca708SEric Dumazet 
2103f86dcc5aSEric Dumazet 	if (state->bucket <= state->udp_table->mask)
2104645ca708SEric Dumazet 		spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
21051da177e4SLinus Torvalds }
21061da177e4SLinus Torvalds 
210773cb88ecSArjan van de Ven int udp_seq_open(struct inode *inode, struct file *file)
21081da177e4SLinus Torvalds {
2109d9dda78bSAl Viro 	struct udp_seq_afinfo *afinfo = PDE_DATA(inode);
2110a2be75c1SDenis V. Lunev 	struct udp_iter_state *s;
2111a2be75c1SDenis V. Lunev 	int err;
21121da177e4SLinus Torvalds 
2113a2be75c1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
2114a2be75c1SDenis V. Lunev 			   sizeof(struct udp_iter_state));
2115a2be75c1SDenis V. Lunev 	if (err < 0)
2116a2be75c1SDenis V. Lunev 		return err;
2117a91275efSDaniel Lezcano 
2118a2be75c1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
21191da177e4SLinus Torvalds 	s->family		= afinfo->family;
2120645ca708SEric Dumazet 	s->udp_table		= afinfo->udp_table;
2121a2be75c1SDenis V. Lunev 	return err;
2122a91275efSDaniel Lezcano }
212373cb88ecSArjan van de Ven EXPORT_SYMBOL(udp_seq_open);
2124a91275efSDaniel Lezcano 
21251da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
21260c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
21271da177e4SLinus Torvalds {
21281da177e4SLinus Torvalds 	struct proc_dir_entry *p;
21291da177e4SLinus Torvalds 	int rc = 0;
21301da177e4SLinus Torvalds 
2131dda61925SDenis V. Lunev 	afinfo->seq_ops.start		= udp_seq_start;
2132dda61925SDenis V. Lunev 	afinfo->seq_ops.next		= udp_seq_next;
2133dda61925SDenis V. Lunev 	afinfo->seq_ops.stop		= udp_seq_stop;
2134dda61925SDenis V. Lunev 
213584841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
213673cb88ecSArjan van de Ven 			     afinfo->seq_fops, afinfo);
213784841c3cSDenis V. Lunev 	if (!p)
21381da177e4SLinus Torvalds 		rc = -ENOMEM;
21391da177e4SLinus Torvalds 	return rc;
21401da177e4SLinus Torvalds }
2141c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_register);
21421da177e4SLinus Torvalds 
21430c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
21441da177e4SLinus Torvalds {
2145ece31ffdSGao feng 	remove_proc_entry(afinfo->name, net->proc_net);
21461da177e4SLinus Torvalds }
2147c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_unregister);
2148db8dac20SDavid S. Miller 
2149db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
21505e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f,
21515e659e4cSPavel Emelyanov 		int bucket, int *len)
2152db8dac20SDavid S. Miller {
2153db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
2154c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2155c720c7e8SEric Dumazet 	__be32 src  = inet->inet_rcv_saddr;
2156c720c7e8SEric Dumazet 	__u16 destp	  = ntohs(inet->inet_dport);
2157c720c7e8SEric Dumazet 	__u16 srcp	  = ntohs(inet->inet_sport);
2158db8dac20SDavid S. Miller 
2159f86dcc5aSEric Dumazet 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
216071338aa7SDan Rosenberg 		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d%n",
2161db8dac20SDavid S. Miller 		bucket, src, srcp, dest, destp, sp->sk_state,
216231e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
216331e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
2164a7cb5a49SEric W. Biederman 		0, 0L, 0,
2165a7cb5a49SEric W. Biederman 		from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
2166a7cb5a49SEric W. Biederman 		0, sock_i_ino(sp),
2167cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_refcnt), sp,
2168cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_drops), len);
2169db8dac20SDavid S. Miller }
2170db8dac20SDavid S. Miller 
2171db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v)
2172db8dac20SDavid S. Miller {
2173db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
2174db8dac20SDavid S. Miller 		seq_printf(seq, "%-127s\n",
2175db8dac20SDavid S. Miller 			   "  sl  local_address rem_address   st tx_queue "
2176db8dac20SDavid S. Miller 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
2177cb61cb9bSEric Dumazet 			   "inode ref pointer drops");
2178db8dac20SDavid S. Miller 	else {
2179db8dac20SDavid S. Miller 		struct udp_iter_state *state = seq->private;
21805e659e4cSPavel Emelyanov 		int len;
2181db8dac20SDavid S. Miller 
21825e659e4cSPavel Emelyanov 		udp4_format_sock(v, seq, state->bucket, &len);
21835e659e4cSPavel Emelyanov 		seq_printf(seq, "%*s\n", 127 - len, "");
2184db8dac20SDavid S. Miller 	}
2185db8dac20SDavid S. Miller 	return 0;
2186db8dac20SDavid S. Miller }
2187db8dac20SDavid S. Miller 
218873cb88ecSArjan van de Ven static const struct file_operations udp_afinfo_seq_fops = {
218973cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
219073cb88ecSArjan van de Ven 	.open     = udp_seq_open,
219173cb88ecSArjan van de Ven 	.read     = seq_read,
219273cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
219373cb88ecSArjan van de Ven 	.release  = seq_release_net
219473cb88ecSArjan van de Ven };
219573cb88ecSArjan van de Ven 
2196db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
2197db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = {
2198db8dac20SDavid S. Miller 	.name		= "udp",
2199db8dac20SDavid S. Miller 	.family		= AF_INET,
2200645ca708SEric Dumazet 	.udp_table	= &udp_table,
220173cb88ecSArjan van de Ven 	.seq_fops	= &udp_afinfo_seq_fops,
2202dda61925SDenis V. Lunev 	.seq_ops	= {
2203dda61925SDenis V. Lunev 		.show		= udp4_seq_show,
2204dda61925SDenis V. Lunev 	},
2205db8dac20SDavid S. Miller };
2206db8dac20SDavid S. Miller 
22072c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net)
220815439febSPavel Emelyanov {
220915439febSPavel Emelyanov 	return udp_proc_register(net, &udp4_seq_afinfo);
221015439febSPavel Emelyanov }
221115439febSPavel Emelyanov 
22122c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net)
221315439febSPavel Emelyanov {
221415439febSPavel Emelyanov 	udp_proc_unregister(net, &udp4_seq_afinfo);
221515439febSPavel Emelyanov }
221615439febSPavel Emelyanov 
221715439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = {
221815439febSPavel Emelyanov 	.init = udp4_proc_init_net,
221915439febSPavel Emelyanov 	.exit = udp4_proc_exit_net,
222015439febSPavel Emelyanov };
222115439febSPavel Emelyanov 
2222db8dac20SDavid S. Miller int __init udp4_proc_init(void)
2223db8dac20SDavid S. Miller {
222415439febSPavel Emelyanov 	return register_pernet_subsys(&udp4_net_ops);
2225db8dac20SDavid S. Miller }
2226db8dac20SDavid S. Miller 
2227db8dac20SDavid S. Miller void udp4_proc_exit(void)
2228db8dac20SDavid S. Miller {
222915439febSPavel Emelyanov 	unregister_pernet_subsys(&udp4_net_ops);
2230db8dac20SDavid S. Miller }
22311da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
22321da177e4SLinus Torvalds 
2233f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries;
2234f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str)
2235645ca708SEric Dumazet {
2236413c27d8SEldad Zack 	ssize_t ret;
2237413c27d8SEldad Zack 
2238f86dcc5aSEric Dumazet 	if (!str)
2239f86dcc5aSEric Dumazet 		return 0;
2240413c27d8SEldad Zack 
2241413c27d8SEldad Zack 	ret = kstrtoul(str, 0, &uhash_entries);
2242413c27d8SEldad Zack 	if (ret)
2243413c27d8SEldad Zack 		return 0;
2244413c27d8SEldad Zack 
2245f86dcc5aSEric Dumazet 	if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
2246f86dcc5aSEric Dumazet 		uhash_entries = UDP_HTABLE_SIZE_MIN;
2247f86dcc5aSEric Dumazet 	return 1;
2248f86dcc5aSEric Dumazet }
2249f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries);
2250645ca708SEric Dumazet 
2251f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name)
2252f86dcc5aSEric Dumazet {
2253f86dcc5aSEric Dumazet 	unsigned int i;
2254f86dcc5aSEric Dumazet 
2255f86dcc5aSEric Dumazet 	table->hash = alloc_large_system_hash(name,
2256512615b6SEric Dumazet 					      2 * sizeof(struct udp_hslot),
2257f86dcc5aSEric Dumazet 					      uhash_entries,
2258f86dcc5aSEric Dumazet 					      21, /* one slot per 2 MB */
2259f86dcc5aSEric Dumazet 					      0,
2260f86dcc5aSEric Dumazet 					      &table->log,
2261f86dcc5aSEric Dumazet 					      &table->mask,
226231fe62b9STim Bird 					      UDP_HTABLE_SIZE_MIN,
2263f86dcc5aSEric Dumazet 					      64 * 1024);
226431fe62b9STim Bird 
2265512615b6SEric Dumazet 	table->hash2 = table->hash + (table->mask + 1);
2266f86dcc5aSEric Dumazet 	for (i = 0; i <= table->mask; i++) {
226788ab1932SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
2268fdcc8aa9SEric Dumazet 		table->hash[i].count = 0;
2269645ca708SEric Dumazet 		spin_lock_init(&table->hash[i].lock);
2270645ca708SEric Dumazet 	}
2271512615b6SEric Dumazet 	for (i = 0; i <= table->mask; i++) {
2272512615b6SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
2273512615b6SEric Dumazet 		table->hash2[i].count = 0;
2274512615b6SEric Dumazet 		spin_lock_init(&table->hash2[i].lock);
2275512615b6SEric Dumazet 	}
2276645ca708SEric Dumazet }
2277645ca708SEric Dumazet 
227895766fffSHideo Aoki void __init udp_init(void)
227995766fffSHideo Aoki {
2280f03d78dbSEric Dumazet 	unsigned long limit;
228195766fffSHideo Aoki 
2282f86dcc5aSEric Dumazet 	udp_table_init(&udp_table, "UDP");
2283f03d78dbSEric Dumazet 	limit = nr_free_buffer_pages() / 8;
228495766fffSHideo Aoki 	limit = max(limit, 128UL);
228595766fffSHideo Aoki 	sysctl_udp_mem[0] = limit / 4 * 3;
228695766fffSHideo Aoki 	sysctl_udp_mem[1] = limit;
228795766fffSHideo Aoki 	sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
228895766fffSHideo Aoki 
228995766fffSHideo Aoki 	sysctl_udp_rmem_min = SK_MEM_QUANTUM;
229095766fffSHideo Aoki 	sysctl_udp_wmem_min = SK_MEM_QUANTUM;
229195766fffSHideo Aoki }
229295766fffSHideo Aoki 
2293d7ca4cc0SSridhar Samudrala int udp4_ufo_send_check(struct sk_buff *skb)
2294d7ca4cc0SSridhar Samudrala {
229573136267SPravin B Shelar 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
229673136267SPravin B Shelar 		return -EINVAL;
229773136267SPravin B Shelar 
229873136267SPravin B Shelar 	if (likely(!skb->encapsulation)) {
2299d7ca4cc0SSridhar Samudrala 		const struct iphdr *iph;
2300d7ca4cc0SSridhar Samudrala 		struct udphdr *uh;
2301d7ca4cc0SSridhar Samudrala 
2302d7ca4cc0SSridhar Samudrala 		iph = ip_hdr(skb);
2303d7ca4cc0SSridhar Samudrala 		uh = udp_hdr(skb);
2304d7ca4cc0SSridhar Samudrala 
2305d7ca4cc0SSridhar Samudrala 		uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
2306d7ca4cc0SSridhar Samudrala 				IPPROTO_UDP, 0);
2307d7ca4cc0SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
2308d7ca4cc0SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
2309d7ca4cc0SSridhar Samudrala 		skb->ip_summed = CHECKSUM_PARTIAL;
231073136267SPravin B Shelar 	}
2311d7ca4cc0SSridhar Samudrala 	return 0;
2312d7ca4cc0SSridhar Samudrala }
2313d7ca4cc0SSridhar Samudrala 
231473136267SPravin B Shelar static struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
231573136267SPravin B Shelar 		netdev_features_t features)
231673136267SPravin B Shelar {
231773136267SPravin B Shelar 	struct sk_buff *segs = ERR_PTR(-EINVAL);
231873136267SPravin B Shelar 	int mac_len = skb->mac_len;
231973136267SPravin B Shelar 	int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
23200d05535dSPravin B Shelar 	__be16 protocol = skb->protocol;
232173136267SPravin B Shelar 	netdev_features_t enc_features;
23220d05535dSPravin B Shelar 	int outer_hlen;
232373136267SPravin B Shelar 
232473136267SPravin B Shelar 	if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
232573136267SPravin B Shelar 		goto out;
232673136267SPravin B Shelar 
232773136267SPravin B Shelar 	skb->encapsulation = 0;
232873136267SPravin B Shelar 	__skb_pull(skb, tnl_hlen);
232973136267SPravin B Shelar 	skb_reset_mac_header(skb);
233073136267SPravin B Shelar 	skb_set_network_header(skb, skb_inner_network_offset(skb));
233173136267SPravin B Shelar 	skb->mac_len = skb_inner_network_offset(skb);
233219acc327SPravin B Shelar 	skb->protocol = htons(ETH_P_TEB);
233373136267SPravin B Shelar 
233473136267SPravin B Shelar 	/* segment inner packet. */
233573136267SPravin B Shelar 	enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
233673136267SPravin B Shelar 	segs = skb_mac_gso_segment(skb, enc_features);
233773136267SPravin B Shelar 	if (!segs || IS_ERR(segs))
233873136267SPravin B Shelar 		goto out;
233973136267SPravin B Shelar 
234073136267SPravin B Shelar 	outer_hlen = skb_tnl_header_len(skb);
234173136267SPravin B Shelar 	skb = segs;
234273136267SPravin B Shelar 	do {
234373136267SPravin B Shelar 		struct udphdr *uh;
234473136267SPravin B Shelar 		int udp_offset = outer_hlen - tnl_hlen;
234573136267SPravin B Shelar 
234673136267SPravin B Shelar 		skb->mac_len = mac_len;
234773136267SPravin B Shelar 
234873136267SPravin B Shelar 		skb_push(skb, outer_hlen);
234973136267SPravin B Shelar 		skb_reset_mac_header(skb);
235073136267SPravin B Shelar 		skb_set_network_header(skb, mac_len);
235173136267SPravin B Shelar 		skb_set_transport_header(skb, udp_offset);
235273136267SPravin B Shelar 		uh = udp_hdr(skb);
235373136267SPravin B Shelar 		uh->len = htons(skb->len - udp_offset);
235473136267SPravin B Shelar 
235573136267SPravin B Shelar 		/* csum segment if tunnel sets skb with csum. */
235673136267SPravin B Shelar 		if (unlikely(uh->check)) {
235773136267SPravin B Shelar 			struct iphdr *iph = ip_hdr(skb);
235873136267SPravin B Shelar 
235973136267SPravin B Shelar 			uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
236073136267SPravin B Shelar 						       skb->len - udp_offset,
236173136267SPravin B Shelar 						       IPPROTO_UDP, 0);
236273136267SPravin B Shelar 			uh->check = csum_fold(skb_checksum(skb, udp_offset,
236373136267SPravin B Shelar 							   skb->len - udp_offset, 0));
236473136267SPravin B Shelar 			if (uh->check == 0)
236573136267SPravin B Shelar 				uh->check = CSUM_MANGLED_0;
236673136267SPravin B Shelar 
236773136267SPravin B Shelar 		}
236873136267SPravin B Shelar 		skb->ip_summed = CHECKSUM_NONE;
23690d05535dSPravin B Shelar 		skb->protocol = protocol;
237073136267SPravin B Shelar 	} while ((skb = skb->next));
237173136267SPravin B Shelar out:
237273136267SPravin B Shelar 	return segs;
237373136267SPravin B Shelar }
237473136267SPravin B Shelar 
2375c8f44affSMichał Mirosław struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
2376c8f44affSMichał Mirosław 	netdev_features_t features)
2377d7ca4cc0SSridhar Samudrala {
2378d7ca4cc0SSridhar Samudrala 	struct sk_buff *segs = ERR_PTR(-EINVAL);
2379d7ca4cc0SSridhar Samudrala 	unsigned int mss;
2380d7ca4cc0SSridhar Samudrala 	mss = skb_shinfo(skb)->gso_size;
2381d7ca4cc0SSridhar Samudrala 	if (unlikely(skb->len <= mss))
2382d7ca4cc0SSridhar Samudrala 		goto out;
2383d7ca4cc0SSridhar Samudrala 
2384d7ca4cc0SSridhar Samudrala 	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
2385d7ca4cc0SSridhar Samudrala 		/* Packet is from an untrusted source, reset gso_segs. */
2386d7ca4cc0SSridhar Samudrala 		int type = skb_shinfo(skb)->gso_type;
2387d7ca4cc0SSridhar Samudrala 
238868c33163SPravin B Shelar 		if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
238973136267SPravin B Shelar 				      SKB_GSO_UDP_TUNNEL |
23900d89d203SSimon Horman 				      SKB_GSO_GRE | SKB_GSO_MPLS) ||
2391d7ca4cc0SSridhar Samudrala 			     !(type & (SKB_GSO_UDP))))
2392d7ca4cc0SSridhar Samudrala 			goto out;
2393d7ca4cc0SSridhar Samudrala 
2394d7ca4cc0SSridhar Samudrala 		skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
2395d7ca4cc0SSridhar Samudrala 
2396d7ca4cc0SSridhar Samudrala 		segs = NULL;
2397d7ca4cc0SSridhar Samudrala 		goto out;
2398d7ca4cc0SSridhar Samudrala 	}
2399d7ca4cc0SSridhar Samudrala 
240073136267SPravin B Shelar 	/* Fragment the skb. IP headers of the fragments are updated in
240173136267SPravin B Shelar 	 * inet_gso_segment()
240273136267SPravin B Shelar 	 */
240373136267SPravin B Shelar 	if (skb->encapsulation && skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL)
240473136267SPravin B Shelar 		segs = skb_udp_tunnel_segment(skb, features);
240573136267SPravin B Shelar 	else {
240673136267SPravin B Shelar 		int offset;
240773136267SPravin B Shelar 		__wsum csum;
240873136267SPravin B Shelar 
240973136267SPravin B Shelar 		/* Do software UFO. Complete and fill in the UDP checksum as
241073136267SPravin B Shelar 		 * HW cannot do checksum of UDP packets sent as multiple
241173136267SPravin B Shelar 		 * IP fragments.
2412d7ca4cc0SSridhar Samudrala 		 */
241355508d60SMichał Mirosław 		offset = skb_checksum_start_offset(skb);
2414d7ca4cc0SSridhar Samudrala 		csum = skb_checksum(skb, offset, skb->len - offset, 0);
2415d7ca4cc0SSridhar Samudrala 		offset += skb->csum_offset;
2416d7ca4cc0SSridhar Samudrala 		*(__sum16 *)(skb->data + offset) = csum_fold(csum);
2417d7ca4cc0SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
2418d7ca4cc0SSridhar Samudrala 
2419d7ca4cc0SSridhar Samudrala 		segs = skb_segment(skb, features);
242073136267SPravin B Shelar 	}
2421d7ca4cc0SSridhar Samudrala out:
2422d7ca4cc0SSridhar Samudrala 	return segs;
2423d7ca4cc0SSridhar Samudrala }
2424