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>
58 #include <net/radix_mpath.h>
64 * This file contains control plane routing tables functions.
66 * All functions assumes they are called in net epoch.
69 struct rib_subscription {
70 CK_STAILQ_ENTRY(rib_subscription) next;
71 rib_subscription_cb_t *func;
74 enum rib_subscription_type type;
75 struct epoch_context epoch_ctx;
78 static int add_route(struct rib_head *rnh, struct rt_addrinfo *info,
79 struct rib_cmd_info *rc);
80 static int add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
81 struct rt_addrinfo *info, struct route_nhop_data *rnd,
82 struct rib_cmd_info *rc);
83 static int del_route(struct rib_head *rnh, struct rt_addrinfo *info,
84 struct rib_cmd_info *rc);
85 static int change_route(struct rib_head *rnh, struct rt_addrinfo *info,
86 struct route_nhop_data *nhd_orig, struct rib_cmd_info *rc);
88 static int rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info,
89 struct rib_cmd_info *rc);
91 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
92 struct rib_cmd_info *rc);
94 static void destroy_subscription_epoch(epoch_context_t ctx);
96 static bool rib_can_multipath(struct rib_head *rh);
99 /* Per-vnet multipath routing configuration */
100 SYSCTL_DECL(_net_route);
101 #define V_rib_route_multipath VNET(rib_route_multipath)
103 #define _MP_FLAGS CTLFLAG_RW
105 #define _MP_FLAGS CTLFLAG_RD
107 VNET_DEFINE(u_int, rib_route_multipath) = 0;
108 SYSCTL_UINT(_net_route, OID_AUTO, multipath, _MP_FLAGS | CTLFLAG_VNET,
109 &VNET_NAME(rib_route_multipath), 0, "Enable route multipath");
112 /* Routing table UMA zone */
113 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
114 #define V_rtzone VNET(rtzone)
120 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
121 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
126 vnet_rtzone_destroy()
129 uma_zdestroy(V_rtzone);
134 destroy_rtentry(struct rtentry *rt)
138 * At this moment rnh, nh_control may be already freed.
139 * nhop interface may have been migrated to a different vnet.
140 * Use vnet stored in the nexthop to delete the entry.
142 CURVNET_SET(nhop_get_vnet(rt->rt_nhop));
144 /* Unreference nexthop */
145 nhop_free_any(rt->rt_nhop);
147 uma_zfree(V_rtzone, rt);
153 * Epoch callback indicating rtentry is safe to destroy
156 destroy_rtentry_epoch(epoch_context_t ctx)
160 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
166 * Schedule rtentry deletion
169 rtfree(struct rtentry *rt)
172 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
174 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
178 static struct rib_head *
179 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
181 struct rib_head *rnh;
182 struct sockaddr *dst;
184 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
186 dst = info->rti_info[RTAX_DST];
187 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
194 rib_can_multipath(struct rib_head *rh)
198 CURVNET_SET(rh->rib_vnet);
199 result = !!V_rib_route_multipath;
206 * Check is nhop is multipath-eligible.
207 * Avoid nhops without gateways and redirects.
209 * Returns 1 for multipath-eligible nexthop,
213 nhop_can_multipath(const struct nhop_object *nh)
216 if ((nh->nh_flags & NHF_MULTIPATH) != 0)
218 if ((nh->nh_flags & NHF_GATEWAY) == 0)
220 if ((nh->nh_flags & NHF_REDIRECT) != 0)
228 get_info_weight(const struct rt_addrinfo *info, uint32_t default_weight)
232 if (info->rti_mflags & RTV_WEIGHT)
233 weight = info->rti_rmx->rmx_weight;
235 weight = default_weight;
236 /* Keep upper 1 byte for adm distance purposes */
237 if (weight > RT_MAX_WEIGHT)
238 weight = RT_MAX_WEIGHT;
244 rt_set_expire_info(struct rtentry *rt, const struct rt_addrinfo *info)
247 /* Kernel -> userland timebase conversion. */
248 if (info->rti_mflags & RTV_EXPIRE)
249 rt->rt_expire = info->rti_rmx->rmx_expire ?
250 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
254 * Check if specified @gw matches gw data in the nexthop @nh.
256 * Returns true if matches, false otherwise.
259 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
262 if (nh->gw_sa.sa_family != gw->sa_family)
265 switch (gw->sa_family) {
267 return (nh->gw4_sa.sin_addr.s_addr ==
268 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
271 const struct sockaddr_in6 *gw6;
272 gw6 = (const struct sockaddr_in6 *)gw;
275 * Currently (2020-09) IPv6 gws in kernel have their
276 * scope embedded. Once this becomes false, this code
277 * has to be revisited.
279 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
286 const struct sockaddr_dl *sdl;
287 sdl = (const struct sockaddr_dl *)gw;
288 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
291 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
299 * Checks if data in @info matches nexhop @nh.
301 * Returns 0 on success,
302 * ESRCH if not matched,
303 * ENOENT if filter function returned false
306 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
307 const struct nhop_object *nh)
309 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
311 if (info->rti_filter != NULL) {
312 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
317 if ((gw != NULL) && !match_nhop_gw(nh, gw))
324 * Checks if nexhop @nh can be rewritten by data in @info because
325 * of higher "priority". Currently the only case for such scenario
326 * is kernel installing interface routes, marked by RTF_PINNED flag.
329 * 1 if @info data has higher priority
330 * 0 if priority is the same
331 * -1 if priority is lower
334 can_override_nhop(const struct rt_addrinfo *info, const struct nhop_object *nh)
337 if (info->rti_flags & RTF_PINNED) {
338 return (NH_IS_PINNED(nh)) ? 0 : 1;
340 return (NH_IS_PINNED(nh)) ? -1 : 0;
345 * Runs exact prefix match based on @dst and @netmask.
346 * Returns matched @rtentry if found or NULL.
347 * If rtentry was found, saves nexthop / weight value into @rnd.
349 static struct rtentry *
350 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
351 const struct sockaddr *netmask, struct route_nhop_data *rnd)
355 RIB_LOCK_ASSERT(rnh);
357 rt = (struct rtentry *)rnh->rnh_lookup(__DECONST(void *, dst),
358 __DECONST(void *, netmask), &rnh->head);
360 rnd->rnd_nhop = rt->rt_nhop;
361 rnd->rnd_weight = rt->rt_weight;
363 rnd->rnd_nhop = NULL;
371 * Runs exact prefix match based on dst/netmask from @info.
372 * Assumes RIB lock is held.
373 * Returns matched @rtentry if found or NULL.
374 * If rtentry was found, saves nexthop / weight value into @rnd.
377 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
378 struct route_nhop_data *rnd)
382 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
383 info->rti_info[RTAX_NETMASK], rnd);
389 * Adds route defined by @info into the kernel table specified by @fibnum and
390 * sa_family in @info->rti_info[RTAX_DST].
392 * Returns 0 on success and fills in operation metadata into @rc.
395 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
396 struct rib_cmd_info *rc)
398 struct rib_head *rnh;
403 rnh = get_rnh(fibnum, info);
405 return (EAFNOSUPPORT);
408 * Check consistency between RTF_HOST flag and netmask
411 if (info->rti_flags & RTF_HOST)
412 info->rti_info[RTAX_NETMASK] = NULL;
413 else if (info->rti_info[RTAX_NETMASK] == NULL)
416 bzero(rc, sizeof(struct rib_cmd_info));
417 rc->rc_cmd = RTM_ADD;
419 error = add_route(rnh, info, rc);
421 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
427 * Creates rtentry and nexthop based on @info data.
428 * Return 0 and fills in rtentry into @prt on success,
429 * return errno otherwise.
432 create_rtentry(struct rib_head *rnh, struct rt_addrinfo *info,
433 struct rtentry **prt)
435 struct sockaddr *dst, *ndst, *gateway, *netmask;
437 struct nhop_object *nh;
441 dst = info->rti_info[RTAX_DST];
442 gateway = info->rti_info[RTAX_GATEWAY];
443 netmask = info->rti_info[RTAX_NETMASK];
444 flags = info->rti_flags;
446 if ((flags & RTF_GATEWAY) && !gateway)
448 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
449 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
452 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
455 if (info->rti_ifa == NULL) {
456 error = rt_getifa_fib(info, rnh->rib_fibnum);
460 ifa_ref(info->rti_ifa);
463 error = nhop_create_from_info(rnh, info, &nh);
465 ifa_free(info->rti_ifa);
469 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
471 ifa_free(info->rti_ifa);
475 rt->rte_flags = (RTF_UP | flags) & RTE_RT_FLAG_MASK;
479 memcpy(&rt->rt_dst, dst, dst->sa_len);
480 rt_key(rt) = &rt->rt_dst;
483 * point to the (possibly newly malloc'd) dest address.
485 ndst = (struct sockaddr *)rt_key(rt);
488 * make sure it contains the value we want (masked if needed).
491 rt_maskedcopy(dst, ndst, netmask);
493 bcopy(dst, ndst, dst->sa_len);
496 * We use the ifa reference returned by rt_getifa_fib().
497 * This moved from below so that rnh->rnh_addaddr() can
498 * examine the ifa and ifa->ifa_ifp if it so desires.
501 rt->rt_weight = get_info_weight(info, RT_DEFAULT_WEIGHT);
502 rt_set_expire_info(rt, info);
509 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
510 struct rib_cmd_info *rc)
512 struct nhop_object *nh_orig;
513 struct route_nhop_data rnd_orig, rnd_add;
514 struct nhop_object *nh;
515 struct rtentry *rt, *rt_orig;
518 error = create_rtentry(rnh, info, &rt);
522 rnd_add.rnd_nhop = rt->rt_nhop;
523 rnd_add.rnd_weight = rt->rt_weight;
527 error = add_route_nhop(rnh, rt, info, &rnd_add, rc);
533 /* addition failed. Lookup prefix in the rib to determine the cause */
534 rt_orig = lookup_prefix(rnh, info, &rnd_orig);
535 if (rt_orig == NULL) {
536 /* No prefix -> rnh_addaddr() failed to allocate memory */
539 uma_zfree(V_rtzone, rt);
543 /* We have existing route in the RIB. */
544 nh_orig = rnd_orig.rnd_nhop;
545 /* Check if new route has higher preference */
546 if (can_override_nhop(info, nh_orig) > 0) {
547 /* Update nexthop to the new route */
548 change_route_nhop(rnh, rt_orig, info, &rnd_add, rc);
550 uma_zfree(V_rtzone, rt);
558 if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) &&
559 nhop_can_multipath(rnd_orig.rnd_nhop))
560 error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc);
563 /* Unable to add - another route with the same preference exists */
567 * ROUTE_MPATH disabled: failed to add route, free both nhop and rt.
568 * ROUTE_MPATH enabled: original nhop reference is unused in any case,
569 * free rt only if not _adding_ new route to rib (e.g. the case
570 * when initial lookup returned existing route, but then it got
571 * deleted prior to multipath group insertion, leading to a simple
572 * non-multipath add as a result).
575 if ((error != 0) || rc->rc_cmd != RTM_ADD)
576 uma_zfree(V_rtzone, rt);
582 * Removes route defined by @info from the kernel table specified by @fibnum and
583 * sa_family in @info->rti_info[RTAX_DST].
585 * Returns 0 on success and fills in operation metadata into @rc.
588 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
590 struct rib_head *rnh;
591 struct sockaddr *dst_orig, *netmask;
592 struct sockaddr_storage mdst;
597 rnh = get_rnh(fibnum, info);
599 return (EAFNOSUPPORT);
601 bzero(rc, sizeof(struct rib_cmd_info));
602 rc->rc_cmd = RTM_DELETE;
604 dst_orig = info->rti_info[RTAX_DST];
605 netmask = info->rti_info[RTAX_NETMASK];
607 if (netmask != NULL) {
608 /* Ensure @dst is always properly masked */
609 if (dst_orig->sa_len > sizeof(mdst))
611 rt_maskedcopy(dst_orig, (struct sockaddr *)&mdst, netmask);
612 info->rti_info[RTAX_DST] = (struct sockaddr *)&mdst;
614 error = del_route(rnh, info, rc);
615 info->rti_info[RTAX_DST] = dst_orig;
621 * Conditionally unlinks rtentry matching data inside @info from @rnh.
622 * Returns 0 on success with operation result stored in @rc.
624 * ESRCH - if prefix was not found,
625 * EADDRINUSE - if trying to delete higher priority route.
626 * ENOENT - if supplied filter function returned 0 (not matched).
629 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, struct rib_cmd_info *rc)
632 struct nhop_object *nh;
633 struct radix_node *rn;
634 struct route_nhop_data rnd;
637 rt = lookup_prefix(rnh, info, &rnd);
643 if (NH_IS_NHGRP(nh)) {
644 error = del_route_mpath(rnh, info, rt,
645 (struct nhgrp_object *)nh, rc);
649 error = check_info_match_nhop(info, rt, nh);
653 if (can_override_nhop(info, nh) < 0)
657 * Remove the item from the tree and return it.
658 * Complain if it is not there and do no more processing.
660 rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST],
661 info->rti_info[RTAX_NETMASK], &rnh->head);
665 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
666 panic ("rtrequest delete");
669 rt->rte_flags &= ~RTF_UP;
671 /* Finalize notification */
675 rc->rc_cmd = RTM_DELETE;
677 rc->rc_nh_old = rt->rt_nhop;
678 rc->rc_nh_weight = rt->rt_weight;
679 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
685 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
686 struct rib_cmd_info *rc)
691 error = rt_unlinkrte(rnh, info, rc);
696 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
699 * If the caller wants it, then it can have it,
700 * the entry will be deleted after the end of the current epoch.
702 if (rc->rc_cmd == RTM_DELETE)
707 * Deleting 1 path may result in RTM_CHANGE to
708 * a different mpath group/nhop.
709 * Free old mpath group.
711 nhop_free_any(rc->rc_nh_old);
719 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
720 struct rib_cmd_info *rc)
723 struct route_nhop_data rnd_orig;
724 struct rib_head *rnh;
730 rnh = get_rnh(fibnum, info);
732 return (EAFNOSUPPORT);
734 bzero(rc, sizeof(struct rib_cmd_info));
735 rc->rc_cmd = RTM_CHANGE;
737 /* Check if updated gateway exists */
738 if ((info->rti_flags & RTF_GATEWAY) &&
739 (info->rti_info[RTAX_GATEWAY] == NULL)) {
742 * route(8) adds RTF_GATEWAY flag if -interface is not set.
743 * Remove RTF_GATEWAY to enforce consistency and maintain
746 info->rti_flags &= ~RTF_GATEWAY;
750 * route change is done in multiple steps, with dropping and
751 * reacquiring lock. In the situations with multiple processes
752 * changes the same route in can lead to the case when route
753 * is changed between the steps. Address it by retrying the operation
754 * multiple times before failing.
758 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
759 info->rti_info[RTAX_NETMASK], &rnh->head);
766 rnd_orig.rnd_nhop = rt->rt_nhop;
767 rnd_orig.rnd_weight = rt->rt_weight;
771 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
772 error = change_route(rnh, info, &rnd_orig, rc);
781 change_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
782 struct nhop_object *nh_orig, struct nhop_object **nh_new)
788 * New gateway could require new ifaddr, ifp;
789 * flags may also be different; ifp may be specified
790 * by ll sockaddr when protocol address is ambiguous
792 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
793 info->rti_info[RTAX_GATEWAY] != NULL) ||
794 info->rti_info[RTAX_IFP] != NULL ||
795 (info->rti_info[RTAX_IFA] != NULL &&
796 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
797 error = rt_getifa_fib(info, rnh->rib_fibnum);
798 if (info->rti_ifa != NULL)
803 ifa_free(info->rti_ifa);
804 info->rti_ifa = NULL;
811 error = nhop_create_from_nhop(rnh, nh_orig, info, nh_new);
813 ifa_free(info->rti_ifa);
814 info->rti_ifa = NULL;
822 change_mpath_route(struct rib_head *rnh, struct rt_addrinfo *info,
823 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
826 struct nhop_object *nh, *nh_orig, *nh_new;
827 struct route_nhop_data rnd_new;
830 nh_orig = rnd_orig->rnd_nhop;
832 struct weightened_nhop *wn = NULL, *wn_new;
835 wn = nhgrp_get_nhops((struct nhgrp_object *)nh_orig, &num_nhops);
837 for (int i = 0; i < num_nhops; i++) {
838 if (check_info_match_nhop(info, NULL, wn[i].nh)) {
847 error = change_nhop(rnh, info, nh_orig, &nh_new);
851 wn_new = mallocarray(num_nhops, sizeof(struct weightened_nhop),
852 M_TEMP, M_NOWAIT | M_ZERO);
853 if (wn_new == NULL) {
858 memcpy(wn_new, wn, num_nhops * sizeof(struct weightened_nhop));
859 for (int i = 0; i < num_nhops; i++) {
860 if (wn[i].nh == nh_orig) {
862 wn[i].weight = get_info_weight(info, rnd_orig->rnd_weight);
867 error = nhgrp_get_group(rnh, wn_new, num_nhops, &rnd_new);
869 free(wn_new, M_TEMP);
874 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
881 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
882 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
885 struct nhop_object *nh, *nh_orig;
886 struct route_nhop_data rnd_new;
889 nh_orig = rnd_orig->rnd_nhop;
894 if (NH_IS_NHGRP(nh_orig))
895 return (change_mpath_route(rnh, info, rnd_orig, rc));
898 rnd_new.rnd_weight = get_info_weight(info, rnd_orig->rnd_weight);
899 error = change_nhop(rnh, info, nh_orig, &rnd_new.rnd_nhop);
902 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
908 * Insert @rt with nhop data from @rnd_new to @rnh.
909 * Returns 0 on success and stores operation results in @rc.
912 add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
913 struct rt_addrinfo *info, struct route_nhop_data *rnd,
914 struct rib_cmd_info *rc)
916 struct sockaddr *ndst, *netmask;
917 struct radix_node *rn;
920 RIB_WLOCK_ASSERT(rnh);
922 ndst = (struct sockaddr *)rt_key(rt);
923 netmask = info->rti_info[RTAX_NETMASK];
925 rt->rt_nhop = rnd->rnd_nhop;
926 rt->rt_weight = rnd->rnd_weight;
927 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
930 if (rt->rt_expire > 0)
931 tmproutes_update(rnh, rt);
933 /* Finalize notification */
937 rc->rc_cmd = RTM_ADD;
939 rc->rc_nh_old = NULL;
940 rc->rc_nh_new = rnd->rnd_nhop;
941 rc->rc_nh_weight = rnd->rnd_weight;
943 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
945 /* Existing route or memory allocation failure */
953 * Switch @rt nhop/weigh to the ones specified in @rnd.
954 * Conditionally set rt_expire if set in @info.
955 * Returns 0 on success.
958 change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
959 struct rt_addrinfo *info, struct route_nhop_data *rnd,
960 struct rib_cmd_info *rc)
962 struct nhop_object *nh_orig;
964 RIB_WLOCK_ASSERT(rnh);
966 nh_orig = rt->rt_nhop;
968 if (rnd->rnd_nhop != NULL) {
969 /* Changing expiration & nexthop & weight to a new one */
970 rt_set_expire_info(rt, info);
971 rt->rt_nhop = rnd->rnd_nhop;
972 rt->rt_weight = rnd->rnd_weight;
973 if (rt->rt_expire > 0)
974 tmproutes_update(rnh, rt);
976 /* Route deletion requested. */
977 struct sockaddr *ndst, *netmask;
978 struct radix_node *rn;
980 ndst = (struct sockaddr *)rt_key(rt);
981 netmask = info->rti_info[RTAX_NETMASK];
982 rn = rnh->rnh_deladdr(ndst, netmask, &rnh->head);
986 rt->rte_flags &= ~RTF_UP;
989 /* Finalize notification */
991 if (rnd->rnd_nhop == NULL)
994 rc->rc_cmd = (rnd->rnd_nhop != NULL) ? RTM_CHANGE : RTM_DELETE;
996 rc->rc_nh_old = nh_orig;
997 rc->rc_nh_new = rnd->rnd_nhop;
998 rc->rc_nh_weight = rnd->rnd_weight;
1000 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1006 * Conditionally update route nhop/weight IFF data in @nhd_orig is
1007 * consistent with the current route data.
1008 * Nexthop in @nhd_new is consumed.
1011 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
1012 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
1013 struct route_nhop_data *rnd_new, struct rib_cmd_info *rc)
1015 struct rtentry *rt_new;
1020 rt_new = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1021 info->rti_info[RTAX_NETMASK], &rnh->head);
1023 if (rt_new == NULL) {
1024 if (rnd_orig->rnd_nhop == NULL)
1025 error = add_route_nhop(rnh, rt, info, rnd_new, rc);
1028 * Prefix does not exist, which was not our assumption.
1029 * Update @rnd_orig with the new data and return
1031 rnd_orig->rnd_nhop = NULL;
1032 rnd_orig->rnd_weight = 0;
1036 /* Prefix exists, try to update */
1037 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
1039 * Nhop/mpath group hasn't changed. Flip
1040 * to the new precalculated one and return
1042 error = change_route_nhop(rnh, rt_new, info, rnd_new, rc);
1044 /* Update and retry */
1045 rnd_orig->rnd_nhop = rt_new->rt_nhop;
1046 rnd_orig->rnd_weight = rt_new->rt_weight;
1054 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
1056 if (rnd_orig->rnd_nhop != NULL)
1057 nhop_free_any(rnd_orig->rnd_nhop);
1060 if (rnd_new->rnd_nhop != NULL)
1061 nhop_free_any(rnd_new->rnd_nhop);
1068 * Performs modification of routing table specificed by @action.
1069 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
1070 * Needs to be run in network epoch.
1072 * Returns 0 on success and fills in @rc with action result.
1075 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
1076 struct rib_cmd_info *rc)
1082 error = rib_add_route(fibnum, info, rc);
1085 error = rib_del_route(fibnum, info, rc);
1088 error = rib_change_route(fibnum, info, rc);
1099 struct rt_addrinfo info;
1100 struct rib_head *rnh;
1101 struct rtentry *head;
1102 struct rib_cmd_info rc;
1106 * Conditionally unlinks @rn from radix tree based
1107 * on info data passed in @arg.
1110 rt_checkdelroute(struct radix_node *rn, void *arg)
1112 struct rt_delinfo *di;
1113 struct rt_addrinfo *info;
1117 di = (struct rt_delinfo *)arg;
1118 rt = (struct rtentry *)rn;
1121 info->rti_info[RTAX_DST] = rt_key(rt);
1122 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1124 error = rt_unlinkrte(di->rnh, info, &di->rc);
1127 * Add deleted rtentries to the list to GC them
1128 * after dropping the lock.
1130 * XXX: Delayed notifications not implemented
1131 * for nexthop updates.
1133 if ((error == 0) && (di->rc.rc_cmd == RTM_DELETE)) {
1134 /* Add to the list and return */
1135 rt->rt_chain = di->head;
1143 * Iterates over a routing table specified by @fibnum and @family and
1144 * deletes elements marked by @filter_f.
1145 * @fibnum: rtable id
1146 * @family: AF_ address family
1147 * @filter_f: function returning non-zero value for items to delete
1148 * @arg: data to pass to the @filter_f function
1149 * @report: true if rtsock notification is needed.
1152 rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f, void *arg, bool report)
1154 struct rib_head *rnh;
1155 struct rt_delinfo di;
1157 struct nhop_object *nh;
1158 struct epoch_tracker et;
1160 rnh = rt_tables_get_rnh(fibnum, family);
1164 bzero(&di, sizeof(di));
1165 di.info.rti_filter = filter_f;
1166 di.info.rti_filterdata = arg;
1168 di.rc.rc_cmd = RTM_DELETE;
1170 NET_EPOCH_ENTER(et);
1173 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1176 /* We might have something to reclaim. */
1177 bzero(&di.rc, sizeof(di.rc));
1178 di.rc.rc_cmd = RTM_DELETE;
1179 while (di.head != NULL) {
1181 di.head = rt->rt_chain;
1182 rt->rt_chain = NULL;
1186 di.rc.rc_nh_old = nh;
1187 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1189 /* TODO std rt -> rt_addrinfo export */
1190 di.info.rti_info[RTAX_DST] = rt_key(rt);
1191 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1195 struct nhgrp_object *nhg;
1196 struct weightened_nhop *wn;
1198 if (NH_IS_NHGRP(nh)) {
1199 nhg = (struct nhgrp_object *)nh;
1200 wn = nhgrp_get_nhops(nhg, &num_nhops);
1201 for (int i = 0; i < num_nhops; i++)
1202 rt_routemsg(RTM_DELETE, rt,
1203 wn[i].nh->nh_ifp, 0, fibnum);
1206 rt_routemsg(RTM_DELETE, rt, nh->nh_ifp, 0, fibnum);
1215 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1216 struct rib_cmd_info *rc)
1218 struct rib_subscription *rs;
1220 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1221 if (rs->type == type)
1222 rs->func(rnh, rc, rs->arg);
1226 static struct rib_subscription *
1227 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1228 enum rib_subscription_type type, bool waitok)
1230 struct rib_subscription *rs;
1231 int flags = M_ZERO | (waitok ? M_WAITOK : M_NOWAIT);
1233 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1245 * Subscribe for the changes in the routing table specified by @fibnum and
1248 * Returns pointer to the subscription structure on success.
1250 struct rib_subscription *
1251 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1252 enum rib_subscription_type type, bool waitok)
1254 struct rib_head *rnh;
1255 struct epoch_tracker et;
1257 NET_EPOCH_ENTER(et);
1258 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1259 rnh = rt_tables_get_rnh(fibnum, family);
1262 return (rib_subscribe_internal(rnh, f, arg, type, waitok));
1265 struct rib_subscription *
1266 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1267 enum rib_subscription_type type, bool waitok)
1269 struct rib_subscription *rs;
1270 struct epoch_tracker et;
1272 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1276 NET_EPOCH_ENTER(et);
1278 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
1286 * Remove rtable subscription @rs from the routing table.
1287 * Needs to be run in network epoch.
1290 rib_unsibscribe(struct rib_subscription *rs)
1292 struct rib_head *rnh = rs->rnh;
1297 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1300 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1305 * Epoch callback indicating subscription is safe to destroy
1308 destroy_subscription_epoch(epoch_context_t ctx)
1310 struct rib_subscription *rs;
1312 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1318 rib_init_subscriptions(struct rib_head *rnh)
1321 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1325 rib_destroy_subscriptions(struct rib_head *rnh)
1327 struct rib_subscription *rs;
1328 struct epoch_tracker et;
1330 NET_EPOCH_ENTER(et);
1332 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1333 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1334 epoch_call(net_epoch_preempt, destroy_subscription_epoch,