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;
148 dst = info->rti_info[RTAX_DST];
149 gateway = info->rti_info[RTAX_GATEWAY];
150 netmask = info->rti_info[RTAX_NETMASK];
151 flags = info->rti_flags;
153 if ((flags & RTF_GATEWAY) && !gateway)
155 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
156 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
159 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
162 if (info->rti_ifa == NULL) {
163 error = rt_getifa_fib(info, rnh->rib_fibnum);
167 ifa_ref(info->rti_ifa);
170 error = nhop_create_from_info(rnh, info, &nh);
172 ifa_free(info->rti_ifa);
176 rt = uma_zalloc(V_rtzone, M_NOWAIT);
178 ifa_free(info->rti_ifa);
182 rt->rt_flags = RTF_UP | flags;
186 memcpy(&rt->rt_dst, dst, dst->sa_len);
187 rt_key(rt) = &rt->rt_dst;
190 * point to the (possibly newly malloc'd) dest address.
192 ndst = (struct sockaddr *)rt_key(rt);
195 * make sure it contains the value we want (masked if needed).
198 rt_maskedcopy(dst, ndst, netmask);
200 bcopy(dst, ndst, dst->sa_len);
203 * We use the ifa reference returned by rt_getifa_fib().
204 * This moved from below so that rnh->rnh_addaddr() can
205 * examine the ifa and ifa->ifa_ifp if it so desires.
210 rt_setmetrics(info, rt);
216 /* do not permit exactly the same dst/mask/gw pair */
217 if (rt_mpath_capable(rnh) &&
218 rt_mpath_conflict(rnh, rt, netmask)) {
222 uma_zfree(V_rtzone, rt);
227 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
230 /* Most common usecase */
231 if (rt->rt_expire > 0)
232 tmproutes_update(rnh, rt);
234 /* Finalize notification */
237 rc->rc_rt = RNTORT(rn);
240 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
241 } else if ((info->rti_flags & RTF_PINNED) != 0) {
244 * Force removal and re-try addition
245 * TODO: better multipath&pinned support
247 struct sockaddr *info_dst = info->rti_info[RTAX_DST];
248 info->rti_info[RTAX_DST] = ndst;
249 /* Do not delete existing PINNED(interface) routes */
250 info->rti_flags &= ~RTF_PINNED;
251 rt_old = rt_unlinkrte(rnh, info, &error);
252 info->rti_flags |= RTF_PINNED;
253 info->rti_info[RTAX_DST] = info_dst;
254 if (rt_old != NULL) {
255 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head,
258 /* Finalize notification */
262 rc->rc_cmd = RTM_CHANGE;
263 rc->rc_rt = RNTORT(rn);
264 rc->rc_nh_old = rt_old->rt_nhop;
267 rc->rc_cmd = RTM_DELETE;
268 rc->rc_rt = RNTORT(rn);
269 rc->rc_nh_old = rt_old->rt_nhop;
272 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
277 if ((rn != NULL) || (rt_old != NULL))
278 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
284 * If it still failed to go into the tree,
285 * then un-make it (this should be a function)
289 uma_zfree(V_rtzone, rt);
300 * Removes route defined by @info from the kernel table specified by @fibnum and
301 * sa_family in @info->rti_info[RTAX_DST].
303 * Returns 0 on success and fills in operation metadata into @rc.
306 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
308 struct rib_head *rnh;
312 rnh = get_rnh(fibnum, info);
314 return (EAFNOSUPPORT);
316 bzero(rc, sizeof(struct rib_cmd_info));
317 rc->rc_cmd = RTM_DELETE;
319 return (del_route(rnh, info, rc));
323 * Conditionally unlinks rtentry matching data inside @info from @rnh.
324 * Returns unlinked, locked and referenced @rtentry on success,
325 * Returns NULL and sets @perror to:
326 * ESRCH - if prefix was not found,
327 * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
328 * ENOENT - if supplied filter function returned 0 (not matched).
331 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror)
333 struct sockaddr *dst, *netmask;
335 struct radix_node *rn;
337 dst = info->rti_info[RTAX_DST];
338 netmask = info->rti_info[RTAX_NETMASK];
340 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head);
346 if ((info->rti_flags & RTF_PINNED) == 0) {
347 /* Check if target route can be deleted */
348 if (rt->rt_flags & RTF_PINNED) {
349 *perror = EADDRINUSE;
354 if (info->rti_filter != NULL) {
355 if (info->rti_filter(rt, rt->rt_nhop, info->rti_filterdata)==0){
362 * Filter function requested rte deletion.
363 * Ease the caller work by filling in remaining info
364 * from that particular entry.
366 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
370 * Remove the item from the tree and return it.
371 * Complain if it is not there and do no more processing.
375 if (rt_mpath_capable(rnh))
376 rn = rt_mpath_unlink(rnh, info, rt, perror);
379 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
383 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
384 panic ("rtrequest delete");
388 rt->rt_flags &= ~RTF_UP;
396 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
397 struct rib_cmd_info *rc)
399 struct sockaddr *dst, *netmask;
400 struct sockaddr_storage mdst;
404 dst = info->rti_info[RTAX_DST];
405 netmask = info->rti_info[RTAX_NETMASK];
408 if (dst->sa_len > sizeof(mdst))
410 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
411 dst = (struct sockaddr *)&mdst;
415 rt = rt_unlinkrte(rnh, info, &error);
417 /* Finalize notification */
420 rc->rc_nh_old = rt->rt_nhop;
421 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
427 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
430 * If the caller wants it, then it can have it,
431 * the entry will be deleted after the end of the current epoch.
439 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
440 struct rib_cmd_info *rc)
442 struct rib_head *rnh;
446 rnh = get_rnh(fibnum, info);
448 return (EAFNOSUPPORT);
450 bzero(rc, sizeof(struct rib_cmd_info));
451 rc->rc_cmd = RTM_CHANGE;
453 return (change_route(rnh, info, rc));
457 change_route_one(struct rib_head *rnh, struct rt_addrinfo *info,
458 struct rib_cmd_info *rc)
461 struct rtentry *rt = NULL;
464 struct nhop_object *nh, *nh_orig;
467 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
468 info->rti_info[RTAX_NETMASK], &rnh->head);
477 * If we got multipath routes,
478 * we require users to specify a matching RTAX_GATEWAY.
480 if (rt_mpath_capable(rnh)) {
481 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
488 nh_orig = rt->rt_nhop;
496 * New gateway could require new ifaddr, ifp;
497 * flags may also be different; ifp may be specified
498 * by ll sockaddr when protocol address is ambiguous
500 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
501 info->rti_info[RTAX_GATEWAY] != NULL) ||
502 info->rti_info[RTAX_IFP] != NULL ||
503 (info->rti_info[RTAX_IFA] != NULL &&
504 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
505 error = rt_getifa_fib(info, rnh->rib_fibnum);
506 if (info->rti_ifa != NULL)
511 ifa_free(info->rti_ifa);
512 info->rti_ifa = NULL;
519 error = nhop_create_from_nhop(rnh, nh_orig, info, &nh);
521 ifa_free(info->rti_ifa);
522 info->rti_ifa = NULL;
529 /* Lookup rtentry once again and check if nexthop is still the same */
530 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
531 info->rti_info[RTAX_NETMASK], &rnh->head);
539 if (rt->rt_nhop != nh_orig) {
545 /* Proceed with the update */
548 /* Provide notification to the protocols.*/
550 rt_setmetrics(info, rt);
552 /* Finalize notification */
554 rc->rc_nh_old = nh_orig;
555 rc->rc_nh_new = rt->rt_nhop;
559 /* Update generation id to reflect rtable change */
561 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
565 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
573 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
574 struct rib_cmd_info *rc)
578 /* Check if updated gateway exists */
579 if ((info->rti_flags & RTF_GATEWAY) &&
580 (info->rti_info[RTAX_GATEWAY] == NULL))
584 * route change is done in multiple steps, with dropping and
585 * reacquiring lock. In the situations with multiple processes
586 * changes the same route in can lead to the case when route
587 * is changed between the steps. Address it by retrying the operation
588 * multiple times before failing.
590 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
591 error = change_route_one(rnh, info, rc);
600 * Performs modification of routing table specificed by @action.
601 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
602 * Needs to be run in network epoch.
604 * Returns 0 on success and fills in @rc with action result.
607 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
608 struct rib_cmd_info *rc)
614 error = rib_add_route(fibnum, info, rc);
617 error = rib_del_route(fibnum, info, rc);
620 error = rib_change_route(fibnum, info, rc);
632 struct rt_addrinfo info;
633 struct rib_head *rnh;
634 struct rtentry *head;
635 struct rib_cmd_info rc;
639 * Conditionally unlinks @rn from radix tree based
640 * on info data passed in @arg.
643 rt_checkdelroute(struct radix_node *rn, void *arg)
645 struct rt_delinfo *di;
646 struct rt_addrinfo *info;
650 di = (struct rt_delinfo *)arg;
651 rt = (struct rtentry *)rn;
655 info->rti_info[RTAX_DST] = rt_key(rt);
656 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
657 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
659 rt = rt_unlinkrte(di->rnh, info, &error);
661 /* Either not allowed or not matched. Skip entry */
665 /* Entry was unlinked. Notify subscribers */
668 di->rc.rc_nh_old = rt->rt_nhop;
669 rib_notify(di->rnh, RIB_NOTIFY_IMMEDIATE, &di->rc);
671 /* Add to the list and return */
672 rt->rt_chain = di->head;
679 * Iterates over a routing table specified by @fibnum and @family and
680 * deletes elements marked by @filter_f.
682 * @family: AF_ address family
683 * @filter_f: function returning non-zero value for items to delete
684 * @arg: data to pass to the @filter_f function
685 * @report: true if rtsock notification is needed.
688 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report)
690 struct rib_head *rnh;
691 struct rt_delinfo di;
693 struct epoch_tracker et;
695 rnh = rt_tables_get_rnh(fibnum, family);
699 bzero(&di, sizeof(di));
700 di.info.rti_filter = filter_f;
701 di.info.rti_filterdata = arg;
703 di.rc.rc_cmd = RTM_DELETE;
708 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
711 /* We might have something to reclaim. */
712 while (di.head != NULL) {
714 di.head = rt->rt_chain;
718 di.rc.rc_nh_old = rt->rt_nhop;
719 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
721 /* TODO std rt -> rt_addrinfo export */
722 di.info.rti_info[RTAX_DST] = rt_key(rt);
723 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
726 rt_routemsg(RTM_DELETE, rt, rt->rt_nhop->nh_ifp, 0,
735 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
736 struct rib_cmd_info *rc)
738 struct rib_subscription *rs;
740 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
741 if (rs->type == type)
742 rs->func(rnh, rc, rs->arg);
747 * Subscribe for the changes in the routing table specified by @fibnum and
750 * Returns pointer to the subscription structure on success.
752 struct rib_subscription *
753 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
754 enum rib_subscription_type type, bool waitok)
756 struct rib_head *rnh;
757 struct rib_subscription *rs;
758 struct epoch_tracker et;
759 int flags = M_ZERO | (waitok ? M_WAITOK : 0);
761 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
766 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
767 rnh = rt_tables_get_rnh(fibnum, family);
774 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
782 * Remove rtable subscription @rs from the table specified by @fibnum
784 * Needs to be run in network epoch.
786 * Returns 0 on success.
789 rib_unsibscribe(uint32_t fibnum, int family, struct rib_subscription *rs)
791 struct rib_head *rnh;
794 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
795 rnh = rt_tables_get_rnh(fibnum, family);
801 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
804 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
811 * Epoch callback indicating subscription is safe to destroy
814 destroy_subscription_epoch(epoch_context_t ctx)
816 struct rib_subscription *rs;
818 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);
824 rib_init_subscriptions(struct rib_head *rnh)
827 CK_STAILQ_INIT(&rnh->rnh_subscribers);
831 rib_destroy_subscriptions(struct rib_head *rnh)
833 struct rib_subscription *rs;
834 struct epoch_tracker et;
838 while ((rs = CK_STAILQ_FIRST(&rnh->rnh_subscribers)) != NULL) {
839 CK_STAILQ_REMOVE_HEAD(&rnh->rnh_subscribers, next);
840 epoch_call(net_epoch_preempt, destroy_subscription_epoch,