]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - net/ipv4/fib_frontend.c
[NETNS]: Refactor fib initialization so it can handle multiple namespaces.
[linux-2.6-omap-h63xx.git] / net / ipv4 / fib_frontend.c
index 60123905dbbf65d4e67a3c7adc86264091a52bfd..0484cae02736744e30291506adc2f08893dfb11d 100644 (file)
@@ -59,6 +59,25 @@ struct fib_table *ip_fib_main_table;
 #define FIB_TABLE_HASHSZ 1
 static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
 
+static int __net_init fib4_rules_init(struct net *net)
+{
+       ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
+       if (ip_fib_local_table == NULL)
+               return -ENOMEM;
+
+       ip_fib_main_table  = fib_hash_init(RT_TABLE_MAIN);
+       if (ip_fib_main_table == NULL)
+               goto fail;
+
+       hlist_add_head_rcu(&ip_fib_local_table->tb_hlist, &fib_table_hash[0]);
+       hlist_add_head_rcu(&ip_fib_main_table->tb_hlist, &fib_table_hash[0]);
+       return 0;
+
+fail:
+       kfree(ip_fib_local_table);
+       ip_fib_local_table = NULL;
+       return -ENOMEM;
+}
 #else
 
 #define FIB_TABLE_HASHSZ 256
@@ -148,16 +167,21 @@ out:
        return dev;
 }
 
-unsigned inet_addr_type(__be32 addr)
+/*
+ * Find address type as if only "dev" was present in the system. If
+ * on_dev is NULL then all interfaces are taken into consideration.
+ */
+static inline unsigned __inet_dev_addr_type(const struct net_device *dev,
+                                           __be32 addr)
 {
        struct flowi            fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
        struct fib_result       res;
        unsigned ret = RTN_BROADCAST;
        struct fib_table *local_table;
 
-       if (ZERONET(addr) || BADCLASS(addr))
+       if (ipv4_is_zeronet(addr) || ipv4_is_badclass(addr))
                return RTN_BROADCAST;
-       if (MULTICAST(addr))
+       if (ipv4_is_multicast(addr))
                return RTN_MULTICAST;
 
 #ifdef CONFIG_IP_MULTIPLE_TABLES
@@ -168,13 +192,24 @@ unsigned inet_addr_type(__be32 addr)
        if (local_table) {
                ret = RTN_UNICAST;
                if (!local_table->tb_lookup(local_table, &fl, &res)) {
-                       ret = res.type;
+                       if (!dev || dev == res.fi->fib_dev)
+                               ret = res.type;
                        fib_res_put(&res);
                }
        }
        return ret;
 }
 
+unsigned int inet_addr_type(__be32 addr)
+{
+       return __inet_dev_addr_type(NULL, addr);
+}
+
+unsigned int inet_dev_addr_type(const struct net_device *dev, __be32 addr)
+{
+       return __inet_dev_addr_type(dev, addr);
+}
+
 /* Given (packet source, input interface) and optional (dst, oif, tos):
    - (main) check, that source is valid i.e. not broadcast or our local
      address.
@@ -531,10 +566,14 @@ errout:
 
 static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
 {
+       struct net *net = skb->sk->sk_net;
        struct fib_config cfg;
        struct fib_table *tb;
        int err;
 
+       if (net != &init_net)
+               return -EINVAL;
+
        err = rtm_to_fib_config(skb, nlh, &cfg);
        if (err < 0)
                goto errout;
@@ -552,10 +591,14 @@ errout:
 
 static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
 {
+       struct net *net = skb->sk->sk_net;
        struct fib_config cfg;
        struct fib_table *tb;
        int err;
 
+       if (net != &init_net)
+               return -EINVAL;
+
        err = rtm_to_fib_config(skb, nlh, &cfg);
        if (err < 0)
                goto errout;
@@ -573,12 +616,16 @@ errout:
 
 static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
 {
+       struct net *net = skb->sk->sk_net;
        unsigned int h, s_h;
        unsigned int e = 0, s_e;
        struct fib_table *tb;
        struct hlist_node *node;
        int dumped = 0;
 
+       if (net != &init_net)
+               return 0;
+
        if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
            ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
                return ip_rt_dump(skb, cb);
@@ -675,7 +722,7 @@ void fib_add_ifaddr(struct in_ifaddr *ifa)
        if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
                fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
 
-       if (!ZERONET(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
+       if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
            (prefix != addr || ifa->ifa_prefixlen < 32)) {
                fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
                          RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim);
@@ -797,10 +844,13 @@ static void nl_fib_input(struct sk_buff *skb)
 
        nlh = nlmsg_hdr(skb);
        if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
-           nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn))) {
-               kfree_skb(skb);
+           nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn)))
                return;
-       }
+
+       skb = skb_clone(skb, GFP_KERNEL);
+       if (skb == NULL)
+               return;
+       nlh = nlmsg_hdr(skb);
 
        frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
        tb = fib_get_table(frn->tb_id_in);
@@ -813,10 +863,18 @@ static void nl_fib_input(struct sk_buff *skb)
        netlink_unicast(fibnl, skb, pid, MSG_DONTWAIT);
 }
 
-static void nl_fib_lookup_init(void)
+static int nl_fib_lookup_init(struct net *net)
 {
-       fibnl = netlink_kernel_create(&init_net, NETLINK_FIB_LOOKUP, 0,
+       fibnl = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0,
                                      nl_fib_input, NULL, THIS_MODULE);
+       if (fibnl == NULL)
+               return -EAFNOSUPPORT;
+       return 0;
+}
+
+static void nl_fib_lookup_exit(struct net *net)
+{
+       sock_put(fibnl);
 }
 
 static void fib_disable_ip(struct net_device *dev, int force)
@@ -899,29 +957,88 @@ static struct notifier_block fib_netdev_notifier = {
        .notifier_call =fib_netdev_event,
 };
 
-void __init ip_fib_init(void)
+static int __net_init ip_fib_net_init(struct net *net)
 {
        unsigned int i;
 
        for (i = 0; i < FIB_TABLE_HASHSZ; i++)
                INIT_HLIST_HEAD(&fib_table_hash[i]);
-#ifndef CONFIG_IP_MULTIPLE_TABLES
-       ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
-       hlist_add_head_rcu(&ip_fib_local_table->tb_hlist, &fib_table_hash[0]);
-       ip_fib_main_table  = fib_hash_init(RT_TABLE_MAIN);
-       hlist_add_head_rcu(&ip_fib_main_table->tb_hlist, &fib_table_hash[0]);
-#else
-       fib4_rules_init();
+
+       return fib4_rules_init(net);
+}
+
+static void __net_exit ip_fib_net_exit(struct net *net)
+{
+       unsigned int i;
+
+#ifdef CONFIG_IP_MULTIPLE_TABLES
+       fib4_rules_exit(net);
 #endif
 
-       register_netdevice_notifier(&fib_netdev_notifier);
-       register_inetaddr_notifier(&fib_inetaddr_notifier);
-       nl_fib_lookup_init();
+       for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
+               struct fib_table *tb;
+               struct hlist_head *head;
+               struct hlist_node *node, *tmp;
+
+               head = &fib_table_hash[i];
+               hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) {
+                       hlist_del(node);
+                       tb->tb_flush(tb);
+                       kfree(tb);
+               }
+       }
+}
+
+static int __net_init fib_net_init(struct net *net)
+{
+       int error;
+
+       error = 0;
+       if (net != &init_net)
+               goto out;
+
+       error = ip_fib_net_init(net);
+       if (error < 0)
+               goto out;
+       error = nl_fib_lookup_init(net);
+       if (error < 0)
+               goto out_nlfl;
+       error = fib_proc_init(net);
+       if (error < 0)
+               goto out_proc;
+out:
+       return error;
+
+out_proc:
+       nl_fib_lookup_exit(net);
+out_nlfl:
+       ip_fib_net_exit(net);
+       goto out;
+}
+
+static void __net_exit fib_net_exit(struct net *net)
+{
+       fib_proc_exit(net);
+       nl_fib_lookup_exit(net);
+       ip_fib_net_exit(net);
+}
 
+static struct pernet_operations fib_net_ops = {
+       .init = fib_net_init,
+       .exit = fib_net_exit,
+};
+
+void __init ip_fib_init(void)
+{
        rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL);
        rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL);
        rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib);
+
+       register_pernet_subsys(&fib_net_ops);
+       register_netdevice_notifier(&fib_netdev_notifier);
+       register_inetaddr_notifier(&fib_inetaddr_notifier);
 }
 
 EXPORT_SYMBOL(inet_addr_type);
+EXPORT_SYMBOL(inet_dev_addr_type);
 EXPORT_SYMBOL(ip_dev_find);