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 ((_one->nh_type != _two->nh_type) || 168 (_one->nh_family != _two->nh_family)) 169 return (0); 170 171 return (1); 172 } 173 174 /* 175 * Conditionally sets @nh mtu data based on the @info data. 176 */ 177 static void 178 set_nhop_mtu_from_info(struct nhop_object *nh, const struct rt_addrinfo *info) 179 { 180 181 if (info->rti_mflags & RTV_MTU) { 182 if (info->rti_rmx->rmx_mtu != 0) { 183 /* 184 * MTU was explicitly provided by user. 185 * Keep it. 186 */ 187 188 nh->nh_priv->rt_flags |= RTF_FIXEDMTU; 189 } else { 190 /* 191 * User explicitly sets MTU to 0. 192 * Assume rollback to default. 193 */ 194 nh->nh_priv->rt_flags &= ~RTF_FIXEDMTU; 195 } 196 nh->nh_mtu = info->rti_rmx->rmx_mtu; 197 } 198 } 199 200 /* 201 * Fills in shorted link-level sockadd version suitable to be stored inside the 202 * nexthop gateway buffer. 203 */ 204 static void 205 fill_sdl_from_ifp(struct sockaddr_dl_short *sdl, const struct ifnet *ifp) 206 { 207 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 if (info->rti_flags & RTF_GATEWAY) { 221 if (gw->sa_len > sizeof(struct sockaddr_in6)) { 222 DPRINTF("nhop SA size too big: AF %d len %u", 223 gw->sa_family, gw->sa_len); 224 return (ENOMEM); 225 } 226 memcpy(&nh->gw_sa, gw, gw->sa_len); 227 } else { 228 229 /* 230 * Interface route. Currently the route.c code adds 231 * sa of type AF_LINK, which is 56 bytes long. The only 232 * meaningful data there is the interface index. It is used 233 * used is the IPv6 loopback output, where we need to preserve 234 * the original interface to maintain proper scoping. 235 * Despite the fact that nexthop code stores original interface 236 * in the separate field (nh_aifp, see below), write AF_LINK 237 * compatible sa with shorter total length. 238 */ 239 struct sockaddr_dl *sdl; 240 struct ifnet *ifp; 241 242 /* Fetch and validate interface index */ 243 sdl = (struct sockaddr_dl *)gw; 244 if (sdl->sdl_family != AF_LINK) { 245 DPRINTF("unsupported AF: %d", sdl->sdl_family); 246 return (ENOTSUP); 247 } 248 ifp = ifnet_byindex(sdl->sdl_index); 249 if (ifp == NULL) { 250 DPRINTF("invalid ifindex %d", sdl->sdl_index); 251 return (EINVAL); 252 } 253 fill_sdl_from_ifp(&nh->gwl_sa, ifp); 254 } 255 256 return (0); 257 } 258 259 static uint16_t 260 convert_rt_to_nh_flags(int rt_flags) 261 { 262 uint16_t res; 263 264 res = (rt_flags & RTF_REJECT) ? NHF_REJECT : 0; 265 res |= (rt_flags & RTF_HOST) ? NHF_HOST : 0; 266 res |= (rt_flags & RTF_BLACKHOLE) ? NHF_BLACKHOLE : 0; 267 res |= (rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) ? NHF_REDIRECT : 0; 268 res |= (rt_flags & RTF_BROADCAST) ? NHF_BROADCAST : 0; 269 res |= (rt_flags & RTF_GATEWAY) ? NHF_GATEWAY : 0; 270 271 return (res); 272 } 273 274 static int 275 fill_nhop_from_info(struct nhop_priv *nh_priv, struct rt_addrinfo *info) 276 { 277 int error, rt_flags; 278 struct nhop_object *nh; 279 280 nh = nh_priv->nh; 281 282 rt_flags = info->rti_flags & NHOP_RT_FLAG_MASK; 283 284 nh->nh_priv->rt_flags = rt_flags; 285 nh_priv->nh_family = info->rti_info[RTAX_DST]->sa_family; 286 nh_priv->nh_type = 0; // hook responsibility to set nhop type 287 288 nh->nh_flags = convert_rt_to_nh_flags(rt_flags); 289 set_nhop_mtu_from_info(nh, info); 290 if ((error = set_nhop_gw_from_info(nh, info)) != 0) 291 return (error); 292 293 nh->nh_ifp = info->rti_ifa->ifa_ifp; 294 nh->nh_ifa = info->rti_ifa; 295 /* depends on the gateway */ 296 nh->nh_aifp = get_aifp(nh, 0); 297 298 /* 299 * Note some of the remaining data is set by the 300 * per-address-family pre-add hook. 301 */ 302 303 return (0); 304 } 305 306 /* 307 * Creates a new nexthop based on the information in @info. 308 * 309 * Returns: 310 * 0 on success, filling @nh_ret with the desired nexthop object ptr 311 * errno otherwise 312 */ 313 int 314 nhop_create_from_info(struct rib_head *rnh, struct rt_addrinfo *info, 315 struct nhop_object **nh_ret) 316 { 317 struct nhop_priv *nh_priv; 318 int error; 319 320 NET_EPOCH_ASSERT(); 321 322 nh_priv = alloc_nhop_structure(); 323 324 error = fill_nhop_from_info(nh_priv, info); 325 if (error != 0) { 326 uma_zfree(nhops_zone, nh_priv->nh); 327 return (error); 328 } 329 330 error = get_nhop(rnh, info, &nh_priv); 331 if (error == 0) 332 *nh_ret = nh_priv->nh; 333 334 return (error); 335 } 336 337 /* 338 * Gets linked nhop using the provided @pnh_priv nexhop data. 339 * If linked nhop is found, returns it, freeing the provided one. 340 * If there is no such nexthop, attaches the remaining data to the 341 * provided nexthop and links it. 342 * 343 * Returns 0 on success, storing referenced nexthop in @pnh_priv. 344 * Otherwise, errno is returned. 345 */ 346 static int 347 get_nhop(struct rib_head *rnh, struct rt_addrinfo *info, 348 struct nhop_priv **pnh_priv) 349 { 350 const struct sockaddr *dst, *gateway, *netmask; 351 struct nhop_priv *nh_priv, *tmp_priv; 352 int error; 353 354 nh_priv = *pnh_priv; 355 356 /* Give the protocols chance to augment the request data */ 357 dst = info->rti_info[RTAX_DST]; 358 netmask = info->rti_info[RTAX_NETMASK]; 359 gateway = info->rti_info[RTAX_GATEWAY]; 360 361 error = rnh->rnh_preadd(rnh->rib_fibnum, dst, netmask, nh_priv->nh); 362 if (error != 0) { 363 uma_zfree(nhops_zone, nh_priv->nh); 364 return (error); 365 } 366 367 tmp_priv = find_nhop(rnh->nh_control, nh_priv); 368 if (tmp_priv != NULL) { 369 uma_zfree(nhops_zone, nh_priv->nh); 370 *pnh_priv = tmp_priv; 371 return (0); 372 } 373 374 /* 375 * Existing nexthop not found, need to create new one. 376 * Note: multiple simultaneous get_nhop() requests 377 * can result in multiple equal nexhops existing in the 378 * nexthop table. This is not a not a problem until the 379 * relative number of such nexthops is significant, which 380 * is extremely unlikely. 381 */ 382 383 error = finalize_nhop(rnh->nh_control, info, nh_priv); 384 if (error != 0) 385 return (error); 386 387 return (0); 388 } 389 390 /* 391 * Update @nh with data supplied in @info. 392 * This is a helper function to support route changes. 393 * 394 * It limits the changes that can be done to the route to the following: 395 * 1) all combination of gateway changes (gw, interface, blackhole/reject) 396 * 2) route flags (FLAG[123],STATIC,BLACKHOLE,REJECT) 397 * 3) route MTU 398 * 399 * Returns: 400 * 0 on success 401 */ 402 static int 403 alter_nhop_from_info(struct nhop_object *nh, struct rt_addrinfo *info) 404 { 405 struct sockaddr *info_gw; 406 int error; 407 408 /* Update MTU if set in the request*/ 409 set_nhop_mtu_from_info(nh, info); 410 411 /* XXX: allow only one of BLACKHOLE,REJECT,GATEWAY */ 412 413 /* Allow some flags (FLAG1,STATIC,BLACKHOLE,REJECT) to be toggled on change. */ 414 nh->nh_priv->rt_flags &= ~RTF_FMASK; 415 nh->nh_priv->rt_flags |= info->rti_flags & RTF_FMASK; 416 417 /* Consider gateway change */ 418 info_gw = info->rti_info[RTAX_GATEWAY]; 419 if (info_gw != NULL) { 420 error = set_nhop_gw_from_info(nh, info); 421 if (error != 0) 422 return (error); 423 /* Update RTF_GATEWAY flag status */ 424 nh->nh_priv->rt_flags &= ~RTF_GATEWAY; 425 nh->nh_priv->rt_flags |= (RTF_GATEWAY & info->rti_flags); 426 } 427 /* Update datapath flags */ 428 nh->nh_flags = convert_rt_to_nh_flags(nh->nh_priv->rt_flags); 429 430 if (info->rti_ifa != NULL) 431 nh->nh_ifa = info->rti_ifa; 432 if (info->rti_ifp != NULL) 433 nh->nh_ifp = info->rti_ifp; 434 nh->nh_aifp = get_aifp(nh, 0); 435 436 return (0); 437 } 438 439 /* 440 * Creates new nexthop based on @nh_orig and augmentation data from @info. 441 * Helper function used in the route changes, please see 442 * alter_nhop_from_info() comments for more details. 443 * 444 * Returns: 445 * 0 on success, filling @nh_ret with the desired nexthop object 446 * errno otherwise 447 */ 448 int 449 nhop_create_from_nhop(struct rib_head *rnh, const struct nhop_object *nh_orig, 450 struct rt_addrinfo *info, struct nhop_object **pnh) 451 { 452 struct nhop_priv *nh_priv; 453 struct nhop_object *nh; 454 int error; 455 456 NET_EPOCH_ASSERT(); 457 458 nh_priv = alloc_nhop_structure(); 459 nh = nh_priv->nh; 460 461 /* Start with copying data from original nexthop */ 462 nh_priv->nh_family = nh_orig->nh_priv->nh_family; 463 nh_priv->rt_flags = nh_orig->nh_priv->rt_flags; 464 nh_priv->nh_type = nh_orig->nh_priv->nh_type; 465 466 nh->nh_ifp = nh_orig->nh_ifp; 467 nh->nh_ifa = nh_orig->nh_ifa; 468 nh->nh_aifp = nh_orig->nh_aifp; 469 nh->nh_mtu = nh_orig->nh_mtu; 470 nh->nh_flags = nh_orig->nh_flags; 471 memcpy(&nh->gw_sa, &nh_orig->gw_sa, nh_orig->gw_sa.sa_len); 472 473 error = alter_nhop_from_info(nh, info); 474 if (error != 0) { 475 uma_zfree(nhops_zone, nh_priv->nh); 476 return (error); 477 } 478 479 error = get_nhop(rnh, info, &nh_priv); 480 if (error == 0) 481 *pnh = nh_priv->nh; 482 483 return (error); 484 } 485 486 /* 487 * Allocates memory for public/private nexthop structures. 488 * 489 * Returns pointer to nhop_priv or NULL. 490 */ 491 static struct nhop_priv * 492 alloc_nhop_structure() 493 { 494 struct nhop_object *nh; 495 struct nhop_priv *nh_priv; 496 497 nh = (struct nhop_object *)uma_zalloc(nhops_zone, M_NOWAIT | M_ZERO); 498 if (nh == NULL) 499 return (NULL); 500 nh_priv = (struct nhop_priv *)((char *)nh + NHOP_OBJECT_ALIGNED_SIZE); 501 502 nh->nh_priv = nh_priv; 503 nh_priv->nh = nh; 504 505 return (nh_priv); 506 } 507 508 /* 509 * Alocates/references the remaining bits of nexthop data and links 510 * it to the hash table. 511 * Returns 0 if successful, 512 * errno otherwise. @nh_priv is freed in case of error. 513 */ 514 static int 515 finalize_nhop(struct nh_control *ctl, struct rt_addrinfo *info, 516 struct nhop_priv *nh_priv) 517 { 518 struct nhop_object *nh; 519 520 nh = nh_priv->nh; 521 522 /* Allocate per-cpu packet counter */ 523 nh->nh_pksent = counter_u64_alloc(M_NOWAIT); 524 if (nh->nh_pksent == NULL) { 525 uma_zfree(nhops_zone, nh); 526 RTSTAT_INC(rts_nh_alloc_failure); 527 DPRINTF("nh_alloc_finalize failed"); 528 return (ENOMEM); 529 } 530 531 /* Save vnet to ease destruction */ 532 nh_priv->nh_vnet = curvnet; 533 534 /* Reference external objects and calculate (referenced) ifa */ 535 if_ref(nh->nh_ifp); 536 ifa_ref(nh->nh_ifa); 537 nh->nh_aifp = get_aifp(nh, 1); 538 DPRINTF("AIFP: %p nh_ifp %p", nh->nh_aifp, nh->nh_ifp); 539 540 refcount_init(&nh_priv->nh_refcnt, 1); 541 542 /* Please see nhop_free() comments on the initial value */ 543 refcount_init(&nh_priv->nh_linked, 2); 544 545 print_nhop("FINALIZE", nh); 546 547 if (link_nhop(ctl, nh_priv) == 0) { 548 /* 549 * Adding nexthop to the datastructures 550 * failed. Call destructor w/o waiting for 551 * the epoch end, as nexthop is not used 552 * and return. 553 */ 554 DPRINTF("link_nhop failed!"); 555 destroy_nhop(nh_priv); 556 557 return (ENOBUFS); 558 } 559 560 return (0); 561 } 562 563 static void 564 print_nhop_sa(char *buf, size_t buflen, const struct sockaddr *sa) 565 { 566 567 if (sa->sa_family == AF_INET) { 568 const struct sockaddr_in *sin4; 569 sin4 = (const struct sockaddr_in *)sa; 570 inet_ntop(AF_INET, &sin4->sin_addr, buf, buflen); 571 } else if (sa->sa_family == AF_INET6) { 572 const struct sockaddr_in6 *sin6; 573 sin6 = (const struct sockaddr_in6 *)sa; 574 inet_ntop(AF_INET6, &sin6->sin6_addr, buf, buflen); 575 } else if (sa->sa_family == AF_LINK) { 576 const struct sockaddr_dl *sdl; 577 sdl = (const struct sockaddr_dl *)sa; 578 snprintf(buf, buflen, "if#%d", sdl->sdl_index); 579 } else 580 snprintf(buf, buflen, "af:%d", sa->sa_family); 581 } 582 583 static void 584 print_nhop(const char *prefix, const struct nhop_object *nh) 585 { 586 char src_buf[INET6_ADDRSTRLEN], addr_buf[INET6_ADDRSTRLEN]; 587 588 print_nhop_sa(src_buf, sizeof(src_buf), nh->nh_ifa->ifa_addr); 589 print_nhop_sa(addr_buf, sizeof(addr_buf), &nh->gw_sa); 590 591 DPRINTF("%s nhop priv %p: AF %d ifp %p %s addr %s src %p %s aifp %p %s mtu %d nh_flags %X", 592 prefix, nh->nh_priv, nh->nh_priv->nh_family, nh->nh_ifp, 593 if_name(nh->nh_ifp), addr_buf, nh->nh_ifa, src_buf, nh->nh_aifp, 594 if_name(nh->nh_aifp), nh->nh_mtu, nh->nh_flags); 595 } 596 597 static void 598 destroy_nhop(struct nhop_priv *nh_priv) 599 { 600 struct nhop_object *nh; 601 602 nh = nh_priv->nh; 603 604 print_nhop("DEL", nh); 605 606 if_rele(nh->nh_ifp); 607 if_rele(nh->nh_aifp); 608 ifa_free(nh->nh_ifa); 609 counter_u64_free(nh->nh_pksent); 610 611 uma_zfree(nhops_zone, nh); 612 } 613 614 /* 615 * Epoch callback indicating nhop is safe to destroy 616 */ 617 static void 618 destroy_nhop_epoch(epoch_context_t ctx) 619 { 620 struct nhop_priv *nh_priv; 621 622 nh_priv = __containerof(ctx, struct nhop_priv, nh_epoch_ctx); 623 624 destroy_nhop(nh_priv); 625 } 626 627 void 628 nhop_ref_object(struct nhop_object *nh) 629 { 630 u_int old; 631 632 old = refcount_acquire(&nh->nh_priv->nh_refcnt); 633 KASSERT(old > 0, ("%s: nhop object %p has 0 refs", __func__, nh)); 634 } 635 636 int 637 nhop_try_ref_object(struct nhop_object *nh) 638 { 639 640 return (refcount_acquire_if_not_zero(&nh->nh_priv->nh_refcnt)); 641 } 642 643 void 644 nhop_free(struct nhop_object *nh) 645 { 646 struct nh_control *ctl; 647 struct nhop_priv *nh_priv = nh->nh_priv; 648 struct epoch_tracker et; 649 650 if (!refcount_release(&nh_priv->nh_refcnt)) 651 return; 652 653 /* 654 * There are only 2 places, where nh_linked can be decreased: 655 * rib destroy (nhops_destroy_rib) and this function. 656 * nh_link can never be increased. 657 * 658 * Hence, use initial value of 2 to make use of 659 * refcount_release_if_not_last(). 660 * 661 * There can be two scenarious when calling this function: 662 * 663 * 1) nh_linked value is 2. This means that either 664 * nhops_destroy_rib() has not been called OR it is running, 665 * but we are guaranteed that nh_control won't be freed in 666 * this epoch. Hence, nexthop can be safely unlinked. 667 * 668 * 2) nh_linked value is 1. In that case, nhops_destroy_rib() 669 * has been called and nhop unlink can be skipped. 670 */ 671 672 NET_EPOCH_ENTER(et); 673 if (refcount_release_if_not_last(&nh_priv->nh_linked)) { 674 ctl = nh_priv->nh_control; 675 if (unlink_nhop(ctl, nh_priv) == NULL) { 676 /* Do not try to reclaim */ 677 DPRINTF("Failed to unlink nexhop %p", nh_priv); 678 NET_EPOCH_EXIT(et); 679 return; 680 } 681 } 682 NET_EPOCH_EXIT(et); 683 684 epoch_call(net_epoch_preempt, destroy_nhop_epoch, 685 &nh_priv->nh_epoch_ctx); 686 } 687 688 int 689 nhop_ref_any(struct nhop_object *nh) 690 { 691 692 return (nhop_try_ref_object(nh)); 693 } 694 695 void 696 nhop_free_any(struct nhop_object *nh) 697 { 698 699 nhop_free(nh); 700 } 701 702 /* Helper functions */ 703 704 uint32_t 705 nhop_get_idx(const struct nhop_object *nh) 706 { 707 708 return (nh->nh_priv->nh_idx); 709 } 710 711 enum nhop_type 712 nhop_get_type(const struct nhop_object *nh) 713 { 714 715 return (nh->nh_priv->nh_type); 716 } 717 718 void 719 nhop_set_type(struct nhop_object *nh, enum nhop_type nh_type) 720 { 721 722 nh->nh_priv->nh_type = nh_type; 723 } 724 725 int 726 nhop_get_rtflags(const struct nhop_object *nh) 727 { 728 729 return (nh->nh_priv->rt_flags); 730 } 731 732 void 733 nhop_set_rtflags(struct nhop_object *nh, int rt_flags) 734 { 735 736 nh->nh_priv->rt_flags = rt_flags; 737 } 738 739 struct vnet * 740 nhop_get_vnet(const struct nhop_object *nh) 741 { 742 743 return (nh->nh_priv->nh_vnet); 744 } 745 746 void 747 nhops_update_ifmtu(struct rib_head *rh, struct ifnet *ifp, uint32_t mtu) 748 { 749 struct nh_control *ctl; 750 struct nhop_priv *nh_priv; 751 struct nhop_object *nh; 752 753 ctl = rh->nh_control; 754 755 NHOPS_WLOCK(ctl); 756 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) { 757 nh = nh_priv->nh; 758 if (nh->nh_ifp == ifp) { 759 if ((nh_priv->rt_flags & RTF_FIXEDMTU) == 0 || 760 nh->nh_mtu > mtu) { 761 /* Update MTU directly */ 762 nh->nh_mtu = mtu; 763 } 764 } 765 } CHT_SLIST_FOREACH_END; 766 NHOPS_WUNLOCK(ctl); 767 768 } 769 770 /* 771 * Dumps a single entry to sysctl buffer. 772 * 773 * Layout: 774 * rt_msghdr - generic RTM header to allow users to skip non-understood messages 775 * nhop_external - nexhop description structure (with length) 776 * nhop_addrs - structure encapsulating GW/SRC sockaddrs 777 */ 778 static int 779 dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w) 780 { 781 struct { 782 struct rt_msghdr rtm; 783 struct nhop_external nhe; 784 struct nhop_addrs na; 785 } arpc; 786 struct nhop_external *pnhe; 787 struct sockaddr *gw_sa, *src_sa; 788 struct sockaddr_storage ss; 789 size_t addrs_len; 790 int error; 791 792 //DPRINTF("Dumping: head %p nh %p flags %X req %p\n", rh, nh, nh->nh_flags, w); 793 794 memset(&arpc, 0, sizeof(arpc)); 795 796 arpc.rtm.rtm_msglen = sizeof(arpc); 797 arpc.rtm.rtm_version = RTM_VERSION; 798 arpc.rtm.rtm_type = RTM_GET; 799 //arpc.rtm.rtm_flags = RTF_UP; 800 arpc.rtm.rtm_flags = nh->nh_priv->rt_flags; 801 802 /* nhop_external */ 803 pnhe = &arpc.nhe; 804 pnhe->nh_len = sizeof(struct nhop_external); 805 pnhe->nh_idx = nh->nh_priv->nh_idx; 806 pnhe->nh_fib = rh->rib_fibnum; 807 pnhe->ifindex = nh->nh_ifp->if_index; 808 pnhe->aifindex = nh->nh_aifp->if_index; 809 pnhe->nh_family = nh->nh_priv->nh_family; 810 pnhe->nh_type = nh->nh_priv->nh_type; 811 pnhe->nh_mtu = nh->nh_mtu; 812 pnhe->nh_flags = nh->nh_flags; 813 814 memcpy(pnhe->nh_prepend, nh->nh_prepend, sizeof(nh->nh_prepend)); 815 pnhe->prepend_len = nh->nh_prepend_len; 816 pnhe->nh_refcount = nh->nh_priv->nh_refcnt; 817 pnhe->nh_pksent = counter_u64_fetch(nh->nh_pksent); 818 819 /* sockaddr container */ 820 addrs_len = sizeof(struct nhop_addrs); 821 arpc.na.gw_sa_off = addrs_len; 822 gw_sa = (struct sockaddr *)&nh->gw4_sa; 823 addrs_len += gw_sa->sa_len; 824 825 src_sa = nh->nh_ifa->ifa_addr; 826 if (src_sa->sa_family == AF_LINK) { 827 /* Shorten structure */ 828 memset(&ss, 0, sizeof(struct sockaddr_storage)); 829 fill_sdl_from_ifp((struct sockaddr_dl_short *)&ss, 830 nh->nh_ifa->ifa_ifp); 831 src_sa = (struct sockaddr *)&ss; 832 } 833 arpc.na.src_sa_off = addrs_len; 834 addrs_len += src_sa->sa_len; 835 836 /* Write total container length */ 837 arpc.na.na_len = addrs_len; 838 839 arpc.rtm.rtm_msglen += arpc.na.na_len - sizeof(struct nhop_addrs); 840 841 error = SYSCTL_OUT(w, &arpc, sizeof(arpc)); 842 if (error == 0) 843 error = SYSCTL_OUT(w, gw_sa, gw_sa->sa_len); 844 if (error == 0) 845 error = SYSCTL_OUT(w, src_sa, src_sa->sa_len); 846 847 return (error); 848 } 849 850 int 851 nhops_dump_sysctl(struct rib_head *rh, struct sysctl_req *w) 852 { 853 struct nh_control *ctl; 854 struct nhop_priv *nh_priv; 855 int error; 856 857 ctl = rh->nh_control; 858 859 NHOPS_RLOCK(ctl); 860 DPRINTF("NHDUMP: count=%u", ctl->nh_head.items_count); 861 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) { 862 error = dump_nhop_entry(rh, nh_priv->nh, w); 863 if (error != 0) { 864 NHOPS_RUNLOCK(ctl); 865 return (error); 866 } 867 } CHT_SLIST_FOREACH_END; 868 NHOPS_RUNLOCK(ctl); 869 870 return (0); 871 } 872