Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.238.2.20
      1  1.238.2.20  pgoyette /*	$NetBSD: rtsock.c,v 1.238.2.20 2019/01/15 03:40:35 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.20  pgoyette __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.238.2.20 2019/01/15 03:40:35 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.238.2.20  pgoyette #undef COMPAT_RTSOCK
    109       1.133      matt #endif
    110       1.133      matt 
    111  1.238.2.20  pgoyette static int if_addrflags(struct ifaddr *);
    112  1.238.2.14  pgoyette static void sysctl_net_route_setup(struct sysctllog **);
    113       1.175  riastrad 
    114  1.238.2.20  pgoyette #include <net/rtsock_shared.c>
    115       1.229     ozaki 
    116   1.238.2.1  pgoyette /*
    117   1.238.2.1  pgoyette  * Compat linkage
    118   1.238.2.1  pgoyette  */
    119  1.238.2.12  pgoyette static int stub_70_rt_newaddrmsg1(int cmd, struct ifaddr *ifa)
    120   1.238.2.1  pgoyette {
    121   1.238.2.1  pgoyette 
    122  1.238.2.12  pgoyette 	return 0;
    123   1.238.2.1  pgoyette }
    124         1.1       cgd 
    125   1.238.2.4  pgoyette /*
    126   1.238.2.4  pgoyette  * XXX avoid using void * once msghdr compat disappears.
    127   1.238.2.4  pgoyette  */
    128   1.238.2.4  pgoyette void
    129   1.238.2.4  pgoyette rt_setmetrics(void *in, struct rtentry *out)
    130   1.238.2.4  pgoyette {
    131   1.238.2.4  pgoyette 	const struct rt_xmsghdr *rtm = in;
    132   1.238.2.4  pgoyette 
    133   1.238.2.4  pgoyette 	_rt_setmetrics(rtm->rtm_inits, rtm, out);
    134   1.238.2.4  pgoyette }
    135        1.10   mycroft 
    136       1.178     ozaki int
    137       1.178     ozaki rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
    138       1.178     ozaki 	int *lenp)
    139       1.178     ozaki {
    140       1.178     ozaki 	return rt_msg2(type, rtinfo, cpv, w, lenp);
    141       1.178     ozaki }
    142       1.178     ozaki 
    143        1.10   mycroft /*
    144  1.238.2.17  pgoyette  * MODULE_HOOK glue for rtsock_14_oifmsg and rtsock_14_iflist
    145   1.238.2.7  pgoyette  */
    146  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_14_oifmsg_hook, (struct ifnet *ifp));
    147  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK(rtsock_14_oifmsg_hook, (struct ifnet *ifp), (ifp),
    148  1.238.2.19  pgoyette     __nothing);
    149   1.238.2.7  pgoyette 
    150  1.238.2.19  pgoyette MODULE_CALL_INT_HOOK_DECL(rtsock_14_iflist_hook,
    151   1.238.2.8  pgoyette     (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
    152  1.238.2.10  pgoyette      size_t len));
    153  1.238.2.19  pgoyette MODULE_CALL_INT_HOOK(rtsock_14_iflist_hook,
    154   1.238.2.7  pgoyette     (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
    155   1.238.2.7  pgoyette      size_t len),
    156   1.238.2.7  pgoyette     (ifp, w, info, len),
    157   1.238.2.7  pgoyette     enosys());
    158   1.238.2.7  pgoyette 
    159   1.238.2.7  pgoyette /*
    160  1.238.2.18  pgoyette  * MODULE_HOOK glue for rtsock_50 ifaddr_list and various message routines
    161  1.238.2.14  pgoyette  */
    162  1.238.2.19  pgoyette MODULE_CALL_INT_HOOK_DECL(rtsock_50_iflist_hook,
    163  1.238.2.14  pgoyette     (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
    164  1.238.2.14  pgoyette      size_t len));
    165  1.238.2.19  pgoyette MODULE_CALL_INT_HOOK(rtsock_50_iflist_hook,
    166  1.238.2.14  pgoyette     (struct ifnet *ifp, struct rt_walkarg *w, struct rt_addrinfo *info,
    167  1.238.2.14  pgoyette      size_t len),
    168  1.238.2.14  pgoyette     (ifp, w, info, len),
    169  1.238.2.14  pgoyette     enosys());
    170  1.238.2.14  pgoyette 
    171  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_missmsg_hook,
    172  1.238.2.18  pgoyette     (int, const struct rt_addrinfo *, int, int));
    173  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK(rtsock_50_rt_missmsg_hook,
    174  1.238.2.18  pgoyette     (int type, const struct rt_addrinfo *rtinfo, int flags, int error),
    175  1.238.2.19  pgoyette     (type, rtinfo, flags, error), __nothing);
    176  1.238.2.18  pgoyette 
    177  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ifmsg_hook, (struct ifnet *));
    178  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK(rtsock_50_rt_ifmsg_hook, (struct ifnet *ifp), (ifp),
    179  1.238.2.19  pgoyette     __nothing);
    180  1.238.2.18  pgoyette 
    181  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_newaddrmsg_hook,
    182  1.238.2.18  pgoyette     (int, struct ifaddr *, int, struct rtentry *));
    183  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK(rtsock_50_rt_newaddrmsg_hook,
    184  1.238.2.18  pgoyette     (int cmd, struct ifaddr *ifa, int error, struct rtentry *rt),
    185  1.238.2.19  pgoyette     (cmd, ifa, error, rt), __nothing);
    186  1.238.2.18  pgoyette 
    187  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ifannouncemsg_hook,
    188  1.238.2.18  pgoyette     (struct ifnet *, int what));
    189  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK(rtsock_50_rt_ifannouncemsg_hook,
    190  1.238.2.19  pgoyette     (struct ifnet *ifp, int what), (ifp, what), __nothing);
    191  1.238.2.18  pgoyette 
    192  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_rt_ieee80211msg_hook,
    193  1.238.2.18  pgoyette     (struct ifnet *, int, void *, size_t));
    194  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK(rtsock_50_rt_ieee80211msg_hook,
    195  1.238.2.18  pgoyette     (struct ifnet *ifp, int what, void *data, size_t data_len),
    196  1.238.2.19  pgoyette     (ifp, what, data, data_len), __nothing);
    197  1.238.2.18  pgoyette 
    198  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_50_oifmsg_hook, (struct ifnet *ifp));
    199  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK(rtsock_50_oifmsg_hook, (struct ifnet *ifp), (ifp),
    200  1.238.2.19  pgoyette     __nothing);
    201  1.238.2.14  pgoyette 
    202  1.238.2.14  pgoyette /*
    203  1.238.2.14  pgoyette  * MODULE_HOOK glue for rtsock70_newaddrmsg1, rtsock70_ifaddr_listaddr,
    204  1.238.2.14  pgoyette  * and rtsock70_ifaddr_listif
    205  1.238.2.12  pgoyette  */
    206  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK_DECL(rtsock_70_newaddr_hook, (int, struct ifaddr *));
    207  1.238.2.19  pgoyette MODULE_CALL_VOID_HOOK(rtsock_70_newaddr_hook, (int cmd, struct ifaddr *ifa),
    208  1.238.2.12  pgoyette     (cmd, ifa), stub_70_rt_newaddrmsg1(cmd, ifa));
    209  1.238.2.12  pgoyette 
    210  1.238.2.19  pgoyette MODULE_CALL_INT_HOOK_DECL(rtsock_70_iflist_hook,
    211  1.238.2.12  pgoyette     (struct rt_walkarg *, struct ifaddr *, struct rt_addrinfo *));
    212  1.238.2.19  pgoyette MODULE_CALL_INT_HOOK(rtsock_70_iflist_hook,
    213  1.238.2.12  pgoyette     (struct rt_walkarg *w, struct ifaddr *ifa, struct rt_addrinfo *info),
    214  1.238.2.12  pgoyette     (w, ifa, info),
    215  1.238.2.14  pgoyette     enosys());
    216  1.238.2.12  pgoyette 
    217  1.238.2.18  pgoyette 
    218       1.196       roy static int
    219       1.196       roy if_addrflags(struct ifaddr *ifa)
    220       1.196       roy {
    221       1.196       roy 
    222       1.196       roy 	switch (ifa->ifa_addr->sa_family) {
    223       1.196       roy #ifdef INET
    224       1.196       roy 	case AF_INET:
    225       1.196       roy 		return ((struct in_ifaddr *)ifa)->ia4_flags;
    226       1.196       roy #endif
    227       1.196       roy #ifdef INET6
    228       1.196       roy 	case AF_INET6:
    229       1.196       roy 		return ((struct in6_ifaddr *)ifa)->ia6_flags;
    230       1.196       roy #endif
    231       1.196       roy 	default:
    232       1.196       roy 		return 0;
    233       1.196       roy 	}
    234       1.196       roy }
    235        1.78    dyoung 
    236        1.10   mycroft 
    237       1.215     ozaki /*
    238       1.215     ozaki  * Send a routing message as mimicing that a cloned route is added.
    239       1.215     ozaki  */
    240       1.215     ozaki void
    241       1.215     ozaki rt_clonedmsg(const struct sockaddr *dst, const struct ifnet *ifp,
    242       1.215     ozaki     const struct rtentry *rt)
    243       1.215     ozaki {
    244       1.215     ozaki 	struct rt_addrinfo info;
    245       1.215     ozaki 	/* Mimic flags exactly */
    246       1.215     ozaki #define RTF_LLINFO	0x400
    247       1.215     ozaki #define RTF_CLONED	0x2000
    248       1.215     ozaki 	int flags = RTF_UP | RTF_HOST | RTF_DONE | RTF_LLINFO | RTF_CLONED;
    249       1.215     ozaki 	union {
    250       1.215     ozaki 		struct sockaddr sa;
    251       1.215     ozaki 		struct sockaddr_storage ss;
    252       1.215     ozaki 		struct sockaddr_dl sdl;
    253       1.215     ozaki 	} u;
    254       1.215     ozaki 	uint8_t namelen = strlen(ifp->if_xname);
    255       1.215     ozaki 	uint8_t addrlen = ifp->if_addrlen;
    256       1.215     ozaki 
    257       1.215     ozaki 	if (rt == NULL)
    258       1.215     ozaki 		return; /* XXX */
    259       1.215     ozaki 
    260       1.215     ozaki 	memset(&info, 0, sizeof(info));
    261       1.215     ozaki 	info.rti_info[RTAX_DST] = dst;
    262       1.215     ozaki 	sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type,
    263       1.215     ozaki 	    NULL, namelen, NULL, addrlen);
    264       1.215     ozaki 	info.rti_info[RTAX_GATEWAY] = &u.sa;
    265       1.215     ozaki 
    266       1.215     ozaki 	rt_missmsg(RTM_ADD, &info, flags, 0);
    267       1.215     ozaki #undef RTF_LLINFO
    268       1.215     ozaki #undef RTF_CLONED
    269       1.215     ozaki }
    270  1.238.2.16  pgoyette 
    271  1.238.2.16  pgoyette 
    272  1.238.2.16  pgoyette /*
    273  1.238.2.16  pgoyette  * The remaining code implements the routing-table sysctl node.  It is
    274  1.238.2.16  pgoyette  * compiled only for the non-COMPAT case.
    275  1.238.2.16  pgoyette  */
    276  1.238.2.16  pgoyette 
    277  1.238.2.16  pgoyette /*
    278        1.10   mycroft  * This is used in dumping the kernel table via sysctl().
    279         1.1       cgd  */
    280        1.40    simonb static int
    281        1.94    dyoung sysctl_dumpentry(struct rtentry *rt, void *v)
    282         1.1       cgd {
    283       1.120  christos 	struct rt_walkarg *w = v;
    284        1.10   mycroft 	int error = 0, size;
    285        1.10   mycroft 	struct rt_addrinfo info;
    286         1.1       cgd 
    287        1.10   mycroft 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    288        1.10   mycroft 		return 0;
    289        1.48   thorpej 	memset(&info, 0, sizeof(info));
    290       1.114    dyoung 	info.rti_info[RTAX_DST] = rt_getkey(rt);
    291       1.114    dyoung 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    292       1.114    dyoung 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    293       1.142    kefren 	info.rti_info[RTAX_TAG] = rt_gettag(rt);
    294        1.16       cgd 	if (rt->rt_ifp) {
    295        1.91    dyoung 		const struct ifaddr *rtifa;
    296       1.114    dyoung 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
    297        1.91    dyoung 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
    298        1.91    dyoung 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
    299        1.91    dyoung 		 * --dyoung
    300        1.91    dyoung 		 */
    301        1.91    dyoung 		rtifa = rt_get_ifa(rt);
    302       1.114    dyoung 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
    303        1.16       cgd 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    304       1.114    dyoung 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
    305        1.16       cgd 	}
    306        1.29    chopps 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
    307        1.95    dyoung 		return error;
    308        1.29    chopps 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    309       1.133      matt 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
    310        1.10   mycroft 
    311        1.10   mycroft 		rtm->rtm_flags = rt->rt_flags;
    312        1.10   mycroft 		rtm->rtm_use = rt->rt_use;
    313       1.133      matt 		rtm_setmetrics(rt, rtm);
    314        1.83  christos 		KASSERT(rt->rt_ifp != NULL);
    315        1.10   mycroft 		rtm->rtm_index = rt->rt_ifp->if_index;
    316        1.10   mycroft 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    317        1.10   mycroft 		rtm->rtm_addrs = info.rti_addrs;
    318        1.21  christos 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    319        1.10   mycroft 			w->w_where = NULL;
    320        1.10   mycroft 		else
    321        1.93  christos 			w->w_where = (char *)w->w_where + size;
    322        1.10   mycroft 	}
    323        1.95    dyoung 	return error;
    324        1.10   mycroft }
    325         1.1       cgd 
    326        1.40    simonb static int
    327       1.195       roy sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
    328       1.195       roy     struct rt_addrinfo *info, size_t len)
    329       1.195       roy {
    330       1.195       roy 	struct if_xmsghdr *ifm;
    331       1.195       roy 	int error;
    332       1.195       roy 
    333       1.195       roy 	ifm = (struct if_xmsghdr *)w->w_tmem;
    334       1.195       roy 	ifm->ifm_index = ifp->if_index;
    335       1.195       roy 	ifm->ifm_flags = ifp->if_flags;
    336       1.195       roy 	ifm->ifm_data = ifp->if_data;
    337       1.195       roy 	ifm->ifm_addrs = info->rti_addrs;
    338       1.195       roy 	if ((error = copyout(ifm, w->w_where, len)) == 0)
    339       1.195       roy 		w->w_where = (char *)w->w_where + len;
    340       1.195       roy 	return error;
    341       1.195       roy }
    342       1.195       roy 
    343       1.195       roy static int
    344       1.195       roy sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
    345       1.195       roy      struct rt_addrinfo *info)
    346       1.195       roy {
    347       1.195       roy 	int len, error;
    348       1.195       roy 
    349       1.196       roy 	if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
    350       1.195       roy 		return error;
    351       1.195       roy 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    352       1.195       roy 		struct ifa_xmsghdr *ifam;
    353       1.195       roy 
    354       1.195       roy 		ifam = (struct ifa_xmsghdr *)w->w_tmem;
    355       1.195       roy 		ifam->ifam_index = ifa->ifa_ifp->if_index;
    356       1.195       roy 		ifam->ifam_flags = ifa->ifa_flags;
    357       1.195       roy 		ifam->ifam_metric = ifa->ifa_metric;
    358       1.195       roy 		ifam->ifam_addrs = info->rti_addrs;
    359       1.196       roy 		ifam->ifam_pid = 0;
    360       1.196       roy 		ifam->ifam_addrflags = if_addrflags(ifa);
    361       1.195       roy 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
    362       1.195       roy 			w->w_where = (char *)w->w_where + len;
    363       1.195       roy 	}
    364       1.195       roy 	return error;
    365       1.195       roy }
    366       1.195       roy 
    367       1.195       roy static int
    368       1.120  christos sysctl_iflist(int af, struct rt_walkarg *w, int type)
    369        1.10   mycroft {
    370        1.39  augustss 	struct ifnet *ifp;
    371        1.39  augustss 	struct ifaddr *ifa;
    372        1.10   mycroft 	struct	rt_addrinfo info;
    373       1.195       roy 	int	cmd, len, error = 0;
    374       1.186     ozaki 	int s;
    375       1.186     ozaki 	struct psref psref;
    376       1.206     ozaki 	int bound;
    377        1.10   mycroft 
    378       1.195       roy 	switch (type) {
    379       1.195       roy 	case NET_RT_IFLIST:
    380       1.195       roy 		cmd = RTM_IFINFO;
    381       1.195       roy 		break;
    382       1.196       roy 	case NET_RT_OOOIFLIST:
    383       1.195       roy 		cmd = RTM_OOIFINFO;
    384       1.195       roy 		break;
    385       1.196       roy 	case NET_RT_OOIFLIST:
    386       1.195       roy 		cmd = RTM_OIFINFO;
    387       1.196       roy 		break;
    388       1.196       roy 	case NET_RT_OIFLIST:
    389       1.196       roy 		cmd = RTM_IFINFO;
    390       1.195       roy 		break;
    391       1.195       roy 	default:
    392       1.203     ozaki #ifdef RTSOCK_DEBUG
    393       1.203     ozaki 		printf("%s: unsupported IFLIST type %d\n", __func__, type);
    394       1.195       roy #endif
    395       1.195       roy 		return EINVAL;
    396       1.195       roy 	}
    397       1.195       roy 
    398        1.48   thorpej 	memset(&info, 0, sizeof(info));
    399       1.186     ozaki 
    400       1.206     ozaki 	bound = curlwp_bind();
    401       1.186     ozaki 	s = pserialize_read_enter();
    402       1.186     ozaki 	IFNET_READER_FOREACH(ifp) {
    403       1.205     ozaki 		int _s;
    404        1.10   mycroft 		if (w->w_arg && w->w_arg != ifp->if_index)
    405        1.10   mycroft 			continue;
    406       1.191     ozaki 		if (IFADDR_READER_EMPTY(ifp))
    407        1.81    rpaulo 			continue;
    408       1.186     ozaki 
    409       1.207     ozaki 		if_acquire(ifp, &psref);
    410       1.186     ozaki 		pserialize_read_exit(s);
    411       1.186     ozaki 
    412       1.114    dyoung 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    413       1.195       roy 		if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
    414       1.186     ozaki 			goto release_exit;
    415       1.114    dyoung 		info.rti_info[RTAX_IFP] = NULL;
    416        1.29    chopps 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    417  1.238.2.14  pgoyette 			switch (type) {
    418  1.238.2.14  pgoyette 			case NET_RT_OIFLIST: /* old _70 */
    419  1.238.2.17  pgoyette 				if (!rtsock_70_iflist_hook.hooked) {
    420  1.238.2.15  pgoyette 					error = EINVAL;
    421  1.238.2.15  pgoyette 					break;
    422  1.238.2.15  pgoyette 				}
    423  1.238.2.15  pgoyette 				/* FALLTHROUGH */
    424  1.238.2.15  pgoyette 			case NET_RT_IFLIST: /* current */
    425  1.238.2.14  pgoyette 				error = sysctl_iflist_if(ifp, w, &info, len);
    426  1.238.2.14  pgoyette 				break;
    427  1.238.2.14  pgoyette 			case NET_RT_OOIFLIST: /* old _50 */
    428  1.238.2.18  pgoyette 				error = rtsock_50_iflist_hook_call(ifp, w, &info,
    429  1.238.2.14  pgoyette 				    len);
    430  1.238.2.14  pgoyette 				break;
    431  1.238.2.14  pgoyette 			case NET_RT_OOOIFLIST: /* old _14 */
    432  1.238.2.17  pgoyette 				error = rtsock_14_iflist_hook_call(ifp, w,
    433  1.238.2.17  pgoyette 				    &info, len);
    434  1.238.2.14  pgoyette 				break;
    435  1.238.2.14  pgoyette 			default:
    436  1.238.2.14  pgoyette 				error = EINVAL;
    437  1.238.2.14  pgoyette 			}
    438  1.238.2.14  pgoyette 			if (error != 0) {
    439   1.238.2.3  pgoyette 				if (error == ENOSYS)
    440   1.238.2.3  pgoyette 					error = EINVAL;
    441       1.195       roy 				goto release_exit;
    442   1.238.2.3  pgoyette 			}
    443        1.10   mycroft 		}
    444       1.205     ozaki 		_s = pserialize_read_enter();
    445       1.191     ozaki 		IFADDR_READER_FOREACH(ifa, ifp) {
    446       1.205     ozaki 			struct psref _psref;
    447        1.10   mycroft 			if (af && af != ifa->ifa_addr->sa_family)
    448        1.10   mycroft 				continue;
    449       1.205     ozaki 			ifa_acquire(ifa, &_psref);
    450       1.205     ozaki 			pserialize_read_exit(_s);
    451       1.205     ozaki 
    452       1.114    dyoung 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
    453       1.114    dyoung 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
    454       1.114    dyoung 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
    455  1.238.2.14  pgoyette 			switch (type) {
    456  1.238.2.14  pgoyette 			case NET_RT_IFLIST:
    457  1.238.2.14  pgoyette 				error = sysctl_iflist_addr(w, ifa, &info);
    458  1.238.2.14  pgoyette 				break;
    459  1.238.2.14  pgoyette 			case NET_RT_OIFLIST:
    460  1.238.2.14  pgoyette 			case NET_RT_OOIFLIST:
    461  1.238.2.14  pgoyette 			case NET_RT_OOOIFLIST:
    462  1.238.2.17  pgoyette 				error = rtsock_70_iflist_hook_call(w, ifa,
    463  1.238.2.17  pgoyette 				    &info);
    464  1.238.2.14  pgoyette 				break;
    465  1.238.2.14  pgoyette 			default:
    466  1.238.2.14  pgoyette 				error = EINVAL;
    467  1.238.2.14  pgoyette 			}
    468       1.205     ozaki 
    469       1.205     ozaki 			_s = pserialize_read_enter();
    470       1.205     ozaki 			ifa_release(ifa, &_psref);
    471       1.208     ozaki 			if (error != 0) {
    472       1.208     ozaki 				pserialize_read_exit(_s);
    473       1.186     ozaki 				goto release_exit;
    474       1.208     ozaki 			}
    475        1.10   mycroft 		}
    476       1.205     ozaki 		pserialize_read_exit(_s);
    477       1.115  christos 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
    478       1.115  christos 		    info.rti_info[RTAX_BRD] = NULL;
    479       1.186     ozaki 
    480       1.186     ozaki 		s = pserialize_read_enter();
    481       1.207     ozaki 		if_release(ifp, &psref);
    482        1.10   mycroft 	}
    483       1.186     ozaki 	pserialize_read_exit(s);
    484       1.190     ozaki 	curlwp_bindx(bound);
    485       1.186     ozaki 
    486        1.95    dyoung 	return 0;
    487       1.186     ozaki 
    488       1.186     ozaki release_exit:
    489       1.207     ozaki 	if_release(ifp, &psref);
    490       1.190     ozaki 	curlwp_bindx(bound);
    491       1.186     ozaki 	return error;
    492         1.1       cgd }
    493         1.1       cgd 
    494        1.40    simonb static int
    495        1.65    atatat sysctl_rtable(SYSCTLFN_ARGS)
    496         1.1       cgd {
    497        1.65    atatat 	void 	*where = oldp;
    498        1.65    atatat 	size_t	*given = oldlenp;
    499        1.10   mycroft 	int	i, s, error = EINVAL;
    500        1.10   mycroft 	u_char  af;
    501       1.120  christos 	struct	rt_walkarg w;
    502         1.1       cgd 
    503        1.66    atatat 	if (namelen == 1 && name[0] == CTL_QUERY)
    504        1.95    dyoung 		return sysctl_query(SYSCTLFN_CALL(rnode));
    505        1.66    atatat 
    506       1.164      matt 	if (newp)
    507        1.95    dyoung 		return EPERM;
    508        1.10   mycroft 	if (namelen != 3)
    509        1.95    dyoung 		return EINVAL;
    510        1.10   mycroft 	af = name[0];
    511        1.29    chopps 	w.w_tmemneeded = 0;
    512        1.29    chopps 	w.w_tmemsize = 0;
    513        1.29    chopps 	w.w_tmem = NULL;
    514        1.29    chopps again:
    515        1.29    chopps 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
    516        1.29    chopps 	if (w.w_tmemneeded) {
    517  1.238.2.13  pgoyette 		w.w_tmem = kmem_zalloc(w.w_tmemneeded, KM_SLEEP);
    518        1.29    chopps 		w.w_tmemsize = w.w_tmemneeded;
    519        1.29    chopps 		w.w_tmemneeded = 0;
    520        1.29    chopps 	}
    521        1.29    chopps 	w.w_op = name[1];
    522        1.29    chopps 	w.w_arg = name[2];
    523        1.10   mycroft 	w.w_given = *given;
    524         1.1       cgd 	w.w_needed = 0 - w.w_given;
    525        1.29    chopps 	w.w_where = where;
    526         1.1       cgd 
    527        1.14   mycroft 	s = splsoftnet();
    528        1.10   mycroft 	switch (w.w_op) {
    529        1.10   mycroft 
    530        1.10   mycroft 	case NET_RT_DUMP:
    531        1.10   mycroft 	case NET_RT_FLAGS:
    532       1.219     ozaki #if defined(INET) || defined(INET6)
    533       1.178     ozaki 		/*
    534       1.178     ozaki 		 * take care of llinfo entries, the caller must
    535       1.178     ozaki 		 * specify an AF
    536       1.178     ozaki 		 */
    537       1.178     ozaki 		if (w.w_op == NET_RT_FLAGS &&
    538       1.178     ozaki 		    (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
    539       1.178     ozaki 			if (af != 0)
    540       1.219     ozaki 				error = lltable_sysctl_dump(af, &w);
    541       1.178     ozaki 			else
    542       1.178     ozaki 				error = EINVAL;
    543       1.178     ozaki 			break;
    544       1.178     ozaki 		}
    545       1.219     ozaki #endif
    546       1.178     ozaki 
    547       1.224     ozaki 		for (i = 1; i <= AF_MAX; i++) {
    548       1.224     ozaki 			if (af == 0 || af == i) {
    549       1.224     ozaki 				error = rt_walktree(i, sysctl_dumpentry, &w);
    550       1.224     ozaki 				if (error != 0)
    551       1.224     ozaki 					break;
    552       1.224     ozaki #if defined(INET) || defined(INET6)
    553       1.224     ozaki 				/*
    554       1.224     ozaki 				 * Return ARP/NDP entries too for
    555       1.224     ozaki 				 * backward compatibility.
    556       1.224     ozaki 				 */
    557       1.224     ozaki 				error = lltable_sysctl_dump(i, &w);
    558       1.224     ozaki 				if (error != 0)
    559       1.224     ozaki 					break;
    560       1.224     ozaki #endif
    561       1.224     ozaki 			}
    562       1.224     ozaki 		}
    563        1.10   mycroft 		break;
    564        1.10   mycroft 
    565  1.238.2.14  pgoyette 	case NET_RT_OOOIFLIST:		/* compat_14 */
    566  1.238.2.14  pgoyette 	case NET_RT_OOIFLIST:		/* compat_50 */
    567  1.238.2.14  pgoyette 	case NET_RT_OIFLIST:		/* compat_70 */
    568  1.238.2.14  pgoyette 	case NET_RT_IFLIST:		/* current */
    569        1.32    bouyer 		error = sysctl_iflist(af, &w, w.w_op);
    570       1.133      matt 		break;
    571         1.1       cgd 	}
    572        1.10   mycroft 	splx(s);
    573        1.29    chopps 
    574        1.29    chopps 	/* check to see if we couldn't allocate memory with NOWAIT */
    575        1.29    chopps 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
    576        1.29    chopps 		goto again;
    577        1.29    chopps 
    578        1.10   mycroft 	if (w.w_tmem)
    579       1.202     ozaki 		kmem_free(w.w_tmem, w.w_tmemsize);
    580         1.1       cgd 	w.w_needed += w.w_given;
    581        1.10   mycroft 	if (where) {
    582        1.93  christos 		*given = (char *)w.w_where - (char *)where;
    583        1.10   mycroft 		if (*given < w.w_needed)
    584        1.95    dyoung 			return ENOMEM;
    585        1.10   mycroft 	} else {
    586        1.10   mycroft 		*given = (11 * w.w_needed) / 10;
    587        1.10   mycroft 	}
    588        1.95    dyoung 	return error;
    589         1.1       cgd }
    590        1.99        ad 
    591       1.133      matt static void
    592       1.127     pooka sysctl_net_route_setup(struct sysctllog **clog)
    593        1.65    atatat {
    594        1.85      elad 	const struct sysctlnode *rnode = NULL;
    595        1.85      elad 
    596        1.85      elad 	sysctl_createv(clog, 0, NULL, &rnode,
    597        1.67    atatat 		       CTLFLAG_PERMANENT,
    598       1.133      matt 		       CTLTYPE_NODE, DOMAINNAME,
    599        1.71    atatat 		       SYSCTL_DESCR("PF_ROUTE information"),
    600        1.65    atatat 		       NULL, 0, NULL, 0,
    601       1.133      matt 		       CTL_NET, PF_XROUTE, CTL_EOL);
    602       1.133      matt 
    603        1.67    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    604        1.67    atatat 		       CTLFLAG_PERMANENT,
    605        1.71    atatat 		       CTLTYPE_NODE, "rtable",
    606        1.71    atatat 		       SYSCTL_DESCR("Routing table information"),
    607        1.65    atatat 		       sysctl_rtable, 0, NULL, 0,
    608       1.133      matt 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
    609       1.133      matt 
    610        1.85      elad 	sysctl_createv(clog, 0, &rnode, NULL,
    611        1.85      elad 		       CTLFLAG_PERMANENT,
    612        1.85      elad 		       CTLTYPE_STRUCT, "stats",
    613        1.85      elad 		       SYSCTL_DESCR("Routing statistics"),
    614        1.85      elad 		       NULL, 0, &rtstat, sizeof(rtstat),
    615        1.85      elad 		       CTL_CREATE, CTL_EOL);
    616        1.65    atatat }
    617