diff --git a/net/tstun/connreject_test.go b/net/tstun/connreject_test.go index 18f800e2c..f5a234b0c 100644 --- a/net/tstun/connreject_test.go +++ b/net/tstun/connreject_test.go @@ -6,6 +6,7 @@ package tstun import ( + "expvar" "testing" "go4.org/netipx" @@ -14,6 +15,7 @@ import ( "tailscale.com/types/ipproto" "tailscale.com/types/logger" "tailscale.com/util/eventbus/eventbustest" + "tailscale.com/util/usermetric" "tailscale.com/wgengine/filter" ) @@ -158,3 +160,58 @@ func TestOutboundTSMPNotRecordedWhenDisabled(t *testing.T) { t.Errorf("got %d events, want 0 when disableTSMPRejected was set", len(events)) } } + +// TestRejectInboundDueToACLRecordsRejection verifies that a post-filter +// hook reporting a policy drop gets the same accounting and the same outbound +// TSMP as a drop by the filter itself. +func TestRejectInboundDueToACLRecordsRejection(t *testing.T) { + t.Parallel() + bus := eventbustest.NewBus(t) + _, w := newFakeTUN(t.Logf, bus, false) + defer w.Close() + + var events []connreject.Event + w.SetConnRejectCallback(func(e connreject.Event) { events = append(events, e) }) + + // Drain the outbound queue so InjectOutbound doesn't block. + go func() { + slab, packets := getSinglePacketReadArgs() + for { + if _, err := w.injectionQueue.Read(slab, packets); err != nil { + return + } + } + }() + + pkt := tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60000) + p := new(packet.Parsed) + p.Decode(pkt) + + w.RejectInboundDueToACL(p, packet.RejectedDueToACLs) + + m, ok := w.metrics.inboundDroppedPacketsTotal.Get(usermetric.DropLabels{ + Reason: usermetric.ReasonACL, + }).(*expvar.Int) + if !ok { + t.Fatal("inbound ACL drop metric missing") + } + if got := m.Value(); got != 1 { + t.Errorf("inbound ACL drops = %d, want 1", got) + } + + if len(events) != 1 { + t.Fatalf("got %d events, want 1", len(events)) + } + got := events[0] + if got.Direction != connreject.Incoming { + t.Errorf("Direction = %v, want Incoming", got.Direction) + } + if got.Source != connreject.SourceTSMPSent { + t.Errorf("Source = %v, want tsmp_sent", got.Source) + } + // A hook has made its own decision, so the rejection is always reported as + // being due to the ACLs rather than to shields up. + if got.Reason != connreject.ReasonACL { + t.Errorf("Reason = %q, want %q", got.Reason, connreject.ReasonACL) + } +} diff --git a/net/tstun/wrap.go b/net/tstun/wrap.go index 2be51f756..831916303 100644 --- a/net/tstun/wrap.go +++ b/net/tstun/wrap.go @@ -1225,34 +1225,11 @@ func (t *Wrapper) filterPacketInboundFromWireGuard(p *packet.Parsed, captHook pa } if outcome != filter.Accept { - metricPacketInDropFilter.Add(1) - t.metrics.inboundDroppedPacketsTotal.Add(usermetric.DropLabels{ - Reason: usermetric.ReasonACL, - }, 1) - - // Tell them, via TSMP, we're dropping them due to the ACL. - // Their host networking stack can translate this into ICMP - // or whatnot as required. But notably, their GUI or tailscale CLI - // can show them a rejection history with reasons. - if p.IPVersion == 4 && p.IPProto == ipproto.TCP && p.TCPFlags&packet.TCPSyn != 0 && !t.disableTSMPRejected { - rj := packet.TailscaleRejectedHeader{ - IPSrc: p.Dst.Addr(), - IPDst: p.Src.Addr(), - Src: p.Src, - Dst: p.Dst, - Proto: p.IPProto, - Reason: packet.RejectedDueToACLs, - } - if filt.ShieldsUp() { - rj.Reason = packet.RejectedDueToShieldsUp - } - t.TryInjectOutbound(packet.Generate(rj, nil)) - - // TODO(bradfitz): also send a TCP RST, after the TSMP message. - - t.notifyConnRejectTSMPSent(rj) + reason := packet.RejectedDueToACLs + if filt.ShieldsUp() { + reason = packet.RejectedDueToShieldsUp } - + t.RejectInboundDueToACL(p, reason) return filter.Drop, gro } @@ -1274,6 +1251,35 @@ func (t *Wrapper) filterPacketInboundFromWireGuard(p *packet.Parsed, captHook pa return filter.Accept, gro } +// RejectInboundDueToACL counts an inbound policy drop of p and, where the sender can +// be told about it, tells them. reason is how the rejection is described to the sender. +func (t *Wrapper) RejectInboundDueToACL(p *packet.Parsed, reason packet.TailscaleRejectReason) { + metricPacketInDropFilter.Add(1) + t.metrics.inboundDroppedPacketsTotal.Add(usermetric.DropLabels{ + Reason: usermetric.ReasonACL, + }, 1) + + // Tell them, via TSMP, we're dropping them due to the ACL. + // Their host networking stack can translate this into ICMP + // or whatnot as required. But notably, their GUI or tailscale CLI + // can show them a rejection history with reasons. + if p.IPVersion == 4 && p.IPProto == ipproto.TCP && p.TCPFlags&packet.TCPSyn != 0 && !t.disableTSMPRejected { + rj := packet.TailscaleRejectedHeader{ + IPSrc: p.Dst.Addr(), + IPDst: p.Src.Addr(), + Src: p.Src, + Dst: p.Dst, + Proto: p.IPProto, + Reason: reason, + } + t.TryInjectOutbound(packet.Generate(rj, nil)) + + // TODO(bradfitz): also send a TCP RST, after the TSMP message. + + t.notifyConnRejectTSMPSent(rj) + } +} + // Write accepts incoming packets. It is equivalent to [Wrapper.WriteTo] // with a zero flow. func (t *Wrapper) Write(buffs [][]byte, offset int) (int, error) { diff --git a/net/tstun/wrap_test.go b/net/tstun/wrap_test.go index 5270386c9..e9eded308 100644 --- a/net/tstun/wrap_test.go +++ b/net/tstun/wrap_test.go @@ -1620,3 +1620,75 @@ func TestCloseIdempotent(t *testing.T) { wg.Wait() w.Close() // and once more, serially } + +// TestRejectInboundDueToACL checks that a policy drop is counted against the +// ACL and sends the TSMP rejection exactly where the filter would have: for an +// IPv4 TCP SYN, and not otherwise. +func TestRejectInboundDueToACL(t *testing.T) { + bus := eventbustest.NewBus(t) + _, w := newFakeTUN(t.Logf, bus, false) + defer w.Close() + w.Start() + + aclDrops := func() int64 { + if m, ok := w.metrics.inboundDroppedPacketsTotal.Get( + usermetric.DropLabels{ + Reason: usermetric.ReasonACL, + }, + ).(*expvar.Int); ok { + return m.Value() + } + return 0 + } + readInjected := func() []byte { + t.Helper() + slab, packets := getSinglePacketReadArgs() + n, err := w.injectionQueue.Read(slab, packets) + if err != nil || n != 1 { + t.Fatalf("reading injected packet: n=%d, err=%v", n, err) + } + return slab[packets[0].Offset : packets[0].Offset+packets[0].Size] + } + // The injection queue is FIFO, so a sentinel queued after a call is read + // next exactly when the call injected nothing. + sentinel := udp4("100.64.1.2", "1.2.3.4", 1, 1) + checkNothingInjected := func() { + t.Helper() + if err := w.TryInjectOutbound(sentinel); err != nil { + t.Fatal(err) + } + if got := readInjected(); !bytes.Equal(got, sentinel) { + t.Errorf("unexpected injected packet %x", got) + } + } + + filterDropsBefore := metricPacketInDropFilter.Value() + + syn := new(packet.Parsed) + syn.Decode(tcp4syn("1.2.3.4", "100.64.1.2", 1234, 60000)) + w.RejectInboundDueToACL(syn, packet.RejectedDueToACLs) + want := packet.Generate(packet.TailscaleRejectedHeader{ + IPSrc: syn.Dst.Addr(), + IPDst: syn.Src.Addr(), + Src: syn.Src, + Dst: syn.Dst, + Proto: ipproto.TCP, + Reason: packet.RejectedDueToACLs, + }, nil) + if got := readInjected(); !bytes.Equal(got, want) { + t.Errorf("injected packet = %x, want TSMP rejection %x", got, want) + } + checkNothingInjected() + + udp := new(packet.Parsed) + udp.Decode(udp4("1.2.3.4", "100.64.1.2", 1234, 60000)) + w.RejectInboundDueToACL(udp, packet.RejectedDueToACLs) + checkNothingInjected() + + if got := aclDrops(); got != 2 { + t.Errorf("inbound ACL drops = %d, want 2", got) + } + if got := metricPacketInDropFilter.Value() - filterDropsBefore; got != 2 { + t.Errorf("metricPacketInDropFilter increased by %d, want 2", got) + } +}