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"
41 #include "opt_mrouting.h"
42 #include "opt_mpath.h"
44 #include <sys/param.h>
45 #include <sys/systm.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 #if defined(INET) || defined(INET6)
91 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
96 /* This is read-only.. */
97 u_int rt_numfibs = RT_NUMFIBS;
98 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RDTUN, &rt_numfibs, 0, "");
101 * By default add routes to all fibs for new interfaces.
102 * Once this is set to 0 then only allocate routes on interface
103 * changes for the FIB of the caller when adding a new set of addresses
104 * to an interface. XXX this is a shotgun aproach to a problem that needs
105 * a more fine grained solution.. that will come.
106 * XXX also has the problems getting the FIB from curthread which will not
107 * always work given the fib can be overridden and prefixes can be added
108 * from the network stack context.
110 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 1;
111 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
112 &VNET_NAME(rt_add_addr_allfibs), 0, "");
114 VNET_DEFINE(struct rtstat, rtstat);
115 #define V_rtstat VNET(rtstat)
117 VNET_DEFINE(struct radix_node_head *, rt_tables);
118 #define V_rt_tables VNET(rt_tables)
120 VNET_DEFINE(int, rttrash); /* routes not in table but not freed */
121 #define V_rttrash VNET(rttrash)
125 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
126 * The operation can be done safely (in this code) because a
127 * 'struct rtentry' starts with two 'struct radix_node''s, the first
128 * one representing leaf nodes in the routing tree, which is
129 * what the code in radix.c passes us as a 'struct radix_node'.
131 * But because there are a lot of assumptions in this conversion,
132 * do not cast explicitly, but always use the macro below.
134 #define RNTORT(p) ((struct rtentry *)(p))
136 static VNET_DEFINE(uma_zone_t, rtzone); /* Routing table UMA zone. */
137 #define V_rtzone VNET(rtzone)
139 static int rtrequest1_fib_change(struct radix_node_head *, struct rt_addrinfo *,
140 struct rtentry **, u_int);
141 static void rt_setmetrics(const struct rt_addrinfo *, struct rtentry *);
142 static int rt_ifdelroute(const struct rtentry *rt, void *arg);
143 static struct rtentry *rt_unlinkrte(struct radix_node_head *rnh,
144 struct rt_addrinfo *info, int *perror);
145 static void rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info);
147 static struct radix_node *rt_mpath_unlink(struct radix_node_head *rnh,
148 struct rt_addrinfo *info, struct rtentry *rto, int *perror);
150 static int rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info,
159 static int if_updatemtu_cb(struct radix_node *, void *);
162 * handler for net.my_fibnum
165 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
170 fibnum = curthread->td_proc->p_fibnum;
171 error = sysctl_handle_int(oidp, &fibnum, 0, req);
175 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
176 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
178 static __inline struct radix_node_head **
179 rt_tables_get_rnh_ptr(int table, int fam)
181 struct radix_node_head **rnh;
183 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
185 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
188 /* rnh is [fib=0][af=0]. */
189 rnh = (struct radix_node_head **)V_rt_tables;
190 /* Get the offset to the requested table and fam. */
191 rnh += table * (AF_MAX+1) + fam;
196 struct radix_node_head *
197 rt_tables_get_rnh(int table, int fam)
200 return (*rt_tables_get_rnh_ptr(table, fam));
204 * route initialization must occur before ip6_init2(), which happenas at
211 /* whack the tunable ints into line. */
212 if (rt_numfibs > RT_MAXFIBS)
213 rt_numfibs = RT_MAXFIBS;
217 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
220 rtentry_zinit(void *mem, int size, int how)
222 struct rtentry *rt = mem;
224 rt->rt_pksent = counter_u64_alloc(how);
225 if (rt->rt_pksent == NULL)
234 rtentry_zfini(void *mem, int size)
236 struct rtentry *rt = mem;
239 counter_u64_free(rt->rt_pksent);
243 rtentry_ctor(void *mem, int size, void *arg, int how)
245 struct rtentry *rt = mem;
247 bzero(rt, offsetof(struct rtentry, rt_endzero));
248 counter_u64_zero(rt->rt_pksent);
255 rtentry_dtor(void *mem, int size, void *arg)
257 struct rtentry *rt = mem;
263 vnet_route_init(const void *unused __unused)
266 struct radix_node_head **rnh;
270 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
271 sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO);
273 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
274 rtentry_ctor, rtentry_dtor,
275 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0);
276 for (dom = domains; dom; dom = dom->dom_next) {
277 if (dom->dom_rtattach == NULL)
280 for (table = 0; table < rt_numfibs; table++) {
281 fam = dom->dom_family;
282 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
285 rnh = rt_tables_get_rnh_ptr(table, fam);
287 panic("%s: rnh NULL", __func__);
288 dom->dom_rtattach((void **)rnh, 0);
292 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
297 vnet_route_uninit(const void *unused __unused)
302 struct radix_node_head **rnh;
304 for (dom = domains; dom; dom = dom->dom_next) {
305 if (dom->dom_rtdetach == NULL)
308 for (table = 0; table < rt_numfibs; table++) {
309 fam = dom->dom_family;
311 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
314 rnh = rt_tables_get_rnh_ptr(table, fam);
316 panic("%s: rnh NULL", __func__);
317 dom->dom_rtdetach((void **)rnh, 0);
321 free(V_rt_tables, M_RTABLE);
322 uma_zdestroy(V_rtzone);
324 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
325 vnet_route_uninit, 0);
328 #ifndef _SYS_SYSPROTO_H_
334 sys_setfib(struct thread *td, struct setfib_args *uap)
336 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
338 td->td_proc->p_fibnum = uap->fibnum;
343 * Packet routing routines.
346 rtalloc(struct route *ro)
349 rtalloc_ign_fib(ro, 0UL, RT_DEFAULT_FIB);
353 rtalloc_fib(struct route *ro, u_int fibnum)
355 rtalloc_ign_fib(ro, 0UL, fibnum);
359 rtalloc_ign(struct route *ro, u_long ignore)
363 if ((rt = ro->ro_rt) != NULL) {
364 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
369 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, RT_DEFAULT_FIB);
371 RT_UNLOCK(ro->ro_rt);
375 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
379 if ((rt = ro->ro_rt) != NULL) {
380 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
385 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
387 RT_UNLOCK(ro->ro_rt);
391 * Look up the route that matches the address given
392 * Or, at least try.. Create a cloned route if needed.
394 * The returned route, if any, is locked.
397 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
400 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB));
404 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
407 struct radix_node_head *rnh;
408 struct radix_node *rn;
409 struct rtentry *newrt;
410 struct rt_addrinfo info;
411 int err = 0, msgtype = RTM_MISS;
414 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
415 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
421 * Look up the address in the table for that Address Family
423 needlock = !(ignflags & RTF_RNH_LOCKED);
425 RADIX_NODE_HEAD_RLOCK(rnh);
428 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
430 rn = rnh->rnh_matchaddr(dst, rnh);
431 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
436 RADIX_NODE_HEAD_RUNLOCK(rnh);
440 RADIX_NODE_HEAD_RUNLOCK(rnh);
443 * Either we hit the root or couldn't find any match,
444 * Which basically means
445 * "caint get there frm here"
448 V_rtstat.rts_unreach++;
452 * If required, report the failure to the supervising
454 * For a delete, this is not an error. (report == 0)
456 bzero(&info, sizeof(info));
457 info.rti_info[RTAX_DST] = dst;
458 rt_missmsg_fib(msgtype, &info, 0, err, fibnum);
462 RT_LOCK_ASSERT(newrt);
467 * Remove a reference count from an rtentry.
468 * If the count gets low enough, take it out of the routing table
471 rtfree(struct rtentry *rt)
473 struct radix_node_head *rnh;
475 KASSERT(rt != NULL,("%s: NULL rt", __func__));
476 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
477 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
482 * The callers should use RTFREE_LOCKED() or RTFREE(), so
483 * we should come here exactly with the last reference.
486 if (rt->rt_refcnt > 0) {
487 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
492 * On last reference give the "close method" a chance
493 * to cleanup private state. This also permits (for
494 * IPv4 and IPv6) a chance to decide if the routing table
495 * entry should be purged immediately or at a later time.
496 * When an immediate purge is to happen the close routine
497 * typically calls rtexpunge which clears the RTF_UP flag
498 * on the entry so that the code below reclaims the storage.
500 if (rt->rt_refcnt == 0 && rnh->rnh_close)
501 rnh->rnh_close((struct radix_node *)rt, rnh);
504 * If we are no longer "up" (and ref == 0)
505 * then we can free the resources associated
508 if ((rt->rt_flags & RTF_UP) == 0) {
509 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
512 * the rtentry must have been removed from the routing table
513 * so it is represented in rttrash.. remove that now.
517 if (rt->rt_refcnt < 0) {
518 printf("rtfree: %p not freed (neg refs)\n", rt);
523 * release references on items we hold them on..
524 * e.g other routes and ifaddrs.
527 ifa_free(rt->rt_ifa);
529 * The key is separatly alloc'd so free it (see rt_setgate()).
530 * This also frees the gateway, as they are always malloc'd
536 * and the rtentry itself of course
538 uma_zfree(V_rtzone, rt);
547 * Force a routing table entry to the specified
548 * destination to go through the given gateway.
549 * Normally called as a result of a routing redirect
550 * message from the network layer.
553 rtredirect(struct sockaddr *dst,
554 struct sockaddr *gateway,
555 struct sockaddr *netmask,
557 struct sockaddr *src)
560 rtredirect_fib(dst, gateway, netmask, flags, src, RT_DEFAULT_FIB);
564 rtredirect_fib(struct sockaddr *dst,
565 struct sockaddr *gateway,
566 struct sockaddr *netmask,
568 struct sockaddr *src,
574 struct rt_addrinfo info;
576 struct radix_node_head *rnh;
579 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
581 error = EAFNOSUPPORT;
585 /* verify the gateway is directly reachable */
586 if ((ifa = ifa_ifwithnet(gateway, 0, fibnum)) == NULL) {
590 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
592 * If the redirect isn't from our current router for this dst,
593 * it's either old or wrong. If it redirects us to ourselves,
594 * we have a routing loop, perhaps as a result of an interface
595 * going down recently.
597 if (!(flags & RTF_DONE) && rt) {
598 if (!sa_equal(src, rt->rt_gateway)) {
602 if (rt->rt_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK) {
607 if ((flags & RTF_GATEWAY) && ifa_ifwithaddr_check(gateway)) {
608 error = EHOSTUNREACH;
612 * Create a new entry if we just got back a wildcard entry
613 * or the lookup failed. This is necessary for hosts
614 * which use routing redirects generated by smart gateways
615 * to dynamically build the routing tables.
617 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
620 * Don't listen to the redirect if it's
621 * for a route to an interface.
623 if (rt->rt_flags & RTF_GATEWAY) {
624 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
626 * Changing from route to net => route to host.
627 * Create new route, rather than smashing route to net.
633 flags |= RTF_DYNAMIC;
634 bzero((caddr_t)&info, sizeof(info));
635 info.rti_info[RTAX_DST] = dst;
636 info.rti_info[RTAX_GATEWAY] = gateway;
637 info.rti_info[RTAX_NETMASK] = netmask;
639 info.rti_flags = flags;
640 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
643 flags = rt->rt_flags;
646 stat = &V_rtstat.rts_dynamic;
650 * Smash the current notion of the gateway to
651 * this destination. Should check about netmask!!!
653 if ((flags & RTF_GATEWAY) == 0)
654 rt->rt_flags &= ~RTF_GATEWAY;
655 rt->rt_flags |= RTF_MODIFIED;
656 flags |= RTF_MODIFIED;
657 stat = &V_rtstat.rts_newgateway;
659 * add the key and gateway (in one malloc'd chunk).
662 RADIX_NODE_HEAD_LOCK(rnh);
664 rt_setgate(rt, rt_key(rt), gateway);
665 RADIX_NODE_HEAD_UNLOCK(rnh);
668 error = EHOSTUNREACH;
674 V_rtstat.rts_badredirect++;
675 else if (stat != NULL)
677 bzero((caddr_t)&info, sizeof(info));
678 info.rti_info[RTAX_DST] = dst;
679 info.rti_info[RTAX_GATEWAY] = gateway;
680 info.rti_info[RTAX_NETMASK] = netmask;
681 info.rti_info[RTAX_AUTHOR] = src;
682 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
688 rtioctl(u_long req, caddr_t data)
691 return (rtioctl_fib(req, data, RT_DEFAULT_FIB));
695 * Routing table ioctl interface.
698 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
702 * If more ioctl commands are added here, make sure the proper
703 * super-user checks are being performed because it is possible for
704 * prison-root to make it this far if raw sockets have been enabled
708 /* Multicast goop, grrr... */
709 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
716 ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway,
722 if ((flags & RTF_GATEWAY) == 0) {
724 * If we are adding a route to an interface,
725 * and the interface is a pt to pt link
726 * we should search for the destination
727 * as our clue to the interface. Otherwise
728 * we can use the local address.
731 if (flags & RTF_HOST)
732 ifa = ifa_ifwithdstaddr(dst, fibnum);
734 ifa = ifa_ifwithaddr(gateway);
737 * If we are adding a route to a remote net
738 * or host, the gateway may still be on the
739 * other end of a pt to pt link.
741 ifa = ifa_ifwithdstaddr(gateway, fibnum);
744 ifa = ifa_ifwithnet(gateway, 0, fibnum);
746 struct rtentry *rt = rtalloc1_fib(gateway, 0, 0, fibnum);
750 * dismiss a gateway that is reachable only
751 * through the default router
753 switch (gateway->sa_family) {
755 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
759 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
765 if (!not_found && rt->rt_ifa != NULL) {
771 if (not_found || ifa == NULL)
774 if (ifa->ifa_addr->sa_family != dst->sa_family) {
775 struct ifaddr *oifa = ifa;
776 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
786 * Do appropriate manipulations of a routing tree given
787 * all the bits of info needed
791 struct sockaddr *dst,
792 struct sockaddr *gateway,
793 struct sockaddr *netmask,
795 struct rtentry **ret_nrt)
798 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt,
803 rtrequest_fib(int req,
804 struct sockaddr *dst,
805 struct sockaddr *gateway,
806 struct sockaddr *netmask,
808 struct rtentry **ret_nrt,
811 struct rt_addrinfo info;
813 if (dst->sa_len == 0)
816 bzero((caddr_t)&info, sizeof(info));
817 info.rti_flags = flags;
818 info.rti_info[RTAX_DST] = dst;
819 info.rti_info[RTAX_GATEWAY] = gateway;
820 info.rti_info[RTAX_NETMASK] = netmask;
821 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
826 * Copy most of @rt data into @info.
828 * If @flags contains NHR_COPY, copies dst,netmask and gw to the
829 * pointers specified by @info structure. Assume such pointers
830 * are zeroed sockaddr-like structures with sa_len field initialized
831 * to reflect size of the provided buffer. if no NHR_COPY is specified,
832 * point dst,netmask and gw @info fields to appropriate @rt values.
834 * if @flags contains NHR_REF, do refcouting on rt_ifp.
836 * Returns 0 on success.
839 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags)
841 struct rt_metrics *rmx;
842 struct sockaddr *src, *dst;
845 if (flags & NHR_COPY) {
846 /* Copy destination if dst is non-zero */
848 dst = info->rti_info[RTAX_DST];
849 sa_len = src->sa_len;
851 if (src->sa_len > dst->sa_len)
853 memcpy(dst, src, src->sa_len);
854 info->rti_addrs |= RTA_DST;
857 /* Copy mask if set && dst is non-zero */
859 dst = info->rti_info[RTAX_NETMASK];
860 if (src != NULL && dst != NULL) {
863 * Radix stores different value in sa_len,
864 * assume rt_mask() to have the same length
867 if (sa_len > dst->sa_len)
869 memcpy(dst, src, src->sa_len);
870 info->rti_addrs |= RTA_NETMASK;
873 /* Copy gateway is set && dst is non-zero */
874 src = rt->rt_gateway;
875 dst = info->rti_info[RTAX_GATEWAY];
876 if ((rt->rt_flags & RTF_GATEWAY) && src != NULL && dst != NULL){
877 if (src->sa_len > dst->sa_len)
879 memcpy(dst, src, src->sa_len);
880 info->rti_addrs |= RTA_GATEWAY;
883 info->rti_info[RTAX_DST] = rt_key(rt);
884 info->rti_addrs |= RTA_DST;
885 if (rt_mask(rt) != NULL) {
886 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
887 info->rti_addrs |= RTA_NETMASK;
889 if (rt->rt_flags & RTF_GATEWAY) {
890 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
891 info->rti_addrs |= RTA_GATEWAY;
897 info->rti_mflags |= RTV_MTU;
898 rmx->rmx_mtu = rt->rt_mtu;
901 info->rti_flags = rt->rt_flags;
902 info->rti_ifp = rt->rt_ifp;
903 info->rti_ifa = rt->rt_ifa;
905 if (flags & NHR_REF) {
906 /* Do 'traditional' refcouting */
907 if_ref(info->rti_ifp);
914 * Lookups up route entry for @dst in RIB database for fib @fibnum.
915 * Exports entry data to @info using rt_exportinfo().
917 * if @flags contains NHR_REF, refcouting is performed on rt_ifp.
918 * All references can be released later by calling rib_free_info()
920 * Returns 0 on success.
921 * Returns ENOENT for lookup failure, ENOMEM for export failure.
924 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags,
925 uint32_t flowid, struct rt_addrinfo *info)
927 struct radix_node_head *rh;
928 struct radix_node *rn;
932 KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum"));
933 rh = rt_tables_get_rnh(fibnum, dst->sa_family);
937 RADIX_NODE_HEAD_RLOCK(rh);
938 rn = rh->rnh_matchaddr(__DECONST(void *, dst), rh);
939 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
941 /* Ensure route & ifp is UP */
942 if (RT_LINK_IS_UP(rt->rt_ifp)) {
943 flags = (flags & NHR_REF) | NHR_COPY;
944 error = rt_exportinfo(rt, info, flags);
945 RADIX_NODE_HEAD_RUNLOCK(rh);
950 RADIX_NODE_HEAD_RUNLOCK(rh);
956 * Releases all references acquired by rib_lookup_info() when
957 * called with NHR_REF flags.
960 rib_free_info(struct rt_addrinfo *info)
963 if_rele(info->rti_ifp);
967 * Iterates over all existing fibs in system calling
968 * @setwa_f function prior to traversing each fib.
969 * Calls @wa_f function for each element in current fib.
970 * If af is not AF_UNSPEC, iterates over fibs in particular
974 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f,
977 struct radix_node_head *rnh;
981 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
982 /* Do we want some specific family? */
983 if (af != AF_UNSPEC) {
984 rnh = rt_tables_get_rnh(fibnum, af);
988 setwa_f(rnh, fibnum, af, arg);
990 RADIX_NODE_HEAD_LOCK(rnh);
991 rnh->rnh_walktree(rnh, (walktree_f_t *)wa_f, arg);
992 RADIX_NODE_HEAD_UNLOCK(rnh);
996 for (i = 1; i <= AF_MAX; i++) {
997 rnh = rt_tables_get_rnh(fibnum, i);
1000 if (setwa_f != NULL)
1001 setwa_f(rnh, fibnum, i, arg);
1003 RADIX_NODE_HEAD_LOCK(rnh);
1004 rnh->rnh_walktree(rnh, (walktree_f_t *)wa_f, arg);
1005 RADIX_NODE_HEAD_UNLOCK(rnh);
1012 struct rt_addrinfo info;
1013 struct radix_node_head *rnh;
1014 struct rtentry *head;
1018 * Conditionally unlinks @rn from radix tree based
1019 * on info data passed in @arg.
1022 rt_checkdelroute(struct radix_node *rn, void *arg)
1024 struct rt_delinfo *di;
1025 struct rt_addrinfo *info;
1029 di = (struct rt_delinfo *)arg;
1030 rt = (struct rtentry *)rn;
1034 info->rti_info[RTAX_DST] = rt_key(rt);
1035 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1036 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1038 rt = rt_unlinkrte(di->rnh, info, &error);
1040 /* Either not allowed or not matched. Skip entry */
1044 /* Entry was unlinked. Add to the list and return */
1045 rt->rt_chain = di->head;
1052 * Iterates over all existing fibs in system.
1053 * Deletes each element for which @filter_f function returned
1055 * If @af is not AF_UNSPEC, iterates over fibs in particular
1059 rt_foreach_fib_walk_del(int af, rt_filter_f_t *filter_f, void *arg)
1061 struct radix_node_head *rnh;
1062 struct rt_delinfo di;
1067 bzero(&di, sizeof(di));
1068 di.info.rti_filter = filter_f;
1069 di.info.rti_filterdata = arg;
1071 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1072 /* Do we want some specific family? */
1073 if (af != AF_UNSPEC) {
1081 for (i = start; i <= end; i++) {
1082 rnh = rt_tables_get_rnh(fibnum, i);
1087 RADIX_NODE_HEAD_LOCK(rnh);
1088 rnh->rnh_walktree(rnh, rt_checkdelroute, &di);
1089 RADIX_NODE_HEAD_UNLOCK(rnh);
1091 if (di.head == NULL)
1094 /* We might have something to reclaim */
1095 while (di.head != NULL) {
1097 di.head = rt->rt_chain;
1098 rt->rt_chain = NULL;
1100 /* TODO std rt -> rt_addrinfo export */
1101 di.info.rti_info[RTAX_DST] = rt_key(rt);
1102 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1104 rt_notifydelete(rt, &di.info);
1113 * Delete Routes for a Network Interface
1115 * Called for each routing entry via the rnh->rnh_walktree() call above
1116 * to delete all route entries referencing a detaching network interface.
1119 * rt pointer to rtentry
1120 * arg argument passed to rnh->rnh_walktree() - detaching interface
1124 * errno failed - reason indicated
1127 rt_ifdelroute(const struct rtentry *rt, void *arg)
1129 struct ifnet *ifp = arg;
1131 if (rt->rt_ifp != ifp)
1135 * Protect (sorta) against walktree recursion problems
1136 * with cloned routes
1138 if ((rt->rt_flags & RTF_UP) == 0)
1145 * Delete all remaining routes using this interface
1146 * Unfortuneatly the only way to do this is to slog through
1147 * the entire routing table looking for routes which point
1148 * to this interface...oh well...
1151 rt_flushifroutes(struct ifnet *ifp)
1154 rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
1158 * Conditionally unlinks rtentry matching data inside @info from @rnh.
1159 * Returns unlinked, locked and referenced @rtentry on success,
1160 * Returns NULL and sets @perror to:
1161 * ESRCH - if prefix was not found,
1162 * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
1163 * ENOENT - if supplied filter function returned 0 (not matched).
1165 static struct rtentry *
1166 rt_unlinkrte(struct radix_node_head *rnh, struct rt_addrinfo *info, int *perror)
1168 struct sockaddr *dst, *netmask;
1170 struct radix_node *rn;
1172 dst = info->rti_info[RTAX_DST];
1173 netmask = info->rti_info[RTAX_NETMASK];
1175 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, rnh);
1181 if ((info->rti_flags & RTF_PINNED) == 0) {
1182 /* Check if target route can be deleted */
1183 if (rt->rt_flags & RTF_PINNED) {
1184 *perror = EADDRINUSE;
1189 if (info->rti_filter != NULL) {
1190 if (info->rti_filter(rt, info->rti_filterdata) == 0) {
1197 * Filter function requested rte deletion.
1198 * Ease the caller work by filling in remaining info
1199 * from that particular entry.
1201 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1205 * Remove the item from the tree and return it.
1206 * Complain if it is not there and do no more processing.
1210 if (rn_mpath_capable(rnh))
1211 rn = rt_mpath_unlink(rnh, info, rt, perror);
1214 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1218 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
1219 panic ("rtrequest delete");
1224 rt->rt_flags &= ~RTF_UP;
1232 rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info)
1237 * give the protocol a chance to keep things in sync.
1240 if (ifa != NULL && ifa->ifa_rtrequest != NULL)
1241 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1244 * One more rtentry floating around that is not
1245 * linked to the routing table. rttrash will be decremented
1246 * when RTFREE(rt) is eventually called.
1253 * These (questionable) definitions of apparent local variables apply
1254 * to the next two functions. XXXXXX!!!
1256 #define dst info->rti_info[RTAX_DST]
1257 #define gateway info->rti_info[RTAX_GATEWAY]
1258 #define netmask info->rti_info[RTAX_NETMASK]
1259 #define ifaaddr info->rti_info[RTAX_IFA]
1260 #define ifpaddr info->rti_info[RTAX_IFP]
1261 #define flags info->rti_flags
1264 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined,
1265 * it will be referenced so the caller must free it.
1268 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
1274 * ifp may be specified by sockaddr_dl
1275 * when protocol address is ambiguous.
1277 if (info->rti_ifp == NULL && ifpaddr != NULL &&
1278 ifpaddr->sa_family == AF_LINK &&
1279 (ifa = ifa_ifwithnet(ifpaddr, 0, fibnum)) != NULL) {
1280 info->rti_ifp = ifa->ifa_ifp;
1283 if (info->rti_ifa == NULL && ifaaddr != NULL)
1284 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
1285 if (info->rti_ifa == NULL) {
1286 struct sockaddr *sa;
1288 sa = ifaaddr != NULL ? ifaaddr :
1289 (gateway != NULL ? gateway : dst);
1290 if (sa != NULL && info->rti_ifp != NULL)
1291 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
1292 else if (dst != NULL && gateway != NULL)
1293 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
1295 else if (sa != NULL)
1296 info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
1299 if ((ifa = info->rti_ifa) != NULL) {
1300 if (info->rti_ifp == NULL)
1301 info->rti_ifp = ifa->ifa_ifp;
1303 error = ENETUNREACH;
1308 if_updatemtu_cb(struct radix_node *rn, void *arg)
1311 struct if_mtuinfo *ifmtu;
1313 rt = (struct rtentry *)rn;
1314 ifmtu = (struct if_mtuinfo *)arg;
1316 if (rt->rt_ifp != ifmtu->ifp)
1319 if (rt->rt_mtu >= ifmtu->mtu) {
1320 /* We have to decrease mtu regardless of flags */
1321 rt->rt_mtu = ifmtu->mtu;
1326 * New MTU is bigger. Check if are allowed to alter it
1328 if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) {
1331 * Skip routes with user-supplied MTU and
1332 * non-interface routes
1337 /* We are safe to update route MTU */
1338 rt->rt_mtu = ifmtu->mtu;
1344 rt_updatemtu(struct ifnet *ifp)
1346 struct if_mtuinfo ifmtu;
1347 struct radix_node_head *rnh;
1353 * Try to update rt_mtu for all routes using this interface
1354 * Unfortunately the only way to do this is to traverse all
1355 * routing tables in all fibs/domains.
1357 for (i = 1; i <= AF_MAX; i++) {
1358 ifmtu.mtu = if_getmtu_family(ifp, i);
1359 for (j = 0; j < rt_numfibs; j++) {
1360 rnh = rt_tables_get_rnh(j, i);
1363 RADIX_NODE_HEAD_LOCK(rnh);
1364 rnh->rnh_walktree(rnh, if_updatemtu_cb, &ifmtu);
1365 RADIX_NODE_HEAD_UNLOCK(rnh);
1372 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
1373 int rt_print(char *buf, int buflen, struct rtentry *rt);
1376 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
1380 switch (s->sa_family) {
1382 paddr = &((struct sockaddr_in *)s)->sin_addr;
1385 paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
1392 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
1395 return (strlen(buf));
1399 rt_print(char *buf, int buflen, struct rtentry *rt)
1401 struct sockaddr *addr, *mask;
1407 i = p_sockaddr(buf, buflen, addr);
1408 if (!(rt->rt_flags & RTF_HOST)) {
1410 i += p_sockaddr(buf + i, buflen - i, mask);
1413 if (rt->rt_flags & RTF_GATEWAY) {
1415 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway);
1424 * Deletes key for single-path routes, unlinks rtentry with
1425 * gateway specified in @info from multi-path routes.
1427 * Returnes unlinked entry. In case of failure, returns NULL
1428 * and sets @perror to ESRCH.
1430 static struct radix_node *
1431 rt_mpath_unlink(struct radix_node_head *rnh, struct rt_addrinfo *info,
1432 struct rtentry *rto, int *perror)
1435 * if we got multipath routes, we require users to specify
1436 * a matching RTAX_GATEWAY.
1438 struct rtentry *rt; // *rto = NULL;
1439 struct radix_node *rn;
1440 struct sockaddr *gw;
1442 gw = info->rti_info[RTAX_GATEWAY];
1443 rt = rt_mpath_matchgate(rto, gw);
1450 * this is the first entry in the chain
1453 rn = rn_mpath_next((struct radix_node *)rt);
1455 * there is another entry, now it's active
1460 rto->rt_flags |= RTF_UP;
1462 } else if (rt->rt_flags & RTF_GATEWAY) {
1464 * For gateway routes, we need to
1465 * make sure that we we are deleting
1466 * the correct gateway.
1467 * rt_mpath_matchgate() does not
1468 * check the case when there is only
1469 * one route in the chain.
1472 (rt->rt_gateway->sa_len != gw->sa_len ||
1473 memcmp(rt->rt_gateway, gw, gw->sa_len))) {
1480 * use the normal delete code to remove
1483 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1489 * if the entry is 2nd and on up
1491 if (rt_mpath_deldup(rto, rt) == 0)
1492 panic ("rtrequest1: rt_mpath_deldup");
1494 rn = (struct radix_node *)rt;
1500 static struct rtentry *
1501 rt_flowtable_check_route(struct radix_node_head *rnh, struct rt_addrinfo *info)
1503 #if defined(INET6) || defined(INET)
1504 struct radix_node *rn;
1506 struct rtentry *rt0;
1509 /* "flow-table" only supports IPv6 and IPv4 at the moment. */
1510 switch (dst->sa_family) {
1517 #if defined(INET6) || defined(INET)
1518 rn = rnh->rnh_matchaddr(dst, rnh);
1519 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
1520 struct sockaddr *mask;
1525 * compare mask to see if the new route is
1526 * more specific than the existing one
1533 * A host route is already present, so
1534 * leave the flow-table entries as is.
1536 if (rt0->rt_flags & RTF_HOST) {
1539 } else if (!(flags & RTF_HOST) && netmask) {
1540 mask = rt_mask(rt0);
1543 n = (u_char *)netmask;
1550 if (len == 0 || (*n < *m)) {
1556 #endif/* INET6 || INET */
1564 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
1568 struct rtentry *rt, *rt_old;
1570 struct rtentry *rt0;
1572 struct radix_node *rn;
1573 struct radix_node_head *rnh;
1575 struct sockaddr *ndst;
1576 struct sockaddr_storage mdst;
1578 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
1579 KASSERT((flags & RTF_RNH_LOCKED) == 0, ("rtrequest1_fib: locked"));
1580 switch (dst->sa_family) {
1583 /* We support multiple FIBs. */
1586 fibnum = RT_DEFAULT_FIB;
1591 * Find the correct routing tree to use for this Address Family
1593 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1595 return (EAFNOSUPPORT);
1598 * If we are adding a host route then we don't want to put
1599 * a netmask in the tree, nor do we want to clone it.
1601 if (flags & RTF_HOST)
1607 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
1608 dst = (struct sockaddr *)&mdst;
1611 RADIX_NODE_HEAD_LOCK(rnh);
1612 rt = rt_unlinkrte(rnh, info, &error);
1613 RADIX_NODE_HEAD_UNLOCK(rnh);
1617 rt_notifydelete(rt, info);
1620 * If the caller wants it, then it can have it,
1621 * but it's up to it to free the rtentry as we won't be
1632 * resolve was only used for route cloning
1637 if ((flags & RTF_GATEWAY) && !gateway)
1639 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1640 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1643 if (info->rti_ifa == NULL) {
1644 error = rt_getifa_fib(info, fibnum);
1648 ifa_ref(info->rti_ifa);
1649 ifa = info->rti_ifa;
1650 rt = uma_zalloc(V_rtzone, M_NOWAIT);
1655 rt->rt_flags = RTF_UP | flags;
1656 rt->rt_fibnum = fibnum;
1658 * Add the gateway. Possibly re-malloc-ing the storage for it.
1660 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1662 uma_zfree(V_rtzone, rt);
1667 * point to the (possibly newly malloc'd) dest address.
1669 ndst = (struct sockaddr *)rt_key(rt);
1672 * make sure it contains the value we want (masked if needed).
1675 rt_maskedcopy(dst, ndst, netmask);
1677 bcopy(dst, ndst, dst->sa_len);
1680 * We use the ifa reference returned by rt_getifa_fib().
1681 * This moved from below so that rnh->rnh_addaddr() can
1682 * examine the ifa and ifa->ifa_ifp if it so desires.
1685 rt->rt_ifp = ifa->ifa_ifp;
1688 rt_setmetrics(info, rt);
1690 RADIX_NODE_HEAD_LOCK(rnh);
1693 /* do not permit exactly the same dst/mask/gw pair */
1694 if (rn_mpath_capable(rnh) &&
1695 rt_mpath_conflict(rnh, rt, netmask)) {
1696 RADIX_NODE_HEAD_UNLOCK(rnh);
1698 ifa_free(rt->rt_ifa);
1700 uma_zfree(V_rtzone, rt);
1706 rt0 = rt_flowtable_check_route(rnh, info);
1707 #endif /* FLOWTABLE */
1709 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1710 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1713 if (rn == NULL && (info->rti_flags & RTF_PINNED) != 0) {
1716 * Force removal and re-try addition
1717 * TODO: better multipath&pinned support
1719 struct sockaddr *info_dst = info->rti_info[RTAX_DST];
1720 info->rti_info[RTAX_DST] = ndst;
1721 /* Do not delete existing PINNED(interface) routes */
1722 info->rti_flags &= ~RTF_PINNED;
1723 rt_old = rt_unlinkrte(rnh, info, &error);
1724 info->rti_flags |= RTF_PINNED;
1725 info->rti_info[RTAX_DST] = info_dst;
1727 rn = rnh->rnh_addaddr(ndst, netmask, rnh,
1730 RADIX_NODE_HEAD_UNLOCK(rnh);
1736 * If it still failed to go into the tree,
1737 * then un-make it (this should be a function)
1740 ifa_free(rt->rt_ifa);
1742 uma_zfree(V_rtzone, rt);
1750 else if (rt0 != NULL) {
1751 flowtable_route_flush(dst->sa_family, rt0);
1756 if (rt_old != NULL) {
1757 rt_notifydelete(rt_old, info);
1762 * If this protocol has something to add to this then
1763 * allow it to do that as well.
1765 if (ifa->ifa_rtrequest)
1766 ifa->ifa_rtrequest(req, rt, info);
1769 * actually return a resultant rtentry and
1770 * give the caller a single reference.
1779 RADIX_NODE_HEAD_LOCK(rnh);
1780 error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum);
1781 RADIX_NODE_HEAD_UNLOCK(rnh);
1798 rtrequest1_fib_change(struct radix_node_head *rnh, struct rt_addrinfo *info,
1799 struct rtentry **ret_nrt, u_int fibnum)
1801 struct rtentry *rt = NULL;
1805 struct if_mtuinfo ifmtu;
1807 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1808 info->rti_info[RTAX_NETMASK], rnh);
1815 * If we got multipath routes,
1816 * we require users to specify a matching RTAX_GATEWAY.
1818 if (rn_mpath_capable(rnh)) {
1819 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
1827 rt_setmetrics(info, rt);
1830 * New gateway could require new ifaddr, ifp;
1831 * flags may also be different; ifp may be specified
1832 * by ll sockaddr when protocol address is ambiguous
1834 if (((rt->rt_flags & RTF_GATEWAY) &&
1835 info->rti_info[RTAX_GATEWAY] != NULL) ||
1836 info->rti_info[RTAX_IFP] != NULL ||
1837 (info->rti_info[RTAX_IFA] != NULL &&
1838 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) {
1840 error = rt_getifa_fib(info, fibnum);
1841 if (info->rti_ifa != NULL)
1848 /* Check if outgoing interface has changed */
1849 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa &&
1850 rt->rt_ifa != NULL && rt->rt_ifa->ifa_rtrequest != NULL) {
1851 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1852 ifa_free(rt->rt_ifa);
1854 /* Update gateway address */
1855 if (info->rti_info[RTAX_GATEWAY] != NULL) {
1856 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]);
1860 rt->rt_flags &= ~RTF_GATEWAY;
1861 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags);
1864 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) {
1865 ifa_ref(info->rti_ifa);
1866 rt->rt_ifa = info->rti_ifa;
1867 rt->rt_ifp = info->rti_ifp;
1869 /* Allow some flags to be toggled on change. */
1870 rt->rt_flags &= ~RTF_FMASK;
1871 rt->rt_flags |= info->rti_flags & RTF_FMASK;
1873 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL)
1874 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
1876 /* Alter route MTU if necessary */
1877 if (rt->rt_ifp != NULL) {
1878 family = info->rti_info[RTAX_DST]->sa_family;
1879 mtu = if_getmtu_family(rt->rt_ifp, family);
1880 /* Set default MTU */
1881 if (rt->rt_mtu == 0)
1883 if (rt->rt_mtu != mtu) {
1884 /* Check if we really need to update */
1885 ifmtu.ifp = rt->rt_ifp;
1887 if_updatemtu_cb(rt->rt_nodes, &ifmtu);
1898 ifa_free(info->rti_ifa);
1903 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt)
1906 if (info->rti_mflags & RTV_MTU) {
1907 if (info->rti_rmx->rmx_mtu != 0) {
1910 * MTU was explicitly provided by user.
1913 rt->rt_flags |= RTF_FIXEDMTU;
1917 * User explicitly sets MTU to 0.
1918 * Assume rollback to default.
1920 rt->rt_flags &= ~RTF_FIXEDMTU;
1922 rt->rt_mtu = info->rti_rmx->rmx_mtu;
1924 if (info->rti_mflags & RTV_WEIGHT)
1925 rt->rt_weight = info->rti_rmx->rmx_weight;
1926 /* Kernel -> userland timebase conversion. */
1927 if (info->rti_mflags & RTV_EXPIRE)
1928 rt->rt_expire = info->rti_rmx->rmx_expire ?
1929 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
1933 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1935 /* XXX dst may be overwritten, can we move this to below */
1936 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1939 * Prepare to store the gateway in rt->rt_gateway.
1940 * Both dst and gateway are stored one after the other in the same
1941 * malloc'd chunk. If we have room, we can reuse the old buffer,
1942 * rt_gateway already points to the right place.
1943 * Otherwise, malloc a new block and update the 'dst' address.
1945 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1948 R_Malloc(new, caddr_t, dlen + glen);
1952 * XXX note, we copy from *dst and not *rt_key(rt) because
1953 * rt_setgate() can be called to initialize a newly
1954 * allocated route entry, in which case rt_key(rt) == NULL
1955 * (and also rt->rt_gateway == NULL).
1956 * Free()/free() handle a NULL argument just fine.
1958 bcopy(dst, new, dlen);
1959 R_Free(rt_key(rt)); /* free old block, if any */
1960 rt_key(rt) = (struct sockaddr *)new;
1961 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1965 * Copy the new gateway value into the memory chunk.
1967 bcopy(gate, rt->rt_gateway, glen);
1973 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1975 u_char *cp1 = (u_char *)src;
1976 u_char *cp2 = (u_char *)dst;
1977 u_char *cp3 = (u_char *)netmask;
1978 u_char *cplim = cp2 + *cp3;
1979 u_char *cplim2 = cp2 + *cp1;
1981 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1986 *cp2++ = *cp1++ & *cp3++;
1988 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1992 * Set up a routing table entry, normally
1995 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1997 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1999 struct sockaddr *dst;
2000 struct sockaddr *netmask;
2001 struct rtentry *rt = NULL;
2002 struct rt_addrinfo info;
2004 int startfib, endfib;
2005 char tempbuf[_SOCKADDR_TMPSIZE];
2008 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
2009 struct radix_node_head *rnh;
2011 if (flags & RTF_HOST) {
2012 dst = ifa->ifa_dstaddr;
2015 dst = ifa->ifa_addr;
2016 netmask = ifa->ifa_netmask;
2018 if (dst->sa_len == 0)
2020 switch (dst->sa_family) {
2023 /* We support multiple FIBs. */
2026 fibnum = RT_DEFAULT_FIB;
2029 if (fibnum == RT_ALL_FIBS) {
2030 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD)
2031 startfib = endfib = ifa->ifa_ifp->if_fib;
2034 endfib = rt_numfibs - 1;
2037 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
2043 * If it's a delete, check that if it exists,
2044 * it's on the correct interface or we might scrub
2045 * a route to another ifa which would
2046 * be confusing at best and possibly worse.
2048 if (cmd == RTM_DELETE) {
2050 * It's a delete, so it should already exist..
2051 * If it's a net, mask off the host bits
2052 * (Assuming we have a mask)
2053 * XXX this is kinda inet specific..
2055 if (netmask != NULL) {
2056 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
2057 dst = (struct sockaddr *)tempbuf;
2061 * Now go through all the requested tables (fibs) and do the
2062 * requested action. Realistically, this will either be fib 0
2063 * for protocols that don't do multiple tables or all the
2064 * tables for those that do.
2066 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
2067 if (cmd == RTM_DELETE) {
2068 struct radix_node *rn;
2070 * Look up an rtentry that is in the routing tree and
2071 * contains the correct info.
2073 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
2075 /* this table doesn't exist but others might */
2077 RADIX_NODE_HEAD_RLOCK(rnh);
2078 rn = rnh->rnh_lookup(dst, netmask, rnh);
2080 if (rn_mpath_capable(rnh)) {
2087 * for interface route the
2088 * rt->rt_gateway is sockaddr_intf
2089 * for cloning ARP entries, so
2090 * rt_mpath_matchgate must use the
2093 rt = rt_mpath_matchgate(rt,
2100 error = (rn == NULL ||
2101 (rn->rn_flags & RNF_ROOT) ||
2102 RNTORT(rn)->rt_ifa != ifa);
2103 RADIX_NODE_HEAD_RUNLOCK(rnh);
2105 /* this is only an error if bad on ALL tables */
2110 * Do the actual request
2112 bzero((caddr_t)&info, sizeof(info));
2114 info.rti_flags = flags |
2115 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
2116 info.rti_info[RTAX_DST] = dst;
2118 * doing this for compatibility reasons
2121 info.rti_info[RTAX_GATEWAY] =
2122 (struct sockaddr *)&null_sdl;
2124 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
2125 info.rti_info[RTAX_NETMASK] = netmask;
2126 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
2128 if (error == 0 && rt != NULL) {
2130 * notify any listening routing agents of the change
2135 * in case address alias finds the first address
2136 * e.g. ifconfig bge0 192.0.2.246/24
2137 * e.g. ifconfig bge0 192.0.2.247/24
2138 * the address set in the route is 192.0.2.246
2139 * so we need to replace it with 192.0.2.247
2141 if (memcmp(rt->rt_ifa->ifa_addr,
2142 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
2143 ifa_free(rt->rt_ifa);
2145 rt->rt_ifp = ifa->ifa_ifp;
2150 * doing this for compatibility reasons
2152 if (cmd == RTM_ADD) {
2153 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
2154 rt->rt_ifp->if_type;
2155 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
2156 rt->rt_ifp->if_index;
2160 rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum);
2163 if (cmd == RTM_DELETE) {
2165 * If we are deleting, and we found an entry,
2166 * then it's been removed from the tree..
2167 * now throw it away.
2171 if (cmd == RTM_ADD) {
2173 * We just wanted to add it..
2174 * we don't actually need a reference.
2185 if (cmd == RTM_DELETE) {
2189 /* we only give an error if it wasn't in any table */
2190 error = ((flags & RTF_HOST) ?
2191 EHOSTUNREACH : ENETUNREACH);
2195 /* return an error if any of them failed */
2203 * Set up a routing table entry, normally
2207 rtinit(struct ifaddr *ifa, int cmd, int flags)
2209 struct sockaddr *dst;
2210 int fib = RT_DEFAULT_FIB;
2212 if (flags & RTF_HOST) {
2213 dst = ifa->ifa_dstaddr;
2215 dst = ifa->ifa_addr;
2218 switch (dst->sa_family) {
2221 /* We do support multiple FIBs. */
2225 return (rtinit1(ifa, cmd, flags, fib));
2229 * Announce interface address arrival/withdraw
2230 * Returns 0 on success.
2233 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
2236 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2237 ("unexpected cmd %d", cmd));
2239 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2240 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2242 #if defined(INET) || defined(INET6)
2245 * notify the SCTP stack
2246 * this will only get called when an address is added/deleted
2247 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
2249 sctp_addr_change(ifa, cmd);
2252 return (rtsock_addrmsg(cmd, ifa, fibnum));
2256 * Announce route addition/removal.
2257 * Users of this function MUST validate input data BEFORE calling.
2258 * However we have to be able to handle invalid data:
2259 * if some userland app sends us "invalid" route message (invalid mask,
2260 * no dst, wrong address families, etc...) we need to pass it back
2261 * to app (and any other rtsock consumers) with rtm_errno field set to
2263 * Returns 0 on success.
2266 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
2270 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2271 ("unexpected cmd %d", cmd));
2273 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2274 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2276 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
2278 return (rtsock_routemsg(cmd, ifp, error, rt, fibnum));
2282 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
2285 rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS);
2289 * This is called to generate messages from the routing socket
2290 * indicating a network interface has had addresses associated with it.
2293 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
2297 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2298 ("unexpected cmd %u", cmd));
2299 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2300 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2302 if (cmd == RTM_ADD) {
2303 rt_addrmsg(cmd, ifa, fibnum);
2305 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
2308 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
2309 rt_addrmsg(cmd, ifa, fibnum);