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