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