xref: /linux/net/ipv4/udp.c (revision 5051ebd275de672b807c28d93002c2fb0514a3c9)
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>
98c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
991da177e4SLinus Torvalds #include <linux/skbuff.h>
1001da177e4SLinus Torvalds #include <linux/proc_fs.h>
1011da177e4SLinus Torvalds #include <linux/seq_file.h>
102457c4cbcSEric W. Biederman #include <net/net_namespace.h>
1031da177e4SLinus Torvalds #include <net/icmp.h>
1041da177e4SLinus Torvalds #include <net/route.h>
1051da177e4SLinus Torvalds #include <net/checksum.h>
1061da177e4SLinus Torvalds #include <net/xfrm.h>
107ba4e58ecSGerrit Renker #include "udp_impl.h"
1081da177e4SLinus Torvalds 
109f86dcc5aSEric Dumazet struct udp_table udp_table __read_mostly;
110645ca708SEric Dumazet EXPORT_SYMBOL(udp_table);
1111da177e4SLinus Torvalds 
11295766fffSHideo Aoki int sysctl_udp_mem[3] __read_mostly;
11395766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_mem);
114c482c568SEric Dumazet 
115c482c568SEric Dumazet int sysctl_udp_rmem_min __read_mostly;
11695766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_rmem_min);
117c482c568SEric Dumazet 
118c482c568SEric Dumazet int sysctl_udp_wmem_min __read_mostly;
11995766fffSHideo Aoki EXPORT_SYMBOL(sysctl_udp_wmem_min);
12095766fffSHideo Aoki 
12195766fffSHideo Aoki atomic_t udp_memory_allocated;
12295766fffSHideo Aoki EXPORT_SYMBOL(udp_memory_allocated);
12395766fffSHideo Aoki 
124f86dcc5aSEric Dumazet #define MAX_UDP_PORTS 65536
125f86dcc5aSEric Dumazet #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
12698322f22SEric Dumazet 
127f24d43c0SEric Dumazet static int udp_lib_lport_inuse(struct net *net, __u16 num,
128645ca708SEric Dumazet 			       const struct udp_hslot *hslot,
12998322f22SEric Dumazet 			       unsigned long *bitmap,
130f24d43c0SEric Dumazet 			       struct sock *sk,
131f24d43c0SEric Dumazet 			       int (*saddr_comp)(const struct sock *sk1,
132f86dcc5aSEric Dumazet 						 const struct sock *sk2),
133f86dcc5aSEric Dumazet 			       unsigned int log)
13425030a7fSGerrit Renker {
135f24d43c0SEric Dumazet 	struct sock *sk2;
13688ab1932SEric Dumazet 	struct hlist_nulls_node *node;
13725030a7fSGerrit Renker 
13888ab1932SEric Dumazet 	sk_nulls_for_each(sk2, node, &hslot->head)
139f24d43c0SEric Dumazet 		if (net_eq(sock_net(sk2), net)			&&
140f24d43c0SEric Dumazet 		    sk2 != sk					&&
141d4cada4aSEric Dumazet 		    (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
142f24d43c0SEric Dumazet 		    (!sk2->sk_reuse || !sk->sk_reuse)		&&
143f24d43c0SEric Dumazet 		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
144f24d43c0SEric Dumazet 			|| sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
14598322f22SEric Dumazet 		    (*saddr_comp)(sk, sk2)) {
14698322f22SEric Dumazet 			if (bitmap)
147d4cada4aSEric Dumazet 				__set_bit(udp_sk(sk2)->udp_port_hash >> log,
148d4cada4aSEric Dumazet 					  bitmap);
14998322f22SEric Dumazet 			else
150fc038410SDavid S. Miller 				return 1;
15198322f22SEric Dumazet 		}
15225030a7fSGerrit Renker 	return 0;
15325030a7fSGerrit Renker }
15425030a7fSGerrit Renker 
15525030a7fSGerrit Renker /**
1566ba5a3c5SPavel Emelyanov  *  udp_lib_get_port  -  UDP/-Lite port lookup for IPv4 and IPv6
15725030a7fSGerrit Renker  *
15825030a7fSGerrit Renker  *  @sk:          socket struct in question
15925030a7fSGerrit Renker  *  @snum:        port number to look up
160df2bc459SDavid S. Miller  *  @saddr_comp:  AF-dependent comparison of bound local IP addresses
16125030a7fSGerrit Renker  */
1626ba5a3c5SPavel Emelyanov int udp_lib_get_port(struct sock *sk, unsigned short snum,
163df2bc459SDavid S. Miller 		       int (*saddr_comp)(const struct sock *sk1,
164df2bc459SDavid S. Miller 					 const struct sock *sk2))
1651da177e4SLinus Torvalds {
166512615b6SEric Dumazet 	struct udp_hslot *hslot, *hslot2;
167645ca708SEric Dumazet 	struct udp_table *udptable = sk->sk_prot->h.udp_table;
16825030a7fSGerrit Renker 	int    error = 1;
1693b1e0a65SYOSHIFUJI Hideaki 	struct net *net = sock_net(sk);
1701da177e4SLinus Torvalds 
17132c1da70SStephen Hemminger 	if (!snum) {
1729088c560SEric Dumazet 		int low, high, remaining;
1739088c560SEric Dumazet 		unsigned rand;
17498322f22SEric Dumazet 		unsigned short first, last;
17598322f22SEric Dumazet 		DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
1761da177e4SLinus Torvalds 
177227b60f5SStephen Hemminger 		inet_get_local_port_range(&low, &high);
178a25de534SAnton Arapov 		remaining = (high - low) + 1;
179227b60f5SStephen Hemminger 
1809088c560SEric Dumazet 		rand = net_random();
18198322f22SEric Dumazet 		first = (((u64)rand * remaining) >> 32) + low;
18298322f22SEric Dumazet 		/*
18398322f22SEric Dumazet 		 * force rand to be an odd multiple of UDP_HTABLE_SIZE
18498322f22SEric Dumazet 		 */
185f86dcc5aSEric Dumazet 		rand = (rand | 1) * (udptable->mask + 1);
186f86dcc5aSEric Dumazet 		for (last = first + udptable->mask + 1;
187f86dcc5aSEric Dumazet 		     first != last;
188f86dcc5aSEric Dumazet 		     first++) {
189f86dcc5aSEric Dumazet 			hslot = udp_hashslot(udptable, net, first);
19098322f22SEric Dumazet 			bitmap_zero(bitmap, PORTS_PER_CHAIN);
191645ca708SEric Dumazet 			spin_lock_bh(&hslot->lock);
19298322f22SEric Dumazet 			udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
193f86dcc5aSEric Dumazet 					    saddr_comp, udptable->log);
19498322f22SEric Dumazet 
19598322f22SEric Dumazet 			snum = first;
19698322f22SEric Dumazet 			/*
19798322f22SEric Dumazet 			 * Iterate on all possible values of snum for this hash.
19898322f22SEric Dumazet 			 * Using steps of an odd multiple of UDP_HTABLE_SIZE
19998322f22SEric Dumazet 			 * give us randomization and full range coverage.
20098322f22SEric Dumazet 			 */
2019088c560SEric Dumazet 			do {
20298322f22SEric Dumazet 				if (low <= snum && snum <= high &&
203f86dcc5aSEric Dumazet 				    !test_bit(snum >> udptable->log, bitmap))
20498322f22SEric Dumazet 					goto found;
20598322f22SEric Dumazet 				snum += rand;
20698322f22SEric Dumazet 			} while (snum != first);
20798322f22SEric Dumazet 			spin_unlock_bh(&hslot->lock);
2089088c560SEric Dumazet 		}
20998322f22SEric Dumazet 		goto fail;
210645ca708SEric Dumazet 	} else {
211f86dcc5aSEric Dumazet 		hslot = udp_hashslot(udptable, net, snum);
212645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
213f86dcc5aSEric Dumazet 		if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk,
214f86dcc5aSEric Dumazet 					saddr_comp, 0))
215645ca708SEric Dumazet 			goto fail_unlock;
216645ca708SEric Dumazet 	}
21798322f22SEric Dumazet found:
218c720c7e8SEric Dumazet 	inet_sk(sk)->inet_num = snum;
219d4cada4aSEric Dumazet 	udp_sk(sk)->udp_port_hash = snum;
220d4cada4aSEric Dumazet 	udp_sk(sk)->udp_portaddr_hash ^= snum;
2211da177e4SLinus Torvalds 	if (sk_unhashed(sk)) {
22288ab1932SEric Dumazet 		sk_nulls_add_node_rcu(sk, &hslot->head);
223fdcc8aa9SEric Dumazet 		hslot->count++;
224c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
225512615b6SEric Dumazet 
226512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
227512615b6SEric Dumazet 		spin_lock(&hslot2->lock);
228512615b6SEric Dumazet 		hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
229512615b6SEric Dumazet 					 &hslot2->head);
230512615b6SEric Dumazet 		hslot2->count++;
231512615b6SEric Dumazet 		spin_unlock(&hslot2->lock);
2321da177e4SLinus Torvalds 	}
23325030a7fSGerrit Renker 	error = 0;
234645ca708SEric Dumazet fail_unlock:
235645ca708SEric Dumazet 	spin_unlock_bh(&hslot->lock);
2361da177e4SLinus Torvalds fail:
23725030a7fSGerrit Renker 	return error;
2381da177e4SLinus Torvalds }
239c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_get_port);
2401da177e4SLinus Torvalds 
241499923c7SVlad Yasevich static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
242db8dac20SDavid S. Miller {
243db8dac20SDavid S. Miller 	struct inet_sock *inet1 = inet_sk(sk1), *inet2 = inet_sk(sk2);
244db8dac20SDavid S. Miller 
245db8dac20SDavid S. Miller 	return 	(!ipv6_only_sock(sk2)  &&
246c720c7e8SEric Dumazet 		 (!inet1->inet_rcv_saddr || !inet2->inet_rcv_saddr ||
247c720c7e8SEric Dumazet 		   inet1->inet_rcv_saddr == inet2->inet_rcv_saddr));
248db8dac20SDavid S. Miller }
249db8dac20SDavid S. Miller 
250d4cada4aSEric Dumazet static unsigned int udp4_portaddr_hash(struct net *net, __be32 saddr,
251d4cada4aSEric Dumazet 				       unsigned int port)
252d4cada4aSEric Dumazet {
253d4cada4aSEric Dumazet 	return jhash_1word(saddr, net_hash_mix(net)) ^ port;
254d4cada4aSEric Dumazet }
255d4cada4aSEric Dumazet 
2566ba5a3c5SPavel Emelyanov int udp_v4_get_port(struct sock *sk, unsigned short snum)
257db8dac20SDavid S. Miller {
258d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
259d4cada4aSEric Dumazet 	udp_sk(sk)->udp_portaddr_hash =
260d4cada4aSEric Dumazet 		udp4_portaddr_hash(sock_net(sk),
261d4cada4aSEric Dumazet 				   inet_sk(sk)->inet_rcv_saddr,
262d4cada4aSEric Dumazet 				   0);
2636ba5a3c5SPavel Emelyanov 	return udp_lib_get_port(sk, snum, ipv4_rcv_saddr_equal);
264db8dac20SDavid S. Miller }
265db8dac20SDavid S. Miller 
266645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net, __be32 saddr,
267645ca708SEric Dumazet 			 unsigned short hnum,
268645ca708SEric Dumazet 			 __be16 sport, __be32 daddr, __be16 dport, int dif)
269645ca708SEric Dumazet {
270645ca708SEric Dumazet 	int score = -1;
271645ca708SEric Dumazet 
272d4cada4aSEric Dumazet 	if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
273645ca708SEric Dumazet 			!ipv6_only_sock(sk)) {
274645ca708SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
275645ca708SEric Dumazet 
276645ca708SEric Dumazet 		score = (sk->sk_family == PF_INET ? 1 : 0);
277c720c7e8SEric Dumazet 		if (inet->inet_rcv_saddr) {
278c720c7e8SEric Dumazet 			if (inet->inet_rcv_saddr != daddr)
279645ca708SEric Dumazet 				return -1;
280645ca708SEric Dumazet 			score += 2;
281645ca708SEric Dumazet 		}
282c720c7e8SEric Dumazet 		if (inet->inet_daddr) {
283c720c7e8SEric Dumazet 			if (inet->inet_daddr != saddr)
284645ca708SEric Dumazet 				return -1;
285645ca708SEric Dumazet 			score += 2;
286645ca708SEric Dumazet 		}
287c720c7e8SEric Dumazet 		if (inet->inet_dport) {
288c720c7e8SEric Dumazet 			if (inet->inet_dport != sport)
289645ca708SEric Dumazet 				return -1;
290645ca708SEric Dumazet 			score += 2;
291645ca708SEric Dumazet 		}
292645ca708SEric Dumazet 		if (sk->sk_bound_dev_if) {
293645ca708SEric Dumazet 			if (sk->sk_bound_dev_if != dif)
294645ca708SEric Dumazet 				return -1;
295645ca708SEric Dumazet 			score += 2;
296645ca708SEric Dumazet 		}
297645ca708SEric Dumazet 	}
298645ca708SEric Dumazet 	return score;
299645ca708SEric Dumazet }
300645ca708SEric Dumazet 
301*5051ebd2SEric Dumazet /*
302*5051ebd2SEric Dumazet  * In this second variant, we check (daddr, dport) matches (inet_rcv_sadd, inet_num)
303*5051ebd2SEric Dumazet  */
304*5051ebd2SEric Dumazet #define SCORE2_MAX (1 + 2 + 2 + 2)
305*5051ebd2SEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
306*5051ebd2SEric Dumazet 				 __be32 saddr, __be16 sport,
307*5051ebd2SEric Dumazet 				 __be32 daddr, unsigned int hnum, int dif)
308*5051ebd2SEric Dumazet {
309*5051ebd2SEric Dumazet 	int score = -1;
310*5051ebd2SEric Dumazet 
311*5051ebd2SEric Dumazet 	if (net_eq(sock_net(sk), net) && !ipv6_only_sock(sk)) {
312*5051ebd2SEric Dumazet 		struct inet_sock *inet = inet_sk(sk);
313*5051ebd2SEric Dumazet 
314*5051ebd2SEric Dumazet 		if (inet->inet_rcv_saddr != daddr)
315*5051ebd2SEric Dumazet 			return -1;
316*5051ebd2SEric Dumazet 		if (inet->inet_num != hnum)
317*5051ebd2SEric Dumazet 			return -1;
318*5051ebd2SEric Dumazet 
319*5051ebd2SEric Dumazet 		score = (sk->sk_family == PF_INET ? 1 : 0);
320*5051ebd2SEric Dumazet 		if (inet->inet_daddr) {
321*5051ebd2SEric Dumazet 			if (inet->inet_daddr != saddr)
322*5051ebd2SEric Dumazet 				return -1;
323*5051ebd2SEric Dumazet 			score += 2;
324*5051ebd2SEric Dumazet 		}
325*5051ebd2SEric Dumazet 		if (inet->inet_dport) {
326*5051ebd2SEric Dumazet 			if (inet->inet_dport != sport)
327*5051ebd2SEric Dumazet 				return -1;
328*5051ebd2SEric Dumazet 			score += 2;
329*5051ebd2SEric Dumazet 		}
330*5051ebd2SEric Dumazet 		if (sk->sk_bound_dev_if) {
331*5051ebd2SEric Dumazet 			if (sk->sk_bound_dev_if != dif)
332*5051ebd2SEric Dumazet 				return -1;
333*5051ebd2SEric Dumazet 			score += 2;
334*5051ebd2SEric Dumazet 		}
335*5051ebd2SEric Dumazet 	}
336*5051ebd2SEric Dumazet 	return score;
337*5051ebd2SEric Dumazet }
338*5051ebd2SEric Dumazet 
339*5051ebd2SEric Dumazet #define udp_portaddr_for_each_entry_rcu(__sk, node, list) \
340*5051ebd2SEric Dumazet 	hlist_nulls_for_each_entry_rcu(__sk, node, list, __sk_common.skc_portaddr_node)
341*5051ebd2SEric Dumazet 
342*5051ebd2SEric Dumazet /* called with read_rcu_lock() */
343*5051ebd2SEric Dumazet static struct sock *udp4_lib_lookup2(struct net *net,
344*5051ebd2SEric Dumazet 		__be32 saddr, __be16 sport,
345*5051ebd2SEric Dumazet 		__be32 daddr, unsigned int hnum, int dif,
346*5051ebd2SEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
347*5051ebd2SEric Dumazet {
348*5051ebd2SEric Dumazet 	struct sock *sk, *result;
349*5051ebd2SEric Dumazet 	struct hlist_nulls_node *node;
350*5051ebd2SEric Dumazet 	int score, badness;
351*5051ebd2SEric Dumazet 
352*5051ebd2SEric Dumazet begin:
353*5051ebd2SEric Dumazet 	result = NULL;
354*5051ebd2SEric Dumazet 	badness = -1;
355*5051ebd2SEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
356*5051ebd2SEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
357*5051ebd2SEric Dumazet 				      daddr, hnum, dif);
358*5051ebd2SEric Dumazet 		if (score > badness) {
359*5051ebd2SEric Dumazet 			result = sk;
360*5051ebd2SEric Dumazet 			badness = score;
361*5051ebd2SEric Dumazet 			if (score == SCORE2_MAX)
362*5051ebd2SEric Dumazet 				goto exact_match;
363*5051ebd2SEric Dumazet 		}
364*5051ebd2SEric Dumazet 	}
365*5051ebd2SEric Dumazet 	/*
366*5051ebd2SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
367*5051ebd2SEric Dumazet 	 * not the expected one, we must restart lookup.
368*5051ebd2SEric Dumazet 	 * We probably met an item that was moved to another chain.
369*5051ebd2SEric Dumazet 	 */
370*5051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot2)
371*5051ebd2SEric Dumazet 		goto begin;
372*5051ebd2SEric Dumazet 
373*5051ebd2SEric Dumazet 	if (result) {
374*5051ebd2SEric Dumazet exact_match:
375*5051ebd2SEric Dumazet 		if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
376*5051ebd2SEric Dumazet 			result = NULL;
377*5051ebd2SEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
378*5051ebd2SEric Dumazet 				  daddr, hnum, dif) < badness)) {
379*5051ebd2SEric Dumazet 			sock_put(result);
380*5051ebd2SEric Dumazet 			goto begin;
381*5051ebd2SEric Dumazet 		}
382*5051ebd2SEric Dumazet 	}
383*5051ebd2SEric Dumazet 	return result;
384*5051ebd2SEric Dumazet }
385*5051ebd2SEric Dumazet 
386db8dac20SDavid S. Miller /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
387db8dac20SDavid S. Miller  * harder than this. -DaveM
388db8dac20SDavid S. Miller  */
389db8dac20SDavid S. Miller static struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
390db8dac20SDavid S. Miller 		__be16 sport, __be32 daddr, __be16 dport,
391645ca708SEric Dumazet 		int dif, struct udp_table *udptable)
392db8dac20SDavid S. Miller {
393271b72c7SEric Dumazet 	struct sock *sk, *result;
39488ab1932SEric Dumazet 	struct hlist_nulls_node *node;
395db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(dport);
396*5051ebd2SEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
397*5051ebd2SEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
398271b72c7SEric Dumazet 	int score, badness;
399db8dac20SDavid S. Miller 
400271b72c7SEric Dumazet 	rcu_read_lock();
401*5051ebd2SEric Dumazet 	if (hslot->count > 10) {
402*5051ebd2SEric Dumazet 		hash2 = udp4_portaddr_hash(net, daddr, hnum);
403*5051ebd2SEric Dumazet 		slot2 = hash2 & udptable->mask;
404*5051ebd2SEric Dumazet 		hslot2 = &udptable->hash2[slot2];
405*5051ebd2SEric Dumazet 		if (hslot->count < hslot2->count)
406*5051ebd2SEric Dumazet 			goto begin;
407*5051ebd2SEric Dumazet 
408*5051ebd2SEric Dumazet 		result = udp4_lib_lookup2(net, saddr, sport,
409*5051ebd2SEric Dumazet 					  daddr, hnum, dif,
410*5051ebd2SEric Dumazet 					  hslot2, slot2);
411*5051ebd2SEric Dumazet 		if (!result) {
412*5051ebd2SEric Dumazet 			hash2 = udp4_portaddr_hash(net, INADDR_ANY, hnum);
413*5051ebd2SEric Dumazet 			slot2 = hash2 & udptable->mask;
414*5051ebd2SEric Dumazet 			hslot2 = &udptable->hash2[slot2];
415*5051ebd2SEric Dumazet 			if (hslot->count < hslot2->count)
416*5051ebd2SEric Dumazet 				goto begin;
417*5051ebd2SEric Dumazet 
418*5051ebd2SEric Dumazet 			result = udp4_lib_lookup2(net, INADDR_ANY, sport,
419*5051ebd2SEric Dumazet 						  daddr, hnum, dif,
420*5051ebd2SEric Dumazet 						  hslot2, slot2);
421*5051ebd2SEric Dumazet 		}
422*5051ebd2SEric Dumazet 		rcu_read_unlock();
423*5051ebd2SEric Dumazet 		return result;
424*5051ebd2SEric Dumazet 	}
425271b72c7SEric Dumazet begin:
426271b72c7SEric Dumazet 	result = NULL;
427271b72c7SEric Dumazet 	badness = -1;
42888ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
429645ca708SEric Dumazet 		score = compute_score(sk, net, saddr, hnum, sport,
430645ca708SEric Dumazet 				      daddr, dport, dif);
431645ca708SEric Dumazet 		if (score > badness) {
432db8dac20SDavid S. Miller 			result = sk;
433db8dac20SDavid S. Miller 			badness = score;
434db8dac20SDavid S. Miller 		}
435db8dac20SDavid S. Miller 	}
43688ab1932SEric Dumazet 	/*
43788ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
43888ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
43988ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
44088ab1932SEric Dumazet 	 */
441*5051ebd2SEric Dumazet 	if (get_nulls_value(node) != slot)
44288ab1932SEric Dumazet 		goto begin;
44388ab1932SEric Dumazet 
444271b72c7SEric Dumazet 	if (result) {
445271b72c7SEric Dumazet 		if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
446271b72c7SEric Dumazet 			result = NULL;
447271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, saddr, hnum, sport,
448271b72c7SEric Dumazet 				  daddr, dport, dif) < badness)) {
449271b72c7SEric Dumazet 			sock_put(result);
450271b72c7SEric Dumazet 			goto begin;
451271b72c7SEric Dumazet 		}
452271b72c7SEric Dumazet 	}
453271b72c7SEric Dumazet 	rcu_read_unlock();
454db8dac20SDavid S. Miller 	return result;
455db8dac20SDavid S. Miller }
456db8dac20SDavid S. Miller 
457607c4aafSKOVACS Krisztian static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
458607c4aafSKOVACS Krisztian 						 __be16 sport, __be16 dport,
459645ca708SEric Dumazet 						 struct udp_table *udptable)
460607c4aafSKOVACS Krisztian {
46123542618SKOVACS Krisztian 	struct sock *sk;
462607c4aafSKOVACS Krisztian 	const struct iphdr *iph = ip_hdr(skb);
463607c4aafSKOVACS Krisztian 
46423542618SKOVACS Krisztian 	if (unlikely(sk = skb_steal_sock(skb)))
46523542618SKOVACS Krisztian 		return sk;
46623542618SKOVACS Krisztian 	else
467adf30907SEric Dumazet 		return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
468607c4aafSKOVACS Krisztian 					 iph->daddr, dport, inet_iif(skb),
469607c4aafSKOVACS Krisztian 					 udptable);
470607c4aafSKOVACS Krisztian }
471607c4aafSKOVACS Krisztian 
472bcd41303SKOVACS Krisztian struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
473bcd41303SKOVACS Krisztian 			     __be32 daddr, __be16 dport, int dif)
474bcd41303SKOVACS Krisztian {
475645ca708SEric Dumazet 	return __udp4_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
476bcd41303SKOVACS Krisztian }
477bcd41303SKOVACS Krisztian EXPORT_SYMBOL_GPL(udp4_lib_lookup);
478bcd41303SKOVACS Krisztian 
479920a4611SEric Dumazet static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
480db8dac20SDavid S. Miller 					     __be16 loc_port, __be32 loc_addr,
481db8dac20SDavid S. Miller 					     __be16 rmt_port, __be32 rmt_addr,
482db8dac20SDavid S. Miller 					     int dif)
483db8dac20SDavid S. Miller {
48488ab1932SEric Dumazet 	struct hlist_nulls_node *node;
485db8dac20SDavid S. Miller 	struct sock *s = sk;
486db8dac20SDavid S. Miller 	unsigned short hnum = ntohs(loc_port);
487db8dac20SDavid S. Miller 
48888ab1932SEric Dumazet 	sk_nulls_for_each_from(s, node) {
489db8dac20SDavid S. Miller 		struct inet_sock *inet = inet_sk(s);
490db8dac20SDavid S. Miller 
491920a4611SEric Dumazet 		if (!net_eq(sock_net(s), net)				||
492d4cada4aSEric Dumazet 		    udp_sk(s)->udp_port_hash != hnum			||
493c720c7e8SEric Dumazet 		    (inet->inet_daddr && inet->inet_daddr != rmt_addr)	||
494c720c7e8SEric Dumazet 		    (inet->inet_dport != rmt_port && inet->inet_dport)	||
495c720c7e8SEric Dumazet 		    (inet->inet_rcv_saddr	&&
496c720c7e8SEric Dumazet 		     inet->inet_rcv_saddr != loc_addr)			||
497db8dac20SDavid S. Miller 		    ipv6_only_sock(s)					||
498db8dac20SDavid S. Miller 		    (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
499db8dac20SDavid S. Miller 			continue;
500db8dac20SDavid S. Miller 		if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
501db8dac20SDavid S. Miller 			continue;
502db8dac20SDavid S. Miller 		goto found;
503db8dac20SDavid S. Miller 	}
504db8dac20SDavid S. Miller 	s = NULL;
505db8dac20SDavid S. Miller found:
506db8dac20SDavid S. Miller 	return s;
507db8dac20SDavid S. Miller }
508db8dac20SDavid S. Miller 
509db8dac20SDavid S. Miller /*
510db8dac20SDavid S. Miller  * This routine is called by the ICMP module when it gets some
511db8dac20SDavid S. Miller  * sort of error condition.  If err < 0 then the socket should
512db8dac20SDavid S. Miller  * be closed and the error returned to the user.  If err > 0
513db8dac20SDavid S. Miller  * it's just the icmp type << 8 | icmp code.
514db8dac20SDavid S. Miller  * Header points to the ip header of the error packet. We move
515db8dac20SDavid S. Miller  * on past this. Then (as it used to claim before adjustment)
516db8dac20SDavid S. Miller  * header points to the first 8 bytes of the udp header.  We need
517db8dac20SDavid S. Miller  * to find the appropriate port.
518db8dac20SDavid S. Miller  */
519db8dac20SDavid S. Miller 
520645ca708SEric Dumazet void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
521db8dac20SDavid S. Miller {
522db8dac20SDavid S. Miller 	struct inet_sock *inet;
523db8dac20SDavid S. Miller 	struct iphdr *iph = (struct iphdr *)skb->data;
524db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
525db8dac20SDavid S. Miller 	const int type = icmp_hdr(skb)->type;
526db8dac20SDavid S. Miller 	const int code = icmp_hdr(skb)->code;
527db8dac20SDavid S. Miller 	struct sock *sk;
528db8dac20SDavid S. Miller 	int harderr;
529db8dac20SDavid S. Miller 	int err;
530fd54d716SPavel Emelyanov 	struct net *net = dev_net(skb->dev);
531db8dac20SDavid S. Miller 
532fd54d716SPavel Emelyanov 	sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
533db8dac20SDavid S. Miller 			iph->saddr, uh->source, skb->dev->ifindex, udptable);
534db8dac20SDavid S. Miller 	if (sk == NULL) {
535dcfc23caSPavel Emelyanov 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
536db8dac20SDavid S. Miller 		return;	/* No socket for error */
537db8dac20SDavid S. Miller 	}
538db8dac20SDavid S. Miller 
539db8dac20SDavid S. Miller 	err = 0;
540db8dac20SDavid S. Miller 	harderr = 0;
541db8dac20SDavid S. Miller 	inet = inet_sk(sk);
542db8dac20SDavid S. Miller 
543db8dac20SDavid S. Miller 	switch (type) {
544db8dac20SDavid S. Miller 	default:
545db8dac20SDavid S. Miller 	case ICMP_TIME_EXCEEDED:
546db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
547db8dac20SDavid S. Miller 		break;
548db8dac20SDavid S. Miller 	case ICMP_SOURCE_QUENCH:
549db8dac20SDavid S. Miller 		goto out;
550db8dac20SDavid S. Miller 	case ICMP_PARAMETERPROB:
551db8dac20SDavid S. Miller 		err = EPROTO;
552db8dac20SDavid S. Miller 		harderr = 1;
553db8dac20SDavid S. Miller 		break;
554db8dac20SDavid S. Miller 	case ICMP_DEST_UNREACH:
555db8dac20SDavid S. Miller 		if (code == ICMP_FRAG_NEEDED) { /* Path MTU discovery */
556db8dac20SDavid S. Miller 			if (inet->pmtudisc != IP_PMTUDISC_DONT) {
557db8dac20SDavid S. Miller 				err = EMSGSIZE;
558db8dac20SDavid S. Miller 				harderr = 1;
559db8dac20SDavid S. Miller 				break;
560db8dac20SDavid S. Miller 			}
561db8dac20SDavid S. Miller 			goto out;
562db8dac20SDavid S. Miller 		}
563db8dac20SDavid S. Miller 		err = EHOSTUNREACH;
564db8dac20SDavid S. Miller 		if (code <= NR_ICMP_UNREACH) {
565db8dac20SDavid S. Miller 			harderr = icmp_err_convert[code].fatal;
566db8dac20SDavid S. Miller 			err = icmp_err_convert[code].errno;
567db8dac20SDavid S. Miller 		}
568db8dac20SDavid S. Miller 		break;
569db8dac20SDavid S. Miller 	}
570db8dac20SDavid S. Miller 
571db8dac20SDavid S. Miller 	/*
572db8dac20SDavid S. Miller 	 *      RFC1122: OK.  Passes ICMP errors back to application, as per
573db8dac20SDavid S. Miller 	 *	4.1.3.3.
574db8dac20SDavid S. Miller 	 */
575db8dac20SDavid S. Miller 	if (!inet->recverr) {
576db8dac20SDavid S. Miller 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
577db8dac20SDavid S. Miller 			goto out;
578db8dac20SDavid S. Miller 	} else {
579db8dac20SDavid S. Miller 		ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
580db8dac20SDavid S. Miller 	}
581db8dac20SDavid S. Miller 	sk->sk_err = err;
582db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
583db8dac20SDavid S. Miller out:
584db8dac20SDavid S. Miller 	sock_put(sk);
585db8dac20SDavid S. Miller }
586db8dac20SDavid S. Miller 
587db8dac20SDavid S. Miller void udp_err(struct sk_buff *skb, u32 info)
588db8dac20SDavid S. Miller {
589645ca708SEric Dumazet 	__udp4_lib_err(skb, info, &udp_table);
590db8dac20SDavid S. Miller }
591db8dac20SDavid S. Miller 
592db8dac20SDavid S. Miller /*
593db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
594db8dac20SDavid S. Miller  */
59536d926b9SDenis V. Lunev void udp_flush_pending_frames(struct sock *sk)
596db8dac20SDavid S. Miller {
597db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
598db8dac20SDavid S. Miller 
599db8dac20SDavid S. Miller 	if (up->pending) {
600db8dac20SDavid S. Miller 		up->len = 0;
601db8dac20SDavid S. Miller 		up->pending = 0;
602db8dac20SDavid S. Miller 		ip_flush_pending_frames(sk);
603db8dac20SDavid S. Miller 	}
604db8dac20SDavid S. Miller }
60536d926b9SDenis V. Lunev EXPORT_SYMBOL(udp_flush_pending_frames);
606db8dac20SDavid S. Miller 
607db8dac20SDavid S. Miller /**
608db8dac20SDavid S. Miller  * 	udp4_hwcsum_outgoing  -  handle outgoing HW checksumming
609db8dac20SDavid S. Miller  * 	@sk: 	socket we are sending on
610db8dac20SDavid S. Miller  * 	@skb: 	sk_buff containing the filled-in UDP header
611db8dac20SDavid S. Miller  * 	        (checksum field must be zeroed out)
612db8dac20SDavid S. Miller  */
613db8dac20SDavid S. Miller static void udp4_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
614db8dac20SDavid S. Miller 				 __be32 src, __be32 dst, int len)
615db8dac20SDavid S. Miller {
616db8dac20SDavid S. Miller 	unsigned int offset;
617db8dac20SDavid S. Miller 	struct udphdr *uh = udp_hdr(skb);
618db8dac20SDavid S. Miller 	__wsum csum = 0;
619db8dac20SDavid S. Miller 
620db8dac20SDavid S. Miller 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
621db8dac20SDavid S. Miller 		/*
622db8dac20SDavid S. Miller 		 * Only one fragment on the socket.
623db8dac20SDavid S. Miller 		 */
624db8dac20SDavid S. Miller 		skb->csum_start = skb_transport_header(skb) - skb->head;
625db8dac20SDavid S. Miller 		skb->csum_offset = offsetof(struct udphdr, check);
626db8dac20SDavid S. Miller 		uh->check = ~csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, 0);
627db8dac20SDavid S. Miller 	} else {
628db8dac20SDavid S. Miller 		/*
629db8dac20SDavid S. Miller 		 * HW-checksum won't work as there are two or more
630db8dac20SDavid S. Miller 		 * fragments on the socket so that all csums of sk_buffs
631db8dac20SDavid S. Miller 		 * should be together
632db8dac20SDavid S. Miller 		 */
633db8dac20SDavid S. Miller 		offset = skb_transport_offset(skb);
634db8dac20SDavid S. Miller 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
635db8dac20SDavid S. Miller 
636db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
637db8dac20SDavid S. Miller 
638db8dac20SDavid S. Miller 		skb_queue_walk(&sk->sk_write_queue, skb) {
639db8dac20SDavid S. Miller 			csum = csum_add(csum, skb->csum);
640db8dac20SDavid S. Miller 		}
641db8dac20SDavid S. Miller 
642db8dac20SDavid S. Miller 		uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
643db8dac20SDavid S. Miller 		if (uh->check == 0)
644db8dac20SDavid S. Miller 			uh->check = CSUM_MANGLED_0;
645db8dac20SDavid S. Miller 	}
646db8dac20SDavid S. Miller }
647db8dac20SDavid S. Miller 
648db8dac20SDavid S. Miller /*
649db8dac20SDavid S. Miller  * Push out all pending data as one UDP datagram. Socket is locked.
650db8dac20SDavid S. Miller  */
651db8dac20SDavid S. Miller static int udp_push_pending_frames(struct sock *sk)
652db8dac20SDavid S. Miller {
653db8dac20SDavid S. Miller 	struct udp_sock  *up = udp_sk(sk);
654db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
655db8dac20SDavid S. Miller 	struct flowi *fl = &inet->cork.fl;
656db8dac20SDavid S. Miller 	struct sk_buff *skb;
657db8dac20SDavid S. Miller 	struct udphdr *uh;
658db8dac20SDavid S. Miller 	int err = 0;
659db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
660db8dac20SDavid S. Miller 	__wsum csum = 0;
661db8dac20SDavid S. Miller 
662db8dac20SDavid S. Miller 	/* Grab the skbuff where UDP header space exists. */
663db8dac20SDavid S. Miller 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
664db8dac20SDavid S. Miller 		goto out;
665db8dac20SDavid S. Miller 
666db8dac20SDavid S. Miller 	/*
667db8dac20SDavid S. Miller 	 * Create a UDP header
668db8dac20SDavid S. Miller 	 */
669db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
670db8dac20SDavid S. Miller 	uh->source = fl->fl_ip_sport;
671db8dac20SDavid S. Miller 	uh->dest = fl->fl_ip_dport;
672db8dac20SDavid S. Miller 	uh->len = htons(up->len);
673db8dac20SDavid S. Miller 	uh->check = 0;
674db8dac20SDavid S. Miller 
675db8dac20SDavid S. Miller 	if (is_udplite)  				 /*     UDP-Lite      */
676db8dac20SDavid S. Miller 		csum  = udplite_csum_outgoing(sk, skb);
677db8dac20SDavid S. Miller 
678db8dac20SDavid S. Miller 	else if (sk->sk_no_check == UDP_CSUM_NOXMIT) {   /* UDP csum disabled */
679db8dac20SDavid S. Miller 
680db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_NONE;
681db8dac20SDavid S. Miller 		goto send;
682db8dac20SDavid S. Miller 
683db8dac20SDavid S. Miller 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
684db8dac20SDavid S. Miller 
685db8dac20SDavid S. Miller 		udp4_hwcsum_outgoing(sk, skb, fl->fl4_src, fl->fl4_dst, up->len);
686db8dac20SDavid S. Miller 		goto send;
687db8dac20SDavid S. Miller 
688db8dac20SDavid S. Miller 	} else						 /*   `normal' UDP    */
689db8dac20SDavid S. Miller 		csum = udp_csum_outgoing(sk, skb);
690db8dac20SDavid S. Miller 
691db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
692db8dac20SDavid S. Miller 	uh->check = csum_tcpudp_magic(fl->fl4_src, fl->fl4_dst, up->len,
693db8dac20SDavid S. Miller 				      sk->sk_protocol, csum);
694db8dac20SDavid S. Miller 	if (uh->check == 0)
695db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
696db8dac20SDavid S. Miller 
697db8dac20SDavid S. Miller send:
698db8dac20SDavid S. Miller 	err = ip_push_pending_frames(sk);
6996ce9e7b5SEric Dumazet 	if (err) {
7006ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet->recverr) {
7016ce9e7b5SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
7026ce9e7b5SEric Dumazet 					   UDP_MIB_SNDBUFERRORS, is_udplite);
7036ce9e7b5SEric Dumazet 			err = 0;
7046ce9e7b5SEric Dumazet 		}
7056ce9e7b5SEric Dumazet 	} else
7066ce9e7b5SEric Dumazet 		UDP_INC_STATS_USER(sock_net(sk),
7076ce9e7b5SEric Dumazet 				   UDP_MIB_OUTDATAGRAMS, is_udplite);
708db8dac20SDavid S. Miller out:
709db8dac20SDavid S. Miller 	up->len = 0;
710db8dac20SDavid S. Miller 	up->pending = 0;
711db8dac20SDavid S. Miller 	return err;
712db8dac20SDavid S. Miller }
713db8dac20SDavid S. Miller 
714db8dac20SDavid S. Miller int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
715db8dac20SDavid S. Miller 		size_t len)
716db8dac20SDavid S. Miller {
717db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
718db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
719db8dac20SDavid S. Miller 	int ulen = len;
720db8dac20SDavid S. Miller 	struct ipcm_cookie ipc;
721db8dac20SDavid S. Miller 	struct rtable *rt = NULL;
722db8dac20SDavid S. Miller 	int free = 0;
723db8dac20SDavid S. Miller 	int connected = 0;
724db8dac20SDavid S. Miller 	__be32 daddr, faddr, saddr;
725db8dac20SDavid S. Miller 	__be16 dport;
726db8dac20SDavid S. Miller 	u8  tos;
727db8dac20SDavid S. Miller 	int err, is_udplite = IS_UDPLITE(sk);
728db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
729db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
730db8dac20SDavid S. Miller 
731db8dac20SDavid S. Miller 	if (len > 0xFFFF)
732db8dac20SDavid S. Miller 		return -EMSGSIZE;
733db8dac20SDavid S. Miller 
734db8dac20SDavid S. Miller 	/*
735db8dac20SDavid S. Miller 	 *	Check the flags.
736db8dac20SDavid S. Miller 	 */
737db8dac20SDavid S. Miller 
738db8dac20SDavid S. Miller 	if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
739db8dac20SDavid S. Miller 		return -EOPNOTSUPP;
740db8dac20SDavid S. Miller 
741db8dac20SDavid S. Miller 	ipc.opt = NULL;
74251f31cabSPatrick Ohly 	ipc.shtx.flags = 0;
743db8dac20SDavid S. Miller 
744db8dac20SDavid S. Miller 	if (up->pending) {
745db8dac20SDavid S. Miller 		/*
746db8dac20SDavid S. Miller 		 * There are pending frames.
747db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
748db8dac20SDavid S. Miller 		 */
749db8dac20SDavid S. Miller 		lock_sock(sk);
750db8dac20SDavid S. Miller 		if (likely(up->pending)) {
751db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET)) {
752db8dac20SDavid S. Miller 				release_sock(sk);
753db8dac20SDavid S. Miller 				return -EINVAL;
754db8dac20SDavid S. Miller 			}
755db8dac20SDavid S. Miller 			goto do_append_data;
756db8dac20SDavid S. Miller 		}
757db8dac20SDavid S. Miller 		release_sock(sk);
758db8dac20SDavid S. Miller 	}
759db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
760db8dac20SDavid S. Miller 
761db8dac20SDavid S. Miller 	/*
762db8dac20SDavid S. Miller 	 *	Get and verify the address.
763db8dac20SDavid S. Miller 	 */
764db8dac20SDavid S. Miller 	if (msg->msg_name) {
765db8dac20SDavid S. Miller 		struct sockaddr_in * usin = (struct sockaddr_in *)msg->msg_name;
766db8dac20SDavid S. Miller 		if (msg->msg_namelen < sizeof(*usin))
767db8dac20SDavid S. Miller 			return -EINVAL;
768db8dac20SDavid S. Miller 		if (usin->sin_family != AF_INET) {
769db8dac20SDavid S. Miller 			if (usin->sin_family != AF_UNSPEC)
770db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
771db8dac20SDavid S. Miller 		}
772db8dac20SDavid S. Miller 
773db8dac20SDavid S. Miller 		daddr = usin->sin_addr.s_addr;
774db8dac20SDavid S. Miller 		dport = usin->sin_port;
775db8dac20SDavid S. Miller 		if (dport == 0)
776db8dac20SDavid S. Miller 			return -EINVAL;
777db8dac20SDavid S. Miller 	} else {
778db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
779db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
780c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
781c720c7e8SEric Dumazet 		dport = inet->inet_dport;
782db8dac20SDavid S. Miller 		/* Open fast path for connected socket.
783db8dac20SDavid S. Miller 		   Route will not be used, if at least one option is set.
784db8dac20SDavid S. Miller 		 */
785db8dac20SDavid S. Miller 		connected = 1;
786db8dac20SDavid S. Miller 	}
787c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
788db8dac20SDavid S. Miller 
789db8dac20SDavid S. Miller 	ipc.oif = sk->sk_bound_dev_if;
79051f31cabSPatrick Ohly 	err = sock_tx_timestamp(msg, sk, &ipc.shtx);
79151f31cabSPatrick Ohly 	if (err)
79251f31cabSPatrick Ohly 		return err;
793db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
7943b1e0a65SYOSHIFUJI Hideaki 		err = ip_cmsg_send(sock_net(sk), msg, &ipc);
795db8dac20SDavid S. Miller 		if (err)
796db8dac20SDavid S. Miller 			return err;
797db8dac20SDavid S. Miller 		if (ipc.opt)
798db8dac20SDavid S. Miller 			free = 1;
799db8dac20SDavid S. Miller 		connected = 0;
800db8dac20SDavid S. Miller 	}
801db8dac20SDavid S. Miller 	if (!ipc.opt)
802db8dac20SDavid S. Miller 		ipc.opt = inet->opt;
803db8dac20SDavid S. Miller 
804db8dac20SDavid S. Miller 	saddr = ipc.addr;
805db8dac20SDavid S. Miller 	ipc.addr = faddr = daddr;
806db8dac20SDavid S. Miller 
807db8dac20SDavid S. Miller 	if (ipc.opt && ipc.opt->srr) {
808db8dac20SDavid S. Miller 		if (!daddr)
809db8dac20SDavid S. Miller 			return -EINVAL;
810db8dac20SDavid S. Miller 		faddr = ipc.opt->faddr;
811db8dac20SDavid S. Miller 		connected = 0;
812db8dac20SDavid S. Miller 	}
813db8dac20SDavid S. Miller 	tos = RT_TOS(inet->tos);
814db8dac20SDavid S. Miller 	if (sock_flag(sk, SOCK_LOCALROUTE) ||
815db8dac20SDavid S. Miller 	    (msg->msg_flags & MSG_DONTROUTE) ||
816db8dac20SDavid S. Miller 	    (ipc.opt && ipc.opt->is_strictroute)) {
817db8dac20SDavid S. Miller 		tos |= RTO_ONLINK;
818db8dac20SDavid S. Miller 		connected = 0;
819db8dac20SDavid S. Miller 	}
820db8dac20SDavid S. Miller 
821db8dac20SDavid S. Miller 	if (ipv4_is_multicast(daddr)) {
822db8dac20SDavid S. Miller 		if (!ipc.oif)
823db8dac20SDavid S. Miller 			ipc.oif = inet->mc_index;
824db8dac20SDavid S. Miller 		if (!saddr)
825db8dac20SDavid S. Miller 			saddr = inet->mc_addr;
826db8dac20SDavid S. Miller 		connected = 0;
827db8dac20SDavid S. Miller 	}
828db8dac20SDavid S. Miller 
829db8dac20SDavid S. Miller 	if (connected)
830db8dac20SDavid S. Miller 		rt = (struct rtable *)sk_dst_check(sk, 0);
831db8dac20SDavid S. Miller 
832db8dac20SDavid S. Miller 	if (rt == NULL) {
833db8dac20SDavid S. Miller 		struct flowi fl = { .oif = ipc.oif,
834914a9ab3SAtis Elsts 				    .mark = sk->sk_mark,
835db8dac20SDavid S. Miller 				    .nl_u = { .ip4_u =
836db8dac20SDavid S. Miller 					      { .daddr = faddr,
837db8dac20SDavid S. Miller 						.saddr = saddr,
838db8dac20SDavid S. Miller 						.tos = tos } },
839db8dac20SDavid S. Miller 				    .proto = sk->sk_protocol,
840a134f85cSBalazs Scheidler 				    .flags = inet_sk_flowi_flags(sk),
841db8dac20SDavid S. Miller 				    .uli_u = { .ports =
842c720c7e8SEric Dumazet 					       { .sport = inet->inet_sport,
843db8dac20SDavid S. Miller 						 .dport = dport } } };
84484a3aa00SPavel Emelyanov 		struct net *net = sock_net(sk);
84584a3aa00SPavel Emelyanov 
846db8dac20SDavid S. Miller 		security_sk_classify_flow(sk, &fl);
84784a3aa00SPavel Emelyanov 		err = ip_route_output_flow(net, &rt, &fl, sk, 1);
848db8dac20SDavid S. Miller 		if (err) {
849db8dac20SDavid S. Miller 			if (err == -ENETUNREACH)
8507c73a6faSPavel Emelyanov 				IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
851db8dac20SDavid S. Miller 			goto out;
852db8dac20SDavid S. Miller 		}
853db8dac20SDavid S. Miller 
854db8dac20SDavid S. Miller 		err = -EACCES;
855db8dac20SDavid S. Miller 		if ((rt->rt_flags & RTCF_BROADCAST) &&
856db8dac20SDavid S. Miller 		    !sock_flag(sk, SOCK_BROADCAST))
857db8dac20SDavid S. Miller 			goto out;
858db8dac20SDavid S. Miller 		if (connected)
859db8dac20SDavid S. Miller 			sk_dst_set(sk, dst_clone(&rt->u.dst));
860db8dac20SDavid S. Miller 	}
861db8dac20SDavid S. Miller 
862db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
863db8dac20SDavid S. Miller 		goto do_confirm;
864db8dac20SDavid S. Miller back_from_confirm:
865db8dac20SDavid S. Miller 
866db8dac20SDavid S. Miller 	saddr = rt->rt_src;
867db8dac20SDavid S. Miller 	if (!ipc.addr)
868db8dac20SDavid S. Miller 		daddr = ipc.addr = rt->rt_dst;
869db8dac20SDavid S. Miller 
870db8dac20SDavid S. Miller 	lock_sock(sk);
871db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
872db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
873db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
874db8dac20SDavid S. Miller 		release_sock(sk);
875db8dac20SDavid S. Miller 
876db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
877db8dac20SDavid S. Miller 		err = -EINVAL;
878db8dac20SDavid S. Miller 		goto out;
879db8dac20SDavid S. Miller 	}
880db8dac20SDavid S. Miller 	/*
881db8dac20SDavid S. Miller 	 *	Now cork the socket to pend data.
882db8dac20SDavid S. Miller 	 */
883db8dac20SDavid S. Miller 	inet->cork.fl.fl4_dst = daddr;
884db8dac20SDavid S. Miller 	inet->cork.fl.fl_ip_dport = dport;
885db8dac20SDavid S. Miller 	inet->cork.fl.fl4_src = saddr;
886c720c7e8SEric Dumazet 	inet->cork.fl.fl_ip_sport = inet->inet_sport;
887db8dac20SDavid S. Miller 	up->pending = AF_INET;
888db8dac20SDavid S. Miller 
889db8dac20SDavid S. Miller do_append_data:
890db8dac20SDavid S. Miller 	up->len += ulen;
891db8dac20SDavid S. Miller 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
892db8dac20SDavid S. Miller 	err = ip_append_data(sk, getfrag, msg->msg_iov, ulen,
8932e77d89bSEric Dumazet 			sizeof(struct udphdr), &ipc, &rt,
894db8dac20SDavid S. Miller 			corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
895db8dac20SDavid S. Miller 	if (err)
896db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
897db8dac20SDavid S. Miller 	else if (!corkreq)
898db8dac20SDavid S. Miller 		err = udp_push_pending_frames(sk);
899db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
900db8dac20SDavid S. Miller 		up->pending = 0;
901db8dac20SDavid S. Miller 	release_sock(sk);
902db8dac20SDavid S. Miller 
903db8dac20SDavid S. Miller out:
904db8dac20SDavid S. Miller 	ip_rt_put(rt);
905db8dac20SDavid S. Miller 	if (free)
906db8dac20SDavid S. Miller 		kfree(ipc.opt);
907db8dac20SDavid S. Miller 	if (!err)
908db8dac20SDavid S. Miller 		return len;
909db8dac20SDavid S. Miller 	/*
910db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
911db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
912db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
913db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
914db8dac20SDavid S. Miller 	 * seems like overkill.
915db8dac20SDavid S. Miller 	 */
916db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
917629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
918629ca23cSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
919db8dac20SDavid S. Miller 	}
920db8dac20SDavid S. Miller 	return err;
921db8dac20SDavid S. Miller 
922db8dac20SDavid S. Miller do_confirm:
923db8dac20SDavid S. Miller 	dst_confirm(&rt->u.dst);
924db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
925db8dac20SDavid S. Miller 		goto back_from_confirm;
926db8dac20SDavid S. Miller 	err = 0;
927db8dac20SDavid S. Miller 	goto out;
928db8dac20SDavid S. Miller }
929c482c568SEric Dumazet EXPORT_SYMBOL(udp_sendmsg);
930db8dac20SDavid S. Miller 
931db8dac20SDavid S. Miller int udp_sendpage(struct sock *sk, struct page *page, int offset,
932db8dac20SDavid S. Miller 		 size_t size, int flags)
933db8dac20SDavid S. Miller {
934db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
935db8dac20SDavid S. Miller 	int ret;
936db8dac20SDavid S. Miller 
937db8dac20SDavid S. Miller 	if (!up->pending) {
938db8dac20SDavid S. Miller 		struct msghdr msg = {	.msg_flags = flags|MSG_MORE };
939db8dac20SDavid S. Miller 
940db8dac20SDavid S. Miller 		/* Call udp_sendmsg to specify destination address which
941db8dac20SDavid S. Miller 		 * sendpage interface can't pass.
942db8dac20SDavid S. Miller 		 * This will succeed only when the socket is connected.
943db8dac20SDavid S. Miller 		 */
944db8dac20SDavid S. Miller 		ret = udp_sendmsg(NULL, sk, &msg, 0);
945db8dac20SDavid S. Miller 		if (ret < 0)
946db8dac20SDavid S. Miller 			return ret;
947db8dac20SDavid S. Miller 	}
948db8dac20SDavid S. Miller 
949db8dac20SDavid S. Miller 	lock_sock(sk);
950db8dac20SDavid S. Miller 
951db8dac20SDavid S. Miller 	if (unlikely(!up->pending)) {
952db8dac20SDavid S. Miller 		release_sock(sk);
953db8dac20SDavid S. Miller 
954db8dac20SDavid S. Miller 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 3\n");
955db8dac20SDavid S. Miller 		return -EINVAL;
956db8dac20SDavid S. Miller 	}
957db8dac20SDavid S. Miller 
958db8dac20SDavid S. Miller 	ret = ip_append_page(sk, page, offset, size, flags);
959db8dac20SDavid S. Miller 	if (ret == -EOPNOTSUPP) {
960db8dac20SDavid S. Miller 		release_sock(sk);
961db8dac20SDavid S. Miller 		return sock_no_sendpage(sk->sk_socket, page, offset,
962db8dac20SDavid S. Miller 					size, flags);
963db8dac20SDavid S. Miller 	}
964db8dac20SDavid S. Miller 	if (ret < 0) {
965db8dac20SDavid S. Miller 		udp_flush_pending_frames(sk);
966db8dac20SDavid S. Miller 		goto out;
967db8dac20SDavid S. Miller 	}
968db8dac20SDavid S. Miller 
969db8dac20SDavid S. Miller 	up->len += size;
970db8dac20SDavid S. Miller 	if (!(up->corkflag || (flags&MSG_MORE)))
971db8dac20SDavid S. Miller 		ret = udp_push_pending_frames(sk);
972db8dac20SDavid S. Miller 	if (!ret)
973db8dac20SDavid S. Miller 		ret = size;
974db8dac20SDavid S. Miller out:
975db8dac20SDavid S. Miller 	release_sock(sk);
976db8dac20SDavid S. Miller 	return ret;
977db8dac20SDavid S. Miller }
978db8dac20SDavid S. Miller 
97985584672SEric Dumazet 
98085584672SEric Dumazet /**
98185584672SEric Dumazet  *	first_packet_length	- return length of first packet in receive queue
98285584672SEric Dumazet  *	@sk: socket
98385584672SEric Dumazet  *
98485584672SEric Dumazet  *	Drops all bad checksum frames, until a valid one is found.
98585584672SEric Dumazet  *	Returns the length of found skb, or 0 if none is found.
98685584672SEric Dumazet  */
98785584672SEric Dumazet static unsigned int first_packet_length(struct sock *sk)
98885584672SEric Dumazet {
98985584672SEric Dumazet 	struct sk_buff_head list_kill, *rcvq = &sk->sk_receive_queue;
99085584672SEric Dumazet 	struct sk_buff *skb;
99185584672SEric Dumazet 	unsigned int res;
99285584672SEric Dumazet 
99385584672SEric Dumazet 	__skb_queue_head_init(&list_kill);
99485584672SEric Dumazet 
99585584672SEric Dumazet 	spin_lock_bh(&rcvq->lock);
99685584672SEric Dumazet 	while ((skb = skb_peek(rcvq)) != NULL &&
99785584672SEric Dumazet 		udp_lib_checksum_complete(skb)) {
99885584672SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
99985584672SEric Dumazet 				 IS_UDPLITE(sk));
10008edf19c2SEric Dumazet 		atomic_inc(&sk->sk_drops);
100185584672SEric Dumazet 		__skb_unlink(skb, rcvq);
100285584672SEric Dumazet 		__skb_queue_tail(&list_kill, skb);
100385584672SEric Dumazet 	}
100485584672SEric Dumazet 	res = skb ? skb->len : 0;
100585584672SEric Dumazet 	spin_unlock_bh(&rcvq->lock);
100685584672SEric Dumazet 
100785584672SEric Dumazet 	if (!skb_queue_empty(&list_kill)) {
100885584672SEric Dumazet 		lock_sock(sk);
100985584672SEric Dumazet 		__skb_queue_purge(&list_kill);
101085584672SEric Dumazet 		sk_mem_reclaim_partial(sk);
101185584672SEric Dumazet 		release_sock(sk);
101285584672SEric Dumazet 	}
101385584672SEric Dumazet 	return res;
101485584672SEric Dumazet }
101585584672SEric Dumazet 
10161da177e4SLinus Torvalds /*
10171da177e4SLinus Torvalds  *	IOCTL requests applicable to the UDP protocol
10181da177e4SLinus Torvalds  */
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
10211da177e4SLinus Torvalds {
10226516c655SStephen Hemminger 	switch (cmd) {
10231da177e4SLinus Torvalds 	case SIOCOUTQ:
10241da177e4SLinus Torvalds 	{
102531e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
102631e6d363SEric Dumazet 
10271da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
10281da177e4SLinus Torvalds 	}
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	case SIOCINQ:
10311da177e4SLinus Torvalds 	{
103285584672SEric Dumazet 		unsigned int amount = first_packet_length(sk);
10331da177e4SLinus Torvalds 
103485584672SEric Dumazet 		if (amount)
10351da177e4SLinus Torvalds 			/*
10361da177e4SLinus Torvalds 			 * We will only return the amount
10371da177e4SLinus Torvalds 			 * of this packet since that is all
10381da177e4SLinus Torvalds 			 * that will be read.
10391da177e4SLinus Torvalds 			 */
104085584672SEric Dumazet 			amount -= sizeof(struct udphdr);
104185584672SEric Dumazet 
10421da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	default:
10461da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
10471da177e4SLinus Torvalds 	}
10486516c655SStephen Hemminger 
10496516c655SStephen Hemminger 	return 0;
10501da177e4SLinus Torvalds }
1051c482c568SEric Dumazet EXPORT_SYMBOL(udp_ioctl);
10521da177e4SLinus Torvalds 
1053db8dac20SDavid S. Miller /*
1054db8dac20SDavid S. Miller  * 	This should be easy, if there is something there we
1055db8dac20SDavid S. Miller  * 	return it, otherwise we block.
1056db8dac20SDavid S. Miller  */
1057db8dac20SDavid S. Miller 
1058db8dac20SDavid S. Miller int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1059db8dac20SDavid S. Miller 		size_t len, int noblock, int flags, int *addr_len)
1060db8dac20SDavid S. Miller {
1061db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1062db8dac20SDavid S. Miller 	struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
1063db8dac20SDavid S. Miller 	struct sk_buff *skb;
1064db8dac20SDavid S. Miller 	unsigned int ulen, copied;
1065db8dac20SDavid S. Miller 	int peeked;
1066db8dac20SDavid S. Miller 	int err;
1067db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1068db8dac20SDavid S. Miller 
1069db8dac20SDavid S. Miller 	/*
1070db8dac20SDavid S. Miller 	 *	Check any passed addresses
1071db8dac20SDavid S. Miller 	 */
1072db8dac20SDavid S. Miller 	if (addr_len)
1073db8dac20SDavid S. Miller 		*addr_len = sizeof(*sin);
1074db8dac20SDavid S. Miller 
1075db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
1076db8dac20SDavid S. Miller 		return ip_recv_error(sk, msg, len);
1077db8dac20SDavid S. Miller 
1078db8dac20SDavid S. Miller try_again:
1079db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
1080db8dac20SDavid S. Miller 				  &peeked, &err);
1081db8dac20SDavid S. Miller 	if (!skb)
1082db8dac20SDavid S. Miller 		goto out;
1083db8dac20SDavid S. Miller 
1084db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
1085db8dac20SDavid S. Miller 	copied = len;
1086db8dac20SDavid S. Miller 	if (copied > ulen)
1087db8dac20SDavid S. Miller 		copied = ulen;
1088db8dac20SDavid S. Miller 	else if (copied < ulen)
1089db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
1090db8dac20SDavid S. Miller 
1091db8dac20SDavid S. Miller 	/*
1092db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
1093db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
1094db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
1095db8dac20SDavid S. Miller 	 */
1096db8dac20SDavid S. Miller 
1097db8dac20SDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
1098db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
1099db8dac20SDavid S. Miller 			goto csum_copy_err;
1100db8dac20SDavid S. Miller 	}
1101db8dac20SDavid S. Miller 
1102db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
1103db8dac20SDavid S. Miller 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
1104db8dac20SDavid S. Miller 					      msg->msg_iov, copied);
1105db8dac20SDavid S. Miller 	else {
1106c482c568SEric Dumazet 		err = skb_copy_and_csum_datagram_iovec(skb,
1107c482c568SEric Dumazet 						       sizeof(struct udphdr),
1108c482c568SEric Dumazet 						       msg->msg_iov);
1109db8dac20SDavid S. Miller 
1110db8dac20SDavid S. Miller 		if (err == -EINVAL)
1111db8dac20SDavid S. Miller 			goto csum_copy_err;
1112db8dac20SDavid S. Miller 	}
1113db8dac20SDavid S. Miller 
1114db8dac20SDavid S. Miller 	if (err)
1115db8dac20SDavid S. Miller 		goto out_free;
1116db8dac20SDavid S. Miller 
1117db8dac20SDavid S. Miller 	if (!peeked)
1118629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk),
1119629ca23cSPavel Emelyanov 				UDP_MIB_INDATAGRAMS, is_udplite);
1120db8dac20SDavid S. Miller 
11213b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
1122db8dac20SDavid S. Miller 
1123db8dac20SDavid S. Miller 	/* Copy the address. */
1124c482c568SEric Dumazet 	if (sin) {
1125db8dac20SDavid S. Miller 		sin->sin_family = AF_INET;
1126db8dac20SDavid S. Miller 		sin->sin_port = udp_hdr(skb)->source;
1127db8dac20SDavid S. Miller 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1128db8dac20SDavid S. Miller 		memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1129db8dac20SDavid S. Miller 	}
1130db8dac20SDavid S. Miller 	if (inet->cmsg_flags)
1131db8dac20SDavid S. Miller 		ip_cmsg_recv(msg, skb);
1132db8dac20SDavid S. Miller 
1133db8dac20SDavid S. Miller 	err = copied;
1134db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
1135db8dac20SDavid S. Miller 		err = ulen;
1136db8dac20SDavid S. Miller 
1137db8dac20SDavid S. Miller out_free:
11389d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
1139db8dac20SDavid S. Miller out:
1140db8dac20SDavid S. Miller 	return err;
1141db8dac20SDavid S. Miller 
1142db8dac20SDavid S. Miller csum_copy_err:
1143db8dac20SDavid S. Miller 	lock_sock(sk);
1144db8dac20SDavid S. Miller 	if (!skb_kill_datagram(sk, skb, flags))
1145629ca23cSPavel Emelyanov 		UDP_INC_STATS_USER(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1146db8dac20SDavid S. Miller 	release_sock(sk);
1147db8dac20SDavid S. Miller 
1148db8dac20SDavid S. Miller 	if (noblock)
1149db8dac20SDavid S. Miller 		return -EAGAIN;
1150db8dac20SDavid S. Miller 	goto try_again;
1151db8dac20SDavid S. Miller }
1152db8dac20SDavid S. Miller 
1153db8dac20SDavid S. Miller 
11541da177e4SLinus Torvalds int udp_disconnect(struct sock *sk, int flags)
11551da177e4SLinus Torvalds {
11561da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
11571da177e4SLinus Torvalds 	/*
11581da177e4SLinus Torvalds 	 *	1003.1g - break association.
11591da177e4SLinus Torvalds 	 */
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	sk->sk_state = TCP_CLOSE;
1162c720c7e8SEric Dumazet 	inet->inet_daddr = 0;
1163c720c7e8SEric Dumazet 	inet->inet_dport = 0;
11641da177e4SLinus Torvalds 	sk->sk_bound_dev_if = 0;
11651da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
11661da177e4SLinus Torvalds 		inet_reset_saddr(sk);
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
11691da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1170c720c7e8SEric Dumazet 		inet->inet_sport = 0;
11711da177e4SLinus Torvalds 	}
11721da177e4SLinus Torvalds 	sk_dst_reset(sk);
11731da177e4SLinus Torvalds 	return 0;
11741da177e4SLinus Torvalds }
1175c482c568SEric Dumazet EXPORT_SYMBOL(udp_disconnect);
11761da177e4SLinus Torvalds 
1177645ca708SEric Dumazet void udp_lib_unhash(struct sock *sk)
1178645ca708SEric Dumazet {
1179723b4610SEric Dumazet 	if (sk_hashed(sk)) {
1180645ca708SEric Dumazet 		struct udp_table *udptable = sk->sk_prot->h.udp_table;
1181512615b6SEric Dumazet 		struct udp_hslot *hslot, *hslot2;
1182512615b6SEric Dumazet 
1183512615b6SEric Dumazet 		hslot  = udp_hashslot(udptable, sock_net(sk),
1184d4cada4aSEric Dumazet 				      udp_sk(sk)->udp_port_hash);
1185512615b6SEric Dumazet 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
1186645ca708SEric Dumazet 
1187c8db3fecSEric Dumazet 		spin_lock_bh(&hslot->lock);
118888ab1932SEric Dumazet 		if (sk_nulls_del_node_init_rcu(sk)) {
1189fdcc8aa9SEric Dumazet 			hslot->count--;
1190c720c7e8SEric Dumazet 			inet_sk(sk)->inet_num = 0;
1191645ca708SEric Dumazet 			sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
1192512615b6SEric Dumazet 
1193512615b6SEric Dumazet 			spin_lock(&hslot2->lock);
1194512615b6SEric Dumazet 			hlist_nulls_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
1195512615b6SEric Dumazet 			hslot2->count--;
1196512615b6SEric Dumazet 			spin_unlock(&hslot2->lock);
1197645ca708SEric Dumazet 		}
1198c8db3fecSEric Dumazet 		spin_unlock_bh(&hslot->lock);
1199645ca708SEric Dumazet 	}
1200723b4610SEric Dumazet }
1201645ca708SEric Dumazet EXPORT_SYMBOL(udp_lib_unhash);
1202645ca708SEric Dumazet 
120393821778SHerbert Xu static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
120493821778SHerbert Xu {
1205766e9037SEric Dumazet 	int rc = sock_queue_rcv_skb(sk, skb);
120693821778SHerbert Xu 
1207766e9037SEric Dumazet 	if (rc < 0) {
1208766e9037SEric Dumazet 		int is_udplite = IS_UDPLITE(sk);
1209766e9037SEric Dumazet 
121093821778SHerbert Xu 		/* Note that an ENOMEM error is charged twice */
1211766e9037SEric Dumazet 		if (rc == -ENOMEM)
121293821778SHerbert Xu 			UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
121393821778SHerbert Xu 					 is_udplite);
1214766e9037SEric Dumazet 		UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1215766e9037SEric Dumazet 		kfree_skb(skb);
1216766e9037SEric Dumazet 		return -1;
121793821778SHerbert Xu 	}
121893821778SHerbert Xu 
121993821778SHerbert Xu 	return 0;
122093821778SHerbert Xu 
122193821778SHerbert Xu }
122293821778SHerbert Xu 
1223db8dac20SDavid S. Miller /* returns:
1224db8dac20SDavid S. Miller  *  -1: error
1225db8dac20SDavid S. Miller  *   0: success
1226db8dac20SDavid S. Miller  *  >0: "udp encap" protocol resubmission
1227db8dac20SDavid S. Miller  *
1228db8dac20SDavid S. Miller  * Note that in the success and error cases, the skb is assumed to
1229db8dac20SDavid S. Miller  * have either been requeued or freed.
1230db8dac20SDavid S. Miller  */
1231db8dac20SDavid S. Miller int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1232db8dac20SDavid S. Miller {
1233db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1234db8dac20SDavid S. Miller 	int rc;
1235db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1236db8dac20SDavid S. Miller 
1237db8dac20SDavid S. Miller 	/*
1238db8dac20SDavid S. Miller 	 *	Charge it to the socket, dropping if the queue is full.
1239db8dac20SDavid S. Miller 	 */
1240db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
1241db8dac20SDavid S. Miller 		goto drop;
1242db8dac20SDavid S. Miller 	nf_reset(skb);
1243db8dac20SDavid S. Miller 
1244db8dac20SDavid S. Miller 	if (up->encap_type) {
1245db8dac20SDavid S. Miller 		/*
1246db8dac20SDavid S. Miller 		 * This is an encapsulation socket so pass the skb to
1247db8dac20SDavid S. Miller 		 * the socket's udp_encap_rcv() hook. Otherwise, just
1248db8dac20SDavid S. Miller 		 * fall through and pass this up the UDP socket.
1249db8dac20SDavid S. Miller 		 * up->encap_rcv() returns the following value:
1250db8dac20SDavid S. Miller 		 * =0 if skb was successfully passed to the encap
1251db8dac20SDavid S. Miller 		 *    handler or was discarded by it.
1252db8dac20SDavid S. Miller 		 * >0 if skb should be passed on to UDP.
1253db8dac20SDavid S. Miller 		 * <0 if skb should be resubmitted as proto -N
1254db8dac20SDavid S. Miller 		 */
1255db8dac20SDavid S. Miller 
1256db8dac20SDavid S. Miller 		/* if we're overly short, let UDP handle it */
1257db8dac20SDavid S. Miller 		if (skb->len > sizeof(struct udphdr) &&
1258db8dac20SDavid S. Miller 		    up->encap_rcv != NULL) {
1259db8dac20SDavid S. Miller 			int ret;
1260db8dac20SDavid S. Miller 
1261db8dac20SDavid S. Miller 			ret = (*up->encap_rcv)(sk, skb);
1262db8dac20SDavid S. Miller 			if (ret <= 0) {
12630283328eSPavel Emelyanov 				UDP_INC_STATS_BH(sock_net(sk),
12640283328eSPavel Emelyanov 						 UDP_MIB_INDATAGRAMS,
1265db8dac20SDavid S. Miller 						 is_udplite);
1266db8dac20SDavid S. Miller 				return -ret;
1267db8dac20SDavid S. Miller 			}
1268db8dac20SDavid S. Miller 		}
1269db8dac20SDavid S. Miller 
1270db8dac20SDavid S. Miller 		/* FALLTHROUGH -- it's a UDP Packet */
1271db8dac20SDavid S. Miller 	}
1272db8dac20SDavid S. Miller 
1273db8dac20SDavid S. Miller 	/*
1274db8dac20SDavid S. Miller 	 * 	UDP-Lite specific tests, ignored on UDP sockets
1275db8dac20SDavid S. Miller 	 */
1276db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
1277db8dac20SDavid S. Miller 
1278db8dac20SDavid S. Miller 		/*
1279db8dac20SDavid S. Miller 		 * MIB statistics other than incrementing the error count are
1280db8dac20SDavid S. Miller 		 * disabled for the following two types of errors: these depend
1281db8dac20SDavid S. Miller 		 * on the application settings, not on the functioning of the
1282db8dac20SDavid S. Miller 		 * protocol stack as such.
1283db8dac20SDavid S. Miller 		 *
1284db8dac20SDavid S. Miller 		 * RFC 3828 here recommends (sec 3.3): "There should also be a
1285db8dac20SDavid S. Miller 		 * way ... to ... at least let the receiving application block
1286db8dac20SDavid S. Miller 		 * delivery of packets with coverage values less than a value
1287db8dac20SDavid S. Miller 		 * provided by the application."
1288db8dac20SDavid S. Miller 		 */
1289db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
1290db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE: partial coverage "
1291db8dac20SDavid S. Miller 				"%d while full coverage %d requested\n",
1292db8dac20SDavid S. Miller 				UDP_SKB_CB(skb)->cscov, skb->len);
1293db8dac20SDavid S. Miller 			goto drop;
1294db8dac20SDavid S. Miller 		}
1295db8dac20SDavid S. Miller 		/* The next case involves violating the min. coverage requested
1296db8dac20SDavid S. Miller 		 * by the receiver. This is subtle: if receiver wants x and x is
1297db8dac20SDavid S. Miller 		 * greater than the buffersize/MTU then receiver will complain
1298db8dac20SDavid S. Miller 		 * that it wants x while sender emits packets of smaller size y.
1299db8dac20SDavid S. Miller 		 * Therefore the above ...()->partial_cov statement is essential.
1300db8dac20SDavid S. Miller 		 */
1301db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
1302db8dac20SDavid S. Miller 			LIMIT_NETDEBUG(KERN_WARNING
1303db8dac20SDavid S. Miller 				"UDPLITE: coverage %d too small, need min %d\n",
1304db8dac20SDavid S. Miller 				UDP_SKB_CB(skb)->cscov, up->pcrlen);
1305db8dac20SDavid S. Miller 			goto drop;
1306db8dac20SDavid S. Miller 		}
1307db8dac20SDavid S. Miller 	}
1308db8dac20SDavid S. Miller 
1309db8dac20SDavid S. Miller 	if (sk->sk_filter) {
1310db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
1311db8dac20SDavid S. Miller 			goto drop;
1312db8dac20SDavid S. Miller 	}
1313db8dac20SDavid S. Miller 
131493821778SHerbert Xu 	rc = 0;
1315db8dac20SDavid S. Miller 
131693821778SHerbert Xu 	bh_lock_sock(sk);
131793821778SHerbert Xu 	if (!sock_owned_by_user(sk))
131893821778SHerbert Xu 		rc = __udp_queue_rcv_skb(sk, skb);
131993821778SHerbert Xu 	else
132093821778SHerbert Xu 		sk_add_backlog(sk, skb);
132193821778SHerbert Xu 	bh_unlock_sock(sk);
132293821778SHerbert Xu 
132393821778SHerbert Xu 	return rc;
1324db8dac20SDavid S. Miller 
1325db8dac20SDavid S. Miller drop:
13260283328eSPavel Emelyanov 	UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
13278edf19c2SEric Dumazet 	atomic_inc(&sk->sk_drops);
1328db8dac20SDavid S. Miller 	kfree_skb(skb);
1329db8dac20SDavid S. Miller 	return -1;
1330db8dac20SDavid S. Miller }
1331db8dac20SDavid S. Miller 
1332db8dac20SDavid S. Miller /*
1333db8dac20SDavid S. Miller  *	Multicasts and broadcasts go to each listener.
1334db8dac20SDavid S. Miller  *
1335db8dac20SDavid S. Miller  *	Note: called only from the BH handler context,
1336db8dac20SDavid S. Miller  *	so we don't need to lock the hashes.
1337db8dac20SDavid S. Miller  */
1338e3163493SPavel Emelyanov static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
1339db8dac20SDavid S. Miller 				    struct udphdr  *uh,
1340db8dac20SDavid S. Miller 				    __be32 saddr, __be32 daddr,
1341645ca708SEric Dumazet 				    struct udp_table *udptable)
1342db8dac20SDavid S. Miller {
1343db8dac20SDavid S. Miller 	struct sock *sk;
1344f86dcc5aSEric Dumazet 	struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
1345db8dac20SDavid S. Miller 	int dif;
1346db8dac20SDavid S. Miller 
1347645ca708SEric Dumazet 	spin_lock(&hslot->lock);
134888ab1932SEric Dumazet 	sk = sk_nulls_head(&hslot->head);
1349db8dac20SDavid S. Miller 	dif = skb->dev->ifindex;
1350920a4611SEric Dumazet 	sk = udp_v4_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
1351db8dac20SDavid S. Miller 	if (sk) {
1352db8dac20SDavid S. Miller 		struct sock *sknext = NULL;
1353db8dac20SDavid S. Miller 
1354db8dac20SDavid S. Miller 		do {
1355db8dac20SDavid S. Miller 			struct sk_buff *skb1 = skb;
1356db8dac20SDavid S. Miller 
135788ab1932SEric Dumazet 			sknext = udp_v4_mcast_next(net, sk_nulls_next(sk), uh->dest,
1358920a4611SEric Dumazet 						   daddr, uh->source, saddr,
1359920a4611SEric Dumazet 						   dif);
1360db8dac20SDavid S. Miller 			if (sknext)
1361db8dac20SDavid S. Miller 				skb1 = skb_clone(skb, GFP_ATOMIC);
1362db8dac20SDavid S. Miller 
1363db8dac20SDavid S. Miller 			if (skb1) {
136493821778SHerbert Xu 				int ret = udp_queue_rcv_skb(sk, skb1);
1365db8dac20SDavid S. Miller 				if (ret > 0)
1366db8dac20SDavid S. Miller 					/* we should probably re-process instead
1367db8dac20SDavid S. Miller 					 * of dropping packets here. */
1368db8dac20SDavid S. Miller 					kfree_skb(skb1);
1369db8dac20SDavid S. Miller 			}
1370db8dac20SDavid S. Miller 			sk = sknext;
1371db8dac20SDavid S. Miller 		} while (sknext);
1372db8dac20SDavid S. Miller 	} else
1373ead2ceb0SNeil Horman 		consume_skb(skb);
1374645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
1375db8dac20SDavid S. Miller 	return 0;
1376db8dac20SDavid S. Miller }
1377db8dac20SDavid S. Miller 
1378db8dac20SDavid S. Miller /* Initialize UDP checksum. If exited with zero value (success),
1379db8dac20SDavid S. Miller  * CHECKSUM_UNNECESSARY means, that no more checks are required.
1380db8dac20SDavid S. Miller  * Otherwise, csum completion requires chacksumming packet body,
1381db8dac20SDavid S. Miller  * including udp header and folding it to skb->csum.
1382db8dac20SDavid S. Miller  */
1383db8dac20SDavid S. Miller static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
1384db8dac20SDavid S. Miller 				 int proto)
1385db8dac20SDavid S. Miller {
1386db8dac20SDavid S. Miller 	const struct iphdr *iph;
1387db8dac20SDavid S. Miller 	int err;
1388db8dac20SDavid S. Miller 
1389db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->partial_cov = 0;
1390db8dac20SDavid S. Miller 	UDP_SKB_CB(skb)->cscov = skb->len;
1391db8dac20SDavid S. Miller 
1392db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDPLITE) {
1393db8dac20SDavid S. Miller 		err = udplite_checksum_init(skb, uh);
1394db8dac20SDavid S. Miller 		if (err)
1395db8dac20SDavid S. Miller 			return err;
1396db8dac20SDavid S. Miller 	}
1397db8dac20SDavid S. Miller 
1398db8dac20SDavid S. Miller 	iph = ip_hdr(skb);
1399db8dac20SDavid S. Miller 	if (uh->check == 0) {
1400db8dac20SDavid S. Miller 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1401db8dac20SDavid S. Miller 	} else if (skb->ip_summed == CHECKSUM_COMPLETE) {
1402db8dac20SDavid S. Miller 		if (!csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
1403db8dac20SDavid S. Miller 				      proto, skb->csum))
1404db8dac20SDavid S. Miller 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1405db8dac20SDavid S. Miller 	}
1406db8dac20SDavid S. Miller 	if (!skb_csum_unnecessary(skb))
1407db8dac20SDavid S. Miller 		skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
1408db8dac20SDavid S. Miller 					       skb->len, proto, 0);
1409db8dac20SDavid S. Miller 	/* Probably, we should checksum udp header (it should be in cache
1410db8dac20SDavid S. Miller 	 * in any case) and data in tiny packets (< rx copybreak).
1411db8dac20SDavid S. Miller 	 */
1412db8dac20SDavid S. Miller 
1413db8dac20SDavid S. Miller 	return 0;
1414db8dac20SDavid S. Miller }
1415db8dac20SDavid S. Miller 
1416db8dac20SDavid S. Miller /*
1417db8dac20SDavid S. Miller  *	All we need to do is get the socket, and then do a checksum.
1418db8dac20SDavid S. Miller  */
1419db8dac20SDavid S. Miller 
1420645ca708SEric Dumazet int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1421db8dac20SDavid S. Miller 		   int proto)
1422db8dac20SDavid S. Miller {
1423db8dac20SDavid S. Miller 	struct sock *sk;
14247b5e56f9SJesper Dangaard Brouer 	struct udphdr *uh;
1425db8dac20SDavid S. Miller 	unsigned short ulen;
1426adf30907SEric Dumazet 	struct rtable *rt = skb_rtable(skb);
14272783ef23SJesper Dangaard Brouer 	__be32 saddr, daddr;
14280283328eSPavel Emelyanov 	struct net *net = dev_net(skb->dev);
1429db8dac20SDavid S. Miller 
1430db8dac20SDavid S. Miller 	/*
1431db8dac20SDavid S. Miller 	 *  Validate the packet.
1432db8dac20SDavid S. Miller 	 */
1433db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1434db8dac20SDavid S. Miller 		goto drop;		/* No space for header. */
1435db8dac20SDavid S. Miller 
14367b5e56f9SJesper Dangaard Brouer 	uh   = udp_hdr(skb);
1437db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
1438db8dac20SDavid S. Miller 	if (ulen > skb->len)
1439db8dac20SDavid S. Miller 		goto short_packet;
1440db8dac20SDavid S. Miller 
1441db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
1442db8dac20SDavid S. Miller 		/* UDP validates ulen. */
1443db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
1444db8dac20SDavid S. Miller 			goto short_packet;
1445db8dac20SDavid S. Miller 		uh = udp_hdr(skb);
1446db8dac20SDavid S. Miller 	}
1447db8dac20SDavid S. Miller 
1448db8dac20SDavid S. Miller 	if (udp4_csum_init(skb, uh, proto))
1449db8dac20SDavid S. Miller 		goto csum_error;
1450db8dac20SDavid S. Miller 
14512783ef23SJesper Dangaard Brouer 	saddr = ip_hdr(skb)->saddr;
14522783ef23SJesper Dangaard Brouer 	daddr = ip_hdr(skb)->daddr;
14532783ef23SJesper Dangaard Brouer 
1454db8dac20SDavid S. Miller 	if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1455e3163493SPavel Emelyanov 		return __udp4_lib_mcast_deliver(net, skb, uh,
1456e3163493SPavel Emelyanov 				saddr, daddr, udptable);
1457db8dac20SDavid S. Miller 
1458607c4aafSKOVACS Krisztian 	sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1459db8dac20SDavid S. Miller 
1460db8dac20SDavid S. Miller 	if (sk != NULL) {
146193821778SHerbert Xu 		int ret = udp_queue_rcv_skb(sk, skb);
1462db8dac20SDavid S. Miller 		sock_put(sk);
1463db8dac20SDavid S. Miller 
1464db8dac20SDavid S. Miller 		/* a return value > 0 means to resubmit the input, but
1465db8dac20SDavid S. Miller 		 * it wants the return to be -protocol, or 0
1466db8dac20SDavid S. Miller 		 */
1467db8dac20SDavid S. Miller 		if (ret > 0)
1468db8dac20SDavid S. Miller 			return -ret;
1469db8dac20SDavid S. Miller 		return 0;
1470db8dac20SDavid S. Miller 	}
1471db8dac20SDavid S. Miller 
1472db8dac20SDavid S. Miller 	if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1473db8dac20SDavid S. Miller 		goto drop;
1474db8dac20SDavid S. Miller 	nf_reset(skb);
1475db8dac20SDavid S. Miller 
1476db8dac20SDavid S. Miller 	/* No socket. Drop packet silently, if checksum is wrong */
1477db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
1478db8dac20SDavid S. Miller 		goto csum_error;
1479db8dac20SDavid S. Miller 
14800283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
1481db8dac20SDavid S. Miller 	icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
1482db8dac20SDavid S. Miller 
1483db8dac20SDavid S. Miller 	/*
1484db8dac20SDavid S. Miller 	 * Hmm.  We got an UDP packet to a port to which we
1485db8dac20SDavid S. Miller 	 * don't wanna listen.  Ignore it.
1486db8dac20SDavid S. Miller 	 */
1487db8dac20SDavid S. Miller 	kfree_skb(skb);
1488db8dac20SDavid S. Miller 	return 0;
1489db8dac20SDavid S. Miller 
1490db8dac20SDavid S. Miller short_packet:
1491673d57e7SHarvey Harrison 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
1492db8dac20SDavid S. Miller 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
1493673d57e7SHarvey Harrison 		       &saddr,
1494db8dac20SDavid S. Miller 		       ntohs(uh->source),
1495db8dac20SDavid S. Miller 		       ulen,
1496db8dac20SDavid S. Miller 		       skb->len,
1497673d57e7SHarvey Harrison 		       &daddr,
1498db8dac20SDavid S. Miller 		       ntohs(uh->dest));
1499db8dac20SDavid S. Miller 	goto drop;
1500db8dac20SDavid S. Miller 
1501db8dac20SDavid S. Miller csum_error:
1502db8dac20SDavid S. Miller 	/*
1503db8dac20SDavid S. Miller 	 * RFC1122: OK.  Discards the bad packet silently (as far as
1504db8dac20SDavid S. Miller 	 * the network is concerned, anyway) as per 4.1.3.4 (MUST).
1505db8dac20SDavid S. Miller 	 */
1506673d57e7SHarvey Harrison 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
1507db8dac20SDavid S. Miller 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
1508673d57e7SHarvey Harrison 		       &saddr,
1509db8dac20SDavid S. Miller 		       ntohs(uh->source),
1510673d57e7SHarvey Harrison 		       &daddr,
1511db8dac20SDavid S. Miller 		       ntohs(uh->dest),
1512db8dac20SDavid S. Miller 		       ulen);
1513db8dac20SDavid S. Miller drop:
15140283328eSPavel Emelyanov 	UDP_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1515db8dac20SDavid S. Miller 	kfree_skb(skb);
1516db8dac20SDavid S. Miller 	return 0;
1517db8dac20SDavid S. Miller }
1518db8dac20SDavid S. Miller 
1519db8dac20SDavid S. Miller int udp_rcv(struct sk_buff *skb)
1520db8dac20SDavid S. Miller {
1521645ca708SEric Dumazet 	return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1522db8dac20SDavid S. Miller }
1523db8dac20SDavid S. Miller 
15247d06b2e0SBrian Haley void udp_destroy_sock(struct sock *sk)
1525db8dac20SDavid S. Miller {
1526db8dac20SDavid S. Miller 	lock_sock(sk);
1527db8dac20SDavid S. Miller 	udp_flush_pending_frames(sk);
1528db8dac20SDavid S. Miller 	release_sock(sk);
1529db8dac20SDavid S. Miller }
1530db8dac20SDavid S. Miller 
15311da177e4SLinus Torvalds /*
15321da177e4SLinus Torvalds  *	Socket option code for UDP
15331da177e4SLinus Torvalds  */
15344c0a6cb0SGerrit Renker int udp_lib_setsockopt(struct sock *sk, int level, int optname,
1535b7058842SDavid S. Miller 		       char __user *optval, unsigned int optlen,
15364c0a6cb0SGerrit Renker 		       int (*push_pending_frames)(struct sock *))
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
15391da177e4SLinus Torvalds 	int val;
15401da177e4SLinus Torvalds 	int err = 0;
1541b2bf1e26SWang Chen 	int is_udplite = IS_UDPLITE(sk);
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	if (optlen < sizeof(int))
15441da177e4SLinus Torvalds 		return -EINVAL;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
15471da177e4SLinus Torvalds 		return -EFAULT;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	switch (optname) {
15501da177e4SLinus Torvalds 	case UDP_CORK:
15511da177e4SLinus Torvalds 		if (val != 0) {
15521da177e4SLinus Torvalds 			up->corkflag = 1;
15531da177e4SLinus Torvalds 		} else {
15541da177e4SLinus Torvalds 			up->corkflag = 0;
15551da177e4SLinus Torvalds 			lock_sock(sk);
15564c0a6cb0SGerrit Renker 			(*push_pending_frames)(sk);
15571da177e4SLinus Torvalds 			release_sock(sk);
15581da177e4SLinus Torvalds 		}
15591da177e4SLinus Torvalds 		break;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	case UDP_ENCAP:
15621da177e4SLinus Torvalds 		switch (val) {
15631da177e4SLinus Torvalds 		case 0:
15641da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP:
15651da177e4SLinus Torvalds 		case UDP_ENCAP_ESPINUDP_NON_IKE:
1566067b207bSJames Chapman 			up->encap_rcv = xfrm4_udp_encap_rcv;
1567067b207bSJames Chapman 			/* FALLTHROUGH */
1568342f0234SJames Chapman 		case UDP_ENCAP_L2TPINUDP:
15691da177e4SLinus Torvalds 			up->encap_type = val;
15701da177e4SLinus Torvalds 			break;
15711da177e4SLinus Torvalds 		default:
15721da177e4SLinus Torvalds 			err = -ENOPROTOOPT;
15731da177e4SLinus Torvalds 			break;
15741da177e4SLinus Torvalds 		}
15751da177e4SLinus Torvalds 		break;
15761da177e4SLinus Torvalds 
1577ba4e58ecSGerrit Renker 	/*
1578ba4e58ecSGerrit Renker 	 * 	UDP-Lite's partial checksum coverage (RFC 3828).
1579ba4e58ecSGerrit Renker 	 */
1580ba4e58ecSGerrit Renker 	/* The sender sets actual checksum coverage length via this option.
1581ba4e58ecSGerrit Renker 	 * The case coverage > packet length is handled by send module. */
1582ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
1583b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
1584ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
1585ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Illegal coverage: use default (8) */
1586ba4e58ecSGerrit Renker 			val = 8;
158747112e25SGerrit Renker 		else if (val > USHORT_MAX)
158847112e25SGerrit Renker 			val = USHORT_MAX;
1589ba4e58ecSGerrit Renker 		up->pcslen = val;
1590ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_SEND_CC;
1591ba4e58ecSGerrit Renker 		break;
1592ba4e58ecSGerrit Renker 
1593ba4e58ecSGerrit Renker 	/* The receiver specifies a minimum checksum coverage value. To make
1594ba4e58ecSGerrit Renker 	 * sense, this should be set to at least 8 (as done below). If zero is
1595ba4e58ecSGerrit Renker 	 * used, this again means full checksum coverage.                     */
1596ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
1597b2bf1e26SWang Chen 		if (!is_udplite)         /* Disable the option on UDP sockets */
1598ba4e58ecSGerrit Renker 			return -ENOPROTOOPT;
1599ba4e58ecSGerrit Renker 		if (val != 0 && val < 8) /* Avoid silly minimal values.       */
1600ba4e58ecSGerrit Renker 			val = 8;
160147112e25SGerrit Renker 		else if (val > USHORT_MAX)
160247112e25SGerrit Renker 			val = USHORT_MAX;
1603ba4e58ecSGerrit Renker 		up->pcrlen = val;
1604ba4e58ecSGerrit Renker 		up->pcflag |= UDPLITE_RECV_CC;
1605ba4e58ecSGerrit Renker 		break;
1606ba4e58ecSGerrit Renker 
16071da177e4SLinus Torvalds 	default:
16081da177e4SLinus Torvalds 		err = -ENOPROTOOPT;
16091da177e4SLinus Torvalds 		break;
16106516c655SStephen Hemminger 	}
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	return err;
16131da177e4SLinus Torvalds }
1614c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_setsockopt);
16151da177e4SLinus Torvalds 
1616db8dac20SDavid S. Miller int udp_setsockopt(struct sock *sk, int level, int optname,
1617b7058842SDavid S. Miller 		   char __user *optval, unsigned int optlen)
1618db8dac20SDavid S. Miller {
1619db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1620db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1621db8dac20SDavid S. Miller 					  udp_push_pending_frames);
1622db8dac20SDavid S. Miller 	return ip_setsockopt(sk, level, optname, optval, optlen);
1623db8dac20SDavid S. Miller }
1624db8dac20SDavid S. Miller 
1625db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1626db8dac20SDavid S. Miller int compat_udp_setsockopt(struct sock *sk, int level, int optname,
1627b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
1628db8dac20SDavid S. Miller {
1629db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1630db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1631db8dac20SDavid S. Miller 					  udp_push_pending_frames);
1632db8dac20SDavid S. Miller 	return compat_ip_setsockopt(sk, level, optname, optval, optlen);
1633db8dac20SDavid S. Miller }
1634db8dac20SDavid S. Miller #endif
1635db8dac20SDavid S. Miller 
16364c0a6cb0SGerrit Renker int udp_lib_getsockopt(struct sock *sk, int level, int optname,
16371da177e4SLinus Torvalds 		       char __user *optval, int __user *optlen)
16381da177e4SLinus Torvalds {
16391da177e4SLinus Torvalds 	struct udp_sock *up = udp_sk(sk);
16401da177e4SLinus Torvalds 	int val, len;
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 	if (get_user(len, optlen))
16431da177e4SLinus Torvalds 		return -EFAULT;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	if (len < 0)
16481da177e4SLinus Torvalds 		return -EINVAL;
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	switch (optname) {
16511da177e4SLinus Torvalds 	case UDP_CORK:
16521da177e4SLinus Torvalds 		val = up->corkflag;
16531da177e4SLinus Torvalds 		break;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	case UDP_ENCAP:
16561da177e4SLinus Torvalds 		val = up->encap_type;
16571da177e4SLinus Torvalds 		break;
16581da177e4SLinus Torvalds 
1659ba4e58ecSGerrit Renker 	/* The following two cannot be changed on UDP sockets, the return is
1660ba4e58ecSGerrit Renker 	 * always 0 (which corresponds to the full checksum coverage of UDP). */
1661ba4e58ecSGerrit Renker 	case UDPLITE_SEND_CSCOV:
1662ba4e58ecSGerrit Renker 		val = up->pcslen;
1663ba4e58ecSGerrit Renker 		break;
1664ba4e58ecSGerrit Renker 
1665ba4e58ecSGerrit Renker 	case UDPLITE_RECV_CSCOV:
1666ba4e58ecSGerrit Renker 		val = up->pcrlen;
1667ba4e58ecSGerrit Renker 		break;
1668ba4e58ecSGerrit Renker 
16691da177e4SLinus Torvalds 	default:
16701da177e4SLinus Torvalds 		return -ENOPROTOOPT;
16716516c655SStephen Hemminger 	}
16721da177e4SLinus Torvalds 
16731da177e4SLinus Torvalds 	if (put_user(len, optlen))
16741da177e4SLinus Torvalds 		return -EFAULT;
16751da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
16761da177e4SLinus Torvalds 		return -EFAULT;
16771da177e4SLinus Torvalds 	return 0;
16781da177e4SLinus Torvalds }
1679c482c568SEric Dumazet EXPORT_SYMBOL(udp_lib_getsockopt);
16801da177e4SLinus Torvalds 
1681db8dac20SDavid S. Miller int udp_getsockopt(struct sock *sk, int level, int optname,
1682db8dac20SDavid S. Miller 		   char __user *optval, int __user *optlen)
1683db8dac20SDavid S. Miller {
1684db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1685db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1686db8dac20SDavid S. Miller 	return ip_getsockopt(sk, level, optname, optval, optlen);
1687db8dac20SDavid S. Miller }
1688db8dac20SDavid S. Miller 
1689db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1690db8dac20SDavid S. Miller int compat_udp_getsockopt(struct sock *sk, int level, int optname,
1691db8dac20SDavid S. Miller 				 char __user *optval, int __user *optlen)
1692db8dac20SDavid S. Miller {
1693db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1694db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1695db8dac20SDavid S. Miller 	return compat_ip_getsockopt(sk, level, optname, optval, optlen);
1696db8dac20SDavid S. Miller }
1697db8dac20SDavid S. Miller #endif
16981da177e4SLinus Torvalds /**
16991da177e4SLinus Torvalds  * 	udp_poll - wait for a UDP event.
17001da177e4SLinus Torvalds  *	@file - file struct
17011da177e4SLinus Torvalds  *	@sock - socket
17021da177e4SLinus Torvalds  *	@wait - poll table
17031da177e4SLinus Torvalds  *
17041da177e4SLinus Torvalds  *	This is same as datagram poll, except for the special case of
17051da177e4SLinus Torvalds  *	blocking sockets. If application is using a blocking fd
17061da177e4SLinus Torvalds  *	and a packet with checksum error is in the queue;
17071da177e4SLinus Torvalds  *	then it could get return from select indicating data available
17081da177e4SLinus Torvalds  *	but then block when reading it. Add special case code
17091da177e4SLinus Torvalds  *	to work around these arguably broken applications.
17101da177e4SLinus Torvalds  */
17111da177e4SLinus Torvalds unsigned int udp_poll(struct file *file, struct socket *sock, poll_table *wait)
17121da177e4SLinus Torvalds {
17131da177e4SLinus Torvalds 	unsigned int mask = datagram_poll(file, sock, wait);
17141da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	/* Check for false positives due to checksum errors */
171785584672SEric Dumazet 	if ((mask & POLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
171885584672SEric Dumazet 	    !(sk->sk_shutdown & RCV_SHUTDOWN) && !first_packet_length(sk))
17191da177e4SLinus Torvalds 		mask &= ~(POLLIN | POLLRDNORM);
17201da177e4SLinus Torvalds 
17211da177e4SLinus Torvalds 	return mask;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds }
1724c482c568SEric Dumazet EXPORT_SYMBOL(udp_poll);
17251da177e4SLinus Torvalds 
1726db8dac20SDavid S. Miller struct proto udp_prot = {
1727db8dac20SDavid S. Miller 	.name		   = "UDP",
1728db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1729db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1730db8dac20SDavid S. Miller 	.connect	   = ip4_datagram_connect,
1731db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1732db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1733db8dac20SDavid S. Miller 	.destroy	   = udp_destroy_sock,
1734db8dac20SDavid S. Miller 	.setsockopt	   = udp_setsockopt,
1735db8dac20SDavid S. Miller 	.getsockopt	   = udp_getsockopt,
1736db8dac20SDavid S. Miller 	.sendmsg	   = udp_sendmsg,
1737db8dac20SDavid S. Miller 	.recvmsg	   = udp_recvmsg,
1738db8dac20SDavid S. Miller 	.sendpage	   = udp_sendpage,
173993821778SHerbert Xu 	.backlog_rcv	   = __udp_queue_rcv_skb,
1740db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1741db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1742db8dac20SDavid S. Miller 	.get_port	   = udp_v4_get_port,
1743db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1744db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1745db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1746db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1747db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp_sock),
1748271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1749645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1750db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1751db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udp_setsockopt,
1752db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udp_getsockopt,
1753db8dac20SDavid S. Miller #endif
1754db8dac20SDavid S. Miller };
1755c482c568SEric Dumazet EXPORT_SYMBOL(udp_prot);
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
17581da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
17591da177e4SLinus Torvalds 
1760645ca708SEric Dumazet static struct sock *udp_get_first(struct seq_file *seq, int start)
17611da177e4SLinus Torvalds {
17621da177e4SLinus Torvalds 	struct sock *sk;
17631da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
17646f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
17651da177e4SLinus Torvalds 
1766f86dcc5aSEric Dumazet 	for (state->bucket = start; state->bucket <= state->udp_table->mask;
1767f86dcc5aSEric Dumazet 	     ++state->bucket) {
176888ab1932SEric Dumazet 		struct hlist_nulls_node *node;
1769645ca708SEric Dumazet 		struct udp_hslot *hslot = &state->udp_table->hash[state->bucket];
1770f86dcc5aSEric Dumazet 
1771f86dcc5aSEric Dumazet 		if (hlist_nulls_empty(&hslot->head))
1772f86dcc5aSEric Dumazet 			continue;
1773f86dcc5aSEric Dumazet 
1774645ca708SEric Dumazet 		spin_lock_bh(&hslot->lock);
177588ab1932SEric Dumazet 		sk_nulls_for_each(sk, node, &hslot->head) {
1776878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
1777a91275efSDaniel Lezcano 				continue;
17781da177e4SLinus Torvalds 			if (sk->sk_family == state->family)
17791da177e4SLinus Torvalds 				goto found;
17801da177e4SLinus Torvalds 		}
1781645ca708SEric Dumazet 		spin_unlock_bh(&hslot->lock);
17821da177e4SLinus Torvalds 	}
17831da177e4SLinus Torvalds 	sk = NULL;
17841da177e4SLinus Torvalds found:
17851da177e4SLinus Torvalds 	return sk;
17861da177e4SLinus Torvalds }
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
17891da177e4SLinus Torvalds {
17901da177e4SLinus Torvalds 	struct udp_iter_state *state = seq->private;
17916f191efeSDenis V. Lunev 	struct net *net = seq_file_net(seq);
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	do {
179488ab1932SEric Dumazet 		sk = sk_nulls_next(sk);
1795878628fbSYOSHIFUJI Hideaki 	} while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != state->family));
17961da177e4SLinus Torvalds 
1797645ca708SEric Dumazet 	if (!sk) {
1798f86dcc5aSEric Dumazet 		if (state->bucket <= state->udp_table->mask)
1799f52b5054SEric Dumazet 			spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
1800645ca708SEric Dumazet 		return udp_get_first(seq, state->bucket + 1);
18011da177e4SLinus Torvalds 	}
18021da177e4SLinus Torvalds 	return sk;
18031da177e4SLinus Torvalds }
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
18061da177e4SLinus Torvalds {
1807645ca708SEric Dumazet 	struct sock *sk = udp_get_first(seq, 0);
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds 	if (sk)
18101da177e4SLinus Torvalds 		while (pos && (sk = udp_get_next(seq, sk)) != NULL)
18111da177e4SLinus Torvalds 			--pos;
18121da177e4SLinus Torvalds 	return pos ? NULL : sk;
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds static void *udp_seq_start(struct seq_file *seq, loff_t *pos)
18161da177e4SLinus Torvalds {
181730842f29SVitaly Mayatskikh 	struct udp_iter_state *state = seq->private;
1818f86dcc5aSEric Dumazet 	state->bucket = MAX_UDP_PORTS;
181930842f29SVitaly Mayatskikh 
1820b50660f1SYOSHIFUJI Hideaki 	return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds static void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
18241da177e4SLinus Torvalds {
18251da177e4SLinus Torvalds 	struct sock *sk;
18261da177e4SLinus Torvalds 
1827b50660f1SYOSHIFUJI Hideaki 	if (v == SEQ_START_TOKEN)
18281da177e4SLinus Torvalds 		sk = udp_get_idx(seq, 0);
18291da177e4SLinus Torvalds 	else
18301da177e4SLinus Torvalds 		sk = udp_get_next(seq, v);
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	++*pos;
18331da177e4SLinus Torvalds 	return sk;
18341da177e4SLinus Torvalds }
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds static void udp_seq_stop(struct seq_file *seq, void *v)
18371da177e4SLinus Torvalds {
1838645ca708SEric Dumazet 	struct udp_iter_state *state = seq->private;
1839645ca708SEric Dumazet 
1840f86dcc5aSEric Dumazet 	if (state->bucket <= state->udp_table->mask)
1841645ca708SEric Dumazet 		spin_unlock_bh(&state->udp_table->hash[state->bucket].lock);
18421da177e4SLinus Torvalds }
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds static int udp_seq_open(struct inode *inode, struct file *file)
18451da177e4SLinus Torvalds {
18461da177e4SLinus Torvalds 	struct udp_seq_afinfo *afinfo = PDE(inode)->data;
1847a2be75c1SDenis V. Lunev 	struct udp_iter_state *s;
1848a2be75c1SDenis V. Lunev 	int err;
18491da177e4SLinus Torvalds 
1850a2be75c1SDenis V. Lunev 	err = seq_open_net(inode, file, &afinfo->seq_ops,
1851a2be75c1SDenis V. Lunev 			   sizeof(struct udp_iter_state));
1852a2be75c1SDenis V. Lunev 	if (err < 0)
1853a2be75c1SDenis V. Lunev 		return err;
1854a91275efSDaniel Lezcano 
1855a2be75c1SDenis V. Lunev 	s = ((struct seq_file *)file->private_data)->private;
18561da177e4SLinus Torvalds 	s->family		= afinfo->family;
1857645ca708SEric Dumazet 	s->udp_table		= afinfo->udp_table;
1858a2be75c1SDenis V. Lunev 	return err;
1859a91275efSDaniel Lezcano }
1860a91275efSDaniel Lezcano 
18611da177e4SLinus Torvalds /* ------------------------------------------------------------------------ */
18620c96d8c5SDaniel Lezcano int udp_proc_register(struct net *net, struct udp_seq_afinfo *afinfo)
18631da177e4SLinus Torvalds {
18641da177e4SLinus Torvalds 	struct proc_dir_entry *p;
18651da177e4SLinus Torvalds 	int rc = 0;
18661da177e4SLinus Torvalds 
18673ba9441bSDenis V. Lunev 	afinfo->seq_fops.open		= udp_seq_open;
18683ba9441bSDenis V. Lunev 	afinfo->seq_fops.read		= seq_read;
18693ba9441bSDenis V. Lunev 	afinfo->seq_fops.llseek		= seq_lseek;
18703ba9441bSDenis V. Lunev 	afinfo->seq_fops.release	= seq_release_net;
18711da177e4SLinus Torvalds 
1872dda61925SDenis V. Lunev 	afinfo->seq_ops.start		= udp_seq_start;
1873dda61925SDenis V. Lunev 	afinfo->seq_ops.next		= udp_seq_next;
1874dda61925SDenis V. Lunev 	afinfo->seq_ops.stop		= udp_seq_stop;
1875dda61925SDenis V. Lunev 
187684841c3cSDenis V. Lunev 	p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
187784841c3cSDenis V. Lunev 			     &afinfo->seq_fops, afinfo);
187884841c3cSDenis V. Lunev 	if (!p)
18791da177e4SLinus Torvalds 		rc = -ENOMEM;
18801da177e4SLinus Torvalds 	return rc;
18811da177e4SLinus Torvalds }
1882c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_register);
18831da177e4SLinus Torvalds 
18840c96d8c5SDaniel Lezcano void udp_proc_unregister(struct net *net, struct udp_seq_afinfo *afinfo)
18851da177e4SLinus Torvalds {
18860c96d8c5SDaniel Lezcano 	proc_net_remove(net, afinfo->name);
18871da177e4SLinus Torvalds }
1888c482c568SEric Dumazet EXPORT_SYMBOL(udp_proc_unregister);
1889db8dac20SDavid S. Miller 
1890db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
18915e659e4cSPavel Emelyanov static void udp4_format_sock(struct sock *sp, struct seq_file *f,
18925e659e4cSPavel Emelyanov 		int bucket, int *len)
1893db8dac20SDavid S. Miller {
1894db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sp);
1895c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr;
1896c720c7e8SEric Dumazet 	__be32 src  = inet->inet_rcv_saddr;
1897c720c7e8SEric Dumazet 	__u16 destp	  = ntohs(inet->inet_dport);
1898c720c7e8SEric Dumazet 	__u16 srcp	  = ntohs(inet->inet_sport);
1899db8dac20SDavid S. Miller 
1900f86dcc5aSEric Dumazet 	seq_printf(f, "%5d: %08X:%04X %08X:%04X"
1901cb61cb9bSEric Dumazet 		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d%n",
1902db8dac20SDavid S. Miller 		bucket, src, srcp, dest, destp, sp->sk_state,
190331e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
190431e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
1905db8dac20SDavid S. Miller 		0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
1906cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_refcnt), sp,
1907cb61cb9bSEric Dumazet 		atomic_read(&sp->sk_drops), len);
1908db8dac20SDavid S. Miller }
1909db8dac20SDavid S. Miller 
1910db8dac20SDavid S. Miller int udp4_seq_show(struct seq_file *seq, void *v)
1911db8dac20SDavid S. Miller {
1912db8dac20SDavid S. Miller 	if (v == SEQ_START_TOKEN)
1913db8dac20SDavid S. Miller 		seq_printf(seq, "%-127s\n",
1914db8dac20SDavid S. Miller 			   "  sl  local_address rem_address   st tx_queue "
1915db8dac20SDavid S. Miller 			   "rx_queue tr tm->when retrnsmt   uid  timeout "
1916cb61cb9bSEric Dumazet 			   "inode ref pointer drops");
1917db8dac20SDavid S. Miller 	else {
1918db8dac20SDavid S. Miller 		struct udp_iter_state *state = seq->private;
19195e659e4cSPavel Emelyanov 		int len;
1920db8dac20SDavid S. Miller 
19215e659e4cSPavel Emelyanov 		udp4_format_sock(v, seq, state->bucket, &len);
19225e659e4cSPavel Emelyanov 		seq_printf(seq, "%*s\n", 127 - len, "");
1923db8dac20SDavid S. Miller 	}
1924db8dac20SDavid S. Miller 	return 0;
1925db8dac20SDavid S. Miller }
1926db8dac20SDavid S. Miller 
1927db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1928db8dac20SDavid S. Miller static struct udp_seq_afinfo udp4_seq_afinfo = {
1929db8dac20SDavid S. Miller 	.name		= "udp",
1930db8dac20SDavid S. Miller 	.family		= AF_INET,
1931645ca708SEric Dumazet 	.udp_table	= &udp_table,
19324ad96d39SDenis V. Lunev 	.seq_fops	= {
19334ad96d39SDenis V. Lunev 		.owner	=	THIS_MODULE,
19344ad96d39SDenis V. Lunev 	},
1935dda61925SDenis V. Lunev 	.seq_ops	= {
1936dda61925SDenis V. Lunev 		.show		= udp4_seq_show,
1937dda61925SDenis V. Lunev 	},
1938db8dac20SDavid S. Miller };
1939db8dac20SDavid S. Miller 
194015439febSPavel Emelyanov static int udp4_proc_init_net(struct net *net)
194115439febSPavel Emelyanov {
194215439febSPavel Emelyanov 	return udp_proc_register(net, &udp4_seq_afinfo);
194315439febSPavel Emelyanov }
194415439febSPavel Emelyanov 
194515439febSPavel Emelyanov static void udp4_proc_exit_net(struct net *net)
194615439febSPavel Emelyanov {
194715439febSPavel Emelyanov 	udp_proc_unregister(net, &udp4_seq_afinfo);
194815439febSPavel Emelyanov }
194915439febSPavel Emelyanov 
195015439febSPavel Emelyanov static struct pernet_operations udp4_net_ops = {
195115439febSPavel Emelyanov 	.init = udp4_proc_init_net,
195215439febSPavel Emelyanov 	.exit = udp4_proc_exit_net,
195315439febSPavel Emelyanov };
195415439febSPavel Emelyanov 
1955db8dac20SDavid S. Miller int __init udp4_proc_init(void)
1956db8dac20SDavid S. Miller {
195715439febSPavel Emelyanov 	return register_pernet_subsys(&udp4_net_ops);
1958db8dac20SDavid S. Miller }
1959db8dac20SDavid S. Miller 
1960db8dac20SDavid S. Miller void udp4_proc_exit(void)
1961db8dac20SDavid S. Miller {
196215439febSPavel Emelyanov 	unregister_pernet_subsys(&udp4_net_ops);
1963db8dac20SDavid S. Miller }
19641da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
19651da177e4SLinus Torvalds 
1966f86dcc5aSEric Dumazet static __initdata unsigned long uhash_entries;
1967f86dcc5aSEric Dumazet static int __init set_uhash_entries(char *str)
1968645ca708SEric Dumazet {
1969f86dcc5aSEric Dumazet 	if (!str)
1970f86dcc5aSEric Dumazet 		return 0;
1971f86dcc5aSEric Dumazet 	uhash_entries = simple_strtoul(str, &str, 0);
1972f86dcc5aSEric Dumazet 	if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
1973f86dcc5aSEric Dumazet 		uhash_entries = UDP_HTABLE_SIZE_MIN;
1974f86dcc5aSEric Dumazet 	return 1;
1975f86dcc5aSEric Dumazet }
1976f86dcc5aSEric Dumazet __setup("uhash_entries=", set_uhash_entries);
1977645ca708SEric Dumazet 
1978f86dcc5aSEric Dumazet void __init udp_table_init(struct udp_table *table, const char *name)
1979f86dcc5aSEric Dumazet {
1980f86dcc5aSEric Dumazet 	unsigned int i;
1981f86dcc5aSEric Dumazet 
1982f86dcc5aSEric Dumazet 	if (!CONFIG_BASE_SMALL)
1983f86dcc5aSEric Dumazet 		table->hash = alloc_large_system_hash(name,
1984512615b6SEric Dumazet 			2 * sizeof(struct udp_hslot),
1985f86dcc5aSEric Dumazet 			uhash_entries,
1986f86dcc5aSEric Dumazet 			21, /* one slot per 2 MB */
1987f86dcc5aSEric Dumazet 			0,
1988f86dcc5aSEric Dumazet 			&table->log,
1989f86dcc5aSEric Dumazet 			&table->mask,
1990f86dcc5aSEric Dumazet 			64 * 1024);
1991f86dcc5aSEric Dumazet 	/*
1992f86dcc5aSEric Dumazet 	 * Make sure hash table has the minimum size
1993f86dcc5aSEric Dumazet 	 */
1994f86dcc5aSEric Dumazet 	if (CONFIG_BASE_SMALL || table->mask < UDP_HTABLE_SIZE_MIN - 1) {
1995f86dcc5aSEric Dumazet 		table->hash = kmalloc(UDP_HTABLE_SIZE_MIN *
1996512615b6SEric Dumazet 				      2 * sizeof(struct udp_hslot), GFP_KERNEL);
1997f86dcc5aSEric Dumazet 		if (!table->hash)
1998f86dcc5aSEric Dumazet 			panic(name);
1999f86dcc5aSEric Dumazet 		table->log = ilog2(UDP_HTABLE_SIZE_MIN);
2000f86dcc5aSEric Dumazet 		table->mask = UDP_HTABLE_SIZE_MIN - 1;
2001f86dcc5aSEric Dumazet 	}
2002512615b6SEric Dumazet 	table->hash2 = table->hash + (table->mask + 1);
2003f86dcc5aSEric Dumazet 	for (i = 0; i <= table->mask; i++) {
200488ab1932SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash[i].head, i);
2005fdcc8aa9SEric Dumazet 		table->hash[i].count = 0;
2006645ca708SEric Dumazet 		spin_lock_init(&table->hash[i].lock);
2007645ca708SEric Dumazet 	}
2008512615b6SEric Dumazet 	for (i = 0; i <= table->mask; i++) {
2009512615b6SEric Dumazet 		INIT_HLIST_NULLS_HEAD(&table->hash2[i].head, i);
2010512615b6SEric Dumazet 		table->hash2[i].count = 0;
2011512615b6SEric Dumazet 		spin_lock_init(&table->hash2[i].lock);
2012512615b6SEric Dumazet 	}
2013645ca708SEric Dumazet }
2014645ca708SEric Dumazet 
201595766fffSHideo Aoki void __init udp_init(void)
201695766fffSHideo Aoki {
20178203efb3SEric Dumazet 	unsigned long nr_pages, limit;
201895766fffSHideo Aoki 
2019f86dcc5aSEric Dumazet 	udp_table_init(&udp_table, "UDP");
202095766fffSHideo Aoki 	/* Set the pressure threshold up by the same strategy of TCP. It is a
202195766fffSHideo Aoki 	 * fraction of global memory that is up to 1/2 at 256 MB, decreasing
202295766fffSHideo Aoki 	 * toward zero with the amount of memory, with a floor of 128 pages.
202395766fffSHideo Aoki 	 */
20248203efb3SEric Dumazet 	nr_pages = totalram_pages - totalhigh_pages;
20258203efb3SEric Dumazet 	limit = min(nr_pages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
20268203efb3SEric Dumazet 	limit = (limit * (nr_pages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
202795766fffSHideo Aoki 	limit = max(limit, 128UL);
202895766fffSHideo Aoki 	sysctl_udp_mem[0] = limit / 4 * 3;
202995766fffSHideo Aoki 	sysctl_udp_mem[1] = limit;
203095766fffSHideo Aoki 	sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
203195766fffSHideo Aoki 
203295766fffSHideo Aoki 	sysctl_udp_rmem_min = SK_MEM_QUANTUM;
203395766fffSHideo Aoki 	sysctl_udp_wmem_min = SK_MEM_QUANTUM;
203495766fffSHideo Aoki }
203595766fffSHideo Aoki 
2036d7ca4cc0SSridhar Samudrala int udp4_ufo_send_check(struct sk_buff *skb)
2037d7ca4cc0SSridhar Samudrala {
2038d7ca4cc0SSridhar Samudrala 	const struct iphdr *iph;
2039d7ca4cc0SSridhar Samudrala 	struct udphdr *uh;
2040d7ca4cc0SSridhar Samudrala 
2041d7ca4cc0SSridhar Samudrala 	if (!pskb_may_pull(skb, sizeof(*uh)))
2042d7ca4cc0SSridhar Samudrala 		return -EINVAL;
2043d7ca4cc0SSridhar Samudrala 
2044d7ca4cc0SSridhar Samudrala 	iph = ip_hdr(skb);
2045d7ca4cc0SSridhar Samudrala 	uh = udp_hdr(skb);
2046d7ca4cc0SSridhar Samudrala 
2047d7ca4cc0SSridhar Samudrala 	uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
2048d7ca4cc0SSridhar Samudrala 				       IPPROTO_UDP, 0);
2049d7ca4cc0SSridhar Samudrala 	skb->csum_start = skb_transport_header(skb) - skb->head;
2050d7ca4cc0SSridhar Samudrala 	skb->csum_offset = offsetof(struct udphdr, check);
2051d7ca4cc0SSridhar Samudrala 	skb->ip_summed = CHECKSUM_PARTIAL;
2052d7ca4cc0SSridhar Samudrala 	return 0;
2053d7ca4cc0SSridhar Samudrala }
2054d7ca4cc0SSridhar Samudrala 
2055d7ca4cc0SSridhar Samudrala struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, int features)
2056d7ca4cc0SSridhar Samudrala {
2057d7ca4cc0SSridhar Samudrala 	struct sk_buff *segs = ERR_PTR(-EINVAL);
2058d7ca4cc0SSridhar Samudrala 	unsigned int mss;
2059d7ca4cc0SSridhar Samudrala 	int offset;
2060d7ca4cc0SSridhar Samudrala 	__wsum csum;
2061d7ca4cc0SSridhar Samudrala 
2062d7ca4cc0SSridhar Samudrala 	mss = skb_shinfo(skb)->gso_size;
2063d7ca4cc0SSridhar Samudrala 	if (unlikely(skb->len <= mss))
2064d7ca4cc0SSridhar Samudrala 		goto out;
2065d7ca4cc0SSridhar Samudrala 
2066d7ca4cc0SSridhar Samudrala 	if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
2067d7ca4cc0SSridhar Samudrala 		/* Packet is from an untrusted source, reset gso_segs. */
2068d7ca4cc0SSridhar Samudrala 		int type = skb_shinfo(skb)->gso_type;
2069d7ca4cc0SSridhar Samudrala 
2070d7ca4cc0SSridhar Samudrala 		if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
2071d7ca4cc0SSridhar Samudrala 			     !(type & (SKB_GSO_UDP))))
2072d7ca4cc0SSridhar Samudrala 			goto out;
2073d7ca4cc0SSridhar Samudrala 
2074d7ca4cc0SSridhar Samudrala 		skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
2075d7ca4cc0SSridhar Samudrala 
2076d7ca4cc0SSridhar Samudrala 		segs = NULL;
2077d7ca4cc0SSridhar Samudrala 		goto out;
2078d7ca4cc0SSridhar Samudrala 	}
2079d7ca4cc0SSridhar Samudrala 
2080d7ca4cc0SSridhar Samudrala 	/* Do software UFO. Complete and fill in the UDP checksum as HW cannot
2081d7ca4cc0SSridhar Samudrala 	 * do checksum of UDP packets sent as multiple IP fragments.
2082d7ca4cc0SSridhar Samudrala 	 */
2083d7ca4cc0SSridhar Samudrala 	offset = skb->csum_start - skb_headroom(skb);
2084d7ca4cc0SSridhar Samudrala 	csum = skb_checksum(skb, offset, skb->len - offset, 0);
2085d7ca4cc0SSridhar Samudrala 	offset += skb->csum_offset;
2086d7ca4cc0SSridhar Samudrala 	*(__sum16 *)(skb->data + offset) = csum_fold(csum);
2087d7ca4cc0SSridhar Samudrala 	skb->ip_summed = CHECKSUM_NONE;
2088d7ca4cc0SSridhar Samudrala 
2089d7ca4cc0SSridhar Samudrala 	/* Fragment the skb. IP headers of the fragments are updated in
2090d7ca4cc0SSridhar Samudrala 	 * inet_gso_segment()
2091d7ca4cc0SSridhar Samudrala 	 */
2092d7ca4cc0SSridhar Samudrala 	segs = skb_segment(skb, features);
2093d7ca4cc0SSridhar Samudrala out:
2094d7ca4cc0SSridhar Samudrala 	return segs;
2095d7ca4cc0SSridhar Samudrala }
2096d7ca4cc0SSridhar Samudrala 
2097