xref: /openbmc/linux/net/ipv6/udp.c (revision 7b58e63e)
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 
20db8dac20SDavid S. Miller #include <linux/errno.h>
21db8dac20SDavid S. Miller #include <linux/types.h>
22db8dac20SDavid S. Miller #include <linux/socket.h>
23db8dac20SDavid S. Miller #include <linux/sockios.h>
24db8dac20SDavid S. Miller #include <linux/net.h>
25db8dac20SDavid S. Miller #include <linux/in6.h>
26db8dac20SDavid S. Miller #include <linux/netdevice.h>
27db8dac20SDavid S. Miller #include <linux/if_arp.h>
28db8dac20SDavid S. Miller #include <linux/ipv6.h>
29db8dac20SDavid S. Miller #include <linux/icmpv6.h>
30db8dac20SDavid S. Miller #include <linux/init.h>
31db8dac20SDavid S. Miller #include <linux/module.h>
32db8dac20SDavid S. Miller #include <linux/skbuff.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
347c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
350e219ae4SPaolo Abeni #include <linux/indirect_call_wrapper.h>
36db8dac20SDavid S. Miller 
37496611d7SCraig Gallek #include <net/addrconf.h>
38db8dac20SDavid S. Miller #include <net/ndisc.h>
39db8dac20SDavid S. Miller #include <net/protocol.h>
40db8dac20SDavid S. Miller #include <net/transp_v6.h>
41db8dac20SDavid S. Miller #include <net/ip6_route.h>
42db8dac20SDavid S. Miller #include <net/raw.h>
43db8dac20SDavid S. Miller #include <net/tcp_states.h>
44db8dac20SDavid S. Miller #include <net/ip6_checksum.h>
45e7cc0824SStefano Brivio #include <net/ip6_tunnel.h>
46db8dac20SDavid S. Miller #include <net/xfrm.h>
470bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h>
4872289b96STom Herbert #include <net/inet6_hashtables.h>
49076bb0c8SEliezer Tamir #include <net/busy_poll.h>
50e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
51db8dac20SDavid S. Miller 
52db8dac20SDavid S. Miller #include <linux/proc_fs.h>
53db8dac20SDavid S. Miller #include <linux/seq_file.h>
5422911fc5SEric Dumazet #include <trace/events/skb.h>
55db8dac20SDavid S. Miller #include "udp_impl.h"
56db8dac20SDavid S. Miller 
576eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net,
58b50026b5SHannes Frederic Sowa 			const struct in6_addr *laddr,
59b50026b5SHannes Frederic Sowa 			const u16 lport,
60b50026b5SHannes Frederic Sowa 			const struct in6_addr *faddr,
61b50026b5SHannes Frederic Sowa 			const __be16 fport)
62b50026b5SHannes Frederic Sowa {
631bbdceefSHannes Frederic Sowa 	static u32 udp6_ehash_secret __read_mostly;
641bbdceefSHannes Frederic Sowa 	static u32 udp_ipv6_hash_secret __read_mostly;
651bbdceefSHannes Frederic Sowa 
661bbdceefSHannes Frederic Sowa 	u32 lhash, fhash;
671bbdceefSHannes Frederic Sowa 
681bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp6_ehash_secret,
691bbdceefSHannes Frederic Sowa 			    sizeof(udp6_ehash_secret));
701bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ipv6_hash_secret,
711bbdceefSHannes Frederic Sowa 			    sizeof(udp_ipv6_hash_secret));
721bbdceefSHannes Frederic Sowa 
731bbdceefSHannes Frederic Sowa 	lhash = (__force u32)laddr->s6_addr32[3];
741bbdceefSHannes Frederic Sowa 	fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
751bbdceefSHannes Frederic Sowa 
76b50026b5SHannes Frederic Sowa 	return __inet6_ehashfn(lhash, lport, fhash, fport,
771bbdceefSHannes Frederic Sowa 			       udp_ipv6_hash_secret + net_hash_mix(net));
78b50026b5SHannes Frederic Sowa }
79b50026b5SHannes Frederic Sowa 
806ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
81db8dac20SDavid S. Miller {
8230fff923SEric Dumazet 	unsigned int hash2_nulladdr =
83f0b1e64cSMartin KaFai Lau 		ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
8430fff923SEric Dumazet 	unsigned int hash2_partial =
85f0b1e64cSMartin KaFai Lau 		ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
8630fff923SEric Dumazet 
87d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
8830fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
89fe38d2a1SJosef Bacik 	return udp_lib_get_port(sk, snum, hash2_nulladdr);
90db8dac20SDavid S. Miller }
91db8dac20SDavid S. Miller 
92f7c46156SAlexey Kodanev void udp_v6_rehash(struct sock *sk)
93719f8358SEric Dumazet {
94f0b1e64cSMartin KaFai Lau 	u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
95efe4208fSEric Dumazet 					  &sk->sk_v6_rcv_saddr,
96719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
97719f8358SEric Dumazet 
98719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
99719f8358SEric Dumazet }
100719f8358SEric Dumazet 
101d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net,
10288440ae7SBalazs Scheidler 			 const struct in6_addr *saddr, __be16 sport,
103d1e37288SSu, Xuemin 			 const struct in6_addr *daddr, unsigned short hnum,
10473545373STim Beale 			 int dif, int sdif)
105db8dac20SDavid S. Miller {
10660c04aecSJoe Perches 	int score;
10760c04aecSJoe Perches 	struct inet_sock *inet;
1086da5b0f0SMike Manning 	bool dev_match;
109db8dac20SDavid S. Miller 
11060c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
11160c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
11260c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
11360c04aecSJoe Perches 		return -1;
114645ca708SEric Dumazet 
11523b0269eSPeter Oskolkov 	if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
11623b0269eSPeter Oskolkov 		return -1;
11723b0269eSPeter Oskolkov 
118645ca708SEric Dumazet 	score = 0;
11960c04aecSJoe Perches 	inet = inet_sk(sk);
12060c04aecSJoe Perches 
121c720c7e8SEric Dumazet 	if (inet->inet_dport) {
122c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
123645ca708SEric Dumazet 			return -1;
124db8dac20SDavid S. Miller 		score++;
125db8dac20SDavid S. Miller 	}
12660c04aecSJoe Perches 
127efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
128efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
129645ca708SEric Dumazet 			return -1;
130db8dac20SDavid S. Miller 		score++;
131db8dac20SDavid S. Miller 	}
13260c04aecSJoe Perches 
1336da5b0f0SMike Manning 	dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
13469678bcdSPaolo Abeni 	if (!dev_match)
135645ca708SEric Dumazet 		return -1;
136db8dac20SDavid S. Miller 	score++;
13760c04aecSJoe Perches 
1387170a977SEric Dumazet 	if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
13970da268bSEric Dumazet 		score++;
14070da268bSEric Dumazet 
141645ca708SEric Dumazet 	return score;
142645ca708SEric Dumazet }
143645ca708SEric Dumazet 
144a57066b1SDavid S. Miller static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
1452a08748cSJakub Sitnicki 				     struct sk_buff *skb,
1462a08748cSJakub Sitnicki 				     const struct in6_addr *saddr,
1472a08748cSJakub Sitnicki 				     __be16 sport,
1482a08748cSJakub Sitnicki 				     const struct in6_addr *daddr,
1492a08748cSJakub Sitnicki 				     unsigned int hnum)
1502a08748cSJakub Sitnicki {
1512a08748cSJakub Sitnicki 	struct sock *reuse_sk = NULL;
1522a08748cSJakub Sitnicki 	u32 hash;
1532a08748cSJakub Sitnicki 
1542a08748cSJakub Sitnicki 	if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
1552a08748cSJakub Sitnicki 		hash = udp6_ehashfn(net, daddr, hnum, saddr, sport);
1562a08748cSJakub Sitnicki 		reuse_sk = reuseport_select_sock(sk, hash, skb,
1572a08748cSJakub Sitnicki 						 sizeof(struct udphdr));
1582a08748cSJakub Sitnicki 	}
1592a08748cSJakub Sitnicki 	return reuse_sk;
1602a08748cSJakub Sitnicki }
1612a08748cSJakub Sitnicki 
162d1e37288SSu, Xuemin /* called with rcu_read_lock() */
163fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
164fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
1651801b570SDavid Ahern 		const struct in6_addr *daddr, unsigned int hnum,
16673545373STim Beale 		int dif, int sdif, struct udp_hslot *hslot2,
16773545373STim Beale 		struct sk_buff *skb)
168fddc17deSEric Dumazet {
169fddc17deSEric Dumazet 	struct sock *sk, *result;
170e94a62f5SPaolo Abeni 	int score, badness;
171fddc17deSEric Dumazet 
172fddc17deSEric Dumazet 	result = NULL;
173fddc17deSEric Dumazet 	badness = -1;
174ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
175d1e37288SSu, Xuemin 		score = compute_score(sk, net, saddr, sport,
17673545373STim Beale 				      daddr, hnum, dif, sdif);
177fddc17deSEric Dumazet 		if (score > badness) {
1782a08748cSJakub Sitnicki 			result = lookup_reuseport(net, sk, skb,
1792a08748cSJakub Sitnicki 						  saddr, sport, daddr, hnum);
180a57066b1SDavid S. Miller 			/* Fall back to scoring if group has connections */
181a57066b1SDavid S. Miller 			if (result && !reuseport_has_conns(sk, false))
182ca065d0cSEric Dumazet 				return result;
1832a08748cSJakub Sitnicki 
184a57066b1SDavid S. Miller 			result = result ? : sk;
185ca065d0cSEric Dumazet 			badness = score;
186fddc17deSEric Dumazet 		}
187fddc17deSEric Dumazet 	}
188fddc17deSEric Dumazet 	return result;
189fddc17deSEric Dumazet }
190fddc17deSEric Dumazet 
1916d4201b1SJakub Sitnicki static inline struct sock *udp6_lookup_run_bpf(struct net *net,
1926d4201b1SJakub Sitnicki 					       struct udp_table *udptable,
1936d4201b1SJakub Sitnicki 					       struct sk_buff *skb,
1946d4201b1SJakub Sitnicki 					       const struct in6_addr *saddr,
1956d4201b1SJakub Sitnicki 					       __be16 sport,
1966d4201b1SJakub Sitnicki 					       const struct in6_addr *daddr,
1976d4201b1SJakub Sitnicki 					       u16 hnum)
1986d4201b1SJakub Sitnicki {
1996d4201b1SJakub Sitnicki 	struct sock *sk, *reuse_sk;
2006d4201b1SJakub Sitnicki 	bool no_reuseport;
2016d4201b1SJakub Sitnicki 
2026d4201b1SJakub Sitnicki 	if (udptable != &udp_table)
2036d4201b1SJakub Sitnicki 		return NULL; /* only UDP is supported */
2046d4201b1SJakub Sitnicki 
2056d4201b1SJakub Sitnicki 	no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP,
2066d4201b1SJakub Sitnicki 					    saddr, sport, daddr, hnum, &sk);
2076d4201b1SJakub Sitnicki 	if (no_reuseport || IS_ERR_OR_NULL(sk))
2086d4201b1SJakub Sitnicki 		return sk;
2096d4201b1SJakub Sitnicki 
2106d4201b1SJakub Sitnicki 	reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
211c64c9c28SJakub Sitnicki 	if (reuse_sk)
2126d4201b1SJakub Sitnicki 		sk = reuse_sk;
2136d4201b1SJakub Sitnicki 	return sk;
2146d4201b1SJakub Sitnicki }
2156d4201b1SJakub Sitnicki 
216ca065d0cSEric Dumazet /* rcu_read_lock() must be held */
217fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
21888440ae7SBalazs Scheidler 			       const struct in6_addr *saddr, __be16 sport,
21988440ae7SBalazs Scheidler 			       const struct in6_addr *daddr, __be16 dport,
2201801b570SDavid Ahern 			       int dif, int sdif, struct udp_table *udptable,
221538950a1SCraig Gallek 			       struct sk_buff *skb)
222645ca708SEric Dumazet {
223645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
22423b0269eSPeter Oskolkov 	unsigned int hash2, slot2;
22523b0269eSPeter Oskolkov 	struct udp_hslot *hslot2;
2266d4201b1SJakub Sitnicki 	struct sock *result, *sk;
227645ca708SEric Dumazet 
228f0b1e64cSMartin KaFai Lau 	hash2 = ipv6_portaddr_hash(net, daddr, hnum);
229fddc17deSEric Dumazet 	slot2 = hash2 & udptable->mask;
230fddc17deSEric Dumazet 	hslot2 = &udptable->hash2[slot2];
231fddc17deSEric Dumazet 
2326d4201b1SJakub Sitnicki 	/* Lookup connected or non-wildcard sockets */
233fddc17deSEric Dumazet 	result = udp6_lib_lookup2(net, saddr, sport,
23473545373STim Beale 				  daddr, hnum, dif, sdif,
235d1e37288SSu, Xuemin 				  hslot2, skb);
2366d4201b1SJakub Sitnicki 	if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
2376d4201b1SJakub Sitnicki 		goto done;
2386d4201b1SJakub Sitnicki 
2396d4201b1SJakub Sitnicki 	/* Lookup redirect from BPF */
2406d4201b1SJakub Sitnicki 	if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
2416d4201b1SJakub Sitnicki 		sk = udp6_lookup_run_bpf(net, udptable, skb,
2426d4201b1SJakub Sitnicki 					 saddr, sport, daddr, hnum);
2436d4201b1SJakub Sitnicki 		if (sk) {
2446d4201b1SJakub Sitnicki 			result = sk;
2456d4201b1SJakub Sitnicki 			goto done;
2466d4201b1SJakub Sitnicki 		}
2476d4201b1SJakub Sitnicki 	}
2486d4201b1SJakub Sitnicki 
2496d4201b1SJakub Sitnicki 	/* Got non-wildcard socket or error on first lookup */
2506d4201b1SJakub Sitnicki 	if (result)
2516d4201b1SJakub Sitnicki 		goto done;
2526d4201b1SJakub Sitnicki 
2536d4201b1SJakub Sitnicki 	/* Lookup wildcard sockets */
254f0b1e64cSMartin KaFai Lau 	hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
255fddc17deSEric Dumazet 	slot2 = hash2 & udptable->mask;
256fddc17deSEric Dumazet 	hslot2 = &udptable->hash2[slot2];
257fddc17deSEric Dumazet 
2581223c67cSJorge Boncompte [DTI2] 	result = udp6_lib_lookup2(net, saddr, sport,
25923b0269eSPeter Oskolkov 				  &in6addr_any, hnum, dif, sdif,
26073545373STim Beale 				  hslot2, skb);
2616d4201b1SJakub Sitnicki done:
26226f8113cSEnrico Weigelt 	if (IS_ERR(result))
2638217ca65SMartin KaFai Lau 		return NULL;
264fddc17deSEric Dumazet 	return result;
265fddc17deSEric Dumazet }
266fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
267db8dac20SDavid S. Miller 
268607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
269607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
270645ca708SEric Dumazet 					  struct udp_table *udptable)
271607c4aafSKOVACS Krisztian {
272b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
273607c4aafSKOVACS Krisztian 
274ed7cbbceSAlexander Duyck 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
275607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
2761801b570SDavid Ahern 				 inet6_sdif(skb), udptable, skb);
277607c4aafSKOVACS Krisztian }
278607c4aafSKOVACS Krisztian 
279*7b58e63eSEric Dumazet struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
28063058308STom Herbert 				 __be16 sport, __be16 dport)
28163058308STom Herbert {
28263058308STom Herbert 	const struct ipv6hdr *iph = ipv6_hdr(skb);
28363058308STom Herbert 
284ed7cbbceSAlexander Duyck 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
28563058308STom Herbert 				 &iph->daddr, dport, inet6_iif(skb),
286257a525fSMartin KaFai Lau 				 inet6_sdif(skb), &udp_table, NULL);
28763058308STom Herbert }
28863058308STom Herbert EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
28963058308STom Herbert 
290ca065d0cSEric Dumazet /* Must be called under rcu_read_lock().
291ca065d0cSEric Dumazet  * Does increment socket refcount.
292ca065d0cSEric Dumazet  */
2936e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
294aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
295aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
296aa976fc0SBalazs Scheidler {
297ca065d0cSEric Dumazet 	struct sock *sk;
298ca065d0cSEric Dumazet 
299ca065d0cSEric Dumazet 	sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
3001801b570SDavid Ahern 				dif, 0, &udp_table, NULL);
30141c6d650SReshetova, Elena 	if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
302ca065d0cSEric Dumazet 		sk = NULL;
303ca065d0cSEric Dumazet 	return sk;
304aa976fc0SBalazs Scheidler }
305aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
306ca065d0cSEric Dumazet #endif
307aa976fc0SBalazs Scheidler 
308cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the
309cb891fa6SPaolo Abeni  * scratch area space; note that the IP6CB flags is still in the first
310cb891fa6SPaolo Abeni  * cacheline, so checking for jumbograms is cheap
311cb891fa6SPaolo Abeni  */
312cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb)
313cb891fa6SPaolo Abeni {
314cb891fa6SPaolo Abeni 	return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
315cb891fa6SPaolo Abeni }
316cb891fa6SPaolo Abeni 
317db8dac20SDavid S. Miller /*
318db8dac20SDavid S. Miller  *	This should be easy, if there is something there we
319db8dac20SDavid S. Miller  *	return it, otherwise we block.
320db8dac20SDavid S. Miller  */
321db8dac20SDavid S. Miller 
3221b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
323db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
324db8dac20SDavid S. Miller {
325db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
326db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
327db8dac20SDavid S. Miller 	struct sk_buff *skb;
32859c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
329fd69c399SPaolo Abeni 	int off, err, peeking = flags & MSG_PEEK;
330db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
331543fc3fbSPaolo Abeni 	struct udp_mib __percpu *mib;
332197c949eSEric Dumazet 	bool checksum_valid = false;
333f26ba175SWei Yongjun 	int is_udp4;
334db8dac20SDavid S. Miller 
335db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
33685fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
337db8dac20SDavid S. Miller 
3384b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
33985fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
3404b340ae2SBrian Haley 
341db8dac20SDavid S. Miller try_again:
342a0917e0bSMatthew Dawson 	off = sk_peek_offset(sk, flags);
343fd69c399SPaolo Abeni 	skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
344db8dac20SDavid S. Miller 	if (!skb)
345627d2d6bSsamanthakumar 		return err;
346db8dac20SDavid S. Miller 
347cb891fa6SPaolo Abeni 	ulen = udp6_skb_len(skb);
34859c2cdaeSDavid S. Miller 	copied = len;
349627d2d6bSsamanthakumar 	if (copied > ulen - off)
350627d2d6bSsamanthakumar 		copied = ulen - off;
35159c2cdaeSDavid S. Miller 	else if (copied < ulen)
352db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
353db8dac20SDavid S. Miller 
354f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
355029a3743SPaolo Abeni 	mib = __UDPX_MIB(sk, is_udp4);
356f26ba175SWei Yongjun 
357db8dac20SDavid S. Miller 	/*
358db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
359db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
360db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
361db8dac20SDavid S. Miller 	 */
362db8dac20SDavid S. Miller 
363d21dbdfeSEric Dumazet 	if (copied < ulen || peeking ||
364d21dbdfeSEric Dumazet 	    (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
36567a51780SPaolo Abeni 		checksum_valid = udp_skb_csum_unnecessary(skb) ||
36667a51780SPaolo Abeni 				!__udp_lib_checksum_complete(skb);
367197c949eSEric Dumazet 		if (!checksum_valid)
368db8dac20SDavid S. Miller 			goto csum_copy_err;
369db8dac20SDavid S. Miller 	}
370db8dac20SDavid S. Miller 
37167a51780SPaolo Abeni 	if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
37267a51780SPaolo Abeni 		if (udp_skb_is_linear(skb))
37367a51780SPaolo Abeni 			err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
37467a51780SPaolo Abeni 		else
375627d2d6bSsamanthakumar 			err = skb_copy_datagram_msg(skb, off, msg, copied);
37667a51780SPaolo Abeni 	} else {
377627d2d6bSsamanthakumar 		err = skb_copy_and_csum_datagram_msg(skb, off, msg);
378db8dac20SDavid S. Miller 		if (err == -EINVAL)
379db8dac20SDavid S. Miller 			goto csum_copy_err;
380db8dac20SDavid S. Miller 	}
38122911fc5SEric Dumazet 	if (unlikely(err)) {
382fd69c399SPaolo Abeni 		if (!peeking) {
383979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
384029a3743SPaolo Abeni 			SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
385979402b1SEric Dumazet 		}
386850cbaddSPaolo Abeni 		kfree_skb(skb);
387627d2d6bSsamanthakumar 		return err;
38822911fc5SEric Dumazet 	}
389fd69c399SPaolo Abeni 	if (!peeking)
390029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
391db8dac20SDavid S. Miller 
3923b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
393db8dac20SDavid S. Miller 
394db8dac20SDavid S. Miller 	/* Copy the address. */
395db8dac20SDavid S. Miller 	if (msg->msg_name) {
396342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
397db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
398db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
399db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
400db8dac20SDavid S. Miller 
401842df073SHannes Frederic Sowa 		if (is_udp4) {
402b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
403b301e82cSBrian Haley 					       &sin6->sin6_addr);
404842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
405842df073SHannes Frederic Sowa 		} else {
4064e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
407842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
408842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
4094330487aSDuan Jiong 						    inet6_iif(skb));
410db8dac20SDavid S. Miller 		}
411bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
412983695faSDaniel Borkmann 
413983695faSDaniel Borkmann 		if (cgroup_bpf_enabled)
414983695faSDaniel Borkmann 			BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
415983695faSDaniel Borkmann 						(struct sockaddr *)sin6);
416db8dac20SDavid S. Miller 	}
4174b261c75SHannes Frederic Sowa 
418bcd1665eSPaolo Abeni 	if (udp_sk(sk)->gro_enabled)
419bcd1665eSPaolo Abeni 		udp_cmsg_recv(msg, sk, skb);
420bcd1665eSPaolo Abeni 
4214b261c75SHannes Frederic Sowa 	if (np->rxopt.all)
4224b261c75SHannes Frederic Sowa 		ip6_datagram_recv_common_ctl(sk, msg, skb);
4234b261c75SHannes Frederic Sowa 
424f26ba175SWei Yongjun 	if (is_udp4) {
425db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
426ad959036SPaolo Abeni 			ip_cmsg_recv_offset(msg, sk, skb,
42710df8e61SEric Dumazet 					    sizeof(struct udphdr), off);
428db8dac20SDavid S. Miller 	} else {
429db8dac20SDavid S. Miller 		if (np->rxopt.all)
4304b261c75SHannes Frederic Sowa 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
431db8dac20SDavid S. Miller 	}
432db8dac20SDavid S. Miller 
43359c2cdaeSDavid S. Miller 	err = copied;
434db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
435db8dac20SDavid S. Miller 		err = ulen;
436db8dac20SDavid S. Miller 
437850cbaddSPaolo Abeni 	skb_consume_udp(sk, skb, peeking ? -err : err);
438db8dac20SDavid S. Miller 	return err;
439db8dac20SDavid S. Miller 
440db8dac20SDavid S. Miller csum_copy_err:
4412276f58aSPaolo Abeni 	if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
4422276f58aSPaolo Abeni 				 udp_skb_destructor)) {
443029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
444029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
4456a5dc9e5SEric Dumazet 	}
446850cbaddSPaolo Abeni 	kfree_skb(skb);
447db8dac20SDavid S. Miller 
448beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
449beb39db5SEric Dumazet 	cond_resched();
4509cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
451db8dac20SDavid S. Miller 	goto try_again;
452db8dac20SDavid S. Miller }
453db8dac20SDavid S. Miller 
454a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
455a36e185eSStefano Brivio void udpv6_encap_enable(void)
456a36e185eSStefano Brivio {
4579c480601SPaolo Abeni 	static_branch_inc(&udpv6_encap_needed_key);
458a36e185eSStefano Brivio }
459a36e185eSStefano Brivio EXPORT_SYMBOL(udpv6_encap_enable);
460a36e185eSStefano Brivio 
461e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
462e7cc0824SStefano Brivio  * through error handlers in encapsulations looking for a match.
463e7cc0824SStefano Brivio  */
464e7cc0824SStefano Brivio static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
465e7cc0824SStefano Brivio 				      struct inet6_skb_parm *opt,
466424a7cd0SPaolo Abeni 				      u8 type, u8 code, int offset, __be32 info)
467e7cc0824SStefano Brivio {
468e7cc0824SStefano Brivio 	int i;
469e7cc0824SStefano Brivio 
470e7cc0824SStefano Brivio 	for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
471e7cc0824SStefano Brivio 		int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
472424a7cd0SPaolo Abeni 			       u8 type, u8 code, int offset, __be32 info);
473424a7cd0SPaolo Abeni 		const struct ip6_tnl_encap_ops *encap;
474e7cc0824SStefano Brivio 
475424a7cd0SPaolo Abeni 		encap = rcu_dereference(ip6tun_encaps[i]);
476424a7cd0SPaolo Abeni 		if (!encap)
477e7cc0824SStefano Brivio 			continue;
478424a7cd0SPaolo Abeni 		handler = encap->err_handler;
479e7cc0824SStefano Brivio 		if (handler && !handler(skb, opt, type, code, offset, info))
480e7cc0824SStefano Brivio 			return 0;
481e7cc0824SStefano Brivio 	}
482e7cc0824SStefano Brivio 
483e7cc0824SStefano Brivio 	return -ENOENT;
484e7cc0824SStefano Brivio }
485e7cc0824SStefano Brivio 
486a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without
487a36e185eSStefano Brivio  * reversing source and destination port: this will match tunnels that force the
488a36e185eSStefano Brivio  * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
489a36e185eSStefano Brivio  * lwtunnels might actually break this assumption by being configured with
490a36e185eSStefano Brivio  * different destination ports on endpoints, in this case we won't be able to
491a36e185eSStefano Brivio  * trace ICMP messages back to them.
492a36e185eSStefano Brivio  *
493e7cc0824SStefano Brivio  * If this doesn't match any socket, probe tunnels with arbitrary destination
494e7cc0824SStefano Brivio  * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
495e7cc0824SStefano Brivio  * we've sent packets to won't necessarily match the local destination port.
496e7cc0824SStefano Brivio  *
497a36e185eSStefano Brivio  * Then ask the tunnel implementation to match the error against a valid
498a36e185eSStefano Brivio  * association.
499a36e185eSStefano Brivio  *
500e7cc0824SStefano Brivio  * Return an error if we can't find a match, the socket if we need further
501e7cc0824SStefano Brivio  * processing, zero otherwise.
502a36e185eSStefano Brivio  */
503a36e185eSStefano Brivio static struct sock *__udp6_lib_err_encap(struct net *net,
504a36e185eSStefano Brivio 					 const struct ipv6hdr *hdr, int offset,
505a36e185eSStefano Brivio 					 struct udphdr *uh,
506a36e185eSStefano Brivio 					 struct udp_table *udptable,
507e7cc0824SStefano Brivio 					 struct sk_buff *skb,
508e7cc0824SStefano Brivio 					 struct inet6_skb_parm *opt,
509e7cc0824SStefano Brivio 					 u8 type, u8 code, __be32 info)
510a36e185eSStefano Brivio {
511a36e185eSStefano Brivio 	int network_offset, transport_offset;
512a36e185eSStefano Brivio 	struct sock *sk;
513a36e185eSStefano Brivio 
514a36e185eSStefano Brivio 	network_offset = skb_network_offset(skb);
515a36e185eSStefano Brivio 	transport_offset = skb_transport_offset(skb);
516a36e185eSStefano Brivio 
517a36e185eSStefano Brivio 	/* Network header needs to point to the outer IPv6 header inside ICMP */
518a36e185eSStefano Brivio 	skb_reset_network_header(skb);
519a36e185eSStefano Brivio 
520a36e185eSStefano Brivio 	/* Transport header needs to point to the UDP header */
521a36e185eSStefano Brivio 	skb_set_transport_header(skb, offset);
522a36e185eSStefano Brivio 
523e7cc0824SStefano Brivio 	sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
524e7cc0824SStefano Brivio 			       &hdr->saddr, uh->dest,
525e7cc0824SStefano Brivio 			       inet6_iif(skb), 0, udptable, skb);
526e7cc0824SStefano Brivio 	if (sk) {
527e7cc0824SStefano Brivio 		int (*lookup)(struct sock *sk, struct sk_buff *skb);
528e7cc0824SStefano Brivio 		struct udp_sock *up = udp_sk(sk);
529e7cc0824SStefano Brivio 
530a36e185eSStefano Brivio 		lookup = READ_ONCE(up->encap_err_lookup);
531a36e185eSStefano Brivio 		if (!lookup || lookup(sk, skb))
532a36e185eSStefano Brivio 			sk = NULL;
533e7cc0824SStefano Brivio 	}
534e7cc0824SStefano Brivio 
535e7cc0824SStefano Brivio 	if (!sk) {
536e7cc0824SStefano Brivio 		sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
537e7cc0824SStefano Brivio 							offset, info));
538e7cc0824SStefano Brivio 	}
539a36e185eSStefano Brivio 
540a36e185eSStefano Brivio 	skb_set_transport_header(skb, transport_offset);
541a36e185eSStefano Brivio 	skb_set_network_header(skb, network_offset);
542e7cc0824SStefano Brivio 
543a36e185eSStefano Brivio 	return sk;
544a36e185eSStefano Brivio }
545a36e185eSStefano Brivio 
54632bbd879SStefano Brivio int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
547d5fdd6baSBrian Haley 		   u8 type, u8 code, int offset, __be32 info,
548645ca708SEric Dumazet 		   struct udp_table *udptable)
549db8dac20SDavid S. Miller {
550db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
551b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
552b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
553b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
554db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
555a36e185eSStefano Brivio 	bool tunnel = false;
556db8dac20SDavid S. Miller 	struct sock *sk;
557e0d8c1b7SWei Wang 	int harderr;
558db8dac20SDavid S. Miller 	int err;
5597304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
560db8dac20SDavid S. Miller 
561e32ea7e7SCraig Gallek 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
5624ac30c4bSMartin KaFai Lau 			       inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
563d26796aeSXin Long 	if (!sk || udp_sk(sk)->encap_type) {
564a36e185eSStefano Brivio 		/* No socket for error: try tunnels before discarding */
565e7cc0824SStefano Brivio 		sk = ERR_PTR(-ENOENT);
566a36e185eSStefano Brivio 		if (static_branch_unlikely(&udpv6_encap_needed_key)) {
567a36e185eSStefano Brivio 			sk = __udp6_lib_err_encap(net, hdr, offset, uh,
568e7cc0824SStefano Brivio 						  udptable, skb,
569e7cc0824SStefano Brivio 						  opt, type, code, info);
570e7cc0824SStefano Brivio 			if (!sk)
571e7cc0824SStefano Brivio 				return 0;
572a36e185eSStefano Brivio 		}
573a36e185eSStefano Brivio 
574e7cc0824SStefano Brivio 		if (IS_ERR(sk)) {
575a16292a0SEric Dumazet 			__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
5767304fe46SDuan Jiong 					  ICMP6_MIB_INERRORS);
577e7cc0824SStefano Brivio 			return PTR_ERR(sk);
5787304fe46SDuan Jiong 		}
579e7cc0824SStefano Brivio 
580a36e185eSStefano Brivio 		tunnel = true;
581a36e185eSStefano Brivio 	}
582db8dac20SDavid S. Miller 
583e0d8c1b7SWei Wang 	harderr = icmpv6_err_convert(type, code, &err);
584e0d8c1b7SWei Wang 	np = inet6_sk(sk);
585e0d8c1b7SWei Wang 
58693b36cf3SHannes Frederic Sowa 	if (type == ICMPV6_PKT_TOOBIG) {
58793b36cf3SHannes Frederic Sowa 		if (!ip6_sk_accept_pmtu(sk))
58893b36cf3SHannes Frederic Sowa 			goto out;
58981aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
590e0d8c1b7SWei Wang 		if (np->pmtudisc != IPV6_PMTUDISC_DONT)
591e0d8c1b7SWei Wang 			harderr = 1;
59293b36cf3SHannes Frederic Sowa 	}
5931a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
594a36e185eSStefano Brivio 		if (tunnel) {
595a36e185eSStefano Brivio 			ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
596a36e185eSStefano Brivio 				     sk->sk_mark, sk->sk_uid);
597a36e185eSStefano Brivio 		} else {
598ec18d9a2SDavid S. Miller 			ip6_sk_redirect(skb, sk);
599a36e185eSStefano Brivio 		}
6001a462d18SDuan Jiong 		goto out;
6011a462d18SDuan Jiong 	}
60281aded24SDavid S. Miller 
603a36e185eSStefano Brivio 	/* Tunnels don't have an application socket: don't pass errors back */
604a36e185eSStefano Brivio 	if (tunnel)
605a36e185eSStefano Brivio 		goto out;
606a36e185eSStefano Brivio 
607e0d8c1b7SWei Wang 	if (!np->recverr) {
608e0d8c1b7SWei Wang 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
609db8dac20SDavid S. Miller 			goto out;
610e0d8c1b7SWei Wang 	} else {
611db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
612e0d8c1b7SWei Wang 	}
613b1faf566SEric Dumazet 
614db8dac20SDavid S. Miller 	sk->sk_err = err;
615db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
616db8dac20SDavid S. Miller out:
61732bbd879SStefano Brivio 	return 0;
618db8dac20SDavid S. Miller }
619db8dac20SDavid S. Miller 
620a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
621f7ad74feSBenjamin LaHaise {
622f7ad74feSBenjamin LaHaise 	int rc;
623f7ad74feSBenjamin LaHaise 
624efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
625f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
626005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
6272c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
628e68b6e50SEric Dumazet 	} else {
629e68b6e50SEric Dumazet 		sk_mark_napi_id_once(sk, skb);
630005ec974SShawn Bohrer 	}
631f7ad74feSBenjamin LaHaise 
632850cbaddSPaolo Abeni 	rc = __udp_enqueue_schedule_skb(sk, skb);
633f7ad74feSBenjamin LaHaise 	if (rc < 0) {
634f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
635f7ad74feSBenjamin LaHaise 
636f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
637f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
638e61da9e2SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
639f7ad74feSBenjamin LaHaise 					 UDP_MIB_RCVBUFERRORS, is_udplite);
640a3ce2b10SMenglong Dong 		else
641a3ce2b10SMenglong Dong 			UDP6_INC_STATS(sock_net(sk),
642a3ce2b10SMenglong Dong 				       UDP_MIB_MEMERRORS, is_udplite);
643e61da9e2SEric Dumazet 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
644f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
645f7ad74feSBenjamin LaHaise 		return -1;
646f7ad74feSBenjamin LaHaise 	}
647850cbaddSPaolo Abeni 
648f7ad74feSBenjamin LaHaise 	return 0;
649f7ad74feSBenjamin LaHaise }
650f7ad74feSBenjamin LaHaise 
65132bbd879SStefano Brivio static __inline__ int udpv6_err(struct sk_buff *skb,
652d5fdd6baSBrian Haley 				struct inet6_skb_parm *opt, u8 type,
653d5fdd6baSBrian Haley 				u8 code, int offset, __be32 info)
654db8dac20SDavid S. Miller {
65532bbd879SStefano Brivio 	return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
656db8dac20SDavid S. Miller }
657db8dac20SDavid S. Miller 
658cf329aa4SPaolo Abeni static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
659db8dac20SDavid S. Miller {
660db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
661db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
662db8dac20SDavid S. Miller 
663db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
664db8dac20SDavid S. Miller 		goto drop;
665db8dac20SDavid S. Miller 
66688ab3108SDavidlohr Bueso 	if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
667d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
668d7f3f621SBenjamin LaHaise 
669d7f3f621SBenjamin LaHaise 		/*
670d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
671d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
672d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
673d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
674d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
675d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
676d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
677d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
678d7f3f621SBenjamin LaHaise 		 */
679d7f3f621SBenjamin LaHaise 
680d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
6816aa7de05SMark Rutland 		encap_rcv = READ_ONCE(up->encap_rcv);
682e5aed006SHannes Frederic Sowa 		if (encap_rcv) {
683d7f3f621SBenjamin LaHaise 			int ret;
684d7f3f621SBenjamin LaHaise 
6850a80966bSTom Herbert 			/* Verify checksum before giving to encap */
6860a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
6870a80966bSTom Herbert 				goto csum_error;
6880a80966bSTom Herbert 
689d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
690d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
69102c22347SEric Dumazet 				__UDP_INC_STATS(sock_net(sk),
692d7f3f621SBenjamin LaHaise 						UDP_MIB_INDATAGRAMS,
693d7f3f621SBenjamin LaHaise 						is_udplite);
694d7f3f621SBenjamin LaHaise 				return -ret;
695d7f3f621SBenjamin LaHaise 			}
696d7f3f621SBenjamin LaHaise 		}
697d7f3f621SBenjamin LaHaise 
698d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
699d7f3f621SBenjamin LaHaise 	}
700d7f3f621SBenjamin LaHaise 
701db8dac20SDavid S. Miller 	/*
702db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
703db8dac20SDavid S. Miller 	 */
704b0a42277SMiaohe Lin 	if ((up->pcflag & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
705db8dac20SDavid S. Miller 
706db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
707ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
708db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
709db8dac20SDavid S. Miller 			goto drop;
710db8dac20SDavid S. Miller 		}
711db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
712ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
713db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
714db8dac20SDavid S. Miller 			goto drop;
715db8dac20SDavid S. Miller 		}
716db8dac20SDavid S. Miller 	}
717db8dac20SDavid S. Miller 
7184b943faeSPaolo Abeni 	prefetch(&sk->sk_rmem_alloc);
719ce25d66aSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
720ce25d66aSEric Dumazet 	    udp_lib_checksum_complete(skb))
7216a5dc9e5SEric Dumazet 		goto csum_error;
722ce25d66aSEric Dumazet 
723ba66bbe5SDaniel Borkmann 	if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
724a6127697SMichal Kubeček 		goto drop;
725db8dac20SDavid S. Miller 
726e6afc8acSsamanthakumar 	udp_csum_pull_header(skb);
727cb80ef46SBenjamin LaHaise 
728d826eb14SEric Dumazet 	skb_dst_drop(skb);
729db8dac20SDavid S. Miller 
730850cbaddSPaolo Abeni 	return __udpv6_queue_rcv_skb(sk, skb);
7313e215c8dSJames M Leddy 
7326a5dc9e5SEric Dumazet csum_error:
73302c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
734db8dac20SDavid S. Miller drop:
73502c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
736cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
737db8dac20SDavid S. Miller 	kfree_skb(skb);
738db8dac20SDavid S. Miller 	return -1;
739db8dac20SDavid S. Miller }
740db8dac20SDavid S. Miller 
741cf329aa4SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
742cf329aa4SPaolo Abeni {
743cf329aa4SPaolo Abeni 	struct sk_buff *next, *segs;
744cf329aa4SPaolo Abeni 	int ret;
745cf329aa4SPaolo Abeni 
746cf329aa4SPaolo Abeni 	if (likely(!udp_unexpected_gso(sk, skb)))
747cf329aa4SPaolo Abeni 		return udpv6_queue_rcv_one_skb(sk, skb);
748cf329aa4SPaolo Abeni 
749cf329aa4SPaolo Abeni 	__skb_push(skb, -skb_mac_offset(skb));
750cf329aa4SPaolo Abeni 	segs = udp_rcv_segment(sk, skb, false);
7511a186c14SJason A. Donenfeld 	skb_list_walk_safe(segs, skb, next) {
752cf329aa4SPaolo Abeni 		__skb_pull(skb, skb_transport_offset(skb));
753cf329aa4SPaolo Abeni 
754cf329aa4SPaolo Abeni 		ret = udpv6_queue_rcv_one_skb(sk, skb);
755cf329aa4SPaolo Abeni 		if (ret > 0)
756cf329aa4SPaolo Abeni 			ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
757cf329aa4SPaolo Abeni 						 true);
758cf329aa4SPaolo Abeni 	}
759cf329aa4SPaolo Abeni 	return 0;
760cf329aa4SPaolo Abeni }
761cf329aa4SPaolo Abeni 
7625cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
763b71d1d42SEric Dumazet 				   __be16 loc_port, const struct in6_addr *loc_addr,
764b71d1d42SEric Dumazet 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
7657bd2db40SDewi Morgan 				   int dif, int sdif, unsigned short hnum)
766db8dac20SDavid S. Miller {
7679e89fd8bSSven Wegener 	struct inet_sock *inet = inet_sk(sk);
768db8dac20SDavid S. Miller 
7699e89fd8bSSven Wegener 	if (!net_eq(sock_net(sk), net))
7705cf3d461SDavid Held 		return false;
771b8ad0cbcSDaniel Lezcano 
7725cf3d461SDavid Held 	if (udp_sk(sk)->udp_port_hash != hnum ||
7735cf3d461SDavid Held 	    sk->sk_family != PF_INET6 ||
7745cf3d461SDavid Held 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
7755cf3d461SDavid Held 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
7765cf3d461SDavid Held 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
7777bd2db40SDewi Morgan 	    !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) ||
77833b4b015SHenning Rogge 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
77933b4b015SHenning Rogge 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
7805cf3d461SDavid Held 		return false;
7819e89fd8bSSven Wegener 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
7825cf3d461SDavid Held 		return false;
7835cf3d461SDavid Held 	return true;
784db8dac20SDavid S. Miller }
785db8dac20SDavid S. Miller 
7864068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
7874068579eSTom Herbert {
7884068579eSTom Herbert 	/* RFC 2460 section 8.1 says that we SHOULD log
7894068579eSTom Herbert 	 * this error. Well, it is reasonable.
7904068579eSTom Herbert 	 */
791ba7a46f1SJoe Perches 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
7924068579eSTom Herbert 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
7934068579eSTom Herbert 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
7944068579eSTom Herbert }
7954068579eSTom Herbert 
796db8dac20SDavid S. Miller /*
797db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
798db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
799db8dac20SDavid S. Miller  */
800e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
801b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
80236cbb245SRick Jones 		struct udp_table *udptable, int proto)
803db8dac20SDavid S. Miller {
804ca065d0cSEric Dumazet 	struct sock *sk, *first = NULL;
805db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
8065cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
8075cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
808ca065d0cSEric Dumazet 	unsigned int offset = offsetof(typeof(*sk), sk_node);
8092dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
810ca065d0cSEric Dumazet 	int dif = inet6_iif(skb);
8117bd2db40SDewi Morgan 	int sdif = inet6_sdif(skb);
812ca065d0cSEric Dumazet 	struct hlist_node *node;
813ca065d0cSEric Dumazet 	struct sk_buff *nskb;
8142dc41cffSDavid Held 
8152dc41cffSDavid Held 	if (use_hash2) {
816f0b1e64cSMartin KaFai Lau 		hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
81773e2d5e3SPablo Neira 			    udptable->mask;
818f0b1e64cSMartin KaFai Lau 		hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
8192dc41cffSDavid Held start_lookup:
82073e2d5e3SPablo Neira 		hslot = &udptable->hash2[hash2];
8212dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
8222dc41cffSDavid Held 	}
823db8dac20SDavid S. Miller 
824ca065d0cSEric Dumazet 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
825ca065d0cSEric Dumazet 		if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
8267bd2db40SDewi Morgan 					    uh->source, saddr, dif, sdif,
8277bd2db40SDewi Morgan 					    hnum))
828ca065d0cSEric Dumazet 			continue;
8291c19448cSTom Herbert 		/* If zero checksum and no_check is not on for
8304068579eSTom Herbert 		 * the socket then skip it.
8314068579eSTom Herbert 		 */
832ca065d0cSEric Dumazet 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
833ca065d0cSEric Dumazet 			continue;
834ca065d0cSEric Dumazet 		if (!first) {
835ca065d0cSEric Dumazet 			first = sk;
836ca065d0cSEric Dumazet 			continue;
837db8dac20SDavid S. Miller 		}
838ca065d0cSEric Dumazet 		nskb = skb_clone(skb, GFP_ATOMIC);
839ca065d0cSEric Dumazet 		if (unlikely(!nskb)) {
840ca065d0cSEric Dumazet 			atomic_inc(&sk->sk_drops);
84102c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
842ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
84302c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_INERRORS,
844ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
845ca065d0cSEric Dumazet 			continue;
846a1ab77f9SEric Dumazet 		}
847db8dac20SDavid S. Miller 
848ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(sk, nskb) > 0)
849ca065d0cSEric Dumazet 			consume_skb(nskb);
850ca065d0cSEric Dumazet 	}
851a1ab77f9SEric Dumazet 
8522dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
8532dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
8542dc41cffSDavid Held 		hash2 = hash2_any;
8552dc41cffSDavid Held 		goto start_lookup;
8562dc41cffSDavid Held 	}
8572dc41cffSDavid Held 
858ca065d0cSEric Dumazet 	if (first) {
859ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(first, skb) > 0)
860ca065d0cSEric Dumazet 			consume_skb(skb);
861a1ab77f9SEric Dumazet 	} else {
862ca065d0cSEric Dumazet 		kfree_skb(skb);
86302c22347SEric Dumazet 		__UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
86436cbb245SRick Jones 				 proto == IPPROTO_UDPLITE);
865a1ab77f9SEric Dumazet 	}
866db8dac20SDavid S. Miller 	return 0;
867db8dac20SDavid S. Miller }
868db8dac20SDavid S. Miller 
86964f0f5d1SPaolo Abeni static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
87064f0f5d1SPaolo Abeni {
87164f0f5d1SPaolo Abeni 	if (udp_sk_rx_dst_set(sk, dst)) {
87264f0f5d1SPaolo Abeni 		const struct rt6_info *rt = (const struct rt6_info *)dst;
87364f0f5d1SPaolo Abeni 
87464f0f5d1SPaolo Abeni 		inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt);
87564f0f5d1SPaolo Abeni 	}
87664f0f5d1SPaolo Abeni }
87764f0f5d1SPaolo Abeni 
878eb63f296SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
879eb63f296SPaolo Abeni  * return code conversion for ip layer consumption
880eb63f296SPaolo Abeni  */
881eb63f296SPaolo Abeni static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
882eb63f296SPaolo Abeni 				struct udphdr *uh)
883eb63f296SPaolo Abeni {
884eb63f296SPaolo Abeni 	int ret;
885eb63f296SPaolo Abeni 
886eb63f296SPaolo Abeni 	if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
887e4aa33adSLi RongQing 		skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
888eb63f296SPaolo Abeni 
889eb63f296SPaolo Abeni 	ret = udpv6_queue_rcv_skb(sk, skb);
890eb63f296SPaolo Abeni 
89184dad559SPaolo Abeni 	/* a return value > 0 means to resubmit the input */
892eb63f296SPaolo Abeni 	if (ret > 0)
89384dad559SPaolo Abeni 		return ret;
894eb63f296SPaolo Abeni 	return 0;
895eb63f296SPaolo Abeni }
896eb63f296SPaolo Abeni 
897645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
898db8dac20SDavid S. Miller 		   int proto)
899db8dac20SDavid S. Miller {
900b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
901ca065d0cSEric Dumazet 	struct net *net = dev_net(skb->dev);
902ca065d0cSEric Dumazet 	struct udphdr *uh;
903ca065d0cSEric Dumazet 	struct sock *sk;
90471489e21SJoe Stringer 	bool refcounted;
905db8dac20SDavid S. Miller 	u32 ulen = 0;
906db8dac20SDavid S. Miller 
907db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
908d6bc0149SBjørn Mork 		goto discard;
909db8dac20SDavid S. Miller 
910db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
911db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
912db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
913db8dac20SDavid S. Miller 
914db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
915db8dac20SDavid S. Miller 	if (ulen > skb->len)
916db8dac20SDavid S. Miller 		goto short_packet;
917db8dac20SDavid S. Miller 
918db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
919db8dac20SDavid S. Miller 		/* UDP validates ulen. */
920db8dac20SDavid S. Miller 
921db8dac20SDavid S. Miller 		/* Check for jumbo payload */
922db8dac20SDavid S. Miller 		if (ulen == 0)
923db8dac20SDavid S. Miller 			ulen = skb->len;
924db8dac20SDavid S. Miller 
925db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
926db8dac20SDavid S. Miller 			goto short_packet;
927db8dac20SDavid S. Miller 
928db8dac20SDavid S. Miller 		if (ulen < skb->len) {
929db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
930db8dac20SDavid S. Miller 				goto short_packet;
931db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
932db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
933db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
934db8dac20SDavid S. Miller 		}
935db8dac20SDavid S. Miller 	}
936db8dac20SDavid S. Miller 
937db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
9386a5dc9e5SEric Dumazet 		goto csum_error;
939db8dac20SDavid S. Miller 
940c9f2c1aeSPaolo Abeni 	/* Check if the socket is already available, e.g. due to early demux */
94171489e21SJoe Stringer 	sk = skb_steal_sock(skb, &refcounted);
942c9f2c1aeSPaolo Abeni 	if (sk) {
943c9f2c1aeSPaolo Abeni 		struct dst_entry *dst = skb_dst(skb);
944c9f2c1aeSPaolo Abeni 		int ret;
945c9f2c1aeSPaolo Abeni 
946c9f2c1aeSPaolo Abeni 		if (unlikely(sk->sk_rx_dst != dst))
94764f0f5d1SPaolo Abeni 			udp6_sk_rx_dst_set(sk, dst);
948c9f2c1aeSPaolo Abeni 
949eb63f296SPaolo Abeni 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
95071489e21SJoe Stringer 			if (refcounted)
951c9f2c1aeSPaolo Abeni 				sock_put(sk);
952eb63f296SPaolo Abeni 			goto report_csum_error;
953eb63f296SPaolo Abeni 		}
954c9f2c1aeSPaolo Abeni 
955eb63f296SPaolo Abeni 		ret = udp6_unicast_rcv_skb(sk, skb, uh);
95671489e21SJoe Stringer 		if (refcounted)
957eb63f296SPaolo Abeni 			sock_put(sk);
958c9f2c1aeSPaolo Abeni 		return ret;
959c9f2c1aeSPaolo Abeni 	}
960c9f2c1aeSPaolo Abeni 
961db8dac20SDavid S. Miller 	/*
962db8dac20SDavid S. Miller 	 *	Multicast receive code
963db8dac20SDavid S. Miller 	 */
964db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
965e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
96636cbb245SRick Jones 				saddr, daddr, udptable, proto);
967db8dac20SDavid S. Miller 
968db8dac20SDavid S. Miller 	/* Unicast */
969607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
97053b24b8fSIan Morris 	if (sk) {
971eb63f296SPaolo Abeni 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
972eb63f296SPaolo Abeni 			goto report_csum_error;
973eb63f296SPaolo Abeni 		return udp6_unicast_rcv_skb(sk, skb, uh);
9744068579eSTom Herbert 	}
9754068579eSTom Herbert 
976eb63f296SPaolo Abeni 	if (!uh->check)
977eb63f296SPaolo Abeni 		goto report_csum_error;
9784068579eSTom Herbert 
979db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
980db8dac20SDavid S. Miller 		goto discard;
981db8dac20SDavid S. Miller 
982db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
9836a5dc9e5SEric Dumazet 		goto csum_error;
984db8dac20SDavid S. Miller 
98502c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
9863ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
987db8dac20SDavid S. Miller 
988db8dac20SDavid S. Miller 	kfree_skb(skb);
989db8dac20SDavid S. Miller 	return 0;
990db8dac20SDavid S. Miller 
991db8dac20SDavid S. Miller short_packet:
992ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
993db8dac20SDavid S. Miller 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
994ba7a46f1SJoe Perches 			    saddr, ntohs(uh->source),
995ba7a46f1SJoe Perches 			    ulen, skb->len,
996ba7a46f1SJoe Perches 			    daddr, ntohs(uh->dest));
9976a5dc9e5SEric Dumazet 	goto discard;
998eb63f296SPaolo Abeni 
999eb63f296SPaolo Abeni report_csum_error:
1000eb63f296SPaolo Abeni 	udp6_csum_zero_error(skb);
10016a5dc9e5SEric Dumazet csum_error:
100202c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1003db8dac20SDavid S. Miller discard:
100402c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1005db8dac20SDavid S. Miller 	kfree_skb(skb);
1006db8dac20SDavid S. Miller 	return 0;
1007db8dac20SDavid S. Miller }
1008db8dac20SDavid S. Miller 
10090bd84065Ssubashab@codeaurora.org 
10105425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net,
10115425077dSsubashab@codeaurora.org 			__be16 loc_port, const struct in6_addr *loc_addr,
10125425077dSsubashab@codeaurora.org 			__be16 rmt_port, const struct in6_addr *rmt_addr,
10134297a0efSDavid Ahern 			int dif, int sdif)
10145425077dSsubashab@codeaurora.org {
10150bd84065Ssubashab@codeaurora.org 	unsigned short hnum = ntohs(loc_port);
1016f0b1e64cSMartin KaFai Lau 	unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
10170bd84065Ssubashab@codeaurora.org 	unsigned int slot2 = hash2 & udp_table.mask;
10180bd84065Ssubashab@codeaurora.org 	struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
10190bd84065Ssubashab@codeaurora.org 	const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
10205425077dSsubashab@codeaurora.org 	struct sock *sk;
10215425077dSsubashab@codeaurora.org 
10220bd84065Ssubashab@codeaurora.org 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
102385cb73ffSWei Wang 		if (sk->sk_state == TCP_ESTABLISHED &&
10244297a0efSDavid Ahern 		    INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
10255425077dSsubashab@codeaurora.org 			return sk;
10260bd84065Ssubashab@codeaurora.org 		/* Only check first socket in chain */
10270bd84065Ssubashab@codeaurora.org 		break;
10280bd84065Ssubashab@codeaurora.org 	}
10290bd84065Ssubashab@codeaurora.org 	return NULL;
10305425077dSsubashab@codeaurora.org }
10315425077dSsubashab@codeaurora.org 
103297ff7ffbSPaolo Abeni INDIRECT_CALLABLE_SCOPE void udp_v6_early_demux(struct sk_buff *skb)
10335425077dSsubashab@codeaurora.org {
10345425077dSsubashab@codeaurora.org 	struct net *net = dev_net(skb->dev);
10355425077dSsubashab@codeaurora.org 	const struct udphdr *uh;
10365425077dSsubashab@codeaurora.org 	struct sock *sk;
10375425077dSsubashab@codeaurora.org 	struct dst_entry *dst;
10385425077dSsubashab@codeaurora.org 	int dif = skb->dev->ifindex;
10394297a0efSDavid Ahern 	int sdif = inet6_sdif(skb);
10405425077dSsubashab@codeaurora.org 
10415425077dSsubashab@codeaurora.org 	if (!pskb_may_pull(skb, skb_transport_offset(skb) +
10425425077dSsubashab@codeaurora.org 	    sizeof(struct udphdr)))
10435425077dSsubashab@codeaurora.org 		return;
10445425077dSsubashab@codeaurora.org 
10455425077dSsubashab@codeaurora.org 	uh = udp_hdr(skb);
10465425077dSsubashab@codeaurora.org 
10475425077dSsubashab@codeaurora.org 	if (skb->pkt_type == PACKET_HOST)
10485425077dSsubashab@codeaurora.org 		sk = __udp6_lib_demux_lookup(net, uh->dest,
10495425077dSsubashab@codeaurora.org 					     &ipv6_hdr(skb)->daddr,
10505425077dSsubashab@codeaurora.org 					     uh->source, &ipv6_hdr(skb)->saddr,
10514297a0efSDavid Ahern 					     dif, sdif);
10525425077dSsubashab@codeaurora.org 	else
10535425077dSsubashab@codeaurora.org 		return;
10545425077dSsubashab@codeaurora.org 
105541c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
10565425077dSsubashab@codeaurora.org 		return;
10575425077dSsubashab@codeaurora.org 
10585425077dSsubashab@codeaurora.org 	skb->sk = sk;
10595425077dSsubashab@codeaurora.org 	skb->destructor = sock_efree;
10605425077dSsubashab@codeaurora.org 	dst = READ_ONCE(sk->sk_rx_dst);
10615425077dSsubashab@codeaurora.org 
10625425077dSsubashab@codeaurora.org 	if (dst)
10635425077dSsubashab@codeaurora.org 		dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
10645425077dSsubashab@codeaurora.org 	if (dst) {
1065d24406c8SWei Wang 		/* set noref for now.
1066d24406c8SWei Wang 		 * any place which wants to hold dst has to call
1067d24406c8SWei Wang 		 * dst_hold_safe()
1068d24406c8SWei Wang 		 */
10695425077dSsubashab@codeaurora.org 		skb_dst_set_noref(skb, dst);
10705425077dSsubashab@codeaurora.org 	}
10715425077dSsubashab@codeaurora.org }
10725425077dSsubashab@codeaurora.org 
10730e219ae4SPaolo Abeni INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
1074db8dac20SDavid S. Miller {
1075645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1076db8dac20SDavid S. Miller }
1077db8dac20SDavid S. Miller 
1078db8dac20SDavid S. Miller /*
1079db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
1080db8dac20SDavid S. Miller  */
1081db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
1082db8dac20SDavid S. Miller {
1083db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1084db8dac20SDavid S. Miller 
108536d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
108636d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
108736d926b9SDenis V. Lunev 	else if (up->pending) {
1088db8dac20SDavid S. Miller 		up->len = 0;
1089db8dac20SDavid S. Miller 		up->pending = 0;
1090db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
1091db8dac20SDavid S. Miller 	}
1092db8dac20SDavid S. Miller }
1093db8dac20SDavid S. Miller 
1094d74bad4eSAndrey Ignatov static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
1095d74bad4eSAndrey Ignatov 			     int addr_len)
1096d74bad4eSAndrey Ignatov {
1097bddc028aSTetsuo Handa 	if (addr_len < offsetofend(struct sockaddr, sa_family))
1098bddc028aSTetsuo Handa 		return -EINVAL;
1099d74bad4eSAndrey Ignatov 	/* The following checks are replicated from __ip6_datagram_connect()
1100d74bad4eSAndrey Ignatov 	 * and intended to prevent BPF program called below from accessing
1101d74bad4eSAndrey Ignatov 	 * bytes that are out of the bound specified by user in addr_len.
1102d74bad4eSAndrey Ignatov 	 */
1103d74bad4eSAndrey Ignatov 	if (uaddr->sa_family == AF_INET) {
1104d74bad4eSAndrey Ignatov 		if (__ipv6_only_sock(sk))
1105d74bad4eSAndrey Ignatov 			return -EAFNOSUPPORT;
1106d74bad4eSAndrey Ignatov 		return udp_pre_connect(sk, uaddr, addr_len);
1107d74bad4eSAndrey Ignatov 	}
1108d74bad4eSAndrey Ignatov 
1109d74bad4eSAndrey Ignatov 	if (addr_len < SIN6_LEN_RFC2133)
1110d74bad4eSAndrey Ignatov 		return -EINVAL;
1111d74bad4eSAndrey Ignatov 
1112d74bad4eSAndrey Ignatov 	return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr);
1113d74bad4eSAndrey Ignatov }
1114d74bad4eSAndrey Ignatov 
1115493c6be3SSridhar Samudrala /**
1116493c6be3SSridhar Samudrala  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
1117493c6be3SSridhar Samudrala  *	@sk:	socket we are sending on
1118493c6be3SSridhar Samudrala  *	@skb:	sk_buff containing the filled-in UDP header
1119493c6be3SSridhar Samudrala  *		(checksum field must be zeroed out)
1120b51cd7c8SAndrew Lunn  *	@saddr: source address
1121b51cd7c8SAndrew Lunn  *	@daddr: destination address
1122b51cd7c8SAndrew Lunn  *	@len:	length of packet
1123493c6be3SSridhar Samudrala  */
1124493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1125493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
1126493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
1127493c6be3SSridhar Samudrala {
1128493c6be3SSridhar Samudrala 	unsigned int offset;
1129493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
1130d39d938cSVlad Yasevich 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1131493c6be3SSridhar Samudrala 	__wsum csum = 0;
1132493c6be3SSridhar Samudrala 
1133d39d938cSVlad Yasevich 	if (!frags) {
1134493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
1135493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
1136493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
1137493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1138493c6be3SSridhar Samudrala 	} else {
1139493c6be3SSridhar Samudrala 		/*
1140493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
1141493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
1142493c6be3SSridhar Samudrala 		 * should be together
1143493c6be3SSridhar Samudrala 		 */
1144493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
1145493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
114663ecc3d9SSubash Abhinov Kasiviswanathan 		csum = skb->csum;
1147493c6be3SSridhar Samudrala 
1148493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
1149493c6be3SSridhar Samudrala 
1150d39d938cSVlad Yasevich 		do {
1151d39d938cSVlad Yasevich 			csum = csum_add(csum, frags->csum);
1152d39d938cSVlad Yasevich 		} while ((frags = frags->next));
1153493c6be3SSridhar Samudrala 
1154493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1155493c6be3SSridhar Samudrala 					    csum);
1156493c6be3SSridhar Samudrala 		if (uh->check == 0)
1157493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
1158493c6be3SSridhar Samudrala 	}
1159493c6be3SSridhar Samudrala }
1160493c6be3SSridhar Samudrala 
1161db8dac20SDavid S. Miller /*
1162db8dac20SDavid S. Miller  *	Sending
1163db8dac20SDavid S. Miller  */
1164db8dac20SDavid S. Miller 
1165bec1f6f6SWillem de Bruijn static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1166bec1f6f6SWillem de Bruijn 			   struct inet_cork *cork)
1167db8dac20SDavid S. Miller {
1168d39d938cSVlad Yasevich 	struct sock *sk = skb->sk;
1169db8dac20SDavid S. Miller 	struct udphdr *uh;
1170db8dac20SDavid S. Miller 	int err = 0;
1171db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1172db8dac20SDavid S. Miller 	__wsum csum = 0;
1173d39d938cSVlad Yasevich 	int offset = skb_transport_offset(skb);
1174d39d938cSVlad Yasevich 	int len = skb->len - offset;
11754094871dSJosh Hunt 	int datalen = len - sizeof(*uh);
1176db8dac20SDavid S. Miller 
1177db8dac20SDavid S. Miller 	/*
1178db8dac20SDavid S. Miller 	 * Create a UDP header
1179db8dac20SDavid S. Miller 	 */
1180db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
11811958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
11821958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
1183d39d938cSVlad Yasevich 	uh->len = htons(len);
1184db8dac20SDavid S. Miller 	uh->check = 0;
1185db8dac20SDavid S. Miller 
1186bec1f6f6SWillem de Bruijn 	if (cork->gso_size) {
1187bec1f6f6SWillem de Bruijn 		const int hlen = skb_network_header_len(skb) +
1188bec1f6f6SWillem de Bruijn 				 sizeof(struct udphdr);
1189bec1f6f6SWillem de Bruijn 
11900f149c9fSWillem de Bruijn 		if (hlen + cork->gso_size > cork->fragsize) {
11910f149c9fSWillem de Bruijn 			kfree_skb(skb);
1192bec1f6f6SWillem de Bruijn 			return -EINVAL;
11930f149c9fSWillem de Bruijn 		}
11940f149c9fSWillem de Bruijn 		if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) {
11950f149c9fSWillem de Bruijn 			kfree_skb(skb);
1196bec1f6f6SWillem de Bruijn 			return -EINVAL;
11970f149c9fSWillem de Bruijn 		}
11980f149c9fSWillem de Bruijn 		if (udp_sk(sk)->no_check6_tx) {
11990f149c9fSWillem de Bruijn 			kfree_skb(skb);
1200a8c744a8SWillem de Bruijn 			return -EINVAL;
12010f149c9fSWillem de Bruijn 		}
1202ff06342cSWillem de Bruijn 		if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
12030f149c9fSWillem de Bruijn 		    dst_xfrm(skb_dst(skb))) {
12040f149c9fSWillem de Bruijn 			kfree_skb(skb);
1205bec1f6f6SWillem de Bruijn 			return -EIO;
12060f149c9fSWillem de Bruijn 		}
1207bec1f6f6SWillem de Bruijn 
12084094871dSJosh Hunt 		if (datalen > cork->gso_size) {
1209bec1f6f6SWillem de Bruijn 			skb_shinfo(skb)->gso_size = cork->gso_size;
1210bec1f6f6SWillem de Bruijn 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
12114094871dSJosh Hunt 			skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
121244b321e5SJosh Hunt 								 cork->gso_size);
12134094871dSJosh Hunt 		}
1214a8c744a8SWillem de Bruijn 		goto csum_partial;
1215bec1f6f6SWillem de Bruijn 	}
1216bec1f6f6SWillem de Bruijn 
1217db8dac20SDavid S. Miller 	if (is_udplite)
1218d39d938cSVlad Yasevich 		csum = udplite_csum(skb);
1219d39d938cSVlad Yasevich 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
12204068579eSTom Herbert 		skb->ip_summed = CHECKSUM_NONE;
12214068579eSTom Herbert 		goto send;
12224068579eSTom Herbert 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1223a8c744a8SWillem de Bruijn csum_partial:
1224d39d938cSVlad Yasevich 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1225493c6be3SSridhar Samudrala 		goto send;
1226493c6be3SSridhar Samudrala 	} else
1227d39d938cSVlad Yasevich 		csum = udp_csum(skb);
1228db8dac20SDavid S. Miller 
1229db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
12304c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1231d39d938cSVlad Yasevich 				    len, fl6->flowi6_proto, csum);
1232db8dac20SDavid S. Miller 	if (uh->check == 0)
1233db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
1234db8dac20SDavid S. Miller 
1235493c6be3SSridhar Samudrala send:
1236d39d938cSVlad Yasevich 	err = ip6_send_skb(skb);
12376ce9e7b5SEric Dumazet 	if (err) {
12386ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
12396aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
12406ce9e7b5SEric Dumazet 				       UDP_MIB_SNDBUFERRORS, is_udplite);
12416ce9e7b5SEric Dumazet 			err = 0;
12426ce9e7b5SEric Dumazet 		}
12436aef70a8SEric Dumazet 	} else {
12446aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
12456ce9e7b5SEric Dumazet 			       UDP_MIB_OUTDATAGRAMS, is_udplite);
12466aef70a8SEric Dumazet 	}
1247d39d938cSVlad Yasevich 	return err;
1248d39d938cSVlad Yasevich }
1249d39d938cSVlad Yasevich 
1250d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
1251d39d938cSVlad Yasevich {
1252d39d938cSVlad Yasevich 	struct sk_buff *skb;
1253d39d938cSVlad Yasevich 	struct udp_sock  *up = udp_sk(sk);
1254d39d938cSVlad Yasevich 	struct flowi6 fl6;
1255d39d938cSVlad Yasevich 	int err = 0;
1256d39d938cSVlad Yasevich 
1257d39d938cSVlad Yasevich 	if (up->pending == AF_INET)
1258d39d938cSVlad Yasevich 		return udp_push_pending_frames(sk);
1259d39d938cSVlad Yasevich 
1260d39d938cSVlad Yasevich 	/* ip6_finish_skb will release the cork, so make a copy of
1261d39d938cSVlad Yasevich 	 * fl6 here.
1262d39d938cSVlad Yasevich 	 */
1263d39d938cSVlad Yasevich 	fl6 = inet_sk(sk)->cork.fl.u.ip6;
1264d39d938cSVlad Yasevich 
1265d39d938cSVlad Yasevich 	skb = ip6_finish_skb(sk);
1266d39d938cSVlad Yasevich 	if (!skb)
1267d39d938cSVlad Yasevich 		goto out;
1268d39d938cSVlad Yasevich 
1269bec1f6f6SWillem de Bruijn 	err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base);
1270d39d938cSVlad Yasevich 
1271db8dac20SDavid S. Miller out:
1272db8dac20SDavid S. Miller 	up->len = 0;
1273db8dac20SDavid S. Miller 	up->pending = 0;
1274db8dac20SDavid S. Miller 	return err;
1275db8dac20SDavid S. Miller }
1276db8dac20SDavid S. Miller 
12771b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1278db8dac20SDavid S. Miller {
1279db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1280db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1281db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1282db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1283342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
128420c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1285db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
128645f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
1287db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
12884c9483b2SDavid S. Miller 	struct flowi6 fl6;
1289db8dac20SDavid S. Miller 	struct dst_entry *dst;
129026879da5SWei Wang 	struct ipcm6_cookie ipc6;
1291db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
12929f542f61SAlexey Kodanev 	bool connected = false;
1293db8dac20SDavid S. Miller 	int ulen = len;
1294db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1295db8dac20SDavid S. Miller 	int err;
1296db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1297db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1298db8dac20SDavid S. Miller 
1299b515430aSWillem de Bruijn 	ipcm6_init(&ipc6);
1300bec1f6f6SWillem de Bruijn 	ipc6.gso_size = up->gso_size;
13015fdaa88dSWillem de Bruijn 	ipc6.sockc.tsflags = sk->sk_tsflags;
1302c6af0c22SWillem de Bruijn 	ipc6.sockc.mark = sk->sk_mark;
130326879da5SWei Wang 
1304db8dac20SDavid S. Miller 	/* destination address check */
1305db8dac20SDavid S. Miller 	if (sin6) {
1306db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1307db8dac20SDavid S. Miller 			return -EINVAL;
1308db8dac20SDavid S. Miller 
1309db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1310db8dac20SDavid S. Miller 		case AF_INET6:
1311db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1312db8dac20SDavid S. Miller 				return -EINVAL;
1313db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1314052d2369SJonathan T. Leighton 			if (ipv6_addr_any(daddr) &&
1315052d2369SJonathan T. Leighton 			    ipv6_addr_v4mapped(&np->saddr))
1316052d2369SJonathan T. Leighton 				ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1317052d2369SJonathan T. Leighton 						       daddr);
1318db8dac20SDavid S. Miller 			break;
1319db8dac20SDavid S. Miller 		case AF_INET:
1320db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1321db8dac20SDavid S. Miller 		case AF_UNSPEC:
1322db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1323db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1324db8dac20SDavid S. Miller 			daddr = NULL;
1325db8dac20SDavid S. Miller 			break;
1326db8dac20SDavid S. Miller 		default:
1327db8dac20SDavid S. Miller 			return -EINVAL;
1328db8dac20SDavid S. Miller 		}
1329db8dac20SDavid S. Miller 	} else if (!up->pending) {
1330db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1331db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1332efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
1333db8dac20SDavid S. Miller 	} else
1334db8dac20SDavid S. Miller 		daddr = NULL;
1335db8dac20SDavid S. Miller 
1336db8dac20SDavid S. Miller 	if (daddr) {
1337db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1338db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1339db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1340c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1341db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1342db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1343db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1344db8dac20SDavid S. Miller do_udp_sendmsg:
1345db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1346db8dac20SDavid S. Miller 				return -ENETUNREACH;
13471b784140SYing Xue 			return udp_sendmsg(sk, msg, len);
1348db8dac20SDavid S. Miller 		}
1349db8dac20SDavid S. Miller 	}
1350db8dac20SDavid S. Miller 
1351db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
13521b784140SYing Xue 		return udp_sendmsg(sk, msg, len);
1353db8dac20SDavid S. Miller 
1354db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1355db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1356db8dac20SDavid S. Miller 	   */
1357db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1358db8dac20SDavid S. Miller 		return -EMSGSIZE;
1359db8dac20SDavid S. Miller 
136003485f2aSVlad Yasevich 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1361db8dac20SDavid S. Miller 	if (up->pending) {
1362db8dac20SDavid S. Miller 		/*
1363db8dac20SDavid S. Miller 		 * There are pending frames.
1364db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1365db8dac20SDavid S. Miller 		 */
1366db8dac20SDavid S. Miller 		lock_sock(sk);
1367db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1368db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1369db8dac20SDavid S. Miller 				release_sock(sk);
1370db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1371db8dac20SDavid S. Miller 			}
1372db8dac20SDavid S. Miller 			dst = NULL;
1373db8dac20SDavid S. Miller 			goto do_append_data;
1374db8dac20SDavid S. Miller 		}
1375db8dac20SDavid S. Miller 		release_sock(sk);
1376db8dac20SDavid S. Miller 	}
1377db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1378db8dac20SDavid S. Miller 
13794c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1380db8dac20SDavid S. Miller 
1381db8dac20SDavid S. Miller 	if (sin6) {
1382db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1383db8dac20SDavid S. Miller 			return -EINVAL;
1384db8dac20SDavid S. Miller 
13851958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1386db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1387db8dac20SDavid S. Miller 
1388db8dac20SDavid S. Miller 		if (np->sndflow) {
13894c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
13904c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
13914c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
139259c820b2SWillem de Bruijn 				if (IS_ERR(flowlabel))
1393db8dac20SDavid S. Miller 					return -EINVAL;
1394db8dac20SDavid S. Miller 			}
1395db8dac20SDavid S. Miller 		}
1396db8dac20SDavid S. Miller 
1397db8dac20SDavid S. Miller 		/*
1398db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1399db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1400db8dac20SDavid S. Miller 		 */
1401db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1402efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1403efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
1404db8dac20SDavid S. Miller 
1405db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1406db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1407842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
14084c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1409db8dac20SDavid S. Miller 	} else {
1410db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1411db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1412db8dac20SDavid S. Miller 
14131958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1414efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
14154c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
14169f542f61SAlexey Kodanev 		connected = true;
1417db8dac20SDavid S. Miller 	}
1418db8dac20SDavid S. Miller 
14194c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
14204c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1421db8dac20SDavid S. Miller 
14224c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
14234c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
14249f690db7SYang Hongyang 
1425c6af0c22SWillem de Bruijn 	fl6.flowi6_mark = ipc6.sockc.mark;
1426e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sk->sk_uid;
142751953d5bSBrian Haley 
1428db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1429db8dac20SDavid S. Miller 		opt = &opt_space;
1430db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1431db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
143226879da5SWei Wang 		ipc6.opt = opt;
1433db8dac20SDavid S. Miller 
14342e8de857SWillem de Bruijn 		err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
14352e8de857SWillem de Bruijn 		if (err > 0)
14362e8de857SWillem de Bruijn 			err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6,
14375fdaa88dSWillem de Bruijn 						    &ipc6);
1438db8dac20SDavid S. Miller 		if (err < 0) {
1439db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1440db8dac20SDavid S. Miller 			return err;
1441db8dac20SDavid S. Miller 		}
14424c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
14434c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
144459c820b2SWillem de Bruijn 			if (IS_ERR(flowlabel))
1445db8dac20SDavid S. Miller 				return -EINVAL;
1446db8dac20SDavid S. Miller 		}
1447db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1448db8dac20SDavid S. Miller 			opt = NULL;
14499f542f61SAlexey Kodanev 		connected = false;
1450db8dac20SDavid S. Miller 	}
145145f6fad8SEric Dumazet 	if (!opt) {
145245f6fad8SEric Dumazet 		opt = txopt_get(np);
145345f6fad8SEric Dumazet 		opt_to_free = opt;
145445f6fad8SEric Dumazet 	}
1455db8dac20SDavid S. Miller 	if (flowlabel)
1456db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1457db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
145826879da5SWei Wang 	ipc6.opt = opt;
1459db8dac20SDavid S. Miller 
14604c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
14614e3fd7a0SAlexey Dobriyan 	fl6.daddr = *daddr;
14624c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
14634e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
14641958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1465db8dac20SDavid S. Miller 
14661cedee13SAndrey Ignatov 	if (cgroup_bpf_enabled && !connected) {
14671cedee13SAndrey Ignatov 		err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
14681cedee13SAndrey Ignatov 					   (struct sockaddr *)sin6, &fl6.saddr);
14691cedee13SAndrey Ignatov 		if (err)
14701cedee13SAndrey Ignatov 			goto out_no_dst;
14711cedee13SAndrey Ignatov 		if (sin6) {
14721cedee13SAndrey Ignatov 			if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
14731cedee13SAndrey Ignatov 				/* BPF program rewrote IPv6-only by IPv4-mapped
14741cedee13SAndrey Ignatov 				 * IPv6. It's currently unsupported.
14751cedee13SAndrey Ignatov 				 */
14761cedee13SAndrey Ignatov 				err = -ENOTSUPP;
14771cedee13SAndrey Ignatov 				goto out_no_dst;
14781cedee13SAndrey Ignatov 			}
14791cedee13SAndrey Ignatov 			if (sin6->sin6_port == 0) {
14801cedee13SAndrey Ignatov 				/* BPF program set invalid port. Reject it. */
14811cedee13SAndrey Ignatov 				err = -EINVAL;
14821cedee13SAndrey Ignatov 				goto out_no_dst;
14831cedee13SAndrey Ignatov 			}
14841cedee13SAndrey Ignatov 			fl6.fl6_dport = sin6->sin6_port;
14851cedee13SAndrey Ignatov 			fl6.daddr = sin6->sin6_addr;
14861cedee13SAndrey Ignatov 		}
14871cedee13SAndrey Ignatov 	}
14881cedee13SAndrey Ignatov 
1489e8e36984SAndrey Ignatov 	if (ipv6_addr_any(&fl6.daddr))
1490e8e36984SAndrey Ignatov 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1491e8e36984SAndrey Ignatov 
14924c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
149320c59de2SArnaud Ebalard 	if (final_p)
14949f542f61SAlexey Kodanev 		connected = false;
1495db8dac20SDavid S. Miller 
14964c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
14974c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
14989f542f61SAlexey Kodanev 		connected = false;
1499c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1500c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1501db8dac20SDavid S. Miller 
15024c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1503db8dac20SDavid S. Miller 
150438b7097bSHannes Frederic Sowa 	if (ipc6.tclass < 0)
150538b7097bSHannes Frederic Sowa 		ipc6.tclass = np->tclass;
150638b7097bSHannes Frederic Sowa 
150738b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
150838b7097bSHannes Frederic Sowa 
15094f858c56SAlexey Kodanev 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected);
151068d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
151168d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
151268d0c6d3SDavid S. Miller 		dst = NULL;
1513db8dac20SDavid S. Miller 		goto out;
1514db8dac20SDavid S. Miller 	}
1515db8dac20SDavid S. Miller 
151626879da5SWei Wang 	if (ipc6.hlimit < 0)
151726879da5SWei Wang 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1518db8dac20SDavid S. Miller 
1519db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1520db8dac20SDavid S. Miller 		goto do_confirm;
1521db8dac20SDavid S. Miller back_from_confirm:
1522db8dac20SDavid S. Miller 
152303485f2aSVlad Yasevich 	/* Lockless fast path for the non-corking case */
152403485f2aSVlad Yasevich 	if (!corkreq) {
15251cd7884dSWillem de Bruijn 		struct inet_cork_full cork;
152603485f2aSVlad Yasevich 		struct sk_buff *skb;
152703485f2aSVlad Yasevich 
152803485f2aSVlad Yasevich 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
152926879da5SWei Wang 				   sizeof(struct udphdr), &ipc6,
153003485f2aSVlad Yasevich 				   &fl6, (struct rt6_info *)dst,
15315fdaa88dSWillem de Bruijn 				   msg->msg_flags, &cork);
153203485f2aSVlad Yasevich 		err = PTR_ERR(skb);
153303485f2aSVlad Yasevich 		if (!IS_ERR_OR_NULL(skb))
1534bec1f6f6SWillem de Bruijn 			err = udp_v6_send_skb(skb, &fl6, &cork.base);
15354f858c56SAlexey Kodanev 		goto out;
153603485f2aSVlad Yasevich 	}
153703485f2aSVlad Yasevich 
1538db8dac20SDavid S. Miller 	lock_sock(sk);
1539db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1540db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1541db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1542db8dac20SDavid S. Miller 		release_sock(sk);
1543db8dac20SDavid S. Miller 
1544ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 2\n");
1545db8dac20SDavid S. Miller 		err = -EINVAL;
1546db8dac20SDavid S. Miller 		goto out;
1547db8dac20SDavid S. Miller 	}
1548db8dac20SDavid S. Miller 
1549db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1550db8dac20SDavid S. Miller 
1551db8dac20SDavid S. Miller do_append_data:
155226879da5SWei Wang 	if (ipc6.dontfrag < 0)
155326879da5SWei Wang 		ipc6.dontfrag = np->dontfrag;
1554db8dac20SDavid S. Miller 	up->len += ulen;
155526879da5SWei Wang 	err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
155626879da5SWei Wang 			      &ipc6, &fl6, (struct rt6_info *)dst,
15575fdaa88dSWillem de Bruijn 			      corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1558db8dac20SDavid S. Miller 	if (err)
1559db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1560db8dac20SDavid S. Miller 	else if (!corkreq)
1561db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1562db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1563db8dac20SDavid S. Miller 		up->pending = 0;
1564db8dac20SDavid S. Miller 
156503485f2aSVlad Yasevich 	if (err > 0)
156603485f2aSVlad Yasevich 		err = np->recverr ? net_xmit_errno(err) : 0;
156703485f2aSVlad Yasevich 	release_sock(sk);
156803485f2aSVlad Yasevich 
1569db8dac20SDavid S. Miller out:
1570a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
15711cedee13SAndrey Ignatov out_no_dst:
1572db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
157345f6fad8SEric Dumazet 	txopt_put(opt_to_free);
1574db8dac20SDavid S. Miller 	if (!err)
1575db8dac20SDavid S. Miller 		return len;
1576db8dac20SDavid S. Miller 	/*
1577db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1578db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1579db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1580db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1581db8dac20SDavid S. Miller 	 * seems like overkill.
1582db8dac20SDavid S. Miller 	 */
1583db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
15846aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
1585235b9f7aSPavel Emelyanov 			       UDP_MIB_SNDBUFERRORS, is_udplite);
1586db8dac20SDavid S. Miller 	}
1587db8dac20SDavid S. Miller 	return err;
1588db8dac20SDavid S. Miller 
1589db8dac20SDavid S. Miller do_confirm:
15900dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
15910dec879fSJulian Anastasov 		dst_confirm_neigh(dst, &fl6.daddr);
1592db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1593db8dac20SDavid S. Miller 		goto back_from_confirm;
1594db8dac20SDavid S. Miller 	err = 0;
1595db8dac20SDavid S. Miller 	goto out;
1596db8dac20SDavid S. Miller }
1597db8dac20SDavid S. Miller 
15987d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1599db8dac20SDavid S. Miller {
160044046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1601db8dac20SDavid S. Miller 	lock_sock(sk);
1602db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1603db8dac20SDavid S. Miller 	release_sock(sk);
1604db8dac20SDavid S. Miller 
160560fb9567SPaolo Abeni 	if (static_branch_unlikely(&udpv6_encap_needed_key)) {
160660fb9567SPaolo Abeni 		if (up->encap_type) {
160744046a59STom Parkin 			void (*encap_destroy)(struct sock *sk);
16086aa7de05SMark Rutland 			encap_destroy = READ_ONCE(up->encap_destroy);
160944046a59STom Parkin 			if (encap_destroy)
161044046a59STom Parkin 				encap_destroy(sk);
161144046a59STom Parkin 		}
161260fb9567SPaolo Abeni 		if (up->encap_enabled)
16139c480601SPaolo Abeni 			static_branch_dec(&udpv6_encap_needed_key);
161460fb9567SPaolo Abeni 	}
161544046a59STom Parkin 
1616db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1617db8dac20SDavid S. Miller }
1618db8dac20SDavid S. Miller 
1619db8dac20SDavid S. Miller /*
1620db8dac20SDavid S. Miller  *	Socket option code for UDP
1621db8dac20SDavid S. Miller  */
1622a7b75c5aSChristoph Hellwig int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1623a7b75c5aSChristoph Hellwig 		     unsigned int optlen)
1624db8dac20SDavid S. Miller {
1625db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
162691ac1ccaSChristoph Hellwig 		return udp_lib_setsockopt(sk, level, optname,
1627a7b75c5aSChristoph Hellwig 					  optval, optlen,
1628db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1629db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1630db8dac20SDavid S. Miller }
1631db8dac20SDavid S. Miller 
1632db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1633db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1634db8dac20SDavid S. Miller {
1635db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1636db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1637db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1638db8dac20SDavid S. Miller }
1639db8dac20SDavid S. Miller 
1640a8e3bb34SDavid Ahern /* thinking of making this const? Don't.
1641a8e3bb34SDavid Ahern  * early_demux can change based on sysctl.
1642a8e3bb34SDavid Ahern  */
1643dddb64bcSsubashab@codeaurora.org static struct inet6_protocol udpv6_protocol = {
16445425077dSsubashab@codeaurora.org 	.early_demux	=	udp_v6_early_demux,
1645dddb64bcSsubashab@codeaurora.org 	.early_demux_handler =  udp_v6_early_demux,
1646db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1647db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1648db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1649db8dac20SDavid S. Miller };
1650db8dac20SDavid S. Miller 
1651db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1652db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1653db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1654db8dac20SDavid S. Miller {
165517ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
165617ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
165717ef66afSLorenzo Colitti 	} else {
165817ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
165917ef66afSLorenzo Colitti 		struct inet_sock *inet = inet_sk(v);
166017ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
166117ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
16626c206b20SPaolo Abeni 		__ip6_dgram_sock_seq_show(seq, v, srcp, destp,
16636c206b20SPaolo Abeni 					  udp_rqueue_get(v), bucket);
166417ef66afSLorenzo Colitti 	}
1665db8dac20SDavid S. Miller 	return 0;
1666db8dac20SDavid S. Miller }
1667db8dac20SDavid S. Miller 
1668c3506372SChristoph Hellwig const struct seq_operations udp6_seq_ops = {
1669a3d2599bSChristoph Hellwig 	.start		= udp_seq_start,
1670a3d2599bSChristoph Hellwig 	.next		= udp_seq_next,
1671a3d2599bSChristoph Hellwig 	.stop		= udp_seq_stop,
1672a3d2599bSChristoph Hellwig 	.show		= udp6_seq_show,
1673a3d2599bSChristoph Hellwig };
1674c3506372SChristoph Hellwig EXPORT_SYMBOL(udp6_seq_ops);
167573cb88ecSArjan van de Ven 
1676db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1677db8dac20SDavid S. Miller 	.family		= AF_INET6,
1678645ca708SEric Dumazet 	.udp_table	= &udp_table,
1679db8dac20SDavid S. Miller };
1680db8dac20SDavid S. Miller 
16812c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1682db8dac20SDavid S. Miller {
1683c3506372SChristoph Hellwig 	if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1684c3506372SChristoph Hellwig 			sizeof(struct udp_iter_state), &udp6_seq_afinfo))
1685a3d2599bSChristoph Hellwig 		return -ENOMEM;
1686a3d2599bSChristoph Hellwig 	return 0;
1687db8dac20SDavid S. Miller }
1688db8dac20SDavid S. Miller 
1689ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1690ec120da6SIan Morris {
1691a3d2599bSChristoph Hellwig 	remove_proc_entry("udp6", net->proc_net);
1692db8dac20SDavid S. Miller }
1693db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1694db8dac20SDavid S. Miller 
1695db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1696db8dac20SDavid S. Miller 
1697db8dac20SDavid S. Miller struct proto udpv6_prot = {
1698db8dac20SDavid S. Miller 	.name			= "UDPv6",
1699db8dac20SDavid S. Miller 	.owner			= THIS_MODULE,
1700db8dac20SDavid S. Miller 	.close			= udp_lib_close,
1701d74bad4eSAndrey Ignatov 	.pre_connect		= udpv6_pre_connect,
1702db8dac20SDavid S. Miller 	.connect		= ip6_datagram_connect,
1703db8dac20SDavid S. Miller 	.disconnect		= udp_disconnect,
1704db8dac20SDavid S. Miller 	.ioctl			= udp_ioctl,
1705850cbaddSPaolo Abeni 	.init			= udp_init_sock,
1706db8dac20SDavid S. Miller 	.destroy		= udpv6_destroy_sock,
1707db8dac20SDavid S. Miller 	.setsockopt		= udpv6_setsockopt,
1708db8dac20SDavid S. Miller 	.getsockopt		= udpv6_getsockopt,
1709db8dac20SDavid S. Miller 	.sendmsg		= udpv6_sendmsg,
1710db8dac20SDavid S. Miller 	.recvmsg		= udpv6_recvmsg,
1711e646b657SMartin KaFai Lau 	.release_cb		= ip6_datagram_release_cb,
1712db8dac20SDavid S. Miller 	.hash			= udp_lib_hash,
1713db8dac20SDavid S. Miller 	.unhash			= udp_lib_unhash,
1714719f8358SEric Dumazet 	.rehash			= udp_v6_rehash,
1715db8dac20SDavid S. Miller 	.get_port		= udp_v6_get_port,
1716db8dac20SDavid S. Miller 	.memory_allocated	= &udp_memory_allocated,
1717db8dac20SDavid S. Miller 	.sysctl_mem		= sysctl_udp_mem,
17181e802951STonghao Zhang 	.sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
17191e802951STonghao Zhang 	.sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1720db8dac20SDavid S. Miller 	.obj_size		= sizeof(struct udp6_sock),
1721645ca708SEric Dumazet 	.h.udp_table		= &udp_table,
17225d77dca8SDavid Ahern 	.diag_destroy		= udp_abort,
1723db8dac20SDavid S. Miller };
1724db8dac20SDavid S. Miller 
1725db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1726db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1727db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1728db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1729db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1730db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1731db8dac20SDavid S. Miller };
1732db8dac20SDavid S. Miller 
1733db8dac20SDavid S. Miller int __init udpv6_init(void)
1734db8dac20SDavid S. Miller {
1735db8dac20SDavid S. Miller 	int ret;
1736db8dac20SDavid S. Miller 
17373336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
17383336288aSVlad Yasevich 	if (ret)
1739c6b641a4SVlad Yasevich 		goto out;
17403336288aSVlad Yasevich 
1741db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1742db8dac20SDavid S. Miller 	if (ret)
1743db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1744db8dac20SDavid S. Miller out:
1745db8dac20SDavid S. Miller 	return ret;
1746db8dac20SDavid S. Miller 
1747db8dac20SDavid S. Miller out_udpv6_protocol:
1748db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1749db8dac20SDavid S. Miller 	goto out;
1750db8dac20SDavid S. Miller }
1751db8dac20SDavid S. Miller 
1752db8dac20SDavid S. Miller void udpv6_exit(void)
1753db8dac20SDavid S. Miller {
1754db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1755db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1756db8dac20SDavid S. Miller }
1757