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