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