1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2020 Alexander V. Chernikov 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 #include "opt_inet.h" 31 #include "opt_inet6.h" 32 #include "opt_route.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/lock.h> 37 #include <sys/rwlock.h> 38 #include <sys/malloc.h> 39 #include <sys/socket.h> 40 #include <sys/sysctl.h> 41 #include <sys/kernel.h> 42 #include <sys/epoch.h> 43 44 #include <net/if.h> 45 #include <net/if_var.h> 46 #include <net/if_dl.h> 47 #include <net/route.h> 48 #include <net/route/route_ctl.h> 49 #include <net/route/route_var.h> 50 #include <net/route/nhop_utils.h> 51 #include <net/route/nhop.h> 52 #include <net/route/nhop_var.h> 53 #include <net/vnet.h> 54 55 #define DEBUG_MOD_NAME nhop_ctl 56 #define DEBUG_MAX_LEVEL LOG_DEBUG 57 #include <net/route/route_debug.h> 58 _DECLARE_DEBUG(LOG_INFO); 59 60 /* 61 * This file contains core functionality for the nexthop ("nhop") route subsystem. 62 * The business logic needed to create nexhop objects is implemented here. 63 * 64 * Nexthops in the original sense are the objects containing all the necessary 65 * information to forward the packet to the selected destination. 66 * In particular, nexthop is defined by a combination of 67 * ifp, ifa, aifp, mtu, gw addr(if set), nh_type, nh_upper_family, mask of rt_flags and 68 * NHF_DEFAULT 69 * 70 * Additionally, each nexthop gets assigned its unique index (nexthop index). 71 * It serves two purposes: first one is to ease the ability of userland programs to 72 * reference nexthops by their index. The second one allows lookup algorithms to 73 * to store index instead of pointer (2 bytes vs 8) as a lookup result. 74 * All nexthops are stored in the resizable hash table. 75 * 76 * Basically, this file revolves around supporting 3 functions: 77 * 1) nhop_create_from_info / nhop_create_from_nhop, which contains all 78 * business logic on filling the nexthop fields based on the provided request. 79 * 2) nhop_get(), which gets a usable referenced nexthops. 80 * 81 * Conventions: 82 * 1) non-exported functions start with verb 83 * 2) exported function starts with the subsystem prefix: "nhop" 84 */ 85 86 static int dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w); 87 88 static int finalize_nhop(struct nh_control *ctl, struct nhop_object *nh); 89 static struct ifnet *get_aifp(const struct nhop_object *nh); 90 static void fill_sdl_from_ifp(struct sockaddr_dl_short *sdl, const struct ifnet *ifp); 91 92 static void destroy_nhop_epoch(epoch_context_t ctx); 93 static void destroy_nhop(struct nhop_object *nh); 94 static struct rib_head *nhop_get_rh(const struct nhop_object *nh); 95 96 _Static_assert(__offsetof(struct nhop_object, nh_ifp) == 32, 97 "nhop_object: wrong nh_ifp offset"); 98 _Static_assert(sizeof(struct nhop_object) <= 128, 99 "nhop_object: size exceeds 128 bytes"); 100 101 static uma_zone_t nhops_zone; /* Global zone for each and every nexthop */ 102 103 #define NHOP_OBJECT_ALIGNED_SIZE roundup2(sizeof(struct nhop_object), \ 104 2 * CACHE_LINE_SIZE) 105 #define NHOP_PRIV_ALIGNED_SIZE roundup2(sizeof(struct nhop_priv), \ 106 2 * CACHE_LINE_SIZE) 107 void 108 nhops_init(void) 109 { 110 111 nhops_zone = uma_zcreate("routing nhops", 112 NHOP_OBJECT_ALIGNED_SIZE + NHOP_PRIV_ALIGNED_SIZE, 113 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 114 } 115 116 /* 117 * Fetches the interface of source address used by the route. 118 * In all cases except interface-address-route it would be the 119 * same as the transmit interfaces. 120 * However, for the interface address this function will return 121 * this interface ifp instead of loopback. This is needed to support 122 * link-local IPv6 loopback communications. 123 * 124 * Returns found ifp. 125 */ 126 static struct ifnet * 127 get_aifp(const struct nhop_object *nh) 128 { 129 struct ifnet *aifp = NULL; 130 131 /* 132 * Adjust the "outgoing" interface. If we're going to loop 133 * the packet back to ourselves, the ifp would be the loopback 134 * interface. However, we'd rather know the interface associated 135 * to the destination address (which should probably be one of 136 * our own addresses). 137 */ 138 if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) && 139 nh->gw_sa.sa_family == AF_LINK) { 140 aifp = ifnet_byindex(nh->gwl_sa.sdl_index); 141 if (aifp == NULL) { 142 FIB_NH_LOG(LOG_WARNING, nh, "unable to get aifp for %s index %d", 143 if_name(nh->nh_ifp), nh->gwl_sa.sdl_index); 144 } 145 } 146 147 if (aifp == NULL) 148 aifp = nh->nh_ifp; 149 150 return (aifp); 151 } 152 153 int 154 cmp_priv(const struct nhop_priv *_one, const struct nhop_priv *_two) 155 { 156 157 if (memcmp(_one->nh, _two->nh, NHOP_END_CMP) != 0) 158 return (0); 159 160 if (memcmp(_one, _two, NH_PRIV_END_CMP) != 0) 161 return (0); 162 163 return (1); 164 } 165 166 /* 167 * Conditionally sets @nh mtu data based on the @info data. 168 */ 169 static void 170 set_nhop_mtu_from_info(struct nhop_object *nh, const struct rt_addrinfo *info) 171 { 172 if (info->rti_mflags & RTV_MTU) 173 nhop_set_mtu(nh, info->rti_rmx->rmx_mtu, true); 174 } 175 176 /* 177 * Fills in shorted link-level sockadd version suitable to be stored inside the 178 * nexthop gateway buffer. 179 */ 180 static void 181 fill_sdl_from_ifp(struct sockaddr_dl_short *sdl, const struct ifnet *ifp) 182 { 183 184 bzero(sdl, sizeof(struct sockaddr_dl_short)); 185 sdl->sdl_family = AF_LINK; 186 sdl->sdl_len = sizeof(struct sockaddr_dl_short); 187 sdl->sdl_index = ifp->if_index; 188 sdl->sdl_type = ifp->if_type; 189 } 190 191 static int 192 set_nhop_gw_from_info(struct nhop_object *nh, struct rt_addrinfo *info) 193 { 194 struct sockaddr *gw; 195 196 gw = info->rti_info[RTAX_GATEWAY]; 197 MPASS(gw != NULL); 198 bool is_gw = info->rti_flags & RTF_GATEWAY; 199 200 if ((gw->sa_family == AF_LINK) && !is_gw) { 201 202 /* 203 * Interface route with interface specified by the interface 204 * index in sockadd_dl structure. It is used in the IPv6 loopback 205 * output code, where we need to preserve the original interface 206 * to maintain proper scoping. 207 * Despite the fact that nexthop code stores original interface 208 * in the separate field (nh_aifp, see below), write AF_LINK 209 * compatible sa with shorter total length. 210 */ 211 struct sockaddr_dl *sdl = (struct sockaddr_dl *)gw; 212 struct ifnet *ifp = ifnet_byindex(sdl->sdl_index); 213 if (ifp == NULL) { 214 FIB_NH_LOG(LOG_DEBUG, nh, "error: invalid ifindex %d", 215 sdl->sdl_index); 216 return (EINVAL); 217 } 218 nhop_set_direct_gw(nh, ifp); 219 } else { 220 221 /* 222 * Multiple options here: 223 * 224 * 1) RTF_GATEWAY with IPv4/IPv6 gateway data 225 * 2) Interface route with IPv4/IPv6 address of the 226 * matching interface. Some routing daemons do that 227 * instead of specifying ifindex in AF_LINK. 228 * 229 * In both cases, save the original nexthop to make the callers 230 * happy. 231 */ 232 if (!nhop_set_gw(nh, gw, is_gw)) 233 return (EINVAL); 234 } 235 return (0); 236 } 237 238 static void 239 set_nhop_expire_from_info(struct nhop_object *nh, const struct rt_addrinfo *info) 240 { 241 uint32_t nh_expire = 0; 242 243 /* Kernel -> userland timebase conversion. */ 244 if ((info->rti_mflags & RTV_EXPIRE) && (info->rti_rmx->rmx_expire > 0)) 245 nh_expire = info->rti_rmx->rmx_expire - time_second + time_uptime; 246 nhop_set_expire(nh, nh_expire); 247 } 248 249 /* 250 * Creates a new nexthop based on the information in @info. 251 * 252 * Returns: 253 * 0 on success, filling @nh_ret with the desired nexthop object ptr 254 * errno otherwise 255 */ 256 int 257 nhop_create_from_info(struct rib_head *rnh, struct rt_addrinfo *info, 258 struct nhop_object **nh_ret) 259 { 260 int error; 261 262 NET_EPOCH_ASSERT(); 263 264 MPASS(info->rti_ifa != NULL); 265 MPASS(info->rti_ifp != NULL); 266 267 if (info->rti_info[RTAX_GATEWAY] == NULL) { 268 FIB_RH_LOG(LOG_DEBUG, rnh, "error: empty gateway"); 269 return (EINVAL); 270 } 271 272 struct nhop_object *nh = nhop_alloc(rnh->rib_fibnum, rnh->rib_family); 273 if (nh == NULL) 274 return (ENOMEM); 275 276 if ((error = set_nhop_gw_from_info(nh, info)) != 0) { 277 nhop_free(nh); 278 return (error); 279 } 280 nhop_set_transmit_ifp(nh, info->rti_ifp); 281 282 nhop_set_blackhole(nh, info->rti_flags & (RTF_BLACKHOLE | RTF_REJECT)); 283 284 error = rnh->rnh_set_nh_pfxflags(rnh->rib_fibnum, info->rti_info[RTAX_DST], 285 info->rti_info[RTAX_NETMASK], nh); 286 287 nhop_set_redirect(nh, info->rti_flags & RTF_DYNAMIC); 288 nhop_set_pinned(nh, info->rti_flags & RTF_PINNED); 289 set_nhop_expire_from_info(nh, info); 290 nhop_set_rtflags(nh, info->rti_flags); 291 292 set_nhop_mtu_from_info(nh, info); 293 nhop_set_src(nh, info->rti_ifa); 294 295 /* 296 * The remaining fields are either set from nh_preadd hook 297 * or are computed from the provided data 298 */ 299 *nh_ret = nhop_get_nhop(nh, &error); 300 301 return (error); 302 } 303 304 /* 305 * Gets linked nhop using the provided @nh nexhop data. 306 * If linked nhop is found, returns it, freeing the provided one. 307 * If there is no such nexthop, attaches the remaining data to the 308 * provided nexthop and links it. 309 * 310 * Returns 0 on success, storing referenced nexthop in @pnh. 311 * Otherwise, errno is returned. 312 */ 313 struct nhop_object * 314 nhop_get_nhop(struct nhop_object *nh, int *perror) 315 { 316 struct rib_head *rnh = nhop_get_rh(nh); 317 318 return (nhop_get_nhop_internal(rnh, nh, perror)); 319 } 320 321 struct nhop_object * 322 nhop_get_nhop_internal(struct rib_head *rnh, struct nhop_object *nh, int *perror) 323 { 324 struct nhop_priv *tmp_priv; 325 int error; 326 327 nh->nh_aifp = get_aifp(nh); 328 329 /* Give the protocols chance to augment nexthop properties */ 330 error = rnh->rnh_augment_nh(rnh->rib_fibnum, nh); 331 if (error != 0) { 332 nhop_free(nh); 333 *perror = error; 334 return (NULL); 335 } 336 337 tmp_priv = find_nhop(rnh->nh_control, nh->nh_priv); 338 if (tmp_priv != NULL) { 339 nhop_free(nh); 340 *perror = 0; 341 return (tmp_priv->nh); 342 } 343 344 /* 345 * Existing nexthop not found, need to create new one. 346 * Note: multiple simultaneous requests 347 * can result in multiple equal nexhops existing in the 348 * nexthop table. This is not a not a problem until the 349 * relative number of such nexthops is significant, which 350 * is extremely unlikely. 351 */ 352 *perror = finalize_nhop(rnh->nh_control, nh); 353 return (*perror == 0 ? nh : NULL); 354 } 355 356 /* 357 * Update @nh with data supplied in @info. 358 * This is a helper function to support route changes. 359 * 360 * It limits the changes that can be done to the route to the following: 361 * 1) all combination of gateway changes 362 * 2) route flags (FLAG[123],STATIC) 363 * 3) route MTU 364 * 365 * Returns: 366 * 0 on success, errno otherwise 367 */ 368 static int 369 alter_nhop_from_info(struct nhop_object *nh, struct rt_addrinfo *info) 370 { 371 struct sockaddr *info_gw; 372 int error; 373 374 /* Update MTU if set in the request*/ 375 set_nhop_mtu_from_info(nh, info); 376 377 /* Only RTF_FLAG[123] and RTF_STATIC */ 378 uint32_t rt_flags = nhop_get_rtflags(nh) & ~RT_CHANGE_RTFLAGS_MASK; 379 rt_flags |= info->rti_flags & RT_CHANGE_RTFLAGS_MASK; 380 nhop_set_rtflags(nh, rt_flags); 381 382 /* Consider gateway change */ 383 info_gw = info->rti_info[RTAX_GATEWAY]; 384 if (info_gw != NULL) { 385 error = set_nhop_gw_from_info(nh, info); 386 if (error != 0) 387 return (error); 388 } 389 390 if (info->rti_ifa != NULL) 391 nhop_set_src(nh, info->rti_ifa); 392 if (info->rti_ifp != NULL) 393 nhop_set_transmit_ifp(nh, info->rti_ifp); 394 395 return (0); 396 } 397 398 /* 399 * Creates new nexthop based on @nh_orig and augmentation data from @info. 400 * Helper function used in the route changes, please see 401 * alter_nhop_from_info() comments for more details. 402 * 403 * Returns: 404 * 0 on success, filling @nh_ret with the desired nexthop object 405 * errno otherwise 406 */ 407 int 408 nhop_create_from_nhop(struct rib_head *rnh, const struct nhop_object *nh_orig, 409 struct rt_addrinfo *info, struct nhop_object **pnh) 410 { 411 struct nhop_object *nh; 412 int error; 413 414 NET_EPOCH_ASSERT(); 415 416 nh = nhop_alloc(rnh->rib_fibnum, rnh->rib_family); 417 if (nh == NULL) 418 return (ENOMEM); 419 420 nhop_copy(nh, nh_orig); 421 422 error = alter_nhop_from_info(nh, info); 423 if (error != 0) { 424 nhop_free(nh); 425 return (error); 426 } 427 428 *pnh = nhop_get_nhop(nh, &error); 429 430 return (error); 431 } 432 433 static bool 434 reference_nhop_deps(struct nhop_object *nh) 435 { 436 if (!ifa_try_ref(nh->nh_ifa)) 437 return (false); 438 nh->nh_aifp = get_aifp(nh); 439 if (!if_try_ref(nh->nh_aifp)) { 440 ifa_free(nh->nh_ifa); 441 return (false); 442 } 443 FIB_NH_LOG(LOG_DEBUG2, nh, "nh_aifp: %s nh_ifp %s", 444 if_name(nh->nh_aifp), if_name(nh->nh_ifp)); 445 if (!if_try_ref(nh->nh_ifp)) { 446 ifa_free(nh->nh_ifa); 447 if_rele(nh->nh_aifp); 448 return (false); 449 } 450 451 return (true); 452 } 453 454 /* 455 * Alocates/references the remaining bits of nexthop data and links 456 * it to the hash table. 457 * Returns 0 if successful, 458 * errno otherwise. @nh_priv is freed in case of error. 459 */ 460 static int 461 finalize_nhop(struct nh_control *ctl, struct nhop_object *nh) 462 { 463 464 /* Allocate per-cpu packet counter */ 465 nh->nh_pksent = counter_u64_alloc(M_NOWAIT); 466 if (nh->nh_pksent == NULL) { 467 nhop_free(nh); 468 RTSTAT_INC(rts_nh_alloc_failure); 469 FIB_NH_LOG(LOG_WARNING, nh, "counter_u64_alloc() failed"); 470 return (ENOMEM); 471 } 472 473 if (!reference_nhop_deps(nh)) { 474 counter_u64_free(nh->nh_pksent); 475 nhop_free(nh); 476 RTSTAT_INC(rts_nh_alloc_failure); 477 FIB_NH_LOG(LOG_WARNING, nh, "interface reference failed"); 478 return (EAGAIN); 479 } 480 481 /* Save vnet to ease destruction */ 482 nh->nh_priv->nh_vnet = curvnet; 483 484 /* Please see nhop_free() comments on the initial value */ 485 refcount_init(&nh->nh_priv->nh_linked, 2); 486 487 nh->nh_priv->nh_fibnum = ctl->ctl_rh->rib_fibnum; 488 489 if (link_nhop(ctl, nh->nh_priv) == 0) { 490 /* 491 * Adding nexthop to the datastructures 492 * failed. Call destructor w/o waiting for 493 * the epoch end, as nexthop is not used 494 * and return. 495 */ 496 char nhbuf[NHOP_PRINT_BUFSIZE]; 497 FIB_NH_LOG(LOG_WARNING, nh, "failed to link %s", 498 nhop_print_buf(nh, nhbuf, sizeof(nhbuf))); 499 destroy_nhop(nh); 500 501 return (ENOBUFS); 502 } 503 504 #if DEBUG_MAX_LEVEL >= LOG_DEBUG 505 char nhbuf[NHOP_PRINT_BUFSIZE]; 506 FIB_NH_LOG(LOG_DEBUG, nh, "finalized: %s", nhop_print_buf(nh, nhbuf, sizeof(nhbuf))); 507 #endif 508 509 return (0); 510 } 511 512 static void 513 destroy_nhop(struct nhop_object *nh) 514 { 515 if_rele(nh->nh_ifp); 516 if_rele(nh->nh_aifp); 517 ifa_free(nh->nh_ifa); 518 counter_u64_free(nh->nh_pksent); 519 520 uma_zfree(nhops_zone, nh); 521 } 522 523 /* 524 * Epoch callback indicating nhop is safe to destroy 525 */ 526 static void 527 destroy_nhop_epoch(epoch_context_t ctx) 528 { 529 struct nhop_priv *nh_priv; 530 531 nh_priv = __containerof(ctx, struct nhop_priv, nh_epoch_ctx); 532 533 destroy_nhop(nh_priv->nh); 534 } 535 536 void 537 nhop_ref_object(struct nhop_object *nh) 538 { 539 u_int old __diagused; 540 541 old = refcount_acquire(&nh->nh_priv->nh_refcnt); 542 KASSERT(old > 0, ("%s: nhop object %p has 0 refs", __func__, nh)); 543 } 544 545 int 546 nhop_try_ref_object(struct nhop_object *nh) 547 { 548 549 return (refcount_acquire_if_not_zero(&nh->nh_priv->nh_refcnt)); 550 } 551 552 void 553 nhop_free(struct nhop_object *nh) 554 { 555 struct nh_control *ctl; 556 struct nhop_priv *nh_priv = nh->nh_priv; 557 struct epoch_tracker et; 558 559 if (!refcount_release(&nh_priv->nh_refcnt)) 560 return; 561 562 /* allows to use nhop_free() during nhop init */ 563 if (__predict_false(nh_priv->nh_finalized == 0)) { 564 uma_zfree(nhops_zone, nh); 565 return; 566 } 567 568 #if DEBUG_MAX_LEVEL >= LOG_DEBUG 569 char nhbuf[NHOP_PRINT_BUFSIZE]; 570 FIB_NH_LOG(LOG_DEBUG, nh, "deleting %s", nhop_print_buf(nh, nhbuf, sizeof(nhbuf))); 571 #endif 572 573 /* 574 * There are only 2 places, where nh_linked can be decreased: 575 * rib destroy (nhops_destroy_rib) and this function. 576 * nh_link can never be increased. 577 * 578 * Hence, use initial value of 2 to make use of 579 * refcount_release_if_not_last(). 580 * 581 * There can be two scenarious when calling this function: 582 * 583 * 1) nh_linked value is 2. This means that either 584 * nhops_destroy_rib() has not been called OR it is running, 585 * but we are guaranteed that nh_control won't be freed in 586 * this epoch. Hence, nexthop can be safely unlinked. 587 * 588 * 2) nh_linked value is 1. In that case, nhops_destroy_rib() 589 * has been called and nhop unlink can be skipped. 590 */ 591 592 NET_EPOCH_ENTER(et); 593 if (refcount_release_if_not_last(&nh_priv->nh_linked)) { 594 ctl = nh_priv->nh_control; 595 if (unlink_nhop(ctl, nh_priv) == NULL) { 596 /* Do not try to reclaim */ 597 char nhbuf[NHOP_PRINT_BUFSIZE]; 598 FIB_NH_LOG(LOG_WARNING, nh, "failed to unlink %s", 599 nhop_print_buf(nh, nhbuf, sizeof(nhbuf))); 600 NET_EPOCH_EXIT(et); 601 return; 602 } 603 } 604 NET_EPOCH_EXIT(et); 605 606 epoch_call(net_epoch_preempt, destroy_nhop_epoch, 607 &nh_priv->nh_epoch_ctx); 608 } 609 610 void 611 nhop_ref_any(struct nhop_object *nh) 612 { 613 #ifdef ROUTE_MPATH 614 if (!NH_IS_NHGRP(nh)) 615 nhop_ref_object(nh); 616 else 617 nhgrp_ref_object((struct nhgrp_object *)nh); 618 #else 619 nhop_ref_object(nh); 620 #endif 621 } 622 623 void 624 nhop_free_any(struct nhop_object *nh) 625 { 626 627 #ifdef ROUTE_MPATH 628 if (!NH_IS_NHGRP(nh)) 629 nhop_free(nh); 630 else 631 nhgrp_free((struct nhgrp_object *)nh); 632 #else 633 nhop_free(nh); 634 #endif 635 } 636 637 /* Nhop-related methods */ 638 639 /* 640 * Allocates an empty unlinked nhop object. 641 * Returns object pointer or NULL on failure 642 */ 643 struct nhop_object * 644 nhop_alloc(uint32_t fibnum, int family) 645 { 646 struct nhop_object *nh; 647 struct nhop_priv *nh_priv; 648 649 nh = (struct nhop_object *)uma_zalloc(nhops_zone, M_NOWAIT | M_ZERO); 650 if (__predict_false(nh == NULL)) 651 return (NULL); 652 653 nh_priv = (struct nhop_priv *)((char *)nh + NHOP_OBJECT_ALIGNED_SIZE); 654 nh->nh_priv = nh_priv; 655 nh_priv->nh = nh; 656 657 nh_priv->nh_upper_family = family; 658 nh_priv->nh_fibnum = fibnum; 659 660 /* Setup refcount early to allow nhop_free() to work */ 661 refcount_init(&nh_priv->nh_refcnt, 1); 662 663 return (nh); 664 } 665 666 void 667 nhop_copy(struct nhop_object *nh, const struct nhop_object *nh_orig) 668 { 669 struct nhop_priv *nh_priv = nh->nh_priv; 670 671 nh->nh_flags = nh_orig->nh_flags; 672 nh->nh_mtu = nh_orig->nh_mtu; 673 memcpy(&nh->gw_sa, &nh_orig->gw_sa, nh_orig->gw_sa.sa_len); 674 nh->nh_ifp = nh_orig->nh_ifp; 675 nh->nh_ifa = nh_orig->nh_ifa; 676 nh->nh_aifp = nh_orig->nh_aifp; 677 678 nh_priv->nh_upper_family = nh_orig->nh_priv->nh_upper_family; 679 nh_priv->nh_neigh_family = nh_orig->nh_priv->nh_neigh_family; 680 nh_priv->nh_type = nh_orig->nh_priv->nh_type; 681 nh_priv->rt_flags = nh_orig->nh_priv->rt_flags; 682 nh_priv->nh_fibnum = nh_orig->nh_priv->nh_fibnum; 683 } 684 685 void 686 nhop_set_direct_gw(struct nhop_object *nh, struct ifnet *ifp) 687 { 688 nh->nh_flags &= ~NHF_GATEWAY; 689 nh->nh_priv->rt_flags &= ~RTF_GATEWAY; 690 nh->nh_priv->nh_neigh_family = nh->nh_priv->nh_upper_family; 691 692 fill_sdl_from_ifp(&nh->gwl_sa, ifp); 693 memset(&nh->gw_buf[nh->gw_sa.sa_len], 0, sizeof(nh->gw_buf) - nh->gw_sa.sa_len); 694 } 695 696 /* 697 * Sets gateway for the nexthop. 698 * It can be "normal" gateway with is_gw set or a special form of 699 * adding interface route, refering to it by specifying local interface 700 * address. In that case is_gw is set to false. 701 */ 702 bool 703 nhop_set_gw(struct nhop_object *nh, const struct sockaddr *gw, bool is_gw) 704 { 705 if (gw->sa_len > sizeof(nh->gw_buf)) { 706 FIB_NH_LOG(LOG_DEBUG, nh, "nhop SA size too big: AF %d len %u", 707 gw->sa_family, gw->sa_len); 708 return (false); 709 } 710 memcpy(&nh->gw_sa, gw, gw->sa_len); 711 memset(&nh->gw_buf[gw->sa_len], 0, sizeof(nh->gw_buf) - gw->sa_len); 712 713 if (is_gw) { 714 nh->nh_flags |= NHF_GATEWAY; 715 nh->nh_priv->rt_flags |= RTF_GATEWAY; 716 nh->nh_priv->nh_neigh_family = gw->sa_family; 717 } else { 718 nh->nh_flags &= ~NHF_GATEWAY; 719 nh->nh_priv->rt_flags &= ~RTF_GATEWAY; 720 nh->nh_priv->nh_neigh_family = nh->nh_priv->nh_upper_family; 721 } 722 723 return (true); 724 } 725 726 void 727 nhop_set_broadcast(struct nhop_object *nh, bool is_broadcast) 728 { 729 if (is_broadcast) { 730 nh->nh_flags |= NHF_BROADCAST; 731 nh->nh_priv->rt_flags |= RTF_BROADCAST; 732 } else { 733 nh->nh_flags &= ~NHF_BROADCAST; 734 nh->nh_priv->rt_flags &= ~RTF_BROADCAST; 735 } 736 } 737 738 void 739 nhop_set_blackhole(struct nhop_object *nh, int blackhole_rt_flag) 740 { 741 nh->nh_flags &= ~(NHF_BLACKHOLE | NHF_REJECT); 742 nh->nh_priv->rt_flags &= ~(RTF_BLACKHOLE | RTF_REJECT); 743 switch (blackhole_rt_flag) { 744 case RTF_BLACKHOLE: 745 nh->nh_flags |= NHF_BLACKHOLE; 746 nh->nh_priv->rt_flags |= RTF_BLACKHOLE; 747 break; 748 case RTF_REJECT: 749 nh->nh_flags |= NHF_REJECT; 750 nh->nh_priv->rt_flags |= RTF_REJECT; 751 break; 752 } 753 } 754 755 void 756 nhop_set_redirect(struct nhop_object *nh, bool is_redirect) 757 { 758 if (is_redirect) { 759 nh->nh_priv->rt_flags |= RTF_DYNAMIC; 760 nh->nh_flags |= NHF_REDIRECT; 761 } else { 762 nh->nh_priv->rt_flags &= ~RTF_DYNAMIC; 763 nh->nh_flags &= ~NHF_REDIRECT; 764 } 765 } 766 767 void 768 nhop_set_pinned(struct nhop_object *nh, bool is_pinned) 769 { 770 if (is_pinned) 771 nh->nh_priv->rt_flags |= RTF_PINNED; 772 else 773 nh->nh_priv->rt_flags &= ~RTF_PINNED; 774 } 775 776 uint32_t 777 nhop_get_idx(const struct nhop_object *nh) 778 { 779 780 return (nh->nh_priv->nh_idx); 781 } 782 783 enum nhop_type 784 nhop_get_type(const struct nhop_object *nh) 785 { 786 787 return (nh->nh_priv->nh_type); 788 } 789 790 void 791 nhop_set_type(struct nhop_object *nh, enum nhop_type nh_type) 792 { 793 794 nh->nh_priv->nh_type = nh_type; 795 } 796 797 int 798 nhop_get_rtflags(const struct nhop_object *nh) 799 { 800 801 return (nh->nh_priv->rt_flags); 802 } 803 804 /* 805 * Sets generic rtflags that are not covered by other functions. 806 */ 807 void 808 nhop_set_rtflags(struct nhop_object *nh, int rt_flags) 809 { 810 nh->nh_priv->rt_flags &= ~RT_SET_RTFLAGS_MASK; 811 nh->nh_priv->rt_flags |= (rt_flags & RT_SET_RTFLAGS_MASK); 812 } 813 814 /* 815 * Sets flags that are specific to the prefix (NHF_HOST or NHF_DEFAULT). 816 */ 817 void 818 nhop_set_pxtype_flag(struct nhop_object *nh, int nh_flag) 819 { 820 if (nh_flag == NHF_HOST) { 821 nh->nh_flags |= NHF_HOST; 822 nh->nh_flags &= ~NHF_DEFAULT; 823 nh->nh_priv->rt_flags |= RTF_HOST; 824 } else if (nh_flag == NHF_DEFAULT) { 825 nh->nh_flags |= NHF_DEFAULT; 826 nh->nh_flags &= ~NHF_HOST; 827 nh->nh_priv->rt_flags &= ~RTF_HOST; 828 } else { 829 nh->nh_flags &= ~(NHF_HOST | NHF_DEFAULT); 830 nh->nh_priv->rt_flags &= ~RTF_HOST; 831 } 832 } 833 834 /* 835 * Sets nhop MTU. Sets RTF_FIXEDMTU if mtu is explicitly 836 * specified by userland. 837 */ 838 void 839 nhop_set_mtu(struct nhop_object *nh, uint32_t mtu, bool from_user) 840 { 841 if (from_user) { 842 if (mtu != 0) 843 nh->nh_priv->rt_flags |= RTF_FIXEDMTU; 844 else 845 nh->nh_priv->rt_flags &= ~RTF_FIXEDMTU; 846 } 847 nh->nh_mtu = mtu; 848 } 849 850 void 851 nhop_set_src(struct nhop_object *nh, struct ifaddr *ifa) 852 { 853 nh->nh_ifa = ifa; 854 } 855 856 void 857 nhop_set_transmit_ifp(struct nhop_object *nh, struct ifnet *ifp) 858 { 859 nh->nh_ifp = ifp; 860 } 861 862 863 struct vnet * 864 nhop_get_vnet(const struct nhop_object *nh) 865 { 866 867 return (nh->nh_priv->nh_vnet); 868 } 869 870 struct nhop_object * 871 nhop_select_func(struct nhop_object *nh, uint32_t flowid) 872 { 873 874 return (nhop_select(nh, flowid)); 875 } 876 877 /* 878 * Returns address family of the traffic uses the nexthop. 879 */ 880 int 881 nhop_get_upper_family(const struct nhop_object *nh) 882 { 883 return (nh->nh_priv->nh_upper_family); 884 } 885 886 /* 887 * Returns address family of the LLE or gateway that is used 888 * to forward the traffic to. 889 */ 890 int 891 nhop_get_neigh_family(const struct nhop_object *nh) 892 { 893 return (nh->nh_priv->nh_neigh_family); 894 } 895 896 uint32_t 897 nhop_get_fibnum(const struct nhop_object *nh) 898 { 899 return (nh->nh_priv->nh_fibnum); 900 } 901 902 void 903 nhop_set_fibnum(struct nhop_object *nh, uint32_t fibnum) 904 { 905 nh->nh_priv->nh_fibnum = fibnum; 906 } 907 908 uint32_t 909 nhop_get_expire(const struct nhop_object *nh) 910 { 911 return (nh->nh_priv->nh_expire); 912 } 913 914 void 915 nhop_set_expire(struct nhop_object *nh, uint32_t expire) 916 { 917 MPASS(!NH_IS_LINKED(nh)); 918 nh->nh_priv->nh_expire = expire; 919 } 920 921 static struct rib_head * 922 nhop_get_rh(const struct nhop_object *nh) 923 { 924 uint32_t fibnum = nhop_get_fibnum(nh); 925 int family = nhop_get_neigh_family(nh); 926 927 return (rt_tables_get_rnh(fibnum, family)); 928 } 929 930 void 931 nhops_update_ifmtu(struct rib_head *rh, struct ifnet *ifp, uint32_t mtu) 932 { 933 struct nh_control *ctl; 934 struct nhop_priv *nh_priv; 935 struct nhop_object *nh; 936 937 ctl = rh->nh_control; 938 939 NHOPS_WLOCK(ctl); 940 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) { 941 nh = nh_priv->nh; 942 if (nh->nh_ifp == ifp) { 943 if ((nh_priv->rt_flags & RTF_FIXEDMTU) == 0 || 944 nh->nh_mtu > mtu) { 945 /* Update MTU directly */ 946 nh->nh_mtu = mtu; 947 } 948 } 949 } CHT_SLIST_FOREACH_END; 950 NHOPS_WUNLOCK(ctl); 951 952 } 953 954 /* 955 * Prints nexthop @nh data in the provided @buf. 956 * Example: nh#33/inet/em0/192.168.0.1 957 */ 958 char * 959 nhop_print_buf(const struct nhop_object *nh, char *buf, size_t bufsize) 960 { 961 #if defined(INET) || defined(INET6) 962 char abuf[INET6_ADDRSTRLEN]; 963 #endif 964 struct nhop_priv *nh_priv = nh->nh_priv; 965 const char *upper_str = rib_print_family(nh->nh_priv->nh_upper_family); 966 967 switch (nh->gw_sa.sa_family) { 968 #ifdef INET 969 case AF_INET: 970 inet_ntop(AF_INET, &nh->gw4_sa.sin_addr, abuf, sizeof(abuf)); 971 snprintf(buf, bufsize, "nh#%d/%s/%s/%s", nh_priv->nh_idx, upper_str, 972 if_name(nh->nh_ifp), abuf); 973 break; 974 #endif 975 #ifdef INET6 976 case AF_INET6: 977 inet_ntop(AF_INET6, &nh->gw6_sa.sin6_addr, abuf, sizeof(abuf)); 978 snprintf(buf, bufsize, "nh#%d/%s/%s/%s", nh_priv->nh_idx, upper_str, 979 if_name(nh->nh_ifp), abuf); 980 break; 981 #endif 982 case AF_LINK: 983 snprintf(buf, bufsize, "nh#%d/%s/%s/resolve", nh_priv->nh_idx, upper_str, 984 if_name(nh->nh_ifp)); 985 break; 986 default: 987 snprintf(buf, bufsize, "nh#%d/%s/%s/????", nh_priv->nh_idx, upper_str, 988 if_name(nh->nh_ifp)); 989 break; 990 } 991 992 return (buf); 993 } 994 995 char * 996 nhop_print_buf_any(const struct nhop_object *nh, char *buf, size_t bufsize) 997 { 998 #ifdef ROUTE_MPATH 999 if (NH_IS_NHGRP(nh)) 1000 return (nhgrp_print_buf((const struct nhgrp_object *)nh, buf, bufsize)); 1001 else 1002 #endif 1003 return (nhop_print_buf(nh, buf, bufsize)); 1004 } 1005 1006 /* 1007 * Dumps a single entry to sysctl buffer. 1008 * 1009 * Layout: 1010 * rt_msghdr - generic RTM header to allow users to skip non-understood messages 1011 * nhop_external - nexhop description structure (with length) 1012 * nhop_addrs - structure encapsulating GW/SRC sockaddrs 1013 */ 1014 static int 1015 dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w) 1016 { 1017 struct { 1018 struct rt_msghdr rtm; 1019 struct nhop_external nhe; 1020 struct nhop_addrs na; 1021 } arpc; 1022 struct nhop_external *pnhe; 1023 struct sockaddr *gw_sa, *src_sa; 1024 struct sockaddr_storage ss; 1025 size_t addrs_len; 1026 int error; 1027 1028 memset(&arpc, 0, sizeof(arpc)); 1029 1030 arpc.rtm.rtm_msglen = sizeof(arpc); 1031 arpc.rtm.rtm_version = RTM_VERSION; 1032 arpc.rtm.rtm_type = RTM_GET; 1033 //arpc.rtm.rtm_flags = RTF_UP; 1034 arpc.rtm.rtm_flags = nh->nh_priv->rt_flags; 1035 1036 /* nhop_external */ 1037 pnhe = &arpc.nhe; 1038 pnhe->nh_len = sizeof(struct nhop_external); 1039 pnhe->nh_idx = nh->nh_priv->nh_idx; 1040 pnhe->nh_fib = rh->rib_fibnum; 1041 pnhe->ifindex = nh->nh_ifp->if_index; 1042 pnhe->aifindex = nh->nh_aifp->if_index; 1043 pnhe->nh_family = nh->nh_priv->nh_upper_family; 1044 pnhe->nh_type = nh->nh_priv->nh_type; 1045 pnhe->nh_mtu = nh->nh_mtu; 1046 pnhe->nh_flags = nh->nh_flags; 1047 1048 memcpy(pnhe->nh_prepend, nh->nh_prepend, sizeof(nh->nh_prepend)); 1049 pnhe->prepend_len = nh->nh_prepend_len; 1050 pnhe->nh_refcount = nh->nh_priv->nh_refcnt; 1051 pnhe->nh_pksent = counter_u64_fetch(nh->nh_pksent); 1052 1053 /* sockaddr container */ 1054 addrs_len = sizeof(struct nhop_addrs); 1055 arpc.na.gw_sa_off = addrs_len; 1056 gw_sa = (struct sockaddr *)&nh->gw4_sa; 1057 addrs_len += gw_sa->sa_len; 1058 1059 src_sa = nh->nh_ifa->ifa_addr; 1060 if (src_sa->sa_family == AF_LINK) { 1061 /* Shorten structure */ 1062 memset(&ss, 0, sizeof(struct sockaddr_storage)); 1063 fill_sdl_from_ifp((struct sockaddr_dl_short *)&ss, 1064 nh->nh_ifa->ifa_ifp); 1065 src_sa = (struct sockaddr *)&ss; 1066 } 1067 arpc.na.src_sa_off = addrs_len; 1068 addrs_len += src_sa->sa_len; 1069 1070 /* Write total container length */ 1071 arpc.na.na_len = addrs_len; 1072 1073 arpc.rtm.rtm_msglen += arpc.na.na_len - sizeof(struct nhop_addrs); 1074 1075 error = SYSCTL_OUT(w, &arpc, sizeof(arpc)); 1076 if (error == 0) 1077 error = SYSCTL_OUT(w, gw_sa, gw_sa->sa_len); 1078 if (error == 0) 1079 error = SYSCTL_OUT(w, src_sa, src_sa->sa_len); 1080 1081 return (error); 1082 } 1083 1084 uint32_t 1085 nhops_get_count(struct rib_head *rh) 1086 { 1087 struct nh_control *ctl; 1088 uint32_t count; 1089 1090 ctl = rh->nh_control; 1091 1092 NHOPS_RLOCK(ctl); 1093 count = ctl->nh_head.items_count; 1094 NHOPS_RUNLOCK(ctl); 1095 1096 return (count); 1097 } 1098 1099 int 1100 nhops_dump_sysctl(struct rib_head *rh, struct sysctl_req *w) 1101 { 1102 struct nh_control *ctl; 1103 struct nhop_priv *nh_priv; 1104 int error; 1105 1106 ctl = rh->nh_control; 1107 1108 NHOPS_RLOCK(ctl); 1109 #if DEBUG_MAX_LEVEL >= LOG_DEBUG 1110 FIB_LOG(LOG_DEBUG, rh->rib_fibnum, rh->rib_family, "dump %u items", 1111 ctl->nh_head.items_count); 1112 #endif 1113 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) { 1114 error = dump_nhop_entry(rh, nh_priv->nh, w); 1115 if (error != 0) { 1116 NHOPS_RUNLOCK(ctl); 1117 return (error); 1118 } 1119 } CHT_SLIST_FOREACH_END; 1120 NHOPS_RUNLOCK(ctl); 1121 1122 return (0); 1123 } 1124