From 4f7439b415ebbc74a7e623f4f27ffbc98708faf2 Mon Sep 17 00:00:00 2001 From: Mazdak Nasab Date: Fri, 20 Nov 2026 14:48:51 -0700 Subject: [PATCH] feature/conn25: name lock-requiring methods Locked (#22532) Two methods read or write state guarded by their receiver's mutex without taking it, relying on every caller to hold it, but were not named to say so. enqueueAddressAssignment documented the requirement in prose only; lookupAddrBySrcIPAndTransitIP had no doc comment at all. Rename both with the Locked suffix the package already uses for drainAddrsLocked, transitIPRoutableLocked and removeFlowLocked, and give the connector lookup a doc comment stating the requirement. Fixes https://github.com/tailscale/corp/issues/38924 Change-Id: Ib8bcaf5809ae6f982a504ae554ad1304dbb53018 Signed-off-by: Mazdak Nasab --- feature/conn25/client.go | 14 +++++++------- feature/conn25/client_test.go | 6 +++--- feature/conn25/conn25_test.go | 2 +- feature/conn25/connector.go | 7 +++++-- feature/conn25/connector_test.go | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/feature/conn25/client.go b/feature/conn25/client.go index 8b961907e..5a18d6ac0 100644 --- a/feature/conn25/client.go +++ b/feature/conn25/client.go @@ -157,8 +157,8 @@ func (c *client) reserveAddresses(appName string, domain dnsname.FQDN, dst netip if existing, ok := c.assignments.lookupByDomainDst(domain, dst); ok { // We have the domain/dst, but it's possible this came from another connector // and the original connector is dead, in which case we currently don't - // call enqueueAddressAssignment and register the TransitIP with the other - // connector. And we don't install the new connector's node key in WireGuard. + // call enqueueAddressAssignmentLocked and register the TransitIP with the + // other connector. And we don't install the new connector's node key in WireGuard. // // The correct action would be to allocate a new TransitIP associated // with that new connector, and then register the TransitIP address, and @@ -241,7 +241,7 @@ func (c *client) reserveAddresses(appName string, domain dnsname.FQDN, dst netip transitPool.returnAddr(tip) return nil, err } - if err := c.enqueueAddressAssignment(as); err != nil { + if err := c.enqueueAddressAssignmentLocked(as); err != nil { c.assignments.remove(as) magicPool.returnAddr(mip) transitPool.returnAddr(tip) @@ -269,9 +269,9 @@ var errQueueFull = errors.New("queue full") // no longer accepts address assignments. var errClientShutdown = errors.New("conn25 client shut down") -// enqueueAddressAssignment queues addrs to be registered with its connector by -// sendLoop. c.mu must be held. -func (c *client) enqueueAddressAssignment(addrs *addrs) error { +// enqueueAddressAssignmentLocked queues addrs to be registered with its +// connector by sendLoop. c.mu must be held. +func (c *client) enqueueAddressAssignmentLocked(addrs *addrs) error { if c.closed { return errClientShutdown } @@ -489,7 +489,7 @@ func (c *client) resendTransitIPMapping(transitIP netip.Addr) { // We have no mappings for this transit IP, so nothing to resend. return } - err := c.enqueueAddressAssignment(mapping) + err := c.enqueueAddressAssignmentLocked(mapping) if err != nil { c.logf("error enqueueing address assignment for resend: %v", err) } diff --git a/feature/conn25/client_test.go b/feature/conn25/client_test.go index ab67e4f06..e5d5e7caa 100644 --- a/feature/conn25/client_test.go +++ b/feature/conn25/client_test.go @@ -411,8 +411,8 @@ func (c *client) currentGeneration() uint64 { return c.generation } -// enqueueAddressAssignmentForTest is [client.enqueueAddressAssignment] with the -// lock it expects callers to hold, plus the generation stamp that +// enqueueAddressAssignmentForTest is [client.enqueueAddressAssignmentLocked] +// with the lock it expects callers to hold, plus the generation stamp that // [client.reserveAddresses] would have applied. The production callers all // enqueue from inside a c.mu critical section; tests reach for it on its own, // and build the [addrs] by hand rather than reserving one. Without the stamp @@ -422,7 +422,7 @@ func (c *client) enqueueAddressAssignmentForTest(as *addrs) error { c.mu.Lock() defer c.mu.Unlock() as.generation = c.generation - return c.enqueueAddressAssignment(as) + return c.enqueueAddressAssignmentLocked(as) } func TestTransitIPConnMapping(t *testing.T) { diff --git a/feature/conn25/conn25_test.go b/feature/conn25/conn25_test.go index aecfbc46c..dbd2f1fd0 100644 --- a/feature/conn25/conn25_test.go +++ b/feature/conn25/conn25_test.go @@ -643,7 +643,7 @@ func TestHandleConnectorTransitIPRequest(t *testing.T) { i, j, len(wantLookup)) } pip, tip, wantDip := wantLookup[0], wantLookup[1], wantLookup[2] - gotDip, _ := c.connector.lookupAddrBySrcIPAndTransitIP(pip, tip) + gotDip, _ := c.connector.lookupAddrBySrcIPAndTransitIPLocked(pip, tip) if gotDip != wantDip { t.Errorf("wrong result on lookup[%d][%d] ([%v], [%v]): got [%v] expected [%v]", i, j, pip, tip, gotDip, wantDip) diff --git a/feature/conn25/connector.go b/feature/conn25/connector.go index 267147f9b..1424e12e6 100644 --- a/feature/conn25/connector.go +++ b/feature/conn25/connector.go @@ -116,7 +116,7 @@ type transitIPExpiryEntry struct { func (c *connector) realIPForTransitIPConnection(srcIP netip.Addr, transitIP netip.Addr) (netip.Addr, bool) { c.mu.Lock() defer c.mu.Unlock() - return c.lookupAddrBySrcIPAndTransitIP(srcIP, transitIP) + return c.lookupAddrBySrcIPAndTransitIPLocked(srcIP, transitIP) } // transitIPInPool reports whether tip is within the transit IP pool of its @@ -164,7 +164,10 @@ func (c *connector) packetFilterAllow(p packet.Parsed) (bool, string) { return false, "" } -func (c *connector) lookupAddrBySrcIPAndTransitIP(srcIP, transitIP netip.Addr) (netip.Addr, bool) { +// lookupAddrBySrcIPAndTransitIPLocked returns the real destination IP that the +// peer at srcIP registered for transitIP, or (zero, false) if it registered +// none. c.mu must be held. +func (c *connector) lookupAddrBySrcIPAndTransitIPLocked(srcIP, transitIP netip.Addr) (netip.Addr, bool) { m, ok := c.transitIPs[srcIP] if !ok || m == nil { return netip.Addr{}, false diff --git a/feature/conn25/connector_test.go b/feature/conn25/connector_test.go index d48277817..3ca6e09bc 100644 --- a/feature/conn25/connector_test.go +++ b/feature/conn25/connector_test.go @@ -272,7 +272,7 @@ func TestConnectorDisallowedDestinations(t *testing.T) { if tt.wantCode == OK { wantDst = unmapped } - if got, _ := c.connector.lookupAddrBySrcIPAndTransitIP(peerIP, transitIP); got != wantDst { + if got, _ := c.connector.lookupAddrBySrcIPAndTransitIPLocked(peerIP, transitIP); got != wantDst { t.Errorf("lookup(%v, %v) = %v, want %v", peerIP, transitIP, got, wantDst) } })