1 /* 2 * Copyright (c) 1980, 1986, 1991, 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95 34 * $FreeBSD$ 35 */ 36 37 #include "opt_inet.h" 38 #include "opt_mrouting.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/socket.h> 45 #include <sys/domain.h> 46 #include <sys/kernel.h> 47 48 #include <net/if.h> 49 #include <net/route.h> 50 51 #include <netinet/in.h> 52 #include <netinet/ip_mroute.h> 53 54 #define SA(p) ((struct sockaddr *)(p)) 55 56 struct route_cb route_cb; 57 static struct rtstat rtstat; 58 struct radix_node_head *rt_tables[AF_MAX+1]; 59 60 static int rttrash; /* routes not in table but not freed */ 61 62 static void rt_maskedcopy(struct sockaddr *, 63 struct sockaddr *, struct sockaddr *); 64 static void rtable_init(void **); 65 66 static void 67 rtable_init(table) 68 void **table; 69 { 70 struct domain *dom; 71 for (dom = domains; dom; dom = dom->dom_next) 72 if (dom->dom_rtattach) 73 dom->dom_rtattach(&table[dom->dom_family], 74 dom->dom_rtoffset); 75 } 76 77 void 78 route_init() 79 { 80 rn_init(); /* initialize all zeroes, all ones, mask table */ 81 rtable_init((void **)rt_tables); 82 } 83 84 /* 85 * Packet routing routines. 86 */ 87 void 88 rtalloc(ro) 89 register struct route *ro; 90 { 91 rtalloc_ign(ro, 0UL); 92 } 93 94 void 95 rtalloc_ign(ro, ignore) 96 register struct route *ro; 97 u_long ignore; 98 { 99 struct rtentry *rt; 100 int s; 101 102 if ((rt = ro->ro_rt) != NULL) { 103 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 104 return; 105 /* XXX - We are probably always at splnet here already. */ 106 s = splnet(); 107 RTFREE(rt); 108 ro->ro_rt = NULL; 109 splx(s); 110 } 111 ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore); 112 } 113 114 /* 115 * Look up the route that matches the address given 116 * Or, at least try.. Create a cloned route if needed. 117 */ 118 struct rtentry * 119 rtalloc1(dst, report, ignflags) 120 register struct sockaddr *dst; 121 int report; 122 u_long ignflags; 123 { 124 register struct radix_node_head *rnh = rt_tables[dst->sa_family]; 125 register struct rtentry *rt; 126 register struct radix_node *rn; 127 struct rtentry *newrt = 0; 128 struct rt_addrinfo info; 129 u_long nflags; 130 int s = splnet(), err = 0, msgtype = RTM_MISS; 131 132 /* 133 * Look up the address in the table for that Address Family 134 */ 135 if (rnh == NULL) { 136 rtstat.rts_unreach++; 137 goto miss2; 138 } 139 RADIX_NODE_HEAD_LOCK(rnh); 140 if ((rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) && 141 ((rn->rn_flags & RNF_ROOT) == 0)) { 142 /* 143 * If we find it and it's not the root node, then 144 * get a refernce on the rtentry associated. 145 */ 146 newrt = rt = (struct rtentry *)rn; 147 nflags = rt->rt_flags & ~ignflags; 148 if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) { 149 /* 150 * We are apparently adding (report = 0 in delete). 151 * If it requires that it be cloned, do so. 152 * (This implies it wasn't a HOST route.) 153 */ 154 err = rtrequest(RTM_RESOLVE, dst, SA(0), 155 SA(0), 0, &newrt); 156 if (err) { 157 /* 158 * If the cloning didn't succeed, maybe 159 * what we have will do. Return that. 160 */ 161 newrt = rt; 162 rt->rt_refcnt++; 163 goto miss; 164 } 165 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) { 166 /* 167 * If the new route specifies it be 168 * externally resolved, then go do that. 169 */ 170 msgtype = RTM_RESOLVE; 171 goto miss; 172 } 173 /* Inform listeners of the new route. */ 174 bzero(&info, sizeof(info)); 175 info.rti_info[RTAX_DST] = rt_key(rt); 176 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 177 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 178 if (rt->rt_ifp != NULL) { 179 info.rti_info[RTAX_IFP] = 180 TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr; 181 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 182 } 183 rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0); 184 } else 185 rt->rt_refcnt++; 186 RADIX_NODE_HEAD_UNLOCK(rnh); 187 } else { 188 /* 189 * Either we hit the root or couldn't find any match, 190 * Which basically means 191 * "caint get there frm here" 192 */ 193 rtstat.rts_unreach++; 194 miss: 195 RADIX_NODE_HEAD_UNLOCK(rnh); 196 miss2: if (report) { 197 /* 198 * If required, report the failure to the supervising 199 * Authorities. 200 * For a delete, this is not an error. (report == 0) 201 */ 202 bzero((caddr_t)&info, sizeof(info)); 203 info.rti_info[RTAX_DST] = dst; 204 rt_missmsg(msgtype, &info, 0, err); 205 } 206 } 207 splx(s); 208 return (newrt); 209 } 210 211 /* 212 * Remove a reference count from an rtentry. 213 * If the count gets low enough, take it out of the routing table 214 */ 215 void 216 rtfree(rt) 217 register struct rtentry *rt; 218 { 219 /* 220 * find the tree for that address family 221 */ 222 struct radix_node_head *rnh = rt_tables[rt_key(rt)->sa_family]; 223 224 if (rt == 0 || rnh == 0) 225 panic("rtfree"); 226 227 /* 228 * decrement the reference count by one and if it reaches 0, 229 * and there is a close function defined, call the close function 230 */ 231 rt->rt_refcnt--; 232 if (rnh->rnh_close && rt->rt_refcnt == 0) { 233 rnh->rnh_close((struct radix_node *)rt, rnh); 234 } 235 236 /* 237 * If we are no longer "up" (and ref == 0) 238 * then we can free the resources associated 239 * with the route. 240 */ 241 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) { 242 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 243 panic ("rtfree 2"); 244 /* 245 * the rtentry must have been removed from the routing table 246 * so it is represented in rttrash.. remove that now. 247 */ 248 rttrash--; 249 250 #ifdef DIAGNOSTIC 251 if (rt->rt_refcnt < 0) { 252 printf("rtfree: %p not freed (neg refs)\n", rt); 253 return; 254 } 255 #endif 256 257 /* 258 * release references on items we hold them on.. 259 * e.g other routes and ifaddrs. 260 */ 261 if (rt->rt_ifa) 262 IFAFREE(rt->rt_ifa); 263 if (rt->rt_parent) 264 RTFREE(rt->rt_parent); 265 266 /* 267 * The key is separatly alloc'd so free it (see rt_setgate()). 268 * This also frees the gateway, as they are always malloc'd 269 * together. 270 */ 271 Free(rt_key(rt)); 272 273 /* 274 * and the rtentry itself of course 275 */ 276 Free(rt); 277 } 278 } 279 280 /* compare two sockaddr structures */ 281 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 282 283 /* 284 * Force a routing table entry to the specified 285 * destination to go through the given gateway. 286 * Normally called as a result of a routing redirect 287 * message from the network layer. 288 * 289 * N.B.: must be called at splnet 290 * 291 */ 292 void 293 rtredirect(dst, gateway, netmask, flags, src, rtp) 294 struct sockaddr *dst, *gateway, *netmask, *src; 295 int flags; 296 struct rtentry **rtp; 297 { 298 struct rtentry *rt; 299 int error = 0; 300 short *stat = 0; 301 struct rt_addrinfo info; 302 struct ifaddr *ifa; 303 304 /* verify the gateway is directly reachable */ 305 if ((ifa = ifa_ifwithnet(gateway)) == 0) { 306 error = ENETUNREACH; 307 goto out; 308 } 309 rt = rtalloc1(dst, 0, 0UL); 310 /* 311 * If the redirect isn't from our current router for this dst, 312 * it's either old or wrong. If it redirects us to ourselves, 313 * we have a routing loop, perhaps as a result of an interface 314 * going down recently. 315 */ 316 if (!(flags & RTF_DONE) && rt && 317 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 318 error = EINVAL; 319 else if (ifa_ifwithaddr(gateway)) 320 error = EHOSTUNREACH; 321 if (error) 322 goto done; 323 /* 324 * Create a new entry if we just got back a wildcard entry 325 * or the the lookup failed. This is necessary for hosts 326 * which use routing redirects generated by smart gateways 327 * to dynamically build the routing tables. 328 */ 329 if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 330 goto create; 331 /* 332 * Don't listen to the redirect if it's 333 * for a route to an interface. 334 */ 335 if (rt->rt_flags & RTF_GATEWAY) { 336 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 337 /* 338 * Changing from route to net => route to host. 339 * Create new route, rather than smashing route to net. 340 */ 341 create: 342 if (rt) 343 rtfree(rt); 344 flags |= RTF_GATEWAY | RTF_DYNAMIC; 345 bzero((caddr_t)&info, sizeof(info)); 346 info.rti_info[RTAX_DST] = dst; 347 info.rti_info[RTAX_GATEWAY] = gateway; 348 info.rti_info[RTAX_NETMASK] = netmask; 349 info.rti_ifa = ifa; 350 info.rti_flags = flags; 351 rt = NULL; 352 error = rtrequest1(RTM_ADD, &info, &rt); 353 if (rt != NULL) 354 flags = rt->rt_flags; 355 stat = &rtstat.rts_dynamic; 356 } else { 357 /* 358 * Smash the current notion of the gateway to 359 * this destination. Should check about netmask!!! 360 */ 361 rt->rt_flags |= RTF_MODIFIED; 362 flags |= RTF_MODIFIED; 363 stat = &rtstat.rts_newgateway; 364 /* 365 * add the key and gateway (in one malloc'd chunk). 366 */ 367 rt_setgate(rt, rt_key(rt), gateway); 368 } 369 } else 370 error = EHOSTUNREACH; 371 done: 372 if (rt) { 373 if (rtp && !error) 374 *rtp = rt; 375 else 376 rtfree(rt); 377 } 378 out: 379 if (error) 380 rtstat.rts_badredirect++; 381 else if (stat != NULL) 382 (*stat)++; 383 bzero((caddr_t)&info, sizeof(info)); 384 info.rti_info[RTAX_DST] = dst; 385 info.rti_info[RTAX_GATEWAY] = gateway; 386 info.rti_info[RTAX_NETMASK] = netmask; 387 info.rti_info[RTAX_AUTHOR] = src; 388 rt_missmsg(RTM_REDIRECT, &info, flags, error); 389 } 390 391 /* 392 * Routing table ioctl interface. 393 */ 394 int 395 rtioctl(req, data) 396 u_long req; 397 caddr_t data; 398 { 399 #ifdef INET 400 /* Multicast goop, grrr... */ 401 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP; 402 #else /* INET */ 403 return ENXIO; 404 #endif /* INET */ 405 } 406 407 struct ifaddr * 408 ifa_ifwithroute(flags, dst, gateway) 409 int flags; 410 struct sockaddr *dst, *gateway; 411 { 412 register struct ifaddr *ifa; 413 if ((flags & RTF_GATEWAY) == 0) { 414 /* 415 * If we are adding a route to an interface, 416 * and the interface is a pt to pt link 417 * we should search for the destination 418 * as our clue to the interface. Otherwise 419 * we can use the local address. 420 */ 421 ifa = 0; 422 if (flags & RTF_HOST) { 423 ifa = ifa_ifwithdstaddr(dst); 424 } 425 if (ifa == 0) 426 ifa = ifa_ifwithaddr(gateway); 427 } else { 428 /* 429 * If we are adding a route to a remote net 430 * or host, the gateway may still be on the 431 * other end of a pt to pt link. 432 */ 433 ifa = ifa_ifwithdstaddr(gateway); 434 } 435 if (ifa == 0) 436 ifa = ifa_ifwithnet(gateway); 437 if (ifa == 0) { 438 struct rtentry *rt = rtalloc1(gateway, 0, 0UL); 439 if (rt == 0) 440 return (0); 441 --rt->rt_refcnt; 442 if ((ifa = rt->rt_ifa) == 0) 443 return (0); 444 } 445 if (ifa->ifa_addr->sa_family != dst->sa_family) { 446 struct ifaddr *oifa = ifa; 447 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 448 if (ifa == 0) 449 ifa = oifa; 450 } 451 return (ifa); 452 } 453 454 static int rt_fixdelete(struct radix_node *, void *); 455 static int rt_fixchange(struct radix_node *, void *); 456 457 struct rtfc_arg { 458 struct rtentry *rt0; 459 struct radix_node_head *rnh; 460 }; 461 462 /* 463 * Do appropriate manipulations of a routing tree given 464 * all the bits of info needed 465 */ 466 int 467 rtrequest(req, dst, gateway, netmask, flags, ret_nrt) 468 int req, flags; 469 struct sockaddr *dst, *gateway, *netmask; 470 struct rtentry **ret_nrt; 471 { 472 struct rt_addrinfo info; 473 474 bzero((caddr_t)&info, sizeof(info)); 475 info.rti_flags = flags; 476 info.rti_info[RTAX_DST] = dst; 477 info.rti_info[RTAX_GATEWAY] = gateway; 478 info.rti_info[RTAX_NETMASK] = netmask; 479 return rtrequest1(req, &info, ret_nrt); 480 } 481 482 /* 483 * These (questionable) definitions of apparent local variables apply 484 * to the next two functions. XXXXXX!!! 485 */ 486 #define dst info->rti_info[RTAX_DST] 487 #define gateway info->rti_info[RTAX_GATEWAY] 488 #define netmask info->rti_info[RTAX_NETMASK] 489 #define ifaaddr info->rti_info[RTAX_IFA] 490 #define ifpaddr info->rti_info[RTAX_IFP] 491 #define flags info->rti_flags 492 493 int 494 rt_getifa(info) 495 struct rt_addrinfo *info; 496 { 497 struct ifaddr *ifa; 498 int error = 0; 499 500 /* 501 * ifp may be specified by sockaddr_dl 502 * when protocol address is ambiguous. 503 */ 504 if (info->rti_ifp == NULL && ifpaddr != NULL && 505 ifpaddr->sa_family == AF_LINK && 506 (ifa = ifa_ifwithnet(ifpaddr)) != NULL) 507 info->rti_ifp = ifa->ifa_ifp; 508 if (info->rti_ifa == NULL && ifaaddr != NULL) 509 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 510 if (info->rti_ifa == NULL) { 511 struct sockaddr *sa; 512 513 sa = ifaaddr != NULL ? ifaaddr : 514 (gateway != NULL ? gateway : dst); 515 if (sa != NULL && info->rti_ifp != NULL) 516 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 517 else if (dst != NULL && gateway != NULL) 518 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway); 519 else if (sa != NULL) 520 info->rti_ifa = ifa_ifwithroute(flags, sa, sa); 521 } 522 if ((ifa = info->rti_ifa) != NULL) { 523 if (info->rti_ifp == NULL) 524 info->rti_ifp = ifa->ifa_ifp; 525 } else 526 error = ENETUNREACH; 527 return (error); 528 } 529 530 int 531 rtrequest1(req, info, ret_nrt) 532 int req; 533 struct rt_addrinfo *info; 534 struct rtentry **ret_nrt; 535 { 536 int s = splnet(); int error = 0; 537 register struct rtentry *rt; 538 register struct radix_node *rn; 539 register struct radix_node_head *rnh; 540 struct ifaddr *ifa; 541 struct sockaddr *ndst; 542 #define senderr(x) { error = x ; goto bad; } 543 544 /* 545 * Find the correct routing tree to use for this Address Family 546 */ 547 if ((rnh = rt_tables[dst->sa_family]) == 0) { 548 splx(s); 549 return (EAFNOSUPPORT); 550 } 551 RADIX_NODE_HEAD_LOCK(rnh); 552 /* 553 * If we are adding a host route then we don't want to put 554 * a netmask in the tree, nor do we want to clone it. 555 */ 556 if (flags & RTF_HOST) { 557 netmask = 0; 558 flags &= ~(RTF_CLONING | RTF_PRCLONING); 559 } 560 switch (req) { 561 case RTM_DELETE: 562 /* 563 * Remove the item from the tree and return it. 564 * Complain if it is not there and do no more processing. 565 */ 566 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0) 567 senderr(ESRCH); 568 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 569 panic ("rtrequest delete"); 570 rt = (struct rtentry *)rn; 571 rt->rt_refcnt++; 572 rt->rt_flags &= ~RTF_UP; 573 574 /* 575 * Now search what's left of the subtree for any cloned 576 * routes which might have been formed from this node. 577 */ 578 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) && 579 rt_mask(rt)) { 580 rnh->rnh_walktree_from(rnh, dst, rt_mask(rt), 581 rt_fixdelete, rt); 582 } 583 584 /* 585 * Remove any external references we may have. 586 * This might result in another rtentry being freed if 587 * we held its last reference. 588 */ 589 if (rt->rt_gwroute) { 590 rt = rt->rt_gwroute; 591 RTFREE(rt); 592 (rt = (struct rtentry *)rn)->rt_gwroute = 0; 593 } 594 595 /* 596 * give the protocol a chance to keep things in sync. 597 */ 598 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 599 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 600 601 /* 602 * one more rtentry floating around that is not 603 * linked to the routing table. 604 */ 605 rttrash++; 606 607 /* 608 * If the caller wants it, then it can have it, 609 * but it's up to it to free the rtentry as we won't be 610 * doing it. 611 */ 612 if (ret_nrt) 613 *ret_nrt = rt; 614 else 615 RTFREE(rt); 616 break; 617 618 case RTM_RESOLVE: 619 if (ret_nrt == 0 || (rt = *ret_nrt) == 0) 620 senderr(EINVAL); 621 ifa = rt->rt_ifa; 622 flags = rt->rt_flags & 623 ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC); 624 flags |= RTF_WASCLONED; 625 gateway = rt->rt_gateway; 626 if ((netmask = rt->rt_genmask) == 0) 627 flags |= RTF_HOST; 628 goto makeroute; 629 630 case RTM_ADD: 631 if ((flags & RTF_GATEWAY) && !gateway) 632 panic("rtrequest: GATEWAY but no gateway"); 633 634 if (info->rti_ifa == NULL && (error = rt_getifa(info))) 635 senderr(error); 636 ifa = info->rti_ifa; 637 638 makeroute: 639 R_Malloc(rt, struct rtentry *, sizeof(*rt)); 640 if (rt == 0) 641 senderr(ENOBUFS); 642 Bzero(rt, sizeof(*rt)); 643 rt->rt_flags = RTF_UP | flags; 644 /* 645 * Add the gateway. Possibly re-malloc-ing the storage for it 646 * also add the rt_gwroute if possible. 647 */ 648 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 649 Free(rt); 650 senderr(error); 651 } 652 653 /* 654 * point to the (possibly newly malloc'd) dest address. 655 */ 656 ndst = rt_key(rt); 657 658 /* 659 * make sure it contains the value we want (masked if needed). 660 */ 661 if (netmask) { 662 rt_maskedcopy(dst, ndst, netmask); 663 } else 664 Bcopy(dst, ndst, dst->sa_len); 665 666 /* 667 * Note that we now have a reference to the ifa. 668 * This moved from below so that rnh->rnh_addaddr() can 669 * examine the ifa and ifa->ifa_ifp if it so desires. 670 */ 671 IFAREF(ifa); 672 rt->rt_ifa = ifa; 673 rt->rt_ifp = ifa->ifa_ifp; 674 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 675 676 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask, 677 rnh, rt->rt_nodes); 678 if (rn == 0) { 679 struct rtentry *rt2; 680 /* 681 * Uh-oh, we already have one of these in the tree. 682 * We do a special hack: if the route that's already 683 * there was generated by the protocol-cloning 684 * mechanism, then we just blow it away and retry 685 * the insertion of the new one. 686 */ 687 rt2 = rtalloc1(dst, 0, RTF_PRCLONING); 688 if (rt2 && rt2->rt_parent) { 689 rtrequest(RTM_DELETE, 690 (struct sockaddr *)rt_key(rt2), 691 rt2->rt_gateway, 692 rt_mask(rt2), rt2->rt_flags, 0); 693 RTFREE(rt2); 694 rn = rnh->rnh_addaddr((caddr_t)ndst, 695 (caddr_t)netmask, 696 rnh, rt->rt_nodes); 697 } else if (rt2) { 698 /* undo the extra ref we got */ 699 RTFREE(rt2); 700 } 701 } 702 703 /* 704 * If it still failed to go into the tree, 705 * then un-make it (this should be a function) 706 */ 707 if (rn == 0) { 708 if (rt->rt_gwroute) 709 rtfree(rt->rt_gwroute); 710 if (rt->rt_ifa) { 711 IFAFREE(rt->rt_ifa); 712 } 713 Free(rt_key(rt)); 714 Free(rt); 715 senderr(EEXIST); 716 } 717 718 rt->rt_parent = 0; 719 720 /* 721 * If we got here from RESOLVE, then we are cloning 722 * so clone the rest, and note that we 723 * are a clone (and increment the parent's references) 724 */ 725 if (req == RTM_RESOLVE) { 726 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 727 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */ 728 if ((*ret_nrt)->rt_flags & (RTF_CLONING | RTF_PRCLONING)) { 729 rt->rt_parent = (*ret_nrt); 730 (*ret_nrt)->rt_refcnt++; 731 } 732 } 733 734 /* 735 * if this protocol has something to add to this then 736 * allow it to do that as well. 737 */ 738 if (ifa->ifa_rtrequest) 739 ifa->ifa_rtrequest(req, rt, info); 740 741 /* 742 * We repeat the same procedure from rt_setgate() here because 743 * it doesn't fire when we call it there because the node 744 * hasn't been added to the tree yet. 745 */ 746 if (req == RTM_ADD && 747 !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 748 struct rtfc_arg arg; 749 arg.rnh = rnh; 750 arg.rt0 = rt; 751 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 752 rt_fixchange, &arg); 753 } 754 755 /* 756 * actually return a resultant rtentry and 757 * give the caller a single reference. 758 */ 759 if (ret_nrt) { 760 *ret_nrt = rt; 761 rt->rt_refcnt++; 762 } 763 break; 764 default: 765 error = EOPNOTSUPP; 766 } 767 bad: 768 RADIX_NODE_HEAD_UNLOCK(rnh); 769 splx(s); 770 return (error); 771 #undef dst 772 #undef gateway 773 #undef netmask 774 #undef ifaaddr 775 #undef ifpaddr 776 #undef flags 777 } 778 779 /* 780 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' 781 * (i.e., the routes related to it by the operation of cloning). This 782 * routine is iterated over all potential former-child-routes by way of 783 * rnh->rnh_walktree_from() above, and those that actually are children of 784 * the late parent (passed in as VP here) are themselves deleted. 785 */ 786 static int 787 rt_fixdelete(rn, vp) 788 struct radix_node *rn; 789 void *vp; 790 { 791 struct rtentry *rt = (struct rtentry *)rn; 792 struct rtentry *rt0 = vp; 793 794 if (rt->rt_parent == rt0 && 795 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) { 796 return rtrequest(RTM_DELETE, rt_key(rt), 797 (struct sockaddr *)0, rt_mask(rt), 798 rt->rt_flags, (struct rtentry **)0); 799 } 800 return 0; 801 } 802 803 /* 804 * This routine is called from rt_setgate() to do the analogous thing for 805 * adds and changes. There is the added complication in this case of a 806 * middle insert; i.e., insertion of a new network route between an older 807 * network route and (cloned) host routes. For this reason, a simple check 808 * of rt->rt_parent is insufficient; each candidate route must be tested 809 * against the (mask, value) of the new route (passed as before in vp) 810 * to see if the new route matches it. 811 * 812 * XXX - it may be possible to do fixdelete() for changes and reserve this 813 * routine just for adds. I'm not sure why I thought it was necessary to do 814 * changes this way. 815 */ 816 #ifdef DEBUG 817 static int rtfcdebug = 0; 818 #endif 819 820 static int 821 rt_fixchange(rn, vp) 822 struct radix_node *rn; 823 void *vp; 824 { 825 struct rtentry *rt = (struct rtentry *)rn; 826 struct rtfc_arg *ap = vp; 827 struct rtentry *rt0 = ap->rt0; 828 struct radix_node_head *rnh = ap->rnh; 829 u_char *xk1, *xm1, *xk2, *xmp; 830 int i, len, mlen; 831 832 #ifdef DEBUG 833 if (rtfcdebug) 834 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0); 835 #endif 836 837 if (!rt->rt_parent || 838 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) { 839 #ifdef DEBUG 840 if(rtfcdebug) printf("no parent, pinned or cloning\n"); 841 #endif 842 return 0; 843 } 844 845 if (rt->rt_parent == rt0) { 846 #ifdef DEBUG 847 if(rtfcdebug) printf("parent match\n"); 848 #endif 849 return rtrequest(RTM_DELETE, rt_key(rt), 850 (struct sockaddr *)0, rt_mask(rt), 851 rt->rt_flags, (struct rtentry **)0); 852 } 853 854 /* 855 * There probably is a function somewhere which does this... 856 * if not, there should be. 857 */ 858 len = imin(((struct sockaddr *)rt_key(rt0))->sa_len, 859 ((struct sockaddr *)rt_key(rt))->sa_len); 860 861 xk1 = (u_char *)rt_key(rt0); 862 xm1 = (u_char *)rt_mask(rt0); 863 xk2 = (u_char *)rt_key(rt); 864 865 /* avoid applying a less specific route */ 866 xmp = (u_char *)rt_mask(rt->rt_parent); 867 mlen = ((struct sockaddr *)rt_key(rt->rt_parent))->sa_len; 868 if (mlen > ((struct sockaddr *)rt_key(rt0))->sa_len) { 869 #ifdef DEBUG 870 if (rtfcdebug) 871 printf("rt_fixchange: inserting a less " 872 "specific route\n"); 873 #endif 874 return 0; 875 } 876 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) { 877 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) { 878 #ifdef DEBUG 879 if (rtfcdebug) 880 printf("rt_fixchange: inserting a less " 881 "specific route\n"); 882 #endif 883 return 0; 884 } 885 } 886 887 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) { 888 if ((xk2[i] & xm1[i]) != xk1[i]) { 889 #ifdef DEBUG 890 if(rtfcdebug) printf("no match\n"); 891 #endif 892 return 0; 893 } 894 } 895 896 /* 897 * OK, this node is a clone, and matches the node currently being 898 * changed/added under the node's mask. So, get rid of it. 899 */ 900 #ifdef DEBUG 901 if(rtfcdebug) printf("deleting\n"); 902 #endif 903 return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, 904 rt_mask(rt), rt->rt_flags, (struct rtentry **)0); 905 } 906 907 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 908 909 int 910 rt_setgate(rt0, dst, gate) 911 struct rtentry *rt0; 912 struct sockaddr *dst, *gate; 913 { 914 caddr_t new, old; 915 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len); 916 register struct rtentry *rt = rt0; 917 struct radix_node_head *rnh = rt_tables[dst->sa_family]; 918 919 /* 920 * A host route with the destination equal to the gateway 921 * will interfere with keeping LLINFO in the routing 922 * table, so disallow it. 923 */ 924 if (((rt0->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) == 925 (RTF_HOST|RTF_GATEWAY)) && 926 (dst->sa_len == gate->sa_len) && 927 (bcmp(dst, gate, dst->sa_len) == 0)) { 928 /* 929 * The route might already exist if this is an RTM_CHANGE 930 * or a routing redirect, so try to delete it. 931 */ 932 if (rt_key(rt0)) 933 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt0), 934 rt0->rt_gateway, rt_mask(rt0), rt0->rt_flags, 0); 935 return EADDRNOTAVAIL; 936 } 937 938 /* 939 * Both dst and gateway are stored in the same malloc'd chunk 940 * (If I ever get my hands on....) 941 * if we need to malloc a new chunk, then keep the old one around 942 * till we don't need it any more. 943 */ 944 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) { 945 old = (caddr_t)rt_key(rt); 946 R_Malloc(new, caddr_t, dlen + glen); 947 if (new == 0) 948 return ENOBUFS; 949 rt->rt_nodes->rn_key = new; 950 } else { 951 /* 952 * otherwise just overwrite the old one 953 */ 954 new = rt->rt_nodes->rn_key; 955 old = 0; 956 } 957 958 /* 959 * copy the new gateway value into the memory chunk 960 */ 961 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen); 962 963 /* 964 * if we are replacing the chunk (or it's new) we need to 965 * replace the dst as well 966 */ 967 if (old) { 968 Bcopy(dst, new, dlen); 969 Free(old); 970 } 971 972 /* 973 * If there is already a gwroute, it's now almost definitly wrong 974 * so drop it. 975 */ 976 if (rt->rt_gwroute != NULL) { 977 RTFREE(rt->rt_gwroute); 978 rt->rt_gwroute = NULL; 979 } 980 /* 981 * Cloning loop avoidance: 982 * In the presence of protocol-cloning and bad configuration, 983 * it is possible to get stuck in bottomless mutual recursion 984 * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing 985 * protocol-cloning to operate for gateways (which is probably the 986 * correct choice anyway), and avoid the resulting reference loops 987 * by disallowing any route to run through itself as a gateway. 988 * This is obviously mandatory when we get rt->rt_output(). 989 */ 990 if (rt->rt_flags & RTF_GATEWAY) { 991 rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING); 992 if (rt->rt_gwroute == rt) { 993 RTFREE(rt->rt_gwroute); 994 rt->rt_gwroute = 0; 995 return EDQUOT; /* failure */ 996 } 997 } 998 999 /* 1000 * This isn't going to do anything useful for host routes, so 1001 * don't bother. Also make sure we have a reasonable mask 1002 * (we don't yet have one during adds). 1003 */ 1004 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 1005 struct rtfc_arg arg; 1006 arg.rnh = rnh; 1007 arg.rt0 = rt; 1008 RADIX_NODE_HEAD_LOCK(rnh); 1009 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 1010 rt_fixchange, &arg); 1011 RADIX_NODE_HEAD_UNLOCK(rnh); 1012 } 1013 1014 return 0; 1015 } 1016 1017 static void 1018 rt_maskedcopy(src, dst, netmask) 1019 struct sockaddr *src, *dst, *netmask; 1020 { 1021 register u_char *cp1 = (u_char *)src; 1022 register u_char *cp2 = (u_char *)dst; 1023 register u_char *cp3 = (u_char *)netmask; 1024 u_char *cplim = cp2 + *cp3; 1025 u_char *cplim2 = cp2 + *cp1; 1026 1027 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1028 cp3 += 2; 1029 if (cplim > cplim2) 1030 cplim = cplim2; 1031 while (cp2 < cplim) 1032 *cp2++ = *cp1++ & *cp3++; 1033 if (cp2 < cplim2) 1034 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1035 } 1036 1037 /* 1038 * Set up a routing table entry, normally 1039 * for an interface. 1040 */ 1041 int 1042 rtinit(ifa, cmd, flags) 1043 register struct ifaddr *ifa; 1044 int cmd, flags; 1045 { 1046 register struct rtentry *rt; 1047 register struct sockaddr *dst; 1048 register struct sockaddr *deldst; 1049 struct sockaddr *netmask; 1050 struct mbuf *m = 0; 1051 struct rtentry *nrt = 0; 1052 struct radix_node_head *rnh; 1053 struct radix_node *rn; 1054 int error; 1055 struct rt_addrinfo info; 1056 1057 if (flags & RTF_HOST) { 1058 dst = ifa->ifa_dstaddr; 1059 netmask = NULL; 1060 } else { 1061 dst = ifa->ifa_addr; 1062 netmask = ifa->ifa_netmask; 1063 } 1064 /* 1065 * If it's a delete, check that if it exists, it's on the correct 1066 * interface or we might scrub a route to another ifa which would 1067 * be confusing at best and possibly worse. 1068 */ 1069 if (cmd == RTM_DELETE) { 1070 /* 1071 * It's a delete, so it should already exist.. 1072 * If it's a net, mask off the host bits 1073 * (Assuming we have a mask) 1074 */ 1075 if (netmask != NULL) { 1076 m = m_get(M_DONTWAIT, MT_SONAME); 1077 if (m == NULL) 1078 return(ENOBUFS); 1079 deldst = mtod(m, struct sockaddr *); 1080 rt_maskedcopy(dst, deldst, netmask); 1081 dst = deldst; 1082 } 1083 /* 1084 * Look up an rtentry that is in the routing tree and 1085 * contains the correct info. 1086 */ 1087 if ((rnh = rt_tables[dst->sa_family]) == NULL) 1088 goto bad; 1089 RADIX_NODE_HEAD_LOCK(rnh); 1090 error = ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL || 1091 (rn->rn_flags & RNF_ROOT) || 1092 ((struct rtentry *)rn)->rt_ifa != ifa || 1093 !sa_equal(SA(rn->rn_key), dst)); 1094 RADIX_NODE_HEAD_UNLOCK(rnh); 1095 if (error) { 1096 bad: 1097 if (m) 1098 (void) m_free(m); 1099 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 1100 } 1101 } 1102 /* 1103 * Do the actual request 1104 */ 1105 bzero((caddr_t)&info, sizeof(info)); 1106 info.rti_ifa = ifa; 1107 info.rti_flags = flags | ifa->ifa_flags; 1108 info.rti_info[RTAX_DST] = dst; 1109 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1110 info.rti_info[RTAX_NETMASK] = netmask; 1111 error = rtrequest1(cmd, &info, &nrt); 1112 if (error == 0 && (rt = nrt) != NULL) { 1113 /* 1114 * notify any listening routing agents of the change 1115 */ 1116 rt_newaddrmsg(cmd, ifa, error, rt); 1117 if (cmd == RTM_DELETE) { 1118 /* 1119 * If we are deleting, and we found an entry, then 1120 * it's been removed from the tree.. now throw it away. 1121 */ 1122 RTFREE(rt); 1123 } else if (cmd == RTM_ADD) { 1124 /* 1125 * We just wanted to add it.. we don't actually 1126 * need a reference. 1127 */ 1128 rt->rt_refcnt--; 1129 } 1130 } 1131 if (m) 1132 (void) m_free(m); 1133 return (error); 1134 } 1135 1136 int 1137 rt_check(lrt, lrt0, dst) 1138 struct rtentry **lrt; 1139 struct rtentry **lrt0; 1140 struct sockaddr *dst; 1141 { 1142 struct rtentry *rt; 1143 struct rtentry *rt0; 1144 int error; 1145 1146 rt = *lrt; 1147 rt0 = *lrt0; 1148 error = 0; 1149 1150 rt = rt0; 1151 1152 if (rt != NULL) { 1153 if ((rt->rt_flags & RTF_UP) == 0) { 1154 rt0 = rt = rtalloc1(dst, 1, 0UL); 1155 if (rt0 != NULL) 1156 rt->rt_refcnt--; 1157 else 1158 senderr(EHOSTUNREACH); 1159 } 1160 if (rt->rt_flags & RTF_GATEWAY) { 1161 if (rt->rt_gwroute == NULL) 1162 goto lookup; 1163 1164 rt = rt->rt_gwroute; 1165 if ((rt->rt_flags & RTF_UP) == 0) { 1166 rtfree(rt); 1167 rt = rt0; 1168 lookup: 1169 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL); 1170 rt = rt->rt_gwroute; 1171 if (rt == NULL) 1172 senderr(EHOSTUNREACH); 1173 } 1174 } 1175 if (rt->rt_flags & RTF_REJECT) 1176 if (rt->rt_rmx.rmx_expire == 0 || 1177 time_second < rt->rt_rmx.rmx_expire) 1178 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 1179 } 1180 1181 bad: 1182 *lrt = rt; 1183 *lrt0 = rt0; 1184 return (error); 1185 } 1186 1187 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */ 1188 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0); 1189