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