lokinet/llarp/handlers/tun.cpp

1003 lines
29 KiB
C++
Raw Normal View History

2018-10-10 12:06:28 +00:00
#include <algorithm>
// harmless on other platforms
#define __USE_MINGW_ANSI_STDIO 1
2018-12-12 01:12:59 +00:00
#include <handlers/tun.hpp>
#include <sys/types.h>
2018-09-25 08:31:29 +00:00
#ifndef _WIN32
#include <sys/socket.h>
#include <netdb.h>
2018-09-25 08:31:29 +00:00
#endif
2018-08-15 15:36:34 +00:00
#include <dns/dns.hpp>
2019-01-11 01:19:36 +00:00
#include <ev/ev.hpp>
#include <router/abstractrouter.hpp>
#include <service/context.hpp>
2019-09-01 12:38:03 +00:00
#include <util/meta/memfn.hpp>
2019-09-01 13:26:16 +00:00
#include <util/thread/logic.hpp>
#include <nodedb.hpp>
#include <util/str.hpp>
#include <absl/strings/ascii.h>
2018-08-15 15:36:34 +00:00
namespace llarp
{
2018-08-16 14:34:15 +00:00
namespace handlers
2018-08-15 15:36:34 +00:00
{
void
TunEndpoint::FlushToUser(std::function< bool(net::IPPacket &) > send)
{
m_ExitMap.ForEachValue([](const auto &exit) { exit->FlushDownstream(); });
// flush network to user
m_NetworkToUserPktQueue.Process(send);
}
2018-12-15 16:56:35 +00:00
static void
tunifTick(llarp_tun_io *tun)
{
2019-07-30 23:42:13 +00:00
auto *self = static_cast< TunEndpoint * >(tun->user);
2018-12-15 16:56:35 +00:00
self->Flush();
}
TunEndpoint::TunEndpoint(const std::string &nickname, AbstractRouter *r,
service::Context *parent, bool lazyVPN)
: service::Endpoint(nickname, r, parent)
, m_UserToNetworkPktQueue(nickname + "_sendq", r->netloop(),
r->netloop())
, m_NetworkToUserPktQueue(nickname + "_recvq", r->netloop(),
r->netloop())
2019-05-23 12:22:48 +00:00
, m_Resolver(std::make_shared< dns::Proxy >(
r->netloop(), r->logic(), r->netloop(), r->logic(), this))
2018-08-15 15:36:34 +00:00
{
if(not lazyVPN)
{
tunif.reset(new llarp_tun_io());
std::fill(tunif->ifaddr, tunif->ifaddr + sizeof(tunif->ifaddr), 0);
std::fill(tunif->ifname, tunif->ifname + sizeof(tunif->ifname), 0);
tunif->netmask = 0;
tunif->get_fd_promise = nullptr;
tunif->user = this;
// eh this shouldn't do anything on windows anyway
tunif->tick = &tunifTick;
tunif->before_write = &tunifBeforeWrite;
tunif->recvpkt = &tunifRecvPkt;
}
2018-08-16 14:34:15 +00:00
}
2019-02-11 17:14:43 +00:00
util::StatusObject
TunEndpoint::ExtractStatus() const
2019-02-08 19:43:25 +00:00
{
auto obj = service::Endpoint::ExtractStatus();
obj["ifaddr"] = m_OurRange.ToString();
2019-02-08 19:43:25 +00:00
std::vector< std::string > resolvers;
for(const auto &addr : m_UpstreamResolvers)
resolvers.emplace_back(addr.ToString());
obj["ustreamResolvers"] = resolvers;
obj["localResolver"] = m_LocalResolverAddr.ToString();
2019-02-11 17:14:43 +00:00
util::StatusObject ips{};
2019-02-08 19:43:25 +00:00
for(const auto &item : m_IPActivity)
{
2019-02-11 17:14:43 +00:00
util::StatusObject ipObj{{"lastActive", item.second}};
2019-02-08 19:43:25 +00:00
std::string remoteStr;
AlignedBuffer< 32 > addr = m_IPToAddr.at(item.first);
if(m_SNodes.at(addr))
remoteStr = RouterID(addr.as_array()).ToString();
else
remoteStr = service::Address(addr.as_array()).ToString();
ipObj["remote"] = remoteStr;
2019-02-08 19:43:25 +00:00
std::string ipaddr = item.first.ToString();
ips[ipaddr] = ipObj;
2019-02-08 19:43:25 +00:00
}
obj["addrs"] = ips;
obj["ourIP"] = m_OurIP.ToString();
obj["nextIP"] = m_NextIP.ToString();
obj["maxIP"] = m_MaxIP.ToString();
2019-02-11 17:14:43 +00:00
return obj;
2019-02-08 19:43:25 +00:00
}
2018-08-16 14:34:15 +00:00
bool
TunEndpoint::SetOption(const std::string &k, const std::string &v)
{
if(k == "isolate-network" && IsTrueValue(v.c_str()))
{
2019-05-22 16:20:50 +00:00
#if defined(__linux__)
LogInfo(Name(), " isolating network...");
if(!SpawnIsolatedNetwork())
{
LogError(Name(), " failed to spawn isolated network");
return false;
2019-05-22 16:20:50 +00:00
}
LogInfo(Name(), " booyeah network isolation succeeded");
return true;
#else
LogError(Name(),
" network isolation is not supported on your platform");
return false;
#endif
}
if(k == "strict-connect")
{
RouterID connect;
if(!connect.FromString(v))
{
LogError(Name(), " invalid snode for strict-connect: ", v);
return false;
}
RouterContact rc;
2019-08-02 09:27:27 +00:00
if(!m_router->nodedb()->Get(connect, rc))
{
LogError(Name(), " we don't have the RC for ", v,
" so we can't use it in strict-connect");
return false;
}
for(const auto &ai : rc.addrs)
{
m_StrictConnectAddrs.emplace_back(ai);
LogInfo(Name(), " added ", m_StrictConnectAddrs.back(),
" to strict connect");
}
return true;
}
// Name won't be set because we need to read the config before we can read
// the keyfile
2018-11-14 19:34:17 +00:00
if(k == "exit-node")
{
IPRange exitRange;
2018-11-14 19:34:17 +00:00
llarp::RouterID exitRouter;
std::string routerStr;
const auto pos = v.find(",");
if(pos != std::string::npos)
{
auto range_str = v.substr(1 + pos);
if(!exitRange.FromString(range_str))
{
LogError("bad exit range: '", range_str, "'");
return false;
}
routerStr = v.substr(0, pos);
}
else
2018-11-14 19:34:17 +00:00
{
routerStr = v;
}
absl::StripAsciiWhitespace(&routerStr);
if(!(exitRouter.FromString(routerStr)
|| HexDecode(routerStr.c_str(), exitRouter.begin(),
exitRouter.size())))
{
llarp::LogError(Name(), " bad exit router key: ", routerStr);
2018-11-14 19:34:17 +00:00
return false;
}
auto exit = std::make_shared< llarp::exit::ExitSession >(
2018-11-14 19:34:17 +00:00
exitRouter,
2019-08-02 09:27:27 +00:00
util::memFn(&TunEndpoint::QueueInboundPacketForExit, this),
m_router, numPaths, numHops, ShouldBundleRC());
m_ExitMap.Insert(exitRange, exit);
llarp::LogInfo(Name(), " using exit at ", exitRouter, " for ",
exitRange);
2018-11-14 19:34:17 +00:00
}
2018-11-11 13:14:19 +00:00
if(k == "local-dns")
{
std::string resolverAddr = v;
uint16_t dnsport = 53;
2018-11-11 13:14:19 +00:00
auto pos = v.find(":");
if(pos != std::string::npos)
{
resolverAddr = v.substr(0, pos);
dnsport = std::atoi(v.substr(pos + 1).c_str());
2018-11-11 13:14:19 +00:00
}
m_LocalResolverAddr = llarp::Addr(resolverAddr, dnsport);
llarp::LogInfo(Name(), " binding DNS server to ", m_LocalResolverAddr);
2018-11-11 13:14:19 +00:00
}
if(k == "upstream-dns")
{
std::string resolverAddr = v;
uint16_t dnsport = 53;
2018-11-11 13:14:19 +00:00
auto pos = v.find(":");
if(pos != std::string::npos)
{
resolverAddr = v.substr(0, pos);
dnsport = std::atoi(v.substr(pos + 1).c_str());
2018-11-11 13:14:19 +00:00
}
2018-12-03 22:22:59 +00:00
m_UpstreamResolvers.emplace_back(resolverAddr, dnsport);
llarp::LogInfo(Name(), " adding upstream DNS server ", resolverAddr,
2018-12-17 20:47:08 +00:00
":", dnsport);
2018-11-11 13:14:19 +00:00
}
2018-08-22 15:52:10 +00:00
if(k == "mapaddr")
{
auto pos = v.find(":");
if(pos == std::string::npos)
{
llarp::LogError("Cannot map address ", v,
2018-11-03 13:19:18 +00:00
" invalid format, missing colon (:), expects "
2018-08-22 15:52:10 +00:00
"address.loki:ip.address.goes.here");
return false;
}
service::Address addr;
auto addr_str = v.substr(0, pos);
if(!addr.FromString(addr_str))
{
2018-09-10 11:08:09 +00:00
llarp::LogError(Name() + " cannot map invalid address ", addr_str);
2018-08-22 15:52:10 +00:00
return false;
}
auto ip_str = v.substr(pos + 1);
2019-06-11 16:44:05 +00:00
huint32_t ip;
huint128_t ipv6;
if(ip.FromString(ip_str))
2018-08-22 15:52:10 +00:00
{
2019-06-11 16:44:05 +00:00
ipv6 = net::IPPacket::ExpandV4(ip);
}
else if(ipv6.FromString(ip_str))
{
}
else
{
llarp::LogError(Name(), "failed to map ", ip_str,
" failed to parse IP");
2018-08-22 15:52:10 +00:00
return false;
}
2019-06-11 16:44:05 +00:00
return MapAddress(addr, ipv6, false);
2018-08-22 15:52:10 +00:00
}
if(k == "ifname" && tunif)
2018-08-16 14:34:15 +00:00
{
if(v.length() >= sizeof(tunif->ifname))
2018-12-28 15:10:05 +00:00
{
llarp::LogError(Name() + " ifname '", v, "' is too long");
return false;
}
strncpy(tunif->ifname, v.c_str(), sizeof(tunif->ifname) - 1);
llarp::LogInfo(Name() + " setting ifname to ", tunif->ifname);
2018-08-16 14:34:15 +00:00
return true;
}
if(k == "ifaddr" && tunif)
2018-08-16 14:34:15 +00:00
{
std::string addr;
2019-06-11 16:44:05 +00:00
m_UseV6 = addr.find(":") != std::string::npos;
2018-08-16 14:34:15 +00:00
auto pos = v.find("/");
if(pos != std::string::npos)
{
2018-11-06 14:06:09 +00:00
int num;
std::string part = v.substr(pos + 1);
2018-11-08 12:31:50 +00:00
#if defined(ANDROID) || defined(RPI)
2018-11-06 14:06:09 +00:00
num = atoi(part.c_str());
#else
num = std::stoi(part);
#endif
2018-08-16 14:34:15 +00:00
if(num > 0)
{
tunif->netmask = num;
addr = v.substr(0, pos);
2018-08-16 14:34:15 +00:00
}
else
{
llarp::LogError("bad ifaddr value: ", v);
return false;
}
}
else
{
2019-06-11 16:44:05 +00:00
if(m_UseV6)
tunif->netmask = 128;
2019-06-11 16:44:05 +00:00
else
tunif->netmask = 32;
2019-06-11 16:44:05 +00:00
addr = v;
2018-08-16 14:34:15 +00:00
}
2018-09-10 11:08:09 +00:00
llarp::LogInfo(Name() + " set ifaddr to ", addr, " with netmask ",
tunif->netmask);
strncpy(tunif->ifaddr, addr.c_str(), sizeof(tunif->ifaddr) - 1);
2018-08-16 14:34:15 +00:00
return true;
}
return Endpoint::SetOption(k, v);
}
2018-11-14 12:23:08 +00:00
bool
2019-06-11 16:44:05 +00:00
TunEndpoint::HasLocalIP(const huint128_t &ip) const
2018-11-14 12:23:08 +00:00
{
return m_IPToAddr.find(ip) != m_IPToAddr.end();
}
bool
2019-06-11 16:44:05 +00:00
TunEndpoint::QueueOutboundTraffic(llarp::net::IPPacket &&pkt)
2018-11-14 12:23:08 +00:00
{
return m_NetworkToUserPktQueue.EmplaceIf(
2019-06-11 16:44:05 +00:00
[](llarp::net::IPPacket &) -> bool { return true; }, std::move(pkt));
2018-11-14 12:23:08 +00:00
}
2018-12-15 16:56:35 +00:00
void
TunEndpoint::Flush()
{
2019-04-30 16:07:17 +00:00
auto self = shared_from_this();
2018-12-15 16:56:35 +00:00
FlushSend();
RouterLogic()->queue_func([=] {
self->m_ExitMap.ForEachValue(
[](const auto &exit) { exit->FlushUpstream(); });
2019-04-30 16:07:17 +00:00
self->Pump(self->Now());
});
2018-12-15 16:56:35 +00:00
}
static bool
is_random_snode(const dns::Message &msg)
{
2019-04-26 12:11:34 +00:00
return msg.questions[0].IsName("random.snode");
}
static bool
is_localhost_loki(const dns::Message &msg)
{
2019-04-26 12:11:34 +00:00
return msg.questions[0].IsName("localhost.loki");
}
2018-12-03 22:22:59 +00:00
bool
TunEndpoint::HandleHookedDNSMessage(
dns::Message &&msg, std::function< void(dns::Message) > reply)
2018-12-03 22:22:59 +00:00
{
// llarp::LogInfo("Tun.HandleHookedDNSMessage ", msg.questions[0].qname, "
// of type", msg.questions[0].qtype);
2018-12-03 22:22:59 +00:00
if(msg.questions.size() != 1)
{
llarp::LogWarn("bad number of dns questions: ", msg.questions.size());
return false;
}
2019-04-26 12:11:34 +00:00
const std::string qname = msg.questions[0].Name();
if(msg.questions[0].qtype == dns::qTypeMX)
2018-12-07 21:52:19 +00:00
{
// mx record
2019-02-05 14:23:51 +00:00
service::Address addr;
2019-02-05 14:22:02 +00:00
if(addr.FromString(qname, ".loki") || addr.FromString(qname, ".snode")
|| is_random_snode(msg) || is_localhost_loki(msg))
2018-12-07 21:52:19 +00:00
msg.AddMXReply(qname, 1);
else
msg.AddNXReply();
2018-12-07 22:08:23 +00:00
reply(msg);
2018-12-07 21:52:19 +00:00
}
2019-01-10 15:49:08 +00:00
else if(msg.questions[0].qtype == dns::qTypeCNAME)
{
2019-02-05 14:22:02 +00:00
if(is_random_snode(msg))
2019-01-10 15:49:08 +00:00
{
RouterID random;
if(Router()->GetRandomGoodRouter(random))
msg.AddCNAMEReply(random.ToString(), 1);
else
msg.AddNXReply();
}
2019-02-05 14:22:02 +00:00
else if(is_localhost_loki(msg))
{
size_t counter = 0;
context->ForEachService(
[&](const std::string &,
const std::shared_ptr< service::Endpoint > &service) -> bool {
2019-07-05 14:41:26 +00:00
const service::Address addr = service->GetIdentity().pub.Addr();
msg.AddCNAMEReply(addr.ToString(), 1);
++counter;
return true;
});
if(counter == 0)
msg.AddNXReply();
}
2019-01-10 15:49:08 +00:00
else
msg.AddNXReply();
2019-02-05 14:03:38 +00:00
reply(msg);
2019-01-10 15:49:08 +00:00
}
else if(msg.questions[0].qtype == dns::qTypeA
|| msg.questions[0].qtype == dns::qTypeAAAA)
2018-12-03 22:22:59 +00:00
{
2019-06-11 21:28:55 +00:00
const bool isV6 =
msg.questions[0].qtype == dns::qTypeAAAA && SupportsV6();
2019-06-11 16:44:05 +00:00
const bool isV4 = msg.questions[0].qtype == dns::qTypeA;
2018-12-03 22:22:59 +00:00
llarp::service::Address addr;
2019-02-05 03:19:06 +00:00
// on MacOS this is a typeA query
2019-02-05 14:22:02 +00:00
if(is_random_snode(msg))
2019-02-05 03:19:06 +00:00
{
2019-02-05 21:04:30 +00:00
RouterID random;
if(Router()->GetRandomGoodRouter(random))
msg.AddCNAMEReply(random.ToString(), 1);
else
msg.AddNXReply();
2019-02-05 03:19:06 +00:00
}
2019-02-05 14:22:02 +00:00
else if(is_localhost_loki(msg))
2018-12-13 00:03:19 +00:00
{
size_t counter = 0;
context->ForEachService(
[&](const std::string &,
const std::shared_ptr< service::Endpoint > &service) -> bool {
2019-07-01 13:44:25 +00:00
if(!service->HasIfAddr())
return true;
2019-06-11 16:44:05 +00:00
huint128_t ip = service->GetIfAddr();
if(ip.h)
{
msg.AddINReply(ip, isV6);
++counter;
}
return true;
});
if(counter == 0)
2018-12-13 00:03:19 +00:00
msg.AddNXReply();
}
else if(addr.FromString(qname, ".loki"))
2018-12-03 22:22:59 +00:00
{
2019-06-11 16:44:05 +00:00
if(isV4 && SupportsV6())
{
2019-06-11 18:23:53 +00:00
msg.hdr_fields |= dns::flags_QR | dns::flags_AA | dns::flags_RA;
2019-06-11 16:44:05 +00:00
}
else if(HasAddress(addr))
{
huint128_t ip = ObtainIPForAddr(addr, false);
2019-06-11 21:28:55 +00:00
msg.AddINReply(ip, isV6);
}
else
{
2019-07-30 23:42:13 +00:00
auto *replyMsg = new dns::Message(std::move(msg));
using service::Address;
using service::OutboundContext;
return EnsurePathToService(
addr,
2019-04-26 12:11:34 +00:00
[=](const Address &, OutboundContext *ctx) {
2019-06-11 21:28:55 +00:00
SendDNSReply(addr, ctx, replyMsg, reply, false,
isV6 || !isV4);
},
2000);
}
2018-12-03 22:22:59 +00:00
}
else if(addr.FromString(qname, ".snode"))
{
2019-06-12 13:48:14 +00:00
if(isV4 && SupportsV6())
{
msg.hdr_fields |= dns::flags_QR | dns::flags_AA | dns::flags_RA;
}
else
{
2019-07-30 23:42:13 +00:00
auto *replyMsg = new dns::Message(std::move(msg));
2019-06-12 13:48:14 +00:00
EnsurePathToSNode(addr.as_array(),
[=](const RouterID &, exit::BaseSession_ptr s) {
SendDNSReply(addr, s, replyMsg, reply, true,
isV6);
});
return true;
}
2018-12-03 22:22:59 +00:00
}
else
2018-12-04 16:16:43 +00:00
msg.AddNXReply();
reply(msg);
2018-12-03 22:22:59 +00:00
}
2019-01-10 15:49:08 +00:00
else if(msg.questions[0].qtype == dns::qTypePTR)
2018-12-03 22:22:59 +00:00
{
2018-12-04 16:16:43 +00:00
// reverse dns
2019-06-12 13:48:14 +00:00
huint128_t ip = {0};
2018-12-03 22:22:59 +00:00
if(!dns::DecodePTR(msg.questions[0].qname, ip))
{
2018-12-04 16:16:43 +00:00
msg.AddNXReply();
reply(msg);
2018-12-03 22:22:59 +00:00
return true;
}
llarp::service::Address addr(
2019-06-12 13:48:14 +00:00
ObtainAddrForIP< llarp::service::Address >(ip, true));
2018-12-03 22:22:59 +00:00
if(!addr.IsZero())
{
2018-12-04 16:16:43 +00:00
msg.AddAReply(addr.ToString(".snode"));
reply(msg);
2018-12-03 22:22:59 +00:00
return true;
}
2019-06-12 13:48:14 +00:00
addr = ObtainAddrForIP< llarp::service::Address >(ip, false);
2018-12-03 22:22:59 +00:00
if(!addr.IsZero())
{
2018-12-04 16:16:43 +00:00
msg.AddAReply(addr.ToString(".loki"));
reply(msg);
2018-12-03 22:22:59 +00:00
return true;
}
2018-12-04 16:16:43 +00:00
msg.AddNXReply();
reply(msg);
2018-12-03 22:22:59 +00:00
return true;
}
else
2018-12-04 16:16:43 +00:00
{
msg.AddNXReply();
reply(msg);
}
2018-12-03 22:22:59 +00:00
return true;
}
2019-05-07 17:46:38 +00:00
void
TunEndpoint::ResetInternalState()
{
service::Endpoint::ResetInternalState();
m_ExitMap.ForEachValue(
[](const auto &exit) { exit->ResetInternalState(); });
2019-05-07 17:46:38 +00:00
}
2019-06-11 16:44:05 +00:00
bool
TunEndpoint::SupportsV6() const
{
return m_UseV6;
}
2019-03-20 03:18:38 +00:00
// FIXME: pass in which question it should be addressing
2018-12-03 22:22:59 +00:00
bool
TunEndpoint::ShouldHookDNSMessage(const dns::Message &msg) const
{
llarp::service::Address addr;
if(msg.questions.size() == 1)
{
/// hook every .loki
if(msg.questions[0].HasTLD(".loki"))
2018-12-13 00:03:19 +00:00
return true;
/// hook every .snode
if(msg.questions[0].HasTLD(".snode"))
2018-12-03 22:22:59 +00:00
return true;
2019-01-10 15:49:08 +00:00
// hook any ranges we own
if(msg.questions[0].qtype == llarp::dns::qTypePTR)
2018-12-03 22:22:59 +00:00
{
2019-06-12 13:48:14 +00:00
huint128_t ip = {0};
2018-12-03 22:22:59 +00:00
if(!dns::DecodePTR(msg.questions[0].qname, ip))
return false;
2019-06-12 13:48:14 +00:00
return m_OurRange.Contains(ip);
2018-12-03 22:22:59 +00:00
}
}
return false;
}
2018-08-22 15:52:10 +00:00
bool
2019-06-11 16:44:05 +00:00
TunEndpoint::MapAddress(const service::Address &addr, huint128_t ip,
bool SNode)
2018-08-22 15:52:10 +00:00
{
auto itr = m_IPToAddr.find(ip);
if(itr != m_IPToAddr.end())
{
2018-11-14 12:23:08 +00:00
llarp::LogWarn(ip, " already mapped to ",
service::Address(itr->second.as_array()).ToString());
2018-08-22 15:52:10 +00:00
return false;
}
2018-11-03 13:19:18 +00:00
llarp::LogInfo(Name() + " map ", addr.ToString(), " to ", ip);
2018-10-19 15:04:14 +00:00
m_IPToAddr[ip] = addr;
m_AddrToIP[addr] = ip;
m_SNodes[addr] = SNode;
2018-09-10 11:08:09 +00:00
MarkIPActiveForever(ip);
2018-08-22 15:52:10 +00:00
return true;
}
2018-08-16 14:34:15 +00:00
bool
TunEndpoint::Start()
{
if(!Endpoint::Start())
{
llarp::LogWarn("Couldn't start endpoint");
2018-08-16 14:34:15 +00:00
return false;
}
const auto blacklist = SnodeBlacklist();
m_ExitMap.ForEachValue([blacklist](const auto &exit) {
for(const auto &snode : blacklist)
exit->BlacklistSnode(snode);
});
2018-08-21 18:39:18 +00:00
return SetupNetworking();
2018-08-16 14:34:15 +00:00
}
bool
TunEndpoint::IsSNode() const
{
// TODO : implement me
return false;
}
2018-08-16 14:34:15 +00:00
bool
TunEndpoint::SetupTun()
{
lazy_vpn vpn;
huint32_t ip;
2019-04-08 12:01:52 +00:00
auto loop = EndpointNetLoop();
if(tunif == nullptr)
{
llarp::LogInfo(Name(), " waiting for vpn to start");
vpn = m_LazyVPNPromise.get_future().get();
vpnif = vpn.io;
if(vpnif == nullptr)
{
llarp::LogError(Name(), " failed to recieve vpn interface");
return false;
}
llarp::LogInfo(Name(), " got vpn interface");
auto self = shared_from_this();
// function to queue a packet to send to vpn interface
auto sendpkt = [self](net::IPPacket &pkt) -> bool {
// drop if no endpoint
auto impl = self->GetVPNImpl();
// drop if no vpn interface
if(impl == nullptr)
return true;
// drop if queue to vpn not enabled
if(not impl->reader.queue.enabled())
return true;
// drop if queue to vpn full
if(impl->reader.queue.full())
return true;
// queue to reader
impl->reader.queue.pushBack(pkt);
return false;
};
// event loop ticker
auto ticker = [self, sendpkt]() {
TunEndpoint *ep = self.get();
const bool running = not ep->IsStopped();
auto impl = ep->GetVPNImpl();
if(impl)
{
/// get packets from vpn
while(not impl->writer.queue.empty())
{
// queue it to be sent over lokinet
auto pkt = impl->writer.queue.popFront();
if(running)
ep->m_UserToNetworkPktQueue.Emplace(pkt);
}
}
// process packets queued from vpn
if(running)
{
ep->Flush();
ep->FlushToUser(sendpkt);
}
// if impl has a tick function call it
if(impl && impl->parent && impl->parent->tick)
impl->parent->tick(impl->parent);
};
if(not loop->add_ticker(ticker))
{
llarp::LogError(Name(), " failed to add vpn to event loop");
if(vpnif->injected)
vpnif->injected(vpnif, false);
return false;
}
2019-06-11 16:44:05 +00:00
}
else
{
if(!llarp_ev_add_tun(loop.get(), tunif.get()))
2019-06-11 16:44:05 +00:00
{
llarp::LogError(Name(),
" failed to set up tun interface: ", tunif->ifaddr,
" on ", tunif->ifname);
2019-06-11 16:44:05 +00:00
return false;
}
}
const char *ifname;
const char *ifaddr;
unsigned char netmask;
if(tunif)
{
ifname = tunif->ifname;
ifaddr = tunif->ifaddr;
netmask = tunif->netmask;
}
else
{
ifname = vpn.info.ifname;
ifaddr = vpn.info.ifaddr;
netmask = vpn.info.netmask;
}
if(ip.FromString(ifaddr))
2019-06-11 16:44:05 +00:00
{
m_OurIP = net::IPPacket::ExpandV4(ip);
m_OurRange.netmask_bits = netmask_ipv6_bits(netmask + 96);
2019-06-11 16:44:05 +00:00
}
else if(m_OurIP.FromString(ifaddr))
2019-06-11 16:44:05 +00:00
{
m_OurRange.netmask_bits = netmask_ipv6_bits(netmask);
m_UseV6 = true;
}
else
{
LogError(Name(), " invalid interface address given, ifaddr=", ifaddr);
if(vpnif && vpnif->injected)
vpnif->injected(vpnif, false);
return false;
}
2019-06-11 16:44:05 +00:00
m_NextIP = m_OurIP;
m_OurRange.addr = m_OurIP;
m_MaxIP = m_OurRange.HighestAddr();
llarp::LogInfo(Name(), " set ", ifname, " to have address ", m_OurIP);
2018-12-03 22:22:59 +00:00
llarp::LogInfo(Name(), " allocated up to ", m_MaxIP, " on range ",
m_OurRange);
2019-06-11 16:44:05 +00:00
MapAddress(m_Identity.pub.Addr(), m_OurIP, IsSNode());
if(m_OnUp)
{
2019-04-22 14:00:59 +00:00
m_OnUp->NotifyAsync(NotifyParams());
}
if(vpnif && vpnif->injected)
{
vpnif->injected(vpnif, true);
}
2018-08-20 19:12:12 +00:00
return true;
2018-08-16 14:34:15 +00:00
}
2019-04-22 14:00:59 +00:00
std::unordered_map< std::string, std::string >
TunEndpoint::NotifyParams() const
{
auto env = Endpoint::NotifyParams();
env.emplace("IP_ADDR", m_OurIP.ToString());
env.emplace("IF_ADDR", m_OurRange.ToString());
if(tunif)
env.emplace("IF_NAME", tunif->ifname);
std::string strictConnect;
for(const auto &addr : m_StrictConnectAddrs)
strictConnect += addr.ToString() + " ";
env.emplace("STRICT_CONNECT_ADDRS", strictConnect);
2019-04-22 14:00:59 +00:00
return env;
}
2018-08-16 14:34:15 +00:00
bool
TunEndpoint::SetupNetworking()
{
llarp::LogInfo("Set Up networking for ", Name());
2018-12-03 22:22:59 +00:00
if(!SetupTun())
2018-10-04 13:42:22 +00:00
{
2018-12-03 22:22:59 +00:00
llarp::LogError(Name(), " failed to set up network interface");
return false;
2018-10-04 13:42:22 +00:00
}
if(!m_Resolver->Start(m_LocalResolverAddr, m_UpstreamResolvers))
{
// downgrade DNS server failure to a warning
llarp::LogWarn(Name(), " failed to start dns server");
// return false;
}
2018-12-03 22:22:59 +00:00
return true;
2018-08-16 14:34:15 +00:00
}
void
TunEndpoint::Tick(llarp_time_t now)
{
2019-09-05 21:28:50 +00:00
EndpointLogic()->queue_func([&]() {
m_ExitMap.ForEachValue([&](const auto &exit) {
this->EnsureRouterIsKnown(exit->Endpoint());
exit->Tick(now);
});
Endpoint::Tick(now);
});
2018-08-16 14:34:15 +00:00
}
bool
TunEndpoint::Stop()
{
m_ExitMap.ForEachValue([](const auto &exit) { exit->Stop(); });
return llarp::service::Endpoint::Stop();
}
2018-08-22 15:52:10 +00:00
void
TunEndpoint::FlushSend()
{
2019-06-11 16:44:05 +00:00
m_UserToNetworkPktQueue.Process([&](net::IPPacket &pkt) {
2019-02-02 23:12:42 +00:00
std::function< bool(const llarp_buffer_t &) > sendFunc;
2019-06-11 16:44:05 +00:00
huint128_t dst;
if(pkt.IsV4())
dst = net::IPPacket::ExpandV4(pkt.dstv4());
else
dst = pkt.dstv6();
auto itr = m_IPToAddr.find(dst);
2018-08-22 15:52:10 +00:00
if(itr == m_IPToAddr.end())
{
const auto exits = m_ExitMap.FindAll(dst);
if(exits.empty())
2018-11-14 19:34:17 +00:00
{
llarp::LogWarn(Name(), " has no exit mapped for ", dst);
return;
2018-11-14 19:34:17 +00:00
}
for(const auto &exit : exits)
2019-06-11 19:48:21 +00:00
{
if(pkt.IsV4() && !llarp::IsIPv4Bogon(pkt.dstv4()))
{
pkt.UpdateIPv4Address({0}, xhtonl(pkt.dstv4()));
exit->QueueUpstreamTraffic(std::move(pkt),
2019-06-11 19:48:21 +00:00
llarp::routing::ExitPadSize);
}
else if(pkt.IsV6())
{
pkt.UpdateIPv6Address({0}, pkt.dstv6());
exit->QueueUpstreamTraffic(std::move(pkt),
llarp::routing::ExitPadSize);
}
2019-06-11 16:44:05 +00:00
}
return;
2018-08-22 15:52:10 +00:00
}
2018-11-29 13:12:35 +00:00
if(m_SNodes.at(itr->second))
{
sendFunc = std::bind(&TunEndpoint::SendToSNodeOrQueue, this,
itr->second.as_array(), std::placeholders::_1);
2018-11-29 13:12:35 +00:00
}
else
2018-11-29 13:12:35 +00:00
{
2019-06-11 16:44:05 +00:00
sendFunc = std::bind(&TunEndpoint::SendToServiceOrQueue, this,
service::Address(itr->second.as_array()),
std::placeholders::_1, pkt.ServiceProtocol());
2018-11-29 13:12:35 +00:00
}
// prepare packet for insertion into network
// this includes clearing IP addresses, recalculating checksums, etc
2019-06-11 16:44:05 +00:00
if(pkt.IsV4())
2019-06-11 21:27:06 +00:00
pkt.UpdateIPv4Address({0}, {0});
2019-06-11 16:44:05 +00:00
else
2019-06-11 21:27:06 +00:00
pkt.UpdateIPv6Address({0}, {0});
2018-11-29 13:12:35 +00:00
if(sendFunc && sendFunc(pkt.Buffer()))
{
MarkIPActive(dst);
2019-06-11 16:44:05 +00:00
return;
}
2018-11-29 13:12:35 +00:00
llarp::LogWarn(Name(), " did not flush packets");
2018-08-22 15:52:10 +00:00
});
Router()->PumpLL();
2018-08-22 15:52:10 +00:00
}
bool
2019-06-11 16:44:05 +00:00
TunEndpoint::HandleWriteIPPacket(
const llarp_buffer_t &b, std::function< huint128_t(void) > getFromIP)
2018-08-18 14:01:21 +00:00
{
2018-11-03 13:19:18 +00:00
// llarp::LogInfo("got packet from ", msg->sender.Addr());
2018-11-29 13:12:35 +00:00
auto themIP = getFromIP();
2018-11-03 13:19:18 +00:00
// llarp::LogInfo("themIP ", themIP);
auto usIP = m_OurIP;
2019-02-03 00:48:10 +00:00
ManagedBuffer buf(b);
2018-11-29 13:12:35 +00:00
return m_NetworkToUserPktQueue.EmplaceIf(
2019-06-11 16:44:05 +00:00
[buf, themIP, usIP](net::IPPacket &pkt) -> bool {
// load
if(!pkt.Load(buf))
return false;
// filter out:
// - packets smaller than minimal IPv4 header
// - non-IPv4 packets
// - packets with weird src/dst addresses
// (0.0.0.0/8 but not 0.0.0.0)
// - packets with 0 src but non-0 dst and oposite
2019-06-11 16:44:05 +00:00
if(pkt.IsV4())
{
2019-06-11 16:44:05 +00:00
auto hdr = pkt.Header();
if(pkt.sz < sizeof(*hdr)
|| (hdr->saddr != 0 && *(byte_t *)&(hdr->saddr) == 0)
|| (hdr->daddr != 0 && *(byte_t *)&(hdr->daddr) == 0)
|| ((hdr->saddr == 0) != (hdr->daddr == 0)))
{
return false;
}
2019-06-11 21:28:55 +00:00
pkt.UpdateIPv4Address(xhtonl(net::IPPacket::TruncateV6(themIP)),
xhtonl(net::IPPacket::TruncateV6(usIP)));
2019-06-11 16:44:05 +00:00
}
else if(pkt.IsV6())
{
2019-06-11 21:27:06 +00:00
pkt.UpdateIPv6Address(themIP, usIP);
}
return true;
});
2018-08-20 19:12:12 +00:00
}
2018-10-19 15:04:14 +00:00
2019-06-11 16:44:05 +00:00
huint128_t
2018-11-14 12:23:08 +00:00
TunEndpoint::GetIfAddr() const
{
2018-11-14 12:23:08 +00:00
return m_OurIP;
}
2018-08-20 19:12:12 +00:00
2019-06-11 16:44:05 +00:00
huint128_t
TunEndpoint::ObtainIPForAddr(const AlignedBuffer< 32 > &addr, bool snode)
2018-08-20 19:12:12 +00:00
{
2019-06-11 16:44:05 +00:00
llarp_time_t now = Now();
huint128_t nextIP = {0};
AlignedBuffer< 32 > ident(addr);
{
// previously allocated address
2018-11-14 20:56:54 +00:00
auto itr = m_AddrToIP.find(ident);
if(itr != m_AddrToIP.end())
2018-08-22 15:52:10 +00:00
{
// mark ip active
2018-09-16 12:25:17 +00:00
MarkIPActive(itr->second);
return itr->second;
2018-08-22 15:52:10 +00:00
}
}
// allocate new address
if(m_NextIP < m_MaxIP)
{
do
{
nextIP = ++m_NextIP;
} while(m_IPToAddr.find(nextIP) != m_IPToAddr.end()
&& m_NextIP < m_MaxIP);
if(nextIP < m_MaxIP)
{
2018-11-14 21:40:44 +00:00
m_AddrToIP[ident] = nextIP;
m_IPToAddr[nextIP] = ident;
m_SNodes[ident] = snode;
2018-11-14 20:56:54 +00:00
llarp::LogInfo(Name(), " mapped ", ident, " to ", nextIP);
MarkIPActive(nextIP);
return nextIP;
}
}
// we are full
// expire least active ip
// TODO: prevent DoS
2019-06-11 16:44:05 +00:00
std::pair< huint128_t, llarp_time_t > oldest = {huint128_t{0}, 0};
// find oldest entry
auto itr = m_IPActivity.begin();
while(itr != m_IPActivity.end())
{
if(itr->second <= now)
{
if((now - itr->second) > oldest.second)
{
oldest.first = itr->first;
oldest.second = itr->second;
}
}
++itr;
}
// remap address
2018-11-14 21:47:58 +00:00
m_IPToAddr[oldest.first] = ident;
m_AddrToIP[ident] = oldest.first;
m_SNodes[ident] = snode;
nextIP = oldest.first;
// mark ip active
m_IPActivity[nextIP] = std::max(m_IPActivity[nextIP], now);
2018-08-20 19:12:12 +00:00
return nextIP;
}
bool
2019-06-11 16:44:05 +00:00
TunEndpoint::HasRemoteForIP(huint128_t ip) const
2018-08-20 19:12:12 +00:00
{
return m_IPToAddr.find(ip) != m_IPToAddr.end();
2018-08-18 14:01:21 +00:00
}
void
2019-06-11 16:44:05 +00:00
TunEndpoint::MarkIPActive(huint128_t ip)
{
2019-06-11 21:28:55 +00:00
llarp::LogDebug(Name(), " address ", ip, " is active");
2018-10-29 16:48:36 +00:00
m_IPActivity[ip] = std::max(Now(), m_IPActivity[ip]);
}
2018-09-10 11:08:09 +00:00
void
2019-06-11 16:44:05 +00:00
TunEndpoint::MarkIPActiveForever(huint128_t ip)
2018-09-10 11:08:09 +00:00
{
m_IPActivity[ip] = std::numeric_limits< uint64_t >::max();
}
void
TunEndpoint::TickTun(__attribute__((unused)) llarp_time_t now)
{
// called in the isolated thread
}
void
TunEndpoint::tunifBeforeWrite(llarp_tun_io *tun)
{
// called in the isolated network thread
auto *self = static_cast< TunEndpoint * >(tun->user);
auto sendpkt = [self, tun](net::IPPacket &pkt) -> bool {
2018-11-23 14:37:26 +00:00
if(!llarp_ev_tun_async_write(tun, pkt.Buffer()))
{
llarp::LogWarn(self->Name(), " packet dropped");
return true;
}
return false;
};
self->EndpointLogic()->queue_func(std::bind(
&TunEndpoint::FlushToUser, self->shared_from_this(), sendpkt));
2018-08-22 15:52:10 +00:00
}
void
TunEndpoint::tunifRecvPkt(llarp_tun_io *tun, const llarp_buffer_t &b)
{
// called for every packet read from user in isolated network thread
2019-07-30 23:42:13 +00:00
auto *self = static_cast< TunEndpoint * >(tun->user);
2019-09-05 21:28:50 +00:00
const ManagedBuffer pkt(b);
self->m_UserToNetworkPktQueue.EmplaceIf(
2019-09-05 21:28:50 +00:00
[&pkt](net::IPPacket &p) -> bool { return p.Load(pkt); });
}
2019-07-30 23:42:13 +00:00
TunEndpoint::~TunEndpoint() = default;
2018-08-15 15:36:34 +00:00
2018-08-16 14:34:15 +00:00
} // namespace handlers
} // namespace llarp