blob: 57e70a407df4f48e34ebbf4b3ba3fd8d06edcabf [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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
68MODULE_AUTHOR("Cast of dozens");
69MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
70MODULE_LICENSE("GPL");
71
72/* The inetsw6 table contains everything that inet6_create needs to
73 * build a new socket.
74 */
75static struct list_head inetsw6[SOCK_MAX];
76static DEFINE_SPINLOCK(inetsw6_lock);
77
78struct ipv6_params ipv6_defaults = {
79 .disable_ipv6 = 0,
80 .autoconf = 1,
81};
82
83static int disable_ipv6_mod;
84
85module_param_named(disable, disable_ipv6_mod, int, 0444);
86MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
87
88module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
89MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
90
91module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
92MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
93
94bool ipv6_mod_enabled(void)
95{
96 return disable_ipv6_mod == 0;
97}
98EXPORT_SYMBOL_GPL(ipv6_mod_enabled);
99
100static __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
107void inet6_sock_destruct(struct sock *sk)
108{
109 inet6_cleanup_sock(sk);
110 inet_sock_destruct(sk);
111}
112EXPORT_SYMBOL_GPL(inet6_sock_destruct);
113
114static 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. */
130lookup_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 goto out_sk_release;
256 }
257 if (sk->sk_prot->init) {
258 err = sk->sk_prot->init(sk);
259 if (err)
260 goto out_sk_release;
261 }
262
263 if (!kern) {
264 err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
265 if (err)
266 goto out_sk_release;
267 }
268out:
269 return err;
270out_rcu_unlock:
271 rcu_read_unlock();
272 goto out;
273out_sk_release:
274 sk_common_release(sk);
275 sock->sk = NULL;
276 goto out;
277}
278
279static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
280 bool force_bind_address_no_port, bool with_lock)
281{
282 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
283 struct inet_sock *inet = inet_sk(sk);
284 struct ipv6_pinfo *np = inet6_sk(sk);
285 struct net *net = sock_net(sk);
286 __be32 v4addr = 0;
287 unsigned short snum;
288 bool saved_ipv6only;
289 int addr_type = 0;
290 int err = 0;
291
292 if (addr->sin6_family != AF_INET6)
293 return -EAFNOSUPPORT;
294
295 addr_type = ipv6_addr_type(&addr->sin6_addr);
296 if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
297 return -EINVAL;
298
299 snum = ntohs(addr->sin6_port);
300 if (snum && inet_is_local_unbindable_port(net, snum))
301 return -EPERM;
302 if (snum && snum < inet_prot_sock(net) &&
303 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
304 return -EACCES;
305
306 if (with_lock)
307 lock_sock(sk);
308
309 /* Check these errors (active socket, double bind). */
310 if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
311 err = -EINVAL;
312 goto out;
313 }
314
315 /* Check if the address belongs to the host. */
316 if (addr_type == IPV6_ADDR_MAPPED) {
317 struct net_device *dev = NULL;
318 int chk_addr_ret;
319
320 /* Binding to v4-mapped address on a v6-only socket
321 * makes no sense
322 */
323 if (sk->sk_ipv6only) {
324 err = -EINVAL;
325 goto out;
326 }
327
328 rcu_read_lock();
329 if (sk->sk_bound_dev_if) {
330 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
331 if (!dev) {
332 err = -ENODEV;
333 goto out_unlock;
334 }
335 }
336
337 /* Reproduce AF_INET checks to make the bindings consistent */
338 v4addr = addr->sin6_addr.s6_addr32[3];
339 chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
340 rcu_read_unlock();
341
342 if (!inet_can_nonlocal_bind(net, inet) &&
343 v4addr != htonl(INADDR_ANY) &&
344 chk_addr_ret != RTN_LOCAL &&
345 chk_addr_ret != RTN_MULTICAST &&
346 chk_addr_ret != RTN_BROADCAST) {
347 err = -EADDRNOTAVAIL;
348 goto out;
349 }
350 } else {
351 if (addr_type != IPV6_ADDR_ANY) {
352 struct net_device *dev = NULL;
353
354 rcu_read_lock();
355 if (__ipv6_addr_needs_scope_id(addr_type)) {
356 if (addr_len >= sizeof(struct sockaddr_in6) &&
357 addr->sin6_scope_id) {
358 /* Override any existing binding, if another one
359 * is supplied by user.
360 */
361 sk->sk_bound_dev_if = addr->sin6_scope_id;
362 }
363
364 /* Binding to link-local address requires an interface */
365 if (!sk->sk_bound_dev_if) {
366 err = -EINVAL;
367 goto out_unlock;
368 }
369 }
370
371 if (sk->sk_bound_dev_if) {
372 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
373 if (!dev) {
374 err = -ENODEV;
375 goto out_unlock;
376 }
377 }
378
379 /* ipv4 addr of the socket is invalid. Only the
380 * unspecified and mapped address have a v4 equivalent.
381 */
382 v4addr = LOOPBACK4_IPV6;
383 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
384 if (!ipv6_can_nonlocal_bind(net, inet) &&
385 !ipv6_chk_addr(net, &addr->sin6_addr,
386 dev, 0)) {
387 err = -EADDRNOTAVAIL;
388 goto out_unlock;
389 }
390 }
391 rcu_read_unlock();
392 }
393 }
394
395 inet->inet_rcv_saddr = v4addr;
396 inet->inet_saddr = v4addr;
397
398 sk->sk_v6_rcv_saddr = addr->sin6_addr;
399
400 if (!(addr_type & IPV6_ADDR_MULTICAST))
401 np->saddr = addr->sin6_addr;
402
403 saved_ipv6only = sk->sk_ipv6only;
404 if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED)
405 sk->sk_ipv6only = 1;
406
407 /* Make sure we are allowed to bind here. */
408 if (snum || !(inet->bind_address_no_port ||
409 force_bind_address_no_port)) {
410 if (sk->sk_prot->get_port(sk, snum)) {
411 sk->sk_ipv6only = saved_ipv6only;
412 inet_reset_saddr(sk);
413 err = -EADDRINUSE;
414 goto out;
415 }
416 err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk);
417 if (err) {
418 sk->sk_ipv6only = saved_ipv6only;
419 inet_reset_saddr(sk);
420 goto out;
421 }
422 }
423
424 if (addr_type != IPV6_ADDR_ANY)
425 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
426 if (snum)
427 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
428 inet->inet_sport = htons(inet->inet_num);
429 inet->inet_dport = 0;
430 inet->inet_daddr = 0;
431out:
432 if (with_lock)
433 release_sock(sk);
434 return err;
435out_unlock:
436 rcu_read_unlock();
437 goto out;
438}
439
440/* bind for INET6 API */
441int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
442{
443 struct sock *sk = sock->sk;
444 const struct proto *prot;
445 int err = 0;
446
447 /* IPV6_ADDRFORM can change sk->sk_prot under us. */
448 prot = READ_ONCE(sk->sk_prot);
449 /* If the socket has its own bind function then use it. */
450 if (prot->bind)
451 return prot->bind(sk, uaddr, addr_len);
452
453 if (addr_len < SIN6_LEN_RFC2133)
454 return -EINVAL;
455
456 /* BPF prog is run before any checks are done so that if the prog
457 * changes context in a wrong way it will be caught.
458 */
459 err = BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr);
460 if (err)
461 return err;
462
463 return __inet6_bind(sk, uaddr, addr_len, false, true);
464}
465EXPORT_SYMBOL(inet6_bind);
466
467int inet6_release(struct socket *sock)
468{
469 struct sock *sk = sock->sk;
470
471 if (!sk)
472 return -EINVAL;
473
474 /* Free mc lists */
475 ipv6_sock_mc_close(sk);
476
477 /* Free ac lists */
478 ipv6_sock_ac_close(sk);
479
480 return inet_release(sock);
481}
482EXPORT_SYMBOL(inet6_release);
483
484void inet6_destroy_sock(struct sock *sk)
485{
486 struct ipv6_pinfo *np = inet6_sk(sk);
487 struct sk_buff *skb;
488 struct ipv6_txoptions *opt;
489
490 /* Release rx options */
491
492 skb = xchg(&np->pktoptions, NULL);
493 kfree_skb(skb);
494
495 skb = xchg(&np->rxpmtu, NULL);
496 kfree_skb(skb);
497
498 /* Free flowlabels */
499 fl6_free_socklist(sk);
500
501 /* Free tx options */
502
503 opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
504 if (opt) {
505 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
506 txopt_put(opt);
507 }
508}
509EXPORT_SYMBOL_GPL(inet6_destroy_sock);
510
511void inet6_cleanup_sock(struct sock *sk)
512{
513 inet6_destroy_sock(sk);
514}
515EXPORT_SYMBOL_GPL(inet6_cleanup_sock);
516
517/*
518 * This does both peername and sockname.
519 */
520
521int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
522 int peer)
523{
524 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
525 struct sock *sk = sock->sk;
526 struct inet_sock *inet = inet_sk(sk);
527 struct ipv6_pinfo *np = inet6_sk(sk);
528
529 sin->sin6_family = AF_INET6;
530 sin->sin6_flowinfo = 0;
531 sin->sin6_scope_id = 0;
532 if (peer) {
533 if (!inet->inet_dport)
534 return -ENOTCONN;
535 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
536 peer == 1)
537 return -ENOTCONN;
538 sin->sin6_port = inet->inet_dport;
539 sin->sin6_addr = sk->sk_v6_daddr;
540 if (np->sndflow)
541 sin->sin6_flowinfo = np->flow_label;
542 } else {
543 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
544 sin->sin6_addr = np->saddr;
545 else
546 sin->sin6_addr = sk->sk_v6_rcv_saddr;
547
548 sin->sin6_port = inet->inet_sport;
549 }
550 sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
551 sk->sk_bound_dev_if);
552 return sizeof(*sin);
553}
554EXPORT_SYMBOL(inet6_getname);
555
556int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
557{
558 struct sock *sk = sock->sk;
559 struct net *net = sock_net(sk);
560 const struct proto *prot;
561
562 switch (cmd) {
563 case SIOCADDRT:
564 case SIOCDELRT:
565
566 return ipv6_route_ioctl(net, cmd, (void __user *)arg);
567
568 case SIOCSIFADDR:
569 return addrconf_add_ifaddr(net, (void __user *) arg);
570 case SIOCDIFADDR:
571 return addrconf_del_ifaddr(net, (void __user *) arg);
572 case SIOCSIFDSTADDR:
573 return addrconf_set_dstaddr(net, (void __user *) arg);
574 default:
575 /* IPV6_ADDRFORM can change sk->sk_prot under us. */
576 prot = READ_ONCE(sk->sk_prot);
577 if (!prot->ioctl)
578 return -ENOIOCTLCMD;
579 return prot->ioctl(sk, cmd, arg);
580 }
581 /*NOTREACHED*/
582 return 0;
583}
584EXPORT_SYMBOL(inet6_ioctl);
585
586INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *,
587 size_t));
588int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
589{
590 struct sock *sk = sock->sk;
591 const struct proto *prot;
592
593 if (unlikely(inet_send_prepare(sk)))
594 return -EAGAIN;
595
596 /* IPV6_ADDRFORM can change sk->sk_prot under us. */
597 prot = READ_ONCE(sk->sk_prot);
598 return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
599 sk, msg, size);
600}
601
602INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *,
603 size_t, int, int, int *));
604int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
605 int flags)
606{
607 struct sock *sk = sock->sk;
608 const struct proto *prot;
609 int addr_len = 0;
610 int err;
611
612 if (likely(!(flags & MSG_ERRQUEUE)))
613 sock_rps_record_flow(sk);
614
615 /* IPV6_ADDRFORM can change sk->sk_prot under us. */
616 prot = READ_ONCE(sk->sk_prot);
617 err = INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
618 sk, msg, size, flags & MSG_DONTWAIT,
619 flags & ~MSG_DONTWAIT, &addr_len);
620 if (err >= 0)
621 msg->msg_namelen = addr_len;
622 return err;
623}
624
625const struct proto_ops inet6_stream_ops = {
626 .family = PF_INET6,
627 .owner = THIS_MODULE,
628 .release = inet6_release,
629 .bind = inet6_bind,
630 .connect = inet_stream_connect, /* ok */
631 .socketpair = sock_no_socketpair, /* a do nothing */
632 .accept = inet_accept, /* ok */
633 .getname = inet6_getname,
634 .poll = tcp_poll, /* ok */
635 .ioctl = inet6_ioctl, /* must change */
636 .gettstamp = sock_gettstamp,
637 .listen = inet_listen, /* ok */
638 .shutdown = inet_shutdown, /* ok */
639 .setsockopt = sock_common_setsockopt, /* ok */
640 .getsockopt = sock_common_getsockopt, /* ok */
641 .sendmsg = inet6_sendmsg, /* retpoline's sake */
642 .recvmsg = inet6_recvmsg, /* retpoline's sake */
643#ifdef CONFIG_MMU
644 .mmap = tcp_mmap,
645#endif
646 .sendpage = inet_sendpage,
647 .sendmsg_locked = tcp_sendmsg_locked,
648 .sendpage_locked = tcp_sendpage_locked,
649 .splice_read = tcp_splice_read,
650 .read_sock = tcp_read_sock,
651 .peek_len = tcp_peek_len,
652#ifdef CONFIG_COMPAT
653 .compat_setsockopt = compat_sock_common_setsockopt,
654 .compat_getsockopt = compat_sock_common_getsockopt,
655#endif
656 .set_rcvlowat = tcp_set_rcvlowat,
657};
658
659const struct proto_ops inet6_dgram_ops = {
660 .family = PF_INET6,
661 .owner = THIS_MODULE,
662 .release = inet6_release,
663 .bind = inet6_bind,
664 .connect = inet_dgram_connect, /* ok */
665 .socketpair = sock_no_socketpair, /* a do nothing */
666 .accept = sock_no_accept, /* a do nothing */
667 .getname = inet6_getname,
668 .poll = udp_poll, /* ok */
669 .ioctl = inet6_ioctl, /* must change */
670 .gettstamp = sock_gettstamp,
671 .listen = sock_no_listen, /* ok */
672 .shutdown = inet_shutdown, /* ok */
673 .setsockopt = sock_common_setsockopt, /* ok */
674 .getsockopt = sock_common_getsockopt, /* ok */
675 .sendmsg = inet6_sendmsg, /* retpoline's sake */
676 .recvmsg = inet6_recvmsg, /* retpoline's sake */
677 .mmap = sock_no_mmap,
678 .sendpage = sock_no_sendpage,
679 .set_peek_off = sk_set_peek_off,
680#ifdef CONFIG_COMPAT
681 .compat_setsockopt = compat_sock_common_setsockopt,
682 .compat_getsockopt = compat_sock_common_getsockopt,
683#endif
684};
685
686static const struct net_proto_family inet6_family_ops = {
687 .family = PF_INET6,
688 .create = inet6_create,
689 .owner = THIS_MODULE,
690};
691
692int inet6_register_protosw(struct inet_protosw *p)
693{
694 struct list_head *lh;
695 struct inet_protosw *answer;
696 struct list_head *last_perm;
697 int protocol = p->protocol;
698 int ret;
699
700 spin_lock_bh(&inetsw6_lock);
701
702 ret = -EINVAL;
703 if (p->type >= SOCK_MAX)
704 goto out_illegal;
705
706 /* If we are trying to override a permanent protocol, bail. */
707 answer = NULL;
708 ret = -EPERM;
709 last_perm = &inetsw6[p->type];
710 list_for_each(lh, &inetsw6[p->type]) {
711 answer = list_entry(lh, struct inet_protosw, list);
712
713 /* Check only the non-wild match. */
714 if (INET_PROTOSW_PERMANENT & answer->flags) {
715 if (protocol == answer->protocol)
716 break;
717 last_perm = lh;
718 }
719
720 answer = NULL;
721 }
722 if (answer)
723 goto out_permanent;
724
725 /* Add the new entry after the last permanent entry if any, so that
726 * the new entry does not override a permanent entry when matched with
727 * a wild-card protocol. But it is allowed to override any existing
728 * non-permanent entry. This means that when we remove this entry, the
729 * system automatically returns to the old behavior.
730 */
731 list_add_rcu(&p->list, last_perm);
732 ret = 0;
733out:
734 spin_unlock_bh(&inetsw6_lock);
735 return ret;
736
737out_permanent:
738 pr_err("Attempt to override permanent protocol %d\n", protocol);
739 goto out;
740
741out_illegal:
742 pr_err("Ignoring attempt to register invalid socket type %d\n",
743 p->type);
744 goto out;
745}
746EXPORT_SYMBOL(inet6_register_protosw);
747
748void
749inet6_unregister_protosw(struct inet_protosw *p)
750{
751 if (INET_PROTOSW_PERMANENT & p->flags) {
752 pr_err("Attempt to unregister permanent protocol %d\n",
753 p->protocol);
754 } else {
755 spin_lock_bh(&inetsw6_lock);
756 list_del_rcu(&p->list);
757 spin_unlock_bh(&inetsw6_lock);
758
759 synchronize_net();
760 }
761}
762EXPORT_SYMBOL(inet6_unregister_protosw);
763
764int inet6_sk_rebuild_header(struct sock *sk)
765{
766 struct ipv6_pinfo *np = inet6_sk(sk);
767 struct dst_entry *dst;
768
769 dst = __sk_dst_check(sk, np->dst_cookie);
770
771 if (!dst) {
772 struct inet_sock *inet = inet_sk(sk);
773 struct in6_addr *final_p, final;
774 struct flowi6 fl6;
775
776 memset(&fl6, 0, sizeof(fl6));
777 fl6.flowi6_proto = sk->sk_protocol;
778 fl6.daddr = sk->sk_v6_daddr;
779 fl6.saddr = np->saddr;
780 fl6.flowlabel = np->flow_label;
781 fl6.flowi6_oif = sk->sk_bound_dev_if;
782 fl6.flowi6_mark = sk->sk_mark;
783 fl6.fl6_dport = inet->inet_dport;
784 fl6.fl6_sport = inet->inet_sport;
785 fl6.flowi6_uid = sk->sk_uid;
786 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
787
788 rcu_read_lock();
789 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
790 &final);
791 rcu_read_unlock();
792
793 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
794 if (IS_ERR(dst)) {
795 sk->sk_route_caps = 0;
796 sk->sk_err_soft = -PTR_ERR(dst);
797 return PTR_ERR(dst);
798 }
799
800 ip6_dst_store(sk, dst, NULL, NULL);
801 }
802
803 return 0;
804}
805EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
806
807bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
808 const struct inet6_skb_parm *opt)
809{
810 const struct ipv6_pinfo *np = inet6_sk(sk);
811
812 if (np->rxopt.all) {
813 if (((opt->flags & IP6SKB_HOPBYHOP) &&
814 (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
815 (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
816 np->rxopt.bits.rxflow) ||
817 (opt->srcrt && (np->rxopt.bits.srcrt ||
818 np->rxopt.bits.osrcrt)) ||
819 ((opt->dst1 || opt->dst0) &&
820 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
821 return true;
822 }
823 return false;
824}
825EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
826
827static struct packet_type ipv6_packet_type __read_mostly = {
828 .type = cpu_to_be16(ETH_P_IPV6),
829 .func = ipv6_rcv,
830 .list_func = ipv6_list_rcv,
831};
832
833static int __init ipv6_packet_init(void)
834{
835 dev_add_pack(&ipv6_packet_type);
836 return 0;
837}
838
839static void ipv6_packet_cleanup(void)
840{
841 dev_remove_pack(&ipv6_packet_type);
842}
843
844static int __net_init ipv6_init_mibs(struct net *net)
845{
846 int i;
847
848 net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
849 if (!net->mib.udp_stats_in6)
850 return -ENOMEM;
851 net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
852 if (!net->mib.udplite_stats_in6)
853 goto err_udplite_mib;
854 net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
855 if (!net->mib.ipv6_statistics)
856 goto err_ip_mib;
857
858 for_each_possible_cpu(i) {
859 struct ipstats_mib *af_inet6_stats;
860 af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
861 u64_stats_init(&af_inet6_stats->syncp);
862 }
863
864
865 net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
866 if (!net->mib.icmpv6_statistics)
867 goto err_icmp_mib;
868 net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
869 GFP_KERNEL);
870 if (!net->mib.icmpv6msg_statistics)
871 goto err_icmpmsg_mib;
872 return 0;
873
874err_icmpmsg_mib:
875 free_percpu(net->mib.icmpv6_statistics);
876err_icmp_mib:
877 free_percpu(net->mib.ipv6_statistics);
878err_ip_mib:
879 free_percpu(net->mib.udplite_stats_in6);
880err_udplite_mib:
881 free_percpu(net->mib.udp_stats_in6);
882 return -ENOMEM;
883}
884
885static void ipv6_cleanup_mibs(struct net *net)
886{
887 free_percpu(net->mib.udp_stats_in6);
888 free_percpu(net->mib.udplite_stats_in6);
889 free_percpu(net->mib.ipv6_statistics);
890 free_percpu(net->mib.icmpv6_statistics);
891 kfree(net->mib.icmpv6msg_statistics);
892}
893
894static int __net_init inet6_net_init(struct net *net)
895{
896 int err = 0;
897
898 net->ipv6.sysctl.bindv6only = 0;
899 net->ipv6.sysctl.icmpv6_time = 1*HZ;
900 net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
901 net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
902 net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
903
904 /* By default, rate limit error messages.
905 * Except for pmtu discovery, it would break it.
906 * proc_do_large_bitmap needs pointer to the bitmap.
907 */
908 bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
909 bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
910 net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
911
912 net->ipv6.sysctl.flowlabel_consistency = 1;
913 net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
914 net->ipv6.sysctl.idgen_retries = 3;
915 net->ipv6.sysctl.idgen_delay = 1 * HZ;
916 net->ipv6.sysctl.flowlabel_state_ranges = 0;
917 net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
918 net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
919 net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
920 net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
921 atomic_set(&net->ipv6.fib6_sernum, 1);
922
923 err = ipv6_init_mibs(net);
924 if (err)
925 return err;
926#ifdef CONFIG_PROC_FS
927 err = udp6_proc_init(net);
928 if (err)
929 goto out;
930 err = tcp6_proc_init(net);
931 if (err)
932 goto proc_tcp6_fail;
933 err = ac6_proc_init(net);
934 if (err)
935 goto proc_ac6_fail;
936#endif
937 return err;
938
939#ifdef CONFIG_PROC_FS
940proc_ac6_fail:
941 tcp6_proc_exit(net);
942proc_tcp6_fail:
943 udp6_proc_exit(net);
944out:
945 ipv6_cleanup_mibs(net);
946 return err;
947#endif
948}
949
950static void __net_exit inet6_net_exit(struct net *net)
951{
952#ifdef CONFIG_PROC_FS
953 udp6_proc_exit(net);
954 tcp6_proc_exit(net);
955 ac6_proc_exit(net);
956#endif
957 ipv6_cleanup_mibs(net);
958}
959
960static struct pernet_operations inet6_net_ops = {
961 .init = inet6_net_init,
962 .exit = inet6_net_exit,
963};
964
965static int ipv6_route_input(struct sk_buff *skb)
966{
967 ip6_route_input(skb);
968 return skb_dst(skb)->error;
969}
970
971static const struct ipv6_stub ipv6_stub_impl = {
972 .ipv6_sock_mc_join = ipv6_sock_mc_join,
973 .ipv6_sock_mc_drop = ipv6_sock_mc_drop,
974 .ipv6_dst_lookup_flow = ip6_dst_lookup_flow,
975 .ipv6_route_input = ipv6_route_input,
976 .fib6_get_table = fib6_get_table,
977 .fib6_table_lookup = fib6_table_lookup,
978 .fib6_lookup = fib6_lookup,
979 .fib6_select_path = fib6_select_path,
980 .ip6_mtu_from_fib6 = ip6_mtu_from_fib6,
981 .fib6_nh_init = fib6_nh_init,
982 .fib6_nh_release = fib6_nh_release,
983 .fib6_nh_release_dsts = fib6_nh_release_dsts,
984 .fib6_update_sernum = fib6_update_sernum_stub,
985 .fib6_rt_update = fib6_rt_update,
986 .ip6_del_rt = ip6_del_rt,
987 .udpv6_encap_enable = udpv6_encap_enable,
988 .ndisc_send_na = ndisc_send_na,
989 .nd_tbl = &nd_tbl,
990};
991
992static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
993 .inet6_bind = __inet6_bind,
994 .udp6_lib_lookup = __udp6_lib_lookup,
995};
996
997static int __init inet6_init(void)
998{
999 struct list_head *r;
1000 int err = 0;
1001
1002 sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
1003
1004 /* Register the socket-side information for inet6_create. */
1005 for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1006 INIT_LIST_HEAD(r);
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(&udplitev6_prot, 1);
1022 if (err)
1023 goto out_unregister_udp_proto;
1024
1025 err = proto_register(&rawv6_prot, 1);
1026 if (err)
1027 goto out_unregister_udplite_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 err = ipv6_netfilter_init();
1071 if (err)
1072 goto netfilter_fail;
1073 /* Create /proc/foo6 entries. */
1074#ifdef CONFIG_PROC_FS
1075 err = -ENOMEM;
1076 if (raw6_proc_init())
1077 goto proc_raw6_fail;
1078 if (udplite6_proc_init())
1079 goto proc_udplite6_fail;
1080 if (ipv6_misc_proc_init())
1081 goto proc_misc6_fail;
1082 if (if6_proc_init())
1083 goto proc_if6_fail;
1084#endif
1085 err = ip6_route_init();
1086 if (err)
1087 goto ip6_route_fail;
1088 err = ndisc_late_init();
1089 if (err)
1090 goto ndisc_late_fail;
1091 err = ip6_flowlabel_init();
1092 if (err)
1093 goto ip6_flowlabel_fail;
1094 err = ipv6_anycast_init();
1095 if (err)
1096 goto ipv6_anycast_fail;
1097 err = addrconf_init();
1098 if (err)
1099 goto addrconf_fail;
1100
1101 /* Init v6 extension headers. */
1102 err = ipv6_exthdrs_init();
1103 if (err)
1104 goto ipv6_exthdrs_fail;
1105
1106 err = ipv6_frag_init();
1107 if (err)
1108 goto ipv6_frag_fail;
1109
1110 /* Init v6 transport protocols. */
1111 err = udpv6_init();
1112 if (err)
1113 goto udpv6_fail;
1114
1115 err = udplitev6_init();
1116 if (err)
1117 goto udplitev6_fail;
1118
1119 err = udpv6_offload_init();
1120 if (err)
1121 goto udpv6_offload_fail;
1122
1123 err = tcpv6_init();
1124 if (err)
1125 goto tcpv6_fail;
1126
1127 err = ipv6_packet_init();
1128 if (err)
1129 goto ipv6_packet_fail;
1130
1131 err = pingv6_init();
1132 if (err)
1133 goto pingv6_fail;
1134
1135 err = calipso_init();
1136 if (err)
1137 goto calipso_fail;
1138
1139 err = seg6_init();
1140 if (err)
1141 goto seg6_fail;
1142
1143 err = igmp6_late_init();
1144 if (err)
1145 goto igmp6_late_err;
1146
1147#ifdef CONFIG_SYSCTL
1148 err = ipv6_sysctl_register();
1149 if (err)
1150 goto sysctl_fail;
1151#endif
1152
1153 /* ensure that ipv6 stubs are visible only after ipv6 is ready */
1154 wmb();
1155 ipv6_stub = &ipv6_stub_impl;
1156 ipv6_bpf_stub = &ipv6_bpf_stub_impl;
1157out:
1158 return err;
1159
1160#ifdef CONFIG_SYSCTL
1161sysctl_fail:
1162 igmp6_late_cleanup();
1163#endif
1164igmp6_late_err:
1165 seg6_exit();
1166seg6_fail:
1167 calipso_exit();
1168calipso_fail:
1169 pingv6_exit();
1170pingv6_fail:
1171 ipv6_packet_cleanup();
1172ipv6_packet_fail:
1173 tcpv6_exit();
1174tcpv6_fail:
1175 udpv6_offload_exit();
1176udpv6_offload_fail:
1177 udplitev6_exit();
1178udplitev6_fail:
1179 udpv6_exit();
1180udpv6_fail:
1181 ipv6_frag_exit();
1182ipv6_frag_fail:
1183 ipv6_exthdrs_exit();
1184ipv6_exthdrs_fail:
1185 addrconf_cleanup();
1186addrconf_fail:
1187 ipv6_anycast_cleanup();
1188ipv6_anycast_fail:
1189 ip6_flowlabel_cleanup();
1190ip6_flowlabel_fail:
1191 ndisc_late_cleanup();
1192ndisc_late_fail:
1193 ip6_route_cleanup();
1194ip6_route_fail:
1195#ifdef CONFIG_PROC_FS
1196 if6_proc_exit();
1197proc_if6_fail:
1198 ipv6_misc_proc_exit();
1199proc_misc6_fail:
1200 udplite6_proc_exit();
1201proc_udplite6_fail:
1202 raw6_proc_exit();
1203proc_raw6_fail:
1204#endif
1205 ipv6_netfilter_fini();
1206netfilter_fail:
1207 igmp6_cleanup();
1208igmp_fail:
1209 ndisc_cleanup();
1210ndisc_fail:
1211 icmpv6_cleanup();
1212icmp_fail:
1213 ip6_mr_cleanup();
1214ipmr_fail:
1215 unregister_pernet_subsys(&inet6_net_ops);
1216register_pernet_fail:
1217 sock_unregister(PF_INET6);
1218 rtnl_unregister_all(PF_INET6);
1219out_sock_register_fail:
1220 rawv6_exit();
1221out_unregister_ping_proto:
1222 proto_unregister(&pingv6_prot);
1223out_unregister_raw_proto:
1224 proto_unregister(&rawv6_prot);
1225out_unregister_udplite_proto:
1226 proto_unregister(&udplitev6_prot);
1227out_unregister_udp_proto:
1228 proto_unregister(&udpv6_prot);
1229out_unregister_tcp_proto:
1230 proto_unregister(&tcpv6_prot);
1231 goto out;
1232}
1233module_init(inet6_init);
1234
1235MODULE_ALIAS_NETPROTO(PF_INET6);