2018-08-08 19:37:33 +00:00
|
|
|
#ifndef LLARP_HANDLERS_TUN_HPP
|
|
|
|
#define LLARP_HANDLERS_TUN_HPP
|
2018-12-12 02:15:08 +00:00
|
|
|
|
|
|
|
#include <dns/server.hpp>
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev.h>
|
2019-01-14 21:46:07 +00:00
|
|
|
#include <net/ip.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <service/endpoint.hpp>
|
2019-01-13 22:39:10 +00:00
|
|
|
#include <util/codel.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/threading.hpp>
|
2018-08-08 19:37:33 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace handlers
|
|
|
|
{
|
2018-08-17 19:49:58 +00:00
|
|
|
static const int DefaultTunNetmask = 16;
|
|
|
|
static const char DefaultTunIfname[] = "lokinet0";
|
|
|
|
static const char DefaultTunDstAddr[] = "10.10.0.1";
|
|
|
|
static const char DefaultTunSrcAddr[] = "10.10.0.2";
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
struct TunEndpoint : public service::Endpoint, public dns::IQueryHandler
|
2018-08-08 19:37:33 +00:00
|
|
|
{
|
2019-01-28 15:26:35 +00:00
|
|
|
TunEndpoint(const std::string& nickname, llarp::Router* r,
|
|
|
|
llarp::service::Context* parent);
|
2018-08-16 14:34:15 +00:00
|
|
|
~TunEndpoint();
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
virtual bool
|
2018-12-04 23:45:08 +00:00
|
|
|
SetOption(const std::string& k, const std::string& v) override;
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
virtual void
|
2018-12-04 23:45:08 +00:00
|
|
|
Tick(llarp_time_t now) override;
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
bool
|
|
|
|
ShouldHookDNSMessage(const dns::Message& msg) const override;
|
|
|
|
|
|
|
|
bool
|
|
|
|
HandleHookedDNSMessage(
|
2019-01-07 22:15:31 +00:00
|
|
|
dns::Message&& query,
|
2018-12-03 22:22:59 +00:00
|
|
|
std::function< void(dns::Message) > sendreply) override;
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
void
|
|
|
|
TickTun(llarp_time_t now);
|
|
|
|
|
2018-08-22 15:52:10 +00:00
|
|
|
bool
|
2018-11-30 14:14:30 +00:00
|
|
|
MapAddress(const service::Address& remote, huint32_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
|
2018-08-21 18:17:16 +00:00
|
|
|
/// handle inbound traffic
|
2018-09-18 11:08:47 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
HandleWriteIPPacket(const llarp_buffer_t& buf,
|
2018-12-02 18:07:07 +00:00
|
|
|
std::function< huint32_t(void) > getFromIP) override;
|
2018-08-18 14:01:21 +00:00
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
/// queue outbound packet to the world
|
|
|
|
bool
|
|
|
|
QueueOutboundTraffic(llarp::net::IPv4Packet&& pkt);
|
|
|
|
|
2019-01-28 15:26:35 +00:00
|
|
|
/// we have a resolvable ip address
|
|
|
|
bool
|
|
|
|
HasIfAddr() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
/// get the local interface's address
|
|
|
|
huint32_t
|
2019-01-29 22:08:51 +00:00
|
|
|
GetIfAddr() const override;
|
2018-11-14 12:23:08 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
HasLocalIP(const huint32_t& ip) const;
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
llarp_tun_io tunif;
|
2018-12-02 18:07:07 +00:00
|
|
|
std::unique_ptr< llarp_fd_promise > Promise;
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2018-08-21 18:17:16 +00:00
|
|
|
/// called before writing to tun interface
|
2018-08-16 14:34:15 +00:00
|
|
|
static void
|
2018-08-17 19:49:58 +00:00
|
|
|
tunifBeforeWrite(llarp_tun_io* t);
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2018-08-22 15:52:10 +00:00
|
|
|
/// handle user to network send buffer flush
|
|
|
|
/// called in router logic thread
|
|
|
|
static void
|
|
|
|
handleNetSend(void*);
|
|
|
|
|
2018-08-21 18:17:16 +00:00
|
|
|
/// called every time we wish to read a packet from the tun interface
|
2018-08-16 14:34:15 +00:00
|
|
|
static void
|
2019-02-01 01:58:06 +00:00
|
|
|
tunifRecvPkt(llarp_tun_io* t, const llarp_buffer_t& buf);
|
2018-08-16 14:34:15 +00:00
|
|
|
|
2018-08-21 18:17:16 +00:00
|
|
|
/// called in the endpoint logic thread
|
2018-08-16 14:34:15 +00:00
|
|
|
static void
|
|
|
|
handleTickTun(void* u);
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
/// get a key for ip address
|
|
|
|
template < typename Addr >
|
|
|
|
Addr
|
2018-11-29 14:01:13 +00:00
|
|
|
ObtainAddrForIP(huint32_t ip, bool isSNode)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
auto itr = m_IPToAddr.find(ip);
|
2018-11-29 14:01:13 +00:00
|
|
|
if(itr == m_IPToAddr.end() || m_SNodes[itr->second] != isSNode)
|
2018-11-14 12:23:08 +00:00
|
|
|
{
|
|
|
|
// not found
|
|
|
|
Addr addr;
|
|
|
|
addr.Zero();
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
// found
|
2019-01-02 01:04:08 +00:00
|
|
|
return Addr{itr->second};
|
2018-11-14 12:23:08 +00:00
|
|
|
}
|
2018-10-19 14:53:06 +00:00
|
|
|
|
2018-10-23 18:18:00 +00:00
|
|
|
bool
|
2019-01-02 01:04:04 +00:00
|
|
|
HasAddress(const AlignedBuffer< 32 >& addr) const override
|
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
|
2018-10-23 21:33:49 +00:00
|
|
|
huint32_t
|
2019-01-02 01:03:53 +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();
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
protected:
|
2018-11-22 23:59:03 +00:00
|
|
|
using PacketQueue_t = llarp::util::CoDelQueue<
|
2018-08-17 19:49:58 +00:00
|
|
|
net::IPv4Packet, net::IPv4Packet::GetTime, net::IPv4Packet::PutTime,
|
2018-11-22 23:59:03 +00:00
|
|
|
net::IPv4Packet::CompareOrder, net::IPv4Packet::GetNow >;
|
2018-08-17 19:49:58 +00:00
|
|
|
/// queue for sending packets over the network from us
|
|
|
|
PacketQueue_t m_UserToNetworkPktQueue;
|
|
|
|
/// queue for sending packets to user from network
|
|
|
|
PacketQueue_t m_NetworkToUserPktQueue;
|
|
|
|
/// return true if we have a remote loki address for this ip address
|
|
|
|
bool
|
2018-10-10 15:14:45 +00:00
|
|
|
HasRemoteForIP(huint32_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
|
2018-10-10 15:14:45 +00:00
|
|
|
MarkIPActive(huint32_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
|
2018-10-10 15:14:45 +00:00
|
|
|
MarkIPActiveForever(huint32_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)
|
|
|
|
std::unordered_map< huint32_t, AlignedBuffer< 32 >, huint32_t::Hash >
|
|
|
|
m_IPToAddr;
|
|
|
|
/// maps key to ip (host byte order)
|
|
|
|
std::unordered_map< AlignedBuffer< 32 >, huint32_t,
|
|
|
|
AlignedBuffer< 32 >::Hash >
|
|
|
|
m_AddrToIP;
|
|
|
|
|
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
|
|
|
|
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:
|
2018-11-14 19:34:17 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
QueueInboundPacketForExit(const llarp_buffer_t& buf)
|
2018-11-14 19:34:17 +00:00
|
|
|
{
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer copy{buf};
|
2019-02-01 01:58:06 +00:00
|
|
|
|
2018-11-14 19:34:17 +00:00
|
|
|
return m_NetworkToUserPktQueue.EmplaceIf(
|
|
|
|
[&](llarp::net::IPv4Packet& pkt) -> bool {
|
2019-02-02 23:12:42 +00:00
|
|
|
if(!pkt.Load(copy.underlying))
|
2018-11-14 19:34:17 +00:00
|
|
|
return false;
|
|
|
|
pkt.UpdateIPv4PacketOnDst(pkt.src(), m_OurIP);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
void
|
|
|
|
SendDNSReply(service::Address addr,
|
2019-01-07 22:15:31 +00:00
|
|
|
service::Endpoint::OutboundContext* ctx,
|
|
|
|
dns::Message&& query,
|
2018-12-03 22:22:59 +00:00
|
|
|
std::function< void(dns::Message) > reply);
|
|
|
|
|
2018-10-19 16:54:08 +00:00
|
|
|
#ifndef WIN32
|
2018-12-03 14:39:30 +00:00
|
|
|
/// handles fd injection force android
|
2018-12-02 18:07:07 +00:00
|
|
|
std::promise< int > m_VPNPromise;
|
2018-12-03 14:39:30 +00:00
|
|
|
#endif
|
2018-12-03 22:22:59 +00:00
|
|
|
|
|
|
|
/// our dns resolver
|
|
|
|
dns::Proxy 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
|
2018-10-10 15:50:52 +00:00
|
|
|
std::unordered_map< huint32_t, llarp_time_t, huint32_t::Hash >
|
|
|
|
m_IPActivity;
|
2018-09-23 16:47:18 +00:00
|
|
|
/// our ip address (host byte order)
|
2018-10-10 15:14:45 +00:00
|
|
|
huint32_t m_OurIP;
|
2018-09-23 16:47:18 +00:00
|
|
|
/// next ip address to allocate (host byte order)
|
2018-10-10 15:14:45 +00:00
|
|
|
huint32_t m_NextIP;
|
|
|
|
/// highest ip address to allocate (host byte order)
|
|
|
|
huint32_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
|
|
|
|
std::vector< llarp::Addr > m_UpstreamResolvers;
|
2018-11-11 13:14:19 +00:00
|
|
|
/// local dns
|
|
|
|
llarp::Addr m_LocalResolverAddr;
|
2018-08-08 19:37:33 +00:00
|
|
|
};
|
|
|
|
} // namespace handlers
|
|
|
|
} // namespace llarp
|
|
|
|
|
2018-08-16 14:34:15 +00:00
|
|
|
#endif
|