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