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
32 /************************************************************************
33 * Note: In this file a 'fib' is a "forwarding information base" *
34 * Which is the new name for an in kernel routing (next hop) table. *
35 ***********************************************************************/
38 #include "opt_route.h"
39 #include "opt_mrouting.h"
40 #include "opt_mpath.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/sysproto.h>
50 #include <sys/domain.h>
51 #include <sys/kernel.h>
52 #include <sys/vimage.h>
55 #include <net/route.h>
58 #include <net/radix_mpath.h>
61 #include <netinet/in.h>
62 #include <netinet/ip_mroute.h>
66 u_int rt_numfibs = RT_NUMFIBS;
67 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
69 * Allow the boot code to allow LESS than RT_MAXFIBS to be used.
70 * We can't do more because storage is statically allocated for now.
71 * (for compatibility reasons.. this will change).
73 TUNABLE_INT("net.fibs", &rt_numfibs);
76 * By default add routes to all fibs for new interfaces.
77 * Once this is set to 0 then only allocate routes on interface
78 * changes for the FIB of the caller when adding a new set of addresses
79 * to an interface. XXX this is a shotgun aproach to a problem that needs
80 * a more fine grained solution.. that will come.
82 u_int rt_add_addr_allfibs = 1;
83 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
84 &rt_add_addr_allfibs, 0, "");
85 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
87 static struct rtstat rtstat;
89 /* by default only the first 'row' of tables will be accessed. */
91 * XXXMRT When we fix netstat, and do this differnetly,
92 * we can allocate this dynamically. As long as we are keeping
93 * things backwards compaitble we need to allocate this
96 struct radix_node_head *rt_tables[RT_MAXFIBS][AF_MAX+1];
98 static int rttrash; /* routes not in table but not freed */
100 static void rt_maskedcopy(struct sockaddr *,
101 struct sockaddr *, struct sockaddr *);
103 /* compare two sockaddr structures */
104 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
107 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
108 * The operation can be done safely (in this code) because a
109 * 'struct rtentry' starts with two 'struct radix_node''s, the first
110 * one representing leaf nodes in the routing tree, which is
111 * what the code in radix.c passes us as a 'struct radix_node'.
113 * But because there are a lot of assumptions in this conversion,
114 * do not cast explicitly, but always use the macro below.
116 #define RNTORT(p) ((struct rtentry *)(p))
118 static uma_zone_t rtzone; /* Routing table UMA zone. */
121 /* default fib for tunnels to use */
122 u_int tunnel_fib = 0;
123 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
127 * handler for net.my_fibnum
130 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
135 fibnum = curthread->td_proc->p_fibnum;
136 error = sysctl_handle_int(oidp, &fibnum, 0, req);
140 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
141 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
150 /* whack the tunable ints into line. */
151 if (rt_numfibs > RT_MAXFIBS)
152 rt_numfibs = RT_MAXFIBS;
155 rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
156 NULL, NULL, UMA_ALIGN_PTR, 0);
157 rn_init(); /* initialize all zeroes, all ones, mask table */
159 for (dom = domains; dom; dom = dom->dom_next) {
160 if (dom->dom_rtattach) {
161 for (table = 0; table < rt_numfibs; table++) {
162 if ( (fam = dom->dom_family) == AF_INET ||
164 /* for now only AF_INET has > 1 table */
166 * rtattach will be also called
167 * from vfs_export.c but the
169 * (only for AF_INET and AF_INET6
170 * which don't need it anyhow)
173 (void **)&V_rt_tables[table][fam],
183 #ifndef _SYS_SYSPROTO_H_
189 setfib(struct thread *td, struct setfib_args *uap)
191 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
193 td->td_proc->p_fibnum = uap->fibnum;
198 * Packet routing routines.
201 rtalloc(struct route *ro)
203 rtalloc_ign_fib(ro, 0UL, 0);
207 rtalloc_fib(struct route *ro, u_int fibnum)
209 rtalloc_ign_fib(ro, 0UL, fibnum);
213 rtalloc_ign(struct route *ro, u_long ignore)
217 if ((rt = ro->ro_rt) != NULL) {
218 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
223 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
225 RT_UNLOCK(ro->ro_rt);
229 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
233 if ((rt = ro->ro_rt) != NULL) {
234 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
239 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
241 RT_UNLOCK(ro->ro_rt);
245 * Look up the route that matches the address given
246 * Or, at least try.. Create a cloned route if needed.
248 * The returned route, if any, is locked.
251 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
253 return (rtalloc1_fib(dst, report, ignflags, 0));
257 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
260 INIT_VNET_NET(curvnet);
261 struct radix_node_head *rnh;
263 struct radix_node *rn;
264 struct rtentry *newrt;
265 struct rt_addrinfo info;
267 int err = 0, msgtype = RTM_MISS;
269 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
270 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
272 rnh = V_rt_tables[fibnum][dst->sa_family];
275 * Look up the address in the table for that Address Family
278 V_rtstat.rts_unreach++;
281 RADIX_NODE_HEAD_LOCK(rnh);
282 if ((rn = rnh->rnh_matchaddr(dst, rnh)) &&
283 (rn->rn_flags & RNF_ROOT) == 0) {
285 * If we find it and it's not the root node, then
286 * get a reference on the rtentry associated.
288 newrt = rt = RNTORT(rn);
289 nflags = rt->rt_flags & ~ignflags;
290 if (report && (nflags & RTF_CLONING)) {
292 * We are apparently adding (report = 0 in delete).
293 * If it requires that it be cloned, do so.
294 * (This implies it wasn't a HOST route.)
296 err = rtrequest_fib(RTM_RESOLVE, dst, NULL,
297 NULL, 0, &newrt, fibnum);
300 * If the cloning didn't succeed, maybe
301 * what we have will do. Return that.
303 newrt = rt; /* existing route */
308 KASSERT(newrt, ("no route and no error"));
310 if (newrt->rt_flags & RTF_XRESOLVE) {
312 * If the new route specifies it be
313 * externally resolved, then go do that.
315 msgtype = RTM_RESOLVE;
318 /* Inform listeners of the new route. */
319 bzero(&info, sizeof(info));
320 info.rti_info[RTAX_DST] = rt_key(newrt);
321 info.rti_info[RTAX_NETMASK] = rt_mask(newrt);
322 info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway;
323 if (newrt->rt_ifp != NULL) {
324 info.rti_info[RTAX_IFP] =
325 newrt->rt_ifp->if_addr->ifa_addr;
326 info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr;
328 rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0);
333 RADIX_NODE_HEAD_UNLOCK(rnh);
336 * Either we hit the root or couldn't find any match,
337 * Which basically means
338 * "caint get there frm here"
340 V_rtstat.rts_unreach++;
342 RADIX_NODE_HEAD_UNLOCK(rnh);
345 * If required, report the failure to the supervising
347 * For a delete, this is not an error. (report == 0)
349 bzero(&info, sizeof(info));
350 info.rti_info[RTAX_DST] = dst;
351 rt_missmsg(msgtype, &info, 0, err);
355 RT_LOCK_ASSERT(newrt);
360 * Remove a reference count from an rtentry.
361 * If the count gets low enough, take it out of the routing table
364 rtfree(struct rtentry *rt)
366 INIT_VNET_NET(curvnet);
367 struct radix_node_head *rnh;
369 KASSERT(rt != NULL,("%s: NULL rt", __func__));
370 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
371 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
376 * The callers should use RTFREE_LOCKED() or RTFREE(), so
377 * we should come here exactly with the last reference.
380 if (rt->rt_refcnt > 0) {
381 printf("%s: %p has %lu refs\n", __func__, rt, rt->rt_refcnt);
386 * On last reference give the "close method" a chance
387 * to cleanup private state. This also permits (for
388 * IPv4 and IPv6) a chance to decide if the routing table
389 * entry should be purged immediately or at a later time.
390 * When an immediate purge is to happen the close routine
391 * typically calls rtexpunge which clears the RTF_UP flag
392 * on the entry so that the code below reclaims the storage.
394 if (rt->rt_refcnt == 0 && rnh->rnh_close)
395 rnh->rnh_close((struct radix_node *)rt, rnh);
398 * If we are no longer "up" (and ref == 0)
399 * then we can free the resources associated
402 if ((rt->rt_flags & RTF_UP) == 0) {
403 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
406 * the rtentry must have been removed from the routing table
407 * so it is represented in rttrash.. remove that now.
411 if (rt->rt_refcnt < 0) {
412 printf("rtfree: %p not freed (neg refs)\n", rt);
417 * release references on items we hold them on..
418 * e.g other routes and ifaddrs.
422 rt->rt_parent = NULL; /* NB: no refcnt on parent */
425 * The key is separatly alloc'd so free it (see rt_setgate()).
426 * This also frees the gateway, as they are always malloc'd
432 * and the rtentry itself of course
435 uma_zfree(rtzone, rt);
444 * Force a routing table entry to the specified
445 * destination to go through the given gateway.
446 * Normally called as a result of a routing redirect
447 * message from the network layer.
450 rtredirect(struct sockaddr *dst,
451 struct sockaddr *gateway,
452 struct sockaddr *netmask,
454 struct sockaddr *src)
456 rtredirect_fib(dst, gateway, netmask, flags, src, 0);
460 rtredirect_fib(struct sockaddr *dst,
461 struct sockaddr *gateway,
462 struct sockaddr *netmask,
464 struct sockaddr *src,
467 INIT_VNET_NET(curvnet);
468 struct rtentry *rt, *rt0 = NULL;
471 struct rt_addrinfo info;
474 /* verify the gateway is directly reachable */
475 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
479 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
481 * If the redirect isn't from our current router for this dst,
482 * it's either old or wrong. If it redirects us to ourselves,
483 * we have a routing loop, perhaps as a result of an interface
484 * going down recently.
486 if (!(flags & RTF_DONE) && rt &&
487 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
489 else if (ifa_ifwithaddr(gateway))
490 error = EHOSTUNREACH;
494 * Create a new entry if we just got back a wildcard entry
495 * or the the lookup failed. This is necessary for hosts
496 * which use routing redirects generated by smart gateways
497 * to dynamically build the routing tables.
499 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
502 * Don't listen to the redirect if it's
503 * for a route to an interface.
505 if (rt->rt_flags & RTF_GATEWAY) {
506 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
508 * Changing from route to net => route to host.
509 * Create new route, rather than smashing route to net.
515 flags |= RTF_GATEWAY | RTF_DYNAMIC;
516 bzero((caddr_t)&info, sizeof(info));
517 info.rti_info[RTAX_DST] = dst;
518 info.rti_info[RTAX_GATEWAY] = gateway;
519 info.rti_info[RTAX_NETMASK] = netmask;
521 info.rti_flags = flags;
522 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
525 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
526 flags = rt->rt_flags;
531 stat = &V_rtstat.rts_dynamic;
533 struct rtentry *gwrt;
536 * Smash the current notion of the gateway to
537 * this destination. Should check about netmask!!!
539 rt->rt_flags |= RTF_MODIFIED;
540 flags |= RTF_MODIFIED;
541 stat = &V_rtstat.rts_newgateway;
543 * add the key and gateway (in one malloc'd chunk).
545 rt_setgate(rt, rt_key(rt), gateway);
546 gwrt = rtalloc1(gateway, 1, 0);
547 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
551 error = EHOSTUNREACH;
557 V_rtstat.rts_badredirect++;
558 else if (stat != NULL)
560 bzero((caddr_t)&info, sizeof(info));
561 info.rti_info[RTAX_DST] = dst;
562 info.rti_info[RTAX_GATEWAY] = gateway;
563 info.rti_info[RTAX_NETMASK] = netmask;
564 info.rti_info[RTAX_AUTHOR] = src;
565 rt_missmsg(RTM_REDIRECT, &info, flags, error);
569 rtioctl(u_long req, caddr_t data)
571 return (rtioctl_fib(req, data, 0));
575 * Routing table ioctl interface.
578 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
582 * If more ioctl commands are added here, make sure the proper
583 * super-user checks are being performed because it is possible for
584 * prison-root to make it this far if raw sockets have been enabled
588 /* Multicast goop, grrr... */
589 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
596 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
598 return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
602 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
605 register struct ifaddr *ifa;
608 if ((flags & RTF_GATEWAY) == 0) {
610 * If we are adding a route to an interface,
611 * and the interface is a pt to pt link
612 * we should search for the destination
613 * as our clue to the interface. Otherwise
614 * we can use the local address.
617 if (flags & RTF_HOST)
618 ifa = ifa_ifwithdstaddr(dst);
620 ifa = ifa_ifwithaddr(gateway);
623 * If we are adding a route to a remote net
624 * or host, the gateway may still be on the
625 * other end of a pt to pt link.
627 ifa = ifa_ifwithdstaddr(gateway);
630 ifa = ifa_ifwithnet(gateway);
632 struct rtentry *rt = rtalloc1_fib(gateway, 0, 0UL, fibnum);
636 * dismiss a gateway that is reachable only
637 * through the default router
639 switch (gateway->sa_family) {
641 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
645 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
655 if ((ifa = rt->rt_ifa) == NULL)
658 if (ifa->ifa_addr->sa_family != dst->sa_family) {
659 struct ifaddr *oifa = ifa;
660 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
667 static walktree_f_t rt_fixdelete;
668 static walktree_f_t rt_fixchange;
672 struct radix_node_head *rnh;
676 * Do appropriate manipulations of a routing tree given
677 * all the bits of info needed
681 struct sockaddr *dst,
682 struct sockaddr *gateway,
683 struct sockaddr *netmask,
685 struct rtentry **ret_nrt)
687 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
691 rtrequest_fib(int req,
692 struct sockaddr *dst,
693 struct sockaddr *gateway,
694 struct sockaddr *netmask,
696 struct rtentry **ret_nrt,
699 struct rt_addrinfo info;
701 if (dst->sa_len == 0)
704 bzero((caddr_t)&info, sizeof(info));
705 info.rti_flags = flags;
706 info.rti_info[RTAX_DST] = dst;
707 info.rti_info[RTAX_GATEWAY] = gateway;
708 info.rti_info[RTAX_NETMASK] = netmask;
709 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
713 * These (questionable) definitions of apparent local variables apply
714 * to the next two functions. XXXXXX!!!
716 #define dst info->rti_info[RTAX_DST]
717 #define gateway info->rti_info[RTAX_GATEWAY]
718 #define netmask info->rti_info[RTAX_NETMASK]
719 #define ifaaddr info->rti_info[RTAX_IFA]
720 #define ifpaddr info->rti_info[RTAX_IFP]
721 #define flags info->rti_flags
724 rt_getifa(struct rt_addrinfo *info)
726 return (rt_getifa_fib(info, 0));
730 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
736 * ifp may be specified by sockaddr_dl
737 * when protocol address is ambiguous.
739 if (info->rti_ifp == NULL && ifpaddr != NULL &&
740 ifpaddr->sa_family == AF_LINK &&
741 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
742 info->rti_ifp = ifa->ifa_ifp;
743 if (info->rti_ifa == NULL && ifaaddr != NULL)
744 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
745 if (info->rti_ifa == NULL) {
748 sa = ifaaddr != NULL ? ifaaddr :
749 (gateway != NULL ? gateway : dst);
750 if (sa != NULL && info->rti_ifp != NULL)
751 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
752 else if (dst != NULL && gateway != NULL)
753 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
756 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
759 if ((ifa = info->rti_ifa) != NULL) {
760 if (info->rti_ifp == NULL)
761 info->rti_ifp = ifa->ifa_ifp;
768 * Expunges references to a route that's about to be reclaimed.
769 * The route must be locked.
772 rtexpunge(struct rtentry *rt)
774 INIT_VNET_NET(curvnet);
775 struct radix_node *rn;
776 struct radix_node_head *rnh;
783 * We cannot assume anything about the reference count
784 * because protocols call us in many situations; often
785 * before unwinding references to the table entry.
787 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
790 * Find the correct routing tree to use for this Address Family
792 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
794 return (EAFNOSUPPORT);
796 RADIX_NODE_HEAD_LOCK(rnh);
799 * Remove the item from the tree; it should be there,
800 * but when callers invoke us blindly it may not (sigh).
802 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
807 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
808 ("unexpected flags 0x%x", rn->rn_flags));
809 KASSERT(rt == RNTORT(rn),
810 ("lookup mismatch, rt %p rn %p", rt, rn));
812 rt->rt_flags &= ~RTF_UP;
815 * Now search what's left of the subtree for any cloned
816 * routes which might have been formed from this node.
818 if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt))
819 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
823 * Remove any external references we may have.
824 * This might result in another rtentry being freed if
825 * we held its last reference.
827 if (rt->rt_gwroute) {
828 RTFREE(rt->rt_gwroute);
829 rt->rt_gwroute = NULL;
833 * Give the protocol a chance to keep things in sync.
835 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
836 struct rt_addrinfo info;
838 bzero((caddr_t)&info, sizeof(info));
839 info.rti_flags = rt->rt_flags;
840 info.rti_info[RTAX_DST] = rt_key(rt);
841 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
842 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
843 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
847 * one more rtentry floating around that is not
848 * linked to the routing table.
852 RADIX_NODE_HEAD_UNLOCK(rnh);
857 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
859 return (rtrequest1_fib(req, info, ret_nrt, 0));
863 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
866 INIT_VNET_NET(curvnet);
868 register struct rtentry *rt;
869 register struct radix_node *rn;
870 register struct radix_node_head *rnh;
872 struct sockaddr *ndst;
873 #define senderr(x) { error = x ; goto bad; }
875 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
876 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
879 * Find the correct routing tree to use for this Address Family
881 rnh = V_rt_tables[fibnum][dst->sa_family];
883 return (EAFNOSUPPORT);
884 RADIX_NODE_HEAD_LOCK(rnh);
886 * If we are adding a host route then we don't want to put
887 * a netmask in the tree, nor do we want to clone it.
889 if (flags & RTF_HOST) {
891 flags &= ~RTF_CLONING;
897 * if we got multipath routes, we require users to specify
898 * a matching RTAX_GATEWAY.
900 if (rn_mpath_capable(rnh)) {
901 struct rtentry *rto = NULL;
903 rn = rnh->rnh_matchaddr(dst, rnh);
906 rto = rt = RNTORT(rn);
907 rt = rt_mpath_matchgate(rt, gateway);
911 * this is the first entry in the chain
914 rn = rn_mpath_next((struct radix_node *)rt);
916 * there is another entry, now it's active
921 rto->rt_flags |= RTF_UP;
923 } else if (rt->rt_flags & RTF_GATEWAY) {
925 * For gateway routes, we need to
926 * make sure that we we are deleting
927 * the correct gateway.
928 * rt_mpath_matchgate() does not
929 * check the case when there is only
930 * one route in the chain.
933 (rt->rt_gateway->sa_len != gateway->sa_len ||
934 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
938 * use the normal delete code to remove
944 * if the entry is 2nd and on up
946 if (!rt_mpath_deldup(rto, rt))
947 panic ("rtrequest1: rt_mpath_deldup");
950 rt->rt_flags &= ~RTF_UP;
951 goto deldone; /* done with the RTM_DELETE command */
957 * Remove the item from the tree and return it.
958 * Complain if it is not there and do no more processing.
960 rn = rnh->rnh_deladdr(dst, netmask, rnh);
963 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
964 panic ("rtrequest delete");
968 rt->rt_flags &= ~RTF_UP;
971 * Now search what's left of the subtree for any cloned
972 * routes which might have been formed from this node.
974 if ((rt->rt_flags & RTF_CLONING) &&
976 rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
981 * Remove any external references we may have.
982 * This might result in another rtentry being freed if
983 * we held its last reference.
985 if (rt->rt_gwroute) {
986 RTFREE(rt->rt_gwroute);
987 rt->rt_gwroute = NULL;
991 * give the protocol a chance to keep things in sync.
993 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
994 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1000 * One more rtentry floating around that is not
1001 * linked to the routing table. rttrash will be decremented
1002 * when RTFREE(rt) is eventually called.
1007 * If the caller wants it, then it can have it,
1008 * but it's up to it to free the rtentry as we won't be
1019 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
1023 flags = rt->rt_flags &
1024 ~(RTF_CLONING | RTF_STATIC);
1025 flags |= RTF_WASCLONED;
1026 gateway = rt->rt_gateway;
1027 if ((netmask = rt->rt_genmask) == NULL)
1032 if ((flags & RTF_GATEWAY) && !gateway)
1034 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1035 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1038 if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum)))
1040 ifa = info->rti_ifa;
1043 rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
1047 rt->rt_flags = RTF_UP | flags;
1048 rt->rt_fibnum = fibnum;
1050 * Add the gateway. Possibly re-malloc-ing the storage for it
1051 * also add the rt_gwroute if possible.
1054 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1055 RT_LOCK_DESTROY(rt);
1056 uma_zfree(rtzone, rt);
1061 * point to the (possibly newly malloc'd) dest address.
1063 ndst = (struct sockaddr *)rt_key(rt);
1066 * make sure it contains the value we want (masked if needed).
1069 rt_maskedcopy(dst, ndst, netmask);
1071 bcopy(dst, ndst, dst->sa_len);
1074 * Note that we now have a reference to the ifa.
1075 * This moved from below so that rnh->rnh_addaddr() can
1076 * examine the ifa and ifa->ifa_ifp if it so desires.
1080 rt->rt_ifp = ifa->ifa_ifp;
1083 /* do not permit exactly the same dst/mask/gw pair */
1084 if (rn_mpath_capable(rnh) &&
1085 rt_mpath_conflict(rnh, rt, netmask)) {
1087 RTFREE(rt->rt_gwroute);
1089 IFAFREE(rt->rt_ifa);
1092 RT_LOCK_DESTROY(rt);
1093 uma_zfree(rtzone, rt);
1098 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1099 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1101 struct rtentry *rt2;
1103 * Uh-oh, we already have one of these in the tree.
1104 * We do a special hack: if the route that's already
1105 * there was generated by the cloning mechanism
1106 * then we just blow it away and retry the insertion
1109 rt2 = rtalloc1_fib(dst, 0, 0, fibnum);
1110 if (rt2 && rt2->rt_parent) {
1113 rn = rnh->rnh_addaddr(ndst, netmask,
1116 /* undo the extra ref we got */
1122 * If it still failed to go into the tree,
1123 * then un-make it (this should be a function)
1127 RTFREE(rt->rt_gwroute);
1129 IFAFREE(rt->rt_ifa);
1131 RT_LOCK_DESTROY(rt);
1132 uma_zfree(rtzone, rt);
1136 rt->rt_parent = NULL;
1139 * If we got here from RESOLVE, then we are cloning
1140 * so clone the rest, and note that we
1141 * are a clone (and increment the parent's references)
1143 if (req == RTM_RESOLVE) {
1144 KASSERT(ret_nrt && *ret_nrt,
1145 ("no route to clone from"));
1146 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
1147 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
1148 if ((*ret_nrt)->rt_flags & RTF_CLONING) {
1150 * NB: We do not bump the refcnt on the parent
1151 * entry under the assumption that it will
1152 * remain so long as we do. This is
1153 * important when deleting the parent route
1154 * as this operation requires traversing
1155 * the tree to delete all clones and futzing
1156 * with refcnts requires us to double-lock
1157 * parent through this back reference.
1159 rt->rt_parent = *ret_nrt;
1164 * If this protocol has something to add to this then
1165 * allow it to do that as well.
1167 if (ifa->ifa_rtrequest)
1168 ifa->ifa_rtrequest(req, rt, info);
1171 * We repeat the same procedure from rt_setgate() here because
1172 * it doesn't fire when we call it there because the node
1173 * hasn't been added to the tree yet.
1175 if (req == RTM_ADD &&
1176 !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1177 struct rtfc_arg arg;
1180 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1181 rt_fixchange, &arg);
1185 * actually return a resultant rtentry and
1186 * give the caller a single reference.
1198 RADIX_NODE_HEAD_UNLOCK(rnh);
1211 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1212 * (i.e., the routes related to it by the operation of cloning). This
1213 * routine is iterated over all potential former-child-routes by way of
1214 * rnh->rnh_walktree_from() above, and those that actually are children of
1215 * the late parent (passed in as VP here) are themselves deleted.
1218 rt_fixdelete(struct radix_node *rn, void *vp)
1220 struct rtentry *rt = RNTORT(rn);
1221 struct rtentry *rt0 = vp;
1223 if (rt->rt_parent == rt0 &&
1224 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) {
1225 return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1226 rt->rt_flags, NULL, rt->rt_fibnum);
1232 * This routine is called from rt_setgate() to do the analogous thing for
1233 * adds and changes. There is the added complication in this case of a
1234 * middle insert; i.e., insertion of a new network route between an older
1235 * network route and (cloned) host routes. For this reason, a simple check
1236 * of rt->rt_parent is insufficient; each candidate route must be tested
1237 * against the (mask, value) of the new route (passed as before in vp)
1238 * to see if the new route matches it.
1240 * XXX - it may be possible to do fixdelete() for changes and reserve this
1241 * routine just for adds. I'm not sure why I thought it was necessary to do
1246 rt_fixchange(struct radix_node *rn, void *vp)
1248 struct rtentry *rt = RNTORT(rn);
1249 struct rtfc_arg *ap = vp;
1250 struct rtentry *rt0 = ap->rt0;
1251 struct radix_node_head *rnh = ap->rnh;
1252 u_char *xk1, *xm1, *xk2, *xmp;
1255 /* make sure we have a parent, and route is not pinned or cloning */
1256 if (!rt->rt_parent ||
1257 (rt->rt_flags & (RTF_PINNED | RTF_CLONING)))
1260 if (rt->rt_parent == rt0) /* parent match */
1263 * There probably is a function somewhere which does this...
1264 * if not, there should be.
1266 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1268 xk1 = (u_char *)rt_key(rt0);
1269 xm1 = (u_char *)rt_mask(rt0);
1270 xk2 = (u_char *)rt_key(rt);
1272 /* avoid applying a less specific route */
1273 xmp = (u_char *)rt_mask(rt->rt_parent);
1274 mlen = rt_key(rt->rt_parent)->sa_len;
1275 if (mlen > rt_key(rt0)->sa_len) /* less specific route */
1277 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++)
1278 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i])
1279 return 0; /* less specific route */
1281 for (i = rnh->rnh_treetop->rn_offset; i < len; i++)
1282 if ((xk2[i] & xm1[i]) != xk1[i])
1283 return 0; /* no match */
1286 * OK, this node is a clone, and matches the node currently being
1287 * changed/added under the node's mask. So, get rid of it.
1290 return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL,
1291 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum);
1295 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1297 INIT_VNET_NET(curvnet);
1298 /* XXX dst may be overwritten, can we move this to below */
1299 struct radix_node_head *rnh =
1300 V_rt_tables[rt->rt_fibnum][dst->sa_family];
1301 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1307 * A host route with the destination equal to the gateway
1308 * will interfere with keeping LLINFO in the routing
1309 * table, so disallow it.
1311 if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
1312 (RTF_HOST|RTF_GATEWAY)) &&
1313 dst->sa_len == gate->sa_len &&
1314 bcmp(dst, gate, dst->sa_len) == 0) {
1316 * The route might already exist if this is an RTM_CHANGE
1317 * or a routing redirect, so try to delete it.
1321 return EADDRNOTAVAIL;
1325 * Cloning loop avoidance in case of bad configuration.
1327 if (rt->rt_flags & RTF_GATEWAY) {
1328 struct rtentry *gwrt;
1330 RT_UNLOCK(rt); /* XXX workaround LOR */
1331 gwrt = rtalloc1_fib(gate, 1, 0, rt->rt_fibnum);
1334 return (EADDRINUSE); /* failure */
1337 * Try to reacquire the lock on rt, and if it fails,
1338 * clean state and restart from scratch.
1340 if (!RT_TRYLOCK(rt)) {
1341 RTFREE_LOCKED(gwrt);
1346 * If there is already a gwroute, then drop it. If we
1347 * are asked to replace route with itself, then do
1348 * not leak its refcounter.
1350 if (rt->rt_gwroute != NULL) {
1351 if (rt->rt_gwroute == gwrt) {
1352 RT_REMREF(rt->rt_gwroute);
1354 RTFREE(rt->rt_gwroute);
1357 if ((rt->rt_gwroute = gwrt) != NULL)
1358 RT_UNLOCK(rt->rt_gwroute);
1362 * Prepare to store the gateway in rt->rt_gateway.
1363 * Both dst and gateway are stored one after the other in the same
1364 * malloc'd chunk. If we have room, we can reuse the old buffer,
1365 * rt_gateway already points to the right place.
1366 * Otherwise, malloc a new block and update the 'dst' address.
1368 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1371 R_Malloc(new, caddr_t, dlen + glen);
1375 * XXX note, we copy from *dst and not *rt_key(rt) because
1376 * rt_setgate() can be called to initialize a newly
1377 * allocated route entry, in which case rt_key(rt) == NULL
1378 * (and also rt->rt_gateway == NULL).
1379 * Free()/free() handle a NULL argument just fine.
1381 bcopy(dst, new, dlen);
1382 Free(rt_key(rt)); /* free old block, if any */
1383 rt_key(rt) = (struct sockaddr *)new;
1384 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1388 * Copy the new gateway value into the memory chunk.
1390 bcopy(gate, rt->rt_gateway, glen);
1393 * This isn't going to do anything useful for host routes, so
1394 * don't bother. Also make sure we have a reasonable mask
1395 * (we don't yet have one during adds).
1397 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1398 struct rtfc_arg arg;
1402 RT_UNLOCK(rt); /* XXX workaround LOR */
1403 RADIX_NODE_HEAD_LOCK(rnh);
1405 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1406 rt_fixchange, &arg);
1407 RADIX_NODE_HEAD_UNLOCK(rnh);
1414 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1416 register u_char *cp1 = (u_char *)src;
1417 register u_char *cp2 = (u_char *)dst;
1418 register u_char *cp3 = (u_char *)netmask;
1419 u_char *cplim = cp2 + *cp3;
1420 u_char *cplim2 = cp2 + *cp1;
1422 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1427 *cp2++ = *cp1++ & *cp3++;
1429 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1433 * Set up a routing table entry, normally
1436 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1438 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1440 INIT_VNET_NET(curvnet);
1441 struct sockaddr *dst;
1442 struct sockaddr *netmask;
1443 struct rtentry *rt = NULL;
1444 struct rt_addrinfo info;
1446 int startfib, endfib;
1447 char tempbuf[_SOCKADDR_TMPSIZE];
1451 if (flags & RTF_HOST) {
1452 dst = ifa->ifa_dstaddr;
1455 dst = ifa->ifa_addr;
1456 netmask = ifa->ifa_netmask;
1458 if ( dst->sa_family != AF_INET)
1461 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1462 startfib = endfib = curthread->td_proc->p_fibnum;
1465 endfib = rt_numfibs - 1;
1468 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1472 if (dst->sa_len == 0)
1476 * If it's a delete, check that if it exists,
1477 * it's on the correct interface or we might scrub
1478 * a route to another ifa which would
1479 * be confusing at best and possibly worse.
1481 if (cmd == RTM_DELETE) {
1483 * It's a delete, so it should already exist..
1484 * If it's a net, mask off the host bits
1485 * (Assuming we have a mask)
1486 * XXX this is kinda inet specific..
1488 if (netmask != NULL) {
1489 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1490 dst = (struct sockaddr *)tempbuf;
1494 * Now go through all the requested tables (fibs) and do the
1495 * requested action. Realistically, this will either be fib 0
1496 * for protocols that don't do multiple tables or all the
1497 * tables for those that do. XXX For this version only AF_INET.
1498 * When that changes code should be refactored to protocol
1499 * independent parts and protocol dependent parts.
1501 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1502 if (cmd == RTM_DELETE) {
1503 struct radix_node_head *rnh;
1504 struct radix_node *rn;
1506 * Look up an rtentry that is in the routing tree and
1507 * contains the correct info.
1509 if ((rnh = V_rt_tables[fibnum][dst->sa_family]) == NULL)
1510 /* this table doesn't exist but others might */
1512 RADIX_NODE_HEAD_LOCK(rnh);
1514 if (rn_mpath_capable(rnh)) {
1516 rn = rnh->rnh_matchaddr(dst, rnh);
1522 * for interface route the
1523 * rt->rt_gateway is sockaddr_intf
1524 * for cloning ARP entries, so
1525 * rt_mpath_matchgate must use the
1528 rt = rt_mpath_matchgate(rt,
1536 rn = rnh->rnh_lookup(dst, netmask, rnh);
1537 error = (rn == NULL ||
1538 (rn->rn_flags & RNF_ROOT) ||
1539 RNTORT(rn)->rt_ifa != ifa ||
1540 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1541 RADIX_NODE_HEAD_UNLOCK(rnh);
1543 /* this is only an error if bad on ALL tables */
1548 * Do the actual request
1550 bzero((caddr_t)&info, sizeof(info));
1552 info.rti_flags = flags | ifa->ifa_flags;
1553 info.rti_info[RTAX_DST] = dst;
1554 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1555 info.rti_info[RTAX_NETMASK] = netmask;
1556 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1557 if (error == 0 && rt != NULL) {
1559 * notify any listening routing agents of the change
1564 * in case address alias finds the first address
1565 * e.g. ifconfig bge0 192.103.54.246/24
1566 * e.g. ifconfig bge0 192.103.54.247/24
1567 * the address set in the route is 192.103.54.246
1568 * so we need to replace it with 192.103.54.247
1570 if (memcmp(rt->rt_ifa->ifa_addr,
1571 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1572 IFAFREE(rt->rt_ifa);
1574 rt->rt_ifp = ifa->ifa_ifp;
1578 rt_newaddrmsg(cmd, ifa, error, rt);
1579 if (cmd == RTM_DELETE) {
1581 * If we are deleting, and we found an entry,
1582 * then it's been removed from the tree..
1583 * now throw it away.
1587 if (cmd == RTM_ADD) {
1589 * We just wanted to add it..
1590 * we don't actually need a reference.
1601 if (cmd == RTM_DELETE) {
1605 /* we only give an error if it wasn't in any table */
1606 error = ((flags & RTF_HOST) ?
1607 EHOSTUNREACH : ENETUNREACH);
1611 /* return an error if any of them failed */
1618 /* special one for inet internal use. may not use. */
1620 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1622 return (rtinit1(ifa, cmd, flags, -1));
1626 * Set up a routing table entry, normally
1630 rtinit(struct ifaddr *ifa, int cmd, int flags)
1632 struct sockaddr *dst;
1635 if (flags & RTF_HOST) {
1636 dst = ifa->ifa_dstaddr;
1638 dst = ifa->ifa_addr;
1641 if (dst->sa_family == AF_INET)
1643 return (rtinit1(ifa, cmd, flags, fib));
1647 * rt_check() is invoked on each layer 2 output path, prior to
1648 * encapsulating outbound packets.
1650 * The function is mostly used to find a routing entry for the gateway,
1651 * which in some protocol families could also point to the link-level
1652 * address for the gateway itself (the side effect of revalidating the
1653 * route to the destination is rather pointless at this stage, we did it
1654 * already a moment before in the pr_output() routine to locate the ifp
1655 * and gateway to use).
1657 * When we remove the layer-3 to layer-2 mapping tables from the
1658 * routing table, this function can be removed.
1661 * *dst is the address of the NEXT HOP (which coincides with the
1662 * final destination if directly reachable);
1663 * *lrt0 points to the cached route to the final destination;
1664 * *lrt is not meaningful;
1665 * (*lrt0 has no ref held on it by us so REMREF is not needed.
1666 * Refs only account for major structural references and not usages,
1667 * which is actually a bit of a problem.)
1670 * If the route is marked down try to find a new route. If the route
1671 * to the gateway is gone, try to setup a new route. Otherwise,
1672 * if the route is marked for packets to be rejected, enforce that.
1673 * Note that rtalloc returns an rtentry with an extra REF that we may
1677 * *dst is unchanged;
1678 * *lrt0 points to the (possibly new) route to the final destination
1679 * *lrt points to the route to the next hop [LOCKED]
1681 * Their values are meaningful ONLY if no error is returned.
1683 * To follow this you have to remember that:
1684 * RT_REMREF reduces the reference count by 1 but doesn't check it for 0 (!)
1685 * RTFREE_LOCKED includes an RT_REMREF (or an rtfree if refs == 1)
1687 * RTFREE does an RT_LOCK and an RTFREE_LOCKED
1688 * The gwroute pointer counts as a reference on the rtentry to which it points.
1689 * so when we add it we use the ref that rtalloc gives us and when we lose it
1690 * we need to remove the reference.
1691 * RT_TEMP_UNLOCK does an RT_ADDREF before freeing the lock, and
1692 * RT_RELOCK locks it (it can't have gone away due to the ref) and
1693 * drops the ref, possibly freeing it and zeroing the pointer if
1694 * the ref goes to 0 (unlocking in the process).
1697 rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst)
1700 struct rtentry *rt0;
1703 KASSERT(*lrt0 != NULL, ("rt_check"));
1706 fibnum = rt0->rt_fibnum;
1708 /* NB: the locking here is tortuous... */
1711 if (rt0 && (rt0->rt_flags & RTF_UP) == 0) {
1712 /* Current rt0 is useless, try get a replacement. */
1717 rt0 = rtalloc1_fib(dst, 1, 0UL, fibnum);
1719 return (EHOSTUNREACH);
1721 RT_REMREF(rt0); /* don't need the reference. */
1724 if (rt0->rt_flags & RTF_GATEWAY) {
1725 if ((rt = rt0->rt_gwroute) != NULL) {
1726 RT_LOCK(rt); /* NB: gwroute */
1727 if ((rt->rt_flags & RTF_UP) == 0) {
1728 /* gw route is dud. ignore/lose it */
1729 RTFREE_LOCKED(rt); /* unref (&unlock) gwroute */
1730 rt = rt0->rt_gwroute = NULL;
1734 if (rt == NULL) { /* NOT AN ELSE CLAUSE */
1735 RT_TEMP_UNLOCK(rt0); /* MUST return to undo this */
1736 rt = rtalloc1_fib(rt0->rt_gateway, 1, 0UL, fibnum);
1737 if ((rt == rt0) || (rt == NULL)) {
1738 /* the best we can do is not good enough */
1740 RT_REMREF(rt); /* assumes ref > 0 */
1743 RTFREE(rt0); /* lock, unref, (unlock) */
1744 return (ENETUNREACH);
1747 * Relock it and lose the added reference.
1748 * All sorts of things could have happenned while we
1749 * had no lock on it, so check for them.
1752 if (rt0 == NULL || ((rt0->rt_flags & RTF_UP) == 0))
1753 /* Ru-roh.. what we had is no longer any good */
1756 * While we were away, someone replaced the gateway.
1757 * Since a reference count is involved we can't just
1760 if (rt0->rt_gwroute) {
1761 if (rt0->rt_gwroute != rt) {
1766 rt0->rt_gwroute = rt;
1772 /* think of rt as having the lock from now on.. */
1775 /* XXX why are we inspecting rmx_expire? */
1776 if ((rt->rt_flags & RTF_REJECT) &&
1777 (rt->rt_rmx.rmx_expire == 0 ||
1778 time_uptime < rt->rt_rmx.rmx_expire)) {
1780 return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1788 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1789 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);