xref: /openbmc/linux/net/ipv6/udp.c (revision 0e219ae4)
1db8dac20SDavid S. Miller /*
2db8dac20SDavid S. Miller  *	UDP over IPv6
3db8dac20SDavid S. Miller  *	Linux INET6 implementation
4db8dac20SDavid S. Miller  *
5db8dac20SDavid S. Miller  *	Authors:
6db8dac20SDavid S. Miller  *	Pedro Roque		<roque@di.fc.ul.pt>
7db8dac20SDavid S. Miller  *
8db8dac20SDavid S. Miller  *	Based on linux/ipv4/udp.c
9db8dac20SDavid S. Miller  *
10db8dac20SDavid S. Miller  *	Fixes:
11db8dac20SDavid S. Miller  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
12db8dac20SDavid S. Miller  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
13db8dac20SDavid S. Miller  *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
14db8dac20SDavid S. Miller  *					a single port at the same time.
15db8dac20SDavid S. Miller  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
16db8dac20SDavid S. Miller  *      YOSHIFUJI Hideaki @USAGI:	convert /proc/net/udp6 to seq_file.
17db8dac20SDavid S. Miller  *
18db8dac20SDavid S. Miller  *	This program is free software; you can redistribute it and/or
19db8dac20SDavid S. Miller  *      modify it under the terms of the GNU General Public License
20db8dac20SDavid S. Miller  *      as published by the Free Software Foundation; either version
21db8dac20SDavid S. Miller  *      2 of the License, or (at your option) any later version.
22db8dac20SDavid S. Miller  */
23db8dac20SDavid S. Miller 
24db8dac20SDavid S. Miller #include <linux/errno.h>
25db8dac20SDavid S. Miller #include <linux/types.h>
26db8dac20SDavid S. Miller #include <linux/socket.h>
27db8dac20SDavid S. Miller #include <linux/sockios.h>
28db8dac20SDavid S. Miller #include <linux/net.h>
29db8dac20SDavid S. Miller #include <linux/in6.h>
30db8dac20SDavid S. Miller #include <linux/netdevice.h>
31db8dac20SDavid S. Miller #include <linux/if_arp.h>
32db8dac20SDavid S. Miller #include <linux/ipv6.h>
33db8dac20SDavid S. Miller #include <linux/icmpv6.h>
34db8dac20SDavid S. Miller #include <linux/init.h>
35db8dac20SDavid S. Miller #include <linux/module.h>
36db8dac20SDavid S. Miller #include <linux/skbuff.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
387c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
390e219ae4SPaolo Abeni #include <linux/indirect_call_wrapper.h>
40db8dac20SDavid S. Miller 
41496611d7SCraig Gallek #include <net/addrconf.h>
42db8dac20SDavid S. Miller #include <net/ndisc.h>
43db8dac20SDavid S. Miller #include <net/protocol.h>
44db8dac20SDavid S. Miller #include <net/transp_v6.h>
45db8dac20SDavid S. Miller #include <net/ip6_route.h>
46db8dac20SDavid S. Miller #include <net/raw.h>
47db8dac20SDavid S. Miller #include <net/tcp_states.h>
48db8dac20SDavid S. Miller #include <net/ip6_checksum.h>
49e7cc0824SStefano Brivio #include <net/ip6_tunnel.h>
50db8dac20SDavid S. Miller #include <net/xfrm.h>
510bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h>
5272289b96STom Herbert #include <net/inet6_hashtables.h>
53076bb0c8SEliezer Tamir #include <net/busy_poll.h>
54e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
55db8dac20SDavid S. Miller 
56db8dac20SDavid S. Miller #include <linux/proc_fs.h>
57db8dac20SDavid S. Miller #include <linux/seq_file.h>
5822911fc5SEric Dumazet #include <trace/events/skb.h>
59db8dac20SDavid S. Miller #include "udp_impl.h"
60db8dac20SDavid S. Miller 
6163a6fff3SRobert Shearman static bool udp6_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
6263a6fff3SRobert Shearman {
6363a6fff3SRobert Shearman #if defined(CONFIG_NET_L3_MASTER_DEV)
6463a6fff3SRobert Shearman 	if (!net->ipv4.sysctl_udp_l3mdev_accept &&
6563a6fff3SRobert Shearman 	    skb && ipv6_l3mdev_skb(IP6CB(skb)->flags))
6663a6fff3SRobert Shearman 		return true;
6763a6fff3SRobert Shearman #endif
6863a6fff3SRobert Shearman 	return false;
6963a6fff3SRobert Shearman }
7063a6fff3SRobert Shearman 
716eada011SEric Dumazet static u32 udp6_ehashfn(const struct net *net,
72b50026b5SHannes Frederic Sowa 			const struct in6_addr *laddr,
73b50026b5SHannes Frederic Sowa 			const u16 lport,
74b50026b5SHannes Frederic Sowa 			const struct in6_addr *faddr,
75b50026b5SHannes Frederic Sowa 			const __be16 fport)
76b50026b5SHannes Frederic Sowa {
771bbdceefSHannes Frederic Sowa 	static u32 udp6_ehash_secret __read_mostly;
781bbdceefSHannes Frederic Sowa 	static u32 udp_ipv6_hash_secret __read_mostly;
791bbdceefSHannes Frederic Sowa 
801bbdceefSHannes Frederic Sowa 	u32 lhash, fhash;
811bbdceefSHannes Frederic Sowa 
821bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp6_ehash_secret,
831bbdceefSHannes Frederic Sowa 			    sizeof(udp6_ehash_secret));
841bbdceefSHannes Frederic Sowa 	net_get_random_once(&udp_ipv6_hash_secret,
851bbdceefSHannes Frederic Sowa 			    sizeof(udp_ipv6_hash_secret));
861bbdceefSHannes Frederic Sowa 
871bbdceefSHannes Frederic Sowa 	lhash = (__force u32)laddr->s6_addr32[3];
881bbdceefSHannes Frederic Sowa 	fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
891bbdceefSHannes Frederic Sowa 
90b50026b5SHannes Frederic Sowa 	return __inet6_ehashfn(lhash, lport, fhash, fport,
911bbdceefSHannes Frederic Sowa 			       udp_ipv6_hash_secret + net_hash_mix(net));
92b50026b5SHannes Frederic Sowa }
93b50026b5SHannes Frederic Sowa 
946ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
95db8dac20SDavid S. Miller {
9630fff923SEric Dumazet 	unsigned int hash2_nulladdr =
97f0b1e64cSMartin KaFai Lau 		ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
9830fff923SEric Dumazet 	unsigned int hash2_partial =
99f0b1e64cSMartin KaFai Lau 		ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
10030fff923SEric Dumazet 
101d4cada4aSEric Dumazet 	/* precompute partial secondary hash */
10230fff923SEric Dumazet 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
103fe38d2a1SJosef Bacik 	return udp_lib_get_port(sk, snum, hash2_nulladdr);
104db8dac20SDavid S. Miller }
105db8dac20SDavid S. Miller 
106f7c46156SAlexey Kodanev void udp_v6_rehash(struct sock *sk)
107719f8358SEric Dumazet {
108f0b1e64cSMartin KaFai Lau 	u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
109efe4208fSEric Dumazet 					  &sk->sk_v6_rcv_saddr,
110719f8358SEric Dumazet 					  inet_sk(sk)->inet_num);
111719f8358SEric Dumazet 
112719f8358SEric Dumazet 	udp_lib_rehash(sk, new_hash);
113719f8358SEric Dumazet }
114719f8358SEric Dumazet 
115d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net,
11688440ae7SBalazs Scheidler 			 const struct in6_addr *saddr, __be16 sport,
117d1e37288SSu, Xuemin 			 const struct in6_addr *daddr, unsigned short hnum,
1181801b570SDavid Ahern 			 int dif, int sdif, bool exact_dif)
119db8dac20SDavid S. Miller {
12060c04aecSJoe Perches 	int score;
12160c04aecSJoe Perches 	struct inet_sock *inet;
1226da5b0f0SMike Manning 	bool dev_match;
123db8dac20SDavid S. Miller 
12460c04aecSJoe Perches 	if (!net_eq(sock_net(sk), net) ||
12560c04aecSJoe Perches 	    udp_sk(sk)->udp_port_hash != hnum ||
12660c04aecSJoe Perches 	    sk->sk_family != PF_INET6)
12760c04aecSJoe Perches 		return -1;
128645ca708SEric Dumazet 
12923b0269eSPeter Oskolkov 	if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
13023b0269eSPeter Oskolkov 		return -1;
13123b0269eSPeter Oskolkov 
132645ca708SEric Dumazet 	score = 0;
13360c04aecSJoe Perches 	inet = inet_sk(sk);
13460c04aecSJoe Perches 
135c720c7e8SEric Dumazet 	if (inet->inet_dport) {
136c720c7e8SEric Dumazet 		if (inet->inet_dport != sport)
137645ca708SEric Dumazet 			return -1;
138db8dac20SDavid S. Miller 		score++;
139db8dac20SDavid S. Miller 	}
14060c04aecSJoe Perches 
141efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
142efe4208fSEric Dumazet 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
143645ca708SEric Dumazet 			return -1;
144db8dac20SDavid S. Miller 		score++;
145db8dac20SDavid S. Miller 	}
14660c04aecSJoe Perches 
1476da5b0f0SMike Manning 	dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
14869678bcdSPaolo Abeni 	if (!dev_match)
149645ca708SEric Dumazet 		return -1;
150db8dac20SDavid S. Miller 	score++;
15160c04aecSJoe Perches 
15270da268bSEric Dumazet 	if (sk->sk_incoming_cpu == raw_smp_processor_id())
15370da268bSEric Dumazet 		score++;
15470da268bSEric Dumazet 
155645ca708SEric Dumazet 	return score;
156645ca708SEric Dumazet }
157645ca708SEric Dumazet 
158d1e37288SSu, Xuemin /* called with rcu_read_lock() */
159fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
160fddc17deSEric Dumazet 		const struct in6_addr *saddr, __be16 sport,
1611801b570SDavid Ahern 		const struct in6_addr *daddr, unsigned int hnum,
1621801b570SDavid Ahern 		int dif, int sdif, bool exact_dif,
1631801b570SDavid Ahern 		struct udp_hslot *hslot2, struct sk_buff *skb)
164fddc17deSEric Dumazet {
165fddc17deSEric Dumazet 	struct sock *sk, *result;
166e94a62f5SPaolo Abeni 	int score, badness;
16772289b96STom Herbert 	u32 hash = 0;
168fddc17deSEric Dumazet 
169fddc17deSEric Dumazet 	result = NULL;
170fddc17deSEric Dumazet 	badness = -1;
171ca065d0cSEric Dumazet 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
172d1e37288SSu, Xuemin 		score = compute_score(sk, net, saddr, sport,
1731801b570SDavid Ahern 				      daddr, hnum, dif, sdif, exact_dif);
174fddc17deSEric Dumazet 		if (score > badness) {
175e94a62f5SPaolo Abeni 			if (sk->sk_reuseport) {
176b50026b5SHannes Frederic Sowa 				hash = udp6_ehashfn(net, daddr, hnum,
17772289b96STom Herbert 						    saddr, sport);
178ed0dfffdSEric Dumazet 
179ca065d0cSEric Dumazet 				result = reuseport_select_sock(sk, hash, skb,
1801134158bSCraig Gallek 							sizeof(struct udphdr));
181ca065d0cSEric Dumazet 				if (result)
182ca065d0cSEric Dumazet 					return result;
18370da268bSEric Dumazet 			}
184ca065d0cSEric Dumazet 			result = sk;
185ca065d0cSEric Dumazet 			badness = score;
186fddc17deSEric Dumazet 		}
187fddc17deSEric Dumazet 	}
188fddc17deSEric Dumazet 	return result;
189fddc17deSEric Dumazet }
190fddc17deSEric Dumazet 
191ca065d0cSEric Dumazet /* rcu_read_lock() must be held */
192fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
19388440ae7SBalazs Scheidler 			       const struct in6_addr *saddr, __be16 sport,
19488440ae7SBalazs Scheidler 			       const struct in6_addr *daddr, __be16 dport,
1951801b570SDavid Ahern 			       int dif, int sdif, struct udp_table *udptable,
196538950a1SCraig Gallek 			       struct sk_buff *skb)
197645ca708SEric Dumazet {
198645ca708SEric Dumazet 	unsigned short hnum = ntohs(dport);
19923b0269eSPeter Oskolkov 	unsigned int hash2, slot2;
20023b0269eSPeter Oskolkov 	struct udp_hslot *hslot2;
20123b0269eSPeter Oskolkov 	struct sock *result;
20263a6fff3SRobert Shearman 	bool exact_dif = udp6_lib_exact_dif_match(net, skb);
203645ca708SEric Dumazet 
204f0b1e64cSMartin KaFai Lau 	hash2 = ipv6_portaddr_hash(net, daddr, hnum);
205fddc17deSEric Dumazet 	slot2 = hash2 & udptable->mask;
206fddc17deSEric Dumazet 	hslot2 = &udptable->hash2[slot2];
207fddc17deSEric Dumazet 
208fddc17deSEric Dumazet 	result = udp6_lib_lookup2(net, saddr, sport,
2091801b570SDavid Ahern 				  daddr, hnum, dif, sdif, exact_dif,
210d1e37288SSu, Xuemin 				  hslot2, skb);
211fddc17deSEric Dumazet 	if (!result) {
212f0b1e64cSMartin KaFai Lau 		hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
213fddc17deSEric Dumazet 		slot2 = hash2 & udptable->mask;
214d1e37288SSu, Xuemin 
215fddc17deSEric Dumazet 		hslot2 = &udptable->hash2[slot2];
216fddc17deSEric Dumazet 
2171223c67cSJorge Boncompte [DTI2] 		result = udp6_lib_lookup2(net, saddr, sport,
21823b0269eSPeter Oskolkov 					  &in6addr_any, hnum, dif, sdif,
21963a6fff3SRobert Shearman 					  exact_dif, hslot2,
22063a6fff3SRobert Shearman 					  skb);
221fddc17deSEric Dumazet 	}
2228217ca65SMartin KaFai Lau 	if (unlikely(IS_ERR(result)))
2238217ca65SMartin KaFai Lau 		return NULL;
224fddc17deSEric Dumazet 	return result;
225fddc17deSEric Dumazet }
226fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
227db8dac20SDavid S. Miller 
228607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
229607c4aafSKOVACS Krisztian 					  __be16 sport, __be16 dport,
230645ca708SEric Dumazet 					  struct udp_table *udptable)
231607c4aafSKOVACS Krisztian {
232b71d1d42SEric Dumazet 	const struct ipv6hdr *iph = ipv6_hdr(skb);
233607c4aafSKOVACS Krisztian 
234ed7cbbceSAlexander Duyck 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
235607c4aafSKOVACS Krisztian 				 &iph->daddr, dport, inet6_iif(skb),
2361801b570SDavid Ahern 				 inet6_sdif(skb), udptable, skb);
237607c4aafSKOVACS Krisztian }
238607c4aafSKOVACS Krisztian 
23963058308STom Herbert struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
24063058308STom Herbert 				 __be16 sport, __be16 dport)
24163058308STom Herbert {
24263058308STom Herbert 	const struct ipv6hdr *iph = ipv6_hdr(skb);
24363058308STom Herbert 
244ed7cbbceSAlexander Duyck 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
24563058308STom Herbert 				 &iph->daddr, dport, inet6_iif(skb),
2461801b570SDavid Ahern 				 inet6_sdif(skb), &udp_table, skb);
24763058308STom Herbert }
24863058308STom Herbert EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
24963058308STom Herbert 
250ca065d0cSEric Dumazet /* Must be called under rcu_read_lock().
251ca065d0cSEric Dumazet  * Does increment socket refcount.
252ca065d0cSEric Dumazet  */
2536e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
254aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
255aa976fc0SBalazs Scheidler 			     const struct in6_addr *daddr, __be16 dport, int dif)
256aa976fc0SBalazs Scheidler {
257ca065d0cSEric Dumazet 	struct sock *sk;
258ca065d0cSEric Dumazet 
259ca065d0cSEric Dumazet 	sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
2601801b570SDavid Ahern 				dif, 0, &udp_table, NULL);
26141c6d650SReshetova, Elena 	if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
262ca065d0cSEric Dumazet 		sk = NULL;
263ca065d0cSEric Dumazet 	return sk;
264aa976fc0SBalazs Scheidler }
265aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
266ca065d0cSEric Dumazet #endif
267aa976fc0SBalazs Scheidler 
268cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the
269cb891fa6SPaolo Abeni  * scratch area space; note that the IP6CB flags is still in the first
270cb891fa6SPaolo Abeni  * cacheline, so checking for jumbograms is cheap
271cb891fa6SPaolo Abeni  */
272cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb)
273cb891fa6SPaolo Abeni {
274cb891fa6SPaolo Abeni 	return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
275cb891fa6SPaolo Abeni }
276cb891fa6SPaolo Abeni 
277db8dac20SDavid S. Miller /*
278db8dac20SDavid S. Miller  *	This should be easy, if there is something there we
279db8dac20SDavid S. Miller  *	return it, otherwise we block.
280db8dac20SDavid S. Miller  */
281db8dac20SDavid S. Miller 
2821b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
283db8dac20SDavid S. Miller 		  int noblock, int flags, int *addr_len)
284db8dac20SDavid S. Miller {
285db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
286db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
287db8dac20SDavid S. Miller 	struct sk_buff *skb;
28859c2cdaeSDavid S. Miller 	unsigned int ulen, copied;
289fd69c399SPaolo Abeni 	int off, err, peeking = flags & MSG_PEEK;
290db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
291543fc3fbSPaolo Abeni 	struct udp_mib __percpu *mib;
292197c949eSEric Dumazet 	bool checksum_valid = false;
293f26ba175SWei Yongjun 	int is_udp4;
294db8dac20SDavid S. Miller 
295db8dac20SDavid S. Miller 	if (flags & MSG_ERRQUEUE)
29685fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
297db8dac20SDavid S. Miller 
2984b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
29985fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
3004b340ae2SBrian Haley 
301db8dac20SDavid S. Miller try_again:
302a0917e0bSMatthew Dawson 	off = sk_peek_offset(sk, flags);
303fd69c399SPaolo Abeni 	skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
304db8dac20SDavid S. Miller 	if (!skb)
305627d2d6bSsamanthakumar 		return err;
306db8dac20SDavid S. Miller 
307cb891fa6SPaolo Abeni 	ulen = udp6_skb_len(skb);
30859c2cdaeSDavid S. Miller 	copied = len;
309627d2d6bSsamanthakumar 	if (copied > ulen - off)
310627d2d6bSsamanthakumar 		copied = ulen - off;
31159c2cdaeSDavid S. Miller 	else if (copied < ulen)
312db8dac20SDavid S. Miller 		msg->msg_flags |= MSG_TRUNC;
313db8dac20SDavid S. Miller 
314f26ba175SWei Yongjun 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
315029a3743SPaolo Abeni 	mib = __UDPX_MIB(sk, is_udp4);
316f26ba175SWei Yongjun 
317db8dac20SDavid S. Miller 	/*
318db8dac20SDavid S. Miller 	 * If checksum is needed at all, try to do it while copying the
319db8dac20SDavid S. Miller 	 * data.  If the data is truncated, or if we only want a partial
320db8dac20SDavid S. Miller 	 * coverage checksum (UDP-Lite), do it before the copy.
321db8dac20SDavid S. Miller 	 */
322db8dac20SDavid S. Miller 
323d21dbdfeSEric Dumazet 	if (copied < ulen || peeking ||
324d21dbdfeSEric Dumazet 	    (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
32567a51780SPaolo Abeni 		checksum_valid = udp_skb_csum_unnecessary(skb) ||
32667a51780SPaolo Abeni 				!__udp_lib_checksum_complete(skb);
327197c949eSEric Dumazet 		if (!checksum_valid)
328db8dac20SDavid S. Miller 			goto csum_copy_err;
329db8dac20SDavid S. Miller 	}
330db8dac20SDavid S. Miller 
33167a51780SPaolo Abeni 	if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
33267a51780SPaolo Abeni 		if (udp_skb_is_linear(skb))
33367a51780SPaolo Abeni 			err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
33467a51780SPaolo Abeni 		else
335627d2d6bSsamanthakumar 			err = skb_copy_datagram_msg(skb, off, msg, copied);
33667a51780SPaolo Abeni 	} else {
337627d2d6bSsamanthakumar 		err = skb_copy_and_csum_datagram_msg(skb, off, msg);
338db8dac20SDavid S. Miller 		if (err == -EINVAL)
339db8dac20SDavid S. Miller 			goto csum_copy_err;
340db8dac20SDavid S. Miller 	}
34122911fc5SEric Dumazet 	if (unlikely(err)) {
342fd69c399SPaolo Abeni 		if (!peeking) {
343979402b1SEric Dumazet 			atomic_inc(&sk->sk_drops);
344029a3743SPaolo Abeni 			SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
345979402b1SEric Dumazet 		}
346850cbaddSPaolo Abeni 		kfree_skb(skb);
347627d2d6bSsamanthakumar 		return err;
34822911fc5SEric Dumazet 	}
349fd69c399SPaolo Abeni 	if (!peeking)
350029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
351db8dac20SDavid S. Miller 
3523b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
353db8dac20SDavid S. Miller 
354db8dac20SDavid S. Miller 	/* Copy the address. */
355db8dac20SDavid S. Miller 	if (msg->msg_name) {
356342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
357db8dac20SDavid S. Miller 		sin6->sin6_family = AF_INET6;
358db8dac20SDavid S. Miller 		sin6->sin6_port = udp_hdr(skb)->source;
359db8dac20SDavid S. Miller 		sin6->sin6_flowinfo = 0;
360db8dac20SDavid S. Miller 
361842df073SHannes Frederic Sowa 		if (is_udp4) {
362b301e82cSBrian Haley 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
363b301e82cSBrian Haley 					       &sin6->sin6_addr);
364842df073SHannes Frederic Sowa 			sin6->sin6_scope_id = 0;
365842df073SHannes Frederic Sowa 		} else {
3664e3fd7a0SAlexey Dobriyan 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
367842df073SHannes Frederic Sowa 			sin6->sin6_scope_id =
368842df073SHannes Frederic Sowa 				ipv6_iface_scope_id(&sin6->sin6_addr,
3694330487aSDuan Jiong 						    inet6_iif(skb));
370db8dac20SDavid S. Miller 		}
371bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
372db8dac20SDavid S. Miller 	}
3734b261c75SHannes Frederic Sowa 
374bcd1665eSPaolo Abeni 	if (udp_sk(sk)->gro_enabled)
375bcd1665eSPaolo Abeni 		udp_cmsg_recv(msg, sk, skb);
376bcd1665eSPaolo Abeni 
3774b261c75SHannes Frederic Sowa 	if (np->rxopt.all)
3784b261c75SHannes Frederic Sowa 		ip6_datagram_recv_common_ctl(sk, msg, skb);
3794b261c75SHannes Frederic Sowa 
380f26ba175SWei Yongjun 	if (is_udp4) {
381db8dac20SDavid S. Miller 		if (inet->cmsg_flags)
382ad959036SPaolo Abeni 			ip_cmsg_recv_offset(msg, sk, skb,
38310df8e61SEric Dumazet 					    sizeof(struct udphdr), off);
384db8dac20SDavid S. Miller 	} else {
385db8dac20SDavid S. Miller 		if (np->rxopt.all)
3864b261c75SHannes Frederic Sowa 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
387db8dac20SDavid S. Miller 	}
388db8dac20SDavid S. Miller 
38959c2cdaeSDavid S. Miller 	err = copied;
390db8dac20SDavid S. Miller 	if (flags & MSG_TRUNC)
391db8dac20SDavid S. Miller 		err = ulen;
392db8dac20SDavid S. Miller 
393850cbaddSPaolo Abeni 	skb_consume_udp(sk, skb, peeking ? -err : err);
394db8dac20SDavid S. Miller 	return err;
395db8dac20SDavid S. Miller 
396db8dac20SDavid S. Miller csum_copy_err:
3972276f58aSPaolo Abeni 	if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
3982276f58aSPaolo Abeni 				 udp_skb_destructor)) {
399029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
400029a3743SPaolo Abeni 		SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
4016a5dc9e5SEric Dumazet 	}
402850cbaddSPaolo Abeni 	kfree_skb(skb);
403db8dac20SDavid S. Miller 
404beb39db5SEric Dumazet 	/* starting over for a new packet, but check if we need to yield */
405beb39db5SEric Dumazet 	cond_resched();
4069cfaa8deSXufeng Zhang 	msg->msg_flags &= ~MSG_TRUNC;
407db8dac20SDavid S. Miller 	goto try_again;
408db8dac20SDavid S. Miller }
409db8dac20SDavid S. Miller 
410a36e185eSStefano Brivio DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
411a36e185eSStefano Brivio void udpv6_encap_enable(void)
412a36e185eSStefano Brivio {
4139c480601SPaolo Abeni 	static_branch_inc(&udpv6_encap_needed_key);
414a36e185eSStefano Brivio }
415a36e185eSStefano Brivio EXPORT_SYMBOL(udpv6_encap_enable);
416a36e185eSStefano Brivio 
417e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
418e7cc0824SStefano Brivio  * through error handlers in encapsulations looking for a match.
419e7cc0824SStefano Brivio  */
420e7cc0824SStefano Brivio static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
421e7cc0824SStefano Brivio 				      struct inet6_skb_parm *opt,
422424a7cd0SPaolo Abeni 				      u8 type, u8 code, int offset, __be32 info)
423e7cc0824SStefano Brivio {
424e7cc0824SStefano Brivio 	int i;
425e7cc0824SStefano Brivio 
426e7cc0824SStefano Brivio 	for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
427e7cc0824SStefano Brivio 		int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
428424a7cd0SPaolo Abeni 			       u8 type, u8 code, int offset, __be32 info);
429424a7cd0SPaolo Abeni 		const struct ip6_tnl_encap_ops *encap;
430e7cc0824SStefano Brivio 
431424a7cd0SPaolo Abeni 		encap = rcu_dereference(ip6tun_encaps[i]);
432424a7cd0SPaolo Abeni 		if (!encap)
433e7cc0824SStefano Brivio 			continue;
434424a7cd0SPaolo Abeni 		handler = encap->err_handler;
435e7cc0824SStefano Brivio 		if (handler && !handler(skb, opt, type, code, offset, info))
436e7cc0824SStefano Brivio 			return 0;
437e7cc0824SStefano Brivio 	}
438e7cc0824SStefano Brivio 
439e7cc0824SStefano Brivio 	return -ENOENT;
440e7cc0824SStefano Brivio }
441e7cc0824SStefano Brivio 
442a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without
443a36e185eSStefano Brivio  * reversing source and destination port: this will match tunnels that force the
444a36e185eSStefano Brivio  * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
445a36e185eSStefano Brivio  * lwtunnels might actually break this assumption by being configured with
446a36e185eSStefano Brivio  * different destination ports on endpoints, in this case we won't be able to
447a36e185eSStefano Brivio  * trace ICMP messages back to them.
448a36e185eSStefano Brivio  *
449e7cc0824SStefano Brivio  * If this doesn't match any socket, probe tunnels with arbitrary destination
450e7cc0824SStefano Brivio  * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
451e7cc0824SStefano Brivio  * we've sent packets to won't necessarily match the local destination port.
452e7cc0824SStefano Brivio  *
453a36e185eSStefano Brivio  * Then ask the tunnel implementation to match the error against a valid
454a36e185eSStefano Brivio  * association.
455a36e185eSStefano Brivio  *
456e7cc0824SStefano Brivio  * Return an error if we can't find a match, the socket if we need further
457e7cc0824SStefano Brivio  * processing, zero otherwise.
458a36e185eSStefano Brivio  */
459a36e185eSStefano Brivio static struct sock *__udp6_lib_err_encap(struct net *net,
460a36e185eSStefano Brivio 					 const struct ipv6hdr *hdr, int offset,
461a36e185eSStefano Brivio 					 struct udphdr *uh,
462a36e185eSStefano Brivio 					 struct udp_table *udptable,
463e7cc0824SStefano Brivio 					 struct sk_buff *skb,
464e7cc0824SStefano Brivio 					 struct inet6_skb_parm *opt,
465e7cc0824SStefano Brivio 					 u8 type, u8 code, __be32 info)
466a36e185eSStefano Brivio {
467a36e185eSStefano Brivio 	int network_offset, transport_offset;
468a36e185eSStefano Brivio 	struct sock *sk;
469a36e185eSStefano Brivio 
470a36e185eSStefano Brivio 	network_offset = skb_network_offset(skb);
471a36e185eSStefano Brivio 	transport_offset = skb_transport_offset(skb);
472a36e185eSStefano Brivio 
473a36e185eSStefano Brivio 	/* Network header needs to point to the outer IPv6 header inside ICMP */
474a36e185eSStefano Brivio 	skb_reset_network_header(skb);
475a36e185eSStefano Brivio 
476a36e185eSStefano Brivio 	/* Transport header needs to point to the UDP header */
477a36e185eSStefano Brivio 	skb_set_transport_header(skb, offset);
478a36e185eSStefano Brivio 
479e7cc0824SStefano Brivio 	sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
480e7cc0824SStefano Brivio 			       &hdr->saddr, uh->dest,
481e7cc0824SStefano Brivio 			       inet6_iif(skb), 0, udptable, skb);
482e7cc0824SStefano Brivio 	if (sk) {
483e7cc0824SStefano Brivio 		int (*lookup)(struct sock *sk, struct sk_buff *skb);
484e7cc0824SStefano Brivio 		struct udp_sock *up = udp_sk(sk);
485e7cc0824SStefano Brivio 
486a36e185eSStefano Brivio 		lookup = READ_ONCE(up->encap_err_lookup);
487a36e185eSStefano Brivio 		if (!lookup || lookup(sk, skb))
488a36e185eSStefano Brivio 			sk = NULL;
489e7cc0824SStefano Brivio 	}
490e7cc0824SStefano Brivio 
491e7cc0824SStefano Brivio 	if (!sk) {
492e7cc0824SStefano Brivio 		sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
493e7cc0824SStefano Brivio 							offset, info));
494e7cc0824SStefano Brivio 	}
495a36e185eSStefano Brivio 
496a36e185eSStefano Brivio 	skb_set_transport_header(skb, transport_offset);
497a36e185eSStefano Brivio 	skb_set_network_header(skb, network_offset);
498e7cc0824SStefano Brivio 
499a36e185eSStefano Brivio 	return sk;
500a36e185eSStefano Brivio }
501a36e185eSStefano Brivio 
50232bbd879SStefano Brivio int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
503d5fdd6baSBrian Haley 		   u8 type, u8 code, int offset, __be32 info,
504645ca708SEric Dumazet 		   struct udp_table *udptable)
505db8dac20SDavid S. Miller {
506db8dac20SDavid S. Miller 	struct ipv6_pinfo *np;
507b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
508b71d1d42SEric Dumazet 	const struct in6_addr *saddr = &hdr->saddr;
509b71d1d42SEric Dumazet 	const struct in6_addr *daddr = &hdr->daddr;
510db8dac20SDavid S. Miller 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
511a36e185eSStefano Brivio 	bool tunnel = false;
512db8dac20SDavid S. Miller 	struct sock *sk;
513e0d8c1b7SWei Wang 	int harderr;
514db8dac20SDavid S. Miller 	int err;
5157304fe46SDuan Jiong 	struct net *net = dev_net(skb->dev);
516db8dac20SDavid S. Miller 
517e32ea7e7SCraig Gallek 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
518f64bf6b8SMike Manning 			       inet6_iif(skb), inet6_sdif(skb), udptable, skb);
51963159f29SIan Morris 	if (!sk) {
520a36e185eSStefano Brivio 		/* No socket for error: try tunnels before discarding */
521e7cc0824SStefano Brivio 		sk = ERR_PTR(-ENOENT);
522a36e185eSStefano Brivio 		if (static_branch_unlikely(&udpv6_encap_needed_key)) {
523a36e185eSStefano Brivio 			sk = __udp6_lib_err_encap(net, hdr, offset, uh,
524e7cc0824SStefano Brivio 						  udptable, skb,
525e7cc0824SStefano Brivio 						  opt, type, code, info);
526e7cc0824SStefano Brivio 			if (!sk)
527e7cc0824SStefano Brivio 				return 0;
528a36e185eSStefano Brivio 		}
529a36e185eSStefano Brivio 
530e7cc0824SStefano Brivio 		if (IS_ERR(sk)) {
531a16292a0SEric Dumazet 			__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
5327304fe46SDuan Jiong 					  ICMP6_MIB_INERRORS);
533e7cc0824SStefano Brivio 			return PTR_ERR(sk);
5347304fe46SDuan Jiong 		}
535e7cc0824SStefano Brivio 
536a36e185eSStefano Brivio 		tunnel = true;
537a36e185eSStefano Brivio 	}
538db8dac20SDavid S. Miller 
539e0d8c1b7SWei Wang 	harderr = icmpv6_err_convert(type, code, &err);
540e0d8c1b7SWei Wang 	np = inet6_sk(sk);
541e0d8c1b7SWei Wang 
54293b36cf3SHannes Frederic Sowa 	if (type == ICMPV6_PKT_TOOBIG) {
54393b36cf3SHannes Frederic Sowa 		if (!ip6_sk_accept_pmtu(sk))
54493b36cf3SHannes Frederic Sowa 			goto out;
54581aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
546e0d8c1b7SWei Wang 		if (np->pmtudisc != IPV6_PMTUDISC_DONT)
547e0d8c1b7SWei Wang 			harderr = 1;
54893b36cf3SHannes Frederic Sowa 	}
5491a462d18SDuan Jiong 	if (type == NDISC_REDIRECT) {
550a36e185eSStefano Brivio 		if (tunnel) {
551a36e185eSStefano Brivio 			ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
552a36e185eSStefano Brivio 				     sk->sk_mark, sk->sk_uid);
553a36e185eSStefano Brivio 		} else {
554ec18d9a2SDavid S. Miller 			ip6_sk_redirect(skb, sk);
555a36e185eSStefano Brivio 		}
5561a462d18SDuan Jiong 		goto out;
5571a462d18SDuan Jiong 	}
55881aded24SDavid S. Miller 
559a36e185eSStefano Brivio 	/* Tunnels don't have an application socket: don't pass errors back */
560a36e185eSStefano Brivio 	if (tunnel)
561a36e185eSStefano Brivio 		goto out;
562a36e185eSStefano Brivio 
563e0d8c1b7SWei Wang 	if (!np->recverr) {
564e0d8c1b7SWei Wang 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
565db8dac20SDavid S. Miller 			goto out;
566e0d8c1b7SWei Wang 	} else {
567db8dac20SDavid S. Miller 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
568e0d8c1b7SWei Wang 	}
569b1faf566SEric Dumazet 
570db8dac20SDavid S. Miller 	sk->sk_err = err;
571db8dac20SDavid S. Miller 	sk->sk_error_report(sk);
572db8dac20SDavid S. Miller out:
57332bbd879SStefano Brivio 	return 0;
574db8dac20SDavid S. Miller }
575db8dac20SDavid S. Miller 
576a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
577f7ad74feSBenjamin LaHaise {
578f7ad74feSBenjamin LaHaise 	int rc;
579f7ad74feSBenjamin LaHaise 
580efe4208fSEric Dumazet 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
581f7ad74feSBenjamin LaHaise 		sock_rps_save_rxhash(sk, skb);
582005ec974SShawn Bohrer 		sk_mark_napi_id(sk, skb);
5832c8c56e1SEric Dumazet 		sk_incoming_cpu_update(sk);
584e68b6e50SEric Dumazet 	} else {
585e68b6e50SEric Dumazet 		sk_mark_napi_id_once(sk, skb);
586005ec974SShawn Bohrer 	}
587f7ad74feSBenjamin LaHaise 
588850cbaddSPaolo Abeni 	rc = __udp_enqueue_schedule_skb(sk, skb);
589f7ad74feSBenjamin LaHaise 	if (rc < 0) {
590f7ad74feSBenjamin LaHaise 		int is_udplite = IS_UDPLITE(sk);
591f7ad74feSBenjamin LaHaise 
592f7ad74feSBenjamin LaHaise 		/* Note that an ENOMEM error is charged twice */
593f7ad74feSBenjamin LaHaise 		if (rc == -ENOMEM)
594e61da9e2SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
595f7ad74feSBenjamin LaHaise 					 UDP_MIB_RCVBUFERRORS, is_udplite);
596e61da9e2SEric Dumazet 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
597f7ad74feSBenjamin LaHaise 		kfree_skb(skb);
598f7ad74feSBenjamin LaHaise 		return -1;
599f7ad74feSBenjamin LaHaise 	}
600850cbaddSPaolo Abeni 
601f7ad74feSBenjamin LaHaise 	return 0;
602f7ad74feSBenjamin LaHaise }
603f7ad74feSBenjamin LaHaise 
60432bbd879SStefano Brivio static __inline__ int udpv6_err(struct sk_buff *skb,
605d5fdd6baSBrian Haley 				struct inet6_skb_parm *opt, u8 type,
606d5fdd6baSBrian Haley 				u8 code, int offset, __be32 info)
607db8dac20SDavid S. Miller {
60832bbd879SStefano Brivio 	return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
609db8dac20SDavid S. Miller }
610db8dac20SDavid S. Miller 
611cf329aa4SPaolo Abeni static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
612db8dac20SDavid S. Miller {
613db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
614db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
615db8dac20SDavid S. Miller 
616db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
617db8dac20SDavid S. Miller 		goto drop;
618db8dac20SDavid S. Miller 
61988ab3108SDavidlohr Bueso 	if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
620d7f3f621SBenjamin LaHaise 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
621d7f3f621SBenjamin LaHaise 
622d7f3f621SBenjamin LaHaise 		/*
623d7f3f621SBenjamin LaHaise 		 * This is an encapsulation socket so pass the skb to
624d7f3f621SBenjamin LaHaise 		 * the socket's udp_encap_rcv() hook. Otherwise, just
625d7f3f621SBenjamin LaHaise 		 * fall through and pass this up the UDP socket.
626d7f3f621SBenjamin LaHaise 		 * up->encap_rcv() returns the following value:
627d7f3f621SBenjamin LaHaise 		 * =0 if skb was successfully passed to the encap
628d7f3f621SBenjamin LaHaise 		 *    handler or was discarded by it.
629d7f3f621SBenjamin LaHaise 		 * >0 if skb should be passed on to UDP.
630d7f3f621SBenjamin LaHaise 		 * <0 if skb should be resubmitted as proto -N
631d7f3f621SBenjamin LaHaise 		 */
632d7f3f621SBenjamin LaHaise 
633d7f3f621SBenjamin LaHaise 		/* if we're overly short, let UDP handle it */
6346aa7de05SMark Rutland 		encap_rcv = READ_ONCE(up->encap_rcv);
635e5aed006SHannes Frederic Sowa 		if (encap_rcv) {
636d7f3f621SBenjamin LaHaise 			int ret;
637d7f3f621SBenjamin LaHaise 
6380a80966bSTom Herbert 			/* Verify checksum before giving to encap */
6390a80966bSTom Herbert 			if (udp_lib_checksum_complete(skb))
6400a80966bSTom Herbert 				goto csum_error;
6410a80966bSTom Herbert 
642d7f3f621SBenjamin LaHaise 			ret = encap_rcv(sk, skb);
643d7f3f621SBenjamin LaHaise 			if (ret <= 0) {
64402c22347SEric Dumazet 				__UDP_INC_STATS(sock_net(sk),
645d7f3f621SBenjamin LaHaise 						UDP_MIB_INDATAGRAMS,
646d7f3f621SBenjamin LaHaise 						is_udplite);
647d7f3f621SBenjamin LaHaise 				return -ret;
648d7f3f621SBenjamin LaHaise 			}
649d7f3f621SBenjamin LaHaise 		}
650d7f3f621SBenjamin LaHaise 
651d7f3f621SBenjamin LaHaise 		/* FALLTHROUGH -- it's a UDP Packet */
652d7f3f621SBenjamin LaHaise 	}
653d7f3f621SBenjamin LaHaise 
654db8dac20SDavid S. Miller 	/*
655db8dac20SDavid S. Miller 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
656db8dac20SDavid S. Miller 	 */
657db8dac20SDavid S. Miller 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
658db8dac20SDavid S. Miller 
659db8dac20SDavid S. Miller 		if (up->pcrlen == 0) {          /* full coverage was set  */
660ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
661db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, skb->len);
662db8dac20SDavid S. Miller 			goto drop;
663db8dac20SDavid S. Miller 		}
664db8dac20SDavid S. Miller 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
665ba7a46f1SJoe Perches 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
666db8dac20SDavid S. Miller 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
667db8dac20SDavid S. Miller 			goto drop;
668db8dac20SDavid S. Miller 		}
669db8dac20SDavid S. Miller 	}
670db8dac20SDavid S. Miller 
6714b943faeSPaolo Abeni 	prefetch(&sk->sk_rmem_alloc);
672ce25d66aSEric Dumazet 	if (rcu_access_pointer(sk->sk_filter) &&
673ce25d66aSEric Dumazet 	    udp_lib_checksum_complete(skb))
6746a5dc9e5SEric Dumazet 		goto csum_error;
675ce25d66aSEric Dumazet 
676ba66bbe5SDaniel Borkmann 	if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
677a6127697SMichal Kubeček 		goto drop;
678db8dac20SDavid S. Miller 
679e6afc8acSsamanthakumar 	udp_csum_pull_header(skb);
680cb80ef46SBenjamin LaHaise 
681d826eb14SEric Dumazet 	skb_dst_drop(skb);
682db8dac20SDavid S. Miller 
683850cbaddSPaolo Abeni 	return __udpv6_queue_rcv_skb(sk, skb);
6843e215c8dSJames M Leddy 
6856a5dc9e5SEric Dumazet csum_error:
68602c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
687db8dac20SDavid S. Miller drop:
68802c22347SEric Dumazet 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
689cb80ef46SBenjamin LaHaise 	atomic_inc(&sk->sk_drops);
690db8dac20SDavid S. Miller 	kfree_skb(skb);
691db8dac20SDavid S. Miller 	return -1;
692db8dac20SDavid S. Miller }
693db8dac20SDavid S. Miller 
694cf329aa4SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
695cf329aa4SPaolo Abeni {
696cf329aa4SPaolo Abeni 	struct sk_buff *next, *segs;
697cf329aa4SPaolo Abeni 	int ret;
698cf329aa4SPaolo Abeni 
699cf329aa4SPaolo Abeni 	if (likely(!udp_unexpected_gso(sk, skb)))
700cf329aa4SPaolo Abeni 		return udpv6_queue_rcv_one_skb(sk, skb);
701cf329aa4SPaolo Abeni 
702cf329aa4SPaolo Abeni 	__skb_push(skb, -skb_mac_offset(skb));
703cf329aa4SPaolo Abeni 	segs = udp_rcv_segment(sk, skb, false);
704cf329aa4SPaolo Abeni 	for (skb = segs; skb; skb = next) {
705cf329aa4SPaolo Abeni 		next = skb->next;
706cf329aa4SPaolo Abeni 		__skb_pull(skb, skb_transport_offset(skb));
707cf329aa4SPaolo Abeni 
708cf329aa4SPaolo Abeni 		ret = udpv6_queue_rcv_one_skb(sk, skb);
709cf329aa4SPaolo Abeni 		if (ret > 0)
710cf329aa4SPaolo Abeni 			ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
711cf329aa4SPaolo Abeni 						 true);
712cf329aa4SPaolo Abeni 	}
713cf329aa4SPaolo Abeni 	return 0;
714cf329aa4SPaolo Abeni }
715cf329aa4SPaolo Abeni 
7165cf3d461SDavid Held static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
717b71d1d42SEric Dumazet 				   __be16 loc_port, const struct in6_addr *loc_addr,
718b71d1d42SEric Dumazet 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
7197bd2db40SDewi Morgan 				   int dif, int sdif, unsigned short hnum)
720db8dac20SDavid S. Miller {
7219e89fd8bSSven Wegener 	struct inet_sock *inet = inet_sk(sk);
722db8dac20SDavid S. Miller 
7239e89fd8bSSven Wegener 	if (!net_eq(sock_net(sk), net))
7245cf3d461SDavid Held 		return false;
725b8ad0cbcSDaniel Lezcano 
7265cf3d461SDavid Held 	if (udp_sk(sk)->udp_port_hash != hnum ||
7275cf3d461SDavid Held 	    sk->sk_family != PF_INET6 ||
7285cf3d461SDavid Held 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
7295cf3d461SDavid Held 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
7305cf3d461SDavid Held 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
7317bd2db40SDewi Morgan 	    !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) ||
73233b4b015SHenning Rogge 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
73333b4b015SHenning Rogge 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
7345cf3d461SDavid Held 		return false;
7359e89fd8bSSven Wegener 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
7365cf3d461SDavid Held 		return false;
7375cf3d461SDavid Held 	return true;
738db8dac20SDavid S. Miller }
739db8dac20SDavid S. Miller 
7404068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
7414068579eSTom Herbert {
7424068579eSTom Herbert 	/* RFC 2460 section 8.1 says that we SHOULD log
7434068579eSTom Herbert 	 * this error. Well, it is reasonable.
7444068579eSTom Herbert 	 */
745ba7a46f1SJoe Perches 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
7464068579eSTom Herbert 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
7474068579eSTom Herbert 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
7484068579eSTom Herbert }
7494068579eSTom Herbert 
750db8dac20SDavid S. Miller /*
751db8dac20SDavid S. Miller  * Note: called only from the BH handler context,
752db8dac20SDavid S. Miller  * so we don't need to lock the hashes.
753db8dac20SDavid S. Miller  */
754e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
755b71d1d42SEric Dumazet 		const struct in6_addr *saddr, const struct in6_addr *daddr,
75636cbb245SRick Jones 		struct udp_table *udptable, int proto)
757db8dac20SDavid S. Miller {
758ca065d0cSEric Dumazet 	struct sock *sk, *first = NULL;
759db8dac20SDavid S. Miller 	const struct udphdr *uh = udp_hdr(skb);
7605cf3d461SDavid Held 	unsigned short hnum = ntohs(uh->dest);
7615cf3d461SDavid Held 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
762ca065d0cSEric Dumazet 	unsigned int offset = offsetof(typeof(*sk), sk_node);
7632dc41cffSDavid Held 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
764ca065d0cSEric Dumazet 	int dif = inet6_iif(skb);
7657bd2db40SDewi Morgan 	int sdif = inet6_sdif(skb);
766ca065d0cSEric Dumazet 	struct hlist_node *node;
767ca065d0cSEric Dumazet 	struct sk_buff *nskb;
7682dc41cffSDavid Held 
7692dc41cffSDavid Held 	if (use_hash2) {
770f0b1e64cSMartin KaFai Lau 		hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
77173e2d5e3SPablo Neira 			    udptable->mask;
772f0b1e64cSMartin KaFai Lau 		hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
7732dc41cffSDavid Held start_lookup:
77473e2d5e3SPablo Neira 		hslot = &udptable->hash2[hash2];
7752dc41cffSDavid Held 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
7762dc41cffSDavid Held 	}
777db8dac20SDavid S. Miller 
778ca065d0cSEric Dumazet 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
779ca065d0cSEric Dumazet 		if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
7807bd2db40SDewi Morgan 					    uh->source, saddr, dif, sdif,
7817bd2db40SDewi Morgan 					    hnum))
782ca065d0cSEric Dumazet 			continue;
7831c19448cSTom Herbert 		/* If zero checksum and no_check is not on for
7844068579eSTom Herbert 		 * the socket then skip it.
7854068579eSTom Herbert 		 */
786ca065d0cSEric Dumazet 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
787ca065d0cSEric Dumazet 			continue;
788ca065d0cSEric Dumazet 		if (!first) {
789ca065d0cSEric Dumazet 			first = sk;
790ca065d0cSEric Dumazet 			continue;
791db8dac20SDavid S. Miller 		}
792ca065d0cSEric Dumazet 		nskb = skb_clone(skb, GFP_ATOMIC);
793ca065d0cSEric Dumazet 		if (unlikely(!nskb)) {
794ca065d0cSEric Dumazet 			atomic_inc(&sk->sk_drops);
79502c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
796ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
79702c22347SEric Dumazet 			__UDP6_INC_STATS(net, UDP_MIB_INERRORS,
798ca065d0cSEric Dumazet 					 IS_UDPLITE(sk));
799ca065d0cSEric Dumazet 			continue;
800a1ab77f9SEric Dumazet 		}
801db8dac20SDavid S. Miller 
802ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(sk, nskb) > 0)
803ca065d0cSEric Dumazet 			consume_skb(nskb);
804ca065d0cSEric Dumazet 	}
805a1ab77f9SEric Dumazet 
8062dc41cffSDavid Held 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
8072dc41cffSDavid Held 	if (use_hash2 && hash2 != hash2_any) {
8082dc41cffSDavid Held 		hash2 = hash2_any;
8092dc41cffSDavid Held 		goto start_lookup;
8102dc41cffSDavid Held 	}
8112dc41cffSDavid Held 
812ca065d0cSEric Dumazet 	if (first) {
813ca065d0cSEric Dumazet 		if (udpv6_queue_rcv_skb(first, skb) > 0)
814ca065d0cSEric Dumazet 			consume_skb(skb);
815a1ab77f9SEric Dumazet 	} else {
816ca065d0cSEric Dumazet 		kfree_skb(skb);
81702c22347SEric Dumazet 		__UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
81836cbb245SRick Jones 				 proto == IPPROTO_UDPLITE);
819a1ab77f9SEric Dumazet 	}
820db8dac20SDavid S. Miller 	return 0;
821db8dac20SDavid S. Miller }
822db8dac20SDavid S. Miller 
82364f0f5d1SPaolo Abeni static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
82464f0f5d1SPaolo Abeni {
82564f0f5d1SPaolo Abeni 	if (udp_sk_rx_dst_set(sk, dst)) {
82664f0f5d1SPaolo Abeni 		const struct rt6_info *rt = (const struct rt6_info *)dst;
82764f0f5d1SPaolo Abeni 
82864f0f5d1SPaolo Abeni 		inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt);
82964f0f5d1SPaolo Abeni 	}
83064f0f5d1SPaolo Abeni }
83164f0f5d1SPaolo Abeni 
832eb63f296SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
833eb63f296SPaolo Abeni  * return code conversion for ip layer consumption
834eb63f296SPaolo Abeni  */
835eb63f296SPaolo Abeni static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
836eb63f296SPaolo Abeni 				struct udphdr *uh)
837eb63f296SPaolo Abeni {
838eb63f296SPaolo Abeni 	int ret;
839eb63f296SPaolo Abeni 
840eb63f296SPaolo Abeni 	if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
841eb63f296SPaolo Abeni 		skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
842eb63f296SPaolo Abeni 					 ip6_compute_pseudo);
843eb63f296SPaolo Abeni 
844eb63f296SPaolo Abeni 	ret = udpv6_queue_rcv_skb(sk, skb);
845eb63f296SPaolo Abeni 
84684dad559SPaolo Abeni 	/* a return value > 0 means to resubmit the input */
847eb63f296SPaolo Abeni 	if (ret > 0)
84884dad559SPaolo Abeni 		return ret;
849eb63f296SPaolo Abeni 	return 0;
850eb63f296SPaolo Abeni }
851eb63f296SPaolo Abeni 
852645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
853db8dac20SDavid S. Miller 		   int proto)
854db8dac20SDavid S. Miller {
855b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
856ca065d0cSEric Dumazet 	struct net *net = dev_net(skb->dev);
857ca065d0cSEric Dumazet 	struct udphdr *uh;
858ca065d0cSEric Dumazet 	struct sock *sk;
859db8dac20SDavid S. Miller 	u32 ulen = 0;
860db8dac20SDavid S. Miller 
861db8dac20SDavid S. Miller 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
862d6bc0149SBjørn Mork 		goto discard;
863db8dac20SDavid S. Miller 
864db8dac20SDavid S. Miller 	saddr = &ipv6_hdr(skb)->saddr;
865db8dac20SDavid S. Miller 	daddr = &ipv6_hdr(skb)->daddr;
866db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
867db8dac20SDavid S. Miller 
868db8dac20SDavid S. Miller 	ulen = ntohs(uh->len);
869db8dac20SDavid S. Miller 	if (ulen > skb->len)
870db8dac20SDavid S. Miller 		goto short_packet;
871db8dac20SDavid S. Miller 
872db8dac20SDavid S. Miller 	if (proto == IPPROTO_UDP) {
873db8dac20SDavid S. Miller 		/* UDP validates ulen. */
874db8dac20SDavid S. Miller 
875db8dac20SDavid S. Miller 		/* Check for jumbo payload */
876db8dac20SDavid S. Miller 		if (ulen == 0)
877db8dac20SDavid S. Miller 			ulen = skb->len;
878db8dac20SDavid S. Miller 
879db8dac20SDavid S. Miller 		if (ulen < sizeof(*uh))
880db8dac20SDavid S. Miller 			goto short_packet;
881db8dac20SDavid S. Miller 
882db8dac20SDavid S. Miller 		if (ulen < skb->len) {
883db8dac20SDavid S. Miller 			if (pskb_trim_rcsum(skb, ulen))
884db8dac20SDavid S. Miller 				goto short_packet;
885db8dac20SDavid S. Miller 			saddr = &ipv6_hdr(skb)->saddr;
886db8dac20SDavid S. Miller 			daddr = &ipv6_hdr(skb)->daddr;
887db8dac20SDavid S. Miller 			uh = udp_hdr(skb);
888db8dac20SDavid S. Miller 		}
889db8dac20SDavid S. Miller 	}
890db8dac20SDavid S. Miller 
891db8dac20SDavid S. Miller 	if (udp6_csum_init(skb, uh, proto))
8926a5dc9e5SEric Dumazet 		goto csum_error;
893db8dac20SDavid S. Miller 
894c9f2c1aeSPaolo Abeni 	/* Check if the socket is already available, e.g. due to early demux */
895c9f2c1aeSPaolo Abeni 	sk = skb_steal_sock(skb);
896c9f2c1aeSPaolo Abeni 	if (sk) {
897c9f2c1aeSPaolo Abeni 		struct dst_entry *dst = skb_dst(skb);
898c9f2c1aeSPaolo Abeni 		int ret;
899c9f2c1aeSPaolo Abeni 
900c9f2c1aeSPaolo Abeni 		if (unlikely(sk->sk_rx_dst != dst))
90164f0f5d1SPaolo Abeni 			udp6_sk_rx_dst_set(sk, dst);
902c9f2c1aeSPaolo Abeni 
903eb63f296SPaolo Abeni 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
904c9f2c1aeSPaolo Abeni 			sock_put(sk);
905eb63f296SPaolo Abeni 			goto report_csum_error;
906eb63f296SPaolo Abeni 		}
907c9f2c1aeSPaolo Abeni 
908eb63f296SPaolo Abeni 		ret = udp6_unicast_rcv_skb(sk, skb, uh);
909eb63f296SPaolo Abeni 		sock_put(sk);
910c9f2c1aeSPaolo Abeni 		return ret;
911c9f2c1aeSPaolo Abeni 	}
912c9f2c1aeSPaolo Abeni 
913db8dac20SDavid S. Miller 	/*
914db8dac20SDavid S. Miller 	 *	Multicast receive code
915db8dac20SDavid S. Miller 	 */
916db8dac20SDavid S. Miller 	if (ipv6_addr_is_multicast(daddr))
917e3163493SPavel Emelyanov 		return __udp6_lib_mcast_deliver(net, skb,
91836cbb245SRick Jones 				saddr, daddr, udptable, proto);
919db8dac20SDavid S. Miller 
920db8dac20SDavid S. Miller 	/* Unicast */
921607c4aafSKOVACS Krisztian 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
92253b24b8fSIan Morris 	if (sk) {
923eb63f296SPaolo Abeni 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
924eb63f296SPaolo Abeni 			goto report_csum_error;
925eb63f296SPaolo Abeni 		return udp6_unicast_rcv_skb(sk, skb, uh);
9264068579eSTom Herbert 	}
9274068579eSTom Herbert 
928eb63f296SPaolo Abeni 	if (!uh->check)
929eb63f296SPaolo Abeni 		goto report_csum_error;
9304068579eSTom Herbert 
931db8dac20SDavid S. Miller 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
932db8dac20SDavid S. Miller 		goto discard;
933db8dac20SDavid S. Miller 
934db8dac20SDavid S. Miller 	if (udp_lib_checksum_complete(skb))
9356a5dc9e5SEric Dumazet 		goto csum_error;
936db8dac20SDavid S. Miller 
93702c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
9383ffe533cSAlexey Dobriyan 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
939db8dac20SDavid S. Miller 
940db8dac20SDavid S. Miller 	kfree_skb(skb);
941db8dac20SDavid S. Miller 	return 0;
942db8dac20SDavid S. Miller 
943db8dac20SDavid S. Miller short_packet:
944ba7a46f1SJoe Perches 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
945db8dac20SDavid S. Miller 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
946ba7a46f1SJoe Perches 			    saddr, ntohs(uh->source),
947ba7a46f1SJoe Perches 			    ulen, skb->len,
948ba7a46f1SJoe Perches 			    daddr, ntohs(uh->dest));
9496a5dc9e5SEric Dumazet 	goto discard;
950eb63f296SPaolo Abeni 
951eb63f296SPaolo Abeni report_csum_error:
952eb63f296SPaolo Abeni 	udp6_csum_zero_error(skb);
9536a5dc9e5SEric Dumazet csum_error:
95402c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
955db8dac20SDavid S. Miller discard:
95602c22347SEric Dumazet 	__UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
957db8dac20SDavid S. Miller 	kfree_skb(skb);
958db8dac20SDavid S. Miller 	return 0;
959db8dac20SDavid S. Miller }
960db8dac20SDavid S. Miller 
9610bd84065Ssubashab@codeaurora.org 
9625425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net,
9635425077dSsubashab@codeaurora.org 			__be16 loc_port, const struct in6_addr *loc_addr,
9645425077dSsubashab@codeaurora.org 			__be16 rmt_port, const struct in6_addr *rmt_addr,
9654297a0efSDavid Ahern 			int dif, int sdif)
9665425077dSsubashab@codeaurora.org {
9670bd84065Ssubashab@codeaurora.org 	unsigned short hnum = ntohs(loc_port);
968f0b1e64cSMartin KaFai Lau 	unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
9690bd84065Ssubashab@codeaurora.org 	unsigned int slot2 = hash2 & udp_table.mask;
9700bd84065Ssubashab@codeaurora.org 	struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
9710bd84065Ssubashab@codeaurora.org 	const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
9725425077dSsubashab@codeaurora.org 	struct sock *sk;
9735425077dSsubashab@codeaurora.org 
9740bd84065Ssubashab@codeaurora.org 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
97585cb73ffSWei Wang 		if (sk->sk_state == TCP_ESTABLISHED &&
9764297a0efSDavid Ahern 		    INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
9775425077dSsubashab@codeaurora.org 			return sk;
9780bd84065Ssubashab@codeaurora.org 		/* Only check first socket in chain */
9790bd84065Ssubashab@codeaurora.org 		break;
9800bd84065Ssubashab@codeaurora.org 	}
9810bd84065Ssubashab@codeaurora.org 	return NULL;
9825425077dSsubashab@codeaurora.org }
9835425077dSsubashab@codeaurora.org 
9845425077dSsubashab@codeaurora.org static void udp_v6_early_demux(struct sk_buff *skb)
9855425077dSsubashab@codeaurora.org {
9865425077dSsubashab@codeaurora.org 	struct net *net = dev_net(skb->dev);
9875425077dSsubashab@codeaurora.org 	const struct udphdr *uh;
9885425077dSsubashab@codeaurora.org 	struct sock *sk;
9895425077dSsubashab@codeaurora.org 	struct dst_entry *dst;
9905425077dSsubashab@codeaurora.org 	int dif = skb->dev->ifindex;
9914297a0efSDavid Ahern 	int sdif = inet6_sdif(skb);
9925425077dSsubashab@codeaurora.org 
9935425077dSsubashab@codeaurora.org 	if (!pskb_may_pull(skb, skb_transport_offset(skb) +
9945425077dSsubashab@codeaurora.org 	    sizeof(struct udphdr)))
9955425077dSsubashab@codeaurora.org 		return;
9965425077dSsubashab@codeaurora.org 
9975425077dSsubashab@codeaurora.org 	uh = udp_hdr(skb);
9985425077dSsubashab@codeaurora.org 
9995425077dSsubashab@codeaurora.org 	if (skb->pkt_type == PACKET_HOST)
10005425077dSsubashab@codeaurora.org 		sk = __udp6_lib_demux_lookup(net, uh->dest,
10015425077dSsubashab@codeaurora.org 					     &ipv6_hdr(skb)->daddr,
10025425077dSsubashab@codeaurora.org 					     uh->source, &ipv6_hdr(skb)->saddr,
10034297a0efSDavid Ahern 					     dif, sdif);
10045425077dSsubashab@codeaurora.org 	else
10055425077dSsubashab@codeaurora.org 		return;
10065425077dSsubashab@codeaurora.org 
100741c6d650SReshetova, Elena 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
10085425077dSsubashab@codeaurora.org 		return;
10095425077dSsubashab@codeaurora.org 
10105425077dSsubashab@codeaurora.org 	skb->sk = sk;
10115425077dSsubashab@codeaurora.org 	skb->destructor = sock_efree;
10125425077dSsubashab@codeaurora.org 	dst = READ_ONCE(sk->sk_rx_dst);
10135425077dSsubashab@codeaurora.org 
10145425077dSsubashab@codeaurora.org 	if (dst)
10155425077dSsubashab@codeaurora.org 		dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
10165425077dSsubashab@codeaurora.org 	if (dst) {
1017d24406c8SWei Wang 		/* set noref for now.
1018d24406c8SWei Wang 		 * any place which wants to hold dst has to call
1019d24406c8SWei Wang 		 * dst_hold_safe()
1020d24406c8SWei Wang 		 */
10215425077dSsubashab@codeaurora.org 		skb_dst_set_noref(skb, dst);
10225425077dSsubashab@codeaurora.org 	}
10235425077dSsubashab@codeaurora.org }
10245425077dSsubashab@codeaurora.org 
10250e219ae4SPaolo Abeni INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
1026db8dac20SDavid S. Miller {
1027645ca708SEric Dumazet 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1028db8dac20SDavid S. Miller }
1029db8dac20SDavid S. Miller 
1030db8dac20SDavid S. Miller /*
1031db8dac20SDavid S. Miller  * Throw away all pending data and cancel the corking. Socket is locked.
1032db8dac20SDavid S. Miller  */
1033db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
1034db8dac20SDavid S. Miller {
1035db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1036db8dac20SDavid S. Miller 
103736d926b9SDenis V. Lunev 	if (up->pending == AF_INET)
103836d926b9SDenis V. Lunev 		udp_flush_pending_frames(sk);
103936d926b9SDenis V. Lunev 	else if (up->pending) {
1040db8dac20SDavid S. Miller 		up->len = 0;
1041db8dac20SDavid S. Miller 		up->pending = 0;
1042db8dac20SDavid S. Miller 		ip6_flush_pending_frames(sk);
1043db8dac20SDavid S. Miller 	}
1044db8dac20SDavid S. Miller }
1045db8dac20SDavid S. Miller 
1046d74bad4eSAndrey Ignatov static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
1047d74bad4eSAndrey Ignatov 			     int addr_len)
1048d74bad4eSAndrey Ignatov {
1049bddc028aSTetsuo Handa 	if (addr_len < offsetofend(struct sockaddr, sa_family))
1050bddc028aSTetsuo Handa 		return -EINVAL;
1051d74bad4eSAndrey Ignatov 	/* The following checks are replicated from __ip6_datagram_connect()
1052d74bad4eSAndrey Ignatov 	 * and intended to prevent BPF program called below from accessing
1053d74bad4eSAndrey Ignatov 	 * bytes that are out of the bound specified by user in addr_len.
1054d74bad4eSAndrey Ignatov 	 */
1055d74bad4eSAndrey Ignatov 	if (uaddr->sa_family == AF_INET) {
1056d74bad4eSAndrey Ignatov 		if (__ipv6_only_sock(sk))
1057d74bad4eSAndrey Ignatov 			return -EAFNOSUPPORT;
1058d74bad4eSAndrey Ignatov 		return udp_pre_connect(sk, uaddr, addr_len);
1059d74bad4eSAndrey Ignatov 	}
1060d74bad4eSAndrey Ignatov 
1061d74bad4eSAndrey Ignatov 	if (addr_len < SIN6_LEN_RFC2133)
1062d74bad4eSAndrey Ignatov 		return -EINVAL;
1063d74bad4eSAndrey Ignatov 
1064d74bad4eSAndrey Ignatov 	return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr);
1065d74bad4eSAndrey Ignatov }
1066d74bad4eSAndrey Ignatov 
1067493c6be3SSridhar Samudrala /**
1068493c6be3SSridhar Samudrala  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
1069493c6be3SSridhar Samudrala  *	@sk:	socket we are sending on
1070493c6be3SSridhar Samudrala  *	@skb:	sk_buff containing the filled-in UDP header
1071493c6be3SSridhar Samudrala  *		(checksum field must be zeroed out)
1072493c6be3SSridhar Samudrala  */
1073493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1074493c6be3SSridhar Samudrala 				 const struct in6_addr *saddr,
1075493c6be3SSridhar Samudrala 				 const struct in6_addr *daddr, int len)
1076493c6be3SSridhar Samudrala {
1077493c6be3SSridhar Samudrala 	unsigned int offset;
1078493c6be3SSridhar Samudrala 	struct udphdr *uh = udp_hdr(skb);
1079d39d938cSVlad Yasevich 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1080493c6be3SSridhar Samudrala 	__wsum csum = 0;
1081493c6be3SSridhar Samudrala 
1082d39d938cSVlad Yasevich 	if (!frags) {
1083493c6be3SSridhar Samudrala 		/* Only one fragment on the socket.  */
1084493c6be3SSridhar Samudrala 		skb->csum_start = skb_transport_header(skb) - skb->head;
1085493c6be3SSridhar Samudrala 		skb->csum_offset = offsetof(struct udphdr, check);
1086493c6be3SSridhar Samudrala 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1087493c6be3SSridhar Samudrala 	} else {
1088493c6be3SSridhar Samudrala 		/*
1089493c6be3SSridhar Samudrala 		 * HW-checksum won't work as there are two or more
1090493c6be3SSridhar Samudrala 		 * fragments on the socket so that all csums of sk_buffs
1091493c6be3SSridhar Samudrala 		 * should be together
1092493c6be3SSridhar Samudrala 		 */
1093493c6be3SSridhar Samudrala 		offset = skb_transport_offset(skb);
1094493c6be3SSridhar Samudrala 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
109563ecc3d9SSubash Abhinov Kasiviswanathan 		csum = skb->csum;
1096493c6be3SSridhar Samudrala 
1097493c6be3SSridhar Samudrala 		skb->ip_summed = CHECKSUM_NONE;
1098493c6be3SSridhar Samudrala 
1099d39d938cSVlad Yasevich 		do {
1100d39d938cSVlad Yasevich 			csum = csum_add(csum, frags->csum);
1101d39d938cSVlad Yasevich 		} while ((frags = frags->next));
1102493c6be3SSridhar Samudrala 
1103493c6be3SSridhar Samudrala 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1104493c6be3SSridhar Samudrala 					    csum);
1105493c6be3SSridhar Samudrala 		if (uh->check == 0)
1106493c6be3SSridhar Samudrala 			uh->check = CSUM_MANGLED_0;
1107493c6be3SSridhar Samudrala 	}
1108493c6be3SSridhar Samudrala }
1109493c6be3SSridhar Samudrala 
1110db8dac20SDavid S. Miller /*
1111db8dac20SDavid S. Miller  *	Sending
1112db8dac20SDavid S. Miller  */
1113db8dac20SDavid S. Miller 
1114bec1f6f6SWillem de Bruijn static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1115bec1f6f6SWillem de Bruijn 			   struct inet_cork *cork)
1116db8dac20SDavid S. Miller {
1117d39d938cSVlad Yasevich 	struct sock *sk = skb->sk;
1118db8dac20SDavid S. Miller 	struct udphdr *uh;
1119db8dac20SDavid S. Miller 	int err = 0;
1120db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1121db8dac20SDavid S. Miller 	__wsum csum = 0;
1122d39d938cSVlad Yasevich 	int offset = skb_transport_offset(skb);
1123d39d938cSVlad Yasevich 	int len = skb->len - offset;
1124db8dac20SDavid S. Miller 
1125db8dac20SDavid S. Miller 	/*
1126db8dac20SDavid S. Miller 	 * Create a UDP header
1127db8dac20SDavid S. Miller 	 */
1128db8dac20SDavid S. Miller 	uh = udp_hdr(skb);
11291958b856SDavid S. Miller 	uh->source = fl6->fl6_sport;
11301958b856SDavid S. Miller 	uh->dest = fl6->fl6_dport;
1131d39d938cSVlad Yasevich 	uh->len = htons(len);
1132db8dac20SDavid S. Miller 	uh->check = 0;
1133db8dac20SDavid S. Miller 
1134bec1f6f6SWillem de Bruijn 	if (cork->gso_size) {
1135bec1f6f6SWillem de Bruijn 		const int hlen = skb_network_header_len(skb) +
1136bec1f6f6SWillem de Bruijn 				 sizeof(struct udphdr);
1137bec1f6f6SWillem de Bruijn 
11380f149c9fSWillem de Bruijn 		if (hlen + cork->gso_size > cork->fragsize) {
11390f149c9fSWillem de Bruijn 			kfree_skb(skb);
1140bec1f6f6SWillem de Bruijn 			return -EINVAL;
11410f149c9fSWillem de Bruijn 		}
11420f149c9fSWillem de Bruijn 		if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) {
11430f149c9fSWillem de Bruijn 			kfree_skb(skb);
1144bec1f6f6SWillem de Bruijn 			return -EINVAL;
11450f149c9fSWillem de Bruijn 		}
11460f149c9fSWillem de Bruijn 		if (udp_sk(sk)->no_check6_tx) {
11470f149c9fSWillem de Bruijn 			kfree_skb(skb);
1148a8c744a8SWillem de Bruijn 			return -EINVAL;
11490f149c9fSWillem de Bruijn 		}
1150ff06342cSWillem de Bruijn 		if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
11510f149c9fSWillem de Bruijn 		    dst_xfrm(skb_dst(skb))) {
11520f149c9fSWillem de Bruijn 			kfree_skb(skb);
1153bec1f6f6SWillem de Bruijn 			return -EIO;
11540f149c9fSWillem de Bruijn 		}
1155bec1f6f6SWillem de Bruijn 
1156bec1f6f6SWillem de Bruijn 		skb_shinfo(skb)->gso_size = cork->gso_size;
1157bec1f6f6SWillem de Bruijn 		skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
1158a8c744a8SWillem de Bruijn 		goto csum_partial;
1159bec1f6f6SWillem de Bruijn 	}
1160bec1f6f6SWillem de Bruijn 
1161db8dac20SDavid S. Miller 	if (is_udplite)
1162d39d938cSVlad Yasevich 		csum = udplite_csum(skb);
1163d39d938cSVlad Yasevich 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
11644068579eSTom Herbert 		skb->ip_summed = CHECKSUM_NONE;
11654068579eSTom Herbert 		goto send;
11664068579eSTom Herbert 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1167a8c744a8SWillem de Bruijn csum_partial:
1168d39d938cSVlad Yasevich 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1169493c6be3SSridhar Samudrala 		goto send;
1170493c6be3SSridhar Samudrala 	} else
1171d39d938cSVlad Yasevich 		csum = udp_csum(skb);
1172db8dac20SDavid S. Miller 
1173db8dac20SDavid S. Miller 	/* add protocol-dependent pseudo-header */
11744c9483b2SDavid S. Miller 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1175d39d938cSVlad Yasevich 				    len, fl6->flowi6_proto, csum);
1176db8dac20SDavid S. Miller 	if (uh->check == 0)
1177db8dac20SDavid S. Miller 		uh->check = CSUM_MANGLED_0;
1178db8dac20SDavid S. Miller 
1179493c6be3SSridhar Samudrala send:
1180d39d938cSVlad Yasevich 	err = ip6_send_skb(skb);
11816ce9e7b5SEric Dumazet 	if (err) {
11826ce9e7b5SEric Dumazet 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
11836aef70a8SEric Dumazet 			UDP6_INC_STATS(sock_net(sk),
11846ce9e7b5SEric Dumazet 				       UDP_MIB_SNDBUFERRORS, is_udplite);
11856ce9e7b5SEric Dumazet 			err = 0;
11866ce9e7b5SEric Dumazet 		}
11876aef70a8SEric Dumazet 	} else {
11886aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
11896ce9e7b5SEric Dumazet 			       UDP_MIB_OUTDATAGRAMS, is_udplite);
11906aef70a8SEric Dumazet 	}
1191d39d938cSVlad Yasevich 	return err;
1192d39d938cSVlad Yasevich }
1193d39d938cSVlad Yasevich 
1194d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
1195d39d938cSVlad Yasevich {
1196d39d938cSVlad Yasevich 	struct sk_buff *skb;
1197d39d938cSVlad Yasevich 	struct udp_sock  *up = udp_sk(sk);
1198d39d938cSVlad Yasevich 	struct flowi6 fl6;
1199d39d938cSVlad Yasevich 	int err = 0;
1200d39d938cSVlad Yasevich 
1201d39d938cSVlad Yasevich 	if (up->pending == AF_INET)
1202d39d938cSVlad Yasevich 		return udp_push_pending_frames(sk);
1203d39d938cSVlad Yasevich 
1204d39d938cSVlad Yasevich 	/* ip6_finish_skb will release the cork, so make a copy of
1205d39d938cSVlad Yasevich 	 * fl6 here.
1206d39d938cSVlad Yasevich 	 */
1207d39d938cSVlad Yasevich 	fl6 = inet_sk(sk)->cork.fl.u.ip6;
1208d39d938cSVlad Yasevich 
1209d39d938cSVlad Yasevich 	skb = ip6_finish_skb(sk);
1210d39d938cSVlad Yasevich 	if (!skb)
1211d39d938cSVlad Yasevich 		goto out;
1212d39d938cSVlad Yasevich 
1213bec1f6f6SWillem de Bruijn 	err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base);
1214d39d938cSVlad Yasevich 
1215db8dac20SDavid S. Miller out:
1216db8dac20SDavid S. Miller 	up->len = 0;
1217db8dac20SDavid S. Miller 	up->pending = 0;
1218db8dac20SDavid S. Miller 	return err;
1219db8dac20SDavid S. Miller }
1220db8dac20SDavid S. Miller 
12211b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1222db8dac20SDavid S. Miller {
1223db8dac20SDavid S. Miller 	struct ipv6_txoptions opt_space;
1224db8dac20SDavid S. Miller 	struct udp_sock *up = udp_sk(sk);
1225db8dac20SDavid S. Miller 	struct inet_sock *inet = inet_sk(sk);
1226db8dac20SDavid S. Miller 	struct ipv6_pinfo *np = inet6_sk(sk);
1227342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
122820c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
1229db8dac20SDavid S. Miller 	struct ipv6_txoptions *opt = NULL;
123045f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
1231db8dac20SDavid S. Miller 	struct ip6_flowlabel *flowlabel = NULL;
12324c9483b2SDavid S. Miller 	struct flowi6 fl6;
1233db8dac20SDavid S. Miller 	struct dst_entry *dst;
123426879da5SWei Wang 	struct ipcm6_cookie ipc6;
1235db8dac20SDavid S. Miller 	int addr_len = msg->msg_namelen;
12369f542f61SAlexey Kodanev 	bool connected = false;
1237db8dac20SDavid S. Miller 	int ulen = len;
1238db8dac20SDavid S. Miller 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1239db8dac20SDavid S. Miller 	int err;
1240db8dac20SDavid S. Miller 	int is_udplite = IS_UDPLITE(sk);
1241db8dac20SDavid S. Miller 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1242db8dac20SDavid S. Miller 
1243b515430aSWillem de Bruijn 	ipcm6_init(&ipc6);
1244bec1f6f6SWillem de Bruijn 	ipc6.gso_size = up->gso_size;
12455fdaa88dSWillem de Bruijn 	ipc6.sockc.tsflags = sk->sk_tsflags;
124626879da5SWei Wang 
1247db8dac20SDavid S. Miller 	/* destination address check */
1248db8dac20SDavid S. Miller 	if (sin6) {
1249db8dac20SDavid S. Miller 		if (addr_len < offsetof(struct sockaddr, sa_data))
1250db8dac20SDavid S. Miller 			return -EINVAL;
1251db8dac20SDavid S. Miller 
1252db8dac20SDavid S. Miller 		switch (sin6->sin6_family) {
1253db8dac20SDavid S. Miller 		case AF_INET6:
1254db8dac20SDavid S. Miller 			if (addr_len < SIN6_LEN_RFC2133)
1255db8dac20SDavid S. Miller 				return -EINVAL;
1256db8dac20SDavid S. Miller 			daddr = &sin6->sin6_addr;
1257052d2369SJonathan T. Leighton 			if (ipv6_addr_any(daddr) &&
1258052d2369SJonathan T. Leighton 			    ipv6_addr_v4mapped(&np->saddr))
1259052d2369SJonathan T. Leighton 				ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1260052d2369SJonathan T. Leighton 						       daddr);
1261db8dac20SDavid S. Miller 			break;
1262db8dac20SDavid S. Miller 		case AF_INET:
1263db8dac20SDavid S. Miller 			goto do_udp_sendmsg;
1264db8dac20SDavid S. Miller 		case AF_UNSPEC:
1265db8dac20SDavid S. Miller 			msg->msg_name = sin6 = NULL;
1266db8dac20SDavid S. Miller 			msg->msg_namelen = addr_len = 0;
1267db8dac20SDavid S. Miller 			daddr = NULL;
1268db8dac20SDavid S. Miller 			break;
1269db8dac20SDavid S. Miller 		default:
1270db8dac20SDavid S. Miller 			return -EINVAL;
1271db8dac20SDavid S. Miller 		}
1272db8dac20SDavid S. Miller 	} else if (!up->pending) {
1273db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1274db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1275efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
1276db8dac20SDavid S. Miller 	} else
1277db8dac20SDavid S. Miller 		daddr = NULL;
1278db8dac20SDavid S. Miller 
1279db8dac20SDavid S. Miller 	if (daddr) {
1280db8dac20SDavid S. Miller 		if (ipv6_addr_v4mapped(daddr)) {
1281db8dac20SDavid S. Miller 			struct sockaddr_in sin;
1282db8dac20SDavid S. Miller 			sin.sin_family = AF_INET;
1283c720c7e8SEric Dumazet 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1284db8dac20SDavid S. Miller 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1285db8dac20SDavid S. Miller 			msg->msg_name = &sin;
1286db8dac20SDavid S. Miller 			msg->msg_namelen = sizeof(sin);
1287db8dac20SDavid S. Miller do_udp_sendmsg:
1288db8dac20SDavid S. Miller 			if (__ipv6_only_sock(sk))
1289db8dac20SDavid S. Miller 				return -ENETUNREACH;
12901b784140SYing Xue 			return udp_sendmsg(sk, msg, len);
1291db8dac20SDavid S. Miller 		}
1292db8dac20SDavid S. Miller 	}
1293db8dac20SDavid S. Miller 
1294db8dac20SDavid S. Miller 	if (up->pending == AF_INET)
12951b784140SYing Xue 		return udp_sendmsg(sk, msg, len);
1296db8dac20SDavid S. Miller 
1297db8dac20SDavid S. Miller 	/* Rough check on arithmetic overflow,
1298db8dac20SDavid S. Miller 	   better check is made in ip6_append_data().
1299db8dac20SDavid S. Miller 	   */
1300db8dac20SDavid S. Miller 	if (len > INT_MAX - sizeof(struct udphdr))
1301db8dac20SDavid S. Miller 		return -EMSGSIZE;
1302db8dac20SDavid S. Miller 
130303485f2aSVlad Yasevich 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1304db8dac20SDavid S. Miller 	if (up->pending) {
1305db8dac20SDavid S. Miller 		/*
1306db8dac20SDavid S. Miller 		 * There are pending frames.
1307db8dac20SDavid S. Miller 		 * The socket lock must be held while it's corked.
1308db8dac20SDavid S. Miller 		 */
1309db8dac20SDavid S. Miller 		lock_sock(sk);
1310db8dac20SDavid S. Miller 		if (likely(up->pending)) {
1311db8dac20SDavid S. Miller 			if (unlikely(up->pending != AF_INET6)) {
1312db8dac20SDavid S. Miller 				release_sock(sk);
1313db8dac20SDavid S. Miller 				return -EAFNOSUPPORT;
1314db8dac20SDavid S. Miller 			}
1315db8dac20SDavid S. Miller 			dst = NULL;
1316db8dac20SDavid S. Miller 			goto do_append_data;
1317db8dac20SDavid S. Miller 		}
1318db8dac20SDavid S. Miller 		release_sock(sk);
1319db8dac20SDavid S. Miller 	}
1320db8dac20SDavid S. Miller 	ulen += sizeof(struct udphdr);
1321db8dac20SDavid S. Miller 
13224c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
1323db8dac20SDavid S. Miller 
1324db8dac20SDavid S. Miller 	if (sin6) {
1325db8dac20SDavid S. Miller 		if (sin6->sin6_port == 0)
1326db8dac20SDavid S. Miller 			return -EINVAL;
1327db8dac20SDavid S. Miller 
13281958b856SDavid S. Miller 		fl6.fl6_dport = sin6->sin6_port;
1329db8dac20SDavid S. Miller 		daddr = &sin6->sin6_addr;
1330db8dac20SDavid S. Miller 
1331db8dac20SDavid S. Miller 		if (np->sndflow) {
13324c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
13334c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
13344c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
133563159f29SIan Morris 				if (!flowlabel)
1336db8dac20SDavid S. Miller 					return -EINVAL;
1337db8dac20SDavid S. Miller 			}
1338db8dac20SDavid S. Miller 		}
1339db8dac20SDavid S. Miller 
1340db8dac20SDavid S. Miller 		/*
1341db8dac20SDavid S. Miller 		 * Otherwise it will be difficult to maintain
1342db8dac20SDavid S. Miller 		 * sk->sk_dst_cache.
1343db8dac20SDavid S. Miller 		 */
1344db8dac20SDavid S. Miller 		if (sk->sk_state == TCP_ESTABLISHED &&
1345efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1346efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
1347db8dac20SDavid S. Miller 
1348db8dac20SDavid S. Miller 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1349db8dac20SDavid S. Miller 		    sin6->sin6_scope_id &&
1350842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
13514c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
1352db8dac20SDavid S. Miller 	} else {
1353db8dac20SDavid S. Miller 		if (sk->sk_state != TCP_ESTABLISHED)
1354db8dac20SDavid S. Miller 			return -EDESTADDRREQ;
1355db8dac20SDavid S. Miller 
13561958b856SDavid S. Miller 		fl6.fl6_dport = inet->inet_dport;
1357efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
13584c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
13599f542f61SAlexey Kodanev 		connected = true;
1360db8dac20SDavid S. Miller 	}
1361db8dac20SDavid S. Miller 
13624c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
13634c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1364db8dac20SDavid S. Miller 
13654c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif)
13664c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
13679f690db7SYang Hongyang 
13684c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
1369e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sk->sk_uid;
137051953d5bSBrian Haley 
1371db8dac20SDavid S. Miller 	if (msg->msg_controllen) {
1372db8dac20SDavid S. Miller 		opt = &opt_space;
1373db8dac20SDavid S. Miller 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1374db8dac20SDavid S. Miller 		opt->tot_len = sizeof(*opt);
137526879da5SWei Wang 		ipc6.opt = opt;
1376db8dac20SDavid S. Miller 
13772e8de857SWillem de Bruijn 		err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
13782e8de857SWillem de Bruijn 		if (err > 0)
13792e8de857SWillem de Bruijn 			err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6,
13805fdaa88dSWillem de Bruijn 						    &ipc6);
1381db8dac20SDavid S. Miller 		if (err < 0) {
1382db8dac20SDavid S. Miller 			fl6_sock_release(flowlabel);
1383db8dac20SDavid S. Miller 			return err;
1384db8dac20SDavid S. Miller 		}
13854c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
13864c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
138763159f29SIan Morris 			if (!flowlabel)
1388db8dac20SDavid S. Miller 				return -EINVAL;
1389db8dac20SDavid S. Miller 		}
1390db8dac20SDavid S. Miller 		if (!(opt->opt_nflen|opt->opt_flen))
1391db8dac20SDavid S. Miller 			opt = NULL;
13929f542f61SAlexey Kodanev 		connected = false;
1393db8dac20SDavid S. Miller 	}
139445f6fad8SEric Dumazet 	if (!opt) {
139545f6fad8SEric Dumazet 		opt = txopt_get(np);
139645f6fad8SEric Dumazet 		opt_to_free = opt;
139745f6fad8SEric Dumazet 	}
1398db8dac20SDavid S. Miller 	if (flowlabel)
1399db8dac20SDavid S. Miller 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1400db8dac20SDavid S. Miller 	opt = ipv6_fixup_options(&opt_space, opt);
140126879da5SWei Wang 	ipc6.opt = opt;
1402db8dac20SDavid S. Miller 
14034c9483b2SDavid S. Miller 	fl6.flowi6_proto = sk->sk_protocol;
14044e3fd7a0SAlexey Dobriyan 	fl6.daddr = *daddr;
14054c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
14064e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
14071958b856SDavid S. Miller 	fl6.fl6_sport = inet->inet_sport;
1408db8dac20SDavid S. Miller 
14091cedee13SAndrey Ignatov 	if (cgroup_bpf_enabled && !connected) {
14101cedee13SAndrey Ignatov 		err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
14111cedee13SAndrey Ignatov 					   (struct sockaddr *)sin6, &fl6.saddr);
14121cedee13SAndrey Ignatov 		if (err)
14131cedee13SAndrey Ignatov 			goto out_no_dst;
14141cedee13SAndrey Ignatov 		if (sin6) {
14151cedee13SAndrey Ignatov 			if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
14161cedee13SAndrey Ignatov 				/* BPF program rewrote IPv6-only by IPv4-mapped
14171cedee13SAndrey Ignatov 				 * IPv6. It's currently unsupported.
14181cedee13SAndrey Ignatov 				 */
14191cedee13SAndrey Ignatov 				err = -ENOTSUPP;
14201cedee13SAndrey Ignatov 				goto out_no_dst;
14211cedee13SAndrey Ignatov 			}
14221cedee13SAndrey Ignatov 			if (sin6->sin6_port == 0) {
14231cedee13SAndrey Ignatov 				/* BPF program set invalid port. Reject it. */
14241cedee13SAndrey Ignatov 				err = -EINVAL;
14251cedee13SAndrey Ignatov 				goto out_no_dst;
14261cedee13SAndrey Ignatov 			}
14271cedee13SAndrey Ignatov 			fl6.fl6_dport = sin6->sin6_port;
14281cedee13SAndrey Ignatov 			fl6.daddr = sin6->sin6_addr;
14291cedee13SAndrey Ignatov 		}
14301cedee13SAndrey Ignatov 	}
14311cedee13SAndrey Ignatov 
1432e8e36984SAndrey Ignatov 	if (ipv6_addr_any(&fl6.daddr))
1433e8e36984SAndrey Ignatov 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1434e8e36984SAndrey Ignatov 
14354c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
143620c59de2SArnaud Ebalard 	if (final_p)
14379f542f61SAlexey Kodanev 		connected = false;
1438db8dac20SDavid S. Miller 
14394c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
14404c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
14419f542f61SAlexey Kodanev 		connected = false;
1442c4062dfcSErich E. Hoover 	} else if (!fl6.flowi6_oif)
1443c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
1444db8dac20SDavid S. Miller 
14454c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1446db8dac20SDavid S. Miller 
144738b7097bSHannes Frederic Sowa 	if (ipc6.tclass < 0)
144838b7097bSHannes Frederic Sowa 		ipc6.tclass = np->tclass;
144938b7097bSHannes Frederic Sowa 
145038b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
145138b7097bSHannes Frederic Sowa 
14524f858c56SAlexey Kodanev 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected);
145368d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
145468d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
145568d0c6d3SDavid S. Miller 		dst = NULL;
1456db8dac20SDavid S. Miller 		goto out;
1457db8dac20SDavid S. Miller 	}
1458db8dac20SDavid S. Miller 
145926879da5SWei Wang 	if (ipc6.hlimit < 0)
146026879da5SWei Wang 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1461db8dac20SDavid S. Miller 
1462db8dac20SDavid S. Miller 	if (msg->msg_flags&MSG_CONFIRM)
1463db8dac20SDavid S. Miller 		goto do_confirm;
1464db8dac20SDavid S. Miller back_from_confirm:
1465db8dac20SDavid S. Miller 
146603485f2aSVlad Yasevich 	/* Lockless fast path for the non-corking case */
146703485f2aSVlad Yasevich 	if (!corkreq) {
14681cd7884dSWillem de Bruijn 		struct inet_cork_full cork;
146903485f2aSVlad Yasevich 		struct sk_buff *skb;
147003485f2aSVlad Yasevich 
147103485f2aSVlad Yasevich 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
147226879da5SWei Wang 				   sizeof(struct udphdr), &ipc6,
147303485f2aSVlad Yasevich 				   &fl6, (struct rt6_info *)dst,
14745fdaa88dSWillem de Bruijn 				   msg->msg_flags, &cork);
147503485f2aSVlad Yasevich 		err = PTR_ERR(skb);
147603485f2aSVlad Yasevich 		if (!IS_ERR_OR_NULL(skb))
1477bec1f6f6SWillem de Bruijn 			err = udp_v6_send_skb(skb, &fl6, &cork.base);
14784f858c56SAlexey Kodanev 		goto out;
147903485f2aSVlad Yasevich 	}
148003485f2aSVlad Yasevich 
1481db8dac20SDavid S. Miller 	lock_sock(sk);
1482db8dac20SDavid S. Miller 	if (unlikely(up->pending)) {
1483db8dac20SDavid S. Miller 		/* The socket is already corked while preparing it. */
1484db8dac20SDavid S. Miller 		/* ... which is an evident application bug. --ANK */
1485db8dac20SDavid S. Miller 		release_sock(sk);
1486db8dac20SDavid S. Miller 
1487ba7a46f1SJoe Perches 		net_dbg_ratelimited("udp cork app bug 2\n");
1488db8dac20SDavid S. Miller 		err = -EINVAL;
1489db8dac20SDavid S. Miller 		goto out;
1490db8dac20SDavid S. Miller 	}
1491db8dac20SDavid S. Miller 
1492db8dac20SDavid S. Miller 	up->pending = AF_INET6;
1493db8dac20SDavid S. Miller 
1494db8dac20SDavid S. Miller do_append_data:
149526879da5SWei Wang 	if (ipc6.dontfrag < 0)
149626879da5SWei Wang 		ipc6.dontfrag = np->dontfrag;
1497db8dac20SDavid S. Miller 	up->len += ulen;
149826879da5SWei Wang 	err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
149926879da5SWei Wang 			      &ipc6, &fl6, (struct rt6_info *)dst,
15005fdaa88dSWillem de Bruijn 			      corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1501db8dac20SDavid S. Miller 	if (err)
1502db8dac20SDavid S. Miller 		udp_v6_flush_pending_frames(sk);
1503db8dac20SDavid S. Miller 	else if (!corkreq)
1504db8dac20SDavid S. Miller 		err = udp_v6_push_pending_frames(sk);
1505db8dac20SDavid S. Miller 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1506db8dac20SDavid S. Miller 		up->pending = 0;
1507db8dac20SDavid S. Miller 
150803485f2aSVlad Yasevich 	if (err > 0)
150903485f2aSVlad Yasevich 		err = np->recverr ? net_xmit_errno(err) : 0;
151003485f2aSVlad Yasevich 	release_sock(sk);
151103485f2aSVlad Yasevich 
1512db8dac20SDavid S. Miller out:
1513a3c96089SYOSHIFUJI Hideaki 	dst_release(dst);
15141cedee13SAndrey Ignatov out_no_dst:
1515db8dac20SDavid S. Miller 	fl6_sock_release(flowlabel);
151645f6fad8SEric Dumazet 	txopt_put(opt_to_free);
1517db8dac20SDavid S. Miller 	if (!err)
1518db8dac20SDavid S. Miller 		return len;
1519db8dac20SDavid S. Miller 	/*
1520db8dac20SDavid S. Miller 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1521db8dac20SDavid S. Miller 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1522db8dac20SDavid S. Miller 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1523db8dac20SDavid S. Miller 	 * things).  We could add another new stat but at least for now that
1524db8dac20SDavid S. Miller 	 * seems like overkill.
1525db8dac20SDavid S. Miller 	 */
1526db8dac20SDavid S. Miller 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
15276aef70a8SEric Dumazet 		UDP6_INC_STATS(sock_net(sk),
1528235b9f7aSPavel Emelyanov 			       UDP_MIB_SNDBUFERRORS, is_udplite);
1529db8dac20SDavid S. Miller 	}
1530db8dac20SDavid S. Miller 	return err;
1531db8dac20SDavid S. Miller 
1532db8dac20SDavid S. Miller do_confirm:
15330dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
15340dec879fSJulian Anastasov 		dst_confirm_neigh(dst, &fl6.daddr);
1535db8dac20SDavid S. Miller 	if (!(msg->msg_flags&MSG_PROBE) || len)
1536db8dac20SDavid S. Miller 		goto back_from_confirm;
1537db8dac20SDavid S. Miller 	err = 0;
1538db8dac20SDavid S. Miller 	goto out;
1539db8dac20SDavid S. Miller }
1540db8dac20SDavid S. Miller 
15417d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1542db8dac20SDavid S. Miller {
154344046a59STom Parkin 	struct udp_sock *up = udp_sk(sk);
1544db8dac20SDavid S. Miller 	lock_sock(sk);
1545db8dac20SDavid S. Miller 	udp_v6_flush_pending_frames(sk);
1546db8dac20SDavid S. Miller 	release_sock(sk);
1547db8dac20SDavid S. Miller 
154860fb9567SPaolo Abeni 	if (static_branch_unlikely(&udpv6_encap_needed_key)) {
154960fb9567SPaolo Abeni 		if (up->encap_type) {
155044046a59STom Parkin 			void (*encap_destroy)(struct sock *sk);
15516aa7de05SMark Rutland 			encap_destroy = READ_ONCE(up->encap_destroy);
155244046a59STom Parkin 			if (encap_destroy)
155344046a59STom Parkin 				encap_destroy(sk);
155444046a59STom Parkin 		}
155560fb9567SPaolo Abeni 		if (up->encap_enabled)
15569c480601SPaolo Abeni 			static_branch_dec(&udpv6_encap_needed_key);
155760fb9567SPaolo Abeni 	}
155844046a59STom Parkin 
1559db8dac20SDavid S. Miller 	inet6_destroy_sock(sk);
1560db8dac20SDavid S. Miller }
1561db8dac20SDavid S. Miller 
1562db8dac20SDavid S. Miller /*
1563db8dac20SDavid S. Miller  *	Socket option code for UDP
1564db8dac20SDavid S. Miller  */
1565db8dac20SDavid S. Miller int udpv6_setsockopt(struct sock *sk, int level, int optname,
1566b7058842SDavid S. Miller 		     char __user *optval, unsigned int optlen)
1567db8dac20SDavid S. Miller {
1568db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1569db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1570db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1571db8dac20SDavid S. Miller 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1572db8dac20SDavid S. Miller }
1573db8dac20SDavid S. Miller 
1574db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1575db8dac20SDavid S. Miller int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1576b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
1577db8dac20SDavid S. Miller {
1578db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1579db8dac20SDavid S. Miller 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1580db8dac20SDavid S. Miller 					  udp_v6_push_pending_frames);
1581db8dac20SDavid S. Miller 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1582db8dac20SDavid S. Miller }
1583db8dac20SDavid S. Miller #endif
1584db8dac20SDavid S. Miller 
1585db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1586db8dac20SDavid S. Miller 		     char __user *optval, int __user *optlen)
1587db8dac20SDavid S. Miller {
1588db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1589db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1590db8dac20SDavid S. Miller 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1591db8dac20SDavid S. Miller }
1592db8dac20SDavid S. Miller 
1593db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1594db8dac20SDavid S. Miller int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1595db8dac20SDavid S. Miller 			    char __user *optval, int __user *optlen)
1596db8dac20SDavid S. Miller {
1597db8dac20SDavid S. Miller 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1598db8dac20SDavid S. Miller 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1599db8dac20SDavid S. Miller 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1600db8dac20SDavid S. Miller }
1601db8dac20SDavid S. Miller #endif
1602db8dac20SDavid S. Miller 
1603a8e3bb34SDavid Ahern /* thinking of making this const? Don't.
1604a8e3bb34SDavid Ahern  * early_demux can change based on sysctl.
1605a8e3bb34SDavid Ahern  */
1606dddb64bcSsubashab@codeaurora.org static struct inet6_protocol udpv6_protocol = {
16075425077dSsubashab@codeaurora.org 	.early_demux	=	udp_v6_early_demux,
1608dddb64bcSsubashab@codeaurora.org 	.early_demux_handler =  udp_v6_early_demux,
1609db8dac20SDavid S. Miller 	.handler	=	udpv6_rcv,
1610db8dac20SDavid S. Miller 	.err_handler	=	udpv6_err,
1611db8dac20SDavid S. Miller 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1612db8dac20SDavid S. Miller };
1613db8dac20SDavid S. Miller 
1614db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1615db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
1616db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1617db8dac20SDavid S. Miller {
161817ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
161917ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
162017ef66afSLorenzo Colitti 	} else {
162117ef66afSLorenzo Colitti 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
162217ef66afSLorenzo Colitti 		struct inet_sock *inet = inet_sk(v);
162317ef66afSLorenzo Colitti 		__u16 srcp = ntohs(inet->inet_sport);
162417ef66afSLorenzo Colitti 		__u16 destp = ntohs(inet->inet_dport);
16256c206b20SPaolo Abeni 		__ip6_dgram_sock_seq_show(seq, v, srcp, destp,
16266c206b20SPaolo Abeni 					  udp_rqueue_get(v), bucket);
162717ef66afSLorenzo Colitti 	}
1628db8dac20SDavid S. Miller 	return 0;
1629db8dac20SDavid S. Miller }
1630db8dac20SDavid S. Miller 
1631c3506372SChristoph Hellwig const struct seq_operations udp6_seq_ops = {
1632a3d2599bSChristoph Hellwig 	.start		= udp_seq_start,
1633a3d2599bSChristoph Hellwig 	.next		= udp_seq_next,
1634a3d2599bSChristoph Hellwig 	.stop		= udp_seq_stop,
1635a3d2599bSChristoph Hellwig 	.show		= udp6_seq_show,
1636a3d2599bSChristoph Hellwig };
1637c3506372SChristoph Hellwig EXPORT_SYMBOL(udp6_seq_ops);
163873cb88ecSArjan van de Ven 
1639db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1640db8dac20SDavid S. Miller 	.family		= AF_INET6,
1641645ca708SEric Dumazet 	.udp_table	= &udp_table,
1642db8dac20SDavid S. Miller };
1643db8dac20SDavid S. Miller 
16442c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1645db8dac20SDavid S. Miller {
1646c3506372SChristoph Hellwig 	if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1647c3506372SChristoph Hellwig 			sizeof(struct udp_iter_state), &udp6_seq_afinfo))
1648a3d2599bSChristoph Hellwig 		return -ENOMEM;
1649a3d2599bSChristoph Hellwig 	return 0;
1650db8dac20SDavid S. Miller }
1651db8dac20SDavid S. Miller 
1652ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1653ec120da6SIan Morris {
1654a3d2599bSChristoph Hellwig 	remove_proc_entry("udp6", net->proc_net);
1655db8dac20SDavid S. Miller }
1656db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1657db8dac20SDavid S. Miller 
1658db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1659db8dac20SDavid S. Miller 
1660db8dac20SDavid S. Miller struct proto udpv6_prot = {
1661db8dac20SDavid S. Miller 	.name			= "UDPv6",
1662db8dac20SDavid S. Miller 	.owner			= THIS_MODULE,
1663db8dac20SDavid S. Miller 	.close			= udp_lib_close,
1664d74bad4eSAndrey Ignatov 	.pre_connect		= udpv6_pre_connect,
1665db8dac20SDavid S. Miller 	.connect		= ip6_datagram_connect,
1666db8dac20SDavid S. Miller 	.disconnect		= udp_disconnect,
1667db8dac20SDavid S. Miller 	.ioctl			= udp_ioctl,
1668850cbaddSPaolo Abeni 	.init			= udp_init_sock,
1669db8dac20SDavid S. Miller 	.destroy		= udpv6_destroy_sock,
1670db8dac20SDavid S. Miller 	.setsockopt		= udpv6_setsockopt,
1671db8dac20SDavid S. Miller 	.getsockopt		= udpv6_getsockopt,
1672db8dac20SDavid S. Miller 	.sendmsg		= udpv6_sendmsg,
1673db8dac20SDavid S. Miller 	.recvmsg		= udpv6_recvmsg,
1674e646b657SMartin KaFai Lau 	.release_cb		= ip6_datagram_release_cb,
1675db8dac20SDavid S. Miller 	.hash			= udp_lib_hash,
1676db8dac20SDavid S. Miller 	.unhash			= udp_lib_unhash,
1677719f8358SEric Dumazet 	.rehash			= udp_v6_rehash,
1678db8dac20SDavid S. Miller 	.get_port		= udp_v6_get_port,
1679db8dac20SDavid S. Miller 	.memory_allocated	= &udp_memory_allocated,
1680db8dac20SDavid S. Miller 	.sysctl_mem		= sysctl_udp_mem,
16811e802951STonghao Zhang 	.sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
16821e802951STonghao Zhang 	.sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1683db8dac20SDavid S. Miller 	.obj_size		= sizeof(struct udp6_sock),
1684645ca708SEric Dumazet 	.h.udp_table		= &udp_table,
1685db8dac20SDavid S. Miller #ifdef CONFIG_COMPAT
1686db8dac20SDavid S. Miller 	.compat_setsockopt	= compat_udpv6_setsockopt,
1687db8dac20SDavid S. Miller 	.compat_getsockopt	= compat_udpv6_getsockopt,
1688db8dac20SDavid S. Miller #endif
16895d77dca8SDavid Ahern 	.diag_destroy		= udp_abort,
1690db8dac20SDavid S. Miller };
1691db8dac20SDavid S. Miller 
1692db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1693db8dac20SDavid S. Miller 	.type =      SOCK_DGRAM,
1694db8dac20SDavid S. Miller 	.protocol =  IPPROTO_UDP,
1695db8dac20SDavid S. Miller 	.prot =      &udpv6_prot,
1696db8dac20SDavid S. Miller 	.ops =       &inet6_dgram_ops,
1697db8dac20SDavid S. Miller 	.flags =     INET_PROTOSW_PERMANENT,
1698db8dac20SDavid S. Miller };
1699db8dac20SDavid S. Miller 
1700db8dac20SDavid S. Miller int __init udpv6_init(void)
1701db8dac20SDavid S. Miller {
1702db8dac20SDavid S. Miller 	int ret;
1703db8dac20SDavid S. Miller 
17043336288aSVlad Yasevich 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
17053336288aSVlad Yasevich 	if (ret)
1706c6b641a4SVlad Yasevich 		goto out;
17073336288aSVlad Yasevich 
1708db8dac20SDavid S. Miller 	ret = inet6_register_protosw(&udpv6_protosw);
1709db8dac20SDavid S. Miller 	if (ret)
1710db8dac20SDavid S. Miller 		goto out_udpv6_protocol;
1711db8dac20SDavid S. Miller out:
1712db8dac20SDavid S. Miller 	return ret;
1713db8dac20SDavid S. Miller 
1714db8dac20SDavid S. Miller out_udpv6_protocol:
1715db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1716db8dac20SDavid S. Miller 	goto out;
1717db8dac20SDavid S. Miller }
1718db8dac20SDavid S. Miller 
1719db8dac20SDavid S. Miller void udpv6_exit(void)
1720db8dac20SDavid S. Miller {
1721db8dac20SDavid S. Miller 	inet6_unregister_protosw(&udpv6_protosw);
1722db8dac20SDavid S. Miller 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1723db8dac20SDavid S. Miller }
1724