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