Fix neighbor table going stale after netlink receive overrun

This commit is contained in:
世界
2026-11-14 05:22:33 +00:00
parent 6420f53fc6
commit 6db3f11263
3 changed files with 58 additions and 57 deletions
+15 -14
View File
@@ -3,34 +3,24 @@
package libbox
import (
"errors"
"net"
"net/netip"
"slices"
"time"
"github.com/sagernet/sing-box/route"
E "github.com/sagernet/sing/common/exceptions"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
)
func SubscribeNeighborTable(listener NeighborUpdateListener) (*NeighborSubscription, error) {
entries, err := route.ReadNeighborEntries()
connection, table, err := route.SubscribeNeighborTable()
if err != nil {
return nil, E.Cause(err, "initial neighbor dump")
}
table := make(map[netip.Addr]net.HardwareAddr)
for _, entry := range entries {
table[entry.Address] = entry.MACAddress
return nil, err
}
listener.UpdateNeighborTable(tableToIterator(table))
connection, err := netlink.Dial(unix.NETLINK_ROUTE, &netlink.Config{
Groups: 1 << (unix.RTNLGRP_NEIGH - 1),
})
if err != nil {
return nil, E.Cause(err, "subscribe neighbor updates")
}
subscription := &NeighborSubscription{
done: make(chan struct{}),
}
@@ -39,7 +29,9 @@ func SubscribeNeighborTable(listener NeighborUpdateListener) (*NeighborSubscript
}
func (s *NeighborSubscription) loop(listener NeighborUpdateListener, connection *netlink.Conn, table map[netip.Addr]net.HardwareAddr) {
defer connection.Close()
defer func() {
connection.Close()
}()
for {
select {
case <-s.done:
@@ -60,6 +52,15 @@ func (s *NeighborSubscription) loop(listener NeighborUpdateListener, connection
return
default:
}
if errors.Is(err, unix.ENOBUFS) {
newConnection, newTable, err := route.SubscribeNeighborTable()
if err != nil {
continue
}
connection.Close()
connection, table = newConnection, newTable
listener.UpdateNeighborTable(tableToIterator(table))
}
continue
}
changed := false
+24 -43
View File
@@ -3,10 +3,10 @@
package route
import (
"errors"
"net"
"net/netip"
"os"
"slices"
"sync"
"time"
@@ -15,7 +15,6 @@ import (
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
"github.com/jsimonetti/rtnetlink"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
)
@@ -61,12 +60,14 @@ func newNeighborResolver(resolverLogger logger.ContextLogger, leaseFiles []strin
}
func (r *neighborResolver) Start() error {
err := r.loadNeighborTable()
connection, neighborIPToMAC, err := SubscribeNeighborTable()
if err != nil {
r.logger.Warn(E.Cause(err, "load neighbor table"))
r.logger.Warn(err)
} else {
r.neighborIPToMAC = neighborIPToMAC
go r.subscribeNeighborUpdates(connection)
}
r.doReloadLeaseFiles()
go r.subscribeNeighborUpdates()
if len(r.leaseFiles) > 0 {
watcher, err := fswatch.NewWatcher(fswatch.Options{
Path: r.leaseFiles,
@@ -143,50 +144,17 @@ func (r *neighborResolver) LookupHostname(address netip.Addr) (string, bool) {
return "", false
}
func (r *neighborResolver) loadNeighborTable() error {
connection, err := rtnetlink.Dial(nil)
if err != nil {
return E.Cause(err, "dial rtnetlink")
}
defer connection.Close()
neighbors, err := connection.Neigh.List()
if err != nil {
return E.Cause(err, "list neighbors")
}
r.access.Lock()
defer r.access.Unlock()
for _, neigh := range neighbors {
if neigh.Attributes == nil {
continue
}
if neigh.Attributes.LLAddress == nil || len(neigh.Attributes.Address) == 0 {
continue
}
address, ok := netip.AddrFromSlice(neigh.Attributes.Address)
if !ok {
continue
}
r.neighborIPToMAC[address] = slices.Clone(neigh.Attributes.LLAddress)
}
return nil
}
func (r *neighborResolver) subscribeNeighborUpdates() {
connection, err := netlink.Dial(unix.NETLINK_ROUTE, &netlink.Config{
Groups: 1 << (unix.RTNLGRP_NEIGH - 1),
})
if err != nil {
r.logger.Warn(E.Cause(err, "subscribe neighbor updates"))
return
}
defer connection.Close()
func (r *neighborResolver) subscribeNeighborUpdates(connection *netlink.Conn) {
defer func() {
connection.Close()
}()
for {
select {
case <-r.done:
return
default:
}
err = connection.SetReadDeadline(time.Now().Add(3 * time.Second))
err := connection.SetReadDeadline(time.Now().Add(3 * time.Second))
if err != nil {
r.logger.Warn(E.Cause(err, "set netlink read deadline"))
return
@@ -201,6 +169,19 @@ func (r *neighborResolver) subscribeNeighborUpdates() {
return
default:
}
if errors.Is(err, unix.ENOBUFS) {
newConnection, neighborIPToMAC, err := SubscribeNeighborTable()
if err != nil {
r.logger.Warn(err)
continue
}
connection.Close()
connection = newConnection
r.access.Lock()
r.neighborIPToMAC = neighborIPToMAC
r.access.Unlock()
continue
}
r.logger.Warn(E.Cause(err, "receive neighbor update"))
continue
}
+19
View File
@@ -45,6 +45,25 @@ func ReadNeighborEntries() ([]adapter.NeighborEntry, error) {
return entries, nil
}
func SubscribeNeighborTable() (*netlink.Conn, map[netip.Addr]net.HardwareAddr, error) {
connection, err := netlink.Dial(unix.NETLINK_ROUTE, &netlink.Config{
Groups: 1 << (unix.RTNLGRP_NEIGH - 1),
})
if err != nil {
return nil, nil, E.Cause(err, "subscribe neighbor updates")
}
entries, err := ReadNeighborEntries()
if err != nil {
connection.Close()
return nil, nil, E.Cause(err, "load neighbor table")
}
table := make(map[netip.Addr]net.HardwareAddr, len(entries))
for _, entry := range entries {
table[entry.Address] = entry.MACAddress
}
return connection, table, nil
}
func ParseNeighborMessage(message netlink.Message) (address netip.Addr, macAddress net.HardwareAddr, isDelete bool, ok bool) {
var neighMessage rtnetlink.NeighMessage
err := neighMessage.UnmarshalBinary(message.Data)