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