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";
91 }
92 
93 /**
94  * \brief Register the ERF file decoder module.
95  */
96 void
98 {
99  tmm_modules[TMM_DECODEERFFILE].name = "DecodeErfFile";
100  tmm_modules[TMM_DECODEERFFILE].ThreadInit = DecodeErfFileThreadInit;
101  tmm_modules[TMM_DECODEERFFILE].Func = DecodeErfFile;
103  tmm_modules[TMM_DECODEERFFILE].ThreadDeinit = DecodeErfFileThreadDeinit;
106 }
107 
108 /**
109  * \brief ERF file reading loop.
110  */
111 TmEcode ReceiveErfFileLoop(ThreadVars *tv, void *data, void *slot)
112 {
113  Packet *p = NULL;
114  ErfFileThreadVars *etv = (ErfFileThreadVars *)data;
115 
116  etv->slot = ((TmSlot *)slot)->slot_next;
117 
118  while (1) {
121  }
122 
123  /* Make sure we have at least one packet in the packet pool,
124  * to prevent us from alloc'ing packets at line rate. */
125  PacketPoolWait();
126 
128  if (unlikely(p == NULL)) {
129  SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate a packet.");
130  EngineStop();
132  }
134 
135  if (ReadErfRecord(tv, p, data) != TM_ECODE_OK) {
136  TmqhOutputPacketpool(etv->tv, p);
137  EngineStop();
139  }
140 
141  if (TmThreadsSlotProcessPkt(etv->tv, etv->slot, p) != TM_ECODE_OK) {
142  EngineStop();
144  }
145  }
147 }
148 
149 static inline TmEcode ReadErfRecord(ThreadVars *tv, Packet *p, void *data)
150 {
151  SCEnter();
152 
153  ErfFileThreadVars *etv = (ErfFileThreadVars *)data;
154  DagRecord dr;
155 
156  int r = fread(&dr, sizeof(DagRecord), 1, etv->erf);
157  if (r < 1) {
158  if (feof(etv->erf)) {
159  SCLogInfo("End of ERF file reached");
160  }
161  else {
162  SCLogInfo("Error reading ERF record");
163  }
165  }
166  uint16_t rlen = SCNtohs(dr.rlen);
167  uint16_t wlen = SCNtohs(dr.wlen);
168  if (rlen < sizeof(DagRecord)) {
169  SCLogError(SC_ERR_ERF_BAD_RLEN, "Bad ERF record, "
170  "record length less than size of header");
172  }
173  r = fread(GET_PKT_DATA(p), rlen - sizeof(DagRecord), 1, etv->erf);
174  if (r < 1) {
175  if (feof(etv->erf)) {
176  SCLogInfo("End of ERF file reached");
177  }
178  else {
179  SCLogInfo("Error reading ERF record");
180  }
182  }
183 
184  /* Only support ethernet at this time. */
185  if (dr.type != DAG_TYPE_ETH) {
187  "DAG record type %d not implemented.", dr.type);
189  }
190 
191  GET_PKT_LEN(p) = wlen;
193 
194  /* Convert ERF time to timeval - from libpcap. */
195  uint64_t ts = dr.ts;
196  p->ts.tv_sec = ts >> 32;
197  ts = (ts & 0xffffffffULL) * 1000000;
198  ts += 0x80000000; /* rounding */
199  p->ts.tv_usec = ts >> 32;
200  if (p->ts.tv_usec >= 1000000) {
201  p->ts.tv_usec -= 1000000;
202  p->ts.tv_sec++;
203  }
204 
205  etv->pkts++;
206  etv->bytes += wlen;
207 
209 }
210 
211 /**
212  * \brief Initialize the ERF receiver thread.
213  */
214 TmEcode
215 ReceiveErfFileThreadInit(ThreadVars *tv, const void *initdata, void **data)
216 {
217  SCEnter();
218 
219  if (initdata == NULL) {
220  SCLogError(SC_ERR_INVALID_ARGUMENT, "Error: No filename provided.");
222  }
223 
224  FILE *erf = fopen((const char *)initdata, "r");
225  if (erf == NULL) {
226  SCLogError(SC_ERR_FOPEN, "Failed to open %s: %s", (char *)initdata,
227  strerror(errno));
228  exit(EXIT_FAILURE);
229  }
230 
232  if (unlikely(etv == NULL)) {
233  SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocate memory for ERF file thread vars.");
234  fclose(erf);
236  }
237  memset(etv, 0, sizeof(*etv));
238  etv->erf = erf;
239  etv->tv = tv;
240  *data = (void *)etv;
241 
242  SCLogInfo("Processing ERF file %s", (char *)initdata);
243 
245 }
246 
247 /**
248  * \brief Initialize the ERF decoder thread.
249  */
250 TmEcode
251 DecodeErfFileThreadInit(ThreadVars *tv, const void *initdata, void **data)
252 {
253  SCEnter();
254  DecodeThreadVars *dtv = NULL;
256 
257  if (dtv == NULL)
259 
261 
262  *data = (void *)dtv;
263 
265 }
266 
267 TmEcode DecodeErfFileThreadDeinit(ThreadVars *tv, void *data)
268 {
269  if (data != NULL)
270  DecodeThreadVarsFree(tv, data);
272 }
273 
274 /**
275  * \brief Decode the ERF file.
276  *
277  * This function ups the decoder counters and then passes the packet
278  * off to the ethernet decoder.
279  */
280 TmEcode
281 DecodeErfFile(ThreadVars *tv, Packet *p, void *data)
282 {
283  SCEnter();
285 
287 
288  /* Update counters. */
290 
292 
294 
296 }
297 
298 /**
299  * \brief Print some stats to the log at program exit.
300  *
301  * \param tv Pointer to ThreadVars.
302  * \param data Pointer to data, ErfFileThreadVars.
303  */
304 void
306 {
307  ErfFileThreadVars *etv = (ErfFileThreadVars *)data;
308 
309  SCLogInfo("Packets: %"PRIu32"; Bytes: %"PRIu64, etv->pkts, etv->bytes);
310 }
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:67
TMM_RECEIVEERFFILE
@ TMM_RECEIVEERFFILE
Definition: tm-threads-common.h:50
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:1147
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:305
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:97
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:81
TmModule_::PktAcqLoop
TmEcode(* PktAcqLoop)(ThreadVars *, void *, void *)
Definition: tm-modules.h:54
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
Packet_::datalink
int datalink
Definition: decode.h:583
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1150
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:485
PKT_SRC_WIRE
@ PKT_SRC_WIRE
Definition: decode.h:51
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:231
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:414
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:230
TmSlot_
Definition: tm-threads.h:52
DagFlags
struct DagFlags_ DagFlags
TmEcode
TmEcode
Definition: tm-threads-common.h:79
TMM_DECODEERFFILE
@ TMM_DECODEERFFILE
Definition: tm-threads-common.h:51
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:659
Packet_::ts
struct timeval ts
Definition: decode.h:457
SCNtohs
#define SCNtohs(x)
Definition: suricata-common.h:400
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:631
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:640
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:215
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
ReceiveErfFileLoop
TmEcode ReceiveErfFileLoop(ThreadVars *, void *, void *)
ERF file reading loop.
Definition: source-erf-file.c:111
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:606
LINKTYPE_ETHERNET
#define LINKTYPE_ETHERNET
Definition: decode.h:1088
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:198
rlen
uint16_t rlen
Definition: source-erf-file.c:57