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