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