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