From 90feb626cd6ce3b0455217fbf81b9deb0f020ceb Mon Sep 17 00:00:00 2001 From: Michael Ben-Ami Date: Mon, 16 Nov 2026 16:15:18 -0500 Subject: [PATCH] feature/conn25: key connector transit IPs by node ID, not node key The client tracked which transit IPs belong to which connector in a map keyed by the connector's node key. The ExtraWireGuardAllowedIPs hook's results and the route manager are keyed by node ID, though, and a node key can rotate while the node ID stays the same, which would orphan a connector's transit IPs under the stale key. Key the map by node ID instead, and rename byConnKey and lookupTransitIPsByConnKey to match. addTransitIPForConnector now rejects connectors with a zero node ID rather than a zero node key, since the ID is what the mapping is keyed on. Updates tailscale/corp#51237 Signed-off-by: Michael Ben-Ami --- feature/conn25/addrAssignments.go | 2 +- feature/conn25/client.go | 36 +++++++++++++++---------------- feature/conn25/client_test.go | 8 +++---- feature/conn25/conn25.go | 8 +++---- feature/conn25/conn25_test.go | 18 ++++++++-------- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/feature/conn25/addrAssignments.go b/feature/conn25/addrAssignments.go index 448a53346..1e3ad28d7 100644 --- a/feature/conn25/addrAssignments.go +++ b/feature/conn25/addrAssignments.go @@ -23,7 +23,7 @@ type domainDst struct { // addrAssignments is the collection of addrs assigned by this client // supporting lookup by magic IP, transit IP or domain+dst, or to lookup all -// transit IPs associated with a given connector (identified by its node key). +// transit IPs associated with a given connector (identified by its node ID). type addrAssignments struct { byMagicIP map[netip.Addr]*addrs byTransitIP map[netip.Addr]*addrs diff --git a/feature/conn25/client.go b/feature/conn25/client.go index e70a9f242..e28bc2bde 100644 --- a/feature/conn25/client.go +++ b/feature/conn25/client.go @@ -13,7 +13,6 @@ import ( "golang.org/x/net/dns/dnsmessage" "tailscale.com/net/packet" "tailscale.com/tailcfg" - "tailscale.com/types/key" "tailscale.com/types/logger" "tailscale.com/types/views" "tailscale.com/util/dnsname" @@ -39,7 +38,7 @@ type client struct { v6MagicIPPool *ippool v6TransitIPPool *ippool assignments addrAssignments - byConnKey map[key.NodePublic]set.Set[netip.Prefix] + byConnID map[tailcfg.NodeID]set.Set[netip.Prefix] } // errNoMagicIPAssignment is returned by [client.PrepareNewFlowForMagicIP] when @@ -58,7 +57,7 @@ func (c *client) transitIPRoutableLocked(as *addrs) bool { return false } tip := as.connectorAddr.transit - return c.byConnKey[conn.Key()].Contains(netip.PrefixFrom(tip, tip.BitLen())) + return c.byConnID[conn.ID()].Contains(netip.PrefixFrom(tip, tip.BitLen())) } // linkLocalAllow returns true if the provided packet with a link-local Dst address has a @@ -106,7 +105,7 @@ func (c *client) reset() { c.v6MagicIPPool = newIPPool(ipSets.v6Magic) c.v6TransitIPPool = newIPPool(ipSets.v6Transit) c.assignments = addrAssignments{clock: c.assignments.clock} - c.byConnKey = nil + c.byConnID = nil // Drop any assignments still waiting to be sent. They describe the state // we just cleared, so delivering them would register transit IPs with @@ -256,11 +255,10 @@ func (c *client) addTransitIPForConnector(as addrs) error { if !conn.Valid() { return fmt.Errorf("unexpectedly received invalid connector node") } - if conn.Key().IsZero() { - return fmt.Errorf("node with stable ID %q does not have a key", conn.StableID()) + if conn.ID() == 0 { + return fmt.Errorf("node with stable ID %q does not have a node ID", conn.StableID()) } - conn.ID() - return c.insertTransitConnMapping(as, conn.Key()) + return c.insertTransitConnMapping(as, conn.ID()) } // errQueueFull is returned when the address assignment send queue is full and @@ -349,8 +347,8 @@ func (c *client) prepareNewFlowForMagicIP(magicIP netip.Addr) (ClientFlowRef, er }}, nil } -func (c *client) extraWireGuardAllowedIPs(k key.NodePublic) views.Slice[netip.Prefix] { - tips, ok := c.lookupTransitIPsByConnKey(k) +func (c *client) extraWireGuardAllowedIPs(id tailcfg.NodeID) views.Slice[netip.Prefix] { + tips, ok := c.lookupTransitIPsByConnID(id) if !ok { return views.Slice[netip.Prefix]{} } @@ -443,11 +441,11 @@ func (c *client) assignmentIsCurrent(as addrs) bool { return as.generation == c.generation } -// insertTransitConnMapping adds an entry to the byConnKey map +// insertTransitConnMapping adds an entry to the byConnID map // for the provided transitIP (as a prefix). // The provided assignment must already be present in the byTransitIP map, and // must be from the current generation. -func (c *client) insertTransitConnMapping(as addrs, connKey key.NodePublic) error { +func (c *client) insertTransitConnMapping(as addrs, connID tailcfg.NodeID) error { c.mu.Lock() defer c.mu.Unlock() if as.generation != c.generation { @@ -458,23 +456,23 @@ func (c *client) insertTransitConnMapping(as addrs, connKey key.NodePublic) erro return errors.New("transit IP is not already known") } - ctips, ok := c.byConnKey[connKey] + ctips, ok := c.byConnID[connID] tipp := netip.PrefixFrom(tip, tip.BitLen()) if !ok { ctips.Make() - mak.Set(&c.byConnKey, connKey, ctips) + mak.Set(&c.byConnID, connID, ctips) } ctips.Add(tipp) return nil } -// lookupTransitIPsByConnKey returns a slice containing the transit IPs (as netipPrefix) -// associated with the given connector (identified by node key), or (nil, false) if there is no entry -// for the given key. -func (c *client) lookupTransitIPsByConnKey(k key.NodePublic) ([]netip.Prefix, bool) { +// lookupTransitIPsByConnID returns a slice containing the transit IPs (as netipPrefix) +// associated with the given connector (identified by node ID), or (nil, false) if there is no entry +// for the given ID. +func (c *client) lookupTransitIPsByConnID(id tailcfg.NodeID) ([]netip.Prefix, bool) { c.mu.Lock() defer c.mu.Unlock() - s, ok := c.byConnKey[k] + s, ok := c.byConnID[id] if !ok { return nil, false } diff --git a/feature/conn25/client_test.go b/feature/conn25/client_test.go index 5abfde6ad..ab67e4f06 100644 --- a/feature/conn25/client_test.go +++ b/feature/conn25/client_test.go @@ -400,7 +400,7 @@ func TestResetDropsInFlightAssignment(t *testing.T) { if !errors.Is(err, errStaleGeneration) { t.Errorf("applying an assignment from before the reset: got error %v, want %v", err, errStaleGeneration) } - if tips, ok := c.lookupTransitIPsByConnKey(oldConnector.Key()); ok { + if tips, ok := c.lookupTransitIPsByConnID(oldConnector.ID()); ok { t.Errorf("connector from before the reset got transit IPs registered: %v", tips) } } @@ -449,7 +449,7 @@ func TestTransitIPConnMapping(t *testing.T) { ID: tailcfg.NodeID(0), Tags: []string{"tag:woo"}, Hostinfo: (&tailcfg.Hostinfo{AppConnector: opt.NewBool(true)}).View(), - Key: key.NodePublic{}, + Key: key.NodePublicFromRaw32(mem.B([]byte{0: 0xff, 31: 0x01})), }).View(), (&tailcfg.Node{ ID: tailcfg.NodeID(2), @@ -467,9 +467,9 @@ func TestTransitIPConnMapping(t *testing.T) { // Insert the address assignments conn25.client.assignments.insert(as) - // Adding a transit IP for a node with an unset key should fail + // Adding a transit IP for a node with an unset node ID should fail if err := conn25.client.addTransitIPForConnector(forConnector(connectorPeers[0])); err == nil { - t.Error("adding an transit IP mapping for a connector with a zero key should fail") + t.Error("adding an transit IP mapping for a connector with a zero node ID should fail") } // Adding a transit IP that is known should succeed if err := conn25.client.addTransitIPForConnector(forConnector(connectorPeers[1])); err != nil { diff --git a/feature/conn25/conn25.go b/feature/conn25/conn25.go index 5b42f7c7a..9b72c2b82 100644 --- a/feature/conn25/conn25.go +++ b/feature/conn25/conn25.go @@ -292,8 +292,8 @@ func (e *extension) installHooks(dph *datapathHandler) error { return nil } var extras map[tailcfg.NodeID][]netip.Prefix - for id, k := range peers { - if pfxs := e.extraWireGuardAllowedIPs(k); pfxs.Len() > 0 { + for id := range peers { + if pfxs := e.extraWireGuardAllowedIPs(id); pfxs.Len() > 0 { mak.Set(&extras, id, pfxs.AsSlice()) } } @@ -497,8 +497,8 @@ func (e *extension) profileStateChange(loginProfile ipn.LoginProfileView, prefs } } -func (e *extension) extraWireGuardAllowedIPs(k key.NodePublic) views.Slice[netip.Prefix] { - return e.conn25.client.extraWireGuardAllowedIPs(k) +func (e *extension) extraWireGuardAllowedIPs(id tailcfg.NodeID) views.Slice[netip.Prefix] { + return e.conn25.client.extraWireGuardAllowedIPs(id) } // Conn25 holds state for routing traffic for a domain via a connector. diff --git a/feature/conn25/conn25_test.go b/feature/conn25/conn25_test.go index 95e2cebaf..46be6211f 100644 --- a/feature/conn25/conn25_test.go +++ b/feature/conn25/conn25_test.go @@ -2172,7 +2172,7 @@ func TestHandleAddressAssignmentDropsStaleGeneration(t *testing.T) { case <-time.After(5 * time.Second): t.Fatal("timed out waiting for AuthReconfigAsync to be called") } - if _, ok := c.lookupTransitIPsByConnKey(connectorPeer.Key()); !ok { + if _, ok := c.lookupTransitIPsByConnID(connectorPeer.ID()); !ok { t.Fatal("control assignment did not register a transit IP for the connector") } @@ -2183,7 +2183,7 @@ func TestHandleAddressAssignmentDropsStaleGeneration(t *testing.T) { if err := ext.handleAddressAssignment(context.Background(), *as); err != nil { t.Errorf("handling an assignment from before the reset: got error %v, want nil", err) } - if _, ok := c.lookupTransitIPsByConnKey(connectorPeer.Key()); ok { + if _, ok := c.lookupTransitIPsByConnID(connectorPeer.ID()); ok { t.Error("connector from before the reset got transit IPs registered") } select { @@ -3287,7 +3287,7 @@ func TestHandleAddressAssignmentStoresTransitIPs(t *testing.T) { ext.conn25.reconfig(cfg) type lookup struct { - connKey key.NodePublic + connID tailcfg.NodeID expectedIPs []netip.Prefix expectedOk bool } @@ -3318,14 +3318,14 @@ func TestHandleAddressAssignmentStoresTransitIPs(t *testing.T) { }, lookups: []lookup{ { - connKey: connectorPeers[0].Key(), + connID: connectorPeers[0].ID(), expectedIPs: []netip.Prefix{ transitIPs[0], }, expectedOk: true, }, { - connKey: connectorPeers[1].Key(), + connID: connectorPeers[1].ID(), expectedIPs: nil, expectedOk: false, }, @@ -3345,7 +3345,7 @@ func TestHandleAddressAssignmentStoresTransitIPs(t *testing.T) { }, lookups: []lookup{ { - connKey: connectorPeers[0].Key(), + connID: connectorPeers[0].ID(), expectedIPs: []netip.Prefix{ transitIPs[0], transitIPs[1], @@ -3368,7 +3368,7 @@ func TestHandleAddressAssignmentStoresTransitIPs(t *testing.T) { }, lookups: []lookup{ { - connKey: connectorPeers[0].Key(), + connID: connectorPeers[0].ID(), expectedIPs: []netip.Prefix{ transitIPs[0], transitIPs[1], @@ -3376,7 +3376,7 @@ func TestHandleAddressAssignmentStoresTransitIPs(t *testing.T) { expectedOk: true, }, { - connKey: connectorPeers[1].Key(), + connID: connectorPeers[1].ID(), expectedIPs: []netip.Prefix{ transitIPs[2], }, @@ -3404,7 +3404,7 @@ func TestHandleAddressAssignmentStoresTransitIPs(t *testing.T) { // Check that each of the lookups behaves as expected for i, lu := range tt.lookups { - got, ok := ext.conn25.client.lookupTransitIPsByConnKey(lu.connKey) + got, ok := ext.conn25.client.lookupTransitIPsByConnID(lu.connID) if ok != lu.expectedOk { t.Fatalf("unexpected ok result at index %d wanted %v, got %v", i, lu.expectedOk, ok) }