feature/conn25: return extra WireGuard AllowedIPs in a stable order (#22630)

lookupTransitIPsByConnID returned the connector's Transit IPs straight
from a map-backed set, in map-iteration order. set.Set.Slice can't order
them: it only sorts ordered kinds, and netip.Prefix is a struct.

Both ends of the ExtraWireGuardAllowedIPs path require a stable order so
that unchanged entries are detected as no-ops: the hook's contract in
ipnext, and routemanager.Mutation.SetExtraAllowedIPs, whose applyExtras
compares each peer's prefixes with slices.Equal. Without it an unchanged
set looked changed, resyncing the WireGuard device peer for every
connector on every reconfig.

Sort the result, and drop the workaround sort in TestTransitIPConnMapping
that was masking this, so that test asserts the order too.

Fixes https://github.com/tailscale/corp/issues/52137

Change-Id: Ie4b17d69fb304a6411f534322314082a7feea542

Signed-off-by: Mazdak Nasab <mazdak.nasab@gmail.com>
This commit is contained in:
Mazdak Nasab authored and GitHub committed 2026-11-24 12:36:00 -07:00
1 parent 8bcc919dcf
commit 076d07d626
3 files changed
+71 -2

No files matched your search

+5 -1
View File
@@ -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
+66
View File
@@ -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
-1
View File
@@ -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)
}