2 * Copyright (c) 1980, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95
34 #include "opt_mrouting.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/domain.h>
42 #include <sys/kernel.h>
45 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/ip_mroute.h>
52 static struct rtstat rtstat;
53 struct radix_node_head *rt_tables[AF_MAX+1];
55 static int rttrash; /* routes not in table but not freed */
57 static void rt_maskedcopy(struct sockaddr *,
58 struct sockaddr *, struct sockaddr *);
59 static void rtable_init(void **);
61 /* compare two sockaddr structures */
62 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
65 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
66 * The operation can be done safely (in this code) because a
67 * 'struct rtentry' starts with two 'struct radix_node''s, the first
68 * one representing leaf nodes in the routing tree, which is
69 * what the code in radix.c passes us as a 'struct radix_node'.
71 * But because there are a lot of assumptions in this conversion,
72 * do not cast explicitly, but always use the macro below.
74 #define RNTORT(p) ((struct rtentry *)(p))
77 rtable_init(void **table)
80 for (dom = domains; dom; dom = dom->dom_next)
81 if (dom->dom_rtattach)
82 dom->dom_rtattach(&table[dom->dom_family],
86 static uma_zone_t rtzone; /* Routing table UMA zone. */
91 rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
92 NULL, NULL, UMA_ALIGN_PTR, 0);
93 rn_init(); /* initialize all zeroes, all ones, mask table */
94 rtable_init((void **)rt_tables);
98 * Packet routing routines.
101 rtalloc(struct route *ro)
103 rtalloc_ign(ro, 0UL);
107 rtalloc_ign(struct route *ro, u_long ignore)
111 if ((rt = ro->ro_rt) != NULL) {
112 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
117 ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore);
119 RT_UNLOCK(ro->ro_rt);
123 * Look up the route that matches the address given
124 * Or, at least try.. Create a cloned route if needed.
126 * The returned route, if any, is locked.
129 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
131 struct radix_node_head *rnh = rt_tables[dst->sa_family];
133 struct radix_node *rn;
134 struct rtentry *newrt;
135 struct rt_addrinfo info;
137 int err = 0, msgtype = RTM_MISS;
140 bzero(&info, sizeof(info));
142 * Look up the address in the table for that Address Family
145 rtstat.rts_unreach++;
148 RADIX_NODE_HEAD_LOCK(rnh);
149 if ((rn = rnh->rnh_matchaddr(dst, rnh)) &&
150 (rn->rn_flags & RNF_ROOT) == 0) {
152 * If we find it and it's not the root node, then
153 * get a refernce on the rtentry associated.
155 newrt = rt = RNTORT(rn);
156 nflags = rt->rt_flags & ~ignflags;
157 if (report && (nflags & RTF_CLONING)) {
159 * We are apparently adding (report = 0 in delete).
160 * If it requires that it be cloned, do so.
161 * (This implies it wasn't a HOST route.)
163 err = rtrequest(RTM_RESOLVE, dst, NULL,
167 * If the cloning didn't succeed, maybe
168 * what we have will do. Return that.
170 newrt = rt; /* existing route */
175 KASSERT(newrt, ("no route and no error"));
177 if (newrt->rt_flags & RTF_XRESOLVE) {
179 * If the new route specifies it be
180 * externally resolved, then go do that.
182 msgtype = RTM_RESOLVE;
185 /* Inform listeners of the new route. */
186 info.rti_info[RTAX_DST] = rt_key(newrt);
187 info.rti_info[RTAX_NETMASK] = rt_mask(newrt);
188 info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway;
189 if (newrt->rt_ifp != NULL) {
190 info.rti_info[RTAX_IFP] =
191 ifaddr_byindex(newrt->rt_ifp->if_index)->ifa_addr;
192 info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr;
194 rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0);
196 KASSERT(rt == newrt, ("locking wrong route"));
200 RADIX_NODE_HEAD_UNLOCK(rnh);
203 * Either we hit the root or couldn't find any match,
204 * Which basically means
205 * "caint get there frm here"
207 rtstat.rts_unreach++;
209 RADIX_NODE_HEAD_UNLOCK(rnh);
212 * If required, report the failure to the supervising
214 * For a delete, this is not an error. (report == 0)
216 info.rti_info[RTAX_DST] = dst;
217 rt_missmsg(msgtype, &info, 0, err);
221 RT_LOCK_ASSERT(newrt);
226 * Remove a reference count from an rtentry.
227 * If the count gets low enough, take it out of the routing table
230 rtfree(struct rtentry *rt)
232 struct radix_node_head *rnh;
234 /* XXX the NULL checks are probably useless */
236 panic("rtfree: NULL rt");
237 rnh = rt_tables[rt_key(rt)->sa_family];
239 panic("rtfree: NULL rnh");
244 * decrement the reference count by one and if it reaches 0,
245 * and there is a close function defined, call the close function
248 if (rt->rt_refcnt > 0)
252 * On last reference give the "close method" a chance
253 * to cleanup private state. This also permits (for
254 * IPv4 and IPv6) a chance to decide if the routing table
255 * entry should be purged immediately or at a later time.
256 * When an immediate purge is to happen the close routine
257 * typically calls rtexpunge which clears the RTF_UP flag
258 * on the entry so that the code below reclaims the storage.
260 if (rt->rt_refcnt == 0 && rnh->rnh_close)
261 rnh->rnh_close((struct radix_node *)rt, rnh);
264 * If we are no longer "up" (and ref == 0)
265 * then we can free the resources associated
268 if ((rt->rt_flags & RTF_UP) == 0) {
269 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
272 * the rtentry must have been removed from the routing table
273 * so it is represented in rttrash.. remove that now.
277 if (rt->rt_refcnt < 0) {
278 printf("rtfree: %p not freed (neg refs)\n", rt);
283 * release references on items we hold them on..
284 * e.g other routes and ifaddrs.
288 rt->rt_parent = NULL; /* NB: no refcnt on parent */
291 * The key is separatly alloc'd so free it (see rt_setgate()).
292 * This also frees the gateway, as they are always malloc'd
298 * and the rtentry itself of course
301 uma_zfree(rtzone, rt);
310 * Force a routing table entry to the specified
311 * destination to go through the given gateway.
312 * Normally called as a result of a routing redirect
313 * message from the network layer.
316 rtredirect(struct sockaddr *dst,
317 struct sockaddr *gateway,
318 struct sockaddr *netmask,
320 struct sockaddr *src)
325 struct rt_addrinfo info;
328 /* verify the gateway is directly reachable */
329 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
333 rt = rtalloc1(dst, 0, 0UL); /* NB: rt is locked */
335 * If the redirect isn't from our current router for this dst,
336 * it's either old or wrong. If it redirects us to ourselves,
337 * we have a routing loop, perhaps as a result of an interface
338 * going down recently.
340 if (!(flags & RTF_DONE) && rt &&
341 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
343 else if (ifa_ifwithaddr(gateway))
344 error = EHOSTUNREACH;
348 * Create a new entry if we just got back a wildcard entry
349 * or the the lookup failed. This is necessary for hosts
350 * which use routing redirects generated by smart gateways
351 * to dynamically build the routing tables.
353 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
356 * Don't listen to the redirect if it's
357 * for a route to an interface.
359 if (rt->rt_flags & RTF_GATEWAY) {
360 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
362 * Changing from route to net => route to host.
363 * Create new route, rather than smashing route to net.
368 flags |= RTF_GATEWAY | RTF_DYNAMIC;
369 bzero((caddr_t)&info, sizeof(info));
370 info.rti_info[RTAX_DST] = dst;
371 info.rti_info[RTAX_GATEWAY] = gateway;
372 info.rti_info[RTAX_NETMASK] = netmask;
374 info.rti_flags = flags;
376 error = rtrequest1(RTM_ADD, &info, &rt);
379 flags = rt->rt_flags;
381 stat = &rtstat.rts_dynamic;
384 * Smash the current notion of the gateway to
385 * this destination. Should check about netmask!!!
387 rt->rt_flags |= RTF_MODIFIED;
388 flags |= RTF_MODIFIED;
389 stat = &rtstat.rts_newgateway;
391 * add the key and gateway (in one malloc'd chunk).
393 rt_setgate(rt, rt_key(rt), gateway);
396 error = EHOSTUNREACH;
402 rtstat.rts_badredirect++;
403 else if (stat != NULL)
405 bzero((caddr_t)&info, sizeof(info));
406 info.rti_info[RTAX_DST] = dst;
407 info.rti_info[RTAX_GATEWAY] = gateway;
408 info.rti_info[RTAX_NETMASK] = netmask;
409 info.rti_info[RTAX_AUTHOR] = src;
410 rt_missmsg(RTM_REDIRECT, &info, flags, error);
414 * Routing table ioctl interface.
417 rtioctl(u_long req, caddr_t data)
421 * If more ioctl commands are added here, make sure the proper
422 * super-user checks are being performed because it is possible for
423 * prison-root to make it this far if raw sockets have been enabled
427 /* Multicast goop, grrr... */
428 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
435 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
437 register struct ifaddr *ifa;
439 if ((flags & RTF_GATEWAY) == 0) {
441 * If we are adding a route to an interface,
442 * and the interface is a pt to pt link
443 * we should search for the destination
444 * as our clue to the interface. Otherwise
445 * we can use the local address.
448 if (flags & RTF_HOST)
449 ifa = ifa_ifwithdstaddr(dst);
451 ifa = ifa_ifwithaddr(gateway);
454 * If we are adding a route to a remote net
455 * or host, the gateway may still be on the
456 * other end of a pt to pt link.
458 ifa = ifa_ifwithdstaddr(gateway);
461 ifa = ifa_ifwithnet(gateway);
463 struct rtentry *rt = rtalloc1(gateway, 0, 0UL);
468 if ((ifa = rt->rt_ifa) == NULL)
471 if (ifa->ifa_addr->sa_family != dst->sa_family) {
472 struct ifaddr *oifa = ifa;
473 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
480 static walktree_f_t rt_fixdelete;
481 static walktree_f_t rt_fixchange;
485 struct radix_node_head *rnh;
489 * Do appropriate manipulations of a routing tree given
490 * all the bits of info needed
494 struct sockaddr *dst,
495 struct sockaddr *gateway,
496 struct sockaddr *netmask,
498 struct rtentry **ret_nrt)
500 struct rt_addrinfo info;
502 bzero((caddr_t)&info, sizeof(info));
503 info.rti_flags = flags;
504 info.rti_info[RTAX_DST] = dst;
505 info.rti_info[RTAX_GATEWAY] = gateway;
506 info.rti_info[RTAX_NETMASK] = netmask;
507 return rtrequest1(req, &info, ret_nrt);
511 * These (questionable) definitions of apparent local variables apply
512 * to the next two functions. XXXXXX!!!
514 #define dst info->rti_info[RTAX_DST]
515 #define gateway info->rti_info[RTAX_GATEWAY]
516 #define netmask info->rti_info[RTAX_NETMASK]
517 #define ifaaddr info->rti_info[RTAX_IFA]
518 #define ifpaddr info->rti_info[RTAX_IFP]
519 #define flags info->rti_flags
522 rt_getifa(struct rt_addrinfo *info)
528 * ifp may be specified by sockaddr_dl
529 * when protocol address is ambiguous.
531 if (info->rti_ifp == NULL && ifpaddr != NULL &&
532 ifpaddr->sa_family == AF_LINK &&
533 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
534 info->rti_ifp = ifa->ifa_ifp;
535 if (info->rti_ifa == NULL && ifaaddr != NULL)
536 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
537 if (info->rti_ifa == NULL) {
540 sa = ifaaddr != NULL ? ifaaddr :
541 (gateway != NULL ? gateway : dst);
542 if (sa != NULL && info->rti_ifp != NULL)
543 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
544 else if (dst != NULL && gateway != NULL)
545 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
547 info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
549 if ((ifa = info->rti_ifa) != NULL) {
550 if (info->rti_ifp == NULL)
551 info->rti_ifp = ifa->ifa_ifp;
558 * Expunges references to a route that's about to be reclaimed.
559 * The route must be locked.
562 rtexpunge(struct rtentry *rt)
564 struct radix_node *rn;
565 struct radix_node_head *rnh;
572 * We cannot assume anything about the reference count
573 * because protocols call us in many situations; often
574 * before unwinding references to the table entry.
576 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
579 * Find the correct routing tree to use for this Address Family
581 rnh = rt_tables[rt_key(rt)->sa_family];
583 return (EAFNOSUPPORT);
585 RADIX_NODE_HEAD_LOCK(rnh);
588 * Remove the item from the tree; it should be there,
589 * but when callers invoke us blindly it may not (sigh).
591 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
596 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
597 ("unexpected flags 0x%x", rn->rn_flags));
598 KASSERT(rt == RNTORT(rn),
599 ("lookup mismatch, rt %p rn %p", rt, rn));
601 rt->rt_flags &= ~RTF_UP;
604 * Now search what's left of the subtree for any cloned
605 * routes which might have been formed from this node.
607 if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt))
608 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
612 * Remove any external references we may have.
613 * This might result in another rtentry being freed if
614 * we held its last reference.
616 if (rt->rt_gwroute) {
617 RTFREE(rt->rt_gwroute);
618 rt->rt_gwroute = NULL;
622 * Give the protocol a chance to keep things in sync.
624 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
625 struct rt_addrinfo info;
627 bzero((caddr_t)&info, sizeof(info));
628 info.rti_flags = rt->rt_flags;
629 info.rti_info[RTAX_DST] = rt_key(rt);
630 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
631 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
632 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
636 * one more rtentry floating around that is not
637 * linked to the routing table.
641 RADIX_NODE_HEAD_UNLOCK(rnh);
646 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
649 register struct rtentry *rt;
650 register struct radix_node *rn;
651 register struct radix_node_head *rnh;
653 struct sockaddr *ndst;
654 #define senderr(x) { error = x ; goto bad; }
657 * Find the correct routing tree to use for this Address Family
659 rnh = rt_tables[dst->sa_family];
661 return (EAFNOSUPPORT);
662 RADIX_NODE_HEAD_LOCK(rnh);
664 * If we are adding a host route then we don't want to put
665 * a netmask in the tree, nor do we want to clone it.
667 if (flags & RTF_HOST) {
669 flags &= ~RTF_CLONING;
674 * Remove the item from the tree and return it.
675 * Complain if it is not there and do no more processing.
677 rn = rnh->rnh_deladdr(dst, netmask, rnh);
680 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
681 panic ("rtrequest delete");
685 rt->rt_flags &= ~RTF_UP;
688 * Now search what's left of the subtree for any cloned
689 * routes which might have been formed from this node.
691 if ((rt->rt_flags & RTF_CLONING) &&
693 rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
698 * Remove any external references we may have.
699 * This might result in another rtentry being freed if
700 * we held its last reference.
702 if (rt->rt_gwroute) {
703 RTFREE(rt->rt_gwroute);
704 rt->rt_gwroute = NULL;
708 * give the protocol a chance to keep things in sync.
710 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
711 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
714 * One more rtentry floating around that is not
715 * linked to the routing table. rttrash will be decremented
716 * when RTFREE(rt) is eventually called.
721 * If the caller wants it, then it can have it,
722 * but it's up to it to free the rtentry as we won't be
733 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
737 flags = rt->rt_flags &
738 ~(RTF_CLONING | RTF_STATIC);
739 flags |= RTF_WASCLONED;
740 gateway = rt->rt_gateway;
741 if ((netmask = rt->rt_genmask) == NULL)
746 if ((flags & RTF_GATEWAY) && !gateway)
747 panic("rtrequest: GATEWAY but no gateway");
749 if (info->rti_ifa == NULL && (error = rt_getifa(info)))
754 rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
758 rt->rt_flags = RTF_UP | flags;
760 * Add the gateway. Possibly re-malloc-ing the storage for it
761 * also add the rt_gwroute if possible.
764 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
766 uma_zfree(rtzone, rt);
771 * point to the (possibly newly malloc'd) dest address.
773 ndst = (struct sockaddr *)rt_key(rt);
776 * make sure it contains the value we want (masked if needed).
779 rt_maskedcopy(dst, ndst, netmask);
781 bcopy(dst, ndst, dst->sa_len);
784 * Note that we now have a reference to the ifa.
785 * This moved from below so that rnh->rnh_addaddr() can
786 * examine the ifa and ifa->ifa_ifp if it so desires.
790 rt->rt_ifp = ifa->ifa_ifp;
792 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
793 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
797 * Uh-oh, we already have one of these in the tree.
798 * We do a special hack: if the route that's already
799 * there was generated by the cloning mechanism
800 * then we just blow it away and retry the insertion
803 rt2 = rtalloc1(dst, 0, 0);
804 if (rt2 && rt2->rt_parent) {
807 rn = rnh->rnh_addaddr(ndst, netmask,
810 /* undo the extra ref we got */
816 * If it still failed to go into the tree,
817 * then un-make it (this should be a function)
821 RTFREE(rt->rt_gwroute);
826 uma_zfree(rtzone, rt);
830 rt->rt_parent = NULL;
833 * If we got here from RESOLVE, then we are cloning
834 * so clone the rest, and note that we
835 * are a clone (and increment the parent's references)
837 if (req == RTM_RESOLVE) {
838 KASSERT(ret_nrt && *ret_nrt,
839 ("no route to clone from"));
840 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
841 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
842 if ((*ret_nrt)->rt_flags & RTF_CLONING) {
844 * NB: We do not bump the refcnt on the parent
845 * entry under the assumption that it will
846 * remain so long as we do. This is
847 * important when deleting the parent route
848 * as this operation requires traversing
849 * the tree to delete all clones and futzing
850 * with refcnts requires us to double-lock
851 * parent through this back reference.
853 rt->rt_parent = *ret_nrt;
858 * if this protocol has something to add to this then
859 * allow it to do that as well.
861 if (ifa->ifa_rtrequest)
862 ifa->ifa_rtrequest(req, rt, info);
865 * We repeat the same procedure from rt_setgate() here because
866 * it doesn't fire when we call it there because the node
867 * hasn't been added to the tree yet.
869 if (req == RTM_ADD &&
870 !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
874 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
879 * actually return a resultant rtentry and
880 * give the caller a single reference.
892 RADIX_NODE_HEAD_UNLOCK(rnh);
905 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
906 * (i.e., the routes related to it by the operation of cloning). This
907 * routine is iterated over all potential former-child-routes by way of
908 * rnh->rnh_walktree_from() above, and those that actually are children of
909 * the late parent (passed in as VP here) are themselves deleted.
912 rt_fixdelete(struct radix_node *rn, void *vp)
914 struct rtentry *rt = RNTORT(rn);
915 struct rtentry *rt0 = vp;
917 if (rt->rt_parent == rt0 &&
918 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) {
919 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
926 * This routine is called from rt_setgate() to do the analogous thing for
927 * adds and changes. There is the added complication in this case of a
928 * middle insert; i.e., insertion of a new network route between an older
929 * network route and (cloned) host routes. For this reason, a simple check
930 * of rt->rt_parent is insufficient; each candidate route must be tested
931 * against the (mask, value) of the new route (passed as before in vp)
932 * to see if the new route matches it.
934 * XXX - it may be possible to do fixdelete() for changes and reserve this
935 * routine just for adds. I'm not sure why I thought it was necessary to do
940 rt_fixchange(struct radix_node *rn, void *vp)
942 struct rtentry *rt = RNTORT(rn);
943 struct rtfc_arg *ap = vp;
944 struct rtentry *rt0 = ap->rt0;
945 struct radix_node_head *rnh = ap->rnh;
946 u_char *xk1, *xm1, *xk2, *xmp;
949 /* make sure we have a parent, and route is not pinned or cloning */
950 if (!rt->rt_parent ||
951 (rt->rt_flags & (RTF_PINNED | RTF_CLONING)))
954 if (rt->rt_parent == rt0) /* parent match */
957 * There probably is a function somewhere which does this...
958 * if not, there should be.
960 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
962 xk1 = (u_char *)rt_key(rt0);
963 xm1 = (u_char *)rt_mask(rt0);
964 xk2 = (u_char *)rt_key(rt);
966 /* avoid applying a less specific route */
967 xmp = (u_char *)rt_mask(rt->rt_parent);
968 mlen = rt_key(rt->rt_parent)->sa_len;
969 if (mlen > rt_key(rt0)->sa_len) /* less specific route */
971 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++)
972 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i])
973 return 0; /* less specific route */
975 for (i = rnh->rnh_treetop->rn_offset; i < len; i++)
976 if ((xk2[i] & xm1[i]) != xk1[i])
977 return 0; /* no match */
980 * OK, this node is a clone, and matches the node currently being
981 * changed/added under the node's mask. So, get rid of it.
984 return rtrequest(RTM_DELETE, rt_key(rt), NULL,
985 rt_mask(rt), rt->rt_flags, NULL);
989 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
991 /* XXX dst may be overwritten, can we move this to below */
992 struct radix_node_head *rnh = rt_tables[dst->sa_family];
993 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
998 * A host route with the destination equal to the gateway
999 * will interfere with keeping LLINFO in the routing
1000 * table, so disallow it.
1002 if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
1003 (RTF_HOST|RTF_GATEWAY)) &&
1004 dst->sa_len == gate->sa_len &&
1005 bcmp(dst, gate, dst->sa_len) == 0) {
1007 * The route might already exist if this is an RTM_CHANGE
1008 * or a routing redirect, so try to delete it.
1012 return EADDRNOTAVAIL;
1016 * Prepare to store the gateway in rt->rt_gateway.
1017 * Both dst and gateway are stored one after the other in the same
1018 * malloc'd chunk. If we have room, we can reuse the old buffer,
1019 * rt_gateway already points to the right place.
1020 * Otherwise, malloc a new block and update the 'dst' address.
1022 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1025 R_Malloc(new, caddr_t, dlen + glen);
1029 * XXX note, we copy from *dst and not *rt_key(rt) because
1030 * rt_setgate() can be called to initialize a newly
1031 * allocated route entry, in which case rt_key(rt) == NULL
1032 * (and also rt->rt_gateway == NULL).
1033 * Free()/free() handle a NULL argument just fine.
1035 bcopy(dst, new, dlen);
1036 Free(rt_key(rt)); /* free old block, if any */
1037 rt_key(rt) = (struct sockaddr *)new;
1038 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1042 * Copy the new gateway value into the memory chunk.
1044 bcopy(gate, rt->rt_gateway, glen);
1047 * If there is already a gwroute, it's now almost definitly wrong
1050 if (rt->rt_gwroute != NULL) {
1051 RTFREE(rt->rt_gwroute);
1052 rt->rt_gwroute = NULL;
1055 * Cloning loop avoidance:
1056 * In the presence of protocol-cloning and bad configuration,
1057 * it is possible to get stuck in bottomless mutual recursion
1058 * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing
1059 * protocol-cloning to operate for gateways (which is probably the
1060 * correct choice anyway), and avoid the resulting reference loops
1061 * by disallowing any route to run through itself as a gateway.
1062 * This is obviously mandatory when we get rt->rt_output().
1063 * XXX: After removal of PRCLONING this is probably not needed anymore.
1065 if (rt->rt_flags & RTF_GATEWAY) {
1066 struct rtentry *gwrt;
1068 RT_UNLOCK(rt); /* XXX workaround LOR */
1069 gwrt = rtalloc1(gate, 1, 0);
1071 rt->rt_gwroute = gwrt;
1072 if (rt->rt_gwroute == rt) {
1073 RTFREE_LOCKED(rt->rt_gwroute);
1074 rt->rt_gwroute = NULL;
1075 return EDQUOT; /* failure */
1077 if (rt->rt_gwroute != NULL)
1078 RT_UNLOCK(rt->rt_gwroute);
1082 * This isn't going to do anything useful for host routes, so
1083 * don't bother. Also make sure we have a reasonable mask
1084 * (we don't yet have one during adds).
1086 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1087 struct rtfc_arg arg;
1091 RT_UNLOCK(rt); /* XXX workaround LOR */
1092 RADIX_NODE_HEAD_LOCK(rnh);
1094 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1095 rt_fixchange, &arg);
1096 RADIX_NODE_HEAD_UNLOCK(rnh);
1103 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1105 register u_char *cp1 = (u_char *)src;
1106 register u_char *cp2 = (u_char *)dst;
1107 register u_char *cp3 = (u_char *)netmask;
1108 u_char *cplim = cp2 + *cp3;
1109 u_char *cplim2 = cp2 + *cp1;
1111 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1116 *cp2++ = *cp1++ & *cp3++;
1118 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1122 * Set up a routing table entry, normally
1126 rtinit(struct ifaddr *ifa, int cmd, int flags)
1128 struct sockaddr *dst;
1129 struct sockaddr *netmask;
1130 struct mbuf *m = NULL;
1131 struct rtentry *rt = NULL;
1132 struct rt_addrinfo info;
1135 if (flags & RTF_HOST) {
1136 dst = ifa->ifa_dstaddr;
1139 dst = ifa->ifa_addr;
1140 netmask = ifa->ifa_netmask;
1143 * If it's a delete, check that if it exists, it's on the correct
1144 * interface or we might scrub a route to another ifa which would
1145 * be confusing at best and possibly worse.
1147 if (cmd == RTM_DELETE) {
1148 struct sockaddr *deldst;
1149 struct radix_node_head *rnh;
1150 struct radix_node *rn;
1153 * It's a delete, so it should already exist..
1154 * If it's a net, mask off the host bits
1155 * (Assuming we have a mask)
1157 if (netmask != NULL) {
1158 m = m_get(M_DONTWAIT, MT_SONAME);
1161 deldst = mtod(m, struct sockaddr *);
1162 rt_maskedcopy(dst, deldst, netmask);
1166 * Look up an rtentry that is in the routing tree and
1167 * contains the correct info.
1169 if ((rnh = rt_tables[dst->sa_family]) == NULL)
1171 RADIX_NODE_HEAD_LOCK(rnh);
1172 error = ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL ||
1173 (rn->rn_flags & RNF_ROOT) ||
1174 RNTORT(rn)->rt_ifa != ifa ||
1175 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1176 RADIX_NODE_HEAD_UNLOCK(rnh);
1181 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1185 * Do the actual request
1187 bzero((caddr_t)&info, sizeof(info));
1189 info.rti_flags = flags | ifa->ifa_flags;
1190 info.rti_info[RTAX_DST] = dst;
1191 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1192 info.rti_info[RTAX_NETMASK] = netmask;
1193 error = rtrequest1(cmd, &info, &rt);
1194 if (error == 0 && rt != NULL) {
1196 * notify any listening routing agents of the change
1199 rt_newaddrmsg(cmd, ifa, error, rt);
1200 if (cmd == RTM_DELETE) {
1202 * If we are deleting, and we found an entry, then
1203 * it's been removed from the tree.. now throw it away.
1207 if (cmd == RTM_ADD) {
1209 * We just wanted to add it.. we don't actually
1223 * rt_check() is invoked on each layer 2 output path, prior to
1224 * encapsulating outbound packets.
1226 * The function is mostly used to find a routing entry for the gateway,
1227 * which in some protocol families could also point to the link-level
1228 * address for the gateway itself (the side effect of revalidating the
1229 * route to the destination is rather pointless at this stage, we did it
1230 * already a moment before in the pr_output() routine to locate the ifp
1231 * and gateway to use).
1233 * When we remove the layer-3 to layer-2 mapping tables from the
1234 * routing table, this function can be removed.
1237 * *dst is the address of the NEXT HOP (which coincides with the
1238 * final destination if directly reachable);
1239 * *lrt0 points to the cached route to the final destination;
1240 * *lrt is not meaningful;
1243 * If the route is marked down try to find a new route. If the route
1244 * to the gateway is gone, try to setup a new route. Otherwise,
1245 * if the route is marked for packets to be rejected, enforce that.
1248 * *dst is unchanged;
1249 * *lrt0 points to the (possibly new) route to the final destination
1250 * *lrt points to the route to the next hop
1252 * Their values are meaningful ONLY if no error is returned.
1255 rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst)
1257 #define senderr(x) { error = x ; goto bad; }
1259 struct rtentry *rt0;
1265 /* NB: the locking here is tortuous... */
1267 if ((rt->rt_flags & RTF_UP) == 0) {
1269 rt = rtalloc1(dst, 1, 0UL);
1272 /* XXX what about if change? */
1274 senderr(EHOSTUNREACH);
1277 /* XXX BSD/OS checks dst->sa_family != AF_NS */
1278 if (rt->rt_flags & RTF_GATEWAY) {
1279 if (rt->rt_gwroute == NULL)
1281 rt = rt->rt_gwroute;
1282 RT_LOCK(rt); /* NB: gwroute */
1283 if ((rt->rt_flags & RTF_UP) == 0) {
1284 rtfree(rt); /* unlock gwroute */
1288 rt = rtalloc1(rt->rt_gateway, 1, 0UL);
1290 rt0->rt_gwroute = rt;
1293 senderr(EHOSTUNREACH);
1298 /* XXX why are we inspecting rmx_expire? */
1299 error = (rt->rt_flags & RTF_REJECT) &&
1300 (rt->rt_rmx.rmx_expire == 0 ||
1301 time_second < rt->rt_rmx.rmx_expire);
1304 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1306 *lrt = rt; /* NB: return unlocked */
1310 /* NB: lrt and lrt0 should not be interpreted if error is non-zero */
1315 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1316 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);