2021-03-09 22:24:35 +00:00
|
|
|
#include "route_poker.hpp"
|
|
|
|
#include "abstractrouter.hpp"
|
2021-04-28 19:48:10 +00:00
|
|
|
#include "net/sock_addr.hpp"
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/service/context.hpp>
|
2022-06-22 16:14:33 +00:00
|
|
|
#include <llarp/dns/platform.hpp>
|
2020-09-24 00:28:38 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
auto logcat = log::Cat("route-poker");
|
|
|
|
}
|
|
|
|
|
2020-09-24 00:28:38 +00:00
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
RoutePoker::AddRoute(net::ipv4addr_t ip)
|
2020-09-24 00:28:38 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
bool has_existing = m_PokedRoutes.count(ip);
|
|
|
|
// set up route and apply as needed
|
|
|
|
auto& gw = m_PokedRoutes[ip];
|
|
|
|
if (m_CurrentGateway)
|
2020-10-20 09:15:39 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
// remove existing mapping as needed
|
|
|
|
if (has_existing)
|
|
|
|
DisableRoute(ip, gw);
|
|
|
|
// update and add new mapping
|
|
|
|
gw = *m_CurrentGateway;
|
2022-09-14 19:08:59 +00:00
|
|
|
log::info(logcat, "add route {} via {}", ip, gw);
|
2022-07-28 16:07:38 +00:00
|
|
|
EnableRoute(ip, gw);
|
2020-10-20 09:15:39 +00:00
|
|
|
}
|
|
|
|
else
|
2022-07-28 16:07:38 +00:00
|
|
|
gw = net::ipv4addr_t{};
|
2020-10-20 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
RoutePoker::DisableRoute(net::ipv4addr_t ip, net::ipv4addr_t gateway)
|
2020-10-20 09:15:39 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
if (ip.n and gateway.n and IsEnabled())
|
|
|
|
{
|
|
|
|
vpn::IRouteManager& route = m_Router->GetVPNPlatform()->RouteManager();
|
|
|
|
route.DelRoute(ip, gateway);
|
|
|
|
}
|
2020-10-20 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
RoutePoker::EnableRoute(net::ipv4addr_t ip, net::ipv4addr_t gateway)
|
2020-10-20 09:15:39 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
if (ip.n and gateway.n and IsEnabled())
|
|
|
|
{
|
|
|
|
vpn::IRouteManager& route = m_Router->GetVPNPlatform()->RouteManager();
|
|
|
|
route.AddRoute(ip, gateway);
|
|
|
|
}
|
2020-09-24 00:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
RoutePoker::DelRoute(net::ipv4addr_t ip)
|
2020-09-24 00:28:38 +00:00
|
|
|
{
|
|
|
|
const auto itr = m_PokedRoutes.find(ip);
|
|
|
|
if (itr == m_PokedRoutes.end())
|
|
|
|
return;
|
2022-09-14 19:08:59 +00:00
|
|
|
log::info(logcat, "del route {} via {}", itr->first, itr->second);
|
2022-07-28 16:07:38 +00:00
|
|
|
DisableRoute(itr->first, itr->second);
|
2021-02-18 23:51:43 +00:00
|
|
|
m_PokedRoutes.erase(itr);
|
2020-09-24 00:28:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 09:15:39 +00:00
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
RoutePoker::Start(AbstractRouter* router)
|
2020-10-20 09:15:39 +00:00
|
|
|
{
|
|
|
|
m_Router = router;
|
2022-07-28 16:07:38 +00:00
|
|
|
if (m_Router->IsServiceNode())
|
|
|
|
return;
|
|
|
|
|
2022-09-14 19:08:59 +00:00
|
|
|
m_Router->loop()->call_every(100ms, weak_from_this(), [self = weak_from_this()]() {
|
|
|
|
if (auto ptr = self.lock())
|
|
|
|
ptr->Update();
|
|
|
|
});
|
2020-10-20 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoutePoker::DeleteAllRoutes()
|
2020-09-24 00:28:38 +00:00
|
|
|
{
|
2020-10-20 09:15:39 +00:00
|
|
|
// DelRoute will check enabled, so no need here
|
2022-07-28 16:07:38 +00:00
|
|
|
for (const auto& item : m_PokedRoutes)
|
|
|
|
DelRoute(item.first);
|
2020-10-20 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RoutePoker::DisableAllRoutes()
|
|
|
|
{
|
|
|
|
for (const auto& [ip, gateway] : m_PokedRoutes)
|
2022-07-28 16:07:38 +00:00
|
|
|
{
|
2020-10-20 09:15:39 +00:00
|
|
|
DisableRoute(ip, gateway);
|
2022-07-28 16:07:38 +00:00
|
|
|
}
|
2020-10-20 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
RoutePoker::RefreshAllRoutes()
|
2020-10-20 09:15:39 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
for (const auto& item : m_PokedRoutes)
|
|
|
|
AddRoute(item.first);
|
2020-10-20 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RoutePoker::~RoutePoker()
|
|
|
|
{
|
2022-01-11 17:44:05 +00:00
|
|
|
if (not m_Router or not m_Router->GetVPNPlatform())
|
2022-01-03 15:16:08 +00:00
|
|
|
return;
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
auto& route = m_Router->GetVPNPlatform()->RouteManager();
|
2020-10-29 14:19:45 +00:00
|
|
|
for (const auto& [ip, gateway] : m_PokedRoutes)
|
2021-01-11 23:13:22 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
if (gateway.n and ip.n)
|
2021-08-27 14:42:04 +00:00
|
|
|
route.DelRoute(ip, gateway);
|
2021-01-11 23:13:22 +00:00
|
|
|
}
|
2021-08-27 14:42:04 +00:00
|
|
|
route.DelBlackhole();
|
2020-09-24 00:28:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
bool
|
|
|
|
RoutePoker::IsEnabled() const
|
2020-09-24 00:28:38 +00:00
|
|
|
{
|
2020-10-20 09:15:39 +00:00
|
|
|
if (not m_Router)
|
2022-07-28 16:07:38 +00:00
|
|
|
throw std::runtime_error{"Attempting to use RoutePoker before calling Init"};
|
|
|
|
if (m_Router->IsServiceNode())
|
|
|
|
return false;
|
|
|
|
if (const auto& conf = m_Router->GetConfig())
|
|
|
|
return conf->network.m_EnableRoutePoker;
|
2020-10-20 09:15:39 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
throw std::runtime_error{"Attempting to use RoutePoker with router with no config set"};
|
2020-09-24 00:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-10-20 09:15:39 +00:00
|
|
|
RoutePoker::Update()
|
2020-09-24 00:28:38 +00:00
|
|
|
{
|
2020-10-20 09:15:39 +00:00
|
|
|
if (not m_Router)
|
2022-07-28 16:07:38 +00:00
|
|
|
throw std::runtime_error{"Attempting to use RoutePoker before calling Init"};
|
2020-10-20 09:15:39 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
// ensure we have an endpoint
|
|
|
|
auto ep = m_Router->hiddenServiceContext().GetDefault();
|
|
|
|
if (ep == nullptr)
|
|
|
|
return;
|
|
|
|
// ensure we have a vpn platform
|
|
|
|
auto* platform = m_Router->GetVPNPlatform();
|
|
|
|
if (platform == nullptr)
|
|
|
|
return;
|
|
|
|
// ensure we have a vpn interface
|
|
|
|
auto* vpn = ep->GetVPNInterface();
|
|
|
|
if (vpn == nullptr)
|
2020-09-24 00:28:38 +00:00
|
|
|
return;
|
2022-07-28 16:07:38 +00:00
|
|
|
|
|
|
|
auto& route = platform->RouteManager();
|
|
|
|
|
|
|
|
// find current gateways
|
|
|
|
auto gateways = route.GetGatewaysNotOnInterface(*vpn);
|
|
|
|
std::optional<net::ipv4addr_t> next_gw;
|
|
|
|
for (auto& gateway : gateways)
|
|
|
|
{
|
|
|
|
if (auto* gw_ptr = std::get_if<net::ipv4addr_t>(&gateway))
|
|
|
|
next_gw = *gw_ptr;
|
2020-09-24 00:28:38 +00:00
|
|
|
}
|
2021-02-08 12:44:01 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
auto is_equal = [](auto lhs, auto rhs) {
|
|
|
|
if (lhs == std::nullopt and rhs == std::nullopt)
|
|
|
|
return true;
|
|
|
|
if (lhs and rhs)
|
|
|
|
return *lhs == *rhs;
|
|
|
|
return false;
|
|
|
|
};
|
2021-02-08 12:44:01 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
// update current gateway and apply state chnages as needed
|
|
|
|
if (not is_equal(m_CurrentGateway, next_gw))
|
2020-09-24 00:28:38 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
if (next_gw and m_CurrentGateway)
|
2021-02-08 12:44:01 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
log::info(logcat, "default gateway changed from {} to {}", *m_CurrentGateway, *next_gw);
|
|
|
|
m_CurrentGateway = next_gw;
|
|
|
|
m_Router->Thaw();
|
|
|
|
if (m_Router->HasClientExit())
|
|
|
|
Up();
|
|
|
|
else
|
|
|
|
RefreshAllRoutes();
|
|
|
|
}
|
|
|
|
else if (m_CurrentGateway)
|
|
|
|
{
|
|
|
|
log::warning(logcat, "default gateway {} has gone away", *m_CurrentGateway);
|
|
|
|
m_CurrentGateway = next_gw;
|
|
|
|
m_Router->Freeze();
|
|
|
|
}
|
|
|
|
else if (next_gw)
|
|
|
|
{
|
|
|
|
log::info(logcat, "default gateway found at {}", *next_gw);
|
|
|
|
m_CurrentGateway = next_gw;
|
2021-02-08 12:44:01 +00:00
|
|
|
}
|
2020-09-24 00:28:38 +00:00
|
|
|
}
|
2022-09-14 19:08:59 +00:00
|
|
|
else if (m_Router->HasClientExit())
|
|
|
|
Up();
|
2020-09-24 00:28:38 +00:00
|
|
|
}
|
2020-10-20 09:15:39 +00:00
|
|
|
|
2022-04-07 20:44:23 +00:00
|
|
|
void
|
|
|
|
RoutePoker::SetDNSMode(bool exit_mode_on) const
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
auto ep = m_Router->hiddenServiceContext().GetDefault();
|
|
|
|
if (not ep)
|
|
|
|
return;
|
|
|
|
if (auto dns_server = ep->DNS())
|
2022-06-22 16:14:33 +00:00
|
|
|
dns_server->SetDNSMode(exit_mode_on);
|
2022-04-07 20:44:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 09:15:39 +00:00
|
|
|
void
|
2022-07-28 16:07:38 +00:00
|
|
|
RoutePoker::Up()
|
2020-10-20 09:15:39 +00:00
|
|
|
{
|
2022-09-14 19:08:59 +00:00
|
|
|
if (IsEnabled() and m_CurrentGateway and not m_up)
|
2021-12-04 15:08:18 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
vpn::IRouteManager& route = m_Router->GetVPNPlatform()->RouteManager();
|
|
|
|
|
|
|
|
// black hole all routes if enabled
|
|
|
|
if (m_Router->GetConfig()->network.m_BlackholeRoutes)
|
|
|
|
route.AddBlackhole();
|
|
|
|
|
|
|
|
// explicit route pokes for first hops
|
|
|
|
m_Router->ForEachPeer(
|
|
|
|
[this](auto session, auto) { AddRoute(session->GetRemoteEndpoint().getIPv4()); }, false);
|
|
|
|
// add default route
|
|
|
|
const auto ep = m_Router->hiddenServiceContext().GetDefault();
|
|
|
|
if (auto* vpn = ep->GetVPNInterface())
|
|
|
|
route.AddDefaultRouteViaInterface(*vpn);
|
2022-09-14 19:08:59 +00:00
|
|
|
log::info(logcat, "route poker up");
|
2021-12-04 15:08:18 +00:00
|
|
|
}
|
2022-09-14 19:14:09 +00:00
|
|
|
if(not m_up)
|
|
|
|
SetDNSMode(true);
|
|
|
|
m_up = true;
|
2020-10-20 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 15:59:18 +00:00
|
|
|
void
|
|
|
|
RoutePoker::Down()
|
|
|
|
{
|
|
|
|
// unpoke routes for first hops
|
|
|
|
m_Router->ForEachPeer(
|
2022-07-28 16:07:38 +00:00
|
|
|
[this](auto session, auto) { DelRoute(session->GetRemoteEndpoint().getIPv4()); }, false);
|
|
|
|
|
2021-02-16 15:59:18 +00:00
|
|
|
// remove default route
|
2021-08-27 14:42:04 +00:00
|
|
|
|
2022-09-14 19:08:59 +00:00
|
|
|
if (IsEnabled() and m_up)
|
2022-07-28 16:07:38 +00:00
|
|
|
{
|
|
|
|
vpn::IRouteManager& route = m_Router->GetVPNPlatform()->RouteManager();
|
|
|
|
const auto ep = m_Router->hiddenServiceContext().GetDefault();
|
|
|
|
if (auto* vpn = ep->GetVPNInterface())
|
|
|
|
route.DelDefaultRouteViaInterface(*vpn);
|
|
|
|
|
|
|
|
// delete route blackhole
|
|
|
|
route.DelBlackhole();
|
2022-09-14 19:08:59 +00:00
|
|
|
log::info(logcat, "route poker down");
|
2022-07-28 16:07:38 +00:00
|
|
|
}
|
2022-09-14 19:14:09 +00:00
|
|
|
if(m_up)
|
|
|
|
SetDNSMode(false);
|
|
|
|
m_up = false;
|
2021-02-16 15:59:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 00:28:38 +00:00
|
|
|
} // namespace llarp
|