netns: add stub functions for per/namespace mibs allocation
[safe/jmp/linux-2.6] / net / ipv6 / af_inet6.c
index 218b8b3..e8f82ec 100644 (file)
@@ -7,8 +7,6 @@
  *
  *     Adapted from linux/net/ipv4/af_inet.c
  *
- *     $Id: af_inet6.c,v 1.66 2002/02/01 22:01:04 davem Exp $
- *
  *     Fixes:
  *     piggy, Karl Knutson     :       Socket protocol table
  *     Hideaki YOSHIFUJI       :       sin6_scope_id support
@@ -52,6 +50,7 @@
 #include <net/ipip.h>
 #include <net/protocol.h>
 #include <net/inet_common.h>
+#include <net/route.h>
 #include <net/transp_v6.h>
 #include <net/ip6_route.h>
 #include <net/addrconf.h>
@@ -61,6 +60,7 @@
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
+#include <linux/mroute6.h>
 
 MODULE_AUTHOR("Cast of dozens");
 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
@@ -72,8 +72,6 @@ MODULE_LICENSE("GPL");
 static struct list_head inetsw6[SOCK_MAX];
 static DEFINE_SPINLOCK(inetsw6_lock);
 
-void ipv6_frag_sysctl_init(struct net *net);
-
 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
 {
        const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
@@ -86,7 +84,6 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol)
        struct inet_sock *inet;
        struct ipv6_pinfo *np;
        struct sock *sk;
-       struct list_head *p;
        struct inet_protosw *answer;
        struct proto *answer_prot;
        unsigned char answer_flags;
@@ -94,22 +91,18 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol)
        int try_loading_module = 0;
        int err;
 
-       if (net != &init_net)
-               return -EAFNOSUPPORT;
-
        if (sock->type != SOCK_RAW &&
            sock->type != SOCK_DGRAM &&
            !inet_ehash_secret)
                build_ehash_secret();
 
        /* Look for the requested type/protocol pair. */
-       answer = NULL;
 lookup_protocol:
        err = -ESOCKTNOSUPPORT;
        rcu_read_lock();
-       list_for_each_rcu(p, &inetsw6[sock->type]) {
-               answer = list_entry(p, struct inet_protosw, list);
+       list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
 
+               err = 0;
                /* Check the non-wild match. */
                if (protocol == answer->protocol) {
                        if (protocol != IPPROTO_IP)
@@ -124,10 +117,9 @@ lookup_protocol:
                                break;
                }
                err = -EPROTONOSUPPORT;
-               answer = NULL;
        }
 
-       if (!answer) {
+       if (err) {
                if (try_loading_module < 2) {
                        rcu_read_unlock();
                        /*
@@ -159,7 +151,7 @@ lookup_protocol:
        answer_flags = answer->flags;
        rcu_read_unlock();
 
-       BUG_TRAP(answer_prot->slab != NULL);
+       WARN_ON(answer_prot->slab == NULL);
 
        err = -ENOBUFS;
        sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
@@ -193,7 +185,7 @@ lookup_protocol:
        np->mcast_hops  = -1;
        np->mc_loop     = 1;
        np->pmtudisc    = IPV6_PMTUDISC_WANT;
-       np->ipv6only    = init_net.ipv6.sysctl.bindv6only;
+       np->ipv6only    = net->ipv6.sysctl.bindv6only;
 
        /* Init the ipv4 part of the socket since we can have sockets
         * using v6 API for ipv4.
@@ -250,6 +242,7 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
        struct sock *sk = sock->sk;
        struct inet_sock *inet = inet_sk(sk);
        struct ipv6_pinfo *np = inet6_sk(sk);
+       struct net *net = sock_net(sk);
        __be32 v4addr = 0;
        unsigned short snum;
        int addr_type = 0;
@@ -280,7 +273,7 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
        /* Check if the address belongs to the host. */
        if (addr_type == IPV6_ADDR_MAPPED) {
                v4addr = addr->sin6_addr.s6_addr32[3];
-               if (inet_addr_type(v4addr) != RTN_LOCAL) {
+               if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
                        err = -EADDRNOTAVAIL;
                        goto out;
                }
@@ -302,7 +295,7 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
                                        err = -EINVAL;
                                        goto out;
                                }
-                               dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
+                               dev = dev_get_by_index(net, sk->sk_bound_dev_if);
                                if (!dev) {
                                        err = -ENODEV;
                                        goto out;
@@ -314,7 +307,8 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
                         */
                        v4addr = LOOPBACK4_IPV6;
                        if (!(addr_type & IPV6_ADDR_MULTICAST)) {
-                               if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
+                               if (!ipv6_chk_addr(net, &addr->sin6_addr,
+                                                  dev, 0)) {
                                        if (dev)
                                                dev_put(dev);
                                        err = -EADDRNOTAVAIL;
@@ -373,7 +367,7 @@ int inet6_release(struct socket *sock)
 
 EXPORT_SYMBOL(inet6_release);
 
-int inet6_destroy_sock(struct sock *sk)
+void inet6_destroy_sock(struct sock *sk)
 {
        struct ipv6_pinfo *np = inet6_sk(sk);
        struct sk_buff *skb;
@@ -391,8 +385,6 @@ int inet6_destroy_sock(struct sock *sk)
 
        if ((opt = xchg(&np->opt, NULL)) != NULL)
                sock_kfree_s(sk, opt, opt->tot_len);
-
-       return 0;
 }
 
 EXPORT_SYMBOL_GPL(inet6_destroy_sock);
@@ -441,6 +433,7 @@ EXPORT_SYMBOL(inet6_getname);
 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 {
        struct sock *sk = sock->sk;
+       struct net *net = sock_net(sk);
 
        switch(cmd)
        {
@@ -453,14 +446,14 @@ int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
        case SIOCADDRT:
        case SIOCDELRT:
 
-               return(ipv6_route_ioctl(cmd,(void __user *)arg));
+               return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
 
        case SIOCSIFADDR:
-               return addrconf_add_ifaddr((void __user *) arg);
+               return addrconf_add_ifaddr(net, (void __user *) arg);
        case SIOCDIFADDR:
-               return addrconf_del_ifaddr((void __user *) arg);
+               return addrconf_del_ifaddr(net, (void __user *) arg);
        case SIOCSIFDSTADDR:
-               return addrconf_set_dstaddr((void __user *) arg);
+               return addrconf_set_dstaddr(net, (void __user *) arg);
        default:
                if (!sk->sk_prot->ioctl)
                        return -ENOIOCTLCMD;
@@ -679,6 +672,129 @@ int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
 
 EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
 
+static struct inet6_protocol *ipv6_gso_pull_exthdrs(struct sk_buff *skb,
+                                                   int proto)
+{
+       struct inet6_protocol *ops = NULL;
+
+       for (;;) {
+               struct ipv6_opt_hdr *opth;
+               int len;
+
+               if (proto != NEXTHDR_HOP) {
+                       ops = rcu_dereference(inet6_protos[proto]);
+
+                       if (unlikely(!ops))
+                               break;
+
+                       if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
+                               break;
+               }
+
+               if (unlikely(!pskb_may_pull(skb, 8)))
+                       break;
+
+               opth = (void *)skb->data;
+               len = ipv6_optlen(opth);
+
+               if (unlikely(!pskb_may_pull(skb, len)))
+                       break;
+
+               proto = opth->nexthdr;
+               __skb_pull(skb, len);
+       }
+
+       return ops;
+}
+
+static int ipv6_gso_send_check(struct sk_buff *skb)
+{
+       struct ipv6hdr *ipv6h;
+       struct inet6_protocol *ops;
+       int err = -EINVAL;
+
+       if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
+               goto out;
+
+       ipv6h = ipv6_hdr(skb);
+       __skb_pull(skb, sizeof(*ipv6h));
+       err = -EPROTONOSUPPORT;
+
+       rcu_read_lock();
+       ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
+       if (likely(ops && ops->gso_send_check)) {
+               skb_reset_transport_header(skb);
+               err = ops->gso_send_check(skb);
+       }
+       rcu_read_unlock();
+
+out:
+       return err;
+}
+
+static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
+{
+       struct sk_buff *segs = ERR_PTR(-EINVAL);
+       struct ipv6hdr *ipv6h;
+       struct inet6_protocol *ops;
+
+       if (!(features & NETIF_F_V6_CSUM))
+               features &= ~NETIF_F_SG;
+
+       if (unlikely(skb_shinfo(skb)->gso_type &
+                    ~(SKB_GSO_UDP |
+                      SKB_GSO_DODGY |
+                      SKB_GSO_TCP_ECN |
+                      SKB_GSO_TCPV6 |
+                      0)))
+               goto out;
+
+       if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
+               goto out;
+
+       ipv6h = ipv6_hdr(skb);
+       __skb_pull(skb, sizeof(*ipv6h));
+       segs = ERR_PTR(-EPROTONOSUPPORT);
+
+       rcu_read_lock();
+       ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
+       if (likely(ops && ops->gso_segment)) {
+               skb_reset_transport_header(skb);
+               segs = ops->gso_segment(skb, features);
+       }
+       rcu_read_unlock();
+
+       if (unlikely(IS_ERR(segs)))
+               goto out;
+
+       for (skb = segs; skb; skb = skb->next) {
+               ipv6h = ipv6_hdr(skb);
+               ipv6h->payload_len = htons(skb->len - skb->mac_len -
+                                          sizeof(*ipv6h));
+       }
+
+out:
+       return segs;
+}
+
+static struct packet_type ipv6_packet_type = {
+       .type = __constant_htons(ETH_P_IPV6),
+       .func = ipv6_rcv,
+       .gso_send_check = ipv6_gso_send_check,
+       .gso_segment = ipv6_gso_segment,
+};
+
+static int __init ipv6_packet_init(void)
+{
+       dev_add_pack(&ipv6_packet_type);
+       return 0;
+}
+
+static void ipv6_packet_cleanup(void)
+{
+       dev_remove_pack(&ipv6_packet_type);
+}
+
 static int __init init_ipv6_mibs(void)
 {
        if (snmp_mib_init((void **)ipv6_statistics,
@@ -719,30 +835,57 @@ static void cleanup_ipv6_mibs(void)
        snmp_mib_free((void **)udplite_stats_in6);
 }
 
+static int __net_init ipv6_init_mibs(struct net *net)
+{
+       return 0;
+}
+
+static void __net_exit ipv6_cleanup_mibs(struct net *net)
+{
+}
+
 static int inet6_net_init(struct net *net)
 {
+       int err = 0;
+
        net->ipv6.sysctl.bindv6only = 0;
-       net->ipv6.sysctl.frags.high_thresh = 256 * 1024;
-       net->ipv6.sysctl.frags.low_thresh = 192 * 1024;
-       net->ipv6.sysctl.frags.timeout = IPV6_FRAG_TIMEOUT;
-       net->ipv6.sysctl.frags.secret_interval = 10 * 60 * HZ;
-       net->ipv6.sysctl.flush_delay = 0;
-       net->ipv6.sysctl.ip6_rt_max_size = 4096;
-       net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
-       net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
-       net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
-       net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
-       net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
-       net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
        net->ipv6.sysctl.icmpv6_time = 1*HZ;
-       ipv6_frag_sysctl_init(net);
 
-       return 0;
+       err = ipv6_init_mibs(net);
+       if (err)
+               return err;
+#ifdef CONFIG_PROC_FS
+       err = udp6_proc_init(net);
+       if (err)
+               goto out;
+       err = tcp6_proc_init(net);
+       if (err)
+               goto proc_tcp6_fail;
+       err = ac6_proc_init(net);
+       if (err)
+               goto proc_ac6_fail;
+#endif
+       return err;
+
+#ifdef CONFIG_PROC_FS
+proc_ac6_fail:
+       tcp6_proc_exit(net);
+proc_tcp6_fail:
+       udp6_proc_exit(net);
+out:
+       ipv6_cleanup_mibs(net);
+       return err;
+#endif
 }
 
 static void inet6_net_exit(struct net *net)
 {
-       return;
+#ifdef CONFIG_PROC_FS
+       udp6_proc_exit(net);
+       tcp6_proc_exit(net);
+       ac6_proc_exit(net);
+#endif
+       ipv6_cleanup_mibs(net);
 }
 
 static struct pernet_operations inet6_net_ops = {
@@ -758,14 +901,6 @@ static int __init inet6_init(void)
 
        BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
 
-#ifdef MODULE
-#if 0 /* FIXME --RR */
-       if (!mod_member_present(&__this_module, can_unload))
-         return -EINVAL;
-
-       __this_module.can_unload = &ipv6_unload;
-#endif
-#endif
        err = proto_register(&tcpv6_prot, 1);
        if (err)
                goto out;
@@ -806,6 +941,11 @@ static int __init inet6_init(void)
        if (err)
                goto out_unregister_sock;
 
+#ifdef CONFIG_SYSCTL
+       err = ipv6_static_sysctl_register();
+       if (err)
+               goto static_sysctl_fail;
+#endif
        /*
         *      ipngwg API draft makes clear that the correct semantics
         *      for TCP and UDP is to consider one TCP and UDP instance
@@ -816,19 +956,16 @@ static int __init inet6_init(void)
        err = register_pernet_subsys(&inet6_net_ops);
        if (err)
                goto register_pernet_fail;
-
-#ifdef CONFIG_SYSCTL
-       err = ipv6_sysctl_register();
-       if (err)
-               goto sysctl_fail;
-#endif
-       err = icmpv6_init(&inet6_family_ops);
+       err = icmpv6_init();
        if (err)
                goto icmp_fail;
-       err = ndisc_init(&inet6_family_ops);
+       err = ip6_mr_init();
+       if (err)
+               goto ipmr_fail;
+       err = ndisc_init();
        if (err)
                goto ndisc_fail;
-       err = igmp6_init(&inet6_family_ops);
+       err = igmp6_init();
        if (err)
                goto igmp_fail;
        err = ipv6_netfilter_init();
@@ -839,17 +976,10 @@ static int __init inet6_init(void)
        err = -ENOMEM;
        if (raw6_proc_init())
                goto proc_raw6_fail;
-       if (tcp6_proc_init())
-               goto proc_tcp6_fail;
-       if (udp6_proc_init())
-               goto proc_udp6_fail;
        if (udplite6_proc_init())
                goto proc_udplite6_fail;
        if (ipv6_misc_proc_init())
                goto proc_misc6_fail;
-
-       if (ac6_proc_init())
-               goto proc_anycast6_fail;
        if (if6_proc_init())
                goto proc_if6_fail;
 #endif
@@ -888,9 +1018,19 @@ static int __init inet6_init(void)
        err = ipv6_packet_init();
        if (err)
                goto ipv6_packet_fail;
+
+#ifdef CONFIG_SYSCTL
+       err = ipv6_sysctl_register();
+       if (err)
+               goto sysctl_fail;
+#endif
 out:
        return err;
 
+#ifdef CONFIG_SYSCTL
+sysctl_fail:
+       ipv6_packet_cleanup();
+#endif
 ipv6_packet_fail:
        tcpv6_exit();
 tcpv6_fail:
@@ -911,16 +1051,10 @@ ip6_route_fail:
 #ifdef CONFIG_PROC_FS
        if6_proc_exit();
 proc_if6_fail:
-       ac6_proc_exit();
-proc_anycast6_fail:
        ipv6_misc_proc_exit();
 proc_misc6_fail:
        udplite6_proc_exit();
 proc_udplite6_fail:
-       udp6_proc_exit();
-proc_udp6_fail:
-       tcp6_proc_exit();
-proc_tcp6_fail:
        raw6_proc_exit();
 proc_raw6_fail:
 #endif
@@ -930,14 +1064,16 @@ netfilter_fail:
 igmp_fail:
        ndisc_cleanup();
 ndisc_fail:
+       ip6_mr_cleanup();
+ipmr_fail:
        icmpv6_cleanup();
 icmp_fail:
-#ifdef CONFIG_SYSCTL
-       ipv6_sysctl_unregister();
-sysctl_fail:
-#endif
        unregister_pernet_subsys(&inet6_net_ops);
 register_pernet_fail:
+#ifdef CONFIG_SYSCTL
+       ipv6_static_sysctl_unregister();
+static_sysctl_fail:
+#endif
        cleanup_ipv6_mibs();
 out_unregister_sock:
        sock_unregister(PF_INET6);
@@ -963,6 +1099,9 @@ static void __exit inet6_exit(void)
        /* Disallow any further netlink messages */
        rtnl_unregister_all(PF_INET6);
 
+#ifdef CONFIG_SYSCTL
+       ipv6_sysctl_unregister();
+#endif
        udpv6_exit();
        udplitev6_exit();
        tcpv6_exit();
@@ -978,22 +1117,21 @@ static void __exit inet6_exit(void)
 
        /* Cleanup code parts. */
        if6_proc_exit();
-       ac6_proc_exit();
        ipv6_misc_proc_exit();
        udplite6_proc_exit();
-       udp6_proc_exit();
-       tcp6_proc_exit();
        raw6_proc_exit();
 #endif
        ipv6_netfilter_fini();
        igmp6_cleanup();
        ndisc_cleanup();
+       ip6_mr_cleanup();
        icmpv6_cleanup();
        rawv6_exit();
+
+       unregister_pernet_subsys(&inet6_net_ops);
 #ifdef CONFIG_SYSCTL
-       ipv6_sysctl_unregister();
+       ipv6_static_sysctl_unregister();
 #endif
-       unregister_pernet_subsys(&inet6_net_ops);
        cleanup_ipv6_mibs();
        proto_unregister(&rawv6_prot);
        proto_unregister(&udplitev6_prot);