ipn: configure hybrid handshakes for peers that support it

Updates tailscale/corp#50068

Signed-off-by: David Anderson <[email protected]>
Change-Id: Ibebab03c31b3fceba83af28d72ddecd26a6a6964
This commit is contained in:
David Anderson authored and Dave Anderson committed 2026-11-17 13:27:24 -07:00
1 parent e232b0a131
commit f4336907aa
5 files changed
+240 -35

No files matched your search

+18 -3
View File
@@ -2648,6 +2648,13 @@ func (b *LocalBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (handled bo
}
b.setDataPlanePeerRoutes()
// Sync peers whose handshake type changed. This change takes effect
// on the next handshake with this peer, so sync before triggering
// handshakes based on disco key changes.
for k := range deltaRes.HandshakeTypeChanged {
b.e.SyncDevicePeer(k)
}
// Mark the WireGuard session for an opportunistic handshake for peers whose
// disco key changed in a way that indicates a restart, flushing their dead
// session keys; or control catching up to a TSMP learned key.
@@ -7747,7 +7754,7 @@ func (b *LocalBackend) setNetMapLocked(nm *netmap.NetworkMap) {
if nm != nil {
login = cmp.Or(profileFromView(nm.UserProfiles[nm.User()]).LoginName, "<missing-profile>")
}
discoChanged, routeChanged := b.currentNode().SetNetMap(nm)
res := b.currentNode().SetNetMap(nm)
// A profile reset swaps in a fresh nodeBackend before clearing its map,
// so notify on every clear even if this node never received a map.
if !b.shutdownCalled && (oldNetMap == nil || nm == nil) {
@@ -7769,16 +7776,24 @@ func (b *LocalBackend) setNetMapLocked(nm *netmap.NetworkMap) {
ms.SetNetworkMap(tailcfg.NodeView{}, nil)
}
}
// Sync peers whose handshake type changed. This change takes effect
// on the next handshake with this peer, so sync before triggering
// handshakes based on disco key changes.
for k := range res.HandshakeTypeChanged {
b.e.SyncDevicePeer(k)
}
// Reset the WireGuard session for peers whose disco key changed in
// a way that indicates a restart, flushing their dead session keys;
// each such peer is lazily re-created on demand with current state.
for _, k := range discoChanged {
for k := range res.DiscoChanged {
b.e.MarkDevicePeerForHandshake(k)
}
// Converge the wireguard-go device for peers whose routes changed
// (or that were removed) in the full-netmap resync above; peers not
// in routeChanged are already up to date.
for k := range routeChanged {
for k := range res.ChangedAllowedIPs {
b.e.SyncDevicePeer(k)
}
if login != b.activeLogin {
+67 -19
View File
@@ -38,6 +38,7 @@ import (
"tailscale.com/util/slicesx"
"tailscale.com/util/testenv"
"tailscale.com/wgengine/filter"
"tailscale.com/wgengine/wgcfg"
)
// nodeBackend is node-specific [LocalBackend] state. It is usually the current node.
@@ -271,20 +272,32 @@ func (nb *nodeBackend) NodeByKey(k key.NodePublic) (_ tailcfg.NodeID, ok bool) {
return nid, ok
}
// PeerAllowedIPs returns the prefixes from which the peer with the
// given public key is currently allowed to originate traffic, or
// ok=false if the key is unknown or the peer currently contributes no
// prefixes.
func (nb *nodeBackend) PeerAllowedIPs(k key.NodePublic) ([]netip.Prefix, bool) {
// PeerWGConfig returns the [wgcfg.PeerConfig] to use for the peer with the
// given public key, or ok=false if the key is unknown or the peer currently
// contributes no routes.
func (nb *nodeBackend) PeerWGConfig(k key.NodePublic) (_ wgcfg.PeerConfig, ok bool) {
nb.mu.Lock()
defer nb.mu.Unlock()
id, ok := nb.nodeByKey[k]
if !ok {
return nil, false
return wgcfg.PeerConfig{}, false
}
// Holding nb.mu satisfies routeMgr's serialization requirement:
// all routeMgr mutations also run under nb.mu.
return nb.routeMgr.PeerAllowedIPs(id)
peer, ok := nb.peers[id]
if !ok {
return wgcfg.PeerConfig{}, false
}
hybrid := peerUsesHybridHandshakes(peer)
allowedIPs, ok := nb.routeMgr.PeerAllowedIPs(id)
if !ok {
return wgcfg.PeerConfig{}, false
}
return wgcfg.PeerConfig{AllowedIPs: allowedIPs, Hybrid: hybrid}, true
}
func peerUsesHybridHandshakes(peer tailcfg.NodeView) bool {
return peer.Cap() >= 152 && !peer.HasCap(nodecap.DisableHybridHandshakes)
}
// NodeByWireGuardString returns the node ID of the peer whose
@@ -647,7 +660,24 @@ func (nb *nodeBackend) netMapWithPeers() *netmap.NetworkMap {
return nm
}
func (nb *nodeBackend) SetNetMap(nm *netmap.NetworkMap) (discoChanged []key.NodePublic, routeChanged routemanager.PeersWithRouteChanges) {
// netmapResult describes the side effects of applying a new netmap
// that the caller must propagate.
type netmapResult struct {
// ChangedAllowedIPs are the peers whose allowed source prefixes
// changed; the caller syncs those peers to the WireGuard device.
ChangedAllowedIPs routemanager.PeersWithRouteChanges
// DiscoChanged is the set of peers whose disco key changed in a
// way that requires a WireGuard session reset (see
// [nodeBackend.discoChanged]).
DiscoChanged set.Set[key.NodePublic]
// HandshakeTypeChanged is the set of peers whose handshake type (standard
// or hybrid post-quantum) changed.
HandshakeTypeChanged set.Set[key.NodePublic]
}
func (nb *nodeBackend) SetNetMap(nm *netmap.NetworkMap) (res netmapResult) {
nb.mu.Lock()
defer nb.mu.Unlock()
nb.netMap = nm
@@ -655,7 +685,7 @@ func (nb *nodeBackend) SetNetMap(nm *netmap.NetworkMap) (discoChanged []key.Node
nb.updateNodeByKeyLocked()
nb.updateNodeByStableIDLocked()
nb.updateNodeByNameLocked()
discoChanged, routeChanged = nb.updatePeersLocked()
res = nb.updatePeersLocked()
nb.signalKeyWaitersForTestLocked()
if nm != nil {
nb.userProfiles = maps.Clone(nm.UserProfiles)
@@ -668,7 +698,7 @@ func (nb *nodeBackend) SetNetMap(nm *netmap.NetworkMap) (discoChanged []key.Node
nb.packetFilter = nil
nb.derpMapViewPub.Publish(tailcfg.DERPMapView{})
}
return discoChanged, routeChanged
return res
}
// peerNodeKeys returns the node public keys of all current peers.
@@ -858,17 +888,21 @@ func (nb *nodeBackend) ExtraDNSByName(hostname string) (_ netip.Addr, ok bool) {
return ip, ok
}
func (nb *nodeBackend) updatePeersLocked() (discoChanged []key.NodePublic, routeChanged routemanager.PeersWithRouteChanges) {
func (nb *nodeBackend) updatePeersLocked() (res netmapResult) {
nm := nb.netMap
oldIDs := slices.Collect(maps.Keys(nb.peers))
// Snapshot the previous disco keys (by node key) before the peers
// map is repopulated, to detect restarted peers below.
var prevDisco map[key.NodePublic]key.DiscoPublic
// Snapshot the previous disco keys and hybrid handshake status (by node key)
// before the peers map is repopulated, to report changes to caller.
var (
prevDisco map[key.NodePublic]key.DiscoPublic
prevHybrid map[key.NodePublic]bool
)
for _, p := range nb.peers {
if !p.DiscoKey().IsZero() {
mak.Set(&prevDisco, p.Key(), p.DiscoKey())
}
mak.Set(&prevHybrid, p.Key(), peerUsesHybridHandshakes(p))
}
if nm == nil {
@@ -883,7 +917,12 @@ func (nb *nodeBackend) updatePeersLocked() (discoChanged []key.NodePublic, route
for _, p := range nb.peers {
if prev, ok := prevDisco[p.Key()]; ok && nb.discoChanged(p.Key(), prev, p.DiscoKey()) {
discoChanged = append(discoChanged, p.Key())
res.DiscoChanged.Make()
res.DiscoChanged.Add(p.Key())
}
if prev, ok := prevHybrid[p.Key()]; ok && peerUsesHybridHandshakes(p) != prev {
res.HandshakeTypeChanged.Make()
res.HandshakeTypeChanged.Add(p.Key())
}
}
@@ -899,8 +938,9 @@ func (nb *nodeBackend) updatePeersLocked() (discoChanged []key.NodePublic, route
for _, p := range nb.peers {
rt.UpsertPeer(p)
}
res := rt.Commit()
return discoChanged, res.AllowedIPs
routeRes := rt.Commit()
res.ChangedAllowedIPs = routeRes.AllowedIPs
return res
}
// discoChanged reports whether a peer's disco key change from prev to
@@ -1065,6 +1105,10 @@ type netmapDeltaResult struct {
// RemovedPeers is a slice of peer stable node IDs (if any) that were
// removed by applying the delta.
RemovedPeers []tailcfg.StableNodeID
// HandshakeTypeChanged is the set of peers whose handshake type (standard
// or hybrid post-quantum) changed.
HandshakeTypeChanged set.Set[key.NodePublic]
}
// UpdateNetmapDelta applies the given netmap mutations to the live
@@ -1103,6 +1147,10 @@ func (nb *nodeBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (res netmap
res.DiscoChanged.Add(m.Node.Key())
}
}
if peerUsesHybridHandshakes(old) != peerUsesHybridHandshakes(m.Node) {
res.HandshakeTypeChanged.Make()
res.HandshakeTypeChanged.Add(m.Node.Key())
}
// Evict index entries derived from the old node value
// before re-adding them from the new one below, so a
// changed name, address, or key doesn't leave a stale
+152 -10
View File
@@ -391,27 +391,27 @@ func TestNodeBackendDiscoChanged(t *testing.T) {
// A brand-new peer is not a disco change.
d1 := newDisco()
if got, _ := nb.SetNetMap(mkNetMap(d1)); len(got) != 0 {
t.Errorf("SetNetMap(new peer) discoChanged = %v; want none", got)
if got := nb.SetNetMap(mkNetMap(d1)); len(got.DiscoChanged) != 0 {
t.Errorf("SetNetMap(new peer) discoChanged = %v; want none", got.DiscoChanged)
}
// A changed disco key requires a session reset.
d2 := newDisco()
if got, _ := nb.SetNetMap(mkNetMap(d2)); !slices.Contains(got, nk) {
t.Errorf("SetNetMap(changed disco) discoChanged = %v; want %v", got, nk)
if got := nb.SetNetMap(mkNetMap(d2)); !got.DiscoChanged.Contains(nk) {
t.Errorf("SetNetMap(changed disco) discoChanged = %v; want %v", got.DiscoChanged, nk)
}
// An unchanged disco key does not.
if got, _ := nb.SetNetMap(mkNetMap(d2)); len(got) != 0 {
t.Errorf("SetNetMap(same disco) discoChanged = %v; want none", got)
if got := nb.SetNetMap(mkNetMap(d2)); len(got.DiscoChanged) != 0 {
t.Errorf("SetNetMap(same disco) discoChanged = %v; want none", got.DiscoChanged)
}
// Transitions to or from a zero disco key never reset.
if got, _ := nb.SetNetMap(mkNetMap(key.DiscoPublic{})); len(got) != 0 {
t.Errorf("SetNetMap(to zero disco) discoChanged = %v; want none", got)
if got := nb.SetNetMap(mkNetMap(key.DiscoPublic{})); len(got.DiscoChanged) != 0 {
t.Errorf("SetNetMap(to zero disco) discoChanged = %v; want none", got.DiscoChanged)
}
if got, _ := nb.SetNetMap(mkNetMap(d1)); len(got) != 0 {
t.Errorf("SetNetMap(from zero disco) discoChanged = %v; want none", got)
if got := nb.SetNetMap(mkNetMap(d1)); len(got.DiscoChanged) != 0 {
t.Errorf("SetNetMap(from zero disco) discoChanged = %v; want none", got.DiscoChanged)
}
}
@@ -494,6 +494,148 @@ func TestNodeBackendDeltaUnknownPeer(t *testing.T) {
}
}
func TestNodeBackendHandshakeChanged(t *testing.T) {
nb := newNodeBackend(t.Context(), tstest.WhileTestRunningLogger(t), eventbus.New())
nk := key.NewNode().Public()
dk := key.NewDisco().Public()
mkNetMap := func(hybrid bool, remoteDisabled bool) *netmap.NetworkMap {
cap := 100
if hybrid {
cap = 152 // first capver to support hybrid handshakes
}
var disable tailcfg.NodeCapMap
if remoteDisabled {
disable = tailcfg.NodeCapMap{
nodecap.DisableHybridHandshakes: nil,
}
}
n := &tailcfg.Node{
ID: 1,
Key: nk,
DiscoKey: dk,
HomeDERP: 1,
Cap: tailcfg.CapabilityVersion(cap),
CapMap: disable,
}
return &netmap.NetworkMap{Peers: []tailcfg.NodeView{n.View()}}
}
empty := &netmap.NetworkMap{}
tests := []struct {
name string
before *netmap.NetworkMap
after *netmap.NetworkMap
wantChange bool
}{
{"add classical peer", empty, mkNetMap(false, false), false},
{"delete classical peer", mkNetMap(false, false), empty, false},
{"add hybrid peer", empty, mkNetMap(true, false), false},
{"delete hybrid peer", mkNetMap(true, false), empty, false},
{"classical to hybrid", mkNetMap(false, false), mkNetMap(true, false), true},
{"hybrid to classical", mkNetMap(true, false), mkNetMap(false, false), true},
{"disable classical peer", mkNetMap(false, false), mkNetMap(false, true), false},
{"enable classical peer", mkNetMap(false, true), mkNetMap(false, false), false},
{"disable hybrid peer", mkNetMap(true, false), mkNetMap(true, true), true},
{"enable hybrid peer", mkNetMap(true, true), mkNetMap(true, false), true},
{"classical to disabled hybrid", mkNetMap(false, false), mkNetMap(true, true), false},
{"disabled hybrid to classical", mkNetMap(true, true), mkNetMap(false, false), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nb.SetNetMap(tt.before)
got := nb.SetNetMap(tt.after)
if tt.wantChange {
if len(got.HandshakeTypeChanged) != 1 || !got.HandshakeTypeChanged.Contains(nk) {
t.Errorf("SetNetMap(%v) HandshakeTypeChanged = %v, want %v", tt.name, got.HandshakeTypeChanged, nk)
}
} else {
if len(got.HandshakeTypeChanged) != 0 {
t.Errorf("SetNetMap(%v) HandshakeTypeChanged = %v, want none", tt.name, got.HandshakeTypeChanged)
}
}
})
}
}
func TestNodeBackendHandshakeChangedDelta(t *testing.T) {
nb := newNodeBackend(t.Context(), tstest.WhileTestRunningLogger(t), eventbus.New())
nk := key.NewNode().Public()
dk := key.NewDisco().Public()
mkNode := func(hybrid bool, remoteDisabled bool) tailcfg.NodeView {
cap := 100
if hybrid {
cap = 152 // first capver to support hybrid handshakes
}
var disable tailcfg.NodeCapMap
if remoteDisabled {
disable = tailcfg.NodeCapMap{
nodecap.DisableHybridHandshakes: nil,
}
}
return (&tailcfg.Node{
ID: 1,
Key: nk,
DiscoKey: dk,
HomeDERP: 1,
Cap: tailcfg.CapabilityVersion(cap),
CapMap: disable,
}).View()
}
upsert := func(hybrid bool, remoteDisabled bool) netmap.NodeMutationUpsert {
return netmap.NodeMutationUpsert{Node: mkNode(hybrid, remoteDisabled)}
}
rm := netmap.MakeNodeMutationRemove(1)
mkNetmap := func(hybrid bool, remoteDisabled bool) *netmap.NetworkMap {
return &netmap.NetworkMap{Peers: []tailcfg.NodeView{mkNode(hybrid, remoteDisabled)}}
}
empty := &netmap.NetworkMap{}
tests := []struct {
name string
before *netmap.NetworkMap
after netmap.NodeMutation
wantChange bool
}{
{"add classical peer", empty, upsert(false, false), false},
{"add hybrid peer", empty, upsert(true, false), false},
{"delete classical peer", mkNetmap(false, false), rm, false},
{"delete hybrid peer", mkNetmap(true, false), rm, false},
{"classical to hybrid", mkNetmap(false, false), upsert(true, false), true},
{"hybrid to classical", mkNetmap(true, false), upsert(false, false), true},
{"disable classical", mkNetmap(false, false), upsert(false, true), false},
{"enable classical", mkNetmap(false, true), upsert(false, false), false},
{"disable hybrid", mkNetmap(true, false), upsert(true, true), true},
{"enable hybrid", mkNetmap(true, true), upsert(true, false), true},
{"classical to disabled hybrid", mkNetmap(false, false), upsert(true, true), false},
{"disabled hybrid to classical", mkNetmap(true, true), upsert(false, false), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nb.SetNetMap(tt.before)
got, handled := nb.UpdateNetmapDelta([]netmap.NodeMutation{tt.after})
if !handled {
t.Fatal("UpdateNetmapDelta not handled")
}
if tt.wantChange {
if len(got.HandshakeTypeChanged) != 1 || !got.HandshakeTypeChanged.Contains(nk) {
t.Errorf("UpdateNetmapDelta(%v) HandshakeTypeChanged = %v, want %v", tt.name, got.HandshakeTypeChanged, nk)
}
} else {
if len(got.HandshakeTypeChanged) != 0 {
t.Errorf("UpdateNetmapDelta(%v) HandshakeTypeChanged = %v, want none", tt.name, got.HandshakeTypeChanged)
}
}
})
}
}
func TestNodeBackendRouteManagerExtras(t *testing.T) {
nb := newNodeBackend(t.Context(), tstest.WhileTestRunningLogger(t), eventbus.New())
+1 -2
View File
@@ -47,8 +47,7 @@ func (b *LocalBackend) lookupPeerByIP(ip netip.Addr) (key.NodePublic, bool) {
// [wgengine.Engine.SetPeerConfigFunc] callback, backing wireguard-go's
// lazy peer creation and per-delta peer sync.
func (b *LocalBackend) peerConfig(k key.NodePublic) (_ wgcfg.PeerConfig, ok bool) {
ips, ok := b.currentNode().PeerAllowedIPs(k)
return wgcfg.PeerConfig{AllowedIPs: ips}, ok
return b.currentNode().PeerWGConfig(k)
}
// resolveMagicDNS resolves a MagicDNS hostname to the owning node's IP
+2 -1
View File
@@ -200,7 +200,8 @@ type CapabilityVersion int
// - 149: 2026-10-07: Sentinel version to request the server send back [NodeAttrCacheNetworkMaps] set to enabled
// - 150: 2026-10-13: Client handles app connectors with "mode": "magicip" as conn25.
// - 151: 2026-11-13: Client understands [nodecap.LogUploadAuth] for authenticated log uploads
const CurrentCapabilityVersion CapabilityVersion = 151
// - 152: 2026-11-17: Client supports hybrid post-quantum handshakes
const CurrentCapabilityVersion CapabilityVersion = 152
// ID is an integer ID for a user, node, or login allocated by the
// control plane.