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(struct rib_head *rnh, struct rt_addrinfo *info,
81 struct rib_cmd_info *rc);
82 static int add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
83 struct route_nhop_data *rnd, struct rib_cmd_info *rc);
84 static int del_route(struct rib_head *rnh, struct rt_addrinfo *info,
85 struct rib_cmd_info *rc);
86 static int change_route(struct rib_head *rnh, struct rt_addrinfo *info,
87 struct route_nhop_data *nhd_orig, struct rib_cmd_info *rc);
89 static int rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info,
90 struct rib_cmd_info *rc);
92 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
93 struct rib_cmd_info *rc);
95 static void destroy_subscription_epoch(epoch_context_t ctx);
97 static bool rib_can_multipath(struct rib_head *rh);
100 /* Per-vnet multipath routing configuration */
101 SYSCTL_DECL(_net_route);
102 #define V_rib_route_multipath VNET(rib_route_multipath)
104 #define _MP_FLAGS CTLFLAG_RW
106 #define _MP_FLAGS CTLFLAG_RD
108 VNET_DEFINE(u_int, rib_route_multipath) = 1;
109 SYSCTL_UINT(_net_route, OID_AUTO, multipath, _MP_FLAGS | CTLFLAG_VNET,
110 &VNET_NAME(rib_route_multipath), 0, "Enable route multipath");
113 #if defined(INET) && defined(INET6)
114 FEATURE(ipv4_rfc5549_support, "Route IPv4 packets via IPv6 nexthops");
115 #define V_rib_route_ipv6_nexthop VNET(rib_route_ipv6_nexthop)
116 VNET_DEFINE(u_int, rib_route_ipv6_nexthop) = 1;
117 SYSCTL_UINT(_net_route, OID_AUTO, ipv6_nexthop, CTLFLAG_RW | CTLFLAG_VNET,
118 &VNET_NAME(rib_route_ipv6_nexthop), 0, "Enable IPv4 route via IPv6 Next Hop address");
121 /* Routing table UMA zone */
122 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
123 #define V_rtzone VNET(rtzone)
126 SYSCTL_NODE(_net_route, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
129 vnet_rtzone_init(void)
132 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
133 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
138 vnet_rtzone_destroy(void)
141 uma_zdestroy(V_rtzone);
146 destroy_rtentry(struct rtentry *rt)
149 struct nhop_object *nh = rt->rt_nhop;
152 * At this moment rnh, nh_control may be already freed.
153 * nhop interface may have been migrated to a different vnet.
154 * Use vnet stored in the nexthop to delete the entry.
157 if (NH_IS_NHGRP(nh)) {
158 const struct weightened_nhop *wn;
160 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
164 CURVNET_SET(nhop_get_vnet(nh));
167 /* Unreference nexthop */
168 nhop_free_any(rt->rt_nhop);
170 uma_zfree(V_rtzone, rt);
176 * Epoch callback indicating rtentry is safe to destroy
179 destroy_rtentry_epoch(epoch_context_t ctx)
183 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
189 * Schedule rtentry deletion
192 rtfree(struct rtentry *rt)
195 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
197 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
201 static struct rib_head *
202 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
204 struct rib_head *rnh;
205 struct sockaddr *dst;
207 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
209 dst = info->rti_info[RTAX_DST];
210 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
215 #if defined(INET) && defined(INET6)
217 rib_can_ipv6_nexthop_address(struct rib_head *rh)
221 CURVNET_SET(rh->rib_vnet);
222 result = !!V_rib_route_ipv6_nexthop;
231 rib_can_multipath(struct rib_head *rh)
235 CURVNET_SET(rh->rib_vnet);
236 result = !!V_rib_route_multipath;
243 * Check is nhop is multipath-eligible.
244 * Avoid nhops without gateways and redirects.
246 * Returns 1 for multipath-eligible nexthop,
250 nhop_can_multipath(const struct nhop_object *nh)
253 if ((nh->nh_flags & NHF_MULTIPATH) != 0)
255 if ((nh->nh_flags & NHF_GATEWAY) == 0)
257 if ((nh->nh_flags & NHF_REDIRECT) != 0)
265 get_info_weight(const struct rt_addrinfo *info, uint32_t default_weight)
269 if (info->rti_mflags & RTV_WEIGHT)
270 weight = info->rti_rmx->rmx_weight;
272 weight = default_weight;
273 /* Keep upper 1 byte for adm distance purposes */
274 if (weight > RT_MAX_WEIGHT)
275 weight = RT_MAX_WEIGHT;
276 else if (weight == 0)
277 weight = default_weight;
283 rt_is_host(const struct rtentry *rt)
286 return (rt->rte_flags & RTF_HOST);
290 rt_get_family(const struct rtentry *rt)
292 const struct sockaddr *dst;
294 dst = (const struct sockaddr *)rt_key_const(rt);
296 return (dst->sa_family);
300 * Returns pointer to nexthop or nexthop group
301 * associated with @rt
304 rt_get_raw_nhop(const struct rtentry *rt)
307 return (rt->rt_nhop);
312 * Stores IPv4 address and prefix length of @rt inside
314 * @pscopeid is currently always set to 0.
317 rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
318 int *plen, uint32_t *pscopeid)
320 const struct sockaddr_in *dst;
322 dst = (const struct sockaddr_in *)rt_key_const(rt);
323 KASSERT((dst->sin_family == AF_INET),
324 ("rt family is %d, not inet", dst->sin_family));
325 *paddr = dst->sin_addr;
326 dst = (const struct sockaddr_in *)rt_mask_const(rt);
330 *plen = bitcount32(dst->sin_addr.s_addr);
335 * Stores IPv4 address and prefix mask of @rt inside
336 * @paddr and @pmask. Sets mask to INADDR_ANY for host routes.
337 * @pscopeid is currently always set to 0.
340 rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
341 struct in_addr *pmask, uint32_t *pscopeid)
343 const struct sockaddr_in *dst;
345 dst = (const struct sockaddr_in *)rt_key_const(rt);
346 KASSERT((dst->sin_family == AF_INET),
347 ("rt family is %d, not inet", dst->sin_family));
348 *paddr = dst->sin_addr;
349 dst = (const struct sockaddr_in *)rt_mask_const(rt);
351 pmask->s_addr = INADDR_BROADCAST;
353 *pmask = dst->sin_addr;
360 inet6_get_plen(const struct in6_addr *addr)
363 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
364 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
368 * Stores IPv6 address and prefix length of @rt inside
369 * @paddr and @plen. Addresses are returned in de-embedded form.
370 * Scopeid is set to 0 for non-LL addresses.
373 rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
374 int *plen, uint32_t *pscopeid)
376 const struct sockaddr_in6 *dst;
378 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
379 KASSERT((dst->sin6_family == AF_INET6),
380 ("rt family is %d, not inet6", dst->sin6_family));
381 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
382 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
384 *paddr = dst->sin6_addr;
385 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
389 *plen = inet6_get_plen(&dst->sin6_addr);
393 * Stores IPv6 address and prefix mask of @rt inside
394 * @paddr and @pmask. Addresses are returned in de-embedded form.
395 * Scopeid is set to 0 for non-LL addresses.
398 rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
399 struct in6_addr *pmask, uint32_t *pscopeid)
401 const struct sockaddr_in6 *dst;
403 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
404 KASSERT((dst->sin6_family == AF_INET6),
405 ("rt family is %d, not inet", dst->sin6_family));
406 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
407 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
409 *paddr = dst->sin6_addr;
410 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
412 memset(pmask, 0xFF, sizeof(struct in6_addr));
414 *pmask = dst->sin6_addr;
419 * Check if specified @gw matches gw data in the nexthop @nh.
421 * Returns true if matches, false otherwise.
424 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
427 if (nh->gw_sa.sa_family != gw->sa_family)
430 switch (gw->sa_family) {
432 return (nh->gw4_sa.sin_addr.s_addr ==
433 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
436 const struct sockaddr_in6 *gw6;
437 gw6 = (const struct sockaddr_in6 *)gw;
440 * Currently (2020-09) IPv6 gws in kernel have their
441 * scope embedded. Once this becomes false, this code
442 * has to be revisited.
444 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
451 const struct sockaddr_dl *sdl;
452 sdl = (const struct sockaddr_dl *)gw;
453 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
456 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
464 * Checks if data in @info matches nexhop @nh.
466 * Returns 0 on success,
467 * ESRCH if not matched,
468 * ENOENT if filter function returned false
471 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
472 const struct nhop_object *nh)
474 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
476 if (info->rti_filter != NULL) {
477 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
482 if ((gw != NULL) && !match_nhop_gw(nh, gw))
489 * Checks if nexhop @nh can be rewritten by data in @info because
490 * of higher "priority". Currently the only case for such scenario
491 * is kernel installing interface routes, marked by RTF_PINNED flag.
494 * 1 if @info data has higher priority
495 * 0 if priority is the same
496 * -1 if priority is lower
499 can_override_nhop(const struct rt_addrinfo *info, const struct nhop_object *nh)
502 if (info->rti_flags & RTF_PINNED) {
503 return (NH_IS_PINNED(nh)) ? 0 : 1;
505 return (NH_IS_PINNED(nh)) ? -1 : 0;
510 * Runs exact prefix match based on @dst and @netmask.
511 * Returns matched @rtentry if found or NULL.
512 * If rtentry was found, saves nexthop / weight value into @rnd.
514 static struct rtentry *
515 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
516 const struct sockaddr *netmask, struct route_nhop_data *rnd)
520 RIB_LOCK_ASSERT(rnh);
522 rt = (struct rtentry *)rnh->rnh_lookup(__DECONST(void *, dst),
523 __DECONST(void *, netmask), &rnh->head);
525 rnd->rnd_nhop = rt->rt_nhop;
526 rnd->rnd_weight = rt->rt_weight;
528 rnd->rnd_nhop = NULL;
536 * Runs exact prefix match based on dst/netmask from @info.
537 * Assumes RIB lock is held.
538 * Returns matched @rtentry if found or NULL.
539 * If rtentry was found, saves nexthop / weight value into @rnd.
542 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
543 struct route_nhop_data *rnd)
547 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
548 info->rti_info[RTAX_NETMASK], rnd);
554 * Adds route defined by @info into the kernel table specified by @fibnum and
555 * sa_family in @info->rti_info[RTAX_DST].
557 * Returns 0 on success and fills in operation metadata into @rc.
560 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
561 struct rib_cmd_info *rc)
563 struct rib_head *rnh;
568 rnh = get_rnh(fibnum, info);
570 return (EAFNOSUPPORT);
573 * Check consistency between RTF_HOST flag and netmask
576 if (info->rti_flags & RTF_HOST)
577 info->rti_info[RTAX_NETMASK] = NULL;
578 else if (info->rti_info[RTAX_NETMASK] == NULL) {
579 FIB_RH_LOG(LOG_DEBUG, rnh, "error: no RTF_HOST and empty netmask");
583 bzero(rc, sizeof(struct rib_cmd_info));
584 rc->rc_cmd = RTM_ADD;
586 error = add_route(rnh, info, rc);
588 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
594 * Checks if @dst and @gateway is valid combination.
596 * Returns true if is valid, false otherwise.
599 check_gateway(struct rib_head *rnh, struct sockaddr *dst,
600 struct sockaddr *gateway)
602 if (dst->sa_family == gateway->sa_family)
604 else if (gateway->sa_family == AF_UNSPEC)
606 else if (gateway->sa_family == AF_LINK)
608 #if defined(INET) && defined(INET6)
609 else if (dst->sa_family == AF_INET && gateway->sa_family == AF_INET6 &&
610 rib_can_ipv6_nexthop_address(rnh))
618 * Creates rtentry and nexthop based on @info data.
619 * Return 0 and fills in rtentry into @prt on success,
620 * Note: rtentry mask will be set to RTAX_NETMASK, thus its pointer is required
621 * to be stable till the end of the operation (radix rt insertion/change/removal).
622 * return errno otherwise.
625 create_rtentry(struct rib_head *rnh, struct rt_addrinfo *info,
626 struct rtentry **prt)
628 struct sockaddr *dst, *ndst, *gateway, *netmask;
630 struct nhop_object *nh;
633 dst = info->rti_info[RTAX_DST];
634 gateway = info->rti_info[RTAX_GATEWAY];
635 netmask = info->rti_info[RTAX_NETMASK];
636 flags = info->rti_flags;
638 if ((flags & RTF_GATEWAY) && !gateway) {
639 FIB_RH_LOG(LOG_DEBUG, rnh, "error: RTF_GATEWAY set with empty gw");
642 if (dst && gateway && !check_gateway(rnh, dst, gateway)) {
643 FIB_RH_LOG(LOG_DEBUG, rnh,
644 "error: invalid dst/gateway family combination (%d, %d)",
645 dst->sa_family, gateway->sa_family);
649 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb)) {
650 FIB_RH_LOG(LOG_DEBUG, rnh, "error: dst->sa_len too large: %d",
655 if (info->rti_ifa == NULL) {
656 error = rt_getifa_fib(info, rnh->rib_fibnum);
661 error = nhop_create_from_info(rnh, info, &nh);
665 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
670 rt->rte_flags = (RTF_UP | flags) & RTE_RT_FLAG_MASK;
674 memcpy(&rt->rt_dst, dst, dst->sa_len);
675 rt_key(rt) = &rt->rt_dst;
678 * point to the (possibly newly malloc'd) dest address.
680 ndst = (struct sockaddr *)rt_key(rt);
683 * make sure it contains the value we want (masked if needed).
686 rt_maskedcopy(dst, ndst, netmask);
688 bcopy(dst, ndst, dst->sa_len);
689 /* Set netmask to the storage from info. It will be updated upon insertion */
690 rt_mask(rt) = netmask;
693 * We use the ifa reference returned by rt_getifa_fib().
694 * This moved from below so that rnh->rnh_addaddr() can
695 * examine the ifa and ifa->ifa_ifp if it so desires.
697 rt->rt_weight = get_info_weight(info, RT_DEFAULT_WEIGHT);
704 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
705 struct rib_cmd_info *rc)
707 struct nhop_object *nh_orig;
708 struct route_nhop_data rnd_orig, rnd_add;
709 struct nhop_object *nh;
710 struct rtentry *rt, *rt_orig;
713 error = create_rtentry(rnh, info, &rt);
717 rnd_add.rnd_nhop = rt->rt_nhop;
718 rnd_add.rnd_weight = rt->rt_weight;
722 error = add_route_nhop(rnh, rt, &rnd_add, rc);
728 /* addition failed. Lookup prefix in the rib to determine the cause */
729 rt_orig = lookup_prefix(rnh, info, &rnd_orig);
730 if (rt_orig == NULL) {
731 /* No prefix -> rnh_addaddr() failed to allocate memory */
734 uma_zfree(V_rtzone, rt);
738 /* We have existing route in the RIB. */
739 nh_orig = rnd_orig.rnd_nhop;
740 /* Check if new route has higher preference */
741 if (can_override_nhop(info, nh_orig) > 0) {
742 /* Update nexthop to the new route */
743 change_route_nhop(rnh, rt_orig, &rnd_add, rc);
745 uma_zfree(V_rtzone, rt);
753 if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) &&
754 nhop_can_multipath(rnd_orig.rnd_nhop))
755 error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc);
758 /* Unable to add - another route with the same preference exists */
762 * ROUTE_MPATH disabled: failed to add route, free both nhop and rt.
763 * ROUTE_MPATH enabled: original nhop reference is unused in any case,
764 * free rt only if not _adding_ new route to rib (e.g. the case
765 * when initial lookup returned existing route, but then it got
766 * deleted prior to multipath group insertion, leading to a simple
767 * non-multipath add as a result).
770 if ((error != 0) || rc->rc_cmd != RTM_ADD)
771 uma_zfree(V_rtzone, rt);
777 * Removes route defined by @info from the kernel table specified by @fibnum and
778 * sa_family in @info->rti_info[RTAX_DST].
780 * Returns 0 on success and fills in operation metadata into @rc.
783 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
785 struct rib_head *rnh;
786 struct sockaddr *dst_orig, *netmask;
787 struct sockaddr_storage mdst;
792 rnh = get_rnh(fibnum, info);
794 return (EAFNOSUPPORT);
796 bzero(rc, sizeof(struct rib_cmd_info));
797 rc->rc_cmd = RTM_DELETE;
799 dst_orig = info->rti_info[RTAX_DST];
800 netmask = info->rti_info[RTAX_NETMASK];
802 if (netmask != NULL) {
803 /* Ensure @dst is always properly masked */
804 if (dst_orig->sa_len > sizeof(mdst)) {
805 FIB_RH_LOG(LOG_DEBUG, rnh, "error: dst->sa_len too large");
808 rt_maskedcopy(dst_orig, (struct sockaddr *)&mdst, netmask);
809 info->rti_info[RTAX_DST] = (struct sockaddr *)&mdst;
811 error = del_route(rnh, info, rc);
812 info->rti_info[RTAX_DST] = dst_orig;
818 * Conditionally unlinks rtentry matching data inside @info from @rnh.
819 * Returns 0 on success with operation result stored in @rc.
821 * ESRCH - if prefix was not found,
822 * EADDRINUSE - if trying to delete higher priority route.
823 * ENOENT - if supplied filter function returned 0 (not matched).
826 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, struct rib_cmd_info *rc)
829 struct nhop_object *nh;
830 struct radix_node *rn;
831 struct route_nhop_data rnd;
834 rt = lookup_prefix(rnh, info, &rnd);
840 if (NH_IS_NHGRP(nh)) {
841 error = del_route_mpath(rnh, info, rt,
842 (struct nhgrp_object *)nh, rc);
846 error = check_info_match_nhop(info, rt, nh);
850 if (can_override_nhop(info, nh) < 0)
854 * Remove the item from the tree and return it.
855 * Complain if it is not there and do no more processing.
857 rn = rnh->rnh_deladdr(rt_key_const(rt), rt_mask_const(rt), &rnh->head);
861 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
862 panic ("rtrequest delete");
865 rt->rte_flags &= ~RTF_UP;
867 /* Finalize notification */
871 rc->rc_cmd = RTM_DELETE;
873 rc->rc_nh_old = rt->rt_nhop;
874 rc->rc_nh_weight = rt->rt_weight;
875 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
881 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
882 struct rib_cmd_info *rc)
887 error = rt_unlinkrte(rnh, info, rc);
892 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
895 * If the caller wants it, then it can have it,
896 * the entry will be deleted after the end of the current epoch.
898 if (rc->rc_cmd == RTM_DELETE)
903 * Deleting 1 path may result in RTM_CHANGE to
904 * a different mpath group/nhop.
905 * Free old mpath group.
907 nhop_free_any(rc->rc_nh_old);
915 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
916 struct rib_cmd_info *rc)
919 struct route_nhop_data rnd_orig;
920 struct rib_head *rnh;
926 rnh = get_rnh(fibnum, info);
928 return (EAFNOSUPPORT);
930 bzero(rc, sizeof(struct rib_cmd_info));
931 rc->rc_cmd = RTM_CHANGE;
933 /* Check if updated gateway exists */
934 if ((info->rti_flags & RTF_GATEWAY) &&
935 (info->rti_info[RTAX_GATEWAY] == NULL)) {
938 * route(8) adds RTF_GATEWAY flag if -interface is not set.
939 * Remove RTF_GATEWAY to enforce consistency and maintain
942 info->rti_flags &= ~RTF_GATEWAY;
946 * route change is done in multiple steps, with dropping and
947 * reacquiring lock. In the situations with multiple processes
948 * changes the same route in can lead to the case when route
949 * is changed between the steps. Address it by retrying the operation
950 * multiple times before failing.
954 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
955 info->rti_info[RTAX_NETMASK], &rnh->head);
962 rnd_orig.rnd_nhop = rt->rt_nhop;
963 rnd_orig.rnd_weight = rt->rt_weight;
967 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
968 error = change_route(rnh, info, &rnd_orig, rc);
977 change_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
978 struct nhop_object *nh_orig, struct nhop_object **nh_new)
983 * New gateway could require new ifaddr, ifp;
984 * flags may also be different; ifp may be specified
985 * by ll sockaddr when protocol address is ambiguous
987 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
988 info->rti_info[RTAX_GATEWAY] != NULL) ||
989 info->rti_info[RTAX_IFP] != NULL ||
990 (info->rti_info[RTAX_IFA] != NULL &&
991 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
992 error = rt_getifa_fib(info, rnh->rib_fibnum);
995 info->rti_ifa = NULL;
1000 error = nhop_create_from_nhop(rnh, nh_orig, info, nh_new);
1001 info->rti_ifa = NULL;
1008 change_mpath_route(struct rib_head *rnh, struct rt_addrinfo *info,
1009 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
1011 int error = 0, found_idx = 0;
1012 struct nhop_object *nh_orig = NULL, *nh_new;
1013 struct route_nhop_data rnd_new = {};
1014 const struct weightened_nhop *wn = NULL;
1015 struct weightened_nhop *wn_new;
1018 wn = nhgrp_get_nhops(rnd_orig->rnd_nhgrp, &num_nhops);
1019 for (int i = 0; i < num_nhops; i++) {
1020 if (check_info_match_nhop(info, NULL, wn[i].nh) == 0) {
1027 if (nh_orig == NULL)
1030 error = change_nhop(rnh, info, nh_orig, &nh_new);
1034 wn_new = mallocarray(num_nhops, sizeof(struct weightened_nhop),
1035 M_TEMP, M_NOWAIT | M_ZERO);
1036 if (wn_new == NULL) {
1041 memcpy(wn_new, wn, num_nhops * sizeof(struct weightened_nhop));
1042 wn_new[found_idx].nh = nh_new;
1043 wn_new[found_idx].weight = get_info_weight(info, wn[found_idx].weight);
1045 error = nhgrp_get_group(rnh, wn_new, num_nhops, &rnd_new.rnd_nhgrp);
1047 free(wn_new, M_TEMP);
1052 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1059 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
1060 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
1063 struct nhop_object *nh_orig;
1064 struct route_nhop_data rnd_new;
1066 nh_orig = rnd_orig->rnd_nhop;
1067 if (nh_orig == NULL)
1071 if (NH_IS_NHGRP(nh_orig))
1072 return (change_mpath_route(rnh, info, rnd_orig, rc));
1075 rnd_new.rnd_weight = get_info_weight(info, rnd_orig->rnd_weight);
1076 error = change_nhop(rnh, info, nh_orig, &rnd_new.rnd_nhop);
1079 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1085 * Insert @rt with nhop data from @rnd_new to @rnh.
1086 * Returns 0 on success and stores operation results in @rc.
1089 add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1090 struct route_nhop_data *rnd, struct rib_cmd_info *rc)
1092 struct radix_node *rn;
1095 RIB_WLOCK_ASSERT(rnh);
1097 rt->rt_nhop = rnd->rnd_nhop;
1098 rt->rt_weight = rnd->rnd_weight;
1099 rn = rnh->rnh_addaddr(rt_key(rt), rt_mask_const(rt), &rnh->head, rt->rt_nodes);
1102 if (!NH_IS_NHGRP(rnd->rnd_nhop) && nhop_get_expire(rnd->rnd_nhop))
1103 tmproutes_update(rnh, rt, rnd->rnd_nhop);
1105 /* Finalize notification */
1107 rnh->rnh_prefixes++;
1109 rc->rc_cmd = RTM_ADD;
1111 rc->rc_nh_old = NULL;
1112 rc->rc_nh_new = rnd->rnd_nhop;
1113 rc->rc_nh_weight = rnd->rnd_weight;
1115 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1117 /* Existing route or memory allocation failure */
1125 * Switch @rt nhop/weigh to the ones specified in @rnd.
1126 * Returns 0 on success.
1129 change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1130 struct route_nhop_data *rnd, struct rib_cmd_info *rc)
1132 struct nhop_object *nh_orig;
1134 RIB_WLOCK_ASSERT(rnh);
1136 nh_orig = rt->rt_nhop;
1138 if (rnd->rnd_nhop != NULL) {
1139 /* Changing nexthop & weight to a new one */
1140 rt->rt_nhop = rnd->rnd_nhop;
1141 rt->rt_weight = rnd->rnd_weight;
1142 if (!NH_IS_NHGRP(rnd->rnd_nhop) && nhop_get_expire(rnd->rnd_nhop))
1143 tmproutes_update(rnh, rt, rnd->rnd_nhop);
1145 /* Route deletion requested. */
1146 struct radix_node *rn;
1148 rn = rnh->rnh_deladdr(rt_key_const(rt), rt_mask_const(rt), &rnh->head);
1152 rt->rte_flags &= ~RTF_UP;
1155 /* Finalize notification */
1157 if (rnd->rnd_nhop == NULL)
1158 rnh->rnh_prefixes--;
1160 rc->rc_cmd = (rnd->rnd_nhop != NULL) ? RTM_CHANGE : RTM_DELETE;
1162 rc->rc_nh_old = nh_orig;
1163 rc->rc_nh_new = rnd->rnd_nhop;
1164 rc->rc_nh_weight = rnd->rnd_weight;
1166 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1172 * Conditionally update route nhop/weight IFF data in @nhd_orig is
1173 * consistent with the current route data.
1174 * Nexthop in @nhd_new is consumed.
1177 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
1178 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
1179 struct route_nhop_data *rnd_new, struct rib_cmd_info *rc)
1181 struct rtentry *rt_new;
1184 #if DEBUG_MAX_LEVEL >= LOG_DEBUG2
1186 char buf_old[NHOP_PRINT_BUFSIZE], buf_new[NHOP_PRINT_BUFSIZE];
1187 nhop_print_buf_any(rnd_orig->rnd_nhop, buf_old, NHOP_PRINT_BUFSIZE);
1188 nhop_print_buf_any(rnd_new->rnd_nhop, buf_new, NHOP_PRINT_BUFSIZE);
1189 FIB_LOG(LOG_DEBUG2, rnh->rib_fibnum, rnh->rib_family,
1190 "trying change %s -> %s", buf_old, buf_new);
1195 rt_new = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1196 info->rti_info[RTAX_NETMASK], &rnh->head);
1198 if (rt_new == NULL) {
1199 if (rnd_orig->rnd_nhop == NULL)
1200 error = add_route_nhop(rnh, rt, rnd_new, rc);
1203 * Prefix does not exist, which was not our assumption.
1204 * Update @rnd_orig with the new data and return
1206 rnd_orig->rnd_nhop = NULL;
1207 rnd_orig->rnd_weight = 0;
1211 /* Prefix exists, try to update */
1212 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
1214 * Nhop/mpath group hasn't changed. Flip
1215 * to the new precalculated one and return
1217 error = change_route_nhop(rnh, rt_new, rnd_new, rc);
1219 /* Update and retry */
1220 rnd_orig->rnd_nhop = rt_new->rt_nhop;
1221 rnd_orig->rnd_weight = rt_new->rt_weight;
1229 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
1231 if (rnd_orig->rnd_nhop != NULL)
1232 nhop_free_any(rnd_orig->rnd_nhop);
1235 if (rnd_new->rnd_nhop != NULL)
1236 nhop_free_any(rnd_new->rnd_nhop);
1243 * Performs modification of routing table specificed by @action.
1244 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
1245 * Needs to be run in network epoch.
1247 * Returns 0 on success and fills in @rc with action result.
1250 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
1251 struct rib_cmd_info *rc)
1257 error = rib_add_route(fibnum, info, rc);
1260 error = rib_del_route(fibnum, info, rc);
1263 error = rib_change_route(fibnum, info, rc);
1274 struct rt_addrinfo info;
1275 struct rib_head *rnh;
1276 struct rtentry *head;
1277 struct rib_cmd_info rc;
1281 * Conditionally unlinks @rn from radix tree based
1282 * on info data passed in @arg.
1285 rt_checkdelroute(struct radix_node *rn, void *arg)
1287 struct rt_delinfo *di;
1288 struct rt_addrinfo *info;
1291 di = (struct rt_delinfo *)arg;
1292 rt = (struct rtentry *)rn;
1295 info->rti_info[RTAX_DST] = rt_key(rt);
1296 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1298 if (rt_unlinkrte(di->rnh, info, &di->rc) != 0)
1302 * Add deleted rtentries to the list to GC them
1303 * after dropping the lock.
1305 * XXX: Delayed notifications not implemented
1306 * for nexthop updates.
1308 if (di->rc.rc_cmd == RTM_DELETE) {
1309 /* Add to the list and return */
1310 rt->rt_chain = di->head;
1315 * RTM_CHANGE to a diferent nexthop or nexthop group.
1316 * Free old multipath group.
1318 nhop_free_any(di->rc.rc_nh_old);
1326 * Iterates over a routing table specified by @fibnum and @family and
1327 * deletes elements marked by @filter_f.
1328 * @fibnum: rtable id
1329 * @family: AF_ address family
1330 * @filter_f: function returning non-zero value for items to delete
1331 * @arg: data to pass to the @filter_f function
1332 * @report: true if rtsock notification is needed.
1335 rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f, void *arg, bool report)
1337 struct rib_head *rnh;
1338 struct rt_delinfo di;
1340 struct nhop_object *nh;
1341 struct epoch_tracker et;
1343 rnh = rt_tables_get_rnh(fibnum, family);
1347 bzero(&di, sizeof(di));
1348 di.info.rti_filter = filter_f;
1349 di.info.rti_filterdata = arg;
1351 di.rc.rc_cmd = RTM_DELETE;
1353 NET_EPOCH_ENTER(et);
1356 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1359 /* We might have something to reclaim. */
1360 bzero(&di.rc, sizeof(di.rc));
1361 di.rc.rc_cmd = RTM_DELETE;
1362 while (di.head != NULL) {
1364 di.head = rt->rt_chain;
1365 rt->rt_chain = NULL;
1369 di.rc.rc_nh_old = nh;
1370 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1372 /* TODO std rt -> rt_addrinfo export */
1373 di.info.rti_info[RTAX_DST] = rt_key(rt);
1374 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1378 struct nhgrp_object *nhg;
1379 const struct weightened_nhop *wn;
1381 if (NH_IS_NHGRP(nh)) {
1382 nhg = (struct nhgrp_object *)nh;
1383 wn = nhgrp_get_nhops(nhg, &num_nhops);
1384 for (int i = 0; i < num_nhops; i++)
1385 rt_routemsg(RTM_DELETE, rt, wn[i].nh, fibnum);
1388 rt_routemsg(RTM_DELETE, rt, nh, fibnum);
1397 rt_delete_unconditional(struct radix_node *rn, void *arg)
1399 struct rtentry *rt = RNTORT(rn);
1400 struct rib_head *rnh = (struct rib_head *)arg;
1402 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), &rnh->head);
1403 if (RNTORT(rn) == rt)
1410 * Removes all routes from the routing table without executing notifications.
1411 * rtentres will be removed after the end of a current epoch.
1414 rib_flush_routes(struct rib_head *rnh)
1417 rnh->rnh_walktree(&rnh->head, rt_delete_unconditional, rnh);
1422 rib_flush_routes_family(int family)
1424 struct rib_head *rnh;
1426 for (uint32_t fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1427 if ((rnh = rt_tables_get_rnh(fibnum, family)) != NULL)
1428 rib_flush_routes(rnh);
1433 rib_print_family(int family)
1447 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1448 struct rib_cmd_info *rc)
1450 struct rib_subscription *rs;
1452 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1453 if (rs->type == type)
1454 rs->func(rnh, rc, rs->arg);
1458 static struct rib_subscription *
1459 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1460 enum rib_subscription_type type, bool waitok)
1462 struct rib_subscription *rs;
1463 int flags = M_ZERO | (waitok ? M_WAITOK : M_NOWAIT);
1465 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1477 * Subscribe for the changes in the routing table specified by @fibnum and
1480 * Returns pointer to the subscription structure on success.
1482 struct rib_subscription *
1483 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1484 enum rib_subscription_type type, bool waitok)
1486 struct rib_head *rnh;
1487 struct epoch_tracker et;
1489 NET_EPOCH_ENTER(et);
1490 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1491 rnh = rt_tables_get_rnh(fibnum, family);
1494 return (rib_subscribe_internal(rnh, f, arg, type, waitok));
1497 struct rib_subscription *
1498 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1499 enum rib_subscription_type type, bool waitok)
1501 struct rib_subscription *rs;
1502 struct epoch_tracker et;
1504 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1508 NET_EPOCH_ENTER(et);
1510 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1517 struct rib_subscription *
1518 rib_subscribe_locked(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1519 enum rib_subscription_type type)
1521 struct rib_subscription *rs;
1524 RIB_WLOCK_ASSERT(rnh);
1526 if ((rs = allocate_subscription(f, arg, type, false)) == NULL)
1530 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1536 * Remove rtable subscription @rs from the routing table.
1537 * Needs to be run in network epoch.
1540 rib_unsubscribe(struct rib_subscription *rs)
1542 struct rib_head *rnh = rs->rnh;
1547 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1550 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1555 rib_unsubscribe_locked(struct rib_subscription *rs)
1557 struct rib_head *rnh = rs->rnh;
1560 RIB_WLOCK_ASSERT(rnh);
1562 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1564 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1569 * Epoch callback indicating subscription is safe to destroy
1572 destroy_subscription_epoch(epoch_context_t ctx)
1574 struct rib_subscription *rs;
1576 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1582 rib_init_subscriptions(struct rib_head *rnh)
1585 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1589 rib_destroy_subscriptions(struct rib_head *rnh)
1591 struct rib_subscription *rs;
1592 struct epoch_tracker et;
1594 NET_EPOCH_ENTER(et);
1596 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1597 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1598 epoch_call(net_epoch_preempt, destroy_subscription_epoch,