xref: /freebsd/sys/net/route.c (revision bc6f027a39c4c7c2758de8f9d185653cad757784)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  *
31  *	@(#)route.c	8.3.1.1 (Berkeley) 2/23/95
32  * $FreeBSD$
33  */
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  ***********************************************************************/
38 
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_mrouting.h"
42 #include "opt_mpath.h"
43 #include "opt_route.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/sysproto.h>
53 #include <sys/proc.h>
54 #include <sys/domain.h>
55 #include <sys/eventhandler.h>
56 #include <sys/kernel.h>
57 #include <sys/lock.h>
58 #include <sys/rmlock.h>
59 
60 #include <net/if.h>
61 #include <net/if_var.h>
62 #include <net/if_dl.h>
63 #include <net/route.h>
64 #include <net/route/route_ctl.h>
65 #include <net/route/route_var.h>
66 #include <net/route/nhop.h>
67 #include <net/route/shared.h>
68 #include <net/vnet.h>
69 
70 #ifdef RADIX_MPATH
71 #include <net/radix_mpath.h>
72 #endif
73 
74 #include <netinet/in.h>
75 #include <netinet/ip_mroute.h>
76 
77 /*
78  * By default add routes to all fibs for new interfaces.
79  * Once this is set to 0 then only allocate routes on interface
80  * changes for the FIB of the caller when adding a new set of addresses
81  * to an interface.  XXX this is a shotgun aproach to a problem that needs
82  * a more fine grained solution.. that will come.
83  * XXX also has the problems getting the FIB from curthread which will not
84  * always work given the fib can be overridden and prefixes can be added
85  * from the network stack context.
86  */
87 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 1;
88 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
89     &VNET_NAME(rt_add_addr_allfibs), 0, "");
90 
91 VNET_PCPUSTAT_DEFINE(struct rtstat, rtstat);
92 
93 VNET_PCPUSTAT_SYSINIT(rtstat);
94 #ifdef VIMAGE
95 VNET_PCPUSTAT_SYSUNINIT(rtstat);
96 #endif
97 
98 EVENTHANDLER_LIST_DEFINE(rt_addrmsg);
99 
100 static int rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *,
101     void *arg);
102 static int rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info,
103     int flags);
104 
105 /*
106  * route initialization must occur before ip6_init2(), which happenas at
107  * SI_ORDER_MIDDLE.
108  */
109 static void
110 route_init(void)
111 {
112 
113 	nhops_init();
114 }
115 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, NULL);
116 
117 struct rib_head *
118 rt_table_init(int offset, int family, u_int fibnum)
119 {
120 	struct rib_head *rh;
121 
122 	rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO);
123 
124 	/* TODO: These details should be hidded inside radix.c */
125 	/* Init masks tree */
126 	rn_inithead_internal(&rh->head, rh->rnh_nodes, offset);
127 	rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0);
128 	rh->head.rnh_masks = &rh->rmhead;
129 
130 	/* Save metadata associated with this routing table. */
131 	rh->rib_family = family;
132 	rh->rib_fibnum = fibnum;
133 #ifdef VIMAGE
134 	rh->rib_vnet = curvnet;
135 #endif
136 
137 	tmproutes_init(rh);
138 
139 	/* Init locks */
140 	RIB_LOCK_INIT(rh);
141 
142 	nhops_init_rib(rh);
143 
144 	/* Init subscription system */
145 	rib_init_subscriptions(rh);
146 
147 	/* Finally, set base callbacks */
148 	rh->rnh_addaddr = rn_addroute;
149 	rh->rnh_deladdr = rn_delete;
150 	rh->rnh_matchaddr = rn_match;
151 	rh->rnh_lookup = rn_lookup;
152 	rh->rnh_walktree = rn_walktree;
153 	rh->rnh_walktree_from = rn_walktree_from;
154 
155 	return (rh);
156 }
157 
158 static int
159 rt_freeentry(struct radix_node *rn, void *arg)
160 {
161 	struct radix_head * const rnh = arg;
162 	struct radix_node *x;
163 
164 	x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh);
165 	if (x != NULL)
166 		R_Free(x);
167 	return (0);
168 }
169 
170 void
171 rt_table_destroy(struct rib_head *rh)
172 {
173 
174 	tmproutes_destroy(rh);
175 
176 	rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head);
177 
178 	nhops_destroy_rib(rh);
179 
180 	rib_destroy_subscriptions(rh);
181 
182 	/* Assume table is already empty */
183 	RIB_LOCK_DESTROY(rh);
184 	free(rh, M_RTABLE);
185 }
186 
187 /*
188  * Adds a temporal redirect entry to the routing table.
189  * @fibnum: fib number
190  * @dst: destination to install redirect to
191  * @gateway: gateway to go via
192  * @author: sockaddr of originating router, can be NULL
193  * @ifp: interface to use for the redirected route
194  * @flags: set of flags to add. Allowed: RTF_GATEWAY
195  * @lifetime_sec: time in seconds to expire this redirect.
196  *
197  * Retuns 0 on success, errno otherwise.
198  */
199 int
200 rib_add_redirect(u_int fibnum, struct sockaddr *dst, struct sockaddr *gateway,
201     struct sockaddr *author, struct ifnet *ifp, int flags, int lifetime_sec)
202 {
203 	struct rib_cmd_info rc;
204 	int error;
205 	struct rt_addrinfo info;
206 	struct rt_metrics rti_rmx;
207 	struct ifaddr *ifa;
208 
209 	NET_EPOCH_ASSERT();
210 
211 	if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL)
212 		return (EAFNOSUPPORT);
213 
214 	/* Verify the allowed flag mask. */
215 	KASSERT(((flags & ~(RTF_GATEWAY)) == 0),
216 	    ("invalid redirect flags: %x", flags));
217 
218 	/* Get the best ifa for the given interface and gateway. */
219 	if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL)
220 		return (ENETUNREACH);
221 	ifa_ref(ifa);
222 
223 	bzero(&info, sizeof(info));
224 	info.rti_info[RTAX_DST] = dst;
225 	info.rti_info[RTAX_GATEWAY] = gateway;
226 	info.rti_ifa = ifa;
227 	info.rti_ifp = ifp;
228 	info.rti_flags = flags | RTF_HOST | RTF_DYNAMIC;
229 
230 	/* Setup route metrics to define expire time. */
231 	bzero(&rti_rmx, sizeof(rti_rmx));
232 	/* Set expire time as absolute. */
233 	rti_rmx.rmx_expire = lifetime_sec + time_second;
234 	info.rti_mflags |= RTV_EXPIRE;
235 	info.rti_rmx = &rti_rmx;
236 
237 	error = rib_action(fibnum, RTM_ADD, &info, &rc);
238 	ifa_free(ifa);
239 
240 	if (error != 0) {
241 		/* TODO: add per-fib redirect stats. */
242 		return (error);
243 	}
244 
245 	RT_LOCK(rc.rc_rt);
246 	flags = rc.rc_rt->rte_flags;
247 	RT_UNLOCK(rc.rc_rt);
248 
249 	RTSTAT_INC(rts_dynamic);
250 
251 	/* Send notification of a route addition to userland. */
252 	bzero(&info, sizeof(info));
253 	info.rti_info[RTAX_DST] = dst;
254 	info.rti_info[RTAX_GATEWAY] = gateway;
255 	info.rti_info[RTAX_AUTHOR] = author;
256 	rt_missmsg_fib(RTM_REDIRECT, &info, flags, error, fibnum);
257 
258 	return (0);
259 }
260 
261 /*
262  * Routing table ioctl interface.
263  */
264 int
265 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
266 {
267 
268 	/*
269 	 * If more ioctl commands are added here, make sure the proper
270 	 * super-user checks are being performed because it is possible for
271 	 * prison-root to make it this far if raw sockets have been enabled
272 	 * in jails.
273 	 */
274 #ifdef INET
275 	/* Multicast goop, grrr... */
276 	return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
277 #else /* INET */
278 	return ENXIO;
279 #endif /* INET */
280 }
281 
282 struct ifaddr *
283 ifa_ifwithroute(int flags, const struct sockaddr *dst,
284     const struct sockaddr *gateway, u_int fibnum)
285 {
286 	struct ifaddr *ifa;
287 
288 	NET_EPOCH_ASSERT();
289 	if ((flags & RTF_GATEWAY) == 0) {
290 		/*
291 		 * If we are adding a route to an interface,
292 		 * and the interface is a pt to pt link
293 		 * we should search for the destination
294 		 * as our clue to the interface.  Otherwise
295 		 * we can use the local address.
296 		 */
297 		ifa = NULL;
298 		if (flags & RTF_HOST)
299 			ifa = ifa_ifwithdstaddr(dst, fibnum);
300 		if (ifa == NULL)
301 			ifa = ifa_ifwithaddr(gateway);
302 	} else {
303 		/*
304 		 * If we are adding a route to a remote net
305 		 * or host, the gateway may still be on the
306 		 * other end of a pt to pt link.
307 		 */
308 		ifa = ifa_ifwithdstaddr(gateway, fibnum);
309 	}
310 	if (ifa == NULL)
311 		ifa = ifa_ifwithnet(gateway, 0, fibnum);
312 	if (ifa == NULL) {
313 		struct nhop_object *nh;
314 
315 		nh = rib_lookup(fibnum, gateway, NHR_NONE, 0);
316 
317 		/*
318 		 * dismiss a gateway that is reachable only
319 		 * through the default router
320 		 */
321 		if ((nh == NULL) || (nh->nh_flags & NHF_DEFAULT))
322 			return (NULL);
323 		ifa = nh->nh_ifa;
324 	}
325 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
326 		struct ifaddr *oifa = ifa;
327 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
328 		if (ifa == NULL)
329 			ifa = oifa;
330 	}
331 
332 	return (ifa);
333 }
334 
335 
336 /*
337  * Copy most of @rt data into @info.
338  *
339  * If @flags contains NHR_COPY, copies dst,netmask and gw to the
340  * pointers specified by @info structure. Assume such pointers
341  * are zeroed sockaddr-like structures with sa_len field initialized
342  * to reflect size of the provided buffer. if no NHR_COPY is specified,
343  * point dst,netmask and gw @info fields to appropriate @rt values.
344  *
345  * if @flags contains NHR_REF, do refcouting on rt_ifp and rt_ifa.
346  *
347  * Returns 0 on success.
348  */
349 int
350 rt_exportinfo(struct rtentry *rt, struct rt_addrinfo *info, int flags)
351 {
352 	struct rt_metrics *rmx;
353 	struct sockaddr *src, *dst;
354 	struct nhop_object *nh;
355 	int sa_len;
356 
357 	nh = rt->rt_nhop;
358 	if (flags & NHR_COPY) {
359 		/* Copy destination if dst is non-zero */
360 		src = rt_key(rt);
361 		dst = info->rti_info[RTAX_DST];
362 		sa_len = src->sa_len;
363 		if (dst != NULL) {
364 			if (src->sa_len > dst->sa_len)
365 				return (ENOMEM);
366 			memcpy(dst, src, src->sa_len);
367 			info->rti_addrs |= RTA_DST;
368 		}
369 
370 		/* Copy mask if set && dst is non-zero */
371 		src = rt_mask(rt);
372 		dst = info->rti_info[RTAX_NETMASK];
373 		if (src != NULL && dst != NULL) {
374 
375 			/*
376 			 * Radix stores different value in sa_len,
377 			 * assume rt_mask() to have the same length
378 			 * as rt_key()
379 			 */
380 			if (sa_len > dst->sa_len)
381 				return (ENOMEM);
382 			memcpy(dst, src, src->sa_len);
383 			info->rti_addrs |= RTA_NETMASK;
384 		}
385 
386 		/* Copy gateway is set && dst is non-zero */
387 		src = &nh->gw_sa;
388 		dst = info->rti_info[RTAX_GATEWAY];
389 		if ((nhop_get_rtflags(nh) & RTF_GATEWAY) &&
390 		    src != NULL && dst != NULL) {
391 			if (src->sa_len > dst->sa_len)
392 				return (ENOMEM);
393 			memcpy(dst, src, src->sa_len);
394 			info->rti_addrs |= RTA_GATEWAY;
395 		}
396 	} else {
397 		info->rti_info[RTAX_DST] = rt_key(rt);
398 		info->rti_addrs |= RTA_DST;
399 		if (rt_mask(rt) != NULL) {
400 			info->rti_info[RTAX_NETMASK] = rt_mask(rt);
401 			info->rti_addrs |= RTA_NETMASK;
402 		}
403 		if (nhop_get_rtflags(nh) & RTF_GATEWAY) {
404 			info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
405 			info->rti_addrs |= RTA_GATEWAY;
406 		}
407 	}
408 
409 	rmx = info->rti_rmx;
410 	if (rmx != NULL) {
411 		info->rti_mflags |= RTV_MTU;
412 		rmx->rmx_mtu = nh->nh_mtu;
413 	}
414 
415 	info->rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
416 	info->rti_ifp = nh->nh_ifp;
417 	info->rti_ifa = nh->nh_ifa;
418 	if (flags & NHR_REF) {
419 		if_ref(info->rti_ifp);
420 		ifa_ref(info->rti_ifa);
421 	}
422 
423 	return (0);
424 }
425 
426 /*
427  * Lookups up route entry for @dst in RIB database for fib @fibnum.
428  * Exports entry data to @info using rt_exportinfo().
429  *
430  * If @flags contains NHR_REF, refcouting is performed on rt_ifp and rt_ifa.
431  * All references can be released later by calling rib_free_info().
432  *
433  * Returns 0 on success.
434  * Returns ENOENT for lookup failure, ENOMEM for export failure.
435  */
436 int
437 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags,
438     uint32_t flowid, struct rt_addrinfo *info)
439 {
440 	RIB_RLOCK_TRACKER;
441 	struct rib_head *rh;
442 	struct radix_node *rn;
443 	struct rtentry *rt;
444 	int error;
445 
446 	KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum"));
447 	rh = rt_tables_get_rnh(fibnum, dst->sa_family);
448 	if (rh == NULL)
449 		return (ENOENT);
450 
451 	RIB_RLOCK(rh);
452 	rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head);
453 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
454 		rt = RNTORT(rn);
455 		/* Ensure route & ifp is UP */
456 		if (RT_LINK_IS_UP(rt->rt_nhop->nh_ifp)) {
457 			flags = (flags & NHR_REF) | NHR_COPY;
458 			error = rt_exportinfo(rt, info, flags);
459 			RIB_RUNLOCK(rh);
460 
461 			return (error);
462 		}
463 	}
464 	RIB_RUNLOCK(rh);
465 
466 	return (ENOENT);
467 }
468 
469 /*
470  * Releases all references acquired by rib_lookup_info() when
471  * called with NHR_REF flags.
472  */
473 void
474 rib_free_info(struct rt_addrinfo *info)
475 {
476 
477 	ifa_free(info->rti_ifa);
478 	if_rele(info->rti_ifp);
479 }
480 
481 /*
482  * Iterates over all existing fibs in system calling
483  *  @setwa_f function prior to traversing each fib.
484  *  Calls @wa_f function for each element in current fib.
485  * If af is not AF_UNSPEC, iterates over fibs in particular
486  * address family.
487  */
488 void
489 rt_foreach_fib_walk(int af, rt_setwarg_t *setwa_f, rt_walktree_f_t *wa_f,
490     void *arg)
491 {
492 	struct rib_head *rnh;
493 	uint32_t fibnum;
494 	int i;
495 
496 	for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
497 		/* Do we want some specific family? */
498 		if (af != AF_UNSPEC) {
499 			rnh = rt_tables_get_rnh(fibnum, af);
500 			if (rnh == NULL)
501 				continue;
502 			if (setwa_f != NULL)
503 				setwa_f(rnh, fibnum, af, arg);
504 
505 			RIB_WLOCK(rnh);
506 			rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg);
507 			RIB_WUNLOCK(rnh);
508 			continue;
509 		}
510 
511 		for (i = 1; i <= AF_MAX; i++) {
512 			rnh = rt_tables_get_rnh(fibnum, i);
513 			if (rnh == NULL)
514 				continue;
515 			if (setwa_f != NULL)
516 				setwa_f(rnh, fibnum, i, arg);
517 
518 			RIB_WLOCK(rnh);
519 			rnh->rnh_walktree(&rnh->head, (walktree_f_t *)wa_f,arg);
520 			RIB_WUNLOCK(rnh);
521 		}
522 	}
523 }
524 
525 /*
526  * Iterates over all existing fibs in system and deletes each element
527  *  for which @filter_f function returns non-zero value.
528  * If @family is not AF_UNSPEC, iterates over fibs in particular
529  * address family.
530  */
531 void
532 rt_foreach_fib_walk_del(int family, rt_filter_f_t *filter_f, void *arg)
533 {
534 	u_int fibnum;
535 	int i, start, end;
536 
537 	for (fibnum = 0; fibnum < rt_numfibs; fibnum++) {
538 		/* Do we want some specific family? */
539 		if (family != AF_UNSPEC) {
540 			start = family;
541 			end = family;
542 		} else {
543 			start = 1;
544 			end = AF_MAX;
545 		}
546 
547 		for (i = start; i <= end; i++) {
548 			if (rt_tables_get_rnh(fibnum, i) == NULL)
549 				continue;
550 
551 			rib_walk_del(fibnum, i, filter_f, arg, 0);
552 		}
553 	}
554 }
555 
556 /*
557  * Delete Routes for a Network Interface
558  *
559  * Called for each routing entry via the rnh->rnh_walktree() call above
560  * to delete all route entries referencing a detaching network interface.
561  *
562  * Arguments:
563  *	rt	pointer to rtentry
564  *	nh	pointer to nhop
565  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
566  *
567  * Returns:
568  *	0	successful
569  *	errno	failed - reason indicated
570  */
571 static int
572 rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
573 {
574 	struct ifnet	*ifp = arg;
575 
576 	if (nh->nh_ifp != ifp)
577 		return (0);
578 
579 	/*
580 	 * Protect (sorta) against walktree recursion problems
581 	 * with cloned routes
582 	 */
583 	if ((rt->rte_flags & RTF_UP) == 0)
584 		return (0);
585 
586 	return (1);
587 }
588 
589 /*
590  * Delete all remaining routes using this interface
591  * Unfortuneatly the only way to do this is to slog through
592  * the entire routing table looking for routes which point
593  * to this interface...oh well...
594  */
595 void
596 rt_flushifroutes_af(struct ifnet *ifp, int af)
597 {
598 	KASSERT((af >= 1 && af <= AF_MAX), ("%s: af %d not >= 1 and <= %d",
599 	    __func__, af, AF_MAX));
600 
601 	rt_foreach_fib_walk_del(af, rt_ifdelroute, ifp);
602 }
603 
604 void
605 rt_flushifroutes(struct ifnet *ifp)
606 {
607 
608 	rt_foreach_fib_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
609 }
610 
611 /*
612  * Look up rt_addrinfo for a specific fib.  Note that if rti_ifa is defined,
613  * it will be referenced so the caller must free it.
614  *
615  * Assume basic consistency checks are executed by callers:
616  * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well.
617  */
618 int
619 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
620 {
621 	const struct sockaddr *dst, *gateway, *ifpaddr, *ifaaddr;
622 	struct epoch_tracker et;
623 	int needref, error, flags;
624 
625 	dst = info->rti_info[RTAX_DST];
626 	gateway = info->rti_info[RTAX_GATEWAY];
627 	ifpaddr = info->rti_info[RTAX_IFP];
628 	ifaaddr = info->rti_info[RTAX_IFA];
629 	flags = info->rti_flags;
630 
631 	/*
632 	 * ifp may be specified by sockaddr_dl
633 	 * when protocol address is ambiguous.
634 	 */
635 	error = 0;
636 	needref = (info->rti_ifa == NULL);
637 	NET_EPOCH_ENTER(et);
638 
639 	/* If we have interface specified by the ifindex in the address, use it */
640 	if (info->rti_ifp == NULL && ifpaddr != NULL &&
641 	    ifpaddr->sa_family == AF_LINK) {
642 	    const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)ifpaddr;
643 	    if (sdl->sdl_index != 0)
644 		    info->rti_ifp = ifnet_byindex(sdl->sdl_index);
645 	}
646 	/*
647 	 * If we have source address specified, try to find it
648 	 * TODO: avoid enumerating all ifas on all interfaces.
649 	 */
650 	if (info->rti_ifa == NULL && ifaaddr != NULL)
651 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
652 	if (info->rti_ifa == NULL) {
653 		const struct sockaddr *sa;
654 
655 		/*
656 		 * Most common use case for the userland-supplied routes.
657 		 *
658 		 * Choose sockaddr to select ifa.
659 		 * -- if ifp is set --
660 		 * Order of preference:
661 		 * 1) IFA address
662 		 * 2) gateway address
663 		 *   Note: for interface routes link-level gateway address
664 		 *     is specified to indicate the interface index without
665 		 *     specifying RTF_GATEWAY. In this case, ignore gateway
666 		 *   Note: gateway AF may be different from dst AF. In this case,
667 		 *   ignore gateway
668 		 * 3) final destination.
669 		 * 4) if all of these fails, try to get at least link-level ifa.
670 		 * -- else --
671 		 * try to lookup gateway or dst in the routing table to get ifa
672 		 */
673 		if (info->rti_info[RTAX_IFA] != NULL)
674 			sa = info->rti_info[RTAX_IFA];
675 		else if ((info->rti_flags & RTF_GATEWAY) != 0 &&
676 		    gateway->sa_family == dst->sa_family)
677 			sa = gateway;
678 		else
679 			sa = dst;
680 		if (info->rti_ifp != NULL) {
681 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
682 			/* Case 4 */
683 			if (info->rti_ifa == NULL && gateway != NULL)
684 				info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp);
685 		} else if (dst != NULL && gateway != NULL)
686 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
687 							fibnum);
688 		else if (sa != NULL)
689 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
690 							fibnum);
691 	}
692 	if (needref && info->rti_ifa != NULL) {
693 		if (info->rti_ifp == NULL)
694 			info->rti_ifp = info->rti_ifa->ifa_ifp;
695 		ifa_ref(info->rti_ifa);
696 	} else
697 		error = ENETUNREACH;
698 	NET_EPOCH_EXIT(et);
699 	return (error);
700 }
701 
702 void
703 rt_updatemtu(struct ifnet *ifp)
704 {
705 	struct rib_head *rnh;
706 	int mtu;
707 	int i, j;
708 
709 	/*
710 	 * Try to update rt_mtu for all routes using this interface
711 	 * Unfortunately the only way to do this is to traverse all
712 	 * routing tables in all fibs/domains.
713 	 */
714 	for (i = 1; i <= AF_MAX; i++) {
715 		mtu = if_getmtu_family(ifp, i);
716 		for (j = 0; j < rt_numfibs; j++) {
717 			rnh = rt_tables_get_rnh(j, i);
718 			if (rnh == NULL)
719 				continue;
720 			nhops_update_ifmtu(rnh, ifp, mtu);
721 		}
722 	}
723 }
724 
725 
726 #if 0
727 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
728 int rt_print(char *buf, int buflen, struct rtentry *rt);
729 
730 int
731 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
732 {
733 	void *paddr = NULL;
734 
735 	switch (s->sa_family) {
736 	case AF_INET:
737 		paddr = &((struct sockaddr_in *)s)->sin_addr;
738 		break;
739 	case AF_INET6:
740 		paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
741 		break;
742 	}
743 
744 	if (paddr == NULL)
745 		return (0);
746 
747 	if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
748 		return (0);
749 
750 	return (strlen(buf));
751 }
752 
753 int
754 rt_print(char *buf, int buflen, struct rtentry *rt)
755 {
756 	struct sockaddr *addr, *mask;
757 	int i = 0;
758 
759 	addr = rt_key(rt);
760 	mask = rt_mask(rt);
761 
762 	i = p_sockaddr(buf, buflen, addr);
763 	if (!(rt->rt_flags & RTF_HOST)) {
764 		buf[i++] = '/';
765 		i += p_sockaddr(buf + i, buflen - i, mask);
766 	}
767 
768 	if (rt->rt_flags & RTF_GATEWAY) {
769 		buf[i++] = '>';
770 		i += p_sockaddr(buf + i, buflen - i, &rt->rt_nhop->gw_sa);
771 	}
772 
773 	return (i);
774 }
775 #endif
776 
777 #ifdef RADIX_MPATH
778 /*
779  * Deletes key for single-path routes, unlinks rtentry with
780  * gateway specified in @info from multi-path routes.
781  *
782  * Returnes unlinked entry. In case of failure, returns NULL
783  * and sets @perror to ESRCH.
784  */
785 struct radix_node *
786 rt_mpath_unlink(struct rib_head *rnh, struct rt_addrinfo *info,
787     struct rtentry *rto, int *perror)
788 {
789 	/*
790 	 * if we got multipath routes, we require users to specify
791 	 * a matching RTAX_GATEWAY.
792 	 */
793 	struct rtentry *rt; // *rto = NULL;
794 	struct radix_node *rn;
795 	struct sockaddr *gw;
796 
797 	gw = info->rti_info[RTAX_GATEWAY];
798 	rt = rt_mpath_matchgate(rto, gw);
799 	if (rt == NULL) {
800 		*perror = ESRCH;
801 		return (NULL);
802 	}
803 
804 	/*
805 	 * this is the first entry in the chain
806 	 */
807 	if (rto == rt) {
808 		rn = rn_mpath_next((struct radix_node *)rt);
809 		/*
810 		 * there is another entry, now it's active
811 		 */
812 		if (rn) {
813 			rto = RNTORT(rn);
814 			RT_LOCK(rto);
815 			rto->rte_flags |= RTF_UP;
816 			RT_UNLOCK(rto);
817 		} else if (rt->rte_flags & RTF_GATEWAY) {
818 			/*
819 			 * For gateway routes, we need to
820 			 * make sure that we we are deleting
821 			 * the correct gateway.
822 			 * rt_mpath_matchgate() does not
823 			 * check the case when there is only
824 			 * one route in the chain.
825 			 */
826 			if (gw &&
827 			    (rt->rt_nhop->gw_sa.sa_len != gw->sa_len ||
828 				memcmp(&rt->rt_nhop->gw_sa, gw, gw->sa_len))) {
829 				*perror = ESRCH;
830 				return (NULL);
831 			}
832 		}
833 
834 		/*
835 		 * use the normal delete code to remove
836 		 * the first entry
837 		 */
838 		rn = rnh->rnh_deladdr(info->rti_info[RTAX_DST],
839 					info->rti_info[RTAX_NETMASK],
840 					&rnh->head);
841 		if (rn != NULL) {
842 			*perror = 0;
843 		} else {
844 			*perror = ESRCH;
845 		}
846 		return (rn);
847 	}
848 
849 	/*
850 	 * if the entry is 2nd and on up
851 	 */
852 	if (rt_mpath_deldup(rto, rt) == 0)
853 		panic ("rtrequest1: rt_mpath_deldup");
854 	*perror = 0;
855 	rn = (struct radix_node *)rt;
856 	return (rn);
857 }
858 #endif
859 
860 void
861 rt_setmetrics(const struct rt_addrinfo *info, struct rtentry *rt)
862 {
863 
864 	if (info->rti_mflags & RTV_WEIGHT)
865 		rt->rt_weight = info->rti_rmx->rmx_weight;
866 	/* Kernel -> userland timebase conversion. */
867 	if (info->rti_mflags & RTV_EXPIRE)
868 		rt->rt_expire = info->rti_rmx->rmx_expire ?
869 		    info->rti_rmx->rmx_expire - time_second + time_uptime : 0;
870 }
871 
872 void
873 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
874 {
875 	u_char *cp1 = (u_char *)src;
876 	u_char *cp2 = (u_char *)dst;
877 	u_char *cp3 = (u_char *)netmask;
878 	u_char *cplim = cp2 + *cp3;
879 	u_char *cplim2 = cp2 + *cp1;
880 
881 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
882 	cp3 += 2;
883 	if (cplim > cplim2)
884 		cplim = cplim2;
885 	while (cp2 < cplim)
886 		*cp2++ = *cp1++ & *cp3++;
887 	if (cp2 < cplim2)
888 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
889 }
890 
891 /*
892  * Set up a routing table entry, normally
893  * for an interface.
894  */
895 #define _SOCKADDR_TMPSIZE 128 /* Not too big.. kernel stack size is limited */
896 static inline  int
897 rtinit1(struct ifaddr *ifa, int cmd, int flags, int fibnum)
898 {
899 	RIB_RLOCK_TRACKER;
900 	struct epoch_tracker et;
901 	struct sockaddr *dst;
902 	struct sockaddr *netmask;
903 	struct rib_cmd_info rc;
904 	struct rt_addrinfo info;
905 	int error = 0;
906 	int startfib, endfib;
907 	char tempbuf[_SOCKADDR_TMPSIZE];
908 	int didwork = 0;
909 	int a_failure = 0;
910 	struct sockaddr_dl_short *sdl = NULL;
911 	struct rib_head *rnh;
912 
913 	if (flags & RTF_HOST) {
914 		dst = ifa->ifa_dstaddr;
915 		netmask = NULL;
916 	} else {
917 		dst = ifa->ifa_addr;
918 		netmask = ifa->ifa_netmask;
919 	}
920 	if (dst->sa_len == 0)
921 		return(EINVAL);
922 	switch (dst->sa_family) {
923 	case AF_INET6:
924 	case AF_INET:
925 		/* We support multiple FIBs. */
926 		break;
927 	default:
928 		fibnum = RT_DEFAULT_FIB;
929 		break;
930 	}
931 	if (fibnum == RT_ALL_FIBS) {
932 		if (V_rt_add_addr_allfibs == 0 && cmd == (int)RTM_ADD)
933 			startfib = endfib = ifa->ifa_ifp->if_fib;
934 		else {
935 			startfib = 0;
936 			endfib = rt_numfibs - 1;
937 		}
938 	} else {
939 		KASSERT((fibnum < rt_numfibs), ("rtinit1: bad fibnum"));
940 		startfib = fibnum;
941 		endfib = fibnum;
942 	}
943 
944 	/*
945 	 * If it's a delete, check that if it exists,
946 	 * it's on the correct interface or we might scrub
947 	 * a route to another ifa which would
948 	 * be confusing at best and possibly worse.
949 	 */
950 	if (cmd == RTM_DELETE) {
951 		/*
952 		 * It's a delete, so it should already exist..
953 		 * If it's a net, mask off the host bits
954 		 * (Assuming we have a mask)
955 		 * XXX this is kinda inet specific..
956 		 */
957 		if (netmask != NULL) {
958 			rt_maskedcopy(dst, (struct sockaddr *)tempbuf, netmask);
959 			dst = (struct sockaddr *)tempbuf;
960 		}
961 	} else if (cmd == RTM_ADD) {
962 		sdl = (struct sockaddr_dl_short *)tempbuf;
963 		bzero(sdl, sizeof(struct sockaddr_dl_short));
964 		sdl->sdl_family = AF_LINK;
965 		sdl->sdl_len = sizeof(struct sockaddr_dl_short);
966 		sdl->sdl_type = ifa->ifa_ifp->if_type;
967 		sdl->sdl_index = ifa->ifa_ifp->if_index;
968         }
969 	/*
970 	 * Now go through all the requested tables (fibs) and do the
971 	 * requested action. Realistically, this will either be fib 0
972 	 * for protocols that don't do multiple tables or all the
973 	 * tables for those that do.
974 	 */
975 	for ( fibnum = startfib; fibnum <= endfib; fibnum++) {
976 		if (cmd == RTM_DELETE) {
977 			struct radix_node *rn;
978 			/*
979 			 * Look up an rtentry that is in the routing tree and
980 			 * contains the correct info.
981 			 */
982 			rnh = rt_tables_get_rnh(fibnum, dst->sa_family);
983 			if (rnh == NULL)
984 				/* this table doesn't exist but others might */
985 				continue;
986 			RIB_RLOCK(rnh);
987 			rn = rnh->rnh_lookup(dst, netmask, &rnh->head);
988 #ifdef RADIX_MPATH
989 			if (rt_mpath_capable(rnh)) {
990 
991 				if (rn == NULL)
992 					error = ESRCH;
993 				else {
994 					struct rtentry *rt = RNTORT(rn);
995 					/*
996 					 * for interface route the gateway
997 					 * gateway is sockaddr_dl, so
998 					 * rt_mpath_matchgate must use the
999 					 * interface address
1000 					 */
1001 					rt = rt_mpath_matchgate(rt,
1002 					    ifa->ifa_addr);
1003 					if (rt == NULL)
1004 						error = ESRCH;
1005 				}
1006 			}
1007 #endif
1008 			error = (rn == NULL ||
1009 			    (rn->rn_flags & RNF_ROOT) ||
1010 			    RNTORT(rn)->rt_nhop->nh_ifa != ifa);
1011 			RIB_RUNLOCK(rnh);
1012 			if (error) {
1013 				/* this is only an error if bad on ALL tables */
1014 				continue;
1015 			}
1016 		}
1017 		/*
1018 		 * Do the actual request
1019 		 */
1020 		bzero((caddr_t)&info, sizeof(info));
1021 		info.rti_ifa = ifa;
1022 		info.rti_flags = flags |
1023 		    (ifa->ifa_flags & ~IFA_RTSELF) | RTF_PINNED;
1024 		info.rti_info[RTAX_DST] = dst;
1025 		/*
1026 		 * doing this for compatibility reasons
1027 		 */
1028 		if (cmd == RTM_ADD)
1029 			info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)sdl;
1030 		else
1031 			info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1032 		info.rti_info[RTAX_NETMASK] = netmask;
1033 		NET_EPOCH_ENTER(et);
1034 		error = rib_action(fibnum, cmd, &info, &rc);
1035 		if (error == 0 && rc.rc_rt != NULL) {
1036 			/*
1037 			 * notify any listening routing agents of the change
1038 			 */
1039 
1040 			/* TODO: interface routes/aliases */
1041 			rt_newaddrmsg_fib(cmd, ifa, rc.rc_rt, fibnum);
1042 			didwork = 1;
1043 		}
1044 		NET_EPOCH_EXIT(et);
1045 		if (error)
1046 			a_failure = error;
1047 	}
1048 	if (cmd == RTM_DELETE) {
1049 		if (didwork) {
1050 			error = 0;
1051 		} else {
1052 			/* we only give an error if it wasn't in any table */
1053 			error = ((flags & RTF_HOST) ?
1054 			    EHOSTUNREACH : ENETUNREACH);
1055 		}
1056 	} else {
1057 		if (a_failure) {
1058 			/* return an error if any of them failed */
1059 			error = a_failure;
1060 		}
1061 	}
1062 	return (error);
1063 }
1064 
1065 /*
1066  * Set up a routing table entry, normally
1067  * for an interface.
1068  */
1069 int
1070 rtinit(struct ifaddr *ifa, int cmd, int flags)
1071 {
1072 	struct sockaddr *dst;
1073 	int fib = RT_DEFAULT_FIB;
1074 
1075 	if (flags & RTF_HOST) {
1076 		dst = ifa->ifa_dstaddr;
1077 	} else {
1078 		dst = ifa->ifa_addr;
1079 	}
1080 
1081 	switch (dst->sa_family) {
1082 	case AF_INET6:
1083 	case AF_INET:
1084 		/* We do support multiple FIBs. */
1085 		fib = RT_ALL_FIBS;
1086 		break;
1087 	}
1088 	return (rtinit1(ifa, cmd, flags, fib));
1089 }
1090 
1091 /*
1092  * Announce interface address arrival/withdraw
1093  * Returns 0 on success.
1094  */
1095 int
1096 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1097 {
1098 
1099 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1100 	    ("unexpected cmd %d", cmd));
1101 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1102 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1103 
1104 	EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd);
1105 	return (rtsock_addrmsg(cmd, ifa, fibnum));
1106 }
1107 
1108 /*
1109  * Announce kernel-originated route addition/removal to rtsock based on @rt data.
1110  * cmd: RTM_ cmd
1111  * @rt: valid rtentry
1112  * @ifp: target route interface
1113  * @fibnum: fib id or RT_ALL_FIBS
1114  *
1115  * Returns 0 on success.
1116  */
1117 int
1118 rt_routemsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int rti_addrs,
1119     int fibnum)
1120 {
1121 
1122 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1123 	    ("unexpected cmd %d", cmd));
1124 
1125 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1126 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1127 
1128 	KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
1129 
1130 	return (rtsock_routemsg(cmd, rt, ifp, 0, fibnum));
1131 }
1132 
1133 /*
1134  * Announce kernel-originated route addition/removal to rtsock based on @rt data.
1135  * cmd: RTM_ cmd
1136  * @info: addrinfo structure with valid data.
1137  * @fibnum: fib id or RT_ALL_FIBS
1138  *
1139  * Returns 0 on success.
1140  */
1141 int
1142 rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
1143 {
1144 
1145 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
1146 	    ("unexpected cmd %d", cmd));
1147 
1148 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1149 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1150 
1151 	KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__));
1152 
1153 	return (rtsock_routemsg_info(cmd, info, fibnum));
1154 }
1155 
1156 
1157 /*
1158  * This is called to generate messages from the routing socket
1159  * indicating a network interface has had addresses associated with it.
1160  */
1161 void
1162 rt_newaddrmsg_fib(int cmd, struct ifaddr *ifa, struct rtentry *rt, int fibnum)
1163 {
1164 
1165 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1166 		("unexpected cmd %u", cmd));
1167 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
1168 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
1169 
1170 	if (cmd == RTM_ADD) {
1171 		rt_addrmsg(cmd, ifa, fibnum);
1172 		if (rt != NULL)
1173 			rt_routemsg(cmd, rt, ifa->ifa_ifp, 0, fibnum);
1174 	} else {
1175 		if (rt != NULL)
1176 			rt_routemsg(cmd, rt, ifa->ifa_ifp, 0, fibnum);
1177 		rt_addrmsg(cmd, ifa, fibnum);
1178 	}
1179 }
1180 
1181