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-private.h"
29 
30 /** \brief initialize the "Decode" module.
31  *
32  * \param tv Pointer to the per-thread structure.
33  * \param initdata Pointer to initialization context.
34  * \param data Pointer to the initialized context.
35  * \return Error code.
36  */
37 static TmEcode DecodeLibThreadInit(ThreadVars *tv, const void *initdata, void **data)
38 {
39  SCEnter();
40  DecodeThreadVars *dtv = NULL;
41 
43 
44  if (dtv == NULL)
46 
48 
49  *data = (void *)dtv;
50 
52 }
53 
54 /** \brief deinitialize the "Decode" module.
55  *
56  * \param tv Pointer to the per-thread structure.
57  * \param data Pointer to the context.
58  * \return Error code.
59  */
60 static TmEcode DecodeLibThreadDeinit(ThreadVars *tv, void *data)
61 {
62  if (data != NULL)
63  DecodeThreadVarsFree(tv, data);
64 
66 }
67 
68 /** \brief main decoding function.
69  *
70  * This method receives a packet and tries to identify layer 2 to 4 layers.
71  *
72  * \param tv Pointer to the per-thread structure.
73  * \param p Pointer to the packet.
74  * \param data Pointer to the context.
75  * \return Error code.
76  */
77 static TmEcode DecodeLib(ThreadVars *tv, Packet *p, void *data)
78 {
79  SCEnter();
81 
83 
84  /* update counters */
86 
87  /* If suri has set vlan during reading, we increase vlan counter */
88  if (p->vlan_idx) {
90  }
91 
92  /* call the decoder */
93  DecodeLinkLayer(tv, dtv, p->datalink, p, GET_PKT_DATA(p), GET_PKT_LEN(p));
94 
96 
98 }
99 
100 /** \brief register a "Decode" module for suricata as a library.
101  *
102  * The "Decode" module is the first module invoked when processing a packet */
104 {
105  tmm_modules[TMM_DECODELIB].name = "DecodeLib";
106  tmm_modules[TMM_DECODELIB].ThreadInit = DecodeLibThreadInit;
107  tmm_modules[TMM_DECODELIB].Func = DecodeLib;
109  tmm_modules[TMM_DECODELIB].ThreadDeinit = DecodeLibThreadDeinit;
112 }
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:77
util-device-private.h
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:1364
Packet_::vlan_idx
uint8_t vlan_idx
Definition: decode.h:544
p
Packet * p
Definition: fuzz_dataset.c:30
PacketDecodeFinalize
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Finalize decoding of a packet.
Definition: decode.c:234
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:53
Packet_::datalink
int datalink
Definition: decode.h:652
DecodeRegisterPerfCounters
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
Definition: decode.c:647
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:210
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
TmModule_::Func
TmEcode(* Func)(ThreadVars *, Packet *, void *)
Definition: tm-modules.h:56
StatsCounterIncr
void StatsCounterIncr(StatsThreadContext *stats, StatsCounterId id)
Increments the local counter.
Definition: counters.c:163
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:322
Packet_
Definition: decode.h:516
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:209
source-lib.h
TmEcode
TmEcode
Definition: tm-threads-common.h:80
TmModule_::name
const char * name
Definition: tm-modules.h:48
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:35
DecodeThreadVarsFree
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
Definition: decode.c:861
suricata-common.h
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:51
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
TmModule_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: tm-modules.h:52
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:995
DecodeThreadVarsAlloc
DecodeThreadVars * DecodeThreadVarsAlloc(ThreadVars *tv)
Alloc and setup DecodeThreadVars.
Definition: decode.c:843
TmModuleDecodeLibRegister
void TmModuleDecodeLibRegister(void)
register a "Decode" module for suricata as a library.
Definition: source-lib.c:103
DecodeThreadVars_::counter_vlan
StatsCounterId counter_vlan
Definition: decode.h:1039
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:80
DecodeUpdatePacketCounters
void DecodeUpdatePacketCounters(ThreadVars *tv, const DecodeThreadVars *dtv, const Packet *p)
Definition: decode.c:811