fix(dialer): skip SO_BINDTODEVICE for loopback interface (issue #785)

When using IP aliases on loopback (e.g. ip addr add 10.1.1.1/32 dev lo)
with policy routing, findInterfaceByIP() resolves the alias IP to the
loopback interface. Binding the socket to lo via SO_BINDTODEVICE prevents
outbound traffic from reaching non-local destinations.

Skip SO_BINDTODEVICE when the resolved interface is loopback — source IP
binding via LocalAddr is sufficient for policy routing to steer traffic
through the correct physical interface.
This commit is contained in:
ginuerzh
2026-06-08 21:36:33 +08:00
parent 078cdbcb81
commit caa82f2861
+8
View File
@@ -11,6 +11,14 @@ func bindDevice(network, address string, fd uintptr, ifceName string) error {
return nil
}
// Skip SO_BINDTODEVICE for loopback interface — it prevents
// outbound traffic from reaching non-local destinations.
// Source IP binding via LocalAddr is sufficient for policy routing
// when using IP aliases on loopback (e.g. ip addr add 10.1.1.1/32 dev lo).
if ifce, err := net.InterfaceByName(ifceName); err == nil && ifce.Flags&net.FlagLoopback != 0 {
return nil
}
host, _, _ := net.SplitHostPort(address)
if ip := net.ParseIP(host); ip != nil && !ip.IsGlobalUnicast() {
return nil