Skip to content

finalmask: UDP masks on freedom and WireGuard - #246

Merged
aimalygin merged 8 commits into
mainfrom
claude/finalmask-udp-direct
Nov 10, 2026
Merged

aimalygin merged 8 commits into
mainfrom
claude/finalmask-udp-direct

Conversation

@aimalygin

@aimalygin aimalygin commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Xray-core v26.10.31's FinalMask.DialUDP wraps every UDP socket it dials, so freedom's flows and WireGuard's socket take finalmask.udp there. Here the parser (WireGuard) or Core::build (both) refused them, and the freedom noises deprecation warning pointed to a config that then failed to start.

How

xray_transport::MaskedUdpSocket runs the QUIC carriers' masks one datagram at a time: noise, header-custom, Salamander with Gecko, sudoku, xdns and xicmp. It uses the carriers' own code in Xray-core's order: carrier::wrap_masks, with the xdns and xicmp clients from the new carrier::tunnel.

  • The socket under the masks. It is the mask's own socket over tokio, connected to the peer, and has none of Quinn's UDP state:
    • no don't-fragment bit and no GRO;
    • each receive is cut to the caller's buffer, with no extra buffer per flow;
    • the ICMP errors a connected socket reports count as loss, as on an unmasked flow.
  • Why not quinn-udp. A first version received through quinn-udp and hit 0.5.15's Apple spin on a cut datagram. deps: quinn-udp 0.5.16, whose Apple receive no longer spins after a cut datagram #245 has since fixed that in main, and this socket no longer goes through quinn-udp's receive.
  • A held send reads. A standalone header-custom holds a send until the server answers its request, and only a read takes the answer in. Xray-core's mask reads in a goroutine of its own meanwhile. Here, under a standalone mask, a held send reads in the receiver's place:
    • the masks take the answer in;
    • what they hand over is kept for the receiver, up to 256 KiB, and the rest dropped;
    • the receiver is woken for it, and again once the send is through, since the socket wakes only the last task waiting on it.

Freedom. Every UDP flow goes through the masks: flows from SOCKS and TUN, the PROXY header, freedom's noises, and DNS queries routed through freedom, the core's own and the TUN DNS proxy's. A masked flow sends only to its destination and takes replies only from it.

WireGuard. Its one socket goes under the masks.

  • The masks take one peer; a second peer fails the core at start.
  • The socket and its mask state open anew at every rebind.
  • The parser now takes sockopt and finalmask.udp in WireGuard's streamSettings, and refuses finalmask.tcp and quicParams.

Refused on both, in the parser, in Core::build and in the outbound constructors:

  • udphop and realm: each needs a driver that replaces or introduces the socket, and only the QUIC carriers have one;
  • mkcp-legacy, as on every carrier except mKCP;
  • sudoku under xdns or xicmp: its datagrams are larger than either can carry.

Differences from Xray-core

These are written up in docs/xray-core-differences.md:

  • Replies. Ours come only from the destination. Xray's masked socket also reads other senders.
  • WireGuard peers. One peer here. Xray writes every peer's packets to the first peer's endpoint.
  • Refused masks. The ones listed above.
  • Datagram size. A Salamander datagram may exceed 4096 bytes here. Xray's header masks write at most 4096 bytes per datagram, so its server cannot answer, for example, a 1200-byte payload under sudoku inside Salamander.
  • Standalone header. A held send keeps up to 256 KiB for the receiver and drops the rest, where Xray's reader goroutine holds 16 datagrams and stops reading while they wait. An unanswered request goes out again after a second, as on the QUIC carriers; Xray's write waits for good.

Evidence

  • Masked-socket unit tests:
    • noise inside and outside Salamander, and salted replies;
    • other senders' and unmaskable datagrams are dropped;
    • a long datagram is cut, or dropped under Salamander;
    • a refused datagram counts as loss; mutating either handler makes the test fail;
    • a send a standalone header holds takes the answer in itself and keeps an earlier reply for the next receive;
    • a receiver in another task, as WireGuard's is, gets what a held send read, and waits on the socket again once the send is through;
    • held datagrams stop at the budget;
    • five mutations of the held-send code each fail at least one of these tests;
    • udphop is refused.
  • Core tests:
    • a TUN UDP flow through freedom runs under noise inside a header-custom prefix, and fails once the masks are removed from the TUN path;
    • a TUN UDP flow through freedom waits out a standalone header; before the fix it hung;
    • a query the TUN DNS proxy sends through freedom goes under a standalone header and its answer comes back; before the fix it went out unmasked;
    • Core::build accepts freedom and one-peer WireGuard under masks, and refuses two peers.
  • Live against Xray-core 26.10.31 (a4a3dff), via direct_udp_masks:: in check-hysteria-interop.sh:
    • freedom through Xray's tunnel inbound under the same masks: Salamander, noise inside it, header-custom in prefix and standalone mode, sudoku, and sudoku inside Salamander;
    • WireGuard against Xray's WireGuard inbound, TCP and UDP: Salamander with noise on either side, header-custom in both modes, and sudoku;
    • without the held-send read, the freedom standalone case fails;
    • on both, a different Salamander password carries nothing;
    • three runs in a row pass;
    • an earlier version on an unconnected socket once lost the sudoku case to the known macOS dual-stack collision. Xray's [::]:0 freedom socket got our IPv4 socket's port, and our socket took the reply. A connected socket does not take it.
  • WireGuard live suites pass against Xray: xray-wireguard lib, lifecycle, interop and multi-peer, plus wireguard_runtime_.
  • Again against Xray-core 26.11.9 (676eaf66), main's new pin, after merging main: direct_udp_masks:: 2/2.
  • After merging main (479b984, with HelloRetryRequest as uTLS answers it: one key share, the cookie's place, padding per flight (stacked on #256) #258 and Land #251: transport hardening against a hostile peer (mKCP, xmc, realm, MASQUE, HTTP inbound, xdns, reverse, DNS, sockopt) #260):
  • go-oracles in CI passed in full for the first time on af48276, xicmp's live checks and the WireGuard direct fixture included. Before that it had stopped at check-realm-interop.sh on every run of this PR; on c10f9d8 the steps from there on were run locally against 26.11.9 and all passed.

Also in this PR

🤖 Generated with Claude Code

Anton Malygin added 5 commits November 9, 2026 19:19
Freedom's UDP flows (SOCKS, TUN, DNS through freedom) and WireGuard's
socket take finalmask.udp, as Xray-core's FinalMask.DialUDP wraps every
UDP socket it dials. xray_transport::MaskedUdpSocket runs the QUIC
carriers' masks one datagram at a time over its own connected socket,
without Quinn's UDP state.

udphop, realm and mkcp-legacy stay refused on these carriers, sudoku
under xdns or xicmp too, and WireGuard under masks takes one peer.
A standalone header-custom holds a send until the server answers its
request, and only a read takes the answer in. Freedom's flows read only
between their sends, so every SOCKS, TUN and DNS flow under such a mask
hung; WireGuard's receive task also sends, under its peer's lock.
Xray-core's mask reads in a goroutine of its own meanwhile.

Under a standalone mask, a held send now reads in the receiver's place.
The masks take the answer in; what they hand over is kept for the
receiver, up to 256 KiB, and the receiver is woken for it and again once
the send is through, since the socket wakes only the last task waiting
on it.

The TUN DNS proxy sent its queries through freedom on a plain socket, so
every query but a plain A or AAAA one bypassed the masks. It now opens
freedom's masked socket, as the other freedom paths do.
Anton Malygin added 3 commits November 10, 2026 10:30
The test parsed its realm mask from a freedom outbound over XHTTP H3 and
moved it onto the VLESS config it runs. Freedom now takes finalmask.udp
for its own UDP flows and refuses realm there, so that parse failed and
check-realm-interop.sh stopped go-oracles. A VLESS outbound over the same
transport, which is what the test runs, reads the mask as before.
@aimalygin
aimalygin merged commit ae11454 into main Nov 10, 2026
16 checks passed
aimalygin pushed a commit that referenced this pull request Nov 14, 2026
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.
@aimalygin
aimalygin deleted the claude/finalmask-udp-direct branch November 15, 2026 18:48
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