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