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(struct rtentry *rt, void *arg);
150 static int if_updatemtu_cb(struct radix_node *, void *);
153 * handler for net.my_fibnum
156 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
161 fibnum = curthread->td_proc->p_fibnum;
162 error = sysctl_handle_int(oidp, &fibnum, 0, req);
166 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
167 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
169 static __inline struct radix_node_head **
170 rt_tables_get_rnh_ptr(int table, int fam)
172 struct radix_node_head **rnh;
174 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
176 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
179 /* rnh is [fib=0][af=0]. */
180 rnh = (struct radix_node_head **)V_rt_tables;
181 /* Get the offset to the requested table and fam. */
182 rnh += table * (AF_MAX+1) + fam;
187 struct radix_node_head *
188 rt_tables_get_rnh(int table, int fam)
191 return (*rt_tables_get_rnh_ptr(table, fam));
195 * route initialization must occur before ip6_init2(), which happenas at
202 /* whack the tunable ints into line. */
203 if (rt_numfibs > RT_MAXFIBS)
204 rt_numfibs = RT_MAXFIBS;
208 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
211 rtentry_zinit(void *mem, int size, int how)
213 struct rtentry *rt = mem;
215 rt->rt_pksent = counter_u64_alloc(how);
216 if (rt->rt_pksent == NULL)
225 rtentry_zfini(void *mem, int size)
227 struct rtentry *rt = mem;
230 counter_u64_free(rt->rt_pksent);
234 rtentry_ctor(void *mem, int size, void *arg, int how)
236 struct rtentry *rt = mem;
238 bzero(rt, offsetof(struct rtentry, rt_endzero));
239 counter_u64_zero(rt->rt_pksent);
245 rtentry_dtor(void *mem, int size, void *arg)
247 struct rtentry *rt = mem;
253 vnet_route_init(const void *unused __unused)
256 struct radix_node_head **rnh;
260 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
261 sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO);
263 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
264 rtentry_ctor, rtentry_dtor,
265 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0);
266 for (dom = domains; dom; dom = dom->dom_next) {
267 if (dom->dom_rtattach == NULL)
270 for (table = 0; table < rt_numfibs; table++) {
271 fam = dom->dom_family;
272 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
275 rnh = rt_tables_get_rnh_ptr(table, fam);
277 panic("%s: rnh NULL", __func__);
278 dom->dom_rtattach((void **)rnh, 0);
282 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
287 vnet_route_uninit(const void *unused __unused)
292 struct radix_node_head **rnh;
294 for (dom = domains; dom; dom = dom->dom_next) {
295 if (dom->dom_rtdetach == NULL)
298 for (table = 0; table < rt_numfibs; table++) {
299 fam = dom->dom_family;
301 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
304 rnh = rt_tables_get_rnh_ptr(table, fam);
306 panic("%s: rnh NULL", __func__);
307 dom->dom_rtdetach((void **)rnh, 0);
311 free(V_rt_tables, M_RTABLE);
312 uma_zdestroy(V_rtzone);
314 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
315 vnet_route_uninit, 0);
318 #ifndef _SYS_SYSPROTO_H_
324 sys_setfib(struct thread *td, struct setfib_args *uap)
326 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
328 td->td_proc->p_fibnum = uap->fibnum;
333 * Packet routing routines.
336 rtalloc(struct route *ro)
339 rtalloc_ign_fib(ro, 0UL, RT_DEFAULT_FIB);
343 rtalloc_fib(struct route *ro, u_int fibnum)
345 rtalloc_ign_fib(ro, 0UL, fibnum);
349 rtalloc_ign(struct route *ro, u_long ignore)
353 if ((rt = ro->ro_rt) != NULL) {
354 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
359 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, RT_DEFAULT_FIB);
361 RT_UNLOCK(ro->ro_rt);
365 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
369 if ((rt = ro->ro_rt) != NULL) {
370 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
375 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
377 RT_UNLOCK(ro->ro_rt);
381 * Look up the route that matches the address given
382 * Or, at least try.. Create a cloned route if needed.
384 * The returned route, if any, is locked.
387 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
390 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB));
394 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
397 struct radix_node_head *rnh;
398 struct radix_node *rn;
399 struct rtentry *newrt;
400 struct rt_addrinfo info;
401 int err = 0, msgtype = RTM_MISS;
404 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
405 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
411 * Look up the address in the table for that Address Family
413 needlock = !(ignflags & RTF_RNH_LOCKED);
415 RADIX_NODE_HEAD_RLOCK(rnh);
418 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
420 rn = rnh->rnh_matchaddr(dst, rnh);
421 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
426 RADIX_NODE_HEAD_RUNLOCK(rnh);
430 RADIX_NODE_HEAD_RUNLOCK(rnh);
433 * Either we hit the root or couldn't find any match,
434 * Which basically means
435 * "caint get there frm here"
438 V_rtstat.rts_unreach++;
442 * If required, report the failure to the supervising
444 * For a delete, this is not an error. (report == 0)
446 bzero(&info, sizeof(info));
447 info.rti_info[RTAX_DST] = dst;
448 rt_missmsg_fib(msgtype, &info, 0, err, fibnum);
452 RT_LOCK_ASSERT(newrt);
457 * Remove a reference count from an rtentry.
458 * If the count gets low enough, take it out of the routing table
461 rtfree(struct rtentry *rt)
463 struct radix_node_head *rnh;
465 KASSERT(rt != NULL,("%s: NULL rt", __func__));
466 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
467 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
472 * The callers should use RTFREE_LOCKED() or RTFREE(), so
473 * we should come here exactly with the last reference.
476 if (rt->rt_refcnt > 0) {
477 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
482 * On last reference give the "close method" a chance
483 * to cleanup private state. This also permits (for
484 * IPv4 and IPv6) a chance to decide if the routing table
485 * entry should be purged immediately or at a later time.
486 * When an immediate purge is to happen the close routine
487 * typically calls rtexpunge which clears the RTF_UP flag
488 * on the entry so that the code below reclaims the storage.
490 if (rt->rt_refcnt == 0 && rnh->rnh_close)
491 rnh->rnh_close((struct radix_node *)rt, rnh);
494 * If we are no longer "up" (and ref == 0)
495 * then we can free the resources associated
498 if ((rt->rt_flags & RTF_UP) == 0) {
499 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
502 * the rtentry must have been removed from the routing table
503 * so it is represented in rttrash.. remove that now.
507 if (rt->rt_refcnt < 0) {
508 printf("rtfree: %p not freed (neg refs)\n", rt);
513 * release references on items we hold them on..
514 * e.g other routes and ifaddrs.
517 ifa_free(rt->rt_ifa);
519 * The key is separatly alloc'd so free it (see rt_setgate()).
520 * This also frees the gateway, as they are always malloc'd
526 * and the rtentry itself of course
528 uma_zfree(V_rtzone, rt);
537 * Force a routing table entry to the specified
538 * destination to go through the given gateway.
539 * Normally called as a result of a routing redirect
540 * message from the network layer.
543 rtredirect(struct sockaddr *dst,
544 struct sockaddr *gateway,
545 struct sockaddr *netmask,
547 struct sockaddr *src)
550 rtredirect_fib(dst, gateway, netmask, flags, src, RT_DEFAULT_FIB);
554 rtredirect_fib(struct sockaddr *dst,
555 struct sockaddr *gateway,
556 struct sockaddr *netmask,
558 struct sockaddr *src,
561 struct rtentry *rt, *rt0 = NULL;
564 struct rt_addrinfo info;
566 struct radix_node_head *rnh;
569 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
571 error = EAFNOSUPPORT;
575 /* verify the gateway is directly reachable */
576 if ((ifa = ifa_ifwithnet(gateway, 0, fibnum)) == NULL) {
580 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
582 * If the redirect isn't from our current router for this dst,
583 * it's either old or wrong. If it redirects us to ourselves,
584 * we have a routing loop, perhaps as a result of an interface
585 * going down recently.
587 if (!(flags & RTF_DONE) && rt) {
588 if (!sa_equal(src, rt->rt_gateway)) {
592 if (rt->rt_ifa != ifa && ifa->ifa_addr->sa_family != AF_LINK) {
597 if ((flags & RTF_GATEWAY) && ifa_ifwithaddr_check(gateway)) {
598 error = EHOSTUNREACH;
602 * Create a new entry if we just got back a wildcard entry
603 * or the lookup failed. This is necessary for hosts
604 * which use routing redirects generated by smart gateways
605 * to dynamically build the routing tables.
607 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
610 * Don't listen to the redirect if it's
611 * for a route to an interface.
613 if (rt->rt_flags & RTF_GATEWAY) {
614 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
616 * Changing from route to net => route to host.
617 * Create new route, rather than smashing route to net.
623 flags |= RTF_DYNAMIC;
624 bzero((caddr_t)&info, sizeof(info));
625 info.rti_info[RTAX_DST] = dst;
626 info.rti_info[RTAX_GATEWAY] = gateway;
627 info.rti_info[RTAX_NETMASK] = netmask;
629 info.rti_flags = flags;
631 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */
632 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
636 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
637 flags = rt->rt_flags;
642 stat = &V_rtstat.rts_dynamic;
644 struct rtentry *gwrt;
647 * Smash the current notion of the gateway to
648 * this destination. Should check about netmask!!!
650 if ((flags & RTF_GATEWAY) == 0)
651 rt->rt_flags &= ~RTF_GATEWAY;
652 rt->rt_flags |= RTF_MODIFIED;
653 flags |= RTF_MODIFIED;
654 stat = &V_rtstat.rts_newgateway;
656 * add the key and gateway (in one malloc'd chunk).
659 RADIX_NODE_HEAD_LOCK(rnh);
661 rt_setgate(rt, rt_key(rt), gateway);
662 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
663 RADIX_NODE_HEAD_UNLOCK(rnh);
664 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
669 error = EHOSTUNREACH;
675 V_rtstat.rts_badredirect++;
676 else if (stat != NULL)
678 bzero((caddr_t)&info, sizeof(info));
679 info.rti_info[RTAX_DST] = dst;
680 info.rti_info[RTAX_GATEWAY] = gateway;
681 info.rti_info[RTAX_NETMASK] = netmask;
682 info.rti_info[RTAX_AUTHOR] = src;
683 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
689 rtioctl(u_long req, caddr_t data)
692 return (rtioctl_fib(req, data, RT_DEFAULT_FIB));
696 * Routing table ioctl interface.
699 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
703 * If more ioctl commands are added here, make sure the proper
704 * super-user checks are being performed because it is possible for
705 * prison-root to make it this far if raw sockets have been enabled
709 /* Multicast goop, grrr... */
710 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
717 ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway,
723 if ((flags & RTF_GATEWAY) == 0) {
725 * If we are adding a route to an interface,
726 * and the interface is a pt to pt link
727 * we should search for the destination
728 * as our clue to the interface. Otherwise
729 * we can use the local address.
732 if (flags & RTF_HOST)
733 ifa = ifa_ifwithdstaddr(dst, fibnum);
735 ifa = ifa_ifwithaddr(gateway);
738 * If we are adding a route to a remote net
739 * or host, the gateway may still be on the
740 * other end of a pt to pt link.
742 ifa = ifa_ifwithdstaddr(gateway, fibnum);
745 ifa = ifa_ifwithnet(gateway, 0, fibnum);
747 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
751 * dismiss a gateway that is reachable only
752 * through the default router
754 switch (gateway->sa_family) {
756 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
760 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
766 if (!not_found && rt->rt_ifa != NULL) {
772 if (not_found || ifa == NULL)
775 if (ifa->ifa_addr->sa_family != dst->sa_family) {
776 struct ifaddr *oifa = ifa;
777 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
787 * Do appropriate manipulations of a routing tree given
788 * all the bits of info needed
792 struct sockaddr *dst,
793 struct sockaddr *gateway,
794 struct sockaddr *netmask,
796 struct rtentry **ret_nrt)
799 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt,
804 rtrequest_fib(int req,
805 struct sockaddr *dst,
806 struct sockaddr *gateway,
807 struct sockaddr *netmask,
809 struct rtentry **ret_nrt,
812 struct rt_addrinfo info;
814 if (dst->sa_len == 0)
817 bzero((caddr_t)&info, sizeof(info));
818 info.rti_flags = flags;
819 info.rti_info[RTAX_DST] = dst;
820 info.rti_info[RTAX_GATEWAY] = gateway;
821 info.rti_info[RTAX_NETMASK] = netmask;
822 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
827 * Iterates over all existing fibs in system calling
828 * @setwa_f function prior to traversing each fib.
829 * Calls @wa_f function for each element in current fib.
830 * If af is not AF_UNSPEC, iterates over fibs in particular
834 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f,
837 struct radix_node_head *rnh;
841 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
842 /* Do we want some specific family? */
843 if (af != AF_UNSPEC) {
844 rnh = rt_tables_get_rnh(fibnum, af);
848 setwa_f(rnh, fibnum, af, arg);
850 RADIX_NODE_HEAD_LOCK(rnh);
851 rnh->rnh_walktree(rnh, (walktree_f_t *)wa_f, arg);
852 RADIX_NODE_HEAD_UNLOCK(rnh);
856 for (i = 1; i <= AF_MAX; i++) {
857 rnh = rt_tables_get_rnh(fibnum, i);
861 setwa_f(rnh, fibnum, i, arg);
863 RADIX_NODE_HEAD_LOCK(rnh);
864 rnh->rnh_walktree(rnh, (walktree_f_t *)wa_f, arg);
865 RADIX_NODE_HEAD_UNLOCK(rnh);
871 * Delete Routes for a Network Interface
873 * Called for each routing entry via the rnh->rnh_walktree() call above
874 * to delete all route entries referencing a detaching network interface.
877 * rt pointer to rtentry
878 * arg argument passed to rnh->rnh_walktree() - detaching interface
882 * errno failed - reason indicated
885 rt_ifdelroute(struct rtentry *rt, void *arg)
887 struct ifnet *ifp = arg;
890 if (rt->rt_ifp != ifp)
894 * Protect (sorta) against walktree recursion problems
897 if ((rt->rt_flags & RTF_UP) == 0)
900 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway,
902 rt->rt_flags | RTF_RNH_LOCKED | RTF_PINNED,
903 (struct rtentry **) NULL, rt->rt_fibnum);
905 log(LOG_WARNING, "rt_ifdelroute: error %d\n", err);
911 * Delete all remaining routes using this interface
912 * Unfortuneatly the only way to do this is to slog through
913 * the entire routing table looking for routes which point
914 * to this interface...oh well...
917 rt_flushifroutes(struct ifnet *ifp)
920 rt_foreach_fib_walk(AF_UNSPEC, NULL, rt_ifdelroute, ifp);
924 * These (questionable) definitions of apparent local variables apply
925 * to the next two functions. XXXXXX!!!
927 #define dst info->rti_info[RTAX_DST]
928 #define gateway info->rti_info[RTAX_GATEWAY]
929 #define netmask info->rti_info[RTAX_NETMASK]
930 #define ifaaddr info->rti_info[RTAX_IFA]
931 #define ifpaddr info->rti_info[RTAX_IFP]
932 #define flags info->rti_flags
935 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined,
936 * it will be referenced so the caller must free it.
939 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
945 * ifp may be specified by sockaddr_dl
946 * when protocol address is ambiguous.
948 if (info->rti_ifp == NULL && ifpaddr != NULL &&
949 ifpaddr->sa_family == AF_LINK &&
950 (ifa = ifa_ifwithnet(ifpaddr, 0, fibnum)) != NULL) {
951 info->rti_ifp = ifa->ifa_ifp;
954 if (info->rti_ifa == NULL && ifaaddr != NULL)
955 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
956 if (info->rti_ifa == NULL) {
959 sa = ifaaddr != NULL ? ifaaddr :
960 (gateway != NULL ? gateway : dst);
961 if (sa != NULL && info->rti_ifp != NULL)
962 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
963 else if (dst != NULL && gateway != NULL)
964 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
967 info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
970 if ((ifa = info->rti_ifa) != NULL) {
971 if (info->rti_ifp == NULL)
972 info->rti_ifp = ifa->ifa_ifp;
979 * Expunges references to a route that's about to be reclaimed.
980 * The route must be locked.
983 rt_expunge(struct radix_node_head *rnh, struct rtentry *rt)
985 #if !defined(RADIX_MPATH)
986 struct radix_node *rn;
988 struct rt_addrinfo info;
996 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1000 bzero(&info, sizeof(info));
1001 info.rti_ifp = rt->rt_ifp;
1002 info.rti_flags = RTF_RNH_LOCKED;
1003 info.rti_info[RTAX_DST] = rt_key(rt);
1004 info.rti_info[RTAX_GATEWAY] = rt->rt_ifa->ifa_addr;
1007 error = rtrequest1_fib(RTM_DELETE, &info, &rt0, fib);
1009 if (error == 0 && rt0 != NULL) {
1012 } else if (error != 0) {
1018 * Remove the item from the tree; it should be there,
1019 * but when callers invoke us blindly it may not (sigh).
1021 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
1026 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
1027 ("unexpected flags 0x%x", rn->rn_flags));
1028 KASSERT(rt == RNTORT(rn),
1029 ("lookup mismatch, rt %p rn %p", rt, rn));
1030 #endif /* RADIX_MPATH */
1032 rt->rt_flags &= ~RTF_UP;
1035 * Give the protocol a chance to keep things in sync.
1037 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
1038 struct rt_addrinfo info;
1040 bzero((caddr_t)&info, sizeof(info));
1041 info.rti_flags = rt->rt_flags;
1042 info.rti_info[RTAX_DST] = rt_key(rt);
1043 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1044 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1045 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
1049 * one more rtentry floating around that is not
1050 * linked to the routing table.
1053 #if !defined(RADIX_MPATH)
1060 if_updatemtu_cb(struct radix_node *rn, void *arg)
1063 struct if_mtuinfo *ifmtu;
1065 rt = (struct rtentry *)rn;
1066 ifmtu = (struct if_mtuinfo *)arg;
1068 if (rt->rt_ifp != ifmtu->ifp)
1071 if (rt->rt_mtu >= ifmtu->mtu) {
1072 /* We have to decrease mtu regardless of flags */
1073 rt->rt_mtu = ifmtu->mtu;
1078 * New MTU is bigger. Check if are allowed to alter it
1080 if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) {
1083 * Skip routes with user-supplied MTU and
1084 * non-interface routes
1089 /* We are safe to update route MTU */
1090 rt->rt_mtu = ifmtu->mtu;
1096 rt_updatemtu(struct ifnet *ifp)
1098 struct if_mtuinfo ifmtu;
1099 struct radix_node_head *rnh;
1105 * Try to update rt_mtu for all routes using this interface
1106 * Unfortunately the only way to do this is to traverse all
1107 * routing tables in all fibs/domains.
1109 for (i = 1; i <= AF_MAX; i++) {
1110 ifmtu.mtu = if_getmtu_family(ifp, i);
1111 for (j = 0; j < rt_numfibs; j++) {
1112 rnh = rt_tables_get_rnh(j, i);
1115 RADIX_NODE_HEAD_LOCK(rnh);
1116 rnh->rnh_walktree(rnh, if_updatemtu_cb, &ifmtu);
1117 RADIX_NODE_HEAD_UNLOCK(rnh);
1124 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
1125 int rt_print(char *buf, int buflen, struct rtentry *rt);
1128 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
1132 switch (s->sa_family) {
1134 paddr = &((struct sockaddr_in *)s)->sin_addr;
1137 paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
1144 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
1147 return (strlen(buf));
1151 rt_print(char *buf, int buflen, struct rtentry *rt)
1153 struct sockaddr *addr, *mask;
1159 i = p_sockaddr(buf, buflen, addr);
1160 if (!(rt->rt_flags & RTF_HOST)) {
1162 i += p_sockaddr(buf + i, buflen - i, mask);
1165 if (rt->rt_flags & RTF_GATEWAY) {
1167 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway);
1176 rn_mpath_update(int req, struct rt_addrinfo *info,
1177 struct radix_node_head *rnh, struct rtentry **ret_nrt)
1180 * if we got multipath routes, we require users to specify
1181 * a matching RTAX_GATEWAY.
1183 struct rtentry *rt, *rto = NULL;
1184 struct radix_node *rn;
1187 rn = rnh->rnh_lookup(dst, netmask, rnh);
1190 rto = rt = RNTORT(rn);
1192 rt = rt_mpath_matchgate(rt, gateway);
1196 * this is the first entry in the chain
1199 rn = rn_mpath_next((struct radix_node *)rt);
1201 * there is another entry, now it's active
1206 rto->rt_flags |= RTF_UP;
1208 } else if (rt->rt_flags & RTF_GATEWAY) {
1210 * For gateway routes, we need to
1211 * make sure that we we are deleting
1212 * the correct gateway.
1213 * rt_mpath_matchgate() does not
1214 * check the case when there is only
1215 * one route in the chain.
1218 (rt->rt_gateway->sa_len != gateway->sa_len ||
1219 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
1223 * remove from tree before returning it
1226 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1227 KASSERT(rt == RNTORT(rn), ("radix node disappeared"));
1233 * use the normal delete code to remove
1236 if (req != RTM_DELETE)
1244 * if the entry is 2nd and on up
1246 if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt))
1247 panic ("rtrequest1: rt_mpath_deldup");
1251 if (req == RTM_DELETE) {
1252 rt->rt_flags &= ~RTF_UP;
1254 * One more rtentry floating around that is not
1255 * linked to the routing table. rttrash will be decremented
1256 * when RTFREE(rt) is eventually called.
1262 if (req != RTM_DELETE)
1263 panic("unrecognized request %d", req);
1267 * If the caller wants it, then it can have it,
1268 * but it's up to it to free the rtentry as we won't be
1282 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
1285 int error = 0, needlock = 0;
1288 struct rtentry *rt0;
1290 struct radix_node *rn;
1291 struct radix_node_head *rnh;
1293 struct sockaddr *ndst;
1294 struct sockaddr_storage mdst;
1295 #define senderr(x) { error = x ; goto bad; }
1297 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
1298 switch (dst->sa_family) {
1301 /* We support multiple FIBs. */
1304 fibnum = RT_DEFAULT_FIB;
1309 * Find the correct routing tree to use for this Address Family
1311 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1313 return (EAFNOSUPPORT);
1314 needlock = ((flags & RTF_RNH_LOCKED) == 0);
1315 flags &= ~RTF_RNH_LOCKED;
1317 RADIX_NODE_HEAD_LOCK(rnh);
1319 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1321 * If we are adding a host route then we don't want to put
1322 * a netmask in the tree, nor do we want to clone it.
1324 if (flags & RTF_HOST)
1330 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
1331 dst = (struct sockaddr *)&mdst;
1334 if (rn_mpath_capable(rnh)) {
1335 error = rn_mpath_update(req, info, rnh, ret_nrt);
1337 * "bad" holds true for the success case
1340 if (error != ENOENT)
1345 if ((flags & RTF_PINNED) == 0) {
1346 /* Check if target route can be deleted */
1347 rt = (struct rtentry *)rnh->rnh_lookup(dst,
1349 if ((rt != NULL) && (rt->rt_flags & RTF_PINNED))
1350 senderr(EADDRINUSE);
1354 * Remove the item from the tree and return it.
1355 * Complain if it is not there and do no more processing.
1357 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1360 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
1361 panic ("rtrequest delete");
1365 rt->rt_flags &= ~RTF_UP;
1368 * give the protocol a chance to keep things in sync.
1370 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
1371 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1374 * One more rtentry floating around that is not
1375 * linked to the routing table. rttrash will be decremented
1376 * when RTFREE(rt) is eventually called.
1381 * If the caller wants it, then it can have it,
1382 * but it's up to it to free the rtentry as we won't be
1393 * resolve was only used for route cloning
1398 if ((flags & RTF_GATEWAY) && !gateway)
1400 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1401 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1404 if (info->rti_ifa == NULL) {
1405 error = rt_getifa_fib(info, fibnum);
1409 ifa_ref(info->rti_ifa);
1410 ifa = info->rti_ifa;
1411 rt = uma_zalloc(V_rtzone, M_NOWAIT);
1416 rt->rt_flags = RTF_UP | flags;
1417 rt->rt_fibnum = fibnum;
1419 * Add the gateway. Possibly re-malloc-ing the storage for it.
1422 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1424 uma_zfree(V_rtzone, rt);
1429 * point to the (possibly newly malloc'd) dest address.
1431 ndst = (struct sockaddr *)rt_key(rt);
1434 * make sure it contains the value we want (masked if needed).
1437 rt_maskedcopy(dst, ndst, netmask);
1439 bcopy(dst, ndst, dst->sa_len);
1442 * We use the ifa reference returned by rt_getifa_fib().
1443 * This moved from below so that rnh->rnh_addaddr() can
1444 * examine the ifa and ifa->ifa_ifp if it so desires.
1447 rt->rt_ifp = ifa->ifa_ifp;
1450 rt_setmetrics(info, rt);
1453 /* do not permit exactly the same dst/mask/gw pair */
1454 if (rn_mpath_capable(rnh) &&
1455 rt_mpath_conflict(rnh, rt, netmask)) {
1456 ifa_free(rt->rt_ifa);
1458 uma_zfree(V_rtzone, rt);
1465 /* "flow-table" only supports IPv6 and IPv4 at the moment. */
1466 switch (dst->sa_family) {
1473 #if defined(INET6) || defined(INET)
1474 rn = rnh->rnh_matchaddr(dst, rnh);
1475 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
1476 struct sockaddr *mask;
1481 * compare mask to see if the new route is
1482 * more specific than the existing one
1489 * A host route is already present, so
1490 * leave the flow-table entries as is.
1492 if (rt0->rt_flags & RTF_HOST) {
1495 } else if (!(flags & RTF_HOST) && netmask) {
1496 mask = rt_mask(rt0);
1499 n = (u_char *)netmask;
1506 if (len == 0 || (*n < *m)) {
1512 #endif/* INET6 || INET */
1514 #endif /* FLOWTABLE */
1516 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1517 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1519 * If it still failed to go into the tree,
1520 * then un-make it (this should be a function)
1523 ifa_free(rt->rt_ifa);
1525 uma_zfree(V_rtzone, rt);
1533 else if (rt0 != NULL) {
1534 flowtable_route_flush(dst->sa_family, rt0);
1540 * If this protocol has something to add to this then
1541 * allow it to do that as well.
1543 if (ifa->ifa_rtrequest)
1544 ifa->ifa_rtrequest(req, rt, info);
1547 * actually return a resultant rtentry and
1548 * give the caller a single reference.
1557 error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum);
1564 RADIX_NODE_HEAD_UNLOCK(rnh);
1577 rtrequest1_fib_change(struct radix_node_head *rnh, struct rt_addrinfo *info,
1578 struct rtentry **ret_nrt, u_int fibnum)
1580 struct rtentry *rt = NULL;
1584 struct if_mtuinfo ifmtu;
1586 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1587 info->rti_info[RTAX_NETMASK], rnh);
1594 * If we got multipath routes,
1595 * we require users to specify a matching RTAX_GATEWAY.
1597 if (rn_mpath_capable(rnh)) {
1598 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
1606 rt_setmetrics(info, rt);
1609 * New gateway could require new ifaddr, ifp;
1610 * flags may also be different; ifp may be specified
1611 * by ll sockaddr when protocol address is ambiguous
1613 if (((rt->rt_flags & RTF_GATEWAY) &&
1614 info->rti_info[RTAX_GATEWAY] != NULL) ||
1615 info->rti_info[RTAX_IFP] != NULL ||
1616 (info->rti_info[RTAX_IFA] != NULL &&
1617 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) {
1619 error = rt_getifa_fib(info, fibnum);
1620 if (info->rti_ifa != NULL)
1627 /* Check if outgoing interface has changed */
1628 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa &&
1629 rt->rt_ifa != NULL && rt->rt_ifa->ifa_rtrequest != NULL) {
1630 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1631 ifa_free(rt->rt_ifa);
1633 /* Update gateway address */
1634 if (info->rti_info[RTAX_GATEWAY] != NULL) {
1635 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]);
1639 rt->rt_flags &= ~RTF_GATEWAY;
1640 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags);
1643 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) {
1644 ifa_ref(info->rti_ifa);
1645 rt->rt_ifa = info->rti_ifa;
1646 rt->rt_ifp = info->rti_ifp;
1648 /* Allow some flags to be toggled on change. */
1649 rt->rt_flags &= ~RTF_FMASK;
1650 rt->rt_flags |= info->rti_flags & RTF_FMASK;
1652 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL)
1653 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
1655 /* Alter route MTU if necessary */
1656 if (rt->rt_ifp != NULL) {
1657 family = info->rti_info[RTAX_DST]->sa_family;
1658 mtu = if_getmtu_family(rt->rt_ifp, family);
1659 /* Set default MTU */
1660 if (rt->rt_mtu == 0)
1662 if (rt->rt_mtu != mtu) {
1663 /* Check if we really need to update */
1664 ifmtu.ifp = rt->rt_ifp;
1666 if_updatemtu_cb(rt->rt_nodes, &ifmtu);
1677 ifa_free(info->rti_ifa);
1682 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt)
1685 if (info->rti_mflags & RTV_MTU) {
1686 if (info->rti_rmx->rmx_mtu != 0) {
1689 * MTU was explicitly provided by user.
1692 rt->rt_flags |= RTF_FIXEDMTU;
1696 * User explicitly sets MTU to 0.
1697 * Assume rollback to default.
1699 rt->rt_flags &= ~RTF_FIXEDMTU;
1701 rt->rt_mtu = info->rti_rmx->rmx_mtu;
1703 if (info->rti_mflags & RTV_WEIGHT)
1704 rt->rt_weight = info->rti_rmx->rmx_weight;
1705 /* Kernel -> userland timebase conversion. */
1706 if (info->rti_mflags & RTV_EXPIRE)
1707 rt->rt_expire = info->rti_rmx->rmx_expire ?
1708 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
1712 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1714 /* XXX dst may be overwritten, can we move this to below */
1715 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1717 struct radix_node_head *rnh;
1719 rnh = rt_tables_get_rnh(rt->rt_fibnum, dst->sa_family);
1723 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1726 * Prepare to store the gateway in rt->rt_gateway.
1727 * Both dst and gateway are stored one after the other in the same
1728 * malloc'd chunk. If we have room, we can reuse the old buffer,
1729 * rt_gateway already points to the right place.
1730 * Otherwise, malloc a new block and update the 'dst' address.
1732 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1735 R_Malloc(new, caddr_t, dlen + glen);
1739 * XXX note, we copy from *dst and not *rt_key(rt) because
1740 * rt_setgate() can be called to initialize a newly
1741 * allocated route entry, in which case rt_key(rt) == NULL
1742 * (and also rt->rt_gateway == NULL).
1743 * Free()/free() handle a NULL argument just fine.
1745 bcopy(dst, new, dlen);
1746 R_Free(rt_key(rt)); /* free old block, if any */
1747 rt_key(rt) = (struct sockaddr *)new;
1748 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1752 * Copy the new gateway value into the memory chunk.
1754 bcopy(gate, rt->rt_gateway, glen);
1760 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1762 u_char *cp1 = (u_char *)src;
1763 u_char *cp2 = (u_char *)dst;
1764 u_char *cp3 = (u_char *)netmask;
1765 u_char *cplim = cp2 + *cp3;
1766 u_char *cplim2 = cp2 + *cp1;
1768 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1773 *cp2++ = *cp1++ & *cp3++;
1775 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1779 * Set up a routing table entry, normally
1782 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1784 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1786 struct sockaddr *dst;
1787 struct sockaddr *netmask;
1788 struct rtentry *rt = NULL;
1789 struct rt_addrinfo info;
1791 int startfib, endfib;
1792 char tempbuf[_SOCKADDR_TMPSIZE];
1795 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1796 struct radix_node_head *rnh;
1798 if (flags & RTF_HOST) {
1799 dst = ifa->ifa_dstaddr;
1802 dst = ifa->ifa_addr;
1803 netmask = ifa->ifa_netmask;
1805 if (dst->sa_len == 0)
1807 switch (dst->sa_family) {
1810 /* We support multiple FIBs. */
1813 fibnum = RT_DEFAULT_FIB;
1816 if (fibnum == RT_ALL_FIBS) {
1817 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD)
1818 startfib = endfib = ifa->ifa_ifp->if_fib;
1821 endfib = rt_numfibs - 1;
1824 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1830 * If it's a delete, check that if it exists,
1831 * it's on the correct interface or we might scrub
1832 * a route to another ifa which would
1833 * be confusing at best and possibly worse.
1835 if (cmd == RTM_DELETE) {
1837 * It's a delete, so it should already exist..
1838 * If it's a net, mask off the host bits
1839 * (Assuming we have a mask)
1840 * XXX this is kinda inet specific..
1842 if (netmask != NULL) {
1843 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1844 dst = (struct sockaddr *)tempbuf;
1848 * Now go through all the requested tables (fibs) and do the
1849 * requested action. Realistically, this will either be fib 0
1850 * for protocols that don't do multiple tables or all the
1851 * tables for those that do.
1853 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1854 if (cmd == RTM_DELETE) {
1855 struct radix_node *rn;
1857 * Look up an rtentry that is in the routing tree and
1858 * contains the correct info.
1860 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1862 /* this table doesn't exist but others might */
1864 RADIX_NODE_HEAD_RLOCK(rnh);
1865 rn = rnh->rnh_lookup(dst, netmask, rnh);
1867 if (rn_mpath_capable(rnh)) {
1874 * for interface route the
1875 * rt->rt_gateway is sockaddr_intf
1876 * for cloning ARP entries, so
1877 * rt_mpath_matchgate must use the
1880 rt = rt_mpath_matchgate(rt,
1887 error = (rn == NULL ||
1888 (rn->rn_flags & RNF_ROOT) ||
1889 RNTORT(rn)->rt_ifa != ifa);
1890 RADIX_NODE_HEAD_RUNLOCK(rnh);
1892 /* this is only an error if bad on ALL tables */
1897 * Do the actual request
1899 bzero((caddr_t)&info, sizeof(info));
1901 info.rti_flags = flags |
1902 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1903 info.rti_info[RTAX_DST] = dst;
1905 * doing this for compatibility reasons
1908 info.rti_info[RTAX_GATEWAY] =
1909 (struct sockaddr *)&null_sdl;
1911 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1912 info.rti_info[RTAX_NETMASK] = netmask;
1913 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1915 if ((error == EEXIST) && (cmd == RTM_ADD)) {
1917 * Interface route addition failed.
1918 * Atomically delete current prefix generating
1919 * RTM_DELETE message, and retry adding
1922 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1923 RADIX_NODE_HEAD_LOCK(rnh);
1925 /* Delete old prefix */
1926 info.rti_ifa = NULL;
1927 info.rti_flags = RTF_RNH_LOCKED;
1929 error = rtrequest1_fib(RTM_DELETE, &info, NULL, fibnum);
1932 info.rti_flags = flags | RTF_RNH_LOCKED |
1933 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1934 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1937 RADIX_NODE_HEAD_UNLOCK(rnh);
1941 if (error == 0 && rt != NULL) {
1943 * notify any listening routing agents of the change
1948 * in case address alias finds the first address
1949 * e.g. ifconfig bge0 192.0.2.246/24
1950 * e.g. ifconfig bge0 192.0.2.247/24
1951 * the address set in the route is 192.0.2.246
1952 * so we need to replace it with 192.0.2.247
1954 if (memcmp(rt->rt_ifa->ifa_addr,
1955 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1956 ifa_free(rt->rt_ifa);
1958 rt->rt_ifp = ifa->ifa_ifp;
1963 * doing this for compatibility reasons
1965 if (cmd == RTM_ADD) {
1966 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1967 rt->rt_ifp->if_type;
1968 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1969 rt->rt_ifp->if_index;
1973 rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum);
1976 if (cmd == RTM_DELETE) {
1978 * If we are deleting, and we found an entry,
1979 * then it's been removed from the tree..
1980 * now throw it away.
1984 if (cmd == RTM_ADD) {
1986 * We just wanted to add it..
1987 * we don't actually need a reference.
1998 if (cmd == RTM_DELETE) {
2002 /* we only give an error if it wasn't in any table */
2003 error = ((flags & RTF_HOST) ?
2004 EHOSTUNREACH : ENETUNREACH);
2008 /* return an error if any of them failed */
2016 * Set up a routing table entry, normally
2020 rtinit(struct ifaddr *ifa, int cmd, int flags)
2022 struct sockaddr *dst;
2023 int fib = RT_DEFAULT_FIB;
2025 if (flags & RTF_HOST) {
2026 dst = ifa->ifa_dstaddr;
2028 dst = ifa->ifa_addr;
2031 switch (dst->sa_family) {
2034 /* We do support multiple FIBs. */
2038 return (rtinit1(ifa, cmd, flags, fib));
2042 * Announce interface address arrival/withdraw
2043 * Returns 0 on success.
2046 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
2049 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2050 ("unexpected cmd %d", cmd));
2052 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2053 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2055 #if defined(INET) || defined(INET6)
2058 * notify the SCTP stack
2059 * this will only get called when an address is added/deleted
2060 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
2062 sctp_addr_change(ifa, cmd);
2065 return (rtsock_addrmsg(cmd, ifa, fibnum));
2069 * Announce route addition/removal.
2070 * Users of this function MUST validate input data BEFORE calling.
2071 * However we have to be able to handle invalid data:
2072 * if some userland app sends us "invalid" route message (invalid mask,
2073 * no dst, wrong address families, etc...) we need to pass it back
2074 * to app (and any other rtsock consumers) with rtm_errno field set to
2076 * Returns 0 on success.
2079 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
2083 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2084 ("unexpected cmd %d", cmd));
2086 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2087 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2089 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
2091 return (rtsock_routemsg(cmd, ifp, error, rt, fibnum));
2095 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
2098 rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS);
2102 * This is called to generate messages from the routing socket
2103 * indicating a network interface has had addresses associated with it.
2106 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
2110 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2111 ("unexpected cmd %u", cmd));
2112 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2113 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2115 if (cmd == RTM_ADD) {
2116 rt_addrmsg(cmd, ifa, fibnum);
2118 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
2121 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
2122 rt_addrmsg(cmd, ifa, fibnum);