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