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