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