mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-01 14:12:32 -08:00
wgengine/router,ipn/ipnlocal: keep Service VIPs in the stale address sweep (#22790)
Since 77d2c87b1, the Linux router removes any Tailscale-range address
on the tunnel interface that is not in LocalAddrs. That deleted the
address of a Tailscale Service served in TUN mode when the operator had
assigned it to the tunnel interface. Pass the Service addresses to the
router as Config.ServiceAddrs so the sweep keeps them, and skip the
sweep while LocalAddrs is empty.
Updates #22701
Signed-off-by: Brendan Creane <[email protected]>
This commit is contained in:
1 parent
4ad3c29228
commit
ccd15f4ff3
6 files changed
+183
-3
No files matched your search
@@ -7029,9 +7029,15 @@ func (b *LocalBackend) routerConfigLocked(cfg *wgcfg.Config, prefs ipn.PrefsView
|
||||
}
|
||||
for vip := range vipServiceIPs {
|
||||
if (vip.Is4() && v4) || (vip.Is6() && v6) {
|
||||
rs.Routes = append(rs.Routes, netip.PrefixFrom(vip, vip.BitLen()))
|
||||
p := netip.PrefixFrom(vip, vip.BitLen())
|
||||
rs.Routes = append(rs.Routes, p)
|
||||
rs.ServiceAddrs = append(rs.ServiceAddrs, p)
|
||||
}
|
||||
}
|
||||
// The loop above fills rs.ServiceAddrs in map iteration order, which
|
||||
// varies between calls. Sort it, because router.Config.Equal compares
|
||||
// slices in order.
|
||||
slices.SortFunc(rs.ServiceAddrs, netip.Prefix.Compare)
|
||||
|
||||
// Get any extra Routes an extension may want installed.
|
||||
if extensionRoutesFx, ok := b.extHost.hooks.ExtraRouterConfigRoutes.GetOk(); ok {
|
||||
|
||||
@@ -10331,6 +10331,63 @@ func TestRouteAllDisabled(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouterConfigServiceAddrs verifies that routerConfigLocked reports the
|
||||
// addresses of the Services this node hosts as ServiceAddrs, sorted, and only
|
||||
// for the IP families the node has an address in.
|
||||
func TestRouterConfigServiceAddrs(t *testing.T) {
|
||||
pp := netip.MustParsePrefix
|
||||
svcIPMapJSON, err := json.Marshal(tailcfg.ServiceIPMappings{
|
||||
"svc:a": []netip.Addr{netip.MustParseAddr("100.99.1.2"), netip.MustParseAddr("fd7a:115c:a1e0::9902")},
|
||||
"svc:b": []netip.Addr{netip.MustParseAddr("100.99.1.1"), netip.MustParseAddr("fd7a:115c:a1e0::9901")},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
addrs []netip.Prefix
|
||||
want []netip.Prefix
|
||||
}{
|
||||
{
|
||||
name: "dual_stack",
|
||||
addrs: []netip.Prefix{pp("100.64.1.1/32"), pp("fd7a:115c:a1e0::1/128")},
|
||||
want: []netip.Prefix{
|
||||
pp("100.99.1.1/32"), pp("100.99.1.2/32"),
|
||||
pp("fd7a:115c:a1e0::9901/128"), pp("fd7a:115c:a1e0::9902/128"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "v4_only",
|
||||
addrs: []netip.Prefix{pp("100.64.1.1/32")},
|
||||
want: []netip.Prefix{pp("100.99.1.1/32"), pp("100.99.1.2/32")},
|
||||
},
|
||||
{
|
||||
name: "no_addrs",
|
||||
addrs: nil,
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
lb := newTestLocalBackend(t)
|
||||
nm := &netmap.NetworkMap{
|
||||
SelfNode: (&tailcfg.Node{
|
||||
Name: "test-node",
|
||||
Addresses: tt.addrs,
|
||||
CapMap: tailcfg.NodeCapMap{
|
||||
nodecap.ServiceHost: []tailcfg.RawMessage{tailcfg.RawMessage(svcIPMapJSON)},
|
||||
},
|
||||
}).View(),
|
||||
}
|
||||
lb.currentNode().SetNetMap(nm)
|
||||
rcfg := lb.routerConfigLocked(&wgcfg.Config{Addresses: tt.addrs}, new(ipn.Prefs).View(), nm)
|
||||
if !slices.Equal(rcfg.ServiceAddrs, tt.want) {
|
||||
t.Errorf("ServiceAddrs = %v; want %v", rcfg.ServiceAddrs, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdvertiseRoute_InvalidPrefix tests that AdvertiseRoute rejects routes
|
||||
// with non-address bits set in the prefix.
|
||||
func TestAdvertiseRoute_InvalidPrefix(t *testing.T) {
|
||||
|
||||
@@ -493,21 +493,37 @@ func (r *linuxRouter) Set(cfg *router.Config) error {
|
||||
// isn't desired and that we don't already track (prevAddrs). Like cidrDiff,
|
||||
// this trusts cfg.LocalAddrs to be authoritative. See #19974.
|
||||
//
|
||||
// The sweep leaves cfg.ServiceAddrs alone. An operator serving a Tailscale
|
||||
// Service in TUN mode may assign its addresses to the tunnel interface.
|
||||
// The sweep is also skipped while cfg.LocalAddrs is empty, such as after
|
||||
// "tailscale down". That config carries no ServiceAddrs, so the sweep
|
||||
// could not tell a Service address from a stale one. The next Set with
|
||||
// addresses runs the sweep again.
|
||||
//
|
||||
// TODO(bcreane): a late orphan with no config change -- IPv6 becoming
|
||||
// available, or an external re-add -- isn't caught here; re-run this sweep on
|
||||
// netmon.ChangeDelta to handle it. See tailscale/corp#43882.
|
||||
wantAddrs := set.SetOf(cfg.LocalAddrs)
|
||||
if r.lastScanAddrs == nil || !r.lastScanAddrs.Equal(wantAddrs) {
|
||||
if len(cfg.LocalAddrs) == 0 {
|
||||
r.lastScanAddrs = nil
|
||||
} else if r.lastScanAddrs == nil || !r.lastScanAddrs.Equal(wantAddrs) {
|
||||
if ifaceAddrs, err := r.tailscaleInterfaceAddrs(); err != nil {
|
||||
r.logf("router: enumerating interface addresses failed, skipping orphan cleanup: %v", err)
|
||||
} else {
|
||||
r.lastScanAddrs = wantAddrs
|
||||
kernelAddrs := r.deletableAddrs(ifaceAddrs)
|
||||
serviceAddrs := make(set.Set[netip.Addr], len(cfg.ServiceAddrs))
|
||||
for _, p := range cfg.ServiceAddrs {
|
||||
serviceAddrs.Add(p.Addr())
|
||||
}
|
||||
var removed []netip.Prefix
|
||||
for p := range orphanedAddrs(kernelAddrs, cfg.LocalAddrs) {
|
||||
if prevAddrs[p] {
|
||||
continue // an address we were tracking; cidrDiff already handled it
|
||||
}
|
||||
if serviceAddrs.Contains(p.Addr()) {
|
||||
continue // assigned by the operator for a Service; not ours to remove
|
||||
}
|
||||
if err := r.delAddress(p); err != nil {
|
||||
r.logf("router: removing stale address %v from %s failed: %v", p, r.tunname, err)
|
||||
errs = append(errs, err)
|
||||
|
||||
@@ -1782,6 +1782,89 @@ func TestSetInstallsLoopbackRuleForExistingAddr(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetKeepsServiceAddrs verifies that the orphan sweep does not remove
|
||||
// Service addresses an operator assigned to the tunnel interface for a Service
|
||||
// served in TUN mode. The sweep still removes other stale addresses.
|
||||
func TestSetKeepsServiceAddrs(t *testing.T) {
|
||||
lr, fake := newTestLinuxRouter(t)
|
||||
|
||||
const (
|
||||
vip4 = "100.99.1.1/32 dev tailscale0"
|
||||
vip6 = "fd7a:115c:a1e0::9901/128 dev tailscale0"
|
||||
orphan = "100.64.0.99/32 dev tailscale0"
|
||||
)
|
||||
fake.ips = []string{vip4, vip6, orphan}
|
||||
slices.Sort(fake.ips)
|
||||
|
||||
up := func(localAddr string) *Config {
|
||||
return &Config{
|
||||
LocalAddrs: mustCIDRs(localAddr),
|
||||
ServiceAddrs: mustCIDRs("100.99.1.1/32", "fd7a:115c:a1e0::9901/128"),
|
||||
NetfilterMode: netfilterOff,
|
||||
}
|
||||
}
|
||||
checkVIPs := func(step string) {
|
||||
t.Helper()
|
||||
for _, vip := range []string{vip4, vip6} {
|
||||
if !slices.Contains(fake.ips, vip) {
|
||||
t.Errorf("%s: Service addr %q was removed; ips=%q", step, vip, fake.ips)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := lr.Set(up("100.64.0.1/32")); err != nil {
|
||||
t.Fatalf("Set up: %v", err)
|
||||
}
|
||||
checkVIPs("up")
|
||||
if slices.Contains(fake.ips, orphan) {
|
||||
t.Errorf("orphan %q was not removed; ips=%q", orphan, fake.ips)
|
||||
}
|
||||
|
||||
// "tailscale down" sends an empty config with no ServiceAddrs.
|
||||
if err := lr.Set(&Config{NetfilterMode: netfilterOff}); err != nil {
|
||||
t.Fatalf("Set down: %v", err)
|
||||
}
|
||||
checkVIPs("down")
|
||||
|
||||
// A LocalAddrs change triggers a new sweep.
|
||||
if err := lr.Set(up("100.64.0.2/32")); err != nil {
|
||||
t.Fatalf("Set up with new addr: %v", err)
|
||||
}
|
||||
checkVIPs("up with new addr")
|
||||
}
|
||||
|
||||
// TestSetOrphanScanAfterDown verifies that an orphan that appears while the
|
||||
// router has no addresses stays in place until the next Set with addresses,
|
||||
// and is removed then even if LocalAddrs is the same as before the down.
|
||||
func TestSetOrphanScanAfterDown(t *testing.T) {
|
||||
lr, fake := newTestLinuxRouter(t)
|
||||
|
||||
const orphan = "100.64.0.99/32 dev tailscale0"
|
||||
up := &Config{
|
||||
LocalAddrs: mustCIDRs("100.64.0.1/32"),
|
||||
NetfilterMode: netfilterOff,
|
||||
}
|
||||
if err := lr.Set(up); err != nil {
|
||||
t.Fatalf("Set up: %v", err)
|
||||
}
|
||||
|
||||
fake.ips = append(fake.ips, orphan)
|
||||
slices.Sort(fake.ips)
|
||||
if err := lr.Set(&Config{NetfilterMode: netfilterOff}); err != nil {
|
||||
t.Fatalf("Set down: %v", err)
|
||||
}
|
||||
if !slices.Contains(fake.ips, orphan) {
|
||||
t.Errorf("orphan removed while down; ips=%q", fake.ips)
|
||||
}
|
||||
|
||||
if err := lr.Set(up); err != nil {
|
||||
t.Fatalf("Set up again: %v", err)
|
||||
}
|
||||
if slices.Contains(fake.ips, orphan) {
|
||||
t.Errorf("orphan not removed on the Set after down; ips=%q", fake.ips)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetOrphanScanGatedByAddrChange verifies the interface is scanned for
|
||||
// orphans on the first Set and when LocalAddrs changes, but not for route-only
|
||||
// updates -- so a subnet router churning routes doesn't pay for a scan on every
|
||||
|
||||
@@ -131,6 +131,13 @@ type Config struct {
|
||||
// flow logging and is otherwise ignored.
|
||||
SubnetRoutes []netip.Prefix
|
||||
|
||||
// ServiceAddrs are the addresses of the Tailscale Services (VIP
|
||||
// services) this node hosts. The router does not assign them to the
|
||||
// tunnel interface. Only Linux uses them: an operator serving a
|
||||
// Service in TUN mode may assign them to the tunnel interface, and
|
||||
// the router must not remove them as stale addresses.
|
||||
ServiceAddrs []netip.Prefix
|
||||
|
||||
// SNATSubnetRoutes enables SNAT for traffic to local subnets.
|
||||
// Implemented on Linux (iptables/nftables) and FreeBSD (PF).
|
||||
SNATSubnetRoutes bool
|
||||
@@ -161,5 +168,6 @@ func (c *Config) Clone() *Config {
|
||||
c2.Routes = slices.Clone(c.Routes)
|
||||
c2.LocalRoutes = slices.Clone(c.LocalRoutes)
|
||||
c2.SubnetRoutes = slices.Clone(c.SubnetRoutes)
|
||||
c2.ServiceAddrs = slices.Clone(c.ServiceAddrs)
|
||||
return &c2
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
func TestConfigEqual(t *testing.T) {
|
||||
testedFields := []string{
|
||||
"LocalAddrs", "Routes", "LocalRoutes", "NewMTU",
|
||||
"SubnetRoutes", "SNATSubnetRoutes", "StatefulFiltering",
|
||||
"SubnetRoutes", "ServiceAddrs", "SNATSubnetRoutes", "StatefulFiltering",
|
||||
"NetfilterMode", "NetfilterKind", "RemoveCGNATDropRule",
|
||||
}
|
||||
configType := reflect.TypeFor[Config]()
|
||||
@@ -106,6 +106,16 @@ func TestConfigEqual(t *testing.T) {
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
&Config{ServiceAddrs: nets("100.99.1.1/32")},
|
||||
&Config{ServiceAddrs: nets("100.99.1.2/32")},
|
||||
false,
|
||||
},
|
||||
{
|
||||
&Config{ServiceAddrs: nets("100.99.1.1/32")},
|
||||
&Config{ServiceAddrs: nets("100.99.1.1/32")},
|
||||
true,
|
||||
},
|
||||
{
|
||||
&Config{SNATSubnetRoutes: false},
|
||||
&Config{SNATSubnetRoutes: true},
|
||||
|
||||
Reference in new issue
Block a user