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