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