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