Skip to content

sockopt.interface: when the interface does not exist, the dial silently goes unbound (can loop back into TUN) #7160

Open

Description

@SashaAch

Integrity requirements

  • I have read all the comments in the issue template and ensured that this issue meet the requirements.
  • I confirm that I have read the documentation, understand the meaning of all the configuration items I wrote, and did not pile up seemingly useful options or default values.
  • I provided the complete config and logs, rather than just providing the truncated parts based on my own judgment.
  • I searched issues and did not find any similar issues.
  • The problem can be successfully reproduced in the latest Release

Description

If an outbound has streamSettings.sockopt.interface set and that interface does not exist at dial time, Xray does not fail the dial. It logs failed to apply socket options > failed to get interface ... at Info level and opens the connection without any binding, so it follows the system routing table.

transport/internet/system_dialer.go (v26.10.10, lines 134-139; same in v26.9.9) only logs the error returned by applyOutboundSocketOptions and goes on to dialer.DialContext. The UDP path does the same (lines 77-81). The error itself comes from net.InterfaceByName in transport/internet/sockopt_darwin.go:136-140. From reading the source, Linux takes the same path (the error is logged, not returned); I tested macOS only.

Why this matters. A typical full-tunnel setup on a desktop routes everything into a TUN interface handled by Xray (default route split as 0.0.0.0/1 + 128.0.0.0/1 into the TUN), and binds outbounds with sockopt.interface so that their own sockets do not enter the TUN again. If the bound interface goes away, the fallback sends the outbound's sockets into the TUN, Xray routes them to the same outbound, and the connection loops.

We hit this loop in practice on macOS. A separate OpenVPN client provided a corporate network through its own utun interface, and the corporate subnet was routed to a freedom outbound so that it would follow that VPN's route. At night the OpenVPN client lost its connection and its utun interface was removed. From then on, every connection to the corporate subnet (including DNS queries to the corporate DNS server) entered the TUN, came back to the same outbound and entered the TUN again. When we looked, there were about 8,000 looped connections, all 16,384 ephemeral ports were in use, and other processes on the host got EADDRNOTAVAIL (Can't assign requested address) on connect until the loop was broken by restarting Xray.

In that incident the outbound had no sockopt.interface at all, so the loop itself is a config problem. The obvious fix is to bind the outbound to the other VPN's interface ("interface": "utunN"), so that it can never fall into the TUN. That does not work because of this issue: once the interface disappears, the binding is skipped and the loop is back. The only visible sign is an Info-level log line, which is hidden at the usual warning level.

Expected: if the interface named in sockopt.interface cannot be found, the dial fails (fail closed), as a bound socket does when its interface has no route. At the very least the error should be logged as a warning. A silent fallback to the routing table defeats the purpose of binding: it can send traffic into a tunnel (loop) or past it (leak).

Reproduction Method

No server and no TUN are needed. Run Xray with the config below (interface utun99 does not exist) and send a request through the SOCKS inbound:

curl --socks5-hostname 127.0.0.1:1080 http://example.com/

Result: HTTP 200. The log shows that the interface was not found and the connection was opened anyway; its local endpoint is the address of whatever interface the routing table chose (on our machine, the address of a TUN interface that held the default route).

For comparison, with "interface": "en0" the same request goes out through en0 (local endpoint is en0's address) and no error is logged.

Expected result: the connection through the outbound bound to utun99 fails.

Tested on macOS arm64 with Xray 26.10.10 (701af60, the latest release) and 26.9.9 (52a412d): same result with both.

Client config

Details

{
  "log": {"loglevel": "debug"},
  "inbounds": [{"listen": "127.0.0.1", "port": 1080, "protocol": "socks", "settings": {}}],
  "outbounds": [{"protocol": "freedom", "streamSettings": {"sockopt": {"interface": "utun99"}}}]
}

Server config

Details

Not needed: the reproduction uses a freedom outbound only.

Client log

Details

Xray 26.10.10 (Xray, Penetrates Everything.) 701af60 (go1.27.2 darwin/arm64)
A unified platform for anti-censorship.
2026/10/15 09:13:07.993097 [Info] infra/conf/serial: Reading config: &{Name:cfg1080-utun99.json Format:json}
2026/10/15 09:13:07.993480 [Debug] app/log: Logger started
2026/10/15 09:13:07.993612 [Debug] app/proxyman/inbound: creating stream worker on 127.0.0.1:1080
2026/10/15 09:13:07.993745 [Info] transport/internet/tcp: listening tcp on 127.0.0.1:1080
2026/10/15 09:13:07.993835 [Warning] core: Xray 26.10.10 started
2026/10/15 09:13:09.516363 [Info] [3266420241] proxy/socks: TCP Connect request to tcp:example.com:80
2026/10/15 09:13:09.516442 [Info] [3266420241] app/dispatcher: default route for tcp:example.com:80
2026/10/15 09:13:09.516552 from tcp:127.0.0.1:<port> accepted tcp:example.com:80
2026/10/15 09:13:09.516680 [Info] [3266420241] transport/internet/tcp: dialing TCP to tcp:example.com:80
2026/10/15 09:13:09.516712 [Debug] [3266420241] transport/internet: dialing to tcp:example.com:80
2026/10/15 09:13:09.621224 [Info] [3266420241] transport/internet: failed to apply socket options > transport/internet: failed to get interface utun99 > route ip+net: no such network interface
2026/10/15 09:13:09.621871 [Info] [3266420241] proxy/freedom: connection opened to tcp:example.com:80, local endpoint <TUN interface address>:<port>, remote endpoint <example.com address>:80
2026/10/15 09:13:09.621988 [Debug] [3266420241] proxy: CopyRawConn (maybe) readv
2026/10/15 09:13:09.727896 [Debug] app/log: Logger closing

For comparison, the same request with "interface": "en0" (no error logged):

Details

2026/10/15 09:13:11.342041 [Info] [3042018327] proxy/freedom: connection opened to tcp:example.com:80, local endpoint <en0 address>:<port>, remote endpoint <example.com address>:80

Server log

Details

Not needed.

Activity

  1. RPRX commented on Oct 15, 2026

    @RPRX
    Member

    终于有人提这个问题了,我这边 Windows 上总是有个目标为 IPv6 27xxx 端口的 UDP 绑 interface 失败然后不断刷,还以为你们都没遇到

    所以为啥会绑 interface 失败?本来想修这个问题来着,风扇那个 commit 治标不治本,不过那个也得有

  2. Fangliding commented on Oct 15, 2026

    @Fangliding
    Member

    2. 所以为啥会绑 interface 失败?本来想修这个问题来着

    99%的情况是interface down了 比如他自己提到的VPN关了

  3. SashaAch commented on Oct 15, 2026

    @SashaAch
    Author

    In my case the interface is not down, it is gone. When the OpenVPN client disconnects (here it lost its server and then failed to re-authenticate), macOS destroys its utun interface (if_detached in the system log). From then on net.InterfaceByName("utunN") fails with no such network interface (transport/internet/sockopt_darwin.go:136-140), and system_dialer.go only logs that at Info and dials without the binding (lines 134-139 for TCP, 77-81 for UDP).

    As long as the interface exists, even if it is down, InterfaceByName succeeds and IP_BOUND_IF is set, so the socket stays bound. The fallback happens only when the interface has disappeared. That is typical for tunnel interfaces (utun/tun/wg/ppp), which are removed when the VPN disconnects and may come back under another name, and for USB or Thunderbolt Ethernet that gets unplugged.

    So the fix that would have helped me is to fail the dial when the configured interface cannot be found, instead of dialing unbound.

  4. VariatezzZ commented on Oct 16, 2026

    @VariatezzZ

    3. > 2. 所以为啥会绑 interface 失败?本来想修这个问题来着

    99%的情况是interface down了 比如他自己提到的VPN关了
    

    在windows 使用tun,一旦网络断开,网卡不是up状态,windows的各种组播广播udp流量会在tun回环,导致xray内存暴涨到1G,1分钟error.log就能写10MB。

  5. RPRX commented on Oct 16, 2026

    @RPRX
    Member

    等 @Fangliding 把 #7161 (comment) 弄好吧,我可以先看看 #7162 ,这俩现在是 release-blocker

  6. RPRX commented on Oct 16, 2026

    @RPRX
    Member

    在windows 使用tun,一旦网络断开,网卡不是up状态,windows的各种组播广播udp流量会在tun回环,导致xray内存暴涨到1G,1分钟error.log就能写10MB。

    我遇到的情况似乎不是这种,是剩下的 1%

    好吧看了下 WLAN 已经知道是什么原因了,TUN 有 IPv6 但 WiFi 没,#6853 的 "misconfigtun" 可以只写路由 IPv4 进来故而自动 block IPv6

    @patterniha 我觉得类似 #7092 ,autoOutboundsInterface 应当识别目标网卡是否有 IPv6 然后决定是否路由 IPv6 进来?不过这样可能会 leak

    但是如果应用程序以为有 IPv6 然后选择走 IPv6 又会导致实际上连不上,有没有什么好的解决方案

  7. RPRX commented on Oct 16, 2026

    @RPRX
    Member

    这里的矛盾点是 TUN 该接管所有 v4v6 流量才不会 leak,但这样又会导致某些应用程序以为有 IPv6(可能它检测了下路由表/网卡?不确定)

    像只有 WiFi IPv4 这一个出口的情况当然不会 leak,但如果有另一个出口有 IPv6 那就 leak 了

  8. RPRX commented on Oct 16, 2026

    @RPRX
    Member

    或许如果 autoOutboundsInterface 的目标网卡没有 IPv6,仅路由 IPv4 进来+把所有网卡的 IPv6 disable 了就能解决问题,只是有点不优雅

  9. RPRX commented on Oct 16, 2026

    @RPRX
    Member
  10. RPRX commented on Oct 16, 2026

    @RPRX
    Member

    或者,Windows 上可以 system-wide disable IPv6 #6874

  11. RPRX commented on Oct 16, 2026

    @RPRX
    Member

    我说的那个场景没有治本所以先 reopen,另外我记得 autoOutboundsInterface 对 interface change 的情况有监听来着 @patterniha 你看下

  12. reopened this on Oct 16, 2026
  13. patterniha commented on Oct 17, 2026

    @patterniha
    Collaborator

    @patterniha 我觉得类似 #7092 ,autoOutboundsInterface 应当识别目标网卡是否有 IPv6 然后决定是否路由 IPv6 进来?

    I think the TUN should keep routing IPv6 even if the outbound interface has no IPv6. Suppose a program sends data to an IPv6 address: we want it to go through our proxy server, which has IPv6, not to bypass Xray or fail on our device. Xray carries it inside its connection to the server, which can be plain IPv4, and the server reaches the IPv6 address on its side. So regardless of whether our PC supports IPv6, the TUN should accept that connection and route it to the proxy; not routing IPv6 would break IPv6 through the proxy.

  14. RPRX commented on Oct 17, 2026

    @RPRX
    Member

    代理服务器当然绝大多数都是 v4v6 双栈,然而上面的重点是本地直连(Direct outbound)流量,TUN 有 v6 但物理出口网卡没 v6 的情况

  15. patterniha commented on Oct 17, 2026

    @patterniha
    Collaborator

    17. 代理服务器当然绝大多数都是 v4v6 双栈,然而上面的重点是本地直连(Direct outbound)流量,TUN 有 v6 但物理出口网卡没 v6 的情况

    When there is a domain (sniffing or fakedns), freedom already handles it: with AsIs, Go's dialer falls back to IPv4, and with UseIP/ForceIP in sockopt.domainStrategy, Xray's happy eyeballs does, since the IPv6 attempt now fails at once when binding fails.

    The real problem is a direct connection to a bare IPv6 address that can't be sniffed: there is no IPv6 path, so it can't work, and the best we can do is fail it as early as possible so that the app falls back to IPv4. But the TUN comes before routing: it has already completed the TCP handshake before Xray even decides where the connection goes, so by the time the direct dial fails, it's too late for the app to notice.

  16. RPRX commented on Oct 18, 2026

    @RPRX
    Member

    @patterniha 你描述的矛盾类似于 Xray 进行 MITM 时我们所遇到的 ALPN 问题(得先连接目标才知道是否仅 h1)

    我的意思是有些程序可能看网卡有 IPv6 地址就 prefer 它了,不过这种情况的话直接让 TUN reject IPv6 也不好,会影响真正走代理的流量

    proxy/tun: [tun] falied to set interface > multierr: An invalid argument was supplied.

    以上报错我本来以为是 IPv6 UDP 专属,看了下 IPv4 UDP 似乎也出现了

  17. RPRX commented on Oct 28, 2026

    @RPRX
    Member

    proxy/tun: [tun] falied to set interface > multierr: An invalid argument was supplied.

    以上报错可能由 #7214 修复了,还没测,不确定

    我的意思是有些程序可能看网卡有 IPv6 地址就 prefer 它了

    不确定有没有这样的程序,比如不确定有没有该被分流到本地直连的应用程序就是 dial IPv6 且不回退到 IPv4(没有 happyEyeballs)

    Reopen,直到上述情况被妥善处理

  18. reopened this on Oct 28, 2026
  19. RPRX commented on Nov 4, 2026

    @RPRX
    Member

    proxy/tun: [tun] falied to set interface > multierr: An invalid argument was supplied.

    已确认 #7214 并未完全修复这一报错,剩下的部分可能还是由于真实物理网络没有 IPv6 但 Xray direct 出站又想发 IPv6 所导致的

  20. mar0ls commented on Nov 21, 2026

    @mar0ls

    Looked at the TUN code on current main (02cf0f6) to see where the ordering comes from.

    The SYN-ACK goes out as soon as the SYN comes in (stack.handleTCP → sendSyn, stack.go:354). HandleConnection only starts once the final ACK arrives (tcp_lightweight.go:521), so by the time routing/dial happens the app's connect() has already returned success.

    When the dial then fails, nothing tells the TUN side directly: the outbound's Interrupt(link.Writer) is a no-op on TCPWriter, and DispatchLink returns nil regardless (default.go:324-376). The conn just gets closed by HandleConnection's deferred conn.Close(), which sends a FIN, or an RST if the client already sent data nobody read (tcp_lightweight.go:1230-1236). Either way the app gets it after a successful connect, so nothing at that point makes it fall back to v4.

    Delaying the SYN-ACK until the outbound is up wouldn't work with sniffing on either (unless metadataOnly): DispatchLink reads the first bytes from the client before routing (default.go:350), and the client only sends them after the handshake. Same chicken-and-egg as the ALPN thing you mentioned.

    Only read the code, I haven't run this on a real TUN with a v4-only physical interface.

  21. RPRX commented on Nov 21, 2026

    @RPRX
    Member

    不过如果 TUN 开了 sniffing,没读到 TCP payload(比如 TLS SNI)的话路由也不确定它是该走 direct 还是 proxy,后者基本都是双栈倒没问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions