xref: /freebsd/sys/net/route.c (revision 254b23eb1f540844cf2a90f2781ae4231c5701ce)
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 /************************************************************************
32  * Note: In this file a 'fib' is a "forwarding information base"	*
33  * Which is the new name for an in kernel routing (next hop) table.	*
34  ***********************************************************************/
35 
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_mrouting.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/sysproto.h>
48 #include <sys/proc.h>
49 #include <sys/devctl.h>
50 #include <sys/domain.h>
51 #include <sys/eventhandler.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/rmlock.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_private.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <net/route/route_ctl.h>
62 #include <net/route/route_var.h>
63 #include <net/route/nhop.h>
64 #include <net/vnet.h>
65 
66 #include <netinet/in.h>
67 #include <netinet/ip_mroute.h>
68 #include <netinet6/in6_var.h>
69 
70 VNET_PCPUSTAT_DEFINE(struct rtstat, rtstat);
71 
72 VNET_PCPUSTAT_SYSINIT(rtstat);
73 #ifdef VIMAGE
74 VNET_PCPUSTAT_SYSUNINIT(rtstat);
75 #endif
76 
77 SYSCTL_DECL(_net_route);
78 SYSCTL_VNET_PCPUSTAT(_net_route, OID_AUTO, stats, struct rtstat,
79     rtstat, "route statistics");
80 
81 EVENTHANDLER_LIST_DEFINE(rt_addrmsg);
82 
83 static int rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *,
84     void *arg);
85 
86 /*
87  * route initialization must occur before ip6_init2(), which happenas at
88  * SI_ORDER_MIDDLE.
89  */
90 static void
route_init(void * dummy __unused)91 route_init(void *dummy __unused)
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 *
rt_table_init(int offset,int family,u_int fibnum)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
rt_freeentry(struct radix_node * rn,void * arg)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
rt_table_destroy(struct rib_head * rh)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
rib_add_redirect(u_int fibnum,struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * author,struct ifnet * ifp,int flags,int lifetime_sec)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 route_nhop_data rnd = { .rnd_weight = RT_DEFAULT_WEIGHT };
193 	struct rib_cmd_info rc;
194 	struct ifaddr *ifa;
195 	int error;
196 
197 	NET_EPOCH_ASSERT();
198 
199 	if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL)
200 		return (EAFNOSUPPORT);
201 
202 	/* Verify the allowed flag mask. */
203 	KASSERT(((flags & ~(RTF_GATEWAY)) == 0),
204 	    ("invalid redirect flags: %x", flags));
205 	flags |= RTF_HOST | RTF_DYNAMIC;
206 
207 	/* Get the best ifa for the given interface and gateway. */
208 	if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL)
209 		return (ENETUNREACH);
210 
211 	struct nhop_object *nh = nhop_alloc(fibnum, dst->sa_family);
212 	if (nh == NULL)
213 		return (ENOMEM);
214 
215 	nhop_set_gw(nh, gateway, flags & RTF_GATEWAY);
216 	nhop_set_transmit_ifp(nh, ifp);
217 	nhop_set_src(nh, ifa);
218 	nhop_set_pxtype_flag(nh, NHF_HOST);
219 	nhop_set_expire(nh, lifetime_sec + time_uptime);
220 	nhop_set_redirect(nh, true);
221 	nhop_set_origin(nh, NH_ORIGIN_REDIRECT);
222 	rnd.rnd_nhop = nhop_get_nhop(nh, &error);
223 	if (error == 0) {
224 		error = rib_add_route_px(fibnum, dst, -1,
225 		    &rnd, RTM_F_CREATE, &rc);
226 	}
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 	struct rt_addrinfo info = {
237 		.rti_info[RTAX_DST] = dst,
238 		.rti_info[RTAX_GATEWAY] = gateway,
239 		.rti_info[RTAX_AUTHOR] = author,
240 	};
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
rtioctl_fib(u_long req,caddr_t data,u_int fibnum)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 *
ifa_ifwithroute(int flags,const struct sockaddr * dst,const struct sockaddr * gateway,u_int fibnum)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  * Delete Routes for a Network Interface
322  *
323  * Called for each routing entry via the rnh->rnh_walktree() call above
324  * to delete all route entries referencing a detaching network interface.
325  *
326  * Arguments:
327  *	rt	pointer to rtentry
328  *	nh	pointer to nhop
329  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
330  *
331  * Returns:
332  *	0	successful
333  *	errno	failed - reason indicated
334  */
335 static int
rt_ifdelroute(const struct rtentry * rt,const struct nhop_object * nh,void * arg)336 rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
337 {
338 	struct ifnet	*ifp = arg;
339 
340 	if (nh->nh_ifp != ifp)
341 		return (0);
342 
343 	/*
344 	 * Protect (sorta) against walktree recursion problems
345 	 * with cloned routes
346 	 */
347 	if ((rt->rte_flags & RTF_UP) == 0)
348 		return (0);
349 
350 	return (1);
351 }
352 
353 void
rt_flushifroutes(struct ifnet * ifp)354 rt_flushifroutes(struct ifnet *ifp)
355 {
356 
357 	rib_foreach_table_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
358 }
359 
360 /*
361  * Tries to extract interface from RTAX_IFP passed in rt_addrinfo.
362  * Interface can be specified ether as interface index (sdl_index) or
363  * the interface name (sdl_data).
364  *
365  * Returns found ifp or NULL
366  */
367 static struct ifnet *
info_get_ifp(struct rt_addrinfo * info)368 info_get_ifp(struct rt_addrinfo *info)
369 {
370 	const struct sockaddr_dl *sdl;
371 
372 	sdl = (const struct sockaddr_dl *)info->rti_info[RTAX_IFP];
373 	if (sdl->sdl_family != AF_LINK)
374 		return (NULL);
375 
376 	if (sdl->sdl_index != 0)
377 		return (ifnet_byindex(sdl->sdl_index));
378 	if (sdl->sdl_nlen > 0) {
379 		char if_name[IF_NAMESIZE];
380 		if (sdl->sdl_nlen + offsetof(struct sockaddr_dl, sdl_data) > sdl->sdl_len)
381 			return (NULL);
382 		if (sdl->sdl_nlen >= IF_NAMESIZE)
383 			return (NULL);
384 		bzero(if_name, sizeof(if_name));
385 		memcpy(if_name, sdl->sdl_data, sdl->sdl_nlen);
386 		return (ifunit(if_name));
387 	}
388 
389 	return (NULL);
390 }
391 
392 /*
393  * Calculates proper ifa/ifp for the cases when gateway AF is different
394  * from dst AF.
395  *
396  * Returns 0 on success.
397  */
398 __noinline static int
rt_getifa_family(struct rt_addrinfo * info,uint32_t fibnum)399 rt_getifa_family(struct rt_addrinfo *info, uint32_t fibnum)
400 {
401 	if (info->rti_ifp == NULL) {
402 		struct ifaddr *ifa = NULL;
403 		/*
404 		 * No transmit interface specified. Guess it by checking gw sa.
405 		 */
406 		const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
407 		ifa = ifa_ifwithroute(RTF_GATEWAY, gw, gw, fibnum);
408 		if (ifa == NULL)
409 			return (ENETUNREACH);
410 		info->rti_ifp = ifa->ifa_ifp;
411 	}
412 
413 	/* Prefer address from outgoing interface */
414 	info->rti_ifa = ifaof_ifpforaddr(info->rti_info[RTAX_DST], info->rti_ifp);
415 #ifdef INET
416 	if (info->rti_ifa == NULL) {
417 		/* Use first found IPv4 address */
418 		bool loopback_ok = info->rti_ifp->if_flags & IFF_LOOPBACK;
419 		info->rti_ifa = (struct ifaddr *)in_findlocal(fibnum, loopback_ok);
420 	}
421 #endif
422 	if (info->rti_ifa == NULL)
423 		return (ENETUNREACH);
424 	return (0);
425 }
426 
427 /*
428  * Fills in rti_ifp and rti_ifa for the provided fib.
429  *
430  * Assume basic consistency checks are executed by callers:
431  * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well.
432  */
433 int
rt_getifa_fib(struct rt_addrinfo * info,u_int fibnum)434 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
435 {
436 	const struct sockaddr *dst, *gateway, *ifaaddr;
437 	int error, flags;
438 
439 	dst = info->rti_info[RTAX_DST];
440 	gateway = info->rti_info[RTAX_GATEWAY];
441 	ifaaddr = info->rti_info[RTAX_IFA];
442 	flags = info->rti_flags;
443 
444 	/*
445 	 * ifp may be specified by sockaddr_dl
446 	 * when protocol address is ambiguous.
447 	 */
448 	error = 0;
449 
450 	/* If we have interface specified by RTAX_IFP address, try to use it */
451 	if ((info->rti_ifp == NULL) && (info->rti_info[RTAX_IFP] != NULL))
452 		info->rti_ifp = info_get_ifp(info);
453 	/*
454 	 * If we have source address specified, try to find it
455 	 * TODO: avoid enumerating all ifas on all interfaces.
456 	 */
457 	if (info->rti_ifa == NULL && ifaaddr != NULL)
458 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
459 	if ((info->rti_ifa == NULL) && ((info->rti_flags & RTF_GATEWAY) != 0) &&
460 	    (gateway->sa_family != dst->sa_family))
461 		return (rt_getifa_family(info, fibnum));
462 	if (info->rti_ifa == NULL) {
463 		const struct sockaddr *sa;
464 
465 		/*
466 		 * Most common use case for the userland-supplied routes.
467 		 *
468 		 * Choose sockaddr to select ifa.
469 		 * -- if ifp is set --
470 		 * Order of preference:
471 		 * 1) IFA address
472 		 * 2) gateway address
473 		 *   Note: for interface routes link-level gateway address
474 		 *     is specified to indicate the interface index without
475 		 *     specifying RTF_GATEWAY. In this case, ignore gateway
476 		 *   Note: gateway AF may be different from dst AF. In this case,
477 		 *   ignore gateway
478 		 * 3) final destination.
479 		 * 4) if all of these fails, try to get at least link-level ifa.
480 		 * -- else --
481 		 * try to lookup gateway or dst in the routing table to get ifa
482 		 */
483 		if (info->rti_info[RTAX_IFA] != NULL)
484 			sa = info->rti_info[RTAX_IFA];
485 		else if ((info->rti_flags & RTF_GATEWAY) != 0 &&
486 		    gateway->sa_family == dst->sa_family)
487 			sa = gateway;
488 		else
489 			sa = dst;
490 		if (info->rti_ifp != NULL) {
491 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
492 			/* Case 4 */
493 			if (info->rti_ifa == NULL && gateway != NULL)
494 				info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp);
495 		} else if (dst != NULL && gateway != NULL)
496 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
497 							fibnum);
498 		else if (sa != NULL)
499 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
500 							fibnum);
501 	}
502 	if (info->rti_ifa != NULL) {
503 		if (info->rti_ifp == NULL)
504 			info->rti_ifp = info->rti_ifa->ifa_ifp;
505 	} else
506 		error = ENETUNREACH;
507 	return (error);
508 }
509 
510 /*
511  * Try to update rt_mtu for all routes using this interface.  Unfortunately the
512  * only way to do this is to traverse all routing tables in all fibs.
513  */
514 void
rt_updatemtu(struct ifnet * ifp)515 rt_updatemtu(struct ifnet *ifp)
516 {
517 #ifdef INET6
518 	uint32_t in6mtu;
519 
520 	in6mtu = in6_ifmtu(ifp);
521 #endif
522 
523 	for (u_int j = 0; j < rt_numfibs; j++) {
524 #ifdef INET
525 		nhops_update_ifmtu(rt_tables_get_rnh(j, AF_INET), ifp,
526 		    ifp->if_mtu);
527 #endif
528 #ifdef INET6
529 		nhops_update_ifmtu(rt_tables_get_rnh(j, AF_INET6), ifp, in6mtu);
530 #endif
531 	}
532 }
533 
534 #if 0
535 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
536 int rt_print(char *buf, int buflen, struct rtentry *rt);
537 
538 int
539 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
540 {
541 	void *paddr = NULL;
542 
543 	switch (s->sa_family) {
544 	case AF_INET:
545 		paddr = &((struct sockaddr_in *)s)->sin_addr;
546 		break;
547 	case AF_INET6:
548 		paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
549 		break;
550 	}
551 
552 	if (paddr == NULL)
553 		return (0);
554 
555 	if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
556 		return (0);
557 
558 	return (strlen(buf));
559 }
560 
561 int
562 rt_print(char *buf, int buflen, struct rtentry *rt)
563 {
564 	struct sockaddr *addr, *mask;
565 	int i = 0;
566 
567 	addr = rt_key(rt);
568 	mask = rt_mask(rt);
569 
570 	i = p_sockaddr(buf, buflen, addr);
571 	if (!(rt->rt_flags & RTF_HOST)) {
572 		buf[i++] = '/';
573 		i += p_sockaddr(buf + i, buflen - i, mask);
574 	}
575 
576 	if (rt->rt_flags & RTF_GATEWAY) {
577 		buf[i++] = '>';
578 		i += p_sockaddr(buf + i, buflen - i, &rt->rt_nhop->gw_sa);
579 	}
580 
581 	return (i);
582 }
583 #endif
584 
585 void
rt_maskedcopy(const struct sockaddr * src,struct sockaddr * dst,const struct sockaddr * netmask)586 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
587     const struct sockaddr *netmask)
588 {
589 	const u_char *cp1 = (const u_char *)src;
590 	u_char *cp2 = (u_char *)dst;
591 	const u_char *cp3 = (const u_char *)netmask;
592 	u_char *cplim = cp2 + *cp3;
593 	u_char *cplim2 = cp2 + *cp1;
594 
595 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
596 	cp3 += 2;
597 	if (cplim > cplim2)
598 		cplim = cplim2;
599 	while (cp2 < cplim)
600 		*cp2++ = *cp1++ & *cp3++;
601 	if (cp2 < cplim2)
602 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
603 }
604 
605 /*
606  * Announce interface address arrival/withdraw
607  * Returns 0 on success.
608  */
609 int
rt_addrmsg(int cmd,struct ifaddr * ifa,int fibnum)610 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
611 {
612 #if defined(INET) || defined(INET6)
613 	struct sockaddr *sa = ifa->ifa_addr;
614 	struct ifnet *ifp = ifa->ifa_ifp;
615 #endif
616 
617 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
618 	    ("unexpected cmd %d", cmd));
619 	KASSERT((fibnum >= 0 && fibnum < rt_numfibs),
620 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
621 
622 	EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd);
623 
624 #ifdef INET
625 	if (sa->sa_family == AF_INET) {
626 		char addrstr[INET_ADDRSTRLEN];
627 		char strbuf[INET_ADDRSTRLEN + 12];
628 
629 		inet_ntoa_r(((struct sockaddr_in *)sa)->sin_addr, addrstr);
630 		snprintf(strbuf, sizeof(strbuf), "address=%s", addrstr);
631 		devctl_notify("IFNET", ifp->if_xname,
632 		    (cmd == RTM_ADD) ? "ADDR_ADD" : "ADDR_DEL", strbuf);
633 	}
634 #endif
635 #ifdef INET6
636 	if (sa->sa_family == AF_INET6) {
637 		char addrstr[INET6_ADDRSTRLEN];
638 		char strbuf[INET6_ADDRSTRLEN + 12];
639 
640 		ip6_sprintf(addrstr, IFA_IN6(ifa));
641 		snprintf(strbuf, sizeof(strbuf), "address=%s", addrstr);
642 		devctl_notify("IFNET", ifp->if_xname,
643 		    (cmd == RTM_ADD) ? "ADDR_ADD" : "ADDR_DEL", strbuf);
644 	}
645 #endif
646 
647 	if (V_rt_add_addr_allfibs)
648 		fibnum = RT_ALL_FIBS;
649 	return (rtsock_addrmsg(cmd, ifa, fibnum));
650 }
651 
652 /*
653  * Announce kernel-originated route addition/removal to rtsock based on @rt data.
654  * cmd: RTM_ cmd
655  * @rt: valid rtentry
656  * @nh: nhop object to announce
657  * @fibnum: fib id or RT_ALL_FIBS
658  *
659  * Returns 0 on success.
660  */
661 int
rt_routemsg(int cmd,struct rtentry * rt,struct nhop_object * nh,int fibnum)662 rt_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
663     int fibnum)
664 {
665 
666 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
667 	    ("unexpected cmd %d", cmd));
668 
669 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
670 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
671 
672 	KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
673 
674 	return (rtsock_routemsg(cmd, rt, nh, fibnum));
675 }
676 
677 /*
678  * Announce kernel-originated route addition/removal to rtsock based on @rt data.
679  * cmd: RTM_ cmd
680  * @info: addrinfo structure with valid data.
681  * @fibnum: fib id or RT_ALL_FIBS
682  *
683  * Returns 0 on success.
684  */
685 int
rt_routemsg_info(int cmd,struct rt_addrinfo * info,int fibnum)686 rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
687 {
688 
689 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
690 	    ("unexpected cmd %d", cmd));
691 
692 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
693 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
694 
695 	KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__));
696 
697 	return (rtsock_routemsg_info(cmd, info, fibnum));
698 }
699 
700 void
rt_ifmsg(struct ifnet * ifp,int if_flags_mask)701 rt_ifmsg(struct ifnet *ifp, int if_flags_mask)
702 {
703 	rtsock_callback_p->ifmsg_f(ifp, if_flags_mask);
704 	netlink_callback_p->ifmsg_f(ifp, if_flags_mask);
705 }
706 
707