xref: /freebsd/sys/net/rtsock.c (revision f61e92ca5a23450bc28169bbdd71d7674df98c19)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1988, 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  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
32  * $FreeBSD$
33  */
34 #include "opt_ddb.h"
35 #include "opt_route.h"
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 
39 #include <sys/param.h>
40 #include <sys/jail.h>
41 #include <sys/kernel.h>
42 #include <sys/domain.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/rmlock.h>
50 #include <sys/rwlock.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/if_llatbl.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63 #include <net/raw_cb.h>
64 #include <net/route.h>
65 #include <net/route/route_ctl.h>
66 #include <net/route/route_var.h>
67 #include <net/vnet.h>
68 
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip_carp.h>
72 #ifdef INET6
73 #include <netinet6/in6_var.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/scope6_var.h>
76 #endif
77 #include <net/route/nhop.h>
78 
79 #ifdef COMPAT_FREEBSD32
80 #include <sys/mount.h>
81 #include <compat/freebsd32/freebsd32.h>
82 
83 struct if_msghdr32 {
84 	uint16_t ifm_msglen;
85 	uint8_t	ifm_version;
86 	uint8_t	ifm_type;
87 	int32_t	ifm_addrs;
88 	int32_t	ifm_flags;
89 	uint16_t ifm_index;
90 	uint16_t _ifm_spare1;
91 	struct	if_data ifm_data;
92 };
93 
94 struct if_msghdrl32 {
95 	uint16_t ifm_msglen;
96 	uint8_t	ifm_version;
97 	uint8_t	ifm_type;
98 	int32_t	ifm_addrs;
99 	int32_t	ifm_flags;
100 	uint16_t ifm_index;
101 	uint16_t _ifm_spare1;
102 	uint16_t ifm_len;
103 	uint16_t ifm_data_off;
104 	uint32_t _ifm_spare2;
105 	struct	if_data ifm_data;
106 };
107 
108 struct ifa_msghdrl32 {
109 	uint16_t ifam_msglen;
110 	uint8_t	ifam_version;
111 	uint8_t	ifam_type;
112 	int32_t	ifam_addrs;
113 	int32_t	ifam_flags;
114 	uint16_t ifam_index;
115 	uint16_t _ifam_spare1;
116 	uint16_t ifam_len;
117 	uint16_t ifam_data_off;
118 	int32_t	ifam_metric;
119 	struct	if_data ifam_data;
120 };
121 
122 #define SA_SIZE32(sa)						\
123     (  (((struct sockaddr *)(sa))->sa_len == 0) ?		\
124 	sizeof(int)		:				\
125 	1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
126 
127 #endif /* COMPAT_FREEBSD32 */
128 
129 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
130 
131 /* NB: these are not modified */
132 static struct	sockaddr route_src = { 2, PF_ROUTE, };
133 static struct	sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
134 
135 /* These are external hooks for CARP. */
136 int	(*carp_get_vhid_p)(struct ifaddr *);
137 
138 /*
139  * Used by rtsock/raw_input callback code to decide whether to filter the update
140  * notification to a socket bound to a particular FIB.
141  */
142 #define	RTS_FILTER_FIB	M_PROTO8
143 
144 typedef struct {
145 	int	ip_count;	/* attached w/ AF_INET */
146 	int	ip6_count;	/* attached w/ AF_INET6 */
147 	int	any_count;	/* total attached */
148 } route_cb_t;
149 VNET_DEFINE_STATIC(route_cb_t, route_cb);
150 #define	V_route_cb VNET(route_cb)
151 
152 struct mtx rtsock_mtx;
153 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
154 
155 #define	RTSOCK_LOCK()	mtx_lock(&rtsock_mtx)
156 #define	RTSOCK_UNLOCK()	mtx_unlock(&rtsock_mtx)
157 #define	RTSOCK_LOCK_ASSERT()	mtx_assert(&rtsock_mtx, MA_OWNED)
158 
159 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "");
160 
161 struct walkarg {
162 	int	family;
163 	int	w_tmemsize;
164 	int	w_op, w_arg;
165 	caddr_t	w_tmem;
166 	struct sysctl_req *w_req;
167 	struct sockaddr *dst;
168 	struct sockaddr *mask;
169 };
170 
171 static void	rts_input(struct mbuf *m);
172 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
173 static int	rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
174 			struct walkarg *w, int *plen);
175 static int	rt_xaddrs(caddr_t cp, caddr_t cplim,
176 			struct rt_addrinfo *rtinfo);
177 static int	cleanup_xaddrs(struct rt_addrinfo *info);
178 static int	sysctl_dumpentry(struct rtentry *rt, void *vw);
179 static int	sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh,
180 			uint32_t weight, struct walkarg *w);
181 static int	sysctl_iflist(int af, struct walkarg *w);
182 static int	sysctl_ifmalist(int af, struct walkarg *w);
183 static int	route_output(struct mbuf *m, struct socket *so, ...);
184 static void	rt_getmetrics(const struct rtentry *rt,
185 			const struct nhop_object *nh, struct rt_metrics *out);
186 static void	rt_dispatch(struct mbuf *, sa_family_t);
187 static int	handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
188 			struct rt_msghdr *rtm, struct rib_cmd_info *rc);
189 static int	update_rtm_from_rc(struct rt_addrinfo *info,
190 			struct rt_msghdr **prtm, int alloc_len,
191 			struct rib_cmd_info *rc, struct nhop_object *nh);
192 static void	send_rtm_reply(struct socket *so, struct rt_msghdr *rtm,
193 			struct mbuf *m, sa_family_t saf, u_int fibnum,
194 			int rtm_errno);
195 static bool	can_export_rte(struct ucred *td_ucred, bool rt_is_host,
196 			const struct sockaddr *rt_dst);
197 
198 static struct netisr_handler rtsock_nh = {
199 	.nh_name = "rtsock",
200 	.nh_handler = rts_input,
201 	.nh_proto = NETISR_ROUTE,
202 	.nh_policy = NETISR_POLICY_SOURCE,
203 };
204 
205 static int
206 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
207 {
208 	int error, qlimit;
209 
210 	netisr_getqlimit(&rtsock_nh, &qlimit);
211 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
212         if (error || !req->newptr)
213                 return (error);
214 	if (qlimit < 1)
215 		return (EINVAL);
216 	return (netisr_setqlimit(&rtsock_nh, qlimit));
217 }
218 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
219     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
220     0, 0, sysctl_route_netisr_maxqlen, "I",
221     "maximum routing socket dispatch queue length");
222 
223 static void
224 vnet_rts_init(void)
225 {
226 	int tmp;
227 
228 	if (IS_DEFAULT_VNET(curvnet)) {
229 		if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
230 			rtsock_nh.nh_qlimit = tmp;
231 		netisr_register(&rtsock_nh);
232 	}
233 #ifdef VIMAGE
234 	 else
235 		netisr_register_vnet(&rtsock_nh);
236 #endif
237 }
238 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
239     vnet_rts_init, 0);
240 
241 #ifdef VIMAGE
242 static void
243 vnet_rts_uninit(void)
244 {
245 
246 	netisr_unregister_vnet(&rtsock_nh);
247 }
248 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
249     vnet_rts_uninit, 0);
250 #endif
251 
252 static int
253 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
254     struct rawcb *rp)
255 {
256 	int fibnum;
257 
258 	KASSERT(m != NULL, ("%s: m is NULL", __func__));
259 	KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
260 	KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
261 
262 	/* No filtering requested. */
263 	if ((m->m_flags & RTS_FILTER_FIB) == 0)
264 		return (0);
265 
266 	/* Check if it is a rts and the fib matches the one of the socket. */
267 	fibnum = M_GETFIB(m);
268 	if (proto->sp_family != PF_ROUTE ||
269 	    rp->rcb_socket == NULL ||
270 	    rp->rcb_socket->so_fibnum == fibnum)
271 		return (0);
272 
273 	/* Filtering requested and no match, the socket shall be skipped. */
274 	return (1);
275 }
276 
277 static void
278 rts_input(struct mbuf *m)
279 {
280 	struct sockproto route_proto;
281 	unsigned short *family;
282 	struct m_tag *tag;
283 
284 	route_proto.sp_family = PF_ROUTE;
285 	tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
286 	if (tag != NULL) {
287 		family = (unsigned short *)(tag + 1);
288 		route_proto.sp_protocol = *family;
289 		m_tag_delete(m, tag);
290 	} else
291 		route_proto.sp_protocol = 0;
292 
293 	raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
294 }
295 
296 /*
297  * It really doesn't make any sense at all for this code to share much
298  * with raw_usrreq.c, since its functionality is so restricted.  XXX
299  */
300 static void
301 rts_abort(struct socket *so)
302 {
303 
304 	raw_usrreqs.pru_abort(so);
305 }
306 
307 static void
308 rts_close(struct socket *so)
309 {
310 
311 	raw_usrreqs.pru_close(so);
312 }
313 
314 /* pru_accept is EOPNOTSUPP */
315 
316 static int
317 rts_attach(struct socket *so, int proto, struct thread *td)
318 {
319 	struct rawcb *rp;
320 	int error;
321 
322 	KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
323 
324 	/* XXX */
325 	rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
326 
327 	so->so_pcb = (caddr_t)rp;
328 	so->so_fibnum = td->td_proc->p_fibnum;
329 	error = raw_attach(so, proto);
330 	rp = sotorawcb(so);
331 	if (error) {
332 		so->so_pcb = NULL;
333 		free(rp, M_PCB);
334 		return error;
335 	}
336 	RTSOCK_LOCK();
337 	switch(rp->rcb_proto.sp_protocol) {
338 	case AF_INET:
339 		V_route_cb.ip_count++;
340 		break;
341 	case AF_INET6:
342 		V_route_cb.ip6_count++;
343 		break;
344 	}
345 	V_route_cb.any_count++;
346 	RTSOCK_UNLOCK();
347 	soisconnected(so);
348 	so->so_options |= SO_USELOOPBACK;
349 	return 0;
350 }
351 
352 static int
353 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
354 {
355 
356 	return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
357 }
358 
359 static int
360 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
361 {
362 
363 	return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
364 }
365 
366 /* pru_connect2 is EOPNOTSUPP */
367 /* pru_control is EOPNOTSUPP */
368 
369 static void
370 rts_detach(struct socket *so)
371 {
372 	struct rawcb *rp = sotorawcb(so);
373 
374 	KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
375 
376 	RTSOCK_LOCK();
377 	switch(rp->rcb_proto.sp_protocol) {
378 	case AF_INET:
379 		V_route_cb.ip_count--;
380 		break;
381 	case AF_INET6:
382 		V_route_cb.ip6_count--;
383 		break;
384 	}
385 	V_route_cb.any_count--;
386 	RTSOCK_UNLOCK();
387 	raw_usrreqs.pru_detach(so);
388 }
389 
390 static int
391 rts_disconnect(struct socket *so)
392 {
393 
394 	return (raw_usrreqs.pru_disconnect(so));
395 }
396 
397 /* pru_listen is EOPNOTSUPP */
398 
399 static int
400 rts_peeraddr(struct socket *so, struct sockaddr **nam)
401 {
402 
403 	return (raw_usrreqs.pru_peeraddr(so, nam));
404 }
405 
406 /* pru_rcvd is EOPNOTSUPP */
407 /* pru_rcvoob is EOPNOTSUPP */
408 
409 static int
410 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
411 	 struct mbuf *control, struct thread *td)
412 {
413 
414 	return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
415 }
416 
417 /* pru_sense is null */
418 
419 static int
420 rts_shutdown(struct socket *so)
421 {
422 
423 	return (raw_usrreqs.pru_shutdown(so));
424 }
425 
426 static int
427 rts_sockaddr(struct socket *so, struct sockaddr **nam)
428 {
429 
430 	return (raw_usrreqs.pru_sockaddr(so, nam));
431 }
432 
433 static struct pr_usrreqs route_usrreqs = {
434 	.pru_abort =		rts_abort,
435 	.pru_attach =		rts_attach,
436 	.pru_bind =		rts_bind,
437 	.pru_connect =		rts_connect,
438 	.pru_detach =		rts_detach,
439 	.pru_disconnect =	rts_disconnect,
440 	.pru_peeraddr =		rts_peeraddr,
441 	.pru_send =		rts_send,
442 	.pru_shutdown =		rts_shutdown,
443 	.pru_sockaddr =		rts_sockaddr,
444 	.pru_close =		rts_close,
445 };
446 
447 #ifndef _SOCKADDR_UNION_DEFINED
448 #define	_SOCKADDR_UNION_DEFINED
449 /*
450  * The union of all possible address formats we handle.
451  */
452 union sockaddr_union {
453 	struct sockaddr		sa;
454 	struct sockaddr_in	sin;
455 	struct sockaddr_in6	sin6;
456 };
457 #endif /* _SOCKADDR_UNION_DEFINED */
458 
459 static int
460 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
461     struct nhop_object *nh, union sockaddr_union *saun, struct ucred *cred)
462 {
463 #if defined(INET) || defined(INET6)
464 	struct epoch_tracker et;
465 #endif
466 
467 	/* First, see if the returned address is part of the jail. */
468 	if (prison_if(cred, nh->nh_ifa->ifa_addr) == 0) {
469 		info->rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
470 		return (0);
471 	}
472 
473 	switch (info->rti_info[RTAX_DST]->sa_family) {
474 #ifdef INET
475 	case AF_INET:
476 	{
477 		struct in_addr ia;
478 		struct ifaddr *ifa;
479 		int found;
480 
481 		found = 0;
482 		/*
483 		 * Try to find an address on the given outgoing interface
484 		 * that belongs to the jail.
485 		 */
486 		NET_EPOCH_ENTER(et);
487 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
488 			struct sockaddr *sa;
489 			sa = ifa->ifa_addr;
490 			if (sa->sa_family != AF_INET)
491 				continue;
492 			ia = ((struct sockaddr_in *)sa)->sin_addr;
493 			if (prison_check_ip4(cred, &ia) == 0) {
494 				found = 1;
495 				break;
496 			}
497 		}
498 		NET_EPOCH_EXIT(et);
499 		if (!found) {
500 			/*
501 			 * As a last resort return the 'default' jail address.
502 			 */
503 			ia = ((struct sockaddr_in *)nh->nh_ifa->ifa_addr)->
504 			    sin_addr;
505 			if (prison_get_ip4(cred, &ia) != 0)
506 				return (ESRCH);
507 		}
508 		bzero(&saun->sin, sizeof(struct sockaddr_in));
509 		saun->sin.sin_len = sizeof(struct sockaddr_in);
510 		saun->sin.sin_family = AF_INET;
511 		saun->sin.sin_addr.s_addr = ia.s_addr;
512 		info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
513 		break;
514 	}
515 #endif
516 #ifdef INET6
517 	case AF_INET6:
518 	{
519 		struct in6_addr ia6;
520 		struct ifaddr *ifa;
521 		int found;
522 
523 		found = 0;
524 		/*
525 		 * Try to find an address on the given outgoing interface
526 		 * that belongs to the jail.
527 		 */
528 		NET_EPOCH_ENTER(et);
529 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
530 			struct sockaddr *sa;
531 			sa = ifa->ifa_addr;
532 			if (sa->sa_family != AF_INET6)
533 				continue;
534 			bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
535 			    &ia6, sizeof(struct in6_addr));
536 			if (prison_check_ip6(cred, &ia6) == 0) {
537 				found = 1;
538 				break;
539 			}
540 		}
541 		NET_EPOCH_EXIT(et);
542 		if (!found) {
543 			/*
544 			 * As a last resort return the 'default' jail address.
545 			 */
546 			ia6 = ((struct sockaddr_in6 *)nh->nh_ifa->ifa_addr)->
547 			    sin6_addr;
548 			if (prison_get_ip6(cred, &ia6) != 0)
549 				return (ESRCH);
550 		}
551 		bzero(&saun->sin6, sizeof(struct sockaddr_in6));
552 		saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
553 		saun->sin6.sin6_family = AF_INET6;
554 		bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
555 		if (sa6_recoverscope(&saun->sin6) != 0)
556 			return (ESRCH);
557 		info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
558 		break;
559 	}
560 #endif
561 	default:
562 		return (ESRCH);
563 	}
564 	return (0);
565 }
566 
567 static int
568 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun)
569 {
570 	struct ifaddr *ifa;
571 	sa_family_t saf;
572 
573 	if (V_loif == NULL) {
574 		printf("Unable to add blackhole/reject nhop without loopback");
575 		return (ENOTSUP);
576 	}
577 	info->rti_ifp = V_loif;
578 
579 	saf = info->rti_info[RTAX_DST]->sa_family;
580 
581 	CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) {
582 		if (ifa->ifa_addr->sa_family == saf) {
583 			info->rti_ifa = ifa;
584 			break;
585 		}
586 	}
587 	if (info->rti_ifa == NULL)
588 		return (ENOTSUP);
589 
590 	bzero(saun, sizeof(union sockaddr_union));
591 	switch (saf) {
592 #ifdef INET
593 	case AF_INET:
594 		saun->sin.sin_family = AF_INET;
595 		saun->sin.sin_len = sizeof(struct sockaddr_in);
596 		saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
597 		break;
598 #endif
599 #ifdef INET6
600 	case AF_INET6:
601 		saun->sin6.sin6_family = AF_INET6;
602 		saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
603 		saun->sin6.sin6_addr = in6addr_loopback;
604 		break;
605 #endif
606 	default:
607 		return (ENOTSUP);
608 	}
609 	info->rti_info[RTAX_GATEWAY] = &saun->sa;
610 	info->rti_flags |= RTF_GATEWAY;
611 
612 	return (0);
613 }
614 
615 /*
616  * Fills in @info based on userland-provided @rtm message.
617  *
618  * Returns 0 on success.
619  */
620 static int
621 fill_addrinfo(struct rt_msghdr *rtm, int len, u_int fibnum, struct rt_addrinfo *info)
622 {
623 	int error;
624 	sa_family_t saf;
625 
626 	rtm->rtm_pid = curproc->p_pid;
627 	info->rti_addrs = rtm->rtm_addrs;
628 
629 	info->rti_mflags = rtm->rtm_inits;
630 	info->rti_rmx = &rtm->rtm_rmx;
631 
632 	/*
633 	 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
634 	 * link-local address because rtrequest requires addresses with
635 	 * embedded scope id.
636 	 */
637 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info))
638 		return (EINVAL);
639 
640 	info->rti_flags = rtm->rtm_flags;
641 	error = cleanup_xaddrs(info);
642 	if (error != 0)
643 		return (error);
644 	saf = info->rti_info[RTAX_DST]->sa_family;
645 	/*
646 	 * Verify that the caller has the appropriate privilege; RTM_GET
647 	 * is the only operation the non-superuser is allowed.
648 	 */
649 	if (rtm->rtm_type != RTM_GET) {
650 		error = priv_check(curthread, PRIV_NET_ROUTE);
651 		if (error != 0)
652 			return (error);
653 	}
654 
655 	/*
656 	 * The given gateway address may be an interface address.
657 	 * For example, issuing a "route change" command on a route
658 	 * entry that was created from a tunnel, and the gateway
659 	 * address given is the local end point. In this case the
660 	 * RTF_GATEWAY flag must be cleared or the destination will
661 	 * not be reachable even though there is no error message.
662 	 */
663 	if (info->rti_info[RTAX_GATEWAY] != NULL &&
664 	    info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
665 		struct rt_addrinfo ginfo;
666 		struct sockaddr *gdst;
667 		struct sockaddr_storage ss;
668 
669 		bzero(&ginfo, sizeof(ginfo));
670 		bzero(&ss, sizeof(ss));
671 		ss.ss_len = sizeof(ss);
672 
673 		ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
674 		gdst = info->rti_info[RTAX_GATEWAY];
675 
676 		/*
677 		 * A host route through the loopback interface is
678 		 * installed for each interface adddress. In pre 8.0
679 		 * releases the interface address of a PPP link type
680 		 * is not reachable locally. This behavior is fixed as
681 		 * part of the new L2/L3 redesign and rewrite work. The
682 		 * signature of this interface address route is the
683 		 * AF_LINK sa_family type of the gateway, and the
684 		 * rt_ifp has the IFF_LOOPBACK flag set.
685 		 */
686 		if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
687 			if (ss.ss_family == AF_LINK &&
688 			    ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
689 				info->rti_flags &= ~RTF_GATEWAY;
690 				info->rti_flags |= RTF_GWFLAG_COMPAT;
691 			}
692 			rib_free_info(&ginfo);
693 		}
694 	}
695 
696 	return (0);
697 }
698 
699 static struct nhop_object *
700 select_nhop(struct nhop_object *nh, const struct sockaddr *gw)
701 {
702 	if (!NH_IS_NHGRP(nh))
703 		return (nh);
704 #ifdef ROUTE_MPATH
705 	struct weightened_nhop *wn;
706 	uint32_t num_nhops;
707 	wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
708 	if (gw == NULL)
709 		return (wn[0].nh);
710 	for (int i = 0; i < num_nhops; i++) {
711 		if (match_nhop_gw(wn[i].nh, gw))
712 			return (wn[i].nh);
713 	}
714 #endif
715 	return (NULL);
716 }
717 
718 /*
719  * Handles RTM_GET message from routing socket, returning matching rt.
720  *
721  * Returns:
722  * 0 on success, with locked and referenced matching rt in @rt_nrt
723  * errno of failure
724  */
725 static int
726 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum,
727     struct rt_msghdr *rtm, struct rib_cmd_info *rc)
728 {
729 	RIB_RLOCK_TRACKER;
730 	struct rib_head *rnh;
731 	struct nhop_object *nh;
732 	sa_family_t saf;
733 
734 	saf = info->rti_info[RTAX_DST]->sa_family;
735 
736 	rnh = rt_tables_get_rnh(fibnum, saf);
737 	if (rnh == NULL)
738 		return (EAFNOSUPPORT);
739 
740 	RIB_RLOCK(rnh);
741 
742 	/*
743 	 * By (implicit) convention host route (one without netmask)
744 	 * means longest-prefix-match request and the route with netmask
745 	 * means exact-match lookup.
746 	 * As cleanup_xaddrs() cleans up info flags&addrs for the /32,/128
747 	 * prefixes, use original data to check for the netmask presence.
748 	 */
749 	if ((rtm->rtm_addrs & RTA_NETMASK) == 0) {
750 		/*
751 		 * Provide longest prefix match for
752 		 * address lookup (no mask).
753 		 * 'route -n get addr'
754 		 */
755 		rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr(
756 		    info->rti_info[RTAX_DST], &rnh->head);
757 	} else
758 		rc->rc_rt = (struct rtentry *) rnh->rnh_lookup(
759 		    info->rti_info[RTAX_DST],
760 		    info->rti_info[RTAX_NETMASK], &rnh->head);
761 
762 	if (rc->rc_rt == NULL) {
763 		RIB_RUNLOCK(rnh);
764 		return (ESRCH);
765 	}
766 
767 	nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
768 	if (nh == NULL) {
769 		RIB_RUNLOCK(rnh);
770 		return (ESRCH);
771 	}
772 	/*
773 	 * If performing proxied L2 entry insertion, and
774 	 * the actual PPP host entry is found, perform
775 	 * another search to retrieve the prefix route of
776 	 * the local end point of the PPP link.
777 	 * TODO: move this logic to userland.
778 	 */
779 	if (rtm->rtm_flags & RTF_ANNOUNCE) {
780 		struct sockaddr laddr;
781 
782 		if (nh->nh_ifp != NULL &&
783 		    nh->nh_ifp->if_type == IFT_PROPVIRTUAL) {
784 			struct ifaddr *ifa;
785 
786 			ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1,
787 					RT_ALL_FIBS);
788 			if (ifa != NULL)
789 				rt_maskedcopy(ifa->ifa_addr,
790 					      &laddr,
791 					      ifa->ifa_netmask);
792 		} else
793 			rt_maskedcopy(nh->nh_ifa->ifa_addr,
794 				      &laddr,
795 				      nh->nh_ifa->ifa_netmask);
796 		/*
797 		 * refactor rt and no lock operation necessary
798 		 */
799 		rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
800 		    &rnh->head);
801 		if (rc->rc_rt == NULL) {
802 			RIB_RUNLOCK(rnh);
803 			return (ESRCH);
804 		}
805 		nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]);
806 		if (nh == NULL) {
807 			RIB_RUNLOCK(rnh);
808 			return (ESRCH);
809 		}
810 	}
811 	rc->rc_nh_new = nh;
812 	rc->rc_nh_weight = rc->rc_rt->rt_weight;
813 	RIB_RUNLOCK(rnh);
814 
815 	return (0);
816 }
817 
818 static void
819 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask)
820 {
821 #ifdef INET
822 	if (family == AF_INET) {
823 		struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
824 		struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
825 
826 		bzero(dst4, sizeof(struct sockaddr_in));
827 		bzero(mask4, sizeof(struct sockaddr_in));
828 
829 		dst4->sin_family = AF_INET;
830 		dst4->sin_len = sizeof(struct sockaddr_in);
831 		mask4->sin_family = AF_INET;
832 		mask4->sin_len = sizeof(struct sockaddr_in);
833 	}
834 #endif
835 #ifdef INET6
836 	if (family == AF_INET6) {
837 		struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
838 		struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
839 
840 		bzero(dst6, sizeof(struct sockaddr_in6));
841 		bzero(mask6, sizeof(struct sockaddr_in6));
842 
843 		dst6->sin6_family = AF_INET6;
844 		dst6->sin6_len = sizeof(struct sockaddr_in6);
845 		mask6->sin6_family = AF_INET6;
846 		mask6->sin6_len = sizeof(struct sockaddr_in6);
847 	}
848 #endif
849 }
850 
851 static void
852 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst,
853     struct sockaddr *mask)
854 {
855 #ifdef INET
856 	if (dst->sa_family == AF_INET) {
857 		struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
858 		struct sockaddr_in *mask4 = (struct sockaddr_in *)mask;
859 		uint32_t scopeid = 0;
860 		rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr,
861 		    &scopeid);
862 		return;
863 	}
864 #endif
865 #ifdef INET6
866 	if (dst->sa_family == AF_INET6) {
867 		struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
868 		struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask;
869 		uint32_t scopeid = 0;
870 		rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr,
871 		    &mask6->sin6_addr, &scopeid);
872 		dst6->sin6_scope_id = scopeid;
873 		return;
874 	}
875 #endif
876 }
877 
878 
879 /*
880  * Update sockaddrs, flags, etc in @prtm based on @rc data.
881  * rtm can be reallocated.
882  *
883  * Returns 0 on success, along with pointer to (potentially reallocated)
884  *  rtm.
885  *
886  */
887 static int
888 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm,
889     int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh)
890 {
891 	struct walkarg w;
892 	union sockaddr_union saun;
893 	struct rt_msghdr *rtm, *orig_rtm = NULL;
894 	struct ifnet *ifp;
895 	int error, len;
896 
897 	rtm = *prtm;
898 	union sockaddr_union sa_dst, sa_mask;
899 	int family = info->rti_info[RTAX_DST]->sa_family;
900 	init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa);
901 	export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa);
902 
903 	info->rti_info[RTAX_DST] = &sa_dst.sa;
904 	info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa;
905 	info->rti_info[RTAX_GATEWAY] = &nh->gw_sa;
906 	info->rti_info[RTAX_GENMASK] = 0;
907 	ifp = nh->nh_ifp;
908 	if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
909 		if (ifp) {
910 			info->rti_info[RTAX_IFP] =
911 			    ifp->if_addr->ifa_addr;
912 			error = rtm_get_jailed(info, ifp, nh,
913 			    &saun, curthread->td_ucred);
914 			if (error != 0)
915 				return (error);
916 			if (ifp->if_flags & IFF_POINTOPOINT)
917 				info->rti_info[RTAX_BRD] =
918 				    nh->nh_ifa->ifa_dstaddr;
919 			rtm->rtm_index = ifp->if_index;
920 		} else {
921 			info->rti_info[RTAX_IFP] = NULL;
922 			info->rti_info[RTAX_IFA] = NULL;
923 		}
924 	} else if (ifp != NULL)
925 		rtm->rtm_index = ifp->if_index;
926 
927 	/* Check if we need to realloc storage */
928 	rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len);
929 	if (len > alloc_len) {
930 		struct rt_msghdr *tmp_rtm;
931 
932 		tmp_rtm = malloc(len, M_TEMP, M_NOWAIT);
933 		if (tmp_rtm == NULL)
934 			return (ENOBUFS);
935 		bcopy(rtm, tmp_rtm, rtm->rtm_msglen);
936 		orig_rtm = rtm;
937 		rtm = tmp_rtm;
938 		alloc_len = len;
939 
940 		/*
941 		 * Delay freeing original rtm as info contains
942 		 * data referencing it.
943 		 */
944 	}
945 
946 	w.w_tmem = (caddr_t)rtm;
947 	w.w_tmemsize = alloc_len;
948 	rtsock_msg_buffer(rtm->rtm_type, info, &w, &len);
949 
950 	rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh);
951 	if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
952 		rtm->rtm_flags = RTF_GATEWAY |
953 			(rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
954 	rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx);
955 	rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight;
956 	rtm->rtm_addrs = info->rti_addrs;
957 
958 	if (orig_rtm != NULL)
959 		free(orig_rtm, M_TEMP);
960 	*prtm = rtm;
961 
962 	return (0);
963 }
964 
965 #ifdef ROUTE_MPATH
966 static void
967 save_del_notification(struct rib_cmd_info *rc, void *_cbdata)
968 {
969 	struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
970 
971 	if (rc->rc_cmd == RTM_DELETE)
972 		*rc_new = *rc;
973 }
974 
975 static void
976 save_add_notification(struct rib_cmd_info *rc, void *_cbdata)
977 {
978 	struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata;
979 
980 	if (rc->rc_cmd == RTM_ADD)
981 		*rc_new = *rc;
982 }
983 #endif
984 
985 /*ARGSUSED*/
986 static int
987 route_output(struct mbuf *m, struct socket *so, ...)
988 {
989 	struct rt_msghdr *rtm = NULL;
990 	struct rtentry *rt = NULL;
991 	struct rt_addrinfo info;
992 	struct epoch_tracker et;
993 #ifdef INET6
994 	struct sockaddr_storage ss;
995 	struct sockaddr_in6 *sin6;
996 	int i, rti_need_deembed = 0;
997 #endif
998 	int alloc_len = 0, len, error = 0, fibnum;
999 	sa_family_t saf = AF_UNSPEC;
1000 	struct rib_cmd_info rc;
1001 	struct nhop_object *nh;
1002 
1003 	fibnum = so->so_fibnum;
1004 #define senderr(e) { error = e; goto flush;}
1005 	if (m == NULL || ((m->m_len < sizeof(long)) &&
1006 		       (m = m_pullup(m, sizeof(long))) == NULL))
1007 		return (ENOBUFS);
1008 	if ((m->m_flags & M_PKTHDR) == 0)
1009 		panic("route_output");
1010 	NET_EPOCH_ENTER(et);
1011 	len = m->m_pkthdr.len;
1012 	if (len < sizeof(*rtm) ||
1013 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen)
1014 		senderr(EINVAL);
1015 
1016 	/*
1017 	 * Most of current messages are in range 200-240 bytes,
1018 	 * minimize possible re-allocation on reply using larger size
1019 	 * buffer aligned on 1k boundaty.
1020 	 */
1021 	alloc_len = roundup2(len, 1024);
1022 	if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
1023 		senderr(ENOBUFS);
1024 
1025 	m_copydata(m, 0, len, (caddr_t)rtm);
1026 	bzero(&info, sizeof(info));
1027 	nh = NULL;
1028 
1029 	if (rtm->rtm_version != RTM_VERSION) {
1030 		/* Do not touch message since format is unknown */
1031 		free(rtm, M_TEMP);
1032 		rtm = NULL;
1033 		senderr(EPROTONOSUPPORT);
1034 	}
1035 
1036 	/*
1037 	 * Starting from here, it is possible
1038 	 * to alter original message and insert
1039 	 * caller PID and error value.
1040 	 */
1041 
1042 	if ((error = fill_addrinfo(rtm, len, fibnum, &info)) != 0) {
1043 		senderr(error);
1044 	}
1045 
1046 	saf = info.rti_info[RTAX_DST]->sa_family;
1047 
1048 	/* support for new ARP code */
1049 	if (rtm->rtm_flags & RTF_LLDATA) {
1050 		error = lla_rt_output(rtm, &info);
1051 #ifdef INET6
1052 		if (error == 0)
1053 			rti_need_deembed = 1;
1054 #endif
1055 		goto flush;
1056 	}
1057 
1058 	union sockaddr_union gw_saun;
1059 	int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT);
1060 	if (blackhole_flags != 0) {
1061 		if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT))
1062 			error = fill_blackholeinfo(&info, &gw_saun);
1063 		else
1064 			error = EINVAL;
1065 		if (error != 0)
1066 			senderr(error);
1067 		/* TODO: rebuild rtm from scratch */
1068 	}
1069 
1070 	switch (rtm->rtm_type) {
1071 	case RTM_ADD:
1072 	case RTM_CHANGE:
1073 		if (rtm->rtm_type == RTM_ADD) {
1074 			if (info.rti_info[RTAX_GATEWAY] == NULL)
1075 				senderr(EINVAL);
1076 		}
1077 		error = rib_action(fibnum, rtm->rtm_type, &info, &rc);
1078 		if (error == 0) {
1079 #ifdef INET6
1080 			rti_need_deembed = 1;
1081 #endif
1082 #ifdef ROUTE_MPATH
1083 			if (NH_IS_NHGRP(rc.rc_nh_new) ||
1084 			    (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) {
1085 				struct rib_cmd_info rc_simple = {};
1086 				rib_decompose_notification(&rc,
1087 				    save_add_notification, (void *)&rc_simple);
1088 				rc = rc_simple;
1089 			}
1090 #endif
1091 			nh = rc.rc_nh_new;
1092 			rtm->rtm_index = nh->nh_ifp->if_index;
1093 			rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh);
1094 		}
1095 		break;
1096 
1097 	case RTM_DELETE:
1098 		error = rib_action(fibnum, RTM_DELETE, &info, &rc);
1099 		if (error == 0) {
1100 #ifdef ROUTE_MPATH
1101 			if (NH_IS_NHGRP(rc.rc_nh_old) ||
1102 			    (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) {
1103 				struct rib_cmd_info rc_simple = {};
1104 				rib_decompose_notification(&rc,
1105 				    save_del_notification, (void *)&rc_simple);
1106 				rc = rc_simple;
1107 			}
1108 #endif
1109 			nh = rc.rc_nh_old;
1110 			goto report;
1111 		}
1112 #ifdef INET6
1113 		/* rt_msg2() will not be used when RTM_DELETE fails. */
1114 		rti_need_deembed = 1;
1115 #endif
1116 		break;
1117 
1118 	case RTM_GET:
1119 		error = handle_rtm_get(&info, fibnum, rtm, &rc);
1120 		if (error != 0)
1121 			senderr(error);
1122 		nh = rc.rc_nh_new;
1123 
1124 report:
1125 		if (!can_export_rte(curthread->td_ucred,
1126 		    info.rti_info[RTAX_NETMASK] == NULL,
1127 		    info.rti_info[RTAX_DST])) {
1128 			senderr(ESRCH);
1129 		}
1130 
1131 		error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh);
1132 		/*
1133 		 * Note that some sockaddr pointers may have changed to
1134 		 * point to memory outsize @rtm. Some may be pointing
1135 		 * to the on-stack variables.
1136 		 * Given that, any pointer in @info CANNOT BE USED.
1137 		 */
1138 
1139 		/*
1140 		 * scopeid deembedding has been performed while
1141 		 * writing updated rtm in rtsock_msg_buffer().
1142 		 * With that in mind, skip deembedding procedure below.
1143 		 */
1144 #ifdef INET6
1145 		rti_need_deembed = 0;
1146 #endif
1147 		if (error != 0)
1148 			senderr(error);
1149 		break;
1150 
1151 	default:
1152 		senderr(EOPNOTSUPP);
1153 	}
1154 
1155 flush:
1156 	NET_EPOCH_EXIT(et);
1157 	rt = NULL;
1158 
1159 #ifdef INET6
1160 	if (rtm != NULL) {
1161 		if (rti_need_deembed) {
1162 			/* sin6_scope_id is recovered before sending rtm. */
1163 			sin6 = (struct sockaddr_in6 *)&ss;
1164 			for (i = 0; i < RTAX_MAX; i++) {
1165 				if (info.rti_info[i] == NULL)
1166 					continue;
1167 				if (info.rti_info[i]->sa_family != AF_INET6)
1168 					continue;
1169 				bcopy(info.rti_info[i], sin6, sizeof(*sin6));
1170 				if (sa6_recoverscope(sin6) == 0)
1171 					bcopy(sin6, info.rti_info[i],
1172 						    sizeof(*sin6));
1173 			}
1174 		}
1175 	}
1176 #endif
1177 	send_rtm_reply(so, rtm, m, saf, fibnum, error);
1178 
1179 	return (error);
1180 }
1181 
1182 /*
1183  * Sends the prepared reply message in @rtm to all rtsock clients.
1184  * Frees @m and @rtm.
1185  *
1186  */
1187 static void
1188 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m,
1189     sa_family_t saf, u_int fibnum, int rtm_errno)
1190 {
1191 	struct rawcb *rp = NULL;
1192 
1193 	/*
1194 	 * Check to see if we don't want our own messages.
1195 	 */
1196 	if ((so->so_options & SO_USELOOPBACK) == 0) {
1197 		if (V_route_cb.any_count <= 1) {
1198 			if (rtm != NULL)
1199 				free(rtm, M_TEMP);
1200 			m_freem(m);
1201 			return;
1202 		}
1203 		/* There is another listener, so construct message */
1204 		rp = sotorawcb(so);
1205 	}
1206 
1207 	if (rtm != NULL) {
1208 		if (rtm_errno!= 0)
1209 			rtm->rtm_errno = rtm_errno;
1210 		else
1211 			rtm->rtm_flags |= RTF_DONE;
1212 
1213 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
1214 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
1215 			m_freem(m);
1216 			m = NULL;
1217 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
1218 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1219 
1220 		free(rtm, M_TEMP);
1221 	}
1222 	if (m != NULL) {
1223 		M_SETFIB(m, fibnum);
1224 		m->m_flags |= RTS_FILTER_FIB;
1225 		if (rp) {
1226 			/*
1227 			 * XXX insure we don't get a copy by
1228 			 * invalidating our protocol
1229 			 */
1230 			unsigned short family = rp->rcb_proto.sp_family;
1231 			rp->rcb_proto.sp_family = 0;
1232 			rt_dispatch(m, saf);
1233 			rp->rcb_proto.sp_family = family;
1234 		} else
1235 			rt_dispatch(m, saf);
1236 	}
1237 }
1238 
1239 static void
1240 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh,
1241     struct rt_metrics *out)
1242 {
1243 
1244 	bzero(out, sizeof(*out));
1245 	out->rmx_mtu = nh->nh_mtu;
1246 	out->rmx_weight = rt->rt_weight;
1247 	out->rmx_nhidx = nhop_get_idx(nh);
1248 	/* Kernel -> userland timebase conversion. */
1249 	out->rmx_expire = rt->rt_expire ?
1250 	    rt->rt_expire - time_uptime + time_second : 0;
1251 }
1252 
1253 /*
1254  * Extract the addresses of the passed sockaddrs.
1255  * Do a little sanity checking so as to avoid bad memory references.
1256  * This data is derived straight from userland.
1257  */
1258 static int
1259 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1260 {
1261 	struct sockaddr *sa;
1262 	int i;
1263 
1264 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1265 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
1266 			continue;
1267 		sa = (struct sockaddr *)cp;
1268 		/*
1269 		 * It won't fit.
1270 		 */
1271 		if (cp + sa->sa_len > cplim)
1272 			return (EINVAL);
1273 		/*
1274 		 * there are no more.. quit now
1275 		 * If there are more bits, they are in error.
1276 		 * I've seen this. route(1) can evidently generate these.
1277 		 * This causes kernel to core dump.
1278 		 * for compatibility, If we see this, point to a safe address.
1279 		 */
1280 		if (sa->sa_len == 0) {
1281 			rtinfo->rti_info[i] = &sa_zero;
1282 			return (0); /* should be EINVAL but for compat */
1283 		}
1284 		/* accept it */
1285 #ifdef INET6
1286 		if (sa->sa_family == AF_INET6)
1287 			sa6_embedscope((struct sockaddr_in6 *)sa,
1288 			    V_ip6_use_defzone);
1289 #endif
1290 		rtinfo->rti_info[i] = sa;
1291 		cp += SA_SIZE(sa);
1292 	}
1293 	return (0);
1294 }
1295 
1296 #ifdef INET
1297 static inline void
1298 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr)
1299 {
1300 
1301 	const struct sockaddr_in nsin = {
1302 		.sin_family = AF_INET,
1303 		.sin_len = sizeof(struct sockaddr_in),
1304 		.sin_addr = addr,
1305 	};
1306 	*sin = nsin;
1307 }
1308 #endif
1309 
1310 #ifdef INET6
1311 static inline void
1312 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6,
1313     uint32_t scopeid)
1314 {
1315 
1316 	const struct sockaddr_in6 nsin6 = {
1317 		.sin6_family = AF_INET6,
1318 		.sin6_len = sizeof(struct sockaddr_in6),
1319 		.sin6_addr = *addr6,
1320 		.sin6_scope_id = scopeid,
1321 	};
1322 	*sin6 = nsin6;
1323 }
1324 #endif
1325 
1326 static int
1327 cleanup_xaddrs_gateway(struct rt_addrinfo *info)
1328 {
1329 	struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
1330 
1331 	switch (gw->sa_family) {
1332 #ifdef INET
1333 	case AF_INET:
1334 		{
1335 			struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw;
1336 			if (gw_sin->sin_len < sizeof(struct sockaddr_in)) {
1337 				printf("gw sin_len too small\n");
1338 				return (EINVAL);
1339 			}
1340 			fill_sockaddr_inet(gw_sin, gw_sin->sin_addr);
1341 		}
1342 		break;
1343 #endif
1344 #ifdef INET6
1345 	case AF_INET6:
1346 		{
1347 			struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw;
1348 			if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) {
1349 				printf("gw sin6_len too small\n");
1350 				return (EINVAL);
1351 			}
1352 			fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0);
1353 			break;
1354 		}
1355 #endif
1356 	case AF_LINK:
1357 		{
1358 			struct sockaddr_dl_short *gw_sdl;
1359 
1360 			gw_sdl = (struct sockaddr_dl_short *)gw;
1361 			if (gw_sdl->sdl_len < sizeof(struct sockaddr_dl_short)) {
1362 				printf("gw sdl_len too small\n");
1363 				return (EINVAL);
1364 			}
1365 
1366 			const struct sockaddr_dl_short sdl = {
1367 				.sdl_family = AF_LINK,
1368 				.sdl_len = sizeof(struct sockaddr_dl_short),
1369 				.sdl_index = gw_sdl->sdl_index,
1370 			};
1371 			*gw_sdl = sdl;
1372 			break;
1373 		}
1374 	}
1375 
1376 	return (0);
1377 }
1378 
1379 #ifdef INET
1380 static int
1381 cleanup_xaddrs_inet(struct rt_addrinfo *info)
1382 {
1383 	struct sockaddr_in *dst_sa, *mask_sa;
1384 
1385 	/* Check & fixup dst/netmask combination first */
1386 	dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST];
1387 	mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
1388 
1389 	struct in_addr mask = {
1390 		.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST,
1391 	};
1392 	struct in_addr dst = {
1393 		.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr))
1394 	};
1395 
1396 	if (dst_sa->sin_len < sizeof(struct sockaddr_in)) {
1397 		printf("dst sin_len too small\n");
1398 		return (EINVAL);
1399 	}
1400 	if (mask_sa && mask_sa->sin_len < sizeof(struct sockaddr_in)) {
1401 		printf("mask sin_len too small\n");
1402 		return (EINVAL);
1403 	}
1404 	fill_sockaddr_inet(dst_sa, dst);
1405 
1406 	if (mask.s_addr != INADDR_BROADCAST)
1407 		fill_sockaddr_inet(mask_sa, mask);
1408 	else {
1409 		info->rti_info[RTAX_NETMASK] = NULL;
1410 		info->rti_flags |= RTF_HOST;
1411 		info->rti_addrs &= ~RTA_NETMASK;
1412 	}
1413 
1414 	/* Check gateway */
1415 	if (info->rti_info[RTAX_GATEWAY] != NULL)
1416 		return (cleanup_xaddrs_gateway(info));
1417 
1418 	return (0);
1419 }
1420 #endif
1421 
1422 #ifdef INET6
1423 static int
1424 cleanup_xaddrs_inet6(struct rt_addrinfo *info)
1425 {
1426 	struct sockaddr_in6 *dst_sa, *mask_sa;
1427 	struct in6_addr mask;
1428 
1429 	/* Check & fixup dst/netmask combination first */
1430 	dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST];
1431 	mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK];
1432 
1433 	mask = mask_sa ? mask_sa->sin6_addr : in6mask128;
1434 	IN6_MASK_ADDR(&dst_sa->sin6_addr, &mask);
1435 
1436 	if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1437 		printf("dst sin6_len too small\n");
1438 		return (EINVAL);
1439 	}
1440 	if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) {
1441 		printf("mask sin6_len too small\n");
1442 		return (EINVAL);
1443 	}
1444 	fill_sockaddr_inet6(dst_sa, &dst_sa->sin6_addr, 0);
1445 
1446 	if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128))
1447 		fill_sockaddr_inet6(mask_sa, &mask, 0);
1448 	else {
1449 		info->rti_info[RTAX_NETMASK] = NULL;
1450 		info->rti_flags |= RTF_HOST;
1451 		info->rti_addrs &= ~RTA_NETMASK;
1452 	}
1453 
1454 	/* Check gateway */
1455 	if (info->rti_info[RTAX_GATEWAY] != NULL)
1456 		return (cleanup_xaddrs_gateway(info));
1457 
1458 	return (0);
1459 }
1460 #endif
1461 
1462 static int
1463 cleanup_xaddrs(struct rt_addrinfo *info)
1464 {
1465 	int error = EAFNOSUPPORT;
1466 
1467 	if (info->rti_info[RTAX_DST] == NULL)
1468 		return (EINVAL);
1469 
1470 	switch (info->rti_info[RTAX_DST]->sa_family) {
1471 #ifdef INET
1472 	case AF_INET:
1473 		error = cleanup_xaddrs_inet(info);
1474 		break;
1475 #endif
1476 #ifdef INET6
1477 	case AF_INET6:
1478 		error = cleanup_xaddrs_inet6(info);
1479 		break;
1480 #endif
1481 	}
1482 
1483 	return (error);
1484 }
1485 
1486 /*
1487  * Fill in @dmask with valid netmask leaving original @smask
1488  * intact. Mostly used with radix netmasks.
1489  */
1490 struct sockaddr *
1491 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask,
1492     struct sockaddr_storage *dmask)
1493 {
1494 	if (dst == NULL || smask == NULL)
1495 		return (NULL);
1496 
1497 	memset(dmask, 0, dst->sa_len);
1498 	memcpy(dmask, smask, smask->sa_len);
1499 	dmask->ss_len = dst->sa_len;
1500 	dmask->ss_family = dst->sa_family;
1501 
1502 	return ((struct sockaddr *)dmask);
1503 }
1504 
1505 /*
1506  * Writes information related to @rtinfo object to newly-allocated mbuf.
1507  * Assumes MCLBYTES is enough to construct any message.
1508  * Used for OS notifications of vaious events (if/ifa announces,etc)
1509  *
1510  * Returns allocated mbuf or NULL on failure.
1511  */
1512 static struct mbuf *
1513 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1514 {
1515 	struct sockaddr_storage ss;
1516 	struct rt_msghdr *rtm;
1517 	struct mbuf *m;
1518 	int i;
1519 	struct sockaddr *sa;
1520 #ifdef INET6
1521 	struct sockaddr_in6 *sin6;
1522 #endif
1523 	int len, dlen;
1524 
1525 	switch (type) {
1526 	case RTM_DELADDR:
1527 	case RTM_NEWADDR:
1528 		len = sizeof(struct ifa_msghdr);
1529 		break;
1530 
1531 	case RTM_DELMADDR:
1532 	case RTM_NEWMADDR:
1533 		len = sizeof(struct ifma_msghdr);
1534 		break;
1535 
1536 	case RTM_IFINFO:
1537 		len = sizeof(struct if_msghdr);
1538 		break;
1539 
1540 	case RTM_IFANNOUNCE:
1541 	case RTM_IEEE80211:
1542 		len = sizeof(struct if_announcemsghdr);
1543 		break;
1544 
1545 	default:
1546 		len = sizeof(struct rt_msghdr);
1547 	}
1548 
1549 	/* XXXGL: can we use MJUMPAGESIZE cluster here? */
1550 	KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1551 	if (len > MHLEN)
1552 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1553 	else
1554 		m = m_gethdr(M_NOWAIT, MT_DATA);
1555 	if (m == NULL)
1556 		return (m);
1557 
1558 	m->m_pkthdr.len = m->m_len = len;
1559 	rtm = mtod(m, struct rt_msghdr *);
1560 	bzero((caddr_t)rtm, len);
1561 	for (i = 0; i < RTAX_MAX; i++) {
1562 		if ((sa = rtinfo->rti_info[i]) == NULL)
1563 			continue;
1564 		rtinfo->rti_addrs |= (1 << i);
1565 
1566 		dlen = SA_SIZE(sa);
1567 		KASSERT(dlen <= sizeof(ss),
1568 		    ("%s: sockaddr size overflow", __func__));
1569 		bzero(&ss, sizeof(ss));
1570 		bcopy(sa, &ss, sa->sa_len);
1571 		sa = (struct sockaddr *)&ss;
1572 #ifdef INET6
1573 		if (sa->sa_family == AF_INET6) {
1574 			sin6 = (struct sockaddr_in6 *)sa;
1575 			(void)sa6_recoverscope(sin6);
1576 		}
1577 #endif
1578 		m_copyback(m, len, dlen, (caddr_t)sa);
1579 		len += dlen;
1580 	}
1581 	if (m->m_pkthdr.len != len) {
1582 		m_freem(m);
1583 		return (NULL);
1584 	}
1585 	rtm->rtm_msglen = len;
1586 	rtm->rtm_version = RTM_VERSION;
1587 	rtm->rtm_type = type;
1588 	return (m);
1589 }
1590 
1591 /*
1592  * Writes information related to @rtinfo object to preallocated buffer.
1593  * Stores needed size in @plen. If @w is NULL, calculates size without
1594  * writing.
1595  * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1596  *
1597  * Returns 0 on success.
1598  *
1599  */
1600 static int
1601 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1602 {
1603 	struct sockaddr_storage ss;
1604 	int len, buflen = 0, dlen, i;
1605 	caddr_t cp = NULL;
1606 	struct rt_msghdr *rtm = NULL;
1607 #ifdef INET6
1608 	struct sockaddr_in6 *sin6;
1609 #endif
1610 #ifdef COMPAT_FREEBSD32
1611 	bool compat32 = false;
1612 #endif
1613 
1614 	switch (type) {
1615 	case RTM_DELADDR:
1616 	case RTM_NEWADDR:
1617 		if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1618 #ifdef COMPAT_FREEBSD32
1619 			if (w->w_req->flags & SCTL_MASK32) {
1620 				len = sizeof(struct ifa_msghdrl32);
1621 				compat32 = true;
1622 			} else
1623 #endif
1624 				len = sizeof(struct ifa_msghdrl);
1625 		} else
1626 			len = sizeof(struct ifa_msghdr);
1627 		break;
1628 
1629 	case RTM_IFINFO:
1630 #ifdef COMPAT_FREEBSD32
1631 		if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1632 			if (w->w_op == NET_RT_IFLISTL)
1633 				len = sizeof(struct if_msghdrl32);
1634 			else
1635 				len = sizeof(struct if_msghdr32);
1636 			compat32 = true;
1637 			break;
1638 		}
1639 #endif
1640 		if (w != NULL && w->w_op == NET_RT_IFLISTL)
1641 			len = sizeof(struct if_msghdrl);
1642 		else
1643 			len = sizeof(struct if_msghdr);
1644 		break;
1645 
1646 	case RTM_NEWMADDR:
1647 		len = sizeof(struct ifma_msghdr);
1648 		break;
1649 
1650 	default:
1651 		len = sizeof(struct rt_msghdr);
1652 	}
1653 
1654 	if (w != NULL) {
1655 		rtm = (struct rt_msghdr *)w->w_tmem;
1656 		buflen = w->w_tmemsize - len;
1657 		cp = (caddr_t)w->w_tmem + len;
1658 	}
1659 
1660 	rtinfo->rti_addrs = 0;
1661 	for (i = 0; i < RTAX_MAX; i++) {
1662 		struct sockaddr *sa;
1663 
1664 		if ((sa = rtinfo->rti_info[i]) == NULL)
1665 			continue;
1666 		rtinfo->rti_addrs |= (1 << i);
1667 #ifdef COMPAT_FREEBSD32
1668 		if (compat32)
1669 			dlen = SA_SIZE32(sa);
1670 		else
1671 #endif
1672 			dlen = SA_SIZE(sa);
1673 		if (cp != NULL && buflen >= dlen) {
1674 			KASSERT(dlen <= sizeof(ss),
1675 			    ("%s: sockaddr size overflow", __func__));
1676 			bzero(&ss, sizeof(ss));
1677 			bcopy(sa, &ss, sa->sa_len);
1678 			sa = (struct sockaddr *)&ss;
1679 #ifdef INET6
1680 			if (sa->sa_family == AF_INET6) {
1681 				sin6 = (struct sockaddr_in6 *)sa;
1682 				(void)sa6_recoverscope(sin6);
1683 			}
1684 #endif
1685 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
1686 			cp += dlen;
1687 			buflen -= dlen;
1688 		} else if (cp != NULL) {
1689 			/*
1690 			 * Buffer too small. Count needed size
1691 			 * and return with error.
1692 			 */
1693 			cp = NULL;
1694 		}
1695 
1696 		len += dlen;
1697 	}
1698 
1699 	if (cp != NULL) {
1700 		dlen = ALIGN(len) - len;
1701 		if (buflen < dlen)
1702 			cp = NULL;
1703 		else {
1704 			bzero(cp, dlen);
1705 			cp += dlen;
1706 			buflen -= dlen;
1707 		}
1708 	}
1709 	len = ALIGN(len);
1710 
1711 	if (cp != NULL) {
1712 		/* fill header iff buffer is large enough */
1713 		rtm->rtm_version = RTM_VERSION;
1714 		rtm->rtm_type = type;
1715 		rtm->rtm_msglen = len;
1716 	}
1717 
1718 	*plen = len;
1719 
1720 	if (w != NULL && cp == NULL)
1721 		return (ENOBUFS);
1722 
1723 	return (0);
1724 }
1725 
1726 /*
1727  * This routine is called to generate a message from the routing
1728  * socket indicating that a redirect has occurred, a routing lookup
1729  * has failed, or that a protocol has detected timeouts to a particular
1730  * destination.
1731  */
1732 void
1733 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1734     int fibnum)
1735 {
1736 	struct rt_msghdr *rtm;
1737 	struct mbuf *m;
1738 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1739 
1740 	if (V_route_cb.any_count == 0)
1741 		return;
1742 	m = rtsock_msg_mbuf(type, rtinfo);
1743 	if (m == NULL)
1744 		return;
1745 
1746 	if (fibnum != RT_ALL_FIBS) {
1747 		KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1748 		    "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1749 		M_SETFIB(m, fibnum);
1750 		m->m_flags |= RTS_FILTER_FIB;
1751 	}
1752 
1753 	rtm = mtod(m, struct rt_msghdr *);
1754 	rtm->rtm_flags = RTF_DONE | flags;
1755 	rtm->rtm_errno = error;
1756 	rtm->rtm_addrs = rtinfo->rti_addrs;
1757 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1758 }
1759 
1760 void
1761 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1762 {
1763 
1764 	rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1765 }
1766 
1767 /*
1768  * This routine is called to generate a message from the routing
1769  * socket indicating that the status of a network interface has changed.
1770  */
1771 void
1772 rt_ifmsg(struct ifnet *ifp)
1773 {
1774 	struct if_msghdr *ifm;
1775 	struct mbuf *m;
1776 	struct rt_addrinfo info;
1777 
1778 	if (V_route_cb.any_count == 0)
1779 		return;
1780 	bzero((caddr_t)&info, sizeof(info));
1781 	m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1782 	if (m == NULL)
1783 		return;
1784 	ifm = mtod(m, struct if_msghdr *);
1785 	ifm->ifm_index = ifp->if_index;
1786 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1787 	if_data_copy(ifp, &ifm->ifm_data);
1788 	ifm->ifm_addrs = 0;
1789 	rt_dispatch(m, AF_UNSPEC);
1790 }
1791 
1792 /*
1793  * Announce interface address arrival/withdraw.
1794  * Please do not call directly, use rt_addrmsg().
1795  * Assume input data to be valid.
1796  * Returns 0 on success.
1797  */
1798 int
1799 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1800 {
1801 	struct rt_addrinfo info;
1802 	struct sockaddr *sa;
1803 	int ncmd;
1804 	struct mbuf *m;
1805 	struct ifa_msghdr *ifam;
1806 	struct ifnet *ifp = ifa->ifa_ifp;
1807 	struct sockaddr_storage ss;
1808 
1809 	if (V_route_cb.any_count == 0)
1810 		return (0);
1811 
1812 	ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1813 
1814 	bzero((caddr_t)&info, sizeof(info));
1815 	info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1816 	info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1817 	info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1818 	    info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss);
1819 	info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1820 	if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1821 		return (ENOBUFS);
1822 	ifam = mtod(m, struct ifa_msghdr *);
1823 	ifam->ifam_index = ifp->if_index;
1824 	ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1825 	ifam->ifam_flags = ifa->ifa_flags;
1826 	ifam->ifam_addrs = info.rti_addrs;
1827 
1828 	if (fibnum != RT_ALL_FIBS) {
1829 		M_SETFIB(m, fibnum);
1830 		m->m_flags |= RTS_FILTER_FIB;
1831 	}
1832 
1833 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1834 
1835 	return (0);
1836 }
1837 
1838 /*
1839  * Announce route addition/removal to rtsock based on @rt data.
1840  * Callers are advives to use rt_routemsg() instead of using this
1841  *  function directly.
1842  * Assume @rt data is consistent.
1843  *
1844  * Returns 0 on success.
1845  */
1846 int
1847 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
1848     int fibnum)
1849 {
1850 	union sockaddr_union dst, mask;
1851 	struct rt_addrinfo info;
1852 
1853 	if (V_route_cb.any_count == 0)
1854 		return (0);
1855 
1856 	int family = rt_get_family(rt);
1857 	init_sockaddrs_family(family, &dst.sa, &mask.sa);
1858 	export_rtaddrs(rt, &dst.sa, &mask.sa);
1859 
1860 	bzero((caddr_t)&info, sizeof(info));
1861 	info.rti_info[RTAX_DST] = &dst.sa;
1862 	info.rti_info[RTAX_NETMASK] = &mask.sa;
1863 	info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
1864 	info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh);
1865 	info.rti_ifp = nh->nh_ifp;
1866 
1867 	return (rtsock_routemsg_info(cmd, &info, fibnum));
1868 }
1869 
1870 int
1871 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
1872 {
1873 	struct rt_msghdr *rtm;
1874 	struct sockaddr *sa;
1875 	struct mbuf *m;
1876 
1877 	if (V_route_cb.any_count == 0)
1878 		return (0);
1879 
1880 	if (info->rti_flags & RTF_HOST)
1881 		info->rti_info[RTAX_NETMASK] = NULL;
1882 
1883 	m = rtsock_msg_mbuf(cmd, info);
1884 	if (m == NULL)
1885 		return (ENOBUFS);
1886 
1887 	if (fibnum != RT_ALL_FIBS) {
1888 		KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1889 		    "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1890 		M_SETFIB(m, fibnum);
1891 		m->m_flags |= RTS_FILTER_FIB;
1892 	}
1893 
1894 	rtm = mtod(m, struct rt_msghdr *);
1895 	rtm->rtm_addrs = info->rti_addrs;
1896 	if (info->rti_ifp != NULL)
1897 		rtm->rtm_index = info->rti_ifp->if_index;
1898 	/* Add RTF_DONE to indicate command 'completion' required by API */
1899 	info->rti_flags |= RTF_DONE;
1900 	/* Reported routes has to be up */
1901 	if (cmd == RTM_ADD || cmd == RTM_CHANGE)
1902 		info->rti_flags |= RTF_UP;
1903 	rtm->rtm_flags = info->rti_flags;
1904 
1905 	sa = info->rti_info[RTAX_DST];
1906 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1907 
1908 	return (0);
1909 }
1910 
1911 /*
1912  * This is the analogue to the rt_newaddrmsg which performs the same
1913  * function but for multicast group memberhips.  This is easier since
1914  * there is no route state to worry about.
1915  */
1916 void
1917 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1918 {
1919 	struct rt_addrinfo info;
1920 	struct mbuf *m = NULL;
1921 	struct ifnet *ifp = ifma->ifma_ifp;
1922 	struct ifma_msghdr *ifmam;
1923 
1924 	if (V_route_cb.any_count == 0)
1925 		return;
1926 
1927 	bzero((caddr_t)&info, sizeof(info));
1928 	info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1929 	if (ifp && ifp->if_addr)
1930 		info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1931 	else
1932 		info.rti_info[RTAX_IFP] = NULL;
1933 	/*
1934 	 * If a link-layer address is present, present it as a ``gateway''
1935 	 * (similarly to how ARP entries, e.g., are presented).
1936 	 */
1937 	info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1938 	m = rtsock_msg_mbuf(cmd, &info);
1939 	if (m == NULL)
1940 		return;
1941 	ifmam = mtod(m, struct ifma_msghdr *);
1942 	KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1943 	    __func__));
1944 	ifmam->ifmam_index = ifp->if_index;
1945 	ifmam->ifmam_addrs = info.rti_addrs;
1946 	rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1947 }
1948 
1949 static struct mbuf *
1950 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1951 	struct rt_addrinfo *info)
1952 {
1953 	struct if_announcemsghdr *ifan;
1954 	struct mbuf *m;
1955 
1956 	if (V_route_cb.any_count == 0)
1957 		return NULL;
1958 	bzero((caddr_t)info, sizeof(*info));
1959 	m = rtsock_msg_mbuf(type, info);
1960 	if (m != NULL) {
1961 		ifan = mtod(m, struct if_announcemsghdr *);
1962 		ifan->ifan_index = ifp->if_index;
1963 		strlcpy(ifan->ifan_name, ifp->if_xname,
1964 			sizeof(ifan->ifan_name));
1965 		ifan->ifan_what = what;
1966 	}
1967 	return m;
1968 }
1969 
1970 /*
1971  * This is called to generate routing socket messages indicating
1972  * IEEE80211 wireless events.
1973  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1974  */
1975 void
1976 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1977 {
1978 	struct mbuf *m;
1979 	struct rt_addrinfo info;
1980 
1981 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1982 	if (m != NULL) {
1983 		/*
1984 		 * Append the ieee80211 data.  Try to stick it in the
1985 		 * mbuf containing the ifannounce msg; otherwise allocate
1986 		 * a new mbuf and append.
1987 		 *
1988 		 * NB: we assume m is a single mbuf.
1989 		 */
1990 		if (data_len > M_TRAILINGSPACE(m)) {
1991 			struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1992 			if (n == NULL) {
1993 				m_freem(m);
1994 				return;
1995 			}
1996 			bcopy(data, mtod(n, void *), data_len);
1997 			n->m_len = data_len;
1998 			m->m_next = n;
1999 		} else if (data_len > 0) {
2000 			bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
2001 			m->m_len += data_len;
2002 		}
2003 		if (m->m_flags & M_PKTHDR)
2004 			m->m_pkthdr.len += data_len;
2005 		mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
2006 		rt_dispatch(m, AF_UNSPEC);
2007 	}
2008 }
2009 
2010 /*
2011  * This is called to generate routing socket messages indicating
2012  * network interface arrival and departure.
2013  */
2014 void
2015 rt_ifannouncemsg(struct ifnet *ifp, int what)
2016 {
2017 	struct mbuf *m;
2018 	struct rt_addrinfo info;
2019 
2020 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
2021 	if (m != NULL)
2022 		rt_dispatch(m, AF_UNSPEC);
2023 }
2024 
2025 static void
2026 rt_dispatch(struct mbuf *m, sa_family_t saf)
2027 {
2028 	struct m_tag *tag;
2029 
2030 	/*
2031 	 * Preserve the family from the sockaddr, if any, in an m_tag for
2032 	 * use when injecting the mbuf into the routing socket buffer from
2033 	 * the netisr.
2034 	 */
2035 	if (saf != AF_UNSPEC) {
2036 		tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
2037 		    M_NOWAIT);
2038 		if (tag == NULL) {
2039 			m_freem(m);
2040 			return;
2041 		}
2042 		*(unsigned short *)(tag + 1) = saf;
2043 		m_tag_prepend(m, tag);
2044 	}
2045 #ifdef VIMAGE
2046 	if (V_loif)
2047 		m->m_pkthdr.rcvif = V_loif;
2048 	else {
2049 		m_freem(m);
2050 		return;
2051 	}
2052 #endif
2053 	netisr_queue(NETISR_ROUTE, m);	/* mbuf is free'd on failure. */
2054 }
2055 
2056 /*
2057  * Checks if rte can be exported v.r.t jails/vnets.
2058  *
2059  * Returns 1 if it can, 0 otherwise.
2060  */
2061 static bool
2062 can_export_rte(struct ucred *td_ucred, bool rt_is_host,
2063     const struct sockaddr *rt_dst)
2064 {
2065 
2066 	if ((!rt_is_host) ? jailed_without_vnet(td_ucred)
2067 	    : prison_if(td_ucred, rt_dst) != 0)
2068 		return (false);
2069 	return (true);
2070 }
2071 
2072 
2073 /*
2074  * This is used in dumping the kernel table via sysctl().
2075  */
2076 static int
2077 sysctl_dumpentry(struct rtentry *rt, void *vw)
2078 {
2079 	struct walkarg *w = vw;
2080 	struct nhop_object *nh;
2081 	int error = 0;
2082 
2083 	NET_EPOCH_ASSERT();
2084 
2085 	export_rtaddrs(rt, w->dst, w->mask);
2086 	if (!can_export_rte(w->w_req->td->td_ucred, rt_is_host(rt), w->dst))
2087 		return (0);
2088 	nh = rt_get_raw_nhop(rt);
2089 #ifdef ROUTE_MPATH
2090 	if (NH_IS_NHGRP(nh)) {
2091 		struct weightened_nhop *wn;
2092 		uint32_t num_nhops;
2093 		wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
2094 		for (int i = 0; i < num_nhops; i++) {
2095 			error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w);
2096 			if (error != 0)
2097 				return (error);
2098 		}
2099 	} else
2100 #endif
2101 		error = sysctl_dumpnhop(rt, nh, rt->rt_weight, w);
2102 
2103 	return (0);
2104 }
2105 
2106 
2107 static int
2108 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight,
2109     struct walkarg *w)
2110 {
2111 	struct rt_addrinfo info;
2112 	int error = 0, size;
2113 	uint32_t rtflags;
2114 
2115 	rtflags = nhop_get_rtflags(nh);
2116 
2117 	if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg))
2118 		return (0);
2119 
2120 	bzero((caddr_t)&info, sizeof(info));
2121 	info.rti_info[RTAX_DST] = w->dst;
2122 	info.rti_info[RTAX_GATEWAY] = &nh->gw_sa;
2123 	info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask;
2124 	info.rti_info[RTAX_GENMASK] = 0;
2125 	if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) {
2126 		info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr;
2127 		info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr;
2128 		if (nh->nh_ifp->if_flags & IFF_POINTOPOINT)
2129 			info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr;
2130 	}
2131 	if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
2132 		return (error);
2133 	if (w->w_req && w->w_tmem) {
2134 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
2135 
2136 		bzero(&rtm->rtm_index,
2137 		    sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
2138 
2139 		/*
2140 		 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags)
2141 		 * and RTF_UP (if entry is linked, which is always true here).
2142 		 * Given that, use nhop rtflags & add RTF_UP.
2143 		 */
2144 		rtm->rtm_flags = rtflags | RTF_UP;
2145 		if (rtm->rtm_flags & RTF_GWFLAG_COMPAT)
2146 			rtm->rtm_flags = RTF_GATEWAY |
2147 				(rtm->rtm_flags & ~RTF_GWFLAG_COMPAT);
2148 		rt_getmetrics(rt, nh, &rtm->rtm_rmx);
2149 		rtm->rtm_rmx.rmx_weight = weight;
2150 		rtm->rtm_index = nh->nh_ifp->if_index;
2151 		rtm->rtm_addrs = info.rti_addrs;
2152 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
2153 		return (error);
2154 	}
2155 	return (error);
2156 }
2157 
2158 static int
2159 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
2160     struct rt_addrinfo *info, struct walkarg *w, int len)
2161 {
2162 	struct if_msghdrl *ifm;
2163 	struct if_data *ifd;
2164 
2165 	ifm = (struct if_msghdrl *)w->w_tmem;
2166 
2167 #ifdef COMPAT_FREEBSD32
2168 	if (w->w_req->flags & SCTL_MASK32) {
2169 		struct if_msghdrl32 *ifm32;
2170 
2171 		ifm32 = (struct if_msghdrl32 *)ifm;
2172 		ifm32->ifm_addrs = info->rti_addrs;
2173 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2174 		ifm32->ifm_index = ifp->if_index;
2175 		ifm32->_ifm_spare1 = 0;
2176 		ifm32->ifm_len = sizeof(*ifm32);
2177 		ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
2178 		ifm32->_ifm_spare2 = 0;
2179 		ifd = &ifm32->ifm_data;
2180 	} else
2181 #endif
2182 	{
2183 		ifm->ifm_addrs = info->rti_addrs;
2184 		ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2185 		ifm->ifm_index = ifp->if_index;
2186 		ifm->_ifm_spare1 = 0;
2187 		ifm->ifm_len = sizeof(*ifm);
2188 		ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
2189 		ifm->_ifm_spare2 = 0;
2190 		ifd = &ifm->ifm_data;
2191 	}
2192 
2193 	memcpy(ifd, src_ifd, sizeof(*ifd));
2194 
2195 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2196 }
2197 
2198 static int
2199 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
2200     struct rt_addrinfo *info, struct walkarg *w, int len)
2201 {
2202 	struct if_msghdr *ifm;
2203 	struct if_data *ifd;
2204 
2205 	ifm = (struct if_msghdr *)w->w_tmem;
2206 
2207 #ifdef COMPAT_FREEBSD32
2208 	if (w->w_req->flags & SCTL_MASK32) {
2209 		struct if_msghdr32 *ifm32;
2210 
2211 		ifm32 = (struct if_msghdr32 *)ifm;
2212 		ifm32->ifm_addrs = info->rti_addrs;
2213 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2214 		ifm32->ifm_index = ifp->if_index;
2215 		ifm32->_ifm_spare1 = 0;
2216 		ifd = &ifm32->ifm_data;
2217 	} else
2218 #endif
2219 	{
2220 		ifm->ifm_addrs = info->rti_addrs;
2221 		ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
2222 		ifm->ifm_index = ifp->if_index;
2223 		ifm->_ifm_spare1 = 0;
2224 		ifd = &ifm->ifm_data;
2225 	}
2226 
2227 	memcpy(ifd, src_ifd, sizeof(*ifd));
2228 
2229 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
2230 }
2231 
2232 static int
2233 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
2234     struct walkarg *w, int len)
2235 {
2236 	struct ifa_msghdrl *ifam;
2237 	struct if_data *ifd;
2238 
2239 	ifam = (struct ifa_msghdrl *)w->w_tmem;
2240 
2241 #ifdef COMPAT_FREEBSD32
2242 	if (w->w_req->flags & SCTL_MASK32) {
2243 		struct ifa_msghdrl32 *ifam32;
2244 
2245 		ifam32 = (struct ifa_msghdrl32 *)ifam;
2246 		ifam32->ifam_addrs = info->rti_addrs;
2247 		ifam32->ifam_flags = ifa->ifa_flags;
2248 		ifam32->ifam_index = ifa->ifa_ifp->if_index;
2249 		ifam32->_ifam_spare1 = 0;
2250 		ifam32->ifam_len = sizeof(*ifam32);
2251 		ifam32->ifam_data_off =
2252 		    offsetof(struct ifa_msghdrl32, ifam_data);
2253 		ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
2254 		ifd = &ifam32->ifam_data;
2255 	} else
2256 #endif
2257 	{
2258 		ifam->ifam_addrs = info->rti_addrs;
2259 		ifam->ifam_flags = ifa->ifa_flags;
2260 		ifam->ifam_index = ifa->ifa_ifp->if_index;
2261 		ifam->_ifam_spare1 = 0;
2262 		ifam->ifam_len = sizeof(*ifam);
2263 		ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
2264 		ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2265 		ifd = &ifam->ifam_data;
2266 	}
2267 
2268 	bzero(ifd, sizeof(*ifd));
2269 	ifd->ifi_datalen = sizeof(struct if_data);
2270 	ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
2271 	ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
2272 	ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
2273 	ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
2274 
2275 	/* Fixup if_data carp(4) vhid. */
2276 	if (carp_get_vhid_p != NULL)
2277 		ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
2278 
2279 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2280 }
2281 
2282 static int
2283 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
2284     struct walkarg *w, int len)
2285 {
2286 	struct ifa_msghdr *ifam;
2287 
2288 	ifam = (struct ifa_msghdr *)w->w_tmem;
2289 	ifam->ifam_addrs = info->rti_addrs;
2290 	ifam->ifam_flags = ifa->ifa_flags;
2291 	ifam->ifam_index = ifa->ifa_ifp->if_index;
2292 	ifam->_ifam_spare1 = 0;
2293 	ifam->ifam_metric = ifa->ifa_ifp->if_metric;
2294 
2295 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
2296 }
2297 
2298 static int
2299 sysctl_iflist(int af, struct walkarg *w)
2300 {
2301 	struct ifnet *ifp;
2302 	struct ifaddr *ifa;
2303 	struct if_data ifd;
2304 	struct rt_addrinfo info;
2305 	int len, error = 0;
2306 	struct sockaddr_storage ss;
2307 
2308 	bzero((caddr_t)&info, sizeof(info));
2309 	bzero(&ifd, sizeof(ifd));
2310 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2311 		if (w->w_arg && w->w_arg != ifp->if_index)
2312 			continue;
2313 		if_data_copy(ifp, &ifd);
2314 		ifa = ifp->if_addr;
2315 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
2316 		error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
2317 		if (error != 0)
2318 			goto done;
2319 		info.rti_info[RTAX_IFP] = NULL;
2320 		if (w->w_req && w->w_tmem) {
2321 			if (w->w_op == NET_RT_IFLISTL)
2322 				error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
2323 				    len);
2324 			else
2325 				error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
2326 				    len);
2327 			if (error)
2328 				goto done;
2329 		}
2330 		while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
2331 			if (af && af != ifa->ifa_addr->sa_family)
2332 				continue;
2333 			if (prison_if(w->w_req->td->td_ucred,
2334 			    ifa->ifa_addr) != 0)
2335 				continue;
2336 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
2337 			info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
2338 			    ifa->ifa_addr, ifa->ifa_netmask, &ss);
2339 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
2340 			error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
2341 			if (error != 0)
2342 				goto done;
2343 			if (w->w_req && w->w_tmem) {
2344 				if (w->w_op == NET_RT_IFLISTL)
2345 					error = sysctl_iflist_ifaml(ifa, &info,
2346 					    w, len);
2347 				else
2348 					error = sysctl_iflist_ifam(ifa, &info,
2349 					    w, len);
2350 				if (error)
2351 					goto done;
2352 			}
2353 		}
2354 		info.rti_info[RTAX_IFA] = NULL;
2355 		info.rti_info[RTAX_NETMASK] = NULL;
2356 		info.rti_info[RTAX_BRD] = NULL;
2357 	}
2358 done:
2359 	return (error);
2360 }
2361 
2362 static int
2363 sysctl_ifmalist(int af, struct walkarg *w)
2364 {
2365 	struct rt_addrinfo info;
2366 	struct ifaddr *ifa;
2367 	struct ifmultiaddr *ifma;
2368 	struct ifnet *ifp;
2369 	int error, len;
2370 
2371 	NET_EPOCH_ASSERT();
2372 
2373 	error = 0;
2374 	bzero((caddr_t)&info, sizeof(info));
2375 
2376 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2377 		if (w->w_arg && w->w_arg != ifp->if_index)
2378 			continue;
2379 		ifa = ifp->if_addr;
2380 		info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
2381 		CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2382 			if (af && af != ifma->ifma_addr->sa_family)
2383 				continue;
2384 			if (prison_if(w->w_req->td->td_ucred,
2385 			    ifma->ifma_addr) != 0)
2386 				continue;
2387 			info.rti_info[RTAX_IFA] = ifma->ifma_addr;
2388 			info.rti_info[RTAX_GATEWAY] =
2389 			    (ifma->ifma_addr->sa_family != AF_LINK) ?
2390 			    ifma->ifma_lladdr : NULL;
2391 			error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
2392 			if (error != 0)
2393 				break;
2394 			if (w->w_req && w->w_tmem) {
2395 				struct ifma_msghdr *ifmam;
2396 
2397 				ifmam = (struct ifma_msghdr *)w->w_tmem;
2398 				ifmam->ifmam_index = ifma->ifma_ifp->if_index;
2399 				ifmam->ifmam_flags = 0;
2400 				ifmam->ifmam_addrs = info.rti_addrs;
2401 				ifmam->_ifmam_spare1 = 0;
2402 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
2403 				if (error != 0)
2404 					break;
2405 			}
2406 		}
2407 		if (error != 0)
2408 			break;
2409 	}
2410 	return (error);
2411 }
2412 
2413 static void
2414 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w)
2415 {
2416 	union sockaddr_union sa_dst, sa_mask;
2417 
2418 	w->family = family;
2419 	w->dst = (struct sockaddr *)&sa_dst;
2420 	w->mask = (struct sockaddr *)&sa_mask;
2421 
2422 	init_sockaddrs_family(family, w->dst, w->mask);
2423 
2424 	rib_walk(fibnum, family, false, sysctl_dumpentry, w);
2425 }
2426 
2427 static int
2428 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
2429 {
2430 	struct epoch_tracker et;
2431 	int	*name = (int *)arg1;
2432 	u_int	namelen = arg2;
2433 	struct rib_head *rnh = NULL; /* silence compiler. */
2434 	int	i, lim, error = EINVAL;
2435 	int	fib = 0;
2436 	u_char	af;
2437 	struct	walkarg w;
2438 
2439 	name ++;
2440 	namelen--;
2441 	if (req->newptr)
2442 		return (EPERM);
2443 	if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) {
2444 		if (namelen == 3)
2445 			fib = req->td->td_proc->p_fibnum;
2446 		else if (namelen == 4)
2447 			fib = (name[3] == RT_ALL_FIBS) ?
2448 			    req->td->td_proc->p_fibnum : name[3];
2449 		else
2450 			return ((namelen < 3) ? EISDIR : ENOTDIR);
2451 		if (fib < 0 || fib >= rt_numfibs)
2452 			return (EINVAL);
2453 	} else if (namelen != 3)
2454 		return ((namelen < 3) ? EISDIR : ENOTDIR);
2455 	af = name[0];
2456 	if (af > AF_MAX)
2457 		return (EINVAL);
2458 	bzero(&w, sizeof(w));
2459 	w.w_op = name[1];
2460 	w.w_arg = name[2];
2461 	w.w_req = req;
2462 
2463 	error = sysctl_wire_old_buffer(req, 0);
2464 	if (error)
2465 		return (error);
2466 
2467 	/*
2468 	 * Allocate reply buffer in advance.
2469 	 * All rtsock messages has maximum length of u_short.
2470 	 */
2471 	w.w_tmemsize = 65536;
2472 	w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
2473 
2474 	NET_EPOCH_ENTER(et);
2475 	switch (w.w_op) {
2476 	case NET_RT_DUMP:
2477 	case NET_RT_FLAGS:
2478 		if (af == 0) {			/* dump all tables */
2479 			i = 1;
2480 			lim = AF_MAX;
2481 		} else				/* dump only one table */
2482 			i = lim = af;
2483 
2484 		/*
2485 		 * take care of llinfo entries, the caller must
2486 		 * specify an AF
2487 		 */
2488 		if (w.w_op == NET_RT_FLAGS &&
2489 		    (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
2490 			if (af != 0)
2491 				error = lltable_sysctl_dumparp(af, w.w_req);
2492 			else
2493 				error = EINVAL;
2494 			break;
2495 		}
2496 		/*
2497 		 * take care of routing entries
2498 		 */
2499 		for (error = 0; error == 0 && i <= lim; i++) {
2500 			rnh = rt_tables_get_rnh(fib, i);
2501 			if (rnh != NULL) {
2502 				rtable_sysctl_dump(fib, i, &w);
2503 			} else if (af != 0)
2504 				error = EAFNOSUPPORT;
2505 		}
2506 		break;
2507 	case NET_RT_NHOP:
2508 	case NET_RT_NHGRP:
2509 		/* Allow dumping one specific af/fib at a time */
2510 		if (namelen < 4) {
2511 			error = EINVAL;
2512 			break;
2513 		}
2514 		fib = name[3];
2515 		if (fib < 0 || fib > rt_numfibs) {
2516 			error = EINVAL;
2517 			break;
2518 		}
2519 		rnh = rt_tables_get_rnh(fib, af);
2520 		if (rnh == NULL) {
2521 			error = EAFNOSUPPORT;
2522 			break;
2523 		}
2524 		if (w.w_op == NET_RT_NHOP)
2525 			error = nhops_dump_sysctl(rnh, w.w_req);
2526 		else
2527 #ifdef ROUTE_MPATH
2528 			error = nhgrp_dump_sysctl(rnh, w.w_req);
2529 #else
2530 			error = ENOTSUP;
2531 #endif
2532 		break;
2533 	case NET_RT_IFLIST:
2534 	case NET_RT_IFLISTL:
2535 		error = sysctl_iflist(af, &w);
2536 		break;
2537 
2538 	case NET_RT_IFMALIST:
2539 		error = sysctl_ifmalist(af, &w);
2540 		break;
2541 	}
2542 	NET_EPOCH_EXIT(et);
2543 
2544 	free(w.w_tmem, M_TEMP);
2545 	return (error);
2546 }
2547 
2548 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE,
2549     sysctl_rtsock, "Return route tables and interface/address lists");
2550 
2551 /*
2552  * Definitions of protocols supported in the ROUTE domain.
2553  */
2554 
2555 static struct domain routedomain;		/* or at least forward */
2556 
2557 static struct protosw routesw[] = {
2558 {
2559 	.pr_type =		SOCK_RAW,
2560 	.pr_domain =		&routedomain,
2561 	.pr_flags =		PR_ATOMIC|PR_ADDR,
2562 	.pr_output =		route_output,
2563 	.pr_ctlinput =		raw_ctlinput,
2564 	.pr_init =		raw_init,
2565 	.pr_usrreqs =		&route_usrreqs
2566 }
2567 };
2568 
2569 static struct domain routedomain = {
2570 	.dom_family =		PF_ROUTE,
2571 	.dom_name =		"route",
2572 	.dom_protosw =		routesw,
2573 	.dom_protoswNPROTOSW =	&routesw[nitems(routesw)]
2574 };
2575 
2576 VNET_DOMAIN_SET(route);
2577