xref: /openbmc/linux/net/ipv6/udp.c (revision 45f6fad8)
1db8dac20SDavid S. Miller /*
2db8dac20SDavid S. Miller  *	UDP over IPv6
3db8dac20SDavid S. Miller  *	Linux INET6 implementation
4db8dac20SDavid S. Miller  *
5db8dac20SDavid S. Miller  *	Authors:
6db8dac20SDavid S. Miller  *	Pedro Roque		<roque@di.fc.ul.pt>
7db8dac20SDavid S. Miller  *
8db8dac20SDavid S. Miller  *	Based on linux/ipv4/udp.c
9db8dac20SDavid S. Miller  *
10db8dac20SDavid S. Miller  *	Fixes:
11db8dac20SDavid S. Miller  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
12db8dac20SDavid S. Miller  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
13db8dac20SDavid S. Miller  *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
14db8dac20SDavid S. Miller  *					a single port at the same time.
15db8dac20SDavid S. Miller  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
16db8dac20SDavid S. Miller  *      YOSHIFUJI Hideaki @USAGI:	convert /proc/net/udp6 to seq_file.
17db8dac20SDavid S. Miller  *
18db8dac20SDavid S. Miller  *	This program is free software; you can redistribute it and/or
19db8dac20SDavid S. Miller  *      modify it under the terms of the GNU General Public License
20db8dac20SDavid S. Miller  *      as published by the Free Software Foundation; either version
21db8dac20SDavid S. Miller  *      2 of the License, or (at your option) any later version.
22db8dac20SDavid S. Miller  */
23db8dac20SDavid S. Miller 
24db8dac20SDavid S. Miller #include <linux/errno.h>
25db8dac20SDavid S. Miller #include <linux/types.h>
26db8dac20SDavid S. Miller #include <linux/socket.h>
27db8dac20SDavid S. Miller #include <linux/sockios.h>
28db8dac20SDavid S. Miller #include <linux/net.h>
29db8dac20SDavid S. Miller #include <linux/in6.h>
30db8dac20SDavid S. Miller #include <linux/netdevice.h>
31db8dac20SDavid S. Miller #include <linux/if_arp.h>
32db8dac20SDavid S. Miller #include <linux/ipv6.h>
33db8dac20SDavid S. Miller #include <linux/icmpv6.h>
34db8dac20SDavid S. Miller #include <linux/init.h>
35db8dac20SDavid S. Miller #include <linux/module.h>
36db8dac20SDavid S. Miller #include <linux/skbuff.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
38db8dac20SDavid S. Miller #include <asm/uaccess.h>
39db8dac20SDavid S. Miller 
40db8dac20SDavid S. Miller #include <net/ndisc.h>
41db8dac20SDavid S. Miller #include <net/protocol.h>
42db8dac20SDavid S. Miller #include <net/transp_v6.h>
43db8dac20SDavid S. Miller #include <net/ip6_route.h>
44db8dac20SDavid S. Miller #include <net/raw.h>
45db8dac20SDavid S. Miller #include <net/tcp_states.h>
46db8dac20SDavid S. Miller #include <net/ip6_checksum.h>
47db8dac20SDavid S. Miller #include <net/xfrm.h>
4872289b96STom Herbert #include <net/inet6_hashtables.h>
49076bb0c8SEliezer Tamir #include <net/busy_poll.h>
50db8dac20SDavid S. Miller 
51db8dac20SDavid S. Miller #include <linux/proc_fs.h>
52db8dac20SDavid S. Miller #include <linux/seq_file.h>
5322911fc5SEric Dumazet #include <trace/events/skb.h>
54db8dac20SDavid S. Miller #include "udp_impl.h"
55db8dac20SDavid S. Miller 
566eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net,
57b50026b5SHannes Frederic Sowa 			const struct in6_addr *laddr,
58b50026b5SHannes Frederic Sowa 			const u16 lport,
59b50026b5SHannes Frederic Sowa 			const struct in6_addr *faddr,
60b50026b5SHannes Frederic Sowa 			const __be16 fport)
61b50026b5SHannes Frederic Sowa {
621bbdceefSHannes Frederic Sowa 	static u32 udp6_ehash_secret __read_mostly;
631bbdceefSHannes Frederic Sowa 	static u32 udp_ipv6_hash_secret __read_mostly;
641bbdceefSHannes Frederic Sowa 
651bbdceefSHannes Frederic Sowa 	u32 lhash, fhash;
661bbdceefSHannes Frederic Sowa 
671bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp6_ehash_secret,
681bbdceefSHannes Frederic Sowa 			    sizeof(udp6_ehash_secret));
691bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ipv6_hash_secret,
701bbdceefSHannes Frederic Sowa 			    sizeof(udp_ipv6_hash_secret));
711bbdceefSHannes Frederic Sowa 
721bbdceefSHannes Frederic Sowa 	lhash = (__force u32)laddr->s6_addr32[3];
731bbdceefSHannes Frederic Sowa 	fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
741bbdceefSHannes Frederic Sowa 
75b50026b5SHannes Frederic Sowa 	return __inet6_ehashfn(lhash, lport, fhash, fport,
761bbdceefSHannes Frederic Sowa 			       udp_ipv6_hash_secret + net_hash_mix(net));
77b50026b5SHannes Frederic Sowa }
78b50026b5SHannes Frederic Sowa 
79b2f5e7cdSVlad Yasevich int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
80b2f5e7cdSVlad Yasevich {
81b2f5e7cdSVlad Yasevich 	const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
82b2f5e7cdSVlad Yasevich 	int sk2_ipv6only = inet_v6_ipv6only(sk2);
83efe4208fSEric Dumazet 	int addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
84b2f5e7cdSVlad Yasevich 	int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
85b2f5e7cdSVlad Yasevich 
86b2f5e7cdSVlad Yasevich 	/* if both are mapped, treat as IPv4 */
87b2f5e7cdSVlad Yasevich 	if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
88499923c7SVlad Yasevich 		return (!sk2_ipv6only &&
8950805466SEric Dumazet 			(!sk->sk_rcv_saddr || !sk2->sk_rcv_saddr ||
9050805466SEric Dumazet 			  sk->sk_rcv_saddr == sk2->sk_rcv_saddr));
91b2f5e7cdSVlad Yasevich 
92b2f5e7cdSVlad Yasevich 	if (addr_type2 == IPV6_ADDR_ANY &&
93b2f5e7cdSVlad Yasevich 	    !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
94b2f5e7cdSVlad Yasevich 		return 1;
95b2f5e7cdSVlad Yasevich 
96b2f5e7cdSVlad Yasevich 	if (addr_type == IPV6_ADDR_ANY &&
979fe516baSEric Dumazet 	    !(ipv6_only_sock(sk) && addr_type2 == IPV6_ADDR_MAPPED))
98b2f5e7cdSVlad Yasevich 		return 1;
99b2f5e7cdSVlad Yasevich 
100b2f5e7cdSVlad Yasevich 	if (sk2_rcv_saddr6 &&
101efe4208fSEric Dumazet 	    ipv6_addr_equal(&sk->sk_v6_rcv_saddr, sk2_rcv_saddr6))
102b2f5e7cdSVlad Yasevich 		return 1;
103b2f5e7cdSVlad Yasevich 
104b2f5e7cdSVlad Yasevich 	return 0;
105b2f5e7cdSVlad Yasevich }
106b2f5e7cdSVlad Yasevich 
1076eada011SEric Dumazet static u32 udp6_portaddr_hash(const struct net *net,
108d4cada4aSEric Dumazet 			      const struct in6_addr *addr6,
109d4cada4aSEric Dumazet 			      unsigned int port)
110d4cada4aSEric Dumazet {
111d4cada4aSEric Dumazet 	unsigned int hash, mix = net_hash_mix(net);
112d4cada4aSEric Dumazet 
113d4cada4aSEric Dumazet 	if (ipv6_addr_any(addr6))
114d4cada4aSEric Dumazet 		hash = jhash_1word(0, mix);
115856540eeSBrian Haley 	else if (ipv6_addr_v4mapped(addr6))
1160eae88f3SEric Dumazet 		hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
117d4cada4aSEric Dumazet 	else
1180eae88f3SEric Dumazet 		hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
119d4cada4aSEric Dumazet 
120d4cada4aSEric Dumazet 	return hash ^ port;
121d4cada4aSEric Dumazet }
122d4cada4aSEric Dumazet 
1236ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
124db8dac20SDavid S. Miller {
12530fff923SEric Dumazet 	unsigned int hash2_nulladdr =
12630fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
12730fff923SEric Dumazet 	unsigned int hash2_partial =
128efe4208fSEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
12930fff923SEric Dumazet 
130d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
13130fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
13230fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
133db8dac20SDavid S. Miller }
134db8dac20SDavid S. Miller 
135719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk)
136719f8358SEric Dumazet {
137719f8358SEric Dumazet 	u16 new_hash = udp6_portaddr_hash(sock_net(sk),
138efe4208fSEric Dumazet 					  &sk->sk_v6_rcv_saddr,
139719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
140719f8358SEric Dumazet 
141719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
142719f8358SEric Dumazet }
143719f8358SEric Dumazet 
144645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net,
145645ca708SEric Dumazet 				unsigned short hnum,
14688440ae7SBalazs Scheidler 				const struct in6_addr *saddr, __be16 sport,
14788440ae7SBalazs Scheidler 				const struct in6_addr *daddr, __be16 dport,
148645ca708SEric Dumazet 				int dif)
149db8dac20SDavid S. Miller {
15060c04aecSJoe Perches 	int score;
15160c04aecSJoe Perches 	struct inet_sock *inet;
152db8dac20SDavid S. Miller 
15360c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
15460c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
15560c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
15660c04aecSJoe Perches 		return -1;
157645ca708SEric Dumazet 
158645ca708SEric Dumazet 	score = 0;
15960c04aecSJoe Perches 	inet = inet_sk(sk);
16060c04aecSJoe Perches 
161c720c7e8SEric Dumazet 	if (inet->inet_dport) {
162c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
163645ca708SEric Dumazet 			return -1;
164db8dac20SDavid S. Miller 		score++;
165db8dac20SDavid S. Miller 	}
16660c04aecSJoe Perches 
167efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
168efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
169645ca708SEric Dumazet 			return -1;
170db8dac20SDavid S. Miller 		score++;
171db8dac20SDavid S. Miller 	}
17260c04aecSJoe Perches 
173efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
174efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
175645ca708SEric Dumazet 			return -1;
176db8dac20SDavid S. Miller 		score++;
177db8dac20SDavid S. Miller 	}
17860c04aecSJoe Perches 
179db8dac20SDavid S. Miller 	if (sk->sk_bound_dev_if) {
180db8dac20SDavid S. Miller 		if (sk->sk_bound_dev_if != dif)
181645ca708SEric Dumazet 			return -1;
182db8dac20SDavid S. Miller 		score++;
183db8dac20SDavid S. Miller 	}
18460c04aecSJoe Perches 
18570da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
18670da268bSEric Dumazet 		score++;
18770da268bSEric Dumazet 
188645ca708SEric Dumazet 	return score;
189645ca708SEric Dumazet }
190645ca708SEric Dumazet 
191fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
192fddc17deSEric Dumazet 				 const struct in6_addr *saddr, __be16 sport,
19360c04aecSJoe Perches 				 const struct in6_addr *daddr,
19460c04aecSJoe Perches 				 unsigned short hnum, int dif)
195fddc17deSEric Dumazet {
19660c04aecSJoe Perches 	int score;
19760c04aecSJoe Perches 	struct inet_sock *inet;
198fddc17deSEric Dumazet 
19960c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
20060c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
20160c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
20260c04aecSJoe Perches 		return -1;
203fddc17deSEric Dumazet 
204efe4208fSEric Dumazet 	if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
205fddc17deSEric Dumazet 		return -1;
20660c04aecSJoe Perches 
207fddc17deSEric Dumazet 	score = 0;
20860c04aecSJoe Perches 	inet = inet_sk(sk);
20960c04aecSJoe Perches 
210fddc17deSEric Dumazet 	if (inet->inet_dport) {
211fddc17deSEric Dumazet 		if (inet->inet_dport != sport)
212fddc17deSEric Dumazet 			return -1;
213fddc17deSEric Dumazet 		score++;
214fddc17deSEric Dumazet 	}
21560c04aecSJoe Perches 
216efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
217efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
218fddc17deSEric Dumazet 			return -1;
219fddc17deSEric Dumazet 		score++;
220fddc17deSEric Dumazet 	}
22160c04aecSJoe Perches 
222fddc17deSEric Dumazet 	if (sk->sk_bound_dev_if) {
223fddc17deSEric Dumazet 		if (sk->sk_bound_dev_if != dif)
224fddc17deSEric Dumazet 			return -1;
225fddc17deSEric Dumazet 		score++;
226fddc17deSEric Dumazet 	}
22760c04aecSJoe Perches 
22870da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
22970da268bSEric Dumazet 		score++;
23070da268bSEric Dumazet 
231fddc17deSEric Dumazet 	return score;
232fddc17deSEric Dumazet }
233fddc17deSEric Dumazet 
234fddc17deSEric Dumazet /* called with read_rcu_lock() */
235fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
236fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
237fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
238fddc17deSEric Dumazet 		struct udp_hslot *hslot2, unsigned int slot2)
239fddc17deSEric Dumazet {
240fddc17deSEric Dumazet 	struct sock *sk, *result;
241fddc17deSEric Dumazet 	struct hlist_nulls_node *node;
24272289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
24372289b96STom Herbert 	u32 hash = 0;
244fddc17deSEric Dumazet 
245fddc17deSEric Dumazet begin:
246fddc17deSEric Dumazet 	result = NULL;
247fddc17deSEric Dumazet 	badness = -1;
248fddc17deSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
249fddc17deSEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
250fddc17deSEric Dumazet 				      daddr, hnum, dif);
251fddc17deSEric Dumazet 		if (score > badness) {
252fddc17deSEric Dumazet 			result = sk;
253fddc17deSEric Dumazet 			badness = score;
25472289b96STom Herbert 			reuseport = sk->sk_reuseport;
25572289b96STom Herbert 			if (reuseport) {
256b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
25772289b96STom Herbert 						    saddr, sport);
25872289b96STom Herbert 				matches = 1;
25970da268bSEric Dumazet 			}
26072289b96STom Herbert 		} else if (score == badness && reuseport) {
26172289b96STom Herbert 			matches++;
2628fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
26372289b96STom Herbert 				result = sk;
26472289b96STom Herbert 			hash = next_pseudo_random32(hash);
265fddc17deSEric Dumazet 		}
266fddc17deSEric Dumazet 	}
267fddc17deSEric Dumazet 	/*
268fddc17deSEric Dumazet 	 * if the nulls value we got at the end of this lookup is
269fddc17deSEric Dumazet 	 * not the expected one, we must restart lookup.
270fddc17deSEric Dumazet 	 * We probably met an item that was moved to another chain.
271fddc17deSEric Dumazet 	 */
272fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot2)
273fddc17deSEric Dumazet 		goto begin;
274fddc17deSEric Dumazet 
275fddc17deSEric Dumazet 	if (result) {
276c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
277fddc17deSEric Dumazet 			result = NULL;
278fddc17deSEric Dumazet 		else if (unlikely(compute_score2(result, net, saddr, sport,
279fddc17deSEric Dumazet 				  daddr, hnum, dif) < badness)) {
280fddc17deSEric Dumazet 			sock_put(result);
281fddc17deSEric Dumazet 			goto begin;
282fddc17deSEric Dumazet 		}
283fddc17deSEric Dumazet 	}
284fddc17deSEric Dumazet 	return result;
285fddc17deSEric Dumazet }
286fddc17deSEric Dumazet 
287fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
28888440ae7SBalazs Scheidler 				      const struct in6_addr *saddr, __be16 sport,
28988440ae7SBalazs Scheidler 				      const struct in6_addr *daddr, __be16 dport,
290645ca708SEric Dumazet 				      int dif, struct udp_table *udptable)
291645ca708SEric Dumazet {
292271b72c7SEric Dumazet 	struct sock *sk, *result;
29388ab1932SEric Dumazet 	struct hlist_nulls_node *node;
294645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
295fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
296fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
29772289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
29872289b96STom Herbert 	u32 hash = 0;
299645ca708SEric Dumazet 
300271b72c7SEric Dumazet 	rcu_read_lock();
301fddc17deSEric Dumazet 	if (hslot->count > 10) {
302fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
303fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
304fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
305fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
306fddc17deSEric Dumazet 			goto begin;
307fddc17deSEric Dumazet 
308fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
309fddc17deSEric Dumazet 					  daddr, hnum, dif,
310fddc17deSEric Dumazet 					  hslot2, slot2);
311fddc17deSEric Dumazet 		if (!result) {
312fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
313fddc17deSEric Dumazet 			slot2 = hash2 & udptable->mask;
314fddc17deSEric Dumazet 			hslot2 = &udptable->hash2[slot2];
315fddc17deSEric Dumazet 			if (hslot->count < hslot2->count)
316fddc17deSEric Dumazet 				goto begin;
317fddc17deSEric Dumazet 
3181223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
3191223c67cSJorge Boncompte [DTI2] 						  &in6addr_any, hnum, dif,
320fddc17deSEric Dumazet 						  hslot2, slot2);
321fddc17deSEric Dumazet 		}
322fddc17deSEric Dumazet 		rcu_read_unlock();
323fddc17deSEric Dumazet 		return result;
324fddc17deSEric Dumazet 	}
325271b72c7SEric Dumazet begin:
326271b72c7SEric Dumazet 	result = NULL;
327271b72c7SEric Dumazet 	badness = -1;
32888ab1932SEric Dumazet 	sk_nulls_for_each_rcu(sk, node, &hslot->head) {
329645ca708SEric Dumazet 		score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
330645ca708SEric Dumazet 		if (score > badness) {
331db8dac20SDavid S. Miller 			result = sk;
332db8dac20SDavid S. Miller 			badness = score;
33372289b96STom Herbert 			reuseport = sk->sk_reuseport;
33472289b96STom Herbert 			if (reuseport) {
335b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
33672289b96STom Herbert 						    saddr, sport);
33772289b96STom Herbert 				matches = 1;
33872289b96STom Herbert 			}
33972289b96STom Herbert 		} else if (score == badness && reuseport) {
34072289b96STom Herbert 			matches++;
3418fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
34272289b96STom Herbert 				result = sk;
34372289b96STom Herbert 			hash = next_pseudo_random32(hash);
344db8dac20SDavid S. Miller 		}
345db8dac20SDavid S. Miller 	}
34688ab1932SEric Dumazet 	/*
34788ab1932SEric Dumazet 	 * if the nulls value we got at the end of this lookup is
34888ab1932SEric Dumazet 	 * not the expected one, we must restart lookup.
34988ab1932SEric Dumazet 	 * We probably met an item that was moved to another chain.
35088ab1932SEric Dumazet 	 */
351fddc17deSEric Dumazet 	if (get_nulls_value(node) != slot)
35288ab1932SEric Dumazet 		goto begin;
35388ab1932SEric Dumazet 
354271b72c7SEric Dumazet 	if (result) {
355c31504dcSEric Dumazet 		if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
356271b72c7SEric Dumazet 			result = NULL;
357271b72c7SEric Dumazet 		else if (unlikely(compute_score(result, net, hnum, saddr, sport,
358271b72c7SEric Dumazet 					daddr, dport, dif) < badness)) {
359271b72c7SEric Dumazet 			sock_put(result);
360271b72c7SEric Dumazet 			goto begin;
361271b72c7SEric Dumazet 		}
362271b72c7SEric Dumazet 	}
363271b72c7SEric Dumazet 	rcu_read_unlock();
364db8dac20SDavid S. Miller 	return result;
365db8dac20SDavid S. Miller }
366fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
367db8dac20SDavid S. Miller 
368607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
369607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
370645ca708SEric Dumazet 					  struct udp_table *udptable)
371607c4aafSKOVACS Krisztian {
37223542618SKOVACS Krisztian 	struct sock *sk;
373b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
374607c4aafSKOVACS Krisztian 
375e5d08d71SIan Morris 	sk = skb_steal_sock(skb);
376e5d08d71SIan Morris 	if (unlikely(sk))
37723542618SKOVACS Krisztian 		return sk;
378adf30907SEric Dumazet 	return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
379607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
380607c4aafSKOVACS Krisztian 				 udptable);
381607c4aafSKOVACS Krisztian }
382607c4aafSKOVACS Krisztian 
383aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
384aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
385aa976fc0SBalazs Scheidler {
386aa976fc0SBalazs Scheidler 	return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
387aa976fc0SBalazs Scheidler }
388aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
389aa976fc0SBalazs Scheidler 
390db8dac20SDavid S. Miller /*
391db8dac20SDavid S. Miller  *	This should be easy, if there is something there we
392db8dac20SDavid S. Miller  *	return it, otherwise we block.
393db8dac20SDavid S. Miller  */
394db8dac20SDavid S. Miller 
3951b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
396db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
397db8dac20SDavid S. Miller {
398db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
399db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
400db8dac20SDavid S. Miller 	struct sk_buff *skb;
40159c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
4023f518bf7SPavel Emelyanov 	int peeked, off = 0;
403db8dac20SDavid S. Miller 	int err;
404db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
405f26ba175SWei Yongjun 	int is_udp4;
4068a74ad60SEric Dumazet 	bool slow;
407db8dac20SDavid S. Miller 
408db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
40985fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
410db8dac20SDavid S. Miller 
4114b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
41285fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4134b340ae2SBrian Haley 
414db8dac20SDavid S. Miller try_again:
415db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
4163f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
417db8dac20SDavid S. Miller 	if (!skb)
418db8dac20SDavid S. Miller 		goto out;
419db8dac20SDavid S. Miller 
420db8dac20SDavid S. Miller 	ulen = skb->len - sizeof(struct udphdr);
42159c2cdaeSDavid S. Miller 	copied = len;
42259c2cdaeSDavid S. Miller 	if (copied > ulen)
42359c2cdaeSDavid S. Miller 		copied = ulen;
42459c2cdaeSDavid S. Miller 	else if (copied < ulen)
425db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
426db8dac20SDavid S. Miller 
427f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
428f26ba175SWei Yongjun 
429db8dac20SDavid S. Miller 	/*
430db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
431db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
432db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
433db8dac20SDavid S. Miller 	 */
434db8dac20SDavid S. Miller 
43559c2cdaeSDavid S. Miller 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
436db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
437db8dac20SDavid S. Miller 			goto csum_copy_err;
438db8dac20SDavid S. Miller 	}
439db8dac20SDavid S. Miller 
440db8dac20SDavid S. Miller 	if (skb_csum_unnecessary(skb))
44151f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, sizeof(struct udphdr),
44251f3d02bSDavid S. Miller 					    msg, copied);
443db8dac20SDavid S. Miller 	else {
444227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, sizeof(struct udphdr), msg);
445db8dac20SDavid S. Miller 		if (err == -EINVAL)
446db8dac20SDavid S. Miller 			goto csum_copy_err;
447db8dac20SDavid S. Miller 	}
44822911fc5SEric Dumazet 	if (unlikely(err)) {
44922911fc5SEric Dumazet 		trace_kfree_skb(skb, udpv6_recvmsg);
450979402b1SEric Dumazet 		if (!peeked) {
451979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
452979402b1SEric Dumazet 			if (is_udp4)
453979402b1SEric Dumazet 				UDP_INC_STATS_USER(sock_net(sk),
454979402b1SEric Dumazet 						   UDP_MIB_INERRORS,
455979402b1SEric Dumazet 						   is_udplite);
456979402b1SEric Dumazet 			else
457979402b1SEric Dumazet 				UDP6_INC_STATS_USER(sock_net(sk),
458979402b1SEric Dumazet 						    UDP_MIB_INERRORS,
459979402b1SEric Dumazet 						    is_udplite);
460979402b1SEric Dumazet 		}
461db8dac20SDavid S. Miller 		goto out_free;
46222911fc5SEric Dumazet 	}
463f26ba175SWei Yongjun 	if (!peeked) {
464f26ba175SWei Yongjun 		if (is_udp4)
465f26ba175SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
466f26ba175SWei Yongjun 					UDP_MIB_INDATAGRAMS, is_udplite);
467f26ba175SWei Yongjun 		else
468235b9f7aSPavel Emelyanov 			UDP6_INC_STATS_USER(sock_net(sk),
469235b9f7aSPavel Emelyanov 					UDP_MIB_INDATAGRAMS, is_udplite);
470f26ba175SWei Yongjun 	}
471db8dac20SDavid S. Miller 
4723b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
473db8dac20SDavid S. Miller 
474db8dac20SDavid S. Miller 	/* Copy the address. */
475db8dac20SDavid S. Miller 	if (msg->msg_name) {
476342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
477db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
478db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
479db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
480db8dac20SDavid S. Miller 
481842df073SHannes Frederic Sowa 		if (is_udp4) {
482b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
483b301e82cSBrian Haley 					       &sin6->sin6_addr);
484842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
485842df073SHannes Frederic Sowa 		} else {
4864e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
487842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
488842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
4894330487aSDuan Jiong 						    inet6_iif(skb));
490db8dac20SDavid S. Miller 		}
491bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
492db8dac20SDavid S. Miller 	}
4934b261c75SHannes Frederic Sowa 
4944b261c75SHannes Frederic Sowa 	if (np->rxopt.all)
4954b261c75SHannes Frederic Sowa 		ip6_datagram_recv_common_ctl(sk, msg, skb);
4964b261c75SHannes Frederic Sowa 
497f26ba175SWei Yongjun 	if (is_udp4) {
498db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
499db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
500db8dac20SDavid S. Miller 	} else {
501db8dac20SDavid S. Miller 		if (np->rxopt.all)
5024b261c75SHannes Frederic Sowa 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
503db8dac20SDavid S. Miller 	}
504db8dac20SDavid S. Miller 
50559c2cdaeSDavid S. Miller 	err = copied;
506db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
507db8dac20SDavid S. Miller 		err = ulen;
508db8dac20SDavid S. Miller 
509db8dac20SDavid S. Miller out_free:
5109d410c79SEric Dumazet 	skb_free_datagram_locked(sk, skb);
511db8dac20SDavid S. Miller out:
512db8dac20SDavid S. Miller 	return err;
513db8dac20SDavid S. Miller 
514db8dac20SDavid S. Miller csum_copy_err:
5158a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
5160856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
5176a5dc9e5SEric Dumazet 		if (is_udp4) {
5186a5dc9e5SEric Dumazet 			UDP_INC_STATS_USER(sock_net(sk),
5196a5dc9e5SEric Dumazet 					UDP_MIB_CSUMERRORS, is_udplite);
5200856f939SWei Yongjun 			UDP_INC_STATS_USER(sock_net(sk),
5210856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
5226a5dc9e5SEric Dumazet 		} else {
5236a5dc9e5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
5246a5dc9e5SEric Dumazet 					UDP_MIB_CSUMERRORS, is_udplite);
5250856f939SWei Yongjun 			UDP6_INC_STATS_USER(sock_net(sk),
5260856f939SWei Yongjun 					UDP_MIB_INERRORS, is_udplite);
5270856f939SWei Yongjun 		}
5286a5dc9e5SEric Dumazet 	}
5298a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
530db8dac20SDavid S. Miller 
531beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
532beb39db5SEric Dumazet 	cond_resched();
5339cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
534db8dac20SDavid S. Miller 	goto try_again;
535db8dac20SDavid S. Miller }
536db8dac20SDavid S. Miller 
537db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
538d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
539645ca708SEric Dumazet 		    struct udp_table *udptable)
540db8dac20SDavid S. Miller {
541db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
542b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
543b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
544b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
545db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
546db8dac20SDavid S. Miller 	struct sock *sk;
547db8dac20SDavid S. Miller 	int err;
5487304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
549db8dac20SDavid S. Miller 
5507304fe46SDuan Jiong 	sk = __udp6_lib_lookup(net, daddr, uh->dest,
551db8dac20SDavid S. Miller 			       saddr, uh->source, inet6_iif(skb), udptable);
55263159f29SIan Morris 	if (!sk) {
5537304fe46SDuan Jiong 		ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
5547304fe46SDuan Jiong 				   ICMP6_MIB_INERRORS);
555db8dac20SDavid S. Miller 		return;
5567304fe46SDuan Jiong 	}
557db8dac20SDavid S. Miller 
55893b36cf3SHannes Frederic Sowa 	if (type == ICMPV6_PKT_TOOBIG) {
55993b36cf3SHannes Frederic Sowa 		if (!ip6_sk_accept_pmtu(sk))
56093b36cf3SHannes Frederic Sowa 			goto out;
56181aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
56293b36cf3SHannes Frederic Sowa 	}
5631a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
564ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
5651a462d18SDuan Jiong 		goto out;
5661a462d18SDuan Jiong 	}
56781aded24SDavid S. Miller 
568db8dac20SDavid S. Miller 	np = inet6_sk(sk);
569db8dac20SDavid S. Miller 
570db8dac20SDavid S. Miller 	if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
571db8dac20SDavid S. Miller 		goto out;
572db8dac20SDavid S. Miller 
573db8dac20SDavid S. Miller 	if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
574db8dac20SDavid S. Miller 		goto out;
575db8dac20SDavid S. Miller 
576b1faf566SEric Dumazet 	if (np->recverr)
577db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
578b1faf566SEric Dumazet 
579db8dac20SDavid S. Miller 	sk->sk_err = err;
580db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
581db8dac20SDavid S. Miller out:
582db8dac20SDavid S. Miller 	sock_put(sk);
583db8dac20SDavid S. Miller }
584db8dac20SDavid S. Miller 
585f7ad74feSBenjamin LaHaise static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
586f7ad74feSBenjamin LaHaise {
587f7ad74feSBenjamin LaHaise 	int rc;
588f7ad74feSBenjamin LaHaise 
589efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
590f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
591005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
5922c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
593005ec974SShawn Bohrer 	}
594f7ad74feSBenjamin LaHaise 
595f7ad74feSBenjamin LaHaise 	rc = sock_queue_rcv_skb(sk, skb);
596f7ad74feSBenjamin LaHaise 	if (rc < 0) {
597f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
598f7ad74feSBenjamin LaHaise 
599f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
600f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
601f7ad74feSBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk),
602f7ad74feSBenjamin LaHaise 					UDP_MIB_RCVBUFERRORS, is_udplite);
603f7ad74feSBenjamin LaHaise 		UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
604f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
605f7ad74feSBenjamin LaHaise 		return -1;
606f7ad74feSBenjamin LaHaise 	}
607f7ad74feSBenjamin LaHaise 	return 0;
608f7ad74feSBenjamin LaHaise }
609f7ad74feSBenjamin LaHaise 
610db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
611d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
612d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info)
613db8dac20SDavid S. Miller {
614645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
615db8dac20SDavid S. Miller }
616db8dac20SDavid S. Miller 
617d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly;
618d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void)
619d7f3f621SBenjamin LaHaise {
620d7f3f621SBenjamin LaHaise 	if (!static_key_enabled(&udpv6_encap_needed))
621d7f3f621SBenjamin LaHaise 		static_key_slow_inc(&udpv6_encap_needed);
622d7f3f621SBenjamin LaHaise }
623d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable);
624d7f3f621SBenjamin LaHaise 
625db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
626db8dac20SDavid S. Miller {
627db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
628db8dac20SDavid S. Miller 	int rc;
629db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
630db8dac20SDavid S. Miller 
631db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
632db8dac20SDavid S. Miller 		goto drop;
633db8dac20SDavid S. Miller 
634d7f3f621SBenjamin LaHaise 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
635d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
636d7f3f621SBenjamin LaHaise 
637d7f3f621SBenjamin LaHaise 		/*
638d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
639d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
640d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
641d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
642d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
643d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
644d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
645d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
646d7f3f621SBenjamin LaHaise 		 */
647d7f3f621SBenjamin LaHaise 
648d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
649d7f3f621SBenjamin LaHaise 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
65053b24b8fSIan Morris 		if (skb->len > sizeof(struct udphdr) && encap_rcv) {
651d7f3f621SBenjamin LaHaise 			int ret;
652d7f3f621SBenjamin LaHaise 
6530a80966bSTom Herbert 			/* Verify checksum before giving to encap */
6540a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
6550a80966bSTom Herbert 				goto csum_error;
6560a80966bSTom Herbert 
657d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
658d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
659d7f3f621SBenjamin LaHaise 				UDP_INC_STATS_BH(sock_net(sk),
660d7f3f621SBenjamin LaHaise 						 UDP_MIB_INDATAGRAMS,
661d7f3f621SBenjamin LaHaise 						 is_udplite);
662d7f3f621SBenjamin LaHaise 				return -ret;
663d7f3f621SBenjamin LaHaise 			}
664d7f3f621SBenjamin LaHaise 		}
665d7f3f621SBenjamin LaHaise 
666d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
667d7f3f621SBenjamin LaHaise 	}
668d7f3f621SBenjamin LaHaise 
669db8dac20SDavid S. Miller 	/*
670db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
671db8dac20SDavid S. Miller 	 */
672db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
673db8dac20SDavid S. Miller 
674db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
675ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
676db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
677db8dac20SDavid S. Miller 			goto drop;
678db8dac20SDavid S. Miller 		}
679db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
680ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
681db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
682db8dac20SDavid S. Miller 			goto drop;
683db8dac20SDavid S. Miller 		}
684db8dac20SDavid S. Miller 	}
685db8dac20SDavid S. Miller 
68633d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter)) {
687db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
6886a5dc9e5SEric Dumazet 			goto csum_error;
689db8dac20SDavid S. Miller 	}
690db8dac20SDavid S. Miller 
691274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
6923e215c8dSJames M Leddy 		UDP6_INC_STATS_BH(sock_net(sk),
6933e215c8dSJames M Leddy 				  UDP_MIB_RCVBUFERRORS, is_udplite);
694cb80ef46SBenjamin LaHaise 		goto drop;
6953e215c8dSJames M Leddy 	}
696cb80ef46SBenjamin LaHaise 
697d826eb14SEric Dumazet 	skb_dst_drop(skb);
698db8dac20SDavid S. Miller 
699cb80ef46SBenjamin LaHaise 	bh_lock_sock(sk);
700cb80ef46SBenjamin LaHaise 	rc = 0;
701cb80ef46SBenjamin LaHaise 	if (!sock_owned_by_user(sk))
702f7ad74feSBenjamin LaHaise 		rc = __udpv6_queue_rcv_skb(sk, skb);
703cb80ef46SBenjamin LaHaise 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
704cb80ef46SBenjamin LaHaise 		bh_unlock_sock(sk);
705cb80ef46SBenjamin LaHaise 		goto drop;
706cb80ef46SBenjamin LaHaise 	}
707cb80ef46SBenjamin LaHaise 	bh_unlock_sock(sk);
708f7ad74feSBenjamin LaHaise 
709f7ad74feSBenjamin LaHaise 	return rc;
7103e215c8dSJames M Leddy 
7116a5dc9e5SEric Dumazet csum_error:
7126a5dc9e5SEric Dumazet 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
713db8dac20SDavid S. Miller drop:
714ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
715cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
716db8dac20SDavid S. Miller 	kfree_skb(skb);
717db8dac20SDavid S. Miller 	return -1;
718db8dac20SDavid S. Miller }
719db8dac20SDavid S. Miller 
7205cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
721b71d1d42SEric Dumazet 				   __be16 loc_port, const struct in6_addr *loc_addr,
722b71d1d42SEric Dumazet 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
7235cf3d461SDavid Held 				   int dif, unsigned short hnum)
724db8dac20SDavid S. Miller {
7259e89fd8bSSven Wegener 	struct inet_sock *inet = inet_sk(sk);
726db8dac20SDavid S. Miller 
7279e89fd8bSSven Wegener 	if (!net_eq(sock_net(sk), net))
7285cf3d461SDavid Held 		return false;
729b8ad0cbcSDaniel Lezcano 
7305cf3d461SDavid Held 	if (udp_sk(sk)->udp_port_hash != hnum ||
7315cf3d461SDavid Held 	    sk->sk_family != PF_INET6 ||
7325cf3d461SDavid Held 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
7335cf3d461SDavid Held 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
7345cf3d461SDavid Held 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
73533b4b015SHenning Rogge 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
73633b4b015SHenning Rogge 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
73733b4b015SHenning Rogge 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
7385cf3d461SDavid Held 		return false;
7399e89fd8bSSven Wegener 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
7405cf3d461SDavid Held 		return false;
7415cf3d461SDavid Held 	return true;
742db8dac20SDavid S. Miller }
743db8dac20SDavid S. Miller 
744a1ab77f9SEric Dumazet static void flush_stack(struct sock **stack, unsigned int count,
745a1ab77f9SEric Dumazet 			struct sk_buff *skb, unsigned int final)
746a1ab77f9SEric Dumazet {
747cb80ef46SBenjamin LaHaise 	struct sk_buff *skb1 = NULL;
748a1ab77f9SEric Dumazet 	struct sock *sk;
749cb80ef46SBenjamin LaHaise 	unsigned int i;
750a1ab77f9SEric Dumazet 
751a1ab77f9SEric Dumazet 	for (i = 0; i < count; i++) {
752a1ab77f9SEric Dumazet 		sk = stack[i];
75363159f29SIan Morris 		if (likely(!skb1))
754cb80ef46SBenjamin LaHaise 			skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
755cb80ef46SBenjamin LaHaise 		if (!skb1) {
756f6b8f32cSEric Dumazet 			atomic_inc(&sk->sk_drops);
757cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
758cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
759cb80ef46SBenjamin LaHaise 			UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS,
760cb80ef46SBenjamin LaHaise 					  IS_UDPLITE(sk));
761a1ab77f9SEric Dumazet 		}
762cb80ef46SBenjamin LaHaise 
763cb80ef46SBenjamin LaHaise 		if (skb1 && udpv6_queue_rcv_skb(sk, skb1) <= 0)
764cb80ef46SBenjamin LaHaise 			skb1 = NULL;
7652dc41cffSDavid Held 		sock_put(sk);
766cb80ef46SBenjamin LaHaise 	}
767cb80ef46SBenjamin LaHaise 	if (unlikely(skb1))
768cb80ef46SBenjamin LaHaise 		kfree_skb(skb1);
769a1ab77f9SEric Dumazet }
7704068579eSTom Herbert 
7714068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
7724068579eSTom Herbert {
7734068579eSTom Herbert 	/* RFC 2460 section 8.1 says that we SHOULD log
7744068579eSTom Herbert 	 * this error. Well, it is reasonable.
7754068579eSTom Herbert 	 */
776ba7a46f1SJoe Perches 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
7774068579eSTom Herbert 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
7784068579eSTom Herbert 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
7794068579eSTom Herbert }
7804068579eSTom Herbert 
781db8dac20SDavid S. Miller /*
782db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
783db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
784db8dac20SDavid S. Miller  */
785e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
786b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
78736cbb245SRick Jones 		struct udp_table *udptable, int proto)
788db8dac20SDavid S. Miller {
789a1ab77f9SEric Dumazet 	struct sock *sk, *stack[256 / sizeof(struct sock *)];
790db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
7915cf3d461SDavid Held 	struct hlist_nulls_node *node;
7925cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
7935cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
7945cf3d461SDavid Held 	int dif = inet6_iif(skb);
7952dc41cffSDavid Held 	unsigned int count = 0, offset = offsetof(typeof(*sk), sk_nulls_node);
7962dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
79736cbb245SRick Jones 	bool inner_flushed = false;
7982dc41cffSDavid Held 
7992dc41cffSDavid Held 	if (use_hash2) {
8002dc41cffSDavid Held 		hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
8012dc41cffSDavid Held 			    udp_table.mask;
8022dc41cffSDavid Held 		hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask;
8032dc41cffSDavid Held start_lookup:
8042dc41cffSDavid Held 		hslot = &udp_table.hash2[hash2];
8052dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
8062dc41cffSDavid Held 	}
807db8dac20SDavid S. Miller 
808645ca708SEric Dumazet 	spin_lock(&hslot->lock);
8092dc41cffSDavid Held 	sk_nulls_for_each_entry_offset(sk, node, &hslot->head, offset) {
8105cf3d461SDavid Held 		if (__udp_v6_is_mcast_sock(net, sk,
8115cf3d461SDavid Held 					   uh->dest, daddr,
8125cf3d461SDavid Held 					   uh->source, saddr,
8135cf3d461SDavid Held 					   dif, hnum) &&
8141c19448cSTom Herbert 		    /* If zero checksum and no_check is not on for
8154068579eSTom Herbert 		     * the socket then skip it.
8164068579eSTom Herbert 		     */
8175cf3d461SDavid Held 		    (uh->check || udp_sk(sk)->no_check6_rx)) {
818a1ab77f9SEric Dumazet 			if (unlikely(count == ARRAY_SIZE(stack))) {
819a1ab77f9SEric Dumazet 				flush_stack(stack, count, skb, ~0);
82036cbb245SRick Jones 				inner_flushed = true;
821a1ab77f9SEric Dumazet 				count = 0;
822db8dac20SDavid S. Miller 			}
8235cf3d461SDavid Held 			stack[count++] = sk;
8242dc41cffSDavid Held 			sock_hold(sk);
8255cf3d461SDavid Held 		}
826a1ab77f9SEric Dumazet 	}
827db8dac20SDavid S. Miller 
828645ca708SEric Dumazet 	spin_unlock(&hslot->lock);
829a1ab77f9SEric Dumazet 
8302dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
8312dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
8322dc41cffSDavid Held 		hash2 = hash2_any;
8332dc41cffSDavid Held 		goto start_lookup;
8342dc41cffSDavid Held 	}
8352dc41cffSDavid Held 
836a1ab77f9SEric Dumazet 	if (count) {
837a1ab77f9SEric Dumazet 		flush_stack(stack, count, skb, count - 1);
838a1ab77f9SEric Dumazet 	} else {
83936cbb245SRick Jones 		if (!inner_flushed)
84036cbb245SRick Jones 			UDP_INC_STATS_BH(net, UDP_MIB_IGNOREDMULTI,
84136cbb245SRick Jones 					 proto == IPPROTO_UDPLITE);
84236cbb245SRick Jones 		consume_skb(skb);
843a1ab77f9SEric Dumazet 	}
844db8dac20SDavid S. Miller 	return 0;
845db8dac20SDavid S. Miller }
846db8dac20SDavid S. Miller 
847645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
848db8dac20SDavid S. Miller 		   int proto)
849db8dac20SDavid S. Miller {
8503ffe533cSAlexey Dobriyan 	struct net *net = dev_net(skb->dev);
851db8dac20SDavid S. Miller 	struct sock *sk;
852db8dac20SDavid S. Miller 	struct udphdr *uh;
853b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
854db8dac20SDavid S. Miller 	u32 ulen = 0;
855db8dac20SDavid S. Miller 
856db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
857d6bc0149SBjørn Mork 		goto discard;
858db8dac20SDavid S. Miller 
859db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
860db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
861db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
862db8dac20SDavid S. Miller 
863db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
864db8dac20SDavid S. Miller 	if (ulen > skb->len)
865db8dac20SDavid S. Miller 		goto short_packet;
866db8dac20SDavid S. Miller 
867db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
868db8dac20SDavid S. Miller 		/* UDP validates ulen. */
869db8dac20SDavid S. Miller 
870db8dac20SDavid S. Miller 		/* Check for jumbo payload */
871db8dac20SDavid S. Miller 		if (ulen == 0)
872db8dac20SDavid S. Miller 			ulen = skb->len;
873db8dac20SDavid S. Miller 
874db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
875db8dac20SDavid S. Miller 			goto short_packet;
876db8dac20SDavid S. Miller 
877db8dac20SDavid S. Miller 		if (ulen < skb->len) {
878db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
879db8dac20SDavid S. Miller 				goto short_packet;
880db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
881db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
882db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
883db8dac20SDavid S. Miller 		}
884db8dac20SDavid S. Miller 	}
885db8dac20SDavid S. Miller 
886db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
8876a5dc9e5SEric Dumazet 		goto csum_error;
888db8dac20SDavid S. Miller 
889db8dac20SDavid S. Miller 	/*
890db8dac20SDavid S. Miller 	 *	Multicast receive code
891db8dac20SDavid S. Miller 	 */
892db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
893e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
89436cbb245SRick Jones 				saddr, daddr, udptable, proto);
895db8dac20SDavid S. Miller 
896db8dac20SDavid S. Miller 	/* Unicast */
897db8dac20SDavid S. Miller 
898db8dac20SDavid S. Miller 	/*
899db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
900db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
901db8dac20SDavid S. Miller 	 */
902607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
90353b24b8fSIan Morris 	if (sk) {
904a5b50476SEliezer Tamir 		int ret;
905a5b50476SEliezer Tamir 
9061c19448cSTom Herbert 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
90779e0f1c9STom Herbert 			sock_put(sk);
9084068579eSTom Herbert 			udp6_csum_zero_error(skb);
9094068579eSTom Herbert 			goto csum_error;
9104068579eSTom Herbert 		}
9114068579eSTom Herbert 
912224d019cSTom Herbert 		if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
9132abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
9142abb7cdcSTom Herbert 						 ip6_compute_pseudo);
9152abb7cdcSTom Herbert 
916a5b50476SEliezer Tamir 		ret = udpv6_queue_rcv_skb(sk, skb);
917cb80ef46SBenjamin LaHaise 		sock_put(sk);
918db8dac20SDavid S. Miller 
919cb80ef46SBenjamin LaHaise 		/* a return value > 0 means to resubmit the input, but
920cb80ef46SBenjamin LaHaise 		 * it wants the return to be -protocol, or 0
921cb80ef46SBenjamin LaHaise 		 */
922cb80ef46SBenjamin LaHaise 		if (ret > 0)
923cb80ef46SBenjamin LaHaise 			return -ret;
924cb80ef46SBenjamin LaHaise 
925cb80ef46SBenjamin LaHaise 		return 0;
926cb80ef46SBenjamin LaHaise 	}
927cb80ef46SBenjamin LaHaise 
9284068579eSTom Herbert 	if (!uh->check) {
9294068579eSTom Herbert 		udp6_csum_zero_error(skb);
9304068579eSTom Herbert 		goto csum_error;
9314068579eSTom Herbert 	}
9324068579eSTom Herbert 
933db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
934db8dac20SDavid S. Miller 		goto discard;
935db8dac20SDavid S. Miller 
936db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
9376a5dc9e5SEric Dumazet 		goto csum_error;
938db8dac20SDavid S. Miller 
939cb80ef46SBenjamin LaHaise 	UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
9403ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
941db8dac20SDavid S. Miller 
942db8dac20SDavid S. Miller 	kfree_skb(skb);
943db8dac20SDavid S. Miller 	return 0;
944db8dac20SDavid S. Miller 
945db8dac20SDavid S. Miller short_packet:
946ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
947db8dac20SDavid S. Miller 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
948ba7a46f1SJoe Perches 			    saddr, ntohs(uh->source),
949ba7a46f1SJoe Perches 			    ulen, skb->len,
950ba7a46f1SJoe Perches 			    daddr, ntohs(uh->dest));
9516a5dc9e5SEric Dumazet 	goto discard;
9526a5dc9e5SEric Dumazet csum_error:
9536a5dc9e5SEric Dumazet 	UDP6_INC_STATS_BH(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
954db8dac20SDavid S. Miller discard:
955ef28d1a2SPavel Emelyanov 	UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
956db8dac20SDavid S. Miller 	kfree_skb(skb);
957db8dac20SDavid S. Miller 	return 0;
958db8dac20SDavid S. Miller }
959db8dac20SDavid S. Miller 
960db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
961db8dac20SDavid S. Miller {
962645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
963db8dac20SDavid S. Miller }
964db8dac20SDavid S. Miller 
965db8dac20SDavid S. Miller /*
966db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
967db8dac20SDavid S. Miller  */
968db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
969db8dac20SDavid S. Miller {
970db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
971db8dac20SDavid S. Miller 
97236d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
97336d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
97436d926b9SDenis V. Lunev 	else if (up->pending) {
975db8dac20SDavid S. Miller 		up->len = 0;
976db8dac20SDavid S. Miller 		up->pending = 0;
977db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
978db8dac20SDavid S. Miller 	}
979db8dac20SDavid S. Miller }
980db8dac20SDavid S. Miller 
981493c6be3SSridhar Samudrala /**
982493c6be3SSridhar Samudrala  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
983493c6be3SSridhar Samudrala  *	@sk:	socket we are sending on
984493c6be3SSridhar Samudrala  *	@skb:	sk_buff containing the filled-in UDP header
985493c6be3SSridhar Samudrala  *		(checksum field must be zeroed out)
986493c6be3SSridhar Samudrala  */
987493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
988493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
989493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
990493c6be3SSridhar Samudrala {
991493c6be3SSridhar Samudrala 	unsigned int offset;
992493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
993d39d938cSVlad Yasevich 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
994493c6be3SSridhar Samudrala 	__wsum csum = 0;
995493c6be3SSridhar Samudrala 
996d39d938cSVlad Yasevich 	if (!frags) {
997493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
998493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
999493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
1000493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1001493c6be3SSridhar Samudrala 	} else {
1002493c6be3SSridhar Samudrala 		/*
1003493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
1004493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
1005493c6be3SSridhar Samudrala 		 * should be together
1006493c6be3SSridhar Samudrala 		 */
1007493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
1008493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1009493c6be3SSridhar Samudrala 
1010493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
1011493c6be3SSridhar Samudrala 
1012d39d938cSVlad Yasevich 		do {
1013d39d938cSVlad Yasevich 			csum = csum_add(csum, frags->csum);
1014d39d938cSVlad Yasevich 		} while ((frags = frags->next));
1015493c6be3SSridhar Samudrala 
1016493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1017493c6be3SSridhar Samudrala 					    csum);
1018493c6be3SSridhar Samudrala 		if (uh->check == 0)
1019493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
1020493c6be3SSridhar Samudrala 	}
1021493c6be3SSridhar Samudrala }
1022493c6be3SSridhar Samudrala 
1023db8dac20SDavid S. Miller /*
1024db8dac20SDavid S. Miller  *	Sending
1025db8dac20SDavid S. Miller  */
1026db8dac20SDavid S. Miller 
1027d39d938cSVlad Yasevich static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
1028db8dac20SDavid S. Miller {
1029d39d938cSVlad Yasevich 	struct sock *sk = skb->sk;
1030db8dac20SDavid S. Miller 	struct udphdr *uh;
1031db8dac20SDavid S. Miller 	int err = 0;
1032db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1033db8dac20SDavid S. Miller 	__wsum csum = 0;
1034d39d938cSVlad Yasevich 	int offset = skb_transport_offset(skb);
1035d39d938cSVlad Yasevich 	int len = skb->len - offset;
1036db8dac20SDavid S. Miller 
1037db8dac20SDavid S. Miller 	/*
1038db8dac20SDavid S. Miller 	 * Create a UDP header
1039db8dac20SDavid S. Miller 	 */
1040db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
10411958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
10421958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
1043d39d938cSVlad Yasevich 	uh->len = htons(len);
1044db8dac20SDavid S. Miller 	uh->check = 0;
1045db8dac20SDavid S. Miller 
1046db8dac20SDavid S. Miller 	if (is_udplite)
1047d39d938cSVlad Yasevich 		csum = udplite_csum(skb);
1048d39d938cSVlad Yasevich 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
10494068579eSTom Herbert 		skb->ip_summed = CHECKSUM_NONE;
10504068579eSTom Herbert 		goto send;
10514068579eSTom Herbert 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1052d39d938cSVlad Yasevich 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1053493c6be3SSridhar Samudrala 		goto send;
1054493c6be3SSridhar Samudrala 	} else
1055d39d938cSVlad Yasevich 		csum = udp_csum(skb);
1056db8dac20SDavid S. Miller 
1057db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
10584c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1059d39d938cSVlad Yasevich 				    len, fl6->flowi6_proto, csum);
1060db8dac20SDavid S. Miller 	if (uh->check == 0)
1061db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
1062db8dac20SDavid S. Miller 
1063493c6be3SSridhar Samudrala send:
1064d39d938cSVlad Yasevich 	err = ip6_send_skb(skb);
10656ce9e7b5SEric Dumazet 	if (err) {
10666ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
10676ce9e7b5SEric Dumazet 			UDP6_INC_STATS_USER(sock_net(sk),
10686ce9e7b5SEric Dumazet 					    UDP_MIB_SNDBUFERRORS, is_udplite);
10696ce9e7b5SEric Dumazet 			err = 0;
10706ce9e7b5SEric Dumazet 		}
10716ce9e7b5SEric Dumazet 	} else
10726ce9e7b5SEric Dumazet 		UDP6_INC_STATS_USER(sock_net(sk),
10736ce9e7b5SEric Dumazet 				    UDP_MIB_OUTDATAGRAMS, is_udplite);
1074d39d938cSVlad Yasevich 	return err;
1075d39d938cSVlad Yasevich }
1076d39d938cSVlad Yasevich 
1077d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
1078d39d938cSVlad Yasevich {
1079d39d938cSVlad Yasevich 	struct sk_buff *skb;
1080d39d938cSVlad Yasevich 	struct udp_sock  *up = udp_sk(sk);
1081d39d938cSVlad Yasevich 	struct flowi6 fl6;
1082d39d938cSVlad Yasevich 	int err = 0;
1083d39d938cSVlad Yasevich 
1084d39d938cSVlad Yasevich 	if (up->pending == AF_INET)
1085d39d938cSVlad Yasevich 		return udp_push_pending_frames(sk);
1086d39d938cSVlad Yasevich 
1087d39d938cSVlad Yasevich 	/* ip6_finish_skb will release the cork, so make a copy of
1088d39d938cSVlad Yasevich 	 * fl6 here.
1089d39d938cSVlad Yasevich 	 */
1090d39d938cSVlad Yasevich 	fl6 = inet_sk(sk)->cork.fl.u.ip6;
1091d39d938cSVlad Yasevich 
1092d39d938cSVlad Yasevich 	skb = ip6_finish_skb(sk);
1093d39d938cSVlad Yasevich 	if (!skb)
1094d39d938cSVlad Yasevich 		goto out;
1095d39d938cSVlad Yasevich 
1096d39d938cSVlad Yasevich 	err = udp_v6_send_skb(skb, &fl6);
1097d39d938cSVlad Yasevich 
1098db8dac20SDavid S. Miller out:
1099db8dac20SDavid S. Miller 	up->len = 0;
1100db8dac20SDavid S. Miller 	up->pending = 0;
1101db8dac20SDavid S. Miller 	return err;
1102db8dac20SDavid S. Miller }
1103db8dac20SDavid S. Miller 
11041b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1105db8dac20SDavid S. Miller {
1106db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1107db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1108db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1109db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1110342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
111120c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1112db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
111345f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
1114db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
11154c9483b2SDavid S. Miller 	struct flowi6 fl6;
1116db8dac20SDavid S. Miller 	struct dst_entry *dst;
1117db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
1118db8dac20SDavid S. Miller 	int ulen = len;
1119db8dac20SDavid S. Miller 	int hlimit = -1;
1120db8dac20SDavid S. Miller 	int tclass = -1;
112113b52cd4SBrian Haley 	int dontfrag = -1;
1122db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1123db8dac20SDavid S. Miller 	int err;
1124db8dac20SDavid S. Miller 	int connected = 0;
1125db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1126db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1127db8dac20SDavid S. Miller 
1128db8dac20SDavid S. Miller 	/* destination address check */
1129db8dac20SDavid S. Miller 	if (sin6) {
1130db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1131db8dac20SDavid S. Miller 			return -EINVAL;
1132db8dac20SDavid S. Miller 
1133db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1134db8dac20SDavid S. Miller 		case AF_INET6:
1135db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1136db8dac20SDavid S. Miller 				return -EINVAL;
1137db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1138db8dac20SDavid S. Miller 			break;
1139db8dac20SDavid S. Miller 		case AF_INET:
1140db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1141db8dac20SDavid S. Miller 		case AF_UNSPEC:
1142db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1143db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1144db8dac20SDavid S. Miller 			daddr = NULL;
1145db8dac20SDavid S. Miller 			break;
1146db8dac20SDavid S. Miller 		default:
1147db8dac20SDavid S. Miller 			return -EINVAL;
1148db8dac20SDavid S. Miller 		}
1149db8dac20SDavid S. Miller 	} else if (!up->pending) {
1150db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1151db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1152efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
1153db8dac20SDavid S. Miller 	} else
1154db8dac20SDavid S. Miller 		daddr = NULL;
1155db8dac20SDavid S. Miller 
1156db8dac20SDavid S. Miller 	if (daddr) {
1157db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1158db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1159db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1160c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1161db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1162db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1163db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1164db8dac20SDavid S. Miller do_udp_sendmsg:
1165db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1166db8dac20SDavid S. Miller 				return -ENETUNREACH;
11671b784140SYing Xue 			return udp_sendmsg(sk, msg, len);
1168db8dac20SDavid S. Miller 		}
1169db8dac20SDavid S. Miller 	}
1170db8dac20SDavid S. Miller 
1171db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
11721b784140SYing Xue 		return udp_sendmsg(sk, msg, len);
1173db8dac20SDavid S. Miller 
1174db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1175db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1176db8dac20SDavid S. Miller 	   */
1177db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1178db8dac20SDavid S. Miller 		return -EMSGSIZE;
1179db8dac20SDavid S. Miller 
118003485f2aSVlad Yasevich 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1181db8dac20SDavid S. Miller 	if (up->pending) {
1182db8dac20SDavid S. Miller 		/*
1183db8dac20SDavid S. Miller 		 * There are pending frames.
1184db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1185db8dac20SDavid S. Miller 		 */
1186db8dac20SDavid S. Miller 		lock_sock(sk);
1187db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1188db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1189db8dac20SDavid S. Miller 				release_sock(sk);
1190db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1191db8dac20SDavid S. Miller 			}
1192db8dac20SDavid S. Miller 			dst = NULL;
1193db8dac20SDavid S. Miller 			goto do_append_data;
1194db8dac20SDavid S. Miller 		}
1195db8dac20SDavid S. Miller 		release_sock(sk);
1196db8dac20SDavid S. Miller 	}
1197db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1198db8dac20SDavid S. Miller 
11994c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1200db8dac20SDavid S. Miller 
1201db8dac20SDavid S. Miller 	if (sin6) {
1202db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1203db8dac20SDavid S. Miller 			return -EINVAL;
1204db8dac20SDavid S. Miller 
12051958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1206db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1207db8dac20SDavid S. Miller 
1208db8dac20SDavid S. Miller 		if (np->sndflow) {
12094c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
12104c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
12114c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
121263159f29SIan Morris 				if (!flowlabel)
1213db8dac20SDavid S. Miller 					return -EINVAL;
1214db8dac20SDavid S. Miller 			}
1215db8dac20SDavid S. Miller 		}
1216db8dac20SDavid S. Miller 
1217db8dac20SDavid S. Miller 		/*
1218db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1219db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1220db8dac20SDavid S. Miller 		 */
1221db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1222efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1223efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
1224db8dac20SDavid S. Miller 
1225db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1226db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1227842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
12284c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1229db8dac20SDavid S. Miller 	} else {
1230db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1231db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1232db8dac20SDavid S. Miller 
12331958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1234efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
12354c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
1236db8dac20SDavid S. Miller 		connected = 1;
1237db8dac20SDavid S. Miller 	}
1238db8dac20SDavid S. Miller 
12394c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
12404c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1241db8dac20SDavid S. Miller 
12424c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
12434c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
12449f690db7SYang Hongyang 
12454c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
124651953d5bSBrian Haley 
1247db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1248db8dac20SDavid S. Miller 		opt = &opt_space;
1249db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1250db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
1251db8dac20SDavid S. Miller 
125273df66f8STom Parkin 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1253ec0506dbSMaciej Żenczykowski 					    &hlimit, &tclass, &dontfrag);
1254db8dac20SDavid S. Miller 		if (err < 0) {
1255db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1256db8dac20SDavid S. Miller 			return err;
1257db8dac20SDavid S. Miller 		}
12584c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
12594c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
126063159f29SIan Morris 			if (!flowlabel)
1261db8dac20SDavid S. Miller 				return -EINVAL;
1262db8dac20SDavid S. Miller 		}
1263db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1264db8dac20SDavid S. Miller 			opt = NULL;
1265db8dac20SDavid S. Miller 		connected = 0;
1266db8dac20SDavid S. Miller 	}
126745f6fad8SEric Dumazet 	if (!opt) {
126845f6fad8SEric Dumazet 		opt = txopt_get(np);
126945f6fad8SEric Dumazet 		opt_to_free = opt;
127045f6fad8SEric Dumazet 	}
1271db8dac20SDavid S. Miller 	if (flowlabel)
1272db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1273db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
1274db8dac20SDavid S. Miller 
12754c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
1276876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
12774e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
1278876c7f41SBrian Haley 	else
12794c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
12804c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
12814e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
12821958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1283db8dac20SDavid S. Miller 
12844c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
128520c59de2SArnaud Ebalard 	if (final_p)
1286db8dac20SDavid S. Miller 		connected = 0;
1287db8dac20SDavid S. Miller 
12884c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
12894c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
1290db8dac20SDavid S. Miller 		connected = 0;
1291c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1292c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1293db8dac20SDavid S. Miller 
12944c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1295db8dac20SDavid S. Miller 
12960e0d44abSSteffen Klassert 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
129768d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
129868d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
129968d0c6d3SDavid S. Miller 		dst = NULL;
1300db8dac20SDavid S. Miller 		goto out;
1301db8dac20SDavid S. Miller 	}
1302db8dac20SDavid S. Miller 
1303db8dac20SDavid S. Miller 	if (hlimit < 0)
13045c98631cSLorenzo Colitti 		hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1305db8dac20SDavid S. Miller 
1306db8dac20SDavid S. Miller 	if (tclass < 0)
1307e651f03aSGerrit Renker 		tclass = np->tclass;
1308db8dac20SDavid S. Miller 
1309db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1310db8dac20SDavid S. Miller 		goto do_confirm;
1311db8dac20SDavid S. Miller back_from_confirm:
1312db8dac20SDavid S. Miller 
131303485f2aSVlad Yasevich 	/* Lockless fast path for the non-corking case */
131403485f2aSVlad Yasevich 	if (!corkreq) {
131503485f2aSVlad Yasevich 		struct sk_buff *skb;
131603485f2aSVlad Yasevich 
131703485f2aSVlad Yasevich 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
131803485f2aSVlad Yasevich 				   sizeof(struct udphdr), hlimit, tclass, opt,
131903485f2aSVlad Yasevich 				   &fl6, (struct rt6_info *)dst,
132003485f2aSVlad Yasevich 				   msg->msg_flags, dontfrag);
132103485f2aSVlad Yasevich 		err = PTR_ERR(skb);
132203485f2aSVlad Yasevich 		if (!IS_ERR_OR_NULL(skb))
132303485f2aSVlad Yasevich 			err = udp_v6_send_skb(skb, &fl6);
132403485f2aSVlad Yasevich 		goto release_dst;
132503485f2aSVlad Yasevich 	}
132603485f2aSVlad Yasevich 
1327db8dac20SDavid S. Miller 	lock_sock(sk);
1328db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1329db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1330db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1331db8dac20SDavid S. Miller 		release_sock(sk);
1332db8dac20SDavid S. Miller 
1333ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 2\n");
1334db8dac20SDavid S. Miller 		err = -EINVAL;
1335db8dac20SDavid S. Miller 		goto out;
1336db8dac20SDavid S. Miller 	}
1337db8dac20SDavid S. Miller 
1338db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1339db8dac20SDavid S. Miller 
1340db8dac20SDavid S. Miller do_append_data:
1341e36d3ff9SJiri Pirko 	if (dontfrag < 0)
1342e36d3ff9SJiri Pirko 		dontfrag = np->dontfrag;
1343db8dac20SDavid S. Miller 	up->len += ulen;
1344f69e6d13SAl Viro 	err = ip6_append_data(sk, getfrag, msg, ulen,
13454c9483b2SDavid S. Miller 		sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1346db8dac20SDavid S. Miller 		(struct rt6_info *)dst,
134713b52cd4SBrian Haley 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1348db8dac20SDavid S. Miller 	if (err)
1349db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1350db8dac20SDavid S. Miller 	else if (!corkreq)
1351db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1352db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1353db8dac20SDavid S. Miller 		up->pending = 0;
1354db8dac20SDavid S. Miller 
135503485f2aSVlad Yasevich 	if (err > 0)
135603485f2aSVlad Yasevich 		err = np->recverr ? net_xmit_errno(err) : 0;
135703485f2aSVlad Yasevich 	release_sock(sk);
135803485f2aSVlad Yasevich 
135903485f2aSVlad Yasevich release_dst:
1360db8dac20SDavid S. Miller 	if (dst) {
1361db8dac20SDavid S. Miller 		if (connected) {
1362db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
1363efe4208fSEric Dumazet 				      ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1364efe4208fSEric Dumazet 				      &sk->sk_v6_daddr : NULL,
1365db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
13664c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1367db8dac20SDavid S. Miller 				      &np->saddr :
1368db8dac20SDavid S. Miller #endif
1369db8dac20SDavid S. Miller 				      NULL);
1370db8dac20SDavid S. Miller 		} else {
1371db8dac20SDavid S. Miller 			dst_release(dst);
1372db8dac20SDavid S. Miller 		}
1373a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1374db8dac20SDavid S. Miller 	}
1375db8dac20SDavid S. Miller 
1376db8dac20SDavid S. Miller out:
1377a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1378db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
137945f6fad8SEric Dumazet 	txopt_put(opt_to_free);
1380db8dac20SDavid S. Miller 	if (!err)
1381db8dac20SDavid S. Miller 		return len;
1382db8dac20SDavid S. Miller 	/*
1383db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1384db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1385db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1386db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1387db8dac20SDavid S. Miller 	 * seems like overkill.
1388db8dac20SDavid S. Miller 	 */
1389db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1390235b9f7aSPavel Emelyanov 		UDP6_INC_STATS_USER(sock_net(sk),
1391235b9f7aSPavel Emelyanov 				UDP_MIB_SNDBUFERRORS, is_udplite);
1392db8dac20SDavid S. Miller 	}
1393db8dac20SDavid S. Miller 	return err;
1394db8dac20SDavid S. Miller 
1395db8dac20SDavid S. Miller do_confirm:
1396db8dac20SDavid S. Miller 	dst_confirm(dst);
1397db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1398db8dac20SDavid S. Miller 		goto back_from_confirm;
1399db8dac20SDavid S. Miller 	err = 0;
1400db8dac20SDavid S. Miller 	goto out;
1401db8dac20SDavid S. Miller }
1402db8dac20SDavid S. Miller 
14037d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1404db8dac20SDavid S. Miller {
140544046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1406db8dac20SDavid S. Miller 	lock_sock(sk);
1407db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1408db8dac20SDavid S. Miller 	release_sock(sk);
1409db8dac20SDavid S. Miller 
141044046a59STom Parkin 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
141144046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
141244046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
141344046a59STom Parkin 		if (encap_destroy)
141444046a59STom Parkin 			encap_destroy(sk);
141544046a59STom Parkin 	}
141644046a59STom Parkin 
1417db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1418db8dac20SDavid S. Miller }
1419db8dac20SDavid S. Miller 
1420db8dac20SDavid S. Miller /*
1421db8dac20SDavid S. Miller  *	Socket option code for UDP
1422db8dac20SDavid S. Miller  */
1423db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1424b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1425db8dac20SDavid S. Miller {
1426db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1427db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1428db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1429db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1430db8dac20SDavid S. Miller }
1431db8dac20SDavid S. Miller 
1432db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1433db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1434b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1435db8dac20SDavid S. Miller {
1436db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1437db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1438db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1439db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1440db8dac20SDavid S. Miller }
1441db8dac20SDavid S. Miller #endif
1442db8dac20SDavid S. Miller 
1443db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1444db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1445db8dac20SDavid S. Miller {
1446db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1447db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1448db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1449db8dac20SDavid S. Miller }
1450db8dac20SDavid S. Miller 
1451db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1452db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1453db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1454db8dac20SDavid S. Miller {
1455db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1456db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1457db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1458db8dac20SDavid S. Miller }
1459db8dac20SDavid S. Miller #endif
1460db8dac20SDavid S. Miller 
146141135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1462db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1463db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1464db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1465db8dac20SDavid S. Miller };
1466db8dac20SDavid S. Miller 
1467db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1468db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1469db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1470db8dac20SDavid S. Miller {
147117ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
147217ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
147317ef66afSLorenzo Colitti 	} else {
147417ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
147517ef66afSLorenzo Colitti 		struct inet_sock *inet = inet_sk(v);
147617ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
147717ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
147817ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
147917ef66afSLorenzo Colitti 	}
1480db8dac20SDavid S. Miller 	return 0;
1481db8dac20SDavid S. Miller }
1482db8dac20SDavid S. Miller 
148373cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = {
148473cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
148573cb88ecSArjan van de Ven 	.open     = udp_seq_open,
148673cb88ecSArjan van de Ven 	.read     = seq_read,
148773cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
148873cb88ecSArjan van de Ven 	.release  = seq_release_net
148973cb88ecSArjan van de Ven };
149073cb88ecSArjan van de Ven 
1491db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1492db8dac20SDavid S. Miller 	.name		= "udp6",
1493db8dac20SDavid S. Miller 	.family		= AF_INET6,
1494645ca708SEric Dumazet 	.udp_table	= &udp_table,
149573cb88ecSArjan van de Ven 	.seq_fops	= &udp6_afinfo_seq_fops,
1496dda61925SDenis V. Lunev 	.seq_ops	= {
1497dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1498dda61925SDenis V. Lunev 	},
1499db8dac20SDavid S. Miller };
1500db8dac20SDavid S. Miller 
15012c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1502db8dac20SDavid S. Miller {
15030c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1504db8dac20SDavid S. Miller }
1505db8dac20SDavid S. Miller 
1506ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1507ec120da6SIan Morris {
15080c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1509db8dac20SDavid S. Miller }
1510db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1511db8dac20SDavid S. Miller 
1512f77d6021SEric Dumazet void udp_v6_clear_sk(struct sock *sk, int size)
1513f77d6021SEric Dumazet {
1514f77d6021SEric Dumazet 	struct inet_sock *inet = inet_sk(sk);
1515f77d6021SEric Dumazet 
1516f77d6021SEric Dumazet 	/* we do not want to clear pinet6 field, because of RCU lookups */
1517f77d6021SEric Dumazet 	sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1518f77d6021SEric Dumazet 
1519f77d6021SEric Dumazet 	size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1520f77d6021SEric Dumazet 	memset(&inet->pinet6 + 1, 0, size);
1521f77d6021SEric Dumazet }
1522f77d6021SEric Dumazet 
1523db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1524db8dac20SDavid S. Miller 
1525db8dac20SDavid S. Miller struct proto udpv6_prot = {
1526db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1527db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1528db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1529db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1530db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1531db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1532db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1533db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1534db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1535db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1536db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1537f7ad74feSBenjamin LaHaise 	.backlog_rcv	   = __udpv6_queue_rcv_skb,
1538db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1539db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1540719f8358SEric Dumazet 	.rehash		   = udp_v6_rehash,
1541db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1542db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1543db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1544db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1545db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1546db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1547271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1548645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1549db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1550db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1551db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1552db8dac20SDavid S. Miller #endif
1553f77d6021SEric Dumazet 	.clear_sk	   = udp_v6_clear_sk,
1554db8dac20SDavid S. Miller };
1555db8dac20SDavid S. Miller 
1556db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1557db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1558db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1559db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1560db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1561db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1562db8dac20SDavid S. Miller };
1563db8dac20SDavid S. Miller 
1564db8dac20SDavid S. Miller int __init udpv6_init(void)
1565db8dac20SDavid S. Miller {
1566db8dac20SDavid S. Miller 	int ret;
1567db8dac20SDavid S. Miller 
15683336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
15693336288aSVlad Yasevich 	if (ret)
1570c6b641a4SVlad Yasevich 		goto out;
15713336288aSVlad Yasevich 
1572db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1573db8dac20SDavid S. Miller 	if (ret)
1574db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1575db8dac20SDavid S. Miller out:
1576db8dac20SDavid S. Miller 	return ret;
1577db8dac20SDavid S. Miller 
1578db8dac20SDavid S. Miller out_udpv6_protocol:
1579db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1580db8dac20SDavid S. Miller 	goto out;
1581db8dac20SDavid S. Miller }
1582db8dac20SDavid S. Miller 
1583db8dac20SDavid S. Miller void udpv6_exit(void)
1584db8dac20SDavid S. Miller {
1585db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1586db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1587db8dac20SDavid S. Miller }
1588