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