mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 17:21:18 -08:00
x
This commit is contained in:
6 files changed
+48
-52
No files matched your search
@@ -242,7 +242,6 @@ func (m *manager) handleConnectionStateChange(ps *peerState, state webrtc.PeerCo
|
||||
|
||||
m.mu.Lock()
|
||||
|
||||
var clearBestAddr bool
|
||||
switch state {
|
||||
case webrtc.PeerConnectionStateConnected:
|
||||
ps.state = stateConnected
|
||||
@@ -273,28 +272,18 @@ func (m *manager) handleConnectionStateChange(ps *peerState, state webrtc.PeerCo
|
||||
case webrtc.PeerConnectionStateFailed:
|
||||
ps.state = stateFailed
|
||||
ps.dcRW.Store(nil)
|
||||
clearBestAddr = true
|
||||
case webrtc.PeerConnectionStateClosed:
|
||||
ps.state = stateClosed
|
||||
ps.dcRW.Store(nil)
|
||||
clearBestAddr = true
|
||||
case webrtc.PeerConnectionStateDisconnected:
|
||||
// Disconnected is recoverable in principle, but no traffic flows while
|
||||
// we're in it. Give up the WebRTC bestAddr immediately so magicsock
|
||||
// falls back to DERP instead of black-holing packets for the (up to
|
||||
// ~seconds) it takes pion to reach Failed. If ICE recovers to Connected,
|
||||
// SetWebRTCPath re-promotes the path. We do not clear dcRW here since the
|
||||
// channel may still resume.
|
||||
clearBestAddr = true
|
||||
// Recoverable in principle; nothing to do. WebRTC liveness is driven by
|
||||
// disco heartbeat pongs, so when traffic stops flowing the trust window
|
||||
// lapses (~6.5s) and magicsock falls back to DERP. bestAddr is left to
|
||||
// the disco machinery rather than demoted on this pion state edge, which
|
||||
// keeps the two sides in agreement.
|
||||
}
|
||||
|
||||
m.mu.Unlock()
|
||||
|
||||
// ClearWebRTCPath acquires the endpoint lock; must be called without m.mu held.
|
||||
if clearBestAddr {
|
||||
ps.peer.ClearWebRTCPath()
|
||||
m.logf("webrtc: cleared WebRTC bestAddr for peer %v, falling back to DERP", ps.remoteDisco.ShortString())
|
||||
}
|
||||
}
|
||||
|
||||
// handleConnectionReady marks a WebRTC connection as ready and updates the peer.
|
||||
|
||||
@@ -82,15 +82,16 @@ func (m *manager) handleRemoteOffer(remoteDisco key.DiscoPublic, offer *webrtc.S
|
||||
// because a stale/dead ICE connection hasn't surfaced as Failed yet,
|
||||
// which is exactly how we can get wedged (we ignore the peer's
|
||||
// recovery offer while black-holing traffic on a dead path). So the
|
||||
// peer's re-offer always wins: tear down our entry, drop the WebRTC
|
||||
// path, and answer fresh below.
|
||||
// peer's re-offer always wins: tear down our entry and answer fresh
|
||||
// below. bestAddr is left to the disco machinery: with the channel
|
||||
// gone the heartbeat pongs stop and the trust window lapses to DERP,
|
||||
// and the rebuilt channel promotes WebRTC again via SetWebRTCPath.
|
||||
m.logf("webrtc: peer %v re-offered while %v; rebuilding connection", remoteDisco.ShortString(), state)
|
||||
ps.peerConn.Close()
|
||||
m.mu.Lock()
|
||||
delete(m.peerConnectionsByPeer, ps.peer)
|
||||
delete(m.peerConnectionsByDisco, remoteDisco)
|
||||
m.mu.Unlock()
|
||||
ps.peer.ClearWebRTCPath()
|
||||
exists = false
|
||||
default:
|
||||
// Any other transitional signaling state; ignore, let it settle.
|
||||
|
||||
@@ -923,12 +923,14 @@ func (de *endpoint) heartbeat() {
|
||||
}
|
||||
|
||||
udpAddr, _, _ := de.addrForSendLocked(now)
|
||||
if udpAddr.ap.IsValid() && udpAddr.ap.Addr() != tailcfg.WebRTCMagicIPAddr {
|
||||
if udpAddr.ap.IsValid() {
|
||||
// We have a preferred path. Ping that every 'heartbeatInterval'.
|
||||
// The WebRTC magic address is excluded: it has no entry in
|
||||
// de.endpointState (WebRTC paths don't go through disco endpoint
|
||||
// discovery), and its liveness is managed by the data channel and
|
||||
// pion's ICE agent, not by disco pings.
|
||||
// This includes the WebRTC magic address: like a peer-relay (VNI)
|
||||
// path, WebRTC is bestAddr-only (never in de.endpointState) but its
|
||||
// liveness is driven by these disco pings over the data channel, not
|
||||
// by pion's connection-state callbacks. startDiscoPingLocked and
|
||||
// handlePongConnLocked special-case the magic address the same way
|
||||
// they special-case vni.IsSet().
|
||||
de.startDiscoPingLocked(udpAddr, now, pingHeartbeat, 0, nil)
|
||||
}
|
||||
|
||||
@@ -1365,7 +1367,12 @@ const (
|
||||
// is interested in the result (such as a CLI "tailscale ping" or a c2n ping
|
||||
// request, etc)
|
||||
func (de *endpoint) startDiscoPingLocked(ep epAddr, now mono.Time, purpose discoPingPurpose, size int, resCB *pingResultAndCallback) {
|
||||
if runtime.GOOS == "js" {
|
||||
isWebRTC := ep.ap.Addr() == tailcfg.WebRTCMagicIPAddr
|
||||
if runtime.GOOS == "js" && !isWebRTC {
|
||||
// A browser has no UDP socket, so it can't disco-ping a real endpoint.
|
||||
// The WebRTC data channel is a usable transport though, so a ping to
|
||||
// the WebRTC magic address is valid on js: sendAddr routes it over the
|
||||
// data channel and never touches a UDP socket.
|
||||
return
|
||||
}
|
||||
if debugNeverDirectUDP() && !ep.vni.IsSet() && ep.ap.Addr() != tailcfg.DerpMagicIPAddr {
|
||||
@@ -1376,7 +1383,7 @@ func (de *endpoint) startDiscoPingLocked(ep epAddr, now mono.Time, purpose disco
|
||||
return
|
||||
}
|
||||
if purpose != pingCLI &&
|
||||
!ep.vni.IsSet() { // de.endpointState is only relevant for direct/non-vni epAddr's
|
||||
!ep.vni.IsSet() && !isWebRTC { // de.endpointState is only relevant for direct/non-vni, non-WebRTC epAddr's
|
||||
st, ok := de.endpointState[ep.ap]
|
||||
if !ok {
|
||||
// Shouldn't happen. But don't ping an endpoint that's
|
||||
@@ -1953,9 +1960,11 @@ func (de *endpoint) handlePongConnLocked(m *disco.Pong, di *discoInfo, src epAdd
|
||||
now := mono.Now()
|
||||
latency := now.Sub(sp.at)
|
||||
|
||||
if !isDerp && !src.vni.IsSet() {
|
||||
// Note: we check vni.isSet() as relay [epAddr]'s are not stored in
|
||||
// endpointState, they are either de.bestAddr or not.
|
||||
isWebRTC := src.ap.Addr() == tailcfg.WebRTCMagicIPAddr
|
||||
if !isDerp && !src.vni.IsSet() && !isWebRTC {
|
||||
// Note: we skip vni.isSet() and the WebRTC magic address as those
|
||||
// [epAddr]'s are not stored in endpointState; they are either
|
||||
// de.bestAddr or not. The promotion block below still runs for them.
|
||||
st, ok := de.endpointState[sp.to.ap]
|
||||
if !ok {
|
||||
// This is no longer an endpoint we care about.
|
||||
|
||||
@@ -2650,7 +2650,14 @@ func (c *Conn) handlePingLocked(dm *disco.Ping, src epAddr, di *discoInfo, derpN
|
||||
|
||||
// Remember this route if not present.
|
||||
var dup bool
|
||||
if isDerp {
|
||||
isWebRTC := src.ap.Addr() == tailcfg.WebRTCMagicIPAddr
|
||||
if isDerp || isWebRTC {
|
||||
// Both DERP and WebRTC carry the sender's node key out of band
|
||||
// (derpNodeSrc), so we resolve the endpoint by node key and reply to
|
||||
// src directly. Unlike a real UDP ping, we must NOT addCandidateEndpoint
|
||||
// the magic address: it isn't a discoverable UDP endpoint, and a
|
||||
// synthetic endpointState entry would be clobbered on the next netmap
|
||||
// update. WebRTC liveness is bestAddr-only, like a peer-relay path.
|
||||
if _, ok := c.peerMap.endpointForNodeKey(derpNodeSrc); ok {
|
||||
numNodes = 1
|
||||
}
|
||||
|
||||
@@ -98,13 +98,11 @@ type WebRTCPeer interface {
|
||||
// which is required before signaling can succeed.
|
||||
DERPReady() bool
|
||||
|
||||
// SetWebRTCPath marks the WebRTC magic address as the peer's best path if
|
||||
// it is better than the peer's current best path.
|
||||
// SetWebRTCPath seeds the WebRTC magic address as the peer's best path (if
|
||||
// better than the current best) when the data channel opens. The disco
|
||||
// heartbeat ping/pong then sustains the path: if pongs stop, the trust
|
||||
// window lapses and traffic falls back to DERP.
|
||||
SetWebRTCPath()
|
||||
|
||||
// ClearWebRTCPath resets the peer's best path if it is currently the
|
||||
// WebRTC magic address, so traffic falls back to DERP.
|
||||
ClearWebRTCPath()
|
||||
}
|
||||
|
||||
// WebRTCBatchMagic is the first byte of a batched WebRTC SCTP message, framed
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/tailscale/wireguard-go/conn"
|
||||
"tailscale.com/disco"
|
||||
@@ -131,13 +130,18 @@ func (de *endpoint) DERPReady() bool {
|
||||
}
|
||||
|
||||
// SetWebRTCPath implements [WebRTCPeer]. It promotes the WebRTC magic address
|
||||
// to the endpoint's best path if it beats the current best path.
|
||||
// to the endpoint's best path if it beats the current best path. This is the
|
||||
// initial promotion on data-channel open, analogous to udpRelayEndpointReady
|
||||
// for a peer-relay path: it seeds bestAddr with a short trust window, after
|
||||
// which the disco heartbeat ping/pong sustains (or expires) the path just like
|
||||
// any other. When the pongs stop, trustBestAddrUntil lapses and traffic falls
|
||||
// back to DERP.
|
||||
func (de *endpoint) SetWebRTCPath() {
|
||||
de.mu.Lock()
|
||||
defer de.mu.Unlock()
|
||||
|
||||
// The magic IP identifies this as WebRTC, not UDP. Latency starts at zero
|
||||
// and is refined by disco pings, same as DERP.
|
||||
// and is refined by the disco heartbeat pongs, same as a peer-relay path.
|
||||
webrtcAddr := addrQuality{
|
||||
epAddr: epAddr{
|
||||
ap: netip.AddrPortFrom(tailcfg.WebRTCMagicIPAddr, webrtcMagicPort),
|
||||
@@ -147,19 +151,7 @@ func (de *endpoint) SetWebRTCPath() {
|
||||
if betterAddr(webrtcAddr, de.bestAddr) {
|
||||
de.bestAddr = webrtcAddr
|
||||
de.bestAddrAt = now
|
||||
de.trustBestAddrUntil = now.Add(5 * time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
// ClearWebRTCPath implements [WebRTCPeer]. If the endpoint's best path is
|
||||
// currently the WebRTC magic address, it resets it so traffic falls back to
|
||||
// DERP.
|
||||
func (de *endpoint) ClearWebRTCPath() {
|
||||
de.mu.Lock()
|
||||
defer de.mu.Unlock()
|
||||
if de.bestAddr.ap.Addr() == tailcfg.WebRTCMagicIPAddr {
|
||||
de.bestAddr = addrQuality{}
|
||||
de.trustBestAddrUntil = 0
|
||||
de.trustBestAddrUntil = now.Add(trustUDPAddrDuration)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user