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/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/syslog.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/rmlock.h>
44
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_dl.h>
48 #include <net/vnet.h>
49 #include <net/route.h>
50 #include <net/route/route_ctl.h>
51 #include <net/route/route_var.h>
52 #include <net/route/nhop_utils.h>
53 #include <net/route/nhop.h>
54 #include <net/route/nhop_var.h>
55 #include <netinet/in.h>
56 #include <netinet6/scope6_var.h>
57
58 #include <vm/uma.h>
59
60 /*
61 * This file contains control plane routing tables functions.
62 *
63 * All functions assumes they are called in net epoch.
64 */
65
66 struct rib_subscription {
67 CK_STAILQ_ENTRY(rib_subscription) next;
68 rib_subscription_cb_t *func;
69 void *arg;
70 struct rib_head *rnh;
71 enum rib_subscription_type type;
72 struct epoch_context epoch_ctx;
73 };
74
75 static int add_route(struct rib_head *rnh, struct rt_addrinfo *info,
76 struct rib_cmd_info *rc);
77 static int add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
78 struct rt_addrinfo *info, struct route_nhop_data *rnd,
79 struct rib_cmd_info *rc);
80 static int del_route(struct rib_head *rnh, struct rt_addrinfo *info,
81 struct rib_cmd_info *rc);
82 static int change_route(struct rib_head *rnh, struct rt_addrinfo *info,
83 struct route_nhop_data *nhd_orig, struct rib_cmd_info *rc);
84
85 static int rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info,
86 struct rib_cmd_info *rc);
87
88 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
89 struct rib_cmd_info *rc);
90
91 static void destroy_subscription_epoch(epoch_context_t ctx);
92 #ifdef ROUTE_MPATH
93 static bool rib_can_multipath(struct rib_head *rh);
94 #endif
95
96 /* Per-vnet multipath routing configuration */
97 SYSCTL_DECL(_net_route);
98 #define V_rib_route_multipath VNET(rib_route_multipath)
99 #ifdef ROUTE_MPATH
100 #define _MP_FLAGS CTLFLAG_RW
101 #else
102 #define _MP_FLAGS CTLFLAG_RD
103 #endif
104 VNET_DEFINE(u_int, rib_route_multipath) = 1;
105 SYSCTL_UINT(_net_route, OID_AUTO, multipath, _MP_FLAGS | CTLFLAG_VNET,
106 &VNET_NAME(rib_route_multipath), 0, "Enable route multipath");
107 #undef _MP_FLAGS
108
109 /* Routing table UMA zone */
110 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
111 #define V_rtzone VNET(rtzone)
112
113 void
vnet_rtzone_init()114 vnet_rtzone_init()
115 {
116
117 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
118 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
119 }
120
121 #ifdef VIMAGE
122 void
vnet_rtzone_destroy()123 vnet_rtzone_destroy()
124 {
125
126 uma_zdestroy(V_rtzone);
127 }
128 #endif
129
130 static void
destroy_rtentry(struct rtentry * rt)131 destroy_rtentry(struct rtentry *rt)
132 {
133 #ifdef VIMAGE
134 struct nhop_object *nh = rt->rt_nhop;
135
136 /*
137 * At this moment rnh, nh_control may be already freed.
138 * nhop interface may have been migrated to a different vnet.
139 * Use vnet stored in the nexthop to delete the entry.
140 */
141 #ifdef ROUTE_MPATH
142 if (NH_IS_NHGRP(nh)) {
143 struct weightened_nhop *wn;
144 uint32_t num_nhops;
145 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
146 nh = wn[0].nh;
147 }
148 #endif
149 CURVNET_SET(nhop_get_vnet(nh));
150 #endif
151
152 /* Unreference nexthop */
153 nhop_free_any(rt->rt_nhop);
154
155 uma_zfree(V_rtzone, rt);
156
157 CURVNET_RESTORE();
158 }
159
160 /*
161 * Epoch callback indicating rtentry is safe to destroy
162 */
163 static void
destroy_rtentry_epoch(epoch_context_t ctx)164 destroy_rtentry_epoch(epoch_context_t ctx)
165 {
166 struct rtentry *rt;
167
168 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
169
170 destroy_rtentry(rt);
171 }
172
173 /*
174 * Schedule rtentry deletion
175 */
176 static void
rtfree(struct rtentry * rt)177 rtfree(struct rtentry *rt)
178 {
179
180 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
181
182 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
183 &rt->rt_epoch_ctx);
184 }
185
186 static struct rib_head *
get_rnh(uint32_t fibnum,const struct rt_addrinfo * info)187 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
188 {
189 struct rib_head *rnh;
190 struct sockaddr *dst;
191
192 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
193
194 dst = info->rti_info[RTAX_DST];
195 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
196
197 return (rnh);
198 }
199
200 #ifdef ROUTE_MPATH
201 static bool
rib_can_multipath(struct rib_head * rh)202 rib_can_multipath(struct rib_head *rh)
203 {
204 int result;
205
206 CURVNET_SET(rh->rib_vnet);
207 result = !!V_rib_route_multipath;
208 CURVNET_RESTORE();
209
210 return (result);
211 }
212
213 /*
214 * Check is nhop is multipath-eligible.
215 * Avoid nhops without gateways and redirects.
216 *
217 * Returns 1 for multipath-eligible nexthop,
218 * 0 otherwise.
219 */
220 bool
nhop_can_multipath(const struct nhop_object * nh)221 nhop_can_multipath(const struct nhop_object *nh)
222 {
223
224 if ((nh->nh_flags & NHF_MULTIPATH) != 0)
225 return (1);
226 if ((nh->nh_flags & NHF_GATEWAY) == 0)
227 return (0);
228 if ((nh->nh_flags & NHF_REDIRECT) != 0)
229 return (0);
230
231 return (1);
232 }
233 #endif
234
235 static int
get_info_weight(const struct rt_addrinfo * info,uint32_t default_weight)236 get_info_weight(const struct rt_addrinfo *info, uint32_t default_weight)
237 {
238 uint32_t weight;
239
240 if (info->rti_mflags & RTV_WEIGHT)
241 weight = info->rti_rmx->rmx_weight;
242 else
243 weight = default_weight;
244 /* Keep upper 1 byte for adm distance purposes */
245 if (weight > RT_MAX_WEIGHT)
246 weight = RT_MAX_WEIGHT;
247
248 return (weight);
249 }
250
251 bool
rt_is_host(const struct rtentry * rt)252 rt_is_host(const struct rtentry *rt)
253 {
254
255 return (rt->rte_flags & RTF_HOST);
256 }
257
258 sa_family_t
rt_get_family(const struct rtentry * rt)259 rt_get_family(const struct rtentry *rt)
260 {
261 const struct sockaddr *dst;
262
263 dst = (const struct sockaddr *)rt_key_const(rt);
264
265 return (dst->sa_family);
266 }
267
268 /*
269 * Returns pointer to nexthop or nexthop group
270 * associated with @rt
271 */
272 struct nhop_object *
rt_get_raw_nhop(const struct rtentry * rt)273 rt_get_raw_nhop(const struct rtentry *rt)
274 {
275
276 return (rt->rt_nhop);
277 }
278
279 #ifdef INET
280 /*
281 * Stores IPv4 address and prefix length of @rt inside
282 * @paddr and @plen.
283 * @pscopeid is currently always set to 0.
284 */
285 void
rt_get_inet_prefix_plen(const struct rtentry * rt,struct in_addr * paddr,int * plen,uint32_t * pscopeid)286 rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
287 int *plen, uint32_t *pscopeid)
288 {
289 const struct sockaddr_in *dst;
290
291 dst = (const struct sockaddr_in *)rt_key_const(rt);
292 KASSERT((dst->sin_family == AF_INET),
293 ("rt family is %d, not inet", dst->sin_family));
294 *paddr = dst->sin_addr;
295 dst = (const struct sockaddr_in *)rt_mask_const(rt);
296 if (dst == NULL)
297 *plen = 32;
298 else
299 *plen = bitcount32(dst->sin_addr.s_addr);
300 *pscopeid = 0;
301 }
302
303 /*
304 * Stores IPv4 address and prefix mask of @rt inside
305 * @paddr and @pmask. Sets mask to INADDR_ANY for host routes.
306 * @pscopeid is currently always set to 0.
307 */
308 void
rt_get_inet_prefix_pmask(const struct rtentry * rt,struct in_addr * paddr,struct in_addr * pmask,uint32_t * pscopeid)309 rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
310 struct in_addr *pmask, uint32_t *pscopeid)
311 {
312 const struct sockaddr_in *dst;
313
314 dst = (const struct sockaddr_in *)rt_key_const(rt);
315 KASSERT((dst->sin_family == AF_INET),
316 ("rt family is %d, not inet", dst->sin_family));
317 *paddr = dst->sin_addr;
318 dst = (const struct sockaddr_in *)rt_mask_const(rt);
319 if (dst == NULL)
320 pmask->s_addr = INADDR_BROADCAST;
321 else
322 *pmask = dst->sin_addr;
323 *pscopeid = 0;
324 }
325 #endif
326
327 #ifdef INET6
328 static int
inet6_get_plen(const struct in6_addr * addr)329 inet6_get_plen(const struct in6_addr *addr)
330 {
331
332 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
333 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
334 }
335
336 /*
337 * Stores IPv6 address and prefix length of @rt inside
338 * @paddr and @plen. Addresses are returned in de-embedded form.
339 * Scopeid is set to 0 for non-LL addresses.
340 */
341 void
rt_get_inet6_prefix_plen(const struct rtentry * rt,struct in6_addr * paddr,int * plen,uint32_t * pscopeid)342 rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
343 int *plen, uint32_t *pscopeid)
344 {
345 const struct sockaddr_in6 *dst;
346
347 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
348 KASSERT((dst->sin6_family == AF_INET6),
349 ("rt family is %d, not inet6", dst->sin6_family));
350 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
351 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
352 else
353 *paddr = dst->sin6_addr;
354 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
355 if (dst == NULL)
356 *plen = 128;
357 else
358 *plen = inet6_get_plen(&dst->sin6_addr);
359 }
360
361 /*
362 * Stores IPv6 address and prefix mask of @rt inside
363 * @paddr and @pmask. Addresses are returned in de-embedded form.
364 * Scopeid is set to 0 for non-LL addresses.
365 */
366 void
rt_get_inet6_prefix_pmask(const struct rtentry * rt,struct in6_addr * paddr,struct in6_addr * pmask,uint32_t * pscopeid)367 rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
368 struct in6_addr *pmask, uint32_t *pscopeid)
369 {
370 const struct sockaddr_in6 *dst;
371
372 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
373 KASSERT((dst->sin6_family == AF_INET6),
374 ("rt family is %d, not inet", dst->sin6_family));
375 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
376 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
377 else
378 *paddr = dst->sin6_addr;
379 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
380 if (dst == NULL)
381 memset(pmask, 0xFF, sizeof(struct in6_addr));
382 else
383 *pmask = dst->sin6_addr;
384 }
385 #endif
386
387 static void
rt_set_expire_info(struct rtentry * rt,const struct rt_addrinfo * info)388 rt_set_expire_info(struct rtentry *rt, const struct rt_addrinfo *info)
389 {
390
391 /* Kernel -> userland timebase conversion. */
392 if (info->rti_mflags & RTV_EXPIRE)
393 rt->rt_expire = info->rti_rmx->rmx_expire ?
394 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
395 }
396
397 /*
398 * Check if specified @gw matches gw data in the nexthop @nh.
399 *
400 * Returns true if matches, false otherwise.
401 */
402 bool
match_nhop_gw(const struct nhop_object * nh,const struct sockaddr * gw)403 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
404 {
405
406 if (nh->gw_sa.sa_family != gw->sa_family)
407 return (false);
408
409 switch (gw->sa_family) {
410 case AF_INET:
411 return (nh->gw4_sa.sin_addr.s_addr ==
412 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
413 case AF_INET6:
414 {
415 const struct sockaddr_in6 *gw6;
416 gw6 = (const struct sockaddr_in6 *)gw;
417
418 /*
419 * Currently (2020-09) IPv6 gws in kernel have their
420 * scope embedded. Once this becomes false, this code
421 * has to be revisited.
422 */
423 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
424 &gw6->sin6_addr))
425 return (true);
426 return (false);
427 }
428 case AF_LINK:
429 {
430 const struct sockaddr_dl *sdl;
431 sdl = (const struct sockaddr_dl *)gw;
432 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
433 }
434 default:
435 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
436 }
437
438 /* NOTREACHED */
439 return (false);
440 }
441
442 /*
443 * Checks if data in @info matches nexhop @nh.
444 *
445 * Returns 0 on success,
446 * ESRCH if not matched,
447 * ENOENT if filter function returned false
448 */
449 int
check_info_match_nhop(const struct rt_addrinfo * info,const struct rtentry * rt,const struct nhop_object * nh)450 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
451 const struct nhop_object *nh)
452 {
453 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
454
455 if (info->rti_filter != NULL) {
456 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
457 return (ENOENT);
458 else
459 return (0);
460 }
461 if ((gw != NULL) && !match_nhop_gw(nh, gw))
462 return (ESRCH);
463
464 return (0);
465 }
466
467 /*
468 * Checks if nexhop @nh can be rewritten by data in @info because
469 * of higher "priority". Currently the only case for such scenario
470 * is kernel installing interface routes, marked by RTF_PINNED flag.
471 *
472 * Returns:
473 * 1 if @info data has higher priority
474 * 0 if priority is the same
475 * -1 if priority is lower
476 */
477 int
can_override_nhop(const struct rt_addrinfo * info,const struct nhop_object * nh)478 can_override_nhop(const struct rt_addrinfo *info, const struct nhop_object *nh)
479 {
480
481 if (info->rti_flags & RTF_PINNED) {
482 return (NH_IS_PINNED(nh)) ? 0 : 1;
483 } else {
484 return (NH_IS_PINNED(nh)) ? -1 : 0;
485 }
486 }
487
488 /*
489 * Runs exact prefix match based on @dst and @netmask.
490 * Returns matched @rtentry if found or NULL.
491 * If rtentry was found, saves nexthop / weight value into @rnd.
492 */
493 static struct rtentry *
lookup_prefix_bysa(struct rib_head * rnh,const struct sockaddr * dst,const struct sockaddr * netmask,struct route_nhop_data * rnd)494 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
495 const struct sockaddr *netmask, struct route_nhop_data *rnd)
496 {
497 struct rtentry *rt;
498
499 RIB_LOCK_ASSERT(rnh);
500
501 rt = (struct rtentry *)rnh->rnh_lookup(__DECONST(void *, dst),
502 __DECONST(void *, netmask), &rnh->head);
503 if (rt != NULL) {
504 rnd->rnd_nhop = rt->rt_nhop;
505 rnd->rnd_weight = rt->rt_weight;
506 } else {
507 rnd->rnd_nhop = NULL;
508 rnd->rnd_weight = 0;
509 }
510
511 return (rt);
512 }
513
514 /*
515 * Runs exact prefix match based on dst/netmask from @info.
516 * Assumes RIB lock is held.
517 * Returns matched @rtentry if found or NULL.
518 * If rtentry was found, saves nexthop / weight value into @rnd.
519 */
520 struct rtentry *
lookup_prefix(struct rib_head * rnh,const struct rt_addrinfo * info,struct route_nhop_data * rnd)521 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
522 struct route_nhop_data *rnd)
523 {
524 struct rtentry *rt;
525
526 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
527 info->rti_info[RTAX_NETMASK], rnd);
528
529 return (rt);
530 }
531
532 /*
533 * Adds route defined by @info into the kernel table specified by @fibnum and
534 * sa_family in @info->rti_info[RTAX_DST].
535 *
536 * Returns 0 on success and fills in operation metadata into @rc.
537 */
538 int
rib_add_route(uint32_t fibnum,struct rt_addrinfo * info,struct rib_cmd_info * rc)539 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
540 struct rib_cmd_info *rc)
541 {
542 struct rib_head *rnh;
543 int error;
544
545 NET_EPOCH_ASSERT();
546
547 rnh = get_rnh(fibnum, info);
548 if (rnh == NULL)
549 return (EAFNOSUPPORT);
550
551 /*
552 * Check consistency between RTF_HOST flag and netmask
553 * existence.
554 */
555 if (info->rti_flags & RTF_HOST)
556 info->rti_info[RTAX_NETMASK] = NULL;
557 else if (info->rti_info[RTAX_NETMASK] == NULL)
558 return (EINVAL);
559
560 bzero(rc, sizeof(struct rib_cmd_info));
561 rc->rc_cmd = RTM_ADD;
562
563 error = add_route(rnh, info, rc);
564 if (error == 0)
565 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
566
567 return (error);
568 }
569
570 /*
571 * Creates rtentry and nexthop based on @info data.
572 * Return 0 and fills in rtentry into @prt on success,
573 * return errno otherwise.
574 */
575 static int
create_rtentry(struct rib_head * rnh,struct rt_addrinfo * info,struct rtentry ** prt)576 create_rtentry(struct rib_head *rnh, struct rt_addrinfo *info,
577 struct rtentry **prt)
578 {
579 struct sockaddr *dst, *ndst, *gateway, *netmask;
580 struct rtentry *rt;
581 struct nhop_object *nh;
582 struct ifaddr *ifa;
583 int error, flags;
584
585 dst = info->rti_info[RTAX_DST];
586 gateway = info->rti_info[RTAX_GATEWAY];
587 netmask = info->rti_info[RTAX_NETMASK];
588 flags = info->rti_flags;
589
590 if ((flags & RTF_GATEWAY) && !gateway)
591 return (EINVAL);
592 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
593 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
594 return (EINVAL);
595
596 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
597 return (EINVAL);
598
599 if (info->rti_ifa == NULL) {
600 error = rt_getifa_fib(info, rnh->rib_fibnum);
601 if (error)
602 return (error);
603 } else {
604 ifa_ref(info->rti_ifa);
605 }
606
607 error = nhop_create_from_info(rnh, info, &nh);
608 ifa_free(info->rti_ifa);
609 if (error != 0)
610 return (error);
611
612 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
613 if (rt == NULL) {
614 nhop_free(nh);
615 return (ENOBUFS);
616 }
617 rt->rte_flags = (RTF_UP | flags) & RTE_RT_FLAG_MASK;
618 rt->rt_nhop = nh;
619
620 /* Fill in dst */
621 memcpy(&rt->rt_dst, dst, dst->sa_len);
622 rt_key(rt) = &rt->rt_dst;
623
624 /*
625 * point to the (possibly newly malloc'd) dest address.
626 */
627 ndst = (struct sockaddr *)rt_key(rt);
628
629 /*
630 * make sure it contains the value we want (masked if needed).
631 */
632 if (netmask) {
633 rt_maskedcopy(dst, ndst, netmask);
634 } else
635 bcopy(dst, ndst, dst->sa_len);
636
637 /*
638 * We use the ifa reference returned by rt_getifa_fib().
639 * This moved from below so that rnh->rnh_addaddr() can
640 * examine the ifa and ifa->ifa_ifp if it so desires.
641 */
642 ifa = info->rti_ifa;
643 rt->rt_weight = get_info_weight(info, RT_DEFAULT_WEIGHT);
644 rt_set_expire_info(rt, info);
645
646 *prt = rt;
647 return (0);
648 }
649
650 static int
add_route(struct rib_head * rnh,struct rt_addrinfo * info,struct rib_cmd_info * rc)651 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
652 struct rib_cmd_info *rc)
653 {
654 struct nhop_object *nh_orig;
655 struct route_nhop_data rnd_orig, rnd_add;
656 struct nhop_object *nh;
657 struct rtentry *rt, *rt_orig;
658 int error;
659
660 error = create_rtentry(rnh, info, &rt);
661 if (error != 0)
662 return (error);
663
664 rnd_add.rnd_nhop = rt->rt_nhop;
665 rnd_add.rnd_weight = rt->rt_weight;
666 nh = rt->rt_nhop;
667
668 RIB_WLOCK(rnh);
669 error = add_route_nhop(rnh, rt, info, &rnd_add, rc);
670 if (error == 0) {
671 RIB_WUNLOCK(rnh);
672 return (0);
673 }
674
675 /* addition failed. Lookup prefix in the rib to determine the cause */
676 rt_orig = lookup_prefix(rnh, info, &rnd_orig);
677 if (rt_orig == NULL) {
678 /* No prefix -> rnh_addaddr() failed to allocate memory */
679 RIB_WUNLOCK(rnh);
680 nhop_free(nh);
681 uma_zfree(V_rtzone, rt);
682 return (ENOMEM);
683 }
684
685 /* We have existing route in the RIB. */
686 nh_orig = rnd_orig.rnd_nhop;
687 /* Check if new route has higher preference */
688 if (can_override_nhop(info, nh_orig) > 0) {
689 /* Update nexthop to the new route */
690 change_route_nhop(rnh, rt_orig, info, &rnd_add, rc);
691 RIB_WUNLOCK(rnh);
692 uma_zfree(V_rtzone, rt);
693 nhop_free(nh_orig);
694 return (0);
695 }
696
697 RIB_WUNLOCK(rnh);
698
699 #ifdef ROUTE_MPATH
700 if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) &&
701 nhop_can_multipath(rnd_orig.rnd_nhop))
702 error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc);
703 else
704 #endif
705 /* Unable to add - another route with the same preference exists */
706 error = EEXIST;
707
708 /*
709 * ROUTE_MPATH disabled: failed to add route, free both nhop and rt.
710 * ROUTE_MPATH enabled: original nhop reference is unused in any case,
711 * free rt only if not _adding_ new route to rib (e.g. the case
712 * when initial lookup returned existing route, but then it got
713 * deleted prior to multipath group insertion, leading to a simple
714 * non-multipath add as a result).
715 */
716 nhop_free(nh);
717 if ((error != 0) || rc->rc_cmd != RTM_ADD)
718 uma_zfree(V_rtzone, rt);
719
720 return (error);
721 }
722
723 /*
724 * Removes route defined by @info from the kernel table specified by @fibnum and
725 * sa_family in @info->rti_info[RTAX_DST].
726 *
727 * Returns 0 on success and fills in operation metadata into @rc.
728 */
729 int
rib_del_route(uint32_t fibnum,struct rt_addrinfo * info,struct rib_cmd_info * rc)730 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
731 {
732 struct rib_head *rnh;
733 struct sockaddr *dst_orig, *netmask;
734 struct sockaddr_storage mdst;
735 int error;
736
737 NET_EPOCH_ASSERT();
738
739 rnh = get_rnh(fibnum, info);
740 if (rnh == NULL)
741 return (EAFNOSUPPORT);
742
743 bzero(rc, sizeof(struct rib_cmd_info));
744 rc->rc_cmd = RTM_DELETE;
745
746 dst_orig = info->rti_info[RTAX_DST];
747 netmask = info->rti_info[RTAX_NETMASK];
748
749 if (netmask != NULL) {
750 /* Ensure @dst is always properly masked */
751 if (dst_orig->sa_len > sizeof(mdst))
752 return (EINVAL);
753 rt_maskedcopy(dst_orig, (struct sockaddr *)&mdst, netmask);
754 info->rti_info[RTAX_DST] = (struct sockaddr *)&mdst;
755 }
756 error = del_route(rnh, info, rc);
757 info->rti_info[RTAX_DST] = dst_orig;
758
759 return (error);
760 }
761
762 /*
763 * Conditionally unlinks rtentry matching data inside @info from @rnh.
764 * Returns 0 on success with operation result stored in @rc.
765 * On error, returns:
766 * ESRCH - if prefix was not found,
767 * EADDRINUSE - if trying to delete higher priority route.
768 * ENOENT - if supplied filter function returned 0 (not matched).
769 */
770 static int
rt_unlinkrte(struct rib_head * rnh,struct rt_addrinfo * info,struct rib_cmd_info * rc)771 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, struct rib_cmd_info *rc)
772 {
773 struct rtentry *rt;
774 struct nhop_object *nh;
775 struct radix_node *rn;
776 struct route_nhop_data rnd;
777 int error;
778
779 rt = lookup_prefix(rnh, info, &rnd);
780 if (rt == NULL)
781 return (ESRCH);
782
783 nh = rt->rt_nhop;
784 #ifdef ROUTE_MPATH
785 if (NH_IS_NHGRP(nh)) {
786 error = del_route_mpath(rnh, info, rt,
787 (struct nhgrp_object *)nh, rc);
788 return (error);
789 }
790 #endif
791 error = check_info_match_nhop(info, rt, nh);
792 if (error != 0)
793 return (error);
794
795 if (can_override_nhop(info, nh) < 0)
796 return (EADDRINUSE);
797
798 /*
799 * Remove the item from the tree and return it.
800 * Complain if it is not there and do no more processing.
801 */
802 rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST],
803 info->rti_info[RTAX_NETMASK], &rnh->head);
804 if (rn == NULL)
805 return (ESRCH);
806
807 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
808 panic ("rtrequest delete");
809
810 rt = RNTORT(rn);
811 rt->rte_flags &= ~RTF_UP;
812
813 /* Finalize notification */
814 rnh->rnh_gen++;
815 rnh->rnh_prefixes--;
816
817 rc->rc_cmd = RTM_DELETE;
818 rc->rc_rt = rt;
819 rc->rc_nh_old = rt->rt_nhop;
820 rc->rc_nh_weight = rt->rt_weight;
821 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
822
823 return (0);
824 }
825
826 static int
del_route(struct rib_head * rnh,struct rt_addrinfo * info,struct rib_cmd_info * rc)827 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
828 struct rib_cmd_info *rc)
829 {
830 int error;
831
832 RIB_WLOCK(rnh);
833 error = rt_unlinkrte(rnh, info, rc);
834 RIB_WUNLOCK(rnh);
835 if (error != 0)
836 return (error);
837
838 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
839
840 /*
841 * If the caller wants it, then it can have it,
842 * the entry will be deleted after the end of the current epoch.
843 */
844 if (rc->rc_cmd == RTM_DELETE)
845 rtfree(rc->rc_rt);
846 #ifdef ROUTE_MPATH
847 else {
848 /*
849 * Deleting 1 path may result in RTM_CHANGE to
850 * a different mpath group/nhop.
851 * Free old mpath group.
852 */
853 nhop_free_any(rc->rc_nh_old);
854 }
855 #endif
856
857 return (0);
858 }
859
860 int
rib_change_route(uint32_t fibnum,struct rt_addrinfo * info,struct rib_cmd_info * rc)861 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
862 struct rib_cmd_info *rc)
863 {
864 RIB_RLOCK_TRACKER;
865 struct route_nhop_data rnd_orig;
866 struct rib_head *rnh;
867 struct rtentry *rt;
868 int error;
869
870 NET_EPOCH_ASSERT();
871
872 rnh = get_rnh(fibnum, info);
873 if (rnh == NULL)
874 return (EAFNOSUPPORT);
875
876 bzero(rc, sizeof(struct rib_cmd_info));
877 rc->rc_cmd = RTM_CHANGE;
878
879 /* Check if updated gateway exists */
880 if ((info->rti_flags & RTF_GATEWAY) &&
881 (info->rti_info[RTAX_GATEWAY] == NULL)) {
882
883 /*
884 * route(8) adds RTF_GATEWAY flag if -interface is not set.
885 * Remove RTF_GATEWAY to enforce consistency and maintain
886 * compatibility..
887 */
888 info->rti_flags &= ~RTF_GATEWAY;
889 }
890
891 /*
892 * route change is done in multiple steps, with dropping and
893 * reacquiring lock. In the situations with multiple processes
894 * changes the same route in can lead to the case when route
895 * is changed between the steps. Address it by retrying the operation
896 * multiple times before failing.
897 */
898
899 RIB_RLOCK(rnh);
900 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
901 info->rti_info[RTAX_NETMASK], &rnh->head);
902
903 if (rt == NULL) {
904 RIB_RUNLOCK(rnh);
905 return (ESRCH);
906 }
907
908 rnd_orig.rnd_nhop = rt->rt_nhop;
909 rnd_orig.rnd_weight = rt->rt_weight;
910
911 RIB_RUNLOCK(rnh);
912
913 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
914 error = change_route(rnh, info, &rnd_orig, rc);
915 if (error != EAGAIN)
916 break;
917 }
918
919 return (error);
920 }
921
922 static int
change_nhop(struct rib_head * rnh,struct rt_addrinfo * info,struct nhop_object * nh_orig,struct nhop_object ** nh_new)923 change_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
924 struct nhop_object *nh_orig, struct nhop_object **nh_new)
925 {
926 int free_ifa = 0;
927 int error;
928
929 /*
930 * New gateway could require new ifaddr, ifp;
931 * flags may also be different; ifp may be specified
932 * by ll sockaddr when protocol address is ambiguous
933 */
934 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
935 info->rti_info[RTAX_GATEWAY] != NULL) ||
936 info->rti_info[RTAX_IFP] != NULL ||
937 (info->rti_info[RTAX_IFA] != NULL &&
938 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
939 error = rt_getifa_fib(info, rnh->rib_fibnum);
940 if (info->rti_ifa != NULL)
941 free_ifa = 1;
942
943 if (error != 0) {
944 if (free_ifa) {
945 ifa_free(info->rti_ifa);
946 info->rti_ifa = NULL;
947 }
948
949 return (error);
950 }
951 }
952
953 error = nhop_create_from_nhop(rnh, nh_orig, info, nh_new);
954 if (free_ifa) {
955 ifa_free(info->rti_ifa);
956 info->rti_ifa = NULL;
957 }
958
959 return (error);
960 }
961
962 #ifdef ROUTE_MPATH
963 static int
change_mpath_route(struct rib_head * rnh,struct rt_addrinfo * info,struct route_nhop_data * rnd_orig,struct rib_cmd_info * rc)964 change_mpath_route(struct rib_head *rnh, struct rt_addrinfo *info,
965 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
966 {
967 int error = 0;
968 struct nhop_object *nh, *nh_orig, *nh_new;
969 struct route_nhop_data rnd_new;
970
971 nh = NULL;
972 nh_orig = rnd_orig->rnd_nhop;
973
974 struct weightened_nhop *wn = NULL, *wn_new;
975 uint32_t num_nhops;
976
977 wn = nhgrp_get_nhops((struct nhgrp_object *)nh_orig, &num_nhops);
978 nh_orig = NULL;
979 for (int i = 0; i < num_nhops; i++) {
980 if (check_info_match_nhop(info, NULL, wn[i].nh)) {
981 nh_orig = wn[i].nh;
982 break;
983 }
984 }
985
986 if (nh_orig == NULL)
987 return (ESRCH);
988
989 error = change_nhop(rnh, info, nh_orig, &nh_new);
990 if (error != 0)
991 return (error);
992
993 wn_new = mallocarray(num_nhops, sizeof(struct weightened_nhop),
994 M_TEMP, M_NOWAIT | M_ZERO);
995 if (wn_new == NULL) {
996 nhop_free(nh_new);
997 return (EAGAIN);
998 }
999
1000 memcpy(wn_new, wn, num_nhops * sizeof(struct weightened_nhop));
1001 for (int i = 0; i < num_nhops; i++) {
1002 if (wn[i].nh == nh_orig) {
1003 wn[i].nh = nh_new;
1004 wn[i].weight = get_info_weight(info, rnd_orig->rnd_weight);
1005 break;
1006 }
1007 }
1008
1009 error = nhgrp_get_group(rnh, wn_new, num_nhops, &rnd_new);
1010 nhop_free(nh_new);
1011 free(wn_new, M_TEMP);
1012
1013 if (error != 0)
1014 return (error);
1015
1016 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1017
1018 return (error);
1019 }
1020 #endif
1021
1022 static int
change_route(struct rib_head * rnh,struct rt_addrinfo * info,struct route_nhop_data * rnd_orig,struct rib_cmd_info * rc)1023 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
1024 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
1025 {
1026 int error = 0;
1027 struct nhop_object *nh, *nh_orig;
1028 struct route_nhop_data rnd_new;
1029
1030 nh = NULL;
1031 nh_orig = rnd_orig->rnd_nhop;
1032 if (nh_orig == NULL)
1033 return (ESRCH);
1034
1035 #ifdef ROUTE_MPATH
1036 if (NH_IS_NHGRP(nh_orig))
1037 return (change_mpath_route(rnh, info, rnd_orig, rc));
1038 #endif
1039
1040 rnd_new.rnd_weight = get_info_weight(info, rnd_orig->rnd_weight);
1041 error = change_nhop(rnh, info, nh_orig, &rnd_new.rnd_nhop);
1042 if (error != 0)
1043 return (error);
1044 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1045
1046 return (error);
1047 }
1048
1049 /*
1050 * Insert @rt with nhop data from @rnd_new to @rnh.
1051 * Returns 0 on success and stores operation results in @rc.
1052 */
1053 static int
add_route_nhop(struct rib_head * rnh,struct rtentry * rt,struct rt_addrinfo * info,struct route_nhop_data * rnd,struct rib_cmd_info * rc)1054 add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1055 struct rt_addrinfo *info, struct route_nhop_data *rnd,
1056 struct rib_cmd_info *rc)
1057 {
1058 struct sockaddr *ndst, *netmask;
1059 struct radix_node *rn;
1060 int error = 0;
1061
1062 RIB_WLOCK_ASSERT(rnh);
1063
1064 ndst = (struct sockaddr *)rt_key(rt);
1065 netmask = info->rti_info[RTAX_NETMASK];
1066
1067 rt->rt_nhop = rnd->rnd_nhop;
1068 rt->rt_weight = rnd->rnd_weight;
1069 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
1070
1071 if (rn != NULL) {
1072 if (rt->rt_expire > 0)
1073 tmproutes_update(rnh, rt);
1074
1075 /* Finalize notification */
1076 rnh->rnh_gen++;
1077 rnh->rnh_prefixes++;
1078
1079 rc->rc_cmd = RTM_ADD;
1080 rc->rc_rt = rt;
1081 rc->rc_nh_old = NULL;
1082 rc->rc_nh_new = rnd->rnd_nhop;
1083 rc->rc_nh_weight = rnd->rnd_weight;
1084
1085 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1086 } else {
1087 /* Existing route or memory allocation failure */
1088 error = EEXIST;
1089 }
1090
1091 return (error);
1092 }
1093
1094 /*
1095 * Switch @rt nhop/weigh to the ones specified in @rnd.
1096 * Conditionally set rt_expire if set in @info.
1097 * Returns 0 on success.
1098 */
1099 int
change_route_nhop(struct rib_head * rnh,struct rtentry * rt,struct rt_addrinfo * info,struct route_nhop_data * rnd,struct rib_cmd_info * rc)1100 change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1101 struct rt_addrinfo *info, struct route_nhop_data *rnd,
1102 struct rib_cmd_info *rc)
1103 {
1104 struct nhop_object *nh_orig;
1105
1106 RIB_WLOCK_ASSERT(rnh);
1107
1108 nh_orig = rt->rt_nhop;
1109
1110 if (rnd->rnd_nhop != NULL) {
1111 /* Changing expiration & nexthop & weight to a new one */
1112 rt_set_expire_info(rt, info);
1113 rt->rt_nhop = rnd->rnd_nhop;
1114 rt->rt_weight = rnd->rnd_weight;
1115 if (rt->rt_expire > 0)
1116 tmproutes_update(rnh, rt);
1117 } else {
1118 /* Route deletion requested. */
1119 struct sockaddr *ndst, *netmask;
1120 struct radix_node *rn;
1121
1122 ndst = (struct sockaddr *)rt_key(rt);
1123 netmask = info->rti_info[RTAX_NETMASK];
1124 rn = rnh->rnh_deladdr(ndst, netmask, &rnh->head);
1125 if (rn == NULL)
1126 return (ESRCH);
1127 rt = RNTORT(rn);
1128 rt->rte_flags &= ~RTF_UP;
1129 }
1130
1131 /* Finalize notification */
1132 rnh->rnh_gen++;
1133 if (rnd->rnd_nhop == NULL)
1134 rnh->rnh_prefixes--;
1135
1136 rc->rc_cmd = (rnd->rnd_nhop != NULL) ? RTM_CHANGE : RTM_DELETE;
1137 rc->rc_rt = rt;
1138 rc->rc_nh_old = nh_orig;
1139 rc->rc_nh_new = rnd->rnd_nhop;
1140 rc->rc_nh_weight = rnd->rnd_weight;
1141
1142 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1143
1144 return (0);
1145 }
1146
1147 /*
1148 * Conditionally update route nhop/weight IFF data in @nhd_orig is
1149 * consistent with the current route data.
1150 * Nexthop in @nhd_new is consumed.
1151 */
1152 int
change_route_conditional(struct rib_head * rnh,struct rtentry * rt,struct rt_addrinfo * info,struct route_nhop_data * rnd_orig,struct route_nhop_data * rnd_new,struct rib_cmd_info * rc)1153 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
1154 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
1155 struct route_nhop_data *rnd_new, struct rib_cmd_info *rc)
1156 {
1157 struct rtentry *rt_new;
1158 int error = 0;
1159
1160 RIB_WLOCK(rnh);
1161
1162 rt_new = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1163 info->rti_info[RTAX_NETMASK], &rnh->head);
1164
1165 if (rt_new == NULL) {
1166 if (rnd_orig->rnd_nhop == NULL)
1167 error = add_route_nhop(rnh, rt, info, rnd_new, rc);
1168 else {
1169 /*
1170 * Prefix does not exist, which was not our assumption.
1171 * Update @rnd_orig with the new data and return
1172 */
1173 rnd_orig->rnd_nhop = NULL;
1174 rnd_orig->rnd_weight = 0;
1175 error = EAGAIN;
1176 }
1177 } else {
1178 /* Prefix exists, try to update */
1179 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
1180 /*
1181 * Nhop/mpath group hasn't changed. Flip
1182 * to the new precalculated one and return
1183 */
1184 error = change_route_nhop(rnh, rt_new, info, rnd_new, rc);
1185 } else {
1186 /* Update and retry */
1187 rnd_orig->rnd_nhop = rt_new->rt_nhop;
1188 rnd_orig->rnd_weight = rt_new->rt_weight;
1189 error = EAGAIN;
1190 }
1191 }
1192
1193 RIB_WUNLOCK(rnh);
1194
1195 if (error == 0) {
1196 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
1197
1198 if (rnd_orig->rnd_nhop != NULL)
1199 nhop_free_any(rnd_orig->rnd_nhop);
1200
1201 } else {
1202 if (rnd_new->rnd_nhop != NULL)
1203 nhop_free_any(rnd_new->rnd_nhop);
1204 }
1205
1206 return (error);
1207 }
1208
1209 /*
1210 * Performs modification of routing table specificed by @action.
1211 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
1212 * Needs to be run in network epoch.
1213 *
1214 * Returns 0 on success and fills in @rc with action result.
1215 */
1216 int
rib_action(uint32_t fibnum,int action,struct rt_addrinfo * info,struct rib_cmd_info * rc)1217 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
1218 struct rib_cmd_info *rc)
1219 {
1220 int error;
1221
1222 switch (action) {
1223 case RTM_ADD:
1224 error = rib_add_route(fibnum, info, rc);
1225 break;
1226 case RTM_DELETE:
1227 error = rib_del_route(fibnum, info, rc);
1228 break;
1229 case RTM_CHANGE:
1230 error = rib_change_route(fibnum, info, rc);
1231 break;
1232 default:
1233 error = ENOTSUP;
1234 }
1235
1236 return (error);
1237 }
1238
1239 struct rt_delinfo
1240 {
1241 struct rt_addrinfo info;
1242 struct rib_head *rnh;
1243 struct rtentry *head;
1244 struct rib_cmd_info rc;
1245 };
1246
1247 /*
1248 * Conditionally unlinks @rn from radix tree based
1249 * on info data passed in @arg.
1250 */
1251 static int
rt_checkdelroute(struct radix_node * rn,void * arg)1252 rt_checkdelroute(struct radix_node *rn, void *arg)
1253 {
1254 struct rt_delinfo *di;
1255 struct rt_addrinfo *info;
1256 struct rtentry *rt;
1257
1258 di = (struct rt_delinfo *)arg;
1259 rt = (struct rtentry *)rn;
1260 info = &di->info;
1261
1262 info->rti_info[RTAX_DST] = rt_key(rt);
1263 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1264
1265 if (rt_unlinkrte(di->rnh, info, &di->rc) != 0)
1266 return (0);
1267
1268 /*
1269 * Add deleted rtentries to the list to GC them
1270 * after dropping the lock.
1271 *
1272 * XXX: Delayed notifications not implemented
1273 * for nexthop updates.
1274 */
1275 if (di->rc.rc_cmd == RTM_DELETE) {
1276 /* Add to the list and return */
1277 rt->rt_chain = di->head;
1278 di->head = rt;
1279 #ifdef ROUTE_MPATH
1280 } else {
1281 /*
1282 * RTM_CHANGE to a diferent nexthop or nexthop group.
1283 * Free old multipath group.
1284 */
1285 nhop_free_any(di->rc.rc_nh_old);
1286 #endif
1287 }
1288
1289 return (0);
1290 }
1291
1292 /*
1293 * Iterates over a routing table specified by @fibnum and @family and
1294 * deletes elements marked by @filter_f.
1295 * @fibnum: rtable id
1296 * @family: AF_ address family
1297 * @filter_f: function returning non-zero value for items to delete
1298 * @arg: data to pass to the @filter_f function
1299 * @report: true if rtsock notification is needed.
1300 */
1301 void
rib_walk_del(u_int fibnum,int family,rib_filter_f_t * filter_f,void * arg,bool report)1302 rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f, void *arg, bool report)
1303 {
1304 struct rib_head *rnh;
1305 struct rt_delinfo di;
1306 struct rtentry *rt;
1307 struct nhop_object *nh;
1308 struct epoch_tracker et;
1309
1310 rnh = rt_tables_get_rnh(fibnum, family);
1311 if (rnh == NULL)
1312 return;
1313
1314 bzero(&di, sizeof(di));
1315 di.info.rti_filter = filter_f;
1316 di.info.rti_filterdata = arg;
1317 di.rnh = rnh;
1318 di.rc.rc_cmd = RTM_DELETE;
1319
1320 NET_EPOCH_ENTER(et);
1321
1322 RIB_WLOCK(rnh);
1323 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1324 RIB_WUNLOCK(rnh);
1325
1326 /* We might have something to reclaim. */
1327 bzero(&di.rc, sizeof(di.rc));
1328 di.rc.rc_cmd = RTM_DELETE;
1329 while (di.head != NULL) {
1330 rt = di.head;
1331 di.head = rt->rt_chain;
1332 rt->rt_chain = NULL;
1333 nh = rt->rt_nhop;
1334
1335 di.rc.rc_rt = rt;
1336 di.rc.rc_nh_old = nh;
1337 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1338
1339 /* TODO std rt -> rt_addrinfo export */
1340 di.info.rti_info[RTAX_DST] = rt_key(rt);
1341 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1342
1343 if (report) {
1344 #ifdef ROUTE_MPATH
1345 struct nhgrp_object *nhg;
1346 struct weightened_nhop *wn;
1347 uint32_t num_nhops;
1348 if (NH_IS_NHGRP(nh)) {
1349 nhg = (struct nhgrp_object *)nh;
1350 wn = nhgrp_get_nhops(nhg, &num_nhops);
1351 for (int i = 0; i < num_nhops; i++)
1352 rt_routemsg(RTM_DELETE, rt, wn[i].nh, fibnum);
1353 } else
1354 #endif
1355 rt_routemsg(RTM_DELETE, rt, nh, fibnum);
1356 }
1357 rtfree(rt);
1358 }
1359
1360 NET_EPOCH_EXIT(et);
1361 }
1362
1363 static int
rt_delete_unconditional(struct radix_node * rn,void * arg)1364 rt_delete_unconditional(struct radix_node *rn, void *arg)
1365 {
1366 struct rtentry *rt = RNTORT(rn);
1367 struct rib_head *rnh = (struct rib_head *)arg;
1368
1369 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), &rnh->head);
1370 if (RNTORT(rn) == rt)
1371 rtfree(rt);
1372
1373 return (0);
1374 }
1375
1376 /*
1377 * Removes all routes from the routing table without executing notifications.
1378 * rtentres will be removed after the end of a current epoch.
1379 */
1380 static void
rib_flush_routes(struct rib_head * rnh)1381 rib_flush_routes(struct rib_head *rnh)
1382 {
1383 RIB_WLOCK(rnh);
1384 rnh->rnh_walktree(&rnh->head, rt_delete_unconditional, rnh);
1385 RIB_WUNLOCK(rnh);
1386 }
1387
1388 void
rib_flush_routes_family(int family)1389 rib_flush_routes_family(int family)
1390 {
1391 struct rib_head *rnh;
1392
1393 for (uint32_t fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1394 if ((rnh = rt_tables_get_rnh(fibnum, family)) != NULL)
1395 rib_flush_routes(rnh);
1396 }
1397 }
1398
1399 static void
rib_notify(struct rib_head * rnh,enum rib_subscription_type type,struct rib_cmd_info * rc)1400 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1401 struct rib_cmd_info *rc)
1402 {
1403 struct rib_subscription *rs;
1404
1405 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1406 if (rs->type == type)
1407 rs->func(rnh, rc, rs->arg);
1408 }
1409 }
1410
1411 static struct rib_subscription *
allocate_subscription(rib_subscription_cb_t * f,void * arg,enum rib_subscription_type type,bool waitok)1412 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1413 enum rib_subscription_type type, bool waitok)
1414 {
1415 struct rib_subscription *rs;
1416 int flags = M_ZERO | (waitok ? M_WAITOK : M_NOWAIT);
1417
1418 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1419 if (rs == NULL)
1420 return (NULL);
1421
1422 rs->func = f;
1423 rs->arg = arg;
1424 rs->type = type;
1425
1426 return (rs);
1427 }
1428
1429 /*
1430 * Subscribe for the changes in the routing table specified by @fibnum and
1431 * @family.
1432 *
1433 * Returns pointer to the subscription structure on success.
1434 */
1435 struct rib_subscription *
rib_subscribe(uint32_t fibnum,int family,rib_subscription_cb_t * f,void * arg,enum rib_subscription_type type,bool waitok)1436 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1437 enum rib_subscription_type type, bool waitok)
1438 {
1439 struct rib_head *rnh;
1440 struct epoch_tracker et;
1441
1442 NET_EPOCH_ENTER(et);
1443 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1444 rnh = rt_tables_get_rnh(fibnum, family);
1445 NET_EPOCH_EXIT(et);
1446
1447 return (rib_subscribe_internal(rnh, f, arg, type, waitok));
1448 }
1449
1450 struct rib_subscription *
rib_subscribe_internal(struct rib_head * rnh,rib_subscription_cb_t * f,void * arg,enum rib_subscription_type type,bool waitok)1451 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1452 enum rib_subscription_type type, bool waitok)
1453 {
1454 struct rib_subscription *rs;
1455 struct epoch_tracker et;
1456
1457 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1458 return (NULL);
1459 rs->rnh = rnh;
1460
1461 NET_EPOCH_ENTER(et);
1462 RIB_WLOCK(rnh);
1463 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1464 RIB_WUNLOCK(rnh);
1465 NET_EPOCH_EXIT(et);
1466
1467 return (rs);
1468 }
1469
1470 struct rib_subscription *
rib_subscribe_locked(struct rib_head * rnh,rib_subscription_cb_t * f,void * arg,enum rib_subscription_type type)1471 rib_subscribe_locked(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1472 enum rib_subscription_type type)
1473 {
1474 struct rib_subscription *rs;
1475
1476 NET_EPOCH_ASSERT();
1477 RIB_WLOCK_ASSERT(rnh);
1478
1479 if ((rs = allocate_subscription(f, arg, type, false)) == NULL)
1480 return (NULL);
1481 rs->rnh = rnh;
1482
1483 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1484
1485 return (rs);
1486 }
1487
1488 /*
1489 * Remove rtable subscription @rs from the routing table.
1490 * Needs to be run in network epoch.
1491 */
1492 void
rib_unsibscribe(struct rib_subscription * rs)1493 rib_unsibscribe(struct rib_subscription *rs)
1494 {
1495 struct rib_head *rnh = rs->rnh;
1496
1497 NET_EPOCH_ASSERT();
1498
1499 RIB_WLOCK(rnh);
1500 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1501 RIB_WUNLOCK(rnh);
1502
1503 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1504 &rs->epoch_ctx);
1505 }
1506
1507 void
rib_unsibscribe_locked(struct rib_subscription * rs)1508 rib_unsibscribe_locked(struct rib_subscription *rs)
1509 {
1510 struct rib_head *rnh = rs->rnh;
1511
1512 NET_EPOCH_ASSERT();
1513 RIB_WLOCK_ASSERT(rnh);
1514
1515 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1516
1517 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1518 &rs->epoch_ctx);
1519 }
1520
1521 /*
1522 * Epoch callback indicating subscription is safe to destroy
1523 */
1524 static void
destroy_subscription_epoch(epoch_context_t ctx)1525 destroy_subscription_epoch(epoch_context_t ctx)
1526 {
1527 struct rib_subscription *rs;
1528
1529 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1530
1531 free(rs, M_RTABLE);
1532 }
1533
1534 void
rib_init_subscriptions(struct rib_head * rnh)1535 rib_init_subscriptions(struct rib_head *rnh)
1536 {
1537
1538 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1539 }
1540
1541 void
rib_destroy_subscriptions(struct rib_head * rnh)1542 rib_destroy_subscriptions(struct rib_head *rnh)
1543 {
1544 struct rib_subscription *rs;
1545 struct epoch_tracker et;
1546
1547 NET_EPOCH_ENTER(et);
1548 RIB_WLOCK(rnh);
1549 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1550 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1551 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1552 &rs->epoch_ctx);
1553 }
1554 RIB_WUNLOCK(rnh);
1555 NET_EPOCH_EXIT(et);
1556 }
1557