net/tstun, feature/conn25: make app connector hooks safe to set while running

feature/conn25 installs its tstun.Wrapper hooks from its ipnext
extension's Init, which runs from LocalBackend.Start. By then the
wireguard-go device's RoutineReadFromTUN goroutine is already reading
packets and calling filterPacketOutboundToWireGuard, which read the
plain PreFilterPacketOutboundToWireGuardAppConnectorIntercept field
with no synchronization. A packet from the OS TUN in that window was a
data race; under -race, tailscaled in tstest/integration died with
"WARNING: DATA RACE" (conn25.go installHooks write vs wrap.go:797
read), and the test then failed with "running tailscale status: ...
EOF". All six CI logs examined show this same race.

Make the three hooks conn25 sets after startup
(PreFilterPacketOutboundToWireGuardAppConnectorIntercept,
PostFilterPacketInboundFromWireGuardAppConnector and
OnUnmappedTransitIPMessage) syncs.AtomicValues.

Add TestSetAppConnectorHooksWhileRunning, which installs the hooks
while packets flow in both directions and reproduces the CI race at
the same wrap.go line.

flakestress -race TestSetAppConnectorHooksWhileRunning:
before: 12564/12564 runs failed; after: 0/1176 runs.

The integration flake was too rare to measure locally (a few failures a
week in CI). It was blamed on tstest/integration TestTUNMode,
TestMagicDNS and TestSplitDNS.

Fixes #22557

Change-Id: Iff618c18c11f84dcbae27805024e908403031a08
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick authored and Brad Fitzpatrick committed 2026-11-22 16:46:06 -08:00
1 parent 678b3be66b
commit af9bfa07c3
3 files changed
+64 -21

No files matched your search

+6 -6
View File
@@ -206,24 +206,24 @@ func (e *extension) installHooks(dph *datapathHandler) error {
// Intercept packets from the tun device and from WireGuard
// to perform DNAT and SNAT.
tun.PreFilterPacketOutboundToWireGuardAppConnectorIntercept = func(p *packet.Parsed, tun *tstun.Wrapper) filter.Response {
tun.PreFilterPacketOutboundToWireGuardAppConnectorIntercept.Store(func(p *packet.Parsed, tun *tstun.Wrapper) filter.Response {
if !e.conn25.isConfigured() {
return filter.Accept
}
return dph.HandlePacketFromTunDevice(p, tun)
}
tun.PostFilterPacketInboundFromWireGuardAppConnector = func(p *packet.Parsed, tun *tstun.Wrapper) filter.Response {
})
tun.PostFilterPacketInboundFromWireGuardAppConnector.Store(func(p *packet.Parsed, tun *tstun.Wrapper) filter.Response {
if !e.conn25.isConfigured() {
return filter.Accept
}
return dph.HandlePacketFromWireGuard(p, tun)
}
tun.OnUnmappedTransitIPMessage = func(pkt packet.TailscaleRejectedHeader) {
})
tun.OnUnmappedTransitIPMessage.Store(func(pkt packet.TailscaleRejectedHeader) {
if !e.conn25.isConfigured() {
return
}
e.conn25.client.resendTransitIPMapping(pkt.Dst.Addr())
}
})
// The profile state change hook needs to clear all active flows on a
// major change (eg. switching tailnets), so make that hook accessible
+14 -12
View File
@@ -157,7 +157,8 @@ type Wrapper struct {
PreFilterPacketInboundFromWireGuard FilterFunc
// PostFilterPacketInboundFromWireGuardAppConnector runs after the filter, but before PostFilterPacketInboundFromWireGuard.
// Non-app connector traffic is passed along. Invalid app connector traffic is dropped.
PostFilterPacketInboundFromWireGuardAppConnector FilterFunc
// It may be set while the Wrapper is running.
PostFilterPacketInboundFromWireGuardAppConnector syncs.AtomicValue[FilterFunc]
// PostFilterPacketInboundFromWireGuard is the inbound filter function that runs after the main filter.
PostFilterPacketInboundFromWireGuard GROFilterFunc
// PreFilterPacketOutboundToWireGuardNetstackIntercept is a filter function that runs before the main filter
@@ -172,8 +173,8 @@ type Wrapper struct {
PreFilterPacketOutboundToWireGuardEngineIntercept FilterFunc
// PreFilterPacketOutboundToWireGuardAppConnectorIntercept runs after PreFilterPacketOutboundToWireGuardEngineIntercept
// for app connector specific traffic. Non-app connector traffic is passed along. Invalid app connector traffic is
// dropped.
PreFilterPacketOutboundToWireGuardAppConnectorIntercept FilterFunc
// dropped. It may be set while the Wrapper is running.
PreFilterPacketOutboundToWireGuardAppConnectorIntercept syncs.AtomicValue[FilterFunc]
// PostFilterPacketOutboundToWireGuard is the outbound filter function that runs after the main filter.
PostFilterPacketOutboundToWireGuard FilterFunc
@@ -187,8 +188,9 @@ type Wrapper struct {
// OnUnmappedTransitIPMessage, if non-nil, is called when a TSMP message is
// received indicating that a packet was rejected by a connector due to a
// missing transit IP->real IP mapping.
OnUnmappedTransitIPMessage func(packet.TailscaleRejectedHeader)
// missing transit IP->real IP mapping. It may be set while the Wrapper
// is running.
OnUnmappedTransitIPMessage syncs.AtomicValue[func(packet.TailscaleRejectedHeader)]
// PeerAPIPort, if non-nil, returns the peerapi port that's
// running for the given IP address.
@@ -794,8 +796,8 @@ func (t *Wrapper) filterPacketOutboundToWireGuard(p *packet.Parsed, pc *peerConf
return res, gro
}
}
if t.PreFilterPacketOutboundToWireGuardAppConnectorIntercept != nil {
if res := t.PreFilterPacketOutboundToWireGuardAppConnectorIntercept(p, t); res.IsDrop() {
if f := t.PreFilterPacketOutboundToWireGuardAppConnectorIntercept.Load(); f != nil {
if res := f(p, t); res.IsDrop() {
// Handled by userspaceEngine's configured hook for Connectors 2025 app connectors.
return res, gro
}
@@ -1108,8 +1110,8 @@ func (t *Wrapper) injectedRead(res tunInjectedRead, slab []byte, packets []tun.R
//
// Cross-tailnet conn25 app connector connections are not supported,
// so at most one of this hook and the following pc.snat should modify the packet.
if t.PreFilterPacketOutboundToWireGuardAppConnectorIntercept != nil {
if r := t.PreFilterPacketOutboundToWireGuardAppConnectorIntercept(p, t); r.IsDrop() {
if f := t.PreFilterPacketOutboundToWireGuardAppConnectorIntercept.Load(); f != nil {
if r := f(p, t); r.IsDrop() {
metricPacketOut.Add(1)
metricPacketOutDrop.Add(1)
return 0, nil
@@ -1168,7 +1170,7 @@ func (t *Wrapper) filterPacketInboundFromWireGuard(p *packet.Parsed, captHook pa
}
} else if data, ok := p.AsTailscaleRejectedHeader(); ok {
if data.Reason == packet.RejectedDueToUnknownAppConnectorTransitIP {
if f := t.OnUnmappedTransitIPMessage; f != nil {
if f := t.OnUnmappedTransitIPMessage.Load(); f != nil {
f(data)
}
}
@@ -1254,8 +1256,8 @@ func (t *Wrapper) filterPacketInboundFromWireGuard(p *packet.Parsed, captHook pa
return filter.Drop, gro
}
if t.PostFilterPacketInboundFromWireGuardAppConnector != nil {
if res := t.PostFilterPacketInboundFromWireGuardAppConnector(p, t); res.IsDrop() {
if f := t.PostFilterPacketInboundFromWireGuardAppConnector.Load(); f != nil {
if res := f(p, t); res.IsDrop() {
// Handled by userspaceEngine's configured hook for Connectors 2025 app connectors.
return res, gro
}
+44 -3
View File
@@ -1087,7 +1087,7 @@ func TestInterceptOrdering(t *testing.T) {
numInboundIntercepts := uint8(3)
tun.PreFilterPacketInboundFromWireGuard = orderedFilterFn(1)
tun.PostFilterPacketInboundFromWireGuardAppConnector = orderedFilterFn(2)
tun.PostFilterPacketInboundFromWireGuardAppConnector.Store(orderedFilterFn(2))
tun.PostFilterPacketInboundFromWireGuard = ordereredGROFilterFn(3)
// Write the packet.
@@ -1106,7 +1106,7 @@ func TestInterceptOrdering(t *testing.T) {
seq = 0
tun.PreFilterPacketOutboundToWireGuardNetstackIntercept = ordereredGROFilterFn(1)
tun.PreFilterPacketOutboundToWireGuardEngineIntercept = orderedFilterFn(2)
tun.PreFilterPacketOutboundToWireGuardAppConnectorIntercept = orderedFilterFn(3)
tun.PreFilterPacketOutboundToWireGuardAppConnectorIntercept.Store(orderedFilterFn(3))
tun.PostFilterPacketOutboundToWireGuard = orderedFilterFn(4)
// Read the packet. chtun.Outbound is unbuffered, and won't be drained until
@@ -1131,7 +1131,7 @@ func TestInjectedReadCallsAppConnectorHook(t *testing.T) {
bus := eventbustest.NewBus(t)
_, tun := newFakeTUN(t.Logf, bus, false)
tun.PreFilterPacketOutboundToWireGuardAppConnectorIntercept = hook
tun.PreFilterPacketOutboundToWireGuardAppConnectorIntercept.Store(hook)
tun.Start()
defer tun.Close()
@@ -1153,6 +1153,47 @@ func TestInjectedReadCallsAppConnectorHook(t *testing.T) {
}
}
// TestSetAppConnectorHooksWhileRunning verifies that the app connector
// hooks can be installed while packets are flowing, as feature/conn25
// does from LocalBackend.Start after the wireguard-go device is running.
func TestSetAppConnectorHooksWhileRunning(t *testing.T) {
bus := eventbustest.NewBus(t)
chtun, tun := newChannelTUN(t.Logf, bus, true)
defer tun.Close()
const n = 10
go func() {
for range n {
chtun.Outbound <- udp4("1.2.3.4", "5.6.7.8", 98, 98)
}
}()
go func() {
for range n {
<-chtun.Inbound
}
}()
readDone := make(chan struct{})
go func() {
defer close(readDone)
tun.ForTest().ReadN(t.Context(), n)
}()
writeDone := make(chan struct{})
go func() {
defer close(writeDone)
for range n {
tun.Write([][]byte{udp4("5.6.7.8", "1.2.3.4", 89, 89)}, 0)
}
}()
accept := func(*packet.Parsed, *Wrapper) filter.Response { return filter.Accept }
tun.PreFilterPacketOutboundToWireGuardAppConnectorIntercept.Store(accept)
tun.PostFilterPacketInboundFromWireGuardAppConnector.Store(accept)
tun.OnUnmappedTransitIPMessage.Store(func(packet.TailscaleRejectedHeader) {})
<-readDone
<-writeDone
}
// Tests SetPeerRoutes's fast path: unchanged inputs (including the
// nil-table case) must not replace the stored config, relying on the
// routes table's immutable-snapshot pointer identity, so callers can