mirror of https://github.com/oxen-io/lokinet
introduce add/del route via interface to route manager
This commit is contained in:
parent
64cd2990bc
commit
e25ae7192f
|
@ -83,6 +83,12 @@ namespace llarp::vpn
|
|||
virtual void
|
||||
DelDefaultRouteViaInterface(std::string ifname) = 0;
|
||||
|
||||
virtual void
|
||||
AddRouteViaInterface(std::string ifname, IPRange range) = 0;
|
||||
|
||||
virtual void
|
||||
DelRouteViaInterface(std::string ifname, IPRange range) = 0;
|
||||
|
||||
virtual std::vector<IPVariant_t>
|
||||
GetGatewaysNotOnInterface(std::string ifname) = 0;
|
||||
|
||||
|
|
|
@ -178,14 +178,14 @@ namespace llarp::vpn
|
|||
unsigned char bitlen;
|
||||
unsigned char data[sizeof(struct in6_addr)];
|
||||
|
||||
_inet_addr(huint32_t addr, int bits = 32)
|
||||
_inet_addr(huint32_t addr, size_t bits = 32)
|
||||
{
|
||||
family = AF_INET;
|
||||
bitlen = bits;
|
||||
htobe32buf(data, addr.h);
|
||||
}
|
||||
|
||||
_inet_addr(huint128_t addr, int bits = 128)
|
||||
_inet_addr(huint128_t addr, size_t bits = 128)
|
||||
{
|
||||
family = AF_INET6;
|
||||
bitlen = bits;
|
||||
|
@ -313,6 +313,34 @@ namespace llarp::vpn
|
|||
}
|
||||
}
|
||||
|
||||
void
|
||||
RouteViaInterface(int cmd, int flags, std::string ifname, IPRange range)
|
||||
{
|
||||
int if_idx = if_nametoindex(ifname.c_str());
|
||||
if (range.IsV4())
|
||||
{
|
||||
const auto maybe = GetInterfaceAddr(ifname);
|
||||
if (not maybe)
|
||||
throw std::runtime_error{"we dont have our own network interface?"};
|
||||
|
||||
const _inet_addr gateway{maybe->asIPv4()};
|
||||
|
||||
const _inet_addr addr{
|
||||
net::TruncateV6(range.addr), bits::count_bits(net::TruncateV6(range.netmask_bits))};
|
||||
|
||||
Route(cmd, flags, addr, gateway, GatewayMode::eUpperDefault, if_idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto maybe = GetInterfaceIPv6Address(ifname);
|
||||
if (not maybe)
|
||||
throw std::runtime_error{"we dont have our own network interface?"};
|
||||
const _inet_addr gateway{*maybe, 128};
|
||||
const _inet_addr addr{range.addr, bits::count_bits(range.netmask_bits)};
|
||||
Route(cmd, flags, addr, gateway, GatewayMode::eUpperDefault, if_idx);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Route(int cmd, int flags, IPVariant_t ip, IPVariant_t gateway)
|
||||
{
|
||||
|
@ -366,6 +394,18 @@ namespace llarp::vpn
|
|||
DefaultRouteViaInterface(ifname, RTM_DELROUTE, 0);
|
||||
}
|
||||
|
||||
void
|
||||
AddRouteViaInterface(std::string ifname, IPRange range) override
|
||||
{
|
||||
RouteViaInterface(RTM_NEWROUTE, NLM_F_CREATE | NLM_F_EXCL, ifname, range);
|
||||
}
|
||||
|
||||
void
|
||||
DelRouteViaInterface(std::string ifname, IPRange range) override
|
||||
{
|
||||
RouteViaInterface(RTM_DELROUTE, 0, ifname, range);
|
||||
}
|
||||
|
||||
std::vector<IPVariant_t>
|
||||
GetGatewaysNotOnInterface(std::string ifname) override
|
||||
{
|
||||
|
|
|
@ -544,6 +544,25 @@ namespace llarp::vpn
|
|||
Execute(RouteCommand() + " " + cmd + " 128.0.0.0 MASK 128.0.0.0 " + ifname);
|
||||
}
|
||||
|
||||
void
|
||||
RouteViaInterface(std::string ifname, IPRange range, std::string cmd)
|
||||
{
|
||||
if (range.IsV4())
|
||||
{
|
||||
const huint32_t addr4 = net::TruncateV6(range.addr);
|
||||
const huint32_t mask4 = net::TruncateV6(range.netmask_bits);
|
||||
Execute(
|
||||
RouteCommand() + " " + cmd + " " + addr4.ToString() + " MASK " + mask4.ToString() + " "
|
||||
+ ifname);
|
||||
}
|
||||
else
|
||||
{
|
||||
Execute(
|
||||
RouteCommand() + " " + cmd + range.addr.ToString() + " MASK "
|
||||
+ range.netmask_bits.ToString() + " " + ifname);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void
|
||||
AddRoute(IPVariant_t ip, IPVariant_t gateway) override
|
||||
|
@ -557,6 +576,18 @@ namespace llarp::vpn
|
|||
Route(ip, gateway, "DELETE");
|
||||
}
|
||||
|
||||
void
|
||||
AddRouteViaInterface(std::string ifname, IPRange range) override
|
||||
{
|
||||
RouteViaInterface(ifname, range, "ADD");
|
||||
}
|
||||
|
||||
void
|
||||
DelRouteViaInterface(std::string ifname, IPRange range) override
|
||||
{
|
||||
RouteViaInterface(ifname, range, "DELETE");
|
||||
}
|
||||
|
||||
void
|
||||
AddBlackhole() override{};
|
||||
|
||||
|
|
Loading…
Reference in New Issue