suricata
output-packet.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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 Victor Julien <victor@inliniac.net>
22  *
23  * Packet Logger Output registration functions
24  */
25 
26 #include "suricata-common.h"
27 #include "output.h"
28 #include "output-packet.h"
29 #include "util-profiling.h"
30 #include "util-validate.h"
31 
32 /** per thread data for this module, contains a list of per thread
33  * data for the packet loggers. */
37 
38 /* logger instance, a module + a output ctx,
39  * it's perfectly valid that have multiple instances of the same
40  * log module (e.g. fast.log) with different output ctx'. */
41 typedef struct OutputPacketLogger_ {
46  const char *name;
52 
53 static OutputPacketLogger *list = NULL;
54 
60 {
61  OutputPacketLogger *op = SCMalloc(sizeof(*op));
62  if (op == NULL)
63  return -1;
64  memset(op, 0x00, sizeof(*op));
65 
66  op->LogFunc = LogFunc;
68  op->output_ctx = output_ctx;
69  op->name = name;
70  op->ThreadInit = ThreadInit;
73  op->logger_id = logger_id;
74 
75  if (list == NULL)
76  list = op;
77  else {
78  OutputPacketLogger *t = list;
79  while (t->next)
80  t = t->next;
81  t->next = op;
82  }
83 
84  SCLogDebug("OutputRegisterPacketLogger happy");
85  return 0;
86 }
87 
88 static TmEcode OutputPacketLog(ThreadVars *tv, Packet *p, void *thread_data)
89 {
90  DEBUG_VALIDATE_BUG_ON(thread_data == NULL);
91 
92  if (list == NULL) {
93  /* No child loggers. */
94  return TM_ECODE_OK;
95  }
96 
97  OutputPacketLoggerThreadData *op_thread_data = (OutputPacketLoggerThreadData *)thread_data;
98  OutputPacketLogger *logger = list;
99  OutputLoggerThreadStore *store = op_thread_data->store;
100 
101  DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
102  DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
103  DEBUG_VALIDATE_BUG_ON(logger == NULL && store == NULL);
104 
105  while (logger && store) {
106  DEBUG_VALIDATE_BUG_ON(logger->LogFunc == NULL || logger->ConditionFunc == NULL);
107 
108  if ((logger->ConditionFunc(tv, store->thread_data, (const Packet *)p)) == TRUE) {
110  logger->LogFunc(tv, store->thread_data, (const Packet *)p);
112  }
113 
114  logger = logger->next;
115  store = store->next;
116 
117  DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
118  DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
119  }
120 
121  return TM_ECODE_OK;
122 }
123 
124 /** \brief thread init for the packet logger
125  * This will run the thread init functions for the individual registered
126  * loggers */
127 static TmEcode OutputPacketLogThreadInit(ThreadVars *tv, const void *initdata, void **data)
128 {
129  OutputPacketLoggerThreadData *td = SCMalloc(sizeof(*td));
130  if (td == NULL)
131  return TM_ECODE_FAILED;
132  memset(td, 0x00, sizeof(*td));
133 
134  *data = (void *)td;
135 
136  SCLogDebug("OutputPacketLogThreadInit happy (*data %p)", *data);
137 
138  OutputPacketLogger *logger = list;
139  while (logger) {
140  if (logger->ThreadInit) {
141  void *retptr = NULL;
142  if (logger->ThreadInit(tv, (void *)logger->output_ctx, &retptr) == TM_ECODE_OK) {
143  OutputLoggerThreadStore *ts = SCMalloc(sizeof(*ts));
144 /* todo */ BUG_ON(ts == NULL);
145  memset(ts, 0x00, sizeof(*ts));
146 
147  /* store thread handle */
148  ts->thread_data = retptr;
149 
150  if (td->store == NULL) {
151  td->store = ts;
152  } else {
153  OutputLoggerThreadStore *tmp = td->store;
154  while (tmp->next != NULL)
155  tmp = tmp->next;
156  tmp->next = ts;
157  }
158 
159  SCLogDebug("%s is now set up", logger->name);
160  }
161  }
162 
163  logger = logger->next;
164  }
165 
166  return TM_ECODE_OK;
167 }
168 
169 static TmEcode OutputPacketLogThreadDeinit(ThreadVars *tv, void *thread_data)
170 {
171  OutputPacketLoggerThreadData *op_thread_data = (OutputPacketLoggerThreadData *)thread_data;
172  OutputLoggerThreadStore *store = op_thread_data->store;
173  OutputPacketLogger *logger = list;
174 
175  while (logger && store) {
176  if (logger->ThreadDeinit) {
177  logger->ThreadDeinit(tv, store->thread_data);
178  }
179 
180  OutputLoggerThreadStore *next_store = store->next;
181  SCFree(store);
182  store = next_store;
183 
184  logger = logger->next;
185  }
186 
187  SCFree(op_thread_data);
188  return TM_ECODE_OK;
189 }
190 
191 static void OutputPacketLogExitPrintStats(ThreadVars *tv, void *thread_data)
192 {
193  OutputPacketLoggerThreadData *op_thread_data = (OutputPacketLoggerThreadData *)thread_data;
194  OutputLoggerThreadStore *store = op_thread_data->store;
195  OutputPacketLogger *logger = list;
196 
197  while (logger && store) {
198  if (logger->ThreadExitPrintStats) {
199  logger->ThreadExitPrintStats(tv, store->thread_data);
200  }
201 
202  logger = logger->next;
203  store = store->next;
204  }
205 }
206 
207 static uint32_t OutputPacketLoggerGetActiveCount(void)
208 {
209  uint32_t cnt = 0;
210  for (OutputPacketLogger *p = list; p != NULL; p = p->next) {
211  cnt++;
212  }
213  return cnt;
214 }
215 
217 {
218  OutputRegisterRootLogger(OutputPacketLogThreadInit,
219  OutputPacketLogThreadDeinit, OutputPacketLogExitPrintStats,
220  OutputPacketLog, OutputPacketLoggerGetActiveCount);
221 }
222 
224 {
225  OutputPacketLogger *logger = list;
226  while (logger) {
227  OutputPacketLogger *next_logger = logger->next;
228  SCFree(logger);
229  logger = next_logger;
230  }
231 
232  /* reset list pointer */
233  list = NULL;
234 }
OutputPacketLoggerThreadData_
Definition: output-packet.c:34
OutputPacketLogger_::ThreadDeinit
ThreadDeinitFunc ThreadDeinit
Definition: output-packet.c:49
OutputPacketLogger_::next
struct OutputPacketLogger_ * next
Definition: output-packet.c:45
ts
uint64_t ts
Definition: source-erf-file.c:55
OutputLoggerThreadStore_
Definition: output.h:33
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:296
LoggerId
LoggerId
Definition: suricata-common.h:449
OutputLoggerThreadStore_::next
struct OutputLoggerThreadStore_ * next
Definition: output.h:35
output-packet.h
PacketLogger
int(* PacketLogger)(ThreadVars *, void *thread_data, const Packet *)
Definition: output-packet.h:30
OutputPacketLogger_::output_ctx
OutputCtx * output_ctx
Definition: output-packet.c:44
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:78
OutputPacketLogger_::ThreadExitPrintStats
ThreadExitPrintStatsFunc ThreadExitPrintStats
Definition: output-packet.c:50
OutputLoggerThreadStore_::thread_data
void * thread_data
Definition: output.h:34
OutputPacketShutdown
void OutputPacketShutdown(void)
Definition: output-packet.c:223
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
ThreadInitFunc
TmEcode(* ThreadInitFunc)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:39
TRUE
#define TRUE
Definition: suricata-common.h:33
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:289
OutputPacketLogger_::ConditionFunc
PacketLogCondition ConditionFunc
Definition: output-packet.c:43
util-profiling.h
Packet_
Definition: decode.h:428
OutputPacketLoggerRegister
void OutputPacketLoggerRegister(void)
Definition: output-packet.c:216
OutputPacketLogger_::name
const char * name
Definition: output-packet.c:46
OutputPacketLoggerThreadData_::store
OutputLoggerThreadStore * store
Definition: output-packet.c:35
TmEcode
TmEcode
Definition: tm-threads-common.h:83
PACKET_PROFILING_LOGGER_END
#define PACKET_PROFILING_LOGGER_END(p, id)
Definition: util-profiling.h:261
suricata-common.h
OutputPacketLogger
struct OutputPacketLogger_ OutputPacketLogger
ThreadExitPrintStatsFunc
void(* ThreadExitPrintStatsFunc)(ThreadVars *, void *)
Definition: tm-modules.h:41
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
util-validate.h
PacketLogCondition
int(* PacketLogCondition)(ThreadVars *, void *thread_data, const Packet *)
Definition: output-packet.h:35
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
Packet_::next
struct Packet_ * next
Definition: decode.h:603
SCFree
#define SCFree(p)
Definition: util-mem.h:61
PACKET_PROFILING_LOGGER_START
#define PACKET_PROFILING_LOGGER_START(p, id)
Definition: util-profiling.h:254
OutputRegisterRootLogger
void OutputRegisterRootLogger(ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats, OutputLogFunc LogFunc, OutputGetActiveCountFunc ActiveCntFunc)
Definition: output.c:963
OutputPacketLogger_
Definition: output-packet.c:41
OutputPacketLoggerThreadData
struct OutputPacketLoggerThreadData_ OutputPacketLoggerThreadData
OutputPacketLogger_::ThreadInit
ThreadInitFunc ThreadInit
Definition: output-packet.c:48
OutputPacketLogger_::LogFunc
PacketLogger LogFunc
Definition: output-packet.c:42
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
output.h
ThreadDeinitFunc
TmEcode(* ThreadDeinitFunc)(ThreadVars *, void *)
Definition: tm-modules.h:40
OutputPacketLogger_::logger_id
LoggerId logger_id
Definition: output-packet.c:47
OutputRegisterPacketLogger
int OutputRegisterPacketLogger(LoggerId logger_id, const char *name, PacketLogger LogFunc, PacketLogCondition ConditionFunc, OutputCtx *output_ctx, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output-packet.c:55