2021-03-09 23:24:35 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <llarp/dns/server.hpp>
|
|
|
|
#include <llarp/ev/ev.hpp>
|
|
|
|
#include <llarp/net/ip.hpp>
|
|
|
|
#include <llarp/net/ip_packet.hpp>
|
|
|
|
#include <llarp/net/net.hpp>
|
|
|
|
#include <llarp/service/endpoint.hpp>
|
2022-07-28 18:07:38 +02:00
|
|
|
#include <llarp/service/protocol_type.hpp>
|
|
|
|
#include <llarp/util/priority_queue.hpp>
|
2021-03-09 23:24:35 +01:00
|
|
|
#include <llarp/util/thread/threading.hpp>
|
|
|
|
#include <llarp/vpn/packet_router.hpp>
|
2022-07-28 18:07:38 +02:00
|
|
|
#include <llarp/vpn/platform.hpp>
|
2018-08-08 21:37:33 +02:00
|
|
|
|
2019-03-03 21:51:47 +01:00
|
|
|
#include <future>
|
2021-03-15 17:01:19 +01:00
|
|
|
#include <type_traits>
|
|
|
|
#include <variant>
|
2022-04-05 00:12:27 +02:00
|
|
|
|
2018-08-08 21:37:33 +02:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace handlers
|
|
|
|
{
|
2019-04-23 18:13:22 +02:00
|
|
|
struct TunEndpoint : public service::Endpoint,
|
2022-04-07 22:44:23 +02:00
|
|
|
public dns::Resolver_Base,
|
2020-04-07 20:38:56 +02:00
|
|
|
public std::enable_shared_from_this<TunEndpoint>
|
2018-08-08 21:37:33 +02:00
|
|
|
{
|
2021-01-12 00:13:22 +01:00
|
|
|
TunEndpoint(AbstractRouter* r, llarp::service::Context* parent);
|
2019-07-31 01:42:13 +02:00
|
|
|
~TunEndpoint() override;
|
2018-08-16 16:34:15 +02:00
|
|
|
|
2022-07-28 18:07:38 +02:00
|
|
|
vpn::NetworkInterface*
|
|
|
|
GetVPNInterface() override
|
|
|
|
{
|
|
|
|
return m_NetIf.get();
|
|
|
|
}
|
|
|
|
|
2022-04-07 22:44:23 +02:00
|
|
|
int
|
|
|
|
Rank() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view
|
|
|
|
ResolverName() const override
|
|
|
|
{
|
|
|
|
return "lokinet";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MaybeHookDNS(
|
2022-07-28 18:07:38 +02:00
|
|
|
std::shared_ptr<dns::PacketSource_Base> source,
|
2022-04-07 22:44:23 +02:00
|
|
|
const dns::Message& query,
|
|
|
|
const SockAddr& to,
|
|
|
|
const SockAddr& from) override;
|
|
|
|
|
2019-04-23 18:13:22 +02:00
|
|
|
path::PathSet_ptr
|
|
|
|
GetSelf() override
|
|
|
|
{
|
|
|
|
return shared_from_this();
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:34:41 +02:00
|
|
|
std::weak_ptr<path::PathSet>
|
|
|
|
GetWeak() override
|
|
|
|
{
|
|
|
|
return weak_from_this();
|
|
|
|
}
|
|
|
|
|
2021-02-05 22:48:57 +01:00
|
|
|
void
|
|
|
|
Thaw() override;
|
|
|
|
|
2021-09-01 20:10:08 +02:00
|
|
|
// Reconfigures DNS servers and restarts libunbound with the new servers. Returns the old set
|
|
|
|
// of configured dns servers.
|
|
|
|
std::vector<SockAddr>
|
|
|
|
ReconfigureDNS(std::vector<SockAddr> servers);
|
|
|
|
|
2019-07-31 01:42:13 +02:00
|
|
|
bool
|
2020-05-01 18:13:49 +02:00
|
|
|
Configure(const NetworkConfig& conf, const DnsConfig& dnsConf) override;
|
2018-08-16 16:34:15 +02:00
|
|
|
|
2020-05-21 16:18:23 +02:00
|
|
|
void
|
2021-03-16 12:56:27 +01:00
|
|
|
SendPacketToRemote(const llarp_buffer_t&, service::ProtocolType) override{};
|
2020-05-21 16:18:23 +02:00
|
|
|
|
2020-08-21 17:07:37 +02:00
|
|
|
std::string
|
|
|
|
GetIfName() const override;
|
|
|
|
|
2019-07-31 01:42:13 +02:00
|
|
|
void
|
2018-12-05 00:45:08 +01:00
|
|
|
Tick(llarp_time_t now) override;
|
2018-08-16 16:34:15 +02:00
|
|
|
|
2019-02-11 18:14:43 +01:00
|
|
|
util::StatusObject
|
2020-06-24 15:24:07 +02:00
|
|
|
ExtractStatus() const override;
|
2019-02-08 20:43:25 +01:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
std::unordered_map<std::string, std::string>
|
2019-04-22 16:00:59 +02:00
|
|
|
NotifyParams() const override;
|
|
|
|
|
2019-06-11 18:44:05 +02:00
|
|
|
bool
|
|
|
|
SupportsV6() const override;
|
|
|
|
|
2018-12-03 23:22:59 +01:00
|
|
|
bool
|
2022-04-07 22:44:23 +02:00
|
|
|
ShouldHookDNSMessage(const dns::Message& msg) const;
|
2018-12-03 23:22:59 +01:00
|
|
|
|
|
|
|
bool
|
2022-04-07 22:44:23 +02:00
|
|
|
HandleHookedDNSMessage(dns::Message query, std::function<void(dns::Message)> sendreply);
|
2018-12-03 23:22:59 +01:00
|
|
|
|
2018-08-16 16:34:15 +02:00
|
|
|
void
|
|
|
|
TickTun(llarp_time_t now);
|
|
|
|
|
2018-08-22 17:52:10 +02:00
|
|
|
bool
|
2019-06-11 18:44:05 +02:00
|
|
|
MapAddress(const service::Address& remote, huint128_t ip, bool SNode);
|
2018-08-22 17:52:10 +02:00
|
|
|
|
2018-08-16 16:34:15 +02:00
|
|
|
bool
|
2018-12-05 00:45:08 +01:00
|
|
|
Start() override;
|
2018-08-16 16:34:15 +02:00
|
|
|
|
2018-12-24 17:09:05 +01:00
|
|
|
bool
|
|
|
|
Stop() override;
|
|
|
|
|
2018-12-02 19:07:07 +01:00
|
|
|
bool
|
2018-11-30 15:14:30 +01:00
|
|
|
IsSNode() const;
|
|
|
|
|
2018-08-16 16:34:15 +02:00
|
|
|
/// set up tun interface, blocking
|
|
|
|
bool
|
|
|
|
SetupTun();
|
|
|
|
|
2022-04-07 22:44:23 +02:00
|
|
|
void
|
|
|
|
SetupDNS();
|
|
|
|
|
|
|
|
/// overrides Endpoint
|
|
|
|
std::shared_ptr<dns::Server>
|
|
|
|
DNS() const override
|
|
|
|
{
|
|
|
|
return m_DNS;
|
|
|
|
};
|
|
|
|
|
2018-08-16 16:34:15 +02:00
|
|
|
/// overrides Endpoint
|
|
|
|
bool
|
2018-12-05 00:45:08 +01:00
|
|
|
SetupNetworking() override;
|
2018-08-16 16:34:15 +02:00
|
|
|
|
2018-09-18 19:48:26 +02:00
|
|
|
/// overrides Endpoint
|
2019-07-01 15:44:25 +02:00
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
HandleInboundPacket(
|
2020-10-08 13:59:01 +02:00
|
|
|
const service::ConvoTag tag,
|
|
|
|
const llarp_buffer_t& pkt,
|
|
|
|
service::ProtocolType t,
|
|
|
|
uint64_t seqno) override;
|
2019-07-01 15:44:25 +02:00
|
|
|
|
2018-08-21 20:17:16 +02:00
|
|
|
/// handle inbound traffic
|
2018-09-18 13:08:47 +02:00
|
|
|
bool
|
2020-10-08 13:59:01 +02:00
|
|
|
HandleWriteIPPacket(
|
|
|
|
const llarp_buffer_t& buf, huint128_t src, huint128_t dst, uint64_t seqno);
|
2018-08-18 16:01:21 +02:00
|
|
|
|
2021-01-12 00:13:22 +01:00
|
|
|
/// we got a packet from the user
|
|
|
|
void
|
|
|
|
HandleGotUserPacket(llarp::net::IPPacket pkt);
|
|
|
|
|
2018-11-14 13:23:08 +01:00
|
|
|
/// get the local interface's address
|
2019-06-11 18:44:05 +02:00
|
|
|
huint128_t
|
2019-07-05 16:41:26 +02:00
|
|
|
GetIfAddr() const override;
|
2018-11-14 13:23:08 +01:00
|
|
|
|
2019-07-05 16:48:10 +02:00
|
|
|
/// we have an interface addr
|
|
|
|
bool
|
|
|
|
HasIfAddr() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:23:08 +01:00
|
|
|
bool
|
2019-06-11 18:44:05 +02:00
|
|
|
HasLocalIP(const huint128_t& ip) const;
|
2018-11-14 13:23:08 +01:00
|
|
|
|
2021-04-14 17:07:06 +02:00
|
|
|
std::optional<net::TrafficPolicy>
|
|
|
|
GetExitPolicy() const override
|
|
|
|
{
|
2021-04-14 21:40:57 +02:00
|
|
|
return m_TrafficPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<IPRange>
|
|
|
|
GetOwnedRanges() const override
|
|
|
|
{
|
|
|
|
return m_OwnedRanges;
|
2021-04-14 17:07:06 +02:00
|
|
|
}
|
|
|
|
|
2021-05-08 13:16:21 +02:00
|
|
|
llarp_time_t
|
2021-05-11 11:12:02 +02:00
|
|
|
PathAlignmentTimeout() const override
|
2021-05-08 13:16:21 +02:00
|
|
|
{
|
|
|
|
return m_PathAlignmentTimeout;
|
|
|
|
}
|
|
|
|
|
2021-04-14 17:07:06 +02:00
|
|
|
/// ip packet against any exit policies we have
|
|
|
|
/// returns false if this traffic is disallowed by any of those policies
|
|
|
|
/// returns true otherwise
|
|
|
|
bool
|
|
|
|
ShouldAllowTraffic(const net::IPPacket& pkt) const;
|
|
|
|
|
2018-11-14 13:23:08 +01:00
|
|
|
/// get a key for ip address
|
2021-03-15 17:01:19 +01:00
|
|
|
std::optional<std::variant<service::Address, RouterID>>
|
|
|
|
ObtainAddrForIP(huint128_t ip) const override;
|
2019-11-29 01:37:58 +01:00
|
|
|
|
2018-10-23 20:18:00 +02:00
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
HasAddress(const AlignedBuffer<32>& addr) const
|
2018-10-23 20:18:00 +02:00
|
|
|
{
|
2018-11-14 13:23:08 +01:00
|
|
|
return m_AddrToIP.find(addr) != m_AddrToIP.end();
|
2018-10-23 20:18:00 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:23:08 +01:00
|
|
|
/// get ip address for key unconditionally
|
2019-06-11 18:44:05 +02:00
|
|
|
huint128_t
|
2021-03-15 17:01:19 +01:00
|
|
|
ObtainIPForAddr(std::variant<service::Address, RouterID> addr) override;
|
2018-10-23 23:33:49 +02:00
|
|
|
|
2019-07-31 01:42:13 +02:00
|
|
|
void
|
2019-05-07 19:46:38 +02:00
|
|
|
ResetInternalState() override;
|
|
|
|
|
2018-08-17 21:49:58 +02:00
|
|
|
protected:
|
2020-10-08 13:59:01 +02:00
|
|
|
struct WritePacket
|
|
|
|
{
|
|
|
|
uint64_t seqno;
|
|
|
|
net::IPPacket pkt;
|
|
|
|
|
|
|
|
bool
|
2022-04-05 00:12:27 +02:00
|
|
|
operator>(const WritePacket& other) const
|
2020-10-08 13:59:01 +02:00
|
|
|
{
|
2022-04-05 00:12:27 +02:00
|
|
|
return seqno > other.seqno;
|
2020-10-08 13:59:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-17 21:49:58 +02:00
|
|
|
/// queue for sending packets to user from network
|
2022-04-05 00:12:27 +02:00
|
|
|
util::ascending_priority_queue<WritePacket> m_NetworkToUserPktQueue;
|
2021-11-12 20:34:03 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
Pump(llarp_time_t now) override;
|
|
|
|
|
2018-08-17 21:49:58 +02:00
|
|
|
/// return true if we have a remote loki address for this ip address
|
|
|
|
bool
|
2019-06-11 18:44:05 +02:00
|
|
|
HasRemoteForIP(huint128_t ipv4) const;
|
2018-10-19 16:53:06 +02:00
|
|
|
|
2018-08-21 20:17:16 +02:00
|
|
|
/// mark this address as active
|
|
|
|
void
|
2019-06-11 18:44:05 +02:00
|
|
|
MarkIPActive(huint128_t ip);
|
2018-08-21 20:17:16 +02:00
|
|
|
|
2018-09-10 13:08:09 +02:00
|
|
|
/// mark this address as active forever
|
|
|
|
void
|
2019-06-11 18:44:05 +02:00
|
|
|
MarkIPActiveForever(huint128_t ip);
|
2018-09-10 13:08:09 +02:00
|
|
|
|
2021-06-04 13:15:00 +02:00
|
|
|
/// flush writing ip packets to interface
|
|
|
|
void
|
|
|
|
FlushWrite();
|
|
|
|
|
2018-11-14 13:23:08 +01:00
|
|
|
/// maps ip to key (host byte order)
|
2020-04-07 20:38:56 +02:00
|
|
|
std::unordered_map<huint128_t, AlignedBuffer<32>> m_IPToAddr;
|
2018-11-14 13:23:08 +01:00
|
|
|
/// maps key to ip (host byte order)
|
2021-03-09 19:39:40 +01:00
|
|
|
std::unordered_map<AlignedBuffer<32>, huint128_t> m_AddrToIP;
|
2018-11-14 13:23:08 +01:00
|
|
|
|
2018-12-02 19:07:07 +01:00
|
|
|
/// maps key to true if key is a service node, maps key to false if key is
|
|
|
|
/// a hidden service
|
2021-03-09 19:39:40 +01:00
|
|
|
std::unordered_map<AlignedBuffer<32>, bool> m_SNodes;
|
2018-11-29 14:12:35 +01:00
|
|
|
|
2021-12-01 19:12:44 +01:00
|
|
|
/// maps ip address to an exit endpoint, useful when we have multiple exits on a range
|
|
|
|
std::unordered_map<huint128_t, service::Address> m_ExitIPToExitAddress;
|
|
|
|
|
2018-08-16 16:34:15 +02:00
|
|
|
private:
|
2021-12-01 19:12:44 +01:00
|
|
|
/// given an ip address that is not mapped locally find the address it shall be forwarded to
|
|
|
|
/// optionally provide a custom selection strategy, if none is provided it will choose a
|
|
|
|
/// random entry from the available choices
|
|
|
|
/// return std::nullopt if we cannot route this address to an exit
|
|
|
|
std::optional<service::Address>
|
|
|
|
ObtainExitAddressFor(
|
|
|
|
huint128_t ip,
|
|
|
|
std::function<service::Address(std::unordered_set<service::Address>)> exitSelectionStrat =
|
|
|
|
nullptr);
|
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
template <typename Addr_t, typename Endpoint_t>
|
2018-12-03 23:22:59 +01:00
|
|
|
void
|
2020-04-07 20:38:56 +02:00
|
|
|
SendDNSReply(
|
|
|
|
Addr_t addr,
|
|
|
|
Endpoint_t ctx,
|
|
|
|
std::shared_ptr<dns::Message> query,
|
|
|
|
std::function<void(dns::Message)> reply,
|
|
|
|
bool sendIPv6)
|
2019-03-01 20:10:42 +01:00
|
|
|
{
|
2021-06-02 21:27:13 +02:00
|
|
|
if (ctx)
|
2019-03-01 20:10:42 +01:00
|
|
|
{
|
2021-03-15 17:01:19 +01:00
|
|
|
huint128_t ip = ObtainIPForAddr(addr);
|
2020-03-09 21:47:27 +01:00
|
|
|
query->answers.clear();
|
2019-03-27 14:36:11 +01:00
|
|
|
query->AddINReply(ip, sendIPv6);
|
2019-03-01 20:10:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
query->AddNXReply();
|
|
|
|
reply(*query);
|
|
|
|
}
|
2022-04-07 22:44:23 +02:00
|
|
|
|
|
|
|
/// dns subsystem for this endpoint
|
|
|
|
std::shared_ptr<dns::Server> m_DNS;
|
|
|
|
|
|
|
|
DnsConfig m_DnsConfig;
|
2018-09-22 12:25:16 +02:00
|
|
|
|
2018-08-21 20:17:16 +02:00
|
|
|
/// maps ip address to timestamp last active
|
2020-04-07 20:38:56 +02:00
|
|
|
std::unordered_map<huint128_t, llarp_time_t> m_IPActivity;
|
2018-09-23 18:47:18 +02:00
|
|
|
/// our ip address (host byte order)
|
2019-06-11 18:44:05 +02:00
|
|
|
huint128_t m_OurIP;
|
2021-02-17 13:54:18 +01:00
|
|
|
/// our network interface's ipv6 address
|
|
|
|
huint128_t m_OurIPv6;
|
2021-02-16 16:59:18 +01:00
|
|
|
|
2018-09-23 18:47:18 +02:00
|
|
|
/// next ip address to allocate (host byte order)
|
2019-06-11 18:44:05 +02:00
|
|
|
huint128_t m_NextIP;
|
2018-10-10 17:14:45 +02:00
|
|
|
/// highest ip address to allocate (host byte order)
|
2019-06-11 18:44:05 +02:00
|
|
|
huint128_t m_MaxIP;
|
2018-12-03 23:22:59 +01:00
|
|
|
/// our ip range we are using
|
|
|
|
llarp::IPRange m_OurRange;
|
2019-05-06 14:42:21 +02:00
|
|
|
/// list of strict connect addresses for hooks
|
2020-05-06 22:38:44 +02:00
|
|
|
std::vector<IpAddress> m_StrictConnectAddrs;
|
2019-06-11 18:44:05 +02:00
|
|
|
/// use v6?
|
|
|
|
bool m_UseV6;
|
2020-08-21 17:07:37 +02:00
|
|
|
std::string m_IfName;
|
2019-10-04 20:10:58 +02:00
|
|
|
|
2021-03-18 16:59:02 +01:00
|
|
|
std::optional<huint128_t> m_BaseV6Address;
|
|
|
|
|
2021-01-12 00:13:22 +01:00
|
|
|
std::shared_ptr<vpn::NetworkInterface> m_NetIf;
|
2021-03-02 19:18:22 +01:00
|
|
|
|
2022-07-28 18:07:38 +02:00
|
|
|
std::shared_ptr<vpn::PacketRouter> m_PacketRouter;
|
2021-04-14 17:07:06 +02:00
|
|
|
|
2021-04-14 21:40:57 +02:00
|
|
|
std::optional<net::TrafficPolicy> m_TrafficPolicy;
|
|
|
|
/// ranges we advetise as reachable
|
|
|
|
std::set<IPRange> m_OwnedRanges;
|
2021-05-01 22:25:32 +02:00
|
|
|
/// how long to wait for path alignment
|
|
|
|
llarp_time_t m_PathAlignmentTimeout;
|
2021-06-17 18:05:50 +02:00
|
|
|
|
|
|
|
/// a file to load / store the ephemeral address map to
|
|
|
|
std::optional<fs::path> m_PersistAddrMapFile;
|
2022-07-28 18:07:38 +02:00
|
|
|
|
|
|
|
/// for raw packet dns
|
|
|
|
std::shared_ptr<vpn::I_Packet_IO> m_RawDNS;
|
2018-08-08 21:37:33 +02:00
|
|
|
};
|
2019-11-29 01:37:58 +01:00
|
|
|
|
2018-08-08 21:37:33 +02:00
|
|
|
} // namespace handlers
|
|
|
|
} // namespace llarp
|