suricata
source-pcap-file-helper.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 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  *
21  * \author Danny Browning <danny.browning@protectwise.com>
22  */
23 
24 #include "suricata-common.h"
25 #include "tm-threads.h"
26 
27 #ifndef SURICATA_SOURCE_PCAP_FILE_HELPER_H
28 #define SURICATA_SOURCE_PCAP_FILE_HELPER_H
29 
30 typedef enum {
35 
36 typedef struct PcapFileGlobalVars_ {
37  uint64_t cnt; /** packet counter */
40  SC_ATOMIC_DECLARE(unsigned int, invalid_checksums);
41  uint32_t read_buffer_size;
43 
44 /**
45  * Data that is shared amongst File, Directory, and Thread level vars
46  */
47 typedef struct PcapFileSharedVars_
48 {
49  char *bpf_string;
50 
51  uint32_t tenant_id;
52 
53  struct timespec last_processed;
54 
56 
59 
60  /* counters */
61  uint64_t pkts;
62  uint64_t bytes;
63  uint64_t files;
64 
65  uint8_t done;
66  uint32_t errs;
67 
68  /** callback result -- set if one of the thread module failed. */
69  int cb_result;
71 
72 /**
73  * Data specific to a single pcap file
74  */
75 typedef struct PcapFileFileVars_
76 {
77  char *filename;
78  pcap_t *pcap_handle;
79 
80  int datalink;
81  struct bpf_program filter;
82 
84 
85  SC_ATOMIC_DECLARE(uint64_t, alerts_count);
86  /* Reference count for outstanding users (e.g., pseudo packets created
87  * during shutdown/timeout handling). Ensures the per-file state is not
88  * cleaned up or the file deleted before all dependent processing that may
89  * still raise alerts has completed. */
90  SC_ATOMIC_DECLARE(uint32_t, ref_cnt);
92 
93  /* fields used to get the first packet's timestamp early,
94  * so it can be used to setup the time subsys. */
95  const u_char *first_pkt_data;
96  struct pcap_pkthdr *first_pkt_hdr;
97  struct timeval first_pkt_ts;
98 
99  /** flex array member for the libc io read buffer. Size controlled by
100  * PcapFileGlobalVars::read_buffer_size. */
101 #if defined(HAVE_SETVBUF) && defined(OS_LINUX)
102  char buffer[];
103 #endif
105 
106 /**
107  * Dispatch a file for processing, where the information necessary to process that
108  * file is as PcapFileFileVars object.
109  * @param ptv PcapFileFileVars object to be processed
110  * @return
111  */
113 
114 /**
115  * From a PcapFileFileVars, prepare the filename for processing by setting
116  * pcap_handle, datalink, and filter
117  * @param pfv PcapFileFileVars object to populate
118  * @return
119  */
121 
122 /**
123  * Cleanup resources associated with a PcapFileFileVars object.
124  * @param pfv Object to be cleaned up
125  */
127 
128 /**
129  * Determine if a datalink type is valid, setting a decoder function if valid.
130  * @param datalink Datalink type to validate
131  * @param decoder Pointer to decoder to set if valid
132  * @return TM_ECODE_OK if valid datalink type and decoder has been set.
133  */
134 TmEcode ValidateLinkType(int datalink, DecoderFunc *decoder);
135 
136 const char *PcapFileGetFilename(void);
137 
139 
141 
143 
144 void PcapFileAddAlertCount(PcapFileFileVars *pfv, uint16_t alert_count);
145 
147 
150 
151 void PcapFileInstallCaptureHooks(void);
152 
153 #ifdef UNITTESTS
155 #endif
156 
157 #endif /* SURICATA_SOURCE_PCAP_FILE_HELPER_H */
PcapFileSetCurrentPfv
void PcapFileSetCurrentPfv(PcapFileFileVars *pfv)
Definition: source-pcap-file-helper.c:173
PcapFileSharedVars_::slot
TmSlot * slot
Definition: source-pcap-file-helper.h:58
tm-threads.h
PcapFileFileVars_::filename
char * filename
Definition: source-pcap-file-helper.h:77
PcapFileSharedVars_
Definition: source-pcap-file-helper.h:48
PcapFileReleasePseudoPacket
void PcapFileReleasePseudoPacket(Packet *p)
Definition: source-pcap-file-helper.c:53
PcapFileGlobalVars_
Definition: source-pcap-file-helper.h:36
PcapFileFileVars_::first_pkt_data
const u_char * first_pkt_data
Definition: source-pcap-file-helper.h:95
PcapFileFileVars_::datalink
int datalink
Definition: source-pcap-file-helper.h:80
PcapFileFileVars_::shared
PcapFileSharedVars * shared
Definition: source-pcap-file-helper.h:83
CleanupPcapFileFileVars
void CleanupPcapFileFileVars(PcapFileFileVars *pfv)
Definition: source-pcap-file-helper.c:67
PcapFileFileVars_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint32_t, ref_cnt)
DecoderFunc
int(* DecoderFunc)(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode.h:1225
PcapFileFileVars_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, alerts_count)
PcapFileFileVars
struct PcapFileFileVars_ PcapFileFileVars
PcapFileSharedVars_::cb_result
int cb_result
Definition: source-pcap-file-helper.h:69
PcapFileSharedVars
struct PcapFileSharedVars_ PcapFileSharedVars
PcapFileSharedVars_::done
uint8_t done
Definition: source-pcap-file-helper.h:65
p
Packet * p
Definition: fuzz_dataset.c:30
InitPcapFile
TmEcode InitPcapFile(PcapFileFileVars *pfv)
Definition: source-pcap-file-helper.c:257
PcapFileParseDeleteMode
PcapFileDeleteMode PcapFileParseDeleteMode(void)
Definition: source-pcap-file-helper.c:445
PCAP_FILE_DELETE_ALWAYS
@ PCAP_FILE_DELETE_ALWAYS
Definition: source-pcap-file-helper.h:32
PcapFileSharedVars_::files
uint64_t files
Definition: source-pcap-file-helper.h:63
PcapFileGlobalVars
struct PcapFileGlobalVars_ PcapFileGlobalVars
PcapFileGlobalVars_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(unsigned int, invalid_checksums)
PCAP_FILE_DELETE_NONE
@ PCAP_FILE_DELETE_NONE
Definition: source-pcap-file-helper.h:31
PcapFileGlobalVars_::cnt
uint64_t cnt
Definition: source-pcap-file-helper.h:37
PcapFileAddAlertCount
void PcapFileAddAlertCount(PcapFileFileVars *pfv, uint16_t alert_count)
Definition: source-pcap-file-helper.c:407
PcapFileGlobalVars_::checksum_mode
ChecksumValidationMode checksum_mode
Definition: source-pcap-file-helper.h:39
PcapFileDispatch
TmEcode PcapFileDispatch(PcapFileFileVars *ptv)
Main PCAP file reading Loop function.
Definition: source-pcap-file-helper.c:186
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
PCAP_FILE_DELETE_NON_ALERTS
@ PCAP_FILE_DELETE_NON_ALERTS
Definition: source-pcap-file-helper.h:33
PcapFileFinalizePacket
void PcapFileFinalizePacket(PcapFileFileVars *pfv)
Definition: source-pcap-file-helper.c:392
Packet_
Definition: decode.h:516
TmSlot_
Definition: tm-threads.h:53
PcapFileFileVars_
Definition: source-pcap-file-helper.h:76
TmEcode
TmEcode
Definition: tm-threads-common.h:80
PcapFileGetFilename
const char * PcapFileGetFilename(void)
Definition: source-pcap-file-helper.c:168
PcapFileSharedVars_::bpf_string
char * bpf_string
Definition: source-pcap-file-helper.h:49
ChecksumValidationMode
ChecksumValidationMode
Definition: decode.h:42
suricata-common.h
PcapFileSharedVars_::tenant_id
uint32_t tenant_id
Definition: source-pcap-file-helper.h:51
PcapFileFileVars_::first_pkt_ts
struct timeval first_pkt_ts
Definition: source-pcap-file-helper.h:97
PcapFileShouldDeletePcapFile
bool PcapFileShouldDeletePcapFile(PcapFileFileVars *pfv)
Definition: source-pcap-file-helper.c:364
PcapFileInstallCaptureHooks
void PcapFileInstallCaptureHooks(void)
Definition: source-pcap-file-helper.c:440
ValidateLinkType
TmEcode ValidateLinkType(int datalink, DecoderFunc *decoder)
Definition: source-pcap-file-helper.c:326
PcapFileSharedVars_::last_processed
struct timespec last_processed
Definition: source-pcap-file-helper.h:53
PcapFileGlobalVars_::conf_checksum_mode
ChecksumValidationMode conf_checksum_mode
Definition: source-pcap-file-helper.h:38
PcapFileFileVars_::pcap_handle
pcap_t * pcap_handle
Definition: source-pcap-file-helper.h:78
PcapFileGetCurrentPfv
PcapFileFileVars * PcapFileGetCurrentPfv(void)
Definition: source-pcap-file-helper.c:178
PcapFileFileVars_::first_pkt_hdr
struct pcap_pkthdr * first_pkt_hdr
Definition: source-pcap-file-helper.h:96
PcapFileFileVars_::filter
struct bpf_program filter
Definition: source-pcap-file-helper.h:81
PcapFileSharedVars_::bytes
uint64_t bytes
Definition: source-pcap-file-helper.h:62
PcapFileSharedVars_::tv
ThreadVars * tv
Definition: source-pcap-file-helper.h:57
SourcePcapFileHelperRegisterTests
void SourcePcapFileHelperRegisterTests(void)
Register unit tests for pcap file helper.
Definition: source-pcap-file-helper.c:1028
PcapFileSharedVars_::delete_mode
PcapFileDeleteMode delete_mode
Definition: source-pcap-file-helper.h:55
PcapFileFileVars_::cleanup_requested
bool cleanup_requested
Definition: source-pcap-file-helper.h:91
PcapFileSharedVars_::errs
uint32_t errs
Definition: source-pcap-file-helper.h:66
PcapFileGlobalVars_::read_buffer_size
uint32_t read_buffer_size
Definition: source-pcap-file-helper.h:41
PcapFileSharedVars_::pkts
uint64_t pkts
Definition: source-pcap-file-helper.h:61
PcapFileDeleteMode
PcapFileDeleteMode
Definition: source-pcap-file-helper.h:30