From 2ca7f41c1972590cbe39c5bb3380eab3f547979f Mon Sep 17 00:00:00 2001 From: cnupy <38653063+cnupy@users.noreply.github.com> Date: Wed, 2 Dec 2026 15:37:06 +0200 Subject: [PATCH] tailcat: keep STUN-only nodes in addresses wireRegionOf dropped every STUN-only node, so a server whose relay can't answer STUN (for example one on Cloudflare Workers, which has no UDP) had no way to hand its clients a STUN server through its address. The wire format now carries STUNOnly as a new CBOR bool ("o", omitted when false), and wireRegionOf keeps STUN-only nodes. derphttp already skips STUN-only nodes when it dials DERP, and netcheck's STUN probes rotate through all of a region's nodes, so these nodes are only used for STUN. Resolve still leaves out a DERP map's STUN-only nodes, since those regions' relay nodes answer STUN themselves. Addresses without STUN-only nodes encode to the same bytes as before, and older addresses still parse. Clients from before this change ignore the unknown "o" key and see a plain node. derphttp dials a region's nodes in order, so such a client only dials a STUN-only node for DERP if the relay nodes before it fail, which costs it one more failed dial. Servers should list their relay nodes first. Fixes #200 Signed-off-by: cnupy <38653063+cnupy@users.noreply.github.com> --- resolve_stun_test.go | 4 +-- tailcat.go | 8 ++--- wire.go | 10 +++--- wire_test.go | 81 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 89 insertions(+), 14 deletions(-) diff --git a/resolve_stun_test.go b/resolve_stun_test.go index c4b757063..838fc4fc1 100644 --- a/resolve_stun_test.go +++ b/resolve_stun_test.go @@ -13,8 +13,8 @@ import ( ) // TestResolveEmbedsRelayNodes checks that resolving a short address embeds -// usable DERP relay nodes. STUN-only nodes can't relay DERP traffic and are -// dropped when the address is encoded (see wireRegionOf), so truncating the +// usable DERP relay nodes. STUN-only nodes can't relay DERP traffic and +// Resolve leaves a DERP map's STUN-only nodes out, so truncating the // region's raw node list before filtering them can leave a resolved address // with fewer than two relay nodes, or none at all, breaking the promise that // a resolved address is self-contained and works offline. diff --git a/tailcat.go b/tailcat.go index 7ec393c20..165174f75 100644 --- a/tailcat.go +++ b/tailcat.go @@ -1177,10 +1177,10 @@ func (a Addr) Resolve(ctx context.Context, opts ...any) (Addr, error) { if err := ci.Expand(ctx, opts...); err != nil { return "", err } - // Keep the address short: two relay nodes suffice. STUN-only nodes - // are dropped by wireRegionOf when the address is encoded, so take - // the first two usable nodes here rather than a raw prefix of the - // region's node list, which STUN-only nodes may fill. + // Keep the address short: two relay nodes suffice. A DERP map's own + // relay nodes answer STUN, so leave out its STUN-only nodes and take + // the first two relay nodes rather than a raw prefix of the region's + // node list, which STUN-only nodes may fill. for _, r := range ci.Region { var nodes []*tailcfg.DERPNode for _, n := range r.Nodes { diff --git a/wire.go b/wire.go index 71edab52a..23a4e3ee9 100644 --- a/wire.go +++ b/wire.go @@ -71,13 +71,12 @@ type wireNode struct { STUNPort int `cbor:"s,omitempty" json:"STUNPort,omitempty"` DERPPort int `cbor:"d,omitempty" json:"DERPPort,omitempty"` InsecureForTests bool `cbor:"x,omitempty" json:"InsecureForTests,omitempty"` + STUNOnly bool `cbor:"o,omitempty" json:"STUNOnly,omitempty"` } // wireRegionOf converts a [tailcfg.DERPRegion] (such as one from the // control plane's DERP map) to its wire form. Fields tailcat doesn't -// use (Latitude, Longitude, CanPort80, ...) are dropped, as are -// STUN-only nodes: they can't relay DERP traffic, which is all an -// embedded region is for. +// use (Latitude, Longitude, CanPort80, ...) are dropped. func wireRegionOf(r *tailcfg.DERPRegion) *wireRegion { w := &wireRegion{ RegionID: r.RegionID.Int64(), @@ -85,9 +84,6 @@ func wireRegionOf(r *tailcfg.DERPRegion) *wireRegion { RegionName: r.RegionName, } for _, n := range r.Nodes { - if n.STUNOnly { - continue - } w.Nodes = append(w.Nodes, &wireNode{ Name: n.Name, RegionID: n.RegionID.Int64(), @@ -98,6 +94,7 @@ func wireRegionOf(r *tailcfg.DERPRegion) *wireRegion { STUNPort: n.STUNPort, DERPPort: n.DERPPort, InsecureForTests: n.InsecureForTests, + STUNOnly: n.STUNOnly, }) } return w @@ -121,6 +118,7 @@ func (w *wireRegion) derpRegion() *tailcfg.DERPRegion { STUNPort: n.STUNPort, DERPPort: n.DERPPort, InsecureForTests: n.InsecureForTests, + STUNOnly: n.STUNOnly, }) } return r diff --git a/wire_test.go b/wire_test.go index e540f63c2..84b222d23 100644 --- a/wire_test.go +++ b/wire_test.go @@ -8,8 +8,10 @@ import ( "strings" "testing" + "github.com/fxamacker/cbor/v2" "github.com/google/go-cmp/cmp" "tailscale.com/tailcfg" + "tailscale.com/types/logger" ) // wireFieldNames maps the short CBOR field names used by the wire @@ -37,6 +39,7 @@ var wireFieldNames = map[string]string{ "s": "STUNPort", "d": "DERPPort", "x": "InsecureForTests", + "o": "STUNOnly", } // TestWireFieldNames verifies that every field of every wire type @@ -83,7 +86,7 @@ func TestWireFieldNames(t *testing.T) { // TestWireRegionRoundTrip tests the mapping between the upstream // tailcfg DERP types (as fetched from the control plane's DERP map) // and tailcat's wire types: fields tailcat uses survive the round -// trip, unused fields are dropped, and STUN-only nodes disappear. +// trip, unused fields are dropped, and STUN-only nodes keep their flag. func TestWireRegionRoundTrip(t *testing.T) { in := &tailcfg.DERPRegion{ RegionID: 10, @@ -108,7 +111,7 @@ func TestWireRegionRoundTrip(t *testing.T) { Name: "10s", RegionID: 10, HostName: "stun-only.tailscale.com", - STUNOnly: true, // whole node dropped + STUNOnly: true, // must survive }, { Name: "custom", @@ -135,6 +138,12 @@ func TestWireRegionRoundTrip(t *testing.T) { STUNPort: 3478, DERPPort: 8443, }, + { + Name: "10s", + RegionID: 10, + HostName: "stun-only.tailscale.com", + STUNOnly: true, + }, { Name: "custom", HostName: "my-derp.example.com", @@ -150,3 +159,71 @@ func TestWireRegionRoundTrip(t *testing.T) { t.Errorf("round trip diff (-want +got):\n%s", diff) } } + +// oldRelayOnlyAddr was made by tailcat v0.8.0, which dropped STUN-only +// nodes, from a region with one relay node (relay.example.com, STUN +// off) and one STUN-only node (stun.example.com:3478). +const oldRelayOnlyAddr = Addr("tcomFwWCABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGFygaFhToGiYWhxcmVsYXkuZXhhbXBsZS5jb21hcyA") + +// TestWireSTUNOnlyCompat checks that the STUNOnly field leaves older +// addresses alone: they still parse, and a region with no STUN-only +// nodes encodes to the same bytes as before. +func TestWireSTUNOnlyCompat(t *testing.T) { + ci, err := ParseAddr(oldRelayOnlyAddr) + if err != nil { + t.Fatalf("ParseAddr(old address): %v", err) + } + if len(ci.Region) != 1 || len(ci.Region[0].Nodes) != 1 { + t.Fatalf("old address regions = %v; want one region with one node", logger.AsJSON(ci.Region)) + } + if n := ci.Region[0].Nodes[0]; n.HostName != "relay.example.com" || n.STUNOnly { + t.Fatalf("old address node = %v; want relay.example.com, not STUN-only", logger.AsJSON(n)) + } + + relayOnly := ConnInfo{ + ServerPublic: ci.ServerPublic, + Region: []*tailcfg.DERPRegion{{ + Nodes: []*tailcfg.DERPNode{{HostName: "relay.example.com", STUNPort: -1}}, + }}, + } + if got := relayOnly.Addr(); got != oldRelayOnlyAddr { + t.Errorf("relay-only region encodes to %s; want the old bytes %s", got, oldRelayOnlyAddr) + } + + withSTUN := relayOnly + withSTUN.Region = []*tailcfg.DERPRegion{{ + Nodes: []*tailcfg.DERPNode{ + {HostName: "relay.example.com", STUNPort: -1}, + {HostName: "stun.example.com", STUNPort: 3478, STUNOnly: true}, + }, + }} + back, err := ParseAddr(withSTUN.Addr()) + if err != nil { + t.Fatalf("ParseAddr(new address): %v", err) + } + nodes := back.Region[0].Nodes + if len(nodes) != 2 || nodes[0].STUNOnly || !nodes[1].STUNOnly || nodes[1].STUNPort != 3478 { + t.Errorf("new address nodes = %v; want the relay then the STUN-only node", logger.AsJSON(nodes)) + } +} + +// TestOldClientsIgnoreSTUNOnly verifies that tailcat clients from before +// the STUNOnly field, whose wire node type has no "o" key, still decode +// a node that carries it, and so see it as a plain node. +func TestOldClientsIgnoreSTUNOnly(t *testing.T) { + type oldWireNode struct { + HostName string `cbor:"h,omitempty"` + STUNPort int `cbor:"s,omitempty"` + } + x, err := cbor.Marshal(&wireNode{HostName: "stun.example.com", STUNPort: 3478, STUNOnly: true}) + if err != nil { + t.Fatal(err) + } + var old oldWireNode + if err := cbor.Unmarshal(x, &old); err != nil { + t.Fatalf("old decoder rejected a STUN-only node: %v", err) + } + if old.HostName != "stun.example.com" || old.STUNPort != 3478 { + t.Errorf("old decoder got %+v; want the node's host and STUN port", old) + } +}