xref: /openbmc/linux/net/ipv6/udp.c (revision 5d77dca8)
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 
118d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net,
11988440ae7SBalazs Scheidler 			 const struct in6_addr *saddr, __be16 sport,
120d1e37288SSu, Xuemin 			 const struct in6_addr *daddr, unsigned short hnum,
121645ca708SEric Dumazet 			 int dif)
122db8dac20SDavid S. Miller {
12360c04aecSJoe Perches 	int score;
12460c04aecSJoe Perches 	struct inet_sock *inet;
125db8dac20SDavid S. Miller 
12660c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
12760c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
12860c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
12960c04aecSJoe Perches 		return -1;
130645ca708SEric Dumazet 
131645ca708SEric Dumazet 	score = 0;
13260c04aecSJoe Perches 	inet = inet_sk(sk);
13360c04aecSJoe Perches 
134c720c7e8SEric Dumazet 	if (inet->inet_dport) {
135c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
136645ca708SEric Dumazet 			return -1;
137db8dac20SDavid S. Miller 		score++;
138db8dac20SDavid S. Miller 	}
13960c04aecSJoe Perches 
140efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
141efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
142645ca708SEric Dumazet 			return -1;
143db8dac20SDavid S. Miller 		score++;
144db8dac20SDavid S. Miller 	}
14560c04aecSJoe Perches 
146efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
147efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
148645ca708SEric Dumazet 			return -1;
149db8dac20SDavid S. Miller 		score++;
150db8dac20SDavid S. Miller 	}
15160c04aecSJoe Perches 
152db8dac20SDavid S. Miller 	if (sk->sk_bound_dev_if) {
153db8dac20SDavid S. Miller 		if (sk->sk_bound_dev_if != dif)
154645ca708SEric Dumazet 			return -1;
155db8dac20SDavid S. Miller 		score++;
156db8dac20SDavid S. Miller 	}
15760c04aecSJoe Perches 
15870da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
15970da268bSEric Dumazet 		score++;
16070da268bSEric Dumazet 
161645ca708SEric Dumazet 	return score;
162645ca708SEric Dumazet }
163645ca708SEric Dumazet 
164d1e37288SSu, Xuemin /* called with rcu_read_lock() */
165fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
166fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
167fddc17deSEric Dumazet 		const struct in6_addr *daddr, unsigned int hnum, int dif,
168d1e37288SSu, Xuemin 		struct udp_hslot *hslot2,
1691134158bSCraig Gallek 		struct sk_buff *skb)
170fddc17deSEric Dumazet {
171fddc17deSEric Dumazet 	struct sock *sk, *result;
17272289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
17372289b96STom Herbert 	u32 hash = 0;
174fddc17deSEric Dumazet 
175fddc17deSEric Dumazet 	result = NULL;
176fddc17deSEric Dumazet 	badness = -1;
177ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
178d1e37288SSu, Xuemin 		score = compute_score(sk, net, saddr, sport,
179fddc17deSEric Dumazet 				      daddr, hnum, dif);
180fddc17deSEric Dumazet 		if (score > badness) {
18172289b96STom Herbert 			reuseport = sk->sk_reuseport;
18272289b96STom Herbert 			if (reuseport) {
183b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
18472289b96STom Herbert 						    saddr, sport);
185ed0dfffdSEric Dumazet 
186ca065d0cSEric Dumazet 				result = reuseport_select_sock(sk, hash, skb,
1871134158bSCraig Gallek 							sizeof(struct udphdr));
188ca065d0cSEric Dumazet 				if (result)
189ca065d0cSEric Dumazet 					return result;
19072289b96STom Herbert 				matches = 1;
19170da268bSEric Dumazet 			}
192ca065d0cSEric Dumazet 			result = sk;
193ca065d0cSEric Dumazet 			badness = score;
19472289b96STom Herbert 		} else if (score == badness && reuseport) {
19572289b96STom Herbert 			matches++;
1968fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
19772289b96STom Herbert 				result = sk;
19872289b96STom Herbert 			hash = next_pseudo_random32(hash);
199fddc17deSEric Dumazet 		}
200fddc17deSEric Dumazet 	}
201fddc17deSEric Dumazet 	return result;
202fddc17deSEric Dumazet }
203fddc17deSEric Dumazet 
204ca065d0cSEric Dumazet /* rcu_read_lock() must be held */
205fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
20688440ae7SBalazs Scheidler 				      const struct in6_addr *saddr, __be16 sport,
20788440ae7SBalazs Scheidler 				      const struct in6_addr *daddr, __be16 dport,
208538950a1SCraig Gallek 				      int dif, struct udp_table *udptable,
209538950a1SCraig Gallek 				      struct sk_buff *skb)
210645ca708SEric Dumazet {
211271b72c7SEric Dumazet 	struct sock *sk, *result;
212645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
213fddc17deSEric Dumazet 	unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
214fddc17deSEric Dumazet 	struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
21572289b96STom Herbert 	int score, badness, matches = 0, reuseport = 0;
21672289b96STom Herbert 	u32 hash = 0;
217645ca708SEric Dumazet 
218fddc17deSEric Dumazet 	if (hslot->count > 10) {
219fddc17deSEric Dumazet 		hash2 = udp6_portaddr_hash(net, daddr, hnum);
220fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
221fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
222fddc17deSEric Dumazet 		if (hslot->count < hslot2->count)
223fddc17deSEric Dumazet 			goto begin;
224fddc17deSEric Dumazet 
225fddc17deSEric Dumazet 		result = udp6_lib_lookup2(net, saddr, sport,
226fddc17deSEric Dumazet 					  daddr, hnum, dif,
227d1e37288SSu, Xuemin 					  hslot2, skb);
228fddc17deSEric Dumazet 		if (!result) {
229d1e37288SSu, Xuemin 			unsigned int old_slot2 = slot2;
230fddc17deSEric Dumazet 			hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
231fddc17deSEric Dumazet 			slot2 = hash2 & udptable->mask;
232d1e37288SSu, Xuemin 			/* avoid searching the same slot again. */
233d1e37288SSu, Xuemin 			if (unlikely(slot2 == old_slot2))
234d1e37288SSu, Xuemin 				return result;
235d1e37288SSu, Xuemin 
236fddc17deSEric Dumazet 			hslot2 = &udptable->hash2[slot2];
237fddc17deSEric Dumazet 			if (hslot->count < hslot2->count)
238fddc17deSEric Dumazet 				goto begin;
239fddc17deSEric Dumazet 
2401223c67cSJorge Boncompte [DTI2] 			result = udp6_lib_lookup2(net, saddr, sport,
241d1e37288SSu, Xuemin 						  daddr, hnum, dif,
242d1e37288SSu, Xuemin 						  hslot2, skb);
243fddc17deSEric Dumazet 		}
244fddc17deSEric Dumazet 		return result;
245fddc17deSEric Dumazet 	}
246271b72c7SEric Dumazet begin:
247271b72c7SEric Dumazet 	result = NULL;
248271b72c7SEric Dumazet 	badness = -1;
249ca065d0cSEric Dumazet 	sk_for_each_rcu(sk, &hslot->head) {
250d1e37288SSu, Xuemin 		score = compute_score(sk, net, saddr, sport, daddr, hnum, dif);
251645ca708SEric Dumazet 		if (score > badness) {
25272289b96STom Herbert 			reuseport = sk->sk_reuseport;
25372289b96STom Herbert 			if (reuseport) {
254b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
25572289b96STom Herbert 						    saddr, sport);
256ca065d0cSEric Dumazet 				result = reuseport_select_sock(sk, hash, skb,
257538950a1SCraig Gallek 							sizeof(struct udphdr));
258ca065d0cSEric Dumazet 				if (result)
259ca065d0cSEric Dumazet 					return result;
26072289b96STom Herbert 				matches = 1;
26172289b96STom Herbert 			}
262ca065d0cSEric Dumazet 			result = sk;
263ca065d0cSEric Dumazet 			badness = score;
26472289b96STom Herbert 		} else if (score == badness && reuseport) {
26572289b96STom Herbert 			matches++;
2668fc54f68SDaniel Borkmann 			if (reciprocal_scale(hash, matches) == 0)
26772289b96STom Herbert 				result = sk;
26872289b96STom Herbert 			hash = next_pseudo_random32(hash);
269db8dac20SDavid S. Miller 		}
270db8dac20SDavid S. Miller 	}
271db8dac20SDavid S. Miller 	return result;
272db8dac20SDavid S. Miller }
273fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
274db8dac20SDavid S. Miller 
275607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
276607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
277645ca708SEric Dumazet 					  struct udp_table *udptable)
278607c4aafSKOVACS Krisztian {
279b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
280ed7cbbceSAlexander Duyck 	struct sock *sk;
281607c4aafSKOVACS Krisztian 
282e5d08d71SIan Morris 	sk = skb_steal_sock(skb);
283e5d08d71SIan Morris 	if (unlikely(sk))
28423542618SKOVACS Krisztian 		return sk;
285ed7cbbceSAlexander Duyck 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
286607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
287538950a1SCraig Gallek 				 udptable, skb);
288607c4aafSKOVACS Krisztian }
289607c4aafSKOVACS Krisztian 
29063058308STom Herbert struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
29163058308STom Herbert 				 __be16 sport, __be16 dport)
29263058308STom Herbert {
29363058308STom Herbert 	const struct ipv6hdr *iph = ipv6_hdr(skb);
29463058308STom Herbert 
295ed7cbbceSAlexander Duyck 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
29663058308STom Herbert 				 &iph->daddr, dport, inet6_iif(skb),
29763058308STom Herbert 				 &udp_table, skb);
29863058308STom Herbert }
29963058308STom Herbert EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
30063058308STom Herbert 
301ca065d0cSEric Dumazet /* Must be called under rcu_read_lock().
302ca065d0cSEric Dumazet  * Does increment socket refcount.
303ca065d0cSEric Dumazet  */
304ca065d0cSEric Dumazet #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \
305ca065d0cSEric Dumazet     IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY)
306aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
307aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
308aa976fc0SBalazs Scheidler {
309ca065d0cSEric Dumazet 	struct sock *sk;
310ca065d0cSEric Dumazet 
311ca065d0cSEric Dumazet 	sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
312ca065d0cSEric Dumazet 				dif, &udp_table, NULL);
313ca065d0cSEric Dumazet 	if (sk && !atomic_inc_not_zero(&sk->sk_refcnt))
314ca065d0cSEric Dumazet 		sk = NULL;
315ca065d0cSEric Dumazet 	return sk;
316aa976fc0SBalazs Scheidler }
317aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
318ca065d0cSEric Dumazet #endif
319aa976fc0SBalazs Scheidler 
320db8dac20SDavid S. Miller /*
321db8dac20SDavid S. Miller  *	This should be easy, if there is something there we
322db8dac20SDavid S. Miller  *	return it, otherwise we block.
323db8dac20SDavid S. Miller  */
324db8dac20SDavid S. Miller 
3251b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
326db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
327db8dac20SDavid S. Miller {
328db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
329db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
330db8dac20SDavid S. Miller 	struct sk_buff *skb;
33159c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
332627d2d6bSsamanthakumar 	int peeked, peeking, off;
333db8dac20SDavid S. Miller 	int err;
334db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
335197c949eSEric Dumazet 	bool checksum_valid = false;
336f26ba175SWei Yongjun 	int is_udp4;
3378a74ad60SEric Dumazet 	bool slow;
338db8dac20SDavid S. Miller 
339db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
34085fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
341db8dac20SDavid S. Miller 
3424b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
34385fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
3444b340ae2SBrian Haley 
345db8dac20SDavid S. Miller try_again:
346627d2d6bSsamanthakumar 	peeking = off = sk_peek_offset(sk, flags);
347db8dac20SDavid S. Miller 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
3483f518bf7SPavel Emelyanov 				  &peeked, &off, &err);
349db8dac20SDavid S. Miller 	if (!skb)
350627d2d6bSsamanthakumar 		return err;
351db8dac20SDavid S. Miller 
352e6afc8acSsamanthakumar 	ulen = skb->len;
35359c2cdaeSDavid S. Miller 	copied = len;
354627d2d6bSsamanthakumar 	if (copied > ulen - off)
355627d2d6bSsamanthakumar 		copied = ulen - off;
35659c2cdaeSDavid S. Miller 	else if (copied < ulen)
357db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
358db8dac20SDavid S. Miller 
359f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
360f26ba175SWei Yongjun 
361db8dac20SDavid S. Miller 	/*
362db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
363db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
364db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
365db8dac20SDavid S. Miller 	 */
366db8dac20SDavid S. Miller 
367627d2d6bSsamanthakumar 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov || peeking) {
368197c949eSEric Dumazet 		checksum_valid = !udp_lib_checksum_complete(skb);
369197c949eSEric Dumazet 		if (!checksum_valid)
370db8dac20SDavid S. Miller 			goto csum_copy_err;
371db8dac20SDavid S. Miller 	}
372db8dac20SDavid S. Miller 
373197c949eSEric Dumazet 	if (checksum_valid || skb_csum_unnecessary(skb))
374627d2d6bSsamanthakumar 		err = skb_copy_datagram_msg(skb, off, msg, copied);
375db8dac20SDavid S. Miller 	else {
376627d2d6bSsamanthakumar 		err = skb_copy_and_csum_datagram_msg(skb, off, msg);
377db8dac20SDavid S. Miller 		if (err == -EINVAL)
378db8dac20SDavid S. Miller 			goto csum_copy_err;
379db8dac20SDavid S. Miller 	}
38022911fc5SEric Dumazet 	if (unlikely(err)) {
38122911fc5SEric Dumazet 		trace_kfree_skb(skb, udpv6_recvmsg);
382979402b1SEric Dumazet 		if (!peeked) {
383979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
384979402b1SEric Dumazet 			if (is_udp4)
3856aef70a8SEric Dumazet 				UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
386979402b1SEric Dumazet 					      is_udplite);
387979402b1SEric Dumazet 			else
3886aef70a8SEric Dumazet 				UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
389979402b1SEric Dumazet 					       is_udplite);
390979402b1SEric Dumazet 		}
391627d2d6bSsamanthakumar 		skb_free_datagram_locked(sk, skb);
392627d2d6bSsamanthakumar 		return err;
39322911fc5SEric Dumazet 	}
394f26ba175SWei Yongjun 	if (!peeked) {
395f26ba175SWei Yongjun 		if (is_udp4)
3966aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
3976aef70a8SEric Dumazet 				      is_udplite);
398f26ba175SWei Yongjun 		else
3996aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
4006aef70a8SEric Dumazet 				       is_udplite);
401f26ba175SWei Yongjun 	}
402db8dac20SDavid S. Miller 
4033b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
404db8dac20SDavid S. Miller 
405db8dac20SDavid S. Miller 	/* Copy the address. */
406db8dac20SDavid S. Miller 	if (msg->msg_name) {
407342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
408db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
409db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
410db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
411db8dac20SDavid S. Miller 
412842df073SHannes Frederic Sowa 		if (is_udp4) {
413b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
414b301e82cSBrian Haley 					       &sin6->sin6_addr);
415842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
416842df073SHannes Frederic Sowa 		} else {
4174e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
418842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
419842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
4204330487aSDuan Jiong 						    inet6_iif(skb));
421db8dac20SDavid S. Miller 		}
422bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
423db8dac20SDavid S. Miller 	}
4244b261c75SHannes Frederic Sowa 
4254b261c75SHannes Frederic Sowa 	if (np->rxopt.all)
4264b261c75SHannes Frederic Sowa 		ip6_datagram_recv_common_ctl(sk, msg, skb);
4274b261c75SHannes Frederic Sowa 
428f26ba175SWei Yongjun 	if (is_udp4) {
429db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
430db8dac20SDavid S. Miller 			ip_cmsg_recv(msg, skb);
431db8dac20SDavid S. Miller 	} else {
432db8dac20SDavid S. Miller 		if (np->rxopt.all)
4334b261c75SHannes Frederic Sowa 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
434db8dac20SDavid S. Miller 	}
435db8dac20SDavid S. Miller 
43659c2cdaeSDavid S. Miller 	err = copied;
437db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
438db8dac20SDavid S. Miller 		err = ulen;
439db8dac20SDavid S. Miller 
440627d2d6bSsamanthakumar 	__skb_free_datagram_locked(sk, skb, peeking ? -err : err);
441db8dac20SDavid S. Miller 	return err;
442db8dac20SDavid S. Miller 
443db8dac20SDavid S. Miller csum_copy_err:
4448a74ad60SEric Dumazet 	slow = lock_sock_fast(sk);
4450856f939SWei Yongjun 	if (!skb_kill_datagram(sk, skb, flags)) {
4466a5dc9e5SEric Dumazet 		if (is_udp4) {
4476aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk),
4486a5dc9e5SEric Dumazet 				      UDP_MIB_CSUMERRORS, is_udplite);
4496aef70a8SEric Dumazet 			UDP_INC_STATS(sock_net(sk),
4500856f939SWei Yongjun 				      UDP_MIB_INERRORS, is_udplite);
4516a5dc9e5SEric Dumazet 		} else {
4526aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
4536a5dc9e5SEric Dumazet 				       UDP_MIB_CSUMERRORS, is_udplite);
4546aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
4550856f939SWei Yongjun 				       UDP_MIB_INERRORS, is_udplite);
4560856f939SWei Yongjun 		}
4576a5dc9e5SEric Dumazet 	}
4588a74ad60SEric Dumazet 	unlock_sock_fast(sk, slow);
459db8dac20SDavid S. Miller 
460beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
461beb39db5SEric Dumazet 	cond_resched();
4629cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
463db8dac20SDavid S. Miller 	goto try_again;
464db8dac20SDavid S. Miller }
465db8dac20SDavid S. Miller 
466db8dac20SDavid S. Miller void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
467d5fdd6baSBrian Haley 		    u8 type, u8 code, int offset, __be32 info,
468645ca708SEric Dumazet 		    struct udp_table *udptable)
469db8dac20SDavid S. Miller {
470db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
471b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
472b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
473b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
474db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
475db8dac20SDavid S. Miller 	struct sock *sk;
476e0d8c1b7SWei Wang 	int harderr;
477db8dac20SDavid S. Miller 	int err;
4787304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
479db8dac20SDavid S. Miller 
480e32ea7e7SCraig Gallek 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
481538950a1SCraig Gallek 			       inet6_iif(skb), udptable, skb);
48263159f29SIan Morris 	if (!sk) {
483a16292a0SEric Dumazet 		__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
4847304fe46SDuan Jiong 				  ICMP6_MIB_INERRORS);
485db8dac20SDavid S. Miller 		return;
4867304fe46SDuan Jiong 	}
487db8dac20SDavid S. Miller 
488e0d8c1b7SWei Wang 	harderr = icmpv6_err_convert(type, code, &err);
489e0d8c1b7SWei Wang 	np = inet6_sk(sk);
490e0d8c1b7SWei Wang 
49193b36cf3SHannes Frederic Sowa 	if (type == ICMPV6_PKT_TOOBIG) {
49293b36cf3SHannes Frederic Sowa 		if (!ip6_sk_accept_pmtu(sk))
49393b36cf3SHannes Frederic Sowa 			goto out;
49481aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
495e0d8c1b7SWei Wang 		if (np->pmtudisc != IPV6_PMTUDISC_DONT)
496e0d8c1b7SWei Wang 			harderr = 1;
49793b36cf3SHannes Frederic Sowa 	}
4981a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
499ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
5001a462d18SDuan Jiong 		goto out;
5011a462d18SDuan Jiong 	}
50281aded24SDavid S. Miller 
503e0d8c1b7SWei Wang 	if (!np->recverr) {
504e0d8c1b7SWei Wang 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
505db8dac20SDavid S. Miller 			goto out;
506e0d8c1b7SWei Wang 	} else {
507db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
508e0d8c1b7SWei Wang 	}
509b1faf566SEric Dumazet 
510db8dac20SDavid S. Miller 	sk->sk_err = err;
511db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
512db8dac20SDavid S. Miller out:
513ca065d0cSEric Dumazet 	return;
514db8dac20SDavid S. Miller }
515db8dac20SDavid S. Miller 
516f7ad74feSBenjamin LaHaise static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
517f7ad74feSBenjamin LaHaise {
518f7ad74feSBenjamin LaHaise 	int rc;
519f7ad74feSBenjamin LaHaise 
520efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
521f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
522005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
5232c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
524005ec974SShawn Bohrer 	}
525f7ad74feSBenjamin LaHaise 
526e6afc8acSsamanthakumar 	rc = __sock_queue_rcv_skb(sk, skb);
527f7ad74feSBenjamin LaHaise 	if (rc < 0) {
528f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
529f7ad74feSBenjamin LaHaise 
530f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
531f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
532e61da9e2SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
533f7ad74feSBenjamin LaHaise 					 UDP_MIB_RCVBUFERRORS, is_udplite);
534e61da9e2SEric Dumazet 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
535f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
536f7ad74feSBenjamin LaHaise 		return -1;
537f7ad74feSBenjamin LaHaise 	}
538f7ad74feSBenjamin LaHaise 	return 0;
539f7ad74feSBenjamin LaHaise }
540f7ad74feSBenjamin LaHaise 
541db8dac20SDavid S. Miller static __inline__ void udpv6_err(struct sk_buff *skb,
542d5fdd6baSBrian Haley 				 struct inet6_skb_parm *opt, u8 type,
543d5fdd6baSBrian Haley 				 u8 code, int offset, __be32 info)
544db8dac20SDavid S. Miller {
545645ca708SEric Dumazet 	__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
546db8dac20SDavid S. Miller }
547db8dac20SDavid S. Miller 
548d7f3f621SBenjamin LaHaise static struct static_key udpv6_encap_needed __read_mostly;
549d7f3f621SBenjamin LaHaise void udpv6_encap_enable(void)
550d7f3f621SBenjamin LaHaise {
551d7f3f621SBenjamin LaHaise 	if (!static_key_enabled(&udpv6_encap_needed))
552d7f3f621SBenjamin LaHaise 		static_key_slow_inc(&udpv6_encap_needed);
553d7f3f621SBenjamin LaHaise }
554d7f3f621SBenjamin LaHaise EXPORT_SYMBOL(udpv6_encap_enable);
555d7f3f621SBenjamin LaHaise 
556db8dac20SDavid S. Miller int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
557db8dac20SDavid S. Miller {
558db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
559db8dac20SDavid S. Miller 	int rc;
560db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
561db8dac20SDavid S. Miller 
562db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
563db8dac20SDavid S. Miller 		goto drop;
564db8dac20SDavid S. Miller 
565d7f3f621SBenjamin LaHaise 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
566d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
567d7f3f621SBenjamin LaHaise 
568d7f3f621SBenjamin LaHaise 		/*
569d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
570d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
571d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
572d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
573d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
574d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
575d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
576d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
577d7f3f621SBenjamin LaHaise 		 */
578d7f3f621SBenjamin LaHaise 
579d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
580d7f3f621SBenjamin LaHaise 		encap_rcv = ACCESS_ONCE(up->encap_rcv);
581e5aed006SHannes Frederic Sowa 		if (encap_rcv) {
582d7f3f621SBenjamin LaHaise 			int ret;
583d7f3f621SBenjamin LaHaise 
5840a80966bSTom Herbert 			/* Verify checksum before giving to encap */
5850a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
5860a80966bSTom Herbert 				goto csum_error;
5870a80966bSTom Herbert 
588d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
589d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
59002c22347SEric Dumazet 				__UDP_INC_STATS(sock_net(sk),
591d7f3f621SBenjamin LaHaise 						UDP_MIB_INDATAGRAMS,
592d7f3f621SBenjamin LaHaise 						is_udplite);
593d7f3f621SBenjamin LaHaise 				return -ret;
594d7f3f621SBenjamin LaHaise 			}
595d7f3f621SBenjamin LaHaise 		}
596d7f3f621SBenjamin LaHaise 
597d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
598d7f3f621SBenjamin LaHaise 	}
599d7f3f621SBenjamin LaHaise 
600db8dac20SDavid S. Miller 	/*
601db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
602db8dac20SDavid S. Miller 	 */
603db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
604db8dac20SDavid S. Miller 
605db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
606ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
607db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
608db8dac20SDavid S. Miller 			goto drop;
609db8dac20SDavid S. Miller 		}
610db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
611ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
612db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
613db8dac20SDavid S. Miller 			goto drop;
614db8dac20SDavid S. Miller 		}
615db8dac20SDavid S. Miller 	}
616db8dac20SDavid S. Miller 
617ce25d66aSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
618ce25d66aSEric Dumazet 	    udp_lib_checksum_complete(skb))
6196a5dc9e5SEric Dumazet 		goto csum_error;
620ce25d66aSEric Dumazet 
621ba66bbe5SDaniel Borkmann 	if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
622a6127697SMichal Kubeček 		goto drop;
623db8dac20SDavid S. Miller 
624e6afc8acSsamanthakumar 	udp_csum_pull_header(skb);
625274f482dSSorin Dumitru 	if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
62602c22347SEric Dumazet 		__UDP6_INC_STATS(sock_net(sk),
6273e215c8dSJames M Leddy 				 UDP_MIB_RCVBUFERRORS, is_udplite);
628cb80ef46SBenjamin LaHaise 		goto drop;
6293e215c8dSJames M Leddy 	}
630cb80ef46SBenjamin LaHaise 
631d826eb14SEric Dumazet 	skb_dst_drop(skb);
632db8dac20SDavid S. Miller 
633cb80ef46SBenjamin LaHaise 	bh_lock_sock(sk);
634cb80ef46SBenjamin LaHaise 	rc = 0;
635cb80ef46SBenjamin LaHaise 	if (!sock_owned_by_user(sk))
636f7ad74feSBenjamin LaHaise 		rc = __udpv6_queue_rcv_skb(sk, skb);
637cb80ef46SBenjamin LaHaise 	else if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) {
638cb80ef46SBenjamin LaHaise 		bh_unlock_sock(sk);
639cb80ef46SBenjamin LaHaise 		goto drop;
640cb80ef46SBenjamin LaHaise 	}
641cb80ef46SBenjamin LaHaise 	bh_unlock_sock(sk);
642f7ad74feSBenjamin LaHaise 
643f7ad74feSBenjamin LaHaise 	return rc;
6443e215c8dSJames M Leddy 
6456a5dc9e5SEric Dumazet csum_error:
64602c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
647db8dac20SDavid S. Miller drop:
64802c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
649cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
650db8dac20SDavid S. Miller 	kfree_skb(skb);
651db8dac20SDavid S. Miller 	return -1;
652db8dac20SDavid S. Miller }
653db8dac20SDavid S. Miller 
6545cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
655b71d1d42SEric Dumazet 				   __be16 loc_port, const struct in6_addr *loc_addr,
656b71d1d42SEric Dumazet 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
6575cf3d461SDavid Held 				   int dif, unsigned short hnum)
658db8dac20SDavid S. Miller {
6599e89fd8bSSven Wegener 	struct inet_sock *inet = inet_sk(sk);
660db8dac20SDavid S. Miller 
6619e89fd8bSSven Wegener 	if (!net_eq(sock_net(sk), net))
6625cf3d461SDavid Held 		return false;
663b8ad0cbcSDaniel Lezcano 
6645cf3d461SDavid Held 	if (udp_sk(sk)->udp_port_hash != hnum ||
6655cf3d461SDavid Held 	    sk->sk_family != PF_INET6 ||
6665cf3d461SDavid Held 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
6675cf3d461SDavid Held 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
6685cf3d461SDavid Held 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
66933b4b015SHenning Rogge 	    (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
67033b4b015SHenning Rogge 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
67133b4b015SHenning Rogge 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
6725cf3d461SDavid Held 		return false;
6739e89fd8bSSven Wegener 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
6745cf3d461SDavid Held 		return false;
6755cf3d461SDavid Held 	return true;
676db8dac20SDavid S. Miller }
677db8dac20SDavid S. Miller 
6784068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
6794068579eSTom Herbert {
6804068579eSTom Herbert 	/* RFC 2460 section 8.1 says that we SHOULD log
6814068579eSTom Herbert 	 * this error. Well, it is reasonable.
6824068579eSTom Herbert 	 */
683ba7a46f1SJoe Perches 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
6844068579eSTom Herbert 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
6854068579eSTom Herbert 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
6864068579eSTom Herbert }
6874068579eSTom Herbert 
688db8dac20SDavid S. Miller /*
689db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
690db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
691db8dac20SDavid S. Miller  */
692e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
693b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
69436cbb245SRick Jones 		struct udp_table *udptable, int proto)
695db8dac20SDavid S. Miller {
696ca065d0cSEric Dumazet 	struct sock *sk, *first = NULL;
697db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
6985cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
6995cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
700ca065d0cSEric Dumazet 	unsigned int offset = offsetof(typeof(*sk), sk_node);
7012dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
702ca065d0cSEric Dumazet 	int dif = inet6_iif(skb);
703ca065d0cSEric Dumazet 	struct hlist_node *node;
704ca065d0cSEric Dumazet 	struct sk_buff *nskb;
7052dc41cffSDavid Held 
7062dc41cffSDavid Held 	if (use_hash2) {
7072dc41cffSDavid Held 		hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
7082dc41cffSDavid Held 			    udp_table.mask;
7092dc41cffSDavid Held 		hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask;
7102dc41cffSDavid Held start_lookup:
7112dc41cffSDavid Held 		hslot = &udp_table.hash2[hash2];
7122dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
7132dc41cffSDavid Held 	}
714db8dac20SDavid S. Miller 
715ca065d0cSEric Dumazet 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
716ca065d0cSEric Dumazet 		if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
717ca065d0cSEric Dumazet 					    uh->source, saddr, dif, hnum))
718ca065d0cSEric Dumazet 			continue;
7191c19448cSTom Herbert 		/* If zero checksum and no_check is not on for
7204068579eSTom Herbert 		 * the socket then skip it.
7214068579eSTom Herbert 		 */
722ca065d0cSEric Dumazet 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
723ca065d0cSEric Dumazet 			continue;
724ca065d0cSEric Dumazet 		if (!first) {
725ca065d0cSEric Dumazet 			first = sk;
726ca065d0cSEric Dumazet 			continue;
727db8dac20SDavid S. Miller 		}
728ca065d0cSEric Dumazet 		nskb = skb_clone(skb, GFP_ATOMIC);
729ca065d0cSEric Dumazet 		if (unlikely(!nskb)) {
730ca065d0cSEric Dumazet 			atomic_inc(&sk->sk_drops);
73102c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
732ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
73302c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_INERRORS,
734ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
735ca065d0cSEric Dumazet 			continue;
736a1ab77f9SEric Dumazet 		}
737db8dac20SDavid S. Miller 
738ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(sk, nskb) > 0)
739ca065d0cSEric Dumazet 			consume_skb(nskb);
740ca065d0cSEric Dumazet 	}
741a1ab77f9SEric Dumazet 
7422dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
7432dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
7442dc41cffSDavid Held 		hash2 = hash2_any;
7452dc41cffSDavid Held 		goto start_lookup;
7462dc41cffSDavid Held 	}
7472dc41cffSDavid Held 
748ca065d0cSEric Dumazet 	if (first) {
749ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(first, skb) > 0)
750ca065d0cSEric Dumazet 			consume_skb(skb);
751a1ab77f9SEric Dumazet 	} else {
752ca065d0cSEric Dumazet 		kfree_skb(skb);
75302c22347SEric Dumazet 		__UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
75436cbb245SRick Jones 				 proto == IPPROTO_UDPLITE);
755a1ab77f9SEric Dumazet 	}
756db8dac20SDavid S. Miller 	return 0;
757db8dac20SDavid S. Miller }
758db8dac20SDavid S. Miller 
759645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
760db8dac20SDavid S. Miller 		   int proto)
761db8dac20SDavid S. Miller {
762b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
763ca065d0cSEric Dumazet 	struct net *net = dev_net(skb->dev);
764ca065d0cSEric Dumazet 	struct udphdr *uh;
765ca065d0cSEric Dumazet 	struct sock *sk;
766db8dac20SDavid S. Miller 	u32 ulen = 0;
767db8dac20SDavid S. Miller 
768db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
769d6bc0149SBjørn Mork 		goto discard;
770db8dac20SDavid S. Miller 
771db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
772db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
773db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
774db8dac20SDavid S. Miller 
775db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
776db8dac20SDavid S. Miller 	if (ulen > skb->len)
777db8dac20SDavid S. Miller 		goto short_packet;
778db8dac20SDavid S. Miller 
779db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
780db8dac20SDavid S. Miller 		/* UDP validates ulen. */
781db8dac20SDavid S. Miller 
782db8dac20SDavid S. Miller 		/* Check for jumbo payload */
783db8dac20SDavid S. Miller 		if (ulen == 0)
784db8dac20SDavid S. Miller 			ulen = skb->len;
785db8dac20SDavid S. Miller 
786db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
787db8dac20SDavid S. Miller 			goto short_packet;
788db8dac20SDavid S. Miller 
789db8dac20SDavid S. Miller 		if (ulen < skb->len) {
790db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
791db8dac20SDavid S. Miller 				goto short_packet;
792db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
793db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
794db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
795db8dac20SDavid S. Miller 		}
796db8dac20SDavid S. Miller 	}
797db8dac20SDavid S. Miller 
798db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
7996a5dc9e5SEric Dumazet 		goto csum_error;
800db8dac20SDavid S. Miller 
801db8dac20SDavid S. Miller 	/*
802db8dac20SDavid S. Miller 	 *	Multicast receive code
803db8dac20SDavid S. Miller 	 */
804db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
805e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
80636cbb245SRick Jones 				saddr, daddr, udptable, proto);
807db8dac20SDavid S. Miller 
808db8dac20SDavid S. Miller 	/* Unicast */
809db8dac20SDavid S. Miller 
810db8dac20SDavid S. Miller 	/*
811db8dac20SDavid S. Miller 	 * check socket cache ... must talk to Alan about his plans
812db8dac20SDavid S. Miller 	 * for sock caches... i'll skip this for now.
813db8dac20SDavid S. Miller 	 */
814607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
81553b24b8fSIan Morris 	if (sk) {
816a5b50476SEliezer Tamir 		int ret;
817a5b50476SEliezer Tamir 
8181c19448cSTom Herbert 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
8194068579eSTom Herbert 			udp6_csum_zero_error(skb);
8204068579eSTom Herbert 			goto csum_error;
8214068579eSTom Herbert 		}
8224068579eSTom Herbert 
823224d019cSTom Herbert 		if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
8242abb7cdcSTom Herbert 			skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
8252abb7cdcSTom Herbert 						 ip6_compute_pseudo);
8262abb7cdcSTom Herbert 
827a5b50476SEliezer Tamir 		ret = udpv6_queue_rcv_skb(sk, skb);
828db8dac20SDavid S. Miller 
82959dca1d8SBill Sommerfeld 		/* a return value > 0 means to resubmit the input */
830cb80ef46SBenjamin LaHaise 		if (ret > 0)
83159dca1d8SBill Sommerfeld 			return ret;
832cb80ef46SBenjamin LaHaise 
833cb80ef46SBenjamin LaHaise 		return 0;
834cb80ef46SBenjamin LaHaise 	}
835cb80ef46SBenjamin LaHaise 
8364068579eSTom Herbert 	if (!uh->check) {
8374068579eSTom Herbert 		udp6_csum_zero_error(skb);
8384068579eSTom Herbert 		goto csum_error;
8394068579eSTom Herbert 	}
8404068579eSTom Herbert 
841db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
842db8dac20SDavid S. Miller 		goto discard;
843db8dac20SDavid S. Miller 
844db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
8456a5dc9e5SEric Dumazet 		goto csum_error;
846db8dac20SDavid S. Miller 
84702c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
8483ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
849db8dac20SDavid S. Miller 
850db8dac20SDavid S. Miller 	kfree_skb(skb);
851db8dac20SDavid S. Miller 	return 0;
852db8dac20SDavid S. Miller 
853db8dac20SDavid S. Miller short_packet:
854ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
855db8dac20SDavid S. Miller 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
856ba7a46f1SJoe Perches 			    saddr, ntohs(uh->source),
857ba7a46f1SJoe Perches 			    ulen, skb->len,
858ba7a46f1SJoe Perches 			    daddr, ntohs(uh->dest));
8596a5dc9e5SEric Dumazet 	goto discard;
8606a5dc9e5SEric Dumazet csum_error:
86102c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
862db8dac20SDavid S. Miller discard:
86302c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
864db8dac20SDavid S. Miller 	kfree_skb(skb);
865db8dac20SDavid S. Miller 	return 0;
866db8dac20SDavid S. Miller }
867db8dac20SDavid S. Miller 
868db8dac20SDavid S. Miller static __inline__ int udpv6_rcv(struct sk_buff *skb)
869db8dac20SDavid S. Miller {
870645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
871db8dac20SDavid S. Miller }
872db8dac20SDavid S. Miller 
873db8dac20SDavid S. Miller /*
874db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
875db8dac20SDavid S. Miller  */
876db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
877db8dac20SDavid S. Miller {
878db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
879db8dac20SDavid S. Miller 
88036d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
88136d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
88236d926b9SDenis V. Lunev 	else if (up->pending) {
883db8dac20SDavid S. Miller 		up->len = 0;
884db8dac20SDavid S. Miller 		up->pending = 0;
885db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
886db8dac20SDavid S. Miller 	}
887db8dac20SDavid S. Miller }
888db8dac20SDavid S. Miller 
889493c6be3SSridhar Samudrala /**
890493c6be3SSridhar Samudrala  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
891493c6be3SSridhar Samudrala  *	@sk:	socket we are sending on
892493c6be3SSridhar Samudrala  *	@skb:	sk_buff containing the filled-in UDP header
893493c6be3SSridhar Samudrala  *		(checksum field must be zeroed out)
894493c6be3SSridhar Samudrala  */
895493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
896493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
897493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
898493c6be3SSridhar Samudrala {
899493c6be3SSridhar Samudrala 	unsigned int offset;
900493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
901d39d938cSVlad Yasevich 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
902493c6be3SSridhar Samudrala 	__wsum csum = 0;
903493c6be3SSridhar Samudrala 
904d39d938cSVlad Yasevich 	if (!frags) {
905493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
906493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
907493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
908493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
909493c6be3SSridhar Samudrala 	} else {
910493c6be3SSridhar Samudrala 		/*
911493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
912493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
913493c6be3SSridhar Samudrala 		 * should be together
914493c6be3SSridhar Samudrala 		 */
915493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
916493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
917493c6be3SSridhar Samudrala 
918493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
919493c6be3SSridhar Samudrala 
920d39d938cSVlad Yasevich 		do {
921d39d938cSVlad Yasevich 			csum = csum_add(csum, frags->csum);
922d39d938cSVlad Yasevich 		} while ((frags = frags->next));
923493c6be3SSridhar Samudrala 
924493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
925493c6be3SSridhar Samudrala 					    csum);
926493c6be3SSridhar Samudrala 		if (uh->check == 0)
927493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
928493c6be3SSridhar Samudrala 	}
929493c6be3SSridhar Samudrala }
930493c6be3SSridhar Samudrala 
931db8dac20SDavid S. Miller /*
932db8dac20SDavid S. Miller  *	Sending
933db8dac20SDavid S. Miller  */
934db8dac20SDavid S. Miller 
935d39d938cSVlad Yasevich static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
936db8dac20SDavid S. Miller {
937d39d938cSVlad Yasevich 	struct sock *sk = skb->sk;
938db8dac20SDavid S. Miller 	struct udphdr *uh;
939db8dac20SDavid S. Miller 	int err = 0;
940db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
941db8dac20SDavid S. Miller 	__wsum csum = 0;
942d39d938cSVlad Yasevich 	int offset = skb_transport_offset(skb);
943d39d938cSVlad Yasevich 	int len = skb->len - offset;
944db8dac20SDavid S. Miller 
945db8dac20SDavid S. Miller 	/*
946db8dac20SDavid S. Miller 	 * Create a UDP header
947db8dac20SDavid S. Miller 	 */
948db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
9491958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
9501958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
951d39d938cSVlad Yasevich 	uh->len = htons(len);
952db8dac20SDavid S. Miller 	uh->check = 0;
953db8dac20SDavid S. Miller 
954db8dac20SDavid S. Miller 	if (is_udplite)
955d39d938cSVlad Yasevich 		csum = udplite_csum(skb);
956d39d938cSVlad Yasevich 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
9574068579eSTom Herbert 		skb->ip_summed = CHECKSUM_NONE;
9584068579eSTom Herbert 		goto send;
9594068579eSTom Herbert 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
960d39d938cSVlad Yasevich 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
961493c6be3SSridhar Samudrala 		goto send;
962493c6be3SSridhar Samudrala 	} else
963d39d938cSVlad Yasevich 		csum = udp_csum(skb);
964db8dac20SDavid S. Miller 
965db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
9664c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
967d39d938cSVlad Yasevich 				    len, fl6->flowi6_proto, csum);
968db8dac20SDavid S. Miller 	if (uh->check == 0)
969db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
970db8dac20SDavid S. Miller 
971493c6be3SSridhar Samudrala send:
972d39d938cSVlad Yasevich 	err = ip6_send_skb(skb);
9736ce9e7b5SEric Dumazet 	if (err) {
9746ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
9756aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
9766ce9e7b5SEric Dumazet 				       UDP_MIB_SNDBUFERRORS, is_udplite);
9776ce9e7b5SEric Dumazet 			err = 0;
9786ce9e7b5SEric Dumazet 		}
9796aef70a8SEric Dumazet 	} else {
9806aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
9816ce9e7b5SEric Dumazet 			       UDP_MIB_OUTDATAGRAMS, is_udplite);
9826aef70a8SEric Dumazet 	}
983d39d938cSVlad Yasevich 	return err;
984d39d938cSVlad Yasevich }
985d39d938cSVlad Yasevich 
986d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
987d39d938cSVlad Yasevich {
988d39d938cSVlad Yasevich 	struct sk_buff *skb;
989d39d938cSVlad Yasevich 	struct udp_sock  *up = udp_sk(sk);
990d39d938cSVlad Yasevich 	struct flowi6 fl6;
991d39d938cSVlad Yasevich 	int err = 0;
992d39d938cSVlad Yasevich 
993d39d938cSVlad Yasevich 	if (up->pending == AF_INET)
994d39d938cSVlad Yasevich 		return udp_push_pending_frames(sk);
995d39d938cSVlad Yasevich 
996d39d938cSVlad Yasevich 	/* ip6_finish_skb will release the cork, so make a copy of
997d39d938cSVlad Yasevich 	 * fl6 here.
998d39d938cSVlad Yasevich 	 */
999d39d938cSVlad Yasevich 	fl6 = inet_sk(sk)->cork.fl.u.ip6;
1000d39d938cSVlad Yasevich 
1001d39d938cSVlad Yasevich 	skb = ip6_finish_skb(sk);
1002d39d938cSVlad Yasevich 	if (!skb)
1003d39d938cSVlad Yasevich 		goto out;
1004d39d938cSVlad Yasevich 
1005d39d938cSVlad Yasevich 	err = udp_v6_send_skb(skb, &fl6);
1006d39d938cSVlad Yasevich 
1007db8dac20SDavid S. Miller out:
1008db8dac20SDavid S. Miller 	up->len = 0;
1009db8dac20SDavid S. Miller 	up->pending = 0;
1010db8dac20SDavid S. Miller 	return err;
1011db8dac20SDavid S. Miller }
1012db8dac20SDavid S. Miller 
10131b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1014db8dac20SDavid S. Miller {
1015db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1016db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1017db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1018db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1019342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
102020c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1021db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
102245f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
1023db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
10244c9483b2SDavid S. Miller 	struct flowi6 fl6;
1025db8dac20SDavid S. Miller 	struct dst_entry *dst;
102626879da5SWei Wang 	struct ipcm6_cookie ipc6;
1027db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
1028db8dac20SDavid S. Miller 	int ulen = len;
1029db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1030db8dac20SDavid S. Miller 	int err;
1031db8dac20SDavid S. Miller 	int connected = 0;
1032db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1033db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1034ad1e46a8SSoheil Hassas Yeganeh 	struct sockcm_cookie sockc;
1035db8dac20SDavid S. Miller 
103626879da5SWei Wang 	ipc6.hlimit = -1;
103726879da5SWei Wang 	ipc6.tclass = -1;
103826879da5SWei Wang 	ipc6.dontfrag = -1;
103926879da5SWei Wang 
1040db8dac20SDavid S. Miller 	/* destination address check */
1041db8dac20SDavid S. Miller 	if (sin6) {
1042db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1043db8dac20SDavid S. Miller 			return -EINVAL;
1044db8dac20SDavid S. Miller 
1045db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1046db8dac20SDavid S. Miller 		case AF_INET6:
1047db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1048db8dac20SDavid S. Miller 				return -EINVAL;
1049db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1050db8dac20SDavid S. Miller 			break;
1051db8dac20SDavid S. Miller 		case AF_INET:
1052db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1053db8dac20SDavid S. Miller 		case AF_UNSPEC:
1054db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1055db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1056db8dac20SDavid S. Miller 			daddr = NULL;
1057db8dac20SDavid S. Miller 			break;
1058db8dac20SDavid S. Miller 		default:
1059db8dac20SDavid S. Miller 			return -EINVAL;
1060db8dac20SDavid S. Miller 		}
1061db8dac20SDavid S. Miller 	} else if (!up->pending) {
1062db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1063db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1064efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
1065db8dac20SDavid S. Miller 	} else
1066db8dac20SDavid S. Miller 		daddr = NULL;
1067db8dac20SDavid S. Miller 
1068db8dac20SDavid S. Miller 	if (daddr) {
1069db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1070db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1071db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1072c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1073db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1074db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1075db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1076db8dac20SDavid S. Miller do_udp_sendmsg:
1077db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1078db8dac20SDavid S. Miller 				return -ENETUNREACH;
10791b784140SYing Xue 			return udp_sendmsg(sk, msg, len);
1080db8dac20SDavid S. Miller 		}
1081db8dac20SDavid S. Miller 	}
1082db8dac20SDavid S. Miller 
1083db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
10841b784140SYing Xue 		return udp_sendmsg(sk, msg, len);
1085db8dac20SDavid S. Miller 
1086db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1087db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1088db8dac20SDavid S. Miller 	   */
1089db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1090db8dac20SDavid S. Miller 		return -EMSGSIZE;
1091db8dac20SDavid S. Miller 
109203485f2aSVlad Yasevich 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1093db8dac20SDavid S. Miller 	if (up->pending) {
1094db8dac20SDavid S. Miller 		/*
1095db8dac20SDavid S. Miller 		 * There are pending frames.
1096db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1097db8dac20SDavid S. Miller 		 */
1098db8dac20SDavid S. Miller 		lock_sock(sk);
1099db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1100db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1101db8dac20SDavid S. Miller 				release_sock(sk);
1102db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1103db8dac20SDavid S. Miller 			}
1104db8dac20SDavid S. Miller 			dst = NULL;
1105db8dac20SDavid S. Miller 			goto do_append_data;
1106db8dac20SDavid S. Miller 		}
1107db8dac20SDavid S. Miller 		release_sock(sk);
1108db8dac20SDavid S. Miller 	}
1109db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1110db8dac20SDavid S. Miller 
11114c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1112db8dac20SDavid S. Miller 
1113db8dac20SDavid S. Miller 	if (sin6) {
1114db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1115db8dac20SDavid S. Miller 			return -EINVAL;
1116db8dac20SDavid S. Miller 
11171958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1118db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1119db8dac20SDavid S. Miller 
1120db8dac20SDavid S. Miller 		if (np->sndflow) {
11214c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
11224c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
11234c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
112463159f29SIan Morris 				if (!flowlabel)
1125db8dac20SDavid S. Miller 					return -EINVAL;
1126db8dac20SDavid S. Miller 			}
1127db8dac20SDavid S. Miller 		}
1128db8dac20SDavid S. Miller 
1129db8dac20SDavid S. Miller 		/*
1130db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1131db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1132db8dac20SDavid S. Miller 		 */
1133db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1134efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1135efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
1136db8dac20SDavid S. Miller 
1137db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1138db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1139842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
11404c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1141db8dac20SDavid S. Miller 	} else {
1142db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1143db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1144db8dac20SDavid S. Miller 
11451958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1146efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
11474c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
1148db8dac20SDavid S. Miller 		connected = 1;
1149db8dac20SDavid S. Miller 	}
1150db8dac20SDavid S. Miller 
11514c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
11524c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1153db8dac20SDavid S. Miller 
11544c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
11554c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
11569f690db7SYang Hongyang 
11574c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
1158c14ac945SSoheil Hassas Yeganeh 	sockc.tsflags = sk->sk_tsflags;
115951953d5bSBrian Haley 
1160db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1161db8dac20SDavid S. Miller 		opt = &opt_space;
1162db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1163db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
116426879da5SWei Wang 		ipc6.opt = opt;
1165db8dac20SDavid S. Miller 
116626879da5SWei Wang 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc);
1167db8dac20SDavid S. Miller 		if (err < 0) {
1168db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1169db8dac20SDavid S. Miller 			return err;
1170db8dac20SDavid S. Miller 		}
11714c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
11724c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
117363159f29SIan Morris 			if (!flowlabel)
1174db8dac20SDavid S. Miller 				return -EINVAL;
1175db8dac20SDavid S. Miller 		}
1176db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1177db8dac20SDavid S. Miller 			opt = NULL;
1178db8dac20SDavid S. Miller 		connected = 0;
1179db8dac20SDavid S. Miller 	}
118045f6fad8SEric Dumazet 	if (!opt) {
118145f6fad8SEric Dumazet 		opt = txopt_get(np);
118245f6fad8SEric Dumazet 		opt_to_free = opt;
118345f6fad8SEric Dumazet 	}
1184db8dac20SDavid S. Miller 	if (flowlabel)
1185db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1186db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
118726879da5SWei Wang 	ipc6.opt = opt;
1188db8dac20SDavid S. Miller 
11894c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
1190876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
11914e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
1192876c7f41SBrian Haley 	else
11934c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
11944c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
11954e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
11961958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1197db8dac20SDavid S. Miller 
11984c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
119920c59de2SArnaud Ebalard 	if (final_p)
1200db8dac20SDavid S. Miller 		connected = 0;
1201db8dac20SDavid S. Miller 
12024c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
12034c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
1204db8dac20SDavid S. Miller 		connected = 0;
1205c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1206c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1207db8dac20SDavid S. Miller 
12084c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1209db8dac20SDavid S. Miller 
121038b7097bSHannes Frederic Sowa 	if (ipc6.tclass < 0)
121138b7097bSHannes Frederic Sowa 		ipc6.tclass = np->tclass;
121238b7097bSHannes Frederic Sowa 
121338b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
121438b7097bSHannes Frederic Sowa 
12150e0d44abSSteffen Klassert 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
121668d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
121768d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
121868d0c6d3SDavid S. Miller 		dst = NULL;
1219db8dac20SDavid S. Miller 		goto out;
1220db8dac20SDavid S. Miller 	}
1221db8dac20SDavid S. Miller 
122226879da5SWei Wang 	if (ipc6.hlimit < 0)
122326879da5SWei Wang 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1224db8dac20SDavid S. Miller 
1225db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1226db8dac20SDavid S. Miller 		goto do_confirm;
1227db8dac20SDavid S. Miller back_from_confirm:
1228db8dac20SDavid S. Miller 
122903485f2aSVlad Yasevich 	/* Lockless fast path for the non-corking case */
123003485f2aSVlad Yasevich 	if (!corkreq) {
123103485f2aSVlad Yasevich 		struct sk_buff *skb;
123203485f2aSVlad Yasevich 
123303485f2aSVlad Yasevich 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
123426879da5SWei Wang 				   sizeof(struct udphdr), &ipc6,
123503485f2aSVlad Yasevich 				   &fl6, (struct rt6_info *)dst,
123626879da5SWei Wang 				   msg->msg_flags, &sockc);
123703485f2aSVlad Yasevich 		err = PTR_ERR(skb);
123803485f2aSVlad Yasevich 		if (!IS_ERR_OR_NULL(skb))
123903485f2aSVlad Yasevich 			err = udp_v6_send_skb(skb, &fl6);
124003485f2aSVlad Yasevich 		goto release_dst;
124103485f2aSVlad Yasevich 	}
124203485f2aSVlad Yasevich 
1243db8dac20SDavid S. Miller 	lock_sock(sk);
1244db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1245db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1246db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1247db8dac20SDavid S. Miller 		release_sock(sk);
1248db8dac20SDavid S. Miller 
1249ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 2\n");
1250db8dac20SDavid S. Miller 		err = -EINVAL;
1251db8dac20SDavid S. Miller 		goto out;
1252db8dac20SDavid S. Miller 	}
1253db8dac20SDavid S. Miller 
1254db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1255db8dac20SDavid S. Miller 
1256db8dac20SDavid S. Miller do_append_data:
125726879da5SWei Wang 	if (ipc6.dontfrag < 0)
125826879da5SWei Wang 		ipc6.dontfrag = np->dontfrag;
1259db8dac20SDavid S. Miller 	up->len += ulen;
126026879da5SWei Wang 	err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
126126879da5SWei Wang 			      &ipc6, &fl6, (struct rt6_info *)dst,
126226879da5SWei Wang 			      corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, &sockc);
1263db8dac20SDavid S. Miller 	if (err)
1264db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1265db8dac20SDavid S. Miller 	else if (!corkreq)
1266db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1267db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1268db8dac20SDavid S. Miller 		up->pending = 0;
1269db8dac20SDavid S. Miller 
127003485f2aSVlad Yasevich 	if (err > 0)
127103485f2aSVlad Yasevich 		err = np->recverr ? net_xmit_errno(err) : 0;
127203485f2aSVlad Yasevich 	release_sock(sk);
127303485f2aSVlad Yasevich 
127403485f2aSVlad Yasevich release_dst:
1275db8dac20SDavid S. Miller 	if (dst) {
1276db8dac20SDavid S. Miller 		if (connected) {
1277db8dac20SDavid S. Miller 			ip6_dst_store(sk, dst,
1278efe4208fSEric Dumazet 				      ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1279efe4208fSEric Dumazet 				      &sk->sk_v6_daddr : NULL,
1280db8dac20SDavid S. Miller #ifdef CONFIG_IPV6_SUBTREES
12814c9483b2SDavid S. Miller 				      ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1282db8dac20SDavid S. Miller 				      &np->saddr :
1283db8dac20SDavid S. Miller #endif
1284db8dac20SDavid S. Miller 				      NULL);
1285db8dac20SDavid S. Miller 		} else {
1286db8dac20SDavid S. Miller 			dst_release(dst);
1287db8dac20SDavid S. Miller 		}
1288a3c96089SYOSHIFUJI Hideaki 		dst = NULL;
1289db8dac20SDavid S. Miller 	}
1290db8dac20SDavid S. Miller 
1291db8dac20SDavid S. Miller out:
1292a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
1293db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
129445f6fad8SEric Dumazet 	txopt_put(opt_to_free);
1295db8dac20SDavid S. Miller 	if (!err)
1296db8dac20SDavid S. Miller 		return len;
1297db8dac20SDavid S. Miller 	/*
1298db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1299db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1300db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1301db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1302db8dac20SDavid S. Miller 	 * seems like overkill.
1303db8dac20SDavid S. Miller 	 */
1304db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
13056aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
1306235b9f7aSPavel Emelyanov 			       UDP_MIB_SNDBUFERRORS, is_udplite);
1307db8dac20SDavid S. Miller 	}
1308db8dac20SDavid S. Miller 	return err;
1309db8dac20SDavid S. Miller 
1310db8dac20SDavid S. Miller do_confirm:
1311db8dac20SDavid S. Miller 	dst_confirm(dst);
1312db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1313db8dac20SDavid S. Miller 		goto back_from_confirm;
1314db8dac20SDavid S. Miller 	err = 0;
1315db8dac20SDavid S. Miller 	goto out;
1316db8dac20SDavid S. Miller }
1317db8dac20SDavid S. Miller 
13187d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1319db8dac20SDavid S. Miller {
132044046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1321db8dac20SDavid S. Miller 	lock_sock(sk);
1322db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1323db8dac20SDavid S. Miller 	release_sock(sk);
1324db8dac20SDavid S. Miller 
132544046a59STom Parkin 	if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
132644046a59STom Parkin 		void (*encap_destroy)(struct sock *sk);
132744046a59STom Parkin 		encap_destroy = ACCESS_ONCE(up->encap_destroy);
132844046a59STom Parkin 		if (encap_destroy)
132944046a59STom Parkin 			encap_destroy(sk);
133044046a59STom Parkin 	}
133144046a59STom Parkin 
1332db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1333db8dac20SDavid S. Miller }
1334db8dac20SDavid S. Miller 
1335db8dac20SDavid S. Miller /*
1336db8dac20SDavid S. Miller  *	Socket option code for UDP
1337db8dac20SDavid S. Miller  */
1338db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1339b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1340db8dac20SDavid S. Miller {
1341db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1342db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1343db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1344db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1345db8dac20SDavid S. Miller }
1346db8dac20SDavid S. Miller 
1347db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1348db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1349b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1350db8dac20SDavid S. Miller {
1351db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1352db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1353db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1354db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1355db8dac20SDavid S. Miller }
1356db8dac20SDavid S. Miller #endif
1357db8dac20SDavid S. Miller 
1358db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1359db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1360db8dac20SDavid S. Miller {
1361db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1362db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1363db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1364db8dac20SDavid S. Miller }
1365db8dac20SDavid S. Miller 
1366db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1367db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1368db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1369db8dac20SDavid S. Miller {
1370db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1371db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1372db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1373db8dac20SDavid S. Miller }
1374db8dac20SDavid S. Miller #endif
1375db8dac20SDavid S. Miller 
137641135cc8SAlexey Dobriyan static const struct inet6_protocol udpv6_protocol = {
1377db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1378db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1379db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1380db8dac20SDavid S. Miller };
1381db8dac20SDavid S. Miller 
1382db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1383db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1384db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1385db8dac20SDavid S. Miller {
138617ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
138717ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
138817ef66afSLorenzo Colitti 	} else {
138917ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
139017ef66afSLorenzo Colitti 		struct inet_sock *inet = inet_sk(v);
139117ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
139217ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
139317ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
139417ef66afSLorenzo Colitti 	}
1395db8dac20SDavid S. Miller 	return 0;
1396db8dac20SDavid S. Miller }
1397db8dac20SDavid S. Miller 
139873cb88ecSArjan van de Ven static const struct file_operations udp6_afinfo_seq_fops = {
139973cb88ecSArjan van de Ven 	.owner    = THIS_MODULE,
140073cb88ecSArjan van de Ven 	.open     = udp_seq_open,
140173cb88ecSArjan van de Ven 	.read     = seq_read,
140273cb88ecSArjan van de Ven 	.llseek   = seq_lseek,
140373cb88ecSArjan van de Ven 	.release  = seq_release_net
140473cb88ecSArjan van de Ven };
140573cb88ecSArjan van de Ven 
1406db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1407db8dac20SDavid S. Miller 	.name		= "udp6",
1408db8dac20SDavid S. Miller 	.family		= AF_INET6,
1409645ca708SEric Dumazet 	.udp_table	= &udp_table,
141073cb88ecSArjan van de Ven 	.seq_fops	= &udp6_afinfo_seq_fops,
1411dda61925SDenis V. Lunev 	.seq_ops	= {
1412dda61925SDenis V. Lunev 		.show		= udp6_seq_show,
1413dda61925SDenis V. Lunev 	},
1414db8dac20SDavid S. Miller };
1415db8dac20SDavid S. Miller 
14162c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1417db8dac20SDavid S. Miller {
14180c96d8c5SDaniel Lezcano 	return udp_proc_register(net, &udp6_seq_afinfo);
1419db8dac20SDavid S. Miller }
1420db8dac20SDavid S. Miller 
1421ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1422ec120da6SIan Morris {
14230c96d8c5SDaniel Lezcano 	udp_proc_unregister(net, &udp6_seq_afinfo);
1424db8dac20SDavid S. Miller }
1425db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1426db8dac20SDavid S. Miller 
1427f77d6021SEric Dumazet void udp_v6_clear_sk(struct sock *sk, int size)
1428f77d6021SEric Dumazet {
1429f77d6021SEric Dumazet 	struct inet_sock *inet = inet_sk(sk);
1430f77d6021SEric Dumazet 
1431f77d6021SEric Dumazet 	/* we do not want to clear pinet6 field, because of RCU lookups */
1432f77d6021SEric Dumazet 	sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1433f77d6021SEric Dumazet 
1434f77d6021SEric Dumazet 	size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1435f77d6021SEric Dumazet 	memset(&inet->pinet6 + 1, 0, size);
1436f77d6021SEric Dumazet }
1437f77d6021SEric Dumazet 
1438db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1439db8dac20SDavid S. Miller 
1440db8dac20SDavid S. Miller struct proto udpv6_prot = {
1441db8dac20SDavid S. Miller 	.name		   = "UDPv6",
1442db8dac20SDavid S. Miller 	.owner		   = THIS_MODULE,
1443db8dac20SDavid S. Miller 	.close		   = udp_lib_close,
1444db8dac20SDavid S. Miller 	.connect	   = ip6_datagram_connect,
1445db8dac20SDavid S. Miller 	.disconnect	   = udp_disconnect,
1446db8dac20SDavid S. Miller 	.ioctl		   = udp_ioctl,
1447db8dac20SDavid S. Miller 	.destroy	   = udpv6_destroy_sock,
1448db8dac20SDavid S. Miller 	.setsockopt	   = udpv6_setsockopt,
1449db8dac20SDavid S. Miller 	.getsockopt	   = udpv6_getsockopt,
1450db8dac20SDavid S. Miller 	.sendmsg	   = udpv6_sendmsg,
1451db8dac20SDavid S. Miller 	.recvmsg	   = udpv6_recvmsg,
1452f7ad74feSBenjamin LaHaise 	.backlog_rcv	   = __udpv6_queue_rcv_skb,
1453e646b657SMartin KaFai Lau 	.release_cb	   = ip6_datagram_release_cb,
1454db8dac20SDavid S. Miller 	.hash		   = udp_lib_hash,
1455db8dac20SDavid S. Miller 	.unhash		   = udp_lib_unhash,
1456719f8358SEric Dumazet 	.rehash		   = udp_v6_rehash,
1457db8dac20SDavid S. Miller 	.get_port	   = udp_v6_get_port,
1458db8dac20SDavid S. Miller 	.memory_allocated  = &udp_memory_allocated,
1459db8dac20SDavid S. Miller 	.sysctl_mem	   = sysctl_udp_mem,
1460db8dac20SDavid S. Miller 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1461db8dac20SDavid S. Miller 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1462db8dac20SDavid S. Miller 	.obj_size	   = sizeof(struct udp6_sock),
1463271b72c7SEric Dumazet 	.slab_flags	   = SLAB_DESTROY_BY_RCU,
1464645ca708SEric Dumazet 	.h.udp_table	   = &udp_table,
1465db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1466db8dac20SDavid S. Miller 	.compat_setsockopt = compat_udpv6_setsockopt,
1467db8dac20SDavid S. Miller 	.compat_getsockopt = compat_udpv6_getsockopt,
1468db8dac20SDavid S. Miller #endif
1469f77d6021SEric Dumazet 	.clear_sk	   = udp_v6_clear_sk,
14705d77dca8SDavid Ahern 	.diag_destroy      = udp_abort,
1471db8dac20SDavid S. Miller };
1472db8dac20SDavid S. Miller 
1473db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1474db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1475db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1476db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1477db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1478db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1479db8dac20SDavid S. Miller };
1480db8dac20SDavid S. Miller 
1481db8dac20SDavid S. Miller int __init udpv6_init(void)
1482db8dac20SDavid S. Miller {
1483db8dac20SDavid S. Miller 	int ret;
1484db8dac20SDavid S. Miller 
14853336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
14863336288aSVlad Yasevich 	if (ret)
1487c6b641a4SVlad Yasevich 		goto out;
14883336288aSVlad Yasevich 
1489db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1490db8dac20SDavid S. Miller 	if (ret)
1491db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1492db8dac20SDavid S. Miller out:
1493db8dac20SDavid S. Miller 	return ret;
1494db8dac20SDavid S. Miller 
1495db8dac20SDavid S. Miller out_udpv6_protocol:
1496db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1497db8dac20SDavid S. Miller 	goto out;
1498db8dac20SDavid S. Miller }
1499db8dac20SDavid S. Miller 
1500db8dac20SDavid S. Miller void udpv6_exit(void)
1501db8dac20SDavid S. Miller {
1502db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1503db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1504db8dac20SDavid S. Miller }
1505