1 /*- 2 * Copyright (c) 1980, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95 30 * $FreeBSD$ 31 */ 32 /************************************************************************ 33 * Note: In this file a 'fib' is a "forwarding information base" * 34 * Which is the new name for an in kernel routing (next hop) table. * 35 ***********************************************************************/ 36 37 #include "opt_inet.h" 38 #include "opt_route.h" 39 #include "opt_mrouting.h" 40 #include "opt_mpath.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/malloc.h> 45 #include <sys/mbuf.h> 46 #include <sys/socket.h> 47 #include <sys/sysctl.h> 48 #include <sys/sysproto.h> 49 #include <sys/proc.h> 50 #include <sys/domain.h> 51 #include <sys/kernel.h> 52 #include <sys/vimage.h> 53 54 #include <net/if.h> 55 #include <net/route.h> 56 57 #ifdef RADIX_MPATH 58 #include <net/radix_mpath.h> 59 #endif 60 61 #include <netinet/in.h> 62 #include <netinet/ip_mroute.h> 63 64 #include <vm/uma.h> 65 66 u_int rt_numfibs = RT_NUMFIBS; 67 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, ""); 68 /* 69 * Allow the boot code to allow LESS than RT_MAXFIBS to be used. 70 * We can't do more because storage is statically allocated for now. 71 * (for compatibility reasons.. this will change). 72 */ 73 TUNABLE_INT("net.fibs", &rt_numfibs); 74 75 /* 76 * By default add routes to all fibs for new interfaces. 77 * Once this is set to 0 then only allocate routes on interface 78 * changes for the FIB of the caller when adding a new set of addresses 79 * to an interface. XXX this is a shotgun aproach to a problem that needs 80 * a more fine grained solution.. that will come. 81 */ 82 u_int rt_add_addr_allfibs = 1; 83 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW, 84 &rt_add_addr_allfibs, 0, ""); 85 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs); 86 87 #ifdef VIMAGE_GLOBALS 88 static struct rtstat rtstat; 89 90 /* by default only the first 'row' of tables will be accessed. */ 91 /* 92 * XXXMRT When we fix netstat, and do this differnetly, 93 * we can allocate this dynamically. As long as we are keeping 94 * things backwards compaitble we need to allocate this 95 * statically. 96 */ 97 struct radix_node_head *rt_tables[RT_MAXFIBS][AF_MAX+1]; 98 99 static int rttrash; /* routes not in table but not freed */ 100 #endif 101 102 static void rt_maskedcopy(struct sockaddr *, 103 struct sockaddr *, struct sockaddr *); 104 105 /* compare two sockaddr structures */ 106 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 107 108 /* 109 * Convert a 'struct radix_node *' to a 'struct rtentry *'. 110 * The operation can be done safely (in this code) because a 111 * 'struct rtentry' starts with two 'struct radix_node''s, the first 112 * one representing leaf nodes in the routing tree, which is 113 * what the code in radix.c passes us as a 'struct radix_node'. 114 * 115 * But because there are a lot of assumptions in this conversion, 116 * do not cast explicitly, but always use the macro below. 117 */ 118 #define RNTORT(p) ((struct rtentry *)(p)) 119 120 static uma_zone_t rtzone; /* Routing table UMA zone. */ 121 122 #if 0 123 /* default fib for tunnels to use */ 124 u_int tunnel_fib = 0; 125 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, ""); 126 #endif 127 128 /* 129 * handler for net.my_fibnum 130 */ 131 static int 132 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS) 133 { 134 int fibnum; 135 int error; 136 137 fibnum = curthread->td_proc->p_fibnum; 138 error = sysctl_handle_int(oidp, &fibnum, 0, req); 139 return (error); 140 } 141 142 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD, 143 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller"); 144 145 static void 146 route_init(void) 147 { 148 INIT_VNET_INET(curvnet); 149 int table; 150 struct domain *dom; 151 int fam; 152 153 /* whack the tunable ints into line. */ 154 if (rt_numfibs > RT_MAXFIBS) 155 rt_numfibs = RT_MAXFIBS; 156 if (rt_numfibs == 0) 157 rt_numfibs = 1; 158 rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL, 159 NULL, NULL, UMA_ALIGN_PTR, 0); 160 rn_init(); /* initialize all zeroes, all ones, mask table */ 161 162 for (dom = domains; dom; dom = dom->dom_next) { 163 if (dom->dom_rtattach) { 164 for (table = 0; table < rt_numfibs; table++) { 165 if ( (fam = dom->dom_family) == AF_INET || 166 table == 0) { 167 /* for now only AF_INET has > 1 table */ 168 /* XXX MRT 169 * rtattach will be also called 170 * from vfs_export.c but the 171 * offset will be 0 172 * (only for AF_INET and AF_INET6 173 * which don't need it anyhow) 174 */ 175 dom->dom_rtattach( 176 (void **)&V_rt_tables[table][fam], 177 dom->dom_rtoffset); 178 } else { 179 break; 180 } 181 } 182 } 183 } 184 } 185 186 #ifndef _SYS_SYSPROTO_H_ 187 struct setfib_args { 188 int fibnum; 189 }; 190 #endif 191 int 192 setfib(struct thread *td, struct setfib_args *uap) 193 { 194 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs) 195 return EINVAL; 196 td->td_proc->p_fibnum = uap->fibnum; 197 return (0); 198 } 199 200 /* 201 * Packet routing routines. 202 */ 203 void 204 rtalloc(struct route *ro) 205 { 206 rtalloc_ign_fib(ro, 0UL, 0); 207 } 208 209 void 210 rtalloc_fib(struct route *ro, u_int fibnum) 211 { 212 rtalloc_ign_fib(ro, 0UL, fibnum); 213 } 214 215 void 216 rtalloc_ign(struct route *ro, u_long ignore) 217 { 218 struct rtentry *rt; 219 220 if ((rt = ro->ro_rt) != NULL) { 221 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 222 return; 223 RTFREE(rt); 224 ro->ro_rt = NULL; 225 } 226 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0); 227 if (ro->ro_rt) 228 RT_UNLOCK(ro->ro_rt); 229 } 230 231 void 232 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum) 233 { 234 struct rtentry *rt; 235 236 if ((rt = ro->ro_rt) != NULL) { 237 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 238 return; 239 RTFREE(rt); 240 ro->ro_rt = NULL; 241 } 242 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum); 243 if (ro->ro_rt) 244 RT_UNLOCK(ro->ro_rt); 245 } 246 247 /* 248 * Look up the route that matches the address given 249 * Or, at least try.. Create a cloned route if needed. 250 * 251 * The returned route, if any, is locked. 252 */ 253 struct rtentry * 254 rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 255 { 256 return (rtalloc1_fib(dst, report, ignflags, 0)); 257 } 258 259 struct rtentry * 260 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags, 261 u_int fibnum) 262 { 263 INIT_VNET_NET(curvnet); 264 struct radix_node_head *rnh; 265 struct rtentry *rt; 266 struct radix_node *rn; 267 struct rtentry *newrt; 268 struct rt_addrinfo info; 269 u_long nflags; 270 int err = 0, msgtype = RTM_MISS; 271 272 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum")); 273 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */ 274 fibnum = 0; 275 rnh = V_rt_tables[fibnum][dst->sa_family]; 276 newrt = NULL; 277 /* 278 * Look up the address in the table for that Address Family 279 */ 280 if (rnh == NULL) { 281 V_rtstat.rts_unreach++; 282 goto miss2; 283 } 284 RADIX_NODE_HEAD_LOCK(rnh); 285 if ((rn = rnh->rnh_matchaddr(dst, rnh)) && 286 (rn->rn_flags & RNF_ROOT) == 0) { 287 /* 288 * If we find it and it's not the root node, then 289 * get a reference on the rtentry associated. 290 */ 291 newrt = rt = RNTORT(rn); 292 nflags = rt->rt_flags & ~ignflags; 293 if (report && (nflags & RTF_CLONING)) { 294 /* 295 * We are apparently adding (report = 0 in delete). 296 * If it requires that it be cloned, do so. 297 * (This implies it wasn't a HOST route.) 298 */ 299 err = rtrequest_fib(RTM_RESOLVE, dst, NULL, 300 NULL, 0, &newrt, fibnum); 301 if (err) { 302 /* 303 * If the cloning didn't succeed, maybe 304 * what we have will do. Return that. 305 */ 306 newrt = rt; /* existing route */ 307 RT_LOCK(newrt); 308 RT_ADDREF(newrt); 309 goto miss; 310 } 311 KASSERT(newrt, ("no route and no error")); 312 RT_LOCK(newrt); 313 if (newrt->rt_flags & RTF_XRESOLVE) { 314 /* 315 * If the new route specifies it be 316 * externally resolved, then go do that. 317 */ 318 msgtype = RTM_RESOLVE; 319 goto miss; 320 } 321 /* Inform listeners of the new route. */ 322 bzero(&info, sizeof(info)); 323 info.rti_info[RTAX_DST] = rt_key(newrt); 324 info.rti_info[RTAX_NETMASK] = rt_mask(newrt); 325 info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway; 326 if (newrt->rt_ifp != NULL) { 327 info.rti_info[RTAX_IFP] = 328 newrt->rt_ifp->if_addr->ifa_addr; 329 info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr; 330 } 331 rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0); 332 } else { 333 RT_LOCK(newrt); 334 RT_ADDREF(newrt); 335 } 336 RADIX_NODE_HEAD_UNLOCK(rnh); 337 } else { 338 /* 339 * Either we hit the root or couldn't find any match, 340 * Which basically means 341 * "caint get there frm here" 342 */ 343 V_rtstat.rts_unreach++; 344 miss: 345 RADIX_NODE_HEAD_UNLOCK(rnh); 346 miss2: if (report) { 347 /* 348 * If required, report the failure to the supervising 349 * Authorities. 350 * For a delete, this is not an error. (report == 0) 351 */ 352 bzero(&info, sizeof(info)); 353 info.rti_info[RTAX_DST] = dst; 354 rt_missmsg(msgtype, &info, 0, err); 355 } 356 } 357 if (newrt) 358 RT_LOCK_ASSERT(newrt); 359 return (newrt); 360 } 361 362 /* 363 * Remove a reference count from an rtentry. 364 * If the count gets low enough, take it out of the routing table 365 */ 366 void 367 rtfree(struct rtentry *rt) 368 { 369 INIT_VNET_NET(curvnet); 370 struct radix_node_head *rnh; 371 372 KASSERT(rt != NULL,("%s: NULL rt", __func__)); 373 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family]; 374 KASSERT(rnh != NULL,("%s: NULL rnh", __func__)); 375 376 RT_LOCK_ASSERT(rt); 377 378 /* 379 * The callers should use RTFREE_LOCKED() or RTFREE(), so 380 * we should come here exactly with the last reference. 381 */ 382 RT_REMREF(rt); 383 if (rt->rt_refcnt > 0) { 384 printf("%s: %p has %lu refs\n", __func__, rt, rt->rt_refcnt); 385 goto done; 386 } 387 388 /* 389 * On last reference give the "close method" a chance 390 * to cleanup private state. This also permits (for 391 * IPv4 and IPv6) a chance to decide if the routing table 392 * entry should be purged immediately or at a later time. 393 * When an immediate purge is to happen the close routine 394 * typically calls rtexpunge which clears the RTF_UP flag 395 * on the entry so that the code below reclaims the storage. 396 */ 397 if (rt->rt_refcnt == 0 && rnh->rnh_close) 398 rnh->rnh_close((struct radix_node *)rt, rnh); 399 400 /* 401 * If we are no longer "up" (and ref == 0) 402 * then we can free the resources associated 403 * with the route. 404 */ 405 if ((rt->rt_flags & RTF_UP) == 0) { 406 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 407 panic("rtfree 2"); 408 /* 409 * the rtentry must have been removed from the routing table 410 * so it is represented in rttrash.. remove that now. 411 */ 412 V_rttrash--; 413 #ifdef DIAGNOSTIC 414 if (rt->rt_refcnt < 0) { 415 printf("rtfree: %p not freed (neg refs)\n", rt); 416 goto done; 417 } 418 #endif 419 /* 420 * release references on items we hold them on.. 421 * e.g other routes and ifaddrs. 422 */ 423 if (rt->rt_ifa) 424 IFAFREE(rt->rt_ifa); 425 rt->rt_parent = NULL; /* NB: no refcnt on parent */ 426 427 /* 428 * The key is separatly alloc'd so free it (see rt_setgate()). 429 * This also frees the gateway, as they are always malloc'd 430 * together. 431 */ 432 Free(rt_key(rt)); 433 434 /* 435 * and the rtentry itself of course 436 */ 437 RT_LOCK_DESTROY(rt); 438 uma_zfree(rtzone, rt); 439 return; 440 } 441 done: 442 RT_UNLOCK(rt); 443 } 444 445 446 /* 447 * Force a routing table entry to the specified 448 * destination to go through the given gateway. 449 * Normally called as a result of a routing redirect 450 * message from the network layer. 451 */ 452 void 453 rtredirect(struct sockaddr *dst, 454 struct sockaddr *gateway, 455 struct sockaddr *netmask, 456 int flags, 457 struct sockaddr *src) 458 { 459 rtredirect_fib(dst, gateway, netmask, flags, src, 0); 460 } 461 462 void 463 rtredirect_fib(struct sockaddr *dst, 464 struct sockaddr *gateway, 465 struct sockaddr *netmask, 466 int flags, 467 struct sockaddr *src, 468 u_int fibnum) 469 { 470 INIT_VNET_NET(curvnet); 471 struct rtentry *rt, *rt0 = NULL; 472 int error = 0; 473 short *stat = NULL; 474 struct rt_addrinfo info; 475 struct ifaddr *ifa; 476 477 /* verify the gateway is directly reachable */ 478 if ((ifa = ifa_ifwithnet(gateway)) == NULL) { 479 error = ENETUNREACH; 480 goto out; 481 } 482 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */ 483 /* 484 * If the redirect isn't from our current router for this dst, 485 * it's either old or wrong. If it redirects us to ourselves, 486 * we have a routing loop, perhaps as a result of an interface 487 * going down recently. 488 */ 489 if (!(flags & RTF_DONE) && rt && 490 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) 491 error = EINVAL; 492 else if (ifa_ifwithaddr(gateway)) 493 error = EHOSTUNREACH; 494 if (error) 495 goto done; 496 /* 497 * Create a new entry if we just got back a wildcard entry 498 * or the the lookup failed. This is necessary for hosts 499 * which use routing redirects generated by smart gateways 500 * to dynamically build the routing tables. 501 */ 502 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 503 goto create; 504 /* 505 * Don't listen to the redirect if it's 506 * for a route to an interface. 507 */ 508 if (rt->rt_flags & RTF_GATEWAY) { 509 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 510 /* 511 * Changing from route to net => route to host. 512 * Create new route, rather than smashing route to net. 513 */ 514 create: 515 rt0 = rt; 516 rt = NULL; 517 518 flags |= RTF_GATEWAY | RTF_DYNAMIC; 519 bzero((caddr_t)&info, sizeof(info)); 520 info.rti_info[RTAX_DST] = dst; 521 info.rti_info[RTAX_GATEWAY] = gateway; 522 info.rti_info[RTAX_NETMASK] = netmask; 523 info.rti_ifa = ifa; 524 info.rti_flags = flags; 525 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum); 526 if (rt != NULL) { 527 RT_LOCK(rt); 528 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst); 529 flags = rt->rt_flags; 530 } 531 if (rt0) 532 RTFREE_LOCKED(rt0); 533 534 stat = &V_rtstat.rts_dynamic; 535 } else { 536 struct rtentry *gwrt; 537 538 /* 539 * Smash the current notion of the gateway to 540 * this destination. Should check about netmask!!! 541 */ 542 rt->rt_flags |= RTF_MODIFIED; 543 flags |= RTF_MODIFIED; 544 stat = &V_rtstat.rts_newgateway; 545 /* 546 * add the key and gateway (in one malloc'd chunk). 547 */ 548 rt_setgate(rt, rt_key(rt), gateway); 549 gwrt = rtalloc1(gateway, 1, 0); 550 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst); 551 RTFREE_LOCKED(gwrt); 552 } 553 } else 554 error = EHOSTUNREACH; 555 done: 556 if (rt) 557 RTFREE_LOCKED(rt); 558 out: 559 if (error) 560 V_rtstat.rts_badredirect++; 561 else if (stat != NULL) 562 (*stat)++; 563 bzero((caddr_t)&info, sizeof(info)); 564 info.rti_info[RTAX_DST] = dst; 565 info.rti_info[RTAX_GATEWAY] = gateway; 566 info.rti_info[RTAX_NETMASK] = netmask; 567 info.rti_info[RTAX_AUTHOR] = src; 568 rt_missmsg(RTM_REDIRECT, &info, flags, error); 569 } 570 571 int 572 rtioctl(u_long req, caddr_t data) 573 { 574 return (rtioctl_fib(req, data, 0)); 575 } 576 577 /* 578 * Routing table ioctl interface. 579 */ 580 int 581 rtioctl_fib(u_long req, caddr_t data, u_int fibnum) 582 { 583 584 /* 585 * If more ioctl commands are added here, make sure the proper 586 * super-user checks are being performed because it is possible for 587 * prison-root to make it this far if raw sockets have been enabled 588 * in jails. 589 */ 590 #ifdef INET 591 /* Multicast goop, grrr... */ 592 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP; 593 #else /* INET */ 594 return ENXIO; 595 #endif /* INET */ 596 } 597 598 struct ifaddr * 599 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway) 600 { 601 return (ifa_ifwithroute_fib(flags, dst, gateway, 0)); 602 } 603 604 struct ifaddr * 605 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway, 606 u_int fibnum) 607 { 608 register struct ifaddr *ifa; 609 int not_found = 0; 610 611 if ((flags & RTF_GATEWAY) == 0) { 612 /* 613 * If we are adding a route to an interface, 614 * and the interface is a pt to pt link 615 * we should search for the destination 616 * as our clue to the interface. Otherwise 617 * we can use the local address. 618 */ 619 ifa = NULL; 620 if (flags & RTF_HOST) 621 ifa = ifa_ifwithdstaddr(dst); 622 if (ifa == NULL) 623 ifa = ifa_ifwithaddr(gateway); 624 } else { 625 /* 626 * If we are adding a route to a remote net 627 * or host, the gateway may still be on the 628 * other end of a pt to pt link. 629 */ 630 ifa = ifa_ifwithdstaddr(gateway); 631 } 632 if (ifa == NULL) 633 ifa = ifa_ifwithnet(gateway); 634 if (ifa == NULL) { 635 struct rtentry *rt = rtalloc1_fib(gateway, 0, 0UL, fibnum); 636 if (rt == NULL) 637 return (NULL); 638 /* 639 * dismiss a gateway that is reachable only 640 * through the default router 641 */ 642 switch (gateway->sa_family) { 643 case AF_INET: 644 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) 645 not_found = 1; 646 break; 647 case AF_INET6: 648 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) 649 not_found = 1; 650 break; 651 default: 652 break; 653 } 654 RT_REMREF(rt); 655 RT_UNLOCK(rt); 656 if (not_found) 657 return (NULL); 658 if ((ifa = rt->rt_ifa) == NULL) 659 return (NULL); 660 } 661 if (ifa->ifa_addr->sa_family != dst->sa_family) { 662 struct ifaddr *oifa = ifa; 663 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 664 if (ifa == NULL) 665 ifa = oifa; 666 } 667 return (ifa); 668 } 669 670 static walktree_f_t rt_fixdelete; 671 static walktree_f_t rt_fixchange; 672 673 struct rtfc_arg { 674 struct rtentry *rt0; 675 struct radix_node_head *rnh; 676 }; 677 678 /* 679 * Do appropriate manipulations of a routing tree given 680 * all the bits of info needed 681 */ 682 int 683 rtrequest(int req, 684 struct sockaddr *dst, 685 struct sockaddr *gateway, 686 struct sockaddr *netmask, 687 int flags, 688 struct rtentry **ret_nrt) 689 { 690 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0)); 691 } 692 693 int 694 rtrequest_fib(int req, 695 struct sockaddr *dst, 696 struct sockaddr *gateway, 697 struct sockaddr *netmask, 698 int flags, 699 struct rtentry **ret_nrt, 700 u_int fibnum) 701 { 702 struct rt_addrinfo info; 703 704 if (dst->sa_len == 0) 705 return(EINVAL); 706 707 bzero((caddr_t)&info, sizeof(info)); 708 info.rti_flags = flags; 709 info.rti_info[RTAX_DST] = dst; 710 info.rti_info[RTAX_GATEWAY] = gateway; 711 info.rti_info[RTAX_NETMASK] = netmask; 712 return rtrequest1_fib(req, &info, ret_nrt, fibnum); 713 } 714 715 /* 716 * These (questionable) definitions of apparent local variables apply 717 * to the next two functions. XXXXXX!!! 718 */ 719 #define dst info->rti_info[RTAX_DST] 720 #define gateway info->rti_info[RTAX_GATEWAY] 721 #define netmask info->rti_info[RTAX_NETMASK] 722 #define ifaaddr info->rti_info[RTAX_IFA] 723 #define ifpaddr info->rti_info[RTAX_IFP] 724 #define flags info->rti_flags 725 726 int 727 rt_getifa(struct rt_addrinfo *info) 728 { 729 return (rt_getifa_fib(info, 0)); 730 } 731 732 int 733 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) 734 { 735 struct ifaddr *ifa; 736 int error = 0; 737 738 /* 739 * ifp may be specified by sockaddr_dl 740 * when protocol address is ambiguous. 741 */ 742 if (info->rti_ifp == NULL && ifpaddr != NULL && 743 ifpaddr->sa_family == AF_LINK && 744 (ifa = ifa_ifwithnet(ifpaddr)) != NULL) 745 info->rti_ifp = ifa->ifa_ifp; 746 if (info->rti_ifa == NULL && ifaaddr != NULL) 747 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 748 if (info->rti_ifa == NULL) { 749 struct sockaddr *sa; 750 751 sa = ifaaddr != NULL ? ifaaddr : 752 (gateway != NULL ? gateway : dst); 753 if (sa != NULL && info->rti_ifp != NULL) 754 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 755 else if (dst != NULL && gateway != NULL) 756 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway, 757 fibnum); 758 else if (sa != NULL) 759 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa, 760 fibnum); 761 } 762 if ((ifa = info->rti_ifa) != NULL) { 763 if (info->rti_ifp == NULL) 764 info->rti_ifp = ifa->ifa_ifp; 765 } else 766 error = ENETUNREACH; 767 return (error); 768 } 769 770 /* 771 * Expunges references to a route that's about to be reclaimed. 772 * The route must be locked. 773 */ 774 int 775 rtexpunge(struct rtentry *rt) 776 { 777 INIT_VNET_NET(curvnet); 778 struct radix_node *rn; 779 struct radix_node_head *rnh; 780 struct ifaddr *ifa; 781 int error = 0; 782 783 RT_LOCK_ASSERT(rt); 784 #if 0 785 /* 786 * We cannot assume anything about the reference count 787 * because protocols call us in many situations; often 788 * before unwinding references to the table entry. 789 */ 790 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt)); 791 #endif 792 /* 793 * Find the correct routing tree to use for this Address Family 794 */ 795 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family]; 796 if (rnh == NULL) 797 return (EAFNOSUPPORT); 798 799 RADIX_NODE_HEAD_LOCK(rnh); 800 801 /* 802 * Remove the item from the tree; it should be there, 803 * but when callers invoke us blindly it may not (sigh). 804 */ 805 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh); 806 if (rn == NULL) { 807 error = ESRCH; 808 goto bad; 809 } 810 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0, 811 ("unexpected flags 0x%x", rn->rn_flags)); 812 KASSERT(rt == RNTORT(rn), 813 ("lookup mismatch, rt %p rn %p", rt, rn)); 814 815 rt->rt_flags &= ~RTF_UP; 816 817 /* 818 * Now search what's left of the subtree for any cloned 819 * routes which might have been formed from this node. 820 */ 821 if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt)) 822 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 823 rt_fixdelete, rt); 824 825 /* 826 * Remove any external references we may have. 827 * This might result in another rtentry being freed if 828 * we held its last reference. 829 */ 830 if (rt->rt_gwroute) { 831 RTFREE(rt->rt_gwroute); 832 rt->rt_gwroute = NULL; 833 } 834 835 /* 836 * Give the protocol a chance to keep things in sync. 837 */ 838 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) { 839 struct rt_addrinfo info; 840 841 bzero((caddr_t)&info, sizeof(info)); 842 info.rti_flags = rt->rt_flags; 843 info.rti_info[RTAX_DST] = rt_key(rt); 844 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 845 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 846 ifa->ifa_rtrequest(RTM_DELETE, rt, &info); 847 } 848 849 /* 850 * one more rtentry floating around that is not 851 * linked to the routing table. 852 */ 853 V_rttrash++; 854 bad: 855 RADIX_NODE_HEAD_UNLOCK(rnh); 856 return (error); 857 } 858 859 int 860 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt) 861 { 862 return (rtrequest1_fib(req, info, ret_nrt, 0)); 863 } 864 865 int 866 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt, 867 u_int fibnum) 868 { 869 INIT_VNET_NET(curvnet); 870 int error = 0; 871 register struct rtentry *rt; 872 register struct radix_node *rn; 873 register struct radix_node_head *rnh; 874 struct ifaddr *ifa; 875 struct sockaddr *ndst; 876 #define senderr(x) { error = x ; goto bad; } 877 878 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum")); 879 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */ 880 fibnum = 0; 881 /* 882 * Find the correct routing tree to use for this Address Family 883 */ 884 rnh = V_rt_tables[fibnum][dst->sa_family]; 885 if (rnh == NULL) 886 return (EAFNOSUPPORT); 887 RADIX_NODE_HEAD_LOCK(rnh); 888 /* 889 * If we are adding a host route then we don't want to put 890 * a netmask in the tree, nor do we want to clone it. 891 */ 892 if (flags & RTF_HOST) { 893 netmask = NULL; 894 flags &= ~RTF_CLONING; 895 } 896 switch (req) { 897 case RTM_DELETE: 898 #ifdef RADIX_MPATH 899 /* 900 * if we got multipath routes, we require users to specify 901 * a matching RTAX_GATEWAY. 902 */ 903 if (rn_mpath_capable(rnh)) { 904 struct rtentry *rto = NULL; 905 906 rn = rnh->rnh_matchaddr(dst, rnh); 907 if (rn == NULL) 908 senderr(ESRCH); 909 rto = rt = RNTORT(rn); 910 rt = rt_mpath_matchgate(rt, gateway); 911 if (!rt) 912 senderr(ESRCH); 913 /* 914 * this is the first entry in the chain 915 */ 916 if (rto == rt) { 917 rn = rn_mpath_next((struct radix_node *)rt); 918 /* 919 * there is another entry, now it's active 920 */ 921 if (rn) { 922 rto = RNTORT(rn); 923 RT_LOCK(rto); 924 rto->rt_flags |= RTF_UP; 925 RT_UNLOCK(rto); 926 } else if (rt->rt_flags & RTF_GATEWAY) { 927 /* 928 * For gateway routes, we need to 929 * make sure that we we are deleting 930 * the correct gateway. 931 * rt_mpath_matchgate() does not 932 * check the case when there is only 933 * one route in the chain. 934 */ 935 if (gateway && 936 (rt->rt_gateway->sa_len != gateway->sa_len || 937 memcmp(rt->rt_gateway, gateway, gateway->sa_len))) 938 senderr(ESRCH); 939 } 940 /* 941 * use the normal delete code to remove 942 * the first entry 943 */ 944 goto normal_rtdel; 945 } 946 /* 947 * if the entry is 2nd and on up 948 */ 949 if (!rt_mpath_deldup(rto, rt)) 950 panic ("rtrequest1: rt_mpath_deldup"); 951 RT_LOCK(rt); 952 RT_ADDREF(rt); 953 rt->rt_flags &= ~RTF_UP; 954 goto deldone; /* done with the RTM_DELETE command */ 955 } 956 957 normal_rtdel: 958 #endif 959 /* 960 * Remove the item from the tree and return it. 961 * Complain if it is not there and do no more processing. 962 */ 963 rn = rnh->rnh_deladdr(dst, netmask, rnh); 964 if (rn == NULL) 965 senderr(ESRCH); 966 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 967 panic ("rtrequest delete"); 968 rt = RNTORT(rn); 969 RT_LOCK(rt); 970 RT_ADDREF(rt); 971 rt->rt_flags &= ~RTF_UP; 972 973 /* 974 * Now search what's left of the subtree for any cloned 975 * routes which might have been formed from this node. 976 */ 977 if ((rt->rt_flags & RTF_CLONING) && 978 rt_mask(rt)) { 979 rnh->rnh_walktree_from(rnh, dst, rt_mask(rt), 980 rt_fixdelete, rt); 981 } 982 983 /* 984 * Remove any external references we may have. 985 * This might result in another rtentry being freed if 986 * we held its last reference. 987 */ 988 if (rt->rt_gwroute) { 989 RTFREE(rt->rt_gwroute); 990 rt->rt_gwroute = NULL; 991 } 992 993 /* 994 * give the protocol a chance to keep things in sync. 995 */ 996 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) 997 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 998 999 #ifdef RADIX_MPATH 1000 deldone: 1001 #endif 1002 /* 1003 * One more rtentry floating around that is not 1004 * linked to the routing table. rttrash will be decremented 1005 * when RTFREE(rt) is eventually called. 1006 */ 1007 V_rttrash++; 1008 1009 /* 1010 * If the caller wants it, then it can have it, 1011 * but it's up to it to free the rtentry as we won't be 1012 * doing it. 1013 */ 1014 if (ret_nrt) { 1015 *ret_nrt = rt; 1016 RT_UNLOCK(rt); 1017 } else 1018 RTFREE_LOCKED(rt); 1019 break; 1020 1021 case RTM_RESOLVE: 1022 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL) 1023 senderr(EINVAL); 1024 ifa = rt->rt_ifa; 1025 /* XXX locking? */ 1026 flags = rt->rt_flags & 1027 ~(RTF_CLONING | RTF_STATIC); 1028 flags |= RTF_WASCLONED; 1029 gateway = rt->rt_gateway; 1030 if ((netmask = rt->rt_genmask) == NULL) 1031 flags |= RTF_HOST; 1032 goto makeroute; 1033 1034 case RTM_ADD: 1035 if ((flags & RTF_GATEWAY) && !gateway) 1036 senderr(EINVAL); 1037 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 1038 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK)) 1039 senderr(EINVAL); 1040 1041 if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum))) 1042 senderr(error); 1043 ifa = info->rti_ifa; 1044 1045 makeroute: 1046 rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO); 1047 if (rt == NULL) 1048 senderr(ENOBUFS); 1049 RT_LOCK_INIT(rt); 1050 rt->rt_flags = RTF_UP | flags; 1051 rt->rt_fibnum = fibnum; 1052 /* 1053 * Add the gateway. Possibly re-malloc-ing the storage for it 1054 * also add the rt_gwroute if possible. 1055 */ 1056 RT_LOCK(rt); 1057 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 1058 RT_LOCK_DESTROY(rt); 1059 uma_zfree(rtzone, rt); 1060 senderr(error); 1061 } 1062 1063 /* 1064 * point to the (possibly newly malloc'd) dest address. 1065 */ 1066 ndst = (struct sockaddr *)rt_key(rt); 1067 1068 /* 1069 * make sure it contains the value we want (masked if needed). 1070 */ 1071 if (netmask) { 1072 rt_maskedcopy(dst, ndst, netmask); 1073 } else 1074 bcopy(dst, ndst, dst->sa_len); 1075 1076 /* 1077 * Note that we now have a reference to the ifa. 1078 * This moved from below so that rnh->rnh_addaddr() can 1079 * examine the ifa and ifa->ifa_ifp if it so desires. 1080 */ 1081 IFAREF(ifa); 1082 rt->rt_ifa = ifa; 1083 rt->rt_ifp = ifa->ifa_ifp; 1084 1085 #ifdef RADIX_MPATH 1086 /* do not permit exactly the same dst/mask/gw pair */ 1087 if (rn_mpath_capable(rnh) && 1088 rt_mpath_conflict(rnh, rt, netmask)) { 1089 if (rt->rt_gwroute) 1090 RTFREE(rt->rt_gwroute); 1091 if (rt->rt_ifa) { 1092 IFAFREE(rt->rt_ifa); 1093 } 1094 Free(rt_key(rt)); 1095 RT_LOCK_DESTROY(rt); 1096 uma_zfree(rtzone, rt); 1097 senderr(EEXIST); 1098 } 1099 #endif 1100 1101 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 1102 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes); 1103 if (rn == NULL) { 1104 struct rtentry *rt2; 1105 /* 1106 * Uh-oh, we already have one of these in the tree. 1107 * We do a special hack: if the route that's already 1108 * there was generated by the cloning mechanism 1109 * then we just blow it away and retry the insertion 1110 * of the new one. 1111 */ 1112 rt2 = rtalloc1_fib(dst, 0, 0, fibnum); 1113 if (rt2 && rt2->rt_parent) { 1114 rtexpunge(rt2); 1115 RT_UNLOCK(rt2); 1116 rn = rnh->rnh_addaddr(ndst, netmask, 1117 rnh, rt->rt_nodes); 1118 } else if (rt2) { 1119 /* undo the extra ref we got */ 1120 RTFREE_LOCKED(rt2); 1121 } 1122 } 1123 1124 /* 1125 * If it still failed to go into the tree, 1126 * then un-make it (this should be a function) 1127 */ 1128 if (rn == NULL) { 1129 if (rt->rt_gwroute) 1130 RTFREE(rt->rt_gwroute); 1131 if (rt->rt_ifa) 1132 IFAFREE(rt->rt_ifa); 1133 Free(rt_key(rt)); 1134 RT_LOCK_DESTROY(rt); 1135 uma_zfree(rtzone, rt); 1136 senderr(EEXIST); 1137 } 1138 1139 rt->rt_parent = NULL; 1140 1141 /* 1142 * If we got here from RESOLVE, then we are cloning 1143 * so clone the rest, and note that we 1144 * are a clone (and increment the parent's references) 1145 */ 1146 if (req == RTM_RESOLVE) { 1147 KASSERT(ret_nrt && *ret_nrt, 1148 ("no route to clone from")); 1149 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ 1150 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */ 1151 if ((*ret_nrt)->rt_flags & RTF_CLONING) { 1152 /* 1153 * NB: We do not bump the refcnt on the parent 1154 * entry under the assumption that it will 1155 * remain so long as we do. This is 1156 * important when deleting the parent route 1157 * as this operation requires traversing 1158 * the tree to delete all clones and futzing 1159 * with refcnts requires us to double-lock 1160 * parent through this back reference. 1161 */ 1162 rt->rt_parent = *ret_nrt; 1163 } 1164 } 1165 1166 /* 1167 * If this protocol has something to add to this then 1168 * allow it to do that as well. 1169 */ 1170 if (ifa->ifa_rtrequest) 1171 ifa->ifa_rtrequest(req, rt, info); 1172 1173 /* 1174 * We repeat the same procedure from rt_setgate() here because 1175 * it doesn't fire when we call it there because the node 1176 * hasn't been added to the tree yet. 1177 */ 1178 if (req == RTM_ADD && 1179 !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) { 1180 struct rtfc_arg arg; 1181 arg.rnh = rnh; 1182 arg.rt0 = rt; 1183 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 1184 rt_fixchange, &arg); 1185 } 1186 1187 /* 1188 * actually return a resultant rtentry and 1189 * give the caller a single reference. 1190 */ 1191 if (ret_nrt) { 1192 *ret_nrt = rt; 1193 RT_ADDREF(rt); 1194 } 1195 RT_UNLOCK(rt); 1196 break; 1197 default: 1198 error = EOPNOTSUPP; 1199 } 1200 bad: 1201 RADIX_NODE_HEAD_UNLOCK(rnh); 1202 return (error); 1203 #undef senderr 1204 } 1205 1206 #undef dst 1207 #undef gateway 1208 #undef netmask 1209 #undef ifaaddr 1210 #undef ifpaddr 1211 #undef flags 1212 1213 /* 1214 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' 1215 * (i.e., the routes related to it by the operation of cloning). This 1216 * routine is iterated over all potential former-child-routes by way of 1217 * rnh->rnh_walktree_from() above, and those that actually are children of 1218 * the late parent (passed in as VP here) are themselves deleted. 1219 */ 1220 static int 1221 rt_fixdelete(struct radix_node *rn, void *vp) 1222 { 1223 struct rtentry *rt = RNTORT(rn); 1224 struct rtentry *rt0 = vp; 1225 1226 if (rt->rt_parent == rt0 && 1227 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) { 1228 return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 1229 rt->rt_flags, NULL, rt->rt_fibnum); 1230 } 1231 return 0; 1232 } 1233 1234 /* 1235 * This routine is called from rt_setgate() to do the analogous thing for 1236 * adds and changes. There is the added complication in this case of a 1237 * middle insert; i.e., insertion of a new network route between an older 1238 * network route and (cloned) host routes. For this reason, a simple check 1239 * of rt->rt_parent is insufficient; each candidate route must be tested 1240 * against the (mask, value) of the new route (passed as before in vp) 1241 * to see if the new route matches it. 1242 * 1243 * XXX - it may be possible to do fixdelete() for changes and reserve this 1244 * routine just for adds. I'm not sure why I thought it was necessary to do 1245 * changes this way. 1246 */ 1247 1248 static int 1249 rt_fixchange(struct radix_node *rn, void *vp) 1250 { 1251 struct rtentry *rt = RNTORT(rn); 1252 struct rtfc_arg *ap = vp; 1253 struct rtentry *rt0 = ap->rt0; 1254 struct radix_node_head *rnh = ap->rnh; 1255 u_char *xk1, *xm1, *xk2, *xmp; 1256 int i, len, mlen; 1257 1258 /* make sure we have a parent, and route is not pinned or cloning */ 1259 if (!rt->rt_parent || 1260 (rt->rt_flags & (RTF_PINNED | RTF_CLONING))) 1261 return 0; 1262 1263 if (rt->rt_parent == rt0) /* parent match */ 1264 goto delete_rt; 1265 /* 1266 * There probably is a function somewhere which does this... 1267 * if not, there should be. 1268 */ 1269 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len); 1270 1271 xk1 = (u_char *)rt_key(rt0); 1272 xm1 = (u_char *)rt_mask(rt0); 1273 xk2 = (u_char *)rt_key(rt); 1274 1275 /* avoid applying a less specific route */ 1276 xmp = (u_char *)rt_mask(rt->rt_parent); 1277 mlen = rt_key(rt->rt_parent)->sa_len; 1278 if (mlen > rt_key(rt0)->sa_len) /* less specific route */ 1279 return 0; 1280 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) 1281 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) 1282 return 0; /* less specific route */ 1283 1284 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) 1285 if ((xk2[i] & xm1[i]) != xk1[i]) 1286 return 0; /* no match */ 1287 1288 /* 1289 * OK, this node is a clone, and matches the node currently being 1290 * changed/added under the node's mask. So, get rid of it. 1291 */ 1292 delete_rt: 1293 return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL, 1294 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum); 1295 } 1296 1297 int 1298 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) 1299 { 1300 INIT_VNET_NET(curvnet); 1301 /* XXX dst may be overwritten, can we move this to below */ 1302 struct radix_node_head *rnh = 1303 V_rt_tables[rt->rt_fibnum][dst->sa_family]; 1304 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate); 1305 1306 again: 1307 RT_LOCK_ASSERT(rt); 1308 1309 /* 1310 * A host route with the destination equal to the gateway 1311 * will interfere with keeping LLINFO in the routing 1312 * table, so disallow it. 1313 */ 1314 if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) == 1315 (RTF_HOST|RTF_GATEWAY)) && 1316 dst->sa_len == gate->sa_len && 1317 bcmp(dst, gate, dst->sa_len) == 0) { 1318 /* 1319 * The route might already exist if this is an RTM_CHANGE 1320 * or a routing redirect, so try to delete it. 1321 */ 1322 if (rt_key(rt)) 1323 rtexpunge(rt); 1324 return EADDRNOTAVAIL; 1325 } 1326 1327 /* 1328 * Cloning loop avoidance in case of bad configuration. 1329 */ 1330 if (rt->rt_flags & RTF_GATEWAY) { 1331 struct rtentry *gwrt; 1332 1333 RT_UNLOCK(rt); /* XXX workaround LOR */ 1334 gwrt = rtalloc1_fib(gate, 1, 0, rt->rt_fibnum); 1335 if (gwrt == rt) { 1336 RT_REMREF(rt); 1337 return (EADDRINUSE); /* failure */ 1338 } 1339 /* 1340 * Try to reacquire the lock on rt, and if it fails, 1341 * clean state and restart from scratch. 1342 */ 1343 if (!RT_TRYLOCK(rt)) { 1344 RTFREE_LOCKED(gwrt); 1345 RT_LOCK(rt); 1346 goto again; 1347 } 1348 /* 1349 * If there is already a gwroute, then drop it. If we 1350 * are asked to replace route with itself, then do 1351 * not leak its refcounter. 1352 */ 1353 if (rt->rt_gwroute != NULL) { 1354 if (rt->rt_gwroute == gwrt) { 1355 RT_REMREF(rt->rt_gwroute); 1356 } else 1357 RTFREE(rt->rt_gwroute); 1358 } 1359 1360 if ((rt->rt_gwroute = gwrt) != NULL) 1361 RT_UNLOCK(rt->rt_gwroute); 1362 } 1363 1364 /* 1365 * Prepare to store the gateway in rt->rt_gateway. 1366 * Both dst and gateway are stored one after the other in the same 1367 * malloc'd chunk. If we have room, we can reuse the old buffer, 1368 * rt_gateway already points to the right place. 1369 * Otherwise, malloc a new block and update the 'dst' address. 1370 */ 1371 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) { 1372 caddr_t new; 1373 1374 R_Malloc(new, caddr_t, dlen + glen); 1375 if (new == NULL) 1376 return ENOBUFS; 1377 /* 1378 * XXX note, we copy from *dst and not *rt_key(rt) because 1379 * rt_setgate() can be called to initialize a newly 1380 * allocated route entry, in which case rt_key(rt) == NULL 1381 * (and also rt->rt_gateway == NULL). 1382 * Free()/free() handle a NULL argument just fine. 1383 */ 1384 bcopy(dst, new, dlen); 1385 Free(rt_key(rt)); /* free old block, if any */ 1386 rt_key(rt) = (struct sockaddr *)new; 1387 rt->rt_gateway = (struct sockaddr *)(new + dlen); 1388 } 1389 1390 /* 1391 * Copy the new gateway value into the memory chunk. 1392 */ 1393 bcopy(gate, rt->rt_gateway, glen); 1394 1395 /* 1396 * This isn't going to do anything useful for host routes, so 1397 * don't bother. Also make sure we have a reasonable mask 1398 * (we don't yet have one during adds). 1399 */ 1400 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { 1401 struct rtfc_arg arg; 1402 1403 arg.rnh = rnh; 1404 arg.rt0 = rt; 1405 RT_UNLOCK(rt); /* XXX workaround LOR */ 1406 RADIX_NODE_HEAD_LOCK(rnh); 1407 RT_LOCK(rt); 1408 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), 1409 rt_fixchange, &arg); 1410 RADIX_NODE_HEAD_UNLOCK(rnh); 1411 } 1412 1413 return 0; 1414 } 1415 1416 static void 1417 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 1418 { 1419 register u_char *cp1 = (u_char *)src; 1420 register u_char *cp2 = (u_char *)dst; 1421 register u_char *cp3 = (u_char *)netmask; 1422 u_char *cplim = cp2 + *cp3; 1423 u_char *cplim2 = cp2 + *cp1; 1424 1425 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1426 cp3 += 2; 1427 if (cplim > cplim2) 1428 cplim = cplim2; 1429 while (cp2 < cplim) 1430 *cp2++ = *cp1++ & *cp3++; 1431 if (cp2 < cplim2) 1432 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1433 } 1434 1435 /* 1436 * Set up a routing table entry, normally 1437 * for an interface. 1438 */ 1439 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */ 1440 static inline int 1441 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum) 1442 { 1443 INIT_VNET_NET(curvnet); 1444 struct sockaddr *dst; 1445 struct sockaddr *netmask; 1446 struct rtentry *rt = NULL; 1447 struct rt_addrinfo info; 1448 int error = 0; 1449 int startfib, endfib; 1450 char tempbuf[_SOCKADDR_TMPSIZE]; 1451 int didwork = 0; 1452 int a_failure = 0; 1453 1454 if (flags & RTF_HOST) { 1455 dst = ifa->ifa_dstaddr; 1456 netmask = NULL; 1457 } else { 1458 dst = ifa->ifa_addr; 1459 netmask = ifa->ifa_netmask; 1460 } 1461 if ( dst->sa_family != AF_INET) 1462 fibnum = 0; 1463 if (fibnum == -1) { 1464 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) { 1465 startfib = endfib = curthread->td_proc->p_fibnum; 1466 } else { 1467 startfib = 0; 1468 endfib = rt_numfibs - 1; 1469 } 1470 } else { 1471 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum")); 1472 startfib = fibnum; 1473 endfib = fibnum; 1474 } 1475 if (dst->sa_len == 0) 1476 return(EINVAL); 1477 1478 /* 1479 * If it's a delete, check that if it exists, 1480 * it's on the correct interface or we might scrub 1481 * a route to another ifa which would 1482 * be confusing at best and possibly worse. 1483 */ 1484 if (cmd == RTM_DELETE) { 1485 /* 1486 * It's a delete, so it should already exist.. 1487 * If it's a net, mask off the host bits 1488 * (Assuming we have a mask) 1489 * XXX this is kinda inet specific.. 1490 */ 1491 if (netmask != NULL) { 1492 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask); 1493 dst = (struct sockaddr *)tempbuf; 1494 } 1495 } 1496 /* 1497 * Now go through all the requested tables (fibs) and do the 1498 * requested action. Realistically, this will either be fib 0 1499 * for protocols that don't do multiple tables or all the 1500 * tables for those that do. XXX For this version only AF_INET. 1501 * When that changes code should be refactored to protocol 1502 * independent parts and protocol dependent parts. 1503 */ 1504 for ( fibnum = startfib; fibnum <= endfib; fibnum++) { 1505 if (cmd == RTM_DELETE) { 1506 struct radix_node_head *rnh; 1507 struct radix_node *rn; 1508 /* 1509 * Look up an rtentry that is in the routing tree and 1510 * contains the correct info. 1511 */ 1512 if ((rnh = V_rt_tables[fibnum][dst->sa_family]) == NULL) 1513 /* this table doesn't exist but others might */ 1514 continue; 1515 RADIX_NODE_HEAD_LOCK(rnh); 1516 #ifdef RADIX_MPATH 1517 if (rn_mpath_capable(rnh)) { 1518 1519 rn = rnh->rnh_matchaddr(dst, rnh); 1520 if (rn == NULL) 1521 error = ESRCH; 1522 else { 1523 rt = RNTORT(rn); 1524 /* 1525 * for interface route the 1526 * rt->rt_gateway is sockaddr_intf 1527 * for cloning ARP entries, so 1528 * rt_mpath_matchgate must use the 1529 * interface address 1530 */ 1531 rt = rt_mpath_matchgate(rt, 1532 ifa->ifa_addr); 1533 if (!rt) 1534 error = ESRCH; 1535 } 1536 } 1537 else 1538 #endif 1539 rn = rnh->rnh_lookup(dst, netmask, rnh); 1540 error = (rn == NULL || 1541 (rn->rn_flags & RNF_ROOT) || 1542 RNTORT(rn)->rt_ifa != ifa || 1543 !sa_equal((struct sockaddr *)rn->rn_key, dst)); 1544 RADIX_NODE_HEAD_UNLOCK(rnh); 1545 if (error) { 1546 /* this is only an error if bad on ALL tables */ 1547 continue; 1548 } 1549 } 1550 /* 1551 * Do the actual request 1552 */ 1553 bzero((caddr_t)&info, sizeof(info)); 1554 info.rti_ifa = ifa; 1555 info.rti_flags = flags | ifa->ifa_flags; 1556 info.rti_info[RTAX_DST] = dst; 1557 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1558 info.rti_info[RTAX_NETMASK] = netmask; 1559 error = rtrequest1_fib(cmd, &info, &rt, fibnum); 1560 if (error == 0 && rt != NULL) { 1561 /* 1562 * notify any listening routing agents of the change 1563 */ 1564 RT_LOCK(rt); 1565 #ifdef RADIX_MPATH 1566 /* 1567 * in case address alias finds the first address 1568 * e.g. ifconfig bge0 192.103.54.246/24 1569 * e.g. ifconfig bge0 192.103.54.247/24 1570 * the address set in the route is 192.103.54.246 1571 * so we need to replace it with 192.103.54.247 1572 */ 1573 if (memcmp(rt->rt_ifa->ifa_addr, 1574 ifa->ifa_addr, ifa->ifa_addr->sa_len)) { 1575 IFAFREE(rt->rt_ifa); 1576 IFAREF(ifa); 1577 rt->rt_ifp = ifa->ifa_ifp; 1578 rt->rt_ifa = ifa; 1579 } 1580 #endif 1581 rt_newaddrmsg(cmd, ifa, error, rt); 1582 if (cmd == RTM_DELETE) { 1583 /* 1584 * If we are deleting, and we found an entry, 1585 * then it's been removed from the tree.. 1586 * now throw it away. 1587 */ 1588 RTFREE_LOCKED(rt); 1589 } else { 1590 if (cmd == RTM_ADD) { 1591 /* 1592 * We just wanted to add it.. 1593 * we don't actually need a reference. 1594 */ 1595 RT_REMREF(rt); 1596 } 1597 RT_UNLOCK(rt); 1598 } 1599 didwork = 1; 1600 } 1601 if (error) 1602 a_failure = error; 1603 } 1604 if (cmd == RTM_DELETE) { 1605 if (didwork) { 1606 error = 0; 1607 } else { 1608 /* we only give an error if it wasn't in any table */ 1609 error = ((flags & RTF_HOST) ? 1610 EHOSTUNREACH : ENETUNREACH); 1611 } 1612 } else { 1613 if (a_failure) { 1614 /* return an error if any of them failed */ 1615 error = a_failure; 1616 } 1617 } 1618 return (error); 1619 } 1620 1621 /* special one for inet internal use. may not use. */ 1622 int 1623 rtinit_fib(struct ifaddr *ifa, int cmd, int flags) 1624 { 1625 return (rtinit1(ifa, cmd, flags, -1)); 1626 } 1627 1628 /* 1629 * Set up a routing table entry, normally 1630 * for an interface. 1631 */ 1632 int 1633 rtinit(struct ifaddr *ifa, int cmd, int flags) 1634 { 1635 struct sockaddr *dst; 1636 int fib = 0; 1637 1638 if (flags & RTF_HOST) { 1639 dst = ifa->ifa_dstaddr; 1640 } else { 1641 dst = ifa->ifa_addr; 1642 } 1643 1644 if (dst->sa_family == AF_INET) 1645 fib = -1; 1646 return (rtinit1(ifa, cmd, flags, fib)); 1647 } 1648 1649 /* 1650 * rt_check() is invoked on each layer 2 output path, prior to 1651 * encapsulating outbound packets. 1652 * 1653 * The function is mostly used to find a routing entry for the gateway, 1654 * which in some protocol families could also point to the link-level 1655 * address for the gateway itself (the side effect of revalidating the 1656 * route to the destination is rather pointless at this stage, we did it 1657 * already a moment before in the pr_output() routine to locate the ifp 1658 * and gateway to use). 1659 * 1660 * When we remove the layer-3 to layer-2 mapping tables from the 1661 * routing table, this function can be removed. 1662 * 1663 * === On input === 1664 * *dst is the address of the NEXT HOP (which coincides with the 1665 * final destination if directly reachable); 1666 * *lrt0 points to the cached route to the final destination; 1667 * *lrt is not meaningful; 1668 * (*lrt0 has no ref held on it by us so REMREF is not needed. 1669 * Refs only account for major structural references and not usages, 1670 * which is actually a bit of a problem.) 1671 * 1672 * === Operation === 1673 * If the route is marked down try to find a new route. If the route 1674 * to the gateway is gone, try to setup a new route. Otherwise, 1675 * if the route is marked for packets to be rejected, enforce that. 1676 * Note that rtalloc returns an rtentry with an extra REF that we may 1677 * need to lose. 1678 * 1679 * === On return === 1680 * *dst is unchanged; 1681 * *lrt0 points to the (possibly new) route to the final destination 1682 * *lrt points to the route to the next hop [LOCKED] 1683 * 1684 * Their values are meaningful ONLY if no error is returned. 1685 * 1686 * To follow this you have to remember that: 1687 * RT_REMREF reduces the reference count by 1 but doesn't check it for 0 (!) 1688 * RTFREE_LOCKED includes an RT_REMREF (or an rtfree if refs == 1) 1689 * and an RT_UNLOCK 1690 * RTFREE does an RT_LOCK and an RTFREE_LOCKED 1691 * The gwroute pointer counts as a reference on the rtentry to which it points. 1692 * so when we add it we use the ref that rtalloc gives us and when we lose it 1693 * we need to remove the reference. 1694 * RT_TEMP_UNLOCK does an RT_ADDREF before freeing the lock, and 1695 * RT_RELOCK locks it (it can't have gone away due to the ref) and 1696 * drops the ref, possibly freeing it and zeroing the pointer if 1697 * the ref goes to 0 (unlocking in the process). 1698 */ 1699 int 1700 rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst) 1701 { 1702 struct rtentry *rt; 1703 struct rtentry *rt0; 1704 u_int fibnum; 1705 1706 KASSERT(*lrt0 != NULL, ("rt_check")); 1707 rt0 = *lrt0; 1708 rt = NULL; 1709 fibnum = rt0->rt_fibnum; 1710 1711 /* NB: the locking here is tortuous... */ 1712 RT_LOCK(rt0); 1713 retry: 1714 if (rt0 && (rt0->rt_flags & RTF_UP) == 0) { 1715 /* Current rt0 is useless, try get a replacement. */ 1716 RT_UNLOCK(rt0); 1717 rt0 = NULL; 1718 } 1719 if (rt0 == NULL) { 1720 rt0 = rtalloc1_fib(dst, 1, 0UL, fibnum); 1721 if (rt0 == NULL) { 1722 return (EHOSTUNREACH); 1723 } 1724 RT_REMREF(rt0); /* don't need the reference. */ 1725 } 1726 1727 if (rt0->rt_flags & RTF_GATEWAY) { 1728 if ((rt = rt0->rt_gwroute) != NULL) { 1729 RT_LOCK(rt); /* NB: gwroute */ 1730 if ((rt->rt_flags & RTF_UP) == 0) { 1731 /* gw route is dud. ignore/lose it */ 1732 RTFREE_LOCKED(rt); /* unref (&unlock) gwroute */ 1733 rt = rt0->rt_gwroute = NULL; 1734 } 1735 } 1736 1737 if (rt == NULL) { /* NOT AN ELSE CLAUSE */ 1738 RT_TEMP_UNLOCK(rt0); /* MUST return to undo this */ 1739 rt = rtalloc1_fib(rt0->rt_gateway, 1, 0UL, fibnum); 1740 if ((rt == rt0) || (rt == NULL)) { 1741 /* the best we can do is not good enough */ 1742 if (rt) { 1743 RT_REMREF(rt); /* assumes ref > 0 */ 1744 RT_UNLOCK(rt); 1745 } 1746 RTFREE(rt0); /* lock, unref, (unlock) */ 1747 return (ENETUNREACH); 1748 } 1749 /* 1750 * Relock it and lose the added reference. 1751 * All sorts of things could have happenned while we 1752 * had no lock on it, so check for them. 1753 */ 1754 RT_RELOCK(rt0); 1755 if (rt0 == NULL || ((rt0->rt_flags & RTF_UP) == 0)) 1756 /* Ru-roh.. what we had is no longer any good */ 1757 goto retry; 1758 /* 1759 * While we were away, someone replaced the gateway. 1760 * Since a reference count is involved we can't just 1761 * overwrite it. 1762 */ 1763 if (rt0->rt_gwroute) { 1764 if (rt0->rt_gwroute != rt) { 1765 RTFREE_LOCKED(rt); 1766 goto retry; 1767 } 1768 } else { 1769 rt0->rt_gwroute = rt; 1770 } 1771 } 1772 RT_LOCK_ASSERT(rt); 1773 RT_UNLOCK(rt0); 1774 } else { 1775 /* think of rt as having the lock from now on.. */ 1776 rt = rt0; 1777 } 1778 /* XXX why are we inspecting rmx_expire? */ 1779 if ((rt->rt_flags & RTF_REJECT) && 1780 (rt->rt_rmx.rmx_expire == 0 || 1781 time_uptime < rt->rt_rmx.rmx_expire)) { 1782 RT_UNLOCK(rt); 1783 return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 1784 } 1785 1786 *lrt = rt; 1787 *lrt0 = rt0; 1788 return (0); 1789 } 1790 1791 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */ 1792 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0); 1793