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