fix(direct): refuse UDP IPv6 direct connects that route through the overlay (#2855)

A public IPv6 client without native IPv6 has its only IPv6 default route
on the EasyTier TUN. The direct connector still dialed the provider's
native IPv6 UDP listener, so the underlay packets entered the TUN, were
forwarded to the provider over that same connection, and looped.

resolve_remote_addr already probes the local source address and rejects
targets that would use an EasyTier-managed IPv6, but connect_public_ipv6
resolved the literal URL directly and skipped it. Run the same check
there, before binding the socket or asking the peer to hole punch.


Claude-Session: https://claude.ai/code/session_018a4dgf9j7JT8Dd7HdqkKCX

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: KKRainbow <5665404+KKRainbow@users.noreply.github.com>
This commit is contained in:
Isshin-O
2026-11-19 00:33:09 +08:00
committed by GitHub
co-authored by Claude Opus 5.5 KKRainbow
parent 5fd772b97a
commit dad0cf466d
+113 -2
View File
@@ -804,6 +804,21 @@ where
dst_peer_id: PeerId,
url: &Url,
) -> anyhow::Result<(PeerId, PeerConnId)> {
let default_port = mapped_listener_port(url)
.ok_or_else(|| anyhow::anyhow!("listener has no port: {url}"))?;
let remote_addr = resolve_remote_addr(
self.peer_manager.as_ref(),
self.host.as_ref(),
self.dns.as_ref(),
url,
default_port,
self.options
.udp_bind
.context
.clone()
.with_ip_version(IpVersion::V6),
)
.await?;
let socket = self
.host
.bind_udp(
@@ -833,7 +848,6 @@ where
.remote_send_udp_hole_punch_packet(dst_peer_id, connector_addrs, preferred_src, url)
.await;
}
let remote_addr = resolve_literal_url(url, IpVersion::V6)?;
let connected = udp::connect_with_socket(self.host.clone(), socket, remote_addr).await?;
let tunnel = self
.protocol
@@ -1275,8 +1289,63 @@ fn is_public_ipv4(ip: Ipv4Addr) -> bool {
mod tests {
mod scheduling;
use std::sync::atomic::Ordering;
use super::*;
use crate::connectivity::NoLocalListeners;
use crate::{
config::{IpPrefix, peers::PeerRuntimeSnapshot},
connectivity::NoLocalListeners,
host::{
packet::host_packet_channel,
testkit::{TestDns, TestHost, TestTcpSocket, TestUdpSocket},
},
peers::peer_manager::PortablePeerManagerConfig,
proto::common::StunInfo,
};
struct UnusedStunAndProtocol;
#[async_trait]
impl StunInfoProvider for UnusedStunAndProtocol {
fn get_stun_info(&self) -> StunInfo {
StunInfo::default()
}
async fn get_udp_port_mapping(&self, _local_port: u16) -> anyhow::Result<SocketAddr> {
anyhow::bail!("unused")
}
async fn get_tcp_port_mapping(&self, _local_port: u16) -> anyhow::Result<SocketAddr> {
anyhow::bail!("unused")
}
fn update_stun_info(&self) {}
}
#[async_trait]
impl StunSocketMapper<TestUdpSocket> for UnusedStunAndProtocol {
async fn get_udp_port_mapping_with_socket(
&self,
_socket: Arc<TestUdpSocket>,
) -> anyhow::Result<SocketAddr> {
anyhow::bail!("unused")
}
}
#[async_trait]
impl ClientProtocolUpgrader<TestTcpSocket> for UnusedStunAndProtocol {
fn supports_scheme(&self, _scheme: &str) -> bool {
true
}
async fn upgrade_client(
&self,
_connected: ConnectedTransport<TestTcpSocket>,
_requested_url: Url,
) -> anyhow::Result<Box<dyn Tunnel>> {
anyhow::bail!("unused")
}
}
impl<H> DirectConnectorRpcHandler<H>
where
@@ -1329,6 +1398,48 @@ mod tests {
assert_eq!(addrs[0], SocketAddr::from(([127, 0, 0, 1], 1)));
}
#[tokio::test]
async fn public_ipv6_udp_refuses_easytier_managed_local_source() {
// TestHost reports [::1] as the local source for every IPv6 remote.
let mut runtime = PeerRuntimeSnapshot::default().runtime;
runtime.core.routes.ipv6 = Some(IpPrefix::new("::1".parse().unwrap(), 128).unwrap());
let (packet_tx, _packet_rx) = host_packet_channel();
let peer_manager = Arc::new(
PeerManagerCore::new_portable_for_test(
PortablePeerManagerConfig::new(runtime),
packet_tx,
)
.unwrap(),
);
let host = Arc::new(TestHost::default());
let unused = Arc::new(UnusedStunAndProtocol);
let data = DirectConnectorData {
peer_manager,
host: host.clone(),
protected_tcp_ports: Arc::default(),
stun: unused.clone(),
running_listeners: Arc::new(NoLocalListeners),
dns: Arc::new(TestDns),
protocol: unused,
options: DirectConnectorOptions::default(),
listener_blacklist: ExpiringSet::default(),
peer_blacklist: ExpiringSet::default(),
};
let error = data
.connect_public_ipv6(2, &"udp://[2001:db8::2]:11010".parse().unwrap())
.await
.unwrap_err();
assert!(
error
.to_string()
.contains("refusing overlay-backed underlay connection"),
"{error:#}"
);
assert_eq!(host.udp_binds.load(Ordering::Relaxed), 0);
}
#[test]
fn expiring_set_removes_expired_entries() {
let set = ExpiringSet::default();