xref: /freebsd/sys/net/route/nhop_ctl.c (revision a3d9bf49b57923118c339642594246ef73872ee8)
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/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 /*
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 #define	NHOP_OBJECT_ALIGNED_SIZE	roundup2(sizeof(struct nhop_object), \
103 							2 * CACHE_LINE_SIZE)
104 #define	NHOP_PRIV_ALIGNED_SIZE		roundup2(sizeof(struct nhop_priv), \
105 							2 * CACHE_LINE_SIZE)
106 void
107 nhops_init(void)
108 {
109 
110 	nhops_zone = uma_zcreate("routing nhops",
111 	    NHOP_OBJECT_ALIGNED_SIZE + NHOP_PRIV_ALIGNED_SIZE,
112 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
113 }
114 
115 /*
116  * Fetches the interface of source address used by the route.
117  * In all cases except interface-address-route it would be the
118  * same as the transmit interfaces.
119  * However, for the interface address this function will return
120  * this interface ifp instead of loopback. This is needed to support
121  * link-local IPv6 loopback communications.
122  *
123  * If @reference is non-zero, found ifp is referenced.
124  *
125  * Returns found ifp.
126  */
127 static struct ifnet *
128 get_aifp(const struct nhop_object *nh, int reference)
129 {
130 	struct ifnet *aifp = NULL;
131 
132 	/*
133 	 * Adjust the "outgoing" interface.  If we're going to loop
134 	 * the packet back to ourselves, the ifp would be the loopback
135 	 * interface. However, we'd rather know the interface associated
136 	 * to the destination address (which should probably be one of
137 	 * our own addresses).
138 	 */
139 	if ((nh->nh_ifp->if_flags & IFF_LOOPBACK) &&
140 			nh->gw_sa.sa_family == AF_LINK) {
141 		if (reference)
142 			aifp = ifnet_byindex_ref(nh->gwl_sa.sdl_index);
143 		else
144 			aifp = ifnet_byindex(nh->gwl_sa.sdl_index);
145 		if (aifp == NULL) {
146 			DPRINTF("unable to get aifp for %s index %d",
147 				if_name(nh->nh_ifp), nh->gwl_sa.sdl_index);
148 		}
149 	}
150 
151 	if (aifp == NULL) {
152 		aifp = nh->nh_ifp;
153 		if (reference)
154 			if_ref(aifp);
155 	}
156 
157 	return (aifp);
158 }
159 
160 int
161 cmp_priv(const struct nhop_priv *_one, const struct nhop_priv *_two)
162 {
163 
164 	if (memcmp(_one->nh, _two->nh, NHOP_END_CMP) != 0)
165 		return (0);
166 
167 	if ((_one->nh_type != _two->nh_type) ||
168 	    (_one->nh_family != _two->nh_family))
169 		return (0);
170 
171 	return (1);
172 }
173 
174 /*
175  * Conditionally sets @nh mtu data based on the @info data.
176  */
177 static void
178 set_nhop_mtu_from_info(struct nhop_object *nh, const struct rt_addrinfo *info)
179 {
180 
181 	if (info->rti_mflags & RTV_MTU) {
182 		if (info->rti_rmx->rmx_mtu != 0) {
183 			/*
184 			 * MTU was explicitly provided by user.
185 			 * Keep it.
186 			 */
187 
188 			nh->nh_priv->rt_flags |= RTF_FIXEDMTU;
189 		} else {
190 			/*
191 			 * User explicitly sets MTU to 0.
192 			 * Assume rollback to default.
193 			 */
194 			nh->nh_priv->rt_flags &= ~RTF_FIXEDMTU;
195 		}
196 		nh->nh_mtu = info->rti_rmx->rmx_mtu;
197 	}
198 }
199 
200 /*
201  * Fills in shorted link-level sockadd version suitable to be stored inside the
202  *  nexthop gateway buffer.
203  */
204 static void
205 fill_sdl_from_ifp(struct sockaddr_dl_short *sdl, const struct ifnet *ifp)
206 {
207 
208 	bzero(sdl, sizeof(struct sockaddr_dl_short));
209 	sdl->sdl_family = AF_LINK;
210 	sdl->sdl_len = sizeof(struct sockaddr_dl_short);
211 	sdl->sdl_index = ifp->if_index;
212 	sdl->sdl_type = ifp->if_type;
213 }
214 
215 static int
216 set_nhop_gw_from_info(struct nhop_object *nh, struct rt_addrinfo *info)
217 {
218 	struct sockaddr *gw;
219 
220 	gw = info->rti_info[RTAX_GATEWAY];
221 	KASSERT(gw != NULL, ("gw is NULL"));
222 
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 		/*
233 		 * Interface route. Currently the route.c code adds
234 		 * sa of type AF_LINK, which is 56 bytes long. The only
235 		 * meaningful data there is the interface index. It is used
236 		 * used is the IPv6 loopback output, where we need to preserve
237 		 * the original interface to maintain proper scoping.
238 		 * Despite the fact that nexthop code stores original interface
239 		 * in the separate field (nh_aifp, see below), write AF_LINK
240 		 * compatible sa with shorter total length.
241 		 */
242 		struct sockaddr_dl *sdl;
243 		struct ifnet *ifp;
244 
245 		/* Fetch and validate interface index */
246 		sdl = (struct sockaddr_dl *)gw;
247 		if (sdl->sdl_family != AF_LINK) {
248 			DPRINTF("unsupported AF: %d", sdl->sdl_family);
249 			return (ENOTSUP);
250 		}
251 		ifp = ifnet_byindex(sdl->sdl_index);
252 		if (ifp == NULL) {
253 			DPRINTF("invalid ifindex %d", sdl->sdl_index);
254 			return (EINVAL);
255 		}
256 		fill_sdl_from_ifp(&nh->gwl_sa, ifp);
257 	}
258 
259 	return (0);
260 }
261 
262 static uint16_t
263 convert_rt_to_nh_flags(int rt_flags)
264 {
265 	uint16_t res;
266 
267 	res = (rt_flags & RTF_REJECT) ? NHF_REJECT : 0;
268 	res |= (rt_flags & RTF_HOST) ? NHF_HOST : 0;
269 	res |= (rt_flags & RTF_BLACKHOLE) ? NHF_BLACKHOLE : 0;
270 	res |= (rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) ? NHF_REDIRECT : 0;
271 	res |= (rt_flags & RTF_BROADCAST) ? NHF_BROADCAST : 0;
272 	res |= (rt_flags & RTF_GATEWAY) ? NHF_GATEWAY : 0;
273 
274 	return (res);
275 }
276 
277 static int
278 fill_nhop_from_info(struct nhop_priv *nh_priv, struct rt_addrinfo *info)
279 {
280 	int error, rt_flags;
281 	struct nhop_object *nh;
282 
283 	nh = nh_priv->nh;
284 
285 	rt_flags = info->rti_flags & NHOP_RT_FLAG_MASK;
286 
287 	nh->nh_priv->rt_flags = rt_flags;
288 	nh_priv->nh_family = info->rti_info[RTAX_DST]->sa_family;
289 	nh_priv->nh_type = 0; // hook responsibility to set nhop type
290 
291 	nh->nh_flags = convert_rt_to_nh_flags(rt_flags);
292 	set_nhop_mtu_from_info(nh, info);
293 	if ((error = set_nhop_gw_from_info(nh, info)) != 0)
294 		return (error);
295 
296 	nh->nh_ifp = info->rti_ifa->ifa_ifp;
297 	nh->nh_ifa = info->rti_ifa;
298 	/* depends on the gateway */
299 	nh->nh_aifp = get_aifp(nh, 0);
300 
301 	/*
302 	 * Note some of the remaining data is set by the
303 	 * per-address-family pre-add hook.
304 	 */
305 
306 	return (0);
307 }
308 
309 /*
310  * Creates a new nexthop based on the information in @info.
311  *
312  * Returns:
313  * 0 on success, filling @nh_ret with the desired nexthop object ptr
314  * errno otherwise
315  */
316 int
317 nhop_create_from_info(struct rib_head *rnh, struct rt_addrinfo *info,
318     struct nhop_object **nh_ret)
319 {
320 	struct nhop_priv *nh_priv;
321 	int error;
322 
323 	NET_EPOCH_ASSERT();
324 
325 	if (info->rti_info[RTAX_GATEWAY] == NULL)
326 		return (EINVAL);
327 
328 	nh_priv = alloc_nhop_structure();
329 
330 	error = fill_nhop_from_info(nh_priv, info);
331 	if (error != 0) {
332 		uma_zfree(nhops_zone, nh_priv->nh);
333 		return (error);
334 	}
335 
336 	error = get_nhop(rnh, info, &nh_priv);
337 	if (error == 0)
338 		*nh_ret = nh_priv->nh;
339 
340 	return (error);
341 }
342 
343 /*
344  * Gets linked nhop using the provided @pnh_priv nexhop data.
345  * If linked nhop is found, returns it, freeing the provided one.
346  * If there is no such nexthop, attaches the remaining data to the
347  *  provided nexthop and links it.
348  *
349  * Returns 0 on success, storing referenced nexthop in @pnh_priv.
350  * Otherwise, errno is returned.
351  */
352 static int
353 get_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
354     struct nhop_priv **pnh_priv)
355 {
356 	const struct sockaddr *dst, *gateway, *netmask;
357 	struct nhop_priv *nh_priv, *tmp_priv;
358 	int error;
359 
360 	nh_priv = *pnh_priv;
361 
362 	/* Give the protocols chance to augment the request data */
363 	dst = info->rti_info[RTAX_DST];
364 	netmask = info->rti_info[RTAX_NETMASK];
365 	gateway = info->rti_info[RTAX_GATEWAY];
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 sockaddr *info_gw;
412 	int error;
413 
414 	/* Update MTU if set in the request*/
415 	set_nhop_mtu_from_info(nh, info);
416 
417 	/* XXX: allow only one of BLACKHOLE,REJECT,GATEWAY */
418 
419 	/* Allow some flags (FLAG1,STATIC,BLACKHOLE,REJECT) to be toggled on change. */
420 	nh->nh_priv->rt_flags &= ~RTF_FMASK;
421 	nh->nh_priv->rt_flags |= info->rti_flags & RTF_FMASK;
422 
423 	/* Consider gateway change */
424 	info_gw = info->rti_info[RTAX_GATEWAY];
425 	if (info_gw != NULL) {
426 		error = set_nhop_gw_from_info(nh, info);
427 		if (error != 0)
428 			return (error);
429 		/* Update RTF_GATEWAY flag status */
430 		nh->nh_priv->rt_flags &= ~RTF_GATEWAY;
431 		nh->nh_priv->rt_flags |= (RTF_GATEWAY & info->rti_flags);
432 	}
433 	/* Update datapath flags */
434 	nh->nh_flags = convert_rt_to_nh_flags(nh->nh_priv->rt_flags);
435 
436 	if (info->rti_ifa != NULL)
437 		nh->nh_ifa = info->rti_ifa;
438 	if (info->rti_ifp != NULL)
439 		nh->nh_ifp = info->rti_ifp;
440 	nh->nh_aifp = get_aifp(nh, 0);
441 
442 	return (0);
443 }
444 
445 /*
446  * Creates new nexthop based on @nh_orig and augmentation data from @info.
447  * Helper function used in the route changes, please see
448  *   alter_nhop_from_info() comments for more details.
449  *
450  * Returns:
451  * 0 on success, filling @nh_ret with the desired nexthop object
452  * errno otherwise
453  */
454 int
455 nhop_create_from_nhop(struct rib_head *rnh, const struct nhop_object *nh_orig,
456     struct rt_addrinfo *info, struct nhop_object **pnh)
457 {
458 	struct nhop_priv *nh_priv;
459 	struct nhop_object *nh;
460 	int error;
461 
462 	NET_EPOCH_ASSERT();
463 
464 	nh_priv = alloc_nhop_structure();
465 	nh = nh_priv->nh;
466 
467 	/* Start with copying data from original nexthop */
468 	nh_priv->nh_family = nh_orig->nh_priv->nh_family;
469 	nh_priv->rt_flags = nh_orig->nh_priv->rt_flags;
470 	nh_priv->nh_type = nh_orig->nh_priv->nh_type;
471 
472 	nh->nh_ifp = nh_orig->nh_ifp;
473 	nh->nh_ifa = nh_orig->nh_ifa;
474 	nh->nh_aifp = nh_orig->nh_aifp;
475 	nh->nh_mtu = nh_orig->nh_mtu;
476 	nh->nh_flags = nh_orig->nh_flags;
477 	memcpy(&nh->gw_sa, &nh_orig->gw_sa, nh_orig->gw_sa.sa_len);
478 
479 	error = alter_nhop_from_info(nh, info);
480 	if (error != 0) {
481 		uma_zfree(nhops_zone, nh_priv->nh);
482 		return (error);
483 	}
484 
485 	error = get_nhop(rnh, info, &nh_priv);
486 	if (error == 0)
487 		*pnh = nh_priv->nh;
488 
489 	return (error);
490 }
491 
492 /*
493  * Allocates memory for public/private nexthop structures.
494  *
495  * Returns pointer to nhop_priv or NULL.
496  */
497 static struct nhop_priv *
498 alloc_nhop_structure()
499 {
500 	struct nhop_object *nh;
501 	struct nhop_priv *nh_priv;
502 
503 	nh = (struct nhop_object *)uma_zalloc(nhops_zone, M_NOWAIT | M_ZERO);
504 	if (nh == NULL)
505 		return (NULL);
506 	nh_priv = (struct nhop_priv *)((char *)nh + NHOP_OBJECT_ALIGNED_SIZE);
507 
508 	nh->nh_priv = nh_priv;
509 	nh_priv->nh = nh;
510 
511 	return (nh_priv);
512 }
513 
514 /*
515  * Alocates/references the remaining bits of nexthop data and links
516  *  it to the hash table.
517  * Returns 0 if successful,
518  *  errno otherwise. @nh_priv is freed in case of error.
519  */
520 static int
521 finalize_nhop(struct nh_control *ctl, struct rt_addrinfo *info,
522     struct nhop_priv *nh_priv)
523 {
524 	struct nhop_object *nh;
525 
526 	nh = nh_priv->nh;
527 
528 	/* Allocate per-cpu packet counter */
529 	nh->nh_pksent = counter_u64_alloc(M_NOWAIT);
530 	if (nh->nh_pksent == NULL) {
531 		uma_zfree(nhops_zone, nh);
532 		RTSTAT_INC(rts_nh_alloc_failure);
533 		DPRINTF("nh_alloc_finalize failed");
534 		return (ENOMEM);
535 	}
536 
537 	/* Save vnet to ease destruction */
538 	nh_priv->nh_vnet = curvnet;
539 
540 	/* Reference external objects and calculate (referenced) ifa */
541 	if_ref(nh->nh_ifp);
542 	ifa_ref(nh->nh_ifa);
543 	nh->nh_aifp = get_aifp(nh, 1);
544 	DPRINTF("AIFP: %p nh_ifp %p", nh->nh_aifp, nh->nh_ifp);
545 
546 	refcount_init(&nh_priv->nh_refcnt, 1);
547 
548 	/* Please see nhop_free() comments on the initial value */
549 	refcount_init(&nh_priv->nh_linked, 2);
550 
551 	print_nhop("FINALIZE", nh);
552 
553 	if (link_nhop(ctl, nh_priv) == 0) {
554 		/*
555 		 * Adding nexthop to the datastructures
556 		 *  failed. Call destructor w/o waiting for
557 		 *  the epoch end, as nexthop is not used
558 		 *  and return.
559 		 */
560 		DPRINTF("link_nhop failed!");
561 		destroy_nhop(nh_priv);
562 
563 		return (ENOBUFS);
564 	}
565 
566 	return (0);
567 }
568 
569 static void
570 print_nhop_sa(char *buf, size_t buflen, const struct sockaddr *sa)
571 {
572 
573 	if (sa->sa_family == AF_INET) {
574 		const struct sockaddr_in *sin4;
575 		sin4 = (const struct sockaddr_in *)sa;
576 		inet_ntop(AF_INET, &sin4->sin_addr, buf, buflen);
577 	} else if (sa->sa_family == AF_INET6) {
578 		const struct sockaddr_in6 *sin6;
579 		sin6 = (const struct sockaddr_in6 *)sa;
580 		inet_ntop(AF_INET6, &sin6->sin6_addr, buf, buflen);
581 	} else if (sa->sa_family == AF_LINK) {
582 		const struct sockaddr_dl *sdl;
583 		sdl = (const struct sockaddr_dl *)sa;
584 		snprintf(buf, buflen, "if#%d", sdl->sdl_index);
585 	} else
586 		snprintf(buf, buflen, "af:%d", sa->sa_family);
587 }
588 
589 static void
590 print_nhop(const char *prefix, const struct nhop_object *nh)
591 {
592 	char src_buf[INET6_ADDRSTRLEN], addr_buf[INET6_ADDRSTRLEN];
593 
594 	print_nhop_sa(src_buf, sizeof(src_buf), nh->nh_ifa->ifa_addr);
595 	print_nhop_sa(addr_buf, sizeof(addr_buf), &nh->gw_sa);
596 
597 	DPRINTF("%s nhop priv %p: AF %d ifp %p %s addr %s src %p %s aifp %p %s mtu %d nh_flags %X",
598 	    prefix, nh->nh_priv, nh->nh_priv->nh_family, nh->nh_ifp,
599 	    if_name(nh->nh_ifp), addr_buf, nh->nh_ifa, src_buf, nh->nh_aifp,
600 	    if_name(nh->nh_aifp), nh->nh_mtu, nh->nh_flags);
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 	print_nhop("DEL", nh);
611 
612 	if_rele(nh->nh_ifp);
613 	if_rele(nh->nh_aifp);
614 	ifa_free(nh->nh_ifa);
615 	counter_u64_free(nh->nh_pksent);
616 
617 	uma_zfree(nhops_zone, nh);
618 }
619 
620 /*
621  * Epoch callback indicating nhop is safe to destroy
622  */
623 static void
624 destroy_nhop_epoch(epoch_context_t ctx)
625 {
626 	struct nhop_priv *nh_priv;
627 
628 	nh_priv = __containerof(ctx, struct nhop_priv, nh_epoch_ctx);
629 
630 	destroy_nhop(nh_priv);
631 }
632 
633 void
634 nhop_ref_object(struct nhop_object *nh)
635 {
636 	u_int old;
637 
638 	old = refcount_acquire(&nh->nh_priv->nh_refcnt);
639 	KASSERT(old > 0, ("%s: nhop object %p has 0 refs", __func__, nh));
640 }
641 
642 int
643 nhop_try_ref_object(struct nhop_object *nh)
644 {
645 
646 	return (refcount_acquire_if_not_zero(&nh->nh_priv->nh_refcnt));
647 }
648 
649 void
650 nhop_free(struct nhop_object *nh)
651 {
652 	struct nh_control *ctl;
653 	struct nhop_priv *nh_priv = nh->nh_priv;
654 	struct epoch_tracker et;
655 
656 	if (!refcount_release(&nh_priv->nh_refcnt))
657 		return;
658 
659 	/*
660 	 * There are only 2 places, where nh_linked can be decreased:
661 	 *  rib destroy (nhops_destroy_rib) and this function.
662 	 * nh_link can never be increased.
663 	 *
664 	 * Hence, use initial value of 2 to make use of
665 	 *  refcount_release_if_not_last().
666 	 *
667 	 * There can be two scenarious when calling this function:
668 	 *
669 	 * 1) nh_linked value is 2. This means that either
670 	 *  nhops_destroy_rib() has not been called OR it is running,
671 	 *  but we are guaranteed that nh_control won't be freed in
672 	 *  this epoch. Hence, nexthop can be safely unlinked.
673 	 *
674 	 * 2) nh_linked value is 1. In that case, nhops_destroy_rib()
675 	 *  has been called and nhop unlink can be skipped.
676 	 */
677 
678 	NET_EPOCH_ENTER(et);
679 	if (refcount_release_if_not_last(&nh_priv->nh_linked)) {
680 		ctl = nh_priv->nh_control;
681 		if (unlink_nhop(ctl, nh_priv) == NULL) {
682 			/* Do not try to reclaim */
683 			DPRINTF("Failed to unlink nexhop %p", nh_priv);
684 			NET_EPOCH_EXIT(et);
685 			return;
686 		}
687 	}
688 	NET_EPOCH_EXIT(et);
689 
690 	epoch_call(net_epoch_preempt, destroy_nhop_epoch,
691 	    &nh_priv->nh_epoch_ctx);
692 }
693 
694 void
695 nhop_free_any(struct nhop_object *nh)
696 {
697 
698 	nhop_free(nh);
699 }
700 
701 /* Helper functions */
702 
703 uint32_t
704 nhop_get_idx(const struct nhop_object *nh)
705 {
706 
707 	return (nh->nh_priv->nh_idx);
708 }
709 
710 enum nhop_type
711 nhop_get_type(const struct nhop_object *nh)
712 {
713 
714 	return (nh->nh_priv->nh_type);
715 }
716 
717 void
718 nhop_set_type(struct nhop_object *nh, enum nhop_type nh_type)
719 {
720 
721 	nh->nh_priv->nh_type = nh_type;
722 }
723 
724 int
725 nhop_get_rtflags(const struct nhop_object *nh)
726 {
727 
728 	return (nh->nh_priv->rt_flags);
729 }
730 
731 void
732 nhop_set_rtflags(struct nhop_object *nh, int rt_flags)
733 {
734 
735 	nh->nh_priv->rt_flags = rt_flags;
736 }
737 
738 struct vnet *
739 nhop_get_vnet(const struct nhop_object *nh)
740 {
741 
742 	return (nh->nh_priv->nh_vnet);
743 }
744 
745 void
746 nhops_update_ifmtu(struct rib_head *rh, struct ifnet *ifp, uint32_t mtu)
747 {
748 	struct nh_control *ctl;
749 	struct nhop_priv *nh_priv;
750 	struct nhop_object *nh;
751 
752 	ctl = rh->nh_control;
753 
754 	NHOPS_WLOCK(ctl);
755 	CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) {
756 		nh = nh_priv->nh;
757 		if (nh->nh_ifp == ifp) {
758 			if ((nh_priv->rt_flags & RTF_FIXEDMTU) == 0 ||
759 			    nh->nh_mtu > mtu) {
760 				/* Update MTU directly */
761 				nh->nh_mtu = mtu;
762 			}
763 		}
764 	} CHT_SLIST_FOREACH_END;
765 	NHOPS_WUNLOCK(ctl);
766 
767 }
768 
769 /*
770  * Dumps a single entry to sysctl buffer.
771  *
772  * Layout:
773  *  rt_msghdr - generic RTM header to allow users to skip non-understood messages
774  *  nhop_external - nexhop description structure (with length)
775  *  nhop_addrs - structure encapsulating GW/SRC sockaddrs
776  */
777 static int
778 dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w)
779 {
780 	struct {
781 		struct rt_msghdr	rtm;
782 		struct nhop_external	nhe;
783 		struct nhop_addrs	na;
784 	} arpc;
785 	struct nhop_external *pnhe;
786 	struct sockaddr *gw_sa, *src_sa;
787 	struct sockaddr_storage ss;
788 	size_t addrs_len;
789 	int error;
790 
791 	//DPRINTF("Dumping: head %p nh %p flags %X req %p\n", rh, nh, nh->nh_flags, w);
792 
793 	memset(&arpc, 0, sizeof(arpc));
794 
795 	arpc.rtm.rtm_msglen = sizeof(arpc);
796 	arpc.rtm.rtm_version = RTM_VERSION;
797 	arpc.rtm.rtm_type = RTM_GET;
798 	//arpc.rtm.rtm_flags = RTF_UP;
799 	arpc.rtm.rtm_flags = nh->nh_priv->rt_flags;
800 
801 	/* nhop_external */
802 	pnhe = &arpc.nhe;
803 	pnhe->nh_len = sizeof(struct nhop_external);
804 	pnhe->nh_idx = nh->nh_priv->nh_idx;
805 	pnhe->nh_fib = rh->rib_fibnum;
806 	pnhe->ifindex = nh->nh_ifp->if_index;
807 	pnhe->aifindex = nh->nh_aifp->if_index;
808 	pnhe->nh_family = nh->nh_priv->nh_family;
809 	pnhe->nh_type = nh->nh_priv->nh_type;
810 	pnhe->nh_mtu = nh->nh_mtu;
811 	pnhe->nh_flags = nh->nh_flags;
812 
813 	memcpy(pnhe->nh_prepend, nh->nh_prepend, sizeof(nh->nh_prepend));
814 	pnhe->prepend_len = nh->nh_prepend_len;
815 	pnhe->nh_refcount = nh->nh_priv->nh_refcnt;
816 	pnhe->nh_pksent = counter_u64_fetch(nh->nh_pksent);
817 
818 	/* sockaddr container */
819 	addrs_len = sizeof(struct nhop_addrs);
820 	arpc.na.gw_sa_off = addrs_len;
821 	gw_sa = (struct sockaddr *)&nh->gw4_sa;
822 	addrs_len += gw_sa->sa_len;
823 
824 	src_sa = nh->nh_ifa->ifa_addr;
825 	if (src_sa->sa_family == AF_LINK) {
826 		/* Shorten structure */
827 		memset(&ss, 0, sizeof(struct sockaddr_storage));
828 		fill_sdl_from_ifp((struct sockaddr_dl_short *)&ss,
829 		    nh->nh_ifa->ifa_ifp);
830 		src_sa = (struct sockaddr *)&ss;
831 	}
832 	arpc.na.src_sa_off = addrs_len;
833 	addrs_len += src_sa->sa_len;
834 
835 	/* Write total container length */
836 	arpc.na.na_len = addrs_len;
837 
838 	arpc.rtm.rtm_msglen += arpc.na.na_len - sizeof(struct nhop_addrs);
839 
840 	error = SYSCTL_OUT(w, &arpc, sizeof(arpc));
841 	if (error == 0)
842 		error = SYSCTL_OUT(w, gw_sa, gw_sa->sa_len);
843 	if (error == 0)
844 		error = SYSCTL_OUT(w, src_sa, src_sa->sa_len);
845 
846 	return (error);
847 }
848 
849 int
850 nhops_dump_sysctl(struct rib_head *rh, struct sysctl_req *w)
851 {
852 	struct nh_control *ctl;
853 	struct nhop_priv *nh_priv;
854 	int error;
855 
856 	ctl = rh->nh_control;
857 
858 	NHOPS_RLOCK(ctl);
859 	DPRINTF("NHDUMP: count=%u", ctl->nh_head.items_count);
860 	CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) {
861 		error = dump_nhop_entry(rh, nh_priv->nh, w);
862 		if (error != 0) {
863 			NHOPS_RUNLOCK(ctl);
864 			return (error);
865 		}
866 	} CHT_SLIST_FOREACH_END;
867 	NHOPS_RUNLOCK(ctl);
868 
869 	return (0);
870 }
871