From 9c13cf6a4540fb8cd49e33e586b7029b2ca3b6ac Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 3 Dec 2026 13:19:01 +0800 Subject: [PATCH] feat(tun): option to refuse IPv6 (TUN_REJECT_IPV6) The stack completes a TCP handshake before the outbound connection is made, so an IPv6 connection that leaf cannot complete looks successful to the application, which then sees a reset instead of falling back to IPv4. With TUN_REJECT_IPV6=true (read on every TUN start) an IPv6 SYN is answered with RST and IPv6 UDP is dropped, so applications use IPv4 at once while IPv6 still cannot leak past the tunnel. - Limits.reject_ipv6, plumbed into the TCP engine - tests: IPv6 SYN reset when rejected, IPv4 still accepted; accepted by default --- leaf/src/proxy/tun/inbound.rs | 6 ++ leaf/src/proxy/tun/netstack/engine.rs | 5 ++ leaf/src/proxy/tun/netstack/stack.rs | 4 ++ leaf/src/proxy/tun/netstack/tcp/input.rs | 10 ++++ leaf/src/proxy/tun/netstack/tcp/mod.rs | 70 ++++++++++++++++++++++++ leaf/src/proxy/tun/netstack/tcp/stack.rs | 4 ++ 6 files changed, 99 insertions(+) diff --git a/leaf/src/proxy/tun/inbound.rs b/leaf/src/proxy/tun/inbound.rs index 0174024..70e2988 100644 --- a/leaf/src/proxy/tun/inbound.rs +++ b/leaf/src/proxy/tun/inbound.rs @@ -176,6 +176,12 @@ fn new_netstack( if let Some(mtu) = mtu { config.limits.mtu = mtu; } + // Read on every start (not cached), so a host app can change it between runs. + // Refuses IPv6 (RST for TCP, UDP dropped) so applications use IPv4. + config.limits.reject_ipv6 = crate::option::get_env_var_or("TUN_REJECT_IPV6", false); + if config.limits.reject_ipv6 { + tracing::info!("TUN rejects IPv6 (TCP reset, UDP dropped)"); + } let (stack, mut tcp_listener, udp_socket) = netstack::new(config); Ok(Box::pin(async move { diff --git a/leaf/src/proxy/tun/netstack/engine.rs b/leaf/src/proxy/tun/netstack/engine.rs index b3d7e70..2126ea1 100644 --- a/leaf/src/proxy/tun/netstack/engine.rs +++ b/leaf/src/proxy/tun/netstack/engine.rs @@ -66,6 +66,10 @@ pub struct Limits { /// per-connection bounds. Backends that allocate whole buffers up front /// cannot honour it and ignore it. pub memory_budget: Option, + /// Refuse IPv6: a SYN is answered with RST and UDP is dropped, so + /// applications fall back to IPv4 at once instead of being accepted and + /// failing later (the stack completes handshakes before dialing out). + pub reject_ipv6: bool, } impl Default for Limits { @@ -82,6 +86,7 @@ impl Default for Limits { keepalive: Some(Duration::from_secs(60)), mtu: 1500, memory_budget: Some(budget), + reject_ipv6: false, } } } diff --git a/leaf/src/proxy/tun/netstack/stack.rs b/leaf/src/proxy/tun/netstack/stack.rs index 6d29ee1..8c63850 100644 --- a/leaf/src/proxy/tun/netstack/stack.rs +++ b/leaf/src/proxy/tun/netstack/stack.rs @@ -268,6 +268,10 @@ impl Core { } fn input_udp(&mut self, view: &ip::IpView<'_>) { + if self.config.limits.reject_ipv6 && view.src.is_ipv6() { + self.input_ignored += 1; + return; + } let Some((src_port, dst_port, data)) = ip::parse_udp(view) else { self.input_ignored += 1; return; diff --git a/leaf/src/proxy/tun/netstack/tcp/input.rs b/leaf/src/proxy/tun/netstack/tcp/input.rs index e7c84fe..4cee786 100644 --- a/leaf/src/proxy/tun/netstack/tcp/input.rs +++ b/leaf/src/proxy/tun/netstack/tcp/input.rs @@ -148,6 +148,16 @@ impl Stack { if !seg.flags.has(TcpFlags::SYN) { return; } + // IPv6 refused: reset right away so the application falls back to IPv4. + if self.env.reject_ipv6 && seg.remote.is_ipv6() { + self.env.net.send_reset( + seg.local, + seg.remote, + SeqNum::new(0), + seg.seq + u32::from(seg.len), + ); + return; + } // No room for it: the SYN goes unanswered. let Some(key) = self.open() else { return; diff --git a/leaf/src/proxy/tun/netstack/tcp/mod.rs b/leaf/src/proxy/tun/netstack/tcp/mod.rs index 1c58b2d..f96b4ff 100644 --- a/leaf/src/proxy/tun/netstack/tcp/mod.rs +++ b/leaf/src/proxy/tun/netstack/tcp/mod.rs @@ -64,6 +64,7 @@ impl TcpEngine { keepalive_idle_ms, limits.max_connections, limits.memory_budget, + limits.reject_ipv6, ), } } @@ -241,3 +242,72 @@ impl TcpEngine { } } } + +#[cfg(test)] +mod reject_ipv6_tests { + use super::*; + use crate::proxy::tun::netstack::{ip, wire}; + use std::net::IpAddr; + + fn syn(src: IpAddr, dst: IpAddr) -> Vec { + let mut seg = vec![0u8; 20]; + seg[0..2].copy_from_slice(&40000u16.to_be_bytes()); + seg[2..4].copy_from_slice(&443u16.to_be_bytes()); + seg[4..8].copy_from_slice(&1000u32.to_be_bytes()); + seg[12] = 5 << 4; + seg[13] = 0x02; // SYN + seg[14..16].copy_from_slice(&65535u16.to_be_bytes()); + wire::fill_transport_checksum(src, dst, 6, &mut seg, 16); + ip::build_packet(src, dst, 6, &seg).unwrap() + } + + fn reply_flags(engine: &mut TcpEngine) -> Vec<(u8, u32)> { + let mut out = Vec::new(); + engine.drain_output(&mut out); + out.iter() + .map(|p| { + let v = ip::parse(p).unwrap(); + let h = wire::read_tcp(v.payload).unwrap(); + (v.payload[13], h.ack) + }) + .collect() + } + + #[test] + fn ipv6_syn_is_reset_when_rejected() { + let limits = Limits { + reject_ipv6: true, + ..Limits::default() + }; + let mut e = TcpEngine::new(limits); + e.input( + Instant::from_micros(1_000), + &syn("fd00::2".parse().unwrap(), "2001:db8::1".parse().unwrap()), + ); + let r = reply_flags(&mut e); + assert_eq!(r.len(), 1); + assert!(r[0].0 & 0x04 != 0, "RST expected, flags {:#x}", r[0].0); + assert_eq!(r[0].1, 1001); + assert!(e.accept().is_none()); + + // IPv4 is still accepted (SYN-ACK). + e.input( + Instant::from_micros(1_000), + &syn("10.0.0.2".parse().unwrap(), "1.1.1.1".parse().unwrap()), + ); + let r = reply_flags(&mut e); + assert_eq!(r.len(), 1); + assert_eq!(r[0].0 & 0x12, 0x12, "SYN-ACK expected, flags {:#x}", r[0].0); + } + + #[test] + fn ipv6_syn_is_accepted_by_default() { + let mut e = TcpEngine::new(Limits::default()); + e.input( + Instant::from_micros(1_000), + &syn("fd00::2".parse().unwrap(), "2001:db8::1".parse().unwrap()), + ); + let r = reply_flags(&mut e); + assert_eq!(r[0].0 & 0x12, 0x12); + } +} diff --git a/leaf/src/proxy/tun/netstack/tcp/stack.rs b/leaf/src/proxy/tun/netstack/tcp/stack.rs index 9620a4a..55f88a4 100644 --- a/leaf/src/proxy/tun/netstack/tcp/stack.rs +++ b/leaf/src/proxy/tun/netstack/tcp/stack.rs @@ -353,6 +353,8 @@ pub struct Env { pub isn: IsnGenerator, /// Keep-alive idle time set on accept; 0 for none. pub keepalive_idle_ms: u32, + /// Answer IPv6 SYNs with RST instead of accepting them. + pub reject_ipv6: bool, pub budget: Budget, /// The connection a segment is being processed for: its output waits /// until the processing is done. @@ -401,6 +403,7 @@ impl Stack { keepalive_idle_ms: u32, max_connections: usize, budget: Option, + reject_ipv6: bool, ) -> Stack { Stack { conns: Slab::default(), @@ -423,6 +426,7 @@ impl Stack { apps: Apps::new(max_connections), isn, keepalive_idle_ms, + reject_ipv6, budget: Budget { cap: budget, others: 0,