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