xref: /openbmc/linux/net/ipv6/udp.c (revision a31edb20)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	UDP over IPv6
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *
9  *	Based on linux/ipv4/udp.c
10  *
11  *	Fixes:
12  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
13  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
14  *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
15  *					a single port at the same time.
16  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
17  *      YOSHIFUJI Hideaki @USAGI:	convert /proc/net/udp6 to seq_file.
18  */
19 
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/in6.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/ipv6.h>
29 #include <linux/icmpv6.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/skbuff.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
35 #include <linux/indirect_call_wrapper.h>
36 
37 #include <net/addrconf.h>
38 #include <net/ndisc.h>
39 #include <net/protocol.h>
40 #include <net/transp_v6.h>
41 #include <net/ip6_route.h>
42 #include <net/raw.h>
43 #include <net/tcp_states.h>
44 #include <net/ip6_checksum.h>
45 #include <net/ip6_tunnel.h>
46 #include <net/xfrm.h>
47 #include <net/inet_hashtables.h>
48 #include <net/inet6_hashtables.h>
49 #include <net/busy_poll.h>
50 #include <net/sock_reuseport.h>
51 
52 #include <linux/proc_fs.h>
53 #include <linux/seq_file.h>
54 #include <trace/events/skb.h>
55 #include "udp_impl.h"
56 
57 static u32 udp6_ehashfn(const struct net *net,
58 			const struct in6_addr *laddr,
59 			const u16 lport,
60 			const struct in6_addr *faddr,
61 			const __be16 fport)
62 {
63 	static u32 udp6_ehash_secret __read_mostly;
64 	static u32 udp_ipv6_hash_secret __read_mostly;
65 
66 	u32 lhash, fhash;
67 
68 	net_get_random_once(&udp6_ehash_secret,
69 			    sizeof(udp6_ehash_secret));
70 	net_get_random_once(&udp_ipv6_hash_secret,
71 			    sizeof(udp_ipv6_hash_secret));
72 
73 	lhash = (__force u32)laddr->s6_addr32[3];
74 	fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
75 
76 	return __inet6_ehashfn(lhash, lport, fhash, fport,
77 			       udp_ipv6_hash_secret + net_hash_mix(net));
78 }
79 
80 int udp_v6_get_port(struct sock *sk, unsigned short snum)
81 {
82 	unsigned int hash2_nulladdr =
83 		ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
84 	unsigned int hash2_partial =
85 		ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
86 
87 	/* precompute partial secondary hash */
88 	udp_sk(sk)->udp_portaddr_hash = hash2_partial;
89 	return udp_lib_get_port(sk, snum, hash2_nulladdr);
90 }
91 
92 void udp_v6_rehash(struct sock *sk)
93 {
94 	u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
95 					  &sk->sk_v6_rcv_saddr,
96 					  inet_sk(sk)->inet_num);
97 
98 	udp_lib_rehash(sk, new_hash);
99 }
100 
101 static int compute_score(struct sock *sk, struct net *net,
102 			 const struct in6_addr *saddr, __be16 sport,
103 			 const struct in6_addr *daddr, unsigned short hnum,
104 			 int dif, int sdif)
105 {
106 	int score;
107 	struct inet_sock *inet;
108 	bool dev_match;
109 
110 	if (!net_eq(sock_net(sk), net) ||
111 	    udp_sk(sk)->udp_port_hash != hnum ||
112 	    sk->sk_family != PF_INET6)
113 		return -1;
114 
115 	if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
116 		return -1;
117 
118 	score = 0;
119 	inet = inet_sk(sk);
120 
121 	if (inet->inet_dport) {
122 		if (inet->inet_dport != sport)
123 			return -1;
124 		score++;
125 	}
126 
127 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
128 		if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
129 			return -1;
130 		score++;
131 	}
132 
133 	dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
134 	if (!dev_match)
135 		return -1;
136 	score++;
137 
138 	if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
139 		score++;
140 
141 	return score;
142 }
143 
144 static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
145 				     struct sk_buff *skb,
146 				     const struct in6_addr *saddr,
147 				     __be16 sport,
148 				     const struct in6_addr *daddr,
149 				     unsigned int hnum)
150 {
151 	struct sock *reuse_sk = NULL;
152 	u32 hash;
153 
154 	if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
155 		hash = udp6_ehashfn(net, daddr, hnum, saddr, sport);
156 		reuse_sk = reuseport_select_sock(sk, hash, skb,
157 						 sizeof(struct udphdr));
158 		/* Fall back to scoring if group has connections */
159 		if (reuseport_has_conns(sk, false))
160 			return NULL;
161 	}
162 	return reuse_sk;
163 }
164 
165 /* called with rcu_read_lock() */
166 static struct sock *udp6_lib_lookup2(struct net *net,
167 		const struct in6_addr *saddr, __be16 sport,
168 		const struct in6_addr *daddr, unsigned int hnum,
169 		int dif, int sdif, struct udp_hslot *hslot2,
170 		struct sk_buff *skb)
171 {
172 	struct sock *sk, *result;
173 	int score, badness;
174 
175 	result = NULL;
176 	badness = -1;
177 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
178 		score = compute_score(sk, net, saddr, sport,
179 				      daddr, hnum, dif, sdif);
180 		if (score > badness) {
181 			result = lookup_reuseport(net, sk, skb,
182 						  saddr, sport, daddr, hnum);
183 			/* Fall back to scoring if group has connections */
184 			if (result && !reuseport_has_conns(sk, false))
185 				return result;
186 
187 			result = result ? : sk;
188 			badness = score;
189 		}
190 	}
191 	return result;
192 }
193 
194 static inline struct sock *udp6_lookup_run_bpf(struct net *net,
195 					       struct udp_table *udptable,
196 					       struct sk_buff *skb,
197 					       const struct in6_addr *saddr,
198 					       __be16 sport,
199 					       const struct in6_addr *daddr,
200 					       u16 hnum)
201 {
202 	struct sock *sk, *reuse_sk;
203 	bool no_reuseport;
204 
205 	if (udptable != &udp_table)
206 		return NULL; /* only UDP is supported */
207 
208 	no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP,
209 					    saddr, sport, daddr, hnum, &sk);
210 	if (no_reuseport || IS_ERR_OR_NULL(sk))
211 		return sk;
212 
213 	reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
214 	if (reuse_sk && !reuseport_has_conns(sk, false))
215 		sk = reuse_sk;
216 	return sk;
217 }
218 
219 /* rcu_read_lock() must be held */
220 struct sock *__udp6_lib_lookup(struct net *net,
221 			       const struct in6_addr *saddr, __be16 sport,
222 			       const struct in6_addr *daddr, __be16 dport,
223 			       int dif, int sdif, struct udp_table *udptable,
224 			       struct sk_buff *skb)
225 {
226 	unsigned short hnum = ntohs(dport);
227 	unsigned int hash2, slot2;
228 	struct udp_hslot *hslot2;
229 	struct sock *result, *sk;
230 
231 	hash2 = ipv6_portaddr_hash(net, daddr, hnum);
232 	slot2 = hash2 & udptable->mask;
233 	hslot2 = &udptable->hash2[slot2];
234 
235 	/* Lookup connected or non-wildcard sockets */
236 	result = udp6_lib_lookup2(net, saddr, sport,
237 				  daddr, hnum, dif, sdif,
238 				  hslot2, skb);
239 	if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
240 		goto done;
241 
242 	/* Lookup redirect from BPF */
243 	if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
244 		sk = udp6_lookup_run_bpf(net, udptable, skb,
245 					 saddr, sport, daddr, hnum);
246 		if (sk) {
247 			result = sk;
248 			goto done;
249 		}
250 	}
251 
252 	/* Got non-wildcard socket or error on first lookup */
253 	if (result)
254 		goto done;
255 
256 	/* Lookup wildcard sockets */
257 	hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
258 	slot2 = hash2 & udptable->mask;
259 	hslot2 = &udptable->hash2[slot2];
260 
261 	result = udp6_lib_lookup2(net, saddr, sport,
262 				  &in6addr_any, hnum, dif, sdif,
263 				  hslot2, skb);
264 done:
265 	if (IS_ERR(result))
266 		return NULL;
267 	return result;
268 }
269 EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
270 
271 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
272 					  __be16 sport, __be16 dport,
273 					  struct udp_table *udptable)
274 {
275 	const struct ipv6hdr *iph = ipv6_hdr(skb);
276 
277 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
278 				 &iph->daddr, dport, inet6_iif(skb),
279 				 inet6_sdif(skb), udptable, skb);
280 }
281 
282 struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
283 				 __be16 sport, __be16 dport)
284 {
285 	const struct ipv6hdr *iph = ipv6_hdr(skb);
286 
287 	return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
288 				 &iph->daddr, dport, inet6_iif(skb),
289 				 inet6_sdif(skb), &udp_table, NULL);
290 }
291 EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
292 
293 /* Must be called under rcu_read_lock().
294  * Does increment socket refcount.
295  */
296 #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
297 struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
298 			     const struct in6_addr *daddr, __be16 dport, int dif)
299 {
300 	struct sock *sk;
301 
302 	sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
303 				dif, 0, &udp_table, NULL);
304 	if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
305 		sk = NULL;
306 	return sk;
307 }
308 EXPORT_SYMBOL_GPL(udp6_lib_lookup);
309 #endif
310 
311 /* do not use the scratch area len for jumbogram: their length execeeds the
312  * scratch area space; note that the IP6CB flags is still in the first
313  * cacheline, so checking for jumbograms is cheap
314  */
315 static int udp6_skb_len(struct sk_buff *skb)
316 {
317 	return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
318 }
319 
320 /*
321  *	This should be easy, if there is something there we
322  *	return it, otherwise we block.
323  */
324 
325 int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
326 		  int noblock, int flags, int *addr_len)
327 {
328 	struct ipv6_pinfo *np = inet6_sk(sk);
329 	struct inet_sock *inet = inet_sk(sk);
330 	struct sk_buff *skb;
331 	unsigned int ulen, copied;
332 	int off, err, peeking = flags & MSG_PEEK;
333 	int is_udplite = IS_UDPLITE(sk);
334 	struct udp_mib __percpu *mib;
335 	bool checksum_valid = false;
336 	int is_udp4;
337 
338 	if (flags & MSG_ERRQUEUE)
339 		return ipv6_recv_error(sk, msg, len, addr_len);
340 
341 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
342 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
343 
344 try_again:
345 	off = sk_peek_offset(sk, flags);
346 	skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
347 	if (!skb)
348 		return err;
349 
350 	ulen = udp6_skb_len(skb);
351 	copied = len;
352 	if (copied > ulen - off)
353 		copied = ulen - off;
354 	else if (copied < ulen)
355 		msg->msg_flags |= MSG_TRUNC;
356 
357 	is_udp4 = (skb->protocol == htons(ETH_P_IP));
358 	mib = __UDPX_MIB(sk, is_udp4);
359 
360 	/*
361 	 * If checksum is needed at all, try to do it while copying the
362 	 * data.  If the data is truncated, or if we only want a partial
363 	 * coverage checksum (UDP-Lite), do it before the copy.
364 	 */
365 
366 	if (copied < ulen || peeking ||
367 	    (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
368 		checksum_valid = udp_skb_csum_unnecessary(skb) ||
369 				!__udp_lib_checksum_complete(skb);
370 		if (!checksum_valid)
371 			goto csum_copy_err;
372 	}
373 
374 	if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
375 		if (udp_skb_is_linear(skb))
376 			err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
377 		else
378 			err = skb_copy_datagram_msg(skb, off, msg, copied);
379 	} else {
380 		err = skb_copy_and_csum_datagram_msg(skb, off, msg);
381 		if (err == -EINVAL)
382 			goto csum_copy_err;
383 	}
384 	if (unlikely(err)) {
385 		if (!peeking) {
386 			atomic_inc(&sk->sk_drops);
387 			SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
388 		}
389 		kfree_skb(skb);
390 		return err;
391 	}
392 	if (!peeking)
393 		SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
394 
395 	sock_recv_ts_and_drops(msg, sk, skb);
396 
397 	/* Copy the address. */
398 	if (msg->msg_name) {
399 		DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
400 		sin6->sin6_family = AF_INET6;
401 		sin6->sin6_port = udp_hdr(skb)->source;
402 		sin6->sin6_flowinfo = 0;
403 
404 		if (is_udp4) {
405 			ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
406 					       &sin6->sin6_addr);
407 			sin6->sin6_scope_id = 0;
408 		} else {
409 			sin6->sin6_addr = ipv6_hdr(skb)->saddr;
410 			sin6->sin6_scope_id =
411 				ipv6_iface_scope_id(&sin6->sin6_addr,
412 						    inet6_iif(skb));
413 		}
414 		*addr_len = sizeof(*sin6);
415 
416 		if (cgroup_bpf_enabled)
417 			BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
418 						(struct sockaddr *)sin6);
419 	}
420 
421 	if (udp_sk(sk)->gro_enabled)
422 		udp_cmsg_recv(msg, sk, skb);
423 
424 	if (np->rxopt.all)
425 		ip6_datagram_recv_common_ctl(sk, msg, skb);
426 
427 	if (is_udp4) {
428 		if (inet->cmsg_flags)
429 			ip_cmsg_recv_offset(msg, sk, skb,
430 					    sizeof(struct udphdr), off);
431 	} else {
432 		if (np->rxopt.all)
433 			ip6_datagram_recv_specific_ctl(sk, msg, skb);
434 	}
435 
436 	err = copied;
437 	if (flags & MSG_TRUNC)
438 		err = ulen;
439 
440 	skb_consume_udp(sk, skb, peeking ? -err : err);
441 	return err;
442 
443 csum_copy_err:
444 	if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
445 				 udp_skb_destructor)) {
446 		SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
447 		SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
448 	}
449 	kfree_skb(skb);
450 
451 	/* starting over for a new packet, but check if we need to yield */
452 	cond_resched();
453 	msg->msg_flags &= ~MSG_TRUNC;
454 	goto try_again;
455 }
456 
457 DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
458 void udpv6_encap_enable(void)
459 {
460 	static_branch_inc(&udpv6_encap_needed_key);
461 }
462 EXPORT_SYMBOL(udpv6_encap_enable);
463 
464 /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
465  * through error handlers in encapsulations looking for a match.
466  */
467 static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
468 				      struct inet6_skb_parm *opt,
469 				      u8 type, u8 code, int offset, __be32 info)
470 {
471 	int i;
472 
473 	for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
474 		int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
475 			       u8 type, u8 code, int offset, __be32 info);
476 		const struct ip6_tnl_encap_ops *encap;
477 
478 		encap = rcu_dereference(ip6tun_encaps[i]);
479 		if (!encap)
480 			continue;
481 		handler = encap->err_handler;
482 		if (handler && !handler(skb, opt, type, code, offset, info))
483 			return 0;
484 	}
485 
486 	return -ENOENT;
487 }
488 
489 /* Try to match ICMP errors to UDP tunnels by looking up a socket without
490  * reversing source and destination port: this will match tunnels that force the
491  * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
492  * lwtunnels might actually break this assumption by being configured with
493  * different destination ports on endpoints, in this case we won't be able to
494  * trace ICMP messages back to them.
495  *
496  * If this doesn't match any socket, probe tunnels with arbitrary destination
497  * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
498  * we've sent packets to won't necessarily match the local destination port.
499  *
500  * Then ask the tunnel implementation to match the error against a valid
501  * association.
502  *
503  * Return an error if we can't find a match, the socket if we need further
504  * processing, zero otherwise.
505  */
506 static struct sock *__udp6_lib_err_encap(struct net *net,
507 					 const struct ipv6hdr *hdr, int offset,
508 					 struct udphdr *uh,
509 					 struct udp_table *udptable,
510 					 struct sk_buff *skb,
511 					 struct inet6_skb_parm *opt,
512 					 u8 type, u8 code, __be32 info)
513 {
514 	int network_offset, transport_offset;
515 	struct sock *sk;
516 
517 	network_offset = skb_network_offset(skb);
518 	transport_offset = skb_transport_offset(skb);
519 
520 	/* Network header needs to point to the outer IPv6 header inside ICMP */
521 	skb_reset_network_header(skb);
522 
523 	/* Transport header needs to point to the UDP header */
524 	skb_set_transport_header(skb, offset);
525 
526 	sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
527 			       &hdr->saddr, uh->dest,
528 			       inet6_iif(skb), 0, udptable, skb);
529 	if (sk) {
530 		int (*lookup)(struct sock *sk, struct sk_buff *skb);
531 		struct udp_sock *up = udp_sk(sk);
532 
533 		lookup = READ_ONCE(up->encap_err_lookup);
534 		if (!lookup || lookup(sk, skb))
535 			sk = NULL;
536 	}
537 
538 	if (!sk) {
539 		sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
540 							offset, info));
541 	}
542 
543 	skb_set_transport_header(skb, transport_offset);
544 	skb_set_network_header(skb, network_offset);
545 
546 	return sk;
547 }
548 
549 int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
550 		   u8 type, u8 code, int offset, __be32 info,
551 		   struct udp_table *udptable)
552 {
553 	struct ipv6_pinfo *np;
554 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
555 	const struct in6_addr *saddr = &hdr->saddr;
556 	const struct in6_addr *daddr = &hdr->daddr;
557 	struct udphdr *uh = (struct udphdr *)(skb->data+offset);
558 	bool tunnel = false;
559 	struct sock *sk;
560 	int harderr;
561 	int err;
562 	struct net *net = dev_net(skb->dev);
563 
564 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
565 			       inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
566 	if (!sk) {
567 		/* No socket for error: try tunnels before discarding */
568 		sk = ERR_PTR(-ENOENT);
569 		if (static_branch_unlikely(&udpv6_encap_needed_key)) {
570 			sk = __udp6_lib_err_encap(net, hdr, offset, uh,
571 						  udptable, skb,
572 						  opt, type, code, info);
573 			if (!sk)
574 				return 0;
575 		}
576 
577 		if (IS_ERR(sk)) {
578 			__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
579 					  ICMP6_MIB_INERRORS);
580 			return PTR_ERR(sk);
581 		}
582 
583 		tunnel = true;
584 	}
585 
586 	harderr = icmpv6_err_convert(type, code, &err);
587 	np = inet6_sk(sk);
588 
589 	if (type == ICMPV6_PKT_TOOBIG) {
590 		if (!ip6_sk_accept_pmtu(sk))
591 			goto out;
592 		ip6_sk_update_pmtu(skb, sk, info);
593 		if (np->pmtudisc != IPV6_PMTUDISC_DONT)
594 			harderr = 1;
595 	}
596 	if (type == NDISC_REDIRECT) {
597 		if (tunnel) {
598 			ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
599 				     sk->sk_mark, sk->sk_uid);
600 		} else {
601 			ip6_sk_redirect(skb, sk);
602 		}
603 		goto out;
604 	}
605 
606 	/* Tunnels don't have an application socket: don't pass errors back */
607 	if (tunnel)
608 		goto out;
609 
610 	if (!np->recverr) {
611 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
612 			goto out;
613 	} else {
614 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
615 	}
616 
617 	sk->sk_err = err;
618 	sk->sk_error_report(sk);
619 out:
620 	return 0;
621 }
622 
623 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
624 {
625 	int rc;
626 
627 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
628 		sock_rps_save_rxhash(sk, skb);
629 		sk_mark_napi_id(sk, skb);
630 		sk_incoming_cpu_update(sk);
631 	} else {
632 		sk_mark_napi_id_once(sk, skb);
633 	}
634 
635 	rc = __udp_enqueue_schedule_skb(sk, skb);
636 	if (rc < 0) {
637 		int is_udplite = IS_UDPLITE(sk);
638 
639 		/* Note that an ENOMEM error is charged twice */
640 		if (rc == -ENOMEM)
641 			UDP6_INC_STATS(sock_net(sk),
642 					 UDP_MIB_RCVBUFERRORS, is_udplite);
643 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
644 		kfree_skb(skb);
645 		return -1;
646 	}
647 
648 	return 0;
649 }
650 
651 static __inline__ int udpv6_err(struct sk_buff *skb,
652 				struct inet6_skb_parm *opt, u8 type,
653 				u8 code, int offset, __be32 info)
654 {
655 	return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
656 }
657 
658 static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
659 {
660 	struct udp_sock *up = udp_sk(sk);
661 	int is_udplite = IS_UDPLITE(sk);
662 
663 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
664 		goto drop;
665 
666 	if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
667 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
668 
669 		/*
670 		 * This is an encapsulation socket so pass the skb to
671 		 * the socket's udp_encap_rcv() hook. Otherwise, just
672 		 * fall through and pass this up the UDP socket.
673 		 * up->encap_rcv() returns the following value:
674 		 * =0 if skb was successfully passed to the encap
675 		 *    handler or was discarded by it.
676 		 * >0 if skb should be passed on to UDP.
677 		 * <0 if skb should be resubmitted as proto -N
678 		 */
679 
680 		/* if we're overly short, let UDP handle it */
681 		encap_rcv = READ_ONCE(up->encap_rcv);
682 		if (encap_rcv) {
683 			int ret;
684 
685 			/* Verify checksum before giving to encap */
686 			if (udp_lib_checksum_complete(skb))
687 				goto csum_error;
688 
689 			ret = encap_rcv(sk, skb);
690 			if (ret <= 0) {
691 				__UDP_INC_STATS(sock_net(sk),
692 						UDP_MIB_INDATAGRAMS,
693 						is_udplite);
694 				return -ret;
695 			}
696 		}
697 
698 		/* FALLTHROUGH -- it's a UDP Packet */
699 	}
700 
701 	/*
702 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
703 	 */
704 	if ((up->pcflag & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
705 
706 		if (up->pcrlen == 0) {          /* full coverage was set  */
707 			net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
708 					    UDP_SKB_CB(skb)->cscov, skb->len);
709 			goto drop;
710 		}
711 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
712 			net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
713 					    UDP_SKB_CB(skb)->cscov, up->pcrlen);
714 			goto drop;
715 		}
716 	}
717 
718 	prefetch(&sk->sk_rmem_alloc);
719 	if (rcu_access_pointer(sk->sk_filter) &&
720 	    udp_lib_checksum_complete(skb))
721 		goto csum_error;
722 
723 	if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
724 		goto drop;
725 
726 	udp_csum_pull_header(skb);
727 
728 	skb_dst_drop(skb);
729 
730 	return __udpv6_queue_rcv_skb(sk, skb);
731 
732 csum_error:
733 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
734 drop:
735 	__UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
736 	atomic_inc(&sk->sk_drops);
737 	kfree_skb(skb);
738 	return -1;
739 }
740 
741 static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
742 {
743 	struct sk_buff *next, *segs;
744 	int ret;
745 
746 	if (likely(!udp_unexpected_gso(sk, skb)))
747 		return udpv6_queue_rcv_one_skb(sk, skb);
748 
749 	__skb_push(skb, -skb_mac_offset(skb));
750 	segs = udp_rcv_segment(sk, skb, false);
751 	skb_list_walk_safe(segs, skb, next) {
752 		__skb_pull(skb, skb_transport_offset(skb));
753 
754 		ret = udpv6_queue_rcv_one_skb(sk, skb);
755 		if (ret > 0)
756 			ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
757 						 true);
758 	}
759 	return 0;
760 }
761 
762 static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
763 				   __be16 loc_port, const struct in6_addr *loc_addr,
764 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
765 				   int dif, int sdif, unsigned short hnum)
766 {
767 	struct inet_sock *inet = inet_sk(sk);
768 
769 	if (!net_eq(sock_net(sk), net))
770 		return false;
771 
772 	if (udp_sk(sk)->udp_port_hash != hnum ||
773 	    sk->sk_family != PF_INET6 ||
774 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
775 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
776 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
777 	    !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) ||
778 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
779 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
780 		return false;
781 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
782 		return false;
783 	return true;
784 }
785 
786 static void udp6_csum_zero_error(struct sk_buff *skb)
787 {
788 	/* RFC 2460 section 8.1 says that we SHOULD log
789 	 * this error. Well, it is reasonable.
790 	 */
791 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
792 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
793 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
794 }
795 
796 /*
797  * Note: called only from the BH handler context,
798  * so we don't need to lock the hashes.
799  */
800 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
801 		const struct in6_addr *saddr, const struct in6_addr *daddr,
802 		struct udp_table *udptable, int proto)
803 {
804 	struct sock *sk, *first = NULL;
805 	const struct udphdr *uh = udp_hdr(skb);
806 	unsigned short hnum = ntohs(uh->dest);
807 	struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
808 	unsigned int offset = offsetof(typeof(*sk), sk_node);
809 	unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
810 	int dif = inet6_iif(skb);
811 	int sdif = inet6_sdif(skb);
812 	struct hlist_node *node;
813 	struct sk_buff *nskb;
814 
815 	if (use_hash2) {
816 		hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
817 			    udptable->mask;
818 		hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
819 start_lookup:
820 		hslot = &udptable->hash2[hash2];
821 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
822 	}
823 
824 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
825 		if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
826 					    uh->source, saddr, dif, sdif,
827 					    hnum))
828 			continue;
829 		/* If zero checksum and no_check is not on for
830 		 * the socket then skip it.
831 		 */
832 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
833 			continue;
834 		if (!first) {
835 			first = sk;
836 			continue;
837 		}
838 		nskb = skb_clone(skb, GFP_ATOMIC);
839 		if (unlikely(!nskb)) {
840 			atomic_inc(&sk->sk_drops);
841 			__UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
842 					 IS_UDPLITE(sk));
843 			__UDP6_INC_STATS(net, UDP_MIB_INERRORS,
844 					 IS_UDPLITE(sk));
845 			continue;
846 		}
847 
848 		if (udpv6_queue_rcv_skb(sk, nskb) > 0)
849 			consume_skb(nskb);
850 	}
851 
852 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
853 	if (use_hash2 && hash2 != hash2_any) {
854 		hash2 = hash2_any;
855 		goto start_lookup;
856 	}
857 
858 	if (first) {
859 		if (udpv6_queue_rcv_skb(first, skb) > 0)
860 			consume_skb(skb);
861 	} else {
862 		kfree_skb(skb);
863 		__UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
864 				 proto == IPPROTO_UDPLITE);
865 	}
866 	return 0;
867 }
868 
869 static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
870 {
871 	if (udp_sk_rx_dst_set(sk, dst)) {
872 		const struct rt6_info *rt = (const struct rt6_info *)dst;
873 
874 		inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt);
875 	}
876 }
877 
878 /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
879  * return code conversion for ip layer consumption
880  */
881 static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
882 				struct udphdr *uh)
883 {
884 	int ret;
885 
886 	if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
887 		skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
888 
889 	ret = udpv6_queue_rcv_skb(sk, skb);
890 
891 	/* a return value > 0 means to resubmit the input */
892 	if (ret > 0)
893 		return ret;
894 	return 0;
895 }
896 
897 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
898 		   int proto)
899 {
900 	const struct in6_addr *saddr, *daddr;
901 	struct net *net = dev_net(skb->dev);
902 	struct udphdr *uh;
903 	struct sock *sk;
904 	bool refcounted;
905 	u32 ulen = 0;
906 
907 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
908 		goto discard;
909 
910 	saddr = &ipv6_hdr(skb)->saddr;
911 	daddr = &ipv6_hdr(skb)->daddr;
912 	uh = udp_hdr(skb);
913 
914 	ulen = ntohs(uh->len);
915 	if (ulen > skb->len)
916 		goto short_packet;
917 
918 	if (proto == IPPROTO_UDP) {
919 		/* UDP validates ulen. */
920 
921 		/* Check for jumbo payload */
922 		if (ulen == 0)
923 			ulen = skb->len;
924 
925 		if (ulen < sizeof(*uh))
926 			goto short_packet;
927 
928 		if (ulen < skb->len) {
929 			if (pskb_trim_rcsum(skb, ulen))
930 				goto short_packet;
931 			saddr = &ipv6_hdr(skb)->saddr;
932 			daddr = &ipv6_hdr(skb)->daddr;
933 			uh = udp_hdr(skb);
934 		}
935 	}
936 
937 	if (udp6_csum_init(skb, uh, proto))
938 		goto csum_error;
939 
940 	/* Check if the socket is already available, e.g. due to early demux */
941 	sk = skb_steal_sock(skb, &refcounted);
942 	if (sk) {
943 		struct dst_entry *dst = skb_dst(skb);
944 		int ret;
945 
946 		if (unlikely(sk->sk_rx_dst != dst))
947 			udp6_sk_rx_dst_set(sk, dst);
948 
949 		if (!uh->check && !udp_sk(sk)->no_check6_rx) {
950 			if (refcounted)
951 				sock_put(sk);
952 			goto report_csum_error;
953 		}
954 
955 		ret = udp6_unicast_rcv_skb(sk, skb, uh);
956 		if (refcounted)
957 			sock_put(sk);
958 		return ret;
959 	}
960 
961 	/*
962 	 *	Multicast receive code
963 	 */
964 	if (ipv6_addr_is_multicast(daddr))
965 		return __udp6_lib_mcast_deliver(net, skb,
966 				saddr, daddr, udptable, proto);
967 
968 	/* Unicast */
969 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
970 	if (sk) {
971 		if (!uh->check && !udp_sk(sk)->no_check6_rx)
972 			goto report_csum_error;
973 		return udp6_unicast_rcv_skb(sk, skb, uh);
974 	}
975 
976 	if (!uh->check)
977 		goto report_csum_error;
978 
979 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
980 		goto discard;
981 
982 	if (udp_lib_checksum_complete(skb))
983 		goto csum_error;
984 
985 	__UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
986 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
987 
988 	kfree_skb(skb);
989 	return 0;
990 
991 short_packet:
992 	net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
993 			    proto == IPPROTO_UDPLITE ? "-Lite" : "",
994 			    saddr, ntohs(uh->source),
995 			    ulen, skb->len,
996 			    daddr, ntohs(uh->dest));
997 	goto discard;
998 
999 report_csum_error:
1000 	udp6_csum_zero_error(skb);
1001 csum_error:
1002 	__UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1003 discard:
1004 	__UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1005 	kfree_skb(skb);
1006 	return 0;
1007 }
1008 
1009 
1010 static struct sock *__udp6_lib_demux_lookup(struct net *net,
1011 			__be16 loc_port, const struct in6_addr *loc_addr,
1012 			__be16 rmt_port, const struct in6_addr *rmt_addr,
1013 			int dif, int sdif)
1014 {
1015 	unsigned short hnum = ntohs(loc_port);
1016 	unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
1017 	unsigned int slot2 = hash2 & udp_table.mask;
1018 	struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
1019 	const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
1020 	struct sock *sk;
1021 
1022 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
1023 		if (sk->sk_state == TCP_ESTABLISHED &&
1024 		    INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
1025 			return sk;
1026 		/* Only check first socket in chain */
1027 		break;
1028 	}
1029 	return NULL;
1030 }
1031 
1032 INDIRECT_CALLABLE_SCOPE void udp_v6_early_demux(struct sk_buff *skb)
1033 {
1034 	struct net *net = dev_net(skb->dev);
1035 	const struct udphdr *uh;
1036 	struct sock *sk;
1037 	struct dst_entry *dst;
1038 	int dif = skb->dev->ifindex;
1039 	int sdif = inet6_sdif(skb);
1040 
1041 	if (!pskb_may_pull(skb, skb_transport_offset(skb) +
1042 	    sizeof(struct udphdr)))
1043 		return;
1044 
1045 	uh = udp_hdr(skb);
1046 
1047 	if (skb->pkt_type == PACKET_HOST)
1048 		sk = __udp6_lib_demux_lookup(net, uh->dest,
1049 					     &ipv6_hdr(skb)->daddr,
1050 					     uh->source, &ipv6_hdr(skb)->saddr,
1051 					     dif, sdif);
1052 	else
1053 		return;
1054 
1055 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
1056 		return;
1057 
1058 	skb->sk = sk;
1059 	skb->destructor = sock_efree;
1060 	dst = READ_ONCE(sk->sk_rx_dst);
1061 
1062 	if (dst)
1063 		dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
1064 	if (dst) {
1065 		/* set noref for now.
1066 		 * any place which wants to hold dst has to call
1067 		 * dst_hold_safe()
1068 		 */
1069 		skb_dst_set_noref(skb, dst);
1070 	}
1071 }
1072 
1073 INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
1074 {
1075 	return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
1076 }
1077 
1078 /*
1079  * Throw away all pending data and cancel the corking. Socket is locked.
1080  */
1081 static void udp_v6_flush_pending_frames(struct sock *sk)
1082 {
1083 	struct udp_sock *up = udp_sk(sk);
1084 
1085 	if (up->pending == AF_INET)
1086 		udp_flush_pending_frames(sk);
1087 	else if (up->pending) {
1088 		up->len = 0;
1089 		up->pending = 0;
1090 		ip6_flush_pending_frames(sk);
1091 	}
1092 }
1093 
1094 static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
1095 			     int addr_len)
1096 {
1097 	if (addr_len < offsetofend(struct sockaddr, sa_family))
1098 		return -EINVAL;
1099 	/* The following checks are replicated from __ip6_datagram_connect()
1100 	 * and intended to prevent BPF program called below from accessing
1101 	 * bytes that are out of the bound specified by user in addr_len.
1102 	 */
1103 	if (uaddr->sa_family == AF_INET) {
1104 		if (__ipv6_only_sock(sk))
1105 			return -EAFNOSUPPORT;
1106 		return udp_pre_connect(sk, uaddr, addr_len);
1107 	}
1108 
1109 	if (addr_len < SIN6_LEN_RFC2133)
1110 		return -EINVAL;
1111 
1112 	return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr);
1113 }
1114 
1115 /**
1116  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
1117  *	@sk:	socket we are sending on
1118  *	@skb:	sk_buff containing the filled-in UDP header
1119  *		(checksum field must be zeroed out)
1120  *	@saddr: source address
1121  *	@daddr: destination address
1122  *	@len:	length of packet
1123  */
1124 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1125 				 const struct in6_addr *saddr,
1126 				 const struct in6_addr *daddr, int len)
1127 {
1128 	unsigned int offset;
1129 	struct udphdr *uh = udp_hdr(skb);
1130 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1131 	__wsum csum = 0;
1132 
1133 	if (!frags) {
1134 		/* Only one fragment on the socket.  */
1135 		skb->csum_start = skb_transport_header(skb) - skb->head;
1136 		skb->csum_offset = offsetof(struct udphdr, check);
1137 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1138 	} else {
1139 		/*
1140 		 * HW-checksum won't work as there are two or more
1141 		 * fragments on the socket so that all csums of sk_buffs
1142 		 * should be together
1143 		 */
1144 		offset = skb_transport_offset(skb);
1145 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1146 		csum = skb->csum;
1147 
1148 		skb->ip_summed = CHECKSUM_NONE;
1149 
1150 		do {
1151 			csum = csum_add(csum, frags->csum);
1152 		} while ((frags = frags->next));
1153 
1154 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1155 					    csum);
1156 		if (uh->check == 0)
1157 			uh->check = CSUM_MANGLED_0;
1158 	}
1159 }
1160 
1161 /*
1162  *	Sending
1163  */
1164 
1165 static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1166 			   struct inet_cork *cork)
1167 {
1168 	struct sock *sk = skb->sk;
1169 	struct udphdr *uh;
1170 	int err = 0;
1171 	int is_udplite = IS_UDPLITE(sk);
1172 	__wsum csum = 0;
1173 	int offset = skb_transport_offset(skb);
1174 	int len = skb->len - offset;
1175 	int datalen = len - sizeof(*uh);
1176 
1177 	/*
1178 	 * Create a UDP header
1179 	 */
1180 	uh = udp_hdr(skb);
1181 	uh->source = fl6->fl6_sport;
1182 	uh->dest = fl6->fl6_dport;
1183 	uh->len = htons(len);
1184 	uh->check = 0;
1185 
1186 	if (cork->gso_size) {
1187 		const int hlen = skb_network_header_len(skb) +
1188 				 sizeof(struct udphdr);
1189 
1190 		if (hlen + cork->gso_size > cork->fragsize) {
1191 			kfree_skb(skb);
1192 			return -EINVAL;
1193 		}
1194 		if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS) {
1195 			kfree_skb(skb);
1196 			return -EINVAL;
1197 		}
1198 		if (udp_sk(sk)->no_check6_tx) {
1199 			kfree_skb(skb);
1200 			return -EINVAL;
1201 		}
1202 		if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
1203 		    dst_xfrm(skb_dst(skb))) {
1204 			kfree_skb(skb);
1205 			return -EIO;
1206 		}
1207 
1208 		if (datalen > cork->gso_size) {
1209 			skb_shinfo(skb)->gso_size = cork->gso_size;
1210 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
1211 			skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
1212 								 cork->gso_size);
1213 		}
1214 		goto csum_partial;
1215 	}
1216 
1217 	if (is_udplite)
1218 		csum = udplite_csum(skb);
1219 	else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
1220 		skb->ip_summed = CHECKSUM_NONE;
1221 		goto send;
1222 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1223 csum_partial:
1224 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1225 		goto send;
1226 	} else
1227 		csum = udp_csum(skb);
1228 
1229 	/* add protocol-dependent pseudo-header */
1230 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1231 				    len, fl6->flowi6_proto, csum);
1232 	if (uh->check == 0)
1233 		uh->check = CSUM_MANGLED_0;
1234 
1235 send:
1236 	err = ip6_send_skb(skb);
1237 	if (err) {
1238 		if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
1239 			UDP6_INC_STATS(sock_net(sk),
1240 				       UDP_MIB_SNDBUFERRORS, is_udplite);
1241 			err = 0;
1242 		}
1243 	} else {
1244 		UDP6_INC_STATS(sock_net(sk),
1245 			       UDP_MIB_OUTDATAGRAMS, is_udplite);
1246 	}
1247 	return err;
1248 }
1249 
1250 static int udp_v6_push_pending_frames(struct sock *sk)
1251 {
1252 	struct sk_buff *skb;
1253 	struct udp_sock  *up = udp_sk(sk);
1254 	struct flowi6 fl6;
1255 	int err = 0;
1256 
1257 	if (up->pending == AF_INET)
1258 		return udp_push_pending_frames(sk);
1259 
1260 	/* ip6_finish_skb will release the cork, so make a copy of
1261 	 * fl6 here.
1262 	 */
1263 	fl6 = inet_sk(sk)->cork.fl.u.ip6;
1264 
1265 	skb = ip6_finish_skb(sk);
1266 	if (!skb)
1267 		goto out;
1268 
1269 	err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base);
1270 
1271 out:
1272 	up->len = 0;
1273 	up->pending = 0;
1274 	return err;
1275 }
1276 
1277 int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1278 {
1279 	struct ipv6_txoptions opt_space;
1280 	struct udp_sock *up = udp_sk(sk);
1281 	struct inet_sock *inet = inet_sk(sk);
1282 	struct ipv6_pinfo *np = inet6_sk(sk);
1283 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1284 	struct in6_addr *daddr, *final_p, final;
1285 	struct ipv6_txoptions *opt = NULL;
1286 	struct ipv6_txoptions *opt_to_free = NULL;
1287 	struct ip6_flowlabel *flowlabel = NULL;
1288 	struct flowi6 fl6;
1289 	struct dst_entry *dst;
1290 	struct ipcm6_cookie ipc6;
1291 	int addr_len = msg->msg_namelen;
1292 	bool connected = false;
1293 	int ulen = len;
1294 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1295 	int err;
1296 	int is_udplite = IS_UDPLITE(sk);
1297 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1298 
1299 	ipcm6_init(&ipc6);
1300 	ipc6.gso_size = up->gso_size;
1301 	ipc6.sockc.tsflags = sk->sk_tsflags;
1302 	ipc6.sockc.mark = sk->sk_mark;
1303 
1304 	/* destination address check */
1305 	if (sin6) {
1306 		if (addr_len < offsetof(struct sockaddr, sa_data))
1307 			return -EINVAL;
1308 
1309 		switch (sin6->sin6_family) {
1310 		case AF_INET6:
1311 			if (addr_len < SIN6_LEN_RFC2133)
1312 				return -EINVAL;
1313 			daddr = &sin6->sin6_addr;
1314 			if (ipv6_addr_any(daddr) &&
1315 			    ipv6_addr_v4mapped(&np->saddr))
1316 				ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1317 						       daddr);
1318 			break;
1319 		case AF_INET:
1320 			goto do_udp_sendmsg;
1321 		case AF_UNSPEC:
1322 			msg->msg_name = sin6 = NULL;
1323 			msg->msg_namelen = addr_len = 0;
1324 			daddr = NULL;
1325 			break;
1326 		default:
1327 			return -EINVAL;
1328 		}
1329 	} else if (!up->pending) {
1330 		if (sk->sk_state != TCP_ESTABLISHED)
1331 			return -EDESTADDRREQ;
1332 		daddr = &sk->sk_v6_daddr;
1333 	} else
1334 		daddr = NULL;
1335 
1336 	if (daddr) {
1337 		if (ipv6_addr_v4mapped(daddr)) {
1338 			struct sockaddr_in sin;
1339 			sin.sin_family = AF_INET;
1340 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1341 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1342 			msg->msg_name = &sin;
1343 			msg->msg_namelen = sizeof(sin);
1344 do_udp_sendmsg:
1345 			if (__ipv6_only_sock(sk))
1346 				return -ENETUNREACH;
1347 			return udp_sendmsg(sk, msg, len);
1348 		}
1349 	}
1350 
1351 	if (up->pending == AF_INET)
1352 		return udp_sendmsg(sk, msg, len);
1353 
1354 	/* Rough check on arithmetic overflow,
1355 	   better check is made in ip6_append_data().
1356 	   */
1357 	if (len > INT_MAX - sizeof(struct udphdr))
1358 		return -EMSGSIZE;
1359 
1360 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1361 	if (up->pending) {
1362 		/*
1363 		 * There are pending frames.
1364 		 * The socket lock must be held while it's corked.
1365 		 */
1366 		lock_sock(sk);
1367 		if (likely(up->pending)) {
1368 			if (unlikely(up->pending != AF_INET6)) {
1369 				release_sock(sk);
1370 				return -EAFNOSUPPORT;
1371 			}
1372 			dst = NULL;
1373 			goto do_append_data;
1374 		}
1375 		release_sock(sk);
1376 	}
1377 	ulen += sizeof(struct udphdr);
1378 
1379 	memset(&fl6, 0, sizeof(fl6));
1380 
1381 	if (sin6) {
1382 		if (sin6->sin6_port == 0)
1383 			return -EINVAL;
1384 
1385 		fl6.fl6_dport = sin6->sin6_port;
1386 		daddr = &sin6->sin6_addr;
1387 
1388 		if (np->sndflow) {
1389 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1390 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1391 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1392 				if (IS_ERR(flowlabel))
1393 					return -EINVAL;
1394 			}
1395 		}
1396 
1397 		/*
1398 		 * Otherwise it will be difficult to maintain
1399 		 * sk->sk_dst_cache.
1400 		 */
1401 		if (sk->sk_state == TCP_ESTABLISHED &&
1402 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1403 			daddr = &sk->sk_v6_daddr;
1404 
1405 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1406 		    sin6->sin6_scope_id &&
1407 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
1408 			fl6.flowi6_oif = sin6->sin6_scope_id;
1409 	} else {
1410 		if (sk->sk_state != TCP_ESTABLISHED)
1411 			return -EDESTADDRREQ;
1412 
1413 		fl6.fl6_dport = inet->inet_dport;
1414 		daddr = &sk->sk_v6_daddr;
1415 		fl6.flowlabel = np->flow_label;
1416 		connected = true;
1417 	}
1418 
1419 	if (!fl6.flowi6_oif)
1420 		fl6.flowi6_oif = sk->sk_bound_dev_if;
1421 
1422 	if (!fl6.flowi6_oif)
1423 		fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1424 
1425 	fl6.flowi6_mark = ipc6.sockc.mark;
1426 	fl6.flowi6_uid = sk->sk_uid;
1427 
1428 	if (msg->msg_controllen) {
1429 		opt = &opt_space;
1430 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1431 		opt->tot_len = sizeof(*opt);
1432 		ipc6.opt = opt;
1433 
1434 		err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
1435 		if (err > 0)
1436 			err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6,
1437 						    &ipc6);
1438 		if (err < 0) {
1439 			fl6_sock_release(flowlabel);
1440 			return err;
1441 		}
1442 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1443 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1444 			if (IS_ERR(flowlabel))
1445 				return -EINVAL;
1446 		}
1447 		if (!(opt->opt_nflen|opt->opt_flen))
1448 			opt = NULL;
1449 		connected = false;
1450 	}
1451 	if (!opt) {
1452 		opt = txopt_get(np);
1453 		opt_to_free = opt;
1454 	}
1455 	if (flowlabel)
1456 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1457 	opt = ipv6_fixup_options(&opt_space, opt);
1458 	ipc6.opt = opt;
1459 
1460 	fl6.flowi6_proto = sk->sk_protocol;
1461 	fl6.daddr = *daddr;
1462 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
1463 		fl6.saddr = np->saddr;
1464 	fl6.fl6_sport = inet->inet_sport;
1465 
1466 	if (cgroup_bpf_enabled && !connected) {
1467 		err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
1468 					   (struct sockaddr *)sin6, &fl6.saddr);
1469 		if (err)
1470 			goto out_no_dst;
1471 		if (sin6) {
1472 			if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
1473 				/* BPF program rewrote IPv6-only by IPv4-mapped
1474 				 * IPv6. It's currently unsupported.
1475 				 */
1476 				err = -ENOTSUPP;
1477 				goto out_no_dst;
1478 			}
1479 			if (sin6->sin6_port == 0) {
1480 				/* BPF program set invalid port. Reject it. */
1481 				err = -EINVAL;
1482 				goto out_no_dst;
1483 			}
1484 			fl6.fl6_dport = sin6->sin6_port;
1485 			fl6.daddr = sin6->sin6_addr;
1486 		}
1487 	}
1488 
1489 	if (ipv6_addr_any(&fl6.daddr))
1490 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1491 
1492 	final_p = fl6_update_dst(&fl6, opt, &final);
1493 	if (final_p)
1494 		connected = false;
1495 
1496 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1497 		fl6.flowi6_oif = np->mcast_oif;
1498 		connected = false;
1499 	} else if (!fl6.flowi6_oif)
1500 		fl6.flowi6_oif = np->ucast_oif;
1501 
1502 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1503 
1504 	if (ipc6.tclass < 0)
1505 		ipc6.tclass = np->tclass;
1506 
1507 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
1508 
1509 	dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected);
1510 	if (IS_ERR(dst)) {
1511 		err = PTR_ERR(dst);
1512 		dst = NULL;
1513 		goto out;
1514 	}
1515 
1516 	if (ipc6.hlimit < 0)
1517 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1518 
1519 	if (msg->msg_flags&MSG_CONFIRM)
1520 		goto do_confirm;
1521 back_from_confirm:
1522 
1523 	/* Lockless fast path for the non-corking case */
1524 	if (!corkreq) {
1525 		struct inet_cork_full cork;
1526 		struct sk_buff *skb;
1527 
1528 		skb = ip6_make_skb(sk, getfrag, msg, ulen,
1529 				   sizeof(struct udphdr), &ipc6,
1530 				   &fl6, (struct rt6_info *)dst,
1531 				   msg->msg_flags, &cork);
1532 		err = PTR_ERR(skb);
1533 		if (!IS_ERR_OR_NULL(skb))
1534 			err = udp_v6_send_skb(skb, &fl6, &cork.base);
1535 		goto out;
1536 	}
1537 
1538 	lock_sock(sk);
1539 	if (unlikely(up->pending)) {
1540 		/* The socket is already corked while preparing it. */
1541 		/* ... which is an evident application bug. --ANK */
1542 		release_sock(sk);
1543 
1544 		net_dbg_ratelimited("udp cork app bug 2\n");
1545 		err = -EINVAL;
1546 		goto out;
1547 	}
1548 
1549 	up->pending = AF_INET6;
1550 
1551 do_append_data:
1552 	if (ipc6.dontfrag < 0)
1553 		ipc6.dontfrag = np->dontfrag;
1554 	up->len += ulen;
1555 	err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
1556 			      &ipc6, &fl6, (struct rt6_info *)dst,
1557 			      corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1558 	if (err)
1559 		udp_v6_flush_pending_frames(sk);
1560 	else if (!corkreq)
1561 		err = udp_v6_push_pending_frames(sk);
1562 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1563 		up->pending = 0;
1564 
1565 	if (err > 0)
1566 		err = np->recverr ? net_xmit_errno(err) : 0;
1567 	release_sock(sk);
1568 
1569 out:
1570 	dst_release(dst);
1571 out_no_dst:
1572 	fl6_sock_release(flowlabel);
1573 	txopt_put(opt_to_free);
1574 	if (!err)
1575 		return len;
1576 	/*
1577 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1578 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1579 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1580 	 * things).  We could add another new stat but at least for now that
1581 	 * seems like overkill.
1582 	 */
1583 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1584 		UDP6_INC_STATS(sock_net(sk),
1585 			       UDP_MIB_SNDBUFERRORS, is_udplite);
1586 	}
1587 	return err;
1588 
1589 do_confirm:
1590 	if (msg->msg_flags & MSG_PROBE)
1591 		dst_confirm_neigh(dst, &fl6.daddr);
1592 	if (!(msg->msg_flags&MSG_PROBE) || len)
1593 		goto back_from_confirm;
1594 	err = 0;
1595 	goto out;
1596 }
1597 
1598 void udpv6_destroy_sock(struct sock *sk)
1599 {
1600 	struct udp_sock *up = udp_sk(sk);
1601 	lock_sock(sk);
1602 	udp_v6_flush_pending_frames(sk);
1603 	release_sock(sk);
1604 
1605 	if (static_branch_unlikely(&udpv6_encap_needed_key)) {
1606 		if (up->encap_type) {
1607 			void (*encap_destroy)(struct sock *sk);
1608 			encap_destroy = READ_ONCE(up->encap_destroy);
1609 			if (encap_destroy)
1610 				encap_destroy(sk);
1611 		}
1612 		if (up->encap_enabled)
1613 			static_branch_dec(&udpv6_encap_needed_key);
1614 	}
1615 
1616 	inet6_destroy_sock(sk);
1617 }
1618 
1619 /*
1620  *	Socket option code for UDP
1621  */
1622 int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1623 		     unsigned int optlen)
1624 {
1625 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1626 		return udp_lib_setsockopt(sk, level, optname,
1627 					  optval, optlen,
1628 					  udp_v6_push_pending_frames);
1629 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1630 }
1631 
1632 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1633 		     char __user *optval, int __user *optlen)
1634 {
1635 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1636 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1637 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
1638 }
1639 
1640 /* thinking of making this const? Don't.
1641  * early_demux can change based on sysctl.
1642  */
1643 static struct inet6_protocol udpv6_protocol = {
1644 	.early_demux	=	udp_v6_early_demux,
1645 	.early_demux_handler =  udp_v6_early_demux,
1646 	.handler	=	udpv6_rcv,
1647 	.err_handler	=	udpv6_err,
1648 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1649 };
1650 
1651 /* ------------------------------------------------------------------------ */
1652 #ifdef CONFIG_PROC_FS
1653 int udp6_seq_show(struct seq_file *seq, void *v)
1654 {
1655 	if (v == SEQ_START_TOKEN) {
1656 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1657 	} else {
1658 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1659 		struct inet_sock *inet = inet_sk(v);
1660 		__u16 srcp = ntohs(inet->inet_sport);
1661 		__u16 destp = ntohs(inet->inet_dport);
1662 		__ip6_dgram_sock_seq_show(seq, v, srcp, destp,
1663 					  udp_rqueue_get(v), bucket);
1664 	}
1665 	return 0;
1666 }
1667 
1668 const struct seq_operations udp6_seq_ops = {
1669 	.start		= udp_seq_start,
1670 	.next		= udp_seq_next,
1671 	.stop		= udp_seq_stop,
1672 	.show		= udp6_seq_show,
1673 };
1674 EXPORT_SYMBOL(udp6_seq_ops);
1675 
1676 static struct udp_seq_afinfo udp6_seq_afinfo = {
1677 	.family		= AF_INET6,
1678 	.udp_table	= &udp_table,
1679 };
1680 
1681 int __net_init udp6_proc_init(struct net *net)
1682 {
1683 	if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1684 			sizeof(struct udp_iter_state), &udp6_seq_afinfo))
1685 		return -ENOMEM;
1686 	return 0;
1687 }
1688 
1689 void udp6_proc_exit(struct net *net)
1690 {
1691 	remove_proc_entry("udp6", net->proc_net);
1692 }
1693 #endif /* CONFIG_PROC_FS */
1694 
1695 /* ------------------------------------------------------------------------ */
1696 
1697 struct proto udpv6_prot = {
1698 	.name			= "UDPv6",
1699 	.owner			= THIS_MODULE,
1700 	.close			= udp_lib_close,
1701 	.pre_connect		= udpv6_pre_connect,
1702 	.connect		= ip6_datagram_connect,
1703 	.disconnect		= udp_disconnect,
1704 	.ioctl			= udp_ioctl,
1705 	.init			= udp_init_sock,
1706 	.destroy		= udpv6_destroy_sock,
1707 	.setsockopt		= udpv6_setsockopt,
1708 	.getsockopt		= udpv6_getsockopt,
1709 	.sendmsg		= udpv6_sendmsg,
1710 	.recvmsg		= udpv6_recvmsg,
1711 	.release_cb		= ip6_datagram_release_cb,
1712 	.hash			= udp_lib_hash,
1713 	.unhash			= udp_lib_unhash,
1714 	.rehash			= udp_v6_rehash,
1715 	.get_port		= udp_v6_get_port,
1716 	.memory_allocated	= &udp_memory_allocated,
1717 	.sysctl_mem		= sysctl_udp_mem,
1718 	.sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
1719 	.sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1720 	.obj_size		= sizeof(struct udp6_sock),
1721 	.h.udp_table		= &udp_table,
1722 	.diag_destroy		= udp_abort,
1723 };
1724 
1725 static struct inet_protosw udpv6_protosw = {
1726 	.type =      SOCK_DGRAM,
1727 	.protocol =  IPPROTO_UDP,
1728 	.prot =      &udpv6_prot,
1729 	.ops =       &inet6_dgram_ops,
1730 	.flags =     INET_PROTOSW_PERMANENT,
1731 };
1732 
1733 int __init udpv6_init(void)
1734 {
1735 	int ret;
1736 
1737 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1738 	if (ret)
1739 		goto out;
1740 
1741 	ret = inet6_register_protosw(&udpv6_protosw);
1742 	if (ret)
1743 		goto out_udpv6_protocol;
1744 out:
1745 	return ret;
1746 
1747 out_udpv6_protocol:
1748 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1749 	goto out;
1750 }
1751 
1752 void udpv6_exit(void)
1753 {
1754 	inet6_unregister_protosw(&udpv6_protosw);
1755 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1756 }
1757