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