NetBSD/sys/net/rtsock.c

526 lines
14 KiB
C
Raw Normal View History

/* $NetBSD: rtsock.c,v 1.255 2020/03/09 21:20:55 roy Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
2005-02-27 01:45:09 +03:00
*
* 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.
2005-02-27 01:45:09 +03:00
*
* 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.
*/
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1988, 1991, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* 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
1993-03-21 12:45:37 +03:00
* 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.
*
1998-03-01 05:20:01 +03:00
* @(#)rtsock.c 8.7 (Berkeley) 10/12/95
1993-03-21 12:45:37 +03:00
*/
2001-11-13 02:49:33 +03:00
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.255 2020/03/09 21:20:55 roy Exp $");
2001-11-13 02:49:33 +03:00
#ifdef _KERNEL_OPT
#include "opt_inet.h"
2009-01-11 05:45:45 +03:00
#include "opt_compat_netbsd.h"
#endif
1993-12-18 03:40:47 +03:00
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/domain.h>
#include <sys/protosw.h>
1996-02-14 00:59:53 +03:00
#include <sys/sysctl.h>
2006-05-15 01:19:33 +04:00
#include <sys/kauth.h>
#include <sys/kmem.h>
#include <sys/intr.h>
#include <sys/condvar.h>
2019-01-27 05:08:33 +03:00
#include <sys/compat_stub.h>
1996-02-14 00:59:53 +03:00
1993-12-18 03:40:47 +03:00
#include <net/if.h>
2016-04-04 10:37:07 +03:00
#include <net/if_llatbl.h>
#include <net/if_types.h>
1993-12-18 03:40:47 +03:00
#include <net/route.h>
#include <net/raw_cb.h>
1993-03-21 12:45:37 +03:00
2016-04-04 10:37:07 +03:00
#include <netinet/in_var.h>
#include <netinet/if_inarp.h>
#include <netmpls/mpls.h>
2019-01-27 05:08:33 +03:00
#include <compat/net/if.h>
#include <compat/net/route.h>
1993-03-21 12:45:37 +03:00
2019-01-27 05:08:33 +03:00
#ifdef COMPAT_RTSOCK
#undef COMPAT_RTSOCK
#endif
2019-01-27 05:08:33 +03:00
static int if_addrflags(struct ifaddr *);
2019-01-27 05:08:33 +03:00
#include <net/rtsock_shared.c>
1993-03-21 12:45:37 +03:00
/*
* XXX avoid using void * once msghdr compat disappears.
*/
void
rt_setmetrics(void *in, struct rtentry *out)
{
const struct rt_xmsghdr *rtm = in;
_rt_setmetrics(rtm->rtm_inits, rtm, out);
}
2016-04-04 10:37:07 +03:00
int
rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
int *lenp)
{
return rt_msg2(type, rtinfo, cpv, w, lenp);
}
static int
if_addrflags(struct ifaddr *ifa)
{
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
return ifatoia(ifa)->ia4_flags;
#endif
#ifdef INET6
case AF_INET6:
return ifatoia6(ifa)->ia6_flags;
#endif
default:
return 0;
}
}
/*
* Send a routing message as mimicing that a cloned route is added.
*/
void
rt_clonedmsg(int type, const struct sockaddr *src, const struct sockaddr *dst,
const uint8_t *lladdr, const struct ifnet *ifp)
{
struct rt_addrinfo info;
/* Mimic flags exactly */
#define RTF_LLINFO 0x400
#define RTF_CLONED 0x2000
int flags = RTF_DONE;
union {
struct sockaddr sa;
struct sockaddr_storage ss;
struct sockaddr_dl sdl;
} u;
if (type != RTM_MISS)
flags |= RTF_HOST | RTF_CLONED | RTF_LLINFO;
if (type == RTM_ADD || type == RTM_CHANGE)
flags |= RTF_UP;
memset(&info, 0, sizeof(info));
info.rti_info[RTAX_AUTHOR] = src;
info.rti_info[RTAX_DST] = dst;
sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type,
NULL, 0, lladdr, ifp->if_addrlen);
info.rti_info[RTAX_GATEWAY] = &u.sa;
rt_missmsg(type, &info, flags, 0);
#undef RTF_LLINFO
#undef RTF_CLONED
}
2019-01-27 05:08:33 +03:00
/*
* 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().
1993-03-21 12:45:37 +03:00
*/
static int
sysctl_dumpentry(struct rtentry *rt, void *v)
1993-03-21 12:45:37 +03:00
{
2009-01-11 05:45:45 +03:00
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;
2001-07-18 20:43:09 +04:00
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);
1995-08-19 11:48:14 +04:00
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;
1995-08-19 11:48:14 +04:00
if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1995-08-19 11:48:14 +04:00
}
if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
Take steps to hide the radix_node implementation of the forwarding table from the forwarding table's users: Introduce rt_walktree() for walking the routing table and applying a function to each rtentry. Replace most rn_walktree() calls with it. Use rt_getkey()/rt_setkey() to get/set a route's destination. Keep a pointer to the sockaddr key in the rtentry, so that rtentry users do not have to grovel in the radix_node for the key. Add a RTM_GET method to rtrequest. Use that instead of radix_node lookups in, e.g., carp(4). Add sys/net/link_proto.c, which supplies sockaddr routines for link-layer socket addresses (sockaddr_dl). Cosmetic: Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH, et cetera. Use NULL instead of 0 for null pointers. Use __arraycount(). Reduce gratuitous parenthesization. Stop using variadic arguments for rip6_output(), it is unnecessary. Remove the unnecessary rtentry member rt_genmask and the code to maintain it, since nothing actually used it. Make rt_maskedcopy() easier to read by using meaningful variable names. Extract a subroutine intern_netmask() for looking up a netmask in the masks table. Start converting backslash-ridden IPv6 macros in sys/netinet6/in6_var.h into inline subroutines that one can read without special eyeglasses. One functional change: when the kernel serves an RTM_GET, RTM_LOCK, or RTM_CHANGE request, it applies the netmask (if supplied) to a destination before searching for it in the forwarding table. I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove the unlawful radix_node knowledge. Apart from the changes to carp(4), netiso, ATM, and strip(4), I have run the changes on three nodes in my wireless routing testbed, which involves IPv4 + IPv6 dynamic routing acrobatics, and it's working beautifully so far.
2007-07-20 00:48:52 +04:00
return error;
if (w->w_where && w->w_tmem && w->w_needed <= 0) {
struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
rtm->rtm_flags = rt->rt_flags;
rtm->rtm_use = rt->rt_use;
rtm_setmetrics(rt, rtm);
KASSERT(rt->rt_ifp != NULL);
rtm->rtm_index = rt->rt_ifp->if_index;
rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
rtm->rtm_addrs = info.rti_addrs;
if ((error = copyout(rtm, w->w_where, size)) != 0)
w->w_where = NULL;
else
w->w_where = (char *)w->w_where + size;
}
Take steps to hide the radix_node implementation of the forwarding table from the forwarding table's users: Introduce rt_walktree() for walking the routing table and applying a function to each rtentry. Replace most rn_walktree() calls with it. Use rt_getkey()/rt_setkey() to get/set a route's destination. Keep a pointer to the sockaddr key in the rtentry, so that rtentry users do not have to grovel in the radix_node for the key. Add a RTM_GET method to rtrequest. Use that instead of radix_node lookups in, e.g., carp(4). Add sys/net/link_proto.c, which supplies sockaddr routines for link-layer socket addresses (sockaddr_dl). Cosmetic: Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH, et cetera. Use NULL instead of 0 for null pointers. Use __arraycount(). Reduce gratuitous parenthesization. Stop using variadic arguments for rip6_output(), it is unnecessary. Remove the unnecessary rtentry member rt_genmask and the code to maintain it, since nothing actually used it. Make rt_maskedcopy() easier to read by using meaningful variable names. Extract a subroutine intern_netmask() for looking up a netmask in the masks table. Start converting backslash-ridden IPv6 macros in sys/netinet6/in6_var.h into inline subroutines that one can read without special eyeglasses. One functional change: when the kernel serves an RTM_GET, RTM_LOCK, or RTM_CHANGE request, it applies the netmask (if supplied) to a destination before searching for it in the forwarding table. I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove the unlawful radix_node knowledge. Apart from the changes to carp(4), netiso, ATM, and strip(4), I have run the changes on three nodes in my wireless routing testbed, which involves IPv4 + IPv6 dynamic routing acrobatics, and it's working beautifully so far.
2007-07-20 00:48:52 +04:00
return error;
}
1993-03-21 12:45:37 +03:00
static int
sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
struct rt_addrinfo *info, size_t len)
{
struct if_xmsghdr *ifm;
int error;
ifm = (struct if_xmsghdr *)w->w_tmem;
ifm->ifm_index = ifp->if_index;
ifm->ifm_flags = ifp->if_flags;
if_export_if_data(ifp, &ifm->ifm_data, false);
ifm->ifm_addrs = info->rti_addrs;
if ((error = copyout(ifm, w->w_where, len)) == 0)
w->w_where = (char *)w->w_where + len;
return error;
}
static int
sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
struct rt_addrinfo *info)
{
int len, error;
if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
return error;
if (w->w_where && w->w_tmem && w->w_needed <= 0) {
struct ifa_xmsghdr *ifam;
ifam = (struct ifa_xmsghdr *)w->w_tmem;
ifam->ifam_index = ifa->ifa_ifp->if_index;
ifam->ifam_flags = ifa->ifa_flags;
ifam->ifam_metric = ifa->ifa_metric;
ifam->ifam_addrs = info->rti_addrs;
ifam->ifam_pid = 0;
ifam->ifam_addrflags = if_addrflags(ifa);
if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
w->w_where = (char *)w->w_where + len;
}
return error;
}
static int
2009-01-11 05:45:45 +03:00
sysctl_iflist(int af, struct rt_walkarg *w, int type)
{
2000-03-30 13:45:33 +04:00
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:
2017-03-14 07:23:15 +03:00
#ifdef RTSOCK_DEBUG
printf("%s: unsupported IFLIST type %d\n", __func__, type);
#endif
return EINVAL;
}
2001-07-18 20:43:09 +04:00
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) {
2019-01-27 05:08:33 +03:00
switch (type) {
case NET_RT_OIFLIST: /* old _70 */
if (!rtsock_iflist_70_hook.hooked) {
2019-01-27 05:08:33 +03:00
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,
2019-01-27 05:08:33 +03:00
(ifp, w, &info, len), enosys(), error);
break;
case NET_RT_OOOIFLIST: /* old _14 */
MODULE_HOOK_CALL(rtsock_iflist_14_hook,
2019-01-27 05:08:33 +03:00
(ifp, w, &info, len), enosys(), error);
break;
default:
error = EINVAL;
}
if (error != 0) {
if (error == ENOSYS)
error = EINVAL;
goto release_exit;
2019-01-27 05:08:33 +03:00
}
}
_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;
2019-01-27 05:08:33 +03:00
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,
2019-01-27 05:08:33 +03:00
(w, ifa, &info), enosys(), error);
break;
default:
error = EINVAL;
}
_s = pserialize_read_enter();
ifa_release(ifa, &_psref);
if (error != 0) {
pserialize_read_exit(_s);
goto release_exit;
}
}
pserialize_read_exit(_s);
info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
info.rti_info[RTAX_BRD] = NULL;
s = pserialize_read_enter();
if_release(ifp, &psref);
1993-03-21 12:45:37 +03:00
}
pserialize_read_exit(s);
curlwp_bindx(bound);
Take steps to hide the radix_node implementation of the forwarding table from the forwarding table's users: Introduce rt_walktree() for walking the routing table and applying a function to each rtentry. Replace most rn_walktree() calls with it. Use rt_getkey()/rt_setkey() to get/set a route's destination. Keep a pointer to the sockaddr key in the rtentry, so that rtentry users do not have to grovel in the radix_node for the key. Add a RTM_GET method to rtrequest. Use that instead of radix_node lookups in, e.g., carp(4). Add sys/net/link_proto.c, which supplies sockaddr routines for link-layer socket addresses (sockaddr_dl). Cosmetic: Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH, et cetera. Use NULL instead of 0 for null pointers. Use __arraycount(). Reduce gratuitous parenthesization. Stop using variadic arguments for rip6_output(), it is unnecessary. Remove the unnecessary rtentry member rt_genmask and the code to maintain it, since nothing actually used it. Make rt_maskedcopy() easier to read by using meaningful variable names. Extract a subroutine intern_netmask() for looking up a netmask in the masks table. Start converting backslash-ridden IPv6 macros in sys/netinet6/in6_var.h into inline subroutines that one can read without special eyeglasses. One functional change: when the kernel serves an RTM_GET, RTM_LOCK, or RTM_CHANGE request, it applies the netmask (if supplied) to a destination before searching for it in the forwarding table. I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove the unlawful radix_node knowledge. Apart from the changes to carp(4), netiso, ATM, and strip(4), I have run the changes on three nodes in my wireless routing testbed, which involves IPv4 + IPv6 dynamic routing acrobatics, and it's working beautifully so far.
2007-07-20 00:48:52 +04:00
return 0;
release_exit:
if_release(ifp, &psref);
curlwp_bindx(bound);
return error;
1993-03-21 12:45:37 +03:00
}
static int
sysctl_rtable(SYSCTLFN_ARGS)
1993-03-21 12:45:37 +03:00
{
void *where = oldp;
size_t *given = oldlenp;
int i, s, error = EINVAL;
u_char af;
2009-01-11 05:45:45 +03:00
struct rt_walkarg w;
1993-03-21 12:45:37 +03:00
if (namelen == 1 && name[0] == CTL_QUERY)
Take steps to hide the radix_node implementation of the forwarding table from the forwarding table's users: Introduce rt_walktree() for walking the routing table and applying a function to each rtentry. Replace most rn_walktree() calls with it. Use rt_getkey()/rt_setkey() to get/set a route's destination. Keep a pointer to the sockaddr key in the rtentry, so that rtentry users do not have to grovel in the radix_node for the key. Add a RTM_GET method to rtrequest. Use that instead of radix_node lookups in, e.g., carp(4). Add sys/net/link_proto.c, which supplies sockaddr routines for link-layer socket addresses (sockaddr_dl). Cosmetic: Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH, et cetera. Use NULL instead of 0 for null pointers. Use __arraycount(). Reduce gratuitous parenthesization. Stop using variadic arguments for rip6_output(), it is unnecessary. Remove the unnecessary rtentry member rt_genmask and the code to maintain it, since nothing actually used it. Make rt_maskedcopy() easier to read by using meaningful variable names. Extract a subroutine intern_netmask() for looking up a netmask in the masks table. Start converting backslash-ridden IPv6 macros in sys/netinet6/in6_var.h into inline subroutines that one can read without special eyeglasses. One functional change: when the kernel serves an RTM_GET, RTM_LOCK, or RTM_CHANGE request, it applies the netmask (if supplied) to a destination before searching for it in the forwarding table. I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove the unlawful radix_node knowledge. Apart from the changes to carp(4), netiso, ATM, and strip(4), I have run the changes on three nodes in my wireless routing testbed, which involves IPv4 + IPv6 dynamic routing acrobatics, and it's working beautifully so far.
2007-07-20 00:48:52 +04:00
return sysctl_query(SYSCTLFN_CALL(rnode));
2014-09-05 10:00:05 +04:00
if (newp)
Take steps to hide the radix_node implementation of the forwarding table from the forwarding table's users: Introduce rt_walktree() for walking the routing table and applying a function to each rtentry. Replace most rn_walktree() calls with it. Use rt_getkey()/rt_setkey() to get/set a route's destination. Keep a pointer to the sockaddr key in the rtentry, so that rtentry users do not have to grovel in the radix_node for the key. Add a RTM_GET method to rtrequest. Use that instead of radix_node lookups in, e.g., carp(4). Add sys/net/link_proto.c, which supplies sockaddr routines for link-layer socket addresses (sockaddr_dl). Cosmetic: Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH, et cetera. Use NULL instead of 0 for null pointers. Use __arraycount(). Reduce gratuitous parenthesization. Stop using variadic arguments for rip6_output(), it is unnecessary. Remove the unnecessary rtentry member rt_genmask and the code to maintain it, since nothing actually used it. Make rt_maskedcopy() easier to read by using meaningful variable names. Extract a subroutine intern_netmask() for looking up a netmask in the masks table. Start converting backslash-ridden IPv6 macros in sys/netinet6/in6_var.h into inline subroutines that one can read without special eyeglasses. One functional change: when the kernel serves an RTM_GET, RTM_LOCK, or RTM_CHANGE request, it applies the netmask (if supplied) to a destination before searching for it in the forwarding table. I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove the unlawful radix_node knowledge. Apart from the changes to carp(4), netiso, ATM, and strip(4), I have run the changes on three nodes in my wireless routing testbed, which involves IPv4 + IPv6 dynamic routing acrobatics, and it's working beautifully so far.
2007-07-20 00:48:52 +04:00
return EPERM;
if (namelen != 3)
Take steps to hide the radix_node implementation of the forwarding table from the forwarding table's users: Introduce rt_walktree() for walking the routing table and applying a function to each rtentry. Replace most rn_walktree() calls with it. Use rt_getkey()/rt_setkey() to get/set a route's destination. Keep a pointer to the sockaddr key in the rtentry, so that rtentry users do not have to grovel in the radix_node for the key. Add a RTM_GET method to rtrequest. Use that instead of radix_node lookups in, e.g., carp(4). Add sys/net/link_proto.c, which supplies sockaddr routines for link-layer socket addresses (sockaddr_dl). Cosmetic: Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH, et cetera. Use NULL instead of 0 for null pointers. Use __arraycount(). Reduce gratuitous parenthesization. Stop using variadic arguments for rip6_output(), it is unnecessary. Remove the unnecessary rtentry member rt_genmask and the code to maintain it, since nothing actually used it. Make rt_maskedcopy() easier to read by using meaningful variable names. Extract a subroutine intern_netmask() for looking up a netmask in the masks table. Start converting backslash-ridden IPv6 macros in sys/netinet6/in6_var.h into inline subroutines that one can read without special eyeglasses. One functional change: when the kernel serves an RTM_GET, RTM_LOCK, or RTM_CHANGE request, it applies the netmask (if supplied) to a destination before searching for it in the forwarding table. I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove the unlawful radix_node knowledge. Apart from the changes to carp(4), netiso, ATM, and strip(4), I have run the changes on three nodes in my wireless routing testbed, which involves IPv4 + IPv6 dynamic routing acrobatics, and it's working beautifully so far.
2007-07-20 00:48:52 +04:00
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;
1993-03-21 12:45:37 +03:00
KERNEL_LOCK_UNLESS_NET_MPSAFE();
1995-08-13 03:59:09 +04:00
s = splsoftnet();
switch (w.w_op) {
case NET_RT_DUMP:
case NET_RT_FLAGS:
#if defined(INET) || defined(INET6)
2016-04-04 10:37:07 +03:00
/*
* 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);
2016-04-04 10:37:07 +03:00
else
error = EINVAL;
break;
}
#endif
2016-04-04 10:37:07 +03:00
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;
2019-01-27 05:08:33 +03:00
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;
1993-03-21 12:45:37 +03:00
}
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;
if (w.w_tmem)
2017-02-21 07:00:01 +03:00
kmem_free(w.w_tmem, w.w_tmemsize);
1993-03-21 12:45:37 +03:00
w.w_needed += w.w_given;
if (where) {
*given = (char *)w.w_where - (char *)where;
if (*given < w.w_needed)
Take steps to hide the radix_node implementation of the forwarding table from the forwarding table's users: Introduce rt_walktree() for walking the routing table and applying a function to each rtentry. Replace most rn_walktree() calls with it. Use rt_getkey()/rt_setkey() to get/set a route's destination. Keep a pointer to the sockaddr key in the rtentry, so that rtentry users do not have to grovel in the radix_node for the key. Add a RTM_GET method to rtrequest. Use that instead of radix_node lookups in, e.g., carp(4). Add sys/net/link_proto.c, which supplies sockaddr routines for link-layer socket addresses (sockaddr_dl). Cosmetic: Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH, et cetera. Use NULL instead of 0 for null pointers. Use __arraycount(). Reduce gratuitous parenthesization. Stop using variadic arguments for rip6_output(), it is unnecessary. Remove the unnecessary rtentry member rt_genmask and the code to maintain it, since nothing actually used it. Make rt_maskedcopy() easier to read by using meaningful variable names. Extract a subroutine intern_netmask() for looking up a netmask in the masks table. Start converting backslash-ridden IPv6 macros in sys/netinet6/in6_var.h into inline subroutines that one can read without special eyeglasses. One functional change: when the kernel serves an RTM_GET, RTM_LOCK, or RTM_CHANGE request, it applies the netmask (if supplied) to a destination before searching for it in the forwarding table. I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove the unlawful radix_node knowledge. Apart from the changes to carp(4), netiso, ATM, and strip(4), I have run the changes on three nodes in my wireless routing testbed, which involves IPv4 + IPv6 dynamic routing acrobatics, and it's working beautifully so far.
2007-07-20 00:48:52 +04:00
return ENOMEM;
} else {
*given = (11 * w.w_needed) / 10;
1993-03-21 12:45:37 +03:00
}
Take steps to hide the radix_node implementation of the forwarding table from the forwarding table's users: Introduce rt_walktree() for walking the routing table and applying a function to each rtentry. Replace most rn_walktree() calls with it. Use rt_getkey()/rt_setkey() to get/set a route's destination. Keep a pointer to the sockaddr key in the rtentry, so that rtentry users do not have to grovel in the radix_node for the key. Add a RTM_GET method to rtrequest. Use that instead of radix_node lookups in, e.g., carp(4). Add sys/net/link_proto.c, which supplies sockaddr routines for link-layer socket addresses (sockaddr_dl). Cosmetic: Constify. KNF. Stop open-coding LIST_FOREACH, TAILQ_FOREACH, et cetera. Use NULL instead of 0 for null pointers. Use __arraycount(). Reduce gratuitous parenthesization. Stop using variadic arguments for rip6_output(), it is unnecessary. Remove the unnecessary rtentry member rt_genmask and the code to maintain it, since nothing actually used it. Make rt_maskedcopy() easier to read by using meaningful variable names. Extract a subroutine intern_netmask() for looking up a netmask in the masks table. Start converting backslash-ridden IPv6 macros in sys/netinet6/in6_var.h into inline subroutines that one can read without special eyeglasses. One functional change: when the kernel serves an RTM_GET, RTM_LOCK, or RTM_CHANGE request, it applies the netmask (if supplied) to a destination before searching for it in the forwarding table. I have changed sys/netinet/ip_carp.c, carp_setroute(), to remove the unlawful radix_node knowledge. Apart from the changes to carp(4), netiso, ATM, and strip(4), I have run the changes on three nodes in my wireless routing testbed, which involves IPv4 + IPv6 dynamic routing acrobatics, and it's working beautifully so far.
2007-07-20 00:48:52 +04:00
return error;
1993-03-21 12:45:37 +03:00
}
void
sysctl_net_route_setup(struct sysctllog **clog, int pf, const char *name)
{
2006-05-28 03:08:11 +04:00
const struct sysctlnode *rnode = NULL;
sysctl_createv(clog, 0, NULL, &rnode,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, name,
SYSCTL_DESCR("PF_ROUTE information"),
NULL, 0, NULL, 0,
CTL_NET, pf, CTL_EOL);
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "rtable",
SYSCTL_DESCR("Routing table information"),
sysctl_rtable, 0, NULL, 0,
CTL_NET, pf, 0 /* any protocol */, CTL_EOL);
2006-05-28 03:08:11 +04:00
sysctl_createv(clog, 0, &rnode, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_STRUCT, "stats",
SYSCTL_DESCR("Routing statistics"),
NULL, 0, &rtstat, sizeof(rtstat),
CTL_CREATE, CTL_EOL);
}