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 void rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
80 struct rib_cmd_info *rc);
82 static void rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info);
83 static void destroy_subscription_epoch(epoch_context_t ctx);
85 static struct rib_head *
86 get_rnh(uint32_t fibnum, const struct rt_addrinfo *info)
91 KASSERT((fibnum < rt_numfibs), ("rib_add_route: bad fibnum"));
93 dst = info->rti_info[RTAX_DST];
94 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
100 * Adds route defined by @info into the kernel table specified by @fibnum and
101 * sa_family in @info->rti_info[RTAX_DST].
103 * Returns 0 on success and fills in operation metadata into @rc.
106 rib_add_route(uint32_t fibnum, struct rt_addrinfo *info,
107 struct rib_cmd_info *rc)
109 struct rib_head *rnh;
113 rnh = get_rnh(fibnum, info);
115 return (EAFNOSUPPORT);
118 * Check consistency between RTF_HOST flag and netmask
121 if (info->rti_flags & RTF_HOST)
122 info->rti_info[RTAX_NETMASK] = NULL;
123 else if (info->rti_info[RTAX_NETMASK] == NULL)
126 bzero(rc, sizeof(struct rib_cmd_info));
127 rc->rc_cmd = RTM_ADD;
129 return (add_route(rnh, info, rc));
133 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
134 struct rib_cmd_info *rc)
136 struct sockaddr *dst, *ndst, *gateway, *netmask;
137 struct rtentry *rt, *rt_old;
138 struct nhop_object *nh;
139 struct radix_node *rn;
142 struct epoch_tracker et;
144 dst = info->rti_info[RTAX_DST];
145 gateway = info->rti_info[RTAX_GATEWAY];
146 netmask = info->rti_info[RTAX_NETMASK];
147 flags = info->rti_flags;
149 if ((flags & RTF_GATEWAY) && !gateway)
151 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
152 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
155 if (dst->sa_len > sizeof(((struct rtentry *)NULL)->rt_dstb))
158 if (info->rti_ifa == NULL) {
159 error = rt_getifa_fib(info, rnh->rib_fibnum);
163 ifa_ref(info->rti_ifa);
167 error = nhop_create_from_info(rnh, info, &nh);
170 ifa_free(info->rti_ifa);
174 rt = uma_zalloc(V_rtzone, M_NOWAIT);
176 ifa_free(info->rti_ifa);
180 rt->rt_flags = RTF_UP | flags;
184 memcpy(&rt->rt_dst, dst, dst->sa_len);
185 rt_key(rt) = &rt->rt_dst;
188 * point to the (possibly newly malloc'd) dest address.
190 ndst = (struct sockaddr *)rt_key(rt);
193 * make sure it contains the value we want (masked if needed).
196 rt_maskedcopy(dst, ndst, netmask);
198 bcopy(dst, ndst, dst->sa_len);
201 * We use the ifa reference returned by rt_getifa_fib().
202 * This moved from below so that rnh->rnh_addaddr() can
203 * examine the ifa and ifa->ifa_ifp if it so desires.
208 rt_setmetrics(info, rt);
214 /* do not permit exactly the same dst/mask/gw pair */
215 if (rt_mpath_capable(rnh) &&
216 rt_mpath_conflict(rnh, rt, netmask)) {
220 uma_zfree(V_rtzone, rt);
225 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
228 /* Most common usecase */
229 if (rt->rt_expire > 0)
230 tmproutes_update(rnh, rt);
232 /* Finalize notification */
235 rc->rc_rt = RNTORT(rn);
238 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
239 } else if ((info->rti_flags & RTF_PINNED) != 0) {
242 * Force removal and re-try addition
243 * TODO: better multipath&pinned support
245 struct sockaddr *info_dst = info->rti_info[RTAX_DST];
246 info->rti_info[RTAX_DST] = ndst;
247 /* Do not delete existing PINNED(interface) routes */
248 info->rti_flags &= ~RTF_PINNED;
249 rt_old = rt_unlinkrte(rnh, info, &error);
250 info->rti_flags |= RTF_PINNED;
251 info->rti_info[RTAX_DST] = info_dst;
252 if (rt_old != NULL) {
253 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head,
256 /* Finalize notification */
260 rc->rc_cmd = RTM_CHANGE;
261 rc->rc_rt = RNTORT(rn);
262 rc->rc_nh_old = rt_old->rt_nhop;
265 rc->rc_cmd = RTM_DELETE;
266 rc->rc_rt = RNTORT(rn);
267 rc->rc_nh_old = rt_old->rt_nhop;
270 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
275 if ((rn != NULL) || (rt_old != NULL))
276 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
278 if (rt_old != NULL) {
279 rt_notifydelete(rt_old, info);
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);
294 * If this protocol has something to add to this then
295 * allow it to do that as well.
297 if (ifa->ifa_rtrequest)
298 ifa->ifa_rtrequest(RTM_ADD, rt, rt->rt_nhop, info);
307 * Removes route defined by @info from the kernel table specified by @fibnum and
308 * sa_family in @info->rti_info[RTAX_DST].
310 * Returns 0 on success and fills in operation metadata into @rc.
313 rib_del_route(uint32_t fibnum, struct rt_addrinfo *info, struct rib_cmd_info *rc)
315 struct rib_head *rnh;
319 rnh = get_rnh(fibnum, info);
321 return (EAFNOSUPPORT);
323 bzero(rc, sizeof(struct rib_cmd_info));
324 rc->rc_cmd = RTM_DELETE;
326 return (del_route(rnh, info, rc));
330 * Conditionally unlinks rtentry matching data inside @info from @rnh.
331 * Returns unlinked, locked and referenced @rtentry on success,
332 * Returns NULL and sets @perror to:
333 * ESRCH - if prefix was not found,
334 * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
335 * ENOENT - if supplied filter function returned 0 (not matched).
338 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror)
340 struct sockaddr *dst, *netmask;
342 struct radix_node *rn;
344 dst = info->rti_info[RTAX_DST];
345 netmask = info->rti_info[RTAX_NETMASK];
347 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head);
353 if ((info->rti_flags & RTF_PINNED) == 0) {
354 /* Check if target route can be deleted */
355 if (rt->rt_flags & RTF_PINNED) {
356 *perror = EADDRINUSE;
361 if (info->rti_filter != NULL) {
362 if (info->rti_filter(rt, rt->rt_nhop, info->rti_filterdata)==0){
369 * Filter function requested rte deletion.
370 * Ease the caller work by filling in remaining info
371 * from that particular entry.
373 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
377 * Remove the item from the tree and return it.
378 * Complain if it is not there and do no more processing.
382 if (rt_mpath_capable(rnh))
383 rn = rt_mpath_unlink(rnh, info, rt, perror);
386 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
390 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
391 panic ("rtrequest delete");
395 rt->rt_flags &= ~RTF_UP;
403 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
404 struct rib_cmd_info *rc)
406 struct sockaddr *dst, *netmask;
407 struct sockaddr_storage mdst;
411 dst = info->rti_info[RTAX_DST];
412 netmask = info->rti_info[RTAX_NETMASK];
415 if (dst->sa_len > sizeof(mdst))
417 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
418 dst = (struct sockaddr *)&mdst;
422 rt = rt_unlinkrte(rnh, info, &error);
424 /* Finalize notification */
427 rc->rc_nh_old = rt->rt_nhop;
428 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
434 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
435 rt_notifydelete(rt, info);
438 * If the caller wants it, then it can have it,
439 * the entry will be deleted after the end of the current epoch.
447 rib_change_route(uint32_t fibnum, struct rt_addrinfo *info,
448 struct rib_cmd_info *rc)
450 struct rib_head *rnh;
454 rnh = get_rnh(fibnum, info);
456 return (EAFNOSUPPORT);
458 bzero(rc, sizeof(struct rib_cmd_info));
459 rc->rc_cmd = RTM_CHANGE;
461 return (change_route(rnh, info, rc));
465 change_route_one(struct rib_head *rnh, struct rt_addrinfo *info,
466 struct rib_cmd_info *rc)
469 struct rtentry *rt = NULL;
472 struct nhop_object *nh, *nh_orig;
475 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
476 info->rti_info[RTAX_NETMASK], &rnh->head);
485 * If we got multipath routes,
486 * we require users to specify a matching RTAX_GATEWAY.
488 if (rt_mpath_capable(rnh)) {
489 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
496 nh_orig = rt->rt_nhop;
504 * New gateway could require new ifaddr, ifp;
505 * flags may also be different; ifp may be specified
506 * by ll sockaddr when protocol address is ambiguous
508 if (((nh_orig->nh_flags & NHF_GATEWAY) &&
509 info->rti_info[RTAX_GATEWAY] != NULL) ||
510 info->rti_info[RTAX_IFP] != NULL ||
511 (info->rti_info[RTAX_IFA] != NULL &&
512 !sa_equal(info->rti_info[RTAX_IFA], nh_orig->nh_ifa->ifa_addr))) {
513 error = rt_getifa_fib(info, rnh->rib_fibnum);
514 if (info->rti_ifa != NULL)
519 ifa_free(info->rti_ifa);
520 info->rti_ifa = NULL;
527 error = nhop_create_from_nhop(rnh, nh_orig, info, &nh);
529 ifa_free(info->rti_ifa);
530 info->rti_ifa = NULL;
537 /* Lookup rtentry once again and check if nexthop is still the same */
538 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
539 info->rti_info[RTAX_NETMASK], &rnh->head);
547 if (rt->rt_nhop != nh_orig) {
553 /* Proceed with the update */
556 /* Provide notification to the protocols.*/
557 if ((nh_orig->nh_ifa != nh->nh_ifa) && nh_orig->nh_ifa->ifa_rtrequest)
558 nh_orig->nh_ifa->ifa_rtrequest(RTM_DELETE, rt, nh_orig, info);
561 rt_setmetrics(info, rt);
563 if ((nh_orig->nh_ifa != nh->nh_ifa) && nh_orig->nh_ifa->ifa_rtrequest)
564 nh_orig->nh_ifa->ifa_rtrequest(RTM_DELETE, rt, nh_orig, info);
566 /* Finalize notification */
568 rc->rc_nh_old = nh_orig;
569 rc->rc_nh_new = rt->rt_nhop;
573 /* Update generation id to reflect rtable change */
575 rib_notify(rnh, RIB_NOTIFY_IMMEDIATE, rc);
579 rib_notify(rnh, RIB_NOTIFY_DELAYED, rc);
587 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
588 struct rib_cmd_info *rc)
592 /* Check if updated gateway exists */
593 if ((info->rti_flags & RTF_GATEWAY) &&
594 (info->rti_info[RTAX_GATEWAY] == NULL))
598 * route change is done in multiple steps, with dropping and
599 * reacquiring lock. In the situations with multiple processes
600 * changes the same route in can lead to the case when route
601 * is changed between the steps. Address it by retrying the operation
602 * multiple times before failing.
604 for (int i = 0; i < RIB_MAX_RETRIES; i++) {
605 error = change_route_one(rnh, info, rc);
614 * Performs modification of routing table specificed by @action.
615 * Table is specified by @fibnum and sa_family in @info->rti_info[RTAX_DST].
616 * Needs to be run in network epoch.
618 * Returns 0 on success and fills in @rc with action result.
621 rib_action(uint32_t fibnum, int action, struct rt_addrinfo *info,
622 struct rib_cmd_info *rc)
628 error = rib_add_route(fibnum, info, rc);
631 error = rib_del_route(fibnum, info, rc);
634 error = rib_change_route(fibnum, info, rc);
645 rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info)
650 * give the protocol a chance to keep things in sync.
652 ifa = rt->rt_nhop->nh_ifa;
653 if (ifa != NULL && ifa->ifa_rtrequest != NULL)
654 ifa->ifa_rtrequest(RTM_DELETE, rt, rt->rt_nhop, info);
659 struct rt_addrinfo info;
660 struct rib_head *rnh;
661 struct rtentry *head;
662 struct rib_cmd_info rc;
666 * Conditionally unlinks @rn from radix tree based
667 * on info data passed in @arg.
670 rt_checkdelroute(struct radix_node *rn, void *arg)
672 struct rt_delinfo *di;
673 struct rt_addrinfo *info;
677 di = (struct rt_delinfo *)arg;
678 rt = (struct rtentry *)rn;
682 info->rti_info[RTAX_DST] = rt_key(rt);
683 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
684 info->rti_info[RTAX_GATEWAY] = &rt->rt_nhop->gw_sa;
686 rt = rt_unlinkrte(di->rnh, info, &error);
688 /* Either not allowed or not matched. Skip entry */
692 /* Entry was unlinked. Notify subscribers */
695 di->rc.rc_nh_old = rt->rt_nhop;
696 rib_notify(di->rnh, RIB_NOTIFY_IMMEDIATE, &di->rc);
698 /* Add to the list and return */
699 rt->rt_chain = di->head;
706 * Iterates over a routing table specified by @fibnum and @family and
707 * deletes elements marked by @filter_f.
709 * @family: AF_ address family
710 * @filter_f: function returning non-zero value for items to delete
711 * @arg: data to pass to the @filter_f function
712 * @report: true if rtsock notification is needed.
715 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report)
717 struct rib_head *rnh;
718 struct rt_delinfo di;
720 struct epoch_tracker et;
722 rnh = rt_tables_get_rnh(fibnum, family);
726 bzero(&di, sizeof(di));
727 di.info.rti_filter = filter_f;
728 di.info.rti_filterdata = arg;
730 di.rc.rc_cmd = RTM_DELETE;
735 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
738 /* We might have something to reclaim. */
739 while (di.head != NULL) {
741 di.head = rt->rt_chain;
745 di.rc.rc_nh_old = rt->rt_nhop;
746 rib_notify(rnh, RIB_NOTIFY_DELAYED, &di.rc);
748 /* TODO std rt -> rt_addrinfo export */
749 di.info.rti_info[RTAX_DST] = rt_key(rt);
750 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
752 rt_notifydelete(rt, &di.info);
755 rt_routemsg(RTM_DELETE, rt, rt->rt_nhop->nh_ifp, 0,
764 rib_notify(struct rib_head *rnh, enum rib_subscription_type type,
765 struct rib_cmd_info *rc)
767 struct rib_subscription *rs;
769 CK_STAILQ_FOREACH(rs, &rnh->rnh_subscribers, next) {
770 if (rs->type == type)
771 rs->func(rnh, rc, rs->arg);
776 * Subscribe for the changes in the routing table specified by @fibnum and
778 * Needs to be run in network epoch.
780 * Returns pointer to the subscription structure on success.
782 struct rib_subscription *
783 rib_subscribe(uint32_t fibnum, int family, rib_subscription_cb_t *f, void *arg,
784 enum rib_subscription_type type, int waitok)
786 struct rib_head *rnh;
787 struct rib_subscription *rs;
788 int flags = M_ZERO | (waitok ? M_WAITOK : 0);
791 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
792 rnh = rt_tables_get_rnh(fibnum, family);
794 rs = malloc(sizeof(struct rib_subscription), M_RTABLE, flags);
803 CK_STAILQ_INSERT_TAIL(&rnh->rnh_subscribers, rs, next);
810 * Remove rtable subscription @rs from the table specified by @fibnum
812 * Needs to be run in network epoch.
814 * Returns 0 on success.
817 rib_unsibscribe(uint32_t fibnum, int family, struct rib_subscription *rs)
819 struct rib_head *rnh;
822 KASSERT((fibnum < rt_numfibs), ("%s: bad fibnum", __func__));
823 rnh = rt_tables_get_rnh(fibnum, family);
829 CK_STAILQ_REMOVE(&rnh->rnh_subscribers, rs, rib_subscription, next);
832 epoch_call(net_epoch_preempt, destroy_subscription_epoch,
839 * Epoch callback indicating subscription is safe to destroy
842 destroy_subscription_epoch(epoch_context_t ctx)
844 struct rib_subscription *rs;
846 rs = __containerof(ctx, struct rib_subscription, epoch_ctx);