xref: /freebsd/sys/net/rtsock.c (revision f01edb6f5d55fe093f8ab9c5da2b1707af698a56)
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 #ifdef INET6
564 	struct sockaddr_in6 *sin6;
565 	int i, rti_need_deembed = 0;
566 #endif
567 	int alloc_len = 0, len, error = 0, fibnum;
568 	struct ifnet *ifp = NULL;
569 	union sockaddr_union saun;
570 	sa_family_t saf = AF_UNSPEC;
571 	struct rawcb *rp = NULL;
572 	struct walkarg w;
573 
574 	fibnum = so->so_fibnum;
575 
576 #define senderr(e) { error = e; goto flush;}
577 	if (m == NULL || ((m->m_len < sizeof(long)) &&
578 		       (m = m_pullup(m, sizeof(long))) == NULL))
579 		return (ENOBUFS);
580 	if ((m->m_flags & M_PKTHDR) == 0)
581 		panic("route_output");
582 	len = m->m_pkthdr.len;
583 	if (len < sizeof(*rtm) ||
584 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen)
585 		senderr(EINVAL);
586 
587 	/*
588 	 * Most of current messages are in range 200-240 bytes,
589 	 * minimize possible re-allocation on reply using larger size
590 	 * buffer aligned on 1k boundaty.
591 	 */
592 	alloc_len = roundup2(len, 1024);
593 	if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
594 		senderr(ENOBUFS);
595 
596 	m_copydata(m, 0, len, (caddr_t)rtm);
597 	bzero(&info, sizeof(info));
598 	bzero(&w, sizeof(w));
599 
600 	if (rtm->rtm_version != RTM_VERSION) {
601 		/* Do not touch message since format is unknown */
602 		free(rtm, M_TEMP);
603 		rtm = NULL;
604 		senderr(EPROTONOSUPPORT);
605 	}
606 
607 	/*
608 	 * Starting from here, it is possible
609 	 * to alter original message and insert
610 	 * caller PID and error value.
611 	 */
612 
613 	rtm->rtm_pid = curproc->p_pid;
614 	info.rti_addrs = rtm->rtm_addrs;
615 
616 	info.rti_mflags = rtm->rtm_inits;
617 	info.rti_rmx = &rtm->rtm_rmx;
618 
619 	/*
620 	 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
621 	 * link-local address because rtrequest requires addresses with
622 	 * embedded scope id.
623 	 */
624 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
625 		senderr(EINVAL);
626 
627 	if (rtm->rtm_flags & RTF_RNH_LOCKED)
628 		senderr(EINVAL);
629 	info.rti_flags = rtm->rtm_flags;
630 	if (info.rti_info[RTAX_DST] == NULL ||
631 	    info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
632 	    (info.rti_info[RTAX_GATEWAY] != NULL &&
633 	     info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
634 		senderr(EINVAL);
635 	saf = info.rti_info[RTAX_DST]->sa_family;
636 	/*
637 	 * Verify that the caller has the appropriate privilege; RTM_GET
638 	 * is the only operation the non-superuser is allowed.
639 	 */
640 	if (rtm->rtm_type != RTM_GET) {
641 		error = priv_check(curthread, PRIV_NET_ROUTE);
642 		if (error)
643 			senderr(error);
644 	}
645 
646 	/*
647 	 * The given gateway address may be an interface address.
648 	 * For example, issuing a "route change" command on a route
649 	 * entry that was created from a tunnel, and the gateway
650 	 * address given is the local end point. In this case the
651 	 * RTF_GATEWAY flag must be cleared or the destination will
652 	 * not be reachable even though there is no error message.
653 	 */
654 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
655 	    info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
656 		struct rt_addrinfo ginfo;
657 		struct sockaddr *gdst;
658 
659 		bzero(&ginfo, sizeof(ginfo));
660 		bzero(&ss, sizeof(ss));
661 		ss.ss_len = sizeof(ss);
662 
663 		ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
664 		gdst = info.rti_info[RTAX_GATEWAY];
665 
666 		/*
667 		 * A host route through the loopback interface is
668 		 * installed for each interface adddress. In pre 8.0
669 		 * releases the interface address of a PPP link type
670 		 * is not reachable locally. This behavior is fixed as
671 		 * part of the new L2/L3 redesign and rewrite work. The
672 		 * signature of this interface address route is the
673 		 * AF_LINK sa_family type of the rt_gateway, and the
674 		 * rt_ifp has the IFF_LOOPBACK flag set.
675 		 */
676 		if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
677 			if (ss.ss_family == AF_LINK &&
678 			    ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
679 				info.rti_flags &= ~RTF_GATEWAY;
680 				info.rti_flags |= RTF_GWFLAG_COMPAT;
681 			}
682 			rib_free_info(&ginfo);
683 		}
684 	}
685 
686 	switch (rtm->rtm_type) {
687 		struct rtentry *saved_nrt;
688 
689 	case RTM_ADD:
690 	case RTM_CHANGE:
691 		if (rtm->rtm_type == RTM_ADD) {
692 			if (info.rti_info[RTAX_GATEWAY] == NULL)
693 				senderr(EINVAL);
694 		}
695 		saved_nrt = NULL;
696 
697 		/* support for new ARP code */
698 		if (info.rti_info[RTAX_GATEWAY] != NULL &&
699 		    info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
700 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
701 			error = lla_rt_output(rtm, &info);
702 #ifdef INET6
703 			if (error == 0)
704 				rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
705 #endif
706 			break;
707 		}
708 		error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
709 		    fibnum);
710 		if (error == 0 && saved_nrt != NULL) {
711 #ifdef INET6
712 			rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
713 #endif
714 			RT_LOCK(saved_nrt);
715 			rtm->rtm_index = saved_nrt->rt_ifp->if_index;
716 			RT_REMREF(saved_nrt);
717 			RT_UNLOCK(saved_nrt);
718 		}
719 		break;
720 
721 	case RTM_DELETE:
722 		saved_nrt = NULL;
723 		/* support for new ARP code */
724 		if (info.rti_info[RTAX_GATEWAY] &&
725 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
726 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
727 			error = lla_rt_output(rtm, &info);
728 #ifdef INET6
729 			if (error == 0)
730 				rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
731 #endif
732 			break;
733 		}
734 		error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
735 		if (error == 0) {
736 			RT_LOCK(saved_nrt);
737 			rt = saved_nrt;
738 			goto report;
739 		}
740 #ifdef INET6
741 		/* rt_msg2() will not be used when RTM_DELETE fails. */
742 		rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
743 #endif
744 		break;
745 
746 	case RTM_GET:
747 		rnh = rt_tables_get_rnh(fibnum, saf);
748 		if (rnh == NULL)
749 			senderr(EAFNOSUPPORT);
750 
751 		RIB_RLOCK(rnh);
752 
753 		if (info.rti_info[RTAX_NETMASK] == NULL &&
754 		    rtm->rtm_type == RTM_GET) {
755 			/*
756 			 * Provide longest prefix match for
757 			 * address lookup (no mask).
758 			 * 'route -n get addr'
759 			 */
760 			rt = (struct rtentry *) rnh->rnh_matchaddr(
761 			    info.rti_info[RTAX_DST], &rnh->head);
762 		} else
763 			rt = (struct rtentry *) rnh->rnh_lookup(
764 			    info.rti_info[RTAX_DST],
765 			    info.rti_info[RTAX_NETMASK], &rnh->head);
766 
767 		if (rt == NULL) {
768 			RIB_RUNLOCK(rnh);
769 			senderr(ESRCH);
770 		}
771 #ifdef RADIX_MPATH
772 		/*
773 		 * for RTM_CHANGE/LOCK, if we got multipath routes,
774 		 * we require users to specify a matching RTAX_GATEWAY.
775 		 *
776 		 * for RTM_GET, gate is optional even with multipath.
777 		 * if gate == NULL the first match is returned.
778 		 * (no need to call rt_mpath_matchgate if gate == NULL)
779 		 */
780 		if (rt_mpath_capable(rnh) &&
781 		    (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
782 			rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
783 			if (!rt) {
784 				RIB_RUNLOCK(rnh);
785 				senderr(ESRCH);
786 			}
787 		}
788 #endif
789 		/*
790 		 * If performing proxied L2 entry insertion, and
791 		 * the actual PPP host entry is found, perform
792 		 * another search to retrieve the prefix route of
793 		 * the local end point of the PPP link.
794 		 */
795 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
796 			struct sockaddr laddr;
797 
798 			if (rt->rt_ifp != NULL &&
799 			    rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
800 				struct epoch_tracker et;
801 				struct ifaddr *ifa;
802 
803 				NET_EPOCH_ENTER(et);
804 				ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
805 						RT_ALL_FIBS);
806 				if (ifa != NULL)
807 					rt_maskedcopy(ifa->ifa_addr,
808 						      &laddr,
809 						      ifa->ifa_netmask);
810 				NET_EPOCH_EXIT(et);
811 			} else
812 				rt_maskedcopy(rt->rt_ifa->ifa_addr,
813 					      &laddr,
814 					      rt->rt_ifa->ifa_netmask);
815 			/*
816 			 * refactor rt and no lock operation necessary
817 			 */
818 			rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
819 			    &rnh->head);
820 			if (rt == NULL) {
821 				RIB_RUNLOCK(rnh);
822 				senderr(ESRCH);
823 			}
824 		}
825 		RT_LOCK(rt);
826 		RT_ADDREF(rt);
827 		RIB_RUNLOCK(rnh);
828 
829 report:
830 		RT_LOCK_ASSERT(rt);
831 		if ((rt->rt_flags & RTF_HOST) == 0
832 		    ? jailed_without_vnet(curthread->td_ucred)
833 		    : prison_if(curthread->td_ucred,
834 		    rt_key(rt)) != 0) {
835 			RT_UNLOCK(rt);
836 			senderr(ESRCH);
837 		}
838 		info.rti_info[RTAX_DST] = rt_key(rt);
839 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
840 		info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
841 		    rt_mask(rt), &ss);
842 		info.rti_info[RTAX_GENMASK] = 0;
843 		if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
844 			ifp = rt->rt_ifp;
845 			if (ifp) {
846 				info.rti_info[RTAX_IFP] =
847 				    ifp->if_addr->ifa_addr;
848 				error = rtm_get_jailed(&info, ifp, rt,
849 				    &saun, curthread->td_ucred);
850 				if (error != 0) {
851 					RT_UNLOCK(rt);
852 					senderr(error);
853 				}
854 				if (ifp->if_flags & IFF_POINTOPOINT)
855 					info.rti_info[RTAX_BRD] =
856 					    rt->rt_ifa->ifa_dstaddr;
857 				rtm->rtm_index = ifp->if_index;
858 			} else {
859 				info.rti_info[RTAX_IFP] = NULL;
860 				info.rti_info[RTAX_IFA] = NULL;
861 			}
862 		} else if ((ifp = rt->rt_ifp) != NULL) {
863 			rtm->rtm_index = ifp->if_index;
864 		}
865 
866 		/* Check if we need to realloc storage */
867 		rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
868 		if (len > alloc_len) {
869 			struct rt_msghdr *new_rtm;
870 			new_rtm = malloc(len, M_TEMP, M_NOWAIT);
871 			if (new_rtm == NULL) {
872 				RT_UNLOCK(rt);
873 				senderr(ENOBUFS);
874 			}
875 			bcopy(rtm, new_rtm, rtm->rtm_msglen);
876 			free(rtm, M_TEMP);
877 			rtm = new_rtm;
878 			alloc_len = len;
879 		}
880 
881 		w.w_tmem = (caddr_t)rtm;
882 		w.w_tmemsize = alloc_len;
883 		rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
884 
885 		if (rt->rt_flags & RTF_GWFLAG_COMPAT)
886 			rtm->rtm_flags = RTF_GATEWAY |
887 				(rt->rt_flags & ~RTF_GWFLAG_COMPAT);
888 		else
889 			rtm->rtm_flags = rt->rt_flags;
890 		rt_getmetrics(rt, &rtm->rtm_rmx);
891 		rtm->rtm_addrs = info.rti_addrs;
892 
893 		RT_UNLOCK(rt);
894 		break;
895 
896 	default:
897 		senderr(EOPNOTSUPP);
898 	}
899 
900 flush:
901 	if (rt != NULL)
902 		RTFREE(rt);
903 	/*
904 	 * Check to see if we don't want our own messages.
905 	 */
906 	if ((so->so_options & SO_USELOOPBACK) == 0) {
907 		if (V_route_cb.any_count <= 1) {
908 			if (rtm != NULL)
909 				free(rtm, M_TEMP);
910 			m_freem(m);
911 			return (error);
912 		}
913 		/* There is another listener, so construct message */
914 		rp = sotorawcb(so);
915 	}
916 
917 	if (rtm != NULL) {
918 #ifdef INET6
919 		if (rti_need_deembed) {
920 			/* sin6_scope_id is recovered before sending rtm. */
921 			sin6 = (struct sockaddr_in6 *)&ss;
922 			for (i = 0; i < RTAX_MAX; i++) {
923 				if (info.rti_info[i] == NULL)
924 					continue;
925 				if (info.rti_info[i]->sa_family != AF_INET6)
926 					continue;
927 				bcopy(info.rti_info[i], sin6, sizeof(*sin6));
928 				if (sa6_recoverscope(sin6) == 0)
929 					bcopy(sin6, info.rti_info[i],
930 						    sizeof(*sin6));
931 			}
932 		}
933 #endif
934 		if (error != 0)
935 			rtm->rtm_errno = error;
936 		else
937 			rtm->rtm_flags |= RTF_DONE;
938 
939 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
940 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
941 			m_freem(m);
942 			m = NULL;
943 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
944 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
945 
946 		free(rtm, M_TEMP);
947 	}
948 	if (m != NULL) {
949 		M_SETFIB(m, fibnum);
950 		m->m_flags |= RTS_FILTER_FIB;
951 		if (rp) {
952 			/*
953 			 * XXX insure we don't get a copy by
954 			 * invalidating our protocol
955 			 */
956 			unsigned short family = rp->rcb_proto.sp_family;
957 			rp->rcb_proto.sp_family = 0;
958 			rt_dispatch(m, saf);
959 			rp->rcb_proto.sp_family = family;
960 		} else
961 			rt_dispatch(m, saf);
962 	}
963 
964 	return (error);
965 }
966 
967 static void
968 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
969 {
970 
971 	bzero(out, sizeof(*out));
972 	out->rmx_mtu = rt->rt_mtu;
973 	out->rmx_weight = rt->rt_weight;
974 	out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
975 	/* Kernel -> userland timebase conversion. */
976 	out->rmx_expire = rt->rt_expire ?
977 	    rt->rt_expire - time_uptime + time_second : 0;
978 }
979 
980 /*
981  * Extract the addresses of the passed sockaddrs.
982  * Do a little sanity checking so as to avoid bad memory references.
983  * This data is derived straight from userland.
984  */
985 static int
986 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
987 {
988 	struct sockaddr *sa;
989 	int i;
990 
991 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
992 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
993 			continue;
994 		sa = (struct sockaddr *)cp;
995 		/*
996 		 * It won't fit.
997 		 */
998 		if (cp + sa->sa_len > cplim)
999 			return (EINVAL);
1000 		/*
1001 		 * there are no more.. quit now
1002 		 * If there are more bits, they are in error.
1003 		 * I've seen this. route(1) can evidently generate these.
1004 		 * This causes kernel to core dump.
1005 		 * for compatibility, If we see this, point to a safe address.
1006 		 */
1007 		if (sa->sa_len == 0) {
1008 			rtinfo->rti_info[i] = &sa_zero;
1009 			return (0); /* should be EINVAL but for compat */
1010 		}
1011 		/* accept it */
1012 #ifdef INET6
1013 		if (sa->sa_family == AF_INET6)
1014 			sa6_embedscope((struct sockaddr_in6 *)sa,
1015 			    V_ip6_use_defzone);
1016 #endif
1017 		rtinfo->rti_info[i] = sa;
1018 		cp += SA_SIZE(sa);
1019 	}
1020 	return (0);
1021 }
1022 
1023 /*
1024  * Fill in @dmask with valid netmask leaving original @smask
1025  * intact. Mostly used with radix netmasks.
1026  */
1027 static struct sockaddr *
1028 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1029     struct sockaddr_storage *dmask)
1030 {
1031 	if (dst == NULL || smask == NULL)
1032 		return (NULL);
1033 
1034 	memset(dmask, 0, dst->sa_len);
1035 	memcpy(dmask, smask, smask->sa_len);
1036 	dmask->ss_len = dst->sa_len;
1037 	dmask->ss_family = dst->sa_family;
1038 
1039 	return ((struct sockaddr *)dmask);
1040 }
1041 
1042 /*
1043  * Writes information related to @rtinfo object to newly-allocated mbuf.
1044  * Assumes MCLBYTES is enough to construct any message.
1045  * Used for OS notifications of vaious events (if/ifa announces,etc)
1046  *
1047  * Returns allocated mbuf or NULL on failure.
1048  */
1049 static struct mbuf *
1050 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1051 {
1052 	struct rt_msghdr *rtm;
1053 	struct mbuf *m;
1054 	int i;
1055 	struct sockaddr *sa;
1056 #ifdef INET6
1057 	struct sockaddr_storage ss;
1058 	struct sockaddr_in6 *sin6;
1059 #endif
1060 	int len, dlen;
1061 
1062 	switch (type) {
1063 
1064 	case RTM_DELADDR:
1065 	case RTM_NEWADDR:
1066 		len = sizeof(struct ifa_msghdr);
1067 		break;
1068 
1069 	case RTM_DELMADDR:
1070 	case RTM_NEWMADDR:
1071 		len = sizeof(struct ifma_msghdr);
1072 		break;
1073 
1074 	case RTM_IFINFO:
1075 		len = sizeof(struct if_msghdr);
1076 		break;
1077 
1078 	case RTM_IFANNOUNCE:
1079 	case RTM_IEEE80211:
1080 		len = sizeof(struct if_announcemsghdr);
1081 		break;
1082 
1083 	default:
1084 		len = sizeof(struct rt_msghdr);
1085 	}
1086 
1087 	/* XXXGL: can we use MJUMPAGESIZE cluster here? */
1088 	KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1089 	if (len > MHLEN)
1090 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1091 	else
1092 		m = m_gethdr(M_NOWAIT, MT_DATA);
1093 	if (m == NULL)
1094 		return (m);
1095 
1096 	m->m_pkthdr.len = m->m_len = len;
1097 	rtm = mtod(m, struct rt_msghdr *);
1098 	bzero((caddr_t)rtm, len);
1099 	for (i = 0; i < RTAX_MAX; i++) {
1100 		if ((sa = rtinfo->rti_info[i]) == NULL)
1101 			continue;
1102 		rtinfo->rti_addrs |= (1 << i);
1103 		dlen = SA_SIZE(sa);
1104 #ifdef INET6
1105 		if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1106 			sin6 = (struct sockaddr_in6 *)&ss;
1107 			bcopy(sa, sin6, sizeof(*sin6));
1108 			if (sa6_recoverscope(sin6) == 0)
1109 				sa = (struct sockaddr *)sin6;
1110 		}
1111 #endif
1112 		m_copyback(m, len, dlen, (caddr_t)sa);
1113 		len += dlen;
1114 	}
1115 	if (m->m_pkthdr.len != len) {
1116 		m_freem(m);
1117 		return (NULL);
1118 	}
1119 	rtm->rtm_msglen = len;
1120 	rtm->rtm_version = RTM_VERSION;
1121 	rtm->rtm_type = type;
1122 	return (m);
1123 }
1124 
1125 /*
1126  * Writes information related to @rtinfo object to preallocated buffer.
1127  * Stores needed size in @plen. If @w is NULL, calculates size without
1128  * writing.
1129  * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1130  *
1131  * Returns 0 on success.
1132  *
1133  */
1134 static int
1135 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1136 {
1137 	int i;
1138 	int len, buflen = 0, dlen;
1139 	caddr_t cp = NULL;
1140 	struct rt_msghdr *rtm = NULL;
1141 #ifdef INET6
1142 	struct sockaddr_storage ss;
1143 	struct sockaddr_in6 *sin6;
1144 #endif
1145 #ifdef COMPAT_FREEBSD32
1146 	bool compat32 = false;
1147 #endif
1148 
1149 	switch (type) {
1150 
1151 	case RTM_DELADDR:
1152 	case RTM_NEWADDR:
1153 		if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1154 #ifdef COMPAT_FREEBSD32
1155 			if (w->w_req->flags & SCTL_MASK32) {
1156 				len = sizeof(struct ifa_msghdrl32);
1157 				compat32 = true;
1158 			} else
1159 #endif
1160 				len = sizeof(struct ifa_msghdrl);
1161 		} else
1162 			len = sizeof(struct ifa_msghdr);
1163 		break;
1164 
1165 	case RTM_IFINFO:
1166 #ifdef COMPAT_FREEBSD32
1167 		if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1168 			if (w->w_op == NET_RT_IFLISTL)
1169 				len = sizeof(struct if_msghdrl32);
1170 			else
1171 				len = sizeof(struct if_msghdr32);
1172 			compat32 = true;
1173 			break;
1174 		}
1175 #endif
1176 		if (w != NULL && w->w_op == NET_RT_IFLISTL)
1177 			len = sizeof(struct if_msghdrl);
1178 		else
1179 			len = sizeof(struct if_msghdr);
1180 		break;
1181 
1182 	case RTM_NEWMADDR:
1183 		len = sizeof(struct ifma_msghdr);
1184 		break;
1185 
1186 	default:
1187 		len = sizeof(struct rt_msghdr);
1188 	}
1189 
1190 	if (w != NULL) {
1191 		rtm = (struct rt_msghdr *)w->w_tmem;
1192 		buflen = w->w_tmemsize - len;
1193 		cp = (caddr_t)w->w_tmem + len;
1194 	}
1195 
1196 	rtinfo->rti_addrs = 0;
1197 	for (i = 0; i < RTAX_MAX; i++) {
1198 		struct sockaddr *sa;
1199 
1200 		if ((sa = rtinfo->rti_info[i]) == NULL)
1201 			continue;
1202 		rtinfo->rti_addrs |= (1 << i);
1203 #ifdef COMPAT_FREEBSD32
1204 		if (compat32)
1205 			dlen = SA_SIZE32(sa);
1206 		else
1207 #endif
1208 			dlen = SA_SIZE(sa);
1209 		if (cp != NULL && buflen >= dlen) {
1210 #ifdef INET6
1211 			if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1212 				sin6 = (struct sockaddr_in6 *)&ss;
1213 				bcopy(sa, sin6, sizeof(*sin6));
1214 				if (sa6_recoverscope(sin6) == 0)
1215 					sa = (struct sockaddr *)sin6;
1216 			}
1217 #endif
1218 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
1219 			cp += dlen;
1220 			buflen -= dlen;
1221 		} else if (cp != NULL) {
1222 			/*
1223 			 * Buffer too small. Count needed size
1224 			 * and return with error.
1225 			 */
1226 			cp = NULL;
1227 		}
1228 
1229 		len += dlen;
1230 	}
1231 
1232 	if (cp != NULL) {
1233 		dlen = ALIGN(len) - len;
1234 		if (buflen < dlen)
1235 			cp = NULL;
1236 		else {
1237 			bzero(cp, dlen);
1238 			cp += dlen;
1239 			buflen -= dlen;
1240 		}
1241 	}
1242 	len = ALIGN(len);
1243 
1244 	if (cp != NULL) {
1245 		/* fill header iff buffer is large enough */
1246 		rtm->rtm_version = RTM_VERSION;
1247 		rtm->rtm_type = type;
1248 		rtm->rtm_msglen = len;
1249 	}
1250 
1251 	*plen = len;
1252 
1253 	if (w != NULL && cp == NULL)
1254 		return (ENOBUFS);
1255 
1256 	return (0);
1257 }
1258 
1259 /*
1260  * This routine is called to generate a message from the routing
1261  * socket indicating that a redirect has occurred, a routing lookup
1262  * has failed, or that a protocol has detected timeouts to a particular
1263  * destination.
1264  */
1265 void
1266 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1267     int fibnum)
1268 {
1269 	struct rt_msghdr *rtm;
1270 	struct mbuf *m;
1271 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1272 
1273 	if (V_route_cb.any_count == 0)
1274 		return;
1275 	m = rtsock_msg_mbuf(type, rtinfo);
1276 	if (m == NULL)
1277 		return;
1278 
1279 	if (fibnum != RT_ALL_FIBS) {
1280 		KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1281 		    "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1282 		M_SETFIB(m, fibnum);
1283 		m->m_flags |= RTS_FILTER_FIB;
1284 	}
1285 
1286 	rtm = mtod(m, struct rt_msghdr *);
1287 	rtm->rtm_flags = RTF_DONE | flags;
1288 	rtm->rtm_errno = error;
1289 	rtm->rtm_addrs = rtinfo->rti_addrs;
1290 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1291 }
1292 
1293 void
1294 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1295 {
1296 
1297 	rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1298 }
1299 
1300 /*
1301  * This routine is called to generate a message from the routing
1302  * socket indicating that the status of a network interface has changed.
1303  */
1304 void
1305 rt_ifmsg(struct ifnet *ifp)
1306 {
1307 	struct if_msghdr *ifm;
1308 	struct mbuf *m;
1309 	struct rt_addrinfo info;
1310 
1311 	if (V_route_cb.any_count == 0)
1312 		return;
1313 	bzero((caddr_t)&info, sizeof(info));
1314 	m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1315 	if (m == NULL)
1316 		return;
1317 	ifm = mtod(m, struct if_msghdr *);
1318 	ifm->ifm_index = ifp->if_index;
1319 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1320 	if_data_copy(ifp, &ifm->ifm_data);
1321 	ifm->ifm_addrs = 0;
1322 	rt_dispatch(m, AF_UNSPEC);
1323 }
1324 
1325 /*
1326  * Announce interface address arrival/withdraw.
1327  * Please do not call directly, use rt_addrmsg().
1328  * Assume input data to be valid.
1329  * Returns 0 on success.
1330  */
1331 int
1332 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1333 {
1334 	struct rt_addrinfo info;
1335 	struct sockaddr *sa;
1336 	int ncmd;
1337 	struct mbuf *m;
1338 	struct ifa_msghdr *ifam;
1339 	struct ifnet *ifp = ifa->ifa_ifp;
1340 	struct sockaddr_storage ss;
1341 
1342 	if (V_route_cb.any_count == 0)
1343 		return (0);
1344 
1345 	ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1346 
1347 	bzero((caddr_t)&info, sizeof(info));
1348 	info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1349 	info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1350 	info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1351 	    info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1352 	info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1353 	if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1354 		return (ENOBUFS);
1355 	ifam = mtod(m, struct ifa_msghdr *);
1356 	ifam->ifam_index = ifp->if_index;
1357 	ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1358 	ifam->ifam_flags = ifa->ifa_flags;
1359 	ifam->ifam_addrs = info.rti_addrs;
1360 
1361 	if (fibnum != RT_ALL_FIBS) {
1362 		M_SETFIB(m, fibnum);
1363 		m->m_flags |= RTS_FILTER_FIB;
1364 	}
1365 
1366 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1367 
1368 	return (0);
1369 }
1370 
1371 /*
1372  * Announce route addition/removal.
1373  * Please do not call directly, use rt_routemsg().
1374  * Note that @rt data MAY be inconsistent/invalid:
1375  * if some userland app sends us "invalid" route message (invalid mask,
1376  * no dst, wrong address families, etc...) we need to pass it back
1377  * to app (and any other rtsock consumers) with rtm_errno field set to
1378  * non-zero value.
1379  *
1380  * Returns 0 on success.
1381  */
1382 int
1383 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1384     int fibnum)
1385 {
1386 	struct rt_addrinfo info;
1387 	struct sockaddr *sa;
1388 	struct mbuf *m;
1389 	struct rt_msghdr *rtm;
1390 	struct sockaddr_storage ss;
1391 
1392 	if (V_route_cb.any_count == 0)
1393 		return (0);
1394 
1395 	bzero((caddr_t)&info, sizeof(info));
1396 	info.rti_info[RTAX_DST] = sa = rt_key(rt);
1397 	info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1398 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1399 	if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1400 		return (ENOBUFS);
1401 	rtm = mtod(m, struct rt_msghdr *);
1402 	rtm->rtm_index = ifp->if_index;
1403 	rtm->rtm_flags |= rt->rt_flags;
1404 	rtm->rtm_errno = error;
1405 	rtm->rtm_addrs = info.rti_addrs;
1406 
1407 	if (fibnum != RT_ALL_FIBS) {
1408 		M_SETFIB(m, fibnum);
1409 		m->m_flags |= RTS_FILTER_FIB;
1410 	}
1411 
1412 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1413 
1414 	return (0);
1415 }
1416 
1417 /*
1418  * This is the analogue to the rt_newaddrmsg which performs the same
1419  * function but for multicast group memberhips.  This is easier since
1420  * there is no route state to worry about.
1421  */
1422 void
1423 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1424 {
1425 	struct rt_addrinfo info;
1426 	struct mbuf *m = NULL;
1427 	struct ifnet *ifp = ifma->ifma_ifp;
1428 	struct ifma_msghdr *ifmam;
1429 
1430 	if (V_route_cb.any_count == 0)
1431 		return;
1432 
1433 	bzero((caddr_t)&info, sizeof(info));
1434 	info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1435 	if (ifp && ifp->if_addr)
1436 		info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1437 	else
1438 		info.rti_info[RTAX_IFP] = NULL;
1439 	/*
1440 	 * If a link-layer address is present, present it as a ``gateway''
1441 	 * (similarly to how ARP entries, e.g., are presented).
1442 	 */
1443 	info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1444 	m = rtsock_msg_mbuf(cmd, &info);
1445 	if (m == NULL)
1446 		return;
1447 	ifmam = mtod(m, struct ifma_msghdr *);
1448 	KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1449 	    __func__));
1450 	ifmam->ifmam_index = ifp->if_index;
1451 	ifmam->ifmam_addrs = info.rti_addrs;
1452 	rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1453 }
1454 
1455 static struct mbuf *
1456 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1457 	struct rt_addrinfo *info)
1458 {
1459 	struct if_announcemsghdr *ifan;
1460 	struct mbuf *m;
1461 
1462 	if (V_route_cb.any_count == 0)
1463 		return NULL;
1464 	bzero((caddr_t)info, sizeof(*info));
1465 	m = rtsock_msg_mbuf(type, info);
1466 	if (m != NULL) {
1467 		ifan = mtod(m, struct if_announcemsghdr *);
1468 		ifan->ifan_index = ifp->if_index;
1469 		strlcpy(ifan->ifan_name, ifp->if_xname,
1470 			sizeof(ifan->ifan_name));
1471 		ifan->ifan_what = what;
1472 	}
1473 	return m;
1474 }
1475 
1476 /*
1477  * This is called to generate routing socket messages indicating
1478  * IEEE80211 wireless events.
1479  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1480  */
1481 void
1482 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1483 {
1484 	struct mbuf *m;
1485 	struct rt_addrinfo info;
1486 
1487 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1488 	if (m != NULL) {
1489 		/*
1490 		 * Append the ieee80211 data.  Try to stick it in the
1491 		 * mbuf containing the ifannounce msg; otherwise allocate
1492 		 * a new mbuf and append.
1493 		 *
1494 		 * NB: we assume m is a single mbuf.
1495 		 */
1496 		if (data_len > M_TRAILINGSPACE(m)) {
1497 			struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1498 			if (n == NULL) {
1499 				m_freem(m);
1500 				return;
1501 			}
1502 			bcopy(data, mtod(n, void *), data_len);
1503 			n->m_len = data_len;
1504 			m->m_next = n;
1505 		} else if (data_len > 0) {
1506 			bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1507 			m->m_len += data_len;
1508 		}
1509 		if (m->m_flags & M_PKTHDR)
1510 			m->m_pkthdr.len += data_len;
1511 		mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1512 		rt_dispatch(m, AF_UNSPEC);
1513 	}
1514 }
1515 
1516 /*
1517  * This is called to generate routing socket messages indicating
1518  * network interface arrival and departure.
1519  */
1520 void
1521 rt_ifannouncemsg(struct ifnet *ifp, int what)
1522 {
1523 	struct mbuf *m;
1524 	struct rt_addrinfo info;
1525 
1526 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1527 	if (m != NULL)
1528 		rt_dispatch(m, AF_UNSPEC);
1529 }
1530 
1531 static void
1532 rt_dispatch(struct mbuf *m, sa_family_t saf)
1533 {
1534 	struct m_tag *tag;
1535 
1536 	/*
1537 	 * Preserve the family from the sockaddr, if any, in an m_tag for
1538 	 * use when injecting the mbuf into the routing socket buffer from
1539 	 * the netisr.
1540 	 */
1541 	if (saf != AF_UNSPEC) {
1542 		tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1543 		    M_NOWAIT);
1544 		if (tag == NULL) {
1545 			m_freem(m);
1546 			return;
1547 		}
1548 		*(unsigned short *)(tag + 1) = saf;
1549 		m_tag_prepend(m, tag);
1550 	}
1551 #ifdef VIMAGE
1552 	if (V_loif)
1553 		m->m_pkthdr.rcvif = V_loif;
1554 	else {
1555 		m_freem(m);
1556 		return;
1557 	}
1558 #endif
1559 	netisr_queue(NETISR_ROUTE, m);	/* mbuf is free'd on failure. */
1560 }
1561 
1562 /*
1563  * This is used in dumping the kernel table via sysctl().
1564  */
1565 static int
1566 sysctl_dumpentry(struct radix_node *rn, void *vw)
1567 {
1568 	struct walkarg *w = vw;
1569 	struct rtentry *rt = (struct rtentry *)rn;
1570 	int error = 0, size;
1571 	struct rt_addrinfo info;
1572 	struct sockaddr_storage ss;
1573 
1574 	NET_EPOCH_ASSERT();
1575 
1576 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1577 		return 0;
1578 	if ((rt->rt_flags & RTF_HOST) == 0
1579 	    ? jailed_without_vnet(w->w_req->td->td_ucred)
1580 	    : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1581 		return (0);
1582 	bzero((caddr_t)&info, sizeof(info));
1583 	info.rti_info[RTAX_DST] = rt_key(rt);
1584 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1585 	info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1586 	    rt_mask(rt), &ss);
1587 	info.rti_info[RTAX_GENMASK] = 0;
1588 	if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1589 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1590 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1591 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1592 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1593 	}
1594 	if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1595 		return (error);
1596 	if (w->w_req && w->w_tmem) {
1597 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1598 
1599 		bzero(&rtm->rtm_index,
1600 		    sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1601 		if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1602 			rtm->rtm_flags = RTF_GATEWAY |
1603 				(rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1604 		else
1605 			rtm->rtm_flags = rt->rt_flags;
1606 		rt_getmetrics(rt, &rtm->rtm_rmx);
1607 		rtm->rtm_index = rt->rt_ifp->if_index;
1608 		rtm->rtm_addrs = info.rti_addrs;
1609 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1610 		return (error);
1611 	}
1612 	return (error);
1613 }
1614 
1615 static int
1616 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1617     struct rt_addrinfo *info, struct walkarg *w, int len)
1618 {
1619 	struct if_msghdrl *ifm;
1620 	struct if_data *ifd;
1621 
1622 	ifm = (struct if_msghdrl *)w->w_tmem;
1623 
1624 #ifdef COMPAT_FREEBSD32
1625 	if (w->w_req->flags & SCTL_MASK32) {
1626 		struct if_msghdrl32 *ifm32;
1627 
1628 		ifm32 = (struct if_msghdrl32 *)ifm;
1629 		ifm32->ifm_addrs = info->rti_addrs;
1630 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1631 		ifm32->ifm_index = ifp->if_index;
1632 		ifm32->_ifm_spare1 = 0;
1633 		ifm32->ifm_len = sizeof(*ifm32);
1634 		ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1635 		ifm32->_ifm_spare2 = 0;
1636 		ifd = &ifm32->ifm_data;
1637 	} else
1638 #endif
1639 	{
1640 		ifm->ifm_addrs = info->rti_addrs;
1641 		ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1642 		ifm->ifm_index = ifp->if_index;
1643 		ifm->_ifm_spare1 = 0;
1644 		ifm->ifm_len = sizeof(*ifm);
1645 		ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1646 		ifm->_ifm_spare2 = 0;
1647 		ifd = &ifm->ifm_data;
1648 	}
1649 
1650 	memcpy(ifd, src_ifd, sizeof(*ifd));
1651 
1652 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1653 }
1654 
1655 static int
1656 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1657     struct rt_addrinfo *info, struct walkarg *w, int len)
1658 {
1659 	struct if_msghdr *ifm;
1660 	struct if_data *ifd;
1661 
1662 	ifm = (struct if_msghdr *)w->w_tmem;
1663 
1664 #ifdef COMPAT_FREEBSD32
1665 	if (w->w_req->flags & SCTL_MASK32) {
1666 		struct if_msghdr32 *ifm32;
1667 
1668 		ifm32 = (struct if_msghdr32 *)ifm;
1669 		ifm32->ifm_addrs = info->rti_addrs;
1670 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1671 		ifm32->ifm_index = ifp->if_index;
1672 		ifm32->_ifm_spare1 = 0;
1673 		ifd = &ifm32->ifm_data;
1674 	} else
1675 #endif
1676 	{
1677 		ifm->ifm_addrs = info->rti_addrs;
1678 		ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1679 		ifm->ifm_index = ifp->if_index;
1680 		ifm->_ifm_spare1 = 0;
1681 		ifd = &ifm->ifm_data;
1682 	}
1683 
1684 	memcpy(ifd, src_ifd, sizeof(*ifd));
1685 
1686 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1687 }
1688 
1689 static int
1690 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1691     struct walkarg *w, int len)
1692 {
1693 	struct ifa_msghdrl *ifam;
1694 	struct if_data *ifd;
1695 
1696 	ifam = (struct ifa_msghdrl *)w->w_tmem;
1697 
1698 #ifdef COMPAT_FREEBSD32
1699 	if (w->w_req->flags & SCTL_MASK32) {
1700 		struct ifa_msghdrl32 *ifam32;
1701 
1702 		ifam32 = (struct ifa_msghdrl32 *)ifam;
1703 		ifam32->ifam_addrs = info->rti_addrs;
1704 		ifam32->ifam_flags = ifa->ifa_flags;
1705 		ifam32->ifam_index = ifa->ifa_ifp->if_index;
1706 		ifam32->_ifam_spare1 = 0;
1707 		ifam32->ifam_len = sizeof(*ifam32);
1708 		ifam32->ifam_data_off =
1709 		    offsetof(struct ifa_msghdrl32, ifam_data);
1710 		ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1711 		ifd = &ifam32->ifam_data;
1712 	} else
1713 #endif
1714 	{
1715 		ifam->ifam_addrs = info->rti_addrs;
1716 		ifam->ifam_flags = ifa->ifa_flags;
1717 		ifam->ifam_index = ifa->ifa_ifp->if_index;
1718 		ifam->_ifam_spare1 = 0;
1719 		ifam->ifam_len = sizeof(*ifam);
1720 		ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1721 		ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1722 		ifd = &ifam->ifam_data;
1723 	}
1724 
1725 	bzero(ifd, sizeof(*ifd));
1726 	ifd->ifi_datalen = sizeof(struct if_data);
1727 	ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1728 	ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1729 	ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1730 	ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1731 
1732 	/* Fixup if_data carp(4) vhid. */
1733 	if (carp_get_vhid_p != NULL)
1734 		ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1735 
1736 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1737 }
1738 
1739 static int
1740 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1741     struct walkarg *w, int len)
1742 {
1743 	struct ifa_msghdr *ifam;
1744 
1745 	ifam = (struct ifa_msghdr *)w->w_tmem;
1746 	ifam->ifam_addrs = info->rti_addrs;
1747 	ifam->ifam_flags = ifa->ifa_flags;
1748 	ifam->ifam_index = ifa->ifa_ifp->if_index;
1749 	ifam->_ifam_spare1 = 0;
1750 	ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1751 
1752 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1753 }
1754 
1755 static int
1756 sysctl_iflist(int af, struct walkarg *w)
1757 {
1758 	struct ifnet *ifp;
1759 	struct ifaddr *ifa;
1760 	struct if_data ifd;
1761 	struct rt_addrinfo info;
1762 	int len, error = 0;
1763 	struct sockaddr_storage ss;
1764 	struct epoch_tracker et;
1765 
1766 	bzero((caddr_t)&info, sizeof(info));
1767 	bzero(&ifd, sizeof(ifd));
1768 	NET_EPOCH_ENTER(et);
1769 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1770 		if (w->w_arg && w->w_arg != ifp->if_index)
1771 			continue;
1772 		if_data_copy(ifp, &ifd);
1773 		ifa = ifp->if_addr;
1774 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1775 		error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1776 		if (error != 0)
1777 			goto done;
1778 		info.rti_info[RTAX_IFP] = NULL;
1779 		if (w->w_req && w->w_tmem) {
1780 			if (w->w_op == NET_RT_IFLISTL)
1781 				error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1782 				    len);
1783 			else
1784 				error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1785 				    len);
1786 			if (error)
1787 				goto done;
1788 		}
1789 		while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1790 			if (af && af != ifa->ifa_addr->sa_family)
1791 				continue;
1792 			if (prison_if(w->w_req->td->td_ucred,
1793 			    ifa->ifa_addr) != 0)
1794 				continue;
1795 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1796 			info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1797 			    ifa->ifa_addr, ifa->ifa_netmask, &ss);
1798 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1799 			error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1800 			if (error != 0)
1801 				goto done;
1802 			if (w->w_req && w->w_tmem) {
1803 				if (w->w_op == NET_RT_IFLISTL)
1804 					error = sysctl_iflist_ifaml(ifa, &info,
1805 					    w, len);
1806 				else
1807 					error = sysctl_iflist_ifam(ifa, &info,
1808 					    w, len);
1809 				if (error)
1810 					goto done;
1811 			}
1812 		}
1813 		info.rti_info[RTAX_IFA] = NULL;
1814 		info.rti_info[RTAX_NETMASK] = NULL;
1815 		info.rti_info[RTAX_BRD] = NULL;
1816 	}
1817 done:
1818 	NET_EPOCH_EXIT(et);
1819 	return (error);
1820 }
1821 
1822 static int
1823 sysctl_ifmalist(int af, struct walkarg *w)
1824 {
1825 	struct rt_addrinfo info;
1826 	struct epoch_tracker et;
1827 	struct ifaddr *ifa;
1828 	struct ifmultiaddr *ifma;
1829 	struct ifnet *ifp;
1830 	int error, len;
1831 
1832 	error = 0;
1833 	bzero((caddr_t)&info, sizeof(info));
1834 
1835 	NET_EPOCH_ENTER(et);
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 	NET_EPOCH_EXIT(et);
1871 	return (error);
1872 }
1873 
1874 static int
1875 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1876 {
1877 	RIB_RLOCK_TRACKER;
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 	switch (w.w_op) {
1922 
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 				struct epoch_tracker et;
1950 
1951 				RIB_RLOCK(rnh);
1952 				NET_EPOCH_ENTER(et);
1953 			    	error = rnh->rnh_walktree(&rnh->head,
1954 				    sysctl_dumpentry, &w);
1955 				NET_EPOCH_EXIT(et);
1956 				RIB_RUNLOCK(rnh);
1957 			} else if (af != 0)
1958 				error = EAFNOSUPPORT;
1959 		}
1960 		break;
1961 
1962 	case NET_RT_IFLIST:
1963 	case NET_RT_IFLISTL:
1964 		error = sysctl_iflist(af, &w);
1965 		break;
1966 
1967 	case NET_RT_IFMALIST:
1968 		error = sysctl_ifmalist(af, &w);
1969 		break;
1970 	}
1971 
1972 	free(w.w_tmem, M_TEMP);
1973 	return (error);
1974 }
1975 
1976 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1977 
1978 /*
1979  * Definitions of protocols supported in the ROUTE domain.
1980  */
1981 
1982 static struct domain routedomain;		/* or at least forward */
1983 
1984 static struct protosw routesw[] = {
1985 {
1986 	.pr_type =		SOCK_RAW,
1987 	.pr_domain =		&routedomain,
1988 	.pr_flags =		PR_ATOMIC|PR_ADDR,
1989 	.pr_output =		route_output,
1990 	.pr_ctlinput =		raw_ctlinput,
1991 	.pr_init =		raw_init,
1992 	.pr_usrreqs =		&route_usrreqs
1993 }
1994 };
1995 
1996 static struct domain routedomain = {
1997 	.dom_family =		PF_ROUTE,
1998 	.dom_name =		 "route",
1999 	.dom_protosw =		routesw,
2000 	.dom_protoswNPROTOSW =	&routesw[nitems(routesw)]
2001 };
2002 
2003 VNET_DOMAIN_SET(route);
2004 
2005 #ifdef DDB
2006 /*
2007  * Unfortunately, RTF_ values are expressed as raw masks rather than powers of
2008  * 2, so we cannot use them as nice C99 initializer indices below.
2009  */
2010 static const char * const rtf_flag_strings[] = {
2011 	"UP",
2012 	"GATEWAY",
2013 	"HOST",
2014 	"REJECT",
2015 	"DYNAMIC",
2016 	"MODIFIED",
2017 	"DONE",
2018 	"UNUSED_0x80",
2019 	"UNUSED_0x100",
2020 	"XRESOLVE",
2021 	"LLDATA",
2022 	"STATIC",
2023 	"BLACKHOLE",
2024 	"UNUSED_0x2000",
2025 	"PROTO2",
2026 	"PROTO1",
2027 	"UNUSED_0x10000",
2028 	"UNUSED_0x20000",
2029 	"PROTO3",
2030 	"FIXEDMTU",
2031 	"PINNED",
2032 	"LOCAL",
2033 	"BROADCAST",
2034 	"MULTICAST",
2035 	/* Big gap. */
2036 	[28] = "STICKY",
2037 	[30] = "RNH_LOCKED",
2038 	[31] = "GWFLAG_COMPAT",
2039 };
2040 
2041 static const char * __pure
2042 rt_flag_name(unsigned idx)
2043 {
2044 	if (idx >= nitems(rtf_flag_strings))
2045 		return ("INVALID_FLAG");
2046 	if (rtf_flag_strings[idx] == NULL)
2047 		return ("UNKNOWN");
2048 	return (rtf_flag_strings[idx]);
2049 }
2050 
2051 static void
2052 rt_dumpaddr_ddb(const char *name, const struct sockaddr *sa)
2053 {
2054 	char buf[INET6_ADDRSTRLEN], *res;
2055 
2056 	res = NULL;
2057 	if (sa == NULL)
2058 		res = "NULL";
2059 	else if (sa->sa_family == AF_INET) {
2060 		res = inet_ntop(AF_INET,
2061 		    &((const struct sockaddr_in *)sa)->sin_addr,
2062 		    buf, sizeof(buf));
2063 	} else if (sa->sa_family == AF_INET6) {
2064 		res = inet_ntop(AF_INET6,
2065 		    &((const struct sockaddr_in6 *)sa)->sin6_addr,
2066 		    buf, sizeof(buf));
2067 	} else if (sa->sa_family == AF_LINK) {
2068 		res = "on link";
2069 	}
2070 
2071 	if (res != NULL) {
2072 		db_printf("%s <%s> ", name, res);
2073 		return;
2074 	}
2075 
2076 	db_printf("%s <af:%d> ", name, sa->sa_family);
2077 }
2078 
2079 static int
2080 rt_dumpentry_ddb(struct radix_node *rn, void *arg __unused)
2081 {
2082 	struct sockaddr_storage ss;
2083 	struct rtentry *rt;
2084 	int flags, idx;
2085 
2086 	/* If RNTORT is important, put it in a header. */
2087 	rt = (void *)rn;
2088 
2089 	rt_dumpaddr_ddb("dst", rt_key(rt));
2090 	rt_dumpaddr_ddb("gateway", rt->rt_gateway);
2091 	rt_dumpaddr_ddb("netmask", rtsock_fix_netmask(rt_key(rt), rt_mask(rt),
2092 	    &ss));
2093 	if (rt->rt_ifp != NULL && (rt->rt_ifp->if_flags & IFF_DYING) == 0) {
2094 		rt_dumpaddr_ddb("ifp", rt->rt_ifp->if_addr->ifa_addr);
2095 		rt_dumpaddr_ddb("ifa", rt->rt_ifa->ifa_addr);
2096 	}
2097 
2098 	db_printf("flags ");
2099 	flags = rt->rt_flags;
2100 	if (flags == 0)
2101 		db_printf("none");
2102 
2103 	while ((idx = ffs(flags)) > 0) {
2104 		idx--;
2105 
2106 		if (flags != rt->rt_flags)
2107 			db_printf(",");
2108 		db_printf("%s", rt_flag_name(idx));
2109 
2110 		flags &= ~(1ul << idx);
2111 	}
2112 
2113 	db_printf("\n");
2114 	return (0);
2115 }
2116 
2117 DB_SHOW_COMMAND(routetable, db_show_routetable_cmd)
2118 {
2119 	struct rib_head *rnh;
2120 	int error, i, lim;
2121 
2122 	if (have_addr)
2123 		i = lim = addr;
2124 	else {
2125 		i = 1;
2126 		lim = AF_MAX;
2127 	}
2128 
2129 	for (; i <= lim; i++) {
2130 		rnh = rt_tables_get_rnh(0, i);
2131 		if (rnh == NULL) {
2132 			if (have_addr) {
2133 				db_printf("%s: AF %d not supported?\n",
2134 				    __func__, i);
2135 				break;
2136 			}
2137 			continue;
2138 		}
2139 
2140 		if (!have_addr && i > 1)
2141 			db_printf("\n");
2142 
2143 		db_printf("Route table for AF %d%s%s%s:\n", i,
2144 		    (i == AF_INET || i == AF_INET6) ? " (" : "",
2145 		    (i == AF_INET) ? "INET" : (i == AF_INET6) ? "INET6" : "",
2146 		    (i == AF_INET || i == AF_INET6) ? ")" : "");
2147 
2148 		error = rnh->rnh_walktree(&rnh->head, rt_dumpentry_ddb, NULL);
2149 		if (error != 0)
2150 			db_printf("%s: walktree(%d): %d\n", __func__, i,
2151 			    error);
2152 	}
2153 }
2154 
2155 _DB_FUNC(_show, route, db_show_route_cmd, db_show_table, CS_OWN, NULL)
2156 {
2157 	char buf[INET6_ADDRSTRLEN], *bp;
2158 	const void *dst_addrp;
2159 	struct sockaddr *dstp;
2160 	struct rtentry *rt;
2161 	union {
2162 		struct sockaddr_in dest_sin;
2163 		struct sockaddr_in6 dest_sin6;
2164 	} u;
2165 	uint16_t hextets[8];
2166 	unsigned i, tets;
2167 	int t, af, exp, tokflags;
2168 
2169 	/*
2170 	 * Undecoded address family.  No double-colon expansion seen yet.
2171 	 */
2172 	af = -1;
2173 	exp = -1;
2174 	/* Assume INET6 to start; we can work back if guess was wrong. */
2175 	tokflags = DRT_WSPACE | DRT_HEX | DRT_HEXADECIMAL;
2176 
2177 	/*
2178 	 * db_command has lexed 'show route' for us.
2179 	 */
2180 	t = db_read_token_flags(tokflags);
2181 	if (t == tWSPACE)
2182 		t = db_read_token_flags(tokflags);
2183 
2184 	/*
2185 	 * tEOL: Just 'show route' isn't a valid mode.
2186 	 * tMINUS: It's either '-h' or some invalid option.  Regardless, usage.
2187 	 */
2188 	if (t == tEOL || t == tMINUS)
2189 		goto usage;
2190 
2191 	db_unread_token(t);
2192 
2193 	tets = nitems(hextets);
2194 
2195 	/*
2196 	 * Each loop iteration, we expect to read one octet (v4) or hextet
2197 	 * (v6), followed by an appropriate field separator ('.' or ':' or
2198 	 * '::').
2199 	 *
2200 	 * At the start of each loop, we're looking for a number (octet or
2201 	 * hextet).
2202 	 *
2203 	 * INET6 addresses have a special case where they may begin with '::'.
2204 	 */
2205 	for (i = 0; i < tets; i++) {
2206 		t = db_read_token_flags(tokflags);
2207 
2208 		if (t == tCOLONCOLON) {
2209 			/* INET6 with leading '::' or invalid. */
2210 			if (i != 0) {
2211 				db_printf("Parse error: unexpected extra "
2212 				    "colons.\n");
2213 				goto exit;
2214 			}
2215 
2216 			af = AF_INET6;
2217 			exp = i;
2218 			hextets[i] = 0;
2219 			continue;
2220 		} else if (t == tNUMBER) {
2221 			/*
2222 			 * Lexer separates out '-' as tMINUS, but make the
2223 			 * assumption explicit here.
2224 			 */
2225 			MPASS(db_tok_number >= 0);
2226 
2227 			if (af == AF_INET && db_tok_number > UINT8_MAX) {
2228 				db_printf("Not a valid v4 octet: %ld\n",
2229 				    (long)db_tok_number);
2230 				goto exit;
2231 			}
2232 			hextets[i] = db_tok_number;
2233 		} else if (t == tEOL) {
2234 			/*
2235 			 * We can only detect the end of an IPv6 address in
2236 			 * compact representation with EOL.
2237 			 */
2238 			if (af != AF_INET6 || exp < 0) {
2239 				db_printf("Parse failed.  Got unexpected EOF "
2240 				    "when the address is not a compact-"
2241 				    "representation IPv6 address.\n");
2242 				goto exit;
2243 			}
2244 			break;
2245 		} else {
2246 			db_printf("Parse failed.  Unexpected token %d.\n", t);
2247 			goto exit;
2248 		}
2249 
2250 		/* Next, look for a separator, if appropriate. */
2251 		if (i == tets - 1)
2252 			continue;
2253 
2254 		t = db_read_token_flags(tokflags);
2255 		if (af < 0) {
2256 			if (t == tCOLON) {
2257 				af = AF_INET6;
2258 				continue;
2259 			}
2260 			if (t == tCOLONCOLON) {
2261 				af = AF_INET6;
2262 				i++;
2263 				hextets[i] = 0;
2264 				exp = i;
2265 				continue;
2266 			}
2267 			if (t == tDOT) {
2268 				unsigned hn, dn;
2269 
2270 				af = AF_INET;
2271 				/* Need to fixup the first parsed number. */
2272 				if (hextets[0] > 0x255 ||
2273 				    (hextets[0] & 0xf0) > 0x90 ||
2274 				    (hextets[0] & 0xf) > 9) {
2275 					db_printf("Not a valid v4 octet: %x\n",
2276 					    hextets[0]);
2277 					goto exit;
2278 				}
2279 
2280 				hn = hextets[0];
2281 				dn = (hn >> 8) * 100 +
2282 				    ((hn >> 4) & 0xf) * 10 +
2283 				    (hn & 0xf);
2284 
2285 				hextets[0] = dn;
2286 
2287 				/* Switch to decimal for remaining octets. */
2288 				tokflags &= ~DRT_RADIX_MASK;
2289 				tokflags |= DRT_DECIMAL;
2290 
2291 				tets = 4;
2292 				continue;
2293 			}
2294 
2295 			db_printf("Parse error.  Unexpected token %d.\n", t);
2296 			goto exit;
2297 		} else if (af == AF_INET) {
2298 			if (t == tDOT)
2299 				continue;
2300 			db_printf("Expected '.' (%d) between octets but got "
2301 			    "(%d).\n", tDOT, t);
2302 			goto exit;
2303 
2304 		} else if (af == AF_INET6) {
2305 			if (t == tCOLON)
2306 				continue;
2307 			if (t == tCOLONCOLON) {
2308 				if (exp < 0) {
2309 					i++;
2310 					hextets[i] = 0;
2311 					exp = i;
2312 					continue;
2313 				}
2314 				db_printf("Got bogus second '::' in v6 "
2315 				    "address.\n");
2316 				goto exit;
2317 			}
2318 			if (t == tEOL) {
2319 				/*
2320 				 * Handle in the earlier part of the loop
2321 				 * because we need to handle trailing :: too.
2322 				 */
2323 				db_unread_token(t);
2324 				continue;
2325 			}
2326 
2327 			db_printf("Expected ':' (%d) or '::' (%d) between "
2328 			    "hextets but got (%d).\n", tCOLON, tCOLONCOLON, t);
2329 			goto exit;
2330 		}
2331 	}
2332 
2333 	/* Check for trailing garbage. */
2334 	if (i == tets) {
2335 		t = db_read_token_flags(tokflags);
2336 		if (t != tEOL) {
2337 			db_printf("Got unexpected garbage after address "
2338 			    "(%d).\n", t);
2339 			goto exit;
2340 		}
2341 	}
2342 
2343 	/*
2344 	 * Need to expand compact INET6 addresses.
2345 	 *
2346 	 * Technically '::' for a single ':0:' is MUST NOT but just in case,
2347 	 * don't bother expanding that form (exp >= 0 && i == tets case).
2348 	 */
2349 	if (af == AF_INET6 && exp >= 0 && i < tets) {
2350 		if (exp + 1 < i) {
2351 			memmove(&hextets[exp + 1 + (nitems(hextets) - i)],
2352 			    &hextets[exp + 1],
2353 			    (i - (exp + 1)) * sizeof(hextets[0]));
2354 		}
2355 		memset(&hextets[exp + 1], 0, (nitems(hextets) - i) *
2356 		    sizeof(hextets[0]));
2357 	}
2358 
2359 	memset(&u, 0, sizeof(u));
2360 	if (af == AF_INET) {
2361 		u.dest_sin.sin_family = AF_INET;
2362 		u.dest_sin.sin_len = sizeof(u.dest_sin);
2363 		u.dest_sin.sin_addr.s_addr = htonl(
2364 		    ((uint32_t)hextets[0] << 24) |
2365 		    ((uint32_t)hextets[1] << 16) |
2366 		    ((uint32_t)hextets[2] << 8) |
2367 		    (uint32_t)hextets[3]);
2368 		dstp = (void *)&u.dest_sin;
2369 		dst_addrp = &u.dest_sin.sin_addr;
2370 	} else if (af == AF_INET6) {
2371 		u.dest_sin6.sin6_family = AF_INET6;
2372 		u.dest_sin6.sin6_len = sizeof(u.dest_sin6);
2373 		for (i = 0; i < nitems(hextets); i++)
2374 			u.dest_sin6.sin6_addr.s6_addr16[i] = htons(hextets[i]);
2375 		dstp = (void *)&u.dest_sin6;
2376 		dst_addrp = &u.dest_sin6.sin6_addr;
2377 	} else {
2378 		MPASS(false);
2379 		/* UNREACHABLE */
2380 		/* Appease Clang false positive: */
2381 		dstp = NULL;
2382 	}
2383 
2384 	bp = inet_ntop(af, dst_addrp, buf, sizeof(buf));
2385 	if (bp != NULL)
2386 		db_printf("Looking up route to destination '%s'\n", bp);
2387 
2388 	CURVNET_SET(vnet0);
2389 	rt = rtalloc1(dstp, 0, RTF_RNH_LOCKED);
2390 	CURVNET_RESTORE();
2391 
2392 	if (rt == NULL) {
2393 		db_printf("Could not get route for that server.\n");
2394 		return;
2395 	}
2396 
2397 	rt_dumpentry_ddb((void *)rt, NULL);
2398 	RTFREE_LOCKED(rt);
2399 
2400 	return;
2401 usage:
2402 	db_printf("Usage: 'show route <address>'\n"
2403 	    "  Currently accepts only dotted-decimal INET or colon-separated\n"
2404 	    "  hextet INET6 addresses.\n");
2405 exit:
2406 	db_skip_to_eol();
2407 }
2408 #endif
2409