1 /* $OpenBSD: rtable.c,v 1.80 2022/06/29 22:20:47 bluhm Exp $ */ 2 3 /* 4 * Copyright (c) 2014-2016 Martin Pieuchot 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #ifndef _KERNEL 20 #include "kern_compat.h" 21 #else 22 #include <sys/param.h> 23 #include <sys/systm.h> 24 #include <sys/socket.h> 25 #include <sys/malloc.h> 26 #include <sys/queue.h> 27 #include <sys/domain.h> 28 #include <sys/srp.h> 29 #endif 30 31 #include <net/rtable.h> 32 #include <net/route.h> 33 34 /* 35 * Structures used by rtable_get() to retrieve the corresponding 36 * routing table for a given pair of ``af'' and ``rtableid''. 37 * 38 * Note that once allocated routing table heads are never freed. 39 * This way we do not need to reference count them. 40 * 41 * afmap rtmap/dommp 42 * ----------- --------- ----- 43 * | 0 |--------> | 0 | 0 | ... | 0 | Array mapping rtableid (=index) 44 * ----------- --------- ----- to rdomain/loopback (=value). 45 * | AF_INET |. 46 * ----------- `. .---------. .---------. 47 * ... `----> | rtable0 | ... | rtableN | Array of pointers for 48 * ----------- '---------' '---------' IPv4 routing tables 49 * | AF_MPLS | indexed by ``rtableid''. 50 * ----------- 51 */ 52 struct srp *afmap; 53 uint8_t af2idx[AF_MAX+1]; /* To only allocate supported AF */ 54 uint8_t af2idx_max; 55 56 /* Array of routing table pointers. */ 57 struct rtmap { 58 unsigned int limit; 59 void **tbl; 60 }; 61 62 /* 63 * Array of rtableid -> rdomain mapping. 64 * 65 * Only used for the first index as described above. 66 */ 67 struct dommp { 68 unsigned int limit; 69 /* 70 * Array to get the routing domain and loopback interface related to 71 * a routing table. Format: 72 * 73 * 8 unused bits | 16 bits for loopback index | 8 bits for rdomain 74 */ 75 unsigned int *value; 76 }; 77 78 unsigned int rtmap_limit = 0; 79 80 void rtmap_init(void); 81 void rtmap_grow(unsigned int, sa_family_t); 82 void rtmap_dtor(void *, void *); 83 84 struct srp_gc rtmap_gc = SRP_GC_INITIALIZER(rtmap_dtor, NULL); 85 86 void rtable_init_backend(void); 87 void *rtable_alloc(unsigned int, unsigned int, unsigned int); 88 void *rtable_get(unsigned int, sa_family_t); 89 90 void 91 rtmap_init(void) 92 { 93 const struct domain *dp; 94 int i; 95 96 /* Start with a single table for every domain that requires it. */ 97 for (i = 0; (dp = domains[i]) != NULL; i++) { 98 if (dp->dom_rtoffset == 0) 99 continue; 100 101 rtmap_grow(1, dp->dom_family); 102 } 103 104 /* Initialize the rtableid->rdomain mapping table. */ 105 rtmap_grow(1, 0); 106 107 rtmap_limit = 1; 108 } 109 110 /* 111 * Grow the size of the array of routing table for AF ``af'' to ``nlimit''. 112 */ 113 void 114 rtmap_grow(unsigned int nlimit, sa_family_t af) 115 { 116 struct rtmap *map, *nmap; 117 int i; 118 119 KERNEL_ASSERT_LOCKED(); 120 121 KASSERT(nlimit > rtmap_limit); 122 123 nmap = malloc(sizeof(*nmap), M_RTABLE, M_WAITOK); 124 nmap->limit = nlimit; 125 nmap->tbl = mallocarray(nlimit, sizeof(*nmap[0].tbl), M_RTABLE, 126 M_WAITOK|M_ZERO); 127 128 map = srp_get_locked(&afmap[af2idx[af]]); 129 if (map != NULL) { 130 KASSERT(map->limit == rtmap_limit); 131 132 for (i = 0; i < map->limit; i++) 133 nmap->tbl[i] = map->tbl[i]; 134 } 135 136 srp_update_locked(&rtmap_gc, &afmap[af2idx[af]], nmap); 137 } 138 139 void 140 rtmap_dtor(void *null, void *xmap) 141 { 142 struct rtmap *map = xmap; 143 144 /* 145 * doesn't need to be serialized since this is the last reference 146 * to this map. there's nothing to race against. 147 */ 148 free(map->tbl, M_RTABLE, map->limit * sizeof(*map[0].tbl)); 149 free(map, M_RTABLE, sizeof(*map)); 150 } 151 152 void 153 rtable_init(void) 154 { 155 const struct domain *dp; 156 int i; 157 158 KASSERT(sizeof(struct rtmap) == sizeof(struct dommp)); 159 160 /* We use index 0 for the rtable/rdomain map. */ 161 af2idx_max = 1; 162 memset(af2idx, 0, sizeof(af2idx)); 163 164 /* 165 * Compute the maximum supported key length in case the routing 166 * table backend needs it. 167 */ 168 for (i = 0; (dp = domains[i]) != NULL; i++) { 169 if (dp->dom_rtoffset == 0) 170 continue; 171 172 af2idx[dp->dom_family] = af2idx_max++; 173 } 174 rtable_init_backend(); 175 176 /* 177 * Allocate AF-to-id table now that we now how many AFs this 178 * kernel supports. 179 */ 180 afmap = mallocarray(af2idx_max + 1, sizeof(*afmap), M_RTABLE, 181 M_WAITOK|M_ZERO); 182 183 rtmap_init(); 184 185 if (rtable_add(0) != 0) 186 panic("unable to create default routing table"); 187 188 rt_timer_init(); 189 } 190 191 int 192 rtable_add(unsigned int id) 193 { 194 const struct domain *dp; 195 void *tbl; 196 struct rtmap *map; 197 struct dommp *dmm; 198 sa_family_t af; 199 unsigned int off, alen; 200 int i, error = 0; 201 202 if (id > RT_TABLEID_MAX) 203 return (EINVAL); 204 205 KERNEL_LOCK(); 206 207 if (rtable_exists(id)) 208 goto out; 209 210 for (i = 0; (dp = domains[i]) != NULL; i++) { 211 if (dp->dom_rtoffset == 0) 212 continue; 213 214 af = dp->dom_family; 215 off = dp->dom_rtoffset; 216 alen = dp->dom_maxplen; 217 218 if (id >= rtmap_limit) 219 rtmap_grow(id + 1, af); 220 221 tbl = rtable_alloc(id, alen, off); 222 if (tbl == NULL) { 223 error = ENOMEM; 224 goto out; 225 } 226 227 map = srp_get_locked(&afmap[af2idx[af]]); 228 map->tbl[id] = tbl; 229 } 230 231 /* Reflect possible growth. */ 232 if (id >= rtmap_limit) { 233 rtmap_grow(id + 1, 0); 234 rtmap_limit = id + 1; 235 } 236 237 /* Use main rtable/rdomain by default. */ 238 dmm = srp_get_locked(&afmap[0]); 239 dmm->value[id] = 0; 240 out: 241 KERNEL_UNLOCK(); 242 243 return (error); 244 } 245 246 void * 247 rtable_get(unsigned int rtableid, sa_family_t af) 248 { 249 struct rtmap *map; 250 void *tbl = NULL; 251 struct srp_ref sr; 252 253 if (af >= nitems(af2idx) || af2idx[af] == 0) 254 return (NULL); 255 256 map = srp_enter(&sr, &afmap[af2idx[af]]); 257 if (rtableid < map->limit) 258 tbl = map->tbl[rtableid]; 259 srp_leave(&sr); 260 261 return (tbl); 262 } 263 264 int 265 rtable_exists(unsigned int rtableid) 266 { 267 const struct domain *dp; 268 void *tbl; 269 int i; 270 271 for (i = 0; (dp = domains[i]) != NULL; i++) { 272 if (dp->dom_rtoffset == 0) 273 continue; 274 275 tbl = rtable_get(rtableid, dp->dom_family); 276 if (tbl != NULL) 277 return (1); 278 } 279 280 return (0); 281 } 282 283 int 284 rtable_empty(unsigned int rtableid) 285 { 286 const struct domain *dp; 287 int i; 288 struct art_root *tbl; 289 290 for (i = 0; (dp = domains[i]) != NULL; i++) { 291 if (dp->dom_rtoffset == 0) 292 continue; 293 294 tbl = rtable_get(rtableid, dp->dom_family); 295 if (tbl == NULL) 296 continue; 297 if (tbl->ar_root.ref != NULL) 298 return (0); 299 } 300 301 return (1); 302 } 303 304 unsigned int 305 rtable_l2(unsigned int rtableid) 306 { 307 struct dommp *dmm; 308 unsigned int rdomain = 0; 309 struct srp_ref sr; 310 311 dmm = srp_enter(&sr, &afmap[0]); 312 if (rtableid < dmm->limit) 313 rdomain = (dmm->value[rtableid] & RT_TABLEID_MASK); 314 srp_leave(&sr); 315 316 return (rdomain); 317 } 318 319 unsigned int 320 rtable_loindex(unsigned int rtableid) 321 { 322 struct dommp *dmm; 323 unsigned int loifidx = 0; 324 struct srp_ref sr; 325 326 dmm = srp_enter(&sr, &afmap[0]); 327 if (rtableid < dmm->limit) 328 loifidx = (dmm->value[rtableid] >> RT_TABLEID_BITS); 329 srp_leave(&sr); 330 331 return (loifidx); 332 } 333 334 void 335 rtable_l2set(unsigned int rtableid, unsigned int rdomain, unsigned int loifidx) 336 { 337 struct dommp *dmm; 338 unsigned int value; 339 340 KERNEL_ASSERT_LOCKED(); 341 342 if (!rtable_exists(rtableid) || !rtable_exists(rdomain)) 343 return; 344 345 value = (rdomain & RT_TABLEID_MASK) | (loifidx << RT_TABLEID_BITS); 346 347 dmm = srp_get_locked(&afmap[0]); 348 dmm->value[rtableid] = value; 349 } 350 351 352 static inline uint8_t *satoaddr(struct art_root *, struct sockaddr *); 353 354 int an_match(struct art_node *, struct sockaddr *, int); 355 void rtentry_ref(void *, void *); 356 void rtentry_unref(void *, void *); 357 358 void rtable_mpath_insert(struct art_node *, struct rtentry *); 359 360 struct srpl_rc rt_rc = SRPL_RC_INITIALIZER(rtentry_ref, rtentry_unref, NULL); 361 362 void 363 rtable_init_backend(void) 364 { 365 art_init(); 366 } 367 368 void * 369 rtable_alloc(unsigned int rtableid, unsigned int alen, unsigned int off) 370 { 371 return (art_alloc(rtableid, alen, off)); 372 } 373 374 int 375 rtable_setsource(unsigned int rtableid, int af, struct sockaddr *src) 376 { 377 struct art_root *ar; 378 379 if ((ar = rtable_get(rtableid, af)) == NULL) 380 return (EAFNOSUPPORT); 381 382 ar->source = src; 383 384 return (0); 385 } 386 387 struct sockaddr * 388 rtable_getsource(unsigned int rtableid, int af) 389 { 390 struct art_root *ar; 391 392 ar = rtable_get(rtableid, af); 393 if (ar == NULL) 394 return (NULL); 395 396 return (ar->source); 397 } 398 399 void 400 rtable_clearsource(unsigned int rtableid, struct sockaddr *src) 401 { 402 struct sockaddr *addr; 403 404 addr = rtable_getsource(rtableid, src->sa_family); 405 if (addr && (addr->sa_len == src->sa_len)) { 406 if (memcmp(src, addr, addr->sa_len) == 0) { 407 rtable_setsource(rtableid, src->sa_family, NULL); 408 } 409 } 410 } 411 412 struct rtentry * 413 rtable_lookup(unsigned int rtableid, struct sockaddr *dst, 414 struct sockaddr *mask, struct sockaddr *gateway, uint8_t prio) 415 { 416 struct art_root *ar; 417 struct art_node *an; 418 struct rtentry *rt = NULL; 419 struct srp_ref sr, nsr; 420 uint8_t *addr; 421 int plen; 422 423 ar = rtable_get(rtableid, dst->sa_family); 424 if (ar == NULL) 425 return (NULL); 426 427 addr = satoaddr(ar, dst); 428 429 /* No need for a perfect match. */ 430 if (mask == NULL) { 431 an = art_match(ar, addr, &nsr); 432 if (an == NULL) 433 goto out; 434 } else { 435 plen = rtable_satoplen(dst->sa_family, mask); 436 if (plen == -1) 437 return (NULL); 438 439 an = art_lookup(ar, addr, plen, &nsr); 440 441 /* Make sure we've got a perfect match. */ 442 if (!an_match(an, dst, plen)) 443 goto out; 444 } 445 446 SRPL_FOREACH(rt, &sr, &an->an_rtlist, rt_next) { 447 if (prio != RTP_ANY && 448 (rt->rt_priority & RTP_MASK) != (prio & RTP_MASK)) 449 continue; 450 451 if (gateway == NULL) 452 break; 453 454 if (rt->rt_gateway->sa_len == gateway->sa_len && 455 memcmp(rt->rt_gateway, gateway, gateway->sa_len) == 0) 456 break; 457 } 458 if (rt != NULL) 459 rtref(rt); 460 461 SRPL_LEAVE(&sr); 462 out: 463 srp_leave(&nsr); 464 465 return (rt); 466 } 467 468 struct rtentry * 469 rtable_match(unsigned int rtableid, struct sockaddr *dst, uint32_t *src) 470 { 471 struct art_root *ar; 472 struct art_node *an; 473 struct rtentry *rt = NULL; 474 struct srp_ref sr, nsr; 475 uint8_t *addr; 476 int hash; 477 478 ar = rtable_get(rtableid, dst->sa_family); 479 if (ar == NULL) 480 return (NULL); 481 482 addr = satoaddr(ar, dst); 483 484 an = art_match(ar, addr, &nsr); 485 if (an == NULL) 486 goto out; 487 488 rt = SRPL_FIRST(&sr, &an->an_rtlist); 489 if (rt == NULL) { 490 SRPL_LEAVE(&sr); 491 goto out; 492 } 493 rtref(rt); 494 SRPL_LEAVE(&sr); 495 496 /* Gateway selection by Hash-Threshold (RFC 2992) */ 497 if ((hash = rt_hash(rt, dst, src)) != -1) { 498 struct rtentry *mrt; 499 int threshold, npaths = 0; 500 501 KASSERT(hash <= 0xffff); 502 503 SRPL_FOREACH(mrt, &sr, &an->an_rtlist, rt_next) { 504 /* Only count nexthops with the same priority. */ 505 if (mrt->rt_priority == rt->rt_priority) 506 npaths++; 507 } 508 SRPL_LEAVE(&sr); 509 510 threshold = (0xffff / npaths) + 1; 511 512 /* 513 * we have no protection against concurrent modification of the 514 * route list attached to the node, so we won't necessarily 515 * have the same number of routes. for most modifications, 516 * we'll pick a route that we wouldn't have if we only saw the 517 * list before or after the change. if we were going to use 518 * the last available route, but it got removed, we'll hit 519 * the end of the list and then pick the first route. 520 */ 521 522 mrt = SRPL_FIRST(&sr, &an->an_rtlist); 523 while (hash > threshold && mrt != NULL) { 524 if (mrt->rt_priority == rt->rt_priority) 525 hash -= threshold; 526 mrt = SRPL_FOLLOW(&sr, mrt, rt_next); 527 } 528 529 if (mrt != NULL) { 530 rtref(mrt); 531 rtfree(rt); 532 rt = mrt; 533 } 534 SRPL_LEAVE(&sr); 535 } 536 out: 537 srp_leave(&nsr); 538 return (rt); 539 } 540 541 int 542 rtable_insert(unsigned int rtableid, struct sockaddr *dst, 543 struct sockaddr *mask, struct sockaddr *gateway, uint8_t prio, 544 struct rtentry *rt) 545 { 546 struct rtentry *mrt; 547 struct srp_ref sr; 548 struct art_root *ar; 549 struct art_node *an, *prev; 550 uint8_t *addr; 551 int plen; 552 unsigned int rt_flags; 553 int error = 0; 554 555 ar = rtable_get(rtableid, dst->sa_family); 556 if (ar == NULL) 557 return (EAFNOSUPPORT); 558 559 addr = satoaddr(ar, dst); 560 plen = rtable_satoplen(dst->sa_family, mask); 561 if (plen == -1) 562 return (EINVAL); 563 564 rtref(rt); /* guarantee rtfree won't do anything during insert */ 565 rw_enter_write(&ar->ar_lock); 566 567 /* Do not permit exactly the same dst/mask/gw pair. */ 568 an = art_lookup(ar, addr, plen, &sr); 569 srp_leave(&sr); /* an can't go away while we have the lock */ 570 if (an_match(an, dst, plen)) { 571 struct rtentry *mrt; 572 int mpathok = ISSET(rt->rt_flags, RTF_MPATH); 573 574 SRPL_FOREACH_LOCKED(mrt, &an->an_rtlist, rt_next) { 575 if (prio != RTP_ANY && 576 (mrt->rt_priority & RTP_MASK) != (prio & RTP_MASK)) 577 continue; 578 579 if (!mpathok || 580 (mrt->rt_gateway->sa_len == gateway->sa_len && 581 memcmp(mrt->rt_gateway, gateway, 582 gateway->sa_len) == 0)) { 583 error = EEXIST; 584 goto leave; 585 } 586 } 587 } 588 589 an = art_get(dst, plen); 590 if (an == NULL) { 591 error = ENOBUFS; 592 goto leave; 593 } 594 595 /* prepare for immediate operation if insert succeeds */ 596 rt_flags = rt->rt_flags; 597 rt->rt_flags &= ~RTF_MPATH; 598 rt->rt_dest = dst; 599 rt->rt_plen = plen; 600 SRPL_INSERT_HEAD_LOCKED(&rt_rc, &an->an_rtlist, rt, rt_next); 601 602 prev = art_insert(ar, an, addr, plen); 603 if (prev != an) { 604 SRPL_REMOVE_LOCKED(&rt_rc, &an->an_rtlist, rt, rtentry, 605 rt_next); 606 rt->rt_flags = rt_flags; 607 art_put(an); 608 609 if (prev == NULL) { 610 error = ESRCH; 611 goto leave; 612 } 613 614 an = prev; 615 616 mrt = SRPL_FIRST_LOCKED(&an->an_rtlist); 617 KASSERT(mrt != NULL); 618 KASSERT((rt->rt_flags & RTF_MPATH) || mrt->rt_priority != prio); 619 620 /* 621 * An ART node with the same destination/netmask already 622 * exists, MPATH conflict must have been already checked. 623 */ 624 if (rt->rt_flags & RTF_MPATH) { 625 /* 626 * Only keep the RTF_MPATH flag if two routes have 627 * the same gateway. 628 */ 629 rt->rt_flags &= ~RTF_MPATH; 630 SRPL_FOREACH_LOCKED(mrt, &an->an_rtlist, rt_next) { 631 if (mrt->rt_priority == prio) { 632 mrt->rt_flags |= RTF_MPATH; 633 rt->rt_flags |= RTF_MPATH; 634 } 635 } 636 } 637 638 /* Put newly inserted entry at the right place. */ 639 rtable_mpath_insert(an, rt); 640 } 641 leave: 642 rw_exit_write(&ar->ar_lock); 643 rtfree(rt); 644 return (error); 645 } 646 647 int 648 rtable_delete(unsigned int rtableid, struct sockaddr *dst, 649 struct sockaddr *mask, struct rtentry *rt) 650 { 651 struct art_root *ar; 652 struct art_node *an; 653 struct srp_ref sr; 654 uint8_t *addr; 655 int plen; 656 struct rtentry *mrt; 657 int npaths = 0; 658 int error = 0; 659 660 ar = rtable_get(rtableid, dst->sa_family); 661 if (ar == NULL) 662 return (EAFNOSUPPORT); 663 664 addr = satoaddr(ar, dst); 665 plen = rtable_satoplen(dst->sa_family, mask); 666 if (plen == -1) 667 return (EINVAL); 668 669 rtref(rt); /* guarantee rtfree won't do anything under ar_lock */ 670 rw_enter_write(&ar->ar_lock); 671 an = art_lookup(ar, addr, plen, &sr); 672 srp_leave(&sr); /* an can't go away while we have the lock */ 673 674 /* Make sure we've got a perfect match. */ 675 if (!an_match(an, dst, plen)) { 676 error = ESRCH; 677 goto leave; 678 } 679 680 /* 681 * If other multipath route entries are still attached to 682 * this ART node we only have to unlink it. 683 */ 684 SRPL_FOREACH_LOCKED(mrt, &an->an_rtlist, rt_next) 685 npaths++; 686 687 if (npaths > 1) { 688 KASSERT(refcnt_read(&rt->rt_refcnt) >= 1); 689 SRPL_REMOVE_LOCKED(&rt_rc, &an->an_rtlist, rt, rtentry, 690 rt_next); 691 692 mrt = SRPL_FIRST_LOCKED(&an->an_rtlist); 693 if (npaths == 2) 694 mrt->rt_flags &= ~RTF_MPATH; 695 696 goto leave; 697 } 698 699 if (art_delete(ar, an, addr, plen) == NULL) 700 panic("art_delete failed to find node %p", an); 701 702 KASSERT(refcnt_read(&rt->rt_refcnt) >= 1); 703 SRPL_REMOVE_LOCKED(&rt_rc, &an->an_rtlist, rt, rtentry, rt_next); 704 art_put(an); 705 706 leave: 707 rw_exit_write(&ar->ar_lock); 708 rtfree(rt); 709 710 return (error); 711 } 712 713 struct rtable_walk_cookie { 714 int (*rwc_func)(struct rtentry *, void *, unsigned int); 715 void *rwc_arg; 716 struct rtentry **rwc_prt; 717 unsigned int rwc_rid; 718 }; 719 720 /* 721 * Helper for rtable_walk to keep the ART code free from any "struct rtentry". 722 */ 723 int 724 rtable_walk_helper(struct art_node *an, void *xrwc) 725 { 726 struct srp_ref sr; 727 struct rtable_walk_cookie *rwc = xrwc; 728 struct rtentry *rt; 729 int error = 0; 730 731 SRPL_FOREACH(rt, &sr, &an->an_rtlist, rt_next) { 732 error = (*rwc->rwc_func)(rt, rwc->rwc_arg, rwc->rwc_rid); 733 if (error != 0) 734 break; 735 } 736 if (rwc->rwc_prt != NULL && rt != NULL) { 737 rtref(rt); 738 *rwc->rwc_prt = rt; 739 } 740 SRPL_LEAVE(&sr); 741 742 return (error); 743 } 744 745 int 746 rtable_walk(unsigned int rtableid, sa_family_t af, struct rtentry **prt, 747 int (*func)(struct rtentry *, void *, unsigned int), void *arg) 748 { 749 struct art_root *ar; 750 struct rtable_walk_cookie rwc; 751 int error; 752 753 ar = rtable_get(rtableid, af); 754 if (ar == NULL) 755 return (EAFNOSUPPORT); 756 757 rwc.rwc_func = func; 758 rwc.rwc_arg = arg; 759 rwc.rwc_prt = prt; 760 rwc.rwc_rid = rtableid; 761 762 error = art_walk(ar, rtable_walk_helper, &rwc); 763 764 return (error); 765 } 766 767 struct rtentry * 768 rtable_iterate(struct rtentry *rt0) 769 { 770 struct rtentry *rt = NULL; 771 struct srp_ref sr; 772 773 rt = SRPL_NEXT(&sr, rt0, rt_next); 774 if (rt != NULL) 775 rtref(rt); 776 SRPL_LEAVE(&sr); 777 rtfree(rt0); 778 return (rt); 779 } 780 781 int 782 rtable_mpath_capable(unsigned int rtableid, sa_family_t af) 783 { 784 return (1); 785 } 786 787 int 788 rtable_mpath_reprio(unsigned int rtableid, struct sockaddr *dst, 789 int plen, uint8_t prio, struct rtentry *rt) 790 { 791 struct art_root *ar; 792 struct art_node *an; 793 struct srp_ref sr; 794 uint8_t *addr; 795 int error = 0; 796 797 ar = rtable_get(rtableid, dst->sa_family); 798 if (ar == NULL) 799 return (EAFNOSUPPORT); 800 801 addr = satoaddr(ar, dst); 802 803 rw_enter_write(&ar->ar_lock); 804 an = art_lookup(ar, addr, plen, &sr); 805 srp_leave(&sr); /* an can't go away while we have the lock */ 806 807 /* Make sure we've got a perfect match. */ 808 if (!an_match(an, dst, plen)) { 809 error = ESRCH; 810 } else if (SRPL_FIRST_LOCKED(&an->an_rtlist) == rt && 811 SRPL_NEXT_LOCKED(rt, rt_next) == NULL) { 812 /* 813 * If there's only one entry on the list do not go 814 * through an insert/remove cycle. This is done to 815 * guarantee that ``an->an_rtlist'' is never empty 816 * when a node is in the tree. 817 */ 818 rt->rt_priority = prio; 819 } else { 820 rtref(rt); /* keep rt alive in between remove and insert */ 821 SRPL_REMOVE_LOCKED(&rt_rc, &an->an_rtlist, 822 rt, rtentry, rt_next); 823 rt->rt_priority = prio; 824 rtable_mpath_insert(an, rt); 825 rtfree(rt); 826 error = EAGAIN; 827 } 828 rw_exit_write(&ar->ar_lock); 829 830 return (error); 831 } 832 833 void 834 rtable_mpath_insert(struct art_node *an, struct rtentry *rt) 835 { 836 struct rtentry *mrt, *prt = NULL; 837 uint8_t prio = rt->rt_priority; 838 839 if ((mrt = SRPL_FIRST_LOCKED(&an->an_rtlist)) == NULL) { 840 SRPL_INSERT_HEAD_LOCKED(&rt_rc, &an->an_rtlist, rt, rt_next); 841 return; 842 } 843 844 /* Iterate until we find the route to be placed after ``rt''. */ 845 while (mrt->rt_priority <= prio && SRPL_NEXT_LOCKED(mrt, rt_next)) { 846 prt = mrt; 847 mrt = SRPL_NEXT_LOCKED(mrt, rt_next); 848 } 849 850 if (mrt->rt_priority <= prio) { 851 SRPL_INSERT_AFTER_LOCKED(&rt_rc, mrt, rt, rt_next); 852 } else if (prt != NULL) { 853 SRPL_INSERT_AFTER_LOCKED(&rt_rc, prt, rt, rt_next); 854 } else { 855 SRPL_INSERT_HEAD_LOCKED(&rt_rc, &an->an_rtlist, rt, rt_next); 856 } 857 } 858 859 /* 860 * Returns 1 if ``an'' perfectly matches (``dst'', ``plen''), 0 otherwise. 861 */ 862 int 863 an_match(struct art_node *an, struct sockaddr *dst, int plen) 864 { 865 struct rtentry *rt; 866 struct srp_ref sr; 867 int match; 868 869 if (an == NULL || an->an_plen != plen) 870 return (0); 871 872 rt = SRPL_FIRST(&sr, &an->an_rtlist); 873 match = (memcmp(rt->rt_dest, dst, dst->sa_len) == 0); 874 SRPL_LEAVE(&sr); 875 876 return (match); 877 } 878 879 void 880 rtentry_ref(void *null, void *xrt) 881 { 882 struct rtentry *rt = xrt; 883 884 rtref(rt); 885 } 886 887 void 888 rtentry_unref(void *null, void *xrt) 889 { 890 struct rtentry *rt = xrt; 891 892 rtfree(rt); 893 } 894 895 /* 896 * Return a pointer to the address (key). This is an heritage from the 897 * BSD radix tree needed to skip the non-address fields from the flavor 898 * of "struct sockaddr" used by this routing table. 899 */ 900 static inline uint8_t * 901 satoaddr(struct art_root *at, struct sockaddr *sa) 902 { 903 return (((uint8_t *)sa) + at->ar_off); 904 } 905 906 /* 907 * Return the prefix length of a mask. 908 */ 909 int 910 rtable_satoplen(sa_family_t af, struct sockaddr *mask) 911 { 912 const struct domain *dp; 913 uint8_t *ap, *ep; 914 int mlen, plen = 0; 915 int i; 916 917 for (i = 0; (dp = domains[i]) != NULL; i++) { 918 if (dp->dom_rtoffset == 0) 919 continue; 920 921 if (af == dp->dom_family) 922 break; 923 } 924 if (dp == NULL) 925 return (-1); 926 927 /* Host route */ 928 if (mask == NULL) 929 return (dp->dom_maxplen); 930 931 mlen = mask->sa_len; 932 933 /* Default route */ 934 if (mlen == 0) 935 return (0); 936 937 ap = (uint8_t *)((uint8_t *)mask) + dp->dom_rtoffset; 938 ep = (uint8_t *)((uint8_t *)mask) + mlen; 939 if (ap > ep) 940 return (-1); 941 942 /* Trim trailing zeroes. */ 943 while (ap < ep && ep[-1] == 0) 944 ep--; 945 946 if (ap == ep) 947 return (0); 948 949 /* "Beauty" adapted from sbin/route/show.c ... */ 950 while (ap < ep) { 951 switch (*ap++) { 952 case 0xff: 953 plen += 8; 954 break; 955 case 0xfe: 956 plen += 7; 957 goto out; 958 case 0xfc: 959 plen += 6; 960 goto out; 961 case 0xf8: 962 plen += 5; 963 goto out; 964 case 0xf0: 965 plen += 4; 966 goto out; 967 case 0xe0: 968 plen += 3; 969 goto out; 970 case 0xc0: 971 plen += 2; 972 goto out; 973 case 0x80: 974 plen += 1; 975 goto out; 976 default: 977 /* Non contiguous mask. */ 978 return (-1); 979 } 980 } 981 982 out: 983 if (plen > dp->dom_maxplen || ap != ep) 984 return -1; 985 986 return (plen); 987 } 988