Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.238.2.19
      1 /*	$NetBSD: rtsock.c,v 1.238.2.19 2019/01/14 13:34:28 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1988, 1991, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.238.2.19 2019/01/14 13:34:28 pgoyette Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_inet.h"
     68 #include "opt_mpls.h"
     69 #include "opt_compat_netbsd.h"
     70 #include "opt_sctp.h"
     71 #include "opt_net_mpsafe.h"
     72 #endif
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/proc.h>
     77 #include <sys/socket.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/domain.h>
     80 #include <sys/protosw.h>
     81 #include <sys/sysctl.h>
     82 #include <sys/kauth.h>
     83 #include <sys/kmem.h>
     84 #include <sys/intr.h>
     85 #include <sys/condvar.h>
     86 #include <sys/compat_stub.h>
     87 
     88 #include <net/if.h>
     89 #include <net/if_llatbl.h>
     90 #include <net/if_types.h>
     91 #include <net/route.h>
     92 #include <net/raw_cb.h>
     93 
     94 #include <netinet/in_var.h>
     95 #include <netinet/if_inarp.h>
     96 
     97 #include <netmpls/mpls.h>
     98 
     99 #ifdef SCTP
    100 extern void sctp_add_ip_address(struct ifaddr *);
    101 extern void sctp_delete_ip_address(struct ifaddr *);
    102 #endif
    103 
    104 #include <compat/net/if.h>
    105 #include <compat/net/route.h>
    106 
    107 #ifdef COMPAT_RTSOCK
    108 #define	RTM_XVERSION	RTM_OVERSION
    109 #define	RTM_XNEWADDR	RTM_ONEWADDR
    110 #define	RTM_XDELADDR	RTM_ODELADDR
    111 #define	RTM_XCHGADDR	RTM_OCHGADDR
    112 #define	RT_XADVANCE(a,b) RT_OADVANCE(a,b)
    113 #define	RT_XROUNDUP(n)	RT_OROUNDUP(n)
    114 #define	PF_XROUTE	PF_OROUTE
    115 #define	rt_xmsghdr	rt_msghdr50
    116 #define	if_xmsghdr	if_msghdr	/* if_msghdr50 is for RTM_OIFINFO */
    117 #define	ifa_xmsghdr	ifa_msghdr50
    118 #define	if_xannouncemsghdr	if_announcemsghdr50
    119 #define	COMPATNAME(x)	compat_50_ ## x
    120 #define	DOMAINNAME	"oroute"
    121 CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
    122 DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
    123 #else /* COMPAT_RTSOCK */
    124 #define	RTM_XVERSION	RTM_VERSION
    125 #define	RTM_XNEWADDR	RTM_NEWADDR
    126 #define	RTM_XDELADDR	RTM_DELADDR
    127 #define	RTM_XCHGADDR	RTM_CHGADDR
    128 #define	RT_XADVANCE(a,b) RT_ADVANCE(a,b)
    129 #define	RT_XROUNDUP(n)	RT_ROUNDUP(n)
    130 #define	PF_XROUTE	PF_ROUTE
    131 #define	rt_xmsghdr	rt_msghdr
    132 #define	if_xmsghdr	if_msghdr
    133 #define	ifa_xmsghdr	ifa_msghdr
    134 #define	if_xannouncemsghdr	if_announcemsghdr
    135 #define	COMPATNAME(x)	x
    136 #define	DOMAINNAME	"route"
    137 CTASSERT(sizeof(struct ifa_xmsghdr) == 32);
    138 #ifdef COMPAT_50
    139 #define	COMPATCALL(name, args)	rtsock_50_ ## name ## _hook_call args
    140 #endif
    141 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
    142 #undef COMPAT_50
    143 #endif /* COMPAT_RTSOCK */
    144 
    145 #ifndef COMPATCALL
    146 #define	COMPATCALL(name, args)	do { } while (/*CONSTCOND*/ 0)
    147 #endif
    148 
    149 #ifdef RTSOCK_DEBUG
    150 #define RT_IN_PRINT(info, b, a) (in_print((b), sizeof(b), \
    151     &((const struct sockaddr_in *)(info)->rti_info[(a)])->sin_addr), (b))
    152 #endif /* RTSOCK_DEBUG */
    153 
    154 struct route_info COMPATNAME(route_info) = {
    155 	.ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
    156 	.ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
    157 	.ri_maxqlen = IFQ_MAXLEN,
    158 };
    159 
    160 static void COMPATNAME(route_init)(void);
    161 static int COMPATNAME(route_output)(struct mbuf *, struct socket *);
    162 
    163 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
    164 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
    165     struct rt_addrinfo *);
    166 static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
    167 static void _rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
    168 static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
    169 static void rt_adjustcount(int, int);
    170 
    171 #ifndef COMPAT_RTSOCK
    172 static void sysctl_net_route_setup(struct sysctllog **);
    173 #endif
    174 
    175 static const struct protosw COMPATNAME(route_protosw)[];
    176 
    177 struct routecb {
    178 	struct rawcb	rocb_rcb;
    179 	unsigned int	rocb_msgfilter;
    180 #define	RTMSGFILTER(m)	(1U << (m))
    181 };
    182 #define sotoroutecb(so)	((struct routecb *)(so)->so_pcb)
    183 
    184 static struct rawcbhead rt_rawcb;
    185 #ifdef NET_MPSAFE
    186 static kmutex_t *rt_so_mtx;
    187 
    188 static bool rt_updating = false;
    189 static kcondvar_t rt_update_cv;
    190 #endif
    191 
    192 #ifndef COMPAT_RTSOCK
    193 /*
    194  * Compat linkage
    195  */
    196 static int stub_70_rt_newaddrmsg1(int cmd, struct ifaddr *ifa)
    197 {
    198 
    199 	return 0;
    200 }
    201 #endif
    202 
    203 static void
    204 rt_adjustcount(int af, int cnt)
    205 {
    206 	struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
    207 
    208 	cb->any_count += cnt;
    209 
    210 	switch (af) {
    211 	case AF_INET:
    212 		cb->ip_count += cnt;
    213 		return;
    214 #ifdef INET6
    215 	case AF_INET6:
    216 		cb->ip6_count += cnt;
    217 		return;
    218 #endif
    219 	case AF_MPLS:
    220 		cb->mpls_count += cnt;
    221 		return;
    222 	}
    223 }
    224 
    225 static int
    226 COMPATNAME(route_filter)(struct mbuf *m, struct sockproto *proto,
    227     struct rawcb *rp)
    228 {
    229 	struct routecb *rop = (struct routecb *)rp;
    230 	struct rt_xmsghdr *rtm;
    231 
    232 	KASSERT(m != NULL);
    233 	KASSERT(proto != NULL);
    234 	KASSERT(rp != NULL);
    235 
    236 	/* Wrong family for this socket. */
    237 	if (proto->sp_family != PF_ROUTE)
    238 		return ENOPROTOOPT;
    239 
    240 	/* If no filter set, just return. */
    241 	if (rop->rocb_msgfilter == 0)
    242 		return 0;
    243 
    244 	/* Ensure we can access rtm_type */
    245 	if (m->m_len <
    246 	    offsetof(struct rt_xmsghdr, rtm_type) + sizeof(rtm->rtm_type))
    247 		return EINVAL;
    248 
    249 	rtm = mtod(m, struct rt_xmsghdr *);
    250 	/* If the rtm type is filtered out, return a positive. */
    251 	if (!(rop->rocb_msgfilter & RTMSGFILTER(rtm->rtm_type)))
    252 		return EEXIST;
    253 
    254 	/* Passed the filter. */
    255 	return 0;
    256 }
    257 
    258 static void
    259 rt_pr_init(void)
    260 {
    261 
    262 	LIST_INIT(&rt_rawcb);
    263 }
    264 
    265 static int
    266 COMPATNAME(route_attach)(struct socket *so, int proto)
    267 {
    268 	struct rawcb *rp;
    269 	struct routecb *rop;
    270 	int s, error;
    271 
    272 	KASSERT(sotorawcb(so) == NULL);
    273 	rop = kmem_zalloc(sizeof(*rop), KM_SLEEP);
    274 	rp = &rop->rocb_rcb;
    275 	rp->rcb_len = sizeof(*rop);
    276 	so->so_pcb = rp;
    277 
    278 	s = splsoftnet();
    279 
    280 #ifdef NET_MPSAFE
    281 	KASSERT(so->so_lock == NULL);
    282 	mutex_obj_hold(rt_so_mtx);
    283 	so->so_lock = rt_so_mtx;
    284 	solock(so);
    285 #endif
    286 
    287 	if ((error = raw_attach(so, proto, &rt_rawcb)) == 0) {
    288 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    289 		rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
    290 		rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
    291 		rp->rcb_filter = COMPATNAME(route_filter);
    292 	}
    293 	splx(s);
    294 
    295 	if (error) {
    296 		kmem_free(rop, sizeof(*rop));
    297 		so->so_pcb = NULL;
    298 		return error;
    299 	}
    300 
    301 	soisconnected(so);
    302 	so->so_options |= SO_USELOOPBACK;
    303 	KASSERT(solocked(so));
    304 
    305 	return error;
    306 }
    307 
    308 static void
    309 COMPATNAME(route_detach)(struct socket *so)
    310 {
    311 	struct rawcb *rp = sotorawcb(so);
    312 	int s;
    313 
    314 	KASSERT(rp != NULL);
    315 	KASSERT(solocked(so));
    316 
    317 	s = splsoftnet();
    318 	rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    319 	raw_detach(so);
    320 	splx(s);
    321 }
    322 
    323 static int
    324 COMPATNAME(route_accept)(struct socket *so, struct sockaddr *nam)
    325 {
    326 	KASSERT(solocked(so));
    327 
    328 	panic("route_accept");
    329 
    330 	return EOPNOTSUPP;
    331 }
    332 
    333 static int
    334 COMPATNAME(route_bind)(struct socket *so, struct sockaddr *nam, struct lwp *l)
    335 {
    336 	KASSERT(solocked(so));
    337 
    338 	return EOPNOTSUPP;
    339 }
    340 
    341 static int
    342 COMPATNAME(route_listen)(struct socket *so, struct lwp *l)
    343 {
    344 	KASSERT(solocked(so));
    345 
    346 	return EOPNOTSUPP;
    347 }
    348 
    349 static int
    350 COMPATNAME(route_connect)(struct socket *so, struct sockaddr *nam, struct lwp *l)
    351 {
    352 	KASSERT(solocked(so));
    353 
    354 	return EOPNOTSUPP;
    355 }
    356 
    357 static int
    358 COMPATNAME(route_connect2)(struct socket *so, struct socket *so2)
    359 {
    360 	KASSERT(solocked(so));
    361 
    362 	return EOPNOTSUPP;
    363 }
    364 
    365 static int
    366 COMPATNAME(route_disconnect)(struct socket *so)
    367 {
    368 	struct rawcb *rp = sotorawcb(so);
    369 	int s;
    370 
    371 	KASSERT(solocked(so));
    372 	KASSERT(rp != NULL);
    373 
    374 	s = splsoftnet();
    375 	soisdisconnected(so);
    376 	raw_disconnect(rp);
    377 	splx(s);
    378 
    379 	return 0;
    380 }
    381 
    382 static int
    383 COMPATNAME(route_shutdown)(struct socket *so)
    384 {
    385 	int s;
    386 
    387 	KASSERT(solocked(so));
    388 
    389 	/*
    390 	 * Mark the connection as being incapable of further input.
    391 	 */
    392 	s = splsoftnet();
    393 	socantsendmore(so);
    394 	splx(s);
    395 	return 0;
    396 }
    397 
    398 static int
    399 COMPATNAME(route_abort)(struct socket *so)
    400 {
    401 	KASSERT(solocked(so));
    402 
    403 	panic("route_abort");
    404 
    405 	return EOPNOTSUPP;
    406 }
    407 
    408 static int
    409 COMPATNAME(route_ioctl)(struct socket *so, u_long cmd, void *nam,
    410     struct ifnet * ifp)
    411 {
    412 	return EOPNOTSUPP;
    413 }
    414 
    415 static int
    416 COMPATNAME(route_stat)(struct socket *so, struct stat *ub)
    417 {
    418 	KASSERT(solocked(so));
    419 
    420 	return 0;
    421 }
    422 
    423 static int
    424 COMPATNAME(route_peeraddr)(struct socket *so, struct sockaddr *nam)
    425 {
    426 	struct rawcb *rp = sotorawcb(so);
    427 
    428 	KASSERT(solocked(so));
    429 	KASSERT(rp != NULL);
    430 	KASSERT(nam != NULL);
    431 
    432 	if (rp->rcb_faddr == NULL)
    433 		return ENOTCONN;
    434 
    435 	raw_setpeeraddr(rp, nam);
    436 	return 0;
    437 }
    438 
    439 static int
    440 COMPATNAME(route_sockaddr)(struct socket *so, struct sockaddr *nam)
    441 {
    442 	struct rawcb *rp = sotorawcb(so);
    443 
    444 	KASSERT(solocked(so));
    445 	KASSERT(rp != NULL);
    446 	KASSERT(nam != NULL);
    447 
    448 	if (rp->rcb_faddr == NULL)
    449 		return ENOTCONN;
    450 
    451 	raw_setsockaddr(rp, nam);
    452 	return 0;
    453 }
    454 
    455 static int
    456 COMPATNAME(route_rcvd)(struct socket *so, int flags, struct lwp *l)
    457 {
    458 	KASSERT(solocked(so));
    459 
    460 	return EOPNOTSUPP;
    461 }
    462 
    463 static int
    464 COMPATNAME(route_recvoob)(struct socket *so, struct mbuf *m, int flags)
    465 {
    466 	KASSERT(solocked(so));
    467 
    468 	return EOPNOTSUPP;
    469 }
    470 
    471 static int
    472 COMPATNAME(route_send)(struct socket *so, struct mbuf *m,
    473     struct sockaddr *nam, struct mbuf *control, struct lwp *l)
    474 {
    475 	int error = 0;
    476 	int s;
    477 
    478 	KASSERT(solocked(so));
    479 	KASSERT(so->so_proto == &COMPATNAME(route_protosw)[0]);
    480 
    481 	s = splsoftnet();
    482 	error = raw_send(so, m, nam, control, l, &COMPATNAME(route_output));
    483 	splx(s);
    484 
    485 	return error;
    486 }
    487 
    488 static int
    489 COMPATNAME(route_sendoob)(struct socket *so, struct mbuf *m,
    490     struct mbuf *control)
    491 {
    492 	KASSERT(solocked(so));
    493 
    494 	m_freem(m);
    495 	m_freem(control);
    496 
    497 	return EOPNOTSUPP;
    498 }
    499 static int
    500 COMPATNAME(route_purgeif)(struct socket *so, struct ifnet *ifp)
    501 {
    502 
    503 	panic("route_purgeif");
    504 
    505 	return EOPNOTSUPP;
    506 }
    507 
    508 #if defined(INET) || defined(INET6)
    509 static int
    510 route_get_sdl_index(struct rt_addrinfo *info, int *sdl_index)
    511 {
    512 	struct rtentry *nrt;
    513 	int error;
    514 
    515 	error = rtrequest1(RTM_GET, info, &nrt);
    516 	if (error != 0)
    517 		return error;
    518 	/*
    519 	 * nrt->rt_ifp->if_index may not be correct
    520 	 * due to changing to ifplo0.
    521 	 */
    522 	*sdl_index = satosdl(nrt->rt_gateway)->sdl_index;
    523 	rt_unref(nrt);
    524 
    525 	return 0;
    526 }
    527 #endif
    528 
    529 static void
    530 route_get_sdl(const struct ifnet *ifp, const struct sockaddr *dst,
    531     struct sockaddr_dl *sdl, int *flags)
    532 {
    533 	struct llentry *la;
    534 
    535 	KASSERT(ifp != NULL);
    536 
    537 	IF_AFDATA_RLOCK(ifp);
    538 	switch (dst->sa_family) {
    539 	case AF_INET:
    540 		la = lla_lookup(LLTABLE(ifp), 0, dst);
    541 		break;
    542 	case AF_INET6:
    543 		la = lla_lookup(LLTABLE6(ifp), 0, dst);
    544 		break;
    545 	default:
    546 		la = NULL;
    547 		KASSERTMSG(0, "Invalid AF=%d\n", dst->sa_family);
    548 		break;
    549 	}
    550 	IF_AFDATA_RUNLOCK(ifp);
    551 
    552 	void *a = (LLE_IS_VALID(la) && (la->la_flags & LLE_VALID) == LLE_VALID)
    553 	    ? &la->ll_addr : NULL;
    554 
    555 	a = sockaddr_dl_init(sdl, sizeof(*sdl), ifp->if_index, ifp->if_type,
    556 	    NULL, 0, a, ifp->if_addrlen);
    557 	KASSERT(a != NULL);
    558 
    559 	if (la != NULL) {
    560 		*flags = la->la_flags;
    561 		LLE_RUNLOCK(la);
    562 	}
    563 }
    564 
    565 static int
    566 route_output_report(struct rtentry *rt, struct rt_addrinfo *info,
    567     struct rt_xmsghdr *rtm, struct rt_xmsghdr **new_rtm)
    568 {
    569 	int len;
    570 
    571 	if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
    572 		const struct ifaddr *rtifa;
    573 		const struct ifnet *ifp = rt->rt_ifp;
    574 
    575 		info->rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    576 		/* rtifa used to be simply rt->rt_ifa.
    577 		 * If rt->rt_ifa != NULL, then
    578 		 * rt_get_ifa() != NULL.  So this
    579 		 * ought to still be safe. --dyoung
    580 		 */
    581 		rtifa = rt_get_ifa(rt);
    582 		info->rti_info[RTAX_IFA] = rtifa->ifa_addr;
    583 #ifdef RTSOCK_DEBUG
    584 		if (info->rti_info[RTAX_IFA]->sa_family == AF_INET) {
    585 			char ibuf[INET_ADDRSTRLEN];
    586 			char abuf[INET_ADDRSTRLEN];
    587 			printf("%s: copying out RTAX_IFA %s "
    588 			    "for info->rti_info[RTAX_DST] %s "
    589 			    "ifa_getifa %p ifa_seqno %p\n",
    590 			    __func__,
    591 			    RT_IN_PRINT(info, ibuf, RTAX_IFA),
    592 			    RT_IN_PRINT(info, abuf, RTAX_DST),
    593 			    (void *)rtifa->ifa_getifa,
    594 			    rtifa->ifa_seqno);
    595 		}
    596 #endif /* RTSOCK_DEBUG */
    597 		if (ifp->if_flags & IFF_POINTOPOINT)
    598 			info->rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
    599 		else
    600 			info->rti_info[RTAX_BRD] = NULL;
    601 		rtm->rtm_index = ifp->if_index;
    602 	}
    603 	(void)rt_msg2(rtm->rtm_type, info, NULL, NULL, &len);
    604 	if (len > rtm->rtm_msglen) {
    605 		struct rt_xmsghdr *old_rtm = rtm;
    606 		R_Malloc(*new_rtm, struct rt_xmsghdr *, len);
    607 		if (*new_rtm == NULL)
    608 			return ENOBUFS;
    609 		(void)memcpy(*new_rtm, old_rtm, old_rtm->rtm_msglen);
    610 		rtm = *new_rtm;
    611 	}
    612 	(void)rt_msg2(rtm->rtm_type, info, rtm, NULL, 0);
    613 	rtm->rtm_flags = rt->rt_flags;
    614 	rtm_setmetrics(rt, rtm);
    615 	rtm->rtm_addrs = info->rti_addrs;
    616 
    617 	return 0;
    618 }
    619 
    620 /*ARGSUSED*/
    621 int
    622 COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
    623 {
    624 	struct sockproto proto = { .sp_family = PF_XROUTE, };
    625 	struct rt_xmsghdr *rtm = NULL;
    626 	struct rt_xmsghdr *old_rtm = NULL, *new_rtm = NULL;
    627 	struct rtentry *rt = NULL;
    628 	struct rtentry *saved_nrt = NULL;
    629 	struct rt_addrinfo info;
    630 	int len, error = 0;
    631 	sa_family_t family;
    632 	struct sockaddr_dl sdl;
    633 	int bound = curlwp_bind();
    634 	bool do_rt_free = false;
    635 	struct sockaddr_storage netmask;
    636 
    637 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
    638 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
    639 	   (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
    640 		error = ENOBUFS;
    641 		goto out;
    642 	}
    643 	if ((m->m_flags & M_PKTHDR) == 0)
    644 		panic("%s", __func__);
    645 	len = m->m_pkthdr.len;
    646 	if (len < sizeof(*rtm) ||
    647 	    len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
    648 		info.rti_info[RTAX_DST] = NULL;
    649 		senderr(EINVAL);
    650 	}
    651 	R_Malloc(rtm, struct rt_xmsghdr *, len);
    652 	if (rtm == NULL) {
    653 		info.rti_info[RTAX_DST] = NULL;
    654 		senderr(ENOBUFS);
    655 	}
    656 	m_copydata(m, 0, len, rtm);
    657 	if (rtm->rtm_version != RTM_XVERSION) {
    658 		info.rti_info[RTAX_DST] = NULL;
    659 		senderr(EPROTONOSUPPORT);
    660 	}
    661 	rtm->rtm_pid = curproc->p_pid;
    662 	memset(&info, 0, sizeof(info));
    663 	info.rti_addrs = rtm->rtm_addrs;
    664 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
    665 	    &info)) {
    666 		senderr(EINVAL);
    667 	}
    668 	info.rti_flags = rtm->rtm_flags;
    669 #ifdef RTSOCK_DEBUG
    670 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    671 		char abuf[INET_ADDRSTRLEN];
    672 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
    673 		    RT_IN_PRINT(&info, abuf, RTAX_DST));
    674 	}
    675 #endif /* RTSOCK_DEBUG */
    676 	if (info.rti_info[RTAX_DST] == NULL ||
    677 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
    678 		senderr(EINVAL);
    679 	}
    680 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
    681 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
    682 		senderr(EINVAL);
    683 	}
    684 
    685 	/*
    686 	 * Verify that the caller has the appropriate privilege; RTM_GET
    687 	 * is the only operation the non-superuser is allowed.
    688 	 */
    689 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
    690 	    0, rtm, NULL, NULL) != 0)
    691 		senderr(EACCES);
    692 
    693 	/*
    694 	 * route(8) passes a sockaddr truncated with prefixlen.
    695 	 * The kernel doesn't expect such sockaddr and need to
    696 	 * use a buffer that is big enough for the sockaddr expected
    697 	 * (padded with 0's). We keep the original length of the sockaddr.
    698 	 */
    699 	if (info.rti_info[RTAX_NETMASK]) {
    700 		/*
    701 		 * Use the family of RTAX_DST, because RTAX_NETMASK
    702 		 * can have a zero family if it comes from the radix
    703 		 * tree via rt_mask().
    704 		 */
    705 		socklen_t sa_len = sockaddr_getsize_by_family(
    706 		    info.rti_info[RTAX_DST]->sa_family);
    707 		socklen_t masklen = sockaddr_getlen(
    708 		    info.rti_info[RTAX_NETMASK]);
    709 		if (sa_len != 0 && sa_len > masklen) {
    710 			KASSERT(sa_len <= sizeof(netmask));
    711 			memcpy(&netmask, info.rti_info[RTAX_NETMASK], masklen);
    712 			memset((char *)&netmask + masklen, 0, sa_len - masklen);
    713 			info.rti_info[RTAX_NETMASK] = sstocsa(&netmask);
    714 		}
    715 	}
    716 
    717 	switch (rtm->rtm_type) {
    718 
    719 	case RTM_ADD:
    720 		if (info.rti_info[RTAX_GATEWAY] == NULL) {
    721 			senderr(EINVAL);
    722 		}
    723 #if defined(INET) || defined(INET6)
    724 		/* support for new ARP/NDP code with keeping backcompat */
    725 		if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
    726 			const struct sockaddr_dl *sdlp =
    727 			    satocsdl(info.rti_info[RTAX_GATEWAY]);
    728 
    729 			/* Allow routing requests by interface index */
    730 			if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
    731 			    && sdlp->sdl_slen == 0)
    732 				goto fallback;
    733 			/*
    734 			 * Old arp binaries don't set the sdl_index
    735 			 * so we have to complement it.
    736 			 */
    737 			int sdl_index = sdlp->sdl_index;
    738 			if (sdl_index == 0) {
    739 				error = route_get_sdl_index(&info, &sdl_index);
    740 				if (error != 0)
    741 					goto fallback;
    742 			} else if (
    743 			    info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    744 				/*
    745 				 * XXX workaround for SIN_PROXY case; proxy arp
    746 				 * entry should be in an interface that has
    747 				 * a network route including the destination,
    748 				 * not a local (link) route that may not be a
    749 				 * desired place, for example a tap.
    750 				 */
    751 				const struct sockaddr_inarp *sina =
    752 				    (const struct sockaddr_inarp *)
    753 				    info.rti_info[RTAX_DST];
    754 				if (sina->sin_other & SIN_PROXY) {
    755 					error = route_get_sdl_index(&info,
    756 					    &sdl_index);
    757 					if (error != 0)
    758 						goto fallback;
    759 				}
    760 			}
    761 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
    762 			    rtm->rtm_rmx.rmx_expire, &info, sdl_index);
    763 			break;
    764 		}
    765 	fallback:
    766 #endif /* defined(INET) || defined(INET6) */
    767 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    768 		if (error == 0) {
    769 			_rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
    770 			rt_unref(saved_nrt);
    771 		}
    772 		break;
    773 
    774 	case RTM_DELETE:
    775 #if defined(INET) || defined(INET6)
    776 		/* support for new ARP/NDP code */
    777 		if (info.rti_info[RTAX_GATEWAY] &&
    778 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
    779 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
    780 			const struct sockaddr_dl *sdlp =
    781 			    satocsdl(info.rti_info[RTAX_GATEWAY]);
    782 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
    783 			    rtm->rtm_rmx.rmx_expire, &info, sdlp->sdl_index);
    784 			rtm->rtm_flags &= ~RTF_UP;
    785 			break;
    786 		}
    787 #endif
    788 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    789 		if (error != 0)
    790 			break;
    791 
    792 		rt = saved_nrt;
    793 		do_rt_free = true;
    794 		info.rti_info[RTAX_DST] = rt_getkey(rt);
    795 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    796 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    797 		info.rti_info[RTAX_TAG] = rt_gettag(rt);
    798 		error = route_output_report(rt, &info, rtm, &new_rtm);
    799 		if (error)
    800 			senderr(error);
    801 		if (new_rtm != NULL) {
    802 			old_rtm = rtm;
    803 			rtm = new_rtm;
    804 		}
    805 		break;
    806 
    807 	case RTM_GET:
    808 	case RTM_CHANGE:
    809 	case RTM_LOCK:
    810                 /* XXX This will mask info.rti_info[RTAX_DST] with
    811 		 * info.rti_info[RTAX_NETMASK] before
    812                  * searching.  It did not used to do that.  --dyoung
    813 		 */
    814 		rt = NULL;
    815 		error = rtrequest1(RTM_GET, &info, &rt);
    816 		if (error != 0)
    817 			senderr(error);
    818 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
    819 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
    820 			    info.rti_info[RTAX_DST]->sa_len) != 0)
    821 				senderr(ESRCH);
    822 			if (info.rti_info[RTAX_NETMASK] == NULL &&
    823 			    rt_mask(rt) != NULL)
    824 				senderr(ETOOMANYREFS);
    825 		}
    826 
    827 		/*
    828 		 * XXX if arp/ndp requests an L2 entry, we have to obtain
    829 		 * it from lltable while for the route command we have to
    830 		 * return a route as it is. How to distinguish them?
    831 		 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
    832 		 * indicates an L2 entry is requested. For old arp/ndp
    833 		 * binaries, we check RTF_UP flag is NOT set; it works
    834 		 * by the fact that arp/ndp don't set it while the route
    835 		 * command sets it.
    836 		 */
    837 		if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
    838 		     (rtm->rtm_flags & RTF_UP) == 0) &&
    839 		    rtm->rtm_type == RTM_GET &&
    840 		    sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
    841 			int ll_flags = 0;
    842 			route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
    843 			    &ll_flags);
    844 			info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
    845 			error = route_output_report(rt, &info, rtm, &new_rtm);
    846 			if (error)
    847 				senderr(error);
    848 			if (new_rtm != NULL) {
    849 				old_rtm = rtm;
    850 				rtm = new_rtm;
    851 			}
    852 			rtm->rtm_flags |= RTF_LLDATA;
    853 			rtm->rtm_flags &= ~RTF_CONNECTED;
    854 			rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
    855 			break;
    856 		}
    857 
    858 		switch (rtm->rtm_type) {
    859 		case RTM_GET:
    860 			info.rti_info[RTAX_DST] = rt_getkey(rt);
    861 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    862 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    863 			info.rti_info[RTAX_TAG] = rt_gettag(rt);
    864 			error = route_output_report(rt, &info, rtm, &new_rtm);
    865 			if (error)
    866 				senderr(error);
    867 			if (new_rtm != NULL) {
    868 				old_rtm = rtm;
    869 				rtm = new_rtm;
    870 			}
    871 			break;
    872 
    873 		case RTM_CHANGE:
    874 #ifdef NET_MPSAFE
    875 			/*
    876 			 * Release rt_so_mtx to avoid a deadlock with route_intr
    877 			 * and also serialize updating routes to avoid another.
    878 			 */
    879 			if (rt_updating) {
    880 				/* Release to allow the updater to proceed */
    881 				rt_unref(rt);
    882 				rt = NULL;
    883 			}
    884 			while (rt_updating) {
    885 				error = cv_wait_sig(&rt_update_cv, rt_so_mtx);
    886 				if (error != 0)
    887 					goto flush;
    888 			}
    889 			if (rt == NULL) {
    890 				error = rtrequest1(RTM_GET, &info, &rt);
    891 				if (error != 0)
    892 					goto flush;
    893 			}
    894 			rt_updating = true;
    895 			mutex_exit(rt_so_mtx);
    896 
    897 			error = rt_update_prepare(rt);
    898 			if (error == 0) {
    899 				error = rt_update(rt, &info, rtm);
    900 				rt_update_finish(rt);
    901 			}
    902 
    903 			mutex_enter(rt_so_mtx);
    904 			rt_updating = false;
    905 			cv_broadcast(&rt_update_cv);
    906 #else
    907 			error = rt_update(rt, &info, rtm);
    908 #endif
    909 			if (error != 0)
    910 				goto flush;
    911 			/*FALLTHROUGH*/
    912 		case RTM_LOCK:
    913 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    914 			rt->rt_rmx.rmx_locks |=
    915 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    916 			break;
    917 		}
    918 		break;
    919 
    920 	default:
    921 		senderr(EOPNOTSUPP);
    922 	}
    923 
    924 flush:
    925 	if (rtm) {
    926 		if (error)
    927 			rtm->rtm_errno = error;
    928 		else
    929 			rtm->rtm_flags |= RTF_DONE;
    930 	}
    931 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
    932 	    0;
    933 	/* We cannot free old_rtm until we have stopped using the
    934 	 * pointers in info, some of which may point to sockaddrs
    935 	 * in old_rtm.
    936 	 */
    937 	if (old_rtm != NULL)
    938 		Free(old_rtm);
    939 	if (rt) {
    940 		if (do_rt_free) {
    941 #ifdef NET_MPSAFE
    942 			/*
    943 			 * Release rt_so_mtx to avoid a deadlock with
    944 			 * route_intr.
    945 			 */
    946 			mutex_exit(rt_so_mtx);
    947 			rt_free(rt);
    948 			mutex_enter(rt_so_mtx);
    949 #else
    950 			rt_free(rt);
    951 #endif
    952 		} else
    953 			rt_unref(rt);
    954 	}
    955     {
    956 	struct rawcb *rp = NULL;
    957 	/*
    958 	 * Check to see if we don't want our own messages.
    959 	 */
    960 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    961 		if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
    962 			if (rtm)
    963 				Free(rtm);
    964 			m_freem(m);
    965 			goto out;
    966 		}
    967 		/* There is another listener, so construct message */
    968 		rp = sotorawcb(so);
    969 	}
    970 	if (rtm) {
    971 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
    972 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
    973 			m_freem(m);
    974 			m = NULL;
    975 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
    976 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
    977 		Free(rtm);
    978 	}
    979 	if (rp)
    980 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    981 	if (family)
    982 		proto.sp_protocol = family;
    983 	if (m)
    984 		raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
    985 		    &COMPATNAME(route_info).ri_dst, &rt_rawcb);
    986 	if (rp)
    987 		rp->rcb_proto.sp_family = PF_XROUTE;
    988     }
    989 out:
    990 	curlwp_bindx(bound);
    991 	return error;
    992 }
    993 
    994 static int
    995 route_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    996 {
    997 	struct routecb *rop = sotoroutecb(so);
    998 	int error = 0;
    999 	unsigned char *rtm_type;
   1000 	size_t len;
   1001 	unsigned int msgfilter;
   1002 
   1003 	KASSERT(solocked(so));
   1004 
   1005 	if (sopt->sopt_level != AF_ROUTE) {
   1006 		error = ENOPROTOOPT;
   1007 	} else switch (op) {
   1008 	case PRCO_SETOPT:
   1009 		switch (sopt->sopt_name) {
   1010 		case RO_MSGFILTER:
   1011 			msgfilter = 0;
   1012 			for (rtm_type = sopt->sopt_data, len = sopt->sopt_size;
   1013 			     len != 0;
   1014 			     rtm_type++, len -= sizeof(*rtm_type))
   1015 			{
   1016 				/* Guard against overflowing our storage. */
   1017 				if (*rtm_type >= sizeof(msgfilter) * CHAR_BIT) {
   1018 					error = EOVERFLOW;
   1019 					break;
   1020 				}
   1021 				msgfilter |= RTMSGFILTER(*rtm_type);
   1022 			}
   1023 			if (error == 0)
   1024 				rop->rocb_msgfilter = msgfilter;
   1025 			break;
   1026 		default:
   1027 			error = ENOPROTOOPT;
   1028 			break;
   1029 		}
   1030 		break;
   1031 	case PRCO_GETOPT:
   1032 		switch (sopt->sopt_name) {
   1033 		case RO_MSGFILTER:
   1034 			error = ENOTSUP;
   1035 			break;
   1036 		default:
   1037 			error = ENOPROTOOPT;
   1038 			break;
   1039 		}
   1040 	}
   1041 	return error;
   1042 }
   1043 
   1044 static void
   1045 _rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
   1046 {
   1047 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
   1048 	metric(RTV_RPIPE, rmx_recvpipe);
   1049 	metric(RTV_SPIPE, rmx_sendpipe);
   1050 	metric(RTV_SSTHRESH, rmx_ssthresh);
   1051 	metric(RTV_RTT, rmx_rtt);
   1052 	metric(RTV_RTTVAR, rmx_rttvar);
   1053 	metric(RTV_HOPCOUNT, rmx_hopcount);
   1054 	metric(RTV_MTU, rmx_mtu);
   1055 #undef metric
   1056 	if (which & RTV_EXPIRE) {
   1057 		out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
   1058 		    time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
   1059 	}
   1060 }
   1061 
   1062 #ifndef COMPAT_RTSOCK
   1063 /*
   1064  * XXX avoid using void * once msghdr compat disappears.
   1065  */
   1066 void
   1067 rt_setmetrics(void *in, struct rtentry *out)
   1068 {
   1069 	const struct rt_xmsghdr *rtm = in;
   1070 
   1071 	_rt_setmetrics(rtm->rtm_inits, rtm, out);
   1072 }
   1073 #endif
   1074 
   1075 static void
   1076 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
   1077 {
   1078 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
   1079 	metric(rmx_recvpipe);
   1080 	metric(rmx_sendpipe);
   1081 	metric(rmx_ssthresh);
   1082 	metric(rmx_rtt);
   1083 	metric(rmx_rttvar);
   1084 	metric(rmx_hopcount);
   1085 	metric(rmx_mtu);
   1086 	metric(rmx_locks);
   1087 #undef metric
   1088 	out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
   1089 	    time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
   1090 }
   1091 
   1092 static int
   1093 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
   1094     struct rt_addrinfo *rtinfo)
   1095 {
   1096 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
   1097 	int i;
   1098 
   1099 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
   1100 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
   1101 			continue;
   1102 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
   1103 		RT_XADVANCE(cp, sa);
   1104 	}
   1105 
   1106 	/*
   1107 	 * Check for extra addresses specified, except RTM_GET asking
   1108 	 * for interface info.
   1109 	 */
   1110 	if (rtmtype == RTM_GET) {
   1111 		if (((rtinfo->rti_addrs &
   1112 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
   1113 			return 1;
   1114 	} else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
   1115 		return 1;
   1116 	/* Check for bad data length.  */
   1117 	if (cp != cplim) {
   1118 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
   1119 		    cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
   1120 			/*
   1121 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
   1122 			 * We accept this for now for the sake of old
   1123 			 * binaries or third party softwares.
   1124 			 */
   1125 			;
   1126 		else
   1127 			return 1;
   1128 	}
   1129 	return 0;
   1130 }
   1131 
   1132 static int
   1133 rt_getlen(int type)
   1134 {
   1135 #ifndef COMPAT_RTSOCK
   1136 	CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
   1137 	CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
   1138 	CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
   1139 	CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
   1140 #endif
   1141 
   1142 	switch (type) {
   1143 	case RTM_ODELADDR:
   1144 	case RTM_ONEWADDR:
   1145 	case RTM_OCHGADDR:
   1146 		if (rtsock_70_iflist_hook.hooked)
   1147 			return sizeof(struct ifa_msghdr70);
   1148 		else {
   1149 #ifdef RTSOCK_DEBUG
   1150 			printf("%s: unsupported RTM type %d\n", __func__, type);
   1151 #endif
   1152 			return -1;
   1153 		}
   1154 
   1155 	case RTM_DELADDR:
   1156 	case RTM_NEWADDR:
   1157 	case RTM_CHGADDR:
   1158 		return sizeof(struct ifa_xmsghdr);
   1159 
   1160 	case RTM_OOIFINFO:
   1161 		if (rtsock_14_iflist_hook.hooked)
   1162 			return sizeof(struct if_msghdr14);
   1163 		else {
   1164 #ifdef RTSOCK_DEBUG
   1165 			printf("%s: unsupported RTM type RTM_OOIFINFO\n",
   1166 			    __func__);
   1167 #endif
   1168 			return -1;
   1169 		}
   1170 
   1171 	case RTM_OIFINFO:
   1172 		if (rtsock_50_iflist_hook.hooked)
   1173 			return sizeof(struct if_msghdr50);
   1174 		else {
   1175 #ifdef RTSOCK_DEBUG
   1176 			printf("%s: unsupported RTM type RTM_OIFINFO\n",
   1177 			    __func__);
   1178 #endif
   1179 			return -1;
   1180 		}
   1181 
   1182 	case RTM_IFINFO:
   1183 		return sizeof(struct if_xmsghdr);
   1184 
   1185 	case RTM_IFANNOUNCE:
   1186 	case RTM_IEEE80211:
   1187 		return sizeof(struct if_xannouncemsghdr);
   1188 
   1189 	default:
   1190 		return sizeof(struct rt_xmsghdr);
   1191 	}
   1192 }
   1193 
   1194 
   1195 struct mbuf *
   1196 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
   1197 {
   1198 	struct rt_xmsghdr *rtm;
   1199 	struct mbuf *m;
   1200 	int i;
   1201 	const struct sockaddr *sa;
   1202 	int len, dlen;
   1203 
   1204 	m = m_gethdr(M_DONTWAIT, MT_DATA);
   1205 	if (m == NULL)
   1206 		return m;
   1207 	MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
   1208 
   1209 	if ((len = rt_getlen(type)) == -1)
   1210 		goto out;
   1211 	if (len > MHLEN + MLEN)
   1212 		panic("%s: message too long", __func__);
   1213 	else if (len > MHLEN) {
   1214 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
   1215 		if (m->m_next == NULL)
   1216 			goto out;
   1217 		MCLAIM(m->m_next, m->m_owner);
   1218 		m->m_pkthdr.len = len;
   1219 		m->m_len = MHLEN;
   1220 		m->m_next->m_len = len - MHLEN;
   1221 	} else {
   1222 		m->m_pkthdr.len = m->m_len = len;
   1223 	}
   1224 	m_reset_rcvif(m);
   1225 	m_copyback(m, 0, datalen, data);
   1226 	if (len > datalen)
   1227 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
   1228 	rtm = mtod(m, struct rt_xmsghdr *);
   1229 	for (i = 0; i < RTAX_MAX; i++) {
   1230 		if ((sa = rtinfo->rti_info[i]) == NULL)
   1231 			continue;
   1232 		rtinfo->rti_addrs |= (1 << i);
   1233 		dlen = RT_XROUNDUP(sa->sa_len);
   1234 		m_copyback(m, len, sa->sa_len, sa);
   1235 		if (dlen != sa->sa_len) {
   1236 			/*
   1237 			 * Up to 7 + 1 nul's since roundup is to
   1238 			 * sizeof(uint64_t) (8 bytes)
   1239 			 */
   1240 			m_copyback(m, len + sa->sa_len,
   1241 			    dlen - sa->sa_len, "\0\0\0\0\0\0\0");
   1242 		}
   1243 		len += dlen;
   1244 	}
   1245 	if (m->m_pkthdr.len != len)
   1246 		goto out;
   1247 	rtm->rtm_msglen = len;
   1248 	rtm->rtm_version = RTM_XVERSION;
   1249 	rtm->rtm_type = type;
   1250 	return m;
   1251 out:
   1252 	m_freem(m);
   1253 	return NULL;
   1254 }
   1255 
   1256 /*
   1257  * rt_msg2
   1258  *
   1259  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
   1260  *		returns the length of the message in 'lenp'.
   1261  *
   1262  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
   1263  *	the message
   1264  * otherwise walkarg's w_needed is updated and if the user buffer is
   1265  *	specified and w_needed indicates space exists the information is copied
   1266  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
   1267  *	if the allocation fails ENOBUFS is returned.
   1268  */
   1269 static int
   1270 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
   1271 	int *lenp)
   1272 {
   1273 	int i;
   1274 	int len, dlen, second_time = 0;
   1275 	char *cp0, *cp = cpv;
   1276 
   1277 	rtinfo->rti_addrs = 0;
   1278 again:
   1279 	if ((len = rt_getlen(type)) == -1)
   1280 		return EINVAL;
   1281 
   1282 	if ((cp0 = cp) != NULL)
   1283 		cp += len;
   1284 	for (i = 0; i < RTAX_MAX; i++) {
   1285 		const struct sockaddr *sa;
   1286 
   1287 		if ((sa = rtinfo->rti_info[i]) == NULL)
   1288 			continue;
   1289 		rtinfo->rti_addrs |= (1 << i);
   1290 		dlen = RT_XROUNDUP(sa->sa_len);
   1291 		if (cp) {
   1292 			int diff = dlen - sa->sa_len;
   1293 			(void)memcpy(cp, sa, (size_t)sa->sa_len);
   1294 			cp += sa->sa_len;
   1295 			if (diff > 0) {
   1296 				(void)memset(cp, 0, (size_t)diff);
   1297 				cp += diff;
   1298 			}
   1299 		}
   1300 		len += dlen;
   1301 	}
   1302 	if (cp == NULL && w != NULL && !second_time) {
   1303 		struct rt_walkarg *rw = w;
   1304 
   1305 		rw->w_needed += len;
   1306 		if (rw->w_needed <= 0 && rw->w_where) {
   1307 			if (rw->w_tmemsize < len) {
   1308 				if (rw->w_tmem)
   1309 					kmem_free(rw->w_tmem, rw->w_tmemsize);
   1310 				rw->w_tmem = kmem_zalloc(len, KM_SLEEP);
   1311 				rw->w_tmemsize = len;
   1312 			}
   1313 			if (rw->w_tmem) {
   1314 				cp = rw->w_tmem;
   1315 				second_time = 1;
   1316 				goto again;
   1317 			} else {
   1318 				rw->w_tmemneeded = len;
   1319 				return ENOBUFS;
   1320 			}
   1321 		}
   1322 	}
   1323 	if (cp) {
   1324 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
   1325 
   1326 		rtm->rtm_version = RTM_XVERSION;
   1327 		rtm->rtm_type = type;
   1328 		rtm->rtm_msglen = len;
   1329 	}
   1330 	if (lenp)
   1331 		*lenp = len;
   1332 	return 0;
   1333 }
   1334 
   1335 #ifndef COMPAT_RTSOCK
   1336 int
   1337 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
   1338 	int *lenp)
   1339 {
   1340 	return rt_msg2(type, rtinfo, cpv, w, lenp);
   1341 }
   1342 #endif
   1343 
   1344 /*
   1345  * MODULE_HOOK glue for rtsock_14_oifmsg and rtsock_14_iflist
   1346  */
   1347 MODULE_CALL_VOID_HOOK_DECL(rtsock_14_oifmsg_hook, (struct ifnet *ifp));
   1348 #ifndef COMPAT_RTSOCK
   1349 MODULE_CALL_VOID_HOOK(rtsock_14_oifmsg_hook, (struct ifnet *ifp), (ifp),
   1350     __nothing);
   1351 #endif
   1352 
   1353 MODULE_CALL_INT_HOOK_DECL(rtsock_14_iflist_hook,
   1354     (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
   1355      size_t len));
   1356 #ifndef COMPAT_RTSOCK
   1357 MODULE_CALL_INT_HOOK(rtsock_14_iflist_hook,
   1358     (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
   1359      size_t len),
   1360     (ifp, w, info, len),
   1361     enosys());
   1362 #endif
   1363 
   1364 /*
   1365  * MODULE_HOOK glue for rtsock_50 ifaddr_list and various message routines
   1366  */
   1367 MODULE_CALL_INT_HOOK_DECL(rtsock_50_iflist_hook,
   1368     (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
   1369      size_t len));
   1370 #ifndef COMPAT_RTSOCK
   1371 MODULE_CALL_INT_HOOK(rtsock_50_iflist_hook,
   1372     (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
   1373      size_t len),
   1374     (ifp, w, info, len),
   1375     enosys());
   1376 #endif
   1377 
   1378 MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_missmsg_hook,
   1379     (int, const struct rt_addrinfo *, int, int));
   1380 #ifndef COMPAT_RTSOCK
   1381 MODULE_CALL_VOID_HOOK(rtsock_50_rt_missmsg_hook,
   1382     (int type, const struct rt_addrinfo *rtinfo, int flags, int error),
   1383     (type, rtinfo, flags, error), __nothing);
   1384 #endif
   1385 
   1386 MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ifmsg_hook, (struct ifnet *));
   1387 #ifndef COMPAT_RTSOCK
   1388 MODULE_CALL_VOID_HOOK(rtsock_50_rt_ifmsg_hook, (struct ifnet *ifp), (ifp),
   1389     __nothing);
   1390 #endif
   1391 
   1392 MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_newaddrmsg_hook,
   1393     (int, struct ifaddr *, int, struct rtentry *));
   1394 #ifndef COMPAT_RTSOCK
   1395 MODULE_CALL_VOID_HOOK(rtsock_50_rt_newaddrmsg_hook,
   1396     (int cmd, struct ifaddr *ifa, int error, struct rtentry *rt),
   1397     (cmd, ifa, error, rt), __nothing);
   1398 #endif
   1399 
   1400 MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ifannouncemsg_hook,
   1401     (struct ifnet *, int what));
   1402 #ifndef COMPAT_RTSOCK
   1403 MODULE_CALL_VOID_HOOK(rtsock_50_rt_ifannouncemsg_hook,
   1404     (struct ifnet *ifp, int what), (ifp, what), __nothing);
   1405 #endif
   1406 
   1407 MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ieee80211msg_hook,
   1408     (struct ifnet *, int, void *, size_t));
   1409 #ifndef COMPAT_RTSOCK
   1410 MODULE_CALL_VOID_HOOK(rtsock_50_rt_ieee80211msg_hook,
   1411     (struct ifnet *ifp, int what, void *data, size_t data_len),
   1412     (ifp, what, data, data_len), __nothing);
   1413 #endif
   1414 
   1415 MODULE_CALL_VOID_HOOK_DECL(rtsock_50_oifmsg_hook, (struct ifnet *ifp));
   1416 #ifndef COMPAT_RTSOCK
   1417 MODULE_CALL_VOID_HOOK(rtsock_50_oifmsg_hook, (struct ifnet *ifp), (ifp),
   1418     __nothing);
   1419 #endif
   1420 
   1421 /*
   1422  * MODULE_HOOK glue for rtsock70_newaddrmsg1, rtsock70_ifaddr_listaddr,
   1423  * and rtsock70_ifaddr_listif
   1424  */
   1425 MODULE_CALL_VOID_HOOK_DECL(rtsock_70_newaddr_hook, (int, struct ifaddr *));
   1426 #ifndef COMPAT_RTSOCK
   1427 MODULE_CALL_VOID_HOOK(rtsock_70_newaddr_hook, (int cmd, struct ifaddr *ifa),
   1428     (cmd, ifa), stub_70_rt_newaddrmsg1(cmd, ifa));
   1429 #endif
   1430 
   1431 MODULE_CALL_INT_HOOK_DECL(rtsock_70_iflist_hook,
   1432     (struct rt_walkarg *, struct ifaddr *, struct rt_addrinfo *));
   1433 #ifndef COMPAT_RTSOCK
   1434 MODULE_CALL_INT_HOOK(rtsock_70_iflist_hook,
   1435     (struct rt_walkarg *w, struct ifaddr *ifa, struct rt_addrinfo *info),
   1436     (w, ifa, info),
   1437     enosys());
   1438 #endif
   1439 
   1440 /*
   1441  * This routine is called to generate a message from the routing
   1442  * socket indicating that a redirect has occurred, a routing lookup
   1443  * has failed, or that a protocol has detected timeouts to a particular
   1444  * destination.
   1445  */
   1446 void
   1447 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
   1448     int error)
   1449 {
   1450 	struct rt_xmsghdr rtm;
   1451 	struct mbuf *m;
   1452 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
   1453 	struct rt_addrinfo info = *rtinfo;
   1454 
   1455 	COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
   1456 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1457 		return;
   1458 	memset(&rtm, 0, sizeof(rtm));
   1459 	rtm.rtm_pid = curproc->p_pid;
   1460 	rtm.rtm_flags = RTF_DONE | flags;
   1461 	rtm.rtm_errno = error;
   1462 	m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
   1463 	if (m == NULL)
   1464 		return;
   1465 	mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
   1466 	COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
   1467 }
   1468 
   1469 /*
   1470  * This routine is called to generate a message from the routing
   1471  * socket indicating that the status of a network interface has changed.
   1472  */
   1473 void
   1474 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
   1475 {
   1476 	struct if_xmsghdr ifm;
   1477 	struct mbuf *m;
   1478 	struct rt_addrinfo info;
   1479 
   1480 	COMPATCALL(rt_ifmsg, (ifp));
   1481 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1482 		return;
   1483 	(void)memset(&info, 0, sizeof(info));
   1484 	(void)memset(&ifm, 0, sizeof(ifm));
   1485 	ifm.ifm_index = ifp->if_index;
   1486 	ifm.ifm_flags = ifp->if_flags;
   1487 	ifm.ifm_data = ifp->if_data;
   1488 	ifm.ifm_addrs = 0;
   1489 	m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
   1490 	if (m == NULL)
   1491 		return;
   1492 	COMPATNAME(route_enqueue)(m, 0);
   1493 	rtsock_14_oifmsg_hook_call(ifp);
   1494 	rtsock_50_oifmsg_hook_call(ifp);
   1495 }
   1496 
   1497 #ifndef COMPAT_RTSOCK
   1498 static int
   1499 if_addrflags(struct ifaddr *ifa)
   1500 {
   1501 
   1502 	switch (ifa->ifa_addr->sa_family) {
   1503 #ifdef INET
   1504 	case AF_INET:
   1505 		return ((struct in_ifaddr *)ifa)->ia4_flags;
   1506 #endif
   1507 #ifdef INET6
   1508 	case AF_INET6:
   1509 		return ((struct in6_ifaddr *)ifa)->ia6_flags;
   1510 #endif
   1511 	default:
   1512 		return 0;
   1513 	}
   1514 }
   1515 #endif
   1516 
   1517 /*
   1518  * This is called to generate messages from the routing socket
   1519  * indicating a network interface has had addresses associated with it.
   1520  * if we ever reverse the logic and replace messages TO the routing
   1521  * socket indicate a request to configure interfaces, then it will
   1522  * be unnecessary as the routing socket will automatically generate
   1523  * copies of it.
   1524  */
   1525 void
   1526 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
   1527     struct rtentry *rt)
   1528 {
   1529 #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
   1530 	struct rt_addrinfo info;
   1531 	const struct sockaddr *sa;
   1532 	int pass;
   1533 	struct mbuf *m;
   1534 	struct ifnet *ifp;
   1535 	struct rt_xmsghdr rtm;
   1536 	struct ifa_xmsghdr ifam;
   1537 	int ncmd;
   1538 
   1539 	KASSERT(ifa != NULL);
   1540 	KASSERT(ifa->ifa_addr != NULL);
   1541 	ifp = ifa->ifa_ifp;
   1542 #ifdef SCTP
   1543 	if (cmd == RTM_ADD) {
   1544 		sctp_add_ip_address(ifa);
   1545 	} else if (cmd == RTM_DELETE) {
   1546 		sctp_delete_ip_address(ifa);
   1547 	}
   1548 #endif
   1549 
   1550 	COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
   1551 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1552 		return;
   1553 	for (pass = 1; pass < 3; pass++) {
   1554 		memset(&info, 0, sizeof(info));
   1555 		switch (cmdpass(cmd, pass)) {
   1556 		case cmdpass(RTM_ADD, 1):
   1557 		case cmdpass(RTM_CHANGE, 1):
   1558 		case cmdpass(RTM_DELETE, 2):
   1559 		case cmdpass(RTM_NEWADDR, 1):
   1560 		case cmdpass(RTM_DELADDR, 1):
   1561 		case cmdpass(RTM_CHGADDR, 1):
   1562 			switch (cmd) {
   1563 			case RTM_ADD:
   1564 				ncmd = RTM_XNEWADDR;
   1565 				break;
   1566 			case RTM_DELETE:
   1567 				ncmd = RTM_XDELADDR;
   1568 				break;
   1569 			case RTM_CHANGE:
   1570 				ncmd = RTM_XCHGADDR;
   1571 				break;
   1572 			case RTM_NEWADDR:
   1573 				ncmd = RTM_XNEWADDR;
   1574 				break;
   1575 			case RTM_DELADDR:
   1576 				ncmd = RTM_XDELADDR;
   1577 				break;
   1578 			case RTM_CHGADDR:
   1579 				ncmd = RTM_XCHGADDR;
   1580 				break;
   1581 			default:
   1582 				panic("%s: unknown command %d", __func__, cmd);
   1583 			}
   1584 			rtsock_70_newaddr_hook_call(ncmd, ifa);
   1585 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
   1586 			KASSERT(ifp->if_dl != NULL);
   1587 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   1588 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1589 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   1590 			memset(&ifam, 0, sizeof(ifam));
   1591 			ifam.ifam_index = ifp->if_index;
   1592 			ifam.ifam_metric = ifa->ifa_metric;
   1593 			ifam.ifam_flags = ifa->ifa_flags;
   1594 #ifndef COMPAT_RTSOCK
   1595 			ifam.ifam_pid = curproc->p_pid;
   1596 			ifam.ifam_addrflags = if_addrflags(ifa);
   1597 #endif
   1598 			m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
   1599 			if (m == NULL)
   1600 				continue;
   1601 			mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
   1602 			    info.rti_addrs;
   1603 			break;
   1604 		case cmdpass(RTM_ADD, 2):
   1605 		case cmdpass(RTM_CHANGE, 2):
   1606 		case cmdpass(RTM_DELETE, 1):
   1607 			if (rt == NULL)
   1608 				continue;
   1609 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1610 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
   1611 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1612 			memset(&rtm, 0, sizeof(rtm));
   1613 			rtm.rtm_pid = curproc->p_pid;
   1614 			rtm.rtm_index = ifp->if_index;
   1615 			rtm.rtm_flags |= rt->rt_flags;
   1616 			rtm.rtm_errno = error;
   1617 			m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
   1618 			if (m == NULL)
   1619 				continue;
   1620 			mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
   1621 			break;
   1622 		default:
   1623 			continue;
   1624 		}
   1625 		KASSERTMSG(m != NULL, "called with wrong command");
   1626 		COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
   1627 	}
   1628 #undef cmdpass
   1629 }
   1630 
   1631 static struct mbuf *
   1632 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
   1633     struct rt_addrinfo *info)
   1634 {
   1635 	struct if_xannouncemsghdr ifan;
   1636 
   1637 	memset(info, 0, sizeof(*info));
   1638 	memset(&ifan, 0, sizeof(ifan));
   1639 	ifan.ifan_index = ifp->if_index;
   1640 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
   1641 	ifan.ifan_what = what;
   1642 	return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
   1643 }
   1644 
   1645 /*
   1646  * This is called to generate routing socket messages indicating
   1647  * network interface arrival and departure.
   1648  */
   1649 void
   1650 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
   1651 {
   1652 	struct mbuf *m;
   1653 	struct rt_addrinfo info;
   1654 
   1655 	COMPATCALL(rt_ifannouncemsg, (ifp, what));
   1656 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1657 		return;
   1658 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
   1659 	if (m == NULL)
   1660 		return;
   1661 	COMPATNAME(route_enqueue)(m, 0);
   1662 }
   1663 
   1664 /*
   1665  * This is called to generate routing socket messages indicating
   1666  * IEEE80211 wireless events.
   1667  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
   1668  */
   1669 void
   1670 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
   1671 	size_t data_len)
   1672 {
   1673 	struct mbuf *m;
   1674 	struct rt_addrinfo info;
   1675 
   1676 	COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
   1677 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1678 		return;
   1679 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
   1680 	if (m == NULL)
   1681 		return;
   1682 	/*
   1683 	 * Append the ieee80211 data.  Try to stick it in the
   1684 	 * mbuf containing the ifannounce msg; otherwise allocate
   1685 	 * a new mbuf and append.
   1686 	 *
   1687 	 * NB: we assume m is a single mbuf.
   1688 	 */
   1689 	if (data_len > M_TRAILINGSPACE(m)) {
   1690 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
   1691 		if (n == NULL) {
   1692 			m_freem(m);
   1693 			return;
   1694 		}
   1695 		(void)memcpy(mtod(n, void *), data, data_len);
   1696 		n->m_len = data_len;
   1697 		m->m_next = n;
   1698 	} else if (data_len > 0) {
   1699 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
   1700 		m->m_len += data_len;
   1701 	}
   1702 	if (m->m_flags & M_PKTHDR)
   1703 		m->m_pkthdr.len += data_len;
   1704 	mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
   1705 	COMPATNAME(route_enqueue)(m, 0);
   1706 }
   1707 
   1708 #ifndef COMPAT_RTSOCK
   1709 /*
   1710  * Send a routing message as mimicing that a cloned route is added.
   1711  */
   1712 void
   1713 rt_clonedmsg(const struct sockaddr *dst, const struct ifnet *ifp,
   1714     const struct rtentry *rt)
   1715 {
   1716 	struct rt_addrinfo info;
   1717 	/* Mimic flags exactly */
   1718 #define RTF_LLINFO	0x400
   1719 #define RTF_CLONED	0x2000
   1720 	int flags = RTF_UP | RTF_HOST | RTF_DONE | RTF_LLINFO | RTF_CLONED;
   1721 	union {
   1722 		struct sockaddr sa;
   1723 		struct sockaddr_storage ss;
   1724 		struct sockaddr_dl sdl;
   1725 	} u;
   1726 	uint8_t namelen = strlen(ifp->if_xname);
   1727 	uint8_t addrlen = ifp->if_addrlen;
   1728 
   1729 	if (rt == NULL)
   1730 		return; /* XXX */
   1731 
   1732 	memset(&info, 0, sizeof(info));
   1733 	info.rti_info[RTAX_DST] = dst;
   1734 	sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type,
   1735 	    NULL, namelen, NULL, addrlen);
   1736 	info.rti_info[RTAX_GATEWAY] = &u.sa;
   1737 
   1738 	rt_missmsg(RTM_ADD, &info, flags, 0);
   1739 #undef RTF_LLINFO
   1740 #undef RTF_CLONED
   1741 }
   1742 #endif	/* COMPAT_RTSOCK */
   1743 
   1744 /*
   1745  * Routing message software interrupt routine
   1746  */
   1747 static void
   1748 COMPATNAME(route_intr)(void *cookie)
   1749 {
   1750 	struct sockproto proto = { .sp_family = PF_XROUTE, };
   1751 	struct route_info * const ri = &COMPATNAME(route_info);
   1752 	struct mbuf *m;
   1753 
   1754 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
   1755 	for (;;) {
   1756 		IFQ_LOCK(&ri->ri_intrq);
   1757 		IF_DEQUEUE(&ri->ri_intrq, m);
   1758 		IFQ_UNLOCK(&ri->ri_intrq);
   1759 		if (m == NULL)
   1760 			break;
   1761 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
   1762 #ifdef NET_MPSAFE
   1763 		mutex_enter(rt_so_mtx);
   1764 #endif
   1765 		raw_input(m, &proto, &ri->ri_src, &ri->ri_dst, &rt_rawcb);
   1766 #ifdef NET_MPSAFE
   1767 		mutex_exit(rt_so_mtx);
   1768 #endif
   1769 	}
   1770 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   1771 }
   1772 
   1773 /*
   1774  * Enqueue a message to the software interrupt routine.
   1775  */
   1776 void
   1777 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
   1778 {
   1779 	struct route_info * const ri = &COMPATNAME(route_info);
   1780 	int wasempty;
   1781 
   1782 	IFQ_LOCK(&ri->ri_intrq);
   1783 	if (IF_QFULL(&ri->ri_intrq)) {
   1784 		printf("%s: queue full, dropped message\n", __func__);
   1785 		IF_DROP(&ri->ri_intrq);
   1786 		IFQ_UNLOCK(&ri->ri_intrq);
   1787 		m_freem(m);
   1788 	} else {
   1789 		wasempty = IF_IS_EMPTY(&ri->ri_intrq);
   1790 		M_SETCTX(m, (uintptr_t)family);
   1791 		IF_ENQUEUE(&ri->ri_intrq, m);
   1792 		IFQ_UNLOCK(&ri->ri_intrq);
   1793 		if (wasempty) {
   1794 			kpreempt_disable();
   1795 			softint_schedule(ri->ri_sih);
   1796 			kpreempt_enable();
   1797 		}
   1798 	}
   1799 }
   1800 
   1801 static void
   1802 COMPATNAME(route_init)(void)
   1803 {
   1804 	struct route_info * const ri = &COMPATNAME(route_info);
   1805 
   1806 #ifndef COMPAT_RTSOCK
   1807 	rt_init();
   1808 #endif
   1809 #ifdef NET_MPSAFE
   1810 	rt_so_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
   1811 
   1812 	cv_init(&rt_update_cv, "rtsock_cv");
   1813 #endif
   1814 
   1815 #ifndef COMPAT_RTSOCK
   1816 	sysctl_net_route_setup(NULL);
   1817 #endif
   1818 	ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
   1819 	ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1820 	    COMPATNAME(route_intr), NULL);
   1821 	IFQ_LOCK_INIT(&ri->ri_intrq);
   1822 }
   1823 
   1824 /*
   1825  * Definitions of protocols supported in the ROUTE domain.
   1826  */
   1827 #ifndef COMPAT_RTSOCK
   1828 PR_WRAP_USRREQS(route);
   1829 #else
   1830 PR_WRAP_USRREQS(compat_50_route);
   1831 #endif
   1832 
   1833 static const struct pr_usrreqs route_usrreqs = {
   1834 	.pr_attach	= COMPATNAME(route_attach_wrapper),
   1835 	.pr_detach	= COMPATNAME(route_detach_wrapper),
   1836 	.pr_accept	= COMPATNAME(route_accept_wrapper),
   1837 	.pr_bind	= COMPATNAME(route_bind_wrapper),
   1838 	.pr_listen	= COMPATNAME(route_listen_wrapper),
   1839 	.pr_connect	= COMPATNAME(route_connect_wrapper),
   1840 	.pr_connect2	= COMPATNAME(route_connect2_wrapper),
   1841 	.pr_disconnect	= COMPATNAME(route_disconnect_wrapper),
   1842 	.pr_shutdown	= COMPATNAME(route_shutdown_wrapper),
   1843 	.pr_abort	= COMPATNAME(route_abort_wrapper),
   1844 	.pr_ioctl	= COMPATNAME(route_ioctl_wrapper),
   1845 	.pr_stat	= COMPATNAME(route_stat_wrapper),
   1846 	.pr_peeraddr	= COMPATNAME(route_peeraddr_wrapper),
   1847 	.pr_sockaddr	= COMPATNAME(route_sockaddr_wrapper),
   1848 	.pr_rcvd	= COMPATNAME(route_rcvd_wrapper),
   1849 	.pr_recvoob	= COMPATNAME(route_recvoob_wrapper),
   1850 	.pr_send	= COMPATNAME(route_send_wrapper),
   1851 	.pr_sendoob	= COMPATNAME(route_sendoob_wrapper),
   1852 	.pr_purgeif	= COMPATNAME(route_purgeif_wrapper),
   1853 };
   1854 
   1855 static const struct protosw COMPATNAME(route_protosw)[] = {
   1856 	{
   1857 		.pr_type = SOCK_RAW,
   1858 		.pr_domain = &COMPATNAME(routedomain),
   1859 		.pr_flags = PR_ATOMIC|PR_ADDR,
   1860 		.pr_ctlinput = raw_ctlinput,
   1861 		.pr_ctloutput = route_ctloutput,
   1862 		.pr_usrreqs = &route_usrreqs,
   1863 		.pr_init = rt_pr_init,
   1864 	},
   1865 };
   1866 
   1867 struct domain COMPATNAME(routedomain) = {
   1868 	.dom_family = PF_XROUTE,
   1869 	.dom_name = DOMAINNAME,
   1870 	.dom_init = COMPATNAME(route_init),
   1871 	.dom_protosw = COMPATNAME(route_protosw),
   1872 	.dom_protoswNPROTOSW =
   1873 	    &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
   1874 };
   1875 
   1876 /*
   1877  * The remaining code implements the routing-table sysctl node.  It is
   1878  * compiled only for the non-COMPAT case.
   1879  */
   1880 
   1881 #ifndef COMPAT_RTSOCK
   1882 /*
   1883  * This is used in dumping the kernel table via sysctl().
   1884  */
   1885 static int
   1886 sysctl_dumpentry(struct rtentry *rt, void *v)
   1887 {
   1888 	struct rt_walkarg *w = v;
   1889 	int error = 0, size;
   1890 	struct rt_addrinfo info;
   1891 
   1892 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
   1893 		return 0;
   1894 	memset(&info, 0, sizeof(info));
   1895 	info.rti_info[RTAX_DST] = rt_getkey(rt);
   1896 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1897 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1898 	info.rti_info[RTAX_TAG] = rt_gettag(rt);
   1899 	if (rt->rt_ifp) {
   1900 		const struct ifaddr *rtifa;
   1901 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
   1902 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
   1903 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
   1904 		 * --dyoung
   1905 		 */
   1906 		rtifa = rt_get_ifa(rt);
   1907 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
   1908 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
   1909 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
   1910 	}
   1911 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
   1912 		return error;
   1913 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1914 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
   1915 
   1916 		rtm->rtm_flags = rt->rt_flags;
   1917 		rtm->rtm_use = rt->rt_use;
   1918 		rtm_setmetrics(rt, rtm);
   1919 		KASSERT(rt->rt_ifp != NULL);
   1920 		rtm->rtm_index = rt->rt_ifp->if_index;
   1921 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
   1922 		rtm->rtm_addrs = info.rti_addrs;
   1923 		if ((error = copyout(rtm, w->w_where, size)) != 0)
   1924 			w->w_where = NULL;
   1925 		else
   1926 			w->w_where = (char *)w->w_where + size;
   1927 	}
   1928 	return error;
   1929 }
   1930 
   1931 static int
   1932 sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
   1933     struct rt_addrinfo *info, size_t len)
   1934 {
   1935 	struct if_xmsghdr *ifm;
   1936 	int error;
   1937 
   1938 	ifm = (struct if_xmsghdr *)w->w_tmem;
   1939 	ifm->ifm_index = ifp->if_index;
   1940 	ifm->ifm_flags = ifp->if_flags;
   1941 	ifm->ifm_data = ifp->if_data;
   1942 	ifm->ifm_addrs = info->rti_addrs;
   1943 	if ((error = copyout(ifm, w->w_where, len)) == 0)
   1944 		w->w_where = (char *)w->w_where + len;
   1945 	return error;
   1946 }
   1947 
   1948 static int
   1949 sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
   1950      struct rt_addrinfo *info)
   1951 {
   1952 	int len, error;
   1953 
   1954 	if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
   1955 		return error;
   1956 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1957 		struct ifa_xmsghdr *ifam;
   1958 
   1959 		ifam = (struct ifa_xmsghdr *)w->w_tmem;
   1960 		ifam->ifam_index = ifa->ifa_ifp->if_index;
   1961 		ifam->ifam_flags = ifa->ifa_flags;
   1962 		ifam->ifam_metric = ifa->ifa_metric;
   1963 		ifam->ifam_addrs = info->rti_addrs;
   1964 		ifam->ifam_pid = 0;
   1965 		ifam->ifam_addrflags = if_addrflags(ifa);
   1966 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
   1967 			w->w_where = (char *)w->w_where + len;
   1968 	}
   1969 	return error;
   1970 }
   1971 
   1972 static int
   1973 sysctl_iflist(int af, struct rt_walkarg *w, int type)
   1974 {
   1975 	struct ifnet *ifp;
   1976 	struct ifaddr *ifa;
   1977 	struct	rt_addrinfo info;
   1978 	int	cmd, len, error = 0;
   1979 	int s;
   1980 	struct psref psref;
   1981 	int bound;
   1982 
   1983 	switch (type) {
   1984 	case NET_RT_IFLIST:
   1985 		cmd = RTM_IFINFO;
   1986 		break;
   1987 	case NET_RT_OOOIFLIST:
   1988 		cmd = RTM_OOIFINFO;
   1989 		break;
   1990 	case NET_RT_OOIFLIST:
   1991 		cmd = RTM_OIFINFO;
   1992 		break;
   1993 	case NET_RT_OIFLIST:
   1994 		cmd = RTM_IFINFO;
   1995 		break;
   1996 	default:
   1997 #ifdef RTSOCK_DEBUG
   1998 		printf("%s: unsupported IFLIST type %d\n", __func__, type);
   1999 #endif
   2000 		return EINVAL;
   2001 	}
   2002 
   2003 	memset(&info, 0, sizeof(info));
   2004 
   2005 	bound = curlwp_bind();
   2006 	s = pserialize_read_enter();
   2007 	IFNET_READER_FOREACH(ifp) {
   2008 		int _s;
   2009 		if (w->w_arg && w->w_arg != ifp->if_index)
   2010 			continue;
   2011 		if (IFADDR_READER_EMPTY(ifp))
   2012 			continue;
   2013 
   2014 		if_acquire(ifp, &psref);
   2015 		pserialize_read_exit(s);
   2016 
   2017 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   2018 		if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
   2019 			goto release_exit;
   2020 		info.rti_info[RTAX_IFP] = NULL;
   2021 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   2022 			switch (type) {
   2023 			case NET_RT_OIFLIST: /* old _70 */
   2024 				if (!rtsock_70_iflist_hook.hooked) {
   2025 					error = EINVAL;
   2026 					break;
   2027 				}
   2028 				/* FALLTHROUGH */
   2029 			case NET_RT_IFLIST: /* current */
   2030 				error = sysctl_iflist_if(ifp, w, &info, len);
   2031 				break;
   2032 			case NET_RT_OOIFLIST: /* old _50 */
   2033 				error = rtsock_50_iflist_hook_call(ifp, w, &info,
   2034 				    len);
   2035 				break;
   2036 			case NET_RT_OOOIFLIST: /* old _14 */
   2037 				error = rtsock_14_iflist_hook_call(ifp, w,
   2038 				    &info, len);
   2039 				break;
   2040 			default:
   2041 				error = EINVAL;
   2042 			}
   2043 			if (error != 0) {
   2044 				if (error == ENOSYS)
   2045 					error = EINVAL;
   2046 				goto release_exit;
   2047 			}
   2048 		}
   2049 		_s = pserialize_read_enter();
   2050 		IFADDR_READER_FOREACH(ifa, ifp) {
   2051 			struct psref _psref;
   2052 			if (af && af != ifa->ifa_addr->sa_family)
   2053 				continue;
   2054 			ifa_acquire(ifa, &_psref);
   2055 			pserialize_read_exit(_s);
   2056 
   2057 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
   2058 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   2059 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   2060 			switch (type) {
   2061 			case NET_RT_IFLIST:
   2062 				error = sysctl_iflist_addr(w, ifa, &info);
   2063 				break;
   2064 			case NET_RT_OIFLIST:
   2065 			case NET_RT_OOIFLIST:
   2066 			case NET_RT_OOOIFLIST:
   2067 				error = rtsock_70_iflist_hook_call(w, ifa,
   2068 				    &info);
   2069 				break;
   2070 			default:
   2071 				error = EINVAL;
   2072 			}
   2073 
   2074 			_s = pserialize_read_enter();
   2075 			ifa_release(ifa, &_psref);
   2076 			if (error != 0) {
   2077 				pserialize_read_exit(_s);
   2078 				goto release_exit;
   2079 			}
   2080 		}
   2081 		pserialize_read_exit(_s);
   2082 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
   2083 		    info.rti_info[RTAX_BRD] = NULL;
   2084 
   2085 		s = pserialize_read_enter();
   2086 		if_release(ifp, &psref);
   2087 	}
   2088 	pserialize_read_exit(s);
   2089 	curlwp_bindx(bound);
   2090 
   2091 	return 0;
   2092 
   2093 release_exit:
   2094 	if_release(ifp, &psref);
   2095 	curlwp_bindx(bound);
   2096 	return error;
   2097 }
   2098 
   2099 static int
   2100 sysctl_rtable(SYSCTLFN_ARGS)
   2101 {
   2102 	void 	*where = oldp;
   2103 	size_t	*given = oldlenp;
   2104 	int	i, s, error = EINVAL;
   2105 	u_char  af;
   2106 	struct	rt_walkarg w;
   2107 
   2108 	if (namelen == 1 && name[0] == CTL_QUERY)
   2109 		return sysctl_query(SYSCTLFN_CALL(rnode));
   2110 
   2111 	if (newp)
   2112 		return EPERM;
   2113 	if (namelen != 3)
   2114 		return EINVAL;
   2115 	af = name[0];
   2116 	w.w_tmemneeded = 0;
   2117 	w.w_tmemsize = 0;
   2118 	w.w_tmem = NULL;
   2119 again:
   2120 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   2121 	if (w.w_tmemneeded) {
   2122 		w.w_tmem = kmem_zalloc(w.w_tmemneeded, KM_SLEEP);
   2123 		w.w_tmemsize = w.w_tmemneeded;
   2124 		w.w_tmemneeded = 0;
   2125 	}
   2126 	w.w_op = name[1];
   2127 	w.w_arg = name[2];
   2128 	w.w_given = *given;
   2129 	w.w_needed = 0 - w.w_given;
   2130 	w.w_where = where;
   2131 
   2132 	s = splsoftnet();
   2133 	switch (w.w_op) {
   2134 
   2135 	case NET_RT_DUMP:
   2136 	case NET_RT_FLAGS:
   2137 #if defined(INET) || defined(INET6)
   2138 		/*
   2139 		 * take care of llinfo entries, the caller must
   2140 		 * specify an AF
   2141 		 */
   2142 		if (w.w_op == NET_RT_FLAGS &&
   2143 		    (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
   2144 			if (af != 0)
   2145 				error = lltable_sysctl_dump(af, &w);
   2146 			else
   2147 				error = EINVAL;
   2148 			break;
   2149 		}
   2150 #endif
   2151 
   2152 		for (i = 1; i <= AF_MAX; i++) {
   2153 			if (af == 0 || af == i) {
   2154 				error = rt_walktree(i, sysctl_dumpentry, &w);
   2155 				if (error != 0)
   2156 					break;
   2157 #if defined(INET) || defined(INET6)
   2158 				/*
   2159 				 * Return ARP/NDP entries too for
   2160 				 * backward compatibility.
   2161 				 */
   2162 				error = lltable_sysctl_dump(i, &w);
   2163 				if (error != 0)
   2164 					break;
   2165 #endif
   2166 			}
   2167 		}
   2168 		break;
   2169 
   2170 	case NET_RT_OOOIFLIST:		/* compat_14 */
   2171 	case NET_RT_OOIFLIST:		/* compat_50 */
   2172 	case NET_RT_OIFLIST:		/* compat_70 */
   2173 	case NET_RT_IFLIST:		/* current */
   2174 		error = sysctl_iflist(af, &w, w.w_op);
   2175 		break;
   2176 	}
   2177 	splx(s);
   2178 
   2179 	/* check to see if we couldn't allocate memory with NOWAIT */
   2180 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   2181 		goto again;
   2182 
   2183 	if (w.w_tmem)
   2184 		kmem_free(w.w_tmem, w.w_tmemsize);
   2185 	w.w_needed += w.w_given;
   2186 	if (where) {
   2187 		*given = (char *)w.w_where - (char *)where;
   2188 		if (*given < w.w_needed)
   2189 			return ENOMEM;
   2190 	} else {
   2191 		*given = (11 * w.w_needed) / 10;
   2192 	}
   2193 	return error;
   2194 }
   2195 
   2196 static void
   2197 sysctl_net_route_setup(struct sysctllog **clog)
   2198 {
   2199 	const struct sysctlnode *rnode = NULL;
   2200 
   2201 	sysctl_createv(clog, 0, NULL, &rnode,
   2202 		       CTLFLAG_PERMANENT,
   2203 		       CTLTYPE_NODE, DOMAINNAME,
   2204 		       SYSCTL_DESCR("PF_ROUTE information"),
   2205 		       NULL, 0, NULL, 0,
   2206 		       CTL_NET, PF_XROUTE, CTL_EOL);
   2207 
   2208 	sysctl_createv(clog, 0, NULL, NULL,
   2209 		       CTLFLAG_PERMANENT,
   2210 		       CTLTYPE_NODE, "rtable",
   2211 		       SYSCTL_DESCR("Routing table information"),
   2212 		       sysctl_rtable, 0, NULL, 0,
   2213 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
   2214 
   2215 	sysctl_createv(clog, 0, &rnode, NULL,
   2216 		       CTLFLAG_PERMANENT,
   2217 		       CTLTYPE_STRUCT, "stats",
   2218 		       SYSCTL_DESCR("Routing statistics"),
   2219 		       NULL, 0, &rtstat, sizeof(rtstat),
   2220 		       CTL_CREATE, CTL_EOL);
   2221 }
   2222 #endif	/* COMPAT_RTSOCK */
   2223