diff --git a/feature/conn25/client.go b/feature/conn25/client.go index 5a18d6ac0..6a5493964 100644 --- a/feature/conn25/client.go +++ b/feature/conn25/client.go @@ -12,6 +12,7 @@ import ( "golang.org/x/net/dns/dnsmessage" "tailscale.com/net/packet" + "tailscale.com/net/tsaddr" "tailscale.com/tailcfg" "tailscale.com/types/logger" "tailscale.com/types/views" @@ -474,7 +475,10 @@ func (c *client) lookupTransitIPsByConnID(id tailcfg.NodeID) ([]netip.Prefix, bo if !ok { return nil, false } - return s.Slice(), true + tips := s.Slice() + // Sort the result, so an unchanged set of transit IPs yields an identical slice on every call. + tsaddr.SortPrefixes(tips) + return tips, true } // resendTransitIPMapping enqueues a request to re-establish an existing diff --git a/feature/conn25/client_test.go b/feature/conn25/client_test.go index e5d5e7caa..41672f02e 100644 --- a/feature/conn25/client_test.go +++ b/feature/conn25/client_test.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "net/netip" + "slices" "sync" "testing" "time" @@ -16,6 +17,7 @@ import ( "golang.org/x/net/dns/dnsmessage" "tailscale.com/net/dns/resolver" "tailscale.com/net/packet" + "tailscale.com/net/tsaddr" "tailscale.com/tailcfg" "tailscale.com/tstest" "tailscale.com/types/appctype" @@ -482,6 +484,70 @@ func TestTransitIPConnMapping(t *testing.T) { } } +// TestLookupTransitIPsByConnIDOrder covers the stable-order requirement that +// [ipnext.Hooks.ExtraWireGuardAllowedIPs], which this lookup feeds, places on +// its results: an unchanged set of Transit IPs has to come back in an unchanged +// order, or the consumer reads it as a change and resyncs the connector's +// WireGuard peer on every reconfig. byConnID stores the Transit IPs in a +// map-backed set, which iterates in a different order each time, so this only +// holds because the lookup sorts. +func TestLookupTransitIPsByConnIDOrder(t *testing.T) { + const appName = "a" + conn25 := newConn25(t.Logf) + conn25.reconfig(&config{ + isConfigured: true, + appsByName: map[string]appctype.AppConnectorAttr{appName: {}}, + ipSets: ipSets{ + v4Magic: mustIPSetFromPrefix("100.64.0.0/24"), + v6Magic: mustIPSetFromPrefix("fd7a:115c:a1e0:a99c:0100::/80"), + v4Transit: mustIPSetFromPrefix("169.254.0.0/24"), + v6Transit: mustIPSetFromPrefix("fd7a:115c:a1e0:a99c:0200::/80"), + }, + }) + c := conn25.client + + // Reserve a handful of assignments against one connector, mixing families + // so the result spans the v4/v6 boundary. + var reserved []*addrs + for i := range 4 { + for _, dst := range []netip.Addr{ + netip.MustParseAddr(fmt.Sprintf("0.0.0.%d", i+1)), + netip.MustParseAddr(fmt.Sprintf("2001:db8::%d", i+1)), + } { + as, err := c.reserveAddresses(appName, "example.com.", dst, time.Minute, testConnectorPeer, "") + if err != nil { + t.Fatalf("reserveAddresses(%v): %v", dst, err) + } + reserved = append(reserved, as) + } + } + // Register them in reverse, so insertion order isn't the sorted order. + for i := len(reserved) - 1; i >= 0; i-- { + if err := c.addTransitIPForConnector(*reserved[i]); err != nil { + t.Fatalf("addTransitIPForConnector(%v): %v", reserved[i].connectorAddr.transit, err) + } + } + + want := make([]netip.Prefix, 0, len(reserved)) + for _, as := range reserved { + tip := as.connectorAddr.transit + want = append(want, netip.PrefixFrom(tip, tip.BitLen())) + } + tsaddr.SortPrefixes(want) + + // Repeat the lookup: one sorted result could be the luck of the map, but + // a run of identical ones can only come from sorting. + for range 10 { + got, ok := c.lookupTransitIPsByConnID(testConnectorPeer.ID()) + if !ok { + t.Fatalf("no Transit IPs registered for connector %d", testConnectorPeer.ID()) + } + if !slices.Equal(got, want) { + t.Fatalf("Transit IPs out of order:\n got %v\nwant %v", got, want) + } + } +} + // registerTransitIPs registers each assignment's Transit IP with its connector, // as the PeerAPI round trip in sendLoop does in production, so that // [client.prepareNewFlowForMagicIP] considers the Transit IP routable. With no diff --git a/feature/conn25/conn25_test.go b/feature/conn25/conn25_test.go index 03e486f15..a5e8ab40f 100644 --- a/feature/conn25/conn25_test.go +++ b/feature/conn25/conn25_test.go @@ -3275,7 +3275,6 @@ func TestHandleAddressAssignmentStoresTransitIPs(t *testing.T) { if ok != lu.expectedOk { t.Fatalf("unexpected ok result at index %d wanted %v, got %v", i, lu.expectedOk, ok) } - slices.SortFunc(got, func(a, b netip.Prefix) int { return a.Compare(b) }) if diff := cmp.Diff(lu.expectedIPs, got, cmpopts.EquateComparable(netip.Prefix{})); diff != "" { t.Fatalf("transit IPs mismatch at index %d, (-want +got):\n%s", i, diff) }