xref: /openbmc/linux/net/ipv6/ip6_fib.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
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.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 
50 struct rt6_statistics	rt6_stats;
51 
52 static kmem_cache_t * fib6_node_kmem;
53 
54 enum 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 
65 struct fib6_cleaner_t
66 {
67 	struct fib6_walker_t w;
68 	int (*func)(struct rt6_info *, void *arg);
69 	void *arg;
70 };
71 
72 DEFINE_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 
83 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
84 static 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 
93 static __u32 rt_sernum;
94 
95 static struct timer_list ip6_fib_timer = TIMER_INITIALIZER(fib6_run_gc, 0, 0);
96 
97 struct 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 
104 static __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 
123 static __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 
135 static __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 
180 static __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 
190 static __inline__ void node_free(struct fib6_node * fn)
191 {
192 	kmem_cache_free(fib6_node_kmem, fn);
193 }
194 
195 static __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 
210 static 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 
286 insert_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 
396 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
397     struct nlmsghdr *nlh)
398 {
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 
447 out:
448 	rt->u.next = iter;
449 	*ins = rt;
450 	rt->rt6i_node = fn;
451 	atomic_inc(&rt->rt6i_ref);
452 	inet6_rt_notify(RTM_NEWROUTE, rt, nlh);
453 	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 
463 static __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 
470 void 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 
482 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr)
483 {
484 	struct fib6_node *fn;
485 	int err = -ENOMEM;
486 
487 	fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
488 			rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
489 
490 	if (fn == NULL)
491 		goto out;
492 
493 #ifdef CONFIG_IPV6_SUBTREES
494 	if (rt->rt6i_src.plen) {
495 		struct fib6_node *sn;
496 
497 		if (fn->subtree == NULL) {
498 			struct fib6_node *sfn;
499 
500 			/*
501 			 * Create subtree.
502 			 *
503 			 *		fn[main tree]
504 			 *		|
505 			 *		sfn[subtree root]
506 			 *		   \
507 			 *		    sn[new leaf node]
508 			 */
509 
510 			/* Create subtree root node */
511 			sfn = node_alloc();
512 			if (sfn == NULL)
513 				goto st_failure;
514 
515 			sfn->leaf = &ip6_null_entry;
516 			atomic_inc(&ip6_null_entry.rt6i_ref);
517 			sfn->fn_flags = RTN_ROOT;
518 			sfn->fn_sernum = fib6_new_sernum();
519 
520 			/* Now add the first leaf node to new subtree */
521 
522 			sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
523 					sizeof(struct in6_addr), rt->rt6i_src.plen,
524 					offsetof(struct rt6_info, rt6i_src));
525 
526 			if (sn == NULL) {
527 				/* If it is failed, discard just allocated
528 				   root, and then (in st_failure) stale node
529 				   in main tree.
530 				 */
531 				node_free(sfn);
532 				goto st_failure;
533 			}
534 
535 			/* Now link new subtree to main tree */
536 			sfn->parent = fn;
537 			fn->subtree = sfn;
538 			if (fn->leaf == NULL) {
539 				fn->leaf = rt;
540 				atomic_inc(&rt->rt6i_ref);
541 			}
542 		} else {
543 			sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
544 					sizeof(struct in6_addr), rt->rt6i_src.plen,
545 					offsetof(struct rt6_info, rt6i_src));
546 
547 			if (sn == NULL)
548 				goto st_failure;
549 		}
550 
551 		fn = sn;
552 	}
553 #endif
554 
555 	err = fib6_add_rt2node(fn, rt, nlh);
556 
557 	if (err == 0) {
558 		fib6_start_gc(rt);
559 		if (!(rt->rt6i_flags&RTF_CACHE))
560 			fib6_prune_clones(fn, rt);
561 	}
562 
563 out:
564 	if (err)
565 		dst_free(&rt->u.dst);
566 	return err;
567 
568 #ifdef CONFIG_IPV6_SUBTREES
569 	/* Subtree creation failed, probably main tree node
570 	   is orphan. If it is, shoot it.
571 	 */
572 st_failure:
573 	if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
574 		fib6_repair_tree(fn);
575 	dst_free(&rt->u.dst);
576 	return err;
577 #endif
578 }
579 
580 /*
581  *	Routing tree lookup
582  *
583  */
584 
585 struct lookup_args {
586 	int		offset;		/* key offset on rt6_info	*/
587 	struct in6_addr	*addr;		/* search key			*/
588 };
589 
590 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
591 					struct lookup_args *args)
592 {
593 	struct fib6_node *fn;
594 	int dir;
595 
596 	/*
597 	 *	Descend on a tree
598 	 */
599 
600 	fn = root;
601 
602 	for (;;) {
603 		struct fib6_node *next;
604 
605 		dir = addr_bit_set(args->addr, fn->fn_bit);
606 
607 		next = dir ? fn->right : fn->left;
608 
609 		if (next) {
610 			fn = next;
611 			continue;
612 		}
613 
614 		break;
615 	}
616 
617 	while ((fn->fn_flags & RTN_ROOT) == 0) {
618 #ifdef CONFIG_IPV6_SUBTREES
619 		if (fn->subtree) {
620 			struct fib6_node *st;
621 			struct lookup_args *narg;
622 
623 			narg = args + 1;
624 
625 			if (narg->addr) {
626 				st = fib6_lookup_1(fn->subtree, narg);
627 
628 				if (st && !(st->fn_flags & RTN_ROOT))
629 					return st;
630 			}
631 		}
632 #endif
633 
634 		if (fn->fn_flags & RTN_RTINFO) {
635 			struct rt6key *key;
636 
637 			key = (struct rt6key *) ((u8 *) fn->leaf +
638 						 args->offset);
639 
640 			if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
641 				return fn;
642 		}
643 
644 		fn = fn->parent;
645 	}
646 
647 	return NULL;
648 }
649 
650 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
651 			       struct in6_addr *saddr)
652 {
653 	struct lookup_args args[2];
654 	struct fib6_node *fn;
655 
656 	args[0].offset = offsetof(struct rt6_info, rt6i_dst);
657 	args[0].addr = daddr;
658 
659 #ifdef CONFIG_IPV6_SUBTREES
660 	args[1].offset = offsetof(struct rt6_info, rt6i_src);
661 	args[1].addr = saddr;
662 #endif
663 
664 	fn = fib6_lookup_1(root, args);
665 
666 	if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
667 		fn = root;
668 
669 	return fn;
670 }
671 
672 /*
673  *	Get node with specified destination prefix (and source prefix,
674  *	if subtrees are used)
675  */
676 
677 
678 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
679 					struct in6_addr *addr,
680 					int plen, int offset)
681 {
682 	struct fib6_node *fn;
683 
684 	for (fn = root; fn ; ) {
685 		struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
686 
687 		/*
688 		 *	Prefix match
689 		 */
690 		if (plen < fn->fn_bit ||
691 		    !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
692 			return NULL;
693 
694 		if (plen == fn->fn_bit)
695 			return fn;
696 
697 		/*
698 		 *	We have more bits to go
699 		 */
700 		if (addr_bit_set(addr, fn->fn_bit))
701 			fn = fn->right;
702 		else
703 			fn = fn->left;
704 	}
705 	return NULL;
706 }
707 
708 struct fib6_node * fib6_locate(struct fib6_node *root,
709 			       struct in6_addr *daddr, int dst_len,
710 			       struct in6_addr *saddr, int src_len)
711 {
712 	struct fib6_node *fn;
713 
714 	fn = fib6_locate_1(root, daddr, dst_len,
715 			   offsetof(struct rt6_info, rt6i_dst));
716 
717 #ifdef CONFIG_IPV6_SUBTREES
718 	if (src_len) {
719 		BUG_TRAP(saddr!=NULL);
720 		if (fn == NULL)
721 			fn = fn->subtree;
722 		if (fn)
723 			fn = fib6_locate_1(fn, saddr, src_len,
724 					   offsetof(struct rt6_info, rt6i_src));
725 	}
726 #endif
727 
728 	if (fn && fn->fn_flags&RTN_RTINFO)
729 		return fn;
730 
731 	return NULL;
732 }
733 
734 
735 /*
736  *	Deletion
737  *
738  */
739 
740 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
741 {
742 	if (fn->fn_flags&RTN_ROOT)
743 		return &ip6_null_entry;
744 
745 	while(fn) {
746 		if(fn->left)
747 			return fn->left->leaf;
748 
749 		if(fn->right)
750 			return fn->right->leaf;
751 
752 		fn = SUBTREE(fn);
753 	}
754 	return NULL;
755 }
756 
757 /*
758  *	Called to trim the tree of intermediate nodes when possible. "fn"
759  *	is the node we want to try and remove.
760  */
761 
762 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
763 {
764 	int children;
765 	int nstate;
766 	struct fib6_node *child, *pn;
767 	struct fib6_walker_t *w;
768 	int iter = 0;
769 
770 	for (;;) {
771 		RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
772 		iter++;
773 
774 		BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
775 		BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
776 		BUG_TRAP(fn->leaf==NULL);
777 
778 		children = 0;
779 		child = NULL;
780 		if (fn->right) child = fn->right, children |= 1;
781 		if (fn->left) child = fn->left, children |= 2;
782 
783 		if (children == 3 || SUBTREE(fn)
784 #ifdef CONFIG_IPV6_SUBTREES
785 		    /* Subtree root (i.e. fn) may have one child */
786 		    || (children && fn->fn_flags&RTN_ROOT)
787 #endif
788 		    ) {
789 			fn->leaf = fib6_find_prefix(fn);
790 #if RT6_DEBUG >= 2
791 			if (fn->leaf==NULL) {
792 				BUG_TRAP(fn->leaf);
793 				fn->leaf = &ip6_null_entry;
794 			}
795 #endif
796 			atomic_inc(&fn->leaf->rt6i_ref);
797 			return fn->parent;
798 		}
799 
800 		pn = fn->parent;
801 #ifdef CONFIG_IPV6_SUBTREES
802 		if (SUBTREE(pn) == fn) {
803 			BUG_TRAP(fn->fn_flags&RTN_ROOT);
804 			SUBTREE(pn) = NULL;
805 			nstate = FWS_L;
806 		} else {
807 			BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
808 #endif
809 			if (pn->right == fn) pn->right = child;
810 			else if (pn->left == fn) pn->left = child;
811 #if RT6_DEBUG >= 2
812 			else BUG_TRAP(0);
813 #endif
814 			if (child)
815 				child->parent = pn;
816 			nstate = FWS_R;
817 #ifdef CONFIG_IPV6_SUBTREES
818 		}
819 #endif
820 
821 		read_lock(&fib6_walker_lock);
822 		FOR_WALKERS(w) {
823 			if (child == NULL) {
824 				if (w->root == fn) {
825 					w->root = w->node = NULL;
826 					RT6_TRACE("W %p adjusted by delroot 1\n", w);
827 				} else if (w->node == fn) {
828 					RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
829 					w->node = pn;
830 					w->state = nstate;
831 				}
832 			} else {
833 				if (w->root == fn) {
834 					w->root = child;
835 					RT6_TRACE("W %p adjusted by delroot 2\n", w);
836 				}
837 				if (w->node == fn) {
838 					w->node = child;
839 					if (children&2) {
840 						RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
841 						w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
842 					} else {
843 						RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
844 						w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
845 					}
846 				}
847 			}
848 		}
849 		read_unlock(&fib6_walker_lock);
850 
851 		node_free(fn);
852 		if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
853 			return pn;
854 
855 		rt6_release(pn->leaf);
856 		pn->leaf = NULL;
857 		fn = pn;
858 	}
859 }
860 
861 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
862     struct nlmsghdr *nlh, void *_rtattr)
863 {
864 	struct fib6_walker_t *w;
865 	struct rt6_info *rt = *rtp;
866 
867 	RT6_TRACE("fib6_del_route\n");
868 
869 	/* Unlink it */
870 	*rtp = rt->u.next;
871 	rt->rt6i_node = NULL;
872 	rt6_stats.fib_rt_entries--;
873 	rt6_stats.fib_discarded_routes++;
874 
875 	/* Adjust walkers */
876 	read_lock(&fib6_walker_lock);
877 	FOR_WALKERS(w) {
878 		if (w->state == FWS_C && w->leaf == rt) {
879 			RT6_TRACE("walker %p adjusted by delroute\n", w);
880 			w->leaf = rt->u.next;
881 			if (w->leaf == NULL)
882 				w->state = FWS_U;
883 		}
884 	}
885 	read_unlock(&fib6_walker_lock);
886 
887 	rt->u.next = NULL;
888 
889 	if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
890 		fn->leaf = &ip6_null_entry;
891 
892 	/* If it was last route, expunge its radix tree node */
893 	if (fn->leaf == NULL) {
894 		fn->fn_flags &= ~RTN_RTINFO;
895 		rt6_stats.fib_route_nodes--;
896 		fn = fib6_repair_tree(fn);
897 	}
898 
899 	if (atomic_read(&rt->rt6i_ref) != 1) {
900 		/* This route is used as dummy address holder in some split
901 		 * nodes. It is not leaked, but it still holds other resources,
902 		 * which must be released in time. So, scan ascendant nodes
903 		 * and replace dummy references to this route with references
904 		 * to still alive ones.
905 		 */
906 		while (fn) {
907 			if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
908 				fn->leaf = fib6_find_prefix(fn);
909 				atomic_inc(&fn->leaf->rt6i_ref);
910 				rt6_release(rt);
911 			}
912 			fn = fn->parent;
913 		}
914 		/* No more references are possible at this point. */
915 		if (atomic_read(&rt->rt6i_ref) != 1) BUG();
916 	}
917 
918 	inet6_rt_notify(RTM_DELROUTE, rt, nlh);
919 	rt6_release(rt);
920 }
921 
922 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr)
923 {
924 	struct fib6_node *fn = rt->rt6i_node;
925 	struct rt6_info **rtp;
926 
927 #if RT6_DEBUG >= 2
928 	if (rt->u.dst.obsolete>0) {
929 		BUG_TRAP(fn==NULL);
930 		return -ENOENT;
931 	}
932 #endif
933 	if (fn == NULL || rt == &ip6_null_entry)
934 		return -ENOENT;
935 
936 	BUG_TRAP(fn->fn_flags&RTN_RTINFO);
937 
938 	if (!(rt->rt6i_flags&RTF_CACHE))
939 		fib6_prune_clones(fn, rt);
940 
941 	/*
942 	 *	Walk the leaf entries looking for ourself
943 	 */
944 
945 	for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
946 		if (*rtp == rt) {
947 			fib6_del_route(fn, rtp, nlh, _rtattr);
948 			return 0;
949 		}
950 	}
951 	return -ENOENT;
952 }
953 
954 /*
955  *	Tree traversal function.
956  *
957  *	Certainly, it is not interrupt safe.
958  *	However, it is internally reenterable wrt itself and fib6_add/fib6_del.
959  *	It means, that we can modify tree during walking
960  *	and use this function for garbage collection, clone pruning,
961  *	cleaning tree when a device goes down etc. etc.
962  *
963  *	It guarantees that every node will be traversed,
964  *	and that it will be traversed only once.
965  *
966  *	Callback function w->func may return:
967  *	0 -> continue walking.
968  *	positive value -> walking is suspended (used by tree dumps,
969  *	and probably by gc, if it will be split to several slices)
970  *	negative value -> terminate walking.
971  *
972  *	The function itself returns:
973  *	0   -> walk is complete.
974  *	>0  -> walk is incomplete (i.e. suspended)
975  *	<0  -> walk is terminated by an error.
976  */
977 
978 int fib6_walk_continue(struct fib6_walker_t *w)
979 {
980 	struct fib6_node *fn, *pn;
981 
982 	for (;;) {
983 		fn = w->node;
984 		if (fn == NULL)
985 			return 0;
986 
987 		if (w->prune && fn != w->root &&
988 		    fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
989 			w->state = FWS_C;
990 			w->leaf = fn->leaf;
991 		}
992 		switch (w->state) {
993 #ifdef CONFIG_IPV6_SUBTREES
994 		case FWS_S:
995 			if (SUBTREE(fn)) {
996 				w->node = SUBTREE(fn);
997 				continue;
998 			}
999 			w->state = FWS_L;
1000 #endif
1001 		case FWS_L:
1002 			if (fn->left) {
1003 				w->node = fn->left;
1004 				w->state = FWS_INIT;
1005 				continue;
1006 			}
1007 			w->state = FWS_R;
1008 		case FWS_R:
1009 			if (fn->right) {
1010 				w->node = fn->right;
1011 				w->state = FWS_INIT;
1012 				continue;
1013 			}
1014 			w->state = FWS_C;
1015 			w->leaf = fn->leaf;
1016 		case FWS_C:
1017 			if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1018 				int err = w->func(w);
1019 				if (err)
1020 					return err;
1021 				continue;
1022 			}
1023 			w->state = FWS_U;
1024 		case FWS_U:
1025 			if (fn == w->root)
1026 				return 0;
1027 			pn = fn->parent;
1028 			w->node = pn;
1029 #ifdef CONFIG_IPV6_SUBTREES
1030 			if (SUBTREE(pn) == fn) {
1031 				BUG_TRAP(fn->fn_flags&RTN_ROOT);
1032 				w->state = FWS_L;
1033 				continue;
1034 			}
1035 #endif
1036 			if (pn->left == fn) {
1037 				w->state = FWS_R;
1038 				continue;
1039 			}
1040 			if (pn->right == fn) {
1041 				w->state = FWS_C;
1042 				w->leaf = w->node->leaf;
1043 				continue;
1044 			}
1045 #if RT6_DEBUG >= 2
1046 			BUG_TRAP(0);
1047 #endif
1048 		}
1049 	}
1050 }
1051 
1052 int fib6_walk(struct fib6_walker_t *w)
1053 {
1054 	int res;
1055 
1056 	w->state = FWS_INIT;
1057 	w->node = w->root;
1058 
1059 	fib6_walker_link(w);
1060 	res = fib6_walk_continue(w);
1061 	if (res <= 0)
1062 		fib6_walker_unlink(w);
1063 	return res;
1064 }
1065 
1066 static int fib6_clean_node(struct fib6_walker_t *w)
1067 {
1068 	int res;
1069 	struct rt6_info *rt;
1070 	struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1071 
1072 	for (rt = w->leaf; rt; rt = rt->u.next) {
1073 		res = c->func(rt, c->arg);
1074 		if (res < 0) {
1075 			w->leaf = rt;
1076 			res = fib6_del(rt, NULL, NULL);
1077 			if (res) {
1078 #if RT6_DEBUG >= 2
1079 				printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1080 #endif
1081 				continue;
1082 			}
1083 			return 0;
1084 		}
1085 		BUG_TRAP(res==0);
1086 	}
1087 	w->leaf = rt;
1088 	return 0;
1089 }
1090 
1091 /*
1092  *	Convenient frontend to tree walker.
1093  *
1094  *	func is called on each route.
1095  *		It may return -1 -> delete this route.
1096  *		              0  -> continue walking
1097  *
1098  *	prune==1 -> only immediate children of node (certainly,
1099  *	ignoring pure split nodes) will be scanned.
1100  */
1101 
1102 void fib6_clean_tree(struct fib6_node *root,
1103 		     int (*func)(struct rt6_info *, void *arg),
1104 		     int prune, void *arg)
1105 {
1106 	struct fib6_cleaner_t c;
1107 
1108 	c.w.root = root;
1109 	c.w.func = fib6_clean_node;
1110 	c.w.prune = prune;
1111 	c.func = func;
1112 	c.arg = arg;
1113 
1114 	fib6_walk(&c.w);
1115 }
1116 
1117 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1118 {
1119 	if (rt->rt6i_flags & RTF_CACHE) {
1120 		RT6_TRACE("pruning clone %p\n", rt);
1121 		return -1;
1122 	}
1123 
1124 	return 0;
1125 }
1126 
1127 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1128 {
1129 	fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1130 }
1131 
1132 /*
1133  *	Garbage collection
1134  */
1135 
1136 static struct fib6_gc_args
1137 {
1138 	int			timeout;
1139 	int			more;
1140 } gc_args;
1141 
1142 static int fib6_age(struct rt6_info *rt, void *arg)
1143 {
1144 	unsigned long now = jiffies;
1145 
1146 	/*
1147 	 *	check addrconf expiration here.
1148 	 *	Routes are expired even if they are in use.
1149 	 *
1150 	 *	Also age clones. Note, that clones are aged out
1151 	 *	only if they are not in use now.
1152 	 */
1153 
1154 	if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1155 		if (time_after(now, rt->rt6i_expires)) {
1156 			RT6_TRACE("expiring %p\n", rt);
1157 			rt6_reset_dflt_pointer(rt);
1158 			return -1;
1159 		}
1160 		gc_args.more++;
1161 	} else if (rt->rt6i_flags & RTF_CACHE) {
1162 		if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1163 		    time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1164 			RT6_TRACE("aging clone %p\n", rt);
1165 			return -1;
1166 		} else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1167 			   (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1168 			RT6_TRACE("purging route %p via non-router but gateway\n",
1169 				  rt);
1170 			return -1;
1171 		}
1172 		gc_args.more++;
1173 	}
1174 
1175 	return 0;
1176 }
1177 
1178 static DEFINE_SPINLOCK(fib6_gc_lock);
1179 
1180 void fib6_run_gc(unsigned long dummy)
1181 {
1182 	if (dummy != ~0UL) {
1183 		spin_lock_bh(&fib6_gc_lock);
1184 		gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1185 	} else {
1186 		local_bh_disable();
1187 		if (!spin_trylock(&fib6_gc_lock)) {
1188 			mod_timer(&ip6_fib_timer, jiffies + HZ);
1189 			local_bh_enable();
1190 			return;
1191 		}
1192 		gc_args.timeout = ip6_rt_gc_interval;
1193 	}
1194 	gc_args.more = 0;
1195 
1196 
1197 	write_lock_bh(&rt6_lock);
1198 	ndisc_dst_gc(&gc_args.more);
1199 	fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1200 	write_unlock_bh(&rt6_lock);
1201 
1202 	if (gc_args.more)
1203 		mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1204 	else {
1205 		del_timer(&ip6_fib_timer);
1206 		ip6_fib_timer.expires = 0;
1207 	}
1208 	spin_unlock_bh(&fib6_gc_lock);
1209 }
1210 
1211 void __init fib6_init(void)
1212 {
1213 	fib6_node_kmem = kmem_cache_create("fib6_nodes",
1214 					   sizeof(struct fib6_node),
1215 					   0, SLAB_HWCACHE_ALIGN,
1216 					   NULL, NULL);
1217 	if (!fib6_node_kmem)
1218 		panic("cannot create fib6_nodes cache");
1219 }
1220 
1221 void fib6_gc_cleanup(void)
1222 {
1223 	del_timer(&ip6_fib_timer);
1224 	kmem_cache_destroy(fib6_node_kmem);
1225 }
1226