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>
54 #include <sys/vimage.h>
57 #include <net/if_dl.h>
58 #include <net/route.h>
61 #include <net/radix_mpath.h>
65 #include <netinet/in.h>
66 #include <netinet/ip_mroute.h>
67 #include <netinet/vinet.h>
71 u_int rt_numfibs = RT_NUMFIBS;
72 SYSCTL_INT(_net, OID_AUTO, fibs, CTLFLAG_RD, &rt_numfibs, 0, "");
74 * Allow the boot code to allow LESS than RT_MAXFIBS to be used.
75 * We can't do more because storage is statically allocated for now.
76 * (for compatibility reasons.. this will change).
78 TUNABLE_INT("net.fibs", &rt_numfibs);
81 * By default add routes to all fibs for new interfaces.
82 * Once this is set to 0 then only allocate routes on interface
83 * changes for the FIB of the caller when adding a new set of addresses
84 * to an interface. XXX this is a shotgun aproach to a problem that needs
85 * a more fine grained solution.. that will come.
87 u_int rt_add_addr_allfibs = 1;
88 SYSCTL_INT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RW,
89 &rt_add_addr_allfibs, 0, "");
90 TUNABLE_INT("net.add_addr_allfibs", &rt_add_addr_allfibs);
93 static struct rtstat rtstat;
95 /* by default only the first 'row' of tables will be accessed. */
97 * XXXMRT When we fix netstat, and do this differnetly,
98 * we can allocate this dynamically. As long as we are keeping
99 * things backwards compaitble we need to allocate this
102 struct radix_node_head *rt_tables[RT_MAXFIBS][AF_MAX+1];
104 static int rttrash; /* routes not in table but not freed */
107 static void rt_maskedcopy(struct sockaddr *,
108 struct sockaddr *, struct sockaddr *);
110 /* compare two sockaddr structures */
111 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
114 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
115 * The operation can be done safely (in this code) because a
116 * 'struct rtentry' starts with two 'struct radix_node''s, the first
117 * one representing leaf nodes in the routing tree, which is
118 * what the code in radix.c passes us as a 'struct radix_node'.
120 * But because there are a lot of assumptions in this conversion,
121 * do not cast explicitly, but always use the macro below.
123 #define RNTORT(p) ((struct rtentry *)(p))
125 static uma_zone_t rtzone; /* Routing table UMA zone. */
128 /* default fib for tunnels to use */
129 u_int tunnel_fib = 0;
130 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
134 * handler for net.my_fibnum
137 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
142 fibnum = curthread->td_proc->p_fibnum;
143 error = sysctl_handle_int(oidp, &fibnum, 0, req);
147 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
148 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
153 INIT_VNET_INET(curvnet);
158 /* whack the tunable ints into line. */
159 if (rt_numfibs > RT_MAXFIBS)
160 rt_numfibs = RT_MAXFIBS;
163 rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
164 NULL, NULL, UMA_ALIGN_PTR, 0);
165 rn_init(); /* initialize all zeroes, all ones, mask table */
167 for (dom = domains; dom; dom = dom->dom_next) {
168 if (dom->dom_rtattach) {
169 for (table = 0; table < rt_numfibs; table++) {
170 if ( (fam = dom->dom_family) == AF_INET ||
172 /* for now only AF_INET has > 1 table */
174 * rtattach will be also called
175 * from vfs_export.c but the
177 * (only for AF_INET and AF_INET6
178 * which don't need it anyhow)
181 (void **)&V_rt_tables[table][fam],
191 #ifndef _SYS_SYSPROTO_H_
197 setfib(struct thread *td, struct setfib_args *uap)
199 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
201 td->td_proc->p_fibnum = uap->fibnum;
206 * Packet routing routines.
209 rtalloc(struct route *ro)
211 rtalloc_ign_fib(ro, 0UL, 0);
215 rtalloc_fib(struct route *ro, u_int fibnum)
217 rtalloc_ign_fib(ro, 0UL, fibnum);
221 rtalloc_ign(struct route *ro, u_long ignore)
225 if ((rt = ro->ro_rt) != NULL) {
226 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
231 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
233 RT_UNLOCK(ro->ro_rt);
237 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
241 if ((rt = ro->ro_rt) != NULL) {
242 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
247 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
249 RT_UNLOCK(ro->ro_rt);
253 * Look up the route that matches the address given
254 * Or, at least try.. Create a cloned route if needed.
256 * The returned route, if any, is locked.
259 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
261 return (rtalloc1_fib(dst, report, ignflags, 0));
265 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
268 INIT_VNET_NET(curvnet);
269 struct radix_node_head *rnh;
271 struct radix_node *rn;
272 struct rtentry *newrt;
273 struct rt_addrinfo info;
274 int err = 0, msgtype = RTM_MISS;
277 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
278 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
280 rnh = V_rt_tables[fibnum][dst->sa_family];
283 * Look up the address in the table for that Address Family
286 V_rtstat.rts_unreach++;
289 needlock = !(ignflags & RTF_RNH_LOCKED);
291 RADIX_NODE_HEAD_RLOCK(rnh);
294 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
296 rn = rnh->rnh_matchaddr(dst, rnh);
297 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
298 newrt = rt = RNTORT(rn);
302 RADIX_NODE_HEAD_RUNLOCK(rnh);
306 RADIX_NODE_HEAD_RUNLOCK(rnh);
309 * Either we hit the root or couldn't find any match,
310 * Which basically means
311 * "caint get there frm here"
313 V_rtstat.rts_unreach++;
317 * If required, report the failure to the supervising
319 * For a delete, this is not an error. (report == 0)
321 bzero(&info, sizeof(info));
322 info.rti_info[RTAX_DST] = dst;
323 rt_missmsg(msgtype, &info, 0, err);
327 RT_LOCK_ASSERT(newrt);
332 * Remove a reference count from an rtentry.
333 * If the count gets low enough, take it out of the routing table
336 rtfree(struct rtentry *rt)
338 INIT_VNET_NET(curvnet);
339 struct radix_node_head *rnh;
341 KASSERT(rt != NULL,("%s: NULL rt", __func__));
342 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
343 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
348 * The callers should use RTFREE_LOCKED() or RTFREE(), so
349 * we should come here exactly with the last reference.
352 if (rt->rt_refcnt > 0) {
353 log(LOG_DEBUG, "%s: %p has %d refs\t", __func__, rt, rt->rt_refcnt);
358 * On last reference give the "close method" a chance
359 * to cleanup private state. This also permits (for
360 * IPv4 and IPv6) a chance to decide if the routing table
361 * entry should be purged immediately or at a later time.
362 * When an immediate purge is to happen the close routine
363 * typically calls rtexpunge which clears the RTF_UP flag
364 * on the entry so that the code below reclaims the storage.
366 if (rt->rt_refcnt == 0 && rnh->rnh_close)
367 rnh->rnh_close((struct radix_node *)rt, rnh);
370 * If we are no longer "up" (and ref == 0)
371 * then we can free the resources associated
374 if ((rt->rt_flags & RTF_UP) == 0) {
375 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
378 * the rtentry must have been removed from the routing table
379 * so it is represented in rttrash.. remove that now.
383 if (rt->rt_refcnt < 0) {
384 printf("rtfree: %p not freed (neg refs)\n", rt);
389 * release references on items we hold them on..
390 * e.g other routes and ifaddrs.
395 * The key is separatly alloc'd so free it (see rt_setgate()).
396 * This also frees the gateway, as they are always malloc'd
402 * and the rtentry itself of course
405 uma_zfree(rtzone, rt);
414 * Force a routing table entry to the specified
415 * destination to go through the given gateway.
416 * Normally called as a result of a routing redirect
417 * message from the network layer.
420 rtredirect(struct sockaddr *dst,
421 struct sockaddr *gateway,
422 struct sockaddr *netmask,
424 struct sockaddr *src)
426 rtredirect_fib(dst, gateway, netmask, flags, src, 0);
430 rtredirect_fib(struct sockaddr *dst,
431 struct sockaddr *gateway,
432 struct sockaddr *netmask,
434 struct sockaddr *src,
437 INIT_VNET_NET(curvnet);
438 struct rtentry *rt, *rt0 = NULL;
441 struct rt_addrinfo info;
443 struct radix_node_head *rnh =
444 V_rt_tables[fibnum][dst->sa_family];
446 /* verify the gateway is directly reachable */
447 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
451 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
453 * If the redirect isn't from our current router for this dst,
454 * it's either old or wrong. If it redirects us to ourselves,
455 * we have a routing loop, perhaps as a result of an interface
456 * going down recently.
458 if (!(flags & RTF_DONE) && rt &&
459 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
461 else if (ifa_ifwithaddr(gateway))
462 error = EHOSTUNREACH;
466 * Create a new entry if we just got back a wildcard entry
467 * or the the lookup failed. This is necessary for hosts
468 * which use routing redirects generated by smart gateways
469 * to dynamically build the routing tables.
471 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
474 * Don't listen to the redirect if it's
475 * for a route to an interface.
477 if (rt->rt_flags & RTF_GATEWAY) {
478 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
480 * Changing from route to net => route to host.
481 * Create new route, rather than smashing route to net.
487 flags |= RTF_GATEWAY | RTF_DYNAMIC;
488 bzero((caddr_t)&info, sizeof(info));
489 info.rti_info[RTAX_DST] = dst;
490 info.rti_info[RTAX_GATEWAY] = gateway;
491 info.rti_info[RTAX_NETMASK] = netmask;
493 info.rti_flags = flags;
495 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */
496 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
500 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
501 flags = rt->rt_flags;
506 stat = &V_rtstat.rts_dynamic;
508 struct rtentry *gwrt;
511 * Smash the current notion of the gateway to
512 * this destination. Should check about netmask!!!
514 rt->rt_flags |= RTF_MODIFIED;
515 flags |= RTF_MODIFIED;
516 stat = &V_rtstat.rts_newgateway;
518 * add the key and gateway (in one malloc'd chunk).
521 RADIX_NODE_HEAD_LOCK(rnh);
523 rt_setgate(rt, rt_key(rt), gateway);
524 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
525 RADIX_NODE_HEAD_UNLOCK(rnh);
526 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
530 error = EHOSTUNREACH;
536 V_rtstat.rts_badredirect++;
537 else if (stat != NULL)
539 bzero((caddr_t)&info, sizeof(info));
540 info.rti_info[RTAX_DST] = dst;
541 info.rti_info[RTAX_GATEWAY] = gateway;
542 info.rti_info[RTAX_NETMASK] = netmask;
543 info.rti_info[RTAX_AUTHOR] = src;
544 rt_missmsg(RTM_REDIRECT, &info, flags, error);
548 rtioctl(u_long req, caddr_t data)
550 return (rtioctl_fib(req, data, 0));
554 * Routing table ioctl interface.
557 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
561 * If more ioctl commands are added here, make sure the proper
562 * super-user checks are being performed because it is possible for
563 * prison-root to make it this far if raw sockets have been enabled
567 /* Multicast goop, grrr... */
568 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
575 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
577 return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
581 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
584 register struct ifaddr *ifa;
587 if ((flags & RTF_GATEWAY) == 0) {
589 * If we are adding a route to an interface,
590 * and the interface is a pt to pt link
591 * we should search for the destination
592 * as our clue to the interface. Otherwise
593 * we can use the local address.
596 if (flags & RTF_HOST)
597 ifa = ifa_ifwithdstaddr(dst);
599 ifa = ifa_ifwithaddr(gateway);
602 * If we are adding a route to a remote net
603 * or host, the gateway may still be on the
604 * other end of a pt to pt link.
606 ifa = ifa_ifwithdstaddr(gateway);
609 ifa = ifa_ifwithnet(gateway);
611 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
615 * dismiss a gateway that is reachable only
616 * through the default router
618 switch (gateway->sa_family) {
620 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
624 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
634 if ((ifa = rt->rt_ifa) == NULL)
637 if (ifa->ifa_addr->sa_family != dst->sa_family) {
638 struct ifaddr *oifa = ifa;
639 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
647 * Do appropriate manipulations of a routing tree given
648 * all the bits of info needed
652 struct sockaddr *dst,
653 struct sockaddr *gateway,
654 struct sockaddr *netmask,
656 struct rtentry **ret_nrt)
658 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
662 rtrequest_fib(int req,
663 struct sockaddr *dst,
664 struct sockaddr *gateway,
665 struct sockaddr *netmask,
667 struct rtentry **ret_nrt,
670 struct rt_addrinfo info;
672 if (dst->sa_len == 0)
675 bzero((caddr_t)&info, sizeof(info));
676 info.rti_flags = flags;
677 info.rti_info[RTAX_DST] = dst;
678 info.rti_info[RTAX_GATEWAY] = gateway;
679 info.rti_info[RTAX_NETMASK] = netmask;
680 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
684 * These (questionable) definitions of apparent local variables apply
685 * to the next two functions. XXXXXX!!!
687 #define dst info->rti_info[RTAX_DST]
688 #define gateway info->rti_info[RTAX_GATEWAY]
689 #define netmask info->rti_info[RTAX_NETMASK]
690 #define ifaaddr info->rti_info[RTAX_IFA]
691 #define ifpaddr info->rti_info[RTAX_IFP]
692 #define flags info->rti_flags
695 rt_getifa(struct rt_addrinfo *info)
697 return (rt_getifa_fib(info, 0));
701 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
707 * ifp may be specified by sockaddr_dl
708 * when protocol address is ambiguous.
710 if (info->rti_ifp == NULL && ifpaddr != NULL &&
711 ifpaddr->sa_family == AF_LINK &&
712 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
713 info->rti_ifp = ifa->ifa_ifp;
714 if (info->rti_ifa == NULL && ifaaddr != NULL)
715 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
716 if (info->rti_ifa == NULL) {
719 sa = ifaaddr != NULL ? ifaaddr :
720 (gateway != NULL ? gateway : dst);
721 if (sa != NULL && info->rti_ifp != NULL)
722 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
723 else if (dst != NULL && gateway != NULL)
724 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
727 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
730 if ((ifa = info->rti_ifa) != NULL) {
731 if (info->rti_ifp == NULL)
732 info->rti_ifp = ifa->ifa_ifp;
739 * Expunges references to a route that's about to be reclaimed.
740 * The route must be locked.
743 rtexpunge(struct rtentry *rt)
745 INIT_VNET_NET(curvnet);
746 struct radix_node *rn;
747 struct radix_node_head *rnh;
752 * Find the correct routing tree to use for this Address Family
754 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
757 return (EAFNOSUPPORT);
758 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
761 * We cannot assume anything about the reference count
762 * because protocols call us in many situations; often
763 * before unwinding references to the table entry.
765 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
768 * Remove the item from the tree; it should be there,
769 * but when callers invoke us blindly it may not (sigh).
771 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
776 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
777 ("unexpected flags 0x%x", rn->rn_flags));
778 KASSERT(rt == RNTORT(rn),
779 ("lookup mismatch, rt %p rn %p", rt, rn));
781 rt->rt_flags &= ~RTF_UP;
784 * Give the protocol a chance to keep things in sync.
786 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
787 struct rt_addrinfo info;
789 bzero((caddr_t)&info, sizeof(info));
790 info.rti_flags = rt->rt_flags;
791 info.rti_info[RTAX_DST] = rt_key(rt);
792 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
793 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
794 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
798 * one more rtentry floating around that is not
799 * linked to the routing table.
807 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
810 INIT_VNET_NET(curvnet);
811 int error = 0, needlock = 0;
812 register struct rtentry *rt;
813 register struct radix_node *rn;
814 register struct radix_node_head *rnh;
816 struct sockaddr *ndst;
817 #define senderr(x) { error = x ; goto bad; }
819 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
820 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
823 * Find the correct routing tree to use for this Address Family
825 rnh = V_rt_tables[fibnum][dst->sa_family];
827 return (EAFNOSUPPORT);
828 needlock = ((flags & RTF_RNH_LOCKED) == 0);
829 flags &= ~RTF_RNH_LOCKED;
831 RADIX_NODE_HEAD_LOCK(rnh);
833 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
835 * If we are adding a host route then we don't want to put
836 * a netmask in the tree, nor do we want to clone it.
838 if (flags & RTF_HOST)
845 * if we got multipath routes, we require users to specify
846 * a matching RTAX_GATEWAY.
848 if (rn_mpath_capable(rnh)) {
849 struct rtentry *rto = NULL;
851 rn = rnh->rnh_matchaddr(dst, rnh);
854 rto = rt = RNTORT(rn);
855 rt = rt_mpath_matchgate(rt, gateway);
859 * this is the first entry in the chain
862 rn = rn_mpath_next((struct radix_node *)rt);
864 * there is another entry, now it's active
869 rto->rt_flags |= RTF_UP;
871 } else if (rt->rt_flags & RTF_GATEWAY) {
873 * For gateway routes, we need to
874 * make sure that we we are deleting
875 * the correct gateway.
876 * rt_mpath_matchgate() does not
877 * check the case when there is only
878 * one route in the chain.
881 (rt->rt_gateway->sa_len != gateway->sa_len ||
882 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
886 * use the normal delete code to remove
892 * if the entry is 2nd and on up
894 if (!rt_mpath_deldup(rto, rt))
895 panic ("rtrequest1: rt_mpath_deldup");
898 rt->rt_flags &= ~RTF_UP;
899 goto deldone; /* done with the RTM_DELETE command */
905 * Remove the item from the tree and return it.
906 * Complain if it is not there and do no more processing.
908 rn = rnh->rnh_deladdr(dst, netmask, rnh);
911 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
912 panic ("rtrequest delete");
916 rt->rt_flags &= ~RTF_UP;
919 * give the protocol a chance to keep things in sync.
921 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
922 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
928 * One more rtentry floating around that is not
929 * linked to the routing table. rttrash will be decremented
930 * when RTFREE(rt) is eventually called.
935 * If the caller wants it, then it can have it,
936 * but it's up to it to free the rtentry as we won't be
947 * resolve was only used for route cloning
952 if ((flags & RTF_GATEWAY) && !gateway)
954 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
955 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
958 if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum)))
961 rt = uma_zalloc(rtzone, M_NOWAIT | M_ZERO);
965 rt->rt_flags = RTF_UP | flags;
966 rt->rt_fibnum = fibnum;
968 * Add the gateway. Possibly re-malloc-ing the storage for it
972 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
974 uma_zfree(rtzone, rt);
979 * point to the (possibly newly malloc'd) dest address.
981 ndst = (struct sockaddr *)rt_key(rt);
984 * make sure it contains the value we want (masked if needed).
987 rt_maskedcopy(dst, ndst, netmask);
989 bcopy(dst, ndst, dst->sa_len);
992 * Note that we now have a reference to the ifa.
993 * This moved from below so that rnh->rnh_addaddr() can
994 * examine the ifa and ifa->ifa_ifp if it so desires.
998 rt->rt_ifp = ifa->ifa_ifp;
1001 /* do not permit exactly the same dst/mask/gw pair */
1002 if (rn_mpath_capable(rnh) &&
1003 rt_mpath_conflict(rnh, rt, netmask)) {
1005 IFAFREE(rt->rt_ifa);
1008 RT_LOCK_DESTROY(rt);
1009 uma_zfree(rtzone, rt);
1014 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1015 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1017 * If it still failed to go into the tree,
1018 * then un-make it (this should be a function)
1022 IFAFREE(rt->rt_ifa);
1024 RT_LOCK_DESTROY(rt);
1025 uma_zfree(rtzone, rt);
1030 * If this protocol has something to add to this then
1031 * allow it to do that as well.
1033 if (ifa->ifa_rtrequest)
1034 ifa->ifa_rtrequest(req, rt, info);
1037 * actually return a resultant rtentry and
1038 * give the caller a single reference.
1051 RADIX_NODE_HEAD_UNLOCK(rnh);
1064 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1066 INIT_VNET_NET(curvnet);
1067 /* XXX dst may be overwritten, can we move this to below */
1068 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1070 struct radix_node_head *rnh =
1071 V_rt_tables[rt->rt_fibnum][dst->sa_family];
1075 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1078 * Prepare to store the gateway in rt->rt_gateway.
1079 * Both dst and gateway are stored one after the other in the same
1080 * malloc'd chunk. If we have room, we can reuse the old buffer,
1081 * rt_gateway already points to the right place.
1082 * Otherwise, malloc a new block and update the 'dst' address.
1084 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1087 R_Malloc(new, caddr_t, dlen + glen);
1091 * XXX note, we copy from *dst and not *rt_key(rt) because
1092 * rt_setgate() can be called to initialize a newly
1093 * allocated route entry, in which case rt_key(rt) == NULL
1094 * (and also rt->rt_gateway == NULL).
1095 * Free()/free() handle a NULL argument just fine.
1097 bcopy(dst, new, dlen);
1098 Free(rt_key(rt)); /* free old block, if any */
1099 rt_key(rt) = (struct sockaddr *)new;
1100 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1104 * Copy the new gateway value into the memory chunk.
1106 bcopy(gate, rt->rt_gateway, glen);
1112 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1114 register u_char *cp1 = (u_char *)src;
1115 register u_char *cp2 = (u_char *)dst;
1116 register u_char *cp3 = (u_char *)netmask;
1117 u_char *cplim = cp2 + *cp3;
1118 u_char *cplim2 = cp2 + *cp1;
1120 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1125 *cp2++ = *cp1++ & *cp3++;
1127 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1131 * Set up a routing table entry, normally
1134 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1136 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1138 INIT_VNET_NET(curvnet);
1139 struct sockaddr *dst;
1140 struct sockaddr *netmask;
1141 struct rtentry *rt = NULL;
1142 struct rt_addrinfo info;
1144 int startfib, endfib;
1145 char tempbuf[_SOCKADDR_TMPSIZE];
1148 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1150 if (flags & RTF_HOST) {
1151 dst = ifa->ifa_dstaddr;
1154 dst = ifa->ifa_addr;
1155 netmask = ifa->ifa_netmask;
1157 if ( dst->sa_family != AF_INET)
1160 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1161 startfib = endfib = curthread->td_proc->p_fibnum;
1164 endfib = rt_numfibs - 1;
1167 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1171 if (dst->sa_len == 0)
1175 * If it's a delete, check that if it exists,
1176 * it's on the correct interface or we might scrub
1177 * a route to another ifa which would
1178 * be confusing at best and possibly worse.
1180 if (cmd == RTM_DELETE) {
1182 * It's a delete, so it should already exist..
1183 * If it's a net, mask off the host bits
1184 * (Assuming we have a mask)
1185 * XXX this is kinda inet specific..
1187 if (netmask != NULL) {
1188 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1189 dst = (struct sockaddr *)tempbuf;
1193 * Now go through all the requested tables (fibs) and do the
1194 * requested action. Realistically, this will either be fib 0
1195 * for protocols that don't do multiple tables or all the
1196 * tables for those that do. XXX For this version only AF_INET.
1197 * When that changes code should be refactored to protocol
1198 * independent parts and protocol dependent parts.
1200 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1201 if (cmd == RTM_DELETE) {
1202 struct radix_node_head *rnh;
1203 struct radix_node *rn;
1205 * Look up an rtentry that is in the routing tree and
1206 * contains the correct info.
1208 if ((rnh = V_rt_tables[fibnum][dst->sa_family]) == NULL)
1209 /* this table doesn't exist but others might */
1211 RADIX_NODE_HEAD_LOCK(rnh);
1213 if (rn_mpath_capable(rnh)) {
1215 rn = rnh->rnh_matchaddr(dst, rnh);
1221 * for interface route the
1222 * rt->rt_gateway is sockaddr_intf
1223 * for cloning ARP entries, so
1224 * rt_mpath_matchgate must use the
1227 rt = rt_mpath_matchgate(rt,
1235 rn = rnh->rnh_lookup(dst, netmask, rnh);
1236 error = (rn == NULL ||
1237 (rn->rn_flags & RNF_ROOT) ||
1238 RNTORT(rn)->rt_ifa != ifa ||
1239 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1240 RADIX_NODE_HEAD_UNLOCK(rnh);
1242 /* this is only an error if bad on ALL tables */
1247 * Do the actual request
1249 bzero((caddr_t)&info, sizeof(info));
1251 info.rti_flags = flags | ifa->ifa_flags;
1252 info.rti_info[RTAX_DST] = dst;
1254 * doing this for compatibility reasons
1257 info.rti_info[RTAX_GATEWAY] =
1258 (struct sockaddr *)&null_sdl;
1260 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1261 info.rti_info[RTAX_NETMASK] = netmask;
1262 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1263 if (error == 0 && rt != NULL) {
1265 * notify any listening routing agents of the change
1270 * in case address alias finds the first address
1271 * e.g. ifconfig bge0 192.103.54.246/24
1272 * e.g. ifconfig bge0 192.103.54.247/24
1273 * the address set in the route is 192.103.54.246
1274 * so we need to replace it with 192.103.54.247
1276 if (memcmp(rt->rt_ifa->ifa_addr,
1277 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1278 IFAFREE(rt->rt_ifa);
1280 rt->rt_ifp = ifa->ifa_ifp;
1285 * doing this for compatibility reasons
1287 if (cmd == RTM_ADD) {
1288 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1289 rt->rt_ifp->if_type;
1290 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1291 rt->rt_ifp->if_index;
1293 rt_newaddrmsg(cmd, ifa, error, rt);
1294 if (cmd == RTM_DELETE) {
1296 * If we are deleting, and we found an entry,
1297 * then it's been removed from the tree..
1298 * now throw it away.
1302 if (cmd == RTM_ADD) {
1304 * We just wanted to add it..
1305 * we don't actually need a reference.
1316 if (cmd == RTM_DELETE) {
1320 /* we only give an error if it wasn't in any table */
1321 error = ((flags & RTF_HOST) ?
1322 EHOSTUNREACH : ENETUNREACH);
1326 /* return an error if any of them failed */
1333 /* special one for inet internal use. may not use. */
1335 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1337 return (rtinit1(ifa, cmd, flags, -1));
1341 * Set up a routing table entry, normally
1345 rtinit(struct ifaddr *ifa, int cmd, int flags)
1347 struct sockaddr *dst;
1350 if (flags & RTF_HOST) {
1351 dst = ifa->ifa_dstaddr;
1353 dst = ifa->ifa_addr;
1356 if (dst->sa_family == AF_INET)
1358 return (rtinit1(ifa, cmd, flags, fib));
1361 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1362 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);