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