mirror of
https://github.com/vnt-dev/vnt.git
synced 2026-12-06 08:06:58 +00:00
feat: relay IKEv2 subnet traffic
This commit is contained in:
1 parent
dce306981c
commit
3413dddbe3
4 files changed
+65
-38
No files matched your search
@@ -14,7 +14,9 @@ use crate::fec::{FecDecoder, FecEncoder};
|
||||
use crate::nat::NetInput;
|
||||
use crate::nat::internal_nat::{InternalNatInbound, PortMappingManager};
|
||||
use crate::nat::subnet_packet::SubnetPacketMapper;
|
||||
use crate::nat::{AllowSubnetExternalRoute, SubnetExternalRoute, SubnetMappingTable};
|
||||
use crate::nat::{
|
||||
AllowSubnetExternalRoute, SubnetExternalRoute, SubnetMappingTable, advertised_subnets,
|
||||
};
|
||||
use crate::protocol::control_message::ErrorResponseMsg;
|
||||
use crate::tun::enhanced_tun::EnhancedTunInbound;
|
||||
use crate::tun::{DeviceConfig, DeviceIOManager, TunDataInbound, TunReceiver, tun_channel};
|
||||
@@ -53,6 +55,7 @@ struct RegistrationContext {
|
||||
auto_sync_subnet: bool,
|
||||
allow_ikev2: bool,
|
||||
allow_wireguard: bool,
|
||||
relay_subnets: AllowSubnetExternalRoute,
|
||||
}
|
||||
|
||||
pub struct NetworkManager {
|
||||
@@ -132,6 +135,10 @@ impl NetworkManager {
|
||||
.collect(),
|
||||
);
|
||||
let allow_subnet = AllowSubnetExternalRoute::new(config.output.clone());
|
||||
let relay_subnets = AllowSubnetExternalRoute::new(advertised_subnets(
|
||||
&config.output,
|
||||
&config.subnet_mapping,
|
||||
));
|
||||
|
||||
let p2p_enabled =
|
||||
!config.no_punch || !config.peer_address.is_empty() || !config.turn.is_empty();
|
||||
@@ -197,6 +204,7 @@ impl NetworkManager {
|
||||
subnet_external_route.clone(),
|
||||
subnet_mapping.clone(),
|
||||
subnet_packet_mapper.clone(),
|
||||
relay_subnets.clone(),
|
||||
fec_encoder,
|
||||
)
|
||||
.with_no_broadcast(config.no_broadcast)
|
||||
@@ -300,6 +308,7 @@ impl NetworkManager {
|
||||
auto_sync_subnet: config.auto_sync_subnet,
|
||||
allow_ikev2: config.allow_ikev2,
|
||||
allow_wireguard: config.allow_wireguard,
|
||||
relay_subnets,
|
||||
});
|
||||
|
||||
app_state.set_config(config.clone());
|
||||
@@ -421,6 +430,7 @@ impl NetworkManager {
|
||||
auto_sync_subnet: ctx.auto_sync_subnet,
|
||||
allow_ikev2: ctx.allow_ikev2,
|
||||
allow_wireguard: ctx.allow_wireguard,
|
||||
relay_subnets: ctx.relay_subnets.clone(),
|
||||
});
|
||||
turn_manager.data_handle_task(
|
||||
task_group,
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::context::{NetworkAddr, ServerInfoCollection, SharedNetworkAddr, Traff
|
||||
use crate::crypto::PacketCrypto;
|
||||
use crate::fec::FecEncoder;
|
||||
use crate::nat::subnet_packet::SubnetPacketMapper;
|
||||
use crate::nat::{SubnetExternalRoute, SubnetMappingTable};
|
||||
use crate::nat::{AllowSubnetExternalRoute, SubnetExternalRoute, SubnetMappingTable};
|
||||
use crate::protocol::control_message::ClientType;
|
||||
use crate::protocol::ip_packet_protocol::{HEAD_LENGTH, MsgType, NetPacket};
|
||||
use crate::protocol::transmission::TransmissionBytes;
|
||||
@@ -260,6 +260,7 @@ pub(crate) struct HybridOutbound {
|
||||
external_route: SubnetExternalRoute,
|
||||
subnet_mapping: SubnetMappingTable,
|
||||
subnet_packet_mapper: SubnetPacketMapper,
|
||||
relay_subnets: AllowSubnetExternalRoute,
|
||||
fec_encoder: Option<FecEncoder>,
|
||||
no_broadcast: bool,
|
||||
allow_ikev2: bool,
|
||||
@@ -276,6 +277,7 @@ impl HybridOutbound {
|
||||
external_route: SubnetExternalRoute,
|
||||
subnet_mapping: SubnetMappingTable,
|
||||
subnet_packet_mapper: SubnetPacketMapper,
|
||||
relay_subnets: AllowSubnetExternalRoute,
|
||||
fec_encoder: Option<FecEncoder>,
|
||||
) -> Self {
|
||||
Self {
|
||||
@@ -287,6 +289,7 @@ impl HybridOutbound {
|
||||
external_route,
|
||||
subnet_mapping,
|
||||
subnet_packet_mapper,
|
||||
relay_subnets,
|
||||
fec_encoder,
|
||||
no_broadcast: false,
|
||||
allow_ikev2: false,
|
||||
@@ -334,8 +337,9 @@ impl HybridOutbound {
|
||||
if header_length < Ipv4Packet::minimum_packet_size()
|
||||
|| total_length < header_length
|
||||
|| total_length > data.len()
|
||||
|| ipv4.get_source() != net.ip
|
||||
|| ipv4.get_destination() != dest
|
||||
|| (ipv4.get_source() != net.ip && !self.relay_subnets.allow(&ipv4.get_source()))
|
||||
|| (ipv4.get_destination() != dest
|
||||
&& self.external_route.route(&ipv4.get_destination()) != Some(dest))
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
@@ -417,6 +421,12 @@ impl HybridOutbound {
|
||||
} else {
|
||||
vec![data]
|
||||
};
|
||||
if self.is_relay_client(&dest) {
|
||||
for data in packets {
|
||||
self.server_relay_outbound(net, data, dest).await?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
for data in packets {
|
||||
self.ipv4_outbound_to(net, data, dest).await?;
|
||||
}
|
||||
@@ -487,6 +497,12 @@ impl HybridOutbound {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
if self.is_relay_client(&dest) {
|
||||
let Some(ip) = crate::ethernet::strip_ipv4(data) else {
|
||||
return Ok(());
|
||||
};
|
||||
return self.server_relay_outbound(net, ip, dest).await;
|
||||
}
|
||||
let packets = if dest_is_overlay {
|
||||
let Some(frame) = crate::ethernet::parse_frame(data.as_ref()) else {
|
||||
return Ok(());
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::crypto::PacketCrypto;
|
||||
use crate::enhanced_tunnel::inbound::EnhancedInbound;
|
||||
use crate::event_script::{EventScript, EventScriptType};
|
||||
use crate::fec::FecDecoder;
|
||||
use crate::nat::AllowSubnetExternalRoute;
|
||||
use crate::protocol::control_message::{RegistrationMode, RequestMessage, ResponseMessage};
|
||||
use crate::tunnel_core::p2p::transport::punch::NatPuncher;
|
||||
use crate::tunnel_core::server::inbound::{IpUpdateContext, ServerTurnInboundHandler};
|
||||
@@ -41,6 +42,7 @@ pub struct InboundHandlerConfig {
|
||||
pub auto_sync_subnet: bool,
|
||||
pub allow_ikev2: bool,
|
||||
pub allow_wireguard: bool,
|
||||
pub relay_subnets: AllowSubnetExternalRoute,
|
||||
}
|
||||
|
||||
pub struct ServerTurnManager {
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::event_script::EventScript;
|
||||
#[cfg(not(target_os = "android"))]
|
||||
use crate::event_script::EventScriptType;
|
||||
use crate::fec::FecDecoder;
|
||||
use crate::nat::AllowSubnetExternalRoute;
|
||||
use crate::protocol::client_message::PunchInfo;
|
||||
use crate::protocol::control_message::{
|
||||
ClientSimpleInfoList, FastRegRequestMsg, RequestMessage, ResponseMessage, SubnetSyncResponse,
|
||||
@@ -364,6 +365,7 @@ pub(crate) struct ServerTurnInboundHandler {
|
||||
auto_sync_subnet: bool,
|
||||
allow_ikev2: bool,
|
||||
allow_wireguard: bool,
|
||||
relay_subnets: AllowSubnetExternalRoute,
|
||||
}
|
||||
|
||||
fn valid_server_relay_ipv4(
|
||||
@@ -371,6 +373,8 @@ fn valid_server_relay_ipv4(
|
||||
src: Ipv4Addr,
|
||||
dest_id: Ipv4Addr,
|
||||
local_ip: Ipv4Addr,
|
||||
network_route: &NetworkRoute,
|
||||
relay_subnets: &AllowSubnetExternalRoute,
|
||||
) -> bool {
|
||||
let Some(ipv4) = Ipv4Packet::new(payload) else {
|
||||
return false;
|
||||
@@ -380,8 +384,9 @@ fn valid_server_relay_ipv4(
|
||||
&& header_length >= Ipv4Packet::minimum_packet_size()
|
||||
&& header_length <= payload.len()
|
||||
&& ipv4.get_total_length() as usize == payload.len()
|
||||
&& ipv4.get_source() == src
|
||||
&& ipv4.get_destination() == local_ip
|
||||
&& (ipv4.get_source() == src
|
||||
|| network_route.subnet_route.route(&ipv4.get_source()) == Some(src))
|
||||
&& (ipv4.get_destination() == local_ip || relay_subnets.allow(&ipv4.get_destination()))
|
||||
&& dest_id == local_ip
|
||||
}
|
||||
|
||||
@@ -414,6 +419,7 @@ impl ServerTurnInboundHandler {
|
||||
auto_sync_subnet: config.auto_sync_subnet,
|
||||
allow_ikev2: config.allow_ikev2,
|
||||
allow_wireguard: config.allow_wireguard,
|
||||
relay_subnets: config.relay_subnets,
|
||||
}
|
||||
}
|
||||
fn network_contains(&self, ip: &Ipv4Addr) -> bool {
|
||||
@@ -461,6 +467,8 @@ impl ServerTurnInboundHandler {
|
||||
src,
|
||||
Ipv4Addr::from(net_packet.dest_id()),
|
||||
network_addr.ip,
|
||||
&self.network_route,
|
||||
&self.relay_subnets,
|
||||
) {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -844,51 +852,42 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_relay_ipv4_requires_exact_authenticated_endpoints_and_length() {
|
||||
fn server_relay_ipv4_authorizes_virtual_and_declared_subnet_addresses() {
|
||||
let source = Ipv4Addr::new(10, 26, 0, 9);
|
||||
let local = Ipv4Addr::new(10, 26, 0, 8);
|
||||
let shared_network = SharedNetworkAddr::default();
|
||||
shared_network.set(network(local));
|
||||
let network_route = NetworkRoute::new(
|
||||
shared_network,
|
||||
crate::nat::SubnetExternalRoute::new(vec![
|
||||
"192.168.30.0/24,10.26.0.9".parse().unwrap(),
|
||||
]),
|
||||
);
|
||||
let relay_subnets = AllowSubnetExternalRoute::new(vec!["172.16.0.0/16".parse().unwrap()]);
|
||||
let check = |payload: &[u8], src, dest_id| {
|
||||
valid_server_relay_ipv4(payload, src, dest_id, local, &network_route, &relay_subnets)
|
||||
};
|
||||
let valid = relay_ipv4(source, local);
|
||||
assert!(valid_server_relay_ipv4(&valid, source, local, local));
|
||||
assert!(!valid_server_relay_ipv4(
|
||||
&valid,
|
||||
Ipv4Addr::new(10, 26, 0, 7),
|
||||
local,
|
||||
local
|
||||
));
|
||||
assert!(!valid_server_relay_ipv4(
|
||||
&valid,
|
||||
source,
|
||||
Ipv4Addr::new(10, 26, 0, 7),
|
||||
local
|
||||
));
|
||||
assert!(check(&valid, source, local));
|
||||
assert!(!check(&valid, Ipv4Addr::new(10, 26, 0, 7), local));
|
||||
assert!(!check(&valid, source, Ipv4Addr::new(10, 26, 0, 7)));
|
||||
|
||||
let subnet = relay_ipv4(Ipv4Addr::new(192, 168, 30, 7), Ipv4Addr::new(172, 16, 2, 3));
|
||||
assert!(check(&subnet, source, local));
|
||||
assert!(!check(&subnet, Ipv4Addr::new(10, 26, 0, 7), local));
|
||||
|
||||
let wrong_destination = relay_ipv4(source, Ipv4Addr::new(10, 26, 0, 7));
|
||||
assert!(!valid_server_relay_ipv4(
|
||||
&wrong_destination,
|
||||
source,
|
||||
local,
|
||||
local
|
||||
));
|
||||
assert!(!check(&wrong_destination, source, local));
|
||||
|
||||
let mut wrong_length = valid;
|
||||
wrong_length.push(0);
|
||||
assert!(!valid_server_relay_ipv4(
|
||||
&wrong_length,
|
||||
source,
|
||||
local,
|
||||
local
|
||||
));
|
||||
assert!(!check(&wrong_length, source, local));
|
||||
|
||||
let mut oversized_header = relay_ipv4(source, local);
|
||||
MutableIpv4Packet::new(&mut oversized_header)
|
||||
.unwrap()
|
||||
.set_header_length(15);
|
||||
assert!(!valid_server_relay_ipv4(
|
||||
&oversized_header,
|
||||
source,
|
||||
local,
|
||||
local
|
||||
));
|
||||
assert!(!check(&oversized_header, source, local));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user