Skip to content

Commit e1f9d8a

Browse files
Ofer Daganvictorjulien
authored andcommitted
pcap: refactor delete-when-done to support non-alerts
Refactor pcap file deletion to use a single delete-when-done option with three values instead of separate boolean options: - false (default): No deletion - true: Always delete files - "non-alerts": Delete only files with no alerts Also account for alerts produced by pseudo packets (flow timeout / shutdown flush): - Introduce small capture hooks and invoke on pseudo-packet creation so the capture layer can retain references and observe alerts emitted after the last live packet - Call the hook from both TmThreadDisableReceiveThreads and TmThreadDrainPacketThreads Key changes: - Replace should_delete/delete_non_alerts_only bools with enum - Move alert counter from global to per-file PcapFileFileVars - Relocate alert counting from PacketAlertFinalize to pcap module - Ensure thread safety for both single and continuous pcap modes - Add unit tests for configuration parsing and pseudo-packet alert path The --pcap-file-delete command line option overrides YAML config and forces "always delete" mode for backward compatibility. Documentation updated to reflect the new three-value configuration. Fixes OISF#7786
1 parent 539e4ee commit e1f9d8a

13 files changed

Lines changed: 993 additions & 30 deletions

doc/userguide/capture-hardware/pcap-file.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Configuration
1515
checksum-checks: auto
1616
# buffer-size: 128 KiB
1717
# tenant-id: none
18+
# Applies to file and directory. Options: false (no deletion), true (always delete),
19+
# "non-alerts" (delete only files with no alerts)
1820
# delete-when-done: false
1921
# recursive: false
2022
# continuous: false
@@ -85,9 +87,22 @@ Other options
8587

8688
**delete-when-done**
8789

88-
- If ``true``, Suricata deletes the PCAP file after processing.
89-
- The command-line option is
90-
:ref:`--pcap-file-delete <cmdline-option-pcap-file-delete>`
90+
Controls when PCAP files are deleted after processing. Three values are supported:
91+
92+
- ``false`` (default): Files are never deleted
93+
- ``true``: Files are always deleted after processing
94+
- ``"non-alerts"``: Files are deleted only if they didn't generate any alerts
95+
96+
.. note::
97+
98+
The command-line option :ref:`--pcap-file-delete <cmdline-option-pcap-file-delete>`
99+
overrides this configuration and forces "always delete" mode (``true``).
100+
101+
.. warning::
102+
103+
When using ``"non-alerts"`` mode, file deletion is deferred until thread
104+
cleanup to ensure alert counts are finalized. This may delay deletion
105+
compared to other modes.
91106

92107
**BPF filter**
93108

doc/userguide/partials/options.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,23 @@
8080

8181
.. option:: --pcap-file-delete
8282

83-
Used with the -r option to indicate that the mode should delete pcap files
84-
after they have been processed. This is useful with pcap-file-continuous to
85-
continuously feed files to a directory and have them cleaned up when done. If
86-
this option is not set, pcap files will not be deleted after processing.
83+
Used with the -r option to force deletion of pcap files after they have been
84+
processed. This is useful with pcap-file-continuous to continuously feed files
85+
to a directory and have them cleaned up when done.
86+
87+
**command-line vs Configuration**: This command-line option overrides the
88+
``pcap-file.delete-when-done`` configuration option in ``suricata.yaml`` and
89+
forces "always delete" mode (equivalent to ``delete-when-done: true``).
90+
91+
**For more control**, use the ``pcap-file.delete-when-done`` configuration
92+
option instead, which supports three values:
93+
94+
- ``false`` (default): No files are deleted
95+
- ``true``: All files are deleted after processing
96+
- ``"non-alerts"``: Only files that generated no alerts are deleted
97+
98+
If neither ``--pcap-file-delete`` nor ``delete-when-done`` is configured,
99+
pcap files will not be deleted after processing.
87100

88101
.. _cmdline-option-pcap-file-buffer-size:
89102

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ noinst_HEADERS = \
5151
app-layer.h \
5252
bindgen.h \
5353
build-info.h \
54+
capture-hooks.h \
5455
conf-yaml-loader.h \
5556
conf.h \
5657
counters.h \
@@ -636,6 +637,7 @@ libsuricata_c_a_SOURCES = \
636637
app-layer-ssl.c \
637638
app-layer-tftp.c \
638639
app-layer.c \
640+
capture-hooks.c \
639641
conf-yaml-loader.c \
640642
conf.c \
641643
counters.c \

src/capture-hooks.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Copyright (C) 2007-2026 Open Information Security Foundation
2+
*
3+
* You can copy, redistribute or modify this Program under the terms of
4+
* the GNU General Public License version 2 as published by the Free
5+
* Software Foundation.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* version 2 along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15+
* 02110-1301, USA.
16+
*/
17+
18+
/**
19+
* \file
20+
* Lightweight indirection layer for capture-related callbacks.
21+
*
22+
* This module lets the capture implementation register small hooks that the
23+
* generic engine can invoke without hard dependencies. Two hooks are used:
24+
* - on-alerts: invoked when a packet produced alerts so capture can update
25+
* per-input stats (e.g., deciding if a pcap should be deleted or kept).
26+
* - on-pseudo-created: invoked when the engine creates pseudo packets (e.g.,
27+
* flow timeout or shutdown flush). This allows capture to retain references
28+
* or track alert outcomes tied to those pseudo packets.
29+
*/
30+
31+
#include "suricata-common.h"
32+
#include "capture-hooks.h"
33+
34+
static CaptureOnPacketWithAlertsHook g_on_alerts_hook = NULL;
35+
static CaptureOnPseudoPacketCreatedHook g_on_pseudo_created_hook = NULL;
36+
37+
void CaptureHooksSet(
38+
CaptureOnPacketWithAlertsHook OnAlerts, CaptureOnPseudoPacketCreatedHook OnPseudoCreated)
39+
{
40+
/* Allow re-setting the same hooks (e.g. during pcap file reload), but BUG_ON
41+
* if overwriting with different ones without clearing. */
42+
if (g_on_alerts_hook != NULL) {
43+
BUG_ON(g_on_alerts_hook != OnAlerts);
44+
}
45+
if (g_on_pseudo_created_hook != NULL) {
46+
BUG_ON(g_on_pseudo_created_hook != OnPseudoCreated);
47+
}
48+
49+
g_on_alerts_hook = OnAlerts;
50+
g_on_pseudo_created_hook = OnPseudoCreated;
51+
}
52+
53+
void CaptureHooksOnPacketWithAlerts(const Packet *p)
54+
{
55+
if (g_on_alerts_hook != NULL) {
56+
g_on_alerts_hook(p);
57+
}
58+
}
59+
60+
void CaptureHooksOnPseudoPacketCreated(Packet *p)
61+
{
62+
if (g_on_pseudo_created_hook != NULL) {
63+
g_on_pseudo_created_hook(p);
64+
}
65+
}

src/capture-hooks.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright (C) 2007-2026 Open Information Security Foundation
2+
*
3+
* You can copy, redistribute or modify this Program under the terms of
4+
* the GNU General Public License version 2 as published by the Free
5+
* Software Foundation.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* version 2 along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15+
* 02110-1301, USA.
16+
*/
17+
18+
/**
19+
* \file capture-hooks.h
20+
* Small hook interface for capture modules to react to events in the
21+
* generic engine without creating circular dependencies.
22+
*/
23+
24+
#ifndef SURICATA_CAPTURE_HOOKS_H
25+
#define SURICATA_CAPTURE_HOOKS_H
26+
27+
#include "suricata-common.h"
28+
29+
struct Packet_;
30+
typedef struct Packet_ Packet;
31+
32+
typedef void (*CaptureOnPacketWithAlertsHook)(const Packet *p);
33+
typedef void (*CaptureOnPseudoPacketCreatedHook)(Packet *p);
34+
35+
/* Register/clear hooks (called by capture implementations) */
36+
void CaptureHooksSet(
37+
CaptureOnPacketWithAlertsHook OnAlerts, CaptureOnPseudoPacketCreatedHook OnPseudoCreated);
38+
39+
/* Invoke hooks (called from generic code, safe if unset) */
40+
void CaptureHooksOnPacketWithAlerts(const Packet *p);
41+
void CaptureHooksOnPseudoPacketCreated(Packet *p);
42+
43+
#endif /* SURICATA_CAPTURE_HOOKS_H */

src/detect-engine-alert.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "util-validate.h"
3838

3939
#include "action-globals.h"
40+
#include "capture-hooks.h"
4041

4142
/** tag signature we use for tag alerts */
4243
static Signature g_tag_signature;
@@ -582,6 +583,12 @@ static inline void PacketAlertFinalizeProcessQueue(
582583
p->flags |= PKT_FIRST_ALERTS;
583584
}
584585
}
586+
587+
/* Notify capture layer about packets with real alerts (not pass-only),
588+
* so capture impl can update per-capture context (e.g. pcap-file alert counts). */
589+
if (alerted) {
590+
CaptureHooksOnPacketWithAlerts(p);
591+
}
585592
}
586593

587594
/**

src/runmode-unittests.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
#include "decode-vntag.h"
117117
#include "decode-vxlan.h"
118118
#include "decode-pppoe.h"
119+
#include "source-pcap-file-helper.h"
119120

120121
#include "output-json-stats.h"
121122

@@ -212,6 +213,7 @@ static void RegisterUnittests(void)
212213
StreamingBufferRegisterTests();
213214
MacSetRegisterTests();
214215
FlowRateRegisterTests();
216+
SourcePcapFileHelperRegisterTests();
215217
#ifdef OS_WIN32
216218
Win32SyscallRegisterTests();
217219
#endif

0 commit comments

Comments
 (0)