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;
73 enum rib_subscription_type type;
74 struct epoch_context epoch_ctx;
77 static int add_route(struct rib_head *rnh, struct rt_addrinfo *info,
78 struct rib_cmd_info *rc);
79 static int add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
80 struct rt_addrinfo *info, struct route_nhop_data *rnd,
81 struct rib_cmd_info *rc);
82 static int del_route(struct rib_head *rnh, struct rt_addrinfo *info,
83 struct rib_cmd_info *rc);
84 static int change_route(struct rib_head *rnh, struct rt_addrinfo *info,
85 struct route_nhop_data *nhd_orig, struct rib_cmd_info *rc);
87 static int rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info,
88 struct rib_cmd_info *rc);
90 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
91 struct rib_cmd_info *rc);
93 static void destroy_subscription_epoch(epoch_context_t ctx);
95 static bool rib_can_multipath(struct rib_head *rh);
98 /* Per-vnet multipath routing configuration */
99 SYSCTL_DECL(_net_route);
100 #define V_rib_route_multipath VNET(rib_route_multipath)
102 #define _MP_FLAGS CTLFLAG_RW
104 #define _MP_FLAGS CTLFLAG_RD
106 VNET_DEFINE(u_int, rib_route_multipath) = 0;
107 SYSCTL_UINT(_net_route, OID_AUTO, multipath, _MP_FLAGS | CTLFLAG_VNET,
108 &VNET_NAME(rib_route_multipath), 0, "Enable route multipath");
111 /* Routing table UMA zone */
112 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
113 #define V_rtzone VNET(rtzone)
119 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
120 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
125 vnet_rtzone_destroy()
128 uma_zdestroy(V_rtzone);
133 destroy_rtentry(struct rtentry *rt)
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.
141 CURVNET_SET(nhop_get_vnet(rt->rt_nhop));
143 /* Unreference nexthop */
144 nhop_free_any(rt->rt_nhop);
146 uma_zfree(V_rtzone, rt);
152 * Epoch callback indicating rtentry is safe to destroy
155 destroy_rtentry_epoch(epoch_context_t ctx)
159 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
165 * Schedule rtentry deletion
168 rtfree(struct rtentry *rt)
171 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
173 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
177 static struct rib_head *
178 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
180 struct rib_head *rnh;
181 struct sockaddr *dst;
183 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
185 dst = info->rti_info[RTAX_DST];
186 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
193 rib_can_multipath(struct rib_head *rh)
197 CURVNET_SET(rh->rib_vnet);
198 result = !!V_rib_route_multipath;
205 * Check is nhop is multipath-eligible.
206 * Avoid nhops without gateways and redirects.
208 * Returns 1 for multipath-eligible nexthop,
212 nhop_can_multipath(const struct nhop_object *nh)
215 if ((nh->nh_flags & NHF_MULTIPATH) != 0)
217 if ((nh->nh_flags & NHF_GATEWAY) == 0)
219 if ((nh->nh_flags & NHF_REDIRECT) != 0)
227 get_info_weight(const struct rt_addrinfo *info, uint32_t default_weight)
231 if (info->rti_mflags & RTV_WEIGHT)
232 weight = info->rti_rmx->rmx_weight;
234 weight = default_weight;
235 /* Keep upper 1 byte for adm distance purposes */
236 if (weight > RT_MAX_WEIGHT)
237 weight = RT_MAX_WEIGHT;
243 rt_set_expire_info(struct rtentry *rt, const struct rt_addrinfo *info)
246 /* Kernel -> userland timebase conversion. */
247 if (info->rti_mflags & RTV_EXPIRE)
248 rt->rt_expire = info->rti_rmx->rmx_expire ?
249 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
253 * Check if specified @gw matches gw data in the nexthop @nh.
255 * Returns true if matches, false otherwise.
258 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
261 if (nh->gw_sa.sa_family != gw->sa_family)
264 switch (gw->sa_family) {
266 return (nh->gw4_sa.sin_addr.s_addr ==
267 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
270 const struct sockaddr_in6 *gw6;
271 gw6 = (const struct sockaddr_in6 *)gw;
274 * Currently (2020-09) IPv6 gws in kernel have their
275 * scope embedded. Once this becomes false, this code
276 * has to be revisited.
278 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
285 const struct sockaddr_dl *sdl;
286 sdl = (const struct sockaddr_dl *)gw;
287 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
290 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
298 * Checks if data in @info matches nexhop @nh.
300 * Returns 0 on success,
301 * ESRCH if not matched,
302 * ENOENT if filter function returned false
305 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
306 const struct nhop_object *nh)
308 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
310 if (info->rti_filter != NULL) {
311 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
316 if ((gw != NULL) && !match_nhop_gw(nh, gw))
323 * Checks if nexhop @nh can be rewritten by data in @info because
324 * of higher "priority". Currently the only case for such scenario
325 * is kernel installing interface routes, marked by RTF_PINNED flag.
328 * 1 if @info data has higher priority
329 * 0 if priority is the same
330 * -1 if priority is lower
333 can_override_nhop(const struct rt_addrinfo *info, const struct nhop_object *nh)
336 if (info->rti_flags & RTF_PINNED) {
337 return (NH_IS_PINNED(nh)) ? 0 : 1;
339 return (NH_IS_PINNED(nh)) ? -1 : 0;
344 * Runs exact prefix match based on @dst and @netmask.
345 * Returns matched @rtentry if found or NULL.
346 * If rtentry was found, saves nexthop / weight value into @rnd.
348 static struct rtentry *
349 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
350 const struct sockaddr *netmask, struct route_nhop_data *rnd)
354 RIB_LOCK_ASSERT(rnh);
356 rt = (struct rtentry *)rnh->rnh_lookup(__DECONST(void *, dst),
357 __DECONST(void *, netmask), &rnh->head);
359 rnd->rnd_nhop = rt->rt_nhop;
360 rnd->rnd_weight = rt->rt_weight;
362 rnd->rnd_nhop = NULL;
370 * Runs exact prefix match based on dst/netmask from @info.
371 * Assumes RIB lock is held.
372 * Returns matched @rtentry if found or NULL.
373 * If rtentry was found, saves nexthop / weight value into @rnd.
376 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
377 struct route_nhop_data *rnd)
381 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
382 info->rti_info[RTAX_NETMASK], rnd);
388 * Adds route defined by @info into the kernel table specified by @fibnum and
389 * sa_family in @info->rti_info[RTAX_DST].
391 * Returns 0 on success and fills in operation metadata into @rc.
394 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
395 struct rib_cmd_info *rc)
397 struct rib_head *rnh;
402 rnh = get_rnh(fibnum, info);
404 return (EAFNOSUPPORT);
407 * Check consistency between RTF_HOST flag and netmask
410 if (info->rti_flags & RTF_HOST)
411 info->rti_info[RTAX_NETMASK] = NULL;
412 else if (info->rti_info[RTAX_NETMASK] == NULL)
415 bzero(rc, sizeof(struct rib_cmd_info));
416 rc->rc_cmd = RTM_ADD;
418 error = add_route(rnh, info, rc);
420 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
426 * Creates rtentry and nexthop based on @info data.
427 * Return 0 and fills in rtentry into @prt on success,
428 * return errno otherwise.
431 create_rtentry(struct rib_head *rnh, struct rt_addrinfo *info,
432 struct rtentry **prt)
434 struct sockaddr *dst, *ndst, *gateway, *netmask;
436 struct nhop_object *nh;
440 dst = info->rti_info[RTAX_DST];
441 gateway = info->rti_info[RTAX_GATEWAY];
442 netmask = info->rti_info[RTAX_NETMASK];
443 flags = info->rti_flags;
445 if ((flags & RTF_GATEWAY) && !gateway)
447 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
448 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
451 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
454 if (info->rti_ifa == NULL) {
455 error = rt_getifa_fib(info, rnh->rib_fibnum);
459 ifa_ref(info->rti_ifa);
462 error = nhop_create_from_info(rnh, info, &nh);
464 ifa_free(info->rti_ifa);
468 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
470 ifa_free(info->rti_ifa);
474 rt->rte_flags = (RTF_UP | flags) & RTE_RT_FLAG_MASK;
478 memcpy(&rt->rt_dst, dst, dst->sa_len);
479 rt_key(rt) = &rt->rt_dst;
482 * point to the (possibly newly malloc'd) dest address.
484 ndst = (struct sockaddr *)rt_key(rt);
487 * make sure it contains the value we want (masked if needed).
490 rt_maskedcopy(dst, ndst, netmask);
492 bcopy(dst, ndst, dst->sa_len);
495 * We use the ifa reference returned by rt_getifa_fib().
496 * This moved from below so that rnh->rnh_addaddr() can
497 * examine the ifa and ifa->ifa_ifp if it so desires.
500 rt->rt_weight = get_info_weight(info, RT_DEFAULT_WEIGHT);
501 rt_set_expire_info(rt, info);
508 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
509 struct rib_cmd_info *rc)
511 struct nhop_object *nh_orig;
512 struct route_nhop_data rnd_orig, rnd_add;
513 struct nhop_object *nh;
514 struct rtentry *rt, *rt_orig;
517 error = create_rtentry(rnh, info, &rt);
521 rnd_add.rnd_nhop = rt->rt_nhop;
522 rnd_add.rnd_weight = rt->rt_weight;
526 error = add_route_nhop(rnh, rt, info, &rnd_add, rc);
532 /* addition failed. Lookup prefix in the rib to determine the cause */
533 rt_orig = lookup_prefix(rnh, info, &rnd_orig);
534 if (rt_orig == NULL) {
535 /* No prefix -> rnh_addaddr() failed to allocate memory */
538 uma_zfree(V_rtzone, rt);
542 /* We have existing route in the RIB. */
543 nh_orig = rnd_orig.rnd_nhop;
544 /* Check if new route has higher preference */
545 if (can_override_nhop(info, nh_orig) > 0) {
546 /* Update nexthop to the new route */
547 change_route_nhop(rnh, rt_orig, info, &rnd_add, rc);
549 uma_zfree(V_rtzone, rt);
557 if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) &&
558 nhop_can_multipath(rnd_orig.rnd_nhop))
559 error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc);
562 /* Unable to add - another route with the same preference exists */
566 * ROUTE_MPATH disabled: failed to add route, free both nhop and rt.
567 * ROUTE_MPATH enabled: original nhop reference is unused in any case,
568 * free rt only if not _adding_ new route to rib (e.g. the case
569 * when initial lookup returned existing route, but then it got
570 * deleted prior to multipath group insertion, leading to a simple
571 * non-multipath add as a result).
574 if ((error != 0) || rc->rc_cmd != RTM_ADD)
575 uma_zfree(V_rtzone, rt);
581 * Removes route defined by @info from the kernel table specified by @fibnum and
582 * sa_family in @info->rti_info[RTAX_DST].
584 * Returns 0 on success and fills in operation metadata into @rc.
587 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
589 struct rib_head *rnh;
590 struct sockaddr *dst_orig, *netmask;
591 struct sockaddr_storage mdst;
596 rnh = get_rnh(fibnum, info);
598 return (EAFNOSUPPORT);
600 bzero(rc, sizeof(struct rib_cmd_info));
601 rc->rc_cmd = RTM_DELETE;
603 dst_orig = info->rti_info[RTAX_DST];
604 netmask = info->rti_info[RTAX_NETMASK];
606 if (netmask != NULL) {
607 /* Ensure @dst is always properly masked */
608 if (dst_orig->sa_len > sizeof(mdst))
610 rt_maskedcopy(dst_orig, (struct sockaddr *)&mdst, netmask);
611 info->rti_info[RTAX_DST] = (struct sockaddr *)&mdst;
613 error = del_route(rnh, info, rc);
614 info->rti_info[RTAX_DST] = dst_orig;
620 * Conditionally unlinks rtentry matching data inside @info from @rnh.
621 * Returns 0 on success with operation result stored in @rc.
623 * ESRCH - if prefix was not found,
624 * EADDRINUSE - if trying to delete higher priority route.
625 * ENOENT - if supplied filter function returned 0 (not matched).
628 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, struct rib_cmd_info *rc)
631 struct nhop_object *nh;
632 struct radix_node *rn;
633 struct route_nhop_data rnd;
636 rt = lookup_prefix(rnh, info, &rnd);
642 if (NH_IS_NHGRP(nh)) {
643 error = del_route_mpath(rnh, info, rt,
644 (struct nhgrp_object *)nh, rc);
648 error = check_info_match_nhop(info, rt, nh);
652 if (can_override_nhop(info, nh) < 0)
656 * Remove the item from the tree and return it.
657 * Complain if it is not there and do no more processing.
659 rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST],
660 info->rti_info[RTAX_NETMASK], &rnh->head);
664 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
665 panic ("rtrequest delete");
668 rt->rte_flags &= ~RTF_UP;
670 /* Finalize notification */
672 rc->rc_cmd = RTM_DELETE;
674 rc->rc_nh_old = rt->rt_nhop;
675 rc->rc_nh_weight = rt->rt_weight;
676 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
682 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
683 struct rib_cmd_info *rc)
688 error = rt_unlinkrte(rnh, info, rc);
693 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
696 * If the caller wants it, then it can have it,
697 * the entry will be deleted after the end of the current epoch.
699 if (rc->rc_cmd == RTM_DELETE)
704 * Deleting 1 path may result in RTM_CHANGE to
705 * a different mpath group/nhop.
706 * Free old mpath group.
708 nhop_free_any(rc->rc_nh_old);
716 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
717 struct rib_cmd_info *rc)
720 struct route_nhop_data rnd_orig;
721 struct rib_head *rnh;
727 rnh = get_rnh(fibnum, info);
729 return (EAFNOSUPPORT);
731 bzero(rc, sizeof(struct rib_cmd_info));
732 rc->rc_cmd = RTM_CHANGE;
734 /* Check if updated gateway exists */
735 if ((info->rti_flags & RTF_GATEWAY) &&
736 (info->rti_info[RTAX_GATEWAY] == NULL)) {
739 * route(8) adds RTF_GATEWAY flag if -interface is not set.
740 * Remove RTF_GATEWAY to enforce consistency and maintain
743 info->rti_flags &= ~RTF_GATEWAY;
747 * route change is done in multiple steps, with dropping and
748 * reacquiring lock. In the situations with multiple processes
749 * changes the same route in can lead to the case when route
750 * is changed between the steps. Address it by retrying the operation
751 * multiple times before failing.
755 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
756 info->rti_info[RTAX_NETMASK], &rnh->head);
763 rnd_orig.rnd_nhop = rt->rt_nhop;
764 rnd_orig.rnd_weight = rt->rt_weight;
768 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
769 error = change_route(rnh, info, &rnd_orig, rc);
778 change_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
779 struct nhop_object *nh_orig, struct nhop_object **nh_new)
785 * New gateway could require new ifaddr, ifp;
786 * flags may also be different; ifp may be specified
787 * by ll sockaddr when protocol address is ambiguous
789 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
790 info->rti_info[RTAX_GATEWAY] != NULL) ||
791 info->rti_info[RTAX_IFP] != NULL ||
792 (info->rti_info[RTAX_IFA] != NULL &&
793 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
794 error = rt_getifa_fib(info, rnh->rib_fibnum);
795 if (info->rti_ifa != NULL)
800 ifa_free(info->rti_ifa);
801 info->rti_ifa = NULL;
808 error = nhop_create_from_nhop(rnh, nh_orig, info, nh_new);
810 ifa_free(info->rti_ifa);
811 info->rti_ifa = NULL;
819 change_mpath_route(struct rib_head *rnh, struct rt_addrinfo *info,
820 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
823 struct nhop_object *nh, *nh_orig, *nh_new;
824 struct route_nhop_data rnd_new;
827 nh_orig = rnd_orig->rnd_nhop;
829 struct weightened_nhop *wn = NULL, *wn_new;
832 wn = nhgrp_get_nhops((struct nhgrp_object *)nh_orig, &num_nhops);
834 for (int i = 0; i < num_nhops; i++) {
835 if (check_info_match_nhop(info, NULL, wn[i].nh)) {
844 error = change_nhop(rnh, info, nh_orig, &nh_new);
848 wn_new = mallocarray(num_nhops, sizeof(struct weightened_nhop),
849 M_TEMP, M_NOWAIT | M_ZERO);
850 if (wn_new == NULL) {
855 memcpy(wn_new, wn, num_nhops * sizeof(struct weightened_nhop));
856 for (int i = 0; i < num_nhops; i++) {
857 if (wn[i].nh == nh_orig) {
859 wn[i].weight = get_info_weight(info, rnd_orig->rnd_weight);
864 error = nhgrp_get_group(rnh, wn_new, num_nhops, &rnd_new);
866 free(wn_new, M_TEMP);
871 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
878 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
879 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
882 struct nhop_object *nh, *nh_orig;
883 struct route_nhop_data rnd_new;
886 nh_orig = rnd_orig->rnd_nhop;
891 if (NH_IS_NHGRP(nh_orig))
892 return (change_mpath_route(rnh, info, rnd_orig, rc));
895 rnd_new.rnd_weight = get_info_weight(info, rnd_orig->rnd_weight);
896 error = change_nhop(rnh, info, nh_orig, &rnd_new.rnd_nhop);
899 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
905 * Insert @rt with nhop data from @rnd_new to @rnh.
906 * Returns 0 on success and stores operation results in @rc.
909 add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
910 struct rt_addrinfo *info, struct route_nhop_data *rnd,
911 struct rib_cmd_info *rc)
913 struct sockaddr *ndst, *netmask;
914 struct radix_node *rn;
917 RIB_WLOCK_ASSERT(rnh);
919 ndst = (struct sockaddr *)rt_key(rt);
920 netmask = info->rti_info[RTAX_NETMASK];
922 rt->rt_nhop = rnd->rnd_nhop;
923 rt->rt_weight = rnd->rnd_weight;
924 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
927 if (rt->rt_expire > 0)
928 tmproutes_update(rnh, rt);
930 /* Finalize notification */
933 rc->rc_cmd = RTM_ADD;
935 rc->rc_nh_old = NULL;
936 rc->rc_nh_new = rnd->rnd_nhop;
937 rc->rc_nh_weight = rnd->rnd_weight;
939 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
941 /* Existing route or memory allocation failure */
949 * Switch @rt nhop/weigh to the ones specified in @rnd.
950 * Conditionally set rt_expire if set in @info.
951 * Returns 0 on success.
954 change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
955 struct rt_addrinfo *info, struct route_nhop_data *rnd,
956 struct rib_cmd_info *rc)
958 struct nhop_object *nh_orig;
960 RIB_WLOCK_ASSERT(rnh);
962 nh_orig = rt->rt_nhop;
964 if (rnd->rnd_nhop != NULL) {
965 /* Changing expiration & nexthop & weight to a new one */
966 rt_set_expire_info(rt, info);
967 rt->rt_nhop = rnd->rnd_nhop;
968 rt->rt_weight = rnd->rnd_weight;
969 if (rt->rt_expire > 0)
970 tmproutes_update(rnh, rt);
972 /* Route deletion requested. */
973 struct sockaddr *ndst, *netmask;
974 struct radix_node *rn;
976 ndst = (struct sockaddr *)rt_key(rt);
977 netmask = info->rti_info[RTAX_NETMASK];
978 rn = rnh->rnh_deladdr(ndst, netmask, &rnh->head);
982 rt->rte_flags &= ~RTF_UP;
985 /* Finalize notification */
988 rc->rc_cmd = (rnd->rnd_nhop != NULL) ? RTM_CHANGE : RTM_DELETE;
990 rc->rc_nh_old = nh_orig;
991 rc->rc_nh_new = rnd->rnd_nhop;
992 rc->rc_nh_weight = rnd->rnd_weight;
994 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1000 * Conditionally update route nhop/weight IFF data in @nhd_orig is
1001 * consistent with the current route data.
1002 * Nexthop in @nhd_new is consumed.
1005 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
1006 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
1007 struct route_nhop_data *rnd_new, struct rib_cmd_info *rc)
1009 struct rtentry *rt_new;
1014 rt_new = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1015 info->rti_info[RTAX_NETMASK], &rnh->head);
1017 if (rt_new == NULL) {
1018 if (rnd_orig->rnd_nhop == NULL)
1019 error = add_route_nhop(rnh, rt, info, rnd_new, rc);
1022 * Prefix does not exist, which was not our assumption.
1023 * Update @rnd_orig with the new data and return
1025 rnd_orig->rnd_nhop = NULL;
1026 rnd_orig->rnd_weight = 0;
1030 /* Prefix exists, try to update */
1031 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
1033 * Nhop/mpath group hasn't changed. Flip
1034 * to the new precalculated one and return
1036 error = change_route_nhop(rnh, rt_new, info, rnd_new, rc);
1038 /* Update and retry */
1039 rnd_orig->rnd_nhop = rt_new->rt_nhop;
1040 rnd_orig->rnd_weight = rt_new->rt_weight;
1048 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
1050 if (rnd_orig->rnd_nhop != NULL)
1051 nhop_free_any(rnd_orig->rnd_nhop);
1054 if (rnd_new->rnd_nhop != NULL)
1055 nhop_free_any(rnd_new->rnd_nhop);
1062 * Performs modification of routing table specificed by @action.
1063 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
1064 * Needs to be run in network epoch.
1066 * Returns 0 on success and fills in @rc with action result.
1069 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
1070 struct rib_cmd_info *rc)
1076 error = rib_add_route(fibnum, info, rc);
1079 error = rib_del_route(fibnum, info, rc);
1082 error = rib_change_route(fibnum, info, rc);
1093 struct rt_addrinfo info;
1094 struct rib_head *rnh;
1095 struct rtentry *head;
1096 struct rib_cmd_info rc;
1100 * Conditionally unlinks @rn from radix tree based
1101 * on info data passed in @arg.
1104 rt_checkdelroute(struct radix_node *rn, void *arg)
1106 struct rt_delinfo *di;
1107 struct rt_addrinfo *info;
1111 di = (struct rt_delinfo *)arg;
1112 rt = (struct rtentry *)rn;
1115 info->rti_info[RTAX_DST] = rt_key(rt);
1116 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1118 error = rt_unlinkrte(di->rnh, info, &di->rc);
1121 * Add deleted rtentries to the list to GC them
1122 * after dropping the lock.
1124 * XXX: Delayed notifications not implemented
1125 * for nexthop updates.
1127 if ((error == 0) && (di->rc.rc_cmd == RTM_DELETE)) {
1128 /* Add to the list and return */
1129 rt->rt_chain = di->head;
1137 * Iterates over a routing table specified by @fibnum and @family and
1138 * deletes elements marked by @filter_f.
1139 * @fibnum: rtable id
1140 * @family: AF_ address family
1141 * @filter_f: function returning non-zero value for items to delete
1142 * @arg: data to pass to the @filter_f function
1143 * @report: true if rtsock notification is needed.
1146 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report)
1148 struct rib_head *rnh;
1149 struct rt_delinfo di;
1151 struct nhop_object *nh;
1152 struct epoch_tracker et;
1154 rnh = rt_tables_get_rnh(fibnum, family);
1158 bzero(&di, sizeof(di));
1159 di.info.rti_filter = filter_f;
1160 di.info.rti_filterdata = arg;
1162 di.rc.rc_cmd = RTM_DELETE;
1164 NET_EPOCH_ENTER(et);
1167 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1170 /* We might have something to reclaim. */
1171 bzero(&di.rc, sizeof(di.rc));
1172 di.rc.rc_cmd = RTM_DELETE;
1173 while (di.head != NULL) {
1175 di.head = rt->rt_chain;
1176 rt->rt_chain = NULL;
1180 di.rc.rc_nh_old = nh;
1181 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1183 /* TODO std rt -> rt_addrinfo export */
1184 di.info.rti_info[RTAX_DST] = rt_key(rt);
1185 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1189 struct nhgrp_object *nhg;
1190 struct weightened_nhop *wn;
1192 if (NH_IS_NHGRP(nh)) {
1193 nhg = (struct nhgrp_object *)nh;
1194 wn = nhgrp_get_nhops(nhg, &num_nhops);
1195 for (int i = 0; i < num_nhops; i++)
1196 rt_routemsg(RTM_DELETE, rt,
1197 wn[i].nh->nh_ifp, 0, fibnum);
1200 rt_routemsg(RTM_DELETE, rt, nh->nh_ifp, 0, fibnum);
1209 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1210 struct rib_cmd_info *rc)
1212 struct rib_subscription *rs;
1214 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1215 if (rs->type == type)
1216 rs->func(rnh, rc, rs->arg);
1220 static struct rib_subscription *
1221 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1222 enum rib_subscription_type type, bool waitok)
1224 struct rib_subscription *rs;
1225 int flags = M_ZERO | (waitok ? M_WAITOK : 0);
1227 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1239 * Subscribe for the changes in the routing table specified by @fibnum and
1242 * Returns pointer to the subscription structure on success.
1244 struct rib_subscription *
1245 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1246 enum rib_subscription_type type, bool waitok)
1248 struct rib_head *rnh;
1249 struct rib_subscription *rs;
1250 struct epoch_tracker et;
1252 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1255 NET_EPOCH_ENTER(et);
1256 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1257 rnh = rt_tables_get_rnh(fibnum, family);
1260 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
1267 struct rib_subscription *
1268 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1269 enum rib_subscription_type type, bool waitok)
1271 struct rib_subscription *rs;
1272 struct epoch_tracker et;
1274 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1277 NET_EPOCH_ENTER(et);
1279 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
1287 * Remove rtable subscription @rs from the table specified by @fibnum
1289 * Needs to be run in network epoch.
1291 * Returns 0 on success.
1294 rib_unsibscribe(uint32_t fibnum, int family, struct rib_subscription *rs)
1296 struct rib_head *rnh;
1299 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1300 rnh = rt_tables_get_rnh(fibnum, family);
1306 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1309 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1316 * Epoch callback indicating subscription is safe to destroy
1319 destroy_subscription_epoch(epoch_context_t ctx)
1321 struct rib_subscription *rs;
1323 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1329 rib_init_subscriptions(struct rib_head *rnh)
1332 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1336 rib_destroy_subscriptions(struct rib_head *rnh)
1338 struct rib_subscription *rs;
1339 struct epoch_tracker et;
1341 NET_EPOCH_ENTER(et);
1343 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1344 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1345 epoch_call(net_epoch_preempt, destroy_subscription_epoch,