diff --git a/cmd/tailscale/cli/status.go b/cmd/tailscale/cli/status.go index a058a2d3c..139c5f21e 100644 --- a/cmd/tailscale/cli/status.go +++ b/cmd/tailscale/cli/status.go @@ -56,6 +56,7 @@ https://github.com/tailscale/tailscale/blob/main/ipn/ipnstate/ipnstate.go fs.StringVar(&statusArgs.listen, "listen", "127.0.0.1:8384", "listen address for web mode; use port 0 for automatic") fs.BoolVar(&statusArgs.browser, "browser", true, "open a browser in web mode") fs.BoolVar(&statusArgs.header, "header", false, "show column headers in table format") + fs.BoolVar(&statusArgs.verbose, "verbose", false, "show additional details, such as hybrid post-quantum sessions") return fs })(), } @@ -69,10 +70,28 @@ var statusArgs struct { self bool // in CLI mode, show status of local machine peers bool // in CLI mode, show status of peer machines header bool // in CLI mode, show column headers in table format + verbose bool // in CLI mode, show additional details } const mullvadTCD = "mullvad.ts.net." +// protocolLabels maps the protocol of a peer's WireGuard session +// to the label shown for it in the status output. +// A protocol with an empty label is not shown. +var protocolLabels = map[string]string{ + ipnstate.ProtocolWireGuardV1: "", + ipnstate.ProtocolWireGuardV1TSPQHybridV1: "hybrid-pq", +} + +// protocolLabel returns the label for the given protocol, +// or the protocol itself if it is not known to this client. +func protocolLabel(protocol string) string { + if label, ok := protocolLabels[protocol]; ok { + return label + } + return protocol +} + func runStatus(ctx context.Context, args []string) error { if len(args) > 0 { return errors.New("unexpected non-flag arguments to 'tailscale status'") @@ -174,20 +193,24 @@ func runStatus(ctx context.Context, args []string) error { if !ps.Online { offline = "; offline" + lastSeenFmt(ps.LastSeen) } + var protocol string + if label := protocolLabel(ps.Protocol); statusArgs.verbose && label != "" { + protocol = "; " + label + } if !ps.Active { if ps.ExitNode { - f("idle; exit node%s", offline) + f("idle%s; exit node%s", protocol, offline) } else if ps.ExitNodeOption { - f("idle; offers exit node%s", offline) + f("idle%s; offers exit node%s", protocol, offline) } else if anyTraffic { - f("idle%s", offline) + f("idle%s%s", protocol, offline) } else if !ps.Online { f("offline%s", lastSeenFmt(ps.LastSeen)) } else { f("-") } } else { - f("active; ") + f("active%s; ", protocol) if ps.ExitNode { f("exit node; ") } else if ps.ExitNodeOption { diff --git a/cmd/tailscale/cli/status_test.go b/cmd/tailscale/cli/status_test.go new file mode 100644 index 000000000..0569f0c79 --- /dev/null +++ b/cmd/tailscale/cli/status_test.go @@ -0,0 +1,27 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package cli + +import ( + "testing" + + "tailscale.com/ipn/ipnstate" +) + +func TestProtocolLabel(t *testing.T) { + tests := []struct { + protocol string + want string + }{ + {"", ""}, + {ipnstate.ProtocolWireGuardV1, ""}, + {ipnstate.ProtocolWireGuardV1TSPQHybridV1, "hybrid-pq"}, + {"wg-v2", "wg-v2"}, + } + for _, tt := range tests { + if got := protocolLabel(tt.protocol); got != tt.want { + t.Errorf("protocolLabel(%q) = %q, want %q", tt.protocol, got, tt.want) + } + } +} diff --git a/flake.nix b/flake.nix index deced0a4a..c01fde093 100644 --- a/flake.nix +++ b/flake.nix @@ -172,4 +172,4 @@ }); }; } -# nix-direnv cache busting line: sha256-GXIh1xGfH5ixVXUkazObeUCEJq20QoUR4j4LFVPrNrs= +# nix-direnv cache busting line: sha256-m8uqJXw76Y9xB/ruEG922edatOOXkYiwUpoELg5tiOc= diff --git a/flakehashes.json b/flakehashes.json index b66939e5a..eef282584 100644 --- a/flakehashes.json +++ b/flakehashes.json @@ -4,7 +4,7 @@ "sri": "sha256-Qmy06FkeFkjqCo1FLSD9dO7hTvdo/gC2VACJniuiQBU=" }, "vendor": { - "goModSum": "sha256-UPSikeu/TsMzCvDYCF0p4uxBlPHFhTpzQCVB7WANYkk=", - "sri": "sha256-GXIh1xGfH5ixVXUkazObeUCEJq20QoUR4j4LFVPrNrs=" + "goModSum": "sha256-lW4jFMZ8IE6bG94SYEl9Otc1iyHdJvMRVyaLYY66K4Y=", + "sri": "sha256-m8uqJXw76Y9xB/ruEG922edatOOXkYiwUpoELg5tiOc=" } } diff --git a/go.mod b/go.mod index 15700cb26..f7b74550f 100644 --- a/go.mod +++ b/go.mod @@ -126,7 +126,7 @@ require ( github.com/tailscale/ts-gokrazy v0.0.0-20260630224145-b83088f2e52e github.com/tailscale/web-client-prebuilt v0.0.0-20260917222731-e0ed2d0d0fea github.com/tailscale/wf v0.0.0-20240214030419-6fbb0a674ee6 - github.com/tailscale/wireguard-go v0.0.0-20261125191149-d74d4ff62061 + github.com/tailscale/wireguard-go v0.0.0-20261204003109-5d2f71d36889 github.com/tailscale/xnet v0.0.0-20240729143630-8497ac4dab2e github.com/tc-hib/winres v0.3.1 github.com/tcnksm/go-httpstat v0.2.0 diff --git a/go.sum b/go.sum index b8b5dde3b..e1928d6f5 100644 --- a/go.sum +++ b/go.sum @@ -840,8 +840,8 @@ github.com/tailscale/web-client-prebuilt v0.0.0-20260917222731-e0ed2d0d0fea h1:a github.com/tailscale/web-client-prebuilt v0.0.0-20260917222731-e0ed2d0d0fea/go.mod h1:agQPE6y6ldqCOui2gkIh7ZMztTkIQKH049tv8siLuNQ= github.com/tailscale/wf v0.0.0-20240214030419-6fbb0a674ee6 h1:l10Gi6w9jxvinoiq15g8OToDdASBni4CyJOdHY1Hr8M= github.com/tailscale/wf v0.0.0-20240214030419-6fbb0a674ee6/go.mod h1:ZXRML051h7o4OcI0d3AaILDIad/Xw0IkXaHM17dic1Y= -github.com/tailscale/wireguard-go v0.0.0-20261125191149-d74d4ff62061 h1:wwKhjUjxTN7Xp9tEJb3I/O0CspKEPkDXy9Aje08A/gY= -github.com/tailscale/wireguard-go v0.0.0-20261125191149-d74d4ff62061/go.mod h1:Hl7pyu83xNjc1LY5e9apq2mWm8Hl76wRtJbwZ2XISqY= +github.com/tailscale/wireguard-go v0.0.0-20261204003109-5d2f71d36889 h1:+09PwX5hiw7SSqiZfQ186I4e/UGLeml0ocuyd1F84xQ= +github.com/tailscale/wireguard-go v0.0.0-20261204003109-5d2f71d36889/go.mod h1:Hl7pyu83xNjc1LY5e9apq2mWm8Hl76wRtJbwZ2XISqY= github.com/tailscale/xnet v0.0.0-20240729143630-8497ac4dab2e h1:zOGKqN5D5hHhiYUp091JqK7DPCqSARyUfduhGUY8Bek= github.com/tailscale/xnet v0.0.0-20240729143630-8497ac4dab2e/go.mod h1:orPd6JZXXRyuDusYilywte7k094d7dycXXU5YnWsrwg= github.com/tc-hib/winres v0.3.1 h1:CwRjEGrKdbi5CvZ4ID+iyVhgyfatxFoizjPhzez9Io4= diff --git a/ipn/backend.go b/ipn/backend.go index ff6bd30d7..06a891750 100644 --- a/ipn/backend.go +++ b/ipn/backend.go @@ -592,6 +592,11 @@ type PeerState struct { // so a later subscriber's initial snapshot reflects the true transition // time rather than the subscription time. PeerWireGuardStateAt time.Time + + // Protocol is the protocol of the peer's WireGuard session, + // such as [ipnstate.ProtocolWireGuardV1TSPQHybridV1]. + // It is only set in [PeerWireGuardStateEstablished]. + Protocol string `json:",omitzero"` } // IsZero reports whether no field of n is set. diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 7da78e7c1..661786fc3 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -32,6 +32,7 @@ import ( "time" "github.com/gaissmai/bart" + "github.com/tailscale/wireguard-go/device" "go4.org/mem" "go4.org/netipx" "golang.org/x/net/dns/dnsmessage" @@ -402,7 +403,7 @@ type LocalBackend struct { // peerWGState is the current non-zero WireGuard session state per peer, // keyed by stable node ID for delivery on the IPN bus. // Entries are added/updated by [LocalBackend.handlePeerWireGuardState] - // and removed when wireguard-go reports [wgengine.PeerWireGuardStateNone] + // and removed when wireguard-go reports [device.PeerSessionNone] // (including the synthetic None fired by wireguard-go's RemovePeer path // when a peer is removed from the WG config). peerWGState map[tailcfg.StableNodeID]ipn.PeerState @@ -1714,6 +1715,7 @@ func (b *LocalBackend) populatePeerStatusLocked(sb *ipnstate.StatusBuilder) { SSH_HostKeys: hostinfo.SSH_HostKeys().AsSlice(), Location: hostinfo.Location().AsStruct(), Capabilities: p.Capabilities().AsSlice(), + Protocol: b.peerWGState[p.StableID()].Protocol, } for _, f := range b.extHost.Hooks().SetPeerStatus { f(ps, p, cn) @@ -6259,16 +6261,23 @@ func (b *LocalBackend) NetMapWithPeers() *netmap.NetworkMap { // not call back into wireguard-go. Acquiring b.mu here can deadlock with // LocalBackend operations that hold b.mu while waiting for wireguard-go to make // progress. -func (b *LocalBackend) onPeerWireGuardState(peerKey key.NodePublic, state wgengine.PeerWireGuardState) { - st := peerWireGuardStateFromEngine(state) +func (b *LocalBackend) onPeerWireGuardState(peerKey key.NodePublic, si device.PeerSessionInfo) { + st := peerWireGuardStateFromDevice(si.State) + var protocol string + if si.State == device.PeerSessionEstablished { + protocol = ipnstate.ProtocolWireGuardV1 + if si.Hybrid { + protocol = ipnstate.ProtocolWireGuardV1TSPQHybridV1 + } + } // 10ms granularity is plenty for diagnostics and keeps the JSON form short. at := b.clock.Now().Round(10 * time.Millisecond) b.peerWGStateQueue.Add(func() { - b.handlePeerWireGuardState(peerKey, st, at) + b.handlePeerWireGuardState(peerKey, st, protocol, at) }) } -func (b *LocalBackend) handlePeerWireGuardState(peerKey key.NodePublic, st ipn.PeerWireGuardState, at time.Time) { +func (b *LocalBackend) handlePeerWireGuardState(peerKey key.NodePublic, st ipn.PeerWireGuardState, protocol string, at time.Time) { b.mu.Lock() defer b.mu.Unlock() @@ -6297,24 +6306,31 @@ func (b *LocalBackend) handlePeerWireGuardState(peerKey key.NodePublic, st ipn.P delete(b.peerWGStableIDByKey, peerKey) addPeerWGStateMetric(old.PeerWireGuardState, -1) } else { - old := b.peerWGState[id].PeerWireGuardState - if old == st { - return - } - if old != ipn.PeerWireGuardStateNone { - addPeerWGStateMetric(old, -1) + old := b.peerWGState[id] + if old.PeerWireGuardState == st { + if old.Protocol == protocol { + return + } + // The peer rekeyed and stays in the same state. + at = old.PeerWireGuardStateAt + } else { + if old.PeerWireGuardState != ipn.PeerWireGuardStateNone { + addPeerWGStateMetric(old.PeerWireGuardState, -1) + } + addPeerWGStateMetric(st, 1) } mak.Set(&b.peerWGState, id, ipn.PeerState{ PeerWireGuardState: st, PeerWireGuardStateAt: at, + Protocol: protocol, }) mak.Set(&b.peerWGStableIDByKey, peerKey, id) - addPeerWGStateMetric(st, 1) } b.sendToLocked(ipn.Notify{ PeerState: map[tailcfg.StableNodeID]ipn.PeerState{id: { PeerWireGuardState: st, PeerWireGuardStateAt: at, + Protocol: protocol, }}, }, allClients) } @@ -6330,18 +6346,18 @@ func addPeerWGStateMetric(state ipn.PeerWireGuardState, delta int64) { } } -func peerWireGuardStateFromEngine(state wgengine.PeerWireGuardState) ipn.PeerWireGuardState { +func peerWireGuardStateFromDevice(state device.PeerSessionState) ipn.PeerWireGuardState { switch state { - case wgengine.PeerWireGuardStateNone: + case device.PeerSessionNone: return ipn.PeerWireGuardStateNone - case wgengine.PeerWireGuardStateHandshake: + case device.PeerSessionHandshake: return ipn.PeerWireGuardStateHandshake - case wgengine.PeerWireGuardStateEstablished: + case device.PeerSessionEstablished: return ipn.PeerWireGuardStateEstablished - case wgengine.PeerWireGuardStateExpired: + case device.PeerSessionExpired: return ipn.PeerWireGuardStateExpired default: - panic(fmt.Sprintf("unexpected wgengine.PeerWireGuardState %d", state)) + panic(fmt.Sprintf("unexpected wireguard-go PeerSessionState %d", state)) } } diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index 3879f40fa..b4b2edefc 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -27,6 +27,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "github.com/tailscale/wireguard-go/device" memro "go4.org/mem" "go4.org/netipx" "golang.org/x/net/dns/dnsmessage" @@ -45,6 +46,7 @@ import ( "tailscale.com/ipn/conffile" "tailscale.com/ipn/ipnauth" "tailscale.com/ipn/ipnlocal/netmapcache" + "tailscale.com/ipn/ipnstate" "tailscale.com/ipn/store/mem" "tailscale.com/net/netcheck" "tailscale.com/net/netmon" @@ -2565,36 +2567,36 @@ func TestNotifyForSessionPeerWireGuardStateVisibility(t *testing.T) { } } -func TestPeerWireGuardStateValuesMatchWGEngine(t *testing.T) { - const unknownPeerWireGuardState wgengine.PeerWireGuardState = 255 +func TestPeerWireGuardStateValuesMatchWireguardGo(t *testing.T) { + const unknownPeerSessionState device.PeerSessionState = 255 tests := []struct { name string - in wgengine.PeerWireGuardState + in device.PeerSessionState want ipn.PeerWireGuardState }{ - {"none", wgengine.PeerWireGuardStateNone, ipn.PeerWireGuardStateNone}, - {"handshake", wgengine.PeerWireGuardStateHandshake, ipn.PeerWireGuardStateHandshake}, - {"established", wgengine.PeerWireGuardStateEstablished, ipn.PeerWireGuardStateEstablished}, - {"expired", wgengine.PeerWireGuardStateExpired, ipn.PeerWireGuardStateExpired}, + {"none", device.PeerSessionNone, ipn.PeerWireGuardStateNone}, + {"handshake", device.PeerSessionHandshake, ipn.PeerWireGuardStateHandshake}, + {"established", device.PeerSessionEstablished, ipn.PeerWireGuardStateEstablished}, + {"expired", device.PeerSessionExpired, ipn.PeerWireGuardStateExpired}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := peerWireGuardStateFromEngine(tt.in); got != tt.want { + if got := peerWireGuardStateFromDevice(tt.in); got != tt.want { t.Fatalf("converted state = %v; want %v", got, tt.want) } if got, want := uint8(tt.in), uint8(tt.want); got != want { - t.Fatalf("wgengine const = %v; want %v", got, want) + t.Fatalf("wireguard-go const = %v; want %v", got, want) } }) } defer func() { if recover() == nil { - t.Fatal("expected panic for unknown wgengine state") + t.Fatal("expected panic for unknown wireguard-go state") } }() - _ = peerWireGuardStateFromEngine(unknownPeerWireGuardState) + _ = peerWireGuardStateFromDevice(unknownPeerSessionState) } func TestPeerWireGuardStateWatchInitialThenDeltas(t *testing.T) { @@ -2615,10 +2617,10 @@ func TestPeerWireGuardStateWatchInitialThenDeltas(t *testing.T) { }) t1 := time.Unix(1700000000, 0) - b.handlePeerWireGuardState(peer1.Key, ipn.PeerWireGuardStateEstablished, t1) - b.handlePeerWireGuardState(peer1.Key, ipn.PeerWireGuardStateEstablished, t1.Add(time.Second)) - defer b.handlePeerWireGuardState(peer1.Key, ipn.PeerWireGuardStateNone, time.Now()) - defer b.handlePeerWireGuardState(peer2.Key, ipn.PeerWireGuardStateNone, time.Now()) + b.handlePeerWireGuardState(peer1.Key, ipn.PeerWireGuardStateEstablished, "", t1) + b.handlePeerWireGuardState(peer1.Key, ipn.PeerWireGuardStateEstablished, "", t1.Add(time.Second)) + defer b.handlePeerWireGuardState(peer1.Key, ipn.PeerWireGuardStateNone, "", time.Now()) + defer b.handlePeerWireGuardState(peer2.Key, ipn.PeerWireGuardStateNone, "", time.Now()) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -2629,7 +2631,7 @@ func TestPeerWireGuardStateWatchInitialThenDeltas(t *testing.T) { defer close(doneCh) var got int b.WatchNotificationsAs(ctx, nil, ipn.NotifyPeerWireGuardState, func() { - b.onPeerWireGuardState(peer2.Key, wgengine.PeerWireGuardStateHandshake) + b.onPeerWireGuardState(peer2.Key, device.PeerSessionInfo{State: device.PeerSessionHandshake}) }, func(n *ipn.Notify) bool { gotCh <- n got++ @@ -2677,25 +2679,49 @@ func TestPeerWireGuardStateMetrics(t *testing.T) { Key: makeNodeKeyFromID(1), } b.currentNode().SetNetMap(&netmap.NetworkMap{ - Peers: []tailcfg.NodeView{peer.View()}, + SelfNode: (&tailcfg.Node{ID: 100}).View(), + Peers: []tailcfg.NodeView{peer.View()}, }) now := time.Now() - defer b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, now) + defer b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", now) baseHandshake := metricPeerWGStateHandshake.Value() baseEstablished := metricPeerWGStateEstablished.Value() baseExpired := metricPeerWGStateExpired.Value() - b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, now) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, "", now) if got, want := metricPeerWGStateEstablished.Value(), baseEstablished+1; got != want { t.Fatalf("established gauge = %v; want %v", got, want) } - b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, now) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, "", now) if got, want := metricPeerWGStateEstablished.Value(), baseEstablished+1; got != want { t.Fatalf("established gauge after no-op = %v; want %v", got, want) } - b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateHandshake, now) + // A rekey with a different handshake type keeps the state and its time. + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateEstablished, ipnstate.ProtocolWireGuardV1TSPQHybridV1, now.Add(time.Second)) + if got, want := metricPeerWGStateEstablished.Value(), baseEstablished+1; got != want { + t.Fatalf("established gauge after rekey = %v; want %v", got, want) + } + b.mu.Lock() + gotState := b.peerWGState[peer.StableID] + b.mu.Unlock() + wantState := ipn.PeerState{ + PeerWireGuardState: ipn.PeerWireGuardStateEstablished, + PeerWireGuardStateAt: now, + Protocol: ipnstate.ProtocolWireGuardV1TSPQHybridV1, + } + if gotState != wantState { + t.Fatalf("peer state after rekey = %+v; want %+v", gotState, wantState) + } + sb := &ipnstate.StatusBuilder{WantPeers: true} + b.UpdateStatus(sb) + ps := sb.Status().Peer[peer.Key] + if got, want := ps.Protocol, ipnstate.ProtocolWireGuardV1TSPQHybridV1; got != want { + t.Fatalf("PeerStatus.Protocol = %q; want %q", got, want) + } + + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateHandshake, "", now) if got, want := metricPeerWGStateEstablished.Value(), baseEstablished; got != want { t.Fatalf("established gauge after transition = %v; want %v", got, want) } @@ -2703,7 +2729,7 @@ func TestPeerWireGuardStateMetrics(t *testing.T) { t.Fatalf("handshake gauge = %v; want %v", got, want) } - b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateExpired, now) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateExpired, "", now) if got, want := metricPeerWGStateHandshake.Value(), baseHandshake; got != want { t.Fatalf("handshake gauge after transition = %v; want %v", got, want) } @@ -2711,7 +2737,7 @@ func TestPeerWireGuardStateMetrics(t *testing.T) { t.Fatalf("expired gauge = %v; want %v", got, want) } - b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, now) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", now) if got, want := metricPeerWGStateExpired.Value(), baseExpired; got != want { t.Fatalf("expired gauge after none = %v; want %v", got, want) } @@ -2731,7 +2757,7 @@ func TestPeerWireGuardStateCallbackDoesNotBlockOnLocalBackendMu(t *testing.T) { b.mu.Lock() done := make(chan struct{}) go func() { - b.onPeerWireGuardState(peer.Key, wgengine.PeerWireGuardStateEstablished) + b.onPeerWireGuardState(peer.Key, device.PeerSessionInfo{State: device.PeerSessionEstablished}) close(done) }() select { @@ -2743,7 +2769,7 @@ func TestPeerWireGuardStateCallbackDoesNotBlockOnLocalBackendMu(t *testing.T) { b.mu.Unlock() _ = b.peerWGStateQueue.Wait(context.Background()) - b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, time.Now()) + b.handlePeerWireGuardState(peer.Key, ipn.PeerWireGuardStateNone, "", time.Now()) } func TestSetControlClientStatusSendsFullNetmapAsPeerChanges(t *testing.T) { diff --git a/ipn/ipnlocal/state_test.go b/ipn/ipnlocal/state_test.go index e04ce0879..56025ba2d 100644 --- a/ipn/ipnlocal/state_test.go +++ b/ipn/ipnlocal/state_test.go @@ -21,6 +21,7 @@ import ( "github.com/gaissmai/bart" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "github.com/tailscale/wireguard-go/device" "go4.org/netipx" "tailscale.com/control/controlclient" @@ -2353,7 +2354,7 @@ func (e *mockEngine) syncedPeersSince(n int) []key.NodePublic { } func (e *mockEngine) MarkDevicePeerForHandshake(key.NodePublic) {} -func (e *mockEngine) SetPeerSessionStateFunc(func(key.NodePublic, wgengine.PeerWireGuardState)) { +func (e *mockEngine) SetPeerSessionStateFunc(func(key.NodePublic, device.PeerSessionInfo)) { } func (e *mockEngine) SetNetLogSource(wgengine.NetLogSource) {} func (e *mockEngine) SetPeerPriorityMessageOnEstablishmentFunc(fn func(key.NodePublic) (msg []byte)) { diff --git a/ipn/ipnstate/ipnstate.go b/ipn/ipnstate/ipnstate.go index ab007bd5a..62f0b65d5 100644 --- a/ipn/ipnstate/ipnstate.go +++ b/ipn/ipnstate/ipnstate.go @@ -229,6 +229,15 @@ type PeerStatusLite struct { LastHandshake time.Time } +// The WireGuard session protocols reported in [PeerStatus.Protocol]. +const ( + // ProtocolWireGuardV1 is WireGuard with the Noise IKpsk2 handshake. + ProtocolWireGuardV1 = "wg-v1" + // ProtocolWireGuardV1TSPQHybridV1 is WireGuard with the hybrid + // post-quantum Noise IKhfs+psk2 handshake using ML-KEM-768. + ProtocolWireGuardV1TSPQHybridV1 = "wg-v1-ts-pqh-v1" +) + // PeerStatus describes a peer node and its current state. // WARNING: The fields in PeerStatus are merged by the AddPeer method in the StatusBuilder. // When adding a new field to PeerStatus, you must update AddPeer to handle merging @@ -281,6 +290,11 @@ type PeerStatus struct { ExitNode bool // true if this is the currently selected exit node. ExitNodeOption bool // true if this node can be an exit node (offered && approved) + // Protocol is the protocol of the current WireGuard session with this peer, + // such as [ProtocolWireGuardV1TSPQHybridV1]. + // It is empty if there is no session. + Protocol 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 @@ -535,6 +549,9 @@ func (sb *StatusBuilder) AddPeer(peer key.NodePublic, st *PeerStatus) { if v := st.LastHandshake; !v.IsZero() { e.LastHandshake = v } + if v := st.Protocol; v != "" { + e.Protocol = v + } if v := st.Created; !v.IsZero() { e.Created = v } @@ -688,6 +705,9 @@ table tbody tr:nth-child(even) td { background-color: #f5f5f5; } ) f("") + if ps.Protocol == ProtocolWireGuardV1TSPQHybridV1 { + f("hybrid-pq
") + } if ps.Active { if ps.Relay != "" && ps.CurAddr == "" { f("relay %s", html.EscapeString(ps.Relay)) diff --git a/shell.nix b/shell.nix index 3a3aa9403..35d54e5ac 100644 --- a/shell.nix +++ b/shell.nix @@ -16,4 +16,4 @@ ) { src = ./.; }).shellNix -# nix-direnv cache busting line: sha256-GXIh1xGfH5ixVXUkazObeUCEJq20QoUR4j4LFVPrNrs= +# nix-direnv cache busting line: sha256-m8uqJXw76Y9xB/ruEG922edatOOXkYiwUpoELg5tiOc= diff --git a/tstest/integration/integration.go b/tstest/integration/integration.go index 07849d6f9..db6ba2fa1 100644 --- a/tstest/integration/integration.go +++ b/tstest/integration/integration.go @@ -1413,6 +1413,33 @@ func (n *TestNode) MustStatus() *ipnstate.Status { return st } +// AwaitPeerWireGuardEstablished waits for tailscaled to report an established +// WireGuard session with the given peer on the IPN bus, and returns its state. +// +// tailscaled applies session state changes asynchronously, +// so a status read right after traffic flows may not reflect them yet. +// Call this first when a test depends on the session state. +func (n *TestNode) AwaitPeerWireGuardEstablished(peer tailcfg.StableNodeID) ipn.PeerState { + tb := n.env.t + tb.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + w, err := n.LocalClient().WatchIPNBus(ctx, ipn.NotifyPeerWireGuardState) + if err != nil { + tb.Fatalf("WatchIPNBus: %v", err) + } + defer w.Close() + for { + notify, err := w.Next() + if err != nil { + tb.Fatalf("waiting for an established WireGuard session with %v: %v", peer, err) + } + if st, ok := notify.PeerState[peer]; ok && st.PeerWireGuardState == ipn.PeerWireGuardStateEstablished { + return st + } + } +} + // PublicKey returns the hex-encoded public key of this node, // e.g. `nodekey:123456abc` func (n *TestNode) PublicKey() string { diff --git a/tstest/integration/integration_test.go b/tstest/integration/integration_test.go index 00517b864..d65a3db4b 100644 --- a/tstest/integration/integration_test.go +++ b/tstest/integration/integration_test.go @@ -41,6 +41,7 @@ import ( "tailscale.com/health" "tailscale.com/hostinfo" "tailscale.com/ipn" + "tailscale.com/ipn/ipnstate" "tailscale.com/net/tsaddr" "tailscale.com/net/tstun" "tailscale.com/net/udprelay/status" @@ -1274,6 +1275,81 @@ func TestNodeAddressIPFields(t *testing.T) { d1.MustCleanShutdown(t) } +func TestPeerProtocolStatus(t *testing.T) { + tstest.Parallel(t) + tests := []struct { + name string + nodeCap tailcfg.NodeCapMap + wantProtocol string + }{ + {name: "hybrid", wantProtocol: ipnstate.ProtocolWireGuardV1TSPQHybridV1}, + { + name: "disabled", + nodeCap: tailcfg.NodeCapMap{nodecap.DisableHybridHandshakes: nil}, + wantProtocol: ipnstate.ProtocolWireGuardV1, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tstest.Parallel(t) + env := NewTestEnv(t, ConfigureControl(func(control *testcontrol.Server) { + if tt.nodeCap != nil { + control.DefaultNodeCapabilities = &tt.nodeCap + } + })) + nodes := []*TestNode{NewTestNode(t, env), NewTestNode(t, env)} + for _, n := range nodes { + n.StartDaemon() + n.AwaitListening() + n.MustUp() + n.AwaitRunning() + } + + // ensure that a WireGuard session is established + n1, n2 := nodes[0], nodes[1] + n2IP := n2.AwaitIP4().String() + if err := tstest.WaitFor(20*time.Second, func() error { + return n1.Tailscale("ping", "--tsmp", "-c", "1", "--timeout=5s", n2IP).Run() + }); err != nil { + t.Fatalf("ping: %v", err) + } + + peerIDs := []tailcfg.StableNodeID{n2.MustStatus().Self.ID, n1.MustStatus().Self.ID} + for i, n := range nodes { + // Wait for the session on the IPN bus first: tailscaled updates + // the peer's state asynchronously, and status reads the same state. + if st := n.AwaitPeerWireGuardEstablished(peerIDs[i]); st.Protocol != tt.wantProtocol { + t.Errorf("node %d: IPN bus Protocol = %q, want %q", i, st.Protocol, tt.wantProtocol) + } + + var ps *ipnstate.PeerStatus + for _, p := range n.MustStatus().Peer { + if p.ID == peerIDs[i] { + ps = p + } + } + if ps == nil { + t.Fatalf("node %d: peer not in status", i) + } + if ps.Protocol != tt.wantProtocol { + t.Errorf("node %d: Protocol = %q, want %q", i, ps.Protocol, tt.wantProtocol) + } + + for _, verbose := range []bool{false, true} { + out, err := n.TailscaleForOutput("status", fmt.Sprintf("--verbose=%v", verbose)).CombinedOutput() + if err != nil { + t.Fatalf("node %d: status: %v, %s", i, err, out) + } + want := verbose && tt.wantProtocol == ipnstate.ProtocolWireGuardV1TSPQHybridV1 + if got := strings.Contains(string(out), "hybrid-pq"); got != want { + t.Errorf("node %d: status --verbose=%v shows hybrid-pq = %v, want %v:\n%s", i, verbose, got, want, out) + } + } + } + }) + } +} + func TestAddPingRequest(t *testing.T) { tstest.Parallel(t) env := NewTestEnv(t) diff --git a/wgengine/userspace.go b/wgengine/userspace.go index e5d430aa4..aa3f751bf 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -768,10 +768,10 @@ func (e *userspaceEngine) SetPeerByIPPacketFunc(fn func(netip.Addr) (_ key.NodeP }) } -func (e *userspaceEngine) SetPeerSessionStateFunc(fn func(key.NodePublic, PeerWireGuardState)) { - e.wgdev.SetSessionStateFunc(func(pk device.NoisePublicKey, state device.PeerSessionState) { +func (e *userspaceEngine) SetPeerSessionStateFunc(fn func(key.NodePublic, device.PeerSessionInfo)) { + e.wgdev.SetSessionStateFunc(func(pk device.NoisePublicKey, si device.PeerSessionInfo) { if fn != nil { - fn(key.NodePublicFromRaw32(mem.B(pk[:])), peerWireGuardStateFromDevice(state)) + fn(key.NodePublicFromRaw32(mem.B(pk[:])), si) } }) } @@ -807,21 +807,6 @@ func (e *userspaceEngine) SetWGPeerLookup(fn func(wgString string) (tsString str e.wgPeerLookup.Store(fn) } -func peerWireGuardStateFromDevice(state device.PeerSessionState) PeerWireGuardState { - switch state { - case device.PeerSessionNone: - return PeerWireGuardStateNone - case device.PeerSessionHandshake: - return PeerWireGuardStateHandshake - case device.PeerSessionEstablished: - return PeerWireGuardStateEstablished - case device.PeerSessionExpired: - return PeerWireGuardStateExpired - default: - panic(fmt.Sprintf("unexpected wireguard-go PeerSessionState %d", state)) - } -} - // ResetAndStop resets the engine to a clean state (like calling Reconfig // with all pointers to zero values) and returns the resulting status. // diff --git a/wgengine/userspace_test.go b/wgengine/userspace_test.go index 0aff1b401..7dba7513c 100644 --- a/wgengine/userspace_test.go +++ b/wgengine/userspace_test.go @@ -18,7 +18,6 @@ import ( "testing" "time" - "github.com/tailscale/wireguard-go/device" "go4.org/mem" "tailscale.com/cmd/testwrapper/flakytest" "tailscale.com/control/controlknobs" @@ -44,38 +43,6 @@ import ( "tailscale.com/wgengine/wgcfg" ) -func TestPeerWireGuardStateValuesMatchWireguardGo(t *testing.T) { - const unknownPeerSessionState device.PeerSessionState = 255 - - tests := []struct { - name string - wg device.PeerSessionState - want PeerWireGuardState - }{ - {"none", device.PeerSessionNone, PeerWireGuardStateNone}, - {"handshake", device.PeerSessionHandshake, PeerWireGuardStateHandshake}, - {"established", device.PeerSessionEstablished, PeerWireGuardStateEstablished}, - {"expired", device.PeerSessionExpired, PeerWireGuardStateExpired}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := peerWireGuardStateFromDevice(tt.wg); got != tt.want { - t.Fatalf("converted state = %v; want %v", got, tt.want) - } - if got, want := uint8(tt.wg), uint8(tt.want); got != want { - t.Fatalf("wireguard-go const = %v; want %v", got, want) - } - }) - } - - defer func() { - if recover() == nil { - t.Fatal("expected panic for unknown wireguard-go state") - } - }() - _ = peerWireGuardStateFromDevice(unknownPeerSessionState) -} - func nodeViews(v []*tailcfg.Node) []tailcfg.NodeView { nv := make([]tailcfg.NodeView, len(v)) for i, n := range v { diff --git a/wgengine/wgengine.go b/wgengine/wgengine.go index 86eaa11d3..91002afaf 100644 --- a/wgengine/wgengine.go +++ b/wgengine/wgengine.go @@ -10,6 +10,7 @@ import ( "time" "github.com/gaissmai/bart" + "github.com/tailscale/wireguard-go/device" "tailscale.com/ipn/ipnstate" "tailscale.com/net/dns" "tailscale.com/net/packet" @@ -43,28 +44,6 @@ type StatusCallback func(*Status, error) // into network map updates. type NetworkMapCallback func(*netmap.NetworkMap) -// PeerWireGuardState is the current WireGuard session state for a peer. -type PeerWireGuardState uint8 - -const ( - // PeerWireGuardStateNone means there is no handshake in progress and no - // session key material retained for this peer. - PeerWireGuardStateNone PeerWireGuardState = 0 - - // PeerWireGuardStateHandshake means a handshake is in progress for this - // peer, but there is not currently a usable WireGuard session. - PeerWireGuardStateHandshake PeerWireGuardState = 1 - - // PeerWireGuardStateEstablished means the peer has a completed WireGuard - // session with usable session key material. - PeerWireGuardStateEstablished PeerWireGuardState = 2 - - // PeerWireGuardStateExpired means the peer's session key material is no - // longer considered usable, but final key cleanup or lazy peer removal may - // not have happened yet. - PeerWireGuardStateExpired PeerWireGuardState = 3 -) - // ErrNoChanges is returned by Engine.Reconfig if no changes were made. var ErrNoChanges = errors.New("no changes made to Engine config") @@ -239,7 +218,7 @@ type Engine interface { SetWGPeerLookup(func(wgString string) (tsString string, ok bool)) // SetPeerSessionStateFunc installs a callback used to observe WireGuard - // peer session state transitions. + // peer session state transitions, see [device.PeerSessionStateFunc]. // // Calls are serialized per Engine and delivered in transition order from // wireguard-go, while wireguard-go is holding locks. The callback must be @@ -252,7 +231,7 @@ type Engine interface { // In Tailscale, the usual implementation is // ipnlocal.LocalBackend.onPeerWireGuardState, installed early in // LocalBackend construction. - SetPeerSessionStateFunc(func(key.NodePublic, PeerWireGuardState)) + SetPeerSessionStateFunc(func(key.NodePublic, device.PeerSessionInfo)) SetPeerPriorityMessageOnEstablishmentFunc(fn func(key.NodePublic) (msg []byte))