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