From 0329538ecd5e33f3f1365ff2d404069be3378a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 14 Mar 2026 20:43:28 +0800 Subject: [PATCH] Fix "Fix auto_redirect dropping SO_BINDTODEVICE traffic" --- redirect_route_linux.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/redirect_route_linux.go b/redirect_route_linux.go index 726ec1d..6b86da1 100644 --- a/redirect_route_linux.go +++ b/redirect_route_linux.go @@ -5,6 +5,7 @@ package tun import ( "math/rand" "net" + "net/netip" "github.com/sagernet/netlink" "github.com/sagernet/sing/common" @@ -38,7 +39,7 @@ func (r *autoRedirect) setupRedirectRoutes() error { }) r.cleanupRedirectRoutes() for _, iface := range r.redirectInterfaces { - err = r.addRedirectRoutes(iface.Index) + err = r.addRedirectRoutes(iface) if err != nil { return err } @@ -66,10 +67,12 @@ func (r *autoRedirect) setupRedirectRoutes() error { return nil } -func (r *autoRedirect) addRedirectRoutes(linkIndex int) error { - if r.enableIPv4 { +func (r *autoRedirect) addRedirectRoutes(iface control.Interface) error { + if r.enableIPv4 && common.Any(iface.Addresses, func(it netip.Prefix) bool { + return it.Addr().Is4() + }) { err := netlink.RouteAppend(&netlink.Route{ - LinkIndex: linkIndex, + LinkIndex: iface.Index, Dst: &net.IPNet{IP: net.IPv4(127, 0, 0, 1), Mask: net.CIDRMask(32, 32)}, Table: r.redirectRouteTableIndex, Type: unix.RTN_LOCAL, @@ -79,9 +82,11 @@ func (r *autoRedirect) addRedirectRoutes(linkIndex int) error { return err } } - if r.enableIPv6 { + if r.enableIPv6 && common.Any(iface.Addresses, func(it netip.Prefix) bool { + return it.Addr().Is6() && !it.Addr().Is4In6() + }) { err := netlink.RouteAppend(&netlink.Route{ - LinkIndex: linkIndex, + LinkIndex: iface.Index, Dst: &net.IPNet{IP: net.IPv6loopback, Mask: net.CIDRMask(128, 128)}, Table: r.redirectRouteTableIndex, Type: unix.RTN_LOCAL, @@ -132,7 +137,7 @@ func (r *autoRedirect) updateRedirectRoutes() error { } for _, iface := range newInterfaces { if !oldMap[iface.Index] { - err = r.addRedirectRoutes(iface.Index) + err = r.addRedirectRoutes(iface) if err != nil { return err }