blob: 04d774963f3cc27056bd7d811fb8aef3463c0586 [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 *
8 * $Id: ip6_fib.c,v 1.25 2001/10/31 21:55:55 davem Exp $
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16/*
17 * Changes:
18 * Yuji SEKIYA @USAGI: Support default route on router node;
19 * remove ip6_null_entry from the top of
20 * routing table.
YOSHIFUJI Hideakic0bece92006-08-23 17:23:25 -070021 * Ville Nuorvala: Fixed routing subtrees.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/types.h>
25#include <linux/net.h>
26#include <linux/route.h>
27#include <linux/netdevice.h>
28#include <linux/in6.h>
29#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070030#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#ifdef CONFIG_PROC_FS
33#include <linux/proc_fs.h>
34#endif
35
36#include <net/ipv6.h>
37#include <net/ndisc.h>
38#include <net/addrconf.h>
39
40#include <net/ip6_fib.h>
41#include <net/ip6_route.h>
42
43#define RT6_DEBUG 2
44
45#if RT6_DEBUG >= 3
46#define RT6_TRACE(x...) printk(KERN_DEBUG x)
47#else
48#define RT6_TRACE(x...) do { ; } while (0)
49#endif
50
51struct rt6_statistics rt6_stats;
52
Christoph Lametere18b8902006-12-06 20:33:20 -080053static struct kmem_cache * fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55enum fib_walk_state_t
56{
57#ifdef CONFIG_IPV6_SUBTREES
58 FWS_S,
59#endif
60 FWS_L,
61 FWS_R,
62 FWS_C,
63 FWS_U
64};
65
66struct fib6_cleaner_t
67{
68 struct fib6_walker_t w;
69 int (*func)(struct rt6_info *, void *arg);
70 void *arg;
71};
72
Adrian Bunk90d41122006-08-14 23:49:16 -070073static DEFINE_RWLOCK(fib6_walker_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75#ifdef CONFIG_IPV6_SUBTREES
76#define FWS_INIT FWS_S
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#else
78#define FWS_INIT FWS_L
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#endif
80
81static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -070082static struct rt6_info * fib6_find_prefix(struct fib6_node *fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
Adrian Bunk90d41122006-08-14 23:49:16 -070084static int fib6_walk(struct fib6_walker_t *w);
85static int fib6_walk_continue(struct fib6_walker_t *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * A routing update causes an increase of the serial number on the
89 * affected subtree. This allows for cached routes to be asynchronously
90 * tested when modifications are made to the destination cache as a
91 * result of redirects, path MTU changes, etc.
92 */
93
94static __u32 rt_sernum;
95
Ingo Molnar8d06afa2005-09-09 13:10:40 -070096static DEFINE_TIMER(ip6_fib_timer, fib6_run_gc, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Adrian Bunk90d41122006-08-14 23:49:16 -070098static struct fib6_walker_t fib6_walker_list = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 .prev = &fib6_walker_list,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900100 .next = &fib6_walker_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103#define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
104
Adrian Bunk90d41122006-08-14 23:49:16 -0700105static inline void fib6_walker_link(struct fib6_walker_t *w)
106{
107 write_lock_bh(&fib6_walker_lock);
108 w->next = fib6_walker_list.next;
109 w->prev = &fib6_walker_list;
110 w->next->prev = w;
111 w->prev->next = w;
112 write_unlock_bh(&fib6_walker_lock);
113}
114
115static inline void fib6_walker_unlink(struct fib6_walker_t *w)
116{
117 write_lock_bh(&fib6_walker_lock);
118 w->next->prev = w->prev;
119 w->prev->next = w->next;
120 w->prev = w->next = w;
121 write_unlock_bh(&fib6_walker_lock);
122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static __inline__ u32 fib6_new_sernum(void)
124{
125 u32 n = ++rt_sernum;
126 if ((__s32)n <= 0)
127 rt_sernum = n = 1;
128 return n;
129}
130
131/*
132 * Auxiliary address test functions for the radix tree.
133 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900134 * These assume a 32bit processor (although it will work on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * 64bit processors)
136 */
137
138/*
139 * test bit
140 */
141
Al Viroe69a4ad2006-11-14 20:56:00 -0800142static __inline__ __be32 addr_bit_set(void *token, int fn_bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Al Viroe69a4ad2006-11-14 20:56:00 -0800144 __be32 *addr = token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static __inline__ struct fib6_node * node_alloc(void)
150{
151 struct fib6_node *fn;
152
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800153 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return fn;
156}
157
158static __inline__ void node_free(struct fib6_node * fn)
159{
160 kmem_cache_free(fib6_node_kmem, fn);
161}
162
163static __inline__ void rt6_release(struct rt6_info *rt)
164{
165 if (atomic_dec_and_test(&rt->rt6i_ref))
166 dst_free(&rt->u.dst);
167}
168
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800169static struct fib6_table *fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700170
171#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Patrick McHardy1b43af52006-08-10 23:11:17 -0700172#define FIB_TABLE_HASHSZ 256
173#else
174#define FIB_TABLE_HASHSZ 1
175#endif
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800176static struct hlist_head *fib_table_hash;
Thomas Grafc71099a2006-08-04 23:20:06 -0700177
Patrick McHardy1b43af52006-08-10 23:11:17 -0700178static void fib6_link_table(struct fib6_table *tb)
179{
180 unsigned int h;
181
Thomas Graf375216a2006-10-21 20:20:54 -0700182 /*
183 * Initialize table lock at a single place to give lockdep a key,
184 * tables aren't visible prior to being linked to the list.
185 */
186 rwlock_init(&tb->tb6_lock);
187
Patrick McHardy1b43af52006-08-10 23:11:17 -0700188 h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1);
189
190 /*
191 * No protection necessary, this is the only list mutatation
192 * operation, tables never disappear once they exist.
193 */
194 hlist_add_head_rcu(&tb->tb6_hlist, &fib_table_hash[h]);
195}
196
197#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800198
199static struct fib6_table *fib6_local_tbl;
Thomas Graf101367c2006-08-04 03:39:02 -0700200
Thomas Grafc71099a2006-08-04 23:20:06 -0700201static struct fib6_table *fib6_alloc_table(u32 id)
202{
203 struct fib6_table *table;
204
205 table = kzalloc(sizeof(*table), GFP_ATOMIC);
206 if (table != NULL) {
207 table->tb6_id = id;
Thomas Grafc71099a2006-08-04 23:20:06 -0700208 table->tb6_root.leaf = &ip6_null_entry;
209 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
210 }
211
212 return table;
213}
214
Thomas Grafc71099a2006-08-04 23:20:06 -0700215struct fib6_table *fib6_new_table(u32 id)
216{
217 struct fib6_table *tb;
218
219 if (id == 0)
220 id = RT6_TABLE_MAIN;
221 tb = fib6_get_table(id);
222 if (tb)
223 return tb;
224
225 tb = fib6_alloc_table(id);
226 if (tb != NULL)
227 fib6_link_table(tb);
228
229 return tb;
230}
231
232struct fib6_table *fib6_get_table(u32 id)
233{
234 struct fib6_table *tb;
235 struct hlist_node *node;
236 unsigned int h;
237
238 if (id == 0)
239 id = RT6_TABLE_MAIN;
240 h = id & (FIB_TABLE_HASHSZ - 1);
241 rcu_read_lock();
242 hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb6_hlist) {
243 if (tb->tb6_id == id) {
244 rcu_read_unlock();
245 return tb;
246 }
247 }
248 rcu_read_unlock();
249
250 return NULL;
251}
252
Thomas Grafc71099a2006-08-04 23:20:06 -0700253static void __init fib6_tables_init(void)
254{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800255 fib6_link_table(fib6_main_tbl);
256 fib6_link_table(fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700257}
258
259#else
260
261struct fib6_table *fib6_new_table(u32 id)
262{
263 return fib6_get_table(id);
264}
265
266struct fib6_table *fib6_get_table(u32 id)
267{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800268 return fib6_main_tbl;
Thomas Grafc71099a2006-08-04 23:20:06 -0700269}
270
271struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
272 pol_lookup_t lookup)
273{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800274 return (struct dst_entry *) lookup(fib6_main_tbl, fl, flags);
Thomas Grafc71099a2006-08-04 23:20:06 -0700275}
276
277static void __init fib6_tables_init(void)
278{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -0800279 fib6_link_table(fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700280}
281
282#endif
283
Patrick McHardy1b43af52006-08-10 23:11:17 -0700284static int fib6_dump_node(struct fib6_walker_t *w)
285{
286 int res;
287 struct rt6_info *rt;
288
Eric Dumazet7cc48262007-02-09 16:22:57 -0800289 for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
Patrick McHardy1b43af52006-08-10 23:11:17 -0700290 res = rt6_dump_route(rt, w->args);
291 if (res < 0) {
292 /* Frame is full, suspend walking */
293 w->leaf = rt;
294 return 1;
295 }
296 BUG_TRAP(res!=0);
297 }
298 w->leaf = NULL;
299 return 0;
300}
301
302static void fib6_dump_end(struct netlink_callback *cb)
303{
304 struct fib6_walker_t *w = (void*)cb->args[2];
305
306 if (w) {
307 cb->args[2] = 0;
308 kfree(w);
309 }
310 cb->done = (void*)cb->args[3];
311 cb->args[1] = 3;
312}
313
314static int fib6_dump_done(struct netlink_callback *cb)
315{
316 fib6_dump_end(cb);
317 return cb->done ? cb->done(cb) : 0;
318}
319
320static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
321 struct netlink_callback *cb)
322{
323 struct fib6_walker_t *w;
324 int res;
325
326 w = (void *)cb->args[2];
327 w->root = &table->tb6_root;
328
329 if (cb->args[4] == 0) {
330 read_lock_bh(&table->tb6_lock);
331 res = fib6_walk(w);
332 read_unlock_bh(&table->tb6_lock);
333 if (res > 0)
334 cb->args[4] = 1;
335 } else {
336 read_lock_bh(&table->tb6_lock);
337 res = fib6_walk_continue(w);
338 read_unlock_bh(&table->tb6_lock);
339 if (res != 0) {
340 if (res < 0)
341 fib6_walker_unlink(w);
342 goto end;
343 }
344 fib6_walker_unlink(w);
345 cb->args[4] = 0;
346 }
347end:
348 return res;
349}
350
Thomas Grafc127ea22007-03-22 11:58:32 -0700351static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Patrick McHardy1b43af52006-08-10 23:11:17 -0700352{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100353 struct net *net = skb->sk->sk_net;
Patrick McHardy1b43af52006-08-10 23:11:17 -0700354 unsigned int h, s_h;
355 unsigned int e = 0, s_e;
356 struct rt6_rtnl_dump_arg arg;
357 struct fib6_walker_t *w;
358 struct fib6_table *tb;
359 struct hlist_node *node;
360 int res = 0;
361
Denis V. Lunevb8542722007-12-01 00:21:31 +1100362 if (net != &init_net)
363 return 0;
364
Patrick McHardy1b43af52006-08-10 23:11:17 -0700365 s_h = cb->args[0];
366 s_e = cb->args[1];
367
368 w = (void *)cb->args[2];
369 if (w == NULL) {
370 /* New dump:
371 *
372 * 1. hook callback destructor.
373 */
374 cb->args[3] = (long)cb->done;
375 cb->done = fib6_dump_done;
376
377 /*
378 * 2. allocate and initialize walker.
379 */
380 w = kzalloc(sizeof(*w), GFP_ATOMIC);
381 if (w == NULL)
382 return -ENOMEM;
383 w->func = fib6_dump_node;
384 cb->args[2] = (long)w;
385 }
386
387 arg.skb = skb;
388 arg.cb = cb;
389 w->args = &arg;
390
391 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
392 e = 0;
393 hlist_for_each_entry(tb, node, &fib_table_hash[h], tb6_hlist) {
394 if (e < s_e)
395 goto next;
396 res = fib6_dump_table(tb, skb, cb);
397 if (res != 0)
398 goto out;
399next:
400 e++;
401 }
402 }
403out:
404 cb->args[1] = e;
405 cb->args[0] = h;
406
407 res = res < 0 ? res : skb->len;
408 if (res <= 0)
409 fib6_dump_end(cb);
410 return res;
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/*
414 * Routing Table
415 *
416 * return the appropriate node for a routing tree "add" operation
417 * by either creating and inserting or by returning an existing
418 * node.
419 */
420
421static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
422 int addrlen, int plen,
423 int offset)
424{
425 struct fib6_node *fn, *in, *ln;
426 struct fib6_node *pn = NULL;
427 struct rt6key *key;
428 int bit;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900429 __be32 dir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 __u32 sernum = fib6_new_sernum();
431
432 RT6_TRACE("fib6_add_1\n");
433
434 /* insert node in tree */
435
436 fn = root;
437
438 do {
439 key = (struct rt6key *)((u8 *)fn->leaf + offset);
440
441 /*
442 * Prefix match
443 */
444 if (plen < fn->fn_bit ||
445 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
446 goto insert_above;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 * Exact match ?
450 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (plen == fn->fn_bit) {
453 /* clean up an intermediate node */
454 if ((fn->fn_flags & RTN_RTINFO) == 0) {
455 rt6_release(fn->leaf);
456 fn->leaf = NULL;
457 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 fn->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return fn;
462 }
463
464 /*
465 * We have more bits to go
466 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* Try to walk down on tree. */
469 fn->fn_sernum = sernum;
470 dir = addr_bit_set(addr, fn->fn_bit);
471 pn = fn;
472 fn = dir ? fn->right: fn->left;
473 } while (fn);
474
475 /*
476 * We walked to the bottom of tree.
477 * Create new leaf node without children.
478 */
479
480 ln = node_alloc();
481
482 if (ln == NULL)
483 return NULL;
484 ln->fn_bit = plen;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 ln->parent = pn;
487 ln->fn_sernum = sernum;
488
489 if (dir)
490 pn->right = ln;
491 else
492 pn->left = ln;
493
494 return ln;
495
496
497insert_above:
498 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900499 * split since we don't have a common prefix anymore or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * we have a less significant route.
501 * we've to insert an intermediate node on the list
502 * this new node will point to the one we need to create
503 * and the current
504 */
505
506 pn = fn->parent;
507
508 /* find 1st bit in difference between the 2 addrs.
509
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800510 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 but if it is >= plen, the value is ignored in any case.
512 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900513
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800514 bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900516 /*
517 * (intermediate)[in]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * / \
519 * (new leaf node)[ln] (old node)[fn]
520 */
521 if (plen > bit) {
522 in = node_alloc();
523 ln = node_alloc();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (in == NULL || ln == NULL) {
526 if (in)
527 node_free(in);
528 if (ln)
529 node_free(ln);
530 return NULL;
531 }
532
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900533 /*
534 * new intermediate node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * RTN_RTINFO will
536 * be off since that an address that chooses one of
537 * the branches would not match less specific routes
538 * in the other branch
539 */
540
541 in->fn_bit = bit;
542
543 in->parent = pn;
544 in->leaf = fn->leaf;
545 atomic_inc(&in->leaf->rt6i_ref);
546
547 in->fn_sernum = sernum;
548
549 /* update parent pointer */
550 if (dir)
551 pn->right = in;
552 else
553 pn->left = in;
554
555 ln->fn_bit = plen;
556
557 ln->parent = in;
558 fn->parent = in;
559
560 ln->fn_sernum = sernum;
561
562 if (addr_bit_set(addr, bit)) {
563 in->right = ln;
564 in->left = fn;
565 } else {
566 in->left = ln;
567 in->right = fn;
568 }
569 } else { /* plen <= bit */
570
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900571 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * (new leaf node)[ln]
573 * / \
574 * (old node)[fn] NULL
575 */
576
577 ln = node_alloc();
578
579 if (ln == NULL)
580 return NULL;
581
582 ln->fn_bit = plen;
583
584 ln->parent = pn;
585
586 ln->fn_sernum = sernum;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (dir)
589 pn->right = ln;
590 else
591 pn->left = ln;
592
593 if (addr_bit_set(&key->addr, plen))
594 ln->right = fn;
595 else
596 ln->left = fn;
597
598 fn->parent = ln;
599 }
600 return ln;
601}
602
603/*
604 * Insert routing information in a node.
605 */
606
607static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Thomas Graf86872cb2006-08-22 00:01:08 -0700608 struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct rt6_info *iter = NULL;
611 struct rt6_info **ins;
612
613 ins = &fn->leaf;
614
Eric Dumazet7cc48262007-02-09 16:22:57 -0800615 for (iter = fn->leaf; iter; iter=iter->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /*
617 * Search for duplicates
618 */
619
620 if (iter->rt6i_metric == rt->rt6i_metric) {
621 /*
622 * Same priority level
623 */
624
625 if (iter->rt6i_dev == rt->rt6i_dev &&
626 iter->rt6i_idev == rt->rt6i_idev &&
627 ipv6_addr_equal(&iter->rt6i_gateway,
628 &rt->rt6i_gateway)) {
629 if (!(iter->rt6i_flags&RTF_EXPIRES))
630 return -EEXIST;
631 iter->rt6i_expires = rt->rt6i_expires;
632 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
633 iter->rt6i_flags &= ~RTF_EXPIRES;
634 iter->rt6i_expires = 0;
635 }
636 return -EEXIST;
637 }
638 }
639
640 if (iter->rt6i_metric > rt->rt6i_metric)
641 break;
642
Eric Dumazet7cc48262007-02-09 16:22:57 -0800643 ins = &iter->u.dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
David S. Millerf11e6652007-03-24 20:36:25 -0700646 /* Reset round-robin state, if necessary */
647 if (ins == &fn->leaf)
648 fn->rr_ptr = NULL;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /*
651 * insert node
652 */
653
Eric Dumazet7cc48262007-02-09 16:22:57 -0800654 rt->u.dst.rt6_next = iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *ins = rt;
656 rt->rt6i_node = fn;
657 atomic_inc(&rt->rt6i_ref);
Thomas Graf86872cb2006-08-22 00:01:08 -0700658 inet6_rt_notify(RTM_NEWROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 rt6_stats.fib_rt_entries++;
660
661 if ((fn->fn_flags & RTN_RTINFO) == 0) {
662 rt6_stats.fib_route_nodes++;
663 fn->fn_flags |= RTN_RTINFO;
664 }
665
666 return 0;
667}
668
669static __inline__ void fib6_start_gc(struct rt6_info *rt)
670{
671 if (ip6_fib_timer.expires == 0 &&
672 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
Daniel Lezcano49905092008-01-10 03:01:01 -0800673 mod_timer(&ip6_fib_timer, jiffies +
674 init_net.ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677void fib6_force_start_gc(void)
678{
679 if (ip6_fib_timer.expires == 0)
Daniel Lezcano49905092008-01-10 03:01:01 -0800680 mod_timer(&ip6_fib_timer, jiffies +
681 init_net.ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684/*
685 * Add routing information to the routing tree.
686 * <destination addr>/<source addr>
687 * with source addr info in sub-trees
688 */
689
Thomas Graf86872cb2006-08-22 00:01:08 -0700690int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700692 struct fib6_node *fn, *pn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int err = -ENOMEM;
694
695 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
696 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
697
698 if (fn == NULL)
699 goto out;
700
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700701 pn = fn;
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703#ifdef CONFIG_IPV6_SUBTREES
704 if (rt->rt6i_src.plen) {
705 struct fib6_node *sn;
706
707 if (fn->subtree == NULL) {
708 struct fib6_node *sfn;
709
710 /*
711 * Create subtree.
712 *
713 * fn[main tree]
714 * |
715 * sfn[subtree root]
716 * \
717 * sn[new leaf node]
718 */
719
720 /* Create subtree root node */
721 sfn = node_alloc();
722 if (sfn == NULL)
723 goto st_failure;
724
725 sfn->leaf = &ip6_null_entry;
726 atomic_inc(&ip6_null_entry.rt6i_ref);
727 sfn->fn_flags = RTN_ROOT;
728 sfn->fn_sernum = fib6_new_sernum();
729
730 /* Now add the first leaf node to new subtree */
731
732 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
733 sizeof(struct in6_addr), rt->rt6i_src.plen,
734 offsetof(struct rt6_info, rt6i_src));
735
736 if (sn == NULL) {
737 /* If it is failed, discard just allocated
738 root, and then (in st_failure) stale node
739 in main tree.
740 */
741 node_free(sfn);
742 goto st_failure;
743 }
744
745 /* Now link new subtree to main tree */
746 sfn->parent = fn;
747 fn->subtree = sfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 } else {
749 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
750 sizeof(struct in6_addr), rt->rt6i_src.plen,
751 offsetof(struct rt6_info, rt6i_src));
752
753 if (sn == NULL)
754 goto st_failure;
755 }
756
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700757 if (fn->leaf == NULL) {
758 fn->leaf = rt;
759 atomic_inc(&rt->rt6i_ref);
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 fn = sn;
762 }
763#endif
764
Thomas Graf86872cb2006-08-22 00:01:08 -0700765 err = fib6_add_rt2node(fn, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (err == 0) {
768 fib6_start_gc(rt);
769 if (!(rt->rt6i_flags&RTF_CACHE))
YOSHIFUJI Hideaki2285adc2006-08-23 17:20:54 -0700770 fib6_prune_clones(pn, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773out:
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700774 if (err) {
775#ifdef CONFIG_IPV6_SUBTREES
776 /*
777 * If fib6_add_1 has cleared the old leaf pointer in the
778 * super-tree leaf node we have to find a new one for it.
779 */
780 if (pn != fn && !pn->leaf && !(pn->fn_flags & RTN_RTINFO)) {
781 pn->leaf = fib6_find_prefix(pn);
782#if RT6_DEBUG >= 2
783 if (!pn->leaf) {
784 BUG_TRAP(pn->leaf != NULL);
785 pn->leaf = &ip6_null_entry;
786 }
787#endif
788 atomic_inc(&pn->leaf->rt6i_ref);
789 }
790#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 dst_free(&rt->u.dst);
YOSHIFUJI Hideaki66729e12006-08-23 17:20:34 -0700792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return err;
794
795#ifdef CONFIG_IPV6_SUBTREES
796 /* Subtree creation failed, probably main tree node
797 is orphan. If it is, shoot it.
798 */
799st_failure:
800 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
801 fib6_repair_tree(fn);
802 dst_free(&rt->u.dst);
803 return err;
804#endif
805}
806
807/*
808 * Routing tree lookup
809 *
810 */
811
812struct lookup_args {
813 int offset; /* key offset on rt6_info */
814 struct in6_addr *addr; /* search key */
815};
816
817static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
818 struct lookup_args *args)
819{
820 struct fib6_node *fn;
Al Viroe69a4ad2006-11-14 20:56:00 -0800821 __be32 dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700823 if (unlikely(args->offset == 0))
824 return NULL;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /*
827 * Descend on a tree
828 */
829
830 fn = root;
831
832 for (;;) {
833 struct fib6_node *next;
834
835 dir = addr_bit_set(args->addr, fn->fn_bit);
836
837 next = dir ? fn->right : fn->left;
838
839 if (next) {
840 fn = next;
841 continue;
842 }
843
844 break;
845 }
846
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700847 while(fn) {
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -0700848 if (FIB6_SUBTREE(fn) || fn->fn_flags & RTN_RTINFO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct rt6key *key;
850
851 key = (struct rt6key *) ((u8 *) fn->leaf +
852 args->offset);
853
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700854 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
855#ifdef CONFIG_IPV6_SUBTREES
856 if (fn->subtree)
857 fn = fib6_lookup_1(fn->subtree, args + 1);
858#endif
859 if (!fn || fn->fn_flags & RTN_RTINFO)
860 return fn;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700864 if (fn->fn_flags & RTN_ROOT)
865 break;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 fn = fn->parent;
868 }
869
870 return NULL;
871}
872
873struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
874 struct in6_addr *saddr)
875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct fib6_node *fn;
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700877 struct lookup_args args[] = {
878 {
879 .offset = offsetof(struct rt6_info, rt6i_dst),
880 .addr = daddr,
881 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700883 {
884 .offset = offsetof(struct rt6_info, rt6i_src),
885 .addr = saddr,
886 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#endif
YOSHIFUJI Hideaki825e2882006-08-23 17:21:29 -0700888 {
889 .offset = 0, /* sentinel */
890 }
891 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
YOSHIFUJI Hideakifefc2a62006-08-23 17:21:50 -0700893 fn = fib6_lookup_1(root, daddr ? args : args + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
896 fn = root;
897
898 return fn;
899}
900
901/*
902 * Get node with specified destination prefix (and source prefix,
903 * if subtrees are used)
904 */
905
906
907static struct fib6_node * fib6_locate_1(struct fib6_node *root,
908 struct in6_addr *addr,
909 int plen, int offset)
910{
911 struct fib6_node *fn;
912
913 for (fn = root; fn ; ) {
914 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
915
916 /*
917 * Prefix match
918 */
919 if (plen < fn->fn_bit ||
920 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
921 return NULL;
922
923 if (plen == fn->fn_bit)
924 return fn;
925
926 /*
927 * We have more bits to go
928 */
929 if (addr_bit_set(addr, fn->fn_bit))
930 fn = fn->right;
931 else
932 fn = fn->left;
933 }
934 return NULL;
935}
936
937struct fib6_node * fib6_locate(struct fib6_node *root,
938 struct in6_addr *daddr, int dst_len,
939 struct in6_addr *saddr, int src_len)
940{
941 struct fib6_node *fn;
942
943 fn = fib6_locate_1(root, daddr, dst_len,
944 offsetof(struct rt6_info, rt6i_dst));
945
946#ifdef CONFIG_IPV6_SUBTREES
947 if (src_len) {
948 BUG_TRAP(saddr!=NULL);
YOSHIFUJI Hideaki3fc5e042006-08-23 17:21:12 -0700949 if (fn && fn->subtree)
950 fn = fib6_locate_1(fn->subtree, saddr, src_len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 offsetof(struct rt6_info, rt6i_src));
952 }
953#endif
954
955 if (fn && fn->fn_flags&RTN_RTINFO)
956 return fn;
957
958 return NULL;
959}
960
961
962/*
963 * Deletion
964 *
965 */
966
967static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
968{
969 if (fn->fn_flags&RTN_ROOT)
970 return &ip6_null_entry;
971
972 while(fn) {
973 if(fn->left)
974 return fn->left->leaf;
975
976 if(fn->right)
977 return fn->right->leaf;
978
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -0700979 fn = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 return NULL;
982}
983
984/*
985 * Called to trim the tree of intermediate nodes when possible. "fn"
986 * is the node we want to try and remove.
987 */
988
989static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
990{
991 int children;
992 int nstate;
993 struct fib6_node *child, *pn;
994 struct fib6_walker_t *w;
995 int iter = 0;
996
997 for (;;) {
998 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
999 iter++;
1000
1001 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
1002 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
1003 BUG_TRAP(fn->leaf==NULL);
1004
1005 children = 0;
1006 child = NULL;
1007 if (fn->right) child = fn->right, children |= 1;
1008 if (fn->left) child = fn->left, children |= 2;
1009
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001010 if (children == 3 || FIB6_SUBTREE(fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#ifdef CONFIG_IPV6_SUBTREES
1012 /* Subtree root (i.e. fn) may have one child */
1013 || (children && fn->fn_flags&RTN_ROOT)
1014#endif
1015 ) {
1016 fn->leaf = fib6_find_prefix(fn);
1017#if RT6_DEBUG >= 2
1018 if (fn->leaf==NULL) {
1019 BUG_TRAP(fn->leaf);
1020 fn->leaf = &ip6_null_entry;
1021 }
1022#endif
1023 atomic_inc(&fn->leaf->rt6i_ref);
1024 return fn->parent;
1025 }
1026
1027 pn = fn->parent;
1028#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001029 if (FIB6_SUBTREE(pn) == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 BUG_TRAP(fn->fn_flags&RTN_ROOT);
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001031 FIB6_SUBTREE(pn) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 nstate = FWS_L;
1033 } else {
1034 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
1035#endif
1036 if (pn->right == fn) pn->right = child;
1037 else if (pn->left == fn) pn->left = child;
1038#if RT6_DEBUG >= 2
1039 else BUG_TRAP(0);
1040#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;
1093
1094 RT6_TRACE("fib6_del_route\n");
1095
1096 /* Unlink it */
Eric Dumazet7cc48262007-02-09 16:22:57 -08001097 *rtp = rt->u.dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 rt->rt6i_node = NULL;
1099 rt6_stats.fib_rt_entries--;
1100 rt6_stats.fib_discarded_routes++;
1101
David S. Millerf11e6652007-03-24 20:36:25 -07001102 /* Reset round-robin state, if necessary */
1103 if (fn->rr_ptr == rt)
1104 fn->rr_ptr = NULL;
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* Adjust walkers */
1107 read_lock(&fib6_walker_lock);
1108 FOR_WALKERS(w) {
1109 if (w->state == FWS_C && w->leaf == rt) {
1110 RT6_TRACE("walker %p adjusted by delroute\n", w);
Eric Dumazet7cc48262007-02-09 16:22:57 -08001111 w->leaf = rt->u.dst.rt6_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (w->leaf == NULL)
1113 w->state = FWS_U;
1114 }
1115 }
1116 read_unlock(&fib6_walker_lock);
1117
Eric Dumazet7cc48262007-02-09 16:22:57 -08001118 rt->u.dst.rt6_next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /* If it was last route, expunge its radix tree node */
1121 if (fn->leaf == NULL) {
1122 fn->fn_flags &= ~RTN_RTINFO;
1123 rt6_stats.fib_route_nodes--;
1124 fn = fib6_repair_tree(fn);
1125 }
1126
1127 if (atomic_read(&rt->rt6i_ref) != 1) {
1128 /* This route is used as dummy address holder in some split
1129 * nodes. It is not leaked, but it still holds other resources,
1130 * which must be released in time. So, scan ascendant nodes
1131 * and replace dummy references to this route with references
1132 * to still alive ones.
1133 */
1134 while (fn) {
1135 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
1136 fn->leaf = fib6_find_prefix(fn);
1137 atomic_inc(&fn->leaf->rt6i_ref);
1138 rt6_release(rt);
1139 }
1140 fn = fn->parent;
1141 }
1142 /* No more references are possible at this point. */
Pavel Emelyanov2df96af2008-02-18 20:50:42 -08001143 BUG_ON(atomic_read(&rt->rt6i_ref) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145
Thomas Graf86872cb2006-08-22 00:01:08 -07001146 inet6_rt_notify(RTM_DELROUTE, rt, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 rt6_release(rt);
1148}
1149
Thomas Graf86872cb2006-08-22 00:01:08 -07001150int fib6_del(struct rt6_info *rt, struct nl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct fib6_node *fn = rt->rt6i_node;
1153 struct rt6_info **rtp;
1154
1155#if RT6_DEBUG >= 2
1156 if (rt->u.dst.obsolete>0) {
1157 BUG_TRAP(fn==NULL);
1158 return -ENOENT;
1159 }
1160#endif
1161 if (fn == NULL || rt == &ip6_null_entry)
1162 return -ENOENT;
1163
1164 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
1165
YOSHIFUJI Hideaki150730d2006-08-23 17:22:55 -07001166 if (!(rt->rt6i_flags&RTF_CACHE)) {
1167 struct fib6_node *pn = fn;
1168#ifdef CONFIG_IPV6_SUBTREES
1169 /* clones of this route might be in another subtree */
1170 if (rt->rt6i_src.plen) {
1171 while (!(pn->fn_flags&RTN_ROOT))
1172 pn = pn->parent;
1173 pn = pn->parent;
1174 }
1175#endif
1176 fib6_prune_clones(pn, rt);
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /*
1180 * Walk the leaf entries looking for ourself
1181 */
1182
Eric Dumazet7cc48262007-02-09 16:22:57 -08001183 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (*rtp == rt) {
Thomas Graf86872cb2006-08-22 00:01:08 -07001185 fib6_del_route(fn, rtp, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 0;
1187 }
1188 }
1189 return -ENOENT;
1190}
1191
1192/*
1193 * Tree traversal function.
1194 *
1195 * Certainly, it is not interrupt safe.
1196 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1197 * It means, that we can modify tree during walking
1198 * and use this function for garbage collection, clone pruning,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001199 * cleaning tree when a device goes down etc. etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 *
1201 * It guarantees that every node will be traversed,
1202 * and that it will be traversed only once.
1203 *
1204 * Callback function w->func may return:
1205 * 0 -> continue walking.
1206 * positive value -> walking is suspended (used by tree dumps,
1207 * and probably by gc, if it will be split to several slices)
1208 * negative value -> terminate walking.
1209 *
1210 * The function itself returns:
1211 * 0 -> walk is complete.
1212 * >0 -> walk is incomplete (i.e. suspended)
1213 * <0 -> walk is terminated by an error.
1214 */
1215
Adrian Bunk90d41122006-08-14 23:49:16 -07001216static int fib6_walk_continue(struct fib6_walker_t *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct fib6_node *fn, *pn;
1219
1220 for (;;) {
1221 fn = w->node;
1222 if (fn == NULL)
1223 return 0;
1224
1225 if (w->prune && fn != w->root &&
1226 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1227 w->state = FWS_C;
1228 w->leaf = fn->leaf;
1229 }
1230 switch (w->state) {
1231#ifdef CONFIG_IPV6_SUBTREES
1232 case FWS_S:
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001233 if (FIB6_SUBTREE(fn)) {
1234 w->node = FIB6_SUBTREE(fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 continue;
1236 }
1237 w->state = FWS_L;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001238#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 case FWS_L:
1240 if (fn->left) {
1241 w->node = fn->left;
1242 w->state = FWS_INIT;
1243 continue;
1244 }
1245 w->state = FWS_R;
1246 case FWS_R:
1247 if (fn->right) {
1248 w->node = fn->right;
1249 w->state = FWS_INIT;
1250 continue;
1251 }
1252 w->state = FWS_C;
1253 w->leaf = fn->leaf;
1254 case FWS_C:
1255 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1256 int err = w->func(w);
1257 if (err)
1258 return err;
1259 continue;
1260 }
1261 w->state = FWS_U;
1262 case FWS_U:
1263 if (fn == w->root)
1264 return 0;
1265 pn = fn->parent;
1266 w->node = pn;
1267#ifdef CONFIG_IPV6_SUBTREES
YOSHIFUJI Hideaki7fc33162006-08-23 17:22:24 -07001268 if (FIB6_SUBTREE(pn) == fn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1270 w->state = FWS_L;
1271 continue;
1272 }
1273#endif
1274 if (pn->left == fn) {
1275 w->state = FWS_R;
1276 continue;
1277 }
1278 if (pn->right == fn) {
1279 w->state = FWS_C;
1280 w->leaf = w->node->leaf;
1281 continue;
1282 }
1283#if RT6_DEBUG >= 2
1284 BUG_TRAP(0);
1285#endif
1286 }
1287 }
1288}
1289
Adrian Bunk90d41122006-08-14 23:49:16 -07001290static int fib6_walk(struct fib6_walker_t *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 int res;
1293
1294 w->state = FWS_INIT;
1295 w->node = w->root;
1296
1297 fib6_walker_link(w);
1298 res = fib6_walk_continue(w);
1299 if (res <= 0)
1300 fib6_walker_unlink(w);
1301 return res;
1302}
1303
1304static int fib6_clean_node(struct fib6_walker_t *w)
1305{
Denis V. Lunev4d1169c2008-01-10 03:26:13 -08001306 struct nl_info info = {
1307 .nl_net = &init_net,
1308 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int res;
1310 struct rt6_info *rt;
Benjamin Thery0a8891a2007-10-08 20:39:36 -07001311 struct fib6_cleaner_t *c = container_of(w, struct fib6_cleaner_t, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Eric Dumazet7cc48262007-02-09 16:22:57 -08001313 for (rt = w->leaf; rt; rt = rt->u.dst.rt6_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 res = c->func(rt, c->arg);
1315 if (res < 0) {
1316 w->leaf = rt;
Denis V. Lunev528c4ce2007-12-13 09:45:12 -08001317 res = fib6_del(rt, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (res) {
1319#if RT6_DEBUG >= 2
1320 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1321#endif
1322 continue;
1323 }
1324 return 0;
1325 }
1326 BUG_TRAP(res==0);
1327 }
1328 w->leaf = rt;
1329 return 0;
1330}
1331
1332/*
1333 * Convenient frontend to tree walker.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001334 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 * func is called on each route.
1336 * It may return -1 -> delete this route.
1337 * 0 -> continue walking
1338 *
1339 * prune==1 -> only immediate children of node (certainly,
1340 * ignoring pure split nodes) will be scanned.
1341 */
1342
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001343static void fib6_clean_tree(struct fib6_node *root,
1344 int (*func)(struct rt6_info *, void *arg),
1345 int prune, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 struct fib6_cleaner_t c;
1348
1349 c.w.root = root;
1350 c.w.func = fib6_clean_node;
1351 c.w.prune = prune;
1352 c.func = func;
1353 c.arg = arg;
1354
1355 fib6_walk(&c.w);
1356}
1357
Thomas Grafc71099a2006-08-04 23:20:06 -07001358void fib6_clean_all(int (*func)(struct rt6_info *, void *arg),
1359 int prune, void *arg)
1360{
Thomas Grafc71099a2006-08-04 23:20:06 -07001361 struct fib6_table *table;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001362 struct hlist_node *node;
1363 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001364
Patrick McHardy1b43af52006-08-10 23:11:17 -07001365 rcu_read_lock();
1366 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1367 hlist_for_each_entry_rcu(table, node, &fib_table_hash[h],
1368 tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001369 write_lock_bh(&table->tb6_lock);
1370 fib6_clean_tree(&table->tb6_root, func, prune, arg);
1371 write_unlock_bh(&table->tb6_lock);
1372 }
1373 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001374 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1378{
1379 if (rt->rt6i_flags & RTF_CACHE) {
1380 RT6_TRACE("pruning clone %p\n", rt);
1381 return -1;
1382 }
1383
1384 return 0;
1385}
1386
1387static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1388{
1389 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1390}
1391
1392/*
1393 * Garbage collection
1394 */
1395
1396static struct fib6_gc_args
1397{
1398 int timeout;
1399 int more;
1400} gc_args;
1401
1402static int fib6_age(struct rt6_info *rt, void *arg)
1403{
1404 unsigned long now = jiffies;
1405
1406 /*
1407 * check addrconf expiration here.
1408 * Routes are expired even if they are in use.
1409 *
1410 * Also age clones. Note, that clones are aged out
1411 * only if they are not in use now.
1412 */
1413
1414 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1415 if (time_after(now, rt->rt6i_expires)) {
1416 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -1;
1418 }
1419 gc_args.more++;
1420 } else if (rt->rt6i_flags & RTF_CACHE) {
1421 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1422 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1423 RT6_TRACE("aging clone %p\n", rt);
1424 return -1;
1425 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1426 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1427 RT6_TRACE("purging route %p via non-router but gateway\n",
1428 rt);
1429 return -1;
1430 }
1431 gc_args.more++;
1432 }
1433
1434 return 0;
1435}
1436
1437static DEFINE_SPINLOCK(fib6_gc_lock);
1438
1439void fib6_run_gc(unsigned long dummy)
1440{
1441 if (dummy != ~0UL) {
1442 spin_lock_bh(&fib6_gc_lock);
Daniel Lezcano49905092008-01-10 03:01:01 -08001443 gc_args.timeout = dummy ? (int)dummy :
1444 init_net.ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 } else {
1446 local_bh_disable();
1447 if (!spin_trylock(&fib6_gc_lock)) {
1448 mod_timer(&ip6_fib_timer, jiffies + HZ);
1449 local_bh_enable();
1450 return;
1451 }
Daniel Lezcano49905092008-01-10 03:01:01 -08001452 gc_args.timeout = init_net.ipv6.sysctl.ip6_rt_gc_interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 gc_args.more = 0;
1455
YOSHIFUJI Hideaki3b009442007-12-06 16:11:48 -08001456 icmp6_dst_gc(&gc_args.more);
Thomas Grafc71099a2006-08-04 23:20:06 -07001457 fib6_clean_all(fib6_age, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 if (gc_args.more)
Daniel Lezcano49905092008-01-10 03:01:01 -08001460 mod_timer(&ip6_fib_timer, jiffies +
1461 init_net.ipv6.sysctl.ip6_rt_gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 else {
1463 del_timer(&ip6_fib_timer);
1464 ip6_fib_timer.expires = 0;
1465 }
1466 spin_unlock_bh(&fib6_gc_lock);
1467}
1468
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001469int __init fib6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001471 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1473 sizeof(struct fib6_node),
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001474 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001475 NULL);
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001476 if (!fib6_node_kmem)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001477 goto out;
1478
1479 fib_table_hash = kzalloc(sizeof(*fib_table_hash)*FIB_TABLE_HASHSZ,
1480 GFP_KERNEL);
1481 if (!fib_table_hash)
1482 goto out_kmem_cache_create;
1483
1484 fib6_main_tbl = kzalloc(sizeof(*fib6_main_tbl), GFP_KERNEL);
1485 if (!fib6_main_tbl)
1486 goto out_fib_table_hash;
1487
1488 fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
1489 fib6_main_tbl->tb6_root.leaf = &ip6_null_entry;
1490 fib6_main_tbl->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1491
1492#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1493 fib6_local_tbl = kzalloc(sizeof(*fib6_local_tbl), GFP_KERNEL);
1494 if (!fib6_local_tbl)
1495 goto out_fib6_main_tbl;
1496
1497 fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
1498 fib6_local_tbl->tb6_root.leaf = &ip6_null_entry;
1499 fib6_local_tbl->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
1500#endif
Daniel Lezcanof845ab62007-12-07 00:45:16 -08001501
Thomas Grafc71099a2006-08-04 23:20:06 -07001502 fib6_tables_init();
Thomas Grafc127ea22007-03-22 11:58:32 -07001503
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001504 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib);
1505 if (ret)
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001506 goto out_fib6_local_tbl;
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001507out:
1508 return ret;
1509
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001510out_fib6_local_tbl:
1511#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1512 kfree(fib6_local_tbl);
1513out_fib6_main_tbl:
1514#endif
1515 kfree(fib6_main_tbl);
1516out_fib_table_hash:
1517 kfree(fib_table_hash);
Daniel Lezcanod63bddb2007-12-07 00:40:34 -08001518out_kmem_cache_create:
1519 kmem_cache_destroy(fib6_node_kmem);
1520 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523void fib6_gc_cleanup(void)
1524{
1525 del_timer(&ip6_fib_timer);
Daniel Lezcanoe0b855902008-03-03 23:24:31 -08001526#ifdef CONFIG_IPV6_MULTIPLE_TABLES
1527 kfree(fib6_local_tbl);
1528#endif
1529 kfree(fib6_main_tbl);
1530 kfree(fib_table_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 kmem_cache_destroy(fib6_node_kmem);
1532}