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