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