xref: /original-bsd/sys/net/radix.c (revision 9a35f7df)
1 /*
2  * Copyright (c) 1988, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)radix.c	8.5 (Berkeley) 05/19/95
8  */
9 
10 /*
11  * Routines to build and maintain radix trees for routing lookups.
12  */
13 #ifndef _RADIX_H_
14 #include <sys/param.h>
15 #ifdef	KERNEL
16 #include <sys/systm.h>
17 #include <sys/malloc.h>
18 #define	M_DONTWAIT M_NOWAIT
19 #include <sys/domain.h>
20 #else
21 #include <stdlib.h>
22 #endif
23 #include <sys/syslog.h>
24 #include <net/radix.h>
25 #endif
26 
27 int	max_keylen;
28 struct radix_mask *rn_mkfreelist;
29 struct radix_node_head *mask_rnhead;
30 static char *addmask_key;
31 static char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
32 static char *rn_zeros, *rn_ones;
33 
34 #define rn_masktop (mask_rnhead->rnh_treetop)
35 #undef Bcmp
36 #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
37 /*
38  * The data structure for the keys is a radix tree with one way
39  * branching removed.  The index rn_b at an internal node n represents a bit
40  * position to be tested.  The tree is arranged so that all descendants
41  * of a node n have keys whose bits all agree up to position rn_b - 1.
42  * (We say the index of n is rn_b.)
43  *
44  * There is at least one descendant which has a one bit at position rn_b,
45  * and at least one with a zero there.
46  *
47  * A route is determined by a pair of key and mask.  We require that the
48  * bit-wise logical and of the key and mask to be the key.
49  * We define the index of a route to associated with the mask to be
50  * the first bit number in the mask where 0 occurs (with bit number 0
51  * representing the highest order bit).
52  *
53  * We say a mask is normal if every bit is 0, past the index of the mask.
54  * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
55  * and m is a normal mask, then the route applies to every descendant of n.
56  * If the index(m) < rn_b, this implies the trailing last few bits of k
57  * before bit b are all 0, (and hence consequently true of every descendant
58  * of n), so the route applies to all descendants of the node as well.
59  *
60  * Similar logic shows that a non-normal mask m such that
61  * index(m) <= index(n) could potentially apply to many children of n.
62  * Thus, for each non-host route, we attach its mask to a list at an internal
63  * node as high in the tree as we can go.
64  *
65  * The present version of the code makes use of normal routes in short-
66  * circuiting an explict mask and compare operation when testing whether
67  * a key satisfies a normal route, and also in remembering the unique leaf
68  * that governs a subtree.
69  */
70 
71 struct radix_node *
72 rn_search(v_arg, head)
73 	void *v_arg;
74 	struct radix_node *head;
75 {
76 	register struct radix_node *x;
77 	register caddr_t v;
78 
79 	for (x = head, v = v_arg; x->rn_b >= 0;) {
80 		if (x->rn_bmask & v[x->rn_off])
81 			x = x->rn_r;
82 		else
83 			x = x->rn_l;
84 	}
85 	return (x);
86 };
87 
88 struct radix_node *
89 rn_search_m(v_arg, head, m_arg)
90 	struct radix_node *head;
91 	void *v_arg, *m_arg;
92 {
93 	register struct radix_node *x;
94 	register caddr_t v = v_arg, m = m_arg;
95 
96 	for (x = head; x->rn_b >= 0;) {
97 		if ((x->rn_bmask & m[x->rn_off]) &&
98 		    (x->rn_bmask & v[x->rn_off]))
99 			x = x->rn_r;
100 		else
101 			x = x->rn_l;
102 	}
103 	return x;
104 };
105 
106 int
107 rn_refines(m_arg, n_arg)
108 	void *m_arg, *n_arg;
109 {
110 	register caddr_t m = m_arg, n = n_arg;
111 	register caddr_t lim, lim2 = lim = n + *(u_char *)n;
112 	int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
113 	int masks_are_equal = 1;
114 
115 	if (longer > 0)
116 		lim -= longer;
117 	while (n < lim) {
118 		if (*n & ~(*m))
119 			return 0;
120 		if (*n++ != *m++)
121 			masks_are_equal = 0;
122 	}
123 	while (n < lim2)
124 		if (*n++)
125 			return 0;
126 	if (masks_are_equal && (longer < 0))
127 		for (lim2 = m - longer; m < lim2; )
128 			if (*m++)
129 				return 1;
130 	return (!masks_are_equal);
131 }
132 
133 struct radix_node *
134 rn_lookup(v_arg, m_arg, head)
135 	void *v_arg, *m_arg;
136 	struct radix_node_head *head;
137 {
138 	register struct radix_node *x;
139 	caddr_t netmask = 0;
140 
141 	if (m_arg) {
142 		if ((x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_off)) == 0)
143 			return (0);
144 		netmask = x->rn_key;
145 	}
146 	x = rn_match(v_arg, head);
147 	if (x && netmask) {
148 		while (x && x->rn_mask != netmask)
149 			x = x->rn_dupedkey;
150 	}
151 	return x;
152 }
153 
154 static int
155 rn_satsifies_leaf(trial, leaf, skip)
156 	char *trial;
157 	register struct radix_node *leaf;
158 	int skip;
159 {
160 	register char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
161 	char *cplim;
162 	int length = min(*(u_char *)cp, *(u_char *)cp2);
163 
164 	if (cp3 == 0)
165 		cp3 = rn_ones;
166 	else
167 		length = min(length, *(u_char *)cp3);
168 	cplim = cp + length; cp3 += skip; cp2 += skip;
169 	for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
170 		if ((*cp ^ *cp2) & *cp3)
171 			return 0;
172 	return 1;
173 }
174 
175 struct radix_node *
176 rn_match(v_arg, head)
177 	void *v_arg;
178 	struct radix_node_head *head;
179 {
180 	caddr_t v = v_arg;
181 	register struct radix_node *t = head->rnh_treetop, *x;
182 	register caddr_t cp = v, cp2;
183 	caddr_t cplim;
184 	struct radix_node *saved_t, *top = t;
185 	int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
186 	register int test, b, rn_b;
187 
188 	/*
189 	 * Open code rn_search(v, top) to avoid overhead of extra
190 	 * subroutine call.
191 	 */
192 	for (; t->rn_b >= 0; ) {
193 		if (t->rn_bmask & cp[t->rn_off])
194 			t = t->rn_r;
195 		else
196 			t = t->rn_l;
197 	}
198 	/*
199 	 * See if we match exactly as a host destination
200 	 * or at least learn how many bits match, for normal mask finesse.
201 	 *
202 	 * It doesn't hurt us to limit how many bytes to check
203 	 * to the length of the mask, since if it matches we had a genuine
204 	 * match and the leaf we have is the most specific one anyway;
205 	 * if it didn't match with a shorter length it would fail
206 	 * with a long one.  This wins big for class B&C netmasks which
207 	 * are probably the most common case...
208 	 */
209 	if (t->rn_mask)
210 		vlen = *(u_char *)t->rn_mask;
211 	cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
212 	for (; cp < cplim; cp++, cp2++)
213 		if (*cp != *cp2)
214 			goto on1;
215 	/*
216 	 * This extra grot is in case we are explicitly asked
217 	 * to look up the default.  Ugh!
218 	 */
219 	if ((t->rn_flags & RNF_ROOT) && t->rn_dupedkey)
220 		t = t->rn_dupedkey;
221 	return t;
222 on1:
223 	test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
224 	for (b = 7; (test >>= 1) > 0;)
225 		b--;
226 	matched_off = cp - v;
227 	b += matched_off << 3;
228 	rn_b = -1 - b;
229 	/*
230 	 * If there is a host route in a duped-key chain, it will be first.
231 	 */
232 	if ((saved_t = t)->rn_mask == 0)
233 		t = t->rn_dupedkey;
234 	for (; t; t = t->rn_dupedkey)
235 		/*
236 		 * Even if we don't match exactly as a host,
237 		 * we may match if the leaf we wound up at is
238 		 * a route to a net.
239 		 */
240 		if (t->rn_flags & RNF_NORMAL) {
241 			if (rn_b <= t->rn_b)
242 				return t;
243 		} else if (rn_satsifies_leaf(v, t, matched_off))
244 				return t;
245 	t = saved_t;
246 	/* start searching up the tree */
247 	do {
248 		register struct radix_mask *m;
249 		t = t->rn_p;
250 		m = t->rn_mklist;
251 		if (m) {
252 			/*
253 			 * If non-contiguous masks ever become important
254 			 * we can restore the masking and open coding of
255 			 * the search and satisfaction test and put the
256 			 * calculation of "off" back before the "do".
257 			 */
258 			do {
259 				if (m->rm_flags & RNF_NORMAL) {
260 					if (rn_b <= m->rm_b)
261 						return (m->rm_leaf);
262 				} else {
263 					off = min(t->rn_off, matched_off);
264 					x = rn_search_m(v, t, m->rm_mask);
265 					while (x && x->rn_mask != m->rm_mask)
266 						x = x->rn_dupedkey;
267 					if (x && rn_satsifies_leaf(v, x, off))
268 						    return x;
269 				}
270 			m = m->rm_mklist;
271 			} while (m);
272 		}
273 	} while (t != top);
274 	return 0;
275 };
276 
277 #ifdef RN_DEBUG
278 int	rn_nodenum;
279 struct	radix_node *rn_clist;
280 int	rn_saveinfo;
281 int	rn_debug =  1;
282 #endif
283 
284 struct radix_node *
285 rn_newpair(v, b, nodes)
286 	void *v;
287 	int b;
288 	struct radix_node nodes[2];
289 {
290 	register struct radix_node *tt = nodes, *t = tt + 1;
291 	t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7);
292 	t->rn_l = tt; t->rn_off = b >> 3;
293 	tt->rn_b = -1; tt->rn_key = (caddr_t)v; tt->rn_p = t;
294 	tt->rn_flags = t->rn_flags = RNF_ACTIVE;
295 #ifdef RN_DEBUG
296 	tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
297 	tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
298 #endif
299 	return t;
300 }
301 
302 struct radix_node *
303 rn_insert(v_arg, head, dupentry, nodes)
304 	void *v_arg;
305 	struct radix_node_head *head;
306 	int *dupentry;
307 	struct radix_node nodes[2];
308 {
309 	caddr_t v = v_arg;
310 	struct radix_node *top = head->rnh_treetop;
311 	int head_off = top->rn_off, vlen = (int)*((u_char *)v);
312 	register struct radix_node *t = rn_search(v_arg, top);
313 	register caddr_t cp = v + head_off;
314 	register int b;
315 	struct radix_node *tt;
316     	/*
317 	 * Find first bit at which v and t->rn_key differ
318 	 */
319     {
320 	register caddr_t cp2 = t->rn_key + head_off;
321 	register int cmp_res;
322 	caddr_t cplim = v + vlen;
323 
324 	while (cp < cplim)
325 		if (*cp2++ != *cp++)
326 			goto on1;
327 	*dupentry = 1;
328 	return t;
329 on1:
330 	*dupentry = 0;
331 	cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
332 	for (b = (cp - v) << 3; cmp_res; b--)
333 		cmp_res >>= 1;
334     }
335     {
336 	register struct radix_node *p, *x = top;
337 	cp = v;
338 	do {
339 		p = x;
340 		if (cp[x->rn_off] & x->rn_bmask)
341 			x = x->rn_r;
342 		else x = x->rn_l;
343 	} while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
344 #ifdef RN_DEBUG
345 	if (rn_debug)
346 		log(LOG_DEBUG, "rn_insert: Going In:\n"), traverse(p);
347 #endif
348 	t = rn_newpair(v_arg, b, nodes); tt = t->rn_l;
349 	if ((cp[p->rn_off] & p->rn_bmask) == 0)
350 		p->rn_l = t;
351 	else
352 		p->rn_r = t;
353 	x->rn_p = t; t->rn_p = p; /* frees x, p as temp vars below */
354 	if ((cp[t->rn_off] & t->rn_bmask) == 0) {
355 		t->rn_r = x;
356 	} else {
357 		t->rn_r = tt; t->rn_l = x;
358 	}
359 #ifdef RN_DEBUG
360 	if (rn_debug)
361 		log(LOG_DEBUG, "rn_insert: Coming Out:\n"), traverse(p);
362 #endif
363     }
364 	return (tt);
365 }
366 
367 struct radix_node *
368 rn_addmask(n_arg, search, skip)
369 	int search, skip;
370 	void *n_arg;
371 {
372 	caddr_t netmask = (caddr_t)n_arg;
373 	register struct radix_node *x;
374 	register caddr_t cp, cplim;
375 	register int b = 0, mlen, j;
376 	int maskduplicated, m0, isnormal;
377 	struct radix_node *saved_x;
378 	static int last_zeroed = 0;
379 
380 	if ((mlen = *(u_char *)netmask) > max_keylen)
381 		mlen = max_keylen;
382 	if (skip == 0)
383 		skip = 1;
384 	if (mlen <= skip)
385 		return (mask_rnhead->rnh_nodes);
386 	if (skip > 1)
387 		Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
388 	if ((m0 = mlen) > skip)
389 		Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
390 	/*
391 	 * Trim trailing zeroes.
392 	 */
393 	for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
394 		cp--;
395 	mlen = cp - addmask_key;
396 	if (mlen <= skip) {
397 		if (m0 >= last_zeroed)
398 			last_zeroed = mlen;
399 		return (mask_rnhead->rnh_nodes);
400 	}
401 	if (m0 < last_zeroed)
402 		Bzero(addmask_key + m0, last_zeroed - m0);
403 	*addmask_key = last_zeroed = mlen;
404 	x = rn_search(addmask_key, rn_masktop);
405 	if (Bcmp(addmask_key, x->rn_key, mlen) != 0)
406 		x = 0;
407 	if (x || search)
408 		return (x);
409 	R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
410 	if ((saved_x = x) == 0)
411 		return (0);
412 	Bzero(x, max_keylen + 2 * sizeof (*x));
413 	netmask = cp = (caddr_t)(x + 2);
414 	Bcopy(addmask_key, cp, mlen);
415 	x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
416 	if (maskduplicated) {
417 		log(LOG_ERR, "rn_addmask: mask impossibly already in tree");
418 		Free(saved_x);
419 		return (x);
420 	}
421 	/*
422 	 * Calculate index of mask, and check for normalcy.
423 	 */
424 	cplim = netmask + mlen; isnormal = 1;
425 	for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;)
426 		cp++;
427 	if (cp != cplim) {
428 		for (j = 0x80; (j & *cp) != 0; j >>= 1)
429 			b++;
430 		if (*cp != normal_chars[b] || cp != (cplim - 1))
431 			isnormal = 0;
432 	}
433 	b += (cp - netmask) << 3;
434 	x->rn_b = -1 - b;
435 	if (isnormal)
436 		x->rn_flags |= RNF_NORMAL;
437 	return (x);
438 }
439 
440 static int	/* XXX: arbitrary ordering for non-contiguous masks */
441 rn_lexobetter(m_arg, n_arg)
442 	void *m_arg, *n_arg;
443 {
444 	register u_char *mp = m_arg, *np = n_arg, *lim;
445 
446 	if (*mp > *np)
447 		return 1;  /* not really, but need to check longer one first */
448 	if (*mp == *np)
449 		for (lim = mp + *mp; mp < lim;)
450 			if (*mp++ > *np++)
451 				return 1;
452 	return 0;
453 }
454 
455 static struct radix_mask *
456 rn_new_radix_mask(tt, next)
457 	register struct radix_node *tt;
458 	register struct radix_mask *next;
459 {
460 	register struct radix_mask *m;
461 
462 	MKGet(m);
463 	if (m == 0) {
464 		log(LOG_ERR, "Mask for route not entered\n");
465 		return (0);
466 	}
467 	Bzero(m, sizeof *m);
468 	m->rm_b = tt->rn_b;
469 	m->rm_flags = tt->rn_flags;
470 	if (tt->rn_flags & RNF_NORMAL)
471 		m->rm_leaf = tt;
472 	else
473 		m->rm_mask = tt->rn_mask;
474 	m->rm_mklist = next;
475 	tt->rn_mklist = m;
476 	return m;
477 }
478 
479 struct radix_node *
480 rn_addroute(v_arg, n_arg, head, treenodes)
481 	void *v_arg, *n_arg;
482 	struct radix_node_head *head;
483 	struct radix_node treenodes[2];
484 {
485 	caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
486 	register struct radix_node *t, *x = 0, *tt;
487 	struct radix_node *saved_tt, *top = head->rnh_treetop;
488 	short b = 0, b_leaf = 0;
489 	int keyduplicated;
490 	caddr_t mmask;
491 	struct radix_mask *m, **mp;
492 
493 	/*
494 	 * In dealing with non-contiguous masks, there may be
495 	 * many different routes which have the same mask.
496 	 * We will find it useful to have a unique pointer to
497 	 * the mask to speed avoiding duplicate references at
498 	 * nodes and possibly save time in calculating indices.
499 	 */
500 	if (netmask)  {
501 		if ((x = rn_addmask(netmask, 0, top->rn_off)) == 0)
502 			return (0);
503 		b_leaf = x->rn_b;
504 		b = -1 - x->rn_b;
505 		netmask = x->rn_key;
506 	}
507 	/*
508 	 * Deal with duplicated keys: attach node to previous instance
509 	 */
510 	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
511 	if (keyduplicated) {
512 		for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
513 			if (tt->rn_mask == netmask)
514 				return (0);
515 			if (netmask == 0 ||
516 			    (tt->rn_mask &&
517 			     ((b_leaf < tt->rn_b) || /* index(netmask) > node */
518 			       rn_refines(netmask, tt->rn_mask) ||
519 			       rn_lexobetter(netmask, tt->rn_mask))))
520 				break;
521 		}
522 		/*
523 		 * If the mask is not duplicated, we wouldn't
524 		 * find it among possible duplicate key entries
525 		 * anyway, so the above test doesn't hurt.
526 		 *
527 		 * We sort the masks for a duplicated key the same way as
528 		 * in a masklist -- most specific to least specific.
529 		 * This may require the unfortunate nuisance of relocating
530 		 * the head of the list.
531 		 *
532 		 * We also reverse, or doubly link the list through the
533 		 * parent pointer.
534 		 */
535 		if (tt == saved_tt) {
536 			struct	radix_node *xx = x;
537 			/* link in at head of list */
538 			(tt = treenodes)->rn_dupedkey = t;
539 			tt->rn_flags = t->rn_flags;
540 			tt->rn_p = x = t->rn_p;
541 			t->rn_p = tt;
542 			if (x->rn_l == t) x->rn_l = tt; else x->rn_r = tt;
543 			saved_tt = tt; x = xx;
544 		} else {
545 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
546 			t->rn_dupedkey = tt;
547 			tt->rn_p = t;
548 			if (tt->rn_dupedkey)
549 				tt->rn_dupedkey->rn_p = tt;
550 		}
551 #ifdef RN_DEBUG
552 		t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
553 		tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
554 #endif
555 		tt->rn_key = (caddr_t) v;
556 		tt->rn_b = -1;
557 		tt->rn_flags = RNF_ACTIVE;
558 	}
559 	/*
560 	 * Put mask in tree.
561 	 */
562 	if (netmask) {
563 		tt->rn_mask = netmask;
564 		tt->rn_b = x->rn_b;
565 		tt->rn_flags |= x->rn_flags & RNF_NORMAL;
566 	}
567 	t = saved_tt->rn_p;
568 	if (keyduplicated)
569 		goto on2;
570 	b_leaf = -1 - t->rn_b;
571 	if (t->rn_r == saved_tt) x = t->rn_l; else x = t->rn_r;
572 	/* Promote general routes from below */
573 	if (x->rn_b < 0) {
574 	    for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
575 		if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
576 			*mp = m = rn_new_radix_mask(x, 0);
577 			if (m)
578 				mp = &m->rm_mklist;
579 		}
580 	} else if (x->rn_mklist) {
581 		/*
582 		 * Skip over masks whose index is > that of new node
583 		 */
584 		for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
585 			if (m->rm_b >= b_leaf)
586 				break;
587 		t->rn_mklist = m; *mp = 0;
588 	}
589 on2:
590 	/* Add new route to highest possible ancestor's list */
591 	if ((netmask == 0) || (b > t->rn_b ))
592 		return tt; /* can't lift at all */
593 	b_leaf = tt->rn_b;
594 	do {
595 		x = t;
596 		t = t->rn_p;
597 	} while (b <= t->rn_b && x != top);
598 	/*
599 	 * Search through routes associated with node to
600 	 * insert new route according to index.
601 	 * Need same criteria as when sorting dupedkeys to avoid
602 	 * double loop on deletion.
603 	 */
604 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
605 		if (m->rm_b < b_leaf)
606 			continue;
607 		if (m->rm_b > b_leaf)
608 			break;
609 		if (m->rm_flags & RNF_NORMAL) {
610 			mmask = m->rm_leaf->rn_mask;
611 			if (tt->rn_flags & RNF_NORMAL) {
612 				log(LOG_ERR,
613 				   "Non-unique normal route, mask not entered");
614 				return tt;
615 			}
616 		} else
617 			mmask = m->rm_mask;
618 		if (mmask == netmask) {
619 			m->rm_refs++;
620 			tt->rn_mklist = m;
621 			return tt;
622 		}
623 		if (rn_refines(netmask, mmask) || rn_lexobetter(netmask, mmask))
624 			break;
625 	}
626 	*mp = rn_new_radix_mask(tt, *mp);
627 	return tt;
628 }
629 
630 struct radix_node *
631 rn_delete(v_arg, netmask_arg, head)
632 	void *v_arg, *netmask_arg;
633 	struct radix_node_head *head;
634 {
635 	register struct radix_node *t, *p, *x, *tt;
636 	struct radix_mask *m, *saved_m, **mp;
637 	struct radix_node *dupedkey, *saved_tt, *top;
638 	caddr_t v, netmask;
639 	int b, head_off, vlen;
640 
641 	v = v_arg;
642 	netmask = netmask_arg;
643 	x = head->rnh_treetop;
644 	tt = rn_search(v, x);
645 	head_off = x->rn_off;
646 	vlen =  *(u_char *)v;
647 	saved_tt = tt;
648 	top = x;
649 	if (tt == 0 ||
650 	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
651 		return (0);
652 	/*
653 	 * Delete our route from mask lists.
654 	 */
655 	if (netmask) {
656 		if ((x = rn_addmask(netmask, 1, head_off)) == 0)
657 			return (0);
658 		netmask = x->rn_key;
659 		while (tt->rn_mask != netmask)
660 			if ((tt = tt->rn_dupedkey) == 0)
661 				return (0);
662 	}
663 	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
664 		goto on1;
665 	if (tt->rn_flags & RNF_NORMAL) {
666 		if (m->rm_leaf != tt || m->rm_refs > 0) {
667 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
668 			return 0;  /* dangling ref could cause disaster */
669 		}
670 	} else {
671 		if (m->rm_mask != tt->rn_mask) {
672 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
673 			goto on1;
674 		}
675 		if (--m->rm_refs >= 0)
676 			goto on1;
677 	}
678 	b = -1 - tt->rn_b;
679 	t = saved_tt->rn_p;
680 	if (b > t->rn_b)
681 		goto on1; /* Wasn't lifted at all */
682 	do {
683 		x = t;
684 		t = t->rn_p;
685 	} while (b <= t->rn_b && x != top);
686 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
687 		if (m == saved_m) {
688 			*mp = m->rm_mklist;
689 			MKFree(m);
690 			break;
691 		}
692 	if (m == 0) {
693 		log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
694 		if (tt->rn_flags & RNF_NORMAL)
695 			return (0); /* Dangling ref to us */
696 	}
697 on1:
698 	/*
699 	 * Eliminate us from tree
700 	 */
701 	if (tt->rn_flags & RNF_ROOT)
702 		return (0);
703 #ifdef RN_DEBUG
704 	/* Get us out of the creation list */
705 	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
706 	if (t) t->rn_ybro = tt->rn_ybro;
707 #endif
708 	t = tt->rn_p;
709 	dupedkey = saved_tt->rn_dupedkey;
710 	if (dupedkey) {
711 		/*
712 		 * Here, tt is the deletion target, and
713 		 * saved_tt is the head of the dupedkey chain.
714 		 */
715 		if (tt == saved_tt) {
716 			x = dupedkey; x->rn_p = t;
717 			if (t->rn_l == tt) t->rn_l = x; else t->rn_r = x;
718 		} else {
719 			/* find node in front of tt on the chain */
720 			for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
721 				p = p->rn_dupedkey;
722 			if (p) {
723 				p->rn_dupedkey = tt->rn_dupedkey;
724 				if (tt->rn_dupedkey)
725 					tt->rn_dupedkey->rn_p = p;
726 			} else log(LOG_ERR, "rn_delete: couldn't find us\n");
727 		}
728 		t = tt + 1;
729 		if  (t->rn_flags & RNF_ACTIVE) {
730 #ifndef RN_DEBUG
731 			*++x = *t; p = t->rn_p;
732 #else
733 			b = t->rn_info; *++x = *t; t->rn_info = b; p = t->rn_p;
734 #endif
735 			if (p->rn_l == t) p->rn_l = x; else p->rn_r = x;
736 			x->rn_l->rn_p = x; x->rn_r->rn_p = x;
737 		}
738 		goto out;
739 	}
740 	if (t->rn_l == tt) x = t->rn_r; else x = t->rn_l;
741 	p = t->rn_p;
742 	if (p->rn_r == t) p->rn_r = x; else p->rn_l = x;
743 	x->rn_p = p;
744 	/*
745 	 * Demote routes attached to us.
746 	 */
747 	if (t->rn_mklist) {
748 		if (x->rn_b >= 0) {
749 			for (mp = &x->rn_mklist; (m = *mp);)
750 				mp = &m->rm_mklist;
751 			*mp = t->rn_mklist;
752 		} else {
753 			/* If there are any key,mask pairs in a sibling
754 			   duped-key chain, some subset will appear sorted
755 			   in the same order attached to our mklist */
756 			for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
757 				if (m == x->rn_mklist) {
758 					struct radix_mask *mm = m->rm_mklist;
759 					x->rn_mklist = 0;
760 					if (--(m->rm_refs) < 0)
761 						MKFree(m);
762 					m = mm;
763 				}
764 			if (m)
765 				log(LOG_ERR, "%s %x at %x\n",
766 					    "rn_delete: Orphaned Mask", m, x);
767 		}
768 	}
769 	/*
770 	 * We may be holding an active internal node in the tree.
771 	 */
772 	x = tt + 1;
773 	if (t != x) {
774 #ifndef RN_DEBUG
775 		*t = *x;
776 #else
777 		b = t->rn_info; *t = *x; t->rn_info = b;
778 #endif
779 		t->rn_l->rn_p = t; t->rn_r->rn_p = t;
780 		p = x->rn_p;
781 		if (p->rn_l == x) p->rn_l = t; else p->rn_r = t;
782 	}
783 out:
784 	tt->rn_flags &= ~RNF_ACTIVE;
785 	tt[1].rn_flags &= ~RNF_ACTIVE;
786 	return (tt);
787 }
788 
789 int
790 rn_walktree(h, f, w)
791 	struct radix_node_head *h;
792 	register int (*f)();
793 	void *w;
794 {
795 	int error;
796 	struct radix_node *base, *next;
797 	register struct radix_node *rn = h->rnh_treetop;
798 	/*
799 	 * This gets complicated because we may delete the node
800 	 * while applying the function f to it, so we need to calculate
801 	 * the successor node in advance.
802 	 */
803 	/* First time through node, go left */
804 	while (rn->rn_b >= 0)
805 		rn = rn->rn_l;
806 	for (;;) {
807 		base = rn;
808 		/* If at right child go back up, otherwise, go right */
809 		while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
810 			rn = rn->rn_p;
811 		/* Find the next *leaf* since next node might vanish, too */
812 		for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
813 			rn = rn->rn_l;
814 		next = rn;
815 		/* Process leaves */
816 		while (rn = base) {
817 			base = rn->rn_dupedkey;
818 			if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
819 				return (error);
820 		}
821 		rn = next;
822 		if (rn->rn_flags & RNF_ROOT)
823 			return (0);
824 	}
825 	/* NOTREACHED */
826 }
827 
828 int
829 rn_inithead(head, off)
830 	void **head;
831 	int off;
832 {
833 	register struct radix_node_head *rnh;
834 	register struct radix_node *t, *tt, *ttt;
835 	if (*head)
836 		return (1);
837 	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
838 	if (rnh == 0)
839 		return (0);
840 	Bzero(rnh, sizeof (*rnh));
841 	*head = rnh;
842 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
843 	ttt = rnh->rnh_nodes + 2;
844 	t->rn_r = ttt;
845 	t->rn_p = t;
846 	tt = t->rn_l;
847 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
848 	tt->rn_b = -1 - off;
849 	*ttt = *tt;
850 	ttt->rn_key = rn_ones;
851 	rnh->rnh_addaddr = rn_addroute;
852 	rnh->rnh_deladdr = rn_delete;
853 	rnh->rnh_matchaddr = rn_match;
854 	rnh->rnh_lookup = rn_lookup;
855 	rnh->rnh_walktree = rn_walktree;
856 	rnh->rnh_treetop = t;
857 	return (1);
858 }
859 
860 void
861 rn_init()
862 {
863 	char *cp, *cplim;
864 #ifdef KERNEL
865 	struct domain *dom;
866 
867 	for (dom = domains; dom; dom = dom->dom_next)
868 		if (dom->dom_maxrtkey > max_keylen)
869 			max_keylen = dom->dom_maxrtkey;
870 #endif
871 	if (max_keylen == 0) {
872 		log(LOG_ERR,
873 		    "rn_init: radix functions require max_keylen be set\n");
874 		return;
875 	}
876 	R_Malloc(rn_zeros, char *, 3 * max_keylen);
877 	if (rn_zeros == NULL)
878 		panic("rn_init");
879 	Bzero(rn_zeros, 3 * max_keylen);
880 	rn_ones = cp = rn_zeros + max_keylen;
881 	addmask_key = cplim = rn_ones + max_keylen;
882 	while (cp < cplim)
883 		*cp++ = -1;
884 	if (rn_inithead((void **)&mask_rnhead, 0) == 0)
885 		panic("rn_init 2");
886 }
887