Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.115.2.4.4.2
      1 /*	$NetBSD: rtsock.c,v 1.115.2.4.4.2 2010/05/13 05:36:49 matt 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.115.2.4.4.2 2010/05/13 05:36:49 matt Exp $");
     65 
     66 #include "opt_inet.h"
     67 
     68 #include <sys/param.h>
     69 #include <sys/systm.h>
     70 #include <sys/proc.h>
     71 #include <sys/mbuf.h>
     72 #include <sys/socket.h>
     73 #include <sys/socketvar.h>
     74 #include <sys/domain.h>
     75 #include <sys/protosw.h>
     76 #include <sys/sysctl.h>
     77 #include <sys/kauth.h>
     78 #include <sys/intr.h>
     79 #ifdef RTSOCK_DEBUG
     80 #include <netinet/in.h>
     81 #endif /* RTSOCK_DEBUG */
     82 
     83 #include <net/if.h>
     84 #include <net/route.h>
     85 #include <net/raw_cb.h>
     86 
     87 #include <machine/stdarg.h>
     88 
     89 DOMAIN_DEFINE(routedomain);	/* forward declare and add to link set */
     90 
     91 struct	sockaddr route_dst = { .sa_len = 2, .sa_family = PF_ROUTE, };
     92 struct	sockaddr route_src = { .sa_len = 2, .sa_family = PF_ROUTE, };
     93 
     94 int	route_maxqlen = IFQ_MAXLEN;
     95 static struct	ifqueue route_intrq;
     96 static void	*route_sih;
     97 
     98 struct walkarg {
     99 	int	w_op;
    100 	int	w_arg;
    101 	int	w_given;
    102 	int	w_needed;
    103 	void *	w_where;
    104 	int	w_tmemsize;
    105 	int	w_tmemneeded;
    106 	void *	w_tmem;
    107 };
    108 
    109 static struct mbuf *rt_msg1(int, struct rt_addrinfo *, void *, int);
    110 static int rt_msg2(int, struct rt_addrinfo *, void *, struct walkarg *, int *);
    111 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
    112 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
    113     struct rt_addrinfo *);
    114 static int sysctl_dumpentry(struct rtentry *, void *);
    115 static int sysctl_iflist(int, struct walkarg *, int);
    116 static int sysctl_rtable(SYSCTLFN_PROTO);
    117 static inline void rt_adjustcount(int, int);
    118 static void route_enqueue(struct mbuf *, int);
    119 
    120 static inline void
    121 rt_adjustcount(int af, int cnt)
    122 {
    123 	route_cb.any_count += cnt;
    124 	switch (af) {
    125 	case AF_INET:
    126 		route_cb.ip_count += cnt;
    127 		return;
    128 #ifdef INET6
    129 	case AF_INET6:
    130 		route_cb.ip6_count += cnt;
    131 		return;
    132 #endif
    133 	case AF_IPX:
    134 		route_cb.ipx_count += cnt;
    135 		return;
    136 	case AF_NS:
    137 		route_cb.ns_count += cnt;
    138 		return;
    139 	case AF_ISO:
    140 		route_cb.iso_count += cnt;
    141 		return;
    142 	}
    143 }
    144 
    145 /*ARGSUSED*/
    146 int
    147 route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    148 	struct mbuf *control, struct lwp *l)
    149 {
    150 	int error = 0;
    151 	struct rawcb *rp = sotorawcb(so);
    152 	int s;
    153 
    154 	if (req == PRU_ATTACH) {
    155 		sosetlock(so);
    156 		MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK|M_ZERO);
    157 		so->so_pcb = rp;
    158 	}
    159 	if (req == PRU_DETACH && rp)
    160 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    161 	s = splsoftnet();
    162 
    163 	/*
    164 	 * Don't call raw_usrreq() in the attach case, because
    165 	 * we want to allow non-privileged processes to listen on
    166 	 * and send "safe" commands to the routing socket.
    167 	 */
    168 	if (req == PRU_ATTACH) {
    169 		if (l == NULL)
    170 			error = EACCES;
    171 		else
    172 			error = raw_attach(so, (int)(long)nam);
    173 	} else
    174 		error = raw_usrreq(so, req, m, nam, control, l);
    175 
    176 	rp = sotorawcb(so);
    177 	if (req == PRU_ATTACH && rp) {
    178 		if (error) {
    179 			free(rp, M_PCB);
    180 			splx(s);
    181 			return error;
    182 		}
    183 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    184 		rp->rcb_laddr = &route_src;
    185 		rp->rcb_faddr = &route_dst;
    186 		soisconnected(so);
    187 		so->so_options |= SO_USELOOPBACK;
    188 	}
    189 	splx(s);
    190 	return error;
    191 }
    192 
    193 static const struct sockaddr *
    194 intern_netmask(const struct sockaddr *mask)
    195 {
    196 	struct radix_node *rn;
    197 	extern struct radix_node_head *mask_rnhead;
    198 
    199 	if (mask != NULL &&
    200 	    (rn = rn_search(mask, mask_rnhead->rnh_treetop)))
    201 		mask = (const struct sockaddr *)rn->rn_key;
    202 
    203 	return mask;
    204 }
    205 
    206 /*ARGSUSED*/
    207 int
    208 route_output(struct mbuf *m, ...)
    209 {
    210 	struct sockproto proto = { .sp_family = PF_ROUTE, };
    211 	struct rt_msghdr *rtm = NULL;
    212 	struct rt_msghdr *old_rtm = NULL;
    213 	struct rtentry *rt = NULL;
    214 	struct rtentry *saved_nrt = NULL;
    215 	struct rt_addrinfo info;
    216 	int len, error = 0;
    217 	struct ifnet *ifp = NULL;
    218 	struct ifaddr *ifa = NULL;
    219 	struct socket *so;
    220 	va_list ap;
    221 	sa_family_t family;
    222 
    223 	va_start(ap, m);
    224 	so = va_arg(ap, struct socket *);
    225 	va_end(ap);
    226 
    227 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
    228 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
    229 	   (m = m_pullup(m, sizeof(int32_t))) == NULL))
    230 		return ENOBUFS;
    231 	if ((m->m_flags & M_PKTHDR) == 0)
    232 		panic("route_output");
    233 	len = m->m_pkthdr.len;
    234 	if (len < sizeof(*rtm) ||
    235 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
    236 		info.rti_info[RTAX_DST] = NULL;
    237 		senderr(EINVAL);
    238 	}
    239 	R_Malloc(rtm, struct rt_msghdr *, len);
    240 	if (rtm == NULL) {
    241 		info.rti_info[RTAX_DST] = NULL;
    242 		senderr(ENOBUFS);
    243 	}
    244 	m_copydata(m, 0, len, rtm);
    245 	if (rtm->rtm_version != RTM_VERSION) {
    246 		info.rti_info[RTAX_DST] = NULL;
    247 		senderr(EPROTONOSUPPORT);
    248 	}
    249 	rtm->rtm_pid = curproc->p_pid;
    250 	memset(&info, 0, sizeof(info));
    251 	info.rti_addrs = rtm->rtm_addrs;
    252 	KASSERT(sizeof(*rtm) == RT_ROUNDUP(sizeof(*rtm)));
    253 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
    254 	    &info))
    255 		senderr(EINVAL);
    256 	info.rti_flags = rtm->rtm_flags;
    257 #ifdef RTSOCK_DEBUG
    258 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    259 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
    260 		    inet_ntoa(((const struct sockaddr_in *)
    261 		    info.rti_info[RTAX_DST])->sin_addr));
    262 	}
    263 #endif /* RTSOCK_DEBUG */
    264 	if (info.rti_info[RTAX_DST] == NULL ||
    265 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX))
    266 		senderr(EINVAL);
    267 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
    268 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
    269 		senderr(EINVAL);
    270 
    271 	/*
    272 	 * Verify that the caller has the appropriate privilege; RTM_GET
    273 	 * is the only operation the non-superuser is allowed.
    274 	 */
    275 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
    276 	    0, rtm, NULL, NULL) != 0)
    277 		senderr(EACCES);
    278 
    279 	switch (rtm->rtm_type) {
    280 
    281 	case RTM_ADD:
    282 		if (info.rti_info[RTAX_GATEWAY] == NULL)
    283 			senderr(EINVAL);
    284 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    285 		if (error == 0 && saved_nrt) {
    286 			rt_setmetrics(rtm->rtm_inits,
    287 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
    288 			saved_nrt->rt_refcnt--;
    289 		}
    290 		break;
    291 
    292 	case RTM_DELETE:
    293 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    294 		if (error == 0) {
    295 			(rt = saved_nrt)->rt_refcnt++;
    296 			goto report;
    297 		}
    298 		break;
    299 
    300 	case RTM_GET:
    301 	case RTM_CHANGE:
    302 	case RTM_LOCK:
    303                 /* XXX This will mask info.rti_info[RTAX_DST] with
    304 		 * info.rti_info[RTAX_NETMASK] before
    305                  * searching.  It did not used to do that.  --dyoung
    306 		 */
    307 		error = rtrequest1(RTM_GET, &info, &rt);
    308 		if (error != 0)
    309 			senderr(error);
    310 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
    311 			struct radix_node *rn;
    312 
    313 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
    314 			    info.rti_info[RTAX_DST]->sa_len) != 0)
    315 				senderr(ESRCH);
    316 			info.rti_info[RTAX_NETMASK] = intern_netmask(
    317 			    info.rti_info[RTAX_NETMASK]);
    318 			for (rn = rt->rt_nodes; rn; rn = rn->rn_dupedkey)
    319 				if (info.rti_info[RTAX_NETMASK] ==
    320 				    (const struct sockaddr *)rn->rn_mask)
    321 					break;
    322 			if (rn == NULL)
    323 				senderr(ETOOMANYREFS);
    324 			rt = (struct rtentry *)rn;
    325 		}
    326 
    327 		switch (rtm->rtm_type) {
    328 		case RTM_GET:
    329 		report:
    330 			info.rti_info[RTAX_DST] = rt_getkey(rt);
    331 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    332 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    333 			if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
    334 				;
    335 			else if ((ifp = rt->rt_ifp) != NULL) {
    336 				const struct ifaddr *rtifa;
    337 				info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    338                                 /* rtifa used to be simply rt->rt_ifa.
    339                                  * If rt->rt_ifa != NULL, then
    340                                  * rt_get_ifa() != NULL.  So this
    341                                  * ought to still be safe. --dyoung
    342 				 */
    343 				rtifa = rt_get_ifa(rt);
    344 				info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
    345 #ifdef RTSOCK_DEBUG
    346 				if (info.rti_info[RTAX_IFA]->sa_family ==
    347 				    AF_INET) {
    348 					printf("%s: copying out RTAX_IFA %s ",
    349 					    __func__, inet_ntoa(
    350 					    (const struct sockaddr_in *)
    351 					    info.rti_info[RTAX_IFA])->sin_addr);
    352 					printf("for info.rti_info[RTAX_DST] %s "
    353 					    "ifa_getifa %p ifa_seqno %p\n",
    354 					    inet_ntoa(
    355 					    (const struct sockaddr_in *)
    356 					    info.rti_info[RTAX_DST])->sin_addr),
    357 					    (void *)rtifa->ifa_getifa,
    358 					    rtifa->ifa_seqno);
    359 				}
    360 #endif /* RTSOCK_DEBUG */
    361 				if (ifp->if_flags & IFF_POINTOPOINT) {
    362 					info.rti_info[RTAX_BRD] =
    363 					    rtifa->ifa_dstaddr;
    364 				} else
    365 					info.rti_info[RTAX_BRD] = NULL;
    366 				rtm->rtm_index = ifp->if_index;
    367 			} else {
    368 				info.rti_info[RTAX_IFP] = NULL;
    369 				info.rti_info[RTAX_IFA] = NULL;
    370 			}
    371 			(void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
    372 			if (len > rtm->rtm_msglen) {
    373 				old_rtm = rtm;
    374 				R_Malloc(rtm, struct rt_msghdr *, len);
    375 				if (rtm == NULL)
    376 					senderr(ENOBUFS);
    377 				(void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
    378 			}
    379 			(void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
    380 			rtm->rtm_flags = rt->rt_flags;
    381 			rtm->rtm_rmx = rt->rt_rmx;
    382 			rtm->rtm_addrs = info.rti_addrs;
    383 			break;
    384 
    385 		case RTM_CHANGE:
    386 			/*
    387 			 * new gateway could require new ifaddr, ifp;
    388 			 * flags may also be different; ifp may be specified
    389 			 * by ll sockaddr when protocol address is ambiguous
    390 			 */
    391 			if ((error = rt_getifa(&info)) != 0)
    392 				senderr(error);
    393 			if (info.rti_info[RTAX_GATEWAY] &&
    394 			    rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
    395 				senderr(EDQUOT);
    396 			/* new gateway could require new ifaddr, ifp;
    397 			   flags may also be different; ifp may be specified
    398 			   by ll sockaddr when protocol address is ambiguous */
    399 			if (info.rti_info[RTAX_IFP] &&
    400 			    (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
    401 			    (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
    402 			    info.rti_info[RTAX_GATEWAY])) {
    403 				ifa = ifaof_ifpforaddr(info.rti_info[RTAX_IFA] ?
    404 				    info.rti_info[RTAX_IFA] :
    405 				    info.rti_info[RTAX_GATEWAY], ifp);
    406 			} else if ((info.rti_info[RTAX_IFA] &&
    407 			    (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
    408 			    (info.rti_info[RTAX_GATEWAY] &&
    409 			    (ifa = ifa_ifwithroute(rt->rt_flags,
    410 			    rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
    411 				ifp = ifa->ifa_ifp;
    412 			}
    413 			if (ifa) {
    414 				struct ifaddr *oifa = rt->rt_ifa;
    415 				if (oifa != ifa) {
    416 					if (oifa && oifa->ifa_rtrequest) {
    417 						oifa->ifa_rtrequest(RTM_DELETE,
    418 						    rt, &info);
    419 					}
    420 					rt_replace_ifa(rt, ifa);
    421 					rt->rt_ifp = ifp;
    422 				}
    423 			}
    424 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
    425 			    &rt->rt_rmx);
    426 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    427 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
    428 			/*FALLTHROUGH*/
    429 		case RTM_LOCK:
    430 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    431 			rt->rt_rmx.rmx_locks |=
    432 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    433 			break;
    434 		}
    435 		break;
    436 
    437 	default:
    438 		senderr(EOPNOTSUPP);
    439 	}
    440 
    441 flush:
    442 	if (rtm) {
    443 		if (error)
    444 			rtm->rtm_errno = error;
    445 		else
    446 			rtm->rtm_flags |= RTF_DONE;
    447 	}
    448 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
    449 	    0;
    450 	/* We cannot free old_rtm until we have stopped using the
    451 	 * pointers in info, some of which may point to sockaddrs
    452 	 * in old_rtm.
    453 	 */
    454 	if (old_rtm != NULL)
    455 		Free(old_rtm);
    456 	if (rt)
    457 		rtfree(rt);
    458     {
    459 	struct rawcb *rp = NULL;
    460 	/*
    461 	 * Check to see if we don't want our own messages.
    462 	 */
    463 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    464 		if (route_cb.any_count <= 1) {
    465 			if (rtm)
    466 				Free(rtm);
    467 			m_freem(m);
    468 			return error;
    469 		}
    470 		/* There is another listener, so construct message */
    471 		rp = sotorawcb(so);
    472 	}
    473 	if (rtm) {
    474 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
    475 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
    476 			m_freem(m);
    477 			m = NULL;
    478 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
    479 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
    480 		Free(rtm);
    481 	}
    482 	if (rp)
    483 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    484 	if (family)
    485 		proto.sp_protocol = family;
    486 	if (m)
    487 		raw_input(m, &proto, &route_src, &route_dst);
    488 	if (rp)
    489 		rp->rcb_proto.sp_family = PF_ROUTE;
    490     }
    491 	return error;
    492 }
    493 
    494 void
    495 rt_setmetrics(u_long which, const struct rt_metrics *in, struct rt_metrics *out)
    496 {
    497 #define metric(f, e) if (which & (f)) out->e = in->e;
    498 	metric(RTV_RPIPE, rmx_recvpipe);
    499 	metric(RTV_SPIPE, rmx_sendpipe);
    500 	metric(RTV_SSTHRESH, rmx_ssthresh);
    501 	metric(RTV_RTT, rmx_rtt);
    502 	metric(RTV_RTTVAR, rmx_rttvar);
    503 	metric(RTV_HOPCOUNT, rmx_hopcount);
    504 	metric(RTV_MTU, rmx_mtu);
    505 	metric(RTV_EXPIRE, rmx_expire);
    506 #undef metric
    507 }
    508 
    509 static int
    510 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
    511     struct rt_addrinfo *rtinfo)
    512 {
    513 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
    514 	int i;
    515 
    516 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
    517 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    518 			continue;
    519 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
    520 		RT_ADVANCE(cp, sa);
    521 	}
    522 
    523 	/*
    524 	 * Check for extra addresses specified, except RTM_GET asking
    525 	 * for interface info.
    526 	 */
    527 	if (rtmtype == RTM_GET) {
    528 		if (((rtinfo->rti_addrs &
    529 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
    530 			return 1;
    531 	} else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
    532 		return 1;
    533 	/* Check for bad data length.  */
    534 	if (cp != cplim) {
    535 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
    536 		    cp - RT_ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
    537 			/*
    538 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
    539 			 * We accept this for now for the sake of old
    540 			 * binaries or third party softwares.
    541 			 */
    542 			;
    543 		else
    544 			return 1;
    545 	}
    546 	return 0;
    547 }
    548 
    549 static struct mbuf *
    550 rt_msg1(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
    551 {
    552 	struct rt_msghdr *rtm;
    553 	struct mbuf *m;
    554 	int i;
    555 	const struct sockaddr *sa;
    556 	int len, dlen;
    557 
    558 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    559 	if (m == NULL)
    560 		return m;
    561 	MCLAIM(m, &routedomain.dom_mowner);
    562 	switch (type) {
    563 
    564 	case RTM_DELADDR:
    565 	case RTM_NEWADDR:
    566 		len = sizeof(struct ifa_msghdr);
    567 		break;
    568 
    569 #ifdef COMPAT_14
    570 	case RTM_OIFINFO:
    571 		len = sizeof(struct if_msghdr14);
    572 		break;
    573 #endif
    574 
    575 	case RTM_IFINFO:
    576 		len = sizeof(struct if_msghdr);
    577 		break;
    578 
    579 	case RTM_IFANNOUNCE:
    580 	case RTM_IEEE80211:
    581 		len = sizeof(struct if_announcemsghdr);
    582 		break;
    583 
    584 	default:
    585 		len = sizeof(struct rt_msghdr);
    586 	}
    587 	len = RT_ROUNDUP(len);
    588 	if (len > MHLEN + MLEN)
    589 		panic("rt_msg1: message too long");
    590 	else if (len > MHLEN) {
    591 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
    592 		if (m->m_next == NULL) {
    593 			m_freem(m);
    594 			return NULL;
    595 		}
    596 		MCLAIM(m->m_next, m->m_owner);
    597 		m->m_pkthdr.len = len;
    598 		m->m_len = MHLEN;
    599 		m->m_next->m_len = len - MHLEN;
    600 	} else {
    601 		m->m_pkthdr.len = m->m_len = len;
    602 	}
    603 	m->m_pkthdr.rcvif = NULL;
    604 	m_copyback(m, 0, datalen, data);
    605 	if (len > datalen)
    606 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
    607 	rtm = mtod(m, struct rt_msghdr *);
    608 	for (i = 0; i < RTAX_MAX; i++) {
    609 		if ((sa = rtinfo->rti_info[i]) == NULL)
    610 			continue;
    611 		rtinfo->rti_addrs |= (1 << i);
    612 		dlen = RT_ROUNDUP(sa->sa_len);
    613 		m_copyback(m, len, dlen, sa);
    614 		len += dlen;
    615 	}
    616 	KASSERT(len == RT_ROUNDUP(len));
    617 	if (m->m_pkthdr.len != len) {
    618 		m_freem(m);
    619 		return NULL;
    620 	}
    621 	rtm->rtm_msglen = len;
    622 	rtm->rtm_version = RTM_VERSION;
    623 	rtm->rtm_type = type;
    624 	return m;
    625 }
    626 
    627 /*
    628  * rt_msg2
    629  *
    630  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    631  *		returns the length of the message in 'lenp'.
    632  *
    633  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    634  *	the message
    635  * otherwise walkarg's w_needed is updated and if the user buffer is
    636  *	specified and w_needed indicates space exists the information is copied
    637  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    638  *	if the allocation fails ENOBUFS is returned.
    639  */
    640 static int
    641 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct walkarg *w,
    642 	int *lenp)
    643 {
    644 	int i;
    645 	int len, dlen, second_time = 0;
    646 	char *cp0, *cp = cpv;
    647 
    648 	rtinfo->rti_addrs = 0;
    649 again:
    650 	switch (type) {
    651 
    652 	case RTM_DELADDR:
    653 	case RTM_NEWADDR:
    654 		len = sizeof(struct ifa_msghdr);
    655 		break;
    656 #ifdef COMPAT_14
    657 	case RTM_OIFINFO:
    658 		len = sizeof(struct if_msghdr14);
    659 		break;
    660 #endif
    661 
    662 	case RTM_IFINFO:
    663 		len = sizeof(struct if_msghdr);
    664 		break;
    665 
    666 	default:
    667 		len = sizeof(struct rt_msghdr);
    668 	}
    669 	len = RT_ROUNDUP(len);
    670 	if ((cp0 = cp) != NULL)
    671 		cp += len;
    672 	for (i = 0; i < RTAX_MAX; i++) {
    673 		const struct sockaddr *sa;
    674 
    675 		if ((sa = rtinfo->rti_info[i]) == NULL)
    676 			continue;
    677 		rtinfo->rti_addrs |= (1 << i);
    678 		dlen = RT_ROUNDUP(sa->sa_len);
    679 		if (cp) {
    680 			(void)memcpy(cp, sa, (size_t)dlen);
    681 			cp += dlen;
    682 		}
    683 		len += dlen;
    684 	}
    685 	if (cp == NULL && w != NULL && !second_time) {
    686 		struct walkarg *rw = w;
    687 
    688 		rw->w_needed += len;
    689 		if (rw->w_needed <= 0 && rw->w_where) {
    690 			if (rw->w_tmemsize < len) {
    691 				if (rw->w_tmem)
    692 					free(rw->w_tmem, M_RTABLE);
    693 				rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
    694 				if (rw->w_tmem)
    695 					rw->w_tmemsize = len;
    696 				else
    697 					rw->w_tmemsize = 0;
    698 			}
    699 			if (rw->w_tmem) {
    700 				cp = rw->w_tmem;
    701 				second_time = 1;
    702 				goto again;
    703 			} else {
    704 				rw->w_tmemneeded = len;
    705 				return ENOBUFS;
    706 			}
    707 		}
    708 	}
    709 	KASSERT(len == RT_ROUNDUP(len));
    710 	if (cp) {
    711 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
    712 
    713 		rtm->rtm_version = RTM_VERSION;
    714 		rtm->rtm_type = type;
    715 		rtm->rtm_msglen = len;
    716 	}
    717 	if (lenp)
    718 		*lenp = len;
    719 	return 0;
    720 }
    721 
    722 /*
    723  * This routine is called to generate a message from the routing
    724  * socket indicating that a redirect has occurred, a routing lookup
    725  * has failed, or that a protocol has detected timeouts to a particular
    726  * destination.
    727  */
    728 void
    729 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
    730 {
    731 	struct rt_msghdr rtm;
    732 	struct mbuf *m;
    733 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    734 
    735 	if (route_cb.any_count == 0)
    736 		return;
    737 	memset(&rtm, 0, sizeof(rtm));
    738 	rtm.rtm_flags = RTF_DONE | flags;
    739 	rtm.rtm_errno = error;
    740 	m = rt_msg1(type, rtinfo, &rtm, sizeof(rtm));
    741 	if (m == NULL)
    742 		return;
    743 	mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
    744 	route_enqueue(m, sa ? sa->sa_family : 0);
    745 }
    746 
    747 /*
    748  * This routine is called to generate a message from the routing
    749  * socket indicating that the status of a network interface has changed.
    750  */
    751 void
    752 rt_ifmsg(struct ifnet *ifp)
    753 {
    754 	struct if_msghdr ifm;
    755 #ifdef COMPAT_14
    756 	struct if_msghdr14 oifm;
    757 #endif
    758 	struct mbuf *m;
    759 	struct rt_addrinfo info;
    760 
    761 	if (route_cb.any_count == 0)
    762 		return;
    763 	memset(&info, 0, sizeof(info));
    764 	memset(&ifm, 0, sizeof(ifm));
    765 	ifm.ifm_index = ifp->if_index;
    766 	ifm.ifm_flags = ifp->if_flags;
    767 	ifm.ifm_data = ifp->if_data;
    768 	ifm.ifm_addrs = 0;
    769 	m = rt_msg1(RTM_IFINFO, &info, &ifm, sizeof(ifm));
    770 	if (m == NULL)
    771 		return;
    772 	route_enqueue(m, 0);
    773 #ifdef COMPAT_14
    774 	memset(&info, 0, sizeof(info));
    775 	memset(&oifm, 0, sizeof(oifm));
    776 	oifm.ifm_index = ifp->if_index;
    777 	oifm.ifm_flags = ifp->if_flags;
    778 	oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
    779 	oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
    780 	oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
    781 	oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
    782 	oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
    783 	oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
    784 	oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
    785 	oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
    786 	oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
    787 	oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
    788 	oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
    789 	oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
    790 	oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
    791 	oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
    792 	oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
    793 	oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
    794 	oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
    795 	oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
    796 	oifm.ifm_addrs = 0;
    797 	m = rt_msg1(RTM_OIFINFO, &info, &oifm, sizeof(oifm));
    798 	if (m == NULL)
    799 		return;
    800 	route_enqueue(m, 0);
    801 #endif
    802 }
    803 
    804 /*
    805  * This is called to generate messages from the routing socket
    806  * indicating a network interface has had addresses associated with it.
    807  * if we ever reverse the logic and replace messages TO the routing
    808  * socket indicate a request to configure interfaces, then it will
    809  * be unnecessary as the routing socket will automatically generate
    810  * copies of it.
    811  */
    812 void
    813 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
    814 {
    815 	struct rt_addrinfo info;
    816 	const struct sockaddr *sa = NULL;
    817 	int pass;
    818 	struct mbuf *m = NULL;
    819 	struct ifnet *ifp = ifa->ifa_ifp;
    820 
    821 	if (route_cb.any_count == 0)
    822 		return;
    823 	for (pass = 1; pass < 3; pass++) {
    824 		memset(&info, 0, sizeof(info));
    825 		if ((cmd == RTM_ADD && pass == 1) ||
    826 		    (cmd == RTM_DELETE && pass == 2)) {
    827 			struct ifa_msghdr ifam;
    828 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
    829 
    830 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
    831 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    832 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
    833 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
    834 			memset(&ifam, 0, sizeof(ifam));
    835 			ifam.ifam_index = ifp->if_index;
    836 			ifam.ifam_metric = ifa->ifa_metric;
    837 			ifam.ifam_flags = ifa->ifa_flags;
    838 			m = rt_msg1(ncmd, &info, &ifam, sizeof(ifam));
    839 			if (m == NULL)
    840 				continue;
    841 			mtod(m, struct ifa_msghdr *)->ifam_addrs =
    842 			    info.rti_addrs;
    843 		}
    844 		if ((cmd == RTM_ADD && pass == 2) ||
    845 		    (cmd == RTM_DELETE && pass == 1)) {
    846 			struct rt_msghdr rtm;
    847 
    848 			if (rt == NULL)
    849 				continue;
    850 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    851 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
    852 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    853 			memset(&rtm, 0, sizeof(rtm));
    854 			rtm.rtm_index = ifp->if_index;
    855 			rtm.rtm_flags |= rt->rt_flags;
    856 			rtm.rtm_errno = error;
    857 			m = rt_msg1(cmd, &info, &rtm, sizeof(rtm));
    858 			if (m == NULL)
    859 				continue;
    860 			mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
    861 		}
    862 #ifdef DIAGNOSTIC
    863 		if (m == NULL)
    864 			panic("%s: called with wrong command", __func__);
    865 #endif
    866 		route_enqueue(m, sa ? sa->sa_family : 0);
    867 	}
    868 }
    869 
    870 static struct mbuf *
    871 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
    872     struct rt_addrinfo *info)
    873 {
    874 	struct if_announcemsghdr ifan;
    875 
    876 	memset(info, 0, sizeof(*info));
    877 	memset(&ifan, 0, sizeof(ifan));
    878 	ifan.ifan_index = ifp->if_index;
    879 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
    880 	ifan.ifan_what = what;
    881 	return rt_msg1(type, info, &ifan, sizeof(ifan));
    882 }
    883 
    884 /*
    885  * This is called to generate routing socket messages indicating
    886  * network interface arrival and departure.
    887  */
    888 void
    889 rt_ifannouncemsg(struct ifnet *ifp, int what)
    890 {
    891 	struct mbuf *m;
    892 	struct rt_addrinfo info;
    893 
    894 	if (route_cb.any_count == 0)
    895 		return;
    896 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
    897 	if (m == NULL)
    898 		return;
    899 	route_enqueue(m, 0);
    900 }
    901 
    902 /*
    903  * This is called to generate routing socket messages indicating
    904  * IEEE80211 wireless events.
    905  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
    906  */
    907 void
    908 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
    909 {
    910 	struct mbuf *m;
    911 	struct rt_addrinfo info;
    912 
    913 	if (route_cb.any_count == 0)
    914 		return;
    915 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
    916 	if (m == NULL)
    917 		return;
    918 	/*
    919 	 * Append the ieee80211 data.  Try to stick it in the
    920 	 * mbuf containing the ifannounce msg; otherwise allocate
    921 	 * a new mbuf and append.
    922 	 *
    923 	 * NB: we assume m is a single mbuf.
    924 	 */
    925 	if (data_len > M_TRAILINGSPACE(m)) {
    926 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
    927 		if (n == NULL) {
    928 			m_freem(m);
    929 			return;
    930 		}
    931 		(void)memcpy(mtod(n, void *), data, data_len);
    932 		n->m_len = data_len;
    933 		m->m_next = n;
    934 	} else if (data_len > 0) {
    935 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
    936 		m->m_len += data_len;
    937 	}
    938 	if (m->m_flags & M_PKTHDR)
    939 		m->m_pkthdr.len += data_len;
    940 	mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
    941 	route_enqueue(m, 0);
    942 }
    943 
    944 /*
    945  * This is used in dumping the kernel table via sysctl().
    946  */
    947 static int
    948 sysctl_dumpentry(struct rtentry *rt, void *v)
    949 {
    950 	struct walkarg *w = v;
    951 	int error = 0, size;
    952 	struct rt_addrinfo info;
    953 
    954 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    955 		return 0;
    956 	memset(&info, 0, sizeof(info));
    957 	info.rti_info[RTAX_DST] = rt_getkey(rt);
    958 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    959 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    960 	if (rt->rt_ifp) {
    961 		const struct ifaddr *rtifa;
    962 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
    963 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
    964 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
    965 		 * --dyoung
    966 		 */
    967 		rtifa = rt_get_ifa(rt);
    968 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
    969 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    970 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
    971 	}
    972 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
    973 		return error;
    974 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    975 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
    976 
    977 		rtm->rtm_flags = rt->rt_flags;
    978 		rtm->rtm_use = rt->rt_use;
    979 		rtm->rtm_rmx = rt->rt_rmx;
    980 		KASSERT(rt->rt_ifp != NULL);
    981 		rtm->rtm_index = rt->rt_ifp->if_index;
    982 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    983 		rtm->rtm_addrs = info.rti_addrs;
    984 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    985 			w->w_where = NULL;
    986 		else
    987 			w->w_where = (char *)w->w_where + size;
    988 	}
    989 	return error;
    990 }
    991 
    992 static int
    993 sysctl_iflist(int af, struct walkarg *w, int type)
    994 {
    995 	struct ifnet *ifp;
    996 	struct ifaddr *ifa;
    997 	struct	rt_addrinfo info;
    998 	int	len, error = 0;
    999 
   1000 	memset(&info, 0, sizeof(info));
   1001 	IFNET_FOREACH(ifp) {
   1002 		if (w->w_arg && w->w_arg != ifp->if_index)
   1003 			continue;
   1004 		if (IFADDR_EMPTY(ifp))
   1005 			continue;
   1006 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   1007 		switch (type) {
   1008 		case NET_RT_IFLIST:
   1009 			error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
   1010 			break;
   1011 #ifdef COMPAT_14
   1012 		case NET_RT_OIFLIST:
   1013 			error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
   1014 			break;
   1015 #endif
   1016 		default:
   1017 			panic("sysctl_iflist(1)");
   1018 		}
   1019 		if (error)
   1020 			return error;
   1021 		info.rti_info[RTAX_IFP] = NULL;
   1022 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1023 			switch (type) {
   1024 			case NET_RT_IFLIST: {
   1025 				struct if_msghdr *ifm;
   1026 
   1027 				ifm = (struct if_msghdr *)w->w_tmem;
   1028 				ifm->ifm_index = ifp->if_index;
   1029 				ifm->ifm_flags = ifp->if_flags;
   1030 				ifm->ifm_data = ifp->if_data;
   1031 				ifm->ifm_addrs = info.rti_addrs;
   1032 				error = copyout(ifm, w->w_where, len);
   1033 				if (error)
   1034 					return error;
   1035 				w->w_where = (char *)w->w_where + len;
   1036 				break;
   1037 			}
   1038 
   1039 #ifdef COMPAT_14
   1040 			case NET_RT_OIFLIST: {
   1041 				struct if_msghdr14 *ifm;
   1042 
   1043 				ifm = (struct if_msghdr14 *)w->w_tmem;
   1044 				ifm->ifm_index = ifp->if_index;
   1045 				ifm->ifm_flags = ifp->if_flags;
   1046 				ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
   1047 				ifm->ifm_data.ifi_addrlen =
   1048 				    ifp->if_data.ifi_addrlen;
   1049 				ifm->ifm_data.ifi_hdrlen =
   1050 				    ifp->if_data.ifi_hdrlen;
   1051 				ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
   1052 				ifm->ifm_data.ifi_metric =
   1053 				    ifp->if_data.ifi_metric;
   1054 				ifm->ifm_data.ifi_baudrate =
   1055 				    ifp->if_data.ifi_baudrate;
   1056 				ifm->ifm_data.ifi_ipackets =
   1057 				    ifp->if_data.ifi_ipackets;
   1058 				ifm->ifm_data.ifi_ierrors =
   1059 				    ifp->if_data.ifi_ierrors;
   1060 				ifm->ifm_data.ifi_opackets =
   1061 				    ifp->if_data.ifi_opackets;
   1062 				ifm->ifm_data.ifi_oerrors =
   1063 				    ifp->if_data.ifi_oerrors;
   1064 				ifm->ifm_data.ifi_collisions =
   1065 				    ifp->if_data.ifi_collisions;
   1066 				ifm->ifm_data.ifi_ibytes =
   1067 				    ifp->if_data.ifi_ibytes;
   1068 				ifm->ifm_data.ifi_obytes =
   1069 				    ifp->if_data.ifi_obytes;
   1070 				ifm->ifm_data.ifi_imcasts =
   1071 				    ifp->if_data.ifi_imcasts;
   1072 				ifm->ifm_data.ifi_omcasts =
   1073 				    ifp->if_data.ifi_omcasts;
   1074 				ifm->ifm_data.ifi_iqdrops =
   1075 				    ifp->if_data.ifi_iqdrops;
   1076 				ifm->ifm_data.ifi_noproto =
   1077 				    ifp->if_data.ifi_noproto;
   1078 				ifm->ifm_data.ifi_lastchange =
   1079 				    ifp->if_data.ifi_lastchange;
   1080 				ifm->ifm_addrs = info.rti_addrs;
   1081 				error = copyout(ifm, w->w_where, len);
   1082 				if (error)
   1083 					return error;
   1084 				w->w_where = (char *)w->w_where + len;
   1085 				break;
   1086 			}
   1087 #endif
   1088 			default:
   1089 				panic("sysctl_iflist(2)");
   1090 			}
   1091 		}
   1092 		IFADDR_FOREACH(ifa, ifp) {
   1093 			if (af && af != ifa->ifa_addr->sa_family)
   1094 				continue;
   1095 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
   1096 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1097 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   1098 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
   1099 				return error;
   1100 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1101 				struct ifa_msghdr *ifam;
   1102 
   1103 				ifam = (struct ifa_msghdr *)w->w_tmem;
   1104 				ifam->ifam_index = ifa->ifa_ifp->if_index;
   1105 				ifam->ifam_flags = ifa->ifa_flags;
   1106 				ifam->ifam_metric = ifa->ifa_metric;
   1107 				ifam->ifam_addrs = info.rti_addrs;
   1108 				error = copyout(w->w_tmem, w->w_where, len);
   1109 				if (error)
   1110 					return error;
   1111 				w->w_where = (char *)w->w_where + len;
   1112 			}
   1113 		}
   1114 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
   1115 		    info.rti_info[RTAX_BRD] = NULL;
   1116 	}
   1117 	return 0;
   1118 }
   1119 
   1120 static int
   1121 sysctl_rtable(SYSCTLFN_ARGS)
   1122 {
   1123 	void 	*where = oldp;
   1124 	size_t	*given = oldlenp;
   1125 	const void *new = newp;
   1126 	int	i, s, error = EINVAL;
   1127 	u_char  af;
   1128 	struct	walkarg w;
   1129 
   1130 	if (namelen == 1 && name[0] == CTL_QUERY)
   1131 		return sysctl_query(SYSCTLFN_CALL(rnode));
   1132 
   1133 	if (new)
   1134 		return EPERM;
   1135 	if (namelen != 3)
   1136 		return EINVAL;
   1137 	af = name[0];
   1138 	w.w_tmemneeded = 0;
   1139 	w.w_tmemsize = 0;
   1140 	w.w_tmem = NULL;
   1141 again:
   1142 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   1143 	if (w.w_tmemneeded) {
   1144 		w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
   1145 		w.w_tmemsize = w.w_tmemneeded;
   1146 		w.w_tmemneeded = 0;
   1147 	}
   1148 	w.w_op = name[1];
   1149 	w.w_arg = name[2];
   1150 	w.w_given = *given;
   1151 	w.w_needed = 0 - w.w_given;
   1152 	w.w_where = where;
   1153 
   1154 	s = splsoftnet();
   1155 	switch (w.w_op) {
   1156 
   1157 	case NET_RT_DUMP:
   1158 	case NET_RT_FLAGS:
   1159 		for (i = 1; i <= AF_MAX; i++)
   1160 			if ((af == 0 || af == i) &&
   1161 			    (error = rt_walktree(i, sysctl_dumpentry, &w)))
   1162 				break;
   1163 		break;
   1164 
   1165 #ifdef COMPAT_14
   1166 	case NET_RT_OIFLIST:
   1167 		error = sysctl_iflist(af, &w, w.w_op);
   1168 		break;
   1169 #endif
   1170 
   1171 	case NET_RT_IFLIST:
   1172 		error = sysctl_iflist(af, &w, w.w_op);
   1173 	}
   1174 	splx(s);
   1175 
   1176 	/* check to see if we couldn't allocate memory with NOWAIT */
   1177 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   1178 		goto again;
   1179 
   1180 	if (w.w_tmem)
   1181 		free(w.w_tmem, M_RTABLE);
   1182 	w.w_needed += w.w_given;
   1183 	if (where) {
   1184 		*given = (char *)w.w_where - (char *)where;
   1185 		if (*given < w.w_needed)
   1186 			return ENOMEM;
   1187 	} else {
   1188 		*given = (11 * w.w_needed) / 10;
   1189 	}
   1190 	return error;
   1191 }
   1192 
   1193 /*
   1194  * Routing message software interrupt routine
   1195  */
   1196 static void
   1197 route_intr(void *cookie)
   1198 {
   1199 	struct sockproto proto = { .sp_family = PF_ROUTE, };
   1200 	struct mbuf *m;
   1201 	int s;
   1202 
   1203 	mutex_enter(softnet_lock);
   1204 	KERNEL_LOCK(1, NULL);
   1205 	while (!IF_IS_EMPTY(&route_intrq)) {
   1206 		s = splnet();
   1207 		IF_DEQUEUE(&route_intrq, m);
   1208 		splx(s);
   1209 		if (m == NULL)
   1210 			break;
   1211 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
   1212 		raw_input(m, &proto, &route_src, &route_dst);
   1213 	}
   1214 	KERNEL_UNLOCK_ONE(NULL);
   1215 	mutex_exit(softnet_lock);
   1216 }
   1217 
   1218 /*
   1219  * Enqueue a message to the software interrupt routine.
   1220  */
   1221 static void
   1222 route_enqueue(struct mbuf *m, int family)
   1223 {
   1224 	int s, wasempty;
   1225 
   1226 	s = splnet();
   1227 	if (IF_QFULL(&route_intrq)) {
   1228 		IF_DROP(&route_intrq);
   1229 		m_freem(m);
   1230 	} else {
   1231 		wasempty = IF_IS_EMPTY(&route_intrq);
   1232 		M_SETCTX(m, (uintptr_t)family);
   1233 		IF_ENQUEUE(&route_intrq, m);
   1234 		if (wasempty)
   1235 			softint_schedule(route_sih);
   1236 	}
   1237 	splx(s);
   1238 }
   1239 
   1240 void
   1241 rt_init(void)
   1242 {
   1243 
   1244 	route_intrq.ifq_maxlen = route_maxqlen;
   1245 	route_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1246 	    route_intr, NULL);
   1247 }
   1248 
   1249 /*
   1250  * Definitions of protocols supported in the ROUTE domain.
   1251  */
   1252 PR_WRAP_USRREQ(route_usrreq)
   1253 #define	route_usrreq	route_usrreq_wrapper
   1254 
   1255 const struct protosw routesw[] = {
   1256 	{
   1257 		.pr_type = SOCK_RAW,
   1258 		.pr_domain = &routedomain,
   1259 		.pr_flags = PR_ATOMIC|PR_ADDR,
   1260 		.pr_input = raw_input,
   1261 		.pr_output = route_output,
   1262 		.pr_ctlinput = raw_ctlinput,
   1263 		.pr_usrreq = route_usrreq,
   1264 		.pr_init = raw_init,
   1265 	},
   1266 };
   1267 
   1268 struct domain routedomain = {
   1269 	.dom_family = PF_ROUTE,
   1270 	.dom_name = "route",
   1271 	.dom_init = route_init,
   1272 	.dom_protosw = routesw,
   1273 	.dom_protoswNPROTOSW = &routesw[__arraycount(routesw)],
   1274 };
   1275 
   1276 SYSCTL_SETUP(sysctl_net_route_setup, "sysctl net.route subtree setup")
   1277 {
   1278 	const struct sysctlnode *rnode = NULL;
   1279 
   1280 	sysctl_createv(clog, 0, NULL, NULL,
   1281 		       CTLFLAG_PERMANENT,
   1282 		       CTLTYPE_NODE, "net", NULL,
   1283 		       NULL, 0, NULL, 0,
   1284 		       CTL_NET, CTL_EOL);
   1285 
   1286 	sysctl_createv(clog, 0, NULL, &rnode,
   1287 		       CTLFLAG_PERMANENT,
   1288 		       CTLTYPE_NODE, "route",
   1289 		       SYSCTL_DESCR("PF_ROUTE information"),
   1290 		       NULL, 0, NULL, 0,
   1291 		       CTL_NET, PF_ROUTE, CTL_EOL);
   1292 	sysctl_createv(clog, 0, NULL, NULL,
   1293 		       CTLFLAG_PERMANENT,
   1294 		       CTLTYPE_NODE, "rtable",
   1295 		       SYSCTL_DESCR("Routing table information"),
   1296 		       sysctl_rtable, 0, NULL, 0,
   1297 		       CTL_NET, PF_ROUTE, 0 /* any protocol */, CTL_EOL);
   1298 	sysctl_createv(clog, 0, &rnode, NULL,
   1299 		       CTLFLAG_PERMANENT,
   1300 		       CTLTYPE_STRUCT, "stats",
   1301 		       SYSCTL_DESCR("Routing statistics"),
   1302 		       NULL, 0, &rtstat, sizeof(rtstat),
   1303 		       CTL_CREATE, CTL_EOL);
   1304 }
   1305