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)
276 if (fd >= 0 && fstat(fd, &sb) == 0 && !S_ISREG(sb.st_mode)) {
277 SCLogInfo(
"%s: skipping setvbuf, underlying fd is not a regular file", pfv->
filename);
280 if (setvbuf(pcap_file(pfv->
pcap_handle), pfv->buffer, _IOFBF,
282 SCLogWarning(
"Failed to setvbuf on PCAP file handle: %s", strerror(errno));
300 pcap_freecode(&pfv->
filter);
303 pcap_freecode(&pfv->
filter);
318 if (!PeekFirstPacketTimestamp(pfv))
357 "datalink type %" PRId32
" not (yet) supported in module PcapFile.", datalink);
366 if (pfv == NULL || pfv->
shared == NULL) {
381 if (file_alerts != 0) {
382 SCLogDebug(
"Skipping deletion of %s due to %" PRIu64
" alert(s) generated.", pfv->
filename,
397 SCLogDebug(
"pcap-file: packet done pfv=%p filename=%s ref_cnt was=%u now=%u", (
void *)pfv,
400 if (pfv->cleanup_requested) {
409 if (pfv != NULL && alert_count > 0) {
414 static void PcapCaptureOnPacketWithAlerts(
const Packet *
p)
426 static void PcapCaptureOnPseudoPacketCreated(
Packet *
p)
442 CaptureHooksSet(PcapCaptureOnPacketWithAlerts, PcapCaptureOnPseudoPacketCreated);
448 const char *delete_when_done_str = NULL;
450 if (
SCConfGetNonNull(
"pcap-file.delete-when-done", &delete_when_done_str) == 1) {
451 if (strcmp(delete_when_done_str,
"non-alerts") == 0) {
454 int delete_always = 0;
455 if (
SCConfGetBool(
"pcap-file.delete-when-done", &delete_always) == 1) {
456 if (delete_always == 1) {
472 static int SourcePcapFileHelperTest01(
void)
475 memset(&shared, 0,
sizeof(shared));
479 memset(&pfv, 0,
sizeof(pfv));
517 static int SourcePcapFileHelperTest02(
void)
520 memset(&pfv, 0,
sizeof(pfv));
555 static int SetupYamlConf(
const char *conf_string)
563 static void CleanupYamlConf(
void)
572 static int SourcePcapFileHelperTest03(
void)
585 const char *conf_false =
"%YAML 1.1\n"
588 " delete-when-done: false\n";
590 SetupYamlConf(conf_false);
596 const char *conf_true =
"%YAML 1.1\n"
599 " delete-when-done: true\n";
601 SetupYamlConf(conf_true);
607 const char *conf_non_alerts =
"%YAML 1.1\n"
610 " delete-when-done: \"non-alerts\"\n";
612 SetupYamlConf(conf_non_alerts);
618 const char *conf_invalid =
"%YAML 1.1\n"
621 " delete-when-done: \"invalid-value\"\n";
623 SetupYamlConf(conf_invalid);
634 static int SourcePcapFileHelperTest04(
void)
644 static int SourcePcapFileHelperTest05(
void)
647 memset(&pfv, 0,
sizeof(pfv));
657 static int SourcePcapFileHelperTest06(
void)
660 memset(&pfv, 0,
sizeof(pfv));
686 static int SourcePcapFileHelperTest07(
void)
690 memset(&shared, 0,
sizeof(shared));
694 memset(&pfv, 0,
sizeof(pfv));
717 static int SourcePcapFileHelperTest08(
void)
720 const char *conf_false =
"%YAML 1.1\n"
723 " delete-when-done: false\n";
725 SetupYamlConf(conf_false);
728 int set_result =
SCConfSetFinal(
"pcap-file.delete-when-done",
"true");
736 const char *conf_non_alerts =
"%YAML 1.1\n"
739 " delete-when-done: \"non-alerts\"\n";
741 SetupYamlConf(conf_non_alerts);
744 set_result =
SCConfSetFinal(
"pcap-file.delete-when-done",
"true");
756 set_result =
SCConfSetFinal(
"pcap-file.delete-when-done",
"true");
772 static int SourcePcapFileHelperTest09(
void)
812 static int SourcePcapFileHelperTest10(
void)
815 const char *tmpname =
"suri_ut_delete_always.pcap";
816 const uint8_t dummy[] = { 0x00 };
843 FILE *f = fopen(tmpname,
"rb");
873 f = fopen(tmpname,
"rb");
882 f = fopen(tmpname,
"rb");
896 static int SourcePcapFileHelperTest11(
void)
900 memset(&pfv, 0,
sizeof(pfv));
921 static int SourcePcapFileHelperTest12(
void)
924 memset(&shared, 0,
sizeof(shared));
955 static int SourcePcapFileHelperTest13(
void)
975 static int SourcePcapFileHelperTest14(
void)
978 memset(&shared, 0,
sizeof(shared));
998 static int SourcePcapFileHelperTest15(
void)
1030 UtRegisterTest(
"SourcePcapFileHelperTest01", SourcePcapFileHelperTest01);
1031 UtRegisterTest(
"SourcePcapFileHelperTest02", SourcePcapFileHelperTest02);
1032 UtRegisterTest(
"SourcePcapFileHelperTest03", SourcePcapFileHelperTest03);
1033 UtRegisterTest(
"SourcePcapFileHelperTest04", SourcePcapFileHelperTest04);
1034 UtRegisterTest(
"SourcePcapFileHelperTest05", SourcePcapFileHelperTest05);
1035 UtRegisterTest(
"SourcePcapFileHelperTest06", SourcePcapFileHelperTest06);
1036 UtRegisterTest(
"SourcePcapFileHelperTest07", SourcePcapFileHelperTest07);
1037 UtRegisterTest(
"SourcePcapFileHelperTest08", SourcePcapFileHelperTest08);
1038 UtRegisterTest(
"SourcePcapFileHelperTest09", SourcePcapFileHelperTest09);
1039 UtRegisterTest(
"SourcePcapFileHelperTest10", SourcePcapFileHelperTest10);
1040 UtRegisterTest(
"SourcePcapFileHelperTest11", SourcePcapFileHelperTest11);
1041 UtRegisterTest(
"SourcePcapFileHelperTest12", SourcePcapFileHelperTest12);
1042 UtRegisterTest(
"SourcePcapFileHelperTest13", SourcePcapFileHelperTest13);
1043 UtRegisterTest(
"SourcePcapFileHelperTest14", SourcePcapFileHelperTest14);
1044 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(* 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
int SCConfGetNonNull(const char *name, const char **vptr)
Retrieve the non-null value of a configuration node.
#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