xref: /linux/net/ipv6/udp.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
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 void udpv6_err_update_exception(struct net *net, struct sk_buff *skb,
694 				       u8 type, __be32 info)
695 {
696 	if (type == ICMPV6_PKT_TOOBIG)
697 		ip6_update_pmtu(skb, net, info, skb->dev->ifindex, 0,
698 				sock_net_uid(net, NULL));
699 	else if (type == NDISC_REDIRECT)
700 		ip6_redirect(skb, net, skb->dev->ifindex, 0,
701 			     sock_net_uid(net, NULL));
702 }
703 
704 static int udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
705 		     u8 type, u8 code, int offset, __be32 info)
706 {
707 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
708 	struct udphdr *uh = (struct udphdr *)(skb->data + offset);
709 	const struct in6_addr *saddr, *daddr;
710 	struct net *net = dev_net(skb->dev);
711 	struct ipv6_pinfo *np;
712 	bool tunnel = false;
713 	struct sock *sk;
714 	int harderr;
715 	int err;
716 
717 	udpv6_err_update_exception(net, skb, type, info);
718 
719 	daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr;
720 	saddr = &hdr->saddr;
721 	sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
722 			       inet6_iif(skb), inet6_sdif(skb), NULL);
723 
724 	if (!sk || READ_ONCE(udp_sk(sk)->encap_type)) {
725 		/* No socket for error: try tunnels before discarding */
726 		if (static_branch_unlikely(&udpv6_encap_needed_key)) {
727 			sk = __udp6_lib_err_encap(net, hdr, offset, uh, sk, skb,
728 						  opt, type, code, info);
729 			if (!sk)
730 				return 0;
731 		} else
732 			sk = ERR_PTR(-ENOENT);
733 
734 		if (IS_ERR(sk)) {
735 			__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
736 					  ICMP6_MIB_INERRORS);
737 			return PTR_ERR(sk);
738 		}
739 
740 		tunnel = true;
741 	}
742 
743 	harderr = icmpv6_err_convert(type, code, &err);
744 	np = inet6_sk(sk);
745 
746 	if (type == ICMPV6_PKT_TOOBIG) {
747 		if (!ip6_sk_accept_pmtu(sk))
748 			goto out;
749 		ip6_sk_update_pmtu(skb, sk, info);
750 		if (READ_ONCE(np->pmtudisc) != IPV6_PMTUDISC_DONT)
751 			harderr = 1;
752 	}
753 	if (type == NDISC_REDIRECT) {
754 		ip6_sk_redirect(skb, sk);
755 		goto out;
756 	}
757 
758 	/* Tunnels don't have an application socket: don't pass errors back */
759 	if (tunnel) {
760 		if (udp_sk(sk)->encap_err_rcv)
761 			udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest,
762 						  ntohl(info), (u8 *)(uh+1));
763 		goto out;
764 	}
765 
766 	if (!inet6_test_bit(RECVERR6, sk)) {
767 		if (!harderr || sk->sk_state != TCP_ESTABLISHED)
768 			goto out;
769 	} else {
770 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
771 	}
772 
773 	sk->sk_err = err;
774 	sk_error_report(sk);
775 out:
776 	return 0;
777 }
778 
779 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
780 {
781 	int rc;
782 
783 	if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
784 		sock_rps_save_rxhash(sk, skb);
785 		sk_mark_napi_id(sk, skb);
786 		sk_incoming_cpu_update(sk);
787 	} else {
788 		sk_mark_napi_id_once(sk, skb);
789 	}
790 
791 	rc = __udp_enqueue_schedule_skb(sk, skb);
792 	if (rc < 0) {
793 		enum skb_drop_reason drop_reason;
794 		struct net *net = sock_net(sk);
795 
796 		/* Note that an ENOMEM error is charged twice */
797 		if (rc == -ENOMEM) {
798 			UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS);
799 			drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
800 		} else {
801 			UDP6_INC_STATS(net, UDP_MIB_MEMERRORS);
802 			drop_reason = SKB_DROP_REASON_PROTO_MEM;
803 		}
804 		UDP6_INC_STATS(net, UDP_MIB_INERRORS);
805 		trace_udp_fail_queue_rcv_skb(rc, sk, skb);
806 		sk_skb_reason_drop(sk, skb, drop_reason);
807 		return -1;
808 	}
809 
810 	return 0;
811 }
812 
813 static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
814 {
815 	enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
816 	struct udp_sock *up = udp_sk(sk);
817 	struct net *net = sock_net(sk);
818 
819 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
820 		drop_reason = SKB_DROP_REASON_XFRM_POLICY;
821 		goto drop;
822 	}
823 	nf_reset_ct(skb);
824 
825 	if (static_branch_unlikely(&udpv6_encap_needed_key) &&
826 	    READ_ONCE(up->encap_type)) {
827 		int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
828 
829 		/*
830 		 * This is an encapsulation socket so pass the skb to
831 		 * the socket's udp_encap_rcv() hook. Otherwise, just
832 		 * fall through and pass this up the UDP socket.
833 		 * up->encap_rcv() returns the following value:
834 		 * =0 if skb was successfully passed to the encap
835 		 *    handler or was discarded by it.
836 		 * >0 if skb should be passed on to UDP.
837 		 * <0 if skb should be resubmitted as proto -N
838 		 */
839 
840 		/* if we're overly short, let UDP handle it */
841 		encap_rcv = READ_ONCE(up->encap_rcv);
842 		if (encap_rcv) {
843 			int ret;
844 
845 			/* Verify checksum before giving to encap */
846 			if (udp_lib_checksum_complete(skb))
847 				goto csum_error;
848 
849 			ret = encap_rcv(sk, skb);
850 			if (ret <= 0) {
851 				__UDP6_INC_STATS(net, UDP_MIB_INDATAGRAMS);
852 				return -ret;
853 			}
854 		}
855 
856 		/* FALLTHROUGH -- it's a UDP Packet */
857 	}
858 
859 	prefetch(&sk->sk_rmem_alloc);
860 	if (rcu_access_pointer(sk->sk_filter) &&
861 	    udp_lib_checksum_complete(skb))
862 		goto csum_error;
863 
864 	drop_reason = sk_filter_trim_cap(sk, skb, sizeof(struct udphdr));
865 	if (drop_reason)
866 		goto drop;
867 
868 	udp_csum_pull_header(skb);
869 
870 	skb_dst_drop(skb);
871 
872 	return __udpv6_queue_rcv_skb(sk, skb);
873 
874 csum_error:
875 	drop_reason = SKB_DROP_REASON_UDP_CSUM;
876 	__UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS);
877 drop:
878 	__UDP6_INC_STATS(net, UDP_MIB_INERRORS);
879 	udp_drops_inc(sk);
880 	sk_skb_reason_drop(sk, skb, drop_reason);
881 	return -1;
882 }
883 
884 static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
885 {
886 	struct sk_buff *next, *segs;
887 	int ret;
888 
889 	if (likely(!udp_unexpected_gso(sk, skb)))
890 		return udpv6_queue_rcv_one_skb(sk, skb);
891 
892 	__skb_push(skb, -skb_mac_offset(skb));
893 	segs = udp_rcv_segment(sk, skb, false);
894 	skb_list_walk_safe(segs, skb, next) {
895 		__skb_pull(skb, skb_transport_offset(skb));
896 
897 		udp_post_segment_fix_csum(skb);
898 		ret = udpv6_queue_rcv_one_skb(sk, skb);
899 		if (ret > 0)
900 			ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
901 						 true);
902 	}
903 	return 0;
904 }
905 
906 static bool __udp_v6_is_mcast_sock(struct net *net, const struct sock *sk,
907 				   __be16 loc_port, const struct in6_addr *loc_addr,
908 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
909 				   int dif, int sdif, unsigned short hnum)
910 {
911 	const struct inet_sock *inet = inet_sk(sk);
912 
913 	if (!net_eq(sock_net(sk), net))
914 		return false;
915 
916 	if (udp_sk(sk)->udp_port_hash != hnum ||
917 	    sk->sk_family != PF_INET6 ||
918 	    (inet->inet_dport && inet->inet_dport != rmt_port) ||
919 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
920 		    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
921 	    !udp_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, sdif) ||
922 	    (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
923 		    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
924 		return false;
925 	if (!inet6_mc_check(sk, loc_addr, rmt_addr))
926 		return false;
927 	return true;
928 }
929 
930 static void udp6_csum_zero_error(struct sk_buff *skb)
931 {
932 	/* RFC 2460 section 8.1 says that we SHOULD log
933 	 * this error. Well, it is reasonable.
934 	 */
935 	net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
936 			    &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
937 			    &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
938 }
939 
940 /*
941  * Note: called only from the BH handler context,
942  * so we don't need to lock the hashes.
943  */
944 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
945 				    const struct in6_addr *saddr,
946 				    const struct in6_addr *daddr)
947 {
948 	struct udp_table *udptable = net->ipv4.udp_table;
949 	const struct udphdr *uh = udp_hdr(skb);
950 	unsigned int hash2, hash2_any, offset;
951 	unsigned short hnum = ntohs(uh->dest);
952 	struct sock *sk, *first = NULL;
953 	int sdif = inet6_sdif(skb);
954 	int dif = inet6_iif(skb);
955 	struct hlist_node *node;
956 	struct udp_hslot *hslot;
957 	struct sk_buff *nskb;
958 	bool use_hash2;
959 	int ret;
960 
961 	hash2_any = 0;
962 	hash2 = 0;
963 	hslot = udp_hashslot(udptable, net, hnum);
964 	use_hash2 = hslot->count > 10;
965 	offset = offsetof(typeof(*sk), sk_node);
966 
967 	if (use_hash2) {
968 		hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
969 			    udptable->mask;
970 		hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
971 start_lookup:
972 		hslot = &udptable->hash2[hash2].hslot;
973 		offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
974 	}
975 
976 	sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
977 		if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
978 					    uh->source, saddr, dif, sdif,
979 					    hnum))
980 			continue;
981 		/* If zero checksum and no_check is not on for
982 		 * the socket then skip it.
983 		 */
984 		if (!uh->check && !udp_get_no_check6_rx(sk))
985 			continue;
986 		if (!first) {
987 			first = sk;
988 			continue;
989 		}
990 		nskb = skb_clone(skb, GFP_ATOMIC);
991 		if (unlikely(!nskb)) {
992 			udp_drops_inc(sk);
993 			__UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS);
994 			__UDP6_INC_STATS(net, UDP_MIB_INERRORS);
995 			continue;
996 		}
997 
998 		if (udpv6_queue_rcv_skb(sk, nskb) > 0)
999 			consume_skb(nskb);
1000 	}
1001 
1002 	/* Also lookup *:port if we are using hash2 and haven't done so yet. */
1003 	if (use_hash2 && hash2 != hash2_any) {
1004 		hash2 = hash2_any;
1005 		goto start_lookup;
1006 	}
1007 
1008 	if (first) {
1009 		ret = udpv6_queue_rcv_skb(first, skb);
1010 		if (ret > 0)
1011 			return ret;
1012 	} else {
1013 		kfree_skb(skb);
1014 		__UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI);
1015 	}
1016 	return 0;
1017 }
1018 
1019 static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
1020 {
1021 	if (udp_sk_rx_dst_set(sk, dst))
1022 		sk->sk_rx_dst_cookie = rt6_get_cookie(dst_rt6_info(dst));
1023 }
1024 
1025 /* wrapper for udp_queue_rcv_skb taking care of csum conversion and
1026  * return code conversion for ip layer consumption
1027  */
1028 static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
1029 				struct udphdr *uh)
1030 {
1031 	int ret;
1032 
1033 	if (inet_get_convert_csum(sk) && uh->check)
1034 		skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
1035 
1036 	ret = udpv6_queue_rcv_skb(sk, skb);
1037 
1038 	/* a return value > 0 means to resubmit the input */
1039 	if (ret > 0)
1040 		return ret;
1041 	return 0;
1042 }
1043 
1044 static int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh)
1045 {
1046 	int err;
1047 
1048 	/* To support RFC 6936 (allow zero checksum in UDP/IPV6 for tunnels)
1049 	 * we accept a checksum of zero here. When we find the socket
1050 	 * for the UDP packet we'll check if that socket allows zero checksum
1051 	 * for IPv6 (set by socket option).
1052 	 *
1053 	 * Note, we are only interested in != 0 or == 0, thus the
1054 	 * force to int.
1055 	 */
1056 	err = (__force int)skb_checksum_init_zero_check(skb, IPPROTO_UDP, uh->check,
1057 							ip6_compute_pseudo);
1058 	if (err)
1059 		return err;
1060 
1061 	if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) {
1062 		/* If SW calculated the value, we know it's bad */
1063 		if (skb->csum_complete_sw)
1064 			return 1;
1065 
1066 		/* HW says the value is bad. Let's validate that.
1067 		 * skb->csum is no longer the full packet checksum,
1068 		 * so don't treat is as such.
1069 		 */
1070 		skb_checksum_complete_unset(skb);
1071 	}
1072 
1073 	return 0;
1074 }
1075 
1076 INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
1077 {
1078 	enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
1079 	const struct in6_addr *saddr, *daddr;
1080 	struct net *net = dev_net(skb->dev);
1081 	struct sock *sk = NULL;
1082 	struct udphdr *uh;
1083 	bool refcounted;
1084 	u32 ulen = 0;
1085 
1086 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
1087 		goto discard;
1088 
1089 	saddr = &ipv6_hdr(skb)->saddr;
1090 	daddr = &ipv6_hdr(skb)->daddr;
1091 	uh = udp_hdr(skb);
1092 
1093 	ulen = udp_get_len(skb, uh, 0);
1094 	if (ulen > skb->len)
1095 		goto short_packet;
1096 
1097 	/* Check for jumbo payload */
1098 	if (ulen == 0 && inet6_is_jumbogram(skb))
1099 		ulen = skb->len;
1100 
1101 	if (ulen < sizeof(*uh))
1102 		goto short_packet;
1103 
1104 	if (ulen < skb->len) {
1105 		if (pskb_trim_rcsum(skb, ulen))
1106 			goto short_packet;
1107 
1108 		saddr = &ipv6_hdr(skb)->saddr;
1109 		daddr = &ipv6_hdr(skb)->daddr;
1110 		uh = udp_hdr(skb);
1111 	}
1112 
1113 	if (udp6_csum_init(skb, uh))
1114 		goto csum_error;
1115 
1116 	/* Check if the socket is already available, e.g. due to early demux */
1117 	sk = inet6_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest,
1118 			      &refcounted, udp6_ehashfn);
1119 	if (IS_ERR(sk))
1120 		goto no_sk;
1121 
1122 	if (sk) {
1123 		struct dst_entry *dst = skb_dst(skb);
1124 		int ret;
1125 
1126 		if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
1127 			udp6_sk_rx_dst_set(sk, dst);
1128 
1129 		if (!uh->check && !udp_get_no_check6_rx(sk)) {
1130 			if (refcounted)
1131 				sock_put(sk);
1132 			goto report_csum_error;
1133 		}
1134 
1135 		ret = udp6_unicast_rcv_skb(sk, skb, uh);
1136 		if (refcounted)
1137 			sock_put(sk);
1138 		return ret;
1139 	}
1140 
1141 	/*
1142 	 *	Multicast receive code
1143 	 */
1144 	if (ipv6_addr_is_multicast(daddr))
1145 		return __udp6_lib_mcast_deliver(net, skb, saddr, daddr);
1146 
1147 	/* Unicast */
1148 	sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest);
1149 	if (sk) {
1150 		if (!uh->check && !udp_get_no_check6_rx(sk))
1151 			goto report_csum_error;
1152 		return udp6_unicast_rcv_skb(sk, skb, uh);
1153 	}
1154 no_sk:
1155 	reason = SKB_DROP_REASON_NO_SOCKET;
1156 
1157 	if (!uh->check)
1158 		goto report_csum_error;
1159 
1160 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1161 		goto discard;
1162 	nf_reset_ct(skb);
1163 
1164 	if (udp_lib_checksum_complete(skb))
1165 		goto csum_error;
1166 
1167 	__UDP6_INC_STATS(net, UDP_MIB_NOPORTS);
1168 	icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
1169 
1170 	sk_skb_reason_drop(sk, skb, reason);
1171 	return 0;
1172 
1173 short_packet:
1174 	if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
1175 		reason = SKB_DROP_REASON_PKT_TOO_SMALL;
1176 	net_dbg_ratelimited("UDPv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
1177 			    saddr, ntohs(uh->source),
1178 			    ulen, skb->len,
1179 			    daddr, ntohs(uh->dest));
1180 	goto discard;
1181 
1182 report_csum_error:
1183 	udp6_csum_zero_error(skb);
1184 csum_error:
1185 	if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
1186 		reason = SKB_DROP_REASON_UDP_CSUM;
1187 	__UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS);
1188 discard:
1189 	__UDP6_INC_STATS(net, UDP_MIB_INERRORS);
1190 	sk_skb_reason_drop(sk, skb, reason);
1191 	return 0;
1192 }
1193 
1194 
1195 static struct sock *__udp6_lib_demux_lookup(struct net *net,
1196 			__be16 loc_port, const struct in6_addr *loc_addr,
1197 			__be16 rmt_port, const struct in6_addr *rmt_addr,
1198 			int dif, int sdif)
1199 {
1200 	struct udp_table *udptable = net->ipv4.udp_table;
1201 	unsigned short hnum = ntohs(loc_port);
1202 	struct udp_hslot *hslot2;
1203 	unsigned int hash2;
1204 	__portpair ports;
1205 	struct sock *sk;
1206 
1207 	hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
1208 	hslot2 = udp_hashslot2(udptable, hash2);
1209 	ports = INET_COMBINED_PORTS(rmt_port, hnum);
1210 
1211 	udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
1212 		if (sk->sk_state == TCP_ESTABLISHED &&
1213 		    inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
1214 			return sk;
1215 		/* Only check first socket in chain */
1216 		break;
1217 	}
1218 	return NULL;
1219 }
1220 
1221 void udp_v6_early_demux(struct sk_buff *skb)
1222 {
1223 	struct net *net = dev_net(skb->dev);
1224 	const struct udphdr *uh;
1225 	struct sock *sk;
1226 	struct dst_entry *dst;
1227 	int dif = skb->dev->ifindex;
1228 	int sdif = inet6_sdif(skb);
1229 
1230 	if (!pskb_may_pull(skb, skb_transport_offset(skb) +
1231 	    sizeof(struct udphdr)))
1232 		return;
1233 
1234 	uh = udp_hdr(skb);
1235 
1236 	if (skb->pkt_type == PACKET_HOST)
1237 		sk = __udp6_lib_demux_lookup(net, uh->dest,
1238 					     &ipv6_hdr(skb)->daddr,
1239 					     uh->source, &ipv6_hdr(skb)->saddr,
1240 					     dif, sdif);
1241 	else
1242 		return;
1243 
1244 	if (!sk)
1245 		return;
1246 
1247 	skb->sk = sk;
1248 	DEBUG_NET_WARN_ON_ONCE(sk_is_refcounted(sk));
1249 	skb->destructor = sock_pfree;
1250 	dst = rcu_dereference(sk->sk_rx_dst);
1251 
1252 	/* A dual-stack socket can cache an IPv4 dst. */
1253 	if (dst && dst->ops->family == AF_INET6)
1254 		dst = dst_check(dst, sk->sk_rx_dst_cookie);
1255 	else
1256 		dst = NULL;
1257 	if (dst) {
1258 		/* set noref for now.
1259 		 * any place which wants to hold dst has to call
1260 		 * dst_hold_safe()
1261 		 */
1262 		skb_dst_set_noref(skb, dst);
1263 	}
1264 }
1265 
1266 /*
1267  * Throw away all pending data and cancel the corking. Socket is locked.
1268  */
1269 static void udp_v6_flush_pending_frames(struct sock *sk)
1270 {
1271 	struct udp_sock *up = udp_sk(sk);
1272 
1273 	if (up->pending == AF_INET)
1274 		udp_flush_pending_frames(sk);
1275 	else if (up->pending) {
1276 		up->len = 0;
1277 		WRITE_ONCE(up->pending, 0);
1278 		ip6_flush_pending_frames(sk);
1279 	}
1280 }
1281 
1282 static int udpv6_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
1283 			     int addr_len)
1284 {
1285 	if (addr_len < offsetofend(struct sockaddr, sa_family))
1286 		return -EINVAL;
1287 	/* The following checks are replicated from __ip6_datagram_connect()
1288 	 * and intended to prevent BPF program called below from accessing
1289 	 * bytes that are out of the bound specified by user in addr_len.
1290 	 */
1291 	if (uaddr->sa_family == AF_INET) {
1292 		if (ipv6_only_sock(sk))
1293 			return -EAFNOSUPPORT;
1294 		return udp_pre_connect(sk, uaddr, addr_len);
1295 	}
1296 
1297 	if (addr_len < SIN6_LEN_RFC2133)
1298 		return -EINVAL;
1299 
1300 	return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr, &addr_len);
1301 }
1302 
1303 static int udpv6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
1304 			 int addr_len)
1305 {
1306 	int res;
1307 
1308 	lock_sock(sk);
1309 	res = __ip6_datagram_connect(sk, uaddr, addr_len);
1310 	if (!res)
1311 		udp6_hash4(sk);
1312 	release_sock(sk);
1313 	return res;
1314 }
1315 
1316 /**
1317  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
1318  *	@sk:	socket we are sending on
1319  *	@skb:	sk_buff containing the filled-in UDP header
1320  *		(checksum field must be zeroed out)
1321  *	@saddr: source address
1322  *	@daddr: destination address
1323  *	@len:	length of packet
1324  */
1325 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1326 				 const struct in6_addr *saddr,
1327 				 const struct in6_addr *daddr, int len)
1328 {
1329 	unsigned int offset;
1330 	struct udphdr *uh = udp_hdr(skb);
1331 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1332 	__wsum csum = 0;
1333 
1334 	if (!frags) {
1335 		/* Only one fragment on the socket.  */
1336 		skb->csum_start = skb_transport_header(skb) - skb->head;
1337 		skb->csum_offset = offsetof(struct udphdr, check);
1338 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1339 	} else {
1340 		/*
1341 		 * HW-checksum won't work as there are two or more
1342 		 * fragments on the socket so that all csums of sk_buffs
1343 		 * should be together
1344 		 */
1345 		offset = skb_transport_offset(skb);
1346 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1347 		csum = skb->csum;
1348 
1349 		skb->ip_summed = CHECKSUM_NONE;
1350 
1351 		do {
1352 			csum = csum_add(csum, frags->csum);
1353 		} while ((frags = frags->next));
1354 
1355 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1356 					    csum);
1357 		if (uh->check == 0)
1358 			uh->check = CSUM_MANGLED_0;
1359 	}
1360 }
1361 
1362 /*
1363  *	Sending
1364  */
1365 
1366 static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1367 			   struct inet_cork *cork)
1368 {
1369 	struct sock *sk = skb->sk;
1370 	int offset, len, datalen;
1371 	struct udphdr *uh;
1372 	int err = 0;
1373 
1374 	offset = skb_transport_offset(skb);
1375 	len = skb->len - offset;
1376 	datalen = len - sizeof(*uh);
1377 
1378 	/*
1379 	 * Create a UDP header
1380 	 */
1381 	uh = udp_hdr(skb);
1382 	uh->source = fl6->fl6_sport;
1383 	uh->dest = fl6->fl6_dport;
1384 	/* Datagram length checked in udpv6_sendmsg. */
1385 	udp_set_len_short(uh, len);
1386 	uh->check = 0;
1387 
1388 	if (cork->gso_size) {
1389 		const int hlen = skb_network_header_len(skb) +
1390 				 sizeof(struct udphdr);
1391 
1392 		if (hlen + min(datalen, cork->gso_size) > cork->fragsize) {
1393 			kfree_skb(skb);
1394 			return -EMSGSIZE;
1395 		}
1396 		if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
1397 			kfree_skb(skb);
1398 			return -EINVAL;
1399 		}
1400 		if (udp_get_no_check6_tx(sk)) {
1401 			kfree_skb(skb);
1402 			return -EINVAL;
1403 		}
1404 		if (dst_xfrm(skb_dst(skb))) {
1405 			kfree_skb(skb);
1406 			return -EIO;
1407 		}
1408 
1409 		if (datalen > cork->gso_size) {
1410 			skb_shinfo(skb)->gso_size = cork->gso_size;
1411 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
1412 			skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
1413 								 cork->gso_size);
1414 
1415 			/* Don't checksum the payload, skb will get segmented */
1416 			goto csum_partial;
1417 		}
1418 	}
1419 
1420 	if (udp_get_no_check6_tx(sk)) {   /* UDP csum disabled */
1421 		skb->ip_summed = CHECKSUM_NONE;
1422 		goto send;
1423 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1424 csum_partial:
1425 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1426 		goto send;
1427 	}
1428 
1429 	/* add protocol-dependent pseudo-header */
1430 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1431 				    len, IPPROTO_UDP, udp_csum(skb));
1432 	if (uh->check == 0)
1433 		uh->check = CSUM_MANGLED_0;
1434 
1435 send:
1436 	err = ip6_send_skb(skb);
1437 	if (unlikely(err)) {
1438 		if (err == -ENOBUFS && !inet6_test_bit(RECVERR6, sk)) {
1439 			UDP6_INC_STATS(sock_net(sk), UDP_MIB_SNDBUFERRORS);
1440 			err = 0;
1441 		}
1442 	} else {
1443 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_OUTDATAGRAMS);
1444 	}
1445 	return err;
1446 }
1447 
1448 static int udp_v6_push_pending_frames(struct sock *sk)
1449 {
1450 	struct sk_buff *skb;
1451 	struct udp_sock  *up = udp_sk(sk);
1452 	int err = 0;
1453 
1454 	if (up->pending == AF_INET)
1455 		return udp_push_pending_frames(sk);
1456 
1457 	skb = ip6_finish_skb(sk);
1458 	if (!skb)
1459 		goto out;
1460 
1461 	err = udp_v6_send_skb(skb, &inet_sk(sk)->cork.fl.u.ip6,
1462 			      &inet_sk(sk)->cork.base);
1463 out:
1464 	up->len = 0;
1465 	WRITE_ONCE(up->pending, 0);
1466 	return err;
1467 }
1468 
1469 int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1470 {
1471 	int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE;
1472 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1473 	struct ipv6_txoptions *opt_to_free = NULL;
1474 	struct in6_addr *daddr, *final_p, final;
1475 	struct ip6_flowlabel *flowlabel = NULL;
1476 	struct inet_sock *inet = inet_sk(sk);
1477 	struct ipv6_pinfo *np = inet6_sk(sk);
1478 	struct ipv6_txoptions *opt = NULL;
1479 	struct udp_sock *up = udp_sk(sk);
1480 	struct ipv6_txoptions opt_space;
1481 	int addr_len = msg->msg_namelen;
1482 	struct inet_cork_full cork;
1483 	struct ipcm6_cookie ipc6;
1484 	bool connected = false;
1485 	struct dst_entry *dst;
1486 	struct flowi6 *fl6;
1487 	int ulen = len;
1488 	int err;
1489 
1490 	fl6 = &cork.fl.u.ip6;
1491 	ipcm6_init_sk(&ipc6, sk);
1492 	ipc6.gso_size = READ_ONCE(up->gso_size);
1493 
1494 	/* destination address check */
1495 	if (sin6) {
1496 		if (addr_len < offsetof(struct sockaddr, sa_data))
1497 			return -EINVAL;
1498 
1499 		switch (sin6->sin6_family) {
1500 		case AF_INET6:
1501 			if (addr_len < SIN6_LEN_RFC2133)
1502 				return -EINVAL;
1503 			daddr = &sin6->sin6_addr;
1504 			if (ipv6_addr_any(daddr) &&
1505 			    ipv6_addr_v4mapped(&np->saddr))
1506 				ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1507 						       daddr);
1508 			break;
1509 		case AF_INET:
1510 			goto do_udp_sendmsg;
1511 		case AF_UNSPEC:
1512 			msg->msg_name = sin6 = NULL;
1513 			msg->msg_namelen = addr_len = 0;
1514 			daddr = NULL;
1515 			break;
1516 		default:
1517 			return -EINVAL;
1518 		}
1519 	} else if (!READ_ONCE(up->pending)) {
1520 		if (sk->sk_state != TCP_ESTABLISHED)
1521 			return -EDESTADDRREQ;
1522 		daddr = &sk->sk_v6_daddr;
1523 	} else
1524 		daddr = NULL;
1525 
1526 	if (daddr) {
1527 		if (ipv6_addr_v4mapped(daddr)) {
1528 			struct sockaddr_in sin;
1529 			sin.sin_family = AF_INET;
1530 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1531 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1532 			msg->msg_name = &sin;
1533 			msg->msg_namelen = sizeof(sin);
1534 do_udp_sendmsg:
1535 			err = ipv6_only_sock(sk) ?
1536 				-ENETUNREACH : udp_sendmsg(sk, msg, len);
1537 			msg->msg_name = sin6;
1538 			msg->msg_namelen = addr_len;
1539 			return err;
1540 		}
1541 	}
1542 
1543 	/* Rough check on arithmetic overflow,
1544 	   better check is made in ip6_append_data().
1545 	   */
1546 	if (len > INT_MAX - sizeof(struct udphdr))
1547 		return -EMSGSIZE;
1548 
1549 	if (READ_ONCE(up->pending)) {
1550 		if (READ_ONCE(up->pending) == AF_INET)
1551 			return udp_sendmsg(sk, msg, len);
1552 		/*
1553 		 * There are pending frames.
1554 		 * The socket lock must be held while it's corked.
1555 		 */
1556 		lock_sock(sk);
1557 		if (likely(up->pending)) {
1558 			if (unlikely(up->pending != AF_INET6)) {
1559 				release_sock(sk);
1560 				return -EAFNOSUPPORT;
1561 			}
1562 			dst = NULL;
1563 			goto do_append_data;
1564 		}
1565 		release_sock(sk);
1566 	}
1567 	ulen += sizeof(struct udphdr);
1568 
1569 	memset(fl6, 0, sizeof(*fl6));
1570 
1571 	if (sin6) {
1572 		if (sin6->sin6_port == 0)
1573 			return -EINVAL;
1574 
1575 		fl6->fl6_dport = sin6->sin6_port;
1576 		daddr = &sin6->sin6_addr;
1577 
1578 		if (inet6_test_bit(SNDFLOW, sk)) {
1579 			fl6->flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1580 			if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) {
1581 				flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
1582 				if (IS_ERR(flowlabel))
1583 					return -EINVAL;
1584 			}
1585 		}
1586 
1587 		/*
1588 		 * Otherwise it will be difficult to maintain
1589 		 * sk->sk_dst_cache.
1590 		 */
1591 		if (sk->sk_state == TCP_ESTABLISHED &&
1592 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1593 			daddr = &sk->sk_v6_daddr;
1594 
1595 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1596 		    sin6->sin6_scope_id &&
1597 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
1598 			fl6->flowi6_oif = sin6->sin6_scope_id;
1599 	} else {
1600 		if (sk->sk_state != TCP_ESTABLISHED)
1601 			return -EDESTADDRREQ;
1602 
1603 		fl6->fl6_dport = inet->inet_dport;
1604 		daddr = &sk->sk_v6_daddr;
1605 		fl6->flowlabel = np->flow_label;
1606 		connected = true;
1607 	}
1608 
1609 	if (!fl6->flowi6_oif)
1610 		fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
1611 
1612 	if (!fl6->flowi6_oif)
1613 		fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1614 
1615 	fl6->flowi6_uid = sk_uid(sk);
1616 
1617 	if (msg->msg_controllen) {
1618 		opt = &opt_space;
1619 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1620 		opt->tot_len = sizeof(*opt);
1621 		ipc6.opt = opt;
1622 
1623 		err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
1624 		if (err > 0) {
1625 			err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, fl6,
1626 						    &ipc6);
1627 			connected = false;
1628 		}
1629 		if (err < 0) {
1630 			fl6_sock_release(flowlabel);
1631 			return err;
1632 		}
1633 		if ((fl6->flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1634 			flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
1635 			if (IS_ERR(flowlabel))
1636 				return -EINVAL;
1637 		}
1638 		if (!(opt->opt_nflen|opt->opt_flen))
1639 			opt = NULL;
1640 	}
1641 	if (!opt) {
1642 		opt = txopt_get(np);
1643 		opt_to_free = opt;
1644 	}
1645 	if (flowlabel)
1646 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1647 	opt = ipv6_fixup_options(&opt_space, opt);
1648 	ipc6.opt = opt;
1649 
1650 	fl6->flowi6_proto = IPPROTO_UDP;
1651 	fl6->flowi6_mark = ipc6.sockc.mark;
1652 	fl6->daddr = *daddr;
1653 	if (ipv6_addr_any(&fl6->saddr) && !ipv6_addr_any(&np->saddr))
1654 		fl6->saddr = np->saddr;
1655 	fl6->fl6_sport = inet->inet_sport;
1656 
1657 	if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) {
1658 		err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
1659 					   (struct sockaddr *)sin6,
1660 					   &addr_len,
1661 					   &fl6->saddr);
1662 		if (err)
1663 			goto out_no_dst;
1664 		if (sin6) {
1665 			if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
1666 				/* BPF program rewrote IPv6-only by IPv4-mapped
1667 				 * IPv6. It's currently unsupported.
1668 				 */
1669 				err = -ENOTSUPP;
1670 				goto out_no_dst;
1671 			}
1672 			if (sin6->sin6_port == 0) {
1673 				/* BPF program set invalid port. Reject it. */
1674 				err = -EINVAL;
1675 				goto out_no_dst;
1676 			}
1677 			fl6->fl6_dport = sin6->sin6_port;
1678 			fl6->daddr = sin6->sin6_addr;
1679 		}
1680 	}
1681 
1682 	if (ipv6_addr_any(&fl6->daddr))
1683 		fl6->daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1684 
1685 	final_p = fl6_update_dst(fl6, opt, &final);
1686 	if (final_p)
1687 		connected = false;
1688 
1689 	if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) {
1690 		fl6->flowi6_oif = READ_ONCE(np->mcast_oif);
1691 		connected = false;
1692 	} else if (!fl6->flowi6_oif)
1693 		fl6->flowi6_oif = READ_ONCE(np->ucast_oif);
1694 
1695 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
1696 
1697 	fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel);
1698 
1699 	dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected);
1700 	if (IS_ERR(dst)) {
1701 		err = PTR_ERR(dst);
1702 		dst = NULL;
1703 		goto out;
1704 	}
1705 
1706 	if (ipc6.hlimit < 0)
1707 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, fl6, dst);
1708 
1709 	if (msg->msg_flags&MSG_CONFIRM)
1710 		goto do_confirm;
1711 back_from_confirm:
1712 
1713 	/* Lockless fast path for the non-corking case */
1714 	if (!corkreq) {
1715 		struct sk_buff *skb;
1716 
1717 		skb = ip6_make_skb(sk, ip_generic_getfrag, msg, ulen,
1718 				   sizeof(struct udphdr), &ipc6,
1719 				   dst_rt6_info(dst),
1720 				   msg->msg_flags, &cork);
1721 		err = PTR_ERR(skb);
1722 		if (!IS_ERR_OR_NULL(skb))
1723 			err = udp_v6_send_skb(skb, fl6, &cork.base);
1724 		/* ip6_make_skb steals dst reference */
1725 		goto out_no_dst;
1726 	}
1727 
1728 	lock_sock(sk);
1729 	if (unlikely(up->pending)) {
1730 		/* The socket is already corked while preparing it. */
1731 		/* ... which is an evident application bug. --ANK */
1732 		release_sock(sk);
1733 
1734 		net_dbg_ratelimited("udp cork app bug 2\n");
1735 		err = -EINVAL;
1736 		goto out;
1737 	}
1738 
1739 	WRITE_ONCE(up->pending, AF_INET6);
1740 
1741 do_append_data:
1742 	up->len += ulen;
1743 	err = ip6_append_data(sk, ip_generic_getfrag, msg, ulen,
1744 			      sizeof(struct udphdr), &ipc6, fl6,
1745 			      dst_rt6_info(dst),
1746 			      corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1747 	if (err)
1748 		udp_v6_flush_pending_frames(sk);
1749 	else if (!corkreq)
1750 		err = udp_v6_push_pending_frames(sk);
1751 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1752 		WRITE_ONCE(up->pending, 0);
1753 
1754 	if (err > 0)
1755 		err = inet6_test_bit(RECVERR6, sk) ? net_xmit_errno(err) : 0;
1756 	release_sock(sk);
1757 
1758 out:
1759 	dst_release(dst);
1760 out_no_dst:
1761 	fl6_sock_release(flowlabel);
1762 	txopt_put(opt_to_free);
1763 	if (!err)
1764 		return len;
1765 	/*
1766 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1767 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1768 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1769 	 * things).  We could add another new stat but at least for now that
1770 	 * seems like overkill.
1771 	 */
1772 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
1773 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_SNDBUFERRORS);
1774 
1775 	return err;
1776 
1777 do_confirm:
1778 	if (msg->msg_flags & MSG_PROBE)
1779 		dst_confirm_neigh(dst, &fl6->daddr);
1780 	if (!(msg->msg_flags&MSG_PROBE) || len)
1781 		goto back_from_confirm;
1782 	err = 0;
1783 	goto out;
1784 }
1785 EXPORT_SYMBOL(udpv6_sendmsg);
1786 
1787 static void udpv6_splice_eof(struct socket *sock)
1788 {
1789 	struct sock *sk = sock->sk;
1790 	struct udp_sock *up = udp_sk(sk);
1791 
1792 	if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk))
1793 		return;
1794 
1795 	lock_sock(sk);
1796 	if (up->pending && !udp_test_bit(CORK, sk))
1797 		udp_v6_push_pending_frames(sk);
1798 	release_sock(sk);
1799 }
1800 
1801 static void udpv6_destroy_sock(struct sock *sk)
1802 {
1803 	struct udp_sock *up = udp_sk(sk);
1804 	lock_sock(sk);
1805 
1806 	/* protects from races with udp_abort() */
1807 	sock_set_flag(sk, SOCK_DEAD);
1808 	udp_v6_flush_pending_frames(sk);
1809 	release_sock(sk);
1810 
1811 	if (static_branch_unlikely(&udpv6_encap_needed_key)) {
1812 		if (up->encap_type) {
1813 			void (*encap_destroy)(struct sock *sk);
1814 			encap_destroy = READ_ONCE(up->encap_destroy);
1815 			if (encap_destroy)
1816 				encap_destroy(sk);
1817 		}
1818 		if (udp_test_bit(ENCAP_ENABLED, sk)) {
1819 			static_branch_dec(&udpv6_encap_needed_key);
1820 			udp_encap_disable();
1821 			udp_tunnel_cleanup_gro(sk);
1822 		}
1823 	}
1824 }
1825 
1826 /*
1827  *	Socket option code for UDP
1828  */
1829 static int udpv6_setsockopt(struct sock *sk, int level, int optname,
1830 			    sockptr_t optval, unsigned int optlen)
1831 {
1832 	if (level == SOL_UDP || level == SOL_SOCKET)
1833 		return udp_lib_setsockopt(sk, level, optname,
1834 					  optval, optlen,
1835 					  udp_v6_push_pending_frames);
1836 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1837 }
1838 
1839 static int udpv6_getsockopt(struct sock *sk, int level, int optname,
1840 			    char __user *optval, int __user *optlen)
1841 {
1842 	sockopt_t opt;
1843 	int err;
1844 
1845 	if (level != SOL_UDP)
1846 		return ipv6_getsockopt(sk, level, optname, optval, optlen);
1847 
1848 	err = sockopt_init_user(&opt, optval, optlen);
1849 	if (err)
1850 		return err;
1851 
1852 	err = udp_lib_getsockopt(sk, level, optname, &opt);
1853 	if (err)
1854 		return err;
1855 	if (put_user(opt.optlen, optlen))
1856 		return -EFAULT;
1857 	return 0;
1858 }
1859 
1860 
1861 /* ------------------------------------------------------------------------ */
1862 #ifdef CONFIG_PROC_FS
1863 static int udp6_seq_show(struct seq_file *seq, void *v)
1864 {
1865 	if (v == SEQ_START_TOKEN) {
1866 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1867 	} else {
1868 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1869 		const struct inet_sock *inet = inet_sk((const struct sock *)v);
1870 		__u16 srcp = ntohs(inet->inet_sport);
1871 		__u16 destp = ntohs(inet->inet_dport);
1872 		__ip6_dgram_sock_seq_show(seq, v, srcp, destp,
1873 					  udp_rqueue_get(v), bucket);
1874 	}
1875 	return 0;
1876 }
1877 
1878 static const struct seq_operations udp6_seq_ops = {
1879 	.start		= udp_seq_start,
1880 	.next		= udp_seq_next,
1881 	.stop		= udp_seq_stop,
1882 	.show		= udp6_seq_show,
1883 };
1884 
1885 static struct udp_seq_afinfo udp6_seq_afinfo = {
1886 	.family		= AF_INET6,
1887 };
1888 
1889 int __net_init udp6_proc_init(struct net *net)
1890 {
1891 	if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1892 			sizeof(struct udp_iter_state), &udp6_seq_afinfo))
1893 		return -ENOMEM;
1894 	return 0;
1895 }
1896 
1897 void udp6_proc_exit(struct net *net)
1898 {
1899 	remove_proc_entry("udp6", net->proc_net);
1900 }
1901 #endif /* CONFIG_PROC_FS */
1902 
1903 /* ------------------------------------------------------------------------ */
1904 
1905 struct proto udpv6_prot = {
1906 	.name			= "UDPv6",
1907 	.owner			= THIS_MODULE,
1908 	.close			= udp_lib_close,
1909 	.pre_connect		= udpv6_pre_connect,
1910 	.connect		= udpv6_connect,
1911 	.disconnect		= udp_disconnect,
1912 	.ioctl			= udp_ioctl,
1913 	.init			= udpv6_init_sock,
1914 	.destroy		= udpv6_destroy_sock,
1915 	.setsockopt		= udpv6_setsockopt,
1916 	.getsockopt		= udpv6_getsockopt,
1917 	.sendmsg		= udpv6_sendmsg,
1918 	.recvmsg		= udpv6_recvmsg,
1919 	.splice_eof		= udpv6_splice_eof,
1920 	.release_cb		= ip6_datagram_release_cb,
1921 	.hash			= udp_lib_hash,
1922 	.unhash			= udp_lib_unhash,
1923 	.rehash			= udp_v6_rehash,
1924 	.get_port		= udp_v6_get_port,
1925 	.put_port		= udp_lib_unhash,
1926 #ifdef CONFIG_BPF_SYSCALL
1927 	.psock_update_sk_prot	= udp_bpf_update_proto,
1928 #endif
1929 
1930 	.memory_allocated	= &net_aligned_data.udp_memory_allocated,
1931 	.per_cpu_fw_alloc	= &udp_memory_per_cpu_fw_alloc,
1932 
1933 	.sysctl_mem		= sysctl_udp_mem,
1934 	.sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
1935 	.sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1936 	.obj_size		= sizeof(struct udp6_sock),
1937 	.ipv6_pinfo_offset = offsetof(struct udp6_sock, inet6),
1938 	.diag_destroy		= udp_abort,
1939 };
1940 
1941 static struct inet_protosw udpv6_protosw = {
1942 	.type =      SOCK_DGRAM,
1943 	.protocol =  IPPROTO_UDP,
1944 	.prot =      &udpv6_prot,
1945 	.ops =       &inet6_dgram_ops,
1946 	.flags =     INET_PROTOSW_PERMANENT,
1947 };
1948 
1949 int __init udpv6_init(void)
1950 {
1951 	int ret;
1952 
1953 	net_hotdata.udpv6_protocol = (struct inet6_protocol) {
1954 		.handler     = udpv6_rcv,
1955 		.err_handler = udpv6_err,
1956 		.flags	     = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
1957 	};
1958 	ret = inet6_add_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
1959 	if (ret)
1960 		goto out;
1961 
1962 	ret = inet6_register_protosw(&udpv6_protosw);
1963 	if (ret)
1964 		goto out_udpv6_protocol;
1965 out:
1966 	return ret;
1967 
1968 out_udpv6_protocol:
1969 	inet6_del_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
1970 	goto out;
1971 }
1972 
1973 void udpv6_exit(void)
1974 {
1975 	inet6_unregister_protosw(&udpv6_protosw);
1976 	inet6_del_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
1977 }
1978