diff --git a/wgengine/magicsock/endpoint.go b/wgengine/magicsock/endpoint.go index 7d62b53d9..43eb09d80 100644 --- a/wgengine/magicsock/endpoint.go +++ b/wgengine/magicsock/endpoint.go @@ -632,10 +632,10 @@ func (de *endpoint) DstToBytes() []byte { return packIPPort(de.fakeWGAddr) } // de.mu must be held. // // TODO(val): Rewrite the addrFor*Locked() variations to share code. -func (de *endpoint) addrForSendLocked(now mono.Time) (udpAddr epAddr, derpAddr netip.AddrPort, sendWGPing bool) { +func (de *endpoint) addrForSendLocked(now mono.Time, forceMirror bool) (udpAddr epAddr, derpAddr netip.AddrPort, sendWGPing bool) { udpAddr = de.bestAddr.epAddr - if udpAddr.ap.IsValid() && !now.After(de.trustBestAddrUntil) { + if udpAddr.ap.IsValid() && !now.After(de.trustBestAddrUntil) && !forceMirror { return udpAddr, netip.AddrPort{}, false } @@ -646,8 +646,8 @@ func (de *endpoint) addrForSendLocked(now mono.Time) (udpAddr epAddr, derpAddr n return udpAddr, netip.AddrPort{}, shouldPing } - // We had a bestAddr but it expired so send both to it - // and DERP. + // udpAddr is nil, or untrusted, or caller forced mirroring. Send over DERP and + // also UDP if available. return udpAddr, de.derpAddr, false } @@ -718,7 +718,7 @@ func (de *endpoint) addrForWireGuardSendLocked(now mono.Time) (udpAddr epAddr, s // de.mu must be held. func (de *endpoint) addrForPingSizeLocked(now mono.Time, size int) (udpAddr epAddr, derpAddr netip.AddrPort) { if size == 0 { - udpAddr, derpAddr, _ = de.addrForSendLocked(now) + udpAddr, derpAddr, _ = de.addrForSendLocked(now, false) return } @@ -928,7 +928,7 @@ func (de *endpoint) heartbeat() { return } - udpAddr, _, _ := de.addrForSendLocked(now) + udpAddr, _, _ := de.addrForSendLocked(now, false) if udpAddr.ap.IsValid() { // We have a preferred path. Ping that every 'heartbeatInterval'. de.startDiscoPingLocked(udpAddr, now, pingHeartbeat, 0, nil) @@ -1109,7 +1109,7 @@ var ( errPingTooBig = errors.New("ping size too big") ) -func (de *endpoint) send(flow int, buffs [][]byte, offset int) error { +func (de *endpoint) send(flow int, buffs [][]byte, offset int, forceMirror bool) error { de.mu.Lock() if de.expired { de.mu.Unlock() @@ -1117,7 +1117,7 @@ func (de *endpoint) send(flow int, buffs [][]byte, offset int) error { } now := mono.Now() - udpAddr, derpAddr, startWGPing := de.addrForSendLocked(now) + udpAddr, derpAddr, startWGPing := de.addrForSendLocked(now, forceMirror) if !hasUDPTransport { // There are no UDP paths to discover; everything goes over DERP. @@ -2267,7 +2267,7 @@ func (de *endpoint) populatePeerStatus(ps *ipnstate.PeerStatus) { ps.LastWrite = de.lastSendExt.WallTime() ps.Active = now.Sub(de.lastSendExt) < sessionActiveTimeout - if udpAddr, derpAddr, _ := de.addrForSendLocked(now); udpAddr.ap.IsValid() && !derpAddr.IsValid() { + if udpAddr, derpAddr, _ := de.addrForSendLocked(now, false); udpAddr.ap.IsValid() && !derpAddr.IsValid() { if udpAddr.vni.IsSet() { ps.PeerRelay = udpAddr.String() } else { diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index faadeda39..0e72861d0 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1542,7 +1542,22 @@ func (c *Conn) SendTo(flow int, buffs [][]byte, ep conn.Endpoint, offset int) (e } switch ep := ep.(type) { case *endpoint: - return ep.send(flow, buffs, offset) + forceMirror := false + // We want to mirror retried hybrid post-quantum handshakes to DERP even when a + // valid UDP path exists, to mitigate against UDP datapaths whose PMTU cannot deliver + // the larger hybrid handshake packets. + // + // The following logic mirrors the entire packet batch if any packet within appears to + // be a handshake retry. If the batch contains both transport packets and handshake + // packets, this will mirror transport packets that don't strictly need to be mirrored. + // + // As of 2026-11-05, handshakes are always emitted as a separate batch of length 1 and + // not coalesced with transport data, and there are no plans to change that. So, + // this logic should precisely mirror handshake retries only. + if batchContainsHandshakeRetry(buffs, offset) { + forceMirror = true + } + return ep.send(flow, buffs, offset, forceMirror) case *lazyEndpoint: // A [*lazyEndpoint] may end up on this TX codepath when wireguard-go is // deemed "under handshake load" and ends up transmitting a cookie reply @@ -1558,6 +1573,17 @@ func (c *Conn) SendTo(flow int, buffs [][]byte, ep conn.Endpoint, offset int) (e return nil } +// batchContainsHandshakeRetry reports whether the packet batch contains at least one +// packet that appears to be a retried handshake. +func batchContainsHandshakeRetry(buffs [][]byte, offset int) bool { + for _, b := range buffs { + if device.PacketIsRetry(b[offset:]) { + return true + } + } + return false +} + var errConnClosed = errors.New("Conn closed") var errDropDerpPacket = errors.New("too many DERP packets queued; dropping") diff --git a/wgengine/magicsock/magicsock_test.go b/wgengine/magicsock/magicsock_test.go index a17f14c74..806fa731f 100644 --- a/wgengine/magicsock/magicsock_test.go +++ b/wgengine/magicsock/magicsock_test.go @@ -3262,7 +3262,7 @@ func TestAddrForSendLockedForWireGuardOnly(t *testing.T) { for _, epd := range test.ep { endpoint.endpointState[epd.addrPort] = &endpointState{} } - udpAddr, _, shouldPing := endpoint.addrForSendLocked(testTime) + udpAddr, _, shouldPing := endpoint.addrForSendLocked(testTime, false) if udpAddr.ap.IsValid() != test.validAddr { t.Errorf("udpAddr validity is incorrect; got %v, want %v", udpAddr.ap.IsValid(), test.validAddr) } @@ -3289,7 +3289,7 @@ func TestAddrForSendLockedForWireGuardOnly(t *testing.T) { state.recentPong = 0 } - udpAddr, _, shouldPing = endpoint.addrForSendLocked(secondPingTime) + udpAddr, _, shouldPing = endpoint.addrForSendLocked(secondPingTime, false) if udpAddr != test.want { t.Errorf("udpAddr returned is not expected: got %v, want %v", udpAddr, test.want) }