fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / ipv4 / fib_hash.c
index 25535ff..eb85887 100644 (file)
  *             2 of the License, or (at your option) any later version.
  */
 
-#include <linux/config.h>
 #include <asm/uaccess.h>
 #include <asm/system.h>
-#include <asm/bitops.h>
+#include <linux/bitops.h>
 #include <linux/types.h>
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/errno.h>
 #include <linux/in.h>
 #include <linux/inet.h>
+#include <linux/inetdevice.h>
 #include <linux/netdevice.h>
 #include <linux/if_arp.h>
 #include <linux/proc_fs.h>
 #include <linux/skbuff.h>
 #include <linux/netlink.h>
 #include <linux/init.h>
+#include <linux/vs_context.h>
 
 #include <net/ip.h>
 #include <net/protocol.h>
 
 #include "fib_lookup.h"
 
-static kmem_cache_t *fn_hash_kmem;
-static kmem_cache_t *fn_alias_kmem;
+static struct kmem_cache *fn_hash_kmem __read_mostly;
+static struct kmem_cache *fn_alias_kmem __read_mostly;
 
 struct fib_node {
        struct hlist_node       fn_hash;
        struct list_head        fn_alias;
-       u32                     fn_key;
+       __be32                  fn_key;
 };
 
 struct fn_zone {
@@ -64,7 +65,7 @@ struct fn_zone {
 #define FZ_HASHMASK(fz)                ((fz)->fz_hashmask)
 
        int                     fz_order;       /* Zone order           */
-       u32                     fz_mask;
+       __be32                  fz_mask;
 #define FZ_MASK(fz)            ((fz)->fz_mask)
 };
 
@@ -77,7 +78,7 @@ struct fn_hash {
        struct fn_zone  *fn_zone_list;
 };
 
-static inline u32 fn_hash(u32 key, struct fn_zone *fz)
+static inline u32 fn_hash(__be32 key, struct fn_zone *fz)
 {
        u32 h = ntohl(key)>>(32 - fz->fz_order);
        h ^= (h>>20);
@@ -87,12 +88,13 @@ static inline u32 fn_hash(u32 key, struct fn_zone *fz)
        return h;
 }
 
-static inline u32 fz_key(u32 dst, struct fn_zone *fz)
+static inline __be32 fz_key(__be32 dst, struct fn_zone *fz)
 {
        return dst & FZ_MASK(fz);
 }
 
-static rwlock_t fib_hash_lock = RW_LOCK_UNLOCKED;
+static DEFINE_RWLOCK(fib_hash_lock);
+static unsigned int fib_hash_genid;
 
 #define FZ_MAX_DIVISOR ((PAGE_SIZE<<MAX_ORDER) / sizeof(struct hlist_head))
 
@@ -181,6 +183,7 @@ static void fn_rehash_zone(struct fn_zone *fz)
                fz->fz_hashmask = new_hashmask;
                fz->fz_divisor = new_divisor;
                fn_rebuild_zone(fz, old_ht, old_divisor);
+               fib_hash_genid++;
                write_unlock_bh(&fib_hash_lock);
 
                fz_hash_free(old_ht, old_divisor);
@@ -202,11 +205,10 @@ static struct fn_zone *
 fn_new_zone(struct fn_hash *table, int z)
 {
        int i;
-       struct fn_zone *fz = kmalloc(sizeof(struct fn_zone), GFP_KERNEL);
+       struct fn_zone *fz = kzalloc(sizeof(struct fn_zone), GFP_KERNEL);
        if (!fz)
                return NULL;
 
-       memset(fz, 0, sizeof(struct fn_zone));
        if (z) {
                fz->fz_divisor = 16;
        } else {
@@ -236,6 +238,7 @@ fn_new_zone(struct fn_hash *table, int z)
                table->fn_zones[i]->fz_next = fz;
        }
        table->fn_zones[z] = fz;
+       fib_hash_genid++;
        write_unlock_bh(&fib_hash_lock);
        return fz;
 }
@@ -252,7 +255,7 @@ fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
                struct hlist_head *head;
                struct hlist_node *node;
                struct fib_node *f;
-               u32 k = fz_key(flp->fl4_dst, fz);
+               __be32 k = fz_key(flp->fl4_dst, fz);
 
                head = &fz->fz_hash[fn_hash(k, fz)];
                hlist_for_each_entry(f, node, head, fn_hash) {
@@ -261,6 +264,7 @@ fn_hash_lookup(struct fib_table *tb, const struct flowi *flp, struct fib_result
 
                        err = fib_semantic_match(&f->fn_alias,
                                                 flp, res,
+                                                f->fn_key, fz->fz_mask,
                                                 fz->fz_order);
                        if (err <= 0)
                                goto out;
@@ -274,29 +278,6 @@ out:
 
 static int fn_hash_last_dflt=-1;
 
-static int fib_detect_death(struct fib_info *fi, int order,
-                           struct fib_info **last_resort, int *last_idx)
-{
-       struct neighbour *n;
-       int state = NUD_NONE;
-
-       n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
-       if (n) {
-               state = n->nud_state;
-               neigh_release(n);
-       }
-       if (state==NUD_REACHABLE)
-               return 0;
-       if ((state&NUD_VALID) && order != fn_hash_last_dflt)
-               return 0;
-       if ((state&NUD_VALID) ||
-           (*last_idx<0 && order > fn_hash_last_dflt)) {
-               *last_resort = fi;
-               *last_idx = order;
-       }
-       return 1;
-}
-
 static void
 fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib_result *res)
 {
@@ -337,7 +318,7 @@ fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib
                                if (next_fi != res->fi)
                                        break;
                        } else if (!fib_detect_death(fi, order, &last_resort,
-                                                    &last_idx)) {
+                                                    &last_idx, &fn_hash_last_dflt)) {
                                if (res->fi)
                                        fib_info_put(res->fi);
                                res->fi = fi;
@@ -355,7 +336,7 @@ fn_hash_select_default(struct fib_table *tb, const struct flowi *flp, struct fib
                goto out;
        }
 
-       if (!fib_detect_death(fi, order, &last_resort, &last_idx)) {
+       if (!fib_detect_death(fi, order, &last_resort, &last_idx, &fn_hash_last_dflt)) {
                if (res->fi)
                        fib_info_put(res->fi);
                res->fi = fi;
@@ -376,11 +357,6 @@ out:
        read_unlock(&fib_hash_lock);
 }
 
-static void rtmsg_fib(int, struct fib_node *, struct fib_alias *,
-                     int, int,
-                     struct nlmsghdr *n,
-                     struct netlink_skb_parms *);
-
 /* Insert node F to FZ. */
 static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
 {
@@ -390,7 +366,7 @@ static inline void fib_insert_node(struct fn_zone *fz, struct fib_node *f)
 }
 
 /* Return the node in FZ matching KEY. */
-static struct fib_node *fib_find_node(struct fn_zone *fz, u32 key)
+static struct fib_node *fib_find_node(struct fn_zone *fz, __be32 key)
 {
        struct hlist_head *head = &fz->fz_hash[fn_hash(key, fz)];
        struct hlist_node *node;
@@ -404,66 +380,47 @@ static struct fib_node *fib_find_node(struct fn_zone *fz, u32 key)
        return NULL;
 }
 
-/* Return the first fib alias matching TOS with
- * priority less than or equal to PRIO.
- */
-static struct fib_alias *fib_find_alias(struct fib_node *fn, u8 tos, u32 prio)
-{
-       if (fn) {
-               struct list_head *head = &fn->fn_alias;
-               struct fib_alias *fa;
-
-               list_for_each_entry(fa, head, fa_list) {
-                       if (fa->fa_tos > tos)
-                               continue;
-                       if (fa->fa_info->fib_priority >= prio ||
-                           fa->fa_tos < tos)
-                               return fa;
-               }
-       }
-       return NULL;
-}
-
-static int
-fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
-              struct nlmsghdr *n, struct netlink_skb_parms *req)
+static int fn_hash_insert(struct fib_table *tb, struct fib_config *cfg)
 {
        struct fn_hash *table = (struct fn_hash *) tb->tb_data;
        struct fib_node *new_f, *f;
        struct fib_alias *fa, *new_fa;
        struct fn_zone *fz;
        struct fib_info *fi;
-       int z = r->rtm_dst_len;
-       int type = r->rtm_type;
-       u8 tos = r->rtm_tos;
-       u32 key;
+       u8 tos = cfg->fc_tos;
+       __be32 key;
        int err;
 
-       if (z > 32)
+       if (cfg->fc_dst_len > 32)
                return -EINVAL;
-       fz = table->fn_zones[z];
-       if (!fz && !(fz = fn_new_zone(table, z)))
+
+       fz = table->fn_zones[cfg->fc_dst_len];
+       if (!fz && !(fz = fn_new_zone(table, cfg->fc_dst_len)))
                return -ENOBUFS;
 
        key = 0;
-       if (rta->rta_dst) {
-               u32 dst;
-               memcpy(&dst, rta->rta_dst, 4);
-               if (dst & ~FZ_MASK(fz))
+       if (cfg->fc_dst) {
+               if (cfg->fc_dst & ~FZ_MASK(fz))
                        return -EINVAL;
-               key = fz_key(dst, fz);
+               key = fz_key(cfg->fc_dst, fz);
        }
 
-       if  ((fi = fib_create_info(r, rta, n, &err)) == NULL)
-               return err;
+       fi = fib_create_info(cfg);
+       if (IS_ERR(fi))
+               return PTR_ERR(fi);
 
        if (fz->fz_nent > (fz->fz_divisor<<1) &&
            fz->fz_divisor < FZ_MAX_DIVISOR &&
-           (z==32 || (1<<z) > fz->fz_divisor))
+           (cfg->fc_dst_len == 32 ||
+            (1 << cfg->fc_dst_len) > fz->fz_divisor))
                fn_rehash_zone(fz);
 
        f = fib_find_node(fz, key);
-       fa = fib_find_alias(f, tos, fi->fib_priority);
+
+       if (!f)
+               fa = NULL;
+       else
+               fa = fib_find_alias(&f->fn_alias, tos, fi->fib_priority);
 
        /* Now fa, if non-NULL, points to the first fib alias
         * with the same keys [prefix,tos,priority], if such key already
@@ -481,20 +438,21 @@ fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
                struct fib_alias *fa_orig;
 
                err = -EEXIST;
-               if (n->nlmsg_flags & NLM_F_EXCL)
+               if (cfg->fc_nlflags & NLM_F_EXCL)
                        goto out;
 
-               if (n->nlmsg_flags & NLM_F_REPLACE) {
+               if (cfg->fc_nlflags & NLM_F_REPLACE) {
                        struct fib_info *fi_drop;
                        u8 state;
 
                        write_lock_bh(&fib_hash_lock);
                        fi_drop = fa->fa_info;
                        fa->fa_info = fi;
-                       fa->fa_type = type;
-                       fa->fa_scope = r->rtm_scope;
+                       fa->fa_type = cfg->fc_type;
+                       fa->fa_scope = cfg->fc_scope;
                        state = fa->fa_state;
                        fa->fa_state &= ~FA_S_ACCESSED;
+                       fib_hash_genid++;
                        write_unlock_bh(&fib_hash_lock);
 
                        fib_release_info(fi_drop);
@@ -514,27 +472,27 @@ fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
                                break;
                        if (fa->fa_info->fib_priority != fi->fib_priority)
                                break;
-                       if (fa->fa_type == type &&
-                           fa->fa_scope == r->rtm_scope &&
+                       if (fa->fa_type == cfg->fc_type &&
+                           fa->fa_scope == cfg->fc_scope &&
                            fa->fa_info == fi)
                                goto out;
                }
-               if (!(n->nlmsg_flags & NLM_F_APPEND))
+               if (!(cfg->fc_nlflags & NLM_F_APPEND))
                        fa = fa_orig;
        }
 
        err = -ENOENT;
-       if (!(n->nlmsg_flags&NLM_F_CREATE))
+       if (!(cfg->fc_nlflags & NLM_F_CREATE))
                goto out;
 
        err = -ENOBUFS;
-       new_fa = kmem_cache_alloc(fn_alias_kmem, SLAB_KERNEL);
+       new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
        if (new_fa == NULL)
                goto out;
 
        new_f = NULL;
        if (!f) {
-               new_f = kmem_cache_alloc(fn_hash_kmem, SLAB_KERNEL);
+               new_f = kmem_cache_alloc(fn_hash_kmem, GFP_KERNEL);
                if (new_f == NULL)
                        goto out_free_new_fa;
 
@@ -546,8 +504,8 @@ fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
 
        new_fa->fa_info = fi;
        new_fa->fa_tos = tos;
-       new_fa->fa_type = type;
-       new_fa->fa_scope = r->rtm_scope;
+       new_fa->fa_type = cfg->fc_type;
+       new_fa->fa_scope = cfg->fc_scope;
        new_fa->fa_state = 0;
 
        /*
@@ -559,13 +517,15 @@ fn_hash_insert(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
                fib_insert_node(fz, new_f);
        list_add_tail(&new_fa->fa_list,
                 (fa ? &fa->fa_list : &f->fn_alias));
+       fib_hash_genid++;
        write_unlock_bh(&fib_hash_lock);
 
        if (new_f)
                fz->fz_nent++;
        rt_cache_flush(-1);
 
-       rtmsg_fib(RTM_NEWROUTE, f, new_fa, z, tb->tb_id, n, req);
+       rtmsg_fib(RTM_NEWROUTE, key, new_fa, cfg->fc_dst_len, tb->tb_id,
+                 &cfg->fc_nlinfo);
        return 0;
 
 out_free_new_fa:
@@ -576,34 +536,33 @@ out:
 }
 
 
-static int
-fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
-              struct nlmsghdr *n, struct netlink_skb_parms *req)
+static int fn_hash_delete(struct fib_table *tb, struct fib_config *cfg)
 {
        struct fn_hash *table = (struct fn_hash*)tb->tb_data;
        struct fib_node *f;
        struct fib_alias *fa, *fa_to_delete;
-       int z = r->rtm_dst_len;
        struct fn_zone *fz;
-       u32 key;
-       u8 tos = r->rtm_tos;
+       __be32 key;
 
-       if (z > 32)
+       if (cfg->fc_dst_len > 32)
                return -EINVAL;
-       if ((fz  = table->fn_zones[z]) == NULL)
+
+       if ((fz  = table->fn_zones[cfg->fc_dst_len]) == NULL)
                return -ESRCH;
 
        key = 0;
-       if (rta->rta_dst) {
-               u32 dst;
-               memcpy(&dst, rta->rta_dst, 4);
-               if (dst & ~FZ_MASK(fz))
+       if (cfg->fc_dst) {
+               if (cfg->fc_dst & ~FZ_MASK(fz))
                        return -EINVAL;
-               key = fz_key(dst, fz);
+               key = fz_key(cfg->fc_dst, fz);
        }
 
        f = fib_find_node(fz, key);
-       fa = fib_find_alias(f, tos, 0);
+
+       if (!f)
+               fa = NULL;
+       else
+               fa = fib_find_alias(&f->fn_alias, cfg->fc_tos, 0);
        if (!fa)
                return -ESRCH;
 
@@ -612,16 +571,16 @@ fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
        list_for_each_entry_continue(fa, &f->fn_alias, fa_list) {
                struct fib_info *fi = fa->fa_info;
 
-               if (fa->fa_tos != tos)
+               if (fa->fa_tos != cfg->fc_tos)
                        break;
 
-               if ((!r->rtm_type ||
-                    fa->fa_type == r->rtm_type) &&
-                   (r->rtm_scope == RT_SCOPE_NOWHERE ||
-                    fa->fa_scope == r->rtm_scope) &&
-                   (!r->rtm_protocol ||
-                    fi->fib_protocol == r->rtm_protocol) &&
-                   fib_nh_match(r, n, rta, fi) == 0) {
+               if ((!cfg->fc_type ||
+                    fa->fa_type == cfg->fc_type) &&
+                   (cfg->fc_scope == RT_SCOPE_NOWHERE ||
+                    fa->fa_scope == cfg->fc_scope) &&
+                   (!cfg->fc_protocol ||
+                    fi->fib_protocol == cfg->fc_protocol) &&
+                   fib_nh_match(cfg, fi) == 0) {
                        fa_to_delete = fa;
                        break;
                }
@@ -631,7 +590,8 @@ fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
                int kill_fn;
 
                fa = fa_to_delete;
-               rtmsg_fib(RTM_DELROUTE, f, fa, z, tb->tb_id, n, req);
+               rtmsg_fib(RTM_DELROUTE, key, fa, cfg->fc_dst_len,
+                         tb->tb_id, &cfg->fc_nlinfo);
 
                kill_fn = 0;
                write_lock_bh(&fib_hash_lock);
@@ -640,6 +600,7 @@ fn_hash_delete(struct fib_table *tb, struct rtmsg *r, struct kern_rta *rta,
                        hlist_del(&f->fn_hash);
                        kill_fn = 1;
                }
+               fib_hash_genid++;
                write_unlock_bh(&fib_hash_lock);
 
                if (fa->fa_state & FA_S_ACCESSED)
@@ -677,6 +638,7 @@ static int fn_flush_list(struct fn_zone *fz, int idx)
                                        hlist_del(&f->fn_hash);
                                        kill_f = 1;
                                }
+                               fib_hash_genid++;
                                write_unlock_bh(&fib_hash_lock);
 
                                fn_free_alias(fa);
@@ -717,14 +679,14 @@ fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
        struct fib_node *f;
        int i, s_i;
 
-       s_i = cb->args[3];
+       s_i = cb->args[4];
        i = 0;
        hlist_for_each_entry(f, node, head, fn_hash) {
                struct fib_alias *fa;
 
                list_for_each_entry(fa, &f->fn_alias, fa_list) {
                        if (i < s_i)
-                               continue;
+                               goto next;
 
                        if (fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
                                          cb->nlh->nlmsg_seq,
@@ -732,18 +694,19 @@ fn_hash_dump_bucket(struct sk_buff *skb, struct netlink_callback *cb,
                                          tb->tb_id,
                                          fa->fa_type,
                                          fa->fa_scope,
-                                         &f->fn_key,
+                                         f->fn_key,
                                          fz->fz_order,
                                          fa->fa_tos,
-                                         fa->fa_info) < 0) {
-                               cb->args[3] = i;
+                                         fa->fa_info,
+                                         NLM_F_MULTI) < 0) {
+                               cb->args[4] = i;
                                return -1;
                        }
-
+               next:
                        i++;
                }
        }
-       cb->args[3] = i;
+       cb->args[4] = i;
        return skb->len;
 }
 
@@ -754,21 +717,21 @@ fn_hash_dump_zone(struct sk_buff *skb, struct netlink_callback *cb,
 {
        int h, s_h;
 
-       s_h = cb->args[2];
+       s_h = cb->args[3];
        for (h=0; h < fz->fz_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 (fz->fz_hash == NULL ||
                    hlist_empty(&fz->fz_hash[h]))
                        continue;
                if (fn_hash_dump_bucket(skb, cb, tb, fz, &fz->fz_hash[h])<0) {
-                       cb->args[2] = h;
+                       cb->args[3] = h;
                        return -1;
                }
        }
-       cb->args[2] = h;
+       cb->args[3] = h;
        return skb->len;
 }
 
@@ -778,55 +741,28 @@ static int fn_hash_dump(struct fib_table *tb, struct sk_buff *skb, struct netlin
        struct fn_zone *fz;
        struct fn_hash *table = (struct fn_hash*)tb->tb_data;
 
-       s_m = cb->args[1];
+       s_m = cb->args[2];
        read_lock(&fib_hash_lock);
        for (fz = table->fn_zone_list, m=0; fz; fz = fz->fz_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 (fn_hash_dump_zone(skb, cb, tb, fz) < 0) {
-                       cb->args[1] = m;
+                       cb->args[2] = m;
                        read_unlock(&fib_hash_lock);
                        return -1;
                }
        }
        read_unlock(&fib_hash_lock);
-       cb->args[1] = m;
+       cb->args[2] = m;
        return skb->len;
 }
 
-static void rtmsg_fib(int event, struct fib_node *f, struct fib_alias *fa,
-                     int z, int tb_id,
-                     struct nlmsghdr *n, 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 (fib_dump_info(skb, pid, n->nlmsg_seq, event, tb_id,
-                         fa->fa_type, fa->fa_scope, &f->fn_key, z,
-                         fa->fa_tos,
-                         fa->fa_info) < 0) {
-               kfree_skb(skb);
-               return;
-       }
-       NETLINK_CB(skb).dst_groups = RTMGRP_IPV4_ROUTE;
-       if (n->nlmsg_flags&NLM_F_ECHO)
-               atomic_inc(&skb->users);
-       netlink_broadcast(rtnl, skb, pid, RTMGRP_IPV4_ROUTE, GFP_KERNEL);
-       if (n->nlmsg_flags&NLM_F_ECHO)
-               netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
-}
-
 #ifdef CONFIG_IP_MULTIPLE_TABLES
-struct fib_table * fib_hash_init(int id)
+struct fib_table * fib_hash_init(u32 id)
 #else
-struct fib_table * __init fib_hash_init(int id)
+struct fib_table * __init fib_hash_init(u32 id)
 #endif
 {
        struct fib_table *tb;
@@ -868,6 +804,9 @@ struct fib_iter_state {
        struct hlist_head *hash_head;
        struct fib_node *fn;
        struct fib_alias *fa;
+       loff_t pos;
+       unsigned int genid;
+       int valid;
 };
 
 static struct fib_alias *fib_get_first(struct seq_file *seq)
@@ -879,6 +818,9 @@ static struct fib_alias *fib_get_first(struct seq_file *seq)
        iter->hash_head = NULL;
        iter->fn        = NULL;
        iter->fa        = NULL;
+       iter->pos       = 0;
+       iter->genid     = fib_hash_genid;
+       iter->valid     = 1;
 
        for (iter->zone = table->fn_zone_list; iter->zone;
             iter->zone = iter->zone->fz_next) {
@@ -983,16 +925,34 @@ static struct fib_alias *fib_get_next(struct seq_file *seq)
                }
        }
 out:
+       iter->pos++;
        return fa;
 }
 
+static struct fib_alias *fib_get_idx(struct seq_file *seq, loff_t pos)
+{
+       struct fib_iter_state *iter = seq->private;
+       struct fib_alias *fa;
+       
+       if (iter->valid && pos >= iter->pos && iter->genid == fib_hash_genid) {
+               fa   = iter->fa;
+               pos -= iter->pos;
+       } else
+               fa = fib_get_first(seq);
+
+       if (fa)
+               while (pos && (fa = fib_get_next(seq)))
+                       --pos;
+       return pos ? NULL : fa;
+}
+
 static void *fib_seq_start(struct seq_file *seq, loff_t *pos)
 {
        void *v = NULL;
 
        read_lock(&fib_hash_lock);
        if (ip_fib_main_table)
-               v = *pos ? fib_get_next(seq) : SEQ_START_TOKEN;
+               v = *pos ? fib_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
        return v;
 }
 
@@ -1007,16 +967,16 @@ static void fib_seq_stop(struct seq_file *seq, void *v)
        read_unlock(&fib_hash_lock);
 }
 
-static unsigned fib_flag_trans(int type, u32 mask, struct fib_info *fi)
+static unsigned fib_flag_trans(int type, __be32 mask, struct fib_info *fi)
 {
-       static unsigned type2flags[RTN_MAX + 1] = {
+       static const unsigned type2flags[RTN_MAX + 1] = {
                [7] = RTF_REJECT, [8] = RTF_REJECT,
        };
        unsigned flags = type2flags[type];
 
        if (fi && fi->fib_nh->nh_gw)
                flags |= RTF_GATEWAY;
-       if (mask == 0xFFFFFFFF)
+       if (mask == htonl(0xFFFFFFFF))
                flags |= RTF_HOST;
        flags |= RTF_UP;
        return flags;
@@ -1034,7 +994,7 @@ static int fib_seq_show(struct seq_file *seq, void *v)
 {
        struct fib_iter_state *iter;
        char bf[128];
-       u32 prefix, mask;
+       __be32 prefix, mask;
        unsigned flags;
        struct fib_node *f;
        struct fib_alias *fa;
@@ -1083,7 +1043,7 @@ static int fib_seq_open(struct inode *inode, struct file *file)
 {
        struct seq_file *seq;
        int rc = -ENOMEM;
-       struct fib_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
+       struct fib_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
        
        if (!s)
                goto out;
@@ -1094,7 +1054,6 @@ static int fib_seq_open(struct inode *inode, struct file *file)
 
        seq          = file->private_data;
        seq->private = s;
-       memset(s, 0, sizeof(*s));
 out:
        return rc;
 out_kfree: