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