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