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