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