Go to the documentation of this file.
41 static void PcapFileCallbackLoop(
char *user,
struct pcap_pkthdr *h, u_char *pkt);
43 static void PcapFileReleasePacket(
Packet *p)
98 if (pcap_current_pfv == pfv) {
99 pcap_current_pfv = NULL;
104 void PcapFileCallbackLoop(
char *user,
struct pcap_pkthdr *h, u_char *pkt)
109 SCLogNotice(
"skipping packet %" PRIu64, g_eps_pcap_packet_loss);
121 SCLogDebug(
"pcap-file: got packet, pfv=%p filename=%s ref_cnt now=%u p=%p", (
void *)ptv,
135 ptv->shared->bytes += h->caplen;
158 if (TmThreadsSlotProcessPkt(ptv->shared->tv, ptv->shared->slot, p) !=
TM_ECODE_OK) {
159 pcap_breakloop(ptv->pcap_handle);
175 pcap_current_pfv = pfv;
180 return pcap_current_pfv;
195 (u_char *)ptv->first_pkt_data);
196 ptv->first_pkt_hdr = NULL;
197 ptv->first_pkt_data = NULL;
200 int packet_q_len = 64;
214 int r = pcap_dispatch(ptv->
pcap_handle, packet_q_len,
215 (pcap_handler)PcapFileCallbackLoop, (u_char *)ptv);
217 SCLogError(
"error code %" PRId32
" %s for %s", r, pcap_geterr(ptv->pcap_handle),
224 SCLogInfo(
"pcap file %s end of file reached (pcap err code %" PRId32
")",
226 ptv->shared->files++;
229 SCLogError(
"Pcap callback PcapFileCallbackLoop failed for %s", ptv->filename);
246 if (r <= 0 || pfv->first_pkt_hdr == NULL) {
247 SCLogError(
"failed to get first packet timestamp. pcap_next_ex(): %d", r);
259 char errbuf[PCAP_ERRBUF_SIZE] =
"";
272 #if defined(HAVE_SETVBUF) && defined(OS_LINUX)
277 SCLogWarning(
"Failed to setvbuf on PCAP file handle: %s", strerror(errno));
294 pcap_freecode(&pfv->
filter);
297 pcap_freecode(&pfv->
filter);
312 if (!PeekFirstPacketTimestamp(pfv))
351 "datalink type %" PRId32
" not (yet) supported in module PcapFile.", datalink);
360 if (pfv == NULL || pfv->
shared == NULL) {
375 if (file_alerts != 0) {
376 SCLogDebug(
"Skipping deletion of %s due to %" PRIu64
" alert(s) generated.", pfv->
filename,
391 SCLogDebug(
"pcap-file: packet done pfv=%p filename=%s ref_cnt was=%u now=%u", (
void *)pfv,
394 if (pfv->cleanup_requested) {
403 if (pfv != NULL && alert_count > 0) {
408 static void PcapCaptureOnPacketWithAlerts(
const Packet *p)
420 static void PcapCaptureOnPseudoPacketCreated(
Packet *p)
436 CaptureHooksSet(PcapCaptureOnPacketWithAlerts, PcapCaptureOnPseudoPacketCreated);
442 const char *delete_when_done_str = NULL;
444 if (
SCConfGet(
"pcap-file.delete-when-done", &delete_when_done_str) == 1) {
445 if (strcmp(delete_when_done_str,
"non-alerts") == 0) {
448 int delete_always = 0;
449 if (
SCConfGetBool(
"pcap-file.delete-when-done", &delete_always) == 1) {
450 if (delete_always == 1) {
466 static int SourcePcapFileHelperTest01(
void)
469 memset(&shared, 0,
sizeof(shared));
473 memset(&pfv, 0,
sizeof(pfv));
511 static int SourcePcapFileHelperTest02(
void)
514 memset(&pfv, 0,
sizeof(pfv));
549 static int SetupYamlConf(
const char *conf_string)
557 static void CleanupYamlConf(
void)
566 static int SourcePcapFileHelperTest03(
void)
579 const char *conf_false =
"%YAML 1.1\n"
582 " delete-when-done: false\n";
584 SetupYamlConf(conf_false);
590 const char *conf_true =
"%YAML 1.1\n"
593 " delete-when-done: true\n";
595 SetupYamlConf(conf_true);
601 const char *conf_non_alerts =
"%YAML 1.1\n"
604 " delete-when-done: \"non-alerts\"\n";
606 SetupYamlConf(conf_non_alerts);
612 const char *conf_invalid =
"%YAML 1.1\n"
615 " delete-when-done: \"invalid-value\"\n";
617 SetupYamlConf(conf_invalid);
628 static int SourcePcapFileHelperTest04(
void)
638 static int SourcePcapFileHelperTest05(
void)
641 memset(&pfv, 0,
sizeof(pfv));
651 static int SourcePcapFileHelperTest06(
void)
654 memset(&pfv, 0,
sizeof(pfv));
680 static int SourcePcapFileHelperTest07(
void)
684 memset(&shared, 0,
sizeof(shared));
688 memset(&pfv, 0,
sizeof(pfv));
711 static int SourcePcapFileHelperTest08(
void)
714 const char *conf_false =
"%YAML 1.1\n"
717 " delete-when-done: false\n";
719 SetupYamlConf(conf_false);
722 int set_result =
SCConfSetFinal(
"pcap-file.delete-when-done",
"true");
730 const char *conf_non_alerts =
"%YAML 1.1\n"
733 " delete-when-done: \"non-alerts\"\n";
735 SetupYamlConf(conf_non_alerts);
738 set_result =
SCConfSetFinal(
"pcap-file.delete-when-done",
"true");
750 set_result =
SCConfSetFinal(
"pcap-file.delete-when-done",
"true");
766 static int SourcePcapFileHelperTest09(
void)
806 static int SourcePcapFileHelperTest10(
void)
809 const char *tmpname =
"suri_ut_delete_always.pcap";
810 const uint8_t dummy[] = { 0x00 };
837 FILE *f = fopen(tmpname,
"rb");
867 f = fopen(tmpname,
"rb");
876 f = fopen(tmpname,
"rb");
890 static int SourcePcapFileHelperTest11(
void)
894 memset(&pfv, 0,
sizeof(pfv));
915 static int SourcePcapFileHelperTest12(
void)
918 memset(&shared, 0,
sizeof(shared));
949 static int SourcePcapFileHelperTest13(
void)
969 static int SourcePcapFileHelperTest14(
void)
972 memset(&shared, 0,
sizeof(shared));
992 static int SourcePcapFileHelperTest15(
void)
1024 UtRegisterTest(
"SourcePcapFileHelperTest01", SourcePcapFileHelperTest01);
1025 UtRegisterTest(
"SourcePcapFileHelperTest02", SourcePcapFileHelperTest02);
1026 UtRegisterTest(
"SourcePcapFileHelperTest03", SourcePcapFileHelperTest03);
1027 UtRegisterTest(
"SourcePcapFileHelperTest04", SourcePcapFileHelperTest04);
1028 UtRegisterTest(
"SourcePcapFileHelperTest05", SourcePcapFileHelperTest05);
1029 UtRegisterTest(
"SourcePcapFileHelperTest06", SourcePcapFileHelperTest06);
1030 UtRegisterTest(
"SourcePcapFileHelperTest07", SourcePcapFileHelperTest07);
1031 UtRegisterTest(
"SourcePcapFileHelperTest08", SourcePcapFileHelperTest08);
1032 UtRegisterTest(
"SourcePcapFileHelperTest09", SourcePcapFileHelperTest09);
1033 UtRegisterTest(
"SourcePcapFileHelperTest10", SourcePcapFileHelperTest10);
1034 UtRegisterTest(
"SourcePcapFileHelperTest11", SourcePcapFileHelperTest11);
1035 UtRegisterTest(
"SourcePcapFileHelperTest12", SourcePcapFileHelperTest12);
1036 UtRegisterTest(
"SourcePcapFileHelperTest13", SourcePcapFileHelperTest13);
1037 UtRegisterTest(
"SourcePcapFileHelperTest14", SourcePcapFileHelperTest14);
1038 UtRegisterTest(
"SourcePcapFileHelperTest15", SourcePcapFileHelperTest15);
int SCConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
TmEcode PcapFileDispatch(PcapFileFileVars *ptv)
Main PCAP file reading Loop function.
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
PcapFileFileVars * PcapFileGetCurrentPfv(void)
void PacketFreeOrRelease(Packet *p)
Return a packet to where it was allocated.
void StatsSyncCountersIfSignalled(StatsThreadContext *stats)
void PcapFileFinalizePacket(PcapFileFileVars *pfv)
const u_char * first_pkt_data
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
int DecodePPP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
int PacketCopyData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
Copy data to Packet payload and set packet length.
TmEcode ValidateLinkType(int datalink, DecoderFunc *DecoderFn)
PcapFileSharedVars * shared
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
bool PcapFileShouldDeletePcapFile(PcapFileFileVars *pfv)
void PcapFileAddAlertCount(PcapFileFileVars *pfv, uint16_t alert_count)
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
int SCConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
int(* DecoderFunc)(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
#define LINKTYPE_GRE_OVER_IP
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
void CaptureHooksOnPacketWithAlerts(const Packet *p)
int DecodeCHDLC(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
@ CHECKSUM_VALIDATION_DISABLE
void PcapFileReleasePseudoPacket(Packet *p)
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
int ChecksumAutoModeCheck(uint64_t thread_count, uint64_t iface_count, uint64_t iface_fail)
Check if the number of invalid checksums indicate checksum offloading in place.
@ PCAP_FILE_DELETE_ALWAYS
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
size_t strlcpy(char *dst, const char *src, size_t siz)
#define SCTIME_FROM_TIMEVAL_UNTRUSTED(tv)
variant to deal with potentially bad timestamps, like from pcap files
void SourcePcapFileHelperRegisterTests(void)
Register unit tests for pcap file helper.
#define PKT_SET_SRC(p, src_val)
void SCConfInit(void)
Initialize the configuration system.
int DecodeNull(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
@ CHECKSUM_VALIDATION_AUTO
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
#define PASS
Pass the test.
void PcapFileSetCurrentPfv(PcapFileFileVars *pfv)
ChecksumValidationMode checksum_mode
#define SCTIME_FROM_TIMEVAL(tv)
@ PCAP_FILE_DELETE_NON_ALERTS
TmEcode InitPcapFile(PcapFileFileVars *pfv)
int TestHelperBufferToFile(const char *name, const uint8_t *data, size_t size)
writes the contents of a buffer into a file
#define SCLogWarning(...)
Macro used to log WARNING messages.
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
void PacketPoolWait(void)
void PcapFileInstallCaptureHooks(void)
#define PACKET_PROFILING_TMM_END(p, id)
#define PKT_IGNORE_CHECKSUM
uint32_t max_pending_packets
void SCConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
int SCConfSetFinal(const char *name, const char *val)
Set a final configuration value.
void TmThreadsInitThreadsTimestamp(const SCTime_t ts)
void(* ReleasePacket)(struct Packet_ *)
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
char pcap_filename[PATH_MAX]
int DecodeSll(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
void SCConfDeInit(void)
De-initializes the configuration system.
struct timeval first_pkt_ts
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
#define PACKET_PROFILING_TMM_START(p, id)
#define SCLogError(...)
Macro used to log ERROR messages.
void DatalinkSetGlobalType(int datalink)
#define LINKTYPE_LINUX_SLL
void SCConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
#define LINKTYPE_LINUX_SLL2
const char * PcapFileGetFilename(void)
#define LINKTYPE_ETHERNET
int DecodeRaw(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
struct pcap_pkthdr * first_pkt_hdr
PcapFileGlobalVars pcap_g
PcapFileDeleteMode PcapFileParseDeleteMode(void)
struct bpf_program filter
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
void CaptureHooksSet(CaptureOnPacketWithAlertsHook OnAlerts, CaptureOnPseudoPacketCreatedHook OnPseudoCreated)
void CleanupPcapFileFileVars(PcapFileFileVars *pfv)
#define SCLogNotice(...)
Macro used to log NOTICE messages.
int DecodeSll2(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
PcapFileDeleteMode delete_mode
#define LINKTYPE_CISCO_HDLC
Packet * PacketGetFromQueueOrAlloc(void)
Get a packet. We try to get a packet from the packetpool first, but if that is empty we alloc a packe...
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
uint32_t read_buffer_size
volatile uint8_t suricata_ctl_flags