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