Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.216
      1 /*	$NetBSD: rtsock.c,v 1.216 2017/06/16 02:26:17 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.216 2017/06/16 02:26:17 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;
    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 		}
    695 		if (new_ifa == NULL)
    696 			ifa_release(ifa, &psref_ifa);
    697 	}
    698 	ifa_release(new_ifa, &psref_new_ifa);
    699 	if (new_ifp && rt->rt_ifp != new_ifp && !if_is_deactivated(new_ifp))
    700 		rt->rt_ifp = new_ifp;
    701 	rt_setmetrics(rtm->rtm_inits, rtm, rt);
    702 	if (rt->rt_flags != info->rti_flags) {
    703 		rt->rt_flags = (info->rti_flags & ~PRESERVED_RTF) |
    704 		    (rt->rt_flags & PRESERVED_RTF);
    705 	}
    706 	if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    707 		rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
    708 out:
    709 	if_put(ifp, &psref_ifp);
    710 
    711 	return error;
    712 }
    713 
    714 /*ARGSUSED*/
    715 int
    716 COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
    717 {
    718 	struct sockproto proto = { .sp_family = PF_XROUTE, };
    719 	struct rt_xmsghdr *rtm = NULL;
    720 	struct rt_xmsghdr *old_rtm = NULL, *new_rtm = NULL;
    721 	struct rtentry *rt = NULL;
    722 	struct rtentry *saved_nrt = NULL;
    723 	struct rt_addrinfo info;
    724 	int len, error = 0;
    725 	sa_family_t family;
    726 	struct sockaddr_dl sdl;
    727 	int bound = curlwp_bind();
    728 	bool do_rt_free = false;
    729 
    730 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
    731 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
    732 	   (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
    733 		error = ENOBUFS;
    734 		goto out;
    735 	}
    736 	if ((m->m_flags & M_PKTHDR) == 0)
    737 		panic("%s", __func__);
    738 	len = m->m_pkthdr.len;
    739 	if (len < sizeof(*rtm) ||
    740 	    len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
    741 		info.rti_info[RTAX_DST] = NULL;
    742 		senderr(EINVAL);
    743 	}
    744 	R_Malloc(rtm, struct rt_xmsghdr *, len);
    745 	if (rtm == NULL) {
    746 		info.rti_info[RTAX_DST] = NULL;
    747 		senderr(ENOBUFS);
    748 	}
    749 	m_copydata(m, 0, len, rtm);
    750 	if (rtm->rtm_version != RTM_XVERSION) {
    751 		info.rti_info[RTAX_DST] = NULL;
    752 		senderr(EPROTONOSUPPORT);
    753 	}
    754 	rtm->rtm_pid = curproc->p_pid;
    755 	memset(&info, 0, sizeof(info));
    756 	info.rti_addrs = rtm->rtm_addrs;
    757 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
    758 	    &info)) {
    759 		senderr(EINVAL);
    760 	}
    761 	info.rti_flags = rtm->rtm_flags;
    762 #ifdef RTSOCK_DEBUG
    763 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    764 		char abuf[INET_ADDRSTRLEN];
    765 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
    766 		    RT_IN_PRINT(&info, abuf, RTAX_DST));
    767 	}
    768 #endif /* RTSOCK_DEBUG */
    769 	if (info.rti_info[RTAX_DST] == NULL ||
    770 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
    771 		senderr(EINVAL);
    772 	}
    773 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
    774 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
    775 		senderr(EINVAL);
    776 	}
    777 
    778 	/*
    779 	 * Verify that the caller has the appropriate privilege; RTM_GET
    780 	 * is the only operation the non-superuser is allowed.
    781 	 */
    782 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
    783 	    0, rtm, NULL, NULL) != 0)
    784 		senderr(EACCES);
    785 
    786 	switch (rtm->rtm_type) {
    787 
    788 	case RTM_ADD:
    789 		if (info.rti_info[RTAX_GATEWAY] == NULL) {
    790 			senderr(EINVAL);
    791 		}
    792 #ifdef INET
    793 		/* support for new ARP code with keeping backcompat */
    794 		if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
    795 			const struct sockaddr_dl *sdlp =
    796 			    satocsdl(info.rti_info[RTAX_GATEWAY]);
    797 
    798 			/* Allow routing requests by interface index */
    799 			if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
    800 			    && sdlp->sdl_slen == 0)
    801 				goto fallback;
    802 			/*
    803 			 * Old arp binaries don't set the sdl_index
    804 			 * so we have to complement it.
    805 			 */
    806 			int sdl_index = sdlp->sdl_index;
    807 			if (sdl_index == 0) {
    808 				error = route_get_sdl_index(&info, &sdl_index);
    809 				if (error != 0)
    810 					goto fallback;
    811 			} else if (
    812 			    info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    813 				/*
    814 				 * XXX workaround for SIN_PROXY case; proxy arp
    815 				 * entry should be in an interface that has
    816 				 * a network route including the destination,
    817 				 * not a local (link) route that may not be a
    818 				 * desired place, for example a tap.
    819 				 */
    820 				const struct sockaddr_inarp *sina =
    821 				    (const struct sockaddr_inarp *)
    822 				    info.rti_info[RTAX_DST];
    823 				if (sina->sin_other & SIN_PROXY) {
    824 					error = route_get_sdl_index(&info,
    825 					    &sdl_index);
    826 					if (error != 0)
    827 						goto fallback;
    828 				}
    829 			}
    830 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
    831 			    rtm->rtm_rmx.rmx_expire, &info, sdl_index);
    832 			break;
    833 		}
    834 	fallback:
    835 #endif /* INET */
    836 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    837 		if (error == 0) {
    838 			rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
    839 			rt_unref(saved_nrt);
    840 		}
    841 		break;
    842 
    843 	case RTM_DELETE:
    844 #ifdef INET
    845 		/* support for new ARP code */
    846 		if (info.rti_info[RTAX_GATEWAY] &&
    847 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
    848 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
    849 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
    850 			    rtm->rtm_rmx.rmx_expire, &info, 0);
    851 			break;
    852 		}
    853 #endif /* INET */
    854 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    855 		if (error != 0)
    856 			break;
    857 
    858 		rt = saved_nrt;
    859 		do_rt_free = true;
    860 		info.rti_info[RTAX_DST] = rt_getkey(rt);
    861 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    862 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    863 		info.rti_info[RTAX_TAG] = rt_gettag(rt);
    864 		error = route_output_report(rt, &info, rtm, &new_rtm);
    865 		if (error)
    866 			senderr(error);
    867 		if (new_rtm != NULL) {
    868 			old_rtm = rtm;
    869 			rtm = new_rtm;
    870 		}
    871 		break;
    872 
    873 	case RTM_GET:
    874 	case RTM_CHANGE:
    875 	case RTM_LOCK:
    876                 /* XXX This will mask info.rti_info[RTAX_DST] with
    877 		 * info.rti_info[RTAX_NETMASK] before
    878                  * searching.  It did not used to do that.  --dyoung
    879 		 */
    880 		rt = NULL;
    881 		error = rtrequest1(RTM_GET, &info, &rt);
    882 		if (error != 0)
    883 			senderr(error);
    884 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
    885 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
    886 			    info.rti_info[RTAX_DST]->sa_len) != 0)
    887 				senderr(ESRCH);
    888 			if (info.rti_info[RTAX_NETMASK] == NULL &&
    889 			    rt_mask(rt) != NULL)
    890 				senderr(ETOOMANYREFS);
    891 		}
    892 
    893 		/*
    894 		 * XXX if arp/ndp requests an L2 entry, we have to obtain
    895 		 * it from lltable while for the route command we have to
    896 		 * return a route as it is. How to distinguish them?
    897 		 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
    898 		 * indicates an L2 entry is requested. For old arp/ndp
    899 		 * binaries, we check RTF_UP flag is NOT set; it works
    900 		 * by the fact that arp/ndp don't set it while the route
    901 		 * command sets it.
    902 		 */
    903 		if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
    904 		     (rtm->rtm_flags & RTF_UP) == 0) &&
    905 		    rtm->rtm_type == RTM_GET &&
    906 		    sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
    907 			int ll_flags = 0;
    908 			route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
    909 			    &ll_flags);
    910 			info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
    911 			error = route_output_report(rt, &info, rtm, &new_rtm);
    912 			if (error)
    913 				senderr(error);
    914 			if (new_rtm != NULL) {
    915 				old_rtm = rtm;
    916 				rtm = new_rtm;
    917 			}
    918 			rtm->rtm_flags |= RTF_LLDATA;
    919 			rtm->rtm_flags &= ~RTF_CONNECTED;
    920 			rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
    921 			break;
    922 		}
    923 
    924 		switch (rtm->rtm_type) {
    925 		case RTM_GET:
    926 			info.rti_info[RTAX_DST] = rt_getkey(rt);
    927 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    928 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    929 			info.rti_info[RTAX_TAG] = rt_gettag(rt);
    930 			error = route_output_report(rt, &info, rtm, &new_rtm);
    931 			if (error)
    932 				senderr(error);
    933 			if (new_rtm != NULL) {
    934 				old_rtm = rtm;
    935 				rtm = new_rtm;
    936 			}
    937 			break;
    938 
    939 		case RTM_CHANGE:
    940 #ifdef NET_MPSAFE
    941 			error = rt_update_prepare(rt);
    942 			if (error == 0) {
    943 				error = route_output_change(rt, &info, rtm);
    944 				rt_update_finish(rt);
    945 			}
    946 #else
    947 			error = route_output_change(rt, &info, rtm);
    948 #endif
    949 			if (error != 0)
    950 				goto flush;
    951 			/*FALLTHROUGH*/
    952 		case RTM_LOCK:
    953 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    954 			rt->rt_rmx.rmx_locks |=
    955 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    956 			break;
    957 		}
    958 		break;
    959 
    960 	default:
    961 		senderr(EOPNOTSUPP);
    962 	}
    963 
    964 flush:
    965 	if (rtm) {
    966 		if (error)
    967 			rtm->rtm_errno = error;
    968 		else
    969 			rtm->rtm_flags |= RTF_DONE;
    970 	}
    971 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
    972 	    0;
    973 	/* We cannot free old_rtm until we have stopped using the
    974 	 * pointers in info, some of which may point to sockaddrs
    975 	 * in old_rtm.
    976 	 */
    977 	if (old_rtm != NULL)
    978 		Free(old_rtm);
    979 	if (rt) {
    980 		if (do_rt_free)
    981 			rt_free(rt);
    982 		else
    983 			rt_unref(rt);
    984 	}
    985     {
    986 	struct rawcb *rp = NULL;
    987 	/*
    988 	 * Check to see if we don't want our own messages.
    989 	 */
    990 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    991 		if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
    992 			if (rtm)
    993 				Free(rtm);
    994 			m_freem(m);
    995 			goto out;
    996 		}
    997 		/* There is another listener, so construct message */
    998 		rp = sotorawcb(so);
    999 	}
   1000 	if (rtm) {
   1001 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
   1002 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
   1003 			m_freem(m);
   1004 			m = NULL;
   1005 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
   1006 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
   1007 		Free(rtm);
   1008 	}
   1009 	if (rp)
   1010 		rp->rcb_proto.sp_family = 0; /* Avoid us */
   1011 	if (family)
   1012 		proto.sp_protocol = family;
   1013 	if (m)
   1014 		raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
   1015 		    &COMPATNAME(route_info).ri_dst);
   1016 	if (rp)
   1017 		rp->rcb_proto.sp_family = PF_XROUTE;
   1018     }
   1019 out:
   1020 	curlwp_bindx(bound);
   1021 	return error;
   1022 }
   1023 
   1024 static int
   1025 route_ctloutput(int op, struct socket *so, struct sockopt *sopt)
   1026 {
   1027 	struct routecb *rop = sotoroutecb(so);
   1028 	int error = 0;
   1029 	unsigned char *rtm_type;
   1030 	size_t len;
   1031 	unsigned int msgfilter;
   1032 
   1033 	KASSERT(solocked(so));
   1034 
   1035 	if (sopt->sopt_level != AF_ROUTE) {
   1036 		error = ENOPROTOOPT;
   1037 	} else switch (op) {
   1038 	case PRCO_SETOPT:
   1039 		switch (sopt->sopt_name) {
   1040 		case RO_MSGFILTER:
   1041 			msgfilter = 0;
   1042 			for (rtm_type = sopt->sopt_data, len = sopt->sopt_size;
   1043 			     len != 0;
   1044 			     rtm_type++, len -= sizeof(*rtm_type))
   1045 			{
   1046 				/* Guard against overflowing our storage. */
   1047 				if (*rtm_type >= sizeof(msgfilter) * CHAR_BIT) {
   1048 					error = EOVERFLOW;
   1049 					break;
   1050 				}
   1051 				msgfilter |= RTMSGFILTER(*rtm_type);
   1052 			}
   1053 			if (error == 0)
   1054 				rop->rocb_msgfilter = msgfilter;
   1055 			break;
   1056 		default:
   1057 			error = ENOPROTOOPT;
   1058 			break;
   1059 		}
   1060 		break;
   1061 	case PRCO_GETOPT:
   1062 		switch (sopt->sopt_name) {
   1063 		case RO_MSGFILTER:
   1064 			error = ENOTSUP;
   1065 			break;
   1066 		default:
   1067 			error = ENOPROTOOPT;
   1068 			break;
   1069 		}
   1070 	}
   1071 	return error;
   1072 }
   1073 
   1074 static void
   1075 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
   1076 {
   1077 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
   1078 	metric(RTV_RPIPE, rmx_recvpipe);
   1079 	metric(RTV_SPIPE, rmx_sendpipe);
   1080 	metric(RTV_SSTHRESH, rmx_ssthresh);
   1081 	metric(RTV_RTT, rmx_rtt);
   1082 	metric(RTV_RTTVAR, rmx_rttvar);
   1083 	metric(RTV_HOPCOUNT, rmx_hopcount);
   1084 	metric(RTV_MTU, rmx_mtu);
   1085 #undef metric
   1086 	if (which & RTV_EXPIRE) {
   1087 		out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
   1088 		    time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
   1089 	}
   1090 }
   1091 
   1092 static void
   1093 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
   1094 {
   1095 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
   1096 	metric(rmx_recvpipe);
   1097 	metric(rmx_sendpipe);
   1098 	metric(rmx_ssthresh);
   1099 	metric(rmx_rtt);
   1100 	metric(rmx_rttvar);
   1101 	metric(rmx_hopcount);
   1102 	metric(rmx_mtu);
   1103 	metric(rmx_locks);
   1104 #undef metric
   1105 	out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
   1106 	    time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
   1107 }
   1108 
   1109 static int
   1110 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
   1111     struct rt_addrinfo *rtinfo)
   1112 {
   1113 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
   1114 	int i;
   1115 
   1116 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
   1117 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
   1118 			continue;
   1119 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
   1120 		RT_XADVANCE(cp, sa);
   1121 	}
   1122 
   1123 	/*
   1124 	 * Check for extra addresses specified, except RTM_GET asking
   1125 	 * for interface info.
   1126 	 */
   1127 	if (rtmtype == RTM_GET) {
   1128 		if (((rtinfo->rti_addrs &
   1129 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
   1130 			return 1;
   1131 	} else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
   1132 		return 1;
   1133 	/* Check for bad data length.  */
   1134 	if (cp != cplim) {
   1135 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
   1136 		    cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
   1137 			/*
   1138 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
   1139 			 * We accept this for now for the sake of old
   1140 			 * binaries or third party softwares.
   1141 			 */
   1142 			;
   1143 		else
   1144 			return 1;
   1145 	}
   1146 	return 0;
   1147 }
   1148 
   1149 static int
   1150 rt_getlen(int type)
   1151 {
   1152 #ifndef COMPAT_RTSOCK
   1153 	CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
   1154 	CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
   1155 	CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
   1156 	CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
   1157 #endif
   1158 
   1159 	switch (type) {
   1160 	case RTM_ODELADDR:
   1161 	case RTM_ONEWADDR:
   1162 	case RTM_OCHGADDR:
   1163 #ifdef COMPAT_70
   1164 		return sizeof(struct ifa_msghdr70);
   1165 #else
   1166 #ifdef RTSOCK_DEBUG
   1167 		printf("%s: unsupported RTM type %d\n", __func__, type);
   1168 #endif
   1169 		return -1;
   1170 #endif
   1171 	case RTM_DELADDR:
   1172 	case RTM_NEWADDR:
   1173 	case RTM_CHGADDR:
   1174 		return sizeof(struct ifa_xmsghdr);
   1175 
   1176 	case RTM_OOIFINFO:
   1177 #ifdef COMPAT_14
   1178 		return sizeof(struct if_msghdr14);
   1179 #else
   1180 #ifdef RTSOCK_DEBUG
   1181 		printf("%s: unsupported RTM type RTM_OOIFINFO\n", __func__);
   1182 #endif
   1183 		return -1;
   1184 #endif
   1185 	case RTM_OIFINFO:
   1186 #ifdef COMPAT_50
   1187 		return sizeof(struct if_msghdr50);
   1188 #else
   1189 #ifdef RTSOCK_DEBUG
   1190 		printf("%s: unsupported RTM type RTM_OIFINFO\n", __func__);
   1191 #endif
   1192 		return -1;
   1193 #endif
   1194 
   1195 	case RTM_IFINFO:
   1196 		return sizeof(struct if_xmsghdr);
   1197 
   1198 	case RTM_IFANNOUNCE:
   1199 	case RTM_IEEE80211:
   1200 		return sizeof(struct if_xannouncemsghdr);
   1201 
   1202 	default:
   1203 		return sizeof(struct rt_xmsghdr);
   1204 	}
   1205 }
   1206 
   1207 
   1208 struct mbuf *
   1209 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
   1210 {
   1211 	struct rt_xmsghdr *rtm;
   1212 	struct mbuf *m;
   1213 	int i;
   1214 	const struct sockaddr *sa;
   1215 	int len, dlen;
   1216 
   1217 	m = m_gethdr(M_DONTWAIT, MT_DATA);
   1218 	if (m == NULL)
   1219 		return m;
   1220 	MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
   1221 
   1222 	if ((len = rt_getlen(type)) == -1)
   1223 		goto out;
   1224 	if (len > MHLEN + MLEN)
   1225 		panic("%s: message too long", __func__);
   1226 	else if (len > MHLEN) {
   1227 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
   1228 		if (m->m_next == NULL)
   1229 			goto out;
   1230 		MCLAIM(m->m_next, m->m_owner);
   1231 		m->m_pkthdr.len = len;
   1232 		m->m_len = MHLEN;
   1233 		m->m_next->m_len = len - MHLEN;
   1234 	} else {
   1235 		m->m_pkthdr.len = m->m_len = len;
   1236 	}
   1237 	m_reset_rcvif(m);
   1238 	m_copyback(m, 0, datalen, data);
   1239 	if (len > datalen)
   1240 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
   1241 	rtm = mtod(m, struct rt_xmsghdr *);
   1242 	for (i = 0; i < RTAX_MAX; i++) {
   1243 		if ((sa = rtinfo->rti_info[i]) == NULL)
   1244 			continue;
   1245 		rtinfo->rti_addrs |= (1 << i);
   1246 		dlen = RT_XROUNDUP(sa->sa_len);
   1247 		m_copyback(m, len, sa->sa_len, sa);
   1248 		if (dlen != sa->sa_len) {
   1249 			/*
   1250 			 * Up to 6 + 1 nul's since roundup is to
   1251 			 * sizeof(uint64_t) (8 bytes)
   1252 			 */
   1253 			m_copyback(m, len + sa->sa_len,
   1254 			    dlen - sa->sa_len, "\0\0\0\0\0\0");
   1255 		}
   1256 		len += dlen;
   1257 	}
   1258 	if (m->m_pkthdr.len != len)
   1259 		goto out;
   1260 	rtm->rtm_msglen = len;
   1261 	rtm->rtm_version = RTM_XVERSION;
   1262 	rtm->rtm_type = type;
   1263 	return m;
   1264 out:
   1265 	m_freem(m);
   1266 	return NULL;
   1267 }
   1268 
   1269 /*
   1270  * rt_msg2
   1271  *
   1272  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
   1273  *		returns the length of the message in 'lenp'.
   1274  *
   1275  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
   1276  *	the message
   1277  * otherwise walkarg's w_needed is updated and if the user buffer is
   1278  *	specified and w_needed indicates space exists the information is copied
   1279  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
   1280  *	if the allocation fails ENOBUFS is returned.
   1281  */
   1282 static int
   1283 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
   1284 	int *lenp)
   1285 {
   1286 	int i;
   1287 	int len, dlen, second_time = 0;
   1288 	char *cp0, *cp = cpv;
   1289 
   1290 	rtinfo->rti_addrs = 0;
   1291 again:
   1292 	if ((len = rt_getlen(type)) == -1)
   1293 		return EINVAL;
   1294 
   1295 	if ((cp0 = cp) != NULL)
   1296 		cp += len;
   1297 	for (i = 0; i < RTAX_MAX; i++) {
   1298 		const struct sockaddr *sa;
   1299 
   1300 		if ((sa = rtinfo->rti_info[i]) == NULL)
   1301 			continue;
   1302 		rtinfo->rti_addrs |= (1 << i);
   1303 		dlen = RT_XROUNDUP(sa->sa_len);
   1304 		if (cp) {
   1305 			int diff = dlen - sa->sa_len;
   1306 			(void)memcpy(cp, sa, (size_t)sa->sa_len);
   1307 			cp += sa->sa_len;
   1308 			if (diff > 0) {
   1309 				(void)memset(cp, 0, (size_t)diff);
   1310 				cp += diff;
   1311 			}
   1312 		}
   1313 		len += dlen;
   1314 	}
   1315 	if (cp == NULL && w != NULL && !second_time) {
   1316 		struct rt_walkarg *rw = w;
   1317 
   1318 		rw->w_needed += len;
   1319 		if (rw->w_needed <= 0 && rw->w_where) {
   1320 			if (rw->w_tmemsize < len) {
   1321 				if (rw->w_tmem)
   1322 					kmem_free(rw->w_tmem, rw->w_tmemsize);
   1323 				rw->w_tmem = kmem_alloc(len, KM_SLEEP);
   1324 				rw->w_tmemsize = len;
   1325 			}
   1326 			if (rw->w_tmem) {
   1327 				cp = rw->w_tmem;
   1328 				second_time = 1;
   1329 				goto again;
   1330 			} else {
   1331 				rw->w_tmemneeded = len;
   1332 				return ENOBUFS;
   1333 			}
   1334 		}
   1335 	}
   1336 	if (cp) {
   1337 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
   1338 
   1339 		rtm->rtm_version = RTM_XVERSION;
   1340 		rtm->rtm_type = type;
   1341 		rtm->rtm_msglen = len;
   1342 	}
   1343 	if (lenp)
   1344 		*lenp = len;
   1345 	return 0;
   1346 }
   1347 
   1348 #ifndef COMPAT_RTSOCK
   1349 int
   1350 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
   1351 	int *lenp)
   1352 {
   1353 	return rt_msg2(type, rtinfo, cpv, w, lenp);
   1354 }
   1355 #endif
   1356 
   1357 /*
   1358  * This routine is called to generate a message from the routing
   1359  * socket indicating that a redirect has occurred, a routing lookup
   1360  * has failed, or that a protocol has detected timeouts to a particular
   1361  * destination.
   1362  */
   1363 void
   1364 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
   1365     int error)
   1366 {
   1367 	struct rt_xmsghdr rtm;
   1368 	struct mbuf *m;
   1369 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
   1370 	struct rt_addrinfo info = *rtinfo;
   1371 
   1372 	COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
   1373 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1374 		return;
   1375 	memset(&rtm, 0, sizeof(rtm));
   1376 	rtm.rtm_pid = curproc->p_pid;
   1377 	rtm.rtm_flags = RTF_DONE | flags;
   1378 	rtm.rtm_errno = error;
   1379 	m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
   1380 	if (m == NULL)
   1381 		return;
   1382 	mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
   1383 	COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
   1384 }
   1385 
   1386 /*
   1387  * This routine is called to generate a message from the routing
   1388  * socket indicating that the status of a network interface has changed.
   1389  */
   1390 void
   1391 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
   1392 {
   1393 	struct if_xmsghdr ifm;
   1394 	struct mbuf *m;
   1395 	struct rt_addrinfo info;
   1396 
   1397 	COMPATCALL(rt_ifmsg, (ifp));
   1398 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1399 		return;
   1400 	(void)memset(&info, 0, sizeof(info));
   1401 	(void)memset(&ifm, 0, sizeof(ifm));
   1402 	ifm.ifm_index = ifp->if_index;
   1403 	ifm.ifm_flags = ifp->if_flags;
   1404 	ifm.ifm_data = ifp->if_data;
   1405 	ifm.ifm_addrs = 0;
   1406 	m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
   1407 	if (m == NULL)
   1408 		return;
   1409 	COMPATNAME(route_enqueue)(m, 0);
   1410 #ifdef COMPAT_14
   1411 	compat_14_rt_oifmsg(ifp);
   1412 #endif
   1413 #ifdef COMPAT_50
   1414 	compat_50_rt_oifmsg(ifp);
   1415 #endif
   1416 }
   1417 
   1418 #ifndef COMPAT_RTSOCK
   1419 static int
   1420 if_addrflags(struct ifaddr *ifa)
   1421 {
   1422 
   1423 	switch (ifa->ifa_addr->sa_family) {
   1424 #ifdef INET
   1425 	case AF_INET:
   1426 		return ((struct in_ifaddr *)ifa)->ia4_flags;
   1427 #endif
   1428 #ifdef INET6
   1429 	case AF_INET6:
   1430 		return ((struct in6_ifaddr *)ifa)->ia6_flags;
   1431 #endif
   1432 	default:
   1433 		return 0;
   1434 	}
   1435 }
   1436 #endif
   1437 
   1438 /*
   1439  * This is called to generate messages from the routing socket
   1440  * indicating a network interface has had addresses associated with it.
   1441  * if we ever reverse the logic and replace messages TO the routing
   1442  * socket indicate a request to configure interfaces, then it will
   1443  * be unnecessary as the routing socket will automatically generate
   1444  * copies of it.
   1445  */
   1446 void
   1447 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
   1448     struct rtentry *rt)
   1449 {
   1450 #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
   1451 	struct rt_addrinfo info;
   1452 	const struct sockaddr *sa;
   1453 	int pass;
   1454 	struct mbuf *m;
   1455 	struct ifnet *ifp;
   1456 	struct rt_xmsghdr rtm;
   1457 	struct ifa_xmsghdr ifam;
   1458 	int ncmd;
   1459 
   1460 	KASSERT(ifa != NULL);
   1461 	KASSERT(ifa->ifa_addr != NULL);
   1462 	ifp = ifa->ifa_ifp;
   1463 #ifdef SCTP
   1464 	if (cmd == RTM_ADD) {
   1465 		sctp_add_ip_address(ifa);
   1466 	} else if (cmd == RTM_DELETE) {
   1467 		sctp_delete_ip_address(ifa);
   1468 	}
   1469 #endif
   1470 
   1471 	COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
   1472 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1473 		return;
   1474 	for (pass = 1; pass < 3; pass++) {
   1475 		memset(&info, 0, sizeof(info));
   1476 		switch (cmdpass(cmd, pass)) {
   1477 		case cmdpass(RTM_ADD, 1):
   1478 		case cmdpass(RTM_CHANGE, 1):
   1479 		case cmdpass(RTM_DELETE, 2):
   1480 		case cmdpass(RTM_NEWADDR, 1):
   1481 		case cmdpass(RTM_DELADDR, 1):
   1482 		case cmdpass(RTM_CHGADDR, 1):
   1483 			switch (cmd) {
   1484 			case RTM_ADD:
   1485 				ncmd = RTM_XNEWADDR;
   1486 				break;
   1487 			case RTM_DELETE:
   1488 				ncmd = RTM_XDELADDR;
   1489 				break;
   1490 			case RTM_CHANGE:
   1491 				ncmd = RTM_XCHGADDR;
   1492 				break;
   1493 			case RTM_NEWADDR:
   1494 				ncmd = RTM_XNEWADDR;
   1495 				break;
   1496 			case RTM_DELADDR:
   1497 				ncmd = RTM_XDELADDR;
   1498 				break;
   1499 			case RTM_CHGADDR:
   1500 				ncmd = RTM_XCHGADDR;
   1501 				break;
   1502 			default:
   1503 				panic("%s: unknown command %d", __func__, cmd);
   1504 			}
   1505 #ifdef COMPAT_70
   1506 			compat_70_rt_newaddrmsg1(ncmd, ifa);
   1507 #endif
   1508 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
   1509 			KASSERT(ifp->if_dl != NULL);
   1510 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   1511 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1512 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   1513 			memset(&ifam, 0, sizeof(ifam));
   1514 			ifam.ifam_index = ifp->if_index;
   1515 			ifam.ifam_metric = ifa->ifa_metric;
   1516 			ifam.ifam_flags = ifa->ifa_flags;
   1517 #ifndef COMPAT_RTSOCK
   1518 			ifam.ifam_pid = curproc->p_pid;
   1519 			ifam.ifam_addrflags = if_addrflags(ifa);
   1520 #endif
   1521 			m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
   1522 			if (m == NULL)
   1523 				continue;
   1524 			mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
   1525 			    info.rti_addrs;
   1526 			break;
   1527 		case cmdpass(RTM_ADD, 2):
   1528 		case cmdpass(RTM_CHANGE, 2):
   1529 		case cmdpass(RTM_DELETE, 1):
   1530 			if (rt == NULL)
   1531 				continue;
   1532 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1533 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
   1534 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1535 			memset(&rtm, 0, sizeof(rtm));
   1536 			rtm.rtm_pid = curproc->p_pid;
   1537 			rtm.rtm_index = ifp->if_index;
   1538 			rtm.rtm_flags |= rt->rt_flags;
   1539 			rtm.rtm_errno = error;
   1540 			m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
   1541 			if (m == NULL)
   1542 				continue;
   1543 			mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
   1544 			break;
   1545 		default:
   1546 			continue;
   1547 		}
   1548 		KASSERTMSG(m != NULL, "called with wrong command");
   1549 		COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
   1550 	}
   1551 #undef cmdpass
   1552 
   1553 }
   1554 
   1555 static struct mbuf *
   1556 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
   1557     struct rt_addrinfo *info)
   1558 {
   1559 	struct if_xannouncemsghdr ifan;
   1560 
   1561 	memset(info, 0, sizeof(*info));
   1562 	memset(&ifan, 0, sizeof(ifan));
   1563 	ifan.ifan_index = ifp->if_index;
   1564 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
   1565 	ifan.ifan_what = what;
   1566 	return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
   1567 }
   1568 
   1569 /*
   1570  * This is called to generate routing socket messages indicating
   1571  * network interface arrival and departure.
   1572  */
   1573 void
   1574 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
   1575 {
   1576 	struct mbuf *m;
   1577 	struct rt_addrinfo info;
   1578 
   1579 	COMPATCALL(rt_ifannouncemsg, (ifp, what));
   1580 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1581 		return;
   1582 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
   1583 	if (m == NULL)
   1584 		return;
   1585 	COMPATNAME(route_enqueue)(m, 0);
   1586 }
   1587 
   1588 /*
   1589  * This is called to generate routing socket messages indicating
   1590  * IEEE80211 wireless events.
   1591  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
   1592  */
   1593 void
   1594 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
   1595 	size_t data_len)
   1596 {
   1597 	struct mbuf *m;
   1598 	struct rt_addrinfo info;
   1599 
   1600 	COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
   1601 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1602 		return;
   1603 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
   1604 	if (m == NULL)
   1605 		return;
   1606 	/*
   1607 	 * Append the ieee80211 data.  Try to stick it in the
   1608 	 * mbuf containing the ifannounce msg; otherwise allocate
   1609 	 * a new mbuf and append.
   1610 	 *
   1611 	 * NB: we assume m is a single mbuf.
   1612 	 */
   1613 	if (data_len > M_TRAILINGSPACE(m)) {
   1614 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
   1615 		if (n == NULL) {
   1616 			m_freem(m);
   1617 			return;
   1618 		}
   1619 		(void)memcpy(mtod(n, void *), data, data_len);
   1620 		n->m_len = data_len;
   1621 		m->m_next = n;
   1622 	} else if (data_len > 0) {
   1623 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
   1624 		m->m_len += data_len;
   1625 	}
   1626 	if (m->m_flags & M_PKTHDR)
   1627 		m->m_pkthdr.len += data_len;
   1628 	mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
   1629 	COMPATNAME(route_enqueue)(m, 0);
   1630 }
   1631 
   1632 #ifndef COMPAT_RTSOCK
   1633 /*
   1634  * Send a routing message as mimicing that a cloned route is added.
   1635  */
   1636 void
   1637 rt_clonedmsg(const struct sockaddr *dst, const struct ifnet *ifp,
   1638     const struct rtentry *rt)
   1639 {
   1640 	struct rt_addrinfo info;
   1641 	/* Mimic flags exactly */
   1642 #define RTF_LLINFO	0x400
   1643 #define RTF_CLONED	0x2000
   1644 	int flags = RTF_UP | RTF_HOST | RTF_DONE | RTF_LLINFO | RTF_CLONED;
   1645 	union {
   1646 		struct sockaddr sa;
   1647 		struct sockaddr_storage ss;
   1648 		struct sockaddr_dl sdl;
   1649 	} u;
   1650 	uint8_t namelen = strlen(ifp->if_xname);
   1651 	uint8_t addrlen = ifp->if_addrlen;
   1652 
   1653 	if (rt == NULL)
   1654 		return; /* XXX */
   1655 
   1656 	memset(&info, 0, sizeof(info));
   1657 	info.rti_info[RTAX_DST] = dst;
   1658 	sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type,
   1659 	    NULL, namelen, NULL, addrlen);
   1660 	info.rti_info[RTAX_GATEWAY] = &u.sa;
   1661 	info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
   1662 	info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
   1663 
   1664 	rt_missmsg(RTM_ADD, &info, flags, 0);
   1665 #undef RTF_LLINFO
   1666 #undef RTF_CLONED
   1667 }
   1668 #endif /* COMPAT_RTSOCK */
   1669 
   1670 /*
   1671  * This is used in dumping the kernel table via sysctl().
   1672  */
   1673 static int
   1674 sysctl_dumpentry(struct rtentry *rt, void *v)
   1675 {
   1676 	struct rt_walkarg *w = v;
   1677 	int error = 0, size;
   1678 	struct rt_addrinfo info;
   1679 
   1680 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
   1681 		return 0;
   1682 	memset(&info, 0, sizeof(info));
   1683 	info.rti_info[RTAX_DST] = rt_getkey(rt);
   1684 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1685 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1686 	info.rti_info[RTAX_TAG] = rt_gettag(rt);
   1687 	if (rt->rt_ifp) {
   1688 		const struct ifaddr *rtifa;
   1689 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
   1690 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
   1691 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
   1692 		 * --dyoung
   1693 		 */
   1694 		rtifa = rt_get_ifa(rt);
   1695 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
   1696 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
   1697 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
   1698 	}
   1699 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
   1700 		return error;
   1701 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1702 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
   1703 
   1704 		rtm->rtm_flags = rt->rt_flags;
   1705 		rtm->rtm_use = rt->rt_use;
   1706 		rtm_setmetrics(rt, rtm);
   1707 		KASSERT(rt->rt_ifp != NULL);
   1708 		rtm->rtm_index = rt->rt_ifp->if_index;
   1709 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
   1710 		rtm->rtm_addrs = info.rti_addrs;
   1711 		if ((error = copyout(rtm, w->w_where, size)) != 0)
   1712 			w->w_where = NULL;
   1713 		else
   1714 			w->w_where = (char *)w->w_where + size;
   1715 	}
   1716 	return error;
   1717 }
   1718 
   1719 static int
   1720 sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
   1721     struct rt_addrinfo *info, size_t len)
   1722 {
   1723 	struct if_xmsghdr *ifm;
   1724 	int error;
   1725 
   1726 	ifm = (struct if_xmsghdr *)w->w_tmem;
   1727 	ifm->ifm_index = ifp->if_index;
   1728 	ifm->ifm_flags = ifp->if_flags;
   1729 	ifm->ifm_data = ifp->if_data;
   1730 	ifm->ifm_addrs = info->rti_addrs;
   1731 	if ((error = copyout(ifm, w->w_where, len)) == 0)
   1732 		w->w_where = (char *)w->w_where + len;
   1733 	return error;
   1734 }
   1735 
   1736 static int
   1737 sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
   1738      struct rt_addrinfo *info)
   1739 {
   1740 	int len, error;
   1741 
   1742 	if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
   1743 		return error;
   1744 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1745 		struct ifa_xmsghdr *ifam;
   1746 
   1747 		ifam = (struct ifa_xmsghdr *)w->w_tmem;
   1748 		ifam->ifam_index = ifa->ifa_ifp->if_index;
   1749 		ifam->ifam_flags = ifa->ifa_flags;
   1750 		ifam->ifam_metric = ifa->ifa_metric;
   1751 		ifam->ifam_addrs = info->rti_addrs;
   1752 #ifndef COMPAT_RTSOCK
   1753 		ifam->ifam_pid = 0;
   1754 		ifam->ifam_addrflags = if_addrflags(ifa);
   1755 #endif
   1756 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
   1757 			w->w_where = (char *)w->w_where + len;
   1758 	}
   1759 	return error;
   1760 }
   1761 
   1762 static int
   1763 sysctl_iflist(int af, struct rt_walkarg *w, int type)
   1764 {
   1765 	struct ifnet *ifp;
   1766 	struct ifaddr *ifa;
   1767 	struct	rt_addrinfo info;
   1768 	int	cmd, len, error = 0;
   1769 	int	(*iflist_if)(struct ifnet *, struct rt_walkarg *,
   1770 			     struct rt_addrinfo *, size_t);
   1771 	int	(*iflist_addr)(struct rt_walkarg *, struct ifaddr *,
   1772 			       struct rt_addrinfo *);
   1773 	int s;
   1774 	struct psref psref;
   1775 	int bound;
   1776 
   1777 	switch (type) {
   1778 	case NET_RT_IFLIST:
   1779 		cmd = RTM_IFINFO;
   1780 		iflist_if = sysctl_iflist_if;
   1781 		iflist_addr = sysctl_iflist_addr;
   1782 		break;
   1783 #ifdef COMPAT_14
   1784 	case NET_RT_OOOIFLIST:
   1785 		cmd = RTM_OOIFINFO;
   1786 		iflist_if = compat_14_iflist;
   1787 		iflist_addr = compat_70_iflist_addr;
   1788 		break;
   1789 #endif
   1790 #ifdef COMPAT_50
   1791 	case NET_RT_OOIFLIST:
   1792 		cmd = RTM_OIFINFO;
   1793 		iflist_if = compat_50_iflist;
   1794 		iflist_addr = compat_70_iflist_addr;
   1795 		break;
   1796 #endif
   1797 #ifdef COMPAT_70
   1798 	case NET_RT_OIFLIST:
   1799 		cmd = RTM_IFINFO;
   1800 		iflist_if = sysctl_iflist_if;
   1801 		iflist_addr = compat_70_iflist_addr;
   1802 		break;
   1803 #endif
   1804 	default:
   1805 #ifdef RTSOCK_DEBUG
   1806 		printf("%s: unsupported IFLIST type %d\n", __func__, type);
   1807 #endif
   1808 		return EINVAL;
   1809 	}
   1810 
   1811 	memset(&info, 0, sizeof(info));
   1812 
   1813 	bound = curlwp_bind();
   1814 	s = pserialize_read_enter();
   1815 	IFNET_READER_FOREACH(ifp) {
   1816 		int _s;
   1817 		if (w->w_arg && w->w_arg != ifp->if_index)
   1818 			continue;
   1819 		if (IFADDR_READER_EMPTY(ifp))
   1820 			continue;
   1821 
   1822 		if_acquire(ifp, &psref);
   1823 		pserialize_read_exit(s);
   1824 
   1825 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   1826 		if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
   1827 			goto release_exit;
   1828 		info.rti_info[RTAX_IFP] = NULL;
   1829 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1830 			if ((error = iflist_if(ifp, w, &info, len)) != 0)
   1831 				goto release_exit;
   1832 		}
   1833 		_s = pserialize_read_enter();
   1834 		IFADDR_READER_FOREACH(ifa, ifp) {
   1835 			struct psref _psref;
   1836 			if (af && af != ifa->ifa_addr->sa_family)
   1837 				continue;
   1838 			ifa_acquire(ifa, &_psref);
   1839 			pserialize_read_exit(_s);
   1840 
   1841 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
   1842 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1843 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   1844 			error = iflist_addr(w, ifa, &info);
   1845 
   1846 			_s = pserialize_read_enter();
   1847 			ifa_release(ifa, &_psref);
   1848 			if (error != 0) {
   1849 				pserialize_read_exit(_s);
   1850 				goto release_exit;
   1851 			}
   1852 		}
   1853 		pserialize_read_exit(_s);
   1854 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
   1855 		    info.rti_info[RTAX_BRD] = NULL;
   1856 
   1857 		s = pserialize_read_enter();
   1858 		if_release(ifp, &psref);
   1859 	}
   1860 	pserialize_read_exit(s);
   1861 	curlwp_bindx(bound);
   1862 
   1863 	return 0;
   1864 
   1865 release_exit:
   1866 	if_release(ifp, &psref);
   1867 	curlwp_bindx(bound);
   1868 	return error;
   1869 }
   1870 
   1871 static int
   1872 sysctl_rtable(SYSCTLFN_ARGS)
   1873 {
   1874 	void 	*where = oldp;
   1875 	size_t	*given = oldlenp;
   1876 	int	i, s, error = EINVAL;
   1877 	u_char  af;
   1878 	struct	rt_walkarg w;
   1879 
   1880 	if (namelen == 1 && name[0] == CTL_QUERY)
   1881 		return sysctl_query(SYSCTLFN_CALL(rnode));
   1882 
   1883 	if (newp)
   1884 		return EPERM;
   1885 	if (namelen != 3)
   1886 		return EINVAL;
   1887 	af = name[0];
   1888 	w.w_tmemneeded = 0;
   1889 	w.w_tmemsize = 0;
   1890 	w.w_tmem = NULL;
   1891 again:
   1892 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   1893 	if (w.w_tmemneeded) {
   1894 		w.w_tmem = kmem_alloc(w.w_tmemneeded, KM_SLEEP);
   1895 		w.w_tmemsize = w.w_tmemneeded;
   1896 		w.w_tmemneeded = 0;
   1897 	}
   1898 	w.w_op = name[1];
   1899 	w.w_arg = name[2];
   1900 	w.w_given = *given;
   1901 	w.w_needed = 0 - w.w_given;
   1902 	w.w_where = where;
   1903 
   1904 	s = splsoftnet();
   1905 	switch (w.w_op) {
   1906 
   1907 	case NET_RT_DUMP:
   1908 	case NET_RT_FLAGS:
   1909 #ifdef INET
   1910 		/*
   1911 		 * take care of llinfo entries, the caller must
   1912 		 * specify an AF
   1913 		 */
   1914 		if (w.w_op == NET_RT_FLAGS &&
   1915 		    (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
   1916 			if (af != 0)
   1917 				error = lltable_sysctl_dumparp(af, &w);
   1918 			else
   1919 				error = EINVAL;
   1920 			break;
   1921 		}
   1922 #endif /* INET */
   1923 
   1924 		for (i = 1; i <= AF_MAX; i++)
   1925 			if ((af == 0 || af == i) &&
   1926 			    (error = rt_walktree(i, sysctl_dumpentry, &w)))
   1927 				break;
   1928 		break;
   1929 
   1930 #ifdef COMPAT_14
   1931 	case NET_RT_OOOIFLIST:
   1932 		error = sysctl_iflist(af, &w, w.w_op);
   1933 		break;
   1934 #endif
   1935 #ifdef COMPAT_50
   1936 	case NET_RT_OOIFLIST:
   1937 		error = sysctl_iflist(af, &w, w.w_op);
   1938 		break;
   1939 #endif
   1940 #ifdef COMPAT_70
   1941 	case NET_RT_OIFLIST:
   1942 		error = sysctl_iflist(af, &w, w.w_op);
   1943 		break;
   1944 #endif
   1945 	case NET_RT_IFLIST:
   1946 		error = sysctl_iflist(af, &w, w.w_op);
   1947 		break;
   1948 	}
   1949 	splx(s);
   1950 
   1951 	/* check to see if we couldn't allocate memory with NOWAIT */
   1952 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   1953 		goto again;
   1954 
   1955 	if (w.w_tmem)
   1956 		kmem_free(w.w_tmem, w.w_tmemsize);
   1957 	w.w_needed += w.w_given;
   1958 	if (where) {
   1959 		*given = (char *)w.w_where - (char *)where;
   1960 		if (*given < w.w_needed)
   1961 			return ENOMEM;
   1962 	} else {
   1963 		*given = (11 * w.w_needed) / 10;
   1964 	}
   1965 	return error;
   1966 }
   1967 
   1968 /*
   1969  * Routing message software interrupt routine
   1970  */
   1971 static void
   1972 COMPATNAME(route_intr)(void *cookie)
   1973 {
   1974 	struct sockproto proto = { .sp_family = PF_XROUTE, };
   1975 	struct route_info * const ri = &COMPATNAME(route_info);
   1976 	struct mbuf *m;
   1977 
   1978 	mutex_enter(softnet_lock);
   1979 	KERNEL_LOCK(1, NULL);
   1980 	for (;;) {
   1981 		IFQ_LOCK(&ri->ri_intrq);
   1982 		IF_DEQUEUE(&ri->ri_intrq, m);
   1983 		IFQ_UNLOCK(&ri->ri_intrq);
   1984 		if (m == NULL)
   1985 			break;
   1986 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
   1987 		raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
   1988 	}
   1989 	KERNEL_UNLOCK_ONE(NULL);
   1990 	mutex_exit(softnet_lock);
   1991 }
   1992 
   1993 /*
   1994  * Enqueue a message to the software interrupt routine.
   1995  */
   1996 void
   1997 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
   1998 {
   1999 	struct route_info * const ri = &COMPATNAME(route_info);
   2000 	int wasempty;
   2001 
   2002 	IFQ_LOCK(&ri->ri_intrq);
   2003 	if (IF_QFULL(&ri->ri_intrq)) {
   2004 		IF_DROP(&ri->ri_intrq);
   2005 		IFQ_UNLOCK(&ri->ri_intrq);
   2006 		m_freem(m);
   2007 	} else {
   2008 		wasempty = IF_IS_EMPTY(&ri->ri_intrq);
   2009 		M_SETCTX(m, (uintptr_t)family);
   2010 		IF_ENQUEUE(&ri->ri_intrq, m);
   2011 		IFQ_UNLOCK(&ri->ri_intrq);
   2012 		if (wasempty) {
   2013 			kpreempt_disable();
   2014 			softint_schedule(ri->ri_sih);
   2015 			kpreempt_enable();
   2016 		}
   2017 	}
   2018 }
   2019 
   2020 static void
   2021 COMPATNAME(route_init)(void)
   2022 {
   2023 	struct route_info * const ri = &COMPATNAME(route_info);
   2024 
   2025 #ifndef COMPAT_RTSOCK
   2026 	rt_init();
   2027 #endif
   2028 
   2029 	sysctl_net_route_setup(NULL);
   2030 	ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
   2031 	ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   2032 	    COMPATNAME(route_intr), NULL);
   2033 	IFQ_LOCK_INIT(&ri->ri_intrq);
   2034 }
   2035 
   2036 /*
   2037  * Definitions of protocols supported in the ROUTE domain.
   2038  */
   2039 #ifndef COMPAT_RTSOCK
   2040 PR_WRAP_USRREQS(route);
   2041 #else
   2042 PR_WRAP_USRREQS(compat_50_route);
   2043 #endif
   2044 
   2045 static const struct pr_usrreqs route_usrreqs = {
   2046 	.pr_attach	= COMPATNAME(route_attach_wrapper),
   2047 	.pr_detach	= COMPATNAME(route_detach_wrapper),
   2048 	.pr_accept	= COMPATNAME(route_accept_wrapper),
   2049 	.pr_bind	= COMPATNAME(route_bind_wrapper),
   2050 	.pr_listen	= COMPATNAME(route_listen_wrapper),
   2051 	.pr_connect	= COMPATNAME(route_connect_wrapper),
   2052 	.pr_connect2	= COMPATNAME(route_connect2_wrapper),
   2053 	.pr_disconnect	= COMPATNAME(route_disconnect_wrapper),
   2054 	.pr_shutdown	= COMPATNAME(route_shutdown_wrapper),
   2055 	.pr_abort	= COMPATNAME(route_abort_wrapper),
   2056 	.pr_ioctl	= COMPATNAME(route_ioctl_wrapper),
   2057 	.pr_stat	= COMPATNAME(route_stat_wrapper),
   2058 	.pr_peeraddr	= COMPATNAME(route_peeraddr_wrapper),
   2059 	.pr_sockaddr	= COMPATNAME(route_sockaddr_wrapper),
   2060 	.pr_rcvd	= COMPATNAME(route_rcvd_wrapper),
   2061 	.pr_recvoob	= COMPATNAME(route_recvoob_wrapper),
   2062 	.pr_send	= COMPATNAME(route_send_wrapper),
   2063 	.pr_sendoob	= COMPATNAME(route_sendoob_wrapper),
   2064 	.pr_purgeif	= COMPATNAME(route_purgeif_wrapper),
   2065 };
   2066 
   2067 static const struct protosw COMPATNAME(route_protosw)[] = {
   2068 	{
   2069 		.pr_type = SOCK_RAW,
   2070 		.pr_domain = &COMPATNAME(routedomain),
   2071 		.pr_flags = PR_ATOMIC|PR_ADDR,
   2072 		.pr_input = raw_input,
   2073 		.pr_ctlinput = raw_ctlinput,
   2074 		.pr_ctloutput = route_ctloutput,
   2075 		.pr_usrreqs = &route_usrreqs,
   2076 		.pr_init = raw_init,
   2077 	},
   2078 };
   2079 
   2080 struct domain COMPATNAME(routedomain) = {
   2081 	.dom_family = PF_XROUTE,
   2082 	.dom_name = DOMAINNAME,
   2083 	.dom_init = COMPATNAME(route_init),
   2084 	.dom_protosw = COMPATNAME(route_protosw),
   2085 	.dom_protoswNPROTOSW =
   2086 	    &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
   2087 };
   2088 
   2089 static void
   2090 sysctl_net_route_setup(struct sysctllog **clog)
   2091 {
   2092 	const struct sysctlnode *rnode = NULL;
   2093 
   2094 	sysctl_createv(clog, 0, NULL, &rnode,
   2095 		       CTLFLAG_PERMANENT,
   2096 		       CTLTYPE_NODE, DOMAINNAME,
   2097 		       SYSCTL_DESCR("PF_ROUTE information"),
   2098 		       NULL, 0, NULL, 0,
   2099 		       CTL_NET, PF_XROUTE, CTL_EOL);
   2100 
   2101 	sysctl_createv(clog, 0, NULL, NULL,
   2102 		       CTLFLAG_PERMANENT,
   2103 		       CTLTYPE_NODE, "rtable",
   2104 		       SYSCTL_DESCR("Routing table information"),
   2105 		       sysctl_rtable, 0, NULL, 0,
   2106 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
   2107 
   2108 	sysctl_createv(clog, 0, &rnode, NULL,
   2109 		       CTLFLAG_PERMANENT,
   2110 		       CTLTYPE_STRUCT, "stats",
   2111 		       SYSCTL_DESCR("Routing statistics"),
   2112 		       NULL, 0, &rtstat, sizeof(rtstat),
   2113 		       CTL_CREATE, CTL_EOL);
   2114 }
   2115