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