Files
tailscale/net/tstun/connreject.go
James Tucker 85c1efb466 net/{connreject,tstun},feature/connreject,wgengine,client/local: add connection-rejection diagnostics
Adds an opt-in, in-memory aggregator of recent connection-rejection
events (TSMP rejects received from peers, outbound TSMP rejects we emit
on ACL-blocked inbound flows, and pendopen timeouts) keyed by
(direction, proto, peer-address, reason). The aggregated data is exposed
over a new debug-rejects LocalAPI endpoint and a GET /debug/rejects c2n
endpoint, intended for future GUI/CLI consumption when diagnosing why a
connection failed.

Architecture:
 - net/connreject holds the data types and a per-LocalBackend
   Aggregator (LRU-bounded, default 256 entries on desktop / 32 on
   mobile, per direction).
 - feature/connreject is a self-registering ipnext.Extension that owns
   one Aggregator per LocalBackend, installs note callbacks on the
   tundev and engine, subscribes to OnSelfChange to flip the runtime
   gate, and serves the LocalAPI/c2n endpoints.
 - wgengine.Engine and *tstun.Wrapper each gain a SetConnRejectNote
   setter; data-plane sites use a single atomic.Pointer load + nil
   check, so the cost when no consumer is installed is one MOV.

Gating:
 - Compile-time: ts_omit_connreject build tag (standard
   feature/buildfeatures + condregister plumbing). Trims ~41 KB.
 - Runtime: nodecap.ConnReject node attribute, off by default
   at the control plane. May be removed once the feature is enabled
   by default.

Updates CapabilityVersion to 146 (clients understand nodecap.ConnReject
and can serve GET /debug/rejects).

Adds Proto/Src/Dst accessors on flowtrack.Tuple (used by pendopen to
construct events without exposing the tuple's internals to the
aggregator).

Updates #1094
Updates #14802

Change-Id: I83e8f24a7e66fa2d158d128bd25fbe851134941b
Signed-off-by: James Tucker <[email protected]>
2026-09-08 16:22:43 -07:00

53 lines
1.6 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build !ts_omit_connreject
package tstun
import (
"tailscale.com/net/connreject"
"tailscale.com/net/packet"
"tailscale.com/syncs"
)
// connRejectState holds the connreject feature's per-Wrapper storage.
// Its definition only references [tailscale.com/net/connreject] when
// the feature is built in; see connreject_stub.go for the no-op
// counterpart used under -tags ts_omit_connreject.
type connRejectState struct {
cb syncs.AtomicValue[func(connreject.Event)]
}
// SetConnRejectCallback installs a callback that is invoked when the
// Wrapper emits an outbound TSMP reject for an inbound peer connection
// that was dropped by the packet filter. The callback receives a fully
// populated [connreject.Event] of [connreject.Incoming] direction.
//
// A nil fn unsets any previously installed callback.
func (t *Wrapper) SetConnRejectCallback(fn func(connreject.Event)) {
t.connReject.cb.Store(fn)
}
// notifyConnRejectTSMPSent delivers an Incoming-direction event to the
// installed callback, if any, derived from a TSMP reject we just
// injected outbound.
func (t *Wrapper) notifyConnRejectTSMPSent(rj packet.TailscaleRejectedHeader) {
fn := t.connReject.cb.Load()
if fn == nil {
return
}
reason := connreject.ReasonACL
if rj.Reason == packet.RejectedDueToShieldsUp {
reason = connreject.ReasonShields
}
fn(connreject.Event{
Direction: connreject.Incoming,
Proto: rj.Proto,
Src: rj.Src,
Dst: rj.Dst,
Reason: reason,
Source: connreject.SourceTSMPSent,
})
}