diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 73df39c8c..aa5acabc2 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -5725,12 +5725,6 @@ func (b *LocalBackend) onEditPrefsLocked(_ ipnauth.Actor, mp *ipn.MaskedPrefs, o } } - if mp.AdvertiseServicesSet { - if ns, ok := b.sys.Netstack.GetOK(); ok { - ns.UpdateActiveVIPServices(newPrefs.AdvertiseServices()) - } - } - // This is recorded here in the EditPrefs path, not the setPrefs path on purpose. // recordForEdit records metrics related to edits and changes, not the final state. // If, in the future, we want to record gauge-metrics related to the state of prefs, @@ -5946,6 +5940,15 @@ func (b *LocalBackend) setPrefsLocked(newp *ipn.Prefs) ipn.PrefsView { b.updateWarnSync(prefs) b.updateNoSNATExitNodeWarning(prefs) + // Netstack handles traffic to a Tailscale Service IP locally only when + // this node advertises that Service. Update its set here so that every + // path that changes prefs keeps it current, including config file reloads. + if buildfeatures.HasServe && (!oldp.Valid() || !views.SliceEqual(oldp.AdvertiseServices(), prefs.AdvertiseServices())) { + if ns, ok := b.sys.Netstack.GetOK(); ok { + ns.UpdateActiveVIPServices(prefs.AdvertiseServices()) + } + } + if oldp.ShieldsUp() != newp.ShieldsUp || hostInfoChanged { b.doSetHostinfoFilterServicesLocked() } diff --git a/wgengine/netstack/netstack_test.go b/wgengine/netstack/netstack_test.go index 1a873025f..fb371eae3 100644 --- a/wgengine/netstack/netstack_test.go +++ b/wgengine/netstack/netstack_test.go @@ -5,10 +5,13 @@ package netstack import ( "context" + "encoding/json" "fmt" "maps" "net" "net/netip" + "os" + "path/filepath" "runtime" "testing" "time" @@ -24,7 +27,9 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/transport/udp" "gvisor.dev/gvisor/pkg/waiter" "tailscale.com/envknob" + "tailscale.com/feature/buildfeatures" "tailscale.com/ipn" + "tailscale.com/ipn/conffile" "tailscale.com/ipn/ipnlocal" "tailscale.com/ipn/store/mem" "tailscale.com/metrics" @@ -33,6 +38,7 @@ import ( "tailscale.com/net/tsaddr" "tailscale.com/net/tsdial" "tailscale.com/net/tstun" + "tailscale.com/tailcfg" "tailscale.com/tsd" "tailscale.com/tstest" "tailscale.com/types/ipproto" @@ -109,9 +115,19 @@ func getMemStats() (ms runtime.MemStats) { } func makeNetstack(tb testing.TB, config func(*Impl)) *Impl { + return makeNetstackWithSys(tb, nil, config) +} + +// makeNetstackWithSys is like makeNetstack, but calls configSys on the +// tsd.System before the LocalBackend is created, so tests can set fields +// like InitialConfig that NewLocalBackend reads. +func makeNetstackWithSys(tb testing.TB, configSys func(*tsd.System), config func(*Impl)) *Impl { tunDev := tstun.NewFake() sys := tsd.NewSystem() sys.Set(new(mem.Store)) + if configSys != nil { + configSys(sys) + } dialer := new(tsdial.Dialer) logf := tstest.WhileTestRunningLogger(tb) eng, err := wgengine.NewUserspaceEngine(logf, wgengine.Config{ @@ -2234,3 +2250,80 @@ func TestInjectLoopback(t *testing.T) { t.Errorf("got %q, want %q", got, "loopback test") } } + +// TestReloadConfigUpdatesActiveVIPServices verifies that a change to +// AdvertiseServices applied through a config file reload reaches netstack's +// set of active Tailscale Services, not only the prefs. +// +// Regression test for tailscale/tailscale#22498. +func TestReloadConfigUpdatesActiveVIPServices(t *testing.T) { + if !buildfeatures.HasServe { + t.Skip("netstack only tracks active Services when serve is built in") + } + const svcA, svcB = "svc:a", "svc:b" + + dir := t.TempDir() + path := filepath.Join(dir, "tailscale.conf") + writeConf := func(services ...string) { + t.Helper() + raw, err := json.Marshal(ipn.ConfigVAlpha{ + Version: "alpha0", + AdvertiseServices: services, + }) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, raw, 0644); err != nil { + t.Fatal(err) + } + } + // Start with no Services advertised. The initial config is applied at + // construction without going through setPrefsLocked, so netstack's set is + // empty at boot regardless; every Service below is added by a reload. + writeConf() + initial, err := conffile.Load(path) + if err != nil { + t.Fatal(err) + } + + ns := makeNetstackWithSys(t, func(sys *tsd.System) { + sys.InitialConfig = initial + }, nil) + + active := func() []string { + var got []string + for s := range ns.atomicActiveVIPServices.Load() { + got = append(got, s.String()) + } + return got + } + isActive := func(name string) bool { + return ns.atomicActiveVIPServices.Load().Contains(tailcfg.ServiceName(name)) + } + reload := func(services ...string) { + t.Helper() + writeConf(services...) + if ok, err := ns.lb.ReloadConfig(); !ok || err != nil { + t.Fatalf("ReloadConfig() = %v, %v; want true, nil", ok, err) + } + } + + // Add a Service. + reload(svcA) + if !isActive(svcA) { + t.Errorf("after adding %q, active services = %v; want it active", svcA, active()) + } + + // Swap the advertised Service from A to B. This checks that a reload + // removes a Service that an earlier reload added. + reload(svcB) + if isActive(svcA) || !isActive(svcB) { + t.Errorf("after swapping to %q, active services = %v; want only %q", svcB, active(), svcB) + } + + // Withdraw every Service. + reload() + if got := active(); len(got) != 0 { + t.Errorf("after withdrawing all services, active services = %v; want none", got) + } +}