radix.c revision 108268
1139804Simp/*
2185404Sbz * Copyright (c) 1988, 1989, 1993
3185404Sbz *	The Regents of the University of California.  All rights reserved.
4185404Sbz *
5185404Sbz * Redistribution and use in source and binary forms, with or without
6185404Sbz * modification, are permitted provided that the following conditions
7185404Sbz * are met:
8185404Sbz * 1. Redistributions of source code must retain the above copyright
9185404Sbz *    notice, this list of conditions and the following disclaimer.
10185404Sbz * 2. Redistributions in binary form must reproduce the above copyright
11185404Sbz *    notice, this list of conditions and the following disclaimer in the
12185404Sbz *    documentation and/or other materials provided with the distribution.
13185404Sbz * 3. All advertising materials mentioning features or use of this software
14185404Sbz *    must display the following acknowledgement:
15185404Sbz *	This product includes software developed by the University of
16185404Sbz *	California, Berkeley and its contributors.
17185404Sbz * 4. Neither the name of the University nor the names of its contributors
18185404Sbz *    may be used to endorse or promote products derived from this software
19185404Sbz *    without specific prior written permission.
20185404Sbz *
21185404Sbz * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22185404Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23185404Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2446197Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2546155Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26116182Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27116182Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28116182Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29131177Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30131177Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3146155Sphk * SUCH DAMAGE.
3246155Sphk *
3346155Sphk *	@(#)radix.c	8.5 (Berkeley) 5/19/95
3446155Sphk * $FreeBSD: head/sys/net/radix.c 108268 2002-12-25 09:16:58Z ru $
3546155Sphk */
3646155Sphk
3746155Sphk/*
38164032Srwatson * Routines to build and maintain radix trees for routing lookups.
3946155Sphk */
40124882Srwatson#ifndef _RADIX_H_
41177785Skib#include <sys/param.h>
4246155Sphk#ifdef	_KERNEL
4387275Srwatson#include <sys/systm.h>
4487275Srwatson#include <sys/malloc.h>
45168401Spjd#include <sys/domain.h>
46113275Smike#else
47147185Spjd#include <stdlib.h>
48113275Smike#endif
4946155Sphk#include <sys/syslog.h>
50113275Smike#include <net/radix.h>
5157163Srwatson#endif
52113275Smike
53181803Sbzstatic int	rn_walktree_from(struct radix_node_head *h, void *a, void *m,
54185029Spjd		    walktree_f_t *f, void *w);
5546155Sphkstatic int rn_walktree(struct radix_node_head *, walktree_f_t *, void *);
5646155Sphkstatic struct radix_node
5746155Sphk	 *rn_insert(void *, struct radix_node_head *, int *,
58163606Srwatson	     struct radix_node [2]),
59163606Srwatson	 *rn_newpair(void *, int, struct radix_node[2]),
6046155Sphk	 *rn_search(void *, struct radix_node *),
6146155Sphk	 *rn_search_m(void *, struct radix_node *, void *);
6289414Sarr
6357163Srwatsonstatic int	max_keylen;
6457163Srwatsonstatic struct radix_mask *rn_mkfreelist;
6557163Srwatsonstatic struct radix_node_head *mask_rnhead;
6689414Sarrstatic char *addmask_key;
6757163Srwatsonstatic char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
6857163Srwatsonstatic char *rn_zeros, *rn_ones;
6957163Srwatson
7061235Srwatson#define rn_masktop (mask_rnhead->rnh_treetop)
7189414Sarr#undef Bcmp
7261235Srwatson#define Bcmp(a, b, l) \
7361235Srwatson	((l) == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)(l)))
7461235Srwatson
7568024Srwatsonstatic int	rn_lexobetter(void *m_arg, void *n_arg);
7689414Sarrstatic struct radix_mask *
7768024Srwatson		rn_new_radix_mask(struct radix_node *tt,
7868024Srwatson		    struct radix_mask *next);
7968024Srwatsonstatic int	rn_satsifies_leaf(char *trial, struct radix_node *leaf,
80147185Spjd		    int skip);
81147185Spjd
82147185Spjd/*
83147185Spjd * The data structure for the keys is a radix tree with one way
84125804Srwatson * branching removed.  The index rn_bit at an internal node n represents a bit
85128664Sbmilekic * position to be tested.  The tree is arranged so that all descendants
86128664Sbmilekic * of a node n have keys whose bits all agree up to position rn_bit - 1.
87128664Sbmilekic * (We say the index of n is rn_bit.)
88128664Sbmilekic *
89128664Sbmilekic * There is at least one descendant which has a one bit at position rn_bit,
90141543Scperciva * and at least one with a zero there.
91141543Scperciva *
92141543Scperciva * A route is determined by a pair of key and mask.  We require that the
93141543Scperciva * bit-wise logical and of the key and mask to be the key.
94141543Scperciva * We define the index of a route to associated with the mask to be
95168396Spjd * the first bit number in the mask where 0 occurs (with bit number 0
96168396Spjd * representing the highest order bit).
97168396Spjd *
98168396Spjd * We say a mask is normal if every bit is 0, past the index of the mask.
99168396Spjd * If a node n has a descendant (k, m) with index(m) == index(n) == rn_bit,
100179881Sdelphij * and m is a normal mask, then the route applies to every descendant of n.
101179881Sdelphij * If the index(m) < rn_bit, this implies the trailing last few bits of k
102168401Spjd * before bit b are all 0, (and hence consequently true of every descendant
103179881Sdelphij * of n), so the route applies to all descendants of the node as well.
104113275Smike *
105113275Smike * Similar logic shows that a non-normal mask m such that
106113275Smike * index(m) <= index(n) could potentially apply to many children of n.
107124882Srwatson * Thus, for each non-host route, we attach its mask to a list at an internal
108113275Smike * node as high in the tree as we can go.
109113275Smike *
110113275Smike * The present version of the code makes use of normal routes in short-
111113275Smike * circuiting an explict mask and compare operation when testing whether
112113275Smike * a key satisfies a normal route, and also in remembering the unique leaf
113113275Smike * that governs a subtree.
114179881Sdelphij */
115179881Sdelphij
116113275Smikestatic struct radix_node *
117113275Smikern_search(v_arg, head)
118113275Smike	void *v_arg;
119113275Smike	struct radix_node *head;
12082710Sdillon{
121114168Smike	register struct radix_node *x;
122114168Smike	register caddr_t v;
123114168Smike
12482710Sdillon	for (x = head, v = v_arg; x->rn_bit >= 0;) {
12546155Sphk		if (x->rn_bmask & v[x->rn_offset])
126114168Smike			x = x->rn_right;
12746155Sphk		else
128113275Smike			x = x->rn_left;
129179881Sdelphij	}
13046155Sphk	return (x);
131113275Smike}
132179881Sdelphij
13346155Sphkstatic struct radix_node *
134114168Smikern_search_m(v_arg, head, m_arg)
13546155Sphk	struct radix_node *head;
13684828Sjhb	void *v_arg, *m_arg;
13784828Sjhb{
13884828Sjhb	register struct radix_node *x;
13984828Sjhb	register caddr_t v = v_arg, m = m_arg;
140184205Sdes
14193818Sjhb	for (x = head; x->rn_bit >= 0;) {
142113275Smike		if ((x->rn_bmask & m[x->rn_offset]) &&
143114168Smike		    (x->rn_bmask & v[x->rn_offset]))
144113275Smike			x = x->rn_right;
145113275Smike		else
146150652Scsjp			x = x->rn_left;
147150652Scsjp	}
148113275Smike	return x;
149150652Scsjp}
150113275Smike
151150652Scsjpint
152113275Smikern_refines(m_arg, n_arg)
153175294Sattilio	void *m_arg, *n_arg;
154113275Smike{
155150652Scsjp	register caddr_t m = m_arg, n = n_arg;
156114168Smike	register caddr_t lim, lim2 = lim = n + *(u_char *)n;
15784828Sjhb	int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
158113275Smike	int masks_are_equal = 1;
159113275Smike
160113275Smike	if (longer > 0)
161113275Smike		lim -= longer;
162185029Spjd	while (n < lim) {
163113275Smike		if (*n & ~(*m))
164179881Sdelphij			return 0;
165168401Spjd		if (*n++ != *m++)
166179881Sdelphij			masks_are_equal = 0;
167179881Sdelphij	}
168179881Sdelphij	while (n < lim2)
169179881Sdelphij		if (*n++)
170179881Sdelphij			return 0;
171179881Sdelphij	if (masks_are_equal && (longer < 0))
172179881Sdelphij		for (lim2 = m - longer; m < lim2; )
173179881Sdelphij			if (*m++)
174179881Sdelphij				return 1;
175179881Sdelphij	return (!masks_are_equal);
176179881Sdelphij}
177179881Sdelphij
178179881Sdelphijstruct radix_node *
179179881Sdelphijrn_lookup(v_arg, m_arg, head)
180179881Sdelphij	void *v_arg, *m_arg;
181179881Sdelphij	struct radix_node_head *head;
182113275Smike{
183113275Smike	register struct radix_node *x;
184185029Spjd	caddr_t netmask = 0;
185113275Smike
186113275Smike	if (m_arg) {
187113275Smike		x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_offset);
188113275Smike		if (x == 0)
189113275Smike			return (0);
190113275Smike		netmask = x->rn_key;
191113275Smike	}
192113275Smike	x = rn_match(v_arg, head);
193113275Smike	if (x && netmask) {
194113275Smike		while (x && x->rn_mask != netmask)
195168401Spjd			x = x->rn_dupedkey;
196113275Smike	}
197113275Smike	return x;
198185029Spjd}
199113275Smike
200150652Scsjpstatic int
201113275Smikern_satsifies_leaf(trial, leaf, skip)
202150652Scsjp	char *trial;
203113275Smike	register struct radix_node *leaf;
204113275Smike	int skip;
205184205Sdes{
206113275Smike	register char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
207113275Smike	char *cplim;
208113275Smike	int length = min(*(u_char *)cp, *(u_char *)cp2);
209113275Smike
210114168Smike	if (cp3 == 0)
211114168Smike		cp3 = rn_ones;
212114168Smike	else
213113275Smike		length = min(length, *(u_char *)cp3);
214113275Smike	cplim = cp + length; cp3 += skip; cp2 += skip;
215114168Smike	for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
216113275Smike		if ((*cp ^ *cp2) & *cp3)
217113275Smike			return 0;
218113275Smike	return 1;
219113275Smike}
220150652Scsjp
221167309Spjdstruct radix_node *
222126023Snectarrn_match(v_arg, head)
223126023Snectar	void *v_arg;
224126023Snectar	struct radix_node_head *head;
225126023Snectar{
226126023Snectar	caddr_t v = v_arg;
227126023Snectar	register struct radix_node *t = head->rnh_treetop, *x;
228126023Snectar	register caddr_t cp = v, cp2;
229126023Snectar	caddr_t cplim;
230164032Srwatson	struct radix_node *saved_t, *top = t;
231126023Snectar	int off = t->rn_offset, vlen = *(u_char *)cp, matched_off;
232126023Snectar	register int test, b, rn_bit;
233126023Snectar
234113275Smike	/*
235168401Spjd	 * Open code rn_search(v, top) to avoid overhead of extra
236113275Smike	 * subroutine call.
237113275Smike	 */
238168401Spjd	for (; t->rn_bit >= 0; ) {
239113275Smike		if (t->rn_bmask & cp[t->rn_offset])
240113275Smike			t = t->rn_right;
241113275Smike		else
242113275Smike			t = t->rn_left;
243168401Spjd	}
244113275Smike	/*
245150652Scsjp	 * See if we match exactly as a host destination
246175202Sattilio	 * or at least learn how many bits match, for normal mask finesse.
247113275Smike	 *
248113275Smike	 * It doesn't hurt us to limit how many bytes to check
249113275Smike	 * to the length of the mask, since if it matches we had a genuine
250172930Srwatson	 * match and the leaf we have is the most specific one anyway;
251113275Smike	 * if it didn't match with a shorter length it would fail
252113275Smike	 * with a long one.  This wins big for class B&C netmasks which
253175294Sattilio	 * are probably the most common case...
254113275Smike	 */
255150652Scsjp	if (t->rn_mask)
256113275Smike		vlen = *(u_char *)t->rn_mask;
25784828Sjhb	cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
25884828Sjhb	for (; cp < cplim; cp++, cp2++)
25984828Sjhb		if (*cp != *cp2)
260113275Smike			goto on1;
26184828Sjhb	/*
262113630Sjhb	 * This extra grot is in case we are explicitly asked
26384828Sjhb	 * to look up the default.  Ugh!
26484828Sjhb	 *
26584828Sjhb	 * Never return the root node itself, it seems to cause a
26646155Sphk	 * lot of confusion.
267113275Smike	 */
268175294Sattilio	if (t->rn_flags & RNF_ROOT)
269150652Scsjp		t = t->rn_dupedkey;
270113275Smike	return t;
271113275Smikeon1:
272113275Smike	test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
27346155Sphk	for (b = 7; (test >>= 1) > 0;)
27446155Sphk		b--;
27546155Sphk	matched_off = cp - v;
276113275Smike	b += matched_off << 3;
277113275Smike	rn_bit = -1 - b;
278113275Smike	/*
279168399Spjd	 * If there is a host route in a duped-key chain, it will be first.
280113275Smike	 */
281113275Smike	if ((saved_t = t)->rn_mask == 0)
282113275Smike		t = t->rn_dupedkey;
283113275Smike	for (; t; t = t->rn_dupedkey)
284168401Spjd		/*
285113275Smike		 * Even if we don't match exactly as a host,
286113275Smike		 * we may match if the leaf we wound up at is
287113275Smike		 * a route to a net.
288168489Spjd		 */
289168489Spjd		if (t->rn_flags & RNF_NORMAL) {
290168489Spjd			if (rn_bit <= t->rn_bit)
291168489Spjd				return t;
292113275Smike		} else if (rn_satsifies_leaf(v, t, matched_off))
293113275Smike				return t;
294113275Smike	t = saved_t;
295113275Smike	/* start searching up the tree */
296113275Smike	do {
297113275Smike		register struct radix_mask *m;
29872786Srwatson		t = t->rn_parent;
299185029Spjd		m = t->rn_mklist;
30072786Srwatson		/*
30172786Srwatson		 * If non-contiguous masks ever become important
302185029Spjd		 * we can restore the masking and open coding of
30372786Srwatson		 * the search and satisfaction test and put the
30472786Srwatson		 * calculation of "off" back before the "do".
305168483Spjd		 */
306124882Srwatson		while (m) {
307144660Sjeff			if (m->rm_flags & RNF_NORMAL) {
30887275Srwatson				if (rn_bit <= m->rm_bit)
30972786Srwatson					return (m->rm_leaf);
31087275Srwatson			} else {
31172786Srwatson				off = min(t->rn_offset, matched_off);
31272786Srwatson				x = rn_search_m(v, t, m->rm_mask);
313185029Spjd				while (x && x->rn_mask != m->rm_mask)
314185029Spjd					x = x->rn_dupedkey;
315185029Spjd				if (x && rn_satsifies_leaf(v, x, off))
316185029Spjd					return x;
317185029Spjd			}
318185029Spjd			m = m->rm_mklist;
319185029Spjd		}
320185029Spjd	} while (t != top);
321124882Srwatson	return 0;
322124882Srwatson}
323124882Srwatson
324124882Srwatson#ifdef RN_DEBUG
325150652Scsjpint	rn_nodenum;
326124882Srwatsonstruct	radix_node *rn_clist;
327124882Srwatsonint	rn_saveinfo;
328124882Srwatsonint	rn_debug =  1;
329168489Spjd#endif
330168489Spjd
331168489Spjdstatic struct radix_node *
332185029Spjdrn_newpair(v, b, nodes)
333168489Spjd	void *v;
334185029Spjd	int b;
335185029Spjd	struct radix_node nodes[2];
336185029Spjd{
337150652Scsjp	register struct radix_node *tt = nodes, *t = tt + 1;
338124882Srwatson	t->rn_bit = b;
339150652Scsjp	t->rn_bmask = 0x80 >> (b & 7);
340124882Srwatson	t->rn_left = tt;
341124882Srwatson	t->rn_offset = b >> 3;
342124882Srwatson	tt->rn_bit = -1;
343184205Sdes	tt->rn_key = (caddr_t)v;
344184205Sdes	tt->rn_parent = t;
345124882Srwatson	tt->rn_flags = t->rn_flags = RNF_ACTIVE;
346124882Srwatson	tt->rn_mklist = t->rn_mklist = 0;
34772786Srwatson#ifdef RN_DEBUG
348185029Spjd	tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
34972786Srwatson	tt->rn_twin = t;
35072786Srwatson	tt->rn_ybro = rn_clist;
351185029Spjd	rn_clist = tt;
352168489Spjd#endif
353168489Spjd	return t;
35472786Srwatson}
355185029Spjd
356185029Spjdstatic struct radix_node *
357185029Spjdrn_insert(v_arg, head, dupentry, nodes)
358185029Spjd	void *v_arg;
359185029Spjd	struct radix_node_head *head;
360185029Spjd	int *dupentry;
361185029Spjd	struct radix_node nodes[2];
362185029Spjd{
36387275Srwatson	caddr_t v = v_arg;
36472786Srwatson	struct radix_node *top = head->rnh_treetop;
36572786Srwatson	int head_off = top->rn_offset, vlen = (int)*((u_char *)v);
36687275Srwatson	register struct radix_node *t = rn_search(v_arg, top);
36787275Srwatson	register caddr_t cp = v + head_off;
36887275Srwatson	register int b;
36987275Srwatson	struct radix_node *tt;
37087275Srwatson    	/*
37187275Srwatson	 * Find first bit at which v and t->rn_key differ
37287275Srwatson	 */
37346155Sphk    {
37472786Srwatson	register caddr_t cp2 = t->rn_key + head_off;
37546155Sphk	register int cmp_res;
37646155Sphk	caddr_t cplim = v + vlen;
37746155Sphk
37872786Srwatson	while (cp < cplim)
37946155Sphk		if (*cp2++ != *cp++)
380167309Spjd			goto on1;
38146155Sphk	*dupentry = 1;
38246155Sphk	return t;
38346155Sphkon1:
38446155Sphk	*dupentry = 0;
385167309Spjd	cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
38672786Srwatson	for (b = (cp - v) << 3; cmp_res; b--)
38746155Sphk		cmp_res >>= 1;
38872786Srwatson    }
38946155Sphk    {
39046155Sphk	register struct radix_node *p, *x = top;
39181114Srwatson	cp = v;
39281114Srwatson	do {
39381114Srwatson		p = x;
39481114Srwatson		if (cp[x->rn_offset] & x->rn_bmask)
39581114Srwatson			x = x->rn_right;
39681114Srwatson		else
39781114Srwatson			x = x->rn_left;
39872786Srwatson	} while (b > (unsigned) x->rn_bit);
39946155Sphk				/* x->rn_bit < b && x->rn_bit >= 0 */
40046155Sphk#ifdef RN_DEBUG
40146155Sphk	if (rn_debug)
40246155Sphk		log(LOG_DEBUG, "rn_insert: Going In:\n"), traverse(p);
40346155Sphk#endif
40472786Srwatson	t = rn_newpair(v_arg, b, nodes);
40546155Sphk	tt = t->rn_left;
40646155Sphk	if ((cp[p->rn_offset] & p->rn_bmask) == 0)
40746155Sphk		p->rn_left = t;
40872786Srwatson	else
40946155Sphk		p->rn_right = t;
41046155Sphk	x->rn_parent = t;
41146155Sphk	t->rn_parent = p; /* frees x, p as temp vars below */
41246155Sphk	if ((cp[t->rn_offset] & t->rn_bmask) == 0) {
41346155Sphk		t->rn_right = x;
41481114Srwatson	} else {
41546155Sphk		t->rn_right = tt;
41672786Srwatson		t->rn_left = x;
41746155Sphk	}
41872786Srwatson#ifdef RN_DEBUG
41946155Sphk	if (rn_debug)
42046155Sphk		log(LOG_DEBUG, "rn_insert: Coming Out:\n"), traverse(p);
42146155Sphk#endif
42246155Sphk    }
42346155Sphk	return (tt);
42446155Sphk}
42572786Srwatson
42646155Sphkstruct radix_node *
427114168Smikern_addmask(n_arg, search, skip)
42846155Sphk	int search, skip;
42946155Sphk	void *n_arg;
430114168Smike{
43161235Srwatson	caddr_t netmask = (caddr_t)n_arg;
43261235Srwatson	register struct radix_node *x;
43361235Srwatson	register caddr_t cp, cplim;
43446155Sphk	register int b = 0, mlen, j;
43572786Srwatson	int maskduplicated, m0, isnormal;
43646155Sphk	struct radix_node *saved_x;
43746155Sphk	static int last_zeroed = 0;
43846155Sphk
43946155Sphk	if ((mlen = *(u_char *)netmask) > max_keylen)
44046155Sphk		mlen = max_keylen;
44172786Srwatson	if (skip == 0)
44272786Srwatson		skip = 1;
44372786Srwatson	if (mlen <= skip)
44472786Srwatson		return (mask_rnhead->rnh_nodes);
44572786Srwatson	if (skip > 1)
446114168Smike		Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
44772786Srwatson	if ((m0 = mlen) > skip)
44872786Srwatson		Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
44972786Srwatson	/*
45072786Srwatson	 * Trim trailing zeroes.
45172786Srwatson	 */
45272786Srwatson	for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
45372786Srwatson		cp--;
45472786Srwatson	mlen = cp - addmask_key;
45572786Srwatson	if (mlen <= skip) {
45672786Srwatson		if (m0 >= last_zeroed)
45772786Srwatson			last_zeroed = mlen;
45872786Srwatson		return (mask_rnhead->rnh_nodes);
45972786Srwatson	}
46072786Srwatson	if (m0 < last_zeroed)
46172786Srwatson		Bzero(addmask_key + m0, last_zeroed - m0);
46272786Srwatson	*addmask_key = last_zeroed = mlen;
463114168Smike	x = rn_search(addmask_key, rn_masktop);
46472786Srwatson	if (Bcmp(addmask_key, x->rn_key, mlen) != 0)
46572786Srwatson		x = 0;
46672786Srwatson	if (x || search)
46772786Srwatson		return (x);
46891384Srobert	R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
46991384Srobert	if ((saved_x = x) == 0)
47091384Srobert		return (0);
47191384Srobert	Bzero(x, max_keylen + 2 * sizeof (*x));
47291391Srobert	netmask = cp = (caddr_t)(x + 2);
473114168Smike	Bcopy(addmask_key, cp, mlen);
47491384Srobert	x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
475183550Szec	if (maskduplicated) {
47691384Srobert		log(LOG_ERR, "rn_addmask: mask impossibly already in tree");
47791391Srobert		Free(saved_x);
47891391Srobert		return (x);
479105354Srobert	}
48091391Srobert	/*
481180291Srwatson	 * Calculate index of mask, and check for normalcy.
482180291Srwatson	 */
483181803Sbz	cplim = netmask + mlen; isnormal = 1;
484180291Srwatson	for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;)
485180291Srwatson		cp++;
48691384Srobert	if (cp != cplim) {
487113275Smike		for (j = 0x80; (j & *cp) != 0; j >>= 1)
488125804Srwatson			b++;
489147185Spjd		if (*cp != normal_chars[b] || cp != (cplim - 1))
490147185Spjd			isnormal = 0;
491147185Spjd	}
492147185Spjd	b += (cp - netmask) << 3;
493147185Spjd	x->rn_bit = -1 - b;
494125804Srwatson	if (isnormal)
495125804Srwatson		x->rn_flags |= RNF_NORMAL;
496147185Spjd	return (x);
497125804Srwatson}
498147185Spjd
499147185Spjdstatic int	/* XXX: arbitrary ordering for non-contiguous masks */
500147185Spjdrn_lexobetter(m_arg, n_arg)
501125804Srwatson	void *m_arg, *n_arg;
502147185Spjd{
503147185Spjd	register u_char *mp = m_arg, *np = n_arg, *lim;
504147185Spjd
505147185Spjd	if (*mp > *np)
506147185Spjd		return 1;  /* not really, but need to check longer one first */
507147185Spjd	if (*mp == *np)
508147185Spjd		for (lim = mp + *mp; mp < lim;)
509147185Spjd			if (*mp++ > *np++)
510147185Spjd				return 1;
511147185Spjd	return 0;
512147185Spjd}
513147185Spjd
514147185Spjdstatic struct radix_mask *
515147185Spjdrn_new_radix_mask(tt, next)
516147185Spjd	register struct radix_node *tt;
517147185Spjd	register struct radix_mask *next;
518147185Spjd{
519147185Spjd	register struct radix_mask *m;
520147185Spjd
521147185Spjd	MKGet(m);
522147185Spjd	if (m == 0) {
523147185Spjd		log(LOG_ERR, "Mask for route not entered\n");
524147185Spjd		return (0);
525147185Spjd	}
526147185Spjd	Bzero(m, sizeof *m);
527147185Spjd	m->rm_bit = tt->rn_bit;
528147185Spjd	m->rm_flags = tt->rn_flags;
529147185Spjd	if (tt->rn_flags & RNF_NORMAL)
530147185Spjd		m->rm_leaf = tt;
531147185Spjd	else
532147185Spjd		m->rm_mask = tt->rn_mask;
533147185Spjd	m->rm_mklist = next;
534147185Spjd	tt->rn_mklist = m;
535147185Spjd	return m;
536147185Spjd}
537147185Spjd
538147185Spjdstruct radix_node *
539147185Spjdrn_addroute(v_arg, n_arg, head, treenodes)
540147185Spjd	void *v_arg, *n_arg;
541147185Spjd	struct radix_node_head *head;
542147185Spjd	struct radix_node treenodes[2];
543147185Spjd{
544147185Spjd	caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
545125804Srwatson	register struct radix_node *t, *x = 0, *tt;
546147185Spjd	struct radix_node *saved_tt, *top = head->rnh_treetop;
547147185Spjd	short b = 0, b_leaf = 0;
548147185Spjd	int keyduplicated;
549147185Spjd	caddr_t mmask;
550147185Spjd	struct radix_mask *m, **mp;
551147185Spjd
552147185Spjd	/*
553147185Spjd	 * In dealing with non-contiguous masks, there may be
554147185Spjd	 * many different routes which have the same mask.
555147185Spjd	 * We will find it useful to have a unique pointer to
556147185Spjd	 * the mask to speed avoiding duplicate references at
557147185Spjd	 * nodes and possibly save time in calculating indices.
558147185Spjd	 */
559147185Spjd	if (netmask)  {
560147185Spjd		if ((x = rn_addmask(netmask, 0, top->rn_offset)) == 0)
561147185Spjd			return (0);
562147185Spjd		b_leaf = x->rn_bit;
563147185Spjd		b = -1 - x->rn_bit;
564147185Spjd		netmask = x->rn_key;
565147185Spjd	}
566147185Spjd	/*
567147185Spjd	 * Deal with duplicated keys: attach node to previous instance
568147185Spjd	 */
569147185Spjd	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
570147185Spjd	if (keyduplicated) {
571147185Spjd		for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
572147185Spjd			if (tt->rn_mask == netmask)
573147185Spjd				return (0);
574125804Srwatson			if (netmask == 0 ||
575125804Srwatson			    (tt->rn_mask &&
576164032Srwatson			     ((b_leaf < tt->rn_bit) /* index(netmask) > node */
577164032Srwatson			      || rn_refines(netmask, tt->rn_mask)
578164032Srwatson			      || rn_lexobetter(netmask, tt->rn_mask))))
579164032Srwatson				break;
580164032Srwatson		}
581164032Srwatson		/*
582164032Srwatson		 * If the mask is not duplicated, we wouldn't
583164032Srwatson		 * find it among possible duplicate key entries
584164032Srwatson		 * anyway, so the above test doesn't hurt.
585164032Srwatson		 *
586164032Srwatson		 * We sort the masks for a duplicated key the same way as
587164032Srwatson		 * in a masklist -- most specific to least specific.
588164032Srwatson		 * This may require the unfortunate nuisance of relocating
589164032Srwatson		 * the head of the list.
590164032Srwatson		 *
591164032Srwatson		 * We also reverse, or doubly link the list through the
592164032Srwatson		 * parent pointer.
593164032Srwatson		 */
594166827Srwatson		if (tt == saved_tt) {
595164032Srwatson			struct	radix_node *xx = x;
596164032Srwatson			/* link in at head of list */
597164032Srwatson			(tt = treenodes)->rn_dupedkey = t;
598164032Srwatson			tt->rn_flags = t->rn_flags;
599164032Srwatson			tt->rn_parent = x = t->rn_parent;
600164032Srwatson			t->rn_parent = tt;	 		/* parent */
601164032Srwatson			if (x->rn_left == t)
602164032Srwatson				x->rn_left = tt;
603164032Srwatson			else
604166827Srwatson				x->rn_right = tt;
605164032Srwatson			saved_tt = tt; x = xx;
606164032Srwatson		} else {
607164032Srwatson			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
608164032Srwatson			t->rn_dupedkey = tt;
609164032Srwatson			tt->rn_parent = t;			/* parent */
610164032Srwatson			if (tt->rn_dupedkey)			/* parent */
611164032Srwatson				tt->rn_dupedkey->rn_parent = tt; /* parent */
612164032Srwatson		}
613164032Srwatson#ifdef RN_DEBUG
614164032Srwatson		t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
615164032Srwatson		tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
616164032Srwatson#endif
617164032Srwatson		tt->rn_key = (caddr_t) v;
618164032Srwatson		tt->rn_bit = -1;
619164032Srwatson		tt->rn_flags = RNF_ACTIVE;
620164032Srwatson	}
621164032Srwatson	/*
622164032Srwatson	 * Put mask in tree.
623164032Srwatson	 */
624164032Srwatson	if (netmask) {
625164032Srwatson		tt->rn_mask = netmask;
626164032Srwatson		tt->rn_bit = x->rn_bit;
627164032Srwatson		tt->rn_flags |= x->rn_flags & RNF_NORMAL;
628164032Srwatson	}
629164032Srwatson	t = saved_tt->rn_parent;
630164032Srwatson	if (keyduplicated)
631164032Srwatson		goto on2;
632164032Srwatson	b_leaf = -1 - t->rn_bit;
633164032Srwatson	if (t->rn_right == saved_tt)
634164032Srwatson		x = t->rn_left;
635164032Srwatson	else
636164032Srwatson		x = t->rn_right;
637164032Srwatson	/* Promote general routes from below */
638164032Srwatson	if (x->rn_bit < 0) {
639164032Srwatson	    for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
640164032Srwatson		if (x->rn_mask && (x->rn_bit >= b_leaf) && x->rn_mklist == 0) {
641164032Srwatson			*mp = m = rn_new_radix_mask(x, 0);
642164032Srwatson			if (m)
643164032Srwatson				mp = &m->rm_mklist;
644164032Srwatson		}
645164032Srwatson	} else if (x->rn_mklist) {
646164032Srwatson		/*
647164032Srwatson		 * Skip over masks whose index is > that of new node
648164032Srwatson		 */
649164032Srwatson		for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
650164032Srwatson			if (m->rm_bit >= b_leaf)
651164032Srwatson				break;
652164032Srwatson		t->rn_mklist = m; *mp = 0;
653164032Srwatson	}
654164032Srwatsonon2:
655164032Srwatson	/* Add new route to highest possible ancestor's list */
656164032Srwatson	if ((netmask == 0) || (b > t->rn_bit ))
657164032Srwatson		return tt; /* can't lift at all */
658164032Srwatson	b_leaf = tt->rn_bit;
659164032Srwatson	do {
660164032Srwatson		x = t;
661164032Srwatson		t = t->rn_parent;
662164032Srwatson	} while (b <= t->rn_bit && x != top);
663164032Srwatson	/*
664164032Srwatson	 * Search through routes associated with node to
665164032Srwatson	 * insert new route according to index.
666164032Srwatson	 * Need same criteria as when sorting dupedkeys to avoid
667164032Srwatson	 * double loop on deletion.
668164032Srwatson	 */
669166831Srwatson	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
670166831Srwatson		if (m->rm_bit < b_leaf)
671164032Srwatson			continue;
672166832Srwatson		if (m->rm_bit > b_leaf)
673166832Srwatson			break;
674164032Srwatson		if (m->rm_flags & RNF_NORMAL) {
675164032Srwatson			mmask = m->rm_leaf->rn_mask;
676164032Srwatson			if (tt->rn_flags & RNF_NORMAL) {
677164032Srwatson			    log(LOG_ERR,
678164032Srwatson			        "Non-unique normal route, mask not entered\n");
679164032Srwatson				return tt;
680164032Srwatson			}
681164032Srwatson		} else
682164032Srwatson			mmask = m->rm_mask;
683164032Srwatson		if (mmask == netmask) {
684164032Srwatson			m->rm_refs++;
685164032Srwatson			tt->rn_mklist = m;
686164032Srwatson			return tt;
687164032Srwatson		}
688164032Srwatson		if (rn_refines(netmask, mmask)
689164032Srwatson		    || rn_lexobetter(netmask, mmask))
690167152Spjd			break;
691164032Srwatson	}
692164032Srwatson	*mp = rn_new_radix_mask(tt, *mp);
693164032Srwatson	return tt;
694172860Srwatson}
695164032Srwatson
696164032Srwatsonstruct radix_node *
697164032Srwatsonrn_delete(v_arg, netmask_arg, head)
698164032Srwatson	void *v_arg, *netmask_arg;
699164032Srwatson	struct radix_node_head *head;
700164032Srwatson{
701164032Srwatson	register struct radix_node *t, *p, *x, *tt;
702164032Srwatson	struct radix_mask *m, *saved_m, **mp;
703164032Srwatson	struct radix_node *dupedkey, *saved_tt, *top;
704164032Srwatson	caddr_t v, netmask;
705164032Srwatson	int b, head_off, vlen;
706164032Srwatson
707164032Srwatson	v = v_arg;
708164032Srwatson	netmask = netmask_arg;
709168396Spjd	x = head->rnh_treetop;
710168396Spjd	tt = rn_search(v, x);
711168396Spjd	head_off = x->rn_offset;
712168396Spjd	vlen =  *(u_char *)v;
713168396Spjd	saved_tt = tt;
714168396Spjd	top = x;
715168699Spjd	if (tt == 0 ||
716168396Spjd	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
717168396Spjd		return (0);
718168396Spjd	/*
719168396Spjd	 * Delete our route from mask lists.
720168396Spjd	 */
721168396Spjd	if (netmask) {
722168591Srwatson		if ((x = rn_addmask(netmask, 1, head_off)) == 0)
723168591Srwatson			return (0);
724164032Srwatson		netmask = x->rn_key;
725164032Srwatson		while (tt->rn_mask != netmask)
726168591Srwatson			if ((tt = tt->rn_dupedkey) == 0)
727164032Srwatson				return (0);
728164032Srwatson	}
729164032Srwatson	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
730175630Sbz		goto on1;
731175630Sbz	if (tt->rn_flags & RNF_NORMAL) {
732175630Sbz		if (m->rm_leaf != tt || m->rm_refs > 0) {
733175630Sbz			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
734175630Sbz			return 0;  /* dangling ref could cause disaster */
735175630Sbz		}
736164032Srwatson	} else {
737164032Srwatson		if (m->rm_mask != tt->rn_mask) {
738164032Srwatson			log(LOG_ERR, "rn_delete: inconsistent annotation\n");
739164032Srwatson			goto on1;
740164032Srwatson		}
741164032Srwatson		if (--m->rm_refs >= 0)
742164032Srwatson			goto on1;
743164032Srwatson	}
744164032Srwatson	b = -1 - tt->rn_bit;
745164032Srwatson	t = saved_tt->rn_parent;
746164032Srwatson	if (b > t->rn_bit)
747164032Srwatson		goto on1; /* Wasn't lifted at all */
748164032Srwatson	do {
749164032Srwatson		x = t;
750164032Srwatson		t = t->rn_parent;
751164032Srwatson	} while (b <= t->rn_bit && x != top);
752164032Srwatson	for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
753164032Srwatson		if (m == saved_m) {
754164032Srwatson			*mp = m->rm_mklist;
755164032Srwatson			MKFree(m);
756164032Srwatson			break;
757164032Srwatson		}
758164032Srwatson	if (m == 0) {
759164032Srwatson		log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
760164032Srwatson		if (tt->rn_flags & RNF_NORMAL)
761164032Srwatson			return (0); /* Dangling ref to us */
762113275Smike	}
763113275Smikeon1:
764113275Smike	/*
765113275Smike	 * Eliminate us from tree
766113275Smike	 */
767113275Smike	if (tt->rn_flags & RNF_ROOT)
768113275Smike		return (0);
769127020Spjd#ifdef RN_DEBUG
770125806Srwatson	/* Get us out of the creation list */
771113275Smike	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
772168401Spjd	if (t) t->rn_ybro = tt->rn_ybro;
773168401Spjd#endif
774168401Spjd	t = tt->rn_parent;
775113275Smike	dupedkey = saved_tt->rn_dupedkey;
776168401Spjd	if (dupedkey) {
777113275Smike		/*
778113275Smike		 * Here, tt is the deletion target and
779167309Spjd		 * saved_tt is the head of the dupekey chain.
780113275Smike		 */
781113275Smike		if (tt == saved_tt) {
782113275Smike			/* remove from head of chain */
783168487Spjd			x = dupedkey; x->rn_parent = t;
784113275Smike			if (t->rn_left == tt)
785168487Spjd				t->rn_left = x;
786113275Smike			else
787113275Smike				t->rn_right = x;
788113275Smike		} else {
789113275Smike			/* find node in front of tt on the chain */
790168401Spjd			for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
791113275Smike				p = p->rn_dupedkey;
792113275Smike			if (p) {
793113275Smike				p->rn_dupedkey = tt->rn_dupedkey;
794167354Spjd				if (tt->rn_dupedkey)		/* parent */
795113275Smike					tt->rn_dupedkey->rn_parent = p;
796113275Smike								/* parent */
797113275Smike			} else log(LOG_ERR, "rn_delete: couldn't find us\n");
798113275Smike		}
799126004Spjd		t = tt + 1;
800126004Spjd		if  (t->rn_flags & RNF_ACTIVE) {
801126004Spjd#ifndef RN_DEBUG
802126004Spjd			*++x = *t;
803126004Spjd			p = t->rn_parent;
804126004Spjd#else
805126004Spjd			b = t->rn_info;
806126004Spjd			*++x = *t;
807126004Spjd			t->rn_info = b;
808126004Spjd			p = t->rn_parent;
809126004Spjd#endif
810126004Spjd			if (p->rn_left == t)
811126004Spjd				p->rn_left = x;
812			else
813				p->rn_right = x;
814			x->rn_left->rn_parent = x;
815			x->rn_right->rn_parent = x;
816		}
817		goto out;
818	}
819	if (t->rn_left == tt)
820		x = t->rn_right;
821	else
822		x = t->rn_left;
823	p = t->rn_parent;
824	if (p->rn_right == t)
825		p->rn_right = x;
826	else
827		p->rn_left = x;
828	x->rn_parent = p;
829	/*
830	 * Demote routes attached to us.
831	 */
832	if (t->rn_mklist) {
833		if (x->rn_bit >= 0) {
834			for (mp = &x->rn_mklist; (m = *mp);)
835				mp = &m->rm_mklist;
836			*mp = t->rn_mklist;
837		} else {
838			/* If there are any key,mask pairs in a sibling
839			   duped-key chain, some subset will appear sorted
840			   in the same order attached to our mklist */
841			for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
842				if (m == x->rn_mklist) {
843					struct radix_mask *mm = m->rm_mklist;
844					x->rn_mklist = 0;
845					if (--(m->rm_refs) < 0)
846						MKFree(m);
847					m = mm;
848				}
849			if (m)
850				log(LOG_ERR,
851				    "rn_delete: Orphaned Mask %p at %p\n",
852				    (void *)m, (void *)x);
853		}
854	}
855	/*
856	 * We may be holding an active internal node in the tree.
857	 */
858	x = tt + 1;
859	if (t != x) {
860#ifndef RN_DEBUG
861		*t = *x;
862#else
863		b = t->rn_info;
864		*t = *x;
865		t->rn_info = b;
866#endif
867		t->rn_left->rn_parent = t;
868		t->rn_right->rn_parent = t;
869		p = x->rn_parent;
870		if (p->rn_left == x)
871			p->rn_left = t;
872		else
873			p->rn_right = t;
874	}
875out:
876	tt->rn_flags &= ~RNF_ACTIVE;
877	tt[1].rn_flags &= ~RNF_ACTIVE;
878	return (tt);
879}
880
881/*
882 * This is the same as rn_walktree() except for the parameters and the
883 * exit.
884 */
885static int
886rn_walktree_from(h, a, m, f, w)
887	struct radix_node_head *h;
888	void *a, *m;
889	walktree_f_t *f;
890	void *w;
891{
892	int error;
893	struct radix_node *base, *next;
894	u_char *xa = (u_char *)a;
895	u_char *xm = (u_char *)m;
896	register struct radix_node *rn, *last = 0 /* shut up gcc */;
897	int stopping = 0;
898	int lastb;
899
900	/*
901	 * rn_search_m is sort-of-open-coded here.
902	 */
903	/* printf("about to search\n"); */
904	for (rn = h->rnh_treetop; rn->rn_bit >= 0; ) {
905		last = rn;
906		/* printf("rn_bit %d, rn_bmask %x, xm[rn_offset] %x\n",
907		       rn->rn_bit, rn->rn_bmask, xm[rn->rn_offset]); */
908		if (!(rn->rn_bmask & xm[rn->rn_offset])) {
909			break;
910		}
911		if (rn->rn_bmask & xa[rn->rn_offset]) {
912			rn = rn->rn_right;
913		} else {
914			rn = rn->rn_left;
915		}
916	}
917	/* printf("done searching\n"); */
918
919	/*
920	 * Two cases: either we stepped off the end of our mask,
921	 * in which case last == rn, or we reached a leaf, in which
922	 * case we want to start from the last node we looked at.
923	 * Either way, last is the node we want to start from.
924	 */
925	rn = last;
926	lastb = rn->rn_bit;
927
928	/* printf("rn %p, lastb %d\n", rn, lastb);*/
929
930	/*
931	 * This gets complicated because we may delete the node
932	 * while applying the function f to it, so we need to calculate
933	 * the successor node in advance.
934	 */
935	while (rn->rn_bit >= 0)
936		rn = rn->rn_left;
937
938	while (!stopping) {
939		/* printf("node %p (%d)\n", rn, rn->rn_bit); */
940		base = rn;
941		/* If at right child go back up, otherwise, go right */
942		while (rn->rn_parent->rn_right == rn
943		       && !(rn->rn_flags & RNF_ROOT)) {
944			rn = rn->rn_parent;
945
946			/* if went up beyond last, stop */
947			if (rn->rn_bit < lastb) {
948				stopping = 1;
949				/* printf("up too far\n"); */
950			}
951		}
952
953		/* Find the next *leaf* since next node might vanish, too */
954		for (rn = rn->rn_parent->rn_right; rn->rn_bit >= 0;)
955			rn = rn->rn_left;
956		next = rn;
957		/* Process leaves */
958		while ((rn = base) != 0) {
959			base = rn->rn_dupedkey;
960			/* printf("leaf %p\n", rn); */
961			if (!(rn->rn_flags & RNF_ROOT)
962			    && (error = (*f)(rn, w)))
963				return (error);
964		}
965		rn = next;
966
967		if (rn->rn_flags & RNF_ROOT) {
968			/* printf("root, stopping"); */
969			stopping = 1;
970		}
971
972	}
973	return 0;
974}
975
976static int
977rn_walktree(h, f, w)
978	struct radix_node_head *h;
979	walktree_f_t *f;
980	void *w;
981{
982	int error;
983	struct radix_node *base, *next;
984	register struct radix_node *rn = h->rnh_treetop;
985	/*
986	 * This gets complicated because we may delete the node
987	 * while applying the function f to it, so we need to calculate
988	 * the successor node in advance.
989	 */
990	/* First time through node, go left */
991	while (rn->rn_bit >= 0)
992		rn = rn->rn_left;
993	for (;;) {
994		base = rn;
995		/* If at right child go back up, otherwise, go right */
996		while (rn->rn_parent->rn_right == rn
997		       && (rn->rn_flags & RNF_ROOT) == 0)
998			rn = rn->rn_parent;
999		/* Find the next *leaf* since next node might vanish, too */
1000		for (rn = rn->rn_parent->rn_right; rn->rn_bit >= 0;)
1001			rn = rn->rn_left;
1002		next = rn;
1003		/* Process leaves */
1004		while ((rn = base)) {
1005			base = rn->rn_dupedkey;
1006			if (!(rn->rn_flags & RNF_ROOT)
1007			    && (error = (*f)(rn, w)))
1008				return (error);
1009		}
1010		rn = next;
1011		if (rn->rn_flags & RNF_ROOT)
1012			return (0);
1013	}
1014	/* NOTREACHED */
1015}
1016
1017int
1018rn_inithead(head, off)
1019	void **head;
1020	int off;
1021{
1022	register struct radix_node_head *rnh;
1023	register struct radix_node *t, *tt, *ttt;
1024	if (*head)
1025		return (1);
1026	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
1027	if (rnh == 0)
1028		return (0);
1029	Bzero(rnh, sizeof (*rnh));
1030	RADIX_NODE_HEAD_LOCK_INIT(rnh);
1031	*head = rnh;
1032	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
1033	ttt = rnh->rnh_nodes + 2;
1034	t->rn_right = ttt;
1035	t->rn_parent = t;
1036	tt = t->rn_left;
1037	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
1038	tt->rn_bit = -1 - off;
1039	*ttt = *tt;
1040	ttt->rn_key = rn_ones;
1041	rnh->rnh_addaddr = rn_addroute;
1042	rnh->rnh_deladdr = rn_delete;
1043	rnh->rnh_matchaddr = rn_match;
1044	rnh->rnh_lookup = rn_lookup;
1045	rnh->rnh_walktree = rn_walktree;
1046	rnh->rnh_walktree_from = rn_walktree_from;
1047	rnh->rnh_treetop = t;
1048	return (1);
1049}
1050
1051void
1052rn_init()
1053{
1054	char *cp, *cplim;
1055#ifdef _KERNEL
1056	struct domain *dom;
1057
1058	for (dom = domains; dom; dom = dom->dom_next)
1059		if (dom->dom_maxrtkey > max_keylen)
1060			max_keylen = dom->dom_maxrtkey;
1061#endif
1062	if (max_keylen == 0) {
1063		log(LOG_ERR,
1064		    "rn_init: radix functions require max_keylen be set\n");
1065		return;
1066	}
1067	R_Malloc(rn_zeros, char *, 3 * max_keylen);
1068	if (rn_zeros == NULL)
1069		panic("rn_init");
1070	Bzero(rn_zeros, 3 * max_keylen);
1071	rn_ones = cp = rn_zeros + max_keylen;
1072	addmask_key = cplim = rn_ones + max_keylen;
1073	while (cp < cplim)
1074		*cp++ = -1;
1075	if (rn_inithead((void **)&mask_rnhead, 0) == 0)
1076		panic("rn_init 2");
1077}
1078