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) 1980, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)route.c 8.3 (Berkeley) 1/9/95 62 * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $ 63 */ 64 65 #include "opt_inet.h" 66 #include "opt_mpls.h" 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/malloc.h> 71 #include <sys/mbuf.h> 72 #include <sys/socket.h> 73 #include <sys/domain.h> 74 #include <sys/kernel.h> 75 #include <sys/sysctl.h> 76 #include <sys/globaldata.h> 77 #include <sys/thread.h> 78 79 #include <net/if.h> 80 #include <net/if_var.h> 81 #include <net/route.h> 82 #include <net/netisr.h> 83 84 #include <netinet/in.h> 85 #include <net/ip_mroute/ip_mroute.h> 86 87 #include <sys/thread2.h> 88 #include <sys/msgport2.h> 89 #include <net/netmsg2.h> 90 #include <net/netisr2.h> 91 92 #ifdef MPLS 93 #include <netproto/mpls/mpls.h> 94 #endif 95 96 static struct rtstatistics rtstatistics_percpu[MAXCPU] __cachealign; 97 #define rtstat rtstatistics_percpu[mycpuid] 98 99 struct radix_node_head *rt_tables[MAXCPU][AF_MAX+1]; 100 101 static void rt_maskedcopy (struct sockaddr *, struct sockaddr *, 102 struct sockaddr *); 103 static void rtable_init(void); 104 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo *, 105 struct rtentry *, void *); 106 107 static void rtredirect_msghandler(netmsg_t msg); 108 static void rtrequest1_msghandler(netmsg_t msg); 109 static void rtsearch_msghandler(netmsg_t msg); 110 static void rtmask_add_msghandler(netmsg_t msg); 111 112 static int rt_setshims(struct rtentry *, struct sockaddr **); 113 114 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW, 0, "Routing"); 115 116 #ifdef ROUTE_DEBUG 117 static int route_debug = 1; 118 SYSCTL_INT(_net_route, OID_AUTO, route_debug, CTLFLAG_RW, 119 &route_debug, 0, ""); 120 #endif 121 122 u_long route_kmalloc_limit = 0; 123 TUNABLE_ULONG("net.route.kmalloc_limit", &route_kmalloc_limit); 124 125 /* 126 * Initialize the route table(s) for protocol domains and 127 * create a helper thread which will be responsible for updating 128 * route table entries on each cpu. 129 */ 130 void 131 route_init(void) 132 { 133 int cpu; 134 135 if (route_kmalloc_limit) 136 kmalloc_raise_limit(M_RTABLE, route_kmalloc_limit); 137 138 for (cpu = 0; cpu < netisr_ncpus; ++cpu) 139 bzero(&rtstatistics_percpu[cpu], sizeof(struct rtstatistics)); 140 rn_init(); /* initialize all zeroes, all ones, mask table */ 141 rtable_init(); /* call dom_rtattach() on each cpu */ 142 } 143 144 static void 145 rtable_init_oncpu(netmsg_t msg) 146 { 147 struct domain *dom; 148 int cpu = mycpuid; 149 150 ASSERT_NETISR_NCPUS(cpu); 151 152 SLIST_FOREACH(dom, &domains, dom_next) { 153 if (dom->dom_rtattach) { 154 dom->dom_rtattach( 155 (void **)&rt_tables[cpu][dom->dom_family], 156 dom->dom_rtoffset); 157 } 158 } 159 netisr_forwardmsg(&msg->base, cpu + 1); 160 } 161 162 static void 163 rtable_init(void) 164 { 165 struct netmsg_base msg; 166 167 netmsg_init(&msg, NULL, &curthread->td_msgport, 0, rtable_init_oncpu); 168 netisr_domsg_global(&msg); 169 } 170 171 /* 172 * Routing statistics. 173 */ 174 static int 175 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS) 176 { 177 int cpu, error = 0; 178 179 for (cpu = 0; cpu < netisr_ncpus; ++cpu) { 180 if ((error = SYSCTL_OUT(req, &rtstatistics_percpu[cpu], 181 sizeof(struct rtstatistics)))) 182 break; 183 if ((error = SYSCTL_IN(req, &rtstatistics_percpu[cpu], 184 sizeof(struct rtstatistics)))) 185 break; 186 } 187 188 return (error); 189 } 190 SYSCTL_PROC(_net_route, OID_AUTO, stats, (CTLTYPE_OPAQUE|CTLFLAG_RW), 191 0, 0, sysctl_rtstatistics, "S,rtstatistics", "Routing statistics"); 192 193 /* 194 * Packet routing routines. 195 */ 196 197 /* 198 * Look up and fill in the "ro_rt" rtentry field in a route structure given 199 * an address in the "ro_dst" field. Always send a report on a miss and 200 * always clone routes. 201 */ 202 void 203 rtalloc(struct route *ro) 204 { 205 rtalloc_ign(ro, 0UL); 206 } 207 208 /* 209 * Look up and fill in the "ro_rt" rtentry field in a route structure given 210 * an address in the "ro_dst" field. Always send a report on a miss and 211 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being 212 * ignored. 213 */ 214 void 215 rtalloc_ign(struct route *ro, u_long ignoreflags) 216 { 217 if (ro->ro_rt != NULL) { 218 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP) 219 return; 220 rtfree(ro->ro_rt); 221 ro->ro_rt = NULL; 222 } 223 ro->ro_rt = _rtlookup(&ro->ro_dst, ignoreflags); 224 } 225 226 /* 227 * Look up the route that matches the given "dst" address. 228 * 229 * Route lookup can have the side-effect of creating and returning 230 * a cloned route instead when "dst" matches a cloning route and the 231 * RTF_CLONING and RTF_PRCLONING flags are not being ignored. 232 * 233 * Any route returned has its reference count incremented. 234 */ 235 struct rtentry * 236 _rtlookup(struct sockaddr *dst, u_long ignore) 237 { 238 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family]; 239 struct rtentry *rt; 240 241 ASSERT_NETISR_NCPUS(mycpuid); 242 243 if (rnh == NULL) 244 goto unreach; 245 246 /* 247 * Look up route in the radix tree. 248 */ 249 rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh); 250 if (rt == NULL) 251 goto unreach; 252 253 /* 254 * Handle cloning routes. 255 */ 256 if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) { 257 struct rtentry *clonedroute; 258 int error; 259 260 clonedroute = rt; /* copy in/copy out parameter */ 261 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0, 262 &clonedroute); /* clone the route */ 263 if (error != 0) { /* cloning failed */ 264 rt_dstmsg(RTM_MISS, dst, error); 265 rt->rt_refcnt++; 266 return (rt); /* return the uncloned route */ 267 } 268 if (clonedroute->rt_flags & RTF_XRESOLVE) 269 rt_dstmsg(RTM_RESOLVE, dst, 0); 270 return (clonedroute); /* return cloned route */ 271 } 272 273 /* 274 * Increment the reference count of the matched route and return. 275 */ 276 rt->rt_refcnt++; 277 return (rt); 278 279 unreach: 280 rtstat.rts_unreach++; 281 rt_dstmsg(RTM_MISS, dst, 0); 282 return (NULL); 283 } 284 285 void 286 rtfree(struct rtentry *rt) 287 { 288 289 ASSERT_NETISR_NCPUS(rt->rt_cpuid); 290 KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt)); 291 292 --rt->rt_refcnt; 293 if (rt->rt_refcnt == 0) { 294 struct radix_node_head *rnh = 295 rt_tables[mycpuid][rt_key(rt)->sa_family]; 296 297 if (rnh->rnh_close) 298 rnh->rnh_close((struct radix_node *)rt, rnh); 299 if (!(rt->rt_flags & RTF_UP)) { 300 /* deallocate route */ 301 if (rt->rt_ifa != NULL) 302 IFAFREE(rt->rt_ifa); 303 if (rt->rt_parent != NULL) 304 RTFREE(rt->rt_parent); /* recursive call! */ 305 Free(rt_key(rt)); 306 Free(rt); 307 } 308 } 309 } 310 311 static void 312 rtfree_async_dispatch(netmsg_t msg) 313 { 314 struct rtentry *rt = msg->lmsg.u.ms_resultp; 315 316 rtfree(rt); 317 netisr_replymsg(&msg->base, 0); 318 } 319 320 void 321 rtfree_async(struct rtentry *rt) 322 { 323 struct netmsg_base *msg; 324 325 if (IN_NETISR_NCPUS(rt->rt_cpuid)) { 326 rtfree(rt); 327 return; 328 } 329 330 KASSERT(rt->rt_refcnt > 0, 331 ("rtfree_async: rt_refcnt %ld", rt->rt_refcnt)); 332 333 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_INTWAIT); 334 netmsg_init(msg, NULL, &netisr_afree_rport, 0, rtfree_async_dispatch); 335 msg->lmsg.u.ms_resultp = rt; 336 337 netisr_sendmsg(msg, rt->rt_cpuid); 338 } 339 340 int 341 rtredirect_oncpu(struct sockaddr *dst, struct sockaddr *gateway, 342 struct sockaddr *netmask, int flags, struct sockaddr *src) 343 { 344 struct rtentry *rt = NULL; 345 struct rt_addrinfo rtinfo; 346 struct ifaddr *ifa; 347 u_long *stat = NULL; 348 int error; 349 350 ASSERT_NETISR_NCPUS(mycpuid); 351 352 /* verify the gateway is directly reachable */ 353 if ((ifa = ifa_ifwithnet(gateway)) == NULL) { 354 error = ENETUNREACH; 355 goto out; 356 } 357 358 /* 359 * If the redirect isn't from our current router for this destination, 360 * it's either old or wrong. 361 */ 362 if (!(flags & RTF_DONE) && /* XXX JH */ 363 (rt = rtpurelookup(dst)) != NULL && 364 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) { 365 error = EINVAL; 366 goto done; 367 } 368 369 /* 370 * If it redirects us to ourselves, we have a routing loop, 371 * perhaps as a result of an interface going down recently. 372 */ 373 if (ifa_ifwithaddr(gateway)) { 374 error = EHOSTUNREACH; 375 goto done; 376 } 377 378 /* 379 * Create a new entry if the lookup failed or if we got back 380 * a wildcard entry for the default route. This is necessary 381 * for hosts which use routing redirects generated by smart 382 * gateways to dynamically build the routing tables. 383 */ 384 if (rt == NULL) 385 goto create; 386 if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) { 387 rtfree(rt); 388 goto create; 389 } 390 391 /* Ignore redirects for directly connected hosts. */ 392 if (!(rt->rt_flags & RTF_GATEWAY)) { 393 error = EHOSTUNREACH; 394 goto done; 395 } 396 397 if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) { 398 /* 399 * Changing from a network route to a host route. 400 * Create a new host route rather than smashing the 401 * network route. 402 */ 403 create: 404 flags |= RTF_GATEWAY | RTF_DYNAMIC; 405 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 406 rtinfo.rti_info[RTAX_DST] = dst; 407 rtinfo.rti_info[RTAX_GATEWAY] = gateway; 408 rtinfo.rti_info[RTAX_NETMASK] = netmask; 409 rtinfo.rti_flags = flags; 410 rtinfo.rti_ifa = ifa; 411 rt = NULL; /* copy-in/copy-out parameter */ 412 error = rtrequest1(RTM_ADD, &rtinfo, &rt); 413 if (rt != NULL) 414 flags = rt->rt_flags; 415 stat = &rtstat.rts_dynamic; 416 } else { 417 /* 418 * Smash the current notion of the gateway to this destination. 419 * Should check about netmask!!! 420 */ 421 rt->rt_flags |= RTF_MODIFIED; 422 flags |= RTF_MODIFIED; 423 424 /* We only need to report rtmsg on CPU0 */ 425 rt_setgate(rt, rt_key(rt), gateway); 426 if (mycpuid == 0) 427 rt_rtmsg(RTM_CHANGE, rt, rt->rt_ifp, 0); 428 error = 0; 429 stat = &rtstat.rts_newgateway; 430 } 431 432 done: 433 if (rt != NULL) 434 rtfree(rt); 435 out: 436 if (error != 0) 437 rtstat.rts_badredirect++; 438 else if (stat != NULL) 439 (*stat)++; 440 441 return error; 442 } 443 444 struct netmsg_rtredirect { 445 struct netmsg_base base; 446 struct sockaddr *dst; 447 struct sockaddr *gateway; 448 struct sockaddr *netmask; 449 int flags; 450 struct sockaddr *src; 451 }; 452 453 /* 454 * Force a routing table entry to the specified 455 * destination to go through the given gateway. 456 * Normally called as a result of a routing redirect 457 * message from the network layer. 458 */ 459 void 460 rtredirect(struct sockaddr *dst, struct sockaddr *gateway, 461 struct sockaddr *netmask, int flags, struct sockaddr *src) 462 { 463 struct rt_addrinfo rtinfo; 464 int error; 465 struct netmsg_rtredirect msg; 466 467 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 468 0, rtredirect_msghandler); 469 msg.dst = dst; 470 msg.gateway = gateway; 471 msg.netmask = netmask; 472 msg.flags = flags; 473 msg.src = src; 474 error = netisr_domsg_global(&msg.base); 475 476 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 477 rtinfo.rti_info[RTAX_DST] = dst; 478 rtinfo.rti_info[RTAX_GATEWAY] = gateway; 479 rtinfo.rti_info[RTAX_NETMASK] = netmask; 480 rtinfo.rti_info[RTAX_AUTHOR] = src; 481 rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error); 482 } 483 484 static void 485 rtredirect_msghandler(netmsg_t msg) 486 { 487 struct netmsg_rtredirect *rmsg = (void *)msg; 488 489 rtredirect_oncpu(rmsg->dst, rmsg->gateway, rmsg->netmask, 490 rmsg->flags, rmsg->src); 491 netisr_forwardmsg(&msg->base, mycpuid + 1); 492 } 493 494 /* 495 * Routing table ioctl interface. 496 */ 497 int 498 rtioctl(u_long req, caddr_t data, struct ucred *cred) 499 { 500 #ifdef INET 501 /* Multicast goop, grrr... */ 502 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP; 503 #else 504 return ENXIO; 505 #endif 506 } 507 508 struct ifaddr * 509 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway) 510 { 511 struct ifaddr *ifa; 512 513 if (!(flags & RTF_GATEWAY)) { 514 /* 515 * If we are adding a route to an interface, 516 * and the interface is a point-to-point link, 517 * we should search for the destination 518 * as our clue to the interface. Otherwise 519 * we can use the local address. 520 */ 521 ifa = NULL; 522 if (flags & RTF_HOST) { 523 ifa = ifa_ifwithdstaddr(dst); 524 } 525 if (ifa == NULL) 526 ifa = ifa_ifwithaddr(gateway); 527 } else { 528 /* 529 * If we are adding a route to a remote net 530 * or host, the gateway may still be on the 531 * other end of a pt to pt link. 532 */ 533 ifa = ifa_ifwithdstaddr(gateway); 534 } 535 if (ifa == NULL) 536 ifa = ifa_ifwithnet(gateway); 537 if (ifa == NULL) { 538 struct rtentry *rt; 539 540 rt = rtpurelookup(gateway); 541 if (rt == NULL) 542 return (NULL); 543 rt->rt_refcnt--; 544 if ((ifa = rt->rt_ifa) == NULL) 545 return (NULL); 546 } 547 if (ifa->ifa_addr->sa_family != dst->sa_family) { 548 struct ifaddr *oldifa = ifa; 549 550 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 551 if (ifa == NULL) 552 ifa = oldifa; 553 } 554 return (ifa); 555 } 556 557 static int rt_fixdelete (struct radix_node *, void *); 558 static int rt_fixchange (struct radix_node *, void *); 559 560 struct rtfc_arg { 561 struct rtentry *rt0; 562 struct radix_node_head *rnh; 563 }; 564 565 /* 566 * Set rtinfo->rti_ifa and rtinfo->rti_ifp. 567 */ 568 int 569 rt_getifa(struct rt_addrinfo *rtinfo) 570 { 571 struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY]; 572 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST]; 573 struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA]; 574 int flags = rtinfo->rti_flags; 575 576 /* 577 * ifp may be specified by sockaddr_dl 578 * when protocol address is ambiguous. 579 */ 580 if (rtinfo->rti_ifp == NULL) { 581 struct sockaddr *ifpaddr; 582 583 ifpaddr = rtinfo->rti_info[RTAX_IFP]; 584 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) { 585 struct ifaddr *ifa; 586 587 ifa = ifa_ifwithnet(ifpaddr); 588 if (ifa != NULL) 589 rtinfo->rti_ifp = ifa->ifa_ifp; 590 } 591 } 592 593 if (rtinfo->rti_ifa == NULL && ifaaddr != NULL) 594 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr); 595 if (rtinfo->rti_ifa == NULL) { 596 struct sockaddr *sa; 597 598 sa = ifaaddr != NULL ? ifaaddr : 599 (gateway != NULL ? gateway : dst); 600 if (sa != NULL && rtinfo->rti_ifp != NULL) 601 rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp); 602 else if (dst != NULL && gateway != NULL) 603 rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway); 604 else if (sa != NULL) 605 rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa); 606 } 607 if (rtinfo->rti_ifa == NULL) 608 return (ENETUNREACH); 609 610 if (rtinfo->rti_ifp == NULL) 611 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp; 612 return (0); 613 } 614 615 /* 616 * Do appropriate manipulations of a routing tree given 617 * all the bits of info needed 618 */ 619 int 620 rtrequest( 621 int req, 622 struct sockaddr *dst, 623 struct sockaddr *gateway, 624 struct sockaddr *netmask, 625 int flags, 626 struct rtentry **ret_nrt) 627 { 628 struct rt_addrinfo rtinfo; 629 630 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 631 rtinfo.rti_info[RTAX_DST] = dst; 632 rtinfo.rti_info[RTAX_GATEWAY] = gateway; 633 rtinfo.rti_info[RTAX_NETMASK] = netmask; 634 rtinfo.rti_flags = flags; 635 return rtrequest1(req, &rtinfo, ret_nrt); 636 } 637 638 int 639 rtrequest_global( 640 int req, 641 struct sockaddr *dst, 642 struct sockaddr *gateway, 643 struct sockaddr *netmask, 644 int flags) 645 { 646 struct rt_addrinfo rtinfo; 647 648 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 649 rtinfo.rti_info[RTAX_DST] = dst; 650 rtinfo.rti_info[RTAX_GATEWAY] = gateway; 651 rtinfo.rti_info[RTAX_NETMASK] = netmask; 652 rtinfo.rti_flags = flags; 653 return rtrequest1_global(req, &rtinfo, NULL, NULL, RTREQ_PRIO_NORM); 654 } 655 656 struct netmsg_rtq { 657 struct netmsg_base base; 658 int req; 659 struct rt_addrinfo *rtinfo; 660 rtrequest1_callback_func_t callback; 661 void *arg; 662 }; 663 664 int 665 rtrequest1_global(int req, struct rt_addrinfo *rtinfo, 666 rtrequest1_callback_func_t callback, void *arg, boolean_t req_prio) 667 { 668 struct netmsg_rtq msg; 669 int flags = 0; 670 671 if (req_prio) 672 flags = MSGF_PRIORITY; 673 netmsg_init(&msg.base, NULL, &curthread->td_msgport, flags, 674 rtrequest1_msghandler); 675 msg.base.lmsg.ms_error = -1; 676 msg.req = req; 677 msg.rtinfo = rtinfo; 678 msg.callback = callback; 679 msg.arg = arg; 680 return (netisr_domsg_global(&msg.base)); 681 } 682 683 /* 684 * Handle a route table request on the current cpu. Since the route table's 685 * are supposed to be identical on each cpu, an error occuring later in the 686 * message chain is considered system-fatal. 687 */ 688 static void 689 rtrequest1_msghandler(netmsg_t msg) 690 { 691 struct netmsg_rtq *rmsg = (void *)msg; 692 struct rt_addrinfo rtinfo; 693 struct rtentry *rt = NULL; 694 int error; 695 696 /* 697 * Copy the rtinfo. We need to make sure that the original 698 * rtinfo, which is setup by the caller, in the netmsg will 699 * _not_ be changed; else the next CPU on the netmsg forwarding 700 * path will see a different rtinfo than what this CPU has seen. 701 */ 702 rtinfo = *rmsg->rtinfo; 703 704 error = rtrequest1(rmsg->req, &rtinfo, &rt); 705 if (rt) 706 --rt->rt_refcnt; 707 if (rmsg->callback) 708 rmsg->callback(rmsg->req, error, &rtinfo, rt, rmsg->arg); 709 710 /* 711 * RTM_DELETE's are propogated even if an error occurs, since a 712 * cloned route might be undergoing deletion and cloned routes 713 * are not necessarily replicated. An overall error is returned 714 * only if no cpus have the route in question. 715 */ 716 if (rmsg->base.lmsg.ms_error < 0 || error == 0) 717 rmsg->base.lmsg.ms_error = error; 718 719 if (error && rmsg->req != RTM_DELETE) { 720 if (mycpuid != 0) { 721 panic("rtrequest1_msghandler: rtrequest table req %d, " 722 "failed on cpu%d, error %d\n", 723 rmsg->req, mycpuid, error); 724 } 725 netisr_replymsg(&rmsg->base, error); 726 } else { 727 netisr_forwardmsg_error(&rmsg->base, mycpuid + 1, 728 rmsg->base.lmsg.ms_error); 729 } 730 } 731 732 int 733 rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt) 734 { 735 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST]; 736 struct rtentry *rt; 737 struct radix_node *rn; 738 struct radix_node_head *rnh; 739 struct ifaddr *ifa; 740 struct sockaddr *ndst; 741 int error = 0; 742 743 ASSERT_NETISR_NCPUS(mycpuid); 744 745 #define gotoerr(x) { error = x ; goto bad; } 746 747 #ifdef ROUTE_DEBUG 748 if (route_debug) 749 rt_addrinfo_print(req, rtinfo); 750 #endif 751 752 crit_enter(); 753 /* 754 * Find the correct routing tree to use for this Address Family 755 */ 756 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL) 757 gotoerr(EAFNOSUPPORT); 758 759 /* 760 * If we are adding a host route then we don't want to put 761 * a netmask in the tree, nor do we want to clone it. 762 */ 763 if (rtinfo->rti_flags & RTF_HOST) { 764 rtinfo->rti_info[RTAX_NETMASK] = NULL; 765 rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING); 766 } 767 768 switch (req) { 769 case RTM_DELETE: 770 /* Remove the item from the tree. */ 771 rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST], 772 (char *)rtinfo->rti_info[RTAX_NETMASK], 773 rnh); 774 if (rn == NULL) 775 gotoerr(ESRCH); 776 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)), 777 ("rnh_deladdr returned flags 0x%x", rn->rn_flags)); 778 rt = (struct rtentry *)rn; 779 780 /* ref to prevent a deletion race */ 781 ++rt->rt_refcnt; 782 783 /* Free any routes cloned from this one. */ 784 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) && 785 rt_mask(rt) != NULL) { 786 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt), 787 (char *)rt_mask(rt), 788 rt_fixdelete, rt); 789 } 790 791 if (rt->rt_gwroute != NULL) { 792 RTFREE(rt->rt_gwroute); 793 rt->rt_gwroute = NULL; 794 } 795 796 /* 797 * NB: RTF_UP must be set during the search above, 798 * because we might delete the last ref, causing 799 * rt to get freed prematurely. 800 */ 801 rt->rt_flags &= ~RTF_UP; 802 803 #ifdef ROUTE_DEBUG 804 if (route_debug) 805 rt_print(rtinfo, rt); 806 #endif 807 808 /* Give the protocol a chance to keep things in sync. */ 809 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 810 ifa->ifa_rtrequest(RTM_DELETE, rt); 811 812 /* 813 * If the caller wants it, then it can have it, 814 * but it's up to it to free the rtentry as we won't be 815 * doing it. 816 */ 817 KASSERT(rt->rt_refcnt >= 0, 818 ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt)); 819 if (ret_nrt != NULL) { 820 /* leave ref intact for return */ 821 *ret_nrt = rt; 822 } else { 823 /* deref / attempt to destroy */ 824 rtfree(rt); 825 } 826 break; 827 828 case RTM_RESOLVE: 829 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL) 830 gotoerr(EINVAL); 831 832 KASSERT(rt->rt_cpuid == mycpuid, 833 ("rt resolve rt_cpuid %d, mycpuid %d", 834 rt->rt_cpuid, mycpuid)); 835 836 ifa = rt->rt_ifa; 837 rtinfo->rti_flags = 838 rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC); 839 rtinfo->rti_flags |= RTF_WASCLONED; 840 rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway; 841 if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL) 842 rtinfo->rti_flags |= RTF_HOST; 843 rtinfo->rti_info[RTAX_MPLS1] = rt->rt_shim[0]; 844 rtinfo->rti_info[RTAX_MPLS2] = rt->rt_shim[1]; 845 rtinfo->rti_info[RTAX_MPLS3] = rt->rt_shim[2]; 846 goto makeroute; 847 848 case RTM_ADD: 849 KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) || 850 rtinfo->rti_info[RTAX_GATEWAY] != NULL, 851 ("rtrequest: GATEWAY but no gateway")); 852 853 if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo))) 854 gotoerr(error); 855 ifa = rtinfo->rti_ifa; 856 makeroute: 857 R_Malloc(rt, struct rtentry *, sizeof(struct rtentry)); 858 if (rt == NULL) { 859 if (req == RTM_ADD) { 860 kprintf("rtrequest1: alloc rtentry failed on " 861 "cpu%d\n", mycpuid); 862 } 863 gotoerr(ENOBUFS); 864 } 865 bzero(rt, sizeof(struct rtentry)); 866 rt->rt_flags = RTF_UP | rtinfo->rti_flags; 867 rt->rt_cpuid = mycpuid; 868 869 error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY]); 870 if (error != 0) { 871 Free(rt); 872 gotoerr(error); 873 } 874 875 ndst = rt_key(rt); 876 if (rtinfo->rti_info[RTAX_NETMASK] != NULL) 877 rt_maskedcopy(dst, ndst, 878 rtinfo->rti_info[RTAX_NETMASK]); 879 else 880 bcopy(dst, ndst, dst->sa_len); 881 882 if (rtinfo->rti_info[RTAX_MPLS1] != NULL) 883 rt_setshims(rt, rtinfo->rti_info); 884 885 /* 886 * Note that we now have a reference to the ifa. 887 * This moved from below so that rnh->rnh_addaddr() can 888 * examine the ifa and ifa->ifa_ifp if it so desires. 889 */ 890 IFAREF(ifa); 891 rt->rt_ifa = ifa; 892 rt->rt_ifp = ifa->ifa_ifp; 893 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 894 895 rn = rnh->rnh_addaddr((char *)ndst, 896 (char *)rtinfo->rti_info[RTAX_NETMASK], 897 rnh, rt->rt_nodes); 898 if (rn == NULL) { 899 struct rtentry *oldrt; 900 901 /* 902 * We already have one of these in the tree. 903 * We do a special hack: if the old route was 904 * cloned, then we blow it away and try 905 * re-inserting the new one. 906 */ 907 oldrt = rtpurelookup(ndst); 908 if (oldrt != NULL) { 909 --oldrt->rt_refcnt; 910 if (oldrt->rt_flags & RTF_WASCLONED) { 911 rtrequest(RTM_DELETE, rt_key(oldrt), 912 oldrt->rt_gateway, 913 rt_mask(oldrt), 914 oldrt->rt_flags, NULL); 915 rn = rnh->rnh_addaddr((char *)ndst, 916 (char *) 917 rtinfo->rti_info[RTAX_NETMASK], 918 rnh, rt->rt_nodes); 919 } 920 } 921 } 922 /* NOTE: rt_ifa may have been changed */ 923 ifa = rt->rt_ifa; 924 925 /* 926 * If it still failed to go into the tree, 927 * then un-make it (this should be a function). 928 */ 929 if (rn == NULL) { 930 if (rt->rt_gwroute != NULL) 931 rtfree(rt->rt_gwroute); 932 IFAFREE(ifa); 933 Free(rt_key(rt)); 934 Free(rt); 935 gotoerr(EEXIST); 936 } 937 938 /* 939 * If we got here from RESOLVE, then we are cloning 940 * so clone the rest, and note that we 941 * are a clone (and increment the parent's references) 942 */ 943 if (req == RTM_RESOLVE) { 944 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 945 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */ 946 if ((*ret_nrt)->rt_flags & 947 (RTF_CLONING | RTF_PRCLONING)) { 948 rt->rt_parent = *ret_nrt; 949 (*ret_nrt)->rt_refcnt++; 950 } 951 } 952 953 /* 954 * if this protocol has something to add to this then 955 * allow it to do that as well. 956 */ 957 if (ifa->ifa_rtrequest != NULL) 958 ifa->ifa_rtrequest(req, rt); 959 960 /* 961 * We repeat the same procedure from rt_setgate() here because 962 * it doesn't fire when we call it there because the node 963 * hasn't been added to the tree yet. 964 */ 965 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) && 966 rt_mask(rt) != NULL) { 967 struct rtfc_arg arg = { rt, rnh }; 968 969 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt), 970 (char *)rt_mask(rt), 971 rt_fixchange, &arg); 972 } 973 974 #ifdef ROUTE_DEBUG 975 if (route_debug) 976 rt_print(rtinfo, rt); 977 #endif 978 /* 979 * Return the resulting rtentry, 980 * increasing the number of references by one. 981 */ 982 if (ret_nrt != NULL) { 983 rt->rt_refcnt++; 984 *ret_nrt = rt; 985 } 986 break; 987 case RTM_GET: 988 /* Get the item from the tree. */ 989 rn = rnh->rnh_lookup((char *)rtinfo->rti_info[RTAX_DST], 990 (char *)rtinfo->rti_info[RTAX_NETMASK], 991 rnh); 992 if (rn == NULL) 993 gotoerr(ESRCH); 994 if (ret_nrt != NULL) { 995 rt = (struct rtentry *)rn; 996 rt->rt_refcnt++; 997 *ret_nrt = rt; 998 } 999 break; 1000 default: 1001 error = EOPNOTSUPP; 1002 } 1003 bad: 1004 #ifdef ROUTE_DEBUG 1005 if (route_debug) { 1006 if (error) 1007 kprintf("rti %p failed error %d\n", rtinfo, error); 1008 else 1009 kprintf("rti %p succeeded\n", rtinfo); 1010 } 1011 #endif 1012 crit_exit(); 1013 return (error); 1014 } 1015 1016 /* 1017 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' 1018 * (i.e., the routes related to it by the operation of cloning). This 1019 * routine is iterated over all potential former-child-routes by way of 1020 * rnh->rnh_walktree_from() above, and those that actually are children of 1021 * the late parent (passed in as VP here) are themselves deleted. 1022 */ 1023 static int 1024 rt_fixdelete(struct radix_node *rn, void *vp) 1025 { 1026 struct rtentry *rt = (struct rtentry *)rn; 1027 struct rtentry *rt0 = vp; 1028 1029 if (rt->rt_parent == rt0 && 1030 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) { 1031 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 1032 rt->rt_flags, NULL); 1033 } 1034 return 0; 1035 } 1036 1037 /* 1038 * This routine is called from rt_setgate() to do the analogous thing for 1039 * adds and changes. There is the added complication in this case of a 1040 * middle insert; i.e., insertion of a new network route between an older 1041 * network route and (cloned) host routes. For this reason, a simple check 1042 * of rt->rt_parent is insufficient; each candidate route must be tested 1043 * against the (mask, value) of the new route (passed as before in vp) 1044 * to see if the new route matches it. 1045 * 1046 * XXX - it may be possible to do fixdelete() for changes and reserve this 1047 * routine just for adds. I'm not sure why I thought it was necessary to do 1048 * changes this way. 1049 */ 1050 #ifdef DEBUG 1051 static int rtfcdebug = 0; 1052 #endif 1053 1054 static int 1055 rt_fixchange(struct radix_node *rn, void *vp) 1056 { 1057 struct rtentry *rt = (struct rtentry *)rn; 1058 struct rtfc_arg *ap = vp; 1059 struct rtentry *rt0 = ap->rt0; 1060 struct radix_node_head *rnh = ap->rnh; 1061 u_char *xk1, *xm1, *xk2, *xmp; 1062 int i, len, mlen; 1063 1064 #ifdef DEBUG 1065 if (rtfcdebug) 1066 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0); 1067 #endif 1068 1069 if (rt->rt_parent == NULL || 1070 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) { 1071 #ifdef DEBUG 1072 if (rtfcdebug) kprintf("no parent, pinned or cloning\n"); 1073 #endif 1074 return 0; 1075 } 1076 1077 if (rt->rt_parent == rt0) { 1078 #ifdef DEBUG 1079 if (rtfcdebug) kprintf("parent match\n"); 1080 #endif 1081 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 1082 rt->rt_flags, NULL); 1083 } 1084 1085 /* 1086 * There probably is a function somewhere which does this... 1087 * if not, there should be. 1088 */ 1089 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len); 1090 1091 xk1 = (u_char *)rt_key(rt0); 1092 xm1 = (u_char *)rt_mask(rt0); 1093 xk2 = (u_char *)rt_key(rt); 1094 1095 /* avoid applying a less specific route */ 1096 xmp = (u_char *)rt_mask(rt->rt_parent); 1097 mlen = rt_key(rt->rt_parent)->sa_len; 1098 if (mlen > rt_key(rt0)->sa_len) { 1099 #ifdef DEBUG 1100 if (rtfcdebug) 1101 kprintf("rt_fixchange: inserting a less " 1102 "specific route\n"); 1103 #endif 1104 return 0; 1105 } 1106 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) { 1107 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) { 1108 #ifdef DEBUG 1109 if (rtfcdebug) 1110 kprintf("rt_fixchange: inserting a less " 1111 "specific route\n"); 1112 #endif 1113 return 0; 1114 } 1115 } 1116 1117 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) { 1118 if ((xk2[i] & xm1[i]) != xk1[i]) { 1119 #ifdef DEBUG 1120 if (rtfcdebug) kprintf("no match\n"); 1121 #endif 1122 return 0; 1123 } 1124 } 1125 1126 /* 1127 * OK, this node is a clone, and matches the node currently being 1128 * changed/added under the node's mask. So, get rid of it. 1129 */ 1130 #ifdef DEBUG 1131 if (rtfcdebug) kprintf("deleting\n"); 1132 #endif 1133 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 1134 rt->rt_flags, NULL); 1135 } 1136 1137 int 1138 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate) 1139 { 1140 char *space, *oldspace; 1141 int dlen = RT_ROUNDUP(dst->sa_len), glen = RT_ROUNDUP(gate->sa_len); 1142 struct rtentry *rt = rt0; 1143 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family]; 1144 1145 ASSERT_NETISR_NCPUS(mycpuid); 1146 1147 /* 1148 * A host route with the destination equal to the gateway 1149 * will interfere with keeping LLINFO in the routing 1150 * table, so disallow it. 1151 */ 1152 if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) == 1153 (RTF_HOST | RTF_GATEWAY)) && 1154 dst->sa_len == gate->sa_len && 1155 sa_equal(dst, gate)) { 1156 /* 1157 * The route might already exist if this is an RTM_CHANGE 1158 * or a routing redirect, so try to delete it. 1159 */ 1160 if (rt_key(rt0) != NULL) 1161 rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway, 1162 rt_mask(rt0), rt0->rt_flags, NULL); 1163 return EADDRNOTAVAIL; 1164 } 1165 1166 /* 1167 * Both dst and gateway are stored in the same malloc'ed chunk 1168 * (If I ever get my hands on....) 1169 * if we need to malloc a new chunk, then keep the old one around 1170 * till we don't need it any more. 1171 */ 1172 if (rt->rt_gateway == NULL || 1173 glen > RT_ROUNDUP(rt->rt_gateway->sa_len)) { 1174 oldspace = (char *)rt_key(rt); 1175 R_Malloc(space, char *, dlen + glen); 1176 if (space == NULL) 1177 return ENOBUFS; 1178 rt->rt_nodes->rn_key = space; 1179 } else { 1180 space = (char *)rt_key(rt); /* Just use the old space. */ 1181 oldspace = NULL; 1182 } 1183 1184 /* Set the gateway value. */ 1185 rt->rt_gateway = (struct sockaddr *)(space + dlen); 1186 bcopy(gate, rt->rt_gateway, glen); 1187 1188 if (oldspace != NULL) { 1189 /* 1190 * If we allocated a new chunk, preserve the original dst. 1191 * This way, rt_setgate() really just sets the gate 1192 * and leaves the dst field alone. 1193 */ 1194 bcopy(dst, space, dlen); 1195 Free(oldspace); 1196 } 1197 1198 /* 1199 * If there is already a gwroute, it's now almost definitely wrong 1200 * so drop it. 1201 */ 1202 if (rt->rt_gwroute != NULL) { 1203 RTFREE(rt->rt_gwroute); 1204 rt->rt_gwroute = NULL; 1205 } 1206 if (rt->rt_flags & RTF_GATEWAY) { 1207 /* 1208 * Cloning loop avoidance: In the presence of 1209 * protocol-cloning and bad configuration, it is 1210 * possible to get stuck in bottomless mutual recursion 1211 * (rtrequest rt_setgate rtlookup). We avoid this 1212 * by not allowing protocol-cloning to operate for 1213 * gateways (which is probably the correct choice 1214 * anyway), and avoid the resulting reference loops 1215 * by disallowing any route to run through itself as 1216 * a gateway. This is obviously mandatory when we 1217 * get rt->rt_output(). 1218 * 1219 * This breaks TTCP for hosts outside the gateway! XXX JH 1220 */ 1221 rt->rt_gwroute = _rtlookup(gate, RTF_PRCLONING); 1222 if (rt->rt_gwroute == rt) { 1223 rt->rt_gwroute = NULL; 1224 --rt->rt_refcnt; 1225 return EDQUOT; /* failure */ 1226 } 1227 } 1228 1229 /* 1230 * This isn't going to do anything useful for host routes, so 1231 * don't bother. Also make sure we have a reasonable mask 1232 * (we don't yet have one during adds). 1233 */ 1234 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) { 1235 struct rtfc_arg arg = { rt, rnh }; 1236 1237 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt), 1238 (char *)rt_mask(rt), 1239 rt_fixchange, &arg); 1240 } 1241 1242 return 0; 1243 } 1244 1245 static void 1246 rt_maskedcopy( 1247 struct sockaddr *src, 1248 struct sockaddr *dst, 1249 struct sockaddr *netmask) 1250 { 1251 u_char *cp1 = (u_char *)src; 1252 u_char *cp2 = (u_char *)dst; 1253 u_char *cp3 = (u_char *)netmask; 1254 u_char *cplim = cp2 + *cp3; 1255 u_char *cplim2 = cp2 + *cp1; 1256 1257 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1258 cp3 += 2; 1259 if (cplim > cplim2) 1260 cplim = cplim2; 1261 while (cp2 < cplim) 1262 *cp2++ = *cp1++ & *cp3++; 1263 if (cp2 < cplim2) 1264 bzero(cp2, cplim2 - cp2); 1265 } 1266 1267 int 1268 rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt) 1269 { 1270 struct rtentry *up_rt, *rt; 1271 1272 ASSERT_NETISR_NCPUS(mycpuid); 1273 1274 if (!(rt0->rt_flags & RTF_UP)) { 1275 up_rt = rtlookup(dst); 1276 if (up_rt == NULL) 1277 return (EHOSTUNREACH); 1278 up_rt->rt_refcnt--; 1279 } else 1280 up_rt = rt0; 1281 if (up_rt->rt_flags & RTF_GATEWAY) { 1282 if (up_rt->rt_gwroute == NULL) { 1283 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway); 1284 if (up_rt->rt_gwroute == NULL) 1285 return (EHOSTUNREACH); 1286 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) { 1287 rtfree(up_rt->rt_gwroute); 1288 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway); 1289 if (up_rt->rt_gwroute == NULL) 1290 return (EHOSTUNREACH); 1291 } 1292 rt = up_rt->rt_gwroute; 1293 } else 1294 rt = up_rt; 1295 if (rt->rt_flags & RTF_REJECT && 1296 (rt->rt_rmx.rmx_expire == 0 || /* rt doesn't expire */ 1297 time_uptime < rt->rt_rmx.rmx_expire)) /* rt not expired */ 1298 return (rt->rt_flags & RTF_HOST ? EHOSTDOWN : EHOSTUNREACH); 1299 *drt = rt; 1300 return 0; 1301 } 1302 1303 static int 1304 rt_setshims(struct rtentry *rt, struct sockaddr **rt_shim){ 1305 int i; 1306 1307 for (i=0; i<3; i++) { 1308 struct sockaddr *shim = rt_shim[RTAX_MPLS1 + i]; 1309 int shimlen; 1310 1311 if (shim == NULL) 1312 break; 1313 1314 shimlen = RT_ROUNDUP(shim->sa_len); 1315 R_Malloc(rt->rt_shim[i], struct sockaddr *, shimlen); 1316 bcopy(shim, rt->rt_shim[i], shimlen); 1317 } 1318 1319 return 0; 1320 } 1321 1322 #ifdef ROUTE_DEBUG 1323 1324 /* 1325 * Print out a route table entry 1326 */ 1327 void 1328 rt_print(struct rt_addrinfo *rtinfo, struct rtentry *rn) 1329 { 1330 kprintf("rti %p cpu %d route %p flags %08lx: ", 1331 rtinfo, mycpuid, rn, rn->rt_flags); 1332 sockaddr_print(rt_key(rn)); 1333 kprintf(" mask "); 1334 sockaddr_print(rt_mask(rn)); 1335 kprintf(" gw "); 1336 sockaddr_print(rn->rt_gateway); 1337 kprintf(" ifc \"%s\"", rn->rt_ifp ? rn->rt_ifp->if_dname : "?"); 1338 kprintf(" ifa %p\n", rn->rt_ifa); 1339 } 1340 1341 void 1342 rt_addrinfo_print(int cmd, struct rt_addrinfo *rti) 1343 { 1344 int didit = 0; 1345 int i; 1346 1347 #ifdef ROUTE_DEBUG 1348 if (cmd == RTM_DELETE && route_debug > 1) 1349 print_backtrace(-1); 1350 #endif 1351 1352 switch(cmd) { 1353 case RTM_ADD: 1354 kprintf("ADD "); 1355 break; 1356 case RTM_RESOLVE: 1357 kprintf("RES "); 1358 break; 1359 case RTM_DELETE: 1360 kprintf("DEL "); 1361 break; 1362 default: 1363 kprintf("C%02d ", cmd); 1364 break; 1365 } 1366 kprintf("rti %p cpu %d ", rti, mycpuid); 1367 for (i = 0; i < rti->rti_addrs; ++i) { 1368 if (rti->rti_info[i] == NULL) 1369 continue; 1370 if (didit) 1371 kprintf(" ,"); 1372 switch(i) { 1373 case RTAX_DST: 1374 kprintf("(DST "); 1375 break; 1376 case RTAX_GATEWAY: 1377 kprintf("(GWY "); 1378 break; 1379 case RTAX_NETMASK: 1380 kprintf("(MSK "); 1381 break; 1382 case RTAX_GENMASK: 1383 kprintf("(GEN "); 1384 break; 1385 case RTAX_IFP: 1386 kprintf("(IFP "); 1387 break; 1388 case RTAX_IFA: 1389 kprintf("(IFA "); 1390 break; 1391 case RTAX_AUTHOR: 1392 kprintf("(AUT "); 1393 break; 1394 case RTAX_BRD: 1395 kprintf("(BRD "); 1396 break; 1397 default: 1398 kprintf("(?%02d ", i); 1399 break; 1400 } 1401 sockaddr_print(rti->rti_info[i]); 1402 kprintf(")"); 1403 didit = 1; 1404 } 1405 kprintf("\n"); 1406 } 1407 1408 void 1409 sockaddr_print(struct sockaddr *sa) 1410 { 1411 struct sockaddr_in *sa4; 1412 struct sockaddr_in6 *sa6; 1413 int len; 1414 int i; 1415 1416 if (sa == NULL) { 1417 kprintf("NULL"); 1418 return; 1419 } 1420 1421 len = sa->sa_len - offsetof(struct sockaddr, sa_data[0]); 1422 1423 switch(sa->sa_family) { 1424 case AF_INET: 1425 case AF_INET6: 1426 default: 1427 switch(sa->sa_family) { 1428 case AF_INET: 1429 sa4 = (struct sockaddr_in *)sa; 1430 kprintf("INET %d %d.%d.%d.%d", 1431 ntohs(sa4->sin_port), 1432 (ntohl(sa4->sin_addr.s_addr) >> 24) & 255, 1433 (ntohl(sa4->sin_addr.s_addr) >> 16) & 255, 1434 (ntohl(sa4->sin_addr.s_addr) >> 8) & 255, 1435 (ntohl(sa4->sin_addr.s_addr) >> 0) & 255 1436 ); 1437 break; 1438 case AF_INET6: 1439 sa6 = (struct sockaddr_in6 *)sa; 1440 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x", 1441 ntohs(sa6->sin6_port), 1442 sa6->sin6_addr.s6_addr16[0], 1443 sa6->sin6_addr.s6_addr16[1], 1444 sa6->sin6_addr.s6_addr16[2], 1445 sa6->sin6_addr.s6_addr16[3], 1446 sa6->sin6_addr.s6_addr16[4], 1447 sa6->sin6_addr.s6_addr16[5], 1448 sa6->sin6_addr.s6_addr16[6], 1449 sa6->sin6_addr.s6_addr16[7] 1450 ); 1451 break; 1452 default: 1453 kprintf("AF%d ", sa->sa_family); 1454 while (len > 0 && sa->sa_data[len-1] == 0) 1455 --len; 1456 1457 for (i = 0; i < len; ++i) { 1458 if (i) 1459 kprintf("."); 1460 kprintf("%d", (unsigned char)sa->sa_data[i]); 1461 } 1462 break; 1463 } 1464 } 1465 } 1466 1467 #endif 1468 1469 /* 1470 * Set up a routing table entry, normally for an interface. 1471 */ 1472 int 1473 rtinit(struct ifaddr *ifa, int cmd, int flags) 1474 { 1475 struct sockaddr *dst, *deldst, *netmask; 1476 struct mbuf *m = NULL; 1477 struct radix_node_head *rnh; 1478 struct radix_node *rn; 1479 struct rt_addrinfo rtinfo; 1480 int error; 1481 1482 ASSERT_NETISR0; 1483 1484 if (flags & RTF_HOST) { 1485 dst = ifa->ifa_dstaddr; 1486 netmask = NULL; 1487 } else { 1488 dst = ifa->ifa_addr; 1489 netmask = ifa->ifa_netmask; 1490 } 1491 /* 1492 * If it's a delete, check that if it exists, it's on the correct 1493 * interface or we might scrub a route to another ifa which would 1494 * be confusing at best and possibly worse. 1495 */ 1496 if (cmd == RTM_DELETE) { 1497 /* 1498 * It's a delete, so it should already exist.. 1499 * If it's a net, mask off the host bits 1500 * (Assuming we have a mask) 1501 */ 1502 if (netmask != NULL) { 1503 m = m_get(M_NOWAIT, MT_SONAME); 1504 if (m == NULL) 1505 return (ENOBUFS); 1506 mbuftrackid(m, 34); 1507 deldst = mtod(m, struct sockaddr *); 1508 rt_maskedcopy(dst, deldst, netmask); 1509 dst = deldst; 1510 } 1511 /* 1512 * Look up an rtentry that is in the routing tree and 1513 * contains the correct info. 1514 */ 1515 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL || 1516 (rn = rnh->rnh_lookup((char *)dst, 1517 (char *)netmask, rnh)) == NULL || 1518 ((struct rtentry *)rn)->rt_ifa != ifa || 1519 !sa_equal((struct sockaddr *)rn->rn_key, dst)) { 1520 if (m != NULL) 1521 m_free(m); 1522 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 1523 } 1524 /* XXX */ 1525 #if 0 1526 else { 1527 /* 1528 * One would think that as we are deleting, and we know 1529 * it doesn't exist, we could just return at this point 1530 * with an "ELSE" clause, but apparently not.. 1531 */ 1532 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH); 1533 } 1534 #endif 1535 } 1536 /* 1537 * Do the actual request 1538 */ 1539 bzero(&rtinfo, sizeof(struct rt_addrinfo)); 1540 rtinfo.rti_info[RTAX_DST] = dst; 1541 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1542 rtinfo.rti_info[RTAX_NETMASK] = netmask; 1543 rtinfo.rti_flags = flags | ifa->ifa_flags; 1544 rtinfo.rti_ifa = ifa; 1545 error = rtrequest1_global(cmd, &rtinfo, rtinit_rtrequest_callback, ifa, 1546 RTREQ_PRIO_HIGH); 1547 if (m != NULL) 1548 m_free(m); 1549 return (error); 1550 } 1551 1552 static void 1553 rtinit_rtrequest_callback(int cmd, int error, 1554 struct rt_addrinfo *rtinfo, struct rtentry *rt, 1555 void *arg) 1556 { 1557 struct ifaddr *ifa = arg; 1558 1559 if (error == 0 && rt) { 1560 if (mycpuid == 0) 1561 rt_newaddrmsg(cmd, ifa, error, rt); 1562 if (cmd == RTM_DELETE) { 1563 if (rt->rt_refcnt == 0) { 1564 ++rt->rt_refcnt; 1565 rtfree(rt); 1566 } 1567 } 1568 } 1569 } 1570 1571 struct netmsg_rts { 1572 struct netmsg_base base; 1573 int req; 1574 struct rt_addrinfo *rtinfo; 1575 rtsearch_callback_func_t callback; 1576 void *arg; 1577 boolean_t exact_match; 1578 int found_cnt; 1579 }; 1580 1581 int 1582 rtsearch_global(int req, struct rt_addrinfo *rtinfo, 1583 rtsearch_callback_func_t callback, void *arg, boolean_t exact_match, 1584 boolean_t req_prio) 1585 { 1586 struct netmsg_rts msg; 1587 int flags = 0; 1588 1589 if (req_prio) 1590 flags = MSGF_PRIORITY; 1591 netmsg_init(&msg.base, NULL, &curthread->td_msgport, flags, 1592 rtsearch_msghandler); 1593 msg.req = req; 1594 msg.rtinfo = rtinfo; 1595 msg.callback = callback; 1596 msg.arg = arg; 1597 msg.exact_match = exact_match; 1598 msg.found_cnt = 0; 1599 return (netisr_domsg_global(&msg.base)); 1600 } 1601 1602 static void 1603 rtsearch_msghandler(netmsg_t msg) 1604 { 1605 struct netmsg_rts *rmsg = (void *)msg; 1606 struct rt_addrinfo rtinfo; 1607 struct radix_node_head *rnh; 1608 struct rtentry *rt; 1609 int error; 1610 1611 ASSERT_NETISR_NCPUS(mycpuid); 1612 1613 /* 1614 * Copy the rtinfo. We need to make sure that the original 1615 * rtinfo, which is setup by the caller, in the netmsg will 1616 * _not_ be changed; else the next CPU on the netmsg forwarding 1617 * path will see a different rtinfo than what this CPU has seen. 1618 */ 1619 rtinfo = *rmsg->rtinfo; 1620 1621 /* 1622 * Find the correct routing tree to use for this Address Family 1623 */ 1624 if ((rnh = rt_tables[mycpuid][rtinfo.rti_dst->sa_family]) == NULL) { 1625 if (mycpuid != 0) 1626 panic("partially initialized routing tables"); 1627 netisr_replymsg(&rmsg->base, EAFNOSUPPORT); 1628 return; 1629 } 1630 1631 /* 1632 * Correct rtinfo for the host route searching. 1633 */ 1634 if (rtinfo.rti_flags & RTF_HOST) { 1635 rtinfo.rti_netmask = NULL; 1636 rtinfo.rti_flags &= ~(RTF_CLONING | RTF_PRCLONING); 1637 } 1638 1639 rt = (struct rtentry *) 1640 rnh->rnh_lookup((char *)rtinfo.rti_dst, 1641 (char *)rtinfo.rti_netmask, rnh); 1642 1643 /* 1644 * If we are asked to do the "exact match", we need to make sure 1645 * that host route searching got a host route while a network 1646 * route searching got a network route. 1647 */ 1648 if (rt != NULL && rmsg->exact_match && 1649 ((rt->rt_flags ^ rtinfo.rti_flags) & RTF_HOST)) 1650 rt = NULL; 1651 1652 if (rt == NULL) { 1653 /* 1654 * No matching routes have been found, don't count this 1655 * as a critical error (here, we set 'error' to 0), just 1656 * keep moving on, since at least prcloned routes are not 1657 * duplicated onto each CPU. 1658 */ 1659 error = 0; 1660 } else { 1661 rmsg->found_cnt++; 1662 1663 rt->rt_refcnt++; 1664 error = rmsg->callback(rmsg->req, &rtinfo, rt, rmsg->arg, 1665 rmsg->found_cnt); 1666 rt->rt_refcnt--; 1667 1668 if (error == EJUSTRETURN) { 1669 netisr_replymsg(&rmsg->base, 0); 1670 return; 1671 } 1672 } 1673 1674 if (error) { 1675 KKASSERT(rmsg->found_cnt > 0); 1676 1677 /* 1678 * Under following cases, unrecoverable error has 1679 * not occured: 1680 * o Request is RTM_GET 1681 * o The first time that we find the route, but the 1682 * modification fails. 1683 */ 1684 if (rmsg->req != RTM_GET && rmsg->found_cnt > 1) { 1685 panic("rtsearch_msghandler: unrecoverable error " 1686 "cpu %d", mycpuid); 1687 } 1688 netisr_replymsg(&rmsg->base, error); 1689 } else { 1690 if (rmsg->found_cnt == 0) { 1691 /* The requested route has not been seen ... */ 1692 error = ESRCH; 1693 } 1694 netisr_forwardmsg_error(&rmsg->base, mycpuid + 1, error); 1695 } 1696 } 1697 1698 int 1699 rtmask_add_global(struct sockaddr *mask, boolean_t req_prio) 1700 { 1701 struct netmsg_base msg; 1702 int flags = 0; 1703 1704 if (req_prio) 1705 flags = MSGF_PRIORITY; 1706 netmsg_init(&msg, NULL, &curthread->td_msgport, flags, 1707 rtmask_add_msghandler); 1708 msg.lmsg.u.ms_resultp = mask; 1709 1710 return (netisr_domsg_global(&msg)); 1711 } 1712 1713 struct sockaddr * 1714 _rtmask_lookup(struct sockaddr *mask, boolean_t search) 1715 { 1716 struct radix_node *n; 1717 1718 #define clen(s) (*(u_char *)(s)) 1719 n = rn_addmask((char *)mask, search, 1, rn_cpumaskhead(mycpuid)); 1720 if (n != NULL && 1721 mask->sa_len >= clen(n->rn_key) && 1722 bcmp((char *)mask + 1, 1723 (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0) { 1724 return (struct sockaddr *)n->rn_key; 1725 } else { 1726 return NULL; 1727 } 1728 #undef clen 1729 } 1730 1731 static void 1732 rtmask_add_msghandler(netmsg_t msg) 1733 { 1734 struct sockaddr *mask = msg->lmsg.u.ms_resultp; 1735 1736 ASSERT_NETISR_NCPUS(mycpuid); 1737 1738 if (rtmask_lookup(mask) == NULL) { 1739 netisr_replymsg(&msg->base, ENOBUFS); 1740 return; 1741 } 1742 netisr_forwardmsg(&msg->base, mycpuid + 1); 1743 } 1744 1745 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */ 1746 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0); 1747 1748 struct rtchange_arg { 1749 struct ifaddr *old_ifa; 1750 struct ifaddr *new_ifa; 1751 struct rtentry *rt; 1752 int changed; 1753 }; 1754 1755 static void 1756 rtchange_ifa(struct rtentry *rt, struct rtchange_arg *ap) 1757 { 1758 if (rt->rt_ifa->ifa_rtrequest != NULL) 1759 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt); 1760 IFAFREE(rt->rt_ifa); 1761 1762 IFAREF(ap->new_ifa); 1763 rt->rt_ifa = ap->new_ifa; 1764 rt->rt_ifp = ap->new_ifa->ifa_ifp; 1765 if (rt->rt_ifa->ifa_rtrequest != NULL) 1766 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt); 1767 1768 ap->changed = 1; 1769 } 1770 1771 static int 1772 rtchange_callback(struct radix_node *rn, void *xap) 1773 { 1774 struct rtchange_arg *ap = xap; 1775 struct rtentry *rt = (struct rtentry *)rn; 1776 1777 if (rt->rt_ifa == ap->old_ifa) { 1778 if (rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) { 1779 /* 1780 * We could saw the branch off when we are 1781 * still sitting on it, if the ifa_rtrequest 1782 * DEL/ADD are called directly from here. 1783 */ 1784 ap->rt = rt; 1785 return EJUSTRETURN; 1786 } 1787 rtchange_ifa(rt, ap); 1788 } 1789 return 0; 1790 } 1791 1792 struct netmsg_rtchange { 1793 struct netmsg_base base; 1794 struct ifaddr *old_ifa; 1795 struct ifaddr *new_ifa; 1796 int changed; 1797 }; 1798 1799 static void 1800 rtchange_dispatch(netmsg_t msg) 1801 { 1802 struct netmsg_rtchange *rmsg = (void *)msg; 1803 struct radix_node_head *rnh; 1804 struct rtchange_arg arg; 1805 int cpu; 1806 1807 cpu = mycpuid; 1808 ASSERT_NETISR_NCPUS(cpu); 1809 1810 memset(&arg, 0, sizeof(arg)); 1811 arg.old_ifa = rmsg->old_ifa; 1812 arg.new_ifa = rmsg->new_ifa; 1813 1814 rnh = rt_tables[cpu][AF_INET]; 1815 for (;;) { 1816 int error; 1817 1818 KKASSERT(arg.rt == NULL); 1819 error = rnh->rnh_walktree(rnh, rtchange_callback, &arg); 1820 if (arg.rt != NULL) { 1821 struct rtentry *rt; 1822 1823 rt = arg.rt; 1824 arg.rt = NULL; 1825 rtchange_ifa(rt, &arg); 1826 } else { 1827 break; 1828 } 1829 } 1830 if (arg.changed) 1831 rmsg->changed = 1; 1832 1833 netisr_forwardmsg(&rmsg->base, cpu + 1); 1834 } 1835 1836 int 1837 rtchange(struct ifaddr *old_ifa, struct ifaddr *new_ifa) 1838 { 1839 struct netmsg_rtchange msg; 1840 1841 /* 1842 * XXX individual requests are not independantly chained, 1843 * which means that the per-cpu route tables will not be 1844 * consistent in the middle of the operation. If routes 1845 * related to the interface are manipulated while we are 1846 * doing this the inconsistancy could trigger a panic. 1847 */ 1848 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY, 1849 rtchange_dispatch); 1850 msg.old_ifa = old_ifa; 1851 msg.new_ifa = new_ifa; 1852 msg.changed = 0; 1853 netisr_domsg_global(&msg.base); 1854 1855 if (msg.changed) { 1856 old_ifa->ifa_flags &= ~IFA_ROUTE; 1857 new_ifa->ifa_flags |= IFA_ROUTE; 1858 return 0; 1859 } else { 1860 return ENOENT; 1861 } 1862 } 1863