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.22 2005/06/15 19:29:30 joerg 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>
103 #include <net/route.h>
105 #include <netinet/in.h>
106 #include <net/ip_mroute/ip_mroute.h>
108 static struct rtstatistics rtstatistics_percpu[MAXCPU];
110 #define rtstat rtstatistics_percpu[mycpuid]
112 #define rtstat rtstatistics_percpu[0]
115 struct radix_node_head *rt_tables[AF_MAX+1];
117 static void rt_maskedcopy (struct sockaddr *, struct sockaddr *,
119 static void rtable_init (void **);
121 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW, 0, "Routing");
124 rtable_init(void **table)
128 SLIST_FOREACH(dom, &domains, dom_next)
129 if (dom->dom_rtattach)
130 dom->dom_rtattach(&table[dom->dom_family],
140 for (ccpu = 0; ccpu < ncpus; ++ccpu)
141 bzero(&rtstatistics_percpu[ccpu], sizeof(struct rtstatistics));
143 bzero(&rtstat, sizeof(struct rtstatistics));
146 rn_init(); /* initialize all zeroes, all ones, mask table */
147 rtable_init((void **)rt_tables);
151 * Routing statistics.
155 sysctl_rtstatistics(SYSCTL_HANDLER_ARGS)
159 for (cpu = 0; cpu < ncpus; ++cpu) {
160 if ((error = SYSCTL_OUT(req, &rtstatistics_percpu[cpu],
161 sizeof(struct rtstatistics))))
163 if ((error = SYSCTL_IN(req, &rtstatistics_percpu[cpu],
164 sizeof(struct rtstatistics))))
170 SYSCTL_PROC(_net_route, OID_AUTO, stats, (CTLTYPE_OPAQUE|CTLFLAG_RW),
171 0, 0, sysctl_rtstatistics, "S,rtstatistics", "Routing statistics");
173 SYSCTL_STRUCT(_net_route, OID_AUTO, stats, CTLFLAG_RW, &rtstat, rtstatistics,
174 "Routing statistics");
178 * Packet routing routines.
182 * Look up and fill in the "ro_rt" rtentry field in a route structure given
183 * an address in the "ro_dst" field. Always send a report on a miss and
184 * always clone routes.
187 rtalloc(struct route *ro)
189 rtalloc_ign(ro, 0UL);
193 * Look up and fill in the "ro_rt" rtentry field in a route structure given
194 * an address in the "ro_dst" field. Always send a report on a miss and
195 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
199 rtalloc_ign(struct route *ro, u_long ignoreflags)
201 if (ro->ro_rt != NULL) {
202 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP)
207 ro->ro_rt = _rtlookup(&ro->ro_dst, RTL_REPORTMSG, ignoreflags);
211 * Look up the route that matches the given "dst" address.
213 * Route lookup can have the side-effect of creating and returning
214 * a cloned route instead when "dst" matches a cloning route and the
215 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
217 * Any route returned has its reference count incremented.
220 _rtlookup(struct sockaddr *dst, boolean_t generate_report, u_long ignore)
222 struct radix_node_head *rnh = rt_tables[dst->sa_family];
229 * Look up route in the radix tree.
231 rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh);
236 * Handle cloning routes.
238 if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) {
239 struct rtentry *clonedroute;
242 clonedroute = rt; /* copy in/copy out parameter */
243 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
244 &clonedroute); /* clone the route */
245 if (error != 0) { /* cloning failed */
247 rt_dstmsg(RTM_MISS, dst, error);
249 return (rt); /* return the uncloned route */
251 if (generate_report) {
252 if (clonedroute->rt_flags & RTF_XRESOLVE)
253 rt_dstmsg(RTM_RESOLVE, dst, 0);
255 rt_rtmsg(RTM_ADD, clonedroute,
256 clonedroute->rt_ifp, 0);
258 return (clonedroute); /* return cloned route */
262 * Increment the reference count of the matched route and return.
268 rtstat.rts_unreach++;
270 rt_dstmsg(RTM_MISS, dst, 0);
275 rtfree(struct rtentry *rt)
277 KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt));
280 if (rt->rt_refcnt == 0) {
281 struct radix_node_head *rnh = rt_tables[rt_key(rt)->sa_family];
284 rnh->rnh_close((struct radix_node *)rt, rnh);
285 if (!(rt->rt_flags & RTF_UP)) {
286 /* deallocate route */
287 if (rt->rt_ifa != NULL)
289 if (rt->rt_parent != NULL)
290 RTFREE(rt->rt_parent); /* recursive call! */
298 * Force a routing table entry to the specified destination to go through
299 * the given gateway. Normally called as a result of a routing redirect
300 * message from the network layer.
302 * N.B.: must be called at splnet
305 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
306 struct sockaddr *netmask, int flags, struct sockaddr *src)
308 struct rtentry *rt = NULL;
309 struct rt_addrinfo rtinfo;
314 /* verify the gateway is directly reachable */
315 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
321 * If the redirect isn't from our current router for this destination,
322 * it's either old or wrong.
324 if (!(flags & RTF_DONE) && /* XXX JH */
325 (rt = rtpurelookup(dst)) != NULL &&
326 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) {
332 * If it redirects us to ourselves, we have a routing loop,
333 * perhaps as a result of an interface going down recently.
335 if (ifa_ifwithaddr(gateway)) {
336 error = EHOSTUNREACH;
341 * Create a new entry if the lookup failed or if we got back
342 * a wildcard entry for the default route. This is necessary
343 * for hosts which use routing redirects generated by smart
344 * gateways to dynamically build the routing tables.
348 if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) {
353 /* Ignore redirects for directly connected hosts. */
354 if (!(rt->rt_flags & RTF_GATEWAY)) {
355 error = EHOSTUNREACH;
359 if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) {
361 * Changing from a network route to a host route.
362 * Create a new host route rather than smashing the
366 flags |= RTF_GATEWAY | RTF_DYNAMIC;
367 bzero(&rtinfo, sizeof(struct rt_addrinfo));
368 rtinfo.rti_info[RTAX_DST] = dst;
369 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
370 rtinfo.rti_info[RTAX_NETMASK] = netmask;
371 rtinfo.rti_flags = flags;
372 rtinfo.rti_ifa = ifa;
373 rt = NULL; /* copy-in/copy-out parameter */
374 error = rtrequest1(RTM_ADD, &rtinfo, &rt);
376 flags = rt->rt_flags;
377 stat = &rtstat.rts_dynamic;
380 * Smash the current notion of the gateway to this destination.
381 * Should check about netmask!!!
383 rt->rt_flags |= RTF_MODIFIED;
384 flags |= RTF_MODIFIED;
385 rt_setgate(rt, rt_key(rt), gateway);
387 stat = &rtstat.rts_newgateway;
395 rtstat.rts_badredirect++;
396 else if (stat != NULL)
399 bzero(&rtinfo, sizeof(struct rt_addrinfo));
400 rtinfo.rti_info[RTAX_DST] = dst;
401 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
402 rtinfo.rti_info[RTAX_NETMASK] = netmask;
403 rtinfo.rti_info[RTAX_AUTHOR] = src;
404 rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error);
408 * Routing table ioctl interface.
411 rtioctl(u_long req, caddr_t data, struct thread *td)
414 /* Multicast goop, grrr... */
415 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
422 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
426 if (!(flags & RTF_GATEWAY)) {
428 * If we are adding a route to an interface,
429 * and the interface is a point-to-point link,
430 * we should search for the destination
431 * as our clue to the interface. Otherwise
432 * we can use the local address.
435 if (flags & RTF_HOST) {
436 ifa = ifa_ifwithdstaddr(dst);
439 ifa = ifa_ifwithaddr(gateway);
442 * If we are adding a route to a remote net
443 * or host, the gateway may still be on the
444 * other end of a pt to pt link.
446 ifa = ifa_ifwithdstaddr(gateway);
449 ifa = ifa_ifwithnet(gateway);
453 rt = rtpurelookup(gateway);
457 if ((ifa = rt->rt_ifa) == NULL)
460 if (ifa->ifa_addr->sa_family != dst->sa_family) {
461 struct ifaddr *oldifa = ifa;
463 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
470 static int rt_fixdelete (struct radix_node *, void *);
471 static int rt_fixchange (struct radix_node *, void *);
475 struct radix_node_head *rnh;
479 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
482 rt_getifa(struct rt_addrinfo *rtinfo)
484 struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY];
485 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
486 struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA];
487 int flags = rtinfo->rti_flags;
490 * ifp may be specified by sockaddr_dl
491 * when protocol address is ambiguous.
493 if (rtinfo->rti_ifp == NULL) {
494 struct sockaddr *ifpaddr;
496 ifpaddr = rtinfo->rti_info[RTAX_IFP];
497 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
500 ifa = ifa_ifwithnet(ifpaddr);
502 rtinfo->rti_ifp = ifa->ifa_ifp;
506 if (rtinfo->rti_ifa == NULL && ifaaddr != NULL)
507 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr);
508 if (rtinfo->rti_ifa == NULL) {
511 sa = ifaaddr != NULL ? ifaaddr :
512 (gateway != NULL ? gateway : dst);
513 if (sa != NULL && rtinfo->rti_ifp != NULL)
514 rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp);
515 else if (dst != NULL && gateway != NULL)
516 rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
518 rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa);
520 if (rtinfo->rti_ifa == NULL)
521 return (ENETUNREACH);
523 if (rtinfo->rti_ifp == NULL)
524 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp;
529 * Do appropriate manipulations of a routing tree given
530 * all the bits of info needed
535 struct sockaddr *dst,
536 struct sockaddr *gateway,
537 struct sockaddr *netmask,
539 struct rtentry **ret_nrt)
541 struct rt_addrinfo rtinfo;
543 bzero(&rtinfo, sizeof(struct rt_addrinfo));
544 rtinfo.rti_info[RTAX_DST] = dst;
545 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
546 rtinfo.rti_info[RTAX_NETMASK] = netmask;
547 rtinfo.rti_flags = flags;
548 return rtrequest1(req, &rtinfo, ret_nrt);
552 rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt)
554 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
556 struct radix_node *rn;
557 struct radix_node_head *rnh;
559 struct sockaddr *ndst;
562 #define gotoerr(x) { error = x ; goto bad; }
566 * Find the correct routing tree to use for this Address Family
568 if ((rnh = rt_tables[dst->sa_family]) == NULL)
569 gotoerr(EAFNOSUPPORT);
572 * If we are adding a host route then we don't want to put
573 * a netmask in the tree, nor do we want to clone it.
575 if (rtinfo->rti_flags & RTF_HOST) {
576 rtinfo->rti_info[RTAX_NETMASK] = NULL;
577 rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
582 /* Remove the item from the tree. */
583 rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST],
584 (char *)rtinfo->rti_info[RTAX_NETMASK],
588 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)),
589 ("rnh_deladdr returned flags 0x%x", rn->rn_flags));
590 rt = (struct rtentry *)rn;
592 /* Free any routes cloned from this one. */
593 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
594 rt_mask(rt) != NULL) {
595 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
600 if (rt->rt_gwroute != NULL) {
601 RTFREE(rt->rt_gwroute);
602 rt->rt_gwroute = NULL;
606 * NB: RTF_UP must be set during the search above,
607 * because we might delete the last ref, causing
608 * rt to get freed prematurely.
610 rt->rt_flags &= ~RTF_UP;
612 /* Give the protocol a chance to keep things in sync. */
613 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
614 ifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
617 * If the caller wants it, then it can have it,
618 * but it's up to it to free the rtentry as we won't be
621 KASSERT(rt->rt_refcnt >= 0,
622 ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt));
623 if (ret_nrt != NULL) {
625 } else if (rt->rt_refcnt == 0) {
626 rt->rt_refcnt++; /* refcnt > 0 required for rtfree() */
632 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
636 rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
637 rtinfo->rti_flags |= RTF_WASCLONED;
638 rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
639 if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL)
640 rtinfo->rti_flags |= RTF_HOST;
644 KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) ||
645 rtinfo->rti_info[RTAX_GATEWAY] != NULL,
646 ("rtrequest: GATEWAY but no gateway"));
648 if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo)))
650 ifa = rtinfo->rti_ifa;
652 R_Malloc(rt, struct rtentry *, sizeof(struct rtentry));
655 bzero(rt, sizeof(struct rtentry));
656 rt->rt_flags = RTF_UP | rtinfo->rti_flags;
657 error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY]);
664 if (rtinfo->rti_info[RTAX_NETMASK] != NULL)
665 rt_maskedcopy(dst, ndst,
666 rtinfo->rti_info[RTAX_NETMASK]);
668 bcopy(dst, ndst, dst->sa_len);
671 * Note that we now have a reference to the ifa.
672 * This moved from below so that rnh->rnh_addaddr() can
673 * examine the ifa and ifa->ifa_ifp if it so desires.
677 rt->rt_ifp = ifa->ifa_ifp;
678 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
680 rn = rnh->rnh_addaddr((char *)ndst,
681 (char *)rtinfo->rti_info[RTAX_NETMASK],
684 struct rtentry *oldrt;
687 * We already have one of these in the tree.
688 * We do a special hack: if the old route was
689 * cloned, then we blow it away and try
690 * re-inserting the new one.
692 oldrt = rtpurelookup(ndst);
695 if (oldrt->rt_flags & RTF_WASCLONED) {
696 rtrequest(RTM_DELETE, rt_key(oldrt),
699 oldrt->rt_flags, NULL);
700 rn = rnh->rnh_addaddr((char *)ndst,
702 rtinfo->rti_info[RTAX_NETMASK],
709 * If it still failed to go into the tree,
710 * then un-make it (this should be a function).
713 if (rt->rt_gwroute != NULL)
714 rtfree(rt->rt_gwroute);
722 * If we got here from RESOLVE, then we are cloning
723 * so clone the rest, and note that we
724 * are a clone (and increment the parent's references)
726 if (req == RTM_RESOLVE) {
727 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
728 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
729 if ((*ret_nrt)->rt_flags &
730 (RTF_CLONING | RTF_PRCLONING)) {
731 rt->rt_parent = *ret_nrt;
732 (*ret_nrt)->rt_refcnt++;
737 * if this protocol has something to add to this then
738 * allow it to do that as well.
740 if (ifa->ifa_rtrequest != NULL)
741 ifa->ifa_rtrequest(req, rt, rtinfo);
744 * We repeat the same procedure from rt_setgate() here because
745 * it doesn't fire when we call it there because the node
746 * hasn't been added to the tree yet.
748 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) &&
749 rt_mask(rt) != NULL) {
750 struct rtfc_arg arg = { rt, rnh };
752 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
758 * Return the resulting rtentry,
759 * increasing the number of references by one.
761 if (ret_nrt != NULL) {
775 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
776 * (i.e., the routes related to it by the operation of cloning). This
777 * routine is iterated over all potential former-child-routes by way of
778 * rnh->rnh_walktree_from() above, and those that actually are children of
779 * the late parent (passed in as VP here) are themselves deleted.
782 rt_fixdelete(struct radix_node *rn, void *vp)
784 struct rtentry *rt = (struct rtentry *)rn;
785 struct rtentry *rt0 = vp;
787 if (rt->rt_parent == rt0 &&
788 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
789 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
796 * This routine is called from rt_setgate() to do the analogous thing for
797 * adds and changes. There is the added complication in this case of a
798 * middle insert; i.e., insertion of a new network route between an older
799 * network route and (cloned) host routes. For this reason, a simple check
800 * of rt->rt_parent is insufficient; each candidate route must be tested
801 * against the (mask, value) of the new route (passed as before in vp)
802 * to see if the new route matches it.
804 * XXX - it may be possible to do fixdelete() for changes and reserve this
805 * routine just for adds. I'm not sure why I thought it was necessary to do
809 static int rtfcdebug = 0;
813 rt_fixchange(struct radix_node *rn, void *vp)
815 struct rtentry *rt = (struct rtentry *)rn;
816 struct rtfc_arg *ap = vp;
817 struct rtentry *rt0 = ap->rt0;
818 struct radix_node_head *rnh = ap->rnh;
819 u_char *xk1, *xm1, *xk2, *xmp;
824 printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
827 if (rt->rt_parent == NULL ||
828 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
830 if (rtfcdebug) printf("no parent, pinned or cloning\n");
835 if (rt->rt_parent == rt0) {
837 if (rtfcdebug) printf("parent match\n");
839 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
844 * There probably is a function somewhere which does this...
845 * if not, there should be.
847 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
849 xk1 = (u_char *)rt_key(rt0);
850 xm1 = (u_char *)rt_mask(rt0);
851 xk2 = (u_char *)rt_key(rt);
853 /* avoid applying a less specific route */
854 xmp = (u_char *)rt_mask(rt->rt_parent);
855 mlen = rt_key(rt->rt_parent)->sa_len;
856 if (mlen > rt_key(rt0)->sa_len) {
859 printf("rt_fixchange: inserting a less "
864 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
865 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
868 printf("rt_fixchange: inserting a less "
875 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
876 if ((xk2[i] & xm1[i]) != xk1[i]) {
878 if (rtfcdebug) printf("no match\n");
885 * OK, this node is a clone, and matches the node currently being
886 * changed/added under the node's mask. So, get rid of it.
889 if (rtfcdebug) printf("deleting\n");
891 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
895 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
898 rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate)
900 char *space, *oldspace;
901 int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
902 struct rtentry *rt = rt0;
903 struct radix_node_head *rnh = rt_tables[dst->sa_family];
906 * A host route with the destination equal to the gateway
907 * will interfere with keeping LLINFO in the routing
908 * table, so disallow it.
910 if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) ==
911 (RTF_HOST | RTF_GATEWAY)) &&
912 dst->sa_len == gate->sa_len &&
913 sa_equal(dst, gate)) {
915 * The route might already exist if this is an RTM_CHANGE
916 * or a routing redirect, so try to delete it.
918 if (rt_key(rt0) != NULL)
919 rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway,
920 rt_mask(rt0), rt0->rt_flags, NULL);
921 return EADDRNOTAVAIL;
925 * Both dst and gateway are stored in the same malloc'ed chunk
926 * (If I ever get my hands on....)
927 * if we need to malloc a new chunk, then keep the old one around
928 * till we don't need it any more.
930 if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
931 oldspace = (char *)rt_key(rt);
932 R_Malloc(space, char *, dlen + glen);
935 rt->rt_nodes->rn_key = space;
937 space = (char *)rt_key(rt); /* Just use the old space. */
941 /* Set the gateway value. */
942 rt->rt_gateway = (struct sockaddr *)(space + dlen);
943 bcopy(gate, rt->rt_gateway, glen);
945 if (oldspace != NULL) {
947 * If we allocated a new chunk, preserve the original dst.
948 * This way, rt_setgate() really just sets the gate
949 * and leaves the dst field alone.
951 bcopy(dst, space, dlen);
956 * If there is already a gwroute, it's now almost definitely wrong
959 if (rt->rt_gwroute != NULL) {
960 RTFREE(rt->rt_gwroute);
961 rt->rt_gwroute = NULL;
963 if (rt->rt_flags & RTF_GATEWAY) {
965 * Cloning loop avoidance: In the presence of
966 * protocol-cloning and bad configuration, it is
967 * possible to get stuck in bottomless mutual recursion
968 * (rtrequest rt_setgate rtlookup). We avoid this
969 * by not allowing protocol-cloning to operate for
970 * gateways (which is probably the correct choice
971 * anyway), and avoid the resulting reference loops
972 * by disallowing any route to run through itself as
973 * a gateway. This is obviously mandatory when we
974 * get rt->rt_output().
976 * This breaks TTCP for hosts outside the gateway! XXX JH
978 rt->rt_gwroute = _rtlookup(gate, RTL_REPORTMSG, RTF_PRCLONING);
979 if (rt->rt_gwroute == rt) {
980 rt->rt_gwroute = NULL;
982 return EDQUOT; /* failure */
987 * This isn't going to do anything useful for host routes, so
988 * don't bother. Also make sure we have a reasonable mask
989 * (we don't yet have one during adds).
991 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
992 struct rtfc_arg arg = { rt, rnh };
994 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1004 struct sockaddr *src,
1005 struct sockaddr *dst,
1006 struct sockaddr *netmask)
1008 u_char *cp1 = (u_char *)src;
1009 u_char *cp2 = (u_char *)dst;
1010 u_char *cp3 = (u_char *)netmask;
1011 u_char *cplim = cp2 + *cp3;
1012 u_char *cplim2 = cp2 + *cp1;
1014 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1019 *cp2++ = *cp1++ & *cp3++;
1021 bzero(cp2, cplim2 - cp2);
1025 rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt)
1027 struct rtentry *up_rt, *rt;
1029 if (!(rt0->rt_flags & RTF_UP)) {
1030 up_rt = rtlookup(dst);
1032 return (EHOSTUNREACH);
1036 if (up_rt->rt_flags & RTF_GATEWAY) {
1037 if (up_rt->rt_gwroute == NULL) {
1038 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1039 if (up_rt->rt_gwroute == NULL)
1040 return (EHOSTUNREACH);
1041 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) {
1042 rtfree(up_rt->rt_gwroute);
1043 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
1044 if (up_rt->rt_gwroute == NULL)
1045 return (EHOSTUNREACH);
1047 rt = up_rt->rt_gwroute;
1050 if (rt->rt_flags & RTF_REJECT &&
1051 (rt->rt_rmx.rmx_expire == 0 || /* rt doesn't expire */
1052 time_second < rt->rt_rmx.rmx_expire)) /* rt not expired */
1053 return (rt->rt_flags & RTF_HOST ? EHOSTDOWN : EHOSTUNREACH);
1059 * Set up a routing table entry, normally for an interface.
1062 rtinit(struct ifaddr *ifa, int cmd, int flags)
1064 struct sockaddr *dst, *deldst, *netmask;
1066 struct mbuf *m = NULL;
1067 struct radix_node_head *rnh;
1068 struct radix_node *rn;
1069 struct rt_addrinfo rtinfo;
1072 if (flags & RTF_HOST) {
1073 dst = ifa->ifa_dstaddr;
1076 dst = ifa->ifa_addr;
1077 netmask = ifa->ifa_netmask;
1080 * If it's a delete, check that if it exists, it's on the correct
1081 * interface or we might scrub a route to another ifa which would
1082 * be confusing at best and possibly worse.
1084 if (cmd == RTM_DELETE) {
1086 * It's a delete, so it should already exist..
1087 * If it's a net, mask off the host bits
1088 * (Assuming we have a mask)
1090 if (netmask != NULL) {
1091 m = m_get(MB_DONTWAIT, MT_SONAME);
1094 deldst = mtod(m, struct sockaddr *);
1095 rt_maskedcopy(dst, deldst, netmask);
1099 * Look up an rtentry that is in the routing tree and
1100 * contains the correct info.
1102 if ((rnh = rt_tables[dst->sa_family]) == NULL ||
1103 (rn = rnh->rnh_lookup((char *)dst,
1104 (char *)netmask, rnh)) == NULL ||
1105 ((struct rtentry *)rn)->rt_ifa != ifa ||
1106 !sa_equal((struct sockaddr *)rn->rn_key, dst)) {
1109 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1115 * One would think that as we are deleting, and we know
1116 * it doesn't exist, we could just return at this point
1117 * with an "ELSE" clause, but apparently not..
1119 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1124 * Do the actual request
1126 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1127 rtinfo.rti_info[RTAX_DST] = dst;
1128 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1129 rtinfo.rti_info[RTAX_NETMASK] = netmask;
1130 rtinfo.rti_flags = flags | ifa->ifa_flags;
1131 rtinfo.rti_ifa = ifa;
1132 error = rtrequest1(cmd, &rtinfo, &rt);
1133 if (error == 0 && rt != NULL) {
1135 * notify any listening routing agents of the change
1137 rt_newaddrmsg(cmd, ifa, error, rt);
1138 if (cmd == RTM_DELETE) {
1140 * If we are deleting, and we found an entry, then
1141 * it's been removed from the tree.. now throw it away.
1143 if (rt->rt_refcnt == 0) {
1144 rt->rt_refcnt++; /* make a 1->0 transition */
1147 } else if (cmd == RTM_ADD) {
1149 * We just wanted to add it.. we don't actually
1160 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1161 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);