xref: /netbsd/sys/net/radix.c (revision 6550d01e)
1 /*	$NetBSD: radix.c,v 1.43 2009/05/27 17:46:50 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)radix.c	8.6 (Berkeley) 10/17/95
32  */
33 
34 /*
35  * Routines to build and maintain radix trees for routing lookups.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: radix.c,v 1.43 2009/05/27 17:46:50 pooka Exp $");
40 
41 #ifndef _NET_RADIX_H_
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/kmem.h>
45 #ifdef	_KERNEL
46 #include "opt_inet.h"
47 
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #define	M_DONTWAIT M_NOWAIT
51 #include <sys/domain.h>
52 #else
53 #include <stdlib.h>
54 #endif
55 #include <machine/stdarg.h>
56 #include <sys/syslog.h>
57 #include <net/radix.h>
58 #endif
59 
60 typedef void (*rn_printer_t)(void *, const char *fmt, ...);
61 
62 int	max_keylen;
63 struct radix_mask *rn_mkfreelist;
64 struct radix_node_head *mask_rnhead;
65 static char *addmask_key;
66 static const char normal_chars[] =
67     {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
68 static char *rn_zeros, *rn_ones;
69 
70 #define rn_masktop (mask_rnhead->rnh_treetop)
71 
72 static int rn_satisfies_leaf(const char *, struct radix_node *, int);
73 static int rn_lexobetter(const void *, const void *);
74 static struct radix_mask *rn_new_radix_mask(struct radix_node *,
75     struct radix_mask *);
76 static struct radix_node *rn_walknext(struct radix_node *, rn_printer_t,
77     void *);
78 static struct radix_node *rn_walkfirst(struct radix_node *, rn_printer_t,
79     void *);
80 static void rn_nodeprint(struct radix_node *, rn_printer_t, void *,
81     const char *);
82 
83 #define	SUBTREE_OPEN	"[ "
84 #define	SUBTREE_CLOSE	" ]"
85 
86 #ifdef RN_DEBUG
87 static void rn_treeprint(struct radix_node_head *, rn_printer_t, void *);
88 #endif /* RN_DEBUG */
89 
90 /*
91  * The data structure for the keys is a radix tree with one way
92  * branching removed.  The index rn_b at an internal node n represents a bit
93  * position to be tested.  The tree is arranged so that all descendants
94  * of a node n have keys whose bits all agree up to position rn_b - 1.
95  * (We say the index of n is rn_b.)
96  *
97  * There is at least one descendant which has a one bit at position rn_b,
98  * and at least one with a zero there.
99  *
100  * A route is determined by a pair of key and mask.  We require that the
101  * bit-wise logical and of the key and mask to be the key.
102  * We define the index of a route to associated with the mask to be
103  * the first bit number in the mask where 0 occurs (with bit number 0
104  * representing the highest order bit).
105  *
106  * We say a mask is normal if every bit is 0, past the index of the mask.
107  * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
108  * and m is a normal mask, then the route applies to every descendant of n.
109  * If the index(m) < rn_b, this implies the trailing last few bits of k
110  * before bit b are all 0, (and hence consequently true of every descendant
111  * of n), so the route applies to all descendants of the node as well.
112  *
113  * Similar logic shows that a non-normal mask m such that
114  * index(m) <= index(n) could potentially apply to many children of n.
115  * Thus, for each non-host route, we attach its mask to a list at an internal
116  * node as high in the tree as we can go.
117  *
118  * The present version of the code makes use of normal routes in short-
119  * circuiting an explicit mask and compare operation when testing whether
120  * a key satisfies a normal route, and also in remembering the unique leaf
121  * that governs a subtree.
122  */
123 
124 struct radix_node *
125 rn_search(
126 	const void *v_arg,
127 	struct radix_node *head)
128 {
129 	const u_char * const v = v_arg;
130 	struct radix_node *x;
131 
132 	for (x = head; x->rn_b >= 0;) {
133 		if (x->rn_bmask & v[x->rn_off])
134 			x = x->rn_r;
135 		else
136 			x = x->rn_l;
137 	}
138 	return x;
139 }
140 
141 struct radix_node *
142 rn_search_m(
143 	const void *v_arg,
144 	struct radix_node *head,
145 	const void *m_arg)
146 {
147 	struct radix_node *x;
148 	const u_char * const v = v_arg;
149 	const u_char * const m = m_arg;
150 
151 	for (x = head; x->rn_b >= 0;) {
152 		if ((x->rn_bmask & m[x->rn_off]) &&
153 		    (x->rn_bmask & v[x->rn_off]))
154 			x = x->rn_r;
155 		else
156 			x = x->rn_l;
157 	}
158 	return x;
159 }
160 
161 int
162 rn_refines(
163 	const void *m_arg,
164 	const void *n_arg)
165 {
166 	const char *m = m_arg;
167 	const char *n = n_arg;
168 	const char *lim = n + *(const u_char *)n;
169 	const char *lim2 = lim;
170 	int longer = (*(const u_char *)n++) - (int)(*(const u_char *)m++);
171 	int masks_are_equal = 1;
172 
173 	if (longer > 0)
174 		lim -= longer;
175 	while (n < lim) {
176 		if (*n & ~(*m))
177 			return 0;
178 		if (*n++ != *m++)
179 			masks_are_equal = 0;
180 	}
181 	while (n < lim2)
182 		if (*n++)
183 			return 0;
184 	if (masks_are_equal && (longer < 0))
185 		for (lim2 = m - longer; m < lim2; )
186 			if (*m++)
187 				return 1;
188 	return !masks_are_equal;
189 }
190 
191 struct radix_node *
192 rn_lookup(
193 	const void *v_arg,
194 	const void *m_arg,
195 	struct radix_node_head *head)
196 {
197 	struct radix_node *x;
198 	const char *netmask = NULL;
199 
200 	if (m_arg) {
201 		if ((x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_off)) == 0)
202 			return NULL;
203 		netmask = x->rn_key;
204 	}
205 	x = rn_match(v_arg, head);
206 	if (x != NULL && netmask != NULL) {
207 		while (x != NULL && x->rn_mask != netmask)
208 			x = x->rn_dupedkey;
209 	}
210 	return x;
211 }
212 
213 static int
214 rn_satisfies_leaf(
215 	const char *trial,
216 	struct radix_node *leaf,
217 	int skip)
218 {
219 	const char *cp = trial;
220 	const char *cp2 = leaf->rn_key;
221 	const char *cp3 = leaf->rn_mask;
222 	const char *cplim;
223 	int length = min(*(const u_char *)cp, *(const u_char *)cp2);
224 
225 	if (cp3 == 0)
226 		cp3 = rn_ones;
227 	else
228 		length = min(length, *(const u_char *)cp3);
229 	cplim = cp + length; cp3 += skip; cp2 += skip;
230 	for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
231 		if ((*cp ^ *cp2) & *cp3)
232 			return 0;
233 	return 1;
234 }
235 
236 struct radix_node *
237 rn_match(
238 	const void *v_arg,
239 	struct radix_node_head *head)
240 {
241 	const char * const v = v_arg;
242 	struct radix_node *t = head->rnh_treetop;
243 	struct radix_node *top = t;
244 	struct radix_node *x;
245 	struct radix_node *saved_t;
246 	const char *cp = v;
247 	const char *cp2;
248 	const char *cplim;
249 	int off = t->rn_off;
250 	int vlen = *(const u_char *)cp;
251 	int matched_off;
252 	int test, b, rn_b;
253 
254 	/*
255 	 * Open code rn_search(v, top) to avoid overhead of extra
256 	 * subroutine call.
257 	 */
258 	for (; t->rn_b >= 0; ) {
259 		if (t->rn_bmask & cp[t->rn_off])
260 			t = t->rn_r;
261 		else
262 			t = t->rn_l;
263 	}
264 	/*
265 	 * See if we match exactly as a host destination
266 	 * or at least learn how many bits match, for normal mask finesse.
267 	 *
268 	 * It doesn't hurt us to limit how many bytes to check
269 	 * to the length of the mask, since if it matches we had a genuine
270 	 * match and the leaf we have is the most specific one anyway;
271 	 * if it didn't match with a shorter length it would fail
272 	 * with a long one.  This wins big for class B&C netmasks which
273 	 * are probably the most common case...
274 	 */
275 	if (t->rn_mask)
276 		vlen = *(const u_char *)t->rn_mask;
277 	cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
278 	for (; cp < cplim; cp++, cp2++)
279 		if (*cp != *cp2)
280 			goto on1;
281 	/*
282 	 * This extra grot is in case we are explicitly asked
283 	 * to look up the default.  Ugh!
284 	 */
285 	if ((t->rn_flags & RNF_ROOT) && t->rn_dupedkey)
286 		t = t->rn_dupedkey;
287 	return t;
288 on1:
289 	test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
290 	for (b = 7; (test >>= 1) > 0;)
291 		b--;
292 	matched_off = cp - v;
293 	b += matched_off << 3;
294 	rn_b = -1 - b;
295 	/*
296 	 * If there is a host route in a duped-key chain, it will be first.
297 	 */
298 	if ((saved_t = t)->rn_mask == 0)
299 		t = t->rn_dupedkey;
300 	for (; t; t = t->rn_dupedkey)
301 		/*
302 		 * Even if we don't match exactly as a host,
303 		 * we may match if the leaf we wound up at is
304 		 * a route to a net.
305 		 */
306 		if (t->rn_flags & RNF_NORMAL) {
307 			if (rn_b <= t->rn_b)
308 				return t;
309 		} else if (rn_satisfies_leaf(v, t, matched_off))
310 				return t;
311 	t = saved_t;
312 	/* start searching up the tree */
313 	do {
314 		struct radix_mask *m;
315 		t = t->rn_p;
316 		m = t->rn_mklist;
317 		if (m) {
318 			/*
319 			 * If non-contiguous masks ever become important
320 			 * we can restore the masking and open coding of
321 			 * the search and satisfaction test and put the
322 			 * calculation of "off" back before the "do".
323 			 */
324 			do {
325 				if (m->rm_flags & RNF_NORMAL) {
326 					if (rn_b <= m->rm_b)
327 						return m->rm_leaf;
328 				} else {
329 					off = min(t->rn_off, matched_off);
330 					x = rn_search_m(v, t, m->rm_mask);
331 					while (x && x->rn_mask != m->rm_mask)
332 						x = x->rn_dupedkey;
333 					if (x && rn_satisfies_leaf(v, x, off))
334 						return x;
335 				}
336 				m = m->rm_mklist;
337 			} while (m);
338 		}
339 	} while (t != top);
340 	return NULL;
341 }
342 
343 static void
344 rn_nodeprint(struct radix_node *rn, rn_printer_t printer, void *arg,
345     const char *delim)
346 {
347 	(*printer)(arg, "%s(%s%p: p<%p> l<%p> r<%p>)",
348 	    delim, ((void *)rn == arg) ? "*" : "", rn, rn->rn_p,
349 	    rn->rn_l, rn->rn_r);
350 }
351 
352 #ifdef RN_DEBUG
353 int	rn_debug =  1;
354 
355 static void
356 rn_dbg_print(void *arg, const char *fmt, ...)
357 {
358 	va_list ap;
359 
360 	va_start(ap, fmt);
361 	vlog(LOG_DEBUG, fmt, ap);
362 	va_end(ap);
363 }
364 
365 static void
366 rn_treeprint(struct radix_node_head *h, rn_printer_t printer, void *arg)
367 {
368 	struct radix_node *dup, *rn;
369 	const char *delim;
370 
371 	if (printer == NULL)
372 		return;
373 
374 	rn = rn_walkfirst(h->rnh_treetop, printer, arg);
375 	for (;;) {
376 		/* Process leaves */
377 		delim = "";
378 		for (dup = rn; dup != NULL; dup = dup->rn_dupedkey) {
379 			if ((dup->rn_flags & RNF_ROOT) != 0)
380 				continue;
381 			rn_nodeprint(dup, printer, arg, delim);
382 			delim = ", ";
383 		}
384 		rn = rn_walknext(rn, printer, arg);
385 		if (rn->rn_flags & RNF_ROOT)
386 			return;
387 	}
388 	/* NOTREACHED */
389 }
390 
391 #define	traverse(__head, __rn)	rn_treeprint((__head), rn_dbg_print, (__rn))
392 #endif /* RN_DEBUG */
393 
394 struct radix_node *
395 rn_newpair(
396 	const void *v,
397 	int b,
398 	struct radix_node nodes[2])
399 {
400 	struct radix_node *tt = nodes;
401 	struct radix_node *t = tt + 1;
402 	t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7);
403 	t->rn_l = tt; t->rn_off = b >> 3;
404 	tt->rn_b = -1; tt->rn_key = v; tt->rn_p = t;
405 	tt->rn_flags = t->rn_flags = RNF_ACTIVE;
406 	return t;
407 }
408 
409 struct radix_node *
410 rn_insert(
411 	const void *v_arg,
412 	struct radix_node_head *head,
413 	int *dupentry,
414 	struct radix_node nodes[2])
415 {
416 	struct radix_node *top = head->rnh_treetop;
417 	struct radix_node *t = rn_search(v_arg, top);
418 	struct radix_node *tt;
419 	const char *v = v_arg;
420 	int head_off = top->rn_off;
421 	int vlen = *((const u_char *)v);
422 	const char *cp = v + head_off;
423 	int b;
424     	/*
425 	 * Find first bit at which v and t->rn_key differ
426 	 */
427     {
428 	const char *cp2 = t->rn_key + head_off;
429 	const char *cplim = v + vlen;
430 	int cmp_res;
431 
432 	while (cp < cplim)
433 		if (*cp2++ != *cp++)
434 			goto on1;
435 	*dupentry = 1;
436 	return t;
437 on1:
438 	*dupentry = 0;
439 	cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
440 	for (b = (cp - v) << 3; cmp_res; b--)
441 		cmp_res >>= 1;
442     }
443     {
444 	struct radix_node *p, *x = top;
445 	cp = v;
446 	do {
447 		p = x;
448 		if (cp[x->rn_off] & x->rn_bmask)
449 			x = x->rn_r;
450 		else x = x->rn_l;
451 	} while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
452 #ifdef RN_DEBUG
453 	if (rn_debug)
454 		log(LOG_DEBUG, "%s: Going In:\n", __func__), traverse(head, p);
455 #endif
456 	t = rn_newpair(v_arg, b, nodes); tt = t->rn_l;
457 	if ((cp[p->rn_off] & p->rn_bmask) == 0)
458 		p->rn_l = t;
459 	else
460 		p->rn_r = t;
461 	x->rn_p = t; t->rn_p = p; /* frees x, p as temp vars below */
462 	if ((cp[t->rn_off] & t->rn_bmask) == 0) {
463 		t->rn_r = x;
464 	} else {
465 		t->rn_r = tt; t->rn_l = x;
466 	}
467 #ifdef RN_DEBUG
468 	if (rn_debug) {
469 		log(LOG_DEBUG, "%s: Coming Out:\n", __func__),
470 		    traverse(head, p);
471 	}
472 #endif /* RN_DEBUG */
473     }
474 	return tt;
475 }
476 
477 struct radix_node *
478 rn_addmask(
479 	const void *n_arg,
480 	int search,
481 	int skip)
482 {
483 	const char *netmask = n_arg;
484 	const char *cp;
485 	const char *cplim;
486 	struct radix_node *x;
487 	struct radix_node *saved_x;
488 	int b = 0, mlen, j;
489 	int maskduplicated, m0, isnormal;
490 	static int last_zeroed = 0;
491 
492 	if ((mlen = *(const u_char *)netmask) > max_keylen)
493 		mlen = max_keylen;
494 	if (skip == 0)
495 		skip = 1;
496 	if (mlen <= skip)
497 		return mask_rnhead->rnh_nodes;
498 	if (skip > 1)
499 		memmove(addmask_key + 1, rn_ones + 1, skip - 1);
500 	if ((m0 = mlen) > skip)
501 		memmove(addmask_key + skip, netmask + skip, mlen - skip);
502 	/*
503 	 * Trim trailing zeroes.
504 	 */
505 	for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
506 		cp--;
507 	mlen = cp - addmask_key;
508 	if (mlen <= skip) {
509 		if (m0 >= last_zeroed)
510 			last_zeroed = mlen;
511 		return mask_rnhead->rnh_nodes;
512 	}
513 	if (m0 < last_zeroed)
514 		memset(addmask_key + m0, 0, last_zeroed - m0);
515 	*addmask_key = last_zeroed = mlen;
516 	x = rn_search(addmask_key, rn_masktop);
517 	if (memcmp(addmask_key, x->rn_key, mlen) != 0)
518 		x = 0;
519 	if (x || search)
520 		return x;
521 	R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
522 	if ((saved_x = x) == NULL)
523 		return NULL;
524 	memset(x, 0, max_keylen + 2 * sizeof (*x));
525 	cp = netmask = (void *)(x + 2);
526 	memmove(x + 2, addmask_key, mlen);
527 	x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
528 	if (maskduplicated) {
529 		log(LOG_ERR, "rn_addmask: mask impossibly already in tree\n");
530 		Free(saved_x);
531 		return x;
532 	}
533 	/*
534 	 * Calculate index of mask, and check for normalcy.
535 	 */
536 	cplim = netmask + mlen; isnormal = 1;
537 	for (cp = netmask + skip; (cp < cplim) && *(const u_char *)cp == 0xff;)
538 		cp++;
539 	if (cp != cplim) {
540 		for (j = 0x80; (j & *cp) != 0; j >>= 1)
541 			b++;
542 		if (*cp != normal_chars[b] || cp != (cplim - 1))
543 			isnormal = 0;
544 	}
545 	b += (cp - netmask) << 3;
546 	x->rn_b = -1 - b;
547 	if (isnormal)
548 		x->rn_flags |= RNF_NORMAL;
549 	return x;
550 }
551 
552 static int	/* XXX: arbitrary ordering for non-contiguous masks */
553 rn_lexobetter(
554 	const void *m_arg,
555 	const void *n_arg)
556 {
557 	const u_char *mp = m_arg;
558 	const u_char *np = n_arg;
559 	const u_char *lim;
560 
561 	if (*mp > *np)
562 		return 1;  /* not really, but need to check longer one first */
563 	if (*mp == *np)
564 		for (lim = mp + *mp; mp < lim;)
565 			if (*mp++ > *np++)
566 				return 1;
567 	return 0;
568 }
569 
570 static struct radix_mask *
571 rn_new_radix_mask(
572 	struct radix_node *tt,
573 	struct radix_mask *next)
574 {
575 	struct radix_mask *m;
576 
577 	MKGet(m);
578 	if (m == NULL) {
579 		log(LOG_ERR, "Mask for route not entered\n");
580 		return NULL;
581 	}
582 	memset(m, 0, sizeof(*m));
583 	m->rm_b = tt->rn_b;
584 	m->rm_flags = tt->rn_flags;
585 	if (tt->rn_flags & RNF_NORMAL)
586 		m->rm_leaf = tt;
587 	else
588 		m->rm_mask = tt->rn_mask;
589 	m->rm_mklist = next;
590 	tt->rn_mklist = m;
591 	return m;
592 }
593 
594 struct radix_node *
595 rn_addroute(
596 	const void *v_arg,
597 	const void *n_arg,
598 	struct radix_node_head *head,
599 	struct radix_node treenodes[2])
600 {
601 	const char *v = v_arg, *netmask = n_arg;
602 	struct radix_node *t, *x = NULL, *tt;
603 	struct radix_node *saved_tt, *top = head->rnh_treetop;
604 	short b = 0, b_leaf = 0;
605 	int keyduplicated;
606 	const char *mmask;
607 	struct radix_mask *m, **mp;
608 
609 	/*
610 	 * In dealing with non-contiguous masks, there may be
611 	 * many different routes which have the same mask.
612 	 * We will find it useful to have a unique pointer to
613 	 * the mask to speed avoiding duplicate references at
614 	 * nodes and possibly save time in calculating indices.
615 	 */
616 	if (netmask != NULL) {
617 		if ((x = rn_addmask(netmask, 0, top->rn_off)) == NULL)
618 			return NULL;
619 		b_leaf = x->rn_b;
620 		b = -1 - x->rn_b;
621 		netmask = x->rn_key;
622 	}
623 	/*
624 	 * Deal with duplicated keys: attach node to previous instance
625 	 */
626 	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
627 	if (keyduplicated) {
628 		for (t = tt; tt != NULL; t = tt, tt = tt->rn_dupedkey) {
629 			if (tt->rn_mask == netmask)
630 				return NULL;
631 			if (netmask == NULL ||
632 			    (tt->rn_mask != NULL &&
633 			     (b_leaf < tt->rn_b || /* index(netmask) > node */
634 			       rn_refines(netmask, tt->rn_mask) ||
635 			       rn_lexobetter(netmask, tt->rn_mask))))
636 				break;
637 		}
638 		/*
639 		 * If the mask is not duplicated, we wouldn't
640 		 * find it among possible duplicate key entries
641 		 * anyway, so the above test doesn't hurt.
642 		 *
643 		 * We sort the masks for a duplicated key the same way as
644 		 * in a masklist -- most specific to least specific.
645 		 * This may require the unfortunate nuisance of relocating
646 		 * the head of the list.
647 		 *
648 		 * We also reverse, or doubly link the list through the
649 		 * parent pointer.
650 		 */
651 		if (tt == saved_tt) {
652 			struct	radix_node *xx = x;
653 			/* link in at head of list */
654 			(tt = treenodes)->rn_dupedkey = t;
655 			tt->rn_flags = t->rn_flags;
656 			tt->rn_p = x = t->rn_p;
657 			t->rn_p = tt;
658 			if (x->rn_l == t)
659 				x->rn_l = tt;
660 			else
661 				x->rn_r = tt;
662 			saved_tt = tt;
663 			x = xx;
664 		} else {
665 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
666 			t->rn_dupedkey = tt;
667 			tt->rn_p = t;
668 			if (tt->rn_dupedkey)
669 				tt->rn_dupedkey->rn_p = tt;
670 		}
671 		tt->rn_key = v;
672 		tt->rn_b = -1;
673 		tt->rn_flags = RNF_ACTIVE;
674 	}
675 	/*
676 	 * Put mask in tree.
677 	 */
678 	if (netmask != NULL) {
679 		tt->rn_mask = netmask;
680 		tt->rn_b = x->rn_b;
681 		tt->rn_flags |= x->rn_flags & RNF_NORMAL;
682 	}
683 	t = saved_tt->rn_p;
684 	if (keyduplicated)
685 		goto on2;
686 	b_leaf = -1 - t->rn_b;
687 	if (t->rn_r == saved_tt)
688 		x = t->rn_l;
689 	else
690 		x = t->rn_r;
691 	/* Promote general routes from below */
692 	if (x->rn_b < 0) {
693 		for (mp = &t->rn_mklist; x != NULL; x = x->rn_dupedkey) {
694 			if (x->rn_mask != NULL && x->rn_b >= b_leaf &&
695 			    x->rn_mklist == NULL) {
696 				*mp = m = rn_new_radix_mask(x, NULL);
697 				if (m != NULL)
698 					mp = &m->rm_mklist;
699 			}
700 		}
701 	} else if (x->rn_mklist != NULL) {
702 		/*
703 		 * Skip over masks whose index is > that of new node
704 		 */
705 		for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist)
706 			if (m->rm_b >= b_leaf)
707 				break;
708 		t->rn_mklist = m;
709 		*mp = NULL;
710 	}
711 on2:
712 	/* Add new route to highest possible ancestor's list */
713 	if (netmask == NULL || b > t->rn_b)
714 		return tt; /* can't lift at all */
715 	b_leaf = tt->rn_b;
716 	do {
717 		x = t;
718 		t = t->rn_p;
719 	} while (b <= t->rn_b && x != top);
720 	/*
721 	 * Search through routes associated with node to
722 	 * insert new route according to index.
723 	 * Need same criteria as when sorting dupedkeys to avoid
724 	 * double loop on deletion.
725 	 */
726 	for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist) {
727 		if (m->rm_b < b_leaf)
728 			continue;
729 		if (m->rm_b > b_leaf)
730 			break;
731 		if (m->rm_flags & RNF_NORMAL) {
732 			mmask = m->rm_leaf->rn_mask;
733 			if (tt->rn_flags & RNF_NORMAL) {
734 				log(LOG_ERR, "Non-unique normal route,"
735 				    " mask not entered\n");
736 				return tt;
737 			}
738 		} else
739 			mmask = m->rm_mask;
740 		if (mmask == netmask) {
741 			m->rm_refs++;
742 			tt->rn_mklist = m;
743 			return tt;
744 		}
745 		if (rn_refines(netmask, mmask) || rn_lexobetter(netmask, mmask))
746 			break;
747 	}
748 	*mp = rn_new_radix_mask(tt, *mp);
749 	return tt;
750 }
751 
752 struct radix_node *
753 rn_delete1(
754 	const void *v_arg,
755 	const void *netmask_arg,
756 	struct radix_node_head *head,
757 	struct radix_node *rn)
758 {
759 	struct radix_node *t, *p, *x, *tt;
760 	struct radix_mask *m, *saved_m, **mp;
761 	struct radix_node *dupedkey, *saved_tt, *top;
762 	const char *v, *netmask;
763 	int b, head_off, vlen;
764 
765 	v = v_arg;
766 	netmask = netmask_arg;
767 	x = head->rnh_treetop;
768 	tt = rn_search(v, x);
769 	head_off = x->rn_off;
770 	vlen =  *(const u_char *)v;
771 	saved_tt = tt;
772 	top = x;
773 	if (tt == NULL ||
774 	    memcmp(v + head_off, tt->rn_key + head_off, vlen - head_off) != 0)
775 		return NULL;
776 	/*
777 	 * Delete our route from mask lists.
778 	 */
779 	if (netmask != NULL) {
780 		if ((x = rn_addmask(netmask, 1, head_off)) == NULL)
781 			return NULL;
782 		netmask = x->rn_key;
783 		while (tt->rn_mask != netmask)
784 			if ((tt = tt->rn_dupedkey) == NULL)
785 				return NULL;
786 	}
787 	if (tt->rn_mask == NULL || (saved_m = m = tt->rn_mklist) == NULL)
788 		goto on1;
789 	if (tt->rn_flags & RNF_NORMAL) {
790 		if (m->rm_leaf != tt || m->rm_refs > 0) {
791 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
792 			return NULL;  /* dangling ref could cause disaster */
793 		}
794 	} else {
795 		if (m->rm_mask != tt->rn_mask) {
796 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
797 			goto on1;
798 		}
799 		if (--m->rm_refs >= 0)
800 			goto on1;
801 	}
802 	b = -1 - tt->rn_b;
803 	t = saved_tt->rn_p;
804 	if (b > t->rn_b)
805 		goto on1; /* Wasn't lifted at all */
806 	do {
807 		x = t;
808 		t = t->rn_p;
809 	} while (b <= t->rn_b && x != top);
810 	for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist) {
811 		if (m == saved_m) {
812 			*mp = m->rm_mklist;
813 			MKFree(m);
814 			break;
815 		}
816 	}
817 	if (m == NULL) {
818 		log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
819 		if (tt->rn_flags & RNF_NORMAL)
820 			return NULL; /* Dangling ref to us */
821 	}
822 on1:
823 	/*
824 	 * Eliminate us from tree
825 	 */
826 	if (tt->rn_flags & RNF_ROOT)
827 		return NULL;
828 #ifdef RN_DEBUG
829 	if (rn_debug)
830 		log(LOG_DEBUG, "%s: Going In:\n", __func__), traverse(head, tt);
831 #endif
832 	t = tt->rn_p;
833 	dupedkey = saved_tt->rn_dupedkey;
834 	if (dupedkey != NULL) {
835 		/*
836 		 * Here, tt is the deletion target, and
837 		 * saved_tt is the head of the dupedkey chain.
838 		 */
839 		if (tt == saved_tt) {
840 			x = dupedkey;
841 			x->rn_p = t;
842 			if (t->rn_l == tt)
843 				t->rn_l = x;
844 			else
845 				t->rn_r = x;
846 		} else {
847 			/* find node in front of tt on the chain */
848 			for (x = p = saved_tt;
849 			     p != NULL && p->rn_dupedkey != tt;)
850 				p = p->rn_dupedkey;
851 			if (p != NULL) {
852 				p->rn_dupedkey = tt->rn_dupedkey;
853 				if (tt->rn_dupedkey != NULL)
854 					tt->rn_dupedkey->rn_p = p;
855 			} else
856 				log(LOG_ERR, "rn_delete: couldn't find us\n");
857 		}
858 		t = tt + 1;
859 		if  (t->rn_flags & RNF_ACTIVE) {
860 			*++x = *t;
861 			p = t->rn_p;
862 			if (p->rn_l == t)
863 				p->rn_l = x;
864 			else
865 				p->rn_r = x;
866 			x->rn_l->rn_p = x;
867 			x->rn_r->rn_p = x;
868 		}
869 		goto out;
870 	}
871 	if (t->rn_l == tt)
872 		x = t->rn_r;
873 	else
874 		x = t->rn_l;
875 	p = t->rn_p;
876 	if (p->rn_r == t)
877 		p->rn_r = x;
878 	else
879 		p->rn_l = x;
880 	x->rn_p = p;
881 	/*
882 	 * Demote routes attached to us.
883 	 */
884 	if (t->rn_mklist == NULL)
885 		;
886 	else if (x->rn_b >= 0) {
887 		for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist)
888 			;
889 		*mp = t->rn_mklist;
890 	} else {
891 		/* If there are any key,mask pairs in a sibling
892 		   duped-key chain, some subset will appear sorted
893 		   in the same order attached to our mklist */
894 		for (m = t->rn_mklist;
895 		     m != NULL && x != NULL;
896 		     x = x->rn_dupedkey) {
897 			if (m == x->rn_mklist) {
898 				struct radix_mask *mm = m->rm_mklist;
899 				x->rn_mklist = NULL;
900 				if (--(m->rm_refs) < 0)
901 					MKFree(m);
902 				m = mm;
903 			}
904 		}
905 		if (m != NULL) {
906 			log(LOG_ERR, "rn_delete: Orphaned Mask %p at %p\n",
907 			    m, x);
908 		}
909 	}
910 	/*
911 	 * We may be holding an active internal node in the tree.
912 	 */
913 	x = tt + 1;
914 	if (t != x) {
915 		*t = *x;
916 		t->rn_l->rn_p = t;
917 		t->rn_r->rn_p = t;
918 		p = x->rn_p;
919 		if (p->rn_l == x)
920 			p->rn_l = t;
921 		else
922 			p->rn_r = t;
923 	}
924 out:
925 #ifdef RN_DEBUG
926 	if (rn_debug) {
927 		log(LOG_DEBUG, "%s: Coming Out:\n", __func__),
928 		    traverse(head, tt);
929 	}
930 #endif /* RN_DEBUG */
931 	tt->rn_flags &= ~RNF_ACTIVE;
932 	tt[1].rn_flags &= ~RNF_ACTIVE;
933 	return tt;
934 }
935 
936 struct radix_node *
937 rn_delete(
938 	const void *v_arg,
939 	const void *netmask_arg,
940 	struct radix_node_head *head)
941 {
942 	return rn_delete1(v_arg, netmask_arg, head, NULL);
943 }
944 
945 static struct radix_node *
946 rn_walknext(struct radix_node *rn, rn_printer_t printer, void *arg)
947 {
948 	/* If at right child go back up, otherwise, go right */
949 	while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0) {
950 		if (printer != NULL)
951 			(*printer)(arg, SUBTREE_CLOSE);
952 		rn = rn->rn_p;
953 	}
954 	if (printer)
955 		rn_nodeprint(rn->rn_p, printer, arg, "");
956 	/* Find the next *leaf* since next node might vanish, too */
957 	for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;) {
958 		if (printer != NULL)
959 			(*printer)(arg, SUBTREE_OPEN);
960 		rn = rn->rn_l;
961 	}
962 	return rn;
963 }
964 
965 static struct radix_node *
966 rn_walkfirst(struct radix_node *rn, rn_printer_t printer, void *arg)
967 {
968 	/* First time through node, go left */
969 	while (rn->rn_b >= 0) {
970 		if (printer != NULL)
971 			(*printer)(arg, SUBTREE_OPEN);
972 		rn = rn->rn_l;
973 	}
974 	return rn;
975 }
976 
977 int
978 rn_walktree(
979 	struct radix_node_head *h,
980 	int (*f)(struct radix_node *, void *),
981 	void *w)
982 {
983 	int error;
984 	struct radix_node *base, *next, *rn;
985 	/*
986 	 * This gets complicated because we may delete the node
987 	 * while applying the function f to it, so we need to calculate
988 	 * the successor node in advance.
989 	 */
990 	rn = rn_walkfirst(h->rnh_treetop, NULL, NULL);
991 	for (;;) {
992 		base = rn;
993 		next = rn_walknext(rn, NULL, NULL);
994 		/* Process leaves */
995 		while ((rn = base) != NULL) {
996 			base = rn->rn_dupedkey;
997 			if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
998 				return error;
999 		}
1000 		rn = next;
1001 		if (rn->rn_flags & RNF_ROOT)
1002 			return 0;
1003 	}
1004 	/* NOTREACHED */
1005 }
1006 
1007 struct delayinit {
1008 	void **head;
1009 	int off;
1010 	SLIST_ENTRY(delayinit) entries;
1011 };
1012 static SLIST_HEAD(, delayinit) delayinits = SLIST_HEAD_INITIALIZER(delayheads);
1013 static int radix_initialized;
1014 
1015 /*
1016  * Initialize a radix tree once radix is initialized.  Only for bootstrap.
1017  * Assume that no concurrency protection is necessary at this stage.
1018  */
1019 void
1020 rn_delayedinit(void **head, int off)
1021 {
1022 	struct delayinit *di;
1023 
1024 	KASSERT(radix_initialized == 0);
1025 
1026 	di = kmem_alloc(sizeof(*di), KM_SLEEP);
1027 	di->head = head;
1028 	di->off = off;
1029 	SLIST_INSERT_HEAD(&delayinits, di, entries);
1030 }
1031 
1032 int
1033 rn_inithead(void **head, int off)
1034 {
1035 	struct radix_node_head *rnh;
1036 
1037 	if (*head != NULL)
1038 		return 1;
1039 	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
1040 	if (rnh == NULL)
1041 		return 0;
1042 	*head = rnh;
1043 	return rn_inithead0(rnh, off);
1044 }
1045 
1046 int
1047 rn_inithead0(struct radix_node_head *rnh, int off)
1048 {
1049 	struct radix_node *t;
1050 	struct radix_node *tt;
1051 	struct radix_node *ttt;
1052 
1053 	memset(rnh, 0, sizeof(*rnh));
1054 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
1055 	ttt = rnh->rnh_nodes + 2;
1056 	t->rn_r = ttt;
1057 	t->rn_p = t;
1058 	tt = t->rn_l;
1059 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
1060 	tt->rn_b = -1 - off;
1061 	*ttt = *tt;
1062 	ttt->rn_key = rn_ones;
1063 	rnh->rnh_addaddr = rn_addroute;
1064 	rnh->rnh_deladdr = rn_delete;
1065 	rnh->rnh_matchaddr = rn_match;
1066 	rnh->rnh_lookup = rn_lookup;
1067 	rnh->rnh_treetop = t;
1068 	return 1;
1069 }
1070 
1071 void
1072 rn_init(void)
1073 {
1074 	char *cp, *cplim;
1075 	struct delayinit *di;
1076 #ifdef _KERNEL
1077 	struct domain *dp;
1078 
1079 	if (radix_initialized)
1080 		panic("radix already initialized");
1081 	radix_initialized = 1;
1082 
1083 	DOMAIN_FOREACH(dp) {
1084 		if (dp->dom_maxrtkey > max_keylen)
1085 			max_keylen = dp->dom_maxrtkey;
1086 	}
1087 #endif
1088 	if (max_keylen == 0) {
1089 		log(LOG_ERR,
1090 		    "rn_init: radix functions require max_keylen be set\n");
1091 		return;
1092 	}
1093 
1094 	R_Malloc(rn_zeros, char *, 3 * max_keylen);
1095 	if (rn_zeros == NULL)
1096 		panic("rn_init");
1097 	memset(rn_zeros, 0, 3 * max_keylen);
1098 	rn_ones = cp = rn_zeros + max_keylen;
1099 	addmask_key = cplim = rn_ones + max_keylen;
1100 	while (cp < cplim)
1101 		*cp++ = -1;
1102 	if (rn_inithead((void *)&mask_rnhead, 0) == 0)
1103 		panic("rn_init 2");
1104 
1105 	while ((di = SLIST_FIRST(&delayinits)) != NULL) {
1106 		if (!rn_inithead(di->head, di->off))
1107 			panic("delayed rn_inithead failed");
1108 		SLIST_REMOVE_HEAD(&delayinits, entries);
1109 		kmem_free(di, sizeof(*di));
1110 	}
1111 }
1112