From 35681380c6df72b36a8fa14a8d0e354ad36040b0 Mon Sep 17 00:00:00 2001 From: Anton Malygin Date: Sat, 14 Nov 2026 18:27:55 -0600 Subject: [PATCH] xicmp: leave the client's ICMP sockets unconnected on Apple, which a Network Extension defuncts #295 got the xicmp client's echo requests out of an iOS Network Extension, but the full smoke against Xray-core's server on a Linux VPS still failed: the server answered and the first exchange timed out. A diagnostic build on the iPhone (the stand's LAN xicmp-route setup) found why. Every connected ICMP socket without a local address sent one echo request, then failed every send with EPIPE and every read with ENOTCONN: XNU's defunct socket. NECP's necp_socket_find_policy_match defuncts a connected socket when a re-evaluation finds a drop or another interface's tunnel, and rip_connect leaves an ICMP socket's local address empty, where connecting a UDP socket fills it in. The sockets dialed while the tunnel came up, the first connection's among them, died so. In the same run, unconnected sockets bound to the wildcard, and one bound to the interface's address, sent and read throughout. On Apple systems the client's ICMP sockets are now bound to the wildcard and left unconnected, as Xray-core's are: sends use sendto, and the reader keeps the replies from its socket's address. Where the bind is refused the socket is connected as before. The interface following from #295 stays, for network changes. Linux and Android are unchanged. This also corrects #295's account: NECP did scope a plain connected ICMP socket, whose first send then failed; the sockets were defuncted, not left unscoped. --- CHANGELOG.md | 33 +++-- crates/xray-transport/src/xicmp/client.rs | 123 ++++++++++++++---- .../xray-transport/src/xicmp/client/scope.rs | 29 ++--- .../xray-transport/src/xicmp/client/tests.rs | 100 ++++++++++++-- docs/config-compatibility.md | 37 +++--- docs/xray-core-differences.md | 31 +++-- 6 files changed, 253 insertions(+), 100 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d98f261..f8edda27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,20 +15,25 @@ long-term supported release series. Release candidate preparation; publication and exact-candidate release qualification are pending. -- The `xicmp` mask works inside an Apple Network Extension. There the system - scopes the extension's UDP sockets to the physical interface but not its - ICMP sockets, so the client's echo requests followed the tunnel's routes - into the tunnel and never left the device: on an iPhone, mKCP over xicmp - failed at its first exchange and the server saw no echo request, while the - macOS CLI and Android worked against the same server. On Apple systems an - xicmp socket nothing bound now takes the interface the system scopes a UDP - socket of the process to on its way to the same address, and asks again at - most once a second while it sends, so it moves between Wi-Fi and cellular; - Xray-core's server knows the client by its id and answers its latest - address. Outside a Network Extension the system scopes no UDP socket and - the socket stays unbound, as before, and `sockopt.interface` still binds - it. Android, Linux and the macOS CLI behave as before. Found on an iPhone - by the v0.10 device fixtures. +- The `xicmp` mask works inside an Apple Network Extension. The client + connected its ICMP sockets to the server, and XNU leaves a connected ICMP + socket's local address empty, where connecting a UDP socket fills it in. + Inside the extension such a flow matches the tunnel's policy, and NECP + defuncts a connected socket with that result: on an iPhone each socket + dialed while the tunnel came up, the first connection's among them, failed + every send after its first echo request with `EPIPE` and read nothing, so + mKCP over xicmp failed at its first exchange, while the macOS CLI and + Android worked against the same server. On Apple systems the client's + sockets are now bound to the wildcard and left unconnected, as Xray-core's + are, and keep the replies from their server's address; where the bind is + refused they are connected as before. Each also takes the interface the + system scopes a UDP socket of the process to on its way to the same + address, and asks again at most once a second while it sends, so it moves + between Wi-Fi and cellular; Xray-core's server knows the client by its id + and answers its latest address. Outside a Network Extension the system + scopes no UDP socket and the socket stays unbound, and `sockopt.interface` + still binds it. Android and Linux behave as before. Found on an iPhone by + the v0.10 device fixtures. - The Apple and Android adapters start a profile whose VLESS outbound is in the simplified form, its server in `settings.address` with no `vnext`, as the core and Xray-core read it. Before the tunnel's DNS takes over, both diff --git a/crates/xray-transport/src/xicmp/client.rs b/crates/xray-transport/src/xicmp/client.rs index 0f6e5354..83ad3fd0 100644 --- a/crates/xray-transport/src/xicmp/client.rs +++ b/crates/xray-transport/src/xicmp/client.rs @@ -2,11 +2,11 @@ //! after the client's id, and echo replies bring the server's packets. //! //! Xray-core listens on one unconnected socket per family, bound to the -//! wildcard. Here each server address gets its own socket, never bound and -//! connected to it: an unbound socket is bound on its first send, and a -//! Network Extension refuses to bind an ICMP socket or read an unconnected -//! one. Replies then come only from the address the socket sends to, as -//! Xray-core's Linux server makes them. +//! wildcard. Here each server address gets its own socket. On Apple systems +//! it is bound to the wildcard and left unconnected as well, and its reader +//! keeps the replies from its address: a Network Extension defuncts a +//! connected one ([`attach`]). Elsewhere it is connected to its address, +//! and replies come only from there, as Xray-core's Linux server makes them. //! //! On Apple systems a socket is also bound to an interface where the system //! scopes the process's UDP sockets to one, as it does inside a Network @@ -76,10 +76,12 @@ struct Shared { dgram: bool, } -/// One server address's socket and the interface it leaves through. +/// One server address's socket, whether it is connected to the address, +/// and the interface it leaves through. struct Socket { address: IpAddr, socket: std::net::UdpSocket, + connected: bool, scope: scope::Scope, } @@ -102,15 +104,16 @@ impl XicmpClient { if sockets.iter().any(|socket| socket.address == address) { continue; } - let (socket, scope) = open(address, config.dgram, protector.as_deref())?; + let (socket, scope, connected) = open(address, config.dgram, protector.as_deref())?; let reader = socket .try_clone() .and_then(UdpSocket::from_std) .map_err(XicmpError::Socket)?; - readers.push((address, reader)); + readers.push((address, connected, reader)); sockets.push(Socket { address, socket, + connected, scope, }); } @@ -128,8 +131,14 @@ impl XicmpClient { let (sender, received) = mpsc::channel(RECEIVE_QUEUE); let tasks = readers .into_iter() - .map(|(address, reader)| { - tokio::spawn(read(shared.clone(), address, reader, sender.clone())) + .map(|(address, connected, reader)| { + tokio::spawn(read( + shared.clone(), + address, + connected, + reader, + sender.clone(), + )) }) .collect(); Ok(Self { @@ -156,7 +165,12 @@ impl XicmpClient { }; let addresses = &self.shared.addresses; let address = addresses[rand::thread_rng().gen_range(0..addresses.len())]; - let Some(Socket { socket, scope, .. }) = self + let Some(Socket { + socket, + connected, + scope, + .. + }) = self .shared .sockets .iter() @@ -173,7 +187,13 @@ impl XicmpClient { } else { message::ECHO_REQUEST_V6 }; - match socket.send(&message::echo(kind, self.shared.id, seq, &data)) { + let echo = message::echo(kind, self.shared.id, seq, &data); + let sent = if *connected { + socket.send(&echo) + } else { + socket.send_to(&echo, SocketAddr::new(address, 0)) + }; + match sent { Err(error) if error.kind() == io::ErrorKind::WouldBlock => Ok(()), result => result.map(drop), } @@ -207,15 +227,15 @@ impl Drop for XicmpClient { } } -/// A nonblocking ICMP socket for `address`'s family, connected to it, and -/// the interface it leaves through. On Apple systems an IPv4 datagram -/// socket returns the IP header unless told not to, which Go's -/// `icmp.ListenPacket` does. +/// A nonblocking ICMP socket for `address`'s family, the interface it +/// leaves through, and whether it is connected to `address` ([`attach`]). +/// On Apple systems an IPv4 datagram socket returns the IP header unless +/// told not to, which Go's `icmp.ListenPacket` does. fn open( address: IpAddr, dgram: bool, protector: Option<&dyn SocketProtector>, -) -> Result<(std::net::UdpSocket, scope::Scope), XicmpError> { +) -> Result<(std::net::UdpSocket, scope::Scope, bool), XicmpError> { use socket2::{Domain, Protocol, Socket, Type}; let (domain, protocol) = if address.is_ipv4() { (Domain::IPV4, Protocol::ICMPV4) @@ -231,11 +251,47 @@ fn open( let socket: std::net::UdpSocket = socket.into(); crate::protect_std_udp_socket(&socket, protector).map_err(XicmpError::Protection)?; let scope = scope::Scope::open(&socket, address, protector).map_err(XicmpError::Socket)?; - socket - .connect(SocketAddr::new(address, 0)) - .map_err(XicmpError::Socket)?; + let connected = attach(&socket, address).map_err(XicmpError::Socket)?; socket.set_nonblocking(true).map_err(XicmpError::Socket)?; - Ok((socket, scope)) + Ok((socket, scope, connected)) +} + +/// Readies `socket` to reach `address`: bound to the wildcard and left +/// unconnected on Apple systems, connected elsewhere. Returns whether it is +/// connected. +/// +/// XNU's `rip_connect` records only the peer of an ICMP socket and leaves +/// its local address empty, where connecting a UDP socket fills it in. +/// Inside a Network Extension a flow with no local address matches the +/// tunnel's policy, and when NECP evaluates a connected socket again and +/// finds a drop or another interface's tunnel, it defuncts the socket +/// (`necp_socket_find_policy_match`): every later send fails with `EPIPE` +/// and every read with `ENOTCONN`. On an iPhone that took each socket +/// dialed while the tunnel came up, the first connection's among them, +/// after one echo request (the 0.10.0 device campaign). NECP never defuncts +/// an unconnected socket so, and checks each of its packets with the source +/// address it leaves from; there both an unconnected socket and one bound +/// to the interface's address kept sending and reading. Binding to the +/// wildcard also has the system scope it, as it scopes UDP sockets when +/// they bind. Where the bind is refused, as Apple's forum once reported +/// inside a Network Extension, the socket is connected as before. +fn attach(socket: &std::net::UdpSocket, address: IpAddr) -> io::Result { + #[cfg(target_vendor = "apple")] + { + let wildcard = if address.is_ipv4() { + SocketAddr::from((std::net::Ipv4Addr::UNSPECIFIED, 0)) + } else { + SocketAddr::from((std::net::Ipv6Addr::UNSPECIFIED, 0)) + }; + if socket2::SockRef::from(socket) + .bind(&wildcard.into()) + .is_ok() + { + return Ok(false); + } + } + socket.connect(SocketAddr::new(address, 0))?; + Ok(true) } /// `IP_STRIPHDR`, Darwin's option to read an ICMP datagram socket without @@ -263,21 +319,34 @@ fn strip_header(socket: &socket2::Socket) -> io::Result<()> { } } -/// `recv4` and `recv6`: echo replies, with this client's identifier where -/// the socket keeps it, whose data does not start with the client's id, -/// which the server's own system echoes back. A read error stops the -/// reader, as it stops Xray-core's. +/// `recv4` and `recv6`: echo replies from `address`, with this client's +/// identifier where the socket keeps it, whose data does not start with the +/// client's id, which the server's own system echoes back. An unconnected +/// socket reads every reply the system gets and keeps those from `address`, +/// all a connected one would get; Xray-core's hands on a reply from any +/// address with that address. A read error stops the reader, as it stops +/// Xray-core's. async fn read( shared: Arc, address: IpAddr, + connected: bool, socket: UdpSocket, sender: mpsc::Sender, ) { let mut buf = vec![0u8; MAX_PACKET]; let v6 = address.is_ipv6(); loop { - let Ok(length) = socket.recv(&mut buf).await else { - return; + let length = if connected { + match socket.recv(&mut buf).await { + Ok(length) => length, + Err(_) => return, + } + } else { + match socket.recv_from(&mut buf).await { + Ok((length, from)) if from.ip().to_canonical() == address => length, + Ok(_) => continue, + Err(_) => return, + } }; let Some(data) = accept(&buf[..length], v6, &shared) else { continue; diff --git a/crates/xray-transport/src/xicmp/client/scope.rs b/crates/xray-transport/src/xicmp/client/scope.rs index a0592502..06c60103 100644 --- a/crates/xray-transport/src/xicmp/client/scope.rs +++ b/crates/xray-transport/src/xicmp/client/scope.rs @@ -4,23 +4,20 @@ //! and the system keeps the extension's own traffic out of it by scoping //! its sockets to the physical interface: NECP's `SOCKET_SCOPED` result, //! which XNU's `inp_update_necp_policy` applies as the socket's bound -//! interface. On an iPhone it scoped the extension's UDP sockets so but -//! left its ICMP datagram socket unbound: no echo request reached the -//! network until `sockopt.interface` bound the socket (the 0.10.0 device -//! campaign). XNU evaluates the same policies for ICMP sockets, so the ones -//! the system installs for the extension do not match them; nothing else -//! scopes such a socket (`rip_connect` records only the address, -//! `rip_output` routes as the socket is bound), and its echo requests -//! follow the tunnel's routes into the tunnel. +//! interface when the socket binds or connects. On an iPhone it scoped the +//! xicmp socket so as well when the socket bound to the wildcard (the +//! 0.10.0 device campaign), but only then: the interface stays when the +//! network changes, and the client keeps its sockets for the life of a +//! connection. //! -//! So an ICMP socket the protector left unbound is bound here to the -//! interface the system gives a UDP socket of this process on its way to -//! the same address, and asks again while it sends, at most once a period: -//! it moves with the network as the UDP sockets opened after a change do, -//! and takes an interface once the system starts scoping, as when the dial -//! came before the tunnel's settings. Xray-core's server knows a client by -//! its id and answers where its latest request came from, so the connection -//! goes on over the new network. +//! So an ICMP socket the protector left unbound is bound here, before it +//! binds, to the interface the system gives a UDP socket of this process on +//! its way to the same address, and asks again while it sends, at most once +//! a period: it moves with the network as the UDP sockets opened after a +//! change do, and takes an interface once the system starts scoping, as +//! when the dial came before the tunnel's settings. Xray-core's server +//! knows a client by its id and answers where its latest request came from, +//! so the connection goes on over the new network. //! //! Where the system scopes no UDP socket, outside a Network Extension, the //! ICMP socket stays unbound and follows the routes, as Xray-core's does. A diff --git a/crates/xray-transport/src/xicmp/client/tests.rs b/crates/xray-transport/src/xicmp/client/tests.rs index 3196339d..bcbd8c9c 100644 --- a/crates/xray-transport/src/xicmp/client/tests.rs +++ b/crates/xray-transport/src/xicmp/client/tests.rs @@ -78,7 +78,7 @@ async fn a_datagram_client_sends_and_drops_the_systems_echo_of_it() { #[tokio::test] async fn an_apple_ipv4_datagram_socket_reads_without_the_header() { use std::os::fd::AsRawFd; - let Ok((socket, _)) = open("127.0.0.1".parse().unwrap(), true, None) else { + let Ok((socket, _, _)) = open("127.0.0.1".parse().unwrap(), true, None) else { return; }; let mut value: libc::c_int = 0; @@ -98,6 +98,81 @@ async fn an_apple_ipv4_datagram_socket_reads_without_the_header() { assert_ne!(value, 0); } +/// The device defect of 0.10.0's candidates: inside a Network Extension +/// NECP defuncted a connected ICMP socket after its first echo request. On +/// Apple systems the socket is left unconnected, in either family. +#[cfg(target_vendor = "apple")] +#[test] +fn an_apple_socket_is_left_unconnected() { + for address in ["127.0.0.1", "::1"] { + let address: IpAddr = address.parse().unwrap(); + let (socket, _, connected) = open(address, true, None).unwrap(); + assert!(!connected, "{address}"); + assert_eq!( + socket.peer_addr().unwrap_err().kind(), + io::ErrorKind::NotConnected, + "{address}" + ); + } +} + +/// Elsewhere the socket is connected to its address, as before. +#[cfg(not(target_vendor = "apple"))] +#[test] +fn elsewhere_a_socket_is_connected_to_its_address() { + let address: IpAddr = "127.0.0.1".parse().unwrap(); + let Ok((socket, _, connected)) = open(address, true, None) else { + return; + }; + assert!(connected); + assert_eq!(socket.peer_addr().unwrap().ip(), address); +} + +/// An unconnected socket reads every echo reply the system gets. A reply +/// from another address, even with the client's identifier and a packet in +/// it, is not the server's; the same from the server's address is. +#[cfg(target_vendor = "apple")] +#[tokio::test] +async fn an_unconnected_socket_keeps_only_the_replies_from_its_address() { + use socket2::{Domain, Protocol, Socket, Type}; + // Another address of this system, which echoes requests to it as well. + let route = std::net::UdpSocket::bind("0.0.0.0:0").unwrap(); + if route.connect("192.0.2.1:9").is_err() { + return; + } + let other = route.local_addr().unwrap().ip(); + if other.is_loopback() || other.is_unspecified() { + return; + } + let config = XicmpConfig { + dgram: true, + ips: vec![], + }; + let server: IpAddr = "127.0.0.1".parse().unwrap(); + let client = XicmpClient::connect(&config, server, None).await.unwrap(); + let pinger = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::ICMPV4)).unwrap(); + let id = client.shared.id; + let elsewhere = message::echo(message::ECHO_REQUEST_V4, id, 1, b"from elsewhere"); + pinger + .send_to(&elsewhere, &SocketAddr::new(other, 0).into()) + .unwrap(); + tokio::time::sleep(std::time::Duration::from_millis(300)).await; + assert!( + client.try_recv().is_none(), + "another address's reply got through" + ); + let from_server = message::echo(message::ECHO_REQUEST_V4, id, 2, b"from the server"); + pinger + .send_to(&from_server, &SocketAddr::new(server, 0).into()) + .unwrap(); + let packet = tokio::time::timeout(std::time::Duration::from_secs(2), client.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(packet.data, b"from the server"); + assert_eq!(packet.from, SocketAddr::new(server, 0)); +} + #[tokio::test] async fn every_listed_address_gets_one_socket_and_draws_count_duplicates() { let config = XicmpConfig { @@ -156,10 +231,11 @@ fn two_interfaces() -> (std::num::NonZeroU32, std::num::NonZeroU32) { ) } -/// What NECP does inside a Network Extension: binds the process's UDP -/// sockets, which take a port, to the interface the tunnel runs over, and -/// leaves its ICMP sockets alone. Zero leaves them unbound, as outside one. -/// With `all`, it binds the ICMP sockets too, as `sockopt.interface` does. +/// Stands in for NECP inside a Network Extension: binds the process's UDP +/// sockets, which take a port, to the interface the tunnel runs over. It +/// leaves the ICMP sockets alone, so that the tests see what the client +/// binds them to. Zero leaves the UDP sockets unbound, as outside one. With +/// `all`, it binds the ICMP sockets too, as `sockopt.interface` does. #[cfg(target_vendor = "apple")] #[derive(Default)] struct ScopesUdp { @@ -213,10 +289,8 @@ impl SocketProtector for ScopesUdp { } } -/// The device defect of 0.10.0's second candidate: in a Network Extension -/// the system scoped the UDP sockets and not the ICMP one, whose echo -/// requests went into the tunnel. The ICMP socket now takes the UDP -/// sockets' interface, in either family. +/// The ICMP socket takes the interface the system scopes the process's UDP +/// sockets to, in either family. #[cfg(target_vendor = "apple")] #[tokio::test] async fn an_icmp_socket_takes_the_interface_the_system_scopes_udp_to() { @@ -224,7 +298,7 @@ async fn an_icmp_socket_takes_the_interface_the_system_scopes_udp_to() { for address in ["127.0.0.1", "::1"] { let address: IpAddr = address.parse().unwrap(); let scopes = ScopesUdp::to(loopback); - let (socket, scope) = open(address, true, Some(&scopes)).unwrap(); + let (socket, scope, _) = open(address, true, Some(&scopes)).unwrap(); assert_eq!( scope::system(address, Some(&scopes)).unwrap(), Some(loopback) @@ -248,7 +322,7 @@ async fn an_icmp_socket_stays_unbound_while_udp_sockets_are() { let (loopback, _) = two_interfaces(); let scopes = ScopesUdp::default(); let address: IpAddr = "127.0.0.1".parse().unwrap(); - let (socket, scope) = open(address, true, Some(&scopes)).unwrap(); + let (socket, scope, _) = open(address, true, Some(&scopes)).unwrap(); assert_eq!(scope::bound(&socket, address).unwrap(), None); assert!(scope.follows()); let later = std::time::Instant::now() + scope::RECHECK; @@ -271,7 +345,7 @@ async fn an_icmp_socket_the_protector_bound_keeps_its_interface() { ..ScopesUdp::to(other) }; let address: IpAddr = "127.0.0.1".parse().unwrap(); - let (socket, scope) = open(address, true, Some(&scopes)).unwrap(); + let (socket, scope, _) = open(address, true, Some(&scopes)).unwrap(); assert_eq!(scope::bound(&socket, address).unwrap(), Some(loopback)); assert!(!scope.follows()); assert_eq!(scopes.asked(), 0, "the system was asked"); @@ -287,7 +361,7 @@ async fn an_icmp_socket_moves_with_the_interface_the_system_scopes_udp_to() { let (loopback, other) = two_interfaces(); let scopes = ScopesUdp::to(loopback); let address: IpAddr = "127.0.0.1".parse().unwrap(); - let (socket, scope) = open(address, true, Some(&scopes)).unwrap(); + let (socket, scope, _) = open(address, true, Some(&scopes)).unwrap(); let opened = std::time::Instant::now(); scopes.set(Some(other)); scope.refresh_at(opened, &socket, address, Some(&scopes)); diff --git a/docs/config-compatibility.md b/docs/config-compatibility.md index 83421cbb..9a3bc6f1 100644 --- a/docs/config-compatibility.md +++ b/docs/config-compatibility.md @@ -3133,9 +3133,10 @@ connection, a sequence number counting from one, then the client's random server answers with the identifier and sequence of the client's last request and the packet alone. The server's own system echoes each request back as well; the client drops a reply that starts with its id. On a raw socket, -and on an Apple datagram socket, which keeps the identifier and receives -every reply from its address, it also drops replies with another -identifier; a Linux datagram socket gets only its own. +and on an Apple datagram socket, which keeps the identifier, it also drops +replies with another identifier; a Linux datagram socket gets only its own. +An Apple socket, unconnected (below), also reads the replies from other +addresses, and drops them. Where the sockets come from: @@ -3144,25 +3145,29 @@ Where the sockets come from: | macOS | Granted to any process (checked) | Root only | | Linux | Granted to the groups in `net.ipv4.ping_group_range`: the kernel's default `1 0` refuses everyone, systemd and Docker set `0 2147483647` (both checked in a container) | `CAP_NET_RAW` | | Android | Granted to apps: `init.rc` sets `ping_group_range` to `0 2147483647`; the sockets take the VPN's protection like the core's others (not checked on a device) | Not granted to apps | -| iOS | Granted without entitlements, as Apple's SimplePing uses them; inside a Network Extension Apple's forum reports `bind` and reads on an unconnected socket failing with `EPERM`, which the client avoids, and the socket must be bound to an interface (below) | Not granted | - -Each server address gets its own socket, never bound and connected to it: an -unbound socket is bound on its first send, and replies come only from the -address each packet went to, as Xray-core's Linux server makes them. Linux -datagram sockets put their own identifier in the request, and the server's -replies with it reach them. On macOS and iOS an IPv4 datagram socket would -return the IP header, which the client switches off as Go does. +| iOS | Granted without entitlements, as Apple's SimplePing uses them; inside a Network Extension a connected socket fails after its first echo request, so the client's is bound to the wildcard and unconnected (checked on an iPhone), and connected only where the bind is refused, as Apple's forum once reported there (below) | Not granted | + +Each server address gets its own socket. On macOS and iOS it is bound to the +wildcard and left unconnected, as Xray-core's sockets are, and keeps the echo +replies from its address. Inside a Network Extension a connected ICMP socket +fails: XNU leaves its local address empty, the tunnel's policy matches such a +flow, and NECP defuncts a connected socket with that result, after which every +send fails with `EPIPE` and every read with `ENOTCONN`. Where the bind is +refused, the socket is connected instead. Elsewhere it is connected to its +address, and replies come only from there, as Xray-core's Linux server makes +them. Linux datagram sockets put their own identifier in the request, and the +server's replies with it reach them. On macOS and iOS an IPv4 datagram socket +would return the IP header, which the client switches off as Go does. On macOS and iOS a socket nothing bound takes the interface the system scopes a UDP socket of the process to on its way to the same address, and asks again at most once a second while it sends, so that it moves between Wi-Fi and cellular as the UDP sockets opened after a change do; the server knows the client by its id and answers where its latest request came from. -Inside a Network Extension the system scopes the extension's UDP sockets to -the physical interface but not its ICMP sockets, whose echo requests would -otherwise follow the tunnel's routes into the tunnel and never leave the -device. Elsewhere the system scopes no UDP socket and the socket stays -unbound, following the routes. A socket `sockopt.interface` bound keeps that +Inside a Network Extension the system scopes a socket to the physical +interface when it binds, and leaves it there when the network changes. +Elsewhere the system scopes no UDP socket and the socket stays unbound, +following the routes. A socket `sockopt.interface` bound keeps that interface. The mask must be the last UDP mask: Xray-core fails every dial with it diff --git a/docs/xray-core-differences.md b/docs/xray-core-differences.md index 7b18eb94..dc06a8e5 100644 --- a/docs/xray-core-differences.md +++ b/docs/xray-core-differences.md @@ -1004,22 +1004,25 @@ x/net/icmp's reading of replies, the drop of the system's own echo and of a raw socket's foreign identifiers, and Go's stripping of a raw IPv4 header. Deliberate differences: -- Each server address gets its own socket, never bound and connected to it. - Xray-core binds one unconnected socket per family to the wildcard, which a - Network Extension refuses; replies here come only from the address each - packet went to, as Xray-core's Linux server sends them. A family the +- Each server address gets its own socket, where Xray-core binds one + unconnected socket per family to the wildcard. On Apple systems it is + bound to the wildcard and unconnected as well, but keeps only the replies + from its address, where Xray-core hands on a reply from any address with + that address; a Network Extension defuncts a connected ICMP socket, whose + local address XNU leaves empty, and where the bind is refused the socket + is connected instead. Elsewhere it is connected to its address, so replies + come only from there, as Xray-core's Linux server sends them. A family the addresses do not use needs no socket, where Xray-core fails every dial unless sockets of both families open. - On Apple systems a socket nothing bound takes the interface the system scopes a UDP socket of the process to on its way to the same address, and asks again at most once a second while it sends. Inside a Network - Extension the system scopes the extension's UDP sockets to the physical - interface but not its ICMP sockets, so an unbound socket's echo requests - follow the tunnel's routes into the tunnel; found on an iPhone, where the - server saw none. Xray-core's sockets are never bound to an interface, and - its server's answering the client's latest address is what lets a socket - here move with the network. Where the system scopes no UDP socket, outside - a Network Extension, the socket stays unbound as Xray-core's does. + Extension the system scopes a socket to the physical interface when it + binds and leaves it there when the network changes. Xray-core's sockets + are never bound to an interface, and its server's answering the client's + latest address is what lets a socket here move with the network. Where + the system scopes no UDP socket, outside a Network Extension, the socket + stays unbound as Xray-core's does. - It must be the last UDP mask, and runs neither with udphop, xdns, realm nor header-custom, nor on QUIC with sudoku: Xray-core fails every dial with it anywhere else, the others send elsewhere, header-custom keys its @@ -1029,9 +1032,9 @@ Deliberate differences: core starts; Xray-core never resolves it and fails every dial. With `ips`, a name there must still resolve. - An Apple datagram socket keeps the client's identifier and receives every - reply from its address, so replies are told apart by identifier, as on a - raw socket. Xray-core's datagram sockets there also read the replies meant - for other connections to the same server. + reply the system gets, so replies are told apart by identifier and + address, as on a raw socket. Xray-core's datagram sockets there also read + the replies meant for other connections. - An echo the socket has no room for is dropped, as a datagram would be; Xray-core's writer waits.