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