xref: /linux/net/ipv4/udp.c (revision f6b9664f8b711cf4fd53e70aa0d21f72d5bf806c)
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 
801da177e4SLinus Torvalds #include <asm/system.h>
811da177e4SLinus Torvalds #include <asm/uaccess.h>
821da177e4SLinus Torvalds #include <asm/ioctls.h>
8395766fffSHideo Aoki #include <linux/bootmem.h>
848203efb3SEric Dumazet #include <linux/highmem.h>
858203efb3SEric Dumazet #include <linux/swap.h>
861da177e4SLinus Torvalds #include <linux/types.h>
871da177e4SLinus Torvalds #include <linux/fcntl.h>
881da177e4SLinus Torvalds #include <linux/module.h>
891da177e4SLinus Torvalds #include <linux/socket.h>
901da177e4SLinus Torvalds #include <linux/sockios.h>
9114c85021SArnaldo Carvalho de Melo #include <linux/igmp.h>
921da177e4SLinus Torvalds #include <linux/in.h>
931da177e4SLinus Torvalds #include <linux/errno.h>
941da177e4SLinus Torvalds #include <linux/timer.h>
951da177e4SLinus Torvalds #include <linux/mm.h>
961da177e4SLinus Torvalds #include <linux/inet.h>
971da177e4SLinus Torvalds #include <linux/netdevice.h>
985a0e3ad6STejun Heo #include <linux/slab.h>
99c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
1001da177e4SLinus Torvalds #include <linux/skbuff.h>
1011da177e4SLinus Torvalds #include <linux/proc_fs.h>
1021da177e4SLinus Torvalds #include <linux/seq_file.h>
103457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1041da177e4SLinus Torvalds #include <net/icmp.h>
1051da177e4SLinus Torvalds #include <net/route.h>
1061da177e4SLinus Torvalds #include <net/checksum.h>
1071da177e4SLinus Torvalds #include <net/xfrm.h>
108ba4e58ecSGerrit Renker #include "udp_impl.h"
1091da177e4SLinus Torvalds 
110f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly;
111645ca708SEric Dumazet EXPORT_SYMBOL(udp_table);
1121da177e4SLinus Torvalds 
1138d987e5cSEric Dumazet long sysctl_udp_mem[3] __read_mostly;
11495766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem);
115c482c568SEric Dumazet 
116c482c568SEric Dumazet int sysctl_udp_rmem_min __read_mostly;
11795766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min);
118c482c568SEric Dumazet 
119c482c568SEric Dumazet int sysctl_udp_wmem_min __read_mostly;
12095766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min);
12195766fffSHideo Aoki 
1228d987e5cSEric Dumazet atomic_long_t udp_memory_allocated;
12395766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated);
12495766fffSHideo Aoki 
125f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536
126f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
12798322f22SEric Dumazet 
128f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num,
129645ca708SEric Dumazet 			       const struct udp_hslot *hslot,
13098322f22SEric Dumazet 			       unsigned long *bitmap,
131f24d43c0SEric Dumazet 			       struct sock *sk,
132f24d43c0SEric Dumazet 			       int (*saddr_comp)(const struct sock *sk1,
133f86dcc5aSEric Dumazet 						 const struct sock *sk2),
134f86dcc5aSEric Dumazet 			       unsigned int log)
13525030a7fSGerrit Renker {
136f24d43c0SEric Dumazet 	struct sock *sk2;
13788ab1932SEric Dumazet 	struct hlist_nulls_node *node;
13825030a7fSGerrit Renker 
13988ab1932SEric Dumazet 	sk_nulls_for_each(sk2, node, &hslot->head)
140f24d43c0SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
141f24d43c0SEric Dumazet 		    sk2 != sk &&
142d4cada4aSEric Dumazet 		    (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
143f24d43c0SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1449d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1459d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
14698322f22SEric Dumazet 		    (*saddr_comp)(sk, sk2)) {
14798322f22SEric Dumazet 			if (bitmap)
148d4cada4aSEric Dumazet 				__set_bit(udp_sk(sk2)->udp_port_hash >> log,
149d4cada4aSEric Dumazet 					  bitmap);
15098322f22SEric Dumazet 			else
151fc038410SDavid S. Miller 				return 1;
15298322f22SEric Dumazet 		}
15325030a7fSGerrit Renker 	return 0;
15425030a7fSGerrit Renker }
15525030a7fSGerrit Renker 
15630fff923SEric Dumazet /*
15730fff923SEric Dumazet  * Note: we still hold spinlock of primary hash chain, so no other writer
15830fff923SEric Dumazet  * can insert/delete a socket with local_port == num
15930fff923SEric Dumazet  */
16030fff923SEric Dumazet static int udp_lib_lport_inuse2(struct net *net, __u16 num,
16130fff923SEric Dumazet 			       struct udp_hslot *hslot2,
16230fff923SEric Dumazet 			       struct sock *sk,
16330fff923SEric Dumazet 			       int (*saddr_comp)(const struct sock *sk1,
16430fff923SEric Dumazet 						 const struct sock *sk2))
16530fff923SEric Dumazet {
16630fff923SEric Dumazet 	struct sock *sk2;
16730fff923SEric Dumazet 	struct hlist_nulls_node *node;
16830fff923SEric Dumazet 	int res = 0;
16930fff923SEric Dumazet 
17030fff923SEric Dumazet 	spin_lock(&hslot2->lock);
17130fff923SEric Dumazet 	udp_portaddr_for_each_entry(sk2, node, &hslot2->head)
17230fff923SEric Dumazet 		if (net_eq(sock_net(sk2), net) &&
17330fff923SEric Dumazet 		    sk2 != sk &&
17430fff923SEric Dumazet 		    (udp_sk(sk2)->udp_port_hash == num) &&
17530fff923SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
1769d4fb27dSJoe Perches 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
1779d4fb27dSJoe Perches 		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
17830fff923SEric Dumazet 		    (*saddr_comp)(sk, sk2)) {
17930fff923SEric Dumazet 			res = 1;
18030fff923SEric Dumazet 			break;
18130fff923SEric Dumazet 		}
18230fff923SEric Dumazet 	spin_unlock(&hslot2->lock);
18330fff923SEric Dumazet 	return res;
18430fff923SEric Dumazet }
18530fff923SEric Dumazet 
18625030a7fSGerrit Renker /**
1876ba5a3c5SPavel Emelyanov  *  udp_lib_get_port  -  UDP/-Lite port lookup for IPv4 and IPv6
18825030a7fSGerrit Renker  *
18925030a7fSGerrit Renker  *  @sk:          socket struct in question
19025030a7fSGerrit Renker  *  @snum:        port number to look up
191df2bc459SDavid S. Miller  *  @saddr_comp:  AF-dependent comparison of bound local IP addresses
19230fff923SEric Dumazet  *  @hash2_nulladdr: AF-dependant hash value in secondary hash chains,
19330fff923SEric Dumazet  *                   with NULL address
19425030a7fSGerrit Renker  */
1956ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum,
196df2bc459SDavid S. Miller 		       int (*saddr_comp)(const struct sock *sk1,
19730fff923SEric Dumazet 					 const struct sock *sk2),
19830fff923SEric Dumazet 		     unsigned int hash2_nulladdr)
1991da177e4SLinus Torvalds {
200512615b6SEric Dumazet 	struct udp_hslot *hslot, *hslot2;
201645ca708SEric Dumazet 	struct udp_table *udptable = sk->sk_prot->h.udp_table;
20225030a7fSGerrit Renker 	int    error = 1;
2033b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
2041da177e4SLinus Torvalds 
20532c1da70SStephen Hemminger 	if (!snum) {
2069088c560SEric Dumazet 		int low, high, remaining;
2079088c560SEric Dumazet 		unsigned rand;
20898322f22SEric Dumazet 		unsigned short first, last;
20998322f22SEric Dumazet 		DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
2101da177e4SLinus Torvalds 
211227b60f5SStephen Hemminger 		inet_get_local_port_range(&low, &high);
212a25de534SAnton Arapov 		remaining = (high - low) + 1;
213227b60f5SStephen Hemminger 
2149088c560SEric Dumazet 		rand = net_random();
21598322f22SEric Dumazet 		first = (((u64)rand * remaining) >> 32) + low;
21698322f22SEric Dumazet 		/*
21798322f22SEric Dumazet 		 * force rand to be an odd multiple of UDP_HTABLE_SIZE
21898322f22SEric Dumazet 		 */
219f86dcc5aSEric Dumazet 		rand = (rand | 1) * (udptable->mask + 1);
2205781b235SEric Dumazet 		last = first + udptable->mask + 1;
2215781b235SEric Dumazet 		do {
222f86dcc5aSEric Dumazet 			hslot = udp_hashslot(udptable, net, first);
22398322f22SEric Dumazet 			bitmap_zero(bitmap, PORTS_PER_CHAIN);
224645ca708SEric Dumazet 			spin_lock_bh(&hslot->lock);
22598322f22SEric Dumazet 			udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
226f86dcc5aSEric Dumazet 					    saddr_comp, udptable->log);
22798322f22SEric Dumazet 
22898322f22SEric Dumazet 			snum = first;
22998322f22SEric Dumazet 			/*
23098322f22SEric Dumazet 			 * Iterate on all possible values of snum for this hash.
23198322f22SEric Dumazet 			 * Using steps of an odd multiple of UDP_HTABLE_SIZE
23298322f22SEric Dumazet 			 * give us randomization and full range coverage.
23398322f22SEric Dumazet 			 */
2349088c560SEric Dumazet 			do {
23598322f22SEric Dumazet 				if (low <= snum && snum <= high &&
236e3826f1eSAmerigo Wang 				    !test_bit(snum >> udptable->log, bitmap) &&
237e3826f1eSAmerigo Wang 				    !inet_is_reserved_local_port(snum))
23898322f22SEric Dumazet 					goto found;
23998322f22SEric Dumazet 				snum += rand;
24098322f22SEric Dumazet 			} while (snum != first);
24198322f22SEric Dumazet 			spin_unlock_bh(&hslot->lock);
2425781b235SEric Dumazet 		} while (++first != last);
24398322f22SEric Dumazet 		goto fail;
244645ca708SEric Dumazet 	} else {
245f86dcc5aSEric Dumazet 		hslot = udp_hashslot(udptable, net, snum);
246645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
24730fff923SEric Dumazet 		if (hslot->count > 10) {
24830fff923SEric Dumazet 			int exist;
24930fff923SEric Dumazet 			unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
25030fff923SEric Dumazet 
25130fff923SEric Dumazet 			slot2          &= udptable->mask;
25230fff923SEric Dumazet 			hash2_nulladdr &= udptable->mask;
25330fff923SEric Dumazet 
25430fff923SEric Dumazet 			hslot2 = udp_hashslot2(udptable, slot2);
25530fff923SEric Dumazet 			if (hslot->count < hslot2->count)
25630fff923SEric Dumazet 				goto scan_primary_hash;
25730fff923SEric Dumazet 
25830fff923SEric Dumazet 			exist = udp_lib_lport_inuse2(net, snum, hslot2,
25930fff923SEric Dumazet 						     sk, saddr_comp);
26030fff923SEric Dumazet 			if (!exist && (hash2_nulladdr != slot2)) {
26130fff923SEric Dumazet 				hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
26230fff923SEric Dumazet 				exist = udp_lib_lport_inuse2(net, snum, hslot2,
26330fff923SEric Dumazet 							     sk, saddr_comp);
26430fff923SEric Dumazet 			}
26530fff923SEric Dumazet 			if (exist)
26630fff923SEric Dumazet 				goto fail_unlock;
26730fff923SEric Dumazet 			else
26830fff923SEric Dumazet 				goto found;
26930fff923SEric Dumazet 		}
27030fff923SEric Dumazet scan_primary_hash:
271f86dcc5aSEric Dumazet 		if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
272f86dcc5aSEric Dumazet 					saddr_comp, 0))
273645ca708SEric Dumazet 			goto fail_unlock;
274645ca708SEric Dumazet 	}
27598322f22SEric Dumazet found:
276c720c7e8SEric Dumazet 	inet_sk(sk)->inet_num = snum;
277d4cada4aSEric Dumazet 	udp_sk(sk)->udp_port_hash = snum;
278d4cada4aSEric Dumazet 	udp_sk(sk)->udp_portaddr_hash ^= snum;
2791da177e4SLinus Torvalds 	if (sk_unhashed(sk)) {
28088ab1932SEric Dumazet 		sk_nulls_add_node_rcu(sk, &hslot->head);
281fdcc8aa9SEric Dumazet 		hslot->count++;
282c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
283512615b6SEric Dumazet 
284512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
285512615b6SEric Dumazet 		spin_lock(&hslot2->lock);
286512615b6SEric Dumazet 		hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
287512615b6SEric Dumazet 					 &hslot2->head);
288512615b6SEric Dumazet 		hslot2->count++;
289512615b6SEric Dumazet 		spin_unlock(&hslot2->lock);
2901da177e4SLinus Torvalds 	}
29125030a7fSGerrit Renker 	error = 0;
292645ca708SEric Dumazet fail_unlock:
293645ca708SEric Dumazet 	spin_unlock_bh(&hslot->lock);
2941da177e4SLinus Torvalds fail:
29525030a7fSGerrit Renker 	return error;
2961da177e4SLinus Torvalds }
297c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port);
2981da177e4SLinus Torvalds 
299499923c7SVlad Yasevich static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
300db8dac20SDavid S. Miller {
301db8dac20SDavid S. Miller 	struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
302db8dac20SDavid S. Miller 
303db8dac20SDavid S. Miller 	return 	(!ipv6_only_sock(sk2)  &&
304c720c7e8SEric Dumazet 		 (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr ||
305c720c7e8SEric Dumazet 		   inet1->inet_rcv_saddr == inet2->inet_rcv_saddr));
306db8dac20SDavid S. Miller }
307db8dac20SDavid S. Miller 
308d4cada4aSEric Dumazet static unsigned int udp4_portaddr_hash(struct net *net, __be32 saddr,
309d4cada4aSEric Dumazet 				       unsigned int port)
310d4cada4aSEric Dumazet {
3110eae88f3SEric Dumazet 	return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
312d4cada4aSEric Dumazet }
313d4cada4aSEric Dumazet 
3146ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum)
315db8dac20SDavid S. Miller {
31630fff923SEric Dumazet 	unsigned int hash2_nulladdr =
3170eae88f3SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
31830fff923SEric Dumazet 	unsigned int hash2_partial =
31930fff923SEric Dumazet 		udp4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
32030fff923SEric Dumazet 
321d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
32230fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
32330fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal, hash2_nulladdr);
324db8dac20SDavid S. Miller }
325db8dac20SDavid S. Miller 
326645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
327645ca708SEric Dumazet 			 unsigned short hnum,
328645ca708SEric Dumazet 			 __be16 sport, __be32 daddr, __be16 dport, int dif)
329645ca708SEric Dumazet {
330645ca708SEric Dumazet 	int score = -1;
331645ca708SEric Dumazet 
332d4cada4aSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
333645ca708SEric Dumazet 			!ipv6_only_sock(sk)) {
334645ca708SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
335645ca708SEric Dumazet 
336645ca708SEric Dumazet 		score = (sk->sk_family == PF_INET ? 1 : 0);
337c720c7e8SEric Dumazet 		if (inet->inet_rcv_saddr) {
338c720c7e8SEric Dumazet 			if (inet->inet_rcv_saddr != daddr)
339645ca708SEric Dumazet 				return -1;
340645ca708SEric Dumazet 			score += 2;
341645ca708SEric Dumazet 		}
342c720c7e8SEric Dumazet 		if (inet->inet_daddr) {
343c720c7e8SEric Dumazet 			if (inet->inet_daddr != saddr)
344645ca708SEric Dumazet 				return -1;
345645ca708SEric Dumazet 			score += 2;
346645ca708SEric Dumazet 		}
347c720c7e8SEric Dumazet 		if (inet->inet_dport) {
348c720c7e8SEric Dumazet 			if (inet->inet_dport != sport)
349645ca708SEric Dumazet 				return -1;
350645ca708SEric Dumazet 			score += 2;
351645ca708SEric Dumazet 		}
352645ca708SEric Dumazet 		if (sk->sk_bound_dev_if) {
353645ca708SEric Dumazet 			if (sk->sk_bound_dev_if != dif)
354645ca708SEric Dumazet 				return -1;
355645ca708SEric Dumazet 			score += 2;
356645ca708SEric Dumazet 		}
357645ca708SEric Dumazet 	}
358645ca708SEric Dumazet 	return score;
359645ca708SEric Dumazet }
360645ca708SEric Dumazet 
3615051ebd2SEric Dumazet /*
3625051ebd2SEric Dumazet  * In this second variant, we check (daddr, dport) matches (inet_rcv_sadd, inet_num)
3635051ebd2SEric Dumazet  */
3645051ebd2SEric Dumazet #define SCORE2_MAX (1 + 2 + 2 + 2)
3655051ebd2SEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
3665051ebd2SEric Dumazet 				 __be32 saddr, __be16 sport,
3675051ebd2SEric Dumazet 				 __be32 daddr, unsigned int hnum, int dif)
3685051ebd2SEric Dumazet {
3695051ebd2SEric Dumazet 	int score = -1;
3705051ebd2SEric Dumazet 
3715051ebd2SEric Dumazet 	if (net_eq(sock_net(sk), net) && !ipv6_only_sock(sk)) {
3725051ebd2SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
3735051ebd2SEric Dumazet 
3745051ebd2SEric Dumazet 		if (inet->inet_rcv_saddr != daddr)
3755051ebd2SEric Dumazet 			return -1;
3765051ebd2SEric Dumazet 		if (inet->inet_num != hnum)
3775051ebd2SEric Dumazet 			return -1;
3785051ebd2SEric Dumazet 
3795051ebd2SEric Dumazet 		score = (sk->sk_family == PF_INET ? 1 : 0);
3805051ebd2SEric Dumazet 		if (inet->inet_daddr) {
3815051ebd2SEric Dumazet 			if (inet->inet_daddr != saddr)
3825051ebd2SEric Dumazet 				return -1;
3835051ebd2SEric Dumazet 			score += 2;
3845051ebd2SEric Dumazet 		}
3855051ebd2SEric Dumazet 		if (inet->inet_dport) {
3865051ebd2SEric Dumazet 			if (inet->inet_dport != sport)
3875051ebd2SEric Dumazet 				return -1;
3885051ebd2SEric Dumazet 			score += 2;
3895051ebd2SEric Dumazet 		}
3905051ebd2SEric Dumazet 		if (sk->sk_bound_dev_if) {
3915051ebd2SEric Dumazet 			if (sk->sk_bound_dev_if != dif)
3925051ebd2SEric Dumazet 				return -1;
3935051ebd2SEric Dumazet 			score += 2;
3945051ebd2SEric Dumazet 		}
3955051ebd2SEric Dumazet 	}
3965051ebd2SEric Dumazet 	return score;
3975051ebd2SEric Dumazet }
3985051ebd2SEric Dumazet 
3995051ebd2SEric Dumazet 
4005051ebd2SEric Dumazet /* called with read_rcu_lock() */
4015051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net,
4025051ebd2SEric Dumazet 		__be32 saddr, __be16 sport,
4035051ebd2SEric Dumazet 		__be32 daddr, unsigned int hnum, int dif,
4045051ebd2SEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
4055051ebd2SEric Dumazet {
4065051ebd2SEric Dumazet 	struct sock *sk, *result;
4075051ebd2SEric Dumazet 	struct hlist_nulls_node *node;
4085051ebd2SEric Dumazet 	int score, badness;
4095051ebd2SEric Dumazet 
4105051ebd2SEric Dumazet begin:
4115051ebd2SEric Dumazet 	result = NULL;
4125051ebd2SEric Dumazet 	badness = -1;
4135051ebd2SEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
4145051ebd2SEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
4155051ebd2SEric Dumazet 				      daddr, hnum, dif);
4165051ebd2SEric Dumazet 		if (score > badness) {
4175051ebd2SEric Dumazet 			result = sk;
4185051ebd2SEric Dumazet 			badness = score;
4195051ebd2SEric Dumazet 			if (score == SCORE2_MAX)
4205051ebd2SEric Dumazet 				goto exact_match;
4215051ebd2SEric Dumazet 		}
4225051ebd2SEric Dumazet 	}
4235051ebd2SEric Dumazet 	/*
4245051ebd2SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
4255051ebd2SEric Dumazet 	 * not the expected one, we must restart lookup.
4265051ebd2SEric Dumazet 	 * We probably met an item that was moved to another chain.
4275051ebd2SEric Dumazet 	 */
4285051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot2)
4295051ebd2SEric Dumazet 		goto begin;
4305051ebd2SEric Dumazet 
4315051ebd2SEric Dumazet 	if (result) {
4325051ebd2SEric Dumazet exact_match:
433c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
4345051ebd2SEric Dumazet 			result = NULL;
4355051ebd2SEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
4365051ebd2SEric Dumazet 				  daddr, hnum, dif) < badness)) {
4375051ebd2SEric Dumazet 			sock_put(result);
4385051ebd2SEric Dumazet 			goto begin;
4395051ebd2SEric Dumazet 		}
4405051ebd2SEric Dumazet 	}
4415051ebd2SEric Dumazet 	return result;
4425051ebd2SEric Dumazet }
4435051ebd2SEric Dumazet 
444db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
445db8dac20SDavid S. Miller  * harder than this. -DaveM
446db8dac20SDavid S. Miller  */
447db8dac20SDavid S. Miller static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
448db8dac20SDavid S. Miller 		__be16 sport, __be32 daddr, __be16 dport,
449645ca708SEric Dumazet 		int dif, struct udp_table *udptable)
450db8dac20SDavid S. Miller {
451271b72c7SEric Dumazet 	struct sock *sk, *result;
45288ab1932SEric Dumazet 	struct hlist_nulls_node *node;
453db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(dport);
4545051ebd2SEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
4555051ebd2SEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
456271b72c7SEric Dumazet 	int score, badness;
457db8dac20SDavid S. Miller 
458271b72c7SEric Dumazet 	rcu_read_lock();
4595051ebd2SEric Dumazet 	if (hslot->count > 10) {
4605051ebd2SEric Dumazet 		hash2 = udp4_portaddr_hash(net, daddr, hnum);
4615051ebd2SEric Dumazet 		slot2 = hash2 & udptable->mask;
4625051ebd2SEric Dumazet 		hslot2 = &udptable->hash2[slot2];
4635051ebd2SEric Dumazet 		if (hslot->count < hslot2->count)
4645051ebd2SEric Dumazet 			goto begin;
4655051ebd2SEric Dumazet 
4665051ebd2SEric Dumazet 		result = udp4_lib_lookup2(net, saddr, sport,
4675051ebd2SEric Dumazet 					  daddr, hnum, dif,
4685051ebd2SEric Dumazet 					  hslot2, slot2);
4695051ebd2SEric Dumazet 		if (!result) {
4700eae88f3SEric Dumazet 			hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
4715051ebd2SEric Dumazet 			slot2 = hash2 & udptable->mask;
4725051ebd2SEric Dumazet 			hslot2 = &udptable->hash2[slot2];
4735051ebd2SEric Dumazet 			if (hslot->count < hslot2->count)
4745051ebd2SEric Dumazet 				goto begin;
4755051ebd2SEric Dumazet 
4761223c67cSJorge Boncompte [DTI2] 			result = udp4_lib_lookup2(net, saddr, sport,
4770eae88f3SEric Dumazet 						  htonl(INADDR_ANY), hnum, dif,
4785051ebd2SEric Dumazet 						  hslot2, slot2);
4795051ebd2SEric Dumazet 		}
4805051ebd2SEric Dumazet 		rcu_read_unlock();
4815051ebd2SEric Dumazet 		return result;
4825051ebd2SEric Dumazet 	}
483271b72c7SEric Dumazet begin:
484271b72c7SEric Dumazet 	result = NULL;
485271b72c7SEric Dumazet 	badness = -1;
48688ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
487645ca708SEric Dumazet 		score = compute_score(sk, net, saddr, hnum, sport,
488645ca708SEric Dumazet 				      daddr, dport, dif);
489645ca708SEric Dumazet 		if (score > badness) {
490db8dac20SDavid S. Miller 			result = sk;
491db8dac20SDavid S. Miller 			badness = score;
492db8dac20SDavid S. Miller 		}
493db8dac20SDavid S. Miller 	}
49488ab1932SEric Dumazet 	/*
49588ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
49688ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
49788ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
49888ab1932SEric Dumazet 	 */
4995051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot)
50088ab1932SEric Dumazet 		goto begin;
50188ab1932SEric Dumazet 
502271b72c7SEric Dumazet 	if (result) {
503c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
504271b72c7SEric Dumazet 			result = NULL;
505271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, saddr, hnum, sport,
506271b72c7SEric Dumazet 				  daddr, dport, dif) < badness)) {
507271b72c7SEric Dumazet 			sock_put(result);
508271b72c7SEric Dumazet 			goto begin;
509271b72c7SEric Dumazet 		}
510271b72c7SEric Dumazet 	}
511271b72c7SEric Dumazet 	rcu_read_unlock();
512db8dac20SDavid S. Miller 	return result;
513db8dac20SDavid S. Miller }
514db8dac20SDavid S. Miller 
515607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
516607c4aafSKOVACS Krisztian 						 __be16 sport, __be16 dport,
517645ca708SEric Dumazet 						 struct udp_table *udptable)
518607c4aafSKOVACS Krisztian {
51923542618SKOVACS Krisztian 	struct sock *sk;
520607c4aafSKOVACS Krisztian 	const struct iphdr *iph = ip_hdr(skb);
521607c4aafSKOVACS Krisztian 
52223542618SKOVACS Krisztian 	if (unlikely(sk = skb_steal_sock(skb)))
52323542618SKOVACS Krisztian 		return sk;
52423542618SKOVACS Krisztian 	else
525adf30907SEric Dumazet 		return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
526607c4aafSKOVACS Krisztian 					 iph->daddr, dport, inet_iif(skb),
527607c4aafSKOVACS Krisztian 					 udptable);
528607c4aafSKOVACS Krisztian }
529607c4aafSKOVACS Krisztian 
530bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
531bcd41303SKOVACS Krisztian 			     __be32 daddr, __be16 dport, int dif)
532bcd41303SKOVACS Krisztian {
533645ca708SEric Dumazet 	return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
534bcd41303SKOVACS Krisztian }
535bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup);
536bcd41303SKOVACS Krisztian 
537920a4611SEric Dumazet static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
538db8dac20SDavid S. Miller 					     __be16 loc_port, __be32 loc_addr,
539db8dac20SDavid S. Miller 					     __be16 rmt_port, __be32 rmt_addr,
540db8dac20SDavid S. Miller 					     int dif)
541db8dac20SDavid S. Miller {
54288ab1932SEric Dumazet 	struct hlist_nulls_node *node;
543db8dac20SDavid S. Miller 	struct sock *s = sk;
544db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(loc_port);
545db8dac20SDavid S. Miller 
54688ab1932SEric Dumazet 	sk_nulls_for_each_from(s, node) {
547db8dac20SDavid S. Miller 		struct inet_sock *inet = inet_sk(s);
548db8dac20SDavid S. Miller 
549920a4611SEric Dumazet 		if (!net_eq(sock_net(s), net) ||
550d4cada4aSEric Dumazet 		    udp_sk(s)->udp_port_hash != hnum ||
551c720c7e8SEric Dumazet 		    (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
552c720c7e8SEric Dumazet 		    (inet->inet_dport != rmt_port && inet->inet_dport) ||
553c720c7e8SEric Dumazet 		    (inet->inet_rcv_saddr &&
554c720c7e8SEric Dumazet 		     inet->inet_rcv_saddr != loc_addr) ||
555db8dac20SDavid S. Miller 		    ipv6_only_sock(s) ||
556db8dac20SDavid S. Miller 		    (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
557db8dac20SDavid S. Miller 			continue;
558db8dac20SDavid S. Miller 		if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
559db8dac20SDavid S. Miller 			continue;
560db8dac20SDavid S. Miller 		goto found;
561db8dac20SDavid S. Miller 	}
562db8dac20SDavid S. Miller 	s = NULL;
563db8dac20SDavid S. Miller found:
564db8dac20SDavid S. Miller 	return s;
565db8dac20SDavid S. Miller }
566db8dac20SDavid S. Miller 
567db8dac20SDavid S. Miller /*
568db8dac20SDavid S. Miller  * This routine is called by the ICMP module when it gets some
569db8dac20SDavid S. Miller  * sort of error condition.  If err < 0 then the socket should
570db8dac20SDavid S. Miller  * be closed and the error returned to the user.  If err > 0
571db8dac20SDavid S. Miller  * it's just the icmp type << 8 | icmp code.
572db8dac20SDavid S. Miller  * Header points to the ip header of the error packet. We move
573db8dac20SDavid S. Miller  * on past this. Then (as it used to claim before adjustment)
574db8dac20SDavid S. Miller  * header points to the first 8 bytes of the udp header.  We need
575db8dac20SDavid S. Miller  * to find the appropriate port.
576db8dac20SDavid S. Miller  */
577db8dac20SDavid S. Miller 
578645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
579db8dac20SDavid S. Miller {
580db8dac20SDavid S. Miller 	struct inet_sock *inet;
581db8dac20SDavid S. Miller 	struct iphdr *iph = (struct iphdr *)skb->data;
582db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
583db8dac20SDavid S. Miller 	const int type = icmp_hdr(skb)->type;
584db8dac20SDavid S. Miller 	const int code = icmp_hdr(skb)->code;
585db8dac20SDavid S. Miller 	struct sock *sk;
586db8dac20SDavid S. Miller 	int harderr;
587db8dac20SDavid S. Miller 	int err;
588fd54d716SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
589db8dac20SDavid S. Miller 
590fd54d716SPavel Emelyanov 	sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
591db8dac20SDavid S. Miller 			iph->saddr, uh->source, skb->dev->ifindex, udptable);
592db8dac20SDavid S. Miller 	if (sk == NULL) {
593dcfc23caSPavel Emelyanov 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
594db8dac20SDavid S. Miller 		return;	/* No socket for error */
595db8dac20SDavid S. Miller 	}
596db8dac20SDavid S. Miller 
597db8dac20SDavid S. Miller 	err = 0;
598db8dac20SDavid S. Miller 	harderr = 0;
599db8dac20SDavid S. Miller 	inet = inet_sk(sk);
600db8dac20SDavid S. Miller 
601db8dac20SDavid S. Miller 	switch (type) {
602db8dac20SDavid S. Miller 	default:
603db8dac20SDavid S. Miller 	case ICMP_TIME_EXCEEDED:
604db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
605db8dac20SDavid S. Miller 		break;
606db8dac20SDavid S. Miller 	case ICMP_SOURCE_QUENCH:
607db8dac20SDavid S. Miller 		goto out;
608db8dac20SDavid S. Miller 	case ICMP_PARAMETERPROB:
609db8dac20SDavid S. Miller 		err = EPROTO;
610db8dac20SDavid S. Miller 		harderr = 1;
611db8dac20SDavid S. Miller 		break;
612db8dac20SDavid S. Miller 	case ICMP_DEST_UNREACH:
613db8dac20SDavid S. Miller 		if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
614db8dac20SDavid S. Miller 			if (inet->pmtudisc != IP_PMTUDISC_DONT) {
615db8dac20SDavid S. Miller 				err = EMSGSIZE;
616db8dac20SDavid S. Miller 				harderr = 1;
617db8dac20SDavid S. Miller 				break;
618db8dac20SDavid S. Miller 			}
619db8dac20SDavid S. Miller 			goto out;
620db8dac20SDavid S. Miller 		}
621db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
622db8dac20SDavid S. Miller 		if (code <= NR_ICMP_UNREACH) {
623db8dac20SDavid S. Miller 			harderr = icmp_err_convert[code].fatal;
624db8dac20SDavid S. Miller 			err = icmp_err_convert[code].errno;
625db8dac20SDavid S. Miller 		}
626db8dac20SDavid S. Miller 		break;
627db8dac20SDavid S. Miller 	}
628db8dac20SDavid S. Miller 
629db8dac20SDavid S. Miller 	/*
630db8dac20SDavid S. Miller 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
631db8dac20SDavid S. Miller 	 *	4.1.3.3.
632db8dac20SDavid S. Miller 	 */
633db8dac20SDavid S. Miller 	if (!inet->recverr) {
634db8dac20SDavid S. Miller 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
635db8dac20SDavid S. Miller 			goto out;
636b1faf566SEric Dumazet 	} else
637db8dac20SDavid S. Miller 		ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
638b1faf566SEric Dumazet 
639db8dac20SDavid S. Miller 	sk->sk_err = err;
640db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
641db8dac20SDavid S. Miller out:
642db8dac20SDavid S. Miller 	sock_put(sk);
643db8dac20SDavid S. Miller }
644db8dac20SDavid S. Miller 
645db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info)
646db8dac20SDavid S. Miller {
647645ca708SEric Dumazet 	__udp4_lib_err(skb, info, &udp_table);
648db8dac20SDavid S. Miller }
649db8dac20SDavid S. Miller 
650db8dac20SDavid S. Miller /*
651db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
652db8dac20SDavid S. Miller  */
65336d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk)
654db8dac20SDavid S. Miller {
655db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
656db8dac20SDavid S. Miller 
657db8dac20SDavid S. Miller 	if (up->pending) {
658db8dac20SDavid S. Miller 		up->len = 0;
659db8dac20SDavid S. Miller 		up->pending = 0;
660db8dac20SDavid S. Miller 		ip_flush_pending_frames(sk);
661db8dac20SDavid S. Miller 	}
662db8dac20SDavid S. Miller }
66336d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames);
664db8dac20SDavid S. Miller 
665db8dac20SDavid S. Miller /**
666*f6b9664fSHerbert Xu  * 	udp4_hwcsum  -  handle outgoing HW checksumming
667db8dac20SDavid S. Miller  * 	@skb: 	sk_buff containing the filled-in UDP header
668db8dac20SDavid S. Miller  * 	        (checksum field must be zeroed out)
669*f6b9664fSHerbert Xu  *	@src:	source IP address
670*f6b9664fSHerbert Xu  *	@dst:	destination IP address
671db8dac20SDavid S. Miller  */
672*f6b9664fSHerbert Xu static void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
673db8dac20SDavid S. Miller {
674db8dac20SDavid S. Miller 	struct udphdr *uh = udp_hdr(skb);
675*f6b9664fSHerbert Xu 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
676*f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
677*f6b9664fSHerbert Xu 	int len = skb->len - offset;
678*f6b9664fSHerbert Xu 	int hlen = len;
679db8dac20SDavid S. Miller 	__wsum csum = 0;
680db8dac20SDavid S. Miller 
681*f6b9664fSHerbert Xu 	if (!frags) {
682db8dac20SDavid S. Miller 		/*
683db8dac20SDavid S. Miller 		 * Only one fragment on the socket.
684db8dac20SDavid S. Miller 		 */
685db8dac20SDavid S. Miller 		skb->csum_start = skb_transport_header(skb) - skb->head;
686db8dac20SDavid S. Miller 		skb->csum_offset = offsetof(struct udphdr, check);
687*f6b9664fSHerbert Xu 		uh->check = ~csum_tcpudp_magic(src, dst, len,
688*f6b9664fSHerbert Xu 					       IPPROTO_UDP, 0);
689db8dac20SDavid S. Miller 	} else {
690db8dac20SDavid S. Miller 		/*
691db8dac20SDavid S. Miller 		 * HW-checksum won't work as there are two or more
692db8dac20SDavid S. Miller 		 * fragments on the socket so that all csums of sk_buffs
693db8dac20SDavid S. Miller 		 * should be together
694db8dac20SDavid S. Miller 		 */
695*f6b9664fSHerbert Xu 		do {
696*f6b9664fSHerbert Xu 			csum = csum_add(csum, frags->csum);
697*f6b9664fSHerbert Xu 			hlen -= frags->len;
698*f6b9664fSHerbert Xu 		} while ((frags = frags->next));
699db8dac20SDavid S. Miller 
700*f6b9664fSHerbert Xu 		csum = skb_checksum(skb, offset, hlen, csum);
701db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
702db8dac20SDavid S. Miller 
703db8dac20SDavid S. Miller 		uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
704db8dac20SDavid S. Miller 		if (uh->check == 0)
705db8dac20SDavid S. Miller 			uh->check = CSUM_MANGLED_0;
706db8dac20SDavid S. Miller 	}
707db8dac20SDavid S. Miller }
708db8dac20SDavid S. Miller 
709*f6b9664fSHerbert Xu static int udp_send_skb(struct sk_buff *skb, __be32 daddr, __be32 dport)
710*f6b9664fSHerbert Xu {
711*f6b9664fSHerbert Xu 	struct sock *sk = skb->sk;
712*f6b9664fSHerbert Xu 	struct inet_sock *inet = inet_sk(sk);
713*f6b9664fSHerbert Xu 	struct udphdr *uh;
714*f6b9664fSHerbert Xu 	struct rtable *rt = (struct rtable *)skb_dst(skb);
715*f6b9664fSHerbert Xu 	int err = 0;
716*f6b9664fSHerbert Xu 	int is_udplite = IS_UDPLITE(sk);
717*f6b9664fSHerbert Xu 	int offset = skb_transport_offset(skb);
718*f6b9664fSHerbert Xu 	int len = skb->len - offset;
719*f6b9664fSHerbert Xu 	__wsum csum = 0;
720*f6b9664fSHerbert Xu 
721*f6b9664fSHerbert Xu 	/*
722*f6b9664fSHerbert Xu 	 * Create a UDP header
723*f6b9664fSHerbert Xu 	 */
724*f6b9664fSHerbert Xu 	uh = udp_hdr(skb);
725*f6b9664fSHerbert Xu 	uh->source = inet->inet_sport;
726*f6b9664fSHerbert Xu 	uh->dest = dport;
727*f6b9664fSHerbert Xu 	uh->len = htons(len);
728*f6b9664fSHerbert Xu 	uh->check = 0;
729*f6b9664fSHerbert Xu 
730*f6b9664fSHerbert Xu 	if (is_udplite)  				 /*     UDP-Lite      */
731*f6b9664fSHerbert Xu 		csum = udplite_csum(skb);
732*f6b9664fSHerbert Xu 
733*f6b9664fSHerbert Xu 	else if (sk->sk_no_check == UDP_CSUM_NOXMIT) {   /* UDP csum disabled */
734*f6b9664fSHerbert Xu 
735*f6b9664fSHerbert Xu 		skb->ip_summed = CHECKSUM_NONE;
736*f6b9664fSHerbert Xu 		goto send;
737*f6b9664fSHerbert Xu 
738*f6b9664fSHerbert Xu 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
739*f6b9664fSHerbert Xu 
740*f6b9664fSHerbert Xu 		udp4_hwcsum(skb, rt->rt_src, daddr);
741*f6b9664fSHerbert Xu 		goto send;
742*f6b9664fSHerbert Xu 
743*f6b9664fSHerbert Xu 	} else
744*f6b9664fSHerbert Xu 		csum = udp_csum(skb);
745*f6b9664fSHerbert Xu 
746*f6b9664fSHerbert Xu 	/* add protocol-dependent pseudo-header */
747*f6b9664fSHerbert Xu 	uh->check = csum_tcpudp_magic(rt->rt_src, daddr, len,
748*f6b9664fSHerbert Xu 				      sk->sk_protocol, csum);
749*f6b9664fSHerbert Xu 	if (uh->check == 0)
750*f6b9664fSHerbert Xu 		uh->check = CSUM_MANGLED_0;
751*f6b9664fSHerbert Xu 
752*f6b9664fSHerbert Xu send:
753*f6b9664fSHerbert Xu 	err = ip_send_skb(skb);
754*f6b9664fSHerbert Xu 	if (err) {
755*f6b9664fSHerbert Xu 		if (err == -ENOBUFS && !inet->recverr) {
756*f6b9664fSHerbert Xu 			UDP_INC_STATS_USER(sock_net(sk),
757*f6b9664fSHerbert Xu 					   UDP_MIB_SNDBUFERRORS, is_udplite);
758*f6b9664fSHerbert Xu 			err = 0;
759*f6b9664fSHerbert Xu 		}
760*f6b9664fSHerbert Xu 	} else
761*f6b9664fSHerbert Xu 		UDP_INC_STATS_USER(sock_net(sk),
762*f6b9664fSHerbert Xu 				   UDP_MIB_OUTDATAGRAMS, is_udplite);
763*f6b9664fSHerbert Xu 	return err;
764*f6b9664fSHerbert Xu }
765*f6b9664fSHerbert Xu 
766db8dac20SDavid S. Miller /*
767db8dac20SDavid S. Miller  * Push out all pending data as one UDP datagram. Socket is locked.
768db8dac20SDavid S. Miller  */
769db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk)
770db8dac20SDavid S. Miller {
771db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
772db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
773db8dac20SDavid S. Miller 	struct flowi *fl = &inet->cork.fl;
774db8dac20SDavid S. Miller 	struct sk_buff *skb;
775db8dac20SDavid S. Miller 	int err = 0;
776db8dac20SDavid S. Miller 
777*f6b9664fSHerbert Xu 	skb = ip_finish_skb(sk);
778*f6b9664fSHerbert Xu 	if (!skb)
779db8dac20SDavid S. Miller 		goto out;
780db8dac20SDavid S. Miller 
781*f6b9664fSHerbert Xu 	err = udp_send_skb(skb, fl->fl4_dst, fl->fl_ip_dport);
782db8dac20SDavid S. Miller 
783db8dac20SDavid S. Miller out:
784db8dac20SDavid S. Miller 	up->len = 0;
785db8dac20SDavid S. Miller 	up->pending = 0;
786db8dac20SDavid S. Miller 	return err;
787db8dac20SDavid S. Miller }
788db8dac20SDavid S. Miller 
789db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
790db8dac20SDavid S. Miller 		size_t len)
791db8dac20SDavid S. Miller {
792db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
793db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
794db8dac20SDavid S. Miller 	int ulen = len;
795db8dac20SDavid S. Miller 	struct ipcm_cookie ipc;
796db8dac20SDavid S. Miller 	struct rtable *rt = NULL;
797db8dac20SDavid S. Miller 	int free = 0;
798db8dac20SDavid S. Miller 	int connected = 0;
799db8dac20SDavid S. Miller 	__be32 daddr, faddr, saddr;
800db8dac20SDavid S. Miller 	__be16 dport;
801db8dac20SDavid S. Miller 	u8  tos;
802db8dac20SDavid S. Miller 	int err, is_udplite = IS_UDPLITE(sk);
803db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
804db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
805db8dac20SDavid S. Miller 
806db8dac20SDavid S. Miller 	if (len > 0xFFFF)
807db8dac20SDavid S. Miller 		return -EMSGSIZE;
808db8dac20SDavid S. Miller 
809db8dac20SDavid S. Miller 	/*
810db8dac20SDavid S. Miller 	 *	Check the flags.
811db8dac20SDavid S. Miller 	 */
812db8dac20SDavid S. Miller 
813db8dac20SDavid S. Miller 	if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
814db8dac20SDavid S. Miller 		return -EOPNOTSUPP;
815db8dac20SDavid S. Miller 
816db8dac20SDavid S. Miller 	ipc.opt = NULL;
8172244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
818db8dac20SDavid S. Miller 
819db8dac20SDavid S. Miller 	if (up->pending) {
820db8dac20SDavid S. Miller 		/*
821db8dac20SDavid S. Miller 		 * There are pending frames.
822db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
823db8dac20SDavid S. Miller 		 */
824db8dac20SDavid S. Miller 		lock_sock(sk);
825db8dac20SDavid S. Miller 		if (likely(up->pending)) {
826db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET)) {
827db8dac20SDavid S. Miller 				release_sock(sk);
828db8dac20SDavid S. Miller 				return -EINVAL;
829db8dac20SDavid S. Miller 			}
830db8dac20SDavid S. Miller 			goto do_append_data;
831db8dac20SDavid S. Miller 		}
832db8dac20SDavid S. Miller 		release_sock(sk);
833db8dac20SDavid S. Miller 	}
834db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
835db8dac20SDavid S. Miller 
836db8dac20SDavid S. Miller 	/*
837db8dac20SDavid S. Miller 	 *	Get and verify the address.
838db8dac20SDavid S. Miller 	 */
839db8dac20SDavid S. Miller 	if (msg->msg_name) {
840db8dac20SDavid S. Miller 		struct sockaddr_in * usin = (struct sockaddr_in *)msg->msg_name;
841db8dac20SDavid S. Miller 		if (msg->msg_namelen < sizeof(*usin))
842db8dac20SDavid S. Miller 			return -EINVAL;
843db8dac20SDavid S. Miller 		if (usin->sin_family != AF_INET) {
844db8dac20SDavid S. Miller 			if (usin->sin_family != AF_UNSPEC)
845db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
846db8dac20SDavid S. Miller 		}
847db8dac20SDavid S. Miller 
848db8dac20SDavid S. Miller 		daddr = usin->sin_addr.s_addr;
849db8dac20SDavid S. Miller 		dport = usin->sin_port;
850db8dac20SDavid S. Miller 		if (dport == 0)
851db8dac20SDavid S. Miller 			return -EINVAL;
852db8dac20SDavid S. Miller 	} else {
853db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
854db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
855c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
856c720c7e8SEric Dumazet 		dport = inet->inet_dport;
857db8dac20SDavid S. Miller 		/* Open fast path for connected socket.
858db8dac20SDavid S. Miller 		   Route will not be used, if at least one option is set.
859db8dac20SDavid S. Miller 		 */
860db8dac20SDavid S. Miller 		connected = 1;
861db8dac20SDavid S. Miller 	}
862c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
863db8dac20SDavid S. Miller 
864db8dac20SDavid S. Miller 	ipc.oif = sk->sk_bound_dev_if;
8652244d07bSOliver Hartkopp 	err = sock_tx_timestamp(sk, &ipc.tx_flags);
86651f31cabSPatrick Ohly 	if (err)
86751f31cabSPatrick Ohly 		return err;
868db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
8693b1e0a65SYOSHIFUJI Hideaki 		err = ip_cmsg_send(sock_net(sk), msg, &ipc);
870db8dac20SDavid S. Miller 		if (err)
871db8dac20SDavid S. Miller 			return err;
872db8dac20SDavid S. Miller 		if (ipc.opt)
873db8dac20SDavid S. Miller 			free = 1;
874db8dac20SDavid S. Miller 		connected = 0;
875db8dac20SDavid S. Miller 	}
876db8dac20SDavid S. Miller 	if (!ipc.opt)
877db8dac20SDavid S. Miller 		ipc.opt = inet->opt;
878db8dac20SDavid S. Miller 
879db8dac20SDavid S. Miller 	saddr = ipc.addr;
880db8dac20SDavid S. Miller 	ipc.addr = faddr = daddr;
881db8dac20SDavid S. Miller 
882db8dac20SDavid S. Miller 	if (ipc.opt && ipc.opt->srr) {
883db8dac20SDavid S. Miller 		if (!daddr)
884db8dac20SDavid S. Miller 			return -EINVAL;
885db8dac20SDavid S. Miller 		faddr = ipc.opt->faddr;
886db8dac20SDavid S. Miller 		connected = 0;
887db8dac20SDavid S. Miller 	}
888db8dac20SDavid S. Miller 	tos = RT_TOS(inet->tos);
889db8dac20SDavid S. Miller 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
890db8dac20SDavid S. Miller 	    (msg->msg_flags & MSG_DONTROUTE) ||
891db8dac20SDavid S. Miller 	    (ipc.opt && ipc.opt->is_strictroute)) {
892db8dac20SDavid S. Miller 		tos |= RTO_ONLINK;
893db8dac20SDavid S. Miller 		connected = 0;
894db8dac20SDavid S. Miller 	}
895db8dac20SDavid S. Miller 
896db8dac20SDavid S. Miller 	if (ipv4_is_multicast(daddr)) {
897db8dac20SDavid S. Miller 		if (!ipc.oif)
898db8dac20SDavid S. Miller 			ipc.oif = inet->mc_index;
899db8dac20SDavid S. Miller 		if (!saddr)
900db8dac20SDavid S. Miller 			saddr = inet->mc_addr;
901db8dac20SDavid S. Miller 		connected = 0;
902db8dac20SDavid S. Miller 	}
903db8dac20SDavid S. Miller 
904db8dac20SDavid S. Miller 	if (connected)
905db8dac20SDavid S. Miller 		rt = (struct rtable *)sk_dst_check(sk, 0);
906db8dac20SDavid S. Miller 
907db8dac20SDavid S. Miller 	if (rt == NULL) {
908db8dac20SDavid S. Miller 		struct flowi fl = { .oif = ipc.oif,
909914a9ab3SAtis Elsts 				    .mark = sk->sk_mark,
9105811662bSChangli Gao 				    .fl4_dst = faddr,
9115811662bSChangli Gao 				    .fl4_src = saddr,
9125811662bSChangli Gao 				    .fl4_tos = tos,
913db8dac20SDavid S. Miller 				    .proto = sk->sk_protocol,
914a134f85cSBalazs Scheidler 				    .flags = inet_sk_flowi_flags(sk),
9155811662bSChangli Gao 				    .fl_ip_sport = inet->inet_sport,
9165811662bSChangli Gao 				    .fl_ip_dport = dport };
91784a3aa00SPavel Emelyanov 		struct net *net = sock_net(sk);
91884a3aa00SPavel Emelyanov 
919db8dac20SDavid S. Miller 		security_sk_classify_flow(sk, &fl);
92084a3aa00SPavel Emelyanov 		err = ip_route_output_flow(net, &rt, &fl, sk, 1);
921db8dac20SDavid S. Miller 		if (err) {
922db8dac20SDavid S. Miller 			if (err == -ENETUNREACH)
9237c73a6faSPavel Emelyanov 				IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
924db8dac20SDavid S. Miller 			goto out;
925db8dac20SDavid S. Miller 		}
926db8dac20SDavid S. Miller 
927db8dac20SDavid S. Miller 		err = -EACCES;
928db8dac20SDavid S. Miller 		if ((rt->rt_flags & RTCF_BROADCAST) &&
929db8dac20SDavid S. Miller 		    !sock_flag(sk, SOCK_BROADCAST))
930db8dac20SDavid S. Miller 			goto out;
931db8dac20SDavid S. Miller 		if (connected)
932d8d1f30bSChangli Gao 			sk_dst_set(sk, dst_clone(&rt->dst));
933db8dac20SDavid S. Miller 	}
934db8dac20SDavid S. Miller 
935db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
936db8dac20SDavid S. Miller 		goto do_confirm;
937db8dac20SDavid S. Miller back_from_confirm:
938db8dac20SDavid S. Miller 
939db8dac20SDavid S. Miller 	saddr = rt->rt_src;
940db8dac20SDavid S. Miller 	if (!ipc.addr)
941db8dac20SDavid S. Miller 		daddr = ipc.addr = rt->rt_dst;
942db8dac20SDavid S. Miller 
943db8dac20SDavid S. Miller 	lock_sock(sk);
944db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
945db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
946db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
947db8dac20SDavid S. Miller 		release_sock(sk);
948db8dac20SDavid S. Miller 
949db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
950db8dac20SDavid S. Miller 		err = -EINVAL;
951db8dac20SDavid S. Miller 		goto out;
952db8dac20SDavid S. Miller 	}
953db8dac20SDavid S. Miller 	/*
954db8dac20SDavid S. Miller 	 *	Now cork the socket to pend data.
955db8dac20SDavid S. Miller 	 */
956db8dac20SDavid S. Miller 	inet->cork.fl.fl4_dst = daddr;
957db8dac20SDavid S. Miller 	inet->cork.fl.fl_ip_dport = dport;
958db8dac20SDavid S. Miller 	inet->cork.fl.fl4_src = saddr;
959c720c7e8SEric Dumazet 	inet->cork.fl.fl_ip_sport = inet->inet_sport;
960db8dac20SDavid S. Miller 	up->pending = AF_INET;
961db8dac20SDavid S. Miller 
962db8dac20SDavid S. Miller do_append_data:
963db8dac20SDavid S. Miller 	up->len += ulen;
964db8dac20SDavid S. Miller 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
965db8dac20SDavid S. Miller 	err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
9662e77d89bSEric Dumazet 			sizeof(struct udphdr), &ipc, &rt,
967db8dac20SDavid S. Miller 			corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
968db8dac20SDavid S. Miller 	if (err)
969db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
970db8dac20SDavid S. Miller 	else if (!corkreq)
971db8dac20SDavid S. Miller 		err = udp_push_pending_frames(sk);
972db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
973db8dac20SDavid S. Miller 		up->pending = 0;
974db8dac20SDavid S. Miller 	release_sock(sk);
975db8dac20SDavid S. Miller 
976db8dac20SDavid S. Miller out:
977db8dac20SDavid S. Miller 	ip_rt_put(rt);
978db8dac20SDavid S. Miller 	if (free)
979db8dac20SDavid S. Miller 		kfree(ipc.opt);
980db8dac20SDavid S. Miller 	if (!err)
981db8dac20SDavid S. Miller 		return len;
982db8dac20SDavid S. Miller 	/*
983db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
984db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
985db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
986db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
987db8dac20SDavid S. Miller 	 * seems like overkill.
988db8dac20SDavid S. Miller 	 */
989db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
990629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
991629ca23cSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
992db8dac20SDavid S. Miller 	}
993db8dac20SDavid S. Miller 	return err;
994db8dac20SDavid S. Miller 
995db8dac20SDavid S. Miller do_confirm:
996d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
997db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
998db8dac20SDavid S. Miller 		goto back_from_confirm;
999db8dac20SDavid S. Miller 	err = 0;
1000db8dac20SDavid S. Miller 	goto out;
1001db8dac20SDavid S. Miller }
1002c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg);
1003db8dac20SDavid S. Miller 
1004db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset,
1005db8dac20SDavid S. Miller 		 size_t size, int flags)
1006db8dac20SDavid S. Miller {
1007db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1008db8dac20SDavid S. Miller 	int ret;
1009db8dac20SDavid S. Miller 
1010db8dac20SDavid S. Miller 	if (!up->pending) {
1011db8dac20SDavid S. Miller 		struct msghdr msg = {	.msg_flags = flags|MSG_MORE };
1012db8dac20SDavid S. Miller 
1013db8dac20SDavid S. Miller 		/* Call udp_sendmsg to specify destination address which
1014db8dac20SDavid S. Miller 		 * sendpage interface can't pass.
1015db8dac20SDavid S. Miller 		 * This will succeed only when the socket is connected.
1016db8dac20SDavid S. Miller 		 */
1017db8dac20SDavid S. Miller 		ret = udp_sendmsg(NULL, sk, &msg, 0);
1018db8dac20SDavid S. Miller 		if (ret < 0)
1019db8dac20SDavid S. Miller 			return ret;
1020db8dac20SDavid S. Miller 	}
1021db8dac20SDavid S. Miller 
1022db8dac20SDavid S. Miller 	lock_sock(sk);
1023db8dac20SDavid S. Miller 
1024db8dac20SDavid S. Miller 	if (unlikely(!up->pending)) {
1025db8dac20SDavid S. Miller 		release_sock(sk);
1026db8dac20SDavid S. Miller 
1027db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
1028db8dac20SDavid S. Miller 		return -EINVAL;
1029db8dac20SDavid S. Miller 	}
1030db8dac20SDavid S. Miller 
1031db8dac20SDavid S. Miller 	ret = ip_append_page(sk, page, offset, size, flags);
1032db8dac20SDavid S. Miller 	if (ret == -EOPNOTSUPP) {
1033db8dac20SDavid S. Miller 		release_sock(sk);
1034db8dac20SDavid S. Miller 		return sock_no_sendpage(sk->sk_socket, page, offset,
1035db8dac20SDavid S. Miller 					size, flags);
1036db8dac20SDavid S. Miller 	}
1037db8dac20SDavid S. Miller 	if (ret < 0) {
1038db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
1039db8dac20SDavid S. Miller 		goto out;
1040db8dac20SDavid S. Miller 	}
1041db8dac20SDavid S. Miller 
1042db8dac20SDavid S. Miller 	up->len += size;
1043db8dac20SDavid S. Miller 	if (!(up->corkflag || (flags&MSG_MORE)))
1044db8dac20SDavid S. Miller 		ret = udp_push_pending_frames(sk);
1045db8dac20SDavid S. Miller 	if (!ret)
1046db8dac20SDavid S. Miller 		ret = size;
1047db8dac20SDavid S. Miller out:
1048db8dac20SDavid S. Miller 	release_sock(sk);
1049db8dac20SDavid S. Miller 	return ret;
1050db8dac20SDavid S. Miller }
1051db8dac20SDavid S. Miller 
105285584672SEric Dumazet 
105385584672SEric Dumazet /**
105485584672SEric Dumazet  *	first_packet_length	- return length of first packet in receive queue
105585584672SEric Dumazet  *	@sk: socket
105685584672SEric Dumazet  *
105785584672SEric Dumazet  *	Drops all bad checksum frames, until a valid one is found.
105885584672SEric Dumazet  *	Returns the length of found skb, or 0 if none is found.
105985584672SEric Dumazet  */
106085584672SEric Dumazet static unsigned int first_packet_length(struct sock *sk)
106185584672SEric Dumazet {
106285584672SEric Dumazet 	struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
106385584672SEric Dumazet 	struct sk_buff *skb;
106485584672SEric Dumazet 	unsigned int res;
106585584672SEric Dumazet 
106685584672SEric Dumazet 	__skb_queue_head_init(&list_kill);
106785584672SEric Dumazet 
106885584672SEric Dumazet 	spin_lock_bh(&rcvq->lock);
106985584672SEric Dumazet 	while ((skb = skb_peek(rcvq)) != NULL &&
107085584672SEric Dumazet 		udp_lib_checksum_complete(skb)) {
107185584672SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
107285584672SEric Dumazet 				 IS_UDPLITE(sk));
10738edf19c2SEric Dumazet 		atomic_inc(&sk->sk_drops);
107485584672SEric Dumazet 		__skb_unlink(skb, rcvq);
107585584672SEric Dumazet 		__skb_queue_tail(&list_kill, skb);
107685584672SEric Dumazet 	}
107785584672SEric Dumazet 	res = skb ? skb->len : 0;
107885584672SEric Dumazet 	spin_unlock_bh(&rcvq->lock);
107985584672SEric Dumazet 
108085584672SEric Dumazet 	if (!skb_queue_empty(&list_kill)) {
10818a74ad60SEric Dumazet 		bool slow = lock_sock_fast(sk);
10828a74ad60SEric Dumazet 
108385584672SEric Dumazet 		__skb_queue_purge(&list_kill);
108485584672SEric Dumazet 		sk_mem_reclaim_partial(sk);
10858a74ad60SEric Dumazet 		unlock_sock_fast(sk, slow);
108685584672SEric Dumazet 	}
108785584672SEric Dumazet 	return res;
108885584672SEric Dumazet }
108985584672SEric Dumazet 
10901da177e4SLinus Torvalds /*
10911da177e4SLinus Torvalds  *	IOCTL requests applicable to the UDP protocol
10921da177e4SLinus Torvalds  */
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
10951da177e4SLinus Torvalds {
10966516c655SStephen Hemminger 	switch (cmd) {
10971da177e4SLinus Torvalds 	case SIOCOUTQ:
10981da177e4SLinus Torvalds 	{
109931e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
110031e6d363SEric Dumazet 
11011da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	case SIOCINQ:
11051da177e4SLinus Torvalds 	{
110685584672SEric Dumazet 		unsigned int amount = first_packet_length(sk);
11071da177e4SLinus Torvalds 
110885584672SEric Dumazet 		if (amount)
11091da177e4SLinus Torvalds 			/*
11101da177e4SLinus Torvalds 			 * We will only return the amount
11111da177e4SLinus Torvalds 			 * of this packet since that is all
11121da177e4SLinus Torvalds 			 * that will be read.
11131da177e4SLinus Torvalds 			 */
111485584672SEric Dumazet 			amount -= sizeof(struct udphdr);
111585584672SEric Dumazet 
11161da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11171da177e4SLinus Torvalds 	}
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	default:
11201da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
11211da177e4SLinus Torvalds 	}
11226516c655SStephen Hemminger 
11236516c655SStephen Hemminger 	return 0;
11241da177e4SLinus Torvalds }
1125c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl);
11261da177e4SLinus Torvalds 
1127db8dac20SDavid S. Miller /*
1128db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
1129db8dac20SDavid S. Miller  * 	return it, otherwise we block.
1130db8dac20SDavid S. Miller  */
1131db8dac20SDavid S. Miller 
1132db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1133db8dac20SDavid S. Miller 		size_t len, int noblock, int flags, int *addr_len)
1134db8dac20SDavid S. Miller {
1135db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1136db8dac20SDavid S. Miller 	struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
1137db8dac20SDavid S. Miller 	struct sk_buff *skb;
113881d54ec8SGerrit Renker 	unsigned int ulen;
1139db8dac20SDavid S. Miller 	int peeked;
1140db8dac20SDavid S. Miller 	int err;
1141db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
11428a74ad60SEric Dumazet 	bool slow;
1143db8dac20SDavid S. Miller 
1144db8dac20SDavid S. Miller 	/*
1145db8dac20SDavid S. Miller 	 *	Check any passed addresses
1146db8dac20SDavid S. Miller 	 */
1147db8dac20SDavid S. Miller 	if (addr_len)
1148db8dac20SDavid S. Miller 		*addr_len = sizeof(*sin);
1149db8dac20SDavid S. Miller 
1150db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
1151db8dac20SDavid S. Miller 		return ip_recv_error(sk, msg, len);
1152db8dac20SDavid S. Miller 
1153db8dac20SDavid S. Miller try_again:
1154db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
1155db8dac20SDavid S. Miller 				  &peeked, &err);
1156db8dac20SDavid S. Miller 	if (!skb)
1157db8dac20SDavid S. Miller 		goto out;
1158db8dac20SDavid S. Miller 
1159db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
116081d54ec8SGerrit Renker 	if (len > ulen)
116181d54ec8SGerrit Renker 		len = ulen;
116281d54ec8SGerrit Renker 	else if (len < ulen)
1163db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
1164db8dac20SDavid S. Miller 
1165db8dac20SDavid S. Miller 	/*
1166db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
1167db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
1168db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
1169db8dac20SDavid S. Miller 	 */
1170db8dac20SDavid S. Miller 
117181d54ec8SGerrit Renker 	if (len < ulen || UDP_SKB_CB(skb)->partial_cov) {
1172db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
1173db8dac20SDavid S. Miller 			goto csum_copy_err;
1174db8dac20SDavid S. Miller 	}
1175db8dac20SDavid S. Miller 
1176db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
1177db8dac20SDavid S. Miller 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
117881d54ec8SGerrit Renker 					      msg->msg_iov, len);
1179db8dac20SDavid S. Miller 	else {
1180c482c568SEric Dumazet 		err = skb_copy_and_csum_datagram_iovec(skb,
1181c482c568SEric Dumazet 						       sizeof(struct udphdr),
1182c482c568SEric Dumazet 						       msg->msg_iov);
1183db8dac20SDavid S. Miller 
1184db8dac20SDavid S. Miller 		if (err == -EINVAL)
1185db8dac20SDavid S. Miller 			goto csum_copy_err;
1186db8dac20SDavid S. Miller 	}
1187db8dac20SDavid S. Miller 
1188db8dac20SDavid S. Miller 	if (err)
1189db8dac20SDavid S. Miller 		goto out_free;
1190db8dac20SDavid S. Miller 
1191db8dac20SDavid S. Miller 	if (!peeked)
1192629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1193629ca23cSPavel Emelyanov 				UDP_MIB_INDATAGRAMS, is_udplite);
1194db8dac20SDavid S. Miller 
11953b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
1196db8dac20SDavid S. Miller 
1197db8dac20SDavid S. Miller 	/* Copy the address. */
1198c482c568SEric Dumazet 	if (sin) {
1199db8dac20SDavid S. Miller 		sin->sin_family = AF_INET;
1200db8dac20SDavid S. Miller 		sin->sin_port = udp_hdr(skb)->source;
1201db8dac20SDavid S. Miller 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1202db8dac20SDavid S. Miller 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1203db8dac20SDavid S. Miller 	}
1204db8dac20SDavid S. Miller 	if (inet->cmsg_flags)
1205db8dac20SDavid S. Miller 		ip_cmsg_recv(msg, skb);
1206db8dac20SDavid S. Miller 
120781d54ec8SGerrit Renker 	err = len;
1208db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
1209db8dac20SDavid S. Miller 		err = ulen;
1210db8dac20SDavid S. Miller 
1211db8dac20SDavid S. Miller out_free:
12129d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
1213db8dac20SDavid S. Miller out:
1214db8dac20SDavid S. Miller 	return err;
1215db8dac20SDavid S. Miller 
1216db8dac20SDavid S. Miller csum_copy_err:
12178a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
1218db8dac20SDavid S. Miller 	if (!skb_kill_datagram(sk, skb, flags))
1219629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
12208a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
1221db8dac20SDavid S. Miller 
1222db8dac20SDavid S. Miller 	if (noblock)
1223db8dac20SDavid S. Miller 		return -EAGAIN;
1224db8dac20SDavid S. Miller 	goto try_again;
1225db8dac20SDavid S. Miller }
1226db8dac20SDavid S. Miller 
1227db8dac20SDavid S. Miller 
12281da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags)
12291da177e4SLinus Torvalds {
12301da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
12311da177e4SLinus Torvalds 	/*
12321da177e4SLinus Torvalds 	 *	1003.1g - break association.
12331da177e4SLinus Torvalds 	 */
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
1236c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
1237c720c7e8SEric Dumazet 	inet->inet_dport = 0;
1238c58dc01bSDavid S. Miller 	sock_rps_save_rxhash(sk, 0);
12391da177e4SLinus Torvalds 	sk->sk_bound_dev_if = 0;
12401da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
12411da177e4SLinus Torvalds 		inet_reset_saddr(sk);
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
12441da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1245c720c7e8SEric Dumazet 		inet->inet_sport = 0;
12461da177e4SLinus Torvalds 	}
12471da177e4SLinus Torvalds 	sk_dst_reset(sk);
12481da177e4SLinus Torvalds 	return 0;
12491da177e4SLinus Torvalds }
1250c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect);
12511da177e4SLinus Torvalds 
1252645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk)
1253645ca708SEric Dumazet {
1254723b4610SEric Dumazet 	if (sk_hashed(sk)) {
1255645ca708SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1256512615b6SEric Dumazet 		struct udp_hslot *hslot, *hslot2;
1257512615b6SEric Dumazet 
1258512615b6SEric Dumazet 		hslot  = udp_hashslot(udptable, sock_net(sk),
1259d4cada4aSEric Dumazet 				      udp_sk(sk)->udp_port_hash);
1260512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1261645ca708SEric Dumazet 
1262c8db3fecSEric Dumazet 		spin_lock_bh(&hslot->lock);
126388ab1932SEric Dumazet 		if (sk_nulls_del_node_init_rcu(sk)) {
1264fdcc8aa9SEric Dumazet 			hslot->count--;
1265c720c7e8SEric Dumazet 			inet_sk(sk)->inet_num = 0;
1266645ca708SEric Dumazet 			sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1267512615b6SEric Dumazet 
1268512615b6SEric Dumazet 			spin_lock(&hslot2->lock);
1269512615b6SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1270512615b6SEric Dumazet 			hslot2->count--;
1271512615b6SEric Dumazet 			spin_unlock(&hslot2->lock);
1272645ca708SEric Dumazet 		}
1273c8db3fecSEric Dumazet 		spin_unlock_bh(&hslot->lock);
1274645ca708SEric Dumazet 	}
1275723b4610SEric Dumazet }
1276645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash);
1277645ca708SEric Dumazet 
1278719f8358SEric Dumazet /*
1279719f8358SEric Dumazet  * inet_rcv_saddr was changed, we must rehash secondary hash
1280719f8358SEric Dumazet  */
1281719f8358SEric Dumazet void udp_lib_rehash(struct sock *sk, u16 newhash)
1282719f8358SEric Dumazet {
1283719f8358SEric Dumazet 	if (sk_hashed(sk)) {
1284719f8358SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1285719f8358SEric Dumazet 		struct udp_hslot *hslot, *hslot2, *nhslot2;
1286719f8358SEric Dumazet 
1287719f8358SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1288719f8358SEric Dumazet 		nhslot2 = udp_hashslot2(udptable, newhash);
1289719f8358SEric Dumazet 		udp_sk(sk)->udp_portaddr_hash = newhash;
1290719f8358SEric Dumazet 		if (hslot2 != nhslot2) {
1291719f8358SEric Dumazet 			hslot = udp_hashslot(udptable, sock_net(sk),
1292719f8358SEric Dumazet 					     udp_sk(sk)->udp_port_hash);
1293719f8358SEric Dumazet 			/* we must lock primary chain too */
1294719f8358SEric Dumazet 			spin_lock_bh(&hslot->lock);
1295719f8358SEric Dumazet 
1296719f8358SEric Dumazet 			spin_lock(&hslot2->lock);
1297719f8358SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1298719f8358SEric Dumazet 			hslot2->count--;
1299719f8358SEric Dumazet 			spin_unlock(&hslot2->lock);
1300719f8358SEric Dumazet 
1301719f8358SEric Dumazet 			spin_lock(&nhslot2->lock);
1302719f8358SEric Dumazet 			hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
1303719f8358SEric Dumazet 						 &nhslot2->head);
1304719f8358SEric Dumazet 			nhslot2->count++;
1305719f8358SEric Dumazet 			spin_unlock(&nhslot2->lock);
1306719f8358SEric Dumazet 
1307719f8358SEric Dumazet 			spin_unlock_bh(&hslot->lock);
1308719f8358SEric Dumazet 		}
1309719f8358SEric Dumazet 	}
1310719f8358SEric Dumazet }
1311719f8358SEric Dumazet EXPORT_SYMBOL(udp_lib_rehash);
1312719f8358SEric Dumazet 
1313719f8358SEric Dumazet static void udp_v4_rehash(struct sock *sk)
1314719f8358SEric Dumazet {
1315719f8358SEric Dumazet 	u16 new_hash = udp4_portaddr_hash(sock_net(sk),
1316719f8358SEric Dumazet 					  inet_sk(sk)->inet_rcv_saddr,
1317719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
1318719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
1319719f8358SEric Dumazet }
1320719f8358SEric Dumazet 
132193821778SHerbert Xu static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
132293821778SHerbert Xu {
1323fec5e652STom Herbert 	int rc;
132493821778SHerbert Xu 
1325fec5e652STom Herbert 	if (inet_sk(sk)->inet_daddr)
1326c58dc01bSDavid S. Miller 		sock_rps_save_rxhash(sk, skb->rxhash);
1327fec5e652STom Herbert 
1328f84af32cSEric Dumazet 	rc = ip_queue_rcv_skb(sk, skb);
1329766e9037SEric Dumazet 	if (rc < 0) {
1330766e9037SEric Dumazet 		int is_udplite = IS_UDPLITE(sk);
1331766e9037SEric Dumazet 
133293821778SHerbert Xu 		/* Note that an ENOMEM error is charged twice */
1333766e9037SEric Dumazet 		if (rc == -ENOMEM)
133493821778SHerbert Xu 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
133593821778SHerbert Xu 					 is_udplite);
1336766e9037SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1337766e9037SEric Dumazet 		kfree_skb(skb);
1338766e9037SEric Dumazet 		return -1;
133993821778SHerbert Xu 	}
134093821778SHerbert Xu 
134193821778SHerbert Xu 	return 0;
134293821778SHerbert Xu 
134393821778SHerbert Xu }
134493821778SHerbert Xu 
1345db8dac20SDavid S. Miller /* returns:
1346db8dac20SDavid S. Miller  *  -1: error
1347db8dac20SDavid S. Miller  *   0: success
1348db8dac20SDavid S. Miller  *  >0: "udp encap" protocol resubmission
1349db8dac20SDavid S. Miller  *
1350db8dac20SDavid S. Miller  * Note that in the success and error cases, the skb is assumed to
1351db8dac20SDavid S. Miller  * have either been requeued or freed.
1352db8dac20SDavid S. Miller  */
1353db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1354db8dac20SDavid S. Miller {
1355db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1356db8dac20SDavid S. Miller 	int rc;
1357db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1358db8dac20SDavid S. Miller 
1359db8dac20SDavid S. Miller 	/*
1360db8dac20SDavid S. Miller 	 *	Charge it to the socket, dropping if the queue is full.
1361db8dac20SDavid S. Miller 	 */
1362db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1363db8dac20SDavid S. Miller 		goto drop;
1364db8dac20SDavid S. Miller 	nf_reset(skb);
1365db8dac20SDavid S. Miller 
1366db8dac20SDavid S. Miller 	if (up->encap_type) {
1367db8dac20SDavid S. Miller 		/*
1368db8dac20SDavid S. Miller 		 * This is an encapsulation socket so pass the skb to
1369db8dac20SDavid S. Miller 		 * the socket's udp_encap_rcv() hook. Otherwise, just
1370db8dac20SDavid S. Miller 		 * fall through and pass this up the UDP socket.
1371db8dac20SDavid S. Miller 		 * up->encap_rcv() returns the following value:
1372db8dac20SDavid S. Miller 		 * =0 if skb was successfully passed to the encap
1373db8dac20SDavid S. Miller 		 *    handler or was discarded by it.
1374db8dac20SDavid S. Miller 		 * >0 if skb should be passed on to UDP.
1375db8dac20SDavid S. Miller 		 * <0 if skb should be resubmitted as proto -N
1376db8dac20SDavid S. Miller 		 */
1377db8dac20SDavid S. Miller 
1378db8dac20SDavid S. Miller 		/* if we're overly short, let UDP handle it */
1379db8dac20SDavid S. Miller 		if (skb->len > sizeof(struct udphdr) &&
1380db8dac20SDavid S. Miller 		    up->encap_rcv != NULL) {
1381db8dac20SDavid S. Miller 			int ret;
1382db8dac20SDavid S. Miller 
1383db8dac20SDavid S. Miller 			ret = (*up->encap_rcv)(sk, skb);
1384db8dac20SDavid S. Miller 			if (ret <= 0) {
13850283328eSPavel Emelyanov 				UDP_INC_STATS_BH(sock_net(sk),
13860283328eSPavel Emelyanov 						 UDP_MIB_INDATAGRAMS,
1387db8dac20SDavid S. Miller 						 is_udplite);
1388db8dac20SDavid S. Miller 				return -ret;
1389db8dac20SDavid S. Miller 			}
1390db8dac20SDavid S. Miller 		}
1391db8dac20SDavid S. Miller 
1392db8dac20SDavid S. Miller 		/* FALLTHROUGH -- it's a UDP Packet */
1393db8dac20SDavid S. Miller 	}
1394db8dac20SDavid S. Miller 
1395db8dac20SDavid S. Miller 	/*
1396db8dac20SDavid S. Miller 	 * 	UDP-Lite specific tests, ignored on UDP sockets
1397db8dac20SDavid S. Miller 	 */
1398db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
1399db8dac20SDavid S. Miller 
1400db8dac20SDavid S. Miller 		/*
1401db8dac20SDavid S. Miller 		 * MIB statistics other than incrementing the error count are
1402db8dac20SDavid S. Miller 		 * disabled for the following two types of errors: these depend
1403db8dac20SDavid S. Miller 		 * on the application settings, not on the functioning of the
1404db8dac20SDavid S. Miller 		 * protocol stack as such.
1405db8dac20SDavid S. Miller 		 *
1406db8dac20SDavid S. Miller 		 * RFC 3828 here recommends (sec 3.3): "There should also be a
1407db8dac20SDavid S. Miller 		 * way ... to ... at least let the receiving application block
1408db8dac20SDavid S. Miller 		 * delivery of packets with coverage values less than a value
1409db8dac20SDavid S. Miller 		 * provided by the application."
1410db8dac20SDavid S. Miller 		 */
1411db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
1412db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
1413db8dac20SDavid S. Miller 				"%d while full coverage %d requested\n",
1414db8dac20SDavid S. Miller 				UDP_SKB_CB(skb)->cscov, skb->len);
1415db8dac20SDavid S. Miller 			goto drop;
1416db8dac20SDavid S. Miller 		}
1417db8dac20SDavid S. Miller 		/* The next case involves violating the min. coverage requested
1418db8dac20SDavid S. Miller 		 * by the receiver. This is subtle: if receiver wants x and x is
1419db8dac20SDavid S. Miller 		 * greater than the buffersize/MTU then receiver will complain
1420db8dac20SDavid S. Miller 		 * that it wants x while sender emits packets of smaller size y.
1421db8dac20SDavid S. Miller 		 * Therefore the above ...()->partial_cov statement is essential.
1422db8dac20SDavid S. Miller 		 */
1423db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
1424db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING
1425db8dac20SDavid S. Miller 				"UDPLITE: coverage %d too small, need min %d\n",
1426db8dac20SDavid S. Miller 				UDP_SKB_CB(skb)->cscov, up->pcrlen);
1427db8dac20SDavid S. Miller 			goto drop;
1428db8dac20SDavid S. Miller 		}
1429db8dac20SDavid S. Miller 	}
1430db8dac20SDavid S. Miller 
14310d7da9ddSEric Dumazet 	if (rcu_dereference_raw(sk->sk_filter)) {
1432db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
1433db8dac20SDavid S. Miller 			goto drop;
1434db8dac20SDavid S. Miller 	}
1435db8dac20SDavid S. Miller 
1436c377411fSEric Dumazet 
1437c377411fSEric Dumazet 	if (sk_rcvqueues_full(sk, skb))
1438c377411fSEric Dumazet 		goto drop;
1439c377411fSEric Dumazet 
144093821778SHerbert Xu 	rc = 0;
1441db8dac20SDavid S. Miller 
144293821778SHerbert Xu 	bh_lock_sock(sk);
144393821778SHerbert Xu 	if (!sock_owned_by_user(sk))
144493821778SHerbert Xu 		rc = __udp_queue_rcv_skb(sk, skb);
1445a3a858ffSZhu Yi 	else if (sk_add_backlog(sk, skb)) {
144655349790SZhu Yi 		bh_unlock_sock(sk);
144755349790SZhu Yi 		goto drop;
144855349790SZhu Yi 	}
144993821778SHerbert Xu 	bh_unlock_sock(sk);
145093821778SHerbert Xu 
145193821778SHerbert Xu 	return rc;
1452db8dac20SDavid S. Miller 
1453db8dac20SDavid S. Miller drop:
14540283328eSPavel Emelyanov 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
14558edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
1456db8dac20SDavid S. Miller 	kfree_skb(skb);
1457db8dac20SDavid S. Miller 	return -1;
1458db8dac20SDavid S. Miller }
1459db8dac20SDavid S. Miller 
14601240d137SEric Dumazet 
14611240d137SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
14621240d137SEric Dumazet 			struct sk_buff *skb, unsigned int final)
14631240d137SEric Dumazet {
14641240d137SEric Dumazet 	unsigned int i;
14651240d137SEric Dumazet 	struct sk_buff *skb1 = NULL;
1466f6b8f32cSEric Dumazet 	struct sock *sk;
14671240d137SEric Dumazet 
14681240d137SEric Dumazet 	for (i = 0; i < count; i++) {
1469f6b8f32cSEric Dumazet 		sk = stack[i];
14701240d137SEric Dumazet 		if (likely(skb1 == NULL))
14711240d137SEric Dumazet 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
14721240d137SEric Dumazet 
1473f6b8f32cSEric Dumazet 		if (!skb1) {
1474f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
1475f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
1476f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1477f6b8f32cSEric Dumazet 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
1478f6b8f32cSEric Dumazet 					 IS_UDPLITE(sk));
1479f6b8f32cSEric Dumazet 		}
1480f6b8f32cSEric Dumazet 
1481f6b8f32cSEric Dumazet 		if (skb1 && udp_queue_rcv_skb(sk, skb1) <= 0)
14821240d137SEric Dumazet 			skb1 = NULL;
14831240d137SEric Dumazet 	}
14841240d137SEric Dumazet 	if (unlikely(skb1))
14851240d137SEric Dumazet 		kfree_skb(skb1);
14861240d137SEric Dumazet }
14871240d137SEric Dumazet 
1488db8dac20SDavid S. Miller /*
1489db8dac20SDavid S. Miller  *	Multicasts and broadcasts go to each listener.
1490db8dac20SDavid S. Miller  *
14911240d137SEric Dumazet  *	Note: called only from the BH handler context.
1492db8dac20SDavid S. Miller  */
1493e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
1494db8dac20SDavid S. Miller 				    struct udphdr  *uh,
1495db8dac20SDavid S. Miller 				    __be32 saddr, __be32 daddr,
1496645ca708SEric Dumazet 				    struct udp_table *udptable)
1497db8dac20SDavid S. Miller {
14981240d137SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
1499f86dcc5aSEric Dumazet 	struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
1500db8dac20SDavid S. Miller 	int dif;
15011240d137SEric Dumazet 	unsigned int i, count = 0;
1502db8dac20SDavid S. Miller 
1503645ca708SEric Dumazet 	spin_lock(&hslot->lock);
150488ab1932SEric Dumazet 	sk = sk_nulls_head(&hslot->head);
1505db8dac20SDavid S. Miller 	dif = skb->dev->ifindex;
1506920a4611SEric Dumazet 	sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
15071240d137SEric Dumazet 	while (sk) {
15081240d137SEric Dumazet 		stack[count++] = sk;
15091240d137SEric Dumazet 		sk = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
15101240d137SEric Dumazet 				       daddr, uh->source, saddr, dif);
15111240d137SEric Dumazet 		if (unlikely(count == ARRAY_SIZE(stack))) {
15121240d137SEric Dumazet 			if (!sk)
15131240d137SEric Dumazet 				break;
15141240d137SEric Dumazet 			flush_stack(stack, count, skb, ~0);
15151240d137SEric Dumazet 			count = 0;
1516db8dac20SDavid S. Miller 		}
15171240d137SEric Dumazet 	}
15181240d137SEric Dumazet 	/*
15191240d137SEric Dumazet 	 * before releasing chain lock, we must take a reference on sockets
15201240d137SEric Dumazet 	 */
15211240d137SEric Dumazet 	for (i = 0; i < count; i++)
15221240d137SEric Dumazet 		sock_hold(stack[i]);
15231240d137SEric Dumazet 
1524645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
15251240d137SEric Dumazet 
15261240d137SEric Dumazet 	/*
15271240d137SEric Dumazet 	 * do the slow work with no lock held
15281240d137SEric Dumazet 	 */
15291240d137SEric Dumazet 	if (count) {
15301240d137SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
15311240d137SEric Dumazet 
15321240d137SEric Dumazet 		for (i = 0; i < count; i++)
15331240d137SEric Dumazet 			sock_put(stack[i]);
15341240d137SEric Dumazet 	} else {
15351240d137SEric Dumazet 		kfree_skb(skb);
15361240d137SEric Dumazet 	}
1537db8dac20SDavid S. Miller 	return 0;
1538db8dac20SDavid S. Miller }
1539db8dac20SDavid S. Miller 
1540db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success),
1541db8dac20SDavid S. Miller  * CHECKSUM_UNNECESSARY means, that no more checks are required.
1542db8dac20SDavid S. Miller  * Otherwise, csum completion requires chacksumming packet body,
1543db8dac20SDavid S. Miller  * including udp header and folding it to skb->csum.
1544db8dac20SDavid S. Miller  */
1545db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1546db8dac20SDavid S. Miller 				 int proto)
1547db8dac20SDavid S. Miller {
1548db8dac20SDavid S. Miller 	const struct iphdr *iph;
1549db8dac20SDavid S. Miller 	int err;
1550db8dac20SDavid S. Miller 
1551db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
1552db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
1553db8dac20SDavid S. Miller 
1554db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
1555db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
1556db8dac20SDavid S. Miller 		if (err)
1557db8dac20SDavid S. Miller 			return err;
1558db8dac20SDavid S. Miller 	}
1559db8dac20SDavid S. Miller 
1560db8dac20SDavid S. Miller 	iph = ip_hdr(skb);
1561db8dac20SDavid S. Miller 	if (uh->check == 0) {
1562db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1563db8dac20SDavid S. Miller 	} else if (skb->ip_summed == CHECKSUM_COMPLETE) {
1564db8dac20SDavid S. Miller 		if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
1565db8dac20SDavid S. Miller 				      proto, skb->csum))
1566db8dac20SDavid S. Miller 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1567db8dac20SDavid S. Miller 	}
1568db8dac20SDavid S. Miller 	if (!skb_csum_unnecessary(skb))
1569db8dac20SDavid S. Miller 		skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
1570db8dac20SDavid S. Miller 					       skb->len, proto, 0);
1571db8dac20SDavid S. Miller 	/* Probably, we should checksum udp header (it should be in cache
1572db8dac20SDavid S. Miller 	 * in any case) and data in tiny packets (< rx copybreak).
1573db8dac20SDavid S. Miller 	 */
1574db8dac20SDavid S. Miller 
1575db8dac20SDavid S. Miller 	return 0;
1576db8dac20SDavid S. Miller }
1577db8dac20SDavid S. Miller 
1578db8dac20SDavid S. Miller /*
1579db8dac20SDavid S. Miller  *	All we need to do is get the socket, and then do a checksum.
1580db8dac20SDavid S. Miller  */
1581db8dac20SDavid S. Miller 
1582645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1583db8dac20SDavid S. Miller 		   int proto)
1584db8dac20SDavid S. Miller {
1585db8dac20SDavid S. Miller 	struct sock *sk;
15867b5e56f9SJesper Dangaard Brouer 	struct udphdr *uh;
1587db8dac20SDavid S. Miller 	unsigned short ulen;
1588adf30907SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
15892783ef23SJesper Dangaard Brouer 	__be32 saddr, daddr;
15900283328eSPavel Emelyanov 	struct net *net = dev_net(skb->dev);
1591db8dac20SDavid S. Miller 
1592db8dac20SDavid S. Miller 	/*
1593db8dac20SDavid S. Miller 	 *  Validate the packet.
1594db8dac20SDavid S. Miller 	 */
1595db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1596db8dac20SDavid S. Miller 		goto drop;		/* No space for header. */
1597db8dac20SDavid S. Miller 
15987b5e56f9SJesper Dangaard Brouer 	uh   = udp_hdr(skb);
1599db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
1600ccc2d97cSBjørn Mork 	saddr = ip_hdr(skb)->saddr;
1601ccc2d97cSBjørn Mork 	daddr = ip_hdr(skb)->daddr;
1602ccc2d97cSBjørn Mork 
1603db8dac20SDavid S. Miller 	if (ulen > skb->len)
1604db8dac20SDavid S. Miller 		goto short_packet;
1605db8dac20SDavid S. Miller 
1606db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
1607db8dac20SDavid S. Miller 		/* UDP validates ulen. */
1608db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1609db8dac20SDavid S. Miller 			goto short_packet;
1610db8dac20SDavid S. Miller 		uh = udp_hdr(skb);
1611db8dac20SDavid S. Miller 	}
1612db8dac20SDavid S. Miller 
1613db8dac20SDavid S. Miller 	if (udp4_csum_init(skb, uh, proto))
1614db8dac20SDavid S. Miller 		goto csum_error;
1615db8dac20SDavid S. Miller 
1616db8dac20SDavid S. Miller 	if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1617e3163493SPavel Emelyanov 		return __udp4_lib_mcast_deliver(net, skb, uh,
1618e3163493SPavel Emelyanov 				saddr, daddr, udptable);
1619db8dac20SDavid S. Miller 
1620607c4aafSKOVACS Krisztian 	sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1621db8dac20SDavid S. Miller 
1622db8dac20SDavid S. Miller 	if (sk != NULL) {
162393821778SHerbert Xu 		int ret = udp_queue_rcv_skb(sk, skb);
1624db8dac20SDavid S. Miller 		sock_put(sk);
1625db8dac20SDavid S. Miller 
1626db8dac20SDavid S. Miller 		/* a return value > 0 means to resubmit the input, but
1627db8dac20SDavid S. Miller 		 * it wants the return to be -protocol, or 0
1628db8dac20SDavid S. Miller 		 */
1629db8dac20SDavid S. Miller 		if (ret > 0)
1630db8dac20SDavid S. Miller 			return -ret;
1631db8dac20SDavid S. Miller 		return 0;
1632db8dac20SDavid S. Miller 	}
1633db8dac20SDavid S. Miller 
1634db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1635db8dac20SDavid S. Miller 		goto drop;
1636db8dac20SDavid S. Miller 	nf_reset(skb);
1637db8dac20SDavid S. Miller 
1638db8dac20SDavid S. Miller 	/* No socket. Drop packet silently, if checksum is wrong */
1639db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
1640db8dac20SDavid S. Miller 		goto csum_error;
1641db8dac20SDavid S. Miller 
16420283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
1643db8dac20SDavid S. Miller 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1644db8dac20SDavid S. Miller 
1645db8dac20SDavid S. Miller 	/*
1646db8dac20SDavid S. Miller 	 * Hmm.  We got an UDP packet to a port to which we
1647db8dac20SDavid S. Miller 	 * don't wanna listen.  Ignore it.
1648db8dac20SDavid S. Miller 	 */
1649db8dac20SDavid S. Miller 	kfree_skb(skb);
1650db8dac20SDavid S. Miller 	return 0;
1651db8dac20SDavid S. Miller 
1652db8dac20SDavid S. Miller short_packet:
1653673d57e7SHarvey Harrison 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
1654db8dac20SDavid S. Miller 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
1655673d57e7SHarvey Harrison 		       &saddr,
1656db8dac20SDavid S. Miller 		       ntohs(uh->source),
1657db8dac20SDavid S. Miller 		       ulen,
1658db8dac20SDavid S. Miller 		       skb->len,
1659673d57e7SHarvey Harrison 		       &daddr,
1660db8dac20SDavid S. Miller 		       ntohs(uh->dest));
1661db8dac20SDavid S. Miller 	goto drop;
1662db8dac20SDavid S. Miller 
1663db8dac20SDavid S. Miller csum_error:
1664db8dac20SDavid S. Miller 	/*
1665db8dac20SDavid S. Miller 	 * RFC1122: OK.  Discards the bad packet silently (as far as
1666db8dac20SDavid S. Miller 	 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1667db8dac20SDavid S. Miller 	 */
1668673d57e7SHarvey Harrison 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
1669db8dac20SDavid S. Miller 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
1670673d57e7SHarvey Harrison 		       &saddr,
1671db8dac20SDavid S. Miller 		       ntohs(uh->source),
1672673d57e7SHarvey Harrison 		       &daddr,
1673db8dac20SDavid S. Miller 		       ntohs(uh->dest),
1674db8dac20SDavid S. Miller 		       ulen);
1675db8dac20SDavid S. Miller drop:
16760283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1677db8dac20SDavid S. Miller 	kfree_skb(skb);
1678db8dac20SDavid S. Miller 	return 0;
1679db8dac20SDavid S. Miller }
1680db8dac20SDavid S. Miller 
1681db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb)
1682db8dac20SDavid S. Miller {
1683645ca708SEric Dumazet 	return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1684db8dac20SDavid S. Miller }
1685db8dac20SDavid S. Miller 
16867d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk)
1687db8dac20SDavid S. Miller {
16888a74ad60SEric Dumazet 	bool slow = lock_sock_fast(sk);
1689db8dac20SDavid S. Miller 	udp_flush_pending_frames(sk);
16908a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
1691db8dac20SDavid S. Miller }
1692db8dac20SDavid S. Miller 
16931da177e4SLinus Torvalds /*
16941da177e4SLinus Torvalds  *	Socket option code for UDP
16951da177e4SLinus Torvalds  */
16964c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname,
1697b7058842SDavid S. Miller 		       char __user *optval, unsigned int optlen,
16984c0a6cb0SGerrit Renker 		       int (*push_pending_frames)(struct sock *))
16991da177e4SLinus Torvalds {
17001da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
17011da177e4SLinus Torvalds 	int val;
17021da177e4SLinus Torvalds 	int err = 0;
1703b2bf1e26SWang Chen 	int is_udplite = IS_UDPLITE(sk);
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	if (optlen < sizeof(int))
17061da177e4SLinus Torvalds 		return -EINVAL;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
17091da177e4SLinus Torvalds 		return -EFAULT;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 	switch (optname) {
17121da177e4SLinus Torvalds 	case UDP_CORK:
17131da177e4SLinus Torvalds 		if (val != 0) {
17141da177e4SLinus Torvalds 			up->corkflag = 1;
17151da177e4SLinus Torvalds 		} else {
17161da177e4SLinus Torvalds 			up->corkflag = 0;
17171da177e4SLinus Torvalds 			lock_sock(sk);
17184c0a6cb0SGerrit Renker 			(*push_pending_frames)(sk);
17191da177e4SLinus Torvalds 			release_sock(sk);
17201da177e4SLinus Torvalds 		}
17211da177e4SLinus Torvalds 		break;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	case UDP_ENCAP:
17241da177e4SLinus Torvalds 		switch (val) {
17251da177e4SLinus Torvalds 		case 0:
17261da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP:
17271da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP_NON_IKE:
1728067b207bSJames Chapman 			up->encap_rcv = xfrm4_udp_encap_rcv;
1729067b207bSJames Chapman 			/* FALLTHROUGH */
1730342f0234SJames Chapman 		case UDP_ENCAP_L2TPINUDP:
17311da177e4SLinus Torvalds 			up->encap_type = val;
17321da177e4SLinus Torvalds 			break;
17331da177e4SLinus Torvalds 		default:
17341da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
17351da177e4SLinus Torvalds 			break;
17361da177e4SLinus Torvalds 		}
17371da177e4SLinus Torvalds 		break;
17381da177e4SLinus Torvalds 
1739ba4e58ecSGerrit Renker 	/*
1740ba4e58ecSGerrit Renker 	 * 	UDP-Lite's partial checksum coverage (RFC 3828).
1741ba4e58ecSGerrit Renker 	 */
1742ba4e58ecSGerrit Renker 	/* The sender sets actual checksum coverage length via this option.
1743ba4e58ecSGerrit Renker 	 * The case coverage > packet length is handled by send module. */
1744ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
1745b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
1746ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
1747ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
1748ba4e58ecSGerrit Renker 			val = 8;
17494be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
17504be929beSAlexey Dobriyan 			val = USHRT_MAX;
1751ba4e58ecSGerrit Renker 		up->pcslen = val;
1752ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_SEND_CC;
1753ba4e58ecSGerrit Renker 		break;
1754ba4e58ecSGerrit Renker 
1755ba4e58ecSGerrit Renker 	/* The receiver specifies a minimum checksum coverage value. To make
1756ba4e58ecSGerrit Renker 	 * sense, this should be set to at least 8 (as done below). If zero is
1757ba4e58ecSGerrit Renker 	 * used, this again means full checksum coverage.                     */
1758ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
1759b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
1760ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
1761ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Avoid silly minimal values.       */
1762ba4e58ecSGerrit Renker 			val = 8;
17634be929beSAlexey Dobriyan 		else if (val > USHRT_MAX)
17644be929beSAlexey Dobriyan 			val = USHRT_MAX;
1765ba4e58ecSGerrit Renker 		up->pcrlen = val;
1766ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_RECV_CC;
1767ba4e58ecSGerrit Renker 		break;
1768ba4e58ecSGerrit Renker 
17691da177e4SLinus Torvalds 	default:
17701da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
17711da177e4SLinus Torvalds 		break;
17726516c655SStephen Hemminger 	}
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 	return err;
17751da177e4SLinus Torvalds }
1776c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt);
17771da177e4SLinus Torvalds 
1778db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname,
1779b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
1780db8dac20SDavid S. Miller {
1781db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1782db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1783db8dac20SDavid S. Miller 					  udp_push_pending_frames);
1784db8dac20SDavid S. Miller 	return ip_setsockopt(sk, level, optname, optval, optlen);
1785db8dac20SDavid S. Miller }
1786db8dac20SDavid S. Miller 
1787db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1788db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1789b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
1790db8dac20SDavid S. Miller {
1791db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1792db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1793db8dac20SDavid S. Miller 					  udp_push_pending_frames);
1794db8dac20SDavid S. Miller 	return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1795db8dac20SDavid S. Miller }
1796db8dac20SDavid S. Miller #endif
1797db8dac20SDavid S. Miller 
17984c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname,
17991da177e4SLinus Torvalds 		       char __user *optval, int __user *optlen)
18001da177e4SLinus Torvalds {
18011da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
18021da177e4SLinus Torvalds 	int val, len;
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 	if (get_user(len, optlen))
18051da177e4SLinus Torvalds 		return -EFAULT;
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	if (len < 0)
18101da177e4SLinus Torvalds 		return -EINVAL;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	switch (optname) {
18131da177e4SLinus Torvalds 	case UDP_CORK:
18141da177e4SLinus Torvalds 		val = up->corkflag;
18151da177e4SLinus Torvalds 		break;
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	case UDP_ENCAP:
18181da177e4SLinus Torvalds 		val = up->encap_type;
18191da177e4SLinus Torvalds 		break;
18201da177e4SLinus Torvalds 
1821ba4e58ecSGerrit Renker 	/* The following two cannot be changed on UDP sockets, the return is
1822ba4e58ecSGerrit Renker 	 * always 0 (which corresponds to the full checksum coverage of UDP). */
1823ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
1824ba4e58ecSGerrit Renker 		val = up->pcslen;
1825ba4e58ecSGerrit Renker 		break;
1826ba4e58ecSGerrit Renker 
1827ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
1828ba4e58ecSGerrit Renker 		val = up->pcrlen;
1829ba4e58ecSGerrit Renker 		break;
1830ba4e58ecSGerrit Renker 
18311da177e4SLinus Torvalds 	default:
18321da177e4SLinus Torvalds 		return -ENOPROTOOPT;
18336516c655SStephen Hemminger 	}
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	if (put_user(len, optlen))
18361da177e4SLinus Torvalds 		return -EFAULT;
18371da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
18381da177e4SLinus Torvalds 		return -EFAULT;
18391da177e4SLinus Torvalds 	return 0;
18401da177e4SLinus Torvalds }
1841c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt);
18421da177e4SLinus Torvalds 
1843db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname,
1844db8dac20SDavid S. Miller 		   char __user *optval, int __user *optlen)
1845db8dac20SDavid S. Miller {
1846db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1847db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1848db8dac20SDavid S. Miller 	return ip_getsockopt(sk, level, optname, optval, optlen);
1849db8dac20SDavid S. Miller }
1850db8dac20SDavid S. Miller 
1851db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1852db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1853db8dac20SDavid S. Miller 				 char __user *optval, int __user *optlen)
1854db8dac20SDavid S. Miller {
1855db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1856db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1857db8dac20SDavid S. Miller 	return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1858db8dac20SDavid S. Miller }
1859db8dac20SDavid S. Miller #endif
18601da177e4SLinus Torvalds /**
18611da177e4SLinus Torvalds  * 	udp_poll - wait for a UDP event.
18621da177e4SLinus Torvalds  *	@file - file struct
18631da177e4SLinus Torvalds  *	@sock - socket
18641da177e4SLinus Torvalds  *	@wait - poll table
18651da177e4SLinus Torvalds  *
18661da177e4SLinus Torvalds  *	This is same as datagram poll, except for the special case of
18671da177e4SLinus Torvalds  *	blocking sockets. If application is using a blocking fd
18681da177e4SLinus Torvalds  *	and a packet with checksum error is in the queue;
18691da177e4SLinus Torvalds  *	then it could get return from select indicating data available
18701da177e4SLinus Torvalds  *	but then block when reading it. Add special case code
18711da177e4SLinus Torvalds  *	to work around these arguably broken applications.
18721da177e4SLinus Torvalds  */
18731da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
18741da177e4SLinus Torvalds {
18751da177e4SLinus Torvalds 	unsigned int mask = datagram_poll(file, sock, wait);
18761da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 	/* Check for false positives due to checksum errors */
187985584672SEric Dumazet 	if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
188085584672SEric Dumazet 	    !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
18811da177e4SLinus Torvalds 		mask &= ~(POLLIN | POLLRDNORM);
18821da177e4SLinus Torvalds 
18831da177e4SLinus Torvalds 	return mask;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds }
1886c482c568SEric Dumazet EXPORT_SYMBOL(udp_poll);
18871da177e4SLinus Torvalds 
1888db8dac20SDavid S. Miller struct proto udp_prot = {
1889db8dac20SDavid S. Miller 	.name		   = "UDP",
1890db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1891db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1892db8dac20SDavid S. Miller 	.connect	   = ip4_datagram_connect,
1893db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1894db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1895db8dac20SDavid S. Miller 	.destroy	   = udp_destroy_sock,
1896db8dac20SDavid S. Miller 	.setsockopt	   = udp_setsockopt,
1897db8dac20SDavid S. Miller 	.getsockopt	   = udp_getsockopt,
1898db8dac20SDavid S. Miller 	.sendmsg	   = udp_sendmsg,
1899db8dac20SDavid S. Miller 	.recvmsg	   = udp_recvmsg,
1900db8dac20SDavid S. Miller 	.sendpage	   = udp_sendpage,
190193821778SHerbert Xu 	.backlog_rcv	   = __udp_queue_rcv_skb,
1902db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1903db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1904719f8358SEric Dumazet 	.rehash		   = udp_v4_rehash,
1905db8dac20SDavid S. Miller 	.get_port	   = udp_v4_get_port,
1906db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1907db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1908db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1909db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1910db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp_sock),
1911271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1912645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1913db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1914db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udp_setsockopt,
1915db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udp_getsockopt,
1916db8dac20SDavid S. Miller #endif
1917fcbdf09dSOctavian Purdila 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
1918db8dac20SDavid S. Miller };
1919c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot);
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
19221da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
19231da177e4SLinus Torvalds 
1924645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start)
19251da177e4SLinus Torvalds {
19261da177e4SLinus Torvalds 	struct sock *sk;
19271da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
19286f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
19291da177e4SLinus Torvalds 
1930f86dcc5aSEric Dumazet 	for (state->bucket = start; state->bucket <= state->udp_table->mask;
1931f86dcc5aSEric Dumazet 	     ++state->bucket) {
193288ab1932SEric Dumazet 		struct hlist_nulls_node *node;
1933645ca708SEric Dumazet 		struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
1934f86dcc5aSEric Dumazet 
1935f86dcc5aSEric Dumazet 		if (hlist_nulls_empty(&hslot->head))
1936f86dcc5aSEric Dumazet 			continue;
1937f86dcc5aSEric Dumazet 
1938645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
193988ab1932SEric Dumazet 		sk_nulls_for_each(sk, node, &hslot->head) {
1940878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
1941a91275efSDaniel Lezcano 				continue;
19421da177e4SLinus Torvalds 			if (sk->sk_family == state->family)
19431da177e4SLinus Torvalds 				goto found;
19441da177e4SLinus Torvalds 		}
1945645ca708SEric Dumazet 		spin_unlock_bh(&hslot->lock);
19461da177e4SLinus Torvalds 	}
19471da177e4SLinus Torvalds 	sk = NULL;
19481da177e4SLinus Torvalds found:
19491da177e4SLinus Torvalds 	return sk;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
19531da177e4SLinus Torvalds {
19541da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
19556f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds 	do {
195888ab1932SEric Dumazet 		sk = sk_nulls_next(sk);
1959878628fbSYOSHIFUJI Hideaki 	} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
19601da177e4SLinus Torvalds 
1961645ca708SEric Dumazet 	if (!sk) {
1962f86dcc5aSEric Dumazet 		if (state->bucket <= state->udp_table->mask)
1963f52b5054SEric Dumazet 			spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
1964645ca708SEric Dumazet 		return udp_get_first(seq, state->bucket + 1);
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds 	return sk;
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
19701da177e4SLinus Torvalds {
1971645ca708SEric Dumazet 	struct sock *sk = udp_get_first(seq, 0);
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	if (sk)
19741da177e4SLinus Torvalds 		while (pos && (sk = udp_get_next(seq, sk)) != NULL)
19751da177e4SLinus Torvalds 			--pos;
19761da177e4SLinus Torvalds 	return pos ? NULL : sk;
19771da177e4SLinus Torvalds }
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
19801da177e4SLinus Torvalds {
198130842f29SVitaly Mayatskikh 	struct udp_iter_state *state = seq->private;
1982f86dcc5aSEric Dumazet 	state->bucket = MAX_UDP_PORTS;
198330842f29SVitaly Mayatskikh 
1984b50660f1SYOSHIFUJI Hideaki 	return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
19851da177e4SLinus Torvalds }
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
19881da177e4SLinus Torvalds {
19891da177e4SLinus Torvalds 	struct sock *sk;
19901da177e4SLinus Torvalds 
1991b50660f1SYOSHIFUJI Hideaki 	if (v == SEQ_START_TOKEN)
19921da177e4SLinus Torvalds 		sk = udp_get_idx(seq, 0);
19931da177e4SLinus Torvalds 	else
19941da177e4SLinus Torvalds 		sk = udp_get_next(seq, v);
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	++*pos;
19971da177e4SLinus Torvalds 	return sk;
19981da177e4SLinus Torvalds }
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v)
20011da177e4SLinus Torvalds {
2002645ca708SEric Dumazet 	struct udp_iter_state *state = seq->private;
2003645ca708SEric Dumazet 
2004f86dcc5aSEric Dumazet 	if (state->bucket <= state->udp_table->mask)
2005645ca708SEric Dumazet 		spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
20061da177e4SLinus Torvalds }
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file)
20091da177e4SLinus Torvalds {
20101da177e4SLinus Torvalds 	struct udp_seq_afinfo *afinfo = PDE(inode)->data;
2011a2be75c1SDenis V. Lunev 	struct udp_iter_state *s;
2012a2be75c1SDenis V. Lunev 	int err;
20131da177e4SLinus Torvalds 
2014a2be75c1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
2015a2be75c1SDenis V. Lunev 			   sizeof(struct udp_iter_state));
2016a2be75c1SDenis V. Lunev 	if (err < 0)
2017a2be75c1SDenis V. Lunev 		return err;
2018a91275efSDaniel Lezcano 
2019a2be75c1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
20201da177e4SLinus Torvalds 	s->family		= afinfo->family;
2021645ca708SEric Dumazet 	s->udp_table		= afinfo->udp_table;
2022a2be75c1SDenis V. Lunev 	return err;
2023a91275efSDaniel Lezcano }
2024a91275efSDaniel Lezcano 
20251da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
20260c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
20271da177e4SLinus Torvalds {
20281da177e4SLinus Torvalds 	struct proc_dir_entry *p;
20291da177e4SLinus Torvalds 	int rc = 0;
20301da177e4SLinus Torvalds 
20313ba9441bSDenis V. Lunev 	afinfo->seq_fops.open		= udp_seq_open;
20323ba9441bSDenis V. Lunev 	afinfo->seq_fops.read		= seq_read;
20333ba9441bSDenis V. Lunev 	afinfo->seq_fops.llseek		= seq_lseek;
20343ba9441bSDenis V. Lunev 	afinfo->seq_fops.release	= seq_release_net;
20351da177e4SLinus Torvalds 
2036dda61925SDenis V. Lunev 	afinfo->seq_ops.start		= udp_seq_start;
2037dda61925SDenis V. Lunev 	afinfo->seq_ops.next		= udp_seq_next;
2038dda61925SDenis V. Lunev 	afinfo->seq_ops.stop		= udp_seq_stop;
2039dda61925SDenis V. Lunev 
204084841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
204184841c3cSDenis V. Lunev 			     &afinfo->seq_fops, afinfo);
204284841c3cSDenis V. Lunev 	if (!p)
20431da177e4SLinus Torvalds 		rc = -ENOMEM;
20441da177e4SLinus Torvalds 	return rc;
20451da177e4SLinus Torvalds }
2046c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_register);
20471da177e4SLinus Torvalds 
20480c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
20491da177e4SLinus Torvalds {
20500c96d8c5SDaniel Lezcano 	proc_net_remove(net, afinfo->name);
20511da177e4SLinus Torvalds }
2052c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_unregister);
2053db8dac20SDavid S. Miller 
2054db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
20555e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f,
20565e659e4cSPavel Emelyanov 		int bucket, int *len)
2057db8dac20SDavid S. Miller {
2058db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
2059c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
2060c720c7e8SEric Dumazet 	__be32 src  = inet->inet_rcv_saddr;
2061c720c7e8SEric Dumazet 	__u16 destp	  = ntohs(inet->inet_dport);
2062c720c7e8SEric Dumazet 	__u16 srcp	  = ntohs(inet->inet_sport);
2063db8dac20SDavid S. Miller 
2064f86dcc5aSEric Dumazet 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2065cb61cb9bSEric Dumazet 		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
2066db8dac20SDavid S. Miller 		bucket, src, srcp, dest, destp, sp->sk_state,
206731e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
206831e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
2069db8dac20SDavid S. Miller 		0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
2070cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_refcnt), sp,
2071cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_drops), len);
2072db8dac20SDavid S. Miller }
2073db8dac20SDavid S. Miller 
2074db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v)
2075db8dac20SDavid S. Miller {
2076db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
2077db8dac20SDavid S. Miller 		seq_printf(seq, "%-127s\n",
2078db8dac20SDavid S. Miller 			   "  sl  local_address rem_address   st tx_queue "
2079db8dac20SDavid S. Miller 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
2080cb61cb9bSEric Dumazet 			   "inode ref pointer drops");
2081db8dac20SDavid S. Miller 	else {
2082db8dac20SDavid S. Miller 		struct udp_iter_state *state = seq->private;
20835e659e4cSPavel Emelyanov 		int len;
2084db8dac20SDavid S. Miller 
20855e659e4cSPavel Emelyanov 		udp4_format_sock(v, seq, state->bucket, &len);
20865e659e4cSPavel Emelyanov 		seq_printf(seq, "%*s\n", 127 - len, "");
2087db8dac20SDavid S. Miller 	}
2088db8dac20SDavid S. Miller 	return 0;
2089db8dac20SDavid S. Miller }
2090db8dac20SDavid S. Miller 
2091db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
2092db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = {
2093db8dac20SDavid S. Miller 	.name		= "udp",
2094db8dac20SDavid S. Miller 	.family		= AF_INET,
2095645ca708SEric Dumazet 	.udp_table	= &udp_table,
20964ad96d39SDenis V. Lunev 	.seq_fops	= {
20974ad96d39SDenis V. Lunev 		.owner	=	THIS_MODULE,
20984ad96d39SDenis V. Lunev 	},
2099dda61925SDenis V. Lunev 	.seq_ops	= {
2100dda61925SDenis V. Lunev 		.show		= udp4_seq_show,
2101dda61925SDenis V. Lunev 	},
2102db8dac20SDavid S. Miller };
2103db8dac20SDavid S. Miller 
21042c8c1e72SAlexey Dobriyan static int __net_init udp4_proc_init_net(struct net *net)
210515439febSPavel Emelyanov {
210615439febSPavel Emelyanov 	return udp_proc_register(net, &udp4_seq_afinfo);
210715439febSPavel Emelyanov }
210815439febSPavel Emelyanov 
21092c8c1e72SAlexey Dobriyan static void __net_exit udp4_proc_exit_net(struct net *net)
211015439febSPavel Emelyanov {
211115439febSPavel Emelyanov 	udp_proc_unregister(net, &udp4_seq_afinfo);
211215439febSPavel Emelyanov }
211315439febSPavel Emelyanov 
211415439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = {
211515439febSPavel Emelyanov 	.init = udp4_proc_init_net,
211615439febSPavel Emelyanov 	.exit = udp4_proc_exit_net,
211715439febSPavel Emelyanov };
211815439febSPavel Emelyanov 
2119db8dac20SDavid S. Miller int __init udp4_proc_init(void)
2120db8dac20SDavid S. Miller {
212115439febSPavel Emelyanov 	return register_pernet_subsys(&udp4_net_ops);
2122db8dac20SDavid S. Miller }
2123db8dac20SDavid S. Miller 
2124db8dac20SDavid S. Miller void udp4_proc_exit(void)
2125db8dac20SDavid S. Miller {
212615439febSPavel Emelyanov 	unregister_pernet_subsys(&udp4_net_ops);
2127db8dac20SDavid S. Miller }
21281da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
21291da177e4SLinus Torvalds 
2130f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries;
2131f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str)
2132645ca708SEric Dumazet {
2133f86dcc5aSEric Dumazet 	if (!str)
2134f86dcc5aSEric Dumazet 		return 0;
2135f86dcc5aSEric Dumazet 	uhash_entries = simple_strtoul(str, &str, 0);
2136f86dcc5aSEric Dumazet 	if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
2137f86dcc5aSEric Dumazet 		uhash_entries = UDP_HTABLE_SIZE_MIN;
2138f86dcc5aSEric Dumazet 	return 1;
2139f86dcc5aSEric Dumazet }
2140f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries);
2141645ca708SEric Dumazet 
2142f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name)
2143f86dcc5aSEric Dumazet {
2144f86dcc5aSEric Dumazet 	unsigned int i;
2145f86dcc5aSEric Dumazet 
2146f86dcc5aSEric Dumazet 	if (!CONFIG_BASE_SMALL)
2147f86dcc5aSEric Dumazet 		table->hash = alloc_large_system_hash(name,
2148512615b6SEric Dumazet 			2 * sizeof(struct udp_hslot),
2149f86dcc5aSEric Dumazet 			uhash_entries,
2150f86dcc5aSEric Dumazet 			21, /* one slot per 2 MB */
2151f86dcc5aSEric Dumazet 			0,
2152f86dcc5aSEric Dumazet 			&table->log,
2153f86dcc5aSEric Dumazet 			&table->mask,
2154f86dcc5aSEric Dumazet 			64 * 1024);
2155f86dcc5aSEric Dumazet 	/*
2156f86dcc5aSEric Dumazet 	 * Make sure hash table has the minimum size
2157f86dcc5aSEric Dumazet 	 */
2158f86dcc5aSEric Dumazet 	if (CONFIG_BASE_SMALL || table->mask < UDP_HTABLE_SIZE_MIN - 1) {
2159f86dcc5aSEric Dumazet 		table->hash = kmalloc(UDP_HTABLE_SIZE_MIN *
2160512615b6SEric Dumazet 				      2 * sizeof(struct udp_hslot), GFP_KERNEL);
2161f86dcc5aSEric Dumazet 		if (!table->hash)
2162f86dcc5aSEric Dumazet 			panic(name);
2163f86dcc5aSEric Dumazet 		table->log = ilog2(UDP_HTABLE_SIZE_MIN);
2164f86dcc5aSEric Dumazet 		table->mask = UDP_HTABLE_SIZE_MIN - 1;
2165f86dcc5aSEric Dumazet 	}
2166512615b6SEric Dumazet 	table->hash2 = table->hash + (table->mask + 1);
2167f86dcc5aSEric Dumazet 	for (i = 0; i <= table->mask; i++) {
216888ab1932SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
2169fdcc8aa9SEric Dumazet 		table->hash[i].count = 0;
2170645ca708SEric Dumazet 		spin_lock_init(&table->hash[i].lock);
2171645ca708SEric Dumazet 	}
2172512615b6SEric Dumazet 	for (i = 0; i <= table->mask; i++) {
2173512615b6SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
2174512615b6SEric Dumazet 		table->hash2[i].count = 0;
2175512615b6SEric Dumazet 		spin_lock_init(&table->hash2[i].lock);
2176512615b6SEric Dumazet 	}
2177645ca708SEric Dumazet }
2178645ca708SEric Dumazet 
217995766fffSHideo Aoki void __init udp_init(void)
218095766fffSHideo Aoki {
21818203efb3SEric Dumazet 	unsigned long nr_pages, limit;
218295766fffSHideo Aoki 
2183f86dcc5aSEric Dumazet 	udp_table_init(&udp_table, "UDP");
218495766fffSHideo Aoki 	/* Set the pressure threshold up by the same strategy of TCP. It is a
218595766fffSHideo Aoki 	 * fraction of global memory that is up to 1/2 at 256 MB, decreasing
218695766fffSHideo Aoki 	 * toward zero with the amount of memory, with a floor of 128 pages.
218795766fffSHideo Aoki 	 */
21888203efb3SEric Dumazet 	nr_pages = totalram_pages - totalhigh_pages;
21898203efb3SEric Dumazet 	limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
21908203efb3SEric Dumazet 	limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
219195766fffSHideo Aoki 	limit = max(limit, 128UL);
219295766fffSHideo Aoki 	sysctl_udp_mem[0] = limit / 4 * 3;
219395766fffSHideo Aoki 	sysctl_udp_mem[1] = limit;
219495766fffSHideo Aoki 	sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
219595766fffSHideo Aoki 
219695766fffSHideo Aoki 	sysctl_udp_rmem_min = SK_MEM_QUANTUM;
219795766fffSHideo Aoki 	sysctl_udp_wmem_min = SK_MEM_QUANTUM;
219895766fffSHideo Aoki }
219995766fffSHideo Aoki 
2200d7ca4cc0SSridhar Samudrala int udp4_ufo_send_check(struct sk_buff *skb)
2201d7ca4cc0SSridhar Samudrala {
2202d7ca4cc0SSridhar Samudrala 	const struct iphdr *iph;
2203d7ca4cc0SSridhar Samudrala 	struct udphdr *uh;
2204d7ca4cc0SSridhar Samudrala 
2205d7ca4cc0SSridhar Samudrala 	if (!pskb_may_pull(skb, sizeof(*uh)))
2206d7ca4cc0SSridhar Samudrala 		return -EINVAL;
2207d7ca4cc0SSridhar Samudrala 
2208d7ca4cc0SSridhar Samudrala 	iph = ip_hdr(skb);
2209d7ca4cc0SSridhar Samudrala 	uh = udp_hdr(skb);
2210d7ca4cc0SSridhar Samudrala 
2211d7ca4cc0SSridhar Samudrala 	uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
2212d7ca4cc0SSridhar Samudrala 				       IPPROTO_UDP, 0);
2213d7ca4cc0SSridhar Samudrala 	skb->csum_start = skb_transport_header(skb) - skb->head;
2214d7ca4cc0SSridhar Samudrala 	skb->csum_offset = offsetof(struct udphdr, check);
2215d7ca4cc0SSridhar Samudrala 	skb->ip_summed = CHECKSUM_PARTIAL;
2216d7ca4cc0SSridhar Samudrala 	return 0;
2217d7ca4cc0SSridhar Samudrala }
2218d7ca4cc0SSridhar Samudrala 
221904ed3e74SMichał Mirosław struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, u32 features)
2220d7ca4cc0SSridhar Samudrala {
2221d7ca4cc0SSridhar Samudrala 	struct sk_buff *segs = ERR_PTR(-EINVAL);
2222d7ca4cc0SSridhar Samudrala 	unsigned int mss;
2223d7ca4cc0SSridhar Samudrala 	int offset;
2224d7ca4cc0SSridhar Samudrala 	__wsum csum;
2225d7ca4cc0SSridhar Samudrala 
2226d7ca4cc0SSridhar Samudrala 	mss = skb_shinfo(skb)->gso_size;
2227d7ca4cc0SSridhar Samudrala 	if (unlikely(skb->len <= mss))
2228d7ca4cc0SSridhar Samudrala 		goto out;
2229d7ca4cc0SSridhar Samudrala 
2230d7ca4cc0SSridhar Samudrala 	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
2231d7ca4cc0SSridhar Samudrala 		/* Packet is from an untrusted source, reset gso_segs. */
2232d7ca4cc0SSridhar Samudrala 		int type = skb_shinfo(skb)->gso_type;
2233d7ca4cc0SSridhar Samudrala 
2234d7ca4cc0SSridhar Samudrala 		if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
2235d7ca4cc0SSridhar Samudrala 			     !(type & (SKB_GSO_UDP))))
2236d7ca4cc0SSridhar Samudrala 			goto out;
2237d7ca4cc0SSridhar Samudrala 
2238d7ca4cc0SSridhar Samudrala 		skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
2239d7ca4cc0SSridhar Samudrala 
2240d7ca4cc0SSridhar Samudrala 		segs = NULL;
2241d7ca4cc0SSridhar Samudrala 		goto out;
2242d7ca4cc0SSridhar Samudrala 	}
2243d7ca4cc0SSridhar Samudrala 
2244d7ca4cc0SSridhar Samudrala 	/* Do software UFO. Complete and fill in the UDP checksum as HW cannot
2245d7ca4cc0SSridhar Samudrala 	 * do checksum of UDP packets sent as multiple IP fragments.
2246d7ca4cc0SSridhar Samudrala 	 */
224755508d60SMichał Mirosław 	offset = skb_checksum_start_offset(skb);
2248d7ca4cc0SSridhar Samudrala 	csum = skb_checksum(skb, offset, skb->len - offset, 0);
2249d7ca4cc0SSridhar Samudrala 	offset += skb->csum_offset;
2250d7ca4cc0SSridhar Samudrala 	*(__sum16 *)(skb->data + offset) = csum_fold(csum);
2251d7ca4cc0SSridhar Samudrala 	skb->ip_summed = CHECKSUM_NONE;
2252d7ca4cc0SSridhar Samudrala 
2253d7ca4cc0SSridhar Samudrala 	/* Fragment the skb. IP headers of the fragments are updated in
2254d7ca4cc0SSridhar Samudrala 	 * inet_gso_segment()
2255d7ca4cc0SSridhar Samudrala 	 */
2256d7ca4cc0SSridhar Samudrala 	segs = skb_segment(skb, features);
2257d7ca4cc0SSridhar Samudrala out:
2258d7ca4cc0SSridhar Samudrala 	return segs;
2259d7ca4cc0SSridhar Samudrala }
2260d7ca4cc0SSridhar Samudrala 
2261