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