Compare commits

...
Author SHA1 Message Date
patternihaandClaude Opus 5.5 3d830eea02 TUN inbound: Don't set IP_BOUND_IF on IPv6 sockets on Darwin
Since #7173, a failed interface binding fails the dial. On Darwin (macOS
and iOS), setinterface fell through from IPV6_BOUND_IF to IP_BOUND_IF on
IPv6 sockets, which Darwin rejects with EINVAL. So every UDP dial failed,
as Go opens a dual-stack IPv6 socket for it, and so did TCP to IPv6. TCP to
IPv4 uses an IPv4 socket and kept working. Before #7173 the error was only
logged.

IPV6_BOUND_IF binds the whole socket, so on a dual-stack socket it covers
its IPv4 traffic as well. Set only the option of the socket's family, as
sockopt_darwin.go already does for sockopt.interface. The new test binds
each kind of socket to lo0.

Linux, Android and FreeBSD bind with socket-level options (SO_BINDTODEVICE,
SO_SETFIB) that don't depend on the IP version. On Windows, the related case
of an adapter without IPv4 or IPv6 is handled in #7214.

Fixes #7228

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 11:17:33 +03:30
2 changed files with 44 additions and 7 deletions
+4 -7
View File
@@ -558,19 +558,16 @@ func ioctlPtr(fd int, req uint, arg unsafe.Pointer) error {
}
func setinterface(network, address string, fd uintptr, iface *net.Interface) error {
var err1, err2 error
switch network {
case "tcp6", "udp6", "ip6":
err1 = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index)
fallthrough
// IPV6_BOUND_IF binds the IPv4 traffic of a dual-stack socket as
// well, and an IPv6 socket rejects IP_BOUND_IF with EINVAL.
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index)
case "tcp4", "udp4", "ip4":
err2 = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index)
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index)
default:
panic(network + " " + address)
}
return errors.Join(err1, err2)
}
func findOutboundInterface(tunIndex int, fixedName string) (*net.Interface, error) {
+40
View File
@@ -3,7 +3,10 @@
package tun
import (
"context"
"net"
"sync"
"syscall"
"testing"
"time"
@@ -231,3 +234,40 @@ func TestWaitKqueueReportsPersistentFailure(t *testing.T) {
}
}
}
// TestSetinterfaceSocketFamilies binds each kind of socket Xray's dialers
// open to lo0, including the dual-stack one Go opens for UDP.
func TestSetinterfaceSocketFamilies(t *testing.T) {
lo, err := net.InterfaceByName("lo0")
if err != nil {
t.Skip(err)
}
lc := net.ListenConfig{Control: func(network, address string, c syscall.RawConn) error {
var err error
if controlErr := c.Control(func(fd uintptr) {
err = setinterface(network, address, fd, lo)
}); controlErr != nil {
return controlErr
}
return err
}}
for _, s := range []struct{ network, address string }{
{"udp", "0.0.0.0:0"}, // dual-stack
{"udp4", "127.0.0.1:0"},
{"udp6", "[::1]:0"},
{"tcp4", "127.0.0.1:0"},
{"tcp6", "[::1]:0"},
} {
var closer interface{ Close() error }
if s.network[:3] == "udp" {
closer, err = lc.ListenPacket(context.Background(), s.network, s.address)
} else {
closer, err = lc.Listen(context.Background(), s.network, s.address)
}
if err != nil {
t.Errorf("%s %s: %v", s.network, s.address, err)
continue
}
closer.Close()
}
}