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