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