Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.140.6.3
      1  1.140.6.1       tls /*	$NetBSD: rtsock.c,v 1.140.6.3 2017/12/03 11:39:02 jdolecek 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.140.6.1       tls __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.140.6.3 2017/12/03 11:39:02 jdolecek Exp $");
     65       1.31   thorpej 
     66      1.133      matt #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.140.6.3  jdolecek #include "opt_sctp.h"
     71  1.140.6.3  jdolecek #include "opt_net_mpsafe.h"
     72      1.120  christos #endif
     73        1.1       cgd 
     74        1.5   mycroft #include <sys/param.h>
     75        1.5   mycroft #include <sys/systm.h>
     76       1.10   mycroft #include <sys/proc.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.84      elad #include <sys/kauth.h>
     83  1.140.6.2       tls #include <sys/kmem.h>
     84       1.99        ad #include <sys/intr.h>
     85       1.17  christos 
     86        1.5   mycroft #include <net/if.h>
     87  1.140.6.3  jdolecek #include <net/if_llatbl.h>
     88  1.140.6.3  jdolecek #include <net/if_types.h>
     89        1.5   mycroft #include <net/route.h>
     90        1.5   mycroft #include <net/raw_cb.h>
     91        1.1       cgd 
     92  1.140.6.3  jdolecek #include <netinet/in_var.h>
     93  1.140.6.3  jdolecek #include <netinet/if_inarp.h>
     94  1.140.6.3  jdolecek 
     95      1.129    kefren #include <netmpls/mpls.h>
     96      1.129    kefren 
     97  1.140.6.3  jdolecek #ifdef SCTP
     98  1.140.6.3  jdolecek extern void sctp_add_ip_address(struct ifaddr *);
     99  1.140.6.3  jdolecek extern void sctp_delete_ip_address(struct ifaddr *);
    100  1.140.6.3  jdolecek #endif
    101  1.140.6.3  jdolecek 
    102  1.140.6.3  jdolecek #if defined(COMPAT_14) || defined(COMPAT_50) || defined(COMPAT_70)
    103      1.120  christos #include <compat/net/if.h>
    104      1.133      matt #include <compat/net/route.h>
    105      1.133      matt #endif
    106      1.133      matt #ifdef COMPAT_RTSOCK
    107      1.133      matt #define	RTM_XVERSION	RTM_OVERSION
    108  1.140.6.3  jdolecek #define	RTM_XNEWADDR	RTM_ONEWADDR
    109  1.140.6.3  jdolecek #define	RTM_XDELADDR	RTM_ODELADDR
    110  1.140.6.3  jdolecek #define	RTM_XCHGADDR	RTM_OCHGADDR
    111      1.133      matt #define	RT_XADVANCE(a,b) RT_OADVANCE(a,b)
    112      1.133      matt #define	RT_XROUNDUP(n)	RT_OROUNDUP(n)
    113      1.133      matt #define	PF_XROUTE	PF_OROUTE
    114      1.133      matt #define	rt_xmsghdr	rt_msghdr50
    115      1.133      matt #define	if_xmsghdr	if_msghdr	/* if_msghdr50 is for RTM_OIFINFO */
    116      1.133      matt #define	ifa_xmsghdr	ifa_msghdr50
    117      1.133      matt #define	if_xannouncemsghdr	if_announcemsghdr50
    118      1.133      matt #define	COMPATNAME(x)	compat_50_ ## x
    119      1.133      matt #define	DOMAINNAME	"oroute"
    120      1.133      matt CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
    121      1.133      matt DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
    122  1.140.6.3  jdolecek #undef COMPAT_70
    123  1.140.6.3  jdolecek #else /* COMPAT_RTSOCK */
    124      1.133      matt #define	RTM_XVERSION	RTM_VERSION
    125  1.140.6.3  jdolecek #define	RTM_XNEWADDR	RTM_NEWADDR
    126  1.140.6.3  jdolecek #define	RTM_XDELADDR	RTM_DELADDR
    127  1.140.6.3  jdolecek #define	RTM_XCHGADDR	RTM_CHGADDR
    128      1.133      matt #define	RT_XADVANCE(a,b) RT_ADVANCE(a,b)
    129      1.133      matt #define	RT_XROUNDUP(n)	RT_ROUNDUP(n)
    130      1.133      matt #define	PF_XROUTE	PF_ROUTE
    131      1.133      matt #define	rt_xmsghdr	rt_msghdr
    132      1.133      matt #define	if_xmsghdr	if_msghdr
    133      1.133      matt #define	ifa_xmsghdr	ifa_msghdr
    134      1.133      matt #define	if_xannouncemsghdr	if_announcemsghdr
    135      1.133      matt #define	COMPATNAME(x)	x
    136      1.133      matt #define	DOMAINNAME	"route"
    137  1.140.6.3  jdolecek CTASSERT(sizeof(struct ifa_xmsghdr) == 32);
    138      1.133      matt #ifdef COMPAT_50
    139      1.133      matt #define	COMPATCALL(name, args)	compat_50_ ## name args
    140      1.133      matt #endif
    141      1.133      matt DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
    142      1.133      matt #undef COMPAT_50
    143      1.133      matt #undef COMPAT_14
    144  1.140.6.3  jdolecek #endif /* COMPAT_RTSOCK */
    145      1.133      matt 
    146      1.133      matt #ifndef COMPATCALL
    147      1.133      matt #define	COMPATCALL(name, args)	do { } while (/*CONSTCOND*/ 0)
    148      1.120  christos #endif
    149      1.120  christos 
    150  1.140.6.3  jdolecek #ifdef RTSOCK_DEBUG
    151  1.140.6.3  jdolecek #define RT_IN_PRINT(info, b, a) (in_print((b), sizeof(b), \
    152  1.140.6.3  jdolecek     &((const struct sockaddr_in *)(info)->rti_info[(a)])->sin_addr), (b))
    153  1.140.6.3  jdolecek #endif /* RTSOCK_DEBUG */
    154  1.140.6.3  jdolecek 
    155      1.133      matt struct route_info COMPATNAME(route_info) = {
    156      1.133      matt 	.ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
    157      1.133      matt 	.ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
    158      1.133      matt 	.ri_maxqlen = IFQ_MAXLEN,
    159      1.133      matt };
    160       1.58      matt 
    161      1.134    kefren #define	PRESERVED_RTF	(RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK)
    162      1.134    kefren 
    163      1.133      matt static void COMPATNAME(route_init)(void);
    164  1.140.6.3  jdolecek static int COMPATNAME(route_output)(struct mbuf *, struct socket *);
    165       1.10   mycroft 
    166       1.72  christos static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
    167       1.78    dyoung static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
    168       1.78    dyoung     struct rt_addrinfo *);
    169  1.140.6.3  jdolecek static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
    170      1.133      matt static void rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
    171      1.133      matt static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
    172      1.127     pooka static void sysctl_net_route_setup(struct sysctllog **);
    173       1.94    dyoung static int sysctl_dumpentry(struct rtentry *, void *);
    174      1.120  christos static int sysctl_iflist(int, struct rt_walkarg *, int);
    175       1.69      matt static int sysctl_rtable(SYSCTLFN_PROTO);
    176      1.123      yamt static void rt_adjustcount(int, int);
    177       1.10   mycroft 
    178  1.140.6.3  jdolecek static const struct protosw COMPATNAME(route_protosw)[];
    179  1.140.6.3  jdolecek 
    180  1.140.6.3  jdolecek struct routecb {
    181  1.140.6.3  jdolecek 	struct rawcb	rocb_rcb;
    182  1.140.6.3  jdolecek 	unsigned int	rocb_msgfilter;
    183  1.140.6.3  jdolecek #define	RTMSGFILTER(m)	(1U << (m))
    184  1.140.6.3  jdolecek };
    185  1.140.6.3  jdolecek #define sotoroutecb(so)	((struct routecb *)(so)->so_pcb)
    186  1.140.6.3  jdolecek 
    187  1.140.6.3  jdolecek static struct rawcbhead rt_rawcb;
    188  1.140.6.3  jdolecek #ifdef NET_MPSAFE
    189  1.140.6.3  jdolecek static kmutex_t *rt_so_mtx;
    190  1.140.6.3  jdolecek #endif
    191  1.140.6.3  jdolecek 
    192      1.123      yamt static void
    193       1.69      matt rt_adjustcount(int af, int cnt)
    194       1.27  christos {
    195      1.133      matt 	struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
    196      1.133      matt 
    197      1.133      matt 	cb->any_count += cnt;
    198      1.133      matt 
    199       1.27  christos 	switch (af) {
    200       1.27  christos 	case AF_INET:
    201      1.133      matt 		cb->ip_count += cnt;
    202       1.27  christos 		return;
    203       1.30    itojun #ifdef INET6
    204       1.30    itojun 	case AF_INET6:
    205      1.133      matt 		cb->ip6_count += cnt;
    206       1.30    itojun 		return;
    207       1.30    itojun #endif
    208      1.129    kefren 	case AF_MPLS:
    209      1.133      matt 		cb->mpls_count += cnt;
    210       1.27  christos 		return;
    211       1.27  christos 	}
    212       1.27  christos }
    213      1.123      yamt 
    214  1.140.6.2       tls static int
    215  1.140.6.3  jdolecek COMPATNAME(route_filter)(struct mbuf *m, struct sockproto *proto,
    216  1.140.6.3  jdolecek     struct rawcb *rp)
    217  1.140.6.3  jdolecek {
    218  1.140.6.3  jdolecek 	struct routecb *rop = (struct routecb *)rp;
    219  1.140.6.3  jdolecek 	struct rt_xmsghdr *rtm;
    220  1.140.6.3  jdolecek 
    221  1.140.6.3  jdolecek 	KASSERT(m != NULL);
    222  1.140.6.3  jdolecek 	KASSERT(proto != NULL);
    223  1.140.6.3  jdolecek 	KASSERT(rp != NULL);
    224  1.140.6.3  jdolecek 
    225  1.140.6.3  jdolecek 	/* Wrong family for this socket. */
    226  1.140.6.3  jdolecek 	if (proto->sp_family != PF_ROUTE)
    227  1.140.6.3  jdolecek 		return ENOPROTOOPT;
    228  1.140.6.3  jdolecek 
    229  1.140.6.3  jdolecek 	/* If no filter set, just return. */
    230  1.140.6.3  jdolecek 	if (rop->rocb_msgfilter == 0)
    231  1.140.6.3  jdolecek 		return 0;
    232  1.140.6.3  jdolecek 
    233  1.140.6.3  jdolecek 	/* Ensure we can access rtm_type */
    234  1.140.6.3  jdolecek 	if (m->m_len <
    235  1.140.6.3  jdolecek 	    offsetof(struct rt_xmsghdr, rtm_type) + sizeof(rtm->rtm_type))
    236  1.140.6.3  jdolecek 		return EINVAL;
    237  1.140.6.3  jdolecek 
    238  1.140.6.3  jdolecek 	rtm = mtod(m, struct rt_xmsghdr *);
    239  1.140.6.3  jdolecek 	/* If the rtm type is filtered out, return a positive. */
    240  1.140.6.3  jdolecek 	if (!(rop->rocb_msgfilter & RTMSGFILTER(rtm->rtm_type)))
    241  1.140.6.3  jdolecek 		return EEXIST;
    242  1.140.6.3  jdolecek 
    243  1.140.6.3  jdolecek 	/* Passed the filter. */
    244  1.140.6.3  jdolecek 	return 0;
    245  1.140.6.3  jdolecek }
    246  1.140.6.3  jdolecek 
    247  1.140.6.3  jdolecek static void
    248  1.140.6.3  jdolecek rt_pr_init(void)
    249  1.140.6.3  jdolecek {
    250  1.140.6.3  jdolecek 
    251  1.140.6.3  jdolecek 	LIST_INIT(&rt_rawcb);
    252  1.140.6.3  jdolecek }
    253  1.140.6.3  jdolecek 
    254  1.140.6.3  jdolecek static int
    255  1.140.6.2       tls COMPATNAME(route_attach)(struct socket *so, int proto)
    256  1.140.6.2       tls {
    257  1.140.6.2       tls 	struct rawcb *rp;
    258  1.140.6.3  jdolecek 	struct routecb *rop;
    259  1.140.6.2       tls 	int s, error;
    260  1.140.6.2       tls 
    261  1.140.6.2       tls 	KASSERT(sotorawcb(so) == NULL);
    262  1.140.6.3  jdolecek 	rop = kmem_zalloc(sizeof(*rop), KM_SLEEP);
    263  1.140.6.3  jdolecek 	rp = &rop->rocb_rcb;
    264  1.140.6.3  jdolecek 	rp->rcb_len = sizeof(*rop);
    265  1.140.6.2       tls 	so->so_pcb = rp;
    266  1.140.6.2       tls 
    267  1.140.6.2       tls 	s = splsoftnet();
    268  1.140.6.3  jdolecek 
    269  1.140.6.3  jdolecek #ifdef NET_MPSAFE
    270  1.140.6.3  jdolecek 	KASSERT(so->so_lock == NULL);
    271  1.140.6.3  jdolecek 	mutex_obj_hold(rt_so_mtx);
    272  1.140.6.3  jdolecek 	so->so_lock = rt_so_mtx;
    273  1.140.6.3  jdolecek 	solock(so);
    274  1.140.6.3  jdolecek #endif
    275  1.140.6.3  jdolecek 
    276  1.140.6.3  jdolecek 	if ((error = raw_attach(so, proto, &rt_rawcb)) == 0) {
    277  1.140.6.2       tls 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
    278  1.140.6.2       tls 		rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
    279  1.140.6.2       tls 		rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
    280  1.140.6.3  jdolecek 		rp->rcb_filter = COMPATNAME(route_filter);
    281  1.140.6.2       tls 	}
    282  1.140.6.2       tls 	splx(s);
    283  1.140.6.2       tls 
    284  1.140.6.2       tls 	if (error) {
    285  1.140.6.3  jdolecek 		kmem_free(rop, sizeof(*rop));
    286  1.140.6.2       tls 		so->so_pcb = NULL;
    287  1.140.6.2       tls 		return error;
    288  1.140.6.2       tls 	}
    289  1.140.6.2       tls 
    290  1.140.6.2       tls 	soisconnected(so);
    291  1.140.6.2       tls 	so->so_options |= SO_USELOOPBACK;
    292  1.140.6.2       tls 	KASSERT(solocked(so));
    293  1.140.6.2       tls 
    294  1.140.6.2       tls 	return error;
    295  1.140.6.2       tls }
    296  1.140.6.2       tls 
    297  1.140.6.2       tls static void
    298  1.140.6.2       tls COMPATNAME(route_detach)(struct socket *so)
    299        1.1       cgd {
    300       1.39  augustss 	struct rawcb *rp = sotorawcb(so);
    301        1.1       cgd 	int s;
    302       1.10   mycroft 
    303  1.140.6.2       tls 	KASSERT(rp != NULL);
    304  1.140.6.2       tls 	KASSERT(solocked(so));
    305  1.140.6.2       tls 
    306       1.14   mycroft 	s = splsoftnet();
    307  1.140.6.2       tls 	rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
    308  1.140.6.2       tls 	raw_detach(so);
    309  1.140.6.2       tls 	splx(s);
    310  1.140.6.2       tls }
    311  1.140.6.2       tls 
    312  1.140.6.2       tls static int
    313  1.140.6.3  jdolecek COMPATNAME(route_accept)(struct socket *so, struct sockaddr *nam)
    314  1.140.6.2       tls {
    315  1.140.6.2       tls 	KASSERT(solocked(so));
    316  1.140.6.2       tls 
    317  1.140.6.2       tls 	panic("route_accept");
    318  1.140.6.2       tls 
    319  1.140.6.2       tls 	return EOPNOTSUPP;
    320  1.140.6.2       tls }
    321  1.140.6.2       tls 
    322  1.140.6.2       tls static int
    323  1.140.6.3  jdolecek COMPATNAME(route_bind)(struct socket *so, struct sockaddr *nam, struct lwp *l)
    324  1.140.6.2       tls {
    325  1.140.6.2       tls 	KASSERT(solocked(so));
    326  1.140.6.2       tls 
    327  1.140.6.2       tls 	return EOPNOTSUPP;
    328  1.140.6.2       tls }
    329  1.140.6.2       tls 
    330  1.140.6.2       tls static int
    331  1.140.6.2       tls COMPATNAME(route_listen)(struct socket *so, struct lwp *l)
    332  1.140.6.2       tls {
    333  1.140.6.2       tls 	KASSERT(solocked(so));
    334  1.140.6.2       tls 
    335  1.140.6.2       tls 	return EOPNOTSUPP;
    336  1.140.6.2       tls }
    337  1.140.6.2       tls 
    338  1.140.6.2       tls static int
    339  1.140.6.3  jdolecek COMPATNAME(route_connect)(struct socket *so, struct sockaddr *nam, struct lwp *l)
    340  1.140.6.2       tls {
    341  1.140.6.2       tls 	KASSERT(solocked(so));
    342  1.140.6.2       tls 
    343  1.140.6.2       tls 	return EOPNOTSUPP;
    344  1.140.6.2       tls }
    345  1.140.6.2       tls 
    346  1.140.6.2       tls static int
    347  1.140.6.2       tls COMPATNAME(route_connect2)(struct socket *so, struct socket *so2)
    348  1.140.6.2       tls {
    349  1.140.6.2       tls 	KASSERT(solocked(so));
    350  1.140.6.2       tls 
    351  1.140.6.2       tls 	return EOPNOTSUPP;
    352  1.140.6.2       tls }
    353  1.140.6.2       tls 
    354  1.140.6.2       tls static int
    355  1.140.6.2       tls COMPATNAME(route_disconnect)(struct socket *so)
    356  1.140.6.2       tls {
    357  1.140.6.2       tls 	struct rawcb *rp = sotorawcb(so);
    358  1.140.6.2       tls 	int s;
    359  1.140.6.2       tls 
    360  1.140.6.2       tls 	KASSERT(solocked(so));
    361  1.140.6.2       tls 	KASSERT(rp != NULL);
    362  1.140.6.2       tls 
    363  1.140.6.2       tls 	s = splsoftnet();
    364  1.140.6.2       tls 	soisdisconnected(so);
    365  1.140.6.2       tls 	raw_disconnect(rp);
    366  1.140.6.2       tls 	splx(s);
    367  1.140.6.2       tls 
    368  1.140.6.2       tls 	return 0;
    369  1.140.6.2       tls }
    370  1.140.6.2       tls 
    371  1.140.6.2       tls static int
    372  1.140.6.2       tls COMPATNAME(route_shutdown)(struct socket *so)
    373  1.140.6.2       tls {
    374  1.140.6.2       tls 	int s;
    375  1.140.6.2       tls 
    376  1.140.6.2       tls 	KASSERT(solocked(so));
    377       1.23   thorpej 
    378       1.23   thorpej 	/*
    379  1.140.6.2       tls 	 * Mark the connection as being incapable of further input.
    380       1.23   thorpej 	 */
    381  1.140.6.2       tls 	s = splsoftnet();
    382  1.140.6.2       tls 	socantsendmore(so);
    383        1.1       cgd 	splx(s);
    384  1.140.6.2       tls 	return 0;
    385  1.140.6.2       tls }
    386  1.140.6.2       tls 
    387  1.140.6.2       tls static int
    388  1.140.6.2       tls COMPATNAME(route_abort)(struct socket *so)
    389  1.140.6.2       tls {
    390  1.140.6.2       tls 	KASSERT(solocked(so));
    391  1.140.6.2       tls 
    392  1.140.6.2       tls 	panic("route_abort");
    393  1.140.6.2       tls 
    394  1.140.6.2       tls 	return EOPNOTSUPP;
    395  1.140.6.2       tls }
    396  1.140.6.2       tls 
    397  1.140.6.2       tls static int
    398  1.140.6.2       tls COMPATNAME(route_ioctl)(struct socket *so, u_long cmd, void *nam,
    399  1.140.6.2       tls     struct ifnet * ifp)
    400  1.140.6.2       tls {
    401  1.140.6.2       tls 	return EOPNOTSUPP;
    402  1.140.6.2       tls }
    403  1.140.6.2       tls 
    404  1.140.6.2       tls static int
    405  1.140.6.2       tls COMPATNAME(route_stat)(struct socket *so, struct stat *ub)
    406  1.140.6.2       tls {
    407  1.140.6.2       tls 	KASSERT(solocked(so));
    408  1.140.6.2       tls 
    409  1.140.6.2       tls 	return 0;
    410  1.140.6.2       tls }
    411  1.140.6.2       tls 
    412  1.140.6.2       tls static int
    413  1.140.6.3  jdolecek COMPATNAME(route_peeraddr)(struct socket *so, struct sockaddr *nam)
    414  1.140.6.2       tls {
    415  1.140.6.2       tls 	struct rawcb *rp = sotorawcb(so);
    416  1.140.6.2       tls 
    417  1.140.6.2       tls 	KASSERT(solocked(so));
    418  1.140.6.2       tls 	KASSERT(rp != NULL);
    419  1.140.6.2       tls 	KASSERT(nam != NULL);
    420  1.140.6.2       tls 
    421  1.140.6.2       tls 	if (rp->rcb_faddr == NULL)
    422  1.140.6.2       tls 		return ENOTCONN;
    423  1.140.6.2       tls 
    424  1.140.6.2       tls 	raw_setpeeraddr(rp, nam);
    425  1.140.6.2       tls 	return 0;
    426  1.140.6.2       tls }
    427  1.140.6.2       tls 
    428  1.140.6.2       tls static int
    429  1.140.6.3  jdolecek COMPATNAME(route_sockaddr)(struct socket *so, struct sockaddr *nam)
    430  1.140.6.2       tls {
    431  1.140.6.2       tls 	struct rawcb *rp = sotorawcb(so);
    432  1.140.6.2       tls 
    433  1.140.6.2       tls 	KASSERT(solocked(so));
    434  1.140.6.2       tls 	KASSERT(rp != NULL);
    435  1.140.6.2       tls 	KASSERT(nam != NULL);
    436  1.140.6.2       tls 
    437  1.140.6.2       tls 	if (rp->rcb_faddr == NULL)
    438  1.140.6.2       tls 		return ENOTCONN;
    439  1.140.6.2       tls 
    440  1.140.6.2       tls 	raw_setsockaddr(rp, nam);
    441  1.140.6.2       tls 	return 0;
    442  1.140.6.2       tls }
    443  1.140.6.2       tls 
    444  1.140.6.2       tls static int
    445  1.140.6.2       tls COMPATNAME(route_rcvd)(struct socket *so, int flags, struct lwp *l)
    446  1.140.6.2       tls {
    447  1.140.6.2       tls 	KASSERT(solocked(so));
    448  1.140.6.2       tls 
    449  1.140.6.2       tls 	return EOPNOTSUPP;
    450  1.140.6.2       tls }
    451  1.140.6.2       tls 
    452  1.140.6.2       tls static int
    453  1.140.6.2       tls COMPATNAME(route_recvoob)(struct socket *so, struct mbuf *m, int flags)
    454  1.140.6.2       tls {
    455  1.140.6.2       tls 	KASSERT(solocked(so));
    456  1.140.6.2       tls 
    457  1.140.6.2       tls 	return EOPNOTSUPP;
    458  1.140.6.2       tls }
    459  1.140.6.2       tls 
    460  1.140.6.2       tls static int
    461  1.140.6.2       tls COMPATNAME(route_send)(struct socket *so, struct mbuf *m,
    462  1.140.6.3  jdolecek     struct sockaddr *nam, struct mbuf *control, struct lwp *l)
    463  1.140.6.2       tls {
    464  1.140.6.2       tls 	int error = 0;
    465  1.140.6.2       tls 	int s;
    466  1.140.6.2       tls 
    467  1.140.6.2       tls 	KASSERT(solocked(so));
    468  1.140.6.3  jdolecek 	KASSERT(so->so_proto == &COMPATNAME(route_protosw)[0]);
    469  1.140.6.2       tls 
    470  1.140.6.2       tls 	s = splsoftnet();
    471  1.140.6.3  jdolecek 	error = raw_send(so, m, nam, control, l, &COMPATNAME(route_output));
    472  1.140.6.2       tls 	splx(s);
    473  1.140.6.2       tls 
    474  1.140.6.2       tls 	return error;
    475  1.140.6.2       tls }
    476  1.140.6.2       tls 
    477  1.140.6.2       tls static int
    478  1.140.6.2       tls COMPATNAME(route_sendoob)(struct socket *so, struct mbuf *m,
    479  1.140.6.2       tls     struct mbuf *control)
    480  1.140.6.2       tls {
    481  1.140.6.2       tls 	KASSERT(solocked(so));
    482  1.140.6.2       tls 
    483  1.140.6.2       tls 	m_freem(m);
    484  1.140.6.2       tls 	m_freem(control);
    485  1.140.6.2       tls 
    486  1.140.6.2       tls 	return EOPNOTSUPP;
    487  1.140.6.2       tls }
    488  1.140.6.2       tls static int
    489  1.140.6.2       tls COMPATNAME(route_purgeif)(struct socket *so, struct ifnet *ifp)
    490  1.140.6.2       tls {
    491  1.140.6.2       tls 
    492  1.140.6.2       tls 	panic("route_purgeif");
    493  1.140.6.2       tls 
    494  1.140.6.2       tls 	return EOPNOTSUPP;
    495  1.140.6.2       tls }
    496  1.140.6.2       tls 
    497  1.140.6.3  jdolecek #if defined(INET) || defined(INET6)
    498  1.140.6.3  jdolecek static int
    499  1.140.6.3  jdolecek route_get_sdl_index(struct rt_addrinfo *info, int *sdl_index)
    500  1.140.6.3  jdolecek {
    501  1.140.6.3  jdolecek 	struct rtentry *nrt;
    502  1.140.6.3  jdolecek 	int error;
    503  1.140.6.3  jdolecek 
    504  1.140.6.3  jdolecek 	error = rtrequest1(RTM_GET, info, &nrt);
    505  1.140.6.3  jdolecek 	if (error != 0)
    506  1.140.6.3  jdolecek 		return error;
    507  1.140.6.3  jdolecek 	/*
    508  1.140.6.3  jdolecek 	 * nrt->rt_ifp->if_index may not be correct
    509  1.140.6.3  jdolecek 	 * due to changing to ifplo0.
    510  1.140.6.3  jdolecek 	 */
    511  1.140.6.3  jdolecek 	*sdl_index = satosdl(nrt->rt_gateway)->sdl_index;
    512  1.140.6.3  jdolecek 	rt_unref(nrt);
    513  1.140.6.3  jdolecek 
    514  1.140.6.3  jdolecek 	return 0;
    515  1.140.6.3  jdolecek }
    516  1.140.6.3  jdolecek #endif
    517  1.140.6.3  jdolecek 
    518  1.140.6.3  jdolecek static void
    519  1.140.6.3  jdolecek route_get_sdl(const struct ifnet *ifp, const struct sockaddr *dst,
    520  1.140.6.3  jdolecek     struct sockaddr_dl *sdl, int *flags)
    521  1.140.6.3  jdolecek {
    522  1.140.6.3  jdolecek 	struct llentry *la;
    523  1.140.6.3  jdolecek 
    524  1.140.6.3  jdolecek 	KASSERT(ifp != NULL);
    525  1.140.6.3  jdolecek 
    526  1.140.6.3  jdolecek 	IF_AFDATA_RLOCK(ifp);
    527  1.140.6.3  jdolecek 	switch (dst->sa_family) {
    528  1.140.6.3  jdolecek 	case AF_INET:
    529  1.140.6.3  jdolecek 		la = lla_lookup(LLTABLE(ifp), 0, dst);
    530  1.140.6.3  jdolecek 		break;
    531  1.140.6.3  jdolecek 	case AF_INET6:
    532  1.140.6.3  jdolecek 		la = lla_lookup(LLTABLE6(ifp), 0, dst);
    533  1.140.6.3  jdolecek 		break;
    534  1.140.6.3  jdolecek 	default:
    535  1.140.6.3  jdolecek 		la = NULL;
    536  1.140.6.3  jdolecek 		KASSERTMSG(0, "Invalid AF=%d\n", dst->sa_family);
    537  1.140.6.3  jdolecek 		break;
    538  1.140.6.3  jdolecek 	}
    539  1.140.6.3  jdolecek 	IF_AFDATA_RUNLOCK(ifp);
    540  1.140.6.3  jdolecek 
    541  1.140.6.3  jdolecek 	void *a = (LLE_IS_VALID(la) && (la->la_flags & LLE_VALID) == LLE_VALID)
    542  1.140.6.3  jdolecek 	    ? &la->ll_addr : NULL;
    543  1.140.6.3  jdolecek 
    544  1.140.6.3  jdolecek 	a = sockaddr_dl_init(sdl, sizeof(*sdl), ifp->if_index, ifp->if_type,
    545  1.140.6.3  jdolecek 	    NULL, 0, a, ifp->if_addrlen);
    546  1.140.6.3  jdolecek 	KASSERT(a != NULL);
    547  1.140.6.3  jdolecek 
    548  1.140.6.3  jdolecek 	if (la != NULL) {
    549  1.140.6.3  jdolecek 		*flags = la->la_flags;
    550  1.140.6.3  jdolecek 		LLE_RUNLOCK(la);
    551  1.140.6.3  jdolecek 	}
    552  1.140.6.3  jdolecek }
    553  1.140.6.3  jdolecek 
    554  1.140.6.2       tls static int
    555  1.140.6.3  jdolecek route_output_report(struct rtentry *rt, struct rt_addrinfo *info,
    556  1.140.6.3  jdolecek     struct rt_xmsghdr *rtm, struct rt_xmsghdr **new_rtm)
    557  1.140.6.2       tls {
    558  1.140.6.3  jdolecek 	int len;
    559  1.140.6.2       tls 
    560  1.140.6.3  jdolecek 	if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
    561  1.140.6.3  jdolecek 		const struct ifaddr *rtifa;
    562  1.140.6.3  jdolecek 		const struct ifnet *ifp = rt->rt_ifp;
    563  1.140.6.2       tls 
    564  1.140.6.3  jdolecek 		info->rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    565  1.140.6.3  jdolecek 		/* rtifa used to be simply rt->rt_ifa.
    566  1.140.6.3  jdolecek 		 * If rt->rt_ifa != NULL, then
    567  1.140.6.3  jdolecek 		 * rt_get_ifa() != NULL.  So this
    568  1.140.6.3  jdolecek 		 * ought to still be safe. --dyoung
    569  1.140.6.3  jdolecek 		 */
    570  1.140.6.3  jdolecek 		rtifa = rt_get_ifa(rt);
    571  1.140.6.3  jdolecek 		info->rti_info[RTAX_IFA] = rtifa->ifa_addr;
    572  1.140.6.3  jdolecek #ifdef RTSOCK_DEBUG
    573  1.140.6.3  jdolecek 		if (info->rti_info[RTAX_IFA]->sa_family == AF_INET) {
    574  1.140.6.3  jdolecek 			char ibuf[INET_ADDRSTRLEN];
    575  1.140.6.3  jdolecek 			char abuf[INET_ADDRSTRLEN];
    576  1.140.6.3  jdolecek 			printf("%s: copying out RTAX_IFA %s "
    577  1.140.6.3  jdolecek 			    "for info->rti_info[RTAX_DST] %s "
    578  1.140.6.3  jdolecek 			    "ifa_getifa %p ifa_seqno %p\n",
    579  1.140.6.3  jdolecek 			    __func__,
    580  1.140.6.3  jdolecek 			    RT_IN_PRINT(info, ibuf, RTAX_IFA),
    581  1.140.6.3  jdolecek 			    RT_IN_PRINT(info, abuf, RTAX_DST),
    582  1.140.6.3  jdolecek 			    (void *)rtifa->ifa_getifa,
    583  1.140.6.3  jdolecek 			    rtifa->ifa_seqno);
    584  1.140.6.3  jdolecek 		}
    585  1.140.6.3  jdolecek #endif /* RTSOCK_DEBUG */
    586  1.140.6.3  jdolecek 		if (ifp->if_flags & IFF_POINTOPOINT)
    587  1.140.6.3  jdolecek 			info->rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
    588  1.140.6.3  jdolecek 		else
    589  1.140.6.3  jdolecek 			info->rti_info[RTAX_BRD] = NULL;
    590  1.140.6.3  jdolecek 		rtm->rtm_index = ifp->if_index;
    591  1.140.6.3  jdolecek 	}
    592  1.140.6.3  jdolecek 	(void)rt_msg2(rtm->rtm_type, info, NULL, NULL, &len);
    593  1.140.6.3  jdolecek 	if (len > rtm->rtm_msglen) {
    594  1.140.6.3  jdolecek 		struct rt_xmsghdr *old_rtm = rtm;
    595  1.140.6.3  jdolecek 		R_Malloc(*new_rtm, struct rt_xmsghdr *, len);
    596  1.140.6.3  jdolecek 		if (*new_rtm == NULL)
    597  1.140.6.3  jdolecek 			return ENOBUFS;
    598  1.140.6.3  jdolecek 		(void)memcpy(*new_rtm, old_rtm, old_rtm->rtm_msglen);
    599  1.140.6.3  jdolecek 		rtm = *new_rtm;
    600  1.140.6.3  jdolecek 	}
    601  1.140.6.3  jdolecek 	(void)rt_msg2(rtm->rtm_type, info, rtm, NULL, 0);
    602  1.140.6.3  jdolecek 	rtm->rtm_flags = rt->rt_flags;
    603  1.140.6.3  jdolecek 	rtm_setmetrics(rt, rtm);
    604  1.140.6.3  jdolecek 	rtm->rtm_addrs = info->rti_addrs;
    605  1.140.6.3  jdolecek 
    606  1.140.6.3  jdolecek 	return 0;
    607  1.140.6.3  jdolecek }
    608  1.140.6.3  jdolecek 
    609  1.140.6.3  jdolecek static struct ifaddr *
    610  1.140.6.3  jdolecek route_output_get_ifa(const struct rt_addrinfo info, const struct rtentry *rt,
    611  1.140.6.3  jdolecek     struct ifnet **ifp, struct psref *psref)
    612  1.140.6.3  jdolecek {
    613  1.140.6.3  jdolecek 	struct ifaddr *ifa = NULL;
    614  1.140.6.3  jdolecek 
    615  1.140.6.3  jdolecek 	*ifp = NULL;
    616  1.140.6.3  jdolecek 	if (info.rti_info[RTAX_IFP] != NULL) {
    617  1.140.6.3  jdolecek 		ifa = ifa_ifwithnet_psref(info.rti_info[RTAX_IFP], psref);
    618  1.140.6.3  jdolecek 		if (ifa == NULL)
    619  1.140.6.3  jdolecek 			goto next;
    620  1.140.6.3  jdolecek 		*ifp = ifa->ifa_ifp;
    621  1.140.6.3  jdolecek 		if (info.rti_info[RTAX_IFA] == NULL &&
    622  1.140.6.3  jdolecek 		    info.rti_info[RTAX_GATEWAY] == NULL)
    623  1.140.6.3  jdolecek 			goto next;
    624  1.140.6.3  jdolecek 		if (info.rti_info[RTAX_IFA] == NULL) {
    625  1.140.6.3  jdolecek 			/* route change <dst> <gw> -ifp <if> */
    626  1.140.6.3  jdolecek 			ifa = ifaof_ifpforaddr_psref(info.rti_info[RTAX_GATEWAY],
    627  1.140.6.3  jdolecek 			    *ifp, psref);
    628  1.140.6.3  jdolecek 		} else {
    629  1.140.6.3  jdolecek 			/* route change <dst> -ifp <if> -ifa <addr> */
    630  1.140.6.3  jdolecek 			ifa = ifa_ifwithaddr_psref(info.rti_info[RTAX_IFA], psref);
    631  1.140.6.3  jdolecek 			if (ifa != NULL)
    632  1.140.6.3  jdolecek 				goto out;
    633  1.140.6.3  jdolecek 			ifa = ifaof_ifpforaddr_psref(info.rti_info[RTAX_IFA],
    634  1.140.6.3  jdolecek 			    *ifp, psref);
    635  1.140.6.3  jdolecek 		}
    636  1.140.6.3  jdolecek 		goto out;
    637  1.140.6.3  jdolecek 	}
    638  1.140.6.3  jdolecek next:
    639  1.140.6.3  jdolecek 	if (info.rti_info[RTAX_IFA] != NULL) {
    640  1.140.6.3  jdolecek 		/* route change <dst> <gw> -ifa <addr> */
    641  1.140.6.3  jdolecek 		ifa = ifa_ifwithaddr_psref(info.rti_info[RTAX_IFA], psref);
    642  1.140.6.3  jdolecek 		if (ifa != NULL)
    643  1.140.6.3  jdolecek 			goto out;
    644  1.140.6.3  jdolecek 	}
    645  1.140.6.3  jdolecek 	if (info.rti_info[RTAX_GATEWAY] != NULL) {
    646  1.140.6.3  jdolecek 		/* route change <dst> <gw> */
    647  1.140.6.3  jdolecek 		ifa = ifa_ifwithroute_psref(rt->rt_flags, rt_getkey(rt),
    648  1.140.6.3  jdolecek 		    info.rti_info[RTAX_GATEWAY], psref);
    649  1.140.6.3  jdolecek 	}
    650  1.140.6.3  jdolecek out:
    651  1.140.6.3  jdolecek 	if (ifa != NULL && *ifp == NULL)
    652  1.140.6.3  jdolecek 		*ifp = ifa->ifa_ifp;
    653  1.140.6.3  jdolecek 	return ifa;
    654  1.140.6.3  jdolecek }
    655  1.140.6.3  jdolecek 
    656  1.140.6.3  jdolecek static int
    657  1.140.6.3  jdolecek route_output_change(struct rtentry *rt, struct rt_addrinfo *info,
    658  1.140.6.3  jdolecek     struct rt_xmsghdr *rtm)
    659  1.140.6.3  jdolecek {
    660  1.140.6.3  jdolecek 	int error = 0;
    661  1.140.6.3  jdolecek 	struct ifnet *ifp = NULL, *new_ifp;
    662  1.140.6.3  jdolecek 	struct ifaddr *ifa = NULL, *new_ifa;
    663  1.140.6.3  jdolecek 	struct psref psref_ifa, psref_new_ifa, psref_ifp;
    664  1.140.6.3  jdolecek 	bool newgw, ifp_changed = false;
    665  1.140.6.3  jdolecek 
    666  1.140.6.3  jdolecek 	/*
    667  1.140.6.3  jdolecek 	 * New gateway could require new ifaddr, ifp;
    668  1.140.6.3  jdolecek 	 * flags may also be different; ifp may be specified
    669  1.140.6.3  jdolecek 	 * by ll sockaddr when protocol address is ambiguous
    670  1.140.6.3  jdolecek 	 */
    671  1.140.6.3  jdolecek 	newgw = info->rti_info[RTAX_GATEWAY] != NULL &&
    672  1.140.6.3  jdolecek 	    sockaddr_cmp(info->rti_info[RTAX_GATEWAY], rt->rt_gateway) != 0;
    673  1.140.6.3  jdolecek 
    674  1.140.6.3  jdolecek 	if (newgw || info->rti_info[RTAX_IFP] != NULL ||
    675  1.140.6.3  jdolecek 	    info->rti_info[RTAX_IFA] != NULL) {
    676  1.140.6.3  jdolecek 		ifp = rt_getifp(info, &psref_ifp);
    677  1.140.6.3  jdolecek 		ifa = rt_getifa(info, &psref_ifa);
    678  1.140.6.3  jdolecek 		if (ifa == NULL) {
    679  1.140.6.3  jdolecek 			error = ENETUNREACH;
    680  1.140.6.3  jdolecek 			goto out;
    681  1.140.6.3  jdolecek 		}
    682  1.140.6.3  jdolecek 	}
    683  1.140.6.3  jdolecek 	if (newgw) {
    684  1.140.6.3  jdolecek 		error = rt_setgate(rt, info->rti_info[RTAX_GATEWAY]);
    685  1.140.6.3  jdolecek 		if (error != 0)
    686  1.140.6.3  jdolecek 			goto out;
    687  1.140.6.3  jdolecek 	}
    688  1.140.6.3  jdolecek 	if (info->rti_info[RTAX_TAG]) {
    689  1.140.6.3  jdolecek 		const struct sockaddr *tag;
    690  1.140.6.3  jdolecek 		tag = rt_settag(rt, info->rti_info[RTAX_TAG]);
    691  1.140.6.3  jdolecek 		if (tag == NULL) {
    692  1.140.6.3  jdolecek 			error = ENOBUFS;
    693  1.140.6.3  jdolecek 			goto out;
    694  1.140.6.3  jdolecek 		}
    695  1.140.6.3  jdolecek 	}
    696  1.140.6.3  jdolecek 	/*
    697  1.140.6.3  jdolecek 	 * New gateway could require new ifaddr, ifp;
    698  1.140.6.3  jdolecek 	 * flags may also be different; ifp may be specified
    699  1.140.6.3  jdolecek 	 * by ll sockaddr when protocol address is ambiguous
    700  1.140.6.3  jdolecek 	 */
    701  1.140.6.3  jdolecek 	new_ifa = route_output_get_ifa(*info, rt, &new_ifp, &psref_new_ifa);
    702  1.140.6.3  jdolecek 	if (new_ifa != NULL) {
    703  1.140.6.3  jdolecek 		ifa_release(ifa, &psref_ifa);
    704  1.140.6.3  jdolecek 		ifa = new_ifa;
    705  1.140.6.3  jdolecek 	}
    706  1.140.6.3  jdolecek 	if (ifa) {
    707  1.140.6.3  jdolecek 		struct ifaddr *oifa = rt->rt_ifa;
    708  1.140.6.3  jdolecek 		if (oifa != ifa && !ifa_is_destroying(ifa) &&
    709  1.140.6.3  jdolecek 		    new_ifp != NULL && !if_is_deactivated(new_ifp)) {
    710  1.140.6.3  jdolecek 			if (oifa && oifa->ifa_rtrequest)
    711  1.140.6.3  jdolecek 				oifa->ifa_rtrequest(RTM_DELETE, rt, info);
    712  1.140.6.3  jdolecek 			rt_replace_ifa(rt, ifa);
    713  1.140.6.3  jdolecek 			rt->rt_ifp = new_ifp;
    714  1.140.6.3  jdolecek 			ifp_changed = true;
    715  1.140.6.3  jdolecek 		}
    716  1.140.6.3  jdolecek 		if (new_ifa == NULL)
    717  1.140.6.3  jdolecek 			ifa_release(ifa, &psref_ifa);
    718  1.140.6.3  jdolecek 	}
    719  1.140.6.3  jdolecek 	ifa_release(new_ifa, &psref_new_ifa);
    720  1.140.6.3  jdolecek 	if (new_ifp && rt->rt_ifp != new_ifp && !if_is_deactivated(new_ifp)) {
    721  1.140.6.3  jdolecek 		rt->rt_ifp = new_ifp;
    722  1.140.6.3  jdolecek 		ifp_changed = true;
    723  1.140.6.3  jdolecek 	}
    724  1.140.6.3  jdolecek 	rt_setmetrics(rtm->rtm_inits, rtm, rt);
    725  1.140.6.3  jdolecek 	if (rt->rt_flags != info->rti_flags) {
    726  1.140.6.3  jdolecek 		rt->rt_flags = (info->rti_flags & ~PRESERVED_RTF) |
    727  1.140.6.3  jdolecek 		    (rt->rt_flags & PRESERVED_RTF);
    728  1.140.6.3  jdolecek 	}
    729  1.140.6.3  jdolecek 	if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
    730  1.140.6.3  jdolecek 		rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, info);
    731  1.140.6.3  jdolecek #if defined(INET) || defined(INET6)
    732  1.140.6.3  jdolecek 	if (ifp_changed && rt_mask(rt) != NULL)
    733  1.140.6.3  jdolecek 		lltable_prefix_free(rt_getkey(rt)->sa_family, rt_getkey(rt),
    734  1.140.6.3  jdolecek 		    rt_mask(rt), 0);
    735  1.140.6.3  jdolecek #else
    736  1.140.6.3  jdolecek 	(void)ifp_changed; /* XXX gcc */
    737  1.140.6.3  jdolecek #endif
    738  1.140.6.3  jdolecek out:
    739  1.140.6.3  jdolecek 	if_put(ifp, &psref_ifp);
    740  1.140.6.2       tls 
    741       1.95    dyoung 	return error;
    742       1.95    dyoung }
    743       1.95    dyoung 
    744        1.1       cgd /*ARGSUSED*/
    745        1.9   mycroft int
    746  1.140.6.3  jdolecek COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
    747        1.1       cgd {
    748      1.133      matt 	struct sockproto proto = { .sp_family = PF_XROUTE, };
    749      1.133      matt 	struct rt_xmsghdr *rtm = NULL;
    750  1.140.6.3  jdolecek 	struct rt_xmsghdr *old_rtm = NULL, *new_rtm = NULL;
    751       1.95    dyoung 	struct rtentry *rt = NULL;
    752       1.95    dyoung 	struct rtentry *saved_nrt = NULL;
    753       1.10   mycroft 	struct rt_addrinfo info;
    754      1.124       roy 	int len, error = 0;
    755       1.55  christos 	sa_family_t family;
    756  1.140.6.3  jdolecek 	struct sockaddr_dl sdl;
    757  1.140.6.3  jdolecek 	int bound = curlwp_bind();
    758  1.140.6.3  jdolecek 	bool do_rt_free = false;
    759  1.140.6.3  jdolecek 	struct sockaddr_storage netmask;
    760       1.17  christos 
    761       1.56     perry #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
    762       1.95    dyoung 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
    763  1.140.6.3  jdolecek 	   (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
    764  1.140.6.3  jdolecek 		error = ENOBUFS;
    765  1.140.6.3  jdolecek 		goto out;
    766  1.140.6.3  jdolecek 	}
    767        1.1       cgd 	if ((m->m_flags & M_PKTHDR) == 0)
    768      1.133      matt 		panic("%s", __func__);
    769        1.1       cgd 	len = m->m_pkthdr.len;
    770        1.1       cgd 	if (len < sizeof(*rtm) ||
    771      1.133      matt 	    len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
    772      1.114    dyoung 		info.rti_info[RTAX_DST] = NULL;
    773        1.1       cgd 		senderr(EINVAL);
    774       1.10   mycroft 	}
    775      1.133      matt 	R_Malloc(rtm, struct rt_xmsghdr *, len);
    776       1.95    dyoung 	if (rtm == NULL) {
    777      1.114    dyoung 		info.rti_info[RTAX_DST] = NULL;
    778        1.1       cgd 		senderr(ENOBUFS);
    779       1.10   mycroft 	}
    780      1.112    dyoung 	m_copydata(m, 0, len, rtm);
    781      1.133      matt 	if (rtm->rtm_version != RTM_XVERSION) {
    782      1.114    dyoung 		info.rti_info[RTAX_DST] = NULL;
    783        1.1       cgd 		senderr(EPROTONOSUPPORT);
    784       1.10   mycroft 	}
    785        1.1       cgd 	rtm->rtm_pid = curproc->p_pid;
    786       1.48   thorpej 	memset(&info, 0, sizeof(info));
    787       1.10   mycroft 	info.rti_addrs = rtm->rtm_addrs;
    788      1.112    dyoung 	if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
    789      1.133      matt 	    &info)) {
    790       1.42       erh 		senderr(EINVAL);
    791      1.133      matt 	}
    792       1.45    itojun 	info.rti_flags = rtm->rtm_flags;
    793       1.91    dyoung #ifdef RTSOCK_DEBUG
    794      1.114    dyoung 	if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    795  1.140.6.3  jdolecek 		char abuf[INET_ADDRSTRLEN];
    796      1.114    dyoung 		printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
    797  1.140.6.3  jdolecek 		    RT_IN_PRINT(&info, abuf, RTAX_DST));
    798       1.91    dyoung 	}
    799       1.91    dyoung #endif /* RTSOCK_DEBUG */
    800      1.115  christos 	if (info.rti_info[RTAX_DST] == NULL ||
    801      1.133      matt 	    (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
    802       1.26      fvdl 		senderr(EINVAL);
    803      1.133      matt 	}
    804      1.115  christos 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
    805      1.133      matt 	    (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
    806        1.1       cgd 		senderr(EINVAL);
    807      1.133      matt 	}
    808       1.23   thorpej 
    809       1.23   thorpej 	/*
    810       1.23   thorpej 	 * Verify that the caller has the appropriate privilege; RTM_GET
    811       1.23   thorpej 	 * is the only operation the non-superuser is allowed.
    812       1.23   thorpej 	 */
    813       1.88      elad 	if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
    814       1.89      elad 	    0, rtm, NULL, NULL) != 0)
    815       1.23   thorpej 		senderr(EACCES);
    816       1.23   thorpej 
    817  1.140.6.3  jdolecek 	/*
    818  1.140.6.3  jdolecek 	 * route(8) passes a sockaddr truncated with prefixlen.
    819  1.140.6.3  jdolecek 	 * The kernel doesn't expect such sockaddr and need to
    820  1.140.6.3  jdolecek 	 * use a buffer that is big enough for the sockaddr expected
    821  1.140.6.3  jdolecek 	 * (padded with 0's). We keep the original length of the sockaddr.
    822  1.140.6.3  jdolecek 	 */
    823  1.140.6.3  jdolecek 	if (info.rti_info[RTAX_NETMASK]) {
    824  1.140.6.3  jdolecek 		/*
    825  1.140.6.3  jdolecek 		 * Use the family of RTAX_DST, because RTAX_NETMASK
    826  1.140.6.3  jdolecek 		 * can have a zero family if it comes from the radix
    827  1.140.6.3  jdolecek 		 * tree via rt_mask().
    828  1.140.6.3  jdolecek 		 */
    829  1.140.6.3  jdolecek 		socklen_t sa_len = sockaddr_getsize_by_family(
    830  1.140.6.3  jdolecek 		    info.rti_info[RTAX_DST]->sa_family);
    831  1.140.6.3  jdolecek 		socklen_t masklen = sockaddr_getlen(
    832  1.140.6.3  jdolecek 		    info.rti_info[RTAX_NETMASK]);
    833  1.140.6.3  jdolecek 		if (sa_len != 0 && sa_len > masklen) {
    834  1.140.6.3  jdolecek 			KASSERT(sa_len <= sizeof(netmask));
    835  1.140.6.3  jdolecek 			memcpy(&netmask, info.rti_info[RTAX_NETMASK], masklen);
    836  1.140.6.3  jdolecek 			memset((char *)&netmask + masklen, 0, sa_len - masklen);
    837  1.140.6.3  jdolecek 			info.rti_info[RTAX_NETMASK] = sstocsa(&netmask);
    838  1.140.6.3  jdolecek 		}
    839  1.140.6.3  jdolecek 	}
    840  1.140.6.3  jdolecek 
    841        1.1       cgd 	switch (rtm->rtm_type) {
    842       1.10   mycroft 
    843        1.1       cgd 	case RTM_ADD:
    844      1.133      matt 		if (info.rti_info[RTAX_GATEWAY] == NULL) {
    845        1.1       cgd 			senderr(EINVAL);
    846      1.133      matt 		}
    847  1.140.6.3  jdolecek #if defined(INET) || defined(INET6)
    848  1.140.6.3  jdolecek 		/* support for new ARP/NDP code with keeping backcompat */
    849  1.140.6.3  jdolecek 		if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
    850  1.140.6.3  jdolecek 			const struct sockaddr_dl *sdlp =
    851  1.140.6.3  jdolecek 			    satocsdl(info.rti_info[RTAX_GATEWAY]);
    852  1.140.6.3  jdolecek 
    853  1.140.6.3  jdolecek 			/* Allow routing requests by interface index */
    854  1.140.6.3  jdolecek 			if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
    855  1.140.6.3  jdolecek 			    && sdlp->sdl_slen == 0)
    856  1.140.6.3  jdolecek 				goto fallback;
    857  1.140.6.3  jdolecek 			/*
    858  1.140.6.3  jdolecek 			 * Old arp binaries don't set the sdl_index
    859  1.140.6.3  jdolecek 			 * so we have to complement it.
    860  1.140.6.3  jdolecek 			 */
    861  1.140.6.3  jdolecek 			int sdl_index = sdlp->sdl_index;
    862  1.140.6.3  jdolecek 			if (sdl_index == 0) {
    863  1.140.6.3  jdolecek 				error = route_get_sdl_index(&info, &sdl_index);
    864  1.140.6.3  jdolecek 				if (error != 0)
    865  1.140.6.3  jdolecek 					goto fallback;
    866  1.140.6.3  jdolecek 			} else if (
    867  1.140.6.3  jdolecek 			    info.rti_info[RTAX_DST]->sa_family == AF_INET) {
    868  1.140.6.3  jdolecek 				/*
    869  1.140.6.3  jdolecek 				 * XXX workaround for SIN_PROXY case; proxy arp
    870  1.140.6.3  jdolecek 				 * entry should be in an interface that has
    871  1.140.6.3  jdolecek 				 * a network route including the destination,
    872  1.140.6.3  jdolecek 				 * not a local (link) route that may not be a
    873  1.140.6.3  jdolecek 				 * desired place, for example a tap.
    874  1.140.6.3  jdolecek 				 */
    875  1.140.6.3  jdolecek 				const struct sockaddr_inarp *sina =
    876  1.140.6.3  jdolecek 				    (const struct sockaddr_inarp *)
    877  1.140.6.3  jdolecek 				    info.rti_info[RTAX_DST];
    878  1.140.6.3  jdolecek 				if (sina->sin_other & SIN_PROXY) {
    879  1.140.6.3  jdolecek 					error = route_get_sdl_index(&info,
    880  1.140.6.3  jdolecek 					    &sdl_index);
    881  1.140.6.3  jdolecek 					if (error != 0)
    882  1.140.6.3  jdolecek 						goto fallback;
    883  1.140.6.3  jdolecek 				}
    884  1.140.6.3  jdolecek 			}
    885  1.140.6.3  jdolecek 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
    886  1.140.6.3  jdolecek 			    rtm->rtm_rmx.rmx_expire, &info, sdl_index);
    887  1.140.6.3  jdolecek 			break;
    888  1.140.6.3  jdolecek 		}
    889  1.140.6.3  jdolecek 	fallback:
    890  1.140.6.3  jdolecek #endif /* defined(INET) || defined(INET6) */
    891       1.45    itojun 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    892  1.140.6.3  jdolecek 		if (error == 0) {
    893      1.133      matt 			rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
    894  1.140.6.3  jdolecek 			rt_unref(saved_nrt);
    895        1.1       cgd 		}
    896        1.1       cgd 		break;
    897        1.1       cgd 
    898        1.1       cgd 	case RTM_DELETE:
    899  1.140.6.3  jdolecek #if defined(INET) || defined(INET6)
    900  1.140.6.3  jdolecek 		/* support for new ARP/NDP code */
    901  1.140.6.3  jdolecek 		if (info.rti_info[RTAX_GATEWAY] &&
    902  1.140.6.3  jdolecek 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
    903  1.140.6.3  jdolecek 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
    904  1.140.6.3  jdolecek 			const struct sockaddr_dl *sdlp =
    905  1.140.6.3  jdolecek 			    satocsdl(info.rti_info[RTAX_GATEWAY]);
    906  1.140.6.3  jdolecek 			error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
    907  1.140.6.3  jdolecek 			    rtm->rtm_rmx.rmx_expire, &info, sdlp->sdl_index);
    908  1.140.6.3  jdolecek 			rtm->rtm_flags &= ~RTF_UP;
    909  1.140.6.3  jdolecek 			break;
    910  1.140.6.3  jdolecek 		}
    911  1.140.6.3  jdolecek #endif
    912       1.45    itojun 		error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
    913  1.140.6.3  jdolecek 		if (error != 0)
    914  1.140.6.3  jdolecek 			break;
    915  1.140.6.3  jdolecek 
    916  1.140.6.3  jdolecek 		rt = saved_nrt;
    917  1.140.6.3  jdolecek 		do_rt_free = true;
    918  1.140.6.3  jdolecek 		info.rti_info[RTAX_DST] = rt_getkey(rt);
    919  1.140.6.3  jdolecek 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    920  1.140.6.3  jdolecek 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    921  1.140.6.3  jdolecek 		info.rti_info[RTAX_TAG] = rt_gettag(rt);
    922  1.140.6.3  jdolecek 		error = route_output_report(rt, &info, rtm, &new_rtm);
    923  1.140.6.3  jdolecek 		if (error)
    924  1.140.6.3  jdolecek 			senderr(error);
    925  1.140.6.3  jdolecek 		if (new_rtm != NULL) {
    926  1.140.6.3  jdolecek 			old_rtm = rtm;
    927  1.140.6.3  jdolecek 			rtm = new_rtm;
    928       1.16       cgd 		}
    929        1.1       cgd 		break;
    930        1.1       cgd 
    931        1.1       cgd 	case RTM_GET:
    932        1.1       cgd 	case RTM_CHANGE:
    933        1.1       cgd 	case RTM_LOCK:
    934      1.115  christos                 /* XXX This will mask info.rti_info[RTAX_DST] with
    935      1.115  christos 		 * info.rti_info[RTAX_NETMASK] before
    936       1.95    dyoung                  * searching.  It did not used to do that.  --dyoung
    937       1.95    dyoung 		 */
    938  1.140.6.3  jdolecek 		rt = NULL;
    939      1.103    dyoung 		error = rtrequest1(RTM_GET, &info, &rt);
    940       1.95    dyoung 		if (error != 0)
    941       1.95    dyoung 			senderr(error);
    942       1.61    itojun 		if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
    943      1.115  christos 			if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
    944      1.115  christos 			    info.rti_info[RTAX_DST]->sa_len) != 0)
    945       1.61    itojun 				senderr(ESRCH);
    946      1.135    dyoung 			if (info.rti_info[RTAX_NETMASK] == NULL &&
    947      1.135    dyoung 			    rt_mask(rt) != NULL)
    948       1.61    itojun 				senderr(ETOOMANYREFS);
    949       1.61    itojun 		}
    950       1.37    itojun 
    951  1.140.6.3  jdolecek 		/*
    952  1.140.6.3  jdolecek 		 * XXX if arp/ndp requests an L2 entry, we have to obtain
    953  1.140.6.3  jdolecek 		 * it from lltable while for the route command we have to
    954  1.140.6.3  jdolecek 		 * return a route as it is. How to distinguish them?
    955  1.140.6.3  jdolecek 		 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
    956  1.140.6.3  jdolecek 		 * indicates an L2 entry is requested. For old arp/ndp
    957  1.140.6.3  jdolecek 		 * binaries, we check RTF_UP flag is NOT set; it works
    958  1.140.6.3  jdolecek 		 * by the fact that arp/ndp don't set it while the route
    959  1.140.6.3  jdolecek 		 * command sets it.
    960  1.140.6.3  jdolecek 		 */
    961  1.140.6.3  jdolecek 		if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
    962  1.140.6.3  jdolecek 		     (rtm->rtm_flags & RTF_UP) == 0) &&
    963  1.140.6.3  jdolecek 		    rtm->rtm_type == RTM_GET &&
    964  1.140.6.3  jdolecek 		    sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
    965  1.140.6.3  jdolecek 			int ll_flags = 0;
    966  1.140.6.3  jdolecek 			route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
    967  1.140.6.3  jdolecek 			    &ll_flags);
    968  1.140.6.3  jdolecek 			info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
    969  1.140.6.3  jdolecek 			error = route_output_report(rt, &info, rtm, &new_rtm);
    970  1.140.6.3  jdolecek 			if (error)
    971  1.140.6.3  jdolecek 				senderr(error);
    972  1.140.6.3  jdolecek 			if (new_rtm != NULL) {
    973  1.140.6.3  jdolecek 				old_rtm = rtm;
    974  1.140.6.3  jdolecek 				rtm = new_rtm;
    975  1.140.6.3  jdolecek 			}
    976  1.140.6.3  jdolecek 			rtm->rtm_flags |= RTF_LLDATA;
    977  1.140.6.3  jdolecek 			rtm->rtm_flags &= ~RTF_CONNECTED;
    978  1.140.6.3  jdolecek 			rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
    979  1.140.6.3  jdolecek 			break;
    980  1.140.6.3  jdolecek 		}
    981  1.140.6.3  jdolecek 
    982       1.59    itojun 		switch (rtm->rtm_type) {
    983        1.1       cgd 		case RTM_GET:
    984      1.114    dyoung 			info.rti_info[RTAX_DST] = rt_getkey(rt);
    985      1.114    dyoung 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    986      1.114    dyoung 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    987      1.137      yamt 			info.rti_info[RTAX_TAG] = rt_gettag(rt);
    988  1.140.6.3  jdolecek 			error = route_output_report(rt, &info, rtm, &new_rtm);
    989  1.140.6.3  jdolecek 			if (error)
    990  1.140.6.3  jdolecek 				senderr(error);
    991  1.140.6.3  jdolecek 			if (new_rtm != NULL) {
    992      1.117  christos 				old_rtm = rtm;
    993  1.140.6.3  jdolecek 				rtm = new_rtm;
    994        1.1       cgd 			}
    995        1.1       cgd 			break;
    996        1.1       cgd 
    997        1.1       cgd 		case RTM_CHANGE:
    998  1.140.6.3  jdolecek #ifdef NET_MPSAFE
    999  1.140.6.3  jdolecek 			error = rt_update_prepare(rt);
   1000  1.140.6.3  jdolecek 			if (error == 0) {
   1001  1.140.6.3  jdolecek 				error = route_output_change(rt, &info, rtm);
   1002  1.140.6.3  jdolecek 				rt_update_finish(rt);
   1003      1.115  christos 			}
   1004  1.140.6.3  jdolecek #else
   1005  1.140.6.3  jdolecek 			error = route_output_change(rt, &info, rtm);
   1006  1.140.6.3  jdolecek #endif
   1007  1.140.6.3  jdolecek 			if (error != 0)
   1008  1.140.6.3  jdolecek 				goto flush;
   1009      1.115  christos 			/*FALLTHROUGH*/
   1010        1.1       cgd 		case RTM_LOCK:
   1011       1.10   mycroft 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
   1012        1.1       cgd 			rt->rt_rmx.rmx_locks |=
   1013       1.21  christos 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
   1014        1.1       cgd 			break;
   1015        1.1       cgd 		}
   1016       1.10   mycroft 		break;
   1017        1.1       cgd 
   1018        1.1       cgd 	default:
   1019        1.1       cgd 		senderr(EOPNOTSUPP);
   1020        1.1       cgd 	}
   1021        1.1       cgd 
   1022        1.1       cgd flush:
   1023        1.1       cgd 	if (rtm) {
   1024        1.1       cgd 		if (error)
   1025        1.1       cgd 			rtm->rtm_errno = error;
   1026       1.75     perry 		else
   1027        1.1       cgd 			rtm->rtm_flags |= RTF_DONE;
   1028        1.1       cgd 	}
   1029      1.115  christos 	family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
   1030      1.115  christos 	    0;
   1031      1.117  christos 	/* We cannot free old_rtm until we have stopped using the
   1032      1.117  christos 	 * pointers in info, some of which may point to sockaddrs
   1033      1.117  christos 	 * in old_rtm.
   1034      1.117  christos 	 */
   1035      1.117  christos 	if (old_rtm != NULL)
   1036      1.117  christos 		Free(old_rtm);
   1037  1.140.6.3  jdolecek 	if (rt) {
   1038  1.140.6.3  jdolecek 		if (do_rt_free)
   1039  1.140.6.3  jdolecek 			rt_free(rt);
   1040  1.140.6.3  jdolecek 		else
   1041  1.140.6.3  jdolecek 			rt_unref(rt);
   1042  1.140.6.3  jdolecek 	}
   1043        1.1       cgd     {
   1044       1.95    dyoung 	struct rawcb *rp = NULL;
   1045        1.1       cgd 	/*
   1046        1.1       cgd 	 * Check to see if we don't want our own messages.
   1047        1.1       cgd 	 */
   1048        1.1       cgd 	if ((so->so_options & SO_USELOOPBACK) == 0) {
   1049      1.133      matt 		if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
   1050        1.1       cgd 			if (rtm)
   1051        1.1       cgd 				Free(rtm);
   1052        1.1       cgd 			m_freem(m);
   1053  1.140.6.3  jdolecek 			goto out;
   1054        1.1       cgd 		}
   1055        1.1       cgd 		/* There is another listener, so construct message */
   1056        1.1       cgd 		rp = sotorawcb(so);
   1057        1.1       cgd 	}
   1058        1.1       cgd 	if (rtm) {
   1059      1.112    dyoung 		m_copyback(m, 0, rtm->rtm_msglen, rtm);
   1060       1.47    itojun 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
   1061       1.46    itojun 			m_freem(m);
   1062       1.46    itojun 			m = NULL;
   1063       1.47    itojun 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
   1064       1.46    itojun 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
   1065        1.1       cgd 		Free(rtm);
   1066        1.1       cgd 	}
   1067        1.1       cgd 	if (rp)
   1068        1.1       cgd 		rp->rcb_proto.sp_family = 0; /* Avoid us */
   1069       1.55  christos 	if (family)
   1070       1.99        ad 		proto.sp_protocol = family;
   1071       1.46    itojun 	if (m)
   1072      1.133      matt 		raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
   1073  1.140.6.3  jdolecek 		    &COMPATNAME(route_info).ri_dst, &rt_rawcb);
   1074        1.1       cgd 	if (rp)
   1075      1.133      matt 		rp->rcb_proto.sp_family = PF_XROUTE;
   1076        1.1       cgd     }
   1077  1.140.6.3  jdolecek out:
   1078  1.140.6.3  jdolecek 	curlwp_bindx(bound);
   1079  1.140.6.3  jdolecek 	return error;
   1080  1.140.6.3  jdolecek }
   1081  1.140.6.3  jdolecek 
   1082  1.140.6.3  jdolecek static int
   1083  1.140.6.3  jdolecek route_ctloutput(int op, struct socket *so, struct sockopt *sopt)
   1084  1.140.6.3  jdolecek {
   1085  1.140.6.3  jdolecek 	struct routecb *rop = sotoroutecb(so);
   1086  1.140.6.3  jdolecek 	int error = 0;
   1087  1.140.6.3  jdolecek 	unsigned char *rtm_type;
   1088  1.140.6.3  jdolecek 	size_t len;
   1089  1.140.6.3  jdolecek 	unsigned int msgfilter;
   1090  1.140.6.3  jdolecek 
   1091  1.140.6.3  jdolecek 	KASSERT(solocked(so));
   1092  1.140.6.3  jdolecek 
   1093  1.140.6.3  jdolecek 	if (sopt->sopt_level != AF_ROUTE) {
   1094  1.140.6.3  jdolecek 		error = ENOPROTOOPT;
   1095  1.140.6.3  jdolecek 	} else switch (op) {
   1096  1.140.6.3  jdolecek 	case PRCO_SETOPT:
   1097  1.140.6.3  jdolecek 		switch (sopt->sopt_name) {
   1098  1.140.6.3  jdolecek 		case RO_MSGFILTER:
   1099  1.140.6.3  jdolecek 			msgfilter = 0;
   1100  1.140.6.3  jdolecek 			for (rtm_type = sopt->sopt_data, len = sopt->sopt_size;
   1101  1.140.6.3  jdolecek 			     len != 0;
   1102  1.140.6.3  jdolecek 			     rtm_type++, len -= sizeof(*rtm_type))
   1103  1.140.6.3  jdolecek 			{
   1104  1.140.6.3  jdolecek 				/* Guard against overflowing our storage. */
   1105  1.140.6.3  jdolecek 				if (*rtm_type >= sizeof(msgfilter) * CHAR_BIT) {
   1106  1.140.6.3  jdolecek 					error = EOVERFLOW;
   1107  1.140.6.3  jdolecek 					break;
   1108  1.140.6.3  jdolecek 				}
   1109  1.140.6.3  jdolecek 				msgfilter |= RTMSGFILTER(*rtm_type);
   1110  1.140.6.3  jdolecek 			}
   1111  1.140.6.3  jdolecek 			if (error == 0)
   1112  1.140.6.3  jdolecek 				rop->rocb_msgfilter = msgfilter;
   1113  1.140.6.3  jdolecek 			break;
   1114  1.140.6.3  jdolecek 		default:
   1115  1.140.6.3  jdolecek 			error = ENOPROTOOPT;
   1116  1.140.6.3  jdolecek 			break;
   1117  1.140.6.3  jdolecek 		}
   1118  1.140.6.3  jdolecek 		break;
   1119  1.140.6.3  jdolecek 	case PRCO_GETOPT:
   1120  1.140.6.3  jdolecek 		switch (sopt->sopt_name) {
   1121  1.140.6.3  jdolecek 		case RO_MSGFILTER:
   1122  1.140.6.3  jdolecek 			error = ENOTSUP;
   1123  1.140.6.3  jdolecek 			break;
   1124  1.140.6.3  jdolecek 		default:
   1125  1.140.6.3  jdolecek 			error = ENOPROTOOPT;
   1126  1.140.6.3  jdolecek 			break;
   1127  1.140.6.3  jdolecek 		}
   1128  1.140.6.3  jdolecek 	}
   1129       1.95    dyoung 	return error;
   1130        1.1       cgd }
   1131        1.1       cgd 
   1132      1.133      matt static void
   1133      1.133      matt rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
   1134        1.1       cgd {
   1135      1.133      matt #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
   1136        1.1       cgd 	metric(RTV_RPIPE, rmx_recvpipe);
   1137        1.1       cgd 	metric(RTV_SPIPE, rmx_sendpipe);
   1138        1.1       cgd 	metric(RTV_SSTHRESH, rmx_ssthresh);
   1139        1.1       cgd 	metric(RTV_RTT, rmx_rtt);
   1140        1.1       cgd 	metric(RTV_RTTVAR, rmx_rttvar);
   1141        1.1       cgd 	metric(RTV_HOPCOUNT, rmx_hopcount);
   1142        1.1       cgd 	metric(RTV_MTU, rmx_mtu);
   1143        1.1       cgd #undef metric
   1144  1.140.6.3  jdolecek 	if (which & RTV_EXPIRE) {
   1145  1.140.6.3  jdolecek 		out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
   1146  1.140.6.3  jdolecek 		    time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
   1147  1.140.6.3  jdolecek 	}
   1148        1.1       cgd }
   1149        1.1       cgd 
   1150      1.133      matt static void
   1151      1.133      matt rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
   1152      1.133      matt {
   1153      1.133      matt #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
   1154      1.133      matt 	metric(rmx_recvpipe);
   1155      1.133      matt 	metric(rmx_sendpipe);
   1156      1.133      matt 	metric(rmx_ssthresh);
   1157      1.133      matt 	metric(rmx_rtt);
   1158      1.133      matt 	metric(rmx_rttvar);
   1159      1.133      matt 	metric(rmx_hopcount);
   1160      1.133      matt 	metric(rmx_mtu);
   1161  1.140.6.3  jdolecek 	metric(rmx_locks);
   1162      1.133      matt #undef metric
   1163  1.140.6.3  jdolecek 	out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
   1164  1.140.6.3  jdolecek 	    time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
   1165      1.133      matt }
   1166      1.133      matt 
   1167       1.42       erh static int
   1168      1.115  christos rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
   1169      1.115  christos     struct rt_addrinfo *rtinfo)
   1170       1.10   mycroft {
   1171       1.69      matt 	const struct sockaddr *sa = NULL;	/* Quell compiler warning */
   1172       1.39  augustss 	int i;
   1173       1.10   mycroft 
   1174      1.112    dyoung 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
   1175       1.10   mycroft 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
   1176       1.10   mycroft 			continue;
   1177      1.117  christos 		rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
   1178      1.133      matt 		RT_XADVANCE(cp, sa);
   1179       1.10   mycroft 	}
   1180       1.44     enami 
   1181      1.115  christos 	/*
   1182      1.115  christos 	 * Check for extra addresses specified, except RTM_GET asking
   1183      1.115  christos 	 * for interface info.
   1184      1.115  christos 	 */
   1185       1.72  christos 	if (rtmtype == RTM_GET) {
   1186      1.115  christos 		if (((rtinfo->rti_addrs &
   1187  1.140.6.3  jdolecek 		    (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
   1188       1.95    dyoung 			return 1;
   1189  1.140.6.3  jdolecek 	} else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
   1190      1.114    dyoung 		return 1;
   1191       1.44     enami 	/* Check for bad data length.  */
   1192       1.44     enami 	if (cp != cplim) {
   1193      1.112    dyoung 		if (i == RTAX_NETMASK + 1 && sa != NULL &&
   1194      1.133      matt 		    cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
   1195       1.44     enami 			/*
   1196      1.114    dyoung 			 * The last sockaddr was info.rti_info[RTAX_NETMASK].
   1197       1.44     enami 			 * We accept this for now for the sake of old
   1198       1.44     enami 			 * binaries or third party softwares.
   1199       1.44     enami 			 */
   1200       1.44     enami 			;
   1201       1.44     enami 		else
   1202       1.95    dyoung 			return 1;
   1203       1.44     enami 	}
   1204       1.95    dyoung 	return 0;
   1205        1.1       cgd }
   1206        1.1       cgd 
   1207      1.132  christos static int
   1208      1.132  christos rt_getlen(int type)
   1209        1.1       cgd {
   1210      1.133      matt #ifndef COMPAT_RTSOCK
   1211      1.133      matt 	CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
   1212      1.133      matt 	CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
   1213      1.133      matt 	CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
   1214      1.133      matt 	CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
   1215      1.133      matt #endif
   1216      1.133      matt 
   1217       1.10   mycroft 	switch (type) {
   1218  1.140.6.3  jdolecek 	case RTM_ODELADDR:
   1219  1.140.6.3  jdolecek 	case RTM_ONEWADDR:
   1220  1.140.6.3  jdolecek 	case RTM_OCHGADDR:
   1221  1.140.6.3  jdolecek #ifdef COMPAT_70
   1222  1.140.6.3  jdolecek 		return sizeof(struct ifa_msghdr70);
   1223  1.140.6.3  jdolecek #else
   1224  1.140.6.3  jdolecek #ifdef RTSOCK_DEBUG
   1225  1.140.6.3  jdolecek 		printf("%s: unsupported RTM type %d\n", __func__, type);
   1226  1.140.6.3  jdolecek #endif
   1227  1.140.6.3  jdolecek 		return -1;
   1228  1.140.6.3  jdolecek #endif
   1229       1.10   mycroft 	case RTM_DELADDR:
   1230       1.10   mycroft 	case RTM_NEWADDR:
   1231      1.131       roy 	case RTM_CHGADDR:
   1232      1.133      matt 		return sizeof(struct ifa_xmsghdr);
   1233       1.10   mycroft 
   1234      1.132  christos 	case RTM_OOIFINFO:
   1235       1.32    bouyer #ifdef COMPAT_14
   1236      1.132  christos 		return sizeof(struct if_msghdr14);
   1237      1.132  christos #else
   1238  1.140.6.3  jdolecek #ifdef RTSOCK_DEBUG
   1239  1.140.6.3  jdolecek 		printf("%s: unsupported RTM type RTM_OOIFINFO\n", __func__);
   1240      1.132  christos #endif
   1241      1.132  christos 		return -1;
   1242      1.120  christos #endif
   1243      1.132  christos 	case RTM_OIFINFO:
   1244      1.120  christos #ifdef COMPAT_50
   1245      1.132  christos 		return sizeof(struct if_msghdr50);
   1246      1.132  christos #else
   1247  1.140.6.3  jdolecek #ifdef RTSOCK_DEBUG
   1248  1.140.6.3  jdolecek 		printf("%s: unsupported RTM type RTM_OIFINFO\n", __func__);
   1249      1.132  christos #endif
   1250      1.132  christos 		return -1;
   1251       1.32    bouyer #endif
   1252       1.32    bouyer 
   1253       1.10   mycroft 	case RTM_IFINFO:
   1254      1.133      matt 		return sizeof(struct if_xmsghdr);
   1255       1.10   mycroft 
   1256       1.36   thorpej 	case RTM_IFANNOUNCE:
   1257       1.78    dyoung 	case RTM_IEEE80211:
   1258      1.133      matt 		return sizeof(struct if_xannouncemsghdr);
   1259       1.36   thorpej 
   1260       1.10   mycroft 	default:
   1261      1.133      matt 		return sizeof(struct rt_xmsghdr);
   1262       1.46    itojun 	}
   1263      1.132  christos }
   1264      1.132  christos 
   1265      1.132  christos 
   1266      1.132  christos struct mbuf *
   1267      1.133      matt COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
   1268      1.132  christos {
   1269      1.133      matt 	struct rt_xmsghdr *rtm;
   1270      1.132  christos 	struct mbuf *m;
   1271      1.132  christos 	int i;
   1272      1.132  christos 	const struct sockaddr *sa;
   1273      1.132  christos 	int len, dlen;
   1274      1.132  christos 
   1275      1.132  christos 	m = m_gethdr(M_DONTWAIT, MT_DATA);
   1276      1.132  christos 	if (m == NULL)
   1277      1.132  christos 		return m;
   1278      1.133      matt 	MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
   1279      1.132  christos 
   1280      1.132  christos 	if ((len = rt_getlen(type)) == -1)
   1281      1.132  christos 		goto out;
   1282       1.47    itojun 	if (len > MHLEN + MLEN)
   1283      1.133      matt 		panic("%s: message too long", __func__);
   1284       1.47    itojun 	else if (len > MHLEN) {
   1285       1.32    bouyer 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
   1286      1.132  christos 		if (m->m_next == NULL)
   1287      1.132  christos 			goto out;
   1288       1.58      matt 		MCLAIM(m->m_next, m->m_owner);
   1289       1.47    itojun 		m->m_pkthdr.len = len;
   1290       1.47    itojun 		m->m_len = MHLEN;
   1291       1.47    itojun 		m->m_next->m_len = len - MHLEN;
   1292       1.47    itojun 	} else {
   1293       1.47    itojun 		m->m_pkthdr.len = m->m_len = len;
   1294       1.32    bouyer 	}
   1295  1.140.6.3  jdolecek 	m_reset_rcvif(m);
   1296       1.32    bouyer 	m_copyback(m, 0, datalen, data);
   1297      1.107  christos 	if (len > datalen)
   1298      1.107  christos 		(void)memset(mtod(m, char *) + datalen, 0, len - datalen);
   1299      1.133      matt 	rtm = mtod(m, struct rt_xmsghdr *);
   1300       1.10   mycroft 	for (i = 0; i < RTAX_MAX; i++) {
   1301       1.10   mycroft 		if ((sa = rtinfo->rti_info[i]) == NULL)
   1302       1.10   mycroft 			continue;
   1303       1.10   mycroft 		rtinfo->rti_addrs |= (1 << i);
   1304      1.133      matt 		dlen = RT_XROUNDUP(sa->sa_len);
   1305      1.133      matt 		m_copyback(m, len, sa->sa_len, sa);
   1306      1.133      matt 		if (dlen != sa->sa_len) {
   1307      1.140  christos 			/*
   1308      1.140  christos 			 * Up to 6 + 1 nul's since roundup is to
   1309      1.140  christos 			 * sizeof(uint64_t) (8 bytes)
   1310      1.140  christos 			 */
   1311      1.133      matt 			m_copyback(m, len + sa->sa_len,
   1312      1.133      matt 			    dlen - sa->sa_len, "\0\0\0\0\0\0");
   1313      1.133      matt 		}
   1314       1.10   mycroft 		len += dlen;
   1315       1.47    itojun 	}
   1316      1.132  christos 	if (m->m_pkthdr.len != len)
   1317      1.132  christos 		goto out;
   1318        1.1       cgd 	rtm->rtm_msglen = len;
   1319      1.133      matt 	rtm->rtm_version = RTM_XVERSION;
   1320        1.1       cgd 	rtm->rtm_type = type;
   1321       1.95    dyoung 	return m;
   1322      1.132  christos out:
   1323      1.132  christos 	m_freem(m);
   1324      1.132  christos 	return NULL;
   1325       1.10   mycroft }
   1326       1.10   mycroft 
   1327       1.29    chopps /*
   1328       1.29    chopps  * rt_msg2
   1329       1.29    chopps  *
   1330       1.29    chopps  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
   1331       1.29    chopps  *		returns the length of the message in 'lenp'.
   1332       1.29    chopps  *
   1333       1.29    chopps  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
   1334       1.29    chopps  *	the message
   1335       1.29    chopps  * otherwise walkarg's w_needed is updated and if the user buffer is
   1336       1.29    chopps  *	specified and w_needed indicates space exists the information is copied
   1337       1.29    chopps  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
   1338       1.29    chopps  *	if the allocation fails ENOBUFS is returned.
   1339       1.29    chopps  */
   1340       1.10   mycroft static int
   1341      1.120  christos rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
   1342       1.69      matt 	int *lenp)
   1343       1.10   mycroft {
   1344       1.39  augustss 	int i;
   1345       1.10   mycroft 	int len, dlen, second_time = 0;
   1346       1.93  christos 	char *cp0, *cp = cpv;
   1347       1.10   mycroft 
   1348       1.10   mycroft 	rtinfo->rti_addrs = 0;
   1349       1.10   mycroft again:
   1350      1.132  christos 	if ((len = rt_getlen(type)) == -1)
   1351      1.132  christos 		return EINVAL;
   1352       1.10   mycroft 
   1353       1.17  christos 	if ((cp0 = cp) != NULL)
   1354       1.10   mycroft 		cp += len;
   1355       1.10   mycroft 	for (i = 0; i < RTAX_MAX; i++) {
   1356       1.68      matt 		const struct sockaddr *sa;
   1357       1.10   mycroft 
   1358       1.95    dyoung 		if ((sa = rtinfo->rti_info[i]) == NULL)
   1359       1.10   mycroft 			continue;
   1360       1.10   mycroft 		rtinfo->rti_addrs |= (1 << i);
   1361      1.133      matt 		dlen = RT_XROUNDUP(sa->sa_len);
   1362       1.10   mycroft 		if (cp) {
   1363      1.140  christos 			int diff = dlen - sa->sa_len;
   1364      1.140  christos 			(void)memcpy(cp, sa, (size_t)sa->sa_len);
   1365      1.140  christos 			cp += sa->sa_len;
   1366      1.140  christos 			if (diff > 0) {
   1367      1.140  christos 				(void)memset(cp, 0, (size_t)diff);
   1368      1.140  christos 				cp += diff;
   1369      1.140  christos 			}
   1370       1.10   mycroft 		}
   1371        1.1       cgd 		len += dlen;
   1372        1.1       cgd 	}
   1373       1.95    dyoung 	if (cp == NULL && w != NULL && !second_time) {
   1374      1.120  christos 		struct rt_walkarg *rw = w;
   1375       1.10   mycroft 
   1376       1.10   mycroft 		rw->w_needed += len;
   1377       1.10   mycroft 		if (rw->w_needed <= 0 && rw->w_where) {
   1378       1.10   mycroft 			if (rw->w_tmemsize < len) {
   1379       1.10   mycroft 				if (rw->w_tmem)
   1380  1.140.6.3  jdolecek 					kmem_free(rw->w_tmem, rw->w_tmemsize);
   1381  1.140.6.3  jdolecek 				rw->w_tmem = kmem_alloc(len, KM_SLEEP);
   1382  1.140.6.3  jdolecek 				rw->w_tmemsize = len;
   1383       1.10   mycroft 			}
   1384       1.10   mycroft 			if (rw->w_tmem) {
   1385       1.10   mycroft 				cp = rw->w_tmem;
   1386       1.10   mycroft 				second_time = 1;
   1387       1.10   mycroft 				goto again;
   1388       1.29    chopps 			} else {
   1389       1.29    chopps 				rw->w_tmemneeded = len;
   1390       1.95    dyoung 				return ENOBUFS;
   1391       1.29    chopps 			}
   1392       1.10   mycroft 		}
   1393        1.1       cgd 	}
   1394       1.10   mycroft 	if (cp) {
   1395      1.133      matt 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
   1396       1.10   mycroft 
   1397      1.133      matt 		rtm->rtm_version = RTM_XVERSION;
   1398       1.10   mycroft 		rtm->rtm_type = type;
   1399       1.10   mycroft 		rtm->rtm_msglen = len;
   1400        1.1       cgd 	}
   1401       1.29    chopps 	if (lenp)
   1402       1.29    chopps 		*lenp = len;
   1403       1.95    dyoung 	return 0;
   1404       1.10   mycroft }
   1405       1.10   mycroft 
   1406  1.140.6.3  jdolecek #ifndef COMPAT_RTSOCK
   1407  1.140.6.3  jdolecek int
   1408  1.140.6.3  jdolecek rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
   1409  1.140.6.3  jdolecek 	int *lenp)
   1410  1.140.6.3  jdolecek {
   1411  1.140.6.3  jdolecek 	return rt_msg2(type, rtinfo, cpv, w, lenp);
   1412  1.140.6.3  jdolecek }
   1413  1.140.6.3  jdolecek #endif
   1414  1.140.6.3  jdolecek 
   1415       1.10   mycroft /*
   1416       1.10   mycroft  * This routine is called to generate a message from the routing
   1417       1.51       wiz  * socket indicating that a redirect has occurred, a routing lookup
   1418       1.10   mycroft  * has failed, or that a protocol has detected timeouts to a particular
   1419       1.10   mycroft  * destination.
   1420       1.10   mycroft  */
   1421       1.10   mycroft void
   1422      1.133      matt COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
   1423      1.133      matt     int error)
   1424       1.10   mycroft {
   1425      1.133      matt 	struct rt_xmsghdr rtm;
   1426       1.39  augustss 	struct mbuf *m;
   1427       1.68      matt 	const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
   1428      1.133      matt 	struct rt_addrinfo info = *rtinfo;
   1429       1.10   mycroft 
   1430      1.133      matt 	COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
   1431      1.133      matt 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1432       1.10   mycroft 		return;
   1433       1.48   thorpej 	memset(&rtm, 0, sizeof(rtm));
   1434  1.140.6.3  jdolecek 	rtm.rtm_pid = curproc->p_pid;
   1435       1.32    bouyer 	rtm.rtm_flags = RTF_DONE | flags;
   1436       1.32    bouyer 	rtm.rtm_errno = error;
   1437      1.133      matt 	m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
   1438       1.95    dyoung 	if (m == NULL)
   1439        1.1       cgd 		return;
   1440      1.133      matt 	mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
   1441      1.133      matt 	COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
   1442       1.10   mycroft }
   1443       1.10   mycroft 
   1444       1.10   mycroft /*
   1445       1.10   mycroft  * This routine is called to generate a message from the routing
   1446       1.10   mycroft  * socket indicating that the status of a network interface has changed.
   1447       1.10   mycroft  */
   1448       1.10   mycroft void
   1449      1.133      matt COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
   1450       1.10   mycroft {
   1451      1.133      matt 	struct if_xmsghdr ifm;
   1452       1.10   mycroft 	struct mbuf *m;
   1453       1.10   mycroft 	struct rt_addrinfo info;
   1454       1.10   mycroft 
   1455      1.133      matt 	COMPATCALL(rt_ifmsg, (ifp));
   1456      1.133      matt 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1457       1.10   mycroft 		return;
   1458      1.120  christos 	(void)memset(&info, 0, sizeof(info));
   1459      1.120  christos 	(void)memset(&ifm, 0, sizeof(ifm));
   1460       1.32    bouyer 	ifm.ifm_index = ifp->if_index;
   1461       1.32    bouyer 	ifm.ifm_flags = ifp->if_flags;
   1462       1.32    bouyer 	ifm.ifm_data = ifp->if_data;
   1463       1.32    bouyer 	ifm.ifm_addrs = 0;
   1464      1.133      matt 	m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
   1465       1.95    dyoung 	if (m == NULL)
   1466       1.32    bouyer 		return;
   1467      1.133      matt 	COMPATNAME(route_enqueue)(m, 0);
   1468       1.32    bouyer #ifdef COMPAT_14
   1469      1.133      matt 	compat_14_rt_oifmsg(ifp);
   1470      1.120  christos #endif
   1471      1.120  christos #ifdef COMPAT_50
   1472      1.133      matt 	compat_50_rt_oifmsg(ifp);
   1473       1.32    bouyer #endif
   1474        1.1       cgd }
   1475        1.1       cgd 
   1476  1.140.6.3  jdolecek #ifndef COMPAT_RTSOCK
   1477  1.140.6.3  jdolecek static int
   1478  1.140.6.3  jdolecek if_addrflags(struct ifaddr *ifa)
   1479  1.140.6.3  jdolecek {
   1480  1.140.6.3  jdolecek 
   1481  1.140.6.3  jdolecek 	switch (ifa->ifa_addr->sa_family) {
   1482  1.140.6.3  jdolecek #ifdef INET
   1483  1.140.6.3  jdolecek 	case AF_INET:
   1484  1.140.6.3  jdolecek 		return ((struct in_ifaddr *)ifa)->ia4_flags;
   1485  1.140.6.3  jdolecek #endif
   1486  1.140.6.3  jdolecek #ifdef INET6
   1487  1.140.6.3  jdolecek 	case AF_INET6:
   1488  1.140.6.3  jdolecek 		return ((struct in6_ifaddr *)ifa)->ia6_flags;
   1489  1.140.6.3  jdolecek #endif
   1490  1.140.6.3  jdolecek 	default:
   1491  1.140.6.3  jdolecek 		return 0;
   1492  1.140.6.3  jdolecek 	}
   1493  1.140.6.3  jdolecek }
   1494  1.140.6.3  jdolecek #endif
   1495      1.120  christos 
   1496        1.1       cgd /*
   1497       1.10   mycroft  * This is called to generate messages from the routing socket
   1498       1.10   mycroft  * indicating a network interface has had addresses associated with it.
   1499       1.10   mycroft  * if we ever reverse the logic and replace messages TO the routing
   1500       1.10   mycroft  * socket indicate a request to configure interfaces, then it will
   1501       1.10   mycroft  * be unnecessary as the routing socket will automatically generate
   1502       1.10   mycroft  * copies of it.
   1503       1.10   mycroft  */
   1504       1.10   mycroft void
   1505      1.133      matt COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
   1506      1.133      matt     struct rtentry *rt)
   1507       1.10   mycroft {
   1508      1.116    dyoung #define	cmdpass(__cmd, __pass)	(((__cmd) << 2) | (__pass))
   1509       1.10   mycroft 	struct rt_addrinfo info;
   1510      1.116    dyoung 	const struct sockaddr *sa;
   1511       1.10   mycroft 	int pass;
   1512      1.116    dyoung 	struct mbuf *m;
   1513      1.139  christos 	struct ifnet *ifp;
   1514      1.133      matt 	struct rt_xmsghdr rtm;
   1515      1.133      matt 	struct ifa_xmsghdr ifam;
   1516      1.116    dyoung 	int ncmd;
   1517       1.10   mycroft 
   1518      1.139  christos 	KASSERT(ifa != NULL);
   1519  1.140.6.3  jdolecek 	KASSERT(ifa->ifa_addr != NULL);
   1520      1.139  christos 	ifp = ifa->ifa_ifp;
   1521  1.140.6.3  jdolecek #ifdef SCTP
   1522  1.140.6.3  jdolecek 	if (cmd == RTM_ADD) {
   1523  1.140.6.3  jdolecek 		sctp_add_ip_address(ifa);
   1524  1.140.6.3  jdolecek 	} else if (cmd == RTM_DELETE) {
   1525  1.140.6.3  jdolecek 		sctp_delete_ip_address(ifa);
   1526  1.140.6.3  jdolecek 	}
   1527  1.140.6.3  jdolecek #endif
   1528  1.140.6.3  jdolecek 
   1529      1.133      matt 	COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
   1530      1.133      matt 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1531       1.10   mycroft 		return;
   1532       1.10   mycroft 	for (pass = 1; pass < 3; pass++) {
   1533       1.48   thorpej 		memset(&info, 0, sizeof(info));
   1534      1.116    dyoung 		switch (cmdpass(cmd, pass)) {
   1535      1.116    dyoung 		case cmdpass(RTM_ADD, 1):
   1536      1.116    dyoung 		case cmdpass(RTM_CHANGE, 1):
   1537      1.116    dyoung 		case cmdpass(RTM_DELETE, 2):
   1538      1.138       roy 		case cmdpass(RTM_NEWADDR, 1):
   1539      1.138       roy 		case cmdpass(RTM_DELADDR, 1):
   1540      1.138       roy 		case cmdpass(RTM_CHGADDR, 1):
   1541      1.131       roy 			switch (cmd) {
   1542      1.138       roy 			case RTM_ADD:
   1543  1.140.6.3  jdolecek 				ncmd = RTM_XNEWADDR;
   1544      1.138       roy 				break;
   1545      1.131       roy 			case RTM_DELETE:
   1546  1.140.6.3  jdolecek 				ncmd = RTM_XDELADDR;
   1547      1.131       roy 				break;
   1548      1.131       roy 			case RTM_CHANGE:
   1549  1.140.6.3  jdolecek 				ncmd = RTM_XCHGADDR;
   1550  1.140.6.3  jdolecek 				break;
   1551  1.140.6.3  jdolecek 			case RTM_NEWADDR:
   1552  1.140.6.3  jdolecek 				ncmd = RTM_XNEWADDR;
   1553  1.140.6.3  jdolecek 				break;
   1554  1.140.6.3  jdolecek 			case RTM_DELADDR:
   1555  1.140.6.3  jdolecek 				ncmd = RTM_XDELADDR;
   1556  1.140.6.3  jdolecek 				break;
   1557  1.140.6.3  jdolecek 			case RTM_CHGADDR:
   1558  1.140.6.3  jdolecek 				ncmd = RTM_XCHGADDR;
   1559      1.131       roy 				break;
   1560      1.131       roy 			default:
   1561  1.140.6.3  jdolecek 				panic("%s: unknown command %d", __func__, cmd);
   1562      1.131       roy 			}
   1563  1.140.6.3  jdolecek #ifdef COMPAT_70
   1564  1.140.6.3  jdolecek 			compat_70_rt_newaddrmsg1(ncmd, ifa);
   1565  1.140.6.3  jdolecek #endif
   1566      1.114    dyoung 			info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
   1567      1.139  christos 			KASSERT(ifp->if_dl != NULL);
   1568      1.114    dyoung 			info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   1569      1.114    dyoung 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1570      1.114    dyoung 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   1571       1.48   thorpej 			memset(&ifam, 0, sizeof(ifam));
   1572       1.32    bouyer 			ifam.ifam_index = ifp->if_index;
   1573       1.32    bouyer 			ifam.ifam_metric = ifa->ifa_metric;
   1574       1.32    bouyer 			ifam.ifam_flags = ifa->ifa_flags;
   1575  1.140.6.3  jdolecek #ifndef COMPAT_RTSOCK
   1576  1.140.6.3  jdolecek 			ifam.ifam_pid = curproc->p_pid;
   1577  1.140.6.3  jdolecek 			ifam.ifam_addrflags = if_addrflags(ifa);
   1578  1.140.6.3  jdolecek #endif
   1579      1.133      matt 			m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
   1580       1.32    bouyer 			if (m == NULL)
   1581       1.10   mycroft 				continue;
   1582      1.133      matt 			mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
   1583       1.32    bouyer 			    info.rti_addrs;
   1584      1.116    dyoung 			break;
   1585      1.116    dyoung 		case cmdpass(RTM_ADD, 2):
   1586      1.116    dyoung 		case cmdpass(RTM_CHANGE, 2):
   1587      1.116    dyoung 		case cmdpass(RTM_DELETE, 1):
   1588       1.95    dyoung 			if (rt == NULL)
   1589       1.10   mycroft 				continue;
   1590      1.114    dyoung 			info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1591      1.114    dyoung 			info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
   1592      1.114    dyoung 			info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1593       1.48   thorpej 			memset(&rtm, 0, sizeof(rtm));
   1594  1.140.6.3  jdolecek 			rtm.rtm_pid = curproc->p_pid;
   1595       1.32    bouyer 			rtm.rtm_index = ifp->if_index;
   1596       1.32    bouyer 			rtm.rtm_flags |= rt->rt_flags;
   1597       1.32    bouyer 			rtm.rtm_errno = error;
   1598      1.133      matt 			m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
   1599       1.32    bouyer 			if (m == NULL)
   1600       1.10   mycroft 				continue;
   1601      1.133      matt 			mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
   1602      1.116    dyoung 			break;
   1603      1.116    dyoung 		default:
   1604      1.116    dyoung 			continue;
   1605       1.10   mycroft 		}
   1606  1.140.6.3  jdolecek 		KASSERTMSG(m != NULL, "called with wrong command");
   1607      1.133      matt 		COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
   1608       1.10   mycroft 	}
   1609      1.116    dyoung #undef cmdpass
   1610  1.140.6.3  jdolecek 
   1611       1.36   thorpej }
   1612       1.36   thorpej 
   1613       1.78    dyoung static struct mbuf *
   1614       1.78    dyoung rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
   1615       1.78    dyoung     struct rt_addrinfo *info)
   1616       1.78    dyoung {
   1617      1.133      matt 	struct if_xannouncemsghdr ifan;
   1618       1.78    dyoung 
   1619       1.78    dyoung 	memset(info, 0, sizeof(*info));
   1620       1.78    dyoung 	memset(&ifan, 0, sizeof(ifan));
   1621       1.78    dyoung 	ifan.ifan_index = ifp->if_index;
   1622       1.78    dyoung 	strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
   1623       1.78    dyoung 	ifan.ifan_what = what;
   1624      1.133      matt 	return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
   1625       1.78    dyoung }
   1626       1.78    dyoung 
   1627       1.36   thorpej /*
   1628       1.36   thorpej  * This is called to generate routing socket messages indicating
   1629       1.36   thorpej  * network interface arrival and departure.
   1630       1.36   thorpej  */
   1631       1.36   thorpej void
   1632      1.133      matt COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
   1633       1.36   thorpej {
   1634       1.36   thorpej 	struct mbuf *m;
   1635       1.36   thorpej 	struct rt_addrinfo info;
   1636       1.36   thorpej 
   1637      1.133      matt 	COMPATCALL(rt_ifannouncemsg, (ifp, what));
   1638      1.133      matt 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1639       1.36   thorpej 		return;
   1640       1.78    dyoung 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
   1641       1.78    dyoung 	if (m == NULL)
   1642       1.78    dyoung 		return;
   1643      1.133      matt 	COMPATNAME(route_enqueue)(m, 0);
   1644       1.78    dyoung }
   1645       1.78    dyoung 
   1646       1.78    dyoung /*
   1647       1.78    dyoung  * This is called to generate routing socket messages indicating
   1648       1.78    dyoung  * IEEE80211 wireless events.
   1649       1.78    dyoung  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
   1650       1.78    dyoung  */
   1651       1.78    dyoung void
   1652      1.133      matt COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
   1653      1.133      matt 	size_t data_len)
   1654       1.78    dyoung {
   1655       1.78    dyoung 	struct mbuf *m;
   1656       1.78    dyoung 	struct rt_addrinfo info;
   1657       1.78    dyoung 
   1658      1.133      matt 	COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
   1659      1.133      matt 	if (COMPATNAME(route_info).ri_cb.any_count == 0)
   1660       1.78    dyoung 		return;
   1661       1.78    dyoung 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
   1662       1.78    dyoung 	if (m == NULL)
   1663       1.36   thorpej 		return;
   1664       1.78    dyoung 	/*
   1665       1.78    dyoung 	 * Append the ieee80211 data.  Try to stick it in the
   1666       1.78    dyoung 	 * mbuf containing the ifannounce msg; otherwise allocate
   1667       1.78    dyoung 	 * a new mbuf and append.
   1668       1.78    dyoung 	 *
   1669       1.78    dyoung 	 * NB: we assume m is a single mbuf.
   1670       1.78    dyoung 	 */
   1671       1.78    dyoung 	if (data_len > M_TRAILINGSPACE(m)) {
   1672       1.78    dyoung 		struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
   1673       1.78    dyoung 		if (n == NULL) {
   1674       1.78    dyoung 			m_freem(m);
   1675       1.78    dyoung 			return;
   1676       1.78    dyoung 		}
   1677       1.78    dyoung 		(void)memcpy(mtod(n, void *), data, data_len);
   1678       1.78    dyoung 		n->m_len = data_len;
   1679       1.78    dyoung 		m->m_next = n;
   1680       1.78    dyoung 	} else if (data_len > 0) {
   1681       1.98      matt 		(void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
   1682       1.78    dyoung 		m->m_len += data_len;
   1683       1.78    dyoung 	}
   1684       1.78    dyoung 	if (m->m_flags & M_PKTHDR)
   1685       1.78    dyoung 		m->m_pkthdr.len += data_len;
   1686      1.133      matt 	mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
   1687      1.133      matt 	COMPATNAME(route_enqueue)(m, 0);
   1688       1.10   mycroft }
   1689       1.10   mycroft 
   1690  1.140.6.3  jdolecek #ifndef COMPAT_RTSOCK
   1691  1.140.6.3  jdolecek /*
   1692  1.140.6.3  jdolecek  * Send a routing message as mimicing that a cloned route is added.
   1693  1.140.6.3  jdolecek  */
   1694  1.140.6.3  jdolecek void
   1695  1.140.6.3  jdolecek rt_clonedmsg(const struct sockaddr *dst, const struct ifnet *ifp,
   1696  1.140.6.3  jdolecek     const struct rtentry *rt)
   1697  1.140.6.3  jdolecek {
   1698  1.140.6.3  jdolecek 	struct rt_addrinfo info;
   1699  1.140.6.3  jdolecek 	/* Mimic flags exactly */
   1700  1.140.6.3  jdolecek #define RTF_LLINFO	0x400
   1701  1.140.6.3  jdolecek #define RTF_CLONED	0x2000
   1702  1.140.6.3  jdolecek 	int flags = RTF_UP | RTF_HOST | RTF_DONE | RTF_LLINFO | RTF_CLONED;
   1703  1.140.6.3  jdolecek 	union {
   1704  1.140.6.3  jdolecek 		struct sockaddr sa;
   1705  1.140.6.3  jdolecek 		struct sockaddr_storage ss;
   1706  1.140.6.3  jdolecek 		struct sockaddr_dl sdl;
   1707  1.140.6.3  jdolecek 	} u;
   1708  1.140.6.3  jdolecek 	uint8_t namelen = strlen(ifp->if_xname);
   1709  1.140.6.3  jdolecek 	uint8_t addrlen = ifp->if_addrlen;
   1710  1.140.6.3  jdolecek 
   1711  1.140.6.3  jdolecek 	if (rt == NULL)
   1712  1.140.6.3  jdolecek 		return; /* XXX */
   1713  1.140.6.3  jdolecek 
   1714  1.140.6.3  jdolecek 	memset(&info, 0, sizeof(info));
   1715  1.140.6.3  jdolecek 	info.rti_info[RTAX_DST] = dst;
   1716  1.140.6.3  jdolecek 	sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type,
   1717  1.140.6.3  jdolecek 	    NULL, namelen, NULL, addrlen);
   1718  1.140.6.3  jdolecek 	info.rti_info[RTAX_GATEWAY] = &u.sa;
   1719  1.140.6.3  jdolecek 
   1720  1.140.6.3  jdolecek 	rt_missmsg(RTM_ADD, &info, flags, 0);
   1721  1.140.6.3  jdolecek #undef RTF_LLINFO
   1722  1.140.6.3  jdolecek #undef RTF_CLONED
   1723  1.140.6.3  jdolecek }
   1724  1.140.6.3  jdolecek #endif /* COMPAT_RTSOCK */
   1725  1.140.6.3  jdolecek 
   1726       1.10   mycroft /*
   1727       1.10   mycroft  * This is used in dumping the kernel table via sysctl().
   1728        1.1       cgd  */
   1729       1.40    simonb static int
   1730       1.94    dyoung sysctl_dumpentry(struct rtentry *rt, void *v)
   1731        1.1       cgd {
   1732      1.120  christos 	struct rt_walkarg *w = v;
   1733       1.10   mycroft 	int error = 0, size;
   1734       1.10   mycroft 	struct rt_addrinfo info;
   1735        1.1       cgd 
   1736       1.10   mycroft 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
   1737       1.10   mycroft 		return 0;
   1738       1.48   thorpej 	memset(&info, 0, sizeof(info));
   1739      1.114    dyoung 	info.rti_info[RTAX_DST] = rt_getkey(rt);
   1740      1.114    dyoung 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
   1741      1.114    dyoung 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
   1742  1.140.6.2       tls 	info.rti_info[RTAX_TAG] = rt_gettag(rt);
   1743       1.16       cgd 	if (rt->rt_ifp) {
   1744       1.91    dyoung 		const struct ifaddr *rtifa;
   1745      1.114    dyoung 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
   1746       1.91    dyoung 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
   1747       1.91    dyoung 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
   1748       1.91    dyoung 		 * --dyoung
   1749       1.91    dyoung 		 */
   1750       1.91    dyoung 		rtifa = rt_get_ifa(rt);
   1751      1.114    dyoung 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
   1752       1.16       cgd 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
   1753      1.114    dyoung 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
   1754       1.16       cgd 	}
   1755       1.29    chopps 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
   1756       1.95    dyoung 		return error;
   1757       1.29    chopps 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1758      1.133      matt 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
   1759       1.10   mycroft 
   1760       1.10   mycroft 		rtm->rtm_flags = rt->rt_flags;
   1761       1.10   mycroft 		rtm->rtm_use = rt->rt_use;
   1762      1.133      matt 		rtm_setmetrics(rt, rtm);
   1763       1.83  christos 		KASSERT(rt->rt_ifp != NULL);
   1764       1.10   mycroft 		rtm->rtm_index = rt->rt_ifp->if_index;
   1765       1.10   mycroft 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
   1766       1.10   mycroft 		rtm->rtm_addrs = info.rti_addrs;
   1767       1.21  christos 		if ((error = copyout(rtm, w->w_where, size)) != 0)
   1768       1.10   mycroft 			w->w_where = NULL;
   1769       1.10   mycroft 		else
   1770       1.93  christos 			w->w_where = (char *)w->w_where + size;
   1771       1.10   mycroft 	}
   1772       1.95    dyoung 	return error;
   1773       1.10   mycroft }
   1774        1.1       cgd 
   1775       1.40    simonb static int
   1776  1.140.6.3  jdolecek sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
   1777  1.140.6.3  jdolecek     struct rt_addrinfo *info, size_t len)
   1778  1.140.6.3  jdolecek {
   1779  1.140.6.3  jdolecek 	struct if_xmsghdr *ifm;
   1780  1.140.6.3  jdolecek 	int error;
   1781  1.140.6.3  jdolecek 
   1782  1.140.6.3  jdolecek 	ifm = (struct if_xmsghdr *)w->w_tmem;
   1783  1.140.6.3  jdolecek 	ifm->ifm_index = ifp->if_index;
   1784  1.140.6.3  jdolecek 	ifm->ifm_flags = ifp->if_flags;
   1785  1.140.6.3  jdolecek 	ifm->ifm_data = ifp->if_data;
   1786  1.140.6.3  jdolecek 	ifm->ifm_addrs = info->rti_addrs;
   1787  1.140.6.3  jdolecek 	if ((error = copyout(ifm, w->w_where, len)) == 0)
   1788  1.140.6.3  jdolecek 		w->w_where = (char *)w->w_where + len;
   1789  1.140.6.3  jdolecek 	return error;
   1790  1.140.6.3  jdolecek }
   1791  1.140.6.3  jdolecek 
   1792  1.140.6.3  jdolecek static int
   1793  1.140.6.3  jdolecek sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
   1794  1.140.6.3  jdolecek      struct rt_addrinfo *info)
   1795  1.140.6.3  jdolecek {
   1796  1.140.6.3  jdolecek 	int len, error;
   1797  1.140.6.3  jdolecek 
   1798  1.140.6.3  jdolecek 	if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
   1799  1.140.6.3  jdolecek 		return error;
   1800  1.140.6.3  jdolecek 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1801  1.140.6.3  jdolecek 		struct ifa_xmsghdr *ifam;
   1802  1.140.6.3  jdolecek 
   1803  1.140.6.3  jdolecek 		ifam = (struct ifa_xmsghdr *)w->w_tmem;
   1804  1.140.6.3  jdolecek 		ifam->ifam_index = ifa->ifa_ifp->if_index;
   1805  1.140.6.3  jdolecek 		ifam->ifam_flags = ifa->ifa_flags;
   1806  1.140.6.3  jdolecek 		ifam->ifam_metric = ifa->ifa_metric;
   1807  1.140.6.3  jdolecek 		ifam->ifam_addrs = info->rti_addrs;
   1808  1.140.6.3  jdolecek #ifndef COMPAT_RTSOCK
   1809  1.140.6.3  jdolecek 		ifam->ifam_pid = 0;
   1810  1.140.6.3  jdolecek 		ifam->ifam_addrflags = if_addrflags(ifa);
   1811  1.140.6.3  jdolecek #endif
   1812  1.140.6.3  jdolecek 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
   1813  1.140.6.3  jdolecek 			w->w_where = (char *)w->w_where + len;
   1814  1.140.6.3  jdolecek 	}
   1815  1.140.6.3  jdolecek 	return error;
   1816  1.140.6.3  jdolecek }
   1817  1.140.6.3  jdolecek 
   1818  1.140.6.3  jdolecek static int
   1819      1.120  christos sysctl_iflist(int af, struct rt_walkarg *w, int type)
   1820       1.10   mycroft {
   1821       1.39  augustss 	struct ifnet *ifp;
   1822       1.39  augustss 	struct ifaddr *ifa;
   1823       1.10   mycroft 	struct	rt_addrinfo info;
   1824  1.140.6.3  jdolecek 	int	cmd, len, error = 0;
   1825  1.140.6.3  jdolecek 	int	(*iflist_if)(struct ifnet *, struct rt_walkarg *,
   1826  1.140.6.3  jdolecek 			     struct rt_addrinfo *, size_t);
   1827  1.140.6.3  jdolecek 	int	(*iflist_addr)(struct rt_walkarg *, struct ifaddr *,
   1828  1.140.6.3  jdolecek 			       struct rt_addrinfo *);
   1829  1.140.6.3  jdolecek 	int s;
   1830  1.140.6.3  jdolecek 	struct psref psref;
   1831  1.140.6.3  jdolecek 	int bound;
   1832  1.140.6.3  jdolecek 
   1833  1.140.6.3  jdolecek 	switch (type) {
   1834  1.140.6.3  jdolecek 	case NET_RT_IFLIST:
   1835  1.140.6.3  jdolecek 		cmd = RTM_IFINFO;
   1836  1.140.6.3  jdolecek 		iflist_if = sysctl_iflist_if;
   1837  1.140.6.3  jdolecek 		iflist_addr = sysctl_iflist_addr;
   1838  1.140.6.3  jdolecek 		break;
   1839  1.140.6.3  jdolecek #ifdef COMPAT_14
   1840  1.140.6.3  jdolecek 	case NET_RT_OOOIFLIST:
   1841  1.140.6.3  jdolecek 		cmd = RTM_OOIFINFO;
   1842  1.140.6.3  jdolecek 		iflist_if = compat_14_iflist;
   1843  1.140.6.3  jdolecek 		iflist_addr = compat_70_iflist_addr;
   1844  1.140.6.3  jdolecek 		break;
   1845  1.140.6.3  jdolecek #endif
   1846  1.140.6.3  jdolecek #ifdef COMPAT_50
   1847  1.140.6.3  jdolecek 	case NET_RT_OOIFLIST:
   1848  1.140.6.3  jdolecek 		cmd = RTM_OIFINFO;
   1849  1.140.6.3  jdolecek 		iflist_if = compat_50_iflist;
   1850  1.140.6.3  jdolecek 		iflist_addr = compat_70_iflist_addr;
   1851  1.140.6.3  jdolecek 		break;
   1852  1.140.6.3  jdolecek #endif
   1853  1.140.6.3  jdolecek #ifdef COMPAT_70
   1854  1.140.6.3  jdolecek 	case NET_RT_OIFLIST:
   1855  1.140.6.3  jdolecek 		cmd = RTM_IFINFO;
   1856  1.140.6.3  jdolecek 		iflist_if = sysctl_iflist_if;
   1857  1.140.6.3  jdolecek 		iflist_addr = compat_70_iflist_addr;
   1858  1.140.6.3  jdolecek 		break;
   1859  1.140.6.3  jdolecek #endif
   1860  1.140.6.3  jdolecek 	default:
   1861  1.140.6.3  jdolecek #ifdef RTSOCK_DEBUG
   1862  1.140.6.3  jdolecek 		printf("%s: unsupported IFLIST type %d\n", __func__, type);
   1863  1.140.6.3  jdolecek #endif
   1864  1.140.6.3  jdolecek 		return EINVAL;
   1865  1.140.6.3  jdolecek 	}
   1866       1.10   mycroft 
   1867       1.48   thorpej 	memset(&info, 0, sizeof(info));
   1868  1.140.6.3  jdolecek 
   1869  1.140.6.3  jdolecek 	bound = curlwp_bind();
   1870  1.140.6.3  jdolecek 	s = pserialize_read_enter();
   1871  1.140.6.3  jdolecek 	IFNET_READER_FOREACH(ifp) {
   1872  1.140.6.3  jdolecek 		int _s;
   1873       1.10   mycroft 		if (w->w_arg && w->w_arg != ifp->if_index)
   1874       1.10   mycroft 			continue;
   1875  1.140.6.3  jdolecek 		if (IFADDR_READER_EMPTY(ifp))
   1876       1.81    rpaulo 			continue;
   1877  1.140.6.3  jdolecek 
   1878  1.140.6.3  jdolecek 		if_acquire(ifp, &psref);
   1879  1.140.6.3  jdolecek 		pserialize_read_exit(s);
   1880  1.140.6.3  jdolecek 
   1881      1.114    dyoung 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
   1882  1.140.6.3  jdolecek 		if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
   1883  1.140.6.3  jdolecek 			goto release_exit;
   1884      1.114    dyoung 		info.rti_info[RTAX_IFP] = NULL;
   1885       1.29    chopps 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
   1886  1.140.6.3  jdolecek 			if ((error = iflist_if(ifp, w, &info, len)) != 0)
   1887  1.140.6.3  jdolecek 				goto release_exit;
   1888       1.10   mycroft 		}
   1889  1.140.6.3  jdolecek 		_s = pserialize_read_enter();
   1890  1.140.6.3  jdolecek 		IFADDR_READER_FOREACH(ifa, ifp) {
   1891  1.140.6.3  jdolecek 			struct psref _psref;
   1892       1.10   mycroft 			if (af && af != ifa->ifa_addr->sa_family)
   1893       1.10   mycroft 				continue;
   1894  1.140.6.3  jdolecek 			ifa_acquire(ifa, &_psref);
   1895  1.140.6.3  jdolecek 			pserialize_read_exit(_s);
   1896  1.140.6.3  jdolecek 
   1897      1.114    dyoung 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
   1898      1.114    dyoung 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
   1899      1.114    dyoung 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
   1900  1.140.6.3  jdolecek 			error = iflist_addr(w, ifa, &info);
   1901  1.140.6.3  jdolecek 
   1902  1.140.6.3  jdolecek 			_s = pserialize_read_enter();
   1903  1.140.6.3  jdolecek 			ifa_release(ifa, &_psref);
   1904  1.140.6.3  jdolecek 			if (error != 0) {
   1905  1.140.6.3  jdolecek 				pserialize_read_exit(_s);
   1906  1.140.6.3  jdolecek 				goto release_exit;
   1907       1.10   mycroft 			}
   1908       1.10   mycroft 		}
   1909  1.140.6.3  jdolecek 		pserialize_read_exit(_s);
   1910      1.115  christos 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
   1911      1.115  christos 		    info.rti_info[RTAX_BRD] = NULL;
   1912  1.140.6.3  jdolecek 
   1913  1.140.6.3  jdolecek 		s = pserialize_read_enter();
   1914  1.140.6.3  jdolecek 		if_release(ifp, &psref);
   1915       1.10   mycroft 	}
   1916  1.140.6.3  jdolecek 	pserialize_read_exit(s);
   1917  1.140.6.3  jdolecek 	curlwp_bindx(bound);
   1918  1.140.6.3  jdolecek 
   1919       1.95    dyoung 	return 0;
   1920  1.140.6.3  jdolecek 
   1921  1.140.6.3  jdolecek release_exit:
   1922  1.140.6.3  jdolecek 	if_release(ifp, &psref);
   1923  1.140.6.3  jdolecek 	curlwp_bindx(bound);
   1924  1.140.6.3  jdolecek 	return error;
   1925        1.1       cgd }
   1926        1.1       cgd 
   1927       1.40    simonb static int
   1928       1.65    atatat sysctl_rtable(SYSCTLFN_ARGS)
   1929        1.1       cgd {
   1930       1.65    atatat 	void 	*where = oldp;
   1931       1.65    atatat 	size_t	*given = oldlenp;
   1932       1.10   mycroft 	int	i, s, error = EINVAL;
   1933       1.10   mycroft 	u_char  af;
   1934      1.120  christos 	struct	rt_walkarg w;
   1935        1.1       cgd 
   1936       1.66    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
   1937       1.95    dyoung 		return sysctl_query(SYSCTLFN_CALL(rnode));
   1938       1.66    atatat 
   1939  1.140.6.3  jdolecek 	if (newp)
   1940       1.95    dyoung 		return EPERM;
   1941       1.10   mycroft 	if (namelen != 3)
   1942       1.95    dyoung 		return EINVAL;
   1943       1.10   mycroft 	af = name[0];
   1944       1.29    chopps 	w.w_tmemneeded = 0;
   1945       1.29    chopps 	w.w_tmemsize = 0;
   1946       1.29    chopps 	w.w_tmem = NULL;
   1947       1.29    chopps again:
   1948       1.29    chopps 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
   1949       1.29    chopps 	if (w.w_tmemneeded) {
   1950  1.140.6.3  jdolecek 		w.w_tmem = kmem_alloc(w.w_tmemneeded, KM_SLEEP);
   1951       1.29    chopps 		w.w_tmemsize = w.w_tmemneeded;
   1952       1.29    chopps 		w.w_tmemneeded = 0;
   1953       1.29    chopps 	}
   1954       1.29    chopps 	w.w_op = name[1];
   1955       1.29    chopps 	w.w_arg = name[2];
   1956       1.10   mycroft 	w.w_given = *given;
   1957        1.1       cgd 	w.w_needed = 0 - w.w_given;
   1958       1.29    chopps 	w.w_where = where;
   1959        1.1       cgd 
   1960       1.14   mycroft 	s = splsoftnet();
   1961       1.10   mycroft 	switch (w.w_op) {
   1962       1.10   mycroft 
   1963       1.10   mycroft 	case NET_RT_DUMP:
   1964       1.10   mycroft 	case NET_RT_FLAGS:
   1965  1.140.6.3  jdolecek #if defined(INET) || defined(INET6)
   1966  1.140.6.3  jdolecek 		/*
   1967  1.140.6.3  jdolecek 		 * take care of llinfo entries, the caller must
   1968  1.140.6.3  jdolecek 		 * specify an AF
   1969  1.140.6.3  jdolecek 		 */
   1970  1.140.6.3  jdolecek 		if (w.w_op == NET_RT_FLAGS &&
   1971  1.140.6.3  jdolecek 		    (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
   1972  1.140.6.3  jdolecek 			if (af != 0)
   1973  1.140.6.3  jdolecek 				error = lltable_sysctl_dump(af, &w);
   1974  1.140.6.3  jdolecek 			else
   1975  1.140.6.3  jdolecek 				error = EINVAL;
   1976  1.140.6.3  jdolecek 			break;
   1977  1.140.6.3  jdolecek 		}
   1978  1.140.6.3  jdolecek #endif
   1979  1.140.6.3  jdolecek 
   1980  1.140.6.3  jdolecek 		for (i = 1; i <= AF_MAX; i++) {
   1981  1.140.6.3  jdolecek 			if (af == 0 || af == i) {
   1982  1.140.6.3  jdolecek 				error = rt_walktree(i, sysctl_dumpentry, &w);
   1983  1.140.6.3  jdolecek 				if (error != 0)
   1984  1.140.6.3  jdolecek 					break;
   1985  1.140.6.3  jdolecek #if defined(INET) || defined(INET6)
   1986  1.140.6.3  jdolecek 				/*
   1987  1.140.6.3  jdolecek 				 * Return ARP/NDP entries too for
   1988  1.140.6.3  jdolecek 				 * backward compatibility.
   1989  1.140.6.3  jdolecek 				 */
   1990  1.140.6.3  jdolecek 				error = lltable_sysctl_dump(i, &w);
   1991  1.140.6.3  jdolecek 				if (error != 0)
   1992  1.140.6.3  jdolecek 					break;
   1993  1.140.6.3  jdolecek #endif
   1994  1.140.6.3  jdolecek 			}
   1995  1.140.6.3  jdolecek 		}
   1996       1.10   mycroft 		break;
   1997       1.10   mycroft 
   1998       1.32    bouyer #ifdef COMPAT_14
   1999  1.140.6.3  jdolecek 	case NET_RT_OOOIFLIST:
   2000      1.120  christos 		error = sysctl_iflist(af, &w, w.w_op);
   2001      1.120  christos 		break;
   2002      1.120  christos #endif
   2003      1.120  christos #ifdef COMPAT_50
   2004  1.140.6.3  jdolecek 	case NET_RT_OOIFLIST:
   2005  1.140.6.3  jdolecek 		error = sysctl_iflist(af, &w, w.w_op);
   2006  1.140.6.3  jdolecek 		break;
   2007  1.140.6.3  jdolecek #endif
   2008  1.140.6.3  jdolecek #ifdef COMPAT_70
   2009       1.32    bouyer 	case NET_RT_OIFLIST:
   2010       1.32    bouyer 		error = sysctl_iflist(af, &w, w.w_op);
   2011       1.32    bouyer 		break;
   2012       1.32    bouyer #endif
   2013       1.10   mycroft 	case NET_RT_IFLIST:
   2014       1.32    bouyer 		error = sysctl_iflist(af, &w, w.w_op);
   2015      1.133      matt 		break;
   2016        1.1       cgd 	}
   2017       1.10   mycroft 	splx(s);
   2018       1.29    chopps 
   2019       1.29    chopps 	/* check to see if we couldn't allocate memory with NOWAIT */
   2020       1.29    chopps 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
   2021       1.29    chopps 		goto again;
   2022       1.29    chopps 
   2023       1.10   mycroft 	if (w.w_tmem)
   2024  1.140.6.3  jdolecek 		kmem_free(w.w_tmem, w.w_tmemsize);
   2025        1.1       cgd 	w.w_needed += w.w_given;
   2026       1.10   mycroft 	if (where) {
   2027       1.93  christos 		*given = (char *)w.w_where - (char *)where;
   2028       1.10   mycroft 		if (*given < w.w_needed)
   2029       1.95    dyoung 			return ENOMEM;
   2030       1.10   mycroft 	} else {
   2031       1.10   mycroft 		*given = (11 * w.w_needed) / 10;
   2032       1.10   mycroft 	}
   2033       1.95    dyoung 	return error;
   2034        1.1       cgd }
   2035        1.1       cgd 
   2036        1.1       cgd /*
   2037       1.99        ad  * Routing message software interrupt routine
   2038       1.99        ad  */
   2039       1.99        ad static void
   2040      1.133      matt COMPATNAME(route_intr)(void *cookie)
   2041       1.99        ad {
   2042      1.133      matt 	struct sockproto proto = { .sp_family = PF_XROUTE, };
   2043      1.133      matt 	struct route_info * const ri = &COMPATNAME(route_info);
   2044       1.99        ad 	struct mbuf *m;
   2045       1.99        ad 
   2046  1.140.6.3  jdolecek 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
   2047  1.140.6.3  jdolecek 	for (;;) {
   2048  1.140.6.3  jdolecek 		IFQ_LOCK(&ri->ri_intrq);
   2049      1.133      matt 		IF_DEQUEUE(&ri->ri_intrq, m);
   2050  1.140.6.3  jdolecek 		IFQ_UNLOCK(&ri->ri_intrq);
   2051       1.99        ad 		if (m == NULL)
   2052       1.99        ad 			break;
   2053      1.100      yamt 		proto.sp_protocol = M_GETCTX(m, uintptr_t);
   2054  1.140.6.3  jdolecek #ifdef NET_MPSAFE
   2055  1.140.6.3  jdolecek 		mutex_enter(rt_so_mtx);
   2056  1.140.6.3  jdolecek #endif
   2057  1.140.6.3  jdolecek 		raw_input(m, &proto, &ri->ri_src, &ri->ri_dst, &rt_rawcb);
   2058  1.140.6.3  jdolecek #ifdef NET_MPSAFE
   2059  1.140.6.3  jdolecek 		mutex_exit(rt_so_mtx);
   2060  1.140.6.3  jdolecek #endif
   2061       1.99        ad 	}
   2062  1.140.6.3  jdolecek 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   2063       1.99        ad }
   2064       1.99        ad 
   2065       1.99        ad /*
   2066       1.99        ad  * Enqueue a message to the software interrupt routine.
   2067       1.99        ad  */
   2068      1.120  christos void
   2069      1.133      matt COMPATNAME(route_enqueue)(struct mbuf *m, int family)
   2070       1.99        ad {
   2071      1.133      matt 	struct route_info * const ri = &COMPATNAME(route_info);
   2072  1.140.6.3  jdolecek 	int wasempty;
   2073       1.99        ad 
   2074  1.140.6.3  jdolecek 	IFQ_LOCK(&ri->ri_intrq);
   2075      1.133      matt 	if (IF_QFULL(&ri->ri_intrq)) {
   2076      1.133      matt 		IF_DROP(&ri->ri_intrq);
   2077  1.140.6.3  jdolecek 		IFQ_UNLOCK(&ri->ri_intrq);
   2078       1.99        ad 		m_freem(m);
   2079       1.99        ad 	} else {
   2080      1.133      matt 		wasempty = IF_IS_EMPTY(&ri->ri_intrq);
   2081       1.99        ad 		M_SETCTX(m, (uintptr_t)family);
   2082      1.133      matt 		IF_ENQUEUE(&ri->ri_intrq, m);
   2083  1.140.6.3  jdolecek 		IFQ_UNLOCK(&ri->ri_intrq);
   2084  1.140.6.3  jdolecek 		if (wasempty) {
   2085  1.140.6.3  jdolecek 			kpreempt_disable();
   2086      1.133      matt 			softint_schedule(ri->ri_sih);
   2087  1.140.6.3  jdolecek 			kpreempt_enable();
   2088  1.140.6.3  jdolecek 		}
   2089       1.99        ad 	}
   2090       1.99        ad }
   2091       1.99        ad 
   2092      1.133      matt static void
   2093      1.133      matt COMPATNAME(route_init)(void)
   2094       1.99        ad {
   2095      1.133      matt 	struct route_info * const ri = &COMPATNAME(route_info);
   2096      1.133      matt 
   2097      1.133      matt #ifndef COMPAT_RTSOCK
   2098      1.133      matt 	rt_init();
   2099      1.133      matt #endif
   2100  1.140.6.3  jdolecek #ifdef NET_MPSAFE
   2101  1.140.6.3  jdolecek 	rt_so_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
   2102  1.140.6.3  jdolecek #endif
   2103       1.99        ad 
   2104      1.127     pooka 	sysctl_net_route_setup(NULL);
   2105      1.133      matt 	ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
   2106      1.133      matt 	ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   2107      1.133      matt 	    COMPATNAME(route_intr), NULL);
   2108  1.140.6.3  jdolecek 	IFQ_LOCK_INIT(&ri->ri_intrq);
   2109       1.99        ad }
   2110       1.99        ad 
   2111       1.99        ad /*
   2112        1.1       cgd  * Definitions of protocols supported in the ROUTE domain.
   2113        1.1       cgd  */
   2114      1.133      matt #ifndef COMPAT_RTSOCK
   2115  1.140.6.2       tls PR_WRAP_USRREQS(route);
   2116      1.133      matt #else
   2117  1.140.6.2       tls PR_WRAP_USRREQS(compat_50_route);
   2118      1.133      matt #endif
   2119        1.1       cgd 
   2120  1.140.6.2       tls static const struct pr_usrreqs route_usrreqs = {
   2121  1.140.6.2       tls 	.pr_attach	= COMPATNAME(route_attach_wrapper),
   2122  1.140.6.2       tls 	.pr_detach	= COMPATNAME(route_detach_wrapper),
   2123  1.140.6.2       tls 	.pr_accept	= COMPATNAME(route_accept_wrapper),
   2124  1.140.6.2       tls 	.pr_bind	= COMPATNAME(route_bind_wrapper),
   2125  1.140.6.2       tls 	.pr_listen	= COMPATNAME(route_listen_wrapper),
   2126  1.140.6.2       tls 	.pr_connect	= COMPATNAME(route_connect_wrapper),
   2127  1.140.6.2       tls 	.pr_connect2	= COMPATNAME(route_connect2_wrapper),
   2128  1.140.6.2       tls 	.pr_disconnect	= COMPATNAME(route_disconnect_wrapper),
   2129  1.140.6.2       tls 	.pr_shutdown	= COMPATNAME(route_shutdown_wrapper),
   2130  1.140.6.2       tls 	.pr_abort	= COMPATNAME(route_abort_wrapper),
   2131  1.140.6.2       tls 	.pr_ioctl	= COMPATNAME(route_ioctl_wrapper),
   2132  1.140.6.2       tls 	.pr_stat	= COMPATNAME(route_stat_wrapper),
   2133  1.140.6.2       tls 	.pr_peeraddr	= COMPATNAME(route_peeraddr_wrapper),
   2134  1.140.6.2       tls 	.pr_sockaddr	= COMPATNAME(route_sockaddr_wrapper),
   2135  1.140.6.2       tls 	.pr_rcvd	= COMPATNAME(route_rcvd_wrapper),
   2136  1.140.6.2       tls 	.pr_recvoob	= COMPATNAME(route_recvoob_wrapper),
   2137  1.140.6.2       tls 	.pr_send	= COMPATNAME(route_send_wrapper),
   2138  1.140.6.2       tls 	.pr_sendoob	= COMPATNAME(route_sendoob_wrapper),
   2139  1.140.6.2       tls 	.pr_purgeif	= COMPATNAME(route_purgeif_wrapper),
   2140  1.140.6.2       tls };
   2141  1.140.6.2       tls 
   2142      1.133      matt static const struct protosw COMPATNAME(route_protosw)[] = {
   2143       1.92      matt 	{
   2144       1.92      matt 		.pr_type = SOCK_RAW,
   2145      1.133      matt 		.pr_domain = &COMPATNAME(routedomain),
   2146       1.92      matt 		.pr_flags = PR_ATOMIC|PR_ADDR,
   2147       1.92      matt 		.pr_input = raw_input,
   2148       1.92      matt 		.pr_ctlinput = raw_ctlinput,
   2149  1.140.6.3  jdolecek 		.pr_ctloutput = route_ctloutput,
   2150  1.140.6.2       tls 		.pr_usrreqs = &route_usrreqs,
   2151  1.140.6.3  jdolecek 		.pr_init = rt_pr_init,
   2152       1.92      matt 	},
   2153       1.92      matt };
   2154       1.69      matt 
   2155      1.133      matt struct domain COMPATNAME(routedomain) = {
   2156      1.133      matt 	.dom_family = PF_XROUTE,
   2157      1.133      matt 	.dom_name = DOMAINNAME,
   2158      1.133      matt 	.dom_init = COMPATNAME(route_init),
   2159      1.133      matt 	.dom_protosw = COMPATNAME(route_protosw),
   2160      1.133      matt 	.dom_protoswNPROTOSW =
   2161      1.133      matt 	    &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
   2162        1.1       cgd };
   2163        1.1       cgd 
   2164      1.127     pooka static void
   2165      1.127     pooka sysctl_net_route_setup(struct sysctllog **clog)
   2166       1.65    atatat {
   2167       1.85      elad 	const struct sysctlnode *rnode = NULL;
   2168       1.85      elad 
   2169       1.85      elad 	sysctl_createv(clog, 0, NULL, &rnode,
   2170       1.67    atatat 		       CTLFLAG_PERMANENT,
   2171      1.133      matt 		       CTLTYPE_NODE, DOMAINNAME,
   2172       1.71    atatat 		       SYSCTL_DESCR("PF_ROUTE information"),
   2173       1.65    atatat 		       NULL, 0, NULL, 0,
   2174      1.133      matt 		       CTL_NET, PF_XROUTE, CTL_EOL);
   2175      1.133      matt 
   2176       1.67    atatat 	sysctl_createv(clog, 0, NULL, NULL,
   2177       1.67    atatat 		       CTLFLAG_PERMANENT,
   2178       1.71    atatat 		       CTLTYPE_NODE, "rtable",
   2179       1.71    atatat 		       SYSCTL_DESCR("Routing table information"),
   2180       1.65    atatat 		       sysctl_rtable, 0, NULL, 0,
   2181      1.133      matt 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
   2182      1.133      matt 
   2183       1.85      elad 	sysctl_createv(clog, 0, &rnode, NULL,
   2184       1.85      elad 		       CTLFLAG_PERMANENT,
   2185       1.85      elad 		       CTLTYPE_STRUCT, "stats",
   2186       1.85      elad 		       SYSCTL_DESCR("Routing statistics"),
   2187       1.85      elad 		       NULL, 0, &rtstat, sizeof(rtstat),
   2188       1.85      elad 		       CTL_CREATE, CTL_EOL);
   2189       1.65    atatat }
   2190