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.28 2007/03/04 18:51:59 swildner Exp $
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/domain.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <sys/globaldata.h>
81 #include <sys/thread.h>
82 #include <sys/thread2.h>
83 #include <sys/msgport2.h>
86 #include <net/route.h>
87 #include <net/netisr.h>
89 #include <netinet/in.h>
90 #include <net/ip_mroute/ip_mroute.h>
92 static struct rtstatistics rtstatistics_percpu[MAXCPU];
94 #define rtstat rtstatistics_percpu[mycpuid]
96 #define rtstat rtstatistics_percpu[0]
99 struct radix_node_head *rt_tables[MAXCPU][AF_MAX+1];
100 struct lwkt_port *rt_ports[MAXCPU];
102 static void rt_maskedcopy (struct sockaddr *, struct sockaddr *,
104 static void rtable_init(void);
105 static void rtable_service_loop(void *dummy);
106 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo *,
107 struct rtentry *, void *);
110 static int rtredirect_msghandler(struct lwkt_msg *lmsg);
111 static int rtrequest1_msghandler(struct lwkt_msg *lmsg);
114 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW, 0, "Routing");
117 static int route_debug = 1;
118 SYSCTL_INT(_net_route, OID_AUTO, route_debug, CTLFLAG_RW,
119 &route_debug, 0, "");
123 * Initialize the route table(s) for protocol domains and
124 * create a helper thread which will be responsible for updating
125 * route table entries on each cpu.
133 for (cpu = 0; cpu < ncpus; ++cpu)
134 bzero(&rtstatistics_percpu[cpu], sizeof(struct rtstatistics));
135 rn_init(); /* initialize all zeroes, all ones, mask table */
137 for (cpu = 0; cpu < ncpus; cpu++) {
138 lwkt_migratecpu(cpu);
140 lwkt_create(rtable_service_loop, NULL, &rtd, NULL,
141 TDF_STOPREQ, cpu, "rtable_cpu %d", cpu);
142 rt_ports[cpu] = &rtd->td_msgport;
145 lwkt_migratecpu(origcpu);
153 SLIST_FOREACH(dom, &domains, dom_next) {
154 if (dom->dom_rtattach) {
156 (void **)&rt_tables[mycpuid][dom->dom_family],
163 * Our per-cpu table management protocol thread. All route table operations
164 * are chained through all cpus in order starting at cpu #0 in order to
165 * maintain duplicate route tables on each cpu. Having a spearate route
166 * table management thread allows the protocol and interrupt threads to
167 * issue route table changes.
170 rtable_service_loop(void *dummy __unused)
172 struct lwkt_msg *lmsg;
173 thread_t td = curthread;
175 while ((lmsg = lwkt_waitport(&td->td_msgport, NULL)) != NULL) {
176 lmsg->ms_cmd.cm_func(lmsg);
181 * Routing statistics.
185 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS)
189 for (cpu = 0; cpu < ncpus; ++cpu) {
190 if ((error = SYSCTL_OUT(req, &rtstatistics_percpu[cpu],
191 sizeof(struct rtstatistics))))
193 if ((error = SYSCTL_IN(req, &rtstatistics_percpu[cpu],
194 sizeof(struct rtstatistics))))
200 SYSCTL_PROC(_net_route, OID_AUTO, stats, (CTLTYPE_OPAQUE|CTLFLAG_RW),
201 0, 0, sysctl_rtstatistics, "S,rtstatistics", "Routing statistics");
203 SYSCTL_STRUCT(_net_route, OID_AUTO, stats, CTLFLAG_RW, &rtstat, rtstatistics,
204 "Routing statistics");
208 * Packet routing routines.
212 * Look up and fill in the "ro_rt" rtentry field in a route structure given
213 * an address in the "ro_dst" field. Always send a report on a miss and
214 * always clone routes.
217 rtalloc(struct route *ro)
219 rtalloc_ign(ro, 0UL);
223 * Look up and fill in the "ro_rt" rtentry field in a route structure given
224 * an address in the "ro_dst" field. Always send a report on a miss and
225 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
229 rtalloc_ign(struct route *ro, u_long ignoreflags)
231 if (ro->ro_rt != NULL) {
232 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP)
237 ro->ro_rt = _rtlookup(&ro->ro_dst, RTL_REPORTMSG, ignoreflags);
241 * Look up the route that matches the given "dst" address.
243 * Route lookup can have the side-effect of creating and returning
244 * a cloned route instead when "dst" matches a cloning route and the
245 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
247 * Any route returned has its reference count incremented.
250 _rtlookup(struct sockaddr *dst, boolean_t generate_report, u_long ignore)
252 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
259 * Look up route in the radix tree.
261 rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh);
266 * Handle cloning routes.
268 if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) {
269 struct rtentry *clonedroute;
272 clonedroute = rt; /* copy in/copy out parameter */
273 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
274 &clonedroute); /* clone the route */
275 if (error != 0) { /* cloning failed */
277 rt_dstmsg(RTM_MISS, dst, error);
279 return (rt); /* return the uncloned route */
281 if (generate_report) {
282 if (clonedroute->rt_flags & RTF_XRESOLVE)
283 rt_dstmsg(RTM_RESOLVE, dst, 0);
285 rt_rtmsg(RTM_ADD, clonedroute,
286 clonedroute->rt_ifp, 0);
288 return (clonedroute); /* return cloned route */
292 * Increment the reference count of the matched route and return.
298 rtstat.rts_unreach++;
300 rt_dstmsg(RTM_MISS, dst, 0);
305 rtfree(struct rtentry *rt)
307 KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt));
310 if (rt->rt_refcnt == 0) {
311 struct radix_node_head *rnh =
312 rt_tables[mycpuid][rt_key(rt)->sa_family];
315 rnh->rnh_close((struct radix_node *)rt, rnh);
316 if (!(rt->rt_flags & RTF_UP)) {
317 /* deallocate route */
318 if (rt->rt_ifa != NULL)
320 if (rt->rt_parent != NULL)
321 RTFREE(rt->rt_parent); /* recursive call! */
329 rtredirect_oncpu(struct sockaddr *dst, struct sockaddr *gateway,
330 struct sockaddr *netmask, int flags, struct sockaddr *src)
332 struct rtentry *rt = NULL;
333 struct rt_addrinfo rtinfo;
338 /* verify the gateway is directly reachable */
339 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
345 * If the redirect isn't from our current router for this destination,
346 * it's either old or wrong.
348 if (!(flags & RTF_DONE) && /* XXX JH */
349 (rt = rtpurelookup(dst)) != NULL &&
350 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) {
356 * If it redirects us to ourselves, we have a routing loop,
357 * perhaps as a result of an interface going down recently.
359 if (ifa_ifwithaddr(gateway)) {
360 error = EHOSTUNREACH;
365 * Create a new entry if the lookup failed or if we got back
366 * a wildcard entry for the default route. This is necessary
367 * for hosts which use routing redirects generated by smart
368 * gateways to dynamically build the routing tables.
372 if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) {
377 /* Ignore redirects for directly connected hosts. */
378 if (!(rt->rt_flags & RTF_GATEWAY)) {
379 error = EHOSTUNREACH;
383 if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) {
385 * Changing from a network route to a host route.
386 * Create a new host route rather than smashing the
390 flags |= RTF_GATEWAY | RTF_DYNAMIC;
391 bzero(&rtinfo, sizeof(struct rt_addrinfo));
392 rtinfo.rti_info[RTAX_DST] = dst;
393 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
394 rtinfo.rti_info[RTAX_NETMASK] = netmask;
395 rtinfo.rti_flags = flags;
396 rtinfo.rti_ifa = ifa;
397 rt = NULL; /* copy-in/copy-out parameter */
398 error = rtrequest1(RTM_ADD, &rtinfo, &rt);
400 flags = rt->rt_flags;
401 stat = &rtstat.rts_dynamic;
404 * Smash the current notion of the gateway to this destination.
405 * Should check about netmask!!!
407 rt->rt_flags |= RTF_MODIFIED;
408 flags |= RTF_MODIFIED;
409 rt_setgate(rt, rt_key(rt), gateway);
411 stat = &rtstat.rts_newgateway;
419 rtstat.rts_badredirect++;
420 else if (stat != NULL)
428 struct netmsg_rtredirect {
429 struct lwkt_msg lmsg;
430 struct sockaddr *dst;
431 struct sockaddr *gateway;
432 struct sockaddr *netmask;
434 struct sockaddr *src;
440 * Force a routing table entry to the specified
441 * destination to go through the given gateway.
442 * Normally called as a result of a routing redirect
443 * message from the network layer.
445 * N.B.: must be called at splnet
448 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
449 struct sockaddr *netmask, int flags, struct sockaddr *src)
451 struct rt_addrinfo rtinfo;
454 struct netmsg_rtredirect msg;
456 lwkt_initmsg(&msg.lmsg, &curthread->td_msgport, 0,
457 lwkt_cmd_func(rtredirect_msghandler), lwkt_cmd_op_none);
459 msg.gateway = gateway;
460 msg.netmask = netmask;
463 error = lwkt_domsg(rtable_portfn(0), &msg.lmsg);
465 error = rtredirect_oncpu(dst, gateway, netmask, flags, src);
467 bzero(&rtinfo, sizeof(struct rt_addrinfo));
468 rtinfo.rti_info[RTAX_DST] = dst;
469 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
470 rtinfo.rti_info[RTAX_NETMASK] = netmask;
471 rtinfo.rti_info[RTAX_AUTHOR] = src;
472 rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error);
478 rtredirect_msghandler(struct lwkt_msg *lmsg)
480 struct netmsg_rtredirect *msg = (void *)lmsg;
483 rtredirect_oncpu(msg->dst, msg->gateway, msg->netmask,
484 msg->flags, msg->src);
485 nextcpu = mycpuid + 1;
487 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->lmsg);
489 lwkt_replymsg(&msg->lmsg, 0);
496 * Routing table ioctl interface.
499 rtioctl(u_long req, caddr_t data, struct ucred *cred)
502 /* Multicast goop, grrr... */
503 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
510 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
514 if (!(flags & RTF_GATEWAY)) {
516 * If we are adding a route to an interface,
517 * and the interface is a point-to-point link,
518 * we should search for the destination
519 * as our clue to the interface. Otherwise
520 * we can use the local address.
523 if (flags & RTF_HOST) {
524 ifa = ifa_ifwithdstaddr(dst);
527 ifa = ifa_ifwithaddr(gateway);
530 * If we are adding a route to a remote net
531 * or host, the gateway may still be on the
532 * other end of a pt to pt link.
534 ifa = ifa_ifwithdstaddr(gateway);
537 ifa = ifa_ifwithnet(gateway);
541 rt = rtpurelookup(gateway);
545 if ((ifa = rt->rt_ifa) == NULL)
548 if (ifa->ifa_addr->sa_family != dst->sa_family) {
549 struct ifaddr *oldifa = ifa;
551 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
558 static int rt_fixdelete (struct radix_node *, void *);
559 static int rt_fixchange (struct radix_node *, void *);
563 struct radix_node_head *rnh;
567 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
570 rt_getifa(struct rt_addrinfo *rtinfo)
572 struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY];
573 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
574 struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA];
575 int flags = rtinfo->rti_flags;
578 * ifp may be specified by sockaddr_dl
579 * when protocol address is ambiguous.
581 if (rtinfo->rti_ifp == NULL) {
582 struct sockaddr *ifpaddr;
584 ifpaddr = rtinfo->rti_info[RTAX_IFP];
585 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
588 ifa = ifa_ifwithnet(ifpaddr);
590 rtinfo->rti_ifp = ifa->ifa_ifp;
594 if (rtinfo->rti_ifa == NULL && ifaaddr != NULL)
595 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr);
596 if (rtinfo->rti_ifa == NULL) {
599 sa = ifaaddr != NULL ? ifaaddr :
600 (gateway != NULL ? gateway : dst);
601 if (sa != NULL && rtinfo->rti_ifp != NULL)
602 rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp);
603 else if (dst != NULL && gateway != NULL)
604 rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
606 rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa);
608 if (rtinfo->rti_ifa == NULL)
609 return (ENETUNREACH);
611 if (rtinfo->rti_ifp == NULL)
612 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp;
617 * Do appropriate manipulations of a routing tree given
618 * all the bits of info needed
623 struct sockaddr *dst,
624 struct sockaddr *gateway,
625 struct sockaddr *netmask,
627 struct rtentry **ret_nrt)
629 struct rt_addrinfo rtinfo;
631 bzero(&rtinfo, sizeof(struct rt_addrinfo));
632 rtinfo.rti_info[RTAX_DST] = dst;
633 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
634 rtinfo.rti_info[RTAX_NETMASK] = netmask;
635 rtinfo.rti_flags = flags;
636 return rtrequest1(req, &rtinfo, ret_nrt);
642 struct sockaddr *dst,
643 struct sockaddr *gateway,
644 struct sockaddr *netmask,
647 struct rt_addrinfo rtinfo;
649 bzero(&rtinfo, sizeof(struct rt_addrinfo));
650 rtinfo.rti_info[RTAX_DST] = dst;
651 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
652 rtinfo.rti_info[RTAX_NETMASK] = netmask;
653 rtinfo.rti_flags = flags;
654 return rtrequest1_global(req, &rtinfo, NULL, NULL);
660 struct lwkt_msg lmsg;
662 struct rt_addrinfo *rtinfo;
663 rtrequest1_callback_func_t callback;
670 rtrequest1_global(int req, struct rt_addrinfo *rtinfo,
671 rtrequest1_callback_func_t callback, void *arg)
675 struct netmsg_rtq msg;
677 lwkt_initmsg(&msg.lmsg, &curthread->td_msgport, 0,
678 lwkt_cmd_func(rtrequest1_msghandler), lwkt_cmd_op_none);
679 msg.lmsg.ms_error = -1;
682 msg.callback = callback;
684 error = lwkt_domsg(rtable_portfn(0), &msg.lmsg);
686 struct rtentry *rt = NULL;
688 error = rtrequest1(req, rtinfo, &rt);
692 callback(req, error, rtinfo, rt, arg);
698 * Handle a route table request on the current cpu. Since the route table's
699 * are supposed to be identical on each cpu, an error occuring later in the
700 * message chain is considered system-fatal.
705 rtrequest1_msghandler(struct lwkt_msg *lmsg)
707 struct netmsg_rtq *msg = (void *)lmsg;
708 struct rtentry *rt = NULL;
712 error = rtrequest1(msg->req, msg->rtinfo, &rt);
716 msg->callback(msg->req, error, msg->rtinfo, rt, msg->arg);
719 * RTM_DELETE's are propogated even if an error occurs, since a
720 * cloned route might be undergoing deletion and cloned routes
721 * are not necessarily replicated. An overall error is returned
722 * only if no cpus have the route in question.
724 if (msg->lmsg.ms_error < 0 || error == 0)
725 msg->lmsg.ms_error = error;
727 nextcpu = mycpuid + 1;
728 if (error && msg->req != RTM_DELETE) {
730 panic("rtrequest1_msghandler: rtrequest table "
731 "error was not on cpu #0: %p", msg->rtinfo);
733 lwkt_replymsg(&msg->lmsg, error);
734 } else if (nextcpu < ncpus) {
735 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->lmsg);
737 lwkt_replymsg(&msg->lmsg, msg->lmsg.ms_error);
745 rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt)
747 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
749 struct radix_node *rn;
750 struct radix_node_head *rnh;
752 struct sockaddr *ndst;
755 #define gotoerr(x) { error = x ; goto bad; }
759 rt_addrinfo_print(req, rtinfo);
764 * Find the correct routing tree to use for this Address Family
766 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL)
767 gotoerr(EAFNOSUPPORT);
770 * If we are adding a host route then we don't want to put
771 * a netmask in the tree, nor do we want to clone it.
773 if (rtinfo->rti_flags & RTF_HOST) {
774 rtinfo->rti_info[RTAX_NETMASK] = NULL;
775 rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
780 /* Remove the item from the tree. */
781 rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST],
782 (char *)rtinfo->rti_info[RTAX_NETMASK],
786 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)),
787 ("rnh_deladdr returned flags 0x%x", rn->rn_flags));
788 rt = (struct rtentry *)rn;
790 /* ref to prevent a deletion race */
793 /* Free any routes cloned from this one. */
794 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
795 rt_mask(rt) != NULL) {
796 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
801 if (rt->rt_gwroute != NULL) {
802 RTFREE(rt->rt_gwroute);
803 rt->rt_gwroute = NULL;
807 * NB: RTF_UP must be set during the search above,
808 * because we might delete the last ref, causing
809 * rt to get freed prematurely.
811 rt->rt_flags &= ~RTF_UP;
815 rt_print(rtinfo, rt);
818 /* Give the protocol a chance to keep things in sync. */
819 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
820 ifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
823 * If the caller wants it, then it can have it,
824 * but it's up to it to free the rtentry as we won't be
827 KASSERT(rt->rt_refcnt >= 0,
828 ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt));
829 if (ret_nrt != NULL) {
830 /* leave ref intact for return */
833 /* deref / attempt to destroy */
839 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
843 rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
844 rtinfo->rti_flags |= RTF_WASCLONED;
845 rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
846 if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL)
847 rtinfo->rti_flags |= RTF_HOST;
851 KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) ||
852 rtinfo->rti_info[RTAX_GATEWAY] != NULL,
853 ("rtrequest: GATEWAY but no gateway"));
855 if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo)))
857 ifa = rtinfo->rti_ifa;
859 R_Malloc(rt, struct rtentry *, sizeof(struct rtentry));
862 bzero(rt, sizeof(struct rtentry));
863 rt->rt_flags = RTF_UP | rtinfo->rti_flags;
864 error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY]);
871 if (rtinfo->rti_info[RTAX_NETMASK] != NULL)
872 rt_maskedcopy(dst, ndst,
873 rtinfo->rti_info[RTAX_NETMASK]);
875 bcopy(dst, ndst, dst->sa_len);
878 * Note that we now have a reference to the ifa.
879 * This moved from below so that rnh->rnh_addaddr() can
880 * examine the ifa and ifa->ifa_ifp if it so desires.
884 rt->rt_ifp = ifa->ifa_ifp;
885 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
887 rn = rnh->rnh_addaddr((char *)ndst,
888 (char *)rtinfo->rti_info[RTAX_NETMASK],
891 struct rtentry *oldrt;
894 * We already have one of these in the tree.
895 * We do a special hack: if the old route was
896 * cloned, then we blow it away and try
897 * re-inserting the new one.
899 oldrt = rtpurelookup(ndst);
902 if (oldrt->rt_flags & RTF_WASCLONED) {
903 rtrequest(RTM_DELETE, rt_key(oldrt),
906 oldrt->rt_flags, NULL);
907 rn = rnh->rnh_addaddr((char *)ndst,
909 rtinfo->rti_info[RTAX_NETMASK],
916 * If it still failed to go into the tree,
917 * then un-make it (this should be a function).
920 if (rt->rt_gwroute != NULL)
921 rtfree(rt->rt_gwroute);
929 * If we got here from RESOLVE, then we are cloning
930 * so clone the rest, and note that we
931 * are a clone (and increment the parent's references)
933 if (req == RTM_RESOLVE) {
934 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
935 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
936 if ((*ret_nrt)->rt_flags &
937 (RTF_CLONING | RTF_PRCLONING)) {
938 rt->rt_parent = *ret_nrt;
939 (*ret_nrt)->rt_refcnt++;
944 * if this protocol has something to add to this then
945 * allow it to do that as well.
947 if (ifa->ifa_rtrequest != NULL)
948 ifa->ifa_rtrequest(req, rt, rtinfo);
951 * We repeat the same procedure from rt_setgate() here because
952 * it doesn't fire when we call it there because the node
953 * hasn't been added to the tree yet.
955 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) &&
956 rt_mask(rt) != NULL) {
957 struct rtfc_arg arg = { rt, rnh };
959 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
966 rt_print(rtinfo, rt);
969 * Return the resulting rtentry,
970 * increasing the number of references by one.
972 if (ret_nrt != NULL) {
984 kprintf("rti %p failed error %d\n", rtinfo, error);
986 kprintf("rti %p succeeded\n", rtinfo);
994 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
995 * (i.e., the routes related to it by the operation of cloning). This
996 * routine is iterated over all potential former-child-routes by way of
997 * rnh->rnh_walktree_from() above, and those that actually are children of
998 * the late parent (passed in as VP here) are themselves deleted.
1001 rt_fixdelete(struct radix_node *rn, void *vp)
1003 struct rtentry *rt = (struct rtentry *)rn;
1004 struct rtentry *rt0 = vp;
1006 if (rt->rt_parent == rt0 &&
1007 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1008 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1009 rt->rt_flags, NULL);
1015 * This routine is called from rt_setgate() to do the analogous thing for
1016 * adds and changes. There is the added complication in this case of a
1017 * middle insert; i.e., insertion of a new network route between an older
1018 * network route and (cloned) host routes. For this reason, a simple check
1019 * of rt->rt_parent is insufficient; each candidate route must be tested
1020 * against the (mask, value) of the new route (passed as before in vp)
1021 * to see if the new route matches it.
1023 * XXX - it may be possible to do fixdelete() for changes and reserve this
1024 * routine just for adds. I'm not sure why I thought it was necessary to do
1028 static int rtfcdebug = 0;
1032 rt_fixchange(struct radix_node *rn, void *vp)
1034 struct rtentry *rt = (struct rtentry *)rn;
1035 struct rtfc_arg *ap = vp;
1036 struct rtentry *rt0 = ap->rt0;
1037 struct radix_node_head *rnh = ap->rnh;
1038 u_char *xk1, *xm1, *xk2, *xmp;
1043 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
1046 if (rt->rt_parent == NULL ||
1047 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1049 if (rtfcdebug) kprintf("no parent, pinned or cloning\n");
1054 if (rt->rt_parent == rt0) {
1056 if (rtfcdebug) kprintf("parent match\n");
1058 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1059 rt->rt_flags, NULL);
1063 * There probably is a function somewhere which does this...
1064 * if not, there should be.
1066 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1068 xk1 = (u_char *)rt_key(rt0);
1069 xm1 = (u_char *)rt_mask(rt0);
1070 xk2 = (u_char *)rt_key(rt);
1072 /* avoid applying a less specific route */
1073 xmp = (u_char *)rt_mask(rt->rt_parent);
1074 mlen = rt_key(rt->rt_parent)->sa_len;
1075 if (mlen > rt_key(rt0)->sa_len) {
1078 kprintf("rt_fixchange: inserting a less "
1079 "specific route\n");
1083 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
1084 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
1087 kprintf("rt_fixchange: inserting a less "
1088 "specific route\n");
1094 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
1095 if ((xk2[i] & xm1[i]) != xk1[i]) {
1097 if (rtfcdebug) kprintf("no match\n");
1104 * OK, this node is a clone, and matches the node currently being
1105 * changed/added under the node's mask. So, get rid of it.
1108 if (rtfcdebug) kprintf("deleting\n");
1110 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1111 rt->rt_flags, NULL);
1114 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1117 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate)
1119 char *space, *oldspace;
1120 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
1121 struct rtentry *rt = rt0;
1122 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
1125 * A host route with the destination equal to the gateway
1126 * will interfere with keeping LLINFO in the routing
1127 * table, so disallow it.
1129 if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) ==
1130 (RTF_HOST | RTF_GATEWAY)) &&
1131 dst->sa_len == gate->sa_len &&
1132 sa_equal(dst, gate)) {
1134 * The route might already exist if this is an RTM_CHANGE
1135 * or a routing redirect, so try to delete it.
1137 if (rt_key(rt0) != NULL)
1138 rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway,
1139 rt_mask(rt0), rt0->rt_flags, NULL);
1140 return EADDRNOTAVAIL;
1144 * Both dst and gateway are stored in the same malloc'ed chunk
1145 * (If I ever get my hands on....)
1146 * if we need to malloc a new chunk, then keep the old one around
1147 * till we don't need it any more.
1149 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
1150 oldspace = (char *)rt_key(rt);
1151 R_Malloc(space, char *, dlen + glen);
1154 rt->rt_nodes->rn_key = space;
1156 space = (char *)rt_key(rt); /* Just use the old space. */
1160 /* Set the gateway value. */
1161 rt->rt_gateway = (struct sockaddr *)(space + dlen);
1162 bcopy(gate, rt->rt_gateway, glen);
1164 if (oldspace != NULL) {
1166 * If we allocated a new chunk, preserve the original dst.
1167 * This way, rt_setgate() really just sets the gate
1168 * and leaves the dst field alone.
1170 bcopy(dst, space, dlen);
1175 * If there is already a gwroute, it's now almost definitely wrong
1178 if (rt->rt_gwroute != NULL) {
1179 RTFREE(rt->rt_gwroute);
1180 rt->rt_gwroute = NULL;
1182 if (rt->rt_flags & RTF_GATEWAY) {
1184 * Cloning loop avoidance: In the presence of
1185 * protocol-cloning and bad configuration, it is
1186 * possible to get stuck in bottomless mutual recursion
1187 * (rtrequest rt_setgate rtlookup). We avoid this
1188 * by not allowing protocol-cloning to operate for
1189 * gateways (which is probably the correct choice
1190 * anyway), and avoid the resulting reference loops
1191 * by disallowing any route to run through itself as
1192 * a gateway. This is obviously mandatory when we
1193 * get rt->rt_output().
1195 * This breaks TTCP for hosts outside the gateway! XXX JH
1197 rt->rt_gwroute = _rtlookup(gate, RTL_REPORTMSG, RTF_PRCLONING);
1198 if (rt->rt_gwroute == rt) {
1199 rt->rt_gwroute = NULL;
1201 return EDQUOT; /* failure */
1206 * This isn't going to do anything useful for host routes, so
1207 * don't bother. Also make sure we have a reasonable mask
1208 * (we don't yet have one during adds).
1210 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1211 struct rtfc_arg arg = { rt, rnh };
1213 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1214 (char *)rt_mask(rt),
1215 rt_fixchange, &arg);
1223 struct sockaddr *src,
1224 struct sockaddr *dst,
1225 struct sockaddr *netmask)
1227 u_char *cp1 = (u_char *)src;
1228 u_char *cp2 = (u_char *)dst;
1229 u_char *cp3 = (u_char *)netmask;
1230 u_char *cplim = cp2 + *cp3;
1231 u_char *cplim2 = cp2 + *cp1;
1233 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1238 *cp2++ = *cp1++ & *cp3++;
1240 bzero(cp2, cplim2 - cp2);
1244 rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt)
1246 struct rtentry *up_rt, *rt;
1248 if (!(rt0->rt_flags & RTF_UP)) {
1249 up_rt = rtlookup(dst);
1251 return (EHOSTUNREACH);
1255 if (up_rt->rt_flags & RTF_GATEWAY) {
1256 if (up_rt->rt_gwroute == NULL) {
1257 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1258 if (up_rt->rt_gwroute == NULL)
1259 return (EHOSTUNREACH);
1260 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) {
1261 rtfree(up_rt->rt_gwroute);
1262 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1263 if (up_rt->rt_gwroute == NULL)
1264 return (EHOSTUNREACH);
1266 rt = up_rt->rt_gwroute;
1269 if (rt->rt_flags & RTF_REJECT &&
1270 (rt->rt_rmx.rmx_expire == 0 || /* rt doesn't expire */
1271 time_second < rt->rt_rmx.rmx_expire)) /* rt not expired */
1272 return (rt->rt_flags & RTF_HOST ? EHOSTDOWN : EHOSTUNREACH);
1280 * Print out a route table entry
1283 rt_print(struct rt_addrinfo *rtinfo, struct rtentry *rn)
1285 kprintf("rti %p cpu %d route %p flags %08lx: ",
1286 rtinfo, mycpuid, rn, rn->rt_flags);
1287 sockaddr_print(rt_key(rn));
1289 sockaddr_print(rt_mask(rn));
1291 sockaddr_print(rn->rt_gateway);
1292 kprintf(" ifc \"%s\"", rn->rt_ifp ? rn->rt_ifp->if_dname : "?");
1293 kprintf(" ifa %p\n", rn->rt_ifa);
1297 rt_addrinfo_print(int cmd, struct rt_addrinfo *rti)
1303 if (cmd == RTM_DELETE && route_debug > 1)
1304 db_print_backtrace();
1318 kprintf("C%02d ", cmd);
1321 kprintf("rti %p cpu %d ", rti, mycpuid);
1322 for (i = 0; i < rti->rti_addrs; ++i) {
1323 if (rti->rti_info[i] == NULL)
1353 kprintf("(?%02d ", i);
1356 sockaddr_print(rti->rti_info[i]);
1364 sockaddr_print(struct sockaddr *sa)
1366 struct sockaddr_in *sa4;
1367 struct sockaddr_in6 *sa6;
1376 len = sa->sa_len - offsetof(struct sockaddr, sa_data[0]);
1378 switch(sa->sa_family) {
1382 switch(sa->sa_family) {
1384 sa4 = (struct sockaddr_in *)sa;
1385 kprintf("INET %d %d.%d.%d.%d",
1386 ntohs(sa4->sin_port),
1387 (ntohl(sa4->sin_addr.s_addr) >> 24) & 255,
1388 (ntohl(sa4->sin_addr.s_addr) >> 16) & 255,
1389 (ntohl(sa4->sin_addr.s_addr) >> 8) & 255,
1390 (ntohl(sa4->sin_addr.s_addr) >> 0) & 255
1394 sa6 = (struct sockaddr_in6 *)sa;
1395 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
1396 ntohs(sa6->sin6_port),
1397 sa6->sin6_addr.s6_addr16[0],
1398 sa6->sin6_addr.s6_addr16[1],
1399 sa6->sin6_addr.s6_addr16[2],
1400 sa6->sin6_addr.s6_addr16[3],
1401 sa6->sin6_addr.s6_addr16[4],
1402 sa6->sin6_addr.s6_addr16[5],
1403 sa6->sin6_addr.s6_addr16[6],
1404 sa6->sin6_addr.s6_addr16[7]
1408 kprintf("AF%d ", sa->sa_family);
1409 while (len > 0 && sa->sa_data[len-1] == 0)
1412 for (i = 0; i < len; ++i) {
1415 kprintf("%d", (unsigned char)sa->sa_data[i]);
1425 * Set up a routing table entry, normally for an interface.
1428 rtinit(struct ifaddr *ifa, int cmd, int flags)
1430 struct sockaddr *dst, *deldst, *netmask;
1431 struct mbuf *m = NULL;
1432 struct radix_node_head *rnh;
1433 struct radix_node *rn;
1434 struct rt_addrinfo rtinfo;
1437 if (flags & RTF_HOST) {
1438 dst = ifa->ifa_dstaddr;
1441 dst = ifa->ifa_addr;
1442 netmask = ifa->ifa_netmask;
1445 * If it's a delete, check that if it exists, it's on the correct
1446 * interface or we might scrub a route to another ifa which would
1447 * be confusing at best and possibly worse.
1449 if (cmd == RTM_DELETE) {
1451 * It's a delete, so it should already exist..
1452 * If it's a net, mask off the host bits
1453 * (Assuming we have a mask)
1455 if (netmask != NULL) {
1456 m = m_get(MB_DONTWAIT, MT_SONAME);
1459 deldst = mtod(m, struct sockaddr *);
1460 rt_maskedcopy(dst, deldst, netmask);
1464 * Look up an rtentry that is in the routing tree and
1465 * contains the correct info.
1467 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL ||
1468 (rn = rnh->rnh_lookup((char *)dst,
1469 (char *)netmask, rnh)) == NULL ||
1470 ((struct rtentry *)rn)->rt_ifa != ifa ||
1471 !sa_equal((struct sockaddr *)rn->rn_key, dst)) {
1474 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1480 * One would think that as we are deleting, and we know
1481 * it doesn't exist, we could just return at this point
1482 * with an "ELSE" clause, but apparently not..
1484 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1489 * Do the actual request
1491 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1492 rtinfo.rti_info[RTAX_DST] = dst;
1493 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1494 rtinfo.rti_info[RTAX_NETMASK] = netmask;
1495 rtinfo.rti_flags = flags | ifa->ifa_flags;
1496 rtinfo.rti_ifa = ifa;
1497 error = rtrequest1_global(cmd, &rtinfo, rtinit_rtrequest_callback, ifa);
1504 rtinit_rtrequest_callback(int cmd, int error,
1505 struct rt_addrinfo *rtinfo, struct rtentry *rt,
1508 struct ifaddr *ifa = arg;
1510 if (error == 0 && rt) {
1513 rt_newaddrmsg(cmd, ifa, error, rt);
1516 if (cmd == RTM_DELETE) {
1517 if (rt->rt_refcnt == 0) {
1525 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1526 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);