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 *);
149 static int if_updatemtu_cb(struct radix_node *, void *);
152 * handler for net.my_fibnum
155 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
160 fibnum = curthread->td_proc->p_fibnum;
161 error = sysctl_handle_int(oidp, &fibnum, 0, req);
165 SYSCTL_PROC(_net, OID_AUTO, my_fibnum, CTLTYPE_INT|CTLFLAG_RD,
166 NULL, 0, &sysctl_my_fibnum, "I", "default FIB of caller");
168 static __inline struct radix_node_head **
169 rt_tables_get_rnh_ptr(int table, int fam)
171 struct radix_node_head **rnh;
173 KASSERT(table >= 0 && table < rt_numfibs, ("%s: table out of bounds.",
175 KASSERT(fam >= 0 && fam < (AF_MAX+1), ("%s: fam out of bounds.",
178 /* rnh is [fib=0][af=0]. */
179 rnh = (struct radix_node_head **)V_rt_tables;
180 /* Get the offset to the requested table and fam. */
181 rnh += table * (AF_MAX+1) + fam;
186 struct radix_node_head *
187 rt_tables_get_rnh(int table, int fam)
190 return (*rt_tables_get_rnh_ptr(table, fam));
194 * route initialization must occur before ip6_init2(), which happenas at
201 /* whack the tunable ints into line. */
202 if (rt_numfibs > RT_MAXFIBS)
203 rt_numfibs = RT_MAXFIBS;
207 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
210 rtentry_zinit(void *mem, int size, int how)
212 struct rtentry *rt = mem;
214 rt->rt_pksent = counter_u64_alloc(how);
215 if (rt->rt_pksent == NULL)
224 rtentry_zfini(void *mem, int size)
226 struct rtentry *rt = mem;
229 counter_u64_free(rt->rt_pksent);
233 rtentry_ctor(void *mem, int size, void *arg, int how)
235 struct rtentry *rt = mem;
237 bzero(rt, offsetof(struct rtentry, rt_endzero));
238 counter_u64_zero(rt->rt_pksent);
244 rtentry_dtor(void *mem, int size, void *arg)
246 struct rtentry *rt = mem;
252 vnet_route_init(const void *unused __unused)
255 struct radix_node_head **rnh;
259 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
260 sizeof(struct radix_node_head *), M_RTABLE, M_WAITOK|M_ZERO);
262 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
263 rtentry_ctor, rtentry_dtor,
264 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0);
265 for (dom = domains; dom; dom = dom->dom_next) {
266 if (dom->dom_rtattach == NULL)
269 for (table = 0; table < rt_numfibs; table++) {
270 fam = dom->dom_family;
271 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
274 rnh = rt_tables_get_rnh_ptr(table, fam);
276 panic("%s: rnh NULL", __func__);
277 dom->dom_rtattach((void **)rnh, 0);
281 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
286 vnet_route_uninit(const void *unused __unused)
291 struct radix_node_head **rnh;
293 for (dom = domains; dom; dom = dom->dom_next) {
294 if (dom->dom_rtdetach == NULL)
297 for (table = 0; table < rt_numfibs; table++) {
298 fam = dom->dom_family;
300 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
303 rnh = rt_tables_get_rnh_ptr(table, fam);
305 panic("%s: rnh NULL", __func__);
306 dom->dom_rtdetach((void **)rnh, 0);
310 free(V_rt_tables, M_RTABLE);
311 uma_zdestroy(V_rtzone);
313 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
314 vnet_route_uninit, 0);
317 #ifndef _SYS_SYSPROTO_H_
323 sys_setfib(struct thread *td, struct setfib_args *uap)
325 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
327 td->td_proc->p_fibnum = uap->fibnum;
332 * Packet routing routines.
335 rtalloc(struct route *ro)
338 rtalloc_ign_fib(ro, 0UL, RT_DEFAULT_FIB);
342 rtalloc_fib(struct route *ro, u_int fibnum)
344 rtalloc_ign_fib(ro, 0UL, fibnum);
348 rtalloc_ign(struct route *ro, u_long ignore)
352 if ((rt = ro->ro_rt) != NULL) {
353 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
358 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, RT_DEFAULT_FIB);
360 RT_UNLOCK(ro->ro_rt);
364 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
368 if ((rt = ro->ro_rt) != NULL) {
369 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
374 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
376 RT_UNLOCK(ro->ro_rt);
380 * Look up the route that matches the address given
381 * Or, at least try.. Create a cloned route if needed.
383 * The returned route, if any, is locked.
386 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
389 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB));
393 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
396 struct radix_node_head *rnh;
397 struct radix_node *rn;
398 struct rtentry *newrt;
399 struct rt_addrinfo info;
400 int err = 0, msgtype = RTM_MISS;
403 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
404 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
410 * Look up the address in the table for that Address Family
412 needlock = !(ignflags & RTF_RNH_LOCKED);
414 RADIX_NODE_HEAD_RLOCK(rnh);
417 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
419 rn = rnh->rnh_matchaddr(dst, rnh);
420 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
425 RADIX_NODE_HEAD_RUNLOCK(rnh);
429 RADIX_NODE_HEAD_RUNLOCK(rnh);
432 * Either we hit the root or couldn't find any match,
433 * Which basically means
434 * "caint get there frm here"
437 V_rtstat.rts_unreach++;
441 * If required, report the failure to the supervising
443 * For a delete, this is not an error. (report == 0)
445 bzero(&info, sizeof(info));
446 info.rti_info[RTAX_DST] = dst;
447 rt_missmsg_fib(msgtype, &info, 0, err, fibnum);
451 RT_LOCK_ASSERT(newrt);
456 * Remove a reference count from an rtentry.
457 * If the count gets low enough, take it out of the routing table
460 rtfree(struct rtentry *rt)
462 struct radix_node_head *rnh;
464 KASSERT(rt != NULL,("%s: NULL rt", __func__));
465 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
466 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
471 * The callers should use RTFREE_LOCKED() or RTFREE(), so
472 * we should come here exactly with the last reference.
475 if (rt->rt_refcnt > 0) {
476 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
481 * On last reference give the "close method" a chance
482 * to cleanup private state. This also permits (for
483 * IPv4 and IPv6) a chance to decide if the routing table
484 * entry should be purged immediately or at a later time.
485 * When an immediate purge is to happen the close routine
486 * typically calls rtexpunge which clears the RTF_UP flag
487 * on the entry so that the code below reclaims the storage.
489 if (rt->rt_refcnt == 0 && rnh->rnh_close)
490 rnh->rnh_close((struct radix_node *)rt, rnh);
493 * If we are no longer "up" (and ref == 0)
494 * then we can free the resources associated
497 if ((rt->rt_flags & RTF_UP) == 0) {
498 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
501 * the rtentry must have been removed from the routing table
502 * so it is represented in rttrash.. remove that now.
506 if (rt->rt_refcnt < 0) {
507 printf("rtfree: %p not freed (neg refs)\n", rt);
512 * release references on items we hold them on..
513 * e.g other routes and ifaddrs.
516 ifa_free(rt->rt_ifa);
518 * The key is separatly alloc'd so free it (see rt_setgate()).
519 * This also frees the gateway, as they are always malloc'd
525 * and the rtentry itself of course
527 uma_zfree(V_rtzone, rt);
536 * Force a routing table entry to the specified
537 * destination to go through the given gateway.
538 * Normally called as a result of a routing redirect
539 * message from the network layer.
542 rtredirect(struct sockaddr *dst,
543 struct sockaddr *gateway,
544 struct sockaddr *netmask,
546 struct sockaddr *src)
549 rtredirect_fib(dst, gateway, netmask, flags, src, RT_DEFAULT_FIB);
553 rtredirect_fib(struct sockaddr *dst,
554 struct sockaddr *gateway,
555 struct sockaddr *netmask,
557 struct sockaddr *src,
560 struct rtentry *rt, *rt0 = NULL;
563 struct rt_addrinfo info;
565 struct radix_node_head *rnh;
568 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
570 error = EAFNOSUPPORT;
574 /* verify the gateway is directly reachable */
575 if ((ifa = ifa_ifwithnet(gateway, 0, fibnum)) == NULL) {
579 rt = rtalloc1_fib(dst, 0, 0UL, fibnum); /* NB: rt is locked */
581 * If the redirect isn't from our current router for this dst,
582 * it's either old or wrong. If it redirects us to ourselves,
583 * we have a routing loop, perhaps as a result of an interface
584 * going down recently.
586 if (!(flags & RTF_DONE) && rt &&
587 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
589 else if (ifa_ifwithaddr_check(gateway))
590 error = EHOSTUNREACH;
594 * Create a new entry if we just got back a wildcard entry
595 * or the lookup failed. This is necessary for hosts
596 * which use routing redirects generated by smart gateways
597 * to dynamically build the routing tables.
599 if (rt == NULL || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
602 * Don't listen to the redirect if it's
603 * for a route to an interface.
605 if (rt->rt_flags & RTF_GATEWAY) {
606 if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
608 * Changing from route to net => route to host.
609 * Create new route, rather than smashing route to net.
615 flags |= RTF_GATEWAY | RTF_DYNAMIC;
616 bzero((caddr_t)&info, sizeof(info));
617 info.rti_info[RTAX_DST] = dst;
618 info.rti_info[RTAX_GATEWAY] = gateway;
619 info.rti_info[RTAX_NETMASK] = netmask;
621 info.rti_flags = flags;
623 RT_UNLOCK(rt0); /* drop lock to avoid LOR with RNH */
624 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
628 EVENTHANDLER_INVOKE(route_redirect_event, rt0, rt, dst);
629 flags = rt->rt_flags;
634 stat = &V_rtstat.rts_dynamic;
636 struct rtentry *gwrt;
639 * Smash the current notion of the gateway to
640 * this destination. Should check about netmask!!!
642 rt->rt_flags |= RTF_MODIFIED;
643 flags |= RTF_MODIFIED;
644 stat = &V_rtstat.rts_newgateway;
646 * add the key and gateway (in one malloc'd chunk).
649 RADIX_NODE_HEAD_LOCK(rnh);
651 rt_setgate(rt, rt_key(rt), gateway);
652 gwrt = rtalloc1(gateway, 1, RTF_RNH_LOCKED);
653 RADIX_NODE_HEAD_UNLOCK(rnh);
654 EVENTHANDLER_INVOKE(route_redirect_event, rt, gwrt, dst);
658 error = EHOSTUNREACH;
664 V_rtstat.rts_badredirect++;
665 else if (stat != NULL)
667 bzero((caddr_t)&info, sizeof(info));
668 info.rti_info[RTAX_DST] = dst;
669 info.rti_info[RTAX_GATEWAY] = gateway;
670 info.rti_info[RTAX_NETMASK] = netmask;
671 info.rti_info[RTAX_AUTHOR] = src;
672 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
678 rtioctl(u_long req, caddr_t data)
681 return (rtioctl_fib(req, data, RT_DEFAULT_FIB));
685 * Routing table ioctl interface.
688 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
692 * If more ioctl commands are added here, make sure the proper
693 * super-user checks are being performed because it is possible for
694 * prison-root to make it this far if raw sockets have been enabled
698 /* Multicast goop, grrr... */
699 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
706 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway,
712 if ((flags & RTF_GATEWAY) == 0) {
714 * If we are adding a route to an interface,
715 * and the interface is a pt to pt link
716 * we should search for the destination
717 * as our clue to the interface. Otherwise
718 * we can use the local address.
721 if (flags & RTF_HOST)
722 ifa = ifa_ifwithdstaddr(dst, fibnum);
724 ifa = ifa_ifwithaddr(gateway);
727 * If we are adding a route to a remote net
728 * or host, the gateway may still be on the
729 * other end of a pt to pt link.
731 ifa = ifa_ifwithdstaddr(gateway, fibnum);
734 ifa = ifa_ifwithnet(gateway, 0, fibnum);
736 struct rtentry *rt = rtalloc1_fib(gateway, 0, RTF_RNH_LOCKED, fibnum);
740 * dismiss a gateway that is reachable only
741 * through the default router
743 switch (gateway->sa_family) {
745 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
749 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
755 if (!not_found && rt->rt_ifa != NULL) {
761 if (not_found || ifa == NULL)
764 if (ifa->ifa_addr->sa_family != dst->sa_family) {
765 struct ifaddr *oifa = ifa;
766 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
776 * Do appropriate manipulations of a routing tree given
777 * all the bits of info needed
781 struct sockaddr *dst,
782 struct sockaddr *gateway,
783 struct sockaddr *netmask,
785 struct rtentry **ret_nrt)
788 return (rtrequest_fib(req, dst, gateway, netmask, flags, ret_nrt,
793 rtrequest_fib(int req,
794 struct sockaddr *dst,
795 struct sockaddr *gateway,
796 struct sockaddr *netmask,
798 struct rtentry **ret_nrt,
801 struct rt_addrinfo info;
803 if (dst->sa_len == 0)
806 bzero((caddr_t)&info, sizeof(info));
807 info.rti_flags = flags;
808 info.rti_info[RTAX_DST] = dst;
809 info.rti_info[RTAX_GATEWAY] = gateway;
810 info.rti_info[RTAX_NETMASK] = netmask;
811 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
815 * These (questionable) definitions of apparent local variables apply
816 * to the next two functions. XXXXXX!!!
818 #define dst info->rti_info[RTAX_DST]
819 #define gateway info->rti_info[RTAX_GATEWAY]
820 #define netmask info->rti_info[RTAX_NETMASK]
821 #define ifaaddr info->rti_info[RTAX_IFA]
822 #define ifpaddr info->rti_info[RTAX_IFP]
823 #define flags info->rti_flags
826 rt_getifa(struct rt_addrinfo *info)
829 return (rt_getifa_fib(info, RT_DEFAULT_FIB));
833 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined,
834 * it will be referenced so the caller must free it.
837 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
843 * ifp may be specified by sockaddr_dl
844 * when protocol address is ambiguous.
846 if (info->rti_ifp == NULL && ifpaddr != NULL &&
847 ifpaddr->sa_family == AF_LINK &&
848 (ifa = ifa_ifwithnet(ifpaddr, 0, fibnum)) != NULL) {
849 info->rti_ifp = ifa->ifa_ifp;
852 if (info->rti_ifa == NULL && ifaaddr != NULL)
853 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
854 if (info->rti_ifa == NULL) {
857 sa = ifaaddr != NULL ? ifaaddr :
858 (gateway != NULL ? gateway : dst);
859 if (sa != NULL && info->rti_ifp != NULL)
860 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
861 else if (dst != NULL && gateway != NULL)
862 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
865 info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
868 if ((ifa = info->rti_ifa) != NULL) {
869 if (info->rti_ifp == NULL)
870 info->rti_ifp = ifa->ifa_ifp;
877 * Expunges references to a route that's about to be reclaimed.
878 * The route must be locked.
881 rt_expunge(struct radix_node_head *rnh, struct rtentry *rt)
883 #if !defined(RADIX_MPATH)
884 struct radix_node *rn;
886 struct rt_addrinfo info;
894 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
898 bzero(&info, sizeof(info));
899 info.rti_ifp = rt->rt_ifp;
900 info.rti_flags = RTF_RNH_LOCKED;
901 info.rti_info[RTAX_DST] = rt_key(rt);
902 info.rti_info[RTAX_GATEWAY] = rt->rt_ifa->ifa_addr;
905 error = rtrequest1_fib(RTM_DELETE, &info, &rt0, fib);
907 if (error == 0 && rt0 != NULL) {
910 } else if (error != 0) {
916 * Remove the item from the tree; it should be there,
917 * but when callers invoke us blindly it may not (sigh).
919 rn = rnh->rnh_deladdr(rt_key(rt), rt_mask(rt), rnh);
924 KASSERT((rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) == 0,
925 ("unexpected flags 0x%x", rn->rn_flags));
926 KASSERT(rt == RNTORT(rn),
927 ("lookup mismatch, rt %p rn %p", rt, rn));
928 #endif /* RADIX_MPATH */
930 rt->rt_flags &= ~RTF_UP;
933 * Give the protocol a chance to keep things in sync.
935 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) {
936 struct rt_addrinfo info;
938 bzero((caddr_t)&info, sizeof(info));
939 info.rti_flags = rt->rt_flags;
940 info.rti_info[RTAX_DST] = rt_key(rt);
941 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
942 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
943 ifa->ifa_rtrequest(RTM_DELETE, rt, &info);
947 * one more rtentry floating around that is not
948 * linked to the routing table.
951 #if !defined(RADIX_MPATH)
958 if_updatemtu_cb(struct radix_node *rn, void *arg)
961 struct if_mtuinfo *ifmtu;
963 rt = (struct rtentry *)rn;
964 ifmtu = (struct if_mtuinfo *)arg;
966 if (rt->rt_ifp != ifmtu->ifp)
969 if (rt->rt_mtu >= ifmtu->mtu) {
970 /* We have to decrease mtu regardless of flags */
971 rt->rt_mtu = ifmtu->mtu;
976 * New MTU is bigger. Check if are allowed to alter it
978 if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) {
981 * Skip routes with user-supplied MTU and
982 * non-interface routes
987 /* We are safe to update route MTU */
988 rt->rt_mtu = ifmtu->mtu;
994 rt_updatemtu(struct ifnet *ifp)
996 struct if_mtuinfo ifmtu;
997 struct radix_node_head *rnh;
1003 * Try to update rt_mtu for all routes using this interface
1004 * Unfortunately the only way to do this is to traverse all
1005 * routing tables in all fibs/domains.
1007 for (i = 1; i <= AF_MAX; i++) {
1008 ifmtu.mtu = if_getmtu_family(ifp, i);
1009 for (j = 0; j < rt_numfibs; j++) {
1010 rnh = rt_tables_get_rnh(j, i);
1013 RADIX_NODE_HEAD_LOCK(rnh);
1014 rnh->rnh_walktree(rnh, if_updatemtu_cb, &ifmtu);
1015 RADIX_NODE_HEAD_UNLOCK(rnh);
1022 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
1023 int rt_print(char *buf, int buflen, struct rtentry *rt);
1026 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
1030 switch (s->sa_family) {
1032 paddr = &((struct sockaddr_in *)s)->sin_addr;
1035 paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
1042 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
1045 return (strlen(buf));
1049 rt_print(char *buf, int buflen, struct rtentry *rt)
1051 struct sockaddr *addr, *mask;
1057 i = p_sockaddr(buf, buflen, addr);
1058 if (!(rt->rt_flags & RTF_HOST)) {
1060 i += p_sockaddr(buf + i, buflen - i, mask);
1063 if (rt->rt_flags & RTF_GATEWAY) {
1065 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway);
1074 rn_mpath_update(int req, struct rt_addrinfo *info,
1075 struct radix_node_head *rnh, struct rtentry **ret_nrt)
1078 * if we got multipath routes, we require users to specify
1079 * a matching RTAX_GATEWAY.
1081 struct rtentry *rt, *rto = NULL;
1082 struct radix_node *rn;
1085 rn = rnh->rnh_lookup(dst, netmask, rnh);
1088 rto = rt = RNTORT(rn);
1090 rt = rt_mpath_matchgate(rt, gateway);
1094 * this is the first entry in the chain
1097 rn = rn_mpath_next((struct radix_node *)rt);
1099 * there is another entry, now it's active
1104 rto->rt_flags |= RTF_UP;
1106 } else if (rt->rt_flags & RTF_GATEWAY) {
1108 * For gateway routes, we need to
1109 * make sure that we we are deleting
1110 * the correct gateway.
1111 * rt_mpath_matchgate() does not
1112 * check the case when there is only
1113 * one route in the chain.
1116 (rt->rt_gateway->sa_len != gateway->sa_len ||
1117 memcmp(rt->rt_gateway, gateway, gateway->sa_len)))
1121 * remove from tree before returning it
1124 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1125 KASSERT(rt == RNTORT(rn), ("radix node disappeared"));
1131 * use the normal delete code to remove
1134 if (req != RTM_DELETE)
1142 * if the entry is 2nd and on up
1144 if ((req == RTM_DELETE) && !rt_mpath_deldup(rto, rt))
1145 panic ("rtrequest1: rt_mpath_deldup");
1149 if (req == RTM_DELETE) {
1150 rt->rt_flags &= ~RTF_UP;
1152 * One more rtentry floating around that is not
1153 * linked to the routing table. rttrash will be decremented
1154 * when RTFREE(rt) is eventually called.
1160 if (req != RTM_DELETE)
1161 panic("unrecognized request %d", req);
1165 * If the caller wants it, then it can have it,
1166 * but it's up to it to free the rtentry as we won't be
1180 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
1183 int error = 0, needlock = 0;
1186 struct rtentry *rt0;
1188 struct radix_node *rn;
1189 struct radix_node_head *rnh;
1191 struct sockaddr *ndst;
1192 struct sockaddr_storage mdst;
1193 #define senderr(x) { error = x ; goto bad; }
1195 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
1196 switch (dst->sa_family) {
1199 /* We support multiple FIBs. */
1202 fibnum = RT_DEFAULT_FIB;
1207 * Find the correct routing tree to use for this Address Family
1209 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1211 return (EAFNOSUPPORT);
1212 needlock = ((flags & RTF_RNH_LOCKED) == 0);
1213 flags &= ~RTF_RNH_LOCKED;
1215 RADIX_NODE_HEAD_LOCK(rnh);
1217 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1219 * If we are adding a host route then we don't want to put
1220 * a netmask in the tree, nor do we want to clone it.
1222 if (flags & RTF_HOST)
1228 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
1229 dst = (struct sockaddr *)&mdst;
1232 if (rn_mpath_capable(rnh)) {
1233 error = rn_mpath_update(req, info, rnh, ret_nrt);
1235 * "bad" holds true for the success case
1238 if (error != ENOENT)
1243 if ((flags & RTF_PINNED) == 0) {
1244 /* Check if target route can be deleted */
1245 rt = (struct rtentry *)rnh->rnh_lookup(dst,
1247 if ((rt != NULL) && (rt->rt_flags & RTF_PINNED))
1248 senderr(EADDRINUSE);
1252 * Remove the item from the tree and return it.
1253 * Complain if it is not there and do no more processing.
1255 rn = rnh->rnh_deladdr(dst, netmask, rnh);
1258 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
1259 panic ("rtrequest delete");
1263 rt->rt_flags &= ~RTF_UP;
1266 * give the protocol a chance to keep things in sync.
1268 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
1269 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1272 * One more rtentry floating around that is not
1273 * linked to the routing table. rttrash will be decremented
1274 * when RTFREE(rt) is eventually called.
1279 * If the caller wants it, then it can have it,
1280 * but it's up to it to free the rtentry as we won't be
1291 * resolve was only used for route cloning
1296 if ((flags & RTF_GATEWAY) && !gateway)
1298 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1299 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1302 if (info->rti_ifa == NULL) {
1303 error = rt_getifa_fib(info, fibnum);
1307 ifa_ref(info->rti_ifa);
1308 ifa = info->rti_ifa;
1309 rt = uma_zalloc(V_rtzone, M_NOWAIT);
1314 rt->rt_flags = RTF_UP | flags;
1315 rt->rt_fibnum = fibnum;
1317 * Add the gateway. Possibly re-malloc-ing the storage for it.
1320 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1322 uma_zfree(V_rtzone, rt);
1327 * point to the (possibly newly malloc'd) dest address.
1329 ndst = (struct sockaddr *)rt_key(rt);
1332 * make sure it contains the value we want (masked if needed).
1335 rt_maskedcopy(dst, ndst, netmask);
1337 bcopy(dst, ndst, dst->sa_len);
1340 * We use the ifa reference returned by rt_getifa_fib().
1341 * This moved from below so that rnh->rnh_addaddr() can
1342 * examine the ifa and ifa->ifa_ifp if it so desires.
1345 rt->rt_ifp = ifa->ifa_ifp;
1348 rt_setmetrics(info, rt);
1351 /* do not permit exactly the same dst/mask/gw pair */
1352 if (rn_mpath_capable(rnh) &&
1353 rt_mpath_conflict(rnh, rt, netmask)) {
1354 ifa_free(rt->rt_ifa);
1356 uma_zfree(V_rtzone, rt);
1363 /* "flow-table" only supports IPv6 and IPv4 at the moment. */
1364 switch (dst->sa_family) {
1371 #if defined(INET6) || defined(INET)
1372 rn = rnh->rnh_matchaddr(dst, rnh);
1373 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
1374 struct sockaddr *mask;
1379 * compare mask to see if the new route is
1380 * more specific than the existing one
1387 * A host route is already present, so
1388 * leave the flow-table entries as is.
1390 if (rt0->rt_flags & RTF_HOST) {
1393 } else if (!(flags & RTF_HOST) && netmask) {
1394 mask = rt_mask(rt0);
1397 n = (u_char *)netmask;
1404 if (len == 0 || (*n < *m)) {
1410 #endif/* INET6 || INET */
1412 #endif /* FLOWTABLE */
1414 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1415 rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
1417 * If it still failed to go into the tree,
1418 * then un-make it (this should be a function)
1421 ifa_free(rt->rt_ifa);
1423 uma_zfree(V_rtzone, rt);
1431 else if (rt0 != NULL) {
1432 flowtable_route_flush(dst->sa_family, rt0);
1438 * If this protocol has something to add to this then
1439 * allow it to do that as well.
1441 if (ifa->ifa_rtrequest)
1442 ifa->ifa_rtrequest(req, rt, info);
1445 * actually return a resultant rtentry and
1446 * give the caller a single reference.
1455 error = rtrequest1_fib_change(rnh, info, ret_nrt, fibnum);
1462 RADIX_NODE_HEAD_UNLOCK(rnh);
1475 rtrequest1_fib_change(struct radix_node_head *rnh, struct rt_addrinfo *info,
1476 struct rtentry **ret_nrt, u_int fibnum)
1478 struct rtentry *rt = NULL;
1482 struct if_mtuinfo ifmtu;
1484 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1485 info->rti_info[RTAX_NETMASK], rnh);
1492 * If we got multipath routes,
1493 * we require users to specify a matching RTAX_GATEWAY.
1495 if (rn_mpath_capable(rnh)) {
1496 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
1504 rt_setmetrics(info, rt);
1507 * New gateway could require new ifaddr, ifp;
1508 * flags may also be different; ifp may be specified
1509 * by ll sockaddr when protocol address is ambiguous
1511 if (((rt->rt_flags & RTF_GATEWAY) &&
1512 info->rti_info[RTAX_GATEWAY] != NULL) ||
1513 info->rti_info[RTAX_IFP] != NULL ||
1514 (info->rti_info[RTAX_IFA] != NULL &&
1515 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) {
1517 error = rt_getifa_fib(info, fibnum);
1518 if (info->rti_ifa != NULL)
1525 /* Check if outgoing interface has changed */
1526 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa &&
1527 rt->rt_ifa != NULL && rt->rt_ifa->ifa_rtrequest != NULL) {
1528 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1529 ifa_free(rt->rt_ifa);
1531 /* Update gateway address */
1532 if (info->rti_info[RTAX_GATEWAY] != NULL) {
1533 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]);
1537 rt->rt_flags &= ~RTF_GATEWAY;
1538 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags);
1541 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) {
1542 ifa_ref(info->rti_ifa);
1543 rt->rt_ifa = info->rti_ifa;
1544 rt->rt_ifp = info->rti_ifp;
1546 /* Allow some flags to be toggled on change. */
1547 rt->rt_flags &= ~RTF_FMASK;
1548 rt->rt_flags |= info->rti_flags & RTF_FMASK;
1550 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL)
1551 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
1553 /* Alter route MTU if necessary */
1554 if (rt->rt_ifp != NULL) {
1555 family = info->rti_info[RTAX_DST]->sa_family;
1556 mtu = if_getmtu_family(rt->rt_ifp, family);
1557 /* Set default MTU */
1558 if (rt->rt_mtu == 0)
1560 if (rt->rt_mtu != mtu) {
1561 /* Check if we really need to update */
1562 ifmtu.ifp = rt->rt_ifp;
1564 if_updatemtu_cb(rt->rt_nodes, &ifmtu);
1575 ifa_free(info->rti_ifa);
1580 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt)
1583 if (info->rti_mflags & RTV_MTU) {
1584 if (info->rti_rmx->rmx_mtu != 0) {
1587 * MTU was explicitly provided by user.
1590 rt->rt_flags |= RTF_FIXEDMTU;
1594 * User explicitly sets MTU to 0.
1595 * Assume rollback to default.
1597 rt->rt_flags &= ~RTF_FIXEDMTU;
1599 rt->rt_mtu = info->rti_rmx->rmx_mtu;
1601 if (info->rti_mflags & RTV_WEIGHT)
1602 rt->rt_weight = info->rti_rmx->rmx_weight;
1603 /* Kernel -> userland timebase conversion. */
1604 if (info->rti_mflags & RTV_EXPIRE)
1605 rt->rt_expire = info->rti_rmx->rmx_expire ?
1606 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
1610 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1612 /* XXX dst may be overwritten, can we move this to below */
1613 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1615 struct radix_node_head *rnh;
1617 rnh = rt_tables_get_rnh(rt->rt_fibnum, dst->sa_family);
1621 RADIX_NODE_HEAD_LOCK_ASSERT(rnh);
1624 * Prepare to store the gateway in rt->rt_gateway.
1625 * Both dst and gateway are stored one after the other in the same
1626 * malloc'd chunk. If we have room, we can reuse the old buffer,
1627 * rt_gateway already points to the right place.
1628 * Otherwise, malloc a new block and update the 'dst' address.
1630 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
1633 R_Malloc(new, caddr_t, dlen + glen);
1637 * XXX note, we copy from *dst and not *rt_key(rt) because
1638 * rt_setgate() can be called to initialize a newly
1639 * allocated route entry, in which case rt_key(rt) == NULL
1640 * (and also rt->rt_gateway == NULL).
1641 * Free()/free() handle a NULL argument just fine.
1643 bcopy(dst, new, dlen);
1644 R_Free(rt_key(rt)); /* free old block, if any */
1645 rt_key(rt) = (struct sockaddr *)new;
1646 rt->rt_gateway = (struct sockaddr *)(new + dlen);
1650 * Copy the new gateway value into the memory chunk.
1652 bcopy(gate, rt->rt_gateway, glen);
1658 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
1660 u_char *cp1 = (u_char *)src;
1661 u_char *cp2 = (u_char *)dst;
1662 u_char *cp3 = (u_char *)netmask;
1663 u_char *cplim = cp2 + *cp3;
1664 u_char *cplim2 = cp2 + *cp1;
1666 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1671 *cp2++ = *cp1++ & *cp3++;
1673 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
1677 * Set up a routing table entry, normally
1680 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
1682 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
1684 struct sockaddr *dst;
1685 struct sockaddr *netmask;
1686 struct rtentry *rt = NULL;
1687 struct rt_addrinfo info;
1689 int startfib, endfib;
1690 char tempbuf[_SOCKADDR_TMPSIZE];
1693 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
1694 struct radix_node_head *rnh;
1696 if (flags & RTF_HOST) {
1697 dst = ifa->ifa_dstaddr;
1700 dst = ifa->ifa_addr;
1701 netmask = ifa->ifa_netmask;
1703 if (dst->sa_len == 0)
1705 switch (dst->sa_family) {
1708 /* We support multiple FIBs. */
1711 fibnum = RT_DEFAULT_FIB;
1714 if (fibnum == RT_ALL_FIBS) {
1715 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD)
1716 startfib = endfib = ifa->ifa_ifp->if_fib;
1719 endfib = rt_numfibs - 1;
1722 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
1728 * If it's a delete, check that if it exists,
1729 * it's on the correct interface or we might scrub
1730 * a route to another ifa which would
1731 * be confusing at best and possibly worse.
1733 if (cmd == RTM_DELETE) {
1735 * It's a delete, so it should already exist..
1736 * If it's a net, mask off the host bits
1737 * (Assuming we have a mask)
1738 * XXX this is kinda inet specific..
1740 if (netmask != NULL) {
1741 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
1742 dst = (struct sockaddr *)tempbuf;
1746 * Now go through all the requested tables (fibs) and do the
1747 * requested action. Realistically, this will either be fib 0
1748 * for protocols that don't do multiple tables or all the
1749 * tables for those that do.
1751 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
1752 if (cmd == RTM_DELETE) {
1753 struct radix_node *rn;
1755 * Look up an rtentry that is in the routing tree and
1756 * contains the correct info.
1758 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1760 /* this table doesn't exist but others might */
1762 RADIX_NODE_HEAD_RLOCK(rnh);
1763 rn = rnh->rnh_lookup(dst, netmask, rnh);
1765 if (rn_mpath_capable(rnh)) {
1772 * for interface route the
1773 * rt->rt_gateway is sockaddr_intf
1774 * for cloning ARP entries, so
1775 * rt_mpath_matchgate must use the
1778 rt = rt_mpath_matchgate(rt,
1785 error = (rn == NULL ||
1786 (rn->rn_flags & RNF_ROOT) ||
1787 RNTORT(rn)->rt_ifa != ifa);
1788 RADIX_NODE_HEAD_RUNLOCK(rnh);
1790 /* this is only an error if bad on ALL tables */
1795 * Do the actual request
1797 bzero((caddr_t)&info, sizeof(info));
1799 info.rti_flags = flags |
1800 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1801 info.rti_info[RTAX_DST] = dst;
1803 * doing this for compatibility reasons
1806 info.rti_info[RTAX_GATEWAY] =
1807 (struct sockaddr *)&null_sdl;
1809 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1810 info.rti_info[RTAX_NETMASK] = netmask;
1811 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1813 if ((error == EEXIST) && (cmd == RTM_ADD)) {
1815 * Interface route addition failed.
1816 * Atomically delete current prefix generating
1817 * RTM_DELETE message, and retry adding
1820 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1821 RADIX_NODE_HEAD_LOCK(rnh);
1823 /* Delete old prefix */
1824 info.rti_ifa = NULL;
1825 info.rti_flags = RTF_RNH_LOCKED;
1827 error = rtrequest1_fib(RTM_DELETE, &info, NULL, fibnum);
1830 info.rti_flags = flags | RTF_RNH_LOCKED |
1831 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1832 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
1835 RADIX_NODE_HEAD_UNLOCK(rnh);
1839 if (error == 0 && rt != NULL) {
1841 * notify any listening routing agents of the change
1846 * in case address alias finds the first address
1847 * e.g. ifconfig bge0 192.0.2.246/24
1848 * e.g. ifconfig bge0 192.0.2.247/24
1849 * the address set in the route is 192.0.2.246
1850 * so we need to replace it with 192.0.2.247
1852 if (memcmp(rt->rt_ifa->ifa_addr,
1853 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
1854 ifa_free(rt->rt_ifa);
1856 rt->rt_ifp = ifa->ifa_ifp;
1861 * doing this for compatibility reasons
1863 if (cmd == RTM_ADD) {
1864 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
1865 rt->rt_ifp->if_type;
1866 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
1867 rt->rt_ifp->if_index;
1871 rt_newaddrmsg_fib(cmd, ifa, error, rt, fibnum);
1874 if (cmd == RTM_DELETE) {
1876 * If we are deleting, and we found an entry,
1877 * then it's been removed from the tree..
1878 * now throw it away.
1882 if (cmd == RTM_ADD) {
1884 * We just wanted to add it..
1885 * we don't actually need a reference.
1896 if (cmd == RTM_DELETE) {
1900 /* we only give an error if it wasn't in any table */
1901 error = ((flags & RTF_HOST) ?
1902 EHOSTUNREACH : ENETUNREACH);
1906 /* return an error if any of them failed */
1914 * Set up a routing table entry, normally
1918 rtinit(struct ifaddr *ifa, int cmd, int flags)
1920 struct sockaddr *dst;
1921 int fib = RT_DEFAULT_FIB;
1923 if (flags & RTF_HOST) {
1924 dst = ifa->ifa_dstaddr;
1926 dst = ifa->ifa_addr;
1929 switch (dst->sa_family) {
1932 /* We do support multiple FIBs. */
1936 return (rtinit1(ifa, cmd, flags, fib));
1940 * Announce interface address arrival/withdraw
1941 * Returns 0 on success.
1944 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1947 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1948 ("unexpected cmd %d", cmd));
1950 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1951 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1953 #if defined(INET) || defined(INET6)
1956 * notify the SCTP stack
1957 * this will only get called when an address is added/deleted
1958 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1960 sctp_addr_change(ifa, cmd);
1963 return (rtsock_addrmsg(cmd, ifa, fibnum));
1967 * Announce route addition/removal.
1968 * Users of this function MUST validate input data BEFORE calling.
1969 * However we have to be able to handle invalid data:
1970 * if some userland app sends us "invalid" route message (invalid mask,
1971 * no dst, wrong address families, etc...) we need to pass it back
1972 * to app (and any other rtsock consumers) with rtm_errno field set to
1974 * Returns 0 on success.
1977 rt_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1981 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1982 ("unexpected cmd %d", cmd));
1984 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1985 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1987 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
1989 return (rtsock_routemsg(cmd, ifp, error, rt, fibnum));
1993 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1996 rt_newaddrmsg_fib(cmd, ifa, error, rt, RT_ALL_FIBS);
2000 * This is called to generate messages from the routing socket
2001 * indicating a network interface has had addresses associated with it.
2004 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt,
2008 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2009 ("unexpected cmd %u", cmd));
2010 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2011 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2013 if (cmd == RTM_ADD) {
2014 rt_addrmsg(cmd, ifa, fibnum);
2016 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
2019 rt_routemsg(cmd, ifa->ifa_ifp, error, rt, fibnum);
2020 rt_addrmsg(cmd, ifa, fibnum);