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>
70 /* while we use 4 bits in the mbuf flags,
71 * we are limited to 16
74 #if ROUTETABLES > RT_MAXFIBS
75 #define RT_NUMFIBS RT_MAXFIBS
76 #error "ROUTETABLES defined too big"
81 #define RT_NUMFIBS ROUTETABLES
86 u_int rt_numfibs = RT_NUMFIBS;
87 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
89 * Allow the boot code to allow LESS than RT_MAXFIBS to be used.
90 * We can't do more because storage is statically allocated for now.
91 * (for compatibility reasons.. this will change).
93 TUNABLE_INT("net.fibs", &rt_numfibs);
96 * By default add routes to all fibs for new interfaces.
97 * Once this is set to 0 then only allocate routes on interface
98 * changes for the FIB of the caller when adding a new set of addresses
99 * to an interface. XXX this is a shotgun aproach to a problem that needs
100 * a more fine grained solution.. that will come.
102 u_int rt_add_addr_allfibs = 1;
103 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
104 &rt_add_addr_allfibs, 0, "");
105 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
107 static struct rtstat rtstat;
109 /* by default only the first 'row' of tables will be accessed. */
111 * XXXMRT When we fix netstat, and do this differnetly,
112 * we can allocate this dynamically. As long as we are keeping
113 * things backwards compaitble we need to allocate this
116 struct radix_node_head *rt_tables[RT_MAXFIBS][AF_MAX+1];
118 static int rttrash; /* routes not in table but not freed */
120 static void rt_maskedcopy(struct sockaddr *,
121 struct sockaddr *, struct sockaddr *);
123 /* compare two sockaddr structures */
124 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
127 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
128 * The operation can be done safely (in this code) because a
129 * 'struct rtentry' starts with two 'struct radix_node''s, the first
130 * one representing leaf nodes in the routing tree, which is
131 * what the code in radix.c passes us as a 'struct radix_node'.
133 * But because there are a lot of assumptions in this conversion,
134 * do not cast explicitly, but always use the macro below.
136 #define RNTORT(p) ((struct rtentry *)(p))
138 static uma_zone_t rtzone; /* Routing table UMA zone. */
141 /* default fib for tunnels to use */
142 u_int tunnel_fib = 0;
143 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
147 * handler for net.my_fibnum
150 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
155 fibnum = curthread->td_proc->p_fibnum;
156 error = sysctl_handle_int(oidp, &fibnum, 0, req);
160 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
161 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
170 /* whack the tunable ints into line. */
171 if (rt_numfibs > RT_MAXFIBS)
172 rt_numfibs = RT_MAXFIBS;
175 rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
176 NULL, NULL, UMA_ALIGN_PTR, 0);
177 rn_init(); /* initialize all zeroes, all ones, mask table */
179 for (dom = domains; dom; dom = dom->dom_next) {
180 if (dom->dom_rtattach) {
181 for (table = 0; table < rt_numfibs; table++) {
182 if ( (fam = dom->dom_family) == AF_INET ||
184 /* for now only AF_INET has > 1 table */
186 * rtattach will be also called
187 * from vfs_export.c but the
189 * (only for AF_INET and AF_INET6
190 * which don't need it anyhow)
193 (void **)&V_rt_tables[table][fam],
203 #ifndef _SYS_SYSPROTO_H_
209 setfib(struct thread *td, struct setfib_args *uap)
211 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
213 td->td_proc->p_fibnum = uap->fibnum;
218 * Packet routing routines.
221 rtalloc(struct route *ro)
223 rtalloc_ign_fib(ro, 0UL, 0);
227 rtalloc_fib(struct route *ro, u_int fibnum)
229 rtalloc_ign_fib(ro, 0UL, fibnum);
233 rtalloc_ign(struct route *ro, u_long ignore)
237 if ((rt = ro->ro_rt) != NULL) {
238 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
243 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
245 RT_UNLOCK(ro->ro_rt);
249 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
253 if ((rt = ro->ro_rt) != NULL) {
254 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
259 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
261 RT_UNLOCK(ro->ro_rt);
265 * Look up the route that matches the address given
266 * Or, at least try.. Create a cloned route if needed.
268 * The returned route, if any, is locked.
271 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
273 return (rtalloc1_fib(dst, report, ignflags, 0));
277 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
280 struct radix_node_head *rnh;
282 struct radix_node *rn;
283 struct rtentry *newrt;
284 struct rt_addrinfo info;
286 int err = 0, msgtype = RTM_MISS;
288 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
289 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
291 rnh = V_rt_tables[fibnum][dst->sa_family];
294 * Look up the address in the table for that Address Family
297 V_rtstat.rts_unreach++;
300 RADIX_NODE_HEAD_LOCK(rnh);
301 if ((rn = rnh->rnh_matchaddr(dst, rnh)) &&
302 (rn->rn_flags & RNF_ROOT) == 0) {
304 * If we find it and it's not the root node, then
305 * get a reference on the rtentry associated.
307 newrt = rt = RNTORT(rn);
308 nflags = rt->rt_flags & ~ignflags;
309 if (report && (nflags & RTF_CLONING)) {
311 * We are apparently adding (report = 0 in delete).
312 * If it requires that it be cloned, do so.
313 * (This implies it wasn't a HOST route.)
315 err = rtrequest_fib(RTM_RESOLVE, dst, NULL,
316 NULL, 0, &newrt, fibnum);
319 * If the cloning didn't succeed, maybe
320 * what we have will do. Return that.
322 newrt = rt; /* existing route */
327 KASSERT(newrt, ("no route and no error"));
329 if (newrt->rt_flags & RTF_XRESOLVE) {
331 * If the new route specifies it be
332 * externally resolved, then go do that.
334 msgtype = RTM_RESOLVE;
337 /* Inform listeners of the new route. */
338 bzero(&info, sizeof(info));
339 info.rti_info[RTAX_DST] = rt_key(newrt);
340 info.rti_info[RTAX_NETMASK] = rt_mask(newrt);
341 info.rti_info[RTAX_GATEWAY] = newrt->rt_gateway;
342 if (newrt->rt_ifp != NULL) {
343 info.rti_info[RTAX_IFP] =
344 newrt->rt_ifp->if_addr->ifa_addr;
345 info.rti_info[RTAX_IFA] = newrt->rt_ifa->ifa_addr;
347 rt_missmsg(RTM_ADD, &info, newrt->rt_flags, 0);
352 RADIX_NODE_HEAD_UNLOCK(rnh);
355 * Either we hit the root or couldn't find any match,
356 * Which basically means
357 * "caint get there frm here"
359 V_rtstat.rts_unreach++;
361 RADIX_NODE_HEAD_UNLOCK(rnh);
364 * If required, report the failure to the supervising
366 * For a delete, this is not an error. (report == 0)
368 bzero(&info, sizeof(info));
369 info.rti_info[RTAX_DST] = dst;
370 rt_missmsg(msgtype, &info, 0, err);
374 RT_LOCK_ASSERT(newrt);
379 * Remove a reference count from an rtentry.
380 * If the count gets low enough, take it out of the routing table
383 rtfree(struct rtentry *rt)
385 struct radix_node_head *rnh;
387 KASSERT(rt != NULL,("%s: NULL rt", __func__));
388 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
389 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
394 * The callers should use RTFREE_LOCKED() or RTFREE(), so
395 * we should come here exactly with the last reference.
398 if (rt->rt_refcnt > 0) {
399 printf("%s: %p has %lu refs\n", __func__, rt, rt->rt_refcnt);
404 * On last reference give the "close method" a chance
405 * to cleanup private state. This also permits (for
406 * IPv4 and IPv6) a chance to decide if the routing table
407 * entry should be purged immediately or at a later time.
408 * When an immediate purge is to happen the close routine
409 * typically calls rtexpunge which clears the RTF_UP flag
410 * on the entry so that the code below reclaims the storage.
412 if (rt->rt_refcnt == 0 && rnh->rnh_close)
413 rnh->rnh_close((struct radix_node *)rt, rnh);
416 * If we are no longer "up" (and ref == 0)
417 * then we can free the resources associated
420 if ((rt->rt_flags & RTF_UP) == 0) {
421 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
424 * the rtentry must have been removed from the routing table
425 * so it is represented in rttrash.. remove that now.
429 if (rt->rt_refcnt < 0) {
430 printf("rtfree: %p not freed (neg refs)\n", rt);
435 * release references on items we hold them on..
436 * e.g other routes and ifaddrs.
440 rt->rt_parent = NULL; /* NB: no refcnt on parent */
443 * The key is separatly alloc'd so free it (see rt_setgate()).
444 * This also frees the gateway, as they are always malloc'd
450 * and the rtentry itself of course
453 uma_zfree(rtzone, rt);
462 * Force a routing table entry to the specified
463 * destination to go through the given gateway.
464 * Normally called as a result of a routing redirect
465 * message from the network layer.
468 rtredirect(struct sockaddr *dst,
469 struct sockaddr *gateway,
470 struct sockaddr *netmask,
472 struct sockaddr *src)
474 rtredirect_fib(dst, gateway, netmask, flags, src, 0);
478 rtredirect_fib(struct sockaddr *dst,
479 struct sockaddr *gateway,
480 struct sockaddr *netmask,
482 struct sockaddr *src,
485 struct rtentry *rt, *rt0 = NULL;
488 struct rt_addrinfo info;
491 /* verify the gateway is directly reachable */
492 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
496 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
498 * If the redirect isn't from our current router for this dst,
499 * it's either old or wrong. If it redirects us to ourselves,
500 * we have a routing loop, perhaps as a result of an interface
501 * going down recently.
503 if (!(flags & RTF_DONE) && rt &&
504 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
506 else if (ifa_ifwithaddr(gateway))
507 error = EHOSTUNREACH;
511 * Create a new entry if we just got back a wildcard entry
512 * or the the lookup failed. This is necessary for hosts
513 * which use routing redirects generated by smart gateways
514 * to dynamically build the routing tables.
516 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
519 * Don't listen to the redirect if it's
520 * for a route to an interface.
522 if (rt->rt_flags & RTF_GATEWAY) {
523 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
525 * Changing from route to net => route to host.
526 * Create new route, rather than smashing route to net.
532 flags |= RTF_GATEWAY | RTF_DYNAMIC;
533 bzero((caddr_t)&info, sizeof(info));
534 info.rti_info[RTAX_DST] = dst;
535 info.rti_info[RTAX_GATEWAY] = gateway;
536 info.rti_info[RTAX_NETMASK] = netmask;
538 info.rti_flags = flags;
539 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
542 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
543 flags = rt->rt_flags;
548 stat = &V_rtstat.rts_dynamic;
550 struct rtentry *gwrt;
553 * Smash the current notion of the gateway to
554 * this destination. Should check about netmask!!!
556 rt->rt_flags |= RTF_MODIFIED;
557 flags |= RTF_MODIFIED;
558 stat = &V_rtstat.rts_newgateway;
560 * add the key and gateway (in one malloc'd chunk).
562 rt_setgate(rt, rt_key(rt), gateway);
563 gwrt = rtalloc1(gateway, 1, 0);
564 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
568 error = EHOSTUNREACH;
574 V_rtstat.rts_badredirect++;
575 else if (stat != NULL)
577 bzero((caddr_t)&info, sizeof(info));
578 info.rti_info[RTAX_DST] = dst;
579 info.rti_info[RTAX_GATEWAY] = gateway;
580 info.rti_info[RTAX_NETMASK] = netmask;
581 info.rti_info[RTAX_AUTHOR] = src;
582 rt_missmsg(RTM_REDIRECT, &info, flags, error);
586 rtioctl(u_long req, caddr_t data)
588 return (rtioctl_fib(req, data, 0));
592 * Routing table ioctl interface.
595 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
599 * If more ioctl commands are added here, make sure the proper
600 * super-user checks are being performed because it is possible for
601 * prison-root to make it this far if raw sockets have been enabled
605 /* Multicast goop, grrr... */
606 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
613 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
615 return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
619 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
622 register struct ifaddr *ifa;
625 if ((flags & RTF_GATEWAY) == 0) {
627 * If we are adding a route to an interface,
628 * and the interface is a pt to pt link
629 * we should search for the destination
630 * as our clue to the interface. Otherwise
631 * we can use the local address.
634 if (flags & RTF_HOST)
635 ifa = ifa_ifwithdstaddr(dst);
637 ifa = ifa_ifwithaddr(gateway);
640 * If we are adding a route to a remote net
641 * or host, the gateway may still be on the
642 * other end of a pt to pt link.
644 ifa = ifa_ifwithdstaddr(gateway);
647 ifa = ifa_ifwithnet(gateway);
649 struct rtentry *rt = rtalloc1_fib(gateway, 0, 0UL, fibnum);
653 * dismiss a gateway that is reachable only
654 * through the default router
656 switch (gateway->sa_family) {
658 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
662 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
672 if ((ifa = rt->rt_ifa) == NULL)
675 if (ifa->ifa_addr->sa_family != dst->sa_family) {
676 struct ifaddr *oifa = ifa;
677 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
684 static walktree_f_t rt_fixdelete;
685 static walktree_f_t rt_fixchange;
689 struct radix_node_head *rnh;
693 * Do appropriate manipulations of a routing tree given
694 * all the bits of info needed
698 struct sockaddr *dst,
699 struct sockaddr *gateway,
700 struct sockaddr *netmask,
702 struct rtentry **ret_nrt)
704 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
708 rtrequest_fib(int req,
709 struct sockaddr *dst,
710 struct sockaddr *gateway,
711 struct sockaddr *netmask,
713 struct rtentry **ret_nrt,
716 struct rt_addrinfo info;
718 if (dst->sa_len == 0)
721 bzero((caddr_t)&info, sizeof(info));
722 info.rti_flags = flags;
723 info.rti_info[RTAX_DST] = dst;
724 info.rti_info[RTAX_GATEWAY] = gateway;
725 info.rti_info[RTAX_NETMASK] = netmask;
726 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
730 * These (questionable) definitions of apparent local variables apply
731 * to the next two functions. XXXXXX!!!
733 #define dst info->rti_info[RTAX_DST]
734 #define gateway info->rti_info[RTAX_GATEWAY]
735 #define netmask info->rti_info[RTAX_NETMASK]
736 #define ifaaddr info->rti_info[RTAX_IFA]
737 #define ifpaddr info->rti_info[RTAX_IFP]
738 #define flags info->rti_flags
741 rt_getifa(struct rt_addrinfo *info)
743 return (rt_getifa_fib(info, 0));
747 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
753 * ifp may be specified by sockaddr_dl
754 * when protocol address is ambiguous.
756 if (info->rti_ifp == NULL && ifpaddr != NULL &&
757 ifpaddr->sa_family == AF_LINK &&
758 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
759 info->rti_ifp = ifa->ifa_ifp;
760 if (info->rti_ifa == NULL && ifaaddr != NULL)
761 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
762 if (info->rti_ifa == NULL) {
765 sa = ifaaddr != NULL ? ifaaddr :
766 (gateway != NULL ? gateway : dst);
767 if (sa != NULL && info->rti_ifp != NULL)
768 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
769 else if (dst != NULL && gateway != NULL)
770 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
773 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
776 if ((ifa = info->rti_ifa) != NULL) {
777 if (info->rti_ifp == NULL)
778 info->rti_ifp = ifa->ifa_ifp;
785 * Expunges references to a route that's about to be reclaimed.
786 * The route must be locked.
789 rtexpunge(struct rtentry *rt)
791 struct radix_node *rn;
792 struct radix_node_head *rnh;
799 * We cannot assume anything about the reference count
800 * because protocols call us in many situations; often
801 * before unwinding references to the table entry.
803 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
806 * Find the correct routing tree to use for this Address Family
808 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
810 return (EAFNOSUPPORT);
812 RADIX_NODE_HEAD_LOCK(rnh);
815 * Remove the item from the tree; it should be there,
816 * but when callers invoke us blindly it may not (sigh).
818 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
823 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
824 ("unexpected flags 0x%x", rn->rn_flags));
825 KASSERT(rt == RNTORT(rn),
826 ("lookup mismatch, rt %p rn %p", rt, rn));
828 rt->rt_flags &= ~RTF_UP;
831 * Now search what's left of the subtree for any cloned
832 * routes which might have been formed from this node.
834 if ((rt->rt_flags & RTF_CLONING) && rt_mask(rt))
835 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
839 * Remove any external references we may have.
840 * This might result in another rtentry being freed if
841 * we held its last reference.
843 if (rt->rt_gwroute) {
844 RTFREE(rt->rt_gwroute);
845 rt->rt_gwroute = NULL;
849 * Give the protocol a chance to keep things in sync.
851 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
852 struct rt_addrinfo info;
854 bzero((caddr_t)&info, sizeof(info));
855 info.rti_flags = rt->rt_flags;
856 info.rti_info[RTAX_DST] = rt_key(rt);
857 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
858 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
859 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
863 * one more rtentry floating around that is not
864 * linked to the routing table.
868 RADIX_NODE_HEAD_UNLOCK(rnh);
873 rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
875 return (rtrequest1_fib(req, info, ret_nrt, 0));
879 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
883 register struct rtentry *rt;
884 register struct radix_node *rn;
885 register struct radix_node_head *rnh;
887 struct sockaddr *ndst;
888 #define senderr(x) { error = x ; goto bad; }
890 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
891 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
894 * Find the correct routing tree to use for this Address Family
896 rnh = V_rt_tables[fibnum][dst->sa_family];
898 return (EAFNOSUPPORT);
899 RADIX_NODE_HEAD_LOCK(rnh);
901 * If we are adding a host route then we don't want to put
902 * a netmask in the tree, nor do we want to clone it.
904 if (flags & RTF_HOST) {
906 flags &= ~RTF_CLONING;
912 * if we got multipath routes, we require users to specify
913 * a matching RTAX_GATEWAY.
915 if (rn_mpath_capable(rnh)) {
916 struct rtentry *rto = NULL;
918 rn = rnh->rnh_matchaddr(dst, rnh);
921 rto = rt = RNTORT(rn);
922 rt = rt_mpath_matchgate(rt, gateway);
926 * this is the first entry in the chain
929 rn = rn_mpath_next((struct radix_node *)rt);
931 * there is another entry, now it's active
936 rto->rt_flags |= RTF_UP;
938 } else if (rt->rt_flags & RTF_GATEWAY) {
940 * For gateway routes, we need to
941 * make sure that we we are deleting
942 * the correct gateway.
943 * rt_mpath_matchgate() does not
944 * check the case when there is only
945 * one route in the chain.
948 (rt->rt_gateway->sa_len != gateway->sa_len ||
949 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
953 * use the normal delete code to remove
959 * if the entry is 2nd and on up
961 if (!rt_mpath_deldup(rto, rt))
962 panic ("rtrequest1: rt_mpath_deldup");
965 rt->rt_flags &= ~RTF_UP;
966 goto deldone; /* done with the RTM_DELETE command */
972 * Remove the item from the tree and return it.
973 * Complain if it is not there and do no more processing.
975 rn = rnh->rnh_deladdr(dst, netmask, rnh);
978 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
979 panic ("rtrequest delete");
983 rt->rt_flags &= ~RTF_UP;
986 * Now search what's left of the subtree for any cloned
987 * routes which might have been formed from this node.
989 if ((rt->rt_flags & RTF_CLONING) &&
991 rnh->rnh_walktree_from(rnh, dst, rt_mask(rt),
996 * Remove any external references we may have.
997 * This might result in another rtentry being freed if
998 * we held its last reference.
1000 if (rt->rt_gwroute) {
1001 RTFREE(rt->rt_gwroute);
1002 rt->rt_gwroute = NULL;
1006 * give the protocol a chance to keep things in sync.
1008 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
1009 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1015 * One more rtentry floating around that is not
1016 * linked to the routing table. rttrash will be decremented
1017 * when RTFREE(rt) is eventually called.
1022 * If the caller wants it, then it can have it,
1023 * but it's up to it to free the rtentry as we won't be
1034 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
1038 flags = rt->rt_flags &
1039 ~(RTF_CLONING | RTF_STATIC);
1040 flags |= RTF_WASCLONED;
1041 gateway = rt->rt_gateway;
1042 if ((netmask = rt->rt_genmask) == NULL)
1047 if ((flags & RTF_GATEWAY) && !gateway)
1049 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1050 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1053 if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum)))
1055 ifa = info->rti_ifa;
1058 rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
1062 rt->rt_flags = RTF_UP | flags;
1063 rt->rt_fibnum = fibnum;
1065 * Add the gateway. Possibly re-malloc-ing the storage for it
1066 * also add the rt_gwroute if possible.
1069 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1070 RT_LOCK_DESTROY(rt);
1071 uma_zfree(rtzone, rt);
1076 * point to the (possibly newly malloc'd) dest address.
1078 ndst = (struct sockaddr *)rt_key(rt);
1081 * make sure it contains the value we want (masked if needed).
1084 rt_maskedcopy(dst, ndst, netmask);
1086 bcopy(dst, ndst, dst->sa_len);
1089 * Note that we now have a reference to the ifa.
1090 * This moved from below so that rnh->rnh_addaddr() can
1091 * examine the ifa and ifa->ifa_ifp if it so desires.
1095 rt->rt_ifp = ifa->ifa_ifp;
1098 /* do not permit exactly the same dst/mask/gw pair */
1099 if (rn_mpath_capable(rnh) &&
1100 rt_mpath_conflict(rnh, rt, netmask)) {
1102 RTFREE(rt->rt_gwroute);
1104 IFAFREE(rt->rt_ifa);
1107 RT_LOCK_DESTROY(rt);
1108 uma_zfree(rtzone, rt);
1113 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1114 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1116 struct rtentry *rt2;
1118 * Uh-oh, we already have one of these in the tree.
1119 * We do a special hack: if the route that's already
1120 * there was generated by the cloning mechanism
1121 * then we just blow it away and retry the insertion
1124 rt2 = rtalloc1_fib(dst, 0, 0, fibnum);
1125 if (rt2 && rt2->rt_parent) {
1128 rn = rnh->rnh_addaddr(ndst, netmask,
1131 /* undo the extra ref we got */
1137 * If it still failed to go into the tree,
1138 * then un-make it (this should be a function)
1142 RTFREE(rt->rt_gwroute);
1144 IFAFREE(rt->rt_ifa);
1146 RT_LOCK_DESTROY(rt);
1147 uma_zfree(rtzone, rt);
1151 rt->rt_parent = NULL;
1154 * If we got here from RESOLVE, then we are cloning
1155 * so clone the rest, and note that we
1156 * are a clone (and increment the parent's references)
1158 if (req == RTM_RESOLVE) {
1159 KASSERT(ret_nrt && *ret_nrt,
1160 ("no route to clone from"));
1161 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
1162 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
1163 if ((*ret_nrt)->rt_flags & RTF_CLONING) {
1165 * NB: We do not bump the refcnt on the parent
1166 * entry under the assumption that it will
1167 * remain so long as we do. This is
1168 * important when deleting the parent route
1169 * as this operation requires traversing
1170 * the tree to delete all clones and futzing
1171 * with refcnts requires us to double-lock
1172 * parent through this back reference.
1174 rt->rt_parent = *ret_nrt;
1179 * If this protocol has something to add to this then
1180 * allow it to do that as well.
1182 if (ifa->ifa_rtrequest)
1183 ifa->ifa_rtrequest(req, rt, info);
1186 * We repeat the same procedure from rt_setgate() here because
1187 * it doesn't fire when we call it there because the node
1188 * hasn't been added to the tree yet.
1190 if (req == RTM_ADD &&
1191 !(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
1192 struct rtfc_arg arg;
1195 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1196 rt_fixchange, &arg);
1200 * actually return a resultant rtentry and
1201 * give the caller a single reference.
1213 RADIX_NODE_HEAD_UNLOCK(rnh);
1226 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1227 * (i.e., the routes related to it by the operation of cloning). This
1228 * routine is iterated over all potential former-child-routes by way of
1229 * rnh->rnh_walktree_from() above, and those that actually are children of
1230 * the late parent (passed in as VP here) are themselves deleted.
1233 rt_fixdelete(struct radix_node *rn, void *vp)
1235 struct rtentry *rt = RNTORT(rn);
1236 struct rtentry *rt0 = vp;
1238 if (rt->rt_parent == rt0 &&
1239 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING))) {
1240 return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1241 rt->rt_flags, NULL, rt->rt_fibnum);
1247 * This routine is called from rt_setgate() to do the analogous thing for
1248 * adds and changes. There is the added complication in this case of a
1249 * middle insert; i.e., insertion of a new network route between an older
1250 * network route and (cloned) host routes. For this reason, a simple check
1251 * of rt->rt_parent is insufficient; each candidate route must be tested
1252 * against the (mask, value) of the new route (passed as before in vp)
1253 * to see if the new route matches it.
1255 * XXX - it may be possible to do fixdelete() for changes and reserve this
1256 * routine just for adds. I'm not sure why I thought it was necessary to do
1261 rt_fixchange(struct radix_node *rn, void *vp)
1263 struct rtentry *rt = RNTORT(rn);
1264 struct rtfc_arg *ap = vp;
1265 struct rtentry *rt0 = ap->rt0;
1266 struct radix_node_head *rnh = ap->rnh;
1267 u_char *xk1, *xm1, *xk2, *xmp;
1270 /* make sure we have a parent, and route is not pinned or cloning */
1271 if (!rt->rt_parent ||
1272 (rt->rt_flags & (RTF_PINNED | RTF_CLONING)))
1275 if (rt->rt_parent == rt0) /* parent match */
1278 * There probably is a function somewhere which does this...
1279 * if not, there should be.
1281 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
1283 xk1 = (u_char *)rt_key(rt0);
1284 xm1 = (u_char *)rt_mask(rt0);
1285 xk2 = (u_char *)rt_key(rt);
1287 /* avoid applying a less specific route */
1288 xmp = (u_char *)rt_mask(rt->rt_parent);
1289 mlen = rt_key(rt->rt_parent)->sa_len;
1290 if (mlen > rt_key(rt0)->sa_len) /* less specific route */
1292 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++)
1293 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i])
1294 return 0; /* less specific route */
1296 for (i = rnh->rnh_treetop->rn_offset; i < len; i++)
1297 if ((xk2[i] & xm1[i]) != xk1[i])
1298 return 0; /* no match */
1301 * OK, this node is a clone, and matches the node currently being
1302 * changed/added under the node's mask. So, get rid of it.
1305 return rtrequest_fib(RTM_DELETE, rt_key(rt), NULL,
1306 rt_mask(rt), rt->rt_flags, NULL, rt->rt_fibnum);
1310 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1312 /* XXX dst may be overwritten, can we move this to below */
1313 struct radix_node_head *rnh =
1314 V_rt_tables[rt->rt_fibnum][dst->sa_family];
1315 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1321 * A host route with the destination equal to the gateway
1322 * will interfere with keeping LLINFO in the routing
1323 * table, so disallow it.
1325 if (((rt->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) ==
1326 (RTF_HOST|RTF_GATEWAY)) &&
1327 dst->sa_len == gate->sa_len &&
1328 bcmp(dst, gate, dst->sa_len) == 0) {
1330 * The route might already exist if this is an RTM_CHANGE
1331 * or a routing redirect, so try to delete it.
1335 return EADDRNOTAVAIL;
1339 * Cloning loop avoidance in case of bad configuration.
1341 if (rt->rt_flags & RTF_GATEWAY) {
1342 struct rtentry *gwrt;
1344 RT_UNLOCK(rt); /* XXX workaround LOR */
1345 gwrt = rtalloc1_fib(gate, 1, 0, rt->rt_fibnum);
1348 return (EADDRINUSE); /* failure */
1351 * Try to reacquire the lock on rt, and if it fails,
1352 * clean state and restart from scratch.
1354 if (!RT_TRYLOCK(rt)) {
1355 RTFREE_LOCKED(gwrt);
1360 * If there is already a gwroute, then drop it. If we
1361 * are asked to replace route with itself, then do
1362 * not leak its refcounter.
1364 if (rt->rt_gwroute != NULL) {
1365 if (rt->rt_gwroute == gwrt) {
1366 RT_REMREF(rt->rt_gwroute);
1368 RTFREE(rt->rt_gwroute);
1371 if ((rt->rt_gwroute = gwrt) != NULL)
1372 RT_UNLOCK(rt->rt_gwroute);
1376 * Prepare to store the gateway in rt->rt_gateway.
1377 * Both dst and gateway are stored one after the other in the same
1378 * malloc'd chunk. If we have room, we can reuse the old buffer,
1379 * rt_gateway already points to the right place.
1380 * Otherwise, malloc a new block and update the 'dst' address.
1382 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1385 R_Malloc(new, caddr_t, dlen + glen);
1389 * XXX note, we copy from *dst and not *rt_key(rt) because
1390 * rt_setgate() can be called to initialize a newly
1391 * allocated route entry, in which case rt_key(rt) == NULL
1392 * (and also rt->rt_gateway == NULL).
1393 * Free()/free() handle a NULL argument just fine.
1395 bcopy(dst, new, dlen);
1396 Free(rt_key(rt)); /* free old block, if any */
1397 rt_key(rt) = (struct sockaddr *)new;
1398 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1402 * Copy the new gateway value into the memory chunk.
1404 bcopy(gate, rt->rt_gateway, glen);
1407 * This isn't going to do anything useful for host routes, so
1408 * don't bother. Also make sure we have a reasonable mask
1409 * (we don't yet have one during adds).
1411 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
1412 struct rtfc_arg arg;
1416 RT_UNLOCK(rt); /* XXX workaround LOR */
1417 RADIX_NODE_HEAD_LOCK(rnh);
1419 rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
1420 rt_fixchange, &arg);
1421 RADIX_NODE_HEAD_UNLOCK(rnh);
1428 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1430 register u_char *cp1 = (u_char *)src;
1431 register u_char *cp2 = (u_char *)dst;
1432 register u_char *cp3 = (u_char *)netmask;
1433 u_char *cplim = cp2 + *cp3;
1434 u_char *cplim2 = cp2 + *cp1;
1436 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1441 *cp2++ = *cp1++ & *cp3++;
1443 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1447 * Set up a routing table entry, normally
1450 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1452 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1454 struct sockaddr *dst;
1455 struct sockaddr *netmask;
1456 struct rtentry *rt = NULL;
1457 struct rt_addrinfo info;
1459 int startfib, endfib;
1460 char tempbuf[_SOCKADDR_TMPSIZE];
1464 if (flags & RTF_HOST) {
1465 dst = ifa->ifa_dstaddr;
1468 dst = ifa->ifa_addr;
1469 netmask = ifa->ifa_netmask;
1471 if ( dst->sa_family != AF_INET)
1474 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1475 startfib = endfib = curthread->td_proc->p_fibnum;
1478 endfib = rt_numfibs - 1;
1481 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1485 if (dst->sa_len == 0)
1489 * If it's a delete, check that if it exists,
1490 * it's on the correct interface or we might scrub
1491 * a route to another ifa which would
1492 * be confusing at best and possibly worse.
1494 if (cmd == RTM_DELETE) {
1496 * It's a delete, so it should already exist..
1497 * If it's a net, mask off the host bits
1498 * (Assuming we have a mask)
1499 * XXX this is kinda inet specific..
1501 if (netmask != NULL) {
1502 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1503 dst = (struct sockaddr *)tempbuf;
1507 * Now go through all the requested tables (fibs) and do the
1508 * requested action. Realistically, this will either be fib 0
1509 * for protocols that don't do multiple tables or all the
1510 * tables for those that do. XXX For this version only AF_INET.
1511 * When that changes code should be refactored to protocol
1512 * independent parts and protocol dependent parts.
1514 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1515 if (cmd == RTM_DELETE) {
1516 struct radix_node_head *rnh;
1517 struct radix_node *rn;
1519 * Look up an rtentry that is in the routing tree and
1520 * contains the correct info.
1522 if ((rnh = V_rt_tables[fibnum][dst->sa_family]) == NULL)
1523 /* this table doesn't exist but others might */
1525 RADIX_NODE_HEAD_LOCK(rnh);
1527 if (rn_mpath_capable(rnh)) {
1529 rn = rnh->rnh_matchaddr(dst, rnh);
1535 * for interface route the
1536 * rt->rt_gateway is sockaddr_intf
1537 * for cloning ARP entries, so
1538 * rt_mpath_matchgate must use the
1541 rt = rt_mpath_matchgate(rt,
1549 rn = rnh->rnh_lookup(dst, netmask, rnh);
1550 error = (rn == NULL ||
1551 (rn->rn_flags & RNF_ROOT) ||
1552 RNTORT(rn)->rt_ifa != ifa ||
1553 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1554 RADIX_NODE_HEAD_UNLOCK(rnh);
1556 /* this is only an error if bad on ALL tables */
1561 * Do the actual request
1563 bzero((caddr_t)&info, sizeof(info));
1565 info.rti_flags = flags | ifa->ifa_flags;
1566 info.rti_info[RTAX_DST] = dst;
1567 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1568 info.rti_info[RTAX_NETMASK] = netmask;
1569 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1570 if (error == 0 && rt != NULL) {
1572 * notify any listening routing agents of the change
1577 * in case address alias finds the first address
1578 * e.g. ifconfig bge0 192.103.54.246/24
1579 * e.g. ifconfig bge0 192.103.54.247/24
1580 * the address set in the route is 192.103.54.246
1581 * so we need to replace it with 192.103.54.247
1583 if (memcmp(rt->rt_ifa->ifa_addr,
1584 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1585 IFAFREE(rt->rt_ifa);
1587 rt->rt_ifp = ifa->ifa_ifp;
1591 rt_newaddrmsg(cmd, ifa, error, rt);
1592 if (cmd == RTM_DELETE) {
1594 * If we are deleting, and we found an entry,
1595 * then it's been removed from the tree..
1596 * now throw it away.
1600 if (cmd == RTM_ADD) {
1602 * We just wanted to add it..
1603 * we don't actually need a reference.
1614 if (cmd == RTM_DELETE) {
1618 /* we only give an error if it wasn't in any table */
1619 error = ((flags & RTF_HOST) ?
1620 EHOSTUNREACH : ENETUNREACH);
1624 /* return an error if any of them failed */
1631 /* special one for inet internal use. may not use. */
1633 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1635 return (rtinit1(ifa, cmd, flags, -1));
1639 * Set up a routing table entry, normally
1643 rtinit(struct ifaddr *ifa, int cmd, int flags)
1645 struct sockaddr *dst;
1648 if (flags & RTF_HOST) {
1649 dst = ifa->ifa_dstaddr;
1651 dst = ifa->ifa_addr;
1654 if (dst->sa_family == AF_INET)
1656 return (rtinit1(ifa, cmd, flags, fib));
1660 * rt_check() is invoked on each layer 2 output path, prior to
1661 * encapsulating outbound packets.
1663 * The function is mostly used to find a routing entry for the gateway,
1664 * which in some protocol families could also point to the link-level
1665 * address for the gateway itself (the side effect of revalidating the
1666 * route to the destination is rather pointless at this stage, we did it
1667 * already a moment before in the pr_output() routine to locate the ifp
1668 * and gateway to use).
1670 * When we remove the layer-3 to layer-2 mapping tables from the
1671 * routing table, this function can be removed.
1674 * *dst is the address of the NEXT HOP (which coincides with the
1675 * final destination if directly reachable);
1676 * *lrt0 points to the cached route to the final destination;
1677 * *lrt is not meaningful;
1680 * If the route is marked down try to find a new route. If the route
1681 * to the gateway is gone, try to setup a new route. Otherwise,
1682 * if the route is marked for packets to be rejected, enforce that.
1685 * *dst is unchanged;
1686 * *lrt0 points to the (possibly new) route to the final destination
1687 * *lrt points to the route to the next hop
1689 * Their values are meaningful ONLY if no error is returned.
1692 rt_check(struct rtentry **lrt, struct rtentry **lrt0, struct sockaddr *dst)
1695 struct rtentry *rt0;
1699 KASSERT(*lrt0 != NULL, ("rt_check"));
1701 fibnum = rt0->rt_fibnum;
1703 /* NB: the locking here is tortuous... */
1705 if ((rt->rt_flags & RTF_UP) == 0) {
1707 rt = rtalloc1_fib(dst, 1, 0UL, fibnum);
1710 /* XXX what about if change? */
1712 return (EHOSTUNREACH);
1715 /* XXX BSD/OS checks dst->sa_family != AF_NS */
1716 if (rt->rt_flags & RTF_GATEWAY) {
1717 if (rt->rt_gwroute == NULL)
1719 rt = rt->rt_gwroute;
1720 RT_LOCK(rt); /* NB: gwroute */
1721 if ((rt->rt_flags & RTF_UP) == 0) {
1722 RTFREE_LOCKED(rt); /* unlock gwroute */
1724 rt0->rt_gwroute = NULL;
1727 rt = rtalloc1_fib(rt->rt_gateway, 1, 0UL, fibnum);
1731 return (ENETUNREACH);
1734 if (rt0->rt_gwroute != NULL)
1735 RTFREE(rt0->rt_gwroute);
1736 rt0->rt_gwroute = rt;
1739 return (EHOSTUNREACH);
1744 /* XXX why are we inspecting rmx_expire? */
1745 error = (rt->rt_flags & RTF_REJECT) &&
1746 (rt->rt_rmx.rmx_expire == 0 ||
1747 time_uptime < rt->rt_rmx.rmx_expire);
1750 return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1758 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1759 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);