net/netmon: move TailscaleInterfaceIndex out of netmon.State (#18428)

fixes tailscale/tailscale#18418

Both Serve and PeerAPI broke when we moved the TailscaleInterfaceName
into State, which is updated asynchronously and may not be
available when we configure the listeners.

This extracts the explicit interface name property from netmon.State
and adds as a static struct with getters that have proper error
handling.

The bug is only found in sandboxed Darwin clients, where we
need to know the Tailscale interface details in order to set up the
listeners correctly (they must bind to our interface explicitly to escape
the network sandboxing that is applied by NECP).

Currently set only sandboxed macOS and Plan9 set this but it will
also be useful on Windows to simplify interface filtering in netns.

Signed-off-by: Jonathan Nobels <jonathan@tailscale.com>
This commit is contained in:
Jonathan Nobels
2026-01-16 14:53:23 -05:00
committed by GitHub
parent 1478028591
commit 643e91f2eb
9 changed files with 184 additions and 60 deletions
+14 -2
View File
@@ -159,7 +159,7 @@ func TestMonitorMode(t *testing.T) {
// tests (*ChangeDelta).RebindRequired
func TestRebindRequired(t *testing.T) {
// s1 cannot be nil by definition
// s1 must not be nil by definition
tests := []struct {
name string
s1, s2 *State
@@ -478,9 +478,11 @@ func TestRebindRequired(t *testing.T) {
withIsInterestingInterface(t, func(ni Interface, pfxs []netip.Prefix) bool {
return !strings.HasPrefix(ni.Name, "boring")
})
saveAndRestoreTailscaleIfaceProps(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Populate dummy interfaces where missing.
for _, s := range []*State{tt.s1, tt.s2} {
if s == nil {
@@ -495,7 +497,8 @@ func TestRebindRequired(t *testing.T) {
}
}
cd, err := NewChangeDelta(tt.s1, tt.s2, false, tt.tsIfName, true)
SetTailscaleInterfaceProps(tt.tsIfName, 1)
cd, err := NewChangeDelta(tt.s1, tt.s2, false, true)
if err != nil {
t.Fatalf("NewChangeDelta error: %v", err)
}
@@ -507,6 +510,15 @@ func TestRebindRequired(t *testing.T) {
}
}
func saveAndRestoreTailscaleIfaceProps(t *testing.T) {
t.Helper()
index, _ := TailscaleInterfaceIndex()
name, _ := TailscaleInterfaceName()
t.Cleanup(func() {
SetTailscaleInterfaceProps(name, index)
})
}
func withIsInterestingInterface(t *testing.T, fn func(Interface, []netip.Prefix) bool) {
t.Helper()
old := IsInterestingInterface