/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1988, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)rtsock.c 8.7 (Berkeley) 10/12/95
*/
/*
* The remaining code implements the routing-table sysctl node. It is
* compiled only for the non-COMPAT case.
*/
/*
* This is used in dumping the kernel table via sysctl().
*/
static int
sysctl_dumpentry(struct rtentry *rt, void *v)
{
struct rt_walkarg *w = v;
int error = 0, size;
struct rt_addrinfo info;
if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
return 0;
memset(&info, 0, sizeof(info));
info.rti_info[RTAX_DST] = rt_getkey(rt);
info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
info.rti_info[RTAX_TAG] = rt_gettag(rt);
if (rt->rt_ifp) {
const struct ifaddr *rtifa;
info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
/* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
* then rt_get_ifa() != NULL. So this ought to still be safe.
* --dyoung
*/
rtifa = rt_get_ifa(rt);
info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
}
if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
return error;
if (w->w_where && w->w_tmem && w->w_needed <= 0) {
struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
static int
sysctl_iflist(int af, struct rt_walkarg *w, int type)
{
struct ifnet *ifp;
struct ifaddr *ifa;
struct rt_addrinfo info;
int cmd, len, error = 0;
int s;
struct psref psref;
int bound;
switch (type) {
case NET_RT_IFLIST:
cmd = RTM_IFINFO;
break;
case NET_RT_OOOIFLIST:
cmd = RTM_OOIFINFO;
break;
case NET_RT_OOIFLIST:
cmd = RTM_OIFINFO;
break;
case NET_RT_OIFLIST:
cmd = RTM_IFINFO;
break;
default:
#ifdef RTSOCK_DEBUG
printf("%s: unsupported IFLIST type %d\n", __func__, type);
#endif
return EINVAL;
}
memset(&info, 0, sizeof(info));
bound = curlwp_bind();
s = pserialize_read_enter();
IFNET_READER_FOREACH(ifp) {
int _s;
if (w->w_arg && w->w_arg != ifp->if_index)
continue;
if (IFADDR_READER_EMPTY(ifp))
continue;
if_acquire(ifp, &psref);
pserialize_read_exit(s);
info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
goto release_exit;
info.rti_info[RTAX_IFP] = NULL;
if (w->w_where && w->w_tmem && w->w_needed <= 0) {
switch (type) {
case NET_RT_OIFLIST: /* old _70 */
if (!rtsock_iflist_70_hook.hooked) {
error = EINVAL;
break;
}
/* FALLTHROUGH */
case NET_RT_IFLIST: /* current */
error = sysctl_iflist_if(ifp, w, &info, len);
break;
case NET_RT_OOIFLIST: /* old _50 */
MODULE_HOOK_CALL(rtsock_iflist_50_hook,
(ifp, w, &info, len), enosys(), error);
break;
case NET_RT_OOOIFLIST: /* old _14 */
MODULE_HOOK_CALL(rtsock_iflist_14_hook,
(ifp, w, &info, len), enosys(), error);
break;
default:
error = EINVAL;
}
if (error != 0) {
if (error == ENOSYS)
error = EINVAL;
goto release_exit;
}
}
_s = pserialize_read_enter();
IFADDR_READER_FOREACH(ifa, ifp) {
struct psref _psref;
if (af && af != ifa->ifa_addr->sa_family)
continue;
ifa_acquire(ifa, &_psref);
pserialize_read_exit(_s);
info.rti_info[RTAX_IFA] = ifa->ifa_addr;
info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
switch (type) {
case NET_RT_IFLIST:
error = sysctl_iflist_addr(w, ifa, &info);
break;
case NET_RT_OIFLIST:
case NET_RT_OOIFLIST:
case NET_RT_OOOIFLIST:
MODULE_HOOK_CALL(rtsock_iflist_70_hook,
(w, ifa, &info), enosys(), error);
break;
default:
error = EINVAL;
}
static int
sysctl_rtable(SYSCTLFN_ARGS)
{
void *where = oldp;
size_t *given = oldlenp;
int i, error = EINVAL;
u_char af;
struct rt_walkarg w;
if (namelen == 1 && name[0] == CTL_QUERY)
return sysctl_query(SYSCTLFN_CALL(rnode));
if (newp)
return EPERM;
if (namelen != 3)
return EINVAL;
af = name[0];
w.w_tmemneeded = 0;
w.w_tmemsize = 0;
w.w_tmem = NULL;
again:
/* we may return here if a later [re]alloc of the t_mem buffer fails */
if (w.w_tmemneeded) {
w.w_tmem = kmem_zalloc(w.w_tmemneeded, KM_SLEEP);
w.w_tmemsize = w.w_tmemneeded;
w.w_tmemneeded = 0;
}
w.w_op = name[1];
w.w_arg = name[2];
w.w_given = *given;
w.w_needed = 0 - w.w_given;
w.w_where = where;
KERNEL_LOCK_UNLESS_NET_MPSAFE();
const int s = splsoftnet();
switch (w.w_op) {
case NET_RT_DUMP:
case NET_RT_FLAGS:
#if defined(INET) || defined(INET6)
/*
* take care of llinfo entries, the caller must
* specify an AF
*/
if (w.w_op == NET_RT_FLAGS &&
(w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
if (af != 0)
error = lltable_sysctl_dump(af, &w);
else
error = EINVAL;
break;
}
#endif
for (i = 1; i <= AF_MAX; i++) {
if (af == 0 || af == i) {
error = rt_walktree(i, sysctl_dumpentry, &w);
if (error != 0)
break;
#if defined(INET) || defined(INET6)
/*
* Return ARP/NDP entries too for
* backward compatibility.
*/
error = lltable_sysctl_dump(i, &w);
if (error != 0)
break;
#endif
}
}
break;
case NET_RT_OOOIFLIST: /* compat_14 */
case NET_RT_OOIFLIST: /* compat_50 */
case NET_RT_OIFLIST: /* compat_70 */
case NET_RT_IFLIST: /* current */
error = sysctl_iflist(af, &w, w.w_op);
break;
}
splx(s);
KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
/* check to see if we couldn't allocate memory with NOWAIT */
if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
goto again;