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