lokinet/llarp/net/route.cpp

562 lines
16 KiB
C++
Raw Permalink Normal View History

2020-08-21 15:07:37 +00:00
#include "route.hpp"
2020-08-21 16:03:07 +00:00
#ifdef __linux__
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <net/if.h>
#include <arpa/inet.h>
#ifndef ANDROID
2020-08-21 16:03:07 +00:00
#include <sys/socket.h>
#include <linux/rtnetlink.h>
#endif
#include "net.hpp"
2020-08-21 19:09:13 +00:00
#include <exception>
#include <charconv>
2020-08-21 16:03:07 +00:00
#endif
2020-08-25 13:51:39 +00:00
#ifdef __APPLE__
#include "net.hpp"
#include <llarp/util/str.hpp>
2020-08-25 13:51:39 +00:00
#endif
2020-08-25 14:12:40 +00:00
#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <cstring>
#include <locale>
#include <codecvt>
#include "net_int.hpp"
#include "ip.hpp"
2020-08-25 14:12:40 +00:00
#endif
2020-08-21 16:03:07 +00:00
2020-08-21 16:07:40 +00:00
#include <sstream>
#include <llarp/util/logging/logger.hpp>
#include <llarp/util/str.hpp>
2020-08-21 16:07:40 +00:00
2020-08-21 15:07:37 +00:00
namespace llarp::net
{
#ifndef __linux__
void
Execute(std::string cmd)
{
LogInfo(cmd);
2020-08-21 15:07:37 +00:00
system(cmd.c_str());
}
#endif
2020-08-21 16:03:07 +00:00
#ifdef __linux__
#ifndef ANDROID
enum class GatewayMode
{
eFirstHop,
eLowerDefault,
eUpperDefault
};
2020-08-21 16:03:07 +00:00
struct NLSocket
{
NLSocket() : fd(socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE))
{
2020-08-21 17:24:07 +00:00
if (fd == -1)
throw std::runtime_error("failed to make netlink socket");
2020-08-21 16:03:07 +00:00
}
~NLSocket()
{
if (fd != -1)
close(fd);
}
const int fd;
};
/* Helper structure for ip address data and attributes */
typedef struct
{
2020-08-25 12:48:35 +00:00
unsigned char family;
unsigned char bitlen;
2020-08-21 16:03:07 +00:00
unsigned char data[sizeof(struct in6_addr)];
} _inet_addr;
/* */
#define NLMSG_TAIL(nmsg) ((struct rtattr*)(((intptr_t)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
2020-08-21 16:03:07 +00:00
/* Add new data to rtattr */
int
rtattr_add(struct nlmsghdr* n, unsigned int maxlen, int type, const void* data, int alen)
2020-08-21 16:03:07 +00:00
{
int len = RTA_LENGTH(alen);
struct rtattr* rta;
if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen)
{
fprintf(stderr, "rtattr_add error: message exceeded bound of %d\n", maxlen);
return -1;
}
rta = NLMSG_TAIL(n);
rta->rta_type = type;
rta->rta_len = len;
if (alen)
{
memcpy(RTA_DATA(rta), data, alen);
}
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
return 0;
}
int
do_route(
int sock,
int cmd,
int flags,
const _inet_addr* dst,
const _inet_addr* gw,
GatewayMode mode,
int if_idx)
2020-08-21 16:03:07 +00:00
{
struct
{
struct nlmsghdr n;
struct rtmsg r;
char buf[4096];
2020-08-21 17:24:07 +00:00
} nl_request{};
2020-08-21 16:03:07 +00:00
/* Initialize request structure */
nl_request.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
nl_request.n.nlmsg_flags = NLM_F_REQUEST | flags;
nl_request.n.nlmsg_type = cmd;
2020-08-21 19:09:13 +00:00
nl_request.n.nlmsg_pid = getpid();
2020-08-21 16:03:07 +00:00
nl_request.r.rtm_family = dst->family;
nl_request.r.rtm_table = RT_TABLE_MAIN;
2020-08-21 19:09:13 +00:00
if (if_idx)
{
nl_request.r.rtm_scope = RT_SCOPE_LINK;
}
else
{
nl_request.r.rtm_scope = RT_SCOPE_NOWHERE;
}
2020-08-21 16:03:07 +00:00
/* Set additional flags if NOT deleting route */
if (cmd != RTM_DELROUTE)
{
nl_request.r.rtm_protocol = RTPROT_BOOT;
nl_request.r.rtm_type = RTN_UNICAST;
}
nl_request.r.rtm_family = dst->family;
nl_request.r.rtm_dst_len = dst->bitlen;
nl_request.r.rtm_scope = 0;
2020-08-21 16:03:07 +00:00
/* Set gateway */
if (gw->bitlen != 0 and dst->family == AF_INET)
2020-08-21 16:03:07 +00:00
{
rtattr_add(&nl_request.n, sizeof(nl_request), RTA_GATEWAY, &gw->data, gw->bitlen / 8);
}
nl_request.r.rtm_family = gw->family;
if (mode == GatewayMode::eFirstHop)
2020-08-21 16:03:07 +00:00
{
rtattr_add(
&nl_request.n, sizeof(nl_request), /*RTA_NEWDST*/ RTA_DST, &dst->data, dst->bitlen / 8);
/* Set interface */
rtattr_add(&nl_request.n, sizeof(nl_request), RTA_OIF, &if_idx, sizeof(int));
}
if (mode == GatewayMode::eUpperDefault)
{
2021-02-16 15:59:18 +00:00
if (dst->family == AF_INET)
{
rtattr_add(
&nl_request.n,
sizeof(nl_request),
/*RTA_NEWDST*/ RTA_DST,
&dst->data,
sizeof(uint32_t));
}
else
{
rtattr_add(&nl_request.n, sizeof(nl_request), RTA_OIF, &if_idx, sizeof(int));
rtattr_add(
&nl_request.n,
sizeof(nl_request),
/*RTA_NEWDST*/ RTA_DST,
&dst->data,
sizeof(in6_addr));
2021-02-16 15:59:18 +00:00
}
}
2020-08-21 16:03:07 +00:00
/* Send message to the netlink */
return send(sock, &nl_request, sizeof(nl_request), 0);
}
int
read_addr(const char* addr, _inet_addr* res, int bitlen = 32)
2020-08-21 16:03:07 +00:00
{
if (strchr(addr, ':'))
{
res->family = AF_INET6;
2021-02-16 15:59:18 +00:00
res->bitlen = bitlen;
2020-08-21 16:03:07 +00:00
}
else
{
res->family = AF_INET;
res->bitlen = bitlen;
2020-08-21 16:03:07 +00:00
}
return inet_pton(res->family, addr, res->data);
}
#endif
2020-09-23 16:33:32 +00:00
#endif
#ifdef _WIN32
std::wstring
get_win_sys_path()
{
wchar_t win_sys_path[MAX_PATH] = {0};
const wchar_t* default_sys_path = L"C:\\Windows\\system32";
if (!GetSystemDirectoryW(win_sys_path, _countof(win_sys_path)))
{
wcsncpy(win_sys_path, default_sys_path, _countof(win_sys_path));
win_sys_path[_countof(win_sys_path) - 1] = L'\0';
}
return win_sys_path;
}
std::string
RouteCommand()
{
std::wstring wcmd = get_win_sys_path() + L"\\route.exe";
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
return converter.to_bytes(wcmd);
}
std::string
NetshCommand()
{
std::wstring wcmd = get_win_sys_path() + L"\\netsh.exe";
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
return converter.to_bytes(wcmd);
}
template <typename Visit>
void
ForEachWIN32Interface(Visit visit)
{
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
MIB_IPFORWARDTABLE* pIpForwardTable;
DWORD dwSize = 0;
DWORD dwRetVal = 0;
pIpForwardTable = (MIB_IPFORWARDTABLE*)MALLOC(sizeof(MIB_IPFORWARDTABLE));
if (pIpForwardTable == nullptr)
return;
if (GetIpForwardTable(pIpForwardTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
FREE(pIpForwardTable);
pIpForwardTable = (MIB_IPFORWARDTABLE*)MALLOC(dwSize);
if (pIpForwardTable == nullptr)
{
return;
}
}
if ((dwRetVal = GetIpForwardTable(pIpForwardTable, &dwSize, 0)) == NO_ERROR)
{
for (int i = 0; i < (int)pIpForwardTable->dwNumEntries; i++)
{
visit(&pIpForwardTable->table[i]);
}
}
FREE(pIpForwardTable);
#undef MALLOC
#undef FREE
}
std::optional<int>
GetInterfaceIndex(huint32_t ip)
{
std::optional<int> ret = std::nullopt;
ForEachWIN32Interface([&ret, n = ToNet(ip)](auto* iface) {
if (ret.has_value())
return;
if (iface->dwForwardNextHop == n.n)
{
ret = iface->dwForwardIfIndex;
}
});
return ret;
}
#endif
2020-08-21 15:07:37 +00:00
void
AddRoute(std::string ip, std::string gateway)
{
LogInfo("Add route: ", ip, " via ", gateway);
2020-08-21 15:07:37 +00:00
#ifdef __linux__
#ifndef ANDROID
2020-08-21 16:03:07 +00:00
NLSocket sock;
int if_idx = 0;
2020-08-21 16:12:37 +00:00
_inet_addr to_addr{};
_inet_addr gw_addr{};
2020-08-21 16:03:07 +00:00
int nl_cmd = RTM_NEWROUTE;
int nl_flags = NLM_F_CREATE | NLM_F_EXCL;
read_addr(gateway.c_str(), &gw_addr);
read_addr(ip.c_str(), &to_addr);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eFirstHop, if_idx);
#endif
2020-08-21 15:07:37 +00:00
#else
std::stringstream ss;
#if _WIN32
ss << RouteCommand() << " ADD " << ip << " MASK 255.255.255.255 " << gateway << " METRIC 2";
2020-08-21 15:07:37 +00:00
#elif __APPLE__
ss << "/sbin/route -n add -host " << ip << " " << gateway;
2020-08-21 15:07:37 +00:00
#else
#error unsupported platform
#endif
Execute(ss.str());
#endif
}
void
DelRoute(std::string ip, std::string gateway)
{
LogInfo("Delete route: ", ip, " via ", gateway);
2020-08-21 15:07:37 +00:00
#ifdef __linux__
#ifndef ANDROID
2020-08-21 16:03:07 +00:00
NLSocket sock;
int if_idx = 0;
2020-08-21 16:12:37 +00:00
_inet_addr to_addr{};
_inet_addr gw_addr{};
2020-08-21 16:03:07 +00:00
int nl_cmd = RTM_DELROUTE;
int nl_flags = 0;
read_addr(gateway.c_str(), &gw_addr);
read_addr(ip.c_str(), &to_addr);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eFirstHop, if_idx);
#endif
2020-08-21 15:07:37 +00:00
#else
std::stringstream ss;
#if _WIN32
ss << RouteCommand() << " DELETE " << ip << " MASK 255.255.255.255 " << gateway << " METRIC 2";
2020-08-21 15:07:37 +00:00
#elif __APPLE__
ss << "/sbin/route -n delete -host " << ip << " " << gateway;
2020-08-21 15:07:37 +00:00
#else
#error unsupported platform
#endif
Execute(ss.str());
#endif
}
void
AddDefaultRouteViaInterface(std::string ifname)
{
LogInfo("Add default route via ", ifname);
2020-08-21 15:07:37 +00:00
#ifdef __linux__
#ifndef ANDROID
2020-08-21 16:03:07 +00:00
NLSocket sock;
int if_idx = if_nametoindex(ifname.c_str());
2020-08-21 16:12:37 +00:00
_inet_addr to_addr{};
_inet_addr gw_addr{};
2021-02-17 12:49:47 +00:00
const auto maybe = GetInterfaceAddr(ifname);
2020-08-21 19:09:13 +00:00
if (not maybe.has_value())
throw std::runtime_error("we dont have our own net interface?");
2020-08-21 16:03:07 +00:00
int nl_cmd = RTM_NEWROUTE;
int nl_flags = NLM_F_CREATE | NLM_F_EXCL;
const auto host = maybe->asIPv4().ToString();
read_addr(host.c_str(), &gw_addr);
read_addr("0.0.0.0", &to_addr, 1);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eLowerDefault, if_idx);
read_addr("128.0.0.0", &to_addr, 1);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
2021-02-17 12:49:47 +00:00
const auto maybeInt = GetInterfaceIPv6Address(ifname);
2021-02-16 15:59:18 +00:00
if (maybeInt.has_value())
{
const auto host = maybeInt->ToString();
LogInfo("add v6 route via ", host);
read_addr(host.c_str(), &gw_addr, 128);
read_addr("::", &to_addr, 2);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
2021-02-16 15:59:18 +00:00
read_addr("4000::", &to_addr, 2);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
read_addr("8000::", &to_addr, 2);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
read_addr("c000::", &to_addr, 2);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
}
#endif
2020-08-21 15:07:37 +00:00
#elif _WIN32
// poke hole for loopback bacause god is dead on windows
Execute(RouteCommand() + " ADD 127.0.0.0 MASK 255.0.0.0 0.0.0.0");
huint32_t ip{};
ip.FromString(ifname);
const auto ipv6 = net::ExpandV4Lan(ip);
Execute(RouteCommand() + " ADD ::/2 " + ipv6.ToString());
Execute(RouteCommand() + " ADD 4000::/2 " + ipv6.ToString());
Execute(RouteCommand() + " ADD 8000::/2 " + ipv6.ToString());
Execute(RouteCommand() + " ADD c000::/2 " + ipv6.ToString());
2020-08-21 15:07:37 +00:00
ifname.back()++;
2020-09-28 22:43:31 +00:00
Execute(RouteCommand() + " ADD 0.0.0.0 MASK 128.0.0.0 " + ifname);
Execute(RouteCommand() + " ADD 128.0.0.0 MASK 128.0.0.0 " + ifname);
2020-08-21 15:07:37 +00:00
#elif __APPLE__
Execute("/sbin/route -n add -cloning -net 0.0.0.0 -netmask 128.0.0.0 -interface " + ifname);
Execute("/sbin/route -n add -cloning -net 128.0.0.0 -netmask 128.0.0.0 -interface " + ifname);
2021-02-16 21:37:08 +00:00
Execute("/sbin/route -n add -inet6 -net ::/2 -interface " + ifname);
Execute("/sbin/route -n add -inet6 -net 4000::/2 -interface " + ifname);
Execute("/sbin/route -n add -inet6 -net 8000::/2 -interface " + ifname);
Execute("/sbin/route -n add -inet6 -net c000::/2 -interface " + ifname);
2020-08-21 15:07:37 +00:00
#else
#error unsupported platform
#endif
}
void
DelDefaultRouteViaInterface(std::string ifname)
{
LogInfo("Remove default route via ", ifname);
2020-08-21 15:07:37 +00:00
#ifdef __linux__
#ifndef ANDROID
2020-08-21 16:03:07 +00:00
NLSocket sock;
int if_idx = if_nametoindex(ifname.c_str());
2020-08-21 16:12:37 +00:00
_inet_addr to_addr{};
_inet_addr gw_addr{};
2021-02-17 12:49:47 +00:00
const auto maybe = GetInterfaceAddr(ifname);
2020-08-21 19:09:13 +00:00
if (not maybe.has_value())
throw std::runtime_error("we dont have our own net interface?");
2020-08-21 16:03:07 +00:00
int nl_cmd = RTM_DELROUTE;
int nl_flags = 0;
const auto host = maybe->asIPv4().ToString();
read_addr(host.c_str(), &gw_addr);
read_addr("0.0.0.0", &to_addr, 1);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eLowerDefault, if_idx);
read_addr("128.0.0.0", &to_addr, 1);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
2021-02-16 15:59:18 +00:00
2021-02-17 12:49:47 +00:00
const auto maybeInt = GetInterfaceIPv6Address(ifname);
2021-02-16 15:59:18 +00:00
if (maybeInt.has_value())
{
const auto host = maybeInt->ToString();
LogInfo("del v6 route via ", host);
read_addr(host.c_str(), &gw_addr, 128);
read_addr("::", &to_addr, 2);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
2021-02-16 15:59:18 +00:00
read_addr("4000::", &to_addr, 2);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
read_addr("8000::", &to_addr, 2);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
read_addr("c000::", &to_addr, 2);
do_route(sock.fd, nl_cmd, nl_flags, &to_addr, &gw_addr, GatewayMode::eUpperDefault, if_idx);
}
#endif
2020-08-21 15:07:37 +00:00
#elif _WIN32
huint32_t ip{};
ip.FromString(ifname);
const auto ipv6 = net::ExpandV4Lan(ip);
Execute(RouteCommand() + " DELETE ::/2 " + ipv6.ToString());
Execute(RouteCommand() + " DELETE 4000::/2 " + ipv6.ToString());
Execute(RouteCommand() + " DELETE 8000::/2 " + ipv6.ToString());
Execute(RouteCommand() + " DELETE c000::/2 " + ipv6.ToString());
2020-08-21 15:07:37 +00:00
ifname.back()++;
Execute(RouteCommand() + " DELETE 0.0.0.0 MASK 128.0.0.0 " + ifname);
Execute(RouteCommand() + " DELETE 128.0.0.0 MASK 128.0.0.0 " + ifname);
Execute(RouteCommand() + " DELETE 127.0.0.0 MASK 255.0.0.0 0.0.0.0");
2020-08-21 15:07:37 +00:00
#elif __APPLE__
Execute("/sbin/route -n delete -cloning -net 0.0.0.0 -netmask 128.0.0.0 -interface " + ifname);
Execute(
"/sbin/route -n delete -cloning -net 128.0.0.0 -netmask 128.0.0.0 -interface " + ifname);
2021-02-16 21:37:08 +00:00
Execute("/sbin/route -n delete -inet6 -net ::/2 -interface " + ifname);
Execute("/sbin/route -n delete -inet6 -net 4000::/2 -interface " + ifname);
Execute("/sbin/route -n delete -inet6 -net 8000::/2 -interface " + ifname);
Execute("/sbin/route -n delete -inet6 -net c000::/2 -interface " + ifname);
2020-08-21 15:07:37 +00:00
#else
#error unsupported platform
#endif
}
std::vector<std::string>
GetGatewaysNotOnInterface(std::string ifname)
{
std::vector<std::string> gateways;
#ifdef __linux__
2021-04-26 10:08:02 +00:00
#ifdef ANDROID
#else
std::ifstream inf("/proc/net/route");
for (std::string line; std::getline(inf, line);)
2020-08-21 15:07:37 +00:00
{
const auto parts = split(line, '\t');
if (parts[1].find_first_not_of('0') == std::string::npos and parts[0] != ifname)
2020-08-21 15:07:37 +00:00
{
const auto& ip = parts[2];
if ((ip.size() == sizeof(uint32_t) * 2) and oxenmq::is_hex(ip))
{
huint32_t x{};
oxenmq::from_hex(ip.begin(), ip.end(), reinterpret_cast<char*>(&x.h));
gateways.emplace_back(x.ToString());
}
2020-08-21 15:07:37 +00:00
}
}
2021-04-26 10:08:02 +00:00
#endif
2020-08-21 15:07:37 +00:00
return gateways;
#elif _WIN32
ForEachWIN32Interface([&](auto w32interface) {
struct in_addr gateway, interface_addr;
gateway.S_un.S_addr = (u_long)w32interface->dwForwardDest;
interface_addr.S_un.S_addr = (u_long)w32interface->dwForwardNextHop;
std::string interface_name{inet_ntoa(interface_addr)};
if ((!gateway.S_un.S_addr) and interface_name != ifname)
2020-08-21 15:07:37 +00:00
{
llarp::LogTrace(
"Win32 find gateway: Adding gateway (", interface_name, ") to list of gateways.");
gateways.push_back(std::move(interface_name));
2020-08-21 15:07:37 +00:00
}
});
2020-08-21 15:07:37 +00:00
return gateways;
#elif __APPLE__
LogDebug("get gateways not on ", ifname);
2020-08-21 15:07:37 +00:00
// mac os is so godawful man
FILE* p = popen("/usr/sbin/netstat -rn -f inet", "r");
2020-08-21 15:07:37 +00:00
if (p == nullptr)
{
2020-08-21 15:07:37 +00:00
return gateways;
}
2020-08-21 15:07:37 +00:00
char* line = nullptr;
size_t len = 0;
ssize_t read = 0;
while ((read = getline(&line, &len, p)) != -1)
{
const std::string line_str(line, len);
const auto parts = llarp::split_any(line_str, " "sv, true);
if (parts[0] == "default" and parts[3] != ifname)
2020-08-21 15:07:37 +00:00
{
gateways.emplace_back(parts[1]);
2020-08-21 15:07:37 +00:00
}
}
pclose(p);
return gateways;
#else
#error unsupported platform
#endif
}
} // namespace llarp::net