xref: /linux/net/ipv6/af_inet6.c (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	PF_INET6 socket protocol family
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *
9  *	Adapted from linux/net/ipv4/af_inet.c
10  *
11  *	Fixes:
12  *	piggy, Karl Knutson	:	Socket protocol table
13  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
14  *	Arnaldo Melo		:	check proc_net_create return, cleanups
15  */
16 
17 #define pr_fmt(fmt) "IPv6: " fmt
18 
19 #include <linux/module.h>
20 #include <linux/capability.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/in.h>
25 #include <linux/kernel.h>
26 #include <linux/timer.h>
27 #include <linux/string.h>
28 #include <linux/sockios.h>
29 #include <linux/net.h>
30 #include <linux/fcntl.h>
31 #include <linux/mm.h>
32 #include <linux/interrupt.h>
33 #include <linux/proc_fs.h>
34 #include <linux/stat.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 
38 #include <linux/inet.h>
39 #include <linux/netdevice.h>
40 #include <linux/icmpv6.h>
41 
42 #include <net/ip.h>
43 #include <net/ipv6.h>
44 #include <net/udp.h>
45 #include <net/tcp.h>
46 #include <net/ping.h>
47 #include <net/protocol.h>
48 #include <net/inet_common.h>
49 #include <net/route.h>
50 #include <net/transp_v6.h>
51 #include <net/ip6_route.h>
52 #include <net/addrconf.h>
53 #include <net/ndisc.h>
54 #ifdef CONFIG_IPV6_TUNNEL
55 #include <net/ip6_tunnel.h>
56 #endif
57 #include <net/calipso.h>
58 #include <net/seg6.h>
59 #include <net/rpl.h>
60 #include <net/compat.h>
61 #include <net/xfrm.h>
62 #include <net/ioam6.h>
63 #include <net/rawv6.h>
64 #include <net/rps.h>
65 
66 #include <linux/uaccess.h>
67 #include <linux/mroute6.h>
68 
69 #include "ip6_offload.h"
70 
71 /* The inetsw6 table contains everything that inet6_create needs to
72  * build a new socket.
73  */
74 static struct list_head inetsw6[SOCK_MAX];
75 static DEFINE_SPINLOCK(inetsw6_lock);
76 
77 struct ipv6_params ipv6_defaults = {
78 	.disable_ipv6 = 0,
79 	.autoconf = 1,
80 };
81 
82 module_param_named(disable, disable_ipv6_mod, int, 0444);
83 MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
84 
85 module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
86 MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
87 
88 module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
89 MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
90 
91 static struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
92 {
93 	const int offset = sk->sk_prot->ipv6_pinfo_offset;
94 
95 	return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
96 }
97 
98 void inet6_sock_destruct(struct sock *sk)
99 {
100 	inet6_cleanup_sock(sk);
101 	inet_sock_destruct(sk);
102 }
103 EXPORT_SYMBOL_GPL(inet6_sock_destruct);
104 
105 static int inet6_create(struct net *net, struct socket *sock, int protocol,
106 			int kern)
107 {
108 	struct inet_sock *inet;
109 	struct ipv6_pinfo *np;
110 	struct sock *sk;
111 	struct inet_protosw *answer;
112 	struct proto *answer_prot;
113 	unsigned char answer_flags;
114 	int try_loading_module = 0;
115 	int err;
116 
117 	if (protocol < 0 || protocol >= IPPROTO_MAX)
118 		return -EINVAL;
119 
120 	/* Look for the requested type/protocol pair. */
121 lookup_protocol:
122 	err = -ESOCKTNOSUPPORT;
123 	rcu_read_lock();
124 	list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
125 
126 		err = 0;
127 		/* Check the non-wild match. */
128 		if (protocol == answer->protocol) {
129 			if (protocol != IPPROTO_IP)
130 				break;
131 		} else {
132 			/* Check for the two wild cases. */
133 			if (IPPROTO_IP == protocol) {
134 				protocol = answer->protocol;
135 				break;
136 			}
137 			if (IPPROTO_IP == answer->protocol)
138 				break;
139 		}
140 		err = -EPROTONOSUPPORT;
141 	}
142 
143 	if (err) {
144 		if (try_loading_module < 2) {
145 			rcu_read_unlock();
146 			/*
147 			 * Be more specific, e.g. net-pf-10-proto-132-type-1
148 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
149 			 */
150 			if (++try_loading_module == 1)
151 				request_module("net-pf-%d-proto-%d-type-%d",
152 						PF_INET6, protocol, sock->type);
153 			/*
154 			 * Fall back to generic, e.g. net-pf-10-proto-132
155 			 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
156 			 */
157 			else
158 				request_module("net-pf-%d-proto-%d",
159 						PF_INET6, protocol);
160 			goto lookup_protocol;
161 		} else
162 			goto out_rcu_unlock;
163 	}
164 
165 	err = -EPERM;
166 	if (sock->type == SOCK_RAW && !kern &&
167 	    !ns_capable(net->user_ns, CAP_NET_RAW))
168 		goto out_rcu_unlock;
169 
170 	sock->ops = answer->ops;
171 	answer_prot = answer->prot;
172 	answer_flags = answer->flags;
173 	rcu_read_unlock();
174 
175 	WARN_ON(!answer_prot->slab);
176 
177 	err = -ENOBUFS;
178 	sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern);
179 	if (!sk)
180 		goto out;
181 
182 	sock_init_data(sock, sk);
183 
184 	err = 0;
185 	if (INET_PROTOSW_REUSE & answer_flags)
186 		sk->sk_reuse = SK_CAN_REUSE;
187 
188 	if (INET_PROTOSW_ICSK & answer_flags)
189 		inet_init_csk_locks(sk);
190 
191 	inet = inet_sk(sk);
192 	inet_assign_bit(IS_ICSK, sk, INET_PROTOSW_ICSK & answer_flags);
193 
194 	if (SOCK_RAW == sock->type) {
195 		inet->inet_num = protocol;
196 		if (IPPROTO_RAW == protocol)
197 			inet_set_bit(HDRINCL, sk);
198 	}
199 
200 	sk->sk_destruct		= inet6_sock_destruct;
201 	sk->sk_family		= PF_INET6;
202 	sk->sk_protocol		= protocol;
203 
204 	sk->sk_backlog_rcv	= answer->prot->backlog_rcv;
205 
206 	inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
207 	np->hop_limit	= -1;
208 	np->mcast_hops	= IPV6_DEFAULT_MCASTHOPS;
209 	inet6_set_bit(MC6_LOOP, sk);
210 	inet6_set_bit(MC6_ALL, sk);
211 	np->pmtudisc	= IPV6_PMTUDISC_WANT;
212 	inet6_assign_bit(REPFLOW, sk, READ_ONCE(net->ipv6.sysctl.flowlabel_reflect) &
213 				      FLOWLABEL_REFLECT_ESTABLISHED);
214 	sk->sk_ipv6only	= READ_ONCE(net->ipv6.sysctl.bindv6only);
215 	sk->sk_txrehash = READ_ONCE(net->core.sysctl_txrehash);
216 
217 	/* Init the ipv4 part of the socket since we can have sockets
218 	 * using v6 API for ipv4.
219 	 */
220 	inet->uc_ttl	= -1;
221 
222 	inet_set_bit(MC_LOOP, sk);
223 	inet->mc_ttl	= 1;
224 	inet->mc_index	= 0;
225 	RCU_INIT_POINTER(inet->mc_list, NULL);
226 	inet->rcv_tos	= 0;
227 
228 	if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc))
229 		inet->pmtudisc = IP_PMTUDISC_DONT;
230 	else
231 		inet->pmtudisc = IP_PMTUDISC_WANT;
232 
233 	if (inet->inet_num) {
234 		/* It assumes that any protocol which allows
235 		 * the user to assign a number at socket
236 		 * creation time automatically shares.
237 		 */
238 		inet->inet_sport = htons(inet->inet_num);
239 		err = sk->sk_prot->hash(sk);
240 		if (err)
241 			goto out_sk_release;
242 	}
243 	if (sk->sk_prot->init) {
244 		err = sk->sk_prot->init(sk);
245 		if (err)
246 			goto out_sk_release;
247 	}
248 
249 	if (!kern) {
250 		err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
251 		if (err)
252 			goto out_sk_release;
253 	}
254 out:
255 	return err;
256 out_rcu_unlock:
257 	rcu_read_unlock();
258 	goto out;
259 out_sk_release:
260 	sk_common_release(sk);
261 	sock->sk = NULL;
262 	goto out;
263 }
264 
265 int __inet6_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
266 		 u32 flags)
267 {
268 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
269 	struct inet_sock *inet = inet_sk(sk);
270 	struct ipv6_pinfo *np = inet6_sk(sk);
271 	struct net *net = sock_net(sk);
272 	__be32 v4addr = 0;
273 	unsigned short snum;
274 	bool saved_ipv6only;
275 	int addr_type = 0;
276 	int err = 0;
277 
278 	if (addr->sin6_family != AF_INET6)
279 		return -EAFNOSUPPORT;
280 
281 	addr_type = ipv6_addr_type(&addr->sin6_addr);
282 	if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
283 		return -EINVAL;
284 
285 	snum = ntohs(addr->sin6_port);
286 	if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) &&
287 	    snum && inet_port_requires_bind_service(net, snum) &&
288 	    !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
289 		return -EACCES;
290 
291 	if (flags & BIND_WITH_LOCK)
292 		lock_sock(sk);
293 
294 	/* Check these errors (active socket, double bind). */
295 	if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
296 		err = -EINVAL;
297 		goto out;
298 	}
299 
300 	/* Check if the address belongs to the host. */
301 	if (addr_type == IPV6_ADDR_MAPPED) {
302 		struct net_device *dev = NULL;
303 		int chk_addr_ret;
304 
305 		/* Binding to v4-mapped address on a v6-only socket
306 		 * makes no sense
307 		 */
308 		if (ipv6_only_sock(sk)) {
309 			err = -EINVAL;
310 			goto out;
311 		}
312 
313 		rcu_read_lock();
314 		if (sk->sk_bound_dev_if) {
315 			dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
316 			if (!dev) {
317 				err = -ENODEV;
318 				goto out_unlock;
319 			}
320 		}
321 
322 		/* Reproduce AF_INET checks to make the bindings consistent */
323 		v4addr = addr->sin6_addr.s6_addr32[3];
324 		chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
325 		rcu_read_unlock();
326 
327 		if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,
328 						 chk_addr_ret)) {
329 			err = -EADDRNOTAVAIL;
330 			goto out;
331 		}
332 	} else {
333 		if (addr_type != IPV6_ADDR_ANY) {
334 			struct net_device *dev = NULL;
335 
336 			rcu_read_lock();
337 			if (__ipv6_addr_needs_scope_id(addr_type)) {
338 				if (addr_len >= sizeof(struct sockaddr_in6) &&
339 				    addr->sin6_scope_id) {
340 					/* Override any existing binding, if another one
341 					 * is supplied by user.
342 					 */
343 					sk->sk_bound_dev_if = addr->sin6_scope_id;
344 				}
345 
346 				/* Binding to link-local address requires an interface */
347 				if (!sk->sk_bound_dev_if) {
348 					err = -EINVAL;
349 					goto out_unlock;
350 				}
351 			}
352 
353 			if (sk->sk_bound_dev_if) {
354 				dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
355 				if (!dev) {
356 					err = -ENODEV;
357 					goto out_unlock;
358 				}
359 			}
360 
361 			/* ipv4 addr of the socket is invalid.  Only the
362 			 * unspecified and mapped address have a v4 equivalent.
363 			 */
364 			v4addr = LOOPBACK4_IPV6;
365 			if (!(addr_type & IPV6_ADDR_MULTICAST))	{
366 				if (!ipv6_can_nonlocal_bind(net, inet) &&
367 				    !ipv6_chk_addr(net, &addr->sin6_addr,
368 						   dev, 0)) {
369 					err = -EADDRNOTAVAIL;
370 					goto out_unlock;
371 				}
372 			}
373 			rcu_read_unlock();
374 		}
375 	}
376 
377 	inet->inet_rcv_saddr = v4addr;
378 	inet->inet_saddr = v4addr;
379 
380 	sk->sk_v6_rcv_saddr = addr->sin6_addr;
381 
382 	if (!(addr_type & IPV6_ADDR_MULTICAST))
383 		np->saddr = addr->sin6_addr;
384 
385 	saved_ipv6only = sk->sk_ipv6only;
386 	if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED)
387 		sk->sk_ipv6only = 1;
388 
389 	/* Make sure we are allowed to bind here. */
390 	if (snum || !(inet_test_bit(BIND_ADDRESS_NO_PORT, sk) ||
391 		      (flags & BIND_FORCE_ADDRESS_NO_PORT))) {
392 		err = sk->sk_prot->get_port(sk, snum);
393 		if (err) {
394 			sk->sk_ipv6only = saved_ipv6only;
395 			inet_reset_saddr(sk);
396 			goto out;
397 		}
398 		if (!(flags & BIND_FROM_BPF)) {
399 			err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk);
400 			if (err) {
401 				sk->sk_ipv6only = saved_ipv6only;
402 				inet_reset_saddr(sk);
403 				if (sk->sk_prot->put_port)
404 					sk->sk_prot->put_port(sk);
405 				goto out;
406 			}
407 		}
408 	}
409 
410 	if (addr_type != IPV6_ADDR_ANY)
411 		sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
412 	if (snum)
413 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
414 	inet->inet_sport = htons(inet->inet_num);
415 	inet->inet_dport = 0;
416 	inet->inet_daddr = 0;
417 out:
418 	if (flags & BIND_WITH_LOCK)
419 		release_sock(sk);
420 	return err;
421 out_unlock:
422 	rcu_read_unlock();
423 	goto out;
424 }
425 
426 int inet6_bind_sk(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
427 {
428 	u32 flags = BIND_WITH_LOCK;
429 	const struct proto *prot;
430 	int err = 0;
431 
432 	prot = READ_ONCE(sk->sk_prot);
433 	/* If the socket has its own bind function then use it. */
434 	if (prot->bind)
435 		return prot->bind(sk, uaddr, addr_len);
436 
437 	if (addr_len < SIN6_LEN_RFC2133)
438 		return -EINVAL;
439 
440 	/* BPF prog is run before any checks are done so that if the prog
441 	 * changes context in a wrong way it will be caught.
442 	 */
443 	err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, &addr_len,
444 						 CGROUP_INET6_BIND, &flags);
445 	if (err)
446 		return err;
447 
448 	return __inet6_bind(sk, uaddr, addr_len, flags);
449 }
450 
451 /* bind for INET6 API */
452 int inet6_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
453 {
454 	return inet6_bind_sk(sock->sk, uaddr, addr_len);
455 }
456 EXPORT_SYMBOL(inet6_bind);
457 
458 int inet6_release(struct socket *sock)
459 {
460 	struct sock *sk = sock->sk;
461 
462 	if (!sk)
463 		return -EINVAL;
464 
465 	/* Free mc lists */
466 	ipv6_sock_mc_close(sk);
467 
468 	/* Free ac lists */
469 	ipv6_sock_ac_close(sk);
470 
471 	return inet_release(sock);
472 }
473 EXPORT_SYMBOL(inet6_release);
474 
475 void inet6_cleanup_sock(struct sock *sk)
476 {
477 	struct ipv6_pinfo *np = inet6_sk(sk);
478 	struct sk_buff *skb;
479 	struct ipv6_txoptions *opt;
480 
481 	/* Release rx options */
482 
483 	skb = xchg(&np->pktoptions, NULL);
484 	kfree_skb(skb);
485 
486 	skb = xchg(&np->rxpmtu, NULL);
487 	kfree_skb(skb);
488 
489 	/* Free flowlabels */
490 	fl6_free_socklist(sk);
491 
492 	/* Free tx options */
493 
494 	opt = unrcu_pointer(xchg(&np->opt, NULL));
495 	if (opt) {
496 		atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
497 		txopt_put(opt);
498 	}
499 }
500 
501 /*
502  *	This does both peername and sockname.
503  */
504 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
505 		  int peer)
506 {
507 	struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
508 	int sin_addr_len = sizeof(*sin);
509 	struct sock *sk = sock->sk;
510 	struct inet_sock *inet = inet_sk(sk);
511 	struct ipv6_pinfo *np = inet6_sk(sk);
512 
513 	sin->sin6_family = AF_INET6;
514 	sin->sin6_flowinfo = 0;
515 	sin->sin6_scope_id = 0;
516 	lock_sock(sk);
517 	if (peer) {
518 		if (!inet->inet_dport ||
519 		    (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
520 		    peer == 1)) {
521 			release_sock(sk);
522 			return -ENOTCONN;
523 		}
524 		sin->sin6_port = inet->inet_dport;
525 		sin->sin6_addr = sk->sk_v6_daddr;
526 		if (inet6_test_bit(SNDFLOW, sk))
527 			sin->sin6_flowinfo = np->flow_label;
528 		BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, &sin_addr_len,
529 				       CGROUP_INET6_GETPEERNAME);
530 	} else {
531 		if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
532 			sin->sin6_addr = np->saddr;
533 		else
534 			sin->sin6_addr = sk->sk_v6_rcv_saddr;
535 		sin->sin6_port = inet->inet_sport;
536 		BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, &sin_addr_len,
537 				       CGROUP_INET6_GETSOCKNAME);
538 	}
539 	sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
540 						 sk->sk_bound_dev_if);
541 	release_sock(sk);
542 	return sin_addr_len;
543 }
544 EXPORT_SYMBOL(inet6_getname);
545 
546 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
547 {
548 	void __user *argp = (void __user *)arg;
549 	struct sock *sk = sock->sk;
550 	struct net *net = sock_net(sk);
551 	const struct proto *prot;
552 
553 	switch (cmd) {
554 	case SIOCADDRT:
555 	case SIOCDELRT: {
556 		struct in6_rtmsg rtmsg;
557 
558 		if (copy_from_user(&rtmsg, argp, sizeof(rtmsg)))
559 			return -EFAULT;
560 		return ipv6_route_ioctl(net, cmd, &rtmsg);
561 	}
562 	case SIOCSIFADDR:
563 		return addrconf_add_ifaddr(net, argp);
564 	case SIOCDIFADDR:
565 		return addrconf_del_ifaddr(net, argp);
566 	case SIOCSIFDSTADDR:
567 		return addrconf_set_dstaddr(net, argp);
568 	default:
569 		prot = READ_ONCE(sk->sk_prot);
570 		if (!prot->ioctl)
571 			return -ENOIOCTLCMD;
572 		return sk_ioctl(sk, cmd, (void __user *)arg);
573 	}
574 	/*NOTREACHED*/
575 	return 0;
576 }
577 EXPORT_SYMBOL(inet6_ioctl);
578 
579 #ifdef CONFIG_COMPAT
580 struct compat_in6_rtmsg {
581 	struct in6_addr		rtmsg_dst;
582 	struct in6_addr		rtmsg_src;
583 	struct in6_addr		rtmsg_gateway;
584 	u32			rtmsg_type;
585 	u16			rtmsg_dst_len;
586 	u16			rtmsg_src_len;
587 	u32			rtmsg_metric;
588 	u32			rtmsg_info;
589 	u32			rtmsg_flags;
590 	s32			rtmsg_ifindex;
591 };
592 
593 static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
594 		struct compat_in6_rtmsg __user *ur)
595 {
596 	struct in6_rtmsg rt;
597 
598 	if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst,
599 			3 * sizeof(struct in6_addr)) ||
600 	    get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
601 	    get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) ||
602 	    get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) ||
603 	    get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
604 	    get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
605 	    get_user(rt.rtmsg_flags, &ur->rtmsg_flags) ||
606 	    get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex))
607 		return -EFAULT;
608 
609 
610 	return ipv6_route_ioctl(sock_net(sk), cmd, &rt);
611 }
612 
613 int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
614 {
615 	void __user *argp = compat_ptr(arg);
616 	struct sock *sk = sock->sk;
617 
618 	switch (cmd) {
619 	case SIOCADDRT:
620 	case SIOCDELRT:
621 		return inet6_compat_routing_ioctl(sk, cmd, argp);
622 	default:
623 		return -ENOIOCTLCMD;
624 	}
625 }
626 EXPORT_SYMBOL_GPL(inet6_compat_ioctl);
627 #endif /* CONFIG_COMPAT */
628 
629 int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
630 {
631 	struct sock *sk = sock->sk;
632 	const struct proto *prot;
633 
634 	if (unlikely(inet_send_prepare(sk)))
635 		return -EAGAIN;
636 
637 	prot = READ_ONCE(sk->sk_prot);
638 	return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
639 			       sk, msg, size);
640 }
641 
642 int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
643 		  int flags)
644 {
645 	struct sock *sk = sock->sk;
646 	const struct proto *prot;
647 
648 	if (likely(!(flags & MSG_ERRQUEUE)))
649 		sock_rps_record_flow(sk);
650 
651 	prot = READ_ONCE(sk->sk_prot);
652 	return INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
653 			       sk, msg, size, flags);
654 }
655 
656 const struct proto_ops inet6_stream_ops = {
657 	.family		   = PF_INET6,
658 	.owner		   = THIS_MODULE,
659 	.release	   = inet6_release,
660 	.bind		   = inet6_bind,
661 	.connect	   = inet_stream_connect,	/* ok		*/
662 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
663 	.accept		   = inet_accept,		/* ok		*/
664 	.getname	   = inet6_getname,
665 	.poll		   = tcp_poll,			/* ok		*/
666 	.ioctl		   = inet6_ioctl,		/* must change  */
667 	.gettstamp	   = sock_gettstamp,
668 	.listen		   = inet_listen,		/* ok		*/
669 	.shutdown	   = inet_shutdown,		/* ok		*/
670 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
671 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
672 	.sendmsg	   = inet6_sendmsg,		/* retpoline's sake */
673 	.recvmsg	   = inet6_recvmsg,		/* retpoline's sake */
674 #ifdef CONFIG_MMU
675 	.mmap		   = tcp_mmap,
676 #endif
677 	.splice_eof	   = inet_splice_eof,
678 	.sendmsg_locked    = tcp_sendmsg_locked,
679 	.splice_read	   = tcp_splice_read,
680 	.set_peek_off      = sk_set_peek_off,
681 	.read_sock	   = tcp_read_sock,
682 	.read_skb	   = tcp_read_skb,
683 	.peek_len	   = tcp_peek_len,
684 #ifdef CONFIG_COMPAT
685 	.compat_ioctl	   = inet6_compat_ioctl,
686 #endif
687 	.set_rcvlowat	   = tcp_set_rcvlowat,
688 	.set_rcvbuf	   = tcp_set_rcvbuf,
689 };
690 EXPORT_SYMBOL_GPL(inet6_stream_ops);
691 
692 const struct proto_ops inet6_dgram_ops = {
693 	.family		   = PF_INET6,
694 	.owner		   = THIS_MODULE,
695 	.release	   = inet6_release,
696 	.bind		   = inet6_bind,
697 	.connect	   = inet_dgram_connect,	/* ok		*/
698 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
699 	.accept		   = sock_no_accept,		/* a do nothing	*/
700 	.getname	   = inet6_getname,
701 	.poll		   = udp_poll,			/* ok		*/
702 	.ioctl		   = inet6_ioctl,		/* must change  */
703 	.gettstamp	   = sock_gettstamp,
704 	.listen		   = sock_no_listen,		/* ok		*/
705 	.shutdown	   = inet_shutdown,		/* ok		*/
706 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
707 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
708 	.sendmsg	   = inet6_sendmsg,		/* retpoline's sake */
709 	.recvmsg	   = inet6_recvmsg,		/* retpoline's sake */
710 	.read_skb	   = udp_read_skb,
711 	.mmap		   = sock_no_mmap,
712 	.set_peek_off	   = udp_set_peek_off,
713 #ifdef CONFIG_COMPAT
714 	.compat_ioctl	   = inet6_compat_ioctl,
715 #endif
716 };
717 
718 static const struct net_proto_family inet6_family_ops = {
719 	.family = PF_INET6,
720 	.create = inet6_create,
721 	.owner	= THIS_MODULE,
722 };
723 
724 int inet6_register_protosw(struct inet_protosw *p)
725 {
726 	struct list_head *lh;
727 	struct inet_protosw *answer;
728 	struct list_head *last_perm;
729 	int protocol = p->protocol;
730 	int ret;
731 
732 	spin_lock_bh(&inetsw6_lock);
733 
734 	ret = -EINVAL;
735 	if (p->type >= SOCK_MAX)
736 		goto out_illegal;
737 
738 	/* If we are trying to override a permanent protocol, bail. */
739 	answer = NULL;
740 	ret = -EPERM;
741 	last_perm = &inetsw6[p->type];
742 	list_for_each(lh, &inetsw6[p->type]) {
743 		answer = list_entry(lh, struct inet_protosw, list);
744 
745 		/* Check only the non-wild match. */
746 		if (INET_PROTOSW_PERMANENT & answer->flags) {
747 			if (protocol == answer->protocol)
748 				break;
749 			last_perm = lh;
750 		}
751 
752 		answer = NULL;
753 	}
754 	if (answer)
755 		goto out_permanent;
756 
757 	/* Add the new entry after the last permanent entry if any, so that
758 	 * the new entry does not override a permanent entry when matched with
759 	 * a wild-card protocol. But it is allowed to override any existing
760 	 * non-permanent entry.  This means that when we remove this entry, the
761 	 * system automatically returns to the old behavior.
762 	 */
763 	list_add_rcu(&p->list, last_perm);
764 	ret = 0;
765 out:
766 	spin_unlock_bh(&inetsw6_lock);
767 	return ret;
768 
769 out_permanent:
770 	pr_err("Attempt to override permanent protocol %d\n", protocol);
771 	goto out;
772 
773 out_illegal:
774 	pr_err("Ignoring attempt to register invalid socket type %d\n",
775 	       p->type);
776 	goto out;
777 }
778 EXPORT_SYMBOL(inet6_register_protosw);
779 
780 void
781 inet6_unregister_protosw(struct inet_protosw *p)
782 {
783 	if (INET_PROTOSW_PERMANENT & p->flags) {
784 		pr_err("Attempt to unregister permanent protocol %d\n",
785 		       p->protocol);
786 	} else {
787 		spin_lock_bh(&inetsw6_lock);
788 		list_del_rcu(&p->list);
789 		spin_unlock_bh(&inetsw6_lock);
790 
791 		synchronize_net();
792 	}
793 }
794 EXPORT_SYMBOL(inet6_unregister_protosw);
795 
796 int inet6_sk_rebuild_header(struct sock *sk)
797 {
798 	struct ipv6_pinfo *np = inet6_sk(sk);
799 	struct inet_sock *inet = inet_sk(sk);
800 	struct in6_addr *final_p;
801 	struct dst_entry *dst;
802 	struct flowi6 *fl6;
803 
804 	dst = __sk_dst_check(sk, np->dst_cookie);
805 	if (dst)
806 		return 0;
807 
808 	fl6 = &inet->cork.fl.u.ip6;
809 	memset(fl6, 0, sizeof(*fl6));
810 	fl6->flowi6_proto = sk->sk_protocol;
811 	fl6->daddr = sk->sk_v6_daddr;
812 	fl6->saddr = np->saddr;
813 	fl6->flowlabel = np->flow_label;
814 	fl6->flowi6_oif = sk->sk_bound_dev_if;
815 	fl6->flowi6_mark = sk->sk_mark;
816 	fl6->fl6_dport = inet->inet_dport;
817 	fl6->fl6_sport = inet->inet_sport;
818 	fl6->flowi6_uid = sk_uid(sk);
819 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
820 
821 	ip6_ecmp_set_mp_hash(sock_net(sk), fl6, sk->sk_txhash);
822 
823 	rcu_read_lock();
824 	final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &np->final);
825 	rcu_read_unlock();
826 
827 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
828 	if (IS_ERR(dst)) {
829 		sk->sk_route_caps = 0;
830 		WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst));
831 		return PTR_ERR(dst);
832 	}
833 
834 	ip6_dst_store(sk, dst, false, false);
835 	return 0;
836 }
837 
838 bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
839 		       const struct inet6_skb_parm *opt)
840 {
841 	const struct ipv6_pinfo *np = inet6_sk(sk);
842 
843 	if (np->rxopt.all) {
844 		if (((opt->flags & IP6SKB_HOPBYHOP) &&
845 		     (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
846 		    (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
847 		     np->rxopt.bits.rxflow) ||
848 		    (opt->srcrt && (np->rxopt.bits.srcrt ||
849 		     np->rxopt.bits.osrcrt)) ||
850 		    ((opt->dst1 || opt->dst0) &&
851 		     (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
852 			return true;
853 	}
854 	return false;
855 }
856 
857 static struct packet_type ipv6_packet_type __read_mostly = {
858 	.type = cpu_to_be16(ETH_P_IPV6),
859 	.func = ipv6_rcv,
860 	.list_func = ipv6_list_rcv,
861 };
862 
863 static int __init ipv6_packet_init(void)
864 {
865 	dev_add_pack(&ipv6_packet_type);
866 	return 0;
867 }
868 
869 static void ipv6_packet_cleanup(void)
870 {
871 	dev_remove_pack(&ipv6_packet_type);
872 }
873 
874 static int __net_init ipv6_init_mibs(struct net *net)
875 {
876 	int i;
877 
878 	net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
879 	if (!net->mib.udp_stats_in6)
880 		return -ENOMEM;
881 
882 	net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
883 	if (!net->mib.ipv6_statistics)
884 		goto err_ip_mib;
885 
886 	for_each_possible_cpu(i) {
887 		struct ipstats_mib *af_inet6_stats;
888 		af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
889 		u64_stats_init(&af_inet6_stats->syncp);
890 	}
891 
892 	net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
893 	if (!net->mib.icmpv6_statistics)
894 		goto err_icmp_mib;
895 
896 	net->mib.icmpv6msg_statistics = kzalloc_obj(struct icmpv6msg_mib);
897 	if (!net->mib.icmpv6msg_statistics)
898 		goto err_icmpmsg_mib;
899 	return 0;
900 
901 err_icmpmsg_mib:
902 	free_percpu(net->mib.icmpv6_statistics);
903 err_icmp_mib:
904 	free_percpu(net->mib.ipv6_statistics);
905 err_ip_mib:
906 	free_percpu(net->mib.udp_stats_in6);
907 	return -ENOMEM;
908 }
909 
910 static void ipv6_cleanup_mibs(struct net *net)
911 {
912 	free_percpu(net->mib.udp_stats_in6);
913 	free_percpu(net->mib.ipv6_statistics);
914 	free_percpu(net->mib.icmpv6_statistics);
915 	kfree(net->mib.icmpv6msg_statistics);
916 }
917 
918 static int __net_init inet6_net_init(struct net *net)
919 {
920 	int err = 0;
921 
922 	net->ipv6.sysctl.bindv6only = 0;
923 	net->ipv6.sysctl.icmpv6_time = HZ / 10;
924 	net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
925 	net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
926 	net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
927 	net->ipv6.sysctl.icmpv6_error_anycast_as_unicast = 0;
928 	net->ipv6.sysctl.icmpv6_errors_extension_mask = 0;
929 
930 	/* By default, rate limit error messages.
931 	 * Except for pmtu discovery, it would break it.
932 	 * proc_do_large_bitmap needs pointer to the bitmap.
933 	 */
934 	bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
935 	bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
936 	net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
937 
938 	net->ipv6.sysctl.flowlabel_consistency = 1;
939 	net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
940 	net->ipv6.sysctl.idgen_retries = 3;
941 	net->ipv6.sysctl.idgen_delay = 1 * HZ;
942 	net->ipv6.sysctl.flowlabel_state_ranges = 0;
943 	net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
944 	net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
945 	net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
946 	net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
947 	net->ipv6.sysctl.fib_notify_on_flag_change = 0;
948 	atomic_set(&net->ipv6.fib6_sernum, 1);
949 
950 	net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID;
951 	net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE;
952 
953 	err = ipv6_init_mibs(net);
954 	if (err)
955 		return err;
956 #ifdef CONFIG_PROC_FS
957 	err = udp6_proc_init(net);
958 	if (err)
959 		goto out;
960 	err = tcp6_proc_init(net);
961 	if (err)
962 		goto proc_tcp6_fail;
963 	err = ac6_proc_init(net);
964 	if (err)
965 		goto proc_ac6_fail;
966 #endif
967 	return err;
968 
969 #ifdef CONFIG_PROC_FS
970 proc_ac6_fail:
971 	tcp6_proc_exit(net);
972 proc_tcp6_fail:
973 	udp6_proc_exit(net);
974 out:
975 	ipv6_cleanup_mibs(net);
976 	return err;
977 #endif
978 }
979 
980 static void __net_exit inet6_net_exit(struct net *net)
981 {
982 #ifdef CONFIG_PROC_FS
983 	udp6_proc_exit(net);
984 	tcp6_proc_exit(net);
985 	ac6_proc_exit(net);
986 #endif
987 	ipv6_cleanup_mibs(net);
988 }
989 
990 static struct pernet_operations inet6_net_ops = {
991 	.init = inet6_net_init,
992 	.exit = inet6_net_exit,
993 };
994 
995 static int __init inet6_init(void)
996 {
997 	struct list_head *r;
998 	int err = 0;
999 
1000 	sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
1001 
1002 	/* Register the socket-side information for inet6_create.  */
1003 	for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1004 		INIT_LIST_HEAD(r);
1005 
1006 	raw_hashinfo_init(&raw_v6_hashinfo);
1007 
1008 	if (disable_ipv6_mod) {
1009 		pr_info("Loaded, but administratively disabled, reboot required to enable\n");
1010 		goto out;
1011 	}
1012 
1013 	err = proto_register(&tcpv6_prot, 1);
1014 	if (err)
1015 		goto out;
1016 
1017 	err = proto_register(&udpv6_prot, 1);
1018 	if (err)
1019 		goto out_unregister_tcp_proto;
1020 
1021 	err = proto_register(&rawv6_prot, 1);
1022 	if (err)
1023 		goto out_unregister_udp_proto;
1024 
1025 	err = proto_register(&pingv6_prot, 1);
1026 	if (err)
1027 		goto out_unregister_raw_proto;
1028 
1029 	/* We MUST register RAW sockets before we create the ICMP6,
1030 	 * IGMP6, or NDISC control sockets.
1031 	 */
1032 	err = rawv6_init();
1033 	if (err)
1034 		goto out_unregister_ping_proto;
1035 
1036 	/* Register the family here so that the init calls below will
1037 	 * be able to create sockets. (?? is this dangerous ??)
1038 	 */
1039 	err = sock_register(&inet6_family_ops);
1040 	if (err)
1041 		goto out_sock_register_fail;
1042 
1043 	/*
1044 	 *	ipngwg API draft makes clear that the correct semantics
1045 	 *	for TCP and UDP is to consider one TCP and UDP instance
1046 	 *	in a host available by both INET and INET6 APIs and
1047 	 *	able to communicate via both network protocols.
1048 	 */
1049 
1050 	err = register_pernet_subsys(&inet6_net_ops);
1051 	if (err)
1052 		goto register_pernet_fail;
1053 	err = ip6_mr_init();
1054 	if (err)
1055 		goto ipmr_fail;
1056 	err = icmpv6_init();
1057 	if (err)
1058 		goto icmp_fail;
1059 	err = ndisc_init();
1060 	if (err)
1061 		goto ndisc_fail;
1062 	err = igmp6_init();
1063 	if (err)
1064 		goto igmp_fail;
1065 
1066 	/* Create /proc/foo6 entries. */
1067 #ifdef CONFIG_PROC_FS
1068 	err = -ENOMEM;
1069 	if (raw6_proc_init())
1070 		goto proc_raw6_fail;
1071 	if (ipv6_misc_proc_init())
1072 		goto proc_misc6_fail;
1073 	if (if6_proc_init())
1074 		goto proc_if6_fail;
1075 #endif
1076 	err = ip6_route_init();
1077 	if (err)
1078 		goto ip6_route_fail;
1079 	err = ndisc_late_init();
1080 	if (err)
1081 		goto ndisc_late_fail;
1082 	err = ip6_flowlabel_init();
1083 	if (err)
1084 		goto ip6_flowlabel_fail;
1085 	err = ipv6_anycast_init();
1086 	if (err)
1087 		goto ipv6_anycast_fail;
1088 	err = addrconf_init();
1089 	if (err)
1090 		goto addrconf_fail;
1091 
1092 	/* Init v6 extension headers. */
1093 	err = ipv6_exthdrs_init();
1094 	if (err)
1095 		goto ipv6_exthdrs_fail;
1096 
1097 	err = ipv6_frag_init();
1098 	if (err)
1099 		goto ipv6_frag_fail;
1100 
1101 	/* Init v6 transport protocols. */
1102 	err = udpv6_init();
1103 	if (err)
1104 		goto udpv6_fail;
1105 
1106 	err = udpv6_offload_init();
1107 	if (err)
1108 		goto udpv6_offload_fail;
1109 
1110 	err = tcpv6_init();
1111 	if (err)
1112 		goto tcpv6_fail;
1113 
1114 	err = ipv6_packet_init();
1115 	if (err)
1116 		goto ipv6_packet_fail;
1117 
1118 	err = pingv6_init();
1119 	if (err)
1120 		goto pingv6_fail;
1121 
1122 	err = calipso_init();
1123 	if (err)
1124 		goto calipso_fail;
1125 
1126 	err = seg6_init();
1127 	if (err)
1128 		goto seg6_fail;
1129 
1130 	err = rpl_init();
1131 	if (err)
1132 		goto rpl_fail;
1133 
1134 	err = ioam6_init();
1135 	if (err)
1136 		goto ioam6_fail;
1137 
1138 	err = igmp6_late_init();
1139 	if (err)
1140 		goto igmp6_late_err;
1141 
1142 #ifdef CONFIG_SYSCTL
1143 	err = ipv6_sysctl_register();
1144 	if (err)
1145 		goto sysctl_fail;
1146 #endif
1147 
1148 out:
1149 	return err;
1150 
1151 #ifdef CONFIG_SYSCTL
1152 sysctl_fail:
1153 	igmp6_late_cleanup();
1154 #endif
1155 igmp6_late_err:
1156 	ioam6_exit();
1157 ioam6_fail:
1158 	rpl_exit();
1159 rpl_fail:
1160 	seg6_exit();
1161 seg6_fail:
1162 	calipso_exit();
1163 calipso_fail:
1164 	pingv6_exit();
1165 pingv6_fail:
1166 	ipv6_packet_cleanup();
1167 ipv6_packet_fail:
1168 	tcpv6_exit();
1169 tcpv6_fail:
1170 	udpv6_offload_exit();
1171 udpv6_offload_fail:
1172 	udpv6_exit();
1173 udpv6_fail:
1174 	ipv6_frag_exit();
1175 ipv6_frag_fail:
1176 	ipv6_exthdrs_exit();
1177 ipv6_exthdrs_fail:
1178 	addrconf_cleanup();
1179 addrconf_fail:
1180 	ipv6_anycast_cleanup();
1181 ipv6_anycast_fail:
1182 	ip6_flowlabel_cleanup();
1183 ip6_flowlabel_fail:
1184 	ndisc_late_cleanup();
1185 ndisc_late_fail:
1186 	ip6_route_cleanup();
1187 ip6_route_fail:
1188 #ifdef CONFIG_PROC_FS
1189 	if6_proc_exit();
1190 proc_if6_fail:
1191 	ipv6_misc_proc_exit();
1192 proc_misc6_fail:
1193 	raw6_proc_exit();
1194 proc_raw6_fail:
1195 #endif
1196 	igmp6_cleanup();
1197 igmp_fail:
1198 	ndisc_cleanup();
1199 ndisc_fail:
1200 	icmpv6_cleanup();
1201 icmp_fail:
1202 	ip6_mr_cleanup();
1203 ipmr_fail:
1204 	unregister_pernet_subsys(&inet6_net_ops);
1205 register_pernet_fail:
1206 	sock_unregister(PF_INET6);
1207 	rtnl_unregister_all(PF_INET6);
1208 out_sock_register_fail:
1209 	rawv6_exit();
1210 out_unregister_ping_proto:
1211 	proto_unregister(&pingv6_prot);
1212 out_unregister_raw_proto:
1213 	proto_unregister(&rawv6_prot);
1214 out_unregister_udp_proto:
1215 	proto_unregister(&udpv6_prot);
1216 out_unregister_tcp_proto:
1217 	proto_unregister(&tcpv6_prot);
1218 	goto out;
1219 }
1220 fs_initcall_sync(inet6_init);
1221