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