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) 2004, 2005 Jeffrey M. Hsu. All rights reserved.
36 * License terms: all terms for the DragonFly license above plus the following:
38 * 4. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
41 * This product includes software developed by Jeffrey M. Hsu
42 * for the DragonFly Project.
44 * This requirement may be waived with permission from Jeffrey Hsu.
45 * Permission will be granted to any DragonFly user for free.
46 * This requirement will sunset and may be removed on Jan 31, 2006,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1980, 1986, 1991, 1993
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)route.c 8.3 (Berkeley) 1/9/95
84 * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $
85 * $DragonFly: src/sys/net/route.c,v 1.24 2006/01/31 19:05:35 dillon Exp $
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/malloc.h>
94 #include <sys/socket.h>
95 #include <sys/domain.h>
96 #include <sys/kernel.h>
97 #include <sys/sysctl.h>
98 #include <sys/globaldata.h>
99 #include <sys/thread.h>
100 #include <sys/thread2.h>
101 #include <sys/msgport2.h>
104 #include <net/route.h>
105 #include <net/netisr.h>
107 #include <netinet/in.h>
108 #include <net/ip_mroute/ip_mroute.h>
110 static struct rtstatistics rtstatistics_percpu[MAXCPU];
112 #define rtstat rtstatistics_percpu[mycpuid]
114 #define rtstat rtstatistics_percpu[0]
117 struct radix_node_head *rt_tables[MAXCPU][AF_MAX+1];
118 struct lwkt_port *rt_ports[MAXCPU];
120 static void rt_maskedcopy (struct sockaddr *, struct sockaddr *,
122 static void rtable_init(void);
123 static void rtable_service_loop(void *dummy);
124 static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo *,
125 struct rtentry *, void *);
128 static int rtredirect_msghandler(struct lwkt_msg *lmsg);
129 static int rtrequest1_msghandler(struct lwkt_msg *lmsg);
132 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW, 0, "Routing");
135 * Initialize the route table(s) for protocol domains and
136 * create a helper thread which will be responsible for updating
137 * route table entries on each cpu.
145 for (cpu = 0; cpu < ncpus; ++cpu)
146 bzero(&rtstatistics_percpu[cpu], sizeof(struct rtstatistics));
147 rn_init(); /* initialize all zeroes, all ones, mask table */
149 for (cpu = 0; cpu < ncpus; cpu++) {
150 lwkt_migratecpu(cpu);
152 lwkt_create(rtable_service_loop, NULL, &rtd, NULL,
153 TDF_STOPREQ, cpu, "rtable_cpu %d", cpu);
154 rt_ports[cpu] = &rtd->td_msgport;
157 lwkt_migratecpu(origcpu);
165 SLIST_FOREACH(dom, &domains, dom_next) {
166 if (dom->dom_rtattach) {
168 (void **)&rt_tables[mycpuid][dom->dom_family],
175 * Our per-cpu table management protocol thread. All route table operations
176 * are chained through all cpus in order starting at cpu #0 in order to
177 * maintain duplicate route tables on each cpu. Having a spearate route
178 * table management thread allows the protocol and interrupt threads to
179 * issue route table changes.
182 rtable_service_loop(void *dummy __unused)
184 struct lwkt_msg *lmsg;
185 thread_t td = curthread;
187 while ((lmsg = lwkt_waitport(&td->td_msgport, NULL)) != NULL) {
188 lmsg->ms_cmd.cm_func(lmsg);
193 * Routing statistics.
197 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS)
201 for (cpu = 0; cpu < ncpus; ++cpu) {
202 if ((error = SYSCTL_OUT(req, &rtstatistics_percpu[cpu],
203 sizeof(struct rtstatistics))))
205 if ((error = SYSCTL_IN(req, &rtstatistics_percpu[cpu],
206 sizeof(struct rtstatistics))))
212 SYSCTL_PROC(_net_route, OID_AUTO, stats, (CTLTYPE_OPAQUE|CTLFLAG_RW),
213 0, 0, sysctl_rtstatistics, "S,rtstatistics", "Routing statistics");
215 SYSCTL_STRUCT(_net_route, OID_AUTO, stats, CTLFLAG_RW, &rtstat, rtstatistics,
216 "Routing statistics");
220 * Packet routing routines.
224 * Look up and fill in the "ro_rt" rtentry field in a route structure given
225 * an address in the "ro_dst" field. Always send a report on a miss and
226 * always clone routes.
229 rtalloc(struct route *ro)
231 rtalloc_ign(ro, 0UL);
235 * Look up and fill in the "ro_rt" rtentry field in a route structure given
236 * an address in the "ro_dst" field. Always send a report on a miss and
237 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
241 rtalloc_ign(struct route *ro, u_long ignoreflags)
243 if (ro->ro_rt != NULL) {
244 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP)
249 ro->ro_rt = _rtlookup(&ro->ro_dst, RTL_REPORTMSG, ignoreflags);
253 * Look up the route that matches the given "dst" address.
255 * Route lookup can have the side-effect of creating and returning
256 * a cloned route instead when "dst" matches a cloning route and the
257 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
259 * Any route returned has its reference count incremented.
262 _rtlookup(struct sockaddr *dst, boolean_t generate_report, u_long ignore)
264 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
271 * Look up route in the radix tree.
273 rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh);
278 * Handle cloning routes.
280 if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) {
281 struct rtentry *clonedroute;
284 clonedroute = rt; /* copy in/copy out parameter */
285 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
286 &clonedroute); /* clone the route */
287 if (error != 0) { /* cloning failed */
289 rt_dstmsg(RTM_MISS, dst, error);
291 return (rt); /* return the uncloned route */
293 if (generate_report) {
294 if (clonedroute->rt_flags & RTF_XRESOLVE)
295 rt_dstmsg(RTM_RESOLVE, dst, 0);
297 rt_rtmsg(RTM_ADD, clonedroute,
298 clonedroute->rt_ifp, 0);
300 return (clonedroute); /* return cloned route */
304 * Increment the reference count of the matched route and return.
310 rtstat.rts_unreach++;
312 rt_dstmsg(RTM_MISS, dst, 0);
317 rtfree(struct rtentry *rt)
319 KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt));
322 if (rt->rt_refcnt == 0) {
323 struct radix_node_head *rnh =
324 rt_tables[mycpuid][rt_key(rt)->sa_family];
327 rnh->rnh_close((struct radix_node *)rt, rnh);
328 if (!(rt->rt_flags & RTF_UP)) {
329 /* deallocate route */
330 if (rt->rt_ifa != NULL)
332 if (rt->rt_parent != NULL)
333 RTFREE(rt->rt_parent); /* recursive call! */
341 rtredirect_oncpu(struct sockaddr *dst, struct sockaddr *gateway,
342 struct sockaddr *netmask, int flags, struct sockaddr *src)
344 struct rtentry *rt = NULL;
345 struct rt_addrinfo rtinfo;
350 /* verify the gateway is directly reachable */
351 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
357 * If the redirect isn't from our current router for this destination,
358 * it's either old or wrong.
360 if (!(flags & RTF_DONE) && /* XXX JH */
361 (rt = rtpurelookup(dst)) != NULL &&
362 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) {
368 * If it redirects us to ourselves, we have a routing loop,
369 * perhaps as a result of an interface going down recently.
371 if (ifa_ifwithaddr(gateway)) {
372 error = EHOSTUNREACH;
377 * Create a new entry if the lookup failed or if we got back
378 * a wildcard entry for the default route. This is necessary
379 * for hosts which use routing redirects generated by smart
380 * gateways to dynamically build the routing tables.
384 if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) {
389 /* Ignore redirects for directly connected hosts. */
390 if (!(rt->rt_flags & RTF_GATEWAY)) {
391 error = EHOSTUNREACH;
395 if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) {
397 * Changing from a network route to a host route.
398 * Create a new host route rather than smashing the
402 flags |= RTF_GATEWAY | RTF_DYNAMIC;
403 bzero(&rtinfo, sizeof(struct rt_addrinfo));
404 rtinfo.rti_info[RTAX_DST] = dst;
405 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
406 rtinfo.rti_info[RTAX_NETMASK] = netmask;
407 rtinfo.rti_flags = flags;
408 rtinfo.rti_ifa = ifa;
409 rt = NULL; /* copy-in/copy-out parameter */
410 error = rtrequest1(RTM_ADD, &rtinfo, &rt);
412 flags = rt->rt_flags;
413 stat = &rtstat.rts_dynamic;
416 * Smash the current notion of the gateway to this destination.
417 * Should check about netmask!!!
419 rt->rt_flags |= RTF_MODIFIED;
420 flags |= RTF_MODIFIED;
421 rt_setgate(rt, rt_key(rt), gateway);
423 stat = &rtstat.rts_newgateway;
431 rtstat.rts_badredirect++;
432 else if (stat != NULL)
440 struct netmsg_rtredirect {
441 struct lwkt_msg lmsg;
442 struct sockaddr *dst;
443 struct sockaddr *gateway;
444 struct sockaddr *netmask;
446 struct sockaddr *src;
452 * Force a routing table entry to the specified
453 * destination to go through the given gateway.
454 * Normally called as a result of a routing redirect
455 * message from the network layer.
457 * N.B.: must be called at splnet
460 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
461 struct sockaddr *netmask, int flags, struct sockaddr *src)
463 struct rt_addrinfo rtinfo;
466 struct netmsg_rtredirect msg;
468 lwkt_initmsg(&msg.lmsg, &curthread->td_msgport, 0,
469 lwkt_cmd_func(rtredirect_msghandler), lwkt_cmd_op_none);
471 msg.gateway = gateway;
472 msg.netmask = netmask;
475 error = lwkt_domsg(rtable_portfn(0), &msg.lmsg);
477 error = rtredirect_oncpu(dst, gateway, netmask, flags, src);
479 bzero(&rtinfo, sizeof(struct rt_addrinfo));
480 rtinfo.rti_info[RTAX_DST] = dst;
481 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
482 rtinfo.rti_info[RTAX_NETMASK] = netmask;
483 rtinfo.rti_info[RTAX_AUTHOR] = src;
484 rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error);
490 rtredirect_msghandler(struct lwkt_msg *lmsg)
492 struct netmsg_rtredirect *msg = (void *)lmsg;
495 rtredirect_oncpu(msg->dst, msg->gateway, msg->netmask,
496 msg->flags, msg->src);
497 nextcpu = mycpuid + 1;
499 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->lmsg);
501 lwkt_replymsg(&msg->lmsg, 0);
508 * Routing table ioctl interface.
511 rtioctl(u_long req, caddr_t data, struct thread *td)
514 /* Multicast goop, grrr... */
515 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
522 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
526 if (!(flags & RTF_GATEWAY)) {
528 * If we are adding a route to an interface,
529 * and the interface is a point-to-point link,
530 * we should search for the destination
531 * as our clue to the interface. Otherwise
532 * we can use the local address.
535 if (flags & RTF_HOST) {
536 ifa = ifa_ifwithdstaddr(dst);
539 ifa = ifa_ifwithaddr(gateway);
542 * If we are adding a route to a remote net
543 * or host, the gateway may still be on the
544 * other end of a pt to pt link.
546 ifa = ifa_ifwithdstaddr(gateway);
549 ifa = ifa_ifwithnet(gateway);
553 rt = rtpurelookup(gateway);
557 if ((ifa = rt->rt_ifa) == NULL)
560 if (ifa->ifa_addr->sa_family != dst->sa_family) {
561 struct ifaddr *oldifa = ifa;
563 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
570 static int rt_fixdelete (struct radix_node *, void *);
571 static int rt_fixchange (struct radix_node *, void *);
575 struct radix_node_head *rnh;
579 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
582 rt_getifa(struct rt_addrinfo *rtinfo)
584 struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY];
585 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
586 struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA];
587 int flags = rtinfo->rti_flags;
590 * ifp may be specified by sockaddr_dl
591 * when protocol address is ambiguous.
593 if (rtinfo->rti_ifp == NULL) {
594 struct sockaddr *ifpaddr;
596 ifpaddr = rtinfo->rti_info[RTAX_IFP];
597 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
600 ifa = ifa_ifwithnet(ifpaddr);
602 rtinfo->rti_ifp = ifa->ifa_ifp;
606 if (rtinfo->rti_ifa == NULL && ifaaddr != NULL)
607 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr);
608 if (rtinfo->rti_ifa == NULL) {
611 sa = ifaaddr != NULL ? ifaaddr :
612 (gateway != NULL ? gateway : dst);
613 if (sa != NULL && rtinfo->rti_ifp != NULL)
614 rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp);
615 else if (dst != NULL && gateway != NULL)
616 rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
618 rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa);
620 if (rtinfo->rti_ifa == NULL)
621 return (ENETUNREACH);
623 if (rtinfo->rti_ifp == NULL)
624 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp;
629 * Do appropriate manipulations of a routing tree given
630 * all the bits of info needed
635 struct sockaddr *dst,
636 struct sockaddr *gateway,
637 struct sockaddr *netmask,
639 struct rtentry **ret_nrt)
641 struct rt_addrinfo rtinfo;
643 bzero(&rtinfo, sizeof(struct rt_addrinfo));
644 rtinfo.rti_info[RTAX_DST] = dst;
645 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
646 rtinfo.rti_info[RTAX_NETMASK] = netmask;
647 rtinfo.rti_flags = flags;
648 return rtrequest1(req, &rtinfo, ret_nrt);
654 struct sockaddr *dst,
655 struct sockaddr *gateway,
656 struct sockaddr *netmask,
659 struct rt_addrinfo rtinfo;
661 bzero(&rtinfo, sizeof(struct rt_addrinfo));
662 rtinfo.rti_info[RTAX_DST] = dst;
663 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
664 rtinfo.rti_info[RTAX_NETMASK] = netmask;
665 rtinfo.rti_flags = flags;
666 return rtrequest1_global(req, &rtinfo, NULL, NULL);
672 struct lwkt_msg lmsg;
674 struct rt_addrinfo *rtinfo;
675 rtrequest1_callback_func_t callback;
682 rtrequest1_global(int req, struct rt_addrinfo *rtinfo,
683 rtrequest1_callback_func_t callback, void *arg)
687 struct netmsg_rtq msg;
689 lwkt_initmsg(&msg.lmsg, &curthread->td_msgport, 0,
690 lwkt_cmd_func(rtrequest1_msghandler), lwkt_cmd_op_none);
691 msg.lmsg.ms_error = -1;
694 msg.callback = callback;
696 error = lwkt_domsg(rtable_portfn(0), &msg.lmsg);
698 struct rtentry *rt = NULL;
700 error = rtrequest1(req, rtinfo, &rt);
704 callback(req, error, rtinfo, rt, arg);
710 * Handle a route table request on the current cpu. Since the route table's
711 * are supposed to be identical on each cpu, an error occuring later in the
712 * message chain is considered system-fatal.
717 rtrequest1_msghandler(struct lwkt_msg *lmsg)
719 struct netmsg_rtq *msg = (void *)lmsg;
720 struct rtentry *rt = NULL;
724 error = rtrequest1(msg->req, msg->rtinfo, &rt);
728 msg->callback(msg->req, error, msg->rtinfo, rt, msg->arg);
731 * RTM_DELETE's are propogated even if an error occurs, since a
732 * cloned route might be undergoing deletion and cloned routes
733 * are not necessarily replicated. An overall error is returned
734 * only if no cpus have the route in question.
736 if (msg->lmsg.ms_error < 0 || error == 0)
737 msg->lmsg.ms_error = error;
739 nextcpu = mycpuid + 1;
740 if (error && msg->req != RTM_DELETE) {
742 panic("rtrequest1_msghandler: rtrequest table "
743 "error was not on cpu #0: %p", msg);
745 lwkt_replymsg(&msg->lmsg, error);
746 } else if (nextcpu < ncpus) {
747 lwkt_forwardmsg(rtable_portfn(nextcpu), &msg->lmsg);
749 lwkt_replymsg(&msg->lmsg, msg->lmsg.ms_error);
757 rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt)
759 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
761 struct radix_node *rn;
762 struct radix_node_head *rnh;
764 struct sockaddr *ndst;
767 #define gotoerr(x) { error = x ; goto bad; }
771 * Find the correct routing tree to use for this Address Family
773 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL)
774 gotoerr(EAFNOSUPPORT);
777 * If we are adding a host route then we don't want to put
778 * a netmask in the tree, nor do we want to clone it.
780 if (rtinfo->rti_flags & RTF_HOST) {
781 rtinfo->rti_info[RTAX_NETMASK] = NULL;
782 rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
787 /* Remove the item from the tree. */
788 rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST],
789 (char *)rtinfo->rti_info[RTAX_NETMASK],
793 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)),
794 ("rnh_deladdr returned flags 0x%x", rn->rn_flags));
795 rt = (struct rtentry *)rn;
797 /* ref to prevent a deletion race */
800 /* Free any routes cloned from this one. */
801 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
802 rt_mask(rt) != NULL) {
803 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
808 if (rt->rt_gwroute != NULL) {
809 RTFREE(rt->rt_gwroute);
810 rt->rt_gwroute = NULL;
814 * NB: RTF_UP must be set during the search above,
815 * because we might delete the last ref, causing
816 * rt to get freed prematurely.
818 rt->rt_flags &= ~RTF_UP;
820 /* Give the protocol a chance to keep things in sync. */
821 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
822 ifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
825 * If the caller wants it, then it can have it,
826 * but it's up to it to free the rtentry as we won't be
829 KASSERT(rt->rt_refcnt >= 0,
830 ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt));
831 if (ret_nrt != NULL) {
832 /* leave ref intact for return */
835 /* deref / attempt to destroy */
841 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
845 rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
846 rtinfo->rti_flags |= RTF_WASCLONED;
847 rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
848 if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL)
849 rtinfo->rti_flags |= RTF_HOST;
853 KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) ||
854 rtinfo->rti_info[RTAX_GATEWAY] != NULL,
855 ("rtrequest: GATEWAY but no gateway"));
857 if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo)))
859 ifa = rtinfo->rti_ifa;
861 R_Malloc(rt, struct rtentry *, sizeof(struct rtentry));
864 bzero(rt, sizeof(struct rtentry));
865 rt->rt_flags = RTF_UP | rtinfo->rti_flags;
866 error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY]);
873 if (rtinfo->rti_info[RTAX_NETMASK] != NULL)
874 rt_maskedcopy(dst, ndst,
875 rtinfo->rti_info[RTAX_NETMASK]);
877 bcopy(dst, ndst, dst->sa_len);
880 * Note that we now have a reference to the ifa.
881 * This moved from below so that rnh->rnh_addaddr() can
882 * examine the ifa and ifa->ifa_ifp if it so desires.
886 rt->rt_ifp = ifa->ifa_ifp;
887 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
889 rn = rnh->rnh_addaddr((char *)ndst,
890 (char *)rtinfo->rti_info[RTAX_NETMASK],
893 struct rtentry *oldrt;
896 * We already have one of these in the tree.
897 * We do a special hack: if the old route was
898 * cloned, then we blow it away and try
899 * re-inserting the new one.
901 oldrt = rtpurelookup(ndst);
904 if (oldrt->rt_flags & RTF_WASCLONED) {
905 rtrequest(RTM_DELETE, rt_key(oldrt),
908 oldrt->rt_flags, NULL);
909 rn = rnh->rnh_addaddr((char *)ndst,
911 rtinfo->rti_info[RTAX_NETMASK],
918 * If it still failed to go into the tree,
919 * then un-make it (this should be a function).
922 if (rt->rt_gwroute != NULL)
923 rtfree(rt->rt_gwroute);
931 * If we got here from RESOLVE, then we are cloning
932 * so clone the rest, and note that we
933 * are a clone (and increment the parent's references)
935 if (req == RTM_RESOLVE) {
936 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
937 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
938 if ((*ret_nrt)->rt_flags &
939 (RTF_CLONING | RTF_PRCLONING)) {
940 rt->rt_parent = *ret_nrt;
941 (*ret_nrt)->rt_refcnt++;
946 * if this protocol has something to add to this then
947 * allow it to do that as well.
949 if (ifa->ifa_rtrequest != NULL)
950 ifa->ifa_rtrequest(req, rt, rtinfo);
953 * We repeat the same procedure from rt_setgate() here because
954 * it doesn't fire when we call it there because the node
955 * hasn't been added to the tree yet.
957 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) &&
958 rt_mask(rt) != NULL) {
959 struct rtfc_arg arg = { rt, rnh };
961 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
967 * Return the resulting rtentry,
968 * increasing the number of references by one.
970 if (ret_nrt != NULL) {
984 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
985 * (i.e., the routes related to it by the operation of cloning). This
986 * routine is iterated over all potential former-child-routes by way of
987 * rnh->rnh_walktree_from() above, and those that actually are children of
988 * the late parent (passed in as VP here) are themselves deleted.
991 rt_fixdelete(struct radix_node *rn, void *vp)
993 struct rtentry *rt = (struct rtentry *)rn;
994 struct rtentry *rt0 = vp;
996 if (rt->rt_parent == rt0 &&
997 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
998 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1005 * This routine is called from rt_setgate() to do the analogous thing for
1006 * adds and changes. There is the added complication in this case of a
1007 * middle insert; i.e., insertion of a new network route between an older
1008 * network route and (cloned) host routes. For this reason, a simple check
1009 * of rt->rt_parent is insufficient; each candidate route must be tested
1010 * against the (mask, value) of the new route (passed as before in vp)
1011 * to see if the new route matches it.
1013 * XXX - it may be possible to do fixdelete() for changes and reserve this
1014 * routine just for adds. I'm not sure why I thought it was necessary to do
1018 static int rtfcdebug = 0;
1022 rt_fixchange(struct radix_node *rn, void *vp)
1024 struct rtentry *rt = (struct rtentry *)rn;
1025 struct rtfc_arg *ap = vp;
1026 struct rtentry *rt0 = ap->rt0;
1027 struct radix_node_head *rnh = ap->rnh;
1028 u_char *xk1, *xm1, *xk2, *xmp;
1033 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
1036 if (rt->rt_parent == NULL ||
1037 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1039 if (rtfcdebug) printf("no parent, pinned or cloning\n");
1044 if (rt->rt_parent == rt0) {
1046 if (rtfcdebug) printf("parent match\n");
1048 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1049 rt->rt_flags, NULL);
1053 * There probably is a function somewhere which does this...
1054 * if not, there should be.
1056 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1058 xk1 = (u_char *)rt_key(rt0);
1059 xm1 = (u_char *)rt_mask(rt0);
1060 xk2 = (u_char *)rt_key(rt);
1062 /* avoid applying a less specific route */
1063 xmp = (u_char *)rt_mask(rt->rt_parent);
1064 mlen = rt_key(rt->rt_parent)->sa_len;
1065 if (mlen > rt_key(rt0)->sa_len) {
1068 printf("rt_fixchange: inserting a less "
1069 "specific route\n");
1073 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
1074 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
1077 printf("rt_fixchange: inserting a less "
1078 "specific route\n");
1084 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
1085 if ((xk2[i] & xm1[i]) != xk1[i]) {
1087 if (rtfcdebug) printf("no match\n");
1094 * OK, this node is a clone, and matches the node currently being
1095 * changed/added under the node's mask. So, get rid of it.
1098 if (rtfcdebug) printf("deleting\n");
1100 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1101 rt->rt_flags, NULL);
1104 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1107 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate)
1109 char *space, *oldspace;
1110 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
1111 struct rtentry *rt = rt0;
1112 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
1115 * A host route with the destination equal to the gateway
1116 * will interfere with keeping LLINFO in the routing
1117 * table, so disallow it.
1119 if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) ==
1120 (RTF_HOST | RTF_GATEWAY)) &&
1121 dst->sa_len == gate->sa_len &&
1122 sa_equal(dst, gate)) {
1124 * The route might already exist if this is an RTM_CHANGE
1125 * or a routing redirect, so try to delete it.
1127 if (rt_key(rt0) != NULL)
1128 rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway,
1129 rt_mask(rt0), rt0->rt_flags, NULL);
1130 return EADDRNOTAVAIL;
1134 * Both dst and gateway are stored in the same malloc'ed chunk
1135 * (If I ever get my hands on....)
1136 * if we need to malloc a new chunk, then keep the old one around
1137 * till we don't need it any more.
1139 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
1140 oldspace = (char *)rt_key(rt);
1141 R_Malloc(space, char *, dlen + glen);
1144 rt->rt_nodes->rn_key = space;
1146 space = (char *)rt_key(rt); /* Just use the old space. */
1150 /* Set the gateway value. */
1151 rt->rt_gateway = (struct sockaddr *)(space + dlen);
1152 bcopy(gate, rt->rt_gateway, glen);
1154 if (oldspace != NULL) {
1156 * If we allocated a new chunk, preserve the original dst.
1157 * This way, rt_setgate() really just sets the gate
1158 * and leaves the dst field alone.
1160 bcopy(dst, space, dlen);
1165 * If there is already a gwroute, it's now almost definitely wrong
1168 if (rt->rt_gwroute != NULL) {
1169 RTFREE(rt->rt_gwroute);
1170 rt->rt_gwroute = NULL;
1172 if (rt->rt_flags & RTF_GATEWAY) {
1174 * Cloning loop avoidance: In the presence of
1175 * protocol-cloning and bad configuration, it is
1176 * possible to get stuck in bottomless mutual recursion
1177 * (rtrequest rt_setgate rtlookup). We avoid this
1178 * by not allowing protocol-cloning to operate for
1179 * gateways (which is probably the correct choice
1180 * anyway), and avoid the resulting reference loops
1181 * by disallowing any route to run through itself as
1182 * a gateway. This is obviously mandatory when we
1183 * get rt->rt_output().
1185 * This breaks TTCP for hosts outside the gateway! XXX JH
1187 rt->rt_gwroute = _rtlookup(gate, RTL_REPORTMSG, RTF_PRCLONING);
1188 if (rt->rt_gwroute == rt) {
1189 rt->rt_gwroute = NULL;
1191 return EDQUOT; /* failure */
1196 * This isn't going to do anything useful for host routes, so
1197 * don't bother. Also make sure we have a reasonable mask
1198 * (we don't yet have one during adds).
1200 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1201 struct rtfc_arg arg = { rt, rnh };
1203 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1204 (char *)rt_mask(rt),
1205 rt_fixchange, &arg);
1213 struct sockaddr *src,
1214 struct sockaddr *dst,
1215 struct sockaddr *netmask)
1217 u_char *cp1 = (u_char *)src;
1218 u_char *cp2 = (u_char *)dst;
1219 u_char *cp3 = (u_char *)netmask;
1220 u_char *cplim = cp2 + *cp3;
1221 u_char *cplim2 = cp2 + *cp1;
1223 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1228 *cp2++ = *cp1++ & *cp3++;
1230 bzero(cp2, cplim2 - cp2);
1234 rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt)
1236 struct rtentry *up_rt, *rt;
1238 if (!(rt0->rt_flags & RTF_UP)) {
1239 up_rt = rtlookup(dst);
1241 return (EHOSTUNREACH);
1245 if (up_rt->rt_flags & RTF_GATEWAY) {
1246 if (up_rt->rt_gwroute == NULL) {
1247 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1248 if (up_rt->rt_gwroute == NULL)
1249 return (EHOSTUNREACH);
1250 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) {
1251 rtfree(up_rt->rt_gwroute);
1252 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1253 if (up_rt->rt_gwroute == NULL)
1254 return (EHOSTUNREACH);
1256 rt = up_rt->rt_gwroute;
1259 if (rt->rt_flags & RTF_REJECT &&
1260 (rt->rt_rmx.rmx_expire == 0 || /* rt doesn't expire */
1261 time_second < rt->rt_rmx.rmx_expire)) /* rt not expired */
1262 return (rt->rt_flags & RTF_HOST ? EHOSTDOWN : EHOSTUNREACH);
1268 * Set up a routing table entry, normally for an interface.
1271 rtinit(struct ifaddr *ifa, int cmd, int flags)
1273 struct sockaddr *dst, *deldst, *netmask;
1274 struct mbuf *m = NULL;
1275 struct radix_node_head *rnh;
1276 struct radix_node *rn;
1277 struct rt_addrinfo rtinfo;
1280 if (flags & RTF_HOST) {
1281 dst = ifa->ifa_dstaddr;
1284 dst = ifa->ifa_addr;
1285 netmask = ifa->ifa_netmask;
1288 * If it's a delete, check that if it exists, it's on the correct
1289 * interface or we might scrub a route to another ifa which would
1290 * be confusing at best and possibly worse.
1292 if (cmd == RTM_DELETE) {
1294 * It's a delete, so it should already exist..
1295 * If it's a net, mask off the host bits
1296 * (Assuming we have a mask)
1298 if (netmask != NULL) {
1299 m = m_get(MB_DONTWAIT, MT_SONAME);
1302 deldst = mtod(m, struct sockaddr *);
1303 rt_maskedcopy(dst, deldst, netmask);
1307 * Look up an rtentry that is in the routing tree and
1308 * contains the correct info.
1310 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL ||
1311 (rn = rnh->rnh_lookup((char *)dst,
1312 (char *)netmask, rnh)) == NULL ||
1313 ((struct rtentry *)rn)->rt_ifa != ifa ||
1314 !sa_equal((struct sockaddr *)rn->rn_key, dst)) {
1317 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1323 * One would think that as we are deleting, and we know
1324 * it doesn't exist, we could just return at this point
1325 * with an "ELSE" clause, but apparently not..
1327 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1332 * Do the actual request
1334 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1335 rtinfo.rti_info[RTAX_DST] = dst;
1336 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1337 rtinfo.rti_info[RTAX_NETMASK] = netmask;
1338 rtinfo.rti_flags = flags | ifa->ifa_flags;
1339 rtinfo.rti_ifa = ifa;
1340 error = rtrequest1_global(cmd, &rtinfo, rtinit_rtrequest_callback, ifa);
1347 rtinit_rtrequest_callback(int cmd, int error,
1348 struct rt_addrinfo *rtinfo, struct rtentry *rt,
1351 struct ifaddr *ifa = arg;
1353 if (error == 0 && rt) {
1356 rt_newaddrmsg(cmd, ifa, error, rt);
1359 if (cmd == RTM_DELETE) {
1360 if (rt->rt_refcnt == 0) {
1368 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1369 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);