Skip to content

Transport: Pass UDP destination to dialer controllers - #7201

Merged
RPRX merged 2 commits into
XTLS:mainfrom
eliotcougar:fix/udp-dialer-controller-destination
Oct 21, 2026
Merged

RPRX merged 2 commits into
XTLS:mainfrom
eliotcougar:fix/udp-dialer-controller-destination

Conversation

@eliotcougar

Copy link
Copy Markdown
Contributor

Native TUN's autoOutboundsInterface can break UDP connections to local DNS servers and local SOCKS relays. TCP to the same services continues working. The TUN controller explicitly exempts loopback destinations, but it is given the wrong address for UDP.

DefaultSystemDialer opens UDP sockets with ListenPacket, whose Control address is the local bind endpoint, often 0.0.0.0:0. Forwarding that argument to dialer controllers makes TUN miss its loopback exemption and bind the socket to the physical interface. TCP controllers already receive the remote endpoint. On Windows the resulting UDP send fails with:

write udp [::]:<local-port>->127.0.0.1:<server-port>: wsasendto: The requested address is not valid in its context.

Pass the resolved destAddr.String() to UDP dialer controllers, consistent with the TCP path and the existing outbound socket-options call. Document that callback contract. Socket creation, explicit source binding, controller error handling and listener-controller arguments are unchanged; no configuration fields are added.

Native reproduction

Tested on Windows 10/amd64 in Hyper-V, using Go 1.27.2 builds from unmodified v26.10.17 (92ccc939) and from that source with only this destination-address correction. The test core processes contain no application-specific patches.

A native TUN with an isolated 198.18.247.1/30 gateway and autoOutboundsInterface: "Ethernet" installs the controller without taking over default routes or system DNS. After waiting for TUN to report ready, loopback UDP echo/DNS fixtures were exercised through both SOCKS and the native TUN data path. A second unmodified core supplied the local SOCKS-chain hop.

Configuration Successful probes Failed probes
Unmodified core, no TUN 11 0
Unmodified core, TUN controller active 4 13
Destination correction, TUN controller active 17 0
Unmodified core, TUN controller active, repeated 4 13
Unmodified core, no TUN, final control 11 0

Both stock TUN runs failed the same cases: UDP to 127.0.0.1, 127.0.0.2 and ::1; local DNS forwarding; the built-in DNS client using a local resolver; and a local SOCKS UDP hop. Failed fixtures received zero requests. Direct UDP outside the test core, TCP by literal IP, and TCP through the SOCKS chain remained healthy. Hostname-based TCP also failed when its local DNS lookup failed, explaining how this can appear to break TCP connectivity too.

This affects other inbounds in the same core because TUN registers a process-wide controller. It is not limited to packets entering the TUN adapter. A fresh build of the final PR source separately passed all 17 native probes, with graceful core shutdown and the original guest runtime, adapters and routes preserved. These are controlled functional tests, not throughput measurements or an estimate of failure frequency.

Regression coverage and validation

  • The new test fails before the fix and passes after it for IPv4/IPv6, with wildcard and explicit source addresses. It also verifies that explicit source binding is preserved. IPv6 cases skip only if the local IPv6 fixture cannot be opened.
  • go test -mod=readonly ./transport/internet -count=100 -shuffle=on -timeout=90s passed; the SOCKS and DNS-outbound packages passed too. Freedom compiles and has no test files.
  • go vet ./transport/internet, upstream vformat, and git diff --check passed.
  • Transport tests cross-compiled for Linux/amd64 and macOS/arm64; they were not executed there. The race detector and full suite were not run locally.
  • The broader app/dns suite stops at the existing TestTCPLocalNameServerWithIPv6Override, which receives an empty AAAA answer from its public resolver here. The same failure was reproduced on the unmodified base.

UDP sockets use ListenPacket, whose Control callback supplies the local
bind address. Passing that address to dialer controllers prevents the
native TUN controller from recognizing loopback destinations, unlike TCP.
With autoOutboundsInterface enabled, local UDP DNS and SOCKS relays can
then be bound to the physical interface and fail to send on Windows.

Supply the resolved remote endpoint to the controllers and document their
address contract. Keep socket creation, explicit source binding and
listener callbacks unchanged.

Add IPv4/IPv6 regression cases for wildcard and explicit source addresses,
including a check that the selected local source remains intact.
@eliotcougar

eliotcougar commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

This is the last issue I encountered for now... Maybe you have a better way to address it.

I use a dnscrypt server on localhost, and it is inaccessible when TUN is used.

@eliotcougar

Copy link
Copy Markdown
Contributor Author

Question for the future: should I tell Codex to exclude tests from PRs?

@Fangliding

Fangliding commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

AI written tests always have the possibility of using impossible upstream calls to trigger bugs, and can lead to infinite expansion of test cases
Sometimes I may read them to understand submitted bugs, but overall they are not suitable for being merged into long-term mainline testing. It will be incorrect if say they are completely meaningless, just do some clean

@eliotcougar

Copy link
Copy Markdown
Contributor Author

AI written tests always have the possibility of using impossible upstream calls to trigger bugs, and can lead to infinite expansion of test cases Sometimes I may read them to understand submitted bugs, but overall they are not suitable for being merged into long-term mainline testing. It will be incorrect if say they are completely meaningless, just do some clean

Noted...

I perfectly understand what you mean. I periodically have to do cleanup passes to remove trivial tests or tests for situations that can never happen in reality. It is very annoying, but I often skip over them, especially when I vibe-code in parallel with my main job... What a time...

@Meo597

Meo597 commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

In the old days, the network was slow, commits took forever, and a week was only enough for one PR.

@RPRX
RPRX requested a review from Fangliding October 21, 2026 09:16
@RPRX

RPRX commented Oct 21, 2026

Copy link
Copy Markdown
Member

这报错 #7160 (comment) 会不会有关

@RPRX RPRX changed the title Transport: pass UDP destinations to dialer controllers Transport: Pass UDP destination to dialer controllers Oct 21, 2026
@RPRX
RPRX merged commit 23e2292 into XTLS:main Oct 21, 2026
@eliotcougar

Copy link
Copy Markdown
Contributor Author

这报错 #7160 (comment) 会不会有关

This PR fixes it only for loopback connections because it skips binding entirely.
The remaining problem is that Go creates a dual-stack UDP socket. Xray’s TUN helper then attempts both IPv6 and IPv4 interface binding. If IPv6 is disabled on that adapter, the IPv6 operations fail even for an IPv4 destination.

@RPRX

RPRX commented Oct 21, 2026

Copy link
Copy Markdown
Member

@eliotcougar 想想咋修比较好

@eliotcougar

Copy link
Copy Markdown
Contributor Author

Hi @RPRX — I’m an AI coding agent based on Codex GPT6.1-Sol, assisting @eliotcougar with this investigation.

I reproduced the remaining failure using the actual Windows TUN binding helper and default dialer: an IPv4 UDP destination gets a dual-stack socket (udp6), then both IPV6_UNICAST_IF and IPV6_MULTICAST_IF fail with WSAEINVAL (10022) on an adapter with IPv6 disabled, while the IPv4 options succeed. These were socket/control-path probes stopped before bind/send, not end-to-end traffic tests.

I’m working on a separate, focused family-aware binding fix. The required interface binding must still fail closed; simply ignoring socket-option errors would reintroduce the loop/leak problem. I’ll also check dual-stack UDP and multi-destination usage before proposing the change, and submit a draft with native before/after evidence.

@Anthrop44

Copy link
Copy Markdown

@eliotcougar 没绷住,你还是换个astra来吧显得尊重些

@eliotcougar

Copy link
Copy Markdown
Contributor Author

@eliotcougar 没绷住,你还是换个astra来吧显得尊重些

I would love to, but Astra drains my weekly budget in a day... 😄

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.

5 participants