projects/linux·blob master · refs

1224 lines · 29.0 KiB

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 <[email protected]>
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 */
74static struct list_head inetsw6[SOCK_MAX];
75static DEFINE_SPINLOCK(inetsw6_lock);
76
77struct ipv6_params ipv6_defaults = {
78 .disable_ipv6 = 0,
79 .autoconf = 1,
80};
81
82module_param_named(disable, disable_ipv6_mod, int, 0444);
83MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
84
85module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
86MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
87
88module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
89MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
90
91static 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
98void inet6_sock_destruct(struct sock *sk)
99{
100 inet6_cleanup_sock(sk);
101 inet_sock_destruct(sk);
102}
103EXPORT_SYMBOL_GPL(inet6_sock_destruct);
104
105static 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. */
121lookup_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 }
254out:
255 return err;
256out_rcu_unlock:
257 rcu_read_unlock();
258 goto out;
259out_sk_release:
260 sk_common_release(sk);
261 sock->sk = NULL;
262 goto out;
263}
264
265int __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;
417out:
418 if (flags & BIND_WITH_LOCK)
419 release_sock(sk);
420 return err;
421out_unlock:
422 rcu_read_unlock();
423 goto out;
424}
425
426int 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 /* IPV6_ADDRFORM can change sk->sk_prot under us. */
433 prot = READ_ONCE(sk->sk_prot);
434 /* If the socket has its own bind function then use it. */
435 if (prot->bind)
436 return prot->bind(sk, uaddr, addr_len);
437
438 if (addr_len < SIN6_LEN_RFC2133)
439 return -EINVAL;
440
441 /* BPF prog is run before any checks are done so that if the prog
442 * changes context in a wrong way it will be caught.
443 */
444 err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, &addr_len,
445 CGROUP_INET6_BIND, &flags);
446 if (err)
447 return err;
448
449 return __inet6_bind(sk, uaddr, addr_len, flags);
450}
451
452/* bind for INET6 API */
453int inet6_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
454{
455 return inet6_bind_sk(sock->sk, uaddr, addr_len);
456}
457EXPORT_SYMBOL(inet6_bind);
458
459int inet6_release(struct socket *sock)
460{
461 struct sock *sk = sock->sk;
462
463 if (!sk)
464 return -EINVAL;
465
466 /* Free mc lists */
467 ipv6_sock_mc_close(sk);
468
469 /* Free ac lists */
470 ipv6_sock_ac_close(sk);
471
472 return inet_release(sock);
473}
474EXPORT_SYMBOL(inet6_release);
475
476void inet6_cleanup_sock(struct sock *sk)
477{
478 struct ipv6_pinfo *np = inet6_sk(sk);
479 struct sk_buff *skb;
480 struct ipv6_txoptions *opt;
481
482 /* Release rx options */
483
484 skb = xchg(&np->pktoptions, NULL);
485 kfree_skb(skb);
486
487 skb = xchg(&np->rxpmtu, NULL);
488 kfree_skb(skb);
489
490 /* Free flowlabels */
491 fl6_free_socklist(sk);
492
493 /* Free tx options */
494
495 opt = unrcu_pointer(xchg(&np->opt, NULL));
496 if (opt) {
497 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
498 txopt_put(opt);
499 }
500}
501
502/*
503 * This does both peername and sockname.
504 */
505int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
506 int peer)
507{
508 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
509 int sin_addr_len = sizeof(*sin);
510 struct sock *sk = sock->sk;
511 struct inet_sock *inet = inet_sk(sk);
512 struct ipv6_pinfo *np = inet6_sk(sk);
513
514 sin->sin6_family = AF_INET6;
515 sin->sin6_flowinfo = 0;
516 sin->sin6_scope_id = 0;
517 lock_sock(sk);
518 if (peer) {
519 if (!inet->inet_dport ||
520 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
521 peer == 1)) {
522 release_sock(sk);
523 return -ENOTCONN;
524 }
525 sin->sin6_port = inet->inet_dport;
526 sin->sin6_addr = sk->sk_v6_daddr;
527 if (inet6_test_bit(SNDFLOW, sk))
528 sin->sin6_flowinfo = np->flow_label;
529 BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, &sin_addr_len,
530 CGROUP_INET6_GETPEERNAME);
531 } else {
532 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
533 sin->sin6_addr = np->saddr;
534 else
535 sin->sin6_addr = sk->sk_v6_rcv_saddr;
536 sin->sin6_port = inet->inet_sport;
537 BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, &sin_addr_len,
538 CGROUP_INET6_GETSOCKNAME);
539 }
540 sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
541 sk->sk_bound_dev_if);
542 release_sock(sk);
543 return sin_addr_len;
544}
545EXPORT_SYMBOL(inet6_getname);
546
547int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
548{
549 void __user *argp = (void __user *)arg;
550 struct sock *sk = sock->sk;
551 struct net *net = sock_net(sk);
552 const struct proto *prot;
553
554 switch (cmd) {
555 case SIOCADDRT:
556 case SIOCDELRT: {
557 struct in6_rtmsg rtmsg;
558
559 if (copy_from_user(&rtmsg, argp, sizeof(rtmsg)))
560 return -EFAULT;
561 return ipv6_route_ioctl(net, cmd, &rtmsg);
562 }
563 case SIOCSIFADDR:
564 return addrconf_add_ifaddr(net, argp);
565 case SIOCDIFADDR:
566 return addrconf_del_ifaddr(net, argp);
567 case SIOCSIFDSTADDR:
568 return addrconf_set_dstaddr(net, argp);
569 default:
570 /* IPV6_ADDRFORM can change sk->sk_prot under us. */
571 prot = READ_ONCE(sk->sk_prot);
572 if (!prot->ioctl)
573 return -ENOIOCTLCMD;
574 return sk_ioctl(sk, cmd, (void __user *)arg);
575 }
576 /*NOTREACHED*/
577 return 0;
578}
579EXPORT_SYMBOL(inet6_ioctl);
580
581#ifdef CONFIG_COMPAT
582struct compat_in6_rtmsg {
583 struct in6_addr rtmsg_dst;
584 struct in6_addr rtmsg_src;
585 struct in6_addr rtmsg_gateway;
586 u32 rtmsg_type;
587 u16 rtmsg_dst_len;
588 u16 rtmsg_src_len;
589 u32 rtmsg_metric;
590 u32 rtmsg_info;
591 u32 rtmsg_flags;
592 s32 rtmsg_ifindex;
593};
594
595static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
596 struct compat_in6_rtmsg __user *ur)
597{
598 struct in6_rtmsg rt;
599
600 if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst,
601 3 * sizeof(struct in6_addr)) ||
602 get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
603 get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) ||
604 get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) ||
605 get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
606 get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
607 get_user(rt.rtmsg_flags, &ur->rtmsg_flags) ||
608 get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex))
609 return -EFAULT;
610
611
612 return ipv6_route_ioctl(sock_net(sk), cmd, &rt);
613}
614
615int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
616{
617 void __user *argp = compat_ptr(arg);
618 struct sock *sk = sock->sk;
619
620 switch (cmd) {
621 case SIOCADDRT:
622 case SIOCDELRT:
623 return inet6_compat_routing_ioctl(sk, cmd, argp);
624 default:
625 return -ENOIOCTLCMD;
626 }
627}
628EXPORT_SYMBOL_GPL(inet6_compat_ioctl);
629#endif /* CONFIG_COMPAT */
630
631int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
632{
633 struct sock *sk = sock->sk;
634 const struct proto *prot;
635
636 if (unlikely(inet_send_prepare(sk)))
637 return -EAGAIN;
638
639 /* IPV6_ADDRFORM can change sk->sk_prot under us. */
640 prot = READ_ONCE(sk->sk_prot);
641 return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
642 sk, msg, size);
643}
644
645int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
646 int flags)
647{
648 struct sock *sk = sock->sk;
649 const struct proto *prot;
650
651 if (likely(!(flags & MSG_ERRQUEUE)))
652 sock_rps_record_flow(sk);
653
654 /* IPV6_ADDRFORM can change sk->sk_prot under us. */
655 prot = READ_ONCE(sk->sk_prot);
656 return INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
657 sk, msg, size, flags);
658}
659
660const struct proto_ops inet6_stream_ops = {
661 .family = PF_INET6,
662 .owner = THIS_MODULE,
663 .release = inet6_release,
664 .bind = inet6_bind,
665 .connect = inet_stream_connect, /* ok */
666 .socketpair = sock_no_socketpair, /* a do nothing */
667 .accept = inet_accept, /* ok */
668 .getname = inet6_getname,
669 .poll = tcp_poll, /* ok */
670 .ioctl = inet6_ioctl, /* must change */
671 .gettstamp = sock_gettstamp,
672 .listen = inet_listen, /* ok */
673 .shutdown = inet_shutdown, /* ok */
674 .setsockopt = sock_common_setsockopt, /* ok */
675 .getsockopt = sock_common_getsockopt, /* ok */
676 .sendmsg = inet6_sendmsg, /* retpoline's sake */
677 .recvmsg = inet6_recvmsg, /* retpoline's sake */
678#ifdef CONFIG_MMU
679 .mmap = tcp_mmap,
680#endif
681 .splice_eof = inet_splice_eof,
682 .sendmsg_locked = tcp_sendmsg_locked,
683 .splice_read = tcp_splice_read,
684 .set_peek_off = sk_set_peek_off,
685 .read_sock = tcp_read_sock,
686 .read_skb = tcp_read_skb,
687 .peek_len = tcp_peek_len,
688#ifdef CONFIG_COMPAT
689 .compat_ioctl = inet6_compat_ioctl,
690#endif
691 .set_rcvlowat = tcp_set_rcvlowat,
692 .set_rcvbuf = tcp_set_rcvbuf,
693};
694EXPORT_SYMBOL_GPL(inet6_stream_ops);
695
696const struct proto_ops inet6_dgram_ops = {
697 .family = PF_INET6,
698 .owner = THIS_MODULE,
699 .release = inet6_release,
700 .bind = inet6_bind,
701 .connect = inet_dgram_connect, /* ok */
702 .socketpair = sock_no_socketpair, /* a do nothing */
703 .accept = sock_no_accept, /* a do nothing */
704 .getname = inet6_getname,
705 .poll = udp_poll, /* ok */
706 .ioctl = inet6_ioctl, /* must change */
707 .gettstamp = sock_gettstamp,
708 .listen = sock_no_listen, /* ok */
709 .shutdown = inet_shutdown, /* ok */
710 .setsockopt = sock_common_setsockopt, /* ok */
711 .getsockopt = sock_common_getsockopt, /* ok */
712 .sendmsg = inet6_sendmsg, /* retpoline's sake */
713 .recvmsg = inet6_recvmsg, /* retpoline's sake */
714 .read_skb = udp_read_skb,
715 .mmap = sock_no_mmap,
716 .set_peek_off = udp_set_peek_off,
717#ifdef CONFIG_COMPAT
718 .compat_ioctl = inet6_compat_ioctl,
719#endif
720};
721
722static const struct net_proto_family inet6_family_ops = {
723 .family = PF_INET6,
724 .create = inet6_create,
725 .owner = THIS_MODULE,
726};
727
728int inet6_register_protosw(struct inet_protosw *p)
729{
730 struct list_head *lh;
731 struct inet_protosw *answer;
732 struct list_head *last_perm;
733 int protocol = p->protocol;
734 int ret;
735
736 spin_lock_bh(&inetsw6_lock);
737
738 ret = -EINVAL;
739 if (p->type >= SOCK_MAX)
740 goto out_illegal;
741
742 /* If we are trying to override a permanent protocol, bail. */
743 answer = NULL;
744 ret = -EPERM;
745 last_perm = &inetsw6[p->type];
746 list_for_each(lh, &inetsw6[p->type]) {
747 answer = list_entry(lh, struct inet_protosw, list);
748
749 /* Check only the non-wild match. */
750 if (INET_PROTOSW_PERMANENT & answer->flags) {
751 if (protocol == answer->protocol)
752 break;
753 last_perm = lh;
754 }
755
756 answer = NULL;
757 }
758 if (answer)
759 goto out_permanent;
760
761 /* Add the new entry after the last permanent entry if any, so that
762 * the new entry does not override a permanent entry when matched with
763 * a wild-card protocol. But it is allowed to override any existing
764 * non-permanent entry. This means that when we remove this entry, the
765 * system automatically returns to the old behavior.
766 */
767 list_add_rcu(&p->list, last_perm);
768 ret = 0;
769out:
770 spin_unlock_bh(&inetsw6_lock);
771 return ret;
772
773out_permanent:
774 pr_err("Attempt to override permanent protocol %d\n", protocol);
775 goto out;
776
777out_illegal:
778 pr_err("Ignoring attempt to register invalid socket type %d\n",
779 p->type);
780 goto out;
781}
782EXPORT_SYMBOL(inet6_register_protosw);
783
784void
785inet6_unregister_protosw(struct inet_protosw *p)
786{
787 if (INET_PROTOSW_PERMANENT & p->flags) {
788 pr_err("Attempt to unregister permanent protocol %d\n",
789 p->protocol);
790 } else {
791 spin_lock_bh(&inetsw6_lock);
792 list_del_rcu(&p->list);
793 spin_unlock_bh(&inetsw6_lock);
794
795 synchronize_net();
796 }
797}
798EXPORT_SYMBOL(inet6_unregister_protosw);
799
800int inet6_sk_rebuild_header(struct sock *sk)
801{
802 struct ipv6_pinfo *np = inet6_sk(sk);
803 struct inet_sock *inet = inet_sk(sk);
804 struct in6_addr *final_p;
805 struct dst_entry *dst;
806 struct flowi6 *fl6;
807
808 dst = __sk_dst_check(sk, np->dst_cookie);
809 if (dst)
810 return 0;
811
812 fl6 = &inet->cork.fl.u.ip6;
813 memset(fl6, 0, sizeof(*fl6));
814 fl6->flowi6_proto = sk->sk_protocol;
815 fl6->daddr = sk->sk_v6_daddr;
816 fl6->saddr = np->saddr;
817 fl6->flowlabel = np->flow_label;
818 fl6->flowi6_oif = sk->sk_bound_dev_if;
819 fl6->flowi6_mark = sk->sk_mark;
820 fl6->fl6_dport = inet->inet_dport;
821 fl6->fl6_sport = inet->inet_sport;
822 fl6->flowi6_uid = sk_uid(sk);
823 security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
824
825 ip6_ecmp_set_mp_hash(sock_net(sk), fl6, sk->sk_txhash);
826
827 rcu_read_lock();
828 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &np->final);
829 rcu_read_unlock();
830
831 dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
832 if (IS_ERR(dst)) {
833 sk->sk_route_caps = 0;
834 WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst));
835 return PTR_ERR(dst);
836 }
837
838 ip6_dst_store(sk, dst, false, false);
839 return 0;
840}
841
842bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
843 const struct inet6_skb_parm *opt)
844{
845 const struct ipv6_pinfo *np = inet6_sk(sk);
846
847 if (np->rxopt.all) {
848 if (((opt->flags & IP6SKB_HOPBYHOP) &&
849 (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
850 (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
851 np->rxopt.bits.rxflow) ||
852 (opt->srcrt && (np->rxopt.bits.srcrt ||
853 np->rxopt.bits.osrcrt)) ||
854 ((opt->dst1 || opt->dst0) &&
855 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
856 return true;
857 }
858 return false;
859}
860
861static struct packet_type ipv6_packet_type __read_mostly = {
862 .type = cpu_to_be16(ETH_P_IPV6),
863 .func = ipv6_rcv,
864 .list_func = ipv6_list_rcv,
865};
866
867static int __init ipv6_packet_init(void)
868{
869 dev_add_pack(&ipv6_packet_type);
870 return 0;
871}
872
873static void ipv6_packet_cleanup(void)
874{
875 dev_remove_pack(&ipv6_packet_type);
876}
877
878static int __net_init ipv6_init_mibs(struct net *net)
879{
880 int i;
881
882 net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
883 if (!net->mib.udp_stats_in6)
884 return -ENOMEM;
885
886 net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
887 if (!net->mib.ipv6_statistics)
888 goto err_ip_mib;
889
890 for_each_possible_cpu(i) {
891 struct ipstats_mib *af_inet6_stats;
892 af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
893 u64_stats_init(&af_inet6_stats->syncp);
894 }
895
896 net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
897 if (!net->mib.icmpv6_statistics)
898 goto err_icmp_mib;
899
900 net->mib.icmpv6msg_statistics = kzalloc_obj(struct icmpv6msg_mib);
901 if (!net->mib.icmpv6msg_statistics)
902 goto err_icmpmsg_mib;
903 return 0;
904
905err_icmpmsg_mib:
906 free_percpu(net->mib.icmpv6_statistics);
907err_icmp_mib:
908 free_percpu(net->mib.ipv6_statistics);
909err_ip_mib:
910 free_percpu(net->mib.udp_stats_in6);
911 return -ENOMEM;
912}
913
914static void ipv6_cleanup_mibs(struct net *net)
915{
916 free_percpu(net->mib.udp_stats_in6);
917 free_percpu(net->mib.ipv6_statistics);
918 free_percpu(net->mib.icmpv6_statistics);
919 kfree(net->mib.icmpv6msg_statistics);
920}
921
922static int __net_init inet6_net_init(struct net *net)
923{
924 int err = 0;
925
926 net->ipv6.sysctl.bindv6only = 0;
927 net->ipv6.sysctl.icmpv6_time = HZ / 10;
928 net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
929 net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
930 net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
931 net->ipv6.sysctl.icmpv6_error_anycast_as_unicast = 0;
932 net->ipv6.sysctl.icmpv6_errors_extension_mask = 0;
933
934 /* By default, rate limit error messages.
935 * Except for pmtu discovery, it would break it.
936 * proc_do_large_bitmap needs pointer to the bitmap.
937 */
938 bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
939 bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
940 net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
941
942 net->ipv6.sysctl.flowlabel_consistency = 1;
943 net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
944 net->ipv6.sysctl.idgen_retries = 3;
945 net->ipv6.sysctl.idgen_delay = 1 * HZ;
946 net->ipv6.sysctl.flowlabel_state_ranges = 0;
947 net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
948 net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
949 net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
950 net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
951 net->ipv6.sysctl.fib_notify_on_flag_change = 0;
952 atomic_set(&net->ipv6.fib6_sernum, 1);
953
954 net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID;
955 net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE;
956
957 err = ipv6_init_mibs(net);
958 if (err)
959 return err;
960#ifdef CONFIG_PROC_FS
961 err = udp6_proc_init(net);
962 if (err)
963 goto out;
964 err = tcp6_proc_init(net);
965 if (err)
966 goto proc_tcp6_fail;
967 err = ac6_proc_init(net);
968 if (err)
969 goto proc_ac6_fail;
970#endif
971 return err;
972
973#ifdef CONFIG_PROC_FS
974proc_ac6_fail:
975 tcp6_proc_exit(net);
976proc_tcp6_fail:
977 udp6_proc_exit(net);
978out:
979 ipv6_cleanup_mibs(net);
980 return err;
981#endif
982}
983
984static void __net_exit inet6_net_exit(struct net *net)
985{
986#ifdef CONFIG_PROC_FS
987 udp6_proc_exit(net);
988 tcp6_proc_exit(net);
989 ac6_proc_exit(net);
990#endif
991 ipv6_cleanup_mibs(net);
992}
993
994static struct pernet_operations inet6_net_ops = {
995 .init = inet6_net_init,
996 .exit = inet6_net_exit,
997};
998
999static int __init inet6_init(void)
1000{
1001 struct list_head *r;
1002 int err = 0;
1003
1004 sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
1005
1006 /* Register the socket-side information for inet6_create. */
1007 for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1008 INIT_LIST_HEAD(r);
1009
1010 raw_hashinfo_init(&raw_v6_hashinfo);
1011
1012 if (disable_ipv6_mod) {
1013 pr_info("Loaded, but administratively disabled, reboot required to enable\n");
1014 goto out;
1015 }
1016
1017 err = proto_register(&tcpv6_prot, 1);
1018 if (err)
1019 goto out;
1020
1021 err = proto_register(&udpv6_prot, 1);
1022 if (err)
1023 goto out_unregister_tcp_proto;
1024
1025 err = proto_register(&rawv6_prot, 1);
1026 if (err)
1027 goto out_unregister_udp_proto;
1028
1029 err = proto_register(&pingv6_prot, 1);
1030 if (err)
1031 goto out_unregister_raw_proto;
1032
1033 /* We MUST register RAW sockets before we create the ICMP6,
1034 * IGMP6, or NDISC control sockets.
1035 */
1036 err = rawv6_init();
1037 if (err)
1038 goto out_unregister_ping_proto;
1039
1040 /* Register the family here so that the init calls below will
1041 * be able to create sockets. (?? is this dangerous ??)
1042 */
1043 err = sock_register(&inet6_family_ops);
1044 if (err)
1045 goto out_sock_register_fail;
1046
1047 /*
1048 * ipngwg API draft makes clear that the correct semantics
1049 * for TCP and UDP is to consider one TCP and UDP instance
1050 * in a host available by both INET and INET6 APIs and
1051 * able to communicate via both network protocols.
1052 */
1053
1054 err = register_pernet_subsys(&inet6_net_ops);
1055 if (err)
1056 goto register_pernet_fail;
1057 err = ip6_mr_init();
1058 if (err)
1059 goto ipmr_fail;
1060 err = icmpv6_init();
1061 if (err)
1062 goto icmp_fail;
1063 err = ndisc_init();
1064 if (err)
1065 goto ndisc_fail;
1066 err = igmp6_init();
1067 if (err)
1068 goto igmp_fail;
1069
1070 /* Create /proc/foo6 entries. */
1071#ifdef CONFIG_PROC_FS
1072 err = -ENOMEM;
1073 if (raw6_proc_init())
1074 goto proc_raw6_fail;
1075 if (ipv6_misc_proc_init())
1076 goto proc_misc6_fail;
1077 if (if6_proc_init())
1078 goto proc_if6_fail;
1079#endif
1080 err = ip6_route_init();
1081 if (err)
1082 goto ip6_route_fail;
1083 err = ndisc_late_init();
1084 if (err)
1085 goto ndisc_late_fail;
1086 err = ip6_flowlabel_init();
1087 if (err)
1088 goto ip6_flowlabel_fail;
1089 err = ipv6_anycast_init();
1090 if (err)
1091 goto ipv6_anycast_fail;
1092 err = addrconf_init();
1093 if (err)
1094 goto addrconf_fail;
1095
1096 /* Init v6 extension headers. */
1097 err = ipv6_exthdrs_init();
1098 if (err)
1099 goto ipv6_exthdrs_fail;
1100
1101 err = ipv6_frag_init();
1102 if (err)
1103 goto ipv6_frag_fail;
1104
1105 /* Init v6 transport protocols. */
1106 err = udpv6_init();
1107 if (err)
1108 goto udpv6_fail;
1109
1110 err = udpv6_offload_init();
1111 if (err)
1112 goto udpv6_offload_fail;
1113
1114 err = tcpv6_init();
1115 if (err)
1116 goto tcpv6_fail;
1117
1118 err = ipv6_packet_init();
1119 if (err)
1120 goto ipv6_packet_fail;
1121
1122 err = pingv6_init();
1123 if (err)
1124 goto pingv6_fail;
1125
1126 err = calipso_init();
1127 if (err)
1128 goto calipso_fail;
1129
1130 err = seg6_init();
1131 if (err)
1132 goto seg6_fail;
1133
1134 err = rpl_init();
1135 if (err)
1136 goto rpl_fail;
1137
1138 err = ioam6_init();
1139 if (err)
1140 goto ioam6_fail;
1141
1142 err = igmp6_late_init();
1143 if (err)
1144 goto igmp6_late_err;
1145
1146#ifdef CONFIG_SYSCTL
1147 err = ipv6_sysctl_register();
1148 if (err)
1149 goto sysctl_fail;
1150#endif
1151
1152out:
1153 return err;
1154
1155#ifdef CONFIG_SYSCTL
1156sysctl_fail:
1157 igmp6_late_cleanup();
1158#endif
1159igmp6_late_err:
1160 ioam6_exit();
1161ioam6_fail:
1162 rpl_exit();
1163rpl_fail:
1164 seg6_exit();
1165seg6_fail:
1166 calipso_exit();
1167calipso_fail:
1168 pingv6_exit();
1169pingv6_fail:
1170 ipv6_packet_cleanup();
1171ipv6_packet_fail:
1172 tcpv6_exit();
1173tcpv6_fail:
1174 udpv6_offload_exit();
1175udpv6_offload_fail:
1176 udpv6_exit();
1177udpv6_fail:
1178 ipv6_frag_exit();
1179ipv6_frag_fail:
1180 ipv6_exthdrs_exit();
1181ipv6_exthdrs_fail:
1182 addrconf_cleanup();
1183addrconf_fail:
1184 ipv6_anycast_cleanup();
1185ipv6_anycast_fail:
1186 ip6_flowlabel_cleanup();
1187ip6_flowlabel_fail:
1188 ndisc_late_cleanup();
1189ndisc_late_fail:
1190 ip6_route_cleanup();
1191ip6_route_fail:
1192#ifdef CONFIG_PROC_FS
1193 if6_proc_exit();
1194proc_if6_fail:
1195 ipv6_misc_proc_exit();
1196proc_misc6_fail:
1197 raw6_proc_exit();
1198proc_raw6_fail:
1199#endif
1200 igmp6_cleanup();
1201igmp_fail:
1202 ndisc_cleanup();
1203ndisc_fail:
1204 icmpv6_cleanup();
1205icmp_fail:
1206 ip6_mr_cleanup();
1207ipmr_fail:
1208 unregister_pernet_subsys(&inet6_net_ops);
1209register_pernet_fail:
1210 sock_unregister(PF_INET6);
1211 rtnl_unregister_all(PF_INET6);
1212out_sock_register_fail:
1213 rawv6_exit();
1214out_unregister_ping_proto:
1215 proto_unregister(&pingv6_prot);
1216out_unregister_raw_proto:
1217 proto_unregister(&rawv6_prot);
1218out_unregister_udp_proto:
1219 proto_unregister(&udpv6_prot);
1220out_unregister_tcp_proto:
1221 proto_unregister(&tcpv6_prot);
1222 goto out;
1223}
1224device_initcall(inet6_init);