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