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 static struct rib_head *
91 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
96 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
98 dst = info->rti_info[RTAX_DST];
99 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
105 * Adds route defined by @info into the kernel table specified by @fibnum and
106 * sa_family in @info->rti_info[RTAX_DST].
108 * Returns 0 on success and fills in operation metadata into @rc.
111 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
112 struct rib_cmd_info *rc)
114 struct rib_head *rnh;
118 rnh = get_rnh(fibnum, info);
120 return (EAFNOSUPPORT);
123 * Check consistency between RTF_HOST flag and netmask
126 if (info->rti_flags & RTF_HOST)
127 info->rti_info[RTAX_NETMASK] = NULL;
128 else if (info->rti_info[RTAX_NETMASK] == NULL)
131 bzero(rc, sizeof(struct rib_cmd_info));
132 rc->rc_cmd = RTM_ADD;
134 return (add_route(rnh, info, rc));
138 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
139 struct rib_cmd_info *rc)
141 struct sockaddr *dst, *ndst, *gateway, *netmask;
142 struct rtentry *rt, *rt_old;
143 struct nhop_object *nh;
144 struct radix_node *rn;
147 struct epoch_tracker et;
149 dst = info->rti_info[RTAX_DST];
150 gateway = info->rti_info[RTAX_GATEWAY];
151 netmask = info->rti_info[RTAX_NETMASK];
152 flags = info->rti_flags;
154 if ((flags & RTF_GATEWAY) && !gateway)
156 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
157 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
160 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
163 if (info->rti_ifa == NULL) {
164 error = rt_getifa_fib(info, rnh->rib_fibnum);
168 ifa_ref(info->rti_ifa);
172 error = nhop_create_from_info(rnh, info, &nh);
175 ifa_free(info->rti_ifa);
179 rt = uma_zalloc(V_rtzone, M_NOWAIT);
181 ifa_free(info->rti_ifa);
185 rt->rt_flags = RTF_UP | flags;
189 memcpy(&rt->rt_dst, dst, dst->sa_len);
190 rt_key(rt) = &rt->rt_dst;
193 * point to the (possibly newly malloc'd) dest address.
195 ndst = (struct sockaddr *)rt_key(rt);
198 * make sure it contains the value we want (masked if needed).
201 rt_maskedcopy(dst, ndst, netmask);
203 bcopy(dst, ndst, dst->sa_len);
206 * We use the ifa reference returned by rt_getifa_fib().
207 * This moved from below so that rnh->rnh_addaddr() can
208 * examine the ifa and ifa->ifa_ifp if it so desires.
213 rt_setmetrics(info, rt);
219 /* do not permit exactly the same dst/mask/gw pair */
220 if (rt_mpath_capable(rnh) &&
221 rt_mpath_conflict(rnh, rt, netmask)) {
225 uma_zfree(V_rtzone, rt);
230 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
233 /* Most common usecase */
234 if (rt->rt_expire > 0)
235 tmproutes_update(rnh, rt);
237 /* Finalize notification */
240 rc->rc_rt = RNTORT(rn);
243 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
244 } else if ((info->rti_flags & RTF_PINNED) != 0) {
247 * Force removal and re-try addition
248 * TODO: better multipath&pinned support
250 struct sockaddr *info_dst = info->rti_info[RTAX_DST];
251 info->rti_info[RTAX_DST] = ndst;
252 /* Do not delete existing PINNED(interface) routes */
253 info->rti_flags &= ~RTF_PINNED;
254 rt_old = rt_unlinkrte(rnh, info, &error);
255 info->rti_flags |= RTF_PINNED;
256 info->rti_info[RTAX_DST] = info_dst;
257 if (rt_old != NULL) {
258 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head,
261 /* Finalize notification */
265 rc->rc_cmd = RTM_CHANGE;
266 rc->rc_rt = RNTORT(rn);
267 rc->rc_nh_old = rt_old->rt_nhop;
270 rc->rc_cmd = RTM_DELETE;
271 rc->rc_rt = RNTORT(rn);
272 rc->rc_nh_old = rt_old->rt_nhop;
275 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
280 if ((rn != NULL) || (rt_old != NULL))
281 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
287 * If it still failed to go into the tree,
288 * then un-make it (this should be a function)
292 uma_zfree(V_rtzone, rt);
303 * Removes route defined by @info from the kernel table specified by @fibnum and
304 * sa_family in @info->rti_info[RTAX_DST].
306 * Returns 0 on success and fills in operation metadata into @rc.
309 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
311 struct rib_head *rnh;
315 rnh = get_rnh(fibnum, info);
317 return (EAFNOSUPPORT);
319 bzero(rc, sizeof(struct rib_cmd_info));
320 rc->rc_cmd = RTM_DELETE;
322 return (del_route(rnh, info, rc));
326 * Conditionally unlinks rtentry matching data inside @info from @rnh.
327 * Returns unlinked, locked and referenced @rtentry on success,
328 * Returns NULL and sets @perror to:
329 * ESRCH - if prefix was not found,
330 * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
331 * ENOENT - if supplied filter function returned 0 (not matched).
334 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror)
336 struct sockaddr *dst, *netmask;
338 struct radix_node *rn;
340 dst = info->rti_info[RTAX_DST];
341 netmask = info->rti_info[RTAX_NETMASK];
343 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head);
349 if ((info->rti_flags & RTF_PINNED) == 0) {
350 /* Check if target route can be deleted */
351 if (rt->rt_flags & RTF_PINNED) {
352 *perror = EADDRINUSE;
357 if (info->rti_filter != NULL) {
358 if (info->rti_filter(rt, rt->rt_nhop, info->rti_filterdata)==0){
365 * Filter function requested rte deletion.
366 * Ease the caller work by filling in remaining info
367 * from that particular entry.
369 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
373 * Remove the item from the tree and return it.
374 * Complain if it is not there and do no more processing.
378 if (rt_mpath_capable(rnh))
379 rn = rt_mpath_unlink(rnh, info, rt, perror);
382 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
386 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
387 panic ("rtrequest delete");
391 rt->rt_flags &= ~RTF_UP;
399 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
400 struct rib_cmd_info *rc)
402 struct sockaddr *dst, *netmask;
403 struct sockaddr_storage mdst;
407 dst = info->rti_info[RTAX_DST];
408 netmask = info->rti_info[RTAX_NETMASK];
411 if (dst->sa_len > sizeof(mdst))
413 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
414 dst = (struct sockaddr *)&mdst;
418 rt = rt_unlinkrte(rnh, info, &error);
420 /* Finalize notification */
423 rc->rc_nh_old = rt->rt_nhop;
424 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
430 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
433 * If the caller wants it, then it can have it,
434 * the entry will be deleted after the end of the current epoch.
442 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
443 struct rib_cmd_info *rc)
445 struct rib_head *rnh;
449 rnh = get_rnh(fibnum, info);
451 return (EAFNOSUPPORT);
453 bzero(rc, sizeof(struct rib_cmd_info));
454 rc->rc_cmd = RTM_CHANGE;
456 return (change_route(rnh, info, rc));
460 change_route_one(struct rib_head *rnh, struct rt_addrinfo *info,
461 struct rib_cmd_info *rc)
464 struct rtentry *rt = NULL;
467 struct nhop_object *nh, *nh_orig;
470 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
471 info->rti_info[RTAX_NETMASK], &rnh->head);
480 * If we got multipath routes,
481 * we require users to specify a matching RTAX_GATEWAY.
483 if (rt_mpath_capable(rnh)) {
484 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
491 nh_orig = rt->rt_nhop;
499 * New gateway could require new ifaddr, ifp;
500 * flags may also be different; ifp may be specified
501 * by ll sockaddr when protocol address is ambiguous
503 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
504 info->rti_info[RTAX_GATEWAY] != NULL) ||
505 info->rti_info[RTAX_IFP] != NULL ||
506 (info->rti_info[RTAX_IFA] != NULL &&
507 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
508 error = rt_getifa_fib(info, rnh->rib_fibnum);
509 if (info->rti_ifa != NULL)
514 ifa_free(info->rti_ifa);
515 info->rti_ifa = NULL;
522 error = nhop_create_from_nhop(rnh, nh_orig, info, &nh);
524 ifa_free(info->rti_ifa);
525 info->rti_ifa = NULL;
532 /* Lookup rtentry once again and check if nexthop is still the same */
533 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
534 info->rti_info[RTAX_NETMASK], &rnh->head);
542 if (rt->rt_nhop != nh_orig) {
548 /* Proceed with the update */
551 /* Provide notification to the protocols.*/
553 rt_setmetrics(info, rt);
555 /* Finalize notification */
557 rc->rc_nh_old = nh_orig;
558 rc->rc_nh_new = rt->rt_nhop;
562 /* Update generation id to reflect rtable change */
564 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
568 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
576 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
577 struct rib_cmd_info *rc)
581 /* Check if updated gateway exists */
582 if ((info->rti_flags & RTF_GATEWAY) &&
583 (info->rti_info[RTAX_GATEWAY] == NULL))
587 * route change is done in multiple steps, with dropping and
588 * reacquiring lock. In the situations with multiple processes
589 * changes the same route in can lead to the case when route
590 * is changed between the steps. Address it by retrying the operation
591 * multiple times before failing.
593 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
594 error = change_route_one(rnh, info, rc);
603 * Performs modification of routing table specificed by @action.
604 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
605 * Needs to be run in network epoch.
607 * Returns 0 on success and fills in @rc with action result.
610 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
611 struct rib_cmd_info *rc)
617 error = rib_add_route(fibnum, info, rc);
620 error = rib_del_route(fibnum, info, rc);
623 error = rib_change_route(fibnum, info, rc);
635 struct rt_addrinfo info;
636 struct rib_head *rnh;
637 struct rtentry *head;
638 struct rib_cmd_info rc;
642 * Conditionally unlinks @rn from radix tree based
643 * on info data passed in @arg.
646 rt_checkdelroute(struct radix_node *rn, void *arg)
648 struct rt_delinfo *di;
649 struct rt_addrinfo *info;
653 di = (struct rt_delinfo *)arg;
654 rt = (struct rtentry *)rn;
658 info->rti_info[RTAX_DST] = rt_key(rt);
659 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
660 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
662 rt = rt_unlinkrte(di->rnh, info, &error);
664 /* Either not allowed or not matched. Skip entry */
668 /* Entry was unlinked. Notify subscribers */
671 di->rc.rc_nh_old = rt->rt_nhop;
672 rib_notify(di->rnh, RIB_NOTIFY_IMMEDIATE, &di->rc);
674 /* Add to the list and return */
675 rt->rt_chain = di->head;
682 * Iterates over a routing table specified by @fibnum and @family and
683 * deletes elements marked by @filter_f.
685 * @family: AF_ address family
686 * @filter_f: function returning non-zero value for items to delete
687 * @arg: data to pass to the @filter_f function
688 * @report: true if rtsock notification is needed.
691 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report)
693 struct rib_head *rnh;
694 struct rt_delinfo di;
696 struct epoch_tracker et;
698 rnh = rt_tables_get_rnh(fibnum, family);
702 bzero(&di, sizeof(di));
703 di.info.rti_filter = filter_f;
704 di.info.rti_filterdata = arg;
706 di.rc.rc_cmd = RTM_DELETE;
711 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
714 /* We might have something to reclaim. */
715 while (di.head != NULL) {
717 di.head = rt->rt_chain;
721 di.rc.rc_nh_old = rt->rt_nhop;
722 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
724 /* TODO std rt -> rt_addrinfo export */
725 di.info.rti_info[RTAX_DST] = rt_key(rt);
726 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
729 rt_routemsg(RTM_DELETE, rt, rt->rt_nhop->nh_ifp, 0,
738 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
739 struct rib_cmd_info *rc)
741 struct rib_subscription *rs;
743 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
744 if (rs->type == type)
745 rs->func(rnh, rc, rs->arg);
750 * Subscribe for the changes in the routing table specified by @fibnum and
753 * Returns pointer to the subscription structure on success.
755 struct rib_subscription *
756 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
757 enum rib_subscription_type type, bool waitok)
759 struct rib_head *rnh;
760 struct rib_subscription *rs;
761 struct epoch_tracker et;
762 int flags = M_ZERO | (waitok ? M_WAITOK : 0);
764 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
769 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
770 rnh = rt_tables_get_rnh(fibnum, family);
777 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
785 * Remove rtable subscription @rs from the table specified by @fibnum
787 * Needs to be run in network epoch.
789 * Returns 0 on success.
792 rib_unsibscribe(uint32_t fibnum, int family, struct rib_subscription *rs)
794 struct rib_head *rnh;
797 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
798 rnh = rt_tables_get_rnh(fibnum, family);
804 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
807 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
814 * Epoch callback indicating subscription is safe to destroy
817 destroy_subscription_epoch(epoch_context_t ctx)
819 struct rib_subscription *rs;
821 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
827 rib_init_subscriptions(struct rib_head *rnh)
830 CK_STAILQ_INIT(&rnh->rnh_subscribers);
834 rib_destroy_subscriptions(struct rib_head *rnh)
836 struct rib_subscription *rs;
837 struct epoch_tracker et;
841 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
842 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
843 epoch_call(net_epoch_preempt, destroy_subscription_epoch,