#include "opt_inet.h"
#include "opt_inet6.h"
#include "opt_mrouting.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/sysproto.h>
#include <sys/proc.h>
#include <sys/devctl.h>
#include <sys/domain.h>
#include <sys/eventhandler.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/rmlock.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_private.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <net/route/route_ctl.h>
#include <net/route/route_var.h>
#include <net/route/nhop.h>
#include <net/vnet.h>
#include <netinet/in.h>
#include <netinet/ip_mroute.h>
#include <netinet6/in6_var.h>
VNET_PCPUSTAT_DEFINE(struct rtstat, rtstat);
VNET_PCPUSTAT_SYSINIT(rtstat);
#ifdef VIMAGE
VNET_PCPUSTAT_SYSUNINIT(rtstat);
#endif
SYSCTL_DECL(_net_route);
SYSCTL_VNET_PCPUSTAT(_net_route, OID_AUTO, stats, struct rtstat,
rtstat, "route statistics");
EVENTHANDLER_LIST_DEFINE(rt_addrmsg);
static int rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *,
void *arg);
static void
route_init(void *dummy __unused)
{
nhops_init();
}
SYSINIT(route_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, NULL);
struct rib_head *
rt_table_init(int offset, int family, u_int fibnum)
{
struct rib_head *rh;
rh = malloc(sizeof(struct rib_head), M_RTABLE, M_WAITOK | M_ZERO);
rn_inithead_internal(&rh->head, rh->rnh_nodes, offset);
rn_inithead_internal(&rh->rmhead.head, rh->rmhead.mask_nodes, 0);
rh->head.rnh_masks = &rh->rmhead;
rh->rib_family = family;
rh->rib_fibnum = fibnum;
#ifdef VIMAGE
rh->rib_vnet = curvnet;
#endif
tmproutes_init(rh);
RIB_LOCK_INIT(rh);
nhops_init_rib(rh);
rib_init_subscriptions(rh);
rh->rnh_addaddr = rn_addroute;
rh->rnh_deladdr = rn_delete;
rh->rnh_matchaddr = rn_match;
rh->rnh_lookup = rn_lookup;
rh->rnh_walktree = rn_walktree;
rh->rnh_walktree_from = rn_walktree_from;
return (rh);
}
static int
rt_freeentry(struct radix_node *rn, void *arg)
{
struct radix_head * const rnh = arg;
struct radix_node *x;
x = (struct radix_node *)rn_delete(rn + 2, NULL, rnh);
if (x != NULL)
R_Free(x);
return (0);
}
void
rt_table_destroy(struct rib_head *rh)
{
RIB_WLOCK(rh);
rh->rib_dying = true;
RIB_WUNLOCK(rh);
#ifdef FIB_ALGO
fib_destroy_rib(rh);
#endif
tmproutes_destroy(rh);
rn_walktree(&rh->rmhead.head, rt_freeentry, &rh->rmhead.head);
nhops_destroy_rib(rh);
rib_destroy_subscriptions(rh);
RIB_LOCK_DESTROY(rh);
free(rh, M_RTABLE);
}
int
rib_add_redirect(u_int fibnum, struct sockaddr *dst, struct sockaddr *gateway,
struct sockaddr *author, struct ifnet *ifp, int flags, int lifetime_sec)
{
struct route_nhop_data rnd = { .rnd_weight = RT_DEFAULT_WEIGHT };
struct rib_cmd_info rc;
struct ifaddr *ifa;
int error;
NET_EPOCH_ASSERT();
if (rt_tables_get_rnh(fibnum, dst->sa_family) == NULL)
return (EAFNOSUPPORT);
KASSERT(((flags & ~(RTF_GATEWAY)) == 0),
("invalid redirect flags: %x", flags));
flags |= RTF_HOST | RTF_DYNAMIC;
if ((ifa = ifaof_ifpforaddr(gateway, ifp)) == NULL)
return (ENETUNREACH);
struct nhop_object *nh = nhop_alloc(fibnum, dst->sa_family);
if (nh == NULL)
return (ENOMEM);
nhop_set_gw(nh, gateway, flags & RTF_GATEWAY);
nhop_set_transmit_ifp(nh, ifp);
nhop_set_src(nh, ifa);
nhop_set_pxtype_flag(nh, NHF_HOST);
nhop_set_expire(nh, lifetime_sec + time_uptime);
nhop_set_redirect(nh, true);
nhop_set_origin(nh, NH_ORIGIN_REDIRECT);
rnd.rnd_nhop = nhop_get_nhop(nh, &error);
if (error == 0) {
error = rib_add_route_px(fibnum, dst, -1,
&rnd, RTM_F_CREATE, &rc);
}
if (error != 0) {
return (error);
}
RTSTAT_INC(rts_dynamic);
struct rt_addrinfo info = {
.rti_info[RTAX_DST] = dst,
.rti_info[RTAX_GATEWAY] = gateway,
.rti_info[RTAX_AUTHOR] = author,
};
rt_missmsg_fib(RTM_REDIRECT, &info, flags | RTF_UP, error, fibnum);
return (0);
}
int
rtioctl_fib(u_long req, caddr_t data, u_int fibnum)
{
#ifdef INET
return mrt_ioctl ? mrt_ioctl(req, data, fibnum) : EOPNOTSUPP;
#else
return ENXIO;
#endif
}
struct ifaddr *
ifa_ifwithroute(int flags, const struct sockaddr *dst,
const struct sockaddr *gateway, u_int fibnum)
{
struct ifaddr *ifa;
NET_EPOCH_ASSERT();
if ((flags & RTF_GATEWAY) == 0) {
ifa = NULL;
if (flags & RTF_HOST)
ifa = ifa_ifwithdstaddr(dst, fibnum);
if (ifa == NULL)
ifa = ifa_ifwithaddr(gateway);
} else {
ifa = ifa_ifwithdstaddr(gateway, fibnum);
}
if (ifa == NULL)
ifa = ifa_ifwithnet(gateway, 0, fibnum);
if (ifa == NULL) {
struct nhop_object *nh;
nh = rib_lookup(fibnum, gateway, NHR_NONE, 0);
if ((nh == NULL) || (nh->nh_flags & NHF_DEFAULT))
return (NULL);
ifa = nh->nh_ifa;
}
if (ifa->ifa_addr->sa_family != dst->sa_family) {
struct ifaddr *oifa = ifa;
ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
if (ifa == NULL)
ifa = oifa;
}
return (ifa);
}
static int
rt_ifdelroute(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
{
struct ifnet *ifp = arg;
if (nh->nh_ifp != ifp)
return (0);
if ((rt->rte_flags & RTF_UP) == 0)
return (0);
return (1);
}
void
rt_flushifroutes(struct ifnet *ifp)
{
rib_foreach_table_walk_del(AF_UNSPEC, rt_ifdelroute, ifp);
}
static struct ifnet *
info_get_ifp(struct rt_addrinfo *info)
{
const struct sockaddr_dl *sdl;
sdl = (const struct sockaddr_dl *)info->rti_info[RTAX_IFP];
if (sdl->sdl_family != AF_LINK)
return (NULL);
if (sdl->sdl_index != 0)
return (ifnet_byindex(sdl->sdl_index));
if (sdl->sdl_nlen > 0) {
char if_name[IF_NAMESIZE];
if (sdl->sdl_nlen + offsetof(struct sockaddr_dl, sdl_data) > sdl->sdl_len)
return (NULL);
if (sdl->sdl_nlen >= IF_NAMESIZE)
return (NULL);
bzero(if_name, sizeof(if_name));
memcpy(if_name, sdl->sdl_data, sdl->sdl_nlen);
return (ifunit(if_name));
}
return (NULL);
}
__noinline static int
rt_getifa_family(struct rt_addrinfo *info, uint32_t fibnum)
{
if (info->rti_ifp == NULL) {
struct ifaddr *ifa = NULL;
const struct sockaddr *gw = info->rti_info[RTAX_GATEWAY];
ifa = ifa_ifwithroute(RTF_GATEWAY, gw, gw, fibnum);
if (ifa == NULL)
return (ENETUNREACH);
info->rti_ifp = ifa->ifa_ifp;
}
info->rti_ifa = ifaof_ifpforaddr(info->rti_info[RTAX_DST], info->rti_ifp);
#ifdef INET
if (info->rti_ifa == NULL) {
bool loopback_ok = info->rti_ifp->if_flags & IFF_LOOPBACK;
info->rti_ifa = (struct ifaddr *)in_findlocal(fibnum, loopback_ok);
}
#endif
if (info->rti_ifa == NULL)
return (ENETUNREACH);
return (0);
}
int
rt_getifa_fib(struct rt_addrinfo *info, u_int fibnum)
{
const struct sockaddr *dst, *gateway, *ifaaddr;
int error, flags;
dst = info->rti_info[RTAX_DST];
gateway = info->rti_info[RTAX_GATEWAY];
ifaaddr = info->rti_info[RTAX_IFA];
flags = info->rti_flags;
error = 0;
if ((info->rti_ifp == NULL) && (info->rti_info[RTAX_IFP] != NULL))
info->rti_ifp = info_get_ifp(info);
if (info->rti_ifa == NULL && ifaaddr != NULL)
info->rti_ifa = ifa_ifwithaddr(ifaaddr);
if ((info->rti_ifa == NULL) && ((info->rti_flags & RTF_GATEWAY) != 0) &&
(gateway->sa_family != dst->sa_family))
return (rt_getifa_family(info, fibnum));
if (info->rti_ifa == NULL) {
const struct sockaddr *sa;
if (info->rti_info[RTAX_IFA] != NULL)
sa = info->rti_info[RTAX_IFA];
else if ((info->rti_flags & RTF_GATEWAY) != 0 &&
gateway->sa_family == dst->sa_family)
sa = gateway;
else
sa = dst;
if (info->rti_ifp != NULL) {
info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
if (info->rti_ifa == NULL && gateway != NULL)
info->rti_ifa = ifaof_ifpforaddr(gateway, info->rti_ifp);
} else if (dst != NULL && gateway != NULL)
info->rti_ifa = ifa_ifwithroute(flags, dst, gateway,
fibnum);
else if (sa != NULL)
info->rti_ifa = ifa_ifwithroute(flags, sa, sa,
fibnum);
}
if (info->rti_ifa != NULL) {
if (info->rti_ifp == NULL)
info->rti_ifp = info->rti_ifa->ifa_ifp;
} else
error = ENETUNREACH;
return (error);
}
void
rt_updatemtu(struct ifnet *ifp)
{
#ifdef INET6
uint32_t in6mtu;
in6mtu = in6_ifmtu(ifp);
#endif
for (u_int j = 0; j < rt_numfibs; j++) {
#ifdef INET
nhops_update_ifmtu(rt_tables_get_rnh(j, AF_INET), ifp,
ifp->if_mtu);
#endif
#ifdef INET6
nhops_update_ifmtu(rt_tables_get_rnh(j, AF_INET6), ifp, in6mtu);
#endif
}
}
#if 0
int p_sockaddr(char *buf, int buflen, struct sockaddr *s);
int rt_print(char *buf, int buflen, struct rtentry *rt);
int
p_sockaddr(char *buf, int buflen, struct sockaddr *s)
{
void *paddr = NULL;
switch (s->sa_family) {
case AF_INET:
paddr = &((struct sockaddr_in *)s)->sin_addr;
break;
case AF_INET6:
paddr = &((struct sockaddr_in6 *)s)->sin6_addr;
break;
}
if (paddr == NULL)
return (0);
if (inet_ntop(s->sa_family, paddr, buf, buflen) == NULL)
return (0);
return (strlen(buf));
}
int
rt_print(char *buf, int buflen, struct rtentry *rt)
{
struct sockaddr *addr, *mask;
int i = 0;
addr = rt_key(rt);
mask = rt_mask(rt);
i = p_sockaddr(buf, buflen, addr);
if (!(rt->rt_flags & RTF_HOST)) {
buf[i++] = '/';
i += p_sockaddr(buf + i, buflen - i, mask);
}
if (rt->rt_flags & RTF_GATEWAY) {
buf[i++] = '>';
i += p_sockaddr(buf + i, buflen - i, &rt->rt_nhop->gw_sa);
}
return (i);
}
#endif
void
rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
const struct sockaddr *netmask)
{
const u_char *cp1 = (const u_char *)src;
u_char *cp2 = (u_char *)dst;
const u_char *cp3 = (const u_char *)netmask;
u_char *cplim = cp2 + *cp3;
u_char *cplim2 = cp2 + *cp1;
*cp2++ = *cp1++; *cp2++ = *cp1++;
cp3 += 2;
if (cplim > cplim2)
cplim = cplim2;
while (cp2 < cplim)
*cp2++ = *cp1++ & *cp3++;
if (cp2 < cplim2)
bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
}
int
rt_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
{
#if defined(INET) || defined(INET6)
struct sockaddr *sa = ifa->ifa_addr;
struct ifnet *ifp = ifa->ifa_ifp;
#endif
KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
("unexpected cmd %d", cmd));
KASSERT((fibnum >= 0 && fibnum < rt_numfibs),
("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
EVENTHANDLER_DIRECT_INVOKE(rt_addrmsg, ifa, cmd);
#ifdef INET
if (sa->sa_family == AF_INET) {
char addrstr[INET_ADDRSTRLEN];
char strbuf[INET_ADDRSTRLEN + 12];
inet_ntoa_r(((struct sockaddr_in *)sa)->sin_addr, addrstr);
snprintf(strbuf, sizeof(strbuf), "address=%s", addrstr);
devctl_notify("IFNET", ifp->if_xname,
(cmd == RTM_ADD) ? "ADDR_ADD" : "ADDR_DEL", strbuf);
}
#endif
#ifdef INET6
if (sa->sa_family == AF_INET6) {
char addrstr[INET6_ADDRSTRLEN];
char strbuf[INET6_ADDRSTRLEN + 12];
ip6_sprintf(addrstr, IFA_IN6(ifa));
snprintf(strbuf, sizeof(strbuf), "address=%s", addrstr);
devctl_notify("IFNET", ifp->if_xname,
(cmd == RTM_ADD) ? "ADDR_ADD" : "ADDR_DEL", strbuf);
}
#endif
if (V_rt_add_addr_allfibs)
fibnum = RT_ALL_FIBS;
return (rtsock_addrmsg(cmd, ifa, fibnum));
}
int
rt_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh,
int fibnum)
{
KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
("unexpected cmd %d", cmd));
KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
KASSERT(rt_key(rt) != NULL, (":%s: rt_key must be supplied", __func__));
return (rtsock_routemsg(cmd, rt, nh, fibnum));
}
int
rt_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum)
{
KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE || cmd == RTM_CHANGE,
("unexpected cmd %d", cmd));
KASSERT(fibnum == RT_ALL_FIBS || (fibnum >= 0 && fibnum < rt_numfibs),
("%s: fib out of range 0 <=%d<%d", __func__, fibnum, rt_numfibs));
KASSERT(info->rti_info[RTAX_DST] != NULL, (":%s: RTAX_DST must be supplied", __func__));
return (rtsock_routemsg_info(cmd, info, fibnum));
}
void
rt_ifmsg(struct ifnet *ifp, int if_flags_mask)
{
rtsock_callback_p->ifmsg_f(ifp, if_flags_mask);
netlink_callback_p->ifmsg_f(ifp, if_flags_mask);
}