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