blob: bececbe9dd2cc197b93da57108aa1aa7f9ee83a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux INET6 implementation
3 * Forwarding Information Database
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
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.
21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/types.h>
24#include <linux/net.h>
25#include <linux/route.h>
26#include <linux/netdevice.h>
27#include <linux/in6.h>
28#include <linux/init.h>
Thomas Grafc71099a2006-08-04 23:20:06 -070029#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#ifdef CONFIG_PROC_FS
32#include <linux/proc_fs.h>
33#endif
34
35#include <net/ipv6.h>
36#include <net/ndisc.h>
37#include <net/addrconf.h>
38
39#include <net/ip6_fib.h>
40#include <net/ip6_route.h>
41
42#define RT6_DEBUG 2
43
44#if RT6_DEBUG >= 3
45#define RT6_TRACE(x...) printk(KERN_DEBUG x)
46#else
47#define RT6_TRACE(x...) do { ; } while (0)
48#endif
49
50struct rt6_statistics rt6_stats;
51
Eric Dumazetba899662005-08-26 12:05:31 -070052static kmem_cache_t * fib6_node_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54enum fib_walk_state_t
55{
56#ifdef CONFIG_IPV6_SUBTREES
57 FWS_S,
58#endif
59 FWS_L,
60 FWS_R,
61 FWS_C,
62 FWS_U
63};
64
65struct fib6_cleaner_t
66{
67 struct fib6_walker_t w;
68 int (*func)(struct rt6_info *, void *arg);
69 void *arg;
70};
71
72DEFINE_RWLOCK(fib6_walker_lock);
73
74
75#ifdef CONFIG_IPV6_SUBTREES
76#define FWS_INIT FWS_S
77#define SUBTREE(fn) ((fn)->subtree)
78#else
79#define FWS_INIT FWS_L
80#define SUBTREE(fn) NULL
81#endif
82
83static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
84static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
85
86/*
87 * A routing update causes an increase of the serial number on the
88 * affected subtree. This allows for cached routes to be asynchronously
89 * tested when modifications are made to the destination cache as a
90 * result of redirects, path MTU changes, etc.
91 */
92
93static __u32 rt_sernum;
94
Ingo Molnar8d06afa2005-09-09 13:10:40 -070095static DEFINE_TIMER(ip6_fib_timer, fib6_run_gc, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97struct fib6_walker_t fib6_walker_list = {
98 .prev = &fib6_walker_list,
99 .next = &fib6_walker_list,
100};
101
102#define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
103
104static __inline__ u32 fib6_new_sernum(void)
105{
106 u32 n = ++rt_sernum;
107 if ((__s32)n <= 0)
108 rt_sernum = n = 1;
109 return n;
110}
111
112/*
113 * Auxiliary address test functions for the radix tree.
114 *
115 * These assume a 32bit processor (although it will work on
116 * 64bit processors)
117 */
118
119/*
120 * test bit
121 */
122
123static __inline__ int addr_bit_set(void *token, int fn_bit)
124{
125 __u32 *addr = token;
126
127 return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static __inline__ struct fib6_node * node_alloc(void)
131{
132 struct fib6_node *fn;
133
134 if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
135 memset(fn, 0, sizeof(struct fib6_node));
136
137 return fn;
138}
139
140static __inline__ void node_free(struct fib6_node * fn)
141{
142 kmem_cache_free(fib6_node_kmem, fn);
143}
144
145static __inline__ void rt6_release(struct rt6_info *rt)
146{
147 if (atomic_dec_and_test(&rt->rt6i_ref))
148 dst_free(&rt->u.dst);
149}
150
Thomas Grafc71099a2006-08-04 23:20:06 -0700151static struct fib6_table fib6_main_tbl = {
152 .tb6_id = RT6_TABLE_MAIN,
153 .tb6_lock = RW_LOCK_UNLOCKED,
154 .tb6_root = {
155 .leaf = &ip6_null_entry,
156 .fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
157 },
158};
159
160#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Patrick McHardy1b43af52006-08-10 23:11:17 -0700161#define FIB_TABLE_HASHSZ 256
162#else
163#define FIB_TABLE_HASHSZ 1
164#endif
165static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
Thomas Grafc71099a2006-08-04 23:20:06 -0700166
Patrick McHardy1b43af52006-08-10 23:11:17 -0700167static void fib6_link_table(struct fib6_table *tb)
168{
169 unsigned int h;
170
171 h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1);
172
173 /*
174 * No protection necessary, this is the only list mutatation
175 * operation, tables never disappear once they exist.
176 */
177 hlist_add_head_rcu(&tb->tb6_hlist, &fib_table_hash[h]);
178}
179
180#ifdef CONFIG_IPV6_MULTIPLE_TABLES
Thomas Graf101367c2006-08-04 03:39:02 -0700181static struct fib6_table fib6_local_tbl = {
182 .tb6_id = RT6_TABLE_LOCAL,
183 .tb6_lock = RW_LOCK_UNLOCKED,
184 .tb6_root = {
185 .leaf = &ip6_null_entry,
186 .fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
187 },
188};
189
Thomas Grafc71099a2006-08-04 23:20:06 -0700190static struct fib6_table *fib6_alloc_table(u32 id)
191{
192 struct fib6_table *table;
193
194 table = kzalloc(sizeof(*table), GFP_ATOMIC);
195 if (table != NULL) {
196 table->tb6_id = id;
197 table->tb6_lock = RW_LOCK_UNLOCKED;
198 table->tb6_root.leaf = &ip6_null_entry;
199 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
200 }
201
202 return table;
203}
204
Thomas Grafc71099a2006-08-04 23:20:06 -0700205struct fib6_table *fib6_new_table(u32 id)
206{
207 struct fib6_table *tb;
208
209 if (id == 0)
210 id = RT6_TABLE_MAIN;
211 tb = fib6_get_table(id);
212 if (tb)
213 return tb;
214
215 tb = fib6_alloc_table(id);
216 if (tb != NULL)
217 fib6_link_table(tb);
218
219 return tb;
220}
221
222struct fib6_table *fib6_get_table(u32 id)
223{
224 struct fib6_table *tb;
225 struct hlist_node *node;
226 unsigned int h;
227
228 if (id == 0)
229 id = RT6_TABLE_MAIN;
230 h = id & (FIB_TABLE_HASHSZ - 1);
231 rcu_read_lock();
232 hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb6_hlist) {
233 if (tb->tb6_id == id) {
234 rcu_read_unlock();
235 return tb;
236 }
237 }
238 rcu_read_unlock();
239
240 return NULL;
241}
242
Thomas Grafc71099a2006-08-04 23:20:06 -0700243static void __init fib6_tables_init(void)
244{
245 fib6_link_table(&fib6_main_tbl);
Thomas Graf101367c2006-08-04 03:39:02 -0700246 fib6_link_table(&fib6_local_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700247}
248
249#else
250
251struct fib6_table *fib6_new_table(u32 id)
252{
253 return fib6_get_table(id);
254}
255
256struct fib6_table *fib6_get_table(u32 id)
257{
258 return &fib6_main_tbl;
259}
260
261struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
262 pol_lookup_t lookup)
263{
264 return (struct dst_entry *) lookup(&fib6_main_tbl, fl, flags);
265}
266
267static void __init fib6_tables_init(void)
268{
Patrick McHardy1b43af52006-08-10 23:11:17 -0700269 fib6_link_table(&fib6_main_tbl);
Thomas Grafc71099a2006-08-04 23:20:06 -0700270}
271
272#endif
273
Patrick McHardy1b43af52006-08-10 23:11:17 -0700274static int fib6_dump_node(struct fib6_walker_t *w)
275{
276 int res;
277 struct rt6_info *rt;
278
279 for (rt = w->leaf; rt; rt = rt->u.next) {
280 res = rt6_dump_route(rt, w->args);
281 if (res < 0) {
282 /* Frame is full, suspend walking */
283 w->leaf = rt;
284 return 1;
285 }
286 BUG_TRAP(res!=0);
287 }
288 w->leaf = NULL;
289 return 0;
290}
291
292static void fib6_dump_end(struct netlink_callback *cb)
293{
294 struct fib6_walker_t *w = (void*)cb->args[2];
295
296 if (w) {
297 cb->args[2] = 0;
298 kfree(w);
299 }
300 cb->done = (void*)cb->args[3];
301 cb->args[1] = 3;
302}
303
304static int fib6_dump_done(struct netlink_callback *cb)
305{
306 fib6_dump_end(cb);
307 return cb->done ? cb->done(cb) : 0;
308}
309
310static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
311 struct netlink_callback *cb)
312{
313 struct fib6_walker_t *w;
314 int res;
315
316 w = (void *)cb->args[2];
317 w->root = &table->tb6_root;
318
319 if (cb->args[4] == 0) {
320 read_lock_bh(&table->tb6_lock);
321 res = fib6_walk(w);
322 read_unlock_bh(&table->tb6_lock);
323 if (res > 0)
324 cb->args[4] = 1;
325 } else {
326 read_lock_bh(&table->tb6_lock);
327 res = fib6_walk_continue(w);
328 read_unlock_bh(&table->tb6_lock);
329 if (res != 0) {
330 if (res < 0)
331 fib6_walker_unlink(w);
332 goto end;
333 }
334 fib6_walker_unlink(w);
335 cb->args[4] = 0;
336 }
337end:
338 return res;
339}
340
341int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
342{
343 unsigned int h, s_h;
344 unsigned int e = 0, s_e;
345 struct rt6_rtnl_dump_arg arg;
346 struct fib6_walker_t *w;
347 struct fib6_table *tb;
348 struct hlist_node *node;
349 int res = 0;
350
351 s_h = cb->args[0];
352 s_e = cb->args[1];
353
354 w = (void *)cb->args[2];
355 if (w == NULL) {
356 /* New dump:
357 *
358 * 1. hook callback destructor.
359 */
360 cb->args[3] = (long)cb->done;
361 cb->done = fib6_dump_done;
362
363 /*
364 * 2. allocate and initialize walker.
365 */
366 w = kzalloc(sizeof(*w), GFP_ATOMIC);
367 if (w == NULL)
368 return -ENOMEM;
369 w->func = fib6_dump_node;
370 cb->args[2] = (long)w;
371 }
372
373 arg.skb = skb;
374 arg.cb = cb;
375 w->args = &arg;
376
377 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
378 e = 0;
379 hlist_for_each_entry(tb, node, &fib_table_hash[h], tb6_hlist) {
380 if (e < s_e)
381 goto next;
382 res = fib6_dump_table(tb, skb, cb);
383 if (res != 0)
384 goto out;
385next:
386 e++;
387 }
388 }
389out:
390 cb->args[1] = e;
391 cb->args[0] = h;
392
393 res = res < 0 ? res : skb->len;
394 if (res <= 0)
395 fib6_dump_end(cb);
396 return res;
397}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399/*
400 * Routing Table
401 *
402 * return the appropriate node for a routing tree "add" operation
403 * by either creating and inserting or by returning an existing
404 * node.
405 */
406
407static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
408 int addrlen, int plen,
409 int offset)
410{
411 struct fib6_node *fn, *in, *ln;
412 struct fib6_node *pn = NULL;
413 struct rt6key *key;
414 int bit;
415 int dir = 0;
416 __u32 sernum = fib6_new_sernum();
417
418 RT6_TRACE("fib6_add_1\n");
419
420 /* insert node in tree */
421
422 fn = root;
423
424 do {
425 key = (struct rt6key *)((u8 *)fn->leaf + offset);
426
427 /*
428 * Prefix match
429 */
430 if (plen < fn->fn_bit ||
431 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
432 goto insert_above;
433
434 /*
435 * Exact match ?
436 */
437
438 if (plen == fn->fn_bit) {
439 /* clean up an intermediate node */
440 if ((fn->fn_flags & RTN_RTINFO) == 0) {
441 rt6_release(fn->leaf);
442 fn->leaf = NULL;
443 }
444
445 fn->fn_sernum = sernum;
446
447 return fn;
448 }
449
450 /*
451 * We have more bits to go
452 */
453
454 /* Try to walk down on tree. */
455 fn->fn_sernum = sernum;
456 dir = addr_bit_set(addr, fn->fn_bit);
457 pn = fn;
458 fn = dir ? fn->right: fn->left;
459 } while (fn);
460
461 /*
462 * We walked to the bottom of tree.
463 * Create new leaf node without children.
464 */
465
466 ln = node_alloc();
467
468 if (ln == NULL)
469 return NULL;
470 ln->fn_bit = plen;
471
472 ln->parent = pn;
473 ln->fn_sernum = sernum;
474
475 if (dir)
476 pn->right = ln;
477 else
478 pn->left = ln;
479
480 return ln;
481
482
483insert_above:
484 /*
485 * split since we don't have a common prefix anymore or
486 * we have a less significant route.
487 * we've to insert an intermediate node on the list
488 * this new node will point to the one we need to create
489 * and the current
490 */
491
492 pn = fn->parent;
493
494 /* find 1st bit in difference between the 2 addrs.
495
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800496 See comment in __ipv6_addr_diff: bit may be an invalid value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 but if it is >= plen, the value is ignored in any case.
498 */
499
YOSHIFUJI Hideaki971f3592005-11-08 09:37:56 -0800500 bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * (intermediate)[in]
504 * / \
505 * (new leaf node)[ln] (old node)[fn]
506 */
507 if (plen > bit) {
508 in = node_alloc();
509 ln = node_alloc();
510
511 if (in == NULL || ln == NULL) {
512 if (in)
513 node_free(in);
514 if (ln)
515 node_free(ln);
516 return NULL;
517 }
518
519 /*
520 * new intermediate node.
521 * RTN_RTINFO will
522 * be off since that an address that chooses one of
523 * the branches would not match less specific routes
524 * in the other branch
525 */
526
527 in->fn_bit = bit;
528
529 in->parent = pn;
530 in->leaf = fn->leaf;
531 atomic_inc(&in->leaf->rt6i_ref);
532
533 in->fn_sernum = sernum;
534
535 /* update parent pointer */
536 if (dir)
537 pn->right = in;
538 else
539 pn->left = in;
540
541 ln->fn_bit = plen;
542
543 ln->parent = in;
544 fn->parent = in;
545
546 ln->fn_sernum = sernum;
547
548 if (addr_bit_set(addr, bit)) {
549 in->right = ln;
550 in->left = fn;
551 } else {
552 in->left = ln;
553 in->right = fn;
554 }
555 } else { /* plen <= bit */
556
557 /*
558 * (new leaf node)[ln]
559 * / \
560 * (old node)[fn] NULL
561 */
562
563 ln = node_alloc();
564
565 if (ln == NULL)
566 return NULL;
567
568 ln->fn_bit = plen;
569
570 ln->parent = pn;
571
572 ln->fn_sernum = sernum;
573
574 if (dir)
575 pn->right = ln;
576 else
577 pn->left = ln;
578
579 if (addr_bit_set(&key->addr, plen))
580 ln->right = fn;
581 else
582 ln->left = fn;
583
584 fn->parent = ln;
585 }
586 return ln;
587}
588
589/*
590 * Insert routing information in a node.
591 */
592
593static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700594 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 struct rt6_info *iter = NULL;
597 struct rt6_info **ins;
598
599 ins = &fn->leaf;
600
601 if (fn->fn_flags&RTN_TL_ROOT &&
602 fn->leaf == &ip6_null_entry &&
603 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
604 fn->leaf = rt;
605 rt->u.next = NULL;
606 goto out;
607 }
608
609 for (iter = fn->leaf; iter; iter=iter->u.next) {
610 /*
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
637 ins = &iter->u.next;
638 }
639
640 /*
641 * insert node
642 */
643
644out:
645 rt->u.next = iter;
646 *ins = rt;
647 rt->rt6i_node = fn;
648 atomic_inc(&rt->rt6i_ref);
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700649 inet6_rt_notify(RTM_NEWROUTE, rt, nlh, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 rt6_stats.fib_rt_entries++;
651
652 if ((fn->fn_flags & RTN_RTINFO) == 0) {
653 rt6_stats.fib_route_nodes++;
654 fn->fn_flags |= RTN_RTINFO;
655 }
656
657 return 0;
658}
659
660static __inline__ void fib6_start_gc(struct rt6_info *rt)
661{
662 if (ip6_fib_timer.expires == 0 &&
663 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
664 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
665}
666
667void fib6_force_start_gc(void)
668{
669 if (ip6_fib_timer.expires == 0)
670 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
671}
672
673/*
674 * Add routing information to the routing tree.
675 * <destination addr>/<source addr>
676 * with source addr info in sub-trees
677 */
678
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700679int fib6_add(struct fib6_node *root, struct rt6_info *rt,
680 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct fib6_node *fn;
683 int err = -ENOMEM;
684
685 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
686 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
687
688 if (fn == NULL)
689 goto out;
690
691#ifdef CONFIG_IPV6_SUBTREES
692 if (rt->rt6i_src.plen) {
693 struct fib6_node *sn;
694
695 if (fn->subtree == NULL) {
696 struct fib6_node *sfn;
697
698 /*
699 * Create subtree.
700 *
701 * fn[main tree]
702 * |
703 * sfn[subtree root]
704 * \
705 * sn[new leaf node]
706 */
707
708 /* Create subtree root node */
709 sfn = node_alloc();
710 if (sfn == NULL)
711 goto st_failure;
712
713 sfn->leaf = &ip6_null_entry;
714 atomic_inc(&ip6_null_entry.rt6i_ref);
715 sfn->fn_flags = RTN_ROOT;
716 sfn->fn_sernum = fib6_new_sernum();
717
718 /* Now add the first leaf node to new subtree */
719
720 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
721 sizeof(struct in6_addr), rt->rt6i_src.plen,
722 offsetof(struct rt6_info, rt6i_src));
723
724 if (sn == NULL) {
725 /* If it is failed, discard just allocated
726 root, and then (in st_failure) stale node
727 in main tree.
728 */
729 node_free(sfn);
730 goto st_failure;
731 }
732
733 /* Now link new subtree to main tree */
734 sfn->parent = fn;
735 fn->subtree = sfn;
736 if (fn->leaf == NULL) {
737 fn->leaf = rt;
738 atomic_inc(&rt->rt6i_ref);
739 }
740 } else {
741 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
742 sizeof(struct in6_addr), rt->rt6i_src.plen,
743 offsetof(struct rt6_info, rt6i_src));
744
745 if (sn == NULL)
746 goto st_failure;
747 }
748
749 fn = sn;
750 }
751#endif
752
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700753 err = fib6_add_rt2node(fn, rt, nlh, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (err == 0) {
756 fib6_start_gc(rt);
757 if (!(rt->rt6i_flags&RTF_CACHE))
758 fib6_prune_clones(fn, rt);
759 }
760
761out:
762 if (err)
763 dst_free(&rt->u.dst);
764 return err;
765
766#ifdef CONFIG_IPV6_SUBTREES
767 /* Subtree creation failed, probably main tree node
768 is orphan. If it is, shoot it.
769 */
770st_failure:
771 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
772 fib6_repair_tree(fn);
773 dst_free(&rt->u.dst);
774 return err;
775#endif
776}
777
778/*
779 * Routing tree lookup
780 *
781 */
782
783struct lookup_args {
784 int offset; /* key offset on rt6_info */
785 struct in6_addr *addr; /* search key */
786};
787
788static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
789 struct lookup_args *args)
790{
791 struct fib6_node *fn;
792 int dir;
793
794 /*
795 * Descend on a tree
796 */
797
798 fn = root;
799
800 for (;;) {
801 struct fib6_node *next;
802
803 dir = addr_bit_set(args->addr, fn->fn_bit);
804
805 next = dir ? fn->right : fn->left;
806
807 if (next) {
808 fn = next;
809 continue;
810 }
811
812 break;
813 }
814
815 while ((fn->fn_flags & RTN_ROOT) == 0) {
816#ifdef CONFIG_IPV6_SUBTREES
817 if (fn->subtree) {
818 struct fib6_node *st;
819 struct lookup_args *narg;
820
821 narg = args + 1;
822
823 if (narg->addr) {
824 st = fib6_lookup_1(fn->subtree, narg);
825
826 if (st && !(st->fn_flags & RTN_ROOT))
827 return st;
828 }
829 }
830#endif
831
832 if (fn->fn_flags & RTN_RTINFO) {
833 struct rt6key *key;
834
835 key = (struct rt6key *) ((u8 *) fn->leaf +
836 args->offset);
837
838 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
839 return fn;
840 }
841
842 fn = fn->parent;
843 }
844
845 return NULL;
846}
847
848struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
849 struct in6_addr *saddr)
850{
851 struct lookup_args args[2];
852 struct fib6_node *fn;
853
854 args[0].offset = offsetof(struct rt6_info, rt6i_dst);
855 args[0].addr = daddr;
856
857#ifdef CONFIG_IPV6_SUBTREES
858 args[1].offset = offsetof(struct rt6_info, rt6i_src);
859 args[1].addr = saddr;
860#endif
861
862 fn = fib6_lookup_1(root, args);
863
864 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
865 fn = root;
866
867 return fn;
868}
869
870/*
871 * Get node with specified destination prefix (and source prefix,
872 * if subtrees are used)
873 */
874
875
876static struct fib6_node * fib6_locate_1(struct fib6_node *root,
877 struct in6_addr *addr,
878 int plen, int offset)
879{
880 struct fib6_node *fn;
881
882 for (fn = root; fn ; ) {
883 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
884
885 /*
886 * Prefix match
887 */
888 if (plen < fn->fn_bit ||
889 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
890 return NULL;
891
892 if (plen == fn->fn_bit)
893 return fn;
894
895 /*
896 * We have more bits to go
897 */
898 if (addr_bit_set(addr, fn->fn_bit))
899 fn = fn->right;
900 else
901 fn = fn->left;
902 }
903 return NULL;
904}
905
906struct fib6_node * fib6_locate(struct fib6_node *root,
907 struct in6_addr *daddr, int dst_len,
908 struct in6_addr *saddr, int src_len)
909{
910 struct fib6_node *fn;
911
912 fn = fib6_locate_1(root, daddr, dst_len,
913 offsetof(struct rt6_info, rt6i_dst));
914
915#ifdef CONFIG_IPV6_SUBTREES
916 if (src_len) {
917 BUG_TRAP(saddr!=NULL);
918 if (fn == NULL)
919 fn = fn->subtree;
920 if (fn)
921 fn = fib6_locate_1(fn, saddr, src_len,
922 offsetof(struct rt6_info, rt6i_src));
923 }
924#endif
925
926 if (fn && fn->fn_flags&RTN_RTINFO)
927 return fn;
928
929 return NULL;
930}
931
932
933/*
934 * Deletion
935 *
936 */
937
938static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
939{
940 if (fn->fn_flags&RTN_ROOT)
941 return &ip6_null_entry;
942
943 while(fn) {
944 if(fn->left)
945 return fn->left->leaf;
946
947 if(fn->right)
948 return fn->right->leaf;
949
950 fn = SUBTREE(fn);
951 }
952 return NULL;
953}
954
955/*
956 * Called to trim the tree of intermediate nodes when possible. "fn"
957 * is the node we want to try and remove.
958 */
959
960static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
961{
962 int children;
963 int nstate;
964 struct fib6_node *child, *pn;
965 struct fib6_walker_t *w;
966 int iter = 0;
967
968 for (;;) {
969 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
970 iter++;
971
972 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
973 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
974 BUG_TRAP(fn->leaf==NULL);
975
976 children = 0;
977 child = NULL;
978 if (fn->right) child = fn->right, children |= 1;
979 if (fn->left) child = fn->left, children |= 2;
980
981 if (children == 3 || SUBTREE(fn)
982#ifdef CONFIG_IPV6_SUBTREES
983 /* Subtree root (i.e. fn) may have one child */
984 || (children && fn->fn_flags&RTN_ROOT)
985#endif
986 ) {
987 fn->leaf = fib6_find_prefix(fn);
988#if RT6_DEBUG >= 2
989 if (fn->leaf==NULL) {
990 BUG_TRAP(fn->leaf);
991 fn->leaf = &ip6_null_entry;
992 }
993#endif
994 atomic_inc(&fn->leaf->rt6i_ref);
995 return fn->parent;
996 }
997
998 pn = fn->parent;
999#ifdef CONFIG_IPV6_SUBTREES
1000 if (SUBTREE(pn) == fn) {
1001 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1002 SUBTREE(pn) = NULL;
1003 nstate = FWS_L;
1004 } else {
1005 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
1006#endif
1007 if (pn->right == fn) pn->right = child;
1008 else if (pn->left == fn) pn->left = child;
1009#if RT6_DEBUG >= 2
1010 else BUG_TRAP(0);
1011#endif
1012 if (child)
1013 child->parent = pn;
1014 nstate = FWS_R;
1015#ifdef CONFIG_IPV6_SUBTREES
1016 }
1017#endif
1018
1019 read_lock(&fib6_walker_lock);
1020 FOR_WALKERS(w) {
1021 if (child == NULL) {
1022 if (w->root == fn) {
1023 w->root = w->node = NULL;
1024 RT6_TRACE("W %p adjusted by delroot 1\n", w);
1025 } else if (w->node == fn) {
1026 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1027 w->node = pn;
1028 w->state = nstate;
1029 }
1030 } else {
1031 if (w->root == fn) {
1032 w->root = child;
1033 RT6_TRACE("W %p adjusted by delroot 2\n", w);
1034 }
1035 if (w->node == fn) {
1036 w->node = child;
1037 if (children&2) {
1038 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1039 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
1040 } else {
1041 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1042 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
1043 }
1044 }
1045 }
1046 }
1047 read_unlock(&fib6_walker_lock);
1048
1049 node_free(fn);
1050 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
1051 return pn;
1052
1053 rt6_release(pn->leaf);
1054 pn->leaf = NULL;
1055 fn = pn;
1056 }
1057}
1058
1059static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07001060 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 struct fib6_walker_t *w;
1063 struct rt6_info *rt = *rtp;
1064
1065 RT6_TRACE("fib6_del_route\n");
1066
1067 /* Unlink it */
1068 *rtp = rt->u.next;
1069 rt->rt6i_node = NULL;
1070 rt6_stats.fib_rt_entries--;
1071 rt6_stats.fib_discarded_routes++;
1072
1073 /* Adjust walkers */
1074 read_lock(&fib6_walker_lock);
1075 FOR_WALKERS(w) {
1076 if (w->state == FWS_C && w->leaf == rt) {
1077 RT6_TRACE("walker %p adjusted by delroute\n", w);
1078 w->leaf = rt->u.next;
1079 if (w->leaf == NULL)
1080 w->state = FWS_U;
1081 }
1082 }
1083 read_unlock(&fib6_walker_lock);
1084
1085 rt->u.next = NULL;
1086
1087 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
1088 fn->leaf = &ip6_null_entry;
1089
1090 /* If it was last route, expunge its radix tree node */
1091 if (fn->leaf == NULL) {
1092 fn->fn_flags &= ~RTN_RTINFO;
1093 rt6_stats.fib_route_nodes--;
1094 fn = fib6_repair_tree(fn);
1095 }
1096
1097 if (atomic_read(&rt->rt6i_ref) != 1) {
1098 /* This route is used as dummy address holder in some split
1099 * nodes. It is not leaked, but it still holds other resources,
1100 * which must be released in time. So, scan ascendant nodes
1101 * and replace dummy references to this route with references
1102 * to still alive ones.
1103 */
1104 while (fn) {
1105 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
1106 fn->leaf = fib6_find_prefix(fn);
1107 atomic_inc(&fn->leaf->rt6i_ref);
1108 rt6_release(rt);
1109 }
1110 fn = fn->parent;
1111 }
1112 /* No more references are possible at this point. */
1113 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
1114 }
1115
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07001116 inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 rt6_release(rt);
1118}
1119
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07001120int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 struct fib6_node *fn = rt->rt6i_node;
1123 struct rt6_info **rtp;
1124
1125#if RT6_DEBUG >= 2
1126 if (rt->u.dst.obsolete>0) {
1127 BUG_TRAP(fn==NULL);
1128 return -ENOENT;
1129 }
1130#endif
1131 if (fn == NULL || rt == &ip6_null_entry)
1132 return -ENOENT;
1133
1134 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
1135
1136 if (!(rt->rt6i_flags&RTF_CACHE))
1137 fib6_prune_clones(fn, rt);
1138
1139 /*
1140 * Walk the leaf entries looking for ourself
1141 */
1142
1143 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
1144 if (*rtp == rt) {
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07001145 fib6_del_route(fn, rtp, nlh, _rtattr, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147 }
1148 }
1149 return -ENOENT;
1150}
1151
1152/*
1153 * Tree traversal function.
1154 *
1155 * Certainly, it is not interrupt safe.
1156 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1157 * It means, that we can modify tree during walking
1158 * and use this function for garbage collection, clone pruning,
1159 * cleaning tree when a device goes down etc. etc.
1160 *
1161 * It guarantees that every node will be traversed,
1162 * and that it will be traversed only once.
1163 *
1164 * Callback function w->func may return:
1165 * 0 -> continue walking.
1166 * positive value -> walking is suspended (used by tree dumps,
1167 * and probably by gc, if it will be split to several slices)
1168 * negative value -> terminate walking.
1169 *
1170 * The function itself returns:
1171 * 0 -> walk is complete.
1172 * >0 -> walk is incomplete (i.e. suspended)
1173 * <0 -> walk is terminated by an error.
1174 */
1175
1176int fib6_walk_continue(struct fib6_walker_t *w)
1177{
1178 struct fib6_node *fn, *pn;
1179
1180 for (;;) {
1181 fn = w->node;
1182 if (fn == NULL)
1183 return 0;
1184
1185 if (w->prune && fn != w->root &&
1186 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1187 w->state = FWS_C;
1188 w->leaf = fn->leaf;
1189 }
1190 switch (w->state) {
1191#ifdef CONFIG_IPV6_SUBTREES
1192 case FWS_S:
1193 if (SUBTREE(fn)) {
1194 w->node = SUBTREE(fn);
1195 continue;
1196 }
1197 w->state = FWS_L;
1198#endif
1199 case FWS_L:
1200 if (fn->left) {
1201 w->node = fn->left;
1202 w->state = FWS_INIT;
1203 continue;
1204 }
1205 w->state = FWS_R;
1206 case FWS_R:
1207 if (fn->right) {
1208 w->node = fn->right;
1209 w->state = FWS_INIT;
1210 continue;
1211 }
1212 w->state = FWS_C;
1213 w->leaf = fn->leaf;
1214 case FWS_C:
1215 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1216 int err = w->func(w);
1217 if (err)
1218 return err;
1219 continue;
1220 }
1221 w->state = FWS_U;
1222 case FWS_U:
1223 if (fn == w->root)
1224 return 0;
1225 pn = fn->parent;
1226 w->node = pn;
1227#ifdef CONFIG_IPV6_SUBTREES
1228 if (SUBTREE(pn) == fn) {
1229 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1230 w->state = FWS_L;
1231 continue;
1232 }
1233#endif
1234 if (pn->left == fn) {
1235 w->state = FWS_R;
1236 continue;
1237 }
1238 if (pn->right == fn) {
1239 w->state = FWS_C;
1240 w->leaf = w->node->leaf;
1241 continue;
1242 }
1243#if RT6_DEBUG >= 2
1244 BUG_TRAP(0);
1245#endif
1246 }
1247 }
1248}
1249
1250int fib6_walk(struct fib6_walker_t *w)
1251{
1252 int res;
1253
1254 w->state = FWS_INIT;
1255 w->node = w->root;
1256
1257 fib6_walker_link(w);
1258 res = fib6_walk_continue(w);
1259 if (res <= 0)
1260 fib6_walker_unlink(w);
1261 return res;
1262}
1263
1264static int fib6_clean_node(struct fib6_walker_t *w)
1265{
1266 int res;
1267 struct rt6_info *rt;
1268 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1269
1270 for (rt = w->leaf; rt; rt = rt->u.next) {
1271 res = c->func(rt, c->arg);
1272 if (res < 0) {
1273 w->leaf = rt;
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07001274 res = fib6_del(rt, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (res) {
1276#if RT6_DEBUG >= 2
1277 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1278#endif
1279 continue;
1280 }
1281 return 0;
1282 }
1283 BUG_TRAP(res==0);
1284 }
1285 w->leaf = rt;
1286 return 0;
1287}
1288
1289/*
1290 * Convenient frontend to tree walker.
1291 *
1292 * func is called on each route.
1293 * It may return -1 -> delete this route.
1294 * 0 -> continue walking
1295 *
1296 * prune==1 -> only immediate children of node (certainly,
1297 * ignoring pure split nodes) will be scanned.
1298 */
1299
Adrian Bunk8ce11e62006-08-07 21:50:48 -07001300static void fib6_clean_tree(struct fib6_node *root,
1301 int (*func)(struct rt6_info *, void *arg),
1302 int prune, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct fib6_cleaner_t c;
1305
1306 c.w.root = root;
1307 c.w.func = fib6_clean_node;
1308 c.w.prune = prune;
1309 c.func = func;
1310 c.arg = arg;
1311
1312 fib6_walk(&c.w);
1313}
1314
Thomas Grafc71099a2006-08-04 23:20:06 -07001315void fib6_clean_all(int (*func)(struct rt6_info *, void *arg),
1316 int prune, void *arg)
1317{
Thomas Grafc71099a2006-08-04 23:20:06 -07001318 struct fib6_table *table;
Patrick McHardy1b43af52006-08-10 23:11:17 -07001319 struct hlist_node *node;
1320 unsigned int h;
Thomas Grafc71099a2006-08-04 23:20:06 -07001321
Patrick McHardy1b43af52006-08-10 23:11:17 -07001322 rcu_read_lock();
1323 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1324 hlist_for_each_entry_rcu(table, node, &fib_table_hash[h],
1325 tb6_hlist) {
Thomas Grafc71099a2006-08-04 23:20:06 -07001326 write_lock_bh(&table->tb6_lock);
1327 fib6_clean_tree(&table->tb6_root, func, prune, arg);
1328 write_unlock_bh(&table->tb6_lock);
1329 }
1330 }
Patrick McHardy1b43af52006-08-10 23:11:17 -07001331 rcu_read_unlock();
Thomas Grafc71099a2006-08-04 23:20:06 -07001332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1335{
1336 if (rt->rt6i_flags & RTF_CACHE) {
1337 RT6_TRACE("pruning clone %p\n", rt);
1338 return -1;
1339 }
1340
1341 return 0;
1342}
1343
1344static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1345{
1346 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1347}
1348
1349/*
1350 * Garbage collection
1351 */
1352
1353static struct fib6_gc_args
1354{
1355 int timeout;
1356 int more;
1357} gc_args;
1358
1359static int fib6_age(struct rt6_info *rt, void *arg)
1360{
1361 unsigned long now = jiffies;
1362
1363 /*
1364 * check addrconf expiration here.
1365 * Routes are expired even if they are in use.
1366 *
1367 * Also age clones. Note, that clones are aged out
1368 * only if they are not in use now.
1369 */
1370
1371 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1372 if (time_after(now, rt->rt6i_expires)) {
1373 RT6_TRACE("expiring %p\n", rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -1;
1375 }
1376 gc_args.more++;
1377 } else if (rt->rt6i_flags & RTF_CACHE) {
1378 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1379 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1380 RT6_TRACE("aging clone %p\n", rt);
1381 return -1;
1382 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1383 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1384 RT6_TRACE("purging route %p via non-router but gateway\n",
1385 rt);
1386 return -1;
1387 }
1388 gc_args.more++;
1389 }
1390
1391 return 0;
1392}
1393
1394static DEFINE_SPINLOCK(fib6_gc_lock);
1395
1396void fib6_run_gc(unsigned long dummy)
1397{
1398 if (dummy != ~0UL) {
1399 spin_lock_bh(&fib6_gc_lock);
1400 gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1401 } else {
1402 local_bh_disable();
1403 if (!spin_trylock(&fib6_gc_lock)) {
1404 mod_timer(&ip6_fib_timer, jiffies + HZ);
1405 local_bh_enable();
1406 return;
1407 }
1408 gc_args.timeout = ip6_rt_gc_interval;
1409 }
1410 gc_args.more = 0;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 ndisc_dst_gc(&gc_args.more);
Thomas Grafc71099a2006-08-04 23:20:06 -07001413 fib6_clean_all(fib6_age, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 if (gc_args.more)
1416 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1417 else {
1418 del_timer(&ip6_fib_timer);
1419 ip6_fib_timer.expires = 0;
1420 }
1421 spin_unlock_bh(&fib6_gc_lock);
1422}
1423
1424void __init fib6_init(void)
1425{
1426 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1427 sizeof(struct fib6_node),
1428 0, SLAB_HWCACHE_ALIGN,
1429 NULL, NULL);
1430 if (!fib6_node_kmem)
1431 panic("cannot create fib6_nodes cache");
Thomas Grafc71099a2006-08-04 23:20:06 -07001432
1433 fib6_tables_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
1436void fib6_gc_cleanup(void)
1437{
1438 del_timer(&ip6_fib_timer);
1439 kmem_cache_destroy(fib6_node_kmem);
1440}