Repository navigation
xicmp: leave the client's ICMP sockets unconnected on Apple, which a Network Extension defuncts - #296
Merged
Merged
Conversation
…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.
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.
The second half of the iOS xicmp fix. #295 got echo requests out of the Network Extension, but the full xicmp smoke on the iPhone against the Linux VPS still failed: the server accepted 39 flows and answered with 4430 echo replies, and the first TCP exchange timed out. Tracking issue: #236 (xicmp on devices).
Evidence: a diagnostic run on the iPhone
The diagnostic build (local branch
claude/xicmp-ios-receive-diag, 9806faa1, never to be merged) reported over UDP from inside the extension. It used the stand's LANxicmp-routesetup: the iPhone sends xicmp to the Mac, and the Mac's kernel echoes every request. The result is indevice-4a61b057.noindex/results/ios/diagnostics/xicmp-route/rx-plain-1-listener/report.txt(Mac echo-in delta 1212).Side probes: one ICMP socket per setup, 20 echo requests each, all opened at the client's first dial while the tunnel came up.
IP_BOUND_IF, connect (#295, main)EPIPE, recvENOTCONNEPIPE/ENOTCONNIP_BOUND_IF, bind 0.0.0.0, connectEPIPE/ENOTCONNIP_BOUND_IF, bind 0.0.0.0, unconnectedEPIPE/ENOTCONNIP_BOUND_IF, connect,SO_RECV_ANYIFEPIPE/ENOTCONNThe client's own sockets (main's code) split the same way:
EPIPEand read nothing.The probe's first TCP exchange rides one of those first sockets, so the VPS smoke stopped there.
Other observations from the run:
bindand an unconnected receive through. An Apple forum report from 2019 had both failing withEPERM.Cause
EPIPEon send together withENOTCONNon receive is XNU's defunct socket (SOF_DEFUNCT,sosend/soreceiveinuipc_socket.c, xnu-12377.121.6). NECP defuncts sockets innecp_socket_find_policy_match(necp.c). When it evaluates a socket that is already connected again and the result is a drop, or an IP tunnel to an interface the socket does not use, it callssosetdefunct(..., SHUTDOWN_SOCKET_LEVEL_NECP | SHUTDOWN_SOCKET_LEVEL_DISCONNECT_ALL).A connected ICMP socket hits that rule inside the extension, and a UDP socket does not:
rip_connectrecords only the peer and leaves an ICMP socket's local address empty. Connecting a UDP socket fills its local address in.This corrects #295's account. Setup F shows that NECP does scope a plain connected ICMP socket at connect, and the socket's very first send then failed with
EPIPE. Before #295 nothing left the phone because the sockets were defuncted, not because they were unscoped. #295's binding before connect kept the sockets dialed after the tunnel came up alive, which is why its egress count looked good (pr295-plain-1, 1408). The first sockets, the ones the first connection uses, still died.Xray-core v26.11.9
The client (
transport/internet/finalmask/xicmp/client.go) listens on one unconnected socket per family, bound to the wildcard (icmp.ListenPacket("udp4", "0.0.0.0")), so it would not be defuncted this way. Its reader hands on a reply from any address, along with that address. sing-box's sing-tun reached the same setup on Darwin in August 2025 ("When running in NetworkExtension on macOS, write to connected socket results in EPIPE", commit e8d7fc1b).Fix
The fix is in
crates/xray-transport/src/xicmp/client.rs, functionattach, and applies on Apple systems only:sendtoto the server address.recvfromand keeps only the replies from its socket's address, which is all a connected socket used to get. Identifier and own-echo checks are unchanged.scope.rs) stays. The socket takes the interface the system gives a UDP socket before it binds, and re-asks at most once a second while it sends. NECP scopes a socket only once, when it binds, so this is what moves the socket between Wi-Fi and cellular.Behaviour elsewhere:
VpnService.protect.Tests
New tests in
crates/xray-transport/src/xicmp/client/tests.rs:peer_addrisNotConnected).Mutation checks, run locally on macOS:
CI doesn't run Rust tests on macOS, so the Apple tests ran locally. The defunct itself can't be reproduced off a device; the device run below covers it.
Local gates on this branch:
cargo fmt --all -- --checkRUSTDOCFLAGS=-D warnings cargo doc --workspace --no-deps --lockedcargo test -p xray-transport --all-targetscargo checkforaarch64-apple-iosaarch64-linux-androidci.ymlunder python3.12Device verification
Run on the iPhone with a Debug build of 3568138, 2026-11-15, by the device-stands session. This is a diagnostic, not release evidence; the third candidate still needs its own run. Write-up:
device-4a61b057.noindex/results/ios/diagnostics/xicmp-pr296-vps/SUMMARY.md.ios-xicmpattempt-2.done:passed, with all seven transitions passing: ipv4-tcp, ipv6-tcp, domain-destination, ipv4-udp, ipv6-udp, routed-dns and start-stop.Docs
CHANGELOG.mdentry under 0.10.0 now describes the cause and fix together.docs/config-compatibility.md: the iOS row, the socket paragraph and the interface paragraph.docs/xray-core-differences.md: the socket and reply-filtering differences.🤖 Generated with Claude Code