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