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