diff --git a/feature/conn25/conn25.go b/feature/conn25/conn25.go index 4dd12957d..221367331 100644 --- a/feature/conn25/conn25.go +++ b/feature/conn25/conn25.go @@ -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 diff --git a/net/tstun/wrap.go b/net/tstun/wrap.go index 29dd805d5..2be51f756 100644 --- a/net/tstun/wrap.go +++ b/net/tstun/wrap.go @@ -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 } diff --git a/net/tstun/wrap_test.go b/net/tstun/wrap_test.go index 570c5d2d3..5270386c9 100644 --- a/net/tstun/wrap_test.go +++ b/net/tstun/wrap_test.go @@ -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