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