Bug #9498
closedRFC: firewall: packets dropped by the engine are counted in firewall.accepted and ips.blocked
Description
Summary¶
In firewall mode, a packet that the engine itself drops, e.g. by the midstream exception policy or by the stream engine for an out-of-window segment, is counted in firewall.accepted and ips.blocked. Neither the firewall nor the IPS rules issued that drop, and it is counted this way also when the firewall ruleset accepts none of the traffic.
The packets are dropped either way. Only the counters are affected.
Background¶
While evaluating the backport of the DPDK segmented mbufs support (#6012, backport #9472), I ran DPDK IPS in firewall mode and saw firewall.accepted increase together with ips.blocked for packets that the capture method had dropped before any rule was evaluated. It was not the IPS that dropped them, and the firewall ruleset did not accept them.
Following that up, the same accounting applies to other drops that come from the engine and not from a firewall or IPS verdict, such as the midstream policy and out-of-window segments. Those are what this ticket is about.
Details¶
The documentation (doc/userguide/firewall/firewall-design.rst) describes the stats as two chained instances: the firewall runs first, a packet it accepts is counted in firewall.accepted, a packet it drops only in firewall.blocked and in no ips.* counter.
CaptureStatsUpdate() in src/decode.c derives the firewall verdict from the drop reason alone:
} else if (PacketCheckAction(p, ACTION_DROP)) {
if (VerdictByFirewall(p)) {
StatsCounterIncr(&tv->stats, s->counter_fw_blocked);
} else {
/* If a packet was dropped by IPS, it had to first be accepted by the firewall, to
* reach the IPS flow control */
StatsCounterIncr(&tv->stats, s->counter_fw_accepted);
StatsCounterIncr(&tv->stats, s->counter_ips_blocked);
}
VerdictByFirewall() is true only for drop_reason >= PKT_DROP_REASON_FW_RULES. The assumption in the comment holds for drops by IPS rules, which run after the firewall rules. It does not hold for the parts of the engine that run before the firewall packet:filter table: defrag, flow lookup, the stream engine and app-layer, with their exception policies.
From the code, such a drop keeps its non-firewall reason in two ways:
- Detection is skipped:
DetectFlow()returns for a flow withFLOW_ACTION_DROP, andDetectNoFlow()returns for a packet that is already dropped. - Detection runs, but
PacketDrop()keeps the first drop reason, so a later drop by the default packet policy does not change it.
Reproducer¶
The attached patch for suricata-verify adds two tests, each a test.yaml and a firewall.rules that reuse the pcaps of existing tests:
tests/firewall/firewall-stats-midstream-policy-no-accepttests/firewall/firewall-stats-stream-error-no-accept
The firewall ruleset accepts none of the traffic in the pcaps:
Both tests expect firewall.accepted: 0 and ips.blocked: 0, and fail on main at 321d52d6f and on 8.0.8-dev with the same numbers (3 packets per pcap):
| test | dropped by the engine | firewall.accepted | firewall.blocked | ips.blocked |
|---|---|---|---|---|
| midstream policy | 3 (stream_midstream 1, exception_policy_flow_drop 2) |
3 | 0 | 3 |
| stream error | 1 (stream_error 1) |
1 | 2 | 1 |
With stream.midstream-policy: ignore, the three packets of the first pcap are counted as expected: firewall.blocked: 3 (default_packet_policy), firewall.accepted: 0, ips.blocked: 0.
Not tested, expected from the code: defrag_memcap, flow_memcap, applayer_error, applayer_memcap, stream_memcap, stream_reassembly, stream_urgent.
Existing tests¶
The Suricata-Verify firewall tests that combine such drops with the firewall counters use rulesets that accept the dropped traffic, e.g. ruletype-firewall-39-pre-stream expects firewall.accepted: 12 for 12 packets dropped by the midstream policy. No existing test covers a ruleset that does not accept the dropped traffic.
Proposal¶
A packet dropped by the engine, and not by a firewall or IPS verdict, should not be counted in firewall.accepted, firewall.blocked, ips.accepted or ips.blocked. It should be counted in separate counters, e.g. in the decoder section.
For reference:
- The per-reason counters for these drops exist today as
ips.drop_reason.*(stream_midstream,stream_error,flow_memcap, ...). - Outside firewall mode, these drops are counted in
ips.blockedas well, so the proposal also concerns plain IPS mode. - The capture drops from the background above already have their own counters, e.g.
capture.dpdk.segmented_drops. - The tests above only assert what holds with either accounting: with a ruleset that accepts nothing,
firewall.acceptedandips.blockedstay 0.
Files