Skip to content

xicmp: bind the client's ICMP sockets like UDP inside an Apple Network Extension - #295

Merged
aimalygin merged 3 commits into
mainfrom
claude/xicmp-ios-interface-648eb0
Nov 14, 2026
Merged

aimalygin merged 3 commits into
mainfrom
claude/xicmp-ios-interface-648eb0

Conversation

@aimalygin

@aimalygin aimalygin commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

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/, see campaign-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).
  • Same server, same home NAT: the candidate's macOS CLI works (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. With streamSettings.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_SCOPED result: inp_update_necp_policy binds the socket with inp_bindif (the same INP_BOUND_IF that IP_BOUND_IF sets) and marks it INP2_SCOPED_BY_NECP. The socket layer runs this from sobind/soconnect/sosend for every PF_INET socket, ICMP included (checked in xnu-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_connect only records inp_faddr, and rip_output passes an interface to ip_output only when INP_BOUND_IF is set. So ip_output looks 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.protect excludes any socket from the VPN, ICMP included. The macOS CLI has no tunnel routes.

Xray-core v26.11.9

transport/internet/finalmask/xicmp/client.go opens one unconnected socket per family with icmp.ListenPacket("udp4", "0.0.0.0"). That socket is bound to the wildcard, never bound to an interface, and not reached by sockopt. 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 in docs/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:

  • Sockets the protector already bound are left alone. After the protector, if the ICMP socket is already bound (IP_BOUND_IF/IPV6_BOUND_IF, e.g. by sockopt.interface), it keeps that interface and is never re-checked.
  • Otherwise the socket takes the interface the system gives UDP. The client asks the system with a UDP socket built like the carriers' sockets: bound to the wildcard, passed through the protector, then connected to the server address on port 9. The connect sends nothing. The client reads that socket's 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.
  • Sends re-check at most once a second. A send re-asks if a second has passed since the last check (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.
  • Why not a host-provided interface: this needs no network-change signal from the host, so there is no FFI/ABI or mobile SDK change. The system's own answer for a UDP socket of the same process is exactly the interface the fix needs.

Behaviour elsewhere:

  • Android, Linux and every non-Apple build: unchanged. Android keeps VpnService protect.
  • macOS CLI: each send tries a lock, and the CLI opens one extra UDP socket per dial and at most one per second while sending (one socket and three calls each). The packets and their routes don't change.

Tests

Apple-only tests in crates/xray-transport/src/xicmp/client/tests.rs use a protector that does what NECP does in the extension: it binds sockets that have a port (UDP) and leaves the ICMP socket alone.

  • The ICMP socket takes the interface the system scopes UDP sockets to, for IPv4 and IPv6.
  • It stays unbound while UDP sockets do. Its sends still check, and it takes the interface once the system starts scoping.
  • A socket the protector bound (sockopt.interface) keeps its interface, and the system is never asked.
  • It moves to the new interface once the recheck period has passed, and stays bound when the system scopes none.
  • A client's send moves the socket once the period has passed.

Mutation check: dropping the bind at open fails three of them. CI doesn't run Rust tests on macOS (the apple job 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 for aarch64-linux-android; and the scripted gates from ci.yml under 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:

  1. LAN check. Run results/ios/diagnostics/xicmp-route/run.sh <name> plain, i.e. the config without sockopt.interface. Expect the Mac's echo-in delta to be well above 0, as in bound-1 (1239). plain-1 was 0. Optionally repeat bound to confirm sockopt.interface still works.
  2. xicmp smoke run against a Linux xicmp server (Xray-core v26.11.9's server as root with a raw ICMP socket, e.g. the VPS setup of 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.
  3. Optional: switch Wi-Fi → cellular during an xicmp run. The connection should continue within about a second of the switch. This is not part of the v0.10 smoke set.

Docs

A CHANGELOG.md entry under 0.10.0; the xicmp section of docs/config-compatibility.md (iOS row and the interface binding); and docs/xray-core-differences.md (the deliberate difference).

Second commit: interop harness port race (unrelated to xicmp)

The first CI run failed go-oracles in "Verify Shadowsocks 2022 TCP, UDP and TUN" (run 43619978436). shadowsocks_runtime_udp_reopens_after_port_change_and_survives_server_restart panicked with "reference exited", and Xray's log showed listen tcp 127.0.0.1:45109: bind: address already in use.

The shared harness crates/xray-core-rs/tests/trojan_runtime/support.rs picks the reference's port by binding 127.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 failed v08_carriers on 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:

  • A start tries again on a fresh port, up to five times.
  • A restart must keep its port, so it respawns for about two seconds.
  • Any other exit still fails the test with the reference's log.

Checked locally:

  • A temporary hook stole the start port and held the restart port for 250 ms. The start moved to a new port, and the restart took its port back on the 4th attempt.
  • CI's Shadowsocks step passes against Xray 26.11.9: scripts/check-shadowsocks2022-interop.sh's cargo command, run with a local copy of the reference binary.
  • fmt and clippy are clean on the xray-core-rs test targets.

Third commit: mKCP header-custom interop livelock (unrelated to xicmp)

The second CI run failed go-oracles in "Verify mKCP masks and live mKCP carriers" (run 43621093196). kcp_header_custom_udp_modes_and_mask_order timed 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_masks already 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:

  • Before: the prefix cases dropped 1,720–10,654 server datagrams against about 120 accepted. Resends kept the tokens moving, cases took 15–56 s, and one run hit the 60 s timeout.
  • After: prefix mode now reuses nothing (rand on the server side, the stream form direct_udp_masks uses). 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 whole mKCP interop step (13 tests) passes against Xray-core 676eaf66.

The client's behaviour is unchanged; it matches Xray-core's.

🤖 Generated with Claude Code

Anton Malygin 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.
@aimalygin
aimalygin merged commit ee58f87 into main Nov 14, 2026
16 checks passed
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.
@aimalygin
aimalygin deleted the claude/xicmp-ios-interface-648eb0 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