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_inet6.h"
39 #include "opt_route.h"
40 #include "opt_mrouting.h"
41 #include "opt_mpath.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/syslog.h>
46 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/syslog.h>
51 #include <sys/sysproto.h>
53 #include <sys/domain.h>
54 #include <sys/kernel.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
61 #include <net/flowtable.h>
64 #include <net/radix_mpath.h>
67 #include <netinet/in.h>
68 #include <netinet/ip_mroute.h>
72 #define RT_MAXFIBS UINT16_MAX
74 /* Kernel config default option. */
77 #error "ROUTETABLES defined too low"
79 #if ROUTETABLES > RT_MAXFIBS
80 #error "ROUTETABLES defined too big"
82 #define RT_NUMFIBS ROUTETABLES
83 #endif /* ROUTETABLES */
84 /* Initialize to default if not otherwise set. */
89 /* This is read-only.. */
90 u_int rt_numfibs = RT_NUMFIBS;
91 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
92 /* and this can be set too big but will be fixed before it is used */
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.
101 * XXX also has the problems getting the FIB from curthread which will not
102 * always work given the fib can be overridden and prefixes can be added
103 * from the network stack context.
105 u_int rt_add_addr_allfibs = 1;
106 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
107 &rt_add_addr_allfibs, 0, "");
108 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
110 VNET_DEFINE(struct rtstat, rtstat);
111 #define V_rtstat VNET(rtstat)
113 VNET_DEFINE(struct radix_node_head *, rt_tables);
114 #define V_rt_tables VNET(rt_tables)
116 VNET_DEFINE(int, rttrash); /* routes not in table but not freed */
117 #define V_rttrash VNET(rttrash)
120 /* compare two sockaddr structures */
121 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
124 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
125 * The operation can be done safely (in this code) because a
126 * 'struct rtentry' starts with two 'struct radix_node''s, the first
127 * one representing leaf nodes in the routing tree, which is
128 * what the code in radix.c passes us as a 'struct radix_node'.
130 * But because there are a lot of assumptions in this conversion,
131 * do not cast explicitly, but always use the macro below.
133 #define RNTORT(p) ((struct rtentry *)(p))
135 static VNET_DEFINE(uma_zone_t, rtzone); /* Routing table UMA zone. */
136 #define V_rtzone VNET(rtzone)
139 * handler for net.my_fibnum
142 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
147 fibnum = curthread->td_proc->p_fibnum;
148 error = sysctl_handle_int(oidp, &fibnum, 0, req);
152 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
153 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
155 static __inline struct radix_node_head **
156 rt_tables_get_rnh_ptr(int table, int fam)
158 struct radix_node_head **rnh;
160 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
162 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
165 /* rnh is [fib=0][af=0]. */
166 rnh = (struct radix_node_head **)V_rt_tables;
167 /* Get the offset to the requested table and fam. */
168 rnh += table * (AF_MAX+1) + fam;
173 struct radix_node_head *
174 rt_tables_get_rnh(int table, int fam)
177 return (*rt_tables_get_rnh_ptr(table, fam));
181 * route initialization must occur before ip6_init2(), which happenas at
188 /* whack the tunable ints into line. */
189 if (rt_numfibs > RT_MAXFIBS)
190 rt_numfibs = RT_MAXFIBS;
194 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
197 vnet_route_init(const void *unused __unused)
200 struct radix_node_head **rnh;
204 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
205 sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO);
207 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
208 NULL, NULL, UMA_ALIGN_PTR, 0);
209 for (dom = domains; dom; dom = dom->dom_next) {
210 if (dom->dom_rtattach == NULL)
213 for (table = 0; table < rt_numfibs; table++) {
214 fam = dom->dom_family;
215 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
219 * XXX MRT rtattach will be also called from
220 * vfs_export.c but the offset will be 0 (only for
221 * AF_INET and AF_INET6 which don't need it anyhow).
223 rnh = rt_tables_get_rnh_ptr(table, fam);
225 panic("%s: rnh NULL", __func__);
226 dom->dom_rtattach((void **)rnh, dom->dom_rtoffset);
230 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
235 vnet_route_uninit(const void *unused __unused)
240 struct radix_node_head **rnh;
242 for (dom = domains; dom; dom = dom->dom_next) {
243 if (dom->dom_rtdetach == NULL)
246 for (table = 0; table < rt_numfibs; table++) {
247 fam = dom->dom_family;
249 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
252 rnh = rt_tables_get_rnh_ptr(table, fam);
254 panic("%s: rnh NULL", __func__);
255 dom->dom_rtdetach((void **)rnh, dom->dom_rtoffset);
259 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
260 vnet_route_uninit, 0);
263 #ifndef _SYS_SYSPROTO_H_
269 sys_setfib(struct thread *td, struct setfib_args *uap)
271 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
273 td->td_proc->p_fibnum = uap->fibnum;
278 * Packet routing routines.
281 rtalloc(struct route *ro)
284 rtalloc_ign_fib(ro, 0UL, RT_DEFAULT_FIB);
288 rtalloc_fib(struct route *ro, u_int fibnum)
290 rtalloc_ign_fib(ro, 0UL, fibnum);
294 rtalloc_ign(struct route *ro, u_long ignore)
298 if ((rt = ro->ro_rt) != NULL) {
299 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
304 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, RT_DEFAULT_FIB);
306 RT_UNLOCK(ro->ro_rt);
310 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
314 if ((rt = ro->ro_rt) != NULL) {
315 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
320 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
322 RT_UNLOCK(ro->ro_rt);
326 * Look up the route that matches the address given
327 * Or, at least try.. Create a cloned route if needed.
329 * The returned route, if any, is locked.
332 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
335 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB));
339 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
342 struct radix_node_head *rnh;
343 struct radix_node *rn;
344 struct rtentry *newrt;
345 struct rt_addrinfo info;
346 int err = 0, msgtype = RTM_MISS;
349 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
350 switch (dst->sa_family) {
353 /* We support multiple FIBs. */
356 fibnum = RT_DEFAULT_FIB;
359 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
365 * Look up the address in the table for that Address Family
367 needlock = !(ignflags & RTF_RNH_LOCKED);
369 RADIX_NODE_HEAD_RLOCK(rnh);
372 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
374 rn = rnh->rnh_matchaddr(dst, rnh);
375 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
380 RADIX_NODE_HEAD_RUNLOCK(rnh);
384 RADIX_NODE_HEAD_RUNLOCK(rnh);
387 * Either we hit the root or couldn't find any match,
388 * Which basically means
389 * "caint get there frm here"
392 V_rtstat.rts_unreach++;
396 * If required, report the failure to the supervising
398 * For a delete, this is not an error. (report == 0)
400 bzero(&info, sizeof(info));
401 info.rti_info[RTAX_DST] = dst;
402 rt_missmsg_fib(msgtype, &info, 0, err, fibnum);
406 RT_LOCK_ASSERT(newrt);
411 * Remove a reference count from an rtentry.
412 * If the count gets low enough, take it out of the routing table
415 rtfree(struct rtentry *rt)
417 struct radix_node_head *rnh;
419 KASSERT(rt != NULL,("%s: NULL rt", __func__));
420 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
421 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
426 * The callers should use RTFREE_LOCKED() or RTFREE(), so
427 * we should come here exactly with the last reference.
430 if (rt->rt_refcnt > 0) {
431 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
436 * On last reference give the "close method" a chance
437 * to cleanup private state. This also permits (for
438 * IPv4 and IPv6) a chance to decide if the routing table
439 * entry should be purged immediately or at a later time.
440 * When an immediate purge is to happen the close routine
441 * typically calls rtexpunge which clears the RTF_UP flag
442 * on the entry so that the code below reclaims the storage.
444 if (rt->rt_refcnt == 0 && rnh->rnh_close)
445 rnh->rnh_close((struct radix_node *)rt, rnh);
448 * If we are no longer "up" (and ref == 0)
449 * then we can free the resources associated
452 if ((rt->rt_flags & RTF_UP) == 0) {
453 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
456 * the rtentry must have been removed from the routing table
457 * so it is represented in rttrash.. remove that now.
461 if (rt->rt_refcnt < 0) {
462 printf("rtfree: %p not freed (neg refs)\n", rt);
467 * release references on items we hold them on..
468 * e.g other routes and ifaddrs.
471 ifa_free(rt->rt_ifa);
473 * The key is separatly alloc'd so free it (see rt_setgate()).
474 * This also frees the gateway, as they are always malloc'd
480 * and the rtentry itself of course
483 uma_zfree(V_rtzone, rt);
492 * Force a routing table entry to the specified
493 * destination to go through the given gateway.
494 * Normally called as a result of a routing redirect
495 * message from the network layer.
498 rtredirect(struct sockaddr *dst,
499 struct sockaddr *gateway,
500 struct sockaddr *netmask,
502 struct sockaddr *src)
505 rtredirect_fib(dst, gateway, netmask, flags, src, RT_DEFAULT_FIB);
509 rtredirect_fib(struct sockaddr *dst,
510 struct sockaddr *gateway,
511 struct sockaddr *netmask,
513 struct sockaddr *src,
516 struct rtentry *rt, *rt0 = NULL;
519 struct rt_addrinfo info;
521 struct radix_node_head *rnh;
524 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
526 error = EAFNOSUPPORT;
530 /* verify the gateway is directly reachable */
531 if ((ifa = ifa_ifwithnet(gateway, 0)) == NULL) {
535 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
537 * If the redirect isn't from our current router for this dst,
538 * it's either old or wrong. If it redirects us to ourselves,
539 * we have a routing loop, perhaps as a result of an interface
540 * going down recently.
542 if (!(flags & RTF_DONE) && rt &&
543 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
545 else if (ifa_ifwithaddr_check(gateway))
546 error = EHOSTUNREACH;
550 * Create a new entry if we just got back a wildcard entry
551 * or the lookup failed. This is necessary for hosts
552 * which use routing redirects generated by smart gateways
553 * to dynamically build the routing tables.
555 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
558 * Don't listen to the redirect if it's
559 * for a route to an interface.
561 if (rt->rt_flags & RTF_GATEWAY) {
562 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
564 * Changing from route to net => route to host.
565 * Create new route, rather than smashing route to net.
571 flags |= RTF_GATEWAY | RTF_DYNAMIC;
572 bzero((caddr_t)&info, sizeof(info));
573 info.rti_info[RTAX_DST] = dst;
574 info.rti_info[RTAX_GATEWAY] = gateway;
575 info.rti_info[RTAX_NETMASK] = netmask;
577 info.rti_flags = flags;
579 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */
580 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
584 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
585 flags = rt->rt_flags;
590 stat = &V_rtstat.rts_dynamic;
592 struct rtentry *gwrt;
595 * Smash the current notion of the gateway to
596 * this destination. Should check about netmask!!!
598 rt->rt_flags |= RTF_MODIFIED;
599 flags |= RTF_MODIFIED;
600 stat = &V_rtstat.rts_newgateway;
602 * add the key and gateway (in one malloc'd chunk).
605 RADIX_NODE_HEAD_LOCK(rnh);
607 rt_setgate(rt, rt_key(rt), gateway);
608 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
609 RADIX_NODE_HEAD_UNLOCK(rnh);
610 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
614 error = EHOSTUNREACH;
620 V_rtstat.rts_badredirect++;
621 else if (stat != NULL)
623 bzero((caddr_t)&info, sizeof(info));
624 info.rti_info[RTAX_DST] = dst;
625 info.rti_info[RTAX_GATEWAY] = gateway;
626 info.rti_info[RTAX_NETMASK] = netmask;
627 info.rti_info[RTAX_AUTHOR] = src;
628 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
634 rtioctl(u_long req, caddr_t data)
637 return (rtioctl_fib(req, data, RT_DEFAULT_FIB));
641 * Routing table ioctl interface.
644 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
648 * If more ioctl commands are added here, make sure the proper
649 * super-user checks are being performed because it is possible for
650 * prison-root to make it this far if raw sockets have been enabled
654 /* Multicast goop, grrr... */
655 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
662 * For both ifa_ifwithroute() routines, 'ifa' is returned referenced.
665 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
668 return (ifa_ifwithroute_fib(flags, dst, gateway, RT_DEFAULT_FIB));
672 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
675 register struct ifaddr *ifa;
678 if ((flags & RTF_GATEWAY) == 0) {
680 * If we are adding a route to an interface,
681 * and the interface is a pt to pt link
682 * we should search for the destination
683 * as our clue to the interface. Otherwise
684 * we can use the local address.
687 if (flags & RTF_HOST)
688 ifa = ifa_ifwithdstaddr(dst);
690 ifa = ifa_ifwithaddr(gateway);
693 * If we are adding a route to a remote net
694 * or host, the gateway may still be on the
695 * other end of a pt to pt link.
697 ifa = ifa_ifwithdstaddr(gateway);
700 ifa = ifa_ifwithnet(gateway, 0);
702 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
706 * dismiss a gateway that is reachable only
707 * through the default router
709 switch (gateway->sa_family) {
711 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
715 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
721 if (!not_found && rt->rt_ifa != NULL) {
727 if (not_found || ifa == NULL)
730 if (ifa->ifa_addr->sa_family != dst->sa_family) {
731 struct ifaddr *oifa = ifa;
732 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
742 * Do appropriate manipulations of a routing tree given
743 * all the bits of info needed
747 struct sockaddr *dst,
748 struct sockaddr *gateway,
749 struct sockaddr *netmask,
751 struct rtentry **ret_nrt)
754 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt,
759 rtrequest_fib(int req,
760 struct sockaddr *dst,
761 struct sockaddr *gateway,
762 struct sockaddr *netmask,
764 struct rtentry **ret_nrt,
767 struct rt_addrinfo info;
769 if (dst->sa_len == 0)
772 bzero((caddr_t)&info, sizeof(info));
773 info.rti_flags = flags;
774 info.rti_info[RTAX_DST] = dst;
775 info.rti_info[RTAX_GATEWAY] = gateway;
776 info.rti_info[RTAX_NETMASK] = netmask;
777 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
781 * These (questionable) definitions of apparent local variables apply
782 * to the next two functions. XXXXXX!!!
784 #define dst info->rti_info[RTAX_DST]
785 #define gateway info->rti_info[RTAX_GATEWAY]
786 #define netmask info->rti_info[RTAX_NETMASK]
787 #define ifaaddr info->rti_info[RTAX_IFA]
788 #define ifpaddr info->rti_info[RTAX_IFP]
789 #define flags info->rti_flags
792 rt_getifa(struct rt_addrinfo *info)
795 return (rt_getifa_fib(info, RT_DEFAULT_FIB));
799 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined,
800 * it will be referenced so the caller must free it.
803 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
809 * ifp may be specified by sockaddr_dl
810 * when protocol address is ambiguous.
812 if (info->rti_ifp == NULL && ifpaddr != NULL &&
813 ifpaddr->sa_family == AF_LINK &&
814 (ifa = ifa_ifwithnet(ifpaddr, 0)) != NULL) {
815 info->rti_ifp = ifa->ifa_ifp;
818 if (info->rti_ifa == NULL && ifaaddr != NULL)
819 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
820 if (info->rti_ifa == NULL) {
823 sa = ifaaddr != NULL ? ifaaddr :
824 (gateway != NULL ? gateway : dst);
825 if (sa != NULL && info->rti_ifp != NULL)
826 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
827 else if (dst != NULL && gateway != NULL)
828 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
831 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
834 if ((ifa = info->rti_ifa) != NULL) {
835 if (info->rti_ifp == NULL)
836 info->rti_ifp = ifa->ifa_ifp;
843 * Expunges references to a route that's about to be reclaimed.
844 * The route must be locked.
847 rtexpunge(struct rtentry *rt)
849 #if !defined(RADIX_MPATH)
850 struct radix_node *rn;
852 struct rt_addrinfo info;
856 struct radix_node_head *rnh;
861 * Find the correct routing tree to use for this Address Family
863 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
866 return (EAFNOSUPPORT);
867 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
871 bzero(&info, sizeof(info));
872 info.rti_ifp = rt->rt_ifp;
873 info.rti_flags = RTF_RNH_LOCKED;
874 info.rti_info[RTAX_DST] = rt_key(rt);
875 info.rti_info[RTAX_GATEWAY] = rt->rt_ifa->ifa_addr;
878 error = rtrequest1_fib(RTM_DELETE, &info, &rt0, fib);
880 if (error == 0 && rt0 != NULL) {
883 } else if (error != 0) {
889 * Remove the item from the tree; it should be there,
890 * but when callers invoke us blindly it may not (sigh).
892 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
897 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
898 ("unexpected flags 0x%x", rn->rn_flags));
899 KASSERT(rt == RNTORT(rn),
900 ("lookup mismatch, rt %p rn %p", rt, rn));
901 #endif /* RADIX_MPATH */
903 rt->rt_flags &= ~RTF_UP;
906 * Give the protocol a chance to keep things in sync.
908 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
909 struct rt_addrinfo info;
911 bzero((caddr_t)&info, sizeof(info));
912 info.rti_flags = rt->rt_flags;
913 info.rti_info[RTAX_DST] = rt_key(rt);
914 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
915 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
916 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
920 * one more rtentry floating around that is not
921 * linked to the routing table.
924 #if !defined(RADIX_MPATH)
932 rn_mpath_update(int req, struct rt_addrinfo *info,
933 struct radix_node_head *rnh, struct rtentry **ret_nrt)
936 * if we got multipath routes, we require users to specify
937 * a matching RTAX_GATEWAY.
939 struct rtentry *rt, *rto = NULL;
940 register struct radix_node *rn;
943 rn = rnh->rnh_matchaddr(dst, rnh);
946 rto = rt = RNTORT(rn);
947 rt = rt_mpath_matchgate(rt, gateway);
951 * this is the first entry in the chain
954 rn = rn_mpath_next((struct radix_node *)rt);
956 * there is another entry, now it's active
961 rto->rt_flags |= RTF_UP;
963 } else if (rt->rt_flags & RTF_GATEWAY) {
965 * For gateway routes, we need to
966 * make sure that we we are deleting
967 * the correct gateway.
968 * rt_mpath_matchgate() does not
969 * check the case when there is only
970 * one route in the chain.
973 (rt->rt_gateway->sa_len != gateway->sa_len ||
974 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
978 * remove from tree before returning it
981 rn = rnh->rnh_deladdr(dst, netmask, rnh);
982 KASSERT(rt == RNTORT(rn), ("radix node disappeared"));
988 * use the normal delete code to remove
991 if (req != RTM_DELETE)
999 * if the entry is 2nd and on up
1001 if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt))
1002 panic ("rtrequest1: rt_mpath_deldup");
1006 if (req == RTM_DELETE) {
1007 rt->rt_flags &= ~RTF_UP;
1009 * One more rtentry floating around that is not
1010 * linked to the routing table. rttrash will be decremented
1011 * when RTFREE(rt) is eventually called.
1017 if (req != RTM_DELETE)
1018 panic("unrecognized request %d", req);
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
1037 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
1040 int error = 0, needlock = 0;
1041 register struct rtentry *rt;
1043 register struct rtentry *rt0;
1045 register struct radix_node *rn;
1046 register struct radix_node_head *rnh;
1048 struct sockaddr *ndst;
1049 struct sockaddr_storage mdst;
1050 #define senderr(x) { error = x ; goto bad; }
1052 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
1053 switch (dst->sa_family) {
1056 /* We support multiple FIBs. */
1059 fibnum = RT_DEFAULT_FIB;
1064 * Find the correct routing tree to use for this Address Family
1066 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1068 return (EAFNOSUPPORT);
1069 needlock = ((flags & RTF_RNH_LOCKED) == 0);
1070 flags &= ~RTF_RNH_LOCKED;
1072 RADIX_NODE_HEAD_LOCK(rnh);
1074 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1076 * If we are adding a host route then we don't want to put
1077 * a netmask in the tree, nor do we want to clone it.
1079 if (flags & RTF_HOST)
1085 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
1086 dst = (struct sockaddr *)&mdst;
1089 if (rn_mpath_capable(rnh)) {
1090 error = rn_mpath_update(req, info, rnh, ret_nrt);
1092 * "bad" holds true for the success case
1095 if (error != ENOENT)
1100 if ((flags & RTF_PINNED) == 0) {
1101 /* Check if target route can be deleted */
1102 rt = (struct rtentry *)rnh->rnh_lookup(dst,
1104 if ((rt != NULL) && (rt->rt_flags & RTF_PINNED))
1105 senderr(EADDRINUSE);
1109 * Remove the item from the tree and return it.
1110 * Complain if it is not there and do no more processing.
1112 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1115 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
1116 panic ("rtrequest delete");
1120 rt->rt_flags &= ~RTF_UP;
1123 * give the protocol a chance to keep things in sync.
1125 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
1126 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1129 * One more rtentry floating around that is not
1130 * linked to the routing table. rttrash will be decremented
1131 * when RTFREE(rt) is eventually called.
1136 * If the caller wants it, then it can have it,
1137 * but it's up to it to free the rtentry as we won't be
1148 * resolve was only used for route cloning
1153 if ((flags & RTF_GATEWAY) && !gateway)
1155 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1156 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1159 if (info->rti_ifa == NULL) {
1160 error = rt_getifa_fib(info, fibnum);
1164 ifa_ref(info->rti_ifa);
1165 ifa = info->rti_ifa;
1166 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
1172 rt->rt_flags = RTF_UP | flags;
1173 rt->rt_fibnum = fibnum;
1175 * Add the gateway. Possibly re-malloc-ing the storage for it.
1178 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1179 RT_LOCK_DESTROY(rt);
1181 uma_zfree(V_rtzone, rt);
1186 * point to the (possibly newly malloc'd) dest address.
1188 ndst = (struct sockaddr *)rt_key(rt);
1191 * make sure it contains the value we want (masked if needed).
1194 rt_maskedcopy(dst, ndst, netmask);
1196 bcopy(dst, ndst, dst->sa_len);
1199 * We use the ifa reference returned by rt_getifa_fib().
1200 * This moved from below so that rnh->rnh_addaddr() can
1201 * examine the ifa and ifa->ifa_ifp if it so desires.
1204 rt->rt_ifp = ifa->ifa_ifp;
1205 rt->rt_rmx.rmx_weight = 1;
1208 /* do not permit exactly the same dst/mask/gw pair */
1209 if (rn_mpath_capable(rnh) &&
1210 rt_mpath_conflict(rnh, rt, netmask)) {
1211 ifa_free(rt->rt_ifa);
1213 RT_LOCK_DESTROY(rt);
1214 uma_zfree(V_rtzone, rt);
1221 /* "flow-table" only supports IPv6 and IPv4 at the moment. */
1222 switch (dst->sa_family) {
1229 #if defined(INET6) || defined(INET)
1230 rn = rnh->rnh_matchaddr(dst, rnh);
1231 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
1232 struct sockaddr *mask;
1237 * compare mask to see if the new route is
1238 * more specific than the existing one
1245 * A host route is already present, so
1246 * leave the flow-table entries as is.
1248 if (rt0->rt_flags & RTF_HOST) {
1251 } else if (!(flags & RTF_HOST) && netmask) {
1252 mask = rt_mask(rt0);
1255 n = (u_char *)netmask;
1262 if (len == 0 || (*n < *m)) {
1268 #endif/* INET6 || INET */
1270 #endif /* FLOWTABLE */
1272 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1273 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1275 * If it still failed to go into the tree,
1276 * then un-make it (this should be a function)
1279 ifa_free(rt->rt_ifa);
1281 RT_LOCK_DESTROY(rt);
1282 uma_zfree(V_rtzone, rt);
1290 else if (rt0 != NULL) {
1291 switch (dst->sa_family) {
1294 flowtable_route_flush(V_ip6_ft, rt0);
1299 flowtable_route_flush(V_ip_ft, rt0);
1308 * If this protocol has something to add to this then
1309 * allow it to do that as well.
1311 if (ifa->ifa_rtrequest)
1312 ifa->ifa_rtrequest(req, rt, info);
1315 * actually return a resultant rtentry and
1316 * give the caller a single reference.
1329 RADIX_NODE_HEAD_UNLOCK(rnh);
1342 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1344 /* XXX dst may be overwritten, can we move this to below */
1345 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1347 struct radix_node_head *rnh;
1349 rnh = rt_tables_get_rnh(rt->rt_fibnum, dst->sa_family);
1353 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1356 * Prepare to store the gateway in rt->rt_gateway.
1357 * Both dst and gateway are stored one after the other in the same
1358 * malloc'd chunk. If we have room, we can reuse the old buffer,
1359 * rt_gateway already points to the right place.
1360 * Otherwise, malloc a new block and update the 'dst' address.
1362 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1365 R_Malloc(new, caddr_t, dlen + glen);
1369 * XXX note, we copy from *dst and not *rt_key(rt) because
1370 * rt_setgate() can be called to initialize a newly
1371 * allocated route entry, in which case rt_key(rt) == NULL
1372 * (and also rt->rt_gateway == NULL).
1373 * Free()/free() handle a NULL argument just fine.
1375 bcopy(dst, new, dlen);
1376 Free(rt_key(rt)); /* free old block, if any */
1377 rt_key(rt) = (struct sockaddr *)new;
1378 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1382 * Copy the new gateway value into the memory chunk.
1384 bcopy(gate, rt->rt_gateway, glen);
1390 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1392 register u_char *cp1 = (u_char *)src;
1393 register u_char *cp2 = (u_char *)dst;
1394 register u_char *cp3 = (u_char *)netmask;
1395 u_char *cplim = cp2 + *cp3;
1396 u_char *cplim2 = cp2 + *cp1;
1398 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1403 *cp2++ = *cp1++ & *cp3++;
1405 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1409 * Set up a routing table entry, normally
1412 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1414 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1416 struct sockaddr *dst;
1417 struct sockaddr *netmask;
1418 struct rtentry *rt = NULL;
1419 struct rt_addrinfo info;
1421 int startfib, endfib;
1422 char tempbuf[_SOCKADDR_TMPSIZE];
1425 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1426 struct radix_node_head *rnh;
1428 if (flags & RTF_HOST) {
1429 dst = ifa->ifa_dstaddr;
1432 dst = ifa->ifa_addr;
1433 netmask = ifa->ifa_netmask;
1435 if (dst->sa_len == 0)
1437 switch (dst->sa_family) {
1440 /* We support multiple FIBs. */
1443 fibnum = RT_DEFAULT_FIB;
1447 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1448 startfib = endfib = curthread->td_proc->p_fibnum;
1451 endfib = rt_numfibs - 1;
1454 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1460 * If it's a delete, check that if it exists,
1461 * it's on the correct interface or we might scrub
1462 * a route to another ifa which would
1463 * be confusing at best and possibly worse.
1465 if (cmd == RTM_DELETE) {
1467 * It's a delete, so it should already exist..
1468 * If it's a net, mask off the host bits
1469 * (Assuming we have a mask)
1470 * XXX this is kinda inet specific..
1472 if (netmask != NULL) {
1473 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1474 dst = (struct sockaddr *)tempbuf;
1478 * Now go through all the requested tables (fibs) and do the
1479 * requested action. Realistically, this will either be fib 0
1480 * for protocols that don't do multiple tables or all the
1481 * tables for those that do.
1483 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1484 if (cmd == RTM_DELETE) {
1485 struct radix_node *rn;
1487 * Look up an rtentry that is in the routing tree and
1488 * contains the correct info.
1490 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1492 /* this table doesn't exist but others might */
1494 RADIX_NODE_HEAD_RLOCK(rnh);
1496 if (rn_mpath_capable(rnh)) {
1498 rn = rnh->rnh_matchaddr(dst, rnh);
1504 * for interface route the
1505 * rt->rt_gateway is sockaddr_intf
1506 * for cloning ARP entries, so
1507 * rt_mpath_matchgate must use the
1510 rt = rt_mpath_matchgate(rt,
1518 rn = rnh->rnh_lookup(dst, netmask, rnh);
1519 error = (rn == NULL ||
1520 (rn->rn_flags & RNF_ROOT) ||
1521 RNTORT(rn)->rt_ifa != ifa ||
1522 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1523 RADIX_NODE_HEAD_RUNLOCK(rnh);
1525 /* this is only an error if bad on ALL tables */
1530 * Do the actual request
1532 bzero((caddr_t)&info, sizeof(info));
1534 info.rti_flags = flags |
1535 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1536 info.rti_info[RTAX_DST] = dst;
1538 * doing this for compatibility reasons
1541 info.rti_info[RTAX_GATEWAY] =
1542 (struct sockaddr *)&null_sdl;
1544 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1545 info.rti_info[RTAX_NETMASK] = netmask;
1546 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1548 if ((error == EEXIST) && (cmd == RTM_ADD)) {
1550 * Interface route addition failed.
1551 * Atomically delete current prefix generating
1552 * RTM_DELETE message, and retry adding
1555 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1556 RADIX_NODE_HEAD_LOCK(rnh);
1558 /* Delete old prefix */
1559 info.rti_ifa = NULL;
1560 info.rti_flags = RTF_RNH_LOCKED;
1562 error = rtrequest1_fib(RTM_DELETE, &info, NULL, fibnum);
1565 info.rti_flags = flags | RTF_RNH_LOCKED |
1566 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1567 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1570 RADIX_NODE_HEAD_UNLOCK(rnh);
1574 if (error == 0 && rt != NULL) {
1576 * notify any listening routing agents of the change
1581 * in case address alias finds the first address
1582 * e.g. ifconfig bge0 192.0.2.246/24
1583 * e.g. ifconfig bge0 192.0.2.247/24
1584 * the address set in the route is 192.0.2.246
1585 * so we need to replace it with 192.0.2.247
1587 if (memcmp(rt->rt_ifa->ifa_addr,
1588 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1589 ifa_free(rt->rt_ifa);
1591 rt->rt_ifp = ifa->ifa_ifp;
1596 * doing this for compatibility reasons
1598 if (cmd == RTM_ADD) {
1599 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1600 rt->rt_ifp->if_type;
1601 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1602 rt->rt_ifp->if_index;
1606 rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum);
1609 if (cmd == RTM_DELETE) {
1611 * If we are deleting, and we found an entry,
1612 * then it's been removed from the tree..
1613 * now throw it away.
1617 if (cmd == RTM_ADD) {
1619 * We just wanted to add it..
1620 * we don't actually need a reference.
1631 if (cmd == RTM_DELETE) {
1635 /* we only give an error if it wasn't in any table */
1636 error = ((flags & RTF_HOST) ?
1637 EHOSTUNREACH : ENETUNREACH);
1641 /* return an error if any of them failed */
1648 #ifndef BURN_BRIDGES
1649 /* special one for inet internal use. may not use. */
1651 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1653 return (rtinit1(ifa, cmd, flags, -1));
1658 * Set up a routing table entry, normally
1662 rtinit(struct ifaddr *ifa, int cmd, int flags)
1664 struct sockaddr *dst;
1665 int fib = RT_DEFAULT_FIB;
1667 if (flags & RTF_HOST) {
1668 dst = ifa->ifa_dstaddr;
1670 dst = ifa->ifa_addr;
1673 switch (dst->sa_family) {
1676 /* We do support multiple FIBs. */
1680 return (rtinit1(ifa, cmd, flags, fib));