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