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