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