TUN inbound: Ignore IPv6 entries on Windows when DisabledComponents turns IPv6 off on all interfaces

https://github.com/XTLS/Xray-core/pull/7332#issuecomment-6676374920

With IPv6 off on all interfaces by DisabledComponents (0x01 and 0x10 both
set, as in 0xFF), the IPv6 entries of gateway, dns and
autoSystemRoutingTable are ignored and the TUN starts with IPv4 only, so
one config works either way. The value takes effect at the next boot only,
so ignored IPv6 routes add "[::/0]:misconfigtun" to autoSystemWfpBlockLeak:
until the reboot, IPv6 the TUN would have taken cannot leave outside it,
also through interfaces connected later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patterniha
2026-11-06 23:53:26 +03:30
co-authored by Claude Opus 5.5
parent 1c8bf4067b
commit 8fe26a91d5
2 changed files with 55 additions and 5 deletions
+1 -1
View File
@@ -223,7 +223,7 @@ To make it start, wintun.dll specific for your Windows/arch must be present next
After the start network adapter with the name you chose in the config will be created in the system, and exist while Xray is running. A Wintun adapter that already has that name is reused.
With IPv6 disabled system-wide (the `DisabledComponents` registry value), the adapter has no IPv6, and the TUN starts with IPv4 only, unless `gateway`, `dns` or `autoSystemRoutingTable` has IPv6 entries, which then keep it from starting.
With IPv6 off on all interfaces by the `DisabledComponents` registry value (0x01 and 0x10 both set, as in 0xFF), the TUN starts with IPv4 only, ignoring the IPv6 entries of `gateway`, `dns` and `autoSystemRoutingTable`. As Windows applies the value at boot only, ignored IPv6 routes add `"[::/0]:misconfigtun"` to `autoSystemWfpBlockLeak`, so IPv6 cannot bypass the TUN before the reboot either. Should the adapter lack IPv6 otherwise, or refuse its IPv6 DNS servers, a config without IPv6 entries starts with IPv4 only, and IPv6 entries keep the TUN from starting.
When `dns` is set, those servers are applied to the adapter. Windows is kept from registering the TUN's addresses in DNS, and its DNS cache is flushed when the TUN starts and stops. Xray's own lookups through the system's DNS servers, like those of the `localhost` DNS server with `autoOutboundsInterface`, skip them, unless another interface uses them as well, as they would lead back into the TUN.
+54 -4
View File
@@ -23,6 +23,7 @@ import (
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/transport/internet"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"golang.zx2c4.com/wintun"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
)
@@ -98,13 +99,34 @@ func open(name, desc string) (*wintun.Adapter, error) {
return nil, err
}
// ipv6Off tells whether DisabledComponents turns IPv6 off on all interfaces
// but loopback, the tunnel (0x01) and the native (0x10) ones, as 0xFF does.
// Windows applies it at boot only.
func ipv6Off() bool {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters`, registry.QUERY_VALUE)
if err != nil {
return false
}
defer key.Close()
value, _, err := key.GetIntegerValue("DisabledComponents")
return err == nil && value&0x11 == 0x11
}
func (t *WindowsTun) Start() (err error) {
// With IPv6 off on all interfaces, the TUN can have IPv4 only, so the
// IPv6 entries are left out, for one config to work either way.
off6 := ipv6Off()
var ignored6, ignoredRoute6 bool
var address4, address6 bool
addresses := make([]netip.Prefix, 0, len(t.options.Gateway))
for _, cidr := range t.options.Gateway {
prefix := netip.MustParsePrefix(cidr)
if prefix.Addr().Is4() {
address4 = true
} else if off6 {
ignored6 = true
continue
} else {
address6 = true
}
@@ -113,7 +135,12 @@ func (t *WindowsTun) Start() (err error) {
dns := make([]netip.Addr, 0, len(t.options.DNS))
for _, ip := range t.options.DNS {
dns = append(dns, netip.MustParseAddr(ip))
addr := netip.MustParseAddr(ip)
if addr.Is6() && off6 {
ignored6 = true
continue
}
dns = append(dns, addr)
}
dns6 := slices.ContainsFunc(dns, netip.Addr.Is6)
@@ -128,12 +155,18 @@ func (t *WindowsTun) Start() (err error) {
if prefix.Addr().Is4() {
route4 = true
route.NextHop = netip.IPv4Unspecified()
} else if off6 {
ignored6, ignoredRoute6 = true, true
continue
} else {
route6 = true
route.NextHop = netip.IPv6Unspecified()
}
routesMap[route] = struct{}{}
}
if ignored6 {
errors.LogInfo(context.Background(), "[tun] IPv6 is off on all interfaces (DisabledComponents), so the IPv6 entries of gateway, dns and autoSystemRoutingTable are ignored")
}
routesData := make([]*winipcfg.RouteData, 0, len(routesMap))
for route := range routesMap {
r := route
@@ -265,21 +298,38 @@ startOver:
if err != nil {
return err
}
// DisabledComponents takes effect at the next boot only, and until then,
// IPv6 of the ignored routes would go around the TUN. So IPv6 is kept from
// leaving outside it, as by "misconfigtun".
if ignoredRoute6 {
leaks = append(leaks, "[::/0]:misconfigtun")
}
routed := route4 || route6 || ignoredRoute6
block := newLeakFilters(leaks, route4, route6)
if route4 || route6 {
if routed {
dnsClient := func(l leak) bool { return l.name == "windnsclient" }
if slices.ContainsFunc(block, dnsClient) && !dnsClientUsesAnyPort() {
block = slices.DeleteFunc(block, dnsClient)
errors.LogInfo(context.Background(), "[tun] windnsclient skipped: before Windows 11 and Server 2022, the DNS Client service only queries port 53")
}
if block.systemDNS() && len(dns) == 0 {
if len(t.options.DNS) > 0 {
return errors.New("keeping the system's DNS inside the TUN needs IPv4 servers in dns, as IPv6 is off on all interfaces")
}
return errors.New("keeping the system's DNS inside the TUN needs dns to be set")
}
}
if blocked := block.String(); (route4 || route6) && blocked != "" {
if blocked := block.String(); routed && blocked != "" {
if t.wfp, err = blockLeaks(t.luid, block); err != nil {
// Rather no TUN than a leaking one.
return errors.New("unable to block ", blocked, " outside the TUN (remove autoSystemWfpBlockLeak to run without)").Base(err)
without := "autoSystemWfpBlockLeak"
switch {
case len(t.options.AutoSystemWfpBlockLeak) == 0:
without = "the IPv6 entries of autoSystemRoutingTable"
case ignoredRoute6:
without += " and the IPv6 entries of autoSystemRoutingTable"
}
return errors.New("unable to block ", blocked, " outside the TUN (remove ", without, " to run without)").Base(err)
}
errors.LogInfo(context.Background(), "[tun] blocked outside the TUN: ", blocked)
covered := slices.Clone(addresses)