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