xref: /original-bsd/sys/net/radix.c (revision 3f14a87d)
1 /*
2  * Copyright (c) 1988, 1989  Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)radix.c	7.18 (Berkeley) 04/08/93
8  */
9 
10 /*
11  * Routines to build and maintain radix trees for routing lookups.
12  */
13 #ifndef RNF_NORMAL
14 #include <sys/param.h>
15 #include <sys/systm.h>
16 #include <sys/malloc.h>
17 #define	M_DONTWAIT M_NOWAIT
18 #ifdef	KERNEL
19 #include <sys/domain.h>
20 #endif
21 #endif
22 
23 #include <net/radix.h>
24 
25 int	max_keylen;
26 struct radix_mask *rn_mkfreelist;
27 struct radix_node_head *mask_rnhead;
28 static int gotOddMasks;
29 static char *maskedKey;
30 static char *rn_zeros, *rn_ones;
31 
32 #define rn_masktop (mask_rnhead->rnh_treetop)
33 #undef Bcmp
34 #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
35 /*
36  * The data structure for the keys is a radix tree with one way
37  * branching removed.  The index rn_b at an internal node n represents a bit
38  * position to be tested.  The tree is arranged so that all descendants
39  * of a node n have keys whose bits all agree up to position rn_b - 1.
40  * (We say the index of n is rn_b.)
41  *
42  * There is at least one descendant which has a one bit at position rn_b,
43  * and at least one with a zero there.
44  *
45  * A route is determined by a pair of key and mask.  We require that the
46  * bit-wise logical and of the key and mask to be the key.
47  * We define the index of a route to associated with the mask to be
48  * the first bit number in the mask where 0 occurs (with bit number 0
49  * representing the highest order bit).
50  *
51  * We say a mask is normal if every bit is 0, past the index of the mask.
52  * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
53  * and m is a normal mask, then the route applies to every descendant of n.
54  * If the index(m) < rn_b, this implies the trailing last few bits of k
55  * before bit b are all 0, (and hence consequently true of every descendant
56  * of n), so the route applies to all descendants of the node as well.
57  *
58  * The present version of the code makes no use of normal routes,
59  * but similar logic shows that a non-normal mask m such that
60  * index(m) <= index(n) could potentially apply to many children of n.
61  * Thus, for each non-host route, we attach its mask to a list at an internal
62  * node as high in the tree as we can go.
63  */
64 
65 struct radix_node *
66 rn_search(v, head)
67 	struct radix_node *head;
68 	register caddr_t v;
69 {
70 	register struct radix_node *x;
71 
72 	for (x = head; x->rn_b >= 0;) {
73 		if (x->rn_bmask & v[x->rn_off])
74 			x = x->rn_r;
75 		else
76 			x = x->rn_l;
77 	}
78 	return x;
79 };
80 
81 struct radix_node *
82 rn_search_m(v, head, m)
83 	struct radix_node *head;
84 	register caddr_t v, m;
85 {
86 	register struct radix_node *x;
87 
88 	for (x = head; x->rn_b >= 0;) {
89 		if ((x->rn_bmask & m[x->rn_off]) &&
90 		    (x->rn_bmask & v[x->rn_off]))
91 			x = x->rn_r;
92 		else
93 			x = x->rn_l;
94 	}
95 	return x;
96 };
97 
98 rn_refines(m, n)
99 	register caddr_t m, n;
100 {
101 	register caddr_t lim, lim2 = lim = n + *(u_char *)n;
102 	int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
103 	int masks_are_equal = 1;
104 
105 	if (longer > 0)
106 		lim -= longer;
107 	while (n < lim) {
108 		if (*n & ~(*m))
109 			return 0;
110 		if (*n++ != *m++)
111 			masks_are_equal = 0;
112 
113 	}
114 	while (n < lim2)
115 		if (*n++)
116 			return 0;
117 	if (masks_are_equal && (longer < 0))
118 		for (lim2 = m - longer; m < lim2; )
119 			if (*m++)
120 				return 1;
121 	return (!masks_are_equal);
122 }
123 
124 
125 static struct radix_node *
126 rn_match(v, head)
127 	struct radix_node_head *head;
128 	caddr_t v;
129 {
130 	register struct radix_node *t = head->rnh_treetop, *x;
131 	register caddr_t cp = v, cp2, cp3;
132 	caddr_t cplim, mstart;
133 	struct radix_node *saved_t, *top = t;
134 	int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
135 
136 	/*
137 	 * Open code rn_search(v, top) to avoid overhead of extra
138 	 * subroutine call.
139 	 */
140 	for (; t->rn_b >= 0; ) {
141 		if (t->rn_bmask & cp[t->rn_off])
142 			t = t->rn_r;
143 		else
144 			t = t->rn_l;
145 	}
146 	/*
147 	 * See if we match exactly as a host destination
148 	 */
149 	cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
150 	for (; cp < cplim; cp++, cp2++)
151 		if (*cp != *cp2)
152 			goto on1;
153 	/*
154 	 * This extra grot is in case we are explicitly asked
155 	 * to look up the default.  Ugh!
156 	 */
157 	if ((t->rn_flags & RNF_ROOT) && t->rn_dupedkey)
158 		t = t->rn_dupedkey;
159 	return t;
160 on1:
161 	matched_off = cp - v;
162 	saved_t = t;
163 	do {
164 	    if (t->rn_mask) {
165 		/*
166 		 * Even if we don't match exactly as a hosts;
167 		 * we may match if the leaf we wound up at is
168 		 * a route to a net.
169 		 */
170 		cp3 = matched_off + t->rn_mask;
171 		cp2 = matched_off + t->rn_key;
172 		for (; cp < cplim; cp++)
173 			if ((*cp2++ ^ *cp) & *cp3++)
174 				break;
175 		if (cp == cplim)
176 			return t;
177 		cp = matched_off + v;
178 	    }
179 	} while (t = t->rn_dupedkey);
180 	t = saved_t;
181 	/* start searching up the tree */
182 	do {
183 		register struct radix_mask *m;
184 		t = t->rn_p;
185 		if (m = t->rn_mklist) {
186 			/*
187 			 * After doing measurements here, it may
188 			 * turn out to be faster to open code
189 			 * rn_search_m here instead of always
190 			 * copying and masking.
191 			 */
192 			off = min(t->rn_off, matched_off);
193 			mstart = maskedKey + off;
194 			do {
195 				cp2 = mstart;
196 				cp3 = m->rm_mask + off;
197 				for (cp = v + off; cp < cplim;)
198 					*cp2++ =  *cp++ & *cp3++;
199 				x = rn_search(maskedKey, t);
200 				while (x && x->rn_mask != m->rm_mask)
201 					x = x->rn_dupedkey;
202 				if (x &&
203 				    (Bcmp(mstart, x->rn_key + off,
204 					vlen - off) == 0))
205 					    return x;
206 			} while (m = m->rm_mklist);
207 		}
208 	} while (t != top);
209 	return 0;
210 };
211 
212 #ifdef RN_DEBUG
213 int	rn_nodenum;
214 struct	radix_node *rn_clist;
215 int	rn_saveinfo;
216 int	rn_debug =  1;
217 #endif
218 
219 struct radix_node *
220 rn_newpair(v, b, nodes)
221 	caddr_t v;
222 	int b;
223 	struct radix_node nodes[2];
224 {
225 	register struct radix_node *tt = nodes, *t = tt + 1;
226 	t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7);
227 	t->rn_l = tt; t->rn_off = b >> 3;
228 	tt->rn_b = -1; tt->rn_key = v; tt->rn_p = t;
229 	tt->rn_flags = t->rn_flags = RNF_ACTIVE;
230 #ifdef RN_DEBUG
231 	tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
232 	tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
233 #endif
234 	return t;
235 }
236 
237 static struct radix_node *
238 rn_insert(v, head, dupentry, nodes)
239 	caddr_t v;
240 	struct radix_node_head *head;
241 	int *dupentry;
242 	struct radix_node nodes[2];
243 {
244 	struct radix_node *top = head->rnh_treetop;
245 	int head_off = top->rn_off, vlen = (int)*((u_char *)v);
246 	register struct radix_node *t = rn_search(v, top);
247 	register caddr_t cp = v + head_off;
248 	register int b;
249 	struct radix_node *tt;
250     	/*
251 	 *find first bit at which v and t->rn_key differ
252 	 */
253     {
254 	register caddr_t cp2 = t->rn_key + head_off;
255 	register int cmp_res;
256 	caddr_t cplim = v + vlen;
257 
258 	while (cp < cplim)
259 		if (*cp2++ != *cp++)
260 			goto on1;
261 	*dupentry = 1;
262 	return t;
263 on1:
264 	*dupentry = 0;
265 	cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
266 	for (b = (cp - v) << 3; cmp_res; b--)
267 		cmp_res >>= 1;
268     }
269     {
270 	register struct radix_node *p, *x = top;
271 	cp = v;
272 	do {
273 		p = x;
274 		if (cp[x->rn_off] & x->rn_bmask)
275 			x = x->rn_r;
276 		else x = x->rn_l;
277 	} while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
278 #ifdef RN_DEBUG
279 	if (rn_debug)
280 		printf("Going In:\n"), traverse(p);
281 #endif
282 	t = rn_newpair(v, b, nodes); tt = t->rn_l;
283 	if ((cp[p->rn_off] & p->rn_bmask) == 0)
284 		p->rn_l = t;
285 	else
286 		p->rn_r = t;
287 	x->rn_p = t; t->rn_p = p; /* frees x, p as temp vars below */
288 	if ((cp[t->rn_off] & t->rn_bmask) == 0) {
289 		t->rn_r = x;
290 	} else {
291 		t->rn_r = tt; t->rn_l = x;
292 	}
293 #ifdef RN_DEBUG
294 	if (rn_debug)
295 		printf("Coming out:\n"), traverse(p);
296 #endif
297     }
298 	return (tt);
299 }
300 
301 struct radix_node *
302 rn_addmask(netmask, search, skip)
303 	caddr_t netmask;
304 	int search, skip;
305 {
306 	register struct radix_node *x;
307 	register caddr_t cp, cplim;
308 	register int b, mlen, j;
309 	int maskduplicated;
310 
311 	mlen = *(u_char *)netmask;
312 	if (search) {
313 		x = rn_search(netmask, rn_masktop);
314 		mlen = *(u_char *)netmask;
315 		if (Bcmp(netmask, x->rn_key, mlen) == 0)
316 			return (x);
317 	}
318 	R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
319 	if (x == 0)
320 		return (0);
321 	Bzero(x, max_keylen + 2 * sizeof (*x));
322 	cp = (caddr_t)(x + 2);
323 	Bcopy(netmask, cp, mlen);
324 	netmask = cp;
325 	x = rn_insert(netmask, mask_rnhead, &maskduplicated, x);
326 	/*
327 	 * Calculate index of mask.
328 	 */
329 	cplim = netmask + mlen;
330 	for (cp = netmask + skip; cp < cplim; cp++)
331 		if (*(u_char *)cp != 0xff)
332 			break;
333 	b = (cp - netmask) << 3;
334 	if (cp != cplim) {
335 		if (*cp != 0) {
336 			gotOddMasks = 1;
337 			for (j = 0x80; j; b++, j >>= 1)
338 				if ((j & *cp) == 0)
339 					break;
340 		}
341 	}
342 	x->rn_b = -1 - b;
343 	return (x);
344 }
345 
346 static struct radix_node *
347 rn_addroute(v, netmask, head, treenodes)
348 	caddr_t v, netmask;
349 	struct radix_node_head *head;
350 	struct radix_node treenodes[2];
351 {
352 	register int j;
353 	register caddr_t cp;
354 	register struct radix_node *t, *x, *tt;
355 	struct radix_node *saved_tt, *top = head->rnh_treetop;
356 	short b = 0, b_leaf;
357 	int vlen = *(u_char *)v, mlen, keyduplicated;
358 	caddr_t cplim; unsigned char *maskp;
359 	struct radix_mask *m, **mp;
360 
361 	/*
362 	 * In dealing with non-contiguous masks, there may be
363 	 * many different routes which have the same mask.
364 	 * We will find it useful to have a unique pointer to
365 	 * the mask to speed avoiding duplicate references at
366 	 * nodes and possibly save time in calculating indices.
367 	 */
368 	if (netmask)  {
369 		x = rn_search(netmask, rn_masktop);
370 		mlen = *(u_char *)netmask;
371 		if (Bcmp(netmask, x->rn_key, mlen) != 0) {
372 			x = rn_addmask(netmask, 0, top->rn_off);
373 			if (x == 0)
374 				return (0);
375 		}
376 		netmask = x->rn_key;
377 		b = -1 - x->rn_b;
378 	}
379 	/*
380 	 * Deal with duplicated keys: attach node to previous instance
381 	 */
382 	saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
383 	if (keyduplicated) {
384 		do {
385 			if (tt->rn_mask == netmask)
386 				return (0);
387 			t = tt;
388 			if (netmask == 0 ||
389 			    (tt->rn_mask && rn_refines(netmask, tt->rn_mask)))
390 				break;
391 		} while (tt = tt->rn_dupedkey);
392 		/*
393 		 * If the mask is not duplicated, we wouldn't
394 		 * find it among possible duplicate key entries
395 		 * anyway, so the above test doesn't hurt.
396 		 *
397 		 * We sort the masks for a duplicated key the same way as
398 		 * in a masklist -- most specific to least specific.
399 		 * This may require the unfortunate nuisance of relocating
400 		 * the head of the list.
401 		 */
402 		if (tt && t == saved_tt) {
403 			struct	radix_node *xx = x;
404 			/* link in at head of list */
405 			(tt = treenodes)->rn_dupedkey = t;
406 			tt->rn_flags = t->rn_flags;
407 			tt->rn_p = x = t->rn_p;
408 			if (x->rn_l == t) x->rn_l = tt; else x->rn_r = tt;
409 			saved_tt = tt; x = xx;
410 		} else {
411 			(tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
412 			t->rn_dupedkey = tt;
413 		}
414 #ifdef RN_DEBUG
415 		t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
416 		tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
417 #endif
418 		t = saved_tt;
419 		tt->rn_key = (caddr_t) v;
420 		tt->rn_b = -1;
421 		tt->rn_flags = t->rn_flags & ~RNF_ROOT;
422 	}
423 	/*
424 	 * Put mask in tree.
425 	 */
426 	if (netmask) {
427 		tt->rn_mask = netmask;
428 		tt->rn_b = x->rn_b;
429 	}
430 	t = saved_tt->rn_p;
431 	b_leaf = -1 - t->rn_b;
432 	if (t->rn_r == saved_tt) x = t->rn_l; else x = t->rn_r;
433 	/* Promote general routes from below */
434 	if (x->rn_b < 0) {
435 		if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
436 			MKGet(m);
437 			if (m) {
438 				Bzero(m, sizeof *m);
439 				m->rm_b = x->rn_b;
440 				m->rm_mask = x->rn_mask;
441 				x->rn_mklist = t->rn_mklist = m;
442 			}
443 		}
444 	} else if (x->rn_mklist) {
445 		/*
446 		 * Skip over masks whose index is > that of new node
447 		 */
448 		for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist)
449 			if (m->rm_b >= b_leaf)
450 				break;
451 		t->rn_mklist = m; *mp = 0;
452 	}
453 	/* Add new route to highest possible ancestor's list */
454 	if ((netmask == 0) || (b > t->rn_b ))
455 		return tt; /* can't lift at all */
456 	b_leaf = tt->rn_b;
457 	do {
458 		x = t;
459 		t = t->rn_p;
460 	} while (b <= t->rn_b && x != top);
461 	/*
462 	 * Search through routes associated with node to
463 	 * insert new route according to index.
464 	 * For nodes of equal index, place more specific
465 	 * masks first.
466 	 */
467 	cplim = netmask + mlen;
468 	for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist) {
469 		if (m->rm_b < b_leaf)
470 			continue;
471 		if (m->rm_b > b_leaf)
472 			break;
473 		if (m->rm_mask == netmask) {
474 			m->rm_refs++;
475 			tt->rn_mklist = m;
476 			return tt;
477 		}
478 		if (rn_refines(netmask, m->rm_mask))
479 			break;
480 	}
481 	MKGet(m);
482 	if (m == 0) {
483 		printf("Mask for route not entered\n");
484 		return (tt);
485 	}
486 	Bzero(m, sizeof *m);
487 	m->rm_b = b_leaf;
488 	m->rm_mask = netmask;
489 	m->rm_mklist = *mp;
490 	*mp = m;
491 	tt->rn_mklist = m;
492 	return tt;
493 }
494 
495 static struct radix_node *
496 rn_delete(v, netmask, head)
497 	caddr_t v, netmask;
498 	struct radix_node_head *head;
499 {
500 	register struct radix_node *t, *p, *x = head->rnh_treetop;
501 	register struct radix_node *tt = rn_search(v, x);
502 	int b, head_off = x->rn_off, vlen =  * (u_char *) v;
503 	struct radix_mask *m, *saved_m, **mp;
504 	struct radix_node *dupedkey, *saved_tt = tt, *top = x;
505 
506 	if (tt == 0 ||
507 	    Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
508 		return (0);
509 	/*
510 	 * Delete our route from mask lists.
511 	 */
512 	if (dupedkey = tt->rn_dupedkey) {
513 		if (netmask)
514 			netmask = rn_search(netmask, rn_masktop)->rn_key;
515 		while (tt->rn_mask != netmask)
516 			if ((tt = tt->rn_dupedkey) == 0)
517 				return (0);
518 	}
519 	if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
520 		goto on1;
521 	if (m->rm_mask != tt->rn_mask) {
522 		printf("rn_delete: inconsistent annotation\n");
523 		goto on1;
524 	}
525 	if (--m->rm_refs >= 0)
526 		goto on1;
527 	b = -1 - tt->rn_b;
528 	t = saved_tt->rn_p;
529 	if (b > t->rn_b)
530 		goto on1; /* Wasn't lifted at all */
531 	do {
532 		x = t;
533 		t = t->rn_p;
534 	} while (b <= t->rn_b && x != top);
535 	for (mp = &x->rn_mklist; m = *mp; mp = &m->rm_mklist)
536 		if (m == saved_m) {
537 			*mp = m->rm_mklist;
538 			MKFree(m);
539 			break;
540 		}
541 	if (m == 0)
542 		printf("rn_delete: couldn't find our annotation\n");
543 on1:
544 	/*
545 	 * Eliminate us from tree
546 	 */
547 	if (tt->rn_flags & RNF_ROOT)
548 		return (0);
549 #ifdef RN_DEBUG
550 	/* Get us out of the creation list */
551 	for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
552 	if (t) t->rn_ybro = tt->rn_ybro;
553 #endif RN_DEBUG
554 	t = tt->rn_p;
555 	if (dupedkey) {
556 		if (tt == saved_tt) {
557 			x = dupedkey; x->rn_p = t;
558 			if (t->rn_l == tt) t->rn_l = x; else t->rn_r = x;
559 		} else {
560 			for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
561 				p = p->rn_dupedkey;
562 			if (p) p->rn_dupedkey = tt->rn_dupedkey;
563 			else printf("rn_delete: couldn't find us\n");
564 		}
565 		t = tt + 1;
566 		if  (t->rn_flags & RNF_ACTIVE) {
567 #ifndef RN_DEBUG
568 			*++x = *t; p = t->rn_p;
569 #else
570 			b = t->rn_info; *++x = *t; t->rn_info = b; p = t->rn_p;
571 #endif
572 			if (p->rn_l == t) p->rn_l = x; else p->rn_r = x;
573 			x->rn_l->rn_p = x; x->rn_r->rn_p = x;
574 		}
575 		goto out;
576 	}
577 	if (t->rn_l == tt) x = t->rn_r; else x = t->rn_l;
578 	p = t->rn_p;
579 	if (p->rn_r == t) p->rn_r = x; else p->rn_l = x;
580 	x->rn_p = p;
581 	/*
582 	 * Demote routes attached to us.
583 	 */
584 	if (t->rn_mklist) {
585 		if (x->rn_b >= 0) {
586 			for (mp = &x->rn_mklist; m = *mp;)
587 				mp = &m->rm_mklist;
588 			*mp = t->rn_mklist;
589 		} else {
590 			for (m = t->rn_mklist; m;) {
591 				struct radix_mask *mm = m->rm_mklist;
592 				if (m == x->rn_mklist && (--(m->rm_refs) < 0)) {
593 					x->rn_mklist = 0;
594 					MKFree(m);
595 				} else
596 					printf("%s %x at %x\n",
597 					    "rn_delete: Orphaned Mask", m, x);
598 				m = mm;
599 			}
600 		}
601 	}
602 	/*
603 	 * We may be holding an active internal node in the tree.
604 	 */
605 	x = tt + 1;
606 	if (t != x) {
607 #ifndef RN_DEBUG
608 		*t = *x;
609 #else
610 		b = t->rn_info; *t = *x; t->rn_info = b;
611 #endif
612 		t->rn_l->rn_p = t; t->rn_r->rn_p = t;
613 		p = x->rn_p;
614 		if (p->rn_l == x) p->rn_l = t; else p->rn_r = t;
615 	}
616 out:
617 	tt->rn_flags &= ~RNF_ACTIVE;
618 	tt[1].rn_flags &= ~RNF_ACTIVE;
619 	return (tt);
620 }
621 
622 rn_walktree(h, f, w)
623 	struct radix_node_head *h;
624 	register int (*f)();
625 	caddr_t  w;
626 {
627 	int error;
628 	struct radix_node *base, *next;
629 	register struct radix_node *rn = h->rnh_treetop;
630 	/*
631 	 * This gets complicated because we may delete the node
632 	 * while applying the function f to it, so we need to calculate
633 	 * the successor node in advance.
634 	 */
635 	/* First time through node, go left */
636 	while (rn->rn_b >= 0)
637 		rn = rn->rn_l;
638 	for (;;) {
639 		base = rn;
640 		/* If at right child go back up, otherwise, go right */
641 		while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
642 			rn = rn->rn_p;
643 		/* Find the next *leaf* since next node might vanish, too */
644 		for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
645 			rn = rn->rn_l;
646 		next = rn;
647 		/* Process leaves */
648 		while (rn = base) {
649 			base = rn->rn_dupedkey;
650 			if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
651 				return (error);
652 		}
653 		rn = next;
654 		if (rn->rn_flags & RNF_ROOT)
655 			return (0);
656 	}
657 }
658 
659 rn_inithead(head, off)
660 	void **head;
661 	int off;
662 {
663 	register struct radix_node_head *rnh;
664 	register struct radix_node *t, *tt, *ttt;
665 	if (*head)
666 		return (1);
667 	R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
668 	if (rnh == 0)
669 		return (0);
670 	Bzero(rnh, sizeof (*rnh));
671 	*head = rnh;
672 	t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
673 	ttt = rnh->rnh_nodes + 2;
674 	t->rn_r = ttt;
675 	t->rn_p = t;
676 	tt = t->rn_l;
677 	tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
678 	tt->rn_b = -1 - off;
679 	*ttt = *tt;
680 	ttt->rn_key = rn_ones;
681 	rnh->rnh_addaddr = rn_addroute;
682 	rnh->rnh_deladdr = rn_delete;
683 	rnh->rnh_matchaddr = rn_match;
684 	rnh->rnh_walktree = rn_walktree;
685 	rnh->rnh_treetop = t;
686 	return (1);
687 }
688 
689 rn_init()
690 {
691 	char *cp, *cplim;
692 #ifdef KERNEL
693 	struct domain *dom;
694 
695 	for (dom = domains; dom; dom = dom->dom_next)
696 		if (dom->dom_maxrtkey > max_keylen)
697 			max_keylen = dom->dom_maxrtkey;
698 #endif
699 	if (max_keylen == 0) {
700 		printf("rn_init: radix functions require max_keylen be set\n");
701 		return;
702 	}
703 	R_Malloc(rn_zeros, char *, 3 * max_keylen);
704 	if (rn_zeros == NULL)
705 		panic("rn_init");
706 	Bzero(rn_zeros, 3 * max_keylen);
707 	rn_ones = cp = rn_zeros + max_keylen;
708 	maskedKey = cplim = rn_ones + max_keylen;
709 	while (cp < cplim)
710 		*cp++ = -1;
711 	if (rn_inithead((void **)&mask_rnhead, 0) == 0)
712 		panic("rn_init 2");
713 }
714