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