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