suricata
output-tx.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  * AppLayer TX Logger Output registration functions
24  */
25 
26 #include "suricata-common.h"
27 #include "output.h"
28 #include "output-tx.h"
29 #include "stream.h"
30 #include "app-layer.h"
31 #include "app-layer-parser.h"
32 #include "util-profiling.h"
33 #include "util-validate.h"
34 
35 /** per thread data for this module, contains a list of per thread
36  * data for the packet loggers. */
37 typedef struct OutputTxLoggerThreadData_ {
38  /* thread local data from file api */
40  /* thread local data from filedata api */
44 
45 /* logger instance, a module + a output ctx,
46  * it's perfectly valid that have multiple instances of the same
47  * log module (e.g. http.log) with different output ctx'. */
48 typedef struct OutputTxLogger_ {
54  const char *name;
56  uint32_t id;
59  TmEcode (*ThreadInit)(ThreadVars *, const void *, void **);
61  void (*ThreadExitPrintStats)(ThreadVars *, void *);
63 
64 static OutputTxLogger *list[ALPROTO_MAX] = { NULL };
65 
72  void (*ThreadExitPrintStats)(ThreadVars *, void *))
73 {
75  SCLogDebug(
76  "%s logger not enabled: protocol %s is disabled", name, AppProtoToString(alproto));
77  return -1;
78  }
79  OutputTxLogger *op = SCCalloc(1, sizeof(*op));
80  if (op == NULL)
81  return -1;
82 
83  op->alproto = alproto;
84  op->LogFunc = LogFunc;
86  op->output_ctx = output_ctx;
87  op->name = name;
88  op->logger_id = id;
89  op->ThreadInit = ThreadInit;
92 
93  if (alproto == ALPROTO_UNKNOWN) {
94  op->tc_log_progress = 0;
95  } else if (tc_log_progress < 0) {
96  op->tc_log_progress =
98  STREAM_TOCLIENT);
99  } else {
101  }
102 
103  if (alproto == ALPROTO_UNKNOWN) {
104  op->ts_log_progress = 0;
105  } else if (ts_log_progress < 0) {
106  op->ts_log_progress =
108  STREAM_TOSERVER);
109  } else {
111  }
112 
113  if (list[alproto] == NULL) {
114  op->id = 1;
115  list[alproto] = op;
116  } else {
117  OutputTxLogger *t = list[alproto];
118  while (t->next)
119  t = t->next;
120  if (t->id * 2ULL > UINT32_MAX) {
121  FatalError("Too many loggers registered.");
122  }
123  op->id = t->id * 2;
124  t->next = op;
125  }
126 
127  SCLogDebug("OutputRegisterTxLogger happy");
128  return 0;
129 }
130 
131 extern bool g_file_logger_enabled;
132 extern bool g_filedata_logger_enabled;
133 
134 /** \brief run the per tx file logging
135  * \todo clean up various end of tx/stream etc indicators
136  */
137 static inline void OutputTxLogFiles(ThreadVars *tv, OutputFileLoggerThreadData *file_td,
138  OutputFiledataLoggerThreadData *filedata_td, Packet *p, Flow *f, void *tx,
139  const uint64_t tx_id, AppLayerTxData *txd, const bool tx_complete, const bool ts_ready,
140  const bool tc_ready, const bool ts_eof, const bool tc_eof, const bool eof)
141 {
142  uint8_t packet_dir;
143  uint8_t opposing_dir;
144  bool packet_dir_ready;
145  const bool opposing_dir_ready = eof;
146  bool opposing_tx_ready;
147  if (p->flowflags & FLOW_PKT_TOSERVER) {
148  packet_dir = STREAM_TOSERVER;
149  opposing_dir = STREAM_TOCLIENT;
150  packet_dir_ready = eof | ts_ready | ts_eof;
151  opposing_tx_ready = tc_ready;
152  } else if (p->flowflags & FLOW_PKT_TOCLIENT) {
153  packet_dir = STREAM_TOCLIENT;
154  opposing_dir = STREAM_TOSERVER;
155  packet_dir_ready = eof | tc_ready | tc_eof;
156  opposing_tx_ready = ts_ready;
157  } else {
158  abort();
159  }
160 
161  SCLogDebug("eof %d ts_ready %d ts_eof %d", eof, ts_ready, ts_eof);
162  SCLogDebug("eof %d tc_ready %d tc_eof %d", eof, tc_ready, tc_eof);
163 
164  SCLogDebug("packet dir %s opposing %s packet_dir_ready %d opposing_dir_ready %d",
165  packet_dir == STREAM_TOSERVER ? "TOSERVER" : "TOCLIENT",
166  opposing_dir == STREAM_TOSERVER ? "TOSERVER" : "TOCLIENT", packet_dir_ready,
167  opposing_dir_ready);
168 
169  AppLayerGetFileState app_files = AppLayerParserGetTxFiles(f, tx, packet_dir);
170  FileContainer *ffc = app_files.fc;
171  AppLayerGetFileState app_files_opposing = AppLayerParserGetTxFiles(f, tx, opposing_dir);
172  FileContainer *ffc_opposing = app_files_opposing.fc;
173 
174  /* see if opposing side is finished: if no file support in this direction, of is not
175  * files and tx is done for opposing dir. */
176  bool opposing_finished =
177  ffc_opposing == NULL || (ffc_opposing->head == NULL && opposing_tx_ready);
178  SCLogDebug("opposing_finished %d ffc_opposing %p ffc_opposing->head %p opposing_tx_ready %d",
179  opposing_finished, ffc_opposing, ffc_opposing ? ffc_opposing->head : NULL,
180  opposing_tx_ready);
181 
182  if (ffc || ffc_opposing)
183  SCLogDebug("pcap_cnt %" PRIu64 " flow %p tx %p tx_id %" PRIu64
184  " ffc %p ffc_opposing %p tx_complete %d",
185  p->pcap_cnt, f, tx, tx_id, ffc, ffc_opposing, tx_complete);
186 
187  if (ffc) {
188  const bool file_close = ((p->flags & PKT_PSEUDO_STREAM_END)) | eof;
189  const bool file_trunc = StreamTcpReassembleDepthReached(p) | eof;
190  SCLogDebug("tx: calling files: ffc %p head %p file_close %d file_trunc %d", ffc, ffc->head,
191  file_close, file_trunc);
192  if (filedata_td && txd->files_opened > txd->files_stored)
193  OutputFiledataLogFfc(tv, filedata_td, p, app_files, tx, tx_id, txd, packet_dir,
194  file_close, file_trunc, packet_dir);
195  if (file_td && txd->files_opened > txd->files_logged)
197  tv, file_td, p, ffc, tx, tx_id, txd, file_close, file_trunc, packet_dir);
198  }
199  /* if EOF and we support files, do a final write out */
200  if (opposing_dir_ready && ffc_opposing != NULL) {
201  const bool file_close = ((p->flags & PKT_PSEUDO_STREAM_END)) | tx_complete | eof;
202  const bool file_trunc = StreamTcpReassembleDepthReached(p) | eof;
203  opposing_finished = true;
204  SCLogDebug("tx: calling for opposing direction files: file_close:%s file_trunc:%s",
205  file_close ? "true" : "false", file_trunc ? "true" : "false");
206  if (filedata_td && txd->files_opened > txd->files_stored)
207  OutputFiledataLogFfc(tv, filedata_td, p, app_files_opposing, tx, tx_id, txd,
208  opposing_dir, file_close, file_trunc, opposing_dir);
209  if (file_td && txd->files_opened > txd->files_logged)
210  OutputFileLogFfc(tv, file_td, p, ffc_opposing, tx, tx_id, txd, file_close, file_trunc,
211  opposing_dir);
212  }
213 
214  const bool tx_done = packet_dir_ready && opposing_finished;
215  SCLogDebug("tx_done %d packet_dir_ready %d opposing_finished %d", tx_done, packet_dir_ready,
216  opposing_finished);
217 
218  /* if not a file tx or if tx is done, set logger flags so tx can move on */
219  const bool is_file_tx = (ffc != NULL || ffc_opposing != NULL);
220  if (!is_file_tx || tx_done) {
221  SCLogDebug("is_file_tx %d tx_done %d", is_file_tx, tx_done);
222  if (file_td) {
223  txd->logged.flags |= BIT_U32(LOGGER_FILE);
224  SCLogDebug("setting LOGGER_FILE => %08x", txd->logged.flags);
225  }
226  if (filedata_td) {
227  txd->logged.flags |= BIT_U32(LOGGER_FILEDATA);
228  SCLogDebug("setting LOGGER_FILEDATA => %08x", txd->logged.flags);
229  }
230  } else {
231  SCLogDebug("pcap_cnt %" PRIu64 " flow %p tx %p tx_id %" PRIu64
232  " NOT SETTING FILE FLAGS ffc %p ffc_opposing %p tx_complete %d",
233  p->pcap_cnt, f, tx, tx_id, ffc, ffc_opposing, tx_complete);
234  }
235 }
236 
237 static void OutputTxLogList0(ThreadVars *tv, OutputTxLoggerThreadData *op_thread_data, Packet *p,
238  Flow *f, void *tx, const uint64_t tx_id)
239 {
240  const OutputTxLogger *logger = list[ALPROTO_UNKNOWN];
241  const OutputLoggerThreadStore *store = op_thread_data->store[ALPROTO_UNKNOWN];
242 
243  DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
244  DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
245  DEBUG_VALIDATE_BUG_ON(logger == NULL && store == NULL);
246 
247  while (logger && store) {
248  DEBUG_VALIDATE_BUG_ON(logger->LogFunc == NULL);
249 
250  SCLogDebug("logger %p", logger);
251 
252  /* always invoke "wild card" tx loggers */
253  SCLogDebug("Logging tx_id %"PRIu64" to logger %d", tx_id, logger->logger_id);
255  logger->LogFunc(tv, store->thread_data, p, f, f->alstate, tx, tx_id);
257 
258  logger = logger->next;
259  store = store->next;
260 
261  DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
262  DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
263  }
264 }
265 
266 struct Ctx {
267  uint32_t tx_logged_old;
268  uint32_t tx_logged;
269 };
270 
271 static void OutputTxLogCallLoggers(ThreadVars *tv, OutputTxLoggerThreadData *op_thread_data,
272  const OutputTxLogger *logger, const OutputLoggerThreadStore *store, Packet *p, Flow *f,
273  void *alstate, void *tx, const uint64_t tx_id, const AppProto alproto, const bool eof,
274  const int tx_progress_ts, const int tx_progress_tc, struct Ctx *ctx)
275 {
276  DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
277  DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
278  // DEBUG_VALIDATE_BUG_ON(logger == NULL && store == NULL);
279 
280  while (logger && store) {
281  DEBUG_VALIDATE_BUG_ON(logger->LogFunc == NULL);
282  DEBUG_VALIDATE_BUG_ON(logger->alproto != alproto);
283 
284  SCLogDebug("logger %p, Alproto %d LogCondition %p, ts_log_progress %d "
285  "tc_log_progress %d",
286  logger, logger->alproto, logger->LogCondition, logger->ts_log_progress,
287  logger->tc_log_progress);
288  if ((ctx->tx_logged_old & BIT_U32(logger->logger_id)) == 0) {
289  SCLogDebug("alproto match %d, logging tx_id %" PRIu64, logger->alproto, tx_id);
290 
291  SCLogDebug("pcap_cnt %" PRIu64 ", tx_id %" PRIu64 " logger %d. EOF %s", p->pcap_cnt,
292  tx_id, logger->logger_id, eof ? "true" : "false");
293 
294  if (eof) {
295  SCLogDebug("EOF, so log now");
296  } else {
297  if (logger->LogCondition) {
298  if (!logger->LogCondition(tv, p, alstate, tx, tx_id)) {
299  SCLogDebug("conditions not met, not logging");
300  goto next_logger;
301  }
302  } else {
303  if (tx_progress_tc < logger->tc_log_progress) {
304  SCLogDebug("progress not far enough, not logging");
305  goto next_logger;
306  }
307 
308  if (tx_progress_ts < logger->ts_log_progress) {
309  SCLogDebug("progress not far enough, not logging");
310  goto next_logger;
311  }
312  }
313  }
314 
315  SCLogDebug("Logging tx_id %" PRIu64 " to logger %d", tx_id, logger->logger_id);
317  logger->LogFunc(tv, store->thread_data, p, f, alstate, tx, tx_id);
319 
320  ctx->tx_logged |= BIT_U32(logger->logger_id);
321  }
322 
323  next_logger:
324  logger = logger->next;
325  store = store->next;
326 
327  DEBUG_VALIDATE_BUG_ON(logger == NULL && store != NULL);
328  DEBUG_VALIDATE_BUG_ON(logger != NULL && store == NULL);
329  }
330 }
331 
332 static TmEcode OutputTxLog(ThreadVars *tv, Packet *p, void *thread_data)
333 {
334  DEBUG_VALIDATE_BUG_ON(thread_data == NULL);
335  if (p->flow == NULL)
336  return TM_ECODE_OK;
337  if (!PKT_IS_PSEUDOPKT(p) && p->app_update_direction == 0 &&
338  ((PKT_IS_TOSERVER(p) && (p->flow->flags & FLOW_TS_APP_UPDATED) == 0) ||
339  (PKT_IS_TOCLIENT(p) && (p->flow->flags & FLOW_TC_APP_UPDATED) == 0))) {
340  SCLogDebug("not pseudo, no app update: skip");
341  return TM_ECODE_OK;
342  }
343  if ((p->flags & PKT_STREAM_EST) == 0 && p->proto == IPPROTO_TCP) {
344  return TM_ECODE_OK;
345  }
346  SCLogDebug("pseudo, or app update: run output");
347 
348  OutputTxLoggerThreadData *op_thread_data = (OutputTxLoggerThreadData *)thread_data;
349 
350  Flow * const f = p->flow;
351  const uint8_t ipproto = f->proto;
352  const AppProto alproto = f->alproto;
353  SCLogDebug("pcap_cnt %u tx logging %u/%s", (uint32_t)p->pcap_cnt, alproto,
354  AppProtoToString(alproto));
355 
356  const bool file_logging_active = (op_thread_data->file || op_thread_data->filedata);
357  if (!file_logging_active) {
358  if (list[alproto] == NULL && list[ALPROTO_UNKNOWN] == NULL) {
359  SCLogDebug("bail");
360  /* No child loggers registered. */
361  return TM_ECODE_OK;
362  }
363  if (AppLayerParserProtocolHasLogger(ipproto, alproto) == 0)
364  goto end;
365  }
366  void *alstate = f->alstate;
367  if (alstate == NULL) {
368  SCLogDebug("no alstate");
369  goto end;
370  }
371  const LoggerId logger_expectation = AppLayerParserProtocolGetLoggerBits(ipproto, alproto);
372  if (logger_expectation == 0) {
373  SCLogDebug("bail: logger_expectation %u. LOGGER_FILE %u LOGGER_FILEDATA %u",
374  logger_expectation, LOGGER_FILE, LOGGER_FILEDATA);
375  goto end;
376  }
377  SCLogDebug("pcap_cnt %" PRIu64, p->pcap_cnt);
378 
379  const bool last_pseudo = (p->flowflags & FLOW_PKT_LAST_PSEUDO) != 0;
380  const bool ts_eof = AppLayerParserStateIssetFlag(f->alparser, APP_LAYER_PARSER_EOF_TS) != 0;
381  const bool tc_eof = AppLayerParserStateIssetFlag(f->alparser, APP_LAYER_PARSER_EOF_TC) != 0;
382 
383  const bool eof = last_pseudo || (ts_eof && tc_eof);
384  SCLogDebug("eof %d last_pseudo %d ts_eof %d tc_eof %d", eof, last_pseudo, ts_eof, tc_eof);
385 
386  const uint8_t ts_disrupt_flags = FlowGetDisruptionFlags(f, STREAM_TOSERVER);
387  const uint8_t tc_disrupt_flags = FlowGetDisruptionFlags(f, STREAM_TOCLIENT);
388  SCLogDebug("ts_disrupt_flags %02x tc_disrupt_flags %02x", ts_disrupt_flags, tc_disrupt_flags);
389  const uint64_t total_txs = AppLayerParserGetTxCnt(f, alstate);
390  uint64_t tx_id = AppLayerParserGetTransactionLogId(f->alparser);
391  uint64_t max_id = tx_id;
392  int logged = 0;
393  int gap = 0;
394  const bool support_files = AppLayerParserSupportsFiles(ipproto, alproto);
395  const uint8_t pkt_dir = STREAM_FLAGS_FOR_PACKET(p);
396 
397  SCLogDebug("pcap_cnt %" PRIu64 ": tx_id %" PRIu64 " total_txs %" PRIu64, p->pcap_cnt, tx_id,
398  total_txs);
399 
400  AppLayerGetTxIteratorFunc IterFunc = AppLayerGetTxIterator(ipproto, alproto);
402  memset(&state, 0, sizeof(state));
403 
404  const int complete_ts =
405  AppLayerParserGetStateProgressCompletionStatus(alproto, STREAM_TOSERVER);
406  const int complete_tc =
407  AppLayerParserGetStateProgressCompletionStatus(alproto, STREAM_TOCLIENT);
408  while (1) {
409  AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, tx_id, total_txs, &state);
410  if (ires.tx_ptr == NULL)
411  break;
412  void * const tx = ires.tx_ptr;
413  tx_id = ires.tx_id;
414  SCLogDebug("STARTING tx_id %" PRIu64 ", tx %p", tx_id, tx);
415 
416  const int tx_progress_ts =
417  AppLayerParserGetStateProgress(ipproto, alproto, tx, ts_disrupt_flags);
418  const int tx_progress_tc =
419  AppLayerParserGetStateProgress(ipproto, alproto, tx, tc_disrupt_flags);
420  const bool tx_complete = (tx_progress_ts == complete_ts && tx_progress_tc == complete_tc);
421 
422  SCLogDebug("file_thread_data %p filedata_thread_data %p", op_thread_data->file,
423  op_thread_data->filedata);
424 
425  AppLayerTxData *txd = AppLayerParserGetTxData(ipproto, alproto, tx);
426  if (unlikely(txd == NULL)) {
427  SCLogDebug("NO TXD");
428  /* make sure this tx, which can't be properly logged is skipped */
429  logged = 1;
430  max_id = tx_id;
431  goto next_tx;
432  }
433 
434  if (file_logging_active) {
435  if (AppLayerParserIsFileTx(txd)) { // need to process each tx that might be a file tx,
436  // even if there are not files (yet)
437  const bool ts_ready = (tx_progress_ts == complete_ts);
438  const bool tc_ready = (tx_progress_tc == complete_tc);
439  SCLogDebug("ts_ready %d tc_ready %d", ts_ready, tc_ready);
440 
441  const bool eval_files = ts_ready | tc_ready | tx_complete | ts_eof | tc_eof | eof;
442 
443  SCLogDebug("eval_files: %u, ts_ready %u, tc_ready %u, tx_complete %u, ts_eof %u, "
444  "tc_eof %u, eof %u",
445  eval_files, ts_ready, tc_ready, tx_complete, ts_eof, tc_eof, eof);
446  SCLogDebug("txd->file_tx & pkt_dir: %02x & %02x -> %02x", txd->file_tx, pkt_dir,
447  (txd->file_tx & pkt_dir));
448 
449  /* call only for the correct direction, except when it looks anything like a end of
450  * transaction or end of stream. Since OutputTxLogFiles has complicated logic around
451  * that, we just leave it to that function to sort things out for now. */
452  if (eval_files || AppLayerParserIsFileTxInDir(txd, pkt_dir)) {
453  OutputTxLogFiles(tv, op_thread_data->file, op_thread_data->filedata, p, f, tx,
454  tx_id, txd, tx_complete, ts_ready, tc_ready, ts_eof, tc_eof, eof);
455  }
456  } else if (support_files) {
457  if (op_thread_data->file) {
458  txd->logged.flags |= BIT_U32(LOGGER_FILE);
459  SCLogDebug("not a file_tx: setting LOGGER_FILE => %08x", txd->logged.flags);
460  }
461  if (op_thread_data->filedata) {
462  txd->logged.flags |= BIT_U32(LOGGER_FILEDATA);
463  SCLogDebug("not a file_tx: setting LOGGER_FILEDATA => %08x", txd->logged.flags);
464  }
465  }
466  }
467  SCLogDebug("logger: expect %08x, have %08x", logger_expectation, txd->logged.flags);
468 
469  if (list[ALPROTO_UNKNOWN] != 0) {
470  OutputTxLogList0(tv, op_thread_data, p, f, tx, tx_id);
471  if (list[alproto] == NULL)
472  goto next_tx;
473  }
474 
475  SCLogDebug("tx %p/%" PRIu64 " txd %p: log_flags %x logger_expectation %x", tx, tx_id, txd,
476  txd->config.log_flags, logger_expectation);
477  if (txd->config.log_flags & BIT_U8(CONFIG_TYPE_TX)) {
478  SCLogDebug("SKIP tx %p/%"PRIu64, tx, tx_id);
479  // so that AppLayerParserTransactionsCleanup can clean this tx
480  txd->logged.flags |= logger_expectation;
481  goto next_tx;
482  }
483 
484  if (txd->logged.flags == logger_expectation) {
485  SCLogDebug("fully logged");
486  /* tx already fully logged */
487  goto next_tx;
488  }
489 
490  SCLogDebug("logger: expect %08x, have %08x", logger_expectation, txd->logged.flags);
491  const OutputTxLogger *logger = list[alproto];
492  const OutputLoggerThreadStore *store = op_thread_data->store[alproto];
493  struct Ctx ctx = { .tx_logged = txd->logged.flags, .tx_logged_old = txd->logged.flags };
494  SCLogDebug("logger: expect %08x, have %08x", logger_expectation, ctx.tx_logged);
495 
496  OutputTxLogCallLoggers(tv, op_thread_data, logger, store, p, f, alstate, tx, tx_id, alproto,
497  eof, tx_progress_ts, tx_progress_tc, &ctx);
498 
499  SCLogDebug("logger: expect %08x, have %08x", logger_expectation, ctx.tx_logged);
500  if (ctx.tx_logged != ctx.tx_logged_old) {
501  SCLogDebug("logger: storing %08x (was %08x)", ctx.tx_logged, ctx.tx_logged_old);
502  DEBUG_VALIDATE_BUG_ON(txd == NULL);
503  txd->logged.flags |= ctx.tx_logged;
504  }
505 
506  /* If all loggers logged set a flag and update the last tx_id
507  * that was logged.
508  *
509  * If not all loggers were logged we flag that there was a gap
510  * so any subsequent transactions in this loop don't increase
511  * the maximum ID that was logged. */
512  if (!gap && ctx.tx_logged == logger_expectation) {
513  SCLogDebug("no gap %d, %08x == %08x", gap, ctx.tx_logged, logger_expectation);
514  logged = 1;
515  max_id = tx_id;
516  SCLogDebug("max_id %" PRIu64, max_id);
517  } else {
518  gap = 1;
519  }
520 next_tx:
521  if (!ires.has_next)
522  break;
523  tx_id++;
524  }
525 
526  /* Update the last ID that has been logged with all
527  * transactions before it. */
528  if (logged) {
529  SCLogDebug("updating log tx_id %"PRIu64, max_id);
531  }
532 
533 end:
534  return TM_ECODE_OK;
535 }
536 
537 /** \brief thread init for the tx logger
538  * This will run the thread init functions for the individual registered
539  * loggers */
540 static TmEcode OutputTxLogThreadInit(ThreadVars *tv, const void *_initdata, void **data)
541 {
543  SCCalloc(1, sizeof(*td) + ALPROTO_MAX * sizeof(OutputLoggerThreadStore *));
544  if (td == NULL)
545  return TM_ECODE_FAILED;
546 
547  *data = (void *)td;
548  SCLogDebug("OutputTxLogThreadInit happy (*data %p)", *data);
549 
550  for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
551  OutputTxLogger *logger = list[alproto];
552  while (logger) {
553  if (logger->ThreadInit) {
554  void *retptr = NULL;
555  if (logger->ThreadInit(tv, (void *)logger->output_ctx, &retptr) == TM_ECODE_OK) {
556  OutputLoggerThreadStore *ts = SCCalloc(1, sizeof(*ts));
557  /* todo */ BUG_ON(ts == NULL);
558 
559  /* store thread handle */
560  ts->thread_data = retptr;
561 
562  if (td->store[alproto] == NULL) {
563  td->store[alproto] = ts;
564  } else {
565  OutputLoggerThreadStore *tmp = td->store[alproto];
566  while (tmp->next != NULL)
567  tmp = tmp->next;
568  tmp->next = ts;
569  }
570 
571  SCLogDebug("%s is now set up", logger->name);
572  }
573  }
574 
575  logger = logger->next;
576  }
577  }
578 
579  if (g_file_logger_enabled) {
580  if (OutputFileLogThreadInit(tv, &td->file) != TM_ECODE_OK) {
581  FatalError("failed to set up file thread data");
582  }
583  }
586  FatalError("failed to set up filedata thread data");
587  }
588  }
589 
590  SCLogDebug("file_thread_data %p filedata_thread_data %p", td->file, td->filedata);
591 
592  return TM_ECODE_OK;
593 }
594 
595 static TmEcode OutputTxLogThreadDeinit(ThreadVars *tv, void *thread_data)
596 {
597  OutputTxLoggerThreadData *op_thread_data = (OutputTxLoggerThreadData *)thread_data;
598 
599  for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
600  OutputLoggerThreadStore *store = op_thread_data->store[alproto];
601  OutputTxLogger *logger = list[alproto];
602 
603  while (logger && store) {
604  if (logger->ThreadDeinit) {
605  logger->ThreadDeinit(tv, store->thread_data);
606  }
607 
608  OutputLoggerThreadStore *next_store = store->next;
609  SCFree(store);
610  store = next_store;
611  logger = logger->next;
612  }
613  }
614 
615  if (op_thread_data->file) {
616  OutputFileLogThreadDeinit(tv, op_thread_data->file);
617  }
618  if (op_thread_data->filedata) {
619  OutputFiledataLogThreadDeinit(tv, op_thread_data->filedata);
620  }
621 
622  SCFree(op_thread_data);
623  return TM_ECODE_OK;
624 }
625 
626 static void OutputTxLogExitPrintStats(ThreadVars *tv, void *thread_data)
627 {
628  OutputTxLoggerThreadData *op_thread_data = (OutputTxLoggerThreadData *)thread_data;
629 
630  for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
631  OutputLoggerThreadStore *store = op_thread_data->store[alproto];
632  OutputTxLogger *logger = list[alproto];
633 
634  while (logger && store) {
635  if (logger->ThreadExitPrintStats) {
636  logger->ThreadExitPrintStats(tv, store->thread_data);
637  }
638 
639  logger = logger->next;
640  store = store->next;
641  }
642  }
643 }
644 
645 static uint32_t OutputTxLoggerGetActiveCount(void)
646 {
647  uint32_t cnt = 0;
648  for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
649  for (OutputTxLogger *p = list[alproto]; p != NULL; p = p->next) {
650  cnt++;
651  }
652  }
653 
654  if (g_file_logger_enabled) {
655  cnt++;
656  SCLogDebug("g_file_logger_enabled");
657  }
659  cnt++;
660  SCLogDebug("g_filedata_logger_enabled");
661  }
662 
663  return cnt;
664 }
665 
666 
668 {
669  OutputRegisterRootLogger(OutputTxLogThreadInit, OutputTxLogThreadDeinit,
670  OutputTxLogExitPrintStats, OutputTxLog, OutputTxLoggerGetActiveCount);
671 }
672 
674 {
675  for (AppProto alproto = 0; alproto < ALPROTO_MAX; alproto++) {
676  OutputTxLogger *logger = list[alproto];
677  while (logger) {
678  OutputTxLogger *next_logger = logger->next;
679  SCFree(logger);
680  logger = next_logger;
681  }
682  list[alproto] = NULL;
683  }
684 }
PKT_IS_TOCLIENT
#define PKT_IS_TOCLIENT(p)
Definition: decode.h:241
OutputFiledataLogThreadInit
TmEcode OutputFiledataLogThreadInit(ThreadVars *tv, OutputFiledataLoggerThreadData **data)
thread init for the filedata logger This will run the thread init functions for the individual regist...
Definition: output-filedata.c:204
OutputTxLogger_::alproto
AppProto alproto
Definition: output-tx.c:49
Packet_::proto
uint8_t proto
Definition: decode.h:501
FileContainer_
Definition: util-file.h:113
output-tx.h
ts
uint64_t ts
Definition: source-erf-file.c:55
OutputLoggerThreadStore_
Definition: output.h:33
AppLayerParserIsEnabled
int AppLayerParserIsEnabled(AppProto alproto)
simple way to globally test if a alproto is registered and fully enabled in the configuration.
Definition: app-layer-parser.c:1507
OutputTxLoggerRegister
void OutputTxLoggerRegister(void)
Definition: output-tx.c:667
AppLayerParserProtocolHasLogger
int AppLayerParserProtocolHasLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:1517
OutputFileLoggerThreadData_
Definition: output-file.h:33
AppLayerParserSetTransactionLogId
void AppLayerParserSetTransactionLogId(AppLayerParserState *pstate, uint64_t tx_id)
Definition: app-layer-parser.c:690
CONFIG_TYPE_TX
@ CONFIG_TYPE_TX
Definition: util-config.h:36
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1326
FLOW_PKT_LAST_PSEUDO
#define FLOW_PKT_LAST_PSEUDO
Definition: flow.h:241
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
OutputTxLogger_::tc_log_progress
int tc_log_progress
Definition: output-tx.c:57
StreamTcpReassembleDepthReached
int StreamTcpReassembleDepthReached(Packet *p)
check if stream in pkt direction has depth reached
Definition: stream-tcp-reassemble.c:615
LOGGER_FILEDATA
@ LOGGER_FILEDATA
Definition: suricata-common.h:470
OutputTxShutdown
void OutputTxShutdown(void)
Definition: output-tx.c:673
Ctx
Definition: output-tx.c:266
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Packet_::pcap_cnt
uint64_t pcap_cnt
Definition: decode.h:601
AppLayerGetTxIterator
AppLayerGetTxIteratorFunc AppLayerGetTxIterator(const uint8_t ipproto, const AppProto alproto)
Definition: app-layer-parser.c:675
Flow_::proto
uint8_t proto
Definition: flow.h:382
Ctx::tx_logged
uint32_t tx_logged
Definition: output-tx.c:268
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:84
AppLayerParserGetStateProgress
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
Definition: app-layer-parser.c:1067
Packet_::flags
uint32_t flags
Definition: decode.h:516
Flow_
Flow data structure.
Definition: flow.h:360
OutputFiledataLogThreadDeinit
TmEcode OutputFiledataLogThreadDeinit(ThreadVars *tv, OutputFiledataLoggerThreadData *op_thread_data)
Definition: output-filedata.c:250
LoggerId
LoggerId
Definition: suricata-common.h:460
AppLayerParserGetTransactionLogId
uint64_t AppLayerParserGetTransactionLogId(AppLayerParserState *pstate)
Definition: app-layer-parser.c:683
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:78
ctx
struct Thresholds ctx
AppLayerParserSupportsFiles
bool AppLayerParserSupportsFiles(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:1149
logged
int logged
Definition: app-layer-htp.h:1
FLOW_TC_APP_UPDATED
#define FLOW_TC_APP_UPDATED
Definition: flow.h:119
AppLayerParserGetStateProgressCompletionStatus
int AppLayerParserGetStateProgressCompletionStatus(AppProto alproto, uint8_t direction)
Definition: app-layer-parser.c:1096
g_filedata_logger_enabled
bool g_filedata_logger_enabled
Definition: output-filedata.c:37
OutputLoggerThreadStore_::next
struct OutputLoggerThreadStore_ * next
Definition: output.h:35
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:232
TxLogger
int(* TxLogger)(ThreadVars *, void *thread_data, const Packet *, Flow *f, void *state, void *tx, uint64_t tx_id)
Definition: output-tx.h:33
OutputTxLogger_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: output-tx.c:59
OutputTxLogger_::logger_id
LoggerId logger_id
Definition: output-tx.c:55
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:510
APP_LAYER_PARSER_EOF_TS
#define APP_LAYER_PARSER_EOF_TS
Definition: app-layer-parser.h:39
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
ALPROTO_MAX
@ ALPROTO_MAX
Definition: app-layer-protos.h:79
OutputTxLogger_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: output-tx.c:60
Ctx::tx_logged_old
uint32_t tx_logged_old
Definition: output-tx.c:267
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:82
AppLayerParserGetTxFiles
AppLayerGetFileState AppLayerParserGetTxFiles(const Flow *f, void *tx, const uint8_t direction)
Definition: app-layer-parser.c:856
OutputCtx_
Definition: tm-modules.h:85
OutputTxLoggerThreadData
struct OutputTxLoggerThreadData_ OutputTxLoggerThreadData
OutputLoggerThreadStore_::thread_data
void * thread_data
Definition: output.h:34
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:484
OutputRegisterTxLogger
int OutputRegisterTxLogger(LoggerId id, const char *name, AppProto alproto, TxLogger LogFunc, OutputCtx *output_ctx, int tc_log_progress, int ts_log_progress, TxLoggerCondition LogCondition, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, void(*ThreadExitPrintStats)(ThreadVars *, void *))
Definition: output-tx.c:66
PKT_IS_TOSERVER
#define PKT_IS_TOSERVER(p)
Definition: decode.h:240
OutputFileLogThreadDeinit
TmEcode OutputFileLogThreadDeinit(ThreadVars *tv, OutputFileLoggerThreadData *op_thread_data)
Definition: output-file.c:215
OutputTxLoggerThreadData_::filedata
OutputFiledataLoggerThreadData * filedata
Definition: output-tx.c:41
BIT_U32
#define BIT_U32(n)
Definition: suricata-common.h:400
PKT_PSEUDO_STREAM_END
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1275
OutputTxLogger_::next
struct OutputTxLogger_ * next
Definition: output-tx.c:53
STREAM_FLAGS_FOR_PACKET
#define STREAM_FLAGS_FOR_PACKET(p)
Definition: stream.h:30
FileContainer_::head
File * head
Definition: util-file.h:114
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
ThreadInitFunc
TmEcode(* ThreadInitFunc)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:40
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
util-profiling.h
stream.h
OutputTxLogger_::ts_log_progress
int ts_log_progress
Definition: output-tx.c:58
AppLayerGetTxIterState
Definition: app-layer-parser.h:143
Packet_
Definition: decode.h:479
APP_LAYER_PARSER_EOF_TC
#define APP_LAYER_PARSER_EOF_TC
Definition: app-layer-parser.h:40
TmEcode
TmEcode
Definition: tm-threads-common.h:81
OutputTxLogger_::output_ctx
OutputCtx * output_ctx
Definition: output-tx.c:52
OutputTxLogger_::LogCondition
TxLoggerCondition LogCondition
Definition: output-tx.c:51
OutputFiledataLoggerThreadData_
Definition: output-filedata.h:34
OutputFiledataLogFfc
void OutputFiledataLogFfc(ThreadVars *tv, OutputFiledataLoggerThreadData *td, Packet *p, AppLayerGetFileState files, void *txv, const uint64_t tx_id, AppLayerTxData *txd, const uint8_t call_flags, const bool file_close, const bool file_trunc, const uint8_t dir)
Definition: output-filedata.c:128
OutputTxLogger_::ThreadExitPrintStats
void(* ThreadExitPrintStats)(ThreadVars *, void *)
Definition: output-tx.c:61
FLOW_PKT_TOCLIENT
#define FLOW_PKT_TOCLIENT
Definition: flow.h:233
OutputTxLoggerThreadData_::store
OutputLoggerThreadStore * store[]
Definition: output-tx.c:42
TxLoggerCondition
bool(* TxLoggerCondition)(ThreadVars *, const Packet *, void *state, void *tx, uint64_t tx_id)
Definition: output-tx.h:38
PACKET_PROFILING_LOGGER_END
#define PACKET_PROFILING_LOGGER_END(p, id)
Definition: util-profiling.h:240
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1364
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
Packet_::flow
struct Flow_ * flow
Definition: decode.h:518
BIT_U8
#define BIT_U8(n)
Definition: suricata-common.h:398
suricata-common.h
OutputTxLogger
struct OutputTxLogger_ OutputTxLogger
Packet_::app_update_direction
uint8_t app_update_direction
Definition: decode.h:513
FLOW_TS_APP_UPDATED
#define FLOW_TS_APP_UPDATED
Definition: flow.h:118
OutputFileLogFfc
void OutputFileLogFfc(ThreadVars *tv, OutputFileLoggerThreadData *op_thread_data, Packet *p, FileContainer *ffc, void *txv, const uint64_t tx_id, AppLayerTxData *txd, const bool file_close, const bool file_trunc, uint8_t dir)
Definition: output-file.c:100
AppLayerParserGetTxData
AppLayerTxData * AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx)
Definition: app-layer-parser.c:1159
FatalError
#define FatalError(...)
Definition: util-debug.h:502
OutputTxLoggerThreadData_::file
OutputFileLoggerThreadData * file
Definition: output-tx.c:39
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
OutputFileLogThreadInit
TmEcode OutputFileLogThreadInit(ThreadVars *tv, OutputFileLoggerThreadData **data)
thread init for the file logger This will run the thread init functions for the individual registered...
Definition: output-file.c:168
util-validate.h
LOGGER_FILE
@ LOGGER_FILE
Definition: suricata-common.h:469
Packet_::next
struct Packet_ * next
Definition: decode.h:610
OutputTxLogger_::LogFunc
TxLogger LogFunc
Definition: output-tx.c:50
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Flow_::alstate
void * alstate
Definition: flow.h:485
Flow_::flags
uint32_t flags
Definition: flow.h:430
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
OutputTxLogger_
Definition: output-tx.c:48
PACKET_PROFILING_LOGGER_START
#define PACKET_PROFILING_LOGGER_START(p, id)
Definition: util-profiling.h:233
OutputRegisterRootLogger
void OutputRegisterRootLogger(ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats, OutputLogFunc LogFunc, OutputGetActiveCountFunc ActiveCntFunc)
Definition: output.c:837
OutputTxLogger_::name
const char * name
Definition: output-tx.c:54
AppLayerParserProtocolGetLoggerBits
LoggerId AppLayerParserProtocolGetLoggerBits(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:1525
AppLayerGetTxIteratorFunc
AppLayerGetTxIterTuple(* AppLayerGetTxIteratorFunc)(const uint8_t ipproto, const AppProto alproto, void *alstate, uint64_t min_tx_id, uint64_t max_tx_id, AppLayerGetTxIterState *state)
tx iterator prototype
Definition: app-layer-parser.h:152
FlowGetDisruptionFlags
uint8_t FlowGetDisruptionFlags(const Flow *f, uint8_t flags)
get 'disruption' flags: GAP/DEPTH/PASS
Definition: flow.c:1137
AppLayerParserStateIssetFlag
uint16_t AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint16_t flag)
Definition: app-layer-parser.c:1761
g_file_logger_enabled
bool g_file_logger_enabled
Definition: output-file.c:39
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:459
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
OutputTxLogger_::id
uint32_t id
Definition: output-tx.c:56
AppLayerParserGetTxCnt
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
Definition: app-layer-parser.c:1082
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
output.h
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1270
ThreadDeinitFunc
TmEcode(* ThreadDeinitFunc)(ThreadVars *, void *)
Definition: tm-modules.h:41
OutputTxLoggerThreadData_
Definition: output-tx.c:37
app-layer.h