xref: /openbsd-src/sys/net/radix.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: radix.c,v 1.29 2011/07/22 13:05:29 henning 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 	/*
476 	 * Longer masks might not really be lexicographically better,
477 	 * but longer masks always have precedence since they must be checked
478 	 * first. The netmasks were normalized before calling this function and
479 	 * don't have unneeded trailing zeros.
480 	 */
481 	if (*mp > *np)
482 		return 1;
483 	if (*mp < *np)
484 		return 0;
485 	/*
486 	 * Must return the first difference between the masks
487 	 * to ensure deterministic sorting.
488 	 */
489 	lim = mp + *mp;
490 	return (memcmp(mp, np, *lim) > 0);
491 }
492 
493 static struct radix_mask *
494 rn_new_radix_mask(struct radix_node *tt, struct radix_mask *next)
495 {
496 	struct radix_mask *m;
497 
498 	MKGet(m);
499 	if (m == 0) {
500 		log(LOG_ERR, "Mask for route not entered\n");
501 		return (0);
502 	}
503 	Bzero(m, sizeof *m);
504 	m->rm_b = tt->rn_b;
505 	m->rm_flags = tt->rn_flags;
506 	if (tt->rn_flags & RNF_NORMAL)
507 		m->rm_leaf = tt;
508 	else
509 		m->rm_mask = tt->rn_mask;
510 	m->rm_mklist = next;
511 	tt->rn_mklist = m;
512 	return m;
513 }
514 
515 struct radix_node *
516 rn_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
517     struct radix_node treenodes[2], u_int8_t prio)
518 {
519 	caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
520 	struct radix_node *t, *x = NULL, *tt;
521 	struct radix_node *saved_tt, *top = head->rnh_treetop;
522 	short b = 0, b_leaf = 0;
523 	int keyduplicated, prioinv = -1;
524 	caddr_t mmask;
525 	struct radix_mask *m, **mp;
526 
527 	/*
528 	 * In dealing with non-contiguous masks, there may be
529 	 * many different routes which have the same mask.
530 	 * We will find it useful to have a unique pointer to
531 	 * the mask to speed avoiding duplicate references at
532 	 * nodes and possibly save time in calculating indices.
533 	 */
534 	if (netmask)  {
535 		if ((x = rn_addmask(netmask, 0, top->rn_off)) == 0)
536 			return (0);
537 		b_leaf = x->rn_b;
538 		b = -1 - x->rn_b;
539 		netmask = x->rn_key;
540 	}
541 	/*
542 	 * Deal with duplicated keys: attach node to previous instance
543 	 */
544 	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
545 	if (keyduplicated) {
546 		for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
547 #ifndef SMALL_KERNEL
548 			/* permit multipath, if enabled for the family */
549 			if (rn_mpath_capable(head) && netmask == tt->rn_mask) {
550 				int mid;
551 				/*
552 				 * Try to insert the new node in the middle
553 				 * of the list of any preexisting multipaths,
554 				 * to reduce the number of path disruptions
555 				 * that occur as a result of an insertion,
556 				 * per RFC2992.
557 				 * Additionally keep the list sorted by route
558 				 * priority.
559 				 */
560 				prioinv = 0;
561 				tt = rn_mpath_prio(tt, prio);
562 				if (((struct rtentry *)tt)->rt_priority !=
563 				    prio) {
564 					/*
565 					 * rn_mpath_prio returns the previous
566 					 * element if no element with the
567 					 * requested priority exists. It could
568 					 * be that the previous element comes
569 					 * with a bigger priority.
570 					 */
571 					if (((struct rtentry *)tt)->
572 					    rt_priority > prio)
573 						prioinv = 1;
574 					t = tt;
575 					break;
576 				}
577 
578 				mid = rn_mpath_count(tt) / 2;
579 				do {
580 					t = tt;
581 					tt = rn_mpath_next(tt, 0);
582 				} while (tt && --mid > 0);
583 				break;
584 			}
585 #endif
586 			if (tt->rn_mask == netmask)
587 				return (0);
588 			if (netmask == 0 ||
589 			    (tt->rn_mask &&
590 			     ((b_leaf < tt->rn_b) || /* index(netmask) > node */
591 			       rn_refines(netmask, tt->rn_mask) ||
592 			       rn_lexobetter(netmask, tt->rn_mask))))
593 				break;
594 		}
595 		/*
596 		 * If the mask is not duplicated, we wouldn't
597 		 * find it among possible duplicate key entries
598 		 * anyway, so the above test doesn't hurt.
599 		 *
600 		 * We sort the masks for a duplicated key the same way as
601 		 * in a masklist -- most specific to least specific.
602 		 * This may require the unfortunate nuisance of relocating
603 		 * the head of the list.
604 		 *
605 		 * We also reverse, or doubly link the list through the
606 		 * parent pointer.
607 		 */
608 		if (tt == saved_tt && prioinv) {
609 			struct	radix_node *xx;
610 			/* link in at head of list */
611 			(tt = treenodes)->rn_dupedkey = t;
612 			tt->rn_flags = t->rn_flags;
613 			tt->rn_p = xx = t->rn_p;
614 			t->rn_p = tt;
615 			if (xx->rn_l == t)
616 				xx->rn_l = tt;
617 			else
618 				xx->rn_r = tt;
619 			saved_tt = tt;
620 		} else if (prioinv == 1) {
621 			(tt = treenodes)->rn_dupedkey = t;
622 			if (t->rn_p == NULL)
623 				panic("rn_addroute: t->rn_p is NULL");
624 			t->rn_p->rn_dupedkey = tt;
625 			tt->rn_p = t->rn_p;
626 			t->rn_p = tt;
627 		} else {
628 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
629 			t->rn_dupedkey = tt;
630 			tt->rn_p = t;
631 			if (tt->rn_dupedkey)
632 				tt->rn_dupedkey->rn_p = tt;
633 		}
634 #ifdef RN_DEBUG
635 		t=tt+1;
636 		tt->rn_info = rn_nodenum++;
637 		t->rn_info = rn_nodenum++;
638 		tt->rn_twin = t;
639 		tt->rn_ybro = rn_clist;
640 		rn_clist = tt;
641 #endif
642 		tt->rn_key = (caddr_t) v;
643 		tt->rn_b = -1;
644 		tt->rn_flags = RNF_ACTIVE;
645 	}
646 	/*
647 	 * Put mask in tree.
648 	 */
649 	if (netmask) {
650 		tt->rn_mask = netmask;
651 		tt->rn_b = x->rn_b;
652 		tt->rn_flags |= x->rn_flags & RNF_NORMAL;
653 	}
654 	t = saved_tt->rn_p;
655 	if (keyduplicated)
656 		goto on2;
657 	b_leaf = -1 - t->rn_b;
658 	if (t->rn_r == saved_tt)
659 		x = t->rn_l;
660 	else
661 		x = t->rn_r;
662 	/* Promote general routes from below */
663 	if (x->rn_b < 0) {
664 	    struct	radix_node *xx = NULL;
665 	    for (mp = &t->rn_mklist; x; xx = x, x = x->rn_dupedkey) {
666 		if (xx && xx->rn_mklist && xx->rn_mask == x->rn_mask &&
667 		    x->rn_mklist == 0) {
668 			/* multipath route, bump refcount on first mklist */
669 			x->rn_mklist = xx->rn_mklist;
670 			x->rn_mklist->rm_refs++;
671 		}
672 		if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
673 			*mp = m = rn_new_radix_mask(x, 0);
674 			if (m)
675 				mp = &m->rm_mklist;
676 		}
677 	    }
678 	} else if (x->rn_mklist) {
679 		/*
680 		 * Skip over masks whose index is > that of new node
681 		 */
682 		for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
683 			if (m->rm_b >= b_leaf)
684 				break;
685 		t->rn_mklist = m;
686 		*mp = 0;
687 	}
688 on2:
689 	/* Add new route to highest possible ancestor's list */
690 	if ((netmask == 0) || (b > t->rn_b ))
691 		return tt; /* can't lift at all */
692 	b_leaf = tt->rn_b;
693 	do {
694 		x = t;
695 		t = t->rn_p;
696 	} while (b <= t->rn_b && x != top);
697 	/*
698 	 * Search through routes associated with node to
699 	 * insert new route according to index.
700 	 * Need same criteria as when sorting dupedkeys to avoid
701 	 * double loop on deletion.
702 	 */
703 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
704 		if (m->rm_b < b_leaf)
705 			continue;
706 		if (m->rm_b > b_leaf)
707 			break;
708 		if (m->rm_flags & RNF_NORMAL) {
709 			mmask = m->rm_leaf->rn_mask;
710 			if (keyduplicated) {
711 				if (m->rm_leaf->rn_p == tt)
712 					/* new route is better */
713 					m->rm_leaf = tt;
714 #ifdef DIAGNOSTIC
715 				else {
716 					for (t = m->rm_leaf; t;
717 						t = t->rn_dupedkey)
718 						if (t == tt)
719 							break;
720 					if (t == NULL) {
721 						log(LOG_ERR, "Non-unique "
722 						    "normal route on dupedkey, "
723 						    "mask not entered\n");
724 						return tt;
725 					}
726 				}
727 #endif
728 				m->rm_refs++;
729 				tt->rn_mklist = m;
730 				return tt;
731 			} else if (tt->rn_flags & RNF_NORMAL) {
732 				log(LOG_ERR, "Non-unique normal route,"
733 				    " mask not entered\n");
734 				return tt;
735 			}
736 		} else
737 			mmask = m->rm_mask;
738 		if (mmask == netmask) {
739 			m->rm_refs++;
740 			tt->rn_mklist = m;
741 			return tt;
742 		}
743 		if (rn_refines(netmask, mmask) || rn_lexobetter(netmask, mmask))
744 			break;
745 	}
746 	*mp = rn_new_radix_mask(tt, *mp);
747 	return tt;
748 }
749 
750 struct radix_node *
751 rn_delete(void *v_arg, void *netmask_arg, struct radix_node_head *head,
752     struct radix_node *rn)
753 {
754 	struct radix_node *t, *p, *x, *tt;
755 	struct radix_mask *m, *saved_m, **mp;
756 	struct radix_node *dupedkey, *saved_tt, *top;
757 	caddr_t v, netmask;
758 	int b, head_off, vlen;
759 
760 	v = v_arg;
761 	netmask = netmask_arg;
762 	x = head->rnh_treetop;
763 	tt = rn_search(v, x);
764 	head_off = x->rn_off;
765 	vlen =  *(u_char *)v;
766 	saved_tt = tt;
767 	top = x;
768 	if (tt == 0 ||
769 	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
770 		return (0);
771 	/*
772 	 * Delete our route from mask lists.
773 	 */
774 	if (netmask) {
775 		if ((x = rn_addmask(netmask, 1, head_off)) == 0)
776 			return (0);
777 		netmask = x->rn_key;
778 		while (tt->rn_mask != netmask)
779 			if ((tt = tt->rn_dupedkey) == 0)
780 				return (0);
781 	}
782 #ifndef SMALL_KERNEL
783 	if (rn) {
784 		while (tt != rn)
785 			if ((tt = tt->rn_dupedkey) == 0)
786 				return (0);
787 	}
788 #endif
789 	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
790 		goto on1;
791 	if (tt->rn_flags & RNF_NORMAL) {
792 		if (m->rm_leaf != tt && m->rm_refs == 0) {
793 			log(LOG_ERR, "rn_delete: inconsistent normal "
794 			    "annotation\n");
795 			return (0);
796 		}
797 		if (m->rm_leaf != tt) {
798 			if (--m->rm_refs >= 0)
799 				goto on1;
800 		}
801 		/* tt is currently the head of the possible multipath chain */
802 		if (m->rm_refs > 0) {
803 			if (tt->rn_dupedkey == NULL ||
804 			    tt->rn_dupedkey->rn_mklist != m) {
805 				log(LOG_ERR, "rn_delete: inconsistent "
806 				    "dupedkey list\n");
807 				return (0);
808 			}
809 			m->rm_leaf = tt->rn_dupedkey;
810 			--m->rm_refs;
811 			goto on1;
812 		}
813 		/* else tt is last and only route */
814 	} else {
815 		if (m->rm_mask != tt->rn_mask) {
816 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
817 			goto on1;
818 		}
819 		if (--m->rm_refs >= 0)
820 			goto on1;
821 	}
822 	b = -1 - tt->rn_b;
823 	t = saved_tt->rn_p;
824 	if (b > t->rn_b)
825 		goto on1; /* Wasn't lifted at all */
826 	do {
827 		x = t;
828 		t = t->rn_p;
829 	} while (b <= t->rn_b && x != top);
830 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
831 		if (m == saved_m) {
832 			*mp = m->rm_mklist;
833 			MKFree(m);
834 			break;
835 		}
836 	if (m == 0) {
837 		log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
838 		if (tt->rn_flags & RNF_NORMAL)
839 			return (0); /* Dangling ref to us */
840 	}
841 on1:
842 	/*
843 	 * Eliminate us from tree
844 	 */
845 	if (tt->rn_flags & RNF_ROOT)
846 		return (0);
847 #ifdef RN_DEBUG
848 	/* Get us out of the creation list */
849 	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro)
850 		;
851 	if (t) t->rn_ybro = tt->rn_ybro;
852 #endif
853 	t = tt->rn_p;
854 	dupedkey = saved_tt->rn_dupedkey;
855 	if (dupedkey) {
856 		/*
857 		 * Here, tt is the deletion target, and
858 		 * saved_tt is the head of the dupedkey chain.
859 		 */
860 		if (tt == saved_tt) {
861 			x = dupedkey;
862 			x->rn_p = t;
863 			if (t->rn_l == tt)
864 				t->rn_l = x;
865 			else
866 				t->rn_r = x;
867 		} else {
868 			x = saved_tt;
869 			t->rn_dupedkey = tt->rn_dupedkey;
870 			if (tt->rn_dupedkey)
871 				tt->rn_dupedkey->rn_p = t;
872 		}
873 		t = tt + 1;
874 		if  (t->rn_flags & RNF_ACTIVE) {
875 #ifndef RN_DEBUG
876 			*++x = *t;
877 			p = t->rn_p;
878 #else
879 			b = t->rn_info;
880 			*++x = *t;
881 			t->rn_info = b;
882 			p = t->rn_p;
883 #endif
884 			if (p->rn_l == t)
885 				p->rn_l = x;
886 			else
887 				p->rn_r = x;
888 			x->rn_l->rn_p = x;
889 			x->rn_r->rn_p = x;
890 		}
891 		goto out;
892 	}
893 	if (t->rn_l == tt)
894 		x = t->rn_r;
895 	else
896 		x = t->rn_l;
897 	p = t->rn_p;
898 	if (p->rn_r == t)
899 		p->rn_r = x;
900 	else
901 		p->rn_l = x;
902 	x->rn_p = p;
903 	/*
904 	 * Demote routes attached to us.
905 	 */
906 	if (t->rn_mklist) {
907 		if (x->rn_b >= 0) {
908 			for (mp = &x->rn_mklist; (m = *mp);)
909 				mp = &m->rm_mklist;
910 			*mp = t->rn_mklist;
911 		} else {
912 			/* If there are any key,mask pairs in a sibling
913 			   duped-key chain, some subset will appear sorted
914 			   in the same order attached to our mklist */
915 			for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
916 				if (m == x->rn_mklist) {
917 					struct radix_mask *mm = m->rm_mklist;
918 					x->rn_mklist = 0;
919 					if (--(m->rm_refs) < 0)
920 						MKFree(m);
921 					else if (m->rm_flags & RNF_NORMAL)
922 						/*
923 						 * don't progress because this
924 						 * a multipath route. Next
925 						 * route will use the same m.
926 						 */
927 						mm = m;
928 					m = mm;
929 				}
930 			if (m)
931 				log(LOG_ERR, "%s %p at %p\n",
932 				    "rn_delete: Orphaned Mask", m, x);
933 		}
934 	}
935 	/*
936 	 * We may be holding an active internal node in the tree.
937 	 */
938 	x = tt + 1;
939 	if (t != x) {
940 #ifndef RN_DEBUG
941 		*t = *x;
942 #else
943 		b = t->rn_info;
944 		*t = *x;
945 		t->rn_info = b;
946 #endif
947 		t->rn_l->rn_p = t;
948 		t->rn_r->rn_p = t;
949 		p = x->rn_p;
950 		if (p->rn_l == x)
951 			p->rn_l = t;
952 		else
953 			p->rn_r = t;
954 	}
955 out:
956 	tt->rn_flags &= ~RNF_ACTIVE;
957 	tt[1].rn_flags &= ~RNF_ACTIVE;
958 	return (tt);
959 }
960 
961 int
962 rn_walktree(struct radix_node_head *h, int (*f)(struct radix_node *, void *,
963     u_int), void *w)
964 {
965 	int error;
966 	struct radix_node *base, *next;
967 	struct radix_node *rn = h->rnh_treetop;
968 	/*
969 	 * This gets complicated because we may delete the node
970 	 * while applying the function f to it, so we need to calculate
971 	 * the successor node in advance.
972 	 */
973 	/* First time through node, go left */
974 	while (rn->rn_b >= 0)
975 		rn = rn->rn_l;
976 	for (;;) {
977 		base = rn;
978 		/* If at right child go back up, otherwise, go right */
979 		while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
980 			rn = rn->rn_p;
981 		/* Find the next *leaf* since next node might vanish, too */
982 		for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
983 			rn = rn->rn_l;
984 		next = rn;
985 		/* Process leaves */
986 		while ((rn = base) != NULL) {
987 			base = rn->rn_dupedkey;
988 			if (!(rn->rn_flags & RNF_ROOT) &&
989 			    (error = (*f)(rn, w, h->rnh_rtableid)))
990 				return (error);
991 		}
992 		rn = next;
993 		if (rn->rn_flags & RNF_ROOT)
994 			return (0);
995 	}
996 	/* NOTREACHED */
997 }
998 
999 int
1000 rn_inithead(void **head, int off)
1001 {
1002 	struct radix_node_head *rnh;
1003 
1004 	if (*head)
1005 		return (1);
1006 	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
1007 	if (rnh == 0)
1008 		return (0);
1009 	*head = rnh;
1010 	return rn_inithead0(rnh, off);
1011 }
1012 
1013 int
1014 rn_inithead0(struct radix_node_head *rnh, int off)
1015 {
1016 	struct radix_node *t, *tt, *ttt;
1017 
1018 	Bzero(rnh, sizeof (*rnh));
1019 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
1020 	ttt = rnh->rnh_nodes + 2;
1021 	t->rn_r = ttt;
1022 	t->rn_p = t;
1023 	tt = t->rn_l;
1024 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
1025 	tt->rn_b = -1 - off;
1026 	*ttt = *tt;
1027 	ttt->rn_key = rn_ones;
1028 	rnh->rnh_addaddr = rn_addroute;
1029 	rnh->rnh_deladdr = rn_delete;
1030 	rnh->rnh_matchaddr = rn_match;
1031 	rnh->rnh_lookup = rn_lookup;
1032 	rnh->rnh_walktree = rn_walktree;
1033 	rnh->rnh_treetop = t;
1034 	return (1);
1035 }
1036 
1037 void
1038 rn_init()
1039 {
1040 	char *cp, *cplim;
1041 #ifdef _KERNEL
1042 	struct domain *dom;
1043 
1044 	for (dom = domains; dom; dom = dom->dom_next)
1045 		if (dom->dom_maxrtkey > max_keylen)
1046 			max_keylen = dom->dom_maxrtkey;
1047 #endif
1048 	if (max_keylen == 0) {
1049 		log(LOG_ERR,
1050 		    "rn_init: radix functions require max_keylen be set\n");
1051 		return;
1052 	}
1053 	R_Malloc(rn_zeros, char *, 3 * max_keylen);
1054 	if (rn_zeros == NULL)
1055 		panic("rn_init");
1056 	Bzero(rn_zeros, 3 * max_keylen);
1057 	rn_ones = cp = rn_zeros + max_keylen;
1058 	addmask_key = cplim = rn_ones + max_keylen;
1059 	while (cp < cplim)
1060 		*cp++ = -1;
1061 	if (rn_inithead((void *)&mask_rnhead, 0) == 0)
1062 		panic("rn_init 2");
1063 }
1064