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