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