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(struct netmsg *netmsg);
118 static void rtrequest1_msghandler(struct netmsg *netmsg);
120 static void rtsearch_msghandler(struct netmsg *netmsg);
122 static void rtmask_add_msghandler(struct netmsg *netmsg);
124 static int rt_setshims(struct rtentry *, struct sockaddr **);
126 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW, 0, "Routing");
129 static int route_debug = 1;
130 SYSCTL_INT(_net_route, OID_AUTO, route_debug, CTLFLAG_RW,
131 &route_debug, 0, "");
134 int route_assert_owner_access = 0;
135 SYSCTL_INT(_net_route, OID_AUTO, assert_owner_access, CTLFLAG_RW,
136 &route_assert_owner_access, 0, "");
139 * Initialize the route table(s) for protocol domains and
140 * create a helper thread which will be responsible for updating
141 * route table entries on each cpu.
149 for (cpu = 0; cpu < ncpus; ++cpu)
150 bzero(&rtstatistics_percpu[cpu], sizeof(struct rtstatistics));
151 rn_init(); /* initialize all zeroes, all ones, mask table */
152 rtable_init(); /* call dom_rtattach() on each cpu */
154 for (cpu = 0; cpu < ncpus; cpu++) {
155 lwkt_create(rtable_service_loop, NULL, &rtd, NULL,
156 0, cpu, "rtable_cpu %d", cpu);
157 rt_ports[cpu] = &rtd->td_msgport;
162 rtable_init_oncpu(struct netmsg *nmsg)
167 SLIST_FOREACH(dom, &domains, dom_next) {
168 if (dom->dom_rtattach) {
170 (void **)&rt_tables[cpu][dom->dom_family],
174 ifnet_forwardmsg(&nmsg->nm_lmsg, cpu + 1);
182 netmsg_init(&nmsg, &curthread->td_msgport, 0, rtable_init_oncpu);
183 ifnet_domsg(&nmsg.nm_lmsg, 0);
187 * Our per-cpu table management protocol thread. All route table operations
188 * are sequentially chained through all cpus starting at cpu #0 in order to
189 * maintain duplicate route tables on each cpu. Having a spearate route
190 * table management thread allows the protocol and interrupt threads to
191 * issue route table changes.
194 rtable_service_loop(void *dummy __unused)
196 struct netmsg *netmsg;
197 thread_t td = curthread;
199 while ((netmsg = lwkt_waitport(&td->td_msgport, 0)) != NULL) {
200 netmsg->nm_dispatch(netmsg);
205 * Routing statistics.
209 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS)
213 for (cpu = 0; cpu < ncpus; ++cpu) {
214 if ((error = SYSCTL_OUT(req, &rtstatistics_percpu[cpu],
215 sizeof(struct rtstatistics))))
217 if ((error = SYSCTL_IN(req, &rtstatistics_percpu[cpu],
218 sizeof(struct rtstatistics))))
224 SYSCTL_PROC(_net_route, OID_AUTO, stats, (CTLTYPE_OPAQUE|CTLFLAG_RW),
225 0, 0, sysctl_rtstatistics, "S,rtstatistics", "Routing statistics");
227 SYSCTL_STRUCT(_net_route, OID_AUTO, stats, CTLFLAG_RW, &rtstat, rtstatistics,
228 "Routing statistics");
232 * Packet routing routines.
236 * Look up and fill in the "ro_rt" rtentry field in a route structure given
237 * an address in the "ro_dst" field. Always send a report on a miss and
238 * always clone routes.
241 rtalloc(struct route *ro)
243 rtalloc_ign(ro, 0UL);
247 * Look up and fill in the "ro_rt" rtentry field in a route structure given
248 * an address in the "ro_dst" field. Always send a report on a miss and
249 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
253 rtalloc_ign(struct route *ro, u_long ignoreflags)
255 if (ro->ro_rt != NULL) {
256 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP)
261 ro->ro_rt = _rtlookup(&ro->ro_dst, RTL_REPORTMSG, ignoreflags);
265 * Look up the route that matches the given "dst" address.
267 * Route lookup can have the side-effect of creating and returning
268 * a cloned route instead when "dst" matches a cloning route and the
269 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
271 * Any route returned has its reference count incremented.
274 _rtlookup(struct sockaddr *dst, boolean_t generate_report, u_long ignore)
276 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
283 * Look up route in the radix tree.
285 rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh);
290 * Handle cloning routes.
292 if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) {
293 struct rtentry *clonedroute;
296 clonedroute = rt; /* copy in/copy out parameter */
297 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
298 &clonedroute); /* clone the route */
299 if (error != 0) { /* cloning failed */
301 rt_dstmsg(RTM_MISS, dst, error);
303 return (rt); /* return the uncloned route */
305 if (generate_report) {
306 if (clonedroute->rt_flags & RTF_XRESOLVE)
307 rt_dstmsg(RTM_RESOLVE, dst, 0);
309 rt_rtmsg(RTM_ADD, clonedroute,
310 clonedroute->rt_ifp, 0);
312 return (clonedroute); /* return cloned route */
316 * Increment the reference count of the matched route and return.
322 rtstat.rts_unreach++;
324 rt_dstmsg(RTM_MISS, dst, 0);
329 rtfree(struct rtentry *rt)
331 if (rt->rt_cpuid == mycpuid)
334 rtfree_remote(rt, 1);
338 rtfree_oncpu(struct rtentry *rt)
340 KKASSERT(rt->rt_cpuid == mycpuid);
341 KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt));
344 if (rt->rt_refcnt == 0) {
345 struct radix_node_head *rnh =
346 rt_tables[mycpuid][rt_key(rt)->sa_family];
349 rnh->rnh_close((struct radix_node *)rt, rnh);
350 if (!(rt->rt_flags & RTF_UP)) {
351 /* deallocate route */
352 if (rt->rt_ifa != NULL)
354 if (rt->rt_parent != NULL)
355 RTFREE(rt->rt_parent); /* recursive call! */
363 rtfree_remote_dispatch(struct netmsg *nmsg)
365 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
366 struct rtentry *rt = lmsg->u.ms_resultp;
369 lwkt_replymsg(lmsg, 0);
373 rtfree_remote(struct rtentry *rt, int allow_panic)
376 struct lwkt_msg *lmsg;
378 KKASSERT(rt->rt_cpuid != mycpuid);
380 if (route_assert_owner_access && allow_panic) {
381 panic("rt remote free rt_cpuid %d, mycpuid %d\n",
382 rt->rt_cpuid, mycpuid);
384 kprintf("rt remote free rt_cpuid %d, mycpuid %d\n",
385 rt->rt_cpuid, mycpuid);
389 netmsg_init(&nmsg, &curthread->td_msgport, 0, rtfree_remote_dispatch);
390 lmsg = &nmsg.nm_lmsg;
391 lmsg->u.ms_resultp = rt;
393 lwkt_domsg(rtable_portfn(rt->rt_cpuid), lmsg, 0);
397 rtredirect_oncpu(struct sockaddr *dst, struct sockaddr *gateway,
398 struct sockaddr *netmask, int flags, struct sockaddr *src)
400 struct rtentry *rt = NULL;
401 struct rt_addrinfo rtinfo;
406 /* verify the gateway is directly reachable */
407 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
413 * If the redirect isn't from our current router for this destination,
414 * it's either old or wrong.
416 if (!(flags & RTF_DONE) && /* XXX JH */
417 (rt = rtpurelookup(dst)) != NULL &&
418 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) {
424 * If it redirects us to ourselves, we have a routing loop,
425 * perhaps as a result of an interface going down recently.
427 if (ifa_ifwithaddr(gateway)) {
428 error = EHOSTUNREACH;
433 * Create a new entry if the lookup failed or if we got back
434 * a wildcard entry for the default route. This is necessary
435 * for hosts which use routing redirects generated by smart
436 * gateways to dynamically build the routing tables.
440 if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) {
445 /* Ignore redirects for directly connected hosts. */
446 if (!(rt->rt_flags & RTF_GATEWAY)) {
447 error = EHOSTUNREACH;
451 if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) {
453 * Changing from a network route to a host route.
454 * Create a new host route rather than smashing the
458 flags |= RTF_GATEWAY | RTF_DYNAMIC;
459 bzero(&rtinfo, sizeof(struct rt_addrinfo));
460 rtinfo.rti_info[RTAX_DST] = dst;
461 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
462 rtinfo.rti_info[RTAX_NETMASK] = netmask;
463 rtinfo.rti_flags = flags;
464 rtinfo.rti_ifa = ifa;
465 rt = NULL; /* copy-in/copy-out parameter */
466 error = rtrequest1(RTM_ADD, &rtinfo, &rt);
468 flags = rt->rt_flags;
469 stat = &rtstat.rts_dynamic;
472 * Smash the current notion of the gateway to this destination.
473 * Should check about netmask!!!
475 rt->rt_flags |= RTF_MODIFIED;
476 flags |= RTF_MODIFIED;
478 /* We only need to report rtmsg on CPU0 */
479 rt_setgate(rt, rt_key(rt), gateway,
480 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT);
482 stat = &rtstat.rts_newgateway;
490 rtstat.rts_badredirect++;
491 else if (stat != NULL)
499 struct netmsg_rtredirect {
500 struct netmsg netmsg;
501 struct sockaddr *dst;
502 struct sockaddr *gateway;
503 struct sockaddr *netmask;
505 struct sockaddr *src;
511 * Force a routing table entry to the specified
512 * destination to go through the given gateway.
513 * Normally called as a result of a routing redirect
514 * message from the network layer.
516 * N.B.: must be called at splnet
519 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
520 struct sockaddr *netmask, int flags, struct sockaddr *src)
522 struct rt_addrinfo rtinfo;
525 struct netmsg_rtredirect msg;
527 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
528 rtredirect_msghandler);
530 msg.gateway = gateway;
531 msg.netmask = netmask;
534 error = lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0);
536 error = rtredirect_oncpu(dst, gateway, netmask, flags, src);
538 bzero(&rtinfo, sizeof(struct rt_addrinfo));
539 rtinfo.rti_info[RTAX_DST] = dst;
540 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
541 rtinfo.rti_info[RTAX_NETMASK] = netmask;
542 rtinfo.rti_info[RTAX_AUTHOR] = src;
543 rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error);
549 rtredirect_msghandler(struct netmsg *netmsg)
551 struct netmsg_rtredirect *msg = (void *)netmsg;
554 rtredirect_oncpu(msg->dst, msg->gateway, msg->netmask,
555 msg->flags, msg->src);
556 nextcpu = mycpuid + 1;
558 lwkt_forwardmsg(rtable_portfn(nextcpu), &netmsg->nm_lmsg);
560 lwkt_replymsg(&netmsg->nm_lmsg, 0);
566 * Routing table ioctl interface.
569 rtioctl(u_long req, caddr_t data, struct ucred *cred)
572 /* Multicast goop, grrr... */
573 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
580 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
584 if (!(flags & RTF_GATEWAY)) {
586 * If we are adding a route to an interface,
587 * and the interface is a point-to-point link,
588 * we should search for the destination
589 * as our clue to the interface. Otherwise
590 * we can use the local address.
593 if (flags & RTF_HOST) {
594 ifa = ifa_ifwithdstaddr(dst);
597 ifa = ifa_ifwithaddr(gateway);
600 * If we are adding a route to a remote net
601 * or host, the gateway may still be on the
602 * other end of a pt to pt link.
604 ifa = ifa_ifwithdstaddr(gateway);
607 ifa = ifa_ifwithnet(gateway);
611 rt = rtpurelookup(gateway);
615 if ((ifa = rt->rt_ifa) == NULL)
618 if (ifa->ifa_addr->sa_family != dst->sa_family) {
619 struct ifaddr *oldifa = ifa;
621 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
628 static int rt_fixdelete (struct radix_node *, void *);
629 static int rt_fixchange (struct radix_node *, void *);
633 struct radix_node_head *rnh;
637 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
640 rt_getifa(struct rt_addrinfo *rtinfo)
642 struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY];
643 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
644 struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA];
645 int flags = rtinfo->rti_flags;
648 * ifp may be specified by sockaddr_dl
649 * when protocol address is ambiguous.
651 if (rtinfo->rti_ifp == NULL) {
652 struct sockaddr *ifpaddr;
654 ifpaddr = rtinfo->rti_info[RTAX_IFP];
655 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
658 ifa = ifa_ifwithnet(ifpaddr);
660 rtinfo->rti_ifp = ifa->ifa_ifp;
664 if (rtinfo->rti_ifa == NULL && ifaaddr != NULL)
665 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr);
666 if (rtinfo->rti_ifa == NULL) {
669 sa = ifaaddr != NULL ? ifaaddr :
670 (gateway != NULL ? gateway : dst);
671 if (sa != NULL && rtinfo->rti_ifp != NULL)
672 rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp);
673 else if (dst != NULL && gateway != NULL)
674 rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
676 rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa);
678 if (rtinfo->rti_ifa == NULL)
679 return (ENETUNREACH);
681 if (rtinfo->rti_ifp == NULL)
682 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp;
687 * Do appropriate manipulations of a routing tree given
688 * all the bits of info needed
693 struct sockaddr *dst,
694 struct sockaddr *gateway,
695 struct sockaddr *netmask,
697 struct rtentry **ret_nrt)
699 struct rt_addrinfo rtinfo;
701 bzero(&rtinfo, sizeof(struct rt_addrinfo));
702 rtinfo.rti_info[RTAX_DST] = dst;
703 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
704 rtinfo.rti_info[RTAX_NETMASK] = netmask;
705 rtinfo.rti_flags = flags;
706 return rtrequest1(req, &rtinfo, ret_nrt);
712 struct sockaddr *dst,
713 struct sockaddr *gateway,
714 struct sockaddr *netmask,
717 struct rt_addrinfo rtinfo;
719 bzero(&rtinfo, sizeof(struct rt_addrinfo));
720 rtinfo.rti_info[RTAX_DST] = dst;
721 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
722 rtinfo.rti_info[RTAX_NETMASK] = netmask;
723 rtinfo.rti_flags = flags;
724 return rtrequest1_global(req, &rtinfo, NULL, NULL);
730 struct netmsg netmsg;
732 struct rt_addrinfo *rtinfo;
733 rtrequest1_callback_func_t callback;
740 rtrequest1_global(int req, struct rt_addrinfo *rtinfo,
741 rtrequest1_callback_func_t callback, void *arg)
745 struct netmsg_rtq msg;
747 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
748 rtrequest1_msghandler);
749 msg.netmsg.nm_lmsg.ms_error = -1;
752 msg.callback = callback;
754 error = lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0);
756 struct rtentry *rt = NULL;
758 error = rtrequest1(req, rtinfo, &rt);
762 callback(req, error, rtinfo, rt, arg);
768 * Handle a route table request on the current cpu. Since the route table's
769 * are supposed to be identical on each cpu, an error occuring later in the
770 * message chain is considered system-fatal.
775 rtrequest1_msghandler(struct netmsg *netmsg)
777 struct netmsg_rtq *msg = (void *)netmsg;
778 struct rt_addrinfo rtinfo;
779 struct rtentry *rt = NULL;
784 * Copy the rtinfo. We need to make sure that the original
785 * rtinfo, which is setup by the caller, in the netmsg will
786 * _not_ be changed; else the next CPU on the netmsg forwarding
787 * path will see a different rtinfo than what this CPU has seen.
789 rtinfo = *msg->rtinfo;
791 error = rtrequest1(msg->req, &rtinfo, &rt);
795 msg->callback(msg->req, error, &rtinfo, rt, msg->arg);
798 * RTM_DELETE's are propogated even if an error occurs, since a
799 * cloned route might be undergoing deletion and cloned routes
800 * are not necessarily replicated. An overall error is returned
801 * only if no cpus have the route in question.
803 if (msg->netmsg.nm_lmsg.ms_error < 0 || error == 0)
804 msg->netmsg.nm_lmsg.ms_error = error;
806 nextcpu = mycpuid + 1;
807 if (error && msg->req != RTM_DELETE) {
809 panic("rtrequest1_msghandler: rtrequest table "
810 "error was not on cpu #0");
812 lwkt_replymsg(&msg->netmsg.nm_lmsg, error);
813 } else if (nextcpu < ncpus) {
814 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg);
816 lwkt_replymsg(&msg->netmsg.nm_lmsg,
817 msg->netmsg.nm_lmsg.ms_error);
824 rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt)
826 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
828 struct radix_node *rn;
829 struct radix_node_head *rnh;
831 struct sockaddr *ndst;
835 #define gotoerr(x) { error = x ; goto bad; }
839 rt_addrinfo_print(req, rtinfo);
844 * Find the correct routing tree to use for this Address Family
846 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL)
847 gotoerr(EAFNOSUPPORT);
850 * If we are adding a host route then we don't want to put
851 * a netmask in the tree, nor do we want to clone it.
853 if (rtinfo->rti_flags & RTF_HOST) {
854 rtinfo->rti_info[RTAX_NETMASK] = NULL;
855 rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
860 /* Remove the item from the tree. */
861 rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST],
862 (char *)rtinfo->rti_info[RTAX_NETMASK],
866 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)),
867 ("rnh_deladdr returned flags 0x%x", rn->rn_flags));
868 rt = (struct rtentry *)rn;
870 /* ref to prevent a deletion race */
873 /* Free any routes cloned from this one. */
874 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
875 rt_mask(rt) != NULL) {
876 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
881 if (rt->rt_gwroute != NULL) {
882 RTFREE(rt->rt_gwroute);
883 rt->rt_gwroute = NULL;
887 * NB: RTF_UP must be set during the search above,
888 * because we might delete the last ref, causing
889 * rt to get freed prematurely.
891 rt->rt_flags &= ~RTF_UP;
895 rt_print(rtinfo, rt);
898 /* Give the protocol a chance to keep things in sync. */
899 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
900 ifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
903 * If the caller wants it, then it can have it,
904 * but it's up to it to free the rtentry as we won't be
907 KASSERT(rt->rt_refcnt >= 0,
908 ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt));
909 if (ret_nrt != NULL) {
910 /* leave ref intact for return */
913 /* deref / attempt to destroy */
919 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
923 rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
924 rtinfo->rti_flags |= RTF_WASCLONED;
925 rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
926 if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL)
927 rtinfo->rti_flags |= RTF_HOST;
928 rtinfo->rti_info[RTAX_MPLS1] = rt->rt_shim[0];
929 rtinfo->rti_info[RTAX_MPLS2] = rt->rt_shim[1];
930 rtinfo->rti_info[RTAX_MPLS3] = rt->rt_shim[2];
934 KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) ||
935 rtinfo->rti_info[RTAX_GATEWAY] != NULL,
936 ("rtrequest: GATEWAY but no gateway"));
938 if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo)))
940 ifa = rtinfo->rti_ifa;
942 R_Malloc(rt, struct rtentry *, sizeof(struct rtentry));
945 bzero(rt, sizeof(struct rtentry));
946 rt->rt_flags = RTF_UP | rtinfo->rti_flags;
947 rt->rt_cpuid = mycpuid;
949 if (mycpuid != 0 && req == RTM_ADD) {
950 /* For RTM_ADD, we have already sent rtmsg on CPU0. */
951 reportmsg = RTL_DONTREPORT;
954 * For RTM_ADD, we only send rtmsg on CPU0.
955 * For RTM_RESOLVE, we always send rtmsg. XXX
957 reportmsg = RTL_REPORTMSG;
959 error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY],
967 if (rtinfo->rti_info[RTAX_NETMASK] != NULL)
968 rt_maskedcopy(dst, ndst,
969 rtinfo->rti_info[RTAX_NETMASK]);
971 bcopy(dst, ndst, dst->sa_len);
973 if (rtinfo->rti_info[RTAX_MPLS1] != NULL)
974 rt_setshims(rt, rtinfo->rti_info);
977 * Note that we now have a reference to the ifa.
978 * This moved from below so that rnh->rnh_addaddr() can
979 * examine the ifa and ifa->ifa_ifp if it so desires.
983 rt->rt_ifp = ifa->ifa_ifp;
984 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
986 rn = rnh->rnh_addaddr((char *)ndst,
987 (char *)rtinfo->rti_info[RTAX_NETMASK],
990 struct rtentry *oldrt;
993 * We already have one of these in the tree.
994 * We do a special hack: if the old route was
995 * cloned, then we blow it away and try
996 * re-inserting the new one.
998 oldrt = rtpurelookup(ndst);
1001 if (oldrt->rt_flags & RTF_WASCLONED) {
1002 rtrequest(RTM_DELETE, rt_key(oldrt),
1005 oldrt->rt_flags, NULL);
1006 rn = rnh->rnh_addaddr((char *)ndst,
1008 rtinfo->rti_info[RTAX_NETMASK],
1015 * If it still failed to go into the tree,
1016 * then un-make it (this should be a function).
1019 if (rt->rt_gwroute != NULL)
1020 rtfree(rt->rt_gwroute);
1028 * If we got here from RESOLVE, then we are cloning
1029 * so clone the rest, and note that we
1030 * are a clone (and increment the parent's references)
1032 if (req == RTM_RESOLVE) {
1033 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
1034 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
1035 if ((*ret_nrt)->rt_flags &
1036 (RTF_CLONING | RTF_PRCLONING)) {
1037 rt->rt_parent = *ret_nrt;
1038 (*ret_nrt)->rt_refcnt++;
1043 * if this protocol has something to add to this then
1044 * allow it to do that as well.
1046 if (ifa->ifa_rtrequest != NULL)
1047 ifa->ifa_rtrequest(req, rt, rtinfo);
1050 * We repeat the same procedure from rt_setgate() here because
1051 * it doesn't fire when we call it there because the node
1052 * hasn't been added to the tree yet.
1054 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) &&
1055 rt_mask(rt) != NULL) {
1056 struct rtfc_arg arg = { rt, rnh };
1058 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1059 (char *)rt_mask(rt),
1060 rt_fixchange, &arg);
1065 rt_print(rtinfo, rt);
1068 * Return the resulting rtentry,
1069 * increasing the number of references by one.
1071 if (ret_nrt != NULL) {
1083 kprintf("rti %p failed error %d\n", rtinfo, error);
1085 kprintf("rti %p succeeded\n", rtinfo);
1093 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1094 * (i.e., the routes related to it by the operation of cloning). This
1095 * routine is iterated over all potential former-child-routes by way of
1096 * rnh->rnh_walktree_from() above, and those that actually are children of
1097 * the late parent (passed in as VP here) are themselves deleted.
1100 rt_fixdelete(struct radix_node *rn, void *vp)
1102 struct rtentry *rt = (struct rtentry *)rn;
1103 struct rtentry *rt0 = vp;
1105 if (rt->rt_parent == rt0 &&
1106 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1107 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1108 rt->rt_flags, NULL);
1114 * This routine is called from rt_setgate() to do the analogous thing for
1115 * adds and changes. There is the added complication in this case of a
1116 * middle insert; i.e., insertion of a new network route between an older
1117 * network route and (cloned) host routes. For this reason, a simple check
1118 * of rt->rt_parent is insufficient; each candidate route must be tested
1119 * against the (mask, value) of the new route (passed as before in vp)
1120 * to see if the new route matches it.
1122 * XXX - it may be possible to do fixdelete() for changes and reserve this
1123 * routine just for adds. I'm not sure why I thought it was necessary to do
1127 static int rtfcdebug = 0;
1131 rt_fixchange(struct radix_node *rn, void *vp)
1133 struct rtentry *rt = (struct rtentry *)rn;
1134 struct rtfc_arg *ap = vp;
1135 struct rtentry *rt0 = ap->rt0;
1136 struct radix_node_head *rnh = ap->rnh;
1137 u_char *xk1, *xm1, *xk2, *xmp;
1142 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
1145 if (rt->rt_parent == NULL ||
1146 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1148 if (rtfcdebug) kprintf("no parent, pinned or cloning\n");
1153 if (rt->rt_parent == rt0) {
1155 if (rtfcdebug) kprintf("parent match\n");
1157 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1158 rt->rt_flags, NULL);
1162 * There probably is a function somewhere which does this...
1163 * if not, there should be.
1165 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1167 xk1 = (u_char *)rt_key(rt0);
1168 xm1 = (u_char *)rt_mask(rt0);
1169 xk2 = (u_char *)rt_key(rt);
1171 /* avoid applying a less specific route */
1172 xmp = (u_char *)rt_mask(rt->rt_parent);
1173 mlen = rt_key(rt->rt_parent)->sa_len;
1174 if (mlen > rt_key(rt0)->sa_len) {
1177 kprintf("rt_fixchange: inserting a less "
1178 "specific route\n");
1182 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
1183 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
1186 kprintf("rt_fixchange: inserting a less "
1187 "specific route\n");
1193 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
1194 if ((xk2[i] & xm1[i]) != xk1[i]) {
1196 if (rtfcdebug) kprintf("no match\n");
1203 * OK, this node is a clone, and matches the node currently being
1204 * changed/added under the node's mask. So, get rid of it.
1207 if (rtfcdebug) kprintf("deleting\n");
1209 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1210 rt->rt_flags, NULL);
1213 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1216 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate,
1217 boolean_t generate_report)
1219 char *space, *oldspace;
1220 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
1221 struct rtentry *rt = rt0;
1222 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
1225 * A host route with the destination equal to the gateway
1226 * will interfere with keeping LLINFO in the routing
1227 * table, so disallow it.
1229 if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) ==
1230 (RTF_HOST | RTF_GATEWAY)) &&
1231 dst->sa_len == gate->sa_len &&
1232 sa_equal(dst, gate)) {
1234 * The route might already exist if this is an RTM_CHANGE
1235 * or a routing redirect, so try to delete it.
1237 if (rt_key(rt0) != NULL)
1238 rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway,
1239 rt_mask(rt0), rt0->rt_flags, NULL);
1240 return EADDRNOTAVAIL;
1244 * Both dst and gateway are stored in the same malloc'ed chunk
1245 * (If I ever get my hands on....)
1246 * if we need to malloc a new chunk, then keep the old one around
1247 * till we don't need it any more.
1249 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
1250 oldspace = (char *)rt_key(rt);
1251 R_Malloc(space, char *, dlen + glen);
1254 rt->rt_nodes->rn_key = space;
1256 space = (char *)rt_key(rt); /* Just use the old space. */
1260 /* Set the gateway value. */
1261 rt->rt_gateway = (struct sockaddr *)(space + dlen);
1262 bcopy(gate, rt->rt_gateway, glen);
1264 if (oldspace != NULL) {
1266 * If we allocated a new chunk, preserve the original dst.
1267 * This way, rt_setgate() really just sets the gate
1268 * and leaves the dst field alone.
1270 bcopy(dst, space, dlen);
1275 * If there is already a gwroute, it's now almost definitely wrong
1278 if (rt->rt_gwroute != NULL) {
1279 RTFREE(rt->rt_gwroute);
1280 rt->rt_gwroute = NULL;
1282 if (rt->rt_flags & RTF_GATEWAY) {
1284 * Cloning loop avoidance: In the presence of
1285 * protocol-cloning and bad configuration, it is
1286 * possible to get stuck in bottomless mutual recursion
1287 * (rtrequest rt_setgate rtlookup). We avoid this
1288 * by not allowing protocol-cloning to operate for
1289 * gateways (which is probably the correct choice
1290 * anyway), and avoid the resulting reference loops
1291 * by disallowing any route to run through itself as
1292 * a gateway. This is obviously mandatory when we
1293 * get rt->rt_output().
1295 * This breaks TTCP for hosts outside the gateway! XXX JH
1297 rt->rt_gwroute = _rtlookup(gate, generate_report,
1299 if (rt->rt_gwroute == rt) {
1300 rt->rt_gwroute = NULL;
1302 return EDQUOT; /* failure */
1307 * This isn't going to do anything useful for host routes, so
1308 * don't bother. Also make sure we have a reasonable mask
1309 * (we don't yet have one during adds).
1311 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1312 struct rtfc_arg arg = { rt, rnh };
1314 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1315 (char *)rt_mask(rt),
1316 rt_fixchange, &arg);
1324 struct sockaddr *src,
1325 struct sockaddr *dst,
1326 struct sockaddr *netmask)
1328 u_char *cp1 = (u_char *)src;
1329 u_char *cp2 = (u_char *)dst;
1330 u_char *cp3 = (u_char *)netmask;
1331 u_char *cplim = cp2 + *cp3;
1332 u_char *cplim2 = cp2 + *cp1;
1334 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1339 *cp2++ = *cp1++ & *cp3++;
1341 bzero(cp2, cplim2 - cp2);
1345 rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt)
1347 struct rtentry *up_rt, *rt;
1349 if (!(rt0->rt_flags & RTF_UP)) {
1350 up_rt = rtlookup(dst);
1352 return (EHOSTUNREACH);
1356 if (up_rt->rt_flags & RTF_GATEWAY) {
1357 if (up_rt->rt_gwroute == NULL) {
1358 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1359 if (up_rt->rt_gwroute == NULL)
1360 return (EHOSTUNREACH);
1361 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) {
1362 rtfree(up_rt->rt_gwroute);
1363 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1364 if (up_rt->rt_gwroute == NULL)
1365 return (EHOSTUNREACH);
1367 rt = up_rt->rt_gwroute;
1370 if (rt->rt_flags & RTF_REJECT &&
1371 (rt->rt_rmx.rmx_expire == 0 || /* rt doesn't expire */
1372 time_second < rt->rt_rmx.rmx_expire)) /* rt not expired */
1373 return (rt->rt_flags & RTF_HOST ? EHOSTDOWN : EHOSTUNREACH);
1379 rt_setshims(struct rtentry *rt, struct sockaddr **rt_shim){
1382 for (i=0; i<3; i++) {
1383 struct sockaddr *shim = rt_shim[RTAX_MPLS1 + i];
1389 shimlen = ROUNDUP(shim->sa_len);
1390 R_Malloc(rt->rt_shim[i], struct sockaddr *, shimlen);
1391 bcopy(shim, rt->rt_shim[i], shimlen);
1400 * Print out a route table entry
1403 rt_print(struct rt_addrinfo *rtinfo, struct rtentry *rn)
1405 kprintf("rti %p cpu %d route %p flags %08lx: ",
1406 rtinfo, mycpuid, rn, rn->rt_flags);
1407 sockaddr_print(rt_key(rn));
1409 sockaddr_print(rt_mask(rn));
1411 sockaddr_print(rn->rt_gateway);
1412 kprintf(" ifc \"%s\"", rn->rt_ifp ? rn->rt_ifp->if_dname : "?");
1413 kprintf(" ifa %p\n", rn->rt_ifa);
1417 rt_addrinfo_print(int cmd, struct rt_addrinfo *rti)
1423 if (cmd == RTM_DELETE && route_debug > 1)
1438 kprintf("C%02d ", cmd);
1441 kprintf("rti %p cpu %d ", rti, mycpuid);
1442 for (i = 0; i < rti->rti_addrs; ++i) {
1443 if (rti->rti_info[i] == NULL)
1473 kprintf("(?%02d ", i);
1476 sockaddr_print(rti->rti_info[i]);
1484 sockaddr_print(struct sockaddr *sa)
1486 struct sockaddr_in *sa4;
1487 struct sockaddr_in6 *sa6;
1496 len = sa->sa_len - offsetof(struct sockaddr, sa_data[0]);
1498 switch(sa->sa_family) {
1502 switch(sa->sa_family) {
1504 sa4 = (struct sockaddr_in *)sa;
1505 kprintf("INET %d %d.%d.%d.%d",
1506 ntohs(sa4->sin_port),
1507 (ntohl(sa4->sin_addr.s_addr) >> 24) & 255,
1508 (ntohl(sa4->sin_addr.s_addr) >> 16) & 255,
1509 (ntohl(sa4->sin_addr.s_addr) >> 8) & 255,
1510 (ntohl(sa4->sin_addr.s_addr) >> 0) & 255
1514 sa6 = (struct sockaddr_in6 *)sa;
1515 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
1516 ntohs(sa6->sin6_port),
1517 sa6->sin6_addr.s6_addr16[0],
1518 sa6->sin6_addr.s6_addr16[1],
1519 sa6->sin6_addr.s6_addr16[2],
1520 sa6->sin6_addr.s6_addr16[3],
1521 sa6->sin6_addr.s6_addr16[4],
1522 sa6->sin6_addr.s6_addr16[5],
1523 sa6->sin6_addr.s6_addr16[6],
1524 sa6->sin6_addr.s6_addr16[7]
1528 kprintf("AF%d ", sa->sa_family);
1529 while (len > 0 && sa->sa_data[len-1] == 0)
1532 for (i = 0; i < len; ++i) {
1535 kprintf("%d", (unsigned char)sa->sa_data[i]);
1545 * Set up a routing table entry, normally for an interface.
1548 rtinit(struct ifaddr *ifa, int cmd, int flags)
1550 struct sockaddr *dst, *deldst, *netmask;
1551 struct mbuf *m = NULL;
1552 struct radix_node_head *rnh;
1553 struct radix_node *rn;
1554 struct rt_addrinfo rtinfo;
1557 if (flags & RTF_HOST) {
1558 dst = ifa->ifa_dstaddr;
1561 dst = ifa->ifa_addr;
1562 netmask = ifa->ifa_netmask;
1565 * If it's a delete, check that if it exists, it's on the correct
1566 * interface or we might scrub a route to another ifa which would
1567 * be confusing at best and possibly worse.
1569 if (cmd == RTM_DELETE) {
1571 * It's a delete, so it should already exist..
1572 * If it's a net, mask off the host bits
1573 * (Assuming we have a mask)
1575 if (netmask != NULL) {
1576 m = m_get(MB_DONTWAIT, MT_SONAME);
1580 deldst = mtod(m, struct sockaddr *);
1581 rt_maskedcopy(dst, deldst, netmask);
1585 * Look up an rtentry that is in the routing tree and
1586 * contains the correct info.
1588 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL ||
1589 (rn = rnh->rnh_lookup((char *)dst,
1590 (char *)netmask, rnh)) == NULL ||
1591 ((struct rtentry *)rn)->rt_ifa != ifa ||
1592 !sa_equal((struct sockaddr *)rn->rn_key, dst)) {
1595 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1601 * One would think that as we are deleting, and we know
1602 * it doesn't exist, we could just return at this point
1603 * with an "ELSE" clause, but apparently not..
1605 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1610 * Do the actual request
1612 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1613 rtinfo.rti_info[RTAX_DST] = dst;
1614 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1615 rtinfo.rti_info[RTAX_NETMASK] = netmask;
1616 rtinfo.rti_flags = flags | ifa->ifa_flags;
1617 rtinfo.rti_ifa = ifa;
1618 error = rtrequest1_global(cmd, &rtinfo, rtinit_rtrequest_callback, ifa);
1625 rtinit_rtrequest_callback(int cmd, int error,
1626 struct rt_addrinfo *rtinfo, struct rtentry *rt,
1629 struct ifaddr *ifa = arg;
1631 if (error == 0 && rt) {
1634 rt_newaddrmsg(cmd, ifa, error, rt);
1637 if (cmd == RTM_DELETE) {
1638 if (rt->rt_refcnt == 0) {
1647 struct netmsg netmsg;
1649 struct rt_addrinfo *rtinfo;
1650 rtsearch_callback_func_t callback;
1652 boolean_t exact_match;
1657 rtsearch_global(int req, struct rt_addrinfo *rtinfo,
1658 rtsearch_callback_func_t callback, void *arg,
1659 boolean_t exact_match)
1661 struct netmsg_rts msg;
1663 netmsg_init(&msg.netmsg, &curthread->td_msgport, 0,
1664 rtsearch_msghandler);
1666 msg.rtinfo = rtinfo;
1667 msg.callback = callback;
1669 msg.exact_match = exact_match;
1671 return lwkt_domsg(rtable_portfn(0), &msg.netmsg.nm_lmsg, 0);
1675 rtsearch_msghandler(struct netmsg *netmsg)
1677 struct netmsg_rts *msg = (void *)netmsg;
1678 struct rt_addrinfo rtinfo;
1679 struct radix_node_head *rnh;
1684 * Copy the rtinfo. We need to make sure that the original
1685 * rtinfo, which is setup by the caller, in the netmsg will
1686 * _not_ be changed; else the next CPU on the netmsg forwarding
1687 * path will see a different rtinfo than what this CPU has seen.
1689 rtinfo = *msg->rtinfo;
1692 * Find the correct routing tree to use for this Address Family
1694 if ((rnh = rt_tables[mycpuid][rtinfo.rti_dst->sa_family]) == NULL) {
1696 panic("partially initialized routing tables\n");
1697 lwkt_replymsg(&msg->netmsg.nm_lmsg, EAFNOSUPPORT);
1702 * Correct rtinfo for the host route searching.
1704 if (rtinfo.rti_flags & RTF_HOST) {
1705 rtinfo.rti_netmask = NULL;
1706 rtinfo.rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
1709 rt = (struct rtentry *)
1710 rnh->rnh_lookup((char *)rtinfo.rti_dst,
1711 (char *)rtinfo.rti_netmask, rnh);
1714 * If we are asked to do the "exact match", we need to make sure
1715 * that host route searching got a host route while a network
1716 * route searching got a network route.
1718 if (rt != NULL && msg->exact_match &&
1719 ((rt->rt_flags ^ rtinfo.rti_flags) & RTF_HOST))
1724 * No matching routes have been found, don't count this
1725 * as a critical error (here, we set 'error' to 0), just
1726 * keep moving on, since at least prcloned routes are not
1727 * duplicated onto each CPU.
1734 error = msg->callback(msg->req, &rtinfo, rt, msg->arg,
1738 if (error == EJUSTRETURN) {
1739 lwkt_replymsg(&msg->netmsg.nm_lmsg, 0);
1744 nextcpu = mycpuid + 1;
1746 KKASSERT(msg->found_cnt > 0);
1749 * Under following cases, unrecoverable error has
1751 * o Request is RTM_GET
1752 * o The first time that we find the route, but the
1753 * modification fails.
1755 if (msg->req != RTM_GET && msg->found_cnt > 1) {
1756 panic("rtsearch_msghandler: unrecoverable error "
1759 lwkt_replymsg(&msg->netmsg.nm_lmsg, error);
1760 } else if (nextcpu < ncpus) {
1761 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->netmsg.nm_lmsg);
1763 if (msg->found_cnt == 0) {
1764 /* The requested route was never seen ... */
1767 lwkt_replymsg(&msg->netmsg.nm_lmsg, error);
1772 rtmask_add_global(struct sockaddr *mask)
1776 netmsg_init(&nmsg, &curthread->td_msgport, 0,
1777 rtmask_add_msghandler);
1778 nmsg.nm_lmsg.u.ms_resultp = mask;
1780 return lwkt_domsg(rtable_portfn(0), &nmsg.nm_lmsg, 0);
1784 _rtmask_lookup(struct sockaddr *mask, boolean_t search)
1786 struct radix_node *n;
1788 #define clen(s) (*(u_char *)(s))
1789 n = rn_addmask((char *)mask, search, 1);
1791 mask->sa_len >= clen(n->rn_key) &&
1792 bcmp((char *)mask + 1,
1793 (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0) {
1794 return (struct sockaddr *)n->rn_key;
1802 rtmask_add_msghandler(struct netmsg *nmsg)
1804 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
1805 struct sockaddr *mask = lmsg->u.ms_resultp;
1806 int error = 0, nextcpu;
1808 if (rtmask_lookup(mask) == NULL)
1811 nextcpu = mycpuid + 1;
1812 if (!error && nextcpu < ncpus)
1813 lwkt_forwardmsg(rtable_portfn(nextcpu), lmsg);
1815 lwkt_replymsg(lmsg, error);
1818 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1819 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);