xref: /linux/net/ipv6/udp.c (revision 4596be8a15db2e7f45cbb140f6644e2dd2b290db)
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 	if (dst)
1253 		dst = dst_check(dst, sk->sk_rx_dst_cookie);
1254 	if (dst) {
1255 		/* set noref for now.
1256 		 * any place which wants to hold dst has to call
1257 		 * dst_hold_safe()
1258 		 */
1259 		skb_dst_set_noref(skb, dst);
1260 	}
1261 }
1262 
1263 /*
1264  * Throw away all pending data and cancel the corking. Socket is locked.
1265  */
1266 static void udp_v6_flush_pending_frames(struct sock *sk)
1267 {
1268 	struct udp_sock *up = udp_sk(sk);
1269 
1270 	if (up->pending == AF_INET)
1271 		udp_flush_pending_frames(sk);
1272 	else if (up->pending) {
1273 		up->len = 0;
1274 		WRITE_ONCE(up->pending, 0);
1275 		ip6_flush_pending_frames(sk);
1276 	}
1277 }
1278 
1279 static int udpv6_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
1280 			     int addr_len)
1281 {
1282 	if (addr_len < offsetofend(struct sockaddr, sa_family))
1283 		return -EINVAL;
1284 	/* The following checks are replicated from __ip6_datagram_connect()
1285 	 * and intended to prevent BPF program called below from accessing
1286 	 * bytes that are out of the bound specified by user in addr_len.
1287 	 */
1288 	if (uaddr->sa_family == AF_INET) {
1289 		if (ipv6_only_sock(sk))
1290 			return -EAFNOSUPPORT;
1291 		return udp_pre_connect(sk, uaddr, addr_len);
1292 	}
1293 
1294 	if (addr_len < SIN6_LEN_RFC2133)
1295 		return -EINVAL;
1296 
1297 	return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr, &addr_len);
1298 }
1299 
1300 static int udpv6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
1301 			 int addr_len)
1302 {
1303 	int res;
1304 
1305 	lock_sock(sk);
1306 	res = __ip6_datagram_connect(sk, uaddr, addr_len);
1307 	if (!res)
1308 		udp6_hash4(sk);
1309 	release_sock(sk);
1310 	return res;
1311 }
1312 
1313 /**
1314  *	udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
1315  *	@sk:	socket we are sending on
1316  *	@skb:	sk_buff containing the filled-in UDP header
1317  *		(checksum field must be zeroed out)
1318  *	@saddr: source address
1319  *	@daddr: destination address
1320  *	@len:	length of packet
1321  */
1322 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1323 				 const struct in6_addr *saddr,
1324 				 const struct in6_addr *daddr, int len)
1325 {
1326 	unsigned int offset;
1327 	struct udphdr *uh = udp_hdr(skb);
1328 	struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1329 	__wsum csum = 0;
1330 
1331 	if (!frags) {
1332 		/* Only one fragment on the socket.  */
1333 		skb->csum_start = skb_transport_header(skb) - skb->head;
1334 		skb->csum_offset = offsetof(struct udphdr, check);
1335 		uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1336 	} else {
1337 		/*
1338 		 * HW-checksum won't work as there are two or more
1339 		 * fragments on the socket so that all csums of sk_buffs
1340 		 * should be together
1341 		 */
1342 		offset = skb_transport_offset(skb);
1343 		skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1344 		csum = skb->csum;
1345 
1346 		skb->ip_summed = CHECKSUM_NONE;
1347 
1348 		do {
1349 			csum = csum_add(csum, frags->csum);
1350 		} while ((frags = frags->next));
1351 
1352 		uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1353 					    csum);
1354 		if (uh->check == 0)
1355 			uh->check = CSUM_MANGLED_0;
1356 	}
1357 }
1358 
1359 /*
1360  *	Sending
1361  */
1362 
1363 static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1364 			   struct inet_cork *cork)
1365 {
1366 	struct sock *sk = skb->sk;
1367 	int offset, len, datalen;
1368 	struct udphdr *uh;
1369 	int err = 0;
1370 
1371 	offset = skb_transport_offset(skb);
1372 	len = skb->len - offset;
1373 	datalen = len - sizeof(*uh);
1374 
1375 	/*
1376 	 * Create a UDP header
1377 	 */
1378 	uh = udp_hdr(skb);
1379 	uh->source = fl6->fl6_sport;
1380 	uh->dest = fl6->fl6_dport;
1381 	/* Datagram length checked in udpv6_sendmsg. */
1382 	udp_set_len_short(uh, len);
1383 	uh->check = 0;
1384 
1385 	if (cork->gso_size) {
1386 		const int hlen = skb_network_header_len(skb) +
1387 				 sizeof(struct udphdr);
1388 
1389 		if (hlen + min(datalen, cork->gso_size) > cork->fragsize) {
1390 			kfree_skb(skb);
1391 			return -EMSGSIZE;
1392 		}
1393 		if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
1394 			kfree_skb(skb);
1395 			return -EINVAL;
1396 		}
1397 		if (udp_get_no_check6_tx(sk)) {
1398 			kfree_skb(skb);
1399 			return -EINVAL;
1400 		}
1401 		if (dst_xfrm(skb_dst(skb))) {
1402 			kfree_skb(skb);
1403 			return -EIO;
1404 		}
1405 
1406 		if (datalen > cork->gso_size) {
1407 			skb_shinfo(skb)->gso_size = cork->gso_size;
1408 			skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
1409 			skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
1410 								 cork->gso_size);
1411 
1412 			/* Don't checksum the payload, skb will get segmented */
1413 			goto csum_partial;
1414 		}
1415 	}
1416 
1417 	if (udp_get_no_check6_tx(sk)) {   /* UDP csum disabled */
1418 		skb->ip_summed = CHECKSUM_NONE;
1419 		goto send;
1420 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1421 csum_partial:
1422 		udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1423 		goto send;
1424 	}
1425 
1426 	/* add protocol-dependent pseudo-header */
1427 	uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1428 				    len, IPPROTO_UDP, udp_csum(skb));
1429 	if (uh->check == 0)
1430 		uh->check = CSUM_MANGLED_0;
1431 
1432 send:
1433 	err = ip6_send_skb(skb);
1434 	if (unlikely(err)) {
1435 		if (err == -ENOBUFS && !inet6_test_bit(RECVERR6, sk)) {
1436 			UDP6_INC_STATS(sock_net(sk), UDP_MIB_SNDBUFERRORS);
1437 			err = 0;
1438 		}
1439 	} else {
1440 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_OUTDATAGRAMS);
1441 	}
1442 	return err;
1443 }
1444 
1445 static int udp_v6_push_pending_frames(struct sock *sk)
1446 {
1447 	struct sk_buff *skb;
1448 	struct udp_sock  *up = udp_sk(sk);
1449 	int err = 0;
1450 
1451 	if (up->pending == AF_INET)
1452 		return udp_push_pending_frames(sk);
1453 
1454 	skb = ip6_finish_skb(sk);
1455 	if (!skb)
1456 		goto out;
1457 
1458 	err = udp_v6_send_skb(skb, &inet_sk(sk)->cork.fl.u.ip6,
1459 			      &inet_sk(sk)->cork.base);
1460 out:
1461 	up->len = 0;
1462 	WRITE_ONCE(up->pending, 0);
1463 	return err;
1464 }
1465 
1466 int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1467 {
1468 	int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE;
1469 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1470 	struct ipv6_txoptions *opt_to_free = NULL;
1471 	struct in6_addr *daddr, *final_p, final;
1472 	struct ip6_flowlabel *flowlabel = NULL;
1473 	struct inet_sock *inet = inet_sk(sk);
1474 	struct ipv6_pinfo *np = inet6_sk(sk);
1475 	struct ipv6_txoptions *opt = NULL;
1476 	struct udp_sock *up = udp_sk(sk);
1477 	struct ipv6_txoptions opt_space;
1478 	int addr_len = msg->msg_namelen;
1479 	struct inet_cork_full cork;
1480 	struct ipcm6_cookie ipc6;
1481 	bool connected = false;
1482 	struct dst_entry *dst;
1483 	struct flowi6 *fl6;
1484 	int ulen = len;
1485 	int err;
1486 
1487 	fl6 = &cork.fl.u.ip6;
1488 	ipcm6_init_sk(&ipc6, sk);
1489 	ipc6.gso_size = READ_ONCE(up->gso_size);
1490 
1491 	/* destination address check */
1492 	if (sin6) {
1493 		if (addr_len < offsetof(struct sockaddr, sa_data))
1494 			return -EINVAL;
1495 
1496 		switch (sin6->sin6_family) {
1497 		case AF_INET6:
1498 			if (addr_len < SIN6_LEN_RFC2133)
1499 				return -EINVAL;
1500 			daddr = &sin6->sin6_addr;
1501 			if (ipv6_addr_any(daddr) &&
1502 			    ipv6_addr_v4mapped(&np->saddr))
1503 				ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1504 						       daddr);
1505 			break;
1506 		case AF_INET:
1507 			goto do_udp_sendmsg;
1508 		case AF_UNSPEC:
1509 			msg->msg_name = sin6 = NULL;
1510 			msg->msg_namelen = addr_len = 0;
1511 			daddr = NULL;
1512 			break;
1513 		default:
1514 			return -EINVAL;
1515 		}
1516 	} else if (!READ_ONCE(up->pending)) {
1517 		if (sk->sk_state != TCP_ESTABLISHED)
1518 			return -EDESTADDRREQ;
1519 		daddr = &sk->sk_v6_daddr;
1520 	} else
1521 		daddr = NULL;
1522 
1523 	if (daddr) {
1524 		if (ipv6_addr_v4mapped(daddr)) {
1525 			struct sockaddr_in sin;
1526 			sin.sin_family = AF_INET;
1527 			sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1528 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
1529 			msg->msg_name = &sin;
1530 			msg->msg_namelen = sizeof(sin);
1531 do_udp_sendmsg:
1532 			err = ipv6_only_sock(sk) ?
1533 				-ENETUNREACH : udp_sendmsg(sk, msg, len);
1534 			msg->msg_name = sin6;
1535 			msg->msg_namelen = addr_len;
1536 			return err;
1537 		}
1538 	}
1539 
1540 	/* Rough check on arithmetic overflow,
1541 	   better check is made in ip6_append_data().
1542 	   */
1543 	if (len > INT_MAX - sizeof(struct udphdr))
1544 		return -EMSGSIZE;
1545 
1546 	if (READ_ONCE(up->pending)) {
1547 		if (READ_ONCE(up->pending) == AF_INET)
1548 			return udp_sendmsg(sk, msg, len);
1549 		/*
1550 		 * There are pending frames.
1551 		 * The socket lock must be held while it's corked.
1552 		 */
1553 		lock_sock(sk);
1554 		if (likely(up->pending)) {
1555 			if (unlikely(up->pending != AF_INET6)) {
1556 				release_sock(sk);
1557 				return -EAFNOSUPPORT;
1558 			}
1559 			dst = NULL;
1560 			goto do_append_data;
1561 		}
1562 		release_sock(sk);
1563 	}
1564 	ulen += sizeof(struct udphdr);
1565 
1566 	memset(fl6, 0, sizeof(*fl6));
1567 
1568 	if (sin6) {
1569 		if (sin6->sin6_port == 0)
1570 			return -EINVAL;
1571 
1572 		fl6->fl6_dport = sin6->sin6_port;
1573 		daddr = &sin6->sin6_addr;
1574 
1575 		if (inet6_test_bit(SNDFLOW, sk)) {
1576 			fl6->flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1577 			if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) {
1578 				flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
1579 				if (IS_ERR(flowlabel))
1580 					return -EINVAL;
1581 			}
1582 		}
1583 
1584 		/*
1585 		 * Otherwise it will be difficult to maintain
1586 		 * sk->sk_dst_cache.
1587 		 */
1588 		if (sk->sk_state == TCP_ESTABLISHED &&
1589 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1590 			daddr = &sk->sk_v6_daddr;
1591 
1592 		if (addr_len >= sizeof(struct sockaddr_in6) &&
1593 		    sin6->sin6_scope_id &&
1594 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
1595 			fl6->flowi6_oif = sin6->sin6_scope_id;
1596 	} else {
1597 		if (sk->sk_state != TCP_ESTABLISHED)
1598 			return -EDESTADDRREQ;
1599 
1600 		fl6->fl6_dport = inet->inet_dport;
1601 		daddr = &sk->sk_v6_daddr;
1602 		fl6->flowlabel = np->flow_label;
1603 		connected = true;
1604 	}
1605 
1606 	if (!fl6->flowi6_oif)
1607 		fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
1608 
1609 	if (!fl6->flowi6_oif)
1610 		fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1611 
1612 	fl6->flowi6_uid = sk_uid(sk);
1613 
1614 	if (msg->msg_controllen) {
1615 		opt = &opt_space;
1616 		memset(opt, 0, sizeof(struct ipv6_txoptions));
1617 		opt->tot_len = sizeof(*opt);
1618 		ipc6.opt = opt;
1619 
1620 		err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
1621 		if (err > 0) {
1622 			err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, fl6,
1623 						    &ipc6);
1624 			connected = false;
1625 		}
1626 		if (err < 0) {
1627 			fl6_sock_release(flowlabel);
1628 			return err;
1629 		}
1630 		if ((fl6->flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1631 			flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
1632 			if (IS_ERR(flowlabel))
1633 				return -EINVAL;
1634 		}
1635 		if (!(opt->opt_nflen|opt->opt_flen))
1636 			opt = NULL;
1637 	}
1638 	if (!opt) {
1639 		opt = txopt_get(np);
1640 		opt_to_free = opt;
1641 	}
1642 	if (flowlabel)
1643 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
1644 	opt = ipv6_fixup_options(&opt_space, opt);
1645 	ipc6.opt = opt;
1646 
1647 	fl6->flowi6_proto = IPPROTO_UDP;
1648 	fl6->flowi6_mark = ipc6.sockc.mark;
1649 	fl6->daddr = *daddr;
1650 	if (ipv6_addr_any(&fl6->saddr) && !ipv6_addr_any(&np->saddr))
1651 		fl6->saddr = np->saddr;
1652 	fl6->fl6_sport = inet->inet_sport;
1653 
1654 	if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) {
1655 		err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
1656 					   (struct sockaddr *)sin6,
1657 					   &addr_len,
1658 					   &fl6->saddr);
1659 		if (err)
1660 			goto out_no_dst;
1661 		if (sin6) {
1662 			if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
1663 				/* BPF program rewrote IPv6-only by IPv4-mapped
1664 				 * IPv6. It's currently unsupported.
1665 				 */
1666 				err = -ENOTSUPP;
1667 				goto out_no_dst;
1668 			}
1669 			if (sin6->sin6_port == 0) {
1670 				/* BPF program set invalid port. Reject it. */
1671 				err = -EINVAL;
1672 				goto out_no_dst;
1673 			}
1674 			fl6->fl6_dport = sin6->sin6_port;
1675 			fl6->daddr = sin6->sin6_addr;
1676 		}
1677 	}
1678 
1679 	if (ipv6_addr_any(&fl6->daddr))
1680 		fl6->daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1681 
1682 	final_p = fl6_update_dst(fl6, opt, &final);
1683 	if (final_p)
1684 		connected = false;
1685 
1686 	if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) {
1687 		fl6->flowi6_oif = READ_ONCE(np->mcast_oif);
1688 		connected = false;
1689 	} else if (!fl6->flowi6_oif)
1690 		fl6->flowi6_oif = READ_ONCE(np->ucast_oif);
1691 
1692 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
1693 
1694 	fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel);
1695 
1696 	dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected);
1697 	if (IS_ERR(dst)) {
1698 		err = PTR_ERR(dst);
1699 		dst = NULL;
1700 		goto out;
1701 	}
1702 
1703 	if (ipc6.hlimit < 0)
1704 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, fl6, dst);
1705 
1706 	if (msg->msg_flags&MSG_CONFIRM)
1707 		goto do_confirm;
1708 back_from_confirm:
1709 
1710 	/* Lockless fast path for the non-corking case */
1711 	if (!corkreq) {
1712 		struct sk_buff *skb;
1713 
1714 		skb = ip6_make_skb(sk, ip_generic_getfrag, msg, ulen,
1715 				   sizeof(struct udphdr), &ipc6,
1716 				   dst_rt6_info(dst),
1717 				   msg->msg_flags, &cork);
1718 		err = PTR_ERR(skb);
1719 		if (!IS_ERR_OR_NULL(skb))
1720 			err = udp_v6_send_skb(skb, fl6, &cork.base);
1721 		/* ip6_make_skb steals dst reference */
1722 		goto out_no_dst;
1723 	}
1724 
1725 	lock_sock(sk);
1726 	if (unlikely(up->pending)) {
1727 		/* The socket is already corked while preparing it. */
1728 		/* ... which is an evident application bug. --ANK */
1729 		release_sock(sk);
1730 
1731 		net_dbg_ratelimited("udp cork app bug 2\n");
1732 		err = -EINVAL;
1733 		goto out;
1734 	}
1735 
1736 	WRITE_ONCE(up->pending, AF_INET6);
1737 
1738 do_append_data:
1739 	up->len += ulen;
1740 	err = ip6_append_data(sk, ip_generic_getfrag, msg, ulen,
1741 			      sizeof(struct udphdr), &ipc6, fl6,
1742 			      dst_rt6_info(dst),
1743 			      corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1744 	if (err)
1745 		udp_v6_flush_pending_frames(sk);
1746 	else if (!corkreq)
1747 		err = udp_v6_push_pending_frames(sk);
1748 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1749 		WRITE_ONCE(up->pending, 0);
1750 
1751 	if (err > 0)
1752 		err = inet6_test_bit(RECVERR6, sk) ? net_xmit_errno(err) : 0;
1753 	release_sock(sk);
1754 
1755 out:
1756 	dst_release(dst);
1757 out_no_dst:
1758 	fl6_sock_release(flowlabel);
1759 	txopt_put(opt_to_free);
1760 	if (!err)
1761 		return len;
1762 	/*
1763 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1764 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1765 	 * we don't have a good statistic (IpOutDiscards but it can be too many
1766 	 * things).  We could add another new stat but at least for now that
1767 	 * seems like overkill.
1768 	 */
1769 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
1770 		UDP6_INC_STATS(sock_net(sk), UDP_MIB_SNDBUFERRORS);
1771 
1772 	return err;
1773 
1774 do_confirm:
1775 	if (msg->msg_flags & MSG_PROBE)
1776 		dst_confirm_neigh(dst, &fl6->daddr);
1777 	if (!(msg->msg_flags&MSG_PROBE) || len)
1778 		goto back_from_confirm;
1779 	err = 0;
1780 	goto out;
1781 }
1782 EXPORT_SYMBOL(udpv6_sendmsg);
1783 
1784 static void udpv6_splice_eof(struct socket *sock)
1785 {
1786 	struct sock *sk = sock->sk;
1787 	struct udp_sock *up = udp_sk(sk);
1788 
1789 	if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk))
1790 		return;
1791 
1792 	lock_sock(sk);
1793 	if (up->pending && !udp_test_bit(CORK, sk))
1794 		udp_v6_push_pending_frames(sk);
1795 	release_sock(sk);
1796 }
1797 
1798 static void udpv6_destroy_sock(struct sock *sk)
1799 {
1800 	struct udp_sock *up = udp_sk(sk);
1801 	lock_sock(sk);
1802 
1803 	/* protects from races with udp_abort() */
1804 	sock_set_flag(sk, SOCK_DEAD);
1805 	udp_v6_flush_pending_frames(sk);
1806 	release_sock(sk);
1807 
1808 	if (static_branch_unlikely(&udpv6_encap_needed_key)) {
1809 		if (up->encap_type) {
1810 			void (*encap_destroy)(struct sock *sk);
1811 			encap_destroy = READ_ONCE(up->encap_destroy);
1812 			if (encap_destroy)
1813 				encap_destroy(sk);
1814 		}
1815 		if (udp_test_bit(ENCAP_ENABLED, sk)) {
1816 			static_branch_dec(&udpv6_encap_needed_key);
1817 			udp_encap_disable();
1818 			udp_tunnel_cleanup_gro(sk);
1819 		}
1820 	}
1821 }
1822 
1823 /*
1824  *	Socket option code for UDP
1825  */
1826 static int udpv6_setsockopt(struct sock *sk, int level, int optname,
1827 			    sockptr_t optval, unsigned int optlen)
1828 {
1829 	if (level == SOL_UDP || level == SOL_SOCKET)
1830 		return udp_lib_setsockopt(sk, level, optname,
1831 					  optval, optlen,
1832 					  udp_v6_push_pending_frames);
1833 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
1834 }
1835 
1836 static int udpv6_getsockopt(struct sock *sk, int level, int optname,
1837 			    char __user *optval, int __user *optlen)
1838 {
1839 	sockopt_t opt;
1840 	int err;
1841 
1842 	if (level != SOL_UDP)
1843 		return ipv6_getsockopt(sk, level, optname, optval, optlen);
1844 
1845 	err = sockopt_init_user(&opt, optval, optlen);
1846 	if (err)
1847 		return err;
1848 
1849 	err = udp_lib_getsockopt(sk, level, optname, &opt);
1850 	if (err)
1851 		return err;
1852 	if (put_user(opt.optlen, optlen))
1853 		return -EFAULT;
1854 	return 0;
1855 }
1856 
1857 
1858 /* ------------------------------------------------------------------------ */
1859 #ifdef CONFIG_PROC_FS
1860 static int udp6_seq_show(struct seq_file *seq, void *v)
1861 {
1862 	if (v == SEQ_START_TOKEN) {
1863 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1864 	} else {
1865 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1866 		const struct inet_sock *inet = inet_sk((const struct sock *)v);
1867 		__u16 srcp = ntohs(inet->inet_sport);
1868 		__u16 destp = ntohs(inet->inet_dport);
1869 		__ip6_dgram_sock_seq_show(seq, v, srcp, destp,
1870 					  udp_rqueue_get(v), bucket);
1871 	}
1872 	return 0;
1873 }
1874 
1875 static const struct seq_operations udp6_seq_ops = {
1876 	.start		= udp_seq_start,
1877 	.next		= udp_seq_next,
1878 	.stop		= udp_seq_stop,
1879 	.show		= udp6_seq_show,
1880 };
1881 
1882 static struct udp_seq_afinfo udp6_seq_afinfo = {
1883 	.family		= AF_INET6,
1884 };
1885 
1886 int __net_init udp6_proc_init(struct net *net)
1887 {
1888 	if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1889 			sizeof(struct udp_iter_state), &udp6_seq_afinfo))
1890 		return -ENOMEM;
1891 	return 0;
1892 }
1893 
1894 void udp6_proc_exit(struct net *net)
1895 {
1896 	remove_proc_entry("udp6", net->proc_net);
1897 }
1898 #endif /* CONFIG_PROC_FS */
1899 
1900 /* ------------------------------------------------------------------------ */
1901 
1902 struct proto udpv6_prot = {
1903 	.name			= "UDPv6",
1904 	.owner			= THIS_MODULE,
1905 	.close			= udp_lib_close,
1906 	.pre_connect		= udpv6_pre_connect,
1907 	.connect		= udpv6_connect,
1908 	.disconnect		= udp_disconnect,
1909 	.ioctl			= udp_ioctl,
1910 	.init			= udpv6_init_sock,
1911 	.destroy		= udpv6_destroy_sock,
1912 	.setsockopt		= udpv6_setsockopt,
1913 	.getsockopt		= udpv6_getsockopt,
1914 	.sendmsg		= udpv6_sendmsg,
1915 	.recvmsg		= udpv6_recvmsg,
1916 	.splice_eof		= udpv6_splice_eof,
1917 	.release_cb		= ip6_datagram_release_cb,
1918 	.hash			= udp_lib_hash,
1919 	.unhash			= udp_lib_unhash,
1920 	.rehash			= udp_v6_rehash,
1921 	.get_port		= udp_v6_get_port,
1922 	.put_port		= udp_lib_unhash,
1923 #ifdef CONFIG_BPF_SYSCALL
1924 	.psock_update_sk_prot	= udp_bpf_update_proto,
1925 #endif
1926 
1927 	.memory_allocated	= &net_aligned_data.udp_memory_allocated,
1928 	.per_cpu_fw_alloc	= &udp_memory_per_cpu_fw_alloc,
1929 
1930 	.sysctl_mem		= sysctl_udp_mem,
1931 	.sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
1932 	.sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1933 	.obj_size		= sizeof(struct udp6_sock),
1934 	.ipv6_pinfo_offset = offsetof(struct udp6_sock, inet6),
1935 	.diag_destroy		= udp_abort,
1936 };
1937 
1938 static struct inet_protosw udpv6_protosw = {
1939 	.type =      SOCK_DGRAM,
1940 	.protocol =  IPPROTO_UDP,
1941 	.prot =      &udpv6_prot,
1942 	.ops =       &inet6_dgram_ops,
1943 	.flags =     INET_PROTOSW_PERMANENT,
1944 };
1945 
1946 int __init udpv6_init(void)
1947 {
1948 	int ret;
1949 
1950 	net_hotdata.udpv6_protocol = (struct inet6_protocol) {
1951 		.handler     = udpv6_rcv,
1952 		.err_handler = udpv6_err,
1953 		.flags	     = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
1954 	};
1955 	ret = inet6_add_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
1956 	if (ret)
1957 		goto out;
1958 
1959 	ret = inet6_register_protosw(&udpv6_protosw);
1960 	if (ret)
1961 		goto out_udpv6_protocol;
1962 out:
1963 	return ret;
1964 
1965 out_udpv6_protocol:
1966 	inet6_del_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
1967 	goto out;
1968 }
1969 
1970 void udpv6_exit(void)
1971 {
1972 	inet6_unregister_protosw(&udpv6_protosw);
1973 	inet6_del_protocol(&net_hotdata.udpv6_protocol, IPPROTO_UDP);
1974 }
1975