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