mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-03 17:45:44 +00:00
feat(socket): add Linux SO_MARK (fwmark) support for underlay sockets (#2288)
Adds a Linux-only socket_mark u32 config flag (CLI: --socket-mark, env:
ET_SOCKET_MARK, TOML/proto: flags.socket_mark, 0 = disabled) that is
applied as SO_MARK to every outbound underlay socket EasyTier creates:
TCP, UDP, QUIC, WebSocket, WireGuard connectors and listeners, plus the
FakeTCP decoy socket. Lets the host policy-route or filter EasyTier
underlay traffic with 'ip rule fwmark ...' or iptables -m mark.
Plumbing mirrors the existing bind_device pattern:
- FlagsInConfig.socket_mark (proto) + default 0 in gen_default_flags
- bind() builder gets a socket_mark arg; setup_socket2_ext calls
apply_socket_mark which is a no-op for mark=0 and on non-Linux
- TunnelConnector trait gets set_socket_mark(u32) default-no-op method
- IP-based connectors override; create_listener_by_url and the connector
factory pass mark from global_ctx flags
- QUIC threads mark through QuicEndpointManager::{server,connect}
- WebSocket/FakeTCP/TCP default-bind bypass paths apply mark via
socket2::SockRef::from(&tokio_socket)
- ForeignNetworkEntry propagates parent socket_mark into its derived ctx
Includes a Linux smoke test plus a CAP_NET_ADMIN-gated test that does a
getsockopt(SO_MARK) round-trip to confirm the kernel applied the value.
SO_MARK requires CAP_NET_ADMIN; ignored silently on non-Linux. FakeTCP's
TUN-written segments are not covered (kernel doesn't tag raw TUN
writes); operators relying on fwmark for FakeTCP must apply an iptables
rule on the FakeTCP TUN device separately.
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -463,6 +463,7 @@ pub struct WgTunnelListener {
|
||||
wg_peer_map: Arc<DashMap<SocketAddr, Arc<WgPeer>>>,
|
||||
|
||||
tasks: JoinSet<()>,
|
||||
socket_mark: Option<u32>,
|
||||
}
|
||||
|
||||
impl WgTunnelListener {
|
||||
@@ -479,9 +480,14 @@ impl WgTunnelListener {
|
||||
wg_peer_map: Arc::new(DashMap::new()),
|
||||
|
||||
tasks: JoinSet::new(),
|
||||
socket_mark: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_socket_mark(&mut self, socket_mark: Option<u32>) {
|
||||
self.socket_mark = socket_mark;
|
||||
}
|
||||
|
||||
fn get_udp_socket(&self) -> Arc<UdpSocket> {
|
||||
self.udp.as_ref().unwrap().clone()
|
||||
}
|
||||
@@ -561,6 +567,7 @@ impl TunnelListener for WgTunnelListener {
|
||||
.addr(addr)
|
||||
.only_v6(true)
|
||||
.maybe_dev(tunnel_url.bind_dev())
|
||||
.maybe_socket_mark(self.socket_mark)
|
||||
.call()?,
|
||||
));
|
||||
self.addr
|
||||
@@ -599,6 +606,7 @@ pub struct WgTunnelConnector {
|
||||
bind_addrs: Vec<SocketAddr>,
|
||||
ip_version: IpVersion,
|
||||
resolved_addr: Option<SocketAddr>,
|
||||
socket_mark: Option<u32>,
|
||||
}
|
||||
|
||||
impl Debug for WgTunnelConnector {
|
||||
@@ -619,6 +627,7 @@ impl WgTunnelConnector {
|
||||
bind_addrs: vec![],
|
||||
ip_version: IpVersion::Both,
|
||||
resolved_addr: None,
|
||||
socket_mark: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -695,6 +704,7 @@ impl WgTunnelConnector {
|
||||
.addr("[::]:0".parse().unwrap())
|
||||
.dev(BindDev::Disabled)
|
||||
.only_v6(true)
|
||||
.maybe_socket_mark(self.socket_mark)
|
||||
.call()?;
|
||||
Self::connect_with_socket(self.addr.clone(), self.config.clone(), socket, addr).await
|
||||
}
|
||||
@@ -721,7 +731,12 @@ impl super::TunnelConnector for WgTunnelConnector {
|
||||
let futures = FuturesUnordered::new();
|
||||
for bind_addr in bind_addrs.into_iter() {
|
||||
tracing::info!(?bind_addr, ?addr, "bind addr");
|
||||
match bind().addr(bind_addr).only_v6(true).call() {
|
||||
match bind()
|
||||
.addr(bind_addr)
|
||||
.only_v6(true)
|
||||
.maybe_socket_mark(self.socket_mark)
|
||||
.call()
|
||||
{
|
||||
Ok(socket) => futures.push(Self::connect_with_socket(
|
||||
self.addr.clone(),
|
||||
self.config.clone(),
|
||||
@@ -753,6 +768,10 @@ impl super::TunnelConnector for WgTunnelConnector {
|
||||
fn set_resolved_addr(&mut self, addr: SocketAddr) {
|
||||
self.resolved_addr = Some(addr);
|
||||
}
|
||||
|
||||
fn set_socket_mark(&mut self, socket_mark: Option<u32>) {
|
||||
self.socket_mark = socket_mark;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user