2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
32 #include "opt_route.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/syslog.h>
41 #include <sys/kernel.h>
43 #include <sys/rmlock.h>
46 #include <net/if_var.h>
47 #include <net/if_dl.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>
60 #define DEBUG_MOD_NAME route_ctl
61 #define DEBUG_MAX_LEVEL LOG_DEBUG
62 #include <net/route/route_debug.h>
63 _DECLARE_DEBUG(LOG_INFO);
66 * This file contains control plane routing tables functions.
68 * All functions assumes they are called in net epoch.
71 struct rib_subscription {
72 CK_STAILQ_ENTRY(rib_subscription) next;
73 rib_subscription_cb_t *func;
76 enum rib_subscription_type type;
77 struct epoch_context epoch_ctx;
80 static int add_route_byinfo(struct rib_head *rnh, struct rt_addrinfo *info,
81 struct rib_cmd_info *rc);
82 static int change_route_byinfo(struct rib_head *rnh, struct rtentry *rt,
83 struct rt_addrinfo *info, struct route_nhop_data *nhd_orig,
84 struct rib_cmd_info *rc);
86 static int add_route(struct rib_head *rnh, struct rtentry *rt,
87 struct route_nhop_data *rnd, struct rib_cmd_info *rc);
88 static int delete_route(struct rib_head *rnh, struct rtentry *rt,
89 struct rib_cmd_info *rc);
90 static int rt_delete_conditional(struct rib_head *rnh, struct rtentry *rt,
91 int prio, rib_filter_f_t *cb, void *cbdata, struct rib_cmd_info *rc);
93 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
94 struct rib_cmd_info *rc);
96 static int get_prio_from_info(const struct rt_addrinfo *info);
97 static int nhop_get_prio(const struct nhop_object *nh);
99 static void destroy_subscription_epoch(epoch_context_t ctx);
101 static bool rib_can_multipath(struct rib_head *rh);
104 /* Per-vnet multipath routing configuration */
105 SYSCTL_DECL(_net_route);
106 #define V_rib_route_multipath VNET(rib_route_multipath)
108 #define _MP_FLAGS CTLFLAG_RW
110 #define _MP_FLAGS CTLFLAG_RD
112 VNET_DEFINE(u_int, rib_route_multipath) = 1;
113 SYSCTL_UINT(_net_route, OID_AUTO, multipath, _MP_FLAGS | CTLFLAG_VNET,
114 &VNET_NAME(rib_route_multipath), 0, "Enable route multipath");
117 #if defined(INET) && defined(INET6)
118 FEATURE(ipv4_rfc5549_support, "Route IPv4 packets via IPv6 nexthops");
119 #define V_rib_route_ipv6_nexthop VNET(rib_route_ipv6_nexthop)
120 VNET_DEFINE(u_int, rib_route_ipv6_nexthop) = 1;
121 SYSCTL_UINT(_net_route, OID_AUTO, ipv6_nexthop, CTLFLAG_RW | CTLFLAG_VNET,
122 &VNET_NAME(rib_route_ipv6_nexthop), 0, "Enable IPv4 route via IPv6 Next Hop address");
125 /* Routing table UMA zone */
126 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
127 #define V_rtzone VNET(rtzone)
130 SYSCTL_NODE(_net_route, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
133 vnet_rtzone_init(void)
136 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
137 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
142 vnet_rtzone_destroy(void)
145 uma_zdestroy(V_rtzone);
150 destroy_rtentry(struct rtentry *rt)
153 struct nhop_object *nh = rt->rt_nhop;
156 * At this moment rnh, nh_control may be already freed.
157 * nhop interface may have been migrated to a different vnet.
158 * Use vnet stored in the nexthop to delete the entry.
161 if (NH_IS_NHGRP(nh)) {
162 const struct weightened_nhop *wn;
164 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
168 CURVNET_SET(nhop_get_vnet(nh));
171 /* Unreference nexthop */
172 nhop_free_any(rt->rt_nhop);
174 uma_zfree(V_rtzone, rt);
180 * Epoch callback indicating rtentry is safe to destroy
183 destroy_rtentry_epoch(epoch_context_t ctx)
187 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
193 * Schedule rtentry deletion
196 rtfree(struct rtentry *rt)
199 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
201 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
205 static struct rib_head *
206 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
208 struct rib_head *rnh;
209 struct sockaddr *dst;
211 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
213 dst = info->rti_info[RTAX_DST];
214 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
219 #if defined(INET) && defined(INET6)
221 rib_can_ipv6_nexthop_address(struct rib_head *rh)
225 CURVNET_SET(rh->rib_vnet);
226 result = !!V_rib_route_ipv6_nexthop;
235 rib_can_multipath(struct rib_head *rh)
239 CURVNET_SET(rh->rib_vnet);
240 result = !!V_rib_route_multipath;
247 * Check is nhop is multipath-eligible.
248 * Avoid nhops without gateways and redirects.
250 * Returns 1 for multipath-eligible nexthop,
254 nhop_can_multipath(const struct nhop_object *nh)
257 if ((nh->nh_flags & NHF_MULTIPATH) != 0)
259 if ((nh->nh_flags & NHF_GATEWAY) == 0)
261 if ((nh->nh_flags & NHF_REDIRECT) != 0)
269 get_info_weight(const struct rt_addrinfo *info, uint32_t default_weight)
273 if (info->rti_mflags & RTV_WEIGHT)
274 weight = info->rti_rmx->rmx_weight;
276 weight = default_weight;
277 /* Keep upper 1 byte for adm distance purposes */
278 if (weight > RT_MAX_WEIGHT)
279 weight = RT_MAX_WEIGHT;
280 else if (weight == 0)
281 weight = default_weight;
287 rt_is_host(const struct rtentry *rt)
290 return (rt->rte_flags & RTF_HOST);
294 rt_get_family(const struct rtentry *rt)
296 const struct sockaddr *dst;
298 dst = (const struct sockaddr *)rt_key_const(rt);
300 return (dst->sa_family);
304 * Returns pointer to nexthop or nexthop group
305 * associated with @rt
308 rt_get_raw_nhop(const struct rtentry *rt)
311 return (rt->rt_nhop);
316 * Stores IPv4 address and prefix length of @rt inside
318 * @pscopeid is currently always set to 0.
321 rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
322 int *plen, uint32_t *pscopeid)
324 const struct sockaddr_in *dst;
326 dst = (const struct sockaddr_in *)rt_key_const(rt);
327 KASSERT((dst->sin_family == AF_INET),
328 ("rt family is %d, not inet", dst->sin_family));
329 *paddr = dst->sin_addr;
330 dst = (const struct sockaddr_in *)rt_mask_const(rt);
334 *plen = bitcount32(dst->sin_addr.s_addr);
339 * Stores IPv4 address and prefix mask of @rt inside
340 * @paddr and @pmask. Sets mask to INADDR_ANY for host routes.
341 * @pscopeid is currently always set to 0.
344 rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
345 struct in_addr *pmask, uint32_t *pscopeid)
347 const struct sockaddr_in *dst;
349 dst = (const struct sockaddr_in *)rt_key_const(rt);
350 KASSERT((dst->sin_family == AF_INET),
351 ("rt family is %d, not inet", dst->sin_family));
352 *paddr = dst->sin_addr;
353 dst = (const struct sockaddr_in *)rt_mask_const(rt);
355 pmask->s_addr = INADDR_BROADCAST;
357 *pmask = dst->sin_addr;
364 inet6_get_plen(const struct in6_addr *addr)
367 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
368 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
372 * Stores IPv6 address and prefix length of @rt inside
373 * @paddr and @plen. Addresses are returned in de-embedded form.
374 * Scopeid is set to 0 for non-LL addresses.
377 rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
378 int *plen, uint32_t *pscopeid)
380 const struct sockaddr_in6 *dst;
382 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
383 KASSERT((dst->sin6_family == AF_INET6),
384 ("rt family is %d, not inet6", dst->sin6_family));
385 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
386 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
388 *paddr = dst->sin6_addr;
389 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
393 *plen = inet6_get_plen(&dst->sin6_addr);
397 * Stores IPv6 address and prefix mask of @rt inside
398 * @paddr and @pmask. Addresses are returned in de-embedded form.
399 * Scopeid is set to 0 for non-LL addresses.
402 rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
403 struct in6_addr *pmask, uint32_t *pscopeid)
405 const struct sockaddr_in6 *dst;
407 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
408 KASSERT((dst->sin6_family == AF_INET6),
409 ("rt family is %d, not inet", dst->sin6_family));
410 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
411 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
413 *paddr = dst->sin6_addr;
414 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
416 memset(pmask, 0xFF, sizeof(struct in6_addr));
418 *pmask = dst->sin6_addr;
423 * Check if specified @gw matches gw data in the nexthop @nh.
425 * Returns true if matches, false otherwise.
428 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
431 if (nh->gw_sa.sa_family != gw->sa_family)
434 switch (gw->sa_family) {
436 return (nh->gw4_sa.sin_addr.s_addr ==
437 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
440 const struct sockaddr_in6 *gw6;
441 gw6 = (const struct sockaddr_in6 *)gw;
444 * Currently (2020-09) IPv6 gws in kernel have their
445 * scope embedded. Once this becomes false, this code
446 * has to be revisited.
448 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
455 const struct sockaddr_dl *sdl;
456 sdl = (const struct sockaddr_dl *)gw;
457 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
460 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
467 struct gw_filter_data {
468 const struct sockaddr *gw;
473 gw_filter_func(const struct rtentry *rt, const struct nhop_object *nh, void *_data)
475 struct gw_filter_data *gwd = (struct gw_filter_data *)_data;
477 /* Return only first match to make rtsock happy */
478 if (match_nhop_gw(nh, gwd->gw) && gwd->count++ == 0)
484 * Checks if data in @info matches nexhop @nh.
486 * Returns 0 on success,
487 * ESRCH if not matched,
488 * ENOENT if filter function returned false
491 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
492 const struct nhop_object *nh)
494 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
496 if (info->rti_filter != NULL) {
497 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
502 if ((gw != NULL) && !match_nhop_gw(nh, gw))
509 * Runs exact prefix match based on @dst and @netmask.
510 * Returns matched @rtentry if found or NULL.
511 * If rtentry was found, saves nexthop / weight value into @rnd.
513 static struct rtentry *
514 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
515 const struct sockaddr *netmask, struct route_nhop_data *rnd)
519 RIB_LOCK_ASSERT(rnh);
521 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head);
523 rnd->rnd_nhop = rt->rt_nhop;
524 rnd->rnd_weight = rt->rt_weight;
526 rnd->rnd_nhop = NULL;
534 lookup_prefix_rt(struct rib_head *rnh, const struct rtentry *rt,
535 struct route_nhop_data *rnd)
537 return (lookup_prefix_bysa(rnh, rt_key_const(rt), rt_mask_const(rt), rnd));
541 * Runs exact prefix match based on dst/netmask from @info.
542 * Assumes RIB lock is held.
543 * Returns matched @rtentry if found or NULL.
544 * If rtentry was found, saves nexthop / weight value into @rnd.
547 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
548 struct route_nhop_data *rnd)
552 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
553 info->rti_info[RTAX_NETMASK], rnd);
559 * Adds route defined by @info into the kernel table specified by @fibnum and
560 * sa_family in @info->rti_info[RTAX_DST].
562 * Returns 0 on success and fills in operation metadata into @rc.
565 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
566 struct rib_cmd_info *rc)
568 struct rib_head *rnh;
573 rnh = get_rnh(fibnum, info);
575 return (EAFNOSUPPORT);
578 * Check consistency between RTF_HOST flag and netmask
581 if (info->rti_flags & RTF_HOST)
582 info->rti_info[RTAX_NETMASK] = NULL;
583 else if (info->rti_info[RTAX_NETMASK] == NULL) {
584 FIB_RH_LOG(LOG_DEBUG, rnh, "error: no RTF_HOST and empty netmask");
588 bzero(rc, sizeof(struct rib_cmd_info));
589 rc->rc_cmd = RTM_ADD;
591 error = add_route_byinfo(rnh, info, rc);
593 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
599 * Checks if @dst and @gateway is valid combination.
601 * Returns true if is valid, false otherwise.
604 check_gateway(struct rib_head *rnh, struct sockaddr *dst,
605 struct sockaddr *gateway)
607 if (dst->sa_family == gateway->sa_family)
609 else if (gateway->sa_family == AF_UNSPEC)
611 else if (gateway->sa_family == AF_LINK)
613 #if defined(INET) && defined(INET6)
614 else if (dst->sa_family == AF_INET && gateway->sa_family == AF_INET6 &&
615 rib_can_ipv6_nexthop_address(rnh))
623 * Creates rtentry and nexthop based on @info data.
624 * Return 0 and fills in rtentry into @prt on success,
625 * Note: rtentry mask will be set to RTAX_NETMASK, thus its pointer is required
626 * to be stable till the end of the operation (radix rt insertion/change/removal).
627 * return errno otherwise.
629 static struct rtentry *
630 create_rtentry(struct rib_head *rnh, const struct sockaddr *dst,
631 struct sockaddr *netmask)
633 MPASS(dst->sa_len <= sizeof(((struct rtentry *)NULL)->rt_dstb));
635 struct rtentry *rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
638 rt->rte_flags = RTF_UP | (netmask == NULL ? RTF_HOST : 0);
640 /* Fill in dst, ensuring it's masked if needed. */
641 if (netmask != NULL) {
642 rt_maskedcopy(dst, &rt->rt_dst, netmask);
644 bcopy(dst, &rt->rt_dst, dst->sa_len);
645 rt_key(rt) = &rt->rt_dst;
646 /* Set netmask to the storage from info. It will be updated upon insertion */
647 rt_mask(rt) = netmask;
653 add_route_byinfo(struct rib_head *rnh, struct rt_addrinfo *info,
654 struct rib_cmd_info *rc)
656 struct nhop_object *nh_orig;
657 struct route_nhop_data rnd_orig, rnd_add;
658 struct nhop_object *nh;
659 struct rtentry *rt, *rt_orig;
660 struct sockaddr *dst, *gateway, *netmask;
663 dst = info->rti_info[RTAX_DST];
664 gateway = info->rti_info[RTAX_GATEWAY];
665 netmask = info->rti_info[RTAX_NETMASK];
666 flags = info->rti_flags;
668 if ((flags & RTF_GATEWAY) && !gateway) {
669 FIB_RH_LOG(LOG_DEBUG, rnh, "error: RTF_GATEWAY set with empty gw");
672 if (dst && gateway && !check_gateway(rnh, dst, gateway)) {
673 FIB_RH_LOG(LOG_DEBUG, rnh,
674 "error: invalid dst/gateway family combination (%d, %d)",
675 dst->sa_family, gateway->sa_family);
679 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb)) {
680 FIB_RH_LOG(LOG_DEBUG, rnh, "error: dst->sa_len too large: %d",
685 if (info->rti_ifa == NULL) {
686 error = rt_getifa_fib(info, rnh->rib_fibnum);
691 if ((rt = create_rtentry(rnh, dst, netmask)) == NULL)
694 error = nhop_create_from_info(rnh, info, &nh);
696 uma_zfree(V_rtzone, rt);
700 rnd_add.rnd_nhop = nh;
701 rnd_add.rnd_weight = get_info_weight(info, RT_DEFAULT_WEIGHT);
704 error = add_route(rnh, rt, &rnd_add, rc);
710 /* addition failed. Lookup prefix in the rib to determine the cause */
711 rt_orig = lookup_prefix(rnh, info, &rnd_orig);
712 if (rt_orig == NULL) {
713 /* No prefix -> rnh_addaddr() failed to allocate memory */
716 uma_zfree(V_rtzone, rt);
720 /* We have existing route in the RIB. */
721 nh_orig = rnd_orig.rnd_nhop;
722 /* Check if new route has higher preference */
723 if (get_prio_from_info(info) > nhop_get_prio(nh_orig)) {
724 /* Update nexthop to the new route */
725 change_route(rnh, rt_orig, &rnd_add, rc);
727 uma_zfree(V_rtzone, rt);
735 if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) &&
736 nhop_can_multipath(rnd_orig.rnd_nhop))
737 error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc);
740 /* Unable to add - another route with the same preference exists */
744 * ROUTE_MPATH disabled: failed to add route, free both nhop and rt.
745 * ROUTE_MPATH enabled: original nhop reference is unused in any case,
746 * free rt only if not _adding_ new route to rib (e.g. the case
747 * when initial lookup returned existing route, but then it got
748 * deleted prior to multipath group insertion, leading to a simple
749 * non-multipath add as a result).
752 if ((error != 0) || rc->rc_cmd != RTM_ADD)
753 uma_zfree(V_rtzone, rt);
759 * Removes route defined by @info from the kernel table specified by @fibnum and
760 * sa_family in @info->rti_info[RTAX_DST].
762 * Returns 0 on success and fills in operation metadata into @rc.
765 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
767 struct rib_head *rnh;
768 struct sockaddr *dst, *netmask;
769 struct sockaddr_storage mdst;
774 rnh = get_rnh(fibnum, info);
776 return (EAFNOSUPPORT);
778 bzero(rc, sizeof(struct rib_cmd_info));
779 rc->rc_cmd = RTM_DELETE;
781 dst = info->rti_info[RTAX_DST];
782 netmask = info->rti_info[RTAX_NETMASK];
784 if (netmask != NULL) {
785 /* Ensure @dst is always properly masked */
786 if (dst->sa_len > sizeof(mdst)) {
787 FIB_RH_LOG(LOG_DEBUG, rnh, "error: dst->sa_len too large");
790 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
791 dst = (struct sockaddr *)&mdst;
794 rib_filter_f_t *filter_func = NULL;
795 void *filter_arg = NULL;
796 struct gw_filter_data gwd = { .gw = info->rti_info[RTAX_GATEWAY] };
798 if (info->rti_filter != NULL) {
799 filter_func = info->rti_filter;
800 filter_arg = info->rti_filterdata;
801 } else if (gwd.gw != NULL) {
802 filter_func = gw_filter_func;
806 int prio = get_prio_from_info(info);
809 struct route_nhop_data rnd;
810 struct rtentry *rt = lookup_prefix_bysa(rnh, dst, netmask, &rnd);
812 error = rt_delete_conditional(rnh, rt, prio, filter_func,
821 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
823 if (rc->rc_cmd == RTM_DELETE)
828 * Deleting 1 path may result in RTM_CHANGE to
829 * a different mpath group/nhop.
830 * Free old mpath group.
832 nhop_free_any(rc->rc_nh_old);
840 * File-local concept for distingushing between the normal and
841 * RTF_PINNED routes tha can override the "normal" one.
843 #define NH_PRIORITY_HIGH 2
844 #define NH_PRIORITY_NORMAL 1
846 get_prio_from_info(const struct rt_addrinfo *info)
848 if (info->rti_flags & RTF_PINNED)
849 return (NH_PRIORITY_HIGH);
850 return (NH_PRIORITY_NORMAL);
854 nhop_get_prio(const struct nhop_object *nh)
856 if (NH_IS_PINNED(nh))
857 return (NH_PRIORITY_HIGH);
858 return (NH_PRIORITY_NORMAL);
862 * Conditionally unlinks rtentry paths from @rnh matching @cb.
863 * Returns 0 on success with operation result stored in @rc.
865 * ESRCH - if prefix was not found or filter function failed to match
866 * EADDRINUSE - if trying to delete higher priority route.
869 rt_delete_conditional(struct rib_head *rnh, struct rtentry *rt,
870 int prio, rib_filter_f_t *cb, void *cbdata, struct rib_cmd_info *rc)
872 struct nhop_object *nh = rt->rt_nhop;
873 struct route_nhop_data rnd;
876 if (NH_IS_NHGRP(nh)) {
877 struct nhgrp_object *nhg = (struct nhgrp_object *)nh;
882 error = nhgrp_get_filtered_group(rnh, rt, nhg, cb, cbdata, &rnd);
884 if (rnd.rnd_nhgrp == nhg) {
885 /* No match, unreference new group and return. */
886 nhop_free_any(rnd.rnd_nhop);
889 error = change_route(rnh, rt, &rnd, rc);
894 if (cb != NULL && !cb(rt, nh, cbdata))
897 if (prio < nhop_get_prio(nh))
900 return (delete_route(rnh, rt, rc));
904 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
905 struct rib_cmd_info *rc)
908 struct route_nhop_data rnd_orig;
909 struct rib_head *rnh;
915 rnh = get_rnh(fibnum, info);
917 return (EAFNOSUPPORT);
919 bzero(rc, sizeof(struct rib_cmd_info));
920 rc->rc_cmd = RTM_CHANGE;
922 /* Check if updated gateway exists */
923 if ((info->rti_flags & RTF_GATEWAY) &&
924 (info->rti_info[RTAX_GATEWAY] == NULL)) {
927 * route(8) adds RTF_GATEWAY flag if -interface is not set.
928 * Remove RTF_GATEWAY to enforce consistency and maintain
931 info->rti_flags &= ~RTF_GATEWAY;
935 * route change is done in multiple steps, with dropping and
936 * reacquiring lock. In the situations with multiple processes
937 * changes the same route in can lead to the case when route
938 * is changed between the steps. Address it by retrying the operation
939 * multiple times before failing.
943 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
944 info->rti_info[RTAX_NETMASK], &rnh->head);
951 rnd_orig.rnd_nhop = rt->rt_nhop;
952 rnd_orig.rnd_weight = rt->rt_weight;
956 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
957 error = change_route_byinfo(rnh, rt, info, &rnd_orig, rc);
966 change_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
967 struct nhop_object *nh_orig, struct nhop_object **nh_new)
972 * New gateway could require new ifaddr, ifp;
973 * flags may also be different; ifp may be specified
974 * by ll sockaddr when protocol address is ambiguous
976 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
977 info->rti_info[RTAX_GATEWAY] != NULL) ||
978 info->rti_info[RTAX_IFP] != NULL ||
979 (info->rti_info[RTAX_IFA] != NULL &&
980 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
981 error = rt_getifa_fib(info, rnh->rib_fibnum);
984 info->rti_ifa = NULL;
989 error = nhop_create_from_nhop(rnh, nh_orig, info, nh_new);
990 info->rti_ifa = NULL;
997 change_mpath_route(struct rib_head *rnh, struct rtentry *rt,
998 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
999 struct rib_cmd_info *rc)
1001 int error = 0, found_idx = 0;
1002 struct nhop_object *nh_orig = NULL, *nh_new;
1003 struct route_nhop_data rnd_new = {};
1004 const struct weightened_nhop *wn = NULL;
1005 struct weightened_nhop *wn_new;
1008 wn = nhgrp_get_nhops(rnd_orig->rnd_nhgrp, &num_nhops);
1009 for (int i = 0; i < num_nhops; i++) {
1010 if (check_info_match_nhop(info, NULL, wn[i].nh) == 0) {
1017 if (nh_orig == NULL)
1020 error = change_nhop(rnh, info, nh_orig, &nh_new);
1024 wn_new = mallocarray(num_nhops, sizeof(struct weightened_nhop),
1025 M_TEMP, M_NOWAIT | M_ZERO);
1026 if (wn_new == NULL) {
1031 memcpy(wn_new, wn, num_nhops * sizeof(struct weightened_nhop));
1032 wn_new[found_idx].nh = nh_new;
1033 wn_new[found_idx].weight = get_info_weight(info, wn[found_idx].weight);
1035 error = nhgrp_get_group(rnh, wn_new, num_nhops, &rnd_new.rnd_nhgrp);
1037 free(wn_new, M_TEMP);
1042 error = change_route_conditional(rnh, rt, rnd_orig, &rnd_new, rc);
1049 change_route_byinfo(struct rib_head *rnh, struct rtentry *rt,
1050 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
1051 struct rib_cmd_info *rc)
1054 struct nhop_object *nh_orig;
1055 struct route_nhop_data rnd_new;
1057 nh_orig = rnd_orig->rnd_nhop;
1058 if (nh_orig == NULL)
1062 if (NH_IS_NHGRP(nh_orig))
1063 return (change_mpath_route(rnh, rt, info, rnd_orig, rc));
1066 rnd_new.rnd_weight = get_info_weight(info, rnd_orig->rnd_weight);
1067 error = change_nhop(rnh, info, nh_orig, &rnd_new.rnd_nhop);
1070 error = change_route_conditional(rnh, rt, rnd_orig, &rnd_new, rc);
1076 * Insert @rt with nhop data from @rnd_new to @rnh.
1077 * Returns 0 on success and stores operation results in @rc.
1080 add_route(struct rib_head *rnh, struct rtentry *rt,
1081 struct route_nhop_data *rnd, struct rib_cmd_info *rc)
1083 struct radix_node *rn;
1085 RIB_WLOCK_ASSERT(rnh);
1087 rt->rt_nhop = rnd->rnd_nhop;
1088 rt->rt_weight = rnd->rnd_weight;
1089 rn = rnh->rnh_addaddr(rt_key(rt), rt_mask_const(rt), &rnh->head, rt->rt_nodes);
1092 if (!NH_IS_NHGRP(rnd->rnd_nhop) && nhop_get_expire(rnd->rnd_nhop))
1093 tmproutes_update(rnh, rt, rnd->rnd_nhop);
1095 /* Finalize notification */
1097 rnh->rnh_prefixes++;
1099 rc->rc_cmd = RTM_ADD;
1101 rc->rc_nh_old = NULL;
1102 rc->rc_nh_new = rnd->rnd_nhop;
1103 rc->rc_nh_weight = rnd->rnd_weight;
1105 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1109 /* Existing route or memory allocation failure. */
1114 * Unconditionally deletes @rt from @rnh.
1117 delete_route(struct rib_head *rnh, struct rtentry *rt, struct rib_cmd_info *rc)
1119 RIB_WLOCK_ASSERT(rnh);
1121 /* Route deletion requested. */
1122 struct radix_node *rn;
1124 rn = rnh->rnh_deladdr(rt_key_const(rt), rt_mask_const(rt), &rnh->head);
1128 rt->rte_flags &= ~RTF_UP;
1131 rnh->rnh_prefixes--;
1133 rc->rc_cmd = RTM_DELETE;
1135 rc->rc_nh_old = rt->rt_nhop;
1136 rc->rc_nh_new = NULL;
1137 rc->rc_nh_weight = rt->rt_weight;
1139 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1145 * Switch @rt nhop/weigh to the ones specified in @rnd.
1146 * Returns 0 on success.
1149 change_route(struct rib_head *rnh, struct rtentry *rt,
1150 struct route_nhop_data *rnd, struct rib_cmd_info *rc)
1152 struct nhop_object *nh_orig;
1154 RIB_WLOCK_ASSERT(rnh);
1156 nh_orig = rt->rt_nhop;
1158 if (rnd->rnd_nhop == NULL)
1159 return (delete_route(rnh, rt, rc));
1161 /* Changing nexthop & weight to a new one */
1162 rt->rt_nhop = rnd->rnd_nhop;
1163 rt->rt_weight = rnd->rnd_weight;
1164 if (!NH_IS_NHGRP(rnd->rnd_nhop) && nhop_get_expire(rnd->rnd_nhop))
1165 tmproutes_update(rnh, rt, rnd->rnd_nhop);
1167 /* Finalize notification */
1169 rc->rc_cmd = RTM_CHANGE;
1171 rc->rc_nh_old = nh_orig;
1172 rc->rc_nh_new = rnd->rnd_nhop;
1173 rc->rc_nh_weight = rnd->rnd_weight;
1175 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1181 * Conditionally update route nhop/weight IFF data in @nhd_orig is
1182 * consistent with the current route data.
1183 * Nexthop in @nhd_new is consumed.
1186 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
1187 struct route_nhop_data *rnd_orig, struct route_nhop_data *rnd_new,
1188 struct rib_cmd_info *rc)
1190 struct rtentry *rt_new;
1193 #if DEBUG_MAX_LEVEL >= LOG_DEBUG2
1195 char buf_old[NHOP_PRINT_BUFSIZE], buf_new[NHOP_PRINT_BUFSIZE];
1196 nhop_print_buf_any(rnd_orig->rnd_nhop, buf_old, NHOP_PRINT_BUFSIZE);
1197 nhop_print_buf_any(rnd_new->rnd_nhop, buf_new, NHOP_PRINT_BUFSIZE);
1198 FIB_LOG(LOG_DEBUG2, rnh->rib_fibnum, rnh->rib_family,
1199 "trying change %s -> %s", buf_old, buf_new);
1204 struct route_nhop_data rnd;
1205 rt_new = lookup_prefix_rt(rnh, rt, &rnd);
1207 if (rt_new == NULL) {
1208 if (rnd_orig->rnd_nhop == NULL)
1209 error = add_route(rnh, rt, rnd_new, rc);
1212 * Prefix does not exist, which was not our assumption.
1213 * Update @rnd_orig with the new data and return
1215 rnd_orig->rnd_nhop = NULL;
1216 rnd_orig->rnd_weight = 0;
1220 /* Prefix exists, try to update */
1221 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
1223 * Nhop/mpath group hasn't changed. Flip
1224 * to the new precalculated one and return
1226 error = change_route(rnh, rt_new, rnd_new, rc);
1228 /* Update and retry */
1229 rnd_orig->rnd_nhop = rt_new->rt_nhop;
1230 rnd_orig->rnd_weight = rt_new->rt_weight;
1238 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
1240 if (rnd_orig->rnd_nhop != NULL)
1241 nhop_free_any(rnd_orig->rnd_nhop);
1244 if (rnd_new->rnd_nhop != NULL)
1245 nhop_free_any(rnd_new->rnd_nhop);
1252 * Performs modification of routing table specificed by @action.
1253 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
1254 * Needs to be run in network epoch.
1256 * Returns 0 on success and fills in @rc with action result.
1259 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
1260 struct rib_cmd_info *rc)
1266 error = rib_add_route(fibnum, info, rc);
1269 error = rib_del_route(fibnum, info, rc);
1272 error = rib_change_route(fibnum, info, rc);
1283 struct rib_head *rnh;
1284 struct rtentry *head;
1285 rib_filter_f_t *filter_f;
1288 struct rib_cmd_info rc;
1292 * Conditionally unlinks rtenties or paths from radix tree based
1293 * on the callback data passed in @arg.
1296 rt_checkdelroute(struct radix_node *rn, void *arg)
1298 struct rt_delinfo *di = (struct rt_delinfo *)arg;
1299 struct rtentry *rt = (struct rtentry *)rn;
1301 if (rt_delete_conditional(di->rnh, rt, di->prio,
1302 di->filter_f, di->filter_arg, &di->rc) != 0)
1306 * Add deleted rtentries to the list to GC them
1307 * after dropping the lock.
1309 * XXX: Delayed notifications not implemented
1310 * for nexthop updates.
1312 if (di->rc.rc_cmd == RTM_DELETE) {
1313 /* Add to the list and return */
1314 rt->rt_chain = di->head;
1319 * RTM_CHANGE to a different nexthop or nexthop group.
1320 * Free old multipath group.
1322 nhop_free_any(di->rc.rc_nh_old);
1330 * Iterates over a routing table specified by @fibnum and @family and
1331 * deletes elements marked by @filter_f.
1332 * @fibnum: rtable id
1333 * @family: AF_ address family
1334 * @filter_f: function returning non-zero value for items to delete
1335 * @arg: data to pass to the @filter_f function
1336 * @report: true if rtsock notification is needed.
1339 rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f, void *filter_arg,
1342 struct rib_head *rnh;
1344 struct nhop_object *nh;
1345 struct epoch_tracker et;
1347 rnh = rt_tables_get_rnh(fibnum, family);
1351 struct rt_delinfo di = {
1353 .filter_f = filter_f,
1354 .filter_arg = filter_arg,
1355 .prio = NH_PRIORITY_NORMAL,
1358 NET_EPOCH_ENTER(et);
1361 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1364 /* We might have something to reclaim. */
1365 bzero(&di.rc, sizeof(di.rc));
1366 di.rc.rc_cmd = RTM_DELETE;
1367 while (di.head != NULL) {
1369 di.head = rt->rt_chain;
1370 rt->rt_chain = NULL;
1374 di.rc.rc_nh_old = nh;
1375 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1379 struct nhgrp_object *nhg;
1380 const struct weightened_nhop *wn;
1382 if (NH_IS_NHGRP(nh)) {
1383 nhg = (struct nhgrp_object *)nh;
1384 wn = nhgrp_get_nhops(nhg, &num_nhops);
1385 for (int i = 0; i < num_nhops; i++)
1386 rt_routemsg(RTM_DELETE, rt, wn[i].nh, fibnum);
1389 rt_routemsg(RTM_DELETE, rt, nh, fibnum);
1398 rt_delete_unconditional(struct radix_node *rn, void *arg)
1400 struct rtentry *rt = RNTORT(rn);
1401 struct rib_head *rnh = (struct rib_head *)arg;
1403 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), &rnh->head);
1404 if (RNTORT(rn) == rt)
1411 * Removes all routes from the routing table without executing notifications.
1412 * rtentres will be removed after the end of a current epoch.
1415 rib_flush_routes(struct rib_head *rnh)
1418 rnh->rnh_walktree(&rnh->head, rt_delete_unconditional, rnh);
1423 rib_flush_routes_family(int family)
1425 struct rib_head *rnh;
1427 for (uint32_t fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1428 if ((rnh = rt_tables_get_rnh(fibnum, family)) != NULL)
1429 rib_flush_routes(rnh);
1434 rib_print_family(int family)
1448 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1449 struct rib_cmd_info *rc)
1451 struct rib_subscription *rs;
1453 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1454 if (rs->type == type)
1455 rs->func(rnh, rc, rs->arg);
1459 static struct rib_subscription *
1460 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1461 enum rib_subscription_type type, bool waitok)
1463 struct rib_subscription *rs;
1464 int flags = M_ZERO | (waitok ? M_WAITOK : M_NOWAIT);
1466 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1478 * Subscribe for the changes in the routing table specified by @fibnum and
1481 * Returns pointer to the subscription structure on success.
1483 struct rib_subscription *
1484 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1485 enum rib_subscription_type type, bool waitok)
1487 struct rib_head *rnh;
1488 struct epoch_tracker et;
1490 NET_EPOCH_ENTER(et);
1491 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1492 rnh = rt_tables_get_rnh(fibnum, family);
1495 return (rib_subscribe_internal(rnh, f, arg, type, waitok));
1498 struct rib_subscription *
1499 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1500 enum rib_subscription_type type, bool waitok)
1502 struct rib_subscription *rs;
1503 struct epoch_tracker et;
1505 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1509 NET_EPOCH_ENTER(et);
1511 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1518 struct rib_subscription *
1519 rib_subscribe_locked(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1520 enum rib_subscription_type type)
1522 struct rib_subscription *rs;
1525 RIB_WLOCK_ASSERT(rnh);
1527 if ((rs = allocate_subscription(f, arg, type, false)) == NULL)
1531 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1537 * Remove rtable subscription @rs from the routing table.
1538 * Needs to be run in network epoch.
1541 rib_unsubscribe(struct rib_subscription *rs)
1543 struct rib_head *rnh = rs->rnh;
1548 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1551 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1556 rib_unsubscribe_locked(struct rib_subscription *rs)
1558 struct rib_head *rnh = rs->rnh;
1561 RIB_WLOCK_ASSERT(rnh);
1563 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1565 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1570 * Epoch callback indicating subscription is safe to destroy
1573 destroy_subscription_epoch(epoch_context_t ctx)
1575 struct rib_subscription *rs;
1577 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1583 rib_init_subscriptions(struct rib_head *rnh)
1586 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1590 rib_destroy_subscriptions(struct rib_head *rnh)
1592 struct rib_subscription *rs;
1593 struct epoch_tracker et;
1595 NET_EPOCH_ENTER(et);
1597 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1598 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1599 epoch_call(net_epoch_preempt, destroy_subscription_epoch,