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/syslog.h>
45 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/sysproto.h>
52 #include <sys/domain.h>
53 #include <sys/kernel.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
61 #include <net/radix_mpath.h>
64 #include <netinet/in.h>
65 #include <netinet/ip_mroute.h>
69 u_int rt_numfibs = RT_NUMFIBS;
70 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
72 * Allow the boot code to allow LESS than RT_MAXFIBS to be used.
73 * We can't do more because storage is statically allocated for now.
74 * (for compatibility reasons.. this will change).
76 TUNABLE_INT("net.fibs", &rt_numfibs);
79 * By default add routes to all fibs for new interfaces.
80 * Once this is set to 0 then only allocate routes on interface
81 * changes for the FIB of the caller when adding a new set of addresses
82 * to an interface. XXX this is a shotgun aproach to a problem that needs
83 * a more fine grained solution.. that will come.
85 u_int rt_add_addr_allfibs = 1;
86 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
87 &rt_add_addr_allfibs, 0, "");
88 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
90 VNET_DEFINE(struct radix_node_head *, rt_tables);
91 static VNET_DEFINE(uma_zone_t, rtzone); /* Routing table UMA zone. */
92 VNET_DEFINE(int, rttrash); /* routes not in table but not freed */
93 VNET_DEFINE(struct rtstat, rtstat);
95 #define V_rt_tables VNET(rt_tables)
96 #define V_rtzone VNET(rtzone)
97 #define V_rttrash VNET(rttrash)
98 #define V_rtstat VNET(rtstat)
100 static void rt_maskedcopy(struct sockaddr *,
101 struct sockaddr *, struct sockaddr *);
103 /* compare two sockaddr structures */
104 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
107 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
108 * The operation can be done safely (in this code) because a
109 * 'struct rtentry' starts with two 'struct radix_node''s, the first
110 * one representing leaf nodes in the routing tree, which is
111 * what the code in radix.c passes us as a 'struct radix_node'.
113 * But because there are a lot of assumptions in this conversion,
114 * do not cast explicitly, but always use the macro below.
116 #define RNTORT(p) ((struct rtentry *)(p))
119 /* default fib for tunnels to use */
120 u_int tunnel_fib = 0;
121 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
125 * handler for net.my_fibnum
128 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
133 fibnum = curthread->td_proc->p_fibnum;
134 error = sysctl_handle_int(oidp, &fibnum, 0, req);
138 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
139 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
141 static __inline struct radix_node_head **
142 rt_tables_get_rnh_ptr(int table, int fam)
144 struct radix_node_head **rnh;
146 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
148 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
151 /* rnh is [fib=0][af=0]. */
152 rnh = (struct radix_node_head **)V_rt_tables;
153 /* Get the offset to the requested table and fam. */
154 rnh += table * (AF_MAX+1) + fam;
159 struct radix_node_head *
160 rt_tables_get_rnh(int table, int fam)
163 return (*rt_tables_get_rnh_ptr(table, fam));
167 * route initialization must occur before ip6_init2(), which happenas at
174 /* whack the tunable ints into line. */
175 if (rt_numfibs > RT_MAXFIBS)
176 rt_numfibs = RT_MAXFIBS;
179 rn_init(); /* initialize all zeroes, all ones, mask table */
181 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
184 vnet_route_init(const void *unused __unused)
187 struct radix_node_head **rnh;
191 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
192 sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO);
194 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
195 NULL, NULL, UMA_ALIGN_PTR, 0);
196 for (dom = domains; dom; dom = dom->dom_next) {
197 if (dom->dom_rtattach) {
198 for (table = 0; table < rt_numfibs; table++) {
199 if ( (fam = dom->dom_family) == AF_INET ||
201 /* for now only AF_INET has > 1 table */
203 * rtattach will be also called
204 * from vfs_export.c but the
206 * (only for AF_INET and AF_INET6
207 * which don't need it anyhow)
209 rnh = rt_tables_get_rnh_ptr(table, fam);
211 panic("%s: rnh NULL", __func__);
212 dom->dom_rtattach((void **)rnh,
221 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
226 vnet_route_uninit(const void *unused __unused)
231 struct radix_node_head **rnh;
233 for (dom = domains; dom; dom = dom->dom_next) {
234 if (dom->dom_rtdetach) {
235 for (table = 0; table < rt_numfibs; table++) {
236 if ( (fam = dom->dom_family) == AF_INET ||
238 /* For now only AF_INET has > 1 tbl. */
239 rnh = rt_tables_get_rnh_ptr(table, fam);
241 panic("%s: rnh NULL", __func__);
242 dom->dom_rtdetach((void **)rnh,
251 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
252 vnet_route_uninit, 0);
255 #ifndef _SYS_SYSPROTO_H_
261 setfib(struct thread *td, struct setfib_args *uap)
263 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
265 td->td_proc->p_fibnum = uap->fibnum;
270 * Packet routing routines.
273 rtalloc(struct route *ro)
275 rtalloc_ign_fib(ro, 0UL, 0);
279 rtalloc_fib(struct route *ro, u_int fibnum)
281 rtalloc_ign_fib(ro, 0UL, fibnum);
285 rtalloc_ign(struct route *ro, u_long ignore)
289 if ((rt = ro->ro_rt) != NULL) {
290 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
295 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
297 RT_UNLOCK(ro->ro_rt);
301 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
305 if ((rt = ro->ro_rt) != NULL) {
306 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
311 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
313 RT_UNLOCK(ro->ro_rt);
317 * Look up the route that matches the address given
318 * Or, at least try.. Create a cloned route if needed.
320 * The returned route, if any, is locked.
323 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
325 return (rtalloc1_fib(dst, report, ignflags, 0));
329 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
332 struct radix_node_head *rnh;
334 struct radix_node *rn;
335 struct rtentry *newrt;
336 struct rt_addrinfo info;
337 int err = 0, msgtype = RTM_MISS;
340 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
341 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
343 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
346 * Look up the address in the table for that Address Family
349 V_rtstat.rts_unreach++;
352 needlock = !(ignflags & RTF_RNH_LOCKED);
354 RADIX_NODE_HEAD_RLOCK(rnh);
357 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
359 rn = rnh->rnh_matchaddr(dst, rnh);
360 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
361 newrt = rt = RNTORT(rn);
365 RADIX_NODE_HEAD_RUNLOCK(rnh);
369 RADIX_NODE_HEAD_RUNLOCK(rnh);
372 * Either we hit the root or couldn't find any match,
373 * Which basically means
374 * "caint get there frm here"
376 V_rtstat.rts_unreach++;
380 * If required, report the failure to the supervising
382 * For a delete, this is not an error. (report == 0)
384 bzero(&info, sizeof(info));
385 info.rti_info[RTAX_DST] = dst;
386 rt_missmsg(msgtype, &info, 0, err);
390 RT_LOCK_ASSERT(newrt);
395 * Remove a reference count from an rtentry.
396 * If the count gets low enough, take it out of the routing table
399 rtfree(struct rtentry *rt)
401 struct radix_node_head *rnh;
403 KASSERT(rt != NULL,("%s: NULL rt", __func__));
404 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
405 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
410 * The callers should use RTFREE_LOCKED() or RTFREE(), so
411 * we should come here exactly with the last reference.
414 if (rt->rt_refcnt > 0) {
415 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
420 * On last reference give the "close method" a chance
421 * to cleanup private state. This also permits (for
422 * IPv4 and IPv6) a chance to decide if the routing table
423 * entry should be purged immediately or at a later time.
424 * When an immediate purge is to happen the close routine
425 * typically calls rtexpunge which clears the RTF_UP flag
426 * on the entry so that the code below reclaims the storage.
428 if (rt->rt_refcnt == 0 && rnh->rnh_close)
429 rnh->rnh_close((struct radix_node *)rt, rnh);
432 * If we are no longer "up" (and ref == 0)
433 * then we can free the resources associated
436 if ((rt->rt_flags & RTF_UP) == 0) {
437 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
440 * the rtentry must have been removed from the routing table
441 * so it is represented in rttrash.. remove that now.
445 if (rt->rt_refcnt < 0) {
446 printf("rtfree: %p not freed (neg refs)\n", rt);
451 * release references on items we hold them on..
452 * e.g other routes and ifaddrs.
455 ifa_free(rt->rt_ifa);
457 * The key is separatly alloc'd so free it (see rt_setgate()).
458 * This also frees the gateway, as they are always malloc'd
464 * and the rtentry itself of course
467 uma_zfree(V_rtzone, rt);
476 * Force a routing table entry to the specified
477 * destination to go through the given gateway.
478 * Normally called as a result of a routing redirect
479 * message from the network layer.
482 rtredirect(struct sockaddr *dst,
483 struct sockaddr *gateway,
484 struct sockaddr *netmask,
486 struct sockaddr *src)
488 rtredirect_fib(dst, gateway, netmask, flags, src, 0);
492 rtredirect_fib(struct sockaddr *dst,
493 struct sockaddr *gateway,
494 struct sockaddr *netmask,
496 struct sockaddr *src,
499 struct rtentry *rt, *rt0 = NULL;
502 struct rt_addrinfo info;
504 struct radix_node_head *rnh;
507 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
509 error = EAFNOSUPPORT;
513 /* verify the gateway is directly reachable */
514 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
518 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
520 * If the redirect isn't from our current router for this dst,
521 * it's either old or wrong. If it redirects us to ourselves,
522 * we have a routing loop, perhaps as a result of an interface
523 * going down recently.
525 if (!(flags & RTF_DONE) && rt &&
526 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
528 else if (ifa_ifwithaddr_check(gateway))
529 error = EHOSTUNREACH;
533 * Create a new entry if we just got back a wildcard entry
534 * or the the lookup failed. This is necessary for hosts
535 * which use routing redirects generated by smart gateways
536 * to dynamically build the routing tables.
538 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
541 * Don't listen to the redirect if it's
542 * for a route to an interface.
544 if (rt->rt_flags & RTF_GATEWAY) {
545 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
547 * Changing from route to net => route to host.
548 * Create new route, rather than smashing route to net.
554 flags |= RTF_GATEWAY | RTF_DYNAMIC;
555 bzero((caddr_t)&info, sizeof(info));
556 info.rti_info[RTAX_DST] = dst;
557 info.rti_info[RTAX_GATEWAY] = gateway;
558 info.rti_info[RTAX_NETMASK] = netmask;
560 info.rti_flags = flags;
562 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */
563 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
567 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
568 flags = rt->rt_flags;
573 stat = &V_rtstat.rts_dynamic;
575 struct rtentry *gwrt;
578 * Smash the current notion of the gateway to
579 * this destination. Should check about netmask!!!
581 rt->rt_flags |= RTF_MODIFIED;
582 flags |= RTF_MODIFIED;
583 stat = &V_rtstat.rts_newgateway;
585 * add the key and gateway (in one malloc'd chunk).
588 RADIX_NODE_HEAD_LOCK(rnh);
590 rt_setgate(rt, rt_key(rt), gateway);
591 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
592 RADIX_NODE_HEAD_UNLOCK(rnh);
593 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
597 error = EHOSTUNREACH;
603 V_rtstat.rts_badredirect++;
604 else if (stat != NULL)
606 bzero((caddr_t)&info, sizeof(info));
607 info.rti_info[RTAX_DST] = dst;
608 info.rti_info[RTAX_GATEWAY] = gateway;
609 info.rti_info[RTAX_NETMASK] = netmask;
610 info.rti_info[RTAX_AUTHOR] = src;
611 rt_missmsg(RTM_REDIRECT, &info, flags, error);
617 rtioctl(u_long req, caddr_t data)
619 return (rtioctl_fib(req, data, 0));
623 * Routing table ioctl interface.
626 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
630 * If more ioctl commands are added here, make sure the proper
631 * super-user checks are being performed because it is possible for
632 * prison-root to make it this far if raw sockets have been enabled
636 /* Multicast goop, grrr... */
637 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
644 * For both ifa_ifwithroute() routines, 'ifa' is returned referenced.
647 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
649 return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
653 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
656 register struct ifaddr *ifa;
659 if ((flags & RTF_GATEWAY) == 0) {
661 * If we are adding a route to an interface,
662 * and the interface is a pt to pt link
663 * we should search for the destination
664 * as our clue to the interface. Otherwise
665 * we can use the local address.
668 if (flags & RTF_HOST)
669 ifa = ifa_ifwithdstaddr(dst);
671 ifa = ifa_ifwithaddr(gateway);
674 * If we are adding a route to a remote net
675 * or host, the gateway may still be on the
676 * other end of a pt to pt link.
678 ifa = ifa_ifwithdstaddr(gateway);
681 ifa = ifa_ifwithnet(gateway);
683 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
687 * dismiss a gateway that is reachable only
688 * through the default router
690 switch (gateway->sa_family) {
692 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
696 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
702 if (!not_found && rt->rt_ifa != NULL) {
708 if (not_found || ifa == NULL)
711 if (ifa->ifa_addr->sa_family != dst->sa_family) {
712 struct ifaddr *oifa = ifa;
713 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
723 * Do appropriate manipulations of a routing tree given
724 * all the bits of info needed
728 struct sockaddr *dst,
729 struct sockaddr *gateway,
730 struct sockaddr *netmask,
732 struct rtentry **ret_nrt)
734 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
738 rtrequest_fib(int req,
739 struct sockaddr *dst,
740 struct sockaddr *gateway,
741 struct sockaddr *netmask,
743 struct rtentry **ret_nrt,
746 struct rt_addrinfo info;
748 if (dst->sa_len == 0)
751 bzero((caddr_t)&info, sizeof(info));
752 info.rti_flags = flags;
753 info.rti_info[RTAX_DST] = dst;
754 info.rti_info[RTAX_GATEWAY] = gateway;
755 info.rti_info[RTAX_NETMASK] = netmask;
756 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
760 * These (questionable) definitions of apparent local variables apply
761 * to the next two functions. XXXXXX!!!
763 #define dst info->rti_info[RTAX_DST]
764 #define gateway info->rti_info[RTAX_GATEWAY]
765 #define netmask info->rti_info[RTAX_NETMASK]
766 #define ifaaddr info->rti_info[RTAX_IFA]
767 #define ifpaddr info->rti_info[RTAX_IFP]
768 #define flags info->rti_flags
771 rt_getifa(struct rt_addrinfo *info)
773 return (rt_getifa_fib(info, 0));
777 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined,
778 * it will be referenced so the caller must free it.
781 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
787 * ifp may be specified by sockaddr_dl
788 * when protocol address is ambiguous.
790 if (info->rti_ifp == NULL && ifpaddr != NULL &&
791 ifpaddr->sa_family == AF_LINK &&
792 (ifa = ifa_ifwithnet(ifpaddr)) != NULL) {
793 info->rti_ifp = ifa->ifa_ifp;
796 if (info->rti_ifa == NULL && ifaaddr != NULL)
797 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
798 if (info->rti_ifa == NULL) {
801 sa = ifaaddr != NULL ? ifaaddr :
802 (gateway != NULL ? gateway : dst);
803 if (sa != NULL && info->rti_ifp != NULL)
804 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
805 else if (dst != NULL && gateway != NULL)
806 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
809 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
812 if ((ifa = info->rti_ifa) != NULL) {
813 if (info->rti_ifp == NULL)
814 info->rti_ifp = ifa->ifa_ifp;
821 * Expunges references to a route that's about to be reclaimed.
822 * The route must be locked.
825 rtexpunge(struct rtentry *rt)
827 struct radix_node *rn;
828 struct radix_node_head *rnh;
833 * Find the correct routing tree to use for this Address Family
835 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
838 return (EAFNOSUPPORT);
839 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
842 * We cannot assume anything about the reference count
843 * because protocols call us in many situations; often
844 * before unwinding references to the table entry.
846 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
849 * Remove the item from the tree; it should be there,
850 * but when callers invoke us blindly it may not (sigh).
852 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
857 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
858 ("unexpected flags 0x%x", rn->rn_flags));
859 KASSERT(rt == RNTORT(rn),
860 ("lookup mismatch, rt %p rn %p", rt, rn));
862 rt->rt_flags &= ~RTF_UP;
865 * Give the protocol a chance to keep things in sync.
867 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
868 struct rt_addrinfo info;
870 bzero((caddr_t)&info, sizeof(info));
871 info.rti_flags = rt->rt_flags;
872 info.rti_info[RTAX_DST] = rt_key(rt);
873 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
874 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
875 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
879 * one more rtentry floating around that is not
880 * linked to the routing table.
889 rn_mpath_update(int req, struct rt_addrinfo *info,
890 struct radix_node_head *rnh, struct rtentry **ret_nrt)
893 * if we got multipath routes, we require users to specify
894 * a matching RTAX_GATEWAY.
896 struct rtentry *rt, *rto = NULL;
897 register struct radix_node *rn;
900 rn = rnh->rnh_matchaddr(dst, rnh);
903 rto = rt = RNTORT(rn);
904 rt = rt_mpath_matchgate(rt, gateway);
908 * this is the first entry in the chain
911 rn = rn_mpath_next((struct radix_node *)rt);
913 * there is another entry, now it's active
918 rto->rt_flags |= RTF_UP;
920 } else if (rt->rt_flags & RTF_GATEWAY) {
922 * For gateway routes, we need to
923 * make sure that we we are deleting
924 * the correct gateway.
925 * rt_mpath_matchgate() does not
926 * check the case when there is only
927 * one route in the chain.
930 (rt->rt_gateway->sa_len != gateway->sa_len ||
931 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
935 * remove from tree before returning it
938 rn = rnh->rnh_deladdr(dst, netmask, rnh);
939 KASSERT(rt == RNTORT(rn), ("radix node disappeared"));
945 * use the normal delete code to remove
948 if (req != RTM_DELETE)
956 * if the entry is 2nd and on up
958 if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt))
959 panic ("rtrequest1: rt_mpath_deldup");
963 if (req == RTM_DELETE) {
964 rt->rt_flags &= ~RTF_UP;
966 * One more rtentry floating around that is not
967 * linked to the routing table. rttrash will be decremented
968 * when RTFREE(rt) is eventually called.
974 if (req != RTM_DELETE)
975 panic("unrecognized request %d", req);
979 * If the caller wants it, then it can have it,
980 * but it's up to it to free the rtentry as we won't be
994 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
997 int error = 0, needlock = 0;
998 register struct rtentry *rt;
999 register struct radix_node *rn;
1000 register struct radix_node_head *rnh;
1002 struct sockaddr *ndst;
1003 #define senderr(x) { error = x ; goto bad; }
1005 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
1006 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
1009 * Find the correct routing tree to use for this Address Family
1011 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1013 return (EAFNOSUPPORT);
1014 needlock = ((flags & RTF_RNH_LOCKED) == 0);
1015 flags &= ~RTF_RNH_LOCKED;
1017 RADIX_NODE_HEAD_LOCK(rnh);
1019 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1021 * If we are adding a host route then we don't want to put
1022 * a netmask in the tree, nor do we want to clone it.
1024 if (flags & RTF_HOST)
1030 if (rn_mpath_capable(rnh)) {
1031 error = rn_mpath_update(req, info, rnh, ret_nrt);
1033 * "bad" holds true for the success case
1036 if (error != ENOENT)
1041 * Remove the item from the tree and return it.
1042 * Complain if it is not there and do no more processing.
1044 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1047 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
1048 panic ("rtrequest delete");
1052 rt->rt_flags &= ~RTF_UP;
1055 * give the protocol a chance to keep things in sync.
1057 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
1058 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1061 * One more rtentry floating around that is not
1062 * linked to the routing table. rttrash will be decremented
1063 * when RTFREE(rt) is eventually called.
1068 * If the caller wants it, then it can have it,
1069 * but it's up to it to free the rtentry as we won't be
1080 * resolve was only used for route cloning
1085 if ((flags & RTF_GATEWAY) && !gateway)
1087 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1088 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1091 if (info->rti_ifa == NULL) {
1092 error = rt_getifa_fib(info, fibnum);
1096 ifa_ref(info->rti_ifa);
1097 ifa = info->rti_ifa;
1098 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
1105 rt->rt_flags = RTF_UP | flags;
1106 rt->rt_fibnum = fibnum;
1108 * Add the gateway. Possibly re-malloc-ing the storage for it
1112 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1113 RT_LOCK_DESTROY(rt);
1116 uma_zfree(V_rtzone, rt);
1121 * point to the (possibly newly malloc'd) dest address.
1123 ndst = (struct sockaddr *)rt_key(rt);
1126 * make sure it contains the value we want (masked if needed).
1129 rt_maskedcopy(dst, ndst, netmask);
1131 bcopy(dst, ndst, dst->sa_len);
1134 * We use the ifa reference returned by rt_getifa_fib().
1135 * This moved from below so that rnh->rnh_addaddr() can
1136 * examine the ifa and ifa->ifa_ifp if it so desires.
1139 rt->rt_ifp = ifa->ifa_ifp;
1140 rt->rt_rmx.rmx_weight = 1;
1143 /* do not permit exactly the same dst/mask/gw pair */
1144 if (rn_mpath_capable(rnh) &&
1145 rt_mpath_conflict(rnh, rt, netmask)) {
1147 ifa_free(rt->rt_ifa);
1150 RT_LOCK_DESTROY(rt);
1151 uma_zfree(V_rtzone, rt);
1156 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1157 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1159 * If it still failed to go into the tree,
1160 * then un-make it (this should be a function)
1164 ifa_free(rt->rt_ifa);
1166 RT_LOCK_DESTROY(rt);
1167 uma_zfree(V_rtzone, rt);
1172 * If this protocol has something to add to this then
1173 * allow it to do that as well.
1175 if (ifa->ifa_rtrequest)
1176 ifa->ifa_rtrequest(req, rt, info);
1179 * actually return a resultant rtentry and
1180 * give the caller a single reference.
1193 RADIX_NODE_HEAD_UNLOCK(rnh);
1206 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1208 /* XXX dst may be overwritten, can we move this to below */
1209 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1211 struct radix_node_head *rnh;
1213 rnh = rt_tables_get_rnh(rt->rt_fibnum, dst->sa_family);
1217 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1220 * Prepare to store the gateway in rt->rt_gateway.
1221 * Both dst and gateway are stored one after the other in the same
1222 * malloc'd chunk. If we have room, we can reuse the old buffer,
1223 * rt_gateway already points to the right place.
1224 * Otherwise, malloc a new block and update the 'dst' address.
1226 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1229 R_Malloc(new, caddr_t, dlen + glen);
1233 * XXX note, we copy from *dst and not *rt_key(rt) because
1234 * rt_setgate() can be called to initialize a newly
1235 * allocated route entry, in which case rt_key(rt) == NULL
1236 * (and also rt->rt_gateway == NULL).
1237 * Free()/free() handle a NULL argument just fine.
1239 bcopy(dst, new, dlen);
1240 Free(rt_key(rt)); /* free old block, if any */
1241 rt_key(rt) = (struct sockaddr *)new;
1242 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1246 * Copy the new gateway value into the memory chunk.
1248 bcopy(gate, rt->rt_gateway, glen);
1254 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1256 register u_char *cp1 = (u_char *)src;
1257 register u_char *cp2 = (u_char *)dst;
1258 register u_char *cp3 = (u_char *)netmask;
1259 u_char *cplim = cp2 + *cp3;
1260 u_char *cplim2 = cp2 + *cp1;
1262 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1267 *cp2++ = *cp1++ & *cp3++;
1269 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1273 * Set up a routing table entry, normally
1276 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1278 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1280 struct sockaddr *dst;
1281 struct sockaddr *netmask;
1282 struct rtentry *rt = NULL;
1283 struct rt_addrinfo info;
1285 int startfib, endfib;
1286 char tempbuf[_SOCKADDR_TMPSIZE];
1289 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1291 if (flags & RTF_HOST) {
1292 dst = ifa->ifa_dstaddr;
1295 dst = ifa->ifa_addr;
1296 netmask = ifa->ifa_netmask;
1298 if ( dst->sa_family != AF_INET)
1301 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1302 startfib = endfib = curthread->td_proc->p_fibnum;
1305 endfib = rt_numfibs - 1;
1308 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1312 if (dst->sa_len == 0)
1316 * If it's a delete, check that if it exists,
1317 * it's on the correct interface or we might scrub
1318 * a route to another ifa which would
1319 * be confusing at best and possibly worse.
1321 if (cmd == RTM_DELETE) {
1323 * It's a delete, so it should already exist..
1324 * If it's a net, mask off the host bits
1325 * (Assuming we have a mask)
1326 * XXX this is kinda inet specific..
1328 if (netmask != NULL) {
1329 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1330 dst = (struct sockaddr *)tempbuf;
1334 * Now go through all the requested tables (fibs) and do the
1335 * requested action. Realistically, this will either be fib 0
1336 * for protocols that don't do multiple tables or all the
1337 * tables for those that do. XXX For this version only AF_INET.
1338 * When that changes code should be refactored to protocol
1339 * independent parts and protocol dependent parts.
1341 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1342 if (cmd == RTM_DELETE) {
1343 struct radix_node_head *rnh;
1344 struct radix_node *rn;
1346 * Look up an rtentry that is in the routing tree and
1347 * contains the correct info.
1349 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1351 /* this table doesn't exist but others might */
1353 RADIX_NODE_HEAD_LOCK(rnh);
1355 if (rn_mpath_capable(rnh)) {
1357 rn = rnh->rnh_matchaddr(dst, rnh);
1363 * for interface route the
1364 * rt->rt_gateway is sockaddr_intf
1365 * for cloning ARP entries, so
1366 * rt_mpath_matchgate must use the
1369 rt = rt_mpath_matchgate(rt,
1377 rn = rnh->rnh_lookup(dst, netmask, rnh);
1378 error = (rn == NULL ||
1379 (rn->rn_flags & RNF_ROOT) ||
1380 RNTORT(rn)->rt_ifa != ifa ||
1381 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1382 RADIX_NODE_HEAD_UNLOCK(rnh);
1384 /* this is only an error if bad on ALL tables */
1389 * Do the actual request
1391 bzero((caddr_t)&info, sizeof(info));
1393 info.rti_flags = flags | ifa->ifa_flags;
1394 info.rti_info[RTAX_DST] = dst;
1396 * doing this for compatibility reasons
1399 info.rti_info[RTAX_GATEWAY] =
1400 (struct sockaddr *)&null_sdl;
1402 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1403 info.rti_info[RTAX_NETMASK] = netmask;
1404 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1405 if (error == 0 && rt != NULL) {
1407 * notify any listening routing agents of the change
1412 * in case address alias finds the first address
1413 * e.g. ifconfig bge0 192.103.54.246/24
1414 * e.g. ifconfig bge0 192.103.54.247/24
1415 * the address set in the route is 192.103.54.246
1416 * so we need to replace it with 192.103.54.247
1418 if (memcmp(rt->rt_ifa->ifa_addr,
1419 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1420 ifa_free(rt->rt_ifa);
1422 rt->rt_ifp = ifa->ifa_ifp;
1427 * doing this for compatibility reasons
1429 if (cmd == RTM_ADD) {
1430 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1431 rt->rt_ifp->if_type;
1432 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1433 rt->rt_ifp->if_index;
1435 rt_newaddrmsg(cmd, ifa, error, rt);
1436 if (cmd == RTM_DELETE) {
1438 * If we are deleting, and we found an entry,
1439 * then it's been removed from the tree..
1440 * now throw it away.
1444 if (cmd == RTM_ADD) {
1446 * We just wanted to add it..
1447 * we don't actually need a reference.
1458 if (cmd == RTM_DELETE) {
1462 /* we only give an error if it wasn't in any table */
1463 error = ((flags & RTF_HOST) ?
1464 EHOSTUNREACH : ENETUNREACH);
1468 /* return an error if any of them failed */
1475 /* special one for inet internal use. may not use. */
1477 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1479 return (rtinit1(ifa, cmd, flags, -1));
1483 * Set up a routing table entry, normally
1487 rtinit(struct ifaddr *ifa, int cmd, int flags)
1489 struct sockaddr *dst;
1492 if (flags & RTF_HOST) {
1493 dst = ifa->ifa_dstaddr;
1495 dst = ifa->ifa_addr;
1498 if (dst->sa_family == AF_INET)
1500 return (rtinit1(ifa, cmd, flags, fib));