xref: /freebsd/sys/net/rtsock.c (revision 5c52a79884070364bfc920fb8e492cfac61ec72f)
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_sctp.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/domain.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/protosw.h>
46 #include <sys/signalvar.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/vimage.h>
52 
53 #include <net/if.h>
54 #include <net/netisr.h>
55 #include <net/raw_cb.h>
56 #include <net/route.h>
57 #include <net/vnet.h>
58 
59 #include <netinet/in.h>
60 #ifdef INET6
61 #include <netinet6/scope6_var.h>
62 #endif
63 
64 #ifdef SCTP
65 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
66 #endif /* SCTP */
67 
68 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
69 
70 /* NB: these are not modified */
71 static struct	sockaddr route_src = { 2, PF_ROUTE, };
72 static struct	sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
73 
74 static struct {
75 	int	ip_count;	/* attached w/ AF_INET */
76 	int	ip6_count;	/* attached w/ AF_INET6 */
77 	int	ipx_count;	/* attached w/ AF_IPX */
78 	int	any_count;	/* total attached */
79 } route_cb;
80 
81 struct mtx rtsock_mtx;
82 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
83 
84 #define	RTSOCK_LOCK()	mtx_lock(&rtsock_mtx)
85 #define	RTSOCK_UNLOCK()	mtx_unlock(&rtsock_mtx)
86 #define	RTSOCK_LOCK_ASSERT()	mtx_assert(&rtsock_mtx, MA_OWNED)
87 
88 static struct	ifqueue rtsintrq;
89 
90 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
91 SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW,
92     &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length");
93 
94 struct walkarg {
95 	int	w_tmemsize;
96 	int	w_op, w_arg;
97 	caddr_t	w_tmem;
98 	struct sysctl_req *w_req;
99 };
100 
101 static void	rts_input(struct mbuf *m);
102 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
103 static int	rt_msg2(int type, struct rt_addrinfo *rtinfo,
104 			caddr_t cp, struct walkarg *w);
105 static int	rt_xaddrs(caddr_t cp, caddr_t cplim,
106 			struct rt_addrinfo *rtinfo);
107 static int	sysctl_dumpentry(struct radix_node *rn, void *vw);
108 static int	sysctl_iflist(int af, struct walkarg *w);
109 static int	sysctl_ifmalist(int af, struct walkarg *w);
110 static int	route_output(struct mbuf *m, struct socket *so);
111 static void	rt_setmetrics(u_long which, const struct rt_metrics *in,
112 			struct rt_metrics_lite *out);
113 static void	rt_getmetrics(const struct rt_metrics_lite *in,
114 			struct rt_metrics *out);
115 static void	rt_dispatch(struct mbuf *, const struct sockaddr *);
116 
117 static void
118 rts_init(void)
119 {
120 	int tmp;
121 
122 	rtsintrq.ifq_maxlen = 256;
123 	if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
124 		rtsintrq.ifq_maxlen = tmp;
125 	mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF);
126 	netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, 0);
127 }
128 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0);
129 
130 static void
131 rts_input(struct mbuf *m)
132 {
133 	struct sockproto route_proto;
134 	unsigned short *family;
135 	struct m_tag *tag;
136 
137 	route_proto.sp_family = PF_ROUTE;
138 	tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
139 	if (tag != NULL) {
140 		family = (unsigned short *)(tag + 1);
141 		route_proto.sp_protocol = *family;
142 		m_tag_delete(m, tag);
143 	} else
144 		route_proto.sp_protocol = 0;
145 
146 	raw_input(m, &route_proto, &route_src);
147 }
148 
149 /*
150  * It really doesn't make any sense at all for this code to share much
151  * with raw_usrreq.c, since its functionality is so restricted.  XXX
152  */
153 static void
154 rts_abort(struct socket *so)
155 {
156 
157 	raw_usrreqs.pru_abort(so);
158 }
159 
160 static void
161 rts_close(struct socket *so)
162 {
163 
164 	raw_usrreqs.pru_close(so);
165 }
166 
167 /* pru_accept is EOPNOTSUPP */
168 
169 static int
170 rts_attach(struct socket *so, int proto, struct thread *td)
171 {
172 	struct rawcb *rp;
173 	int s, error;
174 
175 	KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
176 
177 	/* XXX */
178 	rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
179 	if (rp == NULL)
180 		return ENOBUFS;
181 
182 	/*
183 	 * The splnet() is necessary to block protocols from sending
184 	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
185 	 * this PCB is extant but incompletely initialized.
186 	 * Probably we should try to do more of this work beforehand and
187 	 * eliminate the spl.
188 	 */
189 	s = splnet();
190 	so->so_pcb = (caddr_t)rp;
191 	so->so_fibnum = td->td_proc->p_fibnum;
192 	error = raw_attach(so, proto);
193 	rp = sotorawcb(so);
194 	if (error) {
195 		splx(s);
196 		so->so_pcb = NULL;
197 		free(rp, M_PCB);
198 		return error;
199 	}
200 	RTSOCK_LOCK();
201 	switch(rp->rcb_proto.sp_protocol) {
202 	case AF_INET:
203 		route_cb.ip_count++;
204 		break;
205 	case AF_INET6:
206 		route_cb.ip6_count++;
207 		break;
208 	case AF_IPX:
209 		route_cb.ipx_count++;
210 		break;
211 	}
212 	route_cb.any_count++;
213 	RTSOCK_UNLOCK();
214 	soisconnected(so);
215 	so->so_options |= SO_USELOOPBACK;
216 	splx(s);
217 	return 0;
218 }
219 
220 static int
221 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
222 {
223 
224 	return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
225 }
226 
227 static int
228 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
229 {
230 
231 	return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
232 }
233 
234 /* pru_connect2 is EOPNOTSUPP */
235 /* pru_control is EOPNOTSUPP */
236 
237 static void
238 rts_detach(struct socket *so)
239 {
240 	struct rawcb *rp = sotorawcb(so);
241 
242 	KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
243 
244 	RTSOCK_LOCK();
245 	switch(rp->rcb_proto.sp_protocol) {
246 	case AF_INET:
247 		route_cb.ip_count--;
248 		break;
249 	case AF_INET6:
250 		route_cb.ip6_count--;
251 		break;
252 	case AF_IPX:
253 		route_cb.ipx_count--;
254 		break;
255 	}
256 	route_cb.any_count--;
257 	RTSOCK_UNLOCK();
258 	raw_usrreqs.pru_detach(so);
259 }
260 
261 static int
262 rts_disconnect(struct socket *so)
263 {
264 
265 	return (raw_usrreqs.pru_disconnect(so));
266 }
267 
268 /* pru_listen is EOPNOTSUPP */
269 
270 static int
271 rts_peeraddr(struct socket *so, struct sockaddr **nam)
272 {
273 
274 	return (raw_usrreqs.pru_peeraddr(so, nam));
275 }
276 
277 /* pru_rcvd is EOPNOTSUPP */
278 /* pru_rcvoob is EOPNOTSUPP */
279 
280 static int
281 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
282 	 struct mbuf *control, struct thread *td)
283 {
284 
285 	return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
286 }
287 
288 /* pru_sense is null */
289 
290 static int
291 rts_shutdown(struct socket *so)
292 {
293 
294 	return (raw_usrreqs.pru_shutdown(so));
295 }
296 
297 static int
298 rts_sockaddr(struct socket *so, struct sockaddr **nam)
299 {
300 
301 	return (raw_usrreqs.pru_sockaddr(so, nam));
302 }
303 
304 static struct pr_usrreqs route_usrreqs = {
305 	.pru_abort =		rts_abort,
306 	.pru_attach =		rts_attach,
307 	.pru_bind =		rts_bind,
308 	.pru_connect =		rts_connect,
309 	.pru_detach =		rts_detach,
310 	.pru_disconnect =	rts_disconnect,
311 	.pru_peeraddr =		rts_peeraddr,
312 	.pru_send =		rts_send,
313 	.pru_shutdown =		rts_shutdown,
314 	.pru_sockaddr =		rts_sockaddr,
315 	.pru_close =		rts_close,
316 };
317 
318 #ifndef _SOCKADDR_UNION_DEFINED
319 #define	_SOCKADDR_UNION_DEFINED
320 /*
321  * The union of all possible address formats we handle.
322  */
323 union sockaddr_union {
324 	struct sockaddr		sa;
325 	struct sockaddr_in	sin;
326 	struct sockaddr_in6	sin6;
327 };
328 #endif /* _SOCKADDR_UNION_DEFINED */
329 
330 static int
331 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
332     struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
333 {
334 
335 	switch (info->rti_info[RTAX_DST]->sa_family) {
336 #ifdef INET
337 	case AF_INET:
338 	{
339 		struct in_addr ia;
340 
341 		/*
342 		 * 1. Check if the returned address is part of the jail.
343 		 */
344 		ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->sin_addr;
345 		if (prison_check_ip4(cred, &ia) != 0) {
346 			info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
347 
348 		} else {
349 			struct ifaddr *ifa;
350 			int found;
351 
352 			found = 0;
353 
354 			/*
355 			 * 2. Try to find an address on the given outgoing
356 			 *    interface that belongs to the jail.
357 			 */
358 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
359 				struct sockaddr *sa;
360 				sa = ifa->ifa_addr;
361 				if (sa->sa_family != AF_INET)
362 					continue;
363 				ia = ((struct sockaddr_in *)sa)->sin_addr;
364 				if (prison_check_ip4(cred, &ia) != 0) {
365 					found = 1;
366 					break;
367 				}
368 			}
369 			if (!found) {
370 				/*
371 				 * 3. As a last resort return the 'default'
372 				 * jail address.
373 				 */
374 				if (prison_getip4(cred, &ia) != 0)
375 					return (ESRCH);
376 			}
377 			bzero(&saun->sin, sizeof(struct sockaddr_in));
378 			saun->sin.sin_len = sizeof(struct sockaddr_in);
379 			saun->sin.sin_family = AF_INET;
380 			saun->sin.sin_addr.s_addr = ia.s_addr;
381 			info->rti_info[RTAX_IFA] =
382 			    (struct sockaddr *)&saun->sin;
383 		}
384 		break;
385 	}
386 #endif
387 #ifdef INET6
388 	case AF_INET6:
389 	{
390 		struct in6_addr ia6;
391 
392 		/*
393 		 * 1. Check if the returned address is part of the jail.
394 		 */
395 		bcopy(&((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->sin6_addr,
396 		    &ia6, sizeof(struct in6_addr));
397 		if (prison_check_ip6(cred, &ia6) != 0) {
398 			info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
399 		} else {
400 			struct ifaddr *ifa;
401 			int found;
402 
403 			found = 0;
404 
405 			/*
406 			 * 2. Try to find an address on the given outgoing
407 			 *    interface that belongs to the jail.
408 			 */
409 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
410 				struct sockaddr *sa;
411 				sa = ifa->ifa_addr;
412 				if (sa->sa_family != AF_INET6)
413 					continue;
414 				bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
415 				    &ia6, sizeof(struct in6_addr));
416 				if (prison_check_ip6(cred, &ia6) != 0) {
417 					found = 1;
418 					break;
419 				}
420 			}
421 			if (!found) {
422 				/*
423 				 * 3. As a last resort return the 'default'
424 				 * jail address.
425 				 */
426 				if (prison_getip6(cred, &ia6) != 0)
427 					return (ESRCH);
428 			}
429 			bzero(&saun->sin6, sizeof(struct sockaddr_in6));
430 			saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
431 			saun->sin6.sin6_family = AF_INET6;
432 			bcopy(&ia6, &saun->sin6.sin6_addr,
433 			    sizeof(struct in6_addr));
434 			if (sa6_recoverscope(&saun->sin6) != 0)
435 				return (ESRCH);
436 			info->rti_info[RTAX_IFA] =
437 			    (struct sockaddr *)&saun->sin6;
438 		}
439 		break;
440 	}
441 #endif
442 	default:
443 		return (ESRCH);
444 	}
445 	return (0);
446 }
447 
448 /*ARGSUSED*/
449 static int
450 route_output(struct mbuf *m, struct socket *so)
451 {
452 #define	sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
453 	INIT_VNET_NET(so->so_vnet);
454 	struct rt_msghdr *rtm = NULL;
455 	struct rtentry *rt = NULL;
456 	struct radix_node_head *rnh;
457 	struct rt_addrinfo info;
458 	int len, error = 0;
459 	struct ifnet *ifp = NULL;
460 	union sockaddr_union saun;
461 
462 #define senderr(e) { error = e; goto flush;}
463 	if (m == NULL || ((m->m_len < sizeof(long)) &&
464 		       (m = m_pullup(m, sizeof(long))) == NULL))
465 		return (ENOBUFS);
466 	if ((m->m_flags & M_PKTHDR) == 0)
467 		panic("route_output");
468 	len = m->m_pkthdr.len;
469 	if (len < sizeof(*rtm) ||
470 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
471 		info.rti_info[RTAX_DST] = NULL;
472 		senderr(EINVAL);
473 	}
474 	R_Malloc(rtm, struct rt_msghdr *, len);
475 	if (rtm == NULL) {
476 		info.rti_info[RTAX_DST] = NULL;
477 		senderr(ENOBUFS);
478 	}
479 	m_copydata(m, 0, len, (caddr_t)rtm);
480 	if (rtm->rtm_version != RTM_VERSION) {
481 		info.rti_info[RTAX_DST] = NULL;
482 		senderr(EPROTONOSUPPORT);
483 	}
484 	rtm->rtm_pid = curproc->p_pid;
485 	bzero(&info, sizeof(info));
486 	info.rti_addrs = rtm->rtm_addrs;
487 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
488 		info.rti_info[RTAX_DST] = NULL;
489 		senderr(EINVAL);
490 	}
491 	info.rti_flags = rtm->rtm_flags;
492 	if (info.rti_info[RTAX_DST] == NULL ||
493 	    info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
494 	    (info.rti_info[RTAX_GATEWAY] != NULL &&
495 	     info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
496 		senderr(EINVAL);
497 	if (info.rti_info[RTAX_GENMASK]) {
498 		struct radix_node *t;
499 		t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
500 		if (t != NULL &&
501 		    bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1,
502 		    (char *)(void *)t->rn_key + 1,
503 		    ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0)
504 			info.rti_info[RTAX_GENMASK] =
505 			    (struct sockaddr *)t->rn_key;
506 		else
507 			senderr(ENOBUFS);
508 	}
509 
510 	/*
511 	 * Verify that the caller has the appropriate privilege; RTM_GET
512 	 * is the only operation the non-superuser is allowed.
513 	 */
514 	if (rtm->rtm_type != RTM_GET) {
515 		error = priv_check(curthread, PRIV_NET_ROUTE);
516 		if (error)
517 			senderr(error);
518 	}
519 
520 	switch (rtm->rtm_type) {
521 		struct rtentry *saved_nrt;
522 
523 	case RTM_ADD:
524 		if (info.rti_info[RTAX_GATEWAY] == NULL)
525 			senderr(EINVAL);
526 		saved_nrt = NULL;
527 		error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt,
528 		    so->so_fibnum);
529 		if (error == 0 && saved_nrt) {
530 			RT_LOCK(saved_nrt);
531 			rt_setmetrics(rtm->rtm_inits,
532 				&rtm->rtm_rmx, &saved_nrt->rt_rmx);
533 			rtm->rtm_index = saved_nrt->rt_ifp->if_index;
534 			RT_REMREF(saved_nrt);
535 			saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
536 			RT_UNLOCK(saved_nrt);
537 		}
538 		break;
539 
540 	case RTM_DELETE:
541 		saved_nrt = NULL;
542 		error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt,
543 		    so->so_fibnum);
544 		if (error == 0) {
545 			RT_LOCK(saved_nrt);
546 			rt = saved_nrt;
547 			goto report;
548 		}
549 		break;
550 
551 	case RTM_GET:
552 	case RTM_CHANGE:
553 	case RTM_LOCK:
554 		rnh = V_rt_tables[so->so_fibnum][info.rti_info[RTAX_DST]->sa_family];
555 		if (rnh == NULL)
556 			senderr(EAFNOSUPPORT);
557 		RADIX_NODE_HEAD_LOCK(rnh);
558 		rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
559 			info.rti_info[RTAX_NETMASK], rnh);
560 		if (rt == NULL) {	/* XXX looks bogus */
561 			RADIX_NODE_HEAD_UNLOCK(rnh);
562 			senderr(ESRCH);
563 		}
564 #ifdef RADIX_MPATH
565 		/*
566 		 * for RTM_CHANGE/LOCK, if we got multipath routes,
567 		 * we require users to specify a matching RTAX_GATEWAY.
568 		 *
569 		 * for RTM_GET, gate is optional even with multipath.
570 		 * if gate == NULL the first match is returned.
571 		 * (no need to call rt_mpath_matchgate if gate == NULL)
572 		 */
573 		if (rn_mpath_capable(rnh) &&
574 		    (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
575 			rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
576 			if (!rt) {
577 				RADIX_NODE_HEAD_UNLOCK(rnh);
578 				senderr(ESRCH);
579 			}
580 		}
581 #endif
582 		RT_LOCK(rt);
583 		RT_ADDREF(rt);
584 		RADIX_NODE_HEAD_UNLOCK(rnh);
585 
586 		/*
587 		 * Fix for PR: 82974
588 		 *
589 		 * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
590 		 * returns a perfect match in case a netmask is
591 		 * specified.  For host routes only a longest prefix
592 		 * match is returned so it is necessary to compare the
593 		 * existence of the netmask.  If both have a netmask
594 		 * rnh_lookup() did a perfect match and if none of them
595 		 * have a netmask both are host routes which is also a
596 		 * perfect match.
597 		 */
598 
599 		if (rtm->rtm_type != RTM_GET &&
600 		    (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
601 			RT_UNLOCK(rt);
602 			senderr(ESRCH);
603 		}
604 
605 		switch(rtm->rtm_type) {
606 
607 		case RTM_GET:
608 		report:
609 			RT_LOCK_ASSERT(rt);
610 			info.rti_info[RTAX_DST] = rt_key(rt);
611 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
612 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
613 			info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
614 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
615 				ifp = rt->rt_ifp;
616 				if (ifp) {
617 					info.rti_info[RTAX_IFP] =
618 					    ifp->if_addr->ifa_addr;
619 					if (jailed(so->so_cred)) {
620 						error = rtm_get_jailed(
621 						    &info, ifp, rt, &saun,
622 						    so->so_cred);
623 						if (error != 0) {
624 							RT_UNLOCK(rt);
625 							senderr(ESRCH);
626 						}
627 					} else {
628 						info.rti_info[RTAX_IFA] =
629 						    rt->rt_ifa->ifa_addr;
630 					}
631 					if (ifp->if_flags & IFF_POINTOPOINT)
632 						info.rti_info[RTAX_BRD] =
633 						    rt->rt_ifa->ifa_dstaddr;
634 					rtm->rtm_index = ifp->if_index;
635 				} else {
636 					info.rti_info[RTAX_IFP] = NULL;
637 					info.rti_info[RTAX_IFA] = NULL;
638 				}
639 			} else if ((ifp = rt->rt_ifp) != NULL) {
640 				rtm->rtm_index = ifp->if_index;
641 			}
642 			len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
643 			if (len > rtm->rtm_msglen) {
644 				struct rt_msghdr *new_rtm;
645 				R_Malloc(new_rtm, struct rt_msghdr *, len);
646 				if (new_rtm == NULL) {
647 					RT_UNLOCK(rt);
648 					senderr(ENOBUFS);
649 				}
650 				bcopy(rtm, new_rtm, rtm->rtm_msglen);
651 				Free(rtm); rtm = new_rtm;
652 			}
653 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
654 			rtm->rtm_flags = rt->rt_flags;
655 			rtm->rtm_use = 0;
656 			rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
657 			rtm->rtm_addrs = info.rti_addrs;
658 			break;
659 
660 		case RTM_CHANGE:
661 			/*
662 			 * New gateway could require new ifaddr, ifp;
663 			 * flags may also be different; ifp may be specified
664 			 * by ll sockaddr when protocol address is ambiguous
665 			 */
666 			if (((rt->rt_flags & RTF_GATEWAY) &&
667 			     info.rti_info[RTAX_GATEWAY] != NULL) ||
668 			    info.rti_info[RTAX_IFP] != NULL ||
669 			    (info.rti_info[RTAX_IFA] != NULL &&
670 			     !sa_equal(info.rti_info[RTAX_IFA],
671 				       rt->rt_ifa->ifa_addr))) {
672 				RT_UNLOCK(rt);
673 				if ((error = rt_getifa_fib(&info,
674 				    rt->rt_fibnum)) != 0)
675 					senderr(error);
676 				RT_LOCK(rt);
677 			}
678 			if (info.rti_ifa != NULL &&
679 			    info.rti_ifa != rt->rt_ifa &&
680 			    rt->rt_ifa != NULL &&
681 			    rt->rt_ifa->ifa_rtrequest != NULL) {
682 				rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
683 				    &info);
684 				IFAFREE(rt->rt_ifa);
685 			}
686 			if (info.rti_info[RTAX_GATEWAY] != NULL) {
687 				if ((error = rt_setgate(rt, rt_key(rt),
688 					info.rti_info[RTAX_GATEWAY])) != 0) {
689 					RT_UNLOCK(rt);
690 					senderr(error);
691 				}
692 				if (!(rt->rt_flags & RTF_LLINFO))
693 					rt->rt_flags |= RTF_GATEWAY;
694 			}
695 			if (info.rti_ifa != NULL &&
696 			    info.rti_ifa != rt->rt_ifa) {
697 				IFAREF(info.rti_ifa);
698 				rt->rt_ifa = info.rti_ifa;
699 				rt->rt_ifp = info.rti_ifp;
700 			}
701 			/* Allow some flags to be toggled on change. */
702 			if (rtm->rtm_fmask & RTF_FMASK)
703 				rt->rt_flags = (rt->rt_flags &
704 				    ~rtm->rtm_fmask) |
705 				    (rtm->rtm_flags & rtm->rtm_fmask);
706 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
707 					&rt->rt_rmx);
708 			rtm->rtm_index = rt->rt_ifp->if_index;
709 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
710 			       rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
711 			if (info.rti_info[RTAX_GENMASK])
712 				rt->rt_genmask = info.rti_info[RTAX_GENMASK];
713 			/* FALLTHROUGH */
714 		case RTM_LOCK:
715 			/* We don't support locks anymore */
716 			break;
717 		}
718 		RT_UNLOCK(rt);
719 		break;
720 
721 	default:
722 		senderr(EOPNOTSUPP);
723 	}
724 
725 flush:
726 	if (rtm) {
727 		if (error)
728 			rtm->rtm_errno = error;
729 		else
730 			rtm->rtm_flags |= RTF_DONE;
731 	}
732 	if (rt)		/* XXX can this be true? */
733 		RTFREE(rt);
734     {
735 	struct rawcb *rp = NULL;
736 	/*
737 	 * Check to see if we don't want our own messages.
738 	 */
739 	if ((so->so_options & SO_USELOOPBACK) == 0) {
740 		if (route_cb.any_count <= 1) {
741 			if (rtm)
742 				Free(rtm);
743 			m_freem(m);
744 			return (error);
745 		}
746 		/* There is another listener, so construct message */
747 		rp = sotorawcb(so);
748 	}
749 	if (rtm) {
750 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
751 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
752 			m_freem(m);
753 			m = NULL;
754 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
755 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
756 		Free(rtm);
757 	}
758 	if (m) {
759 		if (rp) {
760 			/*
761 			 * XXX insure we don't get a copy by
762 			 * invalidating our protocol
763 			 */
764 			unsigned short family = rp->rcb_proto.sp_family;
765 			rp->rcb_proto.sp_family = 0;
766 			rt_dispatch(m, info.rti_info[RTAX_DST]);
767 			rp->rcb_proto.sp_family = family;
768 		} else
769 			rt_dispatch(m, info.rti_info[RTAX_DST]);
770 	}
771     }
772 	return (error);
773 #undef	sa_equal
774 }
775 
776 static void
777 rt_setmetrics(u_long which, const struct rt_metrics *in,
778 	struct rt_metrics_lite *out)
779 {
780 #define metric(f, e) if (which & (f)) out->e = in->e;
781 	/*
782 	 * Only these are stored in the routing entry since introduction
783 	 * of tcp hostcache. The rest is ignored.
784 	 */
785 	metric(RTV_MTU, rmx_mtu);
786 	/* Userland -> kernel timebase conversion. */
787 	if (which & RTV_EXPIRE)
788 		out->rmx_expire = in->rmx_expire ?
789 		    in->rmx_expire - time_second + time_uptime : 0;
790 #undef metric
791 }
792 
793 static void
794 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
795 {
796 #define metric(e) out->e = in->e;
797 	bzero(out, sizeof(*out));
798 	metric(rmx_mtu);
799 	/* Kernel -> userland timebase conversion. */
800 	out->rmx_expire = in->rmx_expire ?
801 	    in->rmx_expire - time_uptime + time_second : 0;
802 #undef metric
803 }
804 
805 /*
806  * Extract the addresses of the passed sockaddrs.
807  * Do a little sanity checking so as to avoid bad memory references.
808  * This data is derived straight from userland.
809  */
810 static int
811 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
812 {
813 	struct sockaddr *sa;
814 	int i;
815 
816 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
817 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
818 			continue;
819 		sa = (struct sockaddr *)cp;
820 		/*
821 		 * It won't fit.
822 		 */
823 		if (cp + sa->sa_len > cplim)
824 			return (EINVAL);
825 		/*
826 		 * there are no more.. quit now
827 		 * If there are more bits, they are in error.
828 		 * I've seen this. route(1) can evidently generate these.
829 		 * This causes kernel to core dump.
830 		 * for compatibility, If we see this, point to a safe address.
831 		 */
832 		if (sa->sa_len == 0) {
833 			rtinfo->rti_info[i] = &sa_zero;
834 			return (0); /* should be EINVAL but for compat */
835 		}
836 		/* accept it */
837 		rtinfo->rti_info[i] = sa;
838 		cp += SA_SIZE(sa);
839 	}
840 	return (0);
841 }
842 
843 static struct mbuf *
844 rt_msg1(int type, struct rt_addrinfo *rtinfo)
845 {
846 	struct rt_msghdr *rtm;
847 	struct mbuf *m;
848 	int i;
849 	struct sockaddr *sa;
850 	int len, dlen;
851 
852 	switch (type) {
853 
854 	case RTM_DELADDR:
855 	case RTM_NEWADDR:
856 		len = sizeof(struct ifa_msghdr);
857 		break;
858 
859 	case RTM_DELMADDR:
860 	case RTM_NEWMADDR:
861 		len = sizeof(struct ifma_msghdr);
862 		break;
863 
864 	case RTM_IFINFO:
865 		len = sizeof(struct if_msghdr);
866 		break;
867 
868 	case RTM_IFANNOUNCE:
869 	case RTM_IEEE80211:
870 		len = sizeof(struct if_announcemsghdr);
871 		break;
872 
873 	default:
874 		len = sizeof(struct rt_msghdr);
875 	}
876 	if (len > MCLBYTES)
877 		panic("rt_msg1");
878 	m = m_gethdr(M_DONTWAIT, MT_DATA);
879 	if (m && len > MHLEN) {
880 		MCLGET(m, M_DONTWAIT);
881 		if ((m->m_flags & M_EXT) == 0) {
882 			m_free(m);
883 			m = NULL;
884 		}
885 	}
886 	if (m == NULL)
887 		return (m);
888 	m->m_pkthdr.len = m->m_len = len;
889 	m->m_pkthdr.rcvif = NULL;
890 	rtm = mtod(m, struct rt_msghdr *);
891 	bzero((caddr_t)rtm, len);
892 	for (i = 0; i < RTAX_MAX; i++) {
893 		if ((sa = rtinfo->rti_info[i]) == NULL)
894 			continue;
895 		rtinfo->rti_addrs |= (1 << i);
896 		dlen = SA_SIZE(sa);
897 		m_copyback(m, len, dlen, (caddr_t)sa);
898 		len += dlen;
899 	}
900 	if (m->m_pkthdr.len != len) {
901 		m_freem(m);
902 		return (NULL);
903 	}
904 	rtm->rtm_msglen = len;
905 	rtm->rtm_version = RTM_VERSION;
906 	rtm->rtm_type = type;
907 	return (m);
908 }
909 
910 static int
911 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
912 {
913 	int i;
914 	int len, dlen, second_time = 0;
915 	caddr_t cp0;
916 
917 	rtinfo->rti_addrs = 0;
918 again:
919 	switch (type) {
920 
921 	case RTM_DELADDR:
922 	case RTM_NEWADDR:
923 		len = sizeof(struct ifa_msghdr);
924 		break;
925 
926 	case RTM_IFINFO:
927 		len = sizeof(struct if_msghdr);
928 		break;
929 
930 	case RTM_NEWMADDR:
931 		len = sizeof(struct ifma_msghdr);
932 		break;
933 
934 	default:
935 		len = sizeof(struct rt_msghdr);
936 	}
937 	cp0 = cp;
938 	if (cp0)
939 		cp += len;
940 	for (i = 0; i < RTAX_MAX; i++) {
941 		struct sockaddr *sa;
942 
943 		if ((sa = rtinfo->rti_info[i]) == NULL)
944 			continue;
945 		rtinfo->rti_addrs |= (1 << i);
946 		dlen = SA_SIZE(sa);
947 		if (cp) {
948 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
949 			cp += dlen;
950 		}
951 		len += dlen;
952 	}
953 	len = ALIGN(len);
954 	if (cp == NULL && w != NULL && !second_time) {
955 		struct walkarg *rw = w;
956 
957 		if (rw->w_req) {
958 			if (rw->w_tmemsize < len) {
959 				if (rw->w_tmem)
960 					free(rw->w_tmem, M_RTABLE);
961 				rw->w_tmem = (caddr_t)
962 					malloc(len, M_RTABLE, M_NOWAIT);
963 				if (rw->w_tmem)
964 					rw->w_tmemsize = len;
965 			}
966 			if (rw->w_tmem) {
967 				cp = rw->w_tmem;
968 				second_time = 1;
969 				goto again;
970 			}
971 		}
972 	}
973 	if (cp) {
974 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
975 
976 		rtm->rtm_version = RTM_VERSION;
977 		rtm->rtm_type = type;
978 		rtm->rtm_msglen = len;
979 	}
980 	return (len);
981 }
982 
983 /*
984  * This routine is called to generate a message from the routing
985  * socket indicating that a redirect has occured, a routing lookup
986  * has failed, or that a protocol has detected timeouts to a particular
987  * destination.
988  */
989 void
990 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
991 {
992 	struct rt_msghdr *rtm;
993 	struct mbuf *m;
994 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
995 
996 	if (route_cb.any_count == 0)
997 		return;
998 	m = rt_msg1(type, rtinfo);
999 	if (m == NULL)
1000 		return;
1001 	rtm = mtod(m, struct rt_msghdr *);
1002 	rtm->rtm_flags = RTF_DONE | flags;
1003 	rtm->rtm_errno = error;
1004 	rtm->rtm_addrs = rtinfo->rti_addrs;
1005 	rt_dispatch(m, sa);
1006 }
1007 
1008 /*
1009  * This routine is called to generate a message from the routing
1010  * socket indicating that the status of a network interface has changed.
1011  */
1012 void
1013 rt_ifmsg(struct ifnet *ifp)
1014 {
1015 	struct if_msghdr *ifm;
1016 	struct mbuf *m;
1017 	struct rt_addrinfo info;
1018 
1019 	if (route_cb.any_count == 0)
1020 		return;
1021 	bzero((caddr_t)&info, sizeof(info));
1022 	m = rt_msg1(RTM_IFINFO, &info);
1023 	if (m == NULL)
1024 		return;
1025 	ifm = mtod(m, struct if_msghdr *);
1026 	ifm->ifm_index = ifp->if_index;
1027 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1028 	ifm->ifm_data = ifp->if_data;
1029 	ifm->ifm_addrs = 0;
1030 	rt_dispatch(m, NULL);
1031 }
1032 
1033 /*
1034  * This is called to generate messages from the routing socket
1035  * indicating a network interface has had addresses associated with it.
1036  * if we ever reverse the logic and replace messages TO the routing
1037  * socket indicate a request to configure interfaces, then it will
1038  * be unnecessary as the routing socket will automatically generate
1039  * copies of it.
1040  */
1041 void
1042 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1043 {
1044 	struct rt_addrinfo info;
1045 	struct sockaddr *sa = NULL;
1046 	int pass;
1047 	struct mbuf *m = NULL;
1048 	struct ifnet *ifp = ifa->ifa_ifp;
1049 
1050 	KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
1051 		("unexpected cmd %u", cmd));
1052 #ifdef SCTP
1053 	/*
1054 	 * notify the SCTP stack
1055 	 * this will only get called when an address is added/deleted
1056 	 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1057 	 */
1058 	sctp_addr_change(ifa, cmd);
1059 #endif /* SCTP */
1060 	if (route_cb.any_count == 0)
1061 		return;
1062 	for (pass = 1; pass < 3; pass++) {
1063 		bzero((caddr_t)&info, sizeof(info));
1064 		if ((cmd == RTM_ADD && pass == 1) ||
1065 		    (cmd == RTM_DELETE && pass == 2)) {
1066 			struct ifa_msghdr *ifam;
1067 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1068 
1069 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1070 			info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1071 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1072 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1073 			if ((m = rt_msg1(ncmd, &info)) == NULL)
1074 				continue;
1075 			ifam = mtod(m, struct ifa_msghdr *);
1076 			ifam->ifam_index = ifp->if_index;
1077 			ifam->ifam_metric = ifa->ifa_metric;
1078 			ifam->ifam_flags = ifa->ifa_flags;
1079 			ifam->ifam_addrs = info.rti_addrs;
1080 		}
1081 		if ((cmd == RTM_ADD && pass == 2) ||
1082 		    (cmd == RTM_DELETE && pass == 1)) {
1083 			struct rt_msghdr *rtm;
1084 
1085 			if (rt == NULL)
1086 				continue;
1087 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1088 			info.rti_info[RTAX_DST] = sa = rt_key(rt);
1089 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1090 			if ((m = rt_msg1(cmd, &info)) == NULL)
1091 				continue;
1092 			rtm = mtod(m, struct rt_msghdr *);
1093 			rtm->rtm_index = ifp->if_index;
1094 			rtm->rtm_flags |= rt->rt_flags;
1095 			rtm->rtm_errno = error;
1096 			rtm->rtm_addrs = info.rti_addrs;
1097 		}
1098 		rt_dispatch(m, sa);
1099 	}
1100 }
1101 
1102 /*
1103  * This is the analogue to the rt_newaddrmsg which performs the same
1104  * function but for multicast group memberhips.  This is easier since
1105  * there is no route state to worry about.
1106  */
1107 void
1108 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1109 {
1110 	struct rt_addrinfo info;
1111 	struct mbuf *m = NULL;
1112 	struct ifnet *ifp = ifma->ifma_ifp;
1113 	struct ifma_msghdr *ifmam;
1114 
1115 	if (route_cb.any_count == 0)
1116 		return;
1117 
1118 	bzero((caddr_t)&info, sizeof(info));
1119 	info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1120 	info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1121 	/*
1122 	 * If a link-layer address is present, present it as a ``gateway''
1123 	 * (similarly to how ARP entries, e.g., are presented).
1124 	 */
1125 	info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1126 	m = rt_msg1(cmd, &info);
1127 	if (m == NULL)
1128 		return;
1129 	ifmam = mtod(m, struct ifma_msghdr *);
1130 	KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1131 	    __func__));
1132 	ifmam->ifmam_index = ifp->if_index;
1133 	ifmam->ifmam_addrs = info.rti_addrs;
1134 	rt_dispatch(m, ifma->ifma_addr);
1135 }
1136 
1137 static struct mbuf *
1138 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1139 	struct rt_addrinfo *info)
1140 {
1141 	struct if_announcemsghdr *ifan;
1142 	struct mbuf *m;
1143 
1144 	if (route_cb.any_count == 0)
1145 		return NULL;
1146 	bzero((caddr_t)info, sizeof(*info));
1147 	m = rt_msg1(type, info);
1148 	if (m != NULL) {
1149 		ifan = mtod(m, struct if_announcemsghdr *);
1150 		ifan->ifan_index = ifp->if_index;
1151 		strlcpy(ifan->ifan_name, ifp->if_xname,
1152 			sizeof(ifan->ifan_name));
1153 		ifan->ifan_what = what;
1154 	}
1155 	return m;
1156 }
1157 
1158 /*
1159  * This is called to generate routing socket messages indicating
1160  * IEEE80211 wireless events.
1161  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1162  */
1163 void
1164 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1165 {
1166 	struct mbuf *m;
1167 	struct rt_addrinfo info;
1168 
1169 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1170 	if (m != NULL) {
1171 		/*
1172 		 * Append the ieee80211 data.  Try to stick it in the
1173 		 * mbuf containing the ifannounce msg; otherwise allocate
1174 		 * a new mbuf and append.
1175 		 *
1176 		 * NB: we assume m is a single mbuf.
1177 		 */
1178 		if (data_len > M_TRAILINGSPACE(m)) {
1179 			struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1180 			if (n == NULL) {
1181 				m_freem(m);
1182 				return;
1183 			}
1184 			bcopy(data, mtod(n, void *), data_len);
1185 			n->m_len = data_len;
1186 			m->m_next = n;
1187 		} else if (data_len > 0) {
1188 			bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1189 			m->m_len += data_len;
1190 		}
1191 		if (m->m_flags & M_PKTHDR)
1192 			m->m_pkthdr.len += data_len;
1193 		mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1194 		rt_dispatch(m, NULL);
1195 	}
1196 }
1197 
1198 /*
1199  * This is called to generate routing socket messages indicating
1200  * network interface arrival and departure.
1201  */
1202 void
1203 rt_ifannouncemsg(struct ifnet *ifp, int what)
1204 {
1205 	struct mbuf *m;
1206 	struct rt_addrinfo info;
1207 
1208 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1209 	if (m != NULL)
1210 		rt_dispatch(m, NULL);
1211 }
1212 
1213 static void
1214 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
1215 {
1216 	INIT_VNET_NET(curvnet);
1217 	struct m_tag *tag;
1218 
1219 	/*
1220 	 * Preserve the family from the sockaddr, if any, in an m_tag for
1221 	 * use when injecting the mbuf into the routing socket buffer from
1222 	 * the netisr.
1223 	 */
1224 	if (sa != NULL) {
1225 		tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1226 		    M_NOWAIT);
1227 		if (tag == NULL) {
1228 			m_freem(m);
1229 			return;
1230 		}
1231 		*(unsigned short *)(tag + 1) = sa->sa_family;
1232 		m_tag_prepend(m, tag);
1233 	}
1234 	netisr_queue(NETISR_ROUTE, m);	/* mbuf is free'd on failure. */
1235 }
1236 
1237 /*
1238  * This is used in dumping the kernel table via sysctl().
1239  */
1240 static int
1241 sysctl_dumpentry(struct radix_node *rn, void *vw)
1242 {
1243 	struct walkarg *w = vw;
1244 	struct rtentry *rt = (struct rtentry *)rn;
1245 	int error = 0, size;
1246 	struct rt_addrinfo info;
1247 
1248 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1249 		return 0;
1250 	bzero((caddr_t)&info, sizeof(info));
1251 	info.rti_info[RTAX_DST] = rt_key(rt);
1252 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1253 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1254 	info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
1255 	if (rt->rt_ifp) {
1256 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1257 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1258 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1259 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1260 	}
1261 	size = rt_msg2(RTM_GET, &info, NULL, w);
1262 	if (w->w_req && w->w_tmem) {
1263 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1264 
1265 		rtm->rtm_flags = rt->rt_flags;
1266 		rtm->rtm_use = rt->rt_rmx.rmx_pksent;
1267 		rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1268 		rtm->rtm_index = rt->rt_ifp->if_index;
1269 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1270 		rtm->rtm_addrs = info.rti_addrs;
1271 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1272 		return (error);
1273 	}
1274 	return (error);
1275 }
1276 
1277 static int
1278 sysctl_iflist(int af, struct walkarg *w)
1279 {
1280 	INIT_VNET_NET(curvnet);
1281 	struct ifnet *ifp;
1282 	struct ifaddr *ifa;
1283 	struct rt_addrinfo info;
1284 	int len, error = 0;
1285 
1286 	bzero((caddr_t)&info, sizeof(info));
1287 	IFNET_RLOCK();
1288 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1289 		if (w->w_arg && w->w_arg != ifp->if_index)
1290 			continue;
1291 		ifa = ifp->if_addr;
1292 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1293 		len = rt_msg2(RTM_IFINFO, &info, NULL, w);
1294 		info.rti_info[RTAX_IFP] = NULL;
1295 		if (w->w_req && w->w_tmem) {
1296 			struct if_msghdr *ifm;
1297 
1298 			ifm = (struct if_msghdr *)w->w_tmem;
1299 			ifm->ifm_index = ifp->if_index;
1300 			ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1301 			ifm->ifm_data = ifp->if_data;
1302 			ifm->ifm_addrs = info.rti_addrs;
1303 			error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
1304 			if (error)
1305 				goto done;
1306 		}
1307 		while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1308 			if (af && af != ifa->ifa_addr->sa_family)
1309 				continue;
1310 			if (jailed(curthread->td_ucred) &&
1311 			    !prison_if(curthread->td_ucred, ifa->ifa_addr))
1312 				continue;
1313 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1314 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1315 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1316 			len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
1317 			if (w->w_req && w->w_tmem) {
1318 				struct ifa_msghdr *ifam;
1319 
1320 				ifam = (struct ifa_msghdr *)w->w_tmem;
1321 				ifam->ifam_index = ifa->ifa_ifp->if_index;
1322 				ifam->ifam_flags = ifa->ifa_flags;
1323 				ifam->ifam_metric = ifa->ifa_metric;
1324 				ifam->ifam_addrs = info.rti_addrs;
1325 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1326 				if (error)
1327 					goto done;
1328 			}
1329 		}
1330 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1331 			info.rti_info[RTAX_BRD] = NULL;
1332 	}
1333 done:
1334 	IFNET_RUNLOCK();
1335 	return (error);
1336 }
1337 
1338 int
1339 sysctl_ifmalist(int af, struct walkarg *w)
1340 {
1341 	INIT_VNET_NET(curvnet);
1342 	struct ifnet *ifp;
1343 	struct ifmultiaddr *ifma;
1344 	struct	rt_addrinfo info;
1345 	int	len, error = 0;
1346 	struct ifaddr *ifa;
1347 
1348 	bzero((caddr_t)&info, sizeof(info));
1349 	IFNET_RLOCK();
1350 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1351 		if (w->w_arg && w->w_arg != ifp->if_index)
1352 			continue;
1353 		ifa = ifp->if_addr;
1354 		info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1355 		IF_ADDR_LOCK(ifp);
1356 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1357 			if (af && af != ifma->ifma_addr->sa_family)
1358 				continue;
1359 			if (jailed(curproc->p_ucred) &&
1360 			    !prison_if(curproc->p_ucred, ifma->ifma_addr))
1361 				continue;
1362 			info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1363 			info.rti_info[RTAX_GATEWAY] =
1364 			    (ifma->ifma_addr->sa_family != AF_LINK) ?
1365 			    ifma->ifma_lladdr : NULL;
1366 			len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
1367 			if (w->w_req && w->w_tmem) {
1368 				struct ifma_msghdr *ifmam;
1369 
1370 				ifmam = (struct ifma_msghdr *)w->w_tmem;
1371 				ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1372 				ifmam->ifmam_flags = 0;
1373 				ifmam->ifmam_addrs = info.rti_addrs;
1374 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1375 				if (error) {
1376 					IF_ADDR_UNLOCK(ifp);
1377 					goto done;
1378 				}
1379 			}
1380 		}
1381 		IF_ADDR_UNLOCK(ifp);
1382 	}
1383 done:
1384 	IFNET_RUNLOCK();
1385 	return (error);
1386 }
1387 
1388 static int
1389 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1390 {
1391 	INIT_VNET_NET(curvnet);
1392 	int	*name = (int *)arg1;
1393 	u_int	namelen = arg2;
1394 	struct radix_node_head *rnh;
1395 	int	i, lim, error = EINVAL;
1396 	u_char	af;
1397 	struct	walkarg w;
1398 
1399 	name ++;
1400 	namelen--;
1401 	if (req->newptr)
1402 		return (EPERM);
1403 	if (namelen != 3)
1404 		return ((namelen < 3) ? EISDIR : ENOTDIR);
1405 	af = name[0];
1406 	if (af > AF_MAX)
1407 		return (EINVAL);
1408 	bzero(&w, sizeof(w));
1409 	w.w_op = name[1];
1410 	w.w_arg = name[2];
1411 	w.w_req = req;
1412 
1413 	error = sysctl_wire_old_buffer(req, 0);
1414 	if (error)
1415 		return (error);
1416 	switch (w.w_op) {
1417 
1418 	case NET_RT_DUMP:
1419 	case NET_RT_FLAGS:
1420 		if (af == 0) {			/* dump all tables */
1421 			i = 1;
1422 			lim = AF_MAX;
1423 		} else				/* dump only one table */
1424 			i = lim = af;
1425 		for (error = 0; error == 0 && i <= lim; i++)
1426 			if ((rnh = V_rt_tables[curthread->td_proc->p_fibnum][i]) != NULL) {
1427 				RADIX_NODE_HEAD_LOCK(rnh);
1428 			    	error = rnh->rnh_walktree(rnh,
1429 				    sysctl_dumpentry, &w);
1430 				RADIX_NODE_HEAD_UNLOCK(rnh);
1431 			} else if (af != 0)
1432 				error = EAFNOSUPPORT;
1433 		break;
1434 
1435 	case NET_RT_IFLIST:
1436 		error = sysctl_iflist(af, &w);
1437 		break;
1438 
1439 	case NET_RT_IFMALIST:
1440 		error = sysctl_ifmalist(af, &w);
1441 		break;
1442 	}
1443 	if (w.w_tmem)
1444 		free(w.w_tmem, M_RTABLE);
1445 	return (error);
1446 }
1447 
1448 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1449 
1450 /*
1451  * Definitions of protocols supported in the ROUTE domain.
1452  */
1453 
1454 static struct domain routedomain;		/* or at least forward */
1455 
1456 static struct protosw routesw[] = {
1457 {
1458 	.pr_type =		SOCK_RAW,
1459 	.pr_domain =		&routedomain,
1460 	.pr_flags =		PR_ATOMIC|PR_ADDR,
1461 	.pr_output =		route_output,
1462 	.pr_ctlinput =		raw_ctlinput,
1463 	.pr_init =		raw_init,
1464 	.pr_usrreqs =		&route_usrreqs
1465 }
1466 };
1467 
1468 static struct domain routedomain = {
1469 	.dom_family =		PF_ROUTE,
1470 	.dom_name =		 "route",
1471 	.dom_protosw =		routesw,
1472 	.dom_protoswNPROTOSW =	&routesw[sizeof(routesw)/sizeof(routesw[0])]
1473 };
1474 
1475 DOMAIN_SET(route);
1476