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_route.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/lock.h> 36 #include <sys/rwlock.h> 37 #include <sys/malloc.h> 38 #include <sys/socket.h> 39 #include <sys/sysctl.h> 40 #include <sys/kernel.h> 41 #include <sys/epoch.h> 42 43 #include <net/if.h> 44 #include <net/if_var.h> 45 #include <net/if_dl.h> 46 #include <net/route.h> 47 #include <net/route/route_ctl.h> 48 #include <net/route/route_var.h> 49 #include <net/route/nhop_utils.h> 50 #include <net/route/nhop.h> 51 #include <net/route/nhop_var.h> 52 #include <net/vnet.h> 53 54 /* 55 * This file contains core functionality for the nexthop ("nhop") route subsystem. 56 * The business logic needed to create nexhop objects is implemented here. 57 * 58 * Nexthops in the original sense are the objects containing all the necessary 59 * information to forward the packet to the selected destination. 60 * In particular, nexthop is defined by a combination of 61 * ifp, ifa, aifp, mtu, gw addr(if set), nh_type, nh_family, mask of rt_flags and 62 * NHF_DEFAULT 63 * 64 * Additionally, each nexthop gets assigned its unique index (nexthop index). 65 * It serves two purposes: first one is to ease the ability of userland programs to 66 * reference nexthops by their index. The second one allows lookup algorithms to 67 * to store index instead of pointer (2 bytes vs 8) as a lookup result. 68 * All nexthops are stored in the resizable hash table. 69 * 70 * Basically, this file revolves around supporting 3 functions: 71 * 1) nhop_create_from_info / nhop_create_from_nhop, which contains all 72 * business logic on filling the nexthop fields based on the provided request. 73 * 2) nhop_get(), which gets a usable referenced nexthops. 74 * 75 * Conventions: 76 * 1) non-exported functions start with verb 77 * 2) exported function starts with the subsystem prefix: "nhop" 78 */ 79 80 static int dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w); 81 82 static struct nhop_priv *alloc_nhop_structure(void); 83 static int get_nhop(struct rib_head *rnh, struct rt_addrinfo *info, 84 struct nhop_priv **pnh_priv); 85 static int finalize_nhop(struct nh_control *ctl, struct rt_addrinfo *info, 86 struct nhop_priv *nh_priv); 87 static struct ifnet *get_aifp(const struct nhop_object *nh, int reference); 88 static void fill_sdl_from_ifp(struct sockaddr_dl_short *sdl, const struct ifnet *ifp); 89 90 static void destroy_nhop_epoch(epoch_context_t ctx); 91 static void destroy_nhop(struct nhop_priv *nh_priv); 92 93 static void print_nhop(const char *prefix, const struct nhop_object *nh); 94 95 _Static_assert(__offsetof(struct nhop_object, nh_ifp) == 32, 96 "nhop_object: wrong nh_ifp offset"); 97 _Static_assert(sizeof(struct nhop_object) <= 128, 98 "nhop_object: size exceeds 128 bytes"); 99 100 static uma_zone_t nhops_zone; /* Global zone for each and every nexthop */ 101 102 #define NHOP_OBJECT_ALIGNED_SIZE roundup2(sizeof(struct nhop_object), \ 103 2 * CACHE_LINE_SIZE) 104 #define NHOP_PRIV_ALIGNED_SIZE roundup2(sizeof(struct nhop_priv), \ 105 2 * CACHE_LINE_SIZE) 106 void 107 nhops_init(void) 108 { 109 110 nhops_zone = uma_zcreate("routing nhops", 111 NHOP_OBJECT_ALIGNED_SIZE + NHOP_PRIV_ALIGNED_SIZE, 112 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 113 } 114 115 /* 116 * Fetches the interface of source address used by the route. 117 * In all cases except interface-address-route it would be the 118 * same as the transmit interfaces. 119 * However, for the interface address this function will return 120 * this interface ifp instead of loopback. This is needed to support 121 * link-local IPv6 loopback communications. 122 * 123 * If @reference is non-zero, found ifp is referenced. 124 * 125 * Returns found ifp. 126 */ 127 static struct ifnet * 128 get_aifp(const struct nhop_object *nh, int reference) 129 { 130 struct ifnet *aifp = NULL; 131 132 /* 133 * Adjust the "outgoing" interface. If we're going to loop 134 * the packet back to ourselves, the ifp would be the loopback 135 * interface. However, we'd rather know the interface associated 136 * to the destination address (which should probably be one of 137 * our own addresses). 138 */ 139 if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) && 140 nh->gw_sa.sa_family == AF_LINK) { 141 if (reference) 142 aifp = ifnet_byindex_ref(nh->gwl_sa.sdl_index); 143 else 144 aifp = ifnet_byindex(nh->gwl_sa.sdl_index); 145 if (aifp == NULL) { 146 DPRINTF("unable to get aifp for %s index %d", 147 if_name(nh->nh_ifp), nh->gwl_sa.sdl_index); 148 } 149 } 150 151 if (aifp == NULL) { 152 aifp = nh->nh_ifp; 153 if (reference) 154 if_ref(aifp); 155 } 156 157 return (aifp); 158 } 159 160 int 161 cmp_priv(const struct nhop_priv *_one, const struct nhop_priv *_two) 162 { 163 164 if (memcmp(_one->nh, _two->nh, NHOP_END_CMP) != 0) 165 return (0); 166 167 if (memcmp(_one, _two, NH_PRIV_END_CMP) != 0) 168 return (0); 169 170 return (1); 171 } 172 173 /* 174 * Conditionally sets @nh mtu data based on the @info data. 175 */ 176 static void 177 set_nhop_mtu_from_info(struct nhop_object *nh, const struct rt_addrinfo *info) 178 { 179 180 if (info->rti_mflags & RTV_MTU) { 181 if (info->rti_rmx->rmx_mtu != 0) { 182 /* 183 * MTU was explicitly provided by user. 184 * Keep it. 185 */ 186 187 nh->nh_priv->rt_flags |= RTF_FIXEDMTU; 188 } else { 189 /* 190 * User explicitly sets MTU to 0. 191 * Assume rollback to default. 192 */ 193 nh->nh_priv->rt_flags &= ~RTF_FIXEDMTU; 194 } 195 nh->nh_mtu = info->rti_rmx->rmx_mtu; 196 } 197 } 198 199 /* 200 * Fills in shorted link-level sockadd version suitable to be stored inside the 201 * nexthop gateway buffer. 202 */ 203 static void 204 fill_sdl_from_ifp(struct sockaddr_dl_short *sdl, const struct ifnet *ifp) 205 { 206 207 bzero(sdl, sizeof(struct sockaddr_dl_short)); 208 sdl->sdl_family = AF_LINK; 209 sdl->sdl_len = sizeof(struct sockaddr_dl_short); 210 sdl->sdl_index = ifp->if_index; 211 sdl->sdl_type = ifp->if_type; 212 } 213 214 static int 215 set_nhop_gw_from_info(struct nhop_object *nh, struct rt_addrinfo *info) 216 { 217 struct sockaddr *gw; 218 219 gw = info->rti_info[RTAX_GATEWAY]; 220 KASSERT(gw != NULL, ("gw is NULL")); 221 222 if (info->rti_flags & RTF_GATEWAY) { 223 if (gw->sa_len > sizeof(struct sockaddr_in6)) { 224 DPRINTF("nhop SA size too big: AF %d len %u", 225 gw->sa_family, gw->sa_len); 226 return (ENOMEM); 227 } 228 memcpy(&nh->gw_sa, gw, gw->sa_len); 229 } else { 230 231 /* 232 * Interface route. Currently the route.c code adds 233 * sa of type AF_LINK, which is 56 bytes long. The only 234 * meaningful data there is the interface index. It is used 235 * used is the IPv6 loopback output, where we need to preserve 236 * the original interface to maintain proper scoping. 237 * Despite the fact that nexthop code stores original interface 238 * in the separate field (nh_aifp, see below), write AF_LINK 239 * compatible sa with shorter total length. 240 */ 241 struct sockaddr_dl *sdl; 242 struct ifnet *ifp; 243 244 /* Fetch and validate interface index */ 245 sdl = (struct sockaddr_dl *)gw; 246 if (sdl->sdl_family != AF_LINK) { 247 DPRINTF("unsupported AF: %d", sdl->sdl_family); 248 return (ENOTSUP); 249 } 250 ifp = ifnet_byindex(sdl->sdl_index); 251 if (ifp == NULL) { 252 DPRINTF("invalid ifindex %d", sdl->sdl_index); 253 return (EINVAL); 254 } 255 fill_sdl_from_ifp(&nh->gwl_sa, ifp); 256 } 257 258 return (0); 259 } 260 261 static uint16_t 262 convert_rt_to_nh_flags(int rt_flags) 263 { 264 uint16_t res; 265 266 res = (rt_flags & RTF_REJECT) ? NHF_REJECT : 0; 267 res |= (rt_flags & RTF_HOST) ? NHF_HOST : 0; 268 res |= (rt_flags & RTF_BLACKHOLE) ? NHF_BLACKHOLE : 0; 269 res |= (rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) ? NHF_REDIRECT : 0; 270 res |= (rt_flags & RTF_BROADCAST) ? NHF_BROADCAST : 0; 271 res |= (rt_flags & RTF_GATEWAY) ? NHF_GATEWAY : 0; 272 273 return (res); 274 } 275 276 static int 277 fill_nhop_from_info(struct nhop_priv *nh_priv, struct rt_addrinfo *info) 278 { 279 int error, rt_flags; 280 struct nhop_object *nh; 281 282 nh = nh_priv->nh; 283 284 rt_flags = info->rti_flags & NHOP_RT_FLAG_MASK; 285 286 nh->nh_priv->rt_flags = rt_flags; 287 nh_priv->nh_family = info->rti_info[RTAX_DST]->sa_family; 288 nh_priv->nh_type = 0; // hook responsibility to set nhop type 289 290 nh->nh_flags = convert_rt_to_nh_flags(rt_flags); 291 set_nhop_mtu_from_info(nh, info); 292 if ((error = set_nhop_gw_from_info(nh, info)) != 0) 293 return (error); 294 295 nh->nh_ifp = info->rti_ifa->ifa_ifp; 296 nh->nh_ifa = info->rti_ifa; 297 /* depends on the gateway */ 298 nh->nh_aifp = get_aifp(nh, 0); 299 300 /* 301 * Note some of the remaining data is set by the 302 * per-address-family pre-add hook. 303 */ 304 305 return (0); 306 } 307 308 /* 309 * Creates a new nexthop based on the information in @info. 310 * 311 * Returns: 312 * 0 on success, filling @nh_ret with the desired nexthop object ptr 313 * errno otherwise 314 */ 315 int 316 nhop_create_from_info(struct rib_head *rnh, struct rt_addrinfo *info, 317 struct nhop_object **nh_ret) 318 { 319 struct nhop_priv *nh_priv; 320 int error; 321 322 NET_EPOCH_ASSERT(); 323 324 if (info->rti_info[RTAX_GATEWAY] == NULL) 325 return (EINVAL); 326 327 nh_priv = alloc_nhop_structure(); 328 329 error = fill_nhop_from_info(nh_priv, info); 330 if (error != 0) { 331 uma_zfree(nhops_zone, nh_priv->nh); 332 return (error); 333 } 334 335 error = get_nhop(rnh, info, &nh_priv); 336 if (error == 0) 337 *nh_ret = nh_priv->nh; 338 339 return (error); 340 } 341 342 /* 343 * Gets linked nhop using the provided @pnh_priv nexhop data. 344 * If linked nhop is found, returns it, freeing the provided one. 345 * If there is no such nexthop, attaches the remaining data to the 346 * provided nexthop and links it. 347 * 348 * Returns 0 on success, storing referenced nexthop in @pnh_priv. 349 * Otherwise, errno is returned. 350 */ 351 static int 352 get_nhop(struct rib_head *rnh, struct rt_addrinfo *info, 353 struct nhop_priv **pnh_priv) 354 { 355 const struct sockaddr *dst, *gateway, *netmask; 356 struct nhop_priv *nh_priv, *tmp_priv; 357 int error; 358 359 nh_priv = *pnh_priv; 360 361 /* Give the protocols chance to augment the request data */ 362 dst = info->rti_info[RTAX_DST]; 363 netmask = info->rti_info[RTAX_NETMASK]; 364 gateway = info->rti_info[RTAX_GATEWAY]; 365 366 error = rnh->rnh_preadd(rnh->rib_fibnum, dst, netmask, nh_priv->nh); 367 if (error != 0) { 368 uma_zfree(nhops_zone, nh_priv->nh); 369 return (error); 370 } 371 372 tmp_priv = find_nhop(rnh->nh_control, nh_priv); 373 if (tmp_priv != NULL) { 374 uma_zfree(nhops_zone, nh_priv->nh); 375 *pnh_priv = tmp_priv; 376 return (0); 377 } 378 379 /* 380 * Existing nexthop not found, need to create new one. 381 * Note: multiple simultaneous get_nhop() requests 382 * can result in multiple equal nexhops existing in the 383 * nexthop table. This is not a not a problem until the 384 * relative number of such nexthops is significant, which 385 * is extremely unlikely. 386 */ 387 388 error = finalize_nhop(rnh->nh_control, info, nh_priv); 389 if (error != 0) 390 return (error); 391 392 return (0); 393 } 394 395 /* 396 * Update @nh with data supplied in @info. 397 * This is a helper function to support route changes. 398 * 399 * It limits the changes that can be done to the route to the following: 400 * 1) all combination of gateway changes (gw, interface, blackhole/reject) 401 * 2) route flags (FLAG[123],STATIC,BLACKHOLE,REJECT) 402 * 3) route MTU 403 * 404 * Returns: 405 * 0 on success 406 */ 407 static int 408 alter_nhop_from_info(struct nhop_object *nh, struct rt_addrinfo *info) 409 { 410 struct sockaddr *info_gw; 411 int error; 412 413 /* Update MTU if set in the request*/ 414 set_nhop_mtu_from_info(nh, info); 415 416 /* XXX: allow only one of BLACKHOLE,REJECT,GATEWAY */ 417 418 /* Allow some flags (FLAG1,STATIC,BLACKHOLE,REJECT) to be toggled on change. */ 419 nh->nh_priv->rt_flags &= ~RTF_FMASK; 420 nh->nh_priv->rt_flags |= info->rti_flags & RTF_FMASK; 421 422 /* Consider gateway change */ 423 info_gw = info->rti_info[RTAX_GATEWAY]; 424 if (info_gw != NULL) { 425 error = set_nhop_gw_from_info(nh, info); 426 if (error != 0) 427 return (error); 428 /* Update RTF_GATEWAY flag status */ 429 nh->nh_priv->rt_flags &= ~RTF_GATEWAY; 430 nh->nh_priv->rt_flags |= (RTF_GATEWAY & info->rti_flags); 431 } 432 /* Update datapath flags */ 433 nh->nh_flags = convert_rt_to_nh_flags(nh->nh_priv->rt_flags); 434 435 if (info->rti_ifa != NULL) 436 nh->nh_ifa = info->rti_ifa; 437 if (info->rti_ifp != NULL) 438 nh->nh_ifp = info->rti_ifp; 439 nh->nh_aifp = get_aifp(nh, 0); 440 441 return (0); 442 } 443 444 /* 445 * Creates new nexthop based on @nh_orig and augmentation data from @info. 446 * Helper function used in the route changes, please see 447 * alter_nhop_from_info() comments for more details. 448 * 449 * Returns: 450 * 0 on success, filling @nh_ret with the desired nexthop object 451 * errno otherwise 452 */ 453 int 454 nhop_create_from_nhop(struct rib_head *rnh, const struct nhop_object *nh_orig, 455 struct rt_addrinfo *info, struct nhop_object **pnh) 456 { 457 struct nhop_priv *nh_priv; 458 struct nhop_object *nh; 459 int error; 460 461 NET_EPOCH_ASSERT(); 462 463 nh_priv = alloc_nhop_structure(); 464 nh = nh_priv->nh; 465 466 /* Start with copying data from original nexthop */ 467 nh_priv->nh_family = nh_orig->nh_priv->nh_family; 468 nh_priv->rt_flags = nh_orig->nh_priv->rt_flags; 469 nh_priv->nh_type = nh_orig->nh_priv->nh_type; 470 471 nh->nh_ifp = nh_orig->nh_ifp; 472 nh->nh_ifa = nh_orig->nh_ifa; 473 nh->nh_aifp = nh_orig->nh_aifp; 474 nh->nh_mtu = nh_orig->nh_mtu; 475 nh->nh_flags = nh_orig->nh_flags; 476 memcpy(&nh->gw_sa, &nh_orig->gw_sa, nh_orig->gw_sa.sa_len); 477 478 error = alter_nhop_from_info(nh, info); 479 if (error != 0) { 480 uma_zfree(nhops_zone, nh_priv->nh); 481 return (error); 482 } 483 484 error = get_nhop(rnh, info, &nh_priv); 485 if (error == 0) 486 *pnh = nh_priv->nh; 487 488 return (error); 489 } 490 491 /* 492 * Allocates memory for public/private nexthop structures. 493 * 494 * Returns pointer to nhop_priv or NULL. 495 */ 496 static struct nhop_priv * 497 alloc_nhop_structure() 498 { 499 struct nhop_object *nh; 500 struct nhop_priv *nh_priv; 501 502 nh = (struct nhop_object *)uma_zalloc(nhops_zone, M_NOWAIT | M_ZERO); 503 if (nh == NULL) 504 return (NULL); 505 nh_priv = (struct nhop_priv *)((char *)nh + NHOP_OBJECT_ALIGNED_SIZE); 506 507 nh->nh_priv = nh_priv; 508 nh_priv->nh = nh; 509 510 return (nh_priv); 511 } 512 513 /* 514 * Alocates/references the remaining bits of nexthop data and links 515 * it to the hash table. 516 * Returns 0 if successful, 517 * errno otherwise. @nh_priv is freed in case of error. 518 */ 519 static int 520 finalize_nhop(struct nh_control *ctl, struct rt_addrinfo *info, 521 struct nhop_priv *nh_priv) 522 { 523 struct nhop_object *nh; 524 525 nh = nh_priv->nh; 526 527 /* Allocate per-cpu packet counter */ 528 nh->nh_pksent = counter_u64_alloc(M_NOWAIT); 529 if (nh->nh_pksent == NULL) { 530 uma_zfree(nhops_zone, nh); 531 RTSTAT_INC(rts_nh_alloc_failure); 532 DPRINTF("nh_alloc_finalize failed"); 533 return (ENOMEM); 534 } 535 536 /* Save vnet to ease destruction */ 537 nh_priv->nh_vnet = curvnet; 538 539 /* Reference external objects and calculate (referenced) ifa */ 540 if_ref(nh->nh_ifp); 541 ifa_ref(nh->nh_ifa); 542 nh->nh_aifp = get_aifp(nh, 1); 543 DPRINTF("AIFP: %p nh_ifp %p", nh->nh_aifp, nh->nh_ifp); 544 545 refcount_init(&nh_priv->nh_refcnt, 1); 546 547 /* Please see nhop_free() comments on the initial value */ 548 refcount_init(&nh_priv->nh_linked, 2); 549 550 print_nhop("FINALIZE", nh); 551 552 if (link_nhop(ctl, nh_priv) == 0) { 553 /* 554 * Adding nexthop to the datastructures 555 * failed. Call destructor w/o waiting for 556 * the epoch end, as nexthop is not used 557 * and return. 558 */ 559 DPRINTF("link_nhop failed!"); 560 destroy_nhop(nh_priv); 561 562 return (ENOBUFS); 563 } 564 565 return (0); 566 } 567 568 static void 569 print_nhop_sa(char *buf, size_t buflen, const struct sockaddr *sa) 570 { 571 572 if (sa->sa_family == AF_INET) { 573 const struct sockaddr_in *sin4; 574 sin4 = (const struct sockaddr_in *)sa; 575 inet_ntop(AF_INET, &sin4->sin_addr, buf, buflen); 576 } else if (sa->sa_family == AF_INET6) { 577 const struct sockaddr_in6 *sin6; 578 sin6 = (const struct sockaddr_in6 *)sa; 579 inet_ntop(AF_INET6, &sin6->sin6_addr, buf, buflen); 580 } else if (sa->sa_family == AF_LINK) { 581 const struct sockaddr_dl *sdl; 582 sdl = (const struct sockaddr_dl *)sa; 583 snprintf(buf, buflen, "if#%d", sdl->sdl_index); 584 } else 585 snprintf(buf, buflen, "af:%d", sa->sa_family); 586 } 587 588 static void 589 print_nhop(const char *prefix, const struct nhop_object *nh) 590 { 591 char src_buf[INET6_ADDRSTRLEN], addr_buf[INET6_ADDRSTRLEN]; 592 593 print_nhop_sa(src_buf, sizeof(src_buf), nh->nh_ifa->ifa_addr); 594 print_nhop_sa(addr_buf, sizeof(addr_buf), &nh->gw_sa); 595 596 DPRINTF("%s nhop priv %p: AF %d ifp %p %s addr %s src %p %s aifp %p %s mtu %d nh_flags %X", 597 prefix, nh->nh_priv, nh->nh_priv->nh_family, nh->nh_ifp, 598 if_name(nh->nh_ifp), addr_buf, nh->nh_ifa, src_buf, nh->nh_aifp, 599 if_name(nh->nh_aifp), nh->nh_mtu, nh->nh_flags); 600 } 601 602 static void 603 destroy_nhop(struct nhop_priv *nh_priv) 604 { 605 struct nhop_object *nh; 606 607 nh = nh_priv->nh; 608 609 print_nhop("DEL", nh); 610 611 if_rele(nh->nh_ifp); 612 if_rele(nh->nh_aifp); 613 ifa_free(nh->nh_ifa); 614 counter_u64_free(nh->nh_pksent); 615 616 uma_zfree(nhops_zone, nh); 617 } 618 619 /* 620 * Epoch callback indicating nhop is safe to destroy 621 */ 622 static void 623 destroy_nhop_epoch(epoch_context_t ctx) 624 { 625 struct nhop_priv *nh_priv; 626 627 nh_priv = __containerof(ctx, struct nhop_priv, nh_epoch_ctx); 628 629 destroy_nhop(nh_priv); 630 } 631 632 void 633 nhop_ref_object(struct nhop_object *nh) 634 { 635 u_int old; 636 637 old = refcount_acquire(&nh->nh_priv->nh_refcnt); 638 KASSERT(old > 0, ("%s: nhop object %p has 0 refs", __func__, nh)); 639 } 640 641 int 642 nhop_try_ref_object(struct nhop_object *nh) 643 { 644 645 return (refcount_acquire_if_not_zero(&nh->nh_priv->nh_refcnt)); 646 } 647 648 void 649 nhop_free(struct nhop_object *nh) 650 { 651 struct nh_control *ctl; 652 struct nhop_priv *nh_priv = nh->nh_priv; 653 struct epoch_tracker et; 654 655 if (!refcount_release(&nh_priv->nh_refcnt)) 656 return; 657 658 /* 659 * There are only 2 places, where nh_linked can be decreased: 660 * rib destroy (nhops_destroy_rib) and this function. 661 * nh_link can never be increased. 662 * 663 * Hence, use initial value of 2 to make use of 664 * refcount_release_if_not_last(). 665 * 666 * There can be two scenarious when calling this function: 667 * 668 * 1) nh_linked value is 2. This means that either 669 * nhops_destroy_rib() has not been called OR it is running, 670 * but we are guaranteed that nh_control won't be freed in 671 * this epoch. Hence, nexthop can be safely unlinked. 672 * 673 * 2) nh_linked value is 1. In that case, nhops_destroy_rib() 674 * has been called and nhop unlink can be skipped. 675 */ 676 677 NET_EPOCH_ENTER(et); 678 if (refcount_release_if_not_last(&nh_priv->nh_linked)) { 679 ctl = nh_priv->nh_control; 680 if (unlink_nhop(ctl, nh_priv) == NULL) { 681 /* Do not try to reclaim */ 682 DPRINTF("Failed to unlink nexhop %p", nh_priv); 683 NET_EPOCH_EXIT(et); 684 return; 685 } 686 } 687 NET_EPOCH_EXIT(et); 688 689 epoch_call(net_epoch_preempt, destroy_nhop_epoch, 690 &nh_priv->nh_epoch_ctx); 691 } 692 693 void 694 nhop_ref_any(struct nhop_object *nh) 695 { 696 #ifdef ROUTE_MPATH 697 if (!NH_IS_NHGRP(nh)) 698 nhop_ref_object(nh); 699 else 700 nhgrp_ref_object((struct nhgrp_object *)nh); 701 #else 702 nhop_ref_object(nh); 703 #endif 704 } 705 706 void 707 nhop_free_any(struct nhop_object *nh) 708 { 709 710 #ifdef ROUTE_MPATH 711 if (!NH_IS_NHGRP(nh)) 712 nhop_free(nh); 713 else 714 nhgrp_free((struct nhgrp_object *)nh); 715 #else 716 nhop_free(nh); 717 #endif 718 } 719 720 /* Helper functions */ 721 722 uint32_t 723 nhop_get_idx(const struct nhop_object *nh) 724 { 725 726 return (nh->nh_priv->nh_idx); 727 } 728 729 enum nhop_type 730 nhop_get_type(const struct nhop_object *nh) 731 { 732 733 return (nh->nh_priv->nh_type); 734 } 735 736 void 737 nhop_set_type(struct nhop_object *nh, enum nhop_type nh_type) 738 { 739 740 nh->nh_priv->nh_type = nh_type; 741 } 742 743 int 744 nhop_get_rtflags(const struct nhop_object *nh) 745 { 746 747 return (nh->nh_priv->rt_flags); 748 } 749 750 void 751 nhop_set_rtflags(struct nhop_object *nh, int rt_flags) 752 { 753 754 nh->nh_priv->rt_flags = rt_flags; 755 } 756 757 struct vnet * 758 nhop_get_vnet(const struct nhop_object *nh) 759 { 760 761 return (nh->nh_priv->nh_vnet); 762 } 763 764 struct nhop_object * 765 nhop_select_func(struct nhop_object *nh, uint32_t flowid) 766 { 767 768 return (nhop_select(nh, flowid)); 769 } 770 771 void 772 nhops_update_ifmtu(struct rib_head *rh, struct ifnet *ifp, uint32_t mtu) 773 { 774 struct nh_control *ctl; 775 struct nhop_priv *nh_priv; 776 struct nhop_object *nh; 777 778 ctl = rh->nh_control; 779 780 NHOPS_WLOCK(ctl); 781 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) { 782 nh = nh_priv->nh; 783 if (nh->nh_ifp == ifp) { 784 if ((nh_priv->rt_flags & RTF_FIXEDMTU) == 0 || 785 nh->nh_mtu > mtu) { 786 /* Update MTU directly */ 787 nh->nh_mtu = mtu; 788 } 789 } 790 } CHT_SLIST_FOREACH_END; 791 NHOPS_WUNLOCK(ctl); 792 793 } 794 795 /* 796 * Dumps a single entry to sysctl buffer. 797 * 798 * Layout: 799 * rt_msghdr - generic RTM header to allow users to skip non-understood messages 800 * nhop_external - nexhop description structure (with length) 801 * nhop_addrs - structure encapsulating GW/SRC sockaddrs 802 */ 803 static int 804 dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w) 805 { 806 struct { 807 struct rt_msghdr rtm; 808 struct nhop_external nhe; 809 struct nhop_addrs na; 810 } arpc; 811 struct nhop_external *pnhe; 812 struct sockaddr *gw_sa, *src_sa; 813 struct sockaddr_storage ss; 814 size_t addrs_len; 815 int error; 816 817 //DPRINTF("Dumping: head %p nh %p flags %X req %p\n", rh, nh, nh->nh_flags, w); 818 819 memset(&arpc, 0, sizeof(arpc)); 820 821 arpc.rtm.rtm_msglen = sizeof(arpc); 822 arpc.rtm.rtm_version = RTM_VERSION; 823 arpc.rtm.rtm_type = RTM_GET; 824 //arpc.rtm.rtm_flags = RTF_UP; 825 arpc.rtm.rtm_flags = nh->nh_priv->rt_flags; 826 827 /* nhop_external */ 828 pnhe = &arpc.nhe; 829 pnhe->nh_len = sizeof(struct nhop_external); 830 pnhe->nh_idx = nh->nh_priv->nh_idx; 831 pnhe->nh_fib = rh->rib_fibnum; 832 pnhe->ifindex = nh->nh_ifp->if_index; 833 pnhe->aifindex = nh->nh_aifp->if_index; 834 pnhe->nh_family = nh->nh_priv->nh_family; 835 pnhe->nh_type = nh->nh_priv->nh_type; 836 pnhe->nh_mtu = nh->nh_mtu; 837 pnhe->nh_flags = nh->nh_flags; 838 839 memcpy(pnhe->nh_prepend, nh->nh_prepend, sizeof(nh->nh_prepend)); 840 pnhe->prepend_len = nh->nh_prepend_len; 841 pnhe->nh_refcount = nh->nh_priv->nh_refcnt; 842 pnhe->nh_pksent = counter_u64_fetch(nh->nh_pksent); 843 844 /* sockaddr container */ 845 addrs_len = sizeof(struct nhop_addrs); 846 arpc.na.gw_sa_off = addrs_len; 847 gw_sa = (struct sockaddr *)&nh->gw4_sa; 848 addrs_len += gw_sa->sa_len; 849 850 src_sa = nh->nh_ifa->ifa_addr; 851 if (src_sa->sa_family == AF_LINK) { 852 /* Shorten structure */ 853 memset(&ss, 0, sizeof(struct sockaddr_storage)); 854 fill_sdl_from_ifp((struct sockaddr_dl_short *)&ss, 855 nh->nh_ifa->ifa_ifp); 856 src_sa = (struct sockaddr *)&ss; 857 } 858 arpc.na.src_sa_off = addrs_len; 859 addrs_len += src_sa->sa_len; 860 861 /* Write total container length */ 862 arpc.na.na_len = addrs_len; 863 864 arpc.rtm.rtm_msglen += arpc.na.na_len - sizeof(struct nhop_addrs); 865 866 error = SYSCTL_OUT(w, &arpc, sizeof(arpc)); 867 if (error == 0) 868 error = SYSCTL_OUT(w, gw_sa, gw_sa->sa_len); 869 if (error == 0) 870 error = SYSCTL_OUT(w, src_sa, src_sa->sa_len); 871 872 return (error); 873 } 874 875 uint32_t 876 nhops_get_count(struct rib_head *rh) 877 { 878 struct nh_control *ctl; 879 uint32_t count; 880 881 ctl = rh->nh_control; 882 883 NHOPS_RLOCK(ctl); 884 count = ctl->nh_head.items_count; 885 NHOPS_RUNLOCK(ctl); 886 887 return (count); 888 } 889 890 int 891 nhops_dump_sysctl(struct rib_head *rh, struct sysctl_req *w) 892 { 893 struct nh_control *ctl; 894 struct nhop_priv *nh_priv; 895 int error; 896 897 ctl = rh->nh_control; 898 899 NHOPS_RLOCK(ctl); 900 DPRINTF("NHDUMP: count=%u", ctl->nh_head.items_count); 901 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) { 902 error = dump_nhop_entry(rh, nh_priv->nh, w); 903 if (error != 0) { 904 NHOPS_RUNLOCK(ctl); 905 return (error); 906 } 907 } CHT_SLIST_FOREACH_END; 908 NHOPS_RUNLOCK(ctl); 909 910 return (0); 911 } 912