suricata
source-erf-file.c
Go to the documentation of this file.
1 /* Copyright (C) 2010-2014 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 Endace Technology Limited.
22  *
23  * Support for reading ERF files.
24  *
25  * Only ethernet supported at this time.
26  */
27 
28 #include "suricata-common.h"
29 #include "suricata.h"
30 #include "tm-threads.h"
31 #include "source-erf-file.h"
32 
33 #define DAG_TYPE_ETH 2
34 
35 typedef struct DagFlags_ {
36  uint8_t iface:2;
37  uint8_t vlen:1;
38  uint8_t trunc:1;
39  uint8_t rxerror:1;
40  uint8_t dserror:1;
41  uint8_t reserved:1;
42  uint8_t direction:1;
44 
45 typedef struct DagRecord_ {
46  uint64_t ts;
47  uint8_t type;
49  uint16_t rlen;
50  uint16_t lctr;
51  uint16_t wlen;
52  uint16_t pad;
53 } __attribute__((packed)) DagRecord;
54 
55 typedef struct ErfFileThreadVars_ {
57  TmSlot *slot;
58 
59  FILE *erf;
60 
61  uint32_t pkts;
62  uint64_t bytes;
64 
65 static inline TmEcode ReadErfRecord(ThreadVars *, Packet *, void *);
66 TmEcode ReceiveErfFileLoop(ThreadVars *, void *, void *);
67 TmEcode ReceiveErfFileThreadInit(ThreadVars *, const void *, void **);
70 
71 static TmEcode DecodeErfFileThreadInit(ThreadVars *, const void *, void **);
72 static TmEcode DecodeErfFileThreadDeinit(ThreadVars *tv, void *data);
73 static TmEcode DecodeErfFile(ThreadVars *, Packet *, void *);
74 
75 /**
76  * \brief Register the ERF file receiver (reader) module.
77  */
78 void
80 {
81  tmm_modules[TMM_RECEIVEERFFILE].name = "ReceiveErfFile";
92 }
93 
94 /**
95  * \brief Register the ERF file decoder module.
96  */
97 void
99 {
100  tmm_modules[TMM_DECODEERFFILE].name = "DecodeErfFile";
101  tmm_modules[TMM_DECODEERFFILE].ThreadInit = DecodeErfFileThreadInit;
102  tmm_modules[TMM_DECODEERFFILE].Func = DecodeErfFile;
104  tmm_modules[TMM_DECODEERFFILE].ThreadDeinit = DecodeErfFileThreadDeinit;
108 }
109 
110 /**
111  * \brief ERF file reading loop.
112  */
113 TmEcode ReceiveErfFileLoop(ThreadVars *tv, void *data, void *slot)
114 {
115  Packet *p = NULL;
116  ErfFileThreadVars *etv = (ErfFileThreadVars *)data;
117 
118  etv->slot = ((TmSlot *)slot)->slot_next;
119 
120  while (1) {
123  }
124 
125  /* Make sure we have at least one packet in the packet pool,
126  * to prevent us from alloc'ing packets at line rate. */
127  PacketPoolWait();
128 
130  if (unlikely(p == NULL)) {
131  SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate a packet.");
132  EngineStop();
134  }
136 
137  if (ReadErfRecord(tv, p, data) != TM_ECODE_OK) {
138  TmqhOutputPacketpool(etv->tv, p);
139  EngineStop();
141  }
142 
143  if (TmThreadsSlotProcessPkt(etv->tv, etv->slot, p) != TM_ECODE_OK) {
144  EngineStop();
146  }
147  }
149 }
150 
151 static inline TmEcode ReadErfRecord(ThreadVars *tv, Packet *p, void *data)
152 {
153  SCEnter();
154 
155  ErfFileThreadVars *etv = (ErfFileThreadVars *)data;
156  DagRecord dr;
157 
158  int r = fread(&dr, sizeof(DagRecord), 1, etv->erf);
159  if (r < 1) {
160  if (feof(etv->erf)) {
161  SCLogInfo("End of ERF file reached");
162  }
163  else {
164  SCLogInfo("Error reading ERF record");
165  }
167  }
168  uint16_t rlen = SCNtohs(dr.rlen);
169  uint16_t wlen = SCNtohs(dr.wlen);
170  if (rlen < sizeof(DagRecord)) {
171  SCLogError(SC_ERR_ERF_BAD_RLEN, "Bad ERF record, "
172  "record length less than size of header");
174  }
175  r = fread(GET_PKT_DATA(p), rlen - sizeof(DagRecord), 1, etv->erf);
176  if (r < 1) {
177  if (feof(etv->erf)) {
178  SCLogInfo("End of ERF file reached");
179  }
180  else {
181  SCLogInfo("Error reading ERF record");
182  }
184  }
185 
186  /* Only support ethernet at this time. */
187  if (dr.type != DAG_TYPE_ETH) {
189  "DAG record type %d not implemented.", dr.type);
191  }
192 
193  GET_PKT_LEN(p) = wlen;
195 
196  /* Convert ERF time to timeval - from libpcap. */
197  uint64_t ts = dr.ts;
198  p->ts.tv_sec = ts >> 32;
199  ts = (ts & 0xffffffffULL) * 1000000;
200  ts += 0x80000000; /* rounding */
201  p->ts.tv_usec = ts >> 32;
202  if (p->ts.tv_usec >= 1000000) {
203  p->ts.tv_usec -= 1000000;
204  p->ts.tv_sec++;
205  }
206 
207  etv->pkts++;
208  etv->bytes += wlen;
209 
211 }
212 
213 /**
214  * \brief Initialize the ERF receiver thread.
215  */
216 TmEcode
217 ReceiveErfFileThreadInit(ThreadVars *tv, const void *initdata, void **data)
218 {
219  SCEnter();
220 
221  if (initdata == NULL) {
222  SCLogError(SC_ERR_INVALID_ARGUMENT, "Error: No filename provided.");
224  }
225 
226  FILE *erf = fopen((const char *)initdata, "r");
227  if (erf == NULL) {
228  SCLogError(SC_ERR_FOPEN, "Failed to open %s: %s", (char *)initdata,
229  strerror(errno));
230  exit(EXIT_FAILURE);
231  }
232 
234  if (unlikely(etv == NULL)) {
235  SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate memory for ERF file thread vars.");
236  fclose(erf);
238  }
239  memset(etv, 0, sizeof(*etv));
240  etv->erf = erf;
241  etv->tv = tv;
242  *data = (void *)etv;
243 
244  SCLogInfo("Processing ERF file %s", (char *)initdata);
245 
247 }
248 
249 /**
250  * \brief Initialize the ERF decoder thread.
251  */
252 TmEcode
253 DecodeErfFileThreadInit(ThreadVars *tv, const void *initdata, void **data)
254 {
255  SCEnter();
256  DecodeThreadVars *dtv = NULL;
258 
259  if (dtv == NULL)
261 
263 
264  *data = (void *)dtv;
265 
267 }
268 
269 TmEcode DecodeErfFileThreadDeinit(ThreadVars *tv, void *data)
270 {
271  if (data != NULL)
272  DecodeThreadVarsFree(tv, data);
274 }
275 
276 /**
277  * \brief Decode the ERF file.
278  *
279  * This function ups the decoder counters and then passes the packet
280  * off to the ethernet decoder.
281  */
282 TmEcode
283 DecodeErfFile(ThreadVars *tv, Packet *p, void *data)
284 {
285  SCEnter();
287 
289 
290  /* Update counters. */
292 
294 
296 
298 }
299 
300 /**
301  * \brief Print some stats to the log at program exit.
302  *
303  * \param tv Pointer to ThreadVars.
304  * \param data Pointer to data, ErfFileThreadVars.
305  */
306 void
308 {
309  ErfFileThreadVars *etv = (ErfFileThreadVars *)data;
310 
311  SCLogInfo("Packets: %"PRIu32"; Bytes: %"PRIu64, etv->pkts, etv->bytes);
312 }
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:67
TMM_RECEIVEERFFILE
@ TMM_RECEIVEERFFILE
Definition: tm-threads-common.h:48
tm-threads.h
DagRecord_::type
uint8_t type
Definition: source-erf-file.c:47
ts
uint64_t ts
Definition: source-erf-file.c:54
DagRecord_::lctr
uint16_t lctr
Definition: source-erf-file.c:50
DAG_TYPE_ETH
#define DAG_TYPE_ETH
Definition: source-erf-file.c:33
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1125
DagRecord_::ts
uint64_t ts
Definition: source-erf-file.c:46
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DagFlags_::trunc
uint8_t trunc
Definition: source-erf-file.c:38
ReceiveErfFileThreadExitStats
void ReceiveErfFileThreadExitStats(ThreadVars *, void *)
Print some stats to the log at program exit.
Definition: source-erf-file.c:307
DagRecord_::flags
DagFlags flags
Definition: source-erf-file.c:48
TmModuleDecodeErfFileRegister
void TmModuleDecodeErfFileRegister(void)
Register the ERF file decoder module.
Definition: source-erf-file.c:98
SURICATA_STOP
#define SURICATA_STOP
Definition: suricata.h:90
PacketDecodeFinalize
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
Definition: decode.c:118
TmqhOutputPacketpool
void TmqhOutputPacketpool(ThreadVars *t, Packet *p)
Definition: tmqh-packetpool.c:375
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
TmModule_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:54
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
Packet_::datalink
int datalink
Definition: decode.h:577
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1128
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:478
PKT_SRC_WIRE
@ PKT_SRC_WIRE
Definition: decode.h:50
DagFlags_::rxerror
uint8_t rxerror
Definition: source-erf-file.c:39
DagFlags_::direction
uint8_t direction
Definition: source-erf-file.c:42
TmModule_::PktAcqBreakLoop
TmEcode(* PktAcqBreakLoop)(ThreadVars *, void *)
Definition: tm-modules.h:57
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:228
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
TmModule_::Func
TmEcode(* Func)(ThreadVars *, Packet *, void *)
Definition: tm-modules.h:52
DagRecord_::rlen
uint16_t rlen
Definition: source-erf-file.c:49
DagFlags_
Definition: source-erf-file.c:35
SC_ERR_INVALID_ARGUMENT
@ SC_ERR_INVALID_ARGUMENT
Definition: util-error.h:43
ErfFileThreadVars
ErfFileThreadVars
Definition: source-erf-file.c:63
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:282
PacketPoolWait
void PacketPoolWait(void)
Definition: tmqh-packetpool.c:84
SC_ERR_FOPEN
@ SC_ERR_FOPEN
Definition: util-error.h:74
Packet_
Definition: decode.h:411
TM_FLAG_DECODE_TM
#define TM_FLAG_DECODE_TM
Definition: tm-modules.h:32
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:33
wlen
uint16_t wlen
Definition: source-erf-file.c:59
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:227
TmModule_::RegisterTests
void(* RegisterTests)(void)
Definition: tm-modules.h:65
TmSlot_
Definition: tm-threads.h:52
DagFlags
struct DagFlags_ DagFlags
TmEcode
TmEcode
Definition: tm-threads-common.h:77
TMM_DECODEERFFILE
@ TMM_DECODEERFFILE
Definition: tm-threads-common.h:49
TmModule_::name
const char * name
Definition: tm-modules.h:44
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:217
TM_FLAG_RECEIVE_TM
#define TM_FLAG_RECEIVE_TM
Definition: tm-modules.h:31
TmModuleReceiveErfFileRegister
void TmModuleReceiveErfFileRegister(void)
Register the ERF file receiver (reader) module.
Definition: source-erf-file.c:79
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:30
ReceiveErfFileThreadDeinit
TmEcode ReceiveErfFileThreadDeinit(ThreadVars *, void *)
__attribute__
struct DagRecord_ __attribute__((packed))
DNP3 application object header.
Definition: source-erf-file.c:53
DecodeThreadVarsFree
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
Definition: decode.c:638
Packet_::ts
struct timeval ts
Definition: decode.h:454
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:398
suricata-common.h
DagFlags_::dserror
uint8_t dserror
Definition: source-erf-file.c:40
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
EngineStop
void EngineStop(void)
make sure threads can stop the engine by calling this function. Purpose: pcap file mode needs to be a...
Definition: suricata.c:391
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
SC_ERR_UNIMPLEMENTED
@ SC_ERR_UNIMPLEMENTED
Definition: util-error.h:118
TmModule_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:48
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
DagFlags_::vlen
uint8_t vlen
Definition: source-erf-file.c:37
DagRecord_::wlen
uint16_t wlen
Definition: source-erf-file.c:51
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:625
DagFlags_::reserved
uint8_t reserved
Definition: source-erf-file.c:41
source-erf-file.h
DecodeThreadVarsAlloc
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:619
DagFlags_::iface
uint8_t iface
Definition: source-erf-file.c:36
DagRecord_
Definition: source-erf-file.c:45
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
suricata.h
SC_ERR_ERF_BAD_RLEN
@ SC_ERR_ERF_BAD_RLEN
Definition: util-error.h:361
DagRecord_::pad
uint16_t pad
Definition: source-erf-file.c:52
ReceiveErfFileThreadInit
TmEcode ReceiveErfFileThreadInit(ThreadVars *, const void *, void **)
Initialize the ERF receiver thread.
Definition: source-erf-file.c:217
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
ReceiveErfFileLoop
TmEcode ReceiveErfFileLoop(ThreadVars *, void *, void *)
ERF file reading loop.
Definition: source-erf-file.c:113
PacketGetFromQueueOrAlloc
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...
Definition: decode.c:180
DecodeEthernet
int DecodeEthernet(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint32_t len)
Definition: decode-ethernet.c:41
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:70
DecodeUpdatePacketCounters
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
Definition: decode.c:585
LINKTYPE_ETHERNET
#define LINKTYPE_ETHERNET
Definition: decode.h:1066
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:198
rlen
uint16_t rlen
Definition: source-erf-file.c:57