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