Skip to content

WireGuard kernelTun: Fix IPv6 table index allocation when the first table is occupied - #6881

Merged
RPRX merged 4 commits into
XTLS:mainfrom
Hacinsl:fix-wg-ip6table
Oct 5, 2026
Merged

RPRX merged 4 commits into
XTLS:mainfrom
Hacinsl:fix-wg-ip6table

Conversation

@Hacinsl

@Hacinsl Hacinsl commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

问题

Linux 下使用 kernel TUN(默认模式)且配置包含 IPv6 地址时,启动可能失败:

Failed to initialize: main: failed to create server > failed to find available ipv6 table index

原因

在proxy/wireguard/tun_linux.go 挑选空闲 IPv6 路由表的循环里,过滤器 r 只在循环外构造一次:

    ipv6TableIndex := allocateIPv6TableIndex()
    if v6 != nil {
        r := &netlink.Route{Table: ipv6TableIndex}
        for {
            routeList, fErr := netlink.RouteListFiltered(netlink.FAMILY_V6, r, netlink.RT_FILTER_TABLE)
            if len(routeList) == 0 || fErr != nil {
                break
            }
            ipv6TableIndex--          // 只修改了局部变量
            if ipv6TableIndex < 0 {
                return nil, nil, fmt.Errorf("failed to find available ipv6 table index")
            }
        }
    }

netlink.RouteListFiltered 是按 r.Table 做过滤的,而循环里只递减了 ipv6TableIndex,从未把新值写回 r.Table,因此每次迭代查询的始终是同一张表。

由于 tableIndex 是包级变量、每个进程都从10230开始,10230被占用(最常见的可能性是已有Xray实例运行,同时启动新实例检查配置)会导致启动/配置检查失败。

修复

  1. 循环内每次重新分配表号,并把新表号同步写回过滤器后再查询,使重试真正探测到不同的表;
  2. 将allocateIPv6TableIndex改为返回(int, error),为表号空间加入上界(65535);
  3. 把初始的表号分配移入ipv6分支内部,纯ipv4配置不再消耗表号。

行为变化

若预设的10230被占用,会向上搜索可用表号直到65535,原设计意图可能是向下搜寻,但区别不大。

Hacinsl and others added 2 commits October 1, 2026 15:12
Refactor IPv6 table index allocation to use atomic operations and remove mutex locking.
@Fangliding

Copy link
Copy Markdown
Member

我知道了 这个向下search是最早加的 用以控制多个出站的场景 因为你找的这个bug导致search实际是失效的 多余一个wireguard出站就会坏掉 早年的时候接到这个issue我没看到下面代码以为是硬编码才写了个递增alloc然后放弃多instance场景了 如果这里有搜索的话就拿起来用上吧

Refactor loop to find available IPv6 table index, preventing infinite loop by checking table index limit.
patterniha pushed a commit to patterniha/Xray-core that referenced this pull request Oct 3, 2026
…able is occupied

Early merge of XTLS#6881 (PR head ea728a4), squashed, ahead of
it landing upstream: XTLS#6881

Co-authored-by: 风扇滑翔翼 <[email protected]>
@RPRX

RPRX commented Oct 5, 2026

Copy link
Copy Markdown
Member

@Hacinsl 修一下 fmt

@RPRX
RPRX merged commit 92f7e72 into XTLS:main Oct 5, 2026
olivezane added a commit to olivezane/Xray-core-ai that referenced this pull request Oct 6, 2026
7 upstream commits: WireGuard kernelTun IPv6 table index fix (XTLS#6881),
WireGuard idle-flow memory and ICMP pong (XTLS#6856, XTLS#6855), QUIC sniffer
zero-filled tails (XTLS#6882), XDNS resolver deadlock (XTLS#7089), gRPC redial
context (XTLS#6877), scenario test process leaks (XTLS#6863).

Conflict in proxy/wireguard/tun_linux.go: took upstream's loop guard
while keeping the local errors.New lint fix.
fanymagnet pushed a commit to fanymagnet/Xray-core that referenced this pull request Oct 8, 2026
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.

3 participants