xref: /openbmc/linux/net/ipv6/udp.c (revision 02c22347)
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 
40496611d7SCraig Gallek #include <net/addrconf.h>
41db8dac20SDavid S. Miller #include <net/ndisc.h>
42db8dac20SDavid S. Miller #include <net/protocol.h>
43db8dac20SDavid S. Miller #include <net/transp_v6.h>
44db8dac20SDavid S. Miller #include <net/ip6_route.h>
45db8dac20SDavid S. Miller #include <net/raw.h>
46db8dac20SDavid S. Miller #include <net/tcp_states.h>
47db8dac20SDavid S. Miller #include <net/ip6_checksum.h>
48db8dac20SDavid S. Miller #include <net/xfrm.h>
4972289b96STom Herbert #include <net/inet6_hashtables.h>
50076bb0c8SEliezer Tamir #include <net/busy_poll.h>
51e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
52db8dac20SDavid S. Miller 
53db8dac20SDavid S. Miller #include <linux/proc_fs.h>
54db8dac20SDavid S. Miller #include <linux/seq_file.h>
5522911fc5SEric Dumazet #include <trace/events/skb.h>
56db8dac20SDavid S. Miller #include "udp_impl.h"
57db8dac20SDavid S. Miller 
586eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net,
59b50026b5SHannes Frederic Sowa 			const struct in6_addr *laddr,
60b50026b5SHannes Frederic Sowa 			const u16 lport,
61b50026b5SHannes Frederic Sowa 			const struct in6_addr *faddr,
62b50026b5SHannes Frederic Sowa 			const __be16 fport)
63b50026b5SHannes Frederic Sowa {
641bbdceefSHannes Frederic Sowa 	static u32 udp6_ehash_secret __read_mostly;
651bbdceefSHannes Frederic Sowa 	static u32 udp_ipv6_hash_secret __read_mostly;
661bbdceefSHannes Frederic Sowa 
671bbdceefSHannes Frederic Sowa 	u32 lhash, fhash;
681bbdceefSHannes Frederic Sowa 
691bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp6_ehash_secret,
701bbdceefSHannes Frederic Sowa 			    sizeof(udp6_ehash_secret));
711bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ipv6_hash_secret,
721bbdceefSHannes Frederic Sowa 			    sizeof(udp_ipv6_hash_secret));
731bbdceefSHannes Frederic Sowa 
741bbdceefSHannes Frederic Sowa 	lhash = (__force u32)laddr->s6_addr32[3];
751bbdceefSHannes Frederic Sowa 	fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
761bbdceefSHannes Frederic Sowa 
77b50026b5SHannes Frederic Sowa 	return __inet6_ehashfn(lhash, lport, fhash, fport,
781bbdceefSHannes Frederic Sowa 			       udp_ipv6_hash_secret + net_hash_mix(net));
79b50026b5SHannes Frederic Sowa }
80b50026b5SHannes Frederic Sowa 
816eada011SEric Dumazet static u32 udp6_portaddr_hash(const struct net *net,
82d4cada4aSEric Dumazet 			      const struct in6_addr *addr6,
83d4cada4aSEric Dumazet 			      unsigned int port)
84d4cada4aSEric Dumazet {
85d4cada4aSEric Dumazet 	unsigned int hash, mix = net_hash_mix(net);
86d4cada4aSEric Dumazet 
87d4cada4aSEric Dumazet 	if (ipv6_addr_any(addr6))
88d4cada4aSEric Dumazet 		hash = jhash_1word(0, mix);
89856540eeSBrian Haley 	else if (ipv6_addr_v4mapped(addr6))
900eae88f3SEric Dumazet 		hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
91d4cada4aSEric Dumazet 	else
920eae88f3SEric Dumazet 		hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
93d4cada4aSEric Dumazet 
94d4cada4aSEric Dumazet 	return hash ^ port;
95d4cada4aSEric Dumazet }
96d4cada4aSEric Dumazet 
976ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
98db8dac20SDavid S. Miller {
9930fff923SEric Dumazet 	unsigned int hash2_nulladdr =
10030fff923SEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
10130fff923SEric Dumazet 	unsigned int hash2_partial =
102efe4208fSEric Dumazet 		udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
10330fff923SEric Dumazet 
104d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
10530fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
10630fff923SEric Dumazet 	return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
107db8dac20SDavid S. Miller }
108db8dac20SDavid S. Miller 
109719f8358SEric Dumazet static void udp_v6_rehash(struct sock *sk)
110719f8358SEric Dumazet {
111719f8358SEric Dumazet 	u16 new_hash = udp6_portaddr_hash(sock_net(sk),
112efe4208fSEric Dumazet 					  &sk->sk_v6_rcv_saddr,
113719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
114719f8358SEric Dumazet 
115719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
116719f8358SEric Dumazet }
117719f8358SEric Dumazet 
118645ca708SEric Dumazet static inline int compute_score(struct sock *sk, struct net *net,
119645ca708SEric Dumazet 				unsigned short hnum,
12088440ae7SBalazs Scheidler 				const struct in6_addr *saddr, __be16 sport,
12188440ae7SBalazs Scheidler 				const struct in6_addr *daddr, __be16 dport,
122645ca708SEric Dumazet 				int dif)
123db8dac20SDavid S. Miller {
12460c04aecSJoe Perches 	int score;
12560c04aecSJoe Perches 	struct inet_sock *inet;
126db8dac20SDavid S. Miller 
12760c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
12860c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
12960c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
13060c04aecSJoe Perches 		return -1;
131645ca708SEric Dumazet 
132645ca708SEric Dumazet 	score = 0;
13360c04aecSJoe Perches 	inet = inet_sk(sk);
13460c04aecSJoe Perches 
135c720c7e8SEric Dumazet 	if (inet->inet_dport) {
136c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
137645ca708SEric Dumazet 			return -1;
138db8dac20SDavid S. Miller 		score++;
139db8dac20SDavid S. Miller 	}
14060c04aecSJoe Perches 
141efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
142efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
143645ca708SEric Dumazet 			return -1;
144db8dac20SDavid S. Miller 		score++;
145db8dac20SDavid S. Miller 	}
14660c04aecSJoe Perches 
147efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
148efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
149645ca708SEric Dumazet 			return -1;
150db8dac20SDavid S. Miller 		score++;
151db8dac20SDavid S. Miller 	}
15260c04aecSJoe Perches 
153db8dac20SDavid S. Miller 	if (sk->sk_bound_dev_if) {
154db8dac20SDavid S. Miller 		if (sk->sk_bound_dev_if != dif)
155645ca708SEric Dumazet 			return -1;
156db8dac20SDavid S. Miller 		score++;
157db8dac20SDavid S. Miller 	}
15860c04aecSJoe Perches 
15970da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
16070da268bSEric Dumazet 		score++;
16170da268bSEric Dumazet 
162645ca708SEric Dumazet 	return score;
163645ca708SEric Dumazet }
164645ca708SEric Dumazet 
165fddc17deSEric Dumazet static inline int compute_score2(struct sock *sk, struct net *net,
166fddc17deSEric Dumazet 				 const struct in6_addr *saddr, __be16 sport,
16760c04aecSJoe Perches 				 const struct in6_addr *daddr,
16860c04aecSJoe Perches 				 unsigned short hnum, int dif)
169fddc17deSEric Dumazet {
17060c04aecSJoe Perches 	int score;
17160c04aecSJoe Perches 	struct inet_sock *inet;
172fddc17deSEric Dumazet 
17360c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
17460c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
17560c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
17660c04aecSJoe Perches 		return -1;
177fddc17deSEric Dumazet 
178efe4208fSEric Dumazet 	if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
179fddc17deSEric Dumazet 		return -1;
18060c04aecSJoe Perches 
181fddc17deSEric Dumazet 	score = 0;
18260c04aecSJoe Perches 	inet = inet_sk(sk);
18360c04aecSJoe Perches 
184fddc17deSEric Dumazet 	if (inet->inet_dport) {
185fddc17deSEric Dumazet 		if (inet->inet_dport != sport)
186fddc17deSEric Dumazet 			return -1;
187fddc17deSEric Dumazet 		score++;
188fddc17deSEric Dumazet 	}
18960c04aecSJoe Perches 
190efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
191efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
192fddc17deSEric Dumazet 			return -1;
193fddc17deSEric Dumazet 		score++;
194fddc17deSEric Dumazet 	}
19560c04aecSJoe Perches 
196fddc17deSEric Dumazet 	if (sk->sk_bound_dev_if) {
197fddc17deSEric Dumazet 		if (sk->sk_bound_dev_if != dif)
198fddc17deSEric Dumazet 			return -1;
199fddc17deSEric Dumazet 		score++;
200fddc17deSEric Dumazet 	}
20160c04aecSJoe Perches 
20270da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
20370da268bSEric Dumazet 		score++;
20470da268bSEric Dumazet 
205fddc17deSEric Dumazet 	return score;
206fddc17deSEric Dumazet }
207fddc17deSEric Dumazet 
208fddc17deSEric Dumazet /* called with read_rcu_lock() */
209fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
210fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
211fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
2121134158bSCraig Gallek 		struct udp_hslot *hslot2, unsigned int slot2,
2131134158bSCraig Gallek 		struct sk_buff *skb)
214fddc17deSEric Dumazet {
215fddc17deSEric Dumazet 	struct sock *sk, *result;
21672289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
21772289b96STom Herbert 	u32 hash = 0;
218fddc17deSEric Dumazet 
219fddc17deSEric Dumazet 	result = NULL;
220fddc17deSEric Dumazet 	badness = -1;
221ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
222fddc17deSEric Dumazet 		score = compute_score2(sk, net, saddr, sport,
223fddc17deSEric Dumazet 				      daddr, hnum, dif);
224fddc17deSEric Dumazet 		if (score > badness) {
22572289b96STom Herbert 			reuseport = sk->sk_reuseport;
22672289b96STom Herbert 			if (reuseport) {
227b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
22872289b96STom Herbert 						    saddr, sport);
229ed0dfffdSEric Dumazet 
230ca065d0cSEric Dumazet 				result = reuseport_select_sock(sk, hash, skb,
2311134158bSCraig Gallek 							sizeof(struct udphdr));
232ca065d0cSEric Dumazet 				if (result)
233ca065d0cSEric Dumazet 					return result;
23472289b96STom Herbert 				matches = 1;
23570da268bSEric Dumazet 			}
236ca065d0cSEric Dumazet 			result = sk;
237ca065d0cSEric Dumazet 			badness = score;
23872289b96STom Herbert 		} else if (score == badness && reuseport) {
23972289b96STom Herbert 			matches++;
2408fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
24172289b96STom Herbert 				result = sk;
24272289b96STom Herbert 			hash = next_pseudo_random32(hash);
243fddc17deSEric Dumazet 		}
244fddc17deSEric Dumazet 	}
245fddc17deSEric Dumazet 	return result;
246fddc17deSEric Dumazet }
247fddc17deSEric Dumazet 
248ca065d0cSEric Dumazet /* rcu_read_lock() must be held */
249fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
25088440ae7SBalazs Scheidler 				      const struct in6_addr *saddr, __be16 sport,
25188440ae7SBalazs Scheidler 				      const struct in6_addr *daddr, __be16 dport,
252538950a1SCraig Gallek 				      int dif, struct udp_table *udptable,
253538950a1SCraig Gallek 				      struct sk_buff *skb)
254645ca708SEric Dumazet {
255271b72c7SEric Dumazet 	struct sock *sk, *result;
256645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
257fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
258fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
25972289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
26072289b96STom Herbert 	u32 hash = 0;
261645ca708SEric Dumazet 
262fddc17deSEric Dumazet 	if (hslot->count > 10) {
263fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
264fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
265fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
266fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
267fddc17deSEric Dumazet 			goto begin;
268fddc17deSEric Dumazet 
269fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
270fddc17deSEric Dumazet 					  daddr, hnum, dif,
2711134158bSCraig Gallek 					  hslot2, slot2, skb);
272fddc17deSEric Dumazet 		if (!result) {
273fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
274fddc17deSEric Dumazet 			slot2 = hash2 & udptable->mask;
275fddc17deSEric Dumazet 			hslot2 = &udptable->hash2[slot2];
276fddc17deSEric Dumazet 			if (hslot->count < hslot2->count)
277fddc17deSEric Dumazet 				goto begin;
278fddc17deSEric Dumazet 
2791223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
2801223c67cSJorge Boncompte [DTI2] 						  &in6addr_any, hnum, dif,
2811134158bSCraig Gallek 						  hslot2, slot2, skb);
282fddc17deSEric Dumazet 		}
283fddc17deSEric Dumazet 		return result;
284fddc17deSEric Dumazet 	}
285271b72c7SEric Dumazet begin:
286271b72c7SEric Dumazet 	result = NULL;
287271b72c7SEric Dumazet 	badness = -1;
288ca065d0cSEric Dumazet 	sk_for_each_rcu(sk, &hslot->head) {
289645ca708SEric Dumazet 		score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
290645ca708SEric Dumazet 		if (score > badness) {
29172289b96STom Herbert 			reuseport = sk->sk_reuseport;
29272289b96STom Herbert 			if (reuseport) {
293b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
29472289b96STom Herbert 						    saddr, sport);
295ca065d0cSEric Dumazet 				result = reuseport_select_sock(sk, hash, skb,
296538950a1SCraig Gallek 							sizeof(struct udphdr));
297ca065d0cSEric Dumazet 				if (result)
298ca065d0cSEric Dumazet 					return result;
29972289b96STom Herbert 				matches = 1;
30072289b96STom Herbert 			}
301ca065d0cSEric Dumazet 			result = sk;
302ca065d0cSEric Dumazet 			badness = score;
30372289b96STom Herbert 		} else if (score == badness && reuseport) {
30472289b96STom Herbert 			matches++;
3058fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
30672289b96STom Herbert 				result = sk;
30772289b96STom Herbert 			hash = next_pseudo_random32(hash);
308db8dac20SDavid S. Miller 		}
309db8dac20SDavid S. Miller 	}
310db8dac20SDavid S. Miller 	return result;
311db8dac20SDavid S. Miller }
312fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
313db8dac20SDavid S. Miller 
314607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
315607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
316645ca708SEric Dumazet 					  struct udp_table *udptable)
317607c4aafSKOVACS Krisztian {
31823542618SKOVACS Krisztian 	struct sock *sk;
319b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
320607c4aafSKOVACS Krisztian 
321e5d08d71SIan Morris 	sk = skb_steal_sock(skb);
322e5d08d71SIan Morris 	if (unlikely(sk))
32323542618SKOVACS Krisztian 		return sk;
324adf30907SEric Dumazet 	return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
325607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
326538950a1SCraig Gallek 				 udptable, skb);
327607c4aafSKOVACS Krisztian }
328607c4aafSKOVACS Krisztian 
32963058308STom Herbert struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
33063058308STom Herbert 				 __be16 sport, __be16 dport)
33163058308STom Herbert {
33263058308STom Herbert 	const struct ipv6hdr *iph = ipv6_hdr(skb);
33363058308STom Herbert 	const struct net_device *dev =
33463058308STom Herbert 	    skb_dst(skb) ? skb_dst(skb)->dev : skb->dev;
33563058308STom Herbert 
33663058308STom Herbert 	return __udp6_lib_lookup(dev_net(dev), &iph->saddr, sport,
33763058308STom Herbert 				 &iph->daddr, dport, inet6_iif(skb),
33863058308STom Herbert 				 &udp_table, skb);
33963058308STom Herbert }
34063058308STom Herbert EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
34163058308STom Herbert 
342ca065d0cSEric Dumazet /* Must be called under rcu_read_lock().
343ca065d0cSEric Dumazet  * Does increment socket refcount.
344ca065d0cSEric Dumazet  */
345ca065d0cSEric Dumazet #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \
346ca065d0cSEric Dumazet     IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY)
347aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
348aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
349aa976fc0SBalazs Scheidler {
350ca065d0cSEric Dumazet 	struct sock *sk;
351ca065d0cSEric Dumazet 
352ca065d0cSEric Dumazet 	sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
353ca065d0cSEric Dumazet 				dif, &udp_table, NULL);
354ca065d0cSEric Dumazet 	if (sk && !atomic_inc_not_zero(&sk->sk_refcnt))
355ca065d0cSEric Dumazet 		sk = NULL;
356ca065d0cSEric Dumazet 	return sk;
357aa976fc0SBalazs Scheidler }
358aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
359ca065d0cSEric Dumazet #endif
360aa976fc0SBalazs Scheidler 
361db8dac20SDavid S. Miller /*
362db8dac20SDavid S. Miller  *	This should be easy, if there is something there we
363db8dac20SDavid S. Miller  *	return it, otherwise we block.
364db8dac20SDavid S. Miller  */
365db8dac20SDavid S. Miller 
3661b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
367db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
368db8dac20SDavid S. Miller {
369db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
370db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
371db8dac20SDavid S. Miller 	struct sk_buff *skb;
37259c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
373627d2d6bSsamanthakumar 	int peeked, peeking, off;
374db8dac20SDavid S. Miller 	int err;
375db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
376197c949eSEric Dumazet 	bool checksum_valid = false;
377f26ba175SWei Yongjun 	int is_udp4;
3788a74ad60SEric Dumazet 	bool slow;
379db8dac20SDavid S. Miller 
380db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
38185fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
382db8dac20SDavid S. Miller 
3834b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
38485fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
3854b340ae2SBrian Haley 
386db8dac20SDavid S. Miller try_again:
387627d2d6bSsamanthakumar 	peeking = off = sk_peek_offset(sk, flags);
388db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
3893f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
390db8dac20SDavid S. Miller 	if (!skb)
391627d2d6bSsamanthakumar 		return err;
392db8dac20SDavid S. Miller 
393e6afc8acSsamanthakumar 	ulen = skb->len;
39459c2cdaeSDavid S. Miller 	copied = len;
395627d2d6bSsamanthakumar 	if (copied > ulen - off)
396627d2d6bSsamanthakumar 		copied = ulen - off;
39759c2cdaeSDavid S. Miller 	else if (copied < ulen)
398db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
399db8dac20SDavid S. Miller 
400f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
401f26ba175SWei Yongjun 
402db8dac20SDavid S. Miller 	/*
403db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
404db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
405db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
406db8dac20SDavid S. Miller 	 */
407db8dac20SDavid S. Miller 
408627d2d6bSsamanthakumar 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov || peeking) {
409197c949eSEric Dumazet 		checksum_valid = !udp_lib_checksum_complete(skb);
410197c949eSEric Dumazet 		if (!checksum_valid)
411db8dac20SDavid S. Miller 			goto csum_copy_err;
412db8dac20SDavid S. Miller 	}
413db8dac20SDavid S. Miller 
414197c949eSEric Dumazet 	if (checksum_valid || skb_csum_unnecessary(skb))
415627d2d6bSsamanthakumar 		err = skb_copy_datagram_msg(skb, off, msg, copied);
416db8dac20SDavid S. Miller 	else {
417627d2d6bSsamanthakumar 		err = skb_copy_and_csum_datagram_msg(skb, off, msg);
418db8dac20SDavid S. Miller 		if (err == -EINVAL)
419db8dac20SDavid S. Miller 			goto csum_copy_err;
420db8dac20SDavid S. Miller 	}
42122911fc5SEric Dumazet 	if (unlikely(err)) {
42222911fc5SEric Dumazet 		trace_kfree_skb(skb, udpv6_recvmsg);
423979402b1SEric Dumazet 		if (!peeked) {
424979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
425979402b1SEric Dumazet 			if (is_udp4)
4266aef70a8SEric Dumazet 				UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
427979402b1SEric Dumazet 					      is_udplite);
428979402b1SEric Dumazet 			else
4296aef70a8SEric Dumazet 				UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
430979402b1SEric Dumazet 					       is_udplite);
431979402b1SEric Dumazet 		}
432627d2d6bSsamanthakumar 		skb_free_datagram_locked(sk, skb);
433627d2d6bSsamanthakumar 		return err;
43422911fc5SEric Dumazet 	}
435f26ba175SWei Yongjun 	if (!peeked) {
436f26ba175SWei Yongjun 		if (is_udp4)
4376aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
4386aef70a8SEric Dumazet 				      is_udplite);
439f26ba175SWei Yongjun 		else
4406aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
4416aef70a8SEric Dumazet 				       is_udplite);
442f26ba175SWei Yongjun 	}
443db8dac20SDavid S. Miller 
4443b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
445db8dac20SDavid S. Miller 
446db8dac20SDavid S. Miller 	/* Copy the address. */
447db8dac20SDavid S. Miller 	if (msg->msg_name) {
448342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
449db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
450db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
451db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
452db8dac20SDavid S. Miller 
453842df073SHannes Frederic Sowa 		if (is_udp4) {
454b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
455b301e82cSBrian Haley 					       &sin6->sin6_addr);
456842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
457842df073SHannes Frederic Sowa 		} else {
4584e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
459842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
460842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
4614330487aSDuan Jiong 						    inet6_iif(skb));
462db8dac20SDavid S. Miller 		}
463bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
464db8dac20SDavid S. Miller 	}
4654b261c75SHannes Frederic Sowa 
4664b261c75SHannes Frederic Sowa 	if (np->rxopt.all)
4674b261c75SHannes Frederic Sowa 		ip6_datagram_recv_common_ctl(sk, msg, skb);
4684b261c75SHannes Frederic Sowa 
469f26ba175SWei Yongjun 	if (is_udp4) {
470db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
471db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
472db8dac20SDavid S. Miller 	} else {
473db8dac20SDavid S. Miller 		if (np->rxopt.all)
4744b261c75SHannes Frederic Sowa 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
475db8dac20SDavid S. Miller 	}
476db8dac20SDavid S. Miller 
47759c2cdaeSDavid S. Miller 	err = copied;
478db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
479db8dac20SDavid S. Miller 		err = ulen;
480db8dac20SDavid S. Miller 
481627d2d6bSsamanthakumar 	__skb_free_datagram_locked(sk, skb, peeking ? -err : err);
482db8dac20SDavid S. Miller 	return err;
483db8dac20SDavid S. Miller 
484db8dac20SDavid S. Miller csum_copy_err:
4858a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
4860856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
4876a5dc9e5SEric Dumazet 		if (is_udp4) {
4886aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk),
4896a5dc9e5SEric Dumazet 				      UDP_MIB_CSUMERRORS, is_udplite);
4906aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk),
4910856f939SWei Yongjun 				      UDP_MIB_INERRORS, is_udplite);
4926a5dc9e5SEric Dumazet 		} else {
4936aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
4946a5dc9e5SEric Dumazet 				       UDP_MIB_CSUMERRORS, is_udplite);
4956aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
4960856f939SWei Yongjun 				       UDP_MIB_INERRORS, is_udplite);
4970856f939SWei Yongjun 		}
4986a5dc9e5SEric Dumazet 	}
4998a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
500db8dac20SDavid S. Miller 
501beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
502beb39db5SEric Dumazet 	cond_resched();
5039cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
504db8dac20SDavid S. Miller 	goto try_again;
505db8dac20SDavid S. Miller }
506db8dac20SDavid S. Miller 
507db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
508d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
509645ca708SEric Dumazet 		    struct udp_table *udptable)
510db8dac20SDavid S. Miller {
511db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
512b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
513b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
514b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
515db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
516db8dac20SDavid S. Miller 	struct sock *sk;
517e0d8c1b7SWei Wang 	int harderr;
518db8dac20SDavid S. Miller 	int err;
5197304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
520db8dac20SDavid S. Miller 
521e32ea7e7SCraig Gallek 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
522538950a1SCraig Gallek 			       inet6_iif(skb), udptable, skb);
52363159f29SIan Morris 	if (!sk) {
5247304fe46SDuan Jiong 		ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
5257304fe46SDuan Jiong 				   ICMP6_MIB_INERRORS);
526db8dac20SDavid S. Miller 		return;
5277304fe46SDuan Jiong 	}
528db8dac20SDavid S. Miller 
529e0d8c1b7SWei Wang 	harderr = icmpv6_err_convert(type, code, &err);
530e0d8c1b7SWei Wang 	np = inet6_sk(sk);
531e0d8c1b7SWei Wang 
53293b36cf3SHannes Frederic Sowa 	if (type == ICMPV6_PKT_TOOBIG) {
53393b36cf3SHannes Frederic Sowa 		if (!ip6_sk_accept_pmtu(sk))
53493b36cf3SHannes Frederic Sowa 			goto out;
53581aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
536e0d8c1b7SWei Wang 		if (np->pmtudisc != IPV6_PMTUDISC_DONT)
537e0d8c1b7SWei Wang 			harderr = 1;
53893b36cf3SHannes Frederic Sowa 	}
5391a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
540ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
5411a462d18SDuan Jiong 		goto out;
5421a462d18SDuan Jiong 	}
54381aded24SDavid S. Miller 
544e0d8c1b7SWei Wang 	if (!np->recverr) {
545e0d8c1b7SWei Wang 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
546db8dac20SDavid S. Miller 			goto out;
547e0d8c1b7SWei Wang 	} else {
548db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
549e0d8c1b7SWei Wang 	}
550b1faf566SEric Dumazet 
551db8dac20SDavid S. Miller 	sk->sk_err = err;
552db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
553db8dac20SDavid S. Miller out:
554ca065d0cSEric Dumazet 	return;
555db8dac20SDavid S. Miller }
556db8dac20SDavid S. Miller 
557f7ad74feSBenjamin LaHaise static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
558f7ad74feSBenjamin LaHaise {
559f7ad74feSBenjamin LaHaise 	int rc;
560f7ad74feSBenjamin LaHaise 
561efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
562f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
563005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
5642c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
565005ec974SShawn Bohrer 	}
566f7ad74feSBenjamin LaHaise 
567e6afc8acSsamanthakumar 	rc = __sock_queue_rcv_skb(sk, skb);
568f7ad74feSBenjamin LaHaise 	if (rc < 0) {
569f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
570f7ad74feSBenjamin LaHaise 
571f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
572f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
57302c22347SEric Dumazet 			__UDP6_INC_STATS(sock_net(sk),
574f7ad74feSBenjamin LaHaise 					 UDP_MIB_RCVBUFERRORS, is_udplite);
57502c22347SEric Dumazet 		__UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
576f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
577f7ad74feSBenjamin LaHaise 		return -1;
578f7ad74feSBenjamin LaHaise 	}
579f7ad74feSBenjamin LaHaise 	return 0;
580f7ad74feSBenjamin LaHaise }
581f7ad74feSBenjamin LaHaise 
582db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
583d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
584d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info)
585db8dac20SDavid S. Miller {
586645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
587db8dac20SDavid S. Miller }
588db8dac20SDavid S. Miller 
589d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly;
590d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void)
591d7f3f621SBenjamin LaHaise {
592d7f3f621SBenjamin LaHaise 	if (!static_key_enabled(&udpv6_encap_needed))
593d7f3f621SBenjamin LaHaise 		static_key_slow_inc(&udpv6_encap_needed);
594d7f3f621SBenjamin LaHaise }
595d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable);
596d7f3f621SBenjamin LaHaise 
597db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
598db8dac20SDavid S. Miller {
599db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
600db8dac20SDavid S. Miller 	int rc;
601db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
602db8dac20SDavid S. Miller 
603db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
604db8dac20SDavid S. Miller 		goto drop;
605db8dac20SDavid S. Miller 
606d7f3f621SBenjamin LaHaise 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
607d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
608d7f3f621SBenjamin LaHaise 
609d7f3f621SBenjamin LaHaise 		/*
610d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
611d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
612d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
613d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
614d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
615d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
616d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
617d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
618d7f3f621SBenjamin LaHaise 		 */
619d7f3f621SBenjamin LaHaise 
620d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
621d7f3f621SBenjamin LaHaise 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
62253b24b8fSIan Morris 		if (skb->len > sizeof(struct udphdr) && encap_rcv) {
623d7f3f621SBenjamin LaHaise 			int ret;
624d7f3f621SBenjamin LaHaise 
6250a80966bSTom Herbert 			/* Verify checksum before giving to encap */
6260a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
6270a80966bSTom Herbert 				goto csum_error;
6280a80966bSTom Herbert 
629d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
630d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
63102c22347SEric Dumazet 				__UDP_INC_STATS(sock_net(sk),
632d7f3f621SBenjamin LaHaise 						UDP_MIB_INDATAGRAMS,
633d7f3f621SBenjamin LaHaise 						is_udplite);
634d7f3f621SBenjamin LaHaise 				return -ret;
635d7f3f621SBenjamin LaHaise 			}
636d7f3f621SBenjamin LaHaise 		}
637d7f3f621SBenjamin LaHaise 
638d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
639d7f3f621SBenjamin LaHaise 	}
640d7f3f621SBenjamin LaHaise 
641db8dac20SDavid S. Miller 	/*
642db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
643db8dac20SDavid S. Miller 	 */
644db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
645db8dac20SDavid S. Miller 
646db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
647ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
648db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
649db8dac20SDavid S. Miller 			goto drop;
650db8dac20SDavid S. Miller 		}
651db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
652ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
653db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
654db8dac20SDavid S. Miller 			goto drop;
655db8dac20SDavid S. Miller 		}
656db8dac20SDavid S. Miller 	}
657db8dac20SDavid S. Miller 
65833d480ceSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter)) {
659db8dac20SDavid S. Miller 		if (udp_lib_checksum_complete(skb))
6606a5dc9e5SEric Dumazet 			goto csum_error;
661e6afc8acSsamanthakumar 		if (sk_filter(sk, skb))
662e6afc8acSsamanthakumar 			goto drop;
663db8dac20SDavid S. Miller 	}
664db8dac20SDavid S. Miller 
665e6afc8acSsamanthakumar 	udp_csum_pull_header(skb);
666274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
66702c22347SEric Dumazet 		__UDP6_INC_STATS(sock_net(sk),
6683e215c8dSJames M Leddy 				 UDP_MIB_RCVBUFERRORS, is_udplite);
669cb80ef46SBenjamin LaHaise 		goto drop;
6703e215c8dSJames M Leddy 	}
671cb80ef46SBenjamin LaHaise 
672d826eb14SEric Dumazet 	skb_dst_drop(skb);
673db8dac20SDavid S. Miller 
674cb80ef46SBenjamin LaHaise 	bh_lock_sock(sk);
675cb80ef46SBenjamin LaHaise 	rc = 0;
676cb80ef46SBenjamin LaHaise 	if (!sock_owned_by_user(sk))
677f7ad74feSBenjamin LaHaise 		rc = __udpv6_queue_rcv_skb(sk, skb);
678cb80ef46SBenjamin LaHaise 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
679cb80ef46SBenjamin LaHaise 		bh_unlock_sock(sk);
680cb80ef46SBenjamin LaHaise 		goto drop;
681cb80ef46SBenjamin LaHaise 	}
682cb80ef46SBenjamin LaHaise 	bh_unlock_sock(sk);
683f7ad74feSBenjamin LaHaise 
684f7ad74feSBenjamin LaHaise 	return rc;
6853e215c8dSJames M Leddy 
6866a5dc9e5SEric Dumazet csum_error:
68702c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
688db8dac20SDavid S. Miller drop:
68902c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
690cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
691db8dac20SDavid S. Miller 	kfree_skb(skb);
692db8dac20SDavid S. Miller 	return -1;
693db8dac20SDavid S. Miller }
694db8dac20SDavid S. Miller 
6955cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
696b71d1d42SEric Dumazet 				   __be16 loc_port, const struct in6_addr *loc_addr,
697b71d1d42SEric Dumazet 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
6985cf3d461SDavid Held 				   int dif, unsigned short hnum)
699db8dac20SDavid S. Miller {
7009e89fd8bSSven Wegener 	struct inet_sock *inet = inet_sk(sk);
701db8dac20SDavid S. Miller 
7029e89fd8bSSven Wegener 	if (!net_eq(sock_net(sk), net))
7035cf3d461SDavid Held 		return false;
704b8ad0cbcSDaniel Lezcano 
7055cf3d461SDavid Held 	if (udp_sk(sk)->udp_port_hash != hnum ||
7065cf3d461SDavid Held 	    sk->sk_family != PF_INET6 ||
7075cf3d461SDavid Held 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
7085cf3d461SDavid Held 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
7095cf3d461SDavid Held 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
71033b4b015SHenning Rogge 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
71133b4b015SHenning Rogge 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
71233b4b015SHenning Rogge 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
7135cf3d461SDavid Held 		return false;
7149e89fd8bSSven Wegener 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
7155cf3d461SDavid Held 		return false;
7165cf3d461SDavid Held 	return true;
717db8dac20SDavid S. Miller }
718db8dac20SDavid S. Miller 
7194068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
7204068579eSTom Herbert {
7214068579eSTom Herbert 	/* RFC 2460 section 8.1 says that we SHOULD log
7224068579eSTom Herbert 	 * this error. Well, it is reasonable.
7234068579eSTom Herbert 	 */
724ba7a46f1SJoe Perches 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
7254068579eSTom Herbert 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
7264068579eSTom Herbert 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
7274068579eSTom Herbert }
7284068579eSTom Herbert 
729db8dac20SDavid S. Miller /*
730db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
731db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
732db8dac20SDavid S. Miller  */
733e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
734b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
73536cbb245SRick Jones 		struct udp_table *udptable, int proto)
736db8dac20SDavid S. Miller {
737ca065d0cSEric Dumazet 	struct sock *sk, *first = NULL;
738db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
7395cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
7405cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
741ca065d0cSEric Dumazet 	unsigned int offset = offsetof(typeof(*sk), sk_node);
7422dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
743ca065d0cSEric Dumazet 	int dif = inet6_iif(skb);
744ca065d0cSEric Dumazet 	struct hlist_node *node;
745ca065d0cSEric Dumazet 	struct sk_buff *nskb;
7462dc41cffSDavid Held 
7472dc41cffSDavid Held 	if (use_hash2) {
7482dc41cffSDavid Held 		hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
7492dc41cffSDavid Held 			    udp_table.mask;
7502dc41cffSDavid Held 		hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask;
7512dc41cffSDavid Held start_lookup:
7522dc41cffSDavid Held 		hslot = &udp_table.hash2[hash2];
7532dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
7542dc41cffSDavid Held 	}
755db8dac20SDavid S. Miller 
756ca065d0cSEric Dumazet 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
757ca065d0cSEric Dumazet 		if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
758ca065d0cSEric Dumazet 					    uh->source, saddr, dif, hnum))
759ca065d0cSEric Dumazet 			continue;
7601c19448cSTom Herbert 		/* If zero checksum and no_check is not on for
7614068579eSTom Herbert 		 * the socket then skip it.
7624068579eSTom Herbert 		 */
763ca065d0cSEric Dumazet 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
764ca065d0cSEric Dumazet 			continue;
765ca065d0cSEric Dumazet 		if (!first) {
766ca065d0cSEric Dumazet 			first = sk;
767ca065d0cSEric Dumazet 			continue;
768db8dac20SDavid S. Miller 		}
769ca065d0cSEric Dumazet 		nskb = skb_clone(skb, GFP_ATOMIC);
770ca065d0cSEric Dumazet 		if (unlikely(!nskb)) {
771ca065d0cSEric Dumazet 			atomic_inc(&sk->sk_drops);
77202c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
773ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
77402c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_INERRORS,
775ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
776ca065d0cSEric Dumazet 			continue;
777a1ab77f9SEric Dumazet 		}
778db8dac20SDavid S. Miller 
779ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(sk, nskb) > 0)
780ca065d0cSEric Dumazet 			consume_skb(nskb);
781ca065d0cSEric Dumazet 	}
782a1ab77f9SEric Dumazet 
7832dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
7842dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
7852dc41cffSDavid Held 		hash2 = hash2_any;
7862dc41cffSDavid Held 		goto start_lookup;
7872dc41cffSDavid Held 	}
7882dc41cffSDavid Held 
789ca065d0cSEric Dumazet 	if (first) {
790ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(first, skb) > 0)
791ca065d0cSEric Dumazet 			consume_skb(skb);
792a1ab77f9SEric Dumazet 	} else {
793ca065d0cSEric Dumazet 		kfree_skb(skb);
79402c22347SEric Dumazet 		__UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
79536cbb245SRick Jones 				 proto == IPPROTO_UDPLITE);
796a1ab77f9SEric Dumazet 	}
797db8dac20SDavid S. Miller 	return 0;
798db8dac20SDavid S. Miller }
799db8dac20SDavid S. Miller 
800645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
801db8dac20SDavid S. Miller 		   int proto)
802db8dac20SDavid S. Miller {
803b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
804ca065d0cSEric Dumazet 	struct net *net = dev_net(skb->dev);
805ca065d0cSEric Dumazet 	struct udphdr *uh;
806ca065d0cSEric Dumazet 	struct sock *sk;
807db8dac20SDavid S. Miller 	u32 ulen = 0;
808db8dac20SDavid S. Miller 
809db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
810d6bc0149SBjørn Mork 		goto discard;
811db8dac20SDavid S. Miller 
812db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
813db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
814db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
815db8dac20SDavid S. Miller 
816db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
817db8dac20SDavid S. Miller 	if (ulen > skb->len)
818db8dac20SDavid S. Miller 		goto short_packet;
819db8dac20SDavid S. Miller 
820db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
821db8dac20SDavid S. Miller 		/* UDP validates ulen. */
822db8dac20SDavid S. Miller 
823db8dac20SDavid S. Miller 		/* Check for jumbo payload */
824db8dac20SDavid S. Miller 		if (ulen == 0)
825db8dac20SDavid S. Miller 			ulen = skb->len;
826db8dac20SDavid S. Miller 
827db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
828db8dac20SDavid S. Miller 			goto short_packet;
829db8dac20SDavid S. Miller 
830db8dac20SDavid S. Miller 		if (ulen < skb->len) {
831db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
832db8dac20SDavid S. Miller 				goto short_packet;
833db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
834db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
835db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
836db8dac20SDavid S. Miller 		}
837db8dac20SDavid S. Miller 	}
838db8dac20SDavid S. Miller 
839db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
8406a5dc9e5SEric Dumazet 		goto csum_error;
841db8dac20SDavid S. Miller 
842db8dac20SDavid S. Miller 	/*
843db8dac20SDavid S. Miller 	 *	Multicast receive code
844db8dac20SDavid S. Miller 	 */
845db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
846e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
84736cbb245SRick Jones 				saddr, daddr, udptable, proto);
848db8dac20SDavid S. Miller 
849db8dac20SDavid S. Miller 	/* Unicast */
850db8dac20SDavid S. Miller 
851db8dac20SDavid S. Miller 	/*
852db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
853db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
854db8dac20SDavid S. Miller 	 */
855607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
85653b24b8fSIan Morris 	if (sk) {
857a5b50476SEliezer Tamir 		int ret;
858a5b50476SEliezer Tamir 
8591c19448cSTom Herbert 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
8604068579eSTom Herbert 			udp6_csum_zero_error(skb);
8614068579eSTom Herbert 			goto csum_error;
8624068579eSTom Herbert 		}
8634068579eSTom Herbert 
864224d019cSTom Herbert 		if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
8652abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
8662abb7cdcSTom Herbert 						 ip6_compute_pseudo);
8672abb7cdcSTom Herbert 
868a5b50476SEliezer Tamir 		ret = udpv6_queue_rcv_skb(sk, skb);
869db8dac20SDavid S. Miller 
87059dca1d8SBill Sommerfeld 		/* a return value > 0 means to resubmit the input */
871cb80ef46SBenjamin LaHaise 		if (ret > 0)
87259dca1d8SBill Sommerfeld 			return ret;
873cb80ef46SBenjamin LaHaise 
874cb80ef46SBenjamin LaHaise 		return 0;
875cb80ef46SBenjamin LaHaise 	}
876cb80ef46SBenjamin LaHaise 
8774068579eSTom Herbert 	if (!uh->check) {
8784068579eSTom Herbert 		udp6_csum_zero_error(skb);
8794068579eSTom Herbert 		goto csum_error;
8804068579eSTom Herbert 	}
8814068579eSTom Herbert 
882db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
883db8dac20SDavid S. Miller 		goto discard;
884db8dac20SDavid S. Miller 
885db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
8866a5dc9e5SEric Dumazet 		goto csum_error;
887db8dac20SDavid S. Miller 
88802c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
8893ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
890db8dac20SDavid S. Miller 
891db8dac20SDavid S. Miller 	kfree_skb(skb);
892db8dac20SDavid S. Miller 	return 0;
893db8dac20SDavid S. Miller 
894db8dac20SDavid S. Miller short_packet:
895ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
896db8dac20SDavid S. Miller 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
897ba7a46f1SJoe Perches 			    saddr, ntohs(uh->source),
898ba7a46f1SJoe Perches 			    ulen, skb->len,
899ba7a46f1SJoe Perches 			    daddr, ntohs(uh->dest));
9006a5dc9e5SEric Dumazet 	goto discard;
9016a5dc9e5SEric Dumazet csum_error:
90202c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
903db8dac20SDavid S. Miller discard:
90402c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
905db8dac20SDavid S. Miller 	kfree_skb(skb);
906db8dac20SDavid S. Miller 	return 0;
907db8dac20SDavid S. Miller }
908db8dac20SDavid S. Miller 
909db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
910db8dac20SDavid S. Miller {
911645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
912db8dac20SDavid S. Miller }
913db8dac20SDavid S. Miller 
914db8dac20SDavid S. Miller /*
915db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
916db8dac20SDavid S. Miller  */
917db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
918db8dac20SDavid S. Miller {
919db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
920db8dac20SDavid S. Miller 
92136d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
92236d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
92336d926b9SDenis V. Lunev 	else if (up->pending) {
924db8dac20SDavid S. Miller 		up->len = 0;
925db8dac20SDavid S. Miller 		up->pending = 0;
926db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
927db8dac20SDavid S. Miller 	}
928db8dac20SDavid S. Miller }
929db8dac20SDavid S. Miller 
930493c6be3SSridhar Samudrala /**
931493c6be3SSridhar Samudrala  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
932493c6be3SSridhar Samudrala  *	@sk:	socket we are sending on
933493c6be3SSridhar Samudrala  *	@skb:	sk_buff containing the filled-in UDP header
934493c6be3SSridhar Samudrala  *		(checksum field must be zeroed out)
935493c6be3SSridhar Samudrala  */
936493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
937493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
938493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
939493c6be3SSridhar Samudrala {
940493c6be3SSridhar Samudrala 	unsigned int offset;
941493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
942d39d938cSVlad Yasevich 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
943493c6be3SSridhar Samudrala 	__wsum csum = 0;
944493c6be3SSridhar Samudrala 
945d39d938cSVlad Yasevich 	if (!frags) {
946493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
947493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
948493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
949493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
950493c6be3SSridhar Samudrala 	} else {
951493c6be3SSridhar Samudrala 		/*
952493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
953493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
954493c6be3SSridhar Samudrala 		 * should be together
955493c6be3SSridhar Samudrala 		 */
956493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
957493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
958493c6be3SSridhar Samudrala 
959493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
960493c6be3SSridhar Samudrala 
961d39d938cSVlad Yasevich 		do {
962d39d938cSVlad Yasevich 			csum = csum_add(csum, frags->csum);
963d39d938cSVlad Yasevich 		} while ((frags = frags->next));
964493c6be3SSridhar Samudrala 
965493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
966493c6be3SSridhar Samudrala 					    csum);
967493c6be3SSridhar Samudrala 		if (uh->check == 0)
968493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
969493c6be3SSridhar Samudrala 	}
970493c6be3SSridhar Samudrala }
971493c6be3SSridhar Samudrala 
972db8dac20SDavid S. Miller /*
973db8dac20SDavid S. Miller  *	Sending
974db8dac20SDavid S. Miller  */
975db8dac20SDavid S. Miller 
976d39d938cSVlad Yasevich static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
977db8dac20SDavid S. Miller {
978d39d938cSVlad Yasevich 	struct sock *sk = skb->sk;
979db8dac20SDavid S. Miller 	struct udphdr *uh;
980db8dac20SDavid S. Miller 	int err = 0;
981db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
982db8dac20SDavid S. Miller 	__wsum csum = 0;
983d39d938cSVlad Yasevich 	int offset = skb_transport_offset(skb);
984d39d938cSVlad Yasevich 	int len = skb->len - offset;
985db8dac20SDavid S. Miller 
986db8dac20SDavid S. Miller 	/*
987db8dac20SDavid S. Miller 	 * Create a UDP header
988db8dac20SDavid S. Miller 	 */
989db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
9901958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
9911958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
992d39d938cSVlad Yasevich 	uh->len = htons(len);
993db8dac20SDavid S. Miller 	uh->check = 0;
994db8dac20SDavid S. Miller 
995db8dac20SDavid S. Miller 	if (is_udplite)
996d39d938cSVlad Yasevich 		csum = udplite_csum(skb);
997d39d938cSVlad Yasevich 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
9984068579eSTom Herbert 		skb->ip_summed = CHECKSUM_NONE;
9994068579eSTom Herbert 		goto send;
10004068579eSTom Herbert 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1001d39d938cSVlad Yasevich 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1002493c6be3SSridhar Samudrala 		goto send;
1003493c6be3SSridhar Samudrala 	} else
1004d39d938cSVlad Yasevich 		csum = udp_csum(skb);
1005db8dac20SDavid S. Miller 
1006db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
10074c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1008d39d938cSVlad Yasevich 				    len, fl6->flowi6_proto, csum);
1009db8dac20SDavid S. Miller 	if (uh->check == 0)
1010db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
1011db8dac20SDavid S. Miller 
1012493c6be3SSridhar Samudrala send:
1013d39d938cSVlad Yasevich 	err = ip6_send_skb(skb);
10146ce9e7b5SEric Dumazet 	if (err) {
10156ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
10166aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
10176ce9e7b5SEric Dumazet 				       UDP_MIB_SNDBUFERRORS, is_udplite);
10186ce9e7b5SEric Dumazet 			err = 0;
10196ce9e7b5SEric Dumazet 		}
10206aef70a8SEric Dumazet 	} else {
10216aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
10226ce9e7b5SEric Dumazet 			       UDP_MIB_OUTDATAGRAMS, is_udplite);
10236aef70a8SEric Dumazet 	}
1024d39d938cSVlad Yasevich 	return err;
1025d39d938cSVlad Yasevich }
1026d39d938cSVlad Yasevich 
1027d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
1028d39d938cSVlad Yasevich {
1029d39d938cSVlad Yasevich 	struct sk_buff *skb;
1030d39d938cSVlad Yasevich 	struct udp_sock  *up = udp_sk(sk);
1031d39d938cSVlad Yasevich 	struct flowi6 fl6;
1032d39d938cSVlad Yasevich 	int err = 0;
1033d39d938cSVlad Yasevich 
1034d39d938cSVlad Yasevich 	if (up->pending == AF_INET)
1035d39d938cSVlad Yasevich 		return udp_push_pending_frames(sk);
1036d39d938cSVlad Yasevich 
1037d39d938cSVlad Yasevich 	/* ip6_finish_skb will release the cork, so make a copy of
1038d39d938cSVlad Yasevich 	 * fl6 here.
1039d39d938cSVlad Yasevich 	 */
1040d39d938cSVlad Yasevich 	fl6 = inet_sk(sk)->cork.fl.u.ip6;
1041d39d938cSVlad Yasevich 
1042d39d938cSVlad Yasevich 	skb = ip6_finish_skb(sk);
1043d39d938cSVlad Yasevich 	if (!skb)
1044d39d938cSVlad Yasevich 		goto out;
1045d39d938cSVlad Yasevich 
1046d39d938cSVlad Yasevich 	err = udp_v6_send_skb(skb, &fl6);
1047d39d938cSVlad Yasevich 
1048db8dac20SDavid S. Miller out:
1049db8dac20SDavid S. Miller 	up->len = 0;
1050db8dac20SDavid S. Miller 	up->pending = 0;
1051db8dac20SDavid S. Miller 	return err;
1052db8dac20SDavid S. Miller }
1053db8dac20SDavid S. Miller 
10541b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1055db8dac20SDavid S. Miller {
1056db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1057db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1058db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1059db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1060342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
106120c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1062db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
106345f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
1064db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
10654c9483b2SDavid S. Miller 	struct flowi6 fl6;
1066db8dac20SDavid S. Miller 	struct dst_entry *dst;
1067db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
1068db8dac20SDavid S. Miller 	int ulen = len;
1069db8dac20SDavid S. Miller 	int hlimit = -1;
1070db8dac20SDavid S. Miller 	int tclass = -1;
107113b52cd4SBrian Haley 	int dontfrag = -1;
1072db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1073db8dac20SDavid S. Miller 	int err;
1074db8dac20SDavid S. Miller 	int connected = 0;
1075db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1076db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1077ad1e46a8SSoheil Hassas Yeganeh 	struct sockcm_cookie sockc;
1078db8dac20SDavid S. Miller 
1079db8dac20SDavid S. Miller 	/* destination address check */
1080db8dac20SDavid S. Miller 	if (sin6) {
1081db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1082db8dac20SDavid S. Miller 			return -EINVAL;
1083db8dac20SDavid S. Miller 
1084db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1085db8dac20SDavid S. Miller 		case AF_INET6:
1086db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1087db8dac20SDavid S. Miller 				return -EINVAL;
1088db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1089db8dac20SDavid S. Miller 			break;
1090db8dac20SDavid S. Miller 		case AF_INET:
1091db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1092db8dac20SDavid S. Miller 		case AF_UNSPEC:
1093db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1094db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1095db8dac20SDavid S. Miller 			daddr = NULL;
1096db8dac20SDavid S. Miller 			break;
1097db8dac20SDavid S. Miller 		default:
1098db8dac20SDavid S. Miller 			return -EINVAL;
1099db8dac20SDavid S. Miller 		}
1100db8dac20SDavid S. Miller 	} else if (!up->pending) {
1101db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1102db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1103efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
1104db8dac20SDavid S. Miller 	} else
1105db8dac20SDavid S. Miller 		daddr = NULL;
1106db8dac20SDavid S. Miller 
1107db8dac20SDavid S. Miller 	if (daddr) {
1108db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1109db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1110db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1111c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1112db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1113db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1114db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1115db8dac20SDavid S. Miller do_udp_sendmsg:
1116db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1117db8dac20SDavid S. Miller 				return -ENETUNREACH;
11181b784140SYing Xue 			return udp_sendmsg(sk, msg, len);
1119db8dac20SDavid S. Miller 		}
1120db8dac20SDavid S. Miller 	}
1121db8dac20SDavid S. Miller 
1122db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
11231b784140SYing Xue 		return udp_sendmsg(sk, msg, len);
1124db8dac20SDavid S. Miller 
1125db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1126db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1127db8dac20SDavid S. Miller 	   */
1128db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1129db8dac20SDavid S. Miller 		return -EMSGSIZE;
1130db8dac20SDavid S. Miller 
113103485f2aSVlad Yasevich 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1132db8dac20SDavid S. Miller 	if (up->pending) {
1133db8dac20SDavid S. Miller 		/*
1134db8dac20SDavid S. Miller 		 * There are pending frames.
1135db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1136db8dac20SDavid S. Miller 		 */
1137db8dac20SDavid S. Miller 		lock_sock(sk);
1138db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1139db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1140db8dac20SDavid S. Miller 				release_sock(sk);
1141db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1142db8dac20SDavid S. Miller 			}
1143db8dac20SDavid S. Miller 			dst = NULL;
1144db8dac20SDavid S. Miller 			goto do_append_data;
1145db8dac20SDavid S. Miller 		}
1146db8dac20SDavid S. Miller 		release_sock(sk);
1147db8dac20SDavid S. Miller 	}
1148db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1149db8dac20SDavid S. Miller 
11504c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1151db8dac20SDavid S. Miller 
1152db8dac20SDavid S. Miller 	if (sin6) {
1153db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1154db8dac20SDavid S. Miller 			return -EINVAL;
1155db8dac20SDavid S. Miller 
11561958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1157db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1158db8dac20SDavid S. Miller 
1159db8dac20SDavid S. Miller 		if (np->sndflow) {
11604c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
11614c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
11624c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
116363159f29SIan Morris 				if (!flowlabel)
1164db8dac20SDavid S. Miller 					return -EINVAL;
1165db8dac20SDavid S. Miller 			}
1166db8dac20SDavid S. Miller 		}
1167db8dac20SDavid S. Miller 
1168db8dac20SDavid S. Miller 		/*
1169db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1170db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1171db8dac20SDavid S. Miller 		 */
1172db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1173efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1174efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
1175db8dac20SDavid S. Miller 
1176db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1177db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1178842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
11794c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1180db8dac20SDavid S. Miller 	} else {
1181db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1182db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1183db8dac20SDavid S. Miller 
11841958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1185efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
11864c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
1187db8dac20SDavid S. Miller 		connected = 1;
1188db8dac20SDavid S. Miller 	}
1189db8dac20SDavid S. Miller 
11904c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
11914c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1192db8dac20SDavid S. Miller 
11934c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
11944c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
11959f690db7SYang Hongyang 
11964c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
1197c14ac945SSoheil Hassas Yeganeh 	sockc.tsflags = sk->sk_tsflags;
119851953d5bSBrian Haley 
1199db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1200db8dac20SDavid S. Miller 		opt = &opt_space;
1201db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1202db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
1203db8dac20SDavid S. Miller 
120473df66f8STom Parkin 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1205ad1e46a8SSoheil Hassas Yeganeh 					    &hlimit, &tclass, &dontfrag,
1206ad1e46a8SSoheil Hassas Yeganeh 					    &sockc);
1207db8dac20SDavid S. Miller 		if (err < 0) {
1208db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1209db8dac20SDavid S. Miller 			return err;
1210db8dac20SDavid S. Miller 		}
12114c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
12124c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
121363159f29SIan Morris 			if (!flowlabel)
1214db8dac20SDavid S. Miller 				return -EINVAL;
1215db8dac20SDavid S. Miller 		}
1216db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1217db8dac20SDavid S. Miller 			opt = NULL;
1218db8dac20SDavid S. Miller 		connected = 0;
1219db8dac20SDavid S. Miller 	}
122045f6fad8SEric Dumazet 	if (!opt) {
122145f6fad8SEric Dumazet 		opt = txopt_get(np);
122245f6fad8SEric Dumazet 		opt_to_free = opt;
122345f6fad8SEric Dumazet 	}
1224db8dac20SDavid S. Miller 	if (flowlabel)
1225db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1226db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
1227db8dac20SDavid S. Miller 
12284c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
1229876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
12304e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
1231876c7f41SBrian Haley 	else
12324c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
12334c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
12344e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
12351958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1236db8dac20SDavid S. Miller 
12374c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
123820c59de2SArnaud Ebalard 	if (final_p)
1239db8dac20SDavid S. Miller 		connected = 0;
1240db8dac20SDavid S. Miller 
12414c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
12424c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
1243db8dac20SDavid S. Miller 		connected = 0;
1244c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1245c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1246db8dac20SDavid S. Miller 
12474c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1248db8dac20SDavid S. Miller 
12490e0d44abSSteffen Klassert 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
125068d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
125168d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
125268d0c6d3SDavid S. Miller 		dst = NULL;
1253db8dac20SDavid S. Miller 		goto out;
1254db8dac20SDavid S. Miller 	}
1255db8dac20SDavid S. Miller 
1256db8dac20SDavid S. Miller 	if (hlimit < 0)
12575c98631cSLorenzo Colitti 		hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1258db8dac20SDavid S. Miller 
1259db8dac20SDavid S. Miller 	if (tclass < 0)
1260e651f03aSGerrit Renker 		tclass = np->tclass;
1261db8dac20SDavid S. Miller 
1262db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1263db8dac20SDavid S. Miller 		goto do_confirm;
1264db8dac20SDavid S. Miller back_from_confirm:
1265db8dac20SDavid S. Miller 
126603485f2aSVlad Yasevich 	/* Lockless fast path for the non-corking case */
126703485f2aSVlad Yasevich 	if (!corkreq) {
126803485f2aSVlad Yasevich 		struct sk_buff *skb;
126903485f2aSVlad Yasevich 
127003485f2aSVlad Yasevich 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
127103485f2aSVlad Yasevich 				   sizeof(struct udphdr), hlimit, tclass, opt,
127203485f2aSVlad Yasevich 				   &fl6, (struct rt6_info *)dst,
1273c14ac945SSoheil Hassas Yeganeh 				   msg->msg_flags, dontfrag, &sockc);
127403485f2aSVlad Yasevich 		err = PTR_ERR(skb);
127503485f2aSVlad Yasevich 		if (!IS_ERR_OR_NULL(skb))
127603485f2aSVlad Yasevich 			err = udp_v6_send_skb(skb, &fl6);
127703485f2aSVlad Yasevich 		goto release_dst;
127803485f2aSVlad Yasevich 	}
127903485f2aSVlad Yasevich 
1280db8dac20SDavid S. Miller 	lock_sock(sk);
1281db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1282db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1283db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1284db8dac20SDavid S. Miller 		release_sock(sk);
1285db8dac20SDavid S. Miller 
1286ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 2\n");
1287db8dac20SDavid S. Miller 		err = -EINVAL;
1288db8dac20SDavid S. Miller 		goto out;
1289db8dac20SDavid S. Miller 	}
1290db8dac20SDavid S. Miller 
1291db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1292db8dac20SDavid S. Miller 
1293db8dac20SDavid S. Miller do_append_data:
1294e36d3ff9SJiri Pirko 	if (dontfrag < 0)
1295e36d3ff9SJiri Pirko 		dontfrag = np->dontfrag;
1296db8dac20SDavid S. Miller 	up->len += ulen;
1297f69e6d13SAl Viro 	err = ip6_append_data(sk, getfrag, msg, ulen,
12984c9483b2SDavid S. Miller 		sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1299db8dac20SDavid S. Miller 		(struct rt6_info *)dst,
1300c14ac945SSoheil Hassas Yeganeh 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag,
1301c14ac945SSoheil Hassas Yeganeh 		&sockc);
1302db8dac20SDavid S. Miller 	if (err)
1303db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1304db8dac20SDavid S. Miller 	else if (!corkreq)
1305db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1306db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1307db8dac20SDavid S. Miller 		up->pending = 0;
1308db8dac20SDavid S. Miller 
130903485f2aSVlad Yasevich 	if (err > 0)
131003485f2aSVlad Yasevich 		err = np->recverr ? net_xmit_errno(err) : 0;
131103485f2aSVlad Yasevich 	release_sock(sk);
131203485f2aSVlad Yasevich 
131303485f2aSVlad Yasevich release_dst:
1314db8dac20SDavid S. Miller 	if (dst) {
1315db8dac20SDavid S. Miller 		if (connected) {
1316db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
1317efe4208fSEric Dumazet 				      ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1318efe4208fSEric Dumazet 				      &sk->sk_v6_daddr : NULL,
1319db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
13204c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1321db8dac20SDavid S. Miller 				      &np->saddr :
1322db8dac20SDavid S. Miller #endif
1323db8dac20SDavid S. Miller 				      NULL);
1324db8dac20SDavid S. Miller 		} else {
1325db8dac20SDavid S. Miller 			dst_release(dst);
1326db8dac20SDavid S. Miller 		}
1327a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1328db8dac20SDavid S. Miller 	}
1329db8dac20SDavid S. Miller 
1330db8dac20SDavid S. Miller out:
1331a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1332db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
133345f6fad8SEric Dumazet 	txopt_put(opt_to_free);
1334db8dac20SDavid S. Miller 	if (!err)
1335db8dac20SDavid S. Miller 		return len;
1336db8dac20SDavid S. Miller 	/*
1337db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1338db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1339db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1340db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1341db8dac20SDavid S. Miller 	 * seems like overkill.
1342db8dac20SDavid S. Miller 	 */
1343db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
13446aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
1345235b9f7aSPavel Emelyanov 			       UDP_MIB_SNDBUFERRORS, is_udplite);
1346db8dac20SDavid S. Miller 	}
1347db8dac20SDavid S. Miller 	return err;
1348db8dac20SDavid S. Miller 
1349db8dac20SDavid S. Miller do_confirm:
1350db8dac20SDavid S. Miller 	dst_confirm(dst);
1351db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1352db8dac20SDavid S. Miller 		goto back_from_confirm;
1353db8dac20SDavid S. Miller 	err = 0;
1354db8dac20SDavid S. Miller 	goto out;
1355db8dac20SDavid S. Miller }
1356db8dac20SDavid S. Miller 
13577d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1358db8dac20SDavid S. Miller {
135944046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1360db8dac20SDavid S. Miller 	lock_sock(sk);
1361db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1362db8dac20SDavid S. Miller 	release_sock(sk);
1363db8dac20SDavid S. Miller 
136444046a59STom Parkin 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
136544046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
136644046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
136744046a59STom Parkin 		if (encap_destroy)
136844046a59STom Parkin 			encap_destroy(sk);
136944046a59STom Parkin 	}
137044046a59STom Parkin 
1371db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1372db8dac20SDavid S. Miller }
1373db8dac20SDavid S. Miller 
1374db8dac20SDavid S. Miller /*
1375db8dac20SDavid S. Miller  *	Socket option code for UDP
1376db8dac20SDavid S. Miller  */
1377db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1378b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1379db8dac20SDavid S. Miller {
1380db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1381db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1382db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1383db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1384db8dac20SDavid S. Miller }
1385db8dac20SDavid S. Miller 
1386db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1387db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1388b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1389db8dac20SDavid S. Miller {
1390db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1391db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1392db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1393db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1394db8dac20SDavid S. Miller }
1395db8dac20SDavid S. Miller #endif
1396db8dac20SDavid S. Miller 
1397db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1398db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1399db8dac20SDavid S. Miller {
1400db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1401db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1402db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1403db8dac20SDavid S. Miller }
1404db8dac20SDavid S. Miller 
1405db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1406db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1407db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1408db8dac20SDavid S. Miller {
1409db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1410db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1411db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1412db8dac20SDavid S. Miller }
1413db8dac20SDavid S. Miller #endif
1414db8dac20SDavid S. Miller 
141541135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1416db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1417db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1418db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1419db8dac20SDavid S. Miller };
1420db8dac20SDavid S. Miller 
1421db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1422db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1423db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1424db8dac20SDavid S. Miller {
142517ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
142617ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
142717ef66afSLorenzo Colitti 	} else {
142817ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
142917ef66afSLorenzo Colitti 		struct inet_sock *inet = inet_sk(v);
143017ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
143117ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
143217ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
143317ef66afSLorenzo Colitti 	}
1434db8dac20SDavid S. Miller 	return 0;
1435db8dac20SDavid S. Miller }
1436db8dac20SDavid S. Miller 
143773cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = {
143873cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
143973cb88ecSArjan van de Ven 	.open     = udp_seq_open,
144073cb88ecSArjan van de Ven 	.read     = seq_read,
144173cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
144273cb88ecSArjan van de Ven 	.release  = seq_release_net
144373cb88ecSArjan van de Ven };
144473cb88ecSArjan van de Ven 
1445db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1446db8dac20SDavid S. Miller 	.name		= "udp6",
1447db8dac20SDavid S. Miller 	.family		= AF_INET6,
1448645ca708SEric Dumazet 	.udp_table	= &udp_table,
144973cb88ecSArjan van de Ven 	.seq_fops	= &udp6_afinfo_seq_fops,
1450dda61925SDenis V. Lunev 	.seq_ops	= {
1451dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1452dda61925SDenis V. Lunev 	},
1453db8dac20SDavid S. Miller };
1454db8dac20SDavid S. Miller 
14552c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1456db8dac20SDavid S. Miller {
14570c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1458db8dac20SDavid S. Miller }
1459db8dac20SDavid S. Miller 
1460ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1461ec120da6SIan Morris {
14620c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1463db8dac20SDavid S. Miller }
1464db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1465db8dac20SDavid S. Miller 
1466f77d6021SEric Dumazet void udp_v6_clear_sk(struct sock *sk, int size)
1467f77d6021SEric Dumazet {
1468f77d6021SEric Dumazet 	struct inet_sock *inet = inet_sk(sk);
1469f77d6021SEric Dumazet 
1470f77d6021SEric Dumazet 	/* we do not want to clear pinet6 field, because of RCU lookups */
1471f77d6021SEric Dumazet 	sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1472f77d6021SEric Dumazet 
1473f77d6021SEric Dumazet 	size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1474f77d6021SEric Dumazet 	memset(&inet->pinet6 + 1, 0, size);
1475f77d6021SEric Dumazet }
1476f77d6021SEric Dumazet 
1477db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1478db8dac20SDavid S. Miller 
1479db8dac20SDavid S. Miller struct proto udpv6_prot = {
1480db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1481db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1482db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1483db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1484db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1485db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1486db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1487db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1488db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1489db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1490db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1491f7ad74feSBenjamin LaHaise 	.backlog_rcv	   = __udpv6_queue_rcv_skb,
1492e646b657SMartin KaFai Lau 	.release_cb	   = ip6_datagram_release_cb,
1493db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1494db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1495719f8358SEric Dumazet 	.rehash		   = udp_v6_rehash,
1496db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1497db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1498db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1499db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1500db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1501db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1502271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1503645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1504db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1505db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1506db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1507db8dac20SDavid S. Miller #endif
1508f77d6021SEric Dumazet 	.clear_sk	   = udp_v6_clear_sk,
1509db8dac20SDavid S. Miller };
1510db8dac20SDavid S. Miller 
1511db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1512db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1513db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1514db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1515db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1516db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1517db8dac20SDavid S. Miller };
1518db8dac20SDavid S. Miller 
1519db8dac20SDavid S. Miller int __init udpv6_init(void)
1520db8dac20SDavid S. Miller {
1521db8dac20SDavid S. Miller 	int ret;
1522db8dac20SDavid S. Miller 
15233336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
15243336288aSVlad Yasevich 	if (ret)
1525c6b641a4SVlad Yasevich 		goto out;
15263336288aSVlad Yasevich 
1527db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1528db8dac20SDavid S. Miller 	if (ret)
1529db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1530db8dac20SDavid S. Miller out:
1531db8dac20SDavid S. Miller 	return ret;
1532db8dac20SDavid S. Miller 
1533db8dac20SDavid S. Miller out_udpv6_protocol:
1534db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1535db8dac20SDavid S. Miller 	goto out;
1536db8dac20SDavid S. Miller }
1537db8dac20SDavid S. Miller 
1538db8dac20SDavid S. Miller void udpv6_exit(void)
1539db8dac20SDavid S. Miller {
1540db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1541db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1542db8dac20SDavid S. Miller }
1543