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 <net/route/shared.h>
56 #include <netinet/in.h>
59 #include <net/radix_mpath.h>
66 * This file contains control plane routing tables functions.
68 * All functions assumes they are called in net epoch.
71 struct rib_subscription {
72 CK_STAILQ_ENTRY(rib_subscription) next;
73 rib_subscription_cb_t *func;
75 enum rib_subscription_type type;
76 struct epoch_context epoch_ctx;
79 static int add_route(struct rib_head *rnh, struct rt_addrinfo *info,
80 struct rib_cmd_info *rc);
81 static int del_route(struct rib_head *rnh, struct rt_addrinfo *info,
82 struct rib_cmd_info *rc);
83 static int change_route(struct rib_head *, struct rt_addrinfo *,
84 struct rib_cmd_info *rc);
85 static void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
86 struct rib_cmd_info *rc);
88 static void destroy_subscription_epoch(epoch_context_t ctx);
90 /* Routing table UMA zone */
91 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
92 #define V_rtzone VNET(rtzone)
98 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
99 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
104 vnet_rtzone_destroy()
107 uma_zdestroy(V_rtzone);
112 destroy_rtentry(struct rtentry *rt)
116 * At this moment rnh, nh_control may be already freed.
117 * nhop interface may have been migrated to a different vnet.
118 * Use vnet stored in the nexthop to delete the entry.
120 CURVNET_SET(nhop_get_vnet(rt->rt_nhop));
122 /* Unreference nexthop */
123 nhop_free(rt->rt_nhop);
125 uma_zfree(V_rtzone, rt);
131 * Epoch callback indicating rtentry is safe to destroy
134 destroy_rtentry_epoch(epoch_context_t ctx)
138 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
144 * Schedule rtentry deletion
147 rtfree(struct rtentry *rt)
150 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
152 epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
158 static struct rib_head *
159 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
161 struct rib_head *rnh;
162 struct sockaddr *dst;
164 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
166 dst = info->rti_info[RTAX_DST];
167 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
173 * Adds route defined by @info into the kernel table specified by @fibnum and
174 * sa_family in @info->rti_info[RTAX_DST].
176 * Returns 0 on success and fills in operation metadata into @rc.
179 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
180 struct rib_cmd_info *rc)
182 struct rib_head *rnh;
186 rnh = get_rnh(fibnum, info);
188 return (EAFNOSUPPORT);
191 * Check consistency between RTF_HOST flag and netmask
194 if (info->rti_flags & RTF_HOST)
195 info->rti_info[RTAX_NETMASK] = NULL;
196 else if (info->rti_info[RTAX_NETMASK] == NULL)
199 bzero(rc, sizeof(struct rib_cmd_info));
200 rc->rc_cmd = RTM_ADD;
202 return (add_route(rnh, info, rc));
206 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
207 struct rib_cmd_info *rc)
209 struct sockaddr *dst, *ndst, *gateway, *netmask;
210 struct rtentry *rt, *rt_old;
211 struct nhop_object *nh;
212 struct radix_node *rn;
216 dst = info->rti_info[RTAX_DST];
217 gateway = info->rti_info[RTAX_GATEWAY];
218 netmask = info->rti_info[RTAX_NETMASK];
219 flags = info->rti_flags;
221 if ((flags & RTF_GATEWAY) && !gateway)
223 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
224 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
227 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
230 if (info->rti_ifa == NULL) {
231 error = rt_getifa_fib(info, rnh->rib_fibnum);
235 ifa_ref(info->rti_ifa);
238 error = nhop_create_from_info(rnh, info, &nh);
240 ifa_free(info->rti_ifa);
244 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
246 ifa_free(info->rti_ifa);
250 rt->rte_flags = RTF_UP | flags;
254 memcpy(&rt->rt_dst, dst, dst->sa_len);
255 rt_key(rt) = &rt->rt_dst;
258 * point to the (possibly newly malloc'd) dest address.
260 ndst = (struct sockaddr *)rt_key(rt);
263 * make sure it contains the value we want (masked if needed).
266 rt_maskedcopy(dst, ndst, netmask);
268 bcopy(dst, ndst, dst->sa_len);
271 * We use the ifa reference returned by rt_getifa_fib().
272 * This moved from below so that rnh->rnh_addaddr() can
273 * examine the ifa and ifa->ifa_ifp if it so desires.
278 rt_setmetrics(info, rt);
283 /* do not permit exactly the same dst/mask/gw pair */
284 if (rt_mpath_capable(rnh) &&
285 rt_mpath_conflict(rnh, rt, netmask)) {
289 uma_zfree(V_rtzone, rt);
294 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
297 /* Most common usecase */
298 if (rt->rt_expire > 0)
299 tmproutes_update(rnh, rt);
301 /* Finalize notification */
306 rc->rc_nh_weight = rt->rt_weight;
308 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
309 } else if ((info->rti_flags & RTF_PINNED) != 0) {
312 * Force removal and re-try addition
313 * TODO: better multipath&pinned support
315 struct sockaddr *info_dst = info->rti_info[RTAX_DST];
316 info->rti_info[RTAX_DST] = ndst;
317 /* Do not delete existing PINNED(interface) routes */
318 info->rti_flags &= ~RTF_PINNED;
319 rt_old = rt_unlinkrte(rnh, info, &error);
320 info->rti_flags |= RTF_PINNED;
321 info->rti_info[RTAX_DST] = info_dst;
322 if (rt_old != NULL) {
323 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head,
326 /* Finalize notification */
330 rc->rc_cmd = RTM_CHANGE;
332 rc->rc_nh_old = rt_old->rt_nhop;
334 rc->rc_nh_weight = rt->rt_weight;
336 rc->rc_cmd = RTM_DELETE;
338 rc->rc_nh_old = rt_old->rt_nhop;
339 rc->rc_nh_weight = rt_old->rt_weight;
341 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
346 if ((rn != NULL) || (rt_old != NULL))
347 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
353 * If it still failed to go into the tree,
354 * then un-make it (this should be a function)
358 uma_zfree(V_rtzone, rt);
367 * Removes route defined by @info from the kernel table specified by @fibnum and
368 * sa_family in @info->rti_info[RTAX_DST].
370 * Returns 0 on success and fills in operation metadata into @rc.
373 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
375 struct rib_head *rnh;
379 rnh = get_rnh(fibnum, info);
381 return (EAFNOSUPPORT);
383 bzero(rc, sizeof(struct rib_cmd_info));
384 rc->rc_cmd = RTM_DELETE;
386 return (del_route(rnh, info, rc));
390 * Conditionally unlinks rtentry matching data inside @info from @rnh.
391 * Returns unlinked, locked and referenced @rtentry on success,
392 * Returns NULL and sets @perror to:
393 * ESRCH - if prefix was not found,
394 * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
395 * ENOENT - if supplied filter function returned 0 (not matched).
398 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror)
400 struct sockaddr *dst, *netmask;
402 struct nhop_object *nh;
403 struct radix_node *rn;
405 dst = info->rti_info[RTAX_DST];
406 netmask = info->rti_info[RTAX_NETMASK];
408 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head);
416 if ((info->rti_flags & RTF_PINNED) == 0) {
417 /* Check if target route can be deleted */
418 if (NH_IS_PINNED(nh)) {
419 *perror = EADDRINUSE;
424 if (info->rti_filter != NULL) {
425 if (info->rti_filter(rt, nh, info->rti_filterdata)==0){
432 * Filter function requested rte deletion.
433 * Ease the caller work by filling in remaining info
434 * from that particular entry.
436 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
440 * Remove the item from the tree and return it.
441 * Complain if it is not there and do no more processing.
445 if (rt_mpath_capable(rnh))
446 rn = rt_mpath_unlink(rnh, info, rt, perror);
449 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
453 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
454 panic ("rtrequest delete");
457 rt->rte_flags &= ~RTF_UP;
465 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
466 struct rib_cmd_info *rc)
468 struct sockaddr *dst, *netmask;
469 struct sockaddr_storage mdst;
473 dst = info->rti_info[RTAX_DST];
474 netmask = info->rti_info[RTAX_NETMASK];
477 if (dst->sa_len > sizeof(mdst))
479 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
480 dst = (struct sockaddr *)&mdst;
484 rt = rt_unlinkrte(rnh, info, &error);
486 /* Finalize notification */
489 rc->rc_nh_old = rt->rt_nhop;
490 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
496 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
499 * If the caller wants it, then it can have it,
500 * the entry will be deleted after the end of the current epoch.
508 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
509 struct rib_cmd_info *rc)
511 struct rib_head *rnh;
515 rnh = get_rnh(fibnum, info);
517 return (EAFNOSUPPORT);
519 bzero(rc, sizeof(struct rib_cmd_info));
520 rc->rc_cmd = RTM_CHANGE;
522 return (change_route(rnh, info, rc));
526 change_route_one(struct rib_head *rnh, struct rt_addrinfo *info,
527 struct rib_cmd_info *rc)
530 struct rtentry *rt = NULL;
533 struct nhop_object *nh, *nh_orig;
536 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
537 info->rti_info[RTAX_NETMASK], &rnh->head);
546 * If we got multipath routes,
547 * we require users to specify a matching RTAX_GATEWAY.
549 if (rt_mpath_capable(rnh)) {
550 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
557 nh_orig = rt->rt_nhop;
565 * New gateway could require new ifaddr, ifp;
566 * flags may also be different; ifp may be specified
567 * by ll sockaddr when protocol address is ambiguous
569 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
570 info->rti_info[RTAX_GATEWAY] != NULL) ||
571 info->rti_info[RTAX_IFP] != NULL ||
572 (info->rti_info[RTAX_IFA] != NULL &&
573 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
574 error = rt_getifa_fib(info, rnh->rib_fibnum);
575 if (info->rti_ifa != NULL)
580 ifa_free(info->rti_ifa);
581 info->rti_ifa = NULL;
588 error = nhop_create_from_nhop(rnh, nh_orig, info, &nh);
590 ifa_free(info->rti_ifa);
591 info->rti_ifa = NULL;
598 /* Lookup rtentry once again and check if nexthop is still the same */
599 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
600 info->rti_info[RTAX_NETMASK], &rnh->head);
608 if (rt->rt_nhop != nh_orig) {
614 /* Proceed with the update */
616 /* Provide notification to the protocols.*/
618 rt_setmetrics(info, rt);
620 /* Finalize notification */
624 rc->rc_nh_old = nh_orig;
625 rc->rc_nh_new = rt->rt_nhop;
626 rc->rc_nh_weight = rt->rt_weight;
628 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
632 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
640 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
641 struct rib_cmd_info *rc)
645 /* Check if updated gateway exists */
646 if ((info->rti_flags & RTF_GATEWAY) &&
647 (info->rti_info[RTAX_GATEWAY] == NULL))
651 * route change is done in multiple steps, with dropping and
652 * reacquiring lock. In the situations with multiple processes
653 * changes the same route in can lead to the case when route
654 * is changed between the steps. Address it by retrying the operation
655 * multiple times before failing.
657 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
658 error = change_route_one(rnh, info, rc);
667 * Performs modification of routing table specificed by @action.
668 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
669 * Needs to be run in network epoch.
671 * Returns 0 on success and fills in @rc with action result.
674 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
675 struct rib_cmd_info *rc)
681 error = rib_add_route(fibnum, info, rc);
684 error = rib_del_route(fibnum, info, rc);
687 error = rib_change_route(fibnum, info, rc);
699 struct rt_addrinfo info;
700 struct rib_head *rnh;
701 struct rtentry *head;
702 struct rib_cmd_info rc;
706 * Conditionally unlinks @rn from radix tree based
707 * on info data passed in @arg.
710 rt_checkdelroute(struct radix_node *rn, void *arg)
712 struct rt_delinfo *di;
713 struct rt_addrinfo *info;
717 di = (struct rt_delinfo *)arg;
718 rt = (struct rtentry *)rn;
722 info->rti_info[RTAX_DST] = rt_key(rt);
723 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
724 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
726 rt = rt_unlinkrte(di->rnh, info, &error);
728 /* Either not allowed or not matched. Skip entry */
732 /* Entry was unlinked. Notify subscribers */
735 di->rc.rc_nh_old = rt->rt_nhop;
736 rib_notify(di->rnh, RIB_NOTIFY_IMMEDIATE, &di->rc);
738 /* Add to the list and return */
739 rt->rt_chain = di->head;
746 * Iterates over a routing table specified by @fibnum and @family and
747 * deletes elements marked by @filter_f.
749 * @family: AF_ address family
750 * @filter_f: function returning non-zero value for items to delete
751 * @arg: data to pass to the @filter_f function
752 * @report: true if rtsock notification is needed.
755 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report)
757 struct rib_head *rnh;
758 struct rt_delinfo di;
760 struct epoch_tracker et;
762 rnh = rt_tables_get_rnh(fibnum, family);
766 bzero(&di, sizeof(di));
767 di.info.rti_filter = filter_f;
768 di.info.rti_filterdata = arg;
770 di.rc.rc_cmd = RTM_DELETE;
775 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
778 /* We might have something to reclaim. */
779 while (di.head != NULL) {
781 di.head = rt->rt_chain;
785 di.rc.rc_nh_old = rt->rt_nhop;
786 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
788 /* TODO std rt -> rt_addrinfo export */
789 di.info.rti_info[RTAX_DST] = rt_key(rt);
790 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
793 rt_routemsg(RTM_DELETE, rt, rt->rt_nhop->nh_ifp, 0,
802 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
803 struct rib_cmd_info *rc)
805 struct rib_subscription *rs;
807 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
808 if (rs->type == type)
809 rs->func(rnh, rc, rs->arg);
813 static struct rib_subscription *
814 allocate_subscription(rib_subscription_cb_t *f, void *arg,
815 enum rib_subscription_type type, bool waitok)
817 struct rib_subscription *rs;
818 int flags = M_ZERO | (waitok ? M_WAITOK : 0);
820 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
833 * Subscribe for the changes in the routing table specified by @fibnum and
836 * Returns pointer to the subscription structure on success.
838 struct rib_subscription *
839 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
840 enum rib_subscription_type type, bool waitok)
842 struct rib_head *rnh;
843 struct rib_subscription *rs;
844 struct epoch_tracker et;
846 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
850 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
851 rnh = rt_tables_get_rnh(fibnum, family);
854 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
861 struct rib_subscription *
862 rib_subscribe_internal(struct rib_head *rnh, rib_subscription_cb_t *f, void *arg,
863 enum rib_subscription_type type, bool waitok)
865 struct rib_subscription *rs;
866 struct epoch_tracker et;
868 if ((rs = allocate_subscription(f, arg, type, waitok)) == NULL)
873 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
881 * Remove rtable subscription @rs from the table specified by @fibnum
883 * Needs to be run in network epoch.
885 * Returns 0 on success.
888 rib_unsibscribe(uint32_t fibnum, int family, struct rib_subscription *rs)
890 struct rib_head *rnh;
893 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
894 rnh = rt_tables_get_rnh(fibnum, family);
900 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
903 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
910 * Epoch callback indicating subscription is safe to destroy
913 destroy_subscription_epoch(epoch_context_t ctx)
915 struct rib_subscription *rs;
917 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
923 rib_init_subscriptions(struct rib_head *rnh)
926 CK_STAILQ_INIT(&rnh->rnh_subscribers);
930 rib_destroy_subscriptions(struct rib_head *rnh)
932 struct rib_subscription *rs;
933 struct epoch_tracker et;
937 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
938 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
939 epoch_call(net_epoch_preempt, destroy_subscription_epoch,