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 #include "util-atomic.h"
27 
28 #ifndef SURICATA_SOURCE_PCAP_FILE_HELPER_H
29 #define SURICATA_SOURCE_PCAP_FILE_HELPER_H
30 
31 typedef enum {
36 
37 typedef struct PcapFileGlobalVars_ {
38  uint64_t cnt; /** packet counter */
41  SC_ATOMIC_DECLARE(unsigned int, invalid_checksums);
42  uint32_t read_buffer_size;
44 
45 /**
46  * Data that is shared amongst File, Directory, and Thread level vars
47  */
48 typedef struct PcapFileSharedVars_
49 {
50  char *bpf_string;
51 
52  uint32_t tenant_id;
53 
54  struct timespec last_processed;
55 
57 
60 
61  /* counters */
62  uint64_t pkts;
63  uint64_t bytes;
64  uint64_t files;
65 
66  uint8_t done;
67  uint32_t errs;
68 
69  /** callback result -- set if one of the thread module failed. */
70  int cb_result;
72 
73 /**
74  * Data specific to a single pcap file
75  */
76 typedef struct PcapFileFileVars_
77 {
78  char *filename;
79  pcap_t *pcap_handle;
80 
81  int datalink;
82  struct bpf_program filter;
83 
85 
86  SC_ATOMIC_DECLARE(uint64_t, alerts_count);
87  /* Reference count for outstanding users (e.g., pseudo packets created
88  * during shutdown/timeout handling). Ensures the per-file state is not
89  * cleaned up or the file deleted before all dependent processing that may
90  * still raise alerts has completed. */
91  SC_ATOMIC_DECLARE(uint32_t, ref_cnt);
93 
94  /* fields used to get the first packet's timestamp early,
95  * so it can be used to setup the time subsys. */
96  const u_char *first_pkt_data;
97  struct pcap_pkthdr *first_pkt_hdr;
98  struct timeval first_pkt_ts;
99 
100  /** flex array member for the libc io read buffer. Size controlled by
101  * PcapFileGlobalVars::read_buffer_size. */
102 #if defined(HAVE_SETVBUF) && defined(OS_LINUX)
103  char buffer[];
104 #endif
106 
107 /**
108  * Dispatch a file for processing, where the information necessary to process that
109  * file is as PcapFileFileVars object.
110  * @param ptv PcapFileFileVars object to be processed
111  * @return
112  */
114 
115 /**
116  * From a PcapFileFileVars, prepare the filename for processing by setting
117  * pcap_handle, datalink, and filter
118  * @param pfv PcapFileFileVars object to populate
119  * @return
120  */
122 
123 /**
124  * Cleanup resources associated with a PcapFileFileVars object.
125  * @param pfv Object to be cleaned up
126  */
128 
129 /**
130  * Determine if a datalink type is valid, setting a decoder function if valid.
131  * @param datalink Datalink type to validate
132  * @param decoder Pointer to decoder to set if valid
133  * @return TM_ECODE_OK if valid datalink type and decoder has been set.
134  */
135 TmEcode ValidateLinkType(int datalink, DecoderFunc *decoder);
136 
137 const char *PcapFileGetFilename(void);
138 
140 
142 
144 
145 void PcapFileAddAlertCount(PcapFileFileVars *pfv, uint16_t alert_count);
146 
148 
151 
152 void PcapFileInstallCaptureHooks(void);
153 
154 #ifdef UNITTESTS
156 #endif
157 
158 #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:59
tm-threads.h
PcapFileFileVars_::filename
char * filename
Definition: source-pcap-file-helper.h:78
PcapFileSharedVars_
Definition: source-pcap-file-helper.h:49
PcapFileReleasePseudoPacket
void PcapFileReleasePseudoPacket(Packet *p)
Definition: source-pcap-file-helper.c:53
PcapFileGlobalVars_
Definition: source-pcap-file-helper.h:37
PcapFileFileVars_::first_pkt_data
const u_char * first_pkt_data
Definition: source-pcap-file-helper.h:96
PcapFileFileVars_::datalink
int datalink
Definition: source-pcap-file-helper.h:81
PcapFileFileVars_::shared
PcapFileSharedVars * shared
Definition: source-pcap-file-helper.h:84
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:1182
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:70
PcapFileSharedVars
struct PcapFileSharedVars_ PcapFileSharedVars
PcapFileSharedVars_::done
uint8_t done
Definition: source-pcap-file-helper.h:66
InitPcapFile
TmEcode InitPcapFile(PcapFileFileVars *pfv)
Definition: source-pcap-file-helper.c:257
PcapFileParseDeleteMode
PcapFileDeleteMode PcapFileParseDeleteMode(void)
Definition: source-pcap-file-helper.c:439
PCAP_FILE_DELETE_ALWAYS
@ PCAP_FILE_DELETE_ALWAYS
Definition: source-pcap-file-helper.h:33
PcapFileSharedVars_::files
uint64_t files
Definition: source-pcap-file-helper.h:64
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:32
PcapFileGlobalVars_::cnt
uint64_t cnt
Definition: source-pcap-file-helper.h:38
PcapFileAddAlertCount
void PcapFileAddAlertCount(PcapFileFileVars *pfv, uint16_t alert_count)
Definition: source-pcap-file-helper.c:401
PcapFileGlobalVars_::checksum_mode
ChecksumValidationMode checksum_mode
Definition: source-pcap-file-helper.h:40
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:58
util-atomic.h
PCAP_FILE_DELETE_NON_ALERTS
@ PCAP_FILE_DELETE_NON_ALERTS
Definition: source-pcap-file-helper.h:34
PcapFileFinalizePacket
void PcapFileFinalizePacket(PcapFileFileVars *pfv)
Definition: source-pcap-file-helper.c:386
Packet_
Definition: decode.h:501
TmSlot_
Definition: tm-threads.h:53
PcapFileFileVars_
Definition: source-pcap-file-helper.h:77
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:50
ChecksumValidationMode
ChecksumValidationMode
Definition: decode.h:42
suricata-common.h
PcapFileSharedVars_::tenant_id
uint32_t tenant_id
Definition: source-pcap-file-helper.h:52
PcapFileFileVars_::first_pkt_ts
struct timeval first_pkt_ts
Definition: source-pcap-file-helper.h:98
PcapFileShouldDeletePcapFile
bool PcapFileShouldDeletePcapFile(PcapFileFileVars *pfv)
Definition: source-pcap-file-helper.c:358
PcapFileInstallCaptureHooks
void PcapFileInstallCaptureHooks(void)
Definition: source-pcap-file-helper.c:434
ValidateLinkType
TmEcode ValidateLinkType(int datalink, DecoderFunc *decoder)
Definition: source-pcap-file-helper.c:320
PcapFileSharedVars_::last_processed
struct timespec last_processed
Definition: source-pcap-file-helper.h:54
PcapFileGlobalVars_::conf_checksum_mode
ChecksumValidationMode conf_checksum_mode
Definition: source-pcap-file-helper.h:39
PcapFileFileVars_::pcap_handle
pcap_t * pcap_handle
Definition: source-pcap-file-helper.h:79
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:97
PcapFileFileVars_::filter
struct bpf_program filter
Definition: source-pcap-file-helper.h:82
PcapFileSharedVars_::bytes
uint64_t bytes
Definition: source-pcap-file-helper.h:63
PcapFileSharedVars_::tv
ThreadVars * tv
Definition: source-pcap-file-helper.h:58
SourcePcapFileHelperRegisterTests
void SourcePcapFileHelperRegisterTests(void)
Register unit tests for pcap file helper.
Definition: source-pcap-file-helper.c:1022
PcapFileSharedVars_::delete_mode
PcapFileDeleteMode delete_mode
Definition: source-pcap-file-helper.h:56
PcapFileFileVars_::cleanup_requested
bool cleanup_requested
Definition: source-pcap-file-helper.h:92
PcapFileSharedVars_::errs
uint32_t errs
Definition: source-pcap-file-helper.h:67
PcapFileGlobalVars_::read_buffer_size
uint32_t read_buffer_size
Definition: source-pcap-file-helper.h:42
PcapFileSharedVars_::pkts
uint64_t pkts
Definition: source-pcap-file-helper.h:62
PcapFileDeleteMode
PcapFileDeleteMode
Definition: source-pcap-file-helper.h:31