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