xref: /freebsd/sys/net/route.c (revision 266f97b5)
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/devctl.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/vnet.h>
68 
69 #include <netinet/in.h>
70 #include <netinet/ip_mroute.h>
71 #include <netinet6/in6_var.h>
72 
73 VNET_PCPUSTAT_DEFINE(struct rtstat, rtstat);
74 
75 VNET_PCPUSTAT_SYSINIT(rtstat);
76 #ifdef VIMAGE
77 VNET_PCPUSTAT_SYSUNINIT(rtstat);
78 #endif
79 
80 EVENTHANDLER_LIST_DEFINE(rt_addrmsg);
81 
82 static int rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *,
83     void *arg);
84 static int rt_exportinfo(struct rtentry *rt, struct nhop_object *nh,
85     struct rt_addrinfo *info, int flags);
86 
87 /*
88  * route initialization must occur before ip6_init2(), which happenas at
89  * SI_ORDER_MIDDLE.
90  */
91 static void
92 route_init(void)
93 {
94 
95 	nhops_init();
96 }
97 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, NULL);
98 
99 struct rib_head *
100 rt_table_init(int offset, int family, u_int fibnum)
101 {
102 	struct rib_head *rh;
103 
104 	rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO);
105 
106 	/* TODO: These details should be hidded inside radix.c */
107 	/* Init masks tree */
108 	rn_inithead_internal(&rh->head, rh->rnh_nodes, offset);
109 	rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0);
110 	rh->head.rnh_masks = &rh->rmhead;
111 
112 	/* Save metadata associated with this routing table. */
113 	rh->rib_family = family;
114 	rh->rib_fibnum = fibnum;
115 #ifdef VIMAGE
116 	rh->rib_vnet = curvnet;
117 #endif
118 
119 	tmproutes_init(rh);
120 
121 	/* Init locks */
122 	RIB_LOCK_INIT(rh);
123 
124 	nhops_init_rib(rh);
125 
126 	/* Init subscription system */
127 	rib_init_subscriptions(rh);
128 
129 	/* Finally, set base callbacks */
130 	rh->rnh_addaddr = rn_addroute;
131 	rh->rnh_deladdr = rn_delete;
132 	rh->rnh_matchaddr = rn_match;
133 	rh->rnh_lookup = rn_lookup;
134 	rh->rnh_walktree = rn_walktree;
135 	rh->rnh_walktree_from = rn_walktree_from;
136 
137 	return (rh);
138 }
139 
140 static int
141 rt_freeentry(struct radix_node *rn, void *arg)
142 {
143 	struct radix_head * const rnh = arg;
144 	struct radix_node *x;
145 
146 	x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh);
147 	if (x != NULL)
148 		R_Free(x);
149 	return (0);
150 }
151 
152 void
153 rt_table_destroy(struct rib_head *rh)
154 {
155 
156 	RIB_WLOCK(rh);
157 	rh->rib_dying = true;
158 	RIB_WUNLOCK(rh);
159 
160 #ifdef FIB_ALGO
161 	fib_destroy_rib(rh);
162 #endif
163 
164 	tmproutes_destroy(rh);
165 
166 	rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head);
167 
168 	nhops_destroy_rib(rh);
169 
170 	rib_destroy_subscriptions(rh);
171 
172 	/* Assume table is already empty */
173 	RIB_LOCK_DESTROY(rh);
174 	free(rh, M_RTABLE);
175 }
176 
177 /*
178  * Adds a temporal redirect entry to the routing table.
179  * @fibnum: fib number
180  * @dst: destination to install redirect to
181  * @gateway: gateway to go via
182  * @author: sockaddr of originating router, can be NULL
183  * @ifp: interface to use for the redirected route
184  * @flags: set of flags to add. Allowed: RTF_GATEWAY
185  * @lifetime_sec: time in seconds to expire this redirect.
186  *
187  * Retuns 0 on success, errno otherwise.
188  */
189 int
190 rib_add_redirect(u_int fibnum, struct sockaddr *dst, struct sockaddr *gateway,
191     struct sockaddr *author, struct ifnet *ifp, int flags, int lifetime_sec)
192 {
193 	struct rib_cmd_info rc;
194 	int error;
195 	struct rt_addrinfo info;
196 	struct rt_metrics rti_rmx;
197 	struct ifaddr *ifa;
198 
199 	NET_EPOCH_ASSERT();
200 
201 	if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL)
202 		return (EAFNOSUPPORT);
203 
204 	/* Verify the allowed flag mask. */
205 	KASSERT(((flags & ~(RTF_GATEWAY)) == 0),
206 	    ("invalid redirect flags: %x", flags));
207 	flags |= RTF_HOST | RTF_DYNAMIC;
208 
209 	/* Get the best ifa for the given interface and gateway. */
210 	if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL)
211 		return (ENETUNREACH);
212 
213 	bzero(&info, sizeof(info));
214 	info.rti_info[RTAX_DST] = dst;
215 	info.rti_info[RTAX_GATEWAY] = gateway;
216 	info.rti_ifa = ifa;
217 	info.rti_ifp = ifp;
218 	info.rti_flags = flags;
219 
220 	/* Setup route metrics to define expire time. */
221 	bzero(&rti_rmx, sizeof(rti_rmx));
222 	/* Set expire time as absolute. */
223 	rti_rmx.rmx_expire = lifetime_sec + time_second;
224 	info.rti_mflags |= RTV_EXPIRE;
225 	info.rti_rmx = &rti_rmx;
226 
227 	error = rib_action(fibnum, RTM_ADD, &info, &rc);
228 
229 	if (error != 0) {
230 		/* TODO: add per-fib redirect stats. */
231 		return (error);
232 	}
233 
234 	RTSTAT_INC(rts_dynamic);
235 
236 	/* Send notification of a route addition to userland. */
237 	bzero(&info, sizeof(info));
238 	info.rti_info[RTAX_DST] = dst;
239 	info.rti_info[RTAX_GATEWAY] = gateway;
240 	info.rti_info[RTAX_AUTHOR] = author;
241 	rt_missmsg_fib(RTM_REDIRECT, &info, flags | RTF_UP, error, fibnum);
242 
243 	return (0);
244 }
245 
246 /*
247  * Routing table ioctl interface.
248  */
249 int
250 rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
251 {
252 
253 	/*
254 	 * If more ioctl commands are added here, make sure the proper
255 	 * super-user checks are being performed because it is possible for
256 	 * prison-root to make it this far if raw sockets have been enabled
257 	 * in jails.
258 	 */
259 #ifdef INET
260 	/* Multicast goop, grrr... */
261 	return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
262 #else /* INET */
263 	return ENXIO;
264 #endif /* INET */
265 }
266 
267 struct ifaddr *
268 ifa_ifwithroute(int flags, const struct sockaddr *dst,
269     const struct sockaddr *gateway, u_int fibnum)
270 {
271 	struct ifaddr *ifa;
272 
273 	NET_EPOCH_ASSERT();
274 	if ((flags & RTF_GATEWAY) == 0) {
275 		/*
276 		 * If we are adding a route to an interface,
277 		 * and the interface is a pt to pt link
278 		 * we should search for the destination
279 		 * as our clue to the interface.  Otherwise
280 		 * we can use the local address.
281 		 */
282 		ifa = NULL;
283 		if (flags & RTF_HOST)
284 			ifa = ifa_ifwithdstaddr(dst, fibnum);
285 		if (ifa == NULL)
286 			ifa = ifa_ifwithaddr(gateway);
287 	} else {
288 		/*
289 		 * If we are adding a route to a remote net
290 		 * or host, the gateway may still be on the
291 		 * other end of a pt to pt link.
292 		 */
293 		ifa = ifa_ifwithdstaddr(gateway, fibnum);
294 	}
295 	if (ifa == NULL)
296 		ifa = ifa_ifwithnet(gateway, 0, fibnum);
297 	if (ifa == NULL) {
298 		struct nhop_object *nh;
299 
300 		nh = rib_lookup(fibnum, gateway, NHR_NONE, 0);
301 
302 		/*
303 		 * dismiss a gateway that is reachable only
304 		 * through the default router
305 		 */
306 		if ((nh == NULL) || (nh->nh_flags & NHF_DEFAULT))
307 			return (NULL);
308 		ifa = nh->nh_ifa;
309 	}
310 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
311 		struct ifaddr *oifa = ifa;
312 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
313 		if (ifa == NULL)
314 			ifa = oifa;
315 	}
316 
317 	return (ifa);
318 }
319 
320 /*
321  * Copy most of @rt data into @info.
322  *
323  * If @flags contains NHR_COPY, copies dst,netmask and gw to the
324  * pointers specified by @info structure. Assume such pointers
325  * are zeroed sockaddr-like structures with sa_len field initialized
326  * to reflect size of the provided buffer. if no NHR_COPY is specified,
327  * point dst,netmask and gw @info fields to appropriate @rt values.
328  *
329  * if @flags contains NHR_REF, do refcouting on rt_ifp and rt_ifa.
330  *
331  * Returns 0 on success.
332  */
333 static int
334 rt_exportinfo(struct rtentry *rt, struct nhop_object *nh,
335     struct rt_addrinfo *info, int flags)
336 {
337 	struct rt_metrics *rmx;
338 	struct sockaddr *src, *dst;
339 	int sa_len;
340 
341 	if (flags & NHR_COPY) {
342 		/* Copy destination if dst is non-zero */
343 		src = rt_key(rt);
344 		dst = info->rti_info[RTAX_DST];
345 		sa_len = src->sa_len;
346 		if (dst != NULL) {
347 			if (src->sa_len > dst->sa_len)
348 				return (ENOMEM);
349 			memcpy(dst, src, src->sa_len);
350 			info->rti_addrs |= RTA_DST;
351 		}
352 
353 		/* Copy mask if set && dst is non-zero */
354 		src = rt_mask(rt);
355 		dst = info->rti_info[RTAX_NETMASK];
356 		if (src != NULL && dst != NULL) {
357 			/*
358 			 * Radix stores different value in sa_len,
359 			 * assume rt_mask() to have the same length
360 			 * as rt_key()
361 			 */
362 			if (sa_len > dst->sa_len)
363 				return (ENOMEM);
364 			memcpy(dst, src, src->sa_len);
365 			info->rti_addrs |= RTA_NETMASK;
366 		}
367 
368 		/* Copy gateway is set && dst is non-zero */
369 		src = &nh->gw_sa;
370 		dst = info->rti_info[RTAX_GATEWAY];
371 		if ((nhop_get_rtflags(nh) & RTF_GATEWAY) &&
372 		    src != NULL && dst != NULL) {
373 			if (src->sa_len > dst->sa_len)
374 				return (ENOMEM);
375 			memcpy(dst, src, src->sa_len);
376 			info->rti_addrs |= RTA_GATEWAY;
377 		}
378 	} else {
379 		info->rti_info[RTAX_DST] = rt_key(rt);
380 		info->rti_addrs |= RTA_DST;
381 		if (rt_mask(rt) != NULL) {
382 			info->rti_info[RTAX_NETMASK] = rt_mask(rt);
383 			info->rti_addrs |= RTA_NETMASK;
384 		}
385 		if (nhop_get_rtflags(nh) & RTF_GATEWAY) {
386 			info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
387 			info->rti_addrs |= RTA_GATEWAY;
388 		}
389 	}
390 
391 	rmx = info->rti_rmx;
392 	if (rmx != NULL) {
393 		info->rti_mflags |= RTV_MTU;
394 		rmx->rmx_mtu = nh->nh_mtu;
395 	}
396 
397 	info->rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
398 	info->rti_ifp = nh->nh_ifp;
399 	info->rti_ifa = nh->nh_ifa;
400 	if (flags & NHR_REF) {
401 		if_ref(info->rti_ifp);
402 		ifa_ref(info->rti_ifa);
403 	}
404 
405 	return (0);
406 }
407 
408 /*
409  * Lookups up route entry for @dst in RIB database for fib @fibnum.
410  * Exports entry data to @info using rt_exportinfo().
411  *
412  * If @flags contains NHR_REF, refcouting is performed on rt_ifp and rt_ifa.
413  * All references can be released later by calling rib_free_info().
414  *
415  * Returns 0 on success.
416  * Returns ENOENT for lookup failure, ENOMEM for export failure.
417  */
418 int
419 rib_lookup_info(uint32_t fibnum, const struct sockaddr *dst, uint32_t flags,
420     uint32_t flowid, struct rt_addrinfo *info)
421 {
422 	RIB_RLOCK_TRACKER;
423 	struct rib_head *rh;
424 	struct radix_node *rn;
425 	struct rtentry *rt;
426 	struct nhop_object *nh;
427 	int error;
428 
429 	KASSERT((fibnum < rt_numfibs), ("rib_lookup_rte: bad fibnum"));
430 	rh = rt_tables_get_rnh(fibnum, dst->sa_family);
431 	if (rh == NULL)
432 		return (ENOENT);
433 
434 	RIB_RLOCK(rh);
435 	rn = rh->rnh_matchaddr(__DECONST(void *, dst), &rh->head);
436 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
437 		rt = RNTORT(rn);
438 		nh = nhop_select(rt->rt_nhop, flowid);
439 		/* Ensure route & ifp is UP */
440 		if (RT_LINK_IS_UP(nh->nh_ifp)) {
441 			flags = (flags & NHR_REF) | NHR_COPY;
442 			error = rt_exportinfo(rt, nh, info, flags);
443 			RIB_RUNLOCK(rh);
444 
445 			return (error);
446 		}
447 	}
448 	RIB_RUNLOCK(rh);
449 
450 	return (ENOENT);
451 }
452 
453 /*
454  * Releases all references acquired by rib_lookup_info() when
455  * called with NHR_REF flags.
456  */
457 void
458 rib_free_info(struct rt_addrinfo *info)
459 {
460 
461 	ifa_free(info->rti_ifa);
462 	if_rele(info->rti_ifp);
463 }
464 
465 /*
466  * Delete Routes for a Network Interface
467  *
468  * Called for each routing entry via the rnh->rnh_walktree() call above
469  * to delete all route entries referencing a detaching network interface.
470  *
471  * Arguments:
472  *	rt	pointer to rtentry
473  *	nh	pointer to nhop
474  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
475  *
476  * Returns:
477  *	0	successful
478  *	errno	failed - reason indicated
479  */
480 static int
481 rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
482 {
483 	struct ifnet	*ifp = arg;
484 
485 	if (nh->nh_ifp != ifp)
486 		return (0);
487 
488 	/*
489 	 * Protect (sorta) against walktree recursion problems
490 	 * with cloned routes
491 	 */
492 	if ((rt->rte_flags & RTF_UP) == 0)
493 		return (0);
494 
495 	return (1);
496 }
497 
498 void
499 rt_flushifroutes(struct ifnet *ifp)
500 {
501 
502 	rib_foreach_table_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
503 }
504 
505 /*
506  * Tries to extract interface from RTAX_IFP passed in rt_addrinfo.
507  * Interface can be specified ether as interface index (sdl_index) or
508  * the interface name (sdl_data).
509  *
510  * Returns found ifp or NULL
511  */
512 static struct ifnet *
513 info_get_ifp(struct rt_addrinfo *info)
514 {
515 	const struct sockaddr_dl *sdl;
516 
517 	sdl = (const struct sockaddr_dl *)info->rti_info[RTAX_IFP];
518 	if (sdl->sdl_family != AF_LINK)
519 		return (NULL);
520 
521 	if (sdl->sdl_index != 0)
522 		return (ifnet_byindex(sdl->sdl_index));
523 	if (sdl->sdl_nlen > 0) {
524 		char if_name[IF_NAMESIZE];
525 		if (sdl->sdl_nlen + offsetof(struct sockaddr_dl, sdl_data) > sdl->sdl_len)
526 			return (NULL);
527 		if (sdl->sdl_nlen >= IF_NAMESIZE)
528 			return (NULL);
529 		bzero(if_name, sizeof(if_name));
530 		memcpy(if_name, sdl->sdl_data, sdl->sdl_nlen);
531 		return (ifunit(if_name));
532 	}
533 
534 	return (NULL);
535 }
536 
537 /*
538  * Calculates proper ifa/ifp for the cases when gateway AF is different
539  * from dst AF.
540  *
541  * Returns 0 on success.
542  */
543 __noinline static int
544 rt_getifa_family(struct rt_addrinfo *info, uint32_t fibnum)
545 {
546 	if (info->rti_ifp == NULL) {
547 		struct ifaddr *ifa = NULL;
548 		/*
549 		 * No transmit interface specified. Guess it by checking gw sa.
550 		 */
551 		const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
552 		ifa = ifa_ifwithroute(RTF_GATEWAY, gw, gw, fibnum);
553 		if (ifa == NULL)
554 			return (ENETUNREACH);
555 		info->rti_ifp = ifa->ifa_ifp;
556 	}
557 
558 	/* Prefer address from outgoing interface */
559 	info->rti_ifa = ifaof_ifpforaddr(info->rti_info[RTAX_DST], info->rti_ifp);
560 #ifdef INET
561 	if (info->rti_ifa == NULL) {
562 		/* Use first found IPv4 address */
563 		bool loopback_ok = info->rti_ifp->if_flags & IFF_LOOPBACK;
564 		info->rti_ifa = (struct ifaddr *)in_findlocal(fibnum, loopback_ok);
565 	}
566 #endif
567 	if (info->rti_ifa == NULL)
568 		return (ENETUNREACH);
569 	return (0);
570 }
571 
572 /*
573  * Look up rt_addrinfo for a specific fib.
574  *
575  * Assume basic consistency checks are executed by callers:
576  * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well.
577  */
578 int
579 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
580 {
581 	const struct sockaddr *dst, *gateway, *ifaaddr;
582 	int error, flags;
583 
584 	dst = info->rti_info[RTAX_DST];
585 	gateway = info->rti_info[RTAX_GATEWAY];
586 	ifaaddr = info->rti_info[RTAX_IFA];
587 	flags = info->rti_flags;
588 
589 	/*
590 	 * ifp may be specified by sockaddr_dl
591 	 * when protocol address is ambiguous.
592 	 */
593 	error = 0;
594 
595 	/* If we have interface specified by RTAX_IFP address, try to use it */
596 	if ((info->rti_ifp == NULL) && (info->rti_info[RTAX_IFP] != NULL))
597 		info->rti_ifp = info_get_ifp(info);
598 	/*
599 	 * If we have source address specified, try to find it
600 	 * TODO: avoid enumerating all ifas on all interfaces.
601 	 */
602 	if (info->rti_ifa == NULL && ifaaddr != NULL)
603 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
604 	if ((info->rti_ifa == NULL) && ((info->rti_flags & RTF_GATEWAY) != 0) &&
605 	    (gateway->sa_family != dst->sa_family))
606 		return (rt_getifa_family(info, fibnum));
607 	if (info->rti_ifa == NULL) {
608 		const struct sockaddr *sa;
609 
610 		/*
611 		 * Most common use case for the userland-supplied routes.
612 		 *
613 		 * Choose sockaddr to select ifa.
614 		 * -- if ifp is set --
615 		 * Order of preference:
616 		 * 1) IFA address
617 		 * 2) gateway address
618 		 *   Note: for interface routes link-level gateway address
619 		 *     is specified to indicate the interface index without
620 		 *     specifying RTF_GATEWAY. In this case, ignore gateway
621 		 *   Note: gateway AF may be different from dst AF. In this case,
622 		 *   ignore gateway
623 		 * 3) final destination.
624 		 * 4) if all of these fails, try to get at least link-level ifa.
625 		 * -- else --
626 		 * try to lookup gateway or dst in the routing table to get ifa
627 		 */
628 		if (info->rti_info[RTAX_IFA] != NULL)
629 			sa = info->rti_info[RTAX_IFA];
630 		else if ((info->rti_flags & RTF_GATEWAY) != 0 &&
631 		    gateway->sa_family == dst->sa_family)
632 			sa = gateway;
633 		else
634 			sa = dst;
635 		if (info->rti_ifp != NULL) {
636 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
637 			/* Case 4 */
638 			if (info->rti_ifa == NULL && gateway != NULL)
639 				info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp);
640 		} else if (dst != NULL && gateway != NULL)
641 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
642 							fibnum);
643 		else if (sa != NULL)
644 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
645 							fibnum);
646 	}
647 	if (info->rti_ifa != NULL) {
648 		if (info->rti_ifp == NULL)
649 			info->rti_ifp = info->rti_ifa->ifa_ifp;
650 	} else
651 		error = ENETUNREACH;
652 	return (error);
653 }
654 
655 void
656 rt_updatemtu(struct ifnet *ifp)
657 {
658 	struct rib_head *rnh;
659 	int mtu;
660 	int i, j;
661 
662 	/*
663 	 * Try to update rt_mtu for all routes using this interface
664 	 * Unfortunately the only way to do this is to traverse all
665 	 * routing tables in all fibs/domains.
666 	 */
667 	for (i = 1; i <= AF_MAX; i++) {
668 		mtu = if_getmtu_family(ifp, i);
669 		for (j = 0; j < rt_numfibs; j++) {
670 			rnh = rt_tables_get_rnh(j, i);
671 			if (rnh == NULL)
672 				continue;
673 			nhops_update_ifmtu(rnh, ifp, mtu);
674 		}
675 	}
676 }
677 
678 #if 0
679 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
680 int rt_print(char *buf, int buflen, struct rtentry *rt);
681 
682 int
683 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
684 {
685 	void *paddr = NULL;
686 
687 	switch (s->sa_family) {
688 	case AF_INET:
689 		paddr = &((struct sockaddr_in *)s)->sin_addr;
690 		break;
691 	case AF_INET6:
692 		paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
693 		break;
694 	}
695 
696 	if (paddr == NULL)
697 		return (0);
698 
699 	if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
700 		return (0);
701 
702 	return (strlen(buf));
703 }
704 
705 int
706 rt_print(char *buf, int buflen, struct rtentry *rt)
707 {
708 	struct sockaddr *addr, *mask;
709 	int i = 0;
710 
711 	addr = rt_key(rt);
712 	mask = rt_mask(rt);
713 
714 	i = p_sockaddr(buf, buflen, addr);
715 	if (!(rt->rt_flags & RTF_HOST)) {
716 		buf[i++] = '/';
717 		i += p_sockaddr(buf + i, buflen - i, mask);
718 	}
719 
720 	if (rt->rt_flags & RTF_GATEWAY) {
721 		buf[i++] = '>';
722 		i += p_sockaddr(buf + i, buflen - i, &rt->rt_nhop->gw_sa);
723 	}
724 
725 	return (i);
726 }
727 #endif
728 
729 void
730 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst, struct sockaddr *netmask)
731 {
732 	u_char *cp1 = (u_char *)src;
733 	u_char *cp2 = (u_char *)dst;
734 	u_char *cp3 = (u_char *)netmask;
735 	u_char *cplim = cp2 + *cp3;
736 	u_char *cplim2 = cp2 + *cp1;
737 
738 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
739 	cp3 += 2;
740 	if (cplim > cplim2)
741 		cplim = cplim2;
742 	while (cp2 < cplim)
743 		*cp2++ = *cp1++ & *cp3++;
744 	if (cp2 < cplim2)
745 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
746 }
747 
748 /*
749  * Announce interface address arrival/withdraw
750  * Returns 0 on success.
751  */
752 int
753 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
754 {
755 #if defined(INET) || defined(INET6)
756 	struct sockaddr *sa = ifa->ifa_addr;
757 	struct ifnet *ifp = ifa->ifa_ifp;
758 #endif
759 
760 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
761 	    ("unexpected cmd %d", cmd));
762 	KASSERT((fibnum >= 0 && fibnum < rt_numfibs),
763 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
764 
765 	EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd);
766 
767 #ifdef INET
768 	if (sa->sa_family == AF_INET) {
769 		char addrstr[INET_ADDRSTRLEN];
770 		char strbuf[INET_ADDRSTRLEN + 12];
771 
772 		inet_ntoa_r(((struct sockaddr_in *)sa)->sin_addr, addrstr);
773 		snprintf(strbuf, sizeof(strbuf), "address=%s", addrstr);
774 		devctl_notify("IFNET", ifp->if_xname,
775 		    (cmd == RTM_ADD) ? "ADDR_ADD" : "ADDR_DEL", strbuf);
776 	}
777 #endif
778 #ifdef INET6
779 	if (sa->sa_family == AF_INET6) {
780 		char addrstr[INET6_ADDRSTRLEN];
781 		char strbuf[INET6_ADDRSTRLEN + 12];
782 
783 		ip6_sprintf(addrstr, IFA_IN6(ifa));
784 		snprintf(strbuf, sizeof(strbuf), "address=%s", addrstr);
785 		devctl_notify("IFNET", ifp->if_xname,
786 		    (cmd == RTM_ADD) ? "ADDR_ADD" : "ADDR_DEL", strbuf);
787 	}
788 #endif
789 
790 	if (V_rt_add_addr_allfibs)
791 		fibnum = RT_ALL_FIBS;
792 	return (rtsock_addrmsg(cmd, ifa, fibnum));
793 }
794 
795 /*
796  * Announce kernel-originated route addition/removal to rtsock based on @rt data.
797  * cmd: RTM_ cmd
798  * @rt: valid rtentry
799  * @nh: nhop object to announce
800  * @fibnum: fib id or RT_ALL_FIBS
801  *
802  * Returns 0 on success.
803  */
804 int
805 rt_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
806     int fibnum)
807 {
808 
809 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
810 	    ("unexpected cmd %d", cmd));
811 
812 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
813 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
814 
815 	KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
816 
817 	return (rtsock_routemsg(cmd, rt, nh, fibnum));
818 }
819 
820 /*
821  * Announce kernel-originated route addition/removal to rtsock based on @rt data.
822  * cmd: RTM_ cmd
823  * @info: addrinfo structure with valid data.
824  * @fibnum: fib id or RT_ALL_FIBS
825  *
826  * Returns 0 on success.
827  */
828 int
829 rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
830 {
831 
832 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
833 	    ("unexpected cmd %d", cmd));
834 
835 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
836 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
837 
838 	KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__));
839 
840 	return (rtsock_routemsg_info(cmd, info, fibnum));
841 }
842