2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1980, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)route.c 8.3 (Berkeley) 1/9/95
66 * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $
67 * $DragonFly: src/sys/net/route.c,v 1.41 2008/11/09 10:50:15 sephe Exp $
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
77 #include <sys/socket.h>
78 #include <sys/domain.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
81 #include <sys/globaldata.h>
82 #include <sys/thread.h>
85 #include <net/route.h>
86 #include <net/netisr.h>
88 #include <netinet/in.h>
89 #include <net/ip_mroute/ip_mroute.h>
91 #include <sys/thread2.h>
92 #include <sys/msgport2.h>
93 #include <net/netmsg2.h>
96 #include <netproto/mpls/mpls.h>
99 static struct rtstatistics rtstatistics_percpu[MAXCPU];
101 #define rtstat rtstatistics_percpu[mycpuid]
103 #define rtstat rtstatistics_percpu[0]
106 struct radix_node_head *rt_tables[MAXCPU][AF_MAX+1];
107 struct lwkt_port *rt_ports[MAXCPU];
109 static void rt_maskedcopy (struct sockaddr *, struct sockaddr *,
111 static void rtable_init(void);
112 static void rtable_service_loop(void *dummy);
113 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo *,
114 struct rtentry *, void *);
117 static void rtredirect_msghandler(netmsg_t msg);
118 static void rtrequest1_msghandler(netmsg_t msg);
120 static void rtsearch_msghandler(netmsg_t msg);
121 static void rtmask_add_msghandler(netmsg_t msg);
123 static int rt_setshims(struct rtentry *, struct sockaddr **);
125 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW, 0, "Routing");
128 static int route_debug = 1;
129 SYSCTL_INT(_net_route, OID_AUTO, route_debug, CTLFLAG_RW,
130 &route_debug, 0, "");
133 int route_assert_owner_access = 0;
134 SYSCTL_INT(_net_route, OID_AUTO, assert_owner_access, CTLFLAG_RW,
135 &route_assert_owner_access, 0, "");
137 u_long route_kmalloc_limit = 0;
138 TUNABLE_ULONG("net.route.kmalloc_limit", &route_kmalloc_limit);
141 * Initialize the route table(s) for protocol domains and
142 * create a helper thread which will be responsible for updating
143 * route table entries on each cpu.
151 for (cpu = 0; cpu < ncpus; ++cpu)
152 bzero(&rtstatistics_percpu[cpu], sizeof(struct rtstatistics));
153 rn_init(); /* initialize all zeroes, all ones, mask table */
154 rtable_init(); /* call dom_rtattach() on each cpu */
156 for (cpu = 0; cpu < ncpus; cpu++) {
157 lwkt_create(rtable_service_loop, NULL, &rtd, NULL,
158 0, cpu, "rtable_cpu %d", cpu);
159 rt_ports[cpu] = &rtd->td_msgport;
162 if (route_kmalloc_limit)
163 kmalloc_raise_limit(M_RTABLE, route_kmalloc_limit);
167 rtable_init_oncpu(netmsg_t msg)
172 SLIST_FOREACH(dom, &domains, dom_next) {
173 if (dom->dom_rtattach) {
175 (void **)&rt_tables[cpu][dom->dom_family],
179 ifnet_forwardmsg(&msg->lmsg, cpu + 1);
185 struct netmsg_base msg;
187 netmsg_init(&msg, NULL, &curthread->td_msgport, 0, rtable_init_oncpu);
188 ifnet_domsg(&msg.lmsg, 0);
192 * Our per-cpu table management protocol thread. All route table operations
193 * are sequentially chained through all cpus starting at cpu #0 in order to
194 * maintain duplicate route tables on each cpu. Having a spearate route
195 * table management thread allows the protocol and interrupt threads to
196 * issue route table changes.
199 rtable_service_loop(void *dummy __unused)
202 thread_t td = curthread;
204 while ((msg = lwkt_waitport(&td->td_msgport, 0)) != NULL) {
205 msg->nm_dispatch((netmsg_t)msg);
210 * Routing statistics.
214 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS)
218 for (cpu = 0; cpu < ncpus; ++cpu) {
219 if ((error = SYSCTL_OUT(req, &rtstatistics_percpu[cpu],
220 sizeof(struct rtstatistics))))
222 if ((error = SYSCTL_IN(req, &rtstatistics_percpu[cpu],
223 sizeof(struct rtstatistics))))
229 SYSCTL_PROC(_net_route, OID_AUTO, stats, (CTLTYPE_OPAQUE|CTLFLAG_RW),
230 0, 0, sysctl_rtstatistics, "S,rtstatistics", "Routing statistics");
232 SYSCTL_STRUCT(_net_route, OID_AUTO, stats, CTLFLAG_RW, &rtstat, rtstatistics,
233 "Routing statistics");
237 * Packet routing routines.
241 * Look up and fill in the "ro_rt" rtentry field in a route structure given
242 * an address in the "ro_dst" field. Always send a report on a miss and
243 * always clone routes.
246 rtalloc(struct route *ro)
248 rtalloc_ign(ro, 0UL);
252 * Look up and fill in the "ro_rt" rtentry field in a route structure given
253 * an address in the "ro_dst" field. Always send a report on a miss and
254 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
258 rtalloc_ign(struct route *ro, u_long ignoreflags)
260 if (ro->ro_rt != NULL) {
261 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP)
266 ro->ro_rt = _rtlookup(&ro->ro_dst, RTL_REPORTMSG, ignoreflags);
270 * Look up the route that matches the given "dst" address.
272 * Route lookup can have the side-effect of creating and returning
273 * a cloned route instead when "dst" matches a cloning route and the
274 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
276 * Any route returned has its reference count incremented.
279 _rtlookup(struct sockaddr *dst, boolean_t generate_report, u_long ignore)
281 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
288 * Look up route in the radix tree.
290 rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh);
295 * Handle cloning routes.
297 if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) {
298 struct rtentry *clonedroute;
301 clonedroute = rt; /* copy in/copy out parameter */
302 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
303 &clonedroute); /* clone the route */
304 if (error != 0) { /* cloning failed */
306 rt_dstmsg(RTM_MISS, dst, error);
308 return (rt); /* return the uncloned route */
310 if (generate_report) {
311 if (clonedroute->rt_flags & RTF_XRESOLVE)
312 rt_dstmsg(RTM_RESOLVE, dst, 0);
314 rt_rtmsg(RTM_ADD, clonedroute,
315 clonedroute->rt_ifp, 0);
317 return (clonedroute); /* return cloned route */
321 * Increment the reference count of the matched route and return.
327 rtstat.rts_unreach++;
329 rt_dstmsg(RTM_MISS, dst, 0);
334 rtfree(struct rtentry *rt)
336 if (rt->rt_cpuid == mycpuid)
339 rtfree_remote(rt, 1);
343 rtfree_oncpu(struct rtentry *rt)
345 KKASSERT(rt->rt_cpuid == mycpuid);
346 KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt));
349 if (rt->rt_refcnt == 0) {
350 struct radix_node_head *rnh =
351 rt_tables[mycpuid][rt_key(rt)->sa_family];
354 rnh->rnh_close((struct radix_node *)rt, rnh);
355 if (!(rt->rt_flags & RTF_UP)) {
356 /* deallocate route */
357 if (rt->rt_ifa != NULL)
359 if (rt->rt_parent != NULL)
360 RTFREE(rt->rt_parent); /* recursive call! */
368 rtfree_remote_dispatch(netmsg_t msg)
370 struct lwkt_msg *lmsg = &msg->lmsg;
371 struct rtentry *rt = lmsg->u.ms_resultp;
374 lwkt_replymsg(lmsg, 0);
378 rtfree_remote(struct rtentry *rt, int allow_panic)
380 struct netmsg_base msg;
381 struct lwkt_msg *lmsg;
383 KKASSERT(rt->rt_cpuid != mycpuid);
385 if (route_assert_owner_access && allow_panic) {
386 panic("rt remote free rt_cpuid %d, mycpuid %d\n",
387 rt->rt_cpuid, mycpuid);
389 kprintf("rt remote free rt_cpuid %d, mycpuid %d\n",
390 rt->rt_cpuid, mycpuid);
394 netmsg_init(&msg, NULL, &curthread->td_msgport,
395 0, rtfree_remote_dispatch);
397 lmsg->u.ms_resultp = rt;
399 lwkt_domsg(rtable_portfn(rt->rt_cpuid), lmsg, 0);
403 rtredirect_oncpu(struct sockaddr *dst, struct sockaddr *gateway,
404 struct sockaddr *netmask, int flags, struct sockaddr *src)
406 struct rtentry *rt = NULL;
407 struct rt_addrinfo rtinfo;
412 /* verify the gateway is directly reachable */
413 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
419 * If the redirect isn't from our current router for this destination,
420 * it's either old or wrong.
422 if (!(flags & RTF_DONE) && /* XXX JH */
423 (rt = rtpurelookup(dst)) != NULL &&
424 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) {
430 * If it redirects us to ourselves, we have a routing loop,
431 * perhaps as a result of an interface going down recently.
433 if (ifa_ifwithaddr(gateway)) {
434 error = EHOSTUNREACH;
439 * Create a new entry if the lookup failed or if we got back
440 * a wildcard entry for the default route. This is necessary
441 * for hosts which use routing redirects generated by smart
442 * gateways to dynamically build the routing tables.
446 if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) {
451 /* Ignore redirects for directly connected hosts. */
452 if (!(rt->rt_flags & RTF_GATEWAY)) {
453 error = EHOSTUNREACH;
457 if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) {
459 * Changing from a network route to a host route.
460 * Create a new host route rather than smashing the
464 flags |= RTF_GATEWAY | RTF_DYNAMIC;
465 bzero(&rtinfo, sizeof(struct rt_addrinfo));
466 rtinfo.rti_info[RTAX_DST] = dst;
467 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
468 rtinfo.rti_info[RTAX_NETMASK] = netmask;
469 rtinfo.rti_flags = flags;
470 rtinfo.rti_ifa = ifa;
471 rt = NULL; /* copy-in/copy-out parameter */
472 error = rtrequest1(RTM_ADD, &rtinfo, &rt);
474 flags = rt->rt_flags;
475 stat = &rtstat.rts_dynamic;
478 * Smash the current notion of the gateway to this destination.
479 * Should check about netmask!!!
481 rt->rt_flags |= RTF_MODIFIED;
482 flags |= RTF_MODIFIED;
484 /* We only need to report rtmsg on CPU0 */
485 rt_setgate(rt, rt_key(rt), gateway,
486 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT);
488 stat = &rtstat.rts_newgateway;
496 rtstat.rts_badredirect++;
497 else if (stat != NULL)
505 struct netmsg_rtredirect {
506 struct netmsg_base base;
507 struct sockaddr *dst;
508 struct sockaddr *gateway;
509 struct sockaddr *netmask;
511 struct sockaddr *src;
517 * Force a routing table entry to the specified
518 * destination to go through the given gateway.
519 * Normally called as a result of a routing redirect
520 * message from the network layer.
522 * N.B.: must be called at splnet
525 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
526 struct sockaddr *netmask, int flags, struct sockaddr *src)
528 struct rt_addrinfo rtinfo;
531 struct netmsg_rtredirect msg;
533 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
534 0, rtredirect_msghandler);
536 msg.gateway = gateway;
537 msg.netmask = netmask;
540 error = lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0);
542 error = rtredirect_oncpu(dst, gateway, netmask, flags, src);
544 bzero(&rtinfo, sizeof(struct rt_addrinfo));
545 rtinfo.rti_info[RTAX_DST] = dst;
546 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
547 rtinfo.rti_info[RTAX_NETMASK] = netmask;
548 rtinfo.rti_info[RTAX_AUTHOR] = src;
549 rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error);
555 rtredirect_msghandler(netmsg_t msg)
557 struct netmsg_rtredirect *rmsg = (void *)msg;
560 rtredirect_oncpu(rmsg->dst, rmsg->gateway, rmsg->netmask,
561 rmsg->flags, rmsg->src);
562 nextcpu = mycpuid + 1;
564 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->lmsg);
566 lwkt_replymsg(&msg->lmsg, 0);
572 * Routing table ioctl interface.
575 rtioctl(u_long req, caddr_t data, struct ucred *cred)
578 /* Multicast goop, grrr... */
579 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
586 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
590 if (!(flags & RTF_GATEWAY)) {
592 * If we are adding a route to an interface,
593 * and the interface is a point-to-point link,
594 * we should search for the destination
595 * as our clue to the interface. Otherwise
596 * we can use the local address.
599 if (flags & RTF_HOST) {
600 ifa = ifa_ifwithdstaddr(dst);
603 ifa = ifa_ifwithaddr(gateway);
606 * If we are adding a route to a remote net
607 * or host, the gateway may still be on the
608 * other end of a pt to pt link.
610 ifa = ifa_ifwithdstaddr(gateway);
613 ifa = ifa_ifwithnet(gateway);
617 rt = rtpurelookup(gateway);
621 if ((ifa = rt->rt_ifa) == NULL)
624 if (ifa->ifa_addr->sa_family != dst->sa_family) {
625 struct ifaddr *oldifa = ifa;
627 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
634 static int rt_fixdelete (struct radix_node *, void *);
635 static int rt_fixchange (struct radix_node *, void *);
639 struct radix_node_head *rnh;
643 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
646 rt_getifa(struct rt_addrinfo *rtinfo)
648 struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY];
649 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
650 struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA];
651 int flags = rtinfo->rti_flags;
654 * ifp may be specified by sockaddr_dl
655 * when protocol address is ambiguous.
657 if (rtinfo->rti_ifp == NULL) {
658 struct sockaddr *ifpaddr;
660 ifpaddr = rtinfo->rti_info[RTAX_IFP];
661 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
664 ifa = ifa_ifwithnet(ifpaddr);
666 rtinfo->rti_ifp = ifa->ifa_ifp;
670 if (rtinfo->rti_ifa == NULL && ifaaddr != NULL)
671 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr);
672 if (rtinfo->rti_ifa == NULL) {
675 sa = ifaaddr != NULL ? ifaaddr :
676 (gateway != NULL ? gateway : dst);
677 if (sa != NULL && rtinfo->rti_ifp != NULL)
678 rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp);
679 else if (dst != NULL && gateway != NULL)
680 rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
682 rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa);
684 if (rtinfo->rti_ifa == NULL)
685 return (ENETUNREACH);
687 if (rtinfo->rti_ifp == NULL)
688 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp;
693 * Do appropriate manipulations of a routing tree given
694 * all the bits of info needed
699 struct sockaddr *dst,
700 struct sockaddr *gateway,
701 struct sockaddr *netmask,
703 struct rtentry **ret_nrt)
705 struct rt_addrinfo rtinfo;
707 bzero(&rtinfo, sizeof(struct rt_addrinfo));
708 rtinfo.rti_info[RTAX_DST] = dst;
709 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
710 rtinfo.rti_info[RTAX_NETMASK] = netmask;
711 rtinfo.rti_flags = flags;
712 return rtrequest1(req, &rtinfo, ret_nrt);
718 struct sockaddr *dst,
719 struct sockaddr *gateway,
720 struct sockaddr *netmask,
723 struct rt_addrinfo rtinfo;
725 bzero(&rtinfo, sizeof(struct rt_addrinfo));
726 rtinfo.rti_info[RTAX_DST] = dst;
727 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
728 rtinfo.rti_info[RTAX_NETMASK] = netmask;
729 rtinfo.rti_flags = flags;
730 return rtrequest1_global(req, &rtinfo, NULL, NULL);
736 struct netmsg_base base;
738 struct rt_addrinfo *rtinfo;
739 rtrequest1_callback_func_t callback;
746 rtrequest1_global(int req, struct rt_addrinfo *rtinfo,
747 rtrequest1_callback_func_t callback, void *arg)
751 struct netmsg_rtq msg;
753 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
754 0, rtrequest1_msghandler);
755 msg.base.lmsg.ms_error = -1;
758 msg.callback = callback;
760 error = lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0);
762 struct rtentry *rt = NULL;
764 error = rtrequest1(req, rtinfo, &rt);
768 callback(req, error, rtinfo, rt, arg);
774 * Handle a route table request on the current cpu. Since the route table's
775 * are supposed to be identical on each cpu, an error occuring later in the
776 * message chain is considered system-fatal.
781 rtrequest1_msghandler(netmsg_t msg)
783 struct netmsg_rtq *rmsg = (void *)msg;
784 struct rt_addrinfo rtinfo;
785 struct rtentry *rt = NULL;
790 * Copy the rtinfo. We need to make sure that the original
791 * rtinfo, which is setup by the caller, in the netmsg will
792 * _not_ be changed; else the next CPU on the netmsg forwarding
793 * path will see a different rtinfo than what this CPU has seen.
795 rtinfo = *rmsg->rtinfo;
797 error = rtrequest1(rmsg->req, &rtinfo, &rt);
801 rmsg->callback(rmsg->req, error, &rtinfo, rt, rmsg->arg);
804 * RTM_DELETE's are propogated even if an error occurs, since a
805 * cloned route might be undergoing deletion and cloned routes
806 * are not necessarily replicated. An overall error is returned
807 * only if no cpus have the route in question.
809 if (rmsg->base.lmsg.ms_error < 0 || error == 0)
810 rmsg->base.lmsg.ms_error = error;
812 nextcpu = mycpuid + 1;
813 if (error && rmsg->req != RTM_DELETE) {
815 panic("rtrequest1_msghandler: rtrequest table "
816 "error was cpu%d, err %d\n", mycpuid, error);
818 lwkt_replymsg(&rmsg->base.lmsg, error);
819 } else if (nextcpu < ncpus) {
820 lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
822 lwkt_replymsg(&rmsg->base.lmsg, rmsg->base.lmsg.ms_error);
829 rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt)
831 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
833 struct radix_node *rn;
834 struct radix_node_head *rnh;
836 struct sockaddr *ndst;
840 #define gotoerr(x) { error = x ; goto bad; }
844 rt_addrinfo_print(req, rtinfo);
849 * Find the correct routing tree to use for this Address Family
851 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL)
852 gotoerr(EAFNOSUPPORT);
855 * If we are adding a host route then we don't want to put
856 * a netmask in the tree, nor do we want to clone it.
858 if (rtinfo->rti_flags & RTF_HOST) {
859 rtinfo->rti_info[RTAX_NETMASK] = NULL;
860 rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
865 /* Remove the item from the tree. */
866 rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST],
867 (char *)rtinfo->rti_info[RTAX_NETMASK],
871 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)),
872 ("rnh_deladdr returned flags 0x%x", rn->rn_flags));
873 rt = (struct rtentry *)rn;
875 /* ref to prevent a deletion race */
878 /* Free any routes cloned from this one. */
879 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
880 rt_mask(rt) != NULL) {
881 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
886 if (rt->rt_gwroute != NULL) {
887 RTFREE(rt->rt_gwroute);
888 rt->rt_gwroute = NULL;
892 * NB: RTF_UP must be set during the search above,
893 * because we might delete the last ref, causing
894 * rt to get freed prematurely.
896 rt->rt_flags &= ~RTF_UP;
900 rt_print(rtinfo, rt);
903 /* Give the protocol a chance to keep things in sync. */
904 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
905 ifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
908 * If the caller wants it, then it can have it,
909 * but it's up to it to free the rtentry as we won't be
912 KASSERT(rt->rt_refcnt >= 0,
913 ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt));
914 if (ret_nrt != NULL) {
915 /* leave ref intact for return */
918 /* deref / attempt to destroy */
924 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
928 rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
929 rtinfo->rti_flags |= RTF_WASCLONED;
930 rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
931 if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL)
932 rtinfo->rti_flags |= RTF_HOST;
933 rtinfo->rti_info[RTAX_MPLS1] = rt->rt_shim[0];
934 rtinfo->rti_info[RTAX_MPLS2] = rt->rt_shim[1];
935 rtinfo->rti_info[RTAX_MPLS3] = rt->rt_shim[2];
939 KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) ||
940 rtinfo->rti_info[RTAX_GATEWAY] != NULL,
941 ("rtrequest: GATEWAY but no gateway"));
943 if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo)))
945 ifa = rtinfo->rti_ifa;
947 R_Malloc(rt, struct rtentry *, sizeof(struct rtentry));
949 if (req == RTM_ADD) {
950 kprintf("rtrequest1: alloc rtentry failed on "
955 bzero(rt, sizeof(struct rtentry));
956 rt->rt_flags = RTF_UP | rtinfo->rti_flags;
957 rt->rt_cpuid = mycpuid;
959 if (mycpuid != 0 && req == RTM_ADD) {
960 /* For RTM_ADD, we have already sent rtmsg on CPU0. */
961 reportmsg = RTL_DONTREPORT;
964 * For RTM_ADD, we only send rtmsg on CPU0.
965 * For RTM_RESOLVE, we always send rtmsg. XXX
967 reportmsg = RTL_REPORTMSG;
969 error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY],
977 if (rtinfo->rti_info[RTAX_NETMASK] != NULL)
978 rt_maskedcopy(dst, ndst,
979 rtinfo->rti_info[RTAX_NETMASK]);
981 bcopy(dst, ndst, dst->sa_len);
983 if (rtinfo->rti_info[RTAX_MPLS1] != NULL)
984 rt_setshims(rt, rtinfo->rti_info);
987 * Note that we now have a reference to the ifa.
988 * This moved from below so that rnh->rnh_addaddr() can
989 * examine the ifa and ifa->ifa_ifp if it so desires.
993 rt->rt_ifp = ifa->ifa_ifp;
994 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
996 rn = rnh->rnh_addaddr((char *)ndst,
997 (char *)rtinfo->rti_info[RTAX_NETMASK],
1000 struct rtentry *oldrt;
1003 * We already have one of these in the tree.
1004 * We do a special hack: if the old route was
1005 * cloned, then we blow it away and try
1006 * re-inserting the new one.
1008 oldrt = rtpurelookup(ndst);
1009 if (oldrt != NULL) {
1011 if (oldrt->rt_flags & RTF_WASCLONED) {
1012 rtrequest(RTM_DELETE, rt_key(oldrt),
1015 oldrt->rt_flags, NULL);
1016 rn = rnh->rnh_addaddr((char *)ndst,
1018 rtinfo->rti_info[RTAX_NETMASK],
1025 * If it still failed to go into the tree,
1026 * then un-make it (this should be a function).
1029 if (rt->rt_gwroute != NULL)
1030 rtfree(rt->rt_gwroute);
1038 * If we got here from RESOLVE, then we are cloning
1039 * so clone the rest, and note that we
1040 * are a clone (and increment the parent's references)
1042 if (req == RTM_RESOLVE) {
1043 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
1044 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
1045 if ((*ret_nrt)->rt_flags &
1046 (RTF_CLONING | RTF_PRCLONING)) {
1047 rt->rt_parent = *ret_nrt;
1048 (*ret_nrt)->rt_refcnt++;
1053 * if this protocol has something to add to this then
1054 * allow it to do that as well.
1056 if (ifa->ifa_rtrequest != NULL)
1057 ifa->ifa_rtrequest(req, rt, rtinfo);
1060 * We repeat the same procedure from rt_setgate() here because
1061 * it doesn't fire when we call it there because the node
1062 * hasn't been added to the tree yet.
1064 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) &&
1065 rt_mask(rt) != NULL) {
1066 struct rtfc_arg arg = { rt, rnh };
1068 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1069 (char *)rt_mask(rt),
1070 rt_fixchange, &arg);
1075 rt_print(rtinfo, rt);
1078 * Return the resulting rtentry,
1079 * increasing the number of references by one.
1081 if (ret_nrt != NULL) {
1093 kprintf("rti %p failed error %d\n", rtinfo, error);
1095 kprintf("rti %p succeeded\n", rtinfo);
1103 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1104 * (i.e., the routes related to it by the operation of cloning). This
1105 * routine is iterated over all potential former-child-routes by way of
1106 * rnh->rnh_walktree_from() above, and those that actually are children of
1107 * the late parent (passed in as VP here) are themselves deleted.
1110 rt_fixdelete(struct radix_node *rn, void *vp)
1112 struct rtentry *rt = (struct rtentry *)rn;
1113 struct rtentry *rt0 = vp;
1115 if (rt->rt_parent == rt0 &&
1116 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1117 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1118 rt->rt_flags, NULL);
1124 * This routine is called from rt_setgate() to do the analogous thing for
1125 * adds and changes. There is the added complication in this case of a
1126 * middle insert; i.e., insertion of a new network route between an older
1127 * network route and (cloned) host routes. For this reason, a simple check
1128 * of rt->rt_parent is insufficient; each candidate route must be tested
1129 * against the (mask, value) of the new route (passed as before in vp)
1130 * to see if the new route matches it.
1132 * XXX - it may be possible to do fixdelete() for changes and reserve this
1133 * routine just for adds. I'm not sure why I thought it was necessary to do
1137 static int rtfcdebug = 0;
1141 rt_fixchange(struct radix_node *rn, void *vp)
1143 struct rtentry *rt = (struct rtentry *)rn;
1144 struct rtfc_arg *ap = vp;
1145 struct rtentry *rt0 = ap->rt0;
1146 struct radix_node_head *rnh = ap->rnh;
1147 u_char *xk1, *xm1, *xk2, *xmp;
1152 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
1155 if (rt->rt_parent == NULL ||
1156 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1158 if (rtfcdebug) kprintf("no parent, pinned or cloning\n");
1163 if (rt->rt_parent == rt0) {
1165 if (rtfcdebug) kprintf("parent match\n");
1167 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1168 rt->rt_flags, NULL);
1172 * There probably is a function somewhere which does this...
1173 * if not, there should be.
1175 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1177 xk1 = (u_char *)rt_key(rt0);
1178 xm1 = (u_char *)rt_mask(rt0);
1179 xk2 = (u_char *)rt_key(rt);
1181 /* avoid applying a less specific route */
1182 xmp = (u_char *)rt_mask(rt->rt_parent);
1183 mlen = rt_key(rt->rt_parent)->sa_len;
1184 if (mlen > rt_key(rt0)->sa_len) {
1187 kprintf("rt_fixchange: inserting a less "
1188 "specific route\n");
1192 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
1193 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
1196 kprintf("rt_fixchange: inserting a less "
1197 "specific route\n");
1203 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
1204 if ((xk2[i] & xm1[i]) != xk1[i]) {
1206 if (rtfcdebug) kprintf("no match\n");
1213 * OK, this node is a clone, and matches the node currently being
1214 * changed/added under the node's mask. So, get rid of it.
1217 if (rtfcdebug) kprintf("deleting\n");
1219 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1220 rt->rt_flags, NULL);
1223 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1226 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate,
1227 boolean_t generate_report)
1229 char *space, *oldspace;
1230 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
1231 struct rtentry *rt = rt0;
1232 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
1235 * A host route with the destination equal to the gateway
1236 * will interfere with keeping LLINFO in the routing
1237 * table, so disallow it.
1239 if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) ==
1240 (RTF_HOST | RTF_GATEWAY)) &&
1241 dst->sa_len == gate->sa_len &&
1242 sa_equal(dst, gate)) {
1244 * The route might already exist if this is an RTM_CHANGE
1245 * or a routing redirect, so try to delete it.
1247 if (rt_key(rt0) != NULL)
1248 rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway,
1249 rt_mask(rt0), rt0->rt_flags, NULL);
1250 return EADDRNOTAVAIL;
1254 * Both dst and gateway are stored in the same malloc'ed chunk
1255 * (If I ever get my hands on....)
1256 * if we need to malloc a new chunk, then keep the old one around
1257 * till we don't need it any more.
1259 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
1260 oldspace = (char *)rt_key(rt);
1261 R_Malloc(space, char *, dlen + glen);
1264 rt->rt_nodes->rn_key = space;
1266 space = (char *)rt_key(rt); /* Just use the old space. */
1270 /* Set the gateway value. */
1271 rt->rt_gateway = (struct sockaddr *)(space + dlen);
1272 bcopy(gate, rt->rt_gateway, glen);
1274 if (oldspace != NULL) {
1276 * If we allocated a new chunk, preserve the original dst.
1277 * This way, rt_setgate() really just sets the gate
1278 * and leaves the dst field alone.
1280 bcopy(dst, space, dlen);
1285 * If there is already a gwroute, it's now almost definitely wrong
1288 if (rt->rt_gwroute != NULL) {
1289 RTFREE(rt->rt_gwroute);
1290 rt->rt_gwroute = NULL;
1292 if (rt->rt_flags & RTF_GATEWAY) {
1294 * Cloning loop avoidance: In the presence of
1295 * protocol-cloning and bad configuration, it is
1296 * possible to get stuck in bottomless mutual recursion
1297 * (rtrequest rt_setgate rtlookup). We avoid this
1298 * by not allowing protocol-cloning to operate for
1299 * gateways (which is probably the correct choice
1300 * anyway), and avoid the resulting reference loops
1301 * by disallowing any route to run through itself as
1302 * a gateway. This is obviously mandatory when we
1303 * get rt->rt_output().
1305 * This breaks TTCP for hosts outside the gateway! XXX JH
1307 rt->rt_gwroute = _rtlookup(gate, generate_report,
1309 if (rt->rt_gwroute == rt) {
1310 rt->rt_gwroute = NULL;
1312 return EDQUOT; /* failure */
1317 * This isn't going to do anything useful for host routes, so
1318 * don't bother. Also make sure we have a reasonable mask
1319 * (we don't yet have one during adds).
1321 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1322 struct rtfc_arg arg = { rt, rnh };
1324 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1325 (char *)rt_mask(rt),
1326 rt_fixchange, &arg);
1334 struct sockaddr *src,
1335 struct sockaddr *dst,
1336 struct sockaddr *netmask)
1338 u_char *cp1 = (u_char *)src;
1339 u_char *cp2 = (u_char *)dst;
1340 u_char *cp3 = (u_char *)netmask;
1341 u_char *cplim = cp2 + *cp3;
1342 u_char *cplim2 = cp2 + *cp1;
1344 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1349 *cp2++ = *cp1++ & *cp3++;
1351 bzero(cp2, cplim2 - cp2);
1355 rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt)
1357 struct rtentry *up_rt, *rt;
1359 if (!(rt0->rt_flags & RTF_UP)) {
1360 up_rt = rtlookup(dst);
1362 return (EHOSTUNREACH);
1366 if (up_rt->rt_flags & RTF_GATEWAY) {
1367 if (up_rt->rt_gwroute == NULL) {
1368 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1369 if (up_rt->rt_gwroute == NULL)
1370 return (EHOSTUNREACH);
1371 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) {
1372 rtfree(up_rt->rt_gwroute);
1373 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1374 if (up_rt->rt_gwroute == NULL)
1375 return (EHOSTUNREACH);
1377 rt = up_rt->rt_gwroute;
1380 if (rt->rt_flags & RTF_REJECT &&
1381 (rt->rt_rmx.rmx_expire == 0 || /* rt doesn't expire */
1382 time_second < rt->rt_rmx.rmx_expire)) /* rt not expired */
1383 return (rt->rt_flags & RTF_HOST ? EHOSTDOWN : EHOSTUNREACH);
1389 rt_setshims(struct rtentry *rt, struct sockaddr **rt_shim){
1392 for (i=0; i<3; i++) {
1393 struct sockaddr *shim = rt_shim[RTAX_MPLS1 + i];
1399 shimlen = ROUNDUP(shim->sa_len);
1400 R_Malloc(rt->rt_shim[i], struct sockaddr *, shimlen);
1401 bcopy(shim, rt->rt_shim[i], shimlen);
1410 * Print out a route table entry
1413 rt_print(struct rt_addrinfo *rtinfo, struct rtentry *rn)
1415 kprintf("rti %p cpu %d route %p flags %08lx: ",
1416 rtinfo, mycpuid, rn, rn->rt_flags);
1417 sockaddr_print(rt_key(rn));
1419 sockaddr_print(rt_mask(rn));
1421 sockaddr_print(rn->rt_gateway);
1422 kprintf(" ifc \"%s\"", rn->rt_ifp ? rn->rt_ifp->if_dname : "?");
1423 kprintf(" ifa %p\n", rn->rt_ifa);
1427 rt_addrinfo_print(int cmd, struct rt_addrinfo *rti)
1433 if (cmd == RTM_DELETE && route_debug > 1)
1434 print_backtrace(-1);
1448 kprintf("C%02d ", cmd);
1451 kprintf("rti %p cpu %d ", rti, mycpuid);
1452 for (i = 0; i < rti->rti_addrs; ++i) {
1453 if (rti->rti_info[i] == NULL)
1483 kprintf("(?%02d ", i);
1486 sockaddr_print(rti->rti_info[i]);
1494 sockaddr_print(struct sockaddr *sa)
1496 struct sockaddr_in *sa4;
1497 struct sockaddr_in6 *sa6;
1506 len = sa->sa_len - offsetof(struct sockaddr, sa_data[0]);
1508 switch(sa->sa_family) {
1512 switch(sa->sa_family) {
1514 sa4 = (struct sockaddr_in *)sa;
1515 kprintf("INET %d %d.%d.%d.%d",
1516 ntohs(sa4->sin_port),
1517 (ntohl(sa4->sin_addr.s_addr) >> 24) & 255,
1518 (ntohl(sa4->sin_addr.s_addr) >> 16) & 255,
1519 (ntohl(sa4->sin_addr.s_addr) >> 8) & 255,
1520 (ntohl(sa4->sin_addr.s_addr) >> 0) & 255
1524 sa6 = (struct sockaddr_in6 *)sa;
1525 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
1526 ntohs(sa6->sin6_port),
1527 sa6->sin6_addr.s6_addr16[0],
1528 sa6->sin6_addr.s6_addr16[1],
1529 sa6->sin6_addr.s6_addr16[2],
1530 sa6->sin6_addr.s6_addr16[3],
1531 sa6->sin6_addr.s6_addr16[4],
1532 sa6->sin6_addr.s6_addr16[5],
1533 sa6->sin6_addr.s6_addr16[6],
1534 sa6->sin6_addr.s6_addr16[7]
1538 kprintf("AF%d ", sa->sa_family);
1539 while (len > 0 && sa->sa_data[len-1] == 0)
1542 for (i = 0; i < len; ++i) {
1545 kprintf("%d", (unsigned char)sa->sa_data[i]);
1555 * Set up a routing table entry, normally for an interface.
1558 rtinit(struct ifaddr *ifa, int cmd, int flags)
1560 struct sockaddr *dst, *deldst, *netmask;
1561 struct mbuf *m = NULL;
1562 struct radix_node_head *rnh;
1563 struct radix_node *rn;
1564 struct rt_addrinfo rtinfo;
1567 if (flags & RTF_HOST) {
1568 dst = ifa->ifa_dstaddr;
1571 dst = ifa->ifa_addr;
1572 netmask = ifa->ifa_netmask;
1575 * If it's a delete, check that if it exists, it's on the correct
1576 * interface or we might scrub a route to another ifa which would
1577 * be confusing at best and possibly worse.
1579 if (cmd == RTM_DELETE) {
1581 * It's a delete, so it should already exist..
1582 * If it's a net, mask off the host bits
1583 * (Assuming we have a mask)
1585 if (netmask != NULL) {
1586 m = m_get(MB_DONTWAIT, MT_SONAME);
1590 deldst = mtod(m, struct sockaddr *);
1591 rt_maskedcopy(dst, deldst, netmask);
1595 * Look up an rtentry that is in the routing tree and
1596 * contains the correct info.
1598 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL ||
1599 (rn = rnh->rnh_lookup((char *)dst,
1600 (char *)netmask, rnh)) == NULL ||
1601 ((struct rtentry *)rn)->rt_ifa != ifa ||
1602 !sa_equal((struct sockaddr *)rn->rn_key, dst)) {
1605 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1611 * One would think that as we are deleting, and we know
1612 * it doesn't exist, we could just return at this point
1613 * with an "ELSE" clause, but apparently not..
1615 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1620 * Do the actual request
1622 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1623 rtinfo.rti_info[RTAX_DST] = dst;
1624 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1625 rtinfo.rti_info[RTAX_NETMASK] = netmask;
1626 rtinfo.rti_flags = flags | ifa->ifa_flags;
1627 rtinfo.rti_ifa = ifa;
1628 error = rtrequest1_global(cmd, &rtinfo, rtinit_rtrequest_callback, ifa);
1635 rtinit_rtrequest_callback(int cmd, int error,
1636 struct rt_addrinfo *rtinfo, struct rtentry *rt,
1639 struct ifaddr *ifa = arg;
1641 if (error == 0 && rt) {
1644 rt_newaddrmsg(cmd, ifa, error, rt);
1647 if (cmd == RTM_DELETE) {
1648 if (rt->rt_refcnt == 0) {
1657 struct netmsg_base base;
1659 struct rt_addrinfo *rtinfo;
1660 rtsearch_callback_func_t callback;
1662 boolean_t exact_match;
1667 rtsearch_global(int req, struct rt_addrinfo *rtinfo,
1668 rtsearch_callback_func_t callback, void *arg,
1669 boolean_t exact_match)
1671 struct netmsg_rts msg;
1673 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
1674 0, rtsearch_msghandler);
1676 msg.rtinfo = rtinfo;
1677 msg.callback = callback;
1679 msg.exact_match = exact_match;
1681 return lwkt_domsg(rtable_portfn(0), &msg.base.lmsg, 0);
1685 rtsearch_msghandler(netmsg_t msg)
1687 struct netmsg_rts *rmsg = (void *)msg;
1688 struct rt_addrinfo rtinfo;
1689 struct radix_node_head *rnh;
1694 * Copy the rtinfo. We need to make sure that the original
1695 * rtinfo, which is setup by the caller, in the netmsg will
1696 * _not_ be changed; else the next CPU on the netmsg forwarding
1697 * path will see a different rtinfo than what this CPU has seen.
1699 rtinfo = *rmsg->rtinfo;
1702 * Find the correct routing tree to use for this Address Family
1704 if ((rnh = rt_tables[mycpuid][rtinfo.rti_dst->sa_family]) == NULL) {
1706 panic("partially initialized routing tables\n");
1707 lwkt_replymsg(&rmsg->base.lmsg, EAFNOSUPPORT);
1712 * Correct rtinfo for the host route searching.
1714 if (rtinfo.rti_flags & RTF_HOST) {
1715 rtinfo.rti_netmask = NULL;
1716 rtinfo.rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
1719 rt = (struct rtentry *)
1720 rnh->rnh_lookup((char *)rtinfo.rti_dst,
1721 (char *)rtinfo.rti_netmask, rnh);
1724 * If we are asked to do the "exact match", we need to make sure
1725 * that host route searching got a host route while a network
1726 * route searching got a network route.
1728 if (rt != NULL && rmsg->exact_match &&
1729 ((rt->rt_flags ^ rtinfo.rti_flags) & RTF_HOST))
1734 * No matching routes have been found, don't count this
1735 * as a critical error (here, we set 'error' to 0), just
1736 * keep moving on, since at least prcloned routes are not
1737 * duplicated onto each CPU.
1744 error = rmsg->callback(rmsg->req, &rtinfo, rt, rmsg->arg,
1748 if (error == EJUSTRETURN) {
1749 lwkt_replymsg(&rmsg->base.lmsg, 0);
1754 nextcpu = mycpuid + 1;
1756 KKASSERT(rmsg->found_cnt > 0);
1759 * Under following cases, unrecoverable error has
1761 * o Request is RTM_GET
1762 * o The first time that we find the route, but the
1763 * modification fails.
1765 if (rmsg->req != RTM_GET && rmsg->found_cnt > 1) {
1766 panic("rtsearch_msghandler: unrecoverable error "
1769 lwkt_replymsg(&rmsg->base.lmsg, error);
1770 } else if (nextcpu < ncpus) {
1771 lwkt_forwardmsg(rtable_portfn(nextcpu), &rmsg->base.lmsg);
1773 if (rmsg->found_cnt == 0) {
1774 /* The requested route was never seen ... */
1777 lwkt_replymsg(&rmsg->base.lmsg, error);
1782 rtmask_add_global(struct sockaddr *mask)
1784 struct netmsg_base msg;
1786 netmsg_init(&msg, NULL, &curthread->td_msgport,
1787 0, rtmask_add_msghandler);
1788 msg.lmsg.u.ms_resultp = mask;
1790 return lwkt_domsg(rtable_portfn(0), &msg.lmsg, 0);
1794 _rtmask_lookup(struct sockaddr *mask, boolean_t search)
1796 struct radix_node *n;
1798 #define clen(s) (*(u_char *)(s))
1799 n = rn_addmask((char *)mask, search, 1, rn_cpumaskhead(mycpuid));
1801 mask->sa_len >= clen(n->rn_key) &&
1802 bcmp((char *)mask + 1,
1803 (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0) {
1804 return (struct sockaddr *)n->rn_key;
1812 rtmask_add_msghandler(netmsg_t msg)
1814 struct lwkt_msg *lmsg = &msg->lmsg;
1815 struct sockaddr *mask = lmsg->u.ms_resultp;
1816 int error = 0, nextcpu;
1818 if (rtmask_lookup(mask) == NULL)
1821 nextcpu = mycpuid + 1;
1822 if (!error && nextcpu < ncpus)
1823 lwkt_forwardmsg(rtable_portfn(nextcpu), lmsg);
1825 lwkt_replymsg(lmsg, error);
1828 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1829 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);