mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 20:22:41 +09:00
net/netcheck: never prefer a STUN-only region as the home DERP (#21976)
The PreferredDERP selection loop only considers RegionLatency, which STUN probes populate for STUN-only regions too. When such a region is the only one with a latency sample (e.g. self-hosted DERP behind a TCP-only tunnel while a public STUN server is reachable), it gets selected as the preferred DERP and all relay traffic blackholes, since the region has no DERP server to relay through. Filter STUN-only regions in the selection loop by reusing the existing regionHasDERPNode predicate (renamed to CanServeDERP), which was already applied to the HTTPS/ICMP fallback probing but not here. Regions missing from the map, or present without any nodes, keep the old behavior, as some tests use latency-only DERP maps. Fixes #21975 Signed-off-by: Luo Chen <luochen1990@gmail.com>
This commit is contained in:
1 parent
a967b7d811
commit
dc69869769
4 files changed
+157
-12
No files matched your search
@@ -1004,7 +1004,7 @@ func (c *Client) GetReport(ctx context.Context, dm *tailcfg.DERPMap, opts *GetRe
|
||||
var wg sync.WaitGroup
|
||||
var need []*tailcfg.DERPRegion
|
||||
for rid, reg := range dm.Regions {
|
||||
if !rs.haveRegionLatency(tailcfg.DERPRegionID(rid)) && regionHasDERPNode(reg) && !reg.Avoid && !reg.NoMeasureNoHome {
|
||||
if !rs.haveRegionLatency(tailcfg.DERPRegionID(rid)) && reg.CanServeDERP() && !reg.Avoid && !reg.NoMeasureNoHome {
|
||||
need = append(need, reg)
|
||||
}
|
||||
}
|
||||
@@ -1466,6 +1466,14 @@ func (c *Client) addReportHistoryAndSetPreferredDERP(rs *reportState, r *Report,
|
||||
d = time.Duration(float64(d) * score)
|
||||
}
|
||||
|
||||
// A STUN-only region has no DERP server to relay through, so it must
|
||||
// never become the preferred DERP even though its STUN probe gives it
|
||||
// a RegionLatency entry. Regions missing from the map or without any
|
||||
// DERP server are equally unable to serve as a home.
|
||||
if !dm.Regions().Get(regionID).CanServeDERP() {
|
||||
continue
|
||||
}
|
||||
|
||||
if regionID == prevDERP {
|
||||
oldRegionCurLatency = d
|
||||
}
|
||||
@@ -1755,15 +1763,6 @@ func (c *Client) nodeAddrPort(ctx context.Context, n *tailcfg.DERPNode, port int
|
||||
return zero, false
|
||||
}
|
||||
|
||||
func regionHasDERPNode(r *tailcfg.DERPRegion) bool {
|
||||
for _, n := range r.Nodes {
|
||||
if !n.STUNOnly {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func maxDurationValue(m map[tailcfg.DERPRegionID]time.Duration) (max time.Duration) {
|
||||
for _, v := range m {
|
||||
if v > max {
|
||||
|
||||
@@ -47,7 +47,12 @@ func TestBasic(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, err := c.GetReport(ctx, stuntest.DERPMapOf(stunAddr.String()), nil)
|
||||
dm := stuntest.DERPMapOf(stunAddr.String())
|
||||
// stuntest nodes are all STUNOnly; make this a regular DERP region so
|
||||
// that the PreferredDERP assertion below exercises the normal path.
|
||||
dm.Regions[1].Nodes[0].STUNOnly = false
|
||||
|
||||
r, err := c.GetReport(ctx, dm, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -130,6 +135,51 @@ func TestSTUNResponseProvesCanSend(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestSTUNOnlyRegionNotPreferredDERP verifies a STUNOnly region is
|
||||
// never selected as the preferred DERP, even when it's the only region
|
||||
// with a STUN latency sample (as in real setups where only a public
|
||||
// STUN server is reachable, e.g. self-hosted DERP behind a TCP-only
|
||||
// tunnel), where selecting it would blackhole all relay traffic.
|
||||
func TestSTUNOnlyRegionNotPreferredDERP(t *testing.T) {
|
||||
// Region 1: a real, reachable STUN server marked STUNOnly.
|
||||
stunAddr, cleanup := stuntest.Serve(t)
|
||||
defer cleanup()
|
||||
|
||||
// Region 2: a UDP blackhole (unreachable) normal DERP region.
|
||||
blackhole, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open blackhole STUN listener: %v", err)
|
||||
}
|
||||
defer blackhole.Close()
|
||||
|
||||
dm := stuntest.DERPMapOf(stunAddr.String(), blackhole.LocalAddr().String())
|
||||
dm.Regions[1].Nodes[0].STUNOnly = true
|
||||
dm.Regions[2].Nodes[0].STUNOnly = false
|
||||
|
||||
c := newTestClient(t)
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
if err := c.Standalone(ctx, "127.0.0.1:0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r, err := c.GetReport(ctx, dm, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The STUN-only region must have produced a latency sample...
|
||||
if _, ok := r.RegionLatency[1]; !ok {
|
||||
t.Fatalf("expected region 1 (STUNOnly) to have a STUN latency sample; got %+v", r.RegionLatency)
|
||||
}
|
||||
// ...but must not be selected as the preferred DERP, even though it
|
||||
// is the only region with a latency sample.
|
||||
if r.PreferredDERP == 1 {
|
||||
t.Errorf("PreferredDERP = 1 (STUNOnly region); want a region with DERP nodes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorksWhenUDPBlocked(t *testing.T) {
|
||||
blackhole, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
@@ -530,7 +580,10 @@ func TestAddReportHistoryAndSetPreferredDERP(t *testing.T) {
|
||||
Regions: map[tailcfg.DERPRegionID]*tailcfg.DERPRegion{},
|
||||
}
|
||||
for _, id := range regions {
|
||||
dm.Regions[id] = &tailcfg.DERPRegion{RegionID: id}
|
||||
dm.Regions[id] = &tailcfg.DERPRegion{
|
||||
RegionID: id,
|
||||
Nodes: []*tailcfg.DERPNode{{RegionID: id, Name: fmt.Sprintf("%da", id), HostName: fmt.Sprintf("derp%d.invalid", id)}},
|
||||
}
|
||||
}
|
||||
return dm
|
||||
}
|
||||
|
||||
@@ -183,6 +183,33 @@ type DERPRegion struct {
|
||||
Nodes []*DERPNode
|
||||
}
|
||||
|
||||
// CanServeDERP reports whether this DERP region can serve as a DERP home.
|
||||
// Regions that have no DERP servers can never be DERP homes.
|
||||
func (r *DERPRegion) CanServeDERP() bool {
|
||||
if r == nil {
|
||||
return false
|
||||
}
|
||||
for _, n := range r.Nodes {
|
||||
if !n.STUNOnly {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CanServeDERP is [DERPRegion.CanServeDERP] for [DERPRegionView].
|
||||
func (v DERPRegionView) CanServeDERP() bool {
|
||||
if !v.Valid() {
|
||||
return false
|
||||
}
|
||||
for _, n := range v.Nodes().All() {
|
||||
if !n.STUNOnly() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// DERPNode describes a DERP packet relay node running within a DERPRegion.
|
||||
type DERPNode struct {
|
||||
// Name is a unique node name (across all regions).
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package tailcfg
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDERPRegionCanServeDERP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
region *DERPRegion
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "nil_region",
|
||||
region: (*DERPRegion)(nil),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "no_nodes",
|
||||
region: &DERPRegion{},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "stun_only",
|
||||
region: &DERPRegion{
|
||||
Nodes: []*DERPNode{
|
||||
{Name: "1a", STUNOnly: true},
|
||||
{Name: "1b", STUNOnly: true},
|
||||
},
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "mixed",
|
||||
region: &DERPRegion{
|
||||
Nodes: []*DERPNode{
|
||||
{Name: "1a", STUNOnly: true},
|
||||
{Name: "1b"},
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "derp_only",
|
||||
region: &DERPRegion{
|
||||
Nodes: []*DERPNode{{Name: "1a"}},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.region.CanServeDERP(); got != tt.want {
|
||||
t.Errorf("CanServeDERP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
var v DERPRegionView
|
||||
if tt.region != nil {
|
||||
v = tt.region.View()
|
||||
}
|
||||
if got := v.CanServeDERP(); got != tt.want {
|
||||
t.Errorf("DERPRegionView.CanServeDERP() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user