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