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.2 (Berkeley) 11/15/93 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 __P((struct sockaddr *, 63 struct sockaddr *, struct sockaddr *)); 64 static void rtable_init __P((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 splx(s); 109 } 110 ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore); 111 } 112 113 /* 114 * Look up the route that matches the address given 115 * Or, at least try.. Create a cloned route if needed. 116 */ 117 struct rtentry * 118 rtalloc1(dst, report, ignflags) 119 register struct sockaddr *dst; 120 int report; 121 u_long ignflags; 122 { 123 register struct radix_node_head *rnh = rt_tables[dst->sa_family]; 124 register struct rtentry *rt; 125 register struct radix_node *rn; 126 struct rtentry *newrt = 0; 127 struct rt_addrinfo info; 128 u_long nflags; 129 int s = splnet(), err = 0, msgtype = RTM_MISS; 130 131 /* 132 * Look up the address in the table for that Address Family 133 */ 134 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) && 135 ((rn->rn_flags & RNF_ROOT) == 0)) { 136 /* 137 * If we find it and it's not the root node, then 138 * get a refernce on the rtentry associated. 139 */ 140 newrt = rt = (struct rtentry *)rn; 141 nflags = rt->rt_flags & ~ignflags; 142 if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) { 143 /* 144 * We are apparently adding (report = 0 in delete). 145 * If it requires that it be cloned, do so. 146 * (This implies it wasn't a HOST route.) 147 */ 148 err = rtrequest(RTM_RESOLVE, dst, SA(0), 149 SA(0), 0, &newrt); 150 if (err) { 151 /* 152 * If the cloning didn't succeed, maybe 153 * what we have will do. Return that. 154 */ 155 newrt = rt; 156 rt->rt_refcnt++; 157 goto miss; 158 } 159 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) { 160 /* 161 * If the new route specifies it be 162 * externally resolved, then go do that. 163 */ 164 msgtype = RTM_RESOLVE; 165 goto miss; 166 } 167 } else 168 rt->rt_refcnt++; 169 } else { 170 /* 171 * Either we hit the root or couldn't find any match, 172 * Which basically means 173 * "caint get there frm here" 174 */ 175 rtstat.rts_unreach++; 176 miss: if (report) { 177 /* 178 * If required, report the failure to the supervising 179 * Authorities. 180 * For a delete, this is not an error. (report == 0) 181 */ 182 bzero((caddr_t)&info, sizeof(info)); 183 info.rti_info[RTAX_DST] = dst; 184 rt_missmsg(msgtype, &info, 0, err); 185 } 186 } 187 splx(s); 188 return (newrt); 189 } 190 191 /* 192 * Remove a reference count from an rtentry. 193 * If the count gets low enough, take it out of the routing table 194 */ 195 void 196 rtfree(rt) 197 register struct rtentry *rt; 198 { 199 /* 200 * find the tree for that address family 201 */ 202 register struct radix_node_head *rnh = 203 rt_tables[rt_key(rt)->sa_family]; 204 register struct ifaddr *ifa; 205 206 if (rt == 0 || rnh == 0) 207 panic("rtfree"); 208 209 /* 210 * decrement the reference count by one and if it reaches 0, 211 * and there is a close function defined, call the close function 212 */ 213 rt->rt_refcnt--; 214 if(rnh->rnh_close && rt->rt_refcnt == 0) { 215 rnh->rnh_close((struct radix_node *)rt, rnh); 216 } 217 218 /* 219 * If we are no longer "up" (and ref == 0) 220 * then we can free the resources associated 221 * with the route. 222 */ 223 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) { 224 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 225 panic ("rtfree 2"); 226 /* 227 * the rtentry must have been removed from the routing table 228 * so it is represented in rttrash.. remove that now. 229 */ 230 rttrash--; 231 232 #ifdef DIAGNOSTIC 233 if (rt->rt_refcnt < 0) { 234 printf("rtfree: %p not freed (neg refs)\n", rt); 235 return; 236 } 237 #endif 238 239 /* 240 * release references on items we hold them on.. 241 * e.g other routes and ifaddrs. 242 */ 243 if((ifa = rt->rt_ifa)) 244 IFAFREE(ifa); 245 if (rt->rt_parent) { 246 RTFREE(rt->rt_parent); 247 } 248 249 /* 250 * The key is separatly alloc'd so free it (see rt_setgate()). 251 * This also frees the gateway, as they are always malloc'd 252 * together. 253 */ 254 Free(rt_key(rt)); 255 256 /* 257 * and the rtentry itself of course 258 */ 259 Free(rt); 260 } 261 } 262 263 void 264 ifafree(ifa) 265 register struct ifaddr *ifa; 266 { 267 if (ifa == NULL) 268 panic("ifafree"); 269 if (ifa->ifa_refcnt == 0) 270 free(ifa, M_IFADDR); 271 else 272 ifa->ifa_refcnt--; 273 } 274 275 /* 276 * Force a routing table entry to the specified 277 * destination to go through the given gateway. 278 * Normally called as a result of a routing redirect 279 * message from the network layer. 280 * 281 * N.B.: must be called at splnet 282 * 283 */ 284 void 285 rtredirect(dst, gateway, netmask, flags, src, rtp) 286 struct sockaddr *dst, *gateway, *netmask, *src; 287 int flags; 288 struct rtentry **rtp; 289 { 290 register struct rtentry *rt; 291 int error = 0; 292 short *stat = 0; 293 struct rt_addrinfo info; 294 struct ifaddr *ifa; 295 296 /* verify the gateway is directly reachable */ 297 if ((ifa = ifa_ifwithnet(gateway)) == 0) { 298 error = ENETUNREACH; 299 goto out; 300 } 301 rt = rtalloc1(dst, 0, 0UL); 302 /* 303 * If the redirect isn't from our current router for this dst, 304 * it's either old or wrong. If it redirects us to ourselves, 305 * we have a routing loop, perhaps as a result of an interface 306 * going down recently. 307 */ 308 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0) 309 if (!(flags & RTF_DONE) && rt && 310 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 311 error = EINVAL; 312 else if (ifa_ifwithaddr(gateway)) 313 error = EHOSTUNREACH; 314 if (error) 315 goto done; 316 /* 317 * Create a new entry if we just got back a wildcard entry 318 * or the the lookup failed. This is necessary for hosts 319 * which use routing redirects generated by smart gateways 320 * to dynamically build the routing tables. 321 */ 322 if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 323 goto create; 324 /* 325 * Don't listen to the redirect if it's 326 * for a route to an interface. 327 */ 328 if (rt->rt_flags & RTF_GATEWAY) { 329 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 330 /* 331 * Changing from route to net => route to host. 332 * Create new route, rather than smashing route to net. 333 */ 334 create: 335 flags |= RTF_GATEWAY | RTF_DYNAMIC; 336 error = rtrequest((int)RTM_ADD, dst, gateway, 337 netmask, flags, 338 (struct rtentry **)0); 339 stat = &rtstat.rts_dynamic; 340 } else { 341 /* 342 * Smash the current notion of the gateway to 343 * this destination. Should check about netmask!!! 344 */ 345 rt->rt_flags |= RTF_MODIFIED; 346 flags |= RTF_MODIFIED; 347 stat = &rtstat.rts_newgateway; 348 /* 349 * add the key and gateway (in one malloc'd chunk). 350 */ 351 rt_setgate(rt, rt_key(rt), gateway); 352 } 353 } else 354 error = EHOSTUNREACH; 355 done: 356 if (rt) { 357 if (rtp && !error) 358 *rtp = rt; 359 else 360 rtfree(rt); 361 } 362 out: 363 if (error) 364 rtstat.rts_badredirect++; 365 else if (stat != NULL) 366 (*stat)++; 367 bzero((caddr_t)&info, sizeof(info)); 368 info.rti_info[RTAX_DST] = dst; 369 info.rti_info[RTAX_GATEWAY] = gateway; 370 info.rti_info[RTAX_NETMASK] = netmask; 371 info.rti_info[RTAX_AUTHOR] = src; 372 rt_missmsg(RTM_REDIRECT, &info, flags, error); 373 } 374 375 /* 376 * Routing table ioctl interface. 377 */ 378 int 379 rtioctl(req, data, p) 380 int req; 381 caddr_t data; 382 struct proc *p; 383 { 384 #ifdef INET 385 /* Multicast goop, grrr... */ 386 #ifdef MROUTING 387 return mrt_ioctl(req, data); 388 #else 389 return mrt_ioctl(req, data, p); 390 #endif 391 #else /* INET */ 392 return ENXIO; 393 #endif /* INET */ 394 } 395 396 struct ifaddr * 397 ifa_ifwithroute(flags, dst, gateway) 398 int flags; 399 struct sockaddr *dst, *gateway; 400 { 401 register struct ifaddr *ifa; 402 if ((flags & RTF_GATEWAY) == 0) { 403 /* 404 * If we are adding a route to an interface, 405 * and the interface is a pt to pt link 406 * we should search for the destination 407 * as our clue to the interface. Otherwise 408 * we can use the local address. 409 */ 410 ifa = 0; 411 if (flags & RTF_HOST) { 412 ifa = ifa_ifwithdstaddr(dst); 413 } 414 if (ifa == 0) 415 ifa = ifa_ifwithaddr(gateway); 416 } else { 417 /* 418 * If we are adding a route to a remote net 419 * or host, the gateway may still be on the 420 * other end of a pt to pt link. 421 */ 422 ifa = ifa_ifwithdstaddr(gateway); 423 } 424 if (ifa == 0) 425 ifa = ifa_ifwithnet(gateway); 426 if (ifa == 0) { 427 struct rtentry *rt = rtalloc1(dst, 0, 0UL); 428 if (rt == 0) 429 return (0); 430 rt->rt_refcnt--; 431 if ((ifa = rt->rt_ifa) == 0) 432 return (0); 433 } 434 if (ifa->ifa_addr->sa_family != dst->sa_family) { 435 struct ifaddr *oifa = ifa; 436 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 437 if (ifa == 0) 438 ifa = oifa; 439 } 440 return (ifa); 441 } 442 443 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 444 445 static int rt_fixdelete __P((struct radix_node *, void *)); 446 static int rt_fixchange __P((struct radix_node *, void *)); 447 448 struct rtfc_arg { 449 struct rtentry *rt0; 450 struct radix_node_head *rnh; 451 }; 452 453 /* 454 * Do appropriate manipulations of a routing tree given 455 * all the bits of info needed 456 */ 457 int 458 rtrequest(req, dst, gateway, netmask, flags, ret_nrt) 459 int req, flags; 460 struct sockaddr *dst, *gateway, *netmask; 461 struct rtentry **ret_nrt; 462 { 463 int s = splnet(); int error = 0; 464 register struct rtentry *rt; 465 register struct radix_node *rn; 466 register struct radix_node_head *rnh; 467 struct ifaddr *ifa; 468 struct sockaddr *ndst; 469 #define senderr(x) { error = x ; goto bad; } 470 471 /* 472 * Find the correct routing tree to use for this Address Family 473 */ 474 if ((rnh = rt_tables[dst->sa_family]) == 0) 475 senderr(ESRCH); 476 /* 477 * If we are adding a host route then we don't want to put 478 * a netmask in the tree 479 */ 480 if (flags & RTF_HOST) 481 netmask = 0; 482 switch (req) { 483 case RTM_DELETE: 484 /* 485 * Remove the item from the tree and return it. 486 * Complain if it is not there and do no more processing. 487 */ 488 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0) 489 senderr(ESRCH); 490 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 491 panic ("rtrequest delete"); 492 rt = (struct rtentry *)rn; 493 494 /* 495 * Now search what's left of the subtree for any cloned 496 * routes which might have been formed from this node. 497 */ 498 if ((rt->rt_flags & RTF_PRCLONING) && netmask) { 499 rnh->rnh_walktree_from(rnh, dst, netmask, 500 rt_fixdelete, rt); 501 } 502 503 /* 504 * Remove any external references we may have. 505 * This might result in another rtentry being freed if 506 * we held its last reference. 507 */ 508 if (rt->rt_gwroute) { 509 rt = rt->rt_gwroute; 510 RTFREE(rt); 511 (rt = (struct rtentry *)rn)->rt_gwroute = 0; 512 } 513 514 /* 515 * NB: RTF_UP must be set during the search above, 516 * because we might delete the last ref, causing 517 * rt to get freed prematurely. 518 * eh? then why not just add a reference? 519 * I'm not sure how RTF_UP helps matters. (JRE) 520 */ 521 rt->rt_flags &= ~RTF_UP; 522 523 /* 524 * give the protocol a chance to keep things in sync. 525 */ 526 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 527 ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); 528 529 /* 530 * one more rtentry floating around that is not 531 * linked to the routing table. 532 */ 533 rttrash++; 534 535 /* 536 * If the caller wants it, then it can have it, 537 * but it's up to it to free the rtentry as we won't be 538 * doing it. 539 */ 540 if (ret_nrt) 541 *ret_nrt = rt; 542 else if (rt->rt_refcnt <= 0) { 543 rt->rt_refcnt++; /* make a 1->0 transition */ 544 rtfree(rt); 545 } 546 break; 547 548 case RTM_RESOLVE: 549 if (ret_nrt == 0 || (rt = *ret_nrt) == 0) 550 senderr(EINVAL); 551 ifa = rt->rt_ifa; 552 flags = rt->rt_flags & 553 ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC); 554 flags |= RTF_WASCLONED; 555 gateway = rt->rt_gateway; 556 if ((netmask = rt->rt_genmask) == 0) 557 flags |= RTF_HOST; 558 goto makeroute; 559 560 case RTM_ADD: 561 if ((flags & RTF_GATEWAY) && !gateway) 562 panic("rtrequest: GATEWAY but no gateway"); 563 564 if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0) 565 senderr(ENETUNREACH); 566 567 makeroute: 568 R_Malloc(rt, struct rtentry *, sizeof(*rt)); 569 if (rt == 0) 570 senderr(ENOBUFS); 571 Bzero(rt, sizeof(*rt)); 572 rt->rt_flags = RTF_UP | flags; 573 /* 574 * Add the gateway. Possibly re-malloc-ing the storage for it 575 * also add the rt_gwroute if possible. 576 */ 577 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 578 Free(rt); 579 senderr(error); 580 } 581 582 /* 583 * point to the (possibly newly malloc'd) dest address. 584 */ 585 ndst = rt_key(rt); 586 587 /* 588 * make sure it contains the value we want (masked if needed). 589 */ 590 if (netmask) { 591 rt_maskedcopy(dst, ndst, netmask); 592 } else 593 Bcopy(dst, ndst, dst->sa_len); 594 595 /* 596 * Note that we now have a reference to the ifa. 597 * This moved from below so that rnh->rnh_addaddr() can 598 * examine the ifa and ifa->ifa_ifp if it so desires. 599 */ 600 ifa->ifa_refcnt++; 601 rt->rt_ifa = ifa; 602 rt->rt_ifp = ifa->ifa_ifp; 603 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 604 605 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask, 606 rnh, rt->rt_nodes); 607 if (rn == 0) { 608 struct rtentry *rt2; 609 /* 610 * Uh-oh, we already have one of these in the tree. 611 * We do a special hack: if the route that's already 612 * there was generated by the protocol-cloning 613 * mechanism, then we just blow it away and retry 614 * the insertion of the new one. 615 */ 616 rt2 = rtalloc1(dst, 0, RTF_PRCLONING); 617 if (rt2 && rt2->rt_parent) { 618 rtrequest(RTM_DELETE, 619 (struct sockaddr *)rt_key(rt2), 620 rt2->rt_gateway, 621 rt_mask(rt2), rt2->rt_flags, 0); 622 RTFREE(rt2); 623 rn = rnh->rnh_addaddr((caddr_t)ndst, 624 (caddr_t)netmask, 625 rnh, rt->rt_nodes); 626 } else if (rt2) { 627 /* undo the extra ref we got */ 628 RTFREE(rt2); 629 } 630 } 631 632 /* 633 * If it still failed to go into the tree, 634 * then un-make it (this should be a function) 635 */ 636 if (rn == 0) { 637 if (rt->rt_gwroute) 638 rtfree(rt->rt_gwroute); 639 if (rt->rt_ifa) { 640 IFAFREE(rt->rt_ifa); 641 } 642 Free(rt_key(rt)); 643 Free(rt); 644 senderr(EEXIST); 645 } 646 647 rt->rt_parent = 0; 648 649 /* 650 * If we got here from RESOLVE, then we are cloning 651 * so clone the rest, and note that we 652 * are a clone (and increment the parent's references) 653 */ 654 if (req == RTM_RESOLVE) { 655 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 656 if ((*ret_nrt)->rt_flags & RTF_PRCLONING) { 657 rt->rt_parent = (*ret_nrt); 658 (*ret_nrt)->rt_refcnt++; 659 } 660 } 661 662 /* 663 * if this protocol has something to add to this then 664 * allow it to do that as well. 665 */ 666 if (ifa->ifa_rtrequest) 667 ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0)); 668 669 /* 670 * We repeat the same procedure from rt_setgate() here because 671 * it doesn't fire when we call it there because the node 672 * hasn't been added to the tree yet. 673 */ 674 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 675 struct rtfc_arg arg; 676 arg.rnh = rnh; 677 arg.rt0 = rt; 678 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 679 rt_fixchange, &arg); 680 } 681 682 /* 683 * actually return a resultant rtentry and 684 * give the caller a single reference. 685 */ 686 if (ret_nrt) { 687 *ret_nrt = rt; 688 rt->rt_refcnt++; 689 } 690 break; 691 } 692 bad: 693 splx(s); 694 return (error); 695 } 696 697 /* 698 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' 699 * (i.e., the routes related to it by the operation of cloning). This 700 * routine is iterated over all potential former-child-routes by way of 701 * rnh->rnh_walktree_from() above, and those that actually are children of 702 * the late parent (passed in as VP here) are themselves deleted. 703 */ 704 static int 705 rt_fixdelete(rn, vp) 706 struct radix_node *rn; 707 void *vp; 708 { 709 struct rtentry *rt = (struct rtentry *)rn; 710 struct rtentry *rt0 = vp; 711 712 if (rt->rt_parent == rt0 && !(rt->rt_flags & RTF_PINNED)) { 713 return rtrequest(RTM_DELETE, rt_key(rt), 714 (struct sockaddr *)0, rt_mask(rt), 715 rt->rt_flags, (struct rtentry **)0); 716 } 717 return 0; 718 } 719 720 /* 721 * This routine is called from rt_setgate() to do the analogous thing for 722 * adds and changes. There is the added complication in this case of a 723 * middle insert; i.e., insertion of a new network route between an older 724 * network route and (cloned) host routes. For this reason, a simple check 725 * of rt->rt_parent is insufficient; each candidate route must be tested 726 * against the (mask, value) of the new route (passed as before in vp) 727 * to see if the new route matches it. Unfortunately, this has the obnoxious 728 * property of also triggering for insertion /above/ a pre-existing network 729 * route and clones. Sigh. This may be fixed some day. 730 * 731 * XXX - it may be possible to do fixdelete() for changes and reserve this 732 * routine just for adds. I'm not sure why I thought it was necessary to do 733 * changes this way. 734 */ 735 #ifdef DEBUG 736 static int rtfcdebug = 0; 737 #endif 738 739 static int 740 rt_fixchange(rn, vp) 741 struct radix_node *rn; 742 void *vp; 743 { 744 struct rtentry *rt = (struct rtentry *)rn; 745 struct rtfc_arg *ap = vp; 746 struct rtentry *rt0 = ap->rt0; 747 struct radix_node_head *rnh = ap->rnh; 748 u_char *xk1, *xm1, *xk2; 749 int i, len; 750 751 #ifdef DEBUG 752 if (rtfcdebug) 753 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0); 754 #endif 755 756 if (!rt->rt_parent || (rt->rt_flags & RTF_PINNED)) { 757 #ifdef DEBUG 758 if(rtfcdebug) printf("no parent or pinned\n"); 759 #endif 760 return 0; 761 } 762 763 if (rt->rt_parent == rt0) { 764 #ifdef DEBUG 765 if(rtfcdebug) printf("parent match\n"); 766 #endif 767 return rtrequest(RTM_DELETE, rt_key(rt), 768 (struct sockaddr *)0, rt_mask(rt), 769 rt->rt_flags, (struct rtentry **)0); 770 } 771 772 /* 773 * There probably is a function somewhere which does this... 774 * if not, there should be. 775 */ 776 len = imin(((struct sockaddr *)rt_key(rt0))->sa_len, 777 ((struct sockaddr *)rt_key(rt))->sa_len); 778 779 xk1 = (u_char *)rt_key(rt0); 780 xm1 = (u_char *)rt_mask(rt0); 781 xk2 = (u_char *)rt_key(rt); 782 783 for (i = rnh->rnh_treetop->rn_off; i < len; i++) { 784 if ((xk2[i] & xm1[i]) != xk1[i]) { 785 #ifdef DEBUG 786 if(rtfcdebug) printf("no match\n"); 787 #endif 788 return 0; 789 } 790 } 791 792 /* 793 * OK, this node is a clone, and matches the node currently being 794 * changed/added under the node's mask. So, get rid of it. 795 */ 796 #ifdef DEBUG 797 if(rtfcdebug) printf("deleting\n"); 798 #endif 799 return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, 800 rt_mask(rt), rt->rt_flags, (struct rtentry **)0); 801 } 802 803 int 804 rt_setgate(rt0, dst, gate) 805 struct rtentry *rt0; 806 struct sockaddr *dst, *gate; 807 { 808 caddr_t new, old; 809 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len); 810 register struct rtentry *rt = rt0; 811 struct radix_node_head *rnh = rt_tables[dst->sa_family]; 812 813 /* 814 * A host route with the destination equal to the gateway 815 * will interfere with keeping LLINFO in the routing 816 * table, so disallow it. 817 */ 818 if (((rt0->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) == 819 (RTF_HOST|RTF_GATEWAY)) && 820 (dst->sa_len == gate->sa_len) && 821 (bcmp(dst, gate, dst->sa_len) == 0)) { 822 /* 823 * The route might already exist if this is an RTM_CHANGE 824 * or a routing redirect, so try to delete it. 825 */ 826 if (rt_key(rt0)) 827 rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt0), 828 rt0->rt_gateway, rt_mask(rt0), rt0->rt_flags, 0); 829 return EADDRNOTAVAIL; 830 } 831 832 /* 833 * Both dst and gateway are stored in the same malloc'd chunk 834 * (If I ever get my hands on....) 835 * if we need to malloc a new chunk, then keep the old one around 836 * till we don't need it any more. 837 */ 838 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) { 839 old = (caddr_t)rt_key(rt); 840 R_Malloc(new, caddr_t, dlen + glen); 841 if (new == 0) 842 return ENOBUFS; 843 rt->rt_nodes->rn_key = new; 844 } else { 845 /* 846 * otherwise just overwrite the old one 847 */ 848 new = rt->rt_nodes->rn_key; 849 old = 0; 850 } 851 852 /* 853 * copy the new gateway value into the memory chunk 854 */ 855 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen); 856 857 /* 858 * if we are replacing the chunk (or it's new) we need to 859 * replace the dst as well 860 */ 861 if (old) { 862 Bcopy(dst, new, dlen); 863 Free(old); 864 } 865 866 /* 867 * If there is already a gwroute, it's now almost definitly wrong 868 * so drop it. 869 */ 870 if (rt->rt_gwroute) { 871 rt = rt->rt_gwroute; RTFREE(rt); 872 rt = rt0; rt->rt_gwroute = 0; 873 } 874 /* 875 * Cloning loop avoidance: 876 * In the presence of protocol-cloning and bad configuration, 877 * it is possible to get stuck in bottomless mutual recursion 878 * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing 879 * protocol-cloning to operate for gateways (which is probably the 880 * correct choice anyway), and avoid the resulting reference loops 881 * by disallowing any route to run through itself as a gateway. 882 * This is obviously mandatory when we get rt->rt_output(). 883 */ 884 if (rt->rt_flags & RTF_GATEWAY) { 885 rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING); 886 if (rt->rt_gwroute == rt) { 887 RTFREE(rt->rt_gwroute); 888 rt->rt_gwroute = 0; 889 return EDQUOT; /* failure */ 890 } 891 } 892 893 /* 894 * This isn't going to do anything useful for host routes, so 895 * don't bother. Also make sure we have a reasonable mask 896 * (we don't yet have one during adds). 897 */ 898 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 899 struct rtfc_arg arg; 900 arg.rnh = rnh; 901 arg.rt0 = rt; 902 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 903 rt_fixchange, &arg); 904 } 905 906 return 0; 907 } 908 909 static void 910 rt_maskedcopy(src, dst, netmask) 911 struct sockaddr *src, *dst, *netmask; 912 { 913 register u_char *cp1 = (u_char *)src; 914 register u_char *cp2 = (u_char *)dst; 915 register u_char *cp3 = (u_char *)netmask; 916 u_char *cplim = cp2 + *cp3; 917 u_char *cplim2 = cp2 + *cp1; 918 919 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 920 cp3 += 2; 921 if (cplim > cplim2) 922 cplim = cplim2; 923 while (cp2 < cplim) 924 *cp2++ = *cp1++ & *cp3++; 925 if (cp2 < cplim2) 926 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 927 } 928 929 /* 930 * Set up a routing table entry, normally 931 * for an interface. 932 */ 933 int 934 rtinit(ifa, cmd, flags) 935 register struct ifaddr *ifa; 936 int cmd, flags; 937 { 938 register struct rtentry *rt; 939 register struct sockaddr *dst; 940 register struct sockaddr *deldst; 941 struct mbuf *m = 0; 942 struct rtentry *nrt = 0; 943 int error; 944 945 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr; 946 /* 947 * If it's a delete, check that if it exists, it's on the correct 948 * interface or we might scrub a route to another ifa which would 949 * be confusing at best and possibly worse. 950 */ 951 if (cmd == RTM_DELETE) { 952 /* 953 * It's a delete, so it should already exist.. 954 * If it's a net, mask off the host bits 955 * (Assuming we have a mask) 956 */ 957 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) { 958 m = m_get(M_DONTWAIT, MT_SONAME); 959 if (m == NULL) 960 return(ENOBUFS); 961 deldst = mtod(m, struct sockaddr *); 962 rt_maskedcopy(dst, deldst, ifa->ifa_netmask); 963 dst = deldst; 964 } 965 /* 966 * Get an rtentry that is in the routing tree and 967 * contains the correct info. (if this fails, can't get there). 968 * We set "report" to FALSE so that if it doesn't exist, 969 * it doesn't report an error or clone a route, etc. etc. 970 */ 971 rt = rtalloc1(dst, 0, 0UL); 972 if (rt) { 973 /* 974 * Ok so we found the rtentry. it has an extra reference 975 * for us at this stage. we won't need that so 976 * lop that off now. 977 */ 978 rt->rt_refcnt--; 979 if (rt->rt_ifa != ifa) { 980 /* 981 * If the interface in the rtentry doesn't match 982 * the interface we are using, then we don't 983 * want to delete it, so return an error. 984 * This seems to be the only point of 985 * this whole RTM_DELETE clause. 986 */ 987 if (m) 988 (void) m_free(m); 989 return (flags & RTF_HOST ? EHOSTUNREACH 990 : ENETUNREACH); 991 } 992 } 993 /* XXX */ 994 #if 0 995 else { 996 /* 997 * One would think that as we are deleting, and we know 998 * it doesn't exist, we could just return at this point 999 * with an "ELSE" clause, but apparently not.. 1000 */ 1001 return (flags & RTF_HOST ? EHOSTUNREACH 1002 : ENETUNREACH); 1003 } 1004 #endif 1005 } 1006 /* 1007 * Do the actual request 1008 */ 1009 error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask, 1010 flags | ifa->ifa_flags, &nrt); 1011 if (m) 1012 (void) m_free(m); 1013 /* 1014 * If we are deleting, and we found an entry, then 1015 * it's been removed from the tree.. now throw it away. 1016 */ 1017 if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) { 1018 /* 1019 * notify any listenning routing agents of the change 1020 */ 1021 rt_newaddrmsg(cmd, ifa, error, nrt); 1022 if (rt->rt_refcnt <= 0) { 1023 rt->rt_refcnt++; /* need a 1->0 transition to free */ 1024 rtfree(rt); 1025 } 1026 } 1027 1028 /* 1029 * We are adding, and we have a returned routing entry. 1030 * We need to sanity check the result. 1031 */ 1032 if (cmd == RTM_ADD && error == 0 && (rt = nrt)) { 1033 /* 1034 * We just wanted to add it.. we don't actually need a reference 1035 */ 1036 rt->rt_refcnt--; 1037 /* 1038 * If it came back with an unexpected interface, then it must 1039 * have already existed or something. (XXX) 1040 */ 1041 if (rt->rt_ifa != ifa) { 1042 if (!(rt->rt_ifa->ifa_ifp->if_flags & 1043 (IFF_POINTOPOINT|IFF_LOOPBACK))) 1044 printf("rtinit: wrong ifa (%p) was (%p)\n", 1045 ifa, rt->rt_ifa); 1046 /* 1047 * Ask that the protocol in question 1048 * remove anything it has associated with 1049 * this route and ifaddr. 1050 */ 1051 if (rt->rt_ifa->ifa_rtrequest) 1052 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); 1053 /* 1054 * Remove the reference to its ifaddr. 1055 */ 1056 IFAFREE(rt->rt_ifa); 1057 /* 1058 * And substitute in references to the ifaddr 1059 * we are adding. 1060 */ 1061 rt->rt_ifa = ifa; 1062 rt->rt_ifp = ifa->ifa_ifp; 1063 rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu; /*XXX*/ 1064 ifa->ifa_refcnt++; 1065 /* 1066 * Now ask the protocol to check if it needs 1067 * any special processing in its new form. 1068 */ 1069 if (ifa->ifa_rtrequest) 1070 ifa->ifa_rtrequest(RTM_ADD, rt, SA(0)); 1071 } 1072 /* 1073 * notify any listenning routing agents of the change 1074 */ 1075 rt_newaddrmsg(cmd, ifa, error, nrt); 1076 } 1077 return (error); 1078 } 1079 1080 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, route_init, 0); 1081