2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95
34 /************************************************************************
35 * Note: In this file a 'fib' is a "forwarding information base" *
36 * Which is the new name for an in kernel routing (next hop) table. *
37 ***********************************************************************/
40 #include "opt_inet6.h"
41 #include "opt_mrouting.h"
42 #include "opt_mpath.h"
43 #include "opt_route.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/sysproto.h>
54 #include <sys/domain.h>
55 #include <sys/eventhandler.h>
56 #include <sys/kernel.h>
58 #include <sys/rmlock.h>
61 #include <net/if_var.h>
62 #include <net/if_dl.h>
63 #include <net/route.h>
64 #include <net/route_var.h>
65 #include <net/route/nhop.h>
66 #include <net/route/shared.h>
70 #include <net/radix_mpath.h>
73 #include <netinet/in.h>
74 #include <netinet/ip_mroute.h>
78 #define RT_MAXFIBS UINT16_MAX
80 /* Kernel config default option. */
83 #error "ROUTETABLES defined too low"
85 #if ROUTETABLES > RT_MAXFIBS
86 #error "ROUTETABLES defined too big"
88 #define RT_NUMFIBS ROUTETABLES
89 #endif /* ROUTETABLES */
90 /* Initialize to default if not otherwise set. */
95 /* This is read-only.. */
96 u_int rt_numfibs = RT_NUMFIBS;
97 SYSCTL_UINT(_net, OID_AUTO, fibs, CTLFLAG_RDTUN, &rt_numfibs, 0, "");
100 * By default add routes to all fibs for new interfaces.
101 * Once this is set to 0 then only allocate routes on interface
102 * changes for the FIB of the caller when adding a new set of addresses
103 * to an interface. XXX this is a shotgun aproach to a problem that needs
104 * a more fine grained solution.. that will come.
105 * XXX also has the problems getting the FIB from curthread which will not
106 * always work given the fib can be overridden and prefixes can be added
107 * from the network stack context.
109 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 1;
110 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
111 &VNET_NAME(rt_add_addr_allfibs), 0, "");
113 VNET_PCPUSTAT_DEFINE(struct rtstat, rtstat);
115 VNET_PCPUSTAT_SYSINIT(rtstat);
117 VNET_PCPUSTAT_SYSUNINIT(rtstat);
120 VNET_DEFINE(struct rib_head *, rt_tables);
121 #define V_rt_tables VNET(rt_tables)
123 VNET_DEFINE(int, rttrash); /* routes not in table but not freed */
124 #define V_rttrash VNET(rttrash)
128 * Convert a 'struct radix_node *' to a 'struct rtentry *'.
129 * The operation can be done safely (in this code) because a
130 * 'struct rtentry' starts with two 'struct radix_node''s, the first
131 * one representing leaf nodes in the routing tree, which is
132 * what the code in radix.c passes us as a 'struct radix_node'.
134 * But because there are a lot of assumptions in this conversion,
135 * do not cast explicitly, but always use the macro below.
137 #define RNTORT(p) ((struct rtentry *)(p))
139 VNET_DEFINE_STATIC(uma_zone_t, rtzone); /* Routing table UMA zone. */
140 #define V_rtzone VNET(rtzone)
142 EVENTHANDLER_LIST_DEFINE(rt_addrmsg);
144 static int rt_getifa_fib(struct rt_addrinfo *, u_int);
145 static void rt_setmetrics(const struct rt_addrinfo *, struct rtentry *);
146 static int rt_ifdelroute(const struct rtentry *rt, void *arg);
147 static struct rtentry *rt_unlinkrte(struct rib_head *rnh,
148 struct rt_addrinfo *info, int *perror);
149 static void rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info);
151 static struct radix_node *rt_mpath_unlink(struct rib_head *rnh,
152 struct rt_addrinfo *info, struct rtentry *rto, int *perror);
154 static int rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info,
157 static int add_route(struct rib_head *rnh, struct rt_addrinfo *info,
158 struct rtentry **ret_nrt);
159 static int del_route(struct rib_head *rnh, struct rt_addrinfo *info,
160 struct rtentry **ret_nrt);
161 static int change_route(struct rib_head *, struct rt_addrinfo *,
170 static int if_updatemtu_cb(struct radix_node *, void *);
173 * handler for net.my_fibnum
176 sysctl_my_fibnum(SYSCTL_HANDLER_ARGS)
181 fibnum = curthread->td_proc->p_fibnum;
182 error = sysctl_handle_int(oidp, &fibnum, 0, req);
186 SYSCTL_PROC(_net, OID_AUTO, my_fibnum,
187 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
188 &sysctl_my_fibnum, "I",
189 "default FIB of caller");
191 static __inline struct rib_head **
192 rt_tables_get_rnh_ptr(int table, int fam)
194 struct rib_head **rnh;
196 KASSERT(table >= 0 && table < rt_numfibs,
197 ("%s: table out of bounds (0 <= %d < %d)", __func__, table,
199 KASSERT(fam >= 0 && fam < (AF_MAX + 1),
200 ("%s: fam out of bounds (0 <= %d < %d)", __func__, fam, AF_MAX+1));
202 /* rnh is [fib=0][af=0]. */
203 rnh = (struct rib_head **)V_rt_tables;
204 /* Get the offset to the requested table and fam. */
205 rnh += table * (AF_MAX+1) + fam;
211 rt_tables_get_rnh(int table, int fam)
214 return (*rt_tables_get_rnh_ptr(table, fam));
218 rt_tables_get_gen(int table, int fam)
220 struct rib_head *rnh;
222 rnh = *rt_tables_get_rnh_ptr(table, fam);
223 KASSERT(rnh != NULL, ("%s: NULL rib_head pointer table %d fam %d",
224 __func__, table, fam));
225 return (rnh->rnh_gen);
230 * route initialization must occur before ip6_init2(), which happenas at
237 /* whack the tunable ints into line. */
238 if (rt_numfibs > RT_MAXFIBS)
239 rt_numfibs = RT_MAXFIBS;
244 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, NULL);
247 rtentry_zinit(void *mem, int size, int how)
249 struct rtentry *rt = mem;
251 rt->rt_pksent = counter_u64_alloc(how);
252 if (rt->rt_pksent == NULL)
261 rtentry_zfini(void *mem, int size)
263 struct rtentry *rt = mem;
266 counter_u64_free(rt->rt_pksent);
270 rtentry_ctor(void *mem, int size, void *arg, int how)
272 struct rtentry *rt = mem;
274 bzero(rt, offsetof(struct rtentry, rt_endzero));
275 counter_u64_zero(rt->rt_pksent);
282 rtentry_dtor(void *mem, int size, void *arg)
284 struct rtentry *rt = mem;
290 vnet_route_init(const void *unused __unused)
293 struct rib_head **rnh;
297 V_rt_tables = malloc(rt_numfibs * (AF_MAX+1) *
298 sizeof(struct rib_head *), M_RTABLE, M_WAITOK|M_ZERO);
300 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
301 rtentry_ctor, rtentry_dtor,
302 rtentry_zinit, rtentry_zfini, UMA_ALIGN_PTR, 0);
303 for (dom = domains; dom; dom = dom->dom_next) {
304 if (dom->dom_rtattach == NULL)
307 for (table = 0; table < rt_numfibs; table++) {
308 fam = dom->dom_family;
309 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
312 rnh = rt_tables_get_rnh_ptr(table, fam);
314 panic("%s: rnh NULL", __func__);
315 dom->dom_rtattach((void **)rnh, 0, table);
319 VNET_SYSINIT(vnet_route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH,
324 vnet_route_uninit(const void *unused __unused)
329 struct rib_head **rnh;
331 for (dom = domains; dom; dom = dom->dom_next) {
332 if (dom->dom_rtdetach == NULL)
335 for (table = 0; table < rt_numfibs; table++) {
336 fam = dom->dom_family;
338 if (table != 0 && fam != AF_INET6 && fam != AF_INET)
341 rnh = rt_tables_get_rnh_ptr(table, fam);
343 panic("%s: rnh NULL", __func__);
344 dom->dom_rtdetach((void **)rnh, 0);
348 free(V_rt_tables, M_RTABLE);
349 uma_zdestroy(V_rtzone);
351 VNET_SYSUNINIT(vnet_route_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST,
352 vnet_route_uninit, 0);
356 rt_table_init(int offset, int family, u_int fibnum)
360 rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO);
362 /* TODO: These details should be hidded inside radix.c */
363 /* Init masks tree */
364 rn_inithead_internal(&rh->head, rh->rnh_nodes, offset);
365 rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0);
366 rh->head.rnh_masks = &rh->rmhead;
368 /* Save metadata associated with this routing table. */
369 rh->rib_family = family;
370 rh->rib_fibnum = fibnum;
372 rh->rib_vnet = curvnet;
382 /* Finally, set base callbacks */
383 rh->rnh_addaddr = rn_addroute;
384 rh->rnh_deladdr = rn_delete;
385 rh->rnh_matchaddr = rn_match;
386 rh->rnh_lookup = rn_lookup;
387 rh->rnh_walktree = rn_walktree;
388 rh->rnh_walktree_from = rn_walktree_from;
394 rt_freeentry(struct radix_node *rn, void *arg)
396 struct radix_head * const rnh = arg;
397 struct radix_node *x;
399 x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh);
406 rt_table_destroy(struct rib_head *rh)
409 tmproutes_destroy(rh);
411 rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head);
413 nhops_destroy_rib(rh);
415 /* Assume table is already empty */
416 RIB_LOCK_DESTROY(rh);
421 #ifndef _SYS_SYSPROTO_H_
427 sys_setfib(struct thread *td, struct setfib_args *uap)
429 if (uap->fibnum < 0 || uap->fibnum >= rt_numfibs)
431 td->td_proc->p_fibnum = uap->fibnum;
436 * Packet routing routines.
439 rtalloc_ign_fib(struct route *ro, u_long ignore, u_int fibnum)
443 if ((rt = ro->ro_rt) != NULL) {
444 if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP)
449 ro->ro_rt = rtalloc1_fib(&ro->ro_dst, 1, ignore, fibnum);
451 RT_UNLOCK(ro->ro_rt);
455 * Look up the route that matches the address given
456 * Or, at least try.. Create a cloned route if needed.
458 * The returned route, if any, is locked.
461 rtalloc1(struct sockaddr *dst, int report, u_long ignflags)
464 return (rtalloc1_fib(dst, report, ignflags, RT_DEFAULT_FIB));
468 rtalloc1_fib(struct sockaddr *dst, int report, u_long ignflags,
473 struct radix_node *rn;
474 struct rtentry *newrt;
475 struct rt_addrinfo info;
476 int err = 0, msgtype = RTM_MISS;
478 KASSERT((fibnum < rt_numfibs), ("rtalloc1_fib: bad fibnum"));
479 rh = rt_tables_get_rnh(fibnum, dst->sa_family);
485 * Look up the address in the table for that Address Family
487 if ((ignflags & RTF_RNH_LOCKED) == 0)
493 rn = rh->rnh_matchaddr(dst, &rh->head);
494 if (rn && ((rn->rn_flags & RNF_ROOT) == 0)) {
498 if ((ignflags & RTF_RNH_LOCKED) == 0)
502 } else if ((ignflags & RTF_RNH_LOCKED) == 0)
505 * Either we hit the root or could not find any match,
506 * which basically means: "cannot get there from here".
509 RTSTAT_INC(rts_unreach);
513 * If required, report the failure to the supervising
515 * For a delete, this is not an error. (report == 0)
517 bzero(&info, sizeof(info));
518 info.rti_info[RTAX_DST] = dst;
519 rt_missmsg_fib(msgtype, &info, 0, err, fibnum);
525 * Remove a reference count from an rtentry.
526 * If the count gets low enough, take it out of the routing table
529 rtfree(struct rtentry *rt)
531 struct rib_head *rnh;
533 KASSERT(rt != NULL,("%s: NULL rt", __func__));
534 rnh = rt_tables_get_rnh(rt->rt_fibnum, rt_key(rt)->sa_family);
535 KASSERT(rnh != NULL,("%s: NULL rnh", __func__));
540 * The callers should use RTFREE_LOCKED() or RTFREE(), so
541 * we should come here exactly with the last reference.
544 if (rt->rt_refcnt > 0) {
545 log(LOG_DEBUG, "%s: %p has %d refs\n", __func__, rt, rt->rt_refcnt);
550 * On last reference give the "close method" a chance
551 * to cleanup private state. This also permits (for
552 * IPv4 and IPv6) a chance to decide if the routing table
553 * entry should be purged immediately or at a later time.
554 * When an immediate purge is to happen the close routine
555 * typically calls rtexpunge which clears the RTF_UP flag
556 * on the entry so that the code below reclaims the storage.
558 if (rt->rt_refcnt == 0 && rnh->rnh_close)
559 rnh->rnh_close((struct radix_node *)rt, &rnh->head);
562 * If we are no longer "up" (and ref == 0)
563 * then we can free the resources associated
566 if ((rt->rt_flags & RTF_UP) == 0) {
567 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
570 * the rtentry must have been removed from the routing table
571 * so it is represented in rttrash.. remove that now.
575 if (rt->rt_refcnt < 0) {
576 printf("rtfree: %p not freed (neg refs)\n", rt);
581 * release references on items we hold them on..
582 * e.g other routes and ifaddrs.
585 ifa_free(rt->rt_ifa);
587 * The key is separatly alloc'd so free it (see rt_setgate()).
588 * This also frees the gateway, as they are always malloc'd
593 /* Unreference nexthop */
594 nhop_free(rt->rt_nhop);
597 * and the rtentry itself of course
599 uma_zfree(V_rtzone, rt);
607 * Adds a temporal redirect entry to the routing table.
608 * @fibnum: fib number
609 * @dst: destination to install redirect to
610 * @gateway: gateway to go via
611 * @author: sockaddr of originating router, can be NULL
612 * @ifp: interface to use for the redirected route
613 * @flags: set of flags to add. Allowed: RTF_GATEWAY
614 * @lifetime_sec: time in seconds to expire this redirect.
616 * Retuns 0 on success, errno otherwise.
619 rib_add_redirect(u_int fibnum, struct sockaddr *dst, struct sockaddr *gateway,
620 struct sockaddr *author, struct ifnet *ifp, int flags, int lifetime_sec)
624 struct rt_addrinfo info;
625 struct rt_metrics rti_rmx;
630 if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL)
631 return (EAFNOSUPPORT);
633 /* Verify the allowed flag mask. */
634 KASSERT(((flags & ~(RTF_GATEWAY)) == 0),
635 ("invalid redirect flags: %x", flags));
637 /* Get the best ifa for the given interface and gateway. */
638 if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL)
639 return (ENETUNREACH);
642 bzero(&info, sizeof(info));
643 info.rti_info[RTAX_DST] = dst;
644 info.rti_info[RTAX_GATEWAY] = gateway;
647 info.rti_flags = flags | RTF_HOST | RTF_DYNAMIC;
649 /* Setup route metrics to define expire time. */
650 bzero(&rti_rmx, sizeof(rti_rmx));
651 /* Set expire time as absolute. */
652 rti_rmx.rmx_expire = lifetime_sec + time_second;
653 info.rti_mflags |= RTV_EXPIRE;
654 info.rti_rmx = &rti_rmx;
656 error = rtrequest1_fib(RTM_ADD, &info, &rt, fibnum);
660 /* TODO: add per-fib redirect stats. */
665 flags = rt->rt_flags;
668 RTSTAT_INC(rts_dynamic);
670 /* Send notification of a route addition to userland. */
671 bzero(&info, sizeof(info));
672 info.rti_info[RTAX_DST] = dst;
673 info.rti_info[RTAX_GATEWAY] = gateway;
674 info.rti_info[RTAX_AUTHOR] = author;
675 rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
681 * Routing table ioctl interface.
684 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
688 * If more ioctl commands are added here, make sure the proper
689 * super-user checks are being performed because it is possible for
690 * prison-root to make it this far if raw sockets have been enabled
694 /* Multicast goop, grrr... */
695 return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
702 ifa_ifwithroute(int flags, const struct sockaddr *dst, struct sockaddr *gateway,
709 if ((flags & RTF_GATEWAY) == 0) {
711 * If we are adding a route to an interface,
712 * and the interface is a pt to pt link
713 * we should search for the destination
714 * as our clue to the interface. Otherwise
715 * we can use the local address.
718 if (flags & RTF_HOST)
719 ifa = ifa_ifwithdstaddr(dst, fibnum);
721 ifa = ifa_ifwithaddr(gateway);
724 * If we are adding a route to a remote net
725 * or host, the gateway may still be on the
726 * other end of a pt to pt link.
728 ifa = ifa_ifwithdstaddr(gateway, fibnum);
731 ifa = ifa_ifwithnet(gateway, 0, fibnum);
735 rt = rtalloc1_fib(gateway, 0, flags, fibnum);
739 * dismiss a gateway that is reachable only
740 * through the default router
742 switch (gateway->sa_family) {
744 if (satosin(rt_key(rt))->sin_addr.s_addr == INADDR_ANY)
748 if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(rt))->sin6_addr))
754 if (!not_found && rt->rt_ifa != NULL) {
759 if (not_found || ifa == NULL)
762 if (ifa->ifa_addr->sa_family != dst->sa_family) {
763 struct ifaddr *oifa = ifa;
764 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
773 * Do appropriate manipulations of a routing tree given
774 * all the bits of info needed
777 rtrequest_fib(int req,
778 struct sockaddr *dst,
779 struct sockaddr *gateway,
780 struct sockaddr *netmask,
782 struct rtentry **ret_nrt,
785 struct rt_addrinfo info;
787 if (dst->sa_len == 0)
790 bzero((caddr_t)&info, sizeof(info));
791 info.rti_flags = flags;
792 info.rti_info[RTAX_DST] = dst;
793 info.rti_info[RTAX_GATEWAY] = gateway;
794 info.rti_info[RTAX_NETMASK] = netmask;
795 return rtrequest1_fib(req, &info, ret_nrt, fibnum);
800 * Copy most of @rt data into @info.
802 * If @flags contains NHR_COPY, copies dst,netmask and gw to the
803 * pointers specified by @info structure. Assume such pointers
804 * are zeroed sockaddr-like structures with sa_len field initialized
805 * to reflect size of the provided buffer. if no NHR_COPY is specified,
806 * point dst,netmask and gw @info fields to appropriate @rt values.
808 * if @flags contains NHR_REF, do refcouting on rt_ifp and rt_ifa.
810 * Returns 0 on success.
813 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags)
815 struct rt_metrics *rmx;
816 struct sockaddr *src, *dst;
819 if (flags & NHR_COPY) {
820 /* Copy destination if dst is non-zero */
822 dst = info->rti_info[RTAX_DST];
823 sa_len = src->sa_len;
825 if (src->sa_len > dst->sa_len)
827 memcpy(dst, src, src->sa_len);
828 info->rti_addrs |= RTA_DST;
831 /* Copy mask if set && dst is non-zero */
833 dst = info->rti_info[RTAX_NETMASK];
834 if (src != NULL && dst != NULL) {
837 * Radix stores different value in sa_len,
838 * assume rt_mask() to have the same length
841 if (sa_len > dst->sa_len)
843 memcpy(dst, src, src->sa_len);
844 info->rti_addrs |= RTA_NETMASK;
847 /* Copy gateway is set && dst is non-zero */
848 src = rt->rt_gateway;
849 dst = info->rti_info[RTAX_GATEWAY];
850 if ((rt->rt_flags & RTF_GATEWAY) && src != NULL && dst != NULL){
851 if (src->sa_len > dst->sa_len)
853 memcpy(dst, src, src->sa_len);
854 info->rti_addrs |= RTA_GATEWAY;
857 info->rti_info[RTAX_DST] = rt_key(rt);
858 info->rti_addrs |= RTA_DST;
859 if (rt_mask(rt) != NULL) {
860 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
861 info->rti_addrs |= RTA_NETMASK;
863 if (rt->rt_flags & RTF_GATEWAY) {
864 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
865 info->rti_addrs |= RTA_GATEWAY;
871 info->rti_mflags |= RTV_MTU;
872 rmx->rmx_mtu = rt->rt_mtu;
875 info->rti_flags = rt->rt_flags;
876 info->rti_ifp = rt->rt_ifp;
877 info->rti_ifa = rt->rt_ifa;
878 if (flags & NHR_REF) {
879 if_ref(info->rti_ifp);
880 ifa_ref(info->rti_ifa);
887 * Lookups up route entry for @dst in RIB database for fib @fibnum.
888 * Exports entry data to @info using rt_exportinfo().
890 * If @flags contains NHR_REF, refcouting is performed on rt_ifp and rt_ifa.
891 * All references can be released later by calling rib_free_info().
893 * Returns 0 on success.
894 * Returns ENOENT for lookup failure, ENOMEM for export failure.
897 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags,
898 uint32_t flowid, struct rt_addrinfo *info)
902 struct radix_node *rn;
906 KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum"));
907 rh = rt_tables_get_rnh(fibnum, dst->sa_family);
912 rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head);
913 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
915 /* Ensure route & ifp is UP */
916 if (RT_LINK_IS_UP(rt->rt_ifp)) {
917 flags = (flags & NHR_REF) | NHR_COPY;
918 error = rt_exportinfo(rt, info, flags);
930 * Releases all references acquired by rib_lookup_info() when
931 * called with NHR_REF flags.
934 rib_free_info(struct rt_addrinfo *info)
937 ifa_free(info->rti_ifa);
938 if_rele(info->rti_ifp);
942 * Iterates over all existing fibs in system calling
943 * @setwa_f function prior to traversing each fib.
944 * Calls @wa_f function for each element in current fib.
945 * If af is not AF_UNSPEC, iterates over fibs in particular
949 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f,
952 struct rib_head *rnh;
956 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
957 /* Do we want some specific family? */
958 if (af != AF_UNSPEC) {
959 rnh = rt_tables_get_rnh(fibnum, af);
963 setwa_f(rnh, fibnum, af, arg);
966 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg);
971 for (i = 1; i <= AF_MAX; i++) {
972 rnh = rt_tables_get_rnh(fibnum, i);
976 setwa_f(rnh, fibnum, i, arg);
979 rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg);
987 struct rt_addrinfo info;
988 struct rib_head *rnh;
989 struct rtentry *head;
993 * Conditionally unlinks @rn from radix tree based
994 * on info data passed in @arg.
997 rt_checkdelroute(struct radix_node *rn, void *arg)
999 struct rt_delinfo *di;
1000 struct rt_addrinfo *info;
1004 di = (struct rt_delinfo *)arg;
1005 rt = (struct rtentry *)rn;
1009 info->rti_info[RTAX_DST] = rt_key(rt);
1010 info->rti_info[RTAX_NETMASK] = rt_mask(rt);
1011 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1013 rt = rt_unlinkrte(di->rnh, info, &error);
1015 /* Either not allowed or not matched. Skip entry */
1019 /* Entry was unlinked. Add to the list and return */
1020 rt->rt_chain = di->head;
1027 * Iterates over a routing table specified by @fibnum and @family and
1028 * deletes elements marked by @filter_f.
1029 * @fibnum: rtable id
1030 * @family: AF_ address family
1031 * @filter_f: function returning non-zero value for items to delete
1032 * @arg: data to pass to the @filter_f function
1033 * @report: true if rtsock notification is needed.
1036 rib_walk_del(u_int fibnum, int family, rt_filter_f_t *filter_f, void *arg, bool report)
1038 struct rib_head *rnh;
1039 struct rt_delinfo di;
1042 rnh = rt_tables_get_rnh(fibnum, family);
1046 bzero(&di, sizeof(di));
1047 di.info.rti_filter = filter_f;
1048 di.info.rti_filterdata = arg;
1052 rnh->rnh_walktree(&rnh->head, rt_checkdelroute, &di);
1055 if (di.head == NULL)
1058 /* We might have something to reclaim. */
1059 while (di.head != NULL) {
1061 di.head = rt->rt_chain;
1062 rt->rt_chain = NULL;
1064 /* TODO std rt -> rt_addrinfo export */
1065 di.info.rti_info[RTAX_DST] = rt_key(rt);
1066 di.info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1068 rt_notifydelete(rt, &di.info);
1071 rt_routemsg(RTM_DELETE, rt, rt->rt_ifp, 0, fibnum);
1077 * Iterates over all existing fibs in system and deletes each element
1078 * for which @filter_f function returns non-zero value.
1079 * If @family is not AF_UNSPEC, iterates over fibs in particular
1083 rt_foreach_fib_walk_del(int family, rt_filter_f_t *filter_f, void *arg)
1088 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
1089 /* Do we want some specific family? */
1090 if (family != AF_UNSPEC) {
1098 for (i = start; i <= end; i++) {
1099 if (rt_tables_get_rnh(fibnum, i) == NULL)
1102 rib_walk_del(fibnum, i, filter_f, arg, 0);
1108 * Delete Routes for a Network Interface
1110 * Called for each routing entry via the rnh->rnh_walktree() call above
1111 * to delete all route entries referencing a detaching network interface.
1114 * rt pointer to rtentry
1115 * arg argument passed to rnh->rnh_walktree() - detaching interface
1119 * errno failed - reason indicated
1122 rt_ifdelroute(const struct rtentry *rt, void *arg)
1124 struct ifnet *ifp = arg;
1126 if (rt->rt_ifp != ifp)
1130 * Protect (sorta) against walktree recursion problems
1131 * with cloned routes
1133 if ((rt->rt_flags & RTF_UP) == 0)
1140 * Delete all remaining routes using this interface
1141 * Unfortuneatly the only way to do this is to slog through
1142 * the entire routing table looking for routes which point
1143 * to this interface...oh well...
1146 rt_flushifroutes_af(struct ifnet *ifp, int af)
1148 KASSERT((af >= 1 && af <= AF_MAX), ("%s: af %d not >= 1 and <= %d",
1149 __func__, af, AF_MAX));
1151 rt_foreach_fib_walk_del(af, rt_ifdelroute, ifp);
1155 rt_flushifroutes(struct ifnet *ifp)
1158 rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
1162 * Conditionally unlinks rtentry matching data inside @info from @rnh.
1163 * Returns unlinked, locked and referenced @rtentry on success,
1164 * Returns NULL and sets @perror to:
1165 * ESRCH - if prefix was not found,
1166 * EADDRINUSE - if trying to delete PINNED route without appropriate flag.
1167 * ENOENT - if supplied filter function returned 0 (not matched).
1169 static struct rtentry *
1170 rt_unlinkrte(struct rib_head *rnh, struct rt_addrinfo *info, int *perror)
1172 struct sockaddr *dst, *netmask;
1174 struct radix_node *rn;
1176 dst = info->rti_info[RTAX_DST];
1177 netmask = info->rti_info[RTAX_NETMASK];
1179 rt = (struct rtentry *)rnh->rnh_lookup(dst, netmask, &rnh->head);
1185 if ((info->rti_flags & RTF_PINNED) == 0) {
1186 /* Check if target route can be deleted */
1187 if (rt->rt_flags & RTF_PINNED) {
1188 *perror = EADDRINUSE;
1193 if (info->rti_filter != NULL) {
1194 if (info->rti_filter(rt, info->rti_filterdata) == 0) {
1201 * Filter function requested rte deletion.
1202 * Ease the caller work by filling in remaining info
1203 * from that particular entry.
1205 info->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1209 * Remove the item from the tree and return it.
1210 * Complain if it is not there and do no more processing.
1214 if (rt_mpath_capable(rnh))
1215 rn = rt_mpath_unlink(rnh, info, rt, perror);
1218 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
1222 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
1223 panic ("rtrequest delete");
1228 rt->rt_flags &= ~RTF_UP;
1236 rt_notifydelete(struct rtentry *rt, struct rt_addrinfo *info)
1241 * give the protocol a chance to keep things in sync.
1244 if (ifa != NULL && ifa->ifa_rtrequest != NULL)
1245 ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1248 * One more rtentry floating around that is not
1249 * linked to the routing table. rttrash will be decremented
1250 * when RTFREE(rt) is eventually called.
1257 * These (questionable) definitions of apparent local variables apply
1258 * to the next two functions. XXXXXX!!!
1260 #define dst info->rti_info[RTAX_DST]
1261 #define gateway info->rti_info[RTAX_GATEWAY]
1262 #define netmask info->rti_info[RTAX_NETMASK]
1263 #define ifaaddr info->rti_info[RTAX_IFA]
1264 #define ifpaddr info->rti_info[RTAX_IFP]
1265 #define flags info->rti_flags
1268 * Look up rt_addrinfo for a specific fib. Note that if rti_ifa is defined,
1269 * it will be referenced so the caller must free it.
1271 * Assume basic consistency checks are executed by callers:
1272 * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well.
1275 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
1277 struct epoch_tracker et;
1281 * ifp may be specified by sockaddr_dl
1282 * when protocol address is ambiguous.
1285 needref = (info->rti_ifa == NULL);
1286 NET_EPOCH_ENTER(et);
1288 /* If we have interface specified by the ifindex in the address, use it */
1289 if (info->rti_ifp == NULL && ifpaddr != NULL &&
1290 ifpaddr->sa_family == AF_LINK) {
1291 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)ifpaddr;
1292 if (sdl->sdl_index != 0)
1293 info->rti_ifp = ifnet_byindex(sdl->sdl_index);
1296 * If we have source address specified, try to find it
1297 * TODO: avoid enumerating all ifas on all interfaces.
1299 if (info->rti_ifa == NULL && ifaaddr != NULL)
1300 info->rti_ifa = ifa_ifwithaddr(ifaaddr);
1301 if (info->rti_ifa == NULL) {
1302 struct sockaddr *sa;
1305 * Most common use case for the userland-supplied routes.
1307 * Choose sockaddr to select ifa.
1308 * -- if ifp is set --
1309 * Order of preference:
1311 * 2) gateway address
1312 * Note: for interface routes link-level gateway address
1313 * is specified to indicate the interface index without
1314 * specifying RTF_GATEWAY. In this case, ignore gateway
1315 * Note: gateway AF may be different from dst AF. In this case,
1317 * 3) final destination.
1318 * 4) if all of these fails, try to get at least link-level ifa.
1320 * try to lookup gateway or dst in the routing table to get ifa
1322 if (info->rti_info[RTAX_IFA] != NULL)
1323 sa = info->rti_info[RTAX_IFA];
1324 else if ((info->rti_flags & RTF_GATEWAY) != 0 &&
1325 gateway->sa_family == dst->sa_family)
1329 if (info->rti_ifp != NULL) {
1330 info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
1332 if (info->rti_ifa == NULL && gateway != NULL)
1333 info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp);
1334 } else if (dst != NULL && gateway != NULL)
1335 info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
1337 else if (sa != NULL)
1338 info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
1341 if (needref && info->rti_ifa != NULL) {
1342 if (info->rti_ifp == NULL)
1343 info->rti_ifp = info->rti_ifa->ifa_ifp;
1344 ifa_ref(info->rti_ifa);
1346 error = ENETUNREACH;
1352 if_updatemtu_cb(struct radix_node *rn, void *arg)
1355 struct if_mtuinfo *ifmtu;
1357 rt = (struct rtentry *)rn;
1358 ifmtu = (struct if_mtuinfo *)arg;
1360 if (rt->rt_ifp != ifmtu->ifp)
1363 if (rt->rt_mtu >= ifmtu->mtu) {
1364 /* We have to decrease mtu regardless of flags */
1365 rt->rt_mtu = ifmtu->mtu;
1370 * New MTU is bigger. Check if are allowed to alter it
1372 if ((rt->rt_flags & (RTF_FIXEDMTU | RTF_GATEWAY | RTF_HOST)) != 0) {
1375 * Skip routes with user-supplied MTU and
1376 * non-interface routes
1381 /* We are safe to update route MTU */
1382 rt->rt_mtu = ifmtu->mtu;
1388 rt_updatemtu(struct ifnet *ifp)
1390 struct if_mtuinfo ifmtu;
1391 struct rib_head *rnh;
1397 * Try to update rt_mtu for all routes using this interface
1398 * Unfortunately the only way to do this is to traverse all
1399 * routing tables in all fibs/domains.
1401 for (i = 1; i <= AF_MAX; i++) {
1402 ifmtu.mtu = if_getmtu_family(ifp, i);
1403 for (j = 0; j < rt_numfibs; j++) {
1404 rnh = rt_tables_get_rnh(j, i);
1408 rnh->rnh_walktree(&rnh->head, if_updatemtu_cb, &ifmtu);
1410 nhops_update_ifmtu(rnh, ifp, ifmtu.mtu);
1417 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
1418 int rt_print(char *buf, int buflen, struct rtentry *rt);
1421 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
1425 switch (s->sa_family) {
1427 paddr = &((struct sockaddr_in *)s)->sin_addr;
1430 paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
1437 if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
1440 return (strlen(buf));
1444 rt_print(char *buf, int buflen, struct rtentry *rt)
1446 struct sockaddr *addr, *mask;
1452 i = p_sockaddr(buf, buflen, addr);
1453 if (!(rt->rt_flags & RTF_HOST)) {
1455 i += p_sockaddr(buf + i, buflen - i, mask);
1458 if (rt->rt_flags & RTF_GATEWAY) {
1460 i += p_sockaddr(buf + i, buflen - i, rt->rt_gateway);
1469 * Deletes key for single-path routes, unlinks rtentry with
1470 * gateway specified in @info from multi-path routes.
1472 * Returnes unlinked entry. In case of failure, returns NULL
1473 * and sets @perror to ESRCH.
1475 static struct radix_node *
1476 rt_mpath_unlink(struct rib_head *rnh, struct rt_addrinfo *info,
1477 struct rtentry *rto, int *perror)
1480 * if we got multipath routes, we require users to specify
1481 * a matching RTAX_GATEWAY.
1483 struct rtentry *rt; // *rto = NULL;
1484 struct radix_node *rn;
1485 struct sockaddr *gw;
1487 gw = info->rti_info[RTAX_GATEWAY];
1488 rt = rt_mpath_matchgate(rto, gw);
1495 * this is the first entry in the chain
1498 rn = rn_mpath_next((struct radix_node *)rt);
1500 * there is another entry, now it's active
1505 rto->rt_flags |= RTF_UP;
1507 } else if (rt->rt_flags & RTF_GATEWAY) {
1509 * For gateway routes, we need to
1510 * make sure that we we are deleting
1511 * the correct gateway.
1512 * rt_mpath_matchgate() does not
1513 * check the case when there is only
1514 * one route in the chain.
1517 (rt->rt_gateway->sa_len != gw->sa_len ||
1518 memcmp(rt->rt_gateway, gw, gw->sa_len))) {
1525 * use the normal delete code to remove
1528 rn = rnh->rnh_deladdr(dst, netmask, &rnh->head);
1534 * if the entry is 2nd and on up
1536 if (rt_mpath_deldup(rto, rt) == 0)
1537 panic ("rtrequest1: rt_mpath_deldup");
1539 rn = (struct radix_node *)rt;
1552 rtrequest1_fib(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt,
1555 struct epoch_tracker et;
1556 const struct sockaddr *dst;
1557 struct rib_head *rnh;
1560 KASSERT((fibnum < rt_numfibs), ("rtrequest1_fib: bad fibnum"));
1561 KASSERT((info->rti_flags & RTF_RNH_LOCKED) == 0, ("rtrequest1_fib: locked"));
1563 dst = info->rti_info[RTAX_DST];
1565 switch (dst->sa_family) {
1568 /* We support multiple FIBs. */
1571 fibnum = RT_DEFAULT_FIB;
1576 * Find the correct routing tree to use for this Address Family
1578 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
1580 return (EAFNOSUPPORT);
1583 * If we are adding a host route then we don't want to put
1584 * a netmask in the tree, nor do we want to clone it.
1586 if (info->rti_flags & RTF_HOST)
1587 info->rti_info[RTAX_NETMASK] = NULL;
1592 error = del_route(rnh, info, ret_nrt);
1596 * resolve was only used for route cloning
1601 error = add_route(rnh, info, ret_nrt);
1604 NET_EPOCH_ENTER(et);
1606 error = change_route(rnh, info, ret_nrt);
1618 add_route(struct rib_head *rnh, struct rt_addrinfo *info,
1619 struct rtentry **ret_nrt)
1621 struct sockaddr *dst, *ndst, *gateway, *netmask;
1622 struct rtentry *rt, *rt_old;
1623 struct nhop_object *nh;
1624 struct radix_node *rn;
1627 struct epoch_tracker et;
1629 dst = info->rti_info[RTAX_DST];
1630 gateway = info->rti_info[RTAX_GATEWAY];
1631 netmask = info->rti_info[RTAX_NETMASK];
1632 flags = info->rti_flags;
1634 if ((flags & RTF_GATEWAY) && !gateway)
1636 if (dst && gateway && (dst->sa_family != gateway->sa_family) &&
1637 (gateway->sa_family != AF_UNSPEC) && (gateway->sa_family != AF_LINK))
1640 if (info->rti_ifa == NULL) {
1641 error = rt_getifa_fib(info, rnh->rib_fibnum);
1645 ifa_ref(info->rti_ifa);
1648 NET_EPOCH_ENTER(et);
1649 error = nhop_create_from_info(rnh, info, &nh);
1652 ifa_free(info->rti_ifa);
1656 rt = uma_zalloc(V_rtzone, M_NOWAIT);
1658 ifa_free(info->rti_ifa);
1662 rt->rt_flags = RTF_UP | flags;
1663 rt->rt_fibnum = rnh->rib_fibnum;
1666 * Add the gateway. Possibly re-malloc-ing the storage for it.
1668 if ((error = rt_setgate(rt, dst, gateway)) != 0) {
1669 ifa_free(info->rti_ifa);
1671 uma_zfree(V_rtzone, rt);
1676 * point to the (possibly newly malloc'd) dest address.
1678 ndst = (struct sockaddr *)rt_key(rt);
1681 * make sure it contains the value we want (masked if needed).
1684 rt_maskedcopy(dst, ndst, netmask);
1686 bcopy(dst, ndst, dst->sa_len);
1689 * We use the ifa reference returned by rt_getifa_fib().
1690 * This moved from below so that rnh->rnh_addaddr() can
1691 * examine the ifa and ifa->ifa_ifp if it so desires.
1693 ifa = info->rti_ifa;
1695 rt->rt_ifp = ifa->ifa_ifp;
1698 rt_setmetrics(info, rt);
1703 /* do not permit exactly the same dst/mask/gw pair */
1704 if (rt_mpath_capable(rnh) &&
1705 rt_mpath_conflict(rnh, rt, netmask)) {
1708 ifa_free(rt->rt_ifa);
1711 uma_zfree(V_rtzone, rt);
1716 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
1717 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head, rt->rt_nodes);
1719 if (rn != NULL && rt->rt_expire > 0)
1720 tmproutes_update(rnh, rt);
1723 if (rn == NULL && (info->rti_flags & RTF_PINNED) != 0) {
1726 * Force removal and re-try addition
1727 * TODO: better multipath&pinned support
1729 struct sockaddr *info_dst = info->rti_info[RTAX_DST];
1730 info->rti_info[RTAX_DST] = ndst;
1731 /* Do not delete existing PINNED(interface) routes */
1732 info->rti_flags &= ~RTF_PINNED;
1733 rt_old = rt_unlinkrte(rnh, info, &error);
1734 info->rti_flags |= RTF_PINNED;
1735 info->rti_info[RTAX_DST] = info_dst;
1737 rn = rnh->rnh_addaddr(ndst, netmask, &rnh->head,
1746 * If it still failed to go into the tree,
1747 * then un-make it (this should be a function)
1750 ifa_free(rt->rt_ifa);
1753 uma_zfree(V_rtzone, rt);
1757 if (rt_old != NULL) {
1758 rt_notifydelete(rt_old, info);
1763 * If this protocol has something to add to this then
1764 * allow it to do that as well.
1766 if (ifa->ifa_rtrequest)
1767 ifa->ifa_rtrequest(RTM_ADD, rt, info);
1770 * actually return a resultant rtentry and
1771 * give the caller a single reference.
1777 rnh->rnh_gen++; /* Routing table updated */
1784 del_route(struct rib_head *rnh, struct rt_addrinfo *info,
1785 struct rtentry **ret_nrt)
1787 struct sockaddr *dst, *netmask;
1788 struct sockaddr_storage mdst;
1792 dst = info->rti_info[RTAX_DST];
1793 netmask = info->rti_info[RTAX_NETMASK];
1796 if (dst->sa_len > sizeof(mdst))
1798 rt_maskedcopy(dst, (struct sockaddr *)&mdst, netmask);
1799 dst = (struct sockaddr *)&mdst;
1803 rt = rt_unlinkrte(rnh, info, &error);
1808 rt_notifydelete(rt, info);
1811 * If the caller wants it, then it can have it,
1812 * but it's up to it to free the rtentry as we won't be
1825 change_route(struct rib_head *rnh, struct rt_addrinfo *info,
1826 struct rtentry **ret_nrt)
1828 struct rtentry *rt = NULL;
1832 struct nhop_object *nh;
1833 struct if_mtuinfo ifmtu;
1835 RIB_WLOCK_ASSERT(rnh);
1837 rt = (struct rtentry *)rnh->rnh_lookup(info->rti_info[RTAX_DST],
1838 info->rti_info[RTAX_NETMASK], &rnh->head);
1845 * If we got multipath routes,
1846 * we require users to specify a matching RTAX_GATEWAY.
1848 if (rt_mpath_capable(rnh)) {
1849 rt = rt_mpath_matchgate(rt, info->rti_info[RTAX_GATEWAY]);
1858 rt_setmetrics(info, rt);
1861 * New gateway could require new ifaddr, ifp;
1862 * flags may also be different; ifp may be specified
1863 * by ll sockaddr when protocol address is ambiguous
1865 if (((rt->rt_flags & RTF_GATEWAY) &&
1866 info->rti_info[RTAX_GATEWAY] != NULL) ||
1867 info->rti_info[RTAX_IFP] != NULL ||
1868 (info->rti_info[RTAX_IFA] != NULL &&
1869 !sa_equal(info->rti_info[RTAX_IFA], rt->rt_ifa->ifa_addr))) {
1871 * XXX: Temporarily set RTF_RNH_LOCKED flag in the rti_flags
1872 * to avoid rlock in the ifa_ifwithroute().
1874 info->rti_flags |= RTF_RNH_LOCKED;
1875 error = rt_getifa_fib(info, rnh->rib_fibnum);
1876 info->rti_flags &= ~RTF_RNH_LOCKED;
1877 if (info->rti_ifa != NULL)
1884 error = nhop_create_from_nhop(rnh, rt->rt_nhop, info, &nh);
1888 /* Check if outgoing interface has changed */
1889 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa &&
1890 rt->rt_ifa != NULL) {
1891 if (rt->rt_ifa->ifa_rtrequest != NULL)
1892 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, info);
1893 ifa_free(rt->rt_ifa);
1896 /* Update gateway address */
1897 if (info->rti_info[RTAX_GATEWAY] != NULL) {
1898 error = rt_setgate(rt, rt_key(rt), info->rti_info[RTAX_GATEWAY]);
1902 rt->rt_flags &= ~RTF_GATEWAY;
1903 rt->rt_flags |= (RTF_GATEWAY & info->rti_flags);
1906 if (info->rti_ifa != NULL && info->rti_ifa != rt->rt_ifa) {
1907 ifa_ref(info->rti_ifa);
1908 rt->rt_ifa = info->rti_ifa;
1909 rt->rt_ifp = info->rti_ifp;
1911 /* Allow some flags to be toggled on change. */
1912 rt->rt_flags &= ~RTF_FMASK;
1913 rt->rt_flags |= info->rti_flags & RTF_FMASK;
1915 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest != NULL)
1916 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
1918 /* Alter route MTU if necessary */
1919 if (rt->rt_ifp != NULL) {
1920 family = info->rti_info[RTAX_DST]->sa_family;
1921 mtu = if_getmtu_family(rt->rt_ifp, family);
1922 /* Set default MTU */
1923 if (rt->rt_mtu == 0)
1925 if (rt->rt_mtu != mtu) {
1926 /* Check if we really need to update */
1927 ifmtu.ifp = rt->rt_ifp;
1929 if_updatemtu_cb(rt->rt_nodes, &ifmtu);
1933 /* Update nexthop */
1934 nhop_free(rt->rt_nhop);
1939 * This route change may have modified the route's gateway. In that
1940 * case, any inpcbs that have cached this route need to invalidate their
1953 if (free_ifa != 0) {
1954 ifa_free(info->rti_ifa);
1955 info->rti_ifa = NULL;
1961 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt)
1964 if (info->rti_mflags & RTV_MTU) {
1965 if (info->rti_rmx->rmx_mtu != 0) {
1968 * MTU was explicitly provided by user.
1971 rt->rt_flags |= RTF_FIXEDMTU;
1975 * User explicitly sets MTU to 0.
1976 * Assume rollback to default.
1978 rt->rt_flags &= ~RTF_FIXEDMTU;
1980 rt->rt_mtu = info->rti_rmx->rmx_mtu;
1982 if (info->rti_mflags & RTV_WEIGHT)
1983 rt->rt_weight = info->rti_rmx->rmx_weight;
1984 /* Kernel -> userland timebase conversion. */
1985 if (info->rti_mflags & RTV_EXPIRE)
1986 rt->rt_expire = info->rti_rmx->rmx_expire ?
1987 info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
1991 rt_setgate(struct rtentry *rt, struct sockaddr *dst, struct sockaddr *gate)
1993 /* XXX dst may be overwritten, can we move this to below */
1994 int dlen = SA_SIZE(dst), glen = SA_SIZE(gate);
1997 * Prepare to store the gateway in rt->rt_gateway.
1998 * Both dst and gateway are stored one after the other in the same
1999 * malloc'd chunk. If we have room, we can reuse the old buffer,
2000 * rt_gateway already points to the right place.
2001 * Otherwise, malloc a new block and update the 'dst' address.
2003 if (rt->rt_gateway == NULL || glen > SA_SIZE(rt->rt_gateway)) {
2006 R_Malloc(new, caddr_t, dlen + glen);
2010 * XXX note, we copy from *dst and not *rt_key(rt) because
2011 * rt_setgate() can be called to initialize a newly
2012 * allocated route entry, in which case rt_key(rt) == NULL
2013 * (and also rt->rt_gateway == NULL).
2014 * Free()/free() handle a NULL argument just fine.
2016 bcopy(dst, new, dlen);
2017 R_Free(rt_key(rt)); /* free old block, if any */
2018 rt_key(rt) = (struct sockaddr *)new;
2019 rt->rt_gateway = (struct sockaddr *)(new + dlen);
2023 * Copy the new gateway value into the memory chunk.
2025 bcopy(gate, rt->rt_gateway, glen);
2031 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
2033 u_char *cp1 = (u_char *)src;
2034 u_char *cp2 = (u_char *)dst;
2035 u_char *cp3 = (u_char *)netmask;
2036 u_char *cplim = cp2 + *cp3;
2037 u_char *cplim2 = cp2 + *cp1;
2039 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
2044 *cp2++ = *cp1++ & *cp3++;
2046 bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
2050 * Set up a routing table entry, normally
2053 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
2055 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
2058 struct sockaddr *dst;
2059 struct sockaddr *netmask;
2060 struct rtentry *rt = NULL;
2061 struct rt_addrinfo info;
2063 int startfib, endfib;
2064 char tempbuf[_SOCKADDR_TMPSIZE];
2067 struct sockaddr_dl *sdl = NULL;
2068 struct rib_head *rnh;
2070 if (flags & RTF_HOST) {
2071 dst = ifa->ifa_dstaddr;
2074 dst = ifa->ifa_addr;
2075 netmask = ifa->ifa_netmask;
2077 if (dst->sa_len == 0)
2079 switch (dst->sa_family) {
2082 /* We support multiple FIBs. */
2085 fibnum = RT_DEFAULT_FIB;
2088 if (fibnum == RT_ALL_FIBS) {
2089 if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD)
2090 startfib = endfib = ifa->ifa_ifp->if_fib;
2093 endfib = rt_numfibs - 1;
2096 KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
2102 * If it's a delete, check that if it exists,
2103 * it's on the correct interface or we might scrub
2104 * a route to another ifa which would
2105 * be confusing at best and possibly worse.
2107 if (cmd == RTM_DELETE) {
2109 * It's a delete, so it should already exist..
2110 * If it's a net, mask off the host bits
2111 * (Assuming we have a mask)
2112 * XXX this is kinda inet specific..
2114 if (netmask != NULL) {
2115 rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
2116 dst = (struct sockaddr *)tempbuf;
2118 } else if (cmd == RTM_ADD) {
2119 sdl = (struct sockaddr_dl *)tempbuf;
2120 bzero(sdl, sizeof(struct sockaddr_dl));
2121 sdl->sdl_family = AF_LINK;
2122 sdl->sdl_len = sizeof(struct sockaddr_dl);
2123 sdl->sdl_type = ifa->ifa_ifp->if_type;
2124 sdl->sdl_index = ifa->ifa_ifp->if_index;
2127 * Now go through all the requested tables (fibs) and do the
2128 * requested action. Realistically, this will either be fib 0
2129 * for protocols that don't do multiple tables or all the
2130 * tables for those that do.
2132 for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
2133 if (cmd == RTM_DELETE) {
2134 struct radix_node *rn;
2136 * Look up an rtentry that is in the routing tree and
2137 * contains the correct info.
2139 rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
2141 /* this table doesn't exist but others might */
2144 rn = rnh->rnh_lookup(dst, netmask, &rnh->head);
2146 if (rt_mpath_capable(rnh)) {
2153 * for interface route the
2154 * rt->rt_gateway is sockaddr_intf
2155 * for cloning ARP entries, so
2156 * rt_mpath_matchgate must use the
2159 rt = rt_mpath_matchgate(rt,
2166 error = (rn == NULL ||
2167 (rn->rn_flags & RNF_ROOT) ||
2168 RNTORT(rn)->rt_ifa != ifa);
2171 /* this is only an error if bad on ALL tables */
2176 * Do the actual request
2178 bzero((caddr_t)&info, sizeof(info));
2180 info.rti_flags = flags |
2181 (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
2182 info.rti_info[RTAX_DST] = dst;
2184 * doing this for compatibility reasons
2187 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)sdl;
2189 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
2190 info.rti_info[RTAX_NETMASK] = netmask;
2191 error = rtrequest1_fib(cmd, &info, &rt, fibnum);
2192 if (error == 0 && rt != NULL) {
2194 * notify any listening routing agents of the change
2199 * in case address alias finds the first address
2200 * e.g. ifconfig bge0 192.0.2.246/24
2201 * e.g. ifconfig bge0 192.0.2.247/24
2202 * the address set in the route is 192.0.2.246
2203 * so we need to replace it with 192.0.2.247
2205 if (memcmp(rt->rt_ifa->ifa_addr,
2206 ifa->ifa_addr, ifa->ifa_addr->sa_len)) {
2207 ifa_free(rt->rt_ifa);
2209 rt->rt_ifp = ifa->ifa_ifp;
2215 rt_newaddrmsg_fib(cmd, ifa, rt, fibnum);
2218 if (cmd == RTM_DELETE) {
2220 * If we are deleting, and we found an entry,
2221 * then it's been removed from the tree..
2222 * now throw it away.
2226 if (cmd == RTM_ADD) {
2228 * We just wanted to add it..
2229 * we don't actually need a reference.
2240 if (cmd == RTM_DELETE) {
2244 /* we only give an error if it wasn't in any table */
2245 error = ((flags & RTF_HOST) ?
2246 EHOSTUNREACH : ENETUNREACH);
2250 /* return an error if any of them failed */
2258 * Set up a routing table entry, normally
2262 rtinit(struct ifaddr *ifa, int cmd, int flags)
2264 struct sockaddr *dst;
2265 int fib = RT_DEFAULT_FIB;
2267 if (flags & RTF_HOST) {
2268 dst = ifa->ifa_dstaddr;
2270 dst = ifa->ifa_addr;
2273 switch (dst->sa_family) {
2276 /* We do support multiple FIBs. */
2280 return (rtinit1(ifa, cmd, flags, fib));
2284 * Announce interface address arrival/withdraw
2285 * Returns 0 on success.
2288 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
2291 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2292 ("unexpected cmd %d", cmd));
2293 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2294 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2296 EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd);
2297 return (rtsock_addrmsg(cmd, ifa, fibnum));
2301 * Announce kernel-originated route addition/removal to rtsock based on @rt data.
2303 * @rt: valid rtentry
2304 * @ifp: target route interface
2305 * @fibnum: fib id or RT_ALL_FIBS
2307 * Returns 0 on success.
2310 rt_routemsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int rti_addrs,
2314 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2315 ("unexpected cmd %d", cmd));
2317 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2318 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2320 KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
2322 return (rtsock_routemsg(cmd, rt, ifp, 0, fibnum));
2326 * Announce kernel-originated route addition/removal to rtsock based on @rt data.
2328 * @info: addrinfo structure with valid data.
2329 * @fibnum: fib id or RT_ALL_FIBS
2331 * Returns 0 on success.
2334 rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
2337 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
2338 ("unexpected cmd %d", cmd));
2340 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2341 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2343 KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__));
2345 return (rtsock_routemsg_info(cmd, info, fibnum));
2350 * This is called to generate messages from the routing socket
2351 * indicating a network interface has had addresses associated with it.
2354 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, struct rtentry *rt, int fibnum)
2357 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
2358 ("unexpected cmd %u", cmd));
2359 KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
2360 ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
2362 if (cmd == RTM_ADD) {
2363 rt_addrmsg(cmd, ifa, fibnum);
2365 rt_routemsg(cmd, rt, ifa->ifa_ifp, 0, fibnum);
2368 rt_routemsg(cmd, rt, ifa->ifa_ifp, 0, fibnum);
2369 rt_addrmsg(cmd, ifa, fibnum);