mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-06 23:36:34 +09:00
net/tstun: let a post-filter hook report a policy drop as the filter would
A hook that runs after the inbound packet filter can drop a packet the filter accepted, and when it does so for policy reasons the drop is invisible. The filter's accounting only covers its own verdicts, so the usermetric and the clientmetric go uncounted, and the sender gets no TSMP telling it why its connection went nowhere. Factor the bookkeeping filterPacketInboundFromWireGuard already did for its own drops into RejectInboundDueToACL. The caller of this is the conn2025 datapath, which decides whether a peer's app grant permits a packet to a transit IP. Updates https://github.com/tailscale/corp/issues/49815 Change-Id: I3a05ff25161fbddd7f52b44004b0466cc58268eb Signed-off-by: Mazdak Nasab <mazdak.nasab@gmail.com>
This commit is contained in:
1 parent
83c76880e1
commit
ba4feb91db
3 files changed
+162
-27
No files matched your search
@@ -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)
|
||||
}
|
||||
}
|
||||
+33
-27
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user