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