Repository navigation
xicmp: bind the client's ICMP sockets like UDP inside an Apple Network Extension - #295
Merged
Merged
Conversation
added 3 commits
November 14, 2026 11:13
…k Extension Inside the iOS Network Extension the xicmp client's echo requests never reached the network (0.10.0 device campaign on 4a61b05: tcpdump on the server saw none; the Mac's echo-in counter moved by 0 with the config as is and by 1239 with sockopt.interface = "en0"). The system scopes the extension's UDP sockets to the physical interface (NECP SOCKET_SCOPED, applied by XNU's inp_update_necp_policy as the bound interface) but left the ICMP datagram socket unbound, so rip_output routed it by the tunnel's default route into the tunnel itself. On Apple systems an xicmp socket the protector left unbound now takes the interface the system scopes a UDP socket of the process to on its way to the same address (a wildcard-bound, protected UDP socket connected there, sending nothing, read back through IP_BOUND_IF/IPV6_BOUND_IF), and its sends ask again at most once a second, so it moves between Wi-Fi and cellular and picks up scoping that starts after the dial. It never unbinds. Xray-core's server keys a client by its id and answers its latest address, so the connection goes on. A socket sockopt.interface bound keeps it; outside a Network Extension the socket stays unbound as before. Android, Linux and other systems are unchanged.
go-oracles failed on PR #295 (run 43619978436, "Verify Shadowsocks 2022 TCP, UDP and TUN"): shadowsocks_runtime_udp_reopens_after_port_change_and _survives_server_restart panicked with "reference exited", and Xray's log said "listen tcp 127.0.0.1:45109: bind: address already in use". The harness picks the reference's port by binding 127.0.0.1:0 and dropping the listener before Xray binds it; the binary's other tests run in parallel, and one of their sockets took the port in that gap. The same race failed v08_carriers on PR #246. Unrelated to the PR's change. The shared harness (mux, Shadowsocks, Trojan and VMess runtime tests) now treats a reference that exits with "address already in use" as a lost port: a start tries again on a fresh port, up to five times, and a restart, which must keep its port, respawns for about two seconds. Any other exit still fails the test with the reference's log.
… carry go-oracles failed on PR #295 (run 43621093196, "Verify mKCP masks and live mKCP carriers"): kcp_header_custom_udp_modes_and_mask_order timed out after 60 s in "header-custom prefix noise-inner", the UDP exchange never answered. The same case failed the dependabot libc PR (run 43352629279), which changed no product code. The prefix's server side reused the token the client draws for every datagram. Xray-core's client, and ours with it, takes a reply only with the token of its latest datagram, so a reply that crosses a newer datagram is dropped; direct_udp_masks already notes that such a prefix carries one datagram for one. mKCP's stream crosses them constantly: under `taskpolicy -b` locally the prefix cases dropped 1,720 to 10,654 server datagrams against about 120 taken, resends kept the tokens moving, cases ran 15 to 56 s and one run hit the 60 s timeout. The prefix now reuses nothing (rand on the server side, as direct_udp_masks' stream form), and the standalone handshake, one request for one answer, keeps the token. Under the same load: 0 to 2 drops per case, each under a second, four runs of four passing; the whole mKCP interop step passes against Xray-core 676eaf66.
This was referenced Nov 15, 2026
xicmp: leave the client's ICMP sockets unconnected on Apple, which a Network Extension defuncts
#296
Merged
dec0iiyy
pushed a commit
to dec0iiyy/xray-rust
that referenced
this pull request
Nov 17, 2026
…Network Extension defuncts aimalygin#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 aimalygin#295 stays, for network changes. Linux and Android are unchanged. This also corrects aimalygin#295's account: NECP did scope a plain connected ICMP socket, whose first send then failed; the sockets were defuncted, not left unscoped.
dec0iiyy
pushed a commit
to dec0iiyy/xray-rust
that referenced
this pull request
Nov 17, 2026
The second candidate, 4a61b05, failed on devices: the xicmp mask did not work inside the Apple Network Extension. The fixes are aimalygin#295 and aimalygin#296, and main through 46c38c1 carries both. Date the 0.10.0 changelog section 2026-11-14 and record the new candidate and the superseded second one in docs/v0100-release.md. The same document said the Mac could serve Xray-core's xicmp server under sudo; the campaign showed it cannot, so it now names a Linux host. The workspace version, lockfile and configuration contract are already 0.10.0 and do not change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the iOS-only xicmp defect found in the 0.10.0 device campaign on the second candidate (4a61b05). Inside the iOS Network Extension the xicmp client's ICMP socket put no echo request on the network. Tracking issue: #236 (xicmp on devices).
Evidence (device campaign,
device-4a61b057.noindex/results/, seecampaign-log.md)ios/ios-xicmp/attempt-2: the iPhone against Xray-core v26.11.9's xicmp server on a Linux VPS. The first TCP exchange fails, and tcpdump on the VPS saw 0 echo requests (ios/diagnostics/xicmp-vps/phone-1).ios/diagnostics/xicmp-vps/mac-cli-1, full echo). Android passes on both backends (android/android-xicmp/attempt-2).ios/diagnostics/xicmp-route/(run.sh): the iPhone sends xicmp to the Mac's LAN address while the Mac counts ICMP echo-in. With the config as is (plain-1) the count moved by 0. WithstreamSettings.sockopt.interface = "en0"(bound-1) it moved by 1239.Cause
Inside a Network Extension the tunnel's routes send all traffic into the tunnel. The system keeps the extension's own traffic out of it by scoping the extension's sockets to the physical interface. In XNU this is NECP's
SOCKET_SCOPEDresult:inp_update_necp_policybinds the socket withinp_bindif(the sameINP_BOUND_IFthatIP_BOUND_IFsets) and marks itINP2_SCOPED_BY_NECP. The socket layer runs this fromsobind/soconnect/sosendfor everyPF_INETsocket, ICMP included (checked inxnu-12377.121.6:uipc_socket.c,in_pcb.c,necp.c).On the iPhone the extension's UDP sockets get scoped (mKCP over plain UDP reaches the network with no binding), but the ICMP datagram socket does not (
plain-1: 0 echo requests). So the scoping policies the system installs for the provider don't match ICMP sockets. That part is inferred: NetworkExtension installs those policies from userspace, and XNU doesn't show them. Nothing else scopes an ICMP socket.rip_connectonly recordsinp_faddr, andrip_outputpasses an interface toip_outputonly whenINP_BOUND_IFis set. Soip_outputlooks the route up unscoped, finds the tunnel's default route, and the echo request goes into the tunnel itself. Binding the socket fixes it (bound-1).Why the other platforms work: Android's
VpnService.protectexcludes any socket from the VPN, ICMP included. The macOS CLI has no tunnel routes.Xray-core v26.11.9
transport/internet/finalmask/xicmp/client.goopens one unconnected socket per family withicmp.ListenPacket("udp4", "0.0.0.0"). That socket is bound to the wildcard, never bound to an interface, and not reached bysockopt. Inside a Network Extension it would take the same unscoped route into the tunnel (and the extension refuses its wildcard bind, as the client's header already notes). Its server (server.go) records the client's address, keyed by the client's 8-byte id, on every request and answers that address. So a client whose source address changes keeps working, and that is what lets the socket here follow the network. This is an intentional difference, recorded indocs/xray-core-differences.md.Fix
crates/xray-transport/src/xicmp/client/scope.rs. This applies on Apple only (cfg(target_vendor = "apple")); elsewhere it compiles to a no-op:IP_BOUND_IF/IPV6_BOUND_IF, e.g. bysockopt.interface), it keeps that interface and is never re-checked.IP_BOUND_IF/IPV6_BOUND_IF. Inside a Network Extension NECP has scoped it, and the ICMP socket is bound to the same interface. Outside one the value is 0, and the ICMP socket stays unbound as before.try_lock, never blocks), and the socket rebinds when the index changes. This covers Wi-Fi ↔ cellular and a dial made before the tunnel's settings were applied. It never unbinds, because an unbound socket would send into the tunnel. Xray-core's server answers the client's latest address, so a live connection continues on the new interface.Behaviour elsewhere:
Tests
Apple-only tests in
crates/xray-transport/src/xicmp/client/tests.rsuse a protector that does what NECP does in the extension: it binds sockets that have a port (UDP) and leaves the ICMP socket alone.sockopt.interface) keeps its interface, and the system is never asked.Mutation check: dropping the bind at open fails three of them. CI doesn't run Rust tests on macOS (the
applejob builds the XCFramework and runs the Swift tests), so these ran locally on macOS. CI's Linux jobs cover the non-Apple path.Local gates on this branch:
cargo fmt --all -- --check; workspace clippy with CI's flags on macOS;RUSTDOCFLAGS=-D warnings cargo doc --workspace --no-deps --locked;cargo test -p xray-transport --all-targets;cargo check --all-targets -p xray-transport --target aarch64-apple-ios; clippy foraarch64-linux-android; and the scripted gates fromci.ymlunder python3.12.Device verification (for the device-stands session)
Not checked on a device yet. With a build of this branch on the existing stand:
results/ios/diagnostics/xicmp-route/run.sh <name> plain, i.e. the config withoutsockopt.interface. Expect the Mac's echo-in delta to be well above 0, as inbound-1(1239).plain-1was 0. Optionally repeatboundto confirmsockopt.interfacestill works.ios-xicmp/attempt-2). Expect the run to pass, the server to accept connections, and tcpdump on the server to show echo requests from the phone.Docs
A
CHANGELOG.mdentry under 0.10.0; the xicmp section ofdocs/config-compatibility.md(iOS row and the interface binding); anddocs/xray-core-differences.md(the deliberate difference).Second commit: interop harness port race (unrelated to xicmp)
The first CI run failed
go-oraclesin "Verify Shadowsocks 2022 TCP, UDP and TUN" (run 43619978436).shadowsocks_runtime_udp_reopens_after_port_change_and_survives_server_restartpanicked with "reference exited", and Xray's log showedlisten tcp 127.0.0.1:45109: bind: address already in use.The shared harness
crates/xray-core-rs/tests/trojan_runtime/support.rspicks the reference's port by binding127.0.0.1:0, then drops that listener before Xray binds the port. The binary's other tests run in parallel, and one of their sockets took the port in that gap. The same race failedv08_carrierson PR #246.The harness, shared by the mux, Shadowsocks, Trojan and VMess runtime tests, now treats a reference that exits with "address already in use" as a lost port:
Checked locally:
scripts/check-shadowsocks2022-interop.sh's cargo command, run with a local copy of the reference binary.xray-core-rstest targets.Third commit: mKCP header-custom interop livelock (unrelated to xicmp)
The second CI run failed
go-oraclesin "Verify mKCP masks and live mKCP carriers" (run 43621093196).kcp_header_custom_udp_modes_and_mask_ordertimed out after 60 s in "header-custom prefix noise-inner". The same case failed the dependabot libc PR (run 43352629279), which changed no product code.The test's prefix had the server reuse the token the client draws for every datagram. Xray-core's client, and ours, accepts a reply only if it carries the token of the client's latest datagram, so a reply that crosses a newer datagram is dropped.
direct_udp_masksalready notes that such a prefix carries only one datagram for one. mKCP's stream crosses them all the time.Measured locally under
taskpolicy -b, with a temporary trace that isn't committed:randon the server side, the stream formdirect_udp_masksuses). Standalone mode, one request for one answer, keeps the token. Under the same load there are 0–2 drops per case, each case finishes in under a second, and 4 of 4 runs pass.The client's behaviour is unchanged; it matches Xray-core's.
🤖 Generated with Claude Code