blob: 1b354aa979340f41c0bc5d413f87dbdf2ff69117 [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 */
22#include <linux/config.h>
23#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>
30
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
52static kmem_cache_t * fib6_node_kmem;
53
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
95static struct timer_list ip6_fib_timer = TIMER_INITIALIZER(fib6_run_gc, 0, 0);
96
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
130/*
131 * find the first different bit between two addresses
132 * length of address must be a multiple of 32bits
133 */
134
135static __inline__ int addr_diff(void *token1, void *token2, int addrlen)
136{
137 __u32 *a1 = token1;
138 __u32 *a2 = token2;
139 int i;
140
141 addrlen >>= 2;
142
143 for (i = 0; i < addrlen; i++) {
144 __u32 xb;
145
146 xb = a1[i] ^ a2[i];
147
148 if (xb) {
149 int j = 31;
150
151 xb = ntohl(xb);
152
153 while ((xb & (1 << j)) == 0)
154 j--;
155
156 return (i * 32 + 31 - j);
157 }
158 }
159
160 /*
161 * we should *never* get to this point since that
162 * would mean the addrs are equal
163 *
164 * However, we do get to it 8) And exacly, when
165 * addresses are equal 8)
166 *
167 * ip route add 1111::/128 via ...
168 * ip route add 1111::/64 via ...
169 * and we are here.
170 *
171 * Ideally, this function should stop comparison
172 * at prefix length. It does not, but it is still OK,
173 * if returned value is greater than prefix length.
174 * --ANK (980803)
175 */
176
177 return addrlen<<5;
178}
179
180static __inline__ struct fib6_node * node_alloc(void)
181{
182 struct fib6_node *fn;
183
184 if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
185 memset(fn, 0, sizeof(struct fib6_node));
186
187 return fn;
188}
189
190static __inline__ void node_free(struct fib6_node * fn)
191{
192 kmem_cache_free(fib6_node_kmem, fn);
193}
194
195static __inline__ void rt6_release(struct rt6_info *rt)
196{
197 if (atomic_dec_and_test(&rt->rt6i_ref))
198 dst_free(&rt->u.dst);
199}
200
201
202/*
203 * Routing Table
204 *
205 * return the appropriate node for a routing tree "add" operation
206 * by either creating and inserting or by returning an existing
207 * node.
208 */
209
210static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
211 int addrlen, int plen,
212 int offset)
213{
214 struct fib6_node *fn, *in, *ln;
215 struct fib6_node *pn = NULL;
216 struct rt6key *key;
217 int bit;
218 int dir = 0;
219 __u32 sernum = fib6_new_sernum();
220
221 RT6_TRACE("fib6_add_1\n");
222
223 /* insert node in tree */
224
225 fn = root;
226
227 do {
228 key = (struct rt6key *)((u8 *)fn->leaf + offset);
229
230 /*
231 * Prefix match
232 */
233 if (plen < fn->fn_bit ||
234 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
235 goto insert_above;
236
237 /*
238 * Exact match ?
239 */
240
241 if (plen == fn->fn_bit) {
242 /* clean up an intermediate node */
243 if ((fn->fn_flags & RTN_RTINFO) == 0) {
244 rt6_release(fn->leaf);
245 fn->leaf = NULL;
246 }
247
248 fn->fn_sernum = sernum;
249
250 return fn;
251 }
252
253 /*
254 * We have more bits to go
255 */
256
257 /* Try to walk down on tree. */
258 fn->fn_sernum = sernum;
259 dir = addr_bit_set(addr, fn->fn_bit);
260 pn = fn;
261 fn = dir ? fn->right: fn->left;
262 } while (fn);
263
264 /*
265 * We walked to the bottom of tree.
266 * Create new leaf node without children.
267 */
268
269 ln = node_alloc();
270
271 if (ln == NULL)
272 return NULL;
273 ln->fn_bit = plen;
274
275 ln->parent = pn;
276 ln->fn_sernum = sernum;
277
278 if (dir)
279 pn->right = ln;
280 else
281 pn->left = ln;
282
283 return ln;
284
285
286insert_above:
287 /*
288 * split since we don't have a common prefix anymore or
289 * we have a less significant route.
290 * we've to insert an intermediate node on the list
291 * this new node will point to the one we need to create
292 * and the current
293 */
294
295 pn = fn->parent;
296
297 /* find 1st bit in difference between the 2 addrs.
298
299 See comment in addr_diff: bit may be an invalid value,
300 but if it is >= plen, the value is ignored in any case.
301 */
302
303 bit = addr_diff(addr, &key->addr, addrlen);
304
305 /*
306 * (intermediate)[in]
307 * / \
308 * (new leaf node)[ln] (old node)[fn]
309 */
310 if (plen > bit) {
311 in = node_alloc();
312 ln = node_alloc();
313
314 if (in == NULL || ln == NULL) {
315 if (in)
316 node_free(in);
317 if (ln)
318 node_free(ln);
319 return NULL;
320 }
321
322 /*
323 * new intermediate node.
324 * RTN_RTINFO will
325 * be off since that an address that chooses one of
326 * the branches would not match less specific routes
327 * in the other branch
328 */
329
330 in->fn_bit = bit;
331
332 in->parent = pn;
333 in->leaf = fn->leaf;
334 atomic_inc(&in->leaf->rt6i_ref);
335
336 in->fn_sernum = sernum;
337
338 /* update parent pointer */
339 if (dir)
340 pn->right = in;
341 else
342 pn->left = in;
343
344 ln->fn_bit = plen;
345
346 ln->parent = in;
347 fn->parent = in;
348
349 ln->fn_sernum = sernum;
350
351 if (addr_bit_set(addr, bit)) {
352 in->right = ln;
353 in->left = fn;
354 } else {
355 in->left = ln;
356 in->right = fn;
357 }
358 } else { /* plen <= bit */
359
360 /*
361 * (new leaf node)[ln]
362 * / \
363 * (old node)[fn] NULL
364 */
365
366 ln = node_alloc();
367
368 if (ln == NULL)
369 return NULL;
370
371 ln->fn_bit = plen;
372
373 ln->parent = pn;
374
375 ln->fn_sernum = sernum;
376
377 if (dir)
378 pn->right = ln;
379 else
380 pn->left = ln;
381
382 if (addr_bit_set(&key->addr, plen))
383 ln->right = fn;
384 else
385 ln->left = fn;
386
387 fn->parent = ln;
388 }
389 return ln;
390}
391
392/*
393 * Insert routing information in a node.
394 */
395
396static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700397 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct rt6_info *iter = NULL;
400 struct rt6_info **ins;
401
402 ins = &fn->leaf;
403
404 if (fn->fn_flags&RTN_TL_ROOT &&
405 fn->leaf == &ip6_null_entry &&
406 !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
407 fn->leaf = rt;
408 rt->u.next = NULL;
409 goto out;
410 }
411
412 for (iter = fn->leaf; iter; iter=iter->u.next) {
413 /*
414 * Search for duplicates
415 */
416
417 if (iter->rt6i_metric == rt->rt6i_metric) {
418 /*
419 * Same priority level
420 */
421
422 if (iter->rt6i_dev == rt->rt6i_dev &&
423 iter->rt6i_idev == rt->rt6i_idev &&
424 ipv6_addr_equal(&iter->rt6i_gateway,
425 &rt->rt6i_gateway)) {
426 if (!(iter->rt6i_flags&RTF_EXPIRES))
427 return -EEXIST;
428 iter->rt6i_expires = rt->rt6i_expires;
429 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
430 iter->rt6i_flags &= ~RTF_EXPIRES;
431 iter->rt6i_expires = 0;
432 }
433 return -EEXIST;
434 }
435 }
436
437 if (iter->rt6i_metric > rt->rt6i_metric)
438 break;
439
440 ins = &iter->u.next;
441 }
442
443 /*
444 * insert node
445 */
446
447out:
448 rt->u.next = iter;
449 *ins = rt;
450 rt->rt6i_node = fn;
451 atomic_inc(&rt->rt6i_ref);
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700452 inet6_rt_notify(RTM_NEWROUTE, rt, nlh, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 rt6_stats.fib_rt_entries++;
454
455 if ((fn->fn_flags & RTN_RTINFO) == 0) {
456 rt6_stats.fib_route_nodes++;
457 fn->fn_flags |= RTN_RTINFO;
458 }
459
460 return 0;
461}
462
463static __inline__ void fib6_start_gc(struct rt6_info *rt)
464{
465 if (ip6_fib_timer.expires == 0 &&
466 (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
467 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
468}
469
470void fib6_force_start_gc(void)
471{
472 if (ip6_fib_timer.expires == 0)
473 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
474}
475
476/*
477 * Add routing information to the routing tree.
478 * <destination addr>/<source addr>
479 * with source addr info in sub-trees
480 */
481
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700482int fib6_add(struct fib6_node *root, struct rt6_info *rt,
483 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct fib6_node *fn;
486 int err = -ENOMEM;
487
488 fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
489 rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
490
491 if (fn == NULL)
492 goto out;
493
494#ifdef CONFIG_IPV6_SUBTREES
495 if (rt->rt6i_src.plen) {
496 struct fib6_node *sn;
497
498 if (fn->subtree == NULL) {
499 struct fib6_node *sfn;
500
501 /*
502 * Create subtree.
503 *
504 * fn[main tree]
505 * |
506 * sfn[subtree root]
507 * \
508 * sn[new leaf node]
509 */
510
511 /* Create subtree root node */
512 sfn = node_alloc();
513 if (sfn == NULL)
514 goto st_failure;
515
516 sfn->leaf = &ip6_null_entry;
517 atomic_inc(&ip6_null_entry.rt6i_ref);
518 sfn->fn_flags = RTN_ROOT;
519 sfn->fn_sernum = fib6_new_sernum();
520
521 /* Now add the first leaf node to new subtree */
522
523 sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
524 sizeof(struct in6_addr), rt->rt6i_src.plen,
525 offsetof(struct rt6_info, rt6i_src));
526
527 if (sn == NULL) {
528 /* If it is failed, discard just allocated
529 root, and then (in st_failure) stale node
530 in main tree.
531 */
532 node_free(sfn);
533 goto st_failure;
534 }
535
536 /* Now link new subtree to main tree */
537 sfn->parent = fn;
538 fn->subtree = sfn;
539 if (fn->leaf == NULL) {
540 fn->leaf = rt;
541 atomic_inc(&rt->rt6i_ref);
542 }
543 } else {
544 sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
545 sizeof(struct in6_addr), rt->rt6i_src.plen,
546 offsetof(struct rt6_info, rt6i_src));
547
548 if (sn == NULL)
549 goto st_failure;
550 }
551
552 fn = sn;
553 }
554#endif
555
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700556 err = fib6_add_rt2node(fn, rt, nlh, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (err == 0) {
559 fib6_start_gc(rt);
560 if (!(rt->rt6i_flags&RTF_CACHE))
561 fib6_prune_clones(fn, rt);
562 }
563
564out:
565 if (err)
566 dst_free(&rt->u.dst);
567 return err;
568
569#ifdef CONFIG_IPV6_SUBTREES
570 /* Subtree creation failed, probably main tree node
571 is orphan. If it is, shoot it.
572 */
573st_failure:
574 if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
575 fib6_repair_tree(fn);
576 dst_free(&rt->u.dst);
577 return err;
578#endif
579}
580
581/*
582 * Routing tree lookup
583 *
584 */
585
586struct lookup_args {
587 int offset; /* key offset on rt6_info */
588 struct in6_addr *addr; /* search key */
589};
590
591static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
592 struct lookup_args *args)
593{
594 struct fib6_node *fn;
595 int dir;
596
597 /*
598 * Descend on a tree
599 */
600
601 fn = root;
602
603 for (;;) {
604 struct fib6_node *next;
605
606 dir = addr_bit_set(args->addr, fn->fn_bit);
607
608 next = dir ? fn->right : fn->left;
609
610 if (next) {
611 fn = next;
612 continue;
613 }
614
615 break;
616 }
617
618 while ((fn->fn_flags & RTN_ROOT) == 0) {
619#ifdef CONFIG_IPV6_SUBTREES
620 if (fn->subtree) {
621 struct fib6_node *st;
622 struct lookup_args *narg;
623
624 narg = args + 1;
625
626 if (narg->addr) {
627 st = fib6_lookup_1(fn->subtree, narg);
628
629 if (st && !(st->fn_flags & RTN_ROOT))
630 return st;
631 }
632 }
633#endif
634
635 if (fn->fn_flags & RTN_RTINFO) {
636 struct rt6key *key;
637
638 key = (struct rt6key *) ((u8 *) fn->leaf +
639 args->offset);
640
641 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
642 return fn;
643 }
644
645 fn = fn->parent;
646 }
647
648 return NULL;
649}
650
651struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
652 struct in6_addr *saddr)
653{
654 struct lookup_args args[2];
655 struct fib6_node *fn;
656
657 args[0].offset = offsetof(struct rt6_info, rt6i_dst);
658 args[0].addr = daddr;
659
660#ifdef CONFIG_IPV6_SUBTREES
661 args[1].offset = offsetof(struct rt6_info, rt6i_src);
662 args[1].addr = saddr;
663#endif
664
665 fn = fib6_lookup_1(root, args);
666
667 if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
668 fn = root;
669
670 return fn;
671}
672
673/*
674 * Get node with specified destination prefix (and source prefix,
675 * if subtrees are used)
676 */
677
678
679static struct fib6_node * fib6_locate_1(struct fib6_node *root,
680 struct in6_addr *addr,
681 int plen, int offset)
682{
683 struct fib6_node *fn;
684
685 for (fn = root; fn ; ) {
686 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
687
688 /*
689 * Prefix match
690 */
691 if (plen < fn->fn_bit ||
692 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
693 return NULL;
694
695 if (plen == fn->fn_bit)
696 return fn;
697
698 /*
699 * We have more bits to go
700 */
701 if (addr_bit_set(addr, fn->fn_bit))
702 fn = fn->right;
703 else
704 fn = fn->left;
705 }
706 return NULL;
707}
708
709struct fib6_node * fib6_locate(struct fib6_node *root,
710 struct in6_addr *daddr, int dst_len,
711 struct in6_addr *saddr, int src_len)
712{
713 struct fib6_node *fn;
714
715 fn = fib6_locate_1(root, daddr, dst_len,
716 offsetof(struct rt6_info, rt6i_dst));
717
718#ifdef CONFIG_IPV6_SUBTREES
719 if (src_len) {
720 BUG_TRAP(saddr!=NULL);
721 if (fn == NULL)
722 fn = fn->subtree;
723 if (fn)
724 fn = fib6_locate_1(fn, saddr, src_len,
725 offsetof(struct rt6_info, rt6i_src));
726 }
727#endif
728
729 if (fn && fn->fn_flags&RTN_RTINFO)
730 return fn;
731
732 return NULL;
733}
734
735
736/*
737 * Deletion
738 *
739 */
740
741static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
742{
743 if (fn->fn_flags&RTN_ROOT)
744 return &ip6_null_entry;
745
746 while(fn) {
747 if(fn->left)
748 return fn->left->leaf;
749
750 if(fn->right)
751 return fn->right->leaf;
752
753 fn = SUBTREE(fn);
754 }
755 return NULL;
756}
757
758/*
759 * Called to trim the tree of intermediate nodes when possible. "fn"
760 * is the node we want to try and remove.
761 */
762
763static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
764{
765 int children;
766 int nstate;
767 struct fib6_node *child, *pn;
768 struct fib6_walker_t *w;
769 int iter = 0;
770
771 for (;;) {
772 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
773 iter++;
774
775 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
776 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
777 BUG_TRAP(fn->leaf==NULL);
778
779 children = 0;
780 child = NULL;
781 if (fn->right) child = fn->right, children |= 1;
782 if (fn->left) child = fn->left, children |= 2;
783
784 if (children == 3 || SUBTREE(fn)
785#ifdef CONFIG_IPV6_SUBTREES
786 /* Subtree root (i.e. fn) may have one child */
787 || (children && fn->fn_flags&RTN_ROOT)
788#endif
789 ) {
790 fn->leaf = fib6_find_prefix(fn);
791#if RT6_DEBUG >= 2
792 if (fn->leaf==NULL) {
793 BUG_TRAP(fn->leaf);
794 fn->leaf = &ip6_null_entry;
795 }
796#endif
797 atomic_inc(&fn->leaf->rt6i_ref);
798 return fn->parent;
799 }
800
801 pn = fn->parent;
802#ifdef CONFIG_IPV6_SUBTREES
803 if (SUBTREE(pn) == fn) {
804 BUG_TRAP(fn->fn_flags&RTN_ROOT);
805 SUBTREE(pn) = NULL;
806 nstate = FWS_L;
807 } else {
808 BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
809#endif
810 if (pn->right == fn) pn->right = child;
811 else if (pn->left == fn) pn->left = child;
812#if RT6_DEBUG >= 2
813 else BUG_TRAP(0);
814#endif
815 if (child)
816 child->parent = pn;
817 nstate = FWS_R;
818#ifdef CONFIG_IPV6_SUBTREES
819 }
820#endif
821
822 read_lock(&fib6_walker_lock);
823 FOR_WALKERS(w) {
824 if (child == NULL) {
825 if (w->root == fn) {
826 w->root = w->node = NULL;
827 RT6_TRACE("W %p adjusted by delroot 1\n", w);
828 } else if (w->node == fn) {
829 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
830 w->node = pn;
831 w->state = nstate;
832 }
833 } else {
834 if (w->root == fn) {
835 w->root = child;
836 RT6_TRACE("W %p adjusted by delroot 2\n", w);
837 }
838 if (w->node == fn) {
839 w->node = child;
840 if (children&2) {
841 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
842 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
843 } else {
844 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
845 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
846 }
847 }
848 }
849 }
850 read_unlock(&fib6_walker_lock);
851
852 node_free(fn);
853 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
854 return pn;
855
856 rt6_release(pn->leaf);
857 pn->leaf = NULL;
858 fn = pn;
859 }
860}
861
862static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700863 struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct fib6_walker_t *w;
866 struct rt6_info *rt = *rtp;
867
868 RT6_TRACE("fib6_del_route\n");
869
870 /* Unlink it */
871 *rtp = rt->u.next;
872 rt->rt6i_node = NULL;
873 rt6_stats.fib_rt_entries--;
874 rt6_stats.fib_discarded_routes++;
875
876 /* Adjust walkers */
877 read_lock(&fib6_walker_lock);
878 FOR_WALKERS(w) {
879 if (w->state == FWS_C && w->leaf == rt) {
880 RT6_TRACE("walker %p adjusted by delroute\n", w);
881 w->leaf = rt->u.next;
882 if (w->leaf == NULL)
883 w->state = FWS_U;
884 }
885 }
886 read_unlock(&fib6_walker_lock);
887
888 rt->u.next = NULL;
889
890 if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
891 fn->leaf = &ip6_null_entry;
892
893 /* If it was last route, expunge its radix tree node */
894 if (fn->leaf == NULL) {
895 fn->fn_flags &= ~RTN_RTINFO;
896 rt6_stats.fib_route_nodes--;
897 fn = fib6_repair_tree(fn);
898 }
899
900 if (atomic_read(&rt->rt6i_ref) != 1) {
901 /* This route is used as dummy address holder in some split
902 * nodes. It is not leaked, but it still holds other resources,
903 * which must be released in time. So, scan ascendant nodes
904 * and replace dummy references to this route with references
905 * to still alive ones.
906 */
907 while (fn) {
908 if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
909 fn->leaf = fib6_find_prefix(fn);
910 atomic_inc(&fn->leaf->rt6i_ref);
911 rt6_release(rt);
912 }
913 fn = fn->parent;
914 }
915 /* No more references are possible at this point. */
916 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
917 }
918
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700919 inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 rt6_release(rt);
921}
922
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700923int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 struct fib6_node *fn = rt->rt6i_node;
926 struct rt6_info **rtp;
927
928#if RT6_DEBUG >= 2
929 if (rt->u.dst.obsolete>0) {
930 BUG_TRAP(fn==NULL);
931 return -ENOENT;
932 }
933#endif
934 if (fn == NULL || rt == &ip6_null_entry)
935 return -ENOENT;
936
937 BUG_TRAP(fn->fn_flags&RTN_RTINFO);
938
939 if (!(rt->rt6i_flags&RTF_CACHE))
940 fib6_prune_clones(fn, rt);
941
942 /*
943 * Walk the leaf entries looking for ourself
944 */
945
946 for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
947 if (*rtp == rt) {
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -0700948 fib6_del_route(fn, rtp, nlh, _rtattr, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950 }
951 }
952 return -ENOENT;
953}
954
955/*
956 * Tree traversal function.
957 *
958 * Certainly, it is not interrupt safe.
959 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
960 * It means, that we can modify tree during walking
961 * and use this function for garbage collection, clone pruning,
962 * cleaning tree when a device goes down etc. etc.
963 *
964 * It guarantees that every node will be traversed,
965 * and that it will be traversed only once.
966 *
967 * Callback function w->func may return:
968 * 0 -> continue walking.
969 * positive value -> walking is suspended (used by tree dumps,
970 * and probably by gc, if it will be split to several slices)
971 * negative value -> terminate walking.
972 *
973 * The function itself returns:
974 * 0 -> walk is complete.
975 * >0 -> walk is incomplete (i.e. suspended)
976 * <0 -> walk is terminated by an error.
977 */
978
979int fib6_walk_continue(struct fib6_walker_t *w)
980{
981 struct fib6_node *fn, *pn;
982
983 for (;;) {
984 fn = w->node;
985 if (fn == NULL)
986 return 0;
987
988 if (w->prune && fn != w->root &&
989 fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
990 w->state = FWS_C;
991 w->leaf = fn->leaf;
992 }
993 switch (w->state) {
994#ifdef CONFIG_IPV6_SUBTREES
995 case FWS_S:
996 if (SUBTREE(fn)) {
997 w->node = SUBTREE(fn);
998 continue;
999 }
1000 w->state = FWS_L;
1001#endif
1002 case FWS_L:
1003 if (fn->left) {
1004 w->node = fn->left;
1005 w->state = FWS_INIT;
1006 continue;
1007 }
1008 w->state = FWS_R;
1009 case FWS_R:
1010 if (fn->right) {
1011 w->node = fn->right;
1012 w->state = FWS_INIT;
1013 continue;
1014 }
1015 w->state = FWS_C;
1016 w->leaf = fn->leaf;
1017 case FWS_C:
1018 if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1019 int err = w->func(w);
1020 if (err)
1021 return err;
1022 continue;
1023 }
1024 w->state = FWS_U;
1025 case FWS_U:
1026 if (fn == w->root)
1027 return 0;
1028 pn = fn->parent;
1029 w->node = pn;
1030#ifdef CONFIG_IPV6_SUBTREES
1031 if (SUBTREE(pn) == fn) {
1032 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1033 w->state = FWS_L;
1034 continue;
1035 }
1036#endif
1037 if (pn->left == fn) {
1038 w->state = FWS_R;
1039 continue;
1040 }
1041 if (pn->right == fn) {
1042 w->state = FWS_C;
1043 w->leaf = w->node->leaf;
1044 continue;
1045 }
1046#if RT6_DEBUG >= 2
1047 BUG_TRAP(0);
1048#endif
1049 }
1050 }
1051}
1052
1053int fib6_walk(struct fib6_walker_t *w)
1054{
1055 int res;
1056
1057 w->state = FWS_INIT;
1058 w->node = w->root;
1059
1060 fib6_walker_link(w);
1061 res = fib6_walk_continue(w);
1062 if (res <= 0)
1063 fib6_walker_unlink(w);
1064 return res;
1065}
1066
1067static int fib6_clean_node(struct fib6_walker_t *w)
1068{
1069 int res;
1070 struct rt6_info *rt;
1071 struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1072
1073 for (rt = w->leaf; rt; rt = rt->u.next) {
1074 res = c->func(rt, c->arg);
1075 if (res < 0) {
1076 w->leaf = rt;
Jamal Hadi Salim0d51aa82005-06-21 13:51:04 -07001077 res = fib6_del(rt, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (res) {
1079#if RT6_DEBUG >= 2
1080 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1081#endif
1082 continue;
1083 }
1084 return 0;
1085 }
1086 BUG_TRAP(res==0);
1087 }
1088 w->leaf = rt;
1089 return 0;
1090}
1091
1092/*
1093 * Convenient frontend to tree walker.
1094 *
1095 * func is called on each route.
1096 * It may return -1 -> delete this route.
1097 * 0 -> continue walking
1098 *
1099 * prune==1 -> only immediate children of node (certainly,
1100 * ignoring pure split nodes) will be scanned.
1101 */
1102
1103void fib6_clean_tree(struct fib6_node *root,
1104 int (*func)(struct rt6_info *, void *arg),
1105 int prune, void *arg)
1106{
1107 struct fib6_cleaner_t c;
1108
1109 c.w.root = root;
1110 c.w.func = fib6_clean_node;
1111 c.w.prune = prune;
1112 c.func = func;
1113 c.arg = arg;
1114
1115 fib6_walk(&c.w);
1116}
1117
1118static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1119{
1120 if (rt->rt6i_flags & RTF_CACHE) {
1121 RT6_TRACE("pruning clone %p\n", rt);
1122 return -1;
1123 }
1124
1125 return 0;
1126}
1127
1128static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1129{
1130 fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1131}
1132
1133/*
1134 * Garbage collection
1135 */
1136
1137static struct fib6_gc_args
1138{
1139 int timeout;
1140 int more;
1141} gc_args;
1142
1143static int fib6_age(struct rt6_info *rt, void *arg)
1144{
1145 unsigned long now = jiffies;
1146
1147 /*
1148 * check addrconf expiration here.
1149 * Routes are expired even if they are in use.
1150 *
1151 * Also age clones. Note, that clones are aged out
1152 * only if they are not in use now.
1153 */
1154
1155 if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1156 if (time_after(now, rt->rt6i_expires)) {
1157 RT6_TRACE("expiring %p\n", rt);
1158 rt6_reset_dflt_pointer(rt);
1159 return -1;
1160 }
1161 gc_args.more++;
1162 } else if (rt->rt6i_flags & RTF_CACHE) {
1163 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1164 time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1165 RT6_TRACE("aging clone %p\n", rt);
1166 return -1;
1167 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1168 (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1169 RT6_TRACE("purging route %p via non-router but gateway\n",
1170 rt);
1171 return -1;
1172 }
1173 gc_args.more++;
1174 }
1175
1176 return 0;
1177}
1178
1179static DEFINE_SPINLOCK(fib6_gc_lock);
1180
1181void fib6_run_gc(unsigned long dummy)
1182{
1183 if (dummy != ~0UL) {
1184 spin_lock_bh(&fib6_gc_lock);
1185 gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1186 } else {
1187 local_bh_disable();
1188 if (!spin_trylock(&fib6_gc_lock)) {
1189 mod_timer(&ip6_fib_timer, jiffies + HZ);
1190 local_bh_enable();
1191 return;
1192 }
1193 gc_args.timeout = ip6_rt_gc_interval;
1194 }
1195 gc_args.more = 0;
1196
1197
1198 write_lock_bh(&rt6_lock);
1199 ndisc_dst_gc(&gc_args.more);
1200 fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1201 write_unlock_bh(&rt6_lock);
1202
1203 if (gc_args.more)
1204 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1205 else {
1206 del_timer(&ip6_fib_timer);
1207 ip6_fib_timer.expires = 0;
1208 }
1209 spin_unlock_bh(&fib6_gc_lock);
1210}
1211
1212void __init fib6_init(void)
1213{
1214 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1215 sizeof(struct fib6_node),
1216 0, SLAB_HWCACHE_ALIGN,
1217 NULL, NULL);
1218 if (!fib6_node_kmem)
1219 panic("cannot create fib6_nodes cache");
1220}
1221
1222void fib6_gc_cleanup(void)
1223{
1224 del_timer(&ip6_fib_timer);
1225 kmem_cache_destroy(fib6_node_kmem);
1226}