Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.132.2.1
      1  1.132.2.1    jruoho /*	$NetBSD: rtsock.c,v 1.132.2.1 2011/06/06 09:09:53 jruoho 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.75     perry  *
      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.75     perry  *
     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.64       agc  * 3. Neither the name of the University nor the names of its contributors
     45        1.1       cgd  *    may be used to endorse or promote products derived from this software
     46        1.1       cgd  *    without specific prior written permission.
     47        1.1       cgd  *
     48        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58        1.1       cgd  * SUCH DAMAGE.
     59        1.1       cgd  *
     60       1.26      fvdl  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
     61        1.1       cgd  */
     62       1.54     lukem 
     63       1.54     lukem #include <sys/cdefs.h>
     64  1.132.2.1    jruoho __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.132.2.1 2011/06/06 09:09:53 jruoho Exp $");
     65       1.31   thorpej 
     66  1.132.2.1    jruoho #ifdef _KERNEL_OPT
     67       1.31   thorpej #include "opt_inet.h"
     68      1.129    kefren #include "opt_mpls.h"
     69      1.120  christos #include "opt_compat_netbsd.h"
     70      1.120  christos #endif
     71        1.1       cgd 
     72        1.5   mycroft #include <sys/param.h>
     73        1.5   mycroft #include <sys/systm.h>
     74       1.10   mycroft #include <sys/proc.h>
     75        1.5   mycroft #include <sys/mbuf.h>
     76        1.5   mycroft #include <sys/socket.h>
     77        1.5   mycroft #include <sys/socketvar.h>
     78        1.5   mycroft #include <sys/domain.h>
     79        1.5   mycroft #include <sys/protosw.h>
     80       1.17  christos #include <sys/sysctl.h>
     81       1.84      elad #include <sys/kauth.h>
     82       1.99        ad #include <sys/intr.h>
     83       1.91    dyoung #ifdef RTSOCK_DEBUG
     84       1.91    dyoung #include <netinet/in.h>
     85       1.91    dyoung #endif /* RTSOCK_DEBUG */
     86       1.17  christos 
     87        1.5   mycroft #include <net/if.h>
     88        1.5   mycroft #include <net/route.h>
     89        1.5   mycroft #include <net/raw_cb.h>
     90        1.1       cgd 
     91      1.129    kefren #include <netmpls/mpls.h>
     92      1.129    kefren 
     93      1.120  christos #if defined(COMPAT_14) || defined(COMPAT_50)
     94      1.120  christos #include <compat/net/if.h>
     95  1.132.2.1    jruoho #include <compat/net/route.h>
     96  1.132.2.1    jruoho #endif
     97  1.132.2.1    jruoho #ifdef COMPAT_RTSOCK
     98  1.132.2.1    jruoho #define	RTM_XVERSION	RTM_OVERSION
     99  1.132.2.1    jruoho #define	RT_XADVANCE(a,b) RT_OADVANCE(a,b)
    100  1.132.2.1    jruoho #define	RT_XROUNDUP(n)	RT_OROUNDUP(n)
    101  1.132.2.1    jruoho #define	PF_XROUTE	PF_OROUTE
    102  1.132.2.1    jruoho #define	rt_xmsghdr	rt_msghdr50
    103  1.132.2.1    jruoho #define	if_xmsghdr	if_msghdr	/* if_msghdr50 is for RTM_OIFINFO */
    104  1.132.2.1    jruoho #define	ifa_xmsghdr	ifa_msghdr50
    105  1.132.2.1    jruoho #define	if_xannouncemsghdr	if_announcemsghdr50
    106  1.132.2.1    jruoho #define	COMPATNAME(x)	compat_50_ ## x
    107  1.132.2.1    jruoho #define	DOMAINNAME	"oroute"
    108  1.132.2.1    jruoho CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
    109  1.132.2.1    jruoho DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
    110  1.132.2.1    jruoho #else
    111  1.132.2.1    jruoho #define	RTM_XVERSION	RTM_VERSION
    112  1.132.2.1    jruoho #define	RT_XADVANCE(a,b) RT_ADVANCE(a,b)
    113  1.132.2.1    jruoho #define	RT_XROUNDUP(n)	RT_ROUNDUP(n)
    114  1.132.2.1    jruoho #define	PF_XROUTE	PF_ROUTE
    115  1.132.2.1    jruoho #define	rt_xmsghdr	rt_msghdr
    116  1.132.2.1    jruoho #define	if_xmsghdr	if_msghdr
    117  1.132.2.1    jruoho #define	ifa_xmsghdr	ifa_msghdr
    118  1.132.2.1    jruoho #define	if_xannouncemsghdr	if_announcemsghdr
    119  1.132.2.1    jruoho #define	COMPATNAME(x)	x
    120  1.132.2.1    jruoho #define	DOMAINNAME	"route"
    121  1.132.2.1    jruoho CTASSERT(sizeof(struct ifa_xmsghdr) == 24);
    122  1.132.2.1    jruoho #ifdef COMPAT_50
    123  1.132.2.1    jruoho #define	COMPATCALL(name, args)	compat_50_ ## name args
    124  1.132.2.1    jruoho #endif
    125  1.132.2.1    jruoho DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
    126  1.132.2.1    jruoho #undef COMPAT_50
    127  1.132.2.1    jruoho #undef COMPAT_14
    128  1.132.2.1    jruoho #endif
    129  1.132.2.1    jruoho 
    130  1.132.2.1    jruoho #ifndef COMPATCALL
    131  1.132.2.1    jruoho #define	COMPATCALL(name, args)	do { } while (/*CONSTCOND*/ 0)
    132      1.120  christos #endif
    133      1.120  christos 
    134       1.17  christos #include <machine/stdarg.h>
    135       1.17  christos 
    136  1.132.2.1    jruoho struct route_info COMPATNAME(route_info) = {
    137  1.132.2.1    jruoho 	.ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
    138  1.132.2.1    jruoho 	.ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
    139  1.132.2.1    jruoho 	.ri_maxqlen = IFQ_MAXLEN,
    140  1.132.2.1    jruoho };
    141       1.58      matt 
    142  1.132.2.1    jruoho #define	PRESERVED_RTF	(RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK)
    143       1.99        ad 
    144  1.132.2.1    jruoho static void COMPATNAME(route_init)(void);
    145  1.132.2.1    jruoho static int COMPATNAME(route_output)(struct mbuf *, ...);
    146  1.132.2.1    jruoho static int COMPATNAME(route_usrreq)(struct socket *,
    147  1.132.2.1    jruoho 	    int, struct mbuf *, struct mbuf *, struct mbuf *, struct lwp *);
    148       1.10   mycroft 
    149      1.120  christos static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
    150       1.72  christos static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
    151       1.78    dyoung static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
    152       1.78    dyoung     struct rt_addrinfo *);
    153  1.132.2.1    jruoho static void rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
    154  1.132.2.1    jruoho static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
    155      1.127     pooka static void sysctl_net_route_setup(struct sysctllog **);
    156       1.94    dyoung static int sysctl_dumpentry(struct rtentry *, void *);
    157      1.120  christos static int sysctl_iflist(int, struct rt_walkarg *, int);
    158       1.69      matt static int sysctl_rtable(SYSCTLFN_PROTO);
    159      1.123      yamt static void rt_adjustcount(int, int);
    160       1.10   mycroft 
    161      1.123      yamt static void
    162       1.69      matt rt_adjustcount(int af, int cnt)
    163       1.27  christos {
    164  1.132.2.1    jruoho 	struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
    165  1.132.2.1    jruoho 
    166  1.132.2.1    jruoho 	cb->any_count += cnt;
    167  1.132.2.1    jruoho 
    168       1.27  christos 	switch (af) {
    169       1.27  christos 	case AF_INET:
    170  1.132.2.1    jruoho 		cb->ip_count += cnt;
    171       1.27  christos 		return;
    172       1.30    itojun #ifdef INET6
    173       1.30    itojun 	case AF_INET6:
    174  1.132.2.1    jruoho 		cb->ip6_count += cnt;
    175       1.30    itojun 		return;
    176       1.30    itojun #endif
    177      1.129    kefren 	case AF_ISO:
    178  1.132.2.1    jruoho 		cb->iso_count += cnt;
    179      1.129    kefren 		return;
    180      1.129    kefren 	case AF_MPLS:
    181  1.132.2.1    jruoho 		cb->mpls_count += cnt;
    182       1.27  christos 		return;
    183       1.27  christos 	}
    184       1.27  christos }
    185      1.123      yamt 
    186        1.1       cgd /*ARGSUSED*/
    187        1.9   mycroft int
    188  1.132.2.1    jruoho COMPATNAME(route_usrreq)(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    189       1.79  christos 	struct mbuf *control, struct lwp *l)
    190        1.1       cgd {
    191       1.39  augustss 	int error = 0;
    192       1.39  augustss 	struct rawcb *rp = sotorawcb(so);
    193        1.1       cgd 	int s;
    194       1.10   mycroft 
    195        1.1       cgd 	if (req == PRU_ATTACH) {
    196      1.101        ad 		sosetlock(so);
    197      1.118    cegger 		rp = malloc(sizeof(*rp), M_PCB, M_WAITOK|M_ZERO);
    198      1.109      cube 		so->so_pcb = rp;
    199        1.1       cgd 	}
    200       1.27  christos 	if (req == PRU_DETACH && rp)
    201       1.27  christos 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    202       1.14   mycroft 	s = splsoftnet();
    203       1.23   thorpej 
    204       1.23   thorpej 	/*
    205       1.23   thorpej 	 * Don't call raw_usrreq() in the attach case, because
    206       1.23   thorpej 	 * we want to allow non-privileged processes to listen on
    207       1.23   thorpej 	 * and send "safe" commands to the routing socket.
    208       1.23   thorpej 	 */
    209       1.23   thorpej 	if (req == PRU_ATTACH) {
    210       1.95    dyoung 		if (l == NULL)
    211       1.23   thorpej 			error = EACCES;
    212       1.23   thorpej 		else
    213       1.23   thorpej 			error = raw_attach(so, (int)(long)nam);
    214       1.23   thorpej 	} else
    215       1.79  christos 		error = raw_usrreq(so, req, m, nam, control, l);
    216       1.23   thorpej 
    217        1.1       cgd 	rp = sotorawcb(so);
    218        1.1       cgd 	if (req == PRU_ATTACH && rp) {
    219        1.1       cgd 		if (error) {
    220      1.112    dyoung 			free(rp, M_PCB);
    221        1.1       cgd 			splx(s);
    222       1.95    dyoung 			return error;
    223        1.1       cgd 		}
    224       1.27  christos 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    225  1.132.2.1    jruoho 		rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
    226  1.132.2.1    jruoho 		rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
    227        1.1       cgd 		soisconnected(so);
    228        1.1       cgd 		so->so_options |= SO_USELOOPBACK;
    229        1.1       cgd 	}
    230        1.1       cgd 	splx(s);
    231       1.95    dyoung 	return error;
    232       1.95    dyoung }
    233       1.95    dyoung 
    234        1.1       cgd /*ARGSUSED*/
    235        1.9   mycroft int
    236  1.132.2.1    jruoho COMPATNAME(route_output)(struct mbuf *m, ...)
    237        1.1       cgd {
    238  1.132.2.1    jruoho 	struct sockproto proto = { .sp_family = PF_XROUTE, };
    239  1.132.2.1    jruoho 	struct rt_xmsghdr *rtm = NULL;
    240  1.132.2.1    jruoho 	struct rt_xmsghdr *old_rtm = NULL;
    241       1.95    dyoung 	struct rtentry *rt = NULL;
    242       1.95    dyoung 	struct rtentry *saved_nrt = NULL;
    243       1.10   mycroft 	struct rt_addrinfo info;
    244      1.124       roy 	int len, error = 0;
    245       1.95    dyoung 	struct ifnet *ifp = NULL;
    246      1.124       roy 	struct ifaddr *ifa = NULL;
    247       1.17  christos 	struct socket *so;
    248       1.17  christos 	va_list ap;
    249       1.55  christos 	sa_family_t family;
    250       1.17  christos 
    251       1.17  christos 	va_start(ap, m);
    252       1.17  christos 	so = va_arg(ap, struct socket *);
    253       1.17  christos 	va_end(ap);
    254       1.17  christos 
    255       1.56     perry #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
    256       1.95    dyoung 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
    257       1.95    dyoung 	   (m = m_pullup(m, sizeof(int32_t))) == NULL))
    258       1.95    dyoung 		return ENOBUFS;
    259        1.1       cgd 	if ((m->m_flags & M_PKTHDR) == 0)
    260  1.132.2.1    jruoho 		panic("%s", __func__);
    261        1.1       cgd 	len = m->m_pkthdr.len;
    262        1.1       cgd 	if (len < sizeof(*rtm) ||
    263  1.132.2.1    jruoho 	    len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
    264      1.114    dyoung 		info.rti_info[RTAX_DST] = NULL;
    265        1.1       cgd 		senderr(EINVAL);
    266       1.10   mycroft 	}
    267  1.132.2.1    jruoho 	R_Malloc(rtm, struct rt_xmsghdr *, len);
    268       1.95    dyoung 	if (rtm == NULL) {
    269      1.114    dyoung 		info.rti_info[RTAX_DST] = NULL;
    270        1.1       cgd 		senderr(ENOBUFS);
    271       1.10   mycroft 	}
    272      1.112    dyoung 	m_copydata(m, 0, len, rtm);
    273  1.132.2.1    jruoho 	if (rtm->rtm_version != RTM_XVERSION) {
    274      1.114    dyoung 		info.rti_info[RTAX_DST] = NULL;
    275        1.1       cgd 		senderr(EPROTONOSUPPORT);
    276       1.10   mycroft 	}
    277        1.1       cgd 	rtm->rtm_pid = curproc->p_pid;
    278       1.48   thorpej 	memset(&info, 0, sizeof(info));
    279       1.10   mycroft 	info.rti_addrs = rtm->rtm_addrs;
    280      1.112    dyoung 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
    281  1.132.2.1    jruoho 	    &info)) {
    282       1.42       erh 		senderr(EINVAL);
    283  1.132.2.1    jruoho 	}
    284       1.45    itojun 	info.rti_flags = rtm->rtm_flags;
    285       1.91    dyoung #ifdef RTSOCK_DEBUG
    286      1.114    dyoung 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    287      1.114    dyoung 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
    288      1.115  christos 		    inet_ntoa(((const struct sockaddr_in *)
    289      1.115  christos 		    info.rti_info[RTAX_DST])->sin_addr));
    290       1.91    dyoung 	}
    291       1.91    dyoung #endif /* RTSOCK_DEBUG */
    292      1.115  christos 	if (info.rti_info[RTAX_DST] == NULL ||
    293  1.132.2.1    jruoho 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
    294       1.26      fvdl 		senderr(EINVAL);
    295  1.132.2.1    jruoho 	}
    296      1.115  christos 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
    297  1.132.2.1    jruoho 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
    298        1.1       cgd 		senderr(EINVAL);
    299  1.132.2.1    jruoho 	}
    300       1.23   thorpej 
    301       1.23   thorpej 	/*
    302       1.23   thorpej 	 * Verify that the caller has the appropriate privilege; RTM_GET
    303       1.23   thorpej 	 * is the only operation the non-superuser is allowed.
    304       1.23   thorpej 	 */
    305       1.88      elad 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
    306       1.89      elad 	    0, rtm, NULL, NULL) != 0)
    307       1.23   thorpej 		senderr(EACCES);
    308       1.23   thorpej 
    309        1.1       cgd 	switch (rtm->rtm_type) {
    310       1.10   mycroft 
    311        1.1       cgd 	case RTM_ADD:
    312  1.132.2.1    jruoho 		if (info.rti_info[RTAX_GATEWAY] == NULL) {
    313        1.1       cgd 			senderr(EINVAL);
    314  1.132.2.1    jruoho 		}
    315       1.45    itojun 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    316        1.1       cgd 		if (error == 0 && saved_nrt) {
    317  1.132.2.1    jruoho 			rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
    318        1.1       cgd 			saved_nrt->rt_refcnt--;
    319        1.1       cgd 		}
    320        1.1       cgd 		break;
    321        1.1       cgd 
    322        1.1       cgd 	case RTM_DELETE:
    323       1.45    itojun 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    324       1.16       cgd 		if (error == 0) {
    325       1.16       cgd 			(rt = saved_nrt)->rt_refcnt++;
    326       1.16       cgd 			goto report;
    327       1.16       cgd 		}
    328        1.1       cgd 		break;
    329        1.1       cgd 
    330        1.1       cgd 	case RTM_GET:
    331        1.1       cgd 	case RTM_CHANGE:
    332        1.1       cgd 	case RTM_LOCK:
    333      1.115  christos                 /* XXX This will mask info.rti_info[RTAX_DST] with
    334      1.115  christos 		 * info.rti_info[RTAX_NETMASK] before
    335       1.95    dyoung                  * searching.  It did not used to do that.  --dyoung
    336       1.95    dyoung 		 */
    337      1.103    dyoung 		error = rtrequest1(RTM_GET, &info, &rt);
    338       1.95    dyoung 		if (error != 0)
    339       1.95    dyoung 			senderr(error);
    340       1.61    itojun 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
    341      1.115  christos 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
    342      1.115  christos 			    info.rti_info[RTAX_DST]->sa_len) != 0)
    343       1.61    itojun 				senderr(ESRCH);
    344  1.132.2.1    jruoho 			if (info.rti_info[RTAX_NETMASK] == NULL &&
    345  1.132.2.1    jruoho 			    rt_mask(rt) != NULL)
    346       1.61    itojun 				senderr(ETOOMANYREFS);
    347       1.61    itojun 		}
    348       1.37    itojun 
    349       1.59    itojun 		switch (rtm->rtm_type) {
    350        1.1       cgd 		case RTM_GET:
    351       1.16       cgd 		report:
    352      1.114    dyoung 			info.rti_info[RTAX_DST] = rt_getkey(rt);
    353      1.114    dyoung 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    354      1.114    dyoung 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    355      1.129    kefren 			info.rti_info[RTAX_TAG] = (struct sockaddr*)rt_gettag(rt);
    356       1.91    dyoung 			if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
    357       1.91    dyoung 				;
    358       1.91    dyoung 			else if ((ifp = rt->rt_ifp) != NULL) {
    359       1.91    dyoung 				const struct ifaddr *rtifa;
    360      1.114    dyoung 				info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    361       1.91    dyoung                                 /* rtifa used to be simply rt->rt_ifa.
    362       1.91    dyoung                                  * If rt->rt_ifa != NULL, then
    363       1.91    dyoung                                  * rt_get_ifa() != NULL.  So this
    364       1.91    dyoung                                  * ought to still be safe. --dyoung
    365       1.91    dyoung 				 */
    366       1.91    dyoung 				rtifa = rt_get_ifa(rt);
    367      1.114    dyoung 				info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
    368       1.91    dyoung #ifdef RTSOCK_DEBUG
    369      1.115  christos 				if (info.rti_info[RTAX_IFA]->sa_family ==
    370      1.115  christos 				    AF_INET) {
    371       1.91    dyoung 					printf("%s: copying out RTAX_IFA %s ",
    372      1.115  christos 					    __func__, inet_ntoa(
    373      1.126   tsutsui 					    ((const struct sockaddr_in *)
    374      1.126   tsutsui 					    info.rti_info[RTAX_IFA])->sin_addr)
    375      1.126   tsutsui 					    );
    376      1.115  christos 					printf("for info.rti_info[RTAX_DST] %s "
    377      1.115  christos 					    "ifa_getifa %p ifa_seqno %p\n",
    378      1.115  christos 					    inet_ntoa(
    379      1.126   tsutsui 					    ((const struct sockaddr_in *)
    380      1.115  christos 					    info.rti_info[RTAX_DST])->sin_addr),
    381      1.115  christos 					    (void *)rtifa->ifa_getifa,
    382      1.115  christos 					    rtifa->ifa_seqno);
    383       1.37    itojun 				}
    384       1.91    dyoung #endif /* RTSOCK_DEBUG */
    385      1.115  christos 				if (ifp->if_flags & IFF_POINTOPOINT) {
    386      1.115  christos 					info.rti_info[RTAX_BRD] =
    387      1.115  christos 					    rtifa->ifa_dstaddr;
    388      1.115  christos 				} else
    389      1.114    dyoung 					info.rti_info[RTAX_BRD] = NULL;
    390       1.91    dyoung 				rtm->rtm_index = ifp->if_index;
    391       1.91    dyoung 			} else {
    392      1.114    dyoung 				info.rti_info[RTAX_IFP] = NULL;
    393      1.114    dyoung 				info.rti_info[RTAX_IFA] = NULL;
    394        1.1       cgd 			}
    395       1.95    dyoung 			(void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
    396        1.1       cgd 			if (len > rtm->rtm_msglen) {
    397      1.117  christos 				old_rtm = rtm;
    398  1.132.2.1    jruoho 				R_Malloc(rtm, struct rt_xmsghdr *, len);
    399      1.117  christos 				if (rtm == NULL)
    400        1.1       cgd 					senderr(ENOBUFS);
    401      1.117  christos 				(void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
    402        1.1       cgd 			}
    403      1.111  christos 			(void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
    404        1.1       cgd 			rtm->rtm_flags = rt->rt_flags;
    405  1.132.2.1    jruoho 			rtm_setmetrics(rt, rtm);
    406       1.10   mycroft 			rtm->rtm_addrs = info.rti_addrs;
    407        1.1       cgd 			break;
    408        1.1       cgd 
    409        1.1       cgd 		case RTM_CHANGE:
    410       1.45    itojun 			/*
    411       1.45    itojun 			 * new gateway could require new ifaddr, ifp;
    412       1.45    itojun 			 * flags may also be different; ifp may be specified
    413       1.45    itojun 			 * by ll sockaddr when protocol address is ambiguous
    414       1.45    itojun 			 */
    415       1.45    itojun 			if ((error = rt_getifa(&info)) != 0)
    416       1.45    itojun 				senderr(error);
    417      1.115  christos 			if (info.rti_info[RTAX_GATEWAY] &&
    418      1.115  christos 			    rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
    419        1.1       cgd 				senderr(EDQUOT);
    420      1.129    kefren 			if (info.rti_info[RTAX_TAG])
    421      1.129    kefren 				rt_settag(rt, info.rti_info[RTAX_TAG]);
    422       1.35    itojun 			/* new gateway could require new ifaddr, ifp;
    423       1.35    itojun 			   flags may also be different; ifp may be specified
    424       1.35    itojun 			   by ll sockaddr when protocol address is ambiguous */
    425      1.115  christos 			if (info.rti_info[RTAX_IFP] &&
    426      1.115  christos 			    (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
    427      1.115  christos 			    (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
    428      1.115  christos 			    info.rti_info[RTAX_GATEWAY])) {
    429      1.128    kefren 				if (info.rti_info[RTAX_IFA] == NULL ||
    430      1.128    kefren 				    (ifa = ifa_ifwithaddr(
    431      1.128    kefren 				    info.rti_info[RTAX_IFA])) == NULL)
    432      1.128    kefren 					ifa = ifaof_ifpforaddr(
    433      1.128    kefren 					    info.rti_info[RTAX_IFA] ?
    434      1.128    kefren 					    info.rti_info[RTAX_IFA] :
    435      1.128    kefren 					    info.rti_info[RTAX_GATEWAY], ifp);
    436      1.115  christos 			} else if ((info.rti_info[RTAX_IFA] &&
    437      1.115  christos 			    (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
    438      1.115  christos 			    (info.rti_info[RTAX_GATEWAY] &&
    439      1.115  christos 			    (ifa = ifa_ifwithroute(rt->rt_flags,
    440      1.115  christos 			    rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
    441       1.35    itojun 				ifp = ifa->ifa_ifp;
    442      1.115  christos 			}
    443       1.35    itojun 			if (ifa) {
    444      1.124       roy 				struct ifaddr *oifa = rt->rt_ifa;
    445       1.35    itojun 				if (oifa != ifa) {
    446       1.90    dyoung 					if (oifa && oifa->ifa_rtrequest) {
    447       1.90    dyoung 						oifa->ifa_rtrequest(RTM_DELETE,
    448       1.90    dyoung 						    rt, &info);
    449       1.90    dyoung 					}
    450       1.90    dyoung 					rt_replace_ifa(rt, ifa);
    451       1.90    dyoung 					rt->rt_ifp = ifp;
    452       1.35    itojun 				}
    453       1.35    itojun 			}
    454      1.130    kefren 			if (ifp && rt->rt_ifp != ifp)
    455      1.130    kefren 				rt->rt_ifp = ifp;
    456  1.132.2.1    jruoho 			rt_setmetrics(rtm->rtm_inits, rtm, rt);
    457  1.132.2.1    jruoho 			if (rt->rt_flags != info.rti_flags)
    458  1.132.2.1    jruoho 				rt->rt_flags = (info.rti_flags & ~PRESERVED_RTF)
    459  1.132.2.1    jruoho 				    | (rt->rt_flags & PRESERVED_RTF);
    460       1.35    itojun 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    461       1.45    itojun 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
    462      1.115  christos 			/*FALLTHROUGH*/
    463        1.1       cgd 		case RTM_LOCK:
    464       1.10   mycroft 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
    465        1.1       cgd 			rt->rt_rmx.rmx_locks |=
    466       1.21  christos 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
    467        1.1       cgd 			break;
    468        1.1       cgd 		}
    469       1.10   mycroft 		break;
    470        1.1       cgd 
    471        1.1       cgd 	default:
    472        1.1       cgd 		senderr(EOPNOTSUPP);
    473        1.1       cgd 	}
    474        1.1       cgd 
    475        1.1       cgd flush:
    476        1.1       cgd 	if (rtm) {
    477        1.1       cgd 		if (error)
    478        1.1       cgd 			rtm->rtm_errno = error;
    479       1.75     perry 		else
    480        1.1       cgd 			rtm->rtm_flags |= RTF_DONE;
    481        1.1       cgd 	}
    482      1.115  christos 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
    483      1.115  christos 	    0;
    484      1.117  christos 	/* We cannot free old_rtm until we have stopped using the
    485      1.117  christos 	 * pointers in info, some of which may point to sockaddrs
    486      1.117  christos 	 * in old_rtm.
    487      1.117  christos 	 */
    488      1.117  christos 	if (old_rtm != NULL)
    489      1.117  christos 		Free(old_rtm);
    490        1.1       cgd 	if (rt)
    491        1.1       cgd 		rtfree(rt);
    492        1.1       cgd     {
    493       1.95    dyoung 	struct rawcb *rp = NULL;
    494        1.1       cgd 	/*
    495        1.1       cgd 	 * Check to see if we don't want our own messages.
    496        1.1       cgd 	 */
    497        1.1       cgd 	if ((so->so_options & SO_USELOOPBACK) == 0) {
    498  1.132.2.1    jruoho 		if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
    499        1.1       cgd 			if (rtm)
    500        1.1       cgd 				Free(rtm);
    501        1.1       cgd 			m_freem(m);
    502       1.95    dyoung 			return error;
    503        1.1       cgd 		}
    504        1.1       cgd 		/* There is another listener, so construct message */
    505        1.1       cgd 		rp = sotorawcb(so);
    506        1.1       cgd 	}
    507        1.1       cgd 	if (rtm) {
    508      1.112    dyoung 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
    509       1.47    itojun 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
    510       1.46    itojun 			m_freem(m);
    511       1.46    itojun 			m = NULL;
    512       1.47    itojun 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
    513       1.46    itojun 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
    514        1.1       cgd 		Free(rtm);
    515        1.1       cgd 	}
    516        1.1       cgd 	if (rp)
    517        1.1       cgd 		rp->rcb_proto.sp_family = 0; /* Avoid us */
    518       1.55  christos 	if (family)
    519       1.99        ad 		proto.sp_protocol = family;
    520       1.46    itojun 	if (m)
    521  1.132.2.1    jruoho 		raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
    522  1.132.2.1    jruoho 		    &COMPATNAME(route_info).ri_dst);
    523        1.1       cgd 	if (rp)
    524  1.132.2.1    jruoho 		rp->rcb_proto.sp_family = PF_XROUTE;
    525        1.1       cgd     }
    526       1.95    dyoung 	return error;
    527        1.1       cgd }
    528        1.1       cgd 
    529  1.132.2.1    jruoho static void
    530  1.132.2.1    jruoho rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
    531        1.1       cgd {
    532  1.132.2.1    jruoho #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
    533        1.1       cgd 	metric(RTV_RPIPE, rmx_recvpipe);
    534        1.1       cgd 	metric(RTV_SPIPE, rmx_sendpipe);
    535        1.1       cgd 	metric(RTV_SSTHRESH, rmx_ssthresh);
    536        1.1       cgd 	metric(RTV_RTT, rmx_rtt);
    537        1.1       cgd 	metric(RTV_RTTVAR, rmx_rttvar);
    538        1.1       cgd 	metric(RTV_HOPCOUNT, rmx_hopcount);
    539        1.1       cgd 	metric(RTV_MTU, rmx_mtu);
    540        1.1       cgd 	metric(RTV_EXPIRE, rmx_expire);
    541        1.1       cgd #undef metric
    542        1.1       cgd }
    543        1.1       cgd 
    544  1.132.2.1    jruoho static void
    545  1.132.2.1    jruoho rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
    546  1.132.2.1    jruoho {
    547  1.132.2.1    jruoho #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
    548  1.132.2.1    jruoho 	metric(rmx_recvpipe);
    549  1.132.2.1    jruoho 	metric(rmx_sendpipe);
    550  1.132.2.1    jruoho 	metric(rmx_ssthresh);
    551  1.132.2.1    jruoho 	metric(rmx_rtt);
    552  1.132.2.1    jruoho 	metric(rmx_rttvar);
    553  1.132.2.1    jruoho 	metric(rmx_hopcount);
    554  1.132.2.1    jruoho 	metric(rmx_mtu);
    555  1.132.2.1    jruoho 	metric(rmx_expire);
    556  1.132.2.1    jruoho #undef metric
    557  1.132.2.1    jruoho }
    558  1.132.2.1    jruoho 
    559       1.42       erh static int
    560      1.115  christos rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
    561      1.115  christos     struct rt_addrinfo *rtinfo)
    562       1.10   mycroft {
    563       1.69      matt 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
    564       1.39  augustss 	int i;
    565       1.10   mycroft 
    566      1.112    dyoung 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
    567       1.10   mycroft 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
    568       1.10   mycroft 			continue;
    569      1.117  christos 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
    570  1.132.2.1    jruoho 		RT_XADVANCE(cp, sa);
    571       1.10   mycroft 	}
    572       1.44     enami 
    573      1.115  christos 	/*
    574      1.115  christos 	 * Check for extra addresses specified, except RTM_GET asking
    575      1.115  christos 	 * for interface info.
    576      1.115  christos 	 */
    577       1.72  christos 	if (rtmtype == RTM_GET) {
    578      1.115  christos 		if (((rtinfo->rti_addrs &
    579      1.115  christos 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
    580       1.95    dyoung 			return 1;
    581      1.114    dyoung 	} else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
    582      1.114    dyoung 		return 1;
    583       1.44     enami 	/* Check for bad data length.  */
    584       1.44     enami 	if (cp != cplim) {
    585      1.112    dyoung 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
    586  1.132.2.1    jruoho 		    cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
    587       1.44     enami 			/*
    588      1.114    dyoung 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
    589       1.44     enami 			 * We accept this for now for the sake of old
    590       1.44     enami 			 * binaries or third party softwares.
    591       1.44     enami 			 */
    592       1.44     enami 			;
    593       1.44     enami 		else
    594       1.95    dyoung 			return 1;
    595       1.44     enami 	}
    596       1.95    dyoung 	return 0;
    597        1.1       cgd }
    598        1.1       cgd 
    599      1.132  christos static int
    600      1.132  christos rt_getlen(int type)
    601        1.1       cgd {
    602  1.132.2.1    jruoho #ifndef COMPAT_RTSOCK
    603  1.132.2.1    jruoho 	CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
    604  1.132.2.1    jruoho 	CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
    605  1.132.2.1    jruoho 	CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
    606  1.132.2.1    jruoho 	CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
    607  1.132.2.1    jruoho #endif
    608  1.132.2.1    jruoho 
    609       1.10   mycroft 	switch (type) {
    610       1.10   mycroft 	case RTM_DELADDR:
    611       1.10   mycroft 	case RTM_NEWADDR:
    612      1.131       roy 	case RTM_CHGADDR:
    613  1.132.2.1    jruoho 		return sizeof(struct ifa_xmsghdr);
    614       1.10   mycroft 
    615      1.132  christos 	case RTM_OOIFINFO:
    616       1.32    bouyer #ifdef COMPAT_14
    617      1.132  christos 		return sizeof(struct if_msghdr14);
    618      1.132  christos #else
    619      1.132  christos #ifdef DIAGNOSTIC
    620      1.132  christos 		printf("RTM_OOIFINFO\n");
    621      1.132  christos #endif
    622      1.132  christos 		return -1;
    623      1.120  christos #endif
    624      1.132  christos 	case RTM_OIFINFO:
    625      1.120  christos #ifdef COMPAT_50
    626      1.132  christos 		return sizeof(struct if_msghdr50);
    627      1.132  christos #else
    628      1.132  christos #ifdef DIAGNOSTIC
    629      1.132  christos 		printf("RTM_OIFINFO\n");
    630      1.132  christos #endif
    631      1.132  christos 		return -1;
    632       1.32    bouyer #endif
    633       1.32    bouyer 
    634       1.10   mycroft 	case RTM_IFINFO:
    635  1.132.2.1    jruoho 		return sizeof(struct if_xmsghdr);
    636       1.10   mycroft 
    637       1.36   thorpej 	case RTM_IFANNOUNCE:
    638       1.78    dyoung 	case RTM_IEEE80211:
    639  1.132.2.1    jruoho 		return sizeof(struct if_xannouncemsghdr);
    640       1.36   thorpej 
    641       1.10   mycroft 	default:
    642  1.132.2.1    jruoho 		return sizeof(struct rt_xmsghdr);
    643       1.46    itojun 	}
    644      1.132  christos }
    645      1.132  christos 
    646      1.132  christos 
    647      1.132  christos struct mbuf *
    648  1.132.2.1    jruoho COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
    649      1.132  christos {
    650  1.132.2.1    jruoho 	struct rt_xmsghdr *rtm;
    651      1.132  christos 	struct mbuf *m;
    652      1.132  christos 	int i;
    653      1.132  christos 	const struct sockaddr *sa;
    654      1.132  christos 	int len, dlen;
    655      1.132  christos 
    656      1.132  christos 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    657      1.132  christos 	if (m == NULL)
    658      1.132  christos 		return m;
    659  1.132.2.1    jruoho 	MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
    660      1.132  christos 
    661      1.132  christos 	if ((len = rt_getlen(type)) == -1)
    662      1.132  christos 		goto out;
    663       1.47    itojun 	if (len > MHLEN + MLEN)
    664  1.132.2.1    jruoho 		panic("%s: message too long", __func__);
    665       1.47    itojun 	else if (len > MHLEN) {
    666       1.32    bouyer 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
    667      1.132  christos 		if (m->m_next == NULL)
    668      1.132  christos 			goto out;
    669       1.58      matt 		MCLAIM(m->m_next, m->m_owner);
    670       1.47    itojun 		m->m_pkthdr.len = len;
    671       1.47    itojun 		m->m_len = MHLEN;
    672       1.47    itojun 		m->m_next->m_len = len - MHLEN;
    673       1.47    itojun 	} else {
    674       1.47    itojun 		m->m_pkthdr.len = m->m_len = len;
    675       1.32    bouyer 	}
    676       1.95    dyoung 	m->m_pkthdr.rcvif = NULL;
    677       1.32    bouyer 	m_copyback(m, 0, datalen, data);
    678      1.107  christos 	if (len > datalen)
    679      1.107  christos 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
    680  1.132.2.1    jruoho 	rtm = mtod(m, struct rt_xmsghdr *);
    681       1.10   mycroft 	for (i = 0; i < RTAX_MAX; i++) {
    682       1.10   mycroft 		if ((sa = rtinfo->rti_info[i]) == NULL)
    683       1.10   mycroft 			continue;
    684       1.10   mycroft 		rtinfo->rti_addrs |= (1 << i);
    685  1.132.2.1    jruoho 		dlen = RT_XROUNDUP(sa->sa_len);
    686  1.132.2.1    jruoho 		m_copyback(m, len, sa->sa_len, sa);
    687  1.132.2.1    jruoho 		if (dlen != sa->sa_len) {
    688  1.132.2.1    jruoho 			m_copyback(m, len + sa->sa_len,
    689  1.132.2.1    jruoho 			    dlen - sa->sa_len, "\0\0\0\0\0\0");
    690  1.132.2.1    jruoho 		}
    691       1.10   mycroft 		len += dlen;
    692       1.47    itojun 	}
    693      1.132  christos 	if (m->m_pkthdr.len != len)
    694      1.132  christos 		goto out;
    695        1.1       cgd 	rtm->rtm_msglen = len;
    696  1.132.2.1    jruoho 	rtm->rtm_version = RTM_XVERSION;
    697        1.1       cgd 	rtm->rtm_type = type;
    698       1.95    dyoung 	return m;
    699      1.132  christos out:
    700      1.132  christos 	m_freem(m);
    701      1.132  christos 	return NULL;
    702       1.10   mycroft }
    703       1.10   mycroft 
    704       1.29    chopps /*
    705       1.29    chopps  * rt_msg2
    706       1.29    chopps  *
    707       1.29    chopps  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
    708       1.29    chopps  *		returns the length of the message in 'lenp'.
    709       1.29    chopps  *
    710       1.29    chopps  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
    711       1.29    chopps  *	the message
    712       1.29    chopps  * otherwise walkarg's w_needed is updated and if the user buffer is
    713       1.29    chopps  *	specified and w_needed indicates space exists the information is copied
    714       1.29    chopps  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
    715       1.29    chopps  *	if the allocation fails ENOBUFS is returned.
    716       1.29    chopps  */
    717       1.10   mycroft static int
    718      1.120  christos rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
    719       1.69      matt 	int *lenp)
    720       1.10   mycroft {
    721       1.39  augustss 	int i;
    722       1.10   mycroft 	int len, dlen, second_time = 0;
    723       1.93  christos 	char *cp0, *cp = cpv;
    724       1.10   mycroft 
    725       1.10   mycroft 	rtinfo->rti_addrs = 0;
    726       1.10   mycroft again:
    727      1.132  christos 	if ((len = rt_getlen(type)) == -1)
    728      1.132  christos 		return EINVAL;
    729       1.10   mycroft 
    730       1.17  christos 	if ((cp0 = cp) != NULL)
    731       1.10   mycroft 		cp += len;
    732       1.10   mycroft 	for (i = 0; i < RTAX_MAX; i++) {
    733       1.68      matt 		const struct sockaddr *sa;
    734       1.10   mycroft 
    735       1.95    dyoung 		if ((sa = rtinfo->rti_info[i]) == NULL)
    736       1.10   mycroft 			continue;
    737       1.10   mycroft 		rtinfo->rti_addrs |= (1 << i);
    738  1.132.2.1    jruoho 		dlen = RT_XROUNDUP(sa->sa_len);
    739       1.10   mycroft 		if (cp) {
    740      1.111  christos 			(void)memcpy(cp, sa, (size_t)dlen);
    741       1.10   mycroft 			cp += dlen;
    742       1.10   mycroft 		}
    743        1.1       cgd 		len += dlen;
    744        1.1       cgd 	}
    745       1.95    dyoung 	if (cp == NULL && w != NULL && !second_time) {
    746      1.120  christos 		struct rt_walkarg *rw = w;
    747       1.10   mycroft 
    748       1.10   mycroft 		rw->w_needed += len;
    749       1.10   mycroft 		if (rw->w_needed <= 0 && rw->w_where) {
    750       1.10   mycroft 			if (rw->w_tmemsize < len) {
    751       1.10   mycroft 				if (rw->w_tmem)
    752       1.10   mycroft 					free(rw->w_tmem, M_RTABLE);
    753      1.111  christos 				rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
    754       1.17  christos 				if (rw->w_tmem)
    755       1.10   mycroft 					rw->w_tmemsize = len;
    756      1.111  christos 				else
    757      1.111  christos 					rw->w_tmemsize = 0;
    758       1.10   mycroft 			}
    759       1.10   mycroft 			if (rw->w_tmem) {
    760       1.10   mycroft 				cp = rw->w_tmem;
    761       1.10   mycroft 				second_time = 1;
    762       1.10   mycroft 				goto again;
    763       1.29    chopps 			} else {
    764       1.29    chopps 				rw->w_tmemneeded = len;
    765       1.95    dyoung 				return ENOBUFS;
    766       1.29    chopps 			}
    767       1.10   mycroft 		}
    768        1.1       cgd 	}
    769       1.10   mycroft 	if (cp) {
    770  1.132.2.1    jruoho 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
    771       1.10   mycroft 
    772  1.132.2.1    jruoho 		rtm->rtm_version = RTM_XVERSION;
    773       1.10   mycroft 		rtm->rtm_type = type;
    774       1.10   mycroft 		rtm->rtm_msglen = len;
    775        1.1       cgd 	}
    776       1.29    chopps 	if (lenp)
    777       1.29    chopps 		*lenp = len;
    778       1.95    dyoung 	return 0;
    779       1.10   mycroft }
    780       1.10   mycroft 
    781       1.10   mycroft /*
    782       1.10   mycroft  * This routine is called to generate a message from the routing
    783       1.51       wiz  * socket indicating that a redirect has occurred, a routing lookup
    784       1.10   mycroft  * has failed, or that a protocol has detected timeouts to a particular
    785       1.10   mycroft  * destination.
    786       1.10   mycroft  */
    787       1.10   mycroft void
    788  1.132.2.1    jruoho COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
    789  1.132.2.1    jruoho     int error)
    790       1.10   mycroft {
    791  1.132.2.1    jruoho 	struct rt_xmsghdr rtm;
    792       1.39  augustss 	struct mbuf *m;
    793       1.68      matt 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
    794  1.132.2.1    jruoho 	struct rt_addrinfo info = *rtinfo;
    795       1.10   mycroft 
    796  1.132.2.1    jruoho 	COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
    797  1.132.2.1    jruoho 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    798       1.10   mycroft 		return;
    799       1.48   thorpej 	memset(&rtm, 0, sizeof(rtm));
    800       1.32    bouyer 	rtm.rtm_flags = RTF_DONE | flags;
    801       1.32    bouyer 	rtm.rtm_errno = error;
    802  1.132.2.1    jruoho 	m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
    803       1.95    dyoung 	if (m == NULL)
    804        1.1       cgd 		return;
    805  1.132.2.1    jruoho 	mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
    806  1.132.2.1    jruoho 	COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
    807       1.10   mycroft }
    808       1.10   mycroft 
    809       1.10   mycroft /*
    810       1.10   mycroft  * This routine is called to generate a message from the routing
    811       1.10   mycroft  * socket indicating that the status of a network interface has changed.
    812       1.10   mycroft  */
    813       1.10   mycroft void
    814  1.132.2.1    jruoho COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
    815       1.10   mycroft {
    816  1.132.2.1    jruoho 	struct if_xmsghdr ifm;
    817       1.10   mycroft 	struct mbuf *m;
    818       1.10   mycroft 	struct rt_addrinfo info;
    819       1.10   mycroft 
    820  1.132.2.1    jruoho 	COMPATCALL(rt_ifmsg, (ifp));
    821  1.132.2.1    jruoho 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    822       1.10   mycroft 		return;
    823      1.120  christos 	(void)memset(&info, 0, sizeof(info));
    824      1.120  christos 	(void)memset(&ifm, 0, sizeof(ifm));
    825       1.32    bouyer 	ifm.ifm_index = ifp->if_index;
    826       1.32    bouyer 	ifm.ifm_flags = ifp->if_flags;
    827       1.32    bouyer 	ifm.ifm_data = ifp->if_data;
    828       1.32    bouyer 	ifm.ifm_addrs = 0;
    829  1.132.2.1    jruoho 	m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
    830       1.95    dyoung 	if (m == NULL)
    831       1.32    bouyer 		return;
    832  1.132.2.1    jruoho 	COMPATNAME(route_enqueue)(m, 0);
    833       1.32    bouyer #ifdef COMPAT_14
    834  1.132.2.1    jruoho 	compat_14_rt_oifmsg(ifp);
    835      1.120  christos #endif
    836      1.120  christos #ifdef COMPAT_50
    837  1.132.2.1    jruoho 	compat_50_rt_oifmsg(ifp);
    838       1.32    bouyer #endif
    839        1.1       cgd }
    840        1.1       cgd 
    841      1.120  christos 
    842        1.1       cgd /*
    843       1.10   mycroft  * This is called to generate messages from the routing socket
    844       1.10   mycroft  * indicating a network interface has had addresses associated with it.
    845       1.10   mycroft  * if we ever reverse the logic and replace messages TO the routing
    846       1.10   mycroft  * socket indicate a request to configure interfaces, then it will
    847       1.10   mycroft  * be unnecessary as the routing socket will automatically generate
    848       1.10   mycroft  * copies of it.
    849       1.10   mycroft  */
    850       1.10   mycroft void
    851  1.132.2.1    jruoho COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
    852  1.132.2.1    jruoho     struct rtentry *rt)
    853       1.10   mycroft {
    854      1.116    dyoung #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
    855       1.10   mycroft 	struct rt_addrinfo info;
    856      1.116    dyoung 	const struct sockaddr *sa;
    857       1.10   mycroft 	int pass;
    858      1.116    dyoung 	struct mbuf *m;
    859       1.10   mycroft 	struct ifnet *ifp = ifa->ifa_ifp;
    860  1.132.2.1    jruoho 	struct rt_xmsghdr rtm;
    861  1.132.2.1    jruoho 	struct ifa_xmsghdr ifam;
    862      1.116    dyoung 	int ncmd;
    863       1.10   mycroft 
    864  1.132.2.1    jruoho 	COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
    865  1.132.2.1    jruoho 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    866       1.10   mycroft 		return;
    867       1.10   mycroft 	for (pass = 1; pass < 3; pass++) {
    868       1.48   thorpej 		memset(&info, 0, sizeof(info));
    869      1.116    dyoung 		switch (cmdpass(cmd, pass)) {
    870      1.116    dyoung 		case cmdpass(RTM_ADD, 1):
    871      1.116    dyoung 		case cmdpass(RTM_CHANGE, 1):
    872      1.116    dyoung 		case cmdpass(RTM_DELETE, 2):
    873      1.131       roy 			switch (cmd) {
    874      1.131       roy 			case RTM_DELETE:
    875      1.131       roy 				ncmd = RTM_DELADDR;
    876      1.131       roy 				break;
    877      1.131       roy 			case RTM_CHANGE:
    878      1.131       roy 				ncmd = RTM_CHGADDR;
    879      1.131       roy 				break;
    880      1.131       roy 			default:
    881      1.116    dyoung 				ncmd = RTM_NEWADDR;
    882      1.131       roy 			}
    883      1.114    dyoung 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
    884      1.114    dyoung 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    885      1.114    dyoung 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
    886      1.114    dyoung 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
    887       1.48   thorpej 			memset(&ifam, 0, sizeof(ifam));
    888       1.32    bouyer 			ifam.ifam_index = ifp->if_index;
    889       1.32    bouyer 			ifam.ifam_metric = ifa->ifa_metric;
    890       1.32    bouyer 			ifam.ifam_flags = ifa->ifa_flags;
    891  1.132.2.1    jruoho 			m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
    892       1.32    bouyer 			if (m == NULL)
    893       1.10   mycroft 				continue;
    894  1.132.2.1    jruoho 			mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
    895       1.32    bouyer 			    info.rti_addrs;
    896      1.116    dyoung 			break;
    897      1.116    dyoung 		case cmdpass(RTM_ADD, 2):
    898      1.116    dyoung 		case cmdpass(RTM_CHANGE, 2):
    899      1.116    dyoung 		case cmdpass(RTM_DELETE, 1):
    900       1.95    dyoung 			if (rt == NULL)
    901       1.10   mycroft 				continue;
    902      1.114    dyoung 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    903      1.114    dyoung 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
    904      1.114    dyoung 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    905       1.48   thorpej 			memset(&rtm, 0, sizeof(rtm));
    906       1.32    bouyer 			rtm.rtm_index = ifp->if_index;
    907       1.32    bouyer 			rtm.rtm_flags |= rt->rt_flags;
    908       1.32    bouyer 			rtm.rtm_errno = error;
    909  1.132.2.1    jruoho 			m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
    910       1.32    bouyer 			if (m == NULL)
    911       1.10   mycroft 				continue;
    912  1.132.2.1    jruoho 			mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
    913      1.116    dyoung 			break;
    914      1.116    dyoung 		default:
    915      1.116    dyoung 			continue;
    916       1.10   mycroft 		}
    917      1.104  christos #ifdef DIAGNOSTIC
    918      1.104  christos 		if (m == NULL)
    919      1.105  dholland 			panic("%s: called with wrong command", __func__);
    920      1.104  christos #endif
    921  1.132.2.1    jruoho 		COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
    922       1.10   mycroft 	}
    923      1.116    dyoung #undef cmdpass
    924       1.36   thorpej }
    925       1.36   thorpej 
    926       1.78    dyoung static struct mbuf *
    927       1.78    dyoung rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
    928       1.78    dyoung     struct rt_addrinfo *info)
    929       1.78    dyoung {
    930  1.132.2.1    jruoho 	struct if_xannouncemsghdr ifan;
    931       1.78    dyoung 
    932       1.78    dyoung 	memset(info, 0, sizeof(*info));
    933       1.78    dyoung 	memset(&ifan, 0, sizeof(ifan));
    934       1.78    dyoung 	ifan.ifan_index = ifp->if_index;
    935       1.78    dyoung 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
    936       1.78    dyoung 	ifan.ifan_what = what;
    937  1.132.2.1    jruoho 	return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
    938       1.78    dyoung }
    939       1.78    dyoung 
    940       1.36   thorpej /*
    941       1.36   thorpej  * This is called to generate routing socket messages indicating
    942       1.36   thorpej  * network interface arrival and departure.
    943       1.36   thorpej  */
    944       1.36   thorpej void
    945  1.132.2.1    jruoho COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
    946       1.36   thorpej {
    947       1.36   thorpej 	struct mbuf *m;
    948       1.36   thorpej 	struct rt_addrinfo info;
    949       1.36   thorpej 
    950  1.132.2.1    jruoho 	COMPATCALL(rt_ifannouncemsg, (ifp, what));
    951  1.132.2.1    jruoho 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    952       1.36   thorpej 		return;
    953       1.78    dyoung 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
    954       1.78    dyoung 	if (m == NULL)
    955       1.78    dyoung 		return;
    956  1.132.2.1    jruoho 	COMPATNAME(route_enqueue)(m, 0);
    957       1.78    dyoung }
    958       1.78    dyoung 
    959       1.78    dyoung /*
    960       1.78    dyoung  * This is called to generate routing socket messages indicating
    961       1.78    dyoung  * IEEE80211 wireless events.
    962       1.78    dyoung  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
    963       1.78    dyoung  */
    964       1.78    dyoung void
    965  1.132.2.1    jruoho COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
    966  1.132.2.1    jruoho 	size_t data_len)
    967       1.78    dyoung {
    968       1.78    dyoung 	struct mbuf *m;
    969       1.78    dyoung 	struct rt_addrinfo info;
    970       1.78    dyoung 
    971  1.132.2.1    jruoho 	COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
    972  1.132.2.1    jruoho 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
    973       1.78    dyoung 		return;
    974       1.78    dyoung 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
    975       1.78    dyoung 	if (m == NULL)
    976       1.36   thorpej 		return;
    977       1.78    dyoung 	/*
    978       1.78    dyoung 	 * Append the ieee80211 data.  Try to stick it in the
    979       1.78    dyoung 	 * mbuf containing the ifannounce msg; otherwise allocate
    980       1.78    dyoung 	 * a new mbuf and append.
    981       1.78    dyoung 	 *
    982       1.78    dyoung 	 * NB: we assume m is a single mbuf.
    983       1.78    dyoung 	 */
    984       1.78    dyoung 	if (data_len > M_TRAILINGSPACE(m)) {
    985       1.78    dyoung 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
    986       1.78    dyoung 		if (n == NULL) {
    987       1.78    dyoung 			m_freem(m);
    988       1.78    dyoung 			return;
    989       1.78    dyoung 		}
    990       1.78    dyoung 		(void)memcpy(mtod(n, void *), data, data_len);
    991       1.78    dyoung 		n->m_len = data_len;
    992       1.78    dyoung 		m->m_next = n;
    993       1.78    dyoung 	} else if (data_len > 0) {
    994       1.98      matt 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
    995       1.78    dyoung 		m->m_len += data_len;
    996       1.78    dyoung 	}
    997       1.78    dyoung 	if (m->m_flags & M_PKTHDR)
    998       1.78    dyoung 		m->m_pkthdr.len += data_len;
    999  1.132.2.1    jruoho 	mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
   1000  1.132.2.1    jruoho 	COMPATNAME(route_enqueue)(m, 0);
   1001       1.10   mycroft }
   1002       1.10   mycroft 
   1003       1.10   mycroft /*
   1004       1.10   mycroft  * This is used in dumping the kernel table via sysctl().
   1005        1.1       cgd  */
   1006       1.40    simonb static int
   1007       1.94    dyoung sysctl_dumpentry(struct rtentry *rt, void *v)
   1008        1.1       cgd {
   1009      1.120  christos 	struct rt_walkarg *w = v;
   1010       1.10   mycroft 	int error = 0, size;
   1011       1.10   mycroft 	struct rt_addrinfo info;
   1012        1.1       cgd 
   1013       1.10   mycroft 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
   1014       1.10   mycroft 		return 0;
   1015       1.48   thorpej 	memset(&info, 0, sizeof(info));
   1016      1.114    dyoung 	info.rti_info[RTAX_DST] = rt_getkey(rt);
   1017      1.114    dyoung 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1018      1.114    dyoung 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1019       1.16       cgd 	if (rt->rt_ifp) {
   1020       1.91    dyoung 		const struct ifaddr *rtifa;
   1021      1.114    dyoung 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
   1022       1.91    dyoung 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
   1023       1.91    dyoung 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
   1024       1.91    dyoung 		 * --dyoung
   1025       1.91    dyoung 		 */
   1026       1.91    dyoung 		rtifa = rt_get_ifa(rt);
   1027      1.114    dyoung 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
   1028       1.16       cgd 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
   1029      1.114    dyoung 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
   1030       1.16       cgd 	}
   1031       1.29    chopps 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
   1032       1.95    dyoung 		return error;
   1033       1.29    chopps 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1034  1.132.2.1    jruoho 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
   1035       1.10   mycroft 
   1036       1.10   mycroft 		rtm->rtm_flags = rt->rt_flags;
   1037       1.10   mycroft 		rtm->rtm_use = rt->rt_use;
   1038  1.132.2.1    jruoho 		rtm_setmetrics(rt, rtm);
   1039       1.83  christos 		KASSERT(rt->rt_ifp != NULL);
   1040       1.10   mycroft 		rtm->rtm_index = rt->rt_ifp->if_index;
   1041       1.10   mycroft 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
   1042       1.10   mycroft 		rtm->rtm_addrs = info.rti_addrs;
   1043       1.21  christos 		if ((error = copyout(rtm, w->w_where, size)) != 0)
   1044       1.10   mycroft 			w->w_where = NULL;
   1045       1.10   mycroft 		else
   1046       1.93  christos 			w->w_where = (char *)w->w_where + size;
   1047       1.10   mycroft 	}
   1048       1.95    dyoung 	return error;
   1049       1.10   mycroft }
   1050        1.1       cgd 
   1051       1.40    simonb static int
   1052      1.120  christos sysctl_iflist(int af, struct rt_walkarg *w, int type)
   1053       1.10   mycroft {
   1054       1.39  augustss 	struct ifnet *ifp;
   1055       1.39  augustss 	struct ifaddr *ifa;
   1056       1.10   mycroft 	struct	rt_addrinfo info;
   1057       1.10   mycroft 	int	len, error = 0;
   1058       1.10   mycroft 
   1059       1.48   thorpej 	memset(&info, 0, sizeof(info));
   1060       1.74      matt 	IFNET_FOREACH(ifp) {
   1061       1.10   mycroft 		if (w->w_arg && w->w_arg != ifp->if_index)
   1062       1.10   mycroft 			continue;
   1063       1.97    dyoung 		if (IFADDR_EMPTY(ifp))
   1064       1.81    rpaulo 			continue;
   1065      1.114    dyoung 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   1066       1.59    itojun 		switch (type) {
   1067       1.32    bouyer 		case NET_RT_IFLIST:
   1068      1.111  christos 			error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
   1069       1.32    bouyer 			break;
   1070       1.32    bouyer #ifdef COMPAT_14
   1071      1.120  christos 		case NET_RT_OOIFLIST:
   1072      1.120  christos 			error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
   1073      1.120  christos 			break;
   1074      1.120  christos #endif
   1075      1.120  christos #ifdef COMPAT_50
   1076       1.32    bouyer 		case NET_RT_OIFLIST:
   1077      1.111  christos 			error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
   1078       1.32    bouyer 			break;
   1079       1.32    bouyer #endif
   1080       1.32    bouyer 		default:
   1081       1.32    bouyer 			panic("sysctl_iflist(1)");
   1082       1.32    bouyer 		}
   1083       1.32    bouyer 		if (error)
   1084       1.95    dyoung 			return error;
   1085      1.114    dyoung 		info.rti_info[RTAX_IFP] = NULL;
   1086       1.29    chopps 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1087       1.59    itojun 			switch (type) {
   1088       1.32    bouyer 			case NET_RT_IFLIST: {
   1089  1.132.2.1    jruoho 				struct if_xmsghdr *ifm;
   1090       1.32    bouyer 
   1091  1.132.2.1    jruoho 				ifm = (struct if_xmsghdr *)w->w_tmem;
   1092       1.32    bouyer 				ifm->ifm_index = ifp->if_index;
   1093       1.32    bouyer 				ifm->ifm_flags = ifp->if_flags;
   1094       1.32    bouyer 				ifm->ifm_data = ifp->if_data;
   1095       1.32    bouyer 				ifm->ifm_addrs = info.rti_addrs;
   1096       1.32    bouyer 				error = copyout(ifm, w->w_where, len);
   1097       1.32    bouyer 				if (error)
   1098       1.95    dyoung 					return error;
   1099       1.93  christos 				w->w_where = (char *)w->w_where + len;
   1100       1.32    bouyer 				break;
   1101       1.32    bouyer 			}
   1102       1.10   mycroft 
   1103       1.32    bouyer #ifdef COMPAT_14
   1104      1.120  christos 			case NET_RT_OOIFLIST:
   1105      1.120  christos 				error = compat_14_iflist(ifp, w, &info, len);
   1106      1.120  christos 				if (error)
   1107      1.120  christos 					return error;
   1108      1.120  christos 				break;
   1109      1.120  christos #endif
   1110      1.120  christos #ifdef COMPAT_50
   1111      1.120  christos 			case NET_RT_OIFLIST:
   1112      1.120  christos 				error = compat_50_iflist(ifp, w, &info, len);
   1113       1.32    bouyer 				if (error)
   1114       1.95    dyoung 					return error;
   1115       1.32    bouyer 				break;
   1116       1.32    bouyer #endif
   1117       1.32    bouyer 			default:
   1118       1.32    bouyer 				panic("sysctl_iflist(2)");
   1119       1.32    bouyer 			}
   1120       1.10   mycroft 		}
   1121       1.97    dyoung 		IFADDR_FOREACH(ifa, ifp) {
   1122       1.10   mycroft 			if (af && af != ifa->ifa_addr->sa_family)
   1123       1.10   mycroft 				continue;
   1124      1.114    dyoung 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
   1125      1.114    dyoung 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1126      1.114    dyoung 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   1127       1.29    chopps 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
   1128       1.95    dyoung 				return error;
   1129       1.29    chopps 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1130  1.132.2.1    jruoho 				struct ifa_xmsghdr *ifam;
   1131       1.10   mycroft 
   1132  1.132.2.1    jruoho 				ifam = (struct ifa_xmsghdr *)w->w_tmem;
   1133       1.10   mycroft 				ifam->ifam_index = ifa->ifa_ifp->if_index;
   1134       1.10   mycroft 				ifam->ifam_flags = ifa->ifa_flags;
   1135       1.10   mycroft 				ifam->ifam_metric = ifa->ifa_metric;
   1136       1.10   mycroft 				ifam->ifam_addrs = info.rti_addrs;
   1137       1.17  christos 				error = copyout(w->w_tmem, w->w_where, len);
   1138       1.17  christos 				if (error)
   1139       1.95    dyoung 					return error;
   1140       1.93  christos 				w->w_where = (char *)w->w_where + len;
   1141       1.10   mycroft 			}
   1142       1.10   mycroft 		}
   1143      1.115  christos 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
   1144      1.115  christos 		    info.rti_info[RTAX_BRD] = NULL;
   1145       1.10   mycroft 	}
   1146       1.95    dyoung 	return 0;
   1147        1.1       cgd }
   1148        1.1       cgd 
   1149       1.40    simonb static int
   1150       1.65    atatat sysctl_rtable(SYSCTLFN_ARGS)
   1151        1.1       cgd {
   1152       1.65    atatat 	void 	*where = oldp;
   1153       1.65    atatat 	size_t	*given = oldlenp;
   1154       1.65    atatat 	const void *new = newp;
   1155       1.10   mycroft 	int	i, s, error = EINVAL;
   1156       1.10   mycroft 	u_char  af;
   1157      1.120  christos 	struct	rt_walkarg w;
   1158        1.1       cgd 
   1159       1.66    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
   1160       1.95    dyoung 		return sysctl_query(SYSCTLFN_CALL(rnode));
   1161       1.66    atatat 
   1162       1.10   mycroft 	if (new)
   1163       1.95    dyoung 		return EPERM;
   1164       1.10   mycroft 	if (namelen != 3)
   1165       1.95    dyoung 		return EINVAL;
   1166       1.10   mycroft 	af = name[0];
   1167       1.29    chopps 	w.w_tmemneeded = 0;
   1168       1.29    chopps 	w.w_tmemsize = 0;
   1169       1.29    chopps 	w.w_tmem = NULL;
   1170       1.29    chopps again:
   1171       1.29    chopps 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   1172       1.29    chopps 	if (w.w_tmemneeded) {
   1173      1.111  christos 		w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
   1174       1.29    chopps 		w.w_tmemsize = w.w_tmemneeded;
   1175       1.29    chopps 		w.w_tmemneeded = 0;
   1176       1.29    chopps 	}
   1177       1.29    chopps 	w.w_op = name[1];
   1178       1.29    chopps 	w.w_arg = name[2];
   1179       1.10   mycroft 	w.w_given = *given;
   1180        1.1       cgd 	w.w_needed = 0 - w.w_given;
   1181       1.29    chopps 	w.w_where = where;
   1182        1.1       cgd 
   1183       1.14   mycroft 	s = splsoftnet();
   1184       1.10   mycroft 	switch (w.w_op) {
   1185       1.10   mycroft 
   1186       1.10   mycroft 	case NET_RT_DUMP:
   1187       1.10   mycroft 	case NET_RT_FLAGS:
   1188       1.10   mycroft 		for (i = 1; i <= AF_MAX; i++)
   1189       1.94    dyoung 			if ((af == 0 || af == i) &&
   1190       1.94    dyoung 			    (error = rt_walktree(i, sysctl_dumpentry, &w)))
   1191       1.10   mycroft 				break;
   1192       1.10   mycroft 		break;
   1193       1.10   mycroft 
   1194       1.32    bouyer #ifdef COMPAT_14
   1195      1.120  christos 	case NET_RT_OOIFLIST:
   1196      1.120  christos 		error = sysctl_iflist(af, &w, w.w_op);
   1197      1.120  christos 		break;
   1198      1.120  christos #endif
   1199      1.120  christos #ifdef COMPAT_50
   1200       1.32    bouyer 	case NET_RT_OIFLIST:
   1201       1.32    bouyer 		error = sysctl_iflist(af, &w, w.w_op);
   1202       1.32    bouyer 		break;
   1203       1.32    bouyer #endif
   1204       1.10   mycroft 	case NET_RT_IFLIST:
   1205       1.32    bouyer 		error = sysctl_iflist(af, &w, w.w_op);
   1206  1.132.2.1    jruoho 		break;
   1207        1.1       cgd 	}
   1208       1.10   mycroft 	splx(s);
   1209       1.29    chopps 
   1210       1.29    chopps 	/* check to see if we couldn't allocate memory with NOWAIT */
   1211       1.29    chopps 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   1212       1.29    chopps 		goto again;
   1213       1.29    chopps 
   1214       1.10   mycroft 	if (w.w_tmem)
   1215       1.10   mycroft 		free(w.w_tmem, M_RTABLE);
   1216        1.1       cgd 	w.w_needed += w.w_given;
   1217       1.10   mycroft 	if (where) {
   1218       1.93  christos 		*given = (char *)w.w_where - (char *)where;
   1219       1.10   mycroft 		if (*given < w.w_needed)
   1220       1.95    dyoung 			return ENOMEM;
   1221       1.10   mycroft 	} else {
   1222       1.10   mycroft 		*given = (11 * w.w_needed) / 10;
   1223       1.10   mycroft 	}
   1224       1.95    dyoung 	return error;
   1225        1.1       cgd }
   1226        1.1       cgd 
   1227        1.1       cgd /*
   1228       1.99        ad  * Routing message software interrupt routine
   1229       1.99        ad  */
   1230       1.99        ad static void
   1231  1.132.2.1    jruoho COMPATNAME(route_intr)(void *cookie)
   1232       1.99        ad {
   1233  1.132.2.1    jruoho 	struct sockproto proto = { .sp_family = PF_XROUTE, };
   1234  1.132.2.1    jruoho 	struct route_info * const ri = &COMPATNAME(route_info);
   1235       1.99        ad 	struct mbuf *m;
   1236       1.99        ad 	int s;
   1237       1.99        ad 
   1238      1.101        ad 	mutex_enter(softnet_lock);
   1239      1.101        ad 	KERNEL_LOCK(1, NULL);
   1240  1.132.2.1    jruoho 	while (!IF_IS_EMPTY(&ri->ri_intrq)) {
   1241       1.99        ad 		s = splnet();
   1242  1.132.2.1    jruoho 		IF_DEQUEUE(&ri->ri_intrq, m);
   1243       1.99        ad 		splx(s);
   1244       1.99        ad 		if (m == NULL)
   1245       1.99        ad 			break;
   1246      1.100      yamt 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
   1247  1.132.2.1    jruoho 		raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
   1248       1.99        ad 	}
   1249      1.101        ad 	KERNEL_UNLOCK_ONE(NULL);
   1250      1.101        ad 	mutex_exit(softnet_lock);
   1251       1.99        ad }
   1252       1.99        ad 
   1253       1.99        ad /*
   1254       1.99        ad  * Enqueue a message to the software interrupt routine.
   1255       1.99        ad  */
   1256      1.120  christos void
   1257  1.132.2.1    jruoho COMPATNAME(route_enqueue)(struct mbuf *m, int family)
   1258       1.99        ad {
   1259  1.132.2.1    jruoho 	struct route_info * const ri = &COMPATNAME(route_info);
   1260       1.99        ad 	int s, wasempty;
   1261       1.99        ad 
   1262       1.99        ad 	s = splnet();
   1263  1.132.2.1    jruoho 	if (IF_QFULL(&ri->ri_intrq)) {
   1264  1.132.2.1    jruoho 		IF_DROP(&ri->ri_intrq);
   1265       1.99        ad 		m_freem(m);
   1266       1.99        ad 	} else {
   1267  1.132.2.1    jruoho 		wasempty = IF_IS_EMPTY(&ri->ri_intrq);
   1268       1.99        ad 		M_SETCTX(m, (uintptr_t)family);
   1269  1.132.2.1    jruoho 		IF_ENQUEUE(&ri->ri_intrq, m);
   1270       1.99        ad 		if (wasempty)
   1271  1.132.2.1    jruoho 			softint_schedule(ri->ri_sih);
   1272       1.99        ad 	}
   1273       1.99        ad 	splx(s);
   1274       1.99        ad }
   1275       1.99        ad 
   1276  1.132.2.1    jruoho static void
   1277  1.132.2.1    jruoho COMPATNAME(route_init)(void)
   1278       1.99        ad {
   1279  1.132.2.1    jruoho 	struct route_info * const ri = &COMPATNAME(route_info);
   1280  1.132.2.1    jruoho 
   1281  1.132.2.1    jruoho #ifndef COMPAT_RTSOCK
   1282  1.132.2.1    jruoho 	rt_init();
   1283  1.132.2.1    jruoho #endif
   1284       1.99        ad 
   1285      1.127     pooka 	sysctl_net_route_setup(NULL);
   1286  1.132.2.1    jruoho 	ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
   1287  1.132.2.1    jruoho 	ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1288  1.132.2.1    jruoho 	    COMPATNAME(route_intr), NULL);
   1289       1.99        ad }
   1290       1.99        ad 
   1291       1.99        ad /*
   1292        1.1       cgd  * Definitions of protocols supported in the ROUTE domain.
   1293        1.1       cgd  */
   1294  1.132.2.1    jruoho #ifndef COMPAT_RTSOCK
   1295  1.132.2.1    jruoho PR_WRAP_USRREQ(route_usrreq);
   1296  1.132.2.1    jruoho #else
   1297  1.132.2.1    jruoho PR_WRAP_USRREQ(compat_50_route_usrreq);
   1298  1.132.2.1    jruoho #endif
   1299        1.1       cgd 
   1300  1.132.2.1    jruoho static const struct protosw COMPATNAME(route_protosw)[] = {
   1301       1.92      matt 	{
   1302       1.92      matt 		.pr_type = SOCK_RAW,
   1303  1.132.2.1    jruoho 		.pr_domain = &COMPATNAME(routedomain),
   1304       1.92      matt 		.pr_flags = PR_ATOMIC|PR_ADDR,
   1305       1.92      matt 		.pr_input = raw_input,
   1306  1.132.2.1    jruoho 		.pr_output = COMPATNAME(route_output),
   1307       1.92      matt 		.pr_ctlinput = raw_ctlinput,
   1308  1.132.2.1    jruoho 		.pr_usrreq = COMPATNAME(route_usrreq_wrapper),
   1309       1.92      matt 		.pr_init = raw_init,
   1310       1.92      matt 	},
   1311       1.92      matt };
   1312       1.69      matt 
   1313  1.132.2.1    jruoho struct domain COMPATNAME(routedomain) = {
   1314  1.132.2.1    jruoho 	.dom_family = PF_XROUTE,
   1315  1.132.2.1    jruoho 	.dom_name = DOMAINNAME,
   1316  1.132.2.1    jruoho 	.dom_init = COMPATNAME(route_init),
   1317  1.132.2.1    jruoho 	.dom_protosw = COMPATNAME(route_protosw),
   1318  1.132.2.1    jruoho 	.dom_protoswNPROTOSW =
   1319  1.132.2.1    jruoho 	    &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
   1320        1.1       cgd };
   1321        1.1       cgd 
   1322      1.127     pooka static void
   1323      1.127     pooka sysctl_net_route_setup(struct sysctllog **clog)
   1324       1.65    atatat {
   1325       1.85      elad 	const struct sysctlnode *rnode = NULL;
   1326       1.85      elad 
   1327       1.67    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1328       1.67    atatat 		       CTLFLAG_PERMANENT,
   1329       1.65    atatat 		       CTLTYPE_NODE, "net", NULL,
   1330       1.65    atatat 		       NULL, 0, NULL, 0,
   1331       1.65    atatat 		       CTL_NET, CTL_EOL);
   1332       1.65    atatat 
   1333       1.85      elad 	sysctl_createv(clog, 0, NULL, &rnode,
   1334       1.67    atatat 		       CTLFLAG_PERMANENT,
   1335  1.132.2.1    jruoho 		       CTLTYPE_NODE, DOMAINNAME,
   1336       1.71    atatat 		       SYSCTL_DESCR("PF_ROUTE information"),
   1337       1.65    atatat 		       NULL, 0, NULL, 0,
   1338  1.132.2.1    jruoho 		       CTL_NET, PF_XROUTE, CTL_EOL);
   1339  1.132.2.1    jruoho 
   1340       1.67    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   1341       1.67    atatat 		       CTLFLAG_PERMANENT,
   1342       1.71    atatat 		       CTLTYPE_NODE, "rtable",
   1343       1.71    atatat 		       SYSCTL_DESCR("Routing table information"),
   1344       1.65    atatat 		       sysctl_rtable, 0, NULL, 0,
   1345  1.132.2.1    jruoho 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
   1346  1.132.2.1    jruoho 
   1347       1.85      elad 	sysctl_createv(clog, 0, &rnode, NULL,
   1348       1.85      elad 		       CTLFLAG_PERMANENT,
   1349       1.85      elad 		       CTLTYPE_STRUCT, "stats",
   1350       1.85      elad 		       SYSCTL_DESCR("Routing statistics"),
   1351       1.85      elad 		       NULL, 0, &rtstat, sizeof(rtstat),
   1352       1.85      elad 		       CTL_CREATE, CTL_EOL);
   1353       1.65    atatat }
   1354