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