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)
135 * At this moment rnh, nh_control may be already freed.
136 * nhop interface may have been migrated to a different vnet.
137 * Use vnet stored in the nexthop to delete the entry.
139 CURVNET_SET(nhop_get_vnet(rt->rt_nhop));
141 /* Unreference nexthop */
142 nhop_free_any(rt->rt_nhop);
144 uma_zfree(V_rtzone, rt);
150 * Epoch callback indicating rtentry is safe to destroy
153 destroy_rtentry_epoch(epoch_context_t ctx)
157 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
163 * Schedule rtentry deletion
166 rtfree(struct rtentry *rt)
169 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
171 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
175 static struct rib_head *
176 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
178 struct rib_head *rnh;
179 struct sockaddr *dst;
181 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
183 dst = info->rti_info[RTAX_DST];
184 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
191 rib_can_multipath(struct rib_head *rh)
195 CURVNET_SET(rh->rib_vnet);
196 result = !!V_rib_route_multipath;
203 * Check is nhop is multipath-eligible.
204 * Avoid nhops without gateways and redirects.
206 * Returns 1 for multipath-eligible nexthop,
210 nhop_can_multipath(const struct nhop_object *nh)
213 if ((nh->nh_flags & NHF_MULTIPATH) != 0)
215 if ((nh->nh_flags & NHF_GATEWAY) == 0)
217 if ((nh->nh_flags & NHF_REDIRECT) != 0)
225 get_info_weight(const struct rt_addrinfo *info, uint32_t default_weight)
229 if (info->rti_mflags & RTV_WEIGHT)
230 weight = info->rti_rmx->rmx_weight;
232 weight = default_weight;
233 /* Keep upper 1 byte for adm distance purposes */
234 if (weight > RT_MAX_WEIGHT)
235 weight = RT_MAX_WEIGHT;
241 rt_is_host(const struct rtentry *rt)
244 return (rt->rte_flags & RTF_HOST);
248 rt_get_family(const struct rtentry *rt)
250 const struct sockaddr *dst;
252 dst = (const struct sockaddr *)rt_key_const(rt);
254 return (dst->sa_family);
258 * Returns pointer to nexthop or nexthop group
259 * associated with @rt
262 rt_get_raw_nhop(const struct rtentry *rt)
265 return (rt->rt_nhop);
270 * Stores IPv4 address and prefix length of @rt inside
272 * @pscopeid is currently always set to 0.
275 rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
276 int *plen, uint32_t *pscopeid)
278 const struct sockaddr_in *dst;
280 dst = (const struct sockaddr_in *)rt_key_const(rt);
281 KASSERT((dst->sin_family == AF_INET),
282 ("rt family is %d, not inet", dst->sin_family));
283 *paddr = dst->sin_addr;
284 dst = (const struct sockaddr_in *)rt_mask_const(rt);
288 *plen = bitcount32(dst->sin_addr.s_addr);
293 * Stores IPv4 address and prefix mask of @rt inside
294 * @paddr and @pmask. Sets mask to INADDR_ANY for host routes.
295 * @pscopeid is currently always set to 0.
298 rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
299 struct in_addr *pmask, uint32_t *pscopeid)
301 const struct sockaddr_in *dst;
303 dst = (const struct sockaddr_in *)rt_key_const(rt);
304 KASSERT((dst->sin_family == AF_INET),
305 ("rt family is %d, not inet", dst->sin_family));
306 *paddr = dst->sin_addr;
307 dst = (const struct sockaddr_in *)rt_mask_const(rt);
309 pmask->s_addr = INADDR_BROADCAST;
311 *pmask = dst->sin_addr;
318 inet6_get_plen(const struct in6_addr *addr)
321 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
322 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
326 * Stores IPv6 address and prefix length of @rt inside
327 * @paddr and @plen. Addresses are returned in de-embedded form.
328 * Scopeid is set to 0 for non-LL addresses.
331 rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
332 int *plen, uint32_t *pscopeid)
334 const struct sockaddr_in6 *dst;
336 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
337 KASSERT((dst->sin6_family == AF_INET6),
338 ("rt family is %d, not inet6", dst->sin6_family));
339 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
340 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
342 *paddr = dst->sin6_addr;
343 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
347 *plen = inet6_get_plen(&dst->sin6_addr);
351 * Stores IPv6 address and prefix mask of @rt inside
352 * @paddr and @pmask. Addresses are returned in de-embedded form.
353 * Scopeid is set to 0 for non-LL addresses.
356 rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
357 struct in6_addr *pmask, uint32_t *pscopeid)
359 const struct sockaddr_in6 *dst;
361 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
362 KASSERT((dst->sin6_family == AF_INET6),
363 ("rt family is %d, not inet", dst->sin6_family));
364 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
365 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
367 *paddr = dst->sin6_addr;
368 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
370 memset(pmask, 0xFF, sizeof(struct in6_addr));
372 *pmask = dst->sin6_addr;
377 rt_set_expire_info(struct rtentry *rt, const struct rt_addrinfo *info)
380 /* Kernel -> userland timebase conversion. */
381 if (info->rti_mflags & RTV_EXPIRE)
382 rt->rt_expire = info->rti_rmx->rmx_expire ?
383 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
387 * Check if specified @gw matches gw data in the nexthop @nh.
389 * Returns true if matches, false otherwise.
392 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
395 if (nh->gw_sa.sa_family != gw->sa_family)
398 switch (gw->sa_family) {
400 return (nh->gw4_sa.sin_addr.s_addr ==
401 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
404 const struct sockaddr_in6 *gw6;
405 gw6 = (const struct sockaddr_in6 *)gw;
408 * Currently (2020-09) IPv6 gws in kernel have their
409 * scope embedded. Once this becomes false, this code
410 * has to be revisited.
412 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
419 const struct sockaddr_dl *sdl;
420 sdl = (const struct sockaddr_dl *)gw;
421 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
424 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
432 * Checks if data in @info matches nexhop @nh.
434 * Returns 0 on success,
435 * ESRCH if not matched,
436 * ENOENT if filter function returned false
439 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
440 const struct nhop_object *nh)
442 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
444 if (info->rti_filter != NULL) {
445 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
450 if ((gw != NULL) && !match_nhop_gw(nh, gw))
457 * Checks if nexhop @nh can be rewritten by data in @info because
458 * of higher "priority". Currently the only case for such scenario
459 * is kernel installing interface routes, marked by RTF_PINNED flag.
462 * 1 if @info data has higher priority
463 * 0 if priority is the same
464 * -1 if priority is lower
467 can_override_nhop(const struct rt_addrinfo *info, const struct nhop_object *nh)
470 if (info->rti_flags & RTF_PINNED) {
471 return (NH_IS_PINNED(nh)) ? 0 : 1;
473 return (NH_IS_PINNED(nh)) ? -1 : 0;
478 * Runs exact prefix match based on @dst and @netmask.
479 * Returns matched @rtentry if found or NULL.
480 * If rtentry was found, saves nexthop / weight value into @rnd.
482 static struct rtentry *
483 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
484 const struct sockaddr *netmask, struct route_nhop_data *rnd)
488 RIB_LOCK_ASSERT(rnh);
490 rt = (struct rtentry *)rnh->rnh_lookup(__DECONST(void *, dst),
491 __DECONST(void *, netmask), &rnh->head);
493 rnd->rnd_nhop = rt->rt_nhop;
494 rnd->rnd_weight = rt->rt_weight;
496 rnd->rnd_nhop = NULL;
504 * Runs exact prefix match based on dst/netmask from @info.
505 * Assumes RIB lock is held.
506 * Returns matched @rtentry if found or NULL.
507 * If rtentry was found, saves nexthop / weight value into @rnd.
510 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
511 struct route_nhop_data *rnd)
515 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
516 info->rti_info[RTAX_NETMASK], rnd);
522 * Adds route defined by @info into the kernel table specified by @fibnum and
523 * sa_family in @info->rti_info[RTAX_DST].
525 * Returns 0 on success and fills in operation metadata into @rc.
528 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
529 struct rib_cmd_info *rc)
531 struct rib_head *rnh;
536 rnh = get_rnh(fibnum, info);
538 return (EAFNOSUPPORT);
541 * Check consistency between RTF_HOST flag and netmask
544 if (info->rti_flags & RTF_HOST)
545 info->rti_info[RTAX_NETMASK] = NULL;
546 else if (info->rti_info[RTAX_NETMASK] == NULL)
549 bzero(rc, sizeof(struct rib_cmd_info));
550 rc->rc_cmd = RTM_ADD;
552 error = add_route(rnh, info, rc);
554 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
560 * Creates rtentry and nexthop based on @info data.
561 * Return 0 and fills in rtentry into @prt on success,
562 * return errno otherwise.
565 create_rtentry(struct rib_head *rnh, struct rt_addrinfo *info,
566 struct rtentry **prt)
568 struct sockaddr *dst, *ndst, *gateway, *netmask;
570 struct nhop_object *nh;
574 dst = info->rti_info[RTAX_DST];
575 gateway = info->rti_info[RTAX_GATEWAY];
576 netmask = info->rti_info[RTAX_NETMASK];
577 flags = info->rti_flags;
579 if ((flags & RTF_GATEWAY) && !gateway)
581 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
582 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
585 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
588 if (info->rti_ifa == NULL) {
589 error = rt_getifa_fib(info, rnh->rib_fibnum);
593 ifa_ref(info->rti_ifa);
596 error = nhop_create_from_info(rnh, info, &nh);
597 ifa_free(info->rti_ifa);
601 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
606 rt->rte_flags = (RTF_UP | flags) & RTE_RT_FLAG_MASK;
610 memcpy(&rt->rt_dst, dst, dst->sa_len);
611 rt_key(rt) = &rt->rt_dst;
614 * point to the (possibly newly malloc'd) dest address.
616 ndst = (struct sockaddr *)rt_key(rt);
619 * make sure it contains the value we want (masked if needed).
622 rt_maskedcopy(dst, ndst, netmask);
624 bcopy(dst, ndst, dst->sa_len);
627 * We use the ifa reference returned by rt_getifa_fib().
628 * This moved from below so that rnh->rnh_addaddr() can
629 * examine the ifa and ifa->ifa_ifp if it so desires.
632 rt->rt_weight = get_info_weight(info, RT_DEFAULT_WEIGHT);
633 rt_set_expire_info(rt, info);
640 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
641 struct rib_cmd_info *rc)
643 struct nhop_object *nh_orig;
644 struct route_nhop_data rnd_orig, rnd_add;
645 struct nhop_object *nh;
646 struct rtentry *rt, *rt_orig;
649 error = create_rtentry(rnh, info, &rt);
653 rnd_add.rnd_nhop = rt->rt_nhop;
654 rnd_add.rnd_weight = rt->rt_weight;
658 error = add_route_nhop(rnh, rt, info, &rnd_add, rc);
664 /* addition failed. Lookup prefix in the rib to determine the cause */
665 rt_orig = lookup_prefix(rnh, info, &rnd_orig);
666 if (rt_orig == NULL) {
667 /* No prefix -> rnh_addaddr() failed to allocate memory */
670 uma_zfree(V_rtzone, rt);
674 /* We have existing route in the RIB. */
675 nh_orig = rnd_orig.rnd_nhop;
676 /* Check if new route has higher preference */
677 if (can_override_nhop(info, nh_orig) > 0) {
678 /* Update nexthop to the new route */
679 change_route_nhop(rnh, rt_orig, info, &rnd_add, rc);
681 uma_zfree(V_rtzone, rt);
689 if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) &&
690 nhop_can_multipath(rnd_orig.rnd_nhop))
691 error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc);
694 /* Unable to add - another route with the same preference exists */
698 * ROUTE_MPATH disabled: failed to add route, free both nhop and rt.
699 * ROUTE_MPATH enabled: original nhop reference is unused in any case,
700 * free rt only if not _adding_ new route to rib (e.g. the case
701 * when initial lookup returned existing route, but then it got
702 * deleted prior to multipath group insertion, leading to a simple
703 * non-multipath add as a result).
706 if ((error != 0) || rc->rc_cmd != RTM_ADD)
707 uma_zfree(V_rtzone, rt);
713 * Removes route defined by @info from the kernel table specified by @fibnum and
714 * sa_family in @info->rti_info[RTAX_DST].
716 * Returns 0 on success and fills in operation metadata into @rc.
719 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
721 struct rib_head *rnh;
722 struct sockaddr *dst_orig, *netmask;
723 struct sockaddr_storage mdst;
728 rnh = get_rnh(fibnum, info);
730 return (EAFNOSUPPORT);
732 bzero(rc, sizeof(struct rib_cmd_info));
733 rc->rc_cmd = RTM_DELETE;
735 dst_orig = info->rti_info[RTAX_DST];
736 netmask = info->rti_info[RTAX_NETMASK];
738 if (netmask != NULL) {
739 /* Ensure @dst is always properly masked */
740 if (dst_orig->sa_len > sizeof(mdst))
742 rt_maskedcopy(dst_orig, (struct sockaddr *)&mdst, netmask);
743 info->rti_info[RTAX_DST] = (struct sockaddr *)&mdst;
745 error = del_route(rnh, info, rc);
746 info->rti_info[RTAX_DST] = dst_orig;
752 * Conditionally unlinks rtentry matching data inside @info from @rnh.
753 * Returns 0 on success with operation result stored in @rc.
755 * ESRCH - if prefix was not found,
756 * EADDRINUSE - if trying to delete higher priority route.
757 * ENOENT - if supplied filter function returned 0 (not matched).
760 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, struct rib_cmd_info *rc)
763 struct nhop_object *nh;
764 struct radix_node *rn;
765 struct route_nhop_data rnd;
768 rt = lookup_prefix(rnh, info, &rnd);
774 if (NH_IS_NHGRP(nh)) {
775 error = del_route_mpath(rnh, info, rt,
776 (struct nhgrp_object *)nh, rc);
780 error = check_info_match_nhop(info, rt, nh);
784 if (can_override_nhop(info, nh) < 0)
788 * Remove the item from the tree and return it.
789 * Complain if it is not there and do no more processing.
791 rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST],
792 info->rti_info[RTAX_NETMASK], &rnh->head);
796 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
797 panic ("rtrequest delete");
800 rt->rte_flags &= ~RTF_UP;
802 /* Finalize notification */
806 rc->rc_cmd = RTM_DELETE;
808 rc->rc_nh_old = rt->rt_nhop;
809 rc->rc_nh_weight = rt->rt_weight;
810 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
816 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
817 struct rib_cmd_info *rc)
822 error = rt_unlinkrte(rnh, info, rc);
827 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
830 * If the caller wants it, then it can have it,
831 * the entry will be deleted after the end of the current epoch.
833 if (rc->rc_cmd == RTM_DELETE)
838 * Deleting 1 path may result in RTM_CHANGE to
839 * a different mpath group/nhop.
840 * Free old mpath group.
842 nhop_free_any(rc->rc_nh_old);
850 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
851 struct rib_cmd_info *rc)
854 struct route_nhop_data rnd_orig;
855 struct rib_head *rnh;
861 rnh = get_rnh(fibnum, info);
863 return (EAFNOSUPPORT);
865 bzero(rc, sizeof(struct rib_cmd_info));
866 rc->rc_cmd = RTM_CHANGE;
868 /* Check if updated gateway exists */
869 if ((info->rti_flags & RTF_GATEWAY) &&
870 (info->rti_info[RTAX_GATEWAY] == NULL)) {
873 * route(8) adds RTF_GATEWAY flag if -interface is not set.
874 * Remove RTF_GATEWAY to enforce consistency and maintain
877 info->rti_flags &= ~RTF_GATEWAY;
881 * route change is done in multiple steps, with dropping and
882 * reacquiring lock. In the situations with multiple processes
883 * changes the same route in can lead to the case when route
884 * is changed between the steps. Address it by retrying the operation
885 * multiple times before failing.
889 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
890 info->rti_info[RTAX_NETMASK], &rnh->head);
897 rnd_orig.rnd_nhop = rt->rt_nhop;
898 rnd_orig.rnd_weight = rt->rt_weight;
902 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
903 error = change_route(rnh, info, &rnd_orig, rc);
912 change_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
913 struct nhop_object *nh_orig, struct nhop_object **nh_new)
919 * New gateway could require new ifaddr, ifp;
920 * flags may also be different; ifp may be specified
921 * by ll sockaddr when protocol address is ambiguous
923 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
924 info->rti_info[RTAX_GATEWAY] != NULL) ||
925 info->rti_info[RTAX_IFP] != NULL ||
926 (info->rti_info[RTAX_IFA] != NULL &&
927 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
928 error = rt_getifa_fib(info, rnh->rib_fibnum);
929 if (info->rti_ifa != NULL)
934 ifa_free(info->rti_ifa);
935 info->rti_ifa = NULL;
942 error = nhop_create_from_nhop(rnh, nh_orig, info, nh_new);
944 ifa_free(info->rti_ifa);
945 info->rti_ifa = NULL;
953 change_mpath_route(struct rib_head *rnh, struct rt_addrinfo *info,
954 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
957 struct nhop_object *nh, *nh_orig, *nh_new;
958 struct route_nhop_data rnd_new;
961 nh_orig = rnd_orig->rnd_nhop;
963 struct weightened_nhop *wn = NULL, *wn_new;
966 wn = nhgrp_get_nhops((struct nhgrp_object *)nh_orig, &num_nhops);
968 for (int i = 0; i < num_nhops; i++) {
969 if (check_info_match_nhop(info, NULL, wn[i].nh)) {
978 error = change_nhop(rnh, info, nh_orig, &nh_new);
982 wn_new = mallocarray(num_nhops, sizeof(struct weightened_nhop),
983 M_TEMP, M_NOWAIT | M_ZERO);
984 if (wn_new == NULL) {
989 memcpy(wn_new, wn, num_nhops * sizeof(struct weightened_nhop));
990 for (int i = 0; i < num_nhops; i++) {
991 if (wn[i].nh == nh_orig) {
993 wn[i].weight = get_info_weight(info, rnd_orig->rnd_weight);
998 error = nhgrp_get_group(rnh, wn_new, num_nhops, &rnd_new);
1000 free(wn_new, M_TEMP);
1005 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1012 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
1013 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
1016 struct nhop_object *nh, *nh_orig;
1017 struct route_nhop_data rnd_new;
1020 nh_orig = rnd_orig->rnd_nhop;
1021 if (nh_orig == NULL)
1025 if (NH_IS_NHGRP(nh_orig))
1026 return (change_mpath_route(rnh, info, rnd_orig, rc));
1029 rnd_new.rnd_weight = get_info_weight(info, rnd_orig->rnd_weight);
1030 error = change_nhop(rnh, info, nh_orig, &rnd_new.rnd_nhop);
1033 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1039 * Insert @rt with nhop data from @rnd_new to @rnh.
1040 * Returns 0 on success and stores operation results in @rc.
1043 add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1044 struct rt_addrinfo *info, struct route_nhop_data *rnd,
1045 struct rib_cmd_info *rc)
1047 struct sockaddr *ndst, *netmask;
1048 struct radix_node *rn;
1051 RIB_WLOCK_ASSERT(rnh);
1053 ndst = (struct sockaddr *)rt_key(rt);
1054 netmask = info->rti_info[RTAX_NETMASK];
1056 rt->rt_nhop = rnd->rnd_nhop;
1057 rt->rt_weight = rnd->rnd_weight;
1058 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
1061 if (rt->rt_expire > 0)
1062 tmproutes_update(rnh, rt);
1064 /* Finalize notification */
1066 rnh->rnh_prefixes++;
1068 rc->rc_cmd = RTM_ADD;
1070 rc->rc_nh_old = NULL;
1071 rc->rc_nh_new = rnd->rnd_nhop;
1072 rc->rc_nh_weight = rnd->rnd_weight;
1074 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1076 /* Existing route or memory allocation failure */
1084 * Switch @rt nhop/weigh to the ones specified in @rnd.
1085 * Conditionally set rt_expire if set in @info.
1086 * Returns 0 on success.
1089 change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1090 struct rt_addrinfo *info, struct route_nhop_data *rnd,
1091 struct rib_cmd_info *rc)
1093 struct nhop_object *nh_orig;
1095 RIB_WLOCK_ASSERT(rnh);
1097 nh_orig = rt->rt_nhop;
1099 if (rnd->rnd_nhop != NULL) {
1100 /* Changing expiration & nexthop & weight to a new one */
1101 rt_set_expire_info(rt, info);
1102 rt->rt_nhop = rnd->rnd_nhop;
1103 rt->rt_weight = rnd->rnd_weight;
1104 if (rt->rt_expire > 0)
1105 tmproutes_update(rnh, rt);
1107 /* Route deletion requested. */
1108 struct sockaddr *ndst, *netmask;
1109 struct radix_node *rn;
1111 ndst = (struct sockaddr *)rt_key(rt);
1112 netmask = info->rti_info[RTAX_NETMASK];
1113 rn = rnh->rnh_deladdr(ndst, netmask, &rnh->head);
1117 rt->rte_flags &= ~RTF_UP;
1120 /* Finalize notification */
1122 if (rnd->rnd_nhop == NULL)
1123 rnh->rnh_prefixes--;
1125 rc->rc_cmd = (rnd->rnd_nhop != NULL) ? RTM_CHANGE : RTM_DELETE;
1127 rc->rc_nh_old = nh_orig;
1128 rc->rc_nh_new = rnd->rnd_nhop;
1129 rc->rc_nh_weight = rnd->rnd_weight;
1131 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1137 * Conditionally update route nhop/weight IFF data in @nhd_orig is
1138 * consistent with the current route data.
1139 * Nexthop in @nhd_new is consumed.
1142 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
1143 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
1144 struct route_nhop_data *rnd_new, struct rib_cmd_info *rc)
1146 struct rtentry *rt_new;
1151 rt_new = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1152 info->rti_info[RTAX_NETMASK], &rnh->head);
1154 if (rt_new == NULL) {
1155 if (rnd_orig->rnd_nhop == NULL)
1156 error = add_route_nhop(rnh, rt, info, rnd_new, rc);
1159 * Prefix does not exist, which was not our assumption.
1160 * Update @rnd_orig with the new data and return
1162 rnd_orig->rnd_nhop = NULL;
1163 rnd_orig->rnd_weight = 0;
1167 /* Prefix exists, try to update */
1168 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
1170 * Nhop/mpath group hasn't changed. Flip
1171 * to the new precalculated one and return
1173 error = change_route_nhop(rnh, rt_new, info, rnd_new, rc);
1175 /* Update and retry */
1176 rnd_orig->rnd_nhop = rt_new->rt_nhop;
1177 rnd_orig->rnd_weight = rt_new->rt_weight;
1185 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
1187 if (rnd_orig->rnd_nhop != NULL)
1188 nhop_free_any(rnd_orig->rnd_nhop);
1191 if (rnd_new->rnd_nhop != NULL)
1192 nhop_free_any(rnd_new->rnd_nhop);
1199 * Performs modification of routing table specificed by @action.
1200 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
1201 * Needs to be run in network epoch.
1203 * Returns 0 on success and fills in @rc with action result.
1206 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
1207 struct rib_cmd_info *rc)
1213 error = rib_add_route(fibnum, info, rc);
1216 error = rib_del_route(fibnum, info, rc);
1219 error = rib_change_route(fibnum, info, rc);
1230 struct rt_addrinfo info;
1231 struct rib_head *rnh;
1232 struct rtentry *head;
1233 struct rib_cmd_info rc;
1237 * Conditionally unlinks @rn from radix tree based
1238 * on info data passed in @arg.
1241 rt_checkdelroute(struct radix_node *rn, void *arg)
1243 struct rt_delinfo *di;
1244 struct rt_addrinfo *info;
1248 di = (struct rt_delinfo *)arg;
1249 rt = (struct rtentry *)rn;
1252 info->rti_info[RTAX_DST] = rt_key(rt);
1253 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1255 error = rt_unlinkrte(di->rnh, info, &di->rc);
1258 * Add deleted rtentries to the list to GC them
1259 * after dropping the lock.
1261 * XXX: Delayed notifications not implemented
1262 * for nexthop updates.
1264 if ((error == 0) && (di->rc.rc_cmd == RTM_DELETE)) {
1265 /* Add to the list and return */
1266 rt->rt_chain = di->head;
1274 * Iterates over a routing table specified by @fibnum and @family and
1275 * deletes elements marked by @filter_f.
1276 * @fibnum: rtable id
1277 * @family: AF_ address family
1278 * @filter_f: function returning non-zero value for items to delete
1279 * @arg: data to pass to the @filter_f function
1280 * @report: true if rtsock notification is needed.
1283 rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f, void *arg, bool report)
1285 struct rib_head *rnh;
1286 struct rt_delinfo di;
1288 struct nhop_object *nh;
1289 struct epoch_tracker et;
1291 rnh = rt_tables_get_rnh(fibnum, family);
1295 bzero(&di, sizeof(di));
1296 di.info.rti_filter = filter_f;
1297 di.info.rti_filterdata = arg;
1299 di.rc.rc_cmd = RTM_DELETE;
1301 NET_EPOCH_ENTER(et);
1304 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1307 /* We might have something to reclaim. */
1308 bzero(&di.rc, sizeof(di.rc));
1309 di.rc.rc_cmd = RTM_DELETE;
1310 while (di.head != NULL) {
1312 di.head = rt->rt_chain;
1313 rt->rt_chain = NULL;
1317 di.rc.rc_nh_old = nh;
1318 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1320 /* TODO std rt -> rt_addrinfo export */
1321 di.info.rti_info[RTAX_DST] = rt_key(rt);
1322 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1326 struct nhgrp_object *nhg;
1327 struct weightened_nhop *wn;
1329 if (NH_IS_NHGRP(nh)) {
1330 nhg = (struct nhgrp_object *)nh;
1331 wn = nhgrp_get_nhops(nhg, &num_nhops);
1332 for (int i = 0; i < num_nhops; i++)
1333 rt_routemsg(RTM_DELETE, rt, wn[i].nh, fibnum);
1336 rt_routemsg(RTM_DELETE, rt, nh, fibnum);
1345 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1346 struct rib_cmd_info *rc)
1348 struct rib_subscription *rs;
1350 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1351 if (rs->type == type)
1352 rs->func(rnh, rc, rs->arg);
1356 static struct rib_subscription *
1357 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1358 enum rib_subscription_type type, bool waitok)
1360 struct rib_subscription *rs;
1361 int flags = M_ZERO | (waitok ? M_WAITOK : M_NOWAIT);
1363 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1375 * Subscribe for the changes in the routing table specified by @fibnum and
1378 * Returns pointer to the subscription structure on success.
1380 struct rib_subscription *
1381 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1382 enum rib_subscription_type type, bool waitok)
1384 struct rib_head *rnh;
1385 struct epoch_tracker et;
1387 NET_EPOCH_ENTER(et);
1388 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1389 rnh = rt_tables_get_rnh(fibnum, family);
1392 return (rib_subscribe_internal(rnh, f, arg, type, waitok));
1395 struct rib_subscription *
1396 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1397 enum rib_subscription_type type, bool waitok)
1399 struct rib_subscription *rs;
1400 struct epoch_tracker et;
1402 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1406 NET_EPOCH_ENTER(et);
1408 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1415 struct rib_subscription *
1416 rib_subscribe_locked(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1417 enum rib_subscription_type type)
1419 struct rib_subscription *rs;
1422 RIB_WLOCK_ASSERT(rnh);
1424 if ((rs = allocate_subscription(f, arg, type, false)) == NULL)
1428 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1434 * Remove rtable subscription @rs from the routing table.
1435 * Needs to be run in network epoch.
1438 rib_unsibscribe(struct rib_subscription *rs)
1440 struct rib_head *rnh = rs->rnh;
1445 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1448 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1453 rib_unsibscribe_locked(struct rib_subscription *rs)
1455 struct rib_head *rnh = rs->rnh;
1458 RIB_WLOCK_ASSERT(rnh);
1460 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1462 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1467 * Epoch callback indicating subscription is safe to destroy
1470 destroy_subscription_epoch(epoch_context_t ctx)
1472 struct rib_subscription *rs;
1474 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1480 rib_init_subscriptions(struct rib_head *rnh)
1483 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1487 rib_destroy_subscriptions(struct rib_head *rnh)
1489 struct rib_subscription *rs;
1490 struct epoch_tracker et;
1492 NET_EPOCH_ENTER(et);
1494 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1495 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1496 epoch_call(net_epoch_preempt, destroy_subscription_epoch,