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 *);
109 static int vnet_route_iattach(const void *);
111 #ifndef VIMAGE_GLOBALS
112 static const vnet_modinfo_t vnet_rtable_modinfo = {
113 .vmi_id = VNET_MOD_RTABLE,
114 .vmi_name = "rtable",
115 .vmi_iattach = vnet_route_iattach
117 #endif /* !VIMAGE_GLOBALS */
119 /* compare two sockaddr structures */
120 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
123 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
124 * The operation can be done safely (in this code) because a
125 * 'struct rtentry' starts with two 'struct radix_node''s, the first
126 * one representing leaf nodes in the routing tree, which is
127 * what the code in radix.c passes us as a 'struct radix_node'.
129 * But because there are a lot of assumptions in this conversion,
130 * do not cast explicitly, but always use the macro below.
132 #define RNTORT(p) ((struct rtentry *)(p))
134 #ifdef VIMAGE_GLOBALS
135 static uma_zone_t rtzone; /* Routing table UMA zone. */
139 /* default fib for tunnels to use */
140 u_int tunnel_fib = 0;
141 SYSCTL_INT(_net, OID_AUTO, tunnelfib, CTLFLAG_RD, &tunnel_fib, 0, "");
145 * handler for net.my_fibnum
148 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
153 fibnum = curthread->td_proc->p_fibnum;
154 error = sysctl_handle_int(oidp, &fibnum, 0, req);
158 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
159 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
165 /* whack the tunable ints into line. */
166 if (rt_numfibs > RT_MAXFIBS)
167 rt_numfibs = RT_MAXFIBS;
170 rn_init(); /* initialize all zeroes, all ones, mask table */
172 #ifndef VIMAGE_GLOBALS
173 vnet_mod_register(&vnet_rtable_modinfo);
175 vnet_route_iattach(NULL);
179 static int vnet_route_iattach(const void *unused __unused)
181 INIT_VNET_INET(curvnet);
186 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry), NULL, NULL,
187 NULL, NULL, UMA_ALIGN_PTR, 0);
188 for (dom = domains; dom; dom = dom->dom_next) {
189 if (dom->dom_rtattach) {
190 for (table = 0; table < rt_numfibs; table++) {
191 if ( (fam = dom->dom_family) == AF_INET ||
193 /* for now only AF_INET has > 1 table */
195 * rtattach will be also called
196 * from vfs_export.c but the
198 * (only for AF_INET and AF_INET6
199 * which don't need it anyhow)
202 (void **)&V_rt_tables[table][fam],
214 #ifndef _SYS_SYSPROTO_H_
220 setfib(struct thread *td, struct setfib_args *uap)
222 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
224 td->td_proc->p_fibnum = uap->fibnum;
229 * Packet routing routines.
232 rtalloc(struct route *ro)
234 rtalloc_ign_fib(ro, 0UL, 0);
238 rtalloc_fib(struct route *ro, u_int fibnum)
240 rtalloc_ign_fib(ro, 0UL, fibnum);
244 rtalloc_ign(struct route *ro, u_long ignore)
248 if ((rt = ro->ro_rt) != NULL) {
249 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
254 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, 0);
256 RT_UNLOCK(ro->ro_rt);
260 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
264 if ((rt = ro->ro_rt) != NULL) {
265 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
270 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
272 RT_UNLOCK(ro->ro_rt);
276 * Look up the route that matches the address given
277 * Or, at least try.. Create a cloned route if needed.
279 * The returned route, if any, is locked.
282 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
284 return (rtalloc1_fib(dst, report, ignflags, 0));
288 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
291 INIT_VNET_NET(curvnet);
292 struct radix_node_head *rnh;
294 struct radix_node *rn;
295 struct rtentry *newrt;
296 struct rt_addrinfo info;
297 int err = 0, msgtype = RTM_MISS;
300 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
301 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
303 rnh = V_rt_tables[fibnum][dst->sa_family];
306 * Look up the address in the table for that Address Family
309 V_rtstat.rts_unreach++;
312 needlock = !(ignflags & RTF_RNH_LOCKED);
314 RADIX_NODE_HEAD_RLOCK(rnh);
317 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
319 rn = rnh->rnh_matchaddr(dst, rnh);
320 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
321 newrt = rt = RNTORT(rn);
325 RADIX_NODE_HEAD_RUNLOCK(rnh);
329 RADIX_NODE_HEAD_RUNLOCK(rnh);
332 * Either we hit the root or couldn't find any match,
333 * Which basically means
334 * "caint get there frm here"
336 V_rtstat.rts_unreach++;
340 * If required, report the failure to the supervising
342 * For a delete, this is not an error. (report == 0)
344 bzero(&info, sizeof(info));
345 info.rti_info[RTAX_DST] = dst;
346 rt_missmsg(msgtype, &info, 0, err);
350 RT_LOCK_ASSERT(newrt);
355 * Remove a reference count from an rtentry.
356 * If the count gets low enough, take it out of the routing table
359 rtfree(struct rtentry *rt)
361 INIT_VNET_NET(curvnet);
362 struct radix_node_head *rnh;
364 KASSERT(rt != NULL,("%s: NULL rt", __func__));
365 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
366 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
371 * The callers should use RTFREE_LOCKED() or RTFREE(), so
372 * we should come here exactly with the last reference.
375 if (rt->rt_refcnt > 0) {
376 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
381 * On last reference give the "close method" a chance
382 * to cleanup private state. This also permits (for
383 * IPv4 and IPv6) a chance to decide if the routing table
384 * entry should be purged immediately or at a later time.
385 * When an immediate purge is to happen the close routine
386 * typically calls rtexpunge which clears the RTF_UP flag
387 * on the entry so that the code below reclaims the storage.
389 if (rt->rt_refcnt == 0 && rnh->rnh_close)
390 rnh->rnh_close((struct radix_node *)rt, rnh);
393 * If we are no longer "up" (and ref == 0)
394 * then we can free the resources associated
397 if ((rt->rt_flags & RTF_UP) == 0) {
398 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
401 * the rtentry must have been removed from the routing table
402 * so it is represented in rttrash.. remove that now.
406 if (rt->rt_refcnt < 0) {
407 printf("rtfree: %p not freed (neg refs)\n", rt);
412 * release references on items we hold them on..
413 * e.g other routes and ifaddrs.
418 * The key is separatly alloc'd so free it (see rt_setgate()).
419 * This also frees the gateway, as they are always malloc'd
425 * and the rtentry itself of course
428 uma_zfree(V_rtzone, rt);
437 * Force a routing table entry to the specified
438 * destination to go through the given gateway.
439 * Normally called as a result of a routing redirect
440 * message from the network layer.
443 rtredirect(struct sockaddr *dst,
444 struct sockaddr *gateway,
445 struct sockaddr *netmask,
447 struct sockaddr *src)
449 rtredirect_fib(dst, gateway, netmask, flags, src, 0);
453 rtredirect_fib(struct sockaddr *dst,
454 struct sockaddr *gateway,
455 struct sockaddr *netmask,
457 struct sockaddr *src,
460 INIT_VNET_NET(curvnet);
461 struct rtentry *rt, *rt0 = NULL;
464 struct rt_addrinfo info;
466 struct radix_node_head *rnh =
467 V_rt_tables[fibnum][dst->sa_family];
469 /* verify the gateway is directly reachable */
470 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
474 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
476 * If the redirect isn't from our current router for this dst,
477 * it's either old or wrong. If it redirects us to ourselves,
478 * we have a routing loop, perhaps as a result of an interface
479 * going down recently.
481 if (!(flags & RTF_DONE) && rt &&
482 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
484 else if (ifa_ifwithaddr(gateway))
485 error = EHOSTUNREACH;
489 * Create a new entry if we just got back a wildcard entry
490 * or the the lookup failed. This is necessary for hosts
491 * which use routing redirects generated by smart gateways
492 * to dynamically build the routing tables.
494 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
497 * Don't listen to the redirect if it's
498 * for a route to an interface.
500 if (rt->rt_flags & RTF_GATEWAY) {
501 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
503 * Changing from route to net => route to host.
504 * Create new route, rather than smashing route to net.
510 flags |= RTF_GATEWAY | RTF_DYNAMIC;
511 bzero((caddr_t)&info, sizeof(info));
512 info.rti_info[RTAX_DST] = dst;
513 info.rti_info[RTAX_GATEWAY] = gateway;
514 info.rti_info[RTAX_NETMASK] = netmask;
516 info.rti_flags = flags;
518 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */
519 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
523 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
524 flags = rt->rt_flags;
529 stat = &V_rtstat.rts_dynamic;
531 struct rtentry *gwrt;
534 * Smash the current notion of the gateway to
535 * this destination. Should check about netmask!!!
537 rt->rt_flags |= RTF_MODIFIED;
538 flags |= RTF_MODIFIED;
539 stat = &V_rtstat.rts_newgateway;
541 * add the key and gateway (in one malloc'd chunk).
544 RADIX_NODE_HEAD_LOCK(rnh);
546 rt_setgate(rt, rt_key(rt), gateway);
547 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
548 RADIX_NODE_HEAD_UNLOCK(rnh);
549 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
553 error = EHOSTUNREACH;
559 V_rtstat.rts_badredirect++;
560 else if (stat != NULL)
562 bzero((caddr_t)&info, sizeof(info));
563 info.rti_info[RTAX_DST] = dst;
564 info.rti_info[RTAX_GATEWAY] = gateway;
565 info.rti_info[RTAX_NETMASK] = netmask;
566 info.rti_info[RTAX_AUTHOR] = src;
567 rt_missmsg(RTM_REDIRECT, &info, flags, error);
571 rtioctl(u_long req, caddr_t data)
573 return (rtioctl_fib(req, data, 0));
577 * Routing table ioctl interface.
580 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
584 * If more ioctl commands are added here, make sure the proper
585 * super-user checks are being performed because it is possible for
586 * prison-root to make it this far if raw sockets have been enabled
590 /* Multicast goop, grrr... */
591 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
598 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
600 return (ifa_ifwithroute_fib(flags, dst, gateway, 0));
604 ifa_ifwithroute_fib(int flags, struct sockaddr *dst, struct sockaddr *gateway,
607 register struct ifaddr *ifa;
610 if ((flags & RTF_GATEWAY) == 0) {
612 * If we are adding a route to an interface,
613 * and the interface is a pt to pt link
614 * we should search for the destination
615 * as our clue to the interface. Otherwise
616 * we can use the local address.
619 if (flags & RTF_HOST)
620 ifa = ifa_ifwithdstaddr(dst);
622 ifa = ifa_ifwithaddr(gateway);
625 * If we are adding a route to a remote net
626 * or host, the gateway may still be on the
627 * other end of a pt to pt link.
629 ifa = ifa_ifwithdstaddr(gateway);
632 ifa = ifa_ifwithnet(gateway);
634 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
638 * dismiss a gateway that is reachable only
639 * through the default router
641 switch (gateway->sa_family) {
643 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
647 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
657 if ((ifa = rt->rt_ifa) == NULL)
660 if (ifa->ifa_addr->sa_family != dst->sa_family) {
661 struct ifaddr *oifa = ifa;
662 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
670 * Do appropriate manipulations of a routing tree given
671 * all the bits of info needed
675 struct sockaddr *dst,
676 struct sockaddr *gateway,
677 struct sockaddr *netmask,
679 struct rtentry **ret_nrt)
681 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt, 0));
685 rtrequest_fib(int req,
686 struct sockaddr *dst,
687 struct sockaddr *gateway,
688 struct sockaddr *netmask,
690 struct rtentry **ret_nrt,
693 struct rt_addrinfo info;
695 if (dst->sa_len == 0)
698 bzero((caddr_t)&info, sizeof(info));
699 info.rti_flags = flags;
700 info.rti_info[RTAX_DST] = dst;
701 info.rti_info[RTAX_GATEWAY] = gateway;
702 info.rti_info[RTAX_NETMASK] = netmask;
703 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
707 * These (questionable) definitions of apparent local variables apply
708 * to the next two functions. XXXXXX!!!
710 #define dst info->rti_info[RTAX_DST]
711 #define gateway info->rti_info[RTAX_GATEWAY]
712 #define netmask info->rti_info[RTAX_NETMASK]
713 #define ifaaddr info->rti_info[RTAX_IFA]
714 #define ifpaddr info->rti_info[RTAX_IFP]
715 #define flags info->rti_flags
718 rt_getifa(struct rt_addrinfo *info)
720 return (rt_getifa_fib(info, 0));
724 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
730 * ifp may be specified by sockaddr_dl
731 * when protocol address is ambiguous.
733 if (info->rti_ifp == NULL && ifpaddr != NULL &&
734 ifpaddr->sa_family == AF_LINK &&
735 (ifa = ifa_ifwithnet(ifpaddr)) != NULL)
736 info->rti_ifp = ifa->ifa_ifp;
737 if (info->rti_ifa == NULL && ifaaddr != NULL)
738 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
739 if (info->rti_ifa == NULL) {
742 sa = ifaaddr != NULL ? ifaaddr :
743 (gateway != NULL ? gateway : dst);
744 if (sa != NULL && info->rti_ifp != NULL)
745 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
746 else if (dst != NULL && gateway != NULL)
747 info->rti_ifa = ifa_ifwithroute_fib(flags, dst, gateway,
750 info->rti_ifa = ifa_ifwithroute_fib(flags, sa, sa,
753 if ((ifa = info->rti_ifa) != NULL) {
754 if (info->rti_ifp == NULL)
755 info->rti_ifp = ifa->ifa_ifp;
762 * Expunges references to a route that's about to be reclaimed.
763 * The route must be locked.
766 rtexpunge(struct rtentry *rt)
768 INIT_VNET_NET(curvnet);
769 struct radix_node *rn;
770 struct radix_node_head *rnh;
775 * Find the correct routing tree to use for this Address Family
777 rnh = V_rt_tables[rt->rt_fibnum][rt_key(rt)->sa_family];
780 return (EAFNOSUPPORT);
781 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
784 * We cannot assume anything about the reference count
785 * because protocols call us in many situations; often
786 * before unwinding references to the table entry.
788 KASSERT(rt->rt_refcnt <= 1, ("bogus refcnt %ld", rt->rt_refcnt));
791 * Remove the item from the tree; it should be there,
792 * but when callers invoke us blindly it may not (sigh).
794 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
799 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
800 ("unexpected flags 0x%x", rn->rn_flags));
801 KASSERT(rt == RNTORT(rn),
802 ("lookup mismatch, rt %p rn %p", rt, rn));
804 rt->rt_flags &= ~RTF_UP;
807 * Give the protocol a chance to keep things in sync.
809 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
810 struct rt_addrinfo info;
812 bzero((caddr_t)&info, sizeof(info));
813 info.rti_flags = rt->rt_flags;
814 info.rti_info[RTAX_DST] = rt_key(rt);
815 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
816 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
817 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
821 * one more rtentry floating around that is not
822 * linked to the routing table.
831 rn_mpath_update(int req, struct rt_addrinfo *info,
832 struct radix_node_head *rnh, struct rtentry **ret_nrt)
835 * if we got multipath routes, we require users to specify
836 * a matching RTAX_GATEWAY.
838 struct rtentry *rt, *rto = NULL;
839 register struct radix_node *rn;
842 rn = rnh->rnh_matchaddr(dst, rnh);
845 rto = rt = RNTORT(rn);
846 rt = rt_mpath_matchgate(rt, gateway);
850 * this is the first entry in the chain
853 rn = rn_mpath_next((struct radix_node *)rt);
855 * there is another entry, now it's active
860 rto->rt_flags |= RTF_UP;
862 } else if (rt->rt_flags & RTF_GATEWAY) {
864 * For gateway routes, we need to
865 * make sure that we we are deleting
866 * the correct gateway.
867 * rt_mpath_matchgate() does not
868 * check the case when there is only
869 * one route in the chain.
872 (rt->rt_gateway->sa_len != gateway->sa_len ||
873 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
878 * use the normal delete code to remove
881 if (req != RTM_DELETE)
889 * if the entry is 2nd and on up
891 if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt))
892 panic ("rtrequest1: rt_mpath_deldup");
895 if (req == RTM_DELETE) {
896 rt->rt_flags &= ~RTF_UP;
898 * One more rtentry floating around that is not
899 * linked to the routing table. rttrash will be decremented
900 * when RTFREE(rt) is eventually called.
907 if (req != RTM_DELETE)
908 panic("unrecognized request %d", req);
912 * If the caller wants it, then it can have it,
913 * but it's up to it to free the rtentry as we won't be
927 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
930 INIT_VNET_NET(curvnet);
931 int error = 0, needlock = 0;
932 register struct rtentry *rt;
933 register struct radix_node *rn;
934 register struct radix_node_head *rnh;
936 struct sockaddr *ndst;
937 #define senderr(x) { error = x ; goto bad; }
939 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
940 if (dst->sa_family != AF_INET) /* Only INET supports > 1 fib now */
943 * Find the correct routing tree to use for this Address Family
945 rnh = V_rt_tables[fibnum][dst->sa_family];
947 return (EAFNOSUPPORT);
948 needlock = ((flags & RTF_RNH_LOCKED) == 0);
949 flags &= ~RTF_RNH_LOCKED;
951 RADIX_NODE_HEAD_LOCK(rnh);
953 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
955 * If we are adding a host route then we don't want to put
956 * a netmask in the tree, nor do we want to clone it.
958 if (flags & RTF_HOST)
964 if (rn_mpath_capable(rnh)) {
965 error = rn_mpath_update(req, info, rnh, ret_nrt);
967 * "bad" holds true for the success case
975 * Remove the item from the tree and return it.
976 * Complain if it is not there and do no more processing.
978 rn = rnh->rnh_deladdr(dst, netmask, rnh);
981 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
982 panic ("rtrequest delete");
986 rt->rt_flags &= ~RTF_UP;
989 * give the protocol a chance to keep things in sync.
991 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
992 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
995 * One more rtentry floating around that is not
996 * linked to the routing table. rttrash will be decremented
997 * when RTFREE(rt) is eventually called.
1002 * If the caller wants it, then it can have it,
1003 * but it's up to it to free the rtentry as we won't be
1014 * resolve was only used for route cloning
1019 if ((flags & RTF_GATEWAY) && !gateway)
1021 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1022 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1025 if (info->rti_ifa == NULL && (error = rt_getifa_fib(info, fibnum)))
1027 ifa = info->rti_ifa;
1028 rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
1032 rt->rt_flags = RTF_UP | flags;
1033 rt->rt_fibnum = fibnum;
1035 * Add the gateway. Possibly re-malloc-ing the storage for it
1039 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1040 RT_LOCK_DESTROY(rt);
1041 uma_zfree(V_rtzone, rt);
1046 * point to the (possibly newly malloc'd) dest address.
1048 ndst = (struct sockaddr *)rt_key(rt);
1051 * make sure it contains the value we want (masked if needed).
1054 rt_maskedcopy(dst, ndst, netmask);
1056 bcopy(dst, ndst, dst->sa_len);
1059 * Note that we now have a reference to the ifa.
1060 * This moved from below so that rnh->rnh_addaddr() can
1061 * examine the ifa and ifa->ifa_ifp if it so desires.
1065 rt->rt_ifp = ifa->ifa_ifp;
1066 rt->rt_rmx.rmx_weight = 1;
1069 /* do not permit exactly the same dst/mask/gw pair */
1070 if (rn_mpath_capable(rnh) &&
1071 rt_mpath_conflict(rnh, rt, netmask)) {
1073 IFAFREE(rt->rt_ifa);
1076 RT_LOCK_DESTROY(rt);
1077 uma_zfree(V_rtzone, rt);
1082 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1083 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1085 * If it still failed to go into the tree,
1086 * then un-make it (this should be a function)
1090 IFAFREE(rt->rt_ifa);
1092 RT_LOCK_DESTROY(rt);
1093 uma_zfree(V_rtzone, rt);
1098 * If this protocol has something to add to this then
1099 * allow it to do that as well.
1101 if (ifa->ifa_rtrequest)
1102 ifa->ifa_rtrequest(req, rt, info);
1105 * actually return a resultant rtentry and
1106 * give the caller a single reference.
1119 RADIX_NODE_HEAD_UNLOCK(rnh);
1132 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1134 INIT_VNET_NET(curvnet);
1135 /* XXX dst may be overwritten, can we move this to below */
1136 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1138 struct radix_node_head *rnh =
1139 V_rt_tables[rt->rt_fibnum][dst->sa_family];
1143 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1146 * Prepare to store the gateway in rt->rt_gateway.
1147 * Both dst and gateway are stored one after the other in the same
1148 * malloc'd chunk. If we have room, we can reuse the old buffer,
1149 * rt_gateway already points to the right place.
1150 * Otherwise, malloc a new block and update the 'dst' address.
1152 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1155 R_Malloc(new, caddr_t, dlen + glen);
1159 * XXX note, we copy from *dst and not *rt_key(rt) because
1160 * rt_setgate() can be called to initialize a newly
1161 * allocated route entry, in which case rt_key(rt) == NULL
1162 * (and also rt->rt_gateway == NULL).
1163 * Free()/free() handle a NULL argument just fine.
1165 bcopy(dst, new, dlen);
1166 Free(rt_key(rt)); /* free old block, if any */
1167 rt_key(rt) = (struct sockaddr *)new;
1168 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1172 * Copy the new gateway value into the memory chunk.
1174 bcopy(gate, rt->rt_gateway, glen);
1180 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1182 register u_char *cp1 = (u_char *)src;
1183 register u_char *cp2 = (u_char *)dst;
1184 register u_char *cp3 = (u_char *)netmask;
1185 u_char *cplim = cp2 + *cp3;
1186 u_char *cplim2 = cp2 + *cp1;
1188 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1193 *cp2++ = *cp1++ & *cp3++;
1195 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1199 * Set up a routing table entry, normally
1202 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1204 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1206 INIT_VNET_NET(curvnet);
1207 struct sockaddr *dst;
1208 struct sockaddr *netmask;
1209 struct rtentry *rt = NULL;
1210 struct rt_addrinfo info;
1212 int startfib, endfib;
1213 char tempbuf[_SOCKADDR_TMPSIZE];
1216 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1218 if (flags & RTF_HOST) {
1219 dst = ifa->ifa_dstaddr;
1222 dst = ifa->ifa_addr;
1223 netmask = ifa->ifa_netmask;
1225 if ( dst->sa_family != AF_INET)
1228 if (rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD) {
1229 startfib = endfib = curthread->td_proc->p_fibnum;
1232 endfib = rt_numfibs - 1;
1235 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1239 if (dst->sa_len == 0)
1243 * If it's a delete, check that if it exists,
1244 * it's on the correct interface or we might scrub
1245 * a route to another ifa which would
1246 * be confusing at best and possibly worse.
1248 if (cmd == RTM_DELETE) {
1250 * It's a delete, so it should already exist..
1251 * If it's a net, mask off the host bits
1252 * (Assuming we have a mask)
1253 * XXX this is kinda inet specific..
1255 if (netmask != NULL) {
1256 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1257 dst = (struct sockaddr *)tempbuf;
1261 * Now go through all the requested tables (fibs) and do the
1262 * requested action. Realistically, this will either be fib 0
1263 * for protocols that don't do multiple tables or all the
1264 * tables for those that do. XXX For this version only AF_INET.
1265 * When that changes code should be refactored to protocol
1266 * independent parts and protocol dependent parts.
1268 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1269 if (cmd == RTM_DELETE) {
1270 struct radix_node_head *rnh;
1271 struct radix_node *rn;
1273 * Look up an rtentry that is in the routing tree and
1274 * contains the correct info.
1276 if ((rnh = V_rt_tables[fibnum][dst->sa_family]) == NULL)
1277 /* this table doesn't exist but others might */
1279 RADIX_NODE_HEAD_LOCK(rnh);
1281 if (rn_mpath_capable(rnh)) {
1283 rn = rnh->rnh_matchaddr(dst, rnh);
1289 * for interface route the
1290 * rt->rt_gateway is sockaddr_intf
1291 * for cloning ARP entries, so
1292 * rt_mpath_matchgate must use the
1295 rt = rt_mpath_matchgate(rt,
1303 rn = rnh->rnh_lookup(dst, netmask, rnh);
1304 error = (rn == NULL ||
1305 (rn->rn_flags & RNF_ROOT) ||
1306 RNTORT(rn)->rt_ifa != ifa ||
1307 !sa_equal((struct sockaddr *)rn->rn_key, dst));
1308 RADIX_NODE_HEAD_UNLOCK(rnh);
1310 /* this is only an error if bad on ALL tables */
1315 * Do the actual request
1317 bzero((caddr_t)&info, sizeof(info));
1319 info.rti_flags = flags | ifa->ifa_flags;
1320 info.rti_info[RTAX_DST] = dst;
1322 * doing this for compatibility reasons
1325 info.rti_info[RTAX_GATEWAY] =
1326 (struct sockaddr *)&null_sdl;
1328 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1329 info.rti_info[RTAX_NETMASK] = netmask;
1330 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1331 if (error == 0 && rt != NULL) {
1333 * notify any listening routing agents of the change
1338 * in case address alias finds the first address
1339 * e.g. ifconfig bge0 192.103.54.246/24
1340 * e.g. ifconfig bge0 192.103.54.247/24
1341 * the address set in the route is 192.103.54.246
1342 * so we need to replace it with 192.103.54.247
1344 if (memcmp(rt->rt_ifa->ifa_addr,
1345 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1346 IFAFREE(rt->rt_ifa);
1348 rt->rt_ifp = ifa->ifa_ifp;
1353 * doing this for compatibility reasons
1355 if (cmd == RTM_ADD) {
1356 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1357 rt->rt_ifp->if_type;
1358 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1359 rt->rt_ifp->if_index;
1361 rt_newaddrmsg(cmd, ifa, error, rt);
1362 if (cmd == RTM_DELETE) {
1364 * If we are deleting, and we found an entry,
1365 * then it's been removed from the tree..
1366 * now throw it away.
1370 if (cmd == RTM_ADD) {
1372 * We just wanted to add it..
1373 * we don't actually need a reference.
1384 if (cmd == RTM_DELETE) {
1388 /* we only give an error if it wasn't in any table */
1389 error = ((flags & RTF_HOST) ?
1390 EHOSTUNREACH : ENETUNREACH);
1394 /* return an error if any of them failed */
1401 /* special one for inet internal use. may not use. */
1403 rtinit_fib(struct ifaddr *ifa, int cmd, int flags)
1405 return (rtinit1(ifa, cmd, flags, -1));
1409 * Set up a routing table entry, normally
1413 rtinit(struct ifaddr *ifa, int cmd, int flags)
1415 struct sockaddr *dst;
1418 if (flags & RTF_HOST) {
1419 dst = ifa->ifa_dstaddr;
1421 dst = ifa->ifa_addr;
1424 if (dst->sa_family == AF_INET)
1426 return (rtinit1(ifa, cmd, flags, fib));
1429 /* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1430 SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);