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