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
nhops_init(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 *
get_aifp(const struct nhop_object * nh)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
cmp_priv(const struct nhop_priv * _one,const struct nhop_priv * _two)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
set_nhop_mtu_from_info(struct nhop_object * nh,const struct rt_addrinfo * info)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
fill_sdl_from_ifp(struct sockaddr_dl_short * sdl,const struct ifnet * ifp)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
set_nhop_gw_from_info(struct nhop_object * nh,struct rt_addrinfo * info)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
convert_rt_to_nh_flags(int rt_flags)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
fill_nhop_from_info(struct nhop_priv * nh_priv,struct rt_addrinfo * info)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
nhop_create_from_info(struct rib_head * rnh,struct rt_addrinfo * info,struct nhop_object ** nh_ret)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
get_nhop(struct rib_head * rnh,struct rt_addrinfo * info,struct nhop_priv ** pnh_priv)354 get_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
355 struct nhop_priv **pnh_priv)
356 {
357 const struct sockaddr *dst, *gateway, *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 gateway = info->rti_info[RTAX_GATEWAY];
367
368 error = rnh->rnh_preadd(rnh->rib_fibnum, dst, netmask, nh_priv->nh);
369 if (error != 0) {
370 uma_zfree(nhops_zone, nh_priv->nh);
371 return (error);
372 }
373
374 tmp_priv = find_nhop(rnh->nh_control, nh_priv);
375 if (tmp_priv != NULL) {
376 uma_zfree(nhops_zone, nh_priv->nh);
377 *pnh_priv = tmp_priv;
378 return (0);
379 }
380
381 /*
382 * Existing nexthop not found, need to create new one.
383 * Note: multiple simultaneous get_nhop() requests
384 * can result in multiple equal nexhops existing in the
385 * nexthop table. This is not a not a problem until the
386 * relative number of such nexthops is significant, which
387 * is extremely unlikely.
388 */
389
390 error = finalize_nhop(rnh->nh_control, info, nh_priv);
391 if (error != 0)
392 return (error);
393
394 return (0);
395 }
396
397 /*
398 * Update @nh with data supplied in @info.
399 * This is a helper function to support route changes.
400 *
401 * It limits the changes that can be done to the route to the following:
402 * 1) all combination of gateway changes (gw, interface, blackhole/reject)
403 * 2) route flags (FLAG[123],STATIC,BLACKHOLE,REJECT)
404 * 3) route MTU
405 *
406 * Returns:
407 * 0 on success
408 */
409 static int
alter_nhop_from_info(struct nhop_object * nh,struct rt_addrinfo * info)410 alter_nhop_from_info(struct nhop_object *nh, struct rt_addrinfo *info)
411 {
412 struct nhop_priv *nh_priv = nh->nh_priv;
413 struct sockaddr *info_gw;
414 int error;
415
416 /* Update MTU if set in the request*/
417 set_nhop_mtu_from_info(nh, info);
418
419 /* XXX: allow only one of BLACKHOLE,REJECT,GATEWAY */
420
421 /* Allow some flags (FLAG1,STATIC,BLACKHOLE,REJECT) to be toggled on change. */
422 nh_priv->rt_flags &= ~RTF_FMASK;
423 nh_priv->rt_flags |= info->rti_flags & RTF_FMASK;
424
425 /* Consider gateway change */
426 info_gw = info->rti_info[RTAX_GATEWAY];
427 if (info_gw != NULL) {
428 error = set_nhop_gw_from_info(nh, info);
429 if (error != 0)
430 return (error);
431 if (nh->gw_sa.sa_family == AF_LINK)
432 nh_priv->nh_neigh_family = nh_priv->nh_upper_family;
433 else
434 nh_priv->nh_neigh_family = nh->gw_sa.sa_family;
435 /* Update RTF_GATEWAY flag status */
436 nh_priv->rt_flags &= ~RTF_GATEWAY;
437 nh_priv->rt_flags |= (RTF_GATEWAY & info->rti_flags);
438 }
439 /* Update datapath flags */
440 nh->nh_flags = convert_rt_to_nh_flags(nh_priv->rt_flags);
441
442 if (info->rti_ifa != NULL)
443 nh->nh_ifa = info->rti_ifa;
444 if (info->rti_ifp != NULL)
445 nh->nh_ifp = info->rti_ifp;
446 nh->nh_aifp = get_aifp(nh);
447
448 return (0);
449 }
450
451 /*
452 * Creates new nexthop based on @nh_orig and augmentation data from @info.
453 * Helper function used in the route changes, please see
454 * alter_nhop_from_info() comments for more details.
455 *
456 * Returns:
457 * 0 on success, filling @nh_ret with the desired nexthop object
458 * errno otherwise
459 */
460 int
nhop_create_from_nhop(struct rib_head * rnh,const struct nhop_object * nh_orig,struct rt_addrinfo * info,struct nhop_object ** pnh)461 nhop_create_from_nhop(struct rib_head *rnh, const struct nhop_object *nh_orig,
462 struct rt_addrinfo *info, struct nhop_object **pnh)
463 {
464 struct nhop_priv *nh_priv;
465 struct nhop_object *nh;
466 int error;
467
468 NET_EPOCH_ASSERT();
469
470 nh_priv = alloc_nhop_structure();
471 nh = nh_priv->nh;
472
473 /* Start with copying data from original nexthop */
474 nh_priv->nh_upper_family = nh_orig->nh_priv->nh_upper_family;
475 nh_priv->nh_neigh_family = nh_orig->nh_priv->nh_neigh_family;
476 nh_priv->rt_flags = nh_orig->nh_priv->rt_flags;
477 nh_priv->nh_type = nh_orig->nh_priv->nh_type;
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 *
alloc_nhop_structure()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
reference_nhop_deps(struct nhop_object * nh)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
finalize_nhop(struct nh_control * ctl,struct rt_addrinfo * info,struct nhop_priv * nh_priv)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[48];
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[48];
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
destroy_nhop(struct nhop_priv * nh_priv)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
destroy_nhop_epoch(epoch_context_t ctx)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
nhop_ref_object(struct nhop_object * nh)632 nhop_ref_object(struct nhop_object *nh)
633 {
634 u_int old;
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
nhop_try_ref_object(struct nhop_object * nh)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
nhop_free(struct nhop_object * nh)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[48];
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[48];
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
nhop_ref_any(struct nhop_object * nh)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
nhop_free_any(struct nhop_object * nh)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
nhop_get_idx(const struct nhop_object * nh)729 nhop_get_idx(const struct nhop_object *nh)
730 {
731
732 return (nh->nh_priv->nh_idx);
733 }
734
735 enum nhop_type
nhop_get_type(const struct nhop_object * nh)736 nhop_get_type(const struct nhop_object *nh)
737 {
738
739 return (nh->nh_priv->nh_type);
740 }
741
742 void
nhop_set_type(struct nhop_object * nh,enum nhop_type nh_type)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
nhop_get_rtflags(const struct nhop_object * nh)750 nhop_get_rtflags(const struct nhop_object *nh)
751 {
752
753 return (nh->nh_priv->rt_flags);
754 }
755
756 void
nhop_set_rtflags(struct nhop_object * nh,int rt_flags)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 *
nhop_get_vnet(const struct nhop_object * nh)764 nhop_get_vnet(const struct nhop_object *nh)
765 {
766
767 return (nh->nh_priv->nh_vnet);
768 }
769
770 struct nhop_object *
nhop_select_func(struct nhop_object * nh,uint32_t flowid)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
nhop_get_upper_family(const struct nhop_object * nh)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
nhop_get_neigh_family(const struct nhop_object * nh)791 nhop_get_neigh_family(const struct nhop_object *nh)
792 {
793 return (nh->nh_priv->nh_neigh_family);
794 }
795
796 uint32_t
nhop_get_fibnum(const struct nhop_object * nh)797 nhop_get_fibnum(const struct nhop_object *nh)
798 {
799 return (nh->nh_priv->nh_fibnum);
800 }
801
802 void
nhops_update_ifmtu(struct rib_head * rh,struct ifnet * ifp,uint32_t mtu)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 *
nhop_print_buf(const struct nhop_object * nh,char * buf,size_t bufsize)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 /*
868 * Dumps a single entry to sysctl buffer.
869 *
870 * Layout:
871 * rt_msghdr - generic RTM header to allow users to skip non-understood messages
872 * nhop_external - nexhop description structure (with length)
873 * nhop_addrs - structure encapsulating GW/SRC sockaddrs
874 */
875 static int
dump_nhop_entry(struct rib_head * rh,struct nhop_object * nh,struct sysctl_req * w)876 dump_nhop_entry(struct rib_head *rh, struct nhop_object *nh, struct sysctl_req *w)
877 {
878 struct {
879 struct rt_msghdr rtm;
880 struct nhop_external nhe;
881 struct nhop_addrs na;
882 } arpc;
883 struct nhop_external *pnhe;
884 struct sockaddr *gw_sa, *src_sa;
885 struct sockaddr_storage ss;
886 size_t addrs_len;
887 int error;
888
889 memset(&arpc, 0, sizeof(arpc));
890
891 arpc.rtm.rtm_msglen = sizeof(arpc);
892 arpc.rtm.rtm_version = RTM_VERSION;
893 arpc.rtm.rtm_type = RTM_GET;
894 //arpc.rtm.rtm_flags = RTF_UP;
895 arpc.rtm.rtm_flags = nh->nh_priv->rt_flags;
896
897 /* nhop_external */
898 pnhe = &arpc.nhe;
899 pnhe->nh_len = sizeof(struct nhop_external);
900 pnhe->nh_idx = nh->nh_priv->nh_idx;
901 pnhe->nh_fib = rh->rib_fibnum;
902 pnhe->ifindex = nh->nh_ifp->if_index;
903 pnhe->aifindex = nh->nh_aifp->if_index;
904 pnhe->nh_family = nh->nh_priv->nh_upper_family;
905 pnhe->nh_type = nh->nh_priv->nh_type;
906 pnhe->nh_mtu = nh->nh_mtu;
907 pnhe->nh_flags = nh->nh_flags;
908
909 memcpy(pnhe->nh_prepend, nh->nh_prepend, sizeof(nh->nh_prepend));
910 pnhe->prepend_len = nh->nh_prepend_len;
911 pnhe->nh_refcount = nh->nh_priv->nh_refcnt;
912 pnhe->nh_pksent = counter_u64_fetch(nh->nh_pksent);
913
914 /* sockaddr container */
915 addrs_len = sizeof(struct nhop_addrs);
916 arpc.na.gw_sa_off = addrs_len;
917 gw_sa = (struct sockaddr *)&nh->gw4_sa;
918 addrs_len += gw_sa->sa_len;
919
920 src_sa = nh->nh_ifa->ifa_addr;
921 if (src_sa->sa_family == AF_LINK) {
922 /* Shorten structure */
923 memset(&ss, 0, sizeof(struct sockaddr_storage));
924 fill_sdl_from_ifp((struct sockaddr_dl_short *)&ss,
925 nh->nh_ifa->ifa_ifp);
926 src_sa = (struct sockaddr *)&ss;
927 }
928 arpc.na.src_sa_off = addrs_len;
929 addrs_len += src_sa->sa_len;
930
931 /* Write total container length */
932 arpc.na.na_len = addrs_len;
933
934 arpc.rtm.rtm_msglen += arpc.na.na_len - sizeof(struct nhop_addrs);
935
936 error = SYSCTL_OUT(w, &arpc, sizeof(arpc));
937 if (error == 0)
938 error = SYSCTL_OUT(w, gw_sa, gw_sa->sa_len);
939 if (error == 0)
940 error = SYSCTL_OUT(w, src_sa, src_sa->sa_len);
941
942 return (error);
943 }
944
945 uint32_t
nhops_get_count(struct rib_head * rh)946 nhops_get_count(struct rib_head *rh)
947 {
948 struct nh_control *ctl;
949 uint32_t count;
950
951 ctl = rh->nh_control;
952
953 NHOPS_RLOCK(ctl);
954 count = ctl->nh_head.items_count;
955 NHOPS_RUNLOCK(ctl);
956
957 return (count);
958 }
959
960 int
nhops_dump_sysctl(struct rib_head * rh,struct sysctl_req * w)961 nhops_dump_sysctl(struct rib_head *rh, struct sysctl_req *w)
962 {
963 struct nh_control *ctl;
964 struct nhop_priv *nh_priv;
965 int error;
966
967 ctl = rh->nh_control;
968
969 NHOPS_RLOCK(ctl);
970 #if DEBUG_MAX_LEVEL >= LOG_DEBUG
971 FIB_LOG(LOG_DEBUG, rh->rib_fibnum, rh->rib_family, "dump %u items",
972 ctl->nh_head.items_count);
973 #endif
974 CHT_SLIST_FOREACH(&ctl->nh_head, nhops, nh_priv) {
975 error = dump_nhop_entry(rh, nh_priv->nh, w);
976 if (error != 0) {
977 NHOPS_RUNLOCK(ctl);
978 return (error);
979 }
980 } CHT_SLIST_FOREACH_END;
981 NHOPS_RUNLOCK(ctl);
982
983 return (0);
984 }
985