From c85d84a099469627ea3c895f342a161ef225b3e4 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sat, 11 Jan 2025 07:49:08 -0800 Subject: [PATCH] lanscaping: remove HTTPS/ICMP pings from netcheck -rwxr-xr-x@ 1 bradfitz staff 11577042 Jan 11 07:48 /Users/bradfitz/bin/tailscaled.min -rwxr-xr-x@ 1 bradfitz staff 12058776 Jan 11 07:48 /Users/bradfitz/bin/tailscaled.minlinux Change-Id: I71eb4e17245c53c9733a8b0ee844d1883296ec0f Signed-off-by: Brad Fitzpatrick --- cmd/tailscale/depaware-minlinux.txt | 26 ++-- cmd/tailscaled/depaware-minlinux.txt | 2 - net/netcheck/netcheck.go | 223 --------------------------- wgengine/magicsock/endpoint.go | 73 +-------- wgengine/magicsock/magicsock.go | 17 -- 5 files changed, 10 insertions(+), 331 deletions(-) diff --git a/cmd/tailscale/depaware-minlinux.txt b/cmd/tailscale/depaware-minlinux.txt index 21b2cbb44..74824b6d1 100644 --- a/cmd/tailscale/depaware-minlinux.txt +++ b/cmd/tailscale/depaware-minlinux.txt @@ -58,9 +58,6 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/control/controlhttp from tailscale.com/cmd/tailscale/cli tailscale.com/control/controlhttp/controlhttpcommon from tailscale.com/control/controlhttp tailscale.com/control/controlknobs from tailscale.com/net/portmapper - tailscale.com/derp from tailscale.com/derp/derphttp - tailscale.com/derp/derphttp from tailscale.com/net/netcheck - tailscale.com/disco from tailscale.com/derp tailscale.com/drive from tailscale.com/client/tailscale+ tailscale.com/envknob from tailscale.com/client/tailscale+ tailscale.com/envknob/featureknob from tailscale.com/client/web @@ -71,21 +68,19 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/ipn from tailscale.com/client/tailscale+ tailscale.com/ipn/ipnstate from tailscale.com/client/tailscale+ tailscale.com/licenses from tailscale.com/client/web+ - tailscale.com/metrics from tailscale.com/derp+ + tailscale.com/metrics from tailscale.com/health+ tailscale.com/net/flowtrack from tailscale.com/net/packet tailscale.com/net/netaddr from tailscale.com/ipn+ tailscale.com/net/netcheck from tailscale.com/cmd/tailscale/cli tailscale.com/net/neterror from tailscale.com/net/netcheck+ tailscale.com/net/netknob from tailscale.com/net/netns 💣 tailscale.com/net/netmon from tailscale.com/cmd/tailscale/cli+ - tailscale.com/net/netns from tailscale.com/derp/derphttp+ + tailscale.com/net/netns from tailscale.com/net/netcheck+ tailscale.com/net/netutil from tailscale.com/client/tailscale+ tailscale.com/net/packet from tailscale.com/wgengine/capture - tailscale.com/net/ping from tailscale.com/net/netcheck tailscale.com/net/portmapper from tailscale.com/cmd/tailscale/cli tailscale.com/net/sockstats from tailscale.com/control/controlhttp+ tailscale.com/net/stun from tailscale.com/net/netcheck - L tailscale.com/net/tcpinfo from tailscale.com/derp tailscale.com/net/tlsdial from tailscale.com/cmd/tailscale/cli+ tailscale.com/net/tsaddr from tailscale.com/client/web+ tailscale.com/net/tshttpproxy from tailscale.com/clientupdate/distsign+ @@ -94,9 +89,9 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/syncs from tailscale.com/cmd/tailscale/cli+ tailscale.com/tailcfg from tailscale.com/client/tailscale+ tailscale.com/tempfork/spf13/cobra from tailscale.com/cmd/tailscale/cli/ffcomplete+ - tailscale.com/tstime from tailscale.com/control/controlhttp+ + tailscale.com/tstime from tailscale.com/control/controlhttp tailscale.com/tstime/mono from tailscale.com/tstime/rate - tailscale.com/tstime/rate from tailscale.com/cmd/tailscale/cli+ + tailscale.com/tstime/rate from tailscale.com/cmd/tailscale/cli tailscale.com/tsweb/varz from tailscale.com/util/usermetric tailscale.com/types/dnstype from tailscale.com/client/tailscale+ tailscale.com/types/empty from tailscale.com/ipn @@ -118,7 +113,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/util/clientmetric from tailscale.com/net/netcheck+ tailscale.com/util/cloudenv from tailscale.com/hostinfo tailscale.com/util/cmpver from tailscale.com/clientupdate - tailscale.com/util/ctxkey from tailscale.com/derp+ + tailscale.com/util/ctxkey from tailscale.com/types/logger 💣 tailscale.com/util/deephash from tailscale.com/util/syspolicy/setting L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics tailscale.com/util/dnsname from tailscale.com/cmd/tailscale/cli+ @@ -132,7 +127,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/util/must from tailscale.com/clientupdate/distsign+ tailscale.com/util/nocasemaps from tailscale.com/types/ipproto tailscale.com/util/quarantine from tailscale.com/cmd/tailscale/cli - tailscale.com/util/set from tailscale.com/derp+ + tailscale.com/util/set from tailscale.com/health+ tailscale.com/util/slicesx from tailscale.com/cmd/tailscale/cli+ tailscale.com/util/syspolicy from tailscale.com/ipn tailscale.com/util/syspolicy/internal from tailscale.com/util/syspolicy+ @@ -163,16 +158,13 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep golang.org/x/crypto/salsa20/salsa from golang.org/x/crypto/nacl/box+ golang.org/x/crypto/sha3 from crypto/internal/mlkem768+ golang.org/x/exp/maps from tailscale.com/util/syspolicy/internal/metrics+ - golang.org/x/net/bpf from github.com/mdlayher/netlink+ + L golang.org/x/net/bpf from github.com/mdlayher/netlink+ golang.org/x/net/dns/dnsmessage from net+ golang.org/x/net/http/httpguts from golang.org/x/net/http2+ golang.org/x/net/http/httpproxy from net/http+ golang.org/x/net/http2 from tailscale.com/cmd/tailscale/cli+ golang.org/x/net/http2/hpack from golang.org/x/net/http2+ - golang.org/x/net/icmp from tailscale.com/net/ping golang.org/x/net/idna from golang.org/x/net/http/httpguts+ - golang.org/x/net/ipv4 from golang.org/x/net/icmp+ - golang.org/x/net/ipv6 from golang.org/x/net/icmp+ golang.org/x/net/proxy from tailscale.com/net/netns D golang.org/x/net/route from net+ golang.org/x/oauth2 from golang.org/x/oauth2/clientcredentials @@ -185,7 +177,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep golang.org/x/text/transform from golang.org/x/text/secure/bidirule+ golang.org/x/text/unicode/bidi from golang.org/x/net/idna+ golang.org/x/text/unicode/norm from golang.org/x/net/idna - golang.org/x/time/rate from tailscale.com/cmd/tailscale/cli+ + golang.org/x/time/rate from tailscale.com/cmd/tailscale/cli archive/tar from tailscale.com/clientupdate bufio from compress/flate+ bytes from archive/tar+ @@ -229,7 +221,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep encoding/pem from crypto/tls+ encoding/xml from github.com/tailscale/goupnp+ errors from archive/tar+ - expvar from tailscale.com/derp+ + expvar from tailscale.com/health+ flag from github.com/peterbourgon/ff/v3+ fmt from archive/tar+ hash from compress/zlib+ diff --git a/cmd/tailscaled/depaware-minlinux.txt b/cmd/tailscaled/depaware-minlinux.txt index c579d626e..1331a1f79 100644 --- a/cmd/tailscaled/depaware-minlinux.txt +++ b/cmd/tailscaled/depaware-minlinux.txt @@ -81,7 +81,6 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de tailscale.com/net/netutil from tailscale.com/control/controlclient+ tailscale.com/net/packet from tailscale.com/net/packet/checksum+ tailscale.com/net/packet/checksum from tailscale.com/net/tstun - tailscale.com/net/ping from tailscale.com/net/netcheck+ tailscale.com/net/sockstats from tailscale.com/control/controlclient+ tailscale.com/net/stun from tailscale.com/net/netcheck+ L tailscale.com/net/tcpinfo from tailscale.com/derp @@ -189,7 +188,6 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de golang.org/x/net/http/httpproxy from net/http+ golang.org/x/net/http2 from tailscale.com/control/controlclient+ golang.org/x/net/http2/hpack from golang.org/x/net/http2+ - golang.org/x/net/icmp from tailscale.com/net/ping golang.org/x/net/idna from golang.org/x/net/http/httpguts+ golang.org/x/net/ipv4 from github.com/tailscale/wireguard-go/conn+ golang.org/x/net/ipv6 from github.com/tailscale/wireguard-go/conn+ diff --git a/net/netcheck/netcheck.go b/net/netcheck/netcheck.go index bab76e1c2..8c328075e 100644 --- a/net/netcheck/netcheck.go +++ b/net/netcheck/netcheck.go @@ -8,10 +8,8 @@ import ( "bufio" "cmp" "context" - "crypto/tls" "errors" "fmt" - "io" "log" "maps" "net" @@ -20,15 +18,12 @@ import ( "runtime" "sort" "sync" - "syscall" "time" - "tailscale.com/derp/derphttp" "tailscale.com/envknob" "tailscale.com/net/neterror" "tailscale.com/net/netmon" "tailscale.com/net/netns" - "tailscale.com/net/ping" "tailscale.com/net/sockstats" "tailscale.com/net/stun" "tailscale.com/syncs" @@ -924,65 +919,6 @@ func (c *Client) GetReport(ctx context.Context, dm *tailcfg.DERPMap, opts *GetRe rs.stopTimers() - // Try HTTPS and ICMP latency check if all STUN probes failed due to - // UDP presumably being blocked. - // TODO: this should be moved into the probePlan, using probeProto probeHTTPS. - if !rs.anyUDP() && ctx.Err() == nil { - var wg sync.WaitGroup - var need []*tailcfg.DERPRegion - for rid, reg := range dm.Regions { - if !rs.haveRegionLatency(rid) && regionHasDERPNode(reg) { - need = append(need, reg) - } - } - if len(need) > 0 { - if opts == nil || !opts.OnlyTCP443 { - // Kick off ICMP in parallel to HTTPS checks; we don't - // reuse the same WaitGroup for those probes because we - // need to close the underlying Pinger after a timeout - // or when all ICMP probes are done, regardless of - // whether the HTTPS probes have finished. - wg.Add(1) - go func() { - defer wg.Done() - if err := c.measureAllICMPLatency(ctx, rs, need); err != nil { - c.logf("[v1] measureAllICMPLatency: %v", err) - } - }() - } - wg.Add(len(need)) - c.logf("netcheck: UDP is blocked, trying HTTPS") - } - for _, reg := range need { - go func(reg *tailcfg.DERPRegion) { - defer wg.Done() - if d, ip, err := c.measureHTTPSLatency(ctx, reg); err != nil { - c.logf("[v1] netcheck: measuring HTTPS latency of %v (%d): %v", reg.RegionCode, reg.RegionID, err) - } else { - rs.mu.Lock() - if l, ok := rs.report.RegionLatency[reg.RegionID]; !ok { - mak.Set(&rs.report.RegionLatency, reg.RegionID, d) - } else if l >= d { - rs.report.RegionLatency[reg.RegionID] = d - } - // We set these IPv4 and IPv6 but they're not really used - // and we don't necessarily set them both. If UDP is blocked - // and both IPv4 and IPv6 are available over TCP, it's basically - // random which fields end up getting set here. - // Since they're not needed, that's fine for now. - if ip.Is4() { - rs.report.IPv4 = true - } - if ip.Is6() { - rs.report.IPv6 = true - } - rs.mu.Unlock() - } - }(reg) - } - wg.Wait() - } - // Wait for captive portal check before finishing the report. <-captivePortalDone @@ -1057,165 +993,6 @@ func (c *Client) runHTTPOnlyChecks(ctx context.Context, last *Report, rs *report return nil } -// measureHTTPSLatency measures HTTP request latency to the DERP region, but -// only returns success if an HTTPS request to the region succeeds. -func (c *Client) measureHTTPSLatency(ctx context.Context, reg *tailcfg.DERPRegion) (time.Duration, netip.Addr, error) { - metricHTTPSend.Add(1) - ctx, cancel := context.WithTimeout(ctx, httpsProbeTimeout) - defer cancel() - - var ip netip.Addr - - dc := derphttp.NewNetcheckClient(c.logf, c.NetMon) - defer dc.Close() - - // DialRegionTLS may dial multiple times if a node is not available, as such - // it does not have stable timing to measure. - tlsConn, tcpConn, node, err := dc.DialRegionTLS(ctx, reg) - if err != nil { - return 0, ip, err - } - defer tcpConn.Close() - - if ta, ok := tlsConn.RemoteAddr().(*net.TCPAddr); ok { - ip, _ = netip.AddrFromSlice(ta.IP) - ip = ip.Unmap() - } - if ip == (netip.Addr{}) { - return 0, ip, fmt.Errorf("no unexpected RemoteAddr %#v", tlsConn.RemoteAddr()) - } - - connc := make(chan *tls.Conn, 1) - connc <- tlsConn - - // make an HTTP request to measure, as this enables us to account for MITM - // overhead in e.g. corp environments that have HTTP MITM in front of DERP. - tr := &http.Transport{ - DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - return nil, errors.New("unexpected DialContext dial") - }, - DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) { - select { - case nc := <-connc: - return nc, nil - default: - return nil, errors.New("only one conn expected") - } - }, - } - hc := &http.Client{Transport: tr} - - // This is the request that will be measured, the request and response - // should be small enough to fit into a single packet each way unless the - // connection has already become unstable. - req, err := http.NewRequestWithContext(ctx, "GET", "https://"+node.HostName+"/derp/latency-check", nil) - if err != nil { - return 0, ip, err - } - - startTime := c.timeNow() - resp, err := hc.Do(req) - reqDur := c.timeNow().Sub(startTime) - if err != nil { - return 0, ip, err - } - defer resp.Body.Close() - - // DERPs should give us a nominal status code, so anything else is probably - // an access denied by a MITM proxy (or at the very least a signal not to - // trust this latency check). - if resp.StatusCode > 299 { - return 0, ip, fmt.Errorf("unexpected status code: %d (%s)", resp.StatusCode, resp.Status) - } - - _, err = io.Copy(io.Discard, io.LimitReader(resp.Body, 8<<10)) - if err != nil { - return 0, ip, err - } - - // return the connection duration, not the request duration, as this is the - // best approximation of the RTT latency to the node. Note that the - // connection setup performs happy-eyeballs and TLS so there are additional - // overheads. - return reqDur, ip, nil -} - -func (c *Client) measureAllICMPLatency(ctx context.Context, rs *reportState, need []*tailcfg.DERPRegion) error { - if len(need) == 0 { - return nil - } - ctx, done := context.WithTimeout(ctx, icmpProbeTimeout) - defer done() - - p := ping.New(ctx, c.logf, netns.Listener(c.logf, c.NetMon)) - defer p.Close() - - c.logf("UDP is blocked, trying ICMP") - - var wg sync.WaitGroup - wg.Add(len(need)) - for _, reg := range need { - go func(reg *tailcfg.DERPRegion) { - defer wg.Done() - if d, ok, err := c.measureICMPLatency(ctx, reg, p); err != nil { - c.logf("[v1] measuring ICMP latency of %v (%d): %v", reg.RegionCode, reg.RegionID, err) - } else if ok { - c.logf("[v1] ICMP latency of %v (%d): %v", reg.RegionCode, reg.RegionID, d) - rs.mu.Lock() - if l, ok := rs.report.RegionLatency[reg.RegionID]; !ok { - mak.Set(&rs.report.RegionLatency, reg.RegionID, d) - } else if l >= d { - rs.report.RegionLatency[reg.RegionID] = d - } - - // We only send IPv4 ICMP right now - rs.report.IPv4 = true - rs.report.ICMPv4 = true - - rs.mu.Unlock() - } - }(reg) - } - - wg.Wait() - return nil -} - -func (c *Client) measureICMPLatency(ctx context.Context, reg *tailcfg.DERPRegion, p *ping.Pinger) (_ time.Duration, ok bool, err error) { - if len(reg.Nodes) == 0 { - return 0, false, fmt.Errorf("no nodes for region %d (%v)", reg.RegionID, reg.RegionCode) - } - - // Try pinging the first node in the region - node := reg.Nodes[0] - - if node.STUNPort < 0 { - // If STUN is disabled on a node, interpret that as meaning don't measure latency. - return 0, false, nil - } - const unusedPort = 0 - stunAddrPort, ok := c.nodeAddrPort(ctx, node, unusedPort, probeIPv4) - if !ok { - return 0, false, fmt.Errorf("no address for node %v (v4-for-icmp)", node.Name) - } - ip := stunAddrPort.Addr() - addr := &net.IPAddr{ - IP: net.IP(ip.AsSlice()), - Zone: ip.Zone(), - } - - // Use the unique node.Name field as the packet data to reduce the - // likelihood that we get a mismatched echo response. - d, err := p.Send(ctx, addr, []byte(node.Name)) - if err != nil { - if errors.Is(err, syscall.EPERM) { - return 0, false, nil - } - return 0, false, err - } - return d, true, nil -} - func (c *Client) logConciseReport(r *Report, dm *tailcfg.DERPMap) { c.logf("[v1] report: %v", logger.ArgWriter(func(w *bufio.Writer) { fmt.Fprintf(w, "udp=%v", r.UDP) diff --git a/wgengine/magicsock/endpoint.go b/wgengine/magicsock/endpoint.go index 4aa32ab35..4d0278f9a 100644 --- a/wgengine/magicsock/endpoint.go +++ b/wgengine/magicsock/endpoint.go @@ -5,14 +5,12 @@ package magicsock import ( "bufio" - "context" "encoding/binary" "errors" "fmt" "iter" "math" "math/rand/v2" - "net" "net/netip" "reflect" "runtime" @@ -926,7 +924,7 @@ func (de *endpoint) send(buffs [][]byte) error { if de.isWireguardOnly { if startWGPing { - de.sendWireGuardOnlyPingsLocked(now) + // lanscaping de.sendWireGuardOnlyPingsLocked(now) } } else if !udpAddr.IsValid() || now.After(de.trustBestAddrUntil) { de.sendDiscoPingsLocked(now, true) @@ -1224,75 +1222,6 @@ func (de *endpoint) sendDiscoPingsLocked(now mono.Time, sendCallMeMaybe bool) { } } -// sendWireGuardOnlyPingsLocked evaluates all available addresses for -// a WireGuard only endpoint and initates an ICMP ping for useable -// addresses. -func (de *endpoint) sendWireGuardOnlyPingsLocked(now mono.Time) { - if runtime.GOOS == "js" { - return - } - - // Normally we only send pings at a low rate as the decision to start - // sending a ping sets bestAddrAtUntil with a reasonable time to keep trying - // that address, however, if that code changed we may want to be sure that - // we don't ever send excessive pings to avoid impact to the client/user. - if !now.After(de.lastFullPing.Add(10 * time.Second)) { - return - } - de.lastFullPing = now - - for ipp := range de.endpointState { - if ipp.Addr().Is4() && de.c.noV4.Load() { - continue - } - if ipp.Addr().Is6() && de.c.noV6.Load() { - continue - } - - go de.sendWireGuardOnlyPing(ipp, now) - } -} - -// sendWireGuardOnlyPing sends a ICMP ping to a WireGuard only address to -// discover the latency. -func (de *endpoint) sendWireGuardOnlyPing(ipp netip.AddrPort, now mono.Time) { - ctx, cancel := context.WithTimeout(de.c.connCtx, 5*time.Second) - defer cancel() - - de.setLastPing(ipp, now) - - addr := &net.IPAddr{ - IP: net.IP(ipp.Addr().AsSlice()), - Zone: ipp.Addr().Zone(), - } - - p := de.c.getPinger() - if p == nil { - de.c.logf("[v2] magicsock: sendWireGuardOnlyPingLocked: pinger is nil") - return - } - - latency, err := p.Send(ctx, addr, nil) - if err != nil { - de.c.logf("[v2] magicsock: sendWireGuardOnlyPingLocked: %s", err) - return - } - - de.mu.Lock() - defer de.mu.Unlock() - - state, ok := de.endpointState[ipp] - if !ok { - return - } - state.addPongReplyLocked(pongReply{ - latency: latency, - pongAt: now, - from: ipp, - pongSrc: netip.AddrPort{}, // We don't know this. - }) -} - // setLastPing sets lastPing on the endpointState to now. func (de *endpoint) setLastPing(ipp netip.AddrPort, now mono.Time) { de.mu.Lock() diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index b5ef762b0..96c8c036c 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -38,7 +38,6 @@ import ( "tailscale.com/net/netmon" "tailscale.com/net/netns" "tailscale.com/net/packet" - "tailscale.com/net/ping" "tailscale.com/net/sockstats" "tailscale.com/net/stun" "tailscale.com/net/tstun" @@ -47,7 +46,6 @@ import ( "tailscale.com/tstime" "tailscale.com/tstime/mono" "tailscale.com/types/key" - "tailscale.com/types/lazy" "tailscale.com/types/logger" "tailscale.com/types/netmap" "tailscale.com/types/nettype" @@ -343,9 +341,6 @@ type Conn struct { // peer. It's only used to quiet logging, so we only log on change. peerLastDerp map[key.NodePublic]int - // wgPinger is the WireGuard only pinger used for latency measurements. - wgPinger lazy.SyncValue[*ping.Pinger] - // onPortUpdate is called with the new port when magicsock rebinds to // a new port. onPortUpdate func(port uint16, network string) @@ -2412,10 +2407,6 @@ func (c *Conn) Close() error { c.muCond.Wait() } - if pinger := c.getPinger(); pinger != nil { - pinger.Close() - } - deregisterMetrics(c.metrics) return nil @@ -2859,14 +2850,6 @@ var ( // a endpoint's endpoints are being updated from a new network map. const indexSentinelDeleted = -1 -// getPinger lazily instantiates a pinger and returns it, if it was -// already instantiated it returns the existing one. -func (c *Conn) getPinger() *ping.Pinger { - return c.wgPinger.Get(func() *ping.Pinger { - return ping.New(c.connCtx, c.dlogf, netns.Listener(c.logf, c.netMon)) - }) -} - // DebugPickNewDERP picks a new DERP random home temporarily (even if just for // seconds) and reports it to control. It exists to test DERP home changes and // netmap deltas, etc. It serves no useful user purpose.