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