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