suricata
app-layer-parser.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 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  * Generic App-layer parsing functions.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "util-unittest.h"
29 #include "decode.h"
30 #include "threads.h"
31 
32 #include "util-print.h"
33 #include "util-pool.h"
34 
35 #include "flow-util.h"
36 #include "flow-private.h"
37 
38 #include "detect-engine-state.h"
39 #include "detect-engine-port.h"
40 
41 #include "stream-tcp.h"
42 #include "stream-tcp-private.h"
43 #include "stream.h"
44 #include "stream-tcp-reassemble.h"
45 
46 #include "app-layer.h"
47 #include "app-layer-protos.h"
48 #include "app-layer-parser.h"
49 #include "app-layer-dcerpc.h"
50 #include "app-layer-dcerpc-udp.h"
51 #include "app-layer-smb.h"
52 #include "app-layer-htp.h"
53 #include "app-layer-ftp.h"
54 #include "app-layer-ssl.h"
55 #include "app-layer-ssh.h"
56 #include "app-layer-smtp.h"
57 #include "app-layer-dns-udp.h"
58 #include "app-layer-dns-tcp.h"
59 #include "app-layer-modbus.h"
60 #include "app-layer-enip.h"
61 #include "app-layer-dnp3.h"
62 #include "app-layer-nfs-tcp.h"
63 #include "app-layer-nfs-udp.h"
64 #include "app-layer-ntp.h"
65 #include "app-layer-tftp.h"
66 #include "app-layer-ikev2.h"
67 #include "app-layer-krb5.h"
68 #include "app-layer-dhcp.h"
69 #include "app-layer-snmp.h"
70 #include "app-layer-sip.h"
71 #include "app-layer-template.h"
73 #include "app-layer-rdp.h"
74 
75 #include "conf.h"
76 #include "util-spm.h"
77 
78 #include "util-debug.h"
79 #include "decode-events.h"
80 #include "util-unittest-helper.h"
81 #include "util-validate.h"
82 
83 #include "runmodes.h"
84 
87 };
88 
89 
90 /**
91  * \brief App layer protocol parser context.
92  */
94 {
95  /* 0 - to_server, 1 - to_client. */
96  AppLayerParserFPtr Parser[2];
97  bool logger;
98  uint32_t logger_bits; /**< registered loggers for this proto */
99 
100  void *(*StateAlloc)(void);
101  void (*StateFree)(void *);
102  void (*StateTransactionFree)(void *, uint64_t);
103  void *(*LocalStorageAlloc)(void);
104  void (*LocalStorageFree)(void *);
105 
106  void (*Truncate)(void *, uint8_t);
107  FileContainer *(*StateGetFiles)(void *, uint8_t);
108  AppLayerDecoderEvents *(*StateGetEvents)(void *);
109 
110  int (*StateGetProgress)(void *alstate, uint8_t direction);
111  uint64_t (*StateGetTxCnt)(void *alstate);
112  void *(*StateGetTx)(void *alstate, uint64_t tx_id);
114  int (*StateGetProgressCompletionStatus)(uint8_t direction);
115  int (*StateGetEventInfoById)(int event_id, const char **event_name,
117  int (*StateGetEventInfo)(const char *event_name,
118  int *event_id, AppLayerEventType *event_type);
119 
120  LoggerId (*StateGetTxLogged)(void *alstate, void *tx);
121  void (*StateSetTxLogged)(void *alstate, void *tx, LoggerId logger);
122 
123  DetectEngineState *(*GetTxDetectState)(void *tx);
124  int (*SetTxDetectState)(void *tx, DetectEngineState *);
125 
126  uint64_t (*GetTxDetectFlags)(void *tx, uint8_t dir);
127  void (*SetTxDetectFlags)(void *tx, uint8_t dir, uint64_t);
128 
129  void (*SetStreamDepthFlag)(void *tx, uint8_t flags);
130 
131  /* each app-layer has its own value */
132  uint32_t stream_depth;
133 
134  /* Indicates the direction the parser is ready to see the data
135  * the first time for a flow. Values accepted -
136  * STREAM_TOSERVER, STREAM_TOCLIENT */
137  uint8_t first_data_dir;
138 
139  /* Option flags such as supporting gaps or not. */
140  uint32_t option_flags;
141  /* coccinelle: AppLayerParserProtoCtx:option_flags:APP_LAYER_PARSER_OPT_ */
142 
143  uint32_t internal_flags;
144  /* coccinelle: AppLayerParserProtoCtx:internal_flags:APP_LAYER_PARSER_INT_ */
145 
146 #ifdef UNITTESTS
147  void (*RegisterUnittests)(void);
148 #endif
150 
151 typedef struct AppLayerParserCtx_ {
154 
156  /* coccinelle: AppLayerParserState:flags:APP_LAYER_PARSER_ */
157  uint8_t flags;
158 
159  /* Indicates the current transaction that is being inspected.
160  * We have a var per direction. */
161  uint64_t inspect_id[2];
162  /* Indicates the current transaction being logged. Unlike inspect_id,
163  * we don't need a var per direction since we don't log a transaction
164  * unless we have the entire transaction. */
165  uint64_t log_id;
166 
167  uint64_t min_id;
168 
169  /* Used to store decoder events. */
171 };
172 
173 #ifdef UNITTESTS
174 void UTHAppLayerParserStateGetIds(void *ptr, uint64_t *i1, uint64_t *i2, uint64_t *log, uint64_t *min)
175 {
176  struct AppLayerParserState_ *s = ptr;
177  *i1 = s->inspect_id[0];
178  *i2 = s->inspect_id[1];
179  *log = s->log_id;
180  *min = s->min_id;
181 }
182 #endif
183 
184 /* Static global version of the parser context.
185  * Post 2.0 let's look at changing this to move it out to app-layer.c. */
186 static AppLayerParserCtx alp_ctx;
187 
188 int AppLayerParserProtoIsRegistered(uint8_t ipproto, AppProto alproto)
189 {
190  uint8_t ipproto_map = FlowGetProtoMapping(ipproto);
191 
192  return (alp_ctx.ctxs[ipproto_map][alproto].StateAlloc != NULL) ? 1 : 0;
193 }
194 
196 {
197  SCEnter();
198 
199  AppLayerParserState *pstate = (AppLayerParserState *)SCMalloc(sizeof(*pstate));
200  if (pstate == NULL)
201  goto end;
202  memset(pstate, 0, sizeof(*pstate));
203 
204  end:
205  SCReturnPtr(pstate, "AppLayerParserState");
206 }
207 
209 {
210  SCEnter();
211 
212  if (pstate->decoder_events != NULL)
214  SCFree(pstate);
215 
216  SCReturn;
217 }
218 
220 {
221  SCEnter();
222  memset(&alp_ctx, 0, sizeof(alp_ctx));
223  SCReturnInt(0);
224 }
225 
227 {
228  AppProto alproto = 0;
229  int flow_proto = 0;
230 
231  /* lets set a default value for stream_depth */
232  for (flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
233  for (alproto = 0; alproto < ALPROTO_MAX; alproto++) {
234  if (!(alp_ctx.ctxs[flow_proto][alproto].internal_flags &
236  alp_ctx.ctxs[flow_proto][alproto].stream_depth =
238  }
239  }
240  }
241 }
242 
244 {
245  SCEnter();
246 
249 
250  SCReturnInt(0);
251 }
252 
254 {
255  SCEnter();
256 
257  AppProto alproto = 0;
258  int flow_proto = 0;
260 
261  tctx = SCMalloc(sizeof(*tctx));
262  if (tctx == NULL)
263  goto end;
264  memset(tctx, 0, sizeof(*tctx));
265 
266  for (flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
267  for (alproto = 0; alproto < ALPROTO_MAX; alproto++) {
268  uint8_t ipproto = FlowGetReverseProtoMapping(flow_proto);
269 
270  tctx->alproto_local_storage[flow_proto][alproto] =
272  }
273  }
274 
275  end:
276  SCReturnPtr(tctx, "void *");
277 }
278 
280 {
281  SCEnter();
282 
283  AppProto alproto = 0;
284  int flow_proto = 0;
285 
286  for (flow_proto = 0; flow_proto < FLOW_PROTO_DEFAULT; flow_proto++) {
287  for (alproto = 0; alproto < ALPROTO_MAX; alproto++) {
288  uint8_t ipproto = FlowGetReverseProtoMapping(flow_proto);
289 
291  tctx->alproto_local_storage[flow_proto][alproto]);
292  }
293  }
294 
295  SCFree(tctx);
296  SCReturn;
297 }
298 
299 /** \brief check if a parser is enabled in the config
300  * Returns enabled always if: were running unittests and
301  * when compiled with --enable-afl
302  */
303 int AppLayerParserConfParserEnabled(const char *ipproto,
304  const char *alproto_name)
305 {
306  SCEnter();
307 
308  int enabled = 1;
309  char param[100];
310  ConfNode *node;
311  int r;
312 
313 #ifdef AFLFUZZ_APPLAYER
314  goto enabled;
315 #endif
316  if (RunmodeIsUnittests())
317  goto enabled;
318 
319  r = snprintf(param, sizeof(param), "%s%s%s", "app-layer.protocols.",
320  alproto_name, ".enabled");
321  if (r < 0) {
322  SCLogError(SC_ERR_FATAL, "snprintf failure.");
323  exit(EXIT_FAILURE);
324  } else if (r > (int)sizeof(param)) {
325  SCLogError(SC_ERR_FATAL, "buffer not big enough to write param.");
326  exit(EXIT_FAILURE);
327  }
328 
329  node = ConfGetNode(param);
330  if (node == NULL) {
331  SCLogDebug("Entry for %s not found.", param);
332  r = snprintf(param, sizeof(param), "%s%s%s%s%s", "app-layer.protocols.",
333  alproto_name, ".", ipproto, ".enabled");
334  if (r < 0) {
335  SCLogError(SC_ERR_FATAL, "snprintf failure.");
336  exit(EXIT_FAILURE);
337  } else if (r > (int)sizeof(param)) {
338  SCLogError(SC_ERR_FATAL, "buffer not big enough to write param.");
339  exit(EXIT_FAILURE);
340  }
341 
342  node = ConfGetNode(param);
343  if (node == NULL) {
344  SCLogDebug("Entry for %s not found.", param);
345  goto enabled;
346  }
347  }
348 
349  if (ConfValIsTrue(node->val)) {
350  goto enabled;
351  } else if (ConfValIsFalse(node->val)) {
352  goto disabled;
353  } else if (strcasecmp(node->val, "detection-only") == 0) {
354  goto disabled;
355  } else {
356  SCLogError(SC_ERR_FATAL, "Invalid value found for %s.", param);
357  exit(EXIT_FAILURE);
358  }
359 
360  disabled:
361  enabled = 0;
362  enabled:
363  SCReturnInt(enabled);
364 }
365 
366 /***** Parser related registration *****/
367 
368 int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto,
369  uint8_t direction,
370  AppLayerParserFPtr Parser)
371 {
372  SCEnter();
373 
374  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
375  Parser[(direction & STREAM_TOSERVER) ? 0 : 1] = Parser;
376 
377  SCReturnInt(0);
378 }
379 
381  uint8_t direction)
382 {
383  SCEnter();
384 
385  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].first_data_dir |=
386  (direction & (STREAM_TOSERVER | STREAM_TOCLIENT));
387 
388  SCReturn;
389 }
390 
391 void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto,
392  uint32_t flags)
393 {
394  SCEnter();
395 
396  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].option_flags |= flags;
397 
398  SCReturn;
399 }
400 
401 void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto,
402  void *(*StateAlloc)(void),
403  void (*StateFree)(void *))
404 {
405  SCEnter();
406 
407  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateAlloc =
408  StateAlloc;
409  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateFree =
410  StateFree;
411 
412  SCReturn;
413 }
414 
415 void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto,
416  void *(*LocalStorageAlloc)(void),
417  void (*LocalStorageFree)(void *))
418 {
419  SCEnter();
420 
421  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].LocalStorageAlloc =
422  LocalStorageAlloc;
423  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].LocalStorageFree =
424  LocalStorageFree;
425 
426  SCReturn;
427 }
428 
429 void AppLayerParserRegisterGetFilesFunc(uint8_t ipproto, AppProto alproto,
430  FileContainer *(*StateGetFiles)(void *, uint8_t))
431 {
432  SCEnter();
433 
434  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateGetFiles =
435  StateGetFiles;
436 
437  SCReturn;
438 }
439 
440 void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto,
441  AppLayerDecoderEvents *(*StateGetEvents)(void *))
442 {
443  SCEnter();
444 
445  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateGetEvents =
446  StateGetEvents;
447 
448  SCReturn;
449 }
450 
451 void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto,
452  LoggerId (*StateGetTxLogged)(void *, void *),
453  void (*StateSetTxLogged)(void *, void *, LoggerId))
454 {
455  SCEnter();
456 
457  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateGetTxLogged =
458  StateGetTxLogged;
459 
460  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateSetTxLogged =
461  StateSetTxLogged;
462 
463  SCReturn;
464 }
465 
466 void AppLayerParserRegisterLoggerBits(uint8_t ipproto, AppProto alproto, LoggerId bits)
467 {
468  SCEnter();
469 
470  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].logger_bits = bits;
471 
472  SCReturn;
473 }
474 
475 void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
476 {
477  SCEnter();
478 
479  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].logger = true;
480 
481  SCReturn;
482 }
483 
484 void AppLayerParserRegisterTruncateFunc(uint8_t ipproto, AppProto alproto,
485  void (*Truncate)(void *, uint8_t))
486 {
487  SCEnter();
488 
489  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].Truncate = Truncate;
490 
491  SCReturn;
492 }
493 
495  int (*StateGetProgress)(void *alstate, uint8_t direction))
496 {
497  SCEnter();
498 
499  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
500  StateGetProgress = StateGetProgress;
501 
502  SCReturn;
503 }
504 
505 void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto,
506  void (*StateTransactionFree)(void *, uint64_t))
507 {
508  SCEnter();
509 
510  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
511  StateTransactionFree = StateTransactionFree;
512 
513  SCReturn;
514 }
515 
516 void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto,
517  uint64_t (*StateGetTxCnt)(void *alstate))
518 {
519  SCEnter();
520 
521  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
522  StateGetTxCnt = StateGetTxCnt;
523 
524  SCReturn;
525 }
526 
527 void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto,
528  void *(StateGetTx)(void *alstate, uint64_t tx_id))
529 {
530  SCEnter();
531 
532  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
533  StateGetTx = StateGetTx;
534 
535  SCReturn;
536 }
537 
538 void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto,
540 {
541  SCEnter();
542  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateGetTxIterator = Func;
543  SCReturn;
544 }
545 
547  int (*StateGetProgressCompletionStatus)(uint8_t direction))
548 {
549  SCEnter();
550 
551  alp_ctx.ctxs[FLOW_PROTO_DEFAULT][alproto].
552  StateGetProgressCompletionStatus = StateGetProgressCompletionStatus;
553 
554  SCReturn;
555 }
556 
557 void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto,
558  int (*StateGetEventInfoById)(int event_id, const char **event_name,
560 {
561  SCEnter();
562 
563  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
564  StateGetEventInfoById = StateGetEventInfoById;
565 
566  SCReturn;
567 }
568 
569 void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto,
570  int (*StateGetEventInfo)(const char *event_name, int *event_id,
572 {
573  SCEnter();
574 
575  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
576  StateGetEventInfo = StateGetEventInfo;
577 
578  SCReturn;
579 }
580 
581 void AppLayerParserRegisterDetectStateFuncs(uint8_t ipproto, AppProto alproto,
582  DetectEngineState *(*GetTxDetectState)(void *tx),
583  int (*SetTxDetectState)(void *tx, DetectEngineState *))
584 {
585  SCEnter();
586 
587  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].GetTxDetectState = GetTxDetectState;
588  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].SetTxDetectState = SetTxDetectState;
589 
590  SCReturn;
591 }
592 
593 void AppLayerParserRegisterDetectFlagsFuncs(uint8_t ipproto, AppProto alproto,
594  uint64_t(*GetTxDetectFlags)(void *tx, uint8_t dir),
595  void (*SetTxDetectFlags)(void *tx, uint8_t dir, uint64_t))
596 {
597  SCEnter();
598 
599  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].GetTxDetectFlags = GetTxDetectFlags;
600  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].SetTxDetectFlags = SetTxDetectFlags;
601 
602  SCReturn;
603 }
604 
605 void AppLayerParserRegisterMpmIDsFuncs(uint8_t ipproto, AppProto alproto,
606  uint64_t(*GetTxMpmIDs)(void *tx),
607  int (*SetTxMpmIDs)(void *tx, uint64_t))
608 {
609  SCEnter();
610  SCLogWarning(SC_WARN_DEPRECATED, "%u/%s: GetTxMpmIDs/SetTxMpmIDs is no longer used",
611  ipproto, AppProtoToString(alproto));
612  SCReturn;
613 }
614 
616  void (*SetStreamDepthFlag)(void *tx, uint8_t flags))
617 {
618  SCEnter();
619 
620  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].SetStreamDepthFlag = SetStreamDepthFlag;
621 
622  SCReturn;
623 }
624 
625 /***** Get and transaction functions *****/
626 
628 {
629  SCEnter();
630  void * r = NULL;
631 
632  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
633  LocalStorageAlloc != NULL)
634  {
635  r = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
636  LocalStorageAlloc();
637  }
638 
639  SCReturnPtr(r, "void *");
640 }
641 
643  void *local_data)
644 {
645  SCEnter();
646 
647  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
648  LocalStorageFree != NULL)
649  {
650  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
651  LocalStorageFree(local_data);
652  }
653 
654  SCReturn;
655 }
656 
657 /** \brief default tx iterator
658  *
659  * Used if the app layer parser doesn't register its own iterator.
660  * Simply walks the tx_id space until it finds a tx. Uses 'state' to
661  * keep track of where it left off.
662  *
663  * \retval txptr or NULL if no more txs in list
664  */
665 static AppLayerGetTxIterTuple AppLayerDefaultGetTxIterator(
666  const uint8_t ipproto, const AppProto alproto,
667  void *alstate, uint64_t min_tx_id, uint64_t max_tx_id,
668  AppLayerGetTxIterState *state)
669 {
670  uint64_t ustate = *(uint64_t *)state;
671  uint64_t tx_id = MAX(min_tx_id, ustate);
672  for ( ; tx_id < max_tx_id; tx_id++) {
673  void *tx_ptr = AppLayerParserGetTx(ipproto, alproto, alstate, tx_id);
674  if (tx_ptr != NULL) {
675  ustate = tx_id + 1;
676  *state = *(AppLayerGetTxIterState *)&ustate;
677  AppLayerGetTxIterTuple tuple = {
678  .tx_ptr = tx_ptr,
679  .tx_id = tx_id,
680  .has_next = (tx_id + 1 < max_tx_id),
681  };
682  SCLogDebug("tuple: %p/%"PRIu64"/%s", tuple.tx_ptr, tuple.tx_id,
683  tuple.has_next ? "true" : "false");
684  return tuple;
685  }
686  }
687 
688  AppLayerGetTxIterTuple no_tuple = { NULL, 0, false };
689  return no_tuple;
690 }
691 
693  const AppProto alproto)
694 {
696  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateGetTxIterator;
697  return Func ? Func : AppLayerDefaultGetTxIterator;
698 }
699 
700 void AppLayerParserSetTxLogged(uint8_t ipproto, AppProto alproto,
701  void *alstate, void *tx, LoggerId logger)
702 {
703  SCEnter();
704 
705  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
706  StateSetTxLogged != NULL) {
707  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
708  StateSetTxLogged(alstate, tx, logger);
709  }
710 
711  SCReturn;
712 }
713 
715  void *alstate, void *tx)
716 {
717  SCEnter();
718 
719  LoggerId r = 0;
720  if (alp_ctx.ctxs[f->protomap][f->alproto].StateGetTxLogged != NULL) {
721  r = alp_ctx.ctxs[f->protomap][f->alproto].
722  StateGetTxLogged(alstate, tx);
723  }
724 
725  SCReturnUInt(r);
726 }
727 
729 {
730  SCEnter();
731 
732  SCReturnCT((pstate == NULL) ? 0 : pstate->log_id, "uint64_t");
733 }
734 
736 {
737  SCEnter();
738 
739  if (pstate != NULL)
740  pstate->log_id = tx_id;
741 
742  SCReturn;
743 }
744 
745 uint64_t AppLayerParserGetTransactionInspectId(AppLayerParserState *pstate, uint8_t direction)
746 {
747  SCEnter();
748 
749  if (pstate == NULL)
750  SCReturnCT(0ULL, "uint64_t");
751 
752  SCReturnCT(pstate->inspect_id[direction & STREAM_TOSERVER ? 0 : 1], "uint64_t");
753 }
754 
756  void *alstate, const uint8_t flags,
757  bool tag_txs_as_inspected)
758 {
759  SCEnter();
760 
761  const int direction = (flags & STREAM_TOSERVER) ? 0 : 1;
762  const uint64_t total_txs = AppLayerParserGetTxCnt(f, alstate);
763  uint64_t idx = AppLayerParserGetTransactionInspectId(pstate, flags);
764  const int state_done_progress = AppLayerParserGetStateProgressCompletionStatus(f->alproto, flags);
765  const uint8_t ipproto = f->proto;
766  const AppProto alproto = f->alproto;
767 
768  AppLayerGetTxIteratorFunc IterFunc = AppLayerGetTxIterator(ipproto, alproto);
770  memset(&state, 0, sizeof(state));
771 
772  SCLogDebug("called: %s, tag_txs_as_inspected %s",direction==0?"toserver":"toclient",
773  tag_txs_as_inspected?"true":"false");
774 
775  /* mark all txs as inspected if the applayer progress is
776  * at the 'end state'. */
777  while (1) {
778  AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, idx, total_txs, &state);
779  if (ires.tx_ptr == NULL)
780  break;
781 
782  void *tx = ires.tx_ptr;
783  idx = ires.tx_id;
784 
785  int state_progress = AppLayerParserGetStateProgress(ipproto, alproto, tx, flags);
786  if (state_progress < state_done_progress)
787  break;
788 
789  if (tag_txs_as_inspected) {
790  uint64_t detect_flags = AppLayerParserGetTxDetectFlags(ipproto, alproto, tx, flags);
791  if ((detect_flags & APP_LAYER_TX_INSPECTED_FLAG) == 0) {
792  detect_flags |= APP_LAYER_TX_INSPECTED_FLAG;
793  AppLayerParserSetTxDetectFlags(ipproto, alproto, tx, flags, detect_flags);
794  SCLogDebug("%p/%"PRIu64" in-order tx is done for direction %s. Flag %016"PRIx64,
795  tx, idx, flags & STREAM_TOSERVER ? "toserver" : "toclient", detect_flags);
796  }
797  }
798  if (!ires.has_next)
799  break;
800  idx++;
801  }
802  pstate->inspect_id[direction] = idx;
803  SCLogDebug("inspect_id now %"PRIu64, pstate->inspect_id[direction]);
804 
805  /* if necessary we flag all txs that are complete as 'inspected'
806  * also move inspect_id forward. */
807  if (tag_txs_as_inspected) {
808  /* continue at idx */
809  while (1) {
810  AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, idx, total_txs, &state);
811  if (ires.tx_ptr == NULL)
812  break;
813 
814  void *tx = ires.tx_ptr;
815  /* if we got a higher id than the minimum we requested, we
816  * skipped a bunch of 'null-txs'. Lets see if we can up the
817  * inspect tracker */
818  if (ires.tx_id > idx && pstate->inspect_id[direction] == idx) {
819  pstate->inspect_id[direction] = ires.tx_id;
820  }
821  idx = ires.tx_id;
822 
823  const int state_progress = AppLayerParserGetStateProgress(ipproto, alproto, tx, flags);
824  if (state_progress < state_done_progress)
825  break;
826 
827  uint64_t detect_flags = AppLayerParserGetTxDetectFlags(ipproto, alproto, tx, flags);
828  if ((detect_flags & APP_LAYER_TX_INSPECTED_FLAG) == 0) {
829  detect_flags |= APP_LAYER_TX_INSPECTED_FLAG;
830  AppLayerParserSetTxDetectFlags(ipproto, alproto, tx, flags, detect_flags);
831  SCLogDebug("%p/%"PRIu64" out of order tx is done for direction %s. Flag %016"PRIx64,
832  tx, idx, flags & STREAM_TOSERVER ? "toserver" : "toclient", detect_flags);
833 
834  SCLogDebug("%p/%"PRIu64" out of order tx. Update inspect_id? %"PRIu64,
835  tx, idx, pstate->inspect_id[direction]);
836  if (pstate->inspect_id[direction]+1 == idx)
837  pstate->inspect_id[direction] = idx;
838  }
839  if (!ires.has_next)
840  break;
841  idx++;
842  }
843  }
844 
845  SCReturn;
846 }
847 
849 {
850  SCEnter();
851 
852  SCReturnPtr(pstate->decoder_events,
853  "AppLayerDecoderEvents *");
854 }
855 
857 {
858  pstate->decoder_events = devents;
859 }
860 
862  void *tx)
863 {
864  SCEnter();
865 
866  AppLayerDecoderEvents *ptr = NULL;
867 
868  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
869  StateGetEvents != NULL)
870  {
871  ptr = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
872  StateGetEvents(tx);
873  }
874 
875  SCReturnPtr(ptr, "AppLayerDecoderEvents *");
876 }
877 
879  void *alstate, uint8_t direction)
880 {
881  SCEnter();
882 
883  FileContainer *ptr = NULL;
884 
885  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
886  StateGetFiles != NULL)
887  {
888  ptr = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
889  StateGetFiles(alstate, direction);
890  }
891 
892  SCReturnPtr(ptr, "FileContainer *");
893 }
894 
895 /**
896  * \brief remove obsolete (inspected and logged) transactions
897  */
899 {
900  SCEnter();
902 
903  AppLayerParserProtoCtx *p = &alp_ctx.ctxs[f->protomap][f->alproto];
904  if (unlikely(p->StateTransactionFree == NULL))
905  SCReturn;
906 
907  const uint8_t ipproto = f->proto;
908  const AppProto alproto = f->alproto;
909  void * const alstate = f->alstate;
910  AppLayerParserState * const alparser = f->alparser;
911 
912  if (alstate == NULL || alparser == NULL)
913  SCReturn;
914 
915  const uint64_t min = alparser->min_id;
916  const uint64_t total_txs = AppLayerParserGetTxCnt(f, alstate);
917  const LoggerId logger_expectation = AppLayerParserProtocolGetLoggerBits(ipproto, alproto);
918  const int tx_end_state_ts = AppLayerParserGetStateProgressCompletionStatus(alproto, STREAM_TOSERVER);
919  const int tx_end_state_tc = AppLayerParserGetStateProgressCompletionStatus(alproto, STREAM_TOCLIENT);
920 
921  AppLayerGetTxIteratorFunc IterFunc = AppLayerGetTxIterator(ipproto, alproto);
923  memset(&state, 0, sizeof(state));
924  uint64_t i = min;
925  uint64_t new_min = min;
926  SCLogDebug("start min %"PRIu64, min);
927  bool skipped = false;
928 
929  while (1) {
930  AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, i, total_txs, &state);
931  if (ires.tx_ptr == NULL)
932  break;
933 
934  void *tx = ires.tx_ptr;
935  i = ires.tx_id; // actual tx id for the tx the IterFunc returned
936 
937  SCLogDebug("%p/%"PRIu64" checking", tx, i);
938 
939  const int tx_progress_tc = AppLayerParserGetStateProgress(ipproto, alproto, tx, STREAM_TOCLIENT);
940  if (tx_progress_tc < tx_end_state_tc) {
941  SCLogDebug("%p/%"PRIu64" skipping: tc parser not done", tx, i);
942  skipped = true;
943  goto next;
944  }
945  const int tx_progress_ts = AppLayerParserGetStateProgress(ipproto, alproto, tx, STREAM_TOSERVER);
946  if (tx_progress_ts < tx_end_state_ts) {
947  SCLogDebug("%p/%"PRIu64" skipping: ts parser not done", tx, i);
948  skipped = true;
949  goto next;
950  }
951  if (f->sgh_toserver != NULL) {
952  uint64_t detect_flags_ts = AppLayerParserGetTxDetectFlags(ipproto, alproto, tx, STREAM_TOSERVER);
953  if (!(detect_flags_ts & APP_LAYER_TX_INSPECTED_FLAG)) {
954  SCLogDebug("%p/%"PRIu64" skipping: TS inspect not done: ts:%"PRIx64,
955  tx, i, detect_flags_ts);
956  skipped = true;
957  goto next;
958  }
959  }
960  if (f->sgh_toclient != NULL) {
961  uint64_t detect_flags_tc = AppLayerParserGetTxDetectFlags(ipproto, alproto, tx, STREAM_TOCLIENT);
962  if (!(detect_flags_tc & APP_LAYER_TX_INSPECTED_FLAG)) {
963  SCLogDebug("%p/%"PRIu64" skipping: TC inspect not done: tc:%"PRIx64,
964  tx, i, detect_flags_tc);
965  skipped = true;
966  goto next;
967  }
968  }
969  if (logger_expectation != 0) {
970  LoggerId tx_logged = AppLayerParserGetTxLogged(f, alstate, tx);
971  if (tx_logged != logger_expectation) {
972  SCLogDebug("%p/%"PRIu64" skipping: logging not done: want:%"PRIx32", have:%"PRIx32,
973  tx, i, logger_expectation, tx_logged);
974  skipped = true;
975  goto next;
976  }
977  }
978 
979  /* if we are here, the tx can be freed. */
980  p->StateTransactionFree(alstate, i);
981  SCLogDebug("%p/%"PRIu64" freed", tx, i);
982 
983  /* if we didn't skip any tx so far, up the minimum */
984  SCLogDebug("skipped? %s i %"PRIu64", new_min %"PRIu64, skipped ? "true" : "false", i, new_min);
985  if (!skipped)
986  new_min = i + 1;
987  SCLogDebug("final i %"PRIu64", new_min %"PRIu64, i, new_min);
988 
989 next:
990  if (!ires.has_next) {
991  /* this was the last tx. See if we skipped any. If not
992  * we removed all and can update the minimum to the max
993  * id. */
994  SCLogDebug("no next: cur tx i %"PRIu64", total %"PRIu64, i, total_txs);
995  if (!skipped) {
996  new_min = total_txs;
997  SCLogDebug("no next: cur tx i %"PRIu64", total %"PRIu64": "
998  "new_min updated to %"PRIu64, i, total_txs, new_min);
999  }
1000  break;
1001  }
1002  i++;
1003  }
1004 
1005  /* see if we need to bring all trackers up to date. */
1006  SCLogDebug("update f->alparser->min_id? %"PRIu64" vs %"PRIu64, new_min, alparser->min_id);
1007  if (new_min > alparser->min_id) {
1008  const uint64_t next_id = new_min;
1009  alparser->min_id = next_id;
1010  alparser->inspect_id[0] = MAX(alparser->inspect_id[0], next_id);
1011  alparser->inspect_id[1] = MAX(alparser->inspect_id[1], next_id);
1012  alparser->log_id = MAX(alparser->log_id, next_id);
1013  SCLogDebug("updated f->alparser->min_id %"PRIu64, alparser->min_id);
1014  }
1015  SCReturn;
1016 }
1017 
1018 #define IS_DISRUPTED(flags) \
1019  ((flags) & (STREAM_DEPTH|STREAM_GAP))
1020 
1021 /**
1022  * \brief get the progress value for a tx/protocol
1023  *
1024  * If the stream is disrupted, we return the 'completion' value.
1025  */
1026 int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto,
1027  void *alstate, uint8_t flags)
1028 {
1029  SCEnter();
1030  int r = 0;
1031  if (unlikely(IS_DISRUPTED(flags))) {
1032  r = alp_ctx.ctxs[FLOW_PROTO_DEFAULT][alproto].
1033  StateGetProgressCompletionStatus(flags);
1034  } else {
1035  r = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
1036  StateGetProgress(alstate, flags);
1037  }
1038  SCReturnInt(r);
1039 }
1040 
1041 uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
1042 {
1043  SCEnter();
1044  uint64_t r = 0;
1045  r = alp_ctx.ctxs[f->protomap][f->alproto].
1046  StateGetTxCnt(alstate);
1047  SCReturnCT(r, "uint64_t");
1048 }
1049 
1050 void *AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
1051 {
1052  SCEnter();
1053  void * r = NULL;
1054  r = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
1055  StateGetTx(alstate, tx_id);
1056  SCReturnPtr(r, "void *");
1057 }
1058 
1060  uint8_t direction)
1061 {
1062  SCEnter();
1063  int r = alp_ctx.ctxs[FLOW_PROTO_DEFAULT][alproto].
1064  StateGetProgressCompletionStatus(direction);
1065  SCReturnInt(r);
1066 }
1067 
1068 int AppLayerParserGetEventInfo(uint8_t ipproto, AppProto alproto, const char *event_name,
1069  int *event_id, AppLayerEventType *event_type)
1070 {
1071  SCEnter();
1072  int ipproto_map = FlowGetProtoMapping(ipproto);
1073  int r = (alp_ctx.ctxs[ipproto_map][alproto].StateGetEventInfo == NULL) ?
1074  -1 : alp_ctx.ctxs[ipproto_map][alproto].StateGetEventInfo(event_name, event_id, event_type);
1075  SCReturnInt(r);
1076 }
1077 
1078 int AppLayerParserGetEventInfoById(uint8_t ipproto, AppProto alproto, int event_id,
1079  const char **event_name, AppLayerEventType *event_type)
1080 {
1081  SCEnter();
1082  int ipproto_map = FlowGetProtoMapping(ipproto);
1083  *event_name = (const char *)NULL;
1084  int r = (alp_ctx.ctxs[ipproto_map][alproto].StateGetEventInfoById == NULL) ?
1085  -1 : alp_ctx.ctxs[ipproto_map][alproto].StateGetEventInfoById(event_id, event_name, event_type);
1086  SCReturnInt(r);
1087 }
1088 
1089 uint8_t AppLayerParserGetFirstDataDir(uint8_t ipproto, AppProto alproto)
1090 {
1091  SCEnter();
1092  uint8_t r = 0;
1093  r = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
1094  first_data_dir;
1095  SCReturnCT(r, "uint8_t");
1096 }
1097 
1099  AppLayerParserState *pstate, uint8_t direction)
1100 {
1101  SCEnter();
1102 
1103  uint64_t active_id;
1104 
1105  uint64_t log_id = pstate->log_id;
1106  uint64_t inspect_id = pstate->inspect_id[direction & STREAM_TOSERVER ? 0 : 1];
1107  if (alp_ctx.ctxs[f->protomap][f->alproto].logger == true) {
1108  active_id = (log_id < inspect_id) ? log_id : inspect_id;
1109  } else {
1110  active_id = inspect_id;
1111  }
1112 
1113  SCReturnCT(active_id, "uint64_t");
1114 }
1115 
1116 int AppLayerParserSupportsFiles(uint8_t ipproto, AppProto alproto)
1117 {
1118  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].StateGetFiles != NULL)
1119  return TRUE;
1120  return FALSE;
1121 }
1122 
1123 int AppLayerParserSupportsTxDetectState(uint8_t ipproto, AppProto alproto)
1124 {
1125  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].GetTxDetectState != NULL)
1126  return TRUE;
1127  return FALSE;
1128 }
1129 
1130 DetectEngineState *AppLayerParserGetTxDetectState(uint8_t ipproto, AppProto alproto, void *tx)
1131 {
1132  SCEnter();
1133  DetectEngineState *s;
1134  s = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].GetTxDetectState(tx);
1135  SCReturnPtr(s, "DetectEngineState");
1136 }
1137 
1139  void *tx, DetectEngineState *s)
1140 {
1141  int r;
1142  SCEnter();
1143  if ((alp_ctx.ctxs[f->protomap][f->alproto].GetTxDetectState(tx) != NULL))
1144  SCReturnInt(-EBUSY);
1145  r = alp_ctx.ctxs[f->protomap][f->alproto].SetTxDetectState(tx, s);
1146  SCReturnInt(r);
1147 }
1148 
1149 uint64_t AppLayerParserGetTxDetectFlags(uint8_t ipproto, AppProto alproto, void *tx, uint8_t dir)
1150 {
1151  SCEnter();
1152  uint64_t flags = 0;
1153  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].GetTxDetectFlags != NULL) {
1154  flags = alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].GetTxDetectFlags(tx, dir);
1155  }
1156  SCReturnUInt(flags);
1157 }
1158 
1159 void AppLayerParserSetTxDetectFlags(uint8_t ipproto, AppProto alproto, void *tx, uint8_t dir, uint64_t flags)
1160 {
1161  SCEnter();
1162  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].SetTxDetectFlags != NULL) {
1163  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].SetTxDetectFlags(tx, dir, flags);
1164  }
1165  SCReturn;
1166 }
1167 
1168 /***** General *****/
1169 
1171  uint8_t flags, const uint8_t *input, uint32_t input_len)
1172 {
1173  SCEnter();
1174 #ifdef DEBUG_VALIDATION
1176 #endif
1177  AppLayerParserState *pstate = NULL;
1178  AppLayerParserProtoCtx *p = &alp_ctx.ctxs[f->protomap][alproto];
1179  void *alstate = NULL;
1180  uint64_t p_tx_cnt = 0;
1181 
1182  /* we don't have the parser registered for this protocol */
1183  if (p->StateAlloc == NULL)
1184  goto end;
1185 
1186  if (flags & STREAM_GAP) {
1188  SCLogDebug("app-layer parser does not accept gaps");
1189  if (f->alstate != NULL) {
1191  flags);
1192  }
1193  goto error;
1194  }
1195  }
1196 
1197  /* Get the parser state (if any) */
1198  pstate = f->alparser;
1199  if (pstate == NULL) {
1200  f->alparser = pstate = AppLayerParserStateAlloc();
1201  if (pstate == NULL)
1202  goto error;
1203  }
1204 
1205  if (flags & STREAM_EOF)
1207 
1208  alstate = f->alstate;
1209  if (alstate == NULL) {
1210  f->alstate = alstate = p->StateAlloc();
1211  if (alstate == NULL)
1212  goto error;
1213  SCLogDebug("alloced new app layer state %p (name %s)",
1214  alstate, AppLayerGetProtoName(f->alproto));
1215  } else {
1216  SCLogDebug("using existing app layer state %p (name %s))",
1217  alstate, AppLayerGetProtoName(f->alproto));
1218  }
1219 
1220  p_tx_cnt = AppLayerParserGetTxCnt(f, f->alstate);
1221 
1222  /* invoke the recursive parser, but only on data. We may get empty msgs on EOF */
1223  if (input_len > 0 || (flags & STREAM_EOF)) {
1224  /* invoke the parser */
1225  if (p->Parser[(flags & STREAM_TOSERVER) ? 0 : 1](f, alstate, pstate,
1226  input, input_len,
1227  alp_tctx->alproto_local_storage[f->protomap][alproto],
1228  flags) < 0)
1229  {
1230  goto error;
1231  }
1232  }
1233 
1234  /* set the packets to no inspection and reassembly if required */
1235  if (pstate->flags & APP_LAYER_PARSER_NO_INSPECTION) {
1236  AppLayerParserSetEOF(pstate);
1237  FlowSetNoPayloadInspectionFlag(f);
1238 
1239  if (f->proto == IPPROTO_TCP) {
1241 
1242  /* Set the no reassembly flag for both the stream in this TcpSession */
1243  if (pstate->flags & APP_LAYER_PARSER_NO_REASSEMBLY) {
1244  /* Used only if it's TCP */
1245  TcpSession *ssn = f->protoctx;
1246  if (ssn != NULL) {
1248  flags & STREAM_TOCLIENT ? 1 : 0);
1250  flags & STREAM_TOSERVER ? 1 : 0);
1251  }
1252  }
1253  /* Set the bypass flag for both the stream in this TcpSession */
1254  if (pstate->flags & APP_LAYER_PARSER_BYPASS_READY) {
1255  /* Used only if it's TCP */
1256  TcpSession *ssn = f->protoctx;
1257  if (ssn != NULL) {
1259  }
1260  }
1261  }
1262  }
1263 
1264  /* In cases like HeartBleed for TLS we need to inspect AppLayer but not Payload */
1266  FlowSetNoPayloadInspectionFlag(f);
1267  /* Set the no reassembly flag for both the stream in this TcpSession */
1268  if (f->proto == IPPROTO_TCP) {
1269  /* Used only if it's TCP */
1270  TcpSession *ssn = f->protoctx;
1271  if (ssn != NULL) {
1274  }
1275  }
1276  }
1277 
1278  /* get the diff in tx cnt for stats keeping */
1279  uint64_t cur_tx_cnt = AppLayerParserGetTxCnt(f, f->alstate);
1280  if (cur_tx_cnt > p_tx_cnt && tv) {
1281  AppLayerIncTxCounter(tv, f, cur_tx_cnt - p_tx_cnt);
1282  }
1283 
1284  /* stream truncated, inform app layer */
1285  if (flags & STREAM_DEPTH)
1286  AppLayerParserStreamTruncated(f->proto, alproto, alstate, flags);
1287 
1288  end:
1289  SCReturnInt(0);
1290  error:
1291  /* Set the no app layer inspection flag for both
1292  * the stream in this Flow */
1293  if (f->proto == IPPROTO_TCP) {
1295  }
1296  AppLayerParserSetEOF(pstate);
1297  SCReturnInt(-1);
1298 }
1299 
1301 {
1302  SCEnter();
1303 
1304  if (pstate == NULL)
1305  goto end;
1306 
1308 
1309  end:
1310  SCReturn;
1311 }
1312 
1313 /* return true if there are app parser decoder events. These are
1314  * only the ones that are set during protocol detection. */
1316 {
1317  SCEnter();
1318 
1319  if (pstate == NULL)
1320  return false;
1321 
1323  if (decoder_events && decoder_events->cnt)
1324  return true;
1325 
1326  /* if we have reached here, we don't have events */
1327  return false;
1328 }
1329 
1330 /** \brief simpler way to globally test if a alproto is registered
1331  * and fully enabled in the configuration.
1332  */
1334 {
1335  return (alp_ctx.ctxs[FLOW_PROTO_DEFAULT][alproto]
1337 }
1338 
1339 int AppLayerParserProtocolIsTxEventAware(uint8_t ipproto, AppProto alproto)
1340 {
1341  SCEnter();
1342  int ipproto_map = FlowGetProtoMapping(ipproto);
1343  int r = (alp_ctx.ctxs[ipproto_map][alproto].StateGetEvents == NULL) ? 0 : 1;
1344  SCReturnInt(r);
1345 }
1346 
1347 int AppLayerParserProtocolHasLogger(uint8_t ipproto, AppProto alproto)
1348 {
1349  SCEnter();
1350  int ipproto_map = FlowGetProtoMapping(ipproto);
1351  int r = (alp_ctx.ctxs[ipproto_map][alproto].logger == false) ? 0 : 1;
1352  SCReturnInt(r);
1353 }
1354 
1356 {
1357  SCEnter();
1358  const int ipproto_map = FlowGetProtoMapping(ipproto);
1359  LoggerId r = alp_ctx.ctxs[ipproto_map][alproto].logger_bits;
1360  SCReturnUInt(r);
1361 }
1362 
1364 {
1365  SCEnter();
1366 
1367  SCLogDebug("f %p tcp %p direction %d", f, f ? f->protoctx : NULL, direction);
1368  if (f != NULL && f->protoctx != NULL)
1370 
1371  SCReturn;
1372 }
1373 
1374 void AppLayerParserSetStreamDepth(uint8_t ipproto, AppProto alproto, uint32_t stream_depth)
1375 {
1376  SCEnter();
1377 
1378  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].stream_depth = stream_depth;
1379  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].internal_flags |=
1381 
1382  SCReturn;
1383 }
1384 
1386 {
1387  SCReturnInt(alp_ctx.ctxs[f->protomap][f->alproto].stream_depth);
1388 }
1389 
1390 void AppLayerParserSetStreamDepthFlag(uint8_t ipproto, AppProto alproto, void *state, uint64_t tx_id, uint8_t flags)
1391 {
1392  SCEnter();
1393  void *tx = NULL;
1394  if (state != NULL) {
1395  if ((tx = AppLayerParserGetTx(ipproto, alproto, state, tx_id)) != NULL) {
1396  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].SetStreamDepthFlag != NULL) {
1397  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].SetStreamDepthFlag(tx, flags);
1398  }
1399  }
1400  }
1401  SCReturn;
1402 }
1403 
1404 /***** Cleanup *****/
1405 
1406 void AppLayerParserStateCleanup(const Flow *f, void *alstate,
1407  AppLayerParserState *pstate)
1408 {
1409  SCEnter();
1410 
1411  AppLayerParserProtoCtx *ctx = &alp_ctx.ctxs[f->protomap][f->alproto];
1412 
1413  if (ctx->StateFree != NULL && alstate != NULL)
1414  ctx->StateFree(alstate);
1415 
1416  /* free the app layer parser api state */
1417  if (pstate != NULL)
1418  AppLayerParserStateFree(pstate);
1419 
1420  SCReturn;
1421 }
1422 
1423 static void ValidateParserProtoDump(AppProto alproto, uint8_t ipproto)
1424 {
1425  uint8_t map = FlowGetProtoMapping(ipproto);
1426  const AppLayerParserProtoCtx *ctx = &alp_ctx.ctxs[map][alproto];
1427  const AppLayerParserProtoCtx *ctx_def = &alp_ctx.ctxs[FLOW_PROTO_DEFAULT][alproto];
1428  printf("ERROR: incomplete app-layer registration\n");
1429  printf("AppLayer protocol %s ipproto %u\n", AppProtoToString(alproto), ipproto);
1430  printf("- option flags %"PRIx32"\n", ctx->option_flags);
1431  printf("- first_data_dir %"PRIx8"\n", ctx->first_data_dir);
1432  printf("Mandatory:\n");
1433  printf("- Parser[0] %p Parser[1] %p\n", ctx->Parser[0], ctx->Parser[1]);
1434  printf("- StateAlloc %p StateFree %p\n", ctx->StateAlloc, ctx->StateFree);
1435  printf("- StateGetTx %p StateGetTxCnt %p StateTransactionFree %p\n",
1436  ctx->StateGetTx, ctx->StateGetTxCnt, ctx->StateTransactionFree);
1437  printf("- StateGetProgress %p StateGetProgressCompletionStatus %p\n", ctx->StateGetProgress, ctx_def->StateGetProgressCompletionStatus);
1438  printf("- GetTxDetectState %p SetTxDetectState %p\n", ctx->GetTxDetectState, ctx->SetTxDetectState);
1439  printf("Optional:\n");
1440  printf("- LocalStorageAlloc %p LocalStorageFree %p\n", ctx->LocalStorageAlloc, ctx->LocalStorageFree);
1441  printf("- StateGetTxLogged %p StateSetTxLogged %p\n", ctx->StateGetTxLogged, ctx->StateSetTxLogged);
1442  printf("- StateGetEvents %p StateGetEventInfo %p StateGetEventInfoById %p\n", ctx->StateGetEvents, ctx->StateGetEventInfo,
1443  ctx->StateGetEventInfoById);
1444 }
1445 
1446 #define BOTH_SET(a, b) ((a) != NULL && (b) != NULL)
1447 #define BOTH_SET_OR_BOTH_UNSET(a, b) (((a) == NULL && (b) == NULL) || ((a) != NULL && (b) != NULL))
1448 #define THREE_SET_OR_THREE_UNSET(a, b, c) (((a) == NULL && (b) == NULL && (c) == NULL) || ((a) != NULL && (b) != NULL && (c) != NULL))
1449 #define THREE_SET(a, b, c) ((a) != NULL && (b) != NULL && (c) != NULL)
1450 
1451 static void ValidateParserProto(AppProto alproto, uint8_t ipproto)
1452 {
1453  uint8_t map = FlowGetProtoMapping(ipproto);
1454  const AppLayerParserProtoCtx *ctx = &alp_ctx.ctxs[map][alproto];
1455  const AppLayerParserProtoCtx *ctx_def = &alp_ctx.ctxs[FLOW_PROTO_DEFAULT][alproto];
1456 
1457  if (ctx->Parser[0] == NULL && ctx->Parser[1] == NULL)
1458  return;
1459 
1460  if (!(BOTH_SET(ctx->Parser[0], ctx->Parser[1]))) {
1461  goto bad;
1462  }
1463  if (!(BOTH_SET(ctx->StateFree, ctx->StateAlloc))) {
1464  goto bad;
1465  }
1466  if (!(THREE_SET(ctx->StateGetTx, ctx->StateGetTxCnt, ctx->StateTransactionFree))) {
1467  goto bad;
1468  }
1469  /* special case: StateGetProgressCompletionStatus is used from 'default'. */
1471  goto bad;
1472  }
1473  /* local storage is optional, but needs both set if used */
1475  goto bad;
1476  }
1478  goto bad;
1479  }
1480  if (!(BOTH_SET(ctx->GetTxDetectState, ctx->SetTxDetectState))) {
1481  goto bad;
1482  }
1484  goto bad;
1485  }
1486 
1487  return;
1488 bad:
1489  ValidateParserProtoDump(alproto, ipproto);
1490  exit(EXIT_FAILURE);
1491 }
1492 #undef BOTH_SET
1493 #undef BOTH_SET_OR_BOTH_UNSET
1494 #undef THREE_SET_OR_THREE_UNSET
1495 #undef THREE_SET
1496 
1497 static void ValidateParser(AppProto alproto)
1498 {
1499  ValidateParserProto(alproto, IPPROTO_TCP);
1500  ValidateParserProto(alproto, IPPROTO_UDP);
1501 }
1502 
1503 static void ValidateParsers(void)
1504 {
1505  AppProto p = 0;
1506  for ( ; p < ALPROTO_MAX; p++) {
1507  ValidateParser(p);
1508  }
1509 }
1510 
1512 {
1513  SCEnter();
1514 
1541 
1542  /** IMAP */
1544  if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", "imap")) {
1546  "1|20|capability", 12, 0, STREAM_TOSERVER) < 0)
1547  {
1548  SCLogInfo("imap proto registration failure");
1549  exit(EXIT_FAILURE);
1550  }
1551  } else {
1552  SCLogInfo("Protocol detection and parser disabled for %s protocol.",
1553  "imap");
1554  }
1555 
1556  ValidateParsers();
1557  return;
1558 }
1559 
1560 
1562 {
1563  SCEnter();
1564  pstate->flags |= flag;
1565  SCReturn;
1566 }
1567 
1569 {
1570  SCEnter();
1571  SCReturnInt(pstate->flags & flag);
1572 }
1573 
1574 
1575 void AppLayerParserStreamTruncated(uint8_t ipproto, AppProto alproto, void *alstate,
1576  uint8_t direction)
1577 {
1578  SCEnter();
1579 
1580  if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].Truncate != NULL)
1581  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].Truncate(alstate, direction);
1582 
1583  SCReturn;
1584 }
1585 
1586 #ifdef DEBUG
1587 void AppLayerParserStatePrintDetails(AppLayerParserState *pstate)
1588 {
1589  SCEnter();
1590 
1591  if (pstate == NULL)
1592  SCReturn;
1593 
1594  AppLayerParserState *p = pstate;
1595  SCLogDebug("AppLayerParser parser state information for parser state p(%p). "
1596  "p->inspect_id[0](%"PRIu64"), "
1597  "p->inspect_id[1](%"PRIu64"), "
1598  "p->log_id(%"PRIu64"), "
1599  "p->decoder_events(%p).",
1600  pstate, p->inspect_id[0], p->inspect_id[1], p->log_id,
1601  p->decoder_events);
1602 
1603  SCReturn;
1604 }
1605 #endif
1606 
1607 #ifdef AFLFUZZ_APPLAYER
1608 int AppLayerParserRequestFromFile(uint8_t ipproto, AppProto alproto, char *filename)
1609 {
1610  bool do_dump = (getenv("SC_AFL_DUMP_FILES") != NULL);
1611  struct timeval ts;
1612  memset(&ts, 0, sizeof(ts));
1613  gettimeofday(&ts, NULL);
1614 
1615  int result = 1;
1616  Flow *f = NULL;
1617  TcpSession ssn;
1619 
1620  memset(&ssn, 0, sizeof(ssn));
1621 
1622  f = SCCalloc(1, sizeof(Flow));
1623  if (f == NULL)
1624  goto end;
1625  FLOW_INITIALIZE(f);
1626 
1627  f->flags |= FLOW_IPV4;
1628  f->src.addr_data32[0] = 0x01020304;
1629  f->dst.addr_data32[0] = 0x05060708;
1630  f->sp = 10000;
1631  f->dp = 80;
1632  f->protoctx = &ssn;
1633  f->proto = ipproto;
1635  f->alproto = alproto;
1636 
1637  uint8_t buffer[65536];
1638  uint32_t cnt = 0;
1639 
1640 #ifdef AFLFUZZ_PERSISTANT_MODE
1641  while (__AFL_LOOP(1000)) {
1642  /* reset state */
1643  memset(buffer, 0, sizeof(buffer));
1644 #endif /* AFLFUZZ_PERSISTANT_MODE */
1645 
1646  FILE *fp = fopen(filename, "r");
1647  BUG_ON(fp == NULL);
1648 
1649  int start = 1;
1650  while (1) {
1651  int done = 0;
1652  size_t size = fread(&buffer, 1, sizeof(buffer), fp);
1653  if (size < sizeof(buffer))
1654  done = 1;
1655 
1656  if (do_dump) {
1657  char outfilename[256];
1658  snprintf(outfilename, sizeof(outfilename), "dump/%u-%u.%u",
1659  (unsigned int)ts.tv_sec, (unsigned int)ts.tv_usec, cnt);
1660  FILE *out_fp = fopen(outfilename, "w");
1661  BUG_ON(out_fp == NULL);
1662  (void)fwrite(buffer, size, 1, out_fp);
1663  fclose(out_fp);
1664  }
1665  //SCLogInfo("result %u done %d start %d", (uint)result, done, start);
1666 
1667  uint8_t flags = STREAM_TOSERVER;
1668  if (start--) {
1669  flags |= STREAM_START;
1670  }
1671  if (done) {
1672  flags |= STREAM_EOF;
1673  }
1674  //PrintRawDataFp(stdout, buffer, result);
1675 
1676  (void)AppLayerParserParse(NULL, alp_tctx, f, alproto, flags,
1677  buffer, size);
1678  cnt++;
1679 
1680  if (done)
1681  break;
1682  }
1683 
1684  fclose(fp);
1685 
1686 #ifdef AFLFUZZ_PERSISTANT_MODE
1687  }
1688 #endif /* AFLFUZZ_PERSISTANT_MODE */
1689 
1690  if (do_dump) {
1691  /* if we get here there was no crash, so we can remove our files */
1692  uint32_t x = 0;
1693  for (x = 0; x < cnt; x++) {
1694  char rmfilename[256];
1695  snprintf(rmfilename, sizeof(rmfilename), "dump/%u-%u.%u",
1696  (unsigned int)ts.tv_sec, (unsigned int)ts.tv_usec, x);
1697  unlink(rmfilename);
1698  }
1699  }
1700 
1701  result = 0;
1702 
1703 end:
1704  if (alp_tctx != NULL)
1705  AppLayerParserThreadCtxFree(alp_tctx);
1706  if (f != NULL) {
1707  FlowFree(f);
1708  }
1709  return result;
1710 }
1711 
1712 /* load a serie of files generated by DecoderParseDataFromFile() in
1713  * the same order as it was produced. */
1714 int AppLayerParserRequestFromFileSerie(uint8_t ipproto, AppProto alproto, char *fileprefix)
1715 {
1716  uint32_t cnt = 0;
1717  int start = 1;
1718  int result = 1;
1719  Flow *f = NULL;
1720  TcpSession ssn;
1722 
1723  memset(&ssn, 0, sizeof(ssn));
1724 
1725  f = SCCalloc(1, sizeof(Flow));
1726  if (f == NULL)
1727  goto end;
1728  FLOW_INITIALIZE(f);
1729 
1730  f->flags |= FLOW_IPV4;
1731  f->src.addr_data32[0] = 0x01020304;
1732  f->dst.addr_data32[0] = 0x05060708;
1733  f->sp = 10000;
1734  f->dp = 80;
1735  f->protoctx = &ssn;
1736  f->proto = ipproto;
1738  f->alproto = alproto;
1739 
1740  uint8_t buffer[65536];
1741 
1742  char filename[256];
1743  snprintf(filename, sizeof(filename), "dump/%s.%u", fileprefix, cnt);
1744  FILE *fp;
1745  while ((fp = fopen(filename, "r")) != NULL)
1746  {
1747  memset(buffer, 0, sizeof(buffer));
1748 
1749  size_t size = fread(&buffer, 1, sizeof(buffer), fp);
1750 
1751  uint8_t flags = STREAM_TOSERVER;
1752  if (start--) {
1753  flags |= STREAM_START;
1754  }
1755 
1756  (void)AppLayerParserParse(NULL, alp_tctx, f, alproto, flags,
1757  buffer, size);
1758 
1759  fclose(fp);
1760  cnt++;
1761 
1762  snprintf(filename, sizeof(filename), "dump/%s.%u", fileprefix, cnt);
1763  }
1764 
1765  result = 0;
1766 
1767 end:
1768  if (alp_tctx != NULL)
1769  AppLayerParserThreadCtxFree(alp_tctx);
1770  if (f != NULL) {
1771  FlowFree(f);
1772  }
1773  return result;
1774 }
1775 
1776 int AppLayerParserFromFile(uint8_t ipproto, AppProto alproto, char *filename)
1777 {
1778  bool do_dump = (getenv("SC_AFL_DUMP_FILES") != NULL);
1779  struct timeval ts;
1780  memset(&ts, 0, sizeof(ts));
1781  gettimeofday(&ts, NULL);
1782 
1783  int result = 1;
1784  Flow *f = NULL;
1785  TcpSession ssn;
1787 
1788  memset(&ssn, 0, sizeof(ssn));
1789 
1790  f = SCCalloc(1, sizeof(Flow));
1791  if (f == NULL)
1792  goto end;
1793  FLOW_INITIALIZE(f);
1794 
1795  f->flags |= FLOW_IPV4;
1796  f->src.addr_data32[0] = 0x01020304;
1797  f->dst.addr_data32[0] = 0x05060708;
1798  f->sp = 10000;
1799  f->dp = 80;
1800  f->protoctx = &ssn;
1801  f->proto = ipproto;
1803  f->alproto = alproto;
1804 
1805  uint8_t buffer[65536];
1806  uint32_t cnt = 0;
1807 
1808 #ifdef AFLFUZZ_PERSISTANT_MODE
1809  while (__AFL_LOOP(1000)) {
1810  /* reset state */
1811  memset(buffer, 0, sizeof(buffer));
1812 #endif /* AFLFUZZ_PERSISTANT_MODE */
1813 
1814  FILE *fp = fopen(filename, "r");
1815  BUG_ON(fp == NULL);
1816 
1817  int start = 1;
1818  int flip = 0;
1819  while (1) {
1820  int done = 0;
1821  size_t size = fread(&buffer, 1, sizeof(buffer), fp);
1822  if (size < sizeof(buffer))
1823  done = 1;
1824  if (do_dump) {
1825  char outfilename[256];
1826  snprintf(outfilename, sizeof(outfilename), "dump/%u-%u.%u",
1827  (unsigned int)ts.tv_sec, (unsigned int)ts.tv_usec, cnt);
1828  FILE *out_fp = fopen(outfilename, "w");
1829  BUG_ON(out_fp == NULL);
1830  (void)fwrite(buffer, size, 1, out_fp);
1831  fclose(out_fp);
1832  }
1833  //SCLogInfo("result %u done %d start %d", (uint)result, done, start);
1834 
1835  uint8_t flags = 0;
1836  if (flip) {
1837  flags = STREAM_TOCLIENT;
1838  flip = 0;
1839  } else {
1840  flags = STREAM_TOSERVER;
1841  flip = 1;
1842  }
1843 
1844  if (start--) {
1845  flags |= STREAM_START;
1846  }
1847  if (done) {
1848  flags |= STREAM_EOF;
1849  }
1850  //PrintRawDataFp(stdout, buffer, result);
1851 
1852  (void)AppLayerParserParse(NULL, alp_tctx, f, alproto, flags,
1853  buffer, size);
1854 
1855  cnt++;
1856 
1857  if (done)
1858  break;
1859  }
1860 
1861  fclose(fp);
1862 
1863 #ifdef AFLFUZZ_PERSISTANT_MODE
1864  }
1865 #endif /* AFLFUZZ_PERSISTANT_MODE */
1866 
1867  if (do_dump) {
1868  /* if we get here there was no crash, so we can remove our files */
1869  uint32_t x = 0;
1870  for (x = 0; x < cnt; x++) {
1871  char rmfilename[256];
1872  snprintf(rmfilename, sizeof(rmfilename), "dump/%u-%u.%u",
1873  (unsigned int)ts.tv_sec, (unsigned int)ts.tv_usec, x);
1874  unlink(rmfilename);
1875  }
1876  }
1877 
1878  result = 0;
1879 end:
1880  if (alp_tctx != NULL)
1881  AppLayerParserThreadCtxFree(alp_tctx);
1882  if (f != NULL) {
1883  FlowFree(f);
1884  }
1885  return result;
1886 }
1887 
1888 /* load a serie of files generated by DecoderParseDataFromFile() in
1889  * the same order as it was produced. */
1890 int AppLayerParserFromFileSerie(uint8_t ipproto, AppProto alproto, char *fileprefix)
1891 {
1892  uint32_t cnt = 0;
1893  int start = 1;
1894  int result = 1;
1895  Flow *f = NULL;
1896  TcpSession ssn;
1898 
1899  memset(&ssn, 0, sizeof(ssn));
1900 
1901  f = SCCalloc(1, sizeof(Flow));
1902  if (f == NULL)
1903  goto end;
1904  FLOW_INITIALIZE(f);
1905 
1906  f->flags |= FLOW_IPV4;
1907  f->src.addr_data32[0] = 0x01020304;
1908  f->dst.addr_data32[0] = 0x05060708;
1909  f->sp = 10000;
1910  f->dp = 80;
1911  f->protoctx = &ssn;
1912  f->proto = ipproto;
1914  f->alproto = alproto;
1915 
1916  uint8_t buffer[65536];
1917  int flip = 0;
1918  char filename[256];
1919  snprintf(filename, sizeof(filename), "dump/%s.%u", fileprefix, cnt);
1920  FILE *fp;
1921  while ((fp = fopen(filename, "r")) != NULL)
1922  {
1923  memset(buffer, 0, sizeof(buffer));
1924 
1925  size_t size = fread(&buffer, 1, sizeof(buffer), fp);
1926 
1927  uint8_t flags = 0;
1928  if (flip) {
1929  flags = STREAM_TOCLIENT;
1930  flip = 0;
1931  } else {
1932  flags = STREAM_TOSERVER;
1933  flip = 1;
1934  }
1935 
1936  if (start--) {
1937  flags |= STREAM_START;
1938  }
1939 
1940  (void)AppLayerParserParse(NULL, alp_tctx, f, alproto, flags,
1941  buffer, size);
1942 
1943  fclose(fp);
1944  cnt++;
1945 
1946  snprintf(filename, sizeof(filename), "dump/%s.%u", fileprefix, cnt);
1947  }
1948 
1949  result = 0;
1950 
1951 end:
1952  if (alp_tctx != NULL)
1953  AppLayerParserThreadCtxFree(alp_tctx);
1954  if (f != NULL) {
1955  FlowFree(f);
1956  }
1957  return result;
1958 }
1959 
1960 #endif /* AFLFUZZ_APPLAYER */
1961 
1962 /***** Unittests *****/
1963 
1964 #ifdef UNITTESTS
1965 
1966 static AppLayerParserCtx alp_ctx_backup_unittest;
1967 
1968 typedef struct TestState_ {
1969  uint8_t test;
1970 } TestState;
1971 
1972 /**
1973  * \brief Test parser function to test the memory deallocation of app layer
1974  * parser of occurence of an error.
1975  */
1976 static int TestProtocolParser(Flow *f, void *test_state, AppLayerParserState *pstate,
1977  const uint8_t *input, uint32_t input_len,
1978  void *local_data, const uint8_t flags)
1979 {
1980  SCEnter();
1981  SCReturnInt(-1);
1982 }
1983 
1984 /** \brief Function to allocates the Test protocol state memory
1985  */
1986 static void *TestProtocolStateAlloc(void)
1987 {
1988  SCEnter();
1989  void *s = SCMalloc(sizeof(TestState));
1990  if (unlikely(s == NULL))
1991  goto end;
1992  memset(s, 0, sizeof(TestState));
1993  end:
1994  SCReturnPtr(s, "TestState");
1995 }
1996 
1997 /** \brief Function to free the Test Protocol state memory
1998  */
1999 static void TestProtocolStateFree(void *s)
2000 {
2001  SCFree(s);
2002 }
2003 
2004 static uint64_t TestProtocolGetTxCnt(void *state)
2005 {
2006  /* single tx */
2007  return 1;
2008 }
2009 
2011  void (*RegisterUnittests)(void))
2012 {
2013  SCEnter();
2014  alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
2015  RegisterUnittests = RegisterUnittests;
2016  SCReturn;
2017 }
2018 
2020 {
2021  SCEnter();
2022  alp_ctx_backup_unittest = alp_ctx;
2023  memset(&alp_ctx, 0, sizeof(alp_ctx));
2024  SCReturn;
2025 }
2026 
2028 {
2029  SCEnter();
2030  alp_ctx = alp_ctx_backup_unittest;
2031  memset(&alp_ctx_backup_unittest, 0, sizeof(alp_ctx_backup_unittest));
2032  SCReturn;
2033 }
2034 
2035 /**
2036  * \test Test the deallocation of app layer parser memory on occurance of
2037  * error in the parsing process.
2038  */
2039 static int AppLayerParserTest01(void)
2040 {
2042 
2043  int result = 0;
2044  Flow *f = NULL;
2045  uint8_t testbuf[] = { 0x11 };
2046  uint32_t testlen = sizeof(testbuf);
2047  TcpSession ssn;
2049 
2050  memset(&ssn, 0, sizeof(ssn));
2051 
2052  /* Register the Test protocol state and parser functions */
2054  TestProtocolParser);
2056  TestProtocolStateAlloc, TestProtocolStateFree);
2057  AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TEST, TestProtocolGetTxCnt);
2058 
2059  f = UTHBuildFlow(AF_INET, "1.2.3.4", "4.3.2.1", 20, 40);
2060  if (f == NULL)
2061  goto end;
2062  f->protoctx = &ssn;
2063  f->alproto = ALPROTO_TEST;
2064  f->proto = IPPROTO_TCP;
2065 
2067 
2068  FLOWLOCK_WRLOCK(f);
2069  int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_TEST,
2070  STREAM_TOSERVER | STREAM_EOF, testbuf,
2071  testlen);
2072  if (r != -1) {
2073  printf("returned %" PRId32 ", expected -1: ", r);
2074  FLOWLOCK_UNLOCK(f);
2075  goto end;
2076  }
2077  FLOWLOCK_UNLOCK(f);
2078 
2080  printf("flag should have been set, but is not: ");
2081  goto end;
2082  }
2083 
2084  result = 1;
2085  end:
2088 
2089  UTHFreeFlow(f);
2090  return result;
2091 }
2092 
2093 /**
2094  * \test Test the deallocation of app layer parser memory on occurance of
2095  * error in the parsing process for UDP.
2096  */
2097 static int AppLayerParserTest02(void)
2098 {
2100 
2101  int result = 1;
2102  Flow *f = NULL;
2103  uint8_t testbuf[] = { 0x11 };
2104  uint32_t testlen = sizeof(testbuf);
2106 
2107  /* Register the Test protocol state and parser functions */
2109  TestProtocolParser);
2111  TestProtocolStateAlloc, TestProtocolStateFree);
2112  AppLayerParserRegisterGetTxCnt(IPPROTO_UDP, ALPROTO_TEST, TestProtocolGetTxCnt);
2113 
2114  f = UTHBuildFlow(AF_INET, "1.2.3.4", "4.3.2.1", 20, 40);
2115  if (f == NULL)
2116  goto end;
2117  f->alproto = ALPROTO_TEST;
2118  f->proto = IPPROTO_UDP;
2120 
2122 
2123  FLOWLOCK_WRLOCK(f);
2124  int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_TEST,
2125  STREAM_TOSERVER | STREAM_EOF, testbuf,
2126  testlen);
2127  if (r != -1) {
2128  printf("returned %" PRId32 ", expected -1: \n", r);
2129  result = 0;
2130  FLOWLOCK_UNLOCK(f);
2131  goto end;
2132  }
2133  FLOWLOCK_UNLOCK(f);
2134 
2135  end:
2138  UTHFreeFlow(f);
2139  return result;
2140 }
2141 
2142 
2144 {
2145  SCEnter();
2146 
2147  int ip;
2148  AppProto alproto;
2150 
2151  for (ip = 0; ip < FLOW_PROTO_DEFAULT; ip++) {
2152  for (alproto = 0; alproto < ALPROTO_MAX; alproto++) {
2153  ctx = &alp_ctx.ctxs[ip][alproto];
2154  if (ctx->RegisterUnittests == NULL)
2155  continue;
2156  ctx->RegisterUnittests();
2157  }
2158  }
2159 
2160  UtRegisterTest("AppLayerParserTest01", AppLayerParserTest01);
2161  UtRegisterTest("AppLayerParserTest02", AppLayerParserTest02);
2162 
2163  SCReturn;
2164 }
2165 
2166 #endif
void RegisterSMBParsers(void)
void AppLayerParserPostStreamSetup(void)
AppLayerDecoderEvents * AppLayerParserGetDecoderEvents(AppLayerParserState *pstate)
void AppLayerParserSetTransactionInspectId(const Flow *f, AppLayerParserState *pstate, void *alstate, const uint8_t flags, bool tag_txs_as_inspected)
void AppLayerParserSetStreamDepth(uint8_t ipproto, AppProto alproto, uint32_t stream_depth)
#define THREE_SET(a, b, c)
void RegisterTFTPParsers(void)
enum AppLayerEventType_ AppLayerEventType
void * AppLayerParserGetProtocolParserLocalStorage(uint8_t ipproto, AppProto alproto)
uint16_t flags
void StreamTcpSetSessionNoReassemblyFlag(TcpSession *, char)
disable reassembly
Definition: stream-tcp.c:5819
LoggerId AppLayerParserGetTxLogged(const Flow *f, void *alstate, void *tx)
void RegisterSMTPParsers(void)
Register the SMTP Protocol parser.
#define SCLogDebug(...)
Definition: util-debug.h:335
#define MAX(x, y)
void AppLayerParserRegisterOptionFlags(uint8_t ipproto, AppProto alproto, uint32_t flags)
struct HtpBodyChunk_ * next
void AppLayerParserRegisterLocalStorageFunc(uint8_t ipproto, AppProto alproto, void *(*LocalStorageAlloc)(void), void(*LocalStorageFree)(void *))
void * alproto_local_storage[FLOW_PROTO_MAX][ALPROTO_MAX]
int AppLayerParserProtocolHasLogger(uint8_t ipproto, AppProto alproto)
const struct SigGroupHead_ * sgh_toclient
Definition: flow.h:442
void RegisterNFSTCPParsers(void)
AppLayerDecoderEvents *(* StateGetEvents)(void *)
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
TcpStreamCnf stream_config
Definition: stream-tcp.h:106
#define BUG_ON(x)
void AppLayerParserSetTransactionLogId(AppLayerParserState *pstate, uint64_t tx_id)
uint32_t event_type
uint8_t proto
Definition: flow.h:344
DetectEngineState *(* GetTxDetectState)(void *tx)
int AppLayerParserGetStateProgressCompletionStatus(AppProto alproto, uint8_t direction)
int AppLayerParserIsTxAware(AppProto alproto)
simpler way to globally test if a alproto is registered and fully enabled in the configuration.
LoggerId(* StateGetTxLogged)(void *alstate, void *tx)
#define FALSE
void RegisterDHCPParsers(void)
void(* SetTxDetectFlags)(void *tx, uint8_t dir, uint64_t)
LoggerId
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:243
Port sp
Definition: flow.h:331
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
#define unlikely(expr)
Definition: util-optimize.h:35
int AppLayerParserDeSetup(void)
#define BOTH_SET(a, b)
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
int AppLayerProtoDetectConfProtoDetectionEnabled(const char *ipproto, const char *alproto)
Given a protocol name, checks if proto detection is enabled in the conf file.
void AppLayerParserRegisterDetectFlagsFuncs(uint8_t ipproto, AppProto alproto, uint64_t(*GetTxDetectFlags)(void *tx, uint8_t dir), void(*SetTxDetectFlags)(void *tx, uint8_t dir, uint64_t))
int AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint8_t flag)
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Definition: flow-util.c:80
void AppLayerParserRegisterParserAcceptableDataDirection(uint8_t ipproto, AppProto alproto, uint8_t direction)
void AppLayerParserRegisterDetectStateFuncs(uint8_t ipproto, AppProto alproto, DetectEngineState *(*GetTxDetectState)(void *tx), int(*SetTxDetectState)(void *tx, DetectEngineState *))
void RegisterSSHParsers(void)
Function to register the SSH protocol parsers and other functions.
void RegisterNFSUDPParsers(void)
void(* Truncate)(void *, uint8_t)
int AppLayerParserSupportsFiles(uint8_t ipproto, AppProto alproto)
void RegisterNTPParsers(void)
Definition: app-layer-ntp.c:39
void StreamTcpDisableAppLayer(Flow *f)
void AppLayerParserRegisterTruncateFunc(uint8_t ipproto, AppProto alproto, void(*Truncate)(void *, uint8_t))
#define STREAMTCP_FLAG_APP_LAYER_DISABLED
#define APP_LAYER_PARSER_BYPASS_READY
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
void StreamTcpReassembleTriggerRawReassembly(TcpSession *ssn, int direction)
Trigger RAW stream reassembly.
uint8_t FlowGetProtoMapping(uint8_t proto)
Function to map the protocol to the defined FLOW_PROTO_* enumeration.
Definition: flow-util.c:95
void RegisterDNSTCPParsers(void)
void RegisterSIPParsers(void)
Definition: app-layer-sip.c:38
#define SCReturnUInt(x)
Definition: util-debug.h:343
void StreamTcpFreeConfig(char quiet)
Definition: stream-tcp.c:669
#define FLOWLOCK_WRLOCK(fb)
Definition: flow.h:240
void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto, AppLayerDecoderEvents *(*StateGetEvents)(void *))
AppLayerDecoderEvents * AppLayerParserGetEventsByTx(uint8_t ipproto, AppProto alproto, void *tx)
AppLayerGetTxIteratorFunc AppLayerGetTxIterator(const uint8_t ipproto, const AppProto alproto)
int AppLayerParserGetEventInfoById(uint8_t ipproto, AppProto alproto, int event_id, const char **event_name, AppLayerEventType *event_type)
void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto, LoggerId(*StateGetTxLogged)(void *, void *), void(*StateSetTxLogged)(void *, void *, LoggerId))
uint16_t AppProto
char * val
Definition: conf.h:34
void AppLayerParserBackupParserTable(void)
void RegisterTemplateParsers(void)
#define TRUE
int(* AppLayerParserFPtr)(Flow *f, void *protocol_state, AppLayerParserState *pstate, const uint8_t *buf, uint32_t buf_len, void *local_storage, const uint8_t flags)
Prototype for parsing functions.
#define SCReturnCT(x, type)
Definition: util-debug.h:351
void * protoctx
Definition: flow.h:400
uint64_t AppLayerParserGetTransactionInspectId(AppLayerParserState *pstate, uint8_t direction)
FlowAddress dst
Definition: flow.h:329
#define STREAM_DEPTH
Definition: stream.h:34
void AppLayerParserRegisterProtocolParsers(void)
void AppLayerParserStreamTruncated(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t direction)
int(* StateGetEventInfoById)(int event_id, const char **event_name, AppLayerEventType *event_type)
void RegisterDCERPCUDPParsers(void)
int AppLayerParserSupportsTxDetectState(uint8_t ipproto, AppProto alproto)
void * alstate
Definition: flow.h:438
#define APP_LAYER_TX_INSPECTED_FLAG
#define APP_LAYER_PARSER_NO_REASSEMBLY
int AppLayerParserConfParserEnabled(const char *ipproto, const char *alproto_name)
check if a parser is enabled in the config Returns enabled always if: were running unittests and when...
void AppLayerParserTriggerRawStreamReassembly(Flow *f, int direction)
Flow * UTHBuildFlow(int family, const char *src, const char *dst, Port sp, Port dp)
void FTPParserCleanup(void)
Free memory allocated for global SMTP parser state.
struct AppLayerParserProtoCtx_ AppLayerParserProtoCtx
App layer protocol parser context.
void RegisterENIPUDPParsers(void)
Function to register the ENIP protocol parsers and other functions.
void AppLayerParserRegisterLoggerBits(uint8_t ipproto, AppProto alproto, LoggerId bits)
#define SCCalloc(nm, a)
Definition: util-mem.h:253
Data structure to store app layer decoder events.
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto, uint8_t direction, AppLayerParserFPtr Parser)
Register app layer parser for the protocol.
#define STREAM_GAP
Definition: stream.h:33
uint64_t AppLayerParserGetTransactionLogId(AppLayerParserState *pstate)
int AppLayerParserProtocolIsTxEventAware(uint8_t ipproto, AppProto alproto)
int(* StateGetProgress)(void *alstate, uint8_t direction)
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
void AppLayerParserStateFree(AppLayerParserState *pstate)
void *(* LocalStorageAlloc)(void)
void AppLayerParserRestoreParserTable(void)
void(* StateSetTxLogged)(void *alstate, void *tx, LoggerId logger)
void AppLayerParserRegisterGetFilesFunc(uint8_t ipproto, AppProto alproto, FileContainer *(*StateGetFiles)(void *, uint8_t))
void StreamTcpSetSessionBypassFlag(TcpSession *)
enable bypass
Definition: stream-tcp.c:5846
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
#define BOTH_SET_OR_BOTH_UNSET(a, b)
Data structures and function prototypes for keeping state for the detection engine.
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type))
uint8_t FlowGetReverseProtoMapping(uint8_t rproto)
Definition: flow-util.c:111
void RegisterRdpParsers(void)
Definition: app-layer-rdp.c:35
void AppLayerParserSetDecoderEvents(AppLayerParserState *pstate, AppLayerDecoderEvents *devents)
void RegisterSNMPParsers(void)
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define FLOW_NOPAYLOAD_INSPECTION
Definition: flow.h:61
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
#define IS_DISRUPTED(flags)
#define STREAM_EOF
Definition: stream.h:30
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void AppLayerParserSetEOF(AppLayerParserState *pstate)
void RegisterFTPParsers(void)
#define DEBUG_ASSERT_FLOW_LOCKED(f)
#define APP_LAYER_PARSER_NO_INSPECTION
uint64_t AppLayerParserGetTransactionActive(const Flow *f, AppLayerParserState *pstate, uint8_t direction)
#define SCEnter(...)
Definition: util-debug.h:337
void StreamTcpInitConfig(char)
To initialize the stream global configuration data.
Definition: stream-tcp.c:365
void *(* StateAlloc)(void)
void StreamTcpSetDisableRawReassemblyFlag(TcpSession *ssn, char direction)
Set the No reassembly flag for the given direction in given TCP session.
Definition: stream-tcp.c:5835
void RegisterHTPParsers(void)
Register the HTTP protocol and state handling functions to APP layer of the engine.
void *(* StateGetTx)(void *alstate, uint64_t tx_id)
void AppLayerParserRegisterUnittests(void)
int ConfValIsFalse(const char *val)
Check if a value is false.
Definition: conf.c:591
uint32_t AppLayerParserGetStreamDepth(const Flow *f)
AppLayerParserProtoCtx ctxs[FLOW_PROTO_MAX][ALPROTO_MAX]
#define STREAM_TOCLIENT
Definition: stream.h:32
void AppLayerParserRegisterGetStateProgressCompletionStatus(AppProto alproto, int(*StateGetProgressCompletionStatus)(uint8_t direction))
void RegisterIKEV2Parsers(void)
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol&#39;s parser thread context.
App layer protocol parser context.
FileContainer *(* StateGetFiles)(void *, uint8_t)
void(* LocalStorageFree)(void *)
int RunmodeIsUnittests(void)
Definition: suricata.c:267
void AppLayerParserStateCleanup(const Flow *f, void *alstate, AppLayerParserState *pstate)
#define SCReturnInt(x)
Definition: util-debug.h:341
void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfoById)(int event_id, const char **event_name, AppLayerEventType *event_type))
uint64_t(* GetTxDetectFlags)(void *tx, uint8_t dir)
void(* RegisterUnittests)(void)
int AppLayerParserSetup(void)
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
uint32_t reassembly_depth
Definition: stream-tcp.h:60
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:566
AppLayerParserState * AppLayerParserStateAlloc(void)
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
void RegisterENIPTCPParsers(void)
Function to register the ENIP protocol parsers and other functions.
Definition: conf.h:32
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
void AppLayerParserTransactionsCleanup(Flow *f)
remove obsolete (inspected and logged) transactions
uint64_t(* StateGetTxCnt)(void *alstate)
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
void AppLayerIncTxCounter(ThreadVars *tv, Flow *f, uint64_t step)
Definition: app-layer.c:113
void AppLayerParserRegisterSetStreamDepthFlag(uint8_t ipproto, AppProto alproto, void(*SetStreamDepthFlag)(void *tx, uint8_t flags))
#define SCFree(a)
Definition: util-mem.h:322
int(* StateGetProgressCompletionStatus)(uint8_t direction)
Port dp
Definition: flow.h:338
int AppLayerProtoDetectPMRegisterPatternCS(uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction)
Registers a case-sensitive pattern for protocol detection.
uint16_t tx_id
struct AppLayerParserCtx_ AppLayerParserCtx
void RegisterSSLParsers(void)
Function to register the SSL protocol parser and other functions.
void SMTPParserCleanup(void)
Free memory allocated for global SMTP parser state.
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto, void(*RegisterUnittests)(void))
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
const struct SigGroupHead_ * sgh_toserver
Definition: flow.h:445
#define STREAM_START
Definition: stream.h:29
void RegisterModbusParsers(void)
Function to register the Modbus protocol parsers and other functions.
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:39
AppLayerParserFPtr Parser[2]
#define STREAM_TOSERVER
Definition: stream.h:31
#define APP_LAYER_PARSER_OPT_ACCEPT_GAPS
#define APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD
#define SCReturnPtr(x, type)
Definition: util-debug.h:353
void RegisterKRB5Parsers(void)
void AppLayerParserRegisterGetTxIterator(uint8_t ipproto, AppProto alproto, AppLayerGetTxIteratorFunc Func)
void RegisterTemplateRustParsers(void)
void AppLayerParserSetTxLogged(uint8_t ipproto, AppProto alproto, void *alstate, void *tx, LoggerId logger)
#define APP_LAYER_PARSER_EOF
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:176
AppLayerDecoderEvents * decoder_events
FlowAddress src
Definition: flow.h:329
void RegisterDCERPCParsers(void)
void RegisterDNP3Parsers(void)
Register the DNP3 application protocol parser.
int(* SetTxDetectState)(void *tx, DetectEngineState *)
const char * AppLayerGetProtoName(AppProto alproto)
Given the internal protocol id, returns a string representation of the protocol.
Definition: app-layer.c:766
void AppLayerParserDestroyProtocolParserLocalStorage(uint8_t ipproto, AppProto alproto, void *local_data)
void AppLayerParserSetStreamDepthFlag(uint8_t ipproto, AppProto alproto, void *state, uint64_t tx_id, uint8_t flags)
struct TestState_ TestState
#define SCReturn
Definition: util-debug.h:339
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto, uint64_t(*StateGetTxCnt)(void *alstate))
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto, void *(*StateAlloc)(void), void(*StateFree)(void *))
Per thread variable structure.
Definition: threadvars.h:57
AppProto alproto
application level protocol
Definition: flow.h:409
void UTHAppLayerParserStateGetIds(void *ptr, uint64_t *i1, uint64_t *i2, uint64_t *log, uint64_t *min)
void RegisterDNSUDPParsers(void)
LoggerId AppLayerParserProtocolGetLoggerBits(uint8_t ipproto, AppProto alproto)
void(* SetStreamDepthFlag)(void *tx, uint8_t flags)
FileContainer * AppLayerParserGetFiles(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t direction)
DetectEngineState * AppLayerParserGetTxDetectState(uint8_t ipproto, AppProto alproto, void *tx)
void AppLayerParserSetTxDetectFlags(uint8_t ipproto, AppProto alproto, void *tx, uint8_t dir, uint64_t flags)
bool AppLayerParserHasDecoderEvents(AppLayerParserState *pstate)
AppLayerGetTxIteratorFunc StateGetTxIterator
int(* StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type)
uint8_t protomap
Definition: flow.h:404
Flow data structure.
Definition: flow.h:325
#define FLOW_IPV4
Definition: flow.h:94
void AppLayerParserStateSetFlag(AppLayerParserState *pstate, uint8_t flag)
void(* StateTransactionFree)(void *, uint64_t)
uint32_t flags
Definition: flow.h:379
int AppLayerParserSetTxDetectState(const Flow *f, void *tx, DetectEngineState *s)
#define APP_LAYER_PARSER_INT_STREAM_DEPTH_SET
int AppLayerParserGetEventInfo(uint8_t ipproto, AppProto alproto, const char *event_name, int *event_id, AppLayerEventType *event_type)
AppLayerParserState * alparser
Definition: flow.h:437
uint8_t AppLayerParserGetFirstDataDir(uint8_t ipproto, AppProto alproto)
void UTHFreeFlow(Flow *flow)
int AppLayerParserProtoIsRegistered(uint8_t ipproto, AppProto alproto)
uint64_t AppLayerParserGetTxDetectFlags(uint8_t ipproto, AppProto alproto, void *tx, uint8_t dir)
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))
void AppLayerParserRegisterMpmIDsFuncs(uint8_t ipproto, AppProto alproto, uint64_t(*GetTxMpmIDs)(void *tx), int(*SetTxMpmIDs)(void *tx, uint64_t))
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