Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6.git] / net / decnet / dn_table.c
index dad5603..b9a33bb 100644 (file)
@@ -12,7 +12,6 @@
  * Changes:
  *
  */
-#include <linux/config.h>
 #include <linux/string.h>
 #include <linux/net.h>
 #include <linux/socket.h>
 #include <asm/uaccess.h>
 #include <linux/route.h> /* RTF_xxx */
 #include <net/neighbour.h>
+#include <net/netlink.h>
 #include <net/dst.h>
 #include <net/flow.h>
+#include <net/fib_rules.h>
 #include <net/dn.h>
 #include <net/dn_route.h>
 #include <net/dn_fib.h>
@@ -46,7 +47,7 @@ struct dn_zone
        u32                     dz_hashmask;
 #define DZ_HASHMASK(dz)        ((dz)->dz_hashmask)
        int                     dz_order;
-       u16                     dz_mask;
+       __le16                  dz_mask;
 #define DZ_MASK(dz)    ((dz)->dz_mask)
 };
 
@@ -60,7 +61,7 @@ struct dn_hash
 #define dz_prefix(key,dz)      ((key).datum)
 
 #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
-        for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
+       for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
 
 #define endfor_nexthops(fi) }
 
@@ -75,23 +76,23 @@ for( ; ((f) = *(fp)) != NULL; (fp) = &(f)->fn_next)
 for( ; ((f) = *(fp)) != NULL && dn_key_eq((f)->fn_key, (key)); (fp) = &(f)->fn_next)
 
 #define RT_TABLE_MIN 1
-
+#define DN_FIB_TABLE_HASHSZ 256
+static struct hlist_head dn_fib_table_hash[DN_FIB_TABLE_HASHSZ];
 static DEFINE_RWLOCK(dn_fib_tables_lock);
-struct dn_fib_table *dn_fib_tables[RT_TABLE_MAX + 1];
 
-static kmem_cache_t *dn_hash_kmem;
+static struct kmem_cache *dn_hash_kmem __read_mostly;
 static int dn_fib_hash_zombies;
 
 static inline dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
 {
-       u16 h = ntohs(key.datum)>>(16 - dz->dz_order);
+       u16 h = le16_to_cpu(key.datum)>>(16 - dz->dz_order);
        h ^= (h >> 10);
        h ^= (h >> 6);
        h &= DZ_HASHMASK(dz);
        return *(dn_fib_idx_t *)&h;
 }
 
-static inline dn_fib_key_t dz_key(u16 dst, struct dn_zone *dz)
+static inline dn_fib_key_t dz_key(__le16 dst, struct dn_zone *dz)
 {
        dn_fib_key_t k;
        k.datum = dst & DZ_MASK(dz);
@@ -159,12 +160,10 @@ static void dn_rehash_zone(struct dn_zone *dz)
                        break;
        }
 
-       ht = kmalloc(new_divisor*sizeof(struct dn_fib_node*), GFP_KERNEL);
-
+       ht = kcalloc(new_divisor, sizeof(struct dn_fib_node*), GFP_KERNEL);
        if (ht == NULL)
                return;
 
-       memset(ht, 0, new_divisor*sizeof(struct dn_fib_node *));
        write_lock_bh(&dn_fib_tables_lock);
        old_ht = dz->dz_hash;
        dz->dz_hash = ht;
@@ -185,11 +184,10 @@ static void dn_free_node(struct dn_fib_node *f)
 static struct dn_zone *dn_new_zone(struct dn_hash *table, int z)
 {
        int i;
-       struct dn_zone *dz = kmalloc(sizeof(struct dn_zone), GFP_KERNEL);
+       struct dn_zone *dz = kzalloc(sizeof(struct dn_zone), GFP_KERNEL);
        if (!dz)
                return NULL;
 
-       memset(dz, 0, sizeof(struct dn_zone));
        if (z) {
                dz->dz_divisor = 16;
                dz->dz_hashmask = 0x0F;
@@ -198,14 +196,12 @@ static struct dn_zone *dn_new_zone(struct dn_hash *table, int z)
                dz->dz_hashmask = 0;
        }
 
-       dz->dz_hash = kmalloc(dz->dz_divisor*sizeof(struct dn_fib_node *), GFP_KERNEL);
-
+       dz->dz_hash = kcalloc(dz->dz_divisor, sizeof(struct dn_fib_node *), GFP_KERNEL);
        if (!dz->dz_hash) {
                kfree(dz);
                return NULL;
        }
 
-       memset(dz->dz_hash, 0, dz->dz_divisor*sizeof(struct dn_fib_node*));
        dz->dz_order = z;
        dz->dz_mask = dnet_make_mask(z);
 
@@ -250,7 +246,7 @@ static int dn_fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct dn_kern
 
        for_nexthops(fi) {
                int attrlen = nhlen - sizeof(struct rtnexthop);
-               dn_address gw;
+               __le16 gw;
 
                if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
                        return -EINVAL;
@@ -268,96 +264,125 @@ static int dn_fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct dn_kern
        return 0;
 }
 
+static inline size_t dn_fib_nlmsg_size(struct dn_fib_info *fi)
+{
+       size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
+                        + nla_total_size(4) /* RTA_TABLE */
+                        + nla_total_size(2) /* RTA_DST */
+                        + nla_total_size(4); /* RTA_PRIORITY */
+
+       /* space for nested metrics */
+       payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
+
+       if (fi->fib_nhs) {
+               /* Also handles the special case fib_nhs == 1 */
+
+               /* each nexthop is packed in an attribute */
+               size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
+
+               /* may contain a gateway attribute */
+               nhsize += nla_total_size(4);
+
+               /* all nexthops are packed in a nested attribute */
+               payload += nla_total_size(fi->fib_nhs * nhsize);
+       }
+
+       return payload;
+}
+
 static int dn_fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
-                        u8 tb_id, u8 type, u8 scope, void *dst, int dst_len,
-                        struct dn_fib_info *fi)
+                       u32 tb_id, u8 type, u8 scope, void *dst, int dst_len,
+                       struct dn_fib_info *fi, unsigned int flags)
 {
-        struct rtmsg *rtm;
-        struct nlmsghdr *nlh;
-        unsigned char *b = skb->tail;
-
-        nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*rtm));
-        rtm = NLMSG_DATA(nlh);
-        rtm->rtm_family = AF_DECnet;
-        rtm->rtm_dst_len = dst_len;
-        rtm->rtm_src_len = 0;
-        rtm->rtm_tos = 0;
-        rtm->rtm_table = tb_id;
-        rtm->rtm_flags = fi->fib_flags;
-        rtm->rtm_scope = scope;
+       struct rtmsg *rtm;
+       struct nlmsghdr *nlh;
+       unsigned char *b = skb_tail_pointer(skb);
+
+       nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags);
+       rtm = NLMSG_DATA(nlh);
+       rtm->rtm_family = AF_DECnet;
+       rtm->rtm_dst_len = dst_len;
+       rtm->rtm_src_len = 0;
+       rtm->rtm_tos = 0;
+       rtm->rtm_table = tb_id;
+       RTA_PUT_U32(skb, RTA_TABLE, tb_id);
+       rtm->rtm_flags = fi->fib_flags;
+       rtm->rtm_scope = scope;
        rtm->rtm_type  = type;
-        if (rtm->rtm_dst_len)
-                RTA_PUT(skb, RTA_DST, 2, dst);
-        rtm->rtm_protocol = fi->fib_protocol;
-        if (fi->fib_priority)
-                RTA_PUT(skb, RTA_PRIORITY, 4, &fi->fib_priority);
+       if (rtm->rtm_dst_len)
+               RTA_PUT(skb, RTA_DST, 2, dst);
+       rtm->rtm_protocol = fi->fib_protocol;
+       if (fi->fib_priority)
+               RTA_PUT(skb, RTA_PRIORITY, 4, &fi->fib_priority);
        if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
                goto rtattr_failure;
-        if (fi->fib_nhs == 1) {
-                if (fi->fib_nh->nh_gw)
-                        RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
-                if (fi->fib_nh->nh_oif)
-                        RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
-        }
-        if (fi->fib_nhs > 1) {
-                struct rtnexthop *nhp;
-                struct rtattr *mp_head;
-                if (skb_tailroom(skb) <= RTA_SPACE(0))
-                        goto rtattr_failure;
-                mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
-
-                for_nexthops(fi) {
-                        if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
-                                goto rtattr_failure;
-                        nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
-                        nhp->rtnh_flags = nh->nh_flags & 0xFF;
-                        nhp->rtnh_hops = nh->nh_weight - 1;
-                        nhp->rtnh_ifindex = nh->nh_oif;
-                        if (nh->nh_gw)
-                                RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
-                        nhp->rtnh_len = skb->tail - (unsigned char *)nhp;
-                } endfor_nexthops(fi);
-                mp_head->rta_type = RTA_MULTIPATH;
-                mp_head->rta_len = skb->tail - (u8*)mp_head;
-        }
-
-        nlh->nlmsg_len = skb->tail - b;
-        return skb->len;
+       if (fi->fib_nhs == 1) {
+               if (fi->fib_nh->nh_gw)
+                       RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
+               if (fi->fib_nh->nh_oif)
+                       RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
+       }
+       if (fi->fib_nhs > 1) {
+               struct rtnexthop *nhp;
+               struct rtattr *mp_head;
+               if (skb_tailroom(skb) <= RTA_SPACE(0))
+                       goto rtattr_failure;
+               mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
+
+               for_nexthops(fi) {
+                       if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
+                               goto rtattr_failure;
+                       nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
+                       nhp->rtnh_flags = nh->nh_flags & 0xFF;
+                       nhp->rtnh_hops = nh->nh_weight - 1;
+                       nhp->rtnh_ifindex = nh->nh_oif;
+                       if (nh->nh_gw)
+                               RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
+                       nhp->rtnh_len = skb_tail_pointer(skb) - (unsigned char *)nhp;
+               } endfor_nexthops(fi);
+               mp_head->rta_type = RTA_MULTIPATH;
+               mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
+       }
+
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
+       return skb->len;
 
 
 nlmsg_failure:
 rtattr_failure:
-        skb_trim(skb, b - skb->data);
-        return -1;
+       nlmsg_trim(skb, b);
+       return -EMSGSIZE;
 }
 
 
-static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, int tb_id,
-                        struct nlmsghdr *nlh, struct netlink_skb_parms *req)
+static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, u32 tb_id,
+                       struct nlmsghdr *nlh, struct netlink_skb_parms *req)
 {
-        struct sk_buff *skb;
-        u32 pid = req ? req->pid : 0;
-        int size = NLMSG_SPACE(sizeof(struct rtmsg) + 256);
-
-        skb = alloc_skb(size, GFP_KERNEL);
-        if (!skb)
-                return;
-
-        if (dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id, 
-                                f->fn_type, f->fn_scope, &f->fn_key, z, 
-                                DN_FIB_INFO(f)) < 0) {
-                kfree_skb(skb);
-                return;
-        }
-        NETLINK_CB(skb).dst_groups = RTMGRP_DECnet_ROUTE;
-        if (nlh->nlmsg_flags & NLM_F_ECHO)
-                atomic_inc(&skb->users);
-        netlink_broadcast(rtnl, skb, pid, RTMGRP_DECnet_ROUTE, GFP_KERNEL);
-        if (nlh->nlmsg_flags & NLM_F_ECHO)
-                netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
+       struct sk_buff *skb;
+       u32 pid = req ? req->pid : 0;
+       int err = -ENOBUFS;
+
+       skb = nlmsg_new(dn_fib_nlmsg_size(DN_FIB_INFO(f)), GFP_KERNEL);
+       if (skb == NULL)
+               goto errout;
+
+       err = dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id,
+                              f->fn_type, f->fn_scope, &f->fn_key, z,
+                              DN_FIB_INFO(f), 0);
+       if (err < 0) {
+               /* -EMSGSIZE implies BUG in dn_fib_nlmsg_size() */
+               WARN_ON(err == -EMSGSIZE);
+               kfree_skb(skb);
+               goto errout;
+       }
+       rtnl_notify(skb, &init_net, pid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
+       return;
+errout:
+       if (err < 0)
+               rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_ROUTE, err);
 }
 
-static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb, 
+static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
                                struct netlink_callback *cb,
                                struct dn_fib_table *tb,
                                struct dn_zone *dz,
@@ -365,76 +390,117 @@ static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
 {
        int i, s_i;
 
-       s_i = cb->args[3];
+       s_i = cb->args[4];
        for(i = 0; f; i++, f = f->fn_next) {
                if (i < s_i)
                        continue;
                if (f->fn_state & DN_S_ZOMBIE)
                        continue;
-               if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid, 
+               if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
                                cb->nlh->nlmsg_seq,
                                RTM_NEWROUTE,
-                               tb->n, 
+                               tb->n,
                                (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
-                               f->fn_scope, &f->fn_key, dz->dz_order, 
-                               f->fn_info) < 0) {
-                       cb->args[3] = i;
+                               f->fn_scope, &f->fn_key, dz->dz_order,
+                               f->fn_info, NLM_F_MULTI) < 0) {
+                       cb->args[4] = i;
                        return -1;
                }
        }
-       cb->args[3] = i;
+       cb->args[4] = i;
        return skb->len;
 }
 
-static __inline__ int dn_hash_dump_zone(struct sk_buff *skb, 
+static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
                                struct netlink_callback *cb,
                                struct dn_fib_table *tb,
                                struct dn_zone *dz)
 {
        int h, s_h;
 
-       s_h = cb->args[2];
+       s_h = cb->args[3];
        for(h = 0; h < dz->dz_divisor; h++) {
                if (h < s_h)
                        continue;
                if (h > s_h)
-                       memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
+                       memset(&cb->args[4], 0, sizeof(cb->args) - 4*sizeof(cb->args[0]));
                if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
                        continue;
                if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
-                       cb->args[2] = h;
+                       cb->args[3] = h;
                        return -1;
                }
        }
-       cb->args[2] = h;
+       cb->args[3] = h;
        return skb->len;
 }
 
-static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb, 
-                                struct netlink_callback *cb)
+static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
+                               struct netlink_callback *cb)
 {
-        int m, s_m;
+       int m, s_m;
        struct dn_zone *dz;
        struct dn_hash *table = (struct dn_hash *)tb->data;
 
-       s_m = cb->args[1];
+       s_m = cb->args[2];
        read_lock(&dn_fib_tables_lock);
        for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
                if (m < s_m)
                        continue;
                if (m > s_m)
-                       memset(&cb->args[2], 0, sizeof(cb->args) - 2*sizeof(cb->args[0]));
+                       memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
 
                if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
-                       cb->args[1] = m;
+                       cb->args[2] = m;
                        read_unlock(&dn_fib_tables_lock);
                        return -1;
                }
        }
        read_unlock(&dn_fib_tables_lock);
-       cb->args[1] = m;
+       cb->args[2] = m;
 
-        return skb->len;
+       return skb->len;
+}
+
+int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+       struct net *net = sock_net(skb->sk);
+       unsigned int h, s_h;
+       unsigned int e = 0, s_e;
+       struct dn_fib_table *tb;
+       struct hlist_node *node;
+       int dumped = 0;
+
+       if (!net_eq(net, &init_net))
+               return 0;
+
+       if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
+               ((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
+                       return dn_cache_dump(skb, cb);
+
+       s_h = cb->args[0];
+       s_e = cb->args[1];
+
+       for (h = s_h; h < DN_FIB_TABLE_HASHSZ; h++, s_h = 0) {
+               e = 0;
+               hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist) {
+                       if (e < s_e)
+                               goto next;
+                       if (dumped)
+                               memset(&cb->args[2], 0, sizeof(cb->args) -
+                                                2 * sizeof(cb->args[0]));
+                       if (tb->dump(tb, skb, cb) < 0)
+                               goto out;
+                       dumped = 1;
+next:
+                       e++;
+               }
+       }
+out:
+       cb->args[1] = e;
+       cb->args[0] = h;
+
+       return skb->len;
 }
 
 static int dn_fib_table_insert(struct dn_fib_table *tb, struct rtmsg *r, struct dn_kern_rta *rta, struct nlmsghdr *n, struct netlink_skb_parms *req)
@@ -443,13 +509,13 @@ static int dn_fib_table_insert(struct dn_fib_table *tb, struct rtmsg *r, struct
        struct dn_fib_node *new_f, *f, **fp, **del_fp;
        struct dn_zone *dz;
        struct dn_fib_info *fi;
-        int z = r->rtm_dst_len;
+       int z = r->rtm_dst_len;
        int type = r->rtm_type;
        dn_fib_key_t key;
-        int err;
+       int err;
 
-        if (z > 16)
-                return -EINVAL;
+       if (z > 16)
+               return -EINVAL;
 
        dz = table->dh_zones[z];
        if (!dz && !(dz = dn_new_zone(table, z)))
@@ -457,15 +523,15 @@ static int dn_fib_table_insert(struct dn_fib_table *tb, struct rtmsg *r, struct
 
        dz_key_0(key);
        if (rta->rta_dst) {
-               dn_address dst;
+               __le16 dst;
                memcpy(&dst, rta->rta_dst, 2);
                if (dst & ~DZ_MASK(dz))
                        return -EINVAL;
                key = dz_key(dst, dz);
        }
 
-        if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
-                return err;
+       if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
+               return err;
 
        if (dz->dz_nent > (dz->dz_divisor << 2) &&
                        dz->dz_divisor > DN_MAX_DIVISOR &&
@@ -515,8 +581,9 @@ static int dn_fib_table_insert(struct dn_fib_table *tb, struct rtmsg *r, struct
                DN_FIB_SCAN_KEY(f, fp, key) {
                        if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
                                break;
-                       if (f->fn_type == type && f->fn_scope == r->rtm_scope
-                                       && DN_FIB_INFO(f) == fi)
+                       if (f->fn_type == type &&
+                           f->fn_scope == r->rtm_scope &&
+                           DN_FIB_INFO(f) == fi)
                                goto out;
                }
 
@@ -533,12 +600,10 @@ create:
 
 replace:
        err = -ENOBUFS;
-       new_f = kmem_cache_alloc(dn_hash_kmem, SLAB_KERNEL);
+       new_f = kmem_cache_zalloc(dn_hash_kmem, GFP_KERNEL);
        if (new_f == NULL)
                goto out;
 
-       memset(new_f, 0, sizeof(struct dn_fib_node));
-
        new_f->fn_key = key;
        new_f->fn_type = type;
        new_f->fn_scope = r->rtm_scope;
@@ -566,9 +631,9 @@ replace:
                dn_rt_cache_flush(-1);
        }
 
-        dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
+       dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
 
-        return 0;
+       return 0;
 out:
        dn_fib_release_info(fi);
        return err;
@@ -579,21 +644,21 @@ static int dn_fib_table_delete(struct dn_fib_table *tb, struct rtmsg *r, struct
 {
        struct dn_hash *table = (struct dn_hash*)tb->data;
        struct dn_fib_node **fp, **del_fp, *f;
-        int z = r->rtm_dst_len;
+       int z = r->rtm_dst_len;
        struct dn_zone *dz;
        dn_fib_key_t key;
        int matched;
 
 
-        if (z > 16)
-                return -EINVAL;
+       if (z > 16)
+               return -EINVAL;
 
        if ((dz = table->dh_zones[z]) == NULL)
                return -ESRCH;
 
        dz_key_0(key);
        if (rta->rta_dst) {
-               dn_address dst;
+               __le16 dst;
                memcpy(&dst, rta->rta_dst, 2);
                if (dst & ~DZ_MASK(dz))
                        return -EINVAL;
@@ -622,7 +687,7 @@ static int dn_fib_table_delete(struct dn_fib_table *tb, struct rtmsg *r, struct
                if (del_fp == NULL &&
                                (!r->rtm_type || f->fn_type == r->rtm_type) &&
                                (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
-                               (!r->rtm_protocol || 
+                               (!r->rtm_protocol ||
                                        fi->fib_protocol == r->rtm_protocol) &&
                                dn_fib_nh_match(r, n, rta, fi) == 0)
                        del_fp = fp;
@@ -630,7 +695,7 @@ static int dn_fib_table_delete(struct dn_fib_table *tb, struct rtmsg *r, struct
 
        if (del_fp) {
                f = *del_fp;
-               dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
+               dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
 
                if (matched != 1) {
                        write_lock_bh(&dn_fib_tables_lock);
@@ -654,7 +719,7 @@ static int dn_fib_table_delete(struct dn_fib_table *tb, struct rtmsg *r, struct
                return 0;
        }
 
-        return -ESRCH;
+       return -ESRCH;
 }
 
 static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
@@ -701,7 +766,7 @@ static int dn_fib_table_flush(struct dn_fib_table *tb)
 
 static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp, struct dn_fib_res *res)
 {
-        int err;
+       int err;
        struct dn_zone *dz;
        struct dn_hash *t = (struct dn_hash *)tb->data;
 
@@ -730,7 +795,7 @@ static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp,
 
                        if (err == 0) {
                                res->type = f->fn_type;
-                               res->scope = f->fn_scope; 
+                               res->scope = f->fn_scope;
                                res->prefixlen = dz->dz_order;
                                goto out;
                        }
@@ -741,69 +806,80 @@ static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp,
        err = 1;
 out:
        read_unlock(&dn_fib_tables_lock);
-        return err;
+       return err;
 }
 
 
-struct dn_fib_table *dn_fib_get_table(int n, int create)
+struct dn_fib_table *dn_fib_get_table(u32 n, int create)
 {
-        struct dn_fib_table *t;
+       struct dn_fib_table *t;
+       struct hlist_node *node;
+       unsigned int h;
 
-        if (n < RT_TABLE_MIN)
-                return NULL;
+       if (n < RT_TABLE_MIN)
+               return NULL;
 
-        if (n > RT_TABLE_MAX)
-                return NULL;
+       if (n > RT_TABLE_MAX)
+               return NULL;
 
-        if (dn_fib_tables[n]) 
-                return dn_fib_tables[n];
+       h = n & (DN_FIB_TABLE_HASHSZ - 1);
+       rcu_read_lock();
+       hlist_for_each_entry_rcu(t, node, &dn_fib_table_hash[h], hlist) {
+               if (t->n == n) {
+                       rcu_read_unlock();
+                       return t;
+               }
+       }
+       rcu_read_unlock();
 
-        if (!create)
-                return NULL;
+       if (!create)
+               return NULL;
 
-        if (in_interrupt() && net_ratelimit()) {
-                printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n"); 
-                return NULL;
-        }
-        if ((t = kmalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash), GFP_KERNEL)) == NULL)
-                return NULL;
+       if (in_interrupt() && net_ratelimit()) {
+               printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n");
+               return NULL;
+       }
 
-        memset(t, 0, sizeof(struct dn_fib_table));
+       t = kzalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash),
+                   GFP_KERNEL);
+       if (t == NULL)
+               return NULL;
 
-        t->n = n;
-        t->insert = dn_fib_table_insert;
-        t->delete = dn_fib_table_delete;
-        t->lookup = dn_fib_table_lookup;
-        t->flush  = dn_fib_table_flush;
-        t->dump = dn_fib_table_dump;
-       memset(t->data, 0, sizeof(struct dn_hash));
-        dn_fib_tables[n] = t;
+       t->n = n;
+       t->insert = dn_fib_table_insert;
+       t->delete = dn_fib_table_delete;
+       t->lookup = dn_fib_table_lookup;
+       t->flush  = dn_fib_table_flush;
+       t->dump = dn_fib_table_dump;
+       hlist_add_head_rcu(&t->hlist, &dn_fib_table_hash[h]);
 
-        return t;
+       return t;
 }
 
-static void dn_fib_del_tree(int n)
+struct dn_fib_table *dn_fib_empty_table(void)
 {
-        struct dn_fib_table *t;
-
-        write_lock(&dn_fib_tables_lock);
-        t = dn_fib_tables[n];
-        dn_fib_tables[n] = NULL;
-        write_unlock(&dn_fib_tables_lock);
+       u32 id;
 
-        if (t) {
-                kfree(t);
-        }
+       for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
+               if (dn_fib_get_table(id, 0) == NULL)
+                       return dn_fib_get_table(id, 1);
+       return NULL;
 }
 
-struct dn_fib_table *dn_fib_empty_table(void)
+void dn_fib_flush(void)
 {
-        int id;
+       int flushed = 0;
+       struct dn_fib_table *tb;
+       struct hlist_node *node;
+       unsigned int h;
+
+       for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
+               hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist)
+                       flushed += tb->flush(tb);
+       }
 
-        for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
-                if (dn_fib_tables[id] == NULL)
-                        return dn_fib_get_table(id, 1);
-        return NULL;
+       if (flushed)
+               dn_rt_cache_flush(-1);
 }
 
 void __init dn_fib_table_init(void)
@@ -811,15 +887,22 @@ void __init dn_fib_table_init(void)
        dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
                                        sizeof(struct dn_fib_info),
                                        0, SLAB_HWCACHE_ALIGN,
-                                       NULL, NULL);
+                                       NULL);
 }
 
 void __exit dn_fib_table_cleanup(void)
 {
-       int i;
-
-       for (i = RT_TABLE_MIN; i <= RT_TABLE_MAX; ++i)
-               dn_fib_del_tree(i);
-
-       return;
+       struct dn_fib_table *t;
+       struct hlist_node *node, *next;
+       unsigned int h;
+
+       write_lock(&dn_fib_tables_lock);
+       for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
+               hlist_for_each_entry_safe(t, node, next, &dn_fib_table_hash[h],
+                                         hlist) {
+                       hlist_del(&t->hlist);
+                       kfree(t);
+               }
+       }
+       write_unlock(&dn_fib_tables_lock);
 }