xref: /netbsd-src/sys/net/rtsock.c (revision 9aa0541bdf64142d9a27c2cf274394d60182818f)
1 /*	$NetBSD: rtsock.c,v 1.136 2011/07/17 20:54:52 joerg Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1988, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.136 2011/07/17 20:54:52 joerg Exp $");
65 
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_mpls.h"
69 #include "opt_compat_netbsd.h"
70 #endif
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/proc.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/sysctl.h>
81 #include <sys/kauth.h>
82 #include <sys/intr.h>
83 #ifdef RTSOCK_DEBUG
84 #include <netinet/in.h>
85 #endif /* RTSOCK_DEBUG */
86 
87 #include <net/if.h>
88 #include <net/route.h>
89 #include <net/raw_cb.h>
90 
91 #include <netmpls/mpls.h>
92 
93 #if defined(COMPAT_14) || defined(COMPAT_50)
94 #include <compat/net/if.h>
95 #include <compat/net/route.h>
96 #endif
97 #ifdef COMPAT_RTSOCK
98 #define	RTM_XVERSION	RTM_OVERSION
99 #define	RT_XADVANCE(a,b) RT_OADVANCE(a,b)
100 #define	RT_XROUNDUP(n)	RT_OROUNDUP(n)
101 #define	PF_XROUTE	PF_OROUTE
102 #define	rt_xmsghdr	rt_msghdr50
103 #define	if_xmsghdr	if_msghdr	/* if_msghdr50 is for RTM_OIFINFO */
104 #define	ifa_xmsghdr	ifa_msghdr50
105 #define	if_xannouncemsghdr	if_announcemsghdr50
106 #define	COMPATNAME(x)	compat_50_ ## x
107 #define	DOMAINNAME	"oroute"
108 CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
109 DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
110 #else
111 #define	RTM_XVERSION	RTM_VERSION
112 #define	RT_XADVANCE(a,b) RT_ADVANCE(a,b)
113 #define	RT_XROUNDUP(n)	RT_ROUNDUP(n)
114 #define	PF_XROUTE	PF_ROUTE
115 #define	rt_xmsghdr	rt_msghdr
116 #define	if_xmsghdr	if_msghdr
117 #define	ifa_xmsghdr	ifa_msghdr
118 #define	if_xannouncemsghdr	if_announcemsghdr
119 #define	COMPATNAME(x)	x
120 #define	DOMAINNAME	"route"
121 CTASSERT(sizeof(struct ifa_xmsghdr) == 24);
122 #ifdef COMPAT_50
123 #define	COMPATCALL(name, args)	compat_50_ ## name args
124 #endif
125 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
126 #undef COMPAT_50
127 #undef COMPAT_14
128 #endif
129 
130 #ifndef COMPATCALL
131 #define	COMPATCALL(name, args)	do { } while (/*CONSTCOND*/ 0)
132 #endif
133 
134 struct route_info COMPATNAME(route_info) = {
135 	.ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
136 	.ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
137 	.ri_maxqlen = IFQ_MAXLEN,
138 };
139 
140 #define	PRESERVED_RTF	(RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK)
141 
142 static void COMPATNAME(route_init)(void);
143 static int COMPATNAME(route_output)(struct mbuf *, ...);
144 static int COMPATNAME(route_usrreq)(struct socket *,
145 	    int, struct mbuf *, struct mbuf *, struct mbuf *, struct lwp *);
146 
147 static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
148 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
149 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
150     struct rt_addrinfo *);
151 static void rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
152 static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
153 static void sysctl_net_route_setup(struct sysctllog **);
154 static int sysctl_dumpentry(struct rtentry *, void *);
155 static int sysctl_iflist(int, struct rt_walkarg *, int);
156 static int sysctl_rtable(SYSCTLFN_PROTO);
157 static void rt_adjustcount(int, int);
158 
159 static void
160 rt_adjustcount(int af, int cnt)
161 {
162 	struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
163 
164 	cb->any_count += cnt;
165 
166 	switch (af) {
167 	case AF_INET:
168 		cb->ip_count += cnt;
169 		return;
170 #ifdef INET6
171 	case AF_INET6:
172 		cb->ip6_count += cnt;
173 		return;
174 #endif
175 	case AF_ISO:
176 		cb->iso_count += cnt;
177 		return;
178 	case AF_MPLS:
179 		cb->mpls_count += cnt;
180 		return;
181 	}
182 }
183 
184 /*ARGSUSED*/
185 int
186 COMPATNAME(route_usrreq)(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
187 	struct mbuf *control, struct lwp *l)
188 {
189 	int error = 0;
190 	struct rawcb *rp = sotorawcb(so);
191 	int s;
192 
193 	if (req == PRU_ATTACH) {
194 		sosetlock(so);
195 		rp = malloc(sizeof(*rp), M_PCB, M_WAITOK|M_ZERO);
196 		so->so_pcb = rp;
197 	}
198 	if (req == PRU_DETACH && rp)
199 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
200 	s = splsoftnet();
201 
202 	/*
203 	 * Don't call raw_usrreq() in the attach case, because
204 	 * we want to allow non-privileged processes to listen on
205 	 * and send "safe" commands to the routing socket.
206 	 */
207 	if (req == PRU_ATTACH) {
208 		if (l == NULL)
209 			error = EACCES;
210 		else
211 			error = raw_attach(so, (int)(long)nam);
212 	} else
213 		error = raw_usrreq(so, req, m, nam, control, l);
214 
215 	rp = sotorawcb(so);
216 	if (req == PRU_ATTACH && rp) {
217 		if (error) {
218 			free(rp, M_PCB);
219 			splx(s);
220 			return error;
221 		}
222 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
223 		rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
224 		rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
225 		soisconnected(so);
226 		so->so_options |= SO_USELOOPBACK;
227 	}
228 	splx(s);
229 	return error;
230 }
231 
232 /*ARGSUSED*/
233 int
234 COMPATNAME(route_output)(struct mbuf *m, ...)
235 {
236 	struct sockproto proto = { .sp_family = PF_XROUTE, };
237 	struct rt_xmsghdr *rtm = NULL;
238 	struct rt_xmsghdr *old_rtm = NULL;
239 	struct rtentry *rt = NULL;
240 	struct rtentry *saved_nrt = NULL;
241 	struct rt_addrinfo info;
242 	int len, error = 0;
243 	struct ifnet *ifp = NULL;
244 	struct ifaddr *ifa = NULL;
245 	struct socket *so;
246 	va_list ap;
247 	sa_family_t family;
248 
249 	va_start(ap, m);
250 	so = va_arg(ap, struct socket *);
251 	va_end(ap);
252 
253 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
254 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
255 	   (m = m_pullup(m, sizeof(int32_t))) == NULL))
256 		return ENOBUFS;
257 	if ((m->m_flags & M_PKTHDR) == 0)
258 		panic("%s", __func__);
259 	len = m->m_pkthdr.len;
260 	if (len < sizeof(*rtm) ||
261 	    len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
262 		info.rti_info[RTAX_DST] = NULL;
263 		senderr(EINVAL);
264 	}
265 	R_Malloc(rtm, struct rt_xmsghdr *, len);
266 	if (rtm == NULL) {
267 		info.rti_info[RTAX_DST] = NULL;
268 		senderr(ENOBUFS);
269 	}
270 	m_copydata(m, 0, len, rtm);
271 	if (rtm->rtm_version != RTM_XVERSION) {
272 		info.rti_info[RTAX_DST] = NULL;
273 		senderr(EPROTONOSUPPORT);
274 	}
275 	rtm->rtm_pid = curproc->p_pid;
276 	memset(&info, 0, sizeof(info));
277 	info.rti_addrs = rtm->rtm_addrs;
278 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
279 	    &info)) {
280 		senderr(EINVAL);
281 	}
282 	info.rti_flags = rtm->rtm_flags;
283 #ifdef RTSOCK_DEBUG
284 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
285 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
286 		    inet_ntoa(((const struct sockaddr_in *)
287 		    info.rti_info[RTAX_DST])->sin_addr));
288 	}
289 #endif /* RTSOCK_DEBUG */
290 	if (info.rti_info[RTAX_DST] == NULL ||
291 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
292 		senderr(EINVAL);
293 	}
294 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
295 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
296 		senderr(EINVAL);
297 	}
298 
299 	/*
300 	 * Verify that the caller has the appropriate privilege; RTM_GET
301 	 * is the only operation the non-superuser is allowed.
302 	 */
303 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
304 	    0, rtm, NULL, NULL) != 0)
305 		senderr(EACCES);
306 
307 	switch (rtm->rtm_type) {
308 
309 	case RTM_ADD:
310 		if (info.rti_info[RTAX_GATEWAY] == NULL) {
311 			senderr(EINVAL);
312 		}
313 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
314 		if (error == 0 && saved_nrt) {
315 			rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
316 			saved_nrt->rt_refcnt--;
317 		}
318 		break;
319 
320 	case RTM_DELETE:
321 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
322 		if (error == 0) {
323 			(rt = saved_nrt)->rt_refcnt++;
324 			goto report;
325 		}
326 		break;
327 
328 	case RTM_GET:
329 	case RTM_CHANGE:
330 	case RTM_LOCK:
331                 /* XXX This will mask info.rti_info[RTAX_DST] with
332 		 * info.rti_info[RTAX_NETMASK] before
333                  * searching.  It did not used to do that.  --dyoung
334 		 */
335 		error = rtrequest1(RTM_GET, &info, &rt);
336 		if (error != 0)
337 			senderr(error);
338 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
339 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
340 			    info.rti_info[RTAX_DST]->sa_len) != 0)
341 				senderr(ESRCH);
342 			if (info.rti_info[RTAX_NETMASK] == NULL &&
343 			    rt_mask(rt) != NULL)
344 				senderr(ETOOMANYREFS);
345 		}
346 
347 		switch (rtm->rtm_type) {
348 		case RTM_GET:
349 		report:
350 			info.rti_info[RTAX_DST] = rt_getkey(rt);
351 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
352 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
353 			info.rti_info[RTAX_TAG] = (struct sockaddr*)rt_gettag(rt);
354 			if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
355 				;
356 			else if ((ifp = rt->rt_ifp) != NULL) {
357 				const struct ifaddr *rtifa;
358 				info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
359                                 /* rtifa used to be simply rt->rt_ifa.
360                                  * If rt->rt_ifa != NULL, then
361                                  * rt_get_ifa() != NULL.  So this
362                                  * ought to still be safe. --dyoung
363 				 */
364 				rtifa = rt_get_ifa(rt);
365 				info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
366 #ifdef RTSOCK_DEBUG
367 				if (info.rti_info[RTAX_IFA]->sa_family ==
368 				    AF_INET) {
369 					printf("%s: copying out RTAX_IFA %s ",
370 					    __func__, inet_ntoa(
371 					    ((const struct sockaddr_in *)
372 					    info.rti_info[RTAX_IFA])->sin_addr)
373 					    );
374 					printf("for info.rti_info[RTAX_DST] %s "
375 					    "ifa_getifa %p ifa_seqno %p\n",
376 					    inet_ntoa(
377 					    ((const struct sockaddr_in *)
378 					    info.rti_info[RTAX_DST])->sin_addr),
379 					    (void *)rtifa->ifa_getifa,
380 					    rtifa->ifa_seqno);
381 				}
382 #endif /* RTSOCK_DEBUG */
383 				if (ifp->if_flags & IFF_POINTOPOINT) {
384 					info.rti_info[RTAX_BRD] =
385 					    rtifa->ifa_dstaddr;
386 				} else
387 					info.rti_info[RTAX_BRD] = NULL;
388 				rtm->rtm_index = ifp->if_index;
389 			} else {
390 				info.rti_info[RTAX_IFP] = NULL;
391 				info.rti_info[RTAX_IFA] = NULL;
392 			}
393 			(void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
394 			if (len > rtm->rtm_msglen) {
395 				old_rtm = rtm;
396 				R_Malloc(rtm, struct rt_xmsghdr *, len);
397 				if (rtm == NULL)
398 					senderr(ENOBUFS);
399 				(void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
400 			}
401 			(void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
402 			rtm->rtm_flags = rt->rt_flags;
403 			rtm_setmetrics(rt, rtm);
404 			rtm->rtm_addrs = info.rti_addrs;
405 			break;
406 
407 		case RTM_CHANGE:
408 			/*
409 			 * new gateway could require new ifaddr, ifp;
410 			 * flags may also be different; ifp may be specified
411 			 * by ll sockaddr when protocol address is ambiguous
412 			 */
413 			if ((error = rt_getifa(&info)) != 0)
414 				senderr(error);
415 			if (info.rti_info[RTAX_GATEWAY] &&
416 			    rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
417 				senderr(EDQUOT);
418 			if (info.rti_info[RTAX_TAG])
419 				rt_settag(rt, info.rti_info[RTAX_TAG]);
420 			/* new gateway could require new ifaddr, ifp;
421 			   flags may also be different; ifp may be specified
422 			   by ll sockaddr when protocol address is ambiguous */
423 			if (info.rti_info[RTAX_IFP] &&
424 			    (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
425 			    (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
426 			    info.rti_info[RTAX_GATEWAY])) {
427 				if (info.rti_info[RTAX_IFA] == NULL ||
428 				    (ifa = ifa_ifwithaddr(
429 				    info.rti_info[RTAX_IFA])) == NULL)
430 					ifa = ifaof_ifpforaddr(
431 					    info.rti_info[RTAX_IFA] ?
432 					    info.rti_info[RTAX_IFA] :
433 					    info.rti_info[RTAX_GATEWAY], ifp);
434 			} else if ((info.rti_info[RTAX_IFA] &&
435 			    (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
436 			    (info.rti_info[RTAX_GATEWAY] &&
437 			    (ifa = ifa_ifwithroute(rt->rt_flags,
438 			    rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
439 				ifp = ifa->ifa_ifp;
440 			}
441 			if (ifa) {
442 				struct ifaddr *oifa = rt->rt_ifa;
443 				if (oifa != ifa) {
444 					if (oifa && oifa->ifa_rtrequest) {
445 						oifa->ifa_rtrequest(RTM_DELETE,
446 						    rt, &info);
447 					}
448 					rt_replace_ifa(rt, ifa);
449 					rt->rt_ifp = ifp;
450 				}
451 			}
452 			if (ifp && rt->rt_ifp != ifp)
453 				rt->rt_ifp = ifp;
454 			rt_setmetrics(rtm->rtm_inits, rtm, rt);
455 			if (rt->rt_flags != info.rti_flags)
456 				rt->rt_flags = (info.rti_flags & ~PRESERVED_RTF)
457 				    | (rt->rt_flags & PRESERVED_RTF);
458 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
459 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
460 			/*FALLTHROUGH*/
461 		case RTM_LOCK:
462 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
463 			rt->rt_rmx.rmx_locks |=
464 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
465 			break;
466 		}
467 		break;
468 
469 	default:
470 		senderr(EOPNOTSUPP);
471 	}
472 
473 flush:
474 	if (rtm) {
475 		if (error)
476 			rtm->rtm_errno = error;
477 		else
478 			rtm->rtm_flags |= RTF_DONE;
479 	}
480 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
481 	    0;
482 	/* We cannot free old_rtm until we have stopped using the
483 	 * pointers in info, some of which may point to sockaddrs
484 	 * in old_rtm.
485 	 */
486 	if (old_rtm != NULL)
487 		Free(old_rtm);
488 	if (rt)
489 		rtfree(rt);
490     {
491 	struct rawcb *rp = NULL;
492 	/*
493 	 * Check to see if we don't want our own messages.
494 	 */
495 	if ((so->so_options & SO_USELOOPBACK) == 0) {
496 		if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
497 			if (rtm)
498 				Free(rtm);
499 			m_freem(m);
500 			return error;
501 		}
502 		/* There is another listener, so construct message */
503 		rp = sotorawcb(so);
504 	}
505 	if (rtm) {
506 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
507 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
508 			m_freem(m);
509 			m = NULL;
510 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
511 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
512 		Free(rtm);
513 	}
514 	if (rp)
515 		rp->rcb_proto.sp_family = 0; /* Avoid us */
516 	if (family)
517 		proto.sp_protocol = family;
518 	if (m)
519 		raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
520 		    &COMPATNAME(route_info).ri_dst);
521 	if (rp)
522 		rp->rcb_proto.sp_family = PF_XROUTE;
523     }
524 	return error;
525 }
526 
527 static void
528 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
529 {
530 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
531 	metric(RTV_RPIPE, rmx_recvpipe);
532 	metric(RTV_SPIPE, rmx_sendpipe);
533 	metric(RTV_SSTHRESH, rmx_ssthresh);
534 	metric(RTV_RTT, rmx_rtt);
535 	metric(RTV_RTTVAR, rmx_rttvar);
536 	metric(RTV_HOPCOUNT, rmx_hopcount);
537 	metric(RTV_MTU, rmx_mtu);
538 	metric(RTV_EXPIRE, rmx_expire);
539 #undef metric
540 }
541 
542 static void
543 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
544 {
545 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
546 	metric(rmx_recvpipe);
547 	metric(rmx_sendpipe);
548 	metric(rmx_ssthresh);
549 	metric(rmx_rtt);
550 	metric(rmx_rttvar);
551 	metric(rmx_hopcount);
552 	metric(rmx_mtu);
553 	metric(rmx_expire);
554 #undef metric
555 }
556 
557 static int
558 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
559     struct rt_addrinfo *rtinfo)
560 {
561 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
562 	int i;
563 
564 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
565 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
566 			continue;
567 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
568 		RT_XADVANCE(cp, sa);
569 	}
570 
571 	/*
572 	 * Check for extra addresses specified, except RTM_GET asking
573 	 * for interface info.
574 	 */
575 	if (rtmtype == RTM_GET) {
576 		if (((rtinfo->rti_addrs &
577 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
578 			return 1;
579 	} else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
580 		return 1;
581 	/* Check for bad data length.  */
582 	if (cp != cplim) {
583 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
584 		    cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
585 			/*
586 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
587 			 * We accept this for now for the sake of old
588 			 * binaries or third party softwares.
589 			 */
590 			;
591 		else
592 			return 1;
593 	}
594 	return 0;
595 }
596 
597 static int
598 rt_getlen(int type)
599 {
600 #ifndef COMPAT_RTSOCK
601 	CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
602 	CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
603 	CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
604 	CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
605 #endif
606 
607 	switch (type) {
608 	case RTM_DELADDR:
609 	case RTM_NEWADDR:
610 	case RTM_CHGADDR:
611 		return sizeof(struct ifa_xmsghdr);
612 
613 	case RTM_OOIFINFO:
614 #ifdef COMPAT_14
615 		return sizeof(struct if_msghdr14);
616 #else
617 #ifdef DIAGNOSTIC
618 		printf("RTM_OOIFINFO\n");
619 #endif
620 		return -1;
621 #endif
622 	case RTM_OIFINFO:
623 #ifdef COMPAT_50
624 		return sizeof(struct if_msghdr50);
625 #else
626 #ifdef DIAGNOSTIC
627 		printf("RTM_OIFINFO\n");
628 #endif
629 		return -1;
630 #endif
631 
632 	case RTM_IFINFO:
633 		return sizeof(struct if_xmsghdr);
634 
635 	case RTM_IFANNOUNCE:
636 	case RTM_IEEE80211:
637 		return sizeof(struct if_xannouncemsghdr);
638 
639 	default:
640 		return sizeof(struct rt_xmsghdr);
641 	}
642 }
643 
644 
645 struct mbuf *
646 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
647 {
648 	struct rt_xmsghdr *rtm;
649 	struct mbuf *m;
650 	int i;
651 	const struct sockaddr *sa;
652 	int len, dlen;
653 
654 	m = m_gethdr(M_DONTWAIT, MT_DATA);
655 	if (m == NULL)
656 		return m;
657 	MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
658 
659 	if ((len = rt_getlen(type)) == -1)
660 		goto out;
661 	if (len > MHLEN + MLEN)
662 		panic("%s: message too long", __func__);
663 	else if (len > MHLEN) {
664 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
665 		if (m->m_next == NULL)
666 			goto out;
667 		MCLAIM(m->m_next, m->m_owner);
668 		m->m_pkthdr.len = len;
669 		m->m_len = MHLEN;
670 		m->m_next->m_len = len - MHLEN;
671 	} else {
672 		m->m_pkthdr.len = m->m_len = len;
673 	}
674 	m->m_pkthdr.rcvif = NULL;
675 	m_copyback(m, 0, datalen, data);
676 	if (len > datalen)
677 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
678 	rtm = mtod(m, struct rt_xmsghdr *);
679 	for (i = 0; i < RTAX_MAX; i++) {
680 		if ((sa = rtinfo->rti_info[i]) == NULL)
681 			continue;
682 		rtinfo->rti_addrs |= (1 << i);
683 		dlen = RT_XROUNDUP(sa->sa_len);
684 		m_copyback(m, len, sa->sa_len, sa);
685 		if (dlen != sa->sa_len) {
686 			m_copyback(m, len + sa->sa_len,
687 			    dlen - sa->sa_len, "\0\0\0\0\0\0");
688 		}
689 		len += dlen;
690 	}
691 	if (m->m_pkthdr.len != len)
692 		goto out;
693 	rtm->rtm_msglen = len;
694 	rtm->rtm_version = RTM_XVERSION;
695 	rtm->rtm_type = type;
696 	return m;
697 out:
698 	m_freem(m);
699 	return NULL;
700 }
701 
702 /*
703  * rt_msg2
704  *
705  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
706  *		returns the length of the message in 'lenp'.
707  *
708  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
709  *	the message
710  * otherwise walkarg's w_needed is updated and if the user buffer is
711  *	specified and w_needed indicates space exists the information is copied
712  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
713  *	if the allocation fails ENOBUFS is returned.
714  */
715 static int
716 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
717 	int *lenp)
718 {
719 	int i;
720 	int len, dlen, second_time = 0;
721 	char *cp0, *cp = cpv;
722 
723 	rtinfo->rti_addrs = 0;
724 again:
725 	if ((len = rt_getlen(type)) == -1)
726 		return EINVAL;
727 
728 	if ((cp0 = cp) != NULL)
729 		cp += len;
730 	for (i = 0; i < RTAX_MAX; i++) {
731 		const struct sockaddr *sa;
732 
733 		if ((sa = rtinfo->rti_info[i]) == NULL)
734 			continue;
735 		rtinfo->rti_addrs |= (1 << i);
736 		dlen = RT_XROUNDUP(sa->sa_len);
737 		if (cp) {
738 			(void)memcpy(cp, sa, (size_t)dlen);
739 			cp += dlen;
740 		}
741 		len += dlen;
742 	}
743 	if (cp == NULL && w != NULL && !second_time) {
744 		struct rt_walkarg *rw = w;
745 
746 		rw->w_needed += len;
747 		if (rw->w_needed <= 0 && rw->w_where) {
748 			if (rw->w_tmemsize < len) {
749 				if (rw->w_tmem)
750 					free(rw->w_tmem, M_RTABLE);
751 				rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
752 				if (rw->w_tmem)
753 					rw->w_tmemsize = len;
754 				else
755 					rw->w_tmemsize = 0;
756 			}
757 			if (rw->w_tmem) {
758 				cp = rw->w_tmem;
759 				second_time = 1;
760 				goto again;
761 			} else {
762 				rw->w_tmemneeded = len;
763 				return ENOBUFS;
764 			}
765 		}
766 	}
767 	if (cp) {
768 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
769 
770 		rtm->rtm_version = RTM_XVERSION;
771 		rtm->rtm_type = type;
772 		rtm->rtm_msglen = len;
773 	}
774 	if (lenp)
775 		*lenp = len;
776 	return 0;
777 }
778 
779 /*
780  * This routine is called to generate a message from the routing
781  * socket indicating that a redirect has occurred, a routing lookup
782  * has failed, or that a protocol has detected timeouts to a particular
783  * destination.
784  */
785 void
786 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
787     int error)
788 {
789 	struct rt_xmsghdr rtm;
790 	struct mbuf *m;
791 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
792 	struct rt_addrinfo info = *rtinfo;
793 
794 	COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
795 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
796 		return;
797 	memset(&rtm, 0, sizeof(rtm));
798 	rtm.rtm_flags = RTF_DONE | flags;
799 	rtm.rtm_errno = error;
800 	m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
801 	if (m == NULL)
802 		return;
803 	mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
804 	COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
805 }
806 
807 /*
808  * This routine is called to generate a message from the routing
809  * socket indicating that the status of a network interface has changed.
810  */
811 void
812 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
813 {
814 	struct if_xmsghdr ifm;
815 	struct mbuf *m;
816 	struct rt_addrinfo info;
817 
818 	COMPATCALL(rt_ifmsg, (ifp));
819 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
820 		return;
821 	(void)memset(&info, 0, sizeof(info));
822 	(void)memset(&ifm, 0, sizeof(ifm));
823 	ifm.ifm_index = ifp->if_index;
824 	ifm.ifm_flags = ifp->if_flags;
825 	ifm.ifm_data = ifp->if_data;
826 	ifm.ifm_addrs = 0;
827 	m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
828 	if (m == NULL)
829 		return;
830 	COMPATNAME(route_enqueue)(m, 0);
831 #ifdef COMPAT_14
832 	compat_14_rt_oifmsg(ifp);
833 #endif
834 #ifdef COMPAT_50
835 	compat_50_rt_oifmsg(ifp);
836 #endif
837 }
838 
839 
840 /*
841  * This is called to generate messages from the routing socket
842  * indicating a network interface has had addresses associated with it.
843  * if we ever reverse the logic and replace messages TO the routing
844  * socket indicate a request to configure interfaces, then it will
845  * be unnecessary as the routing socket will automatically generate
846  * copies of it.
847  */
848 void
849 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
850     struct rtentry *rt)
851 {
852 #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
853 	struct rt_addrinfo info;
854 	const struct sockaddr *sa;
855 	int pass;
856 	struct mbuf *m;
857 	struct ifnet *ifp = ifa->ifa_ifp;
858 	struct rt_xmsghdr rtm;
859 	struct ifa_xmsghdr ifam;
860 	int ncmd;
861 
862 	COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
863 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
864 		return;
865 	for (pass = 1; pass < 3; pass++) {
866 		memset(&info, 0, sizeof(info));
867 		switch (cmdpass(cmd, pass)) {
868 		case cmdpass(RTM_ADD, 1):
869 		case cmdpass(RTM_CHANGE, 1):
870 		case cmdpass(RTM_DELETE, 2):
871 			switch (cmd) {
872 			case RTM_DELETE:
873 				ncmd = RTM_DELADDR;
874 				break;
875 			case RTM_CHANGE:
876 				ncmd = RTM_CHGADDR;
877 				break;
878 			default:
879 				ncmd = RTM_NEWADDR;
880 			}
881 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
882 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
883 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
884 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
885 			memset(&ifam, 0, sizeof(ifam));
886 			ifam.ifam_index = ifp->if_index;
887 			ifam.ifam_metric = ifa->ifa_metric;
888 			ifam.ifam_flags = ifa->ifa_flags;
889 			m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
890 			if (m == NULL)
891 				continue;
892 			mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
893 			    info.rti_addrs;
894 			break;
895 		case cmdpass(RTM_ADD, 2):
896 		case cmdpass(RTM_CHANGE, 2):
897 		case cmdpass(RTM_DELETE, 1):
898 			if (rt == NULL)
899 				continue;
900 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
901 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
902 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
903 			memset(&rtm, 0, sizeof(rtm));
904 			rtm.rtm_index = ifp->if_index;
905 			rtm.rtm_flags |= rt->rt_flags;
906 			rtm.rtm_errno = error;
907 			m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
908 			if (m == NULL)
909 				continue;
910 			mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
911 			break;
912 		default:
913 			continue;
914 		}
915 #ifdef DIAGNOSTIC
916 		if (m == NULL)
917 			panic("%s: called with wrong command", __func__);
918 #endif
919 		COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
920 	}
921 #undef cmdpass
922 }
923 
924 static struct mbuf *
925 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
926     struct rt_addrinfo *info)
927 {
928 	struct if_xannouncemsghdr ifan;
929 
930 	memset(info, 0, sizeof(*info));
931 	memset(&ifan, 0, sizeof(ifan));
932 	ifan.ifan_index = ifp->if_index;
933 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
934 	ifan.ifan_what = what;
935 	return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
936 }
937 
938 /*
939  * This is called to generate routing socket messages indicating
940  * network interface arrival and departure.
941  */
942 void
943 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
944 {
945 	struct mbuf *m;
946 	struct rt_addrinfo info;
947 
948 	COMPATCALL(rt_ifannouncemsg, (ifp, what));
949 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
950 		return;
951 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
952 	if (m == NULL)
953 		return;
954 	COMPATNAME(route_enqueue)(m, 0);
955 }
956 
957 /*
958  * This is called to generate routing socket messages indicating
959  * IEEE80211 wireless events.
960  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
961  */
962 void
963 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
964 	size_t data_len)
965 {
966 	struct mbuf *m;
967 	struct rt_addrinfo info;
968 
969 	COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
970 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
971 		return;
972 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
973 	if (m == NULL)
974 		return;
975 	/*
976 	 * Append the ieee80211 data.  Try to stick it in the
977 	 * mbuf containing the ifannounce msg; otherwise allocate
978 	 * a new mbuf and append.
979 	 *
980 	 * NB: we assume m is a single mbuf.
981 	 */
982 	if (data_len > M_TRAILINGSPACE(m)) {
983 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
984 		if (n == NULL) {
985 			m_freem(m);
986 			return;
987 		}
988 		(void)memcpy(mtod(n, void *), data, data_len);
989 		n->m_len = data_len;
990 		m->m_next = n;
991 	} else if (data_len > 0) {
992 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
993 		m->m_len += data_len;
994 	}
995 	if (m->m_flags & M_PKTHDR)
996 		m->m_pkthdr.len += data_len;
997 	mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
998 	COMPATNAME(route_enqueue)(m, 0);
999 }
1000 
1001 /*
1002  * This is used in dumping the kernel table via sysctl().
1003  */
1004 static int
1005 sysctl_dumpentry(struct rtentry *rt, void *v)
1006 {
1007 	struct rt_walkarg *w = v;
1008 	int error = 0, size;
1009 	struct rt_addrinfo info;
1010 
1011 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1012 		return 0;
1013 	memset(&info, 0, sizeof(info));
1014 	info.rti_info[RTAX_DST] = rt_getkey(rt);
1015 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1016 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1017 	if (rt->rt_ifp) {
1018 		const struct ifaddr *rtifa;
1019 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1020 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
1021 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
1022 		 * --dyoung
1023 		 */
1024 		rtifa = rt_get_ifa(rt);
1025 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1026 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1027 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1028 	}
1029 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1030 		return error;
1031 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1032 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1033 
1034 		rtm->rtm_flags = rt->rt_flags;
1035 		rtm->rtm_use = rt->rt_use;
1036 		rtm_setmetrics(rt, rtm);
1037 		KASSERT(rt->rt_ifp != NULL);
1038 		rtm->rtm_index = rt->rt_ifp->if_index;
1039 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1040 		rtm->rtm_addrs = info.rti_addrs;
1041 		if ((error = copyout(rtm, w->w_where, size)) != 0)
1042 			w->w_where = NULL;
1043 		else
1044 			w->w_where = (char *)w->w_where + size;
1045 	}
1046 	return error;
1047 }
1048 
1049 static int
1050 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1051 {
1052 	struct ifnet *ifp;
1053 	struct ifaddr *ifa;
1054 	struct	rt_addrinfo info;
1055 	int	len, error = 0;
1056 
1057 	memset(&info, 0, sizeof(info));
1058 	IFNET_FOREACH(ifp) {
1059 		if (w->w_arg && w->w_arg != ifp->if_index)
1060 			continue;
1061 		if (IFADDR_EMPTY(ifp))
1062 			continue;
1063 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1064 		switch (type) {
1065 		case NET_RT_IFLIST:
1066 			error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1067 			break;
1068 #ifdef COMPAT_14
1069 		case NET_RT_OOIFLIST:
1070 			error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
1071 			break;
1072 #endif
1073 #ifdef COMPAT_50
1074 		case NET_RT_OIFLIST:
1075 			error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1076 			break;
1077 #endif
1078 		default:
1079 			panic("sysctl_iflist(1)");
1080 		}
1081 		if (error)
1082 			return error;
1083 		info.rti_info[RTAX_IFP] = NULL;
1084 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1085 			switch (type) {
1086 			case NET_RT_IFLIST: {
1087 				struct if_xmsghdr *ifm;
1088 
1089 				ifm = (struct if_xmsghdr *)w->w_tmem;
1090 				ifm->ifm_index = ifp->if_index;
1091 				ifm->ifm_flags = ifp->if_flags;
1092 				ifm->ifm_data = ifp->if_data;
1093 				ifm->ifm_addrs = info.rti_addrs;
1094 				error = copyout(ifm, w->w_where, len);
1095 				if (error)
1096 					return error;
1097 				w->w_where = (char *)w->w_where + len;
1098 				break;
1099 			}
1100 
1101 #ifdef COMPAT_14
1102 			case NET_RT_OOIFLIST:
1103 				error = compat_14_iflist(ifp, w, &info, len);
1104 				if (error)
1105 					return error;
1106 				break;
1107 #endif
1108 #ifdef COMPAT_50
1109 			case NET_RT_OIFLIST:
1110 				error = compat_50_iflist(ifp, w, &info, len);
1111 				if (error)
1112 					return error;
1113 				break;
1114 #endif
1115 			default:
1116 				panic("sysctl_iflist(2)");
1117 			}
1118 		}
1119 		IFADDR_FOREACH(ifa, ifp) {
1120 			if (af && af != ifa->ifa_addr->sa_family)
1121 				continue;
1122 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1123 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1124 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1125 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1126 				return error;
1127 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1128 				struct ifa_xmsghdr *ifam;
1129 
1130 				ifam = (struct ifa_xmsghdr *)w->w_tmem;
1131 				ifam->ifam_index = ifa->ifa_ifp->if_index;
1132 				ifam->ifam_flags = ifa->ifa_flags;
1133 				ifam->ifam_metric = ifa->ifa_metric;
1134 				ifam->ifam_addrs = info.rti_addrs;
1135 				error = copyout(w->w_tmem, w->w_where, len);
1136 				if (error)
1137 					return error;
1138 				w->w_where = (char *)w->w_where + len;
1139 			}
1140 		}
1141 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1142 		    info.rti_info[RTAX_BRD] = NULL;
1143 	}
1144 	return 0;
1145 }
1146 
1147 static int
1148 sysctl_rtable(SYSCTLFN_ARGS)
1149 {
1150 	void 	*where = oldp;
1151 	size_t	*given = oldlenp;
1152 	const void *new = newp;
1153 	int	i, s, error = EINVAL;
1154 	u_char  af;
1155 	struct	rt_walkarg w;
1156 
1157 	if (namelen == 1 && name[0] == CTL_QUERY)
1158 		return sysctl_query(SYSCTLFN_CALL(rnode));
1159 
1160 	if (new)
1161 		return EPERM;
1162 	if (namelen != 3)
1163 		return EINVAL;
1164 	af = name[0];
1165 	w.w_tmemneeded = 0;
1166 	w.w_tmemsize = 0;
1167 	w.w_tmem = NULL;
1168 again:
1169 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
1170 	if (w.w_tmemneeded) {
1171 		w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1172 		w.w_tmemsize = w.w_tmemneeded;
1173 		w.w_tmemneeded = 0;
1174 	}
1175 	w.w_op = name[1];
1176 	w.w_arg = name[2];
1177 	w.w_given = *given;
1178 	w.w_needed = 0 - w.w_given;
1179 	w.w_where = where;
1180 
1181 	s = splsoftnet();
1182 	switch (w.w_op) {
1183 
1184 	case NET_RT_DUMP:
1185 	case NET_RT_FLAGS:
1186 		for (i = 1; i <= AF_MAX; i++)
1187 			if ((af == 0 || af == i) &&
1188 			    (error = rt_walktree(i, sysctl_dumpentry, &w)))
1189 				break;
1190 		break;
1191 
1192 #ifdef COMPAT_14
1193 	case NET_RT_OOIFLIST:
1194 		error = sysctl_iflist(af, &w, w.w_op);
1195 		break;
1196 #endif
1197 #ifdef COMPAT_50
1198 	case NET_RT_OIFLIST:
1199 		error = sysctl_iflist(af, &w, w.w_op);
1200 		break;
1201 #endif
1202 	case NET_RT_IFLIST:
1203 		error = sysctl_iflist(af, &w, w.w_op);
1204 		break;
1205 	}
1206 	splx(s);
1207 
1208 	/* check to see if we couldn't allocate memory with NOWAIT */
1209 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1210 		goto again;
1211 
1212 	if (w.w_tmem)
1213 		free(w.w_tmem, M_RTABLE);
1214 	w.w_needed += w.w_given;
1215 	if (where) {
1216 		*given = (char *)w.w_where - (char *)where;
1217 		if (*given < w.w_needed)
1218 			return ENOMEM;
1219 	} else {
1220 		*given = (11 * w.w_needed) / 10;
1221 	}
1222 	return error;
1223 }
1224 
1225 /*
1226  * Routing message software interrupt routine
1227  */
1228 static void
1229 COMPATNAME(route_intr)(void *cookie)
1230 {
1231 	struct sockproto proto = { .sp_family = PF_XROUTE, };
1232 	struct route_info * const ri = &COMPATNAME(route_info);
1233 	struct mbuf *m;
1234 	int s;
1235 
1236 	mutex_enter(softnet_lock);
1237 	KERNEL_LOCK(1, NULL);
1238 	while (!IF_IS_EMPTY(&ri->ri_intrq)) {
1239 		s = splnet();
1240 		IF_DEQUEUE(&ri->ri_intrq, m);
1241 		splx(s);
1242 		if (m == NULL)
1243 			break;
1244 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
1245 		raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
1246 	}
1247 	KERNEL_UNLOCK_ONE(NULL);
1248 	mutex_exit(softnet_lock);
1249 }
1250 
1251 /*
1252  * Enqueue a message to the software interrupt routine.
1253  */
1254 void
1255 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1256 {
1257 	struct route_info * const ri = &COMPATNAME(route_info);
1258 	int s, wasempty;
1259 
1260 	s = splnet();
1261 	if (IF_QFULL(&ri->ri_intrq)) {
1262 		IF_DROP(&ri->ri_intrq);
1263 		m_freem(m);
1264 	} else {
1265 		wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1266 		M_SETCTX(m, (uintptr_t)family);
1267 		IF_ENQUEUE(&ri->ri_intrq, m);
1268 		if (wasempty)
1269 			softint_schedule(ri->ri_sih);
1270 	}
1271 	splx(s);
1272 }
1273 
1274 static void
1275 COMPATNAME(route_init)(void)
1276 {
1277 	struct route_info * const ri = &COMPATNAME(route_info);
1278 
1279 #ifndef COMPAT_RTSOCK
1280 	rt_init();
1281 #endif
1282 
1283 	sysctl_net_route_setup(NULL);
1284 	ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1285 	ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1286 	    COMPATNAME(route_intr), NULL);
1287 }
1288 
1289 /*
1290  * Definitions of protocols supported in the ROUTE domain.
1291  */
1292 #ifndef COMPAT_RTSOCK
1293 PR_WRAP_USRREQ(route_usrreq);
1294 #else
1295 PR_WRAP_USRREQ(compat_50_route_usrreq);
1296 #endif
1297 
1298 static const struct protosw COMPATNAME(route_protosw)[] = {
1299 	{
1300 		.pr_type = SOCK_RAW,
1301 		.pr_domain = &COMPATNAME(routedomain),
1302 		.pr_flags = PR_ATOMIC|PR_ADDR,
1303 		.pr_input = raw_input,
1304 		.pr_output = COMPATNAME(route_output),
1305 		.pr_ctlinput = raw_ctlinput,
1306 		.pr_usrreq = COMPATNAME(route_usrreq_wrapper),
1307 		.pr_init = raw_init,
1308 	},
1309 };
1310 
1311 struct domain COMPATNAME(routedomain) = {
1312 	.dom_family = PF_XROUTE,
1313 	.dom_name = DOMAINNAME,
1314 	.dom_init = COMPATNAME(route_init),
1315 	.dom_protosw = COMPATNAME(route_protosw),
1316 	.dom_protoswNPROTOSW =
1317 	    &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1318 };
1319 
1320 static void
1321 sysctl_net_route_setup(struct sysctllog **clog)
1322 {
1323 	const struct sysctlnode *rnode = NULL;
1324 
1325 	sysctl_createv(clog, 0, NULL, NULL,
1326 		       CTLFLAG_PERMANENT,
1327 		       CTLTYPE_NODE, "net", NULL,
1328 		       NULL, 0, NULL, 0,
1329 		       CTL_NET, CTL_EOL);
1330 
1331 	sysctl_createv(clog, 0, NULL, &rnode,
1332 		       CTLFLAG_PERMANENT,
1333 		       CTLTYPE_NODE, DOMAINNAME,
1334 		       SYSCTL_DESCR("PF_ROUTE information"),
1335 		       NULL, 0, NULL, 0,
1336 		       CTL_NET, PF_XROUTE, CTL_EOL);
1337 
1338 	sysctl_createv(clog, 0, NULL, NULL,
1339 		       CTLFLAG_PERMANENT,
1340 		       CTLTYPE_NODE, "rtable",
1341 		       SYSCTL_DESCR("Routing table information"),
1342 		       sysctl_rtable, 0, NULL, 0,
1343 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
1344 
1345 	sysctl_createv(clog, 0, &rnode, NULL,
1346 		       CTLFLAG_PERMANENT,
1347 		       CTLTYPE_STRUCT, "stats",
1348 		       SYSCTL_DESCR("Routing statistics"),
1349 		       NULL, 0, &rtstat, sizeof(rtstat),
1350 		       CTL_CREATE, CTL_EOL);
1351 }
1352