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