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