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