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 = SCCalloc(1, sizeof(*op));
62  if (op == NULL)
63  return -1;
64 
65  op->LogFunc = LogFunc;
67  op->output_ctx = output_ctx;
68  op->name = name;
69  op->ThreadInit = ThreadInit;
72  op->logger_id = logger_id;
73 
74  if (list == NULL)
75  list = op;
76  else {
77  OutputPacketLogger *t = list;
78  while (t->next)
79  t = t->next;
80  t->next = op;
81  }
82 
83  SCLogDebug("OutputRegisterPacketLogger happy");
84  return 0;
85 }
86 
87 static TmEcode OutputPacketLog(ThreadVars *tv, Packet *p, void *thread_data)
88 {
89  DEBUG_VALIDATE_BUG_ON(thread_data == NULL);
90 
91  if (list == NULL) {
92  /* No child loggers. */
93  return TM_ECODE_OK;
94  }
95 
96  OutputPacketLoggerThreadData *op_thread_data = (OutputPacketLoggerThreadData *)thread_data;
97  OutputPacketLogger *logger = list;
98  OutputLoggerThreadStore *store = op_thread_data->store;
99 
100  DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
101  DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
102  DEBUG_VALIDATE_BUG_ON(logger == NULL && store == NULL);
103 
104  while (logger && store) {
105  DEBUG_VALIDATE_BUG_ON(logger->LogFunc == NULL || logger->ConditionFunc == NULL);
106 
107  if (logger->ConditionFunc(tv, store->thread_data, (const Packet *)p)) {
109  logger->LogFunc(tv, store->thread_data, (const Packet *)p);
111  }
112 
113  logger = logger->next;
114  store = store->next;
115 
116  DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
117  DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
118  }
119 
120  return TM_ECODE_OK;
121 }
122 
123 /** \brief thread init for the packet logger
124  * This will run the thread init functions for the individual registered
125  * loggers */
126 static TmEcode OutputPacketLogThreadInit(ThreadVars *tv, const void *initdata, void **data)
127 {
128  OutputPacketLoggerThreadData *td = SCCalloc(1, sizeof(*td));
129  if (td == NULL)
130  return TM_ECODE_FAILED;
131 
132  *data = (void *)td;
133 
134  SCLogDebug("OutputPacketLogThreadInit happy (*data %p)", *data);
135 
136  OutputPacketLogger *logger = list;
137  while (logger) {
138  if (logger->ThreadInit) {
139  void *retptr = NULL;
140  if (logger->ThreadInit(tv, (void *)logger->output_ctx, &retptr) == TM_ECODE_OK) {
141  OutputLoggerThreadStore *ts = SCCalloc(1, sizeof(*ts));
142  /* todo */ BUG_ON(ts == NULL);
143 
144  /* store thread handle */
145  ts->thread_data = retptr;
146 
147  if (td->store == NULL) {
148  td->store = ts;
149  } else {
150  OutputLoggerThreadStore *tmp = td->store;
151  while (tmp->next != NULL)
152  tmp = tmp->next;
153  tmp->next = ts;
154  }
155 
156  SCLogDebug("%s is now set up", logger->name);
157  }
158  }
159 
160  logger = logger->next;
161  }
162 
163  return TM_ECODE_OK;
164 }
165 
166 static TmEcode OutputPacketLogThreadDeinit(ThreadVars *tv, void *thread_data)
167 {
168  OutputPacketLoggerThreadData *op_thread_data = (OutputPacketLoggerThreadData *)thread_data;
169  OutputLoggerThreadStore *store = op_thread_data->store;
170  OutputPacketLogger *logger = list;
171 
172  while (logger && store) {
173  if (logger->ThreadDeinit) {
174  logger->ThreadDeinit(tv, store->thread_data);
175  }
176 
177  OutputLoggerThreadStore *next_store = store->next;
178  SCFree(store);
179  store = next_store;
180 
181  logger = logger->next;
182  }
183 
184  SCFree(op_thread_data);
185  return TM_ECODE_OK;
186 }
187 
188 static void OutputPacketLogExitPrintStats(ThreadVars *tv, void *thread_data)
189 {
190  OutputPacketLoggerThreadData *op_thread_data = (OutputPacketLoggerThreadData *)thread_data;
191  OutputLoggerThreadStore *store = op_thread_data->store;
192  OutputPacketLogger *logger = list;
193 
194  while (logger && store) {
195  if (logger->ThreadExitPrintStats) {
196  logger->ThreadExitPrintStats(tv, store->thread_data);
197  }
198 
199  logger = logger->next;
200  store = store->next;
201  }
202 }
203 
204 static uint32_t OutputPacketLoggerGetActiveCount(void)
205 {
206  uint32_t cnt = 0;
207  for (OutputPacketLogger *p = list; p != NULL; p = p->next) {
208  cnt++;
209  }
210  return cnt;
211 }
212 
214 {
215  OutputRegisterRootLogger(OutputPacketLogThreadInit,
216  OutputPacketLogThreadDeinit, OutputPacketLogExitPrintStats,
217  OutputPacketLog, OutputPacketLoggerGetActiveCount);
218 }
219 
221 {
222  OutputPacketLogger *logger = list;
223  while (logger) {
224  OutputPacketLogger *next_logger = logger->next;
225  SCFree(logger);
226  logger = next_logger;
227  }
228 
229  /* reset list pointer */
230  list = NULL;
231 }
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:269
LoggerId
LoggerId
Definition: suricata-common.h:460
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:85
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:220
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
ThreadInitFunc
TmEcode(* ThreadInitFunc)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:40
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
OutputPacketLogger_::ConditionFunc
PacketLogCondition ConditionFunc
Definition: output-packet.c:43
util-profiling.h
Packet_
Definition: decode.h:437
OutputPacketLoggerRegister
void OutputPacketLoggerRegister(void)
Definition: output-packet.c:213
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:241
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
suricata-common.h
OutputPacketLogger
struct OutputPacketLogger_ OutputPacketLogger
ThreadExitPrintStatsFunc
void(* ThreadExitPrintStatsFunc)(ThreadVars *, void *)
Definition: tm-modules.h:42
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
PacketLogCondition
bool(* PacketLogCondition)(ThreadVars *, void *thread_data, const Packet *)
Definition: output-packet.h:35
util-validate.h
Packet_::next
struct Packet_ * next
Definition: decode.h:616
SCFree
#define SCFree(p)
Definition: util-mem.h:61
PACKET_PROFILING_LOGGER_START
#define PACKET_PROFILING_LOGGER_START(p, id)
Definition: util-profiling.h:234
OutputRegisterRootLogger
void OutputRegisterRootLogger(ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats, OutputLogFunc LogFunc, OutputGetActiveCountFunc ActiveCntFunc)
Definition: output.c:836
OutputPacketLogger_
Definition: output-packet.c:41
OutputPacketLoggerThreadData
struct OutputPacketLoggerThreadData_ OutputPacketLoggerThreadData
OutputPacketLogger_::ThreadInit
ThreadInitFunc ThreadInit
Definition: output-packet.c:48
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
OutputPacketLogger_::LogFunc
PacketLogger LogFunc
Definition: output-packet.c:42
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:103
output.h
ThreadDeinitFunc
TmEcode(* ThreadDeinitFunc)(ThreadVars *, void *)
Definition: tm-modules.h:41
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