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