2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <llarp/dns/server.hpp>
|
|
|
|
#include <llarp/ev/ev.hpp>
|
|
|
|
#include <llarp/ev/vpn.hpp>
|
|
|
|
#include <llarp/net/ip.hpp>
|
|
|
|
#include <llarp/net/ip_packet.hpp>
|
|
|
|
#include <llarp/net/net.hpp>
|
|
|
|
#include <llarp/service/endpoint.hpp>
|
|
|
|
#include <llarp/util/codel.hpp>
|
|
|
|
#include <llarp/util/thread/threading.hpp>
|
|
|
|
#include <llarp/vpn/packet_router.hpp>
|
2018-08-08 19:37:33 +00:00
|
|
|
|
2019-03-03 20:51:47 +00:00
|
|
|
#include <future>
|
2020-10-08 11:59:01 +00:00
|
|
|
#include <queue>
|
2019-03-03 20:51:47 +00:00
|
|
|
|
2018-08-08 19:37:33 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace handlers
|
|
|
|
{
|
2019-04-23 16:13:22 +00:00
|
|
|
struct TunEndpoint : public service::Endpoint,
|
|
|
|
public dns::IQueryHandler,
|
2020-04-07 18:38:56 +00:00
|
|
|
public std::enable_shared_from_this<TunEndpoint>
|
2018-08-08 19:37:33 +00:00
|
|
|
{
|
2021-01-11 23:13:22 +00:00
|
|
|
TunEndpoint(AbstractRouter* r, llarp::service::Context* parent);
|
2019-07-30 23:42:13 +00:00
|
|
|
~TunEndpoint() override;
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2019-04-23 16:13:22 +00:00
|
|
|
path::PathSet_ptr
|
|
|
|
GetSelf() override
|
|
|
|
{
|
|
|
|
return shared_from_this();
|
|
|
|
}
|
|
|
|
|
2021-02-05 21:48:57 +00:00
|
|
|
void
|
|
|
|
Thaw() override;
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
bool
|
2020-05-01 16:13:49 +00:00
|
|
|
Configure(const NetworkConfig& conf, const DnsConfig& dnsConf) override;
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
void
|
|
|
|
SendPacketToRemote(const llarp_buffer_t&) override{};
|
|
|
|
|
2020-08-21 15:07:37 +00:00
|
|
|
std::string
|
|
|
|
GetIfName() const override;
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
void
|
2018-12-04 23:45:08 +00:00
|
|
|
Tick(llarp_time_t now) override;
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
2020-06-24 13:24:07 +00:00
|
|
|
ExtractStatus() const override;
|
2019-02-08 19:43:25 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unordered_map<std::string, std::string>
|
2019-04-22 14:00:59 +00:00
|
|
|
NotifyParams() const override;
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
bool
|
|
|
|
SupportsV6() const override;
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
bool
|
|
|
|
ShouldHookDNSMessage(const dns::Message& msg) const override;
|
|
|
|
|
|
|
|
bool
|
|
|
|
HandleHookedDNSMessage(
|
2020-04-07 18:38:56 +00:00
|
|
|
dns::Message query, std::function<void(dns::Message)> sendreply) override;
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
void
|
|
|
|
TickTun(llarp_time_t now);
|
|
|
|
|
2018-08-22 15:52:10 +00:00
|
|
|
bool
|
2019-06-11 16:44:05 +00:00
|
|
|
MapAddress(const service::Address& remote, huint128_t ip, bool SNode);
|
2018-08-22 15:52:10 +00:00
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
bool
|
2018-12-04 23:45:08 +00:00
|
|
|
Start() override;
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2018-12-24 16:09:05 +00:00
|
|
|
bool
|
|
|
|
Stop() override;
|
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
bool
|
2018-11-30 14:14:30 +00:00
|
|
|
IsSNode() const;
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
/// set up tun interface, blocking
|
|
|
|
bool
|
|
|
|
SetupTun();
|
|
|
|
|
|
|
|
/// overrides Endpoint
|
|
|
|
bool
|
2018-12-04 23:45:08 +00:00
|
|
|
SetupNetworking() override;
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2018-09-18 17:48:26 +00:00
|
|
|
/// overrides Endpoint
|
2019-07-01 13:44:25 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
HandleInboundPacket(
|
2020-10-08 11:59:01 +00:00
|
|
|
const service::ConvoTag tag,
|
|
|
|
const llarp_buffer_t& pkt,
|
|
|
|
service::ProtocolType t,
|
|
|
|
uint64_t seqno) override;
|
2019-07-01 13:44:25 +00:00
|
|
|
|
2018-08-21 18:17:16 +00:00
|
|
|
/// handle inbound traffic
|
2018-09-18 11:08:47 +00:00
|
|
|
bool
|
2020-10-08 11:59:01 +00:00
|
|
|
HandleWriteIPPacket(
|
|
|
|
const llarp_buffer_t& buf, huint128_t src, huint128_t dst, uint64_t seqno);
|
2018-08-18 14:01:21 +00:00
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
/// queue outbound packet to the world
|
|
|
|
bool
|
2019-06-11 16:44:05 +00:00
|
|
|
QueueOutboundTraffic(llarp::net::IPPacket&& pkt);
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
/// we got a packet from the user
|
|
|
|
void
|
|
|
|
HandleGotUserPacket(llarp::net::IPPacket pkt);
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
/// get the local interface's address
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2019-07-05 14:41:26 +00:00
|
|
|
GetIfAddr() const override;
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2019-07-05 14:48:10 +00:00
|
|
|
/// we have an interface addr
|
|
|
|
bool
|
|
|
|
HasIfAddr() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
2019-06-11 16:44:05 +00:00
|
|
|
HasLocalIP(const huint128_t& ip) const;
|
2018-11-14 12:23:08 +00:00
|
|
|
|
|
|
|
/// get a key for ip address
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Addr_t>
|
2019-11-29 00:37:58 +00:00
|
|
|
Addr_t
|
2019-06-11 16:44:05 +00:00
|
|
|
ObtainAddrForIP(huint128_t ip, bool isSNode)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2019-11-29 00:37:58 +00:00
|
|
|
Addr_t addr;
|
2018-11-14 12:23:08 +00:00
|
|
|
auto itr = m_IPToAddr.find(ip);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr != m_IPToAddr.end() and m_SNodes[itr->second] == isSNode)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
2019-11-29 00:37:58 +00:00
|
|
|
addr = Addr_t(itr->second);
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
|
|
|
// found
|
2019-11-29 00:37:58 +00:00
|
|
|
return addr;
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
2018-10-19 14:53:06 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Addr_t>
|
2019-11-29 00:37:58 +00:00
|
|
|
bool
|
|
|
|
FindAddrForIP(Addr_t& addr, huint128_t ip);
|
|
|
|
|
2018-10-23 18:18:00 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
HasAddress(const AlignedBuffer<32>& addr) const
|
2018-10-23 18:18:00 +00:00
|
|
|
{
|
2018-11-14 12:23:08 +00:00
|
|
|
return m_AddrToIP.find(addr) != m_AddrToIP.end();
|
2018-10-23 18:18:00 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
/// get ip address for key unconditionally
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
2020-08-20 12:59:01 +00:00
|
|
|
ObtainIPForAddr(const AlignedBuffer<32>& addr, bool serviceNode) override;
|
2018-10-23 21:33:49 +00:00
|
|
|
|
2018-12-15 16:56:35 +00:00
|
|
|
/// flush network traffic
|
|
|
|
void
|
|
|
|
Flush();
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
void
|
2019-05-07 17:46:38 +00:00
|
|
|
ResetInternalState() override;
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
protected:
|
2020-04-07 18:38:56 +00:00
|
|
|
using PacketQueue_t = llarp::util::CoDelQueue<
|
|
|
|
net::IPPacket,
|
|
|
|
net::IPPacket::GetTime,
|
|
|
|
net::IPPacket::PutTime,
|
|
|
|
net::IPPacket::CompareOrder,
|
|
|
|
net::IPPacket::GetNow>;
|
2019-11-25 13:18:24 +00:00
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
/// queue for sending packets over the network from us
|
|
|
|
PacketQueue_t m_UserToNetworkPktQueue;
|
2020-10-08 11:59:01 +00:00
|
|
|
|
|
|
|
struct WritePacket
|
|
|
|
{
|
|
|
|
uint64_t seqno;
|
|
|
|
net::IPPacket pkt;
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator<(const WritePacket& other) const
|
|
|
|
{
|
|
|
|
return other.seqno < seqno;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
/// queue for sending packets to user from network
|
2020-10-08 11:59:01 +00:00
|
|
|
std::priority_queue<WritePacket> m_NetworkToUserPktQueue;
|
2018-08-17 19:49:58 +00:00
|
|
|
/// return true if we have a remote loki address for this ip address
|
|
|
|
bool
|
2019-06-11 16:44:05 +00:00
|
|
|
HasRemoteForIP(huint128_t ipv4) const;
|
2018-10-19 14:53:06 +00:00
|
|
|
|
2018-08-21 18:17:16 +00:00
|
|
|
/// mark this address as active
|
|
|
|
void
|
2019-06-11 16:44:05 +00:00
|
|
|
MarkIPActive(huint128_t ip);
|
2018-08-21 18:17:16 +00:00
|
|
|
|
2018-09-10 11:08:09 +00:00
|
|
|
/// mark this address as active forever
|
|
|
|
void
|
2019-06-11 16:44:05 +00:00
|
|
|
MarkIPActiveForever(huint128_t ip);
|
2018-09-10 11:08:09 +00:00
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
/// flush ip packets
|
|
|
|
virtual void
|
2018-08-22 15:52:10 +00:00
|
|
|
FlushSend();
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
/// maps ip to key (host byte order)
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unordered_map<huint128_t, AlignedBuffer<32>> m_IPToAddr;
|
2018-11-14 12:23:08 +00:00
|
|
|
/// maps key to ip (host byte order)
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unordered_map<AlignedBuffer<32>, huint128_t, AlignedBuffer<32>::Hash> m_AddrToIP;
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
/// maps key to true if key is a service node, maps key to false if key is
|
|
|
|
/// a hidden service
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unordered_map<AlignedBuffer<32>, bool, AlignedBuffer<32>::Hash> m_SNodes;
|
2018-11-29 13:12:35 +00:00
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
private:
|
2020-04-07 18:38:56 +00:00
|
|
|
template <typename Addr_t, typename Endpoint_t>
|
2018-12-03 22:22:59 +00:00
|
|
|
void
|
2020-04-07 18:38:56 +00:00
|
|
|
SendDNSReply(
|
|
|
|
Addr_t addr,
|
|
|
|
Endpoint_t ctx,
|
|
|
|
std::shared_ptr<dns::Message> query,
|
|
|
|
std::function<void(dns::Message)> reply,
|
|
|
|
bool snode,
|
|
|
|
bool sendIPv6)
|
2019-03-01 19:10:42 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ctx)
|
2019-03-01 19:10:42 +00:00
|
|
|
{
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t ip = ObtainIPForAddr(addr, snode);
|
2020-03-09 20:47:27 +00:00
|
|
|
query->answers.clear();
|
2019-03-27 13:36:11 +00:00
|
|
|
query->AddINReply(ip, sendIPv6);
|
2019-03-01 19:10:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
query->AddNXReply();
|
|
|
|
reply(*query);
|
|
|
|
}
|
2018-12-03 22:22:59 +00:00
|
|
|
/// our dns resolver
|
2021-03-02 18:18:22 +00:00
|
|
|
std::shared_ptr<dns::PacketHandler> m_Resolver;
|
2018-09-22 10:25:16 +00:00
|
|
|
|
2018-08-21 18:17:16 +00:00
|
|
|
/// maps ip address to timestamp last active
|
2020-04-07 18:38:56 +00:00
|
|
|
std::unordered_map<huint128_t, llarp_time_t> m_IPActivity;
|
2018-09-23 16:47:18 +00:00
|
|
|
/// our ip address (host byte order)
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t m_OurIP;
|
2021-02-17 12:54:18 +00:00
|
|
|
/// our network interface's ipv6 address
|
|
|
|
huint128_t m_OurIPv6;
|
2021-02-16 15:59:18 +00:00
|
|
|
|
2018-09-23 16:47:18 +00:00
|
|
|
/// next ip address to allocate (host byte order)
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t m_NextIP;
|
2018-10-10 15:14:45 +00:00
|
|
|
/// highest ip address to allocate (host byte order)
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t m_MaxIP;
|
2018-12-03 22:22:59 +00:00
|
|
|
/// our ip range we are using
|
|
|
|
llarp::IPRange m_OurRange;
|
|
|
|
/// upstream dns resolver list
|
2020-05-06 20:38:44 +00:00
|
|
|
std::vector<IpAddress> m_UpstreamResolvers;
|
2018-11-11 13:14:19 +00:00
|
|
|
/// local dns
|
2020-05-06 20:38:44 +00:00
|
|
|
IpAddress m_LocalResolverAddr;
|
2019-05-06 12:42:21 +00:00
|
|
|
/// list of strict connect addresses for hooks
|
2020-05-06 20:38:44 +00:00
|
|
|
std::vector<IpAddress> m_StrictConnectAddrs;
|
2019-06-11 16:44:05 +00:00
|
|
|
/// use v6?
|
|
|
|
bool m_UseV6;
|
2020-08-21 15:07:37 +00:00
|
|
|
std::string m_IfName;
|
2019-10-04 18:10:58 +00:00
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
std::shared_ptr<vpn::NetworkInterface> m_NetIf;
|
2021-03-02 18:18:22 +00:00
|
|
|
|
|
|
|
std::unique_ptr<vpn::PacketRouter> m_PacketRouter;
|
2018-08-08 19:37:33 +00:00
|
|
|
};
|
2019-11-29 00:37:58 +00:00
|
|
|
|
2018-08-08 19:37:33 +00:00
|
|
|
} // namespace handlers
|
|
|
|
} // namespace llarp
|