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