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