2018-12-12 01:12:59 +00:00
|
|
|
#include <handlers/exit.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
|
|
|
#include <dns/dns.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.hpp>
|
2019-06-17 23:19:39 +00:00
|
|
|
#include <path/path_context.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/str.hpp>
|
2019-09-03 15:56:56 +00:00
|
|
|
#include <util/bits.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2018-11-15 21:47:05 +00:00
|
|
|
#include <cassert>
|
2018-11-12 16:43:40 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace handlers
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::ExitEndpoint(const std::string& name, AbstractRouter* r)
|
2018-11-15 13:13:19 +00:00
|
|
|
: m_Router(r)
|
2021-03-02 18:18:22 +00:00
|
|
|
, m_Resolver(std::make_shared<dns::Proxy>(r->netloop(), r->logic(), this))
|
2020-01-31 19:47:16 +00:00
|
|
|
, m_Name(name)
|
2020-01-30 07:36:03 +00:00
|
|
|
, m_LocalResolverAddr("127.0.0.1", 53)
|
|
|
|
, m_InetToNetwork(name + "_exit_rx", r->netloop(), r->netloop())
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
m_ShouldInitTun = true;
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
ExitEndpoint::~ExitEndpoint() = default;
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
ExitEndpoint::ExtractStatus() const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
util::StatusObject obj{{"permitExit", m_PermitExit}, {"ip", m_IfAddr.ToString()}};
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject exitsObj{};
|
2020-04-07 18:38:56 +00:00
|
|
|
for (const auto& item : m_ActiveExits)
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2019-08-19 21:06:30 +00:00
|
|
|
exitsObj[item.first.ToString()] = item.second->ExtractStatus();
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
2019-08-19 09:33:26 +00:00
|
|
|
obj["exits"] = exitsObj;
|
2019-02-11 17:14:43 +00:00
|
|
|
return obj;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:42:11 +00:00
|
|
|
bool
|
|
|
|
ExitEndpoint::SupportsV6() const
|
|
|
|
{
|
|
|
|
return m_UseV6;
|
|
|
|
}
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::ShouldHookDNSMessage(const dns::Message& msg) const
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions.size() == 0)
|
2018-12-13 16:14:44 +00:00
|
|
|
return false;
|
2019-01-10 15:49:08 +00:00
|
|
|
// always hook ptr for ranges we own
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].qtype == dns::qTypePTR)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-06-12 13:48:14 +00:00
|
|
|
huint128_t ip;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!dns::DecodePTR(msg.questions[0].qname, ip))
|
2018-12-13 16:14:44 +00:00
|
|
|
return false;
|
2019-06-12 13:48:14 +00:00
|
|
|
return m_OurRange.Contains(ip);
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].qtype == dns::qTypeA || msg.questions[0].qtype == dns::qTypeCNAME
|
|
|
|
|| msg.questions[0].qtype == dns::qTypeAAAA)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].IsName("localhost.loki"))
|
2019-05-01 13:40:10 +00:00
|
|
|
return true;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].HasTLD(".snode"))
|
2019-03-20 16:40:28 +00:00
|
|
|
return true;
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
2019-05-01 13:53:43 +00:00
|
|
|
return false;
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::HandleHookedDNSMessage(dns::Message msg, std::function<void(dns::Message)> reply)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].qtype == dns::qTypePTR)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-06-12 13:48:14 +00:00
|
|
|
huint128_t ip;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!dns::DecodePTR(msg.questions[0].qname, ip))
|
2018-12-13 16:14:44 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ip == m_IfAddr)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-01-07 22:15:31 +00:00
|
|
|
RouterID us = GetRouter()->pubkey();
|
2018-12-13 16:14:44 +00:00
|
|
|
msg.AddAReply(us.ToString(), 300);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-06-12 13:48:14 +00:00
|
|
|
auto itr = m_IPToKey.find(ip);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_IPToKey.end() && m_SNodeKeys.find(itr->second) != m_SNodeKeys.end())
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
|
|
|
RouterID them = itr->second;
|
|
|
|
msg.AddAReply(them.ToString());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (msg.questions[0].qtype == dns::qTypeCNAME)
|
2019-01-10 15:49:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].IsName("random.snode"))
|
2019-01-10 15:49:08 +00:00
|
|
|
{
|
|
|
|
RouterID random;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (GetRouter()->GetRandomGoodRouter(random))
|
2019-01-10 15:49:08 +00:00
|
|
|
msg.AddCNAMEReply(random.ToString(), 1);
|
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (msg.questions[0].IsName("localhost.loki"))
|
2019-03-20 16:40:28 +00:00
|
|
|
{
|
|
|
|
RouterID us = m_Router->pubkey();
|
|
|
|
msg.AddAReply(us.ToString(), 1);
|
|
|
|
}
|
2019-01-10 15:49:08 +00:00
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (msg.questions[0].qtype == dns::qTypeA || msg.questions[0].qtype == dns::qTypeAAAA)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-03-27 13:36:11 +00:00
|
|
|
const bool isV6 = msg.questions[0].qtype == dns::qTypeAAAA;
|
2019-06-11 19:42:11 +00:00
|
|
|
const bool isV4 = msg.questions[0].qtype == dns::qTypeA;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].IsName("random.snode"))
|
2019-03-22 01:00:38 +00:00
|
|
|
{
|
|
|
|
RouterID random;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (GetRouter()->GetRandomGoodRouter(random))
|
2019-05-09 12:24:38 +00:00
|
|
|
{
|
2019-03-22 01:00:38 +00:00
|
|
|
msg.AddCNAMEReply(random.ToString(), 1);
|
2019-05-09 12:24:38 +00:00
|
|
|
auto ip = ObtainServiceNodeIP(random);
|
|
|
|
msg.AddINReply(ip, false);
|
|
|
|
}
|
2019-03-22 01:00:38 +00:00
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
|
|
|
reply(msg);
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (msg.questions[0].IsName("localhost.loki"))
|
2019-03-20 16:40:28 +00:00
|
|
|
{
|
2019-03-27 13:36:11 +00:00
|
|
|
msg.AddINReply(GetIfAddr(), isV6);
|
2019-03-20 16:40:28 +00:00
|
|
|
reply(msg);
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
// forward dns for snode
|
|
|
|
RouterID r;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (r.FromString(msg.questions[0].Name()))
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t ip;
|
2019-01-02 01:04:08 +00:00
|
|
|
PubKey pubKey(r);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (isV4 && SupportsV6())
|
2019-06-11 19:42:11 +00:00
|
|
|
{
|
|
|
|
msg.hdr_fields |= dns::flags_QR | dns::flags_AA | dns::flags_RA;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (m_SNodeKeys.find(pubKey) == m_SNodeKeys.end())
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-07-26 16:19:08 +00:00
|
|
|
// we do not have it mapped, async obtain it
|
2020-04-07 18:38:56 +00:00
|
|
|
ObtainSNodeSession(r, [&](std::shared_ptr<exit::BaseSession> session) {
|
|
|
|
if (session && session->IsReady())
|
|
|
|
{
|
|
|
|
msg.AddINReply(m_KeyToIP[pubKey], isV6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
|
|
|
reply(msg);
|
|
|
|
});
|
2019-07-28 13:20:08 +00:00
|
|
|
return true;
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we have it mapped already as a service node
|
2019-01-02 01:04:08 +00:00
|
|
|
auto itr = m_KeyToIP.find(pubKey);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_KeyToIP.end())
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
|
|
|
ip = itr->second;
|
2019-03-27 13:36:11 +00:00
|
|
|
msg.AddINReply(ip, isV6);
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
|
|
|
else // fallback case that should never happen (probably)
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
msg.AddNXReply();
|
|
|
|
}
|
|
|
|
reply(msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-26 16:19:08 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::ObtainSNodeSession(const RouterID& router, exit::SessionReadyFunc obtainCb)
|
2019-07-26 16:19:08 +00:00
|
|
|
{
|
|
|
|
ObtainServiceNodeIP(router);
|
|
|
|
m_SNodeSessions[router]->AddReadyHook(obtainCb);
|
|
|
|
}
|
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
llarp_time_t
|
2018-11-28 12:32:38 +00:00
|
|
|
ExitEndpoint::Now() const
|
|
|
|
{
|
|
|
|
return m_Router->Now();
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:07:44 +00:00
|
|
|
bool
|
2019-05-07 17:46:38 +00:00
|
|
|
ExitEndpoint::VisitEndpointsFor(
|
2020-04-07 18:38:56 +00:00
|
|
|
const PubKey& pk, std::function<bool(exit::Endpoint* const)> visit)
|
2019-05-07 16:07:44 +00:00
|
|
|
{
|
|
|
|
auto range = m_ActiveExits.equal_range(pk);
|
2020-04-07 18:38:56 +00:00
|
|
|
auto itr = range.first;
|
|
|
|
while (itr != range.second)
|
2019-05-07 16:07:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (visit(itr->second.get()))
|
2019-05-07 16:07:44 +00:00
|
|
|
++itr;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
void
|
2018-11-28 16:38:20 +00:00
|
|
|
ExitEndpoint::Flush()
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
m_InetToNetwork.Process([&](Pkt_t& pkt) {
|
2019-01-07 22:15:31 +00:00
|
|
|
PubKey pk;
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2019-07-31 12:26:11 +00:00
|
|
|
auto itr = m_IPToKey.find(pkt.dstv6());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr == m_IPToKey.end())
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
|
|
|
// drop
|
2020-04-07 18:38:56 +00:00
|
|
|
LogWarn(Name(), " dropping packet, has no session at ", pkt.dstv6());
|
2018-11-15 13:13:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
pk = itr->second;
|
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
// check if this key is a service node
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_SNodeKeys.find(pk) != m_SNodeKeys.end())
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
|
|
|
// check if it's a service node session we made and queue it via our
|
|
|
|
// snode session that we made otherwise use an inbound session that
|
|
|
|
// was made by the other service node
|
|
|
|
auto itr = m_SNodeSessions.find(pk);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_SNodeSessions.end())
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->QueueUpstreamTraffic(pkt, routing::ExitPadSize))
|
2018-12-13 16:14:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
auto tryFlushingTraffic = [&](exit::Endpoint* const ep) -> bool {
|
|
|
|
if (!ep->QueueInboundTraffic(ManagedBuffer{pkt.Buffer()}))
|
2019-05-08 14:59:28 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogWarn(
|
|
|
|
Name(),
|
|
|
|
" dropped inbound traffic for session ",
|
|
|
|
pk,
|
|
|
|
" as we are overloaded (probably)");
|
2019-05-08 14:59:28 +00:00
|
|
|
// continue iteration
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// break iteration
|
|
|
|
return false;
|
|
|
|
};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!VisitEndpointsFor(pk, tryFlushingTraffic))
|
2019-05-07 16:07:44 +00:00
|
|
|
{
|
|
|
|
// we may have all dead sessions, wtf now?
|
2020-04-07 18:38:56 +00:00
|
|
|
LogWarn(
|
|
|
|
Name(),
|
|
|
|
" dropped inbound traffic for session ",
|
|
|
|
pk,
|
|
|
|
" as we have no working endpoints");
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
});
|
2018-11-28 16:38:20 +00:00
|
|
|
{
|
2018-12-13 16:14:44 +00:00
|
|
|
auto itr = m_ActiveExits.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_ActiveExits.end())
|
2018-11-28 16:38:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!itr->second->Flush())
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-01-07 22:15:31 +00:00
|
|
|
LogWarn("exit session with ", itr->first, " dropped packets");
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto itr = m_SNodeSessions.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_SNodeSessions.end())
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-04-30 13:56:39 +00:00
|
|
|
// TODO: move flush upstream to router event loop
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!itr->second->FlushUpstream())
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogWarn("failed to flush snode traffic to ", itr->first, " via outbound session");
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
2019-04-30 13:56:39 +00:00
|
|
|
itr->second->FlushDownstream();
|
2018-12-13 16:14:44 +00:00
|
|
|
++itr;
|
2018-11-28 16:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-30 16:07:17 +00:00
|
|
|
m_Router->PumpLL();
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ExitEndpoint::Start()
|
|
|
|
{
|
2019-06-10 15:54:04 +00:00
|
|
|
// map our address
|
|
|
|
const PubKey us(m_Router->pubkey());
|
2019-06-11 16:44:05 +00:00
|
|
|
const huint128_t ip = GetIfAddr();
|
2020-04-07 18:38:56 +00:00
|
|
|
m_KeyToIP[us] = ip;
|
|
|
|
m_IPToKey[ip] = us;
|
|
|
|
m_IPActivity[ip] = std::numeric_limits<llarp_time_t>::max();
|
2019-06-10 15:54:04 +00:00
|
|
|
m_SNodeKeys.insert(us);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_ShouldInitTun)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
vpn::InterfaceInfo info;
|
|
|
|
info.ifname = m_ifname;
|
|
|
|
info.addrs.emplace(m_OurRange);
|
|
|
|
|
|
|
|
m_NetIf = GetRouter()->GetVPNPlatform()->ObtainInterface(std::move(info));
|
|
|
|
if (not m_NetIf)
|
|
|
|
{
|
|
|
|
llarp::LogError("Could not create interface");
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-08 12:01:52 +00:00
|
|
|
auto loop = GetRouter()->netloop();
|
2021-01-11 23:13:22 +00:00
|
|
|
if (not loop->add_network_interface(
|
|
|
|
m_NetIf, [&](net::IPPacket pkt) { OnInetPacket(std::move(pkt)); }))
|
2019-01-16 21:06:53 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("Could not create tunnel for exit endpoint");
|
2018-12-13 16:14:44 +00:00
|
|
|
return false;
|
2019-01-16 21:06:53 +00:00
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
|
|
|
|
loop->add_ticker([&]() { Flush(); });
|
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
llarp::LogInfo("Trying to start resolver ", m_LocalResolverAddr.toString());
|
2021-03-02 18:18:22 +00:00
|
|
|
return m_Resolver->Start(m_LocalResolverAddr.createSockAddr(), m_UpstreamResolvers);
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
2018-11-15 21:47:05 +00:00
|
|
|
return true;
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
AbstractRouter*
|
2019-01-07 22:15:31 +00:00
|
|
|
ExitEndpoint::GetRouter()
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
|
|
|
return m_Router;
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2018-11-15 13:13:19 +00:00
|
|
|
ExitEndpoint::GetIfAddr() const
|
|
|
|
{
|
|
|
|
return m_IfAddr;
|
|
|
|
}
|
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
bool
|
|
|
|
ExitEndpoint::Stop()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& item : m_SNodeSessions)
|
2018-12-24 16:09:05 +00:00
|
|
|
item.second->Stop();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ExitEndpoint::ShouldRemove() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& item : m_SNodeSessions)
|
|
|
|
if (!item.second->ShouldRemove())
|
2018-12-24 16:09:05 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2018-11-15 16:19:24 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::HasLocalMappedAddrFor(const PubKey& pk) const
|
2018-11-15 16:19:24 +00:00
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
return m_KeyToIP.find(pk) != m_KeyToIP.end();
|
2018-11-15 16:19:24 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2019-01-07 22:15:31 +00:00
|
|
|
ExitEndpoint::GetIPForIdent(const PubKey pk)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t found = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!HasLocalMappedAddrFor(pk))
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
|
|
|
// allocate and map
|
2018-11-15 21:47:05 +00:00
|
|
|
found.h = AllocateNewAddress().h;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!m_KeyToIP.emplace(pk, found).second)
|
2018-11-15 16:00:16 +00:00
|
|
|
{
|
2019-01-07 22:15:31 +00:00
|
|
|
LogError(Name(), "failed to map ", pk, " to ", found);
|
2018-11-15 16:00:16 +00:00
|
|
|
return found;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!m_IPToKey.emplace(found, pk).second)
|
2018-11-15 16:00:16 +00:00
|
|
|
{
|
2019-01-07 22:15:31 +00:00
|
|
|
LogError(Name(), "failed to map ", found, " to ", pk);
|
2018-11-15 16:00:16 +00:00
|
|
|
return found;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (HasLocalMappedAddrFor(pk))
|
2019-01-07 22:15:31 +00:00
|
|
|
LogInfo(Name(), " mapping ", pk, " to ", found);
|
2018-11-15 15:43:58 +00:00
|
|
|
else
|
2019-01-07 22:15:31 +00:00
|
|
|
LogError(Name(), "failed to map ", pk, " to ", found);
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
2018-11-15 16:12:05 +00:00
|
|
|
else
|
2018-11-15 16:15:25 +00:00
|
|
|
found.h = m_KeyToIP[pk].h;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
|
|
|
MarkIPActive(found);
|
2018-11-15 21:47:05 +00:00
|
|
|
m_KeyToIP.rehash(0);
|
|
|
|
assert(HasLocalMappedAddrFor(pk));
|
2018-11-15 13:13:19 +00:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2018-11-15 13:13:19 +00:00
|
|
|
ExitEndpoint::AllocateNewAddress()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_NextAddr < m_HigestAddr)
|
2018-11-15 13:13:19 +00:00
|
|
|
return ++m_NextAddr;
|
|
|
|
|
|
|
|
// find oldest activity ip address
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t found = {0};
|
2020-04-07 18:38:56 +00:00
|
|
|
llarp_time_t min = std::numeric_limits<llarp_time_t>::max();
|
|
|
|
auto itr = m_IPActivity.begin();
|
|
|
|
while (itr != m_IPActivity.end())
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second < min)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2018-11-15 18:10:09 +00:00
|
|
|
found.h = itr->first.h;
|
2020-04-07 18:38:56 +00:00
|
|
|
min = itr->second;
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
// kick old ident off exit
|
|
|
|
// TODO: DoS
|
2019-01-07 22:15:31 +00:00
|
|
|
PubKey pk = m_IPToKey[found];
|
2018-11-15 13:13:19 +00:00
|
|
|
KickIdentOffExit(pk);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-01-11 23:13:22 +00:00
|
|
|
ExitEndpoint::QueueOutboundTraffic(net::IPPacket pkt)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
return m_NetIf && m_NetIf->WritePacket(std::move(pkt));
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::KickIdentOffExit(const PubKey& pk)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2019-01-07 22:15:31 +00:00
|
|
|
LogInfo(Name(), " kicking ", pk, " off exit");
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t ip = m_KeyToIP[pk];
|
2018-11-15 13:13:19 +00:00
|
|
|
m_KeyToIP.erase(pk);
|
|
|
|
m_IPToKey.erase(ip);
|
2020-04-07 18:38:56 +00:00
|
|
|
auto range = m_ActiveExits.equal_range(pk);
|
2018-11-15 13:13:19 +00:00
|
|
|
auto exit_itr = range.first;
|
2020-04-07 18:38:56 +00:00
|
|
|
while (exit_itr != range.second)
|
2018-11-15 13:13:19 +00:00
|
|
|
exit_itr = m_ActiveExits.erase(exit_itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-11 16:44:05 +00:00
|
|
|
ExitEndpoint::MarkIPActive(huint128_t ip)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2019-01-07 22:15:31 +00:00
|
|
|
m_IPActivity[ip] = GetRouter()->Now();
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-01-11 23:13:22 +00:00
|
|
|
ExitEndpoint::OnInetPacket(net::IPPacket pkt)
|
2018-11-15 13:13:19 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
m_InetToNetwork.Emplace(std::move(pkt));
|
2018-11-15 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::QueueSNodePacket(const llarp_buffer_t& buf, huint128_t from)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-06-11 16:44:05 +00:00
|
|
|
net::IPPacket pkt;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!pkt.Load(buf))
|
2018-12-13 16:14:44 +00:00
|
|
|
return false;
|
|
|
|
// rewrite ip
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_UseV6)
|
2019-12-06 20:33:43 +00:00
|
|
|
pkt.UpdateIPv6Address(from, m_IfAddr);
|
|
|
|
else
|
2020-05-20 22:48:13 +00:00
|
|
|
pkt.UpdateIPv4Address(xhtonl(net::TruncateV6(from)), xhtonl(net::TruncateV6(m_IfAddr)));
|
2021-01-11 23:13:22 +00:00
|
|
|
return m_NetIf and m_NetIf->WritePacket(std::move(pkt));
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
exit::Endpoint*
|
|
|
|
ExitEndpoint::FindEndpointByPath(const PathID_t& path)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
exit::Endpoint* endpoint = nullptr;
|
2019-01-07 22:15:31 +00:00
|
|
|
PubKey pk;
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
auto itr = m_Paths.find(path);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr == m_Paths.end())
|
2018-11-14 12:23:08 +00:00
|
|
|
return nullptr;
|
|
|
|
pk = itr->second;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto itr = m_ActiveExits.find(pk);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_ActiveExits.end())
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->PubKey() == pk)
|
2018-11-15 21:47:05 +00:00
|
|
|
endpoint = itr->second.get();
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::UpdateEndpointPath(const PubKey& remote, const PathID_t& next)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
// check if already mapped
|
|
|
|
auto itr = m_Paths.find(next);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_Paths.end())
|
2018-11-14 12:23:08 +00:00
|
|
|
return false;
|
2019-02-18 10:35:16 +00:00
|
|
|
m_Paths.emplace(next, remote);
|
2018-11-14 12:23:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:22:04 +00:00
|
|
|
void
|
|
|
|
ExitEndpoint::Configure(const NetworkConfig& networkConfig, const DnsConfig& dnsConfig)
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2020-04-28 14:22:04 +00:00
|
|
|
/*
|
2020-04-28 16:46:13 +00:00
|
|
|
* TODO: pre-config refactor, this was checking a couple things that were extremely vague
|
|
|
|
* these could have appeared on either [dns] or [network], but they weren't documented
|
|
|
|
* anywhere
|
|
|
|
*
|
2020-04-07 18:38:56 +00:00
|
|
|
if (k == "type" && v == "null")
|
2018-11-15 21:47:05 +00:00
|
|
|
{
|
|
|
|
m_ShouldInitTun = false;
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (k == "exit")
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-11-14 12:23:08 +00:00
|
|
|
m_PermitExit = IsTrueValue(v.c_str());
|
2018-11-12 16:43:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-28 14:22:04 +00:00
|
|
|
*/
|
2020-05-06 14:45:30 +00:00
|
|
|
if (networkConfig.m_endpointType == "null")
|
|
|
|
{
|
|
|
|
m_ShouldInitTun = false;
|
|
|
|
}
|
2020-04-28 14:22:04 +00:00
|
|
|
|
2020-04-28 15:37:22 +00:00
|
|
|
m_LocalResolverAddr = dnsConfig.m_bind;
|
|
|
|
m_UpstreamResolvers = dnsConfig.m_upstreamDNS;
|
2020-04-28 14:22:04 +00:00
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
m_OurRange = networkConfig.m_ifaddr;
|
2020-10-09 15:39:39 +00:00
|
|
|
if (!m_OurRange.addr.h)
|
|
|
|
{
|
|
|
|
const auto maybe = llarp::FindFreeRange();
|
|
|
|
if (not maybe.has_value())
|
|
|
|
throw std::runtime_error("cannot find free interface range");
|
|
|
|
m_OurRange = *maybe;
|
|
|
|
}
|
2020-06-24 13:24:07 +00:00
|
|
|
const auto host_str = m_OurRange.BaseAddressString();
|
2020-04-28 14:22:04 +00:00
|
|
|
// string, or just a plain char array?
|
|
|
|
m_IfAddr = m_OurRange.addr;
|
|
|
|
m_NextAddr = m_IfAddr;
|
|
|
|
m_HigestAddr = m_OurRange.HighestAddr();
|
2020-06-24 13:24:07 +00:00
|
|
|
m_UseV6 = not m_OurRange.IsV4();
|
2020-04-28 14:22:04 +00:00
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
m_ifname = networkConfig.m_ifname;
|
|
|
|
if (m_ifname.empty())
|
2020-10-09 15:39:39 +00:00
|
|
|
{
|
|
|
|
const auto maybe = llarp::FindFreeTun();
|
|
|
|
if (not maybe.has_value())
|
|
|
|
throw std::runtime_error("cannot find free interface name");
|
2021-01-11 23:13:22 +00:00
|
|
|
m_ifname = *maybe;
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
LogInfo(Name(), " set ifname to ", m_ifname);
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2020-04-28 14:22:04 +00:00
|
|
|
// TODO: "exit-whitelist" and "exit-blacklist"
|
|
|
|
// (which weren't originally implemented)
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::ObtainServiceNodeIP(const RouterID& other)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2019-06-10 15:54:04 +00:00
|
|
|
const PubKey pubKey(other);
|
|
|
|
const PubKey us(m_Router->pubkey());
|
|
|
|
// just in case
|
2020-04-07 18:38:56 +00:00
|
|
|
if (pubKey == us)
|
2019-06-10 15:54:04 +00:00
|
|
|
return m_IfAddr;
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t ip = GetIPForIdent(pubKey);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_SNodeKeys.emplace(pubKey).second)
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
auto session = std::make_shared<exit::SNodeSession>(
|
2019-01-02 01:04:08 +00:00
|
|
|
other,
|
2020-04-07 18:38:56 +00:00
|
|
|
std::bind(&ExitEndpoint::QueueSNodePacket, this, std::placeholders::_1, ip),
|
|
|
|
GetRouter(),
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
true,
|
|
|
|
false);
|
2019-04-23 16:13:22 +00:00
|
|
|
// this is a new service node make an outbound session to them
|
|
|
|
m_SNodeSessions.emplace(other, session);
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::AllocateNewExit(const PubKey pk, const PathID_t& path, bool wantInternet)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (wantInternet && !m_PermitExit)
|
2018-11-15 13:13:19 +00:00
|
|
|
return false;
|
2019-06-11 16:44:05 +00:00
|
|
|
auto ip = GetIPForIdent(pk);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (GetRouter()->pathContext().TransitHopPreviousIsRouter(path, pk.as_array()))
|
2018-12-13 16:14:44 +00:00
|
|
|
{
|
|
|
|
// we think this path belongs to a service node
|
|
|
|
// mark it as such so we don't make an outbound session to them
|
2019-01-02 01:03:53 +00:00
|
|
|
m_SNodeKeys.emplace(pk.as_array());
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
m_ActiveExits.emplace(
|
|
|
|
pk, std::make_unique<exit::Endpoint>(pk, path, !wantInternet, ip, this));
|
2018-12-13 16:14:44 +00:00
|
|
|
|
2018-11-15 14:08:42 +00:00
|
|
|
m_Paths[path] = pk;
|
2019-07-02 21:28:28 +00:00
|
|
|
|
2018-11-15 21:47:05 +00:00
|
|
|
return HasLocalMappedAddrFor(pk);
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
std::string
|
|
|
|
ExitEndpoint::Name() const
|
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::DelEndpointInfo(const PathID_t& path)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
m_Paths.erase(path);
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
ExitEndpoint::RemoveExit(const exit::Endpoint* ep)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
|
|
|
auto range = m_ActiveExits.equal_range(ep->PubKey());
|
2020-04-07 18:38:56 +00:00
|
|
|
auto itr = range.first;
|
|
|
|
while (itr != range.second)
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->LocalPath() == ep->LocalPath())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
|
|
|
itr = m_ActiveExits.erase(itr);
|
|
|
|
// now ep is gone af
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
void
|
|
|
|
ExitEndpoint::Tick(llarp_time_t now)
|
|
|
|
{
|
|
|
|
{
|
2018-12-13 16:14:44 +00:00
|
|
|
auto itr = m_SNodeSessions.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_SNodeSessions.end())
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->IsExpired(now))
|
2018-12-13 16:14:44 +00:00
|
|
|
itr = m_SNodeSessions.erase(itr);
|
|
|
|
else
|
2019-04-23 16:13:22 +00:00
|
|
|
{
|
|
|
|
itr->second->Tick(now);
|
2018-12-13 16:14:44 +00:00
|
|
|
++itr;
|
2019-04-23 16:13:22 +00:00
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
|
|
|
{
|
2018-12-23 13:29:11 +00:00
|
|
|
// expire
|
2018-12-13 16:14:44 +00:00
|
|
|
auto itr = m_ActiveExits.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_ActiveExits.end())
|
2018-11-14 18:02:27 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr->second->IsExpired(now))
|
2018-12-13 16:14:44 +00:00
|
|
|
itr = m_ActiveExits.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
2018-12-23 13:29:11 +00:00
|
|
|
}
|
|
|
|
// pick chosen exits and tick
|
|
|
|
m_ChosenExits.clear();
|
|
|
|
itr = m_ActiveExits.begin();
|
2020-04-07 18:38:56 +00:00
|
|
|
while (itr != m_ActiveExits.end())
|
2018-12-23 13:29:11 +00:00
|
|
|
{
|
|
|
|
// do we have an exit set for this key?
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_ChosenExits.find(itr->first) != m_ChosenExits.end())
|
2018-12-23 13:29:11 +00:00
|
|
|
{
|
|
|
|
// yes
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_ChosenExits[itr->first]->createdAt < itr->second->createdAt)
|
2018-12-23 13:29:11 +00:00
|
|
|
{
|
|
|
|
// if the iterators's exit is newer use it for the chosen exit for
|
|
|
|
// key
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!itr->second->LooksDead(now))
|
2018-12-23 14:04:26 +00:00
|
|
|
m_ChosenExits[itr->first] = itr->second.get();
|
2018-12-23 13:29:11 +00:00
|
|
|
}
|
2018-12-13 16:14:44 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
else if (!itr->second->LooksDead(now)) // set chosen exit if not dead for key that
|
|
|
|
// doesn't have one yet
|
2018-12-23 13:29:11 +00:00
|
|
|
m_ChosenExits[itr->first] = itr->second.get();
|
|
|
|
// tick which clears the tx rx counters
|
|
|
|
itr->second->Tick(now);
|
|
|
|
++itr;
|
2018-11-14 18:02:27 +00:00
|
|
|
}
|
2018-11-12 16:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace handlers
|
2018-11-21 23:37:17 +00:00
|
|
|
} // namespace llarp
|