xref: /netbsd-src/sys/net/rtsock.c (revision c34236556bea94afcaca1782d7d228301edc3ea0)
1 /*	$NetBSD: rtsock.c,v 1.199 2016/12/12 03:55:57 ozaki-r 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.199 2016/12/12 03:55:57 ozaki-r Exp $");
65 
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_mpls.h"
69 #include "opt_compat_netbsd.h"
70 #include "opt_sctp.h"
71 #endif
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/proc.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/kmem.h>
83 #include <sys/intr.h>
84 
85 #include <net/if.h>
86 #include <net/if_llatbl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 #include <net/raw_cb.h>
90 
91 #include <netinet/in_var.h>
92 #include <netinet/if_inarp.h>
93 
94 #include <netmpls/mpls.h>
95 
96 #ifdef SCTP
97 extern void sctp_add_ip_address(struct ifaddr *);
98 extern void sctp_delete_ip_address(struct ifaddr *);
99 #endif
100 
101 #if defined(COMPAT_14) || defined(COMPAT_50) || defined(COMPAT_70)
102 #include <compat/net/if.h>
103 #include <compat/net/route.h>
104 #endif
105 #ifdef COMPAT_RTSOCK
106 #define	RTM_XVERSION	RTM_OVERSION
107 #define	RTM_XNEWADDR	RTM_ONEWADDR
108 #define	RTM_XDELADDR	RTM_ODELADDR
109 #define	RTM_XCHGADDR	RTM_OCHGADDR
110 #define	RT_XADVANCE(a,b) RT_OADVANCE(a,b)
111 #define	RT_XROUNDUP(n)	RT_OROUNDUP(n)
112 #define	PF_XROUTE	PF_OROUTE
113 #define	rt_xmsghdr	rt_msghdr50
114 #define	if_xmsghdr	if_msghdr	/* if_msghdr50 is for RTM_OIFINFO */
115 #define	ifa_xmsghdr	ifa_msghdr50
116 #define	if_xannouncemsghdr	if_announcemsghdr50
117 #define	COMPATNAME(x)	compat_50_ ## x
118 #define	DOMAINNAME	"oroute"
119 CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
120 DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
121 #undef COMPAT_70
122 #else /* COMPAT_RTSOCK */
123 #define	RTM_XVERSION	RTM_VERSION
124 #define	RTM_XNEWADDR	RTM_NEWADDR
125 #define	RTM_XDELADDR	RTM_DELADDR
126 #define	RTM_XCHGADDR	RTM_CHGADDR
127 #define	RT_XADVANCE(a,b) RT_ADVANCE(a,b)
128 #define	RT_XROUNDUP(n)	RT_ROUNDUP(n)
129 #define	PF_XROUTE	PF_ROUTE
130 #define	rt_xmsghdr	rt_msghdr
131 #define	if_xmsghdr	if_msghdr
132 #define	ifa_xmsghdr	ifa_msghdr
133 #define	if_xannouncemsghdr	if_announcemsghdr
134 #define	COMPATNAME(x)	x
135 #define	DOMAINNAME	"route"
136 CTASSERT(sizeof(struct ifa_xmsghdr) == 32);
137 #ifdef COMPAT_50
138 #define	COMPATCALL(name, args)	compat_50_ ## name args
139 #endif
140 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
141 #undef COMPAT_50
142 #undef COMPAT_14
143 #endif /* COMPAT_RTSOCK */
144 
145 #ifndef COMPATCALL
146 #define	COMPATCALL(name, args)	do { } while (/*CONSTCOND*/ 0)
147 #endif
148 
149 #ifdef RTSOCK_DEBUG
150 #define RT_IN_PRINT(info, b, a) (in_print((b), sizeof(b), \
151     &((const struct sockaddr_in *)(info)->rti_info[(a)])->sin_addr), (b))
152 #endif /* RTSOCK_DEBUG */
153 
154 struct route_info COMPATNAME(route_info) = {
155 	.ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
156 	.ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
157 	.ri_maxqlen = IFQ_MAXLEN,
158 };
159 
160 #define	PRESERVED_RTF	(RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK)
161 
162 static void COMPATNAME(route_init)(void);
163 static int COMPATNAME(route_output)(struct mbuf *, struct socket *);
164 
165 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
166 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
167     struct rt_addrinfo *);
168 static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
169 static void rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
170 static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
171 static void sysctl_net_route_setup(struct sysctllog **);
172 static int sysctl_dumpentry(struct rtentry *, void *);
173 static int sysctl_iflist(int, struct rt_walkarg *, int);
174 static int sysctl_rtable(SYSCTLFN_PROTO);
175 static void rt_adjustcount(int, int);
176 
177 static const struct protosw COMPATNAME(route_protosw)[];
178 
179 static void
180 rt_adjustcount(int af, int cnt)
181 {
182 	struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
183 
184 	cb->any_count += cnt;
185 
186 	switch (af) {
187 	case AF_INET:
188 		cb->ip_count += cnt;
189 		return;
190 #ifdef INET6
191 	case AF_INET6:
192 		cb->ip6_count += cnt;
193 		return;
194 #endif
195 	case AF_MPLS:
196 		cb->mpls_count += cnt;
197 		return;
198 	}
199 }
200 
201 static int
202 COMPATNAME(route_attach)(struct socket *so, int proto)
203 {
204 	struct rawcb *rp;
205 	int s, error;
206 
207 	KASSERT(sotorawcb(so) == NULL);
208 	rp = kmem_zalloc(sizeof(*rp), KM_SLEEP);
209 	rp->rcb_len = sizeof(*rp);
210 	so->so_pcb = rp;
211 
212 	s = splsoftnet();
213 	if ((error = raw_attach(so, proto)) == 0) {
214 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
215 		rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
216 		rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
217 	}
218 	splx(s);
219 
220 	if (error) {
221 		kmem_free(rp, sizeof(*rp));
222 		so->so_pcb = NULL;
223 		return error;
224 	}
225 
226 	soisconnected(so);
227 	so->so_options |= SO_USELOOPBACK;
228 	KASSERT(solocked(so));
229 
230 	return error;
231 }
232 
233 static void
234 COMPATNAME(route_detach)(struct socket *so)
235 {
236 	struct rawcb *rp = sotorawcb(so);
237 	int s;
238 
239 	KASSERT(rp != NULL);
240 	KASSERT(solocked(so));
241 
242 	s = splsoftnet();
243 	rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
244 	raw_detach(so);
245 	splx(s);
246 }
247 
248 static int
249 COMPATNAME(route_accept)(struct socket *so, struct sockaddr *nam)
250 {
251 	KASSERT(solocked(so));
252 
253 	panic("route_accept");
254 
255 	return EOPNOTSUPP;
256 }
257 
258 static int
259 COMPATNAME(route_bind)(struct socket *so, struct sockaddr *nam, struct lwp *l)
260 {
261 	KASSERT(solocked(so));
262 
263 	return EOPNOTSUPP;
264 }
265 
266 static int
267 COMPATNAME(route_listen)(struct socket *so, struct lwp *l)
268 {
269 	KASSERT(solocked(so));
270 
271 	return EOPNOTSUPP;
272 }
273 
274 static int
275 COMPATNAME(route_connect)(struct socket *so, struct sockaddr *nam, struct lwp *l)
276 {
277 	KASSERT(solocked(so));
278 
279 	return EOPNOTSUPP;
280 }
281 
282 static int
283 COMPATNAME(route_connect2)(struct socket *so, struct socket *so2)
284 {
285 	KASSERT(solocked(so));
286 
287 	return EOPNOTSUPP;
288 }
289 
290 static int
291 COMPATNAME(route_disconnect)(struct socket *so)
292 {
293 	struct rawcb *rp = sotorawcb(so);
294 	int s;
295 
296 	KASSERT(solocked(so));
297 	KASSERT(rp != NULL);
298 
299 	s = splsoftnet();
300 	soisdisconnected(so);
301 	raw_disconnect(rp);
302 	splx(s);
303 
304 	return 0;
305 }
306 
307 static int
308 COMPATNAME(route_shutdown)(struct socket *so)
309 {
310 	int s;
311 
312 	KASSERT(solocked(so));
313 
314 	/*
315 	 * Mark the connection as being incapable of further input.
316 	 */
317 	s = splsoftnet();
318 	socantsendmore(so);
319 	splx(s);
320 	return 0;
321 }
322 
323 static int
324 COMPATNAME(route_abort)(struct socket *so)
325 {
326 	KASSERT(solocked(so));
327 
328 	panic("route_abort");
329 
330 	return EOPNOTSUPP;
331 }
332 
333 static int
334 COMPATNAME(route_ioctl)(struct socket *so, u_long cmd, void *nam,
335     struct ifnet * ifp)
336 {
337 	return EOPNOTSUPP;
338 }
339 
340 static int
341 COMPATNAME(route_stat)(struct socket *so, struct stat *ub)
342 {
343 	KASSERT(solocked(so));
344 
345 	return 0;
346 }
347 
348 static int
349 COMPATNAME(route_peeraddr)(struct socket *so, struct sockaddr *nam)
350 {
351 	struct rawcb *rp = sotorawcb(so);
352 
353 	KASSERT(solocked(so));
354 	KASSERT(rp != NULL);
355 	KASSERT(nam != NULL);
356 
357 	if (rp->rcb_faddr == NULL)
358 		return ENOTCONN;
359 
360 	raw_setpeeraddr(rp, nam);
361 	return 0;
362 }
363 
364 static int
365 COMPATNAME(route_sockaddr)(struct socket *so, struct sockaddr *nam)
366 {
367 	struct rawcb *rp = sotorawcb(so);
368 
369 	KASSERT(solocked(so));
370 	KASSERT(rp != NULL);
371 	KASSERT(nam != NULL);
372 
373 	if (rp->rcb_faddr == NULL)
374 		return ENOTCONN;
375 
376 	raw_setsockaddr(rp, nam);
377 	return 0;
378 }
379 
380 static int
381 COMPATNAME(route_rcvd)(struct socket *so, int flags, struct lwp *l)
382 {
383 	KASSERT(solocked(so));
384 
385 	return EOPNOTSUPP;
386 }
387 
388 static int
389 COMPATNAME(route_recvoob)(struct socket *so, struct mbuf *m, int flags)
390 {
391 	KASSERT(solocked(so));
392 
393 	return EOPNOTSUPP;
394 }
395 
396 static int
397 COMPATNAME(route_send)(struct socket *so, struct mbuf *m,
398     struct sockaddr *nam, struct mbuf *control, struct lwp *l)
399 {
400 	int error = 0;
401 	int s;
402 
403 	KASSERT(solocked(so));
404 	KASSERT(so->so_proto == &COMPATNAME(route_protosw)[0]);
405 
406 	s = splsoftnet();
407 	error = raw_send(so, m, nam, control, l, &COMPATNAME(route_output));
408 	splx(s);
409 
410 	return error;
411 }
412 
413 static int
414 COMPATNAME(route_sendoob)(struct socket *so, struct mbuf *m,
415     struct mbuf *control)
416 {
417 	KASSERT(solocked(so));
418 
419 	m_freem(m);
420 	m_freem(control);
421 
422 	return EOPNOTSUPP;
423 }
424 static int
425 COMPATNAME(route_purgeif)(struct socket *so, struct ifnet *ifp)
426 {
427 
428 	panic("route_purgeif");
429 
430 	return EOPNOTSUPP;
431 }
432 
433 #ifdef INET
434 static int
435 route_get_sdl_index(struct rt_addrinfo *info, int *sdl_index)
436 {
437 	struct rtentry *nrt;
438 	int error;
439 
440 	error = rtrequest1(RTM_GET, info, &nrt);
441 	if (error != 0)
442 		return error;
443 	/*
444 	 * nrt->rt_ifp->if_index may not be correct
445 	 * due to changing to ifplo0.
446 	 */
447 	*sdl_index = satosdl(nrt->rt_gateway)->sdl_index;
448 	rt_unref(nrt);
449 
450 	return 0;
451 }
452 #endif /* INET */
453 
454 static void
455 route_get_sdl(const struct ifnet *ifp, const struct sockaddr *dst,
456     struct sockaddr_dl *sdl, int *flags)
457 {
458 	struct llentry *la;
459 
460 	KASSERT(ifp != NULL);
461 
462 	IF_AFDATA_RLOCK(ifp);
463 	switch (dst->sa_family) {
464 	case AF_INET:
465 		la = lla_lookup(LLTABLE(ifp), 0, dst);
466 		break;
467 	case AF_INET6:
468 		la = lla_lookup(LLTABLE6(ifp), 0, dst);
469 		break;
470 	default:
471 		la = NULL;
472 		KASSERTMSG(0, "Invalid AF=%d\n", dst->sa_family);
473 		break;
474 	}
475 	IF_AFDATA_RUNLOCK(ifp);
476 
477 	void *a = (LLE_IS_VALID(la) && (la->la_flags & LLE_VALID) == LLE_VALID)
478 	    ? &la->ll_addr : NULL;
479 
480 	a = sockaddr_dl_init(sdl, sizeof(*sdl), ifp->if_index, ifp->if_type,
481 	    NULL, 0, a, ifp->if_addrlen);
482 	KASSERT(a != NULL);
483 
484 	if (la != NULL) {
485 		*flags = la->la_flags;
486 		LLE_RUNLOCK(la);
487 	}
488 }
489 
490 static int
491 route_output_report(struct rtentry *rt, struct rt_addrinfo *info,
492     struct rt_xmsghdr *rtm, struct rt_xmsghdr **new_rtm)
493 {
494 	int len;
495 	struct ifnet *ifp;
496 
497 	if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
498 		;
499 	else if ((ifp = rt->rt_ifp) != NULL) {
500 		const struct ifaddr *rtifa;
501 		info->rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
502 		/* rtifa used to be simply rt->rt_ifa.
503 		 * If rt->rt_ifa != NULL, then
504 		 * rt_get_ifa() != NULL.  So this
505 		 * ought to still be safe. --dyoung
506 		 */
507 		rtifa = rt_get_ifa(rt);
508 		info->rti_info[RTAX_IFA] = rtifa->ifa_addr;
509 #ifdef RTSOCK_DEBUG
510 		if (info->rti_info[RTAX_IFA]->sa_family == AF_INET) {
511 			char ibuf[INET_ADDRSTRLEN];
512 			char abuf[INET_ADDRSTRLEN];
513 			printf("%s: copying out RTAX_IFA %s "
514 			    "for info->rti_info[RTAX_DST] %s "
515 			    "ifa_getifa %p ifa_seqno %p\n",
516 			    __func__,
517 			    RT_IN_PRINT(info, ibuf, RTAX_IFA),
518 			    RT_IN_PRINT(info, abuf, RTAX_DST),
519 			    (void *)rtifa->ifa_getifa,
520 			    rtifa->ifa_seqno);
521 		}
522 #endif /* RTSOCK_DEBUG */
523 		if (ifp->if_flags & IFF_POINTOPOINT)
524 			info->rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
525 		else
526 			info->rti_info[RTAX_BRD] = NULL;
527 		rtm->rtm_index = ifp->if_index;
528 	} else {
529 		info->rti_info[RTAX_IFP] = NULL;
530 		info->rti_info[RTAX_IFA] = NULL;
531 	}
532 	(void)rt_msg2(rtm->rtm_type, info, NULL, NULL, &len);
533 	if (len > rtm->rtm_msglen) {
534 		struct rt_xmsghdr *old_rtm = rtm;
535 		R_Malloc(*new_rtm, struct rt_xmsghdr *, len);
536 		if (*new_rtm == NULL)
537 			return ENOBUFS;
538 		(void)memcpy(*new_rtm, old_rtm, old_rtm->rtm_msglen);
539 		rtm = *new_rtm;
540 	}
541 	(void)rt_msg2(rtm->rtm_type, info, rtm, NULL, 0);
542 	rtm->rtm_flags = rt->rt_flags;
543 	rtm_setmetrics(rt, rtm);
544 	rtm->rtm_addrs = info->rti_addrs;
545 
546 	return 0;
547 }
548 
549 static struct ifaddr *
550 route_output_get_ifa(const struct rt_addrinfo info, const struct rtentry *rt,
551     struct ifnet **ifp, struct psref *psref)
552 {
553 	struct ifaddr *ifa = NULL;
554 
555 	*ifp = NULL;
556 	if (info.rti_info[RTAX_IFP] != NULL) {
557 		ifa = ifa_ifwithnet_psref(info.rti_info[RTAX_IFP], psref);
558 		if (ifa == NULL)
559 			goto next;
560 		*ifp = ifa->ifa_ifp;
561 		if (info.rti_info[RTAX_IFA] == NULL &&
562 		    info.rti_info[RTAX_GATEWAY] == NULL)
563 			goto next;
564 		if (info.rti_info[RTAX_IFA] == NULL) {
565 			/* route change <dst> <gw> -ifp <if> */
566 			ifa = ifaof_ifpforaddr_psref(info.rti_info[RTAX_GATEWAY],
567 			    *ifp, psref);
568 		} else {
569 			/* route change <dst> -ifp <if> -ifa <addr> */
570 			ifa = ifa_ifwithaddr_psref(info.rti_info[RTAX_IFA], psref);
571 			if (ifa != NULL)
572 				goto out;
573 			ifa = ifaof_ifpforaddr_psref(info.rti_info[RTAX_IFA],
574 			    *ifp, psref);
575 		}
576 		goto out;
577 	}
578 next:
579 	if (info.rti_info[RTAX_IFA] != NULL) {
580 		/* route change <dst> <gw> -ifa <addr> */
581 		ifa = ifa_ifwithaddr_psref(info.rti_info[RTAX_IFA], psref);
582 		if (ifa != NULL)
583 			goto out;
584 	}
585 	if (info.rti_info[RTAX_GATEWAY] != NULL) {
586 		/* route change <dst> <gw> */
587 		ifa = ifa_ifwithroute_psref(rt->rt_flags, rt_getkey(rt),
588 		    info.rti_info[RTAX_GATEWAY], psref);
589 	}
590 out:
591 	if (ifa != NULL && *ifp == NULL)
592 		*ifp = ifa->ifa_ifp;
593 	return ifa;
594 }
595 
596 static int
597 route_output_change(struct rtentry *rt, struct rt_addrinfo *info,
598     struct rt_xmsghdr *rtm)
599 {
600 	int error = 0;
601 	struct ifnet *ifp, *new_ifp;
602 	struct ifaddr *ifa, *new_ifa;
603 	struct psref psref_ifa, psref_new_ifa, psref_ifp;
604 
605 	/*
606 	 * new gateway could require new ifaddr, ifp;
607 	 * flags may also be different; ifp may be specified
608 	 * by ll sockaddr when protocol address is ambiguous
609 	 */
610 	ifp = rt_getifp(info, &psref_ifp);
611 	ifa = rt_getifa(info, &psref_ifa);
612 	if (ifa == NULL) {
613 		error = ENETUNREACH;
614 		goto out;
615 	}
616 	if (info->rti_info[RTAX_GATEWAY]) {
617 		error = rt_setgate(rt, info->rti_info[RTAX_GATEWAY]);
618 		if (error != 0)
619 			goto out;
620 	}
621 	if (info->rti_info[RTAX_TAG]) {
622 		const struct sockaddr *tag;
623 		tag = rt_settag(rt, info->rti_info[RTAX_TAG]);
624 		if (tag == NULL) {
625 			error = ENOBUFS;
626 			goto out;
627 		}
628 	}
629 	/* new gateway could require new ifaddr, ifp;
630 	   flags may also be different; ifp may be specified
631 	   by ll sockaddr when protocol address is ambiguous */
632 	new_ifa = route_output_get_ifa(*info, rt, &new_ifp, &psref_new_ifa);
633 	if (new_ifa != NULL) {
634 		ifa_release(ifa, &psref_ifa);
635 		ifa = new_ifa;
636 	}
637 	if (ifa) {
638 		struct ifaddr *oifa = rt->rt_ifa;
639 		if (oifa != ifa &&
640 		    !ifa_is_destroying(ifa) && !if_is_deactivated(new_ifp)) {
641 			if (oifa && oifa->ifa_rtrequest)
642 				oifa->ifa_rtrequest(RTM_DELETE, rt, info);
643 			rt_replace_ifa(rt, ifa);
644 			rt->rt_ifp = new_ifp;
645 		}
646 		if (new_ifa == NULL)
647 			ifa_release(ifa, &psref_ifa);
648 	}
649 	ifa_release(new_ifa, &psref_new_ifa);
650 	if (new_ifp && rt->rt_ifp != new_ifp
651 	    && !if_is_deactivated(new_ifp))
652 		rt->rt_ifp = new_ifp;
653 	rt_setmetrics(rtm->rtm_inits, rtm, rt);
654 	if (rt->rt_flags != info->rti_flags)
655 		rt->rt_flags = (info->rti_flags & ~PRESERVED_RTF)
656 		    | (rt->rt_flags & PRESERVED_RTF);
657 	if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
658 		rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
659 out:
660 	if_put(ifp, &psref_ifp);
661 
662 	return error;
663 }
664 
665 /*ARGSUSED*/
666 int
667 COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
668 {
669 	struct sockproto proto = { .sp_family = PF_XROUTE, };
670 	struct rt_xmsghdr *rtm = NULL;
671 	struct rt_xmsghdr *old_rtm = NULL, *new_rtm = NULL;
672 	struct rtentry *rt = NULL;
673 	struct rtentry *saved_nrt = NULL;
674 	struct rt_addrinfo info;
675 	int len, error = 0;
676 	sa_family_t family;
677 	struct sockaddr_dl sdl;
678 	int bound = curlwp_bind();
679 	bool do_rt_free = false;
680 
681 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
682 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
683 	   (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
684 		error = ENOBUFS;
685 		goto out;
686 	}
687 	if ((m->m_flags & M_PKTHDR) == 0)
688 		panic("%s", __func__);
689 	len = m->m_pkthdr.len;
690 	if (len < sizeof(*rtm) ||
691 	    len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
692 		info.rti_info[RTAX_DST] = NULL;
693 		senderr(EINVAL);
694 	}
695 	R_Malloc(rtm, struct rt_xmsghdr *, len);
696 	if (rtm == NULL) {
697 		info.rti_info[RTAX_DST] = NULL;
698 		senderr(ENOBUFS);
699 	}
700 	m_copydata(m, 0, len, rtm);
701 	if (rtm->rtm_version != RTM_XVERSION) {
702 		info.rti_info[RTAX_DST] = NULL;
703 		senderr(EPROTONOSUPPORT);
704 	}
705 	rtm->rtm_pid = curproc->p_pid;
706 	memset(&info, 0, sizeof(info));
707 	info.rti_addrs = rtm->rtm_addrs;
708 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
709 	    &info)) {
710 		senderr(EINVAL);
711 	}
712 	info.rti_flags = rtm->rtm_flags;
713 #ifdef RTSOCK_DEBUG
714 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
715 		char abuf[INET_ADDRSTRLEN];
716 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
717 		    RT_IN_PRINT(&info, abuf, RTAX_DST));
718 	}
719 #endif /* RTSOCK_DEBUG */
720 	if (info.rti_info[RTAX_DST] == NULL ||
721 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
722 		senderr(EINVAL);
723 	}
724 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
725 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
726 		senderr(EINVAL);
727 	}
728 
729 	/*
730 	 * Verify that the caller has the appropriate privilege; RTM_GET
731 	 * is the only operation the non-superuser is allowed.
732 	 */
733 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
734 	    0, rtm, NULL, NULL) != 0)
735 		senderr(EACCES);
736 
737 	switch (rtm->rtm_type) {
738 
739 	case RTM_ADD:
740 		if (info.rti_info[RTAX_GATEWAY] == NULL) {
741 			senderr(EINVAL);
742 		}
743 #ifdef INET
744 		/* support for new ARP code with keeping backcompat */
745 		if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
746 			const struct sockaddr_dl *sdlp =
747 			    satocsdl(info.rti_info[RTAX_GATEWAY]);
748 
749 			/* Allow routing requests by interface index */
750 			if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
751 			    && sdlp->sdl_slen == 0)
752 				goto fallback;
753 			/*
754 			 * Old arp binaries don't set the sdl_index
755 			 * so we have to complement it.
756 			 */
757 			int sdl_index = sdlp->sdl_index;
758 			if (sdl_index == 0) {
759 				error = route_get_sdl_index(&info, &sdl_index);
760 				if (error != 0)
761 					goto fallback;
762 			} else if (
763 			    info.rti_info[RTAX_DST]->sa_family == AF_INET) {
764 				/*
765 				 * XXX workaround for SIN_PROXY case; proxy arp
766 				 * entry should be in an interface that has
767 				 * a network route including the destination,
768 				 * not a local (link) route that may not be a
769 				 * desired place, for example a tap.
770 				 */
771 				const struct sockaddr_inarp *sina =
772 				    (const struct sockaddr_inarp *)
773 				    info.rti_info[RTAX_DST];
774 				if (sina->sin_other & SIN_PROXY) {
775 					error = route_get_sdl_index(&info,
776 					    &sdl_index);
777 					if (error != 0)
778 						goto fallback;
779 				}
780 			}
781 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
782 			    rtm->rtm_rmx.rmx_expire, &info, sdl_index);
783 			break;
784 		}
785 	fallback:
786 #endif /* INET */
787 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
788 		if (error == 0) {
789 			rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
790 			rt_unref(saved_nrt);
791 		}
792 		break;
793 
794 	case RTM_DELETE:
795 #ifdef INET
796 		/* support for new ARP code */
797 		if (info.rti_info[RTAX_GATEWAY] &&
798 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
799 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
800 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
801 			    rtm->rtm_rmx.rmx_expire, &info, 0);
802 			break;
803 		}
804 #endif /* INET */
805 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
806 		if (error != 0)
807 			break;
808 
809 		rt = saved_nrt;
810 		do_rt_free = true;
811 		info.rti_info[RTAX_DST] = rt_getkey(rt);
812 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
813 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
814 		info.rti_info[RTAX_TAG] = rt_gettag(rt);
815 		error = route_output_report(rt, &info, rtm, &new_rtm);
816 		if (error)
817 			senderr(error);
818 		if (new_rtm != NULL) {
819 			old_rtm = rtm;
820 			rtm = new_rtm;
821 		}
822 		break;
823 
824 	case RTM_GET:
825 	case RTM_CHANGE:
826 	case RTM_LOCK:
827                 /* XXX This will mask info.rti_info[RTAX_DST] with
828 		 * info.rti_info[RTAX_NETMASK] before
829                  * searching.  It did not used to do that.  --dyoung
830 		 */
831 		rt = NULL;
832 		error = rtrequest1(RTM_GET, &info, &rt);
833 		if (error != 0)
834 			senderr(error);
835 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
836 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
837 			    info.rti_info[RTAX_DST]->sa_len) != 0)
838 				senderr(ESRCH);
839 			if (info.rti_info[RTAX_NETMASK] == NULL &&
840 			    rt_mask(rt) != NULL)
841 				senderr(ETOOMANYREFS);
842 		}
843 
844 		/*
845 		 * XXX if arp/ndp requests an L2 entry, we have to obtain
846 		 * it from lltable while for the route command we have to
847 		 * return a route as it is. How to distinguish them?
848 		 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
849 		 * indicates an L2 entry is requested. For old arp/ndp
850 		 * binaries, we check RTF_UP flag is NOT set; it works
851 		 * by the fact that arp/ndp don't set it while the route
852 		 * command sets it.
853 		 */
854 		if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
855 		     (rtm->rtm_flags & RTF_UP) == 0) &&
856 		    rtm->rtm_type == RTM_GET &&
857 		    sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
858 			int ll_flags = 0;
859 			route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
860 			    &ll_flags);
861 			info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
862 			error = route_output_report(rt, &info, rtm, &new_rtm);
863 			if (error)
864 				senderr(error);
865 			if (new_rtm != NULL) {
866 				old_rtm = rtm;
867 				rtm = new_rtm;
868 			}
869 			rtm->rtm_flags |= RTF_LLDATA;
870 			rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
871 			break;
872 		}
873 
874 		switch (rtm->rtm_type) {
875 		case RTM_GET:
876 			info.rti_info[RTAX_DST] = rt_getkey(rt);
877 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
878 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
879 			info.rti_info[RTAX_TAG] = rt_gettag(rt);
880 			error = route_output_report(rt, &info, rtm, &new_rtm);
881 			if (error)
882 				senderr(error);
883 			if (new_rtm != NULL) {
884 				old_rtm = rtm;
885 				rtm = new_rtm;
886 			}
887 			break;
888 
889 		case RTM_CHANGE:
890 			error = rt_update_prepare(rt);
891 			if (error == 0) {
892 				error = route_output_change(rt, &info, rtm);
893 				rt_update_finish(rt);
894 			}
895 			if (error != 0)
896 				goto flush;
897 			/*FALLTHROUGH*/
898 		case RTM_LOCK:
899 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
900 			rt->rt_rmx.rmx_locks |=
901 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
902 			break;
903 		}
904 		break;
905 
906 	default:
907 		senderr(EOPNOTSUPP);
908 	}
909 
910 flush:
911 	if (rtm) {
912 		if (error)
913 			rtm->rtm_errno = error;
914 		else
915 			rtm->rtm_flags |= RTF_DONE;
916 	}
917 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
918 	    0;
919 	/* We cannot free old_rtm until we have stopped using the
920 	 * pointers in info, some of which may point to sockaddrs
921 	 * in old_rtm.
922 	 */
923 	if (old_rtm != NULL)
924 		Free(old_rtm);
925 	if (rt) {
926 		if (do_rt_free)
927 			rt_free(rt);
928 		else
929 			rt_unref(rt);
930 	}
931     {
932 	struct rawcb *rp = NULL;
933 	/*
934 	 * Check to see if we don't want our own messages.
935 	 */
936 	if ((so->so_options & SO_USELOOPBACK) == 0) {
937 		if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
938 			if (rtm)
939 				Free(rtm);
940 			m_freem(m);
941 			goto out;
942 		}
943 		/* There is another listener, so construct message */
944 		rp = sotorawcb(so);
945 	}
946 	if (rtm) {
947 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
948 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
949 			m_freem(m);
950 			m = NULL;
951 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
952 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
953 		Free(rtm);
954 	}
955 	if (rp)
956 		rp->rcb_proto.sp_family = 0; /* Avoid us */
957 	if (family)
958 		proto.sp_protocol = family;
959 	if (m)
960 		raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
961 		    &COMPATNAME(route_info).ri_dst);
962 	if (rp)
963 		rp->rcb_proto.sp_family = PF_XROUTE;
964     }
965 out:
966 	curlwp_bindx(bound);
967 	return error;
968 }
969 
970 static void
971 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
972 {
973 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
974 	metric(RTV_RPIPE, rmx_recvpipe);
975 	metric(RTV_SPIPE, rmx_sendpipe);
976 	metric(RTV_SSTHRESH, rmx_ssthresh);
977 	metric(RTV_RTT, rmx_rtt);
978 	metric(RTV_RTTVAR, rmx_rttvar);
979 	metric(RTV_HOPCOUNT, rmx_hopcount);
980 	metric(RTV_MTU, rmx_mtu);
981 #undef metric
982 	if (which & RTV_EXPIRE) {
983 		out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
984 		    time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
985 	}
986 }
987 
988 static void
989 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
990 {
991 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
992 	metric(rmx_recvpipe);
993 	metric(rmx_sendpipe);
994 	metric(rmx_ssthresh);
995 	metric(rmx_rtt);
996 	metric(rmx_rttvar);
997 	metric(rmx_hopcount);
998 	metric(rmx_mtu);
999 #undef metric
1000 	out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
1001 	    time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
1002 }
1003 
1004 static int
1005 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
1006     struct rt_addrinfo *rtinfo)
1007 {
1008 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
1009 	int i;
1010 
1011 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1012 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
1013 			continue;
1014 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
1015 		RT_XADVANCE(cp, sa);
1016 	}
1017 
1018 	/*
1019 	 * Check for extra addresses specified, except RTM_GET asking
1020 	 * for interface info.
1021 	 */
1022 	if (rtmtype == RTM_GET) {
1023 		if (((rtinfo->rti_addrs &
1024 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
1025 			return 1;
1026 	} else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
1027 		return 1;
1028 	/* Check for bad data length.  */
1029 	if (cp != cplim) {
1030 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
1031 		    cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
1032 			/*
1033 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
1034 			 * We accept this for now for the sake of old
1035 			 * binaries or third party softwares.
1036 			 */
1037 			;
1038 		else
1039 			return 1;
1040 	}
1041 	return 0;
1042 }
1043 
1044 static int
1045 rt_getlen(int type)
1046 {
1047 #ifndef COMPAT_RTSOCK
1048 	CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
1049 	CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
1050 	CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
1051 	CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
1052 #endif
1053 
1054 	switch (type) {
1055 	case RTM_ODELADDR:
1056 	case RTM_ONEWADDR:
1057 	case RTM_OCHGADDR:
1058 #ifdef COMPAT_70
1059 		return sizeof(struct ifa_msghdr70);
1060 #else
1061 #ifdef DIAGNOSTIC
1062 		printf("RTM_ONEWADDR\n");
1063 #endif
1064 		return -1;
1065 #endif
1066 	case RTM_DELADDR:
1067 	case RTM_NEWADDR:
1068 	case RTM_CHGADDR:
1069 		return sizeof(struct ifa_xmsghdr);
1070 
1071 	case RTM_OOIFINFO:
1072 #ifdef COMPAT_14
1073 		return sizeof(struct if_msghdr14);
1074 #else
1075 #ifdef DIAGNOSTIC
1076 		printf("RTM_OOIFINFO\n");
1077 #endif
1078 		return -1;
1079 #endif
1080 	case RTM_OIFINFO:
1081 #ifdef COMPAT_50
1082 		return sizeof(struct if_msghdr50);
1083 #else
1084 #ifdef DIAGNOSTIC
1085 		printf("RTM_OIFINFO\n");
1086 #endif
1087 		return -1;
1088 #endif
1089 
1090 	case RTM_IFINFO:
1091 		return sizeof(struct if_xmsghdr);
1092 
1093 	case RTM_IFANNOUNCE:
1094 	case RTM_IEEE80211:
1095 		return sizeof(struct if_xannouncemsghdr);
1096 
1097 	default:
1098 		return sizeof(struct rt_xmsghdr);
1099 	}
1100 }
1101 
1102 
1103 struct mbuf *
1104 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
1105 {
1106 	struct rt_xmsghdr *rtm;
1107 	struct mbuf *m;
1108 	int i;
1109 	const struct sockaddr *sa;
1110 	int len, dlen;
1111 
1112 	m = m_gethdr(M_DONTWAIT, MT_DATA);
1113 	if (m == NULL)
1114 		return m;
1115 	MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
1116 
1117 	if ((len = rt_getlen(type)) == -1)
1118 		goto out;
1119 	if (len > MHLEN + MLEN)
1120 		panic("%s: message too long", __func__);
1121 	else if (len > MHLEN) {
1122 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
1123 		if (m->m_next == NULL)
1124 			goto out;
1125 		MCLAIM(m->m_next, m->m_owner);
1126 		m->m_pkthdr.len = len;
1127 		m->m_len = MHLEN;
1128 		m->m_next->m_len = len - MHLEN;
1129 	} else {
1130 		m->m_pkthdr.len = m->m_len = len;
1131 	}
1132 	m_reset_rcvif(m);
1133 	m_copyback(m, 0, datalen, data);
1134 	if (len > datalen)
1135 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1136 	rtm = mtod(m, struct rt_xmsghdr *);
1137 	for (i = 0; i < RTAX_MAX; i++) {
1138 		if ((sa = rtinfo->rti_info[i]) == NULL)
1139 			continue;
1140 		rtinfo->rti_addrs |= (1 << i);
1141 		dlen = RT_XROUNDUP(sa->sa_len);
1142 		m_copyback(m, len, sa->sa_len, sa);
1143 		if (dlen != sa->sa_len) {
1144 			/*
1145 			 * Up to 6 + 1 nul's since roundup is to
1146 			 * sizeof(uint64_t) (8 bytes)
1147 			 */
1148 			m_copyback(m, len + sa->sa_len,
1149 			    dlen - sa->sa_len, "\0\0\0\0\0\0");
1150 		}
1151 		len += dlen;
1152 	}
1153 	if (m->m_pkthdr.len != len)
1154 		goto out;
1155 	rtm->rtm_msglen = len;
1156 	rtm->rtm_version = RTM_XVERSION;
1157 	rtm->rtm_type = type;
1158 	return m;
1159 out:
1160 	m_freem(m);
1161 	return NULL;
1162 }
1163 
1164 /*
1165  * rt_msg2
1166  *
1167  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
1168  *		returns the length of the message in 'lenp'.
1169  *
1170  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1171  *	the message
1172  * otherwise walkarg's w_needed is updated and if the user buffer is
1173  *	specified and w_needed indicates space exists the information is copied
1174  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1175  *	if the allocation fails ENOBUFS is returned.
1176  */
1177 static int
1178 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1179 	int *lenp)
1180 {
1181 	int i;
1182 	int len, dlen, second_time = 0;
1183 	char *cp0, *cp = cpv;
1184 
1185 	rtinfo->rti_addrs = 0;
1186 again:
1187 	if ((len = rt_getlen(type)) == -1)
1188 		return EINVAL;
1189 
1190 	if ((cp0 = cp) != NULL)
1191 		cp += len;
1192 	for (i = 0; i < RTAX_MAX; i++) {
1193 		const struct sockaddr *sa;
1194 
1195 		if ((sa = rtinfo->rti_info[i]) == NULL)
1196 			continue;
1197 		rtinfo->rti_addrs |= (1 << i);
1198 		dlen = RT_XROUNDUP(sa->sa_len);
1199 		if (cp) {
1200 			int diff = dlen - sa->sa_len;
1201 			(void)memcpy(cp, sa, (size_t)sa->sa_len);
1202 			cp += sa->sa_len;
1203 			if (diff > 0) {
1204 				(void)memset(cp, 0, (size_t)diff);
1205 				cp += diff;
1206 			}
1207 		}
1208 		len += dlen;
1209 	}
1210 	if (cp == NULL && w != NULL && !second_time) {
1211 		struct rt_walkarg *rw = w;
1212 
1213 		rw->w_needed += len;
1214 		if (rw->w_needed <= 0 && rw->w_where) {
1215 			if (rw->w_tmemsize < len) {
1216 				if (rw->w_tmem)
1217 					free(rw->w_tmem, M_RTABLE);
1218 				rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
1219 				if (rw->w_tmem)
1220 					rw->w_tmemsize = len;
1221 				else
1222 					rw->w_tmemsize = 0;
1223 			}
1224 			if (rw->w_tmem) {
1225 				cp = rw->w_tmem;
1226 				second_time = 1;
1227 				goto again;
1228 			} else {
1229 				rw->w_tmemneeded = len;
1230 				return ENOBUFS;
1231 			}
1232 		}
1233 	}
1234 	if (cp) {
1235 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1236 
1237 		rtm->rtm_version = RTM_XVERSION;
1238 		rtm->rtm_type = type;
1239 		rtm->rtm_msglen = len;
1240 	}
1241 	if (lenp)
1242 		*lenp = len;
1243 	return 0;
1244 }
1245 
1246 #ifndef COMPAT_RTSOCK
1247 int
1248 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1249 	int *lenp)
1250 {
1251 	return rt_msg2(type, rtinfo, cpv, w, lenp);
1252 }
1253 #endif
1254 
1255 /*
1256  * This routine is called to generate a message from the routing
1257  * socket indicating that a redirect has occurred, a routing lookup
1258  * has failed, or that a protocol has detected timeouts to a particular
1259  * destination.
1260  */
1261 void
1262 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1263     int error)
1264 {
1265 	struct rt_xmsghdr rtm;
1266 	struct mbuf *m;
1267 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1268 	struct rt_addrinfo info = *rtinfo;
1269 
1270 	COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1271 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1272 		return;
1273 	memset(&rtm, 0, sizeof(rtm));
1274 	rtm.rtm_pid = curproc->p_pid;
1275 	rtm.rtm_flags = RTF_DONE | flags;
1276 	rtm.rtm_errno = error;
1277 	m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1278 	if (m == NULL)
1279 		return;
1280 	mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1281 	COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1282 }
1283 
1284 /*
1285  * This routine is called to generate a message from the routing
1286  * socket indicating that the status of a network interface has changed.
1287  */
1288 void
1289 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1290 {
1291 	struct if_xmsghdr ifm;
1292 	struct mbuf *m;
1293 	struct rt_addrinfo info;
1294 
1295 	COMPATCALL(rt_ifmsg, (ifp));
1296 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1297 		return;
1298 	(void)memset(&info, 0, sizeof(info));
1299 	(void)memset(&ifm, 0, sizeof(ifm));
1300 	ifm.ifm_index = ifp->if_index;
1301 	ifm.ifm_flags = ifp->if_flags;
1302 	ifm.ifm_data = ifp->if_data;
1303 	ifm.ifm_addrs = 0;
1304 	m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1305 	if (m == NULL)
1306 		return;
1307 	COMPATNAME(route_enqueue)(m, 0);
1308 #ifdef COMPAT_14
1309 	compat_14_rt_oifmsg(ifp);
1310 #endif
1311 #ifdef COMPAT_50
1312 	compat_50_rt_oifmsg(ifp);
1313 #endif
1314 }
1315 
1316 #ifndef COMPAT_RTSOCK
1317 static int
1318 if_addrflags(struct ifaddr *ifa)
1319 {
1320 
1321 	switch (ifa->ifa_addr->sa_family) {
1322 #ifdef INET
1323 	case AF_INET:
1324 		return ((struct in_ifaddr *)ifa)->ia4_flags;
1325 #endif
1326 #ifdef INET6
1327 	case AF_INET6:
1328 		return ((struct in6_ifaddr *)ifa)->ia6_flags;
1329 #endif
1330 	default:
1331 		return 0;
1332 	}
1333 }
1334 #endif
1335 
1336 /*
1337  * This is called to generate messages from the routing socket
1338  * indicating a network interface has had addresses associated with it.
1339  * if we ever reverse the logic and replace messages TO the routing
1340  * socket indicate a request to configure interfaces, then it will
1341  * be unnecessary as the routing socket will automatically generate
1342  * copies of it.
1343  */
1344 void
1345 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
1346     struct rtentry *rt)
1347 {
1348 #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
1349 	struct rt_addrinfo info;
1350 	const struct sockaddr *sa;
1351 	int pass;
1352 	struct mbuf *m;
1353 	struct ifnet *ifp;
1354 	struct rt_xmsghdr rtm;
1355 	struct ifa_xmsghdr ifam;
1356 	int ncmd;
1357 
1358 	KASSERT(ifa != NULL);
1359 	KASSERT(ifa->ifa_addr != NULL);
1360 	ifp = ifa->ifa_ifp;
1361 #ifdef SCTP
1362 	if (cmd == RTM_ADD) {
1363 		sctp_add_ip_address(ifa);
1364 	} else if (cmd == RTM_DELETE) {
1365 		sctp_delete_ip_address(ifa);
1366 	}
1367 #endif
1368 
1369 	COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
1370 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1371 		return;
1372 	for (pass = 1; pass < 3; pass++) {
1373 		memset(&info, 0, sizeof(info));
1374 		switch (cmdpass(cmd, pass)) {
1375 		case cmdpass(RTM_ADD, 1):
1376 		case cmdpass(RTM_CHANGE, 1):
1377 		case cmdpass(RTM_DELETE, 2):
1378 		case cmdpass(RTM_NEWADDR, 1):
1379 		case cmdpass(RTM_DELADDR, 1):
1380 		case cmdpass(RTM_CHGADDR, 1):
1381 			switch (cmd) {
1382 			case RTM_ADD:
1383 				ncmd = RTM_XNEWADDR;
1384 				break;
1385 			case RTM_DELETE:
1386 				ncmd = RTM_XDELADDR;
1387 				break;
1388 			case RTM_CHANGE:
1389 				ncmd = RTM_XCHGADDR;
1390 				break;
1391 			case RTM_NEWADDR:
1392 				ncmd = RTM_XNEWADDR;
1393 				break;
1394 			case RTM_DELADDR:
1395 				ncmd = RTM_XDELADDR;
1396 				break;
1397 			case RTM_CHGADDR:
1398 				ncmd = RTM_XCHGADDR;
1399 				break;
1400 			default:
1401 				panic("%s: unknown command %d", __func__, cmd);
1402 			}
1403 #ifdef COMPAT_70
1404 			compat_70_rt_newaddrmsg1(ncmd, ifa);
1405 #endif
1406 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1407 			KASSERT(ifp->if_dl != NULL);
1408 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1409 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1410 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1411 			memset(&ifam, 0, sizeof(ifam));
1412 			ifam.ifam_index = ifp->if_index;
1413 			ifam.ifam_metric = ifa->ifa_metric;
1414 			ifam.ifam_flags = ifa->ifa_flags;
1415 #ifndef COMPAT_RTSOCK
1416 			ifam.ifam_pid = curproc->p_pid;
1417 			ifam.ifam_addrflags = if_addrflags(ifa);
1418 #endif
1419 			m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1420 			if (m == NULL)
1421 				continue;
1422 			mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1423 			    info.rti_addrs;
1424 			break;
1425 		case cmdpass(RTM_ADD, 2):
1426 		case cmdpass(RTM_CHANGE, 2):
1427 		case cmdpass(RTM_DELETE, 1):
1428 			if (rt == NULL)
1429 				continue;
1430 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1431 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1432 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1433 			memset(&rtm, 0, sizeof(rtm));
1434 			rtm.rtm_pid = curproc->p_pid;
1435 			rtm.rtm_index = ifp->if_index;
1436 			rtm.rtm_flags |= rt->rt_flags;
1437 			rtm.rtm_errno = error;
1438 			m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1439 			if (m == NULL)
1440 				continue;
1441 			mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1442 			break;
1443 		default:
1444 			continue;
1445 		}
1446 #ifdef DIAGNOSTIC
1447 		if (m == NULL)
1448 			panic("%s: called with wrong command", __func__);
1449 #endif
1450 		COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1451 	}
1452 #undef cmdpass
1453 
1454 }
1455 
1456 static struct mbuf *
1457 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1458     struct rt_addrinfo *info)
1459 {
1460 	struct if_xannouncemsghdr ifan;
1461 
1462 	memset(info, 0, sizeof(*info));
1463 	memset(&ifan, 0, sizeof(ifan));
1464 	ifan.ifan_index = ifp->if_index;
1465 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1466 	ifan.ifan_what = what;
1467 	return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1468 }
1469 
1470 /*
1471  * This is called to generate routing socket messages indicating
1472  * network interface arrival and departure.
1473  */
1474 void
1475 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1476 {
1477 	struct mbuf *m;
1478 	struct rt_addrinfo info;
1479 
1480 	COMPATCALL(rt_ifannouncemsg, (ifp, what));
1481 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1482 		return;
1483 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1484 	if (m == NULL)
1485 		return;
1486 	COMPATNAME(route_enqueue)(m, 0);
1487 }
1488 
1489 /*
1490  * This is called to generate routing socket messages indicating
1491  * IEEE80211 wireless events.
1492  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1493  */
1494 void
1495 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1496 	size_t data_len)
1497 {
1498 	struct mbuf *m;
1499 	struct rt_addrinfo info;
1500 
1501 	COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1502 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
1503 		return;
1504 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1505 	if (m == NULL)
1506 		return;
1507 	/*
1508 	 * Append the ieee80211 data.  Try to stick it in the
1509 	 * mbuf containing the ifannounce msg; otherwise allocate
1510 	 * a new mbuf and append.
1511 	 *
1512 	 * NB: we assume m is a single mbuf.
1513 	 */
1514 	if (data_len > M_TRAILINGSPACE(m)) {
1515 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1516 		if (n == NULL) {
1517 			m_freem(m);
1518 			return;
1519 		}
1520 		(void)memcpy(mtod(n, void *), data, data_len);
1521 		n->m_len = data_len;
1522 		m->m_next = n;
1523 	} else if (data_len > 0) {
1524 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1525 		m->m_len += data_len;
1526 	}
1527 	if (m->m_flags & M_PKTHDR)
1528 		m->m_pkthdr.len += data_len;
1529 	mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1530 	COMPATNAME(route_enqueue)(m, 0);
1531 }
1532 
1533 /*
1534  * This is used in dumping the kernel table via sysctl().
1535  */
1536 static int
1537 sysctl_dumpentry(struct rtentry *rt, void *v)
1538 {
1539 	struct rt_walkarg *w = v;
1540 	int error = 0, size;
1541 	struct rt_addrinfo info;
1542 
1543 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1544 		return 0;
1545 	memset(&info, 0, sizeof(info));
1546 	info.rti_info[RTAX_DST] = rt_getkey(rt);
1547 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1548 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1549 	info.rti_info[RTAX_TAG] = rt_gettag(rt);
1550 	if (rt->rt_ifp) {
1551 		const struct ifaddr *rtifa;
1552 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1553 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
1554 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
1555 		 * --dyoung
1556 		 */
1557 		rtifa = rt_get_ifa(rt);
1558 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1559 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1560 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1561 	}
1562 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1563 		return error;
1564 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1565 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1566 
1567 		rtm->rtm_flags = rt->rt_flags;
1568 		rtm->rtm_use = rt->rt_use;
1569 		rtm_setmetrics(rt, rtm);
1570 		KASSERT(rt->rt_ifp != NULL);
1571 		rtm->rtm_index = rt->rt_ifp->if_index;
1572 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1573 		rtm->rtm_addrs = info.rti_addrs;
1574 		if ((error = copyout(rtm, w->w_where, size)) != 0)
1575 			w->w_where = NULL;
1576 		else
1577 			w->w_where = (char *)w->w_where + size;
1578 	}
1579 	return error;
1580 }
1581 
1582 static int
1583 sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
1584     struct rt_addrinfo *info, size_t len)
1585 {
1586 	struct if_xmsghdr *ifm;
1587 	int error;
1588 
1589 	ifm = (struct if_xmsghdr *)w->w_tmem;
1590 	ifm->ifm_index = ifp->if_index;
1591 	ifm->ifm_flags = ifp->if_flags;
1592 	ifm->ifm_data = ifp->if_data;
1593 	ifm->ifm_addrs = info->rti_addrs;
1594 	if ((error = copyout(ifm, w->w_where, len)) == 0)
1595 		w->w_where = (char *)w->w_where + len;
1596 	return error;
1597 }
1598 
1599 static int
1600 sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
1601      struct rt_addrinfo *info)
1602 {
1603 	int len, error;
1604 
1605 	if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
1606 		return error;
1607 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1608 		struct ifa_xmsghdr *ifam;
1609 
1610 		ifam = (struct ifa_xmsghdr *)w->w_tmem;
1611 		ifam->ifam_index = ifa->ifa_ifp->if_index;
1612 		ifam->ifam_flags = ifa->ifa_flags;
1613 		ifam->ifam_metric = ifa->ifa_metric;
1614 		ifam->ifam_addrs = info->rti_addrs;
1615 #ifndef COMPAT_RTSOCK
1616 		ifam->ifam_pid = 0;
1617 		ifam->ifam_addrflags = if_addrflags(ifa);
1618 #endif
1619 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
1620 			w->w_where = (char *)w->w_where + len;
1621 	}
1622 	return error;
1623 }
1624 
1625 static int
1626 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1627 {
1628 	struct ifnet *ifp;
1629 	struct ifaddr *ifa;
1630 	struct	rt_addrinfo info;
1631 	int	cmd, len, error = 0;
1632 	int	(*iflist_if)(struct ifnet *, struct rt_walkarg *,
1633 			     struct rt_addrinfo *, size_t);
1634 	int	(*iflist_addr)(struct rt_walkarg *, struct ifaddr *,
1635 			       struct rt_addrinfo *);
1636 	int s;
1637 	struct psref psref;
1638 	int bound = curlwp_bind();
1639 
1640 	switch (type) {
1641 	case NET_RT_IFLIST:
1642 		cmd = RTM_IFINFO;
1643 		iflist_if = sysctl_iflist_if;
1644 		iflist_addr = sysctl_iflist_addr;
1645 		break;
1646 #ifdef COMPAT_14
1647 	case NET_RT_OOOIFLIST:
1648 		cmd = RTM_OOIFINFO;
1649 		iflist_if = compat_14_iflist;
1650 		iflist_addr = compat_70_iflist_addr;
1651 		break;
1652 #endif
1653 #ifdef COMPAT_50
1654 	case NET_RT_OOIFLIST:
1655 		cmd = RTM_OIFINFO;
1656 		iflist_if = compat_50_iflist;
1657 		iflist_addr = compat_70_iflist_addr;
1658 		break;
1659 #endif
1660 #ifdef COMPAT_70
1661 	case NET_RT_OIFLIST:
1662 		cmd = RTM_IFINFO;
1663 		iflist_if = sysctl_iflist_if;
1664 		iflist_addr = compat_70_iflist_addr;
1665 		break;
1666 #endif
1667 	default:
1668 #ifdef DIAGNOSTIC
1669 		printf("sysctl_iflist\n");
1670 #endif
1671 		return EINVAL;
1672 	}
1673 
1674 	memset(&info, 0, sizeof(info));
1675 
1676 	s = pserialize_read_enter();
1677 	IFNET_READER_FOREACH(ifp) {
1678 		if (w->w_arg && w->w_arg != ifp->if_index)
1679 			continue;
1680 		if (IFADDR_READER_EMPTY(ifp))
1681 			continue;
1682 
1683 		psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
1684 		pserialize_read_exit(s);
1685 
1686 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1687 		if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
1688 			goto release_exit;
1689 		info.rti_info[RTAX_IFP] = NULL;
1690 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1691 			if ((error = iflist_if(ifp, w, &info, len)) != 0)
1692 				goto release_exit;
1693 		}
1694 		IFADDR_READER_FOREACH(ifa, ifp) {
1695 			if (af && af != ifa->ifa_addr->sa_family)
1696 				continue;
1697 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1698 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1699 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1700 			if ((error = iflist_addr(w, ifa, &info)) != 0)
1701 				goto release_exit;
1702 		}
1703 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1704 		    info.rti_info[RTAX_BRD] = NULL;
1705 
1706 		s = pserialize_read_enter();
1707 		psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1708 	}
1709 	pserialize_read_exit(s);
1710 	curlwp_bindx(bound);
1711 
1712 	return 0;
1713 
1714 release_exit:
1715 	psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1716 	curlwp_bindx(bound);
1717 	return error;
1718 }
1719 
1720 static int
1721 sysctl_rtable(SYSCTLFN_ARGS)
1722 {
1723 	void 	*where = oldp;
1724 	size_t	*given = oldlenp;
1725 	int	i, s, error = EINVAL;
1726 	u_char  af;
1727 	struct	rt_walkarg w;
1728 
1729 	if (namelen == 1 && name[0] == CTL_QUERY)
1730 		return sysctl_query(SYSCTLFN_CALL(rnode));
1731 
1732 	if (newp)
1733 		return EPERM;
1734 	if (namelen != 3)
1735 		return EINVAL;
1736 	af = name[0];
1737 	w.w_tmemneeded = 0;
1738 	w.w_tmemsize = 0;
1739 	w.w_tmem = NULL;
1740 again:
1741 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
1742 	if (w.w_tmemneeded) {
1743 		w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1744 		w.w_tmemsize = w.w_tmemneeded;
1745 		w.w_tmemneeded = 0;
1746 	}
1747 	w.w_op = name[1];
1748 	w.w_arg = name[2];
1749 	w.w_given = *given;
1750 	w.w_needed = 0 - w.w_given;
1751 	w.w_where = where;
1752 
1753 	s = splsoftnet();
1754 	switch (w.w_op) {
1755 
1756 	case NET_RT_DUMP:
1757 	case NET_RT_FLAGS:
1758 #ifdef INET
1759 		/*
1760 		 * take care of llinfo entries, the caller must
1761 		 * specify an AF
1762 		 */
1763 		if (w.w_op == NET_RT_FLAGS &&
1764 		    (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
1765 			if (af != 0)
1766 				error = lltable_sysctl_dumparp(af, &w);
1767 			else
1768 				error = EINVAL;
1769 			break;
1770 		}
1771 #endif /* INET */
1772 
1773 		for (i = 1; i <= AF_MAX; i++)
1774 			if ((af == 0 || af == i) &&
1775 			    (error = rt_walktree(i, sysctl_dumpentry, &w)))
1776 				break;
1777 		break;
1778 
1779 #ifdef COMPAT_14
1780 	case NET_RT_OOOIFLIST:
1781 		error = sysctl_iflist(af, &w, w.w_op);
1782 		break;
1783 #endif
1784 #ifdef COMPAT_50
1785 	case NET_RT_OOIFLIST:
1786 		error = sysctl_iflist(af, &w, w.w_op);
1787 		break;
1788 #endif
1789 #ifdef COMPAT_70
1790 	case NET_RT_OIFLIST:
1791 		error = sysctl_iflist(af, &w, w.w_op);
1792 		break;
1793 #endif
1794 	case NET_RT_IFLIST:
1795 		error = sysctl_iflist(af, &w, w.w_op);
1796 		break;
1797 	}
1798 	splx(s);
1799 
1800 	/* check to see if we couldn't allocate memory with NOWAIT */
1801 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1802 		goto again;
1803 
1804 	if (w.w_tmem)
1805 		free(w.w_tmem, M_RTABLE);
1806 	w.w_needed += w.w_given;
1807 	if (where) {
1808 		*given = (char *)w.w_where - (char *)where;
1809 		if (*given < w.w_needed)
1810 			return ENOMEM;
1811 	} else {
1812 		*given = (11 * w.w_needed) / 10;
1813 	}
1814 	return error;
1815 }
1816 
1817 /*
1818  * Routing message software interrupt routine
1819  */
1820 static void
1821 COMPATNAME(route_intr)(void *cookie)
1822 {
1823 	struct sockproto proto = { .sp_family = PF_XROUTE, };
1824 	struct route_info * const ri = &COMPATNAME(route_info);
1825 	struct mbuf *m;
1826 
1827 	mutex_enter(softnet_lock);
1828 	KERNEL_LOCK(1, NULL);
1829 	for (;;) {
1830 		IFQ_LOCK(&ri->ri_intrq);
1831 		IF_DEQUEUE(&ri->ri_intrq, m);
1832 		IFQ_UNLOCK(&ri->ri_intrq);
1833 		if (m == NULL)
1834 			break;
1835 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
1836 		raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
1837 	}
1838 	KERNEL_UNLOCK_ONE(NULL);
1839 	mutex_exit(softnet_lock);
1840 }
1841 
1842 /*
1843  * Enqueue a message to the software interrupt routine.
1844  */
1845 void
1846 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1847 {
1848 	struct route_info * const ri = &COMPATNAME(route_info);
1849 	int wasempty;
1850 
1851 	IFQ_LOCK(&ri->ri_intrq);
1852 	if (IF_QFULL(&ri->ri_intrq)) {
1853 		IF_DROP(&ri->ri_intrq);
1854 		IFQ_UNLOCK(&ri->ri_intrq);
1855 		m_freem(m);
1856 	} else {
1857 		wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1858 		M_SETCTX(m, (uintptr_t)family);
1859 		IF_ENQUEUE(&ri->ri_intrq, m);
1860 		IFQ_UNLOCK(&ri->ri_intrq);
1861 		if (wasempty) {
1862 			kpreempt_disable();
1863 			softint_schedule(ri->ri_sih);
1864 			kpreempt_enable();
1865 		}
1866 	}
1867 }
1868 
1869 static void
1870 COMPATNAME(route_init)(void)
1871 {
1872 	struct route_info * const ri = &COMPATNAME(route_info);
1873 
1874 #ifndef COMPAT_RTSOCK
1875 	rt_init();
1876 #endif
1877 
1878 	sysctl_net_route_setup(NULL);
1879 	ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1880 	ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1881 	    COMPATNAME(route_intr), NULL);
1882 	IFQ_LOCK_INIT(&ri->ri_intrq);
1883 }
1884 
1885 /*
1886  * Definitions of protocols supported in the ROUTE domain.
1887  */
1888 #ifndef COMPAT_RTSOCK
1889 PR_WRAP_USRREQS(route);
1890 #else
1891 PR_WRAP_USRREQS(compat_50_route);
1892 #endif
1893 
1894 static const struct pr_usrreqs route_usrreqs = {
1895 	.pr_attach	= COMPATNAME(route_attach_wrapper),
1896 	.pr_detach	= COMPATNAME(route_detach_wrapper),
1897 	.pr_accept	= COMPATNAME(route_accept_wrapper),
1898 	.pr_bind	= COMPATNAME(route_bind_wrapper),
1899 	.pr_listen	= COMPATNAME(route_listen_wrapper),
1900 	.pr_connect	= COMPATNAME(route_connect_wrapper),
1901 	.pr_connect2	= COMPATNAME(route_connect2_wrapper),
1902 	.pr_disconnect	= COMPATNAME(route_disconnect_wrapper),
1903 	.pr_shutdown	= COMPATNAME(route_shutdown_wrapper),
1904 	.pr_abort	= COMPATNAME(route_abort_wrapper),
1905 	.pr_ioctl	= COMPATNAME(route_ioctl_wrapper),
1906 	.pr_stat	= COMPATNAME(route_stat_wrapper),
1907 	.pr_peeraddr	= COMPATNAME(route_peeraddr_wrapper),
1908 	.pr_sockaddr	= COMPATNAME(route_sockaddr_wrapper),
1909 	.pr_rcvd	= COMPATNAME(route_rcvd_wrapper),
1910 	.pr_recvoob	= COMPATNAME(route_recvoob_wrapper),
1911 	.pr_send	= COMPATNAME(route_send_wrapper),
1912 	.pr_sendoob	= COMPATNAME(route_sendoob_wrapper),
1913 	.pr_purgeif	= COMPATNAME(route_purgeif_wrapper),
1914 };
1915 
1916 static const struct protosw COMPATNAME(route_protosw)[] = {
1917 	{
1918 		.pr_type = SOCK_RAW,
1919 		.pr_domain = &COMPATNAME(routedomain),
1920 		.pr_flags = PR_ATOMIC|PR_ADDR,
1921 		.pr_input = raw_input,
1922 		.pr_ctlinput = raw_ctlinput,
1923 		.pr_usrreqs = &route_usrreqs,
1924 		.pr_init = raw_init,
1925 	},
1926 };
1927 
1928 struct domain COMPATNAME(routedomain) = {
1929 	.dom_family = PF_XROUTE,
1930 	.dom_name = DOMAINNAME,
1931 	.dom_init = COMPATNAME(route_init),
1932 	.dom_protosw = COMPATNAME(route_protosw),
1933 	.dom_protoswNPROTOSW =
1934 	    &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1935 };
1936 
1937 static void
1938 sysctl_net_route_setup(struct sysctllog **clog)
1939 {
1940 	const struct sysctlnode *rnode = NULL;
1941 
1942 	sysctl_createv(clog, 0, NULL, &rnode,
1943 		       CTLFLAG_PERMANENT,
1944 		       CTLTYPE_NODE, DOMAINNAME,
1945 		       SYSCTL_DESCR("PF_ROUTE information"),
1946 		       NULL, 0, NULL, 0,
1947 		       CTL_NET, PF_XROUTE, CTL_EOL);
1948 
1949 	sysctl_createv(clog, 0, NULL, NULL,
1950 		       CTLFLAG_PERMANENT,
1951 		       CTLTYPE_NODE, "rtable",
1952 		       SYSCTL_DESCR("Routing table information"),
1953 		       sysctl_rtable, 0, NULL, 0,
1954 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
1955 
1956 	sysctl_createv(clog, 0, &rnode, NULL,
1957 		       CTLFLAG_PERMANENT,
1958 		       CTLTYPE_STRUCT, "stats",
1959 		       SYSCTL_DESCR("Routing statistics"),
1960 		       NULL, 0, &rtstat, sizeof(rtstat),
1961 		       CTL_CREATE, CTL_EOL);
1962 }
1963