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