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