2021-01-11 23:13:22 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/net/ip_range.hpp>
|
|
|
|
#include <llarp/net/ip_packet.hpp>
|
2022-02-17 18:44:31 +00:00
|
|
|
#include <oxenc/variant.h>
|
2021-08-27 14:42:04 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
#include "i_packet_io.hpp"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct Context;
|
2021-12-07 16:17:20 +00:00
|
|
|
struct AbstractRouter;
|
|
|
|
} // namespace llarp
|
2021-01-11 23:13:22 +00:00
|
|
|
|
|
|
|
namespace llarp::vpn
|
|
|
|
{
|
|
|
|
struct InterfaceAddress
|
|
|
|
{
|
|
|
|
constexpr InterfaceAddress(IPRange r, int f = AF_INET) : range{std::move(r)}, fam{f}
|
|
|
|
{}
|
|
|
|
IPRange range;
|
|
|
|
int fam;
|
|
|
|
bool
|
|
|
|
operator<(const InterfaceAddress& other) const
|
|
|
|
{
|
2022-10-13 17:19:25 +00:00
|
|
|
return std::tie(range, fam) < std::tie(other.range, other.fam);
|
2021-01-11 23:13:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct InterfaceInfo
|
|
|
|
{
|
|
|
|
std::string ifname;
|
2022-07-28 16:07:38 +00:00
|
|
|
unsigned int index;
|
2021-01-11 23:13:22 +00:00
|
|
|
huint32_t dnsaddr;
|
2022-07-28 16:07:38 +00:00
|
|
|
std::vector<InterfaceAddress> addrs;
|
|
|
|
|
|
|
|
/// get address number N
|
|
|
|
inline net::ipaddr_t
|
|
|
|
operator[](size_t idx) const
|
|
|
|
{
|
|
|
|
const auto& range = addrs[idx].range;
|
|
|
|
if (range.IsV4())
|
|
|
|
return ToNet(net::TruncateV6(range.addr));
|
|
|
|
return ToNet(range.addr);
|
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// a vpn network interface
|
2022-07-28 16:07:38 +00:00
|
|
|
class NetworkInterface : public I_Packet_IO
|
2021-01-11 23:13:22 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
protected:
|
|
|
|
InterfaceInfo m_Info;
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
public:
|
2022-07-28 16:07:38 +00:00
|
|
|
NetworkInterface(InterfaceInfo info) : m_Info{std::move(info)}
|
|
|
|
{}
|
2021-01-11 23:13:22 +00:00
|
|
|
NetworkInterface(const NetworkInterface&) = delete;
|
|
|
|
NetworkInterface(NetworkInterface&&) = delete;
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
const InterfaceInfo&
|
|
|
|
Info() const
|
|
|
|
{
|
|
|
|
return m_Info;
|
|
|
|
}
|
2021-12-07 16:17:20 +00:00
|
|
|
|
|
|
|
/// idempotently wake up the upper layers as needed (platform dependant)
|
|
|
|
virtual void
|
|
|
|
MaybeWakeUpperLayers() const {};
|
2021-01-11 23:13:22 +00:00
|
|
|
};
|
|
|
|
|
2021-08-27 14:42:04 +00:00
|
|
|
class IRouteManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IRouteManager() = default;
|
|
|
|
IRouteManager(const IRouteManager&) = delete;
|
|
|
|
IRouteManager(IRouteManager&&) = delete;
|
|
|
|
virtual ~IRouteManager() = default;
|
|
|
|
|
2022-07-09 15:05:52 +00:00
|
|
|
virtual const llarp::net::Platform*
|
|
|
|
Net_ptr() const;
|
|
|
|
|
|
|
|
inline const llarp::net::Platform&
|
|
|
|
Net() const
|
|
|
|
{
|
|
|
|
return *Net_ptr();
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:42:04 +00:00
|
|
|
virtual void
|
2022-07-28 16:07:38 +00:00
|
|
|
AddRoute(net::ipaddr_t ip, net::ipaddr_t gateway) = 0;
|
2021-08-27 14:42:04 +00:00
|
|
|
|
|
|
|
virtual void
|
2022-07-28 16:07:38 +00:00
|
|
|
DelRoute(net::ipaddr_t ip, net::ipaddr_t gateway) = 0;
|
2021-08-27 14:42:04 +00:00
|
|
|
|
|
|
|
virtual void
|
2022-07-28 16:07:38 +00:00
|
|
|
AddDefaultRouteViaInterface(NetworkInterface& vpn) = 0;
|
2021-08-27 14:42:04 +00:00
|
|
|
|
|
|
|
virtual void
|
2022-07-28 16:07:38 +00:00
|
|
|
DelDefaultRouteViaInterface(NetworkInterface& vpn) = 0;
|
2021-08-27 14:42:04 +00:00
|
|
|
|
2021-08-27 15:40:40 +00:00
|
|
|
virtual void
|
2021-08-27 15:55:57 +00:00
|
|
|
AddRouteViaInterface(NetworkInterface& vpn, IPRange range) = 0;
|
2021-08-27 15:40:40 +00:00
|
|
|
|
|
|
|
virtual void
|
2021-08-27 15:55:57 +00:00
|
|
|
DelRouteViaInterface(NetworkInterface& vpn, IPRange range) = 0;
|
2021-08-27 15:40:40 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
virtual std::vector<net::ipaddr_t>
|
|
|
|
GetGatewaysNotOnInterface(NetworkInterface& vpn) = 0;
|
2021-08-27 14:42:04 +00:00
|
|
|
|
|
|
|
virtual void
|
2021-08-27 15:42:24 +00:00
|
|
|
AddBlackhole(){};
|
2021-08-27 14:42:04 +00:00
|
|
|
|
|
|
|
virtual void
|
2021-08-27 15:42:24 +00:00
|
|
|
DelBlackhole(){};
|
2021-08-27 14:42:04 +00:00
|
|
|
};
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
/// a vpn platform
|
|
|
|
/// responsible for obtaining vpn interfaces
|
|
|
|
class Platform
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
protected:
|
|
|
|
/// get a new network interface fully configured given the interface info
|
|
|
|
/// blocks until ready, throws on error
|
|
|
|
virtual std::shared_ptr<NetworkInterface>
|
|
|
|
ObtainInterface(InterfaceInfo info, AbstractRouter* router) = 0;
|
|
|
|
|
2021-01-11 23:13:22 +00:00
|
|
|
public:
|
|
|
|
Platform() = default;
|
|
|
|
Platform(const Platform&) = delete;
|
|
|
|
Platform(Platform&&) = delete;
|
|
|
|
virtual ~Platform() = default;
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
/// create and start a network interface
|
|
|
|
inline std::shared_ptr<NetworkInterface>
|
|
|
|
CreateInterface(InterfaceInfo info, AbstractRouter* router)
|
|
|
|
{
|
|
|
|
if (auto netif = ObtainInterface(std::move(info), router))
|
|
|
|
{
|
|
|
|
netif->Start();
|
|
|
|
return netif;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-08-27 14:42:04 +00:00
|
|
|
|
|
|
|
/// get owned ip route manager for managing routing table
|
|
|
|
virtual IRouteManager&
|
|
|
|
RouteManager() = 0;
|
2022-07-28 16:07:38 +00:00
|
|
|
|
|
|
|
/// create a packet io that will read (and optionally write) packets on a network interface the
|
|
|
|
/// lokinet process does not own
|
|
|
|
/// @param index the interface index of the network interface to use or 0 for all
|
|
|
|
/// interfaces on the system
|
|
|
|
virtual std::shared_ptr<I_Packet_IO>
|
2022-09-13 20:24:25 +00:00
|
|
|
create_packet_io(
|
|
|
|
[[maybe_unused]] unsigned int ifindex,
|
|
|
|
[[maybe_unused]] const std::optional<SockAddr>& dns_upstream_src)
|
2022-07-28 16:07:38 +00:00
|
|
|
{
|
|
|
|
throw std::runtime_error{"raw packet io is unimplemented"};
|
|
|
|
}
|
2021-01-11 23:13:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// create native vpn platform
|
2021-02-02 14:35:40 +00:00
|
|
|
std::shared_ptr<Platform>
|
2021-01-11 23:13:22 +00:00
|
|
|
MakeNativePlatform(llarp::Context* ctx);
|
|
|
|
|
|
|
|
} // namespace llarp::vpn
|