Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.46
      1  1.46    itojun /*	$NetBSD: rtsock.c,v 1.46 2001/06/04 01:30:11 itojun Exp $	*/
      2  1.30    itojun 
      3  1.30    itojun /*
      4  1.30    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  1.30    itojun  * All rights reserved.
      6  1.30    itojun  *
      7  1.30    itojun  * Redistribution and use in source and binary forms, with or without
      8  1.30    itojun  * modification, are permitted provided that the following conditions
      9  1.30    itojun  * are met:
     10  1.30    itojun  * 1. Redistributions of source code must retain the above copyright
     11  1.30    itojun  *    notice, this list of conditions and the following disclaimer.
     12  1.30    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.30    itojun  *    notice, this list of conditions and the following disclaimer in the
     14  1.30    itojun  *    documentation and/or other materials provided with the distribution.
     15  1.30    itojun  * 3. Neither the name of the project nor the names of its contributors
     16  1.30    itojun  *    may be used to endorse or promote products derived from this software
     17  1.30    itojun  *    without specific prior written permission.
     18  1.30    itojun  *
     19  1.30    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.30    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.30    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.30    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.30    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.30    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.30    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.30    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.30    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.30    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.30    itojun  * SUCH DAMAGE.
     30  1.30    itojun  */
     31  1.11       cgd 
     32   1.1       cgd /*
     33  1.10   mycroft  * Copyright (c) 1988, 1991, 1993
     34  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
     35   1.1       cgd  *
     36   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     37   1.1       cgd  * modification, are permitted provided that the following conditions
     38   1.1       cgd  * are met:
     39   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     40   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     41   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     43   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     44   1.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.31   thorpej 
     67  1.31   thorpej #include "opt_inet.h"
     68   1.1       cgd 
     69   1.5   mycroft #include <sys/param.h>
     70   1.5   mycroft #include <sys/systm.h>
     71  1.10   mycroft #include <sys/proc.h>
     72   1.5   mycroft #include <sys/mbuf.h>
     73   1.5   mycroft #include <sys/socket.h>
     74   1.5   mycroft #include <sys/socketvar.h>
     75   1.5   mycroft #include <sys/domain.h>
     76   1.5   mycroft #include <sys/protosw.h>
     77   1.5   mycroft 
     78  1.41       mrg #include <uvm/uvm_extern.h>
     79  1.41       mrg 
     80  1.17  christos #include <sys/sysctl.h>
     81  1.17  christos 
     82   1.5   mycroft #include <net/if.h>
     83   1.5   mycroft #include <net/route.h>
     84   1.5   mycroft #include <net/raw_cb.h>
     85   1.1       cgd 
     86  1.17  christos #include <machine/stdarg.h>
     87  1.17  christos 
     88  1.10   mycroft struct	sockaddr route_dst = { 2, PF_ROUTE, };
     89  1.10   mycroft struct	sockaddr route_src = { 2, PF_ROUTE, };
     90  1.10   mycroft struct	sockproto route_proto = { PF_ROUTE, };
     91  1.10   mycroft 
     92  1.10   mycroft struct walkarg {
     93  1.29    chopps 	int	w_op;
     94  1.29    chopps 	int	w_arg;
     95  1.29    chopps 	int	w_given;
     96  1.29    chopps 	int	w_needed;
     97  1.29    chopps 	caddr_t	w_where;
     98  1.29    chopps 	int	w_tmemsize;
     99  1.29    chopps 	int	w_tmemneeded;
    100  1.29    chopps 	caddr_t	w_tmem;
    101  1.10   mycroft };
    102   1.1       cgd 
    103  1.32    bouyer static struct mbuf *rt_msg1 __P((int, struct rt_addrinfo *, caddr_t, int));
    104  1.29    chopps static int rt_msg2 __P((int, struct rt_addrinfo *, caddr_t, struct walkarg *,
    105  1.29    chopps     int *));
    106  1.42       erh static int rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
    107  1.40    simonb static int sysctl_dumpentry __P((struct radix_node *, void *));
    108  1.40    simonb static int sysctl_iflist __P((int, struct walkarg *, int));
    109  1.40    simonb static int sysctl_rtable __P((int *, u_int, void *, size_t *, void *, size_t));
    110  1.27  christos static __inline void rt_adjustcount __P((int, int));
    111  1.10   mycroft 
    112  1.10   mycroft /* Sleazy use of local variables throughout file, warning!!!! */
    113  1.10   mycroft #define dst	info.rti_info[RTAX_DST]
    114  1.10   mycroft #define gate	info.rti_info[RTAX_GATEWAY]
    115  1.10   mycroft #define netmask	info.rti_info[RTAX_NETMASK]
    116  1.10   mycroft #define genmask	info.rti_info[RTAX_GENMASK]
    117  1.10   mycroft #define ifpaddr	info.rti_info[RTAX_IFP]
    118  1.10   mycroft #define ifaaddr	info.rti_info[RTAX_IFA]
    119  1.10   mycroft #define brdaddr	info.rti_info[RTAX_BRD]
    120   1.1       cgd 
    121  1.27  christos static __inline void
    122  1.27  christos rt_adjustcount(af, cnt)
    123  1.27  christos 	int af, cnt;
    124  1.27  christos {
    125  1.28  christos 	route_cb.any_count += cnt;
    126  1.27  christos 	switch (af) {
    127  1.27  christos 	case AF_INET:
    128  1.27  christos 		route_cb.ip_count += cnt;
    129  1.27  christos 		return;
    130  1.30    itojun #ifdef INET6
    131  1.30    itojun 	case AF_INET6:
    132  1.30    itojun 		route_cb.ip6_count += cnt;
    133  1.30    itojun 		return;
    134  1.30    itojun #endif
    135  1.27  christos 	case AF_IPX:
    136  1.27  christos 		route_cb.ipx_count += cnt;
    137  1.27  christos 		return;
    138  1.27  christos 	case AF_NS:
    139  1.27  christos 		route_cb.ns_count += cnt;
    140  1.27  christos 		return;
    141  1.27  christos 	case AF_ISO:
    142  1.27  christos 		route_cb.iso_count += cnt;
    143  1.27  christos 		return;
    144  1.27  christos 	}
    145  1.27  christos }
    146  1.27  christos 
    147   1.1       cgd /*ARGSUSED*/
    148   1.9   mycroft int
    149  1.19   mycroft route_usrreq(so, req, m, nam, control, p)
    150  1.39  augustss 	struct socket *so;
    151   1.1       cgd 	int req;
    152   1.1       cgd 	struct mbuf *m, *nam, *control;
    153  1.19   mycroft 	struct proc *p;
    154   1.1       cgd {
    155  1.39  augustss 	int error = 0;
    156  1.39  augustss 	struct rawcb *rp = sotorawcb(so);
    157   1.1       cgd 	int s;
    158  1.10   mycroft 
    159   1.1       cgd 	if (req == PRU_ATTACH) {
    160   1.1       cgd 		MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
    161  1.17  christos 		if ((so->so_pcb = rp) != NULL)
    162  1.17  christos 			bzero(so->so_pcb, sizeof(*rp));
    163   1.1       cgd 
    164   1.1       cgd 	}
    165  1.27  christos 	if (req == PRU_DETACH && rp)
    166  1.27  christos 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    167  1.14   mycroft 	s = splsoftnet();
    168  1.23   thorpej 
    169  1.23   thorpej 	/*
    170  1.23   thorpej 	 * Don't call raw_usrreq() in the attach case, because
    171  1.23   thorpej 	 * we want to allow non-privileged processes to listen on
    172  1.23   thorpej 	 * and send "safe" commands to the routing socket.
    173  1.23   thorpej 	 */
    174  1.23   thorpej 	if (req == PRU_ATTACH) {
    175  1.23   thorpej 		if (p == 0)
    176  1.23   thorpej 			error = EACCES;
    177  1.23   thorpej 		else
    178  1.23   thorpej 			error = raw_attach(so, (int)(long)nam);
    179  1.23   thorpej 	} else
    180  1.23   thorpej 		error = raw_usrreq(so, req, m, nam, control, p);
    181  1.23   thorpej 
    182   1.1       cgd 	rp = sotorawcb(so);
    183   1.1       cgd 	if (req == PRU_ATTACH && rp) {
    184   1.1       cgd 		if (error) {
    185   1.1       cgd 			free((caddr_t)rp, M_PCB);
    186   1.1       cgd 			splx(s);
    187   1.1       cgd 			return (error);
    188   1.1       cgd 		}
    189  1.27  christos 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    190  1.20   mycroft 		rp->rcb_laddr = &route_src;
    191  1.20   mycroft 		rp->rcb_faddr = &route_dst;
    192   1.1       cgd 		soisconnected(so);
    193   1.1       cgd 		so->so_options |= SO_USELOOPBACK;
    194   1.1       cgd 	}
    195   1.1       cgd 	splx(s);
    196   1.1       cgd 	return (error);
    197   1.1       cgd }
    198   1.1       cgd 
    199   1.1       cgd /*ARGSUSED*/
    200   1.9   mycroft int
    201  1.17  christos #if __STDC__
    202  1.17  christos route_output(struct mbuf *m, ...)
    203  1.17  christos #else
    204  1.17  christos route_output(m, va_alist)
    205  1.17  christos 	struct mbuf *m;
    206  1.17  christos 	va_dcl
    207  1.17  christos #endif
    208   1.1       cgd {
    209  1.39  augustss 	struct rt_msghdr *rtm = 0;
    210  1.39  augustss 	struct radix_node *rn = 0;
    211  1.39  augustss 	struct rtentry *rt = 0;
    212   1.1       cgd 	struct rtentry *saved_nrt = 0;
    213  1.16       cgd 	struct radix_node_head *rnh;
    214  1.10   mycroft 	struct rt_addrinfo info;
    215   1.1       cgd 	int len, error = 0;
    216   1.1       cgd 	struct ifnet *ifp = 0;
    217  1.35    itojun 	struct ifaddr *ifa = 0;
    218  1.17  christos 	struct socket *so;
    219  1.17  christos 	va_list ap;
    220  1.17  christos 
    221  1.17  christos 	va_start(ap, m);
    222  1.17  christos 	so = va_arg(ap, struct socket *);
    223  1.17  christos 	va_end(ap);
    224  1.17  christos 
    225  1.37    itojun #define senderr(e) do { error = e; goto flush;} while (0)
    226  1.12       cgd 	if (m == 0 || ((m->m_len < sizeof(int32_t)) &&
    227  1.21  christos 	   (m = m_pullup(m, sizeof(int32_t))) == 0))
    228   1.1       cgd 		return (ENOBUFS);
    229   1.1       cgd 	if ((m->m_flags & M_PKTHDR) == 0)
    230   1.1       cgd 		panic("route_output");
    231   1.1       cgd 	len = m->m_pkthdr.len;
    232   1.1       cgd 	if (len < sizeof(*rtm) ||
    233  1.10   mycroft 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
    234  1.10   mycroft 		dst = 0;
    235   1.1       cgd 		senderr(EINVAL);
    236  1.10   mycroft 	}
    237   1.1       cgd 	R_Malloc(rtm, struct rt_msghdr *, len);
    238  1.10   mycroft 	if (rtm == 0) {
    239  1.10   mycroft 		dst = 0;
    240   1.1       cgd 		senderr(ENOBUFS);
    241  1.10   mycroft 	}
    242   1.1       cgd 	m_copydata(m, 0, len, (caddr_t)rtm);
    243  1.10   mycroft 	if (rtm->rtm_version != RTM_VERSION) {
    244  1.10   mycroft 		dst = 0;
    245   1.1       cgd 		senderr(EPROTONOSUPPORT);
    246  1.10   mycroft 	}
    247   1.1       cgd 	rtm->rtm_pid = curproc->p_pid;
    248  1.45    itojun 	bzero(&info, sizeof(info));
    249  1.10   mycroft 	info.rti_addrs = rtm->rtm_addrs;
    250  1.42       erh 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
    251  1.42       erh 		senderr(EINVAL);
    252  1.45    itojun 	info.rti_flags = rtm->rtm_flags;
    253  1.26      fvdl 	if (dst == 0 || (dst->sa_family >= AF_MAX))
    254  1.26      fvdl 		senderr(EINVAL);
    255  1.26      fvdl 	if (gate != 0 && (gate->sa_family >= AF_MAX))
    256   1.1       cgd 		senderr(EINVAL);
    257  1.10   mycroft 	if (genmask) {
    258  1.10   mycroft 		struct radix_node *t;
    259  1.16       cgd 		t = rn_addmask((caddr_t)genmask, 0, 1);
    260   1.1       cgd 		if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
    261   1.1       cgd 			genmask = (struct sockaddr *)(t->rn_key);
    262   1.1       cgd 		else
    263   1.1       cgd 			senderr(ENOBUFS);
    264   1.1       cgd 	}
    265  1.23   thorpej 
    266  1.23   thorpej 	/*
    267  1.23   thorpej 	 * Verify that the caller has the appropriate privilege; RTM_GET
    268  1.23   thorpej 	 * is the only operation the non-superuser is allowed.
    269  1.23   thorpej 	 */
    270  1.23   thorpej 	if (rtm->rtm_type != RTM_GET &&
    271  1.23   thorpej 	    suser(curproc->p_ucred, &curproc->p_acflag) != 0)
    272  1.23   thorpej 		senderr(EACCES);
    273  1.23   thorpej 
    274   1.1       cgd 	switch (rtm->rtm_type) {
    275  1.10   mycroft 
    276   1.1       cgd 	case RTM_ADD:
    277   1.1       cgd 		if (gate == 0)
    278   1.1       cgd 			senderr(EINVAL);
    279  1.45    itojun 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    280   1.1       cgd 		if (error == 0 && saved_nrt) {
    281   1.1       cgd 			rt_setmetrics(rtm->rtm_inits,
    282  1.21  christos 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
    283   1.1       cgd 			saved_nrt->rt_refcnt--;
    284   1.1       cgd 			saved_nrt->rt_genmask = genmask;
    285   1.1       cgd 		}
    286   1.1       cgd 		break;
    287   1.1       cgd 
    288   1.1       cgd 	case RTM_DELETE:
    289  1.45    itojun 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    290  1.16       cgd 		if (error == 0) {
    291  1.16       cgd 			(rt = saved_nrt)->rt_refcnt++;
    292  1.16       cgd 			goto report;
    293  1.16       cgd 		}
    294   1.1       cgd 		break;
    295   1.1       cgd 
    296   1.1       cgd 	case RTM_GET:
    297   1.1       cgd 	case RTM_CHANGE:
    298   1.1       cgd 	case RTM_LOCK:
    299  1.16       cgd 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
    300  1.16       cgd 			senderr(EAFNOSUPPORT);
    301  1.37    itojun 		}
    302  1.37    itojun 		rn = rnh->rnh_lookup(dst, netmask, rnh);
    303  1.37    itojun 		if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0) {
    304   1.1       cgd 			senderr(ESRCH);
    305  1.37    itojun 		}
    306  1.37    itojun 		rt = (struct rtentry *)rn;
    307  1.37    itojun 		rt->rt_refcnt++;
    308  1.37    itojun 
    309   1.1       cgd 		switch(rtm->rtm_type) {
    310   1.1       cgd 
    311   1.1       cgd 		case RTM_GET:
    312  1.16       cgd 		report:
    313  1.10   mycroft 			dst = rt_key(rt);
    314  1.10   mycroft 			gate = rt->rt_gateway;
    315  1.10   mycroft 			netmask = rt_mask(rt);
    316  1.10   mycroft 			genmask = rt->rt_genmask;
    317   1.1       cgd 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
    318  1.17  christos 				if ((ifp = rt->rt_ifp) != NULL) {
    319  1.13   mycroft 					ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    320   1.1       cgd 					ifaaddr = rt->rt_ifa->ifa_addr;
    321  1.16       cgd 					if (ifp->if_flags & IFF_POINTOPOINT)
    322  1.16       cgd 						brdaddr = rt->rt_ifa->ifa_dstaddr;
    323  1.16       cgd 					else
    324  1.16       cgd 						brdaddr = 0;
    325  1.10   mycroft 					rtm->rtm_index = ifp->if_index;
    326   1.1       cgd 				} else {
    327  1.10   mycroft 					ifpaddr = 0;
    328  1.10   mycroft 					ifaaddr = 0;
    329  1.37    itojun 				}
    330   1.1       cgd 			}
    331  1.29    chopps 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
    332  1.29    chopps 			    (struct walkarg *)0, &len);
    333   1.1       cgd 			if (len > rtm->rtm_msglen) {
    334   1.1       cgd 				struct rt_msghdr *new_rtm;
    335   1.1       cgd 				R_Malloc(new_rtm, struct rt_msghdr *, len);
    336   1.1       cgd 				if (new_rtm == 0)
    337   1.1       cgd 					senderr(ENOBUFS);
    338   1.1       cgd 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
    339   1.1       cgd 				Free(rtm); rtm = new_rtm;
    340   1.1       cgd 			}
    341  1.22   mycroft 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
    342  1.29    chopps 			    (struct walkarg *)0, 0);
    343   1.1       cgd 			rtm->rtm_flags = rt->rt_flags;
    344   1.1       cgd 			rtm->rtm_rmx = rt->rt_rmx;
    345  1.10   mycroft 			rtm->rtm_addrs = info.rti_addrs;
    346   1.1       cgd 			break;
    347   1.1       cgd 
    348   1.1       cgd 		case RTM_CHANGE:
    349  1.45    itojun 			/*
    350  1.45    itojun 			 * new gateway could require new ifaddr, ifp;
    351  1.45    itojun 			 * flags may also be different; ifp may be specified
    352  1.45    itojun 			 * by ll sockaddr when protocol address is ambiguous
    353  1.45    itojun 			 */
    354  1.45    itojun 			if ((error = rt_getifa(&info)) != 0)
    355  1.45    itojun 				senderr(error);
    356  1.10   mycroft 			if (gate && rt_setgate(rt, rt_key(rt), gate))
    357   1.1       cgd 				senderr(EDQUOT);
    358  1.35    itojun 			/* new gateway could require new ifaddr, ifp;
    359  1.35    itojun 			   flags may also be different; ifp may be specified
    360  1.35    itojun 			   by ll sockaddr when protocol address is ambiguous */
    361  1.35    itojun 			if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
    362  1.35    itojun 			    (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
    363  1.35    itojun 				ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
    364  1.35    itojun 				    ifp);
    365  1.35    itojun 			else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
    366  1.35    itojun 			    (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
    367  1.35    itojun 			    rt_key(rt), gate))))
    368  1.35    itojun 				ifp = ifa->ifa_ifp;
    369  1.35    itojun 			if (ifa) {
    370  1.39  augustss 				struct ifaddr *oifa = rt->rt_ifa;
    371  1.35    itojun 				if (oifa != ifa) {
    372  1.35    itojun 				    if (oifa && oifa->ifa_rtrequest)
    373  1.45    itojun 					oifa->ifa_rtrequest(RTM_DELETE, rt,
    374  1.45    itojun 					    &info);
    375  1.35    itojun 				    IFAFREE(rt->rt_ifa);
    376  1.35    itojun 				    rt->rt_ifa = ifa;
    377  1.35    itojun 				    IFAREF(rt->rt_ifa);
    378  1.35    itojun 				    rt->rt_ifp = ifp;
    379  1.35    itojun 				}
    380  1.35    itojun 			}
    381   1.1       cgd 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
    382  1.21  christos 			    &rt->rt_rmx);
    383  1.35    itojun 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    384  1.45    itojun 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
    385   1.1       cgd 			if (genmask)
    386   1.1       cgd 				rt->rt_genmask = genmask;
    387   1.1       cgd 			/*
    388   1.1       cgd 			 * Fall into
    389   1.1       cgd 			 */
    390   1.1       cgd 		case RTM_LOCK:
    391  1.10   mycroft 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    392   1.1       cgd 			rt->rt_rmx.rmx_locks |=
    393  1.21  christos 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    394   1.1       cgd 			break;
    395   1.1       cgd 		}
    396  1.10   mycroft 		break;
    397   1.1       cgd 
    398   1.1       cgd 	default:
    399   1.1       cgd 		senderr(EOPNOTSUPP);
    400   1.1       cgd 	}
    401   1.1       cgd 
    402   1.1       cgd flush:
    403   1.1       cgd 	if (rtm) {
    404   1.1       cgd 		if (error)
    405   1.1       cgd 			rtm->rtm_errno = error;
    406   1.1       cgd 		else
    407   1.1       cgd 			rtm->rtm_flags |= RTF_DONE;
    408   1.1       cgd 	}
    409   1.1       cgd 	if (rt)
    410   1.1       cgd 		rtfree(rt);
    411   1.1       cgd     {
    412  1.39  augustss 	struct rawcb *rp = 0;
    413   1.1       cgd 	/*
    414   1.1       cgd 	 * Check to see if we don't want our own messages.
    415   1.1       cgd 	 */
    416   1.1       cgd 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    417   1.1       cgd 		if (route_cb.any_count <= 1) {
    418   1.1       cgd 			if (rtm)
    419   1.1       cgd 				Free(rtm);
    420   1.1       cgd 			m_freem(m);
    421   1.1       cgd 			return (error);
    422   1.1       cgd 		}
    423   1.1       cgd 		/* There is another listener, so construct message */
    424   1.1       cgd 		rp = sotorawcb(so);
    425   1.1       cgd 	}
    426  1.46    itojun 	/*
    427  1.46    itojun 	 * copy rtm into m.
    428  1.46    itojun 	 * XXX is it okay if we omit responses when we are unable to reply?
    429  1.46    itojun 	 */
    430   1.1       cgd 	if (rtm) {
    431  1.46    itojun 		if (rtm->rtm_msglen > MCLBYTES) {
    432  1.46    itojun 			m_freem(m);
    433  1.46    itojun 			m = NULL;
    434  1.46    itojun 		} else if (m->m_pkthdr.len > rtm->rtm_msglen) {
    435  1.46    itojun 			m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
    436  1.46    itojun 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
    437  1.46    itojun 		} else {
    438  1.46    itojun 			m_freem(m);
    439  1.46    itojun 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    440  1.46    itojun 			if (m && rtm->rtm_msglen > MHLEN) {
    441  1.46    itojun 				MCLGET(m, M_DONTWAIT);
    442  1.46    itojun 				if ((m->m_flags & M_EXT) == 0) {
    443  1.46    itojun 					m_free(m);
    444  1.46    itojun 					m = NULL;
    445  1.46    itojun 				}
    446  1.46    itojun 			}
    447  1.46    itojun 			if (m) {
    448  1.46    itojun 				m->m_pkthdr.len = m->m_len = rtm->rtm_msglen;
    449  1.46    itojun 				m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
    450  1.46    itojun 			}
    451  1.46    itojun 		}
    452   1.1       cgd 		Free(rtm);
    453  1.46    itojun 	} else {
    454  1.46    itojun 		m_freem(m);
    455  1.46    itojun 		m = NULL;
    456   1.1       cgd 	}
    457   1.1       cgd 	if (rp)
    458   1.1       cgd 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    459   1.1       cgd 	if (dst)
    460   1.1       cgd 		route_proto.sp_protocol = dst->sa_family;
    461  1.46    itojun 	if (m)
    462  1.46    itojun 		raw_input(m, &route_proto, &route_src, &route_dst);
    463   1.1       cgd 	if (rp)
    464   1.1       cgd 		rp->rcb_proto.sp_family = PF_ROUTE;
    465   1.1       cgd     }
    466   1.1       cgd 	return (error);
    467   1.1       cgd }
    468   1.1       cgd 
    469   1.9   mycroft void
    470   1.1       cgd rt_setmetrics(which, in, out)
    471   1.1       cgd 	u_long which;
    472  1.39  augustss 	struct rt_metrics *in, *out;
    473   1.1       cgd {
    474   1.1       cgd #define metric(f, e) if (which & (f)) out->e = in->e;
    475   1.1       cgd 	metric(RTV_RPIPE, rmx_recvpipe);
    476   1.1       cgd 	metric(RTV_SPIPE, rmx_sendpipe);
    477   1.1       cgd 	metric(RTV_SSTHRESH, rmx_ssthresh);
    478   1.1       cgd 	metric(RTV_RTT, rmx_rtt);
    479   1.1       cgd 	metric(RTV_RTTVAR, rmx_rttvar);
    480   1.1       cgd 	metric(RTV_HOPCOUNT, rmx_hopcount);
    481   1.1       cgd 	metric(RTV_MTU, rmx_mtu);
    482   1.1       cgd 	metric(RTV_EXPIRE, rmx_expire);
    483   1.1       cgd #undef metric
    484   1.1       cgd }
    485   1.1       cgd 
    486  1.10   mycroft #define ROUNDUP(a) \
    487  1.18       cgd 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
    488  1.10   mycroft #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
    489  1.10   mycroft 
    490  1.42       erh static int
    491  1.10   mycroft rt_xaddrs(cp, cplim, rtinfo)
    492  1.39  augustss 	caddr_t cp, cplim;
    493  1.39  augustss 	struct rt_addrinfo *rtinfo;
    494  1.10   mycroft {
    495  1.39  augustss 	struct sockaddr *sa;
    496  1.39  augustss 	int i;
    497  1.10   mycroft 
    498  1.10   mycroft 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
    499  1.10   mycroft 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
    500  1.10   mycroft 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    501  1.10   mycroft 			continue;
    502  1.10   mycroft 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
    503  1.10   mycroft 		ADVANCE(cp, sa);
    504  1.10   mycroft 	}
    505  1.44     enami 
    506  1.44     enami 	/* Check for extra addresses specified.  */
    507  1.44     enami 	if ((rtinfo->rti_addrs & (~0 << i)) != 0)
    508  1.44     enami 		return (1);
    509  1.44     enami 	/* Check for bad data length.  */
    510  1.44     enami 	if (cp != cplim) {
    511  1.44     enami 		if (i == RTAX_NETMASK + 1 &&
    512  1.44     enami 		    cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
    513  1.44     enami 			/*
    514  1.44     enami 			 * The last sockaddr was netmask.
    515  1.44     enami 			 * We accept this for now for the sake of old
    516  1.44     enami 			 * binaries or third party softwares.
    517  1.44     enami 			 */
    518  1.44     enami 			;
    519  1.44     enami 		else
    520  1.44     enami 			return (1);
    521  1.44     enami 	}
    522  1.44     enami 	return (0);
    523   1.1       cgd }
    524   1.1       cgd 
    525  1.10   mycroft static struct mbuf *
    526  1.32    bouyer rt_msg1(type, rtinfo, data, datalen)
    527  1.10   mycroft 	int type;
    528  1.39  augustss 	struct rt_addrinfo *rtinfo;
    529  1.32    bouyer 	caddr_t data;
    530  1.32    bouyer 	int datalen;
    531   1.1       cgd {
    532  1.39  augustss 	struct rt_msghdr *rtm;
    533  1.39  augustss 	struct mbuf *m;
    534  1.39  augustss 	int i;
    535  1.39  augustss 	struct sockaddr *sa;
    536  1.46    itojun 	int hlen, alen, len, dlen;
    537   1.1       cgd 
    538  1.46    itojun 	/* pre-compute length */
    539  1.10   mycroft 	switch (type) {
    540  1.10   mycroft 	case RTM_DELADDR:
    541  1.10   mycroft 	case RTM_NEWADDR:
    542  1.46    itojun 		hlen = sizeof(struct ifa_msghdr);
    543  1.10   mycroft 		break;
    544  1.10   mycroft 
    545  1.32    bouyer #ifdef COMPAT_14
    546  1.32    bouyer 	case RTM_OIFINFO:
    547  1.46    itojun 		hlen = sizeof(struct if_msghdr14);
    548  1.32    bouyer 		break;
    549  1.32    bouyer #endif
    550  1.32    bouyer 
    551  1.10   mycroft 	case RTM_IFINFO:
    552  1.46    itojun 		hlen = sizeof(struct if_msghdr);
    553  1.10   mycroft 		break;
    554  1.10   mycroft 
    555  1.36   thorpej 	case RTM_IFANNOUNCE:
    556  1.46    itojun 		hlen = sizeof(struct if_announcemsghdr);
    557  1.36   thorpej 		break;
    558  1.36   thorpej 
    559  1.10   mycroft 	default:
    560  1.46    itojun 		hlen = sizeof(struct rt_msghdr);
    561  1.46    itojun 		break;
    562  1.10   mycroft 	}
    563  1.46    itojun 	alen = 0;
    564  1.46    itojun 	for (i = 0; i < RTAX_MAX; i++) {
    565  1.46    itojun 		if ((sa = rtinfo->rti_info[i]) == NULL)
    566  1.46    itojun 			continue;
    567  1.46    itojun 		dlen = ROUNDUP(sa->sa_len);
    568  1.46    itojun 		alen += dlen;
    569  1.46    itojun 	}
    570  1.46    itojun 
    571  1.46    itojun 	if (hlen > MHLEN || alen > MCLBYTES)
    572  1.34    itojun 		panic("rt_msg1: message too long");
    573  1.46    itojun 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    574  1.46    itojun 	if (!m)
    575  1.46    itojun 		return (NULL);
    576  1.46    itojun 	m->m_pkthdr.len = hlen;
    577  1.46    itojun 	m->m_len = hlen;
    578  1.46    itojun 	if (hlen + alen > MHLEN) {
    579  1.32    bouyer 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
    580  1.46    itojun 		if (m->m_next && alen > MLEN) {
    581  1.46    itojun 			MCLGET(m->m_next, M_DONTWAIT);
    582  1.46    itojun 			if ((m->m_next->m_flags & M_EXT) == 0) {
    583  1.46    itojun 				m_freem(m->m_next);
    584  1.46    itojun 				m->m_next = NULL;
    585  1.46    itojun 			}
    586  1.46    itojun 		}
    587  1.46    itojun 		if (!m->m_next) {
    588  1.32    bouyer 			m_freem(m);
    589  1.32    bouyer 			return (NULL);
    590  1.32    bouyer 		}
    591  1.46    itojun 		m->m_pkthdr.len += alen;
    592  1.46    itojun 		m->m_next->m_len = alen;
    593  1.46    itojun 	} else  {
    594  1.46    itojun 		m->m_pkthdr.len += alen;
    595  1.46    itojun 		m->m_len += alen;
    596  1.32    bouyer 	}
    597  1.46    itojun 	m->m_pkthdr.rcvif = NULL;
    598  1.32    bouyer 	m_copyback(m, 0, datalen, data);
    599   1.1       cgd 	rtm = mtod(m, struct rt_msghdr *);
    600  1.46    itojun 	len = hlen;
    601  1.10   mycroft 	for (i = 0; i < RTAX_MAX; i++) {
    602  1.10   mycroft 		if ((sa = rtinfo->rti_info[i]) == NULL)
    603  1.10   mycroft 			continue;
    604  1.10   mycroft 		rtinfo->rti_addrs |= (1 << i);
    605  1.10   mycroft 		dlen = ROUNDUP(sa->sa_len);
    606  1.10   mycroft 		m_copyback(m, len, dlen, (caddr_t)sa);
    607  1.10   mycroft 		len += dlen;
    608  1.10   mycroft 	}
    609   1.1       cgd 	rtm->rtm_msglen = len;
    610   1.1       cgd 	rtm->rtm_version = RTM_VERSION;
    611   1.1       cgd 	rtm->rtm_type = type;
    612  1.10   mycroft 	return (m);
    613  1.10   mycroft }
    614  1.10   mycroft 
    615  1.29    chopps /*
    616  1.29    chopps  * rt_msg2
    617  1.29    chopps  *
    618  1.29    chopps  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    619  1.29    chopps  *		returns the length of the message in 'lenp'.
    620  1.29    chopps  *
    621  1.29    chopps  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    622  1.29    chopps  *	the message
    623  1.29    chopps  * otherwise walkarg's w_needed is updated and if the user buffer is
    624  1.29    chopps  *	specified and w_needed indicates space exists the information is copied
    625  1.29    chopps  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    626  1.29    chopps  *	if the allocation fails ENOBUFS is returned.
    627  1.29    chopps  */
    628  1.10   mycroft static int
    629  1.29    chopps rt_msg2(type, rtinfo, cp, w, lenp)
    630  1.10   mycroft 	int type;
    631  1.39  augustss 	struct rt_addrinfo *rtinfo;
    632  1.10   mycroft 	caddr_t cp;
    633  1.10   mycroft 	struct walkarg *w;
    634  1.29    chopps 	int *lenp;
    635  1.10   mycroft {
    636  1.39  augustss 	int i;
    637  1.10   mycroft 	int len, dlen, second_time = 0;
    638  1.10   mycroft 	caddr_t cp0;
    639  1.10   mycroft 
    640  1.10   mycroft 	rtinfo->rti_addrs = 0;
    641  1.10   mycroft again:
    642  1.10   mycroft 	switch (type) {
    643  1.10   mycroft 
    644  1.10   mycroft 	case RTM_DELADDR:
    645  1.10   mycroft 	case RTM_NEWADDR:
    646  1.10   mycroft 		len = sizeof(struct ifa_msghdr);
    647  1.10   mycroft 		break;
    648  1.32    bouyer #ifdef COMPAT_14
    649  1.32    bouyer 	case RTM_OIFINFO:
    650  1.32    bouyer 		len = sizeof(struct if_msghdr14);
    651  1.32    bouyer 		break;
    652  1.32    bouyer #endif
    653  1.10   mycroft 
    654  1.10   mycroft 	case RTM_IFINFO:
    655  1.10   mycroft 		len = sizeof(struct if_msghdr);
    656  1.10   mycroft 		break;
    657  1.10   mycroft 
    658  1.10   mycroft 	default:
    659  1.10   mycroft 		len = sizeof(struct rt_msghdr);
    660  1.10   mycroft 	}
    661  1.17  christos 	if ((cp0 = cp) != NULL)
    662  1.10   mycroft 		cp += len;
    663  1.10   mycroft 	for (i = 0; i < RTAX_MAX; i++) {
    664  1.39  augustss 		struct sockaddr *sa;
    665  1.10   mycroft 
    666  1.10   mycroft 		if ((sa = rtinfo->rti_info[i]) == 0)
    667  1.10   mycroft 			continue;
    668  1.10   mycroft 		rtinfo->rti_addrs |= (1 << i);
    669  1.10   mycroft 		dlen = ROUNDUP(sa->sa_len);
    670  1.10   mycroft 		if (cp) {
    671  1.21  christos 			bcopy(sa, cp, (unsigned)dlen);
    672  1.10   mycroft 			cp += dlen;
    673  1.10   mycroft 		}
    674   1.1       cgd 		len += dlen;
    675   1.1       cgd 	}
    676  1.10   mycroft 	if (cp == 0 && w != NULL && !second_time) {
    677  1.39  augustss 		struct walkarg *rw = w;
    678  1.10   mycroft 
    679  1.10   mycroft 		rw->w_needed += len;
    680  1.10   mycroft 		if (rw->w_needed <= 0 && rw->w_where) {
    681  1.10   mycroft 			if (rw->w_tmemsize < len) {
    682  1.10   mycroft 				if (rw->w_tmem)
    683  1.10   mycroft 					free(rw->w_tmem, M_RTABLE);
    684  1.17  christos 				rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
    685  1.21  christos 				    M_NOWAIT);
    686  1.17  christos 				if (rw->w_tmem)
    687  1.10   mycroft 					rw->w_tmemsize = len;
    688  1.10   mycroft 			}
    689  1.10   mycroft 			if (rw->w_tmem) {
    690  1.10   mycroft 				cp = rw->w_tmem;
    691  1.10   mycroft 				second_time = 1;
    692  1.10   mycroft 				goto again;
    693  1.29    chopps 			} else {
    694  1.29    chopps 				rw->w_tmemneeded = len;
    695  1.29    chopps 				return (ENOBUFS);
    696  1.29    chopps 			}
    697  1.10   mycroft 		}
    698   1.1       cgd 	}
    699  1.10   mycroft 	if (cp) {
    700  1.39  augustss 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
    701  1.10   mycroft 
    702  1.10   mycroft 		rtm->rtm_version = RTM_VERSION;
    703  1.10   mycroft 		rtm->rtm_type = type;
    704  1.10   mycroft 		rtm->rtm_msglen = len;
    705   1.1       cgd 	}
    706  1.29    chopps 	if (lenp)
    707  1.29    chopps 		*lenp = len;
    708  1.29    chopps 	return (0);
    709  1.10   mycroft }
    710  1.10   mycroft 
    711  1.10   mycroft /*
    712  1.10   mycroft  * This routine is called to generate a message from the routing
    713  1.10   mycroft  * socket indicating that a redirect has occured, a routing lookup
    714  1.10   mycroft  * has failed, or that a protocol has detected timeouts to a particular
    715  1.10   mycroft  * destination.
    716  1.10   mycroft  */
    717  1.10   mycroft void
    718  1.10   mycroft rt_missmsg(type, rtinfo, flags, error)
    719  1.10   mycroft 	int type, flags, error;
    720  1.39  augustss 	struct rt_addrinfo *rtinfo;
    721  1.10   mycroft {
    722  1.32    bouyer 	struct rt_msghdr rtm;
    723  1.39  augustss 	struct mbuf *m;
    724  1.10   mycroft 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    725  1.10   mycroft 
    726  1.10   mycroft 	if (route_cb.any_count == 0)
    727  1.10   mycroft 		return;
    728  1.32    bouyer 	bzero(&rtm, sizeof(rtm));
    729  1.32    bouyer 	rtm.rtm_flags = RTF_DONE | flags;
    730  1.32    bouyer 	rtm.rtm_errno = error;
    731  1.32    bouyer 	m = rt_msg1(type, rtinfo, (caddr_t)&rtm, sizeof(rtm));
    732  1.10   mycroft 	if (m == 0)
    733   1.1       cgd 		return;
    734  1.32    bouyer 	mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
    735  1.10   mycroft 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
    736  1.10   mycroft 	raw_input(m, &route_proto, &route_src, &route_dst);
    737  1.10   mycroft }
    738  1.10   mycroft 
    739  1.10   mycroft /*
    740  1.10   mycroft  * This routine is called to generate a message from the routing
    741  1.10   mycroft  * socket indicating that the status of a network interface has changed.
    742  1.10   mycroft  */
    743  1.10   mycroft void
    744  1.10   mycroft rt_ifmsg(ifp)
    745  1.39  augustss 	struct ifnet *ifp;
    746  1.10   mycroft {
    747  1.32    bouyer 	struct if_msghdr ifm;
    748  1.32    bouyer #ifdef COMPAT_14
    749  1.32    bouyer 	struct if_msghdr14 oifm;
    750  1.32    bouyer #endif
    751  1.10   mycroft 	struct mbuf *m;
    752  1.10   mycroft 	struct rt_addrinfo info;
    753  1.10   mycroft 
    754  1.10   mycroft 	if (route_cb.any_count == 0)
    755  1.10   mycroft 		return;
    756  1.21  christos 	bzero(&info, sizeof(info));
    757  1.32    bouyer 	bzero(&ifm, sizeof(ifm));
    758  1.32    bouyer 	ifm.ifm_index = ifp->if_index;
    759  1.32    bouyer 	ifm.ifm_flags = ifp->if_flags;
    760  1.32    bouyer 	ifm.ifm_data = ifp->if_data;
    761  1.32    bouyer 	ifm.ifm_addrs = 0;
    762  1.32    bouyer 	m = rt_msg1(RTM_IFINFO, &info, (caddr_t)&ifm, sizeof(ifm));
    763  1.32    bouyer 	if (m == 0)
    764  1.32    bouyer 		return;
    765  1.32    bouyer 	route_proto.sp_protocol = 0;
    766  1.32    bouyer 	raw_input(m, &route_proto, &route_src, &route_dst);
    767  1.32    bouyer #ifdef COMPAT_14
    768  1.32    bouyer 	bzero(&info, sizeof(info));
    769  1.43    itojun 	bzero(&oifm, sizeof(oifm));
    770  1.32    bouyer 	oifm.ifm_index = ifp->if_index;
    771  1.32    bouyer 	oifm.ifm_flags = ifp->if_flags;
    772  1.32    bouyer 	oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
    773  1.32    bouyer 	oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
    774  1.32    bouyer 	oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
    775  1.32    bouyer 	oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
    776  1.32    bouyer 	oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
    777  1.32    bouyer 	oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
    778  1.32    bouyer 	oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
    779  1.32    bouyer 	oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
    780  1.32    bouyer 	oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
    781  1.32    bouyer 	oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
    782  1.32    bouyer 	oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
    783  1.32    bouyer 	oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
    784  1.32    bouyer 	oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
    785  1.32    bouyer 	oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
    786  1.32    bouyer 	oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
    787  1.32    bouyer 	oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
    788  1.32    bouyer 	oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
    789  1.32    bouyer 	oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
    790  1.32    bouyer 	oifm.ifm_addrs = 0;
    791  1.32    bouyer 	m = rt_msg1(RTM_OIFINFO, &info, (caddr_t)&oifm, sizeof(oifm));
    792  1.10   mycroft 	if (m == 0)
    793  1.10   mycroft 		return;
    794  1.10   mycroft 	route_proto.sp_protocol = 0;
    795   1.1       cgd 	raw_input(m, &route_proto, &route_src, &route_dst);
    796  1.32    bouyer #endif
    797   1.1       cgd }
    798   1.1       cgd 
    799   1.1       cgd /*
    800  1.10   mycroft  * This is called to generate messages from the routing socket
    801  1.10   mycroft  * indicating a network interface has had addresses associated with it.
    802  1.10   mycroft  * if we ever reverse the logic and replace messages TO the routing
    803  1.10   mycroft  * socket indicate a request to configure interfaces, then it will
    804  1.10   mycroft  * be unnecessary as the routing socket will automatically generate
    805  1.10   mycroft  * copies of it.
    806  1.10   mycroft  */
    807  1.10   mycroft void
    808  1.10   mycroft rt_newaddrmsg(cmd, ifa, error, rt)
    809  1.10   mycroft 	int cmd, error;
    810  1.39  augustss 	struct ifaddr *ifa;
    811  1.39  augustss 	struct rtentry *rt;
    812  1.10   mycroft {
    813  1.10   mycroft 	struct rt_addrinfo info;
    814  1.17  christos 	struct sockaddr *sa = NULL;
    815  1.10   mycroft 	int pass;
    816  1.17  christos 	struct mbuf *m = NULL;
    817  1.10   mycroft 	struct ifnet *ifp = ifa->ifa_ifp;
    818  1.10   mycroft 
    819  1.10   mycroft 	if (route_cb.any_count == 0)
    820  1.10   mycroft 		return;
    821  1.10   mycroft 	for (pass = 1; pass < 3; pass++) {
    822  1.21  christos 		bzero(&info, sizeof(info));
    823  1.10   mycroft 		if ((cmd == RTM_ADD && pass == 1) ||
    824  1.10   mycroft 		    (cmd == RTM_DELETE && pass == 2)) {
    825  1.32    bouyer 			struct ifa_msghdr ifam;
    826  1.10   mycroft 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
    827  1.10   mycroft 
    828  1.10   mycroft 			ifaaddr = sa = ifa->ifa_addr;
    829  1.13   mycroft 			ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
    830  1.10   mycroft 			netmask = ifa->ifa_netmask;
    831  1.10   mycroft 			brdaddr = ifa->ifa_dstaddr;
    832  1.32    bouyer 			bzero(&ifam, sizeof(ifam));
    833  1.32    bouyer 			ifam.ifam_index = ifp->if_index;
    834  1.32    bouyer 			ifam.ifam_metric = ifa->ifa_metric;
    835  1.32    bouyer 			ifam.ifam_flags = ifa->ifa_flags;
    836  1.32    bouyer 			m = rt_msg1(ncmd, &info, (caddr_t)&ifam, sizeof(ifam));
    837  1.32    bouyer 			if (m == NULL)
    838  1.10   mycroft 				continue;
    839  1.32    bouyer 			mtod(m, struct ifa_msghdr *)->ifam_addrs =
    840  1.32    bouyer 			    info.rti_addrs;
    841  1.10   mycroft 		}
    842  1.10   mycroft 		if ((cmd == RTM_ADD && pass == 2) ||
    843  1.10   mycroft 		    (cmd == RTM_DELETE && pass == 1)) {
    844  1.32    bouyer 			struct rt_msghdr rtm;
    845  1.10   mycroft 
    846  1.10   mycroft 			if (rt == 0)
    847  1.10   mycroft 				continue;
    848  1.10   mycroft 			netmask = rt_mask(rt);
    849  1.10   mycroft 			dst = sa = rt_key(rt);
    850  1.10   mycroft 			gate = rt->rt_gateway;
    851  1.32    bouyer 			bzero(&rtm, sizeof(rtm));
    852  1.32    bouyer 			rtm.rtm_index = ifp->if_index;
    853  1.32    bouyer 			rtm.rtm_flags |= rt->rt_flags;
    854  1.32    bouyer 			rtm.rtm_errno = error;
    855  1.32    bouyer 			m = rt_msg1(cmd, &info, (caddr_t)&rtm, sizeof(rtm));
    856  1.32    bouyer 			if (m == NULL)
    857  1.10   mycroft 				continue;
    858  1.32    bouyer 			mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
    859  1.10   mycroft 		}
    860  1.10   mycroft 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
    861  1.10   mycroft 		raw_input(m, &route_proto, &route_src, &route_dst);
    862  1.10   mycroft 	}
    863  1.36   thorpej }
    864  1.36   thorpej 
    865  1.36   thorpej /*
    866  1.36   thorpej  * This is called to generate routing socket messages indicating
    867  1.36   thorpej  * network interface arrival and departure.
    868  1.36   thorpej  */
    869  1.36   thorpej void
    870  1.36   thorpej rt_ifannouncemsg(ifp, what)
    871  1.36   thorpej 	struct ifnet *ifp;
    872  1.36   thorpej 	int what;
    873  1.36   thorpej {
    874  1.36   thorpej 	struct if_announcemsghdr ifan;
    875  1.36   thorpej 	struct mbuf *m;
    876  1.36   thorpej 	struct rt_addrinfo info;
    877  1.36   thorpej 
    878  1.36   thorpej 	if (route_cb.any_count == 0)
    879  1.36   thorpej 		return;
    880  1.36   thorpej 	bzero(&info, sizeof(info));
    881  1.36   thorpej 	bzero(&ifan, sizeof(ifan));
    882  1.36   thorpej 	ifan.ifan_index = ifp->if_index;
    883  1.36   thorpej 	strcpy(ifan.ifan_name, ifp->if_xname);
    884  1.36   thorpej 	ifan.ifan_what = what;
    885  1.36   thorpej 	m = rt_msg1(RTM_IFANNOUNCE, &info, (caddr_t)&ifan, sizeof(ifan));
    886  1.36   thorpej 	if (m == 0)
    887  1.36   thorpej 		return;
    888  1.36   thorpej 	route_proto.sp_protocol = 0;
    889  1.36   thorpej 	raw_input(m, &route_proto, &route_src, &route_dst);
    890  1.10   mycroft }
    891  1.10   mycroft 
    892  1.10   mycroft /*
    893  1.10   mycroft  * This is used in dumping the kernel table via sysctl().
    894   1.1       cgd  */
    895  1.40    simonb static int
    896  1.17  christos sysctl_dumpentry(rn, v)
    897   1.1       cgd 	struct radix_node *rn;
    898  1.39  augustss 	void *v;
    899   1.1       cgd {
    900  1.39  augustss 	struct walkarg *w = v;
    901  1.39  augustss 	struct rtentry *rt = (struct rtentry *)rn;
    902  1.10   mycroft 	int error = 0, size;
    903  1.10   mycroft 	struct rt_addrinfo info;
    904   1.1       cgd 
    905  1.10   mycroft 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    906  1.10   mycroft 		return 0;
    907  1.21  christos 	bzero(&info, sizeof(info));
    908  1.10   mycroft 	dst = rt_key(rt);
    909  1.10   mycroft 	gate = rt->rt_gateway;
    910  1.10   mycroft 	netmask = rt_mask(rt);
    911  1.10   mycroft 	genmask = rt->rt_genmask;
    912  1.16       cgd 	if (rt->rt_ifp) {
    913  1.16       cgd 		ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
    914  1.16       cgd 		ifaaddr = rt->rt_ifa->ifa_addr;
    915  1.16       cgd 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    916  1.16       cgd 			brdaddr = rt->rt_ifa->ifa_dstaddr;
    917  1.16       cgd 	}
    918  1.29    chopps 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
    919  1.29    chopps 		return (error);
    920  1.29    chopps 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    921  1.39  augustss 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
    922  1.10   mycroft 
    923  1.10   mycroft 		rtm->rtm_flags = rt->rt_flags;
    924  1.10   mycroft 		rtm->rtm_use = rt->rt_use;
    925  1.10   mycroft 		rtm->rtm_rmx = rt->rt_rmx;
    926  1.10   mycroft 		rtm->rtm_index = rt->rt_ifp->if_index;
    927  1.10   mycroft 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    928  1.10   mycroft 		rtm->rtm_addrs = info.rti_addrs;
    929  1.21  christos 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    930  1.10   mycroft 			w->w_where = NULL;
    931  1.10   mycroft 		else
    932  1.10   mycroft 			w->w_where += size;
    933  1.10   mycroft 	}
    934  1.10   mycroft 	return (error);
    935  1.10   mycroft }
    936   1.1       cgd 
    937  1.40    simonb static int
    938  1.32    bouyer sysctl_iflist(af, w, type)
    939  1.10   mycroft 	int	af;
    940  1.39  augustss 	struct	walkarg *w;
    941  1.32    bouyer 	int type;
    942  1.10   mycroft {
    943  1.39  augustss 	struct ifnet *ifp;
    944  1.39  augustss 	struct ifaddr *ifa;
    945  1.10   mycroft 	struct	rt_addrinfo info;
    946  1.10   mycroft 	int	len, error = 0;
    947  1.10   mycroft 
    948  1.21  christos 	bzero(&info, sizeof(info));
    949  1.13   mycroft 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
    950  1.10   mycroft 		if (w->w_arg && w->w_arg != ifp->if_index)
    951  1.10   mycroft 			continue;
    952  1.13   mycroft 		ifa = ifp->if_addrlist.tqh_first;
    953  1.10   mycroft 		ifpaddr = ifa->ifa_addr;
    954  1.32    bouyer 		switch(type) {
    955  1.32    bouyer 		case NET_RT_IFLIST:
    956  1.32    bouyer 			error =
    957  1.32    bouyer 			    rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len);
    958  1.32    bouyer 			break;
    959  1.32    bouyer #ifdef COMPAT_14
    960  1.32    bouyer 		case NET_RT_OIFLIST:
    961  1.32    bouyer 			error =
    962  1.32    bouyer 			    rt_msg2(RTM_OIFINFO, &info, (caddr_t)0, w, &len);
    963  1.32    bouyer 			break;
    964  1.32    bouyer #endif
    965  1.32    bouyer 		default:
    966  1.32    bouyer 			panic("sysctl_iflist(1)");
    967  1.32    bouyer 		}
    968  1.32    bouyer 		if (error)
    969  1.29    chopps 			return (error);
    970  1.10   mycroft 		ifpaddr = 0;
    971  1.29    chopps 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    972  1.32    bouyer 			switch(type) {
    973  1.32    bouyer 			case NET_RT_IFLIST: {
    974  1.39  augustss 				struct if_msghdr *ifm;
    975  1.32    bouyer 
    976  1.32    bouyer 				ifm = (struct if_msghdr *)w->w_tmem;
    977  1.32    bouyer 				ifm->ifm_index = ifp->if_index;
    978  1.32    bouyer 				ifm->ifm_flags = ifp->if_flags;
    979  1.32    bouyer 				ifm->ifm_data = ifp->if_data;
    980  1.32    bouyer 				ifm->ifm_addrs = info.rti_addrs;
    981  1.32    bouyer 				error = copyout(ifm, w->w_where, len);
    982  1.32    bouyer 				if (error)
    983  1.32    bouyer 					return (error);
    984  1.32    bouyer 				w->w_where += len;
    985  1.32    bouyer 				break;
    986  1.32    bouyer 			}
    987  1.10   mycroft 
    988  1.32    bouyer #ifdef COMPAT_14
    989  1.32    bouyer 			case NET_RT_OIFLIST: {
    990  1.39  augustss 				struct if_msghdr14 *ifm;
    991  1.32    bouyer 
    992  1.32    bouyer 				ifm = (struct if_msghdr14 *)w->w_tmem;
    993  1.32    bouyer 				ifm->ifm_index = ifp->if_index;
    994  1.32    bouyer 				ifm->ifm_flags = ifp->if_flags;
    995  1.32    bouyer 				ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
    996  1.32    bouyer 				ifm->ifm_data.ifi_addrlen =
    997  1.32    bouyer 				    ifp->if_data.ifi_addrlen;
    998  1.32    bouyer 				ifm->ifm_data.ifi_hdrlen =
    999  1.32    bouyer 				    ifp->if_data.ifi_hdrlen;
   1000  1.32    bouyer 				ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
   1001  1.32    bouyer 				ifm->ifm_data.ifi_metric =
   1002  1.32    bouyer 				    ifp->if_data.ifi_metric;
   1003  1.32    bouyer 				ifm->ifm_data.ifi_baudrate =
   1004  1.32    bouyer 				    ifp->if_data.ifi_baudrate;
   1005  1.32    bouyer 				ifm->ifm_data.ifi_ipackets =
   1006  1.32    bouyer 				    ifp->if_data.ifi_ipackets;
   1007  1.32    bouyer 				ifm->ifm_data.ifi_ierrors =
   1008  1.32    bouyer 				    ifp->if_data.ifi_ierrors;
   1009  1.32    bouyer 				ifm->ifm_data.ifi_opackets =
   1010  1.32    bouyer 				    ifp->if_data.ifi_opackets;
   1011  1.32    bouyer 				ifm->ifm_data.ifi_oerrors =
   1012  1.32    bouyer 				    ifp->if_data.ifi_oerrors;
   1013  1.32    bouyer 				ifm->ifm_data.ifi_collisions =
   1014  1.32    bouyer 				    ifp->if_data.ifi_collisions;
   1015  1.32    bouyer 				ifm->ifm_data.ifi_ibytes =
   1016  1.32    bouyer 				    ifp->if_data.ifi_ibytes;
   1017  1.32    bouyer 				ifm->ifm_data.ifi_obytes =
   1018  1.32    bouyer 				    ifp->if_data.ifi_obytes;
   1019  1.32    bouyer 				ifm->ifm_data.ifi_imcasts =
   1020  1.32    bouyer 				    ifp->if_data.ifi_imcasts;
   1021  1.32    bouyer 				ifm->ifm_data.ifi_omcasts =
   1022  1.32    bouyer 				    ifp->if_data.ifi_omcasts;
   1023  1.32    bouyer 				ifm->ifm_data.ifi_iqdrops =
   1024  1.32    bouyer 				    ifp->if_data.ifi_iqdrops;
   1025  1.32    bouyer 				ifm->ifm_data.ifi_noproto =
   1026  1.32    bouyer 				    ifp->if_data.ifi_noproto;
   1027  1.32    bouyer 				ifm->ifm_data.ifi_lastchange =
   1028  1.32    bouyer 				    ifp->if_data.ifi_lastchange;
   1029  1.32    bouyer 				ifm->ifm_addrs = info.rti_addrs;
   1030  1.32    bouyer 				error = copyout(ifm, w->w_where, len);
   1031  1.32    bouyer 				if (error)
   1032  1.32    bouyer 					return (error);
   1033  1.32    bouyer 				w->w_where += len;
   1034  1.32    bouyer 				break;
   1035  1.32    bouyer 			}
   1036  1.32    bouyer #endif
   1037  1.32    bouyer 			default:
   1038  1.32    bouyer 				panic("sysctl_iflist(2)");
   1039  1.32    bouyer 			}
   1040  1.10   mycroft 		}
   1041  1.17  christos 		while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
   1042  1.10   mycroft 			if (af && af != ifa->ifa_addr->sa_family)
   1043  1.10   mycroft 				continue;
   1044  1.10   mycroft 			ifaaddr = ifa->ifa_addr;
   1045  1.10   mycroft 			netmask = ifa->ifa_netmask;
   1046  1.10   mycroft 			brdaddr = ifa->ifa_dstaddr;
   1047  1.29    chopps 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
   1048  1.29    chopps 				return (error);
   1049  1.29    chopps 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1050  1.39  augustss 				struct ifa_msghdr *ifam;
   1051  1.10   mycroft 
   1052  1.10   mycroft 				ifam = (struct ifa_msghdr *)w->w_tmem;
   1053  1.10   mycroft 				ifam->ifam_index = ifa->ifa_ifp->if_index;
   1054  1.10   mycroft 				ifam->ifam_flags = ifa->ifa_flags;
   1055  1.10   mycroft 				ifam->ifam_metric = ifa->ifa_metric;
   1056  1.10   mycroft 				ifam->ifam_addrs = info.rti_addrs;
   1057  1.17  christos 				error = copyout(w->w_tmem, w->w_where, len);
   1058  1.17  christos 				if (error)
   1059  1.10   mycroft 					return (error);
   1060  1.10   mycroft 				w->w_where += len;
   1061  1.10   mycroft 			}
   1062  1.10   mycroft 		}
   1063  1.10   mycroft 		ifaaddr = netmask = brdaddr = 0;
   1064  1.10   mycroft 	}
   1065   1.1       cgd 	return (0);
   1066   1.1       cgd }
   1067   1.1       cgd 
   1068  1.40    simonb static int
   1069  1.10   mycroft sysctl_rtable(name, namelen, where, given, new, newlen)
   1070  1.10   mycroft 	int	*name;
   1071  1.17  christos 	u_int	namelen;
   1072  1.17  christos 	void 	*where;
   1073  1.10   mycroft 	size_t	*given;
   1074  1.17  christos 	void	*new;
   1075  1.10   mycroft 	size_t	newlen;
   1076   1.1       cgd {
   1077  1.39  augustss 	struct radix_node_head *rnh;
   1078  1.10   mycroft 	int	i, s, error = EINVAL;
   1079  1.10   mycroft 	u_char  af;
   1080   1.1       cgd 	struct	walkarg w;
   1081   1.1       cgd 
   1082  1.10   mycroft 	if (new)
   1083  1.10   mycroft 		return (EPERM);
   1084  1.10   mycroft 	if (namelen != 3)
   1085   1.1       cgd 		return (EINVAL);
   1086  1.10   mycroft 	af = name[0];
   1087  1.29    chopps 	w.w_tmemneeded = 0;
   1088  1.29    chopps 	w.w_tmemsize = 0;
   1089  1.29    chopps 	w.w_tmem = NULL;
   1090  1.29    chopps again:
   1091  1.29    chopps 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   1092  1.29    chopps 	if (w.w_tmemneeded) {
   1093  1.29    chopps 		w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
   1094  1.29    chopps 		w.w_tmemsize = w.w_tmemneeded;
   1095  1.29    chopps 		w.w_tmemneeded = 0;
   1096  1.29    chopps 	}
   1097  1.29    chopps 	w.w_op = name[1];
   1098  1.29    chopps 	w.w_arg = name[2];
   1099  1.10   mycroft 	w.w_given = *given;
   1100   1.1       cgd 	w.w_needed = 0 - w.w_given;
   1101  1.29    chopps 	w.w_where = where;
   1102   1.1       cgd 
   1103  1.14   mycroft 	s = splsoftnet();
   1104  1.10   mycroft 	switch (w.w_op) {
   1105  1.10   mycroft 
   1106  1.10   mycroft 	case NET_RT_DUMP:
   1107  1.10   mycroft 	case NET_RT_FLAGS:
   1108  1.10   mycroft 		for (i = 1; i <= AF_MAX; i++)
   1109  1.10   mycroft 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
   1110  1.17  christos 			    (error = (*rnh->rnh_walktree)(rnh,
   1111  1.21  christos 			    sysctl_dumpentry, &w)))
   1112  1.10   mycroft 				break;
   1113  1.10   mycroft 		break;
   1114  1.10   mycroft 
   1115  1.32    bouyer #ifdef COMPAT_14
   1116  1.32    bouyer 	case NET_RT_OIFLIST:
   1117  1.32    bouyer 		error = sysctl_iflist(af, &w, w.w_op);
   1118  1.32    bouyer 		break;
   1119  1.32    bouyer #endif
   1120  1.32    bouyer 
   1121  1.10   mycroft 	case NET_RT_IFLIST:
   1122  1.32    bouyer 		error = sysctl_iflist(af, &w, w.w_op);
   1123   1.1       cgd 	}
   1124  1.10   mycroft 	splx(s);
   1125  1.29    chopps 
   1126  1.29    chopps 	/* check to see if we couldn't allocate memory with NOWAIT */
   1127  1.29    chopps 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   1128  1.29    chopps 		goto again;
   1129  1.29    chopps 
   1130  1.10   mycroft 	if (w.w_tmem)
   1131  1.10   mycroft 		free(w.w_tmem, M_RTABLE);
   1132   1.1       cgd 	w.w_needed += w.w_given;
   1133  1.10   mycroft 	if (where) {
   1134  1.17  christos 		*given = w.w_where - (caddr_t) where;
   1135  1.10   mycroft 		if (*given < w.w_needed)
   1136  1.10   mycroft 			return (ENOMEM);
   1137  1.10   mycroft 	} else {
   1138  1.10   mycroft 		*given = (11 * w.w_needed) / 10;
   1139  1.10   mycroft 	}
   1140   1.1       cgd 	return (error);
   1141   1.1       cgd }
   1142   1.1       cgd 
   1143   1.1       cgd /*
   1144   1.1       cgd  * Definitions of protocols supported in the ROUTE domain.
   1145   1.1       cgd  */
   1146   1.1       cgd 
   1147   1.1       cgd extern	struct domain routedomain;		/* or at least forward */
   1148   1.1       cgd 
   1149   1.1       cgd struct protosw routesw[] = {
   1150   1.1       cgd { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
   1151   1.1       cgd   raw_input,	route_output,	raw_ctlinput,	0,
   1152   1.1       cgd   route_usrreq,
   1153   1.1       cgd   raw_init,	0,		0,		0,
   1154  1.10   mycroft   sysctl_rtable,
   1155   1.1       cgd }
   1156   1.1       cgd };
   1157   1.1       cgd 
   1158   1.1       cgd struct domain routedomain =
   1159  1.10   mycroft     { PF_ROUTE, "route", route_init, 0, 0,
   1160   1.1       cgd       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
   1161