mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-19 11:59:09 +02:00
[client] Keep interface discovery out of the bind open path (#8226)
* [client] Keep interface discovery out of the bind open path Since 0.81.0 a userspace-mode agent whose interface name is not covered by the interface blacklist deadlocks while bringing the device up, and the daemon, the GUI and the service stop all hang. wireguard-go holds the device net lock across bind.Open, our bind builds the UDP mux there, the mux enumerated interfaces in its constructor, and the interface filter probed every unlisted name through wgctrl. For the agent's own interface that probe connected to the daemon's own UAPI endpoint and waited, with no deadline, for a reply the handler could only give after taking the same lock. #7346 moved the first enumeration from the stdnet constructor, which ran before the device existed, to this call site. The mux no longer discovers interfaces when it is constructed. It records whether its socket is bound to an unspecified address and GetConn uses that instead of the length of the discovered list, while GetListenAddresses keeps refreshing on demand, which pion calls during candidate gathering outside the device locks. The WireGuard detector also answers for the agent's own interface without probing, so no filter asks the daemon about itself. * [client] Take the agent's own interface name in the detector constructor Every device implementation reports the configured name, so there is no later name to follow and the setter was state without a reader that needed it. The detector takes the name once and compares without a lock. * [client] Create the default mux Net in the constructor With discovery moved to GetListenAddresses, the nil Net fallback ran there as well, and the first concurrent callers raced on writing it. stdnet.NewNet does no discovery, so the constructor can create it before the mux is shared and the address refresh only reads it. * [client] Follow the created interface name in the detector Android ignores the configured interface name and keeps the TUN name returned by CreateUnmonitoredTUNFromFD, so the detector built from the configuration would still probe the agent's real interface. The engine updates the detector with wgInterface.Name() once the interface exists. * [client] Keep the detector's own interface name immutable On Android the kernel names the TUN and the name can change again on renew, while the UAPI socket cannot be opened there at all, so neither the probe nor a name set after creation can identify the agent's own interface reliably. Excluding that interface on Android belongs to the interface discovery the app provides. Everywhere else the configured name is the created name, so the constructor argument is enough and the detector needs no setter and no lock on the hot path.
This commit is contained in:
@@ -387,7 +387,7 @@ func NewEngine(
|
||||
mgmClient: services.MgmClient,
|
||||
relayManager: services.RelayManager,
|
||||
peerStore: peerstore.NewConnStore(),
|
||||
wgDetector: stdnet.NewWGDetector(),
|
||||
wgDetector: stdnet.NewWGDetector(config.WgIfaceName),
|
||||
syncMsgMux: &sync.Mutex{},
|
||||
config: config,
|
||||
mobileDep: mobileDep,
|
||||
|
||||
@@ -34,6 +34,8 @@ type WGDetector struct {
|
||||
// probe is replaced in tests; it is the call this type exists to avoid repeating.
|
||||
probe func(string) bool
|
||||
|
||||
ownIface string
|
||||
|
||||
mu sync.RWMutex
|
||||
cache map[string]wgDetectorEntry
|
||||
|
||||
@@ -41,11 +43,12 @@ type WGDetector struct {
|
||||
}
|
||||
|
||||
// NewWGDetector returns a detector with the default time to live.
|
||||
func NewWGDetector() *WGDetector {
|
||||
func NewWGDetector(ownIface string) *WGDetector {
|
||||
return &WGDetector{
|
||||
ttl: wgDetectorTTL,
|
||||
probe: probeWireGuard,
|
||||
cache: make(map[string]wgDetectorEntry),
|
||||
ttl: wgDetectorTTL,
|
||||
probe: probeWireGuard,
|
||||
ownIface: ownIface,
|
||||
cache: make(map[string]wgDetectorEntry),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +60,10 @@ func (d *WGDetector) IsWireGuard(iFace string) bool {
|
||||
return probeWireGuard(iFace)
|
||||
}
|
||||
|
||||
if d.isOwnInterface(iFace) {
|
||||
return true
|
||||
}
|
||||
|
||||
if isWireGuard, ok := d.cached(iFace); ok {
|
||||
return isWireGuard
|
||||
}
|
||||
@@ -104,6 +111,10 @@ func (d *WGDetector) store(iFace string, isWireGuard bool) {
|
||||
d.cache[iFace] = wgDetectorEntry{isWireGuard: isWireGuard, expireAt: now.Add(d.ttl)}
|
||||
}
|
||||
|
||||
func (d *WGDetector) isOwnInterface(iFace string) bool {
|
||||
return d.ownIface != "" && iFace == d.ownIface
|
||||
}
|
||||
|
||||
func probeWireGuard(iFace string) bool {
|
||||
wg, err := wgctrl.New()
|
||||
if err != nil {
|
||||
|
||||
@@ -150,3 +150,39 @@ func TestWGDetectorDropsExpiredEntries(t *testing.T) {
|
||||
assert.Len(t, d.cache, 1, "expired entries of vanished interfaces must be dropped")
|
||||
assert.Contains(t, d.cache, "eth0", "the fresh answer must be kept")
|
||||
}
|
||||
|
||||
func TestWGDetectorOwnInterfaceIsWireGuardWithoutProbing(t *testing.T) {
|
||||
d, calls := newCountingDetector(t, time.Minute, false)
|
||||
d.ownIface = "NetBird"
|
||||
|
||||
assert.True(t, d.IsWireGuard("NetBird"), "the agent's own interface is a WireGuard device")
|
||||
assert.Equal(t, int64(0), calls.Load(), "the own interface must never reach the probe")
|
||||
|
||||
assert.False(t, d.IsWireGuard("NetBird2"), "only the exact own name is answered without probing")
|
||||
assert.Equal(t, int64(1), calls.Load(), "a different interface is probed")
|
||||
}
|
||||
|
||||
func TestWGDetectorWithoutOwnInterfaceProbesEmptyName(t *testing.T) {
|
||||
d, calls := newCountingDetector(t, time.Minute, false)
|
||||
|
||||
assert.False(t, d.IsWireGuard(""), "an unnamed interface is not mistaken for the own one")
|
||||
assert.Equal(t, int64(1), calls.Load(), "with no own interface set every name is probed")
|
||||
}
|
||||
|
||||
func TestInterfaceFilterRejectsOwnInterfaceRegardlessOfDisallowList(t *testing.T) {
|
||||
d, calls := newCountingDetector(t, time.Minute, false)
|
||||
d.ownIface = "NetBird"
|
||||
filter := InterfaceFilter([]string{"wt"}, d)
|
||||
|
||||
assert.False(t, filter("NetBird"), "the own interface is rejected even when the disallow list does not cover it")
|
||||
assert.Equal(t, int64(0), calls.Load(), "the own interface is not probed")
|
||||
|
||||
assert.True(t, filter("eth0"), "an ordinary interface stays available")
|
||||
assert.Equal(t, int64(1), calls.Load(), "an ordinary interface is still probed")
|
||||
}
|
||||
|
||||
func TestNewWGDetectorKeepsTheOwnInterface(t *testing.T) {
|
||||
d := NewWGDetector("NetbirdWT")
|
||||
|
||||
assert.True(t, d.IsWireGuard("NetbirdWT"), "the constructor records the own interface")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user