xref: /xnu-8792.81.2/bsd/net/radix.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions /*
2*19c3b8c2SApple OSS Distributions  * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3*19c3b8c2SApple OSS Distributions  *
4*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*19c3b8c2SApple OSS Distributions  *
6*19c3b8c2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*19c3b8c2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*19c3b8c2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*19c3b8c2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*19c3b8c2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*19c3b8c2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*19c3b8c2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*19c3b8c2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*19c3b8c2SApple OSS Distributions  *
15*19c3b8c2SApple OSS Distributions  * Please obtain a copy of the License at
16*19c3b8c2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*19c3b8c2SApple OSS Distributions  *
18*19c3b8c2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*19c3b8c2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*19c3b8c2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*19c3b8c2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*19c3b8c2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*19c3b8c2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*19c3b8c2SApple OSS Distributions  * limitations under the License.
25*19c3b8c2SApple OSS Distributions  *
26*19c3b8c2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*19c3b8c2SApple OSS Distributions  */
28*19c3b8c2SApple OSS Distributions /*
29*19c3b8c2SApple OSS Distributions  * Copyright (c) 1988, 1989, 1993
30*19c3b8c2SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*19c3b8c2SApple OSS Distributions  *
32*19c3b8c2SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*19c3b8c2SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*19c3b8c2SApple OSS Distributions  * are met:
35*19c3b8c2SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*19c3b8c2SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
37*19c3b8c2SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*19c3b8c2SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
39*19c3b8c2SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
40*19c3b8c2SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
41*19c3b8c2SApple OSS Distributions  *    must display the following acknowledgement:
42*19c3b8c2SApple OSS Distributions  *	This product includes software developed by the University of
43*19c3b8c2SApple OSS Distributions  *	California, Berkeley and its contributors.
44*19c3b8c2SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
45*19c3b8c2SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
46*19c3b8c2SApple OSS Distributions  *    without specific prior written permission.
47*19c3b8c2SApple OSS Distributions  *
48*19c3b8c2SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*19c3b8c2SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*19c3b8c2SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*19c3b8c2SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*19c3b8c2SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*19c3b8c2SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*19c3b8c2SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*19c3b8c2SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*19c3b8c2SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*19c3b8c2SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*19c3b8c2SApple OSS Distributions  * SUCH DAMAGE.
59*19c3b8c2SApple OSS Distributions  *
60*19c3b8c2SApple OSS Distributions  *	@(#)radix.c	8.4 (Berkeley) 11/2/94
61*19c3b8c2SApple OSS Distributions  * $FreeBSD: src/sys/net/radix.c,v 1.20.2.2 2001/03/06 00:56:50 obrien Exp $
62*19c3b8c2SApple OSS Distributions  */
63*19c3b8c2SApple OSS Distributions 
64*19c3b8c2SApple OSS Distributions /*
65*19c3b8c2SApple OSS Distributions  * Routines to build and maintain radix trees for routing lookups.
66*19c3b8c2SApple OSS Distributions  */
67*19c3b8c2SApple OSS Distributions #ifndef _RADIX_H_
68*19c3b8c2SApple OSS Distributions #include <sys/param.h>
69*19c3b8c2SApple OSS Distributions #include <sys/systm.h>
70*19c3b8c2SApple OSS Distributions #include <sys/domain.h>
71*19c3b8c2SApple OSS Distributions #include <sys/syslog.h>
72*19c3b8c2SApple OSS Distributions #include <net/radix.h>
73*19c3b8c2SApple OSS Distributions #include <sys/socket.h>
74*19c3b8c2SApple OSS Distributions #include <sys/socketvar.h>
75*19c3b8c2SApple OSS Distributions #include <kern/locks.h>
76*19c3b8c2SApple OSS Distributions #endif
77*19c3b8c2SApple OSS Distributions 
78*19c3b8c2SApple OSS Distributions static int      rn_walktree_from(struct radix_node_head *h, void *a,
79*19c3b8c2SApple OSS Distributions     void *m, walktree_f_t *f, void *w);
80*19c3b8c2SApple OSS Distributions static int rn_walktree(struct radix_node_head *, walktree_f_t *, void *);
81*19c3b8c2SApple OSS Distributions static struct radix_node *rn_insert(void *, struct radix_node_head *, int *, struct radix_node[2]);
82*19c3b8c2SApple OSS Distributions static struct radix_node *rn_newpair(void *, int, struct radix_node[2]);
83*19c3b8c2SApple OSS Distributions static struct radix_node *rn_search(void *, struct radix_node *);
84*19c3b8c2SApple OSS Distributions static struct radix_node *rn_search_m(void *, struct radix_node *, void *);
85*19c3b8c2SApple OSS Distributions 
86*19c3b8c2SApple OSS Distributions static int max_keylen;
87*19c3b8c2SApple OSS Distributions static struct radix_mask *rn_mkfreelist;
88*19c3b8c2SApple OSS Distributions static struct radix_node_head *mask_rnhead;
89*19c3b8c2SApple OSS Distributions static char *addmask_key;
90*19c3b8c2SApple OSS Distributions static char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
91*19c3b8c2SApple OSS Distributions static char *rn_zeros, *rn_ones;
92*19c3b8c2SApple OSS Distributions 
93*19c3b8c2SApple OSS Distributions static zone_t radix_node_zone;
94*19c3b8c2SApple OSS Distributions KALLOC_TYPE_DEFINE(radix_node_head_zone, struct radix_node_head, KT_DEFAULT);
95*19c3b8c2SApple OSS Distributions 
96*19c3b8c2SApple OSS Distributions #define rn_masktop (mask_rnhead->rnh_treetop)
97*19c3b8c2SApple OSS Distributions #undef Bcmp
98*19c3b8c2SApple OSS Distributions #define Bcmp(a, b, l) \
99*19c3b8c2SApple OSS Distributions 	(l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (uint32_t)l))
100*19c3b8c2SApple OSS Distributions 
101*19c3b8c2SApple OSS Distributions static int      rn_lexobetter(void *m_arg, void *n_arg);
102*19c3b8c2SApple OSS Distributions static struct radix_mask *
103*19c3b8c2SApple OSS Distributions rn_new_radix_mask(struct radix_node *tt,
104*19c3b8c2SApple OSS Distributions     struct radix_mask *next);
105*19c3b8c2SApple OSS Distributions static int rn_satisfies_leaf(char *trial, struct radix_node *leaf, int skip,
106*19c3b8c2SApple OSS Distributions     rn_matchf_t *f, void *w);
107*19c3b8c2SApple OSS Distributions 
108*19c3b8c2SApple OSS Distributions #define RN_MATCHF(rn, f, arg)   (f == NULL || (*f)((rn), arg))
109*19c3b8c2SApple OSS Distributions 
110*19c3b8c2SApple OSS Distributions /*
111*19c3b8c2SApple OSS Distributions  * The data structure for the keys is a radix tree with one way
112*19c3b8c2SApple OSS Distributions  * branching removed.  The index rn_bit at an internal node n represents a bit
113*19c3b8c2SApple OSS Distributions  * position to be tested.  The tree is arranged so that all descendants
114*19c3b8c2SApple OSS Distributions  * of a node n have keys whose bits all agree up to position rn_bit - 1.
115*19c3b8c2SApple OSS Distributions  * (We say the index of n is rn_bit.)
116*19c3b8c2SApple OSS Distributions  *
117*19c3b8c2SApple OSS Distributions  * There is at least one descendant which has a one bit at position rn_bit,
118*19c3b8c2SApple OSS Distributions  * and at least one with a zero there.
119*19c3b8c2SApple OSS Distributions  *
120*19c3b8c2SApple OSS Distributions  * A route is determined by a pair of key and mask.  We require that the
121*19c3b8c2SApple OSS Distributions  * bit-wise logical and of the key and mask to be the key.
122*19c3b8c2SApple OSS Distributions  * We define the index of a route to associated with the mask to be
123*19c3b8c2SApple OSS Distributions  * the first bit number in the mask where 0 occurs (with bit number 0
124*19c3b8c2SApple OSS Distributions  * representing the highest order bit).
125*19c3b8c2SApple OSS Distributions  *
126*19c3b8c2SApple OSS Distributions  * We say a mask is normal if every bit is 0, past the index of the mask.
127*19c3b8c2SApple OSS Distributions  * If a node n has a descendant (k, m) with index(m) == index(n) == rn_bit,
128*19c3b8c2SApple OSS Distributions  * and m is a normal mask, then the route applies to every descendant of n.
129*19c3b8c2SApple OSS Distributions  * If the index(m) < rn_bit, this implies the trailing last few bits of k
130*19c3b8c2SApple OSS Distributions  * before bit b are all 0, (and hence consequently true of every descendant
131*19c3b8c2SApple OSS Distributions  * of n), so the route applies to all descendants of the node as well.
132*19c3b8c2SApple OSS Distributions  *
133*19c3b8c2SApple OSS Distributions  * Similar logic shows that a non-normal mask m such that
134*19c3b8c2SApple OSS Distributions  * index(m) <= index(n) could potentially apply to many children of n.
135*19c3b8c2SApple OSS Distributions  * Thus, for each non-host route, we attach its mask to a list at an internal
136*19c3b8c2SApple OSS Distributions  * node as high in the tree as we can go.
137*19c3b8c2SApple OSS Distributions  *
138*19c3b8c2SApple OSS Distributions  * The present version of the code makes use of normal routes in short-
139*19c3b8c2SApple OSS Distributions  * circuiting an explict mask and compare operation when testing whether
140*19c3b8c2SApple OSS Distributions  * a key satisfies a normal route, and also in remembering the unique leaf
141*19c3b8c2SApple OSS Distributions  * that governs a subtree.
142*19c3b8c2SApple OSS Distributions  */
143*19c3b8c2SApple OSS Distributions 
144*19c3b8c2SApple OSS Distributions static struct radix_node *
rn_search(void * v_arg,struct radix_node * head)145*19c3b8c2SApple OSS Distributions rn_search(void *v_arg, struct radix_node *head)
146*19c3b8c2SApple OSS Distributions {
147*19c3b8c2SApple OSS Distributions 	struct radix_node *x;
148*19c3b8c2SApple OSS Distributions 	caddr_t v;
149*19c3b8c2SApple OSS Distributions 
150*19c3b8c2SApple OSS Distributions 	for (x = head, v = v_arg; x->rn_bit >= 0;) {
151*19c3b8c2SApple OSS Distributions 		if (x->rn_bmask & v[x->rn_offset]) {
152*19c3b8c2SApple OSS Distributions 			x = x->rn_right;
153*19c3b8c2SApple OSS Distributions 		} else {
154*19c3b8c2SApple OSS Distributions 			x = x->rn_left;
155*19c3b8c2SApple OSS Distributions 		}
156*19c3b8c2SApple OSS Distributions 	}
157*19c3b8c2SApple OSS Distributions 	return x;
158*19c3b8c2SApple OSS Distributions }
159*19c3b8c2SApple OSS Distributions 
160*19c3b8c2SApple OSS Distributions static struct radix_node *
rn_search_m(void * v_arg,struct radix_node * head,void * m_arg)161*19c3b8c2SApple OSS Distributions rn_search_m(void *v_arg, struct radix_node *head, void *m_arg)
162*19c3b8c2SApple OSS Distributions {
163*19c3b8c2SApple OSS Distributions 	struct radix_node *x;
164*19c3b8c2SApple OSS Distributions 	caddr_t v = v_arg, m = m_arg;
165*19c3b8c2SApple OSS Distributions 
166*19c3b8c2SApple OSS Distributions 	for (x = head; x->rn_bit >= 0;) {
167*19c3b8c2SApple OSS Distributions 		if ((x->rn_bmask & m[x->rn_offset]) &&
168*19c3b8c2SApple OSS Distributions 		    (x->rn_bmask & v[x->rn_offset])) {
169*19c3b8c2SApple OSS Distributions 			x = x->rn_right;
170*19c3b8c2SApple OSS Distributions 		} else {
171*19c3b8c2SApple OSS Distributions 			x = x->rn_left;
172*19c3b8c2SApple OSS Distributions 		}
173*19c3b8c2SApple OSS Distributions 	}
174*19c3b8c2SApple OSS Distributions 	return x;
175*19c3b8c2SApple OSS Distributions }
176*19c3b8c2SApple OSS Distributions 
177*19c3b8c2SApple OSS Distributions int
rn_refines(void * m_arg,void * n_arg)178*19c3b8c2SApple OSS Distributions rn_refines(void *m_arg, void *n_arg)
179*19c3b8c2SApple OSS Distributions {
180*19c3b8c2SApple OSS Distributions 	caddr_t m = m_arg, n = n_arg;
181*19c3b8c2SApple OSS Distributions 	caddr_t lim, lim2 = lim = n + *(u_char *)n;
182*19c3b8c2SApple OSS Distributions 	int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
183*19c3b8c2SApple OSS Distributions 	int masks_are_equal = 1;
184*19c3b8c2SApple OSS Distributions 
185*19c3b8c2SApple OSS Distributions 	if (longer > 0) {
186*19c3b8c2SApple OSS Distributions 		lim -= longer;
187*19c3b8c2SApple OSS Distributions 	}
188*19c3b8c2SApple OSS Distributions 	while (n < lim) {
189*19c3b8c2SApple OSS Distributions 		if (*n & ~(*m)) {
190*19c3b8c2SApple OSS Distributions 			return 0;
191*19c3b8c2SApple OSS Distributions 		}
192*19c3b8c2SApple OSS Distributions 		if (*n++ != *m++) {
193*19c3b8c2SApple OSS Distributions 			masks_are_equal = 0;
194*19c3b8c2SApple OSS Distributions 		}
195*19c3b8c2SApple OSS Distributions 	}
196*19c3b8c2SApple OSS Distributions 	while (n < lim2) {
197*19c3b8c2SApple OSS Distributions 		if (*n++) {
198*19c3b8c2SApple OSS Distributions 			return 0;
199*19c3b8c2SApple OSS Distributions 		}
200*19c3b8c2SApple OSS Distributions 	}
201*19c3b8c2SApple OSS Distributions 	if (masks_are_equal && (longer < 0)) {
202*19c3b8c2SApple OSS Distributions 		for (lim2 = m - longer; m < lim2;) {
203*19c3b8c2SApple OSS Distributions 			if (*m++) {
204*19c3b8c2SApple OSS Distributions 				return 1;
205*19c3b8c2SApple OSS Distributions 			}
206*19c3b8c2SApple OSS Distributions 		}
207*19c3b8c2SApple OSS Distributions 	}
208*19c3b8c2SApple OSS Distributions 	return !masks_are_equal;
209*19c3b8c2SApple OSS Distributions }
210*19c3b8c2SApple OSS Distributions 
211*19c3b8c2SApple OSS Distributions struct radix_node *
rn_lookup(void * v_arg,void * m_arg,struct radix_node_head * head)212*19c3b8c2SApple OSS Distributions rn_lookup(void *v_arg, void *m_arg, struct radix_node_head *head)
213*19c3b8c2SApple OSS Distributions {
214*19c3b8c2SApple OSS Distributions 	return rn_lookup_args(v_arg, m_arg, head, NULL, NULL);
215*19c3b8c2SApple OSS Distributions }
216*19c3b8c2SApple OSS Distributions 
217*19c3b8c2SApple OSS Distributions struct radix_node *
rn_lookup_args(void * v_arg,void * m_arg,struct radix_node_head * head,rn_matchf_t * f,void * w)218*19c3b8c2SApple OSS Distributions rn_lookup_args(void *v_arg, void *m_arg, struct radix_node_head *head,
219*19c3b8c2SApple OSS Distributions     rn_matchf_t *f, void *w)
220*19c3b8c2SApple OSS Distributions {
221*19c3b8c2SApple OSS Distributions 	struct radix_node *x;
222*19c3b8c2SApple OSS Distributions 	caddr_t netmask = NULL;
223*19c3b8c2SApple OSS Distributions 
224*19c3b8c2SApple OSS Distributions 	if (m_arg) {
225*19c3b8c2SApple OSS Distributions 		x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_offset);
226*19c3b8c2SApple OSS Distributions 		if (x == 0) {
227*19c3b8c2SApple OSS Distributions 			return NULL;
228*19c3b8c2SApple OSS Distributions 		}
229*19c3b8c2SApple OSS Distributions 		netmask = x->rn_key;
230*19c3b8c2SApple OSS Distributions 	}
231*19c3b8c2SApple OSS Distributions 	x = rn_match_args(v_arg, head, f, w);
232*19c3b8c2SApple OSS Distributions 	if (x && netmask) {
233*19c3b8c2SApple OSS Distributions 		while (x && x->rn_mask != netmask) {
234*19c3b8c2SApple OSS Distributions 			x = x->rn_dupedkey;
235*19c3b8c2SApple OSS Distributions 		}
236*19c3b8c2SApple OSS Distributions 	}
237*19c3b8c2SApple OSS Distributions 	return x;
238*19c3b8c2SApple OSS Distributions }
239*19c3b8c2SApple OSS Distributions 
240*19c3b8c2SApple OSS Distributions /*
241*19c3b8c2SApple OSS Distributions  * Returns true if address 'trial' has no bits differing from the
242*19c3b8c2SApple OSS Distributions  * leaf's key when compared under the leaf's mask.  In other words,
243*19c3b8c2SApple OSS Distributions  * returns true when 'trial' matches leaf.  If a leaf-matching
244*19c3b8c2SApple OSS Distributions  * routine is passed in, it is also used to find a match on the
245*19c3b8c2SApple OSS Distributions  * conditions defined by the caller of rn_match.
246*19c3b8c2SApple OSS Distributions  */
247*19c3b8c2SApple OSS Distributions static int
rn_satisfies_leaf(char * trial,struct radix_node * leaf,int skip,rn_matchf_t * f,void * w)248*19c3b8c2SApple OSS Distributions rn_satisfies_leaf(char *trial, struct radix_node *leaf, int skip,
249*19c3b8c2SApple OSS Distributions     rn_matchf_t *f, void *w)
250*19c3b8c2SApple OSS Distributions {
251*19c3b8c2SApple OSS Distributions 	char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
252*19c3b8c2SApple OSS Distributions 	char *cplim;
253*19c3b8c2SApple OSS Distributions 	int length = min(*(u_char *)cp, *(u_char *)cp2);
254*19c3b8c2SApple OSS Distributions 
255*19c3b8c2SApple OSS Distributions 	if (cp3 == 0) {
256*19c3b8c2SApple OSS Distributions 		cp3 = rn_ones;
257*19c3b8c2SApple OSS Distributions 	} else {
258*19c3b8c2SApple OSS Distributions 		length = min(length, *(u_char *)cp3);
259*19c3b8c2SApple OSS Distributions 	}
260*19c3b8c2SApple OSS Distributions 	cplim = cp + length; cp3 += skip; cp2 += skip;
261*19c3b8c2SApple OSS Distributions 	for (cp += skip; cp < cplim; cp++, cp2++, cp3++) {
262*19c3b8c2SApple OSS Distributions 		if ((*cp ^ *cp2) & *cp3) {
263*19c3b8c2SApple OSS Distributions 			return 0;
264*19c3b8c2SApple OSS Distributions 		}
265*19c3b8c2SApple OSS Distributions 	}
266*19c3b8c2SApple OSS Distributions 
267*19c3b8c2SApple OSS Distributions 	return RN_MATCHF(leaf, f, w);
268*19c3b8c2SApple OSS Distributions }
269*19c3b8c2SApple OSS Distributions 
270*19c3b8c2SApple OSS Distributions struct radix_node *
rn_match(void * v_arg,struct radix_node_head * head)271*19c3b8c2SApple OSS Distributions rn_match(void *v_arg, struct radix_node_head *head)
272*19c3b8c2SApple OSS Distributions {
273*19c3b8c2SApple OSS Distributions 	return rn_match_args(v_arg, head, NULL, NULL);
274*19c3b8c2SApple OSS Distributions }
275*19c3b8c2SApple OSS Distributions 
276*19c3b8c2SApple OSS Distributions struct radix_node *
rn_match_args(void * v_arg,struct radix_node_head * head,rn_matchf_t * f,void * w)277*19c3b8c2SApple OSS Distributions rn_match_args(void *v_arg, struct radix_node_head *head,
278*19c3b8c2SApple OSS Distributions     rn_matchf_t *f, void *w)
279*19c3b8c2SApple OSS Distributions {
280*19c3b8c2SApple OSS Distributions 	caddr_t v = v_arg;
281*19c3b8c2SApple OSS Distributions 	struct radix_node *t = head->rnh_treetop, *x;
282*19c3b8c2SApple OSS Distributions 	caddr_t cp = v, cp2;
283*19c3b8c2SApple OSS Distributions 	caddr_t cplim;
284*19c3b8c2SApple OSS Distributions 	struct radix_node *saved_t, *top = t;
285*19c3b8c2SApple OSS Distributions 	int off = t->rn_offset, vlen = *(u_char *)cp, matched_off;
286*19c3b8c2SApple OSS Distributions 	int test, b, rn_bit;
287*19c3b8c2SApple OSS Distributions 
288*19c3b8c2SApple OSS Distributions 	/*
289*19c3b8c2SApple OSS Distributions 	 * Open code rn_search(v, top) to avoid overhead of extra
290*19c3b8c2SApple OSS Distributions 	 * subroutine call.
291*19c3b8c2SApple OSS Distributions 	 */
292*19c3b8c2SApple OSS Distributions 	for (; t->rn_bit >= 0;) {
293*19c3b8c2SApple OSS Distributions 		if (t->rn_bmask & cp[t->rn_offset]) {
294*19c3b8c2SApple OSS Distributions 			t = t->rn_right;
295*19c3b8c2SApple OSS Distributions 		} else {
296*19c3b8c2SApple OSS Distributions 			t = t->rn_left;
297*19c3b8c2SApple OSS Distributions 		}
298*19c3b8c2SApple OSS Distributions 	}
299*19c3b8c2SApple OSS Distributions 	/*
300*19c3b8c2SApple OSS Distributions 	 * See if we match exactly as a host destination
301*19c3b8c2SApple OSS Distributions 	 * or at least learn how many bits match, for normal mask finesse.
302*19c3b8c2SApple OSS Distributions 	 *
303*19c3b8c2SApple OSS Distributions 	 * It doesn't hurt us to limit how many bytes to check
304*19c3b8c2SApple OSS Distributions 	 * to the length of the mask, since if it matches we had a genuine
305*19c3b8c2SApple OSS Distributions 	 * match and the leaf we have is the most specific one anyway;
306*19c3b8c2SApple OSS Distributions 	 * if it didn't match with a shorter length it would fail
307*19c3b8c2SApple OSS Distributions 	 * with a long one.  This wins big for class B&C netmasks which
308*19c3b8c2SApple OSS Distributions 	 * are probably the most common case...
309*19c3b8c2SApple OSS Distributions 	 */
310*19c3b8c2SApple OSS Distributions 	if (t->rn_mask) {
311*19c3b8c2SApple OSS Distributions 		vlen = *(u_char *)t->rn_mask;
312*19c3b8c2SApple OSS Distributions 	}
313*19c3b8c2SApple OSS Distributions 	cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
314*19c3b8c2SApple OSS Distributions 	for (; cp < cplim; cp++, cp2++) {
315*19c3b8c2SApple OSS Distributions 		if (*cp != *cp2) {
316*19c3b8c2SApple OSS Distributions 			goto on1;
317*19c3b8c2SApple OSS Distributions 		}
318*19c3b8c2SApple OSS Distributions 	}
319*19c3b8c2SApple OSS Distributions 	/*
320*19c3b8c2SApple OSS Distributions 	 * This extra grot is in case we are explicitly asked
321*19c3b8c2SApple OSS Distributions 	 * to look up the default.  Ugh!
322*19c3b8c2SApple OSS Distributions 	 *
323*19c3b8c2SApple OSS Distributions 	 * Never return the root node itself, it seems to cause a
324*19c3b8c2SApple OSS Distributions 	 * lot of confusion.
325*19c3b8c2SApple OSS Distributions 	 */
326*19c3b8c2SApple OSS Distributions 	if (t->rn_flags & RNF_ROOT) {
327*19c3b8c2SApple OSS Distributions 		t = t->rn_dupedkey;
328*19c3b8c2SApple OSS Distributions 	}
329*19c3b8c2SApple OSS Distributions 	if (t == NULL || RN_MATCHF(t, f, w)) {
330*19c3b8c2SApple OSS Distributions 		return t;
331*19c3b8c2SApple OSS Distributions 	} else {
332*19c3b8c2SApple OSS Distributions 		/*
333*19c3b8c2SApple OSS Distributions 		 * Although we found an exact match on the key,
334*19c3b8c2SApple OSS Distributions 		 * f() is looking for some other criteria as well.
335*19c3b8c2SApple OSS Distributions 		 * Continue looking as if the exact match failed.
336*19c3b8c2SApple OSS Distributions 		 */
337*19c3b8c2SApple OSS Distributions 		if (t->rn_parent->rn_flags & RNF_ROOT) {
338*19c3b8c2SApple OSS Distributions 			/* Hit the top; have to give up */
339*19c3b8c2SApple OSS Distributions 			return NULL;
340*19c3b8c2SApple OSS Distributions 		}
341*19c3b8c2SApple OSS Distributions 		b = 0;
342*19c3b8c2SApple OSS Distributions 		goto keeplooking;
343*19c3b8c2SApple OSS Distributions 	}
344*19c3b8c2SApple OSS Distributions on1:
345*19c3b8c2SApple OSS Distributions 	test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
346*19c3b8c2SApple OSS Distributions 	for (b = 7; (test >>= 1) > 0;) {
347*19c3b8c2SApple OSS Distributions 		b--;
348*19c3b8c2SApple OSS Distributions 	}
349*19c3b8c2SApple OSS Distributions keeplooking:
350*19c3b8c2SApple OSS Distributions 	matched_off = (int)(cp - v);
351*19c3b8c2SApple OSS Distributions 	b += matched_off << 3;
352*19c3b8c2SApple OSS Distributions 	rn_bit = -1 - b;
353*19c3b8c2SApple OSS Distributions 	/*
354*19c3b8c2SApple OSS Distributions 	 * If there is a host route in a duped-key chain, it will be first.
355*19c3b8c2SApple OSS Distributions 	 */
356*19c3b8c2SApple OSS Distributions 	if ((saved_t = t)->rn_mask == 0) {
357*19c3b8c2SApple OSS Distributions 		t = t->rn_dupedkey;
358*19c3b8c2SApple OSS Distributions 	}
359*19c3b8c2SApple OSS Distributions 	for (; t; t = t->rn_dupedkey) {
360*19c3b8c2SApple OSS Distributions 		/*
361*19c3b8c2SApple OSS Distributions 		 * Even if we don't match exactly as a host,
362*19c3b8c2SApple OSS Distributions 		 * we may match if the leaf we wound up at is
363*19c3b8c2SApple OSS Distributions 		 * a route to a net.
364*19c3b8c2SApple OSS Distributions 		 */
365*19c3b8c2SApple OSS Distributions 		if (t->rn_flags & RNF_NORMAL) {
366*19c3b8c2SApple OSS Distributions 			if ((rn_bit <= t->rn_bit) && RN_MATCHF(t, f, w)) {
367*19c3b8c2SApple OSS Distributions 				return t;
368*19c3b8c2SApple OSS Distributions 			}
369*19c3b8c2SApple OSS Distributions 		} else if (rn_satisfies_leaf(v, t, matched_off, f, w)) {
370*19c3b8c2SApple OSS Distributions 			return t;
371*19c3b8c2SApple OSS Distributions 		}
372*19c3b8c2SApple OSS Distributions 	}
373*19c3b8c2SApple OSS Distributions 	t = saved_t;
374*19c3b8c2SApple OSS Distributions 	/* start searching up the tree */
375*19c3b8c2SApple OSS Distributions 	do {
376*19c3b8c2SApple OSS Distributions 		struct radix_mask *m;
377*19c3b8c2SApple OSS Distributions 		t = t->rn_parent;
378*19c3b8c2SApple OSS Distributions 		m = t->rn_mklist;
379*19c3b8c2SApple OSS Distributions 		/*
380*19c3b8c2SApple OSS Distributions 		 * If non-contiguous masks ever become important
381*19c3b8c2SApple OSS Distributions 		 * we can restore the masking and open coding of
382*19c3b8c2SApple OSS Distributions 		 * the search and satisfaction test and put the
383*19c3b8c2SApple OSS Distributions 		 * calculation of "off" back before the "do".
384*19c3b8c2SApple OSS Distributions 		 */
385*19c3b8c2SApple OSS Distributions 		while (m) {
386*19c3b8c2SApple OSS Distributions 			if (m->rm_flags & RNF_NORMAL) {
387*19c3b8c2SApple OSS Distributions 				if ((rn_bit <= m->rm_bit) &&
388*19c3b8c2SApple OSS Distributions 				    RN_MATCHF(m->rm_leaf, f, w)) {
389*19c3b8c2SApple OSS Distributions 					return m->rm_leaf;
390*19c3b8c2SApple OSS Distributions 				}
391*19c3b8c2SApple OSS Distributions 			} else {
392*19c3b8c2SApple OSS Distributions 				off = min(t->rn_offset, matched_off);
393*19c3b8c2SApple OSS Distributions 				x = rn_search_m(v, t, m->rm_mask);
394*19c3b8c2SApple OSS Distributions 				while (x && x->rn_mask != m->rm_mask) {
395*19c3b8c2SApple OSS Distributions 					x = x->rn_dupedkey;
396*19c3b8c2SApple OSS Distributions 				}
397*19c3b8c2SApple OSS Distributions 				if (x && rn_satisfies_leaf(v, x, off, f, w)) {
398*19c3b8c2SApple OSS Distributions 					return x;
399*19c3b8c2SApple OSS Distributions 				}
400*19c3b8c2SApple OSS Distributions 			}
401*19c3b8c2SApple OSS Distributions 			m = m->rm_mklist;
402*19c3b8c2SApple OSS Distributions 		}
403*19c3b8c2SApple OSS Distributions 	} while (t != top);
404*19c3b8c2SApple OSS Distributions 	return NULL;
405*19c3b8c2SApple OSS Distributions }
406*19c3b8c2SApple OSS Distributions 
407*19c3b8c2SApple OSS Distributions #ifdef RN_DEBUG
408*19c3b8c2SApple OSS Distributions int     rn_nodenum;
409*19c3b8c2SApple OSS Distributions struct  radix_node *rn_clist;
410*19c3b8c2SApple OSS Distributions int     rn_saveinfo;
411*19c3b8c2SApple OSS Distributions int     rn_debug =  1;
412*19c3b8c2SApple OSS Distributions #endif
413*19c3b8c2SApple OSS Distributions 
414*19c3b8c2SApple OSS Distributions static struct radix_node *
rn_newpair(void * v,int b,struct radix_node nodes[2])415*19c3b8c2SApple OSS Distributions rn_newpair(void *v, int b, struct radix_node nodes[2])
416*19c3b8c2SApple OSS Distributions {
417*19c3b8c2SApple OSS Distributions 	struct radix_node *tt = nodes, *t = tt + 1;
418*19c3b8c2SApple OSS Distributions 	t->rn_bit = (short)b;
419*19c3b8c2SApple OSS Distributions 	t->rn_bmask = 0x80 >> (b & 7);
420*19c3b8c2SApple OSS Distributions 	t->rn_left = tt;
421*19c3b8c2SApple OSS Distributions 	t->rn_offset = b >> 3;
422*19c3b8c2SApple OSS Distributions 	tt->rn_bit = -1;
423*19c3b8c2SApple OSS Distributions 	tt->rn_key = (caddr_t)v;
424*19c3b8c2SApple OSS Distributions 	tt->rn_parent = t;
425*19c3b8c2SApple OSS Distributions 	tt->rn_flags = t->rn_flags = RNF_ACTIVE;
426*19c3b8c2SApple OSS Distributions 	tt->rn_mklist = t->rn_mklist = NULL;
427*19c3b8c2SApple OSS Distributions #ifdef RN_DEBUG
428*19c3b8c2SApple OSS Distributions 	tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
429*19c3b8c2SApple OSS Distributions 	tt->rn_twin = t;
430*19c3b8c2SApple OSS Distributions 	tt->rn_ybro = rn_clist;
431*19c3b8c2SApple OSS Distributions 	rn_clist = tt;
432*19c3b8c2SApple OSS Distributions #endif
433*19c3b8c2SApple OSS Distributions 	return t;
434*19c3b8c2SApple OSS Distributions }
435*19c3b8c2SApple OSS Distributions 
436*19c3b8c2SApple OSS Distributions static struct radix_node *
rn_insert(void * v_arg,struct radix_node_head * head,int * dupentry,struct radix_node nodes[2])437*19c3b8c2SApple OSS Distributions rn_insert(void *v_arg, struct radix_node_head *head, int *dupentry,
438*19c3b8c2SApple OSS Distributions     struct radix_node nodes[2])
439*19c3b8c2SApple OSS Distributions {
440*19c3b8c2SApple OSS Distributions 	caddr_t v = v_arg;
441*19c3b8c2SApple OSS Distributions 	struct radix_node *top = head->rnh_treetop;
442*19c3b8c2SApple OSS Distributions 	int head_off = top->rn_offset, vlen = (int)*((u_char *)v);
443*19c3b8c2SApple OSS Distributions 	struct radix_node *t = rn_search(v_arg, top);
444*19c3b8c2SApple OSS Distributions 	caddr_t cp = v + head_off;
445*19c3b8c2SApple OSS Distributions 	int b;
446*19c3b8c2SApple OSS Distributions 	struct radix_node *tt;
447*19c3b8c2SApple OSS Distributions 	/*
448*19c3b8c2SApple OSS Distributions 	 * Find first bit at which v and t->rn_key differ
449*19c3b8c2SApple OSS Distributions 	 */
450*19c3b8c2SApple OSS Distributions 	{
451*19c3b8c2SApple OSS Distributions 		caddr_t cp2 = t->rn_key + head_off;
452*19c3b8c2SApple OSS Distributions 		int cmp_res;
453*19c3b8c2SApple OSS Distributions 		caddr_t cplim = v + vlen;
454*19c3b8c2SApple OSS Distributions 
455*19c3b8c2SApple OSS Distributions 		while (cp < cplim) {
456*19c3b8c2SApple OSS Distributions 			if (*cp2++ != *cp++) {
457*19c3b8c2SApple OSS Distributions 				goto on1;
458*19c3b8c2SApple OSS Distributions 			}
459*19c3b8c2SApple OSS Distributions 		}
460*19c3b8c2SApple OSS Distributions 		*dupentry = 1;
461*19c3b8c2SApple OSS Distributions 		return t;
462*19c3b8c2SApple OSS Distributions on1:
463*19c3b8c2SApple OSS Distributions 		*dupentry = 0;
464*19c3b8c2SApple OSS Distributions 		cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
465*19c3b8c2SApple OSS Distributions 		for (b = (int)(cp - v) << 3; cmp_res; b--) {
466*19c3b8c2SApple OSS Distributions 			cmp_res >>= 1;
467*19c3b8c2SApple OSS Distributions 		}
468*19c3b8c2SApple OSS Distributions 	}
469*19c3b8c2SApple OSS Distributions 	{
470*19c3b8c2SApple OSS Distributions 		struct radix_node *p, *x = top;
471*19c3b8c2SApple OSS Distributions 		cp = v;
472*19c3b8c2SApple OSS Distributions 		do {
473*19c3b8c2SApple OSS Distributions 			p = x;
474*19c3b8c2SApple OSS Distributions 			if (cp[x->rn_offset] & x->rn_bmask) {
475*19c3b8c2SApple OSS Distributions 				x = x->rn_right;
476*19c3b8c2SApple OSS Distributions 			} else {
477*19c3b8c2SApple OSS Distributions 				x = x->rn_left;
478*19c3b8c2SApple OSS Distributions 			}
479*19c3b8c2SApple OSS Distributions 		} while (b > (unsigned) x->rn_bit);
480*19c3b8c2SApple OSS Distributions 		/* x->rn_bit < b && x->rn_bit >= 0 */
481*19c3b8c2SApple OSS Distributions #ifdef RN_DEBUG
482*19c3b8c2SApple OSS Distributions 		if (rn_debug) {
483*19c3b8c2SApple OSS Distributions 			log(LOG_DEBUG, "rn_insert: Going In:\n"), traverse(p);
484*19c3b8c2SApple OSS Distributions 		}
485*19c3b8c2SApple OSS Distributions #endif
486*19c3b8c2SApple OSS Distributions 		t = rn_newpair(v_arg, b, nodes);
487*19c3b8c2SApple OSS Distributions 		tt = t->rn_left;
488*19c3b8c2SApple OSS Distributions 		if ((cp[p->rn_offset] & p->rn_bmask) == 0) {
489*19c3b8c2SApple OSS Distributions 			p->rn_left = t;
490*19c3b8c2SApple OSS Distributions 		} else {
491*19c3b8c2SApple OSS Distributions 			p->rn_right = t;
492*19c3b8c2SApple OSS Distributions 		}
493*19c3b8c2SApple OSS Distributions 		x->rn_parent = t;
494*19c3b8c2SApple OSS Distributions 		t->rn_parent = p; /* frees x, p as temp vars below */
495*19c3b8c2SApple OSS Distributions 		if ((cp[t->rn_offset] & t->rn_bmask) == 0) {
496*19c3b8c2SApple OSS Distributions 			t->rn_right = x;
497*19c3b8c2SApple OSS Distributions 		} else {
498*19c3b8c2SApple OSS Distributions 			t->rn_right = tt;
499*19c3b8c2SApple OSS Distributions 			t->rn_left = x;
500*19c3b8c2SApple OSS Distributions 		}
501*19c3b8c2SApple OSS Distributions #ifdef RN_DEBUG
502*19c3b8c2SApple OSS Distributions 		if (rn_debug) {
503*19c3b8c2SApple OSS Distributions 			log(LOG_DEBUG, "rn_insert: Coming Out:\n"), traverse(p);
504*19c3b8c2SApple OSS Distributions 		}
505*19c3b8c2SApple OSS Distributions #endif
506*19c3b8c2SApple OSS Distributions 	}
507*19c3b8c2SApple OSS Distributions 	return tt;
508*19c3b8c2SApple OSS Distributions }
509*19c3b8c2SApple OSS Distributions 
510*19c3b8c2SApple OSS Distributions struct radix_node *
rn_addmask(void * n_arg,int search,int skip)511*19c3b8c2SApple OSS Distributions rn_addmask(void *n_arg, int search, int skip)
512*19c3b8c2SApple OSS Distributions {
513*19c3b8c2SApple OSS Distributions 	caddr_t netmask = (caddr_t)n_arg;
514*19c3b8c2SApple OSS Distributions 	struct radix_node *x;
515*19c3b8c2SApple OSS Distributions 	caddr_t cp, cplim;
516*19c3b8c2SApple OSS Distributions 	int b = 0, mlen, j;
517*19c3b8c2SApple OSS Distributions 	int maskduplicated, m0, isnormal;
518*19c3b8c2SApple OSS Distributions 	struct radix_node *saved_x;
519*19c3b8c2SApple OSS Distributions 	static int last_zeroed = 0;
520*19c3b8c2SApple OSS Distributions 
521*19c3b8c2SApple OSS Distributions 	if ((mlen = *(u_char *)netmask) > max_keylen) {
522*19c3b8c2SApple OSS Distributions 		mlen = max_keylen;
523*19c3b8c2SApple OSS Distributions 	}
524*19c3b8c2SApple OSS Distributions 	if (skip == 0) {
525*19c3b8c2SApple OSS Distributions 		skip = 1;
526*19c3b8c2SApple OSS Distributions 	}
527*19c3b8c2SApple OSS Distributions 	if (mlen <= skip) {
528*19c3b8c2SApple OSS Distributions 		return mask_rnhead->rnh_nodes;
529*19c3b8c2SApple OSS Distributions 	}
530*19c3b8c2SApple OSS Distributions 	if (skip > 1) {
531*19c3b8c2SApple OSS Distributions 		Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
532*19c3b8c2SApple OSS Distributions 	}
533*19c3b8c2SApple OSS Distributions 	if ((m0 = mlen) > skip) {
534*19c3b8c2SApple OSS Distributions 		Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
535*19c3b8c2SApple OSS Distributions 	}
536*19c3b8c2SApple OSS Distributions 	/*
537*19c3b8c2SApple OSS Distributions 	 * Trim trailing zeroes.
538*19c3b8c2SApple OSS Distributions 	 */
539*19c3b8c2SApple OSS Distributions 	for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;) {
540*19c3b8c2SApple OSS Distributions 		cp--;
541*19c3b8c2SApple OSS Distributions 	}
542*19c3b8c2SApple OSS Distributions 	mlen = (int)(cp - addmask_key);
543*19c3b8c2SApple OSS Distributions 	if (mlen <= skip) {
544*19c3b8c2SApple OSS Distributions 		if (m0 >= last_zeroed) {
545*19c3b8c2SApple OSS Distributions 			last_zeroed = mlen;
546*19c3b8c2SApple OSS Distributions 		}
547*19c3b8c2SApple OSS Distributions 		return mask_rnhead->rnh_nodes;
548*19c3b8c2SApple OSS Distributions 	}
549*19c3b8c2SApple OSS Distributions 	if (m0 < last_zeroed) {
550*19c3b8c2SApple OSS Distributions 		Bzero(addmask_key + m0, last_zeroed - m0);
551*19c3b8c2SApple OSS Distributions 	}
552*19c3b8c2SApple OSS Distributions 	*addmask_key = last_zeroed = (char)mlen;
553*19c3b8c2SApple OSS Distributions 	x = rn_search(addmask_key, rn_masktop);
554*19c3b8c2SApple OSS Distributions 	if (Bcmp(addmask_key, x->rn_key, mlen) != 0) {
555*19c3b8c2SApple OSS Distributions 		x = NULL;
556*19c3b8c2SApple OSS Distributions 	}
557*19c3b8c2SApple OSS Distributions 	if (x || search) {
558*19c3b8c2SApple OSS Distributions 		return x;
559*19c3b8c2SApple OSS Distributions 	}
560*19c3b8c2SApple OSS Distributions 	x = saved_x = zalloc_flags(radix_node_zone, Z_WAITOK_ZERO_NOFAIL);
561*19c3b8c2SApple OSS Distributions 	netmask = cp = (caddr_t)(x + 2);
562*19c3b8c2SApple OSS Distributions 	Bcopy(addmask_key, cp, mlen);
563*19c3b8c2SApple OSS Distributions 	x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
564*19c3b8c2SApple OSS Distributions 	if (maskduplicated) {
565*19c3b8c2SApple OSS Distributions 		log(LOG_ERR, "rn_addmask: mask impossibly already in tree");
566*19c3b8c2SApple OSS Distributions 		zfree(radix_node_zone, saved_x);
567*19c3b8c2SApple OSS Distributions 		return x;
568*19c3b8c2SApple OSS Distributions 	}
569*19c3b8c2SApple OSS Distributions 	mask_rnhead->rnh_cnt++;
570*19c3b8c2SApple OSS Distributions 	/*
571*19c3b8c2SApple OSS Distributions 	 * Calculate index of mask, and check for normalcy.
572*19c3b8c2SApple OSS Distributions 	 */
573*19c3b8c2SApple OSS Distributions 	cplim = netmask + mlen; isnormal = 1;
574*19c3b8c2SApple OSS Distributions 	for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;) {
575*19c3b8c2SApple OSS Distributions 		cp++;
576*19c3b8c2SApple OSS Distributions 	}
577*19c3b8c2SApple OSS Distributions 	if (cp != cplim) {
578*19c3b8c2SApple OSS Distributions 		for (j = 0x80; (j & *cp) != 0; j >>= 1) {
579*19c3b8c2SApple OSS Distributions 			b++;
580*19c3b8c2SApple OSS Distributions 		}
581*19c3b8c2SApple OSS Distributions 		if (*cp != normal_chars[b] || cp != (cplim - 1)) {
582*19c3b8c2SApple OSS Distributions 			isnormal = 0;
583*19c3b8c2SApple OSS Distributions 		}
584*19c3b8c2SApple OSS Distributions 	}
585*19c3b8c2SApple OSS Distributions 	b += (cp - netmask) << 3;
586*19c3b8c2SApple OSS Distributions 	x->rn_bit = (short)(-1 - b);
587*19c3b8c2SApple OSS Distributions 	if (isnormal) {
588*19c3b8c2SApple OSS Distributions 		x->rn_flags |= RNF_NORMAL;
589*19c3b8c2SApple OSS Distributions 	}
590*19c3b8c2SApple OSS Distributions 	return x;
591*19c3b8c2SApple OSS Distributions }
592*19c3b8c2SApple OSS Distributions 
593*19c3b8c2SApple OSS Distributions static int
594*19c3b8c2SApple OSS Distributions /* XXX: arbitrary ordering for non-contiguous masks */
rn_lexobetter(void * m_arg,void * n_arg)595*19c3b8c2SApple OSS Distributions rn_lexobetter(void *m_arg, void *n_arg)
596*19c3b8c2SApple OSS Distributions {
597*19c3b8c2SApple OSS Distributions 	u_char *mp = m_arg, *np = n_arg, *lim;
598*19c3b8c2SApple OSS Distributions 
599*19c3b8c2SApple OSS Distributions 	if (*mp > *np) {
600*19c3b8c2SApple OSS Distributions 		return 1;  /* not really, but need to check longer one first */
601*19c3b8c2SApple OSS Distributions 	}
602*19c3b8c2SApple OSS Distributions 	if (*mp == *np) {
603*19c3b8c2SApple OSS Distributions 		for (lim = mp + *mp; mp < lim;) {
604*19c3b8c2SApple OSS Distributions 			if (*mp++ > *np++) {
605*19c3b8c2SApple OSS Distributions 				return 1;
606*19c3b8c2SApple OSS Distributions 			}
607*19c3b8c2SApple OSS Distributions 		}
608*19c3b8c2SApple OSS Distributions 	}
609*19c3b8c2SApple OSS Distributions 	return 0;
610*19c3b8c2SApple OSS Distributions }
611*19c3b8c2SApple OSS Distributions 
612*19c3b8c2SApple OSS Distributions static struct radix_mask *
rn_new_radix_mask(struct radix_node * tt,struct radix_mask * next)613*19c3b8c2SApple OSS Distributions rn_new_radix_mask(struct radix_node *tt, struct radix_mask *next)
614*19c3b8c2SApple OSS Distributions {
615*19c3b8c2SApple OSS Distributions 	struct radix_mask *m;
616*19c3b8c2SApple OSS Distributions 
617*19c3b8c2SApple OSS Distributions 	MKGet(m);
618*19c3b8c2SApple OSS Distributions 	m->rm_bit = tt->rn_bit;
619*19c3b8c2SApple OSS Distributions 	m->rm_flags = tt->rn_flags;
620*19c3b8c2SApple OSS Distributions 	if (tt->rn_flags & RNF_NORMAL) {
621*19c3b8c2SApple OSS Distributions 		m->rm_leaf = tt;
622*19c3b8c2SApple OSS Distributions 	} else {
623*19c3b8c2SApple OSS Distributions 		m->rm_mask = tt->rn_mask;
624*19c3b8c2SApple OSS Distributions 	}
625*19c3b8c2SApple OSS Distributions 	m->rm_mklist = next;
626*19c3b8c2SApple OSS Distributions 	tt->rn_mklist = m;
627*19c3b8c2SApple OSS Distributions 	return m;
628*19c3b8c2SApple OSS Distributions }
629*19c3b8c2SApple OSS Distributions 
630*19c3b8c2SApple OSS Distributions struct radix_node *
rn_addroute(void * v_arg,void * n_arg,struct radix_node_head * head,struct radix_node treenodes[2])631*19c3b8c2SApple OSS Distributions rn_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
632*19c3b8c2SApple OSS Distributions     struct radix_node treenodes[2])
633*19c3b8c2SApple OSS Distributions {
634*19c3b8c2SApple OSS Distributions 	caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
635*19c3b8c2SApple OSS Distributions 	struct radix_node *t, *x = NULL, *tt;
636*19c3b8c2SApple OSS Distributions 	struct radix_node *saved_tt, *top = head->rnh_treetop;
637*19c3b8c2SApple OSS Distributions 	short b = 0, b_leaf = 0;
638*19c3b8c2SApple OSS Distributions 	int keyduplicated;
639*19c3b8c2SApple OSS Distributions 	caddr_t mmask;
640*19c3b8c2SApple OSS Distributions 	struct radix_mask *m, **mp;
641*19c3b8c2SApple OSS Distributions 
642*19c3b8c2SApple OSS Distributions 	/*
643*19c3b8c2SApple OSS Distributions 	 * In dealing with non-contiguous masks, there may be
644*19c3b8c2SApple OSS Distributions 	 * many different routes which have the same mask.
645*19c3b8c2SApple OSS Distributions 	 * We will find it useful to have a unique pointer to
646*19c3b8c2SApple OSS Distributions 	 * the mask to speed avoiding duplicate references at
647*19c3b8c2SApple OSS Distributions 	 * nodes and possibly save time in calculating indices.
648*19c3b8c2SApple OSS Distributions 	 */
649*19c3b8c2SApple OSS Distributions 	if (netmask) {
650*19c3b8c2SApple OSS Distributions 		if ((x = rn_addmask(netmask, 0, top->rn_offset)) == 0) {
651*19c3b8c2SApple OSS Distributions 			return NULL;
652*19c3b8c2SApple OSS Distributions 		}
653*19c3b8c2SApple OSS Distributions 		b_leaf = x->rn_bit;
654*19c3b8c2SApple OSS Distributions 		b = -1 - x->rn_bit;
655*19c3b8c2SApple OSS Distributions 		netmask = x->rn_key;
656*19c3b8c2SApple OSS Distributions 	}
657*19c3b8c2SApple OSS Distributions 	/*
658*19c3b8c2SApple OSS Distributions 	 * Deal with duplicated keys: attach node to previous instance
659*19c3b8c2SApple OSS Distributions 	 */
660*19c3b8c2SApple OSS Distributions 	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
661*19c3b8c2SApple OSS Distributions 	if (keyduplicated) {
662*19c3b8c2SApple OSS Distributions 		for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
663*19c3b8c2SApple OSS Distributions 			if (tt->rn_mask == netmask) {
664*19c3b8c2SApple OSS Distributions 				return NULL;
665*19c3b8c2SApple OSS Distributions 			}
666*19c3b8c2SApple OSS Distributions 			if (netmask == 0 ||
667*19c3b8c2SApple OSS Distributions 			    (tt->rn_mask &&
668*19c3b8c2SApple OSS Distributions 			    ((b_leaf < tt->rn_bit)  /* index(netmask) > node */
669*19c3b8c2SApple OSS Distributions 			    || rn_refines(netmask, tt->rn_mask)
670*19c3b8c2SApple OSS Distributions 			    || rn_lexobetter(netmask, tt->rn_mask)))) {
671*19c3b8c2SApple OSS Distributions 				break;
672*19c3b8c2SApple OSS Distributions 			}
673*19c3b8c2SApple OSS Distributions 		}
674*19c3b8c2SApple OSS Distributions 		/*
675*19c3b8c2SApple OSS Distributions 		 * If the mask is not duplicated, we wouldn't
676*19c3b8c2SApple OSS Distributions 		 * find it among possible duplicate key entries
677*19c3b8c2SApple OSS Distributions 		 * anyway, so the above test doesn't hurt.
678*19c3b8c2SApple OSS Distributions 		 *
679*19c3b8c2SApple OSS Distributions 		 * We sort the masks for a duplicated key the same way as
680*19c3b8c2SApple OSS Distributions 		 * in a masklist -- most specific to least specific.
681*19c3b8c2SApple OSS Distributions 		 * This may require the unfortunate nuisance of relocating
682*19c3b8c2SApple OSS Distributions 		 * the head of the list.
683*19c3b8c2SApple OSS Distributions 		 */
684*19c3b8c2SApple OSS Distributions 		if (tt == saved_tt) {
685*19c3b8c2SApple OSS Distributions 			struct  radix_node *xx = x;
686*19c3b8c2SApple OSS Distributions 			/* link in at head of list */
687*19c3b8c2SApple OSS Distributions 			(tt = treenodes)->rn_dupedkey = t;
688*19c3b8c2SApple OSS Distributions 			tt->rn_flags = t->rn_flags;
689*19c3b8c2SApple OSS Distributions 			tt->rn_parent = x = t->rn_parent;
690*19c3b8c2SApple OSS Distributions 			t->rn_parent = tt;                      /* parent */
691*19c3b8c2SApple OSS Distributions 			if (x->rn_left == t) {
692*19c3b8c2SApple OSS Distributions 				x->rn_left = tt;
693*19c3b8c2SApple OSS Distributions 			} else {
694*19c3b8c2SApple OSS Distributions 				x->rn_right = tt;
695*19c3b8c2SApple OSS Distributions 			}
696*19c3b8c2SApple OSS Distributions 			saved_tt = tt; x = xx;
697*19c3b8c2SApple OSS Distributions 		} else {
698*19c3b8c2SApple OSS Distributions 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
699*19c3b8c2SApple OSS Distributions 			t->rn_dupedkey = tt;
700*19c3b8c2SApple OSS Distributions 			tt->rn_parent = t;                      /* parent */
701*19c3b8c2SApple OSS Distributions 			if (tt->rn_dupedkey) {                  /* parent */
702*19c3b8c2SApple OSS Distributions 				tt->rn_dupedkey->rn_parent = tt; /* parent */
703*19c3b8c2SApple OSS Distributions 			}
704*19c3b8c2SApple OSS Distributions 		}
705*19c3b8c2SApple OSS Distributions #ifdef RN_DEBUG
706*19c3b8c2SApple OSS Distributions 		t = tt + 1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
707*19c3b8c2SApple OSS Distributions 		tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
708*19c3b8c2SApple OSS Distributions #endif
709*19c3b8c2SApple OSS Distributions 		tt->rn_key = (caddr_t) v;
710*19c3b8c2SApple OSS Distributions 		tt->rn_bit = -1;
711*19c3b8c2SApple OSS Distributions 		tt->rn_flags = RNF_ACTIVE;
712*19c3b8c2SApple OSS Distributions 	}
713*19c3b8c2SApple OSS Distributions 	head->rnh_cnt++;
714*19c3b8c2SApple OSS Distributions 	/*
715*19c3b8c2SApple OSS Distributions 	 * Put mask in tree.
716*19c3b8c2SApple OSS Distributions 	 */
717*19c3b8c2SApple OSS Distributions 	if (netmask) {
718*19c3b8c2SApple OSS Distributions 		tt->rn_mask = netmask;
719*19c3b8c2SApple OSS Distributions 		tt->rn_bit = x->rn_bit;
720*19c3b8c2SApple OSS Distributions 		tt->rn_flags |= x->rn_flags & RNF_NORMAL;
721*19c3b8c2SApple OSS Distributions 	}
722*19c3b8c2SApple OSS Distributions 	t = saved_tt->rn_parent;
723*19c3b8c2SApple OSS Distributions 	if (keyduplicated) {
724*19c3b8c2SApple OSS Distributions 		goto on2;
725*19c3b8c2SApple OSS Distributions 	}
726*19c3b8c2SApple OSS Distributions 	b_leaf = -1 - t->rn_bit;
727*19c3b8c2SApple OSS Distributions 	if (t->rn_right == saved_tt) {
728*19c3b8c2SApple OSS Distributions 		x = t->rn_left;
729*19c3b8c2SApple OSS Distributions 	} else {
730*19c3b8c2SApple OSS Distributions 		x = t->rn_right;
731*19c3b8c2SApple OSS Distributions 	}
732*19c3b8c2SApple OSS Distributions 	/* Promote general routes from below */
733*19c3b8c2SApple OSS Distributions 	if (x->rn_bit < 0) {
734*19c3b8c2SApple OSS Distributions 		for (mp = &t->rn_mklist; x; x = x->rn_dupedkey) {
735*19c3b8c2SApple OSS Distributions 			if (x->rn_mask && (x->rn_bit >= b_leaf) && x->rn_mklist == 0) {
736*19c3b8c2SApple OSS Distributions 				*mp = m = rn_new_radix_mask(x, NULL);
737*19c3b8c2SApple OSS Distributions 				if (m) {
738*19c3b8c2SApple OSS Distributions 					mp = &m->rm_mklist;
739*19c3b8c2SApple OSS Distributions 				}
740*19c3b8c2SApple OSS Distributions 			}
741*19c3b8c2SApple OSS Distributions 		}
742*19c3b8c2SApple OSS Distributions 	} else if (x->rn_mklist) {
743*19c3b8c2SApple OSS Distributions 		/*
744*19c3b8c2SApple OSS Distributions 		 * Skip over masks whose index is > that of new node
745*19c3b8c2SApple OSS Distributions 		 */
746*19c3b8c2SApple OSS Distributions 		for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
747*19c3b8c2SApple OSS Distributions 			if (m->rm_bit >= b_leaf) {
748*19c3b8c2SApple OSS Distributions 				break;
749*19c3b8c2SApple OSS Distributions 			}
750*19c3b8c2SApple OSS Distributions 		}
751*19c3b8c2SApple OSS Distributions 		t->rn_mklist = m; *mp = NULL;
752*19c3b8c2SApple OSS Distributions 	}
753*19c3b8c2SApple OSS Distributions on2:
754*19c3b8c2SApple OSS Distributions 	/* Add new route to highest possible ancestor's list */
755*19c3b8c2SApple OSS Distributions 	if ((netmask == 0) || (b > t->rn_bit)) {
756*19c3b8c2SApple OSS Distributions 		return tt; /* can't lift at all */
757*19c3b8c2SApple OSS Distributions 	}
758*19c3b8c2SApple OSS Distributions 	b_leaf = tt->rn_bit;
759*19c3b8c2SApple OSS Distributions 	do {
760*19c3b8c2SApple OSS Distributions 		x = t;
761*19c3b8c2SApple OSS Distributions 		t = t->rn_parent;
762*19c3b8c2SApple OSS Distributions 	} while (b <= t->rn_bit && x != top);
763*19c3b8c2SApple OSS Distributions 	/*
764*19c3b8c2SApple OSS Distributions 	 * Search through routes associated with node to
765*19c3b8c2SApple OSS Distributions 	 * insert new route according to index.
766*19c3b8c2SApple OSS Distributions 	 * Need same criteria as when sorting dupedkeys to avoid
767*19c3b8c2SApple OSS Distributions 	 * double loop on deletion.
768*19c3b8c2SApple OSS Distributions 	 */
769*19c3b8c2SApple OSS Distributions 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
770*19c3b8c2SApple OSS Distributions 		if (m->rm_bit < b_leaf) {
771*19c3b8c2SApple OSS Distributions 			continue;
772*19c3b8c2SApple OSS Distributions 		}
773*19c3b8c2SApple OSS Distributions 		if (m->rm_bit > b_leaf) {
774*19c3b8c2SApple OSS Distributions 			break;
775*19c3b8c2SApple OSS Distributions 		}
776*19c3b8c2SApple OSS Distributions 		if (m->rm_flags & RNF_NORMAL) {
777*19c3b8c2SApple OSS Distributions 			mmask = m->rm_leaf->rn_mask;
778*19c3b8c2SApple OSS Distributions 			if (tt->rn_flags & RNF_NORMAL) {
779*19c3b8c2SApple OSS Distributions 				log(LOG_ERR,
780*19c3b8c2SApple OSS Distributions 				    "Non-unique normal route, mask not entered");
781*19c3b8c2SApple OSS Distributions 				return tt;
782*19c3b8c2SApple OSS Distributions 			}
783*19c3b8c2SApple OSS Distributions 		} else {
784*19c3b8c2SApple OSS Distributions 			mmask = m->rm_mask;
785*19c3b8c2SApple OSS Distributions 		}
786*19c3b8c2SApple OSS Distributions 		if (mmask == netmask) {
787*19c3b8c2SApple OSS Distributions 			m->rm_refs++;
788*19c3b8c2SApple OSS Distributions 			tt->rn_mklist = m;
789*19c3b8c2SApple OSS Distributions 			return tt;
790*19c3b8c2SApple OSS Distributions 		}
791*19c3b8c2SApple OSS Distributions 		if (rn_refines(netmask, mmask)
792*19c3b8c2SApple OSS Distributions 		    || rn_lexobetter(netmask, mmask)) {
793*19c3b8c2SApple OSS Distributions 			break;
794*19c3b8c2SApple OSS Distributions 		}
795*19c3b8c2SApple OSS Distributions 	}
796*19c3b8c2SApple OSS Distributions 	*mp = rn_new_radix_mask(tt, *mp);
797*19c3b8c2SApple OSS Distributions 	return tt;
798*19c3b8c2SApple OSS Distributions }
799*19c3b8c2SApple OSS Distributions 
800*19c3b8c2SApple OSS Distributions struct radix_node *
rn_delete(void * v_arg,void * netmask_arg,struct radix_node_head * head)801*19c3b8c2SApple OSS Distributions rn_delete(void *v_arg, void *netmask_arg, struct radix_node_head *head)
802*19c3b8c2SApple OSS Distributions {
803*19c3b8c2SApple OSS Distributions 	struct radix_node *t, *p, *x, *tt;
804*19c3b8c2SApple OSS Distributions 	struct radix_mask *m, *saved_m, **mp;
805*19c3b8c2SApple OSS Distributions 	struct radix_node *dupedkey, *saved_tt, *top;
806*19c3b8c2SApple OSS Distributions 	caddr_t v, netmask;
807*19c3b8c2SApple OSS Distributions 	int b, head_off, vlen;
808*19c3b8c2SApple OSS Distributions 
809*19c3b8c2SApple OSS Distributions 	v = v_arg;
810*19c3b8c2SApple OSS Distributions 	netmask = netmask_arg;
811*19c3b8c2SApple OSS Distributions 	x = head->rnh_treetop;
812*19c3b8c2SApple OSS Distributions 	tt = rn_search(v, x);
813*19c3b8c2SApple OSS Distributions 	head_off = x->rn_offset;
814*19c3b8c2SApple OSS Distributions 	vlen =  *(u_char *)v;
815*19c3b8c2SApple OSS Distributions 	saved_tt = tt;
816*19c3b8c2SApple OSS Distributions 	top = x;
817*19c3b8c2SApple OSS Distributions 	if (tt == 0 ||
818*19c3b8c2SApple OSS Distributions 	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off)) {
819*19c3b8c2SApple OSS Distributions 		return NULL;
820*19c3b8c2SApple OSS Distributions 	}
821*19c3b8c2SApple OSS Distributions 	/*
822*19c3b8c2SApple OSS Distributions 	 * Delete our route from mask lists.
823*19c3b8c2SApple OSS Distributions 	 */
824*19c3b8c2SApple OSS Distributions 	if (netmask) {
825*19c3b8c2SApple OSS Distributions 		if ((x = rn_addmask(netmask, 1, head_off)) == 0) {
826*19c3b8c2SApple OSS Distributions 			return NULL;
827*19c3b8c2SApple OSS Distributions 		}
828*19c3b8c2SApple OSS Distributions 		netmask = x->rn_key;
829*19c3b8c2SApple OSS Distributions 		while (tt->rn_mask != netmask) {
830*19c3b8c2SApple OSS Distributions 			if ((tt = tt->rn_dupedkey) == 0) {
831*19c3b8c2SApple OSS Distributions 				return NULL;
832*19c3b8c2SApple OSS Distributions 			}
833*19c3b8c2SApple OSS Distributions 		}
834*19c3b8c2SApple OSS Distributions 	}
835*19c3b8c2SApple OSS Distributions 	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0) {
836*19c3b8c2SApple OSS Distributions 		goto on1;
837*19c3b8c2SApple OSS Distributions 	}
838*19c3b8c2SApple OSS Distributions 	if (tt->rn_flags & RNF_NORMAL) {
839*19c3b8c2SApple OSS Distributions 		if (m->rm_leaf != tt || m->rm_refs > 0) {
840*19c3b8c2SApple OSS Distributions 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
841*19c3b8c2SApple OSS Distributions 			return NULL;  /* dangling ref could cause disaster */
842*19c3b8c2SApple OSS Distributions 		}
843*19c3b8c2SApple OSS Distributions 	} else {
844*19c3b8c2SApple OSS Distributions 		if (m->rm_mask != tt->rn_mask) {
845*19c3b8c2SApple OSS Distributions 			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
846*19c3b8c2SApple OSS Distributions 			goto on1;
847*19c3b8c2SApple OSS Distributions 		}
848*19c3b8c2SApple OSS Distributions 		if (--m->rm_refs >= 0) {
849*19c3b8c2SApple OSS Distributions 			goto on1;
850*19c3b8c2SApple OSS Distributions 		}
851*19c3b8c2SApple OSS Distributions 	}
852*19c3b8c2SApple OSS Distributions 	b = -1 - tt->rn_bit;
853*19c3b8c2SApple OSS Distributions 	t = saved_tt->rn_parent;
854*19c3b8c2SApple OSS Distributions 	if (b > t->rn_bit) {
855*19c3b8c2SApple OSS Distributions 		goto on1; /* Wasn't lifted at all */
856*19c3b8c2SApple OSS Distributions 	}
857*19c3b8c2SApple OSS Distributions 	do {
858*19c3b8c2SApple OSS Distributions 		x = t;
859*19c3b8c2SApple OSS Distributions 		t = t->rn_parent;
860*19c3b8c2SApple OSS Distributions 	} while (b <= t->rn_bit && x != top);
861*19c3b8c2SApple OSS Distributions 	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
862*19c3b8c2SApple OSS Distributions 		if (m == saved_m) {
863*19c3b8c2SApple OSS Distributions 			*mp = m->rm_mklist;
864*19c3b8c2SApple OSS Distributions 			if (tt->rn_mklist == m) {
865*19c3b8c2SApple OSS Distributions 				tt->rn_mklist = *mp;
866*19c3b8c2SApple OSS Distributions 			}
867*19c3b8c2SApple OSS Distributions 			MKFree(m);
868*19c3b8c2SApple OSS Distributions 			break;
869*19c3b8c2SApple OSS Distributions 		}
870*19c3b8c2SApple OSS Distributions 	}
871*19c3b8c2SApple OSS Distributions 	if (m == 0) {
872*19c3b8c2SApple OSS Distributions 		log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
873*19c3b8c2SApple OSS Distributions 		if (tt->rn_flags & RNF_NORMAL) {
874*19c3b8c2SApple OSS Distributions 			return NULL; /* Dangling ref to us */
875*19c3b8c2SApple OSS Distributions 		}
876*19c3b8c2SApple OSS Distributions 	}
877*19c3b8c2SApple OSS Distributions on1:
878*19c3b8c2SApple OSS Distributions 	/*
879*19c3b8c2SApple OSS Distributions 	 * Eliminate us from tree
880*19c3b8c2SApple OSS Distributions 	 */
881*19c3b8c2SApple OSS Distributions 	if (tt->rn_flags & RNF_ROOT) {
882*19c3b8c2SApple OSS Distributions 		return NULL;
883*19c3b8c2SApple OSS Distributions 	}
884*19c3b8c2SApple OSS Distributions 	head->rnh_cnt--;
885*19c3b8c2SApple OSS Distributions #ifdef RN_DEBUG
886*19c3b8c2SApple OSS Distributions 	/* Get us out of the creation list */
887*19c3b8c2SApple OSS Distributions 	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {
888*19c3b8c2SApple OSS Distributions 	}
889*19c3b8c2SApple OSS Distributions 	if (t) {
890*19c3b8c2SApple OSS Distributions 		t->rn_ybro = tt->rn_ybro;
891*19c3b8c2SApple OSS Distributions 	}
892*19c3b8c2SApple OSS Distributions #endif
893*19c3b8c2SApple OSS Distributions 	t = tt->rn_parent;
894*19c3b8c2SApple OSS Distributions 	dupedkey = saved_tt->rn_dupedkey;
895*19c3b8c2SApple OSS Distributions 	if (dupedkey) {
896*19c3b8c2SApple OSS Distributions 		/*
897*19c3b8c2SApple OSS Distributions 		 * at this point, tt is the deletion target and saved_tt
898*19c3b8c2SApple OSS Distributions 		 * is the head of the dupekey chain
899*19c3b8c2SApple OSS Distributions 		 */
900*19c3b8c2SApple OSS Distributions 		if (tt == saved_tt) {
901*19c3b8c2SApple OSS Distributions 			/* remove from head of chain */
902*19c3b8c2SApple OSS Distributions 			x = dupedkey; x->rn_parent = t;
903*19c3b8c2SApple OSS Distributions 			if (t->rn_left == tt) {
904*19c3b8c2SApple OSS Distributions 				t->rn_left = x;
905*19c3b8c2SApple OSS Distributions 			} else {
906*19c3b8c2SApple OSS Distributions 				t->rn_right = x;
907*19c3b8c2SApple OSS Distributions 			}
908*19c3b8c2SApple OSS Distributions 		} else {
909*19c3b8c2SApple OSS Distributions 			/* find node in front of tt on the chain */
910*19c3b8c2SApple OSS Distributions 			for (x = p = saved_tt; p && p->rn_dupedkey != tt;) {
911*19c3b8c2SApple OSS Distributions 				p = p->rn_dupedkey;
912*19c3b8c2SApple OSS Distributions 			}
913*19c3b8c2SApple OSS Distributions 			if (p) {
914*19c3b8c2SApple OSS Distributions 				p->rn_dupedkey = tt->rn_dupedkey;
915*19c3b8c2SApple OSS Distributions 				if (tt->rn_dupedkey) {          /* parent */
916*19c3b8c2SApple OSS Distributions 					tt->rn_dupedkey->rn_parent = p;
917*19c3b8c2SApple OSS Distributions 				}
918*19c3b8c2SApple OSS Distributions 				/* parent */
919*19c3b8c2SApple OSS Distributions 			} else {
920*19c3b8c2SApple OSS Distributions 				log(LOG_ERR, "rn_delete: couldn't find us\n");
921*19c3b8c2SApple OSS Distributions 			}
922*19c3b8c2SApple OSS Distributions 		}
923*19c3b8c2SApple OSS Distributions 		t = tt + 1;
924*19c3b8c2SApple OSS Distributions 		if (t->rn_flags & RNF_ACTIVE) {
925*19c3b8c2SApple OSS Distributions #ifndef RN_DEBUG
926*19c3b8c2SApple OSS Distributions 			*++x = *t;
927*19c3b8c2SApple OSS Distributions 			p = t->rn_parent;
928*19c3b8c2SApple OSS Distributions #else
929*19c3b8c2SApple OSS Distributions 			b = t->rn_info;
930*19c3b8c2SApple OSS Distributions 			*++x = *t;
931*19c3b8c2SApple OSS Distributions 			t->rn_info = b;
932*19c3b8c2SApple OSS Distributions 			p = t->rn_parent;
933*19c3b8c2SApple OSS Distributions #endif
934*19c3b8c2SApple OSS Distributions 			if (p->rn_left == t) {
935*19c3b8c2SApple OSS Distributions 				p->rn_left = x;
936*19c3b8c2SApple OSS Distributions 			} else {
937*19c3b8c2SApple OSS Distributions 				p->rn_right = x;
938*19c3b8c2SApple OSS Distributions 			}
939*19c3b8c2SApple OSS Distributions 			x->rn_left->rn_parent = x;
940*19c3b8c2SApple OSS Distributions 			x->rn_right->rn_parent = x;
941*19c3b8c2SApple OSS Distributions 		}
942*19c3b8c2SApple OSS Distributions 		goto out;
943*19c3b8c2SApple OSS Distributions 	}
944*19c3b8c2SApple OSS Distributions 	if (t->rn_left == tt) {
945*19c3b8c2SApple OSS Distributions 		x = t->rn_right;
946*19c3b8c2SApple OSS Distributions 	} else {
947*19c3b8c2SApple OSS Distributions 		x = t->rn_left;
948*19c3b8c2SApple OSS Distributions 	}
949*19c3b8c2SApple OSS Distributions 	p = t->rn_parent;
950*19c3b8c2SApple OSS Distributions 	if (p->rn_right == t) {
951*19c3b8c2SApple OSS Distributions 		p->rn_right = x;
952*19c3b8c2SApple OSS Distributions 	} else {
953*19c3b8c2SApple OSS Distributions 		p->rn_left = x;
954*19c3b8c2SApple OSS Distributions 	}
955*19c3b8c2SApple OSS Distributions 	x->rn_parent = p;
956*19c3b8c2SApple OSS Distributions 	/*
957*19c3b8c2SApple OSS Distributions 	 * Demote routes attached to us.
958*19c3b8c2SApple OSS Distributions 	 */
959*19c3b8c2SApple OSS Distributions 	if (t->rn_mklist) {
960*19c3b8c2SApple OSS Distributions 		if (x->rn_bit >= 0) {
961*19c3b8c2SApple OSS Distributions 			for (mp = &x->rn_mklist; (m = *mp);) {
962*19c3b8c2SApple OSS Distributions 				mp = &m->rm_mklist;
963*19c3b8c2SApple OSS Distributions 			}
964*19c3b8c2SApple OSS Distributions 			*mp = t->rn_mklist;
965*19c3b8c2SApple OSS Distributions 		} else {
966*19c3b8c2SApple OSS Distributions 			/* If there are any key,mask pairs in a sibling
967*19c3b8c2SApple OSS Distributions 			 *  duped-key chain, some subset will appear sorted
968*19c3b8c2SApple OSS Distributions 			 *  in the same order attached to our mklist */
969*19c3b8c2SApple OSS Distributions 			for (m = t->rn_mklist; m && x; x = x->rn_dupedkey) {
970*19c3b8c2SApple OSS Distributions 				if (m == x->rn_mklist) {
971*19c3b8c2SApple OSS Distributions 					struct radix_mask *mm = m->rm_mklist;
972*19c3b8c2SApple OSS Distributions 					x->rn_mklist = NULL;
973*19c3b8c2SApple OSS Distributions 					if (--(m->rm_refs) < 0) {
974*19c3b8c2SApple OSS Distributions 						MKFree(m);
975*19c3b8c2SApple OSS Distributions 					}
976*19c3b8c2SApple OSS Distributions 					m = mm;
977*19c3b8c2SApple OSS Distributions 				}
978*19c3b8c2SApple OSS Distributions 			}
979*19c3b8c2SApple OSS Distributions 			if (m) {
980*19c3b8c2SApple OSS Distributions 				log(LOG_ERR, "rn_delete: Orphaned Mask "
981*19c3b8c2SApple OSS Distributions 				    "0x%llx at 0x%llx\n",
982*19c3b8c2SApple OSS Distributions 				    (uint64_t)VM_KERNEL_ADDRPERM(m),
983*19c3b8c2SApple OSS Distributions 				    (uint64_t)VM_KERNEL_ADDRPERM(x));
984*19c3b8c2SApple OSS Distributions 			}
985*19c3b8c2SApple OSS Distributions 		}
986*19c3b8c2SApple OSS Distributions 	}
987*19c3b8c2SApple OSS Distributions 	/*
988*19c3b8c2SApple OSS Distributions 	 * We may be holding an active internal node in the tree.
989*19c3b8c2SApple OSS Distributions 	 */
990*19c3b8c2SApple OSS Distributions 	x = tt + 1;
991*19c3b8c2SApple OSS Distributions 	if (t != x) {
992*19c3b8c2SApple OSS Distributions #ifndef RN_DEBUG
993*19c3b8c2SApple OSS Distributions 		*t = *x;
994*19c3b8c2SApple OSS Distributions #else
995*19c3b8c2SApple OSS Distributions 		b = t->rn_info;
996*19c3b8c2SApple OSS Distributions 		*t = *x;
997*19c3b8c2SApple OSS Distributions 		t->rn_info = b;
998*19c3b8c2SApple OSS Distributions #endif
999*19c3b8c2SApple OSS Distributions 		t->rn_left->rn_parent = t;
1000*19c3b8c2SApple OSS Distributions 		t->rn_right->rn_parent = t;
1001*19c3b8c2SApple OSS Distributions 		p = x->rn_parent;
1002*19c3b8c2SApple OSS Distributions 		if (p->rn_left == x) {
1003*19c3b8c2SApple OSS Distributions 			p->rn_left = t;
1004*19c3b8c2SApple OSS Distributions 		} else {
1005*19c3b8c2SApple OSS Distributions 			p->rn_right = t;
1006*19c3b8c2SApple OSS Distributions 		}
1007*19c3b8c2SApple OSS Distributions 	}
1008*19c3b8c2SApple OSS Distributions out:
1009*19c3b8c2SApple OSS Distributions 	tt->rn_flags &= ~RNF_ACTIVE;
1010*19c3b8c2SApple OSS Distributions 	tt[1].rn_flags &= ~RNF_ACTIVE;
1011*19c3b8c2SApple OSS Distributions 	return tt;
1012*19c3b8c2SApple OSS Distributions }
1013*19c3b8c2SApple OSS Distributions 
1014*19c3b8c2SApple OSS Distributions /*
1015*19c3b8c2SApple OSS Distributions  * This is the same as rn_walktree() except for the parameters and the
1016*19c3b8c2SApple OSS Distributions  * exit.
1017*19c3b8c2SApple OSS Distributions  */
1018*19c3b8c2SApple OSS Distributions static int
rn_walktree_from(struct radix_node_head * h,void * a,void * m,walktree_f_t * f,void * w)1019*19c3b8c2SApple OSS Distributions rn_walktree_from(struct radix_node_head *h, void *a, void *m, walktree_f_t *f,
1020*19c3b8c2SApple OSS Distributions     void *w)
1021*19c3b8c2SApple OSS Distributions {
1022*19c3b8c2SApple OSS Distributions 	int error;
1023*19c3b8c2SApple OSS Distributions 	struct radix_node *base, *next;
1024*19c3b8c2SApple OSS Distributions 	u_char *xa = (u_char *)a;
1025*19c3b8c2SApple OSS Distributions 	u_char *xm = (u_char *)m;
1026*19c3b8c2SApple OSS Distributions 	struct radix_node *rn, *last;
1027*19c3b8c2SApple OSS Distributions 	int stopping;
1028*19c3b8c2SApple OSS Distributions 	int lastb;
1029*19c3b8c2SApple OSS Distributions 	int rnh_cnt;
1030*19c3b8c2SApple OSS Distributions 
1031*19c3b8c2SApple OSS Distributions 	/*
1032*19c3b8c2SApple OSS Distributions 	 * This gets complicated because we may delete the node while
1033*19c3b8c2SApple OSS Distributions 	 * applying the function f to it; we cannot simply use the next
1034*19c3b8c2SApple OSS Distributions 	 * leaf as the successor node in advance, because that leaf may
1035*19c3b8c2SApple OSS Distributions 	 * be removed as well during deletion when it is a clone of the
1036*19c3b8c2SApple OSS Distributions 	 * current node.  When that happens, we would end up referring
1037*19c3b8c2SApple OSS Distributions 	 * to an already-freed radix node as the successor node.  To get
1038*19c3b8c2SApple OSS Distributions 	 * around this issue, if we detect that the radix tree has changed
1039*19c3b8c2SApple OSS Distributions 	 * in dimension (smaller than before), we simply restart the walk
1040*19c3b8c2SApple OSS Distributions 	 * from the top of tree.
1041*19c3b8c2SApple OSS Distributions 	 */
1042*19c3b8c2SApple OSS Distributions restart:
1043*19c3b8c2SApple OSS Distributions 	last = NULL;
1044*19c3b8c2SApple OSS Distributions 	stopping = 0;
1045*19c3b8c2SApple OSS Distributions 	rnh_cnt = h->rnh_cnt;
1046*19c3b8c2SApple OSS Distributions 
1047*19c3b8c2SApple OSS Distributions 	/*
1048*19c3b8c2SApple OSS Distributions 	 * rn_search_m is sort-of-open-coded here.
1049*19c3b8c2SApple OSS Distributions 	 */
1050*19c3b8c2SApple OSS Distributions 	for (rn = h->rnh_treetop; rn->rn_bit >= 0;) {
1051*19c3b8c2SApple OSS Distributions 		last = rn;
1052*19c3b8c2SApple OSS Distributions 		if (!(rn->rn_bmask & xm[rn->rn_offset])) {
1053*19c3b8c2SApple OSS Distributions 			break;
1054*19c3b8c2SApple OSS Distributions 		}
1055*19c3b8c2SApple OSS Distributions 
1056*19c3b8c2SApple OSS Distributions 		if (rn->rn_bmask & xa[rn->rn_offset]) {
1057*19c3b8c2SApple OSS Distributions 			rn = rn->rn_right;
1058*19c3b8c2SApple OSS Distributions 		} else {
1059*19c3b8c2SApple OSS Distributions 			rn = rn->rn_left;
1060*19c3b8c2SApple OSS Distributions 		}
1061*19c3b8c2SApple OSS Distributions 	}
1062*19c3b8c2SApple OSS Distributions 
1063*19c3b8c2SApple OSS Distributions 	/*
1064*19c3b8c2SApple OSS Distributions 	 * Two cases: either we stepped off the end of our mask,
1065*19c3b8c2SApple OSS Distributions 	 * in which case last == rn, or we reached a leaf, in which
1066*19c3b8c2SApple OSS Distributions 	 * case we want to start from the last node we looked at.
1067*19c3b8c2SApple OSS Distributions 	 * Either way, last is the node we want to start from.
1068*19c3b8c2SApple OSS Distributions 	 */
1069*19c3b8c2SApple OSS Distributions 	rn = last;
1070*19c3b8c2SApple OSS Distributions 	lastb = rn->rn_bit;
1071*19c3b8c2SApple OSS Distributions 
1072*19c3b8c2SApple OSS Distributions 	/* First time through node, go left */
1073*19c3b8c2SApple OSS Distributions 	while (rn->rn_bit >= 0) {
1074*19c3b8c2SApple OSS Distributions 		rn = rn->rn_left;
1075*19c3b8c2SApple OSS Distributions 	}
1076*19c3b8c2SApple OSS Distributions 
1077*19c3b8c2SApple OSS Distributions 	while (!stopping) {
1078*19c3b8c2SApple OSS Distributions 		base = rn;
1079*19c3b8c2SApple OSS Distributions 		/* If at right child go back up, otherwise, go right */
1080*19c3b8c2SApple OSS Distributions 		while (rn->rn_parent->rn_right == rn
1081*19c3b8c2SApple OSS Distributions 		    && !(rn->rn_flags & RNF_ROOT)) {
1082*19c3b8c2SApple OSS Distributions 			rn = rn->rn_parent;
1083*19c3b8c2SApple OSS Distributions 
1084*19c3b8c2SApple OSS Distributions 			/* if went up beyond last, stop */
1085*19c3b8c2SApple OSS Distributions 			if (rn->rn_bit <= lastb) {
1086*19c3b8c2SApple OSS Distributions 				stopping = 1;
1087*19c3b8c2SApple OSS Distributions 				/*
1088*19c3b8c2SApple OSS Distributions 				 * XXX we should jump to the 'Process leaves'
1089*19c3b8c2SApple OSS Distributions 				 * part, because the values of 'rn' and 'next'
1090*19c3b8c2SApple OSS Distributions 				 * we compute will not be used. Not a big deal
1091*19c3b8c2SApple OSS Distributions 				 * because this loop will terminate, but it is
1092*19c3b8c2SApple OSS Distributions 				 * inefficient and hard to understand!
1093*19c3b8c2SApple OSS Distributions 				 */
1094*19c3b8c2SApple OSS Distributions 			}
1095*19c3b8c2SApple OSS Distributions 		}
1096*19c3b8c2SApple OSS Distributions 
1097*19c3b8c2SApple OSS Distributions 		/*
1098*19c3b8c2SApple OSS Distributions 		 * The following code (bug fix) inherited from FreeBSD is
1099*19c3b8c2SApple OSS Distributions 		 * currently disabled, because our implementation uses the
1100*19c3b8c2SApple OSS Distributions 		 * RTF_PRCLONING scheme that has been abandoned in current
1101*19c3b8c2SApple OSS Distributions 		 * FreeBSD release.  The scheme involves setting such a flag
1102*19c3b8c2SApple OSS Distributions 		 * for the default route entry, and therefore all off-link
1103*19c3b8c2SApple OSS Distributions 		 * destinations would become clones of that entry.  Enabling
1104*19c3b8c2SApple OSS Distributions 		 * the following code would be problematic at this point,
1105*19c3b8c2SApple OSS Distributions 		 * because the removal of default route would cause only
1106*19c3b8c2SApple OSS Distributions 		 * the left-half of the tree to be traversed, leaving the
1107*19c3b8c2SApple OSS Distributions 		 * right-half untouched.  If there are clones of the entry
1108*19c3b8c2SApple OSS Distributions 		 * that reside in that right-half, they would not be deleted
1109*19c3b8c2SApple OSS Distributions 		 * and would linger around until they expire or explicitly
1110*19c3b8c2SApple OSS Distributions 		 * deleted, which is a very bad thing.
1111*19c3b8c2SApple OSS Distributions 		 *
1112*19c3b8c2SApple OSS Distributions 		 * This code should be uncommented only after we get rid
1113*19c3b8c2SApple OSS Distributions 		 * of the RTF_PRCLONING scheme.
1114*19c3b8c2SApple OSS Distributions 		 */
1115*19c3b8c2SApple OSS Distributions #if 0
1116*19c3b8c2SApple OSS Distributions 		/*
1117*19c3b8c2SApple OSS Distributions 		 * At the top of the tree, no need to traverse the right
1118*19c3b8c2SApple OSS Distributions 		 * half, prevent the traversal of the entire tree in the
1119*19c3b8c2SApple OSS Distributions 		 * case of default route.
1120*19c3b8c2SApple OSS Distributions 		 */
1121*19c3b8c2SApple OSS Distributions 		if (rn->rn_parent->rn_flags & RNF_ROOT) {
1122*19c3b8c2SApple OSS Distributions 			stopping = 1;
1123*19c3b8c2SApple OSS Distributions 		}
1124*19c3b8c2SApple OSS Distributions #endif
1125*19c3b8c2SApple OSS Distributions 
1126*19c3b8c2SApple OSS Distributions 		/* Find the next *leaf* to start from */
1127*19c3b8c2SApple OSS Distributions 		for (rn = rn->rn_parent->rn_right; rn->rn_bit >= 0;) {
1128*19c3b8c2SApple OSS Distributions 			rn = rn->rn_left;
1129*19c3b8c2SApple OSS Distributions 		}
1130*19c3b8c2SApple OSS Distributions 		next = rn;
1131*19c3b8c2SApple OSS Distributions 		/* Process leaves */
1132*19c3b8c2SApple OSS Distributions 		while ((rn = base) != 0) {
1133*19c3b8c2SApple OSS Distributions 			base = rn->rn_dupedkey;
1134*19c3b8c2SApple OSS Distributions 			if (!(rn->rn_flags & RNF_ROOT)
1135*19c3b8c2SApple OSS Distributions 			    && (error = (*f)(rn, w))) {
1136*19c3b8c2SApple OSS Distributions 				return error;
1137*19c3b8c2SApple OSS Distributions 			}
1138*19c3b8c2SApple OSS Distributions 		}
1139*19c3b8c2SApple OSS Distributions 		/* If one or more nodes got deleted, restart from top */
1140*19c3b8c2SApple OSS Distributions 		if (h->rnh_cnt < rnh_cnt) {
1141*19c3b8c2SApple OSS Distributions 			goto restart;
1142*19c3b8c2SApple OSS Distributions 		}
1143*19c3b8c2SApple OSS Distributions 		rn = next;
1144*19c3b8c2SApple OSS Distributions 		if (rn->rn_flags & RNF_ROOT) {
1145*19c3b8c2SApple OSS Distributions 			stopping = 1;
1146*19c3b8c2SApple OSS Distributions 		}
1147*19c3b8c2SApple OSS Distributions 	}
1148*19c3b8c2SApple OSS Distributions 	return 0;
1149*19c3b8c2SApple OSS Distributions }
1150*19c3b8c2SApple OSS Distributions 
1151*19c3b8c2SApple OSS Distributions static int
rn_walktree(struct radix_node_head * h,walktree_f_t * f,void * w)1152*19c3b8c2SApple OSS Distributions rn_walktree(struct radix_node_head *h, walktree_f_t *f, void *w)
1153*19c3b8c2SApple OSS Distributions {
1154*19c3b8c2SApple OSS Distributions 	int error;
1155*19c3b8c2SApple OSS Distributions 	struct radix_node *base, *next;
1156*19c3b8c2SApple OSS Distributions 	struct radix_node *rn;
1157*19c3b8c2SApple OSS Distributions 	int rnh_cnt;
1158*19c3b8c2SApple OSS Distributions 
1159*19c3b8c2SApple OSS Distributions 	/*
1160*19c3b8c2SApple OSS Distributions 	 * This gets complicated because we may delete the node while
1161*19c3b8c2SApple OSS Distributions 	 * applying the function f to it; we cannot simply use the next
1162*19c3b8c2SApple OSS Distributions 	 * leaf as the successor node in advance, because that leaf may
1163*19c3b8c2SApple OSS Distributions 	 * be removed as well during deletion when it is a clone of the
1164*19c3b8c2SApple OSS Distributions 	 * current node.  When that happens, we would end up referring
1165*19c3b8c2SApple OSS Distributions 	 * to an already-freed radix node as the successor node.  To get
1166*19c3b8c2SApple OSS Distributions 	 * around this issue, if we detect that the radix tree has changed
1167*19c3b8c2SApple OSS Distributions 	 * in dimension (smaller than before), we simply restart the walk
1168*19c3b8c2SApple OSS Distributions 	 * from the top of tree.
1169*19c3b8c2SApple OSS Distributions 	 */
1170*19c3b8c2SApple OSS Distributions restart:
1171*19c3b8c2SApple OSS Distributions 	rn = h->rnh_treetop;
1172*19c3b8c2SApple OSS Distributions 	rnh_cnt = h->rnh_cnt;
1173*19c3b8c2SApple OSS Distributions 
1174*19c3b8c2SApple OSS Distributions 	/* First time through node, go left */
1175*19c3b8c2SApple OSS Distributions 	while (rn->rn_bit >= 0) {
1176*19c3b8c2SApple OSS Distributions 		rn = rn->rn_left;
1177*19c3b8c2SApple OSS Distributions 	}
1178*19c3b8c2SApple OSS Distributions 	for (;;) {
1179*19c3b8c2SApple OSS Distributions 		base = rn;
1180*19c3b8c2SApple OSS Distributions 		/* If at right child go back up, otherwise, go right */
1181*19c3b8c2SApple OSS Distributions 		while (rn->rn_parent->rn_right == rn &&
1182*19c3b8c2SApple OSS Distributions 		    (rn->rn_flags & RNF_ROOT) == 0) {
1183*19c3b8c2SApple OSS Distributions 			rn = rn->rn_parent;
1184*19c3b8c2SApple OSS Distributions 		}
1185*19c3b8c2SApple OSS Distributions 		/* Find the next *leaf* to start from */
1186*19c3b8c2SApple OSS Distributions 		for (rn = rn->rn_parent->rn_right; rn->rn_bit >= 0;) {
1187*19c3b8c2SApple OSS Distributions 			rn = rn->rn_left;
1188*19c3b8c2SApple OSS Distributions 		}
1189*19c3b8c2SApple OSS Distributions 		next = rn;
1190*19c3b8c2SApple OSS Distributions 		/* Process leaves */
1191*19c3b8c2SApple OSS Distributions 		while ((rn = base) != NULL) {
1192*19c3b8c2SApple OSS Distributions 			base = rn->rn_dupedkey;
1193*19c3b8c2SApple OSS Distributions 			if (!(rn->rn_flags & RNF_ROOT)
1194*19c3b8c2SApple OSS Distributions 			    && (error = (*f)(rn, w))) {
1195*19c3b8c2SApple OSS Distributions 				return error;
1196*19c3b8c2SApple OSS Distributions 			}
1197*19c3b8c2SApple OSS Distributions 		}
1198*19c3b8c2SApple OSS Distributions 		/* If one or more nodes got deleted, restart from top */
1199*19c3b8c2SApple OSS Distributions 		if (h->rnh_cnt < rnh_cnt) {
1200*19c3b8c2SApple OSS Distributions 			goto restart;
1201*19c3b8c2SApple OSS Distributions 		}
1202*19c3b8c2SApple OSS Distributions 		rn = next;
1203*19c3b8c2SApple OSS Distributions 		if (rn->rn_flags & RNF_ROOT) {
1204*19c3b8c2SApple OSS Distributions 			return 0;
1205*19c3b8c2SApple OSS Distributions 		}
1206*19c3b8c2SApple OSS Distributions 	}
1207*19c3b8c2SApple OSS Distributions 	/* NOTREACHED */
1208*19c3b8c2SApple OSS Distributions }
1209*19c3b8c2SApple OSS Distributions 
1210*19c3b8c2SApple OSS Distributions int
rn_inithead(void ** head,int off)1211*19c3b8c2SApple OSS Distributions rn_inithead(void **head, int off)
1212*19c3b8c2SApple OSS Distributions {
1213*19c3b8c2SApple OSS Distributions 	struct radix_node_head *rnh;
1214*19c3b8c2SApple OSS Distributions 	struct radix_node *t, *tt, *ttt;
1215*19c3b8c2SApple OSS Distributions 	if (off > INT8_MAX) {
1216*19c3b8c2SApple OSS Distributions 		return 0;
1217*19c3b8c2SApple OSS Distributions 	}
1218*19c3b8c2SApple OSS Distributions 	if (*head) {
1219*19c3b8c2SApple OSS Distributions 		return 1;
1220*19c3b8c2SApple OSS Distributions 	}
1221*19c3b8c2SApple OSS Distributions 
1222*19c3b8c2SApple OSS Distributions 	rnh = zalloc_flags(radix_node_head_zone, Z_WAITOK_ZERO_NOFAIL);
1223*19c3b8c2SApple OSS Distributions 	*head = rnh;
1224*19c3b8c2SApple OSS Distributions 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
1225*19c3b8c2SApple OSS Distributions 	ttt = rnh->rnh_nodes + 2;
1226*19c3b8c2SApple OSS Distributions 	t->rn_right = ttt;
1227*19c3b8c2SApple OSS Distributions 	t->rn_parent = t;
1228*19c3b8c2SApple OSS Distributions 	tt = t->rn_left;
1229*19c3b8c2SApple OSS Distributions 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
1230*19c3b8c2SApple OSS Distributions 	tt->rn_bit = (short)(-1 - off);
1231*19c3b8c2SApple OSS Distributions 	*ttt = *tt;
1232*19c3b8c2SApple OSS Distributions 	ttt->rn_key = rn_ones;
1233*19c3b8c2SApple OSS Distributions 	rnh->rnh_addaddr = rn_addroute;
1234*19c3b8c2SApple OSS Distributions 	rnh->rnh_deladdr = rn_delete;
1235*19c3b8c2SApple OSS Distributions 	rnh->rnh_matchaddr = rn_match;
1236*19c3b8c2SApple OSS Distributions 	rnh->rnh_matchaddr_args = rn_match_args;
1237*19c3b8c2SApple OSS Distributions 	rnh->rnh_lookup = rn_lookup;
1238*19c3b8c2SApple OSS Distributions 	rnh->rnh_lookup_args = rn_lookup_args;
1239*19c3b8c2SApple OSS Distributions 	rnh->rnh_walktree = rn_walktree;
1240*19c3b8c2SApple OSS Distributions 	rnh->rnh_walktree_from = rn_walktree_from;
1241*19c3b8c2SApple OSS Distributions 	rnh->rnh_treetop = t;
1242*19c3b8c2SApple OSS Distributions 	rnh->rnh_cnt = 3;
1243*19c3b8c2SApple OSS Distributions 	return 1;
1244*19c3b8c2SApple OSS Distributions }
1245*19c3b8c2SApple OSS Distributions 
1246*19c3b8c2SApple OSS Distributions void
rn_init(void)1247*19c3b8c2SApple OSS Distributions rn_init(void)
1248*19c3b8c2SApple OSS Distributions {
1249*19c3b8c2SApple OSS Distributions 	char *cp, *cplim;
1250*19c3b8c2SApple OSS Distributions 	struct domain *dom;
1251*19c3b8c2SApple OSS Distributions 
1252*19c3b8c2SApple OSS Distributions 	/* lock already held when rn_init is called */
1253*19c3b8c2SApple OSS Distributions 	TAILQ_FOREACH(dom, &domains, dom_entry) {
1254*19c3b8c2SApple OSS Distributions 		if (dom->dom_maxrtkey > max_keylen) {
1255*19c3b8c2SApple OSS Distributions 			max_keylen = dom->dom_maxrtkey;
1256*19c3b8c2SApple OSS Distributions 		}
1257*19c3b8c2SApple OSS Distributions 	}
1258*19c3b8c2SApple OSS Distributions 	if (max_keylen == 0) {
1259*19c3b8c2SApple OSS Distributions 		log(LOG_ERR,
1260*19c3b8c2SApple OSS Distributions 		    "rn_init: radix functions require max_keylen be set\n");
1261*19c3b8c2SApple OSS Distributions 		return;
1262*19c3b8c2SApple OSS Distributions 	}
1263*19c3b8c2SApple OSS Distributions 	rn_zeros = zalloc_permanent(3 * max_keylen, ZALIGN_NONE);
1264*19c3b8c2SApple OSS Distributions 	rn_ones = cp = rn_zeros + max_keylen;
1265*19c3b8c2SApple OSS Distributions 	addmask_key = cplim = rn_ones + max_keylen;
1266*19c3b8c2SApple OSS Distributions 	while (cp < cplim) {
1267*19c3b8c2SApple OSS Distributions 		*cp++ = -1;
1268*19c3b8c2SApple OSS Distributions 	}
1269*19c3b8c2SApple OSS Distributions 	if (rn_inithead((void **)&mask_rnhead, 0) == 0) {
1270*19c3b8c2SApple OSS Distributions 		panic("rn_init 2");
1271*19c3b8c2SApple OSS Distributions 	}
1272*19c3b8c2SApple OSS Distributions 
1273*19c3b8c2SApple OSS Distributions 	radix_node_zone = zone_create("radix_node",
1274*19c3b8c2SApple OSS Distributions 	    sizeof(struct radix_node) * 2 + max_keylen,
1275*19c3b8c2SApple OSS Distributions 	    ZC_PGZ_USE_GUARDS | ZC_ZFREE_CLEARMEM);
1276*19c3b8c2SApple OSS Distributions }
1277