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/route_ctl.h> 65 #include <net/route/route_var.h> 66 #include <net/route/nhop.h> 67 #include <net/route/shared.h> 68 #include <net/vnet.h> 69 70 #ifdef RADIX_MPATH 71 #include <net/radix_mpath.h> 72 #endif 73 74 #include <netinet/in.h> 75 #include <netinet/ip_mroute.h> 76 77 #include <vm/uma.h> 78 79 #define RT_MAXFIBS UINT16_MAX 80 81 /* Kernel config default option. */ 82 #ifdef ROUTETABLES 83 #if ROUTETABLES <= 0 84 #error "ROUTETABLES defined too low" 85 #endif 86 #if ROUTETABLES > RT_MAXFIBS 87 #error "ROUTETABLES defined too big" 88 #endif 89 #define RT_NUMFIBS ROUTETABLES 90 #endif /* ROUTETABLES */ 91 /* Initialize to default if not otherwise set. */ 92 #ifndef RT_NUMFIBS 93 #define RT_NUMFIBS 1 94 #endif 95 96 /* This is read-only.. */ 97 u_int rt_numfibs = RT_NUMFIBS; 98 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RDTUN, &rt_numfibs, 0, ""); 99 100 /* 101 * By default add routes to all fibs for new interfaces. 102 * Once this is set to 0 then only allocate routes on interface 103 * changes for the FIB of the caller when adding a new set of addresses 104 * to an interface. XXX this is a shotgun aproach to a problem that needs 105 * a more fine grained solution.. that will come. 106 * XXX also has the problems getting the FIB from curthread which will not 107 * always work given the fib can be overridden and prefixes can be added 108 * from the network stack context. 109 */ 110 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 1; 111 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET, 112 &VNET_NAME(rt_add_addr_allfibs), 0, ""); 113 114 VNET_PCPUSTAT_DEFINE(struct rtstat, rtstat); 115 116 VNET_PCPUSTAT_SYSINIT(rtstat); 117 #ifdef VIMAGE 118 VNET_PCPUSTAT_SYSUNINIT(rtstat); 119 #endif 120 121 VNET_DEFINE(struct rib_head *, rt_tables); 122 #define V_rt_tables VNET(rt_tables) 123 124 125 VNET_DEFINE(uma_zone_t, rtzone); /* Routing table UMA zone. */ 126 #define V_rtzone VNET(rtzone) 127 128 EVENTHANDLER_LIST_DEFINE(rt_addrmsg); 129 130 static int rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *, 131 void *arg); 132 static void destroy_rtentry_epoch(epoch_context_t ctx); 133 static int rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, 134 int flags); 135 136 /* 137 * handler for net.my_fibnum 138 */ 139 static int 140 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS) 141 { 142 int fibnum; 143 int error; 144 145 fibnum = curthread->td_proc->p_fibnum; 146 error = sysctl_handle_int(oidp, &fibnum, 0, req); 147 return (error); 148 } 149 150 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, 151 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 152 &sysctl_my_fibnum, "I", 153 "default FIB of caller"); 154 155 static __inline struct rib_head ** 156 rt_tables_get_rnh_ptr(int table, int fam) 157 { 158 struct rib_head **rnh; 159 160 KASSERT(table >= 0 && table < rt_numfibs, 161 ("%s: table out of bounds (0 <= %d < %d)", __func__, table, 162 rt_numfibs)); 163 KASSERT(fam >= 0 && fam < (AF_MAX + 1), 164 ("%s: fam out of bounds (0 <= %d < %d)", __func__, fam, AF_MAX+1)); 165 166 /* rnh is [fib=0][af=0]. */ 167 rnh = (struct rib_head **)V_rt_tables; 168 /* Get the offset to the requested table and fam. */ 169 rnh += table * (AF_MAX+1) + fam; 170 171 return (rnh); 172 } 173 174 struct rib_head * 175 rt_tables_get_rnh(int table, int fam) 176 { 177 178 return (*rt_tables_get_rnh_ptr(table, fam)); 179 } 180 181 u_int 182 rt_tables_get_gen(int table, int fam) 183 { 184 struct rib_head *rnh; 185 186 rnh = *rt_tables_get_rnh_ptr(table, fam); 187 KASSERT(rnh != NULL, ("%s: NULL rib_head pointer table %d fam %d", 188 __func__, table, fam)); 189 return (rnh->rnh_gen); 190 } 191 192 193 /* 194 * route initialization must occur before ip6_init2(), which happenas at 195 * SI_ORDER_MIDDLE. 196 */ 197 static void 198 route_init(void) 199 { 200 201 /* whack the tunable ints into line. */ 202 if (rt_numfibs > RT_MAXFIBS) 203 rt_numfibs = RT_MAXFIBS; 204 if (rt_numfibs == 0) 205 rt_numfibs = 1; 206 nhops_init(); 207 } 208 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, NULL); 209 210 static int 211 rtentry_zinit(void *mem, int size, int how) 212 { 213 struct rtentry *rt = mem; 214 215 RT_LOCK_INIT(rt); 216 217 return (0); 218 } 219 220 static void 221 rtentry_zfini(void *mem, int size) 222 { 223 struct rtentry *rt = mem; 224 225 RT_LOCK_DESTROY(rt); 226 } 227 228 static int 229 rtentry_ctor(void *mem, int size, void *arg, int how) 230 { 231 struct rtentry *rt = mem; 232 233 bzero(rt, offsetof(struct rtentry, rt_endzero)); 234 rt->rt_chain = NULL; 235 236 return (0); 237 } 238 239 static void 240 rtentry_dtor(void *mem, int size, void *arg) 241 { 242 struct rtentry *rt = mem; 243 244 RT_UNLOCK_COND(rt); 245 } 246 247 static void 248 vnet_route_init(const void *unused __unused) 249 { 250 struct domain *dom; 251 struct rib_head **rnh; 252 int table; 253 int fam; 254 255 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) * 256 sizeof(struct rib_head *), M_RTABLE, M_WAITOK|M_ZERO); 257 258 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), 259 rtentry_ctor, rtentry_dtor, 260 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0); 261 for (dom = domains; dom; dom = dom->dom_next) { 262 if (dom->dom_rtattach == NULL) 263 continue; 264 265 for (table = 0; table < rt_numfibs; table++) { 266 fam = dom->dom_family; 267 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 268 break; 269 270 rnh = rt_tables_get_rnh_ptr(table, fam); 271 if (rnh == NULL) 272 panic("%s: rnh NULL", __func__); 273 dom->dom_rtattach((void **)rnh, 0, table); 274 } 275 } 276 } 277 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, 278 vnet_route_init, 0); 279 280 #ifdef VIMAGE 281 static void 282 vnet_route_uninit(const void *unused __unused) 283 { 284 int table; 285 int fam; 286 struct domain *dom; 287 struct rib_head **rnh; 288 289 for (dom = domains; dom; dom = dom->dom_next) { 290 if (dom->dom_rtdetach == NULL) 291 continue; 292 293 for (table = 0; table < rt_numfibs; table++) { 294 fam = dom->dom_family; 295 296 if (table != 0 && fam != AF_INET6 && fam != AF_INET) 297 break; 298 299 rnh = rt_tables_get_rnh_ptr(table, fam); 300 if (rnh == NULL) 301 panic("%s: rnh NULL", __func__); 302 dom->dom_rtdetach((void **)rnh, 0); 303 } 304 } 305 306 /* 307 * dom_rtdetach calls rt_table_destroy(), which 308 * schedules deletion for all rtentries, nexthops and control 309 * structures. Wait for the destruction callbacks to fire. 310 * Note that this should result in freeing all rtentries, but 311 * nexthops deletions will be scheduled for the next epoch run 312 * and will be completed after vnet teardown. 313 */ 314 epoch_drain_callbacks(net_epoch_preempt); 315 316 free(V_rt_tables, M_RTABLE); 317 uma_zdestroy(V_rtzone); 318 } 319 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, 320 vnet_route_uninit, 0); 321 #endif 322 323 struct rib_head * 324 rt_table_init(int offset, int family, u_int fibnum) 325 { 326 struct rib_head *rh; 327 328 rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO); 329 330 /* TODO: These details should be hidded inside radix.c */ 331 /* Init masks tree */ 332 rn_inithead_internal(&rh->head, rh->rnh_nodes, offset); 333 rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0); 334 rh->head.rnh_masks = &rh->rmhead; 335 336 /* Save metadata associated with this routing table. */ 337 rh->rib_family = family; 338 rh->rib_fibnum = fibnum; 339 #ifdef VIMAGE 340 rh->rib_vnet = curvnet; 341 #endif 342 343 tmproutes_init(rh); 344 345 /* Init locks */ 346 RIB_LOCK_INIT(rh); 347 348 nhops_init_rib(rh); 349 350 /* Init subscription system */ 351 CK_STAILQ_INIT(&rh->rnh_subscribers); 352 353 /* Finally, set base callbacks */ 354 rh->rnh_addaddr = rn_addroute; 355 rh->rnh_deladdr = rn_delete; 356 rh->rnh_matchaddr = rn_match; 357 rh->rnh_lookup = rn_lookup; 358 rh->rnh_walktree = rn_walktree; 359 rh->rnh_walktree_from = rn_walktree_from; 360 361 return (rh); 362 } 363 364 static int 365 rt_freeentry(struct radix_node *rn, void *arg) 366 { 367 struct radix_head * const rnh = arg; 368 struct radix_node *x; 369 370 x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh); 371 if (x != NULL) 372 R_Free(x); 373 return (0); 374 } 375 376 void 377 rt_table_destroy(struct rib_head *rh) 378 { 379 380 tmproutes_destroy(rh); 381 382 rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head); 383 384 nhops_destroy_rib(rh); 385 386 /* Assume table is already empty */ 387 RIB_LOCK_DESTROY(rh); 388 free(rh, M_RTABLE); 389 } 390 391 392 #ifndef _SYS_SYSPROTO_H_ 393 struct setfib_args { 394 int fibnum; 395 }; 396 #endif 397 int 398 sys_setfib(struct thread *td, struct setfib_args *uap) 399 { 400 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs) 401 return EINVAL; 402 td->td_proc->p_fibnum = uap->fibnum; 403 return (0); 404 } 405 406 /* 407 * Remove a reference count from an rtentry. 408 * If the count gets low enough, take it out of the routing table 409 */ 410 void 411 rtfree(struct rtentry *rt) 412 { 413 414 KASSERT(rt != NULL,("%s: NULL rt", __func__)); 415 416 RT_LOCK_ASSERT(rt); 417 418 RT_UNLOCK(rt); 419 epoch_call(net_epoch_preempt, destroy_rtentry_epoch, 420 &rt->rt_epoch_ctx); 421 } 422 423 static void 424 destroy_rtentry(struct rtentry *rt) 425 { 426 427 /* 428 * At this moment rnh, nh_control may be already freed. 429 * nhop interface may have been migrated to a different vnet. 430 * Use vnet stored in the nexthop to delete the entry. 431 */ 432 CURVNET_SET(nhop_get_vnet(rt->rt_nhop)); 433 434 /* Unreference nexthop */ 435 nhop_free(rt->rt_nhop); 436 437 uma_zfree(V_rtzone, rt); 438 439 CURVNET_RESTORE(); 440 } 441 442 /* 443 * Epoch callback indicating rtentry is safe to destroy 444 */ 445 static void 446 destroy_rtentry_epoch(epoch_context_t ctx) 447 { 448 struct rtentry *rt; 449 450 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx); 451 452 destroy_rtentry(rt); 453 } 454 455 /* 456 * Adds a temporal redirect entry to the routing table. 457 * @fibnum: fib number 458 * @dst: destination to install redirect to 459 * @gateway: gateway to go via 460 * @author: sockaddr of originating router, can be NULL 461 * @ifp: interface to use for the redirected route 462 * @flags: set of flags to add. Allowed: RTF_GATEWAY 463 * @lifetime_sec: time in seconds to expire this redirect. 464 * 465 * Retuns 0 on success, errno otherwise. 466 */ 467 int 468 rib_add_redirect(u_int fibnum, struct sockaddr *dst, struct sockaddr *gateway, 469 struct sockaddr *author, struct ifnet *ifp, int flags, int lifetime_sec) 470 { 471 struct rtentry *rt; 472 int error; 473 struct rt_addrinfo info; 474 struct rt_metrics rti_rmx; 475 struct ifaddr *ifa; 476 477 NET_EPOCH_ASSERT(); 478 479 if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL) 480 return (EAFNOSUPPORT); 481 482 /* Verify the allowed flag mask. */ 483 KASSERT(((flags & ~(RTF_GATEWAY)) == 0), 484 ("invalid redirect flags: %x", flags)); 485 486 /* Get the best ifa for the given interface and gateway. */ 487 if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL) 488 return (ENETUNREACH); 489 ifa_ref(ifa); 490 491 bzero(&info, sizeof(info)); 492 info.rti_info[RTAX_DST] = dst; 493 info.rti_info[RTAX_GATEWAY] = gateway; 494 info.rti_ifa = ifa; 495 info.rti_ifp = ifp; 496 info.rti_flags = flags | RTF_HOST | RTF_DYNAMIC; 497 498 /* Setup route metrics to define expire time. */ 499 bzero(&rti_rmx, sizeof(rti_rmx)); 500 /* Set expire time as absolute. */ 501 rti_rmx.rmx_expire = lifetime_sec + time_second; 502 info.rti_mflags |= RTV_EXPIRE; 503 info.rti_rmx = &rti_rmx; 504 505 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum); 506 ifa_free(ifa); 507 508 if (error != 0) { 509 /* TODO: add per-fib redirect stats. */ 510 return (error); 511 } 512 513 RT_LOCK(rt); 514 flags = rt->rt_flags; 515 RT_UNLOCK(rt); 516 517 RTSTAT_INC(rts_dynamic); 518 519 /* Send notification of a route addition to userland. */ 520 bzero(&info, sizeof(info)); 521 info.rti_info[RTAX_DST] = dst; 522 info.rti_info[RTAX_GATEWAY] = gateway; 523 info.rti_info[RTAX_AUTHOR] = author; 524 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum); 525 526 return (0); 527 } 528 529 /* 530 * Routing table ioctl interface. 531 */ 532 int 533 rtioctl_fib(u_long req, caddr_t data, u_int fibnum) 534 { 535 536 /* 537 * If more ioctl commands are added here, make sure the proper 538 * super-user checks are being performed because it is possible for 539 * prison-root to make it this far if raw sockets have been enabled 540 * in jails. 541 */ 542 #ifdef INET 543 /* Multicast goop, grrr... */ 544 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP; 545 #else /* INET */ 546 return ENXIO; 547 #endif /* INET */ 548 } 549 550 struct ifaddr * 551 ifa_ifwithroute(int flags, const struct sockaddr *dst, 552 const struct sockaddr *gateway, u_int fibnum) 553 { 554 struct ifaddr *ifa; 555 556 NET_EPOCH_ASSERT(); 557 if ((flags & RTF_GATEWAY) == 0) { 558 /* 559 * If we are adding a route to an interface, 560 * and the interface is a pt to pt link 561 * we should search for the destination 562 * as our clue to the interface. Otherwise 563 * we can use the local address. 564 */ 565 ifa = NULL; 566 if (flags & RTF_HOST) 567 ifa = ifa_ifwithdstaddr(dst, fibnum); 568 if (ifa == NULL) 569 ifa = ifa_ifwithaddr(gateway); 570 } else { 571 /* 572 * If we are adding a route to a remote net 573 * or host, the gateway may still be on the 574 * other end of a pt to pt link. 575 */ 576 ifa = ifa_ifwithdstaddr(gateway, fibnum); 577 } 578 if (ifa == NULL) 579 ifa = ifa_ifwithnet(gateway, 0, fibnum); 580 if (ifa == NULL) { 581 struct nhop_object *nh; 582 583 nh = rib_lookup(fibnum, gateway, NHR_NONE, 0); 584 585 /* 586 * dismiss a gateway that is reachable only 587 * through the default router 588 */ 589 if ((nh == NULL) || (nh->nh_flags & NHF_DEFAULT)) 590 return (NULL); 591 ifa = nh->nh_ifa; 592 } 593 if (ifa->ifa_addr->sa_family != dst->sa_family) { 594 struct ifaddr *oifa = ifa; 595 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); 596 if (ifa == NULL) 597 ifa = oifa; 598 } 599 600 return (ifa); 601 } 602 603 /* 604 * Do appropriate manipulations of a routing tree given 605 * all the bits of info needed 606 */ 607 int 608 rtrequest_fib(int req, 609 struct sockaddr *dst, 610 struct sockaddr *gateway, 611 struct sockaddr *netmask, 612 int flags, 613 struct rtentry **ret_nrt, 614 u_int fibnum) 615 { 616 struct rt_addrinfo info; 617 618 if (dst->sa_len == 0) 619 return(EINVAL); 620 621 bzero((caddr_t)&info, sizeof(info)); 622 info.rti_flags = flags; 623 info.rti_info[RTAX_DST] = dst; 624 info.rti_info[RTAX_GATEWAY] = gateway; 625 info.rti_info[RTAX_NETMASK] = netmask; 626 return rtrequest1_fib(req, &info, ret_nrt, fibnum); 627 } 628 629 630 /* 631 * Copy most of @rt data into @info. 632 * 633 * If @flags contains NHR_COPY, copies dst,netmask and gw to the 634 * pointers specified by @info structure. Assume such pointers 635 * are zeroed sockaddr-like structures with sa_len field initialized 636 * to reflect size of the provided buffer. if no NHR_COPY is specified, 637 * point dst,netmask and gw @info fields to appropriate @rt values. 638 * 639 * if @flags contains NHR_REF, do refcouting on rt_ifp and rt_ifa. 640 * 641 * Returns 0 on success. 642 */ 643 int 644 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags) 645 { 646 struct rt_metrics *rmx; 647 struct sockaddr *src, *dst; 648 struct nhop_object *nh; 649 int sa_len; 650 651 if (flags & NHR_COPY) { 652 /* Copy destination if dst is non-zero */ 653 src = rt_key(rt); 654 dst = info->rti_info[RTAX_DST]; 655 sa_len = src->sa_len; 656 if (dst != NULL) { 657 if (src->sa_len > dst->sa_len) 658 return (ENOMEM); 659 memcpy(dst, src, src->sa_len); 660 info->rti_addrs |= RTA_DST; 661 } 662 663 /* Copy mask if set && dst is non-zero */ 664 src = rt_mask(rt); 665 dst = info->rti_info[RTAX_NETMASK]; 666 if (src != NULL && dst != NULL) { 667 668 /* 669 * Radix stores different value in sa_len, 670 * assume rt_mask() to have the same length 671 * as rt_key() 672 */ 673 if (sa_len > dst->sa_len) 674 return (ENOMEM); 675 memcpy(dst, src, src->sa_len); 676 info->rti_addrs |= RTA_NETMASK; 677 } 678 679 /* Copy gateway is set && dst is non-zero */ 680 src = &rt->rt_nhop->gw_sa; 681 dst = info->rti_info[RTAX_GATEWAY]; 682 if ((rt->rt_flags & RTF_GATEWAY) && src != NULL && dst != NULL){ 683 if (src->sa_len > dst->sa_len) 684 return (ENOMEM); 685 memcpy(dst, src, src->sa_len); 686 info->rti_addrs |= RTA_GATEWAY; 687 } 688 } else { 689 info->rti_info[RTAX_DST] = rt_key(rt); 690 info->rti_addrs |= RTA_DST; 691 if (rt_mask(rt) != NULL) { 692 info->rti_info[RTAX_NETMASK] = rt_mask(rt); 693 info->rti_addrs |= RTA_NETMASK; 694 } 695 if (rt->rt_flags & RTF_GATEWAY) { 696 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa; 697 info->rti_addrs |= RTA_GATEWAY; 698 } 699 } 700 701 nh = rt->rt_nhop; 702 rmx = info->rti_rmx; 703 if (rmx != NULL) { 704 info->rti_mflags |= RTV_MTU; 705 rmx->rmx_mtu = nh->nh_mtu; 706 } 707 708 info->rti_flags = rt->rt_flags | nhop_get_rtflags(nh); 709 info->rti_ifp = nh->nh_ifp; 710 info->rti_ifa = nh->nh_ifa; 711 if (flags & NHR_REF) { 712 if_ref(info->rti_ifp); 713 ifa_ref(info->rti_ifa); 714 } 715 716 return (0); 717 } 718 719 /* 720 * Lookups up route entry for @dst in RIB database for fib @fibnum. 721 * Exports entry data to @info using rt_exportinfo(). 722 * 723 * If @flags contains NHR_REF, refcouting is performed on rt_ifp and rt_ifa. 724 * All references can be released later by calling rib_free_info(). 725 * 726 * Returns 0 on success. 727 * Returns ENOENT for lookup failure, ENOMEM for export failure. 728 */ 729 int 730 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags, 731 uint32_t flowid, struct rt_addrinfo *info) 732 { 733 RIB_RLOCK_TRACKER; 734 struct rib_head *rh; 735 struct radix_node *rn; 736 struct rtentry *rt; 737 int error; 738 739 KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum")); 740 rh = rt_tables_get_rnh(fibnum, dst->sa_family); 741 if (rh == NULL) 742 return (ENOENT); 743 744 RIB_RLOCK(rh); 745 rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head); 746 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) { 747 rt = RNTORT(rn); 748 /* Ensure route & ifp is UP */ 749 if (RT_LINK_IS_UP(rt->rt_nhop->nh_ifp)) { 750 flags = (flags & NHR_REF) | NHR_COPY; 751 error = rt_exportinfo(rt, info, flags); 752 RIB_RUNLOCK(rh); 753 754 return (error); 755 } 756 } 757 RIB_RUNLOCK(rh); 758 759 return (ENOENT); 760 } 761 762 /* 763 * Releases all references acquired by rib_lookup_info() when 764 * called with NHR_REF flags. 765 */ 766 void 767 rib_free_info(struct rt_addrinfo *info) 768 { 769 770 ifa_free(info->rti_ifa); 771 if_rele(info->rti_ifp); 772 } 773 774 /* 775 * Iterates over all existing fibs in system calling 776 * @setwa_f function prior to traversing each fib. 777 * Calls @wa_f function for each element in current fib. 778 * If af is not AF_UNSPEC, iterates over fibs in particular 779 * address family. 780 */ 781 void 782 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f, 783 void *arg) 784 { 785 struct rib_head *rnh; 786 uint32_t fibnum; 787 int i; 788 789 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 790 /* Do we want some specific family? */ 791 if (af != AF_UNSPEC) { 792 rnh = rt_tables_get_rnh(fibnum, af); 793 if (rnh == NULL) 794 continue; 795 if (setwa_f != NULL) 796 setwa_f(rnh, fibnum, af, arg); 797 798 RIB_WLOCK(rnh); 799 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg); 800 RIB_WUNLOCK(rnh); 801 continue; 802 } 803 804 for (i = 1; i <= AF_MAX; i++) { 805 rnh = rt_tables_get_rnh(fibnum, i); 806 if (rnh == NULL) 807 continue; 808 if (setwa_f != NULL) 809 setwa_f(rnh, fibnum, i, arg); 810 811 RIB_WLOCK(rnh); 812 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg); 813 RIB_WUNLOCK(rnh); 814 } 815 } 816 } 817 818 /* 819 * Iterates over all existing fibs in system and deletes each element 820 * for which @filter_f function returns non-zero value. 821 * If @family is not AF_UNSPEC, iterates over fibs in particular 822 * address family. 823 */ 824 void 825 rt_foreach_fib_walk_del(int family, rt_filter_f_t *filter_f, void *arg) 826 { 827 u_int fibnum; 828 int i, start, end; 829 830 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 831 /* Do we want some specific family? */ 832 if (family != AF_UNSPEC) { 833 start = family; 834 end = family; 835 } else { 836 start = 1; 837 end = AF_MAX; 838 } 839 840 for (i = start; i <= end; i++) { 841 if (rt_tables_get_rnh(fibnum, i) == NULL) 842 continue; 843 844 rib_walk_del(fibnum, i, filter_f, arg, 0); 845 } 846 } 847 } 848 849 /* 850 * Delete Routes for a Network Interface 851 * 852 * Called for each routing entry via the rnh->rnh_walktree() call above 853 * to delete all route entries referencing a detaching network interface. 854 * 855 * Arguments: 856 * rt pointer to rtentry 857 * nh pointer to nhop 858 * arg argument passed to rnh->rnh_walktree() - detaching interface 859 * 860 * Returns: 861 * 0 successful 862 * errno failed - reason indicated 863 */ 864 static int 865 rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *nh, void *arg) 866 { 867 struct ifnet *ifp = arg; 868 869 if (nh->nh_ifp != ifp) 870 return (0); 871 872 /* 873 * Protect (sorta) against walktree recursion problems 874 * with cloned routes 875 */ 876 if ((rt->rt_flags & RTF_UP) == 0) 877 return (0); 878 879 return (1); 880 } 881 882 /* 883 * Delete all remaining routes using this interface 884 * Unfortuneatly the only way to do this is to slog through 885 * the entire routing table looking for routes which point 886 * to this interface...oh well... 887 */ 888 void 889 rt_flushifroutes_af(struct ifnet *ifp, int af) 890 { 891 KASSERT((af >= 1 && af <= AF_MAX), ("%s: af %d not >= 1 and <= %d", 892 __func__, af, AF_MAX)); 893 894 rt_foreach_fib_walk_del(af, rt_ifdelroute, ifp); 895 } 896 897 void 898 rt_flushifroutes(struct ifnet *ifp) 899 { 900 901 rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp); 902 } 903 904 /* 905 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined, 906 * it will be referenced so the caller must free it. 907 * 908 * Assume basic consistency checks are executed by callers: 909 * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well. 910 */ 911 int 912 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum) 913 { 914 const struct sockaddr *dst, *gateway, *ifpaddr, *ifaaddr; 915 struct epoch_tracker et; 916 int needref, error, flags; 917 918 dst = info->rti_info[RTAX_DST]; 919 gateway = info->rti_info[RTAX_GATEWAY]; 920 ifpaddr = info->rti_info[RTAX_IFP]; 921 ifaaddr = info->rti_info[RTAX_IFA]; 922 flags = info->rti_flags; 923 924 /* 925 * ifp may be specified by sockaddr_dl 926 * when protocol address is ambiguous. 927 */ 928 error = 0; 929 needref = (info->rti_ifa == NULL); 930 NET_EPOCH_ENTER(et); 931 932 /* If we have interface specified by the ifindex in the address, use it */ 933 if (info->rti_ifp == NULL && ifpaddr != NULL && 934 ifpaddr->sa_family == AF_LINK) { 935 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)ifpaddr; 936 if (sdl->sdl_index != 0) 937 info->rti_ifp = ifnet_byindex(sdl->sdl_index); 938 } 939 /* 940 * If we have source address specified, try to find it 941 * TODO: avoid enumerating all ifas on all interfaces. 942 */ 943 if (info->rti_ifa == NULL && ifaaddr != NULL) 944 info->rti_ifa = ifa_ifwithaddr(ifaaddr); 945 if (info->rti_ifa == NULL) { 946 const struct sockaddr *sa; 947 948 /* 949 * Most common use case for the userland-supplied routes. 950 * 951 * Choose sockaddr to select ifa. 952 * -- if ifp is set -- 953 * Order of preference: 954 * 1) IFA address 955 * 2) gateway address 956 * Note: for interface routes link-level gateway address 957 * is specified to indicate the interface index without 958 * specifying RTF_GATEWAY. In this case, ignore gateway 959 * Note: gateway AF may be different from dst AF. In this case, 960 * ignore gateway 961 * 3) final destination. 962 * 4) if all of these fails, try to get at least link-level ifa. 963 * -- else -- 964 * try to lookup gateway or dst in the routing table to get ifa 965 */ 966 if (info->rti_info[RTAX_IFA] != NULL) 967 sa = info->rti_info[RTAX_IFA]; 968 else if ((info->rti_flags & RTF_GATEWAY) != 0 && 969 gateway->sa_family == dst->sa_family) 970 sa = gateway; 971 else 972 sa = dst; 973 if (info->rti_ifp != NULL) { 974 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp); 975 /* Case 4 */ 976 if (info->rti_ifa == NULL && gateway != NULL) 977 info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp); 978 } else if (dst != NULL && gateway != NULL) 979 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway, 980 fibnum); 981 else if (sa != NULL) 982 info->rti_ifa = ifa_ifwithroute(flags, sa, sa, 983 fibnum); 984 } 985 if (needref && info->rti_ifa != NULL) { 986 if (info->rti_ifp == NULL) 987 info->rti_ifp = info->rti_ifa->ifa_ifp; 988 ifa_ref(info->rti_ifa); 989 } else 990 error = ENETUNREACH; 991 NET_EPOCH_EXIT(et); 992 return (error); 993 } 994 995 void 996 rt_updatemtu(struct ifnet *ifp) 997 { 998 struct rib_head *rnh; 999 int mtu; 1000 int i, j; 1001 1002 /* 1003 * Try to update rt_mtu for all routes using this interface 1004 * Unfortunately the only way to do this is to traverse all 1005 * routing tables in all fibs/domains. 1006 */ 1007 for (i = 1; i <= AF_MAX; i++) { 1008 mtu = if_getmtu_family(ifp, i); 1009 for (j = 0; j < rt_numfibs; j++) { 1010 rnh = rt_tables_get_rnh(j, i); 1011 if (rnh == NULL) 1012 continue; 1013 nhops_update_ifmtu(rnh, ifp, mtu); 1014 } 1015 } 1016 } 1017 1018 1019 #if 0 1020 int p_sockaddr(char *buf, int buflen, struct sockaddr *s); 1021 int rt_print(char *buf, int buflen, struct rtentry *rt); 1022 1023 int 1024 p_sockaddr(char *buf, int buflen, struct sockaddr *s) 1025 { 1026 void *paddr = NULL; 1027 1028 switch (s->sa_family) { 1029 case AF_INET: 1030 paddr = &((struct sockaddr_in *)s)->sin_addr; 1031 break; 1032 case AF_INET6: 1033 paddr = &((struct sockaddr_in6 *)s)->sin6_addr; 1034 break; 1035 } 1036 1037 if (paddr == NULL) 1038 return (0); 1039 1040 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL) 1041 return (0); 1042 1043 return (strlen(buf)); 1044 } 1045 1046 int 1047 rt_print(char *buf, int buflen, struct rtentry *rt) 1048 { 1049 struct sockaddr *addr, *mask; 1050 int i = 0; 1051 1052 addr = rt_key(rt); 1053 mask = rt_mask(rt); 1054 1055 i = p_sockaddr(buf, buflen, addr); 1056 if (!(rt->rt_flags & RTF_HOST)) { 1057 buf[i++] = '/'; 1058 i += p_sockaddr(buf + i, buflen - i, mask); 1059 } 1060 1061 if (rt->rt_flags & RTF_GATEWAY) { 1062 buf[i++] = '>'; 1063 i += p_sockaddr(buf + i, buflen - i, &rt->rt_nhop->gw_sa); 1064 } 1065 1066 return (i); 1067 } 1068 #endif 1069 1070 #ifdef RADIX_MPATH 1071 /* 1072 * Deletes key for single-path routes, unlinks rtentry with 1073 * gateway specified in @info from multi-path routes. 1074 * 1075 * Returnes unlinked entry. In case of failure, returns NULL 1076 * and sets @perror to ESRCH. 1077 */ 1078 struct radix_node * 1079 rt_mpath_unlink(struct rib_head *rnh, struct rt_addrinfo *info, 1080 struct rtentry *rto, int *perror) 1081 { 1082 /* 1083 * if we got multipath routes, we require users to specify 1084 * a matching RTAX_GATEWAY. 1085 */ 1086 struct rtentry *rt; // *rto = NULL; 1087 struct radix_node *rn; 1088 struct sockaddr *gw; 1089 1090 gw = info->rti_info[RTAX_GATEWAY]; 1091 rt = rt_mpath_matchgate(rto, gw); 1092 if (rt == NULL) { 1093 *perror = ESRCH; 1094 return (NULL); 1095 } 1096 1097 /* 1098 * this is the first entry in the chain 1099 */ 1100 if (rto == rt) { 1101 rn = rn_mpath_next((struct radix_node *)rt); 1102 /* 1103 * there is another entry, now it's active 1104 */ 1105 if (rn) { 1106 rto = RNTORT(rn); 1107 RT_LOCK(rto); 1108 rto->rt_flags |= RTF_UP; 1109 RT_UNLOCK(rto); 1110 } else if (rt->rt_flags & RTF_GATEWAY) { 1111 /* 1112 * For gateway routes, we need to 1113 * make sure that we we are deleting 1114 * the correct gateway. 1115 * rt_mpath_matchgate() does not 1116 * check the case when there is only 1117 * one route in the chain. 1118 */ 1119 if (gw && 1120 (rt->rt_nhop->gw_sa.sa_len != gw->sa_len || 1121 memcmp(&rt->rt_nhop->gw_sa, gw, gw->sa_len))) { 1122 *perror = ESRCH; 1123 return (NULL); 1124 } 1125 } 1126 1127 /* 1128 * use the normal delete code to remove 1129 * the first entry 1130 */ 1131 rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST], 1132 info->rti_info[RTAX_NETMASK], 1133 &rnh->head); 1134 *perror = 0; 1135 return (rn); 1136 } 1137 1138 /* 1139 * if the entry is 2nd and on up 1140 */ 1141 if (rt_mpath_deldup(rto, rt) == 0) 1142 panic ("rtrequest1: rt_mpath_deldup"); 1143 *perror = 0; 1144 rn = (struct radix_node *)rt; 1145 return (rn); 1146 } 1147 #endif 1148 1149 int 1150 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt, 1151 u_int fibnum) 1152 { 1153 const struct sockaddr *dst; 1154 struct rib_head *rnh; 1155 struct rib_cmd_info rc; 1156 int error; 1157 1158 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum")); 1159 KASSERT((info->rti_flags & RTF_RNH_LOCKED) == 0, ("rtrequest1_fib: locked")); 1160 NET_EPOCH_ASSERT(); 1161 1162 dst = info->rti_info[RTAX_DST]; 1163 1164 switch (dst->sa_family) { 1165 case AF_INET6: 1166 case AF_INET: 1167 /* We support multiple FIBs. */ 1168 break; 1169 default: 1170 fibnum = RT_DEFAULT_FIB; 1171 break; 1172 } 1173 1174 /* 1175 * Find the correct routing tree to use for this Address Family 1176 */ 1177 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1178 if (rnh == NULL) 1179 return (EAFNOSUPPORT); 1180 1181 /* 1182 * If we are adding a host route then we don't want to put 1183 * a netmask in the tree, nor do we want to clone it. 1184 */ 1185 if (info->rti_flags & RTF_HOST) 1186 info->rti_info[RTAX_NETMASK] = NULL; 1187 1188 bzero(&rc, sizeof(struct rib_cmd_info)); 1189 error = 0; 1190 switch (req) { 1191 case RTM_DELETE: 1192 error = del_route(rnh, info, &rc); 1193 break; 1194 case RTM_RESOLVE: 1195 /* 1196 * resolve was only used for route cloning 1197 * here for compat 1198 */ 1199 break; 1200 case RTM_ADD: 1201 error = add_route(rnh, info, &rc); 1202 break; 1203 case RTM_CHANGE: 1204 error = change_route(rnh, info, &rc); 1205 break; 1206 default: 1207 error = EOPNOTSUPP; 1208 } 1209 1210 if (ret_nrt != NULL) 1211 *ret_nrt = rc.rc_rt; 1212 1213 return (error); 1214 } 1215 1216 void 1217 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt) 1218 { 1219 1220 if (info->rti_mflags & RTV_WEIGHT) 1221 rt->rt_weight = info->rti_rmx->rmx_weight; 1222 /* Kernel -> userland timebase conversion. */ 1223 if (info->rti_mflags & RTV_EXPIRE) 1224 rt->rt_expire = info->rti_rmx->rmx_expire ? 1225 info->rti_rmx->rmx_expire - time_second + time_uptime : 0; 1226 } 1227 1228 void 1229 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask) 1230 { 1231 u_char *cp1 = (u_char *)src; 1232 u_char *cp2 = (u_char *)dst; 1233 u_char *cp3 = (u_char *)netmask; 1234 u_char *cplim = cp2 + *cp3; 1235 u_char *cplim2 = cp2 + *cp1; 1236 1237 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ 1238 cp3 += 2; 1239 if (cplim > cplim2) 1240 cplim = cplim2; 1241 while (cp2 < cplim) 1242 *cp2++ = *cp1++ & *cp3++; 1243 if (cp2 < cplim2) 1244 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); 1245 } 1246 1247 /* 1248 * Set up a routing table entry, normally 1249 * for an interface. 1250 */ 1251 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */ 1252 static inline int 1253 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum) 1254 { 1255 RIB_RLOCK_TRACKER; 1256 struct epoch_tracker et; 1257 struct sockaddr *dst; 1258 struct sockaddr *netmask; 1259 struct rtentry *rt = NULL; 1260 struct rt_addrinfo info; 1261 int error = 0; 1262 int startfib, endfib; 1263 char tempbuf[_SOCKADDR_TMPSIZE]; 1264 int didwork = 0; 1265 int a_failure = 0; 1266 struct sockaddr_dl_short *sdl = NULL; 1267 struct rib_head *rnh; 1268 1269 if (flags & RTF_HOST) { 1270 dst = ifa->ifa_dstaddr; 1271 netmask = NULL; 1272 } else { 1273 dst = ifa->ifa_addr; 1274 netmask = ifa->ifa_netmask; 1275 } 1276 if (dst->sa_len == 0) 1277 return(EINVAL); 1278 switch (dst->sa_family) { 1279 case AF_INET6: 1280 case AF_INET: 1281 /* We support multiple FIBs. */ 1282 break; 1283 default: 1284 fibnum = RT_DEFAULT_FIB; 1285 break; 1286 } 1287 if (fibnum == RT_ALL_FIBS) { 1288 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) 1289 startfib = endfib = ifa->ifa_ifp->if_fib; 1290 else { 1291 startfib = 0; 1292 endfib = rt_numfibs - 1; 1293 } 1294 } else { 1295 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum")); 1296 startfib = fibnum; 1297 endfib = fibnum; 1298 } 1299 1300 /* 1301 * If it's a delete, check that if it exists, 1302 * it's on the correct interface or we might scrub 1303 * a route to another ifa which would 1304 * be confusing at best and possibly worse. 1305 */ 1306 if (cmd == RTM_DELETE) { 1307 /* 1308 * It's a delete, so it should already exist.. 1309 * If it's a net, mask off the host bits 1310 * (Assuming we have a mask) 1311 * XXX this is kinda inet specific.. 1312 */ 1313 if (netmask != NULL) { 1314 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask); 1315 dst = (struct sockaddr *)tempbuf; 1316 } 1317 } else if (cmd == RTM_ADD) { 1318 sdl = (struct sockaddr_dl_short *)tempbuf; 1319 bzero(sdl, sizeof(struct sockaddr_dl_short)); 1320 sdl->sdl_family = AF_LINK; 1321 sdl->sdl_len = sizeof(struct sockaddr_dl_short); 1322 sdl->sdl_type = ifa->ifa_ifp->if_type; 1323 sdl->sdl_index = ifa->ifa_ifp->if_index; 1324 } 1325 /* 1326 * Now go through all the requested tables (fibs) and do the 1327 * requested action. Realistically, this will either be fib 0 1328 * for protocols that don't do multiple tables or all the 1329 * tables for those that do. 1330 */ 1331 for ( fibnum = startfib; fibnum <= endfib; fibnum++) { 1332 if (cmd == RTM_DELETE) { 1333 struct radix_node *rn; 1334 /* 1335 * Look up an rtentry that is in the routing tree and 1336 * contains the correct info. 1337 */ 1338 rnh = rt_tables_get_rnh(fibnum, dst->sa_family); 1339 if (rnh == NULL) 1340 /* this table doesn't exist but others might */ 1341 continue; 1342 RIB_RLOCK(rnh); 1343 rn = rnh->rnh_lookup(dst, netmask, &rnh->head); 1344 #ifdef RADIX_MPATH 1345 if (rt_mpath_capable(rnh)) { 1346 1347 if (rn == NULL) 1348 error = ESRCH; 1349 else { 1350 rt = RNTORT(rn); 1351 /* 1352 * for interface route the gateway 1353 * gateway is sockaddr_dl, so 1354 * rt_mpath_matchgate must use the 1355 * interface address 1356 */ 1357 rt = rt_mpath_matchgate(rt, 1358 ifa->ifa_addr); 1359 if (rt == NULL) 1360 error = ESRCH; 1361 } 1362 } 1363 #endif 1364 error = (rn == NULL || 1365 (rn->rn_flags & RNF_ROOT) || 1366 RNTORT(rn)->rt_nhop->nh_ifa != ifa); 1367 RIB_RUNLOCK(rnh); 1368 if (error) { 1369 /* this is only an error if bad on ALL tables */ 1370 continue; 1371 } 1372 } 1373 /* 1374 * Do the actual request 1375 */ 1376 bzero((caddr_t)&info, sizeof(info)); 1377 info.rti_ifa = ifa; 1378 info.rti_flags = flags | 1379 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED; 1380 info.rti_info[RTAX_DST] = dst; 1381 /* 1382 * doing this for compatibility reasons 1383 */ 1384 if (cmd == RTM_ADD) 1385 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)sdl; 1386 else 1387 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1388 info.rti_info[RTAX_NETMASK] = netmask; 1389 NET_EPOCH_ENTER(et); 1390 error = rtrequest1_fib(cmd, &info, &rt, fibnum); 1391 if (error == 0 && rt != NULL) { 1392 /* 1393 * notify any listening routing agents of the change 1394 */ 1395 1396 /* TODO: interface routes/aliases */ 1397 rt_newaddrmsg_fib(cmd, ifa, rt, fibnum); 1398 didwork = 1; 1399 } 1400 NET_EPOCH_EXIT(et); 1401 if (error) 1402 a_failure = error; 1403 } 1404 if (cmd == RTM_DELETE) { 1405 if (didwork) { 1406 error = 0; 1407 } else { 1408 /* we only give an error if it wasn't in any table */ 1409 error = ((flags & RTF_HOST) ? 1410 EHOSTUNREACH : ENETUNREACH); 1411 } 1412 } else { 1413 if (a_failure) { 1414 /* return an error if any of them failed */ 1415 error = a_failure; 1416 } 1417 } 1418 return (error); 1419 } 1420 1421 /* 1422 * Set up a routing table entry, normally 1423 * for an interface. 1424 */ 1425 int 1426 rtinit(struct ifaddr *ifa, int cmd, int flags) 1427 { 1428 struct sockaddr *dst; 1429 int fib = RT_DEFAULT_FIB; 1430 1431 if (flags & RTF_HOST) { 1432 dst = ifa->ifa_dstaddr; 1433 } else { 1434 dst = ifa->ifa_addr; 1435 } 1436 1437 switch (dst->sa_family) { 1438 case AF_INET6: 1439 case AF_INET: 1440 /* We do support multiple FIBs. */ 1441 fib = RT_ALL_FIBS; 1442 break; 1443 } 1444 return (rtinit1(ifa, cmd, flags, fib)); 1445 } 1446 1447 /* 1448 * Announce interface address arrival/withdraw 1449 * Returns 0 on success. 1450 */ 1451 int 1452 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 1453 { 1454 1455 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 1456 ("unexpected cmd %d", cmd)); 1457 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 1458 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 1459 1460 EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd); 1461 return (rtsock_addrmsg(cmd, ifa, fibnum)); 1462 } 1463 1464 /* 1465 * Announce kernel-originated route addition/removal to rtsock based on @rt data. 1466 * cmd: RTM_ cmd 1467 * @rt: valid rtentry 1468 * @ifp: target route interface 1469 * @fibnum: fib id or RT_ALL_FIBS 1470 * 1471 * Returns 0 on success. 1472 */ 1473 int 1474 rt_routemsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int rti_addrs, 1475 int fibnum) 1476 { 1477 1478 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 1479 ("unexpected cmd %d", cmd)); 1480 1481 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 1482 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 1483 1484 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__)); 1485 1486 return (rtsock_routemsg(cmd, rt, ifp, 0, fibnum)); 1487 } 1488 1489 /* 1490 * Announce kernel-originated route addition/removal to rtsock based on @rt data. 1491 * cmd: RTM_ cmd 1492 * @info: addrinfo structure with valid data. 1493 * @fibnum: fib id or RT_ALL_FIBS 1494 * 1495 * Returns 0 on success. 1496 */ 1497 int 1498 rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum) 1499 { 1500 1501 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE, 1502 ("unexpected cmd %d", cmd)); 1503 1504 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 1505 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 1506 1507 KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__)); 1508 1509 return (rtsock_routemsg_info(cmd, info, fibnum)); 1510 } 1511 1512 1513 /* 1514 * This is called to generate messages from the routing socket 1515 * indicating a network interface has had addresses associated with it. 1516 */ 1517 void 1518 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, struct rtentry *rt, int fibnum) 1519 { 1520 1521 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 1522 ("unexpected cmd %u", cmd)); 1523 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs), 1524 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs)); 1525 1526 if (cmd == RTM_ADD) { 1527 rt_addrmsg(cmd, ifa, fibnum); 1528 if (rt != NULL) 1529 rt_routemsg(cmd, rt, ifa->ifa_ifp, 0, fibnum); 1530 } else { 1531 if (rt != NULL) 1532 rt_routemsg(cmd, rt, ifa->ifa_ifp, 0, fibnum); 1533 rt_addrmsg(cmd, ifa, fibnum); 1534 } 1535 } 1536 1537