[client] Use unique sequence numbers for bsd routes (#4081)

updates the route manager on Unix to use a unique, incrementing sequence number for each route message instead of a fixed value.

Replace the static Seq: 1 with a call to r.getSeq()
Add an atomic seq field and the getSeq method in SysOps
This commit is contained in:
Viktor Liu
2025-07-03 09:02:53 +02:00
committed by GitHub
parent 2c81cf2c1e
commit 9afbecb7ac
2 changed files with 10 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"net" "net"
"net/netip" "net/netip"
"sync" "sync"
"sync/atomic"
"github.com/netbirdio/netbird/client/iface/wgaddr" "github.com/netbirdio/netbird/client/iface/wgaddr"
"github.com/netbirdio/netbird/client/internal/routemanager/notifier" "github.com/netbirdio/netbird/client/internal/routemanager/notifier"
@ -52,6 +53,9 @@ type SysOps struct {
mu sync.Mutex mu sync.Mutex
// notifier is used to notify the system of route changes (also used on mobile) // notifier is used to notify the system of route changes (also used on mobile)
notifier *notifier.Notifier notifier *notifier.Notifier
// seq is an atomic counter for generating unique sequence numbers for route messages
//nolint:unused // only used on BSD systems
seq atomic.Uint32
} }
func NewSysOps(wgInterface wgIface, notifier *notifier.Notifier) *SysOps { func NewSysOps(wgInterface wgIface, notifier *notifier.Notifier) *SysOps {
@ -61,6 +65,11 @@ func NewSysOps(wgInterface wgIface, notifier *notifier.Notifier) *SysOps {
} }
} }
//nolint:unused // only used on BSD systems
func (r *SysOps) getSeq() int {
return int(r.seq.Add(1))
}
func (r *SysOps) validateRoute(prefix netip.Prefix) error { func (r *SysOps) validateRoute(prefix netip.Prefix) error {
addr := prefix.Addr() addr := prefix.Addr()

View File

@ -108,7 +108,7 @@ func (r *SysOps) buildRouteMessage(action int, prefix netip.Prefix, nexthop Next
Type: action, Type: action,
Flags: unix.RTF_UP, Flags: unix.RTF_UP,
Version: unix.RTM_VERSION, Version: unix.RTM_VERSION,
Seq: 1, Seq: r.getSeq(),
} }
const numAddrs = unix.RTAX_NETMASK + 1 const numAddrs = unix.RTAX_NETMASK + 1