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