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>
61 * This file contains control plane routing tables functions.
63 * All functions assumes they are called in net epoch.
66 struct rib_subscription {
67 CK_STAILQ_ENTRY(rib_subscription) next;
68 rib_subscription_cb_t *func;
71 enum rib_subscription_type type;
72 struct epoch_context epoch_ctx;
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);
85 static int rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info,
86 struct rib_cmd_info *rc);
88 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
89 struct rib_cmd_info *rc);
91 static void destroy_subscription_epoch(epoch_context_t ctx);
93 static bool rib_can_multipath(struct rib_head *rh);
96 /* Per-vnet multipath routing configuration */
97 SYSCTL_DECL(_net_route);
98 #define V_rib_route_multipath VNET(rib_route_multipath)
100 #define _MP_FLAGS CTLFLAG_RW
102 #define _MP_FLAGS CTLFLAG_RD
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");
109 /* Routing table UMA zone */
110 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
111 #define V_rtzone VNET(rtzone)
117 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
118 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
123 vnet_rtzone_destroy()
126 uma_zdestroy(V_rtzone);
131 destroy_rtentry(struct rtentry *rt)
134 struct nhop_object *nh = rt->rt_nhop;
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.
142 if (NH_IS_NHGRP(nh)) {
143 struct weightened_nhop *wn;
145 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
149 CURVNET_SET(nhop_get_vnet(nh));
152 /* Unreference nexthop */
153 nhop_free_any(rt->rt_nhop);
155 uma_zfree(V_rtzone, rt);
161 * Epoch callback indicating rtentry is safe to destroy
164 destroy_rtentry_epoch(epoch_context_t ctx)
168 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
174 * Schedule rtentry deletion
177 rtfree(struct rtentry *rt)
180 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
182 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
186 static struct rib_head *
187 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
189 struct rib_head *rnh;
190 struct sockaddr *dst;
192 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
194 dst = info->rti_info[RTAX_DST];
195 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
202 rib_can_multipath(struct rib_head *rh)
206 CURVNET_SET(rh->rib_vnet);
207 result = !!V_rib_route_multipath;
214 * Check is nhop is multipath-eligible.
215 * Avoid nhops without gateways and redirects.
217 * Returns 1 for multipath-eligible nexthop,
221 nhop_can_multipath(const struct nhop_object *nh)
224 if ((nh->nh_flags & NHF_MULTIPATH) != 0)
226 if ((nh->nh_flags & NHF_GATEWAY) == 0)
228 if ((nh->nh_flags & NHF_REDIRECT) != 0)
236 get_info_weight(const struct rt_addrinfo *info, uint32_t default_weight)
240 if (info->rti_mflags & RTV_WEIGHT)
241 weight = info->rti_rmx->rmx_weight;
243 weight = default_weight;
244 /* Keep upper 1 byte for adm distance purposes */
245 if (weight > RT_MAX_WEIGHT)
246 weight = RT_MAX_WEIGHT;
252 rt_is_host(const struct rtentry *rt)
255 return (rt->rte_flags & RTF_HOST);
259 rt_get_family(const struct rtentry *rt)
261 const struct sockaddr *dst;
263 dst = (const struct sockaddr *)rt_key_const(rt);
265 return (dst->sa_family);
269 * Returns pointer to nexthop or nexthop group
270 * associated with @rt
273 rt_get_raw_nhop(const struct rtentry *rt)
276 return (rt->rt_nhop);
281 * Stores IPv4 address and prefix length of @rt inside
283 * @pscopeid is currently always set to 0.
286 rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
287 int *plen, uint32_t *pscopeid)
289 const struct sockaddr_in *dst;
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);
299 *plen = bitcount32(dst->sin_addr.s_addr);
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.
309 rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
310 struct in_addr *pmask, uint32_t *pscopeid)
312 const struct sockaddr_in *dst;
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);
320 pmask->s_addr = INADDR_BROADCAST;
322 *pmask = dst->sin_addr;
329 inet6_get_plen(const struct in6_addr *addr)
332 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
333 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
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.
342 rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
343 int *plen, uint32_t *pscopeid)
345 const struct sockaddr_in6 *dst;
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);
353 *paddr = dst->sin6_addr;
354 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
358 *plen = inet6_get_plen(&dst->sin6_addr);
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.
367 rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
368 struct in6_addr *pmask, uint32_t *pscopeid)
370 const struct sockaddr_in6 *dst;
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);
378 *paddr = dst->sin6_addr;
379 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
381 memset(pmask, 0xFF, sizeof(struct in6_addr));
383 *pmask = dst->sin6_addr;
388 rt_set_expire_info(struct rtentry *rt, const struct rt_addrinfo *info)
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;
398 * Check if specified @gw matches gw data in the nexthop @nh.
400 * Returns true if matches, false otherwise.
403 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
406 if (nh->gw_sa.sa_family != gw->sa_family)
409 switch (gw->sa_family) {
411 return (nh->gw4_sa.sin_addr.s_addr ==
412 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
415 const struct sockaddr_in6 *gw6;
416 gw6 = (const struct sockaddr_in6 *)gw;
419 * Currently (2020-09) IPv6 gws in kernel have their
420 * scope embedded. Once this becomes false, this code
421 * has to be revisited.
423 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
430 const struct sockaddr_dl *sdl;
431 sdl = (const struct sockaddr_dl *)gw;
432 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
435 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
443 * Checks if data in @info matches nexhop @nh.
445 * Returns 0 on success,
446 * ESRCH if not matched,
447 * ENOENT if filter function returned false
450 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
451 const struct nhop_object *nh)
453 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
455 if (info->rti_filter != NULL) {
456 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
461 if ((gw != NULL) && !match_nhop_gw(nh, gw))
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.
473 * 1 if @info data has higher priority
474 * 0 if priority is the same
475 * -1 if priority is lower
478 can_override_nhop(const struct rt_addrinfo *info, const struct nhop_object *nh)
481 if (info->rti_flags & RTF_PINNED) {
482 return (NH_IS_PINNED(nh)) ? 0 : 1;
484 return (NH_IS_PINNED(nh)) ? -1 : 0;
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.
493 static struct rtentry *
494 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
495 const struct sockaddr *netmask, struct route_nhop_data *rnd)
499 RIB_LOCK_ASSERT(rnh);
501 rt = (struct rtentry *)rnh->rnh_lookup(__DECONST(void *, dst),
502 __DECONST(void *, netmask), &rnh->head);
504 rnd->rnd_nhop = rt->rt_nhop;
505 rnd->rnd_weight = rt->rt_weight;
507 rnd->rnd_nhop = NULL;
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.
521 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
522 struct route_nhop_data *rnd)
526 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
527 info->rti_info[RTAX_NETMASK], rnd);
533 * Adds route defined by @info into the kernel table specified by @fibnum and
534 * sa_family in @info->rti_info[RTAX_DST].
536 * Returns 0 on success and fills in operation metadata into @rc.
539 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
540 struct rib_cmd_info *rc)
542 struct rib_head *rnh;
547 rnh = get_rnh(fibnum, info);
549 return (EAFNOSUPPORT);
552 * Check consistency between RTF_HOST flag and netmask
555 if (info->rti_flags & RTF_HOST)
556 info->rti_info[RTAX_NETMASK] = NULL;
557 else if (info->rti_info[RTAX_NETMASK] == NULL)
560 bzero(rc, sizeof(struct rib_cmd_info));
561 rc->rc_cmd = RTM_ADD;
563 error = add_route(rnh, info, rc);
565 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
571 * Creates rtentry and nexthop based on @info data.
572 * Return 0 and fills in rtentry into @prt on success,
573 * return errno otherwise.
576 create_rtentry(struct rib_head *rnh, struct rt_addrinfo *info,
577 struct rtentry **prt)
579 struct sockaddr *dst, *ndst, *gateway, *netmask;
581 struct nhop_object *nh;
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;
590 if ((flags & RTF_GATEWAY) && !gateway)
592 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
593 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
596 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
599 if (info->rti_ifa == NULL) {
600 error = rt_getifa_fib(info, rnh->rib_fibnum);
605 error = nhop_create_from_info(rnh, info, &nh);
609 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
614 rt->rte_flags = (RTF_UP | flags) & RTE_RT_FLAG_MASK;
618 memcpy(&rt->rt_dst, dst, dst->sa_len);
619 rt_key(rt) = &rt->rt_dst;
622 * point to the (possibly newly malloc'd) dest address.
624 ndst = (struct sockaddr *)rt_key(rt);
627 * make sure it contains the value we want (masked if needed).
630 rt_maskedcopy(dst, ndst, netmask);
632 bcopy(dst, ndst, dst->sa_len);
635 * We use the ifa reference returned by rt_getifa_fib().
636 * This moved from below so that rnh->rnh_addaddr() can
637 * examine the ifa and ifa->ifa_ifp if it so desires.
640 rt->rt_weight = get_info_weight(info, RT_DEFAULT_WEIGHT);
641 rt_set_expire_info(rt, info);
648 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
649 struct rib_cmd_info *rc)
651 struct nhop_object *nh_orig;
652 struct route_nhop_data rnd_orig, rnd_add;
653 struct nhop_object *nh;
654 struct rtentry *rt, *rt_orig;
657 error = create_rtentry(rnh, info, &rt);
661 rnd_add.rnd_nhop = rt->rt_nhop;
662 rnd_add.rnd_weight = rt->rt_weight;
666 error = add_route_nhop(rnh, rt, info, &rnd_add, rc);
672 /* addition failed. Lookup prefix in the rib to determine the cause */
673 rt_orig = lookup_prefix(rnh, info, &rnd_orig);
674 if (rt_orig == NULL) {
675 /* No prefix -> rnh_addaddr() failed to allocate memory */
678 uma_zfree(V_rtzone, rt);
682 /* We have existing route in the RIB. */
683 nh_orig = rnd_orig.rnd_nhop;
684 /* Check if new route has higher preference */
685 if (can_override_nhop(info, nh_orig) > 0) {
686 /* Update nexthop to the new route */
687 change_route_nhop(rnh, rt_orig, info, &rnd_add, rc);
689 uma_zfree(V_rtzone, rt);
697 if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) &&
698 nhop_can_multipath(rnd_orig.rnd_nhop))
699 error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc);
702 /* Unable to add - another route with the same preference exists */
706 * ROUTE_MPATH disabled: failed to add route, free both nhop and rt.
707 * ROUTE_MPATH enabled: original nhop reference is unused in any case,
708 * free rt only if not _adding_ new route to rib (e.g. the case
709 * when initial lookup returned existing route, but then it got
710 * deleted prior to multipath group insertion, leading to a simple
711 * non-multipath add as a result).
714 if ((error != 0) || rc->rc_cmd != RTM_ADD)
715 uma_zfree(V_rtzone, rt);
721 * Removes route defined by @info from the kernel table specified by @fibnum and
722 * sa_family in @info->rti_info[RTAX_DST].
724 * Returns 0 on success and fills in operation metadata into @rc.
727 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
729 struct rib_head *rnh;
730 struct sockaddr *dst_orig, *netmask;
731 struct sockaddr_storage mdst;
736 rnh = get_rnh(fibnum, info);
738 return (EAFNOSUPPORT);
740 bzero(rc, sizeof(struct rib_cmd_info));
741 rc->rc_cmd = RTM_DELETE;
743 dst_orig = info->rti_info[RTAX_DST];
744 netmask = info->rti_info[RTAX_NETMASK];
746 if (netmask != NULL) {
747 /* Ensure @dst is always properly masked */
748 if (dst_orig->sa_len > sizeof(mdst))
750 rt_maskedcopy(dst_orig, (struct sockaddr *)&mdst, netmask);
751 info->rti_info[RTAX_DST] = (struct sockaddr *)&mdst;
753 error = del_route(rnh, info, rc);
754 info->rti_info[RTAX_DST] = dst_orig;
760 * Conditionally unlinks rtentry matching data inside @info from @rnh.
761 * Returns 0 on success with operation result stored in @rc.
763 * ESRCH - if prefix was not found,
764 * EADDRINUSE - if trying to delete higher priority route.
765 * ENOENT - if supplied filter function returned 0 (not matched).
768 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, struct rib_cmd_info *rc)
771 struct nhop_object *nh;
772 struct radix_node *rn;
773 struct route_nhop_data rnd;
776 rt = lookup_prefix(rnh, info, &rnd);
782 if (NH_IS_NHGRP(nh)) {
783 error = del_route_mpath(rnh, info, rt,
784 (struct nhgrp_object *)nh, rc);
788 error = check_info_match_nhop(info, rt, nh);
792 if (can_override_nhop(info, nh) < 0)
796 * Remove the item from the tree and return it.
797 * Complain if it is not there and do no more processing.
799 rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST],
800 info->rti_info[RTAX_NETMASK], &rnh->head);
804 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
805 panic ("rtrequest delete");
808 rt->rte_flags &= ~RTF_UP;
810 /* Finalize notification */
814 rc->rc_cmd = RTM_DELETE;
816 rc->rc_nh_old = rt->rt_nhop;
817 rc->rc_nh_weight = rt->rt_weight;
818 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
824 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
825 struct rib_cmd_info *rc)
830 error = rt_unlinkrte(rnh, info, rc);
835 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
838 * If the caller wants it, then it can have it,
839 * the entry will be deleted after the end of the current epoch.
841 if (rc->rc_cmd == RTM_DELETE)
846 * Deleting 1 path may result in RTM_CHANGE to
847 * a different mpath group/nhop.
848 * Free old mpath group.
850 nhop_free_any(rc->rc_nh_old);
858 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
859 struct rib_cmd_info *rc)
862 struct route_nhop_data rnd_orig;
863 struct rib_head *rnh;
869 rnh = get_rnh(fibnum, info);
871 return (EAFNOSUPPORT);
873 bzero(rc, sizeof(struct rib_cmd_info));
874 rc->rc_cmd = RTM_CHANGE;
876 /* Check if updated gateway exists */
877 if ((info->rti_flags & RTF_GATEWAY) &&
878 (info->rti_info[RTAX_GATEWAY] == NULL)) {
881 * route(8) adds RTF_GATEWAY flag if -interface is not set.
882 * Remove RTF_GATEWAY to enforce consistency and maintain
885 info->rti_flags &= ~RTF_GATEWAY;
889 * route change is done in multiple steps, with dropping and
890 * reacquiring lock. In the situations with multiple processes
891 * changes the same route in can lead to the case when route
892 * is changed between the steps. Address it by retrying the operation
893 * multiple times before failing.
897 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
898 info->rti_info[RTAX_NETMASK], &rnh->head);
905 rnd_orig.rnd_nhop = rt->rt_nhop;
906 rnd_orig.rnd_weight = rt->rt_weight;
910 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
911 error = change_route(rnh, info, &rnd_orig, rc);
920 change_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
921 struct nhop_object *nh_orig, struct nhop_object **nh_new)
926 * New gateway could require new ifaddr, ifp;
927 * flags may also be different; ifp may be specified
928 * by ll sockaddr when protocol address is ambiguous
930 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
931 info->rti_info[RTAX_GATEWAY] != NULL) ||
932 info->rti_info[RTAX_IFP] != NULL ||
933 (info->rti_info[RTAX_IFA] != NULL &&
934 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
935 error = rt_getifa_fib(info, rnh->rib_fibnum);
938 info->rti_ifa = NULL;
943 error = nhop_create_from_nhop(rnh, nh_orig, info, nh_new);
944 info->rti_ifa = NULL;
951 change_mpath_route(struct rib_head *rnh, struct rt_addrinfo *info,
952 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
955 struct nhop_object *nh, *nh_orig, *nh_new;
956 struct route_nhop_data rnd_new;
959 nh_orig = rnd_orig->rnd_nhop;
961 struct weightened_nhop *wn = NULL, *wn_new;
964 wn = nhgrp_get_nhops((struct nhgrp_object *)nh_orig, &num_nhops);
966 for (int i = 0; i < num_nhops; i++) {
967 if (check_info_match_nhop(info, NULL, wn[i].nh)) {
976 error = change_nhop(rnh, info, nh_orig, &nh_new);
980 wn_new = mallocarray(num_nhops, sizeof(struct weightened_nhop),
981 M_TEMP, M_NOWAIT | M_ZERO);
982 if (wn_new == NULL) {
987 memcpy(wn_new, wn, num_nhops * sizeof(struct weightened_nhop));
988 for (int i = 0; i < num_nhops; i++) {
989 if (wn[i].nh == nh_orig) {
991 wn[i].weight = get_info_weight(info, rnd_orig->rnd_weight);
996 error = nhgrp_get_group(rnh, wn_new, num_nhops, &rnd_new);
998 free(wn_new, M_TEMP);
1003 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1010 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
1011 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
1014 struct nhop_object *nh, *nh_orig;
1015 struct route_nhop_data rnd_new;
1018 nh_orig = rnd_orig->rnd_nhop;
1019 if (nh_orig == NULL)
1023 if (NH_IS_NHGRP(nh_orig))
1024 return (change_mpath_route(rnh, info, rnd_orig, rc));
1027 rnd_new.rnd_weight = get_info_weight(info, rnd_orig->rnd_weight);
1028 error = change_nhop(rnh, info, nh_orig, &rnd_new.rnd_nhop);
1031 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1037 * Insert @rt with nhop data from @rnd_new to @rnh.
1038 * Returns 0 on success and stores operation results in @rc.
1041 add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1042 struct rt_addrinfo *info, struct route_nhop_data *rnd,
1043 struct rib_cmd_info *rc)
1045 struct sockaddr *ndst, *netmask;
1046 struct radix_node *rn;
1049 RIB_WLOCK_ASSERT(rnh);
1051 ndst = (struct sockaddr *)rt_key(rt);
1052 netmask = info->rti_info[RTAX_NETMASK];
1054 rt->rt_nhop = rnd->rnd_nhop;
1055 rt->rt_weight = rnd->rnd_weight;
1056 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
1059 if (rt->rt_expire > 0)
1060 tmproutes_update(rnh, rt);
1062 /* Finalize notification */
1064 rnh->rnh_prefixes++;
1066 rc->rc_cmd = RTM_ADD;
1068 rc->rc_nh_old = NULL;
1069 rc->rc_nh_new = rnd->rnd_nhop;
1070 rc->rc_nh_weight = rnd->rnd_weight;
1072 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1074 /* Existing route or memory allocation failure */
1082 * Switch @rt nhop/weigh to the ones specified in @rnd.
1083 * Conditionally set rt_expire if set in @info.
1084 * Returns 0 on success.
1087 change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1088 struct rt_addrinfo *info, struct route_nhop_data *rnd,
1089 struct rib_cmd_info *rc)
1091 struct nhop_object *nh_orig;
1093 RIB_WLOCK_ASSERT(rnh);
1095 nh_orig = rt->rt_nhop;
1097 if (rnd->rnd_nhop != NULL) {
1098 /* Changing expiration & nexthop & weight to a new one */
1099 rt_set_expire_info(rt, info);
1100 rt->rt_nhop = rnd->rnd_nhop;
1101 rt->rt_weight = rnd->rnd_weight;
1102 if (rt->rt_expire > 0)
1103 tmproutes_update(rnh, rt);
1105 /* Route deletion requested. */
1106 struct sockaddr *ndst, *netmask;
1107 struct radix_node *rn;
1109 ndst = (struct sockaddr *)rt_key(rt);
1110 netmask = info->rti_info[RTAX_NETMASK];
1111 rn = rnh->rnh_deladdr(ndst, netmask, &rnh->head);
1115 rt->rte_flags &= ~RTF_UP;
1118 /* Finalize notification */
1120 if (rnd->rnd_nhop == NULL)
1121 rnh->rnh_prefixes--;
1123 rc->rc_cmd = (rnd->rnd_nhop != NULL) ? RTM_CHANGE : RTM_DELETE;
1125 rc->rc_nh_old = nh_orig;
1126 rc->rc_nh_new = rnd->rnd_nhop;
1127 rc->rc_nh_weight = rnd->rnd_weight;
1129 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1135 * Conditionally update route nhop/weight IFF data in @nhd_orig is
1136 * consistent with the current route data.
1137 * Nexthop in @nhd_new is consumed.
1140 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
1141 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
1142 struct route_nhop_data *rnd_new, struct rib_cmd_info *rc)
1144 struct rtentry *rt_new;
1149 rt_new = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1150 info->rti_info[RTAX_NETMASK], &rnh->head);
1152 if (rt_new == NULL) {
1153 if (rnd_orig->rnd_nhop == NULL)
1154 error = add_route_nhop(rnh, rt, info, rnd_new, rc);
1157 * Prefix does not exist, which was not our assumption.
1158 * Update @rnd_orig with the new data and return
1160 rnd_orig->rnd_nhop = NULL;
1161 rnd_orig->rnd_weight = 0;
1165 /* Prefix exists, try to update */
1166 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
1168 * Nhop/mpath group hasn't changed. Flip
1169 * to the new precalculated one and return
1171 error = change_route_nhop(rnh, rt_new, info, rnd_new, rc);
1173 /* Update and retry */
1174 rnd_orig->rnd_nhop = rt_new->rt_nhop;
1175 rnd_orig->rnd_weight = rt_new->rt_weight;
1183 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
1185 if (rnd_orig->rnd_nhop != NULL)
1186 nhop_free_any(rnd_orig->rnd_nhop);
1189 if (rnd_new->rnd_nhop != NULL)
1190 nhop_free_any(rnd_new->rnd_nhop);
1197 * Performs modification of routing table specificed by @action.
1198 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
1199 * Needs to be run in network epoch.
1201 * Returns 0 on success and fills in @rc with action result.
1204 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
1205 struct rib_cmd_info *rc)
1211 error = rib_add_route(fibnum, info, rc);
1214 error = rib_del_route(fibnum, info, rc);
1217 error = rib_change_route(fibnum, info, rc);
1228 struct rt_addrinfo info;
1229 struct rib_head *rnh;
1230 struct rtentry *head;
1231 struct rib_cmd_info rc;
1235 * Conditionally unlinks @rn from radix tree based
1236 * on info data passed in @arg.
1239 rt_checkdelroute(struct radix_node *rn, void *arg)
1241 struct rt_delinfo *di;
1242 struct rt_addrinfo *info;
1245 di = (struct rt_delinfo *)arg;
1246 rt = (struct rtentry *)rn;
1249 info->rti_info[RTAX_DST] = rt_key(rt);
1250 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1252 if (rt_unlinkrte(di->rnh, info, &di->rc) != 0)
1256 * Add deleted rtentries to the list to GC them
1257 * after dropping the lock.
1259 * XXX: Delayed notifications not implemented
1260 * for nexthop updates.
1262 if (di->rc.rc_cmd == RTM_DELETE) {
1263 /* Add to the list and return */
1264 rt->rt_chain = di->head;
1269 * RTM_CHANGE to a diferent nexthop or nexthop group.
1270 * Free old multipath group.
1272 nhop_free_any(di->rc.rc_nh_old);
1280 * Iterates over a routing table specified by @fibnum and @family and
1281 * deletes elements marked by @filter_f.
1282 * @fibnum: rtable id
1283 * @family: AF_ address family
1284 * @filter_f: function returning non-zero value for items to delete
1285 * @arg: data to pass to the @filter_f function
1286 * @report: true if rtsock notification is needed.
1289 rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f, void *arg, bool report)
1291 struct rib_head *rnh;
1292 struct rt_delinfo di;
1294 struct nhop_object *nh;
1295 struct epoch_tracker et;
1297 rnh = rt_tables_get_rnh(fibnum, family);
1301 bzero(&di, sizeof(di));
1302 di.info.rti_filter = filter_f;
1303 di.info.rti_filterdata = arg;
1305 di.rc.rc_cmd = RTM_DELETE;
1307 NET_EPOCH_ENTER(et);
1310 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1313 /* We might have something to reclaim. */
1314 bzero(&di.rc, sizeof(di.rc));
1315 di.rc.rc_cmd = RTM_DELETE;
1316 while (di.head != NULL) {
1318 di.head = rt->rt_chain;
1319 rt->rt_chain = NULL;
1323 di.rc.rc_nh_old = nh;
1324 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1326 /* TODO std rt -> rt_addrinfo export */
1327 di.info.rti_info[RTAX_DST] = rt_key(rt);
1328 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1332 struct nhgrp_object *nhg;
1333 struct weightened_nhop *wn;
1335 if (NH_IS_NHGRP(nh)) {
1336 nhg = (struct nhgrp_object *)nh;
1337 wn = nhgrp_get_nhops(nhg, &num_nhops);
1338 for (int i = 0; i < num_nhops; i++)
1339 rt_routemsg(RTM_DELETE, rt, wn[i].nh, fibnum);
1342 rt_routemsg(RTM_DELETE, rt, nh, fibnum);
1351 rt_delete_unconditional(struct radix_node *rn, void *arg)
1353 struct rtentry *rt = RNTORT(rn);
1354 struct rib_head *rnh = (struct rib_head *)arg;
1356 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), &rnh->head);
1357 if (RNTORT(rn) == rt)
1364 * Removes all routes from the routing table without executing notifications.
1365 * rtentres will be removed after the end of a current epoch.
1368 rib_flush_routes(struct rib_head *rnh)
1371 rnh->rnh_walktree(&rnh->head, rt_delete_unconditional, rnh);
1376 rib_flush_routes_family(int family)
1378 struct rib_head *rnh;
1380 for (uint32_t fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1381 if ((rnh = rt_tables_get_rnh(fibnum, family)) != NULL)
1382 rib_flush_routes(rnh);
1387 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1388 struct rib_cmd_info *rc)
1390 struct rib_subscription *rs;
1392 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1393 if (rs->type == type)
1394 rs->func(rnh, rc, rs->arg);
1398 static struct rib_subscription *
1399 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1400 enum rib_subscription_type type, bool waitok)
1402 struct rib_subscription *rs;
1403 int flags = M_ZERO | (waitok ? M_WAITOK : M_NOWAIT);
1405 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1417 * Subscribe for the changes in the routing table specified by @fibnum and
1420 * Returns pointer to the subscription structure on success.
1422 struct rib_subscription *
1423 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1424 enum rib_subscription_type type, bool waitok)
1426 struct rib_head *rnh;
1427 struct epoch_tracker et;
1429 NET_EPOCH_ENTER(et);
1430 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1431 rnh = rt_tables_get_rnh(fibnum, family);
1434 return (rib_subscribe_internal(rnh, f, arg, type, waitok));
1437 struct rib_subscription *
1438 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1439 enum rib_subscription_type type, bool waitok)
1441 struct rib_subscription *rs;
1442 struct epoch_tracker et;
1444 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1448 NET_EPOCH_ENTER(et);
1450 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1457 struct rib_subscription *
1458 rib_subscribe_locked(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1459 enum rib_subscription_type type)
1461 struct rib_subscription *rs;
1464 RIB_WLOCK_ASSERT(rnh);
1466 if ((rs = allocate_subscription(f, arg, type, false)) == NULL)
1470 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1476 * Remove rtable subscription @rs from the routing table.
1477 * Needs to be run in network epoch.
1480 rib_unsibscribe(struct rib_subscription *rs)
1482 struct rib_head *rnh = rs->rnh;
1487 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1490 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1495 rib_unsibscribe_locked(struct rib_subscription *rs)
1497 struct rib_head *rnh = rs->rnh;
1500 RIB_WLOCK_ASSERT(rnh);
1502 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1504 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1509 * Epoch callback indicating subscription is safe to destroy
1512 destroy_subscription_epoch(epoch_context_t ctx)
1514 struct rib_subscription *rs;
1516 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1522 rib_init_subscriptions(struct rib_head *rnh)
1525 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1529 rib_destroy_subscriptions(struct rib_head *rnh)
1531 struct rib_subscription *rs;
1532 struct epoch_tracker et;
1534 NET_EPOCH_ENTER(et);
1536 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1537 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1538 epoch_call(net_epoch_preempt, destroy_subscription_epoch,