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