Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.122
      1 /*	$NetBSD: rtsock.c,v 1.122 2009/02/14 20:48:46 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.122 2009/02/14 20:48:46 christos 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 inline void rt_adjustcount(int, int);
    113 
    114 static inline 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 static inline void
    139 cvtmetrics(struct rt_metrics *ortm, const struct nrt_metrics *rtm)
    140 {
    141 	ortm->rmx_locks = rtm->rmx_locks;
    142 	ortm->rmx_mtu = rtm->rmx_mtu;
    143 	ortm->rmx_hopcount = rtm->rmx_hopcount;
    144 	ortm->rmx_expire = rtm->rmx_expire;
    145 	ortm->rmx_recvpipe = rtm->rmx_recvpipe;
    146 	ortm->rmx_sendpipe = rtm->rmx_sendpipe;
    147 	ortm->rmx_ssthresh = rtm->rmx_ssthresh;
    148 	ortm->rmx_rtt = rtm->rmx_rtt;
    149 	ortm->rmx_rttvar = rtm->rmx_rttvar;
    150 	ortm->rmx_pksent = rtm->rmx_pksent;
    151 }
    152 
    153 /*ARGSUSED*/
    154 int
    155 route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    156 	struct mbuf *control, struct lwp *l)
    157 {
    158 	int error = 0;
    159 	struct rawcb *rp = sotorawcb(so);
    160 	int s;
    161 
    162 	if (req == PRU_ATTACH) {
    163 		sosetlock(so);
    164 		rp = malloc(sizeof(*rp), M_PCB, M_WAITOK|M_ZERO);
    165 		so->so_pcb = rp;
    166 	}
    167 	if (req == PRU_DETACH && rp)
    168 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    169 	s = splsoftnet();
    170 
    171 	/*
    172 	 * Don't call raw_usrreq() in the attach case, because
    173 	 * we want to allow non-privileged processes to listen on
    174 	 * and send "safe" commands to the routing socket.
    175 	 */
    176 	if (req == PRU_ATTACH) {
    177 		if (l == NULL)
    178 			error = EACCES;
    179 		else
    180 			error = raw_attach(so, (int)(long)nam);
    181 	} else
    182 		error = raw_usrreq(so, req, m, nam, control, l);
    183 
    184 	rp = sotorawcb(so);
    185 	if (req == PRU_ATTACH && rp) {
    186 		if (error) {
    187 			free(rp, M_PCB);
    188 			splx(s);
    189 			return error;
    190 		}
    191 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    192 		rp->rcb_laddr = &route_src;
    193 		rp->rcb_faddr = &route_dst;
    194 		soisconnected(so);
    195 		so->so_options |= SO_USELOOPBACK;
    196 	}
    197 	splx(s);
    198 	return error;
    199 }
    200 
    201 static const struct sockaddr *
    202 intern_netmask(const struct sockaddr *mask)
    203 {
    204 	struct radix_node *rn;
    205 	extern struct radix_node_head *mask_rnhead;
    206 
    207 	if (mask != NULL &&
    208 	    (rn = rn_search(mask, mask_rnhead->rnh_treetop)))
    209 		mask = (const struct sockaddr *)rn->rn_key;
    210 
    211 	return mask;
    212 }
    213 
    214 /*ARGSUSED*/
    215 int
    216 route_output(struct mbuf *m, ...)
    217 {
    218 	struct sockproto proto = { .sp_family = PF_ROUTE, };
    219 	struct rt_msghdr *rtm = NULL;
    220 	struct rt_msghdr *old_rtm = NULL;
    221 	struct rtentry *rt = NULL;
    222 	struct rtentry *saved_nrt = NULL;
    223 	struct rt_addrinfo info;
    224 	int len, error = 0, ifa_route = 0;
    225 	struct ifnet *ifp = NULL;
    226 	struct ifaddr *ifa = NULL, *oifa;
    227 	struct socket *so;
    228 	va_list ap;
    229 	sa_family_t family;
    230 
    231 	va_start(ap, m);
    232 	so = va_arg(ap, struct socket *);
    233 	va_end(ap);
    234 
    235 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
    236 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
    237 	   (m = m_pullup(m, sizeof(int32_t))) == NULL))
    238 		return ENOBUFS;
    239 	if ((m->m_flags & M_PKTHDR) == 0)
    240 		panic("route_output");
    241 	len = m->m_pkthdr.len;
    242 	if (len < sizeof(*rtm) ||
    243 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
    244 		info.rti_info[RTAX_DST] = NULL;
    245 		senderr(EINVAL);
    246 	}
    247 	R_Malloc(rtm, struct rt_msghdr *, len);
    248 	if (rtm == NULL) {
    249 		info.rti_info[RTAX_DST] = NULL;
    250 		senderr(ENOBUFS);
    251 	}
    252 	m_copydata(m, 0, len, rtm);
    253 	if (rtm->rtm_version != RTM_VERSION) {
    254 		info.rti_info[RTAX_DST] = NULL;
    255 		senderr(EPROTONOSUPPORT);
    256 	}
    257 	rtm->rtm_pid = curproc->p_pid;
    258 	memset(&info, 0, sizeof(info));
    259 	info.rti_addrs = rtm->rtm_addrs;
    260 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
    261 	    &info))
    262 		senderr(EINVAL);
    263 	info.rti_flags = rtm->rtm_flags;
    264 #ifdef RTSOCK_DEBUG
    265 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    266 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
    267 		    inet_ntoa(((const struct sockaddr_in *)
    268 		    info.rti_info[RTAX_DST])->sin_addr));
    269 	}
    270 #endif /* RTSOCK_DEBUG */
    271 	if (info.rti_info[RTAX_DST] == NULL ||
    272 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX))
    273 		senderr(EINVAL);
    274 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
    275 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
    276 		senderr(EINVAL);
    277 
    278 	/*
    279 	 * Verify that the caller has the appropriate privilege; RTM_GET
    280 	 * is the only operation the non-superuser is allowed.
    281 	 */
    282 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
    283 	    0, rtm, NULL, NULL) != 0)
    284 		senderr(EACCES);
    285 
    286 	switch (rtm->rtm_type) {
    287 
    288 	case RTM_ADD:
    289 		if (info.rti_info[RTAX_GATEWAY] == NULL)
    290 			senderr(EINVAL);
    291 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    292 		if (error == 0 && saved_nrt) {
    293 			rt_setmetrics(rtm->rtm_inits,
    294 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
    295 			saved_nrt->rt_refcnt--;
    296 		}
    297 		break;
    298 
    299 	case RTM_DELETE:
    300 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    301 		if (error == 0) {
    302 			(rt = saved_nrt)->rt_refcnt++;
    303 			ifa = rt_get_ifa(rt);
    304 			/*
    305 			 * If deleting an automatic route, scrub the flag.
    306 			 */
    307 			if (ifa->ifa_flags & IFA_ROUTE)
    308 				ifa->ifa_flags &= ~IFA_ROUTE;
    309 			goto report;
    310 		}
    311 		break;
    312 
    313 	case RTM_GET:
    314 	case RTM_CHANGE:
    315 	case RTM_LOCK:
    316                 /* XXX This will mask info.rti_info[RTAX_DST] with
    317 		 * info.rti_info[RTAX_NETMASK] before
    318                  * searching.  It did not used to do that.  --dyoung
    319 		 */
    320 		error = rtrequest1(RTM_GET, &info, &rt);
    321 		if (error != 0)
    322 			senderr(error);
    323 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
    324 			struct radix_node *rn;
    325 
    326 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
    327 			    info.rti_info[RTAX_DST]->sa_len) != 0)
    328 				senderr(ESRCH);
    329 			info.rti_info[RTAX_NETMASK] = intern_netmask(
    330 			    info.rti_info[RTAX_NETMASK]);
    331 			for (rn = rt->rt_nodes; rn; rn = rn->rn_dupedkey)
    332 				if (info.rti_info[RTAX_NETMASK] ==
    333 				    (const struct sockaddr *)rn->rn_mask)
    334 					break;
    335 			if (rn == NULL)
    336 				senderr(ETOOMANYREFS);
    337 			rt = (struct rtentry *)rn;
    338 		}
    339 
    340 		switch (rtm->rtm_type) {
    341 		case RTM_GET:
    342 		report:
    343 			info.rti_info[RTAX_DST] = rt_getkey(rt);
    344 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    345 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    346 			if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
    347 				;
    348 			else if ((ifp = rt->rt_ifp) != NULL) {
    349 				const struct ifaddr *rtifa;
    350 				info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    351                                 /* rtifa used to be simply rt->rt_ifa.
    352                                  * If rt->rt_ifa != NULL, then
    353                                  * rt_get_ifa() != NULL.  So this
    354                                  * ought to still be safe. --dyoung
    355 				 */
    356 				rtifa = rt_get_ifa(rt);
    357 				info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
    358 #ifdef RTSOCK_DEBUG
    359 				if (info.rti_info[RTAX_IFA]->sa_family ==
    360 				    AF_INET) {
    361 					printf("%s: copying out RTAX_IFA %s ",
    362 					    __func__, inet_ntoa(
    363 					    (const struct sockaddr_in *)
    364 					    info.rti_info[RTAX_IFA])->sin_addr);
    365 					printf("for info.rti_info[RTAX_DST] %s "
    366 					    "ifa_getifa %p ifa_seqno %p\n",
    367 					    inet_ntoa(
    368 					    (const struct sockaddr_in *)
    369 					    info.rti_info[RTAX_DST])->sin_addr),
    370 					    (void *)rtifa->ifa_getifa,
    371 					    rtifa->ifa_seqno);
    372 				}
    373 #endif /* RTSOCK_DEBUG */
    374 				if (ifp->if_flags & IFF_POINTOPOINT) {
    375 					info.rti_info[RTAX_BRD] =
    376 					    rtifa->ifa_dstaddr;
    377 				} else
    378 					info.rti_info[RTAX_BRD] = NULL;
    379 				rtm->rtm_index = ifp->if_index;
    380 			} else {
    381 				info.rti_info[RTAX_IFP] = NULL;
    382 				info.rti_info[RTAX_IFA] = NULL;
    383 			}
    384 			(void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
    385 			if (len > rtm->rtm_msglen) {
    386 				old_rtm = rtm;
    387 				R_Malloc(rtm, struct rt_msghdr *, len);
    388 				if (rtm == NULL)
    389 					senderr(ENOBUFS);
    390 				(void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
    391 			}
    392 			(void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
    393 			rtm->rtm_flags = rt->rt_flags;
    394 			cvtmetrics(&rtm->rtm_rmx, &rt->rt_rmx);
    395 			rtm->rtm_addrs = info.rti_addrs;
    396 			break;
    397 
    398 		case RTM_CHANGE:
    399 			/*
    400 			 * new gateway could require new ifaddr, ifp;
    401 			 * flags may also be different; ifp may be specified
    402 			 * by ll sockaddr when protocol address is ambiguous
    403 			 */
    404 			if ((error = rt_getifa(&info)) != 0)
    405 				senderr(error);
    406 			if (info.rti_info[RTAX_GATEWAY] &&
    407 			    rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
    408 				senderr(EDQUOT);
    409 			/* new gateway could require new ifaddr, ifp;
    410 			   flags may also be different; ifp may be specified
    411 			   by ll sockaddr when protocol address is ambiguous */
    412 			if (info.rti_info[RTAX_IFP] &&
    413 			    (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
    414 			    (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
    415 			    info.rti_info[RTAX_GATEWAY])) {
    416 				ifa = ifaof_ifpforaddr(info.rti_info[RTAX_IFA] ?
    417 				    info.rti_info[RTAX_IFA] :
    418 				    info.rti_info[RTAX_GATEWAY], ifp);
    419 			} else if ((info.rti_info[RTAX_IFA] &&
    420 			    (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
    421 			    (info.rti_info[RTAX_GATEWAY] &&
    422 			    (ifa = ifa_ifwithroute(rt->rt_flags,
    423 			    rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
    424 				ifp = ifa->ifa_ifp;
    425 			}
    426 			oifa = rt->rt_ifa;
    427 			if (oifa && oifa->ifa_flags & IFA_ROUTE) {
    428 				/*
    429 				 * If changing an automatically added route,
    430 				 * remove the flag and store the fact.
    431 				 */
    432 				oifa->ifa_flags &= ~IFA_ROUTE;
    433 				ifa_route = 1;
    434 			}
    435 			if (ifa) {
    436 				if (oifa != ifa) {
    437 					if (oifa && oifa->ifa_rtrequest) {
    438 						oifa->ifa_rtrequest(RTM_DELETE,
    439 						    rt, &info);
    440 					}
    441 					/*
    442 					 * If changing an automatically added
    443 					 * route, store this if not static.
    444 					 */
    445 					if (ifa_route &&
    446 					    !(rt->rt_flags & RTF_STATIC))
    447 						ifa->ifa_flags |= IFA_ROUTE;
    448 					rt_replace_ifa(rt, ifa);
    449 					rt->rt_ifp = ifp;
    450 				}
    451 			}
    452 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
    453 			    &rt->rt_rmx);
    454 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    455 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
    456 			/*FALLTHROUGH*/
    457 		case RTM_LOCK:
    458 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    459 			rt->rt_rmx.rmx_locks |=
    460 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    461 			break;
    462 		}
    463 		break;
    464 
    465 	default:
    466 		senderr(EOPNOTSUPP);
    467 	}
    468 
    469 flush:
    470 	if (rtm) {
    471 		if (error)
    472 			rtm->rtm_errno = error;
    473 		else
    474 			rtm->rtm_flags |= RTF_DONE;
    475 	}
    476 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
    477 	    0;
    478 	/* We cannot free old_rtm until we have stopped using the
    479 	 * pointers in info, some of which may point to sockaddrs
    480 	 * in old_rtm.
    481 	 */
    482 	if (old_rtm != NULL)
    483 		Free(old_rtm);
    484 	if (rt)
    485 		rtfree(rt);
    486     {
    487 	struct rawcb *rp = NULL;
    488 	/*
    489 	 * Check to see if we don't want our own messages.
    490 	 */
    491 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    492 		if (route_cb.any_count <= 1) {
    493 			if (rtm)
    494 				Free(rtm);
    495 			m_freem(m);
    496 			return error;
    497 		}
    498 		/* There is another listener, so construct message */
    499 		rp = sotorawcb(so);
    500 	}
    501 	if (rtm) {
    502 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
    503 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
    504 			m_freem(m);
    505 			m = NULL;
    506 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
    507 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
    508 		Free(rtm);
    509 	}
    510 	if (rp)
    511 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    512 	if (family)
    513 		proto.sp_protocol = family;
    514 	if (m)
    515 		raw_input(m, &proto, &route_src, &route_dst);
    516 	if (rp)
    517 		rp->rcb_proto.sp_family = PF_ROUTE;
    518     }
    519 	return error;
    520 }
    521 
    522 void
    523 rt_setmetrics(u_long which, const struct rt_metrics *in, struct nrt_metrics *out)
    524 {
    525 #define metric(f, e) if (which & (f)) out->e = in->e;
    526 	metric(RTV_RPIPE, rmx_recvpipe);
    527 	metric(RTV_SPIPE, rmx_sendpipe);
    528 	metric(RTV_SSTHRESH, rmx_ssthresh);
    529 	metric(RTV_RTT, rmx_rtt);
    530 	metric(RTV_RTTVAR, rmx_rttvar);
    531 	metric(RTV_HOPCOUNT, rmx_hopcount);
    532 	metric(RTV_MTU, rmx_mtu);
    533 	/* XXX time_t: Will not work after February 2145 (u_long time) */
    534 	metric(RTV_EXPIRE, rmx_expire);
    535 #undef metric
    536 }
    537 
    538 #define ROUNDUP(a) \
    539 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    540 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    541 
    542 static int
    543 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
    544     struct rt_addrinfo *rtinfo)
    545 {
    546 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
    547 	int i;
    548 
    549 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
    550 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    551 			continue;
    552 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
    553 		ADVANCE(cp, sa);
    554 	}
    555 
    556 	/*
    557 	 * Check for extra addresses specified, except RTM_GET asking
    558 	 * for interface info.
    559 	 */
    560 	if (rtmtype == RTM_GET) {
    561 		if (((rtinfo->rti_addrs &
    562 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
    563 			return 1;
    564 	} else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
    565 		return 1;
    566 	/* Check for bad data length.  */
    567 	if (cp != cplim) {
    568 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
    569 		    cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
    570 			/*
    571 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
    572 			 * We accept this for now for the sake of old
    573 			 * binaries or third party softwares.
    574 			 */
    575 			;
    576 		else
    577 			return 1;
    578 	}
    579 	return 0;
    580 }
    581 
    582 struct mbuf *
    583 rt_msg1(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
    584 {
    585 	struct rt_msghdr *rtm;
    586 	struct mbuf *m;
    587 	int i;
    588 	const struct sockaddr *sa;
    589 	int len, dlen;
    590 
    591 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    592 	if (m == NULL)
    593 		return m;
    594 	MCLAIM(m, &routedomain.dom_mowner);
    595 	switch (type) {
    596 
    597 	case RTM_DELADDR:
    598 	case RTM_NEWADDR:
    599 		len = sizeof(struct ifa_msghdr);
    600 		break;
    601 
    602 #ifdef COMPAT_14
    603 	case RTM_OOIFINFO:
    604 		len = sizeof(struct if_msghdr14);
    605 		break;
    606 #endif
    607 #ifdef COMPAT_50
    608 	case RTM_OIFINFO:
    609 		len = sizeof(struct if_msghdr50);
    610 		break;
    611 #endif
    612 
    613 	case RTM_IFINFO:
    614 		len = sizeof(struct if_msghdr);
    615 		break;
    616 
    617 	case RTM_IFANNOUNCE:
    618 	case RTM_IEEE80211:
    619 		len = sizeof(struct if_announcemsghdr);
    620 		break;
    621 
    622 	default:
    623 		len = sizeof(struct rt_msghdr);
    624 	}
    625 	if (len > MHLEN + MLEN)
    626 		panic("rt_msg1: message too long");
    627 	else if (len > MHLEN) {
    628 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
    629 		if (m->m_next == NULL) {
    630 			m_freem(m);
    631 			return NULL;
    632 		}
    633 		MCLAIM(m->m_next, m->m_owner);
    634 		m->m_pkthdr.len = len;
    635 		m->m_len = MHLEN;
    636 		m->m_next->m_len = len - MHLEN;
    637 	} else {
    638 		m->m_pkthdr.len = m->m_len = len;
    639 	}
    640 	m->m_pkthdr.rcvif = NULL;
    641 	m_copyback(m, 0, datalen, data);
    642 	if (len > datalen)
    643 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
    644 	rtm = mtod(m, struct rt_msghdr *);
    645 	for (i = 0; i < RTAX_MAX; i++) {
    646 		if ((sa = rtinfo->rti_info[i]) == NULL)
    647 			continue;
    648 		rtinfo->rti_addrs |= (1 << i);
    649 		dlen = ROUNDUP(sa->sa_len);
    650 		m_copyback(m, len, dlen, sa);
    651 		len += dlen;
    652 	}
    653 	if (m->m_pkthdr.len != len) {
    654 		m_freem(m);
    655 		return NULL;
    656 	}
    657 	rtm->rtm_msglen = len;
    658 	rtm->rtm_version = RTM_VERSION;
    659 	rtm->rtm_type = type;
    660 	return m;
    661 }
    662 
    663 /*
    664  * rt_msg2
    665  *
    666  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    667  *		returns the length of the message in 'lenp'.
    668  *
    669  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    670  *	the message
    671  * otherwise walkarg's w_needed is updated and if the user buffer is
    672  *	specified and w_needed indicates space exists the information is copied
    673  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    674  *	if the allocation fails ENOBUFS is returned.
    675  */
    676 static int
    677 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
    678 	int *lenp)
    679 {
    680 	int i;
    681 	int len, dlen, second_time = 0;
    682 	char *cp0, *cp = cpv;
    683 
    684 	rtinfo->rti_addrs = 0;
    685 again:
    686 	switch (type) {
    687 
    688 	case RTM_DELADDR:
    689 	case RTM_NEWADDR:
    690 		len = sizeof(struct ifa_msghdr);
    691 		break;
    692 #ifdef COMPAT_14
    693 	case RTM_OOIFINFO:
    694 		len = sizeof(struct if_msghdr14);
    695 		break;
    696 #endif
    697 #ifdef COMPAT_50
    698 	case RTM_OIFINFO:
    699 		len = sizeof(struct if_msghdr50);
    700 		break;
    701 #endif
    702 
    703 	case RTM_IFINFO:
    704 		len = sizeof(struct if_msghdr);
    705 		break;
    706 
    707 	default:
    708 		len = sizeof(struct rt_msghdr);
    709 	}
    710 	if ((cp0 = cp) != NULL)
    711 		cp += len;
    712 	for (i = 0; i < RTAX_MAX; i++) {
    713 		const struct sockaddr *sa;
    714 
    715 		if ((sa = rtinfo->rti_info[i]) == NULL)
    716 			continue;
    717 		rtinfo->rti_addrs |= (1 << i);
    718 		dlen = ROUNDUP(sa->sa_len);
    719 		if (cp) {
    720 			(void)memcpy(cp, sa, (size_t)dlen);
    721 			cp += dlen;
    722 		}
    723 		len += dlen;
    724 	}
    725 	if (cp == NULL && w != NULL && !second_time) {
    726 		struct rt_walkarg *rw = w;
    727 
    728 		rw->w_needed += len;
    729 		if (rw->w_needed <= 0 && rw->w_where) {
    730 			if (rw->w_tmemsize < len) {
    731 				if (rw->w_tmem)
    732 					free(rw->w_tmem, M_RTABLE);
    733 				rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
    734 				if (rw->w_tmem)
    735 					rw->w_tmemsize = len;
    736 				else
    737 					rw->w_tmemsize = 0;
    738 			}
    739 			if (rw->w_tmem) {
    740 				cp = rw->w_tmem;
    741 				second_time = 1;
    742 				goto again;
    743 			} else {
    744 				rw->w_tmemneeded = len;
    745 				return ENOBUFS;
    746 			}
    747 		}
    748 	}
    749 	if (cp) {
    750 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
    751 
    752 		rtm->rtm_version = RTM_VERSION;
    753 		rtm->rtm_type = type;
    754 		rtm->rtm_msglen = len;
    755 	}
    756 	if (lenp)
    757 		*lenp = len;
    758 	return 0;
    759 }
    760 
    761 /*
    762  * This routine is called to generate a message from the routing
    763  * socket indicating that a redirect has occurred, a routing lookup
    764  * has failed, or that a protocol has detected timeouts to a particular
    765  * destination.
    766  */
    767 void
    768 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
    769 {
    770 	struct rt_msghdr rtm;
    771 	struct mbuf *m;
    772 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    773 
    774 	if (route_cb.any_count == 0)
    775 		return;
    776 	memset(&rtm, 0, sizeof(rtm));
    777 	rtm.rtm_flags = RTF_DONE | flags;
    778 	rtm.rtm_errno = error;
    779 	m = rt_msg1(type, rtinfo, &rtm, sizeof(rtm));
    780 	if (m == NULL)
    781 		return;
    782 	mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
    783 	route_enqueue(m, sa ? sa->sa_family : 0);
    784 }
    785 
    786 /*
    787  * This routine is called to generate a message from the routing
    788  * socket indicating that the status of a network interface has changed.
    789  */
    790 void
    791 rt_ifmsg(struct ifnet *ifp)
    792 {
    793 	struct if_msghdr ifm;
    794 	struct mbuf *m;
    795 	struct rt_addrinfo info;
    796 
    797 	if (route_cb.any_count == 0)
    798 		return;
    799 	(void)memset(&info, 0, sizeof(info));
    800 	(void)memset(&ifm, 0, sizeof(ifm));
    801 	ifm.ifm_index = ifp->if_index;
    802 	ifm.ifm_flags = ifp->if_flags;
    803 	ifm.ifm_data = ifp->if_data;
    804 	ifm.ifm_addrs = 0;
    805 	m = rt_msg1(RTM_IFINFO, &info, &ifm, sizeof(ifm));
    806 	if (m == NULL)
    807 		return;
    808 	route_enqueue(m, 0);
    809 #ifdef COMPAT_14
    810 	compat_14_rt_ifmsg(ifp, &ifm);
    811 #endif
    812 #ifdef COMPAT_50
    813 	compat_50_rt_ifmsg(ifp, &ifm);
    814 #endif
    815 }
    816 
    817 
    818 /*
    819  * This is called to generate messages from the routing socket
    820  * indicating a network interface has had addresses associated with it.
    821  * if we ever reverse the logic and replace messages TO the routing
    822  * socket indicate a request to configure interfaces, then it will
    823  * be unnecessary as the routing socket will automatically generate
    824  * copies of it.
    825  */
    826 void
    827 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
    828 {
    829 #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
    830 	struct rt_addrinfo info;
    831 	const struct sockaddr *sa;
    832 	int pass;
    833 	struct mbuf *m;
    834 	struct ifnet *ifp = ifa->ifa_ifp;
    835 	struct rt_msghdr rtm;
    836 	struct ifa_msghdr ifam;
    837 	int ncmd;
    838 
    839 	if (route_cb.any_count == 0)
    840 		return;
    841 	for (pass = 1; pass < 3; pass++) {
    842 		memset(&info, 0, sizeof(info));
    843 		switch (cmdpass(cmd, pass)) {
    844 		case cmdpass(RTM_ADD, 1):
    845 		case cmdpass(RTM_CHANGE, 1):
    846 		case cmdpass(RTM_DELETE, 2):
    847 			if (cmd == RTM_ADD)
    848 				ncmd = RTM_NEWADDR;
    849 			else
    850 				ncmd = RTM_DELADDR;
    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 	route_intrq.ifq_maxlen = route_maxqlen;
   1245 	route_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1246 	    route_intr, NULL);
   1247 }
   1248 
   1249 /*
   1250  * Definitions of protocols supported in the ROUTE domain.
   1251  */
   1252 PR_WRAP_USRREQ(route_usrreq)
   1253 #define	route_usrreq	route_usrreq_wrapper
   1254 
   1255 const struct protosw routesw[] = {
   1256 	{
   1257 		.pr_type = SOCK_RAW,
   1258 		.pr_domain = &routedomain,
   1259 		.pr_flags = PR_ATOMIC|PR_ADDR,
   1260 		.pr_input = raw_input,
   1261 		.pr_output = route_output,
   1262 		.pr_ctlinput = raw_ctlinput,
   1263 		.pr_usrreq = route_usrreq,
   1264 		.pr_init = raw_init,
   1265 	},
   1266 };
   1267 
   1268 struct domain routedomain = {
   1269 	.dom_family = PF_ROUTE,
   1270 	.dom_name = "route",
   1271 	.dom_init = route_init,
   1272 	.dom_protosw = routesw,
   1273 	.dom_protoswNPROTOSW = &routesw[__arraycount(routesw)],
   1274 };
   1275 
   1276 SYSCTL_SETUP(sysctl_net_route_setup, "sysctl net.route subtree setup")
   1277 {
   1278 	const struct sysctlnode *rnode = NULL;
   1279 
   1280 	sysctl_createv(clog, 0, NULL, NULL,
   1281 		       CTLFLAG_PERMANENT,
   1282 		       CTLTYPE_NODE, "net", NULL,
   1283 		       NULL, 0, NULL, 0,
   1284 		       CTL_NET, CTL_EOL);
   1285 
   1286 	sysctl_createv(clog, 0, NULL, &rnode,
   1287 		       CTLFLAG_PERMANENT,
   1288 		       CTLTYPE_NODE, "route",
   1289 		       SYSCTL_DESCR("PF_ROUTE information"),
   1290 		       NULL, 0, NULL, 0,
   1291 		       CTL_NET, PF_ROUTE, CTL_EOL);
   1292 	sysctl_createv(clog, 0, NULL, NULL,
   1293 		       CTLFLAG_PERMANENT,
   1294 		       CTLTYPE_NODE, "rtable",
   1295 		       SYSCTL_DESCR("Routing table information"),
   1296 		       sysctl_rtable, 0, NULL, 0,
   1297 		       CTL_NET, PF_ROUTE, 0 /* any protocol */, CTL_EOL);
   1298 	sysctl_createv(clog, 0, &rnode, NULL,
   1299 		       CTLFLAG_PERMANENT,
   1300 		       CTLTYPE_STRUCT, "stats",
   1301 		       SYSCTL_DESCR("Routing statistics"),
   1302 		       NULL, 0, &rtstat, sizeof(rtstat),
   1303 		       CTL_CREATE, CTL_EOL);
   1304 }
   1305