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