]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - net/ipv4/route.c
IPV4: use xor rather than multiple ands for route compare
[net-next-2.6.git] / net / ipv4 / route.c
index 4313255e5a14f36c8d10082ddaef11e0d7642e52..139799f8a8a125f9113e7fed020c87ac0947f4e8 100644 (file)
 
 #define RT_GC_TIMEOUT (300*HZ)
 
-static int ip_rt_min_delay             = 2 * HZ;
-static int ip_rt_max_delay             = 10 * HZ;
 static int ip_rt_max_size;
-static int ip_rt_gc_timeout            = RT_GC_TIMEOUT;
-static int ip_rt_gc_interval           = 60 * HZ;
-static int ip_rt_gc_min_interval       = HZ / 2;
-static int ip_rt_redirect_number       = 9;
-static int ip_rt_redirect_load         = HZ / 50;
-static int ip_rt_redirect_silence      = ((HZ / 50) << (9 + 1));
-static int ip_rt_error_cost            = HZ;
-static int ip_rt_error_burst           = 5 * HZ;
-static int ip_rt_gc_elasticity         = 8;
-static int ip_rt_mtu_expires           = 10 * 60 * HZ;
-static int ip_rt_min_pmtu              = 512 + 20 + 20;
-static int ip_rt_min_advmss            = 256;
-static int ip_rt_secret_interval       = 10 * 60 * HZ;
-static int ip_rt_flush_expected;
-static unsigned long rt_deadline;
-
-#define RTprint(a...)  printk(KERN_DEBUG a)
-
-static struct timer_list rt_flush_timer;
+static int ip_rt_gc_timeout __read_mostly      = RT_GC_TIMEOUT;
+static int ip_rt_gc_interval __read_mostly     = 60 * HZ;
+static int ip_rt_gc_min_interval __read_mostly = HZ / 2;
+static int ip_rt_redirect_number __read_mostly = 9;
+static int ip_rt_redirect_load __read_mostly   = HZ / 50;
+static int ip_rt_redirect_silence __read_mostly        = ((HZ / 50) << (9 + 1));
+static int ip_rt_error_cost __read_mostly      = HZ;
+static int ip_rt_error_burst __read_mostly     = 5 * HZ;
+static int ip_rt_gc_elasticity __read_mostly   = 8;
+static int ip_rt_mtu_expires __read_mostly     = 10 * 60 * HZ;
+static int ip_rt_min_pmtu __read_mostly                = 512 + 20 + 20;
+static int ip_rt_min_advmss __read_mostly      = 256;
+static int ip_rt_secret_interval __read_mostly = 10 * 60 * HZ;
+
 static void rt_worker_func(struct work_struct *work);
 static DECLARE_DELAYED_WORK(expires_work, rt_worker_func);
 static struct timer_list rt_secret_timer;
@@ -169,6 +162,7 @@ static struct dst_ops ipv4_dst_ops = {
        .update_pmtu =          ip_rt_update_pmtu,
        .local_out =            ip_local_out,
        .entry_size =           sizeof(struct rtable),
+       .entries =              ATOMIC_INIT(0),
 };
 
 #define ECN_OR_COST(class)     TC_PRIO_##class
@@ -256,52 +250,53 @@ static inline void rt_hash_lock_init(void)
 }
 #endif
 
-static struct rt_hash_bucket   *rt_hash_table;
-static unsigned                        rt_hash_mask;
-static unsigned int            rt_hash_log;
-static unsigned int            rt_hash_rnd;
+static struct rt_hash_bucket   *rt_hash_table __read_mostly;
+static unsigned                        rt_hash_mask __read_mostly;
+static unsigned int            rt_hash_log  __read_mostly;
+static atomic_t                        rt_genid __read_mostly;
 
 static DEFINE_PER_CPU(struct rt_cache_stat, rt_cache_stat);
 #define RT_CACHE_STAT_INC(field) \
        (__raw_get_cpu_var(rt_cache_stat).field++)
 
-static int rt_intern_hash(unsigned hash, struct rtable *rth,
-                               struct rtable **res);
-
-static unsigned int rt_hash_code(u32 daddr, u32 saddr)
+static inline unsigned int rt_hash(__be32 daddr, __be32 saddr, int idx)
 {
-       return (jhash_2words(daddr, saddr, rt_hash_rnd)
-               & rt_hash_mask);
+       return jhash_3words((__force u32)(__be32)(daddr),
+                           (__force u32)(__be32)(saddr),
+                           idx, atomic_read(&rt_genid))
+               & rt_hash_mask;
 }
 
-#define rt_hash(daddr, saddr, idx) \
-       rt_hash_code((__force u32)(__be32)(daddr),\
-                    (__force u32)(__be32)(saddr) ^ ((idx) << 5))
-
 #ifdef CONFIG_PROC_FS
 struct rt_cache_iter_state {
+       struct seq_net_private p;
        int bucket;
+       int genid;
 };
 
 static struct rtable *rt_cache_get_first(struct seq_file *seq)
 {
-       struct rtable *r = NULL;
        struct rt_cache_iter_state *st = seq->private;
+       struct rtable *r = NULL;
 
        for (st->bucket = rt_hash_mask; st->bucket >= 0; --st->bucket) {
                rcu_read_lock_bh();
-               r = rt_hash_table[st->bucket].chain;
-               if (r)
-                       break;
+               r = rcu_dereference(rt_hash_table[st->bucket].chain);
+               while (r) {
+                       if (dev_net(r->u.dst.dev) == seq_file_net(seq) &&
+                           r->rt_genid == st->genid)
+                               return r;
+                       r = rcu_dereference(r->u.dst.rt_next);
+               }
                rcu_read_unlock_bh();
        }
-       return rcu_dereference(r);
+       return r;
 }
 
-static struct rtable *rt_cache_get_next(struct seq_file *seq, struct rtable *r)
+static struct rtable *__rt_cache_get_next(struct seq_file *seq,
+                                         struct rtable *r)
 {
        struct rt_cache_iter_state *st = seq->private;
-
        r = r->u.dst.rt_next;
        while (!r) {
                rcu_read_unlock_bh();
@@ -313,6 +308,19 @@ static struct rtable *rt_cache_get_next(struct seq_file *seq, struct rtable *r)
        return rcu_dereference(r);
 }
 
+static struct rtable *rt_cache_get_next(struct seq_file *seq,
+                                       struct rtable *r)
+{
+       struct rt_cache_iter_state *st = seq->private;
+       while ((r = __rt_cache_get_next(seq, r)) != NULL) {
+               if (dev_net(r->u.dst.dev) != seq_file_net(seq))
+                       continue;
+               if (r->rt_genid == st->genid)
+                       break;
+       }
+       return r;
+}
+
 static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
 {
        struct rtable *r = rt_cache_get_first(seq);
@@ -325,12 +333,16 @@ static struct rtable *rt_cache_get_idx(struct seq_file *seq, loff_t pos)
 
 static void *rt_cache_seq_start(struct seq_file *seq, loff_t *pos)
 {
-       return *pos ? rt_cache_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
+       struct rt_cache_iter_state *st = seq->private;
+       if (*pos)
+               return rt_cache_get_idx(seq, *pos - 1);
+       st->genid = atomic_read(&rt_genid);
+       return SEQ_START_TOKEN;
 }
 
 static void *rt_cache_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
-       struct rtable *r = NULL;
+       struct rtable *r;
 
        if (v == SEQ_START_TOKEN)
                r = rt_cache_get_first(seq);
@@ -387,7 +399,7 @@ static const struct seq_operations rt_cache_seq_ops = {
 
 static int rt_cache_seq_open(struct inode *inode, struct file *file)
 {
-       return seq_open_private(file, &rt_cache_seq_ops,
+       return seq_open_net(inode, file, &rt_cache_seq_ops,
                        sizeof(struct rt_cache_iter_state));
 }
 
@@ -396,7 +408,7 @@ static const struct file_operations rt_cache_seq_fops = {
        .open    = rt_cache_seq_open,
        .read    = seq_read,
        .llseek  = seq_lseek,
-       .release = seq_release_private,
+       .release = seq_release_net,
 };
 
 
@@ -530,7 +542,7 @@ static int ip_rt_acct_read(char *buffer, char **start, off_t offset,
 }
 #endif
 
-static __init int ip_rt_proc_init(struct net *net)
+static int __net_init ip_rt_do_proc_init(struct net *net)
 {
        struct proc_dir_entry *pde;
 
@@ -539,12 +551,11 @@ static __init int ip_rt_proc_init(struct net *net)
        if (!pde)
                goto err1;
 
-       pde = create_proc_entry("rt_cache", S_IRUGO, net->proc_net_stat);
+       pde = proc_create("rt_cache", S_IRUGO,
+                         net->proc_net_stat, &rt_cpu_seq_fops);
        if (!pde)
                goto err2;
 
-       pde->proc_fops = &rt_cpu_seq_fops;
-
 #ifdef CONFIG_NET_CLS_ROUTE
        pde = create_proc_read_entry("rt_acct", 0, net->proc_net,
                        ip_rt_acct_read, NULL);
@@ -562,25 +573,43 @@ err2:
 err1:
        return -ENOMEM;
 }
+
+static void __net_exit ip_rt_do_proc_exit(struct net *net)
+{
+       remove_proc_entry("rt_cache", net->proc_net_stat);
+       remove_proc_entry("rt_cache", net->proc_net);
+       remove_proc_entry("rt_acct", net->proc_net);
+}
+
+static struct pernet_operations ip_rt_proc_ops __net_initdata =  {
+       .init = ip_rt_do_proc_init,
+       .exit = ip_rt_do_proc_exit,
+};
+
+static int __init ip_rt_proc_init(void)
+{
+       return register_pernet_subsys(&ip_rt_proc_ops);
+}
+
 #else
-static inline int ip_rt_proc_init(struct net *net)
+static inline int ip_rt_proc_init(void)
 {
        return 0;
 }
 #endif /* CONFIG_PROC_FS */
 
-static __inline__ void rt_free(struct rtable *rt)
+static inline void rt_free(struct rtable *rt)
 {
        call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
 }
 
-static __inline__ void rt_drop(struct rtable *rt)
+static inline void rt_drop(struct rtable *rt)
 {
        ip_rt_put(rt);
        call_rcu_bh(&rt->u.dst.rcu_head, dst_rcu_free);
 }
 
-static __inline__ int rt_fast_clean(struct rtable *rth)
+static inline int rt_fast_clean(struct rtable *rth)
 {
        /* Kill broadcast/multicast entries very aggresively, if they
           collide in hash table with more useful entries */
@@ -588,7 +617,7 @@ static __inline__ int rt_fast_clean(struct rtable *rth)
                rth->fl.iif && rth->u.dst.rt_next;
 }
 
-static __inline__ int rt_valuable(struct rtable *rth)
+static inline int rt_valuable(struct rtable *rth)
 {
        return (rth->rt_flags & (RTCF_REDIRECTED | RTCF_NOTIFY)) ||
                rth->u.dst.expires;
@@ -648,6 +677,11 @@ static inline int compare_keys(struct flowi *fl1, struct flowi *fl2)
                (fl1->iif ^ fl2->iif)) == 0;
 }
 
+static inline int compare_netns(struct rtable *rt1, struct rtable *rt2)
+{
+       return dev_net(rt1->u.dst.dev) == dev_net(rt2->u.dst.dev);
+}
+
 /*
  * Perform a full scan of hash table and free all entries.
  * Can be called by a softirq or a process.
@@ -703,6 +737,11 @@ static void rt_check_expire(void)
                        continue;
                spin_lock_bh(rt_hash_lock_addr(i));
                while ((rth = *rthp) != NULL) {
+                       if (rth->rt_genid != atomic_read(&rt_genid)) {
+                               *rthp = rth->u.dst.rt_next;
+                               rt_free(rth);
+                               continue;
+                       }
                        if (rth->u.dst.expires) {
                                /* Entry is expired even if it is in use */
                                if (time_before_eq(jiffies, rth->u.dst.expires)) {
@@ -727,83 +766,45 @@ static void rt_check_expire(void)
 
 /*
  * rt_worker_func() is run in process context.
- * If a whole flush was scheduled, it is done.
- * Else, we call rt_check_expire() to scan part of the hash table
+ * we call rt_check_expire() to scan part of the hash table
  */
 static void rt_worker_func(struct work_struct *work)
 {
-       if (ip_rt_flush_expected) {
-               ip_rt_flush_expected = 0;
-               rt_do_flush(1);
-       } else
-               rt_check_expire();
+       rt_check_expire();
        schedule_delayed_work(&expires_work, ip_rt_gc_interval);
 }
 
-/* This can run from both BH and non-BH contexts, the latter
- * in the case of a forced flush event.
+/*
+ * Pertubation of rt_genid by a small quantity [1..256]
+ * Using 8 bits of shuffling ensure we can call rt_cache_invalidate()
+ * many times (2^24) without giving recent rt_genid.
+ * Jenkins hash is strong enough that litle changes of rt_genid are OK.
  */
-static void rt_run_flush(unsigned long process_context)
+static void rt_cache_invalidate(void)
 {
-       rt_deadline = 0;
+       unsigned char shuffle;
 
-       get_random_bytes(&rt_hash_rnd, 4);
-
-       rt_do_flush(process_context);
+       get_random_bytes(&shuffle, sizeof(shuffle));
+       atomic_add(shuffle + 1U, &rt_genid);
 }
 
-static DEFINE_SPINLOCK(rt_flush_lock);
-
+/*
+ * delay < 0  : invalidate cache (fast : entries will be deleted later)
+ * delay >= 0 : invalidate & flush cache (can be long)
+ */
 void rt_cache_flush(int delay)
 {
-       unsigned long now = jiffies;
-       int user_mode = !in_softirq();
-
-       if (delay < 0)
-               delay = ip_rt_min_delay;
-
-       spin_lock_bh(&rt_flush_lock);
-
-       if (del_timer(&rt_flush_timer) && delay > 0 && rt_deadline) {
-               long tmo = (long)(rt_deadline - now);
-
-               /* If flush timer is already running
-                  and flush request is not immediate (delay > 0):
-
-                  if deadline is not achieved, prolongate timer to "delay",
-                  otherwise fire it at deadline time.
-                */
-
-               if (user_mode && tmo < ip_rt_max_delay-ip_rt_min_delay)
-                       tmo = 0;
-
-               if (delay > tmo)
-                       delay = tmo;
-       }
-
-       if (delay <= 0) {
-               spin_unlock_bh(&rt_flush_lock);
-               rt_run_flush(user_mode);
-               return;
-       }
-
-       if (rt_deadline == 0)
-               rt_deadline = now + ip_rt_max_delay;
-
-       mod_timer(&rt_flush_timer, now+delay);
-       spin_unlock_bh(&rt_flush_lock);
+       rt_cache_invalidate();
+       if (delay >= 0)
+               rt_do_flush(!in_softirq());
 }
 
 /*
- * We change rt_hash_rnd and ask next rt_worker_func() invocation
- * to perform a flush in process context
+ * We change rt_genid and let gc do the cleanup
  */
 static void rt_secret_rebuild(unsigned long dummy)
 {
-       get_random_bytes(&rt_hash_rnd, 4);
-       ip_rt_flush_expected = 1;
-       cancel_delayed_work(&expires_work);
-       schedule_delayed_work(&expires_work, HZ/10);
+       rt_cache_invalidate();
        mod_timer(&rt_secret_timer, jiffies + ip_rt_secret_interval);
 }
 
@@ -880,7 +881,8 @@ static int rt_garbage_collect(struct dst_ops *ops)
                        rthp = &rt_hash_table[k].chain;
                        spin_lock_bh(rt_hash_lock_addr(k));
                        while ((rth = *rthp) != NULL) {
-                               if (!rt_may_expire(rth, tmo, expire)) {
+                               if (rth->rt_genid == atomic_read(&rt_genid) &&
+                                       !rt_may_expire(rth, tmo, expire)) {
                                        tmo >>= 1;
                                        rthp = &rth->u.dst.rt_next;
                                        continue;
@@ -961,7 +963,12 @@ restart:
 
        spin_lock_bh(rt_hash_lock_addr(hash));
        while ((rth = *rthp) != NULL) {
-               if (compare_keys(&rth->fl, &rt->fl)) {
+               if (rth->rt_genid != atomic_read(&rt_genid)) {
+                       *rthp = rth->u.dst.rt_next;
+                       rt_free(rth);
+                       continue;
+               }
+               if (compare_keys(&rth->fl, &rt->fl) && compare_netns(rth, rt)) {
                        /* Put it first */
                        *rthp = rth->u.dst.rt_next;
                        /*
@@ -1126,17 +1133,19 @@ void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more)
 
 static void rt_del(unsigned hash, struct rtable *rt)
 {
-       struct rtable **rthp;
+       struct rtable **rthp, *aux;
 
+       rthp = &rt_hash_table[hash].chain;
        spin_lock_bh(rt_hash_lock_addr(hash));
        ip_rt_put(rt);
-       for (rthp = &rt_hash_table[hash].chain; *rthp;
-            rthp = &(*rthp)->u.dst.rt_next)
-               if (*rthp == rt) {
-                       *rthp = rt->u.dst.rt_next;
-                       rt_free(rt);
-                       break;
+       while ((aux = *rthp) != NULL) {
+               if (aux == rt || (aux->rt_genid != atomic_read(&rt_genid))) {
+                       *rthp = aux->u.dst.rt_next;
+                       rt_free(aux);
+                       continue;
                }
+               rthp = &aux->u.dst.rt_next;
+       }
        spin_unlock_bh(rt_hash_lock_addr(hash));
 }
 
@@ -1149,10 +1158,12 @@ void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
        __be32  skeys[2] = { saddr, 0 };
        int  ikeys[2] = { dev->ifindex, 0 };
        struct netevent_redirect netevent;
+       struct net *net;
 
        if (!in_dev)
                return;
 
+       net = dev_net(dev);
        if (new_gw == old_gw || !IN_DEV_RX_REDIRECTS(in_dev)
            || ipv4_is_multicast(new_gw) || ipv4_is_lbcast(new_gw)
            || ipv4_is_zeronet(new_gw))
@@ -1164,7 +1175,7 @@ void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
                if (IN_DEV_SEC_REDIRECTS(in_dev) && ip_fib_check_default(new_gw, dev))
                        goto reject_redirect;
        } else {
-               if (inet_addr_type(&init_net, new_gw) != RTN_UNICAST)
+               if (inet_addr_type(net, new_gw) != RTN_UNICAST)
                        goto reject_redirect;
        }
 
@@ -1181,7 +1192,9 @@ void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
                                if (rth->fl.fl4_dst != daddr ||
                                    rth->fl.fl4_src != skeys[i] ||
                                    rth->fl.oif != ikeys[k] ||
-                                   rth->fl.iif != 0) {
+                                   rth->fl.iif != 0 ||
+                                   rth->rt_genid != atomic_read(&rt_genid) ||
+                                   !net_eq(dev_net(rth->u.dst.dev), net)) {
                                        rthp = &rth->u.dst.rt_next;
                                        continue;
                                }
@@ -1219,7 +1232,7 @@ void ip_rt_redirect(__be32 old_gw, __be32 daddr, __be32 new_gw,
                                rt->u.dst.neighbour     = NULL;
                                rt->u.dst.hh            = NULL;
                                rt->u.dst.xfrm          = NULL;
-
+                               rt->rt_genid            = atomic_read(&rt_genid);
                                rt->rt_flags            |= RTCF_REDIRECTED;
 
                                /* Gateway is different ... */
@@ -1273,7 +1286,7 @@ reject_redirect:
 
 static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
 {
-       struct rtable *rt = (struct rtable*)dst;
+       struct rtable *rt = (struct rtable *)dst;
        struct dst_entry *ret = dst;
 
        if (rt) {
@@ -1314,7 +1327,7 @@ static struct dst_entry *ipv4_negative_advice(struct dst_entry *dst)
 
 void ip_rt_send_redirect(struct sk_buff *skb)
 {
-       struct rtable *rt = (struct rtable*)skb->dst;
+       struct rtable *rt = skb->rtable;
        struct in_device *in_dev = in_dev_get(rt->u.dst.dev);
 
        if (!in_dev)
@@ -1363,7 +1376,7 @@ out:
 
 static int ip_error(struct sk_buff *skb)
 {
-       struct rtable *rt = (struct rtable*)skb->dst;
+       struct rtable *rt = skb->rtable;
        unsigned long now;
        int code;
 
@@ -1405,7 +1418,7 @@ out:      kfree_skb(skb);
 static const unsigned short mtu_plateau[] =
 {32000, 17914, 8166, 4352, 2002, 1492, 576, 296, 216, 128 };
 
-static __inline__ unsigned short guess_mtu(unsigned short old_mtu)
+static inline unsigned short guess_mtu(unsigned short old_mtu)
 {
        int i;
 
@@ -1415,7 +1428,8 @@ static __inline__ unsigned short guess_mtu(unsigned short old_mtu)
        return 68;
 }
 
-unsigned short ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu)
+unsigned short ip_rt_frag_needed(struct net *net, struct iphdr *iph,
+                                unsigned short new_mtu)
 {
        int i;
        unsigned short old_mtu = ntohs(iph->tot_len);
@@ -1438,7 +1452,9 @@ unsigned short ip_rt_frag_needed(struct iphdr *iph, unsigned short new_mtu)
                            rth->rt_dst  == daddr &&
                            rth->rt_src  == iph->saddr &&
                            rth->fl.iif == 0 &&
-                           !(dst_metric_locked(&rth->u.dst, RTAX_MTU))) {
+                           !(dst_metric_locked(&rth->u.dst, RTAX_MTU)) &&
+                           net_eq(dev_net(rth->u.dst.dev), net) &&
+                           rth->rt_genid == atomic_read(&rt_genid)) {
                                unsigned short mtu = new_mtu;
 
                                if (new_mtu < 68 || new_mtu >= old_mtu) {
@@ -1513,9 +1529,9 @@ static void ipv4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
 {
        struct rtable *rt = (struct rtable *) dst;
        struct in_device *idev = rt->idev;
-       if (dev != dev->nd_net->loopback_dev && idev && idev->dev == dev) {
+       if (dev != dev_net(dev)->loopback_dev && idev && idev->dev == dev) {
                struct in_device *loopback_idev =
-                       in_dev_get(dev->nd_net->loopback_dev);
+                       in_dev_get(dev_net(dev)->loopback_dev);
                if (loopback_idev) {
                        rt->idev = loopback_idev;
                        in_dev_put(idev);
@@ -1529,7 +1545,7 @@ static void ipv4_link_failure(struct sk_buff *skb)
 
        icmp_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
 
-       rt = (struct rtable *) skb->dst;
+       rt = skb->rtable;
        if (rt)
                dst_set_expires(&rt->u.dst, 0);
 }
@@ -1559,7 +1575,7 @@ void ip_rt_get_source(u8 *addr, struct rtable *rt)
 
        if (rt->fl.iif == 0)
                src = rt->rt_src;
-       else if (fib_lookup(rt->u.dst.dev->nd_net, &rt->fl, &res) == 0) {
+       else if (fib_lookup(dev_net(rt->u.dst.dev), &rt->fl, &res) == 0) {
                src = FIB_RES_PREFSRC(res);
                fib_res_put(&res);
        } else
@@ -1673,8 +1689,9 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
        rth->fl.oif     = 0;
        rth->rt_gateway = daddr;
        rth->rt_spec_dst= spec_dst;
-       rth->rt_type    = RTN_MULTICAST;
+       rth->rt_genid   = atomic_read(&rt_genid);
        rth->rt_flags   = RTCF_MULTICAST;
+       rth->rt_type    = RTN_MULTICAST;
        if (our) {
                rth->u.dst.input= ip_local_deliver;
                rth->rt_flags |= RTCF_LOCAL;
@@ -1688,7 +1705,7 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
 
        in_dev_put(in_dev);
        hash = rt_hash(daddr, saddr, dev->ifindex);
-       return rt_intern_hash(hash, rth, (struct rtable**) &skb->dst);
+       return rt_intern_hash(hash, rth, &skb->rtable);
 
 e_nobufs:
        in_dev_put(in_dev);
@@ -1731,11 +1748,11 @@ static void ip_handle_martian_source(struct net_device *dev,
 #endif
 }
 
-static inline int __mkroute_input(struct sk_buff *skb,
-                                 struct fib_result* res,
-                                 struct in_device *in_dev,
-                                 __be32 daddr, __be32 saddr, u32 tos,
-                                 struct rtable **result)
+static int __mkroute_input(struct sk_buff *skb,
+                          struct fib_result *res,
+                          struct in_device *in_dev,
+                          __be32 daddr, __be32 saddr, u32 tos,
+                          struct rtable **result)
 {
 
        struct rtable *rth;
@@ -1813,6 +1830,7 @@ static inline int __mkroute_input(struct sk_buff *skb,
 
        rth->u.dst.input = ip_forward;
        rth->u.dst.output = ip_output;
+       rth->rt_genid = atomic_read(&rt_genid);
 
        rt_set_nexthop(rth, res, itag);
 
@@ -1826,11 +1844,11 @@ static inline int __mkroute_input(struct sk_buff *skb,
        return err;
 }
 
-static inline int ip_mkroute_input(struct sk_buff *skb,
-                                  struct fib_result* res,
-                                  const struct flowi *fl,
-                                  struct in_device *in_dev,
-                                  __be32 daddr, __be32 saddr, u32 tos)
+static int ip_mkroute_input(struct sk_buff *skb,
+                           struct fib_result *res,
+                           const struct flowi *fl,
+                           struct in_device *in_dev,
+                           __be32 daddr, __be32 saddr, u32 tos)
 {
        struct rtable* rth = NULL;
        int err;
@@ -1848,7 +1866,7 @@ static inline int ip_mkroute_input(struct sk_buff *skb,
 
        /* put it into the cache */
        hash = rt_hash(daddr, saddr, fl->iif);
-       return rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
+       return rt_intern_hash(hash, rth, &skb->rtable);
 }
 
 /*
@@ -1881,7 +1899,7 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
        __be32          spec_dst;
        int             err = -EINVAL;
        int             free_res = 0;
-       struct net    * net = dev->nd_net;
+       struct net    * net = dev_net(dev);
 
        /* IP on this device is disabled. */
 
@@ -1973,6 +1991,7 @@ local_input:
                goto e_nobufs;
 
        rth->u.dst.output= ip_rt_bug;
+       rth->rt_genid = atomic_read(&rt_genid);
 
        atomic_set(&rth->u.dst.__refcnt, 1);
        rth->u.dst.flags= DST_HOST;
@@ -2003,7 +2022,7 @@ local_input:
        }
        rth->rt_type    = res.type;
        hash = rt_hash(daddr, saddr, fl.iif);
-       err = rt_intern_hash(hash, rth, (struct rtable**)&skb->dst);
+       err = rt_intern_hash(hash, rth, &skb->rtable);
        goto done;
 
 no_route:
@@ -2049,23 +2068,27 @@ int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
        struct rtable * rth;
        unsigned        hash;
        int iif = dev->ifindex;
+       struct net *net;
 
+       net = dev_net(dev);
        tos &= IPTOS_RT_MASK;
        hash = rt_hash(daddr, saddr, iif);
 
        rcu_read_lock();
        for (rth = rcu_dereference(rt_hash_table[hash].chain); rth;
             rth = rcu_dereference(rth->u.dst.rt_next)) {
-               if (rth->fl.fl4_dst == daddr &&
-                   rth->fl.fl4_src == saddr &&
-                   rth->fl.iif == iif &&
-                   rth->fl.oif == 0 &&
+               if (((rth->fl.fl4_dst ^ daddr) |
+                    (rth->fl.fl4_src ^ saddr) |
+                    (rth->fl.iif ^ iif) |
+                    rth->fl.oif |
+                    (rth->fl.fl4_tos ^ tos)) == 0 &&
                    rth->fl.mark == skb->mark &&
-                   rth->fl.fl4_tos == tos) {
+                   net_eq(dev_net(rth->u.dst.dev), net) &&
+                   rth->rt_genid == atomic_read(&rt_genid)) {
                        dst_use(&rth->u.dst, jiffies);
                        RT_CACHE_STAT_INC(in_hit);
                        rcu_read_unlock();
-                       skb->dst = (struct dst_entry*)rth;
+                       skb->rtable = rth;
                        return 0;
                }
                RT_CACHE_STAT_INC(in_hlist_search);
@@ -2107,12 +2130,12 @@ int ip_route_input(struct sk_buff *skb, __be32 daddr, __be32 saddr,
        return ip_route_input_slow(skb, daddr, saddr, tos, dev);
 }
 
-static inline int __mkroute_output(struct rtable **result,
-                                  struct fib_result* res,
-                                  const struct flowi *fl,
-                                  const struct flowi *oldflp,
-                                  struct net_device *dev_out,
-                                  unsigned flags)
+static int __mkroute_output(struct rtable **result,
+                           struct fib_result *res,
+                           const struct flowi *fl,
+                           const struct flowi *oldflp,
+                           struct net_device *dev_out,
+                           unsigned flags)
 {
        struct rtable *rth;
        struct in_device *in_dev;
@@ -2189,6 +2212,7 @@ static inline int __mkroute_output(struct rtable **result,
        rth->rt_spec_dst= fl->fl4_src;
 
        rth->u.dst.output=ip_output;
+       rth->rt_genid = atomic_read(&rt_genid);
 
        RT_CACHE_STAT_INC(out_slow_tot);
 
@@ -2226,12 +2250,12 @@ static inline int __mkroute_output(struct rtable **result,
        return err;
 }
 
-static inline int ip_mkroute_output(struct rtable **rp,
-                                   struct fib_result* res,
-                                   const struct flowi *fl,
-                                   const struct flowi *oldflp,
-                                   struct net_device *dev_out,
-                                   unsigned flags)
+static int ip_mkroute_output(struct rtable **rp,
+                            struct fib_result *res,
+                            const struct flowi *fl,
+                            const struct flowi *oldflp,
+                            struct net_device *dev_out,
+                            unsigned flags)
 {
        struct rtable *rth = NULL;
        int err = __mkroute_output(&rth, res, fl, oldflp, dev_out, flags);
@@ -2248,7 +2272,8 @@ static inline int ip_mkroute_output(struct rtable **rp,
  * Major route resolver routine.
  */
 
-static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
+static int ip_route_output_slow(struct net *net, struct rtable **rp,
+                               const struct flowi *oldflp)
 {
        u32 tos = RT_FL_TOS(oldflp);
        struct flowi fl = { .nl_u = { .ip4_u =
@@ -2260,7 +2285,7 @@ static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
                                                  RT_SCOPE_UNIVERSE),
                                      } },
                            .mark = oldflp->mark,
-                           .iif = init_net.loopback_dev->ifindex,
+                           .iif = net->loopback_dev->ifindex,
                            .oif = oldflp->oif };
        struct fib_result res;
        unsigned flags = 0;
@@ -2282,14 +2307,14 @@ static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
                        goto out;
 
                /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
-               dev_out = ip_dev_find(oldflp->fl4_src);
+               dev_out = ip_dev_find(net, oldflp->fl4_src);
                if (dev_out == NULL)
                        goto out;
 
                /* I removed check for oif == dev_out->oif here.
                   It was wrong for two reasons:
-                  1. ip_dev_find(saddr) can return wrong iface, if saddr is
-                     assigned to multiple interfaces.
+                  1. ip_dev_find(net, saddr) can return wrong iface, if saddr
+                     is assigned to multiple interfaces.
                   2. Moreover, we are allowed to send packets with saddr
                      of another iface. --ANK
                 */
@@ -2322,7 +2347,7 @@ static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
 
 
        if (oldflp->oif) {
-               dev_out = dev_get_by_index(&init_net, oldflp->oif);
+               dev_out = dev_get_by_index(net, oldflp->oif);
                err = -ENODEV;
                if (dev_out == NULL)
                        goto out;
@@ -2356,15 +2381,15 @@ static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
                        fl.fl4_dst = fl.fl4_src = htonl(INADDR_LOOPBACK);
                if (dev_out)
                        dev_put(dev_out);
-               dev_out = init_net.loopback_dev;
+               dev_out = net->loopback_dev;
                dev_hold(dev_out);
-               fl.oif = init_net.loopback_dev->ifindex;
+               fl.oif = net->loopback_dev->ifindex;
                res.type = RTN_LOCAL;
                flags |= RTCF_LOCAL;
                goto make_route;
        }
 
-       if (fib_lookup(&init_net, &fl, &res)) {
+       if (fib_lookup(net, &fl, &res)) {
                res.fi = NULL;
                if (oldflp->oif) {
                        /* Apparently, routing tables are wrong. Assume,
@@ -2403,7 +2428,7 @@ static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
                        fl.fl4_src = fl.fl4_dst;
                if (dev_out)
                        dev_put(dev_out);
-               dev_out = init_net.loopback_dev;
+               dev_out = net->loopback_dev;
                dev_hold(dev_out);
                fl.oif = dev_out->ifindex;
                if (res.fi)
@@ -2419,7 +2444,7 @@ static int ip_route_output_slow(struct rtable **rp, const struct flowi *oldflp)
        else
 #endif
        if (!res.prefixlen && res.type == RTN_UNICAST && !fl.oif)
-               fib_select_default(&init_net, &fl, &res);
+               fib_select_default(net, &fl, &res);
 
        if (!fl.fl4_src)
                fl.fl4_src = FIB_RES_PREFSRC(res);
@@ -2442,7 +2467,8 @@ make_route:
 out:   return err;
 }
 
-int __ip_route_output_key(struct rtable **rp, const struct flowi *flp)
+int __ip_route_output_key(struct net *net, struct rtable **rp,
+                         const struct flowi *flp)
 {
        unsigned hash;
        struct rtable *rth;
@@ -2458,7 +2484,9 @@ int __ip_route_output_key(struct rtable **rp, const struct flowi *flp)
                    rth->fl.oif == flp->oif &&
                    rth->fl.mark == flp->mark &&
                    !((rth->fl.fl4_tos ^ flp->fl4_tos) &
-                           (IPTOS_RT_MASK | RTO_ONLINK))) {
+                           (IPTOS_RT_MASK | RTO_ONLINK)) &&
+                   net_eq(dev_net(rth->u.dst.dev), net) &&
+                   rth->rt_genid == atomic_read(&rt_genid)) {
                        dst_use(&rth->u.dst, jiffies);
                        RT_CACHE_STAT_INC(out_hit);
                        rcu_read_unlock_bh();
@@ -2469,7 +2497,7 @@ int __ip_route_output_key(struct rtable **rp, const struct flowi *flp)
        }
        rcu_read_unlock_bh();
 
-       return ip_route_output_slow(rp, flp);
+       return ip_route_output_slow(net, rp, flp);
 }
 
 EXPORT_SYMBOL_GPL(__ip_route_output_key);
@@ -2485,10 +2513,11 @@ static struct dst_ops ipv4_dst_blackhole_ops = {
        .check                  =       ipv4_dst_check,
        .update_pmtu            =       ipv4_rt_blackhole_update_pmtu,
        .entry_size             =       sizeof(struct rtable),
+       .entries                =       ATOMIC_INIT(0),
 };
 
 
-static int ipv4_dst_blackhole(struct rtable **rp, struct flowi *flp, struct sock *sk)
+static int ipv4_dst_blackhole(struct rtable **rp, struct flowi *flp)
 {
        struct rtable *ort = *rp;
        struct rtable *rt = (struct rtable *)
@@ -2512,6 +2541,7 @@ static int ipv4_dst_blackhole(struct rtable **rp, struct flowi *flp, struct sock
                rt->idev = ort->idev;
                if (rt->idev)
                        in_dev_hold(rt->idev);
+               rt->rt_genid = atomic_read(&rt_genid);
                rt->rt_flags = ort->rt_flags;
                rt->rt_type = ort->rt_type;
                rt->rt_dst = ort->rt_dst;
@@ -2531,11 +2561,12 @@ static int ipv4_dst_blackhole(struct rtable **rp, struct flowi *flp, struct sock
        return (rt ? 0 : -ENOMEM);
 }
 
-int ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk, int flags)
+int ip_route_output_flow(struct net *net, struct rtable **rp, struct flowi *flp,
+                        struct sock *sk, int flags)
 {
        int err;
 
-       if ((err = __ip_route_output_key(rp, flp)) != 0)
+       if ((err = __ip_route_output_key(net, rp, flp)) != 0)
                return err;
 
        if (flp->proto) {
@@ -2546,7 +2577,7 @@ int ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk,
                err = __xfrm_lookup((struct dst_entry **)rp, flp, sk,
                                    flags ? XFRM_LOOKUP_WAIT : 0);
                if (err == -EREMOTE)
-                       err = ipv4_dst_blackhole(rp, flp, sk);
+                       err = ipv4_dst_blackhole(rp, flp);
 
                return err;
        }
@@ -2556,15 +2587,15 @@ int ip_route_output_flow(struct rtable **rp, struct flowi *flp, struct sock *sk,
 
 EXPORT_SYMBOL_GPL(ip_route_output_flow);
 
-int ip_route_output_key(struct rtable **rp, struct flowi *flp)
+int ip_route_output_key(struct net *net, struct rtable **rp, struct flowi *flp)
 {
-       return ip_route_output_flow(rp, flp, NULL, 0);
+       return ip_route_output_flow(net, rp, flp, NULL, 0);
 }
 
 static int rt_fill_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
                        int nowait, unsigned int flags)
 {
-       struct rtable *rt = (struct rtable*)skb->dst;
+       struct rtable *rt = skb->rtable;
        struct rtmsg *r;
        struct nlmsghdr *nlh;
        long expires;
@@ -2657,7 +2688,7 @@ nla_put_failure:
 
 static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
 {
-       struct net *net = in_skb->sk->sk_net;
+       struct net *net = sock_net(in_skb->sk);
        struct rtmsg *rtm;
        struct nlattr *tb[RTA_MAX+1];
        struct rtable *rt = NULL;
@@ -2667,9 +2698,6 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void
        int err;
        struct sk_buff *skb;
 
-       if (net != &init_net)
-               return -EINVAL;
-
        err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv4_policy);
        if (err < 0)
                goto errout;
@@ -2699,7 +2727,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void
        if (iif) {
                struct net_device *dev;
 
-               dev = __dev_get_by_index(&init_net, iif);
+               dev = __dev_get_by_index(net, iif);
                if (dev == NULL) {
                        err = -ENODEV;
                        goto errout_free;
@@ -2711,7 +2739,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void
                err = ip_route_input(skb, dst, src, rtm->rtm_tos, dev);
                local_bh_enable();
 
-               rt = (struct rtable*) skb->dst;
+               rt = skb->rtable;
                if (err == 0 && rt->u.dst.error)
                        err = -rt->u.dst.error;
        } else {
@@ -2725,22 +2753,22 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void
                        },
                        .oif = tb[RTA_OIF] ? nla_get_u32(tb[RTA_OIF]) : 0,
                };
-               err = ip_route_output_key(&rt, &fl);
+               err = ip_route_output_key(net, &rt, &fl);
        }
 
        if (err)
                goto errout_free;
 
-       skb->dst = &rt->u.dst;
+       skb->rtable = rt;
        if (rtm->rtm_flags & RTM_F_NOTIFY)
                rt->rt_flags |= RTCF_NOTIFY;
 
        err = rt_fill_info(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq,
-                               RTM_NEWROUTE, 0, 0);
+                          RTM_NEWROUTE, 0, 0);
        if (err <= 0)
                goto errout_free;
 
-       err = rtnl_unicast(skb, &init_net, NETLINK_CB(in_skb).pid);
+       err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
 errout:
        return err;
 
@@ -2754,6 +2782,9 @@ int ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb)
        struct rtable *rt;
        int h, s_h;
        int idx, s_idx;
+       struct net *net;
+
+       net = sock_net(skb->sk);
 
        s_h = cb->args[0];
        if (s_h < 0)
@@ -2763,7 +2794,9 @@ int ip_rt_dump(struct sk_buff *skb,  struct netlink_callback *cb)
                rcu_read_lock_bh();
                for (rt = rcu_dereference(rt_hash_table[h].chain), idx = 0; rt;
                     rt = rcu_dereference(rt->u.dst.rt_next), idx++) {
-                       if (idx < s_idx)
+                       if (!net_eq(dev_net(rt->u.dst.dev), net) || idx < s_idx)
+                               continue;
+                       if (rt->rt_genid != atomic_read(&rt_genid))
                                continue;
                        skb->dst = dst_clone(&rt->u.dst);
                        if (rt_fill_info(skb, NETLINK_CB(cb->skb).pid,
@@ -2833,24 +2866,6 @@ ctl_table ipv4_route_table[] = {
                .proc_handler   = &ipv4_sysctl_rtcache_flush,
                .strategy       = &ipv4_sysctl_rtcache_flush_strategy,
        },
-       {
-               .ctl_name       = NET_IPV4_ROUTE_MIN_DELAY,
-               .procname       = "min_delay",
-               .data           = &ip_rt_min_delay,
-               .maxlen         = sizeof(int),
-               .mode           = 0644,
-               .proc_handler   = &proc_dointvec_jiffies,
-               .strategy       = &sysctl_jiffies,
-       },
-       {
-               .ctl_name       = NET_IPV4_ROUTE_MAX_DELAY,
-               .procname       = "max_delay",
-               .data           = &ip_rt_max_delay,
-               .maxlen         = sizeof(int),
-               .mode           = 0644,
-               .proc_handler   = &proc_dointvec_jiffies,
-               .strategy       = &sysctl_jiffies,
-       },
        {
                .ctl_name       = NET_IPV4_ROUTE_GC_THRESH,
                .procname       = "gc_thresh",
@@ -3009,8 +3024,8 @@ int __init ip_rt_init(void)
 {
        int rc = 0;
 
-       rt_hash_rnd = (int) ((num_physpages ^ (num_physpages>>8)) ^
-                            (jiffies ^ (jiffies >> 7)));
+       atomic_set(&rt_genid, (int) ((num_physpages ^ (num_physpages>>8)) ^
+                            (jiffies ^ (jiffies >> 7))));
 
 #ifdef CONFIG_NET_CLS_ROUTE
        ip_rt_acct = __alloc_percpu(256 * sizeof(struct ip_rt_acct));
@@ -3043,8 +3058,9 @@ int __init ip_rt_init(void)
        devinet_init();
        ip_fib_init();
 
-       setup_timer(&rt_flush_timer, rt_run_flush, 0);
-       setup_timer(&rt_secret_timer, rt_secret_rebuild, 0);
+       rt_secret_timer.function = rt_secret_rebuild;
+       rt_secret_timer.data = 0;
+       init_timer_deferrable(&rt_secret_timer);
 
        /* All the timers, started at system startup tend
           to synchronize. Perturb it a bit.
@@ -3056,7 +3072,7 @@ int __init ip_rt_init(void)
                ip_rt_secret_interval;
        add_timer(&rt_secret_timer);
 
-       if (ip_rt_proc_init(&init_net))
+       if (ip_rt_proc_init())
                printk(KERN_ERR "Unable to create route proc files\n");
 #ifdef CONFIG_XFRM
        xfrm_init();