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