wgengine/magicsock: always set home DERP if no control conn

The logic we added in #11378 would prevent selecting a home DERP if we
have no control connection.

Updates tailscale/corp#18095

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: I44bb6ac4393989444e4961b8cfa27dc149a33c6e
pull/12474/head
Andrew Dunham 5 months ago
parent a475c435ec
commit 8161024176

@ -158,6 +158,10 @@ func (c *Conn) maybeSetNearestDERP(report *netcheck.Report) (preferredDERP int)
// //
// For tests, always assume we're connected to control unless we're // For tests, always assume we're connected to control unless we're
// explicitly testing this behaviour. // explicitly testing this behaviour.
//
// Despite the above behaviour, ensure that we set the nearest DERP if
// we don't currently have one set; any DERP server is better than
// none, even if not connected to control.
var connectedToControl bool var connectedToControl bool
if testenv.InTest() && !checkControlHealthDuringNearestDERPInTests { if testenv.InTest() && !checkControlHealthDuringNearestDERPInTests {
connectedToControl = true connectedToControl = true
@ -166,8 +170,15 @@ func (c *Conn) maybeSetNearestDERP(report *netcheck.Report) (preferredDERP int)
} }
if !connectedToControl { if !connectedToControl {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() myDerp := c.myDerp
return c.myDerp c.mu.Unlock()
if myDerp != 0 {
return myDerp
}
// Intentionally fall through; we don't have a current DERP, so
// as mentioned above selecting one even if not connected is
// strictly better than doing nothing.
} }
preferredDERP = report.PreferredDERP preferredDERP = report.PreferredDERP

@ -1276,6 +1276,7 @@ func newTestConn(t testing.TB) *Conn {
conn, err := NewConn(Options{ conn, err := NewConn(Options{
NetMon: netMon, NetMon: netMon,
HealthTracker: new(health.Tracker),
DisablePortMapper: true, DisablePortMapper: true,
Logf: t.Logf, Logf: t.Logf,
Port: port, Port: port,
@ -3128,6 +3129,20 @@ func TestMaybeSetNearestDERP(t *testing.T) {
connectedToControl: false, connectedToControl: false,
want: 1, // no change want: 1, // no change
}, },
{
name: "not_connected_with_report_derp_and_no_current",
old: 0, // no current DERP
reportDERP: 21, // have new DERP
connectedToControl: false, // not connected...
want: 21, // ... but want to change to new DERP
},
{
name: "not_connected_with_fallback_and_no_current",
old: 0, // no current DERP
reportDERP: 0, // no new DERP
connectedToControl: false, // not connected...
want: 31, // ... but we fallback to deterministic value
},
{ {
name: "connected_no_derp", name: "connected_no_derp",
old: 1, old: 1,

Loading…
Cancel
Save