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