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