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 
120  PREFILTER_PROFILING_START(det_ctx);
121  engine->cb.PrefilterTx(det_ctx, engine->pectx, p, p->flow, tx->tx_ptr, tx->tx_id,
122  tx->tx_data_ptr, 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 {
162  PREFILTER_PROFILING_START(det_ctx);
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) {
181  PREFILTER_PROFILING_START(det_ctx);
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  PrefilterTxFn PrefilterTxFunc, AppProto alproto, int tx_min_progress, void *pectx,
272  void (*FreeFunc)(void *pectx), const char *name)
273 {
274  if (sgh == NULL || PrefilterTxFunc == NULL || pectx == NULL)
275  return -1;
276 
277  PrefilterEngineList *e = SCMallocAligned(sizeof(*e), CLS);
278  if (e == NULL)
279  return -1;
280  memset(e, 0x00, sizeof(*e));
281 
282  e->PrefilterTx = PrefilterTxFunc;
283  e->pectx = pectx;
284  e->alproto = alproto;
285  // TODO change function prototype ?
286  DEBUG_VALIDATE_BUG_ON(tx_min_progress > UINT8_MAX);
287  e->tx_min_progress = (uint8_t)tx_min_progress;
288  e->Free = FreeFunc;
289 
290  if (sgh->init->tx_engines == NULL) {
291  sgh->init->tx_engines = e;
292  } else {
294  while (t->next != NULL) {
295  t = t->next;
296  }
297 
298  t->next = e;
299  e->id = t->id + 1;
300  }
301 
302  e->name = name;
303  e->gid = PrefilterStoreGetId(de_ctx, e->name, e->Free);
304  return 0;
305 }
306 
308  PrefilterFrameFn PrefilterFrameFunc, AppProto alproto, uint8_t frame_type, void *pectx,
309  void (*FreeFunc)(void *pectx), const char *name)
310 {
311  if (sgh == NULL || PrefilterFrameFunc == NULL || pectx == NULL)
312  return -1;
313 
314  PrefilterEngineList *e = SCMallocAligned(sizeof(*e), CLS);
315  if (e == NULL)
316  return -1;
317  memset(e, 0x00, sizeof(*e));
318 
319  e->frame_type = frame_type;
320  e->alproto = alproto;
321  e->PrefilterFrame = PrefilterFrameFunc;
322  e->pectx = pectx;
323  e->Free = FreeFunc;
324 
325  if (sgh->init->frame_engines == NULL) {
326  sgh->init->frame_engines = e;
327  } else {
329  while (t->next != NULL) {
330  t = t->next;
331  }
332 
333  t->next = e;
334  e->id = t->id + 1;
335  }
336 
337  e->name = name;
338  e->gid = PrefilterStoreGetId(de_ctx, e->name, e->Free);
339  return 0;
340 }
341 
342 static void PrefilterFreeEngineList(PrefilterEngineList *e)
343 {
344  if (e->Free && e->pectx) {
345  e->Free(e->pectx);
346  }
347  SCFreeAligned(e);
348 }
349 
351 {
352  PrefilterEngineList *t = list;
353 
354  while (t != NULL) {
356  PrefilterFreeEngineList(t);
357  t = next;
358  }
359 }
360 
361 static void PrefilterFreeEngines(const DetectEngineCtx *de_ctx, PrefilterEngine *list)
362 {
363  PrefilterEngine *t = list;
364 
365  while (1) {
366  const PrefilterStore *s = PrefilterStoreGetStore(de_ctx, t->gid);
367  if (s && s->FreeFunc && t->pectx) {
368  s->FreeFunc(t->pectx);
369  }
370 
371  if (t->is_last)
372  break;
373  t++;
374  }
375  SCFreeAligned(list);
376 }
377 
379 {
380  if (sgh->pkt_engines) {
381  PrefilterFreeEngines(de_ctx, sgh->pkt_engines);
382  sgh->pkt_engines = NULL;
383  }
384  if (sgh->payload_engines) {
385  PrefilterFreeEngines(de_ctx, sgh->payload_engines);
386  sgh->payload_engines = NULL;
387  }
388  if (sgh->tx_engines) {
389  PrefilterFreeEngines(de_ctx, sgh->tx_engines);
390  sgh->tx_engines = NULL;
391  }
392  if (sgh->frame_engines) {
393  PrefilterFreeEngines(de_ctx, sgh->frame_engines);
394  sgh->frame_engines = NULL;
395  }
396 }
397 
398 static int PrefilterSetupRuleGroupSortHelper(const void *a, const void *b)
399 {
400  const PrefilterEngine *s0 = a;
401  const PrefilterEngine *s1 = b;
402  if (s1->ctx.tx_min_progress == s0->ctx.tx_min_progress) {
403  if (s1->alproto == s0->alproto) {
404  return s0->local_id > s1->local_id ? 1 : -1;
405  } else {
406  return s0->alproto > s1->alproto ? 1 : -1;
407  }
408  } else {
409  return s0->ctx.tx_min_progress > s1->ctx.tx_min_progress ? 1 : -1;
410  }
411 }
412 
414 {
415  int r = PatternMatchPrepareGroup(de_ctx, sgh);
416  if (r != 0) {
417  FatalError("failed to set up pattern matching");
418  }
419 
420  /* set up engines if needed - when prefilter is set to auto we run
421  * all engines, otherwise only those that have been forced by the
422  * prefilter keyword. */
424  for (int i = 0; i < DETECT_TBLSIZE; i++)
425  {
426  if (sigmatch_table[i].SetupPrefilter != NULL &&
427  (setting == DETECT_PREFILTER_AUTO ||
429  {
431  }
432  }
433 
434  /* we have lists of engines in sgh->init now. Lets setup the
435  * match arrays */
437  if (sgh->init->pkt_engines != NULL) {
438  uint32_t cnt = 0;
439  for (el = sgh->init->pkt_engines ; el != NULL; el = el->next) {
440  cnt++;
441  }
443  if (sgh->pkt_engines == NULL) {
444  return;
445  }
446  memset(sgh->pkt_engines, 0x00, (cnt * sizeof(PrefilterEngine)));
447 
448  PrefilterEngine *e = sgh->pkt_engines;
449  for (el = sgh->init->pkt_engines ; el != NULL; el = el->next) {
450  e->local_id = el->id;
451  e->cb.Prefilter = el->Prefilter;
452  e->pectx = el->pectx;
453  el->pectx = NULL; // e now owns the ctx
454  e->gid = el->gid;
455  if (el->next == NULL) {
456  e->is_last = true;
457  }
458  e++;
459  }
460  }
461  if (sgh->init->payload_engines != NULL) {
462  uint32_t cnt = 0;
463  for (el = sgh->init->payload_engines ; el != NULL; el = el->next) {
464  cnt++;
465  }
467  if (sgh->payload_engines == NULL) {
468  return;
469  }
470  memset(sgh->payload_engines, 0x00, (cnt * sizeof(PrefilterEngine)));
471 
473  for (el = sgh->init->payload_engines ; el != NULL; el = el->next) {
474  e->local_id = el->id;
475  e->cb.Prefilter = el->Prefilter;
476  e->pectx = el->pectx;
477  el->pectx = NULL; // e now owns the ctx
478  e->gid = el->gid;
479  if (el->next == NULL) {
480  e->is_last = true;
481  }
482  e++;
483  }
484  }
485  if (sgh->init->tx_engines != NULL) {
486  uint32_t cnt = 0;
487  for (el = sgh->init->tx_engines ; el != NULL; el = el->next) {
488  cnt++;
489  }
490  sgh->tx_engines = SCMallocAligned(cnt * sizeof(PrefilterEngine), CLS);
491  if (sgh->tx_engines == NULL) {
492  return;
493  }
494  memset(sgh->tx_engines, 0x00, (cnt * sizeof(PrefilterEngine)));
495 
496  uint16_t local_id = 0;
497  PrefilterEngine *e = sgh->tx_engines;
498  for (el = sgh->init->tx_engines ; el != NULL; el = el->next) {
499  e->local_id = local_id++;
500  e->alproto = el->alproto;
502  e->cb.PrefilterTx = el->PrefilterTx;
503  e->pectx = el->pectx;
504  el->pectx = NULL; // e now owns the ctx
505  e->gid = el->gid;
506  e++;
507  }
508 
509  /* sort by tx_min_progress, then alproto, then local_id */
510  qsort(sgh->tx_engines, local_id, sizeof(PrefilterEngine),
511  PrefilterSetupRuleGroupSortHelper);
512  sgh->tx_engines[local_id - 1].is_last = true;
513  sgh->tx_engines[local_id - 1].is_last_for_progress = true;
514 
515  PrefilterEngine *engine;
516 
517  /* per alproto to set is_last_for_progress per alproto because the inspect
518  * loop skips over engines that are not the correct alproto */
519  for (AppProto a = 1; a < ALPROTO_FAILED; a++) {
520  int last_tx_progress = 0;
521  bool last_tx_progress_set = false;
522  PrefilterEngine *prev_engine = NULL;
523  engine = sgh->tx_engines;
524  do {
525  BUG_ON(engine->ctx.tx_min_progress < last_tx_progress);
526  if (engine->alproto == a) {
527  if (last_tx_progress_set && engine->ctx.tx_min_progress > last_tx_progress) {
528  if (prev_engine) {
529  prev_engine->is_last_for_progress = true;
530  }
531  }
532 
533  last_tx_progress_set = true;
534  prev_engine = engine;
535  } else {
536  if (prev_engine) {
537  prev_engine->is_last_for_progress = true;
538  }
539  }
540  last_tx_progress = engine->ctx.tx_min_progress;
541  if (engine->is_last)
542  break;
543  engine++;
544  } while (1);
545  }
546 #ifdef DEBUG
547  SCLogDebug("sgh %p", sgh);
548  engine = sgh->tx_engines;
549  do {
550  SCLogDebug("engine: gid %u alproto %s tx_min_progress %d is_last %s "
551  "is_last_for_progress %s",
552  engine->gid, AppProtoToString(engine->alproto), engine->ctx.tx_min_progress,
553  engine->is_last ? "true" : "false",
554  engine->is_last_for_progress ? "true" : "false");
555  if (engine->is_last)
556  break;
557  engine++;
558  } while (1);
559 #endif
560  }
561  if (sgh->init->frame_engines != NULL) {
562  uint32_t cnt = 0;
563  for (el = sgh->init->frame_engines; el != NULL; el = el->next) {
564  cnt++;
565  }
567  if (sgh->frame_engines == NULL) {
568  return;
569  }
570  memset(sgh->frame_engines, 0x00, (cnt * sizeof(PrefilterEngine)));
571 
572  PrefilterEngine *e = sgh->frame_engines;
573  for (el = sgh->init->frame_engines; el != NULL; el = el->next) {
574  e->local_id = el->id;
575  e->ctx.frame_type = el->frame_type;
577  e->alproto = el->alproto;
578  e->pectx = el->pectx;
579  el->pectx = NULL; // e now owns the ctx
580  e->gid = el->gid;
581  if (el->next == NULL) {
582  e->is_last = true;
583  }
584  e++;
585  }
586  }
587 }
588 
589 /* hash table for assigning a unique id to each engine type. */
590 
591 static uint32_t PrefilterStoreHashFunc(HashListTable *ht, void *data, uint16_t datalen)
592 {
593  PrefilterStore *ctx = data;
594 
595  uint32_t hash = strlen(ctx->name);
596 
597  for (size_t u = 0; u < strlen(ctx->name); u++) {
598  hash += ctx->name[u];
599  }
600 
601  hash %= ht->array_size;
602  return hash;
603 }
604 
605 static char PrefilterStoreCompareFunc(void *data1, uint16_t len1,
606  void *data2, uint16_t len2)
607 {
608  PrefilterStore *ctx1 = data1;
609  PrefilterStore *ctx2 = data2;
610  return (strcmp(ctx1->name, ctx2->name) == 0);
611 }
612 
613 static void PrefilterStoreFreeFunc(void *ptr)
614 {
615  SCFree(ptr);
616 }
617 
619 {
620  if (de_ctx->prefilter_hash_table != NULL) {
622  }
623 }
624 
626 {
628 
630  PrefilterStoreHashFunc,
631  PrefilterStoreCompareFunc,
632  PrefilterStoreFreeFunc);
634 }
635 
636 static int PrefilterStoreGetId(DetectEngineCtx *de_ctx,
637  const char *name, void (*FreeFunc)(void *))
638 {
639  PrefilterStore ctx = { name, FreeFunc, 0 };
640 
642 
643  SCLogDebug("looking up %s", name);
644 
646  if (rctx != NULL) {
647  return rctx->id;
648  }
649 
650  PrefilterStore *actx = SCCalloc(1, sizeof(*actx));
651  if (actx == NULL) {
652  return -1;
653  }
654 
655  actx->name = name;
656  actx->FreeFunc = FreeFunc;
657  actx->id = de_ctx->prefilter_id++;
658  SCLogDebug("prefilter engine %s has profile id %u", actx->name, actx->id);
659 
660  int ret = HashListTableAdd(de_ctx->prefilter_hash_table, actx, 0);
661  if (ret != 0) {
662  SCFree(actx);
663  return -1;
664  }
665 
666  int r = actx->id;
667  return r;
668 }
669 
670 /** \warning slow */
671 static const PrefilterStore *PrefilterStoreGetStore(const DetectEngineCtx *de_ctx,
672  const uint32_t id)
673 {
674 
675  const PrefilterStore *store = NULL;
676  if (de_ctx->prefilter_hash_table != NULL) {
678  for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) {
680  if (ctx->id == id) {
681  store = ctx;
682  break;
683  }
684  }
685  }
686  return store;
687 }
688 
689 #ifdef PROFILING
690 const char *PrefilterStoreGetName(const uint32_t id)
691 {
692  return NULL;
693 }
694 #endif
695 
696 #include "util-print.h"
697 
698 typedef struct PrefilterMpmCtx {
699  int list_id;
701  const MpmCtx *mpm_ctx;
704 
705 /** \brief Generic Mpm prefilter callback
706  *
707  * \param det_ctx detection engine thread ctx
708  * \param p packet to inspect
709  * \param f flow to inspect
710  * \param txv tx to inspect
711  * \param pectx inspection context
712  */
713 static void PrefilterMpm(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f,
714  void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
715 {
716  SCEnter();
717 
718  const PrefilterMpmCtx *ctx = (const PrefilterMpmCtx *)pectx;
719  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
720  SCLogDebug("running on list %d", ctx->list_id);
721 
722  InspectionBuffer *buffer = ctx->GetData(det_ctx, ctx->transforms,
723  f, flags, txv, ctx->list_id);
724  if (buffer == NULL)
725  return;
726 
727  const uint32_t data_len = buffer->inspect_len;
728  const uint8_t *data = buffer->inspect;
729 
730  SCLogDebug("mpm'ing buffer:");
731  //PrintRawDataFp(stdout, data, data_len);
732 
733  if (data != NULL && data_len >= mpm_ctx->minlen) {
734  (void)mpm_table[mpm_ctx->mpm_type].Search(
735  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, data, data_len);
736  PREFILTER_PROFILING_ADD_BYTES(det_ctx, data_len);
737  }
738 }
739 
740 static void PrefilterGenericMpmFree(void *ptr)
741 {
742  SCFree(ptr);
743 }
744 
746  const DetectBufferMpmRegistry *mpm_reg, int list_id)
747 {
748  SCEnter();
749  PrefilterMpmCtx *pectx = SCCalloc(1, sizeof(*pectx));
750  if (pectx == NULL)
751  return -1;
752  pectx->list_id = list_id;
753  pectx->GetData = mpm_reg->app_v2.GetData;
754  pectx->mpm_ctx = mpm_ctx;
755  pectx->transforms = &mpm_reg->transforms;
756 
758  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress,
759  pectx, PrefilterGenericMpmFree, mpm_reg->pname);
760  if (r != 0) {
761  SCFree(pectx);
762  }
763  return r;
764 }
765 
766 /* generic mpm for pkt engines */
767 
768 typedef struct PrefilterMpmPktCtx {
769  int list_id;
771  const MpmCtx *mpm_ctx;
774 
775 /** \brief Generic Mpm prefilter callback
776  *
777  * \param det_ctx detection engine thread ctx
778  * \param p packet to inspect
779  * \param f flow to inspect
780  * \param txv tx to inspect
781  * \param pectx inspection context
782  */
783 static void PrefilterMpmPkt(DetectEngineThreadCtx *det_ctx,
784  Packet *p, const void *pectx)
785 {
786  SCEnter();
787 
788  const PrefilterMpmPktCtx *ctx = (const PrefilterMpmPktCtx *)pectx;
789  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
790  SCLogDebug("running on list %d", ctx->list_id);
791 
792  InspectionBuffer *buffer = ctx->GetData(det_ctx, ctx->transforms,
793  p, ctx->list_id);
794  if (buffer == NULL)
795  return;
796 
797  const uint32_t data_len = buffer->inspect_len;
798  const uint8_t *data = buffer->inspect;
799 
800  SCLogDebug("mpm'ing buffer:");
801  //PrintRawDataFp(stdout, data, data_len);
802 
803  if (data != NULL && data_len >= mpm_ctx->minlen) {
804  (void)mpm_table[mpm_ctx->mpm_type].Search(
805  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, data, data_len);
806  PREFILTER_PROFILING_ADD_BYTES(det_ctx, data_len);
807  }
808 }
809 
810 static void PrefilterMpmPktFree(void *ptr)
811 {
812  SCFree(ptr);
813 }
814 
816  const DetectBufferMpmRegistry *mpm_reg, int list_id)
817 {
818  SCEnter();
819  PrefilterMpmPktCtx *pectx = SCCalloc(1, sizeof(*pectx));
820  if (pectx == NULL)
821  return -1;
822  pectx->list_id = list_id;
823  pectx->GetData = mpm_reg->pkt_v1.GetData;
824  pectx->mpm_ctx = mpm_ctx;
825  pectx->transforms = &mpm_reg->transforms;
826 
827  int r = PrefilterAppendEngine(de_ctx, sgh, PrefilterMpmPkt,
828  pectx, PrefilterMpmPktFree, mpm_reg->pname);
829  if (r != 0) {
830  SCFree(pectx);
831  }
832  return r;
833 }
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:57
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:815
PrefilterEngineList_::frame_type
uint8_t frame_type
Definition: detect.h:1367
SigGroupHead_::tx_engines
PrefilterEngine * tx_engines
Definition: detect.h:1463
Packet_::proto
uint8_t proto
Definition: decode.h:452
DetectTransaction_::tx_data_ptr
struct AppLayerTxData * tx_data_ptr
Definition: detect-engine-prefilter.h:34
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:1405
PatternMatchPrepareGroup
int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh)
Prepare the pattern matcher ctx in a sig group head.
Definition: detect-engine-mpm.c:2202
PrefilterMpmPktCtx
struct PrefilterMpmPktCtx PrefilterMpmPktCtx
PrefilterStore_::FreeFunc
void(* FreeFunc)(void *)
Definition: detect-engine-prefilter.h:46
PrefilterEngine_::ctx
union PrefilterEngine_::@101 ctx
PREFILTER_PROFILING_END
#define PREFILTER_PROFILING_END(ctx, profile_id)
Definition: util-profiling.h:277
PrefilterStore_
Definition: detect-engine-prefilter.h:44
CLS
#define CLS
Definition: suricata-common.h:56
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:40
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1444
DetectEngineTransforms
Definition: detect.h:405
PrefilterEngineList_::id
uint16_t id
Definition: detect.h:1359
PROF_DETECT_PF_PAYLOAD
@ PROF_DETECT_PF_PAYLOAD
Definition: suricata-common.h:446
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
Packet_::pcap_cnt
uint64_t pcap_cnt
Definition: decode.h:598
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:80
PrefilterMpmCtx::GetData
InspectionBufferGetDataPtr GetData
Definition: detect-engine-prefilter.c:700
InspectionBuffer
Definition: detect.h:370
Packet_::flags
uint32_t flags
Definition: decode.h:467
DETECT_TBLSIZE
@ DETECT_TBLSIZE
Definition: detect-engine-register.h:350
Flow_
Flow data structure.
Definition: flow.h:350
PREFILTER_PROFILING_START
#define PREFILTER_PROFILING_START(det_ctx)
Definition: util-profiling.h:261
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1200
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:411
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:74
PrefilterEngineList_::name
const char * name
Definition: detect.h:1382
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:835
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:295
PrefilterEngine_::tx_min_progress
uint8_t tx_min_progress
Definition: detect.h:1396
InspectionBufferGetPktDataPtr
InspectionBuffer *(* InspectionBufferGetPktDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Packet *p, const int list_id)
Definition: detect.h:472
PrefilterDeinit
void PrefilterDeinit(DetectEngineCtx *de_ctx)
Definition: detect-engine-prefilter.c:618
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:676
PrefilterMpmPktCtx
Definition: detect-engine-prefilter.c:768
detect-engine-frame.h
PrefilterEngineList_::Prefilter
void(* Prefilter)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
Definition: detect.h:1373
SigGroupHead_::payload_engines
PrefilterEngine * payload_engines
Definition: detect.h:1462
DetectEngineCtx_::prefilter_setting
enum DetectEnginePrefilterSetting prefilter_setting
Definition: detect.h:971
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@88::@90 app_v2
PKT_NOPAYLOAD_INSPECTION
#define PKT_NOPAYLOAD_INSPECTION
Definition: decode.h:994
PACKET_PROFILING_DETECT_END
#define PACKET_PROFILING_DETECT_END(p, id)
Definition: util-profiling.h:222
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:253
PrefilterEngine_::local_id
uint16_t local_id
Definition: detect.h:1388
PrefilterEngineList_::Free
void(* Free)(void *pectx)
Definition: detect.h:1380
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:307
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:689
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:578
DetectTransaction_::tx_progress
const int tx_progress
Definition: detect-engine-prefilter.h:40
detect-engine-prefilter.h
PrefilterStore_::id
uint32_t id
Definition: detect-engine-prefilter.h:47
HashListTableAdd
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:122
DETECT_PREFILTER_AUTO
@ DETECT_PREFILTER_AUTO
Definition: detect.h:817
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:1413
PROF_DETECT_PF_SORT1
@ PROF_DETECT_PF_SORT1
Definition: suricata-common.h:449
DetectEngineCtx_::prefilter_id
uint32_t prefilter_id
Definition: detect.h:1002
PrefilterEngineList_::next
struct PrefilterEngineList_ * next
Definition: detect.h:1377
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1280
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:474
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:56
PrefilterEngine_::cb
union PrefilterEngine_::@102 cb
DetectEngineCtx_::prefilter_hash_table
HashListTable * prefilter_hash_table
Definition: detect.h:1003
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:1362
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
PrefilterStoreGetName
const char * PrefilterStoreGetName(const uint32_t id)
Definition: detect-engine-prefilter.c:690
DetectEngineThreadCtx_
Definition: detect.h:1091
PrefilterEngine_
Definition: detect.h:1387
SigGroupHeadInitData_::tx_engines
PrefilterEngineList * tx_engines
Definition: detect.h:1433
PROF_DETECT_PF_PKT
@ PROF_DETECT_PF_PKT
Definition: suricata-common.h:445
PrefilterEngineList_::PrefilterFrame
PrefilterFrameFn PrefilterFrame
Definition: detect.h:1375
PrefilterEngineList_::pectx
void * pectx
Definition: detect.h:1371
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
PrefilterEngineList_::PrefilterTx
PrefilterTxFn PrefilterTx
Definition: detect.h:1374
PrefilterEngineList_::gid
uint32_t gid
Definition: detect.h:1384
PrefilterMpmPktCtx::GetData
InspectionBufferGetPktDataPtr GetData
Definition: detect-engine-prefilter.c:770
PKT_DETECT_HAS_STREAMDATA
#define PKT_DETECT_HAS_STREAMDATA
Definition: decode.h:1044
PrefilterMpmCtx
struct PrefilterMpmCtx PrefilterMpmCtx
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:745
PrefilterFrameFn
void(* PrefilterFrameFn)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, const struct Frames *frames, const struct Frame *frame)
Definition: detect.h:1351
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1467
DetectBufferMpmRegistry_::pkt_v1
struct DetectBufferMpmRegistry_::@88::@91 pkt_v1
app-layer-parser.h
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
util-profiling.h
DetectTransaction_::tx_id
const uint64_t tx_id
Definition: detect-engine-prefilter.h:33
SCReturn
#define SCReturn
Definition: util-debug.h:273
PrefilterEngine_::alproto
AppProto alproto
Definition: detect.h:1391
DetectTransaction_::prefilter_flags
uint64_t prefilter_flags
Definition: detect-engine-prefilter.h:37
Packet_
Definition: decode.h:430
SCFreeAligned
#define SCFreeAligned(p)
Definition: util-mem.h:77
PrefilterEngine_::PrefilterFrame
PrefilterFrameFn PrefilterFrame
Definition: detect.h:1407
PrefilterEngine_::frame_type
uint8_t frame_type
Definition: detect.h:1397
HashListTable_
Definition: util-hashlist.h:37
BIT_U64
#define BIT_U64(n)
Definition: suricata-common.h:401
PrefilterTxFn
void(* PrefilterTxFn)(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f, void *tx, const uint64_t tx_id, const AppLayerTxData *tx_data, const uint8_t flags)
Definition: detect.h:1355
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:163
SigGroupHead_::frame_engines
PrefilterEngine * frame_engines
Definition: detect.h:1464
PrefilterMpmCtx::list_id
int list_id
Definition: detect-engine-prefilter.c:699
DetectEngineThreadCtx_::mtc
MpmThreadCtx mtc
Definition: detect.h:1199
PrefilterMpmPktCtx::list_id
int list_id
Definition: detect-engine-prefilter.c:769
SigGroupHeadInitData_::pkt_engines
PrefilterEngineList * pkt_engines
Definition: detect.h:1431
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1354
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:287
Packet_::flow
struct Flow_ * flow
Definition: decode.h:469
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
SigGroupHeadInitData_::frame_engines
PrefilterEngineList * frame_engines
Definition: detect.h:1434
SigGroupHeadInitData_::payload_engines
PrefilterEngineList * payload_engines
Definition: detect.h:1432
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:88
PROF_DETECT_PF_RECORD
@ PROF_DETECT_PF_RECORD
Definition: suricata-common.h:448
SCMallocAligned
#define SCMallocAligned(size, align)
Definition: util-mem.h:68
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
DetectEnginePrefilterSetting
DetectEnginePrefilterSetting
Definition: detect.h:815
DetectTransaction_
Definition: detect-engine-prefilter.h:31
PrefilterEngineList_
Definition: detect.h:1358
PrefilterCleanupRuleGroup
void PrefilterCleanupRuleGroup(const DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Definition: detect-engine-prefilter.c:378
PrefilterEngine_::gid
uint32_t gid
Definition: detect.h:1411
FatalError
#define FatalError(...)
Definition: util-debug.h:502
PrefilterMpmPktCtx::transforms
const DetectEngineTransforms * transforms
Definition: detect-engine-prefilter.c:772
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, PrefilterTxFn PrefilterTxFunc, AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:270
PrefilterMpmCtx
Definition: detect-engine-prefilter.c:698
util-validate.h
DetectTransaction_::tx_ptr
void * tx_ptr
Definition: detect-engine-prefilter.h:32
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:373
PrefilterEngine_::pectx
void * pectx
Definition: detect.h:1402
PrefilterInit
void PrefilterInit(DetectEngineCtx *de_ctx)
Definition: detect-engine-prefilter.c:625
DetectEngineCtx_::sm_types_prefilter
bool sm_types_prefilter[DETECT_TBLSIZE]
Definition: detect.h:1018
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:371
PACKET_PROFILING_DETECT_START
#define PACKET_PROFILING_DETECT_START(p, id)
Definition: util-profiling.h:215
PrefilterMpmCtx::transforms
const DetectEngineTransforms * transforms
Definition: detect-engine-prefilter.c:702
PrefilterStore_::name
const char * name
Definition: detect-engine-prefilter.h:45
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:771
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:47
PrefilterEngine_::is_last
bool is_last
Definition: detect.h:1412
suricata.h
PrefilterEngineList_::tx_min_progress
uint8_t tx_min_progress
Definition: detect.h:1365
PrefilterSetupRuleGroup
void PrefilterSetupRuleGroup(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Definition: detect-engine-prefilter.c:413
likely
#define likely(expr)
Definition: util-optimize.h:32
SigGroupHead_::pkt_engines
PrefilterEngine * pkt_engines
Definition: detect.h:1461
PrefilterEngine_::PrefilterTx
PrefilterTxFn PrefilterTx
Definition: detect.h:1406
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:315
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:449
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PrefilterMpm
Definition: detect-dns-answer-name.c:33
PrefilterFreeEnginesList
void PrefilterFreeEnginesList(PrefilterEngineList *list)
Definition: detect-engine-prefilter.c:350
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:104
PrefilterMpmCtx::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-engine-prefilter.c:701
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:38
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:678