xref: /freebsd/sys/net/route.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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  */
33 /************************************************************************
34  * Note: In this file a 'fib' is a "forwarding information base"	*
35  * Which is the new name for an in kernel routing (next hop) table.	*
36  ***********************************************************************/
37 
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_mrouting.h"
41 #include "opt_route.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/sysproto.h>
51 #include <sys/proc.h>
52 #include <sys/devctl.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_private.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 
85 /*
86  * route initialization must occur before ip6_init2(), which happenas at
87  * SI_ORDER_MIDDLE.
88  */
89 static void
90 route_init(void)
91 {
92 
93 	nhops_init();
94 }
95 SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, NULL);
96 
97 struct rib_head *
98 rt_table_init(int offset, int family, u_int fibnum)
99 {
100 	struct rib_head *rh;
101 
102 	rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO);
103 
104 	/* TODO: These details should be hidded inside radix.c */
105 	/* Init masks tree */
106 	rn_inithead_internal(&rh->head, rh->rnh_nodes, offset);
107 	rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0);
108 	rh->head.rnh_masks = &rh->rmhead;
109 
110 	/* Save metadata associated with this routing table. */
111 	rh->rib_family = family;
112 	rh->rib_fibnum = fibnum;
113 #ifdef VIMAGE
114 	rh->rib_vnet = curvnet;
115 #endif
116 
117 	tmproutes_init(rh);
118 
119 	/* Init locks */
120 	RIB_LOCK_INIT(rh);
121 
122 	nhops_init_rib(rh);
123 
124 	/* Init subscription system */
125 	rib_init_subscriptions(rh);
126 
127 	/* Finally, set base callbacks */
128 	rh->rnh_addaddr = rn_addroute;
129 	rh->rnh_deladdr = rn_delete;
130 	rh->rnh_matchaddr = rn_match;
131 	rh->rnh_lookup = rn_lookup;
132 	rh->rnh_walktree = rn_walktree;
133 	rh->rnh_walktree_from = rn_walktree_from;
134 
135 	return (rh);
136 }
137 
138 static int
139 rt_freeentry(struct radix_node *rn, void *arg)
140 {
141 	struct radix_head * const rnh = arg;
142 	struct radix_node *x;
143 
144 	x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh);
145 	if (x != NULL)
146 		R_Free(x);
147 	return (0);
148 }
149 
150 void
151 rt_table_destroy(struct rib_head *rh)
152 {
153 
154 	RIB_WLOCK(rh);
155 	rh->rib_dying = true;
156 	RIB_WUNLOCK(rh);
157 
158 #ifdef FIB_ALGO
159 	fib_destroy_rib(rh);
160 #endif
161 
162 	tmproutes_destroy(rh);
163 
164 	rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head);
165 
166 	nhops_destroy_rib(rh);
167 
168 	rib_destroy_subscriptions(rh);
169 
170 	/* Assume table is already empty */
171 	RIB_LOCK_DESTROY(rh);
172 	free(rh, M_RTABLE);
173 }
174 
175 /*
176  * Adds a temporal redirect entry to the routing table.
177  * @fibnum: fib number
178  * @dst: destination to install redirect to
179  * @gateway: gateway to go via
180  * @author: sockaddr of originating router, can be NULL
181  * @ifp: interface to use for the redirected route
182  * @flags: set of flags to add. Allowed: RTF_GATEWAY
183  * @lifetime_sec: time in seconds to expire this redirect.
184  *
185  * Retuns 0 on success, errno otherwise.
186  */
187 int
188 rib_add_redirect(u_int fibnum, struct sockaddr *dst, struct sockaddr *gateway,
189     struct sockaddr *author, struct ifnet *ifp, int flags, int lifetime_sec)
190 {
191 	struct route_nhop_data rnd = { .rnd_weight = RT_DEFAULT_WEIGHT };
192 	struct rib_cmd_info rc;
193 	struct ifaddr *ifa;
194 	int error;
195 
196 	NET_EPOCH_ASSERT();
197 
198 	if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL)
199 		return (EAFNOSUPPORT);
200 
201 	/* Verify the allowed flag mask. */
202 	KASSERT(((flags & ~(RTF_GATEWAY)) == 0),
203 	    ("invalid redirect flags: %x", flags));
204 	flags |= RTF_HOST | RTF_DYNAMIC;
205 
206 	/* Get the best ifa for the given interface and gateway. */
207 	if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL)
208 		return (ENETUNREACH);
209 
210 	struct nhop_object *nh = nhop_alloc(fibnum, dst->sa_family);
211 	if (nh == NULL)
212 		return (ENOMEM);
213 
214 	nhop_set_gw(nh, gateway, flags & RTF_GATEWAY);
215 	nhop_set_transmit_ifp(nh, ifp);
216 	nhop_set_src(nh, ifa);
217 	nhop_set_pxtype_flag(nh, NHF_HOST);
218 	nhop_set_expire(nh, lifetime_sec + time_uptime);
219 	nhop_set_redirect(nh, true);
220 	nhop_set_origin(nh, NH_ORIGIN_REDIRECT);
221 	rnd.rnd_nhop = nhop_get_nhop(nh, &error);
222 	if (error == 0) {
223 		error = rib_add_route_px(fibnum, dst, -1,
224 		    &rnd, RTM_F_CREATE, &rc);
225 	}
226 
227 	if (error != 0) {
228 		/* TODO: add per-fib redirect stats. */
229 		return (error);
230 	}
231 
232 	RTSTAT_INC(rts_dynamic);
233 
234 	/* Send notification of a route addition to userland. */
235 	struct rt_addrinfo info = {
236 		.rti_info[RTAX_DST] = dst,
237 		.rti_info[RTAX_GATEWAY] = gateway,
238 		.rti_info[RTAX_AUTHOR] = author,
239 	};
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)
250 {
251 
252 	/*
253 	 * If more ioctl commands are added here, make sure the proper
254 	 * super-user checks are being performed because it is possible for
255 	 * prison-root to make it this far if raw sockets have been enabled
256 	 * in jails.
257 	 */
258 #ifdef INET
259 	/* Multicast goop, grrr... */
260 	return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
261 #else /* INET */
262 	return ENXIO;
263 #endif /* INET */
264 }
265 
266 struct ifaddr *
267 ifa_ifwithroute(int flags, const struct sockaddr *dst,
268     const struct sockaddr *gateway, u_int fibnum)
269 {
270 	struct ifaddr *ifa;
271 
272 	NET_EPOCH_ASSERT();
273 	if ((flags & RTF_GATEWAY) == 0) {
274 		/*
275 		 * If we are adding a route to an interface,
276 		 * and the interface is a pt to pt link
277 		 * we should search for the destination
278 		 * as our clue to the interface.  Otherwise
279 		 * we can use the local address.
280 		 */
281 		ifa = NULL;
282 		if (flags & RTF_HOST)
283 			ifa = ifa_ifwithdstaddr(dst, fibnum);
284 		if (ifa == NULL)
285 			ifa = ifa_ifwithaddr(gateway);
286 	} else {
287 		/*
288 		 * If we are adding a route to a remote net
289 		 * or host, the gateway may still be on the
290 		 * other end of a pt to pt link.
291 		 */
292 		ifa = ifa_ifwithdstaddr(gateway, fibnum);
293 	}
294 	if (ifa == NULL)
295 		ifa = ifa_ifwithnet(gateway, 0, fibnum);
296 	if (ifa == NULL) {
297 		struct nhop_object *nh;
298 
299 		nh = rib_lookup(fibnum, gateway, NHR_NONE, 0);
300 
301 		/*
302 		 * dismiss a gateway that is reachable only
303 		 * through the default router
304 		 */
305 		if ((nh == NULL) || (nh->nh_flags & NHF_DEFAULT))
306 			return (NULL);
307 		ifa = nh->nh_ifa;
308 	}
309 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
310 		struct ifaddr *oifa = ifa;
311 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
312 		if (ifa == NULL)
313 			ifa = oifa;
314 	}
315 
316 	return (ifa);
317 }
318 
319 /*
320  * Delete Routes for a Network Interface
321  *
322  * Called for each routing entry via the rnh->rnh_walktree() call above
323  * to delete all route entries referencing a detaching network interface.
324  *
325  * Arguments:
326  *	rt	pointer to rtentry
327  *	nh	pointer to nhop
328  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
329  *
330  * Returns:
331  *	0	successful
332  *	errno	failed - reason indicated
333  */
334 static int
335 rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
336 {
337 	struct ifnet	*ifp = arg;
338 
339 	if (nh->nh_ifp != ifp)
340 		return (0);
341 
342 	/*
343 	 * Protect (sorta) against walktree recursion problems
344 	 * with cloned routes
345 	 */
346 	if ((rt->rte_flags & RTF_UP) == 0)
347 		return (0);
348 
349 	return (1);
350 }
351 
352 void
353 rt_flushifroutes(struct ifnet *ifp)
354 {
355 
356 	rib_foreach_table_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
357 }
358 
359 /*
360  * Tries to extract interface from RTAX_IFP passed in rt_addrinfo.
361  * Interface can be specified ether as interface index (sdl_index) or
362  * the interface name (sdl_data).
363  *
364  * Returns found ifp or NULL
365  */
366 static struct ifnet *
367 info_get_ifp(struct rt_addrinfo *info)
368 {
369 	const struct sockaddr_dl *sdl;
370 
371 	sdl = (const struct sockaddr_dl *)info->rti_info[RTAX_IFP];
372 	if (sdl->sdl_family != AF_LINK)
373 		return (NULL);
374 
375 	if (sdl->sdl_index != 0)
376 		return (ifnet_byindex(sdl->sdl_index));
377 	if (sdl->sdl_nlen > 0) {
378 		char if_name[IF_NAMESIZE];
379 		if (sdl->sdl_nlen + offsetof(struct sockaddr_dl, sdl_data) > sdl->sdl_len)
380 			return (NULL);
381 		if (sdl->sdl_nlen >= IF_NAMESIZE)
382 			return (NULL);
383 		bzero(if_name, sizeof(if_name));
384 		memcpy(if_name, sdl->sdl_data, sdl->sdl_nlen);
385 		return (ifunit(if_name));
386 	}
387 
388 	return (NULL);
389 }
390 
391 /*
392  * Calculates proper ifa/ifp for the cases when gateway AF is different
393  * from dst AF.
394  *
395  * Returns 0 on success.
396  */
397 __noinline static int
398 rt_getifa_family(struct rt_addrinfo *info, uint32_t fibnum)
399 {
400 	if (info->rti_ifp == NULL) {
401 		struct ifaddr *ifa = NULL;
402 		/*
403 		 * No transmit interface specified. Guess it by checking gw sa.
404 		 */
405 		const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
406 		ifa = ifa_ifwithroute(RTF_GATEWAY, gw, gw, fibnum);
407 		if (ifa == NULL)
408 			return (ENETUNREACH);
409 		info->rti_ifp = ifa->ifa_ifp;
410 	}
411 
412 	/* Prefer address from outgoing interface */
413 	info->rti_ifa = ifaof_ifpforaddr(info->rti_info[RTAX_DST], info->rti_ifp);
414 #ifdef INET
415 	if (info->rti_ifa == NULL) {
416 		/* Use first found IPv4 address */
417 		bool loopback_ok = info->rti_ifp->if_flags & IFF_LOOPBACK;
418 		info->rti_ifa = (struct ifaddr *)in_findlocal(fibnum, loopback_ok);
419 	}
420 #endif
421 	if (info->rti_ifa == NULL)
422 		return (ENETUNREACH);
423 	return (0);
424 }
425 
426 /*
427  * Fills in rti_ifp and rti_ifa for the provided fib.
428  *
429  * Assume basic consistency checks are executed by callers:
430  * RTAX_DST exists, if RTF_GATEWAY is set, RTAX_GATEWAY exists as well.
431  */
432 int
433 rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
434 {
435 	const struct sockaddr *dst, *gateway, *ifaaddr;
436 	int error, flags;
437 
438 	dst = info->rti_info[RTAX_DST];
439 	gateway = info->rti_info[RTAX_GATEWAY];
440 	ifaaddr = info->rti_info[RTAX_IFA];
441 	flags = info->rti_flags;
442 
443 	/*
444 	 * ifp may be specified by sockaddr_dl
445 	 * when protocol address is ambiguous.
446 	 */
447 	error = 0;
448 
449 	/* If we have interface specified by RTAX_IFP address, try to use it */
450 	if ((info->rti_ifp == NULL) && (info->rti_info[RTAX_IFP] != NULL))
451 		info->rti_ifp = info_get_ifp(info);
452 	/*
453 	 * If we have source address specified, try to find it
454 	 * TODO: avoid enumerating all ifas on all interfaces.
455 	 */
456 	if (info->rti_ifa == NULL && ifaaddr != NULL)
457 		info->rti_ifa = ifa_ifwithaddr(ifaaddr);
458 	if ((info->rti_ifa == NULL) && ((info->rti_flags & RTF_GATEWAY) != 0) &&
459 	    (gateway->sa_family != dst->sa_family))
460 		return (rt_getifa_family(info, fibnum));
461 	if (info->rti_ifa == NULL) {
462 		const struct sockaddr *sa;
463 
464 		/*
465 		 * Most common use case for the userland-supplied routes.
466 		 *
467 		 * Choose sockaddr to select ifa.
468 		 * -- if ifp is set --
469 		 * Order of preference:
470 		 * 1) IFA address
471 		 * 2) gateway address
472 		 *   Note: for interface routes link-level gateway address
473 		 *     is specified to indicate the interface index without
474 		 *     specifying RTF_GATEWAY. In this case, ignore gateway
475 		 *   Note: gateway AF may be different from dst AF. In this case,
476 		 *   ignore gateway
477 		 * 3) final destination.
478 		 * 4) if all of these fails, try to get at least link-level ifa.
479 		 * -- else --
480 		 * try to lookup gateway or dst in the routing table to get ifa
481 		 */
482 		if (info->rti_info[RTAX_IFA] != NULL)
483 			sa = info->rti_info[RTAX_IFA];
484 		else if ((info->rti_flags & RTF_GATEWAY) != 0 &&
485 		    gateway->sa_family == dst->sa_family)
486 			sa = gateway;
487 		else
488 			sa = dst;
489 		if (info->rti_ifp != NULL) {
490 			info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
491 			/* Case 4 */
492 			if (info->rti_ifa == NULL && gateway != NULL)
493 				info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp);
494 		} else if (dst != NULL && gateway != NULL)
495 			info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
496 							fibnum);
497 		else if (sa != NULL)
498 			info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
499 							fibnum);
500 	}
501 	if (info->rti_ifa != NULL) {
502 		if (info->rti_ifp == NULL)
503 			info->rti_ifp = info->rti_ifa->ifa_ifp;
504 	} else
505 		error = ENETUNREACH;
506 	return (error);
507 }
508 
509 void
510 rt_updatemtu(struct ifnet *ifp)
511 {
512 	struct rib_head *rnh;
513 	int mtu;
514 	int i, j;
515 
516 	/*
517 	 * Try to update rt_mtu for all routes using this interface
518 	 * Unfortunately the only way to do this is to traverse all
519 	 * routing tables in all fibs/domains.
520 	 */
521 	for (i = 1; i <= AF_MAX; i++) {
522 		mtu = if_getmtu_family(ifp, i);
523 		for (j = 0; j < rt_numfibs; j++) {
524 			rnh = rt_tables_get_rnh(j, i);
525 			if (rnh == NULL)
526 				continue;
527 			nhops_update_ifmtu(rnh, ifp, mtu);
528 		}
529 	}
530 }
531 
532 #if 0
533 int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
534 int rt_print(char *buf, int buflen, struct rtentry *rt);
535 
536 int
537 p_sockaddr(char *buf, int buflen, struct sockaddr *s)
538 {
539 	void *paddr = NULL;
540 
541 	switch (s->sa_family) {
542 	case AF_INET:
543 		paddr = &((struct sockaddr_in *)s)->sin_addr;
544 		break;
545 	case AF_INET6:
546 		paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
547 		break;
548 	}
549 
550 	if (paddr == NULL)
551 		return (0);
552 
553 	if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
554 		return (0);
555 
556 	return (strlen(buf));
557 }
558 
559 int
560 rt_print(char *buf, int buflen, struct rtentry *rt)
561 {
562 	struct sockaddr *addr, *mask;
563 	int i = 0;
564 
565 	addr = rt_key(rt);
566 	mask = rt_mask(rt);
567 
568 	i = p_sockaddr(buf, buflen, addr);
569 	if (!(rt->rt_flags & RTF_HOST)) {
570 		buf[i++] = '/';
571 		i += p_sockaddr(buf + i, buflen - i, mask);
572 	}
573 
574 	if (rt->rt_flags & RTF_GATEWAY) {
575 		buf[i++] = '>';
576 		i += p_sockaddr(buf + i, buflen - i, &rt->rt_nhop->gw_sa);
577 	}
578 
579 	return (i);
580 }
581 #endif
582 
583 void
584 rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
585     const struct sockaddr *netmask)
586 {
587 	const u_char *cp1 = (const u_char *)src;
588 	u_char *cp2 = (u_char *)dst;
589 	const u_char *cp3 = (const u_char *)netmask;
590 	u_char *cplim = cp2 + *cp3;
591 	u_char *cplim2 = cp2 + *cp1;
592 
593 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
594 	cp3 += 2;
595 	if (cplim > cplim2)
596 		cplim = cplim2;
597 	while (cp2 < cplim)
598 		*cp2++ = *cp1++ & *cp3++;
599 	if (cp2 < cplim2)
600 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
601 }
602 
603 /*
604  * Announce interface address arrival/withdraw
605  * Returns 0 on success.
606  */
607 int
608 rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
609 {
610 #if defined(INET) || defined(INET6)
611 	struct sockaddr *sa = ifa->ifa_addr;
612 	struct ifnet *ifp = ifa->ifa_ifp;
613 #endif
614 
615 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
616 	    ("unexpected cmd %d", cmd));
617 	KASSERT((fibnum >= 0 && fibnum < rt_numfibs),
618 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
619 
620 	EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd);
621 
622 #ifdef INET
623 	if (sa->sa_family == AF_INET) {
624 		char addrstr[INET_ADDRSTRLEN];
625 		char strbuf[INET_ADDRSTRLEN + 12];
626 
627 		inet_ntoa_r(((struct sockaddr_in *)sa)->sin_addr, addrstr);
628 		snprintf(strbuf, sizeof(strbuf), "address=%s", addrstr);
629 		devctl_notify("IFNET", ifp->if_xname,
630 		    (cmd == RTM_ADD) ? "ADDR_ADD" : "ADDR_DEL", strbuf);
631 	}
632 #endif
633 #ifdef INET6
634 	if (sa->sa_family == AF_INET6) {
635 		char addrstr[INET6_ADDRSTRLEN];
636 		char strbuf[INET6_ADDRSTRLEN + 12];
637 
638 		ip6_sprintf(addrstr, IFA_IN6(ifa));
639 		snprintf(strbuf, sizeof(strbuf), "address=%s", addrstr);
640 		devctl_notify("IFNET", ifp->if_xname,
641 		    (cmd == RTM_ADD) ? "ADDR_ADD" : "ADDR_DEL", strbuf);
642 	}
643 #endif
644 
645 	if (V_rt_add_addr_allfibs)
646 		fibnum = RT_ALL_FIBS;
647 	return (rtsock_addrmsg(cmd, ifa, fibnum));
648 }
649 
650 /*
651  * Announce kernel-originated route addition/removal to rtsock based on @rt data.
652  * cmd: RTM_ cmd
653  * @rt: valid rtentry
654  * @nh: nhop object to announce
655  * @fibnum: fib id or RT_ALL_FIBS
656  *
657  * Returns 0 on success.
658  */
659 int
660 rt_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
661     int fibnum)
662 {
663 
664 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
665 	    ("unexpected cmd %d", cmd));
666 
667 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
668 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
669 
670 	KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
671 
672 	return (rtsock_routemsg(cmd, rt, nh, fibnum));
673 }
674 
675 /*
676  * Announce kernel-originated route addition/removal to rtsock based on @rt data.
677  * cmd: RTM_ cmd
678  * @info: addrinfo structure with valid data.
679  * @fibnum: fib id or RT_ALL_FIBS
680  *
681  * Returns 0 on success.
682  */
683 int
684 rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
685 {
686 
687 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
688 	    ("unexpected cmd %d", cmd));
689 
690 	KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
691 	    ("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
692 
693 	KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__));
694 
695 	return (rtsock_routemsg_info(cmd, info, fibnum));
696 }
697 
698 void
699 rt_ifmsg(struct ifnet *ifp, int if_flags_mask)
700 {
701 	rtsock_callback_p->ifmsg_f(ifp, if_flags_mask);
702 	netlink_callback_p->ifmsg_f(ifp, if_flags_mask);
703 }
704 
705