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