Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.28.2.1.2.1
      1  1.28.2.1.2.1   thorpej /*	$NetBSD: rtsock.c,v 1.28.2.1.2.1 1999/06/21 01:27:37 thorpej Exp $	*/
      2          1.11       cgd 
      3           1.1       cgd /*
      4          1.10   mycroft  * Copyright (c) 1988, 1991, 1993
      5          1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6           1.1       cgd  *
      7           1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8           1.1       cgd  * modification, are permitted provided that the following conditions
      9           1.1       cgd  * are met:
     10           1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11           1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12           1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13           1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14           1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15           1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16           1.1       cgd  *    must display the following acknowledgement:
     17           1.1       cgd  *	This product includes software developed by the University of
     18           1.1       cgd  *	California, Berkeley and its contributors.
     19           1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20           1.1       cgd  *    may be used to endorse or promote products derived from this software
     21           1.1       cgd  *    without specific prior written permission.
     22           1.1       cgd  *
     23           1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24           1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25           1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26           1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27           1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28           1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29           1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30           1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31           1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32           1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33           1.1       cgd  * SUCH DAMAGE.
     34           1.1       cgd  *
     35          1.26      fvdl  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
     36           1.1       cgd  */
     37           1.1       cgd 
     38           1.5   mycroft #include <sys/param.h>
     39           1.5   mycroft #include <sys/systm.h>
     40          1.10   mycroft #include <sys/proc.h>
     41           1.5   mycroft #include <sys/mbuf.h>
     42           1.5   mycroft #include <sys/socket.h>
     43           1.5   mycroft #include <sys/socketvar.h>
     44           1.5   mycroft #include <sys/domain.h>
     45           1.5   mycroft #include <sys/protosw.h>
     46           1.5   mycroft 
     47          1.17  christos #include <vm/vm.h>
     48          1.17  christos #include <sys/sysctl.h>
     49          1.17  christos 
     50           1.5   mycroft #include <net/if.h>
     51           1.5   mycroft #include <net/route.h>
     52           1.5   mycroft #include <net/raw_cb.h>
     53           1.1       cgd 
     54          1.17  christos #include <machine/stdarg.h>
     55          1.17  christos 
     56          1.10   mycroft struct	sockaddr route_dst = { 2, PF_ROUTE, };
     57          1.10   mycroft struct	sockaddr route_src = { 2, PF_ROUTE, };
     58          1.10   mycroft struct	sockproto route_proto = { PF_ROUTE, };
     59          1.10   mycroft 
     60          1.10   mycroft struct walkarg {
     61      1.28.2.1    chopps 	int	w_op;
     62      1.28.2.1    chopps 	int	w_arg;
     63      1.28.2.1    chopps 	int	w_given;
     64      1.28.2.1    chopps 	int	w_needed;
     65      1.28.2.1    chopps 	caddr_t	w_where;
     66      1.28.2.1    chopps 	int	w_tmemsize;
     67      1.28.2.1    chopps 	int	w_tmemneeded;
     68      1.28.2.1    chopps 	caddr_t	w_tmem;
     69          1.10   mycroft };
     70           1.1       cgd 
     71          1.21  christos static struct mbuf *rt_msg1 __P((int, struct rt_addrinfo *));
     72      1.28.2.1    chopps static int rt_msg2 __P((int, struct rt_addrinfo *, caddr_t, struct walkarg *,
     73      1.28.2.1    chopps     int *));
     74          1.21  christos static void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
     75          1.27  christos static __inline void rt_adjustcount __P((int, int));
     76          1.10   mycroft 
     77          1.10   mycroft /* Sleazy use of local variables throughout file, warning!!!! */
     78          1.10   mycroft #define dst	info.rti_info[RTAX_DST]
     79          1.10   mycroft #define gate	info.rti_info[RTAX_GATEWAY]
     80          1.10   mycroft #define netmask	info.rti_info[RTAX_NETMASK]
     81          1.10   mycroft #define genmask	info.rti_info[RTAX_GENMASK]
     82          1.10   mycroft #define ifpaddr	info.rti_info[RTAX_IFP]
     83          1.10   mycroft #define ifaaddr	info.rti_info[RTAX_IFA]
     84          1.10   mycroft #define brdaddr	info.rti_info[RTAX_BRD]
     85           1.1       cgd 
     86          1.27  christos static __inline void
     87          1.27  christos rt_adjustcount(af, cnt)
     88          1.27  christos 	int af, cnt;
     89          1.27  christos {
     90          1.28  christos 	route_cb.any_count += cnt;
     91          1.27  christos 	switch (af) {
     92          1.27  christos 	case AF_INET:
     93          1.27  christos 		route_cb.ip_count += cnt;
     94          1.27  christos 		return;
     95          1.27  christos 	case AF_IPX:
     96          1.27  christos 		route_cb.ipx_count += cnt;
     97          1.27  christos 		return;
     98          1.27  christos 	case AF_NS:
     99          1.27  christos 		route_cb.ns_count += cnt;
    100          1.27  christos 		return;
    101          1.27  christos 	case AF_ISO:
    102          1.27  christos 		route_cb.iso_count += cnt;
    103          1.27  christos 		return;
    104          1.27  christos 	}
    105          1.27  christos }
    106          1.27  christos 
    107           1.1       cgd /*ARGSUSED*/
    108           1.9   mycroft int
    109          1.19   mycroft route_usrreq(so, req, m, nam, control, p)
    110           1.1       cgd 	register struct socket *so;
    111           1.1       cgd 	int req;
    112           1.1       cgd 	struct mbuf *m, *nam, *control;
    113          1.19   mycroft 	struct proc *p;
    114           1.1       cgd {
    115           1.1       cgd 	register int error = 0;
    116           1.1       cgd 	register struct rawcb *rp = sotorawcb(so);
    117           1.1       cgd 	int s;
    118          1.10   mycroft 
    119           1.1       cgd 	if (req == PRU_ATTACH) {
    120           1.1       cgd 		MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
    121          1.17  christos 		if ((so->so_pcb = rp) != NULL)
    122          1.17  christos 			bzero(so->so_pcb, sizeof(*rp));
    123           1.1       cgd 
    124           1.1       cgd 	}
    125          1.27  christos 	if (req == PRU_DETACH && rp)
    126          1.27  christos 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    127          1.14   mycroft 	s = splsoftnet();
    128          1.23   thorpej 
    129          1.23   thorpej 	/*
    130          1.23   thorpej 	 * Don't call raw_usrreq() in the attach case, because
    131          1.23   thorpej 	 * we want to allow non-privileged processes to listen on
    132          1.23   thorpej 	 * and send "safe" commands to the routing socket.
    133          1.23   thorpej 	 */
    134          1.23   thorpej 	if (req == PRU_ATTACH) {
    135          1.23   thorpej 		if (p == 0)
    136          1.23   thorpej 			error = EACCES;
    137          1.23   thorpej 		else
    138          1.23   thorpej 			error = raw_attach(so, (int)(long)nam);
    139          1.23   thorpej 	} else
    140          1.23   thorpej 		error = raw_usrreq(so, req, m, nam, control, p);
    141          1.23   thorpej 
    142           1.1       cgd 	rp = sotorawcb(so);
    143           1.1       cgd 	if (req == PRU_ATTACH && rp) {
    144           1.1       cgd 		if (error) {
    145           1.1       cgd 			free((caddr_t)rp, M_PCB);
    146           1.1       cgd 			splx(s);
    147           1.1       cgd 			return (error);
    148           1.1       cgd 		}
    149          1.27  christos 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    150          1.20   mycroft 		rp->rcb_laddr = &route_src;
    151          1.20   mycroft 		rp->rcb_faddr = &route_dst;
    152           1.1       cgd 		soisconnected(so);
    153           1.1       cgd 		so->so_options |= SO_USELOOPBACK;
    154           1.1       cgd 	}
    155           1.1       cgd 	splx(s);
    156           1.1       cgd 	return (error);
    157           1.1       cgd }
    158           1.1       cgd 
    159           1.1       cgd /*ARGSUSED*/
    160           1.9   mycroft int
    161          1.17  christos #if __STDC__
    162          1.17  christos route_output(struct mbuf *m, ...)
    163          1.17  christos #else
    164          1.17  christos route_output(m, va_alist)
    165          1.17  christos 	struct mbuf *m;
    166          1.17  christos 	va_dcl
    167          1.17  christos #endif
    168           1.1       cgd {
    169           1.1       cgd 	register struct rt_msghdr *rtm = 0;
    170           1.1       cgd 	register struct rtentry *rt = 0;
    171           1.1       cgd 	struct rtentry *saved_nrt = 0;
    172          1.16       cgd 	struct radix_node_head *rnh;
    173          1.10   mycroft 	struct rt_addrinfo info;
    174           1.1       cgd 	int len, error = 0;
    175           1.1       cgd 	struct ifnet *ifp = 0;
    176           1.1       cgd 	struct ifaddr *ifa = 0;
    177          1.17  christos 	struct socket *so;
    178          1.17  christos 	va_list ap;
    179          1.17  christos 
    180          1.17  christos 	va_start(ap, m);
    181          1.17  christos 	so = va_arg(ap, struct socket *);
    182          1.17  christos 	va_end(ap);
    183          1.17  christos 
    184           1.1       cgd 
    185           1.1       cgd #define senderr(e) { error = e; goto flush;}
    186          1.12       cgd 	if (m == 0 || ((m->m_len < sizeof(int32_t)) &&
    187          1.21  christos 	   (m = m_pullup(m, sizeof(int32_t))) == 0))
    188           1.1       cgd 		return (ENOBUFS);
    189           1.1       cgd 	if ((m->m_flags & M_PKTHDR) == 0)
    190           1.1       cgd 		panic("route_output");
    191           1.1       cgd 	len = m->m_pkthdr.len;
    192           1.1       cgd 	if (len < sizeof(*rtm) ||
    193          1.10   mycroft 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
    194          1.10   mycroft 		dst = 0;
    195           1.1       cgd 		senderr(EINVAL);
    196          1.10   mycroft 	}
    197           1.1       cgd 	R_Malloc(rtm, struct rt_msghdr *, len);
    198          1.10   mycroft 	if (rtm == 0) {
    199          1.10   mycroft 		dst = 0;
    200           1.1       cgd 		senderr(ENOBUFS);
    201          1.10   mycroft 	}
    202           1.1       cgd 	m_copydata(m, 0, len, (caddr_t)rtm);
    203          1.10   mycroft 	if (rtm->rtm_version != RTM_VERSION) {
    204          1.10   mycroft 		dst = 0;
    205           1.1       cgd 		senderr(EPROTONOSUPPORT);
    206          1.10   mycroft 	}
    207           1.1       cgd 	rtm->rtm_pid = curproc->p_pid;
    208          1.10   mycroft 	info.rti_addrs = rtm->rtm_addrs;
    209          1.10   mycroft 	rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info);
    210          1.26      fvdl 	if (dst == 0 || (dst->sa_family >= AF_MAX))
    211          1.26      fvdl 		senderr(EINVAL);
    212          1.26      fvdl 	if (gate != 0 && (gate->sa_family >= AF_MAX))
    213           1.1       cgd 		senderr(EINVAL);
    214          1.10   mycroft 	if (genmask) {
    215          1.10   mycroft 		struct radix_node *t;
    216          1.16       cgd 		t = rn_addmask((caddr_t)genmask, 0, 1);
    217           1.1       cgd 		if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
    218           1.1       cgd 			genmask = (struct sockaddr *)(t->rn_key);
    219           1.1       cgd 		else
    220           1.1       cgd 			senderr(ENOBUFS);
    221           1.1       cgd 	}
    222          1.23   thorpej 
    223          1.23   thorpej 	/*
    224          1.23   thorpej 	 * Verify that the caller has the appropriate privilege; RTM_GET
    225          1.23   thorpej 	 * is the only operation the non-superuser is allowed.
    226          1.23   thorpej 	 */
    227          1.23   thorpej 	if (rtm->rtm_type != RTM_GET &&
    228          1.23   thorpej 	    suser(curproc->p_ucred, &curproc->p_acflag) != 0)
    229          1.23   thorpej 		senderr(EACCES);
    230          1.23   thorpej 
    231           1.1       cgd 	switch (rtm->rtm_type) {
    232          1.10   mycroft 
    233           1.1       cgd 	case RTM_ADD:
    234           1.1       cgd 		if (gate == 0)
    235           1.1       cgd 			senderr(EINVAL);
    236           1.1       cgd 		error = rtrequest(RTM_ADD, dst, gate, netmask,
    237          1.21  christos 		    rtm->rtm_flags, &saved_nrt);
    238           1.1       cgd 		if (error == 0 && saved_nrt) {
    239           1.1       cgd 			rt_setmetrics(rtm->rtm_inits,
    240          1.21  christos 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
    241           1.1       cgd 			saved_nrt->rt_refcnt--;
    242           1.1       cgd 			saved_nrt->rt_genmask = genmask;
    243           1.1       cgd 		}
    244           1.1       cgd 		break;
    245           1.1       cgd 
    246           1.1       cgd 	case RTM_DELETE:
    247           1.1       cgd 		error = rtrequest(RTM_DELETE, dst, gate, netmask,
    248          1.21  christos 		    rtm->rtm_flags, &saved_nrt);
    249          1.16       cgd 		if (error == 0) {
    250          1.16       cgd 			(rt = saved_nrt)->rt_refcnt++;
    251          1.16       cgd 			goto report;
    252          1.16       cgd 		}
    253           1.1       cgd 		break;
    254           1.1       cgd 
    255           1.1       cgd 	case RTM_GET:
    256           1.1       cgd 	case RTM_CHANGE:
    257           1.1       cgd 	case RTM_LOCK:
    258          1.16       cgd 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
    259          1.16       cgd 			senderr(EAFNOSUPPORT);
    260          1.17  christos 		} else if ((rt = (struct rtentry *)
    261          1.21  christos 		    rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
    262          1.16       cgd 			rt->rt_refcnt++;
    263          1.16       cgd 		else
    264           1.1       cgd 			senderr(ESRCH);
    265           1.1       cgd 		switch(rtm->rtm_type) {
    266           1.1       cgd 
    267           1.1       cgd 		case RTM_GET:
    268          1.16       cgd 		report:
    269          1.10   mycroft 			dst = rt_key(rt);
    270          1.10   mycroft 			gate = rt->rt_gateway;
    271          1.10   mycroft 			netmask = rt_mask(rt);
    272          1.10   mycroft 			genmask = rt->rt_genmask;
    273           1.1       cgd 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
    274          1.17  christos 				if ((ifp = rt->rt_ifp) != NULL) {
    275          1.13   mycroft 					ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    276           1.1       cgd 					ifaaddr = rt->rt_ifa->ifa_addr;
    277          1.16       cgd 					if (ifp->if_flags & IFF_POINTOPOINT)
    278          1.16       cgd 						brdaddr = rt->rt_ifa->ifa_dstaddr;
    279          1.16       cgd 					else
    280          1.16       cgd 						brdaddr = 0;
    281          1.10   mycroft 					rtm->rtm_index = ifp->if_index;
    282           1.1       cgd 				} else {
    283          1.10   mycroft 					ifpaddr = 0;
    284          1.10   mycroft 					ifaaddr = 0;
    285          1.10   mycroft 			    }
    286           1.1       cgd 			}
    287      1.28.2.1    chopps 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
    288      1.28.2.1    chopps 			    (struct walkarg *)0, &len);
    289           1.1       cgd 			if (len > rtm->rtm_msglen) {
    290           1.1       cgd 				struct rt_msghdr *new_rtm;
    291           1.1       cgd 				R_Malloc(new_rtm, struct rt_msghdr *, len);
    292           1.1       cgd 				if (new_rtm == 0)
    293           1.1       cgd 					senderr(ENOBUFS);
    294           1.1       cgd 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
    295           1.1       cgd 				Free(rtm); rtm = new_rtm;
    296           1.1       cgd 			}
    297          1.22   mycroft 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
    298      1.28.2.1    chopps 			    (struct walkarg *)0, 0);
    299           1.1       cgd 			rtm->rtm_flags = rt->rt_flags;
    300           1.1       cgd 			rtm->rtm_rmx = rt->rt_rmx;
    301          1.10   mycroft 			rtm->rtm_addrs = info.rti_addrs;
    302           1.1       cgd 			break;
    303           1.1       cgd 
    304           1.1       cgd 		case RTM_CHANGE:
    305          1.10   mycroft 			if (gate && rt_setgate(rt, rt_key(rt), gate))
    306           1.1       cgd 				senderr(EDQUOT);
    307           1.1       cgd 			/* new gateway could require new ifaddr, ifp;
    308           1.1       cgd 			   flags may also be different; ifp may be specified
    309           1.1       cgd 			   by ll sockaddr when protocol address is ambiguous */
    310           1.1       cgd 			if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
    311          1.25  christos 			    (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
    312           1.1       cgd 				ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
    313          1.21  christos 				    ifp);
    314           1.1       cgd 			else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
    315          1.21  christos 			    (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
    316          1.21  christos 			    rt_key(rt), gate))))
    317           1.1       cgd 				ifp = ifa->ifa_ifp;
    318           1.1       cgd 			if (ifa) {
    319           1.1       cgd 				register struct ifaddr *oifa = rt->rt_ifa;
    320           1.1       cgd 				if (oifa != ifa) {
    321           1.1       cgd 				    if (oifa && oifa->ifa_rtrequest)
    322           1.1       cgd 					oifa->ifa_rtrequest(RTM_DELETE,
    323          1.21  christos 					rt, gate);
    324          1.10   mycroft 				    IFAFREE(rt->rt_ifa);
    325           1.1       cgd 				    rt->rt_ifa = ifa;
    326          1.10   mycroft 				    ifa->ifa_refcnt++;
    327           1.1       cgd 				    rt->rt_ifp = ifp;
    328           1.1       cgd 				}
    329           1.1       cgd 			}
    330           1.1       cgd 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
    331          1.21  christos 			    &rt->rt_rmx);
    332           1.1       cgd 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    333          1.21  christos 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate);
    334           1.1       cgd 			if (genmask)
    335           1.1       cgd 				rt->rt_genmask = genmask;
    336           1.1       cgd 			/*
    337           1.1       cgd 			 * Fall into
    338           1.1       cgd 			 */
    339           1.1       cgd 		case RTM_LOCK:
    340          1.10   mycroft 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    341           1.1       cgd 			rt->rt_rmx.rmx_locks |=
    342          1.21  christos 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    343           1.1       cgd 			break;
    344           1.1       cgd 		}
    345          1.10   mycroft 		break;
    346           1.1       cgd 
    347           1.1       cgd 	default:
    348           1.1       cgd 		senderr(EOPNOTSUPP);
    349           1.1       cgd 	}
    350           1.1       cgd 
    351           1.1       cgd flush:
    352           1.1       cgd 	if (rtm) {
    353           1.1       cgd 		if (error)
    354           1.1       cgd 			rtm->rtm_errno = error;
    355           1.1       cgd 		else
    356           1.1       cgd 			rtm->rtm_flags |= RTF_DONE;
    357           1.1       cgd 	}
    358           1.1       cgd 	if (rt)
    359           1.1       cgd 		rtfree(rt);
    360           1.1       cgd     {
    361           1.1       cgd 	register struct rawcb *rp = 0;
    362           1.1       cgd 	/*
    363           1.1       cgd 	 * Check to see if we don't want our own messages.
    364           1.1       cgd 	 */
    365           1.1       cgd 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    366           1.1       cgd 		if (route_cb.any_count <= 1) {
    367           1.1       cgd 			if (rtm)
    368           1.1       cgd 				Free(rtm);
    369           1.1       cgd 			m_freem(m);
    370           1.1       cgd 			return (error);
    371           1.1       cgd 		}
    372           1.1       cgd 		/* There is another listener, so construct message */
    373           1.1       cgd 		rp = sotorawcb(so);
    374           1.1       cgd 	}
    375           1.1       cgd 	if (rtm) {
    376           1.1       cgd 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
    377           1.1       cgd 		Free(rtm);
    378           1.1       cgd 	}
    379           1.1       cgd 	if (rp)
    380           1.1       cgd 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    381           1.1       cgd 	if (dst)
    382           1.1       cgd 		route_proto.sp_protocol = dst->sa_family;
    383           1.1       cgd 	raw_input(m, &route_proto, &route_src, &route_dst);
    384           1.1       cgd 	if (rp)
    385           1.1       cgd 		rp->rcb_proto.sp_family = PF_ROUTE;
    386           1.1       cgd     }
    387           1.1       cgd 	return (error);
    388           1.1       cgd }
    389           1.1       cgd 
    390           1.9   mycroft void
    391           1.1       cgd rt_setmetrics(which, in, out)
    392           1.1       cgd 	u_long which;
    393           1.1       cgd 	register struct rt_metrics *in, *out;
    394           1.1       cgd {
    395           1.1       cgd #define metric(f, e) if (which & (f)) out->e = in->e;
    396           1.1       cgd 	metric(RTV_RPIPE, rmx_recvpipe);
    397           1.1       cgd 	metric(RTV_SPIPE, rmx_sendpipe);
    398           1.1       cgd 	metric(RTV_SSTHRESH, rmx_ssthresh);
    399           1.1       cgd 	metric(RTV_RTT, rmx_rtt);
    400           1.1       cgd 	metric(RTV_RTTVAR, rmx_rttvar);
    401           1.1       cgd 	metric(RTV_HOPCOUNT, rmx_hopcount);
    402           1.1       cgd 	metric(RTV_MTU, rmx_mtu);
    403           1.1       cgd 	metric(RTV_EXPIRE, rmx_expire);
    404           1.1       cgd #undef metric
    405           1.1       cgd }
    406           1.1       cgd 
    407          1.10   mycroft #define ROUNDUP(a) \
    408          1.18       cgd 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    409          1.10   mycroft #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    410          1.10   mycroft 
    411          1.10   mycroft static void
    412          1.10   mycroft rt_xaddrs(cp, cplim, rtinfo)
    413          1.10   mycroft 	register caddr_t cp, cplim;
    414          1.10   mycroft 	register struct rt_addrinfo *rtinfo;
    415          1.10   mycroft {
    416          1.10   mycroft 	register struct sockaddr *sa;
    417          1.10   mycroft 	register int i;
    418          1.10   mycroft 
    419          1.10   mycroft 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
    420          1.10   mycroft 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
    421          1.10   mycroft 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    422          1.10   mycroft 			continue;
    423          1.10   mycroft 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
    424          1.10   mycroft 		ADVANCE(cp, sa);
    425          1.10   mycroft 	}
    426           1.1       cgd }
    427           1.1       cgd 
    428          1.10   mycroft static struct mbuf *
    429          1.10   mycroft rt_msg1(type, rtinfo)
    430          1.10   mycroft 	int type;
    431          1.10   mycroft 	register struct rt_addrinfo *rtinfo;
    432           1.1       cgd {
    433           1.1       cgd 	register struct rt_msghdr *rtm;
    434           1.1       cgd 	register struct mbuf *m;
    435          1.10   mycroft 	register int i;
    436          1.10   mycroft 	register struct sockaddr *sa;
    437          1.10   mycroft 	int len, dlen;
    438           1.1       cgd 
    439           1.1       cgd 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    440           1.1       cgd 	if (m == 0)
    441          1.10   mycroft 		return (m);
    442          1.10   mycroft 	switch (type) {
    443          1.10   mycroft 
    444          1.10   mycroft 	case RTM_DELADDR:
    445          1.10   mycroft 	case RTM_NEWADDR:
    446          1.10   mycroft 		len = sizeof(struct ifa_msghdr);
    447          1.10   mycroft 		break;
    448          1.10   mycroft 
    449          1.10   mycroft 	case RTM_IFINFO:
    450          1.10   mycroft 		len = sizeof(struct if_msghdr);
    451          1.10   mycroft 		break;
    452          1.10   mycroft 
    453          1.10   mycroft 	default:
    454          1.10   mycroft 		len = sizeof(struct rt_msghdr);
    455          1.10   mycroft 	}
    456          1.10   mycroft 	if (len > MHLEN)
    457          1.10   mycroft 		panic("rt_msg1");
    458          1.10   mycroft 	m->m_pkthdr.len = m->m_len = len;
    459           1.1       cgd 	m->m_pkthdr.rcvif = 0;
    460           1.1       cgd 	rtm = mtod(m, struct rt_msghdr *);
    461          1.21  christos 	bzero(rtm, len);
    462          1.10   mycroft 	for (i = 0; i < RTAX_MAX; i++) {
    463          1.10   mycroft 		if ((sa = rtinfo->rti_info[i]) == NULL)
    464          1.10   mycroft 			continue;
    465          1.10   mycroft 		rtinfo->rti_addrs |= (1 << i);
    466          1.10   mycroft 		dlen = ROUNDUP(sa->sa_len);
    467          1.10   mycroft 		m_copyback(m, len, dlen, (caddr_t)sa);
    468          1.10   mycroft 		len += dlen;
    469          1.10   mycroft 	}
    470          1.10   mycroft 	if (m->m_pkthdr.len != len) {
    471          1.10   mycroft 		m_freem(m);
    472          1.10   mycroft 		return (NULL);
    473          1.10   mycroft 	}
    474           1.1       cgd 	rtm->rtm_msglen = len;
    475           1.1       cgd 	rtm->rtm_version = RTM_VERSION;
    476           1.1       cgd 	rtm->rtm_type = type;
    477          1.10   mycroft 	return (m);
    478          1.10   mycroft }
    479          1.10   mycroft 
    480      1.28.2.1    chopps /*
    481      1.28.2.1    chopps  * rt_msg2
    482      1.28.2.1    chopps  *
    483      1.28.2.1    chopps  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    484      1.28.2.1    chopps  *		returns the length of the message in 'lenp'.
    485      1.28.2.1    chopps  *
    486      1.28.2.1    chopps  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    487      1.28.2.1    chopps  *	the message
    488      1.28.2.1    chopps  * otherwise walkarg's w_needed is updated and if the user buffer is
    489      1.28.2.1    chopps  *	specified and w_needed indicates space exists the information is copied
    490      1.28.2.1    chopps  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    491      1.28.2.1    chopps  *	if the allocation fails ENOBUFS is returned.
    492      1.28.2.1    chopps  */
    493          1.10   mycroft static int
    494      1.28.2.1    chopps rt_msg2(type, rtinfo, cp, w, lenp)
    495          1.10   mycroft 	int type;
    496          1.10   mycroft 	register struct rt_addrinfo *rtinfo;
    497          1.10   mycroft 	caddr_t cp;
    498          1.10   mycroft 	struct walkarg *w;
    499      1.28.2.1    chopps 	int *lenp;
    500          1.10   mycroft {
    501          1.10   mycroft 	register int i;
    502          1.10   mycroft 	int len, dlen, second_time = 0;
    503          1.10   mycroft 	caddr_t cp0;
    504          1.10   mycroft 
    505          1.10   mycroft 	rtinfo->rti_addrs = 0;
    506          1.10   mycroft again:
    507          1.10   mycroft 	switch (type) {
    508          1.10   mycroft 
    509          1.10   mycroft 	case RTM_DELADDR:
    510          1.10   mycroft 	case RTM_NEWADDR:
    511          1.10   mycroft 		len = sizeof(struct ifa_msghdr);
    512          1.10   mycroft 		break;
    513          1.10   mycroft 
    514          1.10   mycroft 	case RTM_IFINFO:
    515          1.10   mycroft 		len = sizeof(struct if_msghdr);
    516          1.10   mycroft 		break;
    517          1.10   mycroft 
    518          1.10   mycroft 	default:
    519          1.10   mycroft 		len = sizeof(struct rt_msghdr);
    520          1.10   mycroft 	}
    521          1.17  christos 	if ((cp0 = cp) != NULL)
    522          1.10   mycroft 		cp += len;
    523          1.10   mycroft 	for (i = 0; i < RTAX_MAX; i++) {
    524          1.10   mycroft 		register struct sockaddr *sa;
    525          1.10   mycroft 
    526          1.10   mycroft 		if ((sa = rtinfo->rti_info[i]) == 0)
    527          1.10   mycroft 			continue;
    528          1.10   mycroft 		rtinfo->rti_addrs |= (1 << i);
    529          1.10   mycroft 		dlen = ROUNDUP(sa->sa_len);
    530          1.10   mycroft 		if (cp) {
    531          1.21  christos 			bcopy(sa, cp, (unsigned)dlen);
    532          1.10   mycroft 			cp += dlen;
    533          1.10   mycroft 		}
    534           1.1       cgd 		len += dlen;
    535           1.1       cgd 	}
    536          1.10   mycroft 	if (cp == 0 && w != NULL && !second_time) {
    537          1.10   mycroft 		register struct walkarg *rw = w;
    538          1.10   mycroft 
    539          1.10   mycroft 		rw->w_needed += len;
    540          1.10   mycroft 		if (rw->w_needed <= 0 && rw->w_where) {
    541          1.10   mycroft 			if (rw->w_tmemsize < len) {
    542          1.10   mycroft 				if (rw->w_tmem)
    543          1.10   mycroft 					free(rw->w_tmem, M_RTABLE);
    544          1.17  christos 				rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
    545          1.21  christos 				    M_NOWAIT);
    546          1.17  christos 				if (rw->w_tmem)
    547          1.10   mycroft 					rw->w_tmemsize = len;
    548          1.10   mycroft 			}
    549          1.10   mycroft 			if (rw->w_tmem) {
    550          1.10   mycroft 				cp = rw->w_tmem;
    551          1.10   mycroft 				second_time = 1;
    552          1.10   mycroft 				goto again;
    553      1.28.2.1    chopps 			} else {
    554      1.28.2.1    chopps 				rw->w_tmemneeded = len;
    555      1.28.2.1    chopps 				return (ENOBUFS);
    556      1.28.2.1    chopps 			}
    557          1.10   mycroft 		}
    558           1.1       cgd 	}
    559          1.10   mycroft 	if (cp) {
    560          1.10   mycroft 		register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
    561          1.10   mycroft 
    562          1.10   mycroft 		rtm->rtm_version = RTM_VERSION;
    563          1.10   mycroft 		rtm->rtm_type = type;
    564          1.10   mycroft 		rtm->rtm_msglen = len;
    565           1.1       cgd 	}
    566      1.28.2.1    chopps 	if (lenp)
    567      1.28.2.1    chopps 		*lenp = len;
    568      1.28.2.1    chopps 	return (0);
    569          1.10   mycroft }
    570          1.10   mycroft 
    571          1.10   mycroft /*
    572          1.10   mycroft  * This routine is called to generate a message from the routing
    573          1.10   mycroft  * socket indicating that a redirect has occured, a routing lookup
    574          1.10   mycroft  * has failed, or that a protocol has detected timeouts to a particular
    575          1.10   mycroft  * destination.
    576          1.10   mycroft  */
    577          1.10   mycroft void
    578          1.10   mycroft rt_missmsg(type, rtinfo, flags, error)
    579          1.10   mycroft 	int type, flags, error;
    580          1.10   mycroft 	register struct rt_addrinfo *rtinfo;
    581          1.10   mycroft {
    582          1.10   mycroft 	register struct rt_msghdr *rtm;
    583          1.10   mycroft 	register struct mbuf *m;
    584          1.10   mycroft 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    585          1.10   mycroft 
    586          1.10   mycroft 	if (route_cb.any_count == 0)
    587          1.10   mycroft 		return;
    588          1.10   mycroft 	m = rt_msg1(type, rtinfo);
    589          1.10   mycroft 	if (m == 0)
    590           1.1       cgd 		return;
    591          1.10   mycroft 	rtm = mtod(m, struct rt_msghdr *);
    592          1.10   mycroft 	rtm->rtm_flags = RTF_DONE | flags;
    593           1.1       cgd 	rtm->rtm_errno = error;
    594          1.10   mycroft 	rtm->rtm_addrs = rtinfo->rti_addrs;
    595          1.10   mycroft 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
    596          1.10   mycroft 	raw_input(m, &route_proto, &route_src, &route_dst);
    597          1.10   mycroft }
    598          1.10   mycroft 
    599          1.10   mycroft /*
    600          1.10   mycroft  * This routine is called to generate a message from the routing
    601          1.10   mycroft  * socket indicating that the status of a network interface has changed.
    602          1.10   mycroft  */
    603          1.10   mycroft void
    604          1.10   mycroft rt_ifmsg(ifp)
    605          1.10   mycroft 	register struct ifnet *ifp;
    606          1.10   mycroft {
    607          1.10   mycroft 	register struct if_msghdr *ifm;
    608          1.10   mycroft 	struct mbuf *m;
    609          1.10   mycroft 	struct rt_addrinfo info;
    610          1.10   mycroft 
    611          1.10   mycroft 	if (route_cb.any_count == 0)
    612          1.10   mycroft 		return;
    613          1.21  christos 	bzero(&info, sizeof(info));
    614          1.10   mycroft 	m = rt_msg1(RTM_IFINFO, &info);
    615          1.10   mycroft 	if (m == 0)
    616          1.10   mycroft 		return;
    617          1.10   mycroft 	ifm = mtod(m, struct if_msghdr *);
    618          1.10   mycroft 	ifm->ifm_index = ifp->if_index;
    619          1.10   mycroft 	ifm->ifm_flags = ifp->if_flags;
    620          1.10   mycroft 	ifm->ifm_data = ifp->if_data;
    621          1.10   mycroft 	ifm->ifm_addrs = 0;
    622          1.10   mycroft 	route_proto.sp_protocol = 0;
    623           1.1       cgd 	raw_input(m, &route_proto, &route_src, &route_dst);
    624           1.1       cgd }
    625           1.1       cgd 
    626           1.1       cgd /*
    627          1.10   mycroft  * This is called to generate messages from the routing socket
    628          1.10   mycroft  * indicating a network interface has had addresses associated with it.
    629          1.10   mycroft  * if we ever reverse the logic and replace messages TO the routing
    630          1.10   mycroft  * socket indicate a request to configure interfaces, then it will
    631          1.10   mycroft  * be unnecessary as the routing socket will automatically generate
    632          1.10   mycroft  * copies of it.
    633          1.10   mycroft  */
    634          1.10   mycroft void
    635          1.10   mycroft rt_newaddrmsg(cmd, ifa, error, rt)
    636          1.10   mycroft 	int cmd, error;
    637          1.10   mycroft 	register struct ifaddr *ifa;
    638          1.10   mycroft 	register struct rtentry *rt;
    639          1.10   mycroft {
    640          1.10   mycroft 	struct rt_addrinfo info;
    641          1.17  christos 	struct sockaddr *sa = NULL;
    642          1.10   mycroft 	int pass;
    643          1.17  christos 	struct mbuf *m = NULL;
    644          1.10   mycroft 	struct ifnet *ifp = ifa->ifa_ifp;
    645          1.10   mycroft 
    646          1.10   mycroft 	if (route_cb.any_count == 0)
    647          1.10   mycroft 		return;
    648          1.10   mycroft 	for (pass = 1; pass < 3; pass++) {
    649          1.21  christos 		bzero(&info, sizeof(info));
    650          1.10   mycroft 		if ((cmd == RTM_ADD && pass == 1) ||
    651          1.10   mycroft 		    (cmd == RTM_DELETE && pass == 2)) {
    652          1.10   mycroft 			register struct ifa_msghdr *ifam;
    653          1.10   mycroft 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
    654          1.10   mycroft 
    655          1.10   mycroft 			ifaaddr = sa = ifa->ifa_addr;
    656          1.13   mycroft 			ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    657          1.10   mycroft 			netmask = ifa->ifa_netmask;
    658          1.10   mycroft 			brdaddr = ifa->ifa_dstaddr;
    659          1.10   mycroft 			if ((m = rt_msg1(ncmd, &info)) == NULL)
    660          1.10   mycroft 				continue;
    661          1.10   mycroft 			ifam = mtod(m, struct ifa_msghdr *);
    662          1.10   mycroft 			ifam->ifam_index = ifp->if_index;
    663          1.10   mycroft 			ifam->ifam_metric = ifa->ifa_metric;
    664          1.10   mycroft 			ifam->ifam_flags = ifa->ifa_flags;
    665          1.10   mycroft 			ifam->ifam_addrs = info.rti_addrs;
    666          1.10   mycroft 		}
    667          1.10   mycroft 		if ((cmd == RTM_ADD && pass == 2) ||
    668          1.10   mycroft 		    (cmd == RTM_DELETE && pass == 1)) {
    669          1.10   mycroft 			register struct rt_msghdr *rtm;
    670          1.10   mycroft 
    671          1.10   mycroft 			if (rt == 0)
    672          1.10   mycroft 				continue;
    673          1.10   mycroft 			netmask = rt_mask(rt);
    674          1.10   mycroft 			dst = sa = rt_key(rt);
    675          1.10   mycroft 			gate = rt->rt_gateway;
    676          1.10   mycroft 			if ((m = rt_msg1(cmd, &info)) == NULL)
    677          1.10   mycroft 				continue;
    678          1.10   mycroft 			rtm = mtod(m, struct rt_msghdr *);
    679          1.10   mycroft 			rtm->rtm_index = ifp->if_index;
    680          1.10   mycroft 			rtm->rtm_flags |= rt->rt_flags;
    681          1.10   mycroft 			rtm->rtm_errno = error;
    682          1.10   mycroft 			rtm->rtm_addrs = info.rti_addrs;
    683          1.10   mycroft 		}
    684          1.10   mycroft 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
    685          1.10   mycroft 		raw_input(m, &route_proto, &route_src, &route_dst);
    686          1.10   mycroft 	}
    687          1.10   mycroft }
    688          1.10   mycroft 
    689          1.10   mycroft /*
    690          1.10   mycroft  * This is used in dumping the kernel table via sysctl().
    691           1.1       cgd  */
    692          1.10   mycroft int
    693          1.17  christos sysctl_dumpentry(rn, v)
    694           1.1       cgd 	struct radix_node *rn;
    695          1.17  christos 	register void *v;
    696           1.1       cgd {
    697          1.17  christos 	register struct walkarg *w = v;
    698          1.10   mycroft 	register struct rtentry *rt = (struct rtentry *)rn;
    699          1.10   mycroft 	int error = 0, size;
    700          1.10   mycroft 	struct rt_addrinfo info;
    701           1.1       cgd 
    702          1.10   mycroft 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    703          1.10   mycroft 		return 0;
    704          1.21  christos 	bzero(&info, sizeof(info));
    705          1.10   mycroft 	dst = rt_key(rt);
    706          1.10   mycroft 	gate = rt->rt_gateway;
    707          1.10   mycroft 	netmask = rt_mask(rt);
    708          1.10   mycroft 	genmask = rt->rt_genmask;
    709          1.16       cgd 	if (rt->rt_ifp) {
    710          1.16       cgd 		ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
    711          1.16       cgd 		ifaaddr = rt->rt_ifa->ifa_addr;
    712          1.16       cgd 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    713          1.16       cgd 			brdaddr = rt->rt_ifa->ifa_dstaddr;
    714          1.16       cgd 	}
    715      1.28.2.1    chopps 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
    716      1.28.2.1    chopps 		return (error);
    717      1.28.2.1    chopps 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    718          1.10   mycroft 		register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
    719          1.10   mycroft 
    720          1.10   mycroft 		rtm->rtm_flags = rt->rt_flags;
    721          1.10   mycroft 		rtm->rtm_use = rt->rt_use;
    722          1.10   mycroft 		rtm->rtm_rmx = rt->rt_rmx;
    723          1.10   mycroft 		rtm->rtm_index = rt->rt_ifp->if_index;
    724          1.10   mycroft 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    725          1.10   mycroft 		rtm->rtm_addrs = info.rti_addrs;
    726          1.21  christos 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    727          1.10   mycroft 			w->w_where = NULL;
    728          1.10   mycroft 		else
    729          1.10   mycroft 			w->w_where += size;
    730          1.10   mycroft 	}
    731          1.10   mycroft 	return (error);
    732          1.10   mycroft }
    733           1.1       cgd 
    734          1.10   mycroft int
    735          1.10   mycroft sysctl_iflist(af, w)
    736          1.10   mycroft 	int	af;
    737          1.10   mycroft 	register struct	walkarg *w;
    738          1.10   mycroft {
    739          1.10   mycroft 	register struct ifnet *ifp;
    740          1.10   mycroft 	register struct ifaddr *ifa;
    741          1.10   mycroft 	struct	rt_addrinfo info;
    742          1.10   mycroft 	int	len, error = 0;
    743          1.10   mycroft 
    744          1.21  christos 	bzero(&info, sizeof(info));
    745          1.13   mycroft 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
    746          1.10   mycroft 		if (w->w_arg && w->w_arg != ifp->if_index)
    747          1.10   mycroft 			continue;
    748          1.13   mycroft 		ifa = ifp->if_addrlist.tqh_first;
    749          1.10   mycroft 		ifpaddr = ifa->ifa_addr;
    750      1.28.2.1    chopps 		if ((error = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len)))
    751      1.28.2.1    chopps 			return (error);
    752          1.10   mycroft 		ifpaddr = 0;
    753      1.28.2.1    chopps 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    754          1.10   mycroft 			register struct if_msghdr *ifm;
    755          1.10   mycroft 
    756          1.10   mycroft 			ifm = (struct if_msghdr *)w->w_tmem;
    757          1.10   mycroft 			ifm->ifm_index = ifp->if_index;
    758          1.10   mycroft 			ifm->ifm_flags = ifp->if_flags;
    759          1.10   mycroft 			ifm->ifm_data = ifp->if_data;
    760          1.10   mycroft 			ifm->ifm_addrs = info.rti_addrs;
    761          1.21  christos 			error = copyout(ifm, w->w_where, len);
    762          1.17  christos 			if (error)
    763          1.10   mycroft 				return (error);
    764          1.10   mycroft 			w->w_where += len;
    765          1.10   mycroft 		}
    766          1.17  christos 		while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
    767          1.10   mycroft 			if (af && af != ifa->ifa_addr->sa_family)
    768          1.10   mycroft 				continue;
    769          1.10   mycroft 			ifaaddr = ifa->ifa_addr;
    770          1.10   mycroft 			netmask = ifa->ifa_netmask;
    771          1.10   mycroft 			brdaddr = ifa->ifa_dstaddr;
    772      1.28.2.1    chopps 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
    773      1.28.2.1    chopps 				return (error);
    774      1.28.2.1    chopps 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    775          1.10   mycroft 				register struct ifa_msghdr *ifam;
    776          1.10   mycroft 
    777          1.10   mycroft 				ifam = (struct ifa_msghdr *)w->w_tmem;
    778          1.10   mycroft 				ifam->ifam_index = ifa->ifa_ifp->if_index;
    779          1.10   mycroft 				ifam->ifam_flags = ifa->ifa_flags;
    780          1.10   mycroft 				ifam->ifam_metric = ifa->ifa_metric;
    781          1.10   mycroft 				ifam->ifam_addrs = info.rti_addrs;
    782          1.17  christos 				error = copyout(w->w_tmem, w->w_where, len);
    783          1.17  christos 				if (error)
    784          1.10   mycroft 					return (error);
    785          1.10   mycroft 				w->w_where += len;
    786          1.10   mycroft 			}
    787          1.10   mycroft 		}
    788          1.10   mycroft 		ifaaddr = netmask = brdaddr = 0;
    789          1.10   mycroft 	}
    790           1.1       cgd 	return (0);
    791           1.1       cgd }
    792           1.1       cgd 
    793          1.10   mycroft int
    794          1.10   mycroft sysctl_rtable(name, namelen, where, given, new, newlen)
    795          1.10   mycroft 	int	*name;
    796          1.17  christos 	u_int	namelen;
    797          1.17  christos 	void 	*where;
    798          1.10   mycroft 	size_t	*given;
    799          1.17  christos 	void	*new;
    800          1.10   mycroft 	size_t	newlen;
    801           1.1       cgd {
    802           1.1       cgd 	register struct radix_node_head *rnh;
    803          1.10   mycroft 	int	i, s, error = EINVAL;
    804          1.10   mycroft 	u_char  af;
    805           1.1       cgd 	struct	walkarg w;
    806           1.1       cgd 
    807          1.10   mycroft 	if (new)
    808          1.10   mycroft 		return (EPERM);
    809          1.10   mycroft 	if (namelen != 3)
    810           1.1       cgd 		return (EINVAL);
    811          1.10   mycroft 	af = name[0];
    812      1.28.2.1    chopps 	w.w_tmemneeded = 0;
    813      1.28.2.1    chopps 	w.w_tmemsize = 0;
    814      1.28.2.1    chopps 	w.w_tmem = NULL;
    815      1.28.2.1    chopps again:
    816      1.28.2.1    chopps 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
    817      1.28.2.1    chopps 	if (w.w_tmemneeded) {
    818      1.28.2.1    chopps 		w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
    819      1.28.2.1    chopps 		w.w_tmemsize = w.w_tmemneeded;
    820      1.28.2.1    chopps 		w.w_tmemneeded = 0;
    821      1.28.2.1    chopps 	}
    822          1.10   mycroft 	w.w_op = name[1];
    823          1.10   mycroft 	w.w_arg = name[2];
    824      1.28.2.1    chopps 	w.w_given = *given;
    825      1.28.2.1    chopps 	w.w_needed = 0 - w.w_given;
    826      1.28.2.1    chopps 	w.w_where = where;
    827           1.1       cgd 
    828          1.14   mycroft 	s = splsoftnet();
    829          1.10   mycroft 	switch (w.w_op) {
    830          1.10   mycroft 
    831          1.10   mycroft 	case NET_RT_DUMP:
    832          1.10   mycroft 	case NET_RT_FLAGS:
    833          1.10   mycroft 		for (i = 1; i <= AF_MAX; i++)
    834          1.10   mycroft 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
    835          1.17  christos 			    (error = (*rnh->rnh_walktree)(rnh,
    836          1.21  christos 			    sysctl_dumpentry, &w)))
    837          1.10   mycroft 				break;
    838          1.10   mycroft 		break;
    839          1.10   mycroft 
    840          1.10   mycroft 	case NET_RT_IFLIST:
    841          1.10   mycroft 		error = sysctl_iflist(af, &w);
    842           1.1       cgd 	}
    843          1.10   mycroft 	splx(s);
    844      1.28.2.1    chopps 
    845      1.28.2.1    chopps 	/* check to see if we couldn't allocate memory with NOWAIT */
    846      1.28.2.1    chopps 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
    847      1.28.2.1    chopps 		goto again;
    848      1.28.2.1    chopps 
    849          1.10   mycroft 	if (w.w_tmem)
    850          1.10   mycroft 		free(w.w_tmem, M_RTABLE);
    851           1.1       cgd 	w.w_needed += w.w_given;
    852          1.10   mycroft 	if (where) {
    853          1.17  christos 		*given = w.w_where - (caddr_t) where;
    854          1.10   mycroft 		if (*given < w.w_needed)
    855          1.10   mycroft 			return (ENOMEM);
    856          1.10   mycroft 	} else {
    857          1.10   mycroft 		*given = (11 * w.w_needed) / 10;
    858          1.10   mycroft 	}
    859           1.1       cgd 	return (error);
    860           1.1       cgd }
    861           1.1       cgd 
    862           1.1       cgd /*
    863           1.1       cgd  * Definitions of protocols supported in the ROUTE domain.
    864           1.1       cgd  */
    865           1.1       cgd 
    866           1.1       cgd extern	struct domain routedomain;		/* or at least forward */
    867           1.1       cgd 
    868           1.1       cgd struct protosw routesw[] = {
    869           1.1       cgd { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
    870           1.1       cgd   raw_input,	route_output,	raw_ctlinput,	0,
    871           1.1       cgd   route_usrreq,
    872           1.1       cgd   raw_init,	0,		0,		0,
    873          1.10   mycroft   sysctl_rtable,
    874           1.1       cgd }
    875           1.1       cgd };
    876           1.1       cgd 
    877           1.1       cgd struct domain routedomain =
    878          1.10   mycroft     { PF_ROUTE, "route", route_init, 0, 0,
    879           1.1       cgd       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
    880