Skip to content

xicmp: leave the client's ICMP sockets unconnected on Apple, which a Network Extension defuncts - #296

Merged
aimalygin merged 1 commit into
mainfrom
claude/xicmp-ios-receive
Nov 15, 2026
Merged

aimalygin merged 1 commit into
mainfrom
claude/xicmp-ios-receive

Conversation

@aimalygin

@aimalygin aimalygin commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

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 LAN xicmp-route setup: the iPhone sends xicmp to the Mac, and the Mac's kernel echoes every request. The result is in device-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.

Setup Sent Replies Errors
A: IP_BOUND_IF, connect (#295, main) 1/20 0 send EPIPE, recv ENOTCONN
B: bind 0.0.0.0, connect 1/20 0 EPIPE / ENOTCONN
C: IP_BOUND_IF, bind 0.0.0.0, connect 1/20 0 EPIPE / ENOTCONN
D: bind 0.0.0.0, unconnected 20/20 15 none
E: IP_BOUND_IF, bind 0.0.0.0, unconnected 20/20 14 none
F: connect only (before #295) 0/20 0 EPIPE / ENOTCONN
G: bind to en0's address, connect 20/20 14 none
H: IP_BOUND_IF, connect, SO_RECV_ANYIF 1/20 0 EPIPE / ENOTCONN

The client's own sockets (main's code) split the same way:

  • About 20 sockets dialed while the tunnel came up each sent one echo request, then failed every send with EPIPE and read nothing.
  • The sockets dialed from 00:11:08 on sent 70 to 95 requests each and read the Mac's echoes, about 1000 messages apiece.

The probe's first TCP exchange rides one of those first sockets, so the VPS smoke stopped there.

Other observations from the run:

  • The sandbox let a wildcard bind and an unconnected receive through. An Apple forum report from 2019 had both failing with EPERM.
  • NECP scoped a socket to en0 when it bound (D) and when it connected (F).

Cause

EPIPE on send together with ENOTCONN on receive is XNU's defunct socket (SOF_DEFUNCT, sosend/soreceive in uipc_socket.c, xnu-12377.121.6). NECP defuncts sockets in necp_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 calls sosetdefunct(..., 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:

  • The local address. XNU's rip_connect records only the peer and leaves an ICMP socket's local address empty. Connecting a UDP socket fills its local address in.
  • The re-evaluation. NECP evaluates a socket again when its state or the policies change; the tunnel coming up does both. A flow with no local address then matches the tunnel's policy, and NECP defuncts the socket.
  • The setups that survived. G kept sending and reading because it is bound to en0's address. D and E are unconnected, which that rule never touches, and NECP checks each of their packets with the source address it actually leaves from.
  • What is inferred. The policies themselves are installed from userspace and aren't visible in XNU, so "a flow with no local address matches the tunnel's policy" is inferred from G against A.

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, function attach, and applies on Apple systems only:

  • Bind and send. The client's ICMP socket is bound to the wildcard and left unconnected. Sends use sendto to the server address.
  • Receive. The reader uses recvfrom and 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.
  • Refused bind. Where the wildcard bind is refused, as the 2019 forum report describes inside an extension, the socket is connected as before. Current iOS on the campaign's iPhone allowed the bind.
  • Interface following. xicmp: bind the client's ICMP sockets like UDP inside an Apple Network Extension #295's interface following (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:

  • Linux and Android: unchanged. The socket is still connected. Android keeps VpnService.protect.
  • The macOS CLI: its sockets are unconnected too. A connected one falls under the same NECP rule whenever a VPN's policies change (not checked on macOS), and an unconnected wildcard socket is what Xray-core uses there. The packets don't change. The socket now also reads echo replies from other addresses and drops them.

Tests

New tests in crates/xray-transport/src/xicmp/client/tests.rs:

  • Apple: the socket is left unconnected, for IPv4 and IPv6 (peer_addr is NotConnected).
  • Apple: a reply carrying the client's identifier and a packet, sent from another address of the host, is dropped; the same from the server's address is read.
  • Elsewhere: the socket is connected to its address.

Mutation checks, run locally on macOS:

  • Accepting replies from any address fails the source test.
  • Connecting instead of the wildcard bind fails the unconnected test.

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 -- --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 for aarch64-apple-ios
  • clippy for aarch64-linux-android
  • The scripted gates from ci.yml under python3.12

Device 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.

  • Round trip on the iPhone: pass. The full xicmp smoke ran against Xray-core v26.11.9's xicmp server on the Linux VPS, set up as ios-xicmp attempt-2.
    • The result was done:passed, with all seven transitions passing: ipv4-tcp, ipv6-tcp, domain-destination, ipv4-udp, ipv6-udp, routed-dns and start-stop.
    • Over 3 cycles: TCP 18/18, UDP 12/12, dns-anchor 6, connection close verified 3/3.
    • The server accepted 139 flows. tcpdump on the VPS saw 4295 echo requests from the home NAT and 9020 replies to it, 4807 of them from the xicmp server.
    • With xicmp: bind the client's ICMP sockets like UDP inside an Apple Network Extension #295 alone, the same setup timed out at the first TCP exchange.
  • macOS CLI built from 3568138, same server: pass. 4096/4096 bytes echoed in 0.3 s; the server accepted 1 flow.
  • Not checked: Wi-Fi → cellular during an xicmp run, which is not in the v0.10 smoke set.

Docs

  • The CHANGELOG.md entry under 0.10.0 now describes the cause and fix together.
  • The xicmp section of 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

…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.
@aimalygin
aimalygin merged commit 46c38c1 into main Nov 15, 2026
15 checks passed
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.
@aimalygin
aimalygin deleted the claude/xicmp-ios-receive branch November 23, 2026 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant