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 * $Id: route.c,v 1.25 1995/07/29 11:41:02 bde Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/proc.h> 41 #include <sys/mbuf.h> 42 #include <sys/socket.h> 43 #include <sys/socketvar.h> 44 #include <sys/domain.h> 45 #include <sys/protosw.h> 46 #include <sys/ioctl.h> 47 48 #include <net/if.h> 49 #include <net/route.h> 50 #include <net/raw_cb.h> 51 52 #include <netinet/in.h> 53 #include <netinet/in_var.h> 54 #include <netinet/ip_mroute.h> 55 56 #define SA(p) ((struct sockaddr *)(p)) 57 58 struct route_cb route_cb; 59 struct rtstat rtstat; 60 struct radix_node_head *rt_tables[AF_MAX+1]; 61 62 int rttrash; /* routes not in table but not freed */ 63 struct sockaddr wildcard; /* zero valued cookie for wildcard searches */ 64 65 void 66 rtable_init(table) 67 void **table; 68 { 69 struct domain *dom; 70 for (dom = domains; dom; dom = dom->dom_next) 71 if (dom->dom_rtattach) 72 dom->dom_rtattach(&table[dom->dom_family], 73 dom->dom_rtoffset); 74 } 75 76 void 77 route_init() 78 { 79 rn_init(); /* initialize all zeroes, all ones, mask table */ 80 rtable_init((void **)rt_tables); 81 } 82 83 /* 84 * Packet routing routines. 85 */ 86 void 87 rtalloc(ro) 88 register struct route *ro; 89 { 90 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)) 91 return; /* XXX */ 92 ro->ro_rt = rtalloc1(&ro->ro_dst, 1, 0UL); 93 } 94 95 void 96 rtalloc_ign(ro, ignore) 97 register struct route *ro; 98 u_long ignore; 99 { 100 if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP)) 101 return; /* XXX */ 102 ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore); 103 } 104 105 struct rtentry * 106 rtalloc1(dst, report, ignflags) 107 register struct sockaddr *dst; 108 int report; 109 u_long ignflags; 110 { 111 register struct radix_node_head *rnh = rt_tables[dst->sa_family]; 112 register struct rtentry *rt; 113 register struct radix_node *rn; 114 struct rtentry *newrt = 0; 115 struct rt_addrinfo info; 116 u_long nflags; 117 int s = splnet(), err = 0, msgtype = RTM_MISS; 118 119 if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) && 120 ((rn->rn_flags & RNF_ROOT) == 0)) { 121 newrt = rt = (struct rtentry *)rn; 122 nflags = rt->rt_flags & ~ignflags; 123 if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) { 124 err = rtrequest(RTM_RESOLVE, dst, SA(0), 125 SA(0), 0, &newrt); 126 if (err) { 127 newrt = rt; 128 rt->rt_refcnt++; 129 goto miss; 130 } 131 if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) { 132 msgtype = RTM_RESOLVE; 133 goto miss; 134 } 135 } else 136 rt->rt_refcnt++; 137 } else { 138 rtstat.rts_unreach++; 139 miss: if (report) { 140 bzero((caddr_t)&info, sizeof(info)); 141 info.rti_info[RTAX_DST] = dst; 142 rt_missmsg(msgtype, &info, 0, err); 143 } 144 } 145 splx(s); 146 return (newrt); 147 } 148 149 void 150 rtfree(rt) 151 register struct rtentry *rt; 152 { 153 register struct radix_node_head *rnh = 154 rt_tables[rt_key(rt)->sa_family]; 155 register struct ifaddr *ifa; 156 157 if (rt == 0 || rnh == 0) 158 panic("rtfree"); 159 rt->rt_refcnt--; 160 if(rnh->rnh_close && rt->rt_refcnt == 0) { 161 rnh->rnh_close((struct radix_node *)rt, rnh); 162 } 163 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) { 164 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 165 panic ("rtfree 2"); 166 rttrash--; 167 if (rt->rt_refcnt < 0) { 168 printf("rtfree: %p not freed (neg refs)\n", rt); 169 return; 170 } 171 ifa = rt->rt_ifa; 172 IFAFREE(ifa); 173 if (rt->rt_parent) { 174 RTFREE(rt->rt_parent); 175 } 176 Free(rt_key(rt)); 177 Free(rt); 178 } 179 } 180 181 void 182 ifafree(ifa) 183 register struct ifaddr *ifa; 184 { 185 if (ifa == NULL) 186 panic("ifafree"); 187 if (ifa->ifa_refcnt == 0) 188 free(ifa, M_IFADDR); 189 else 190 ifa->ifa_refcnt--; 191 } 192 193 /* 194 * Force a routing table entry to the specified 195 * destination to go through the given gateway. 196 * Normally called as a result of a routing redirect 197 * message from the network layer. 198 * 199 * N.B.: must be called at splnet 200 * 201 */ 202 void 203 rtredirect(dst, gateway, netmask, flags, src, rtp) 204 struct sockaddr *dst, *gateway, *netmask, *src; 205 int flags; 206 struct rtentry **rtp; 207 { 208 register struct rtentry *rt; 209 int error = 0; 210 short *stat = 0; 211 struct rt_addrinfo info; 212 struct ifaddr *ifa; 213 214 /* verify the gateway is directly reachable */ 215 if ((ifa = ifa_ifwithnet(gateway)) == 0) { 216 error = ENETUNREACH; 217 goto out; 218 } 219 rt = rtalloc1(dst, 0, 0UL); 220 /* 221 * If the redirect isn't from our current router for this dst, 222 * it's either old or wrong. If it redirects us to ourselves, 223 * we have a routing loop, perhaps as a result of an interface 224 * going down recently. 225 */ 226 #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0) 227 if (!(flags & RTF_DONE) && rt && 228 (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 229 error = EINVAL; 230 else if (ifa_ifwithaddr(gateway)) 231 error = EHOSTUNREACH; 232 if (error) 233 goto done; 234 /* 235 * Create a new entry if we just got back a wildcard entry 236 * or the the lookup failed. This is necessary for hosts 237 * which use routing redirects generated by smart gateways 238 * to dynamically build the routing tables. 239 */ 240 if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 241 goto create; 242 /* 243 * Don't listen to the redirect if it's 244 * for a route to an interface. 245 */ 246 if (rt->rt_flags & RTF_GATEWAY) { 247 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 248 /* 249 * Changing from route to net => route to host. 250 * Create new route, rather than smashing route to net. 251 */ 252 create: 253 flags |= RTF_GATEWAY | RTF_DYNAMIC; 254 error = rtrequest((int)RTM_ADD, dst, gateway, 255 netmask, flags, 256 (struct rtentry **)0); 257 stat = &rtstat.rts_dynamic; 258 } else { 259 /* 260 * Smash the current notion of the gateway to 261 * this destination. Should check about netmask!!! 262 */ 263 rt->rt_flags |= RTF_MODIFIED; 264 flags |= RTF_MODIFIED; 265 stat = &rtstat.rts_newgateway; 266 rt_setgate(rt, rt_key(rt), gateway); 267 } 268 } else 269 error = EHOSTUNREACH; 270 done: 271 if (rt) { 272 if (rtp && !error) 273 *rtp = rt; 274 else 275 rtfree(rt); 276 } 277 out: 278 if (error) 279 rtstat.rts_badredirect++; 280 else if (stat != NULL) 281 (*stat)++; 282 bzero((caddr_t)&info, sizeof(info)); 283 info.rti_info[RTAX_DST] = dst; 284 info.rti_info[RTAX_GATEWAY] = gateway; 285 info.rti_info[RTAX_NETMASK] = netmask; 286 info.rti_info[RTAX_AUTHOR] = src; 287 rt_missmsg(RTM_REDIRECT, &info, flags, error); 288 } 289 290 /* 291 * Routing table ioctl interface. 292 */ 293 int 294 rtioctl(req, data, p) 295 int req; 296 caddr_t data; 297 struct proc *p; 298 { 299 #ifdef INET 300 /* Multicast goop, grrr... */ 301 return mrt_ioctl(req, data, p); 302 #else /* INET */ 303 return ENXIO; 304 #endif /* INET */ 305 } 306 307 struct ifaddr * 308 ifa_ifwithroute(flags, dst, gateway) 309 int flags; 310 struct sockaddr *dst, *gateway; 311 { 312 register struct ifaddr *ifa; 313 if ((flags & RTF_GATEWAY) == 0) { 314 /* 315 * If we are adding a route to an interface, 316 * and the interface is a pt to pt link 317 * we should search for the destination 318 * as our clue to the interface. Otherwise 319 * we can use the local address. 320 */ 321 ifa = 0; 322 if (flags & RTF_HOST) { 323 ifa = ifa_ifwithdstaddr(dst); 324 } 325 if (ifa == 0) 326 ifa = ifa_ifwithaddr(gateway); 327 } else { 328 /* 329 * If we are adding a route to a remote net 330 * or host, the gateway may still be on the 331 * other end of a pt to pt link. 332 */ 333 ifa = ifa_ifwithdstaddr(gateway); 334 } 335 if (ifa == 0) 336 ifa = ifa_ifwithnet(gateway); 337 if (ifa == 0) { 338 struct rtentry *rt = rtalloc1(dst, 0, 0UL); 339 if (rt == 0) 340 return (0); 341 rt->rt_refcnt--; 342 if ((ifa = rt->rt_ifa) == 0) 343 return (0); 344 } 345 if (ifa->ifa_addr->sa_family != dst->sa_family) { 346 struct ifaddr *oifa = ifa; 347 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 348 if (ifa == 0) 349 ifa = oifa; 350 } 351 return (ifa); 352 } 353 354 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 355 356 static int rt_fixdelete(struct radix_node *, void *); 357 static int rt_fixchange(struct radix_node *, void *); 358 359 struct rtfc_arg { 360 struct rtentry *rt0; 361 struct radix_node_head *rnh; 362 }; 363 364 int 365 rtrequest(req, dst, gateway, netmask, flags, ret_nrt) 366 int req, flags; 367 struct sockaddr *dst, *gateway, *netmask; 368 struct rtentry **ret_nrt; 369 { 370 int s = splnet(); int error = 0; 371 register struct rtentry *rt; 372 register struct radix_node *rn; 373 register struct radix_node_head *rnh; 374 struct ifaddr *ifa; 375 struct sockaddr *ndst; 376 u_long prflags = 0UL; 377 #define senderr(x) { error = x ; goto bad; } 378 379 if ((rnh = rt_tables[dst->sa_family]) == 0) 380 senderr(ESRCH); 381 if (flags & RTF_HOST) 382 netmask = 0; 383 switch (req) { 384 case RTM_DELETE: 385 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0) 386 senderr(ESRCH); 387 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 388 panic ("rtrequest delete"); 389 rt = (struct rtentry *)rn; 390 391 /* 392 * Now search what's left of the subtree for any cloned 393 * routes which might have been formed from this node. 394 */ 395 if ((rt->rt_flags & RTF_PRCLONING) && netmask) { 396 rnh->rnh_walktree_from(rnh, dst, netmask, 397 rt_fixdelete, rt); 398 } 399 400 /* 401 * NB: RTF_UP must be set during the search above, 402 * because we might delete the last ref, causing 403 * rt to get freed prematurely. 404 */ 405 rt->rt_flags &= ~RTF_UP; 406 407 if (rt->rt_gwroute) { 408 rt = rt->rt_gwroute; RTFREE(rt); 409 (rt = (struct rtentry *)rn)->rt_gwroute = 0; 410 } 411 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 412 ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); 413 rttrash++; 414 if (ret_nrt) 415 *ret_nrt = rt; 416 else if (rt->rt_refcnt <= 0) { 417 rt->rt_refcnt++; 418 rtfree(rt); 419 } 420 break; 421 422 case RTM_RESOLVE: 423 if (ret_nrt == 0 || (rt = *ret_nrt) == 0) 424 senderr(EINVAL); 425 ifa = rt->rt_ifa; 426 flags = rt->rt_flags & 427 ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC); 428 flags |= RTF_WASCLONED; 429 gateway = rt->rt_gateway; 430 if ((netmask = rt->rt_genmask) == 0) 431 flags |= RTF_HOST; 432 goto makeroute; 433 434 case RTM_ADD: 435 if ((flags & RTF_GATEWAY) && !gateway) 436 panic("rtrequest: GATEWAY but no gateway"); 437 438 if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0) 439 senderr(ENETUNREACH); 440 441 makeroute: 442 R_Malloc(rt, struct rtentry *, sizeof(*rt)); 443 if (rt == 0) 444 senderr(ENOBUFS); 445 Bzero(rt, sizeof(*rt)); 446 rt->rt_flags = RTF_UP | flags; 447 if (rt_setgate(rt, dst, gateway)) { 448 Free(rt); 449 senderr(ENOBUFS); 450 } 451 ndst = rt_key(rt); 452 if (netmask) { 453 rt_maskedcopy(dst, ndst, netmask); 454 } else 455 Bcopy(dst, ndst, dst->sa_len); 456 457 /* 458 * This moved from below so that rnh->rnh_addaddr() can 459 * examine the ifa and ifp if it so desires. 460 */ 461 ifa->ifa_refcnt++; 462 rt->rt_ifa = ifa; 463 rt->rt_ifp = ifa->ifa_ifp; 464 465 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask, 466 rnh, rt->rt_nodes); 467 if (rn == 0) { 468 struct rtentry *rt2; 469 /* 470 * Uh-oh, we already have one of these in the tree. 471 * We do a special hack: if the route that's already 472 * there was generated by the protocol-cloning 473 * mechanism, then we just blow it away and retry 474 * the insertion of the new one. 475 */ 476 rt2 = rtalloc1(dst, 0, RTF_PRCLONING); 477 if (rt2 && rt2->rt_parent) { 478 rtrequest(RTM_DELETE, 479 (struct sockaddr *)rt_key(rt2), 480 rt2->rt_gateway, 481 rt_mask(rt2), rt2->rt_flags, 0); 482 RTFREE(rt2); 483 rn = rnh->rnh_addaddr((caddr_t)ndst, 484 (caddr_t)netmask, 485 rnh, rt->rt_nodes); 486 } 487 } 488 489 if (rn == 0) { 490 if (rt->rt_gwroute) 491 rtfree(rt->rt_gwroute); 492 if (rt->rt_ifa) { 493 IFAFREE(rt->rt_ifa); 494 } 495 Free(rt_key(rt)); 496 Free(rt); 497 senderr(EEXIST); 498 } 499 rt->rt_parent = 0; 500 501 if (req == RTM_RESOLVE) { 502 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 503 if ((*ret_nrt)->rt_flags & RTF_PRCLONING) { 504 rt->rt_parent = (*ret_nrt); 505 (*ret_nrt)->rt_refcnt++; 506 } 507 } 508 if (ifa->ifa_rtrequest) 509 ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0)); 510 /* 511 * We repeat the same procedure from rt_setgate() here because 512 * it doesn't fire when we call it there because the node 513 * hasn't been added to the tree yet. 514 */ 515 if (!(rt->rt_flags & RTF_HOST)) { 516 struct rtfc_arg arg; 517 arg.rnh = rnh; 518 arg.rt0 = rt; 519 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 520 rt_fixchange, &arg); 521 } 522 523 if (ret_nrt) { 524 *ret_nrt = rt; 525 rt->rt_refcnt++; 526 } 527 break; 528 } 529 bad: 530 splx(s); 531 return (error); 532 } 533 534 /* 535 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' 536 * (i.e., the routes related to it by the operation of cloning). This 537 * routine is iterated over all potential former-child-routes by way of 538 * rnh->rnh_walktree_from() above, and those that actually are children of 539 * the late parent (passed in as VP here) are themselves deleted. 540 */ 541 static int 542 rt_fixdelete(struct radix_node *rn, void *vp) 543 { 544 struct rtentry *rt = (struct rtentry *)rn; 545 struct rtentry *rt0 = vp; 546 547 if (rt->rt_parent == rt0 && !(rt->rt_flags & RTF_PINNED)) { 548 return rtrequest(RTM_DELETE, rt_key(rt), 549 (struct sockaddr *)0, rt_mask(rt), 550 rt->rt_flags, (struct rtentry **)0); 551 } 552 return 0; 553 } 554 555 /* 556 * This routine is called from rt_setgate() to do the analogous thing for 557 * adds and changes. There is the added complication in this case of a 558 * middle insert; i.e., insertion of a new network route between an older 559 * network route and (cloned) host routes. For this reason, a simple check 560 * of rt->rt_parent is insufficient; each candidate route must be tested 561 * against the (mask, value) of the new route (passed as before in vp) 562 * to see if the new route matches it. Unfortunately, this has the obnoxious 563 * property of also triggering for insertion /above/ a pre-existing network 564 * route and clones. Sigh. This may be fixed some day. 565 * 566 * XXX - it may be possible to do fixdelete() for changes and reserve this 567 * routine just for adds. I'm not sure why I thought it was necessary to do 568 * changes this way. 569 */ 570 #ifdef DEBUG 571 int rtfcdebug = 0; 572 #endif 573 574 static int 575 rt_fixchange(struct radix_node *rn, void *vp) 576 { 577 struct rtentry *rt = (struct rtentry *)rn; 578 struct rtfc_arg *ap = vp; 579 struct rtentry *rt0 = ap->rt0; 580 struct radix_node_head *rnh = ap->rnh; 581 u_char *xk1, *xm1, *xk2; 582 int i, len; 583 584 #ifdef DEBUG 585 if (rtfcdebug) 586 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0); 587 #endif 588 589 if (!rt->rt_parent || (rt->rt_flags & RTF_PINNED)) { 590 #ifdef DEBUG 591 if(rtfcdebug) printf("no parent or pinned\n"); 592 #endif 593 return 0; 594 } 595 596 if (rt->rt_parent == rt0) { 597 #ifdef DEBUG 598 if(rtfcdebug) printf("parent match\n"); 599 #endif 600 return rtrequest(RTM_DELETE, rt_key(rt), 601 (struct sockaddr *)0, rt_mask(rt), 602 rt->rt_flags, (struct rtentry **)0); 603 } 604 605 /* 606 * There probably is a function somewhere which does this... 607 * if not, there should be. 608 */ 609 len = imin(((struct sockaddr *)rt_key(rt0))->sa_len, 610 ((struct sockaddr *)rt_key(rt))->sa_len); 611 612 xk1 = (u_char *)rt_key(rt0); 613 xm1 = (u_char *)rt_mask(rt0); 614 xk2 = (u_char *)rt_key(rt); 615 616 for (i = rnh->rnh_treetop->rn_off; i < len; i++) { 617 if ((xk2[i] & xm1[i]) != xk1[i]) { 618 #ifdef DEBUG 619 if(rtfcdebug) printf("no match\n"); 620 #endif 621 return 0; 622 } 623 } 624 625 /* 626 * OK, this node is a clone, and matches the node currently being 627 * changed/added under the node's mask. So, get rid of it. 628 */ 629 #ifdef DEBUG 630 if(rtfcdebug) printf("deleting\n"); 631 #endif 632 return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, 633 rt_mask(rt), rt->rt_flags, (struct rtentry **)0); 634 } 635 636 int 637 rt_setgate(rt0, dst, gate) 638 struct rtentry *rt0; 639 struct sockaddr *dst, *gate; 640 { 641 caddr_t new, old; 642 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len); 643 register struct rtentry *rt = rt0; 644 struct radix_node_head *rnh = rt_tables[dst->sa_family]; 645 646 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) { 647 old = (caddr_t)rt_key(rt); 648 R_Malloc(new, caddr_t, dlen + glen); 649 if (new == 0) 650 return 1; 651 rt->rt_nodes->rn_key = new; 652 } else { 653 new = rt->rt_nodes->rn_key; 654 old = 0; 655 } 656 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen); 657 if (old) { 658 Bcopy(dst, new, dlen); 659 Free(old); 660 } 661 if (rt->rt_gwroute) { 662 rt = rt->rt_gwroute; RTFREE(rt); 663 rt = rt0; rt->rt_gwroute = 0; 664 } 665 /* 666 * Cloning loop avoidance: 667 * In the presence of protocol-cloning and bad configuration, 668 * it is possible to get stuck in bottomless mutual recursion 669 * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing 670 * protocol-cloning to operate for gateways (which is probably the 671 * correct choice anyway), and avoid the resulting reference loops 672 * by disallowing any route to run through itself as a gateway. 673 * This is obviuosly mandatory when we get rt->rt_output(). 674 */ 675 if (rt->rt_flags & RTF_GATEWAY) { 676 rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING); 677 if (rt->rt_gwroute == rt) { 678 RTFREE(rt->rt_gwroute); 679 rt->rt_gwroute = 0; 680 return 1; /* failure */ 681 } 682 } 683 684 /* 685 * This isn't going to do anything useful for host routes, so 686 * don't bother. Also make sure we have a reasonable mask 687 * (we don't yet have one during adds). 688 */ 689 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 690 struct rtfc_arg arg; 691 arg.rnh = rnh; 692 arg.rt0 = rt; 693 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 694 rt_fixchange, &arg); 695 } 696 697 return 0; 698 } 699 700 void 701 rt_maskedcopy(src, dst, netmask) 702 struct sockaddr *src, *dst, *netmask; 703 { 704 register u_char *cp1 = (u_char *)src; 705 register u_char *cp2 = (u_char *)dst; 706 register u_char *cp3 = (u_char *)netmask; 707 u_char *cplim = cp2 + *cp3; 708 u_char *cplim2 = cp2 + *cp1; 709 710 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 711 cp3 += 2; 712 if (cplim > cplim2) 713 cplim = cplim2; 714 while (cp2 < cplim) 715 *cp2++ = *cp1++ & *cp3++; 716 if (cp2 < cplim2) 717 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 718 } 719 720 /* 721 * Set up a routing table entry, normally 722 * for an interface. 723 */ 724 int 725 rtinit(ifa, cmd, flags) 726 register struct ifaddr *ifa; 727 int cmd, flags; 728 { 729 register struct rtentry *rt; 730 register struct sockaddr *dst; 731 register struct sockaddr *deldst; 732 struct mbuf *m = 0; 733 struct rtentry *nrt = 0; 734 int error; 735 736 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr; 737 if (cmd == RTM_DELETE) { 738 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) { 739 m = m_get(M_WAIT, MT_SONAME); 740 deldst = mtod(m, struct sockaddr *); 741 rt_maskedcopy(dst, deldst, ifa->ifa_netmask); 742 dst = deldst; 743 } 744 rt = rtalloc1(dst, 0, 0UL); 745 if (rt) { 746 rt->rt_refcnt--; 747 if (rt->rt_ifa != ifa) { 748 if (m) 749 (void) m_free(m); 750 return (flags & RTF_HOST ? EHOSTUNREACH 751 : ENETUNREACH); 752 } 753 } 754 } 755 error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask, 756 flags | ifa->ifa_flags, &nrt); 757 if (m) 758 (void) m_free(m); 759 if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) { 760 rt_newaddrmsg(cmd, ifa, error, nrt); 761 if (rt->rt_refcnt <= 0) { 762 rt->rt_refcnt++; 763 rtfree(rt); 764 } 765 } 766 if (cmd == RTM_ADD && error == 0 && (rt = nrt)) { 767 rt->rt_refcnt--; 768 if (rt->rt_ifa != ifa) { 769 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa, 770 rt->rt_ifa); 771 if (rt->rt_ifa->ifa_rtrequest) 772 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); 773 IFAFREE(rt->rt_ifa); 774 rt->rt_ifa = ifa; 775 rt->rt_ifp = ifa->ifa_ifp; 776 ifa->ifa_refcnt++; 777 if (ifa->ifa_rtrequest) 778 ifa->ifa_rtrequest(RTM_ADD, rt, SA(0)); 779 } 780 rt_newaddrmsg(cmd, ifa, error, nrt); 781 } 782 return (error); 783 } 784