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