suricata
detect-engine-prefilter.c
Go to the documentation of this file.
1 /* Copyright (C) 2016-2021 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  * Prefilter engine
24  *
25  * Prefilter engines have as purpose to check for a critical common part of
26  * a set of rules. If the condition is present in the traffic, the rules
27  * will have to be inspected individually. Otherwise, the rules can be
28  * skipped.
29  *
30  * The best example of this is the MPM. From each rule take a pattern and
31  * add it to the MPM state machine. Inspect that in one step and only
32  * individually inspect the rules that had a match in MPM.
33  *
34  * This prefilter API is designed to abstract this logic so that it becomes
35  * easier to add other types of prefilters.
36  *
37  * The prefilter engines are structured as a simple list of engines. Each
38  * engine checks for a condition using it's callback function and private
39  * data. It then adds the rule match candidates to the PrefilterRuleStore
40  * structure.
41  *
42  * After the engines have run the resulting list of match candidates is
43  * sorted by the rule id's so that the individual inspection happens in
44  * the correct order.
45  */
46 
47 #include "suricata-common.h"
48 #include "suricata.h"
49 
50 #include "detect-engine.h"
52 #include "detect-engine-mpm.h"
53 #include "detect-engine-frame.h"
54 
55 #include "app-layer-parser.h"
56 #include "app-layer-htp.h"
57 
58 #include "util-profiling.h"
59 #include "util-validate.h"
60 
61 static int PrefilterStoreGetId(DetectEngineCtx *de_ctx,
62  const char *name, void (*FreeFunc)(void *));
63 static const PrefilterStore *PrefilterStoreGetStore(const DetectEngineCtx *de_ctx,
64  const uint32_t id);
65 
66 static inline void QuickSortSigIntId(SigIntId *sids, uint32_t n)
67 {
68  if (n < 2)
69  return;
70  SigIntId p = sids[n / 2];
71  SigIntId *l = sids;
72  SigIntId *r = sids + n - 1;
73  while (l <= r) {
74  if (*l < p)
75  l++;
76  else if (*r > p)
77  r--;
78  else {
79  SigIntId t = *l;
80  *l = *r;
81  *r = t;
82  l++;
83  r--;
84  }
85  }
86  QuickSortSigIntId(sids, r - sids + 1);
87  QuickSortSigIntId(l, sids + n - l);
88 }
89 
90 /**
91  * \brief run prefilter engines on a transaction
92  */
94  const SigGroupHead *sgh,
95  Packet *p,
96  const uint8_t ipproto,
97  const uint8_t flow_flags,
98  const AppProto alproto,
99  void *alstate,
100  DetectTransaction *tx)
101 {
102  /* reset rule store */
103  det_ctx->pmq.rule_id_array_cnt = 0;
104 
105  SCLogDebug("packet %" PRIu64 " tx %p progress %d tx->prefilter_flags %" PRIx64, p->pcap_cnt,
106  tx->tx_ptr, tx->tx_progress, tx->prefilter_flags);
107 
108  PrefilterEngine *engine = sgh->tx_engines;
109  do {
110  if (engine->alproto != alproto)
111  goto next;
112  if (engine->ctx.tx_min_progress > tx->tx_progress)
113  break;
114  if (tx->tx_progress > engine->ctx.tx_min_progress) {
115  if (tx->prefilter_flags & BIT_U64(engine->ctx.tx_min_progress)) {
116  goto next;
117  }
118  }
119 
121  engine->cb.PrefilterTx(det_ctx, engine->pectx,
122  p, p->flow, tx->tx_ptr, tx->tx_id, flow_flags);
123  PREFILTER_PROFILING_END(det_ctx, engine->gid);
124 
125  if (tx->tx_progress > engine->ctx.tx_min_progress && engine->is_last_for_progress) {
126  tx->prefilter_flags |= BIT_U64(engine->ctx.tx_min_progress);
127  }
128  next:
129  if (engine->is_last)
130  break;
131  engine++;
132  } while (1);
133 
134  /* Sort the rule list to lets look at pmq.
135  * NOTE due to merging of 'stream' pmqs we *MAY* have duplicate entries */
136  if (likely(det_ctx->pmq.rule_id_array_cnt > 1)) {
138  QuickSortSigIntId(det_ctx->pmq.rule_id_array, det_ctx->pmq.rule_id_array_cnt);
140  }
141 }
142 
143 void Prefilter(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh,
144  Packet *p, const uint8_t flags)
145 {
146  SCEnter();
147 #if 0
148  /* TODO review this check */
149  SCLogDebug("sgh %p frame_engines %p", sgh, sgh->frame_engines);
150  if (p->proto == IPPROTO_TCP && sgh->frame_engines && p->flow &&
151  p->flow->alproto != ALPROTO_UNKNOWN && p->flow->alparser != NULL) {
153  PrefilterFrames(det_ctx, sgh, p, flags, p->flow->alproto);
155  }
156 #endif
157  if (sgh->pkt_engines) {
159  /* run packet engines */
160  PrefilterEngine *engine = sgh->pkt_engines;
161  do {
163  engine->cb.Prefilter(det_ctx, p, engine->pectx);
164  PREFILTER_PROFILING_END(det_ctx, engine->gid);
165 
166  if (engine->is_last)
167  break;
168  engine++;
169  } while (1);
171  }
172 
173  /* run payload inspecting engines */
174  if (sgh->payload_engines &&
177  {
179  PrefilterEngine *engine = sgh->payload_engines;
180  while (1) {
182  engine->cb.Prefilter(det_ctx, p, engine->pectx);
183  PREFILTER_PROFILING_END(det_ctx, engine->gid);
184 
185  if (engine->is_last)
186  break;
187  engine++;
188  }
190  }
191 
192  /* Sort the rule list to lets look at pmq.
193  * NOTE due to merging of 'stream' pmqs we *MAY* have duplicate entries */
194  if (likely(det_ctx->pmq.rule_id_array_cnt > 1)) {
196  QuickSortSigIntId(det_ctx->pmq.rule_id_array, det_ctx->pmq.rule_id_array_cnt);
198  }
199  SCReturn;
200 }
201 
203  void (*PrefilterFunc)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx),
204  void *pectx, void (*FreeFunc)(void *pectx),
205  const char *name)
206 {
207  if (sgh == NULL || PrefilterFunc == NULL || pectx == NULL)
208  return -1;
209 
210  PrefilterEngineList *e = SCMallocAligned(sizeof(*e), CLS);
211  if (e == NULL)
212  return -1;
213  memset(e, 0x00, sizeof(*e));
214 
215  e->Prefilter = PrefilterFunc;
216  e->pectx = pectx;
217  e->Free = FreeFunc;
218 
219  if (sgh->init->pkt_engines == NULL) {
220  sgh->init->pkt_engines = e;
221  } else {
223  while (t->next != NULL) {
224  t = t->next;
225  }
226 
227  t->next = e;
228  e->id = t->id + 1;
229  }
230 
231  e->name = name;
232  e->gid = PrefilterStoreGetId(de_ctx, e->name, e->Free);
233  return 0;
234 }
235 
237  void (*PrefilterFunc)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx),
238  void *pectx, void (*FreeFunc)(void *pectx),
239  const char *name)
240 {
241  if (sgh == NULL || PrefilterFunc == NULL || pectx == NULL)
242  return -1;
243 
244  PrefilterEngineList *e = SCMallocAligned(sizeof(*e), CLS);
245  if (e == NULL)
246  return -1;
247  memset(e, 0x00, sizeof(*e));
248 
249  e->Prefilter = PrefilterFunc;
250  e->pectx = pectx;
251  e->Free = FreeFunc;
252 
253  if (sgh->init->payload_engines == NULL) {
254  sgh->init->payload_engines = e;
255  } else {
257  while (t->next != NULL) {
258  t = t->next;
259  }
260 
261  t->next = e;
262  e->id = t->id + 1;
263  }
264 
265  e->name = name;
266  e->gid = PrefilterStoreGetId(de_ctx, e->name, e->Free);
267  return 0;
268 }
269 
271  void (*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx,
272  Packet *p, Flow *f, void *tx,
273  const uint64_t idx, const uint8_t flags),
274  AppProto alproto, int tx_min_progress,
275  void *pectx, void (*FreeFunc)(void *pectx),
276  const char *name)
277 {
278  if (sgh == NULL || PrefilterTxFunc == NULL || pectx == NULL)
279  return -1;
280 
281  PrefilterEngineList *e = SCMallocAligned(sizeof(*e), CLS);
282  if (e == NULL)
283  return -1;
284  memset(e, 0x00, sizeof(*e));
285 
286  e->PrefilterTx = PrefilterTxFunc;
287  e->pectx = pectx;
288  e->alproto = alproto;
289  // TODO change function prototype ?
290  DEBUG_VALIDATE_BUG_ON(tx_min_progress > UINT8_MAX);
291  e->tx_min_progress = (uint8_t)tx_min_progress;
292  e->Free = FreeFunc;
293 
294  if (sgh->init->tx_engines == NULL) {
295  sgh->init->tx_engines = e;
296  } else {
298  while (t->next != NULL) {
299  t = t->next;
300  }
301 
302  t->next = e;
303  e->id = t->id + 1;
304  }
305 
306  e->name = name;
307  e->gid = PrefilterStoreGetId(de_ctx, e->name, e->Free);
308  return 0;
309 }
310 
312  PrefilterFrameFn PrefilterFrameFunc, AppProto alproto, uint8_t frame_type, void *pectx,
313  void (*FreeFunc)(void *pectx), const char *name)
314 {
315  if (sgh == NULL || PrefilterFrameFunc == NULL || pectx == NULL)
316  return -1;
317 
318  PrefilterEngineList *e = SCMallocAligned(sizeof(*e), CLS);
319  if (e == NULL)
320  return -1;
321  memset(e, 0x00, sizeof(*e));
322 
323  e->frame_type = frame_type;
324  e->alproto = alproto;
325  e->PrefilterFrame = PrefilterFrameFunc;
326  e->pectx = pectx;
327  e->Free = FreeFunc;
328 
329  if (sgh->init->frame_engines == NULL) {
330  sgh->init->frame_engines = e;
331  } else {
333  while (t->next != NULL) {
334  t = t->next;
335  }
336 
337  t->next = e;
338  e->id = t->id + 1;
339  }
340 
341  e->name = name;
342  e->gid = PrefilterStoreGetId(de_ctx, e->name, e->Free);
343  return 0;
344 }
345 
346 static void PrefilterFreeEngineList(PrefilterEngineList *e)
347 {
348  if (e->Free && e->pectx) {
349  e->Free(e->pectx);
350  }
351  SCFreeAligned(e);
352 }
353 
355 {
356  PrefilterEngineList *t = list;
357 
358  while (t != NULL) {
360  PrefilterFreeEngineList(t);
361  t = next;
362  }
363 }
364 
365 static void PrefilterFreeEngines(const DetectEngineCtx *de_ctx, PrefilterEngine *list)
366 {
367  PrefilterEngine *t = list;
368 
369  while (1) {
370  const PrefilterStore *s = PrefilterStoreGetStore(de_ctx, t->gid);
371  if (s && s->FreeFunc && t->pectx) {
372  s->FreeFunc(t->pectx);
373  }
374 
375  if (t->is_last)
376  break;
377  t++;
378  }
379  SCFreeAligned(list);
380 }
381 
383 {
384  if (sgh->pkt_engines) {
385  PrefilterFreeEngines(de_ctx, sgh->pkt_engines);
386  sgh->pkt_engines = NULL;
387  }
388  if (sgh->payload_engines) {
389  PrefilterFreeEngines(de_ctx, sgh->payload_engines);
390  sgh->payload_engines = NULL;
391  }
392  if (sgh->tx_engines) {
393  PrefilterFreeEngines(de_ctx, sgh->tx_engines);
394  sgh->tx_engines = NULL;
395  }
396  if (sgh->frame_engines) {
397  PrefilterFreeEngines(de_ctx, sgh->frame_engines);
398  sgh->frame_engines = NULL;
399  }
400 }
401 
402 static int PrefilterSetupRuleGroupSortHelper(const void *a, const void *b)
403 {
404  const PrefilterEngine *s0 = a;
405  const PrefilterEngine *s1 = b;
406  if (s1->ctx.tx_min_progress == s0->ctx.tx_min_progress) {
407  if (s1->alproto == s0->alproto) {
408  return s0->local_id > s1->local_id ? 1 : -1;
409  } else {
410  return s0->alproto > s1->alproto ? 1 : -1;
411  }
412  } else {
413  return s0->ctx.tx_min_progress > s1->ctx.tx_min_progress ? 1 : -1;
414  }
415 }
416 
418 {
419  int r = PatternMatchPrepareGroup(de_ctx, sgh);
420  if (r != 0) {
421  FatalError(SC_ERR_INITIALIZATION, "failed to set up pattern matching");
422  }
423 
424  /* set up engines if needed - when prefilter is set to auto we run
425  * all engines, otherwise only those that have been forced by the
426  * prefilter keyword. */
428  for (int i = 0; i < DETECT_TBLSIZE; i++)
429  {
430  if (sigmatch_table[i].SetupPrefilter != NULL &&
431  (setting == DETECT_PREFILTER_AUTO ||
433  {
435  }
436  }
437 
438  /* we have lists of engines in sgh->init now. Lets setup the
439  * match arrays */
441  if (sgh->init->pkt_engines != NULL) {
442  uint32_t cnt = 0;
443  for (el = sgh->init->pkt_engines ; el != NULL; el = el->next) {
444  cnt++;
446  }
447  sgh->pkt_engines = SCMallocAligned(cnt * sizeof(PrefilterEngine), CLS);
448  if (sgh->pkt_engines == NULL) {
449  return;
450  }
451  memset(sgh->pkt_engines, 0x00, (cnt * sizeof(PrefilterEngine)));
452 
453  PrefilterEngine *e = sgh->pkt_engines;
454  for (el = sgh->init->pkt_engines ; el != NULL; el = el->next) {
455  e->local_id = el->id;
456  e->cb.Prefilter = el->Prefilter;
457  e->pectx = el->pectx;
458  el->pectx = NULL; // e now owns the ctx
459  e->gid = el->gid;
460  if (el->next == NULL) {
461  e->is_last = TRUE;
462  }
463  e++;
464  }
465  }
466  if (sgh->init->payload_engines != NULL) {
467  uint32_t cnt = 0;
468  for (el = sgh->init->payload_engines ; el != NULL; el = el->next) {
469  cnt++;
471  }
472  sgh->payload_engines = SCMallocAligned(cnt * sizeof(PrefilterEngine), CLS);
473  if (sgh->payload_engines == NULL) {
474  return;
475  }
476  memset(sgh->payload_engines, 0x00, (cnt * sizeof(PrefilterEngine)));
477 
479  for (el = sgh->init->payload_engines ; el != NULL; el = el->next) {
480  e->local_id = el->id;
481  e->cb.Prefilter = el->Prefilter;
482  e->pectx = el->pectx;
483  el->pectx = NULL; // e now owns the ctx
484  e->gid = el->gid;
485  if (el->next == NULL) {
486  e->is_last = TRUE;
487  }
488  e++;
489  }
490  }
491  if (sgh->init->tx_engines != NULL) {
492  uint32_t cnt = 0;
493  for (el = sgh->init->tx_engines ; el != NULL; el = el->next) {
494  cnt++;
496  }
497  sgh->tx_engines = SCMallocAligned(cnt * sizeof(PrefilterEngine), CLS);
498  if (sgh->tx_engines == NULL) {
499  return;
500  }
501  memset(sgh->tx_engines, 0x00, (cnt * sizeof(PrefilterEngine)));
502 
503  uint16_t local_id = 0;
504  PrefilterEngine *e = sgh->tx_engines;
505  for (el = sgh->init->tx_engines ; el != NULL; el = el->next) {
506  e->local_id = local_id++;
507  e->alproto = el->alproto;
509  e->cb.PrefilterTx = el->PrefilterTx;
510  e->pectx = el->pectx;
511  el->pectx = NULL; // e now owns the ctx
512  e->gid = el->gid;
513  e++;
514  }
515 
516  /* sort by tx_min_progress, then alproto, then local_id */
517  qsort(sgh->tx_engines, local_id, sizeof(PrefilterEngine),
518  PrefilterSetupRuleGroupSortHelper);
519  sgh->tx_engines[local_id - 1].is_last = true;
520  sgh->tx_engines[local_id - 1].is_last_for_progress = true;
521 
522  PrefilterEngine *engine;
523 
524  /* per alproto to set is_last_for_progress per alproto because the inspect
525  * loop skips over engines that are not the correct alproto */
526  for (AppProto a = 1; a < ALPROTO_FAILED; a++) {
527  int last_tx_progress = 0;
528  bool last_tx_progress_set = false;
529  PrefilterEngine *prev_engine = NULL;
530  engine = sgh->tx_engines;
531  do {
532  BUG_ON(engine->ctx.tx_min_progress < last_tx_progress);
533  if (engine->alproto == a) {
534  if (last_tx_progress_set && engine->ctx.tx_min_progress > last_tx_progress) {
535  if (prev_engine) {
536  prev_engine->is_last_for_progress = true;
537  }
538  }
539 
540  last_tx_progress_set = true;
541  prev_engine = engine;
542  } else {
543  if (prev_engine) {
544  prev_engine->is_last_for_progress = true;
545  }
546  }
547  last_tx_progress = engine->ctx.tx_min_progress;
548  if (engine->is_last)
549  break;
550  engine++;
551  } while (1);
552  }
553 #ifdef DEBUG
554  SCLogDebug("sgh %p", sgh);
555  engine = sgh->tx_engines;
556  do {
557  SCLogDebug("engine: gid %u alproto %s tx_min_progress %d is_last %s "
558  "is_last_for_progress %s",
559  engine->gid, AppProtoToString(engine->alproto), engine->ctx.tx_min_progress,
560  engine->is_last ? "true" : "false",
561  engine->is_last_for_progress ? "true" : "false");
562  if (engine->is_last)
563  break;
564  engine++;
565  } while (1);
566 #endif
567  }
568  if (sgh->init->frame_engines != NULL) {
569  uint32_t cnt = 0;
570  for (el = sgh->init->frame_engines; el != NULL; el = el->next) {
571  cnt++;
573  }
574  sgh->frame_engines = SCMallocAligned(cnt * sizeof(PrefilterEngine), CLS);
575  if (sgh->frame_engines == NULL) {
576  return;
577  }
578  memset(sgh->frame_engines, 0x00, (cnt * sizeof(PrefilterEngine)));
579 
580  PrefilterEngine *e = sgh->frame_engines;
581  for (el = sgh->init->frame_engines; el != NULL; el = el->next) {
582  e->local_id = el->id;
583  e->ctx.frame_type = el->frame_type;
585  e->alproto = el->alproto;
586  e->pectx = el->pectx;
587  el->pectx = NULL; // e now owns the ctx
588  e->gid = el->gid;
589  if (el->next == NULL) {
590  e->is_last = TRUE;
591  }
592  e++;
593  }
594  }
595 }
596 
597 /* hash table for assigning a unique id to each engine type. */
598 
599 static uint32_t PrefilterStoreHashFunc(HashListTable *ht, void *data, uint16_t datalen)
600 {
601  PrefilterStore *ctx = data;
602 
603  uint32_t hash = strlen(ctx->name);
604 
605  for (size_t u = 0; u < strlen(ctx->name); u++) {
606  hash += ctx->name[u];
607  }
608 
609  hash %= ht->array_size;
610  return hash;
611 }
612 
613 static char PrefilterStoreCompareFunc(void *data1, uint16_t len1,
614  void *data2, uint16_t len2)
615 {
616  PrefilterStore *ctx1 = data1;
617  PrefilterStore *ctx2 = data2;
618  return (strcmp(ctx1->name, ctx2->name) == 0);
619 }
620 
621 static void PrefilterStoreFreeFunc(void *ptr)
622 {
623  SCFree(ptr);
624 }
625 
627 {
628  if (de_ctx->prefilter_hash_table != NULL) {
630  }
631 }
632 
634 {
636 
638  PrefilterStoreHashFunc,
639  PrefilterStoreCompareFunc,
640  PrefilterStoreFreeFunc);
642 }
643 
644 static int PrefilterStoreGetId(DetectEngineCtx *de_ctx,
645  const char *name, void (*FreeFunc)(void *))
646 {
647  PrefilterStore ctx = { name, FreeFunc, 0 };
648 
650 
651  SCLogDebug("looking up %s", name);
652 
654  if (rctx != NULL) {
655  return rctx->id;
656  }
657 
658  PrefilterStore *actx = SCCalloc(1, sizeof(*actx));
659  if (actx == NULL) {
660  return -1;
661  }
662 
663  actx->name = name;
664  actx->FreeFunc = FreeFunc;
665  actx->id = de_ctx->prefilter_id++;
666  SCLogDebug("prefilter engine %s has profile id %u", actx->name, actx->id);
667 
668  int ret = HashListTableAdd(de_ctx->prefilter_hash_table, actx, 0);
669  if (ret != 0) {
670  SCFree(actx);
671  return -1;
672  }
673 
674  int r = actx->id;
675  return r;
676 }
677 
678 /** \warning slow */
679 static const PrefilterStore *PrefilterStoreGetStore(const DetectEngineCtx *de_ctx,
680  const uint32_t id)
681 {
682 
683  const PrefilterStore *store = NULL;
684  if (de_ctx->prefilter_hash_table != NULL) {
686  for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) {
688  if (ctx->id == id) {
689  store = ctx;
690  break;
691  }
692  }
693  }
694  return store;
695 }
696 
697 #ifdef PROFILING
698 const char *PrefilterStoreGetName(const uint32_t id)
699 {
700  return NULL;
701 }
702 #endif
703 
704 #include "util-print.h"
705 
706 typedef struct PrefilterMpmCtx {
707  int list_id;
709  const MpmCtx *mpm_ctx;
712 
713 /** \brief Generic Mpm prefilter callback
714  *
715  * \param det_ctx detection engine thread ctx
716  * \param p packet to inspect
717  * \param f flow to inspect
718  * \param txv tx to inspect
719  * \param pectx inspection context
720  */
721 static void PrefilterMpm(DetectEngineThreadCtx *det_ctx,
722  const void *pectx,
723  Packet *p, Flow *f, void *txv,
724  const uint64_t idx, const uint8_t flags)
725 {
726  SCEnter();
727 
728  const PrefilterMpmCtx *ctx = (const PrefilterMpmCtx *)pectx;
729  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
730  SCLogDebug("running on list %d", ctx->list_id);
731 
732  InspectionBuffer *buffer = ctx->GetData(det_ctx, ctx->transforms,
733  f, flags, txv, ctx->list_id);
734  if (buffer == NULL)
735  return;
736 
737  const uint32_t data_len = buffer->inspect_len;
738  const uint8_t *data = buffer->inspect;
739 
740  SCLogDebug("mpm'ing buffer:");
741  //PrintRawDataFp(stdout, data, data_len);
742 
743  if (data != NULL && data_len >= mpm_ctx->minlen) {
744  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
745  &det_ctx->mtcu, &det_ctx->pmq, data, data_len);
746  }
747 }
748 
749 static void PrefilterGenericMpmFree(void *ptr)
750 {
751  SCFree(ptr);
752 }
753 
755  SigGroupHead *sgh, MpmCtx *mpm_ctx,
756  const DetectBufferMpmRegistery *mpm_reg, int list_id)
757 {
758  SCEnter();
759  PrefilterMpmCtx *pectx = SCCalloc(1, sizeof(*pectx));
760  if (pectx == NULL)
761  return -1;
762  pectx->list_id = list_id;
763  pectx->GetData = mpm_reg->app_v2.GetData;
764  pectx->mpm_ctx = mpm_ctx;
765  pectx->transforms = &mpm_reg->transforms;
766 
767  int r = PrefilterAppendTxEngine(de_ctx, sgh, PrefilterMpm,
768  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
769  pectx, PrefilterGenericMpmFree, mpm_reg->pname);
770  if (r != 0) {
771  SCFree(pectx);
772  }
773  return r;
774 }
775 
776 /* generic mpm for pkt engines */
777 
778 typedef struct PrefilterMpmPktCtx {
779  int list_id;
781  const MpmCtx *mpm_ctx;
784 
785 /** \brief Generic Mpm prefilter callback
786  *
787  * \param det_ctx detection engine thread ctx
788  * \param p packet to inspect
789  * \param f flow to inspect
790  * \param txv tx to inspect
791  * \param pectx inspection context
792  */
793 static void PrefilterMpmPkt(DetectEngineThreadCtx *det_ctx,
794  Packet *p, const void *pectx)
795 {
796  SCEnter();
797 
798  const PrefilterMpmPktCtx *ctx = (const PrefilterMpmPktCtx *)pectx;
799  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
800  SCLogDebug("running on list %d", ctx->list_id);
801 
802  InspectionBuffer *buffer = ctx->GetData(det_ctx, ctx->transforms,
803  p, ctx->list_id);
804  if (buffer == NULL)
805  return;
806 
807  const uint32_t data_len = buffer->inspect_len;
808  const uint8_t *data = buffer->inspect;
809 
810  SCLogDebug("mpm'ing buffer:");
811  //PrintRawDataFp(stdout, data, data_len);
812 
813  if (data != NULL && data_len >= mpm_ctx->minlen) {
814  (void)mpm_table[mpm_ctx->mpm_type].Search(mpm_ctx,
815  &det_ctx->mtcu, &det_ctx->pmq, data, data_len);
816  }
817 }
818 
819 static void PrefilterMpmPktFree(void *ptr)
820 {
821  SCFree(ptr);
822 }
823 
825  SigGroupHead *sgh, MpmCtx *mpm_ctx,
826  const DetectBufferMpmRegistery *mpm_reg, int list_id)
827 {
828  SCEnter();
829  PrefilterMpmPktCtx *pectx = SCCalloc(1, sizeof(*pectx));
830  if (pectx == NULL)
831  return -1;
832  pectx->list_id = list_id;
833  pectx->GetData = mpm_reg->pkt_v1.GetData;
834  pectx->mpm_ctx = mpm_ctx;
835  pectx->transforms = &mpm_reg->transforms;
836 
837  int r = PrefilterAppendEngine(de_ctx, sgh, PrefilterMpmPkt,
838  pectx, PrefilterMpmPktFree, mpm_reg->pname);
839  if (r != 0) {
840  SCFree(pectx);
841  }
842  return r;
843 }
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:59
PrefilterEngineList_::frame_type
uint8_t frame_type
Definition: detect.h:1325
SigGroupHead_::tx_engines
PrefilterEngine * tx_engines
Definition: detect.h:1427
Packet_::proto
uint8_t proto
Definition: decode.h:464
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
detect-engine.h
PrefilterEngine_::Prefilter
void(* Prefilter)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
Definition: detect.h:1365
PatternMatchPrepareGroup
int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh)
Prepare the pattern matcher ctx in a sig group head.
Definition: detect-engine-mpm.c:2055
PrefilterMpmPktCtx
struct PrefilterMpmPktCtx PrefilterMpmPktCtx
PrefilterStore_::FreeFunc
void(* FreeFunc)(void *)
Definition: detect-engine-prefilter.h:31
PREFILTER_PROFILING_END
#define PREFILTER_PROFILING_END(ctx, profile_id)
Definition: util-profiling.h:294
PrefilterStore_
Definition: detect-engine-prefilter.h:29
CLS
#define CLS
Definition: suricata-common.h:45
Prefilter
void Prefilter(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p, const uint8_t flags)
Definition: detect-engine-prefilter.c:143
PrefilterRuleStore_::rule_id_array_cnt
uint32_t rule_id_array_cnt
Definition: util-prefilter.h:38
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1408
DetectEngineTransforms
Definition: detect.h:381
PrefilterEngineList_::id
uint16_t id
Definition: detect.h:1317
PROF_DETECT_PF_PAYLOAD
@ PROF_DETECT_PF_PAYLOAD
Definition: suricata-common.h:423
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
Packet_::pcap_cnt
uint64_t pcap_cnt
Definition: decode.h:603
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
PREFILTER_PROFILING_START
#define PREFILTER_PROFILING_START
Definition: util-profiling.h:280
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:80
PrefilterMpmCtx::GetData
InspectionBufferGetDataPtr GetData
Definition: detect-engine-prefilter.c:708
DetectBufferMpmRegistery_::transforms
DetectEngineTransforms transforms
Definition: detect.h:649
InspectionBuffer
Definition: detect.h:347
Packet_::flags
uint32_t flags
Definition: decode.h:477
DetectEngineCtx_::prefilter_maxid
uint32_t prefilter_maxid
Definition: detect.h:924
DETECT_TBLSIZE
@ DETECT_TBLSIZE
Definition: detect-engine-register.h:321
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1150
InspectionBufferGetDataPtr
InspectionBuffer *(* InspectionBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id)
Definition: detect.h:387
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:754
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:30
DetectBufferMpmRegistery_::app_v2
struct DetectBufferMpmRegistery_::@87::@89 app_v2
PrefilterEngineList_::name
const char * name
Definition: detect.h:1342
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:794
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:290
PrefilterEngine_::tx_min_progress
uint8_t tx_min_progress
Definition: detect.h:1356
InspectionBufferGetPktDataPtr
InspectionBuffer *(* InspectionBufferGetPktDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Packet *p, const int list_id)
Definition: detect.h:447
PrefilterDeinit
void PrefilterDeinit(DetectEngineCtx *de_ctx)
Definition: detect-engine-prefilter.c:626
PrefilterMpmPktCtx
Definition: detect-engine-prefilter.c:778
DetectBufferMpmRegistery_
one time registration of keywords at start up
Definition: detect.h:635
detect-engine-frame.h
PrefilterEngineList_::Prefilter
void(* Prefilter)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
Definition: detect.h:1331
SigGroupHead_::payload_engines
PrefilterEngine * payload_engines
Definition: detect.h:1426
DetectEngineCtx_::prefilter_setting
enum DetectEnginePrefilterSetting prefilter_setting
Definition: detect.h:939
PKT_NOPAYLOAD_INSPECTION
#define PKT_NOPAYLOAD_INSPECTION
Definition: decode.h:1174
PACKET_PROFILING_DETECT_END
#define PACKET_PROFILING_DETECT_END(p, id)
Definition: util-profiling.h:241
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:248
PrefilterEngine_::local_id
uint16_t local_id
Definition: detect.h:1348
PrefilterEngineList_::Free
void(* Free)(void *pectx)
Definition: detect.h:1340
MAX
#define MAX(x, y)
Definition: suricata-common.h:376
PrefilterAppendFrameEngine
int PrefilterAppendFrameEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, PrefilterFrameFn PrefilterFrameFunc, AppProto alproto, uint8_t frame_type, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:311
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:583
DetectTransaction_::tx_progress
const int tx_progress
Definition: detect-engine-state.h:106
detect-engine-prefilter.h
DetectEngineThreadCtx_::mtcu
MpmThreadCtx mtcu
Definition: detect.h:1148
PrefilterStore_::id
uint32_t id
Definition: detect-engine-prefilter.h:32
HashListTableAdd
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:116
DETECT_PREFILTER_AUTO
@ DETECT_PREFILTER_AUTO
Definition: detect.h:776
HashListTable_::array_size
uint32_t array_size
Definition: util-hashlist.h:41
PrefilterEngine_::is_last_for_progress
bool is_last_for_progress
Definition: detect.h:1375
PROF_DETECT_PF_SORT1
@ PROF_DETECT_PF_SORT1
Definition: suricata-common.h:426
DetectEngineCtx_::prefilter_id
uint32_t prefilter_id
Definition: detect.h:970
PrefilterEngineList_::next
struct PrefilterEngineList_ * next
Definition: detect.h:1337
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1238
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:488
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:824
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:58
DetectEngineCtx_::prefilter_hash_table
HashListTable * prefilter_hash_table
Definition: detect.h:971
app-layer-htp.h
HashListTableInit
HashListTable * HashListTableInit(uint32_t size, uint32_t(*Hash)(struct HashListTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hashlist.c:35
PrefilterEngineList_::alproto
AppProto alproto
Definition: detect.h:1320
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
PrefilterEngine_::PrefilterTx
void(* PrefilterTx)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags)
Definition: detect.h:1366
PrefilterStoreGetName
const char * PrefilterStoreGetName(const uint32_t id)
Definition: detect-engine-prefilter.c:698
DetectEngineThreadCtx_
Definition: detect.h:1043
PrefilterEngine_
Definition: detect.h:1347
SigGroupHeadInitData_::tx_engines
PrefilterEngineList * tx_engines
Definition: detect.h:1394
PROF_DETECT_PF_PKT
@ PROF_DETECT_PF_PKT
Definition: suricata-common.h:422
PrefilterEngineList_::PrefilterFrame
PrefilterFrameFn PrefilterFrame
Definition: detect.h:1335
PrefilterEngineList_::pectx
void * pectx
Definition: detect.h:1329
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect-engine-mpm.h
PrefilterFrameFn
void(* PrefilterFrameFn)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, const struct Frames *frames, const struct Frame *frame, const uint32_t idx)
Definition: detect.h:1313
PrefilterEngineList_::gid
uint32_t gid
Definition: detect.h:1344
PrefilterMpmPktCtx::GetData
InspectionBufferGetPktDataPtr GetData
Definition: detect-engine-prefilter.c:780
PKT_DETECT_HAS_STREAMDATA
#define PKT_DETECT_HAS_STREAMDATA
Definition: decode.h:1224
PrefilterMpmCtx
struct PrefilterMpmCtx PrefilterMpmCtx
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1431
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
TRUE
#define TRUE
Definition: suricata-common.h:33
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:281
util-profiling.h
DetectTransaction_::tx_id
const uint64_t tx_id
Definition: detect-engine-state.h:100
SCReturn
#define SCReturn
Definition: util-debug.h:302
PrefilterEngine_::alproto
AppProto alproto
Definition: detect.h:1351
DetectTransaction_::prefilter_flags
uint64_t prefilter_flags
Definition: detect-engine-state.h:104
Packet_
Definition: decode.h:442
SCFreeAligned
#define SCFreeAligned(p)
Definition: util-mem.h:77
PrefilterEngine_::cb
union PrefilterEngine_::@101 cb
PrefilterEngine_::PrefilterFrame
PrefilterFrameFn PrefilterFrame
Definition: detect.h:1369
PrefilterEngine_::frame_type
uint8_t frame_type
Definition: detect.h:1357
HashListTable_
Definition: util-hashlist.h:37
BIT_U64
#define BIT_U64(n)
Definition: suricata-common.h:382
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:165
SigGroupHead_::frame_engines
PrefilterEngine * frame_engines
Definition: detect.h:1428
PrefilterMpmCtx::list_id
int list_id
Definition: detect-engine-prefilter.c:707
PrefilterMpmPktCtx::list_id
int list_id
Definition: detect-engine-prefilter.c:779
SigGroupHeadInitData_::pkt_engines
PrefilterEngineList * pkt_engines
Definition: detect.h:1392
Packet_::flow
struct Flow_ * flow
Definition: decode.h:479
DetectBufferMpmRegistery_::pname
char pname[32]
Definition: detect.h:637
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigGroupHeadInitData_::frame_engines
PrefilterEngineList * frame_engines
Definition: detect.h:1395
SigGroupHeadInitData_::payload_engines
PrefilterEngineList * payload_engines
Definition: detect.h:1393
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:82
PROF_DETECT_PF_RECORD
@ PROF_DETECT_PF_RECORD
Definition: suricata-common.h:425
SCMallocAligned
#define SCMallocAligned(size, align)
Definition: util-mem.h:68
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
DetectEnginePrefilterSetting
DetectEnginePrefilterSetting
Definition: detect.h:774
DetectTransaction_
Definition: detect-engine-state.h:98
PrefilterEngineList_
Definition: detect.h:1316
PrefilterCleanupRuleGroup
void PrefilterCleanupRuleGroup(const DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Definition: detect-engine-prefilter.c:382
PrefilterEngine_::gid
uint32_t gid
Definition: detect.h:1373
FatalError
#define FatalError(x,...)
Definition: util-debug.h:532
PrefilterEngineList_::PrefilterTx
void(* PrefilterTx)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags)
Definition: detect.h:1332
PrefilterMpmPktCtx::transforms
const DetectEngineTransforms * transforms
Definition: detect-engine-prefilter.c:782
PrefilterMpmCtx
Definition: detect-engine-prefilter.c:706
util-validate.h
DetectTransaction_::tx_ptr
void * tx_ptr
Definition: detect-engine-state.h:99
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:350
PrefilterEngine_::pectx
void * pectx
Definition: detect.h:1362
PrefilterInit
void PrefilterInit(DetectEngineCtx *de_ctx)
Definition: detect-engine-prefilter.c:633
DetectEngineCtx_::sm_types_prefilter
bool sm_types_prefilter[DETECT_TBLSIZE]
Definition: detect.h:982
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:348
PACKET_PROFILING_DETECT_START
#define PACKET_PROFILING_DETECT_START(p, id)
Definition: util-profiling.h:234
PrefilterMpmCtx::transforms
const DetectEngineTransforms * transforms
Definition: detect-engine-prefilter.c:710
PrefilterStore_::name
const char * name
Definition: detect-engine-prefilter.h:30
SCFree
#define SCFree(p)
Definition: util-mem.h:61
PrefilterAppendPayloadEngine
int PrefilterAppendPayloadEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterFunc)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx), void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:236
HashListTableBucket_
Definition: util-hashlist.h:28
PrefilterMpmPktCtx::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-engine-prefilter.c:781
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterTxFunc)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t idx, const uint8_t flags), AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:270
DetectBufferMpmRegistery_::pkt_v1
struct DetectBufferMpmRegistery_::@87::@90 pkt_v1
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
ALPROTO_FAILED
@ ALPROTO_FAILED
Definition: app-layer-protos.h:70
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:48
PrefilterEngine_::ctx
union PrefilterEngine_::@100 ctx
PrefilterEngine_::is_last
bool is_last
Definition: detect.h:1374
suricata.h
PrefilterEngineList_::tx_min_progress
uint8_t tx_min_progress
Definition: detect.h:1323
PrefilterSetupRuleGroup
void PrefilterSetupRuleGroup(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Definition: detect-engine-prefilter.c:417
likely
#define likely(expr)
Definition: util-optimize.h:32
SigGroupHead_::pkt_engines
PrefilterEngine * pkt_engines
Definition: detect.h:1425
SC_ERR_INITIALIZATION
@ SC_ERR_INITIALIZATION
Definition: util-error.h:75
MpmCtx_
Definition: util-mpm.h:88
PrefilterAppendEngine
int PrefilterAppendEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, void(*PrefilterFunc)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx), void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:202
SigIntId
#define SigIntId
Definition: suricata-common.h:296
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:463
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PrefilterFreeEnginesList
void PrefilterFreeEnginesList(PrefilterEngineList *list)
Definition: detect-engine-prefilter.c:354
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
PrefilterMpmCtx::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-engine-prefilter.c:709
DetectRunPrefilterTx
void DetectRunPrefilterTx(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p, const uint8_t ipproto, const uint8_t flow_flags, const AppProto alproto, void *alstate, DetectTransaction *tx)
run prefilter engines on a transaction
Definition: detect-engine-prefilter.c:93
PrefilterRuleStore_::rule_id_array
SigIntId * rule_id_array
Definition: util-prefilter.h:36