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 #if defined(INET) && defined(INET6)
110 FEATURE(ipv4_rfc5549_support, "Route IPv4 packets via IPv6 nexthops");
111 #define V_rib_route_ipv6_nexthop VNET(rib_route_ipv6_nexthop)
112 VNET_DEFINE(u_int, rib_route_ipv6_nexthop) = 1;
113 SYSCTL_UINT(_net_route, OID_AUTO, ipv6_nexthop, CTLFLAG_RW | CTLFLAG_VNET,
114 &VNET_NAME(rib_route_ipv6_nexthop), 0, "Enable IPv4 route via IPv6 Next Hop address");
117 /* Routing table UMA zone */
118 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
119 #define V_rtzone VNET(rtzone)
125 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
126 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
131 vnet_rtzone_destroy()
134 uma_zdestroy(V_rtzone);
139 destroy_rtentry(struct rtentry *rt)
142 struct nhop_object *nh = rt->rt_nhop;
145 * At this moment rnh, nh_control may be already freed.
146 * nhop interface may have been migrated to a different vnet.
147 * Use vnet stored in the nexthop to delete the entry.
150 if (NH_IS_NHGRP(nh)) {
151 struct weightened_nhop *wn;
153 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
157 CURVNET_SET(nhop_get_vnet(nh));
160 /* Unreference nexthop */
161 nhop_free_any(rt->rt_nhop);
163 uma_zfree(V_rtzone, rt);
169 * Epoch callback indicating rtentry is safe to destroy
172 destroy_rtentry_epoch(epoch_context_t ctx)
176 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
182 * Schedule rtentry deletion
185 rtfree(struct rtentry *rt)
188 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
190 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
194 static struct rib_head *
195 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
197 struct rib_head *rnh;
198 struct sockaddr *dst;
200 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
202 dst = info->rti_info[RTAX_DST];
203 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
208 #if defined(INET) && defined(INET6)
210 rib_can_ipv6_nexthop_address(struct rib_head *rh)
214 CURVNET_SET(rh->rib_vnet);
215 result = !!V_rib_route_ipv6_nexthop;
224 rib_can_multipath(struct rib_head *rh)
228 CURVNET_SET(rh->rib_vnet);
229 result = !!V_rib_route_multipath;
236 * Check is nhop is multipath-eligible.
237 * Avoid nhops without gateways and redirects.
239 * Returns 1 for multipath-eligible nexthop,
243 nhop_can_multipath(const struct nhop_object *nh)
246 if ((nh->nh_flags & NHF_MULTIPATH) != 0)
248 if ((nh->nh_flags & NHF_GATEWAY) == 0)
250 if ((nh->nh_flags & NHF_REDIRECT) != 0)
258 get_info_weight(const struct rt_addrinfo *info, uint32_t default_weight)
262 if (info->rti_mflags & RTV_WEIGHT)
263 weight = info->rti_rmx->rmx_weight;
265 weight = default_weight;
266 /* Keep upper 1 byte for adm distance purposes */
267 if (weight > RT_MAX_WEIGHT)
268 weight = RT_MAX_WEIGHT;
269 else if (weight == 0)
270 weight = default_weight;
276 rt_is_host(const struct rtentry *rt)
279 return (rt->rte_flags & RTF_HOST);
283 rt_get_family(const struct rtentry *rt)
285 const struct sockaddr *dst;
287 dst = (const struct sockaddr *)rt_key_const(rt);
289 return (dst->sa_family);
293 * Returns pointer to nexthop or nexthop group
294 * associated with @rt
297 rt_get_raw_nhop(const struct rtentry *rt)
300 return (rt->rt_nhop);
305 * Stores IPv4 address and prefix length of @rt inside
307 * @pscopeid is currently always set to 0.
310 rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
311 int *plen, uint32_t *pscopeid)
313 const struct sockaddr_in *dst;
315 dst = (const struct sockaddr_in *)rt_key_const(rt);
316 KASSERT((dst->sin_family == AF_INET),
317 ("rt family is %d, not inet", dst->sin_family));
318 *paddr = dst->sin_addr;
319 dst = (const struct sockaddr_in *)rt_mask_const(rt);
323 *plen = bitcount32(dst->sin_addr.s_addr);
328 * Stores IPv4 address and prefix mask of @rt inside
329 * @paddr and @pmask. Sets mask to INADDR_ANY for host routes.
330 * @pscopeid is currently always set to 0.
333 rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
334 struct in_addr *pmask, uint32_t *pscopeid)
336 const struct sockaddr_in *dst;
338 dst = (const struct sockaddr_in *)rt_key_const(rt);
339 KASSERT((dst->sin_family == AF_INET),
340 ("rt family is %d, not inet", dst->sin_family));
341 *paddr = dst->sin_addr;
342 dst = (const struct sockaddr_in *)rt_mask_const(rt);
344 pmask->s_addr = INADDR_BROADCAST;
346 *pmask = dst->sin_addr;
353 inet6_get_plen(const struct in6_addr *addr)
356 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
357 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
361 * Stores IPv6 address and prefix length of @rt inside
362 * @paddr and @plen. Addresses are returned in de-embedded form.
363 * Scopeid is set to 0 for non-LL addresses.
366 rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
367 int *plen, uint32_t *pscopeid)
369 const struct sockaddr_in6 *dst;
371 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
372 KASSERT((dst->sin6_family == AF_INET6),
373 ("rt family is %d, not inet6", dst->sin6_family));
374 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
375 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
377 *paddr = dst->sin6_addr;
378 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
382 *plen = inet6_get_plen(&dst->sin6_addr);
386 * Stores IPv6 address and prefix mask of @rt inside
387 * @paddr and @pmask. Addresses are returned in de-embedded form.
388 * Scopeid is set to 0 for non-LL addresses.
391 rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
392 struct in6_addr *pmask, uint32_t *pscopeid)
394 const struct sockaddr_in6 *dst;
396 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
397 KASSERT((dst->sin6_family == AF_INET6),
398 ("rt family is %d, not inet", dst->sin6_family));
399 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
400 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
402 *paddr = dst->sin6_addr;
403 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
405 memset(pmask, 0xFF, sizeof(struct in6_addr));
407 *pmask = dst->sin6_addr;
412 rt_set_expire_info(struct rtentry *rt, const struct rt_addrinfo *info)
415 /* Kernel -> userland timebase conversion. */
416 if (info->rti_mflags & RTV_EXPIRE)
417 rt->rt_expire = info->rti_rmx->rmx_expire ?
418 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
422 * Check if specified @gw matches gw data in the nexthop @nh.
424 * Returns true if matches, false otherwise.
427 match_nhop_gw(const struct nhop_object *nh, const struct sockaddr *gw)
430 if (nh->gw_sa.sa_family != gw->sa_family)
433 switch (gw->sa_family) {
435 return (nh->gw4_sa.sin_addr.s_addr ==
436 ((const struct sockaddr_in *)gw)->sin_addr.s_addr);
439 const struct sockaddr_in6 *gw6;
440 gw6 = (const struct sockaddr_in6 *)gw;
443 * Currently (2020-09) IPv6 gws in kernel have their
444 * scope embedded. Once this becomes false, this code
445 * has to be revisited.
447 if (IN6_ARE_ADDR_EQUAL(&nh->gw6_sa.sin6_addr,
454 const struct sockaddr_dl *sdl;
455 sdl = (const struct sockaddr_dl *)gw;
456 return (nh->gwl_sa.sdl_index == sdl->sdl_index);
459 return (memcmp(&nh->gw_sa, gw, nh->gw_sa.sa_len) == 0);
467 * Checks if data in @info matches nexhop @nh.
469 * Returns 0 on success,
470 * ESRCH if not matched,
471 * ENOENT if filter function returned false
474 check_info_match_nhop(const struct rt_addrinfo *info, const struct rtentry *rt,
475 const struct nhop_object *nh)
477 const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
479 if (info->rti_filter != NULL) {
480 if (info->rti_filter(rt, nh, info->rti_filterdata) == 0)
485 if ((gw != NULL) && !match_nhop_gw(nh, gw))
492 * Checks if nexhop @nh can be rewritten by data in @info because
493 * of higher "priority". Currently the only case for such scenario
494 * is kernel installing interface routes, marked by RTF_PINNED flag.
497 * 1 if @info data has higher priority
498 * 0 if priority is the same
499 * -1 if priority is lower
502 can_override_nhop(const struct rt_addrinfo *info, const struct nhop_object *nh)
505 if (info->rti_flags & RTF_PINNED) {
506 return (NH_IS_PINNED(nh)) ? 0 : 1;
508 return (NH_IS_PINNED(nh)) ? -1 : 0;
513 * Runs exact prefix match based on @dst and @netmask.
514 * Returns matched @rtentry if found or NULL.
515 * If rtentry was found, saves nexthop / weight value into @rnd.
517 static struct rtentry *
518 lookup_prefix_bysa(struct rib_head *rnh, const struct sockaddr *dst,
519 const struct sockaddr *netmask, struct route_nhop_data *rnd)
523 RIB_LOCK_ASSERT(rnh);
525 rt = (struct rtentry *)rnh->rnh_lookup(__DECONST(void *, dst),
526 __DECONST(void *, netmask), &rnh->head);
528 rnd->rnd_nhop = rt->rt_nhop;
529 rnd->rnd_weight = rt->rt_weight;
531 rnd->rnd_nhop = NULL;
539 * Runs exact prefix match based on dst/netmask from @info.
540 * Assumes RIB lock is held.
541 * Returns matched @rtentry if found or NULL.
542 * If rtentry was found, saves nexthop / weight value into @rnd.
545 lookup_prefix(struct rib_head *rnh, const struct rt_addrinfo *info,
546 struct route_nhop_data *rnd)
550 rt = lookup_prefix_bysa(rnh, info->rti_info[RTAX_DST],
551 info->rti_info[RTAX_NETMASK], rnd);
557 * Adds route defined by @info into the kernel table specified by @fibnum and
558 * sa_family in @info->rti_info[RTAX_DST].
560 * Returns 0 on success and fills in operation metadata into @rc.
563 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
564 struct rib_cmd_info *rc)
566 struct rib_head *rnh;
571 rnh = get_rnh(fibnum, info);
573 return (EAFNOSUPPORT);
576 * Check consistency between RTF_HOST flag and netmask
579 if (info->rti_flags & RTF_HOST)
580 info->rti_info[RTAX_NETMASK] = NULL;
581 else if (info->rti_info[RTAX_NETMASK] == NULL)
584 bzero(rc, sizeof(struct rib_cmd_info));
585 rc->rc_cmd = RTM_ADD;
587 error = add_route(rnh, info, rc);
589 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
595 * Checks if @dst and @gateway is valid combination.
597 * Returns true if is valid, false otherwise.
600 check_gateway(struct rib_head *rnh, struct sockaddr *dst,
601 struct sockaddr *gateway)
603 if (dst->sa_family == gateway->sa_family)
605 else if (gateway->sa_family == AF_UNSPEC)
607 else if (gateway->sa_family == AF_LINK)
609 #if defined(INET) && defined(INET6)
610 else if (dst->sa_family == AF_INET && gateway->sa_family == AF_INET6 &&
611 rib_can_ipv6_nexthop_address(rnh))
619 * Creates rtentry and nexthop based on @info data.
620 * Return 0 and fills in rtentry into @prt on success,
621 * return errno otherwise.
624 create_rtentry(struct rib_head *rnh, struct rt_addrinfo *info,
625 struct rtentry **prt)
627 struct sockaddr *dst, *ndst, *gateway, *netmask;
629 struct nhop_object *nh;
633 dst = info->rti_info[RTAX_DST];
634 gateway = info->rti_info[RTAX_GATEWAY];
635 netmask = info->rti_info[RTAX_NETMASK];
636 flags = info->rti_flags;
638 if ((flags & RTF_GATEWAY) && !gateway)
640 if (dst && gateway && !check_gateway(rnh, dst, gateway))
643 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
646 if (info->rti_ifa == NULL) {
647 error = rt_getifa_fib(info, rnh->rib_fibnum);
652 error = nhop_create_from_info(rnh, info, &nh);
656 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
661 rt->rte_flags = (RTF_UP | flags) & RTE_RT_FLAG_MASK;
665 memcpy(&rt->rt_dst, dst, dst->sa_len);
666 rt_key(rt) = &rt->rt_dst;
669 * point to the (possibly newly malloc'd) dest address.
671 ndst = (struct sockaddr *)rt_key(rt);
674 * make sure it contains the value we want (masked if needed).
677 rt_maskedcopy(dst, ndst, netmask);
679 bcopy(dst, ndst, dst->sa_len);
682 * We use the ifa reference returned by rt_getifa_fib().
683 * This moved from below so that rnh->rnh_addaddr() can
684 * examine the ifa and ifa->ifa_ifp if it so desires.
687 rt->rt_weight = get_info_weight(info, RT_DEFAULT_WEIGHT);
688 rt_set_expire_info(rt, info);
695 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
696 struct rib_cmd_info *rc)
698 struct nhop_object *nh_orig;
699 struct route_nhop_data rnd_orig, rnd_add;
700 struct nhop_object *nh;
701 struct rtentry *rt, *rt_orig;
704 error = create_rtentry(rnh, info, &rt);
708 rnd_add.rnd_nhop = rt->rt_nhop;
709 rnd_add.rnd_weight = rt->rt_weight;
713 error = add_route_nhop(rnh, rt, info, &rnd_add, rc);
719 /* addition failed. Lookup prefix in the rib to determine the cause */
720 rt_orig = lookup_prefix(rnh, info, &rnd_orig);
721 if (rt_orig == NULL) {
722 /* No prefix -> rnh_addaddr() failed to allocate memory */
725 uma_zfree(V_rtzone, rt);
729 /* We have existing route in the RIB. */
730 nh_orig = rnd_orig.rnd_nhop;
731 /* Check if new route has higher preference */
732 if (can_override_nhop(info, nh_orig) > 0) {
733 /* Update nexthop to the new route */
734 change_route_nhop(rnh, rt_orig, info, &rnd_add, rc);
736 uma_zfree(V_rtzone, rt);
744 if (rib_can_multipath(rnh) && nhop_can_multipath(rnd_add.rnd_nhop) &&
745 nhop_can_multipath(rnd_orig.rnd_nhop))
746 error = add_route_mpath(rnh, info, rt, &rnd_add, &rnd_orig, rc);
749 /* Unable to add - another route with the same preference exists */
753 * ROUTE_MPATH disabled: failed to add route, free both nhop and rt.
754 * ROUTE_MPATH enabled: original nhop reference is unused in any case,
755 * free rt only if not _adding_ new route to rib (e.g. the case
756 * when initial lookup returned existing route, but then it got
757 * deleted prior to multipath group insertion, leading to a simple
758 * non-multipath add as a result).
761 if ((error != 0) || rc->rc_cmd != RTM_ADD)
762 uma_zfree(V_rtzone, rt);
768 * Removes route defined by @info from the kernel table specified by @fibnum and
769 * sa_family in @info->rti_info[RTAX_DST].
771 * Returns 0 on success and fills in operation metadata into @rc.
774 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
776 struct rib_head *rnh;
777 struct sockaddr *dst_orig, *netmask;
778 struct sockaddr_storage mdst;
783 rnh = get_rnh(fibnum, info);
785 return (EAFNOSUPPORT);
787 bzero(rc, sizeof(struct rib_cmd_info));
788 rc->rc_cmd = RTM_DELETE;
790 dst_orig = info->rti_info[RTAX_DST];
791 netmask = info->rti_info[RTAX_NETMASK];
793 if (netmask != NULL) {
794 /* Ensure @dst is always properly masked */
795 if (dst_orig->sa_len > sizeof(mdst))
797 rt_maskedcopy(dst_orig, (struct sockaddr *)&mdst, netmask);
798 info->rti_info[RTAX_DST] = (struct sockaddr *)&mdst;
800 error = del_route(rnh, info, rc);
801 info->rti_info[RTAX_DST] = dst_orig;
807 * Conditionally unlinks rtentry matching data inside @info from @rnh.
808 * Returns 0 on success with operation result stored in @rc.
810 * ESRCH - if prefix was not found,
811 * EADDRINUSE - if trying to delete higher priority route.
812 * ENOENT - if supplied filter function returned 0 (not matched).
815 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, struct rib_cmd_info *rc)
818 struct nhop_object *nh;
819 struct radix_node *rn;
820 struct route_nhop_data rnd;
823 rt = lookup_prefix(rnh, info, &rnd);
829 if (NH_IS_NHGRP(nh)) {
830 error = del_route_mpath(rnh, info, rt,
831 (struct nhgrp_object *)nh, rc);
835 error = check_info_match_nhop(info, rt, nh);
839 if (can_override_nhop(info, nh) < 0)
843 * Remove the item from the tree and return it.
844 * Complain if it is not there and do no more processing.
846 rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST],
847 info->rti_info[RTAX_NETMASK], &rnh->head);
851 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
852 panic ("rtrequest delete");
855 rt->rte_flags &= ~RTF_UP;
857 /* Finalize notification */
861 rc->rc_cmd = RTM_DELETE;
863 rc->rc_nh_old = rt->rt_nhop;
864 rc->rc_nh_weight = rt->rt_weight;
865 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
871 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
872 struct rib_cmd_info *rc)
877 error = rt_unlinkrte(rnh, info, rc);
882 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
885 * If the caller wants it, then it can have it,
886 * the entry will be deleted after the end of the current epoch.
888 if (rc->rc_cmd == RTM_DELETE)
893 * Deleting 1 path may result in RTM_CHANGE to
894 * a different mpath group/nhop.
895 * Free old mpath group.
897 nhop_free_any(rc->rc_nh_old);
905 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
906 struct rib_cmd_info *rc)
909 struct route_nhop_data rnd_orig;
910 struct rib_head *rnh;
916 rnh = get_rnh(fibnum, info);
918 return (EAFNOSUPPORT);
920 bzero(rc, sizeof(struct rib_cmd_info));
921 rc->rc_cmd = RTM_CHANGE;
923 /* Check if updated gateway exists */
924 if ((info->rti_flags & RTF_GATEWAY) &&
925 (info->rti_info[RTAX_GATEWAY] == NULL)) {
928 * route(8) adds RTF_GATEWAY flag if -interface is not set.
929 * Remove RTF_GATEWAY to enforce consistency and maintain
932 info->rti_flags &= ~RTF_GATEWAY;
936 * route change is done in multiple steps, with dropping and
937 * reacquiring lock. In the situations with multiple processes
938 * changes the same route in can lead to the case when route
939 * is changed between the steps. Address it by retrying the operation
940 * multiple times before failing.
944 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
945 info->rti_info[RTAX_NETMASK], &rnh->head);
952 rnd_orig.rnd_nhop = rt->rt_nhop;
953 rnd_orig.rnd_weight = rt->rt_weight;
957 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
958 error = change_route(rnh, info, &rnd_orig, rc);
967 change_nhop(struct rib_head *rnh, struct rt_addrinfo *info,
968 struct nhop_object *nh_orig, struct nhop_object **nh_new)
973 * New gateway could require new ifaddr, ifp;
974 * flags may also be different; ifp may be specified
975 * by ll sockaddr when protocol address is ambiguous
977 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
978 info->rti_info[RTAX_GATEWAY] != NULL) ||
979 info->rti_info[RTAX_IFP] != NULL ||
980 (info->rti_info[RTAX_IFA] != NULL &&
981 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
982 error = rt_getifa_fib(info, rnh->rib_fibnum);
985 info->rti_ifa = NULL;
990 error = nhop_create_from_nhop(rnh, nh_orig, info, nh_new);
991 info->rti_ifa = NULL;
998 change_mpath_route(struct rib_head *rnh, struct rt_addrinfo *info,
999 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
1002 struct nhop_object *nh, *nh_orig, *nh_new;
1003 struct route_nhop_data rnd_new;
1006 nh_orig = rnd_orig->rnd_nhop;
1008 struct weightened_nhop *wn = NULL, *wn_new;
1011 wn = nhgrp_get_nhops((struct nhgrp_object *)nh_orig, &num_nhops);
1013 for (int i = 0; i < num_nhops; i++) {
1014 if (check_info_match_nhop(info, NULL, wn[i].nh)) {
1020 if (nh_orig == NULL)
1023 error = change_nhop(rnh, info, nh_orig, &nh_new);
1027 wn_new = mallocarray(num_nhops, sizeof(struct weightened_nhop),
1028 M_TEMP, M_NOWAIT | M_ZERO);
1029 if (wn_new == NULL) {
1034 memcpy(wn_new, wn, num_nhops * sizeof(struct weightened_nhop));
1035 for (int i = 0; i < num_nhops; i++) {
1036 if (wn[i].nh == nh_orig) {
1038 wn[i].weight = get_info_weight(info, rnd_orig->rnd_weight);
1043 error = nhgrp_get_group(rnh, wn_new, num_nhops, &rnd_new);
1045 free(wn_new, M_TEMP);
1050 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1057 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
1058 struct route_nhop_data *rnd_orig, struct rib_cmd_info *rc)
1061 struct nhop_object *nh, *nh_orig;
1062 struct route_nhop_data rnd_new;
1065 nh_orig = rnd_orig->rnd_nhop;
1066 if (nh_orig == NULL)
1070 if (NH_IS_NHGRP(nh_orig))
1071 return (change_mpath_route(rnh, info, rnd_orig, rc));
1074 rnd_new.rnd_weight = get_info_weight(info, rnd_orig->rnd_weight);
1075 error = change_nhop(rnh, info, nh_orig, &rnd_new.rnd_nhop);
1078 error = change_route_conditional(rnh, NULL, info, rnd_orig, &rnd_new, rc);
1084 * Insert @rt with nhop data from @rnd_new to @rnh.
1085 * Returns 0 on success and stores operation results in @rc.
1088 add_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1089 struct rt_addrinfo *info, struct route_nhop_data *rnd,
1090 struct rib_cmd_info *rc)
1092 struct sockaddr *ndst, *netmask;
1093 struct radix_node *rn;
1096 RIB_WLOCK_ASSERT(rnh);
1098 ndst = (struct sockaddr *)rt_key(rt);
1099 netmask = info->rti_info[RTAX_NETMASK];
1101 rt->rt_nhop = rnd->rnd_nhop;
1102 rt->rt_weight = rnd->rnd_weight;
1103 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
1106 if (rt->rt_expire > 0)
1107 tmproutes_update(rnh, rt);
1109 /* Finalize notification */
1111 rnh->rnh_prefixes++;
1113 rc->rc_cmd = RTM_ADD;
1115 rc->rc_nh_old = NULL;
1116 rc->rc_nh_new = rnd->rnd_nhop;
1117 rc->rc_nh_weight = rnd->rnd_weight;
1119 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1121 /* Existing route or memory allocation failure */
1129 * Switch @rt nhop/weigh to the ones specified in @rnd.
1130 * Conditionally set rt_expire if set in @info.
1131 * Returns 0 on success.
1134 change_route_nhop(struct rib_head *rnh, struct rtentry *rt,
1135 struct rt_addrinfo *info, struct route_nhop_data *rnd,
1136 struct rib_cmd_info *rc)
1138 struct nhop_object *nh_orig;
1140 RIB_WLOCK_ASSERT(rnh);
1142 nh_orig = rt->rt_nhop;
1144 if (rnd->rnd_nhop != NULL) {
1145 /* Changing expiration & nexthop & weight to a new one */
1146 rt_set_expire_info(rt, info);
1147 rt->rt_nhop = rnd->rnd_nhop;
1148 rt->rt_weight = rnd->rnd_weight;
1149 if (rt->rt_expire > 0)
1150 tmproutes_update(rnh, rt);
1152 /* Route deletion requested. */
1153 struct sockaddr *ndst, *netmask;
1154 struct radix_node *rn;
1156 ndst = (struct sockaddr *)rt_key(rt);
1157 netmask = info->rti_info[RTAX_NETMASK];
1158 rn = rnh->rnh_deladdr(ndst, netmask, &rnh->head);
1162 rt->rte_flags &= ~RTF_UP;
1165 /* Finalize notification */
1167 if (rnd->rnd_nhop == NULL)
1168 rnh->rnh_prefixes--;
1170 rc->rc_cmd = (rnd->rnd_nhop != NULL) ? RTM_CHANGE : RTM_DELETE;
1172 rc->rc_nh_old = nh_orig;
1173 rc->rc_nh_new = rnd->rnd_nhop;
1174 rc->rc_nh_weight = rnd->rnd_weight;
1176 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
1182 * Conditionally update route nhop/weight IFF data in @nhd_orig is
1183 * consistent with the current route data.
1184 * Nexthop in @nhd_new is consumed.
1187 change_route_conditional(struct rib_head *rnh, struct rtentry *rt,
1188 struct rt_addrinfo *info, struct route_nhop_data *rnd_orig,
1189 struct route_nhop_data *rnd_new, struct rib_cmd_info *rc)
1191 struct rtentry *rt_new;
1196 rt_new = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1197 info->rti_info[RTAX_NETMASK], &rnh->head);
1199 if (rt_new == NULL) {
1200 if (rnd_orig->rnd_nhop == NULL)
1201 error = add_route_nhop(rnh, rt, info, rnd_new, rc);
1204 * Prefix does not exist, which was not our assumption.
1205 * Update @rnd_orig with the new data and return
1207 rnd_orig->rnd_nhop = NULL;
1208 rnd_orig->rnd_weight = 0;
1212 /* Prefix exists, try to update */
1213 if (rnd_orig->rnd_nhop == rt_new->rt_nhop) {
1215 * Nhop/mpath group hasn't changed. Flip
1216 * to the new precalculated one and return
1218 error = change_route_nhop(rnh, rt_new, info, rnd_new, rc);
1220 /* Update and retry */
1221 rnd_orig->rnd_nhop = rt_new->rt_nhop;
1222 rnd_orig->rnd_weight = rt_new->rt_weight;
1230 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
1232 if (rnd_orig->rnd_nhop != NULL)
1233 nhop_free_any(rnd_orig->rnd_nhop);
1236 if (rnd_new->rnd_nhop != NULL)
1237 nhop_free_any(rnd_new->rnd_nhop);
1244 * Performs modification of routing table specificed by @action.
1245 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
1246 * Needs to be run in network epoch.
1248 * Returns 0 on success and fills in @rc with action result.
1251 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
1252 struct rib_cmd_info *rc)
1258 error = rib_add_route(fibnum, info, rc);
1261 error = rib_del_route(fibnum, info, rc);
1264 error = rib_change_route(fibnum, info, rc);
1275 struct rt_addrinfo info;
1276 struct rib_head *rnh;
1277 struct rtentry *head;
1278 struct rib_cmd_info rc;
1282 * Conditionally unlinks @rn from radix tree based
1283 * on info data passed in @arg.
1286 rt_checkdelroute(struct radix_node *rn, void *arg)
1288 struct rt_delinfo *di;
1289 struct rt_addrinfo *info;
1292 di = (struct rt_delinfo *)arg;
1293 rt = (struct rtentry *)rn;
1296 info->rti_info[RTAX_DST] = rt_key(rt);
1297 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1299 if (rt_unlinkrte(di->rnh, info, &di->rc) != 0)
1303 * Add deleted rtentries to the list to GC them
1304 * after dropping the lock.
1306 * XXX: Delayed notifications not implemented
1307 * for nexthop updates.
1309 if (di->rc.rc_cmd == RTM_DELETE) {
1310 /* Add to the list and return */
1311 rt->rt_chain = di->head;
1316 * RTM_CHANGE to a diferent nexthop or nexthop group.
1317 * Free old multipath group.
1319 nhop_free_any(di->rc.rc_nh_old);
1327 * Iterates over a routing table specified by @fibnum and @family and
1328 * deletes elements marked by @filter_f.
1329 * @fibnum: rtable id
1330 * @family: AF_ address family
1331 * @filter_f: function returning non-zero value for items to delete
1332 * @arg: data to pass to the @filter_f function
1333 * @report: true if rtsock notification is needed.
1336 rib_walk_del(u_int fibnum, int family, rib_filter_f_t *filter_f, void *arg, bool report)
1338 struct rib_head *rnh;
1339 struct rt_delinfo di;
1341 struct nhop_object *nh;
1342 struct epoch_tracker et;
1344 rnh = rt_tables_get_rnh(fibnum, family);
1348 bzero(&di, sizeof(di));
1349 di.info.rti_filter = filter_f;
1350 di.info.rti_filterdata = arg;
1352 di.rc.rc_cmd = RTM_DELETE;
1354 NET_EPOCH_ENTER(et);
1357 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1360 /* We might have something to reclaim. */
1361 bzero(&di.rc, sizeof(di.rc));
1362 di.rc.rc_cmd = RTM_DELETE;
1363 while (di.head != NULL) {
1365 di.head = rt->rt_chain;
1366 rt->rt_chain = NULL;
1370 di.rc.rc_nh_old = nh;
1371 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
1373 /* TODO std rt -> rt_addrinfo export */
1374 di.info.rti_info[RTAX_DST] = rt_key(rt);
1375 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1379 struct nhgrp_object *nhg;
1380 struct weightened_nhop *wn;
1382 if (NH_IS_NHGRP(nh)) {
1383 nhg = (struct nhgrp_object *)nh;
1384 wn = nhgrp_get_nhops(nhg, &num_nhops);
1385 for (int i = 0; i < num_nhops; i++)
1386 rt_routemsg(RTM_DELETE, rt, wn[i].nh, fibnum);
1389 rt_routemsg(RTM_DELETE, rt, nh, fibnum);
1398 rt_delete_unconditional(struct radix_node *rn, void *arg)
1400 struct rtentry *rt = RNTORT(rn);
1401 struct rib_head *rnh = (struct rib_head *)arg;
1403 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), &rnh->head);
1404 if (RNTORT(rn) == rt)
1411 * Removes all routes from the routing table without executing notifications.
1412 * rtentres will be removed after the end of a current epoch.
1415 rib_flush_routes(struct rib_head *rnh)
1418 rnh->rnh_walktree(&rnh->head, rt_delete_unconditional, rnh);
1423 rib_flush_routes_family(int family)
1425 struct rib_head *rnh;
1427 for (uint32_t fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1428 if ((rnh = rt_tables_get_rnh(fibnum, family)) != NULL)
1429 rib_flush_routes(rnh);
1434 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
1435 struct rib_cmd_info *rc)
1437 struct rib_subscription *rs;
1439 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
1440 if (rs->type == type)
1441 rs->func(rnh, rc, rs->arg);
1445 static struct rib_subscription *
1446 allocate_subscription(rib_subscription_cb_t *f, void *arg,
1447 enum rib_subscription_type type, bool waitok)
1449 struct rib_subscription *rs;
1450 int flags = M_ZERO | (waitok ? M_WAITOK : M_NOWAIT);
1452 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
1464 * Subscribe for the changes in the routing table specified by @fibnum and
1467 * Returns pointer to the subscription structure on success.
1469 struct rib_subscription *
1470 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
1471 enum rib_subscription_type type, bool waitok)
1473 struct rib_head *rnh;
1474 struct epoch_tracker et;
1476 NET_EPOCH_ENTER(et);
1477 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
1478 rnh = rt_tables_get_rnh(fibnum, family);
1481 return (rib_subscribe_internal(rnh, f, arg, type, waitok));
1484 struct rib_subscription *
1485 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1486 enum rib_subscription_type type, bool waitok)
1488 struct rib_subscription *rs;
1489 struct epoch_tracker et;
1491 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
1495 NET_EPOCH_ENTER(et);
1497 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1504 struct rib_subscription *
1505 rib_subscribe_locked(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
1506 enum rib_subscription_type type)
1508 struct rib_subscription *rs;
1511 RIB_WLOCK_ASSERT(rnh);
1513 if ((rs = allocate_subscription(f, arg, type, false)) == NULL)
1517 CK_STAILQ_INSERT_HEAD(&rnh->rnh_subscribers, rs, next);
1523 * Remove rtable subscription @rs from the routing table.
1524 * Needs to be run in network epoch.
1527 rib_unsubscribe(struct rib_subscription *rs)
1529 struct rib_head *rnh = rs->rnh;
1534 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1537 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1542 rib_unsubscribe_locked(struct rib_subscription *rs)
1544 struct rib_head *rnh = rs->rnh;
1547 RIB_WLOCK_ASSERT(rnh);
1549 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
1551 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
1556 * Epoch callback indicating subscription is safe to destroy
1559 destroy_subscription_epoch(epoch_context_t ctx)
1561 struct rib_subscription *rs;
1563 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
1569 rib_init_subscriptions(struct rib_head *rnh)
1572 CK_STAILQ_INIT(&rnh->rnh_subscribers);
1576 rib_destroy_subscriptions(struct rib_head *rnh)
1578 struct rib_subscription *rs;
1579 struct epoch_tracker et;
1581 NET_EPOCH_ENTER(et);
1583 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
1584 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
1585 epoch_call(net_epoch_preempt, destroy_subscription_epoch,