xref: /freebsd/sys/net/radix.c (revision 0c43d89a0d8e976ca494d4837f4c1f3734d2c300)
1 /*
2  * Copyright (c) 1988, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)radix.c	8.2 (Berkeley) 1/4/94
34  * $Id$
35  */
36 
37 /*
38  * Routines to build and maintain radix trees for routing lookups.
39  */
40 #ifndef RNF_NORMAL
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #define	M_DONTWAIT M_NOWAIT
45 #ifdef	KERNEL
46 #include <sys/domain.h>
47 #endif
48 #endif
49 
50 #include <net/radix.h>
51 
52 int	max_keylen;
53 struct radix_mask *rn_mkfreelist;
54 struct radix_node_head *mask_rnhead;
55 static int gotOddMasks;
56 static char *maskedKey;
57 static char *rn_zeros, *rn_ones;
58 
59 #define rn_masktop (mask_rnhead->rnh_treetop)
60 #undef Bcmp
61 #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
62 /*
63  * The data structure for the keys is a radix tree with one way
64  * branching removed.  The index rn_b at an internal node n represents a bit
65  * position to be tested.  The tree is arranged so that all descendants
66  * of a node n have keys whose bits all agree up to position rn_b - 1.
67  * (We say the index of n is rn_b.)
68  *
69  * There is at least one descendant which has a one bit at position rn_b,
70  * and at least one with a zero there.
71  *
72  * A route is determined by a pair of key and mask.  We require that the
73  * bit-wise logical and of the key and mask to be the key.
74  * We define the index of a route to associated with the mask to be
75  * the first bit number in the mask where 0 occurs (with bit number 0
76  * representing the highest order bit).
77  *
78  * We say a mask is normal if every bit is 0, past the index of the mask.
79  * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
80  * and m is a normal mask, then the route applies to every descendant of n.
81  * If the index(m) < rn_b, this implies the trailing last few bits of k
82  * before bit b are all 0, (and hence consequently true of every descendant
83  * of n), so the route applies to all descendants of the node as well.
84  *
85  * The present version of the code makes no use of normal routes,
86  * but similar logic shows that a non-normal mask m such that
87  * index(m) <= index(n) could potentially apply to many children of n.
88  * Thus, for each non-host route, we attach its mask to a list at an internal
89  * node as high in the tree as we can go.
90  */
91 
92 struct radix_node *
93 rn_search(v_arg, head)
94 	void *v_arg;
95 	struct radix_node *head;
96 {
97 	register struct radix_node *x;
98 	register caddr_t v;
99 
100 	for (x = head, v = v_arg; x->rn_b >= 0;) {
101 		if (x->rn_bmask & v[x->rn_off])
102 			x = x->rn_r;
103 		else
104 			x = x->rn_l;
105 	}
106 	return (x);
107 };
108 
109 struct radix_node *
110 rn_search_m(v_arg, head, m_arg)
111 	struct radix_node *head;
112 	void *v_arg, *m_arg;
113 {
114 	register struct radix_node *x;
115 	register caddr_t v = v_arg, m = m_arg;
116 
117 	for (x = head; x->rn_b >= 0;) {
118 		if ((x->rn_bmask & m[x->rn_off]) &&
119 		    (x->rn_bmask & v[x->rn_off]))
120 			x = x->rn_r;
121 		else
122 			x = x->rn_l;
123 	}
124 	return x;
125 };
126 
127 int
128 rn_refines(m_arg, n_arg)
129 	void *m_arg, *n_arg;
130 {
131 	register caddr_t m = m_arg, n = n_arg;
132 	register caddr_t lim, lim2 = lim = n + *(u_char *)n;
133 	int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
134 	int masks_are_equal = 1;
135 
136 	if (longer > 0)
137 		lim -= longer;
138 	while (n < lim) {
139 		if (*n & ~(*m))
140 			return 0;
141 		if (*n++ != *m++)
142 			masks_are_equal = 0;
143 
144 	}
145 	while (n < lim2)
146 		if (*n++)
147 			return 0;
148 	if (masks_are_equal && (longer < 0))
149 		for (lim2 = m - longer; m < lim2; )
150 			if (*m++)
151 				return 1;
152 	return (!masks_are_equal);
153 }
154 
155 
156 struct radix_node *
157 rn_match(v_arg, head)
158 	void *v_arg;
159 	struct radix_node_head *head;
160 {
161 	caddr_t v = v_arg;
162 	register struct radix_node *t = head->rnh_treetop, *x;
163 	register caddr_t cp = v, cp2, cp3;
164 	caddr_t cplim, mstart;
165 	struct radix_node *saved_t, *top = t;
166 	int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
167 
168 	/*
169 	 * Open code rn_search(v, top) to avoid overhead of extra
170 	 * subroutine call.
171 	 */
172 	for (; t->rn_b >= 0; ) {
173 		if (t->rn_bmask & cp[t->rn_off])
174 			t = t->rn_r;
175 		else
176 			t = t->rn_l;
177 	}
178 	/*
179 	 * See if we match exactly as a host destination
180 	 */
181 	cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
182 	for (; cp < cplim; cp++, cp2++)
183 		if (*cp != *cp2)
184 			goto on1;
185 	/*
186 	 * This extra grot is in case we are explicitly asked
187 	 * to look up the default.  Ugh!
188 	 */
189 	if ((t->rn_flags & RNF_ROOT) && t->rn_dupedkey)
190 		t = t->rn_dupedkey;
191 	return t;
192 on1:
193 	matched_off = cp - v;
194 	saved_t = t;
195 	do {
196 	    if (t->rn_mask) {
197 		/*
198 		 * Even if we don't match exactly as a hosts;
199 		 * we may match if the leaf we wound up at is
200 		 * a route to a net.
201 		 */
202 		cp3 = matched_off + t->rn_mask;
203 		cp2 = matched_off + t->rn_key;
204 		for (; cp < cplim; cp++)
205 			if ((*cp2++ ^ *cp) & *cp3++)
206 				break;
207 		if (cp == cplim)
208 			return t;
209 		cp = matched_off + v;
210 	    }
211 	} while (t = t->rn_dupedkey);
212 	t = saved_t;
213 	/* start searching up the tree */
214 	do {
215 		register struct radix_mask *m;
216 		t = t->rn_p;
217 		if (m = t->rn_mklist) {
218 			/*
219 			 * After doing measurements here, it may
220 			 * turn out to be faster to open code
221 			 * rn_search_m here instead of always
222 			 * copying and masking.
223 			 */
224 			off = min(t->rn_off, matched_off);
225 			mstart = maskedKey + off;
226 			do {
227 				cp2 = mstart;
228 				cp3 = m->rm_mask + off;
229 				for (cp = v + off; cp < cplim;)
230 					*cp2++ =  *cp++ & *cp3++;
231 				x = rn_search(maskedKey, t);
232 				while (x && x->rn_mask != m->rm_mask)
233 					x = x->rn_dupedkey;
234 				if (x &&
235 				    (Bcmp(mstart, x->rn_key + off,
236 					vlen - off) == 0))
237 					    return x;
238 			} while (m = m->rm_mklist);
239 		}
240 	} while (t != top);
241 	return 0;
242 };
243 
244 #ifdef RN_DEBUG
245 int	rn_nodenum;
246 struct	radix_node *rn_clist;
247 int	rn_saveinfo;
248 int	rn_debug =  1;
249 #endif
250 
251 struct radix_node *
252 rn_newpair(v, b, nodes)
253 	void *v;
254 	int b;
255 	struct radix_node nodes[2];
256 {
257 	register struct radix_node *tt = nodes, *t = tt + 1;
258 	t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7);
259 	t->rn_l = tt; t->rn_off = b >> 3;
260 	tt->rn_b = -1; tt->rn_key = (caddr_t)v; tt->rn_p = t;
261 	tt->rn_flags = t->rn_flags = RNF_ACTIVE;
262 #ifdef RN_DEBUG
263 	tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
264 	tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
265 #endif
266 	return t;
267 }
268 
269 struct radix_node *
270 rn_insert(v_arg, head, dupentry, nodes)
271 	void *v_arg;
272 	struct radix_node_head *head;
273 	int *dupentry;
274 	struct radix_node nodes[2];
275 {
276 	caddr_t v = v_arg;
277 	struct radix_node *top = head->rnh_treetop;
278 	int head_off = top->rn_off, vlen = (int)*((u_char *)v);
279 	register struct radix_node *t = rn_search(v_arg, top);
280 	register caddr_t cp = v + head_off;
281 	register int b;
282 	struct radix_node *tt;
283     	/*
284 	 *find first bit at which v and t->rn_key differ
285 	 */
286     {
287 	register caddr_t cp2 = t->rn_key + head_off;
288 	register int cmp_res;
289 	caddr_t cplim = v + vlen;
290 
291 	while (cp < cplim)
292 		if (*cp2++ != *cp++)
293 			goto on1;
294 	*dupentry = 1;
295 	return t;
296 on1:
297 	*dupentry = 0;
298 	cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
299 	for (b = (cp - v) << 3; cmp_res; b--)
300 		cmp_res >>= 1;
301     }
302     {
303 	register struct radix_node *p, *x = top;
304 	cp = v;
305 	do {
306 		p = x;
307 		if (cp[x->rn_off] & x->rn_bmask)
308 			x = x->rn_r;
309 		else x = x->rn_l;
310 	} while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
311 #ifdef RN_DEBUG
312 	if (rn_debug)
313 		printf("Going In:\n"), traverse(p);
314 #endif
315 	t = rn_newpair(v_arg, b, nodes); tt = t->rn_l;
316 	if ((cp[p->rn_off] & p->rn_bmask) == 0)
317 		p->rn_l = t;
318 	else
319 		p->rn_r = t;
320 	x->rn_p = t; t->rn_p = p; /* frees x, p as temp vars below */
321 	if ((cp[t->rn_off] & t->rn_bmask) == 0) {
322 		t->rn_r = x;
323 	} else {
324 		t->rn_r = tt; t->rn_l = x;
325 	}
326 #ifdef RN_DEBUG
327 	if (rn_debug)
328 		printf("Coming out:\n"), traverse(p);
329 #endif
330     }
331 	return (tt);
332 }
333 
334 struct radix_node *
335 rn_addmask(n_arg, search, skip)
336 	int search, skip;
337 	void *n_arg;
338 {
339 	caddr_t netmask = (caddr_t)n_arg;
340 	register struct radix_node *x;
341 	register caddr_t cp, cplim;
342 	register int b, mlen, j;
343 	int maskduplicated;
344 
345 	mlen = *(u_char *)netmask;
346 	if (search) {
347 		x = rn_search(netmask, rn_masktop);
348 		mlen = *(u_char *)netmask;
349 		if (Bcmp(netmask, x->rn_key, mlen) == 0)
350 			return (x);
351 	}
352 	R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
353 	if (x == 0)
354 		return (0);
355 	Bzero(x, max_keylen + 2 * sizeof (*x));
356 	cp = (caddr_t)(x + 2);
357 	Bcopy(netmask, cp, mlen);
358 	netmask = cp;
359 	x = rn_insert(netmask, mask_rnhead, &maskduplicated, x);
360 	/*
361 	 * Calculate index of mask.
362 	 */
363 	cplim = netmask + mlen;
364 	for (cp = netmask + skip; cp < cplim; cp++)
365 		if (*(u_char *)cp != 0xff)
366 			break;
367 	b = (cp - netmask) << 3;
368 	if (cp != cplim) {
369 		if (*cp != 0) {
370 			gotOddMasks = 1;
371 			for (j = 0x80; j; b++, j >>= 1)
372 				if ((j & *cp) == 0)
373 					break;
374 		}
375 	}
376 	x->rn_b = -1 - b;
377 	return (x);
378 }
379 
380 struct radix_node *
381 rn_addroute(v_arg, n_arg, head, treenodes)
382 	void *v_arg, *n_arg;
383 	struct radix_node_head *head;
384 	struct radix_node treenodes[2];
385 {
386 	caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
387 	register struct radix_node *t, *x = 0, *tt;
388 	struct radix_node *saved_tt, *top = head->rnh_treetop;
389 	short b = 0, b_leaf;
390 	int mlen, keyduplicated;
391 	caddr_t cplim;
392 	struct radix_mask *m, **mp;
393 
394 	/*
395 	 * In dealing with non-contiguous masks, there may be
396 	 * many different routes which have the same mask.
397 	 * We will find it useful to have a unique pointer to
398 	 * the mask to speed avoiding duplicate references at
399 	 * nodes and possibly save time in calculating indices.
400 	 */
401 	if (netmask)  {
402 		x = rn_search(netmask, rn_masktop);
403 		mlen = *(u_char *)netmask;
404 		if (Bcmp(netmask, x->rn_key, mlen) != 0) {
405 			x = rn_addmask(netmask, 0, top->rn_off);
406 			if (x == 0)
407 				return (0);
408 		}
409 		netmask = x->rn_key;
410 		b = -1 - x->rn_b;
411 	}
412 	/*
413 	 * Deal with duplicated keys: attach node to previous instance
414 	 */
415 	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
416 	if (keyduplicated) {
417 		do {
418 			if (tt->rn_mask == netmask)
419 				return (0);
420 			t = tt;
421 			if (netmask == 0 ||
422 			    (tt->rn_mask && rn_refines(netmask, tt->rn_mask)))
423 				break;
424 		} while (tt = tt->rn_dupedkey);
425 		/*
426 		 * If the mask is not duplicated, we wouldn't
427 		 * find it among possible duplicate key entries
428 		 * anyway, so the above test doesn't hurt.
429 		 *
430 		 * We sort the masks for a duplicated key the same way as
431 		 * in a masklist -- most specific to least specific.
432 		 * This may require the unfortunate nuisance of relocating
433 		 * the head of the list.
434 		 */
435 		if (tt && t == saved_tt) {
436 			struct	radix_node *xx = x;
437 			/* link in at head of list */
438 			(tt = treenodes)->rn_dupedkey = t;
439 			tt->rn_flags = t->rn_flags;
440 			tt->rn_p = x = t->rn_p;
441 			if (x->rn_l == t) x->rn_l = tt; else x->rn_r = tt;
442 			saved_tt = tt; x = xx;
443 		} else {
444 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
445 			t->rn_dupedkey = tt;
446 		}
447 #ifdef RN_DEBUG
448 		t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
449 		tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
450 #endif
451 		t = saved_tt;
452 		tt->rn_key = (caddr_t) v;
453 		tt->rn_b = -1;
454 		tt->rn_flags = t->rn_flags & ~RNF_ROOT;
455 	}
456 	/*
457 	 * Put mask in tree.
458 	 */
459 	if (netmask) {
460 		tt->rn_mask = netmask;
461 		tt->rn_b = x->rn_b;
462 	}
463 	t = saved_tt->rn_p;
464 	b_leaf = -1 - t->rn_b;
465 	if (t->rn_r == saved_tt) x = t->rn_l; else x = t->rn_r;
466 	/* Promote general routes from below */
467 	if (x->rn_b < 0) {
468 		if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
469 			MKGet(m);
470 			if (m) {
471 				Bzero(m, sizeof *m);
472 				m->rm_b = x->rn_b;
473 				m->rm_mask = x->rn_mask;
474 				x->rn_mklist = t->rn_mklist = m;
475 			}
476 		}
477 	} else if (x->rn_mklist) {
478 		/*
479 		 * Skip over masks whose index is > that of new node
480 		 */
481 		for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist)
482 			if (m->rm_b >= b_leaf)
483 				break;
484 		t->rn_mklist = m; *mp = 0;
485 	}
486 	/* Add new route to highest possible ancestor's list */
487 	if ((netmask == 0) || (b > t->rn_b ))
488 		return tt; /* can't lift at all */
489 	b_leaf = tt->rn_b;
490 	do {
491 		x = t;
492 		t = t->rn_p;
493 	} while (b <= t->rn_b && x != top);
494 	/*
495 	 * Search through routes associated with node to
496 	 * insert new route according to index.
497 	 * For nodes of equal index, place more specific
498 	 * masks first.
499 	 */
500 	cplim = netmask + mlen;
501 	for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist) {
502 		if (m->rm_b < b_leaf)
503 			continue;
504 		if (m->rm_b > b_leaf)
505 			break;
506 		if (m->rm_mask == netmask) {
507 			m->rm_refs++;
508 			tt->rn_mklist = m;
509 			return tt;
510 		}
511 		if (rn_refines(netmask, m->rm_mask))
512 			break;
513 	}
514 	MKGet(m);
515 	if (m == 0) {
516 		printf("Mask for route not entered\n");
517 		return (tt);
518 	}
519 	Bzero(m, sizeof *m);
520 	m->rm_b = b_leaf;
521 	m->rm_mask = netmask;
522 	m->rm_mklist = *mp;
523 	*mp = m;
524 	tt->rn_mklist = m;
525 	return tt;
526 }
527 
528 struct radix_node *
529 rn_delete(v_arg, netmask_arg, head)
530 	void *v_arg, *netmask_arg;
531 	struct radix_node_head *head;
532 {
533 	register struct radix_node *t, *p, *x, *tt;
534 	struct radix_mask *m, *saved_m, **mp;
535 	struct radix_node *dupedkey, *saved_tt, *top;
536 	caddr_t v, netmask;
537 	int b, head_off, vlen;
538 
539 	v = v_arg;
540 	netmask = netmask_arg;
541 	x = head->rnh_treetop;
542 	tt = rn_search(v, x);
543 	head_off = x->rn_off;
544 	vlen =  *(u_char *)v;
545 	saved_tt = tt;
546 	top = x;
547 	if (tt == 0 ||
548 	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
549 		return (0);
550 	/*
551 	 * Delete our route from mask lists.
552 	 */
553 	if (dupedkey = tt->rn_dupedkey) {
554 		if (netmask)
555 			netmask = rn_search(netmask, rn_masktop)->rn_key;
556 		while (tt->rn_mask != netmask)
557 			if ((tt = tt->rn_dupedkey) == 0)
558 				return (0);
559 	}
560 	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
561 		goto on1;
562 	if (m->rm_mask != tt->rn_mask) {
563 		printf("rn_delete: inconsistent annotation\n");
564 		goto on1;
565 	}
566 	if (--m->rm_refs >= 0)
567 		goto on1;
568 	b = -1 - tt->rn_b;
569 	t = saved_tt->rn_p;
570 	if (b > t->rn_b)
571 		goto on1; /* Wasn't lifted at all */
572 	do {
573 		x = t;
574 		t = t->rn_p;
575 	} while (b <= t->rn_b && x != top);
576 	for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist)
577 		if (m == saved_m) {
578 			*mp = m->rm_mklist;
579 			MKFree(m);
580 			break;
581 		}
582 	if (m == 0)
583 		printf("rn_delete: couldn't find our annotation\n");
584 on1:
585 	/*
586 	 * Eliminate us from tree
587 	 */
588 	if (tt->rn_flags & RNF_ROOT)
589 		return (0);
590 #ifdef RN_DEBUG
591 	/* Get us out of the creation list */
592 	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
593 	if (t) t->rn_ybro = tt->rn_ybro;
594 #endif
595 	t = tt->rn_p;
596 	if (dupedkey) {
597 		if (tt == saved_tt) {
598 			x = dupedkey; x->rn_p = t;
599 			if (t->rn_l == tt) t->rn_l = x; else t->rn_r = x;
600 		} else {
601 			for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
602 				p = p->rn_dupedkey;
603 			if (p) p->rn_dupedkey = tt->rn_dupedkey;
604 			else printf("rn_delete: couldn't find us\n");
605 		}
606 		t = tt + 1;
607 		if  (t->rn_flags & RNF_ACTIVE) {
608 #ifndef RN_DEBUG
609 			*++x = *t; p = t->rn_p;
610 #else
611 			b = t->rn_info; *++x = *t; t->rn_info = b; p = t->rn_p;
612 #endif
613 			if (p->rn_l == t) p->rn_l = x; else p->rn_r = x;
614 			x->rn_l->rn_p = x; x->rn_r->rn_p = x;
615 		}
616 		goto out;
617 	}
618 	if (t->rn_l == tt) x = t->rn_r; else x = t->rn_l;
619 	p = t->rn_p;
620 	if (p->rn_r == t) p->rn_r = x; else p->rn_l = x;
621 	x->rn_p = p;
622 	/*
623 	 * Demote routes attached to us.
624 	 */
625 	if (t->rn_mklist) {
626 		if (x->rn_b >= 0) {
627 			for (mp = &x->rn_mklist; m = *mp;)
628 				mp = &m->rm_mklist;
629 			*mp = t->rn_mklist;
630 		} else {
631 			for (m = t->rn_mklist; m;) {
632 				struct radix_mask *mm = m->rm_mklist;
633 				if (m == x->rn_mklist && (--(m->rm_refs) < 0)) {
634 					x->rn_mklist = 0;
635 					MKFree(m);
636 				} else
637 					printf("%s %x at %x\n",
638 					    "rn_delete: Orphaned Mask", m, x);
639 				m = mm;
640 			}
641 		}
642 	}
643 	/*
644 	 * We may be holding an active internal node in the tree.
645 	 */
646 	x = tt + 1;
647 	if (t != x) {
648 #ifndef RN_DEBUG
649 		*t = *x;
650 #else
651 		b = t->rn_info; *t = *x; t->rn_info = b;
652 #endif
653 		t->rn_l->rn_p = t; t->rn_r->rn_p = t;
654 		p = x->rn_p;
655 		if (p->rn_l == x) p->rn_l = t; else p->rn_r = t;
656 	}
657 out:
658 	tt->rn_flags &= ~RNF_ACTIVE;
659 	tt[1].rn_flags &= ~RNF_ACTIVE;
660 	return (tt);
661 }
662 
663 int
664 rn_walktree(h, f, w)
665 	struct radix_node_head *h;
666 	register int (*f)();
667 	void *w;
668 {
669 	int error;
670 	struct radix_node *base, *next;
671 	register struct radix_node *rn = h->rnh_treetop;
672 	/*
673 	 * This gets complicated because we may delete the node
674 	 * while applying the function f to it, so we need to calculate
675 	 * the successor node in advance.
676 	 */
677 	/* First time through node, go left */
678 	while (rn->rn_b >= 0)
679 		rn = rn->rn_l;
680 	for (;;) {
681 		base = rn;
682 		/* If at right child go back up, otherwise, go right */
683 		while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
684 			rn = rn->rn_p;
685 		/* Find the next *leaf* since next node might vanish, too */
686 		for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
687 			rn = rn->rn_l;
688 		next = rn;
689 		/* Process leaves */
690 		while (rn = base) {
691 			base = rn->rn_dupedkey;
692 			if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
693 				return (error);
694 		}
695 		rn = next;
696 		if (rn->rn_flags & RNF_ROOT)
697 			return (0);
698 	}
699 	/* NOTREACHED */
700 }
701 
702 int
703 rn_inithead(head, off)
704 	void **head;
705 	int off;
706 {
707 	register struct radix_node_head *rnh;
708 	register struct radix_node *t, *tt, *ttt;
709 	if (*head)
710 		return (1);
711 	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
712 	if (rnh == 0)
713 		return (0);
714 	Bzero(rnh, sizeof (*rnh));
715 	*head = rnh;
716 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
717 	ttt = rnh->rnh_nodes + 2;
718 	t->rn_r = ttt;
719 	t->rn_p = t;
720 	tt = t->rn_l;
721 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
722 	tt->rn_b = -1 - off;
723 	*ttt = *tt;
724 	ttt->rn_key = rn_ones;
725 	rnh->rnh_addaddr = rn_addroute;
726 	rnh->rnh_deladdr = rn_delete;
727 	rnh->rnh_matchaddr = rn_match;
728 	rnh->rnh_walktree = rn_walktree;
729 	rnh->rnh_treetop = t;
730 	return (1);
731 }
732 
733 void
734 rn_init()
735 {
736 	char *cp, *cplim;
737 #ifdef KERNEL
738 	struct domain *dom;
739 
740 	for (dom = domains; dom; dom = dom->dom_next)
741 		if (dom->dom_maxrtkey > max_keylen)
742 			max_keylen = dom->dom_maxrtkey;
743 #endif
744 	if (max_keylen == 0) {
745 		printf("rn_init: radix functions require max_keylen be set\n");
746 		return;
747 	}
748 	R_Malloc(rn_zeros, char *, 3 * max_keylen);
749 	if (rn_zeros == NULL)
750 		panic("rn_init");
751 	Bzero(rn_zeros, 3 * max_keylen);
752 	rn_ones = cp = rn_zeros + max_keylen;
753 	maskedKey = cplim = rn_ones + max_keylen;
754 	while (cp < cplim)
755 		*cp++ = -1;
756 	if (rn_inithead((void **)&mask_rnhead, 0) == 0)
757 		panic("rn_init 2");
758 }
759