xref: /openbmc/linux/net/ipv6/udp.c (revision 8139dccd)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2db8dac20SDavid S. Miller /*
3db8dac20SDavid S. Miller  *	UDP over IPv6
4db8dac20SDavid S. Miller  *	Linux INET6 implementation
5db8dac20SDavid S. Miller  *
6db8dac20SDavid S. Miller  *	Authors:
7db8dac20SDavid S. Miller  *	Pedro Roque		<roque@di.fc.ul.pt>
8db8dac20SDavid S. Miller  *
9db8dac20SDavid S. Miller  *	Based on linux/ipv4/udp.c
10db8dac20SDavid S. Miller  *
11db8dac20SDavid S. Miller  *	Fixes:
12db8dac20SDavid S. Miller  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
13db8dac20SDavid S. Miller  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
14db8dac20SDavid S. Miller  *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
15db8dac20SDavid S. Miller  *					a single port at the same time.
16db8dac20SDavid S. Miller  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
17db8dac20SDavid S. Miller  *      YOSHIFUJI Hideaki @USAGI:	convert /proc/net/udp6 to seq_file.
18db8dac20SDavid S. Miller  */
19db8dac20SDavid S. Miller 
20aef2fedaSJakub Kicinski #include <linux/bpf-cgroup.h>
21db8dac20SDavid S. Miller #include <linux/errno.h>
22db8dac20SDavid S. Miller #include <linux/types.h>
23db8dac20SDavid S. Miller #include <linux/socket.h>
24db8dac20SDavid S. Miller #include <linux/sockios.h>
25db8dac20SDavid S. Miller #include <linux/net.h>
26db8dac20SDavid S. Miller #include <linux/in6.h>
27db8dac20SDavid S. Miller #include <linux/netdevice.h>
28db8dac20SDavid S. Miller #include <linux/if_arp.h>
29db8dac20SDavid S. Miller #include <linux/ipv6.h>
30db8dac20SDavid S. Miller #include <linux/icmpv6.h>
31db8dac20SDavid S. Miller #include <linux/init.h>
32db8dac20SDavid S. Miller #include <linux/module.h>
33db8dac20SDavid S. Miller #include <linux/skbuff.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
357c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
360e219ae4SPaolo Abeni #include <linux/indirect_call_wrapper.h>
37db8dac20SDavid S. Miller 
38496611d7SCraig Gallek #include <net/addrconf.h>
39db8dac20SDavid S. Miller #include <net/ndisc.h>
40db8dac20SDavid S. Miller #include <net/protocol.h>
41db8dac20SDavid S. Miller #include <net/transp_v6.h>
42db8dac20SDavid S. Miller #include <net/ip6_route.h>
43db8dac20SDavid S. Miller #include <net/raw.h>
44222a011eSAndrew Lunn #include <net/seg6.h>
45db8dac20SDavid S. Miller #include <net/tcp_states.h>
46db8dac20SDavid S. Miller #include <net/ip6_checksum.h>
47e7cc0824SStefano Brivio #include <net/ip6_tunnel.h>
48*8139dccdSIvan Babrou #include <trace/events/udp.h>
49db8dac20SDavid S. Miller #include <net/xfrm.h>
500bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h>
5172289b96STom Herbert #include <net/inet6_hashtables.h>
52076bb0c8SEliezer Tamir #include <net/busy_poll.h>
53e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
54db8dac20SDavid S. Miller 
55db8dac20SDavid S. Miller #include <linux/proc_fs.h>
56db8dac20SDavid S. Miller #include <linux/seq_file.h>
5722911fc5SEric Dumazet #include <trace/events/skb.h>
58db8dac20SDavid S. Miller #include "udp_impl.h"
59db8dac20SDavid S. Miller 
60d38afeecSKuniyuki Iwashima static void udpv6_destruct_sock(struct sock *sk)
61d38afeecSKuniyuki Iwashima {
62d38afeecSKuniyuki Iwashima 	udp_destruct_common(sk);
63d38afeecSKuniyuki Iwashima 	inet6_sock_destruct(sk);
64d38afeecSKuniyuki Iwashima }
65d38afeecSKuniyuki Iwashima 
66d38afeecSKuniyuki Iwashima int udpv6_init_sock(struct sock *sk)
67d38afeecSKuniyuki Iwashima {
688a3854c7SPaolo Abeni 	udp_lib_init_sock(sk);
69d38afeecSKuniyuki Iwashima 	sk->sk_destruct = udpv6_destruct_sock;
708f279fb0SPavel Begunkov 	set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
71d38afeecSKuniyuki Iwashima 	return 0;
72d38afeecSKuniyuki Iwashima }
73d38afeecSKuniyuki Iwashima 
746eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net,
75b50026b5SHannes Frederic Sowa 			const struct in6_addr *laddr,
76b50026b5SHannes Frederic Sowa 			const u16 lport,
77b50026b5SHannes Frederic Sowa 			const struct in6_addr *faddr,
78b50026b5SHannes Frederic Sowa 			const __be16 fport)
79b50026b5SHannes Frederic Sowa {
801bbdceefSHannes Frederic Sowa 	static u32 udp6_ehash_secret __read_mostly;
811bbdceefSHannes Frederic Sowa 	static u32 udp_ipv6_hash_secret __read_mostly;
821bbdceefSHannes Frederic Sowa 
831bbdceefSHannes Frederic Sowa 	u32 lhash, fhash;
841bbdceefSHannes Frederic Sowa 
851bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp6_ehash_secret,
861bbdceefSHannes Frederic Sowa 			    sizeof(udp6_ehash_secret));
871bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ipv6_hash_secret,
881bbdceefSHannes Frederic Sowa 			    sizeof(udp_ipv6_hash_secret));
891bbdceefSHannes Frederic Sowa 
901bbdceefSHannes Frederic Sowa 	lhash = (__force u32)laddr->s6_addr32[3];
911bbdceefSHannes Frederic Sowa 	fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
921bbdceefSHannes Frederic Sowa 
93b50026b5SHannes Frederic Sowa 	return __inet6_ehashfn(lhash, lport, fhash, fport,
941bbdceefSHannes Frederic Sowa 			       udp_ipv6_hash_secret + net_hash_mix(net));
95b50026b5SHannes Frederic Sowa }
96b50026b5SHannes Frederic Sowa 
976ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
98db8dac20SDavid S. Miller {
9930fff923SEric Dumazet 	unsigned int hash2_nulladdr =
100f0b1e64cSMartin KaFai Lau 		ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
10130fff923SEric Dumazet 	unsigned int hash2_partial =
102f0b1e64cSMartin KaFai Lau 		ipv6_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;
106fe38d2a1SJosef Bacik 	return udp_lib_get_port(sk, snum, hash2_nulladdr);
107db8dac20SDavid S. Miller }
108db8dac20SDavid S. Miller 
109f7c46156SAlexey Kodanev void udp_v6_rehash(struct sock *sk)
110719f8358SEric Dumazet {
111f0b1e64cSMartin KaFai Lau 	u16 new_hash = ipv6_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,
12173545373STim Beale 			 int dif, int sdif)
122db8dac20SDavid S. Miller {
1234c971d2fSEric Dumazet 	int bound_dev_if, score;
12460c04aecSJoe Perches 	struct inet_sock *inet;
1256da5b0f0SMike Manning 	bool dev_match;
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 
13223b0269eSPeter Oskolkov 	if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
13323b0269eSPeter Oskolkov 		return -1;
13423b0269eSPeter Oskolkov 
135645ca708SEric Dumazet 	score = 0;
13660c04aecSJoe Perches 	inet = inet_sk(sk);
13760c04aecSJoe Perches 
138c720c7e8SEric Dumazet 	if (inet->inet_dport) {
139c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
140645ca708SEric Dumazet 			return -1;
141db8dac20SDavid S. Miller 		score++;
142db8dac20SDavid S. Miller 	}
14360c04aecSJoe Perches 
144efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
145efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
146645ca708SEric Dumazet 			return -1;
147db8dac20SDavid S. Miller 		score++;
148db8dac20SDavid S. Miller 	}
14960c04aecSJoe Perches 
1504c971d2fSEric Dumazet 	bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
1514c971d2fSEric Dumazet 	dev_match = udp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif);
15269678bcdSPaolo Abeni 	if (!dev_match)
153645ca708SEric Dumazet 		return -1;
1544c971d2fSEric Dumazet 	if (bound_dev_if)
155db8dac20SDavid S. Miller 		score++;
15660c04aecSJoe Perches 
1577170a977SEric Dumazet 	if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
15870da268bSEric Dumazet 		score++;
15970da268bSEric Dumazet 
160645ca708SEric Dumazet 	return score;
161645ca708SEric Dumazet }
162645ca708SEric Dumazet 
163a57066b1SDavid S. Miller static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
1642a08748cSJakub Sitnicki 				     struct sk_buff *skb,
1652a08748cSJakub Sitnicki 				     const struct in6_addr *saddr,
1662a08748cSJakub Sitnicki 				     __be16 sport,
1672a08748cSJakub Sitnicki 				     const struct in6_addr *daddr,
1682a08748cSJakub Sitnicki 				     unsigned int hnum)
1692a08748cSJakub Sitnicki {
1702a08748cSJakub Sitnicki 	struct sock *reuse_sk = NULL;
1712a08748cSJakub Sitnicki 	u32 hash;
1722a08748cSJakub Sitnicki 
1732a08748cSJakub Sitnicki 	if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
1742a08748cSJakub Sitnicki 		hash = udp6_ehashfn(net, daddr, hnum, saddr, sport);
1752a08748cSJakub Sitnicki 		reuse_sk = reuseport_select_sock(sk, hash, skb,
1762a08748cSJakub Sitnicki 						 sizeof(struct udphdr));
1772a08748cSJakub Sitnicki 	}
1782a08748cSJakub Sitnicki 	return reuse_sk;
1792a08748cSJakub Sitnicki }
1802a08748cSJakub Sitnicki 
181d1e37288SSu, Xuemin /* called with rcu_read_lock() */
182fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
183fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
1841801b570SDavid Ahern 		const struct in6_addr *daddr, unsigned int hnum,
18573545373STim Beale 		int dif, int sdif, struct udp_hslot *hslot2,
18673545373STim Beale 		struct sk_buff *skb)
187fddc17deSEric Dumazet {
188fddc17deSEric Dumazet 	struct sock *sk, *result;
189e94a62f5SPaolo Abeni 	int score, badness;
190fddc17deSEric Dumazet 
191fddc17deSEric Dumazet 	result = NULL;
192fddc17deSEric Dumazet 	badness = -1;
193ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
194d1e37288SSu, Xuemin 		score = compute_score(sk, net, saddr, sport,
19573545373STim Beale 				      daddr, hnum, dif, sdif);
196fddc17deSEric Dumazet 		if (score > badness) {
1972a08748cSJakub Sitnicki 			result = lookup_reuseport(net, sk, skb,
1982a08748cSJakub Sitnicki 						  saddr, sport, daddr, hnum);
199a57066b1SDavid S. Miller 			/* Fall back to scoring if group has connections */
20069421bf9SKuniyuki Iwashima 			if (result && !reuseport_has_conns(sk))
201ca065d0cSEric Dumazet 				return result;
2022a08748cSJakub Sitnicki 
203a57066b1SDavid S. Miller 			result = result ? : sk;
204ca065d0cSEric Dumazet 			badness = score;
205fddc17deSEric Dumazet 		}
206fddc17deSEric Dumazet 	}
207fddc17deSEric Dumazet 	return result;
208fddc17deSEric Dumazet }
209fddc17deSEric Dumazet 
2106d4201b1SJakub Sitnicki static inline struct sock *udp6_lookup_run_bpf(struct net *net,
2116d4201b1SJakub Sitnicki 					       struct udp_table *udptable,
2126d4201b1SJakub Sitnicki 					       struct sk_buff *skb,
2136d4201b1SJakub Sitnicki 					       const struct in6_addr *saddr,
2146d4201b1SJakub Sitnicki 					       __be16 sport,
2156d4201b1SJakub Sitnicki 					       const struct in6_addr *daddr,
216f8931565SMark Pashmfouroush 					       u16 hnum, const int dif)
2176d4201b1SJakub Sitnicki {
2186d4201b1SJakub Sitnicki 	struct sock *sk, *reuse_sk;
2196d4201b1SJakub Sitnicki 	bool no_reuseport;
2206d4201b1SJakub Sitnicki 
221ba6aac15SKuniyuki Iwashima 	if (udptable != net->ipv4.udp_table)
2226d4201b1SJakub Sitnicki 		return NULL; /* only UDP is supported */
2236d4201b1SJakub Sitnicki 
224f8931565SMark Pashmfouroush 	no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP, saddr, sport,
225f8931565SMark Pashmfouroush 					    daddr, hnum, dif, &sk);
2266d4201b1SJakub Sitnicki 	if (no_reuseport || IS_ERR_OR_NULL(sk))
2276d4201b1SJakub Sitnicki 		return sk;
2286d4201b1SJakub Sitnicki 
2296d4201b1SJakub Sitnicki 	reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
230c64c9c28SJakub Sitnicki 	if (reuse_sk)
2316d4201b1SJakub Sitnicki 		sk = reuse_sk;
2326d4201b1SJakub Sitnicki 	return sk;
2336d4201b1SJakub Sitnicki }
2346d4201b1SJakub Sitnicki 
235ca065d0cSEric Dumazet /* rcu_read_lock() must be held */
236fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
23788440ae7SBalazs Scheidler 			       const struct in6_addr *saddr, __be16 sport,
23888440ae7SBalazs Scheidler 			       const struct in6_addr *daddr, __be16 dport,
2391801b570SDavid Ahern 			       int dif, int sdif, struct udp_table *udptable,
240538950a1SCraig Gallek 			       struct sk_buff *skb)
241645ca708SEric Dumazet {
242645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
24323b0269eSPeter Oskolkov 	unsigned int hash2, slot2;
24423b0269eSPeter Oskolkov 	struct udp_hslot *hslot2;
2456d4201b1SJakub Sitnicki 	struct sock *result, *sk;
246645ca708SEric Dumazet 
247f0b1e64cSMartin KaFai Lau 	hash2 = ipv6_portaddr_hash(net, daddr, hnum);
248fddc17deSEric Dumazet 	slot2 = hash2 & udptable->mask;
249fddc17deSEric Dumazet 	hslot2 = &udptable->hash2[slot2];
250fddc17deSEric Dumazet 
2516d4201b1SJakub Sitnicki 	/* Lookup connected or non-wildcard sockets */
252fddc17deSEric Dumazet 	result = udp6_lib_lookup2(net, saddr, sport,
25373545373STim Beale 				  daddr, hnum, dif, sdif,
254d1e37288SSu, Xuemin 				  hslot2, skb);
2556d4201b1SJakub Sitnicki 	if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
2566d4201b1SJakub Sitnicki 		goto done;
2576d4201b1SJakub Sitnicki 
2586d4201b1SJakub Sitnicki 	/* Lookup redirect from BPF */
2596d4201b1SJakub Sitnicki 	if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
2606d4201b1SJakub Sitnicki 		sk = udp6_lookup_run_bpf(net, udptable, skb,
261f8931565SMark Pashmfouroush 					 saddr, sport, daddr, hnum, dif);
2626d4201b1SJakub Sitnicki 		if (sk) {
2636d4201b1SJakub Sitnicki 			result = sk;
2646d4201b1SJakub Sitnicki 			goto done;
2656d4201b1SJakub Sitnicki 		}
2666d4201b1SJakub Sitnicki 	}
2676d4201b1SJakub Sitnicki 
2686d4201b1SJakub Sitnicki 	/* Got non-wildcard socket or error on first lookup */
2696d4201b1SJakub Sitnicki 	if (result)
2706d4201b1SJakub Sitnicki 		goto done;
2716d4201b1SJakub Sitnicki 
2726d4201b1SJakub Sitnicki 	/* Lookup wildcard sockets */
273f0b1e64cSMartin KaFai Lau 	hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
274fddc17deSEric Dumazet 	slot2 = hash2 & udptable->mask;
275fddc17deSEric Dumazet 	hslot2 = &udptable->hash2[slot2];
276fddc17deSEric Dumazet 
2771223c67cSJorge Boncompte [DTI2] 	result = udp6_lib_lookup2(net, saddr, sport,
27823b0269eSPeter Oskolkov 				  &in6addr_any, hnum, dif, sdif,
27973545373STim Beale 				  hslot2, skb);
2806d4201b1SJakub Sitnicki done:
28126f8113cSEnrico Weigelt 	if (IS_ERR(result))
2828217ca65SMartin KaFai Lau 		return NULL;
283fddc17deSEric Dumazet 	return result;
284fddc17deSEric Dumazet }
285fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
286db8dac20SDavid S. Miller 
287607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
288607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
289645ca708SEric Dumazet 					  struct udp_table *udptable)
290607c4aafSKOVACS Krisztian {
291b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
292607c4aafSKOVACS Krisztian 
293ed7cbbceSAlexander Duyck 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
294607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
2951801b570SDavid Ahern 				 inet6_sdif(skb), udptable, skb);
296607c4aafSKOVACS Krisztian }
297607c4aafSKOVACS Krisztian 
2987b58e63eSEric Dumazet struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
29963058308STom Herbert 				 __be16 sport, __be16 dport)
30063058308STom Herbert {
30163058308STom Herbert 	const struct ipv6hdr *iph = ipv6_hdr(skb);
302ba6aac15SKuniyuki Iwashima 	struct net *net = dev_net(skb->dev);
30363058308STom Herbert 
304ba6aac15SKuniyuki Iwashima 	return __udp6_lib_lookup(net, &iph->saddr, sport,
30563058308STom Herbert 				 &iph->daddr, dport, inet6_iif(skb),
306ba6aac15SKuniyuki Iwashima 				 inet6_sdif(skb), net->ipv4.udp_table, NULL);
30763058308STom Herbert }
30863058308STom Herbert 
309ca065d0cSEric Dumazet /* Must be called under rcu_read_lock().
310ca065d0cSEric Dumazet  * Does increment socket refcount.
311ca065d0cSEric Dumazet  */
3126e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
313aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
314aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
315aa976fc0SBalazs Scheidler {
316ca065d0cSEric Dumazet 	struct sock *sk;
317ca065d0cSEric Dumazet 
318ca065d0cSEric Dumazet 	sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
319ba6aac15SKuniyuki Iwashima 				dif, 0, net->ipv4.udp_table, NULL);
32041c6d650SReshetova, Elena 	if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
321ca065d0cSEric Dumazet 		sk = NULL;
322ca065d0cSEric Dumazet 	return sk;
323aa976fc0SBalazs Scheidler }
324aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
325ca065d0cSEric Dumazet #endif
326aa976fc0SBalazs Scheidler 
327cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the
328cb891fa6SPaolo Abeni  * scratch area space; note that the IP6CB flags is still in the first
329cb891fa6SPaolo Abeni  * cacheline, so checking for jumbograms is cheap
330cb891fa6SPaolo Abeni  */
331cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb)
332cb891fa6SPaolo Abeni {
333cb891fa6SPaolo Abeni 	return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
334cb891fa6SPaolo Abeni }
335cb891fa6SPaolo Abeni 
336db8dac20SDavid S. Miller /*
337db8dac20SDavid S. Miller  *	This should be easy, if there is something there we
338db8dac20SDavid S. Miller  *	return it, otherwise we block.
339db8dac20SDavid S. Miller  */
340db8dac20SDavid S. Miller 
3411b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
342ec095263SOliver Hartkopp 		  int flags, int *addr_len)
343db8dac20SDavid S. Miller {
344db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
345db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
346db8dac20SDavid S. Miller 	struct sk_buff *skb;
34759c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
348fd69c399SPaolo Abeni 	int off, err, peeking = flags & MSG_PEEK;
349db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
350543fc3fbSPaolo Abeni 	struct udp_mib __percpu *mib;
351197c949eSEric Dumazet 	bool checksum_valid = false;
352f26ba175SWei Yongjun 	int is_udp4;
353db8dac20SDavid S. Miller 
354db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
35585fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
356db8dac20SDavid S. Miller 
3574b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
35885fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
3594b340ae2SBrian Haley 
360db8dac20SDavid S. Miller try_again:
361a0917e0bSMatthew Dawson 	off = sk_peek_offset(sk, flags);
362ec095263SOliver Hartkopp 	skb = __skb_recv_udp(sk, flags, &off, &err);
363db8dac20SDavid S. Miller 	if (!skb)
364627d2d6bSsamanthakumar 		return err;
365db8dac20SDavid S. Miller 
366cb891fa6SPaolo Abeni 	ulen = udp6_skb_len(skb);
36759c2cdaeSDavid S. Miller 	copied = len;
368627d2d6bSsamanthakumar 	if (copied > ulen - off)
369627d2d6bSsamanthakumar 		copied = ulen - off;
37059c2cdaeSDavid S. Miller 	else if (copied < ulen)
371db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
372db8dac20SDavid S. Miller 
373f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
374029a3743SPaolo Abeni 	mib = __UDPX_MIB(sk, is_udp4);
375f26ba175SWei Yongjun 
376db8dac20SDavid S. Miller 	/*
377db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
378db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
379db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
380db8dac20SDavid S. Miller 	 */
381db8dac20SDavid S. Miller 
382d21dbdfeSEric Dumazet 	if (copied < ulen || peeking ||
383d21dbdfeSEric Dumazet 	    (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
38467a51780SPaolo Abeni 		checksum_valid = udp_skb_csum_unnecessary(skb) ||
38567a51780SPaolo Abeni 				!__udp_lib_checksum_complete(skb);
386197c949eSEric Dumazet 		if (!checksum_valid)
387db8dac20SDavid S. Miller 			goto csum_copy_err;
388db8dac20SDavid S. Miller 	}
389db8dac20SDavid S. Miller 
39067a51780SPaolo Abeni 	if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
39167a51780SPaolo Abeni 		if (udp_skb_is_linear(skb))
39267a51780SPaolo Abeni 			err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
39367a51780SPaolo Abeni 		else
394627d2d6bSsamanthakumar 			err = skb_copy_datagram_msg(skb, off, msg, copied);
39567a51780SPaolo Abeni 	} else {
396627d2d6bSsamanthakumar 		err = skb_copy_and_csum_datagram_msg(skb, off, msg);
397db8dac20SDavid S. Miller 		if (err == -EINVAL)
398db8dac20SDavid S. Miller 			goto csum_copy_err;
399db8dac20SDavid S. Miller 	}
40022911fc5SEric Dumazet 	if (unlikely(err)) {
401fd69c399SPaolo Abeni 		if (!peeking) {
402979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
403029a3743SPaolo Abeni 			SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
404979402b1SEric Dumazet 		}
405850cbaddSPaolo Abeni 		kfree_skb(skb);
406627d2d6bSsamanthakumar 		return err;
40722911fc5SEric Dumazet 	}
408fd69c399SPaolo Abeni 	if (!peeking)
409029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
410db8dac20SDavid S. Miller 
4116fd1d51cSErin MacNeil 	sock_recv_cmsgs(msg, sk, skb);
412db8dac20SDavid S. Miller 
413db8dac20SDavid S. Miller 	/* Copy the address. */
414db8dac20SDavid S. Miller 	if (msg->msg_name) {
415342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
416db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
417db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
418db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
419db8dac20SDavid S. Miller 
420842df073SHannes Frederic Sowa 		if (is_udp4) {
421b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
422b301e82cSBrian Haley 					       &sin6->sin6_addr);
423842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
424842df073SHannes Frederic Sowa 		} else {
4254e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
426842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
427842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
4284330487aSDuan Jiong 						    inet6_iif(skb));
429db8dac20SDavid S. Miller 		}
430bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
431983695faSDaniel Borkmann 
432983695faSDaniel Borkmann 		BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
433983695faSDaniel Borkmann 						      (struct sockaddr *)sin6);
434db8dac20SDavid S. Miller 	}
4354b261c75SHannes Frederic Sowa 
436bcd1665eSPaolo Abeni 	if (udp_sk(sk)->gro_enabled)
437bcd1665eSPaolo Abeni 		udp_cmsg_recv(msg, sk, skb);
438bcd1665eSPaolo Abeni 
4394b261c75SHannes Frederic Sowa 	if (np->rxopt.all)
4404b261c75SHannes Frederic Sowa 		ip6_datagram_recv_common_ctl(sk, msg, skb);
4414b261c75SHannes Frederic Sowa 
442f26ba175SWei Yongjun 	if (is_udp4) {
443db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
444ad959036SPaolo Abeni 			ip_cmsg_recv_offset(msg, sk, skb,
44510df8e61SEric Dumazet 					    sizeof(struct udphdr), off);
446db8dac20SDavid S. Miller 	} else {
447db8dac20SDavid S. Miller 		if (np->rxopt.all)
4484b261c75SHannes Frederic Sowa 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
449db8dac20SDavid S. Miller 	}
450db8dac20SDavid S. Miller 
45159c2cdaeSDavid S. Miller 	err = copied;
452db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
453db8dac20SDavid S. Miller 		err = ulen;
454db8dac20SDavid S. Miller 
455850cbaddSPaolo Abeni 	skb_consume_udp(sk, skb, peeking ? -err : err);
456db8dac20SDavid S. Miller 	return err;
457db8dac20SDavid S. Miller 
458db8dac20SDavid S. Miller csum_copy_err:
4592276f58aSPaolo Abeni 	if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
4602276f58aSPaolo Abeni 				 udp_skb_destructor)) {
461029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
462029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
4636a5dc9e5SEric Dumazet 	}
464850cbaddSPaolo Abeni 	kfree_skb(skb);
465db8dac20SDavid S. Miller 
466beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
467beb39db5SEric Dumazet 	cond_resched();
4689cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
469db8dac20SDavid S. Miller 	goto try_again;
470db8dac20SDavid S. Miller }
471db8dac20SDavid S. Miller 
472a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
473a36e185eSStefano Brivio void udpv6_encap_enable(void)
474a36e185eSStefano Brivio {
4759c480601SPaolo Abeni 	static_branch_inc(&udpv6_encap_needed_key);
476a36e185eSStefano Brivio }
477a36e185eSStefano Brivio EXPORT_SYMBOL(udpv6_encap_enable);
478a36e185eSStefano Brivio 
479e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
480e7cc0824SStefano Brivio  * through error handlers in encapsulations looking for a match.
481e7cc0824SStefano Brivio  */
482e7cc0824SStefano Brivio static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
483e7cc0824SStefano Brivio 				      struct inet6_skb_parm *opt,
484424a7cd0SPaolo Abeni 				      u8 type, u8 code, int offset, __be32 info)
485e7cc0824SStefano Brivio {
486e7cc0824SStefano Brivio 	int i;
487e7cc0824SStefano Brivio 
488e7cc0824SStefano Brivio 	for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
489e7cc0824SStefano Brivio 		int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
490424a7cd0SPaolo Abeni 			       u8 type, u8 code, int offset, __be32 info);
491424a7cd0SPaolo Abeni 		const struct ip6_tnl_encap_ops *encap;
492e7cc0824SStefano Brivio 
493424a7cd0SPaolo Abeni 		encap = rcu_dereference(ip6tun_encaps[i]);
494424a7cd0SPaolo Abeni 		if (!encap)
495e7cc0824SStefano Brivio 			continue;
496424a7cd0SPaolo Abeni 		handler = encap->err_handler;
497e7cc0824SStefano Brivio 		if (handler && !handler(skb, opt, type, code, offset, info))
498e7cc0824SStefano Brivio 			return 0;
499e7cc0824SStefano Brivio 	}
500e7cc0824SStefano Brivio 
501e7cc0824SStefano Brivio 	return -ENOENT;
502e7cc0824SStefano Brivio }
503e7cc0824SStefano Brivio 
504a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without
505a36e185eSStefano Brivio  * reversing source and destination port: this will match tunnels that force the
506a36e185eSStefano Brivio  * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
507a36e185eSStefano Brivio  * lwtunnels might actually break this assumption by being configured with
508a36e185eSStefano Brivio  * different destination ports on endpoints, in this case we won't be able to
509a36e185eSStefano Brivio  * trace ICMP messages back to them.
510a36e185eSStefano Brivio  *
511e7cc0824SStefano Brivio  * If this doesn't match any socket, probe tunnels with arbitrary destination
512e7cc0824SStefano Brivio  * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
513e7cc0824SStefano Brivio  * we've sent packets to won't necessarily match the local destination port.
514e7cc0824SStefano Brivio  *
515a36e185eSStefano Brivio  * Then ask the tunnel implementation to match the error against a valid
516a36e185eSStefano Brivio  * association.
517a36e185eSStefano Brivio  *
518e7cc0824SStefano Brivio  * Return an error if we can't find a match, the socket if we need further
519e7cc0824SStefano Brivio  * processing, zero otherwise.
520a36e185eSStefano Brivio  */
521a36e185eSStefano Brivio static struct sock *__udp6_lib_err_encap(struct net *net,
522a36e185eSStefano Brivio 					 const struct ipv6hdr *hdr, int offset,
523a36e185eSStefano Brivio 					 struct udphdr *uh,
524a36e185eSStefano Brivio 					 struct udp_table *udptable,
5259bfce73cSVadim Fedorenko 					 struct sock *sk,
526e7cc0824SStefano Brivio 					 struct sk_buff *skb,
527e7cc0824SStefano Brivio 					 struct inet6_skb_parm *opt,
528e7cc0824SStefano Brivio 					 u8 type, u8 code, __be32 info)
529a36e185eSStefano Brivio {
5309bfce73cSVadim Fedorenko 	int (*lookup)(struct sock *sk, struct sk_buff *skb);
531a36e185eSStefano Brivio 	int network_offset, transport_offset;
5329bfce73cSVadim Fedorenko 	struct udp_sock *up;
533a36e185eSStefano Brivio 
534a36e185eSStefano Brivio 	network_offset = skb_network_offset(skb);
535a36e185eSStefano Brivio 	transport_offset = skb_transport_offset(skb);
536a36e185eSStefano Brivio 
537a36e185eSStefano Brivio 	/* Network header needs to point to the outer IPv6 header inside ICMP */
538a36e185eSStefano Brivio 	skb_reset_network_header(skb);
539a36e185eSStefano Brivio 
540a36e185eSStefano Brivio 	/* Transport header needs to point to the UDP header */
541a36e185eSStefano Brivio 	skb_set_transport_header(skb, offset);
542a36e185eSStefano Brivio 
5439bfce73cSVadim Fedorenko 	if (sk) {
5449bfce73cSVadim Fedorenko 		up = udp_sk(sk);
5459bfce73cSVadim Fedorenko 
5469bfce73cSVadim Fedorenko 		lookup = READ_ONCE(up->encap_err_lookup);
5479bfce73cSVadim Fedorenko 		if (lookup && lookup(sk, skb))
5489bfce73cSVadim Fedorenko 			sk = NULL;
5499bfce73cSVadim Fedorenko 
5509bfce73cSVadim Fedorenko 		goto out;
5519bfce73cSVadim Fedorenko 	}
5529bfce73cSVadim Fedorenko 
553e7cc0824SStefano Brivio 	sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
554e7cc0824SStefano Brivio 			       &hdr->saddr, uh->dest,
555e7cc0824SStefano Brivio 			       inet6_iif(skb), 0, udptable, skb);
556e7cc0824SStefano Brivio 	if (sk) {
5579bfce73cSVadim Fedorenko 		up = udp_sk(sk);
558e7cc0824SStefano Brivio 
559a36e185eSStefano Brivio 		lookup = READ_ONCE(up->encap_err_lookup);
560a36e185eSStefano Brivio 		if (!lookup || lookup(sk, skb))
561a36e185eSStefano Brivio 			sk = NULL;
562e7cc0824SStefano Brivio 	}
563e7cc0824SStefano Brivio 
5649bfce73cSVadim Fedorenko out:
565e7cc0824SStefano Brivio 	if (!sk) {
566e7cc0824SStefano Brivio 		sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
567e7cc0824SStefano Brivio 							offset, info));
568e7cc0824SStefano Brivio 	}
569a36e185eSStefano Brivio 
570a36e185eSStefano Brivio 	skb_set_transport_header(skb, transport_offset);
571a36e185eSStefano Brivio 	skb_set_network_header(skb, network_offset);
572e7cc0824SStefano Brivio 
573a36e185eSStefano Brivio 	return sk;
574a36e185eSStefano Brivio }
575a36e185eSStefano Brivio 
57632bbd879SStefano Brivio int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
577d5fdd6baSBrian Haley 		   u8 type, u8 code, int offset, __be32 info,
578645ca708SEric Dumazet 		   struct udp_table *udptable)
579db8dac20SDavid S. Miller {
580db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
581b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
582b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
583222a011eSAndrew Lunn 	const struct in6_addr *daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr;
584db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
585a36e185eSStefano Brivio 	bool tunnel = false;
586db8dac20SDavid S. Miller 	struct sock *sk;
587e0d8c1b7SWei Wang 	int harderr;
588db8dac20SDavid S. Miller 	int err;
5897304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
590db8dac20SDavid S. Miller 
591e32ea7e7SCraig Gallek 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
5924ac30c4bSMartin KaFai Lau 			       inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
5939bfce73cSVadim Fedorenko 
594d26796aeSXin Long 	if (!sk || udp_sk(sk)->encap_type) {
595a36e185eSStefano Brivio 		/* No socket for error: try tunnels before discarding */
596a36e185eSStefano Brivio 		if (static_branch_unlikely(&udpv6_encap_needed_key)) {
597a36e185eSStefano Brivio 			sk = __udp6_lib_err_encap(net, hdr, offset, uh,
5989bfce73cSVadim Fedorenko 						  udptable, sk, skb,
599e7cc0824SStefano Brivio 						  opt, type, code, info);
600e7cc0824SStefano Brivio 			if (!sk)
601e7cc0824SStefano Brivio 				return 0;
6029bfce73cSVadim Fedorenko 		} else
6039bfce73cSVadim Fedorenko 			sk = ERR_PTR(-ENOENT);
604a36e185eSStefano Brivio 
605e7cc0824SStefano Brivio 		if (IS_ERR(sk)) {
606a16292a0SEric Dumazet 			__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
6077304fe46SDuan Jiong 					  ICMP6_MIB_INERRORS);
608e7cc0824SStefano Brivio 			return PTR_ERR(sk);
6097304fe46SDuan Jiong 		}
610e7cc0824SStefano Brivio 
611a36e185eSStefano Brivio 		tunnel = true;
612a36e185eSStefano Brivio 	}
613db8dac20SDavid S. Miller 
614e0d8c1b7SWei Wang 	harderr = icmpv6_err_convert(type, code, &err);
615e0d8c1b7SWei Wang 	np = inet6_sk(sk);
616e0d8c1b7SWei Wang 
61793b36cf3SHannes Frederic Sowa 	if (type == ICMPV6_PKT_TOOBIG) {
61893b36cf3SHannes Frederic Sowa 		if (!ip6_sk_accept_pmtu(sk))
61993b36cf3SHannes Frederic Sowa 			goto out;
62081aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
621e0d8c1b7SWei Wang 		if (np->pmtudisc != IPV6_PMTUDISC_DONT)
622e0d8c1b7SWei Wang 			harderr = 1;
62393b36cf3SHannes Frederic Sowa 	}
6241a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
625a36e185eSStefano Brivio 		if (tunnel) {
626a36e185eSStefano Brivio 			ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
627a36e185eSStefano Brivio 				     sk->sk_mark, sk->sk_uid);
628a36e185eSStefano Brivio 		} else {
629ec18d9a2SDavid S. Miller 			ip6_sk_redirect(skb, sk);
630a36e185eSStefano Brivio 		}
6311a462d18SDuan Jiong 		goto out;
6321a462d18SDuan Jiong 	}
63381aded24SDavid S. Miller 
634a36e185eSStefano Brivio 	/* Tunnels don't have an application socket: don't pass errors back */
635ac56a0b4SDavid Howells 	if (tunnel) {
636ac56a0b4SDavid Howells 		if (udp_sk(sk)->encap_err_rcv)
63742fb06b3SDavid Howells 			udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest,
63842fb06b3SDavid Howells 						  ntohl(info), (u8 *)(uh+1));
639a36e185eSStefano Brivio 		goto out;
640ac56a0b4SDavid Howells 	}
641a36e185eSStefano Brivio 
642e0d8c1b7SWei Wang 	if (!np->recverr) {
643e0d8c1b7SWei Wang 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
644db8dac20SDavid S. Miller 			goto out;
645e0d8c1b7SWei Wang 	} else {
646db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
647e0d8c1b7SWei Wang 	}
648b1faf566SEric Dumazet 
649db8dac20SDavid S. Miller 	sk->sk_err = err;
650e3ae2365SAlexander Aring 	sk_error_report(sk);
651db8dac20SDavid S. Miller out:
65232bbd879SStefano Brivio 	return 0;
653db8dac20SDavid S. Miller }
654db8dac20SDavid S. Miller 
655a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
656f7ad74feSBenjamin LaHaise {
657f7ad74feSBenjamin LaHaise 	int rc;
658f7ad74feSBenjamin LaHaise 
659efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
660f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
661005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
6622c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
663e68b6e50SEric Dumazet 	} else {
664e68b6e50SEric Dumazet 		sk_mark_napi_id_once(sk, skb);
665005ec974SShawn Bohrer 	}
666f7ad74feSBenjamin LaHaise 
667850cbaddSPaolo Abeni 	rc = __udp_enqueue_schedule_skb(sk, skb);
668f7ad74feSBenjamin LaHaise 	if (rc < 0) {
669f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
6700d92efdeSDonald Hunter 		enum skb_drop_reason drop_reason;
671f7ad74feSBenjamin LaHaise 
672f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
6730d92efdeSDonald Hunter 		if (rc == -ENOMEM) {
674e61da9e2SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
675f7ad74feSBenjamin LaHaise 					 UDP_MIB_RCVBUFERRORS, is_udplite);
6760d92efdeSDonald Hunter 			drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
6770d92efdeSDonald Hunter 		} else {
678a3ce2b10SMenglong Dong 			UDP6_INC_STATS(sock_net(sk),
679a3ce2b10SMenglong Dong 				       UDP_MIB_MEMERRORS, is_udplite);
6800d92efdeSDonald Hunter 			drop_reason = SKB_DROP_REASON_PROTO_MEM;
6810d92efdeSDonald Hunter 		}
682e61da9e2SEric Dumazet 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
6830d92efdeSDonald Hunter 		kfree_skb_reason(skb, drop_reason);
684*8139dccdSIvan Babrou 		trace_udp_fail_queue_rcv_skb(rc, sk);
685f7ad74feSBenjamin LaHaise 		return -1;
686f7ad74feSBenjamin LaHaise 	}
687850cbaddSPaolo Abeni 
688f7ad74feSBenjamin LaHaise 	return 0;
689f7ad74feSBenjamin LaHaise }
690f7ad74feSBenjamin LaHaise 
69132bbd879SStefano Brivio static __inline__ int udpv6_err(struct sk_buff *skb,
692d5fdd6baSBrian Haley 				struct inet6_skb_parm *opt, u8 type,
693d5fdd6baSBrian Haley 				u8 code, int offset, __be32 info)
694db8dac20SDavid S. Miller {
695ba6aac15SKuniyuki Iwashima 	return __udp6_lib_err(skb, opt, type, code, offset, info,
696ba6aac15SKuniyuki Iwashima 			      dev_net(skb->dev)->ipv4.udp_table);
697db8dac20SDavid S. Miller }
698db8dac20SDavid S. Miller 
699cf329aa4SPaolo Abeni static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
700db8dac20SDavid S. Miller {
7010d92efdeSDonald Hunter 	enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
702db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
703db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
704db8dac20SDavid S. Miller 
7050d92efdeSDonald Hunter 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
7060d92efdeSDonald Hunter 		drop_reason = SKB_DROP_REASON_XFRM_POLICY;
707db8dac20SDavid S. Miller 		goto drop;
7080d92efdeSDonald Hunter 	}
709b0e214d2SMadhu Koriginja 	nf_reset_ct(skb);
710db8dac20SDavid S. Miller 
71188ab3108SDavidlohr Bueso 	if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
712d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
713d7f3f621SBenjamin LaHaise 
714d7f3f621SBenjamin LaHaise 		/*
715d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
716d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
717d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
718d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
719d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
720d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
721d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
722d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
723d7f3f621SBenjamin LaHaise 		 */
724d7f3f621SBenjamin LaHaise 
725d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
7266aa7de05SMark Rutland 		encap_rcv = READ_ONCE(up->encap_rcv);
727e5aed006SHannes Frederic Sowa 		if (encap_rcv) {
728d7f3f621SBenjamin LaHaise 			int ret;
729d7f3f621SBenjamin LaHaise 
7300a80966bSTom Herbert 			/* Verify checksum before giving to encap */
7310a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
7320a80966bSTom Herbert 				goto csum_error;
7330a80966bSTom Herbert 
734d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
735d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
736250962e4SMenglong Dong 				__UDP6_INC_STATS(sock_net(sk),
737d7f3f621SBenjamin LaHaise 						 UDP_MIB_INDATAGRAMS,
738d7f3f621SBenjamin LaHaise 						 is_udplite);
739d7f3f621SBenjamin LaHaise 				return -ret;
740d7f3f621SBenjamin LaHaise 			}
741d7f3f621SBenjamin LaHaise 		}
742d7f3f621SBenjamin LaHaise 
743d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
744d7f3f621SBenjamin LaHaise 	}
745d7f3f621SBenjamin LaHaise 
746db8dac20SDavid S. Miller 	/*
747db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
748db8dac20SDavid S. Miller 	 */
749b0a42277SMiaohe Lin 	if ((up->pcflag & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
750db8dac20SDavid S. Miller 
751db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
752ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
753db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
754db8dac20SDavid S. Miller 			goto drop;
755db8dac20SDavid S. Miller 		}
756db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
757ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
758db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
759db8dac20SDavid S. Miller 			goto drop;
760db8dac20SDavid S. Miller 		}
761db8dac20SDavid S. Miller 	}
762db8dac20SDavid S. Miller 
7634b943faeSPaolo Abeni 	prefetch(&sk->sk_rmem_alloc);
764ce25d66aSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
765ce25d66aSEric Dumazet 	    udp_lib_checksum_complete(skb))
7666a5dc9e5SEric Dumazet 		goto csum_error;
767ce25d66aSEric Dumazet 
7680d92efdeSDonald Hunter 	if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) {
7690d92efdeSDonald Hunter 		drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
770a6127697SMichal Kubeček 		goto drop;
7710d92efdeSDonald Hunter 	}
772db8dac20SDavid S. Miller 
773e6afc8acSsamanthakumar 	udp_csum_pull_header(skb);
774cb80ef46SBenjamin LaHaise 
775d826eb14SEric Dumazet 	skb_dst_drop(skb);
776db8dac20SDavid S. Miller 
777850cbaddSPaolo Abeni 	return __udpv6_queue_rcv_skb(sk, skb);
7783e215c8dSJames M Leddy 
7796a5dc9e5SEric Dumazet csum_error:
7800d92efdeSDonald Hunter 	drop_reason = SKB_DROP_REASON_UDP_CSUM;
78102c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
782db8dac20SDavid S. Miller drop:
78302c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
784cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
7850d92efdeSDonald Hunter 	kfree_skb_reason(skb, drop_reason);
786db8dac20SDavid S. Miller 	return -1;
787db8dac20SDavid S. Miller }
788db8dac20SDavid S. Miller 
789cf329aa4SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
790cf329aa4SPaolo Abeni {
791cf329aa4SPaolo Abeni 	struct sk_buff *next, *segs;
792cf329aa4SPaolo Abeni 	int ret;
793cf329aa4SPaolo Abeni 
794cf329aa4SPaolo Abeni 	if (likely(!udp_unexpected_gso(sk, skb)))
795cf329aa4SPaolo Abeni 		return udpv6_queue_rcv_one_skb(sk, skb);
796cf329aa4SPaolo Abeni 
797cf329aa4SPaolo Abeni 	__skb_push(skb, -skb_mac_offset(skb));
798cf329aa4SPaolo Abeni 	segs = udp_rcv_segment(sk, skb, false);
7991a186c14SJason A. Donenfeld 	skb_list_walk_safe(segs, skb, next) {
800cf329aa4SPaolo Abeni 		__skb_pull(skb, skb_transport_offset(skb));
801cf329aa4SPaolo Abeni 
802000ac44dSPaolo Abeni 		udp_post_segment_fix_csum(skb);
803cf329aa4SPaolo Abeni 		ret = udpv6_queue_rcv_one_skb(sk, skb);
804cf329aa4SPaolo Abeni 		if (ret > 0)
805cf329aa4SPaolo Abeni 			ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
806cf329aa4SPaolo Abeni 						 true);
807cf329aa4SPaolo Abeni 	}
808cf329aa4SPaolo Abeni 	return 0;
809cf329aa4SPaolo Abeni }
810cf329aa4SPaolo Abeni 
811dc3731baSEric Dumazet static bool __udp_v6_is_mcast_sock(struct net *net, const struct sock *sk,
812b71d1d42SEric Dumazet 				   __be16 loc_port, const struct in6_addr *loc_addr,
813b71d1d42SEric Dumazet 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
8147bd2db40SDewi Morgan 				   int dif, int sdif, unsigned short hnum)
815db8dac20SDavid S. Miller {
816dc3731baSEric Dumazet 	const struct inet_sock *inet = inet_sk(sk);
817db8dac20SDavid S. Miller 
8189e89fd8bSSven Wegener 	if (!net_eq(sock_net(sk), net))
8195cf3d461SDavid Held 		return false;
820b8ad0cbcSDaniel Lezcano 
8215cf3d461SDavid Held 	if (udp_sk(sk)->udp_port_hash != hnum ||
8225cf3d461SDavid Held 	    sk->sk_family != PF_INET6 ||
8235cf3d461SDavid Held 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
8245cf3d461SDavid Held 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
8255cf3d461SDavid Held 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
8264c971d2fSEric Dumazet 	    !udp_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, sdif) ||
82733b4b015SHenning Rogge 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
82833b4b015SHenning Rogge 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
8295cf3d461SDavid Held 		return false;
8309e89fd8bSSven Wegener 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
8315cf3d461SDavid Held 		return false;
8325cf3d461SDavid Held 	return true;
833db8dac20SDavid S. Miller }
834db8dac20SDavid S. Miller 
8354068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
8364068579eSTom Herbert {
8374068579eSTom Herbert 	/* RFC 2460 section 8.1 says that we SHOULD log
8384068579eSTom Herbert 	 * this error. Well, it is reasonable.
8394068579eSTom Herbert 	 */
840ba7a46f1SJoe Perches 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
8414068579eSTom Herbert 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
8424068579eSTom Herbert 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
8434068579eSTom Herbert }
8444068579eSTom Herbert 
845db8dac20SDavid S. Miller /*
846db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
847db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
848db8dac20SDavid S. Miller  */
849e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
850b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
85136cbb245SRick Jones 		struct udp_table *udptable, int proto)
852db8dac20SDavid S. Miller {
853ca065d0cSEric Dumazet 	struct sock *sk, *first = NULL;
854db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
8555cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
8565cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
857ca065d0cSEric Dumazet 	unsigned int offset = offsetof(typeof(*sk), sk_node);
8582dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
859ca065d0cSEric Dumazet 	int dif = inet6_iif(skb);
8607bd2db40SDewi Morgan 	int sdif = inet6_sdif(skb);
861ca065d0cSEric Dumazet 	struct hlist_node *node;
862ca065d0cSEric Dumazet 	struct sk_buff *nskb;
8632dc41cffSDavid Held 
8642dc41cffSDavid Held 	if (use_hash2) {
865f0b1e64cSMartin KaFai Lau 		hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
86673e2d5e3SPablo Neira 			    udptable->mask;
867f0b1e64cSMartin KaFai Lau 		hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
8682dc41cffSDavid Held start_lookup:
86973e2d5e3SPablo Neira 		hslot = &udptable->hash2[hash2];
8702dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
8712dc41cffSDavid Held 	}
872db8dac20SDavid S. Miller 
873ca065d0cSEric Dumazet 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
874ca065d0cSEric Dumazet 		if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
8757bd2db40SDewi Morgan 					    uh->source, saddr, dif, sdif,
8767bd2db40SDewi Morgan 					    hnum))
877ca065d0cSEric Dumazet 			continue;
8781c19448cSTom Herbert 		/* If zero checksum and no_check is not on for
8794068579eSTom Herbert 		 * the socket then skip it.
8804068579eSTom Herbert 		 */
881ca065d0cSEric Dumazet 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
882ca065d0cSEric Dumazet 			continue;
883ca065d0cSEric Dumazet 		if (!first) {
884ca065d0cSEric Dumazet 			first = sk;
885ca065d0cSEric Dumazet 			continue;
886db8dac20SDavid S. Miller 		}
887ca065d0cSEric Dumazet 		nskb = skb_clone(skb, GFP_ATOMIC);
888ca065d0cSEric Dumazet 		if (unlikely(!nskb)) {
889ca065d0cSEric Dumazet 			atomic_inc(&sk->sk_drops);
89002c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
891ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
89202c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_INERRORS,
893ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
894ca065d0cSEric Dumazet 			continue;
895a1ab77f9SEric Dumazet 		}
896db8dac20SDavid S. Miller 
897ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(sk, nskb) > 0)
898ca065d0cSEric Dumazet 			consume_skb(nskb);
899ca065d0cSEric Dumazet 	}
900a1ab77f9SEric Dumazet 
9012dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
9022dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
9032dc41cffSDavid Held 		hash2 = hash2_any;
9042dc41cffSDavid Held 		goto start_lookup;
9052dc41cffSDavid Held 	}
9062dc41cffSDavid Held 
907ca065d0cSEric Dumazet 	if (first) {
908ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(first, skb) > 0)
909ca065d0cSEric Dumazet 			consume_skb(skb);
910a1ab77f9SEric Dumazet 	} else {
911ca065d0cSEric Dumazet 		kfree_skb(skb);
91202c22347SEric Dumazet 		__UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
91336cbb245SRick Jones 				 proto == IPPROTO_UDPLITE);
914a1ab77f9SEric Dumazet 	}
915db8dac20SDavid S. Miller 	return 0;
916db8dac20SDavid S. Miller }
917db8dac20SDavid S. Miller 
91864f0f5d1SPaolo Abeni static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
91964f0f5d1SPaolo Abeni {
92064f0f5d1SPaolo Abeni 	if (udp_sk_rx_dst_set(sk, dst)) {
92164f0f5d1SPaolo Abeni 		const struct rt6_info *rt = (const struct rt6_info *)dst;
92264f0f5d1SPaolo Abeni 
923ef57c161SEric Dumazet 		sk->sk_rx_dst_cookie = rt6_get_cookie(rt);
92464f0f5d1SPaolo Abeni 	}
92564f0f5d1SPaolo Abeni }
92664f0f5d1SPaolo Abeni 
927eb63f296SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
928eb63f296SPaolo Abeni  * return code conversion for ip layer consumption
929eb63f296SPaolo Abeni  */
930eb63f296SPaolo Abeni static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
931eb63f296SPaolo Abeni 				struct udphdr *uh)
932eb63f296SPaolo Abeni {
933eb63f296SPaolo Abeni 	int ret;
934eb63f296SPaolo Abeni 
935eb63f296SPaolo Abeni 	if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
936e4aa33adSLi RongQing 		skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
937eb63f296SPaolo Abeni 
938eb63f296SPaolo Abeni 	ret = udpv6_queue_rcv_skb(sk, skb);
939eb63f296SPaolo Abeni 
94084dad559SPaolo Abeni 	/* a return value > 0 means to resubmit the input */
941eb63f296SPaolo Abeni 	if (ret > 0)
94284dad559SPaolo Abeni 		return ret;
943eb63f296SPaolo Abeni 	return 0;
944eb63f296SPaolo Abeni }
945eb63f296SPaolo Abeni 
946645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
947db8dac20SDavid S. Miller 		   int proto)
948db8dac20SDavid S. Miller {
9494cf91f82SDavid Ahern 	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
950b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
951ca065d0cSEric Dumazet 	struct net *net = dev_net(skb->dev);
952ca065d0cSEric Dumazet 	struct udphdr *uh;
953ca065d0cSEric Dumazet 	struct sock *sk;
95471489e21SJoe Stringer 	bool refcounted;
955db8dac20SDavid S. Miller 	u32 ulen = 0;
956db8dac20SDavid S. Miller 
957db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
958d6bc0149SBjørn Mork 		goto discard;
959db8dac20SDavid S. Miller 
960db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
961db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
962db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
963db8dac20SDavid S. Miller 
964db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
965db8dac20SDavid S. Miller 	if (ulen > skb->len)
966db8dac20SDavid S. Miller 		goto short_packet;
967db8dac20SDavid S. Miller 
968db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
969db8dac20SDavid S. Miller 		/* UDP validates ulen. */
970db8dac20SDavid S. Miller 
971db8dac20SDavid S. Miller 		/* Check for jumbo payload */
972db8dac20SDavid S. Miller 		if (ulen == 0)
973db8dac20SDavid S. Miller 			ulen = skb->len;
974db8dac20SDavid S. Miller 
975db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
976db8dac20SDavid S. Miller 			goto short_packet;
977db8dac20SDavid S. Miller 
978db8dac20SDavid S. Miller 		if (ulen < skb->len) {
979db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
980db8dac20SDavid S. Miller 				goto short_packet;
981db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
982db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
983db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
984db8dac20SDavid S. Miller 		}
985db8dac20SDavid S. Miller 	}
986db8dac20SDavid S. Miller 
987db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
9886a5dc9e5SEric Dumazet 		goto csum_error;
989db8dac20SDavid S. Miller 
990c9f2c1aeSPaolo Abeni 	/* Check if the socket is already available, e.g. due to early demux */
99171489e21SJoe Stringer 	sk = skb_steal_sock(skb, &refcounted);
992c9f2c1aeSPaolo Abeni 	if (sk) {
993c9f2c1aeSPaolo Abeni 		struct dst_entry *dst = skb_dst(skb);
994c9f2c1aeSPaolo Abeni 		int ret;
995c9f2c1aeSPaolo Abeni 
9968f905c0eSEric Dumazet 		if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
99764f0f5d1SPaolo Abeni 			udp6_sk_rx_dst_set(sk, dst);
998c9f2c1aeSPaolo Abeni 
999eb63f296SPaolo Abeni 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
100071489e21SJoe Stringer 			if (refcounted)
1001c9f2c1aeSPaolo Abeni 				sock_put(sk);
1002eb63f296SPaolo Abeni 			goto report_csum_error;
1003eb63f296SPaolo Abeni 		}
1004c9f2c1aeSPaolo Abeni 
1005eb63f296SPaolo Abeni 		ret = udp6_unicast_rcv_skb(sk, skb, uh);
100671489e21SJoe Stringer 		if (refcounted)
1007eb63f296SPaolo Abeni 			sock_put(sk);
1008c9f2c1aeSPaolo Abeni 		return ret;
1009c9f2c1aeSPaolo Abeni 	}
1010c9f2c1aeSPaolo Abeni 
1011db8dac20SDavid S. Miller 	/*
1012db8dac20SDavid S. Miller 	 *	Multicast receive code
1013db8dac20SDavid S. Miller 	 */
1014db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
1015e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
101636cbb245SRick Jones 				saddr, daddr, udptable, proto);
1017db8dac20SDavid S. Miller 
1018db8dac20SDavid S. Miller 	/* Unicast */
1019607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
102053b24b8fSIan Morris 	if (sk) {
1021eb63f296SPaolo Abeni 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
1022eb63f296SPaolo Abeni 			goto report_csum_error;
1023eb63f296SPaolo Abeni 		return udp6_unicast_rcv_skb(sk, skb, uh);
10244068579eSTom Herbert 	}
10254068579eSTom Herbert 
10264cf91f82SDavid Ahern 	reason = SKB_DROP_REASON_NO_SOCKET;
10274cf91f82SDavid Ahern 
1028eb63f296SPaolo Abeni 	if (!uh->check)
1029eb63f296SPaolo Abeni 		goto report_csum_error;
10304068579eSTom Herbert 
1031db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1032db8dac20SDavid S. Miller 		goto discard;
1033b0e214d2SMadhu Koriginja 	nf_reset_ct(skb);
1034db8dac20SDavid S. Miller 
1035db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
10366a5dc9e5SEric Dumazet 		goto csum_error;
1037db8dac20SDavid S. Miller 
103802c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
10393ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
1040db8dac20SDavid S. Miller 
10414cf91f82SDavid Ahern 	kfree_skb_reason(skb, reason);
1042db8dac20SDavid S. Miller 	return 0;
1043db8dac20SDavid S. Miller 
1044db8dac20SDavid S. Miller short_packet:
10454cf91f82SDavid Ahern 	if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
10464cf91f82SDavid Ahern 		reason = SKB_DROP_REASON_PKT_TOO_SMALL;
1047ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
1048db8dac20SDavid S. Miller 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
1049ba7a46f1SJoe Perches 			    saddr, ntohs(uh->source),
1050ba7a46f1SJoe Perches 			    ulen, skb->len,
1051ba7a46f1SJoe Perches 			    daddr, ntohs(uh->dest));
10526a5dc9e5SEric Dumazet 	goto discard;
1053eb63f296SPaolo Abeni 
1054eb63f296SPaolo Abeni report_csum_error:
1055eb63f296SPaolo Abeni 	udp6_csum_zero_error(skb);
10566a5dc9e5SEric Dumazet csum_error:
10574cf91f82SDavid Ahern 	if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
10584cf91f82SDavid Ahern 		reason = SKB_DROP_REASON_UDP_CSUM;
105902c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1060db8dac20SDavid S. Miller discard:
106102c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
10624cf91f82SDavid Ahern 	kfree_skb_reason(skb, reason);
1063db8dac20SDavid S. Miller 	return 0;
1064db8dac20SDavid S. Miller }
1065db8dac20SDavid S. Miller 
10660bd84065Ssubashab@codeaurora.org 
10675425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net,
10685425077dSsubashab@codeaurora.org 			__be16 loc_port, const struct in6_addr *loc_addr,
10695425077dSsubashab@codeaurora.org 			__be16 rmt_port, const struct in6_addr *rmt_addr,
10704297a0efSDavid Ahern 			int dif, int sdif)
10715425077dSsubashab@codeaurora.org {
1072ba6aac15SKuniyuki Iwashima 	struct udp_table *udptable = net->ipv4.udp_table;
10730bd84065Ssubashab@codeaurora.org 	unsigned short hnum = ntohs(loc_port);
1074919dfa0bSKuniyuki Iwashima 	unsigned int hash2, slot2;
1075919dfa0bSKuniyuki Iwashima 	struct udp_hslot *hslot2;
1076919dfa0bSKuniyuki Iwashima 	__portpair ports;
10775425077dSsubashab@codeaurora.org 	struct sock *sk;
10785425077dSsubashab@codeaurora.org 
1079919dfa0bSKuniyuki Iwashima 	hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
1080ba6aac15SKuniyuki Iwashima 	slot2 = hash2 & udptable->mask;
1081ba6aac15SKuniyuki Iwashima 	hslot2 = &udptable->hash2[slot2];
1082919dfa0bSKuniyuki Iwashima 	ports = INET_COMBINED_PORTS(rmt_port, hnum);
1083919dfa0bSKuniyuki Iwashima 
10840bd84065Ssubashab@codeaurora.org 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
108585cb73ffSWei Wang 		if (sk->sk_state == TCP_ESTABLISHED &&
10865d368f03SEric Dumazet 		    inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
10875425077dSsubashab@codeaurora.org 			return sk;
10880bd84065Ssubashab@codeaurora.org 		/* Only check first socket in chain */
10890bd84065Ssubashab@codeaurora.org 		break;
10900bd84065Ssubashab@codeaurora.org 	}
10910bd84065Ssubashab@codeaurora.org 	return NULL;
10925425077dSsubashab@codeaurora.org }
10935425077dSsubashab@codeaurora.org 
109411052589SKuniyuki Iwashima void udp_v6_early_demux(struct sk_buff *skb)
10955425077dSsubashab@codeaurora.org {
10965425077dSsubashab@codeaurora.org 	struct net *net = dev_net(skb->dev);
10975425077dSsubashab@codeaurora.org 	const struct udphdr *uh;
10985425077dSsubashab@codeaurora.org 	struct sock *sk;
10995425077dSsubashab@codeaurora.org 	struct dst_entry *dst;
11005425077dSsubashab@codeaurora.org 	int dif = skb->dev->ifindex;
11014297a0efSDavid Ahern 	int sdif = inet6_sdif(skb);
11025425077dSsubashab@codeaurora.org 
11035425077dSsubashab@codeaurora.org 	if (!pskb_may_pull(skb, skb_transport_offset(skb) +
11045425077dSsubashab@codeaurora.org 	    sizeof(struct udphdr)))
11055425077dSsubashab@codeaurora.org 		return;
11065425077dSsubashab@codeaurora.org 
11075425077dSsubashab@codeaurora.org 	uh = udp_hdr(skb);
11085425077dSsubashab@codeaurora.org 
11095425077dSsubashab@codeaurora.org 	if (skb->pkt_type == PACKET_HOST)
11105425077dSsubashab@codeaurora.org 		sk = __udp6_lib_demux_lookup(net, uh->dest,
11115425077dSsubashab@codeaurora.org 					     &ipv6_hdr(skb)->daddr,
11125425077dSsubashab@codeaurora.org 					     uh->source, &ipv6_hdr(skb)->saddr,
11134297a0efSDavid Ahern 					     dif, sdif);
11145425077dSsubashab@codeaurora.org 	else
11155425077dSsubashab@codeaurora.org 		return;
11165425077dSsubashab@codeaurora.org 
111741c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
11185425077dSsubashab@codeaurora.org 		return;
11195425077dSsubashab@codeaurora.org 
11205425077dSsubashab@codeaurora.org 	skb->sk = sk;
11215425077dSsubashab@codeaurora.org 	skb->destructor = sock_efree;
11228f905c0eSEric Dumazet 	dst = rcu_dereference(sk->sk_rx_dst);
11235425077dSsubashab@codeaurora.org 
11245425077dSsubashab@codeaurora.org 	if (dst)
1125ef57c161SEric Dumazet 		dst = dst_check(dst, sk->sk_rx_dst_cookie);
11265425077dSsubashab@codeaurora.org 	if (dst) {
1127d24406c8SWei Wang 		/* set noref for now.
1128d24406c8SWei Wang 		 * any place which wants to hold dst has to call
1129d24406c8SWei Wang 		 * dst_hold_safe()
1130d24406c8SWei Wang 		 */
11315425077dSsubashab@codeaurora.org 		skb_dst_set_noref(skb, dst);
11325425077dSsubashab@codeaurora.org 	}
11335425077dSsubashab@codeaurora.org }
11345425077dSsubashab@codeaurora.org 
11350e219ae4SPaolo Abeni INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
1136db8dac20SDavid S. Miller {
1137ba6aac15SKuniyuki Iwashima 	return __udp6_lib_rcv(skb, dev_net(skb->dev)->ipv4.udp_table, IPPROTO_UDP);
1138db8dac20SDavid S. Miller }
1139db8dac20SDavid S. Miller 
1140db8dac20SDavid S. Miller /*
1141db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
1142db8dac20SDavid S. Miller  */
1143db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
1144db8dac20SDavid S. Miller {
1145db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1146db8dac20SDavid S. Miller 
114736d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
114836d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
114936d926b9SDenis V. Lunev 	else if (up->pending) {
1150db8dac20SDavid S. Miller 		up->len = 0;
1151db8dac20SDavid S. Miller 		up->pending = 0;
1152db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
1153db8dac20SDavid S. Miller 	}
1154db8dac20SDavid S. Miller }
1155db8dac20SDavid S. Miller 
1156d74bad4eSAndrey Ignatov static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
1157d74bad4eSAndrey Ignatov 			     int addr_len)
1158d74bad4eSAndrey Ignatov {
1159bddc028aSTetsuo Handa 	if (addr_len < offsetofend(struct sockaddr, sa_family))
1160bddc028aSTetsuo Handa 		return -EINVAL;
1161d74bad4eSAndrey Ignatov 	/* The following checks are replicated from __ip6_datagram_connect()
1162d74bad4eSAndrey Ignatov 	 * and intended to prevent BPF program called below from accessing
1163d74bad4eSAndrey Ignatov 	 * bytes that are out of the bound specified by user in addr_len.
1164d74bad4eSAndrey Ignatov 	 */
1165d74bad4eSAndrey Ignatov 	if (uaddr->sa_family == AF_INET) {
116689e9c728SKuniyuki Iwashima 		if (ipv6_only_sock(sk))
1167d74bad4eSAndrey Ignatov 			return -EAFNOSUPPORT;
1168d74bad4eSAndrey Ignatov 		return udp_pre_connect(sk, uaddr, addr_len);
1169d74bad4eSAndrey Ignatov 	}
1170d74bad4eSAndrey Ignatov 
1171d74bad4eSAndrey Ignatov 	if (addr_len < SIN6_LEN_RFC2133)
1172d74bad4eSAndrey Ignatov 		return -EINVAL;
1173d74bad4eSAndrey Ignatov 
1174d74bad4eSAndrey Ignatov 	return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr);
1175d74bad4eSAndrey Ignatov }
1176d74bad4eSAndrey Ignatov 
1177493c6be3SSridhar Samudrala /**
1178493c6be3SSridhar Samudrala  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
1179493c6be3SSridhar Samudrala  *	@sk:	socket we are sending on
1180493c6be3SSridhar Samudrala  *	@skb:	sk_buff containing the filled-in UDP header
1181493c6be3SSridhar Samudrala  *		(checksum field must be zeroed out)
1182b51cd7c8SAndrew Lunn  *	@saddr: source address
1183b51cd7c8SAndrew Lunn  *	@daddr: destination address
1184b51cd7c8SAndrew Lunn  *	@len:	length of packet
1185493c6be3SSridhar Samudrala  */
1186493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1187493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
1188493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
1189493c6be3SSridhar Samudrala {
1190493c6be3SSridhar Samudrala 	unsigned int offset;
1191493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
1192d39d938cSVlad Yasevich 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1193493c6be3SSridhar Samudrala 	__wsum csum = 0;
1194493c6be3SSridhar Samudrala 
1195d39d938cSVlad Yasevich 	if (!frags) {
1196493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
1197493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
1198493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
1199493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1200493c6be3SSridhar Samudrala 	} else {
1201493c6be3SSridhar Samudrala 		/*
1202493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
1203493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
1204493c6be3SSridhar Samudrala 		 * should be together
1205493c6be3SSridhar Samudrala 		 */
1206493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
1207493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
120863ecc3d9SSubash Abhinov Kasiviswanathan 		csum = skb->csum;
1209493c6be3SSridhar Samudrala 
1210493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
1211493c6be3SSridhar Samudrala 
1212d39d938cSVlad Yasevich 		do {
1213d39d938cSVlad Yasevich 			csum = csum_add(csum, frags->csum);
1214d39d938cSVlad Yasevich 		} while ((frags = frags->next));
1215493c6be3SSridhar Samudrala 
1216493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1217493c6be3SSridhar Samudrala 					    csum);
1218493c6be3SSridhar Samudrala 		if (uh->check == 0)
1219493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
1220493c6be3SSridhar Samudrala 	}
1221493c6be3SSridhar Samudrala }
1222493c6be3SSridhar Samudrala 
1223db8dac20SDavid S. Miller /*
1224db8dac20SDavid S. Miller  *	Sending
1225db8dac20SDavid S. Miller  */
1226db8dac20SDavid S. Miller 
1227bec1f6f6SWillem de Bruijn static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1228bec1f6f6SWillem de Bruijn 			   struct inet_cork *cork)
1229db8dac20SDavid S. Miller {
1230d39d938cSVlad Yasevich 	struct sock *sk = skb->sk;
1231db8dac20SDavid S. Miller 	struct udphdr *uh;
1232db8dac20SDavid S. Miller 	int err = 0;
1233db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1234db8dac20SDavid S. Miller 	__wsum csum = 0;
1235d39d938cSVlad Yasevich 	int offset = skb_transport_offset(skb);
1236d39d938cSVlad Yasevich 	int len = skb->len - offset;
12374094871dSJosh Hunt 	int datalen = len - sizeof(*uh);
1238db8dac20SDavid S. Miller 
1239db8dac20SDavid S. Miller 	/*
1240db8dac20SDavid S. Miller 	 * Create a UDP header
1241db8dac20SDavid S. Miller 	 */
1242db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
12431958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
12441958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
1245d39d938cSVlad Yasevich 	uh->len = htons(len);
1246db8dac20SDavid S. Miller 	uh->check = 0;
1247db8dac20SDavid S. Miller 
1248bec1f6f6SWillem de Bruijn 	if (cork->gso_size) {
1249bec1f6f6SWillem de Bruijn 		const int hlen = skb_network_header_len(skb) +
1250bec1f6f6SWillem de Bruijn 				 sizeof(struct udphdr);
1251bec1f6f6SWillem de Bruijn 
12520f149c9fSWillem de Bruijn 		if (hlen + cork->gso_size > cork->fragsize) {
12530f149c9fSWillem de Bruijn 			kfree_skb(skb);
1254bec1f6f6SWillem de Bruijn 			return -EINVAL;
12550f149c9fSWillem de Bruijn 		}
1256736ef37fSCoco Li 		if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
12570f149c9fSWillem de Bruijn 			kfree_skb(skb);
1258bec1f6f6SWillem de Bruijn 			return -EINVAL;
12590f149c9fSWillem de Bruijn 		}
12600f149c9fSWillem de Bruijn 		if (udp_sk(sk)->no_check6_tx) {
12610f149c9fSWillem de Bruijn 			kfree_skb(skb);
1262a8c744a8SWillem de Bruijn 			return -EINVAL;
12630f149c9fSWillem de Bruijn 		}
1264ff06342cSWillem de Bruijn 		if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
12650f149c9fSWillem de Bruijn 		    dst_xfrm(skb_dst(skb))) {
12660f149c9fSWillem de Bruijn 			kfree_skb(skb);
1267bec1f6f6SWillem de Bruijn 			return -EIO;
12680f149c9fSWillem de Bruijn 		}
1269bec1f6f6SWillem de Bruijn 
12704094871dSJosh Hunt 		if (datalen > cork->gso_size) {
1271bec1f6f6SWillem de Bruijn 			skb_shinfo(skb)->gso_size = cork->gso_size;
1272bec1f6f6SWillem de Bruijn 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
12734094871dSJosh Hunt 			skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
127444b321e5SJosh Hunt 								 cork->gso_size);
12754094871dSJosh Hunt 		}
1276a8c744a8SWillem de Bruijn 		goto csum_partial;
1277bec1f6f6SWillem de Bruijn 	}
1278bec1f6f6SWillem de Bruijn 
1279db8dac20SDavid S. Miller 	if (is_udplite)
1280d39d938cSVlad Yasevich 		csum = udplite_csum(skb);
1281d39d938cSVlad Yasevich 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
12824068579eSTom Herbert 		skb->ip_summed = CHECKSUM_NONE;
12834068579eSTom Herbert 		goto send;
12844068579eSTom Herbert 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1285a8c744a8SWillem de Bruijn csum_partial:
1286d39d938cSVlad Yasevich 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1287493c6be3SSridhar Samudrala 		goto send;
1288493c6be3SSridhar Samudrala 	} else
1289d39d938cSVlad Yasevich 		csum = udp_csum(skb);
1290db8dac20SDavid S. Miller 
1291db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
12924c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1293d39d938cSVlad Yasevich 				    len, fl6->flowi6_proto, csum);
1294db8dac20SDavid S. Miller 	if (uh->check == 0)
1295db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
1296db8dac20SDavid S. Miller 
1297493c6be3SSridhar Samudrala send:
1298d39d938cSVlad Yasevich 	err = ip6_send_skb(skb);
12996ce9e7b5SEric Dumazet 	if (err) {
13006ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
13016aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
13026ce9e7b5SEric Dumazet 				       UDP_MIB_SNDBUFERRORS, is_udplite);
13036ce9e7b5SEric Dumazet 			err = 0;
13046ce9e7b5SEric Dumazet 		}
13056aef70a8SEric Dumazet 	} else {
13066aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
13076ce9e7b5SEric Dumazet 			       UDP_MIB_OUTDATAGRAMS, is_udplite);
13086aef70a8SEric Dumazet 	}
1309d39d938cSVlad Yasevich 	return err;
1310d39d938cSVlad Yasevich }
1311d39d938cSVlad Yasevich 
1312d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
1313d39d938cSVlad Yasevich {
1314d39d938cSVlad Yasevich 	struct sk_buff *skb;
1315d39d938cSVlad Yasevich 	struct udp_sock  *up = udp_sk(sk);
1316d39d938cSVlad Yasevich 	int err = 0;
1317d39d938cSVlad Yasevich 
1318d39d938cSVlad Yasevich 	if (up->pending == AF_INET)
1319d39d938cSVlad Yasevich 		return udp_push_pending_frames(sk);
1320d39d938cSVlad Yasevich 
1321d39d938cSVlad Yasevich 	skb = ip6_finish_skb(sk);
1322d39d938cSVlad Yasevich 	if (!skb)
1323d39d938cSVlad Yasevich 		goto out;
1324d39d938cSVlad Yasevich 
1325940ea00bSPavel Begunkov 	err = udp_v6_send_skb(skb, &inet_sk(sk)->cork.fl.u.ip6,
1326940ea00bSPavel Begunkov 			      &inet_sk(sk)->cork.base);
1327db8dac20SDavid S. Miller out:
1328db8dac20SDavid S. Miller 	up->len = 0;
1329db8dac20SDavid S. Miller 	up->pending = 0;
1330db8dac20SDavid S. Miller 	return err;
1331db8dac20SDavid S. Miller }
1332db8dac20SDavid S. Miller 
13331b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1334db8dac20SDavid S. Miller {
1335db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1336db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1337db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1338db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1339342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
134020c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1341db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
134245f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
1343db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
13445298953eSPavel Begunkov 	struct inet_cork_full cork;
13455298953eSPavel Begunkov 	struct flowi6 *fl6 = &cork.fl.u.ip6;
1346db8dac20SDavid S. Miller 	struct dst_entry *dst;
134726879da5SWei Wang 	struct ipcm6_cookie ipc6;
1348db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
13499f542f61SAlexey Kodanev 	bool connected = false;
1350db8dac20SDavid S. Miller 	int ulen = len;
1351a9f59707SEric Dumazet 	int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE;
1352db8dac20SDavid S. Miller 	int err;
1353db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1354db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1355db8dac20SDavid S. Miller 
1356b515430aSWillem de Bruijn 	ipcm6_init(&ipc6);
135718a419baSEric Dumazet 	ipc6.gso_size = READ_ONCE(up->gso_size);
13585fdaa88dSWillem de Bruijn 	ipc6.sockc.tsflags = sk->sk_tsflags;
1359c6af0c22SWillem de Bruijn 	ipc6.sockc.mark = sk->sk_mark;
136026879da5SWei Wang 
1361db8dac20SDavid S. Miller 	/* destination address check */
1362db8dac20SDavid S. Miller 	if (sin6) {
1363db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1364db8dac20SDavid S. Miller 			return -EINVAL;
1365db8dac20SDavid S. Miller 
1366db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1367db8dac20SDavid S. Miller 		case AF_INET6:
1368db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1369db8dac20SDavid S. Miller 				return -EINVAL;
1370db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1371052d2369SJonathan T. Leighton 			if (ipv6_addr_any(daddr) &&
1372052d2369SJonathan T. Leighton 			    ipv6_addr_v4mapped(&np->saddr))
1373052d2369SJonathan T. Leighton 				ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1374052d2369SJonathan T. Leighton 						       daddr);
1375db8dac20SDavid S. Miller 			break;
1376db8dac20SDavid S. Miller 		case AF_INET:
1377db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1378db8dac20SDavid S. Miller 		case AF_UNSPEC:
1379db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1380db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1381db8dac20SDavid S. Miller 			daddr = NULL;
1382db8dac20SDavid S. Miller 			break;
1383db8dac20SDavid S. Miller 		default:
1384db8dac20SDavid S. Miller 			return -EINVAL;
1385db8dac20SDavid S. Miller 		}
1386db8dac20SDavid S. Miller 	} else if (!up->pending) {
1387db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1388db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1389efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
1390db8dac20SDavid S. Miller 	} else
1391db8dac20SDavid S. Miller 		daddr = NULL;
1392db8dac20SDavid S. Miller 
1393db8dac20SDavid S. Miller 	if (daddr) {
1394db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1395db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1396db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1397c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1398db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1399db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1400db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1401db8dac20SDavid S. Miller do_udp_sendmsg:
14021c5950fcSEric Dumazet 			err = ipv6_only_sock(sk) ?
14031c5950fcSEric Dumazet 				-ENETUNREACH : udp_sendmsg(sk, msg, len);
14041c5950fcSEric Dumazet 			msg->msg_name = sin6;
14051c5950fcSEric Dumazet 			msg->msg_namelen = addr_len;
14061c5950fcSEric Dumazet 			return err;
1407db8dac20SDavid S. Miller 		}
1408db8dac20SDavid S. Miller 	}
1409db8dac20SDavid S. Miller 
1410db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1411db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1412db8dac20SDavid S. Miller 	   */
1413db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1414db8dac20SDavid S. Miller 		return -EMSGSIZE;
1415db8dac20SDavid S. Miller 
141603485f2aSVlad Yasevich 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1417db8dac20SDavid S. Miller 	if (up->pending) {
1418406c4a0aSPavel Begunkov 		if (up->pending == AF_INET)
1419406c4a0aSPavel Begunkov 			return udp_sendmsg(sk, msg, len);
1420db8dac20SDavid S. Miller 		/*
1421db8dac20SDavid S. Miller 		 * There are pending frames.
1422db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1423db8dac20SDavid S. Miller 		 */
1424db8dac20SDavid S. Miller 		lock_sock(sk);
1425db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1426db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1427db8dac20SDavid S. Miller 				release_sock(sk);
1428db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1429db8dac20SDavid S. Miller 			}
1430db8dac20SDavid S. Miller 			dst = NULL;
1431db8dac20SDavid S. Miller 			goto do_append_data;
1432db8dac20SDavid S. Miller 		}
1433db8dac20SDavid S. Miller 		release_sock(sk);
1434db8dac20SDavid S. Miller 	}
1435db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1436db8dac20SDavid S. Miller 
14375298953eSPavel Begunkov 	memset(fl6, 0, sizeof(*fl6));
1438db8dac20SDavid S. Miller 
1439db8dac20SDavid S. Miller 	if (sin6) {
1440db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1441db8dac20SDavid S. Miller 			return -EINVAL;
1442db8dac20SDavid S. Miller 
14435298953eSPavel Begunkov 		fl6->fl6_dport = sin6->sin6_port;
1444db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1445db8dac20SDavid S. Miller 
1446db8dac20SDavid S. Miller 		if (np->sndflow) {
14475298953eSPavel Begunkov 			fl6->flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
14485298953eSPavel Begunkov 			if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) {
14495298953eSPavel Begunkov 				flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
145059c820b2SWillem de Bruijn 				if (IS_ERR(flowlabel))
1451db8dac20SDavid S. Miller 					return -EINVAL;
1452db8dac20SDavid S. Miller 			}
1453db8dac20SDavid S. Miller 		}
1454db8dac20SDavid S. Miller 
1455db8dac20SDavid S. Miller 		/*
1456db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1457db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1458db8dac20SDavid S. Miller 		 */
1459db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1460efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1461efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
1462db8dac20SDavid S. Miller 
1463db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1464db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1465842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
14665298953eSPavel Begunkov 			fl6->flowi6_oif = sin6->sin6_scope_id;
1467db8dac20SDavid S. Miller 	} else {
1468db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1469db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1470db8dac20SDavid S. Miller 
14715298953eSPavel Begunkov 		fl6->fl6_dport = inet->inet_dport;
1472efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
14735298953eSPavel Begunkov 		fl6->flowlabel = np->flow_label;
14749f542f61SAlexey Kodanev 		connected = true;
1475db8dac20SDavid S. Miller 	}
1476db8dac20SDavid S. Miller 
14775298953eSPavel Begunkov 	if (!fl6->flowi6_oif)
14784c971d2fSEric Dumazet 		fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
1479db8dac20SDavid S. Miller 
14805298953eSPavel Begunkov 	if (!fl6->flowi6_oif)
14815298953eSPavel Begunkov 		fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
14829f690db7SYang Hongyang 
14835298953eSPavel Begunkov 	fl6->flowi6_uid = sk->sk_uid;
148451953d5bSBrian Haley 
1485db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1486db8dac20SDavid S. Miller 		opt = &opt_space;
1487db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1488db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
148926879da5SWei Wang 		ipc6.opt = opt;
1490db8dac20SDavid S. Miller 
14912e8de857SWillem de Bruijn 		err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
14922e8de857SWillem de Bruijn 		if (err > 0)
14935298953eSPavel Begunkov 			err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, fl6,
14945fdaa88dSWillem de Bruijn 						    &ipc6);
1495db8dac20SDavid S. Miller 		if (err < 0) {
1496db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1497db8dac20SDavid S. Miller 			return err;
1498db8dac20SDavid S. Miller 		}
14995298953eSPavel Begunkov 		if ((fl6->flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
15005298953eSPavel Begunkov 			flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
150159c820b2SWillem de Bruijn 			if (IS_ERR(flowlabel))
1502db8dac20SDavid S. Miller 				return -EINVAL;
1503db8dac20SDavid S. Miller 		}
1504db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1505db8dac20SDavid S. Miller 			opt = NULL;
15069f542f61SAlexey Kodanev 		connected = false;
1507db8dac20SDavid S. Miller 	}
150845f6fad8SEric Dumazet 	if (!opt) {
150945f6fad8SEric Dumazet 		opt = txopt_get(np);
151045f6fad8SEric Dumazet 		opt_to_free = opt;
151145f6fad8SEric Dumazet 	}
1512db8dac20SDavid S. Miller 	if (flowlabel)
1513db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1514db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
151526879da5SWei Wang 	ipc6.opt = opt;
1516db8dac20SDavid S. Miller 
15175298953eSPavel Begunkov 	fl6->flowi6_proto = sk->sk_protocol;
15185298953eSPavel Begunkov 	fl6->flowi6_mark = ipc6.sockc.mark;
15195298953eSPavel Begunkov 	fl6->daddr = *daddr;
15205298953eSPavel Begunkov 	if (ipv6_addr_any(&fl6->saddr) && !ipv6_addr_any(&np->saddr))
15215298953eSPavel Begunkov 		fl6->saddr = np->saddr;
15225298953eSPavel Begunkov 	fl6->fl6_sport = inet->inet_sport;
1523db8dac20SDavid S. Miller 
15246fc88c35SDave Marchevsky 	if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) {
15251cedee13SAndrey Ignatov 		err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
15265298953eSPavel Begunkov 					   (struct sockaddr *)sin6,
15275298953eSPavel Begunkov 					   &fl6->saddr);
15281cedee13SAndrey Ignatov 		if (err)
15291cedee13SAndrey Ignatov 			goto out_no_dst;
15301cedee13SAndrey Ignatov 		if (sin6) {
15311cedee13SAndrey Ignatov 			if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
15321cedee13SAndrey Ignatov 				/* BPF program rewrote IPv6-only by IPv4-mapped
15331cedee13SAndrey Ignatov 				 * IPv6. It's currently unsupported.
15341cedee13SAndrey Ignatov 				 */
15351cedee13SAndrey Ignatov 				err = -ENOTSUPP;
15361cedee13SAndrey Ignatov 				goto out_no_dst;
15371cedee13SAndrey Ignatov 			}
15381cedee13SAndrey Ignatov 			if (sin6->sin6_port == 0) {
15391cedee13SAndrey Ignatov 				/* BPF program set invalid port. Reject it. */
15401cedee13SAndrey Ignatov 				err = -EINVAL;
15411cedee13SAndrey Ignatov 				goto out_no_dst;
15421cedee13SAndrey Ignatov 			}
15435298953eSPavel Begunkov 			fl6->fl6_dport = sin6->sin6_port;
15445298953eSPavel Begunkov 			fl6->daddr = sin6->sin6_addr;
15451cedee13SAndrey Ignatov 		}
15461cedee13SAndrey Ignatov 	}
15471cedee13SAndrey Ignatov 
15485298953eSPavel Begunkov 	if (ipv6_addr_any(&fl6->daddr))
15495298953eSPavel Begunkov 		fl6->daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1550e8e36984SAndrey Ignatov 
15515298953eSPavel Begunkov 	final_p = fl6_update_dst(fl6, opt, &final);
155220c59de2SArnaud Ebalard 	if (final_p)
15539f542f61SAlexey Kodanev 		connected = false;
1554db8dac20SDavid S. Miller 
15555298953eSPavel Begunkov 	if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) {
15565298953eSPavel Begunkov 		fl6->flowi6_oif = np->mcast_oif;
15579f542f61SAlexey Kodanev 		connected = false;
15585298953eSPavel Begunkov 	} else if (!fl6->flowi6_oif)
15595298953eSPavel Begunkov 		fl6->flowi6_oif = np->ucast_oif;
1560db8dac20SDavid S. Miller 
15615298953eSPavel Begunkov 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
1562db8dac20SDavid S. Miller 
156338b7097bSHannes Frederic Sowa 	if (ipc6.tclass < 0)
156438b7097bSHannes Frederic Sowa 		ipc6.tclass = np->tclass;
156538b7097bSHannes Frederic Sowa 
15665298953eSPavel Begunkov 	fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel);
156738b7097bSHannes Frederic Sowa 
15685298953eSPavel Begunkov 	dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected);
156968d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
157068d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
157168d0c6d3SDavid S. Miller 		dst = NULL;
1572db8dac20SDavid S. Miller 		goto out;
1573db8dac20SDavid S. Miller 	}
1574db8dac20SDavid S. Miller 
157526879da5SWei Wang 	if (ipc6.hlimit < 0)
15765298953eSPavel Begunkov 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, fl6, dst);
1577db8dac20SDavid S. Miller 
1578db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1579db8dac20SDavid S. Miller 		goto do_confirm;
1580db8dac20SDavid S. Miller back_from_confirm:
1581db8dac20SDavid S. Miller 
158203485f2aSVlad Yasevich 	/* Lockless fast path for the non-corking case */
158303485f2aSVlad Yasevich 	if (!corkreq) {
158403485f2aSVlad Yasevich 		struct sk_buff *skb;
158503485f2aSVlad Yasevich 
158603485f2aSVlad Yasevich 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
158726879da5SWei Wang 				   sizeof(struct udphdr), &ipc6,
1588f37a4cc6SPavel Begunkov 				   (struct rt6_info *)dst,
15895fdaa88dSWillem de Bruijn 				   msg->msg_flags, &cork);
159003485f2aSVlad Yasevich 		err = PTR_ERR(skb);
159103485f2aSVlad Yasevich 		if (!IS_ERR_OR_NULL(skb))
15925298953eSPavel Begunkov 			err = udp_v6_send_skb(skb, fl6, &cork.base);
159340ac240cSPavel Begunkov 		/* ip6_make_skb steals dst reference */
159440ac240cSPavel Begunkov 		goto out_no_dst;
159503485f2aSVlad Yasevich 	}
159603485f2aSVlad Yasevich 
1597db8dac20SDavid S. Miller 	lock_sock(sk);
1598db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1599db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1600db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1601db8dac20SDavid S. Miller 		release_sock(sk);
1602db8dac20SDavid S. Miller 
1603ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 2\n");
1604db8dac20SDavid S. Miller 		err = -EINVAL;
1605db8dac20SDavid S. Miller 		goto out;
1606db8dac20SDavid S. Miller 	}
1607db8dac20SDavid S. Miller 
1608db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1609db8dac20SDavid S. Miller 
1610db8dac20SDavid S. Miller do_append_data:
161126879da5SWei Wang 	if (ipc6.dontfrag < 0)
161226879da5SWei Wang 		ipc6.dontfrag = np->dontfrag;
1613db8dac20SDavid S. Miller 	up->len += ulen;
161426879da5SWei Wang 	err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
16155298953eSPavel Begunkov 			      &ipc6, fl6, (struct rt6_info *)dst,
16165fdaa88dSWillem de Bruijn 			      corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1617db8dac20SDavid S. Miller 	if (err)
1618db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1619db8dac20SDavid S. Miller 	else if (!corkreq)
1620db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1621db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1622db8dac20SDavid S. Miller 		up->pending = 0;
1623db8dac20SDavid S. Miller 
162403485f2aSVlad Yasevich 	if (err > 0)
162503485f2aSVlad Yasevich 		err = np->recverr ? net_xmit_errno(err) : 0;
162603485f2aSVlad Yasevich 	release_sock(sk);
162703485f2aSVlad Yasevich 
1628db8dac20SDavid S. Miller out:
1629a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
16301cedee13SAndrey Ignatov out_no_dst:
1631db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
163245f6fad8SEric Dumazet 	txopt_put(opt_to_free);
1633db8dac20SDavid S. Miller 	if (!err)
1634db8dac20SDavid S. Miller 		return len;
1635db8dac20SDavid S. Miller 	/*
1636db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1637db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1638db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1639db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1640db8dac20SDavid S. Miller 	 * seems like overkill.
1641db8dac20SDavid S. Miller 	 */
1642db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
16436aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
1644235b9f7aSPavel Emelyanov 			       UDP_MIB_SNDBUFERRORS, is_udplite);
1645db8dac20SDavid S. Miller 	}
1646db8dac20SDavid S. Miller 	return err;
1647db8dac20SDavid S. Miller 
1648db8dac20SDavid S. Miller do_confirm:
16490dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
16505298953eSPavel Begunkov 		dst_confirm_neigh(dst, &fl6->daddr);
1651db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1652db8dac20SDavid S. Miller 		goto back_from_confirm;
1653db8dac20SDavid S. Miller 	err = 0;
1654db8dac20SDavid S. Miller 	goto out;
1655db8dac20SDavid S. Miller }
1656ed472b0cSDavid Howells EXPORT_SYMBOL(udpv6_sendmsg);
1657db8dac20SDavid S. Miller 
16581d7e4538SDavid Howells static void udpv6_splice_eof(struct socket *sock)
16591d7e4538SDavid Howells {
16601d7e4538SDavid Howells 	struct sock *sk = sock->sk;
16611d7e4538SDavid Howells 	struct udp_sock *up = udp_sk(sk);
16621d7e4538SDavid Howells 
16631d7e4538SDavid Howells 	if (!up->pending || READ_ONCE(up->corkflag))
16641d7e4538SDavid Howells 		return;
16651d7e4538SDavid Howells 
16661d7e4538SDavid Howells 	lock_sock(sk);
16671d7e4538SDavid Howells 	if (up->pending && !READ_ONCE(up->corkflag))
16681d7e4538SDavid Howells 		udp_v6_push_pending_frames(sk);
16691d7e4538SDavid Howells 	release_sock(sk);
16701d7e4538SDavid Howells }
16711d7e4538SDavid Howells 
16727d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1673db8dac20SDavid S. Miller {
167444046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1675db8dac20SDavid S. Miller 	lock_sock(sk);
1676a8b897c7SPaolo Abeni 
1677a8b897c7SPaolo Abeni 	/* protects from races with udp_abort() */
1678a8b897c7SPaolo Abeni 	sock_set_flag(sk, SOCK_DEAD);
1679db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1680db8dac20SDavid S. Miller 	release_sock(sk);
1681db8dac20SDavid S. Miller 
168260fb9567SPaolo Abeni 	if (static_branch_unlikely(&udpv6_encap_needed_key)) {
168360fb9567SPaolo Abeni 		if (up->encap_type) {
168444046a59STom Parkin 			void (*encap_destroy)(struct sock *sk);
16856aa7de05SMark Rutland 			encap_destroy = READ_ONCE(up->encap_destroy);
168644046a59STom Parkin 			if (encap_destroy)
168744046a59STom Parkin 				encap_destroy(sk);
168844046a59STom Parkin 		}
1689a4a600ddSXin Long 		if (up->encap_enabled) {
16909c480601SPaolo Abeni 			static_branch_dec(&udpv6_encap_needed_key);
1691a4a600ddSXin Long 			udp_encap_disable();
1692a4a600ddSXin Long 		}
169360fb9567SPaolo Abeni 	}
1694db8dac20SDavid S. Miller }
1695db8dac20SDavid S. Miller 
1696db8dac20SDavid S. Miller /*
1697db8dac20SDavid S. Miller  *	Socket option code for UDP
1698db8dac20SDavid S. Miller  */
1699a7b75c5aSChristoph Hellwig int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1700a7b75c5aSChristoph Hellwig 		     unsigned int optlen)
1701db8dac20SDavid S. Miller {
17028a3854c7SPaolo Abeni 	if (level == SOL_UDP  ||  level == SOL_UDPLITE || level == SOL_SOCKET)
170391ac1ccaSChristoph Hellwig 		return udp_lib_setsockopt(sk, level, optname,
1704a7b75c5aSChristoph Hellwig 					  optval, optlen,
1705db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1706db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1707db8dac20SDavid S. Miller }
1708db8dac20SDavid S. Miller 
1709db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1710db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1711db8dac20SDavid S. Miller {
1712db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1713db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1714db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1715db8dac20SDavid S. Miller }
1716db8dac20SDavid S. Miller 
171711052589SKuniyuki Iwashima static const struct inet6_protocol udpv6_protocol = {
1718db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1719db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1720db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1721db8dac20SDavid S. Miller };
1722db8dac20SDavid S. Miller 
1723db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1724db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1725db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1726db8dac20SDavid S. Miller {
172717ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
172817ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
172917ef66afSLorenzo Colitti 	} else {
173017ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1731abc17a11SEric Dumazet 		const struct inet_sock *inet = inet_sk((const struct sock *)v);
173217ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
173317ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
17346c206b20SPaolo Abeni 		__ip6_dgram_sock_seq_show(seq, v, srcp, destp,
17356c206b20SPaolo Abeni 					  udp_rqueue_get(v), bucket);
173617ef66afSLorenzo Colitti 	}
1737db8dac20SDavid S. Miller 	return 0;
1738db8dac20SDavid S. Miller }
1739db8dac20SDavid S. Miller 
1740c3506372SChristoph Hellwig const struct seq_operations udp6_seq_ops = {
1741a3d2599bSChristoph Hellwig 	.start		= udp_seq_start,
1742a3d2599bSChristoph Hellwig 	.next		= udp_seq_next,
1743a3d2599bSChristoph Hellwig 	.stop		= udp_seq_stop,
1744a3d2599bSChristoph Hellwig 	.show		= udp6_seq_show,
1745a3d2599bSChristoph Hellwig };
1746c3506372SChristoph Hellwig EXPORT_SYMBOL(udp6_seq_ops);
174773cb88ecSArjan van de Ven 
1748db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1749db8dac20SDavid S. Miller 	.family		= AF_INET6,
1750478aee5dSKuniyuki Iwashima 	.udp_table	= NULL,
1751db8dac20SDavid S. Miller };
1752db8dac20SDavid S. Miller 
17532c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1754db8dac20SDavid S. Miller {
1755c3506372SChristoph Hellwig 	if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1756c3506372SChristoph Hellwig 			sizeof(struct udp_iter_state), &udp6_seq_afinfo))
1757a3d2599bSChristoph Hellwig 		return -ENOMEM;
1758a3d2599bSChristoph Hellwig 	return 0;
1759db8dac20SDavid S. Miller }
1760db8dac20SDavid S. Miller 
1761ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1762ec120da6SIan Morris {
1763a3d2599bSChristoph Hellwig 	remove_proc_entry("udp6", net->proc_net);
1764db8dac20SDavid S. Miller }
1765db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1766db8dac20SDavid S. Miller 
1767db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1768db8dac20SDavid S. Miller 
1769db8dac20SDavid S. Miller struct proto udpv6_prot = {
1770db8dac20SDavid S. Miller 	.name			= "UDPv6",
1771db8dac20SDavid S. Miller 	.owner			= THIS_MODULE,
1772db8dac20SDavid S. Miller 	.close			= udp_lib_close,
1773d74bad4eSAndrey Ignatov 	.pre_connect		= udpv6_pre_connect,
1774db8dac20SDavid S. Miller 	.connect		= ip6_datagram_connect,
1775db8dac20SDavid S. Miller 	.disconnect		= udp_disconnect,
1776db8dac20SDavid S. Miller 	.ioctl			= udp_ioctl,
1777d38afeecSKuniyuki Iwashima 	.init			= udpv6_init_sock,
1778db8dac20SDavid S. Miller 	.destroy		= udpv6_destroy_sock,
1779db8dac20SDavid S. Miller 	.setsockopt		= udpv6_setsockopt,
1780db8dac20SDavid S. Miller 	.getsockopt		= udpv6_getsockopt,
1781db8dac20SDavid S. Miller 	.sendmsg		= udpv6_sendmsg,
1782db8dac20SDavid S. Miller 	.recvmsg		= udpv6_recvmsg,
17831d7e4538SDavid Howells 	.splice_eof		= udpv6_splice_eof,
1784e646b657SMartin KaFai Lau 	.release_cb		= ip6_datagram_release_cb,
1785db8dac20SDavid S. Miller 	.hash			= udp_lib_hash,
1786db8dac20SDavid S. Miller 	.unhash			= udp_lib_unhash,
1787719f8358SEric Dumazet 	.rehash			= udp_v6_rehash,
1788db8dac20SDavid S. Miller 	.get_port		= udp_v6_get_port,
178991a760b2SMenglong Dong 	.put_port		= udp_lib_unhash,
17908a59f9d1SCong Wang #ifdef CONFIG_BPF_SYSCALL
17918a59f9d1SCong Wang 	.psock_update_sk_prot	= udp_bpf_update_proto,
17928a59f9d1SCong Wang #endif
17930defbb0aSEric Dumazet 
1794db8dac20SDavid S. Miller 	.memory_allocated	= &udp_memory_allocated,
17950defbb0aSEric Dumazet 	.per_cpu_fw_alloc	= &udp_memory_per_cpu_fw_alloc,
17960defbb0aSEric Dumazet 
1797db8dac20SDavid S. Miller 	.sysctl_mem		= sysctl_udp_mem,
17981e802951STonghao Zhang 	.sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
17991e802951STonghao Zhang 	.sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1800db8dac20SDavid S. Miller 	.obj_size		= sizeof(struct udp6_sock),
180167fb4330SKuniyuki Iwashima 	.h.udp_table		= NULL,
18025d77dca8SDavid Ahern 	.diag_destroy		= udp_abort,
1803db8dac20SDavid S. Miller };
1804db8dac20SDavid S. Miller 
1805db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1806db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1807db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1808db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1809db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1810db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1811db8dac20SDavid S. Miller };
1812db8dac20SDavid S. Miller 
1813db8dac20SDavid S. Miller int __init udpv6_init(void)
1814db8dac20SDavid S. Miller {
1815db8dac20SDavid S. Miller 	int ret;
1816db8dac20SDavid S. Miller 
18173336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
18183336288aSVlad Yasevich 	if (ret)
1819c6b641a4SVlad Yasevich 		goto out;
18203336288aSVlad Yasevich 
1821db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1822db8dac20SDavid S. Miller 	if (ret)
1823db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1824db8dac20SDavid S. Miller out:
1825db8dac20SDavid S. Miller 	return ret;
1826db8dac20SDavid S. Miller 
1827db8dac20SDavid S. Miller out_udpv6_protocol:
1828db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1829db8dac20SDavid S. Miller 	goto out;
1830db8dac20SDavid S. Miller }
1831db8dac20SDavid S. Miller 
1832db8dac20SDavid S. Miller void udpv6_exit(void)
1833db8dac20SDavid S. Miller {
1834db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1835db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1836db8dac20SDavid S. Miller }
1837