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