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>
54 #include <net/route.h>
57 #include <net/radix_mpath.h>
60 #include <netinet/in.h>
61 #include <netinet/ip_mroute.h>
69 /* while we use 4 bits in the mbuf flags,
70 * we are limited to 16
73 #if ROUTETABLES > RT_MAXFIBS
74 #define RT_NUMFIBS RT_MAXFIBS
75 #error "ROUTETABLES defined too big"
80 #define RT_NUMFIBS ROUTETABLES
85 u_int rt_numfibs = RT_NUMFIBS;
86 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
88 * Allow the boot code to allow LESS than RT_MAXFIBS to be used.
89 * We can't do more because storage is statically allocated for now.
90 * (for compatibility reasons.. this will change).
92 TUNABLE_INT("net.fibs", &rt_numfibs);
95 * By default add routes to all fibs for new interfaces.
96 * Once this is set to 0 then only allocate routes on interface
97 * changes for the FIB of the caller when adding a new set of addresses
98 * to an interface. XXX this is a shotgun aproach to a problem that needs
99 * a more fine grained solution.. that will come.
101 u_int rt_add_addr_allfibs = 1;
102 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
103 &rt_add_addr_allfibs, 0, "");
104 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
106 static struct rtstat rtstat;
108 /* by default only the first 'row' of tables will be accessed. */
110 * XXXMRT When we fix netstat, and do this differnetly,
111 * we can allocate this dynamically. As long as we are keeping
112 * things backwards compaitble we need to allocate this
115 struct radix_node_head *rt_tables[RT_MAXFIBS][AF_MAX+1];
117 static int rttrash; /* routes not in table but not freed */
119 static void rt_maskedcopy(struct sockaddr *,
120 struct sockaddr *, struct sockaddr *);
122 /* compare two sockaddr structures */
123 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
126 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
127 * The operation can be done safely (in this code) because a
128 * 'struct rtentry' starts with two 'struct radix_node''s, the first
129 * one representing leaf nodes in the routing tree, which is
130 * what the code in radix.c passes us as a 'struct radix_node'.
132 * But because there are a lot of assumptions in this conversion,
133 * do not cast explicitly, but always use the macro below.
135 #define RNTORT(p) ((struct rtentry *)(p))
137 static uma_zone_t rtzone; /* Routing table UMA zone. */
140 /* default fib for tunnels to use */
141 u_int tunnel_fib = 0;
142 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
146 * handler for net.my_fibnum
149 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
154 fibnum = curthread->td_proc->p_fibnum;
155 error = sysctl_handle_int(oidp, &fibnum, 0, req);
159 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
160 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
169 /* whack the tunable ints into line. */
170 if (rt_numfibs > RT_MAXFIBS)
171 rt_numfibs = RT_MAXFIBS;
174 rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
175 NULL, NULL, UMA_ALIGN_PTR, 0);
176 rn_init(); /* initialize all zeroes, all ones, mask table */
178 for (dom = domains; dom; dom = dom->dom_next) {
179 if (dom->dom_rtattach) {
180 for (table = 0; table < rt_numfibs; table++) {
181 if ( (fam = dom->dom_family) == AF_INET ||
183 /* for now only AF_INET has > 1 table */
185 * rtattach will be also called
186 * from vfs_export.c but the
188 * (only for AF_INET and AF_INET6
189 * which don't need it anyhow)
192 (void **)&rt_tables[table][fam],
202 #ifndef _SYS_SYSPROTO_H_
208 setfib(struct thread *td, struct setfib_args *uap)
210 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
212 td->td_proc->p_fibnum = uap->fibnum;
217 * Packet routing routines.
220 rtalloc(struct route *ro)
222 rtalloc_ign_fib(ro, 0UL, 0);
226 rtalloc_fib(struct route *ro, u_int fibnum)
228 rtalloc_ign_fib(ro, 0UL, fibnum);
232 rtalloc_ign(struct route *ro, u_long ignore)
236 if ((rt = ro->ro_rt) != NULL) {
237 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
242 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
244 RT_UNLOCK(ro->ro_rt);
248 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
252 if ((rt = ro->ro_rt) != NULL) {
253 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
258 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
260 RT_UNLOCK(ro->ro_rt);
264 * Look up the route that matches the address given
265 * Or, at least try.. Create a cloned route if needed.
267 * The returned route, if any, is locked.
270 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
272 return (rtalloc1_fib(dst, report, ignflags, 0));
276 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
279 struct radix_node_head *rnh;
281 struct radix_node *rn;
282 struct rtentry *newrt;
283 struct rt_addrinfo info;
285 int err = 0, msgtype = RTM_MISS;
287 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
288 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
290 rnh = rt_tables[fibnum][dst->sa_family];
293 * Look up the address in the table for that Address Family
296 rtstat.rts_unreach++;
299 RADIX_NODE_HEAD_LOCK(rnh);
300 if ((rn = rnh->rnh_matchaddr(dst, rnh)) &&
301 (rn->rn_flags & RNF_ROOT) == 0) {
303 * If we find it and it's not the root node, then
304 * get a reference on the rtentry associated.
306 newrt = rt = RNTORT(rn);
307 nflags = rt->rt_flags & ~ignflags;
308 if (report && (nflags & RTF_CLONING)) {
310 * We are apparently adding (report = 0 in delete).
311 * If it requires that it be cloned, do so.
312 * (This implies it wasn't a HOST route.)
314 err = rtrequest_fib(RTM_RESOLVE, dst, NULL,
315 NULL, 0, &newrt, fibnum);
318 * If the cloning didn't succeed, maybe
319 * what we have will do. Return that.
321 newrt = rt; /* existing route */
326 KASSERT(newrt, ("no route and no error"));
328 if (newrt->rt_flags & RTF_XRESOLVE) {
330 * If the new route specifies it be
331 * externally resolved, then go do that.
333 msgtype = RTM_RESOLVE;
336 /* Inform listeners of the new route. */
337 bzero(&info, sizeof(info));
338 info.rti_info[RTAX_DST] = rt_key(newrt);
339 info.rti_info[RTAX_NETMASK] = rt_mask(newrt);
340 info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway;
341 if (newrt->rt_ifp != NULL) {
342 info.rti_info[RTAX_IFP] =
343 newrt->rt_ifp->if_addr->ifa_addr;
344 info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr;
346 rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0);
351 RADIX_NODE_HEAD_UNLOCK(rnh);
354 * Either we hit the root or couldn't find any match,
355 * Which basically means
356 * "caint get there frm here"
358 rtstat.rts_unreach++;
360 RADIX_NODE_HEAD_UNLOCK(rnh);
363 * If required, report the failure to the supervising
365 * For a delete, this is not an error. (report == 0)
367 bzero(&info, sizeof(info));
368 info.rti_info[RTAX_DST] = dst;
369 rt_missmsg(msgtype, &info, 0, err);
373 RT_LOCK_ASSERT(newrt);
378 * Remove a reference count from an rtentry.
379 * If the count gets low enough, take it out of the routing table
382 rtfree(struct rtentry *rt)
384 struct radix_node_head *rnh;
386 KASSERT(rt != NULL,("%s: NULL rt", __func__));
387 rnh = rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
388 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
393 * The callers should use RTFREE_LOCKED() or RTFREE(), so
394 * we should come here exactly with the last reference.
397 if (rt->rt_refcnt > 0) {
398 printf("%s: %p has %lu refs\n", __func__, rt, rt->rt_refcnt);
403 * On last reference give the "close method" a chance
404 * to cleanup private state. This also permits (for
405 * IPv4 and IPv6) a chance to decide if the routing table
406 * entry should be purged immediately or at a later time.
407 * When an immediate purge is to happen the close routine
408 * typically calls rtexpunge which clears the RTF_UP flag
409 * on the entry so that the code below reclaims the storage.
411 if (rt->rt_refcnt == 0 && rnh->rnh_close)
412 rnh->rnh_close((struct radix_node *)rt, rnh);
415 * If we are no longer "up" (and ref == 0)
416 * then we can free the resources associated
419 if ((rt->rt_flags & RTF_UP) == 0) {
420 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
423 * the rtentry must have been removed from the routing table
424 * so it is represented in rttrash.. remove that now.
428 if (rt->rt_refcnt < 0) {
429 printf("rtfree: %p not freed (neg refs)\n", rt);
434 * release references on items we hold them on..
435 * e.g other routes and ifaddrs.
439 rt->rt_parent = NULL; /* NB: no refcnt on parent */
442 * The key is separatly alloc'd so free it (see rt_setgate()).
443 * This also frees the gateway, as they are always malloc'd
449 * and the rtentry itself of course
452 uma_zfree(rtzone, rt);
461 * Force a routing table entry to the specified
462 * destination to go through the given gateway.
463 * Normally called as a result of a routing redirect
464 * message from the network layer.
467 rtredirect(struct sockaddr *dst,
468 struct sockaddr *gateway,
469 struct sockaddr *netmask,
471 struct sockaddr *src)
473 rtredirect_fib(dst, gateway, netmask, flags, src, 0);
477 rtredirect_fib(struct sockaddr *dst,
478 struct sockaddr *gateway,
479 struct sockaddr *netmask,
481 struct sockaddr *src,
484 struct rtentry *rt, *rt0 = NULL;
487 struct rt_addrinfo info;
490 /* verify the gateway is directly reachable */
491 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
495 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
497 * If the redirect isn't from our current router for this dst,
498 * it's either old or wrong. If it redirects us to ourselves,
499 * we have a routing loop, perhaps as a result of an interface
500 * going down recently.
502 if (!(flags & RTF_DONE) && rt &&
503 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
505 else if (ifa_ifwithaddr(gateway))
506 error = EHOSTUNREACH;
510 * Create a new entry if we just got back a wildcard entry
511 * or the the lookup failed. This is necessary for hosts
512 * which use routing redirects generated by smart gateways
513 * to dynamically build the routing tables.
515 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
518 * Don't listen to the redirect if it's
519 * for a route to an interface.
521 if (rt->rt_flags & RTF_GATEWAY) {
522 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
524 * Changing from route to net => route to host.
525 * Create new route, rather than smashing route to net.
531 flags |= RTF_GATEWAY | RTF_DYNAMIC;
532 bzero((caddr_t)&info, sizeof(info));
533 info.rti_info[RTAX_DST] = dst;
534 info.rti_info[RTAX_GATEWAY] = gateway;
535 info.rti_info[RTAX_NETMASK] = netmask;
537 info.rti_flags = flags;
538 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
541 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
542 flags = rt->rt_flags;
547 stat = &rtstat.rts_dynamic;
549 struct rtentry *gwrt;
552 * Smash the current notion of the gateway to
553 * this destination. Should check about netmask!!!
555 rt->rt_flags |= RTF_MODIFIED;
556 flags |= RTF_MODIFIED;
557 stat = &rtstat.rts_newgateway;
559 * add the key and gateway (in one malloc'd chunk).
561 rt_setgate(rt, rt_key(rt), gateway);
562 gwrt = rtalloc1(gateway, 1, 0);
563 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
567 error = EHOSTUNREACH;
573 rtstat.rts_badredirect++;
574 else if (stat != NULL)
576 bzero((caddr_t)&info, sizeof(info));
577 info.rti_info[RTAX_DST] = dst;
578 info.rti_info[RTAX_GATEWAY] = gateway;
579 info.rti_info[RTAX_NETMASK] = netmask;
580 info.rti_info[RTAX_AUTHOR] = src;
581 rt_missmsg(RTM_REDIRECT, &info, flags, error);
585 rtioctl(u_long req, caddr_t data)
587 return (rtioctl_fib(req, data, 0));
591 * Routing table ioctl interface.
594 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
598 * If more ioctl commands are added here, make sure the proper
599 * super-user checks are being performed because it is possible for
600 * prison-root to make it this far if raw sockets have been enabled
604 /* Multicast goop, grrr... */
605 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
612 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
614 return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
618 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
621 register struct ifaddr *ifa;
624 if ((flags & RTF_GATEWAY) == 0) {
626 * If we are adding a route to an interface,
627 * and the interface is a pt to pt link
628 * we should search for the destination
629 * as our clue to the interface. Otherwise
630 * we can use the local address.
633 if (flags & RTF_HOST)
634 ifa = ifa_ifwithdstaddr(dst);
636 ifa = ifa_ifwithaddr(gateway);
639 * If we are adding a route to a remote net
640 * or host, the gateway may still be on the
641 * other end of a pt to pt link.
643 ifa = ifa_ifwithdstaddr(gateway);
646 ifa = ifa_ifwithnet(gateway);
648 struct rtentry *rt = rtalloc1_fib(gateway, 0, 0UL, fibnum);
652 * dismiss a gateway that is reachable only
653 * through the default router
655 switch (gateway->sa_family) {
657 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
661 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
671 if ((ifa = rt->rt_ifa) == NULL)
674 if (ifa->ifa_addr->sa_family != dst->sa_family) {
675 struct ifaddr *oifa = ifa;
676 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
683 static walktree_f_t rt_fixdelete;
684 static walktree_f_t rt_fixchange;
688 struct radix_node_head *rnh;
692 * Do appropriate manipulations of a routing tree given
693 * all the bits of info needed
697 struct sockaddr *dst,
698 struct sockaddr *gateway,
699 struct sockaddr *netmask,
701 struct rtentry **ret_nrt)
703 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
707 rtrequest_fib(int req,
708 struct sockaddr *dst,
709 struct sockaddr *gateway,
710 struct sockaddr *netmask,
712 struct rtentry **ret_nrt,
715 struct rt_addrinfo info;
717 if (dst->sa_len == 0)
720 bzero((caddr_t)&info, sizeof(info));
721 info.rti_flags = flags;
722 info.rti_info[RTAX_DST] = dst;
723 info.rti_info[RTAX_GATEWAY] = gateway;
724 info.rti_info[RTAX_NETMASK] = netmask;
725 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
729 * These (questionable) definitions of apparent local variables apply
730 * to the next two functions. XXXXXX!!!
732 #define dst info->rti_info[RTAX_DST]
733 #define gateway info->rti_info[RTAX_GATEWAY]
734 #define netmask info->rti_info[RTAX_NETMASK]
735 #define ifaaddr info->rti_info[RTAX_IFA]
736 #define ifpaddr info->rti_info[RTAX_IFP]
737 #define flags info->rti_flags
740 rt_getifa(struct rt_addrinfo *info)
742 return (rt_getifa_fib(info, 0));
746 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
752 * ifp may be specified by sockaddr_dl
753 * when protocol address is ambiguous.
755 if (info->rti_ifp == NULL && ifpaddr != NULL &&
756 ifpaddr->sa_family == AF_LINK &&
757 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
758 info->rti_ifp = ifa->ifa_ifp;
759 if (info->rti_ifa == NULL && ifaaddr != NULL)
760 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
761 if (info->rti_ifa == NULL) {
764 sa = ifaaddr != NULL ? ifaaddr :
765 (gateway != NULL ? gateway : dst);
766 if (sa != NULL && info->rti_ifp != NULL)
767 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
768 else if (dst != NULL && gateway != NULL)
769 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
772 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
775 if ((ifa = info->rti_ifa) != NULL) {
776 if (info->rti_ifp == NULL)
777 info->rti_ifp = ifa->ifa_ifp;
784 * Expunges references to a route that's about to be reclaimed.
785 * The route must be locked.
788 rtexpunge(struct rtentry *rt)
790 struct radix_node *rn;
791 struct radix_node_head *rnh;
798 * We cannot assume anything about the reference count
799 * because protocols call us in many situations; often
800 * before unwinding references to the table entry.
802 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
805 * Find the correct routing tree to use for this Address Family
807 rnh = rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
809 return (EAFNOSUPPORT);
811 RADIX_NODE_HEAD_LOCK(rnh);
814 * Remove the item from the tree; it should be there,
815 * but when callers invoke us blindly it may not (sigh).
817 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
822 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
823 ("unexpected flags 0x%x", rn->rn_flags));
824 KASSERT(rt == RNTORT(rn),
825 ("lookup mismatch, rt %p rn %p", rt, rn));
827 rt->rt_flags &= ~RTF_UP;
830 * Now search what's left of the subtree for any cloned
831 * routes which might have been formed from this node.
833 if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt))
834 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
838 * Remove any external references we may have.
839 * This might result in another rtentry being freed if
840 * we held its last reference.
842 if (rt->rt_gwroute) {
843 RTFREE(rt->rt_gwroute);
844 rt->rt_gwroute = NULL;
848 * Give the protocol a chance to keep things in sync.
850 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
851 struct rt_addrinfo info;
853 bzero((caddr_t)&info, sizeof(info));
854 info.rti_flags = rt->rt_flags;
855 info.rti_info[RTAX_DST] = rt_key(rt);
856 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
857 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
858 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
862 * one more rtentry floating around that is not
863 * linked to the routing table.
867 RADIX_NODE_HEAD_UNLOCK(rnh);
872 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
874 return (rtrequest1_fib(req, info, ret_nrt, 0));
878 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
882 register struct rtentry *rt;
883 register struct radix_node *rn;
884 register struct radix_node_head *rnh;
886 struct sockaddr *ndst;
887 #define senderr(x) { error = x ; goto bad; }
889 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
890 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
893 * Find the correct routing tree to use for this Address Family
895 rnh = rt_tables[fibnum][dst->sa_family];
897 return (EAFNOSUPPORT);
898 RADIX_NODE_HEAD_LOCK(rnh);
900 * If we are adding a host route then we don't want to put
901 * a netmask in the tree, nor do we want to clone it.
903 if (flags & RTF_HOST) {
905 flags &= ~RTF_CLONING;
911 * if we got multipath routes, we require users to specify
912 * a matching RTAX_GATEWAY.
914 if (rn_mpath_capable(rnh)) {
915 struct rtentry *rto = NULL;
917 rn = rnh->rnh_matchaddr(dst, rnh);
920 rto = rt = RNTORT(rn);
921 rt = rt_mpath_matchgate(rt, gateway);
925 * this is the first entry in the chain
928 rn = rn_mpath_next((struct radix_node *)rt);
930 * there is another entry, now it's active
935 rto->rt_flags |= RTF_UP;
937 } else if (rt->rt_flags & RTF_GATEWAY) {
939 * For gateway routes, we need to
940 * make sure that we we are deleting
941 * the correct gateway.
942 * rt_mpath_matchgate() does not
943 * check the case when there is only
944 * one route in the chain.
947 (rt->rt_gateway->sa_len != gateway->sa_len ||
948 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
952 * use the normal delete code to remove
958 * if the entry is 2nd and on up
960 if (!rt_mpath_deldup(rto, rt))
961 panic ("rtrequest1: rt_mpath_deldup");
964 rt->rt_flags &= ~RTF_UP;
965 goto deldone; /* done with the RTM_DELETE command */
971 * Remove the item from the tree and return it.
972 * Complain if it is not there and do no more processing.
974 rn = rnh->rnh_deladdr(dst, netmask, rnh);
977 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
978 panic ("rtrequest delete");
982 rt->rt_flags &= ~RTF_UP;
985 * Now search what's left of the subtree for any cloned
986 * routes which might have been formed from this node.
988 if ((rt->rt_flags & RTF_CLONING) &&
990 rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
995 * Remove any external references we may have.
996 * This might result in another rtentry being freed if
997 * we held its last reference.
999 if (rt->rt_gwroute) {
1000 RTFREE(rt->rt_gwroute);
1001 rt->rt_gwroute = NULL;
1005 * give the protocol a chance to keep things in sync.
1007 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
1008 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1014 * One more rtentry floating around that is not
1015 * linked to the routing table. rttrash will be decremented
1016 * when RTFREE(rt) is eventually called.
1021 * If the caller wants it, then it can have it,
1022 * but it's up to it to free the rtentry as we won't be
1033 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
1037 flags = rt->rt_flags &
1038 ~(RTF_CLONING | RTF_STATIC);
1039 flags |= RTF_WASCLONED;
1040 gateway = rt->rt_gateway;
1041 if ((netmask = rt->rt_genmask) == NULL)
1046 if ((flags & RTF_GATEWAY) && !gateway)
1048 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1049 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1052 if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum)))
1054 ifa = info->rti_ifa;
1057 rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
1061 rt->rt_flags = RTF_UP | flags;
1062 rt->rt_fibnum = fibnum;
1064 * Add the gateway. Possibly re-malloc-ing the storage for it
1065 * also add the rt_gwroute if possible.
1068 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1069 RT_LOCK_DESTROY(rt);
1070 uma_zfree(rtzone, rt);
1075 * point to the (possibly newly malloc'd) dest address.
1077 ndst = (struct sockaddr *)rt_key(rt);
1080 * make sure it contains the value we want (masked if needed).
1083 rt_maskedcopy(dst, ndst, netmask);
1085 bcopy(dst, ndst, dst->sa_len);
1088 * Note that we now have a reference to the ifa.
1089 * This moved from below so that rnh->rnh_addaddr() can
1090 * examine the ifa and ifa->ifa_ifp if it so desires.
1094 rt->rt_ifp = ifa->ifa_ifp;
1097 /* do not permit exactly the same dst/mask/gw pair */
1098 if (rn_mpath_capable(rnh) &&
1099 rt_mpath_conflict(rnh, rt, netmask)) {
1101 RTFREE(rt->rt_gwroute);
1103 IFAFREE(rt->rt_ifa);
1106 RT_LOCK_DESTROY(rt);
1107 uma_zfree(rtzone, rt);
1112 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1113 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1115 struct rtentry *rt2;
1117 * Uh-oh, we already have one of these in the tree.
1118 * We do a special hack: if the route that's already
1119 * there was generated by the cloning mechanism
1120 * then we just blow it away and retry the insertion
1123 rt2 = rtalloc1_fib(dst, 0, 0, fibnum);
1124 if (rt2 && rt2->rt_parent) {
1127 rn = rnh->rnh_addaddr(ndst, netmask,
1130 /* undo the extra ref we got */
1136 * If it still failed to go into the tree,
1137 * then un-make it (this should be a function)
1141 RTFREE(rt->rt_gwroute);
1143 IFAFREE(rt->rt_ifa);
1145 RT_LOCK_DESTROY(rt);
1146 uma_zfree(rtzone, rt);
1150 rt->rt_parent = NULL;
1153 * If we got here from RESOLVE, then we are cloning
1154 * so clone the rest, and note that we
1155 * are a clone (and increment the parent's references)
1157 if (req == RTM_RESOLVE) {
1158 KASSERT(ret_nrt && *ret_nrt,
1159 ("no route to clone from"));
1160 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
1161 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
1162 if ((*ret_nrt)->rt_flags & RTF_CLONING) {
1164 * NB: We do not bump the refcnt on the parent
1165 * entry under the assumption that it will
1166 * remain so long as we do. This is
1167 * important when deleting the parent route
1168 * as this operation requires traversing
1169 * the tree to delete all clones and futzing
1170 * with refcnts requires us to double-lock
1171 * parent through this back reference.
1173 rt->rt_parent = *ret_nrt;
1178 * If this protocol has something to add to this then
1179 * allow it to do that as well.
1181 if (ifa->ifa_rtrequest)
1182 ifa->ifa_rtrequest(req, rt, info);
1185 * We repeat the same procedure from rt_setgate() here because
1186 * it doesn't fire when we call it there because the node
1187 * hasn't been added to the tree yet.
1189 if (req == RTM_ADD &&
1190 !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1191 struct rtfc_arg arg;
1194 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1195 rt_fixchange, &arg);
1199 * actually return a resultant rtentry and
1200 * give the caller a single reference.
1212 RADIX_NODE_HEAD_UNLOCK(rnh);
1225 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1226 * (i.e., the routes related to it by the operation of cloning). This
1227 * routine is iterated over all potential former-child-routes by way of
1228 * rnh->rnh_walktree_from() above, and those that actually are children of
1229 * the late parent (passed in as VP here) are themselves deleted.
1232 rt_fixdelete(struct radix_node *rn, void *vp)
1234 struct rtentry *rt = RNTORT(rn);
1235 struct rtentry *rt0 = vp;
1237 if (rt->rt_parent == rt0 &&
1238 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) {
1239 return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1240 rt->rt_flags, NULL, rt->rt_fibnum);
1246 * This routine is called from rt_setgate() to do the analogous thing for
1247 * adds and changes. There is the added complication in this case of a
1248 * middle insert; i.e., insertion of a new network route between an older
1249 * network route and (cloned) host routes. For this reason, a simple check
1250 * of rt->rt_parent is insufficient; each candidate route must be tested
1251 * against the (mask, value) of the new route (passed as before in vp)
1252 * to see if the new route matches it.
1254 * XXX - it may be possible to do fixdelete() for changes and reserve this
1255 * routine just for adds. I'm not sure why I thought it was necessary to do
1260 rt_fixchange(struct radix_node *rn, void *vp)
1262 struct rtentry *rt = RNTORT(rn);
1263 struct rtfc_arg *ap = vp;
1264 struct rtentry *rt0 = ap->rt0;
1265 struct radix_node_head *rnh = ap->rnh;
1266 u_char *xk1, *xm1, *xk2, *xmp;
1269 /* make sure we have a parent, and route is not pinned or cloning */
1270 if (!rt->rt_parent ||
1271 (rt->rt_flags & (RTF_PINNED | RTF_CLONING)))
1274 if (rt->rt_parent == rt0) /* parent match */
1277 * There probably is a function somewhere which does this...
1278 * if not, there should be.
1280 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1282 xk1 = (u_char *)rt_key(rt0);
1283 xm1 = (u_char *)rt_mask(rt0);
1284 xk2 = (u_char *)rt_key(rt);
1286 /* avoid applying a less specific route */
1287 xmp = (u_char *)rt_mask(rt->rt_parent);
1288 mlen = rt_key(rt->rt_parent)->sa_len;
1289 if (mlen > rt_key(rt0)->sa_len) /* less specific route */
1291 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++)
1292 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i])
1293 return 0; /* less specific route */
1295 for (i = rnh->rnh_treetop->rn_offset; i < len; i++)
1296 if ((xk2[i] & xm1[i]) != xk1[i])
1297 return 0; /* no match */
1300 * OK, this node is a clone, and matches the node currently being
1301 * changed/added under the node's mask. So, get rid of it.
1304 return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL,
1305 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum);
1309 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1311 /* XXX dst may be overwritten, can we move this to below */
1312 struct radix_node_head *rnh = rt_tables[rt->rt_fibnum][dst->sa_family];
1313 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1319 * A host route with the destination equal to the gateway
1320 * will interfere with keeping LLINFO in the routing
1321 * table, so disallow it.
1323 if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
1324 (RTF_HOST|RTF_GATEWAY)) &&
1325 dst->sa_len == gate->sa_len &&
1326 bcmp(dst, gate, dst->sa_len) == 0) {
1328 * The route might already exist if this is an RTM_CHANGE
1329 * or a routing redirect, so try to delete it.
1333 return EADDRNOTAVAIL;
1337 * Cloning loop avoidance in case of bad configuration.
1339 if (rt->rt_flags & RTF_GATEWAY) {
1340 struct rtentry *gwrt;
1342 RT_UNLOCK(rt); /* XXX workaround LOR */
1343 gwrt = rtalloc1_fib(gate, 1, 0, rt->rt_fibnum);
1346 return (EADDRINUSE); /* failure */
1349 * Try to reacquire the lock on rt, and if it fails,
1350 * clean state and restart from scratch.
1352 if (!RT_TRYLOCK(rt)) {
1353 RTFREE_LOCKED(gwrt);
1358 * If there is already a gwroute, then drop it. If we
1359 * are asked to replace route with itself, then do
1360 * not leak its refcounter.
1362 if (rt->rt_gwroute != NULL) {
1363 if (rt->rt_gwroute == gwrt) {
1364 RT_REMREF(rt->rt_gwroute);
1366 RTFREE(rt->rt_gwroute);
1369 if ((rt->rt_gwroute = gwrt) != NULL)
1370 RT_UNLOCK(rt->rt_gwroute);
1374 * Prepare to store the gateway in rt->rt_gateway.
1375 * Both dst and gateway are stored one after the other in the same
1376 * malloc'd chunk. If we have room, we can reuse the old buffer,
1377 * rt_gateway already points to the right place.
1378 * Otherwise, malloc a new block and update the 'dst' address.
1380 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1383 R_Malloc(new, caddr_t, dlen + glen);
1387 * XXX note, we copy from *dst and not *rt_key(rt) because
1388 * rt_setgate() can be called to initialize a newly
1389 * allocated route entry, in which case rt_key(rt) == NULL
1390 * (and also rt->rt_gateway == NULL).
1391 * Free()/free() handle a NULL argument just fine.
1393 bcopy(dst, new, dlen);
1394 Free(rt_key(rt)); /* free old block, if any */
1395 rt_key(rt) = (struct sockaddr *)new;
1396 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1400 * Copy the new gateway value into the memory chunk.
1402 bcopy(gate, rt->rt_gateway, glen);
1405 * This isn't going to do anything useful for host routes, so
1406 * don't bother. Also make sure we have a reasonable mask
1407 * (we don't yet have one during adds).
1409 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1410 struct rtfc_arg arg;
1414 RT_UNLOCK(rt); /* XXX workaround LOR */
1415 RADIX_NODE_HEAD_LOCK(rnh);
1417 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1418 rt_fixchange, &arg);
1419 RADIX_NODE_HEAD_UNLOCK(rnh);
1426 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1428 register u_char *cp1 = (u_char *)src;
1429 register u_char *cp2 = (u_char *)dst;
1430 register u_char *cp3 = (u_char *)netmask;
1431 u_char *cplim = cp2 + *cp3;
1432 u_char *cplim2 = cp2 + *cp1;
1434 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1439 *cp2++ = *cp1++ & *cp3++;
1441 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1445 * Set up a routing table entry, normally
1448 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1450 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1452 struct sockaddr *dst;
1453 struct sockaddr *netmask;
1454 struct rtentry *rt = NULL;
1455 struct rt_addrinfo info;
1457 int startfib, endfib;
1458 char tempbuf[_SOCKADDR_TMPSIZE];
1462 if (flags & RTF_HOST) {
1463 dst = ifa->ifa_dstaddr;
1466 dst = ifa->ifa_addr;
1467 netmask = ifa->ifa_netmask;
1469 if ( dst->sa_family != AF_INET)
1472 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1473 startfib = endfib = curthread->td_proc->p_fibnum;
1476 endfib = rt_numfibs - 1;
1479 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1483 if (dst->sa_len == 0)
1487 * If it's a delete, check that if it exists,
1488 * it's on the correct interface or we might scrub
1489 * a route to another ifa which would
1490 * be confusing at best and possibly worse.
1492 if (cmd == RTM_DELETE) {
1494 * It's a delete, so it should already exist..
1495 * If it's a net, mask off the host bits
1496 * (Assuming we have a mask)
1497 * XXX this is kinda inet specific..
1499 if (netmask != NULL) {
1500 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1501 dst = (struct sockaddr *)tempbuf;
1505 * Now go through all the requested tables (fibs) and do the
1506 * requested action. Realistically, this will either be fib 0
1507 * for protocols that don't do multiple tables or all the
1508 * tables for those that do. XXX For this version only AF_INET.
1509 * When that changes code should be refactored to protocol
1510 * independent parts and protocol dependent parts.
1512 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1513 if (cmd == RTM_DELETE) {
1514 struct radix_node_head *rnh;
1515 struct radix_node *rn;
1517 * Look up an rtentry that is in the routing tree and
1518 * contains the correct info.
1520 if ((rnh = rt_tables[fibnum][dst->sa_family]) == NULL)
1521 /* this table doesn't exist but others might */
1523 RADIX_NODE_HEAD_LOCK(rnh);
1525 if (rn_mpath_capable(rnh)) {
1527 rn = rnh->rnh_matchaddr(dst, rnh);
1533 * for interface route the
1534 * rt->rt_gateway is sockaddr_intf
1535 * for cloning ARP entries, so
1536 * rt_mpath_matchgate must use the
1539 rt = rt_mpath_matchgate(rt,
1547 rn = rnh->rnh_lookup(dst, netmask, rnh);
1548 error = (rn == NULL ||
1549 (rn->rn_flags & RNF_ROOT) ||
1550 RNTORT(rn)->rt_ifa != ifa ||
1551 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1552 RADIX_NODE_HEAD_UNLOCK(rnh);
1554 /* this is only an error if bad on ALL tables */
1559 * Do the actual request
1561 bzero((caddr_t)&info, sizeof(info));
1563 info.rti_flags = flags | ifa->ifa_flags;
1564 info.rti_info[RTAX_DST] = dst;
1565 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1566 info.rti_info[RTAX_NETMASK] = netmask;
1567 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1568 if (error == 0 && rt != NULL) {
1570 * notify any listening routing agents of the change
1575 * in case address alias finds the first address
1576 * e.g. ifconfig bge0 192.103.54.246/24
1577 * e.g. ifconfig bge0 192.103.54.247/24
1578 * the address set in the route is 192.103.54.246
1579 * so we need to replace it with 192.103.54.247
1581 if (memcmp(rt->rt_ifa->ifa_addr,
1582 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1583 IFAFREE(rt->rt_ifa);
1585 rt->rt_ifp = ifa->ifa_ifp;
1589 rt_newaddrmsg(cmd, ifa, error, rt);
1590 if (cmd == RTM_DELETE) {
1592 * If we are deleting, and we found an entry,
1593 * then it's been removed from the tree..
1594 * now throw it away.
1598 if (cmd == RTM_ADD) {
1600 * We just wanted to add it..
1601 * we don't actually need a reference.
1612 if (cmd == RTM_DELETE) {
1616 /* we only give an error if it wasn't in any table */
1617 error = ((flags & RTF_HOST) ?
1618 EHOSTUNREACH : ENETUNREACH);
1622 /* return an error if any of them failed */
1629 /* special one for inet internal use. may not use. */
1631 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1633 return (rtinit1(ifa, cmd, flags, -1));
1637 * Set up a routing table entry, normally
1641 rtinit(struct ifaddr *ifa, int cmd, int flags)
1643 struct sockaddr *dst;
1646 if (flags & RTF_HOST) {
1647 dst = ifa->ifa_dstaddr;
1649 dst = ifa->ifa_addr;
1652 if (dst->sa_family == AF_INET)
1654 return (rtinit1(ifa, cmd, flags, fib));
1658 * rt_check() is invoked on each layer 2 output path, prior to
1659 * encapsulating outbound packets.
1661 * The function is mostly used to find a routing entry for the gateway,
1662 * which in some protocol families could also point to the link-level
1663 * address for the gateway itself (the side effect of revalidating the
1664 * route to the destination is rather pointless at this stage, we did it
1665 * already a moment before in the pr_output() routine to locate the ifp
1666 * and gateway to use).
1668 * When we remove the layer-3 to layer-2 mapping tables from the
1669 * routing table, this function can be removed.
1672 * *dst is the address of the NEXT HOP (which coincides with the
1673 * final destination if directly reachable);
1674 * *lrt0 points to the cached route to the final destination;
1675 * *lrt is not meaningful;
1676 * fibnum is the index to the correct network fib for this packet
1679 * If the route is marked down try to find a new route. If the route
1680 * to the gateway is gone, try to setup a new route. Otherwise,
1681 * if the route is marked for packets to be rejected, enforce that.
1684 * *dst is unchanged;
1685 * *lrt0 points to the (possibly new) route to the final destination
1686 * *lrt points to the route to the next hop
1688 * Their values are meaningful ONLY if no error is returned.
1691 rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst)
1693 return (rt_check_fib(lrt, lrt0, dst, 0));
1697 rt_check_fib(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst,
1701 struct rtentry *rt0;
1704 KASSERT(*lrt0 != NULL, ("rt_check"));
1707 /* NB: the locking here is tortuous... */
1709 if ((rt->rt_flags & RTF_UP) == 0) {
1711 rt = rtalloc1_fib(dst, 1, 0UL, fibnum);
1714 /* XXX what about if change? */
1716 return (EHOSTUNREACH);
1719 /* XXX BSD/OS checks dst->sa_family != AF_NS */
1720 if (rt->rt_flags & RTF_GATEWAY) {
1721 if (rt->rt_gwroute == NULL)
1723 rt = rt->rt_gwroute;
1724 RT_LOCK(rt); /* NB: gwroute */
1725 if ((rt->rt_flags & RTF_UP) == 0) {
1726 RTFREE_LOCKED(rt); /* unlock gwroute */
1728 rt0->rt_gwroute = NULL;
1731 /* XXX MRT link level looked up in table 0 */
1732 rt = rtalloc1_fib(rt->rt_gateway, 1, 0UL, 0);
1736 return (ENETUNREACH);
1739 if (rt0->rt_gwroute != NULL)
1740 RTFREE(rt0->rt_gwroute);
1741 rt0->rt_gwroute = rt;
1744 return (EHOSTUNREACH);
1749 /* XXX why are we inspecting rmx_expire? */
1750 error = (rt->rt_flags & RTF_REJECT) &&
1751 (rt->rt_rmx.rmx_expire == 0 ||
1752 time_uptime < rt->rt_rmx.rmx_expire);
1755 return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1763 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1764 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);