Home | History | Annotate | Line # | Download | only in net
radix.c revision 1.7
      1  1.7      cgd /*	$NetBSD: radix.c,v 1.7 1994/06/29 06:36:33 cgd Exp $	*/
      2  1.7      cgd 
      3  1.1      cgd /*
      4  1.6  mycroft  * Copyright (c) 1988, 1989, 1993
      5  1.6  mycroft  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1      cgd  * modification, are permitted provided that the following conditions
      9  1.1      cgd  * are met:
     10  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  *
     35  1.7      cgd  *	@(#)radix.c	8.2 (Berkeley) 1/4/94
     36  1.1      cgd  */
     37  1.1      cgd 
     38  1.1      cgd /*
     39  1.1      cgd  * Routines to build and maintain radix trees for routing lookups.
     40  1.1      cgd  */
     41  1.1      cgd #ifndef RNF_NORMAL
     42  1.4  mycroft #include <sys/param.h>
     43  1.4  mycroft #include <sys/systm.h>
     44  1.4  mycroft #include <sys/malloc.h>
     45  1.1      cgd #define	M_DONTWAIT M_NOWAIT
     46  1.6  mycroft #ifdef	KERNEL
     47  1.6  mycroft #include <sys/domain.h>
     48  1.6  mycroft #endif
     49  1.6  mycroft #endif
     50  1.6  mycroft 
     51  1.4  mycroft #include <net/radix.h>
     52  1.6  mycroft 
     53  1.6  mycroft int	max_keylen;
     54  1.6  mycroft struct radix_mask *rn_mkfreelist;
     55  1.1      cgd struct radix_node_head *mask_rnhead;
     56  1.6  mycroft static int gotOddMasks;
     57  1.6  mycroft static char *maskedKey;
     58  1.6  mycroft static char *rn_zeros, *rn_ones;
     59  1.6  mycroft 
     60  1.6  mycroft #define rn_masktop (mask_rnhead->rnh_treetop)
     61  1.1      cgd #undef Bcmp
     62  1.1      cgd #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
     63  1.1      cgd /*
     64  1.1      cgd  * The data structure for the keys is a radix tree with one way
     65  1.1      cgd  * branching removed.  The index rn_b at an internal node n represents a bit
     66  1.1      cgd  * position to be tested.  The tree is arranged so that all descendants
     67  1.1      cgd  * of a node n have keys whose bits all agree up to position rn_b - 1.
     68  1.1      cgd  * (We say the index of n is rn_b.)
     69  1.1      cgd  *
     70  1.1      cgd  * There is at least one descendant which has a one bit at position rn_b,
     71  1.1      cgd  * and at least one with a zero there.
     72  1.1      cgd  *
     73  1.1      cgd  * A route is determined by a pair of key and mask.  We require that the
     74  1.1      cgd  * bit-wise logical and of the key and mask to be the key.
     75  1.1      cgd  * We define the index of a route to associated with the mask to be
     76  1.1      cgd  * the first bit number in the mask where 0 occurs (with bit number 0
     77  1.1      cgd  * representing the highest order bit).
     78  1.1      cgd  *
     79  1.1      cgd  * We say a mask is normal if every bit is 0, past the index of the mask.
     80  1.1      cgd  * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
     81  1.1      cgd  * and m is a normal mask, then the route applies to every descendant of n.
     82  1.1      cgd  * If the index(m) < rn_b, this implies the trailing last few bits of k
     83  1.1      cgd  * before bit b are all 0, (and hence consequently true of every descendant
     84  1.1      cgd  * of n), so the route applies to all descendants of the node as well.
     85  1.1      cgd  *
     86  1.1      cgd  * The present version of the code makes no use of normal routes,
     87  1.1      cgd  * but similar logic shows that a non-normal mask m such that
     88  1.1      cgd  * index(m) <= index(n) could potentially apply to many children of n.
     89  1.1      cgd  * Thus, for each non-host route, we attach its mask to a list at an internal
     90  1.1      cgd  * node as high in the tree as we can go.
     91  1.1      cgd  */
     92  1.1      cgd 
     93  1.1      cgd struct radix_node *
     94  1.6  mycroft rn_search(v_arg, head)
     95  1.6  mycroft 	void *v_arg;
     96  1.1      cgd 	struct radix_node *head;
     97  1.1      cgd {
     98  1.1      cgd 	register struct radix_node *x;
     99  1.6  mycroft 	register caddr_t v;
    100  1.1      cgd 
    101  1.6  mycroft 	for (x = head, v = v_arg; x->rn_b >= 0;) {
    102  1.1      cgd 		if (x->rn_bmask & v[x->rn_off])
    103  1.1      cgd 			x = x->rn_r;
    104  1.1      cgd 		else
    105  1.1      cgd 			x = x->rn_l;
    106  1.1      cgd 	}
    107  1.6  mycroft 	return (x);
    108  1.1      cgd };
    109  1.1      cgd 
    110  1.1      cgd struct radix_node *
    111  1.6  mycroft rn_search_m(v_arg, head, m_arg)
    112  1.1      cgd 	struct radix_node *head;
    113  1.6  mycroft 	void *v_arg, *m_arg;
    114  1.1      cgd {
    115  1.1      cgd 	register struct radix_node *x;
    116  1.6  mycroft 	register caddr_t v = v_arg, m = m_arg;
    117  1.1      cgd 
    118  1.1      cgd 	for (x = head; x->rn_b >= 0;) {
    119  1.1      cgd 		if ((x->rn_bmask & m[x->rn_off]) &&
    120  1.1      cgd 		    (x->rn_bmask & v[x->rn_off]))
    121  1.1      cgd 			x = x->rn_r;
    122  1.1      cgd 		else
    123  1.1      cgd 			x = x->rn_l;
    124  1.1      cgd 	}
    125  1.1      cgd 	return x;
    126  1.1      cgd };
    127  1.1      cgd 
    128  1.6  mycroft int
    129  1.6  mycroft rn_refines(m_arg, n_arg)
    130  1.6  mycroft 	void *m_arg, *n_arg;
    131  1.6  mycroft {
    132  1.6  mycroft 	register caddr_t m = m_arg, n = n_arg;
    133  1.6  mycroft 	register caddr_t lim, lim2 = lim = n + *(u_char *)n;
    134  1.6  mycroft 	int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
    135  1.6  mycroft 	int masks_are_equal = 1;
    136  1.6  mycroft 
    137  1.6  mycroft 	if (longer > 0)
    138  1.6  mycroft 		lim -= longer;
    139  1.6  mycroft 	while (n < lim) {
    140  1.6  mycroft 		if (*n & ~(*m))
    141  1.6  mycroft 			return 0;
    142  1.6  mycroft 		if (*n++ != *m++)
    143  1.6  mycroft 			masks_are_equal = 0;
    144  1.6  mycroft 
    145  1.6  mycroft 	}
    146  1.6  mycroft 	while (n < lim2)
    147  1.6  mycroft 		if (*n++)
    148  1.6  mycroft 			return 0;
    149  1.6  mycroft 	if (masks_are_equal && (longer < 0))
    150  1.6  mycroft 		for (lim2 = m - longer; m < lim2; )
    151  1.6  mycroft 			if (*m++)
    152  1.6  mycroft 				return 1;
    153  1.6  mycroft 	return (!masks_are_equal);
    154  1.6  mycroft }
    155  1.1      cgd 
    156  1.1      cgd 
    157  1.1      cgd struct radix_node *
    158  1.6  mycroft rn_match(v_arg, head)
    159  1.6  mycroft 	void *v_arg;
    160  1.6  mycroft 	struct radix_node_head *head;
    161  1.1      cgd {
    162  1.6  mycroft 	caddr_t v = v_arg;
    163  1.6  mycroft 	register struct radix_node *t = head->rnh_treetop, *x;
    164  1.1      cgd 	register caddr_t cp = v, cp2, cp3;
    165  1.1      cgd 	caddr_t cplim, mstart;
    166  1.6  mycroft 	struct radix_node *saved_t, *top = t;
    167  1.1      cgd 	int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
    168  1.1      cgd 
    169  1.1      cgd 	/*
    170  1.6  mycroft 	 * Open code rn_search(v, top) to avoid overhead of extra
    171  1.1      cgd 	 * subroutine call.
    172  1.1      cgd 	 */
    173  1.1      cgd 	for (; t->rn_b >= 0; ) {
    174  1.1      cgd 		if (t->rn_bmask & cp[t->rn_off])
    175  1.1      cgd 			t = t->rn_r;
    176  1.1      cgd 		else
    177  1.1      cgd 			t = t->rn_l;
    178  1.1      cgd 	}
    179  1.1      cgd 	/*
    180  1.1      cgd 	 * See if we match exactly as a host destination
    181  1.1      cgd 	 */
    182  1.1      cgd 	cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
    183  1.1      cgd 	for (; cp < cplim; cp++, cp2++)
    184  1.1      cgd 		if (*cp != *cp2)
    185  1.1      cgd 			goto on1;
    186  1.1      cgd 	/*
    187  1.1      cgd 	 * This extra grot is in case we are explicitly asked
    188  1.1      cgd 	 * to look up the default.  Ugh!
    189  1.1      cgd 	 */
    190  1.1      cgd 	if ((t->rn_flags & RNF_ROOT) && t->rn_dupedkey)
    191  1.1      cgd 		t = t->rn_dupedkey;
    192  1.1      cgd 	return t;
    193  1.1      cgd on1:
    194  1.1      cgd 	matched_off = cp - v;
    195  1.1      cgd 	saved_t = t;
    196  1.1      cgd 	do {
    197  1.1      cgd 	    if (t->rn_mask) {
    198  1.1      cgd 		/*
    199  1.1      cgd 		 * Even if we don't match exactly as a hosts;
    200  1.1      cgd 		 * we may match if the leaf we wound up at is
    201  1.1      cgd 		 * a route to a net.
    202  1.1      cgd 		 */
    203  1.1      cgd 		cp3 = matched_off + t->rn_mask;
    204  1.1      cgd 		cp2 = matched_off + t->rn_key;
    205  1.1      cgd 		for (; cp < cplim; cp++)
    206  1.1      cgd 			if ((*cp2++ ^ *cp) & *cp3++)
    207  1.1      cgd 				break;
    208  1.1      cgd 		if (cp == cplim)
    209  1.1      cgd 			return t;
    210  1.1      cgd 		cp = matched_off + v;
    211  1.1      cgd 	    }
    212  1.1      cgd 	} while (t = t->rn_dupedkey);
    213  1.1      cgd 	t = saved_t;
    214  1.1      cgd 	/* start searching up the tree */
    215  1.1      cgd 	do {
    216  1.1      cgd 		register struct radix_mask *m;
    217  1.1      cgd 		t = t->rn_p;
    218  1.1      cgd 		if (m = t->rn_mklist) {
    219  1.1      cgd 			/*
    220  1.1      cgd 			 * After doing measurements here, it may
    221  1.1      cgd 			 * turn out to be faster to open code
    222  1.1      cgd 			 * rn_search_m here instead of always
    223  1.1      cgd 			 * copying and masking.
    224  1.1      cgd 			 */
    225  1.1      cgd 			off = min(t->rn_off, matched_off);
    226  1.1      cgd 			mstart = maskedKey + off;
    227  1.1      cgd 			do {
    228  1.1      cgd 				cp2 = mstart;
    229  1.1      cgd 				cp3 = m->rm_mask + off;
    230  1.1      cgd 				for (cp = v + off; cp < cplim;)
    231  1.1      cgd 					*cp2++ =  *cp++ & *cp3++;
    232  1.1      cgd 				x = rn_search(maskedKey, t);
    233  1.1      cgd 				while (x && x->rn_mask != m->rm_mask)
    234  1.1      cgd 					x = x->rn_dupedkey;
    235  1.1      cgd 				if (x &&
    236  1.1      cgd 				    (Bcmp(mstart, x->rn_key + off,
    237  1.1      cgd 					vlen - off) == 0))
    238  1.1      cgd 					    return x;
    239  1.1      cgd 			} while (m = m->rm_mklist);
    240  1.1      cgd 		}
    241  1.6  mycroft 	} while (t != top);
    242  1.1      cgd 	return 0;
    243  1.1      cgd };
    244  1.1      cgd 
    245  1.1      cgd #ifdef RN_DEBUG
    246  1.1      cgd int	rn_nodenum;
    247  1.1      cgd struct	radix_node *rn_clist;
    248  1.1      cgd int	rn_saveinfo;
    249  1.6  mycroft int	rn_debug =  1;
    250  1.1      cgd #endif
    251  1.1      cgd 
    252  1.1      cgd struct radix_node *
    253  1.1      cgd rn_newpair(v, b, nodes)
    254  1.6  mycroft 	void *v;
    255  1.6  mycroft 	int b;
    256  1.1      cgd 	struct radix_node nodes[2];
    257  1.1      cgd {
    258  1.1      cgd 	register struct radix_node *tt = nodes, *t = tt + 1;
    259  1.1      cgd 	t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7);
    260  1.1      cgd 	t->rn_l = tt; t->rn_off = b >> 3;
    261  1.6  mycroft 	tt->rn_b = -1; tt->rn_key = (caddr_t)v; tt->rn_p = t;
    262  1.1      cgd 	tt->rn_flags = t->rn_flags = RNF_ACTIVE;
    263  1.1      cgd #ifdef RN_DEBUG
    264  1.1      cgd 	tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
    265  1.1      cgd 	tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
    266  1.1      cgd #endif
    267  1.1      cgd 	return t;
    268  1.1      cgd }
    269  1.1      cgd 
    270  1.1      cgd struct radix_node *
    271  1.6  mycroft rn_insert(v_arg, head, dupentry, nodes)
    272  1.6  mycroft 	void *v_arg;
    273  1.6  mycroft 	struct radix_node_head *head;
    274  1.1      cgd 	int *dupentry;
    275  1.1      cgd 	struct radix_node nodes[2];
    276  1.1      cgd {
    277  1.6  mycroft 	caddr_t v = v_arg;
    278  1.6  mycroft 	struct radix_node *top = head->rnh_treetop;
    279  1.6  mycroft 	int head_off = top->rn_off, vlen = (int)*((u_char *)v);
    280  1.6  mycroft 	register struct radix_node *t = rn_search(v_arg, top);
    281  1.1      cgd 	register caddr_t cp = v + head_off;
    282  1.1      cgd 	register int b;
    283  1.1      cgd 	struct radix_node *tt;
    284  1.1      cgd     	/*
    285  1.1      cgd 	 *find first bit at which v and t->rn_key differ
    286  1.1      cgd 	 */
    287  1.1      cgd     {
    288  1.1      cgd 	register caddr_t cp2 = t->rn_key + head_off;
    289  1.1      cgd 	register int cmp_res;
    290  1.1      cgd 	caddr_t cplim = v + vlen;
    291  1.1      cgd 
    292  1.1      cgd 	while (cp < cplim)
    293  1.1      cgd 		if (*cp2++ != *cp++)
    294  1.1      cgd 			goto on1;
    295  1.1      cgd 	*dupentry = 1;
    296  1.1      cgd 	return t;
    297  1.1      cgd on1:
    298  1.1      cgd 	*dupentry = 0;
    299  1.1      cgd 	cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
    300  1.1      cgd 	for (b = (cp - v) << 3; cmp_res; b--)
    301  1.1      cgd 		cmp_res >>= 1;
    302  1.1      cgd     }
    303  1.1      cgd     {
    304  1.6  mycroft 	register struct radix_node *p, *x = top;
    305  1.1      cgd 	cp = v;
    306  1.1      cgd 	do {
    307  1.1      cgd 		p = x;
    308  1.1      cgd 		if (cp[x->rn_off] & x->rn_bmask)
    309  1.1      cgd 			x = x->rn_r;
    310  1.1      cgd 		else x = x->rn_l;
    311  1.1      cgd 	} while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
    312  1.1      cgd #ifdef RN_DEBUG
    313  1.1      cgd 	if (rn_debug)
    314  1.1      cgd 		printf("Going In:\n"), traverse(p);
    315  1.1      cgd #endif
    316  1.6  mycroft 	t = rn_newpair(v_arg, b, nodes); tt = t->rn_l;
    317  1.1      cgd 	if ((cp[p->rn_off] & p->rn_bmask) == 0)
    318  1.1      cgd 		p->rn_l = t;
    319  1.1      cgd 	else
    320  1.1      cgd 		p->rn_r = t;
    321  1.1      cgd 	x->rn_p = t; t->rn_p = p; /* frees x, p as temp vars below */
    322  1.1      cgd 	if ((cp[t->rn_off] & t->rn_bmask) == 0) {
    323  1.1      cgd 		t->rn_r = x;
    324  1.1      cgd 	} else {
    325  1.1      cgd 		t->rn_r = tt; t->rn_l = x;
    326  1.1      cgd 	}
    327  1.1      cgd #ifdef RN_DEBUG
    328  1.1      cgd 	if (rn_debug)
    329  1.1      cgd 		printf("Coming out:\n"), traverse(p);
    330  1.1      cgd #endif
    331  1.1      cgd     }
    332  1.1      cgd 	return (tt);
    333  1.1      cgd }
    334  1.1      cgd 
    335  1.1      cgd struct radix_node *
    336  1.6  mycroft rn_addmask(n_arg, search, skip)
    337  1.6  mycroft 	int search, skip;
    338  1.6  mycroft 	void *n_arg;
    339  1.1      cgd {
    340  1.6  mycroft 	caddr_t netmask = (caddr_t)n_arg;
    341  1.1      cgd 	register struct radix_node *x;
    342  1.1      cgd 	register caddr_t cp, cplim;
    343  1.1      cgd 	register int b, mlen, j;
    344  1.1      cgd 	int maskduplicated;
    345  1.1      cgd 
    346  1.1      cgd 	mlen = *(u_char *)netmask;
    347  1.1      cgd 	if (search) {
    348  1.6  mycroft 		x = rn_search(netmask, rn_masktop);
    349  1.1      cgd 		mlen = *(u_char *)netmask;
    350  1.1      cgd 		if (Bcmp(netmask, x->rn_key, mlen) == 0)
    351  1.1      cgd 			return (x);
    352  1.1      cgd 	}
    353  1.6  mycroft 	R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
    354  1.1      cgd 	if (x == 0)
    355  1.1      cgd 		return (0);
    356  1.6  mycroft 	Bzero(x, max_keylen + 2 * sizeof (*x));
    357  1.1      cgd 	cp = (caddr_t)(x + 2);
    358  1.1      cgd 	Bcopy(netmask, cp, mlen);
    359  1.1      cgd 	netmask = cp;
    360  1.6  mycroft 	x = rn_insert(netmask, mask_rnhead, &maskduplicated, x);
    361  1.1      cgd 	/*
    362  1.1      cgd 	 * Calculate index of mask.
    363  1.1      cgd 	 */
    364  1.1      cgd 	cplim = netmask + mlen;
    365  1.1      cgd 	for (cp = netmask + skip; cp < cplim; cp++)
    366  1.1      cgd 		if (*(u_char *)cp != 0xff)
    367  1.1      cgd 			break;
    368  1.1      cgd 	b = (cp - netmask) << 3;
    369  1.1      cgd 	if (cp != cplim) {
    370  1.1      cgd 		if (*cp != 0) {
    371  1.1      cgd 			gotOddMasks = 1;
    372  1.1      cgd 			for (j = 0x80; j; b++, j >>= 1)
    373  1.1      cgd 				if ((j & *cp) == 0)
    374  1.1      cgd 					break;
    375  1.1      cgd 		}
    376  1.1      cgd 	}
    377  1.1      cgd 	x->rn_b = -1 - b;
    378  1.1      cgd 	return (x);
    379  1.1      cgd }
    380  1.1      cgd 
    381  1.1      cgd struct radix_node *
    382  1.6  mycroft rn_addroute(v_arg, n_arg, head, treenodes)
    383  1.6  mycroft 	void *v_arg, *n_arg;
    384  1.6  mycroft 	struct radix_node_head *head;
    385  1.1      cgd 	struct radix_node treenodes[2];
    386  1.1      cgd {
    387  1.6  mycroft 	caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
    388  1.1      cgd 	register struct radix_node *t, *x, *tt;
    389  1.6  mycroft 	struct radix_node *saved_tt, *top = head->rnh_treetop;
    390  1.1      cgd 	short b = 0, b_leaf;
    391  1.6  mycroft 	int mlen, keyduplicated;
    392  1.6  mycroft 	caddr_t cplim;
    393  1.1      cgd 	struct radix_mask *m, **mp;
    394  1.1      cgd 
    395  1.1      cgd 	/*
    396  1.1      cgd 	 * In dealing with non-contiguous masks, there may be
    397  1.1      cgd 	 * many different routes which have the same mask.
    398  1.1      cgd 	 * We will find it useful to have a unique pointer to
    399  1.1      cgd 	 * the mask to speed avoiding duplicate references at
    400  1.1      cgd 	 * nodes and possibly save time in calculating indices.
    401  1.1      cgd 	 */
    402  1.1      cgd 	if (netmask)  {
    403  1.6  mycroft 		x = rn_search(netmask, rn_masktop);
    404  1.1      cgd 		mlen = *(u_char *)netmask;
    405  1.1      cgd 		if (Bcmp(netmask, x->rn_key, mlen) != 0) {
    406  1.6  mycroft 			x = rn_addmask(netmask, 0, top->rn_off);
    407  1.1      cgd 			if (x == 0)
    408  1.1      cgd 				return (0);
    409  1.1      cgd 		}
    410  1.1      cgd 		netmask = x->rn_key;
    411  1.1      cgd 		b = -1 - x->rn_b;
    412  1.1      cgd 	}
    413  1.1      cgd 	/*
    414  1.1      cgd 	 * Deal with duplicated keys: attach node to previous instance
    415  1.1      cgd 	 */
    416  1.1      cgd 	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
    417  1.1      cgd 	if (keyduplicated) {
    418  1.1      cgd 		do {
    419  1.1      cgd 			if (tt->rn_mask == netmask)
    420  1.1      cgd 				return (0);
    421  1.1      cgd 			t = tt;
    422  1.6  mycroft 			if (netmask == 0 ||
    423  1.6  mycroft 			    (tt->rn_mask && rn_refines(netmask, tt->rn_mask)))
    424  1.6  mycroft 				break;
    425  1.1      cgd 		} while (tt = tt->rn_dupedkey);
    426  1.1      cgd 		/*
    427  1.1      cgd 		 * If the mask is not duplicated, we wouldn't
    428  1.1      cgd 		 * find it among possible duplicate key entries
    429  1.1      cgd 		 * anyway, so the above test doesn't hurt.
    430  1.1      cgd 		 *
    431  1.6  mycroft 		 * We sort the masks for a duplicated key the same way as
    432  1.6  mycroft 		 * in a masklist -- most specific to least specific.
    433  1.6  mycroft 		 * This may require the unfortunate nuisance of relocating
    434  1.1      cgd 		 * the head of the list.
    435  1.1      cgd 		 */
    436  1.6  mycroft 		if (tt && t == saved_tt) {
    437  1.6  mycroft 			struct	radix_node *xx = x;
    438  1.6  mycroft 			/* link in at head of list */
    439  1.6  mycroft 			(tt = treenodes)->rn_dupedkey = t;
    440  1.6  mycroft 			tt->rn_flags = t->rn_flags;
    441  1.6  mycroft 			tt->rn_p = x = t->rn_p;
    442  1.6  mycroft 			if (x->rn_l == t) x->rn_l = tt; else x->rn_r = tt;
    443  1.6  mycroft 			saved_tt = tt; x = xx;
    444  1.6  mycroft 		} else {
    445  1.6  mycroft 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
    446  1.6  mycroft 			t->rn_dupedkey = tt;
    447  1.6  mycroft 		}
    448  1.1      cgd #ifdef RN_DEBUG
    449  1.1      cgd 		t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
    450  1.1      cgd 		tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
    451  1.1      cgd #endif
    452  1.1      cgd 		t = saved_tt;
    453  1.1      cgd 		tt->rn_key = (caddr_t) v;
    454  1.1      cgd 		tt->rn_b = -1;
    455  1.1      cgd 		tt->rn_flags = t->rn_flags & ~RNF_ROOT;
    456  1.1      cgd 	}
    457  1.1      cgd 	/*
    458  1.1      cgd 	 * Put mask in tree.
    459  1.1      cgd 	 */
    460  1.1      cgd 	if (netmask) {
    461  1.1      cgd 		tt->rn_mask = netmask;
    462  1.1      cgd 		tt->rn_b = x->rn_b;
    463  1.1      cgd 	}
    464  1.1      cgd 	t = saved_tt->rn_p;
    465  1.1      cgd 	b_leaf = -1 - t->rn_b;
    466  1.1      cgd 	if (t->rn_r == saved_tt) x = t->rn_l; else x = t->rn_r;
    467  1.1      cgd 	/* Promote general routes from below */
    468  1.1      cgd 	if (x->rn_b < 0) {
    469  1.1      cgd 		if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
    470  1.1      cgd 			MKGet(m);
    471  1.1      cgd 			if (m) {
    472  1.1      cgd 				Bzero(m, sizeof *m);
    473  1.1      cgd 				m->rm_b = x->rn_b;
    474  1.1      cgd 				m->rm_mask = x->rn_mask;
    475  1.1      cgd 				x->rn_mklist = t->rn_mklist = m;
    476  1.1      cgd 			}
    477  1.1      cgd 		}
    478  1.1      cgd 	} else if (x->rn_mklist) {
    479  1.1      cgd 		/*
    480  1.1      cgd 		 * Skip over masks whose index is > that of new node
    481  1.1      cgd 		 */
    482  1.1      cgd 		for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist)
    483  1.1      cgd 			if (m->rm_b >= b_leaf)
    484  1.1      cgd 				break;
    485  1.1      cgd 		t->rn_mklist = m; *mp = 0;
    486  1.1      cgd 	}
    487  1.1      cgd 	/* Add new route to highest possible ancestor's list */
    488  1.1      cgd 	if ((netmask == 0) || (b > t->rn_b ))
    489  1.1      cgd 		return tt; /* can't lift at all */
    490  1.1      cgd 	b_leaf = tt->rn_b;
    491  1.1      cgd 	do {
    492  1.1      cgd 		x = t;
    493  1.1      cgd 		t = t->rn_p;
    494  1.6  mycroft 	} while (b <= t->rn_b && x != top);
    495  1.1      cgd 	/*
    496  1.1      cgd 	 * Search through routes associated with node to
    497  1.1      cgd 	 * insert new route according to index.
    498  1.1      cgd 	 * For nodes of equal index, place more specific
    499  1.1      cgd 	 * masks first.
    500  1.1      cgd 	 */
    501  1.1      cgd 	cplim = netmask + mlen;
    502  1.1      cgd 	for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist) {
    503  1.1      cgd 		if (m->rm_b < b_leaf)
    504  1.1      cgd 			continue;
    505  1.1      cgd 		if (m->rm_b > b_leaf)
    506  1.1      cgd 			break;
    507  1.1      cgd 		if (m->rm_mask == netmask) {
    508  1.1      cgd 			m->rm_refs++;
    509  1.1      cgd 			tt->rn_mklist = m;
    510  1.1      cgd 			return tt;
    511  1.1      cgd 		}
    512  1.6  mycroft 		if (rn_refines(netmask, m->rm_mask))
    513  1.6  mycroft 			break;
    514  1.1      cgd 	}
    515  1.1      cgd 	MKGet(m);
    516  1.1      cgd 	if (m == 0) {
    517  1.1      cgd 		printf("Mask for route not entered\n");
    518  1.1      cgd 		return (tt);
    519  1.1      cgd 	}
    520  1.1      cgd 	Bzero(m, sizeof *m);
    521  1.1      cgd 	m->rm_b = b_leaf;
    522  1.1      cgd 	m->rm_mask = netmask;
    523  1.1      cgd 	m->rm_mklist = *mp;
    524  1.1      cgd 	*mp = m;
    525  1.1      cgd 	tt->rn_mklist = m;
    526  1.1      cgd 	return tt;
    527  1.1      cgd }
    528  1.1      cgd 
    529  1.1      cgd struct radix_node *
    530  1.6  mycroft rn_delete(v_arg, netmask_arg, head)
    531  1.6  mycroft 	void *v_arg, *netmask_arg;
    532  1.6  mycroft 	struct radix_node_head *head;
    533  1.1      cgd {
    534  1.6  mycroft 	register struct radix_node *t, *p, *x, *tt;
    535  1.1      cgd 	struct radix_mask *m, *saved_m, **mp;
    536  1.6  mycroft 	struct radix_node *dupedkey, *saved_tt, *top;
    537  1.6  mycroft 	caddr_t v, netmask;
    538  1.6  mycroft 	int b, head_off, vlen;
    539  1.1      cgd 
    540  1.6  mycroft 	v = v_arg;
    541  1.6  mycroft 	netmask = netmask_arg;
    542  1.6  mycroft 	x = head->rnh_treetop;
    543  1.6  mycroft 	tt = rn_search(v, x);
    544  1.6  mycroft 	head_off = x->rn_off;
    545  1.6  mycroft 	vlen =  *(u_char *)v;
    546  1.6  mycroft 	saved_tt = tt;
    547  1.6  mycroft 	top = x;
    548  1.1      cgd 	if (tt == 0 ||
    549  1.1      cgd 	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
    550  1.1      cgd 		return (0);
    551  1.1      cgd 	/*
    552  1.1      cgd 	 * Delete our route from mask lists.
    553  1.1      cgd 	 */
    554  1.1      cgd 	if (dupedkey = tt->rn_dupedkey) {
    555  1.1      cgd 		if (netmask)
    556  1.6  mycroft 			netmask = rn_search(netmask, rn_masktop)->rn_key;
    557  1.1      cgd 		while (tt->rn_mask != netmask)
    558  1.1      cgd 			if ((tt = tt->rn_dupedkey) == 0)
    559  1.1      cgd 				return (0);
    560  1.1      cgd 	}
    561  1.1      cgd 	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
    562  1.1      cgd 		goto on1;
    563  1.1      cgd 	if (m->rm_mask != tt->rn_mask) {
    564  1.1      cgd 		printf("rn_delete: inconsistent annotation\n");
    565  1.1      cgd 		goto on1;
    566  1.1      cgd 	}
    567  1.1      cgd 	if (--m->rm_refs >= 0)
    568  1.1      cgd 		goto on1;
    569  1.1      cgd 	b = -1 - tt->rn_b;
    570  1.1      cgd 	t = saved_tt->rn_p;
    571  1.1      cgd 	if (b > t->rn_b)
    572  1.1      cgd 		goto on1; /* Wasn't lifted at all */
    573  1.1      cgd 	do {
    574  1.1      cgd 		x = t;
    575  1.1      cgd 		t = t->rn_p;
    576  1.6  mycroft 	} while (b <= t->rn_b && x != top);
    577  1.1      cgd 	for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist)
    578  1.1      cgd 		if (m == saved_m) {
    579  1.1      cgd 			*mp = m->rm_mklist;
    580  1.1      cgd 			MKFree(m);
    581  1.1      cgd 			break;
    582  1.1      cgd 		}
    583  1.1      cgd 	if (m == 0)
    584  1.1      cgd 		printf("rn_delete: couldn't find our annotation\n");
    585  1.1      cgd on1:
    586  1.1      cgd 	/*
    587  1.1      cgd 	 * Eliminate us from tree
    588  1.1      cgd 	 */
    589  1.1      cgd 	if (tt->rn_flags & RNF_ROOT)
    590  1.1      cgd 		return (0);
    591  1.1      cgd #ifdef RN_DEBUG
    592  1.1      cgd 	/* Get us out of the creation list */
    593  1.1      cgd 	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
    594  1.1      cgd 	if (t) t->rn_ybro = tt->rn_ybro;
    595  1.6  mycroft #endif
    596  1.1      cgd 	t = tt->rn_p;
    597  1.1      cgd 	if (dupedkey) {
    598  1.1      cgd 		if (tt == saved_tt) {
    599  1.1      cgd 			x = dupedkey; x->rn_p = t;
    600  1.1      cgd 			if (t->rn_l == tt) t->rn_l = x; else t->rn_r = x;
    601  1.6  mycroft 		} else {
    602  1.6  mycroft 			for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
    603  1.6  mycroft 				p = p->rn_dupedkey;
    604  1.6  mycroft 			if (p) p->rn_dupedkey = tt->rn_dupedkey;
    605  1.6  mycroft 			else printf("rn_delete: couldn't find us\n");
    606  1.6  mycroft 		}
    607  1.6  mycroft 		t = tt + 1;
    608  1.6  mycroft 		if  (t->rn_flags & RNF_ACTIVE) {
    609  1.1      cgd #ifndef RN_DEBUG
    610  1.6  mycroft 			*++x = *t; p = t->rn_p;
    611  1.1      cgd #else
    612  1.6  mycroft 			b = t->rn_info; *++x = *t; t->rn_info = b; p = t->rn_p;
    613  1.1      cgd #endif
    614  1.1      cgd 			if (p->rn_l == t) p->rn_l = x; else p->rn_r = x;
    615  1.1      cgd 			x->rn_l->rn_p = x; x->rn_r->rn_p = x;
    616  1.1      cgd 		}
    617  1.1      cgd 		goto out;
    618  1.1      cgd 	}
    619  1.1      cgd 	if (t->rn_l == tt) x = t->rn_r; else x = t->rn_l;
    620  1.1      cgd 	p = t->rn_p;
    621  1.1      cgd 	if (p->rn_r == t) p->rn_r = x; else p->rn_l = x;
    622  1.1      cgd 	x->rn_p = p;
    623  1.1      cgd 	/*
    624  1.1      cgd 	 * Demote routes attached to us.
    625  1.1      cgd 	 */
    626  1.1      cgd 	if (t->rn_mklist) {
    627  1.1      cgd 		if (x->rn_b >= 0) {
    628  1.1      cgd 			for (mp = &x->rn_mklist; m = *mp;)
    629  1.1      cgd 				mp = &m->rm_mklist;
    630  1.1      cgd 			*mp = t->rn_mklist;
    631  1.1      cgd 		} else {
    632  1.1      cgd 			for (m = t->rn_mklist; m;) {
    633  1.1      cgd 				struct radix_mask *mm = m->rm_mklist;
    634  1.1      cgd 				if (m == x->rn_mklist && (--(m->rm_refs) < 0)) {
    635  1.1      cgd 					x->rn_mklist = 0;
    636  1.1      cgd 					MKFree(m);
    637  1.1      cgd 				} else
    638  1.1      cgd 					printf("%s %x at %x\n",
    639  1.1      cgd 					    "rn_delete: Orphaned Mask", m, x);
    640  1.1      cgd 				m = mm;
    641  1.1      cgd 			}
    642  1.1      cgd 		}
    643  1.1      cgd 	}
    644  1.1      cgd 	/*
    645  1.1      cgd 	 * We may be holding an active internal node in the tree.
    646  1.1      cgd 	 */
    647  1.1      cgd 	x = tt + 1;
    648  1.1      cgd 	if (t != x) {
    649  1.1      cgd #ifndef RN_DEBUG
    650  1.1      cgd 		*t = *x;
    651  1.1      cgd #else
    652  1.1      cgd 		b = t->rn_info; *t = *x; t->rn_info = b;
    653  1.1      cgd #endif
    654  1.1      cgd 		t->rn_l->rn_p = t; t->rn_r->rn_p = t;
    655  1.1      cgd 		p = x->rn_p;
    656  1.1      cgd 		if (p->rn_l == x) p->rn_l = t; else p->rn_r = t;
    657  1.1      cgd 	}
    658  1.1      cgd out:
    659  1.1      cgd 	tt->rn_flags &= ~RNF_ACTIVE;
    660  1.1      cgd 	tt[1].rn_flags &= ~RNF_ACTIVE;
    661  1.1      cgd 	return (tt);
    662  1.1      cgd }
    663  1.1      cgd 
    664  1.6  mycroft int
    665  1.6  mycroft rn_walktree(h, f, w)
    666  1.6  mycroft 	struct radix_node_head *h;
    667  1.6  mycroft 	register int (*f)();
    668  1.6  mycroft 	void *w;
    669  1.6  mycroft {
    670  1.6  mycroft 	int error;
    671  1.6  mycroft 	struct radix_node *base, *next;
    672  1.6  mycroft 	register struct radix_node *rn = h->rnh_treetop;
    673  1.6  mycroft 	/*
    674  1.6  mycroft 	 * This gets complicated because we may delete the node
    675  1.6  mycroft 	 * while applying the function f to it, so we need to calculate
    676  1.6  mycroft 	 * the successor node in advance.
    677  1.6  mycroft 	 */
    678  1.6  mycroft 	/* First time through node, go left */
    679  1.6  mycroft 	while (rn->rn_b >= 0)
    680  1.6  mycroft 		rn = rn->rn_l;
    681  1.6  mycroft 	for (;;) {
    682  1.6  mycroft 		base = rn;
    683  1.6  mycroft 		/* If at right child go back up, otherwise, go right */
    684  1.6  mycroft 		while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
    685  1.6  mycroft 			rn = rn->rn_p;
    686  1.6  mycroft 		/* Find the next *leaf* since next node might vanish, too */
    687  1.6  mycroft 		for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
    688  1.6  mycroft 			rn = rn->rn_l;
    689  1.6  mycroft 		next = rn;
    690  1.6  mycroft 		/* Process leaves */
    691  1.6  mycroft 		while (rn = base) {
    692  1.6  mycroft 			base = rn->rn_dupedkey;
    693  1.6  mycroft 			if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
    694  1.6  mycroft 				return (error);
    695  1.6  mycroft 		}
    696  1.6  mycroft 		rn = next;
    697  1.6  mycroft 		if (rn->rn_flags & RNF_ROOT)
    698  1.6  mycroft 			return (0);
    699  1.6  mycroft 	}
    700  1.6  mycroft 	/* NOTREACHED */
    701  1.6  mycroft }
    702  1.6  mycroft 
    703  1.6  mycroft int
    704  1.6  mycroft rn_inithead(head, off)
    705  1.6  mycroft 	void **head;
    706  1.6  mycroft 	int off;
    707  1.1      cgd {
    708  1.1      cgd 	register struct radix_node_head *rnh;
    709  1.1      cgd 	register struct radix_node *t, *tt, *ttt;
    710  1.1      cgd 	if (*head)
    711  1.1      cgd 		return (1);
    712  1.1      cgd 	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
    713  1.1      cgd 	if (rnh == 0)
    714  1.1      cgd 		return (0);
    715  1.1      cgd 	Bzero(rnh, sizeof (*rnh));
    716  1.1      cgd 	*head = rnh;
    717  1.1      cgd 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
    718  1.1      cgd 	ttt = rnh->rnh_nodes + 2;
    719  1.1      cgd 	t->rn_r = ttt;
    720  1.1      cgd 	t->rn_p = t;
    721  1.1      cgd 	tt = t->rn_l;
    722  1.1      cgd 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
    723  1.1      cgd 	tt->rn_b = -1 - off;
    724  1.1      cgd 	*ttt = *tt;
    725  1.1      cgd 	ttt->rn_key = rn_ones;
    726  1.6  mycroft 	rnh->rnh_addaddr = rn_addroute;
    727  1.6  mycroft 	rnh->rnh_deladdr = rn_delete;
    728  1.6  mycroft 	rnh->rnh_matchaddr = rn_match;
    729  1.6  mycroft 	rnh->rnh_walktree = rn_walktree;
    730  1.1      cgd 	rnh->rnh_treetop = t;
    731  1.1      cgd 	return (1);
    732  1.6  mycroft }
    733  1.6  mycroft 
    734  1.6  mycroft void
    735  1.6  mycroft rn_init()
    736  1.6  mycroft {
    737  1.6  mycroft 	char *cp, *cplim;
    738  1.6  mycroft #ifdef KERNEL
    739  1.6  mycroft 	struct domain *dom;
    740  1.6  mycroft 
    741  1.6  mycroft 	for (dom = domains; dom; dom = dom->dom_next)
    742  1.6  mycroft 		if (dom->dom_maxrtkey > max_keylen)
    743  1.6  mycroft 			max_keylen = dom->dom_maxrtkey;
    744  1.6  mycroft #endif
    745  1.6  mycroft 	if (max_keylen == 0) {
    746  1.6  mycroft 		printf("rn_init: radix functions require max_keylen be set\n");
    747  1.6  mycroft 		return;
    748  1.6  mycroft 	}
    749  1.6  mycroft 	R_Malloc(rn_zeros, char *, 3 * max_keylen);
    750  1.6  mycroft 	if (rn_zeros == NULL)
    751  1.6  mycroft 		panic("rn_init");
    752  1.6  mycroft 	Bzero(rn_zeros, 3 * max_keylen);
    753  1.6  mycroft 	rn_ones = cp = rn_zeros + max_keylen;
    754  1.6  mycroft 	maskedKey = cplim = rn_ones + max_keylen;
    755  1.6  mycroft 	while (cp < cplim)
    756  1.6  mycroft 		*cp++ = -1;
    757  1.6  mycroft 	if (rn_inithead((void **)&mask_rnhead, 0) == 0)
    758  1.6  mycroft 		panic("rn_init 2");
    759  1.1      cgd }
    760