2a3e8c942b3351ffd7fa8a64bf1b8cd2d80d9062
[linux-flexiantxendom0-3.2.10.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  *      $Id: af_inet6.c,v 1.66 2002/02/01 22:01:04 davem Exp $
11  *
12  *      Fixes:
13  *      piggy, Karl Knutson     :       Socket protocol table
14  *      Hideaki YOSHIFUJI       :       sin6_scope_id support
15  *      Arnaldo Melo            :       check proc_net_create return, cleanups
16  *
17  *      This program is free software; you can redistribute it and/or
18  *      modify it under the terms of the GNU General Public License
19  *      as published by the Free Software Foundation; either version
20  *      2 of the License, or (at your option) any later version.
21  */
22
23
24 #include <linux/module.h>
25 #include <linux/config.h>
26 #include <linux/errno.h>
27 #include <linux/types.h>
28 #include <linux/socket.h>
29 #include <linux/in.h>
30 #include <linux/kernel.h>
31 #include <linux/major.h>
32 #include <linux/sched.h>
33 #include <linux/timer.h>
34 #include <linux/string.h>
35 #include <linux/sockios.h>
36 #include <linux/net.h>
37 #include <linux/fcntl.h>
38 #include <linux/mm.h>
39 #include <linux/interrupt.h>
40 #include <linux/proc_fs.h>
41 #include <linux/stat.h>
42 #include <linux/init.h>
43
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/icmpv6.h>
47 #include <linux/smp_lock.h>
48
49 #include <net/ip.h>
50 #include <net/ipv6.h>
51 #include <net/udp.h>
52 #include <net/tcp.h>
53 #include <net/ipip.h>
54 #include <net/protocol.h>
55 #include <net/inet_common.h>
56 #include <net/transp_v6.h>
57 #include <net/ip6_route.h>
58 #include <net/addrconf.h>
59 #if CONFIG_IPV6_TUNNEL
60 #include <net/ip6_tunnel.h>
61 #endif
62
63 #include <asm/uaccess.h>
64 #include <asm/system.h>
65
66 #if 0 /*def MODULE*/
67 static int unloadable;     /* XX: Turn to one when all is ok within the
68                               module for allowing unload */
69 MODULE_PARM(unloadable, "i");
70 #endif
71
72 MODULE_AUTHOR("Cast of dozens");
73 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
74 MODULE_LICENSE("GPL");
75
76 /* IPv6 procfs goodies... */
77
78 #ifdef CONFIG_PROC_FS
79 extern int raw6_proc_init(void);
80 extern void raw6_proc_exit(void);
81 extern int tcp6_proc_init(void);
82 extern void tcp6_proc_exit(void);
83 extern int udp6_proc_init(void);
84 extern void udp6_proc_exit(void);
85 extern int ipv6_misc_proc_init(void);
86 extern void ipv6_misc_proc_exit(void);
87 extern int ac6_proc_init(void);
88 extern void ac6_proc_exit(void);
89 extern int if6_proc_init(void);
90 extern void if6_proc_exit(void);
91 #endif
92
93 #ifdef CONFIG_SYSCTL
94 extern void ipv6_sysctl_register(void);
95 extern void ipv6_sysctl_unregister(void);
96 #endif
97
98 int sysctl_ipv6_bindv6only;
99
100 #ifdef INET_REFCNT_DEBUG
101 atomic_t inet6_sock_nr;
102 #endif
103
104 /* Per protocol sock slabcache */
105 kmem_cache_t *tcp6_sk_cachep;
106 kmem_cache_t *udp6_sk_cachep;
107 kmem_cache_t *raw6_sk_cachep;
108
109 /* The inetsw table contains everything that inet_create needs to
110  * build a new socket.
111  */
112 static struct list_head inetsw6[SOCK_MAX];
113 static spinlock_t inetsw6_lock = SPIN_LOCK_UNLOCKED;
114
115 static void inet6_sock_destruct(struct sock *sk)
116 {
117         inet_sock_destruct(sk);
118
119 #ifdef INET_REFCNT_DEBUG
120         atomic_dec(&inet6_sock_nr);
121 #endif
122 }
123
124 static __inline__ kmem_cache_t *inet6_sk_slab(int protocol)
125 {
126         kmem_cache_t* rc = tcp6_sk_cachep;
127
128         if (protocol == IPPROTO_UDP)
129                 rc = udp6_sk_cachep;
130         else if (protocol == IPPROTO_RAW)
131                 rc = raw6_sk_cachep;
132         return rc;
133 }
134
135 static __inline__ int inet6_sk_size(int protocol)
136 {
137         int rc = sizeof(struct tcp6_sock);
138
139         if (protocol == IPPROTO_UDP)
140                 rc = sizeof(struct udp6_sock);
141         else if (protocol == IPPROTO_RAW)
142                 rc = sizeof(struct raw6_sock);
143         return rc;
144 }
145
146 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
147 {
148         struct ipv6_pinfo *rc = (&((struct tcp6_sock *)sk)->inet6);
149
150         if (sk->sk_protocol == IPPROTO_UDP)
151                 rc = (&((struct udp6_sock *)sk)->inet6);
152         else if (sk->sk_protocol == IPPROTO_RAW)
153                 rc = (&((struct raw6_sock *)sk)->inet6);
154         return rc;
155 }
156
157 static int inet6_create(struct socket *sock, int protocol)
158 {
159         struct inet_opt *inet;
160         struct ipv6_pinfo *np;
161         struct sock *sk;
162         struct tcp6_sock* tcp6sk;
163         struct list_head *p;
164         struct inet_protosw *answer;
165
166         sk = sk_alloc(PF_INET6, GFP_KERNEL, inet6_sk_size(protocol),
167                       inet6_sk_slab(protocol));
168         if (sk == NULL) 
169                 goto do_oom;
170
171         /* Look for the requested type/protocol pair. */
172         answer = NULL;
173         rcu_read_lock();
174         list_for_each_rcu(p, &inetsw6[sock->type]) {
175                 answer = list_entry(p, struct inet_protosw, list);
176
177                 /* Check the non-wild match. */
178                 if (protocol == answer->protocol) {
179                         if (protocol != IPPROTO_IP)
180                                 break;
181                 } else {
182                         /* Check for the two wild cases. */
183                         if (IPPROTO_IP == protocol) {
184                                 protocol = answer->protocol;
185                                 break;
186                         }
187                         if (IPPROTO_IP == answer->protocol)
188                                 break;
189                 }
190                 answer = NULL;
191         }
192
193         if (!answer)
194                 goto free_and_badtype;
195         if (answer->capability > 0 && !capable(answer->capability))
196                 goto free_and_badperm;
197         if (!protocol)
198                 goto free_and_noproto;
199
200         sock->ops = answer->ops;
201         sock_init_data(sock, sk);
202         sk_set_owner(sk, THIS_MODULE);
203
204         sk->sk_prot = answer->prot;
205         sk->sk_no_check = answer->no_check;
206         if (INET_PROTOSW_REUSE & answer->flags)
207                 sk->sk_reuse = 1;
208         rcu_read_unlock();
209
210         inet = inet_sk(sk);
211
212         if (SOCK_RAW == sock->type) {
213                 inet->num = protocol;
214                 if (IPPROTO_RAW == protocol)
215                         inet->hdrincl = 1;
216         }
217
218         sk->sk_destruct         = inet6_sock_destruct;
219         sk->sk_zapped           = 0;
220         sk->sk_family           = PF_INET6;
221         sk->sk_protocol         = protocol;
222
223         sk->sk_backlog_rcv      = answer->prot->backlog_rcv;
224
225         tcp6sk          = (struct tcp6_sock *)sk;
226         tcp6sk->pinet6 = np = inet6_sk_generic(sk);
227         np->hop_limit   = -1;
228         np->mcast_hops  = -1;
229         np->mc_loop     = 1;
230         np->pmtudisc    = IPV6_PMTUDISC_WANT;
231         np->ipv6only    = sysctl_ipv6_bindv6only;
232         
233         /* Init the ipv4 part of the socket since we can have sockets
234          * using v6 API for ipv4.
235          */
236         inet->uc_ttl    = -1;
237
238         inet->mc_loop   = 1;
239         inet->mc_ttl    = 1;
240         inet->mc_index  = 0;
241         inet->mc_list   = NULL;
242
243         if (ipv4_config.no_pmtu_disc)
244                 inet->pmtudisc = IP_PMTUDISC_DONT;
245         else
246                 inet->pmtudisc = IP_PMTUDISC_WANT;
247
248
249 #ifdef INET_REFCNT_DEBUG
250         atomic_inc(&inet6_sock_nr);
251         atomic_inc(&inet_sock_nr);
252 #endif
253         if (inet->num) {
254                 /* It assumes that any protocol which allows
255                  * the user to assign a number at socket
256                  * creation time automatically shares.
257                  */
258                 inet->sport = ntohs(inet->num);
259                 sk->sk_prot->hash(sk);
260         }
261         if (sk->sk_prot->init) {
262                 int err = sk->sk_prot->init(sk);
263                 if (err != 0) {
264                         inet_sock_release(sk);
265                         return err;
266                 }
267         }
268         return 0;
269
270 free_and_badtype:
271         rcu_read_unlock();
272         sk_free(sk);
273         return -ESOCKTNOSUPPORT;
274 free_and_badperm:
275         rcu_read_unlock();
276         sk_free(sk);
277         return -EPERM;
278 free_and_noproto:
279         rcu_read_unlock();
280         sk_free(sk);
281         return -EPROTONOSUPPORT;
282 do_oom:
283         return -ENOBUFS;
284 }
285
286
287 /* bind for INET6 API */
288 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
289 {
290         struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
291         struct sock *sk = sock->sk;
292         struct inet_opt *inet = inet_sk(sk);
293         struct ipv6_pinfo *np = inet6_sk(sk);
294         __u32 v4addr = 0;
295         unsigned short snum;
296         int addr_type = 0;
297         int err = 0;
298
299         /* If the socket has its own bind function then use it. */
300         if (sk->sk_prot->bind)
301                 return sk->sk_prot->bind(sk, uaddr, addr_len);
302
303         if (addr_len < SIN6_LEN_RFC2133)
304                 return -EINVAL;
305         addr_type = ipv6_addr_type(&addr->sin6_addr);
306         if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
307                 return -EINVAL;
308
309         snum = ntohs(addr->sin6_port);
310         if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
311                 return -EACCES;
312
313         lock_sock(sk);
314
315         /* Check these errors (active socket, double bind). */
316         if (sk->sk_state != TCP_CLOSE || inet->num) {
317                 err = -EINVAL;
318                 goto out;
319         }
320
321         /* Check if the address belongs to the host. */
322         if (addr_type == IPV6_ADDR_MAPPED) {
323                 v4addr = addr->sin6_addr.s6_addr32[3];
324                 if (inet_addr_type(v4addr) != RTN_LOCAL) {
325                         err = -EADDRNOTAVAIL;
326                         goto out;
327                 }
328         } else {
329                 if (addr_type != IPV6_ADDR_ANY) {
330                         struct net_device *dev = NULL;
331
332                         if (addr_type & IPV6_ADDR_LINKLOCAL) {
333                                 if (addr_len >= sizeof(struct sockaddr_in6) &&
334                                     addr->sin6_scope_id) {
335                                         /* Override any existing binding, if another one
336                                          * is supplied by user.
337                                          */
338                                         sk->sk_bound_dev_if = addr->sin6_scope_id;
339                                 }
340                                 
341                                 /* Binding to link-local address requires an interface */
342                                 if (!sk->sk_bound_dev_if) {
343                                         err = -EINVAL;
344                                         goto out;
345                                 }
346                                 dev = dev_get_by_index(sk->sk_bound_dev_if);
347                                 if (!dev) {
348                                         err = -ENODEV;
349                                         goto out;
350                                 }
351                         }
352
353                         /* ipv4 addr of the socket is invalid.  Only the
354                          * unspecified and mapped address have a v4 equivalent.
355                          */
356                         v4addr = LOOPBACK4_IPV6;
357                         if (!(addr_type & IPV6_ADDR_MULTICAST)) {
358                                 if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
359                                         if (dev)
360                                                 dev_put(dev);
361                                         err = -EADDRNOTAVAIL;
362                                         goto out;
363                                 }
364                         }
365                         if (dev)
366                                 dev_put(dev);
367                 }
368         }
369
370         inet->rcv_saddr = v4addr;
371         inet->saddr = v4addr;
372
373         ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
374                 
375         if (!(addr_type & IPV6_ADDR_MULTICAST))
376                 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
377
378         /* Make sure we are allowed to bind here. */
379         if (sk->sk_prot->get_port(sk, snum)) {
380                 inet_reset_saddr(sk);
381                 err = -EADDRINUSE;
382                 goto out;
383         }
384
385         if (addr_type != IPV6_ADDR_ANY)
386                 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
387         if (snum)
388                 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
389         inet->sport = ntohs(inet->num);
390         inet->dport = 0;
391         inet->daddr = 0;
392 out:
393         release_sock(sk);
394         return err;
395 }
396
397 int inet6_release(struct socket *sock)
398 {
399         struct sock *sk = sock->sk;
400
401         if (sk == NULL)
402                 return -EINVAL;
403
404         /* Free mc lists */
405         ipv6_sock_mc_close(sk);
406
407         /* Free ac lists */
408         ipv6_sock_ac_close(sk);
409
410         return inet_release(sock);
411 }
412
413 int inet6_destroy_sock(struct sock *sk)
414 {
415         struct ipv6_pinfo *np = inet6_sk(sk);
416         struct sk_buff *skb;
417         struct ipv6_txoptions *opt;
418
419         /*
420          *      Release destination entry
421          */
422
423         sk_dst_reset(sk);
424
425         /* Release rx options */
426
427         if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
428                 kfree_skb(skb);
429
430         /* Free flowlabels */
431         fl6_free_socklist(sk);
432
433         /* Free tx options */
434
435         if ((opt = xchg(&np->opt, NULL)) != NULL)
436                 sock_kfree_s(sk, opt, opt->tot_len);
437
438         return 0;
439 }
440
441 /*
442  *      This does both peername and sockname.
443  */
444  
445 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
446                  int *uaddr_len, int peer)
447 {
448         struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
449         struct sock *sk = sock->sk;
450         struct inet_opt *inet = inet_sk(sk);
451         struct ipv6_pinfo *np = inet6_sk(sk);
452   
453         sin->sin6_family = AF_INET6;
454         sin->sin6_flowinfo = 0;
455         sin->sin6_scope_id = 0;
456         if (peer) {
457                 if (!inet->dport)
458                         return -ENOTCONN;
459                 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
460                     peer == 1)
461                         return -ENOTCONN;
462                 sin->sin6_port = inet->dport;
463                 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
464                 if (np->sndflow)
465                         sin->sin6_flowinfo = np->flow_label;
466         } else {
467                 if (ipv6_addr_any(&np->rcv_saddr))
468                         ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
469                 else
470                         ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
471
472                 sin->sin6_port = inet->sport;
473         }
474         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
475                 sin->sin6_scope_id = sk->sk_bound_dev_if;
476         *uaddr_len = sizeof(*sin);
477         return(0);
478 }
479
480 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
481 {
482         struct sock *sk = sock->sk;
483         int err = -EINVAL;
484
485         switch(cmd) 
486         {
487         case SIOCGSTAMP:
488                 if (!sk->sk_stamp.tv_sec)
489                         return -ENOENT;
490                 err = copy_to_user((void *)arg, &sk->sk_stamp,
491                                    sizeof(struct timeval));
492                 if (err)
493                         return -EFAULT;
494                 return 0;
495
496         case SIOCADDRT:
497         case SIOCDELRT:
498           
499                 return(ipv6_route_ioctl(cmd,(void *)arg));
500
501         case SIOCSIFADDR:
502                 return addrconf_add_ifaddr((void *) arg);
503         case SIOCDIFADDR:
504                 return addrconf_del_ifaddr((void *) arg);
505         case SIOCSIFDSTADDR:
506                 return addrconf_set_dstaddr((void *) arg);
507         default:
508                 if (!sk->sk_prot->ioctl ||
509                     (err = sk->sk_prot->ioctl(sk, cmd, arg)) == -ENOIOCTLCMD)
510                         return(dev_ioctl(cmd,(void *) arg));            
511                 return err;
512         }
513         /*NOTREACHED*/
514         return(0);
515 }
516
517 struct proto_ops inet6_stream_ops = {
518         .family =       PF_INET6,
519         .owner =        THIS_MODULE,
520         .release =      inet6_release,
521         .bind =         inet6_bind,
522         .connect =      inet_stream_connect,            /* ok           */
523         .socketpair =   sock_no_socketpair,             /* a do nothing */
524         .accept =       inet_accept,                    /* ok           */
525         .getname =      inet6_getname, 
526         .poll =         tcp_poll,                       /* ok           */
527         .ioctl =        inet6_ioctl,                    /* must change  */
528         .listen =       inet_listen,                    /* ok           */
529         .shutdown =     inet_shutdown,                  /* ok           */
530         .setsockopt =   inet_setsockopt,                /* ok           */
531         .getsockopt =   inet_getsockopt,                /* ok           */
532         .sendmsg =      inet_sendmsg,                   /* ok           */
533         .recvmsg =      inet_recvmsg,                   /* ok           */
534         .mmap =         sock_no_mmap,
535         .sendpage =     tcp_sendpage
536 };
537
538 struct proto_ops inet6_dgram_ops = {
539         .family =       PF_INET6,
540         .owner =        THIS_MODULE,
541         .release =      inet6_release,
542         .bind =         inet6_bind,
543         .connect =      inet_dgram_connect,             /* ok           */
544         .socketpair =   sock_no_socketpair,             /* a do nothing */
545         .accept =       sock_no_accept,                 /* a do nothing */
546         .getname =      inet6_getname, 
547         .poll =         datagram_poll,                  /* ok           */
548         .ioctl =        inet6_ioctl,                    /* must change  */
549         .listen =       sock_no_listen,                 /* ok           */
550         .shutdown =     inet_shutdown,                  /* ok           */
551         .setsockopt =   inet_setsockopt,                /* ok           */
552         .getsockopt =   inet_getsockopt,                /* ok           */
553         .sendmsg =      inet_sendmsg,                   /* ok           */
554         .recvmsg =      inet_recvmsg,                   /* ok           */
555         .mmap =         sock_no_mmap,
556         .sendpage =     sock_no_sendpage,
557 };
558
559 struct net_proto_family inet6_family_ops = {
560         .family = PF_INET6,
561         .create = inet6_create,
562         .owner  = THIS_MODULE,
563 };
564
565 #ifdef MODULE
566 #if 0 /* FIXME --RR */
567 int ipv6_unload(void)
568 {
569         if (!unloadable) return 1;
570         /* We keep internally 3 raw sockets */
571         return atomic_read(&(__this_module.uc.usecount)) - 3;
572 }
573 #endif
574 #endif
575
576 #if defined(MODULE) && defined(CONFIG_SYSCTL)
577 extern void ipv6_sysctl_register(void);
578 extern void ipv6_sysctl_unregister(void);
579 #endif
580
581 static struct inet_protosw rawv6_protosw = {
582         .type           = SOCK_RAW,
583         .protocol       = IPPROTO_IP,   /* wild card */
584         .prot           = &rawv6_prot,
585         .ops            = &inet6_dgram_ops,
586         .capability     = CAP_NET_RAW,
587         .no_check       = UDP_CSUM_DEFAULT,
588         .flags          = INET_PROTOSW_REUSE,
589 };
590
591 #define INETSW6_ARRAY_LEN (sizeof(inetsw6_array) / sizeof(struct inet_protosw))
592
593 void
594 inet6_register_protosw(struct inet_protosw *p)
595 {
596         struct list_head *lh;
597         struct inet_protosw *answer;
598         int protocol = p->protocol;
599         struct list_head *last_perm;
600
601         spin_lock_bh(&inetsw6_lock);
602
603         if (p->type > SOCK_MAX)
604                 goto out_illegal;
605
606         /* If we are trying to override a permanent protocol, bail. */
607         answer = NULL;
608         last_perm = &inetsw6[p->type];
609         list_for_each(lh, &inetsw6[p->type]) {
610                 answer = list_entry(lh, struct inet_protosw, list);
611
612                 /* Check only the non-wild match. */
613                 if (INET_PROTOSW_PERMANENT & answer->flags) {
614                         if (protocol == answer->protocol)
615                                 break;
616                         last_perm = lh;
617                 }
618
619                 answer = NULL;
620         }
621         if (answer)
622                 goto out_permanent;
623
624         /* Add the new entry after the last permanent entry if any, so that
625          * the new entry does not override a permanent entry when matched with
626          * a wild-card protocol. But it is allowed to override any existing
627          * non-permanent entry.  This means that when we remove this entry, the 
628          * system automatically returns to the old behavior.
629          */
630         list_add_rcu(&p->list, last_perm);
631 out:
632         spin_unlock_bh(&inetsw6_lock);
633         return;
634
635 out_permanent:
636         printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
637                protocol);
638         goto out;
639
640 out_illegal:
641         printk(KERN_ERR
642                "Ignoring attempt to register invalid socket type %d.\n",
643                p->type);
644         goto out;
645 }
646
647 void
648 inet6_unregister_protosw(struct inet_protosw *p)
649 {
650         if (INET_PROTOSW_PERMANENT & p->flags) {
651                 printk(KERN_ERR
652                        "Attempt to unregister permanent protocol %d.\n",
653                        p->protocol);
654         } else {
655                 spin_lock_bh(&inetsw6_lock);
656                 list_del_rcu(&p->list);
657                 spin_unlock_bh(&inetsw6_lock);
658
659                 synchronize_net();
660         }
661 }
662
663 int
664 snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
665 {
666         if (ptr == NULL)
667                 return -EINVAL;
668
669         ptr[0] = __alloc_percpu(mibsize, mibalign);
670         if (!ptr[0])
671                 goto err0;
672
673         ptr[1] = __alloc_percpu(mibsize, mibalign);
674         if (!ptr[1])
675                 goto err1;
676
677         return 0;
678
679 err1:
680         free_percpu(ptr[0]);
681         ptr[0] = NULL;
682 err0:
683         return -ENOMEM;
684 }
685
686 void
687 snmp6_mib_free(void *ptr[2])
688 {
689         if (ptr == NULL)
690                 return;
691         free_percpu(ptr[0]);
692         free_percpu(ptr[1]);
693         ptr[0] = ptr[1] = NULL;
694 }
695
696 static int __init init_ipv6_mibs(void)
697 {
698         if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipv6_mib),
699                            __alignof__(struct ipv6_mib)) < 0)
700                 goto err_ip_mib;
701         if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
702                            __alignof__(struct ipv6_mib)) < 0)
703                 goto err_icmp_mib;
704         if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
705                            __alignof__(struct ipv6_mib)) < 0)
706                 goto err_udp_mib;
707         return 0;
708
709 err_udp_mib:
710         snmp6_mib_free((void **)icmpv6_statistics);
711 err_icmp_mib:
712         snmp6_mib_free((void **)ipv6_statistics);
713 err_ip_mib:
714         return -ENOMEM;
715         
716 }
717
718 static void cleanup_ipv6_mibs(void)
719 {
720         snmp6_mib_free((void **)ipv6_statistics);
721         snmp6_mib_free((void **)icmpv6_statistics);
722         snmp6_mib_free((void **)udp_stats_in6);
723 }
724
725 extern int ipv6_misc_proc_init(void);
726
727 static int __init inet6_init(void)
728 {
729         struct sk_buff *dummy_skb;
730         struct list_head *r;
731         int err;
732
733 #ifdef MODULE
734 #if 0 /* FIXME --RR */
735         if (!mod_member_present(&__this_module, can_unload))
736           return -EINVAL;
737
738         __this_module.can_unload = &ipv6_unload;
739 #endif
740 #endif
741
742         if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb))
743         {
744                 printk(KERN_CRIT "inet6_proto_init: size fault\n");
745                 return -EINVAL;
746         }
747         /* allocate our sock slab caches */
748         tcp6_sk_cachep = kmem_cache_create("tcp6_sock",
749                                            sizeof(struct tcp6_sock), 0,
750                                            SLAB_HWCACHE_ALIGN, 0, 0);
751         udp6_sk_cachep = kmem_cache_create("udp6_sock",
752                                            sizeof(struct udp6_sock), 0,
753                                            SLAB_HWCACHE_ALIGN, 0, 0);
754         raw6_sk_cachep = kmem_cache_create("raw6_sock",
755                                            sizeof(struct raw6_sock), 0,
756                                            SLAB_HWCACHE_ALIGN, 0, 0);
757         if (!tcp6_sk_cachep || !udp6_sk_cachep || !raw6_sk_cachep)
758                 printk(KERN_CRIT "%s: Can't create protocol sock SLAB "
759                        "caches!\n", __FUNCTION__);
760
761         /* Register the socket-side information for inet6_create.  */
762         for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
763                 INIT_LIST_HEAD(r);
764
765         /* We MUST register RAW sockets before we create the ICMP6,
766          * IGMP6, or NDISC control sockets.
767          */
768         inet6_register_protosw(&rawv6_protosw);
769
770         /* Register the family here so that the init calls below will
771          * be able to create sockets. (?? is this dangerous ??)
772          */
773         (void) sock_register(&inet6_family_ops);
774
775         /* Initialise ipv6 mibs */
776         err = init_ipv6_mibs();
777         if (err)
778                 goto init_mib_fail;
779         
780         /*
781          *      ipngwg API draft makes clear that the correct semantics
782          *      for TCP and UDP is to consider one TCP and UDP instance
783          *      in a host availiable by both INET and INET6 APIs and
784          *      able to communicate via both network protocols.
785          */
786
787 #if defined(MODULE) && defined(CONFIG_SYSCTL)
788         ipv6_sysctl_register();
789 #endif
790         err = icmpv6_init(&inet6_family_ops);
791         if (err)
792                 goto icmp_fail;
793         err = ndisc_init(&inet6_family_ops);
794         if (err)
795                 goto ndisc_fail;
796 #ifdef CONFIG_IPV6_TUNNEL
797         err = ip6_tunnel_init();
798         if (err)
799                 goto ip6_tunnel_fail;
800 #endif
801         err = igmp6_init(&inet6_family_ops);
802         if (err)
803                 goto igmp_fail;
804         /* Create /proc/foo6 entries. */
805 #ifdef CONFIG_PROC_FS
806         err = -ENOMEM;
807         if (raw6_proc_init())
808                 goto proc_raw6_fail;
809         if (tcp6_proc_init())
810                 goto proc_tcp6_fail;
811         if (udp6_proc_init())
812                 goto proc_udp6_fail;
813         if (ipv6_misc_proc_init())
814                 goto proc_misc6_fail;
815
816         if (ac6_proc_init())
817                 goto proc_anycast6_fail;
818         if (if6_proc_init())
819                 goto proc_if6_fail;
820 #endif
821         ipv6_packet_init();
822         ip6_route_init();
823         ip6_flowlabel_init();
824         addrconf_init();
825         sit_init();
826
827         /* Init v6 extension headers. */
828         ipv6_rthdr_init();
829         ipv6_frag_init();
830         ipv6_nodata_init();
831         ipv6_destopt_init();
832
833         /* Init v6 transport protocols. */
834         udpv6_init();
835         tcpv6_init();
836
837         return 0;
838
839 #ifdef CONFIG_PROC_FS
840 proc_if6_fail:
841         ac6_proc_exit();
842 proc_anycast6_fail:
843         ipv6_misc_proc_exit();
844 proc_misc6_fail:
845         udp6_proc_exit();
846 proc_udp6_fail:
847         tcp6_proc_exit();
848 proc_tcp6_fail:
849         raw6_proc_exit();
850 proc_raw6_fail:
851         igmp6_cleanup();
852 #endif
853 igmp_fail:
854 #ifdef CONFIG_IPV6_TUNNEL
855         ip6_tunnel_cleanup();
856 ip6_tunnel_fail:
857 #endif
858         ndisc_cleanup();
859 ndisc_fail:
860         icmpv6_cleanup();
861 icmp_fail:
862 #if defined(MODULE) && defined(CONFIG_SYSCTL)
863         ipv6_sysctl_unregister();
864 #endif
865         cleanup_ipv6_mibs();
866 init_mib_fail:
867         return err;
868 }
869 module_init(inet6_init);
870
871
872 #ifdef MODULE
873 static void inet6_exit(void)
874 {
875         /* First of all disallow new sockets creation. */
876         sock_unregister(PF_INET6);
877 #ifdef CONFIG_PROC_FS
878         if6_proc_exit();
879         ac6_proc_exit();
880         ipv6_misc_proc_exit();
881         udp6_proc_exit();
882         tcp6_proc_exit();
883         raw6_proc_exit();
884 #endif
885         /* Cleanup code parts. */
886         sit_cleanup();
887         ip6_flowlabel_cleanup();
888         addrconf_cleanup();
889         ip6_route_cleanup();
890         ipv6_packet_cleanup();
891         igmp6_cleanup();
892 #ifdef CONFIG_IPV6_TUNNEL
893         ip6_tunnel_cleanup();
894 #endif
895         ndisc_cleanup();
896         icmpv6_cleanup();
897 #ifdef CONFIG_SYSCTL
898         ipv6_sysctl_unregister();       
899 #endif
900         cleanup_ipv6_mibs();
901         kmem_cache_destroy(tcp6_sk_cachep);
902         kmem_cache_destroy(udp6_sk_cachep);
903         kmem_cache_destroy(raw6_sk_cachep);
904 }
905 module_exit(inet6_exit);
906 #endif /* MODULE */
907
908 MODULE_ALIAS_NETPROTO(PF_INET6);