From ef31dd7bb5fc4c99349f96a2fa43cc56b92f0c44 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 6 Mar 2020 10:36:33 -0800 Subject: [PATCH] wgengine/magicsock: check all 3 fast paths independently. The previous code would skip the DERP short-circuit if roamAddr was set, which is not what we wanted. More generally, hitting any of the fast path conditions is a direct return, so we can just have 3 standalone branches rather than 'else if' stuff. Signed-Off-By: David Anderson --- wgengine/magicsock/magicsock.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 8a2eb36a1..4be0cd073 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1124,16 +1124,23 @@ func (a *AddrSet) SrcToString() string { return "" } func (a *AddrSet) ClearSrc() {} func (a *AddrSet) UpdateDst(new *net.UDPAddr) error { + if new.IP.Equal(derpMagicIP) { + // Never consider DERP addresses as a viable candidate for + // either curAddr or roamAddr. It's only ever a last resort + // choice, never a preferred choice. + // This is a hot path for established connections. + return nil + } + a.mu.Lock() defer a.mu.Unlock() - if a.roamAddr != nil { - if equalUDPAddr(a.roamAddr, new) { - // Packet from the current roaming address, no logging. - // This is a hot path for established connections. - return nil - } - } else if a.curAddr >= 0 && equalUDPAddr(new, &a.addrs[a.curAddr]) { + if a.roamAddr != nil && equalUDPAddr(new, a.roamAddr) { + // Packet from the current roaming address, no logging. + // This is a hot path for established connections. + return nil + } + if a.curAddr >= 0 && equalUDPAddr(new, &a.addrs[a.curAddr]) { // Packet from current-priority address, no logging. // This is a hot path for established connections. return nil