Fix "Fix auto_redirect dropping SO_BINDTODEVICE traffic"

This commit is contained in:
世界 2026-03-14 20:43:28 +08:00
parent caaf8469e0
commit 0329538ecd
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -5,6 +5,7 @@ package tun
import ( import (
"math/rand" "math/rand"
"net" "net"
"net/netip"
"github.com/sagernet/netlink" "github.com/sagernet/netlink"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
@ -38,7 +39,7 @@ func (r *autoRedirect) setupRedirectRoutes() error {
}) })
r.cleanupRedirectRoutes() r.cleanupRedirectRoutes()
for _, iface := range r.redirectInterfaces { for _, iface := range r.redirectInterfaces {
err = r.addRedirectRoutes(iface.Index) err = r.addRedirectRoutes(iface)
if err != nil { if err != nil {
return err return err
} }
@ -66,10 +67,12 @@ func (r *autoRedirect) setupRedirectRoutes() error {
return nil return nil
} }
func (r *autoRedirect) addRedirectRoutes(linkIndex int) error { func (r *autoRedirect) addRedirectRoutes(iface control.Interface) error {
if r.enableIPv4 { if r.enableIPv4 && common.Any(iface.Addresses, func(it netip.Prefix) bool {
return it.Addr().Is4()
}) {
err := netlink.RouteAppend(&netlink.Route{ 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)}, Dst: &net.IPNet{IP: net.IPv4(127, 0, 0, 1), Mask: net.CIDRMask(32, 32)},
Table: r.redirectRouteTableIndex, Table: r.redirectRouteTableIndex,
Type: unix.RTN_LOCAL, Type: unix.RTN_LOCAL,
@ -79,9 +82,11 @@ func (r *autoRedirect) addRedirectRoutes(linkIndex int) error {
return err 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{ err := netlink.RouteAppend(&netlink.Route{
LinkIndex: linkIndex, LinkIndex: iface.Index,
Dst: &net.IPNet{IP: net.IPv6loopback, Mask: net.CIDRMask(128, 128)}, Dst: &net.IPNet{IP: net.IPv6loopback, Mask: net.CIDRMask(128, 128)},
Table: r.redirectRouteTableIndex, Table: r.redirectRouteTableIndex,
Type: unix.RTN_LOCAL, Type: unix.RTN_LOCAL,
@ -132,7 +137,7 @@ func (r *autoRedirect) updateRedirectRoutes() error {
} }
for _, iface := range newInterfaces { for _, iface := range newInterfaces {
if !oldMap[iface.Index] { if !oldMap[iface.Index] {
err = r.addRedirectRoutes(iface.Index) err = r.addRedirectRoutes(iface)
if err != nil { if err != nil {
return err return err
} }