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