From 5e622e4a4c857e0a5ad705439374e8aafbbd0d01 Mon Sep 17 00:00:00 2001 From: Nick Khyl Date: Thu, 3 Dec 2026 19:54:21 -0800 Subject: [PATCH] ipn/ipn{state,local},cmd/tailscale/cli: report the protocol of the last WireGuard session Add PeerStatus.LastProtocol, the protocol of the WireGuard session last established with a peer. Unlike Protocol, it is kept after the session ends. It is forgotten when the peer is removed or its node key changes, and it is not persisted across tailscaled restarts. Use it for the hybrid-pq label in tailscale status --verbose and on the web status page, so that the label no longer depends on the session still being up. Updates #22853 Updates tailscale/corp#50068 Updates tailscale/corp#52622 Signed-off-by: Nick Khyl --- cmd/tailscale/cli/status.go | 3 +- ipn/ipnlocal/local.go | 7 ++ ipn/ipnlocal/local_test.go | 137 +++++++++++++++++++++++++ ipn/ipnlocal/node_backend.go | 41 ++++++++ ipn/ipnstate/ipnstate.go | 13 ++- tstest/integration/integration_test.go | 3 + 6 files changed, 202 insertions(+), 2 deletions(-) diff --git a/cmd/tailscale/cli/status.go b/cmd/tailscale/cli/status.go index 139c5f21e..9ec4eca03 100644 --- a/cmd/tailscale/cli/status.go +++ b/cmd/tailscale/cli/status.go @@ -194,7 +194,8 @@ func runStatus(ctx context.Context, args []string) error { offline = "; offline" + lastSeenFmt(ps.LastSeen) } var protocol string - if label := protocolLabel(ps.Protocol); statusArgs.verbose && label != "" { + // Older tailscaled versions only report Protocol. + if label := protocolLabel(cmp.Or(ps.LastProtocol, ps.Protocol)); statusArgs.verbose && label != "" { protocol = "; " + label } if !ps.Active { diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 661786fc3..6eb4698ec 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -1688,6 +1688,7 @@ func (b *LocalBackend) populatePeerStatusLocked(sb *ipnstate.StatusBuilder) { } exitNodeID := b.pm.CurrentPrefs().ExitNodeID() blankHostinfo := new(tailcfg.Hostinfo).View() + lastProtocols := cn.PeerLastProtocols() for _, p := range cn.Peers() { tailscaleIPs := make([]netip.Addr, 0, p.Addresses().Len()) for i := range p.Addresses().Len() { @@ -1717,6 +1718,9 @@ func (b *LocalBackend) populatePeerStatusLocked(sb *ipnstate.StatusBuilder) { Capabilities: p.Capabilities().AsSlice(), Protocol: b.peerWGState[p.StableID()].Protocol, } + // The current session comes first, so that LastProtocol + // is never empty while Protocol is set. + ps.LastProtocol = cmp.Or(ps.Protocol, lastProtocols[p.Key()]) for _, f := range b.extHost.Hooks().SetPeerStatus { f(ps, p, cn) } @@ -6297,6 +6301,9 @@ func (b *LocalBackend) handlePeerWireGuardState(peerKey key.NodePublic, st ipn.P id = peer.StableID() } + if st == ipn.PeerWireGuardStateEstablished { + b.currentNode().SetPeerLastProtocol(peerKey, protocol) + } if st == ipn.PeerWireGuardStateNone { old, ok := b.peerWGState[id] if !ok { diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index b4b2edefc..005931334 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -2671,6 +2671,143 @@ func TestPeerWireGuardStateWatchInitialThenDeltas(t *testing.T) { } } +func TestPeerLastProtocol(t *testing.T) { + b := newTestLocalBackend(t) + peer := &tailcfg.Node{ + ID: 1, + StableID: "stable1", + Key: makeNodeKeyFromID(1), + Cap: 152, // first capver to support hybrid handshakes + } + self := (&tailcfg.Node{ID: 100}).View() + netMapWith := func(peers ...*tailcfg.Node) *netmap.NetworkMap { + nm := &netmap.NetworkMap{SelfNode: self} + for _, p := range peers { + nm.Peers = append(nm.Peers, p.View()) + } + return nm + } + b.currentNode().SetNetMap(netMapWith(peer)) + + check := func(t *testing.T, wantProtocol, wantLast string) { + t.Helper() + sb := &ipnstate.StatusBuilder{WantPeers: true} + b.UpdateStatus(sb) + peers := sb.Status().Peer + if len(peers) != 1 { + t.Fatalf("got %d peers in status, want 1", len(peers)) + } + var ps *ipnstate.PeerStatus + for _, ps = range peers { + } + if ps.Protocol != wantProtocol || ps.LastProtocol != wantLast { + t.Fatalf("Protocol, LastProtocol = %q, %q; want %q, %q", + ps.Protocol, ps.LastProtocol, wantProtocol, wantLast) + } + } + const ( + classical = ipnstate.ProtocolWireGuardV1 + hybrid = ipnstate.ProtocolWireGuardV1TSPQHybridV1 + ) + now := time.Now() + + check(t, "", "") + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateHandshake, "", now) + check(t, "", "") + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, classical, now) + check(t, classical, classical) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, hybrid, now) + check(t, hybrid, hybrid) + + // The last protocol is kept after the session ends. + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateExpired, "", now) + check(t, "", hybrid) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", now) + check(t, "", hybrid) + + // It is forgotten once the peer is removed or its node key changes. + rekeyed := peer.Clone() + rekeyed.Key = makeNodeKeyFromID(2) + nb := b.currentNode() + for _, tt := range []struct { + name string + to *tailcfg.Node // or nil to remove the peer + }{ + {"removed", nil}, + {"rekeyed", rekeyed}, + } { + for _, delta := range []bool{false, true} { + t.Run(fmt.Sprintf("%s/delta=%v", tt.name, delta), func(t *testing.T) { + nb.SetNetMap(netMapWith(peer)) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, hybrid, now) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", now) + check(t, "", hybrid) + + switch { + case !delta && tt.to == nil: + nb.SetNetMap(netMapWith()) + case !delta: + nb.SetNetMap(netMapWith(tt.to)) + case tt.to == nil: + nb.UpdateNetmapDelta([]netmap.NodeMutation{netmap.MakeNodeMutationRemove(peer.ID)}) + default: + nb.UpdateNetmapDelta([]netmap.NodeMutation{netmap.NodeMutationUpsert{Node: tt.to.View()}}) + } + nb.mu.Lock() + n := len(nb.lastProtocols) + nb.mu.Unlock() + if n != 0 { + t.Errorf("got %d recorded protocols, want none", n) + } + if tt.to == nil { + nb.SetNetMap(netMapWith(peer)) + } + check(t, "", "") + }) + } + } + + // It is kept when the peer's handshake type changes, + // until a session of the new type is established. + classicalOnly := peer.Clone() + classicalOnly.Cap = 151 + for _, delta := range []bool{false, true} { + nb.SetNetMap(netMapWith(peer)) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, hybrid, now) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", now) + if delta { + nb.UpdateNetmapDelta([]netmap.NodeMutation{netmap.NodeMutationUpsert{Node: classicalOnly.View()}}) + } else { + nb.SetNetMap(netMapWith(classicalOnly)) + } + check(t, "", hybrid) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, classical, now) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", now) + check(t, "", classical) + } + + // It is not empty while the previous key's session is still reported. + nb.SetNetMap(netMapWith(peer)) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, hybrid, now) + nb.SetNetMap(netMapWith(rekeyed)) + check(t, hybrid, hybrid) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", now) + check(t, "", "") + + // It is kept when another node takes over the key. + nb.SetNetMap(netMapWith(peer)) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, hybrid, now) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", now) + replacement := peer.Clone() + replacement.ID = 2 + replacement.StableID = "stable2" + nb.UpdateNetmapDelta([]netmap.NodeMutation{ + netmap.MakeNodeMutationRemove(peer.ID), + netmap.NodeMutationUpsert{Node: replacement.View()}, + }) + check(t, "", hybrid) +} + func TestPeerWireGuardStateMetrics(t *testing.T) { b := newTestLocalBackend(t) peer := &tailcfg.Node{ diff --git a/ipn/ipnlocal/node_backend.go b/ipn/ipnlocal/node_backend.go index 3d5b8aa99..d1c479bfa 100644 --- a/ipn/ipnlocal/node_backend.go +++ b/ipn/ipnlocal/node_backend.go @@ -139,6 +139,12 @@ type nodeBackend struct { // It is mutated in place (with mu held) and must not escape the [nodeBackend]. nodeByStableID map[tailcfg.StableNodeID]tailcfg.NodeID + // lastProtocols is the protocol of the WireGuard session + // last established with each peer, by node key. + // Unlike the session state, an entry is kept after the session ends, + // until no node has the key. + lastProtocols map[key.NodePublic]string + // nodeByName maps MagicDNS hostnames (lowercase, no trailing dot) to // node IDs. Both the FQDN and the short name (suffix stripped) are // keys. It is used by the tsdial MagicDNS resolution callback. @@ -331,6 +337,26 @@ func (nb *nodeBackend) nodeByIDLocked(id tailcfg.NodeID) (_ tailcfg.NodeView, ok return n, ok } +// SetPeerLastProtocol records the protocol of the WireGuard session +// last established with the peer that has the given node key. +// It does nothing if there is no such peer. +func (nb *nodeBackend) SetPeerLastProtocol(k key.NodePublic, protocol string) { + nb.mu.Lock() + defer nb.mu.Unlock() + if _, ok := nb.nodeByKey[k]; ok { + mak.Set(&nb.lastProtocols, k, protocol) + } +} + +// PeerLastProtocols returns the protocol of the WireGuard session +// last established with each peer, by node key. +// Peers without such a session have no entry. +func (nb *nodeBackend) PeerLastProtocols() map[key.NodePublic]string { + nb.mu.Lock() + defer nb.mu.Unlock() + return maps.Clone(nb.lastProtocols) +} + func (nb *nodeBackend) PeerByStableID(id tailcfg.StableNodeID) (_ tailcfg.NodeView, ok bool) { nb.mu.Lock() defer nb.mu.Unlock() @@ -943,6 +969,10 @@ func (nb *nodeBackend) updatePeersLocked() (res netmapResult) { } }) } + maps.DeleteFunc(nb.lastProtocols, func(k key.NodePublic, _ string) bool { + _, ok := nb.nodeByKey[k] + return !ok + }) for _, p := range nb.peers { if prev, ok := prevDisco[p.Key()]; ok && nb.discoChanged(p.Key(), prev, p.DiscoKey()) { @@ -1165,6 +1195,9 @@ func (nb *nodeBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (res netmap res.ChangedAllowedIPs = rt.Commit().AllowedIPs }() + // Keys that peers had before they were rekeyed or removed. + var staleKeys []key.NodePublic + for _, m := range muts { switch m := m.(type) { case netmap.NodeMutationUpsert: @@ -1175,6 +1208,8 @@ func (nb *nodeBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (res netmap res.DiscoChanged.Make() res.DiscoChanged.Add(m.Node.Key()) } + } else { + staleKeys = append(staleKeys, old.Key()) } if peerUsesHybridHandshakes(old) != peerUsesHybridHandshakes(m.Node) { res.HandshakeTypeChanged.Make() @@ -1224,6 +1259,7 @@ func (nb *nodeBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (res netmap deleteIfOwned(nb.nodeByStableID, old.StableID(), nid) nb.removeNodeNameLocked(old.Name(), nid) delete(nb.peers, nid) + staleKeys = append(staleKeys, old.Key()) rt.RemovePeer(nid) res.RemovedPeers = append(res.RemovedPeers, old.StableID()) } @@ -1249,6 +1285,11 @@ func (nb *nodeBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (res netmap for nid, n := range mutableNodes { nb.peers[nid] = n.View() } + for _, k := range staleKeys { + if _, ok := nb.nodeByKey[k]; !ok { + delete(nb.lastProtocols, k) + } + } nb.signalKeyWaitersForTestLocked() return res, true } diff --git a/ipn/ipnstate/ipnstate.go b/ipn/ipnstate/ipnstate.go index 62f0b65d5..8c43c60a3 100644 --- a/ipn/ipnstate/ipnstate.go +++ b/ipn/ipnstate/ipnstate.go @@ -7,6 +7,7 @@ package ipnstate import ( + "cmp" "fmt" "html" "io" @@ -295,6 +296,13 @@ type PeerStatus struct { // It is empty if there is no session. Protocol string `json:",omitzero"` + // LastProtocol is the protocol of the WireGuard session + // last established with this peer, such as [ProtocolWireGuardV1TSPQHybridV1]. + // Unlike Protocol, it is kept after the session ends. + // It is empty if no session has been established with this peer + // since tailscaled started, or if the peer's node key has changed since. + LastProtocol string `json:",omitzero"` + // Active is whether the node was recently active. The // definition is somewhat undefined but has historically and // currently means that there was some packet sent to this @@ -552,6 +560,9 @@ func (sb *StatusBuilder) AddPeer(peer key.NodePublic, st *PeerStatus) { if v := st.Protocol; v != "" { e.Protocol = v } + if v := st.LastProtocol; v != "" { + e.LastProtocol = v + } if v := st.Created; !v.IsZero() { e.Created = v } @@ -705,7 +716,7 @@ table tbody tr:nth-child(even) td { background-color: #f5f5f5; } ) f("") - if ps.Protocol == ProtocolWireGuardV1TSPQHybridV1 { + if cmp.Or(ps.LastProtocol, ps.Protocol) == ProtocolWireGuardV1TSPQHybridV1 { f("hybrid-pq
") } if ps.Active { diff --git a/tstest/integration/integration_test.go b/tstest/integration/integration_test.go index d65a3db4b..66e3ba647 100644 --- a/tstest/integration/integration_test.go +++ b/tstest/integration/integration_test.go @@ -1334,6 +1334,9 @@ func TestPeerProtocolStatus(t *testing.T) { if ps.Protocol != tt.wantProtocol { t.Errorf("node %d: Protocol = %q, want %q", i, ps.Protocol, tt.wantProtocol) } + if ps.LastProtocol != tt.wantProtocol { + t.Errorf("node %d: LastProtocol = %q, want %q", i, ps.LastProtocol, tt.wantProtocol) + } for _, verbose := range []bool{false, true} { out, err := n.TailscaleForOutput("status", fmt.Sprintf("--verbose=%v", verbose)).CombinedOutput()