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