suricata
source-lib.c
Go to the documentation of this file.
1 /* Copyright (C) 2023-2024 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 /** \file
19  *
20  * \author Angelo Mirabella <angelo.mirabella@broadcom.com>
21  *
22  * LIB packet and stream decoding support
23  *
24  */
25 
26 #include "suricata-common.h"
27 #include "source-lib.h"
28 #include "util-device.h"
29 
30 /* Set time to the first packet timestamp when replaying a PCAP. */
31 static bool time_set = false;
32 
33 /** \brief initialize the "Decode" module.
34  *
35  * \param tv Pointer to the per-thread structure.
36  * \param initdata Pointer to initialization context.
37  * \param data Pointer to the initialized context.
38  * \return Error code.
39  */
40 static TmEcode DecodeLibThreadInit(ThreadVars *tv, const void *initdata, void **data)
41 {
42  SCEnter();
43  DecodeThreadVars *dtv = NULL;
44 
46 
47  if (dtv == NULL)
49 
51 
52  *data = (void *)dtv;
53 
55 }
56 
57 /** \brief deinitialize the "Decode" module.
58  *
59  * \param tv Pointer to the per-thread structure.
60  * \param data Pointer to the context.
61  * \return Error code.
62  */
63 static TmEcode DecodeLibThreadDeinit(ThreadVars *tv, void *data)
64 {
65  if (data != NULL)
66  DecodeThreadVarsFree(tv, data);
67 
68  time_set = false;
70 }
71 
72 /** \brief main decoding function.
73  *
74  * This method receives a packet and tries to identify layer 2 to 4 layers.
75  *
76  * \param tv Pointer to the per-thread structure.
77  * \param p Pointer to the packet.
78  * \param data Pointer to the context.
79  * \return Error code.
80  */
81 static TmEcode DecodeLib(ThreadVars *tv, Packet *p, void *data)
82 {
83  SCEnter();
85 
87 
88  /* update counters */
90 
91  /* If suri has set vlan during reading, we increase vlan counter */
92  if (p->vlan_idx) {
94  }
95 
96  /* call the decoder */
97  DecodeLinkLayer(tv, dtv, p->datalink, p, GET_PKT_DATA(p), GET_PKT_LEN(p));
98 
100 
102 }
103 
104 /** \brief register a "Decode" module for suricata as a library.
105  *
106  * The "Decode" module is the first module invoked when processing a packet */
108 {
109  tmm_modules[TMM_DECODELIB].name = "DecodeLib";
110  tmm_modules[TMM_DECODELIB].ThreadInit = DecodeLibThreadInit;
111  tmm_modules[TMM_DECODELIB].Func = DecodeLib;
113  tmm_modules[TMM_DECODELIB].ThreadDeinit = DecodeLibThreadDeinit;
116 }
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:73
StatsIncr
void StatsIncr(ThreadVars *tv, uint16_t id)
Increments the local counter.
Definition: counters.c:166
TMM_DECODELIB
@ TMM_DECODELIB
Definition: tm-threads-common.h:67
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1297
Packet_::vlan_idx
uint8_t vlan_idx
Definition: decode.h:512
PacketDecodeFinalize
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
Definition: decode.c:206
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
Packet_::datalink
int datalink
Definition: decode.h:622
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:602
util-device.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:209
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
TmModule_::Func
TmEcode(* Func)(ThreadVars *, Packet *, void *)
Definition: tm-modules.h:52
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:309
Packet_
Definition: decode.h:484
TM_FLAG_DECODE_TM
#define TM_FLAG_DECODE_TM
Definition: tm-modules.h:33
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:29
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:208
source-lib.h
TmEcode
TmEcode
Definition: tm-threads-common.h:80
TmModule_::name
const char * name
Definition: tm-modules.h:44
DecodeThreadVars_::counter_vlan
uint16_t counter_vlan
Definition: decode.h:983
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:33
DecodeThreadVarsFree
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
Definition: decode.c:793
suricata-common.h
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
TmModule_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:48
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:946
DecodeThreadVarsAlloc
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:775
TmModuleDecodeLibRegister
void TmModuleDecodeLibRegister(void)
register a "Decode" module for suricata as a library.
Definition: source-lib.c:107
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:76
DecodeUpdatePacketCounters
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
Definition: decode.c:741