xref: /openbsd-src/sys/net/radix.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: radix.c,v 1.23 2008/05/09 07:39:31 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);
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 = x;
601 			/* link in at head of list */
602 			(tt = treenodes)->rn_dupedkey = t;
603 			tt->rn_flags = t->rn_flags;
604 			tt->rn_p = x = t->rn_p;
605 			t->rn_p = tt;
606 			if (x->rn_l == t)
607 				x->rn_l = tt;
608 			else
609 				x->rn_r = tt;
610 			saved_tt = tt;
611 			x = xx;
612 		} else if (prioinv == 1) {
613 			(tt = treenodes)->rn_dupedkey = t;
614 			if (t->rn_p == NULL)
615 				panic("rn_addroute: t->rn_p is NULL");
616 			t->rn_p->rn_dupedkey = tt;
617 			tt->rn_p = t->rn_p;
618 			t->rn_p = tt;
619 		} else {
620 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
621 			t->rn_dupedkey = tt;
622 			tt->rn_p = t;
623 			if (tt->rn_dupedkey)
624 				tt->rn_dupedkey->rn_p = tt;
625 		}
626 #ifdef RN_DEBUG
627 		t=tt+1;
628 		tt->rn_info = rn_nodenum++;
629 		t->rn_info = rn_nodenum++;
630 		tt->rn_twin = t;
631 		tt->rn_ybro = rn_clist;
632 		rn_clist = tt;
633 #endif
634 		tt->rn_key = (caddr_t) v;
635 		tt->rn_b = -1;
636 		tt->rn_flags = RNF_ACTIVE;
637 	}
638 	/*
639 	 * Put mask in tree.
640 	 */
641 	if (netmask) {
642 		tt->rn_mask = netmask;
643 		tt->rn_b = x->rn_b;
644 		tt->rn_flags |= x->rn_flags & RNF_NORMAL;
645 	}
646 	t = saved_tt->rn_p;
647 	if (keyduplicated)
648 		goto on2;
649 	b_leaf = -1 - t->rn_b;
650 	if (t->rn_r == saved_tt)
651 		x = t->rn_l;
652 	else
653 		x = t->rn_r;
654 	/* Promote general routes from below */
655 	if (x->rn_b < 0) {
656 	    for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
657 		if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
658 			*mp = m = rn_new_radix_mask(x, 0);
659 			if (m)
660 				mp = &m->rm_mklist;
661 		}
662 	} else if (x->rn_mklist) {
663 		/*
664 		 * Skip over masks whose index is > that of new node
665 		 */
666 		for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
667 			if (m->rm_b >= b_leaf)
668 				break;
669 		t->rn_mklist = m;
670 		*mp = 0;
671 	}
672 on2:
673 	/* Add new route to highest possible ancestor's list */
674 	if ((netmask == 0) || (b > t->rn_b ))
675 		return tt; /* can't lift at all */
676 	b_leaf = tt->rn_b;
677 	do {
678 		x = t;
679 		t = t->rn_p;
680 	} while (b <= t->rn_b && x != top);
681 	/*
682 	 * Search through routes associated with node to
683 	 * insert new route according to index.
684 	 * Need same criteria as when sorting dupedkeys to avoid
685 	 * double loop on deletion.
686 	 */
687 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
688 		if (m->rm_b < b_leaf)
689 			continue;
690 		if (m->rm_b > b_leaf)
691 			break;
692 		if (m->rm_flags & RNF_NORMAL) {
693 			mmask = m->rm_leaf->rn_mask;
694 			if (tt->rn_flags & RNF_NORMAL) {
695 				log(LOG_ERR, "Non-unique normal route,"
696 				    " mask not entered\n");
697 				return tt;
698 			}
699 		} else
700 			mmask = m->rm_mask;
701 		if (mmask == netmask) {
702 			m->rm_refs++;
703 			tt->rn_mklist = m;
704 			return tt;
705 		}
706 		if (rn_refines(netmask, mmask) || rn_lexobetter(netmask, mmask))
707 			break;
708 	}
709 	*mp = rn_new_radix_mask(tt, *mp);
710 	return tt;
711 }
712 
713 struct radix_node *
714 rn_delete(void *v_arg, void *netmask_arg, struct radix_node_head *head,
715     struct radix_node *rn)
716 {
717 	struct radix_node *t, *p, *x, *tt;
718 	struct radix_mask *m, *saved_m, **mp;
719 	struct radix_node *dupedkey, *saved_tt, *top;
720 	caddr_t v, netmask;
721 	int b, head_off, vlen;
722 
723 	v = v_arg;
724 	netmask = netmask_arg;
725 	x = head->rnh_treetop;
726 	tt = rn_search(v, x);
727 	head_off = x->rn_off;
728 	vlen =  *(u_char *)v;
729 	saved_tt = tt;
730 	top = x;
731 	if (tt == 0 ||
732 	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
733 		return (0);
734 	/*
735 	 * Delete our route from mask lists.
736 	 */
737 	if (netmask) {
738 		if ((x = rn_addmask(netmask, 1, head_off)) == 0)
739 			return (0);
740 		netmask = x->rn_key;
741 		while (tt->rn_mask != netmask)
742 			if ((tt = tt->rn_dupedkey) == 0)
743 				return (0);
744 	}
745 #ifndef SMALL_KERNEL
746 	if (rn) {
747 		while (tt != rn)
748 			if ((tt = tt->rn_dupedkey) == 0)
749 				return (0);
750 	}
751 #endif
752 	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
753 		goto on1;
754 	if (tt->rn_flags & RNF_NORMAL) {
755 		if (m->rm_leaf != tt || m->rm_refs > 0) {
756 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
757 			return 0;  /* dangling ref could cause disaster */
758 		}
759 	} else {
760 		if (m->rm_mask != tt->rn_mask) {
761 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
762 			goto on1;
763 		}
764 		if (--m->rm_refs >= 0)
765 			goto on1;
766 	}
767 	b = -1 - tt->rn_b;
768 	t = saved_tt->rn_p;
769 	if (b > t->rn_b)
770 		goto on1; /* Wasn't lifted at all */
771 	do {
772 		x = t;
773 		t = t->rn_p;
774 	} while (b <= t->rn_b && x != top);
775 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
776 		if (m == saved_m) {
777 			*mp = m->rm_mklist;
778 			MKFree(m);
779 			break;
780 		}
781 	if (m == 0) {
782 		log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
783 		if (tt->rn_flags & RNF_NORMAL)
784 			return (0); /* Dangling ref to us */
785 	}
786 on1:
787 	/*
788 	 * Eliminate us from tree
789 	 */
790 	if (tt->rn_flags & RNF_ROOT)
791 		return (0);
792 #ifdef RN_DEBUG
793 	/* Get us out of the creation list */
794 	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro)
795 		;
796 	if (t) t->rn_ybro = tt->rn_ybro;
797 #endif
798 	t = tt->rn_p;
799 	dupedkey = saved_tt->rn_dupedkey;
800 	if (dupedkey) {
801 		/*
802 		 * Here, tt is the deletion target, and
803 		 * saved_tt is the head of the dupedkey chain.
804 		 */
805 		if (tt == saved_tt) {
806 			x = dupedkey;
807 			x->rn_p = t;
808 			if (t->rn_l == tt)
809 				t->rn_l = x;
810 			else
811 				t->rn_r = x;
812 		} else {
813 			x = saved_tt;
814 			t->rn_dupedkey = tt->rn_dupedkey;
815 			if (tt->rn_dupedkey)
816 				tt->rn_dupedkey->rn_p = t;
817 		}
818 		t = tt + 1;
819 		if  (t->rn_flags & RNF_ACTIVE) {
820 #ifndef RN_DEBUG
821 			*++x = *t;
822 			p = t->rn_p;
823 #else
824 			b = t->rn_info;
825 			*++x = *t;
826 			t->rn_info = b;
827 			p = t->rn_p;
828 #endif
829 			if (p->rn_l == t)
830 				p->rn_l = x;
831 			else
832 				p->rn_r = x;
833 			x->rn_l->rn_p = x;
834 			x->rn_r->rn_p = x;
835 		}
836 		goto out;
837 	}
838 	if (t->rn_l == tt)
839 		x = t->rn_r;
840 	else
841 		x = t->rn_l;
842 	p = t->rn_p;
843 	if (p->rn_r == t)
844 		p->rn_r = x;
845 	else
846 		p->rn_l = x;
847 	x->rn_p = p;
848 	/*
849 	 * Demote routes attached to us.
850 	 */
851 	if (t->rn_mklist) {
852 		if (x->rn_b >= 0) {
853 			for (mp = &x->rn_mklist; (m = *mp);)
854 				mp = &m->rm_mklist;
855 			*mp = t->rn_mklist;
856 		} else {
857 			/* If there are any key,mask pairs in a sibling
858 			   duped-key chain, some subset will appear sorted
859 			   in the same order attached to our mklist */
860 			for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
861 				if (m == x->rn_mklist) {
862 					struct radix_mask *mm = m->rm_mklist;
863 					x->rn_mklist = 0;
864 					if (--(m->rm_refs) < 0)
865 						MKFree(m);
866 					m = mm;
867 				}
868 			if (m)
869 				log(LOG_ERR, "%s %p at %p\n",
870 				    "rn_delete: Orphaned Mask", m, x);
871 		}
872 	}
873 	/*
874 	 * We may be holding an active internal node in the tree.
875 	 */
876 	x = tt + 1;
877 	if (t != x) {
878 #ifndef RN_DEBUG
879 		*t = *x;
880 #else
881 		b = t->rn_info;
882 		*t = *x;
883 		t->rn_info = b;
884 #endif
885 		t->rn_l->rn_p = t;
886 		t->rn_r->rn_p = t;
887 		p = x->rn_p;
888 		if (p->rn_l == x)
889 			p->rn_l = t;
890 		else
891 			p->rn_r = t;
892 	}
893 out:
894 	tt->rn_flags &= ~RNF_ACTIVE;
895 	tt[1].rn_flags &= ~RNF_ACTIVE;
896 	return (tt);
897 }
898 
899 int
900 rn_walktree(struct radix_node_head *h, int (*f)(struct radix_node *, void *),
901     void *w)
902 {
903 	int error;
904 	struct radix_node *base, *next;
905 	struct radix_node *rn = h->rnh_treetop;
906 	/*
907 	 * This gets complicated because we may delete the node
908 	 * while applying the function f to it, so we need to calculate
909 	 * the successor node in advance.
910 	 */
911 	/* First time through node, go left */
912 	while (rn->rn_b >= 0)
913 		rn = rn->rn_l;
914 	for (;;) {
915 		base = rn;
916 		/* If at right child go back up, otherwise, go right */
917 		while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
918 			rn = rn->rn_p;
919 		/* Find the next *leaf* since next node might vanish, too */
920 		for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
921 			rn = rn->rn_l;
922 		next = rn;
923 		/* Process leaves */
924 		while ((rn = base) != NULL) {
925 			base = rn->rn_dupedkey;
926 			if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
927 				return (error);
928 		}
929 		rn = next;
930 		if (rn->rn_flags & RNF_ROOT)
931 			return (0);
932 	}
933 	/* NOTREACHED */
934 }
935 
936 int
937 rn_inithead(void **head, int off)
938 {
939 	struct radix_node_head *rnh;
940 
941 	if (*head)
942 		return (1);
943 	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
944 	if (rnh == 0)
945 		return (0);
946 	*head = rnh;
947 	return rn_inithead0(rnh, off);
948 }
949 
950 int
951 rn_inithead0(struct radix_node_head *rnh, int off)
952 {
953 	struct radix_node *t, *tt, *ttt;
954 
955 	Bzero(rnh, sizeof (*rnh));
956 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
957 	ttt = rnh->rnh_nodes + 2;
958 	t->rn_r = ttt;
959 	t->rn_p = t;
960 	tt = t->rn_l;
961 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
962 	tt->rn_b = -1 - off;
963 	*ttt = *tt;
964 	ttt->rn_key = rn_ones;
965 	rnh->rnh_addaddr = rn_addroute;
966 	rnh->rnh_deladdr = rn_delete;
967 	rnh->rnh_matchaddr = rn_match;
968 	rnh->rnh_lookup = rn_lookup;
969 	rnh->rnh_walktree = rn_walktree;
970 	rnh->rnh_treetop = t;
971 	return (1);
972 }
973 
974 void
975 rn_init()
976 {
977 	char *cp, *cplim;
978 #ifdef _KERNEL
979 	struct domain *dom;
980 
981 	for (dom = domains; dom; dom = dom->dom_next)
982 		if (dom->dom_maxrtkey > max_keylen)
983 			max_keylen = dom->dom_maxrtkey;
984 #endif
985 	if (max_keylen == 0) {
986 		log(LOG_ERR,
987 		    "rn_init: radix functions require max_keylen be set\n");
988 		return;
989 	}
990 	R_Malloc(rn_zeros, char *, 3 * max_keylen);
991 	if (rn_zeros == NULL)
992 		panic("rn_init");
993 	Bzero(rn_zeros, 3 * max_keylen);
994 	rn_ones = cp = rn_zeros + max_keylen;
995 	addmask_key = cplim = rn_ones + max_keylen;
996 	while (cp < cplim)
997 		*cp++ = -1;
998 	if (rn_inithead((void *)&mask_rnhead, 0) == 0)
999 		panic("rn_init 2");
1000 }
1001