blob: 2f9847924fa51c95a342ab7bff4c54ea60552342 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Forwarding Information Database
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14/*
15 * Changes:
16 * Yuji SEKIYA @USAGI: Support default route on router node;
17 * remove ip6_null_entry from the top of
18 * routing table.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070019 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
22#include <linux/types.h>
23#include <linux/net.h>
24#include <linux/route.h>
25#include <linux/netdevice.h>
26#include <linux/in6.h>
27#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070028#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#ifdef CONFIG_PROC_FS
31#include <linux/proc_fs.h>
32#endif
33
34#include <net/ipv6.h>
35#include <net/ndisc.h>
36#include <net/addrconf.h>
37
38#include <net/ip6_fib.h>
39#include <net/ip6_route.h>
40
41#define RT6_DEBUG 2
42
43#if RT6_DEBUG >= 3
44#define RT6_TRACE(x...) printk(KERN_DEBUG x)
45#else
46#define RT6_TRACE(x...) do { ; } while (0)
47#endif
48
Christoph Lametere18b8902006-12-06 20:33:20 -080049static struct kmem_cache * fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51enum fib_walk_state_t
52{
53#ifdef CONFIG_IPV6_SUBTREES
54 FWS_S,
55#endif
56 FWS_L,
57 FWS_R,
58 FWS_C,
59 FWS_U
60};
61
62struct fib6_cleaner_t
63{
64 struct fib6_walker_t w;
Benjamin Theryec7d43c2008-03-03 23:31:57 -080065 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 int (*func)(struct rt6_info *, void *arg);
67 void *arg;
68};
69
Adrian Bunk90d41122006-08-14 23:49:16 -070070static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#ifdef CONFIG_IPV6_SUBTREES
73#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#else
75#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77
Benjamin Theryec7d43c2008-03-03 23:31:57 -080078static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
79 struct rt6_info *rt);
Daniel Lezcano8ed67782008-03-04 13:48:30 -080080static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn);
81static struct fib6_node *fib6_repair_tree(struct net *net, struct fib6_node *fn);
Adrian Bunk90d41122006-08-14 23:49:16 -070082static int fib6_walk(struct fib6_walker_t *w);
83static int fib6_walk_continue(struct fib6_walker_t *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * A routing update causes an increase of the serial number on the
87 * affected subtree. This allows for cached routes to be asynchronously
88 * tested when modifications are made to the destination cache as a
89 * result of redirects, path MTU changes, etc.
90 */
91
92static __u32 rt_sernum;
93
Daniel Lezcano5b7c9312008-03-03 23:28:58 -080094static void fib6_gc_timer_cb(unsigned long arg);
95
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +000096static LIST_HEAD(fib6_walkers);
97#define FOR_WALKERS(w) list_for_each_entry(w, &fib6_walkers, lh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Adrian Bunk90d41122006-08-14 23:49:16 -070099static inline void fib6_walker_link(struct fib6_walker_t *w)
100{
101 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +0000102 list_add(&w->lh, &fib6_walkers);
Adrian Bunk90d41122006-08-14 23:49:16 -0700103 write_unlock_bh(&fib6_walker_lock);
104}
105
106static inline void fib6_walker_unlink(struct fib6_walker_t *w)
107{
108 write_lock_bh(&fib6_walker_lock);
Alexey Dobriyanbbef49d2010-02-18 08:13:30 +0000109 list_del(&w->lh);
Adrian Bunk90d41122006-08-14 23:49:16 -0700110 write_unlock_bh(&fib6_walker_lock);
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static __inline__ u32 fib6_new_sernum(void)
113{
114 u32 n = ++rt_sernum;
115 if ((__s32)n <= 0)
116 rt_sernum = n = 1;
117 return n;
118}
119
120/*
121 * Auxiliary address test functions for the radix tree.
122 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900123 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * 64bit processors)
125 */
126
127/*
128 * test bit
129 */
130
Al Viroe69a4ad2006-11-14 20:56:00 -0800131static __inline__ __be32 addr_bit_set(void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Al Viroe69a4ad2006-11-14 20:56:00 -0800133 __be32 *addr = token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static __inline__ struct fib6_node * node_alloc(void)
139{
140 struct fib6_node *fn;
141
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800142 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return fn;
145}
146
147static __inline__ void node_free(struct fib6_node * fn)
148{
149 kmem_cache_free(fib6_node_kmem, fn);
150}
151
152static __inline__ void rt6_release(struct rt6_info *rt)
153{
154 if (atomic_dec_and_test(&rt->rt6i_ref))
155 dst_free(&rt->u.dst);
156}
157
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800158static void fib6_link_table(struct net *net, struct fib6_table *tb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700159{
160 unsigned int h;
161
Thomas Graf375216a2006-10-21 20:20:54 -0700162 /*
163 * Initialize table lock at a single place to give lockdep a key,
164 * tables aren't visible prior to being linked to the list.
165 */
166 rwlock_init(&tb->tb6_lock);
167
Neil Hormana33bc5c2009-07-30 18:52:15 -0700168 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700169
170 /*
171 * No protection necessary, this is the only list mutatation
172 * operation, tables never disappear once they exist.
173 */
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800174 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700175}
176
177#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b85592008-03-03 23:24:31 -0800178
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800179static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700180{
181 struct fib6_table *table;
182
183 table = kzalloc(sizeof(*table), GFP_ATOMIC);
184 if (table != NULL) {
185 table->tb6_id = id;
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800186 table->tb6_root.leaf = net->ipv6.ip6_null_entry;
Thomas Grafc71099a2006-08-04 23:20:06 -0700187 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
188 }
189
190 return table;
191}
192
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800193struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700194{
195 struct fib6_table *tb;
196
197 if (id == 0)
198 id = RT6_TABLE_MAIN;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800199 tb = fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700200 if (tb)
201 return tb;
202
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800203 tb = fib6_alloc_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700204 if (tb != NULL)
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800205 fib6_link_table(net, tb);
Thomas Grafc71099a2006-08-04 23:20:06 -0700206
207 return tb;
208}
209
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800210struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700211{
212 struct fib6_table *tb;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800213 struct hlist_head *head;
Thomas Grafc71099a2006-08-04 23:20:06 -0700214 struct hlist_node *node;
215 unsigned int h;
216
217 if (id == 0)
218 id = RT6_TABLE_MAIN;
Neil Hormana33bc5c2009-07-30 18:52:15 -0700219 h = id & (FIB6_TABLE_HASHSZ - 1);
Thomas Grafc71099a2006-08-04 23:20:06 -0700220 rcu_read_lock();
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800221 head = &net->ipv6.fib_table_hash[h];
222 hlist_for_each_entry_rcu(tb, node, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -0700223 if (tb->tb6_id == id) {
224 rcu_read_unlock();
225 return tb;
226 }
227 }
228 rcu_read_unlock();
229
230 return NULL;
231}
232
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000233static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700234{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800235 fib6_link_table(net, net->ipv6.fib6_main_tbl);
236 fib6_link_table(net, net->ipv6.fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700237}
Thomas Grafc71099a2006-08-04 23:20:06 -0700238#else
239
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800240struct fib6_table *fib6_new_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700241{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800242 return fib6_get_table(net, id);
Thomas Grafc71099a2006-08-04 23:20:06 -0700243}
244
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800245struct fib6_table *fib6_get_table(struct net *net, u32 id)
Thomas Grafc71099a2006-08-04 23:20:06 -0700246{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800247 return net->ipv6.fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700248}
249
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800250struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi *fl,
251 int flags, pol_lookup_t lookup)
Thomas Grafc71099a2006-08-04 23:20:06 -0700252{
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800253 return (struct dst_entry *) lookup(net, net->ipv6.fib6_main_tbl, fl, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700254}
255
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000256static void __net_init fib6_tables_init(struct net *net)
Thomas Grafc71099a2006-08-04 23:20:06 -0700257{
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800258 fib6_link_table(net, net->ipv6.fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700259}
260
261#endif
262
Patrick McHardy1b43af52006-08-10 23:11:17 -0700263static int fib6_dump_node(struct fib6_walker_t *w)
264{
265 int res;
266 struct rt6_info *rt;
267
Eric Dumazet7cc48262007-02-09 16:22:57 -0800268 for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700269 res = rt6_dump_route(rt, w->args);
270 if (res < 0) {
271 /* Frame is full, suspend walking */
272 w->leaf = rt;
273 return 1;
274 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700275 WARN_ON(res == 0);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700276 }
277 w->leaf = NULL;
278 return 0;
279}
280
281static void fib6_dump_end(struct netlink_callback *cb)
282{
283 struct fib6_walker_t *w = (void*)cb->args[2];
284
285 if (w) {
Herbert Xu7891cc82009-01-13 22:17:51 -0800286 if (cb->args[4]) {
287 cb->args[4] = 0;
288 fib6_walker_unlink(w);
289 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700290 cb->args[2] = 0;
291 kfree(w);
292 }
293 cb->done = (void*)cb->args[3];
294 cb->args[1] = 3;
295}
296
297static int fib6_dump_done(struct netlink_callback *cb)
298{
299 fib6_dump_end(cb);
300 return cb->done ? cb->done(cb) : 0;
301}
302
303static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
304 struct netlink_callback *cb)
305{
306 struct fib6_walker_t *w;
307 int res;
308
309 w = (void *)cb->args[2];
310 w->root = &table->tb6_root;
311
312 if (cb->args[4] == 0) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000313 w->count = 0;
314 w->skip = 0;
315
Patrick McHardy1b43af52006-08-10 23:11:17 -0700316 read_lock_bh(&table->tb6_lock);
317 res = fib6_walk(w);
318 read_unlock_bh(&table->tb6_lock);
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000319 if (res > 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700320 cb->args[4] = 1;
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000321 cb->args[5] = w->root->fn_sernum;
322 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700323 } else {
Patrick McHardy2bec5a32010-02-08 05:19:03 +0000324 if (cb->args[5] != w->root->fn_sernum) {
325 /* Begin at the root if the tree changed */
326 cb->args[5] = w->root->fn_sernum;
327 w->state = FWS_INIT;
328 w->node = w->root;
329 w->skip = w->count;
330 } else
331 w->skip = 0;
332
Patrick McHardy1b43af52006-08-10 23:11:17 -0700333 read_lock_bh(&table->tb6_lock);
334 res = fib6_walk_continue(w);
335 read_unlock_bh(&table->tb6_lock);
Herbert Xu7891cc82009-01-13 22:17:51 -0800336 if (res <= 0) {
337 fib6_walker_unlink(w);
338 cb->args[4] = 0;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700339 }
Patrick McHardy1b43af52006-08-10 23:11:17 -0700340 }
Herbert Xu7891cc82009-01-13 22:17:51 -0800341
Patrick McHardy1b43af52006-08-10 23:11:17 -0700342 return res;
343}
344
Thomas Grafc127ea22007-03-22 11:58:32 -0700345static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700346{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900347 struct net *net = sock_net(skb->sk);
Patrick McHardy1b43af52006-08-10 23:11:17 -0700348 unsigned int h, s_h;
349 unsigned int e = 0, s_e;
350 struct rt6_rtnl_dump_arg arg;
351 struct fib6_walker_t *w;
352 struct fib6_table *tb;
353 struct hlist_node *node;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800354 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700355 int res = 0;
356
357 s_h = cb->args[0];
358 s_e = cb->args[1];
359
360 w = (void *)cb->args[2];
361 if (w == NULL) {
362 /* New dump:
363 *
364 * 1. hook callback destructor.
365 */
366 cb->args[3] = (long)cb->done;
367 cb->done = fib6_dump_done;
368
369 /*
370 * 2. allocate and initialize walker.
371 */
372 w = kzalloc(sizeof(*w), GFP_ATOMIC);
373 if (w == NULL)
374 return -ENOMEM;
375 w->func = fib6_dump_node;
376 cb->args[2] = (long)w;
377 }
378
379 arg.skb = skb;
380 arg.cb = cb;
Brian Haley191cd582008-08-14 15:33:21 -0700381 arg.net = net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700382 w->args = &arg;
383
Neil Hormana33bc5c2009-07-30 18:52:15 -0700384 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700385 e = 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -0800386 head = &net->ipv6.fib_table_hash[h];
387 hlist_for_each_entry(tb, node, head, tb6_hlist) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700388 if (e < s_e)
389 goto next;
390 res = fib6_dump_table(tb, skb, cb);
391 if (res != 0)
392 goto out;
393next:
394 e++;
395 }
396 }
397out:
398 cb->args[1] = e;
399 cb->args[0] = h;
400
401 res = res < 0 ? res : skb->len;
402 if (res <= 0)
403 fib6_dump_end(cb);
404 return res;
405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407/*
408 * Routing Table
409 *
410 * return the appropriate node for a routing tree "add" operation
411 * by either creating and inserting or by returning an existing
412 * node.
413 */
414
415static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
416 int addrlen, int plen,
417 int offset)
418{
419 struct fib6_node *fn, *in, *ln;
420 struct fib6_node *pn = NULL;
421 struct rt6key *key;
422 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900423 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 __u32 sernum = fib6_new_sernum();
425
426 RT6_TRACE("fib6_add_1\n");
427
428 /* insert node in tree */
429
430 fn = root;
431
432 do {
433 key = (struct rt6key *)((u8 *)fn->leaf + offset);
434
435 /*
436 * Prefix match
437 */
438 if (plen < fn->fn_bit ||
439 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
440 goto insert_above;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /*
443 * Exact match ?
444 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (plen == fn->fn_bit) {
447 /* clean up an intermediate node */
448 if ((fn->fn_flags & RTN_RTINFO) == 0) {
449 rt6_release(fn->leaf);
450 fn->leaf = NULL;
451 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return fn;
456 }
457
458 /*
459 * We have more bits to go
460 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* Try to walk down on tree. */
463 fn->fn_sernum = sernum;
464 dir = addr_bit_set(addr, fn->fn_bit);
465 pn = fn;
466 fn = dir ? fn->right: fn->left;
467 } while (fn);
468
469 /*
470 * We walked to the bottom of tree.
471 * Create new leaf node without children.
472 */
473
474 ln = node_alloc();
475
476 if (ln == NULL)
477 return NULL;
478 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ln->parent = pn;
481 ln->fn_sernum = sernum;
482
483 if (dir)
484 pn->right = ln;
485 else
486 pn->left = ln;
487
488 return ln;
489
490
491insert_above:
492 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900493 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * we have a less significant route.
495 * we've to insert an intermediate node on the list
496 * this new node will point to the one we need to create
497 * and the current
498 */
499
500 pn = fn->parent;
501
502 /* find 1st bit in difference between the 2 addrs.
503
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800504 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 but if it is >= plen, the value is ignored in any case.
506 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900507
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800508 bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900510 /*
511 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * / \
513 * (new leaf node)[ln] (old node)[fn]
514 */
515 if (plen > bit) {
516 in = node_alloc();
517 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (in == NULL || ln == NULL) {
520 if (in)
521 node_free(in);
522 if (ln)
523 node_free(ln);
524 return NULL;
525 }
526
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900527 /*
528 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * RTN_RTINFO will
530 * be off since that an address that chooses one of
531 * the branches would not match less specific routes
532 * in the other branch
533 */
534
535 in->fn_bit = bit;
536
537 in->parent = pn;
538 in->leaf = fn->leaf;
539 atomic_inc(&in->leaf->rt6i_ref);
540
541 in->fn_sernum = sernum;
542
543 /* update parent pointer */
544 if (dir)
545 pn->right = in;
546 else
547 pn->left = in;
548
549 ln->fn_bit = plen;
550
551 ln->parent = in;
552 fn->parent = in;
553
554 ln->fn_sernum = sernum;
555
556 if (addr_bit_set(addr, bit)) {
557 in->right = ln;
558 in->left = fn;
559 } else {
560 in->left = ln;
561 in->right = fn;
562 }
563 } else { /* plen <= bit */
564
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900565 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * (new leaf node)[ln]
567 * / \
568 * (old node)[fn] NULL
569 */
570
571 ln = node_alloc();
572
573 if (ln == NULL)
574 return NULL;
575
576 ln->fn_bit = plen;
577
578 ln->parent = pn;
579
580 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (dir)
583 pn->right = ln;
584 else
585 pn->left = ln;
586
587 if (addr_bit_set(&key->addr, plen))
588 ln->right = fn;
589 else
590 ln->left = fn;
591
592 fn->parent = ln;
593 }
594 return ln;
595}
596
597/*
598 * Insert routing information in a node.
599 */
600
601static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Thomas Graf86872cb2006-08-22 00:01:08 -0700602 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct rt6_info *iter = NULL;
605 struct rt6_info **ins;
606
607 ins = &fn->leaf;
608
Eric Dumazet7cc48262007-02-09 16:22:57 -0800609 for (iter = fn->leaf; iter; iter=iter->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
611 * Search for duplicates
612 */
613
614 if (iter->rt6i_metric == rt->rt6i_metric) {
615 /*
616 * Same priority level
617 */
618
619 if (iter->rt6i_dev == rt->rt6i_dev &&
620 iter->rt6i_idev == rt->rt6i_idev &&
621 ipv6_addr_equal(&iter->rt6i_gateway,
622 &rt->rt6i_gateway)) {
623 if (!(iter->rt6i_flags&RTF_EXPIRES))
624 return -EEXIST;
625 iter->rt6i_expires = rt->rt6i_expires;
626 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
627 iter->rt6i_flags &= ~RTF_EXPIRES;
628 iter->rt6i_expires = 0;
629 }
630 return -EEXIST;
631 }
632 }
633
634 if (iter->rt6i_metric > rt->rt6i_metric)
635 break;
636
Eric Dumazet7cc48262007-02-09 16:22:57 -0800637 ins = &iter->u.dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
David S. Millerf11e6652007-03-24 20:36:25 -0700640 /* Reset round-robin state, if necessary */
641 if (ins == &fn->leaf)
642 fn->rr_ptr = NULL;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
645 * insert node
646 */
647
Eric Dumazet7cc48262007-02-09 16:22:57 -0800648 rt->u.dst.rt6_next = iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 *ins = rt;
650 rt->rt6i_node = fn;
651 atomic_inc(&rt->rt6i_ref);
Thomas Graf86872cb2006-08-22 00:01:08 -0700652 inet6_rt_notify(RTM_NEWROUTE, rt, info);
Benjamin Theryc5728722008-03-03 23:34:17 -0800653 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if ((fn->fn_flags & RTN_RTINFO) == 0) {
Benjamin Theryc5728722008-03-03 23:34:17 -0800656 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 fn->fn_flags |= RTN_RTINFO;
658 }
659
660 return 0;
661}
662
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800663static __inline__ void fib6_start_gc(struct net *net, struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700665 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700667 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700668 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800671void fib6_force_start_gc(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Stephen Hemminger417f28b2008-07-22 14:33:45 -0700673 if (!timer_pending(&net->ipv6.ip6_fib_timer))
674 mod_timer(&net->ipv6.ip6_fib_timer,
Stephen Hemminger847499c2008-07-21 13:21:35 -0700675 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678/*
679 * Add routing information to the routing tree.
680 * <destination addr>/<source addr>
681 * with source addr info in sub-trees
682 */
683
Thomas Graf86872cb2006-08-22 00:01:08 -0700684int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700686 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int err = -ENOMEM;
688
689 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
690 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
691
692 if (fn == NULL)
693 goto out;
694
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700695 pn = fn;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697#ifdef CONFIG_IPV6_SUBTREES
698 if (rt->rt6i_src.plen) {
699 struct fib6_node *sn;
700
701 if (fn->subtree == NULL) {
702 struct fib6_node *sfn;
703
704 /*
705 * Create subtree.
706 *
707 * fn[main tree]
708 * |
709 * sfn[subtree root]
710 * \
711 * sn[new leaf node]
712 */
713
714 /* Create subtree root node */
715 sfn = node_alloc();
716 if (sfn == NULL)
717 goto st_failure;
718
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800719 sfn->leaf = info->nl_net->ipv6.ip6_null_entry;
720 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 sfn->fn_flags = RTN_ROOT;
722 sfn->fn_sernum = fib6_new_sernum();
723
724 /* Now add the first leaf node to new subtree */
725
726 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
727 sizeof(struct in6_addr), rt->rt6i_src.plen,
728 offsetof(struct rt6_info, rt6i_src));
729
730 if (sn == NULL) {
731 /* If it is failed, discard just allocated
732 root, and then (in st_failure) stale node
733 in main tree.
734 */
735 node_free(sfn);
736 goto st_failure;
737 }
738
739 /* Now link new subtree to main tree */
740 sfn->parent = fn;
741 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 } else {
743 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
744 sizeof(struct in6_addr), rt->rt6i_src.plen,
745 offsetof(struct rt6_info, rt6i_src));
746
747 if (sn == NULL)
748 goto st_failure;
749 }
750
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700751 if (fn->leaf == NULL) {
752 fn->leaf = rt;
753 atomic_inc(&rt->rt6i_ref);
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 fn = sn;
756 }
757#endif
758
Thomas Graf86872cb2006-08-22 00:01:08 -0700759 err = fib6_add_rt2node(fn, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (err == 0) {
Daniel Lezcano63152fc2008-03-03 23:31:11 -0800762 fib6_start_gc(info->nl_net, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (!(rt->rt6i_flags&RTF_CACHE))
Benjamin Theryec7d43c2008-03-03 23:31:57 -0800764 fib6_prune_clones(info->nl_net, pn, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
767out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700768 if (err) {
769#ifdef CONFIG_IPV6_SUBTREES
770 /*
771 * If fib6_add_1 has cleared the old leaf pointer in the
772 * super-tree leaf node we have to find a new one for it.
773 */
David S. Miller3c051232008-04-18 01:46:19 -0700774 if (pn != fn && pn->leaf == rt) {
775 pn->leaf = NULL;
776 atomic_dec(&rt->rt6i_ref);
777 }
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700778 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800779 pn->leaf = fib6_find_prefix(info->nl_net, pn);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700780#if RT6_DEBUG >= 2
781 if (!pn->leaf) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700782 WARN_ON(pn->leaf == NULL);
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800783 pn->leaf = info->nl_net->ipv6.ip6_null_entry;
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700784 }
785#endif
786 atomic_inc(&pn->leaf->rt6i_ref);
787 }
788#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 dst_free(&rt->u.dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return err;
792
793#ifdef CONFIG_IPV6_SUBTREES
794 /* Subtree creation failed, probably main tree node
795 is orphan. If it is, shoot it.
796 */
797st_failure:
798 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800799 fib6_repair_tree(info->nl_net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 dst_free(&rt->u.dst);
801 return err;
802#endif
803}
804
805/*
806 * Routing tree lookup
807 *
808 */
809
810struct lookup_args {
811 int offset; /* key offset on rt6_info */
812 struct in6_addr *addr; /* search key */
813};
814
815static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
816 struct lookup_args *args)
817{
818 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -0800819 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700821 if (unlikely(args->offset == 0))
822 return NULL;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /*
825 * Descend on a tree
826 */
827
828 fn = root;
829
830 for (;;) {
831 struct fib6_node *next;
832
833 dir = addr_bit_set(args->addr, fn->fn_bit);
834
835 next = dir ? fn->right : fn->left;
836
837 if (next) {
838 fn = next;
839 continue;
840 }
841
842 break;
843 }
844
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700845 while(fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -0700846 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct rt6key *key;
848
849 key = (struct rt6key *) ((u8 *) fn->leaf +
850 args->offset);
851
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700852 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
853#ifdef CONFIG_IPV6_SUBTREES
854 if (fn->subtree)
855 fn = fib6_lookup_1(fn->subtree, args + 1);
856#endif
857 if (!fn || fn->fn_flags & RTN_RTINFO)
858 return fn;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700862 if (fn->fn_flags & RTN_ROOT)
863 break;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 fn = fn->parent;
866 }
867
868 return NULL;
869}
870
871struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
872 struct in6_addr *saddr)
873{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700875 struct lookup_args args[] = {
876 {
877 .offset = offsetof(struct rt6_info, rt6i_dst),
878 .addr = daddr,
879 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700881 {
882 .offset = offsetof(struct rt6_info, rt6i_src),
883 .addr = saddr,
884 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700886 {
887 .offset = 0, /* sentinel */
888 }
889 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -0700891 fn = fib6_lookup_1(root, daddr ? args : args + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
894 fn = root;
895
896 return fn;
897}
898
899/*
900 * Get node with specified destination prefix (and source prefix,
901 * if subtrees are used)
902 */
903
904
905static struct fib6_node * fib6_locate_1(struct fib6_node *root,
906 struct in6_addr *addr,
907 int plen, int offset)
908{
909 struct fib6_node *fn;
910
911 for (fn = root; fn ; ) {
912 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
913
914 /*
915 * Prefix match
916 */
917 if (plen < fn->fn_bit ||
918 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
919 return NULL;
920
921 if (plen == fn->fn_bit)
922 return fn;
923
924 /*
925 * We have more bits to go
926 */
927 if (addr_bit_set(addr, fn->fn_bit))
928 fn = fn->right;
929 else
930 fn = fn->left;
931 }
932 return NULL;
933}
934
935struct fib6_node * fib6_locate(struct fib6_node *root,
936 struct in6_addr *daddr, int dst_len,
937 struct in6_addr *saddr, int src_len)
938{
939 struct fib6_node *fn;
940
941 fn = fib6_locate_1(root, daddr, dst_len,
942 offsetof(struct rt6_info, rt6i_dst));
943
944#ifdef CONFIG_IPV6_SUBTREES
945 if (src_len) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700946 WARN_ON(saddr == NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700947 if (fn && fn->subtree)
948 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 offsetof(struct rt6_info, rt6i_src));
950 }
951#endif
952
953 if (fn && fn->fn_flags&RTN_RTINFO)
954 return fn;
955
956 return NULL;
957}
958
959
960/*
961 * Deletion
962 *
963 */
964
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800965static struct rt6_info *fib6_find_prefix(struct net *net, struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 if (fn->fn_flags&RTN_ROOT)
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800968 return net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 while(fn) {
971 if(fn->left)
972 return fn->left->leaf;
973
974 if(fn->right)
975 return fn->right->leaf;
976
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -0700977 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979 return NULL;
980}
981
982/*
983 * Called to trim the tree of intermediate nodes when possible. "fn"
984 * is the node we want to try and remove.
985 */
986
Daniel Lezcano8ed67782008-03-04 13:48:30 -0800987static struct fib6_node *fib6_repair_tree(struct net *net,
988 struct fib6_node *fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 int children;
991 int nstate;
992 struct fib6_node *child, *pn;
993 struct fib6_walker_t *w;
994 int iter = 0;
995
996 for (;;) {
997 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
998 iter++;
999
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001000 WARN_ON(fn->fn_flags & RTN_RTINFO);
1001 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1002 WARN_ON(fn->leaf != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 children = 0;
1005 child = NULL;
1006 if (fn->right) child = fn->right, children |= 1;
1007 if (fn->left) child = fn->left, children |= 2;
1008
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001009 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010#ifdef CONFIG_IPV6_SUBTREES
1011 /* Subtree root (i.e. fn) may have one child */
1012 || (children && fn->fn_flags&RTN_ROOT)
1013#endif
1014 ) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001015 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016#if RT6_DEBUG >= 2
1017 if (fn->leaf==NULL) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001018 WARN_ON(!fn->leaf);
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001019 fn->leaf = net->ipv6.ip6_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021#endif
1022 atomic_inc(&fn->leaf->rt6i_ref);
1023 return fn->parent;
1024 }
1025
1026 pn = fn->parent;
1027#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001028 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001029 WARN_ON(!(fn->fn_flags & RTN_ROOT));
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001030 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 nstate = FWS_L;
1032 } else {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001033 WARN_ON(fn->fn_flags & RTN_ROOT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034#endif
1035 if (pn->right == fn) pn->right = child;
1036 else if (pn->left == fn) pn->left = child;
1037#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001038 else
1039 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040#endif
1041 if (child)
1042 child->parent = pn;
1043 nstate = FWS_R;
1044#ifdef CONFIG_IPV6_SUBTREES
1045 }
1046#endif
1047
1048 read_lock(&fib6_walker_lock);
1049 FOR_WALKERS(w) {
1050 if (child == NULL) {
1051 if (w->root == fn) {
1052 w->root = w->node = NULL;
1053 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1054 } else if (w->node == fn) {
1055 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1056 w->node = pn;
1057 w->state = nstate;
1058 }
1059 } else {
1060 if (w->root == fn) {
1061 w->root = child;
1062 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1063 }
1064 if (w->node == fn) {
1065 w->node = child;
1066 if (children&2) {
1067 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1068 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
1069 } else {
1070 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1071 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
1072 }
1073 }
1074 }
1075 }
1076 read_unlock(&fib6_walker_lock);
1077
1078 node_free(fn);
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001079 if (pn->fn_flags&RTN_RTINFO || FIB6_SUBTREE(pn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return pn;
1081
1082 rt6_release(pn->leaf);
1083 pn->leaf = NULL;
1084 fn = pn;
1085 }
1086}
1087
1088static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Thomas Graf86872cb2006-08-22 00:01:08 -07001089 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct fib6_walker_t *w;
1092 struct rt6_info *rt = *rtp;
Benjamin Theryc5728722008-03-03 23:34:17 -08001093 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 RT6_TRACE("fib6_del_route\n");
1096
1097 /* Unlink it */
Eric Dumazet7cc48262007-02-09 16:22:57 -08001098 *rtp = rt->u.dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 rt->rt6i_node = NULL;
Benjamin Theryc5728722008-03-03 23:34:17 -08001100 net->ipv6.rt6_stats->fib_rt_entries--;
1101 net->ipv6.rt6_stats->fib_discarded_routes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
David S. Millerf11e6652007-03-24 20:36:25 -07001103 /* Reset round-robin state, if necessary */
1104 if (fn->rr_ptr == rt)
1105 fn->rr_ptr = NULL;
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /* Adjust walkers */
1108 read_lock(&fib6_walker_lock);
1109 FOR_WALKERS(w) {
1110 if (w->state == FWS_C && w->leaf == rt) {
1111 RT6_TRACE("walker %p adjusted by delroute\n", w);
Eric Dumazet7cc48262007-02-09 16:22:57 -08001112 w->leaf = rt->u.dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (w->leaf == NULL)
1114 w->state = FWS_U;
1115 }
1116 }
1117 read_unlock(&fib6_walker_lock);
1118
Eric Dumazet7cc48262007-02-09 16:22:57 -08001119 rt->u.dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* If it was last route, expunge its radix tree node */
1122 if (fn->leaf == NULL) {
1123 fn->fn_flags &= ~RTN_RTINFO;
Benjamin Theryc5728722008-03-03 23:34:17 -08001124 net->ipv6.rt6_stats->fib_route_nodes--;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001125 fn = fib6_repair_tree(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 if (atomic_read(&rt->rt6i_ref) != 1) {
1129 /* This route is used as dummy address holder in some split
1130 * nodes. It is not leaked, but it still holds other resources,
1131 * which must be released in time. So, scan ascendant nodes
1132 * and replace dummy references to this route with references
1133 * to still alive ones.
1134 */
1135 while (fn) {
1136 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001137 fn->leaf = fib6_find_prefix(net, fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 atomic_inc(&fn->leaf->rt6i_ref);
1139 rt6_release(rt);
1140 }
1141 fn = fn->parent;
1142 }
1143 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001144 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
Thomas Graf86872cb2006-08-22 00:01:08 -07001147 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 rt6_release(rt);
1149}
1150
Thomas Graf86872cb2006-08-22 00:01:08 -07001151int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001153 struct net *net = info->nl_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct fib6_node *fn = rt->rt6i_node;
1155 struct rt6_info **rtp;
1156
1157#if RT6_DEBUG >= 2
1158 if (rt->u.dst.obsolete>0) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001159 WARN_ON(fn != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -ENOENT;
1161 }
1162#endif
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001163 if (fn == NULL || rt == net->ipv6.ip6_null_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return -ENOENT;
1165
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001166 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001168 if (!(rt->rt6i_flags&RTF_CACHE)) {
1169 struct fib6_node *pn = fn;
1170#ifdef CONFIG_IPV6_SUBTREES
1171 /* clones of this route might be in another subtree */
1172 if (rt->rt6i_src.plen) {
1173 while (!(pn->fn_flags&RTN_ROOT))
1174 pn = pn->parent;
1175 pn = pn->parent;
1176 }
1177#endif
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001178 fib6_prune_clones(info->nl_net, pn, rt);
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /*
1182 * Walk the leaf entries looking for ourself
1183 */
1184
Eric Dumazet7cc48262007-02-09 16:22:57 -08001185 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001187 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return 0;
1189 }
1190 }
1191 return -ENOENT;
1192}
1193
1194/*
1195 * Tree traversal function.
1196 *
1197 * Certainly, it is not interrupt safe.
1198 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1199 * It means, that we can modify tree during walking
1200 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001201 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 *
1203 * It guarantees that every node will be traversed,
1204 * and that it will be traversed only once.
1205 *
1206 * Callback function w->func may return:
1207 * 0 -> continue walking.
1208 * positive value -> walking is suspended (used by tree dumps,
1209 * and probably by gc, if it will be split to several slices)
1210 * negative value -> terminate walking.
1211 *
1212 * The function itself returns:
1213 * 0 -> walk is complete.
1214 * >0 -> walk is incomplete (i.e. suspended)
1215 * <0 -> walk is terminated by an error.
1216 */
1217
Adrian Bunk90d41122006-08-14 23:49:16 -07001218static int fib6_walk_continue(struct fib6_walker_t *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct fib6_node *fn, *pn;
1221
1222 for (;;) {
1223 fn = w->node;
1224 if (fn == NULL)
1225 return 0;
1226
1227 if (w->prune && fn != w->root &&
1228 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1229 w->state = FWS_C;
1230 w->leaf = fn->leaf;
1231 }
1232 switch (w->state) {
1233#ifdef CONFIG_IPV6_SUBTREES
1234 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001235 if (FIB6_SUBTREE(fn)) {
1236 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 continue;
1238 }
1239 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001240#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 case FWS_L:
1242 if (fn->left) {
1243 w->node = fn->left;
1244 w->state = FWS_INIT;
1245 continue;
1246 }
1247 w->state = FWS_R;
1248 case FWS_R:
1249 if (fn->right) {
1250 w->node = fn->right;
1251 w->state = FWS_INIT;
1252 continue;
1253 }
1254 w->state = FWS_C;
1255 w->leaf = fn->leaf;
1256 case FWS_C:
1257 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001258 int err;
1259
1260 if (w->count < w->skip) {
1261 w->count++;
1262 continue;
1263 }
1264
1265 err = w->func(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (err)
1267 return err;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001268
1269 w->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 continue;
1271 }
1272 w->state = FWS_U;
1273 case FWS_U:
1274 if (fn == w->root)
1275 return 0;
1276 pn = fn->parent;
1277 w->node = pn;
1278#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001279 if (FIB6_SUBTREE(pn) == fn) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001280 WARN_ON(!(fn->fn_flags & RTN_ROOT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 w->state = FWS_L;
1282 continue;
1283 }
1284#endif
1285 if (pn->left == fn) {
1286 w->state = FWS_R;
1287 continue;
1288 }
1289 if (pn->right == fn) {
1290 w->state = FWS_C;
1291 w->leaf = w->node->leaf;
1292 continue;
1293 }
1294#if RT6_DEBUG >= 2
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001295 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296#endif
1297 }
1298 }
1299}
1300
Adrian Bunk90d41122006-08-14 23:49:16 -07001301static int fib6_walk(struct fib6_walker_t *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 int res;
1304
1305 w->state = FWS_INIT;
1306 w->node = w->root;
1307
1308 fib6_walker_link(w);
1309 res = fib6_walk_continue(w);
1310 if (res <= 0)
1311 fib6_walker_unlink(w);
1312 return res;
1313}
1314
1315static int fib6_clean_node(struct fib6_walker_t *w)
1316{
1317 int res;
1318 struct rt6_info *rt;
Benjamin Thery0a8891a2007-10-08 20:39:36 -07001319 struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001320 struct nl_info info = {
1321 .nl_net = c->net,
1322 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Eric Dumazet7cc48262007-02-09 16:22:57 -08001324 for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 res = c->func(rt, c->arg);
1326 if (res < 0) {
1327 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001328 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (res) {
1330#if RT6_DEBUG >= 2
1331 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1332#endif
1333 continue;
1334 }
1335 return 0;
1336 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001337 WARN_ON(res != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 w->leaf = rt;
1340 return 0;
1341}
1342
1343/*
1344 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001345 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * func is called on each route.
1347 * It may return -1 -> delete this route.
1348 * 0 -> continue walking
1349 *
1350 * prune==1 -> only immediate children of node (certainly,
1351 * ignoring pure split nodes) will be scanned.
1352 */
1353
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001354static void fib6_clean_tree(struct net *net, struct fib6_node *root,
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001355 int (*func)(struct rt6_info *, void *arg),
1356 int prune, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct fib6_cleaner_t c;
1359
1360 c.w.root = root;
1361 c.w.func = fib6_clean_node;
1362 c.w.prune = prune;
Patrick McHardy2bec5a32010-02-08 05:19:03 +00001363 c.w.count = 0;
1364 c.w.skip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 c.func = func;
1366 c.arg = arg;
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001367 c.net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 fib6_walk(&c.w);
1370}
1371
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001372void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
Thomas Grafc71099a2006-08-04 23:20:06 -07001373 int prune, void *arg)
1374{
Thomas Grafc71099a2006-08-04 23:20:06 -07001375 struct fib6_table *table;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001376 struct hlist_node *node;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001377 struct hlist_head *head;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001378 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001379
Patrick McHardy1b43af52006-08-10 23:11:17 -07001380 rcu_read_lock();
Neil Hormana33bc5c2009-07-30 18:52:15 -07001381 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001382 head = &net->ipv6.fib_table_hash[h];
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001383 hlist_for_each_entry_rcu(table, node, head, tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001384 write_lock_bh(&table->tb6_lock);
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001385 fib6_clean_tree(net, &table->tb6_root,
1386 func, prune, arg);
Thomas Grafc71099a2006-08-04 23:20:06 -07001387 write_unlock_bh(&table->tb6_lock);
1388 }
1389 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001390 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001391}
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1394{
1395 if (rt->rt6i_flags & RTF_CACHE) {
1396 RT6_TRACE("pruning clone %p\n", rt);
1397 return -1;
1398 }
1399
1400 return 0;
1401}
1402
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001403static void fib6_prune_clones(struct net *net, struct fib6_node *fn,
1404 struct rt6_info *rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Benjamin Theryec7d43c2008-03-03 23:31:57 -08001406 fib6_clean_tree(net, fn, fib6_prune_clone, 1, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
1409/*
1410 * Garbage collection
1411 */
1412
1413static struct fib6_gc_args
1414{
1415 int timeout;
1416 int more;
1417} gc_args;
1418
1419static int fib6_age(struct rt6_info *rt, void *arg)
1420{
1421 unsigned long now = jiffies;
1422
1423 /*
1424 * check addrconf expiration here.
1425 * Routes are expired even if they are in use.
1426 *
1427 * Also age clones. Note, that clones are aged out
1428 * only if they are not in use now.
1429 */
1430
1431 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1432 if (time_after(now, rt->rt6i_expires)) {
1433 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return -1;
1435 }
1436 gc_args.more++;
1437 } else if (rt->rt6i_flags & RTF_CACHE) {
1438 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1439 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1440 RT6_TRACE("aging clone %p\n", rt);
1441 return -1;
1442 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1443 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1444 RT6_TRACE("purging route %p via non-router but gateway\n",
1445 rt);
1446 return -1;
1447 }
1448 gc_args.more++;
1449 }
1450
1451 return 0;
1452}
1453
1454static DEFINE_SPINLOCK(fib6_gc_lock);
1455
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001456void fib6_run_gc(unsigned long expires, struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001458 if (expires != ~0UL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 spin_lock_bh(&fib6_gc_lock);
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001460 gc_args.timeout = expires ? (int)expires :
1461 net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 } else {
Stephen Hemmingera76d7342008-07-22 14:34:35 -07001463 if (!spin_trylock_bh(&fib6_gc_lock)) {
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001464 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return;
1466 }
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001467 gc_args.timeout = net->ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Stephen Hemminger3d0f24a2008-07-22 14:35:50 -07001470 gc_args.more = icmp6_dst_gc();
Daniel Lezcanof3db4852008-03-03 23:27:06 -08001471
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001472 fib6_clean_all(net, fib6_age, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (gc_args.more)
Stephen Hemmingerc8a45222008-07-22 14:34:09 -07001475 mod_timer(&net->ipv6.ip6_fib_timer,
1476 round_jiffies(jiffies
1477 + net->ipv6.sysctl.ip6_rt_gc_interval));
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001478 else
1479 del_timer(&net->ipv6.ip6_fib_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 spin_unlock_bh(&fib6_gc_lock);
1481}
1482
Daniel Lezcano5b7c9312008-03-03 23:28:58 -08001483static void fib6_gc_timer_cb(unsigned long arg)
1484{
1485 fib6_run_gc(0, (struct net *)arg);
1486}
1487
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001488static int __net_init fib6_net_init(struct net *net)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001489{
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001490 setup_timer(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, (unsigned long)net);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001491
Benjamin Theryc5728722008-03-03 23:34:17 -08001492 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
1493 if (!net->ipv6.rt6_stats)
1494 goto out_timer;
1495
Neil Hormana33bc5c2009-07-30 18:52:15 -07001496 net->ipv6.fib_table_hash = kcalloc(FIB6_TABLE_HASHSZ,
Stephen Hemminger75307c02008-07-22 14:35:07 -07001497 sizeof(*net->ipv6.fib_table_hash),
1498 GFP_KERNEL);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001499 if (!net->ipv6.fib_table_hash)
Benjamin Theryc5728722008-03-03 23:34:17 -08001500 goto out_rt6_stats;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001501
1502 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
1503 GFP_KERNEL);
1504 if (!net->ipv6.fib6_main_tbl)
1505 goto out_fib_table_hash;
1506
1507 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001508 net->ipv6.fib6_main_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001509 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
1510 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1511
1512#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1513 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
1514 GFP_KERNEL);
1515 if (!net->ipv6.fib6_local_tbl)
1516 goto out_fib6_main_tbl;
1517 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001518 net->ipv6.fib6_local_tbl->tb6_root.leaf = net->ipv6.ip6_null_entry;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001519 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
1520 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1521#endif
1522 fib6_tables_init(net);
1523
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001524 return 0;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001525
1526#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1527out_fib6_main_tbl:
1528 kfree(net->ipv6.fib6_main_tbl);
1529#endif
1530out_fib_table_hash:
1531 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001532out_rt6_stats:
1533 kfree(net->ipv6.rt6_stats);
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001534out_timer:
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001535 return -ENOMEM;
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001536 }
1537
1538static void fib6_net_exit(struct net *net)
1539{
Daniel Lezcano8ed67782008-03-04 13:48:30 -08001540 rt6_ifdown(net, NULL);
Stephen Hemminger417f28b2008-07-22 14:33:45 -07001541 del_timer_sync(&net->ipv6.ip6_fib_timer);
1542
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001543#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1544 kfree(net->ipv6.fib6_local_tbl);
1545#endif
1546 kfree(net->ipv6.fib6_main_tbl);
1547 kfree(net->ipv6.fib_table_hash);
Benjamin Theryc5728722008-03-03 23:34:17 -08001548 kfree(net->ipv6.rt6_stats);
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001549}
1550
1551static struct pernet_operations fib6_net_ops = {
1552 .init = fib6_net_init,
1553 .exit = fib6_net_exit,
1554};
1555
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001556int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Daniel Lezcanoe0b85592008-03-03 23:24:31 -08001558 int ret = -ENOMEM;
Daniel Lezcano63152fc2008-03-03 23:31:11 -08001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1561 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001562 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001563 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001564 if (!fib6_node_kmem)
Daniel Lezcanoe0b85592008-03-03 23:24:31 -08001565 goto out;
1566
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001567 ret = register_pernet_subsys(&fib6_net_ops);
1568 if (ret)
Benjamin Theryc5728722008-03-03 23:34:17 -08001569 goto out_kmem_cache_create;
Daniel Lezcanoe0b85592008-03-03 23:24:31 -08001570
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001571 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib);
1572 if (ret)
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001573 goto out_unregister_subsys;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001574out:
1575 return ret;
1576
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001577out_unregister_subsys:
1578 unregister_pernet_subsys(&fib6_net_ops);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001579out_kmem_cache_create:
1580 kmem_cache_destroy(fib6_node_kmem);
1581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584void fib6_gc_cleanup(void)
1585{
Daniel Lezcano58f09b72008-03-03 23:25:27 -08001586 unregister_pernet_subsys(&fib6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 kmem_cache_destroy(fib6_node_kmem);
1588}