ipip: add module alias for tunl0 tunnel device
[linux-2.6.git] / net / ipv4 / fib_semantics.c
index c1263e2..12d3dc3 100644 (file)
@@ -5,8 +5,6 @@
  *
  *             IPv4 Forwarding Information Base: semantics.
  *
- * Version:    $Id: fib_semantics.c,v 1.19 2002/01/12 07:54:56 davem Exp $
- *
  * Authors:    Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  *
  *             This program is free software; you can redistribute it and/or
@@ -34,6 +32,7 @@
 #include <linux/proc_fs.h>
 #include <linux/skbuff.h>
 #include <linux/init.h>
+#include <linux/slab.h>
 
 #include <net/arp.h>
 #include <net/ip.h>
@@ -47,8 +46,6 @@
 
 #include "fib_lookup.h"
 
-#define FSprintk(a...)
-
 static DEFINE_SPINLOCK(fib_info_lock);
 static struct hlist_head *fib_info_hash;
 static struct hlist_head *fib_info_laddrhash;
@@ -63,21 +60,30 @@ static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
 
 static DEFINE_SPINLOCK(fib_multipath_lock);
 
-#define for_nexthops(fi) { int nhsel; const struct fib_nh * nh; \
-for (nhsel=0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
+#define for_nexthops(fi) {                                             \
+       int nhsel; const struct fib_nh *nh;                             \
+       for (nhsel = 0, nh = (fi)->fib_nh;                              \
+            nhsel < (fi)->fib_nhs;                                     \
+            nh++, nhsel++)
 
-#define change_nexthops(fi) { int nhsel; struct fib_nh * nh; \
-for (nhsel=0, nh = (struct fib_nh*)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++)
+#define change_nexthops(fi) {                                          \
+       int nhsel; struct fib_nh *nexthop_nh;                           \
+       for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh);   \
+            nhsel < (fi)->fib_nhs;                                     \
+            nexthop_nh++, nhsel++)
 
 #else /* CONFIG_IP_ROUTE_MULTIPATH */
 
 /* Hope, that gcc will optimize it to get rid of dummy loop */
 
-#define for_nexthops(fi) { int nhsel=0; const struct fib_nh * nh = (fi)->fib_nh; \
-for (nhsel=0; nhsel < 1; nhsel++)
+#define for_nexthops(fi) {                                             \
+       int nhsel; const struct fib_nh *nh = (fi)->fib_nh;              \
+       for (nhsel = 0; nhsel < 1; nhsel++)
 
-#define change_nexthops(fi) { int nhsel=0; struct fib_nh * nh = (struct fib_nh*)((fi)->fib_nh); \
-for (nhsel=0; nhsel < 1; nhsel++)
+#define change_nexthops(fi) {                                          \
+       int nhsel;                                                      \
+       struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh);    \
+       for (nhsel = 0; nhsel < 1; nhsel++)
 
 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
 
@@ -89,72 +95,80 @@ static const struct
        int     error;
        u8      scope;
 } fib_props[RTN_MAX + 1] = {
-       {
+       [RTN_UNSPEC] = {
                .error  = 0,
                .scope  = RT_SCOPE_NOWHERE,
-       },      /* RTN_UNSPEC */
-       {
+       },
+       [RTN_UNICAST] = {
                .error  = 0,
                .scope  = RT_SCOPE_UNIVERSE,
-       },      /* RTN_UNICAST */
-       {
+       },
+       [RTN_LOCAL] = {
                .error  = 0,
                .scope  = RT_SCOPE_HOST,
-       },      /* RTN_LOCAL */
-       {
+       },
+       [RTN_BROADCAST] = {
                .error  = 0,
                .scope  = RT_SCOPE_LINK,
-       },      /* RTN_BROADCAST */
-       {
+       },
+       [RTN_ANYCAST] = {
                .error  = 0,
                .scope  = RT_SCOPE_LINK,
-       },      /* RTN_ANYCAST */
-       {
+       },
+       [RTN_MULTICAST] = {
                .error  = 0,
                .scope  = RT_SCOPE_UNIVERSE,
-       },      /* RTN_MULTICAST */
-       {
+       },
+       [RTN_BLACKHOLE] = {
                .error  = -EINVAL,
                .scope  = RT_SCOPE_UNIVERSE,
-       },      /* RTN_BLACKHOLE */
-       {
+       },
+       [RTN_UNREACHABLE] = {
                .error  = -EHOSTUNREACH,
                .scope  = RT_SCOPE_UNIVERSE,
-       },      /* RTN_UNREACHABLE */
-       {
+       },
+       [RTN_PROHIBIT] = {
                .error  = -EACCES,
                .scope  = RT_SCOPE_UNIVERSE,
-       },      /* RTN_PROHIBIT */
-       {
+       },
+       [RTN_THROW] = {
                .error  = -EAGAIN,
                .scope  = RT_SCOPE_UNIVERSE,
-       },      /* RTN_THROW */
-       {
+       },
+       [RTN_NAT] = {
                .error  = -EINVAL,
                .scope  = RT_SCOPE_NOWHERE,
-       },      /* RTN_NAT */
-       {
+       },
+       [RTN_XRESOLVE] = {
                .error  = -EINVAL,
                .scope  = RT_SCOPE_NOWHERE,
-       },      /* RTN_XRESOLVE */
+       },
 };
 
 
 /* Release a nexthop info record */
 
+static void free_fib_info_rcu(struct rcu_head *head)
+{
+       struct fib_info *fi = container_of(head, struct fib_info, rcu);
+
+       kfree(fi);
+}
+
 void free_fib_info(struct fib_info *fi)
 {
        if (fi->fib_dead == 0) {
-               printk("Freeing alive fib_info %p\n", fi);
+               pr_warning("Freeing alive fib_info %p\n", fi);
                return;
        }
        change_nexthops(fi) {
-               if (nh->nh_dev)
-                       dev_put(nh->nh_dev);
-               nh->nh_dev = NULL;
+               if (nexthop_nh->nh_dev)
+                       dev_put(nexthop_nh->nh_dev);
+               nexthop_nh->nh_dev = NULL;
        } endfor_nexthops(fi);
        fib_info_cnt--;
-       kfree(fi);
+       release_net(fi->fib_net);
+       call_rcu(&fi->rcu, free_fib_info_rcu);
 }
 
 void fib_release_info(struct fib_info *fi)
@@ -165,9 +179,9 @@ void fib_release_info(struct fib_info *fi)
                if (fi->fib_prefsrc)
                        hlist_del(&fi->fib_lhash);
                change_nexthops(fi) {
-                       if (!nh->nh_dev)
+                       if (!nexthop_nh->nh_dev)
                                continue;
-                       hlist_del(&nh->nh_hash);
+                       hlist_del(&nexthop_nh->nh_hash);
                } endfor_nexthops(fi)
                fi->fib_dead = 1;
                fib_info_put(fi);
@@ -175,7 +189,7 @@ void fib_release_info(struct fib_info *fi)
        spin_unlock_bh(&fib_info_lock);
 }
 
-static __inline__ int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
+static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
 {
        const struct fib_nh *onh = ofi->fib_nh;
 
@@ -189,13 +203,22 @@ static __inline__ int nh_comp(const struct fib_info *fi, const struct fib_info *
 #ifdef CONFIG_NET_CLS_ROUTE
                    nh->nh_tclassid != onh->nh_tclassid ||
 #endif
-                   ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
+                   ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
                        return -1;
                onh++;
        } endfor_nexthops(fi);
        return 0;
 }
 
+static inline unsigned int fib_devindex_hashfn(unsigned int val)
+{
+       unsigned int mask = DEVINDEX_HASHSIZE - 1;
+
+       return (val ^
+               (val >> DEVINDEX_HASHBITS) ^
+               (val >> (DEVINDEX_HASHBITS * 2))) & mask;
+}
+
 static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
 {
        unsigned int mask = (fib_hash_size - 1);
@@ -204,6 +227,9 @@ static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
        val ^= fi->fib_protocol;
        val ^= (__force u32)fi->fib_prefsrc;
        val ^= fi->fib_priority;
+       for_nexthops(fi) {
+               val ^= fib_devindex_hashfn(nh->nh_oif);
+       } endfor_nexthops(fi)
 
        return (val ^ (val >> 7) ^ (val >> 12)) & mask;
 }
@@ -219,6 +245,8 @@ static struct fib_info *fib_find_info(const struct fib_info *nfi)
        head = &fib_info_hash[hash];
 
        hlist_for_each_entry(fi, node, head, fib_hash) {
+               if (!net_eq(fi->fib_net, nfi->fib_net))
+                       continue;
                if (fi->fib_nhs != nfi->fib_nhs)
                        continue;
                if (nfi->fib_protocol == fi->fib_protocol &&
@@ -226,7 +254,7 @@ static struct fib_info *fib_find_info(const struct fib_info *nfi)
                    nfi->fib_priority == fi->fib_priority &&
                    memcmp(nfi->fib_metrics, fi->fib_metrics,
                           sizeof(fi->fib_metrics)) == 0 &&
-                   ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
+                   ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
                    (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
                        return fi;
        }
@@ -234,19 +262,9 @@ static struct fib_info *fib_find_info(const struct fib_info *nfi)
        return NULL;
 }
 
-static inline unsigned int fib_devindex_hashfn(unsigned int val)
-{
-       unsigned int mask = DEVINDEX_HASHSIZE - 1;
-
-       return (val ^
-               (val >> DEVINDEX_HASHBITS) ^
-               (val >> (DEVINDEX_HASHBITS * 2))) & mask;
-}
-
 /* Check, that the gateway is already configured.
-   Used only by redirect accept routine.
+ * Used only by redirect accept routine.
  */
-
 int ip_fib_check_default(__be32 gw, struct net_device *dev)
 {
        struct hlist_head *head;
@@ -261,7 +279,7 @@ int ip_fib_check_default(__be32 gw, struct net_device *dev)
        hlist_for_each_entry(nh, node, head, nh_hash) {
                if (nh->nh_dev == dev &&
                    nh->nh_gw == gw &&
-                   !(nh->nh_flags&RTNH_F_DEAD)) {
+                   !(nh->nh_flags & RTNH_F_DEAD)) {
                        spin_unlock(&fib_info_lock);
                        return 0;
                }
@@ -320,11 +338,12 @@ void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
                kfree_skb(skb);
                goto errout;
        }
-       err = rtnl_notify(skb, &init_net, info->pid, RTNLGRP_IPV4_ROUTE,
-                         info->nlh, GFP_KERNEL);
+       rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
+                   info->nlh, GFP_KERNEL);
+       return;
 errout:
        if (err < 0)
-               rtnl_set_sk_err(&init_net, RTNLGRP_IPV4_ROUTE, err);
+               rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
 }
 
 /* Return the first fib alias matching TOS with
@@ -356,12 +375,12 @@ int fib_detect_death(struct fib_info *fi, int order,
                state = n->nud_state;
                neigh_release(n);
        }
-       if (state==NUD_REACHABLE)
+       if (state == NUD_REACHABLE)
                return 0;
-       if ((state&NUD_VALID) && order != dflt)
+       if ((state & NUD_VALID) && order != dflt)
                return 0;
-       if ((state&NUD_VALID) ||
-           (*last_idx<0 && order > dflt)) {
+       if ((state & NUD_VALID) ||
+           (*last_idx < 0 && order > dflt)) {
                *last_resort = fi;
                *last_idx = order;
        }
@@ -392,19 +411,20 @@ static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
                if (!rtnh_ok(rtnh, remaining))
                        return -EINVAL;
 
-               nh->nh_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
-               nh->nh_oif = rtnh->rtnh_ifindex;
-               nh->nh_weight = rtnh->rtnh_hops + 1;
+               nexthop_nh->nh_flags =
+                       (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
+               nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
+               nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
 
                attrlen = rtnh_attrlen(rtnh);
                if (attrlen > 0) {
                        struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
 
                        nla = nla_find(attrs, attrlen, RTA_GATEWAY);
-                       nh->nh_gw = nla ? nla_get_be32(nla) : 0;
+                       nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
 #ifdef CONFIG_NET_CLS_ROUTE
                        nla = nla_find(attrs, attrlen, RTA_FLOW);
-                       nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
+                       nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
 #endif
                }
 
@@ -471,135 +491,133 @@ int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
 
 
 /*
-   Picture
-   -------
-
-   Semantics of nexthop is very messy by historical reasons.
-   We have to take into account, that:
-   a) gateway can be actually local interface address,
-      so that gatewayed route is direct.
-   b) gateway must be on-link address, possibly
-      described not by an ifaddr, but also by a direct route.
-   c) If both gateway and interface are specified, they should not
-      contradict.
-   d) If we use tunnel routes, gateway could be not on-link.
-
-   Attempt to reconcile all of these (alas, self-contradictory) conditions
-   results in pretty ugly and hairy code with obscure logic.
-
-   I chose to generalized it instead, so that the size
-   of code does not increase practically, but it becomes
-   much more general.
-   Every prefix is assigned a "scope" value: "host" is local address,
-   "link" is direct route,
-   [ ... "site" ... "interior" ... ]
-   and "universe" is true gateway route with global meaning.
-
-   Every prefix refers to a set of "nexthop"s (gw, oif),
-   where gw must have narrower scope. This recursion stops
-   when gw has LOCAL scope or if "nexthop" is declared ONLINK,
-   which means that gw is forced to be on link.
-
-   Code is still hairy, but now it is apparently logically
-   consistent and very flexible. F.e. as by-product it allows
-   to co-exists in peace independent exterior and interior
-   routing processes.
-
-   Normally it looks as following.
-
-   {universe prefix}  -> (gw, oif) [scope link]
-                         |
-                         |-> {link prefix} -> (gw, oif) [scope local]
-                                               |
-                                               |-> {local prefix} (terminal node)
+ * Picture
+ * -------
+ *
+ * Semantics of nexthop is very messy by historical reasons.
+ * We have to take into account, that:
+ * a) gateway can be actually local interface address,
+ *    so that gatewayed route is direct.
+ * b) gateway must be on-link address, possibly
+ *    described not by an ifaddr, but also by a direct route.
+ * c) If both gateway and interface are specified, they should not
+ *    contradict.
+ * d) If we use tunnel routes, gateway could be not on-link.
+ *
+ * Attempt to reconcile all of these (alas, self-contradictory) conditions
+ * results in pretty ugly and hairy code with obscure logic.
+ *
+ * I chose to generalized it instead, so that the size
+ * of code does not increase practically, but it becomes
+ * much more general.
+ * Every prefix is assigned a "scope" value: "host" is local address,
+ * "link" is direct route,
+ * [ ... "site" ... "interior" ... ]
+ * and "universe" is true gateway route with global meaning.
+ *
+ * Every prefix refers to a set of "nexthop"s (gw, oif),
+ * where gw must have narrower scope. This recursion stops
+ * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
+ * which means that gw is forced to be on link.
+ *
+ * Code is still hairy, but now it is apparently logically
+ * consistent and very flexible. F.e. as by-product it allows
+ * to co-exists in peace independent exterior and interior
+ * routing processes.
+ *
+ * Normally it looks as following.
+ *
+ * {universe prefix}  -> (gw, oif) [scope link]
+ *               |
+ *               |-> {link prefix} -> (gw, oif) [scope local]
+ *                                     |
+ *                                     |-> {local prefix} (terminal node)
  */
-
 static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
                        struct fib_nh *nh)
 {
        int err;
+       struct net *net;
+       struct net_device *dev;
 
+       net = cfg->fc_nlinfo.nl_net;
        if (nh->nh_gw) {
                struct fib_result res;
 
-#ifdef CONFIG_IP_ROUTE_PERVASIVE
-               if (nh->nh_flags&RTNH_F_PERVASIVE)
-                       return 0;
-#endif
-               if (nh->nh_flags&RTNH_F_ONLINK) {
-                       struct net_device *dev;
+               if (nh->nh_flags & RTNH_F_ONLINK) {
 
                        if (cfg->fc_scope >= RT_SCOPE_LINK)
                                return -EINVAL;
-                       if (inet_addr_type(&init_net, nh->nh_gw) != RTN_UNICAST)
+                       if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
                                return -EINVAL;
-                       if ((dev = __dev_get_by_index(&init_net, nh->nh_oif)) == NULL)
+                       dev = __dev_get_by_index(net, nh->nh_oif);
+                       if (!dev)
                                return -ENODEV;
-                       if (!(dev->flags&IFF_UP))
+                       if (!(dev->flags & IFF_UP))
                                return -ENETDOWN;
                        nh->nh_dev = dev;
                        dev_hold(dev);
                        nh->nh_scope = RT_SCOPE_LINK;
                        return 0;
                }
+               rcu_read_lock();
                {
                        struct flowi fl = {
-                               .nl_u = {
-                                       .ip4_u = {
-                                               .daddr = nh->nh_gw,
-                                               .scope = cfg->fc_scope + 1,
-                                       },
-                               },
+                               .fl4_dst = nh->nh_gw,
+                               .fl4_scope = cfg->fc_scope + 1,
                                .oif = nh->nh_oif,
                        };
 
                        /* It is not necessary, but requires a bit of thinking */
                        if (fl.fl4_scope < RT_SCOPE_LINK)
                                fl.fl4_scope = RT_SCOPE_LINK;
-                       if ((err = fib_lookup(&fl, &res)) != 0)
+                       err = fib_lookup(net, &fl, &res);
+                       if (err) {
+                               rcu_read_unlock();
                                return err;
+                       }
                }
                err = -EINVAL;
                if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
                        goto out;
                nh->nh_scope = res.scope;
                nh->nh_oif = FIB_RES_OIF(res);
-               if ((nh->nh_dev = FIB_RES_DEV(res)) == NULL)
-                       goto out;
-               dev_hold(nh->nh_dev);
-               err = -ENETDOWN;
-               if (!(nh->nh_dev->flags & IFF_UP))
+               nh->nh_dev = dev = FIB_RES_DEV(res);
+               if (!dev)
                        goto out;
-               err = 0;
-out:
-               fib_res_put(&res);
-               return err;
+               dev_hold(dev);
+               err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
        } else {
                struct in_device *in_dev;
 
-               if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK))
+               if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
                        return -EINVAL;
 
-               in_dev = inetdev_by_index(nh->nh_oif);
+               rcu_read_lock();
+               err = -ENODEV;
+               in_dev = inetdev_by_index(net, nh->nh_oif);
                if (in_dev == NULL)
-                       return -ENODEV;
-               if (!(in_dev->dev->flags&IFF_UP)) {
-                       in_dev_put(in_dev);
-                       return -ENETDOWN;
-               }
+                       goto out;
+               err = -ENETDOWN;
+               if (!(in_dev->dev->flags & IFF_UP))
+                       goto out;
                nh->nh_dev = in_dev->dev;
                dev_hold(nh->nh_dev);
                nh->nh_scope = RT_SCOPE_HOST;
-               in_dev_put(in_dev);
+               err = 0;
        }
-       return 0;
+out:
+       rcu_read_unlock();
+       return err;
 }
 
 static inline unsigned int fib_laddr_hashfn(__be32 val)
 {
        unsigned int mask = (fib_hash_size - 1);
 
-       return ((__force u32)val ^ ((__force u32)val >> 7) ^ ((__force u32)val >> 14)) & mask;
+       return ((__force u32)val ^
+               ((__force u32)val >> 7) ^
+               ((__force u32)val >> 14)) & mask;
 }
 
 static struct hlist_head *fib_hash_alloc(int bytes)
@@ -608,7 +626,8 @@ static struct hlist_head *fib_hash_alloc(int bytes)
                return kzalloc(bytes, GFP_KERNEL);
        else
                return (struct hlist_head *)
-                       __get_free_pages(GFP_KERNEL | __GFP_ZERO, get_order(bytes));
+                       __get_free_pages(GFP_KERNEL | __GFP_ZERO,
+                                        get_order(bytes));
 }
 
 static void fib_hash_free(struct hlist_head *hash, int bytes)
@@ -684,6 +703,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
        struct fib_info *fi = NULL;
        struct fib_info *ofi;
        int nhs = 1;
+       struct net *net = cfg->fc_nlinfo.nl_net;
 
        /* Fast check to catch the most weird cases */
        if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
@@ -724,6 +744,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
                goto failure;
        fib_info_cnt++;
 
+       fi->fib_net = hold_net(net);
        fi->fib_protocol = cfg->fc_protocol;
        fi->fib_flags = cfg->fc_flags;
        fi->fib_priority = cfg->fc_priority;
@@ -731,7 +752,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
 
        fi->fib_nhs = nhs;
        change_nexthops(fi) {
-               nh->nh_parent = fi;
+               nexthop_nh->nh_parent = fi;
        } endfor_nexthops(fi)
 
        if (cfg->fc_mx) {
@@ -795,13 +816,14 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
                if (nhs != 1 || nh->nh_gw)
                        goto err_inval;
                nh->nh_scope = RT_SCOPE_NOWHERE;
-               nh->nh_dev = dev_get_by_index(&init_net, fi->fib_nh->nh_oif);
+               nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
                err = -ENODEV;
                if (nh->nh_dev == NULL)
                        goto failure;
        } else {
                change_nexthops(fi) {
-                       if ((err = fib_check_nh(cfg, fi, nh)) != 0)
+                       err = fib_check_nh(cfg, fi, nexthop_nh);
+                       if (err != 0)
                                goto failure;
                } endfor_nexthops(fi)
        }
@@ -809,12 +831,13 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
        if (fi->fib_prefsrc) {
                if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
                    fi->fib_prefsrc != cfg->fc_dst)
-                       if (inet_addr_type(&init_net, fi->fib_prefsrc) != RTN_LOCAL)
+                       if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
                                goto err_inval;
        }
 
 link_it:
-       if ((ofi = fib_find_info(fi)) != NULL) {
+       ofi = fib_find_info(fi);
+       if (ofi) {
                fi->fib_dead = 1;
                free_fib_info(fi);
                ofi->fib_treeref++;
@@ -836,11 +859,11 @@ link_it:
                struct hlist_head *head;
                unsigned int hash;
 
-               if (!nh->nh_dev)
+               if (!nexthop_nh->nh_dev)
                        continue;
-               hash = fib_devindex_hashfn(nh->nh_dev->ifindex);
+               hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
                head = &fib_info_devhash[hash];
-               hlist_add_head(&nh->nh_hash, head);
+               hlist_add_head(&nexthop_nh->nh_hash, head);
        } endfor_nexthops(fi)
        spin_unlock_bh(&fib_info_lock);
        return fi;
@@ -859,8 +882,7 @@ failure:
 
 /* Note! fib_semantic_match intentionally uses  RCU list functions. */
 int fib_semantic_match(struct list_head *head, const struct flowi *flp,
-                      struct fib_result *res, __be32 zone, __be32 mask,
-                       int prefixlen)
+                      struct fib_result *res, int prefixlen, int fib_flags)
 {
        struct fib_alias *fa;
        int nh_sel = 0;
@@ -875,7 +897,7 @@ int fib_semantic_match(struct list_head *head, const struct flowi *flp,
                if (fa->fa_scope < flp->fl4_scope)
                        continue;
 
-               fa->fa_state |= FA_S_ACCESSED;
+               fib_alias_accessed(fa);
 
                err = fib_props[fa->fa_type].error;
                if (err == 0) {
@@ -891,7 +913,7 @@ int fib_semantic_match(struct list_head *head, const struct flowi *flp,
                        case RTN_ANYCAST:
                        case RTN_MULTICAST:
                                for_nexthops(fi) {
-                                       if (nh->nh_flags&RTNH_F_DEAD)
+                                       if (nh->nh_flags & RTNH_F_DEAD)
                                                continue;
                                        if (!flp->oif || flp->oif == nh->nh_oif)
                                                break;
@@ -902,15 +924,15 @@ int fib_semantic_match(struct list_head *head, const struct flowi *flp,
                                        goto out_fill_res;
                                }
 #else
-                               if (nhsel < 1) {
+                               if (nhsel < 1)
                                        goto out_fill_res;
-                               }
 #endif
                                endfor_nexthops(fi);
                                continue;
 
                        default:
-                               printk(KERN_DEBUG "impossible 102\n");
+                               pr_warning("fib_semantic_match bad type %#x\n",
+                                          fa->fa_type);
                                return -EINVAL;
                        }
                }
@@ -924,7 +946,8 @@ out_fill_res:
        res->type = fa->fa_type;
        res->scope = fa->fa_scope;
        res->fi = fa->fa_info;
-       atomic_inc(&res->fi->fib_clntref);
+       if (!(fib_flags & FIB_LOOKUP_NOREF))
+               atomic_inc(&res->fi->fib_clntref);
        return 0;
 }
 
@@ -951,7 +974,10 @@ int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
        rtm->rtm_dst_len = dst_len;
        rtm->rtm_src_len = 0;
        rtm->rtm_tos = tos;
-       rtm->rtm_table = tb_id;
+       if (tb_id < 256)
+               rtm->rtm_table = tb_id;
+       else
+               rtm->rtm_table = RT_TABLE_COMPAT;
        NLA_PUT_U32(skb, RTA_TABLE, tb_id);
        rtm->rtm_type = type;
        rtm->rtm_flags = fi->fib_flags;
@@ -1020,75 +1046,79 @@ nla_put_failure:
 }
 
 /*
-   Update FIB if:
-   - local address disappeared -> we must delete all the entries
-     referring to it.
-   - device went down -> we must shutdown all nexthops going via it.
+ * Update FIB if:
+ * - local address disappeared -> we must delete all the entries
+ *   referring to it.
+ * - device went down -> we must shutdown all nexthops going via it.
  */
-
-int fib_sync_down(__be32 local, struct net_device *dev, int force)
+int fib_sync_down_addr(struct net *net, __be32 local)
 {
        int ret = 0;
-       int scope = RT_SCOPE_NOWHERE;
-
-       if (force)
-               scope = -1;
+       unsigned int hash = fib_laddr_hashfn(local);
+       struct hlist_head *head = &fib_info_laddrhash[hash];
+       struct hlist_node *node;
+       struct fib_info *fi;
 
-       if (local && fib_info_laddrhash) {
-               unsigned int hash = fib_laddr_hashfn(local);
-               struct hlist_head *head = &fib_info_laddrhash[hash];
-               struct hlist_node *node;
-               struct fib_info *fi;
+       if (fib_info_laddrhash == NULL || local == 0)
+               return 0;
 
-               hlist_for_each_entry(fi, node, head, fib_lhash) {
-                       if (fi->fib_prefsrc == local) {
-                               fi->fib_flags |= RTNH_F_DEAD;
-                               ret++;
-                       }
+       hlist_for_each_entry(fi, node, head, fib_lhash) {
+               if (!net_eq(fi->fib_net, net))
+                       continue;
+               if (fi->fib_prefsrc == local) {
+                       fi->fib_flags |= RTNH_F_DEAD;
+                       ret++;
                }
        }
+       return ret;
+}
+
+int fib_sync_down_dev(struct net_device *dev, int force)
+{
+       int ret = 0;
+       int scope = RT_SCOPE_NOWHERE;
+       struct fib_info *prev_fi = NULL;
+       unsigned int hash = fib_devindex_hashfn(dev->ifindex);
+       struct hlist_head *head = &fib_info_devhash[hash];
+       struct hlist_node *node;
+       struct fib_nh *nh;
 
-       if (dev) {
-               struct fib_info *prev_fi = NULL;
-               unsigned int hash = fib_devindex_hashfn(dev->ifindex);
-               struct hlist_head *head = &fib_info_devhash[hash];
-               struct hlist_node *node;
-               struct fib_nh *nh;
+       if (force)
+               scope = -1;
 
-               hlist_for_each_entry(nh, node, head, nh_hash) {
-                       struct fib_info *fi = nh->nh_parent;
-                       int dead;
+       hlist_for_each_entry(nh, node, head, nh_hash) {
+               struct fib_info *fi = nh->nh_parent;
+               int dead;
 
-                       BUG_ON(!fi->fib_nhs);
-                       if (nh->nh_dev != dev || fi == prev_fi)
-                               continue;
-                       prev_fi = fi;
-                       dead = 0;
-                       change_nexthops(fi) {
-                               if (nh->nh_flags&RTNH_F_DEAD)
-                                       dead++;
-                               else if (nh->nh_dev == dev &&
-                                        nh->nh_scope != scope) {
-                                       nh->nh_flags |= RTNH_F_DEAD;
+               BUG_ON(!fi->fib_nhs);
+               if (nh->nh_dev != dev || fi == prev_fi)
+                       continue;
+               prev_fi = fi;
+               dead = 0;
+               change_nexthops(fi) {
+                       if (nexthop_nh->nh_flags & RTNH_F_DEAD)
+                               dead++;
+                       else if (nexthop_nh->nh_dev == dev &&
+                                nexthop_nh->nh_scope != scope) {
+                               nexthop_nh->nh_flags |= RTNH_F_DEAD;
 #ifdef CONFIG_IP_ROUTE_MULTIPATH
-                                       spin_lock_bh(&fib_multipath_lock);
-                                       fi->fib_power -= nh->nh_power;
-                                       nh->nh_power = 0;
-                                       spin_unlock_bh(&fib_multipath_lock);
+                               spin_lock_bh(&fib_multipath_lock);
+                               fi->fib_power -= nexthop_nh->nh_power;
+                               nexthop_nh->nh_power = 0;
+                               spin_unlock_bh(&fib_multipath_lock);
 #endif
-                                       dead++;
-                               }
+                               dead++;
+                       }
 #ifdef CONFIG_IP_ROUTE_MULTIPATH
-                               if (force > 1 && nh->nh_dev == dev) {
-                                       dead = fi->fib_nhs;
-                                       break;
-                               }
-#endif
-                       } endfor_nexthops(fi)
-                       if (dead == fi->fib_nhs) {
-                               fi->fib_flags |= RTNH_F_DEAD;
-                               ret++;
+                       if (force > 1 && nexthop_nh->nh_dev == dev) {
+                               dead = fi->fib_nhs;
+                               break;
                        }
+#endif
+               } endfor_nexthops(fi)
+               if (dead == fi->fib_nhs) {
+                       fi->fib_flags |= RTNH_F_DEAD;
+                       ret++;
                }
        }
 
@@ -1098,10 +1128,9 @@ int fib_sync_down(__be32 local, struct net_device *dev, int force)
 #ifdef CONFIG_IP_ROUTE_MULTIPATH
 
 /*
-   Dead device goes up. We wake up dead nexthops.
-   It takes sense only on multipath routes.
+ * Dead device goes up. We wake up dead nexthops.
+ * It takes sense only on multipath routes.
  */
-
 int fib_sync_up(struct net_device *dev)
 {
        struct fib_info *prev_fi;
@@ -1111,7 +1140,7 @@ int fib_sync_up(struct net_device *dev)
        struct fib_nh *nh;
        int ret;
 
-       if (!(dev->flags&IFF_UP))
+       if (!(dev->flags & IFF_UP))
                return 0;
 
        prev_fi = NULL;
@@ -1130,18 +1159,20 @@ int fib_sync_up(struct net_device *dev)
                prev_fi = fi;
                alive = 0;
                change_nexthops(fi) {
-                       if (!(nh->nh_flags&RTNH_F_DEAD)) {
+                       if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
                                alive++;
                                continue;
                        }
-                       if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
+                       if (nexthop_nh->nh_dev == NULL ||
+                           !(nexthop_nh->nh_dev->flags & IFF_UP))
                                continue;
-                       if (nh->nh_dev != dev || !__in_dev_get_rtnl(dev))
+                       if (nexthop_nh->nh_dev != dev ||
+                           !__in_dev_get_rtnl(dev))
                                continue;
                        alive++;
                        spin_lock_bh(&fib_multipath_lock);
-                       nh->nh_power = 0;
-                       nh->nh_flags &= ~RTNH_F_DEAD;
+                       nexthop_nh->nh_power = 0;
+                       nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
                        spin_unlock_bh(&fib_multipath_lock);
                } endfor_nexthops(fi)
 
@@ -1155,10 +1186,9 @@ int fib_sync_up(struct net_device *dev)
 }
 
 /*
-   The algorithm is suboptimal, but it provides really
-   fair weighted route distribution.
+ * The algorithm is suboptimal, but it provides really
+ * fair weighted route distribution.
  */
-
 void fib_select_multipath(const struct flowi *flp, struct fib_result *res)
 {
        struct fib_info *fi = res->fi;
@@ -1168,9 +1198,9 @@ void fib_select_multipath(const struct flowi *flp, struct fib_result *res)
        if (fi->fib_power <= 0) {
                int power = 0;
                change_nexthops(fi) {
-                       if (!(nh->nh_flags&RTNH_F_DEAD)) {
-                               power += nh->nh_weight;
-                               nh->nh_power = nh->nh_weight;
+                       if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
+                               power += nexthop_nh->nh_weight;
+                               nexthop_nh->nh_power = nexthop_nh->nh_weight;
                        }
                } endfor_nexthops(fi);
                fi->fib_power = power;
@@ -1184,15 +1214,17 @@ void fib_select_multipath(const struct flowi *flp, struct fib_result *res)
 
 
        /* w should be random number [0..fi->fib_power-1],
-          it is pretty bad approximation.
+        * it is pretty bad approximation.
         */
 
        w = jiffies % fi->fib_power;
 
        change_nexthops(fi) {
-               if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
-                       if ((w -= nh->nh_power) <= 0) {
-                               nh->nh_power--;
+               if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
+                   nexthop_nh->nh_power) {
+                       w -= nexthop_nh->nh_power;
+                       if (w <= 0) {
+                               nexthop_nh->nh_power--;
                                fi->fib_power--;
                                res->nh_sel = nhsel;
                                spin_unlock_bh(&fib_multipath_lock);