From d0512a9ef7790cd69336dabccad8ab3d351f769e Mon Sep 17 00:00:00 2001 From: Jordan Whited Date: Wed, 18 Nov 2026 15:48:38 -0800 Subject: [PATCH] wgengine/magicsock: update packetLooksLike docs And associated tests to cover WireGuard handshake messages with the retry bit set. Updates tailscale/corp#50068 Signed-off-by: Jordan Whited --- wgengine/magicsock/magicsock.go | 30 +++++++++++++--------------- wgengine/magicsock/magicsock_test.go | 26 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 0e72861d0..0a92bbde3 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -2211,23 +2211,21 @@ const ( // Geneve header where the LSB is always 0 (part of 6 "reserved" bits). // // 2. STUN binding @ msg[1] (0x01) is sufficiently non-overlapping with naked -// WireGuard, which is always a 0 byte value (WireGuard message type -// occupies msg[0:4], and msg[1:4] are always 0). +// WireGuard (0x00). // -// 3. STUN binding @ msg[1] (0x01) is sufficiently non-overlapping with the -// second byte of [disco.Magic] (0x53). +// 3. STUN binding @ msg[1] (0x01) is sufficiently non-overlapping with +// [disco.Magic] (0x53). // -// 4. [disco.Magic] @ msg[2:4] (0xf09f) is sufficiently non-overlapping with a -// Geneve protocol field value of [packet.GeneveProtocolDisco] or -// [packet.GeneveProtocolWireGuard] . +// 4. [disco.Magic] @ msg[2] (0xf0) is sufficiently non-overlapping with a +// Geneve protocol field value of [packet.GeneveProtocolDisco] (0x7a) or +// [packet.GeneveProtocolWireGuard] (0x7a). // -// 5. [disco.Magic] @ msg[0] (0x54) is sufficiently non-overlapping with the -// first byte of a WireGuard packet (0x01-0x04). +// 5. [disco.Magic] @ msg[0] (0x54) is sufficiently non-overlapping with +// WireGuard (0x01-0x04). // -// 6. [packet.GeneveHeader] with a Geneve protocol field value of -// [packet.GeneveProtocolDisco] or [packet.GeneveProtocolWireGuard] -// (msg[2:4]) is sufficiently non-overlapping with the second 2 bytes of a -// WireGuard packet which are always 0x0000. +// 6. [packet.GeneveHeader] with a Geneve protocol field value starting with (msg[2]) +// [packet.GeneveProtocolDisco] (0x7a) or [packet.GeneveProtocolWireGuard] (0x7a) +// is sufficiently non-overlapping with naked WireGuard (0x00). func packetLooksLike(msg []byte) (t packetLooksLikeType, isGeneveEncap bool) { if stun.Is(msg) && msg[1] == 0x01 { // method binding @@ -2256,9 +2254,9 @@ func packetLooksLike(msg []byte) (t packetLooksLikeType, isGeneveEncap bool) { // The Geneve header is well-formed, and it indicated this // was disco, but it's not. The evaluated bytes at this point // are always distinct from naked WireGuard (msg[2:4] are always - // 0x0000) and naked Disco (msg[2:4] are always 0xf09f), but - // maintain pre-Geneve behavior and fall back to assuming it's - // naked WireGuard. + // 0x0000 or 0x0080 w/retry bit set) and naked Disco (msg[2:4] + // are always 0xf09f), but maintain pre-Geneve behavior and fall + // back to assuming it's naked WireGuard. return packetLooksLikeWireGuard, false } case packet.GeneveProtocolWireGuard: diff --git a/wgengine/magicsock/magicsock_test.go b/wgengine/magicsock/magicsock_test.go index 806fa731f..8fdbefddc 100644 --- a/wgengine/magicsock/magicsock_test.go +++ b/wgengine/magicsock/magicsock_test.go @@ -3843,6 +3843,20 @@ func Test_packetLooksLike(t *testing.T) { nakedWireGuardInitiation := make([]byte, len(geneveEncapDisco)) binary.LittleEndian.PutUint32(nakedWireGuardInitiation, device.MessageInitiationType) + nakedWireGuardInitiationRetry := make([]byte, len(geneveEncapDisco)) + binary.LittleEndian.PutUint32(nakedWireGuardInitiationRetry, device.MessageInitiationType) + nakedWireGuardInitiationRetry[3] |= 0x80 + nakedWireGuardResponseRetry := make([]byte, len(geneveEncapDisco)) + binary.LittleEndian.PutUint32(nakedWireGuardResponseRetry, device.MessageResponseType) + nakedWireGuardResponseRetry[3] |= 0x80 + t.Run("retry-fixtures-packet-is-retry", func(t *testing.T) { + if !device.PacketIsRetry(nakedWireGuardInitiationRetry) { + t.Fatal("device.PacketIsRetry returned false for initiation message retry fixture") + } + if !device.PacketIsRetry(nakedWireGuardResponseRetry) { + t.Fatal("device.PacketIsRetry returned false for response message retry fixture") + } + }) nakedWireGuardResponse := make([]byte, len(geneveEncapDisco)) binary.LittleEndian.PutUint32(nakedWireGuardResponse, device.MessageResponseType) nakedWireGuardCookieReply := make([]byte, len(geneveEncapDisco)) @@ -3970,6 +3984,18 @@ func Test_packetLooksLike(t *testing.T) { wantPacketLooksLikeType: packetLooksLikeWireGuard, wantIsGeneveEncap: false, }, + { + name: "naked-WireGuard-Initiation-type-retry-set", + msg: nakedWireGuardInitiationRetry, + wantPacketLooksLikeType: packetLooksLikeWireGuard, + wantIsGeneveEncap: false, + }, + { + name: "naked-WireGuard-Response-type-retry-set", + msg: nakedWireGuardResponseRetry, + wantPacketLooksLikeType: packetLooksLikeWireGuard, + wantIsGeneveEncap: false, + }, { name: "naked-WireGuard-Cookie-Reply-type", msg: nakedWireGuardCookieReply,