Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.146
      1 /*	$NetBSD: rtsock.c,v 1.146 2014/05/20 19:04:00 rmind 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.146 2014/05/20 19:04:00 rmind Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_inet.h"
     68 #include "opt_mpls.h"
     69 #include "opt_compat_netbsd.h"
     70 #endif
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/proc.h>
     75 #include <sys/socket.h>
     76 #include <sys/socketvar.h>
     77 #include <sys/domain.h>
     78 #include <sys/protosw.h>
     79 #include <sys/sysctl.h>
     80 #include <sys/kauth.h>
     81 #include <sys/kmem.h>
     82 #include <sys/intr.h>
     83 #ifdef RTSOCK_DEBUG
     84 #include <netinet/in.h>
     85 #endif /* RTSOCK_DEBUG */
     86 
     87 #include <net/if.h>
     88 #include <net/route.h>
     89 #include <net/raw_cb.h>
     90 
     91 #include <netmpls/mpls.h>
     92 
     93 #if defined(COMPAT_14) || defined(COMPAT_50)
     94 #include <compat/net/if.h>
     95 #include <compat/net/route.h>
     96 #endif
     97 #ifdef COMPAT_RTSOCK
     98 #define	RTM_XVERSION	RTM_OVERSION
     99 #define	RT_XADVANCE(a,b) RT_OADVANCE(a,b)
    100 #define	RT_XROUNDUP(n)	RT_OROUNDUP(n)
    101 #define	PF_XROUTE	PF_OROUTE
    102 #define	rt_xmsghdr	rt_msghdr50
    103 #define	if_xmsghdr	if_msghdr	/* if_msghdr50 is for RTM_OIFINFO */
    104 #define	ifa_xmsghdr	ifa_msghdr50
    105 #define	if_xannouncemsghdr	if_announcemsghdr50
    106 #define	COMPATNAME(x)	compat_50_ ## x
    107 #define	DOMAINNAME	"oroute"
    108 CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
    109 DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
    110 #else
    111 #define	RTM_XVERSION	RTM_VERSION
    112 #define	RT_XADVANCE(a,b) RT_ADVANCE(a,b)
    113 #define	RT_XROUNDUP(n)	RT_ROUNDUP(n)
    114 #define	PF_XROUTE	PF_ROUTE
    115 #define	rt_xmsghdr	rt_msghdr
    116 #define	if_xmsghdr	if_msghdr
    117 #define	ifa_xmsghdr	ifa_msghdr
    118 #define	if_xannouncemsghdr	if_announcemsghdr
    119 #define	COMPATNAME(x)	x
    120 #define	DOMAINNAME	"route"
    121 CTASSERT(sizeof(struct ifa_xmsghdr) == 24);
    122 #ifdef COMPAT_50
    123 #define	COMPATCALL(name, args)	compat_50_ ## name args
    124 #endif
    125 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
    126 #undef COMPAT_50
    127 #undef COMPAT_14
    128 #endif
    129 
    130 #ifndef COMPATCALL
    131 #define	COMPATCALL(name, args)	do { } while (/*CONSTCOND*/ 0)
    132 #endif
    133 
    134 struct route_info COMPATNAME(route_info) = {
    135 	.ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
    136 	.ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
    137 	.ri_maxqlen = IFQ_MAXLEN,
    138 };
    139 
    140 #define	PRESERVED_RTF	(RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK)
    141 
    142 static void COMPATNAME(route_init)(void);
    143 static int COMPATNAME(route_output)(struct mbuf *, ...);
    144 
    145 static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
    146 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
    147 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
    148     struct rt_addrinfo *);
    149 static void rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
    150 static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
    151 static void sysctl_net_route_setup(struct sysctllog **);
    152 static int sysctl_dumpentry(struct rtentry *, void *);
    153 static int sysctl_iflist(int, struct rt_walkarg *, int);
    154 static int sysctl_rtable(SYSCTLFN_PROTO);
    155 static void rt_adjustcount(int, int);
    156 
    157 static void
    158 rt_adjustcount(int af, int cnt)
    159 {
    160 	struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
    161 
    162 	cb->any_count += cnt;
    163 
    164 	switch (af) {
    165 	case AF_INET:
    166 		cb->ip_count += cnt;
    167 		return;
    168 #ifdef INET6
    169 	case AF_INET6:
    170 		cb->ip6_count += cnt;
    171 		return;
    172 #endif
    173 	case AF_MPLS:
    174 		cb->mpls_count += cnt;
    175 		return;
    176 	}
    177 }
    178 
    179 static int
    180 COMPATNAME(route_attach)(struct socket *so, int proto)
    181 {
    182 	struct rawcb *rp;
    183 	int s, error;
    184 
    185 	KASSERT(sotorawcb(so) == NULL);
    186 	rp = kmem_zalloc(sizeof(*rp), KM_SLEEP);
    187 	so->so_pcb = rp;
    188 
    189 	s = splsoftnet();
    190 	if ((error = raw_attach(so, proto)) == 0) {
    191 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    192 		rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
    193 		rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
    194 	}
    195 	splx(s);
    196 
    197 	if (error) {
    198 		kmem_free(rp, sizeof(*rp));
    199 		so->so_pcb = NULL;
    200 		return error;
    201 	}
    202 
    203 	soisconnected(so);
    204 	so->so_options |= SO_USELOOPBACK;
    205 	KASSERT(solocked(so));
    206 
    207 	return error;
    208 }
    209 
    210 static void
    211 COMPATNAME(route_detach)(struct socket *so)
    212 {
    213 	struct rawcb *rp = sotorawcb(so);
    214 	int s;
    215 
    216 	KASSERT(rp != NULL);
    217 	KASSERT(solocked(so));
    218 
    219 	s = splsoftnet();
    220 	rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    221 	raw_detach(so);
    222 	splx(s);
    223 }
    224 
    225 static int
    226 COMPATNAME(route_usrreq)(struct socket *so, int req, struct mbuf *m,
    227     struct mbuf *nam, struct mbuf *control, struct lwp *l)
    228 {
    229 	int s, error = 0;
    230 
    231 	KASSERT(req != PRU_ATTACH);
    232 	KASSERT(req != PRU_DETACH);
    233 
    234 	s = splsoftnet();
    235 	error = raw_usrreq(so, req, m, nam, control, l);
    236 	splx(s);
    237 
    238 	return error;
    239 }
    240 
    241 /*ARGSUSED*/
    242 int
    243 COMPATNAME(route_output)(struct mbuf *m, ...)
    244 {
    245 	struct sockproto proto = { .sp_family = PF_XROUTE, };
    246 	struct rt_xmsghdr *rtm = NULL;
    247 	struct rt_xmsghdr *old_rtm = NULL;
    248 	struct rtentry *rt = NULL;
    249 	struct rtentry *saved_nrt = NULL;
    250 	struct rt_addrinfo info;
    251 	int len, error = 0;
    252 	struct ifnet *ifp = NULL;
    253 	struct ifaddr *ifa = NULL;
    254 	struct socket *so;
    255 	va_list ap;
    256 	sa_family_t family;
    257 
    258 	va_start(ap, m);
    259 	so = va_arg(ap, struct socket *);
    260 	va_end(ap);
    261 
    262 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
    263 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
    264 	   (m = m_pullup(m, sizeof(int32_t))) == NULL))
    265 		return ENOBUFS;
    266 	if ((m->m_flags & M_PKTHDR) == 0)
    267 		panic("%s", __func__);
    268 	len = m->m_pkthdr.len;
    269 	if (len < sizeof(*rtm) ||
    270 	    len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
    271 		info.rti_info[RTAX_DST] = NULL;
    272 		senderr(EINVAL);
    273 	}
    274 	R_Malloc(rtm, struct rt_xmsghdr *, len);
    275 	if (rtm == NULL) {
    276 		info.rti_info[RTAX_DST] = NULL;
    277 		senderr(ENOBUFS);
    278 	}
    279 	m_copydata(m, 0, len, rtm);
    280 	if (rtm->rtm_version != RTM_XVERSION) {
    281 		info.rti_info[RTAX_DST] = NULL;
    282 		senderr(EPROTONOSUPPORT);
    283 	}
    284 	rtm->rtm_pid = curproc->p_pid;
    285 	memset(&info, 0, sizeof(info));
    286 	info.rti_addrs = rtm->rtm_addrs;
    287 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
    288 	    &info)) {
    289 		senderr(EINVAL);
    290 	}
    291 	info.rti_flags = rtm->rtm_flags;
    292 #ifdef RTSOCK_DEBUG
    293 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    294 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
    295 		    inet_ntoa(((const struct sockaddr_in *)
    296 		    info.rti_info[RTAX_DST])->sin_addr));
    297 	}
    298 #endif /* RTSOCK_DEBUG */
    299 	if (info.rti_info[RTAX_DST] == NULL ||
    300 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
    301 		senderr(EINVAL);
    302 	}
    303 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
    304 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
    305 		senderr(EINVAL);
    306 	}
    307 
    308 	/*
    309 	 * Verify that the caller has the appropriate privilege; RTM_GET
    310 	 * is the only operation the non-superuser is allowed.
    311 	 */
    312 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
    313 	    0, rtm, NULL, NULL) != 0)
    314 		senderr(EACCES);
    315 
    316 	switch (rtm->rtm_type) {
    317 
    318 	case RTM_ADD:
    319 		if (info.rti_info[RTAX_GATEWAY] == NULL) {
    320 			senderr(EINVAL);
    321 		}
    322 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    323 		if (error == 0 && saved_nrt) {
    324 			rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
    325 			saved_nrt->rt_refcnt--;
    326 		}
    327 		break;
    328 
    329 	case RTM_DELETE:
    330 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    331 		if (error == 0) {
    332 			(rt = saved_nrt)->rt_refcnt++;
    333 			goto report;
    334 		}
    335 		break;
    336 
    337 	case RTM_GET:
    338 	case RTM_CHANGE:
    339 	case RTM_LOCK:
    340                 /* XXX This will mask info.rti_info[RTAX_DST] with
    341 		 * info.rti_info[RTAX_NETMASK] before
    342                  * searching.  It did not used to do that.  --dyoung
    343 		 */
    344 		error = rtrequest1(RTM_GET, &info, &rt);
    345 		if (error != 0)
    346 			senderr(error);
    347 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
    348 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
    349 			    info.rti_info[RTAX_DST]->sa_len) != 0)
    350 				senderr(ESRCH);
    351 			if (info.rti_info[RTAX_NETMASK] == NULL &&
    352 			    rt_mask(rt) != NULL)
    353 				senderr(ETOOMANYREFS);
    354 		}
    355 
    356 		switch (rtm->rtm_type) {
    357 		case RTM_GET:
    358 		report:
    359 			info.rti_info[RTAX_DST] = rt_getkey(rt);
    360 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    361 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    362 			info.rti_info[RTAX_TAG] = rt_gettag(rt);
    363 			if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
    364 				;
    365 			else if ((ifp = rt->rt_ifp) != NULL) {
    366 				const struct ifaddr *rtifa;
    367 				info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    368                                 /* rtifa used to be simply rt->rt_ifa.
    369                                  * If rt->rt_ifa != NULL, then
    370                                  * rt_get_ifa() != NULL.  So this
    371                                  * ought to still be safe. --dyoung
    372 				 */
    373 				rtifa = rt_get_ifa(rt);
    374 				info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
    375 #ifdef RTSOCK_DEBUG
    376 				if (info.rti_info[RTAX_IFA]->sa_family ==
    377 				    AF_INET) {
    378 					printf("%s: copying out RTAX_IFA %s ",
    379 					    __func__, inet_ntoa(
    380 					    ((const struct sockaddr_in *)
    381 					    info.rti_info[RTAX_IFA])->sin_addr)
    382 					    );
    383 					printf("for info.rti_info[RTAX_DST] %s "
    384 					    "ifa_getifa %p ifa_seqno %p\n",
    385 					    inet_ntoa(
    386 					    ((const struct sockaddr_in *)
    387 					    info.rti_info[RTAX_DST])->sin_addr),
    388 					    (void *)rtifa->ifa_getifa,
    389 					    rtifa->ifa_seqno);
    390 				}
    391 #endif /* RTSOCK_DEBUG */
    392 				if (ifp->if_flags & IFF_POINTOPOINT) {
    393 					info.rti_info[RTAX_BRD] =
    394 					    rtifa->ifa_dstaddr;
    395 				} else
    396 					info.rti_info[RTAX_BRD] = NULL;
    397 				rtm->rtm_index = ifp->if_index;
    398 			} else {
    399 				info.rti_info[RTAX_IFP] = NULL;
    400 				info.rti_info[RTAX_IFA] = NULL;
    401 			}
    402 			(void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
    403 			if (len > rtm->rtm_msglen) {
    404 				old_rtm = rtm;
    405 				R_Malloc(rtm, struct rt_xmsghdr *, len);
    406 				if (rtm == NULL)
    407 					senderr(ENOBUFS);
    408 				(void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
    409 			}
    410 			(void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
    411 			rtm->rtm_flags = rt->rt_flags;
    412 			rtm_setmetrics(rt, rtm);
    413 			rtm->rtm_addrs = info.rti_addrs;
    414 			break;
    415 
    416 		case RTM_CHANGE:
    417 			/*
    418 			 * new gateway could require new ifaddr, ifp;
    419 			 * flags may also be different; ifp may be specified
    420 			 * by ll sockaddr when protocol address is ambiguous
    421 			 */
    422 			if ((error = rt_getifa(&info)) != 0)
    423 				senderr(error);
    424 			if (info.rti_info[RTAX_GATEWAY] &&
    425 			    rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
    426 				senderr(EDQUOT);
    427 			if (info.rti_info[RTAX_TAG])
    428 				rt_settag(rt, info.rti_info[RTAX_TAG]);
    429 			/* new gateway could require new ifaddr, ifp;
    430 			   flags may also be different; ifp may be specified
    431 			   by ll sockaddr when protocol address is ambiguous */
    432 			if (info.rti_info[RTAX_IFP] &&
    433 			    (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
    434 			    (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
    435 			    info.rti_info[RTAX_GATEWAY])) {
    436 				if (info.rti_info[RTAX_IFA] == NULL ||
    437 				    (ifa = ifa_ifwithaddr(
    438 				    info.rti_info[RTAX_IFA])) == NULL)
    439 					ifa = ifaof_ifpforaddr(
    440 					    info.rti_info[RTAX_IFA] ?
    441 					    info.rti_info[RTAX_IFA] :
    442 					    info.rti_info[RTAX_GATEWAY], ifp);
    443 			} else if ((info.rti_info[RTAX_IFA] &&
    444 			    (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
    445 			    (info.rti_info[RTAX_GATEWAY] &&
    446 			    (ifa = ifa_ifwithroute(rt->rt_flags,
    447 			    rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
    448 				ifp = ifa->ifa_ifp;
    449 			}
    450 			if (ifa) {
    451 				struct ifaddr *oifa = rt->rt_ifa;
    452 				if (oifa != ifa) {
    453 					if (oifa && oifa->ifa_rtrequest) {
    454 						oifa->ifa_rtrequest(RTM_DELETE,
    455 						    rt, &info);
    456 					}
    457 					rt_replace_ifa(rt, ifa);
    458 					rt->rt_ifp = ifp;
    459 				}
    460 			}
    461 			if (ifp && rt->rt_ifp != ifp)
    462 				rt->rt_ifp = ifp;
    463 			rt_setmetrics(rtm->rtm_inits, rtm, rt);
    464 			if (rt->rt_flags != info.rti_flags)
    465 				rt->rt_flags = (info.rti_flags & ~PRESERVED_RTF)
    466 				    | (rt->rt_flags & PRESERVED_RTF);
    467 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    468 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
    469 			/*FALLTHROUGH*/
    470 		case RTM_LOCK:
    471 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    472 			rt->rt_rmx.rmx_locks |=
    473 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    474 			break;
    475 		}
    476 		break;
    477 
    478 	default:
    479 		senderr(EOPNOTSUPP);
    480 	}
    481 
    482 flush:
    483 	if (rtm) {
    484 		if (error)
    485 			rtm->rtm_errno = error;
    486 		else
    487 			rtm->rtm_flags |= RTF_DONE;
    488 	}
    489 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
    490 	    0;
    491 	/* We cannot free old_rtm until we have stopped using the
    492 	 * pointers in info, some of which may point to sockaddrs
    493 	 * in old_rtm.
    494 	 */
    495 	if (old_rtm != NULL)
    496 		Free(old_rtm);
    497 	if (rt)
    498 		rtfree(rt);
    499     {
    500 	struct rawcb *rp = NULL;
    501 	/*
    502 	 * Check to see if we don't want our own messages.
    503 	 */
    504 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    505 		if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
    506 			if (rtm)
    507 				Free(rtm);
    508 			m_freem(m);
    509 			return error;
    510 		}
    511 		/* There is another listener, so construct message */
    512 		rp = sotorawcb(so);
    513 	}
    514 	if (rtm) {
    515 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
    516 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
    517 			m_freem(m);
    518 			m = NULL;
    519 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
    520 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
    521 		Free(rtm);
    522 	}
    523 	if (rp)
    524 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    525 	if (family)
    526 		proto.sp_protocol = family;
    527 	if (m)
    528 		raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
    529 		    &COMPATNAME(route_info).ri_dst);
    530 	if (rp)
    531 		rp->rcb_proto.sp_family = PF_XROUTE;
    532     }
    533 	return error;
    534 }
    535 
    536 static void
    537 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
    538 {
    539 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
    540 	metric(RTV_RPIPE, rmx_recvpipe);
    541 	metric(RTV_SPIPE, rmx_sendpipe);
    542 	metric(RTV_SSTHRESH, rmx_ssthresh);
    543 	metric(RTV_RTT, rmx_rtt);
    544 	metric(RTV_RTTVAR, rmx_rttvar);
    545 	metric(RTV_HOPCOUNT, rmx_hopcount);
    546 	metric(RTV_MTU, rmx_mtu);
    547 	metric(RTV_EXPIRE, rmx_expire);
    548 #undef metric
    549 }
    550 
    551 static void
    552 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
    553 {
    554 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
    555 	metric(rmx_recvpipe);
    556 	metric(rmx_sendpipe);
    557 	metric(rmx_ssthresh);
    558 	metric(rmx_rtt);
    559 	metric(rmx_rttvar);
    560 	metric(rmx_hopcount);
    561 	metric(rmx_mtu);
    562 	metric(rmx_expire);
    563 #undef metric
    564 }
    565 
    566 static int
    567 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
    568     struct rt_addrinfo *rtinfo)
    569 {
    570 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
    571 	int i;
    572 
    573 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
    574 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    575 			continue;
    576 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
    577 		RT_XADVANCE(cp, sa);
    578 	}
    579 
    580 	/*
    581 	 * Check for extra addresses specified, except RTM_GET asking
    582 	 * for interface info.
    583 	 */
    584 	if (rtmtype == RTM_GET) {
    585 		if (((rtinfo->rti_addrs &
    586 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
    587 			return 1;
    588 	} else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
    589 		return 1;
    590 	/* Check for bad data length.  */
    591 	if (cp != cplim) {
    592 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
    593 		    cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
    594 			/*
    595 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
    596 			 * We accept this for now for the sake of old
    597 			 * binaries or third party softwares.
    598 			 */
    599 			;
    600 		else
    601 			return 1;
    602 	}
    603 	return 0;
    604 }
    605 
    606 static int
    607 rt_getlen(int type)
    608 {
    609 #ifndef COMPAT_RTSOCK
    610 	CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
    611 	CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
    612 	CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
    613 	CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
    614 #endif
    615 
    616 	switch (type) {
    617 	case RTM_DELADDR:
    618 	case RTM_NEWADDR:
    619 	case RTM_CHGADDR:
    620 		return sizeof(struct ifa_xmsghdr);
    621 
    622 	case RTM_OOIFINFO:
    623 #ifdef COMPAT_14
    624 		return sizeof(struct if_msghdr14);
    625 #else
    626 #ifdef DIAGNOSTIC
    627 		printf("RTM_OOIFINFO\n");
    628 #endif
    629 		return -1;
    630 #endif
    631 	case RTM_OIFINFO:
    632 #ifdef COMPAT_50
    633 		return sizeof(struct if_msghdr50);
    634 #else
    635 #ifdef DIAGNOSTIC
    636 		printf("RTM_OIFINFO\n");
    637 #endif
    638 		return -1;
    639 #endif
    640 
    641 	case RTM_IFINFO:
    642 		return sizeof(struct if_xmsghdr);
    643 
    644 	case RTM_IFANNOUNCE:
    645 	case RTM_IEEE80211:
    646 		return sizeof(struct if_xannouncemsghdr);
    647 
    648 	default:
    649 		return sizeof(struct rt_xmsghdr);
    650 	}
    651 }
    652 
    653 
    654 struct mbuf *
    655 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
    656 {
    657 	struct rt_xmsghdr *rtm;
    658 	struct mbuf *m;
    659 	int i;
    660 	const struct sockaddr *sa;
    661 	int len, dlen;
    662 
    663 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    664 	if (m == NULL)
    665 		return m;
    666 	MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
    667 
    668 	if ((len = rt_getlen(type)) == -1)
    669 		goto out;
    670 	if (len > MHLEN + MLEN)
    671 		panic("%s: message too long", __func__);
    672 	else if (len > MHLEN) {
    673 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
    674 		if (m->m_next == NULL)
    675 			goto out;
    676 		MCLAIM(m->m_next, m->m_owner);
    677 		m->m_pkthdr.len = len;
    678 		m->m_len = MHLEN;
    679 		m->m_next->m_len = len - MHLEN;
    680 	} else {
    681 		m->m_pkthdr.len = m->m_len = len;
    682 	}
    683 	m->m_pkthdr.rcvif = NULL;
    684 	m_copyback(m, 0, datalen, data);
    685 	if (len > datalen)
    686 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
    687 	rtm = mtod(m, struct rt_xmsghdr *);
    688 	for (i = 0; i < RTAX_MAX; i++) {
    689 		if ((sa = rtinfo->rti_info[i]) == NULL)
    690 			continue;
    691 		rtinfo->rti_addrs |= (1 << i);
    692 		dlen = RT_XROUNDUP(sa->sa_len);
    693 		m_copyback(m, len, sa->sa_len, sa);
    694 		if (dlen != sa->sa_len) {
    695 			/*
    696 			 * Up to 6 + 1 nul's since roundup is to
    697 			 * sizeof(uint64_t) (8 bytes)
    698 			 */
    699 			m_copyback(m, len + sa->sa_len,
    700 			    dlen - sa->sa_len, "\0\0\0\0\0\0");
    701 		}
    702 		len += dlen;
    703 	}
    704 	if (m->m_pkthdr.len != len)
    705 		goto out;
    706 	rtm->rtm_msglen = len;
    707 	rtm->rtm_version = RTM_XVERSION;
    708 	rtm->rtm_type = type;
    709 	return m;
    710 out:
    711 	m_freem(m);
    712 	return NULL;
    713 }
    714 
    715 /*
    716  * rt_msg2
    717  *
    718  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    719  *		returns the length of the message in 'lenp'.
    720  *
    721  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    722  *	the message
    723  * otherwise walkarg's w_needed is updated and if the user buffer is
    724  *	specified and w_needed indicates space exists the information is copied
    725  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    726  *	if the allocation fails ENOBUFS is returned.
    727  */
    728 static int
    729 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
    730 	int *lenp)
    731 {
    732 	int i;
    733 	int len, dlen, second_time = 0;
    734 	char *cp0, *cp = cpv;
    735 
    736 	rtinfo->rti_addrs = 0;
    737 again:
    738 	if ((len = rt_getlen(type)) == -1)
    739 		return EINVAL;
    740 
    741 	if ((cp0 = cp) != NULL)
    742 		cp += len;
    743 	for (i = 0; i < RTAX_MAX; i++) {
    744 		const struct sockaddr *sa;
    745 
    746 		if ((sa = rtinfo->rti_info[i]) == NULL)
    747 			continue;
    748 		rtinfo->rti_addrs |= (1 << i);
    749 		dlen = RT_XROUNDUP(sa->sa_len);
    750 		if (cp) {
    751 			int diff = dlen - sa->sa_len;
    752 			(void)memcpy(cp, sa, (size_t)sa->sa_len);
    753 			cp += sa->sa_len;
    754 			if (diff > 0) {
    755 				(void)memset(cp, 0, (size_t)diff);
    756 				cp += diff;
    757 			}
    758 		}
    759 		len += dlen;
    760 	}
    761 	if (cp == NULL && w != NULL && !second_time) {
    762 		struct rt_walkarg *rw = w;
    763 
    764 		rw->w_needed += len;
    765 		if (rw->w_needed <= 0 && rw->w_where) {
    766 			if (rw->w_tmemsize < len) {
    767 				if (rw->w_tmem)
    768 					free(rw->w_tmem, M_RTABLE);
    769 				rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
    770 				if (rw->w_tmem)
    771 					rw->w_tmemsize = len;
    772 				else
    773 					rw->w_tmemsize = 0;
    774 			}
    775 			if (rw->w_tmem) {
    776 				cp = rw->w_tmem;
    777 				second_time = 1;
    778 				goto again;
    779 			} else {
    780 				rw->w_tmemneeded = len;
    781 				return ENOBUFS;
    782 			}
    783 		}
    784 	}
    785 	if (cp) {
    786 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
    787 
    788 		rtm->rtm_version = RTM_XVERSION;
    789 		rtm->rtm_type = type;
    790 		rtm->rtm_msglen = len;
    791 	}
    792 	if (lenp)
    793 		*lenp = len;
    794 	return 0;
    795 }
    796 
    797 /*
    798  * This routine is called to generate a message from the routing
    799  * socket indicating that a redirect has occurred, a routing lookup
    800  * has failed, or that a protocol has detected timeouts to a particular
    801  * destination.
    802  */
    803 void
    804 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
    805     int error)
    806 {
    807 	struct rt_xmsghdr rtm;
    808 	struct mbuf *m;
    809 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    810 	struct rt_addrinfo info = *rtinfo;
    811 
    812 	COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
    813 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    814 		return;
    815 	memset(&rtm, 0, sizeof(rtm));
    816 	rtm.rtm_flags = RTF_DONE | flags;
    817 	rtm.rtm_errno = error;
    818 	m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
    819 	if (m == NULL)
    820 		return;
    821 	mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
    822 	COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
    823 }
    824 
    825 /*
    826  * This routine is called to generate a message from the routing
    827  * socket indicating that the status of a network interface has changed.
    828  */
    829 void
    830 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
    831 {
    832 	struct if_xmsghdr ifm;
    833 	struct mbuf *m;
    834 	struct rt_addrinfo info;
    835 
    836 	COMPATCALL(rt_ifmsg, (ifp));
    837 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    838 		return;
    839 	(void)memset(&info, 0, sizeof(info));
    840 	(void)memset(&ifm, 0, sizeof(ifm));
    841 	ifm.ifm_index = ifp->if_index;
    842 	ifm.ifm_flags = ifp->if_flags;
    843 	ifm.ifm_data = ifp->if_data;
    844 	ifm.ifm_addrs = 0;
    845 	m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
    846 	if (m == NULL)
    847 		return;
    848 	COMPATNAME(route_enqueue)(m, 0);
    849 #ifdef COMPAT_14
    850 	compat_14_rt_oifmsg(ifp);
    851 #endif
    852 #ifdef COMPAT_50
    853 	compat_50_rt_oifmsg(ifp);
    854 #endif
    855 }
    856 
    857 
    858 /*
    859  * This is called to generate messages from the routing socket
    860  * indicating a network interface has had addresses associated with it.
    861  * if we ever reverse the logic and replace messages TO the routing
    862  * socket indicate a request to configure interfaces, then it will
    863  * be unnecessary as the routing socket will automatically generate
    864  * copies of it.
    865  */
    866 void
    867 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
    868     struct rtentry *rt)
    869 {
    870 #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
    871 	struct rt_addrinfo info;
    872 	const struct sockaddr *sa;
    873 	int pass;
    874 	struct mbuf *m;
    875 	struct ifnet *ifp;
    876 	struct rt_xmsghdr rtm;
    877 	struct ifa_xmsghdr ifam;
    878 	int ncmd;
    879 
    880 	KASSERT(ifa != NULL);
    881 	ifp = ifa->ifa_ifp;
    882 	COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
    883 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    884 		return;
    885 	for (pass = 1; pass < 3; pass++) {
    886 		memset(&info, 0, sizeof(info));
    887 		switch (cmdpass(cmd, pass)) {
    888 		case cmdpass(RTM_ADD, 1):
    889 		case cmdpass(RTM_CHANGE, 1):
    890 		case cmdpass(RTM_DELETE, 2):
    891 		case cmdpass(RTM_NEWADDR, 1):
    892 		case cmdpass(RTM_DELADDR, 1):
    893 		case cmdpass(RTM_CHGADDR, 1):
    894 			switch (cmd) {
    895 			case RTM_ADD:
    896 				ncmd = RTM_NEWADDR;
    897 				break;
    898 			case RTM_DELETE:
    899 				ncmd = RTM_DELADDR;
    900 				break;
    901 			case RTM_CHANGE:
    902 				ncmd = RTM_CHGADDR;
    903 				break;
    904 			default:
    905 				ncmd = cmd;
    906 			}
    907 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
    908 			KASSERT(ifp->if_dl != NULL);
    909 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    910 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
    911 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
    912 			memset(&ifam, 0, sizeof(ifam));
    913 			ifam.ifam_index = ifp->if_index;
    914 			ifam.ifam_metric = ifa->ifa_metric;
    915 			ifam.ifam_flags = ifa->ifa_flags;
    916 			m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
    917 			if (m == NULL)
    918 				continue;
    919 			mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
    920 			    info.rti_addrs;
    921 			break;
    922 		case cmdpass(RTM_ADD, 2):
    923 		case cmdpass(RTM_CHANGE, 2):
    924 		case cmdpass(RTM_DELETE, 1):
    925 			if (rt == NULL)
    926 				continue;
    927 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    928 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
    929 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    930 			memset(&rtm, 0, sizeof(rtm));
    931 			rtm.rtm_index = ifp->if_index;
    932 			rtm.rtm_flags |= rt->rt_flags;
    933 			rtm.rtm_errno = error;
    934 			m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
    935 			if (m == NULL)
    936 				continue;
    937 			mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
    938 			break;
    939 		default:
    940 			continue;
    941 		}
    942 #ifdef DIAGNOSTIC
    943 		if (m == NULL)
    944 			panic("%s: called with wrong command", __func__);
    945 #endif
    946 		COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
    947 	}
    948 #undef cmdpass
    949 }
    950 
    951 static struct mbuf *
    952 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
    953     struct rt_addrinfo *info)
    954 {
    955 	struct if_xannouncemsghdr ifan;
    956 
    957 	memset(info, 0, sizeof(*info));
    958 	memset(&ifan, 0, sizeof(ifan));
    959 	ifan.ifan_index = ifp->if_index;
    960 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
    961 	ifan.ifan_what = what;
    962 	return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
    963 }
    964 
    965 /*
    966  * This is called to generate routing socket messages indicating
    967  * network interface arrival and departure.
    968  */
    969 void
    970 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
    971 {
    972 	struct mbuf *m;
    973 	struct rt_addrinfo info;
    974 
    975 	COMPATCALL(rt_ifannouncemsg, (ifp, what));
    976 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    977 		return;
    978 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
    979 	if (m == NULL)
    980 		return;
    981 	COMPATNAME(route_enqueue)(m, 0);
    982 }
    983 
    984 /*
    985  * This is called to generate routing socket messages indicating
    986  * IEEE80211 wireless events.
    987  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
    988  */
    989 void
    990 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
    991 	size_t data_len)
    992 {
    993 	struct mbuf *m;
    994 	struct rt_addrinfo info;
    995 
    996 	COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
    997 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    998 		return;
    999 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
   1000 	if (m == NULL)
   1001 		return;
   1002 	/*
   1003 	 * Append the ieee80211 data.  Try to stick it in the
   1004 	 * mbuf containing the ifannounce msg; otherwise allocate
   1005 	 * a new mbuf and append.
   1006 	 *
   1007 	 * NB: we assume m is a single mbuf.
   1008 	 */
   1009 	if (data_len > M_TRAILINGSPACE(m)) {
   1010 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
   1011 		if (n == NULL) {
   1012 			m_freem(m);
   1013 			return;
   1014 		}
   1015 		(void)memcpy(mtod(n, void *), data, data_len);
   1016 		n->m_len = data_len;
   1017 		m->m_next = n;
   1018 	} else if (data_len > 0) {
   1019 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
   1020 		m->m_len += data_len;
   1021 	}
   1022 	if (m->m_flags & M_PKTHDR)
   1023 		m->m_pkthdr.len += data_len;
   1024 	mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
   1025 	COMPATNAME(route_enqueue)(m, 0);
   1026 }
   1027 
   1028 /*
   1029  * This is used in dumping the kernel table via sysctl().
   1030  */
   1031 static int
   1032 sysctl_dumpentry(struct rtentry *rt, void *v)
   1033 {
   1034 	struct rt_walkarg *w = v;
   1035 	int error = 0, size;
   1036 	struct rt_addrinfo info;
   1037 
   1038 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
   1039 		return 0;
   1040 	memset(&info, 0, sizeof(info));
   1041 	info.rti_info[RTAX_DST] = rt_getkey(rt);
   1042 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1043 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1044 	info.rti_info[RTAX_TAG] = rt_gettag(rt);
   1045 	if (rt->rt_ifp) {
   1046 		const struct ifaddr *rtifa;
   1047 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
   1048 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
   1049 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
   1050 		 * --dyoung
   1051 		 */
   1052 		rtifa = rt_get_ifa(rt);
   1053 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
   1054 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
   1055 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
   1056 	}
   1057 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
   1058 		return error;
   1059 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1060 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
   1061 
   1062 		rtm->rtm_flags = rt->rt_flags;
   1063 		rtm->rtm_use = rt->rt_use;
   1064 		rtm_setmetrics(rt, rtm);
   1065 		KASSERT(rt->rt_ifp != NULL);
   1066 		rtm->rtm_index = rt->rt_ifp->if_index;
   1067 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
   1068 		rtm->rtm_addrs = info.rti_addrs;
   1069 		if ((error = copyout(rtm, w->w_where, size)) != 0)
   1070 			w->w_where = NULL;
   1071 		else
   1072 			w->w_where = (char *)w->w_where + size;
   1073 	}
   1074 	return error;
   1075 }
   1076 
   1077 static int
   1078 sysctl_iflist(int af, struct rt_walkarg *w, int type)
   1079 {
   1080 	struct ifnet *ifp;
   1081 	struct ifaddr *ifa;
   1082 	struct	rt_addrinfo info;
   1083 	int	len, error = 0;
   1084 
   1085 	memset(&info, 0, sizeof(info));
   1086 	IFNET_FOREACH(ifp) {
   1087 		if (w->w_arg && w->w_arg != ifp->if_index)
   1088 			continue;
   1089 		if (IFADDR_EMPTY(ifp))
   1090 			continue;
   1091 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   1092 		switch (type) {
   1093 		case NET_RT_IFLIST:
   1094 			error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
   1095 			break;
   1096 #ifdef COMPAT_14
   1097 		case NET_RT_OOIFLIST:
   1098 			error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
   1099 			break;
   1100 #endif
   1101 #ifdef COMPAT_50
   1102 		case NET_RT_OIFLIST:
   1103 			error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
   1104 			break;
   1105 #endif
   1106 		default:
   1107 			panic("sysctl_iflist(1)");
   1108 		}
   1109 		if (error)
   1110 			return error;
   1111 		info.rti_info[RTAX_IFP] = NULL;
   1112 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1113 			switch (type) {
   1114 			case NET_RT_IFLIST: {
   1115 				struct if_xmsghdr *ifm;
   1116 
   1117 				ifm = (struct if_xmsghdr *)w->w_tmem;
   1118 				ifm->ifm_index = ifp->if_index;
   1119 				ifm->ifm_flags = ifp->if_flags;
   1120 				ifm->ifm_data = ifp->if_data;
   1121 				ifm->ifm_addrs = info.rti_addrs;
   1122 				error = copyout(ifm, w->w_where, len);
   1123 				if (error)
   1124 					return error;
   1125 				w->w_where = (char *)w->w_where + len;
   1126 				break;
   1127 			}
   1128 
   1129 #ifdef COMPAT_14
   1130 			case NET_RT_OOIFLIST:
   1131 				error = compat_14_iflist(ifp, w, &info, len);
   1132 				if (error)
   1133 					return error;
   1134 				break;
   1135 #endif
   1136 #ifdef COMPAT_50
   1137 			case NET_RT_OIFLIST:
   1138 				error = compat_50_iflist(ifp, w, &info, len);
   1139 				if (error)
   1140 					return error;
   1141 				break;
   1142 #endif
   1143 			default:
   1144 				panic("sysctl_iflist(2)");
   1145 			}
   1146 		}
   1147 		IFADDR_FOREACH(ifa, ifp) {
   1148 			if (af && af != ifa->ifa_addr->sa_family)
   1149 				continue;
   1150 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
   1151 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1152 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   1153 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
   1154 				return error;
   1155 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1156 				struct ifa_xmsghdr *ifam;
   1157 
   1158 				ifam = (struct ifa_xmsghdr *)w->w_tmem;
   1159 				ifam->ifam_index = ifa->ifa_ifp->if_index;
   1160 				ifam->ifam_flags = ifa->ifa_flags;
   1161 				ifam->ifam_metric = ifa->ifa_metric;
   1162 				ifam->ifam_addrs = info.rti_addrs;
   1163 				error = copyout(w->w_tmem, w->w_where, len);
   1164 				if (error)
   1165 					return error;
   1166 				w->w_where = (char *)w->w_where + len;
   1167 			}
   1168 		}
   1169 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
   1170 		    info.rti_info[RTAX_BRD] = NULL;
   1171 	}
   1172 	return 0;
   1173 }
   1174 
   1175 static int
   1176 sysctl_rtable(SYSCTLFN_ARGS)
   1177 {
   1178 	void 	*where = oldp;
   1179 	size_t	*given = oldlenp;
   1180 	const void *new = newp;
   1181 	int	i, s, error = EINVAL;
   1182 	u_char  af;
   1183 	struct	rt_walkarg w;
   1184 
   1185 	if (namelen == 1 && name[0] == CTL_QUERY)
   1186 		return sysctl_query(SYSCTLFN_CALL(rnode));
   1187 
   1188 	if (new)
   1189 		return EPERM;
   1190 	if (namelen != 3)
   1191 		return EINVAL;
   1192 	af = name[0];
   1193 	w.w_tmemneeded = 0;
   1194 	w.w_tmemsize = 0;
   1195 	w.w_tmem = NULL;
   1196 again:
   1197 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   1198 	if (w.w_tmemneeded) {
   1199 		w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
   1200 		w.w_tmemsize = w.w_tmemneeded;
   1201 		w.w_tmemneeded = 0;
   1202 	}
   1203 	w.w_op = name[1];
   1204 	w.w_arg = name[2];
   1205 	w.w_given = *given;
   1206 	w.w_needed = 0 - w.w_given;
   1207 	w.w_where = where;
   1208 
   1209 	s = splsoftnet();
   1210 	switch (w.w_op) {
   1211 
   1212 	case NET_RT_DUMP:
   1213 	case NET_RT_FLAGS:
   1214 		for (i = 1; i <= AF_MAX; i++)
   1215 			if ((af == 0 || af == i) &&
   1216 			    (error = rt_walktree(i, sysctl_dumpentry, &w)))
   1217 				break;
   1218 		break;
   1219 
   1220 #ifdef COMPAT_14
   1221 	case NET_RT_OOIFLIST:
   1222 		error = sysctl_iflist(af, &w, w.w_op);
   1223 		break;
   1224 #endif
   1225 #ifdef COMPAT_50
   1226 	case NET_RT_OIFLIST:
   1227 		error = sysctl_iflist(af, &w, w.w_op);
   1228 		break;
   1229 #endif
   1230 	case NET_RT_IFLIST:
   1231 		error = sysctl_iflist(af, &w, w.w_op);
   1232 		break;
   1233 	}
   1234 	splx(s);
   1235 
   1236 	/* check to see if we couldn't allocate memory with NOWAIT */
   1237 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   1238 		goto again;
   1239 
   1240 	if (w.w_tmem)
   1241 		free(w.w_tmem, M_RTABLE);
   1242 	w.w_needed += w.w_given;
   1243 	if (where) {
   1244 		*given = (char *)w.w_where - (char *)where;
   1245 		if (*given < w.w_needed)
   1246 			return ENOMEM;
   1247 	} else {
   1248 		*given = (11 * w.w_needed) / 10;
   1249 	}
   1250 	return error;
   1251 }
   1252 
   1253 /*
   1254  * Routing message software interrupt routine
   1255  */
   1256 static void
   1257 COMPATNAME(route_intr)(void *cookie)
   1258 {
   1259 	struct sockproto proto = { .sp_family = PF_XROUTE, };
   1260 	struct route_info * const ri = &COMPATNAME(route_info);
   1261 	struct mbuf *m;
   1262 	int s;
   1263 
   1264 	mutex_enter(softnet_lock);
   1265 	KERNEL_LOCK(1, NULL);
   1266 	while (!IF_IS_EMPTY(&ri->ri_intrq)) {
   1267 		s = splnet();
   1268 		IF_DEQUEUE(&ri->ri_intrq, m);
   1269 		splx(s);
   1270 		if (m == NULL)
   1271 			break;
   1272 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
   1273 		raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
   1274 	}
   1275 	KERNEL_UNLOCK_ONE(NULL);
   1276 	mutex_exit(softnet_lock);
   1277 }
   1278 
   1279 /*
   1280  * Enqueue a message to the software interrupt routine.
   1281  */
   1282 void
   1283 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
   1284 {
   1285 	struct route_info * const ri = &COMPATNAME(route_info);
   1286 	int s, wasempty;
   1287 
   1288 	s = splnet();
   1289 	if (IF_QFULL(&ri->ri_intrq)) {
   1290 		IF_DROP(&ri->ri_intrq);
   1291 		m_freem(m);
   1292 	} else {
   1293 		wasempty = IF_IS_EMPTY(&ri->ri_intrq);
   1294 		M_SETCTX(m, (uintptr_t)family);
   1295 		IF_ENQUEUE(&ri->ri_intrq, m);
   1296 		if (wasempty)
   1297 			softint_schedule(ri->ri_sih);
   1298 	}
   1299 	splx(s);
   1300 }
   1301 
   1302 static void
   1303 COMPATNAME(route_init)(void)
   1304 {
   1305 	struct route_info * const ri = &COMPATNAME(route_info);
   1306 
   1307 #ifndef COMPAT_RTSOCK
   1308 	rt_init();
   1309 #endif
   1310 
   1311 	sysctl_net_route_setup(NULL);
   1312 	ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
   1313 	ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1314 	    COMPATNAME(route_intr), NULL);
   1315 }
   1316 
   1317 /*
   1318  * Definitions of protocols supported in the ROUTE domain.
   1319  */
   1320 #ifndef COMPAT_RTSOCK
   1321 PR_WRAP_USRREQS(route);
   1322 #else
   1323 PR_WRAP_USRREQS(compat_50_route);
   1324 #endif
   1325 
   1326 static const struct pr_usrreqs route_usrreqs = {
   1327 	.pr_attach	= COMPATNAME(route_attach_wrapper),
   1328 	.pr_detach	= COMPATNAME(route_detach_wrapper),
   1329 	.pr_generic	= COMPATNAME(route_usrreq_wrapper),
   1330 };
   1331 
   1332 static const struct protosw COMPATNAME(route_protosw)[] = {
   1333 	{
   1334 		.pr_type = SOCK_RAW,
   1335 		.pr_domain = &COMPATNAME(routedomain),
   1336 		.pr_flags = PR_ATOMIC|PR_ADDR,
   1337 		.pr_input = raw_input,
   1338 		.pr_output = COMPATNAME(route_output),
   1339 		.pr_ctlinput = raw_ctlinput,
   1340 		.pr_usrreqs = &route_usrreqs,
   1341 		.pr_init = raw_init,
   1342 	},
   1343 };
   1344 
   1345 struct domain COMPATNAME(routedomain) = {
   1346 	.dom_family = PF_XROUTE,
   1347 	.dom_name = DOMAINNAME,
   1348 	.dom_init = COMPATNAME(route_init),
   1349 	.dom_protosw = COMPATNAME(route_protosw),
   1350 	.dom_protoswNPROTOSW =
   1351 	    &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
   1352 };
   1353 
   1354 static void
   1355 sysctl_net_route_setup(struct sysctllog **clog)
   1356 {
   1357 	const struct sysctlnode *rnode = NULL;
   1358 
   1359 	sysctl_createv(clog, 0, NULL, &rnode,
   1360 		       CTLFLAG_PERMANENT,
   1361 		       CTLTYPE_NODE, DOMAINNAME,
   1362 		       SYSCTL_DESCR("PF_ROUTE information"),
   1363 		       NULL, 0, NULL, 0,
   1364 		       CTL_NET, PF_XROUTE, CTL_EOL);
   1365 
   1366 	sysctl_createv(clog, 0, NULL, NULL,
   1367 		       CTLFLAG_PERMANENT,
   1368 		       CTLTYPE_NODE, "rtable",
   1369 		       SYSCTL_DESCR("Routing table information"),
   1370 		       sysctl_rtable, 0, NULL, 0,
   1371 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
   1372 
   1373 	sysctl_createv(clog, 0, &rnode, NULL,
   1374 		       CTLFLAG_PERMANENT,
   1375 		       CTLTYPE_STRUCT, "stats",
   1376 		       SYSCTL_DESCR("Routing statistics"),
   1377 		       NULL, 0, &rtstat, sizeof(rtstat),
   1378 		       CTL_CREATE, CTL_EOL);
   1379 }
   1380