]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blobdiff - net/ipv6/ip6_fib.c
netns: Add network namespace argument to rt6_fill_node() and ipv6_dev_get_saddr()
[lisovros/linux_canprio.git] / net / ipv6 / ip6_fib.c
index 4aa67988f518a734ed88e1ddc079e41643af08cc..fe801716a7fc8718997a1b79eff6ec686e5aa77f 100644 (file)
@@ -48,8 +48,6 @@
 #define RT6_TRACE(x...) do { ; } while (0)
 #endif
 
-struct rt6_statistics  rt6_stats;
-
 static struct kmem_cache * fib6_node_kmem __read_mostly;
 
 enum fib_walk_state_t
@@ -66,6 +64,7 @@ enum fib_walk_state_t
 struct fib6_cleaner_t
 {
        struct fib6_walker_t w;
+       struct net *net;
        int (*func)(struct rt6_info *, void *arg);
        void *arg;
 };
@@ -78,9 +77,10 @@ static DEFINE_RWLOCK(fib6_walker_lock);
 #define FWS_INIT FWS_L
 #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 void fib6_prune_clones(struct net *net, struct fib6_node *fn,
+                             struct rt6_info *rt);
+static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
+static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
 static int fib6_walk(struct fib6_walker_t *w);
 static int fib6_walk_continue(struct fib6_walker_t *w);
 
@@ -193,14 +193,14 @@ static void fib6_link_table(struct net *net, struct fib6_table *tb)
 
 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
 
-static struct fib6_table *fib6_alloc_table(u32 id)
+static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
 {
        struct fib6_table *table;
 
        table = kzalloc(sizeof(*table), GFP_ATOMIC);
        if (table != NULL) {
                table->tb6_id = id;
-               table->tb6_root.leaf = &ip6_null_entry;
+               table->tb6_root.leaf = net->ipv6.ip6_null_entry;
                table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
        }
 
@@ -217,7 +217,7 @@ struct fib6_table *fib6_new_table(struct net *net, u32 id)
        if (tb)
                return tb;
 
-       tb = fib6_alloc_table(id);
+       tb = fib6_alloc_table(net, id);
        if (tb != NULL)
                fib6_link_table(net, tb);
 
@@ -267,7 +267,7 @@ struct fib6_table *fib6_get_table(struct net *net, u32 id)
 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi *fl,
                                   int flags, pol_lookup_t lookup)
 {
-       return (struct dst_entry *) lookup(net->ipv6.fib6_main_tbl, fl, flags);
+       return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl, flags);
 }
 
 static void fib6_tables_init(struct net *net)
@@ -346,7 +346,7 @@ end:
 
 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
 {
-       struct net *net = skb->sk->sk_net;
+       struct net *net = sock_net(skb->sk);
        unsigned int h, s_h;
        unsigned int e = 0, s_e;
        struct rt6_rtnl_dump_arg arg;
@@ -380,6 +380,7 @@ static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
 
        arg.skb = skb;
        arg.cb = cb;
+       arg.net = net;
        w->args = &arg;
 
        for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
@@ -651,10 +652,10 @@ static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
        rt->rt6i_node = fn;
        atomic_inc(&rt->rt6i_ref);
        inet6_rt_notify(RTM_NEWROUTE, rt, info);
-       rt6_stats.fib_rt_entries++;
+       info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
 
        if ((fn->fn_flags & RTN_RTINFO) == 0) {
-               rt6_stats.fib_route_nodes++;
+               info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
                fn->fn_flags |= RTN_RTINFO;
        }
 
@@ -663,7 +664,7 @@ static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
 
 static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
 {
-       if (net->ipv6.ip6_fib_timer->expires == 0 &&
+       if (!timer_pending(net->ipv6.ip6_fib_timer) &&
            (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
                mod_timer(net->ipv6.ip6_fib_timer, jiffies +
                          net->ipv6.sysctl.ip6_rt_gc_interval);
@@ -671,7 +672,7 @@ static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
 
 void fib6_force_start_gc(struct net *net)
 {
-       if (net->ipv6.ip6_fib_timer->expires == 0)
+       if (!timer_pending(net->ipv6.ip6_fib_timer))
                mod_timer(net->ipv6.ip6_fib_timer, jiffies +
                          net->ipv6.sysctl.ip6_rt_gc_interval);
 }
@@ -717,8 +718,8 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
                        if (sfn == NULL)
                                goto st_failure;
 
-                       sfn->leaf = &ip6_null_entry;
-                       atomic_inc(&ip6_null_entry.rt6i_ref);
+                       sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
+                       atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
                        sfn->fn_flags = RTN_ROOT;
                        sfn->fn_sernum = fib6_new_sernum();
 
@@ -762,7 +763,7 @@ int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
        if (err == 0) {
                fib6_start_gc(info->nl_net, rt);
                if (!(rt->rt6i_flags&RTF_CACHE))
-                       fib6_prune_clones(pn, rt);
+                       fib6_prune_clones(info->nl_net, pn, rt);
        }
 
 out:
@@ -772,12 +773,16 @@ out:
                 * 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 == rt) {
+                       pn->leaf = NULL;
+                       atomic_dec(&rt->rt6i_ref);
+               }
                if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
-                       pn->leaf = fib6_find_prefix(pn);
+                       pn->leaf = fib6_find_prefix(info->nl_net, pn);
 #if RT6_DEBUG >= 2
                        if (!pn->leaf) {
                                BUG_TRAP(pn->leaf != NULL);
-                               pn->leaf = &ip6_null_entry;
+                               pn->leaf = info->nl_net->ipv6.ip6_null_entry;
                        }
 #endif
                        atomic_inc(&pn->leaf->rt6i_ref);
@@ -793,7 +798,7 @@ out:
         */
 st_failure:
        if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
-               fib6_repair_tree(fn);
+               fib6_repair_tree(info->nl_net, fn);
        dst_free(&rt->u.dst);
        return err;
 #endif
@@ -959,10 +964,10 @@ struct fib6_node * fib6_locate(struct fib6_node *root,
  *
  */
 
-static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
+static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
 {
        if (fn->fn_flags&RTN_ROOT)
-               return &ip6_null_entry;
+               return net->ipv6.ip6_null_entry;
 
        while(fn) {
                if(fn->left)
@@ -981,7 +986,8 @@ static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
  *     is the node we want to try and remove.
  */
 
-static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
+static struct fib6_node *fib6_repair_tree(struct net *net,
+                                          struct fib6_node *fn)
 {
        int children;
        int nstate;
@@ -1008,11 +1014,11 @@ static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
                    || (children && fn->fn_flags&RTN_ROOT)
 #endif
                    ) {
-                       fn->leaf = fib6_find_prefix(fn);
+                       fn->leaf = fib6_find_prefix(net, fn);
 #if RT6_DEBUG >= 2
                        if (fn->leaf==NULL) {
                                BUG_TRAP(fn->leaf);
-                               fn->leaf = &ip6_null_entry;
+                               fn->leaf = net->ipv6.ip6_null_entry;
                        }
 #endif
                        atomic_inc(&fn->leaf->rt6i_ref);
@@ -1085,14 +1091,15 @@ static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
 {
        struct fib6_walker_t *w;
        struct rt6_info *rt = *rtp;
+       struct net *net = info->nl_net;
 
        RT6_TRACE("fib6_del_route\n");
 
        /* Unlink it */
        *rtp = rt->u.dst.rt6_next;
        rt->rt6i_node = NULL;
-       rt6_stats.fib_rt_entries--;
-       rt6_stats.fib_discarded_routes++;
+       net->ipv6.rt6_stats->fib_rt_entries--;
+       net->ipv6.rt6_stats->fib_discarded_routes++;
 
        /* Reset round-robin state, if necessary */
        if (fn->rr_ptr == rt)
@@ -1115,8 +1122,8 @@ static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
        /* If it was last route, expunge its radix tree node */
        if (fn->leaf == NULL) {
                fn->fn_flags &= ~RTN_RTINFO;
-               rt6_stats.fib_route_nodes--;
-               fn = fib6_repair_tree(fn);
+               net->ipv6.rt6_stats->fib_route_nodes--;
+               fn = fib6_repair_tree(net, fn);
        }
 
        if (atomic_read(&rt->rt6i_ref) != 1) {
@@ -1128,7 +1135,7 @@ static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
                 */
                while (fn) {
                        if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
-                               fn->leaf = fib6_find_prefix(fn);
+                               fn->leaf = fib6_find_prefix(net, fn);
                                atomic_inc(&fn->leaf->rt6i_ref);
                                rt6_release(rt);
                        }
@@ -1144,6 +1151,7 @@ static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
 
 int fib6_del(struct rt6_info *rt, struct nl_info *info)
 {
+       struct net *net = info->nl_net;
        struct fib6_node *fn = rt->rt6i_node;
        struct rt6_info **rtp;
 
@@ -1153,7 +1161,7 @@ int fib6_del(struct rt6_info *rt, struct nl_info *info)
                return -ENOENT;
        }
 #endif
-       if (fn == NULL || rt == &ip6_null_entry)
+       if (fn == NULL || rt == net->ipv6.ip6_null_entry)
                return -ENOENT;
 
        BUG_TRAP(fn->fn_flags&RTN_RTINFO);
@@ -1168,7 +1176,7 @@ int fib6_del(struct rt6_info *rt, struct nl_info *info)
                        pn = pn->parent;
                }
 #endif
-               fib6_prune_clones(pn, rt);
+               fib6_prune_clones(info->nl_net, pn, rt);
        }
 
        /*
@@ -1298,12 +1306,12 @@ static int fib6_walk(struct fib6_walker_t *w)
 
 static int fib6_clean_node(struct fib6_walker_t *w)
 {
-       struct nl_info info = {
-               .nl_net = &init_net,
-       };
        int res;
        struct rt6_info *rt;
        struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
+       struct nl_info info = {
+               .nl_net = c->net,
+       };
 
        for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
                res = c->func(rt, c->arg);
@@ -1335,7 +1343,7 @@ static int fib6_clean_node(struct fib6_walker_t *w)
  *     ignoring pure split nodes) will be scanned.
  */
 
-static void fib6_clean_tree(struct fib6_node *root,
+static void fib6_clean_tree(struct net *net, struct fib6_node *root,
                            int (*func)(struct rt6_info *, void *arg),
                            int prune, void *arg)
 {
@@ -1346,6 +1354,7 @@ static void fib6_clean_tree(struct fib6_node *root,
        c.w.prune = prune;
        c.func = func;
        c.arg = arg;
+       c.net = net;
 
        fib6_walk(&c.w);
 }
@@ -1363,7 +1372,8 @@ void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
                head = &net->ipv6.fib_table_hash[h];
                hlist_for_each_entry_rcu(table, node, head, tb6_hlist) {
                        write_lock_bh(&table->tb6_lock);
-                       fib6_clean_tree(&table->tb6_root, func, prune, arg);
+                       fib6_clean_tree(net, &table->tb6_root,
+                                       func, prune, arg);
                        write_unlock_bh(&table->tb6_lock);
                }
        }
@@ -1380,9 +1390,10 @@ static int fib6_prune_clone(struct rt6_info *rt, void *arg)
        return 0;
 }
 
-static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
+static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
+                             struct rt6_info *rt)
 {
-       fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
+       fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
 }
 
 /*
@@ -1481,11 +1492,15 @@ static int fib6_net_init(struct net *net)
        setup_timer(timer, fib6_gc_timer_cb, (unsigned long)net);
        net->ipv6.ip6_fib_timer = timer;
 
+       net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
+       if (!net->ipv6.rt6_stats)
+               goto out_timer;
+
        net->ipv6.fib_table_hash =
                kzalloc(sizeof(*net->ipv6.fib_table_hash)*FIB_TABLE_HASHSZ,
                        GFP_KERNEL);
        if (!net->ipv6.fib_table_hash)
-               goto out_timer;
+               goto out_rt6_stats;
 
        net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
                                          GFP_KERNEL);
@@ -1493,7 +1508,7 @@ static int fib6_net_init(struct net *net)
                goto out_fib_table_hash;
 
        net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
-       net->ipv6.fib6_main_tbl->tb6_root.leaf = &ip6_null_entry;
+       net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
        net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
                RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
 
@@ -1503,7 +1518,7 @@ static int fib6_net_init(struct net *net)
        if (!net->ipv6.fib6_local_tbl)
                goto out_fib6_main_tbl;
        net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
-       net->ipv6.fib6_local_tbl->tb6_root.leaf = &ip6_null_entry;
+       net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
        net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
                RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
 #endif
@@ -1519,6 +1534,8 @@ out_fib6_main_tbl:
 #endif
 out_fib_table_hash:
        kfree(net->ipv6.fib_table_hash);
+out_rt6_stats:
+       kfree(net->ipv6.rt6_stats);
 out_timer:
        kfree(timer);
        goto out;
@@ -1526,13 +1543,15 @@ out_timer:
 
 static void fib6_net_exit(struct net *net)
 {
-       del_timer(net->ipv6.ip6_fib_timer);
+       rt6_ifdown(net, NULL);
+       del_timer_sync(net->ipv6.ip6_fib_timer);
        kfree(net->ipv6.ip6_fib_timer);
 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
        kfree(net->ipv6.fib6_local_tbl);
 #endif
        kfree(net->ipv6.fib6_main_tbl);
        kfree(net->ipv6.fib_table_hash);
+       kfree(net->ipv6.rt6_stats);
 }
 
 static struct pernet_operations fib6_net_ops = {