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.26 1995/10/16 19:09:40 wollman 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 #define senderr(x) { error = x ; goto bad; } 377 378 if ((rnh = rt_tables[dst->sa_family]) == 0) 379 senderr(ESRCH); 380 if (flags & RTF_HOST) 381 netmask = 0; 382 switch (req) { 383 case RTM_DELETE: 384 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0) 385 senderr(ESRCH); 386 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 387 panic ("rtrequest delete"); 388 rt = (struct rtentry *)rn; 389 390 /* 391 * Now search what's left of the subtree for any cloned 392 * routes which might have been formed from this node. 393 */ 394 if ((rt->rt_flags & RTF_PRCLONING) && netmask) { 395 rnh->rnh_walktree_from(rnh, dst, netmask, 396 rt_fixdelete, rt); 397 } 398 399 /* 400 * NB: RTF_UP must be set during the search above, 401 * because we might delete the last ref, causing 402 * rt to get freed prematurely. 403 */ 404 rt->rt_flags &= ~RTF_UP; 405 406 if (rt->rt_gwroute) { 407 rt = rt->rt_gwroute; RTFREE(rt); 408 (rt = (struct rtentry *)rn)->rt_gwroute = 0; 409 } 410 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 411 ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); 412 rttrash++; 413 if (ret_nrt) 414 *ret_nrt = rt; 415 else if (rt->rt_refcnt <= 0) { 416 rt->rt_refcnt++; 417 rtfree(rt); 418 } 419 break; 420 421 case RTM_RESOLVE: 422 if (ret_nrt == 0 || (rt = *ret_nrt) == 0) 423 senderr(EINVAL); 424 ifa = rt->rt_ifa; 425 flags = rt->rt_flags & 426 ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC); 427 flags |= RTF_WASCLONED; 428 gateway = rt->rt_gateway; 429 if ((netmask = rt->rt_genmask) == 0) 430 flags |= RTF_HOST; 431 goto makeroute; 432 433 case RTM_ADD: 434 if ((flags & RTF_GATEWAY) && !gateway) 435 panic("rtrequest: GATEWAY but no gateway"); 436 437 if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0) 438 senderr(ENETUNREACH); 439 440 makeroute: 441 R_Malloc(rt, struct rtentry *, sizeof(*rt)); 442 if (rt == 0) 443 senderr(ENOBUFS); 444 Bzero(rt, sizeof(*rt)); 445 rt->rt_flags = RTF_UP | flags; 446 if (rt_setgate(rt, dst, gateway)) { 447 Free(rt); 448 senderr(ENOBUFS); 449 } 450 ndst = rt_key(rt); 451 if (netmask) { 452 rt_maskedcopy(dst, ndst, netmask); 453 } else 454 Bcopy(dst, ndst, dst->sa_len); 455 456 /* 457 * This moved from below so that rnh->rnh_addaddr() can 458 * examine the ifa and ifp if it so desires. 459 */ 460 ifa->ifa_refcnt++; 461 rt->rt_ifa = ifa; 462 rt->rt_ifp = ifa->ifa_ifp; 463 464 rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask, 465 rnh, rt->rt_nodes); 466 if (rn == 0) { 467 struct rtentry *rt2; 468 /* 469 * Uh-oh, we already have one of these in the tree. 470 * We do a special hack: if the route that's already 471 * there was generated by the protocol-cloning 472 * mechanism, then we just blow it away and retry 473 * the insertion of the new one. 474 */ 475 rt2 = rtalloc1(dst, 0, RTF_PRCLONING); 476 if (rt2 && rt2->rt_parent) { 477 rtrequest(RTM_DELETE, 478 (struct sockaddr *)rt_key(rt2), 479 rt2->rt_gateway, 480 rt_mask(rt2), rt2->rt_flags, 0); 481 RTFREE(rt2); 482 rn = rnh->rnh_addaddr((caddr_t)ndst, 483 (caddr_t)netmask, 484 rnh, rt->rt_nodes); 485 } 486 } 487 488 if (rn == 0) { 489 if (rt->rt_gwroute) 490 rtfree(rt->rt_gwroute); 491 if (rt->rt_ifa) { 492 IFAFREE(rt->rt_ifa); 493 } 494 Free(rt_key(rt)); 495 Free(rt); 496 senderr(EEXIST); 497 } 498 rt->rt_parent = 0; 499 500 if (req == RTM_RESOLVE) { 501 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 502 if ((*ret_nrt)->rt_flags & RTF_PRCLONING) { 503 rt->rt_parent = (*ret_nrt); 504 (*ret_nrt)->rt_refcnt++; 505 } 506 } 507 if (ifa->ifa_rtrequest) 508 ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0)); 509 /* 510 * We repeat the same procedure from rt_setgate() here because 511 * it doesn't fire when we call it there because the node 512 * hasn't been added to the tree yet. 513 */ 514 if (!(rt->rt_flags & RTF_HOST)) { 515 struct rtfc_arg arg; 516 arg.rnh = rnh; 517 arg.rt0 = rt; 518 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 519 rt_fixchange, &arg); 520 } 521 522 if (ret_nrt) { 523 *ret_nrt = rt; 524 rt->rt_refcnt++; 525 } 526 break; 527 } 528 bad: 529 splx(s); 530 return (error); 531 } 532 533 /* 534 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' 535 * (i.e., the routes related to it by the operation of cloning). This 536 * routine is iterated over all potential former-child-routes by way of 537 * rnh->rnh_walktree_from() above, and those that actually are children of 538 * the late parent (passed in as VP here) are themselves deleted. 539 */ 540 static int 541 rt_fixdelete(struct radix_node *rn, void *vp) 542 { 543 struct rtentry *rt = (struct rtentry *)rn; 544 struct rtentry *rt0 = vp; 545 546 if (rt->rt_parent == rt0 && !(rt->rt_flags & RTF_PINNED)) { 547 return rtrequest(RTM_DELETE, rt_key(rt), 548 (struct sockaddr *)0, rt_mask(rt), 549 rt->rt_flags, (struct rtentry **)0); 550 } 551 return 0; 552 } 553 554 /* 555 * This routine is called from rt_setgate() to do the analogous thing for 556 * adds and changes. There is the added complication in this case of a 557 * middle insert; i.e., insertion of a new network route between an older 558 * network route and (cloned) host routes. For this reason, a simple check 559 * of rt->rt_parent is insufficient; each candidate route must be tested 560 * against the (mask, value) of the new route (passed as before in vp) 561 * to see if the new route matches it. Unfortunately, this has the obnoxious 562 * property of also triggering for insertion /above/ a pre-existing network 563 * route and clones. Sigh. This may be fixed some day. 564 * 565 * XXX - it may be possible to do fixdelete() for changes and reserve this 566 * routine just for adds. I'm not sure why I thought it was necessary to do 567 * changes this way. 568 */ 569 #ifdef DEBUG 570 int rtfcdebug = 0; 571 #endif 572 573 static int 574 rt_fixchange(struct radix_node *rn, void *vp) 575 { 576 struct rtentry *rt = (struct rtentry *)rn; 577 struct rtfc_arg *ap = vp; 578 struct rtentry *rt0 = ap->rt0; 579 struct radix_node_head *rnh = ap->rnh; 580 u_char *xk1, *xm1, *xk2; 581 int i, len; 582 583 #ifdef DEBUG 584 if (rtfcdebug) 585 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0); 586 #endif 587 588 if (!rt->rt_parent || (rt->rt_flags & RTF_PINNED)) { 589 #ifdef DEBUG 590 if(rtfcdebug) printf("no parent or pinned\n"); 591 #endif 592 return 0; 593 } 594 595 if (rt->rt_parent == rt0) { 596 #ifdef DEBUG 597 if(rtfcdebug) printf("parent match\n"); 598 #endif 599 return rtrequest(RTM_DELETE, rt_key(rt), 600 (struct sockaddr *)0, rt_mask(rt), 601 rt->rt_flags, (struct rtentry **)0); 602 } 603 604 /* 605 * There probably is a function somewhere which does this... 606 * if not, there should be. 607 */ 608 len = imin(((struct sockaddr *)rt_key(rt0))->sa_len, 609 ((struct sockaddr *)rt_key(rt))->sa_len); 610 611 xk1 = (u_char *)rt_key(rt0); 612 xm1 = (u_char *)rt_mask(rt0); 613 xk2 = (u_char *)rt_key(rt); 614 615 for (i = rnh->rnh_treetop->rn_off; i < len; i++) { 616 if ((xk2[i] & xm1[i]) != xk1[i]) { 617 #ifdef DEBUG 618 if(rtfcdebug) printf("no match\n"); 619 #endif 620 return 0; 621 } 622 } 623 624 /* 625 * OK, this node is a clone, and matches the node currently being 626 * changed/added under the node's mask. So, get rid of it. 627 */ 628 #ifdef DEBUG 629 if(rtfcdebug) printf("deleting\n"); 630 #endif 631 return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, 632 rt_mask(rt), rt->rt_flags, (struct rtentry **)0); 633 } 634 635 int 636 rt_setgate(rt0, dst, gate) 637 struct rtentry *rt0; 638 struct sockaddr *dst, *gate; 639 { 640 caddr_t new, old; 641 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len); 642 register struct rtentry *rt = rt0; 643 struct radix_node_head *rnh = rt_tables[dst->sa_family]; 644 645 if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) { 646 old = (caddr_t)rt_key(rt); 647 R_Malloc(new, caddr_t, dlen + glen); 648 if (new == 0) 649 return 1; 650 rt->rt_nodes->rn_key = new; 651 } else { 652 new = rt->rt_nodes->rn_key; 653 old = 0; 654 } 655 Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen); 656 if (old) { 657 Bcopy(dst, new, dlen); 658 Free(old); 659 } 660 if (rt->rt_gwroute) { 661 rt = rt->rt_gwroute; RTFREE(rt); 662 rt = rt0; rt->rt_gwroute = 0; 663 } 664 /* 665 * Cloning loop avoidance: 666 * In the presence of protocol-cloning and bad configuration, 667 * it is possible to get stuck in bottomless mutual recursion 668 * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing 669 * protocol-cloning to operate for gateways (which is probably the 670 * correct choice anyway), and avoid the resulting reference loops 671 * by disallowing any route to run through itself as a gateway. 672 * This is obviuosly mandatory when we get rt->rt_output(). 673 */ 674 if (rt->rt_flags & RTF_GATEWAY) { 675 rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING); 676 if (rt->rt_gwroute == rt) { 677 RTFREE(rt->rt_gwroute); 678 rt->rt_gwroute = 0; 679 return 1; /* failure */ 680 } 681 } 682 683 /* 684 * This isn't going to do anything useful for host routes, so 685 * don't bother. Also make sure we have a reasonable mask 686 * (we don't yet have one during adds). 687 */ 688 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 689 struct rtfc_arg arg; 690 arg.rnh = rnh; 691 arg.rt0 = rt; 692 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 693 rt_fixchange, &arg); 694 } 695 696 return 0; 697 } 698 699 void 700 rt_maskedcopy(src, dst, netmask) 701 struct sockaddr *src, *dst, *netmask; 702 { 703 register u_char *cp1 = (u_char *)src; 704 register u_char *cp2 = (u_char *)dst; 705 register u_char *cp3 = (u_char *)netmask; 706 u_char *cplim = cp2 + *cp3; 707 u_char *cplim2 = cp2 + *cp1; 708 709 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 710 cp3 += 2; 711 if (cplim > cplim2) 712 cplim = cplim2; 713 while (cp2 < cplim) 714 *cp2++ = *cp1++ & *cp3++; 715 if (cp2 < cplim2) 716 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 717 } 718 719 /* 720 * Set up a routing table entry, normally 721 * for an interface. 722 */ 723 int 724 rtinit(ifa, cmd, flags) 725 register struct ifaddr *ifa; 726 int cmd, flags; 727 { 728 register struct rtentry *rt; 729 register struct sockaddr *dst; 730 register struct sockaddr *deldst; 731 struct mbuf *m = 0; 732 struct rtentry *nrt = 0; 733 int error; 734 735 dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr; 736 if (cmd == RTM_DELETE) { 737 if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) { 738 m = m_get(M_WAIT, MT_SONAME); 739 deldst = mtod(m, struct sockaddr *); 740 rt_maskedcopy(dst, deldst, ifa->ifa_netmask); 741 dst = deldst; 742 } 743 rt = rtalloc1(dst, 0, 0UL); 744 if (rt) { 745 rt->rt_refcnt--; 746 if (rt->rt_ifa != ifa) { 747 if (m) 748 (void) m_free(m); 749 return (flags & RTF_HOST ? EHOSTUNREACH 750 : ENETUNREACH); 751 } 752 } 753 } 754 error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask, 755 flags | ifa->ifa_flags, &nrt); 756 if (m) 757 (void) m_free(m); 758 if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) { 759 rt_newaddrmsg(cmd, ifa, error, nrt); 760 if (rt->rt_refcnt <= 0) { 761 rt->rt_refcnt++; 762 rtfree(rt); 763 } 764 } 765 if (cmd == RTM_ADD && error == 0 && (rt = nrt)) { 766 rt->rt_refcnt--; 767 if (rt->rt_ifa != ifa) { 768 printf("rtinit: wrong ifa (%p) was (%p)\n", ifa, 769 rt->rt_ifa); 770 if (rt->rt_ifa->ifa_rtrequest) 771 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); 772 IFAFREE(rt->rt_ifa); 773 rt->rt_ifa = ifa; 774 rt->rt_ifp = ifa->ifa_ifp; 775 ifa->ifa_refcnt++; 776 if (ifa->ifa_rtrequest) 777 ifa->ifa_rtrequest(RTM_ADD, rt, SA(0)); 778 } 779 rt_newaddrmsg(cmd, ifa, error, nrt); 780 } 781 return (error); 782 } 783