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