1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1986, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95 32 * $FreeBSD$ 33 */ 34 /************************************************************************ 35 * Note: In this file a 'fib' is a "forwarding information base" * 36 * Which is the new name for an in kernel routing (next hop) table. * 37 ***********************************************************************/ 38 39 #include "opt_inet.h" 40 #include "opt_inet6.h" 41 #include "opt_mrouting.h" 42 #include "opt_mpath.h" 43 #include "opt_route.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/malloc.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/sysctl.h> 51 #include <sys/syslog.h> 52 #include <sys/sysproto.h> 53 #include <sys/proc.h> 54 #include <sys/domain.h> 55 #include <sys/eventhandler.h> 56 #include <sys/kernel.h> 57 #include <sys/lock.h> 58 #include <sys/rmlock.h> 59 60 #include <net/if.h> 61 #include <net/if_var.h> 62 #include <net/if_dl.h> 63 #include <net/route.h> 64 #include <net/route_var.h> 65 #include <net/vnet.h> 66 67 #ifdef RADIX_MPATH 68 #include <net/radix_mpath.h> 69 #endif 70 71 #include <netinet/in.h> 72 #include <netinet/ip_mroute.h> 73 74 #include <vm/uma.h> 75 76 #define RT_MAXFIBS UINT16_MAX 77 78 /* Kernel config default option. */ 79 #ifdef ROUTETABLES 80 #if ROUTETABLES <= 0 81 #error "ROUTETABLES defined too low" 82 #endif 83 #if ROUTETABLES > RT_MAXFIBS 84 #error "ROUTETABLES defined too big" 85 #endif 86 #define RT_NUMFIBS ROUTETABLES 87 #endif /* ROUTETABLES */ 88 /* Initialize to default if not otherwise set. */ 89 #ifndef RT_NUMFIBS 90 #define RT_NUMFIBS 1 91 #endif 92 93 /* This is read-only.. */ 94 u_int rt_numfibs = RT_NUMFIBS; 95 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RDTUN, &rt_numfibs, 0, ""); 96 97 /* 98 * By default add routes to all fibs for new interfaces. 99 * Once this is set to 0 then only allocate routes on interface 100 * changes for the FIB of the caller when adding a new set of addresses 101 * to an interface. XXX this is a shotgun aproach to a problem that needs 102 * a more fine grained solution.. that will come. 103 * XXX also has the problems getting the FIB from curthread which will not 104 * always work given the fib can be overridden and prefixes can be added 105 * from the network stack context. 106 */ 107 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 1; 108 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET, 109 &VNET_NAME(rt_add_addr_allfibs), 0, ""); 110 111 VNET_DEFINE(struct rtstat, rtstat); 112 #define V_rtstat VNET(rtstat) 113 114 VNET_DEFINE(struct rib_head *, rt_tables); 115 #define V_rt_tables VNET(rt_tables) 116 117 VNET_DEFINE(int, rttrash); /* routes not in table but not freed */ 118 #define V_rttrash VNET(rttrash) 119 120 121 /* 122 * Convert a 'struct radix_node *' to a 'struct rtentry *'. 123 * The operation can be done safely (in this code) because a 124 * 'struct rtentry' starts with two 'struct radix_node''s, the first 125 * one representing leaf nodes in the routing tree, which is 126 * what the code in radix.c passes us as a 'struct radix_node'. 127 * 128 * But because there are a lot of assumptions in this conversion, 129 * do not cast explicitly, but always use the macro below. 130 */ 131 #define RNTORT(p) ((struct rtentry *)(p)) 132 133 VNET_DEFINE_STATIC(uma_zone_t, rtzone); /* Routing table UMA zone. */ 134 #define V_rtzone VNET(rtzone) 135 136 EVENTHANDLER_LIST_DEFINE(rt_addrmsg); 137 138 static int rt_getifa_fib(struct rt_addrinfo *, u_int); 139 static int rtrequest1_fib_change(struct rib_head *, struct rt_addrinfo *, 140 struct rtentry **, u_int); 141 static void rt_setmetrics(const struct rt_addrinfo *, struct rtentry *); 142 static int rt_ifdelroute(const struct rtentry *rt, void *arg); 143 static struct rtentry *rt_unlinkrte(struct rib_head *rnh, 144 struct rt_addrinfo *info, int *perror); 145 static void rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info); 146 #ifdef RADIX_MPATH 147 static struct radix_node *rt_mpath_unlink(struct rib_head *rnh, 148 struct rt_addrinfo *info, struct rtentry *rto, int *perror); 149 #endif 150 static int rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, 151 int flags); 152 153 struct if_mtuinfo 154 { 155 struct ifnet *ifp; 156 int mtu; 157 }; 158 159 static int if_updatemtu_cb(struct radix_node *, void *); 160 161 /* 162 * handler for net.my_fibnum 163 */ 164 static int 165 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS) 166 { 167 int fibnum; 168 int error; 169 170 fibnum = curthread->td_proc->p_fibnum; 171 error = sysctl_handle_int(oidp, &fibnum, 0, req); 172 return (error); 173 } 174 175 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD, 176 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller"); 177 178 static __inline struct rib_head ** 179 rt_tables_get_rnh_ptr(int table, int fam) 180 { 181 struct rib_head **rnh; 182 183 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.", 184 __func__)); 185 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.", 186 __func__)); 187 188 /* rnh is [fib=0][af=0]. */ 189 rnh = (struct rib_head **)V_rt_tables; 190 /* Get the offset to the requested table and fam. */ 191 rnh += table * (AF_MAX+1) + fam; 192 193 return (rnh); 194 } 195 196 struct rib_head * 197 rt_tables_get_rnh(int table, int fam) 198 { 199 200 return (*rt_tables_get_rnh_ptr(table, fam)); 201 } 202 203 u_int 204 rt_tables_get_gen(int table, int fam) 205 { 206 struct rib_head *rnh; 207 208 rnh = *rt_tables_get_rnh_ptr(table, fam); 209 KASSERT(rnh != NULL, ("%s: NULL rib_head pointer table %d fam %d", 210 __func__, table, fam)); 211 return (rnh->rnh_gen); 212 } 213 214 215 /* 216 * route initialization must occur before ip6_init2(), which happenas at 217 * SI_ORDER_MIDDLE. 218 */ 219 static void 220 route_init(void) 221 { 222 223 /* whack the tunable ints into line. */ 224 if (rt_numfibs > RT_MAXFIBS) 225 rt_numfibs = RT_MAXFIBS; 226 if (rt_numfibs == 0) 227 rt_numfibs = 1; 228 } 229 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, NULL); 230 231 static int 232 rtentry_zinit(void *mem, int size, int how) 233 { 234 struct rtentry *rt = mem; 235 236 rt->rt_pksent = counter_u64_alloc(how); 237 if (rt->rt_pksent == NULL) 238 return (ENOMEM); 239 240 RT_LOCK_INIT(rt); 241 242 return (0); 243 } 244 245 static void 246 rtentry_zfini(void *mem, int size) 247 { 248 struct rtentry *rt = mem; 249 250 RT_LOCK_DESTROY(rt); 251 counter_u64_free(rt->rt_pksent); 252 } 253 254 static int 255 rtentry_ctor(void *mem, int size, void *arg, int how) 256 { 257 struct rtentry *rt = mem; 258 259 bzero(rt, offsetof(struct rtentry, rt_endzero)); 260 counter_u64_zero(rt->rt_pksent); 261 rt->rt_chain = NULL; 262 263 return (0); 264 } 265 266 static void 267 rtentry_dtor(void *mem, int size, void *arg) 268 { 269 struct rtentry *rt = mem; 270 271 RT_UNLOCK_COND(rt); 272 } 273 274 static void 275 vnet_route_init(const void *unused __unused) 276 { 277 struct domain *dom; 278 struct rib_head **rnh; 279 int table; 280 int fam; 281 282 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) * 283 sizeof(struct rib_head *), M_RTABLE, M_WAITOK|M_ZERO); 284 285 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), 286 rtentry_ctor, rtentry_dtor, 287 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0); 288 for (dom = domains; dom; dom = dom->dom_next) { 289 if (dom->dom_rtattach == NULL) 290 continue; 291 292 for (table = 0; table < rt_numfibs; table++) { 293 fam = dom->dom_family; 294 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 295 break; 296 297 rnh = rt_tables_get_rnh_ptr(table, fam); 298 if (rnh == NULL) 299 panic("%s: rnh NULL", __func__); 300 dom->dom_rtattach((void **)rnh, 0); 301 } 302 } 303 } 304 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, 305 vnet_route_init, 0); 306 307 #ifdef VIMAGE 308 static void 309 vnet_route_uninit(const void *unused __unused) 310 { 311 int table; 312 int fam; 313 struct domain *dom; 314 struct rib_head **rnh; 315 316 for (dom = domains; dom; dom = dom->dom_next) { 317 if (dom->dom_rtdetach == NULL) 318 continue; 319 320 for (table = 0; table < rt_numfibs; table++) { 321 fam = dom->dom_family; 322 323 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 324 break; 325 326 rnh = rt_tables_get_rnh_ptr(table, fam); 327 if (rnh == NULL) 328 panic("%s: rnh NULL", __func__); 329 dom->dom_rtdetach((void **)rnh, 0); 330 } 331 } 332 333 free(V_rt_tables, M_RTABLE); 334 uma_zdestroy(V_rtzone); 335 } 336 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 337 vnet_route_uninit, 0); 338 #endif 339 340 struct rib_head * 341 rt_table_init(int offset) 342 { 343 struct rib_head *rh; 344 345 rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO); 346 347 /* TODO: These details should be hidded inside radix.c */ 348 /* Init masks tree */ 349 rn_inithead_internal(&rh->head, rh->rnh_nodes, offset); 350 rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0); 351 rh->head.rnh_masks = &rh->rmhead; 352 353 /* Init locks */ 354 RIB_LOCK_INIT(rh); 355 356 /* Finally, set base callbacks */ 357 rh->rnh_addaddr = rn_addroute; 358 rh->rnh_deladdr = rn_delete; 359 rh->rnh_matchaddr = rn_match; 360 rh->rnh_lookup = rn_lookup; 361 rh->rnh_walktree = rn_walktree; 362 rh->rnh_walktree_from = rn_walktree_from; 363 364 return (rh); 365 } 366 367 static int 368 rt_freeentry(struct radix_node *rn, void *arg) 369 { 370 struct radix_head * const rnh = arg; 371 struct radix_node *x; 372 373 x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh); 374 if (x != NULL) 375 R_Free(x); 376 return (0); 377 } 378 379 void 380 rt_table_destroy(struct rib_head *rh) 381 { 382 383 rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head); 384 385 /* Assume table is already empty */ 386 RIB_LOCK_DESTROY(rh); 387 free(rh, M_RTABLE); 388 } 389 390 391 #ifndef _SYS_SYSPROTO_H_ 392 struct setfib_args { 393 int fibnum; 394 }; 395 #endif 396 int 397 sys_setfib(struct thread *td, struct setfib_args *uap) 398 { 399 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs) 400 return EINVAL; 401 td->td_proc->p_fibnum = uap->fibnum; 402 return (0); 403 } 404 405 /* 406 * Packet routing routines. 407 */ 408 void 409 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum) 410 { 411 struct rtentry *rt; 412 413 if ((rt = ro->ro_rt) != NULL) { 414 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) 415 return; 416 RTFREE(rt); 417 ro->ro_rt = NULL; 418 } 419 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum); 420 if (ro->ro_rt) 421 RT_UNLOCK(ro->ro_rt); 422 } 423 424 /* 425 * Look up the route that matches the address given 426 * Or, at least try.. Create a cloned route if needed. 427 * 428 * The returned route, if any, is locked. 429 */ 430 struct rtentry * 431 rtalloc1(struct sockaddr *dst, int report, u_long ignflags) 432 { 433 434 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB)); 435 } 436 437 struct rtentry * 438 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags, 439 u_int fibnum) 440 { 441 RIB_RLOCK_TRACKER; 442 struct rib_head *rh; 443 struct radix_node *rn; 444 struct rtentry *newrt; 445 struct rt_addrinfo info; 446 int err = 0, msgtype = RTM_MISS; 447 448 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum")); 449 rh = rt_tables_get_rnh(fibnum, dst->sa_family); 450 newrt = NULL; 451 if (rh == NULL) 452 goto miss; 453 454 /* 455 * Look up the address in the table for that Address Family 456 */ 457 if ((ignflags & RTF_RNH_LOCKED) == 0) 458 RIB_RLOCK(rh); 459 #ifdef INVARIANTS 460 else 461 RIB_LOCK_ASSERT(rh); 462 #endif 463 rn = rh->rnh_matchaddr(dst, &rh->head); 464 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) { 465 newrt = RNTORT(rn); 466 RT_LOCK(newrt); 467 RT_ADDREF(newrt); 468 if ((ignflags & RTF_RNH_LOCKED) == 0) 469 RIB_RUNLOCK(rh); 470 return (newrt); 471 472 } else if ((ignflags & RTF_RNH_LOCKED) == 0) 473 RIB_RUNLOCK(rh); 474 /* 475 * Either we hit the root or could not find any match, 476 * which basically means: "cannot get there from here". 477 */ 478 miss: 479 V_rtstat.rts_unreach++; 480 481 if (report) { 482 /* 483 * If required, report the failure to the supervising 484 * Authorities. 485 * For a delete, this is not an error. (report == 0) 486 */ 487 bzero(&info, sizeof(info)); 488 info.rti_info[RTAX_DST] = dst; 489 rt_missmsg_fib(msgtype, &info, 0, err, fibnum); 490 } 491 return (newrt); 492 } 493 494 /* 495 * Remove a reference count from an rtentry. 496 * If the count gets low enough, take it out of the routing table 497 */ 498 void 499 rtfree(struct rtentry *rt) 500 { 501 struct rib_head *rnh; 502 503 KASSERT(rt != NULL,("%s: NULL rt", __func__)); 504 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family); 505 KASSERT(rnh != NULL,("%s: NULL rnh", __func__)); 506 507 RT_LOCK_ASSERT(rt); 508 509 /* 510 * The callers should use RTFREE_LOCKED() or RTFREE(), so 511 * we should come here exactly with the last reference. 512 */ 513 RT_REMREF(rt); 514 if (rt->rt_refcnt > 0) { 515 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt); 516 goto done; 517 } 518 519 /* 520 * On last reference give the "close method" a chance 521 * to cleanup private state. This also permits (for 522 * IPv4 and IPv6) a chance to decide if the routing table 523 * entry should be purged immediately or at a later time. 524 * When an immediate purge is to happen the close routine 525 * typically calls rtexpunge which clears the RTF_UP flag 526 * on the entry so that the code below reclaims the storage. 527 */ 528 if (rt->rt_refcnt == 0 && rnh->rnh_close) 529 rnh->rnh_close((struct radix_node *)rt, &rnh->head); 530 531 /* 532 * If we are no longer "up" (and ref == 0) 533 * then we can free the resources associated 534 * with the route. 535 */ 536 if ((rt->rt_flags & RTF_UP) == 0) { 537 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 538 panic("rtfree 2"); 539 /* 540 * the rtentry must have been removed from the routing table 541 * so it is represented in rttrash.. remove that now. 542 */ 543 V_rttrash--; 544 #ifdef DIAGNOSTIC 545 if (rt->rt_refcnt < 0) { 546 printf("rtfree: %p not freed (neg refs)\n", rt); 547 goto done; 548 } 549 #endif 550 /* 551 * release references on items we hold them on.. 552 * e.g other routes and ifaddrs. 553 */ 554 if (rt->rt_ifa) 555 ifa_free(rt->rt_ifa); 556 /* 557 * The key is separatly alloc'd so free it (see rt_setgate()). 558 * This also frees the gateway, as they are always malloc'd 559 * together. 560 */ 561 R_Free(rt_key(rt)); 562 563 /* 564 * and the rtentry itself of course 565 */ 566 uma_zfree(V_rtzone, rt); 567 return; 568 } 569 done: 570 RT_UNLOCK(rt); 571 } 572 573 574 /* 575 * Force a routing table entry to the specified 576 * destination to go through the given gateway. 577 * Normally called as a result of a routing redirect 578 * message from the network layer. 579 */ 580 void 581 rtredirect_fib(struct sockaddr *dst, 582 struct sockaddr *gateway, 583 struct sockaddr *netmask, 584 int flags, 585 struct sockaddr *src, 586 u_int fibnum) 587 { 588 struct rtentry *rt; 589 int error = 0; 590 short *stat = NULL; 591 struct rt_addrinfo info; 592 struct ifaddr *ifa; 593 struct rib_head *rnh; 594 595 NET_EPOCH_ASSERT(); 596 597 ifa = NULL; 598 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 599 if (rnh == NULL) { 600 error = EAFNOSUPPORT; 601 goto out; 602 } 603 /* verify the gateway is directly reachable */ 604 if ((ifa = ifa_ifwithnet(gateway, 0, fibnum)) == NULL) { 605 error = ENETUNREACH; 606 goto out; 607 } 608 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */ 609 /* 610 * If the redirect isn't from our current router for this dst, 611 * it's either old or wrong. If it redirects us to ourselves, 612 * we have a routing loop, perhaps as a result of an interface 613 * going down recently. 614 */ 615 if (!(flags & RTF_DONE) && rt) { 616 if (!sa_equal(src, rt->rt_gateway)) { 617 error = EINVAL; 618 goto done; 619 } 620 if (rt->rt_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK) { 621 error = EINVAL; 622 goto done; 623 } 624 } 625 if ((flags & RTF_GATEWAY) && ifa_ifwithaddr_check(gateway)) { 626 error = EHOSTUNREACH; 627 goto done; 628 } 629 /* 630 * Create a new entry if we just got back a wildcard entry 631 * or the lookup failed. This is necessary for hosts 632 * which use routing redirects generated by smart gateways 633 * to dynamically build the routing tables. 634 */ 635 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) 636 goto create; 637 /* 638 * Don't listen to the redirect if it's 639 * for a route to an interface. 640 */ 641 if (rt->rt_flags & RTF_GATEWAY) { 642 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { 643 /* 644 * Changing from route to net => route to host. 645 * Create new route, rather than smashing route to net. 646 */ 647 create: 648 if (rt != NULL) 649 RTFREE_LOCKED(rt); 650 651 flags |= RTF_DYNAMIC; 652 bzero((caddr_t)&info, sizeof(info)); 653 info.rti_info[RTAX_DST] = dst; 654 info.rti_info[RTAX_GATEWAY] = gateway; 655 info.rti_info[RTAX_NETMASK] = netmask; 656 ifa_ref(ifa); 657 info.rti_ifa = ifa; 658 info.rti_flags = flags; 659 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum); 660 if (rt != NULL) { 661 RT_LOCK(rt); 662 flags = rt->rt_flags; 663 } 664 665 stat = &V_rtstat.rts_dynamic; 666 } else { 667 668 /* 669 * Smash the current notion of the gateway to 670 * this destination. Should check about netmask!!! 671 */ 672 if ((flags & RTF_GATEWAY) == 0) 673 rt->rt_flags &= ~RTF_GATEWAY; 674 rt->rt_flags |= RTF_MODIFIED; 675 flags |= RTF_MODIFIED; 676 stat = &V_rtstat.rts_newgateway; 677 /* 678 * add the key and gateway (in one malloc'd chunk). 679 */ 680 RT_UNLOCK(rt); 681 RIB_WLOCK(rnh); 682 RT_LOCK(rt); 683 rt_setgate(rt, rt_key(rt), gateway); 684 RIB_WUNLOCK(rnh); 685 } 686 } else 687 error = EHOSTUNREACH; 688 done: 689 if (rt) 690 RTFREE_LOCKED(rt); 691 out: 692 if (error) 693 V_rtstat.rts_badredirect++; 694 else if (stat != NULL) 695 (*stat)++; 696 bzero((caddr_t)&info, sizeof(info)); 697 info.rti_info[RTAX_DST] = dst; 698 info.rti_info[RTAX_GATEWAY] = gateway; 699 info.rti_info[RTAX_NETMASK] = netmask; 700 info.rti_info[RTAX_AUTHOR] = src; 701 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum); 702 } 703 704 /* 705 * Routing table ioctl interface. 706 */ 707 int 708 rtioctl_fib(u_long req, caddr_t data, u_int fibnum) 709 { 710 711 /* 712 * If more ioctl commands are added here, make sure the proper 713 * super-user checks are being performed because it is possible for 714 * prison-root to make it this far if raw sockets have been enabled 715 * in jails. 716 */ 717 #ifdef INET 718 /* Multicast goop, grrr... */ 719 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP; 720 #else /* INET */ 721 return ENXIO; 722 #endif /* INET */ 723 } 724 725 struct ifaddr * 726 ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway, 727 u_int fibnum) 728 { 729 struct ifaddr *ifa; 730 int not_found = 0; 731 732 MPASS(in_epoch(net_epoch_preempt)); 733 if ((flags & RTF_GATEWAY) == 0) { 734 /* 735 * If we are adding a route to an interface, 736 * and the interface is a pt to pt link 737 * we should search for the destination 738 * as our clue to the interface. Otherwise 739 * we can use the local address. 740 */ 741 ifa = NULL; 742 if (flags & RTF_HOST) 743 ifa = ifa_ifwithdstaddr(dst, fibnum); 744 if (ifa == NULL) 745 ifa = ifa_ifwithaddr(gateway); 746 } else { 747 /* 748 * If we are adding a route to a remote net 749 * or host, the gateway may still be on the 750 * other end of a pt to pt link. 751 */ 752 ifa = ifa_ifwithdstaddr(gateway, fibnum); 753 } 754 if (ifa == NULL) 755 ifa = ifa_ifwithnet(gateway, 0, fibnum); 756 if (ifa == NULL) { 757 struct rtentry *rt; 758 759 rt = rtalloc1_fib(gateway, 0, flags, fibnum); 760 if (rt == NULL) 761 goto out; 762 /* 763 * dismiss a gateway that is reachable only 764 * through the default router 765 */ 766 switch (gateway->sa_family) { 767 case AF_INET: 768 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) 769 not_found = 1; 770 break; 771 case AF_INET6: 772 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr)) 773 not_found = 1; 774 break; 775 default: 776 break; 777 } 778 if (!not_found && rt->rt_ifa != NULL) { 779 ifa = rt->rt_ifa; 780 } 781 RT_REMREF(rt); 782 RT_UNLOCK(rt); 783 if (not_found || ifa == NULL) 784 goto out; 785 } 786 if (ifa->ifa_addr->sa_family != dst->sa_family) { 787 struct ifaddr *oifa = ifa; 788 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 789 if (ifa == NULL) 790 ifa = oifa; 791 } 792 out: 793 return (ifa); 794 } 795 796 /* 797 * Do appropriate manipulations of a routing tree given 798 * all the bits of info needed 799 */ 800 int 801 rtrequest_fib(int req, 802 struct sockaddr *dst, 803 struct sockaddr *gateway, 804 struct sockaddr *netmask, 805 int flags, 806 struct rtentry **ret_nrt, 807 u_int fibnum) 808 { 809 struct rt_addrinfo info; 810 811 if (dst->sa_len == 0) 812 return(EINVAL); 813 814 bzero((caddr_t)&info, sizeof(info)); 815 info.rti_flags = flags; 816 info.rti_info[RTAX_DST] = dst; 817 info.rti_info[RTAX_GATEWAY] = gateway; 818 info.rti_info[RTAX_NETMASK] = netmask; 819 return rtrequest1_fib(req, &info, ret_nrt, fibnum); 820 } 821 822 823 /* 824 * Copy most of @rt data into @info. 825 * 826 * If @flags contains NHR_COPY, copies dst,netmask and gw to the 827 * pointers specified by @info structure. Assume such pointers 828 * are zeroed sockaddr-like structures with sa_len field initialized 829 * to reflect size of the provided buffer. if no NHR_COPY is specified, 830 * point dst,netmask and gw @info fields to appropriate @rt values. 831 * 832 * if @flags contains NHR_REF, do refcouting on rt_ifp. 833 * 834 * Returns 0 on success. 835 */ 836 int 837 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags) 838 { 839 struct rt_metrics *rmx; 840 struct sockaddr *src, *dst; 841 int sa_len; 842 843 if (flags & NHR_COPY) { 844 /* Copy destination if dst is non-zero */ 845 src = rt_key(rt); 846 dst = info->rti_info[RTAX_DST]; 847 sa_len = src->sa_len; 848 if (dst != NULL) { 849 if (src->sa_len > dst->sa_len) 850 return (ENOMEM); 851 memcpy(dst, src, src->sa_len); 852 info->rti_addrs |= RTA_DST; 853 } 854 855 /* Copy mask if set && dst is non-zero */ 856 src = rt_mask(rt); 857 dst = info->rti_info[RTAX_NETMASK]; 858 if (src != NULL && dst != NULL) { 859 860 /* 861 * Radix stores different value in sa_len, 862 * assume rt_mask() to have the same length 863 * as rt_key() 864 */ 865 if (sa_len > dst->sa_len) 866 return (ENOMEM); 867 memcpy(dst, src, src->sa_len); 868 info->rti_addrs |= RTA_NETMASK; 869 } 870 871 /* Copy gateway is set && dst is non-zero */ 872 src = rt->rt_gateway; 873 dst = info->rti_info[RTAX_GATEWAY]; 874 if ((rt->rt_flags & RTF_GATEWAY) && src != NULL && dst != NULL){ 875 if (src->sa_len > dst->sa_len) 876 return (ENOMEM); 877 memcpy(dst, src, src->sa_len); 878 info->rti_addrs |= RTA_GATEWAY; 879 } 880 } else { 881 info->rti_info[RTAX_DST] = rt_key(rt); 882 info->rti_addrs |= RTA_DST; 883 if (rt_mask(rt) != NULL) { 884 info->rti_info[RTAX_NETMASK] = rt_mask(rt); 885 info->rti_addrs |= RTA_NETMASK; 886 } 887 if (rt->rt_flags & RTF_GATEWAY) { 888 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway; 889 info->rti_addrs |= RTA_GATEWAY; 890 } 891 } 892 893 rmx = info->rti_rmx; 894 if (rmx != NULL) { 895 info->rti_mflags |= RTV_MTU; 896 rmx->rmx_mtu = rt->rt_mtu; 897 } 898 899 info->rti_flags = rt->rt_flags; 900 info->rti_ifp = rt->rt_ifp; 901 info->rti_ifa = rt->rt_ifa; 902 ifa_ref(info->rti_ifa); 903 if (flags & NHR_REF) { 904 /* Do 'traditional' refcouting */ 905 if_ref(info->rti_ifp); 906 } 907 908 return (0); 909 } 910 911 /* 912 * Lookups up route entry for @dst in RIB database for fib @fibnum. 913 * Exports entry data to @info using rt_exportinfo(). 914 * 915 * if @flags contains NHR_REF, refcouting is performed on rt_ifp. 916 * All references can be released later by calling rib_free_info() 917 * 918 * Returns 0 on success. 919 * Returns ENOENT for lookup failure, ENOMEM for export failure. 920 */ 921 int 922 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags, 923 uint32_t flowid, struct rt_addrinfo *info) 924 { 925 RIB_RLOCK_TRACKER; 926 struct rib_head *rh; 927 struct radix_node *rn; 928 struct rtentry *rt; 929 int error; 930 931 KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum")); 932 rh = rt_tables_get_rnh(fibnum, dst->sa_family); 933 if (rh == NULL) 934 return (ENOENT); 935 936 RIB_RLOCK(rh); 937 rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head); 938 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) { 939 rt = RNTORT(rn); 940 /* Ensure route & ifp is UP */ 941 if (RT_LINK_IS_UP(rt->rt_ifp)) { 942 flags = (flags & NHR_REF) | NHR_COPY; 943 error = rt_exportinfo(rt, info, flags); 944 RIB_RUNLOCK(rh); 945 946 return (error); 947 } 948 } 949 RIB_RUNLOCK(rh); 950 951 return (ENOENT); 952 } 953 954 /* 955 * Releases all references acquired by rib_lookup_info() when 956 * called with NHR_REF flags. 957 */ 958 void 959 rib_free_info(struct rt_addrinfo *info) 960 { 961 962 if_rele(info->rti_ifp); 963 } 964 965 /* 966 * Iterates over all existing fibs in system calling 967 * @setwa_f function prior to traversing each fib. 968 * Calls @wa_f function for each element in current fib. 969 * If af is not AF_UNSPEC, iterates over fibs in particular 970 * address family. 971 */ 972 void 973 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f, 974 void *arg) 975 { 976 struct rib_head *rnh; 977 uint32_t fibnum; 978 int i; 979 980 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 981 /* Do we want some specific family? */ 982 if (af != AF_UNSPEC) { 983 rnh = rt_tables_get_rnh(fibnum, af); 984 if (rnh == NULL) 985 continue; 986 if (setwa_f != NULL) 987 setwa_f(rnh, fibnum, af, arg); 988 989 RIB_WLOCK(rnh); 990 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg); 991 RIB_WUNLOCK(rnh); 992 continue; 993 } 994 995 for (i = 1; i <= AF_MAX; i++) { 996 rnh = rt_tables_get_rnh(fibnum, i); 997 if (rnh == NULL) 998 continue; 999 if (setwa_f != NULL) 1000 setwa_f(rnh, fibnum, i, arg); 1001 1002 RIB_WLOCK(rnh); 1003 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg); 1004 RIB_WUNLOCK(rnh); 1005 } 1006 } 1007 } 1008 1009 struct rt_delinfo 1010 { 1011 struct rt_addrinfo info; 1012 struct rib_head *rnh; 1013 struct rtentry *head; 1014 }; 1015 1016 /* 1017 * Conditionally unlinks @rn from radix tree based 1018 * on info data passed in @arg. 1019 */ 1020 static int 1021 rt_checkdelroute(struct radix_node *rn, void *arg) 1022 { 1023 struct rt_delinfo *di; 1024 struct rt_addrinfo *info; 1025 struct rtentry *rt; 1026 int error; 1027 1028 di = (struct rt_delinfo *)arg; 1029 rt = (struct rtentry *)rn; 1030 info = &di->info; 1031 error = 0; 1032 1033 info->rti_info[RTAX_DST] = rt_key(rt); 1034 info->rti_info[RTAX_NETMASK] = rt_mask(rt); 1035 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1036 1037 rt = rt_unlinkrte(di->rnh, info, &error); 1038 if (rt == NULL) { 1039 /* Either not allowed or not matched. Skip entry */ 1040 return (0); 1041 } 1042 1043 /* Entry was unlinked. Add to the list and return */ 1044 rt->rt_chain = di->head; 1045 di->head = rt; 1046 1047 return (0); 1048 } 1049 1050 /* 1051 * Iterates over all existing fibs in system. 1052 * Deletes each element for which @filter_f function returned 1053 * non-zero value. 1054 * If @af is not AF_UNSPEC, iterates over fibs in particular 1055 * address family. 1056 */ 1057 void 1058 rt_foreach_fib_walk_del(int af, rt_filter_f_t *filter_f, void *arg) 1059 { 1060 struct rib_head *rnh; 1061 struct rt_delinfo di; 1062 struct rtentry *rt; 1063 uint32_t fibnum; 1064 int i, start, end; 1065 1066 bzero(&di, sizeof(di)); 1067 di.info.rti_filter = filter_f; 1068 di.info.rti_filterdata = arg; 1069 1070 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 1071 /* Do we want some specific family? */ 1072 if (af != AF_UNSPEC) { 1073 start = af; 1074 end = af; 1075 } else { 1076 start = 1; 1077 end = AF_MAX; 1078 } 1079 1080 for (i = start; i <= end; i++) { 1081 rnh = rt_tables_get_rnh(fibnum, i); 1082 if (rnh == NULL) 1083 continue; 1084 di.rnh = rnh; 1085 1086 RIB_WLOCK(rnh); 1087 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di); 1088 RIB_WUNLOCK(rnh); 1089 1090 if (di.head == NULL) 1091 continue; 1092 1093 /* We might have something to reclaim */ 1094 while (di.head != NULL) { 1095 rt = di.head; 1096 di.head = rt->rt_chain; 1097 rt->rt_chain = NULL; 1098 1099 /* TODO std rt -> rt_addrinfo export */ 1100 di.info.rti_info[RTAX_DST] = rt_key(rt); 1101 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1102 1103 rt_notifydelete(rt, &di.info); 1104 RTFREE_LOCKED(rt); 1105 } 1106 1107 } 1108 } 1109 } 1110 1111 /* 1112 * Delete Routes for a Network Interface 1113 * 1114 * Called for each routing entry via the rnh->rnh_walktree() call above 1115 * to delete all route entries referencing a detaching network interface. 1116 * 1117 * Arguments: 1118 * rt pointer to rtentry 1119 * arg argument passed to rnh->rnh_walktree() - detaching interface 1120 * 1121 * Returns: 1122 * 0 successful 1123 * errno failed - reason indicated 1124 */ 1125 static int 1126 rt_ifdelroute(const struct rtentry *rt, void *arg) 1127 { 1128 struct ifnet *ifp = arg; 1129 1130 if (rt->rt_ifp != ifp) 1131 return (0); 1132 1133 /* 1134 * Protect (sorta) against walktree recursion problems 1135 * with cloned routes 1136 */ 1137 if ((rt->rt_flags & RTF_UP) == 0) 1138 return (0); 1139 1140 return (1); 1141 } 1142 1143 /* 1144 * Delete all remaining routes using this interface 1145 * Unfortuneatly the only way to do this is to slog through 1146 * the entire routing table looking for routes which point 1147 * to this interface...oh well... 1148 */ 1149 void 1150 rt_flushifroutes_af(struct ifnet *ifp, int af) 1151 { 1152 KASSERT((af >= 1 && af <= AF_MAX), ("%s: af %d not >= 1 and <= %d", 1153 __func__, af, AF_MAX)); 1154 1155 rt_foreach_fib_walk_del(af, rt_ifdelroute, ifp); 1156 } 1157 1158 void 1159 rt_flushifroutes(struct ifnet *ifp) 1160 { 1161 1162 rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp); 1163 } 1164 1165 /* 1166 * Conditionally unlinks rtentry matching data inside @info from @rnh. 1167 * Returns unlinked, locked and referenced @rtentry on success, 1168 * Returns NULL and sets @perror to: 1169 * ESRCH - if prefix was not found, 1170 * EADDRINUSE - if trying to delete PINNED route without appropriate flag. 1171 * ENOENT - if supplied filter function returned 0 (not matched). 1172 */ 1173 static struct rtentry * 1174 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror) 1175 { 1176 struct sockaddr *dst, *netmask; 1177 struct rtentry *rt; 1178 struct radix_node *rn; 1179 1180 dst = info->rti_info[RTAX_DST]; 1181 netmask = info->rti_info[RTAX_NETMASK]; 1182 1183 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head); 1184 if (rt == NULL) { 1185 *perror = ESRCH; 1186 return (NULL); 1187 } 1188 1189 if ((info->rti_flags & RTF_PINNED) == 0) { 1190 /* Check if target route can be deleted */ 1191 if (rt->rt_flags & RTF_PINNED) { 1192 *perror = EADDRINUSE; 1193 return (NULL); 1194 } 1195 } 1196 1197 if (info->rti_filter != NULL) { 1198 if (info->rti_filter(rt, info->rti_filterdata) == 0) { 1199 /* Not matched */ 1200 *perror = ENOENT; 1201 return (NULL); 1202 } 1203 1204 /* 1205 * Filter function requested rte deletion. 1206 * Ease the caller work by filling in remaining info 1207 * from that particular entry. 1208 */ 1209 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1210 } 1211 1212 /* 1213 * Remove the item from the tree and return it. 1214 * Complain if it is not there and do no more processing. 1215 */ 1216 *perror = ESRCH; 1217 #ifdef RADIX_MPATH 1218 if (rt_mpath_capable(rnh)) 1219 rn = rt_mpath_unlink(rnh, info, rt, perror); 1220 else 1221 #endif 1222 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head); 1223 if (rn == NULL) 1224 return (NULL); 1225 1226 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 1227 panic ("rtrequest delete"); 1228 1229 rt = RNTORT(rn); 1230 RT_LOCK(rt); 1231 RT_ADDREF(rt); 1232 rt->rt_flags &= ~RTF_UP; 1233 1234 *perror = 0; 1235 1236 return (rt); 1237 } 1238 1239 static void 1240 rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info) 1241 { 1242 struct ifaddr *ifa; 1243 1244 /* 1245 * give the protocol a chance to keep things in sync. 1246 */ 1247 ifa = rt->rt_ifa; 1248 if (ifa != NULL && ifa->ifa_rtrequest != NULL) 1249 ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1250 1251 /* 1252 * One more rtentry floating around that is not 1253 * linked to the routing table. rttrash will be decremented 1254 * when RTFREE(rt) is eventually called. 1255 */ 1256 V_rttrash++; 1257 } 1258 1259 1260 /* 1261 * These (questionable) definitions of apparent local variables apply 1262 * to the next two functions. XXXXXX!!! 1263 */ 1264 #define dst info->rti_info[RTAX_DST] 1265 #define gateway info->rti_info[RTAX_GATEWAY] 1266 #define netmask info->rti_info[RTAX_NETMASK] 1267 #define ifaaddr info->rti_info[RTAX_IFA] 1268 #define ifpaddr info->rti_info[RTAX_IFP] 1269 #define flags info->rti_flags 1270 1271 /* 1272 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined, 1273 * it will be referenced so the caller must free it. 1274 * 1275 * Assume basic consistency checks are executed by callers: 1276 * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well. 1277 */ 1278 int 1279 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) 1280 { 1281 struct epoch_tracker et; 1282 int needref, error; 1283 1284 /* 1285 * ifp may be specified by sockaddr_dl 1286 * when protocol address is ambiguous. 1287 */ 1288 error = 0; 1289 needref = (info->rti_ifa == NULL); 1290 NET_EPOCH_ENTER(et); 1291 1292 /* If we have interface specified by the ifindex in the address, use it */ 1293 if (info->rti_ifp == NULL && ifpaddr != NULL && 1294 ifpaddr->sa_family == AF_LINK) { 1295 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)ifpaddr; 1296 if (sdl->sdl_index != 0) 1297 info->rti_ifp = ifnet_byindex(sdl->sdl_index); 1298 } 1299 /* 1300 * If we have source address specified, try to find it 1301 * TODO: avoid enumerating all ifas on all interfaces. 1302 */ 1303 if (info->rti_ifa == NULL && ifaaddr != NULL) 1304 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 1305 if (info->rti_ifa == NULL) { 1306 struct sockaddr *sa; 1307 1308 /* 1309 * Most common use case for the userland-supplied routes. 1310 * 1311 * Choose sockaddr to select ifa. 1312 * -- if ifp is set -- 1313 * Order of preference: 1314 * 1) IFA address 1315 * 2) gateway address 1316 * Note: for interface routes link-level gateway address 1317 * is specified to indicate the interface index without 1318 * specifying RTF_GATEWAY. In this case, ignore gateway 1319 * Note: gateway AF may be different from dst AF. In this case, 1320 * ignore gateway 1321 * 3) final destination. 1322 * 4) if all of these fails, try to get at least link-level ifa. 1323 * -- else -- 1324 * try to lookup gateway or dst in the routing table to get ifa 1325 */ 1326 if (info->rti_info[RTAX_IFA] != NULL) 1327 sa = info->rti_info[RTAX_IFA]; 1328 else if ((info->rti_flags & RTF_GATEWAY) != 0 && 1329 gateway->sa_family == dst->sa_family) 1330 sa = gateway; 1331 else 1332 sa = dst; 1333 if (info->rti_ifp != NULL) { 1334 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 1335 /* Case 4 */ 1336 if (info->rti_ifa == NULL && gateway != NULL) 1337 info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp); 1338 } else if (dst != NULL && gateway != NULL) 1339 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway, 1340 fibnum); 1341 else if (sa != NULL) 1342 info->rti_ifa = ifa_ifwithroute(flags, sa, sa, 1343 fibnum); 1344 } 1345 if (needref && info->rti_ifa != NULL) { 1346 if (info->rti_ifp == NULL) 1347 info->rti_ifp = info->rti_ifa->ifa_ifp; 1348 ifa_ref(info->rti_ifa); 1349 } else 1350 error = ENETUNREACH; 1351 NET_EPOCH_EXIT(et); 1352 return (error); 1353 } 1354 1355 static int 1356 if_updatemtu_cb(struct radix_node *rn, void *arg) 1357 { 1358 struct rtentry *rt; 1359 struct if_mtuinfo *ifmtu; 1360 1361 rt = (struct rtentry *)rn; 1362 ifmtu = (struct if_mtuinfo *)arg; 1363 1364 if (rt->rt_ifp != ifmtu->ifp) 1365 return (0); 1366 1367 if (rt->rt_mtu >= ifmtu->mtu) { 1368 /* We have to decrease mtu regardless of flags */ 1369 rt->rt_mtu = ifmtu->mtu; 1370 return (0); 1371 } 1372 1373 /* 1374 * New MTU is bigger. Check if are allowed to alter it 1375 */ 1376 if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) { 1377 1378 /* 1379 * Skip routes with user-supplied MTU and 1380 * non-interface routes 1381 */ 1382 return (0); 1383 } 1384 1385 /* We are safe to update route MTU */ 1386 rt->rt_mtu = ifmtu->mtu; 1387 1388 return (0); 1389 } 1390 1391 void 1392 rt_updatemtu(struct ifnet *ifp) 1393 { 1394 struct if_mtuinfo ifmtu; 1395 struct rib_head *rnh; 1396 int i, j; 1397 1398 ifmtu.ifp = ifp; 1399 1400 /* 1401 * Try to update rt_mtu for all routes using this interface 1402 * Unfortunately the only way to do this is to traverse all 1403 * routing tables in all fibs/domains. 1404 */ 1405 for (i = 1; i <= AF_MAX; i++) { 1406 ifmtu.mtu = if_getmtu_family(ifp, i); 1407 for (j = 0; j < rt_numfibs; j++) { 1408 rnh = rt_tables_get_rnh(j, i); 1409 if (rnh == NULL) 1410 continue; 1411 RIB_WLOCK(rnh); 1412 rnh->rnh_walktree(&rnh->head, if_updatemtu_cb, &ifmtu); 1413 RIB_WUNLOCK(rnh); 1414 } 1415 } 1416 } 1417 1418 1419 #if 0 1420 int p_sockaddr(char *buf, int buflen, struct sockaddr *s); 1421 int rt_print(char *buf, int buflen, struct rtentry *rt); 1422 1423 int 1424 p_sockaddr(char *buf, int buflen, struct sockaddr *s) 1425 { 1426 void *paddr = NULL; 1427 1428 switch (s->sa_family) { 1429 case AF_INET: 1430 paddr = &((struct sockaddr_in *)s)->sin_addr; 1431 break; 1432 case AF_INET6: 1433 paddr = &((struct sockaddr_in6 *)s)->sin6_addr; 1434 break; 1435 } 1436 1437 if (paddr == NULL) 1438 return (0); 1439 1440 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL) 1441 return (0); 1442 1443 return (strlen(buf)); 1444 } 1445 1446 int 1447 rt_print(char *buf, int buflen, struct rtentry *rt) 1448 { 1449 struct sockaddr *addr, *mask; 1450 int i = 0; 1451 1452 addr = rt_key(rt); 1453 mask = rt_mask(rt); 1454 1455 i = p_sockaddr(buf, buflen, addr); 1456 if (!(rt->rt_flags & RTF_HOST)) { 1457 buf[i++] = '/'; 1458 i += p_sockaddr(buf + i, buflen - i, mask); 1459 } 1460 1461 if (rt->rt_flags & RTF_GATEWAY) { 1462 buf[i++] = '>'; 1463 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway); 1464 } 1465 1466 return (i); 1467 } 1468 #endif 1469 1470 #ifdef RADIX_MPATH 1471 /* 1472 * Deletes key for single-path routes, unlinks rtentry with 1473 * gateway specified in @info from multi-path routes. 1474 * 1475 * Returnes unlinked entry. In case of failure, returns NULL 1476 * and sets @perror to ESRCH. 1477 */ 1478 static struct radix_node * 1479 rt_mpath_unlink(struct rib_head *rnh, struct rt_addrinfo *info, 1480 struct rtentry *rto, int *perror) 1481 { 1482 /* 1483 * if we got multipath routes, we require users to specify 1484 * a matching RTAX_GATEWAY. 1485 */ 1486 struct rtentry *rt; // *rto = NULL; 1487 struct radix_node *rn; 1488 struct sockaddr *gw; 1489 1490 gw = info->rti_info[RTAX_GATEWAY]; 1491 rt = rt_mpath_matchgate(rto, gw); 1492 if (rt == NULL) { 1493 *perror = ESRCH; 1494 return (NULL); 1495 } 1496 1497 /* 1498 * this is the first entry in the chain 1499 */ 1500 if (rto == rt) { 1501 rn = rn_mpath_next((struct radix_node *)rt); 1502 /* 1503 * there is another entry, now it's active 1504 */ 1505 if (rn) { 1506 rto = RNTORT(rn); 1507 RT_LOCK(rto); 1508 rto->rt_flags |= RTF_UP; 1509 RT_UNLOCK(rto); 1510 } else if (rt->rt_flags & RTF_GATEWAY) { 1511 /* 1512 * For gateway routes, we need to 1513 * make sure that we we are deleting 1514 * the correct gateway. 1515 * rt_mpath_matchgate() does not 1516 * check the case when there is only 1517 * one route in the chain. 1518 */ 1519 if (gw && 1520 (rt->rt_gateway->sa_len != gw->sa_len || 1521 memcmp(rt->rt_gateway, gw, gw->sa_len))) { 1522 *perror = ESRCH; 1523 return (NULL); 1524 } 1525 } 1526 1527 /* 1528 * use the normal delete code to remove 1529 * the first entry 1530 */ 1531 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head); 1532 *perror = 0; 1533 return (rn); 1534 } 1535 1536 /* 1537 * if the entry is 2nd and on up 1538 */ 1539 if (rt_mpath_deldup(rto, rt) == 0) 1540 panic ("rtrequest1: rt_mpath_deldup"); 1541 *perror = 0; 1542 rn = (struct radix_node *)rt; 1543 return (rn); 1544 } 1545 #endif 1546 1547 int 1548 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt, 1549 u_int fibnum) 1550 { 1551 int error = 0; 1552 struct rtentry *rt, *rt_old; 1553 struct radix_node *rn; 1554 struct rib_head *rnh; 1555 struct ifaddr *ifa; 1556 struct sockaddr *ndst; 1557 struct sockaddr_storage mdst; 1558 1559 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum")); 1560 KASSERT((flags & RTF_RNH_LOCKED) == 0, ("rtrequest1_fib: locked")); 1561 switch (dst->sa_family) { 1562 case AF_INET6: 1563 case AF_INET: 1564 /* We support multiple FIBs. */ 1565 break; 1566 default: 1567 fibnum = RT_DEFAULT_FIB; 1568 break; 1569 } 1570 1571 /* 1572 * Find the correct routing tree to use for this Address Family 1573 */ 1574 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1575 if (rnh == NULL) 1576 return (EAFNOSUPPORT); 1577 1578 /* 1579 * If we are adding a host route then we don't want to put 1580 * a netmask in the tree, nor do we want to clone it. 1581 */ 1582 if (flags & RTF_HOST) 1583 netmask = NULL; 1584 1585 switch (req) { 1586 case RTM_DELETE: 1587 if (netmask) { 1588 if (dst->sa_len > sizeof(mdst)) 1589 return (EINVAL); 1590 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask); 1591 dst = (struct sockaddr *)&mdst; 1592 } 1593 1594 RIB_WLOCK(rnh); 1595 rt = rt_unlinkrte(rnh, info, &error); 1596 RIB_WUNLOCK(rnh); 1597 if (error != 0) 1598 return (error); 1599 1600 rt_notifydelete(rt, info); 1601 1602 /* 1603 * If the caller wants it, then it can have it, 1604 * but it's up to it to free the rtentry as we won't be 1605 * doing it. 1606 */ 1607 if (ret_nrt) { 1608 *ret_nrt = rt; 1609 RT_UNLOCK(rt); 1610 } else 1611 RTFREE_LOCKED(rt); 1612 break; 1613 case RTM_RESOLVE: 1614 /* 1615 * resolve was only used for route cloning 1616 * here for compat 1617 */ 1618 break; 1619 case RTM_ADD: 1620 if ((flags & RTF_GATEWAY) && !gateway) 1621 return (EINVAL); 1622 if (dst && gateway && (dst->sa_family != gateway->sa_family) && 1623 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK)) 1624 return (EINVAL); 1625 1626 if (info->rti_ifa == NULL) { 1627 error = rt_getifa_fib(info, fibnum); 1628 if (error) 1629 return (error); 1630 } 1631 rt = uma_zalloc(V_rtzone, M_NOWAIT); 1632 if (rt == NULL) { 1633 return (ENOBUFS); 1634 } 1635 rt->rt_flags = RTF_UP | flags; 1636 rt->rt_fibnum = fibnum; 1637 /* 1638 * Add the gateway. Possibly re-malloc-ing the storage for it. 1639 */ 1640 if ((error = rt_setgate(rt, dst, gateway)) != 0) { 1641 uma_zfree(V_rtzone, rt); 1642 return (error); 1643 } 1644 1645 /* 1646 * point to the (possibly newly malloc'd) dest address. 1647 */ 1648 ndst = (struct sockaddr *)rt_key(rt); 1649 1650 /* 1651 * make sure it contains the value we want (masked if needed). 1652 */ 1653 if (netmask) { 1654 rt_maskedcopy(dst, ndst, netmask); 1655 } else 1656 bcopy(dst, ndst, dst->sa_len); 1657 1658 /* 1659 * We use the ifa reference returned by rt_getifa_fib(). 1660 * This moved from below so that rnh->rnh_addaddr() can 1661 * examine the ifa and ifa->ifa_ifp if it so desires. 1662 */ 1663 ifa = info->rti_ifa; 1664 ifa_ref(ifa); 1665 rt->rt_ifa = ifa; 1666 rt->rt_ifp = ifa->ifa_ifp; 1667 rt->rt_weight = 1; 1668 1669 rt_setmetrics(info, rt); 1670 1671 RIB_WLOCK(rnh); 1672 RT_LOCK(rt); 1673 #ifdef RADIX_MPATH 1674 /* do not permit exactly the same dst/mask/gw pair */ 1675 if (rt_mpath_capable(rnh) && 1676 rt_mpath_conflict(rnh, rt, netmask)) { 1677 RIB_WUNLOCK(rnh); 1678 1679 ifa_free(rt->rt_ifa); 1680 R_Free(rt_key(rt)); 1681 uma_zfree(V_rtzone, rt); 1682 return (EEXIST); 1683 } 1684 #endif 1685 1686 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ 1687 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes); 1688 1689 rt_old = NULL; 1690 if (rn == NULL && (info->rti_flags & RTF_PINNED) != 0) { 1691 1692 /* 1693 * Force removal and re-try addition 1694 * TODO: better multipath&pinned support 1695 */ 1696 struct sockaddr *info_dst = info->rti_info[RTAX_DST]; 1697 info->rti_info[RTAX_DST] = ndst; 1698 /* Do not delete existing PINNED(interface) routes */ 1699 info->rti_flags &= ~RTF_PINNED; 1700 rt_old = rt_unlinkrte(rnh, info, &error); 1701 info->rti_flags |= RTF_PINNED; 1702 info->rti_info[RTAX_DST] = info_dst; 1703 if (rt_old != NULL) 1704 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, 1705 rt->rt_nodes); 1706 } 1707 RIB_WUNLOCK(rnh); 1708 1709 if (rt_old != NULL) 1710 RT_UNLOCK(rt_old); 1711 1712 /* 1713 * If it still failed to go into the tree, 1714 * then un-make it (this should be a function) 1715 */ 1716 if (rn == NULL) { 1717 ifa_free(rt->rt_ifa); 1718 R_Free(rt_key(rt)); 1719 uma_zfree(V_rtzone, rt); 1720 return (EEXIST); 1721 } 1722 1723 if (rt_old != NULL) { 1724 rt_notifydelete(rt_old, info); 1725 RTFREE(rt_old); 1726 } 1727 1728 /* 1729 * If this protocol has something to add to this then 1730 * allow it to do that as well. 1731 */ 1732 if (ifa->ifa_rtrequest) 1733 ifa->ifa_rtrequest(req, rt, info); 1734 1735 /* 1736 * actually return a resultant rtentry and 1737 * give the caller a single reference. 1738 */ 1739 if (ret_nrt) { 1740 *ret_nrt = rt; 1741 RT_ADDREF(rt); 1742 } 1743 rnh->rnh_gen++; /* Routing table updated */ 1744 RT_UNLOCK(rt); 1745 break; 1746 case RTM_CHANGE: 1747 RIB_WLOCK(rnh); 1748 error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum); 1749 RIB_WUNLOCK(rnh); 1750 break; 1751 default: 1752 error = EOPNOTSUPP; 1753 } 1754 1755 return (error); 1756 } 1757 1758 #undef dst 1759 #undef gateway 1760 #undef netmask 1761 #undef ifaaddr 1762 #undef ifpaddr 1763 #undef flags 1764 1765 static int 1766 rtrequest1_fib_change(struct rib_head *rnh, struct rt_addrinfo *info, 1767 struct rtentry **ret_nrt, u_int fibnum) 1768 { 1769 struct rtentry *rt = NULL; 1770 int error = 0; 1771 int free_ifa = 0; 1772 int family, mtu; 1773 struct if_mtuinfo ifmtu; 1774 1775 RIB_WLOCK_ASSERT(rnh); 1776 1777 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST], 1778 info->rti_info[RTAX_NETMASK], &rnh->head); 1779 1780 if (rt == NULL) 1781 return (ESRCH); 1782 1783 #ifdef RADIX_MPATH 1784 /* 1785 * If we got multipath routes, 1786 * we require users to specify a matching RTAX_GATEWAY. 1787 */ 1788 if (rt_mpath_capable(rnh)) { 1789 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]); 1790 if (rt == NULL) 1791 return (ESRCH); 1792 } 1793 #endif 1794 1795 RT_LOCK(rt); 1796 1797 rt_setmetrics(info, rt); 1798 1799 /* 1800 * New gateway could require new ifaddr, ifp; 1801 * flags may also be different; ifp may be specified 1802 * by ll sockaddr when protocol address is ambiguous 1803 */ 1804 if (((rt->rt_flags & RTF_GATEWAY) && 1805 info->rti_info[RTAX_GATEWAY] != NULL) || 1806 info->rti_info[RTAX_IFP] != NULL || 1807 (info->rti_info[RTAX_IFA] != NULL && 1808 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) { 1809 /* 1810 * XXX: Temporarily set RTF_RNH_LOCKED flag in the rti_flags 1811 * to avoid rlock in the ifa_ifwithroute(). 1812 */ 1813 info->rti_flags |= RTF_RNH_LOCKED; 1814 error = rt_getifa_fib(info, fibnum); 1815 info->rti_flags &= ~RTF_RNH_LOCKED; 1816 if (info->rti_ifa != NULL) 1817 free_ifa = 1; 1818 1819 if (error != 0) 1820 goto bad; 1821 } 1822 1823 /* Check if outgoing interface has changed */ 1824 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa && 1825 rt->rt_ifa != NULL) { 1826 if (rt->rt_ifa->ifa_rtrequest != NULL) 1827 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info); 1828 ifa_free(rt->rt_ifa); 1829 rt->rt_ifa = NULL; 1830 } 1831 /* Update gateway address */ 1832 if (info->rti_info[RTAX_GATEWAY] != NULL) { 1833 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]); 1834 if (error != 0) 1835 goto bad; 1836 1837 rt->rt_flags &= ~RTF_GATEWAY; 1838 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags); 1839 } 1840 1841 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) { 1842 ifa_ref(info->rti_ifa); 1843 rt->rt_ifa = info->rti_ifa; 1844 rt->rt_ifp = info->rti_ifp; 1845 } 1846 /* Allow some flags to be toggled on change. */ 1847 rt->rt_flags &= ~RTF_FMASK; 1848 rt->rt_flags |= info->rti_flags & RTF_FMASK; 1849 1850 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL) 1851 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info); 1852 1853 /* Alter route MTU if necessary */ 1854 if (rt->rt_ifp != NULL) { 1855 family = info->rti_info[RTAX_DST]->sa_family; 1856 mtu = if_getmtu_family(rt->rt_ifp, family); 1857 /* Set default MTU */ 1858 if (rt->rt_mtu == 0) 1859 rt->rt_mtu = mtu; 1860 if (rt->rt_mtu != mtu) { 1861 /* Check if we really need to update */ 1862 ifmtu.ifp = rt->rt_ifp; 1863 ifmtu.mtu = mtu; 1864 if_updatemtu_cb(rt->rt_nodes, &ifmtu); 1865 } 1866 } 1867 1868 /* 1869 * This route change may have modified the route's gateway. In that 1870 * case, any inpcbs that have cached this route need to invalidate their 1871 * llentry cache. 1872 */ 1873 rnh->rnh_gen++; 1874 1875 if (ret_nrt) { 1876 *ret_nrt = rt; 1877 RT_ADDREF(rt); 1878 } 1879 bad: 1880 RT_UNLOCK(rt); 1881 if (free_ifa != 0) { 1882 ifa_free(info->rti_ifa); 1883 info->rti_ifa = NULL; 1884 } 1885 return (error); 1886 } 1887 1888 static void 1889 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt) 1890 { 1891 1892 if (info->rti_mflags & RTV_MTU) { 1893 if (info->rti_rmx->rmx_mtu != 0) { 1894 1895 /* 1896 * MTU was explicitly provided by user. 1897 * Keep it. 1898 */ 1899 rt->rt_flags |= RTF_FIXEDMTU; 1900 } else { 1901 1902 /* 1903 * User explicitly sets MTU to 0. 1904 * Assume rollback to default. 1905 */ 1906 rt->rt_flags &= ~RTF_FIXEDMTU; 1907 } 1908 rt->rt_mtu = info->rti_rmx->rmx_mtu; 1909 } 1910 if (info->rti_mflags & RTV_WEIGHT) 1911 rt->rt_weight = info->rti_rmx->rmx_weight; 1912 /* Kernel -> userland timebase conversion. */ 1913 if (info->rti_mflags & RTV_EXPIRE) 1914 rt->rt_expire = info->rti_rmx->rmx_expire ? 1915 info->rti_rmx->rmx_expire - time_second + time_uptime : 0; 1916 } 1917 1918 int 1919 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate) 1920 { 1921 /* XXX dst may be overwritten, can we move this to below */ 1922 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate); 1923 1924 /* 1925 * Prepare to store the gateway in rt->rt_gateway. 1926 * Both dst and gateway are stored one after the other in the same 1927 * malloc'd chunk. If we have room, we can reuse the old buffer, 1928 * rt_gateway already points to the right place. 1929 * Otherwise, malloc a new block and update the 'dst' address. 1930 */ 1931 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) { 1932 caddr_t new; 1933 1934 R_Malloc(new, caddr_t, dlen + glen); 1935 if (new == NULL) 1936 return ENOBUFS; 1937 /* 1938 * XXX note, we copy from *dst and not *rt_key(rt) because 1939 * rt_setgate() can be called to initialize a newly 1940 * allocated route entry, in which case rt_key(rt) == NULL 1941 * (and also rt->rt_gateway == NULL). 1942 * Free()/free() handle a NULL argument just fine. 1943 */ 1944 bcopy(dst, new, dlen); 1945 R_Free(rt_key(rt)); /* free old block, if any */ 1946 rt_key(rt) = (struct sockaddr *)new; 1947 rt->rt_gateway = (struct sockaddr *)(new + dlen); 1948 } 1949 1950 /* 1951 * Copy the new gateway value into the memory chunk. 1952 */ 1953 bcopy(gate, rt->rt_gateway, glen); 1954 1955 return (0); 1956 } 1957 1958 void 1959 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 1960 { 1961 u_char *cp1 = (u_char *)src; 1962 u_char *cp2 = (u_char *)dst; 1963 u_char *cp3 = (u_char *)netmask; 1964 u_char *cplim = cp2 + *cp3; 1965 u_char *cplim2 = cp2 + *cp1; 1966 1967 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1968 cp3 += 2; 1969 if (cplim > cplim2) 1970 cplim = cplim2; 1971 while (cp2 < cplim) 1972 *cp2++ = *cp1++ & *cp3++; 1973 if (cp2 < cplim2) 1974 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1975 } 1976 1977 /* 1978 * Set up a routing table entry, normally 1979 * for an interface. 1980 */ 1981 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */ 1982 static inline int 1983 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum) 1984 { 1985 RIB_RLOCK_TRACKER; 1986 struct sockaddr *dst; 1987 struct sockaddr *netmask; 1988 struct rtentry *rt = NULL; 1989 struct rt_addrinfo info; 1990 int error = 0; 1991 int startfib, endfib; 1992 char tempbuf[_SOCKADDR_TMPSIZE]; 1993 int didwork = 0; 1994 int a_failure = 0; 1995 struct sockaddr_dl *sdl = NULL; 1996 struct rib_head *rnh; 1997 1998 if (flags & RTF_HOST) { 1999 dst = ifa->ifa_dstaddr; 2000 netmask = NULL; 2001 } else { 2002 dst = ifa->ifa_addr; 2003 netmask = ifa->ifa_netmask; 2004 } 2005 if (dst->sa_len == 0) 2006 return(EINVAL); 2007 switch (dst->sa_family) { 2008 case AF_INET6: 2009 case AF_INET: 2010 /* We support multiple FIBs. */ 2011 break; 2012 default: 2013 fibnum = RT_DEFAULT_FIB; 2014 break; 2015 } 2016 if (fibnum == RT_ALL_FIBS) { 2017 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) 2018 startfib = endfib = ifa->ifa_ifp->if_fib; 2019 else { 2020 startfib = 0; 2021 endfib = rt_numfibs - 1; 2022 } 2023 } else { 2024 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum")); 2025 startfib = fibnum; 2026 endfib = fibnum; 2027 } 2028 2029 /* 2030 * If it's a delete, check that if it exists, 2031 * it's on the correct interface or we might scrub 2032 * a route to another ifa which would 2033 * be confusing at best and possibly worse. 2034 */ 2035 if (cmd == RTM_DELETE) { 2036 /* 2037 * It's a delete, so it should already exist.. 2038 * If it's a net, mask off the host bits 2039 * (Assuming we have a mask) 2040 * XXX this is kinda inet specific.. 2041 */ 2042 if (netmask != NULL) { 2043 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask); 2044 dst = (struct sockaddr *)tempbuf; 2045 } 2046 } else if (cmd == RTM_ADD) { 2047 sdl = (struct sockaddr_dl *)tempbuf; 2048 bzero(sdl, sizeof(struct sockaddr_dl)); 2049 sdl->sdl_family = AF_LINK; 2050 sdl->sdl_len = sizeof(struct sockaddr_dl); 2051 sdl->sdl_type = ifa->ifa_ifp->if_type; 2052 sdl->sdl_index = ifa->ifa_ifp->if_index; 2053 } 2054 /* 2055 * Now go through all the requested tables (fibs) and do the 2056 * requested action. Realistically, this will either be fib 0 2057 * for protocols that don't do multiple tables or all the 2058 * tables for those that do. 2059 */ 2060 for ( fibnum = startfib; fibnum <= endfib; fibnum++) { 2061 if (cmd == RTM_DELETE) { 2062 struct radix_node *rn; 2063 /* 2064 * Look up an rtentry that is in the routing tree and 2065 * contains the correct info. 2066 */ 2067 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 2068 if (rnh == NULL) 2069 /* this table doesn't exist but others might */ 2070 continue; 2071 RIB_RLOCK(rnh); 2072 rn = rnh->rnh_lookup(dst, netmask, &rnh->head); 2073 #ifdef RADIX_MPATH 2074 if (rt_mpath_capable(rnh)) { 2075 2076 if (rn == NULL) 2077 error = ESRCH; 2078 else { 2079 rt = RNTORT(rn); 2080 /* 2081 * for interface route the 2082 * rt->rt_gateway is sockaddr_intf 2083 * for cloning ARP entries, so 2084 * rt_mpath_matchgate must use the 2085 * interface address 2086 */ 2087 rt = rt_mpath_matchgate(rt, 2088 ifa->ifa_addr); 2089 if (rt == NULL) 2090 error = ESRCH; 2091 } 2092 } 2093 #endif 2094 error = (rn == NULL || 2095 (rn->rn_flags & RNF_ROOT) || 2096 RNTORT(rn)->rt_ifa != ifa); 2097 RIB_RUNLOCK(rnh); 2098 if (error) { 2099 /* this is only an error if bad on ALL tables */ 2100 continue; 2101 } 2102 } 2103 /* 2104 * Do the actual request 2105 */ 2106 bzero((caddr_t)&info, sizeof(info)); 2107 ifa_ref(ifa); 2108 info.rti_ifa = ifa; 2109 info.rti_flags = flags | 2110 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED; 2111 info.rti_info[RTAX_DST] = dst; 2112 /* 2113 * doing this for compatibility reasons 2114 */ 2115 if (cmd == RTM_ADD) 2116 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)sdl; 2117 else 2118 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 2119 info.rti_info[RTAX_NETMASK] = netmask; 2120 error = rtrequest1_fib(cmd, &info, &rt, fibnum); 2121 2122 if (error == 0 && rt != NULL) { 2123 /* 2124 * notify any listening routing agents of the change 2125 */ 2126 RT_LOCK(rt); 2127 #ifdef RADIX_MPATH 2128 /* 2129 * in case address alias finds the first address 2130 * e.g. ifconfig bge0 192.0.2.246/24 2131 * e.g. ifconfig bge0 192.0.2.247/24 2132 * the address set in the route is 192.0.2.246 2133 * so we need to replace it with 192.0.2.247 2134 */ 2135 if (memcmp(rt->rt_ifa->ifa_addr, 2136 ifa->ifa_addr, ifa->ifa_addr->sa_len)) { 2137 ifa_free(rt->rt_ifa); 2138 ifa_ref(ifa); 2139 rt->rt_ifp = ifa->ifa_ifp; 2140 rt->rt_ifa = ifa; 2141 } 2142 #endif 2143 RT_ADDREF(rt); 2144 RT_UNLOCK(rt); 2145 rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum); 2146 RT_LOCK(rt); 2147 RT_REMREF(rt); 2148 if (cmd == RTM_DELETE) { 2149 /* 2150 * If we are deleting, and we found an entry, 2151 * then it's been removed from the tree.. 2152 * now throw it away. 2153 */ 2154 RTFREE_LOCKED(rt); 2155 } else { 2156 if (cmd == RTM_ADD) { 2157 /* 2158 * We just wanted to add it.. 2159 * we don't actually need a reference. 2160 */ 2161 RT_REMREF(rt); 2162 } 2163 RT_UNLOCK(rt); 2164 } 2165 didwork = 1; 2166 } 2167 if (error) 2168 a_failure = error; 2169 } 2170 if (cmd == RTM_DELETE) { 2171 if (didwork) { 2172 error = 0; 2173 } else { 2174 /* we only give an error if it wasn't in any table */ 2175 error = ((flags & RTF_HOST) ? 2176 EHOSTUNREACH : ENETUNREACH); 2177 } 2178 } else { 2179 if (a_failure) { 2180 /* return an error if any of them failed */ 2181 error = a_failure; 2182 } 2183 } 2184 return (error); 2185 } 2186 2187 /* 2188 * Set up a routing table entry, normally 2189 * for an interface. 2190 */ 2191 int 2192 rtinit(struct ifaddr *ifa, int cmd, int flags) 2193 { 2194 struct sockaddr *dst; 2195 int fib = RT_DEFAULT_FIB; 2196 2197 if (flags & RTF_HOST) { 2198 dst = ifa->ifa_dstaddr; 2199 } else { 2200 dst = ifa->ifa_addr; 2201 } 2202 2203 switch (dst->sa_family) { 2204 case AF_INET6: 2205 case AF_INET: 2206 /* We do support multiple FIBs. */ 2207 fib = RT_ALL_FIBS; 2208 break; 2209 } 2210 return (rtinit1(ifa, cmd, flags, fib)); 2211 } 2212 2213 /* 2214 * Announce interface address arrival/withdraw 2215 * Returns 0 on success. 2216 */ 2217 int 2218 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 2219 { 2220 2221 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2222 ("unexpected cmd %d", cmd)); 2223 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2224 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2225 2226 EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd); 2227 return (rtsock_addrmsg(cmd, ifa, fibnum)); 2228 } 2229 2230 /* 2231 * Announce route addition/removal. 2232 * Users of this function MUST validate input data BEFORE calling. 2233 * However we have to be able to handle invalid data: 2234 * if some userland app sends us "invalid" route message (invalid mask, 2235 * no dst, wrong address families, etc...) we need to pass it back 2236 * to app (and any other rtsock consumers) with rtm_errno field set to 2237 * non-zero value. 2238 * Returns 0 on success. 2239 */ 2240 int 2241 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt, 2242 int fibnum) 2243 { 2244 2245 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2246 ("unexpected cmd %d", cmd)); 2247 2248 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2249 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2250 2251 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__)); 2252 2253 return (rtsock_routemsg(cmd, ifp, error, rt, fibnum)); 2254 } 2255 2256 void 2257 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 2258 { 2259 2260 rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS); 2261 } 2262 2263 /* 2264 * This is called to generate messages from the routing socket 2265 * indicating a network interface has had addresses associated with it. 2266 */ 2267 void 2268 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt, 2269 int fibnum) 2270 { 2271 2272 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 2273 ("unexpected cmd %u", cmd)); 2274 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 2275 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 2276 2277 if (cmd == RTM_ADD) { 2278 rt_addrmsg(cmd, ifa, fibnum); 2279 if (rt != NULL) 2280 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum); 2281 } else { 2282 if (rt != NULL) 2283 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum); 2284 rt_addrmsg(cmd, ifa, fibnum); 2285 } 2286 } 2287 2288