[IPV4]: Unify access to the routing tables.
[safe/jmp/linux-2.6] / net / ipv4 / fib_frontend.c
index 5bf718a..9ff1e66 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;
 
-#else
+       main_table  = fib_hash_init(RT_TABLE_MAIN);
+       if (main_table == NULL)
+               goto fail;
 
-#define FIB_TABLE_HASHSZ 256
-static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
+       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
 
 struct fib_table *fib_new_table(u32 id)
 {
@@ -126,13 +142,14 @@ struct net_device * ip_dev_find(__be32 addr)
        struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
        struct fib_result res;
        struct net_device *dev = NULL;
+       struct fib_table *local_table;
 
 #ifdef CONFIG_IP_MULTIPLE_TABLES
        res.r = NULL;
 #endif
 
-       if (!ip_fib_local_table ||
-           ip_fib_local_table->tb_lookup(ip_fib_local_table, &fl, &res))
+       local_table = fib_get_table(RT_TABLE_LOCAL);
+       if (!local_table || local_table->tb_lookup(local_table, &fl, &res))
                return NULL;
        if (res.type != RTN_LOCAL)
                goto out;
@@ -145,32 +162,49 @@ 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
        res.r = NULL;
 #endif
 
-       if (ip_fib_local_table) {
+       local_table = fib_get_table(RT_TABLE_LOCAL);
+       if (local_table) {
                ret = RTN_UNICAST;
-               if (!ip_fib_local_table->tb_lookup(ip_fib_local_table,
-                                                  &fl, &res)) {
-                       ret = res.type;
+               if (!local_table->tb_lookup(local_table, &fl, &res)) {
+                       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.
@@ -250,8 +284,6 @@ e_inval:
        return -EINVAL;
 }
 
-#ifndef CONFIG_IP_NOSIOCRT
-
 static inline __be32 sk_extract_addr(struct sockaddr *addr)
 {
        return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
@@ -336,7 +368,7 @@ static int rtentry_to_fib_config(int cmd, struct rtentry *rt,
                colon = strchr(devname, ':');
                if (colon)
                        *colon = 0;
-               dev = __dev_get_by_name(devname);
+               dev = __dev_get_by_name(&init_net, devname);
                if (!dev)
                        return -ENODEV;
                cfg->fc_oif = dev->ifindex;
@@ -443,16 +475,7 @@ int ip_rt_ioctl(unsigned int cmd, void __user *arg)
        return -EINVAL;
 }
 
-#else
-
-int ip_rt_ioctl(unsigned int cmd, void *arg)
-{
-       return -EINVAL;
-}
-
-#endif
-
-struct nla_policy rtm_ipv4_policy[RTA_MAX+1] __read_mostly = {
+const struct nla_policy rtm_ipv4_policy[RTA_MAX+1] = {
        [RTA_DST]               = { .type = NLA_U32 },
        [RTA_SRC]               = { .type = NLA_U32 },
        [RTA_IIF]               = { .type = NLA_U32 },
@@ -464,7 +487,6 @@ struct nla_policy rtm_ipv4_policy[RTA_MAX+1] __read_mostly = {
        [RTA_MULTIPATH]         = { .len = sizeof(struct rtnexthop) },
        [RTA_PROTOINFO]         = { .type = NLA_U32 },
        [RTA_FLOW]              = { .type = NLA_U32 },
-       [RTA_MP_ALGO]           = { .type = NLA_U32 },
 };
 
 static int rtm_to_fib_config(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -499,7 +521,7 @@ static int rtm_to_fib_config(struct sk_buff *skb, struct nlmsghdr *nlh,
        }
 
        nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
-               switch (attr->nla_type) {
+               switch (nla_type(attr)) {
                case RTA_DST:
                        cfg->fc_dst = nla_get_be32(attr);
                        break;
@@ -526,9 +548,6 @@ static int rtm_to_fib_config(struct sk_buff *skb, struct nlmsghdr *nlh,
                case RTA_FLOW:
                        cfg->fc_flow = nla_get_u32(attr);
                        break;
-               case RTA_MP_ALGO:
-                       cfg->fc_mp_alg = nla_get_u32(attr);
-                       break;
                case RTA_TABLE:
                        cfg->fc_table = nla_get_u32(attr);
                        break;
@@ -542,10 +561,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;
@@ -563,10 +586,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;
@@ -584,12 +611,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);
@@ -686,7 +717,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);
@@ -777,6 +808,10 @@ static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
                                                            .tos = frn->fl_tos,
                                                            .scope = frn->fl_scope } } };
 
+#ifdef CONFIG_IP_MULTIPLE_TABLES
+       res.r = NULL;
+#endif
+
        frn->err = -ENOENT;
        if (tb) {
                local_bh_disable();
@@ -795,24 +830,22 @@ static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
        }
 }
 
-static void nl_fib_input(struct sock *sk, int len)
+static void nl_fib_input(struct sk_buff *skb)
 {
-       struct sk_buff *skb = NULL;
-       struct nlmsghdr *nlh = NULL;
        struct fib_result_nl *frn;
-       u32 pid;
+       struct nlmsghdr *nlh;
        struct fib_table *tb;
-
-       skb = skb_dequeue(&sk->sk_receive_queue);
-       if (skb == NULL)
-               return;
+       u32 pid;
 
        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);
@@ -822,12 +855,21 @@ static void nl_fib_input(struct sock *sk, int len)
        pid = NETLINK_CB(skb).pid;       /* pid of sending process */
        NETLINK_CB(skb).pid = 0;         /* from kernel */
        NETLINK_CB(skb).dst_group = 0;  /* unicast */
-       netlink_unicast(sk, skb, pid, MSG_DONTWAIT);
+       netlink_unicast(fibnl, skb, pid, MSG_DONTWAIT);
+}
+
+static int nl_fib_lookup_init(struct net *net)
+{
+       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_init(void)
+static void nl_fib_lookup_exit(struct net *net)
 {
-      netlink_kernel_create(NETLINK_FIB_LOOKUP, 0, nl_fib_input, THIS_MODULE);
+       sock_put(fibnl);
 }
 
 static void fib_disable_ip(struct net_device *dev, int force)
@@ -870,6 +912,9 @@ static int fib_netdev_event(struct notifier_block *this, unsigned long event, vo
        struct net_device *dev = ptr;
        struct in_device *in_dev = __in_dev_get_rtnl(dev);
 
+       if (dev->nd_net != &init_net)
+               return NOTIFY_DONE;
+
        if (event == NETDEV_UNREGISTER) {
                fib_disable_ip(dev, 2);
                return NOTIFY_DONE;
@@ -907,29 +952,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);