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