2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2020 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
32 #include "opt_route.h"
34 #include <sys/param.h>
35 #include <sys/systm.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>
45 #include <net/if_var.h>
46 #include <net/if_private.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
49 #include <net/route/route_ctl.h>
50 #include <net/route/route_var.h>
51 #include <net/route/nhop_utils.h>
52 #include <net/route/nhop.h>
53 #include <net/route/nhop_var.h>
56 #define DEBUG_MOD_NAME nhop_ctl
57 #define DEBUG_MAX_LEVEL LOG_DEBUG
58 #include <net/route/route_debug.h>
59 _DECLARE_DEBUG(LOG_INFO);
62 * This file contains core functionality for the nexthop ("nhop") route subsystem.
63 * The business logic needed to create nexhop objects is implemented here.
65 * Nexthops in the original sense are the objects containing all the necessary
66 * information to forward the packet to the selected destination.
67 * In particular, nexthop is defined by a combination of
68 * ifp, ifa, aifp, mtu, gw addr(if set), nh_type, nh_upper_family, mask of rt_flags and
71 * Additionally, each nexthop gets assigned its unique index (nexthop index).
72 * It serves two purposes: first one is to ease the ability of userland programs to
73 * reference nexthops by their index. The second one allows lookup algorithms to
74 * to store index instead of pointer (2 bytes vs 8) as a lookup result.
75 * All nexthops are stored in the resizable hash table.
77 * Basically, this file revolves around supporting 3 functions:
78 * 1) nhop_create_from_info / nhop_create_from_nhop, which contains all
79 * business logic on filling the nexthop fields based on the provided request.
80 * 2) nhop_get(), which gets a usable referenced nexthops.
83 * 1) non-exported functions start with verb
84 * 2) exported function starts with the subsystem prefix: "nhop"
87 static int dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w);
89 static int finalize_nhop(struct nh_control *ctl, struct nhop_object *nh, bool link);
90 static struct ifnet *get_aifp(const struct nhop_object *nh);
91 static void fill_sdl_from_ifp(struct sockaddr_dl_short *sdl, const struct ifnet *ifp);
93 static void destroy_nhop_epoch(epoch_context_t ctx);
94 static void destroy_nhop(struct nhop_object *nh);
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");
101 static uma_zone_t nhops_zone; /* Global zone for each and every nexthop */
103 #define NHOP_OBJECT_ALIGNED_SIZE roundup2(sizeof(struct nhop_object), \
105 #define NHOP_PRIV_ALIGNED_SIZE roundup2(sizeof(struct nhop_priv), \
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);
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.
126 static struct ifnet *
127 get_aifp(const struct nhop_object *nh)
129 struct ifnet *aifp = NULL;
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).
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);
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);
154 cmp_priv(const struct nhop_priv *_one, const struct nhop_priv *_two)
157 if (memcmp(_one->nh, _two->nh, NHOP_END_CMP) != 0)
160 if (memcmp(_one, _two, NH_PRIV_END_CMP) != 0)
167 * Conditionally sets @nh mtu data based on the @info data.
170 set_nhop_mtu_from_info(struct nhop_object *nh, const struct rt_addrinfo *info)
172 if (info->rti_mflags & RTV_MTU)
173 nhop_set_mtu(nh, info->rti_rmx->rmx_mtu, true);
177 * Fills in shorted link-level sockadd version suitable to be stored inside the
178 * nexthop gateway buffer.
181 fill_sdl_from_ifp(struct sockaddr_dl_short *sdl, const struct ifnet *ifp)
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;
192 set_nhop_gw_from_info(struct nhop_object *nh, struct rt_addrinfo *info)
196 gw = info->rti_info[RTAX_GATEWAY];
198 bool is_gw = info->rti_flags & RTF_GATEWAY;
200 if ((gw->sa_family == AF_LINK) && !is_gw) {
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.
211 struct sockaddr_dl *sdl = (struct sockaddr_dl *)gw;
212 struct ifnet *ifp = ifnet_byindex(sdl->sdl_index);
214 FIB_NH_LOG(LOG_DEBUG, nh, "error: invalid ifindex %d",
218 nhop_set_direct_gw(nh, ifp);
222 * Multiple options here:
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.
229 * In both cases, save the original nexthop to make the callers
232 if (!nhop_set_gw(nh, gw, is_gw))
239 set_nhop_expire_from_info(struct nhop_object *nh, const struct rt_addrinfo *info)
241 uint32_t nh_expire = 0;
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);
250 * Creates a new nexthop based on the information in @info.
253 * 0 on success, filling @nh_ret with the desired nexthop object ptr
257 nhop_create_from_info(struct rib_head *rnh, struct rt_addrinfo *info,
258 struct nhop_object **nh_ret)
264 MPASS(info->rti_ifa != NULL);
265 MPASS(info->rti_ifp != NULL);
267 if (info->rti_info[RTAX_GATEWAY] == NULL) {
268 FIB_RH_LOG(LOG_DEBUG, rnh, "error: empty gateway");
272 struct nhop_object *nh = nhop_alloc(rnh->rib_fibnum, rnh->rib_family);
276 if ((error = set_nhop_gw_from_info(nh, info)) != 0) {
280 nhop_set_transmit_ifp(nh, info->rti_ifp);
282 nhop_set_blackhole(nh, info->rti_flags & (RTF_BLACKHOLE | RTF_REJECT));
284 error = rnh->rnh_set_nh_pfxflags(rnh->rib_fibnum, info->rti_info[RTAX_DST],
285 info->rti_info[RTAX_NETMASK], nh);
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);
292 set_nhop_mtu_from_info(nh, info);
293 nhop_set_src(nh, info->rti_ifa);
296 * The remaining fields are either set from nh_preadd hook
297 * or are computed from the provided data
299 *nh_ret = nhop_get_nhop(nh, &error);
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.
310 * Returns 0 on success, storing referenced nexthop in @pnh.
311 * Otherwise, errno is returned.
314 nhop_get_nhop(struct nhop_object *nh, int *perror)
316 struct rib_head *rnh = nhop_get_rh(nh);
318 if (__predict_false(rnh == NULL)) {
319 *perror = EAFNOSUPPORT;
324 return (nhop_get_nhop_internal(rnh, nh, perror));
328 nhop_get_nhop_internal(struct rib_head *rnh, struct nhop_object *nh, int *perror)
330 struct nhop_priv *tmp_priv;
333 nh->nh_aifp = get_aifp(nh);
335 /* Give the protocols chance to augment nexthop properties */
336 error = rnh->rnh_augment_nh(rnh->rib_fibnum, nh);
343 tmp_priv = find_nhop(rnh->nh_control, nh->nh_priv);
344 if (tmp_priv != NULL) {
347 return (tmp_priv->nh);
351 * Existing nexthop not found, need to create new one.
352 * Note: multiple simultaneous requests
353 * can result in multiple equal nexhops existing in the
354 * nexthop table. This is not a not a problem until the
355 * relative number of such nexthops is significant, which
356 * is extremely unlikely.
358 *perror = finalize_nhop(rnh->nh_control, nh, true);
359 return (*perror == 0 ? nh : NULL);
363 * Gets referenced but unlinked nhop.
364 * Alocates/references the remaining bits of the nexthop data, so
365 * it can be safely linked later or used as a clone source.
367 * Returns 0 on success.
370 nhop_get_unlinked(struct nhop_object *nh)
372 struct rib_head *rnh = nhop_get_rh(nh);
374 if (__predict_false(rnh == NULL)) {
376 return (EAFNOSUPPORT);
379 nh->nh_aifp = get_aifp(nh);
381 return (finalize_nhop(rnh->nh_control, nh, false));
386 * Update @nh with data supplied in @info.
387 * This is a helper function to support route changes.
389 * It limits the changes that can be done to the route to the following:
390 * 1) all combination of gateway changes
391 * 2) route flags (FLAG[123],STATIC)
395 * 0 on success, errno otherwise
398 alter_nhop_from_info(struct nhop_object *nh, struct rt_addrinfo *info)
400 struct sockaddr *info_gw;
403 /* Update MTU if set in the request*/
404 set_nhop_mtu_from_info(nh, info);
406 /* Only RTF_FLAG[123] and RTF_STATIC */
407 uint32_t rt_flags = nhop_get_rtflags(nh) & ~RT_CHANGE_RTFLAGS_MASK;
408 rt_flags |= info->rti_flags & RT_CHANGE_RTFLAGS_MASK;
409 nhop_set_rtflags(nh, rt_flags);
411 /* Consider gateway change */
412 info_gw = info->rti_info[RTAX_GATEWAY];
413 if (info_gw != NULL) {
414 error = set_nhop_gw_from_info(nh, info);
419 if (info->rti_ifa != NULL)
420 nhop_set_src(nh, info->rti_ifa);
421 if (info->rti_ifp != NULL)
422 nhop_set_transmit_ifp(nh, info->rti_ifp);
428 * Creates new nexthop based on @nh_orig and augmentation data from @info.
429 * Helper function used in the route changes, please see
430 * alter_nhop_from_info() comments for more details.
433 * 0 on success, filling @nh_ret with the desired nexthop object
437 nhop_create_from_nhop(struct rib_head *rnh, const struct nhop_object *nh_orig,
438 struct rt_addrinfo *info, struct nhop_object **pnh)
440 struct nhop_object *nh;
445 nh = nhop_alloc(rnh->rib_fibnum, rnh->rib_family);
449 nhop_copy(nh, nh_orig);
451 error = alter_nhop_from_info(nh, info);
457 *pnh = nhop_get_nhop(nh, &error);
463 reference_nhop_deps(struct nhop_object *nh)
465 if (!ifa_try_ref(nh->nh_ifa))
467 nh->nh_aifp = get_aifp(nh);
468 if (!if_try_ref(nh->nh_aifp)) {
469 ifa_free(nh->nh_ifa);
472 FIB_NH_LOG(LOG_DEBUG2, nh, "nh_aifp: %s nh_ifp %s",
473 if_name(nh->nh_aifp), if_name(nh->nh_ifp));
474 if (!if_try_ref(nh->nh_ifp)) {
475 ifa_free(nh->nh_ifa);
476 if_rele(nh->nh_aifp);
484 * Alocates/references the remaining bits of nexthop data and links
485 * it to the hash table.
486 * Returns 0 if successful,
487 * errno otherwise. @nh_priv is freed in case of error.
490 finalize_nhop(struct nh_control *ctl, struct nhop_object *nh, bool link)
493 /* Allocate per-cpu packet counter */
494 nh->nh_pksent = counter_u64_alloc(M_NOWAIT);
495 if (nh->nh_pksent == NULL) {
497 RTSTAT_INC(rts_nh_alloc_failure);
498 FIB_NH_LOG(LOG_WARNING, nh, "counter_u64_alloc() failed");
502 if (!reference_nhop_deps(nh)) {
503 counter_u64_free(nh->nh_pksent);
505 RTSTAT_INC(rts_nh_alloc_failure);
506 FIB_NH_LOG(LOG_WARNING, nh, "interface reference failed");
510 /* Save vnet to ease destruction */
511 nh->nh_priv->nh_vnet = curvnet;
513 /* Please see nhop_free() comments on the initial value */
514 refcount_init(&nh->nh_priv->nh_linked, 2);
516 MPASS(nh->nh_priv->nh_fibnum == ctl->ctl_rh->rib_fibnum);
519 refcount_release(&nh->nh_priv->nh_linked);
521 nh->nh_priv->nh_finalized = 1;
523 } else if (link_nhop(ctl, nh->nh_priv) == 0) {
525 * Adding nexthop to the datastructures
526 * failed. Call destructor w/o waiting for
527 * the epoch end, as nexthop is not used
530 char nhbuf[NHOP_PRINT_BUFSIZE];
531 FIB_NH_LOG(LOG_WARNING, nh, "failed to link %s",
532 nhop_print_buf(nh, nhbuf, sizeof(nhbuf)));
538 IF_DEBUG_LEVEL(LOG_DEBUG) {
539 char nhbuf[NHOP_PRINT_BUFSIZE] __unused;
540 FIB_NH_LOG(LOG_DEBUG, nh, "finalized: %s",
541 nhop_print_buf(nh, nhbuf, sizeof(nhbuf)));
548 destroy_nhop(struct nhop_object *nh)
551 if_rele(nh->nh_aifp);
552 ifa_free(nh->nh_ifa);
553 counter_u64_free(nh->nh_pksent);
555 uma_zfree(nhops_zone, nh);
559 * Epoch callback indicating nhop is safe to destroy
562 destroy_nhop_epoch(epoch_context_t ctx)
564 struct nhop_priv *nh_priv;
566 nh_priv = __containerof(ctx, struct nhop_priv, nh_epoch_ctx);
568 destroy_nhop(nh_priv->nh);
572 nhop_ref_object(struct nhop_object *nh)
574 u_int old __diagused;
576 old = refcount_acquire(&nh->nh_priv->nh_refcnt);
577 KASSERT(old > 0, ("%s: nhop object %p has 0 refs", __func__, nh));
581 nhop_try_ref_object(struct nhop_object *nh)
584 return (refcount_acquire_if_not_zero(&nh->nh_priv->nh_refcnt));
588 nhop_free(struct nhop_object *nh)
590 struct nh_control *ctl;
591 struct nhop_priv *nh_priv = nh->nh_priv;
592 struct epoch_tracker et;
594 if (!refcount_release(&nh_priv->nh_refcnt))
597 /* allows to use nhop_free() during nhop init */
598 if (__predict_false(nh_priv->nh_finalized == 0)) {
599 uma_zfree(nhops_zone, nh);
603 IF_DEBUG_LEVEL(LOG_DEBUG) {
604 char nhbuf[NHOP_PRINT_BUFSIZE] __unused;
605 FIB_NH_LOG(LOG_DEBUG, nh, "deleting %s",
606 nhop_print_buf(nh, nhbuf, sizeof(nhbuf)));
610 * There are only 2 places, where nh_linked can be decreased:
611 * rib destroy (nhops_destroy_rib) and this function.
612 * nh_link can never be increased.
614 * Hence, use initial value of 2 to make use of
615 * refcount_release_if_not_last().
617 * There can be two scenarious when calling this function:
619 * 1) nh_linked value is 2. This means that either
620 * nhops_destroy_rib() has not been called OR it is running,
621 * but we are guaranteed that nh_control won't be freed in
622 * this epoch. Hence, nexthop can be safely unlinked.
624 * 2) nh_linked value is 1. In that case, nhops_destroy_rib()
625 * has been called and nhop unlink can be skipped.
629 if (refcount_release_if_not_last(&nh_priv->nh_linked)) {
630 ctl = nh_priv->nh_control;
631 if (unlink_nhop(ctl, nh_priv) == NULL) {
632 /* Do not try to reclaim */
633 char nhbuf[NHOP_PRINT_BUFSIZE];
634 FIB_NH_LOG(LOG_WARNING, nh, "failed to unlink %s",
635 nhop_print_buf(nh, nhbuf, sizeof(nhbuf)));
642 NET_EPOCH_CALL(destroy_nhop_epoch, &nh_priv->nh_epoch_ctx);
646 nhop_ref_any(struct nhop_object *nh)
649 if (!NH_IS_NHGRP(nh))
652 nhgrp_ref_object((struct nhgrp_object *)nh);
659 nhop_free_any(struct nhop_object *nh)
663 if (!NH_IS_NHGRP(nh))
666 nhgrp_free((struct nhgrp_object *)nh);
672 /* Nhop-related methods */
675 * Allocates an empty unlinked nhop object.
676 * Returns object pointer or NULL on failure
679 nhop_alloc(uint32_t fibnum, int family)
681 struct nhop_object *nh;
682 struct nhop_priv *nh_priv;
684 nh = (struct nhop_object *)uma_zalloc(nhops_zone, M_NOWAIT | M_ZERO);
685 if (__predict_false(nh == NULL))
688 nh_priv = (struct nhop_priv *)((char *)nh + NHOP_OBJECT_ALIGNED_SIZE);
689 nh->nh_priv = nh_priv;
692 nh_priv->nh_upper_family = family;
693 nh_priv->nh_fibnum = fibnum;
695 /* Setup refcount early to allow nhop_free() to work */
696 refcount_init(&nh_priv->nh_refcnt, 1);
702 nhop_copy(struct nhop_object *nh, const struct nhop_object *nh_orig)
704 struct nhop_priv *nh_priv = nh->nh_priv;
706 nh->nh_flags = nh_orig->nh_flags;
707 nh->nh_mtu = nh_orig->nh_mtu;
708 memcpy(&nh->gw_sa, &nh_orig->gw_sa, nh_orig->gw_sa.sa_len);
709 nh->nh_ifp = nh_orig->nh_ifp;
710 nh->nh_ifa = nh_orig->nh_ifa;
711 nh->nh_aifp = nh_orig->nh_aifp;
713 nh_priv->nh_upper_family = nh_orig->nh_priv->nh_upper_family;
714 nh_priv->nh_neigh_family = nh_orig->nh_priv->nh_neigh_family;
715 nh_priv->nh_type = nh_orig->nh_priv->nh_type;
716 nh_priv->rt_flags = nh_orig->nh_priv->rt_flags;
717 nh_priv->nh_fibnum = nh_orig->nh_priv->nh_fibnum;
718 nh_priv->nh_origin = nh_orig->nh_priv->nh_origin;
722 nhop_set_direct_gw(struct nhop_object *nh, struct ifnet *ifp)
724 nh->nh_flags &= ~NHF_GATEWAY;
725 nh->nh_priv->rt_flags &= ~RTF_GATEWAY;
726 nh->nh_priv->nh_neigh_family = nh->nh_priv->nh_upper_family;
728 fill_sdl_from_ifp(&nh->gwl_sa, ifp);
729 memset(&nh->gw_buf[nh->gw_sa.sa_len], 0, sizeof(nh->gw_buf) - nh->gw_sa.sa_len);
733 nhop_check_gateway(int upper_family, int neigh_family)
735 if (upper_family == neigh_family)
737 else if (neigh_family == AF_UNSPEC || neigh_family == AF_LINK)
739 #if defined(INET) && defined(INET6)
740 else if (upper_family == AF_INET && neigh_family == AF_INET6 &&
749 * Sets gateway for the nexthop.
750 * It can be "normal" gateway with is_gw set or a special form of
751 * adding interface route, refering to it by specifying local interface
752 * address. In that case is_gw is set to false.
755 nhop_set_gw(struct nhop_object *nh, const struct sockaddr *gw, bool is_gw)
757 if (gw->sa_len > sizeof(nh->gw_buf)) {
758 FIB_NH_LOG(LOG_DEBUG, nh, "nhop SA size too big: AF %d len %u",
759 gw->sa_family, gw->sa_len);
763 if (!nhop_check_gateway(nh->nh_priv->nh_upper_family, gw->sa_family)) {
764 FIB_NH_LOG(LOG_DEBUG, nh,
765 "error: invalid dst/gateway family combination (%d, %d)",
766 nh->nh_priv->nh_upper_family, gw->sa_family);
770 memcpy(&nh->gw_sa, gw, gw->sa_len);
771 memset(&nh->gw_buf[gw->sa_len], 0, sizeof(nh->gw_buf) - gw->sa_len);
774 nh->nh_flags |= NHF_GATEWAY;
775 nh->nh_priv->rt_flags |= RTF_GATEWAY;
776 nh->nh_priv->nh_neigh_family = gw->sa_family;
778 nh->nh_flags &= ~NHF_GATEWAY;
779 nh->nh_priv->rt_flags &= ~RTF_GATEWAY;
780 nh->nh_priv->nh_neigh_family = nh->nh_priv->nh_upper_family;
787 nhop_set_upper_family(struct nhop_object *nh, int family)
789 if (!nhop_check_gateway(nh->nh_priv->nh_upper_family, family)) {
790 FIB_NH_LOG(LOG_DEBUG, nh,
791 "error: invalid upper/neigh family combination (%d, %d)",
792 nh->nh_priv->nh_upper_family, family);
796 nh->nh_priv->nh_upper_family = family;
801 nhop_set_broadcast(struct nhop_object *nh, bool is_broadcast)
804 nh->nh_flags |= NHF_BROADCAST;
805 nh->nh_priv->rt_flags |= RTF_BROADCAST;
807 nh->nh_flags &= ~NHF_BROADCAST;
808 nh->nh_priv->rt_flags &= ~RTF_BROADCAST;
813 nhop_set_blackhole(struct nhop_object *nh, int blackhole_rt_flag)
815 nh->nh_flags &= ~(NHF_BLACKHOLE | NHF_REJECT);
816 nh->nh_priv->rt_flags &= ~(RTF_BLACKHOLE | RTF_REJECT);
817 switch (blackhole_rt_flag) {
819 nh->nh_flags |= NHF_BLACKHOLE;
820 nh->nh_priv->rt_flags |= RTF_BLACKHOLE;
823 nh->nh_flags |= NHF_REJECT;
824 nh->nh_priv->rt_flags |= RTF_REJECT;
827 /* Not a blackhole nexthop */
832 nh->nh_flags &= ~NHF_GATEWAY;
833 nh->nh_priv->rt_flags &= ~RTF_GATEWAY;
834 nh->nh_priv->nh_neigh_family = nh->nh_priv->nh_upper_family;
836 bzero(&nh->gw_sa, sizeof(nh->gw_sa));
838 switch (nh->nh_priv->nh_upper_family) {
841 nh->gw4_sa.sin_family = AF_INET;
842 nh->gw4_sa.sin_len = sizeof(struct sockaddr_in);
843 nh->gw4_sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
848 nh->gw6_sa.sin6_family = AF_INET6;
849 nh->gw6_sa.sin6_len = sizeof(struct sockaddr_in6);
850 nh->gw6_sa.sin6_addr = in6addr_loopback;
857 nhop_set_redirect(struct nhop_object *nh, bool is_redirect)
860 nh->nh_priv->rt_flags |= RTF_DYNAMIC;
861 nh->nh_flags |= NHF_REDIRECT;
863 nh->nh_priv->rt_flags &= ~RTF_DYNAMIC;
864 nh->nh_flags &= ~NHF_REDIRECT;
869 nhop_set_pinned(struct nhop_object *nh, bool is_pinned)
872 nh->nh_priv->rt_flags |= RTF_PINNED;
874 nh->nh_priv->rt_flags &= ~RTF_PINNED;
878 nhop_get_idx(const struct nhop_object *nh)
881 return (nh->nh_priv->nh_idx);
885 nhop_get_uidx(const struct nhop_object *nh)
887 return (nh->nh_priv->nh_uidx);
891 nhop_set_uidx(struct nhop_object *nh, uint32_t uidx)
893 nh->nh_priv->nh_uidx = uidx;
897 nhop_get_type(const struct nhop_object *nh)
900 return (nh->nh_priv->nh_type);
904 nhop_set_type(struct nhop_object *nh, enum nhop_type nh_type)
907 nh->nh_priv->nh_type = nh_type;
911 nhop_get_rtflags(const struct nhop_object *nh)
914 return (nh->nh_priv->rt_flags);
918 * Sets generic rtflags that are not covered by other functions.
921 nhop_set_rtflags(struct nhop_object *nh, int rt_flags)
923 nh->nh_priv->rt_flags &= ~RT_SET_RTFLAGS_MASK;
924 nh->nh_priv->rt_flags |= (rt_flags & RT_SET_RTFLAGS_MASK);
928 * Sets flags that are specific to the prefix (NHF_HOST or NHF_DEFAULT).
931 nhop_set_pxtype_flag(struct nhop_object *nh, int nh_flag)
933 if (nh_flag == NHF_HOST) {
934 nh->nh_flags |= NHF_HOST;
935 nh->nh_flags &= ~NHF_DEFAULT;
936 nh->nh_priv->rt_flags |= RTF_HOST;
937 } else if (nh_flag == NHF_DEFAULT) {
938 nh->nh_flags |= NHF_DEFAULT;
939 nh->nh_flags &= ~NHF_HOST;
940 nh->nh_priv->rt_flags &= ~RTF_HOST;
942 nh->nh_flags &= ~(NHF_HOST | NHF_DEFAULT);
943 nh->nh_priv->rt_flags &= ~RTF_HOST;
948 * Sets nhop MTU. Sets RTF_FIXEDMTU if mtu is explicitly
949 * specified by userland.
952 nhop_set_mtu(struct nhop_object *nh, uint32_t mtu, bool from_user)
956 nh->nh_priv->rt_flags |= RTF_FIXEDMTU;
958 nh->nh_priv->rt_flags &= ~RTF_FIXEDMTU;
964 nhop_set_src(struct nhop_object *nh, struct ifaddr *ifa)
970 nhop_set_transmit_ifp(struct nhop_object *nh, struct ifnet *ifp)
977 nhop_get_vnet(const struct nhop_object *nh)
980 return (nh->nh_priv->nh_vnet);
984 nhop_select_func(struct nhop_object *nh, uint32_t flowid)
987 return (nhop_select(nh, flowid));
991 * Returns address family of the traffic uses the nexthop.
994 nhop_get_upper_family(const struct nhop_object *nh)
996 return (nh->nh_priv->nh_upper_family);
1000 * Returns address family of the LLE or gateway that is used
1001 * to forward the traffic to.
1004 nhop_get_neigh_family(const struct nhop_object *nh)
1006 return (nh->nh_priv->nh_neigh_family);
1010 nhop_get_fibnum(const struct nhop_object *nh)
1012 return (nh->nh_priv->nh_fibnum);
1016 nhop_set_fibnum(struct nhop_object *nh, uint32_t fibnum)
1018 nh->nh_priv->nh_fibnum = fibnum;
1022 nhop_get_expire(const struct nhop_object *nh)
1024 return (nh->nh_priv->nh_expire);
1028 nhop_set_expire(struct nhop_object *nh, uint32_t expire)
1030 MPASS(!NH_IS_LINKED(nh));
1031 nh->nh_priv->nh_expire = expire;
1035 nhop_get_rh(const struct nhop_object *nh)
1037 uint32_t fibnum = nhop_get_fibnum(nh);
1038 int family = nhop_get_neigh_family(nh);
1040 return (rt_tables_get_rnh(fibnum, family));
1044 nhop_get_origin(const struct nhop_object *nh)
1046 return (nh->nh_priv->nh_origin);
1050 nhop_set_origin(struct nhop_object *nh, uint8_t origin)
1052 nh->nh_priv->nh_origin = origin;
1056 nhops_update_ifmtu(struct rib_head *rh, struct ifnet *ifp, uint32_t mtu)
1058 struct nh_control *ctl;
1059 struct nhop_priv *nh_priv;
1060 struct nhop_object *nh;
1062 ctl = rh->nh_control;
1065 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) {
1067 if (nh->nh_ifp == ifp) {
1068 if ((nh_priv->rt_flags & RTF_FIXEDMTU) == 0 ||
1070 /* Update MTU directly */
1074 } CHT_SLIST_FOREACH_END;
1079 struct nhop_object *
1080 nhops_iter_start(struct nhop_iter *iter)
1082 if (iter->rh == NULL)
1083 iter->rh = rt_tables_get_rnh_safe(iter->fibnum, iter->family);
1084 if (iter->rh != NULL) {
1085 struct nh_control *ctl = iter->rh->nh_control;
1090 iter->_next = CHT_FIRST(&ctl->nh_head, iter->_i);
1092 return (nhops_iter_next(iter));
1097 struct nhop_object *
1098 nhops_iter_next(struct nhop_iter *iter)
1100 struct nhop_priv *nh_priv = iter->_next;
1102 if (nh_priv != NULL) {
1103 iter->_next = nh_priv->nh_next;
1104 return (nh_priv->nh);
1107 struct nh_control *ctl = iter->rh->nh_control;
1108 while (++iter->_i < ctl->nh_head.hash_size) {
1109 nh_priv = CHT_FIRST(&ctl->nh_head, iter->_i);
1110 if (nh_priv != NULL) {
1111 iter->_next = nh_priv->nh_next;
1112 return (nh_priv->nh);
1120 nhops_iter_stop(struct nhop_iter *iter)
1122 if (iter->rh != NULL) {
1123 struct nh_control *ctl = iter->rh->nh_control;
1130 * Prints nexthop @nh data in the provided @buf.
1131 * Example: nh#33/inet/em0/192.168.0.1
1134 nhop_print_buf(const struct nhop_object *nh, char *buf, size_t bufsize)
1136 #if defined(INET) || defined(INET6)
1137 char abuf[INET6_ADDRSTRLEN];
1139 struct nhop_priv *nh_priv = nh->nh_priv;
1140 const char *upper_str = rib_print_family(nh->nh_priv->nh_upper_family);
1142 switch (nh->gw_sa.sa_family) {
1145 inet_ntop(AF_INET, &nh->gw4_sa.sin_addr, abuf, sizeof(abuf));
1146 snprintf(buf, bufsize, "nh#%d/%s/%s/%s", nh_priv->nh_idx, upper_str,
1147 if_name(nh->nh_ifp), abuf);
1152 inet_ntop(AF_INET6, &nh->gw6_sa.sin6_addr, abuf, sizeof(abuf));
1153 snprintf(buf, bufsize, "nh#%d/%s/%s/%s", nh_priv->nh_idx, upper_str,
1154 if_name(nh->nh_ifp), abuf);
1158 snprintf(buf, bufsize, "nh#%d/%s/%s/resolve", nh_priv->nh_idx, upper_str,
1159 if_name(nh->nh_ifp));
1162 snprintf(buf, bufsize, "nh#%d/%s/%s/????", nh_priv->nh_idx, upper_str,
1163 if_name(nh->nh_ifp));
1171 nhop_print_buf_any(const struct nhop_object *nh, char *buf, size_t bufsize)
1174 if (NH_IS_NHGRP(nh))
1175 return (nhgrp_print_buf((const struct nhgrp_object *)nh, buf, bufsize));
1178 return (nhop_print_buf(nh, buf, bufsize));
1182 * Dumps a single entry to sysctl buffer.
1185 * rt_msghdr - generic RTM header to allow users to skip non-understood messages
1186 * nhop_external - nexhop description structure (with length)
1187 * nhop_addrs - structure encapsulating GW/SRC sockaddrs
1190 dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w)
1193 struct rt_msghdr rtm;
1194 struct nhop_external nhe;
1195 struct nhop_addrs na;
1197 struct nhop_external *pnhe;
1198 struct sockaddr *gw_sa, *src_sa;
1199 struct sockaddr_storage ss;
1203 memset(&arpc, 0, sizeof(arpc));
1205 arpc.rtm.rtm_msglen = sizeof(arpc);
1206 arpc.rtm.rtm_version = RTM_VERSION;
1207 arpc.rtm.rtm_type = RTM_GET;
1208 //arpc.rtm.rtm_flags = RTF_UP;
1209 arpc.rtm.rtm_flags = nh->nh_priv->rt_flags;
1213 pnhe->nh_len = sizeof(struct nhop_external);
1214 pnhe->nh_idx = nh->nh_priv->nh_idx;
1215 pnhe->nh_fib = rh->rib_fibnum;
1216 pnhe->ifindex = nh->nh_ifp->if_index;
1217 pnhe->aifindex = nh->nh_aifp->if_index;
1218 pnhe->nh_family = nh->nh_priv->nh_upper_family;
1219 pnhe->nh_type = nh->nh_priv->nh_type;
1220 pnhe->nh_mtu = nh->nh_mtu;
1221 pnhe->nh_flags = nh->nh_flags;
1223 memcpy(pnhe->nh_prepend, nh->nh_prepend, sizeof(nh->nh_prepend));
1224 pnhe->prepend_len = nh->nh_prepend_len;
1225 pnhe->nh_refcount = nh->nh_priv->nh_refcnt;
1226 pnhe->nh_pksent = counter_u64_fetch(nh->nh_pksent);
1228 /* sockaddr container */
1229 addrs_len = sizeof(struct nhop_addrs);
1230 arpc.na.gw_sa_off = addrs_len;
1231 gw_sa = (struct sockaddr *)&nh->gw4_sa;
1232 addrs_len += gw_sa->sa_len;
1234 src_sa = nh->nh_ifa->ifa_addr;
1235 if (src_sa->sa_family == AF_LINK) {
1236 /* Shorten structure */
1237 memset(&ss, 0, sizeof(struct sockaddr_storage));
1238 fill_sdl_from_ifp((struct sockaddr_dl_short *)&ss,
1239 nh->nh_ifa->ifa_ifp);
1240 src_sa = (struct sockaddr *)&ss;
1242 arpc.na.src_sa_off = addrs_len;
1243 addrs_len += src_sa->sa_len;
1245 /* Write total container length */
1246 arpc.na.na_len = addrs_len;
1248 arpc.rtm.rtm_msglen += arpc.na.na_len - sizeof(struct nhop_addrs);
1250 error = SYSCTL_OUT(w, &arpc, sizeof(arpc));
1252 error = SYSCTL_OUT(w, gw_sa, gw_sa->sa_len);
1254 error = SYSCTL_OUT(w, src_sa, src_sa->sa_len);
1260 nhops_get_count(struct rib_head *rh)
1262 struct nh_control *ctl;
1265 ctl = rh->nh_control;
1268 count = ctl->nh_head.items_count;
1275 nhops_dump_sysctl(struct rib_head *rh, struct sysctl_req *w)
1277 struct nh_control *ctl;
1278 struct nhop_priv *nh_priv;
1281 ctl = rh->nh_control;
1284 FIB_RH_LOG(LOG_DEBUG, rh, "dump %u items", ctl->nh_head.items_count);
1285 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) {
1286 error = dump_nhop_entry(rh, nh_priv->nh, w);
1291 } CHT_SLIST_FOREACH_END;