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_mpath.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);
86 static int change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
87 struct rt_addrinfo *info, struct route_nhop_data *rnd,
88 struct rib_cmd_info *rc);
90 static int rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info,
91 struct rib_cmd_info *rc);
93 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
94 struct rib_cmd_info *rc);
96 static void destroy_subscription_epoch(epoch_context_t ctx);
98 /* Routing table UMA zone */
99 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
100 #define V_rtzone VNET(rtzone)
106 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
107 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
112 vnet_rtzone_destroy()
115 uma_zdestroy(V_rtzone);
120 destroy_rtentry(struct rtentry *rt)
124 * At this moment rnh, nh_control may be already freed.
125 * nhop interface may have been migrated to a different vnet.
126 * Use vnet stored in the nexthop to delete the entry.
128 CURVNET_SET(nhop_get_vnet(rt->rt_nhop));
130 /* Unreference nexthop */
131 nhop_free(rt->rt_nhop);
133 uma_zfree(V_rtzone, rt);
139 * Epoch callback indicating rtentry is safe to destroy
142 destroy_rtentry_epoch(epoch_context_t ctx)
146 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
152 * Schedule rtentry deletion
155 rtfree(struct rtentry *rt)
158 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
160 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
164 static struct rib_head *
165 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
167 struct rib_head *rnh;
168 struct sockaddr *dst;
170 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
172 dst = info->rti_info[RTAX_DST];
173 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
179 * Check if specified @gw matches gw data in the nexthop @nh.
181 * Returns true if matches, false otherwise.
184 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
187 if (nh->gw_sa.sa_family != gw->sa_family)
190 switch (gw->sa_family) {
192 return (nh->gw4_sa.sin_addr.s_addr ==
193 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
196 const struct sockaddr_in6 *gw6;
197 gw6 = (const struct sockaddr_in6 *)gw;
200 * Currently (2020-09) IPv6 gws in kernel have their
201 * scope embedded. Once this becomes false, this code
202 * has to be revisited.
204 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
211 const struct sockaddr_dl *sdl;
212 sdl = (const struct sockaddr_dl *)gw;
213 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
216 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
224 * Checks if data in @info matches nexhop @nh.
226 * Returns 0 on success,
227 * ESRCH if not matched,
228 * ENOENT if filter function returned false
231 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
232 const struct nhop_object *nh)
234 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
236 if (info->rti_filter != NULL) {
237 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
242 if ((gw != NULL) && !match_nhop_gw(nh, gw))
249 * Checks if nexhop @nh can be rewritten by data in @info because
250 * of higher "priority". Currently the only case for such scenario
251 * is kernel installing interface routes, marked by RTF_PINNED flag.
254 * 1 if @info data has higher priority
255 * 0 if priority is the same
256 * -1 if priority is lower
259 can_override_nhop(const struct rt_addrinfo *info, const struct nhop_object *nh)
262 if (info->rti_flags & RTF_PINNED) {
263 return (NH_IS_PINNED(nh)) ? 0 : 1;
265 return (NH_IS_PINNED(nh)) ? -1 : 0;
270 * Runs exact prefix match based on @dst and @netmask.
271 * Returns matched @rtentry if found or NULL.
272 * If rtentry was found, saves nexthop / weight value into @rnd.
274 static struct rtentry *
275 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
276 const struct sockaddr *netmask, struct route_nhop_data *rnd)
280 RIB_LOCK_ASSERT(rnh);
282 rt = (struct rtentry *)rnh->rnh_lookup(__DECONST(void *, dst),
283 __DECONST(void *, netmask), &rnh->head);
285 rnd->rnd_nhop = rt->rt_nhop;
286 rnd->rnd_weight = rt->rt_weight;
288 rnd->rnd_nhop = NULL;
296 * Runs exact prefix match based on dst/netmask from @info.
297 * Assumes RIB lock is held.
298 * Returns matched @rtentry if found or NULL.
299 * If rtentry was found, saves nexthop / weight value into @rnd.
302 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
303 struct route_nhop_data *rnd)
307 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
308 info->rti_info[RTAX_NETMASK], rnd);
314 * Adds route defined by @info into the kernel table specified by @fibnum and
315 * sa_family in @info->rti_info[RTAX_DST].
317 * Returns 0 on success and fills in operation metadata into @rc.
320 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
321 struct rib_cmd_info *rc)
323 struct rib_head *rnh;
328 rnh = get_rnh(fibnum, info);
330 return (EAFNOSUPPORT);
333 * Check consistency between RTF_HOST flag and netmask
336 if (info->rti_flags & RTF_HOST)
337 info->rti_info[RTAX_NETMASK] = NULL;
338 else if (info->rti_info[RTAX_NETMASK] == NULL)
341 bzero(rc, sizeof(struct rib_cmd_info));
342 rc->rc_cmd = RTM_ADD;
344 error = add_route(rnh, info, rc);
346 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
352 * Creates rtentry and nexthop based on @info data.
353 * Return 0 and fills in rtentry into @prt on success,
354 * return errno otherwise.
357 create_rtentry(struct rib_head *rnh, struct rt_addrinfo *info,
358 struct rtentry **prt)
360 struct sockaddr *dst, *ndst, *gateway, *netmask;
362 struct nhop_object *nh;
366 dst = info->rti_info[RTAX_DST];
367 gateway = info->rti_info[RTAX_GATEWAY];
368 netmask = info->rti_info[RTAX_NETMASK];
369 flags = info->rti_flags;
371 if ((flags & RTF_GATEWAY) && !gateway)
373 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
374 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
377 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
380 if (info->rti_ifa == NULL) {
381 error = rt_getifa_fib(info, rnh->rib_fibnum);
385 ifa_ref(info->rti_ifa);
388 error = nhop_create_from_info(rnh, info, &nh);
390 ifa_free(info->rti_ifa);
394 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
396 ifa_free(info->rti_ifa);
400 rt->rte_flags = RTF_UP | flags;
404 memcpy(&rt->rt_dst, dst, dst->sa_len);
405 rt_key(rt) = &rt->rt_dst;
408 * point to the (possibly newly malloc'd) dest address.
410 ndst = (struct sockaddr *)rt_key(rt);
413 * make sure it contains the value we want (masked if needed).
416 rt_maskedcopy(dst, ndst, netmask);
418 bcopy(dst, ndst, dst->sa_len);
421 * We use the ifa reference returned by rt_getifa_fib().
422 * This moved from below so that rnh->rnh_addaddr() can
423 * examine the ifa and ifa->ifa_ifp if it so desires.
428 rt_setmetrics(info, rt);
435 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
436 struct rib_cmd_info *rc)
438 struct nhop_object *nh_orig;
439 struct route_nhop_data rnd;
440 struct nhop_object *nh;
441 struct rtentry *rt, *rt_orig;
444 error = create_rtentry(rnh, info, &rt);
448 rnd.rnd_nhop = rt->rt_nhop;
449 rnd.rnd_weight = rt->rt_weight;
454 struct sockaddr *netmask;
455 netmask = info->rti_info[RTAX_NETMASK];
456 /* do not permit exactly the same dst/mask/gw pair */
457 if (rt_mpath_capable(rnh) &&
458 rt_mpath_conflict(rnh, rt, netmask)) {
462 uma_zfree(V_rtzone, rt);
466 error = add_route_nhop(rnh, rt, info, &rnd, rc);
472 /* addition failed. Lookup prefix in the rib to determine the cause */
473 rt_orig = lookup_prefix(rnh, info, &rnd);
474 if (rt_orig == NULL) {
475 /* No prefix -> rnh_addaddr() failed to allocate memory */
478 uma_zfree(V_rtzone, rt);
482 /* We have existing route in the RIB. */
483 nh_orig = rnd.rnd_nhop;
484 /* Check if new route has higher preference */
485 if (can_override_nhop(info, nh_orig) > 0) {
486 /* Update nexthop to the new route */
487 change_route_nhop(rnh, rt_orig, info, &rnd, rc);
489 uma_zfree(V_rtzone, rt);
496 /* Unable to add - another route with the same preference exists */
500 uma_zfree(V_rtzone, rt);
506 * Removes route defined by @info from the kernel table specified by @fibnum and
507 * sa_family in @info->rti_info[RTAX_DST].
509 * Returns 0 on success and fills in operation metadata into @rc.
512 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
514 struct rib_head *rnh;
515 struct sockaddr *dst_orig, *netmask;
516 struct sockaddr_storage mdst;
521 rnh = get_rnh(fibnum, info);
523 return (EAFNOSUPPORT);
525 bzero(rc, sizeof(struct rib_cmd_info));
526 rc->rc_cmd = RTM_DELETE;
528 dst_orig = info->rti_info[RTAX_DST];
529 netmask = info->rti_info[RTAX_NETMASK];
531 if (netmask != NULL) {
532 /* Ensure @dst is always properly masked */
533 if (dst_orig->sa_len > sizeof(mdst))
535 rt_maskedcopy(dst_orig, (struct sockaddr *)&mdst, netmask);
536 info->rti_info[RTAX_DST] = (struct sockaddr *)&mdst;
538 error = del_route(rnh, info, rc);
539 info->rti_info[RTAX_DST] = dst_orig;
545 * Conditionally unlinks rtentry matching data inside @info from @rnh.
546 * Returns 0 on success with operation result stored in @rc.
548 * ESRCH - if prefix was not found,
549 * EADDRINUSE - if trying to delete higher priority route.
550 * ENOENT - if supplied filter function returned 0 (not matched).
553 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, struct rib_cmd_info *rc)
556 struct nhop_object *nh;
557 struct radix_node *rn;
558 struct route_nhop_data rnd;
561 rt = lookup_prefix(rnh, info, &rnd);
567 error = check_info_match_nhop(info, rt, nh);
571 if (can_override_nhop(info, nh) < 0)
575 * Remove the item from the tree and return it.
576 * Complain if it is not there and do no more processing.
579 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
580 if (rt_mpath_capable(rnh)) {
581 rn = rt_mpath_unlink(rnh, info, rt, &error);
586 rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST],
587 info->rti_info[RTAX_NETMASK], &rnh->head);
591 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
592 panic ("rtrequest delete");
595 rt->rte_flags &= ~RTF_UP;
597 /* Finalize notification */
599 rc->rc_cmd = RTM_DELETE;
601 rc->rc_nh_old = rt->rt_nhop;
602 rc->rc_nh_weight = rt->rt_weight;
603 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
609 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
610 struct rib_cmd_info *rc)
615 error = rt_unlinkrte(rnh, info, rc);
620 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
623 * If the caller wants it, then it can have it,
624 * the entry will be deleted after the end of the current epoch.
632 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
633 struct rib_cmd_info *rc)
636 struct route_nhop_data rnd_orig;
637 struct rib_head *rnh;
643 rnh = get_rnh(fibnum, info);
645 return (EAFNOSUPPORT);
647 bzero(rc, sizeof(struct rib_cmd_info));
648 rc->rc_cmd = RTM_CHANGE;
650 /* Check if updated gateway exists */
651 if ((info->rti_flags & RTF_GATEWAY) &&
652 (info->rti_info[RTAX_GATEWAY] == NULL))
656 * route change is done in multiple steps, with dropping and
657 * reacquiring lock. In the situations with multiple processes
658 * changes the same route in can lead to the case when route
659 * is changed between the steps. Address it by retrying the operation
660 * multiple times before failing.
664 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
665 info->rti_info[RTAX_NETMASK], &rnh->head);
674 * If we got multipath routes,
675 * we require users to specify a matching RTAX_GATEWAY.
677 if (rt_mpath_capable(rnh)) {
678 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
685 rnd_orig.rnd_nhop = rt->rt_nhop;
686 rnd_orig.rnd_weight = rt->rt_weight;
690 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
691 error = change_route(rnh, info, &rnd_orig, rc);
700 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
701 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
705 struct nhop_object *nh, *nh_orig;
706 struct route_nhop_data rnd_new;
709 nh_orig = rnd_orig->rnd_nhop;
714 * New gateway could require new ifaddr, ifp;
715 * flags may also be different; ifp may be specified
716 * by ll sockaddr when protocol address is ambiguous
718 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
719 info->rti_info[RTAX_GATEWAY] != NULL) ||
720 info->rti_info[RTAX_IFP] != NULL ||
721 (info->rti_info[RTAX_IFA] != NULL &&
722 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
723 error = rt_getifa_fib(info, rnh->rib_fibnum);
724 if (info->rti_ifa != NULL)
729 ifa_free(info->rti_ifa);
730 info->rti_ifa = NULL;
737 error = nhop_create_from_nhop(rnh, nh_orig, info, &nh);
739 ifa_free(info->rti_ifa);
740 info->rti_ifa = NULL;
745 rnd_new.rnd_nhop = nh;
746 if (info->rti_mflags & RTV_WEIGHT)
747 rnd_new.rnd_weight = info->rti_rmx->rmx_weight;
749 rnd_new.rnd_weight = rnd_orig->rnd_weight;
751 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
757 * Insert @rt with nhop data from @rnd_new to @rnh.
758 * Returns 0 on success and stores operation results in @rc.
761 add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
762 struct rt_addrinfo *info, struct route_nhop_data *rnd,
763 struct rib_cmd_info *rc)
765 struct sockaddr *ndst, *netmask;
766 struct radix_node *rn;
769 RIB_WLOCK_ASSERT(rnh);
771 ndst = (struct sockaddr *)rt_key(rt);
772 netmask = info->rti_info[RTAX_NETMASK];
774 rt->rt_nhop = rnd->rnd_nhop;
775 rt->rt_weight = rnd->rnd_weight;
776 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
779 if (rt->rt_expire > 0)
780 tmproutes_update(rnh, rt);
782 /* Finalize notification */
785 rc->rc_cmd = RTM_ADD;
787 rc->rc_nh_old = NULL;
788 rc->rc_nh_new = rnd->rnd_nhop;
789 rc->rc_nh_weight = rnd->rnd_weight;
791 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
793 /* Existing route or memory allocation failure */
801 * Switch @rt nhop/weigh to the ones specified in @rnd.
802 * Conditionally set rt_expire if set in @info.
803 * Returns 0 on success.
806 change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
807 struct rt_addrinfo *info, struct route_nhop_data *rnd,
808 struct rib_cmd_info *rc)
810 struct nhop_object *nh_orig;
812 RIB_WLOCK_ASSERT(rnh);
814 nh_orig = rt->rt_nhop;
816 if (rnd->rnd_nhop != NULL) {
817 /* Changing expiration & nexthop & weight to a new one */
818 rt_setmetrics(info, rt);
819 rt->rt_nhop = rnd->rnd_nhop;
820 rt->rt_weight = rnd->rnd_weight;
821 if (rt->rt_expire > 0)
822 tmproutes_update(rnh, rt);
824 /* Route deletion requested. */
825 struct sockaddr *ndst, *netmask;
826 struct radix_node *rn;
828 ndst = (struct sockaddr *)rt_key(rt);
829 netmask = info->rti_info[RTAX_NETMASK];
830 rn = rnh->rnh_deladdr(ndst, netmask, &rnh->head);
835 /* Finalize notification */
838 rc->rc_cmd = (rnd->rnd_nhop != NULL) ? RTM_CHANGE : RTM_DELETE;
840 rc->rc_nh_old = nh_orig;
841 rc->rc_nh_new = rnd->rnd_nhop;
842 rc->rc_nh_weight = rnd->rnd_weight;
844 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
850 * Conditionally update route nhop/weight IFF data in @nhd_orig is
851 * consistent with the current route data.
852 * Nexthop in @nhd_new is consumed.
855 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
856 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
857 struct route_nhop_data *rnd_new, struct rib_cmd_info *rc)
859 struct rtentry *rt_new;
864 rt_new = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
865 info->rti_info[RTAX_NETMASK], &rnh->head);
867 if (rt_new == NULL) {
868 if (rnd_orig->rnd_nhop == NULL)
869 error = add_route_nhop(rnh, rt, info, rnd_new, rc);
872 * Prefix does not exist, which was not our assumption.
873 * Update @rnd_orig with the new data and return
875 rnd_orig->rnd_nhop = NULL;
876 rnd_orig->rnd_weight = 0;
880 /* Prefix exists, try to update */
881 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
883 * Nhop/mpath group hasn't changed. Flip
884 * to the new precalculated one and return
886 error = change_route_nhop(rnh, rt_new, info, rnd_new, rc);
888 /* Update and retry */
889 rnd_orig->rnd_nhop = rt_new->rt_nhop;
890 rnd_orig->rnd_weight = rt_new->rt_weight;
898 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
900 if (rnd_orig->rnd_nhop != NULL)
901 nhop_free_any(rnd_orig->rnd_nhop);
904 if (rnd_new->rnd_nhop != NULL)
905 nhop_free_any(rnd_new->rnd_nhop);
912 * Performs modification of routing table specificed by @action.
913 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
914 * Needs to be run in network epoch.
916 * Returns 0 on success and fills in @rc with action result.
919 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
920 struct rib_cmd_info *rc)
926 error = rib_add_route(fibnum, info, rc);
929 error = rib_del_route(fibnum, info, rc);
932 error = rib_change_route(fibnum, info, rc);
943 struct rt_addrinfo info;
944 struct rib_head *rnh;
945 struct rtentry *head;
946 struct rib_cmd_info rc;
950 * Conditionally unlinks @rn from radix tree based
951 * on info data passed in @arg.
954 rt_checkdelroute(struct radix_node *rn, void *arg)
956 struct rt_delinfo *di;
957 struct rt_addrinfo *info;
961 di = (struct rt_delinfo *)arg;
962 rt = (struct rtentry *)rn;
965 info->rti_info[RTAX_DST] = rt_key(rt);
966 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
967 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
969 error = rt_unlinkrte(di->rnh, info, &di->rc);
972 * Add deleted rtentries to the list to GC them
973 * after dropping the lock.
975 * XXX: Delayed notifications not implemented
976 * for nexthop updates.
979 /* Add to the list and return */
980 rt->rt_chain = di->head;
988 * Iterates over a routing table specified by @fibnum and @family and
989 * deletes elements marked by @filter_f.
991 * @family: AF_ address family
992 * @filter_f: function returning non-zero value for items to delete
993 * @arg: data to pass to the @filter_f function
994 * @report: true if rtsock notification is needed.
997 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report)
999 struct rib_head *rnh;
1000 struct rt_delinfo di;
1002 struct epoch_tracker et;
1004 rnh = rt_tables_get_rnh(fibnum, family);
1008 bzero(&di, sizeof(di));
1009 di.info.rti_filter = filter_f;
1010 di.info.rti_filterdata = arg;
1012 di.rc.rc_cmd = RTM_DELETE;
1014 NET_EPOCH_ENTER(et);
1017 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1020 /* We might have something to reclaim. */
1021 bzero(&di.rc, sizeof(di.rc));
1022 di.rc.rc_cmd = RTM_DELETE;
1023 while (di.head != NULL) {
1025 di.head = rt->rt_chain;
1026 rt->rt_chain = NULL;
1029 di.rc.rc_nh_old = rt->rt_nhop;
1030 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1032 /* TODO std rt -> rt_addrinfo export */
1033 di.info.rti_info[RTAX_DST] = rt_key(rt);
1034 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1037 rt_routemsg(RTM_DELETE, rt, rt->rt_nhop->nh_ifp, 0,
1046 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1047 struct rib_cmd_info *rc)
1049 struct rib_subscription *rs;
1051 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1052 if (rs->type == type)
1053 rs->func(rnh, rc, rs->arg);
1057 static struct rib_subscription *
1058 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1059 enum rib_subscription_type type, bool waitok)
1061 struct rib_subscription *rs;
1062 int flags = M_ZERO | (waitok ? M_WAITOK : 0);
1064 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1076 * Subscribe for the changes in the routing table specified by @fibnum and
1079 * Returns pointer to the subscription structure on success.
1081 struct rib_subscription *
1082 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1083 enum rib_subscription_type type, bool waitok)
1085 struct rib_head *rnh;
1086 struct rib_subscription *rs;
1087 struct epoch_tracker et;
1089 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1092 NET_EPOCH_ENTER(et);
1093 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1094 rnh = rt_tables_get_rnh(fibnum, family);
1097 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
1104 struct rib_subscription *
1105 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1106 enum rib_subscription_type type, bool waitok)
1108 struct rib_subscription *rs;
1109 struct epoch_tracker et;
1111 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1114 NET_EPOCH_ENTER(et);
1116 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
1124 * Remove rtable subscription @rs from the table specified by @fibnum
1126 * Needs to be run in network epoch.
1128 * Returns 0 on success.
1131 rib_unsibscribe(uint32_t fibnum, int family, struct rib_subscription *rs)
1133 struct rib_head *rnh;
1136 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1137 rnh = rt_tables_get_rnh(fibnum, family);
1143 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1146 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1153 * Epoch callback indicating subscription is safe to destroy
1156 destroy_subscription_epoch(epoch_context_t ctx)
1158 struct rib_subscription *rs;
1160 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1166 rib_init_subscriptions(struct rib_head *rnh)
1169 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1173 rib_destroy_subscriptions(struct rib_head *rnh)
1175 struct rib_subscription *rs;
1176 struct epoch_tracker et;
1178 NET_EPOCH_ENTER(et);
1180 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1181 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1182 epoch_call(net_epoch_preempt, destroy_subscription_epoch,