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