[NETNS]: Add netns parameter to inet_(dev_)add_type.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / fib_frontend.c
index 732d8f088b13298fdfef8eb1051477c7f6453080..d1a45cb6f6b0cb997ed772219e79adb1df6ade9e 100644 (file)
 #define FFprint(a...) printk(KERN_DEBUG a)
 
 static struct sock *fibnl;
+struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
 
 #ifndef CONFIG_IP_MULTIPLE_TABLES
 
-struct fib_table *ip_fib_local_table;
-struct fib_table *ip_fib_main_table;
+static int __net_init fib4_rules_init(struct net *net)
+{
+       struct fib_table *local_table, *main_table;
 
-#define FIB_TABLE_HASHSZ 1
-static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
+       local_table = fib_hash_init(RT_TABLE_LOCAL);
+       if (local_table == NULL)
+               return -ENOMEM;
 
-static void __init fib4_rules_init(void)
-{
-       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]);
+       main_table  = fib_hash_init(RT_TABLE_MAIN);
+       if (main_table == NULL)
+               goto fail;
+
+       hlist_add_head_rcu(&local_table->tb_hlist,
+                               &fib_table_hash[TABLE_LOCAL_INDEX]);
+       hlist_add_head_rcu(&main_table->tb_hlist,
+                               &fib_table_hash[TABLE_MAIN_INDEX]);
+       return 0;
+
+fail:
+       kfree(local_table);
+       return -ENOMEM;
 }
 #else
 
-#define FIB_TABLE_HASHSZ 256
-static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
-
-struct fib_table *fib_new_table(u32 id)
+struct fib_table *fib_new_table(struct net *net, u32 id)
 {
        struct fib_table *tb;
        unsigned int h;
 
        if (id == 0)
                id = RT_TABLE_MAIN;
-       tb = fib_get_table(id);
+       tb = fib_get_table(net, id);
        if (tb)
                return tb;
        tb = fib_hash_init(id);
@@ -89,7 +96,7 @@ struct fib_table *fib_new_table(u32 id)
        return tb;
 }
 
-struct fib_table *fib_get_table(u32 id)
+struct fib_table *fib_get_table(struct net *net, u32 id)
 {
        struct fib_table *tb;
        struct hlist_node *node;
@@ -141,7 +148,7 @@ struct net_device * ip_dev_find(__be32 addr)
        res.r = NULL;
 #endif
 
-       local_table = fib_get_table(RT_TABLE_LOCAL);
+       local_table = fib_get_table(&init_net, RT_TABLE_LOCAL);
        if (!local_table || local_table->tb_lookup(local_table, &fl, &res))
                return NULL;
        if (res.type != RTN_LOCAL)
@@ -155,33 +162,51 @@ 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(struct net *net,
+                                           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
        res.r = NULL;
 #endif
 
-       local_table = fib_get_table(RT_TABLE_LOCAL);
+       local_table = fib_get_table(net, RT_TABLE_LOCAL);
        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(struct net *net, __be32 addr)
+{
+       return __inet_dev_addr_type(net, NULL, addr);
+}
+
+unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
+                               __be32 addr)
+{
+       return __inet_dev_addr_type(net, 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.
@@ -368,7 +393,7 @@ static int rtentry_to_fib_config(int cmd, struct rtentry *rt,
        if (rt->rt_gateway.sa_family == AF_INET && addr) {
                cfg->fc_gw = addr;
                if (rt->rt_flags & RTF_GATEWAY &&
-                   inet_addr_type(addr) == RTN_UNICAST)
+                   inet_addr_type(&init_net, addr) == RTN_UNICAST)
                        cfg->fc_scope = RT_SCOPE_UNIVERSE;
        }
 
@@ -430,13 +455,13 @@ int ip_rt_ioctl(unsigned int cmd, void __user *arg)
                        struct fib_table *tb;
 
                        if (cmd == SIOCDELRT) {
-                               tb = fib_get_table(cfg.fc_table);
+                               tb = fib_get_table(&init_net, cfg.fc_table);
                                if (tb)
                                        err = tb->tb_delete(tb, &cfg);
                                else
                                        err = -ESRCH;
                        } else {
-                               tb = fib_new_table(cfg.fc_table);
+                               tb = fib_new_table(&init_net, cfg.fc_table);
                                if (tb)
                                        err = tb->tb_insert(tb, &cfg);
                                else
@@ -538,15 +563,19 @@ 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;
 
-       tb = fib_get_table(cfg.fc_table);
+       tb = fib_get_table(net, cfg.fc_table);
        if (tb == NULL) {
                err = -ESRCH;
                goto errout;
@@ -559,15 +588,19 @@ 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;
 
-       tb = fib_new_table(cfg.fc_table);
+       tb = fib_new_table(&init_net, cfg.fc_table);
        if (tb == NULL) {
                err = -ENOBUFS;
                goto errout;
@@ -580,12 +613,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);
@@ -636,9 +673,9 @@ static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifad
        };
 
        if (type == RTN_UNICAST)
-               tb = fib_new_table(RT_TABLE_MAIN);
+               tb = fib_new_table(&init_net, RT_TABLE_MAIN);
        else
-               tb = fib_new_table(RT_TABLE_LOCAL);
+               tb = fib_new_table(&init_net, RT_TABLE_LOCAL);
 
        if (tb == NULL)
                return;
@@ -682,7 +719,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);
@@ -747,7 +784,7 @@ static void fib_del_ifaddr(struct in_ifaddr *ifa)
                fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
 
                /* Check, that this local address finally disappeared. */
-               if (inet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
+               if (inet_addr_type(&init_net, ifa->ifa_local) != RTN_LOCAL) {
                        /* And the last, but not the least thing.
                           We must flush stray FIB entries.
 
@@ -804,13 +841,16 @@ 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);
+       tb = fib_get_table(&init_net, frn->tb_id_in);
 
        nl_fib_lookup(frn, tb);
 
@@ -820,10 +860,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)
@@ -906,23 +954,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]);
 
-       fib4_rules_init();
+       return fib4_rules_init(net);
+}
+
+static void __net_exit ip_fib_net_exit(struct net *net)
+{
+       unsigned int i;
 
-       register_netdevice_notifier(&fib_netdev_notifier);
-       register_inetaddr_notifier(&fib_inetaddr_notifier);
-       nl_fib_lookup_init();
+#ifdef CONFIG_IP_MULTIPLE_TABLES
+       fib4_rules_exit(net);
+#endif
+
+       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);