[IPV6]: annotate inet6_csk_search_req()
[safe/jmp/linux-2.6] / net / ipv6 / ip6_fib.c
index 667b1b1..f98ca30 100644 (file)
@@ -18,6 +18,7 @@
  *     Yuji SEKIYA @USAGI:     Support default route on router node;
  *                             remove ip6_null_entry from the top of
  *                             routing table.
+ *     Ville Nuorvala:         Fixed routing subtrees.
  */
 #include <linux/errno.h>
 #include <linux/types.h>
@@ -73,13 +74,12 @@ static DEFINE_RWLOCK(fib6_walker_lock);
 
 #ifdef CONFIG_IPV6_SUBTREES
 #define FWS_INIT FWS_S
-#define SUBTREE(fn) ((fn)->subtree)
 #else
 #define FWS_INIT FWS_L
-#define SUBTREE(fn) NULL
 #endif
 
 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
+static struct rt6_info * fib6_find_prefix(struct fib6_node *fn);
 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
 static int fib6_walk(struct fib6_walker_t *w);
 static int fib6_walk_continue(struct fib6_walker_t *w);
@@ -169,7 +169,6 @@ static __inline__ void rt6_release(struct rt6_info *rt)
 
 static struct fib6_table fib6_main_tbl = {
        .tb6_id         = RT6_TABLE_MAIN,
-       .tb6_lock       = RW_LOCK_UNLOCKED,
        .tb6_root       = {
                .leaf           = &ip6_null_entry,
                .fn_flags       = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
@@ -187,6 +186,12 @@ static void fib6_link_table(struct fib6_table *tb)
 {
        unsigned int h;
 
+       /*
+        * Initialize table lock at a single place to give lockdep a key,
+        * tables aren't visible prior to being linked to the list.
+        */
+       rwlock_init(&tb->tb6_lock);
+
        h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1);
 
        /*
@@ -199,7 +204,6 @@ static void fib6_link_table(struct fib6_table *tb)
 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
 static struct fib6_table fib6_local_tbl = {
        .tb6_id         = RT6_TABLE_LOCAL,
-       .tb6_lock       = RW_LOCK_UNLOCKED,
        .tb6_root       = {
                .leaf           = &ip6_null_entry,
                .fn_flags       = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
@@ -213,7 +217,6 @@ static struct fib6_table *fib6_alloc_table(u32 id)
        table = kzalloc(sizeof(*table), GFP_ATOMIC);
        if (table != NULL) {
                table->tb6_id = id;
-               table->tb6_lock = RW_LOCK_UNLOCKED;
                table->tb6_root.leaf = &ip6_null_entry;
                table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
        }
@@ -697,7 +700,7 @@ void fib6_force_start_gc(void)
 
 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
 {
-       struct fib6_node *fn;
+       struct fib6_node *fn, *pn = NULL;
        int err = -ENOMEM;
 
        fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
@@ -706,6 +709,8 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
        if (fn == NULL)
                goto out;
 
+       pn = fn;
+
 #ifdef CONFIG_IPV6_SUBTREES
        if (rt->rt6i_src.plen) {
                struct fib6_node *sn;
@@ -751,10 +756,6 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
                        /* Now link new subtree to main tree */
                        sfn->parent = fn;
                        fn->subtree = sfn;
-                       if (fn->leaf == NULL) {
-                               fn->leaf = rt;
-                               atomic_inc(&rt->rt6i_ref);
-                       }
                } else {
                        sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
                                        sizeof(struct in6_addr), rt->rt6i_src.plen,
@@ -764,6 +765,10 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
                                goto st_failure;
                }
 
+               if (fn->leaf == NULL) {
+                       fn->leaf = rt;
+                       atomic_inc(&rt->rt6i_ref);
+               }
                fn = sn;
        }
 #endif
@@ -773,12 +778,29 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
        if (err == 0) {
                fib6_start_gc(rt);
                if (!(rt->rt6i_flags&RTF_CACHE))
-                       fib6_prune_clones(fn, rt);
+                       fib6_prune_clones(pn, rt);
        }
 
 out:
-       if (err)
+       if (err) {
+#ifdef CONFIG_IPV6_SUBTREES
+               /*
+                * If fib6_add_1 has cleared the old leaf pointer in the
+                * super-tree leaf node we have to find a new one for it.
+                */
+               if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
+                       pn->leaf = fib6_find_prefix(pn);
+#if RT6_DEBUG >= 2
+                       if (!pn->leaf) {
+                               BUG_TRAP(pn->leaf != NULL);
+                               pn->leaf = &ip6_null_entry;
+                       }
+#endif
+                       atomic_inc(&pn->leaf->rt6i_ref);
+               }
+#endif
                dst_free(&rt->u.dst);
+       }
        return err;
 
 #ifdef CONFIG_IPV6_SUBTREES
@@ -809,6 +831,9 @@ static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
        struct fib6_node *fn;
        int dir;
 
+       if (unlikely(args->offset == 0))
+               return NULL;
+
        /*
         *      Descend on a tree
         */
@@ -830,33 +855,26 @@ static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
                break;
        }
 
-       while ((fn->fn_flags & RTN_ROOT) == 0) {
-#ifdef CONFIG_IPV6_SUBTREES
-               if (fn->subtree) {
-                       struct fib6_node *st;
-                       struct lookup_args *narg;
-
-                       narg = args + 1;
-
-                       if (narg->addr) {
-                               st = fib6_lookup_1(fn->subtree, narg);
-
-                               if (st && !(st->fn_flags & RTN_ROOT))
-                                       return st;
-                       }
-               }
-#endif
-
-               if (fn->fn_flags & RTN_RTINFO) {
+       while(fn) {
+               if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
                        struct rt6key *key;
 
                        key = (struct rt6key *) ((u8 *) fn->leaf +
                                                 args->offset);
 
-                       if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
-                               return fn;
+                       if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
+#ifdef CONFIG_IPV6_SUBTREES
+                               if (fn->subtree)
+                                       fn = fib6_lookup_1(fn->subtree, args + 1);
+#endif
+                               if (!fn || fn->fn_flags & RTN_RTINFO)
+                                       return fn;
+                       }
                }
 
+               if (fn->fn_flags & RTN_ROOT)
+                       break;
+
                fn = fn->parent;
        }
 
@@ -866,18 +884,24 @@ static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
                               struct in6_addr *saddr)
 {
-       struct lookup_args args[2];
        struct fib6_node *fn;
-
-       args[0].offset = offsetof(struct rt6_info, rt6i_dst);
-       args[0].addr = daddr;
-
+       struct lookup_args args[] = {
+               {
+                       .offset = offsetof(struct rt6_info, rt6i_dst),
+                       .addr = daddr,
+               },
 #ifdef CONFIG_IPV6_SUBTREES
-       args[1].offset = offsetof(struct rt6_info, rt6i_src);
-       args[1].addr = saddr;
+               {
+                       .offset = offsetof(struct rt6_info, rt6i_src),
+                       .addr = saddr,
+               },
 #endif
+               {
+                       .offset = 0,    /* sentinel */
+               }
+       };
 
-       fn = fib6_lookup_1(root, args);
+       fn = fib6_lookup_1(root, daddr ? args : args + 1);
 
        if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
                fn = root;
@@ -933,10 +957,8 @@ struct fib6_node * fib6_locate(struct fib6_node *root,
 #ifdef CONFIG_IPV6_SUBTREES
        if (src_len) {
                BUG_TRAP(saddr!=NULL);
-               if (fn == NULL)
-                       fn = fn->subtree;
-               if (fn)
-                       fn = fib6_locate_1(fn, saddr, src_len,
+               if (fn && fn->subtree)
+                       fn = fib6_locate_1(fn->subtree, saddr, src_len,
                                           offsetof(struct rt6_info, rt6i_src));
        }
 #endif
@@ -965,7 +987,7 @@ static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
                if(fn->right)
                        return fn->right->leaf;
 
-               fn = SUBTREE(fn);
+               fn = FIB6_SUBTREE(fn);
        }
        return NULL;
 }
@@ -996,7 +1018,7 @@ static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
                if (fn->right) child = fn->right, children |= 1;
                if (fn->left) child = fn->left, children |= 2;
 
-               if (children == 3 || SUBTREE(fn) 
+               if (children == 3 || FIB6_SUBTREE(fn)
 #ifdef CONFIG_IPV6_SUBTREES
                    /* Subtree root (i.e. fn) may have one child */
                    || (children && fn->fn_flags&RTN_ROOT)
@@ -1015,9 +1037,9 @@ static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
 
                pn = fn->parent;
 #ifdef CONFIG_IPV6_SUBTREES
-               if (SUBTREE(pn) == fn) {
+               if (FIB6_SUBTREE(pn) == fn) {
                        BUG_TRAP(fn->fn_flags&RTN_ROOT);
-                       SUBTREE(pn) = NULL;
+                       FIB6_SUBTREE(pn) = NULL;
                        nstate = FWS_L;
                } else {
                        BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
@@ -1065,7 +1087,7 @@ static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
                read_unlock(&fib6_walker_lock);
 
                node_free(fn);
-               if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
+               if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn))
                        return pn;
 
                rt6_release(pn->leaf);
@@ -1151,8 +1173,18 @@ int fib6_del(struct rt6_info *rt, struct nl_info *info)
 
        BUG_TRAP(fn->fn_flags&RTN_RTINFO);
 
-       if (!(rt->rt6i_flags&RTF_CACHE))
-               fib6_prune_clones(fn, rt);
+       if (!(rt->rt6i_flags&RTF_CACHE)) {
+               struct fib6_node *pn = fn;
+#ifdef CONFIG_IPV6_SUBTREES
+               /* clones of this route might be in another subtree */
+               if (rt->rt6i_src.plen) {
+                       while (!(pn->fn_flags&RTN_ROOT))
+                               pn = pn->parent;
+                       pn = pn->parent;
+               }
+#endif
+               fib6_prune_clones(pn, rt);
+       }
 
        /*
         *      Walk the leaf entries looking for ourself
@@ -1208,8 +1240,8 @@ static int fib6_walk_continue(struct fib6_walker_t *w)
                switch (w->state) {
 #ifdef CONFIG_IPV6_SUBTREES
                case FWS_S:
-                       if (SUBTREE(fn)) {
-                               w->node = SUBTREE(fn);
+                       if (FIB6_SUBTREE(fn)) {
+                               w->node = FIB6_SUBTREE(fn);
                                continue;
                        }
                        w->state = FWS_L;
@@ -1243,7 +1275,7 @@ static int fib6_walk_continue(struct fib6_walker_t *w)
                        pn = fn->parent;
                        w->node = pn;
 #ifdef CONFIG_IPV6_SUBTREES
-                       if (SUBTREE(pn) == fn) {
+                       if (FIB6_SUBTREE(pn) == fn) {
                                BUG_TRAP(fn->fn_flags&RTN_ROOT);
                                w->state = FWS_L;
                                continue;
@@ -1443,10 +1475,8 @@ void __init fib6_init(void)
 {
        fib6_node_kmem = kmem_cache_create("fib6_nodes",
                                           sizeof(struct fib6_node),
-                                          0, SLAB_HWCACHE_ALIGN,
+                                          0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
                                           NULL, NULL);
-       if (!fib6_node_kmem)
-               panic("cannot create fib6_nodes cache");
 
        fib6_tables_init();
 }