suricata
detect-engine-mpm.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
23  *
24  * Multi pattern matcher
25  */
26 
27 #include "suricata.h"
28 #include "suricata-common.h"
29 
30 #include "app-layer-protos.h"
31 
32 #include "decode.h"
33 #include "detect.h"
34 #include "detect-engine.h"
35 #include "detect-engine-siggroup.h"
36 #include "detect-engine-mpm.h"
37 #include "detect-engine-iponly.h"
38 #include "detect-parse.h"
40 #include "util-mpm.h"
41 #include "util-memcmp.h"
42 #include "util-memcpy.h"
43 #include "conf.h"
44 #include "detect-fast-pattern.h"
45 
46 #include "detect-tcphdr.h"
47 #include "detect-udphdr.h"
48 
49 #include "flow.h"
50 #include "flow-var.h"
51 #include "detect-flow.h"
52 
53 #include "detect-content.h"
54 
55 #include "detect-engine-payload.h"
56 
57 #include "stream.h"
58 
59 #include "util-misc.h"
60 #include "util-enum.h"
61 #include "util-debug.h"
62 #include "util-print.h"
63 #include "util-validate.h"
64 #include "util-hash-string.h"
65 
66 const char *builtin_mpms[] = {
67  "toserver TCP packet",
68  "toclient TCP packet",
69  "toserver TCP stream",
70  "toclient TCP stream",
71  "toserver UDP packet",
72  "toclient UDP packet",
73  "other IP packet",
74 
75  NULL };
76 
77 /* Registry for mpm keywords
78  *
79  * Keywords are registered at engine start up
80  */
81 
82 static DetectBufferMpmRegistry *g_mpm_list[DETECT_BUFFER_MPM_TYPE_SIZE] = { NULL, NULL, NULL };
83 static int g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_SIZE] = { 0, 0, 0 };
84 
85 /** \brief register a MPM engine
86  *
87  * \note to be used at start up / registration only. Errors are fatal.
88  */
89 static void RegisterInternal(const char *name, int direction, int priority,
90  PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData,
91  InspectionMultiBufferGetDataPtr GetMultiData, AppProto alproto, int tx_min_progress)
92 {
93  SCLogDebug("registering %s/%d/%d/%p/%p/%u/%d", name, direction, priority,
94  PrefilterRegister, GetData, alproto, tx_min_progress);
95 
96  BUG_ON(tx_min_progress >= 48);
97 
98  if (PrefilterRegister == PrefilterGenericMpmRegister && GetData == NULL) {
99  // must register GetData with PrefilterGenericMpmRegister
100  abort();
101  }
102 
105  int sm_list = DetectBufferTypeGetByName(name);
106  if (sm_list == -1) {
107  FatalError("MPM engine registration for %s failed", name);
108  }
109 
110  // every HTTP2 can be accessed from DOH2
111  if (alproto == ALPROTO_HTTP2 || alproto == ALPROTO_DNS) {
112  RegisterInternal(name, direction, priority, PrefilterRegister, GetData, GetMultiData,
113  ALPROTO_DOH2, tx_min_progress);
114  }
115  DetectBufferMpmRegistry *am = SCCalloc(1, sizeof(*am));
116  BUG_ON(am == NULL);
117  am->name = name;
118  snprintf(am->pname, sizeof(am->pname), "%s", am->name);
119  am->direction = direction;
120  DEBUG_VALIDATE_BUG_ON(sm_list < 0 || sm_list > INT16_MAX);
121  am->sm_list = (int16_t)sm_list;
122  am->sm_list_base = (int16_t)sm_list;
123  am->priority = priority;
125 
126  am->PrefilterRegisterWithListId = PrefilterRegister;
127  if (GetData != NULL) {
128  am->app_v2.GetData = GetData;
129  } else if (GetMultiData != NULL) {
130  am->app_v2.GetMultiData = GetMultiData;
131  }
132  am->app_v2.alproto = alproto;
133  am->app_v2.tx_min_progress = tx_min_progress;
134 
135  if (g_mpm_list[DETECT_BUFFER_MPM_TYPE_APP] == NULL) {
136  g_mpm_list[DETECT_BUFFER_MPM_TYPE_APP] = am;
137  } else {
139  while (t->next != NULL) {
140  t = t->next;
141  }
142 
143  t->next = am;
144  am->id = t->id + 1;
145  }
146  g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_APP]++;
147 
148  SupportFastPatternForSigMatchList(sm_list, priority);
149 }
150 
151 void DetectAppLayerMpmRegister(const char *name, int direction, int priority,
152  PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData,
153  AppProto alproto, int tx_min_progress)
154 {
155  RegisterInternal(
156  name, direction, priority, PrefilterRegister, GetData, NULL, alproto, tx_min_progress);
157 }
158 
159 void DetectAppLayerMpmMultiRegister(const char *name, int direction, int priority,
160  PrefilterRegisterFunc PrefilterRegister, InspectionMultiBufferGetDataPtr GetData,
161  AppProto alproto, int tx_min_progress)
162 {
163  RegisterInternal(
164  name, direction, priority, PrefilterRegister, NULL, GetData, alproto, tx_min_progress);
165 }
166 
167 /** \brief copy a mpm engine from parent_id, add in transforms */
169  const int id, const int parent_id,
170  DetectEngineTransforms *transforms)
171 {
172  SCLogDebug("registering %d/%d", id, parent_id);
173 
175  while (t) {
176  if (t->sm_list == parent_id) {
177  DetectBufferMpmRegistry *am = SCCalloc(1, sizeof(*am));
178  BUG_ON(am == NULL);
179  am->name = t->name;
180  am->direction = t->direction;
181  DEBUG_VALIDATE_BUG_ON(id < 0 || id > INT16_MAX);
182  am->sm_list = (uint16_t)id; // use new id
183  am->sm_list_base = t->sm_list;
186  am->app_v2.GetData = t->app_v2.GetData;
187  am->app_v2.alproto = t->app_v2.alproto;
188  am->app_v2.tx_min_progress = t->app_v2.tx_min_progress;
189  am->priority = t->priority;
192  de_ctx, am->name, am->sm_list, am->app_v2.alproto);
193  am->next = t->next;
194  if (transforms) {
195  memcpy(&am->transforms, transforms, sizeof(*transforms));
196 
197  /* create comma separated string of the names of the
198  * transforms and then shorten it if necessary. Finally
199  * use it to construct the 'profile' name for the engine */
200  char xforms[1024] = "";
201  for (int i = 0; i < transforms->cnt; i++) {
202  char ttstr[64];
203  (void)snprintf(ttstr,sizeof(ttstr), "%s,",
204  sigmatch_table[transforms->transforms[i].transform].name);
205  strlcat(xforms, ttstr, sizeof(xforms));
206  }
207  xforms[strlen(xforms)-1] = '\0';
208 
209  size_t space = sizeof(am->pname) - strlen(am->name) - 3;
210  char toprint[space + 1];
211  memset(toprint, 0x00, space + 1);
212  if (space < strlen(xforms)) {
213  ShortenString(xforms, toprint, space, '~');
214  } else {
215  strlcpy(toprint, xforms,sizeof(toprint));
216  }
217  (void)snprintf(am->pname, sizeof(am->pname), "%s#%d (%s)",
218  am->name, id, toprint);
219  } else {
220  (void)snprintf(am->pname, sizeof(am->pname), "%s#%d",
221  am->name, id);
222  }
223  am->id = de_ctx->app_mpms_list_cnt++;
224 
226  t->next = am;
227  SCLogDebug("copied mpm registration for %s id %u "
228  "with parent %u and GetData %p",
229  t->name, id, parent_id, am->app_v2.GetData);
230  t = am;
231  }
232  t = t->next;
233  }
234 }
235 
237 {
238  const DetectBufferMpmRegistry *list = g_mpm_list[DETECT_BUFFER_MPM_TYPE_APP];
240  while (list != NULL) {
241  DetectBufferMpmRegistry *n = SCCalloc(1, sizeof(*n));
242  BUG_ON(n == NULL);
243 
244  *n = *list;
245  n->next = NULL;
246 
247  if (toadd == NULL) {
248  toadd = n;
249  de_ctx->app_mpms_list = n;
250  } else {
251  toadd->next = n;
252  toadd = toadd->next;
253  }
254 
255  /* default to whatever the global setting is */
257 
258  /* see if we use a unique or shared mpm ctx for this type */
259  int confshared = 0;
260  char confstring[256] = "detect.mpm.";
261  strlcat(confstring, n->name, sizeof(confstring));
262  strlcat(confstring, ".shared", sizeof(confstring));
263  if (ConfGetBool(confstring, &confshared) == 1)
264  shared = confshared;
265 
266  if (shared == 0) {
267  SCLogDebug("using unique mpm ctx' for %s", n->name);
269  } else {
270  SCLogDebug("using shared mpm ctx' for %s", n->name);
271  n->sgh_mpm_context =
273  }
274 
275  list = list->next;
276  }
278  SCLogDebug("mpm: de_ctx app_mpms_list %p %u",
280 }
281 
282 /**
283  * \brief initialize mpm contexts for applayer buffers that are in
284  * "single or "shared" mode.
285  */
287 {
288  int r = 0;
290  while (am != NULL) {
291  int dir = (am->direction == SIG_FLAG_TOSERVER) ? 1 : 0;
292 
294  {
296  if (mpm_ctx != NULL) {
297  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
298  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
299  }
300  }
301  }
302  am = am->next;
303  }
304  return r;
305 }
306 
307 /** \brief register a MPM engine
308  *
309  * \note to be used at start up / registration only. Errors are fatal.
310  */
311 void DetectFrameMpmRegister(const char *name, int direction, int priority,
312  int (*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
313  const DetectBufferMpmRegistry *mpm_reg, int list_id),
314  AppProto alproto, uint8_t type)
315 {
316  SCLogDebug("registering %s/%d/%p/%s/%u", name, priority, PrefilterRegister,
317  AppProtoToString(alproto), type);
318 
322  int sm_list = DetectBufferTypeGetByName(name);
323  if (sm_list < 0 || sm_list > UINT16_MAX) {
324  FatalError("MPM engine registration for %s failed", name);
325  }
326 
327  DetectBufferMpmRegistry *am = SCCalloc(1, sizeof(*am));
328  BUG_ON(am == NULL);
329  am->name = name;
330  snprintf(am->pname, sizeof(am->pname), "%s", am->name);
331  am->sm_list = (uint16_t)sm_list;
332  am->direction = direction;
333  am->priority = priority;
335 
336  am->PrefilterRegisterWithListId = PrefilterRegister;
337  am->frame_v1.alproto = alproto;
338  am->frame_v1.type = type;
339  SCLogDebug("type %u", type);
340  SCLogDebug("am type %u", am->frame_v1.type);
341 
342  if (g_mpm_list[DETECT_BUFFER_MPM_TYPE_FRAME] == NULL) {
343  g_mpm_list[DETECT_BUFFER_MPM_TYPE_FRAME] = am;
344  } else {
346  while (t->next != NULL) {
347  t = t->next;
348  }
349  t->next = am;
350  am->id = t->id + 1;
351  }
352  g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_FRAME]++;
353 
354  SupportFastPatternForSigMatchList(sm_list, priority);
355  SCLogDebug("%s/%d done", name, sm_list);
356 }
357 
358 /** \brief copy a mpm engine from parent_id, add in transforms */
359 void DetectFrameMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id,
360  DetectEngineTransforms *transforms)
361 {
362  SCLogDebug("registering %d/%d", id, parent_id);
363 
365  while (t) {
366  if (t->sm_list == parent_id) {
367  DetectBufferMpmRegistry *am = SCCalloc(1, sizeof(*am));
368  BUG_ON(am == NULL);
369  am->name = t->name;
370  snprintf(am->pname, sizeof(am->pname), "%s#%d", am->name, id);
371  DEBUG_VALIDATE_BUG_ON(id < 0 || id > UINT16_MAX);
372  am->sm_list = (uint16_t)id; // use new id
373  am->sm_list_base = t->sm_list;
376  am->frame_v1 = t->frame_v1;
377  SCLogDebug("am type %u", am->frame_v1.type);
378  am->priority = t->priority;
379  am->direction = t->direction;
381  am->next = t->next;
382  if (transforms) {
383  memcpy(&am->transforms, transforms, sizeof(*transforms));
384  }
385  am->id = de_ctx->frame_mpms_list_cnt++;
386 
388  t->next = am;
389  SCLogDebug("copied mpm registration for %s id %u "
390  "with parent %u",
391  t->name, id, parent_id);
392  t = am;
393  }
394  t = t->next;
395  }
396 }
397 
398 void DetectEngineFrameMpmRegister(DetectEngineCtx *de_ctx, const char *name, int direction,
399  int priority,
400  int (*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
401  const DetectBufferMpmRegistry *mpm_reg, int list_id),
402  AppProto alproto, uint8_t type)
403 {
404  SCLogDebug("registering %s/%d/%p/%s/%u", name, priority, PrefilterRegister,
405  AppProtoToString(alproto), type);
406 
407  const int sm_list = DetectEngineBufferTypeRegister(de_ctx, name);
408  if (sm_list < 0 || sm_list > UINT16_MAX) {
409  FatalError("MPM engine registration for %s failed", name);
410  }
411 
415 
416  DetectBufferMpmRegistry *am = SCCalloc(1, sizeof(*am));
417  BUG_ON(am == NULL);
418  am->name = name;
419  snprintf(am->pname, sizeof(am->pname), "%s", am->name);
420  am->sm_list = (uint16_t)sm_list;
421  am->direction = direction;
422  am->priority = priority;
424 
425  am->PrefilterRegisterWithListId = PrefilterRegister;
426  am->frame_v1.alproto = alproto;
427  am->frame_v1.type = type;
428 
429  // TODO is it ok to do this here?
430 
431  /* default to whatever the global setting is */
433  /* see if we use a unique or shared mpm ctx for this type */
434  int confshared = 0;
435  if (ConfGetBool("detect.mpm.frame.shared", &confshared) == 1)
436  shared = confshared;
437 
438  if (shared == 0) {
440  } else {
441  am->sgh_mpm_context =
443  }
444 
445  if (de_ctx->frame_mpms_list == NULL) {
446  de_ctx->frame_mpms_list = am;
447  } else {
449  while (t->next != NULL) {
450  t = t->next;
451  }
452 
453  t->next = am;
454  }
456 
457  DetectEngineRegisterFastPatternForId(de_ctx, sm_list, priority);
458  SCLogDebug("%s/%d done", name, sm_list);
459 }
460 
462 {
463  const DetectBufferMpmRegistry *list = g_mpm_list[DETECT_BUFFER_MPM_TYPE_FRAME];
464  while (list != NULL) {
465  DetectBufferMpmRegistry *n = SCCalloc(1, sizeof(*n));
466  BUG_ON(n == NULL);
467 
468  *n = *list;
469  n->next = NULL;
470 
471  if (de_ctx->frame_mpms_list == NULL) {
472  de_ctx->frame_mpms_list = n;
473  } else {
475  while (t->next != NULL) {
476  t = t->next;
477  }
478 
479  t->next = n;
480  }
481 
482  /* default to whatever the global setting is */
484 
485  /* see if we use a unique or shared mpm ctx for this type */
486  int confshared = 0;
487  char confstring[256] = "detect.mpm.";
488  strlcat(confstring, n->name, sizeof(confstring));
489  strlcat(confstring, ".shared", sizeof(confstring));
490  if (ConfGetBool(confstring, &confshared) == 1)
491  shared = confshared;
492 
493  if (shared == 0) {
494  SCLogDebug("using unique mpm ctx' for %s", n->name);
496  } else {
497  SCLogDebug("using shared mpm ctx' for %s", n->name);
499  de_ctx, n->name, n->sm_list, n->frame_v1.alproto);
500  }
501 
502  list = list->next;
503  }
505  SCLogDebug("mpm: de_ctx frame_mpms_list %p %u", de_ctx->frame_mpms_list,
507 }
508 
509 /**
510  * \brief initialize mpm contexts for applayer buffers that are in
511  * "single or "shared" mode.
512  */
514 {
515  SCLogDebug("preparing frame mpm");
516  int r = 0;
518  while (am != NULL) {
519  SCLogDebug("am %p %s sgh_mpm_context %d", am, am->name, am->sgh_mpm_context);
520  SCLogDebug("%s", am->name);
522  int dir = (am->direction == SIG_FLAG_TOSERVER) ? 1 : 0;
524  SCLogDebug("%s: %d mpm_Ctx %p", am->name, r, mpm_ctx);
525  if (mpm_ctx != NULL) {
526  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
527  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
528  SCLogDebug("%s: %d", am->name, r);
529  }
530  }
531  }
532  am = am->next;
533  }
534  return r;
535 }
536 
537 /** \brief register a MPM engine
538  *
539  * \note to be used at start up / registration only. Errors are fatal.
540  */
541 void DetectPktMpmRegister(const char *name, int priority,
542  int (*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
543  const DetectBufferMpmRegistry *mpm_reg, int list_id),
545 {
546  SCLogDebug("registering %s/%d/%p/%p", name, priority,
547  PrefilterRegister, GetData);
548 
549  if (PrefilterRegister == PrefilterGenericMpmPktRegister && GetData == NULL) {
550  // must register GetData with PrefilterGenericMpmRegister
551  abort();
552  }
553 
556  int sm_list = DetectBufferTypeGetByName(name);
557  if (sm_list == -1) {
558  FatalError("MPM engine registration for %s failed", name);
559  }
560 
561  DetectBufferMpmRegistry *am = SCCalloc(1, sizeof(*am));
562  BUG_ON(am == NULL);
563  am->name = name;
564  snprintf(am->pname, sizeof(am->pname), "%s", am->name);
565  DEBUG_VALIDATE_BUG_ON(sm_list < 0 || sm_list > INT16_MAX);
566  am->sm_list = (uint16_t)sm_list;
567  am->priority = priority;
569 
570  am->PrefilterRegisterWithListId = PrefilterRegister;
571  am->pkt_v1.GetData = GetData;
572 
573  if (g_mpm_list[DETECT_BUFFER_MPM_TYPE_PKT] == NULL) {
574  g_mpm_list[DETECT_BUFFER_MPM_TYPE_PKT] = am;
575  } else {
577  while (t->next != NULL) {
578  t = t->next;
579  }
580  t->next = am;
581  am->id = t->id + 1;
582  }
583  g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_PKT]++;
584 
585  SupportFastPatternForSigMatchList(sm_list, priority);
586  SCLogDebug("%s/%d done", name, sm_list);
587 }
588 
589 /** \brief copy a mpm engine from parent_id, add in transforms */
591  const int id, const int parent_id,
592  DetectEngineTransforms *transforms)
593 {
594  SCLogDebug("registering %d/%d", id, parent_id);
595 
597  while (t) {
598  if (t->sm_list == parent_id) {
599  DetectBufferMpmRegistry *am = SCCalloc(1, sizeof(*am));
600  BUG_ON(am == NULL);
601  am->name = t->name;
602  snprintf(am->pname, sizeof(am->pname), "%s#%d", am->name, id);
603  DEBUG_VALIDATE_BUG_ON(id < 0 || id > INT16_MAX);
604  am->sm_list = (uint16_t)id; // use new id
605  am->sm_list_base = t->sm_list;
608  am->pkt_v1.GetData = t->pkt_v1.GetData;
609  am->priority = t->priority;
611  am->next = t->next;
612  if (transforms) {
613  memcpy(&am->transforms, transforms, sizeof(*transforms));
614  }
615  am->id = de_ctx->pkt_mpms_list_cnt++;
616 
618  t->next = am;
619  SCLogDebug("copied mpm registration for %s id %u "
620  "with parent %u and GetData %p",
621  t->name, id, parent_id, am->pkt_v1.GetData);
622  t = am;
623  }
624  t = t->next;
625  }
626 }
627 
629 {
630  const DetectBufferMpmRegistry *list = g_mpm_list[DETECT_BUFFER_MPM_TYPE_PKT];
631  while (list != NULL) {
632  DetectBufferMpmRegistry *n = SCCalloc(1, sizeof(*n));
633  BUG_ON(n == NULL);
634 
635  *n = *list;
636  n->next = NULL;
637 
638  if (de_ctx->pkt_mpms_list == NULL) {
639  de_ctx->pkt_mpms_list = n;
640  } else {
642  while (t->next != NULL) {
643  t = t->next;
644  }
645 
646  t->next = n;
647  }
648 
649  /* default to whatever the global setting is */
651 
652  /* see if we use a unique or shared mpm ctx for this type */
653  int confshared = 0;
654  char confstring[256] = "detect.mpm.";
655  strlcat(confstring, n->name, sizeof(confstring));
656  strlcat(confstring, ".shared", sizeof(confstring));
657  if (ConfGetBool(confstring, &confshared) == 1)
658  shared = confshared;
659 
660  if (shared == 0) {
661  SCLogDebug("using unique mpm ctx' for %s", n->name);
663  } else {
664  SCLogDebug("using shared mpm ctx' for %s", n->name);
665  n->sgh_mpm_context =
667  }
668 
669  list = list->next;
670  }
672  SCLogDebug("mpm: de_ctx pkt_mpms_list %p %u",
674 }
675 
676 /**
677  * \brief initialize mpm contexts for applayer buffers that are in
678  * "single or "shared" mode.
679  */
681 {
682  SCLogDebug("preparing pkt mpm");
683  int r = 0;
685  while (am != NULL) {
686  SCLogDebug("%s", am->name);
688  {
690  if (mpm_ctx != NULL) {
691  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
692  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
693  SCLogDebug("%s: %d", am->name, r);
694  }
695  }
696  }
697  am = am->next;
698  }
699  return r;
700 }
701 
702 static int32_t SetupBuiltinMpm(DetectEngineCtx *de_ctx, const char *name)
703 {
704  /* default to whatever the global setting is */
706 
707  /* see if we use a unique or shared mpm ctx for this type */
708  int confshared = 0;
709  char confstring[256] = "detect.mpm.";
710  strlcat(confstring, name, sizeof(confstring));
711  strlcat(confstring, ".shared", sizeof(confstring));
712  if (ConfGetBool(confstring, &confshared) == 1)
713  shared = confshared;
714 
715  int32_t ctx;
716  if (shared == 0) {
718  SCLogDebug("using unique mpm ctx' for %s", name);
719  } else {
721  SCLogDebug("using shared mpm ctx' for %s", name);
722  }
723  return ctx;
724 }
725 
727 {
728  de_ctx->sgh_mpm_context_proto_tcp_packet = SetupBuiltinMpm(de_ctx, "tcp-packet");
729  de_ctx->sgh_mpm_context_stream = SetupBuiltinMpm(de_ctx, "tcp-stream");
730 
731  de_ctx->sgh_mpm_context_proto_udp_packet = SetupBuiltinMpm(de_ctx, "udp-packet");
732  de_ctx->sgh_mpm_context_proto_other_packet = SetupBuiltinMpm(de_ctx, "other-ip");
733 }
734 
735 /**
736  * \brief initialize mpm contexts for builtin buffers that are in
737  * "single or "shared" mode.
738  */
740 {
741  int r = 0;
742  MpmCtx *mpm_ctx = NULL;
743 
746  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
747  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
748  }
750  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
751  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
752  }
753  }
754 
757  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
758  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
759  }
761  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
762  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
763  }
764  }
765 
768  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
769  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
770  }
771  }
772 
775  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
776  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
777  }
779  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
780  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
781  }
782  }
783 
784  return r;
785 }
786 
787 /**
788  * \brief check if a signature has patterns that are to be inspected
789  * against a packets payload (as opposed to the stream payload)
790  *
791  * \param s signature
792  *
793  * \retval 1 true
794  * \retval 0 false
795  */
797 {
798  SCEnter();
799 
800  if (!(s->proto.proto[IPPROTO_TCP / 8] & 1 << (IPPROTO_TCP % 8))) {
801  SCReturnInt(1);
802  }
803 
804  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] == NULL) {
805  SCLogDebug("no PMATCH");
806  SCReturnInt(0);
807  }
808 
809  if (!(s->flags & SIG_FLAG_REQUIRE_PACKET)) {
810  SCReturnInt(0);
811  }
812 
813  SCReturnInt(1);
814 }
815 
816 /**
817  * \brief check if a signature has patterns that are to be inspected
818  * against the stream payload (as opposed to the individual packets
819  * payload(s))
820  *
821  * \param s signature
822  *
823  * \retval 1 true
824  * \retval 0 false
825  */
827 {
828  SCEnter();
829 
830  if (!(s->proto.proto[IPPROTO_TCP / 8] & 1 << (IPPROTO_TCP % 8))) {
831  SCReturnInt(0);
832  }
833 
834  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] == NULL) {
835  SCLogDebug("no PMATCH");
836  SCReturnInt(0);
837  }
838 
839  if (!(s->flags & SIG_FLAG_REQUIRE_STREAM)) {
840  SCReturnInt(0);
841  }
842 
843  SCReturnInt(1);
844 }
845 
846 
847 /**
848  * \brief Function to return the multi pattern matcher algorithm to be
849  * used by the engine, based on the mpm-algo setting in yaml
850  * Use the default mpm if none is specified in the yaml file.
851  *
852  * \retval mpm algo value
853  */
855 {
856  const char *mpm_algo;
857  uint8_t mpm_algo_val = mpm_default_matcher;
858 
859  /* Get the mpm algo defined in config file by the user */
860  if ((ConfGet("mpm-algo", &mpm_algo)) == 1) {
861  if (mpm_algo != NULL) {
862 #if __BYTE_ORDER == __BIG_ENDIAN
863  if (strcmp(mpm_algo, "ac-ks") == 0) {
864  FatalError("ac-ks does "
865  "not work on big endian systems at this time.");
866  }
867 #endif
868  if (strcmp("auto", mpm_algo) == 0) {
869  goto done;
870  } else if (strcmp("ac-bs", mpm_algo) == 0) {
871  SCLogWarning("mpm-algo \"ac-bs\" has been removed. See ticket #6586.");
872  goto done;
873  }
874  for (uint8_t u = 0; u < MPM_TABLE_SIZE; u++) {
875  if (mpm_table[u].name == NULL)
876  continue;
877 
878  if (strcmp(mpm_table[u].name, mpm_algo) == 0) {
879  mpm_algo_val = u;
880  goto done;
881  }
882  }
883 
884 #ifndef BUILD_HYPERSCAN
885  if ((strcmp(mpm_algo, "hs") == 0)) {
886  FatalError("Hyperscan (hs) support for mpm-algo is "
887  "not compiled into Suricata.");
888  }
889 #endif
890  }
891  FatalError("Invalid mpm algo supplied "
892  "in the yaml conf file: \"%s\"",
893  mpm_algo);
894  }
895 
896  done:
897  return mpm_algo_val;
898 }
899 
900 void PatternMatchDestroy(MpmCtx *mpm_ctx, uint16_t mpm_matcher)
901 {
902  SCLogDebug("mpm_ctx %p, mpm_matcher %"PRIu16"", mpm_ctx, mpm_matcher);
903  mpm_table[mpm_matcher].DestroyCtx(mpm_ctx);
904 }
905 
906 void PatternMatchThreadDestroy(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
907 {
908  SCLogDebug("mpm_thread_ctx %p, mpm_matcher %"PRIu16"", mpm_thread_ctx, mpm_matcher);
909  MpmDestroyThreadCtx(mpm_thread_ctx, mpm_matcher);
910 }
911 void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
912 {
913  SCLogDebug("mpm_thread_ctx %p, type %"PRIu16, mpm_thread_ctx, mpm_matcher);
914  MpmInitThreadCtx(mpm_thread_ctx, mpm_matcher);
915 }
916 
917 /** \brief Predict a strength value for patterns
918  *
919  * Patterns with high character diversity score higher.
920  * Alpha chars score not so high
921  * Other printable + a few common codes a little higher
922  * Everything else highest.
923  * Longer patterns score better than short patters.
924  *
925  * \param pat pattern
926  * \param patlen length of the pattern
927  *
928  * \retval s pattern score
929  */
930 uint32_t PatternStrength(uint8_t *pat, uint16_t patlen)
931 {
932  uint8_t a[256];
933  memset(&a, 0 ,sizeof(a));
934 
935  uint32_t s = 0;
936  uint16_t u = 0;
937  for (u = 0; u < patlen; u++) {
938  if (a[pat[u]] == 0) {
939  if (isalpha(pat[u]))
940  s += 3;
941  else if (isprint(pat[u]) || pat[u] == 0x00 || pat[u] == 0x01 || pat[u] == 0xFF)
942  s += 4;
943  else
944  s += 6;
945 
946  a[pat[u]] = 1;
947  } else {
948  s++;
949  }
950  }
951 
952  return s;
953 }
954 
955 static void PopulateMpmHelperAddPattern(MpmCtx *mpm_ctx, const DetectContentData *cd,
956  const Signature *s, const uint8_t flags, const int chop)
957 {
958  uint16_t pat_offset = cd->offset;
959  uint16_t pat_depth = cd->depth;
960 
961  /* recompute offset/depth to cope with chop */
962  if (chop && (pat_depth || pat_offset)) {
963  pat_offset += cd->fp_chop_offset;
964  if (pat_depth) {
965  pat_depth -= cd->content_len;
966  pat_depth += cd->fp_chop_offset + cd->fp_chop_len;
967  }
968  }
969 
970  /* We have to effectively "wild card" values that will be coming from
971  * byte_extract variables
972  */
974  pat_depth = pat_offset = 0;
975  }
976 
977  if (cd->flags & DETECT_CONTENT_NOCASE) {
978  if (chop) {
979  MpmAddPatternCI(mpm_ctx,
980  cd->content + cd->fp_chop_offset, cd->fp_chop_len,
981  pat_offset, pat_depth,
983  } else {
984  MpmAddPatternCI(mpm_ctx,
985  cd->content, cd->content_len,
986  pat_offset, pat_depth,
988  }
989  } else {
990  if (chop) {
991  MpmAddPatternCS(mpm_ctx,
992  cd->content + cd->fp_chop_offset, cd->fp_chop_len,
993  pat_offset, pat_depth,
995  } else {
996  MpmAddPatternCS(mpm_ctx,
997  cd->content, cd->content_len,
998  pat_offset, pat_depth,
1000  }
1001  }
1002 }
1003 
1004 #define SGH_PROTO(sgh, p) ((sgh)->init->protos[(p)] == 1)
1005 #define SGH_DIRECTION_TS(sgh) ((sgh)->init->direction & SIG_FLAG_TOSERVER)
1006 #define SGH_DIRECTION_TC(sgh) ((sgh)->init->direction & SIG_FLAG_TOCLIENT)
1008 static void SetMpm(Signature *s, SigMatch *mpm_sm, const int mpm_sm_list)
1009 {
1010  if (s == NULL || mpm_sm == NULL)
1011  return;
1012 
1013  DetectContentData *cd = (DetectContentData *)mpm_sm->ctx;
1015  if (DETECT_CONTENT_IS_SINGLE(cd) &&
1016  !(cd->flags & DETECT_CONTENT_NEGATED) &&
1017  !(cd->flags & DETECT_CONTENT_REPLACE) &&
1018  cd->content_len == cd->fp_chop_len)
1019  {
1021  }
1022  } else {
1023  if (DETECT_CONTENT_IS_SINGLE(cd) &&
1024  !(cd->flags & DETECT_CONTENT_NEGATED) &&
1025  !(cd->flags & DETECT_CONTENT_REPLACE))
1026  {
1028  }
1029  }
1030  cd->flags |= DETECT_CONTENT_MPM;
1031  s->init_data->mpm_sm_list = mpm_sm_list;
1032  s->init_data->mpm_sm = mpm_sm;
1033 }
1034 
1035 static SigMatch *GetMpmForList(const Signature *s, SigMatch *list, SigMatch *mpm_sm,
1036  uint16_t max_len, bool skip_negated_content)
1037 {
1038  for (SigMatch *sm = list; sm != NULL; sm = sm->next) {
1039  if (sm->type != DETECT_CONTENT)
1040  continue;
1041 
1042  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1043  /* skip_negated_content is only set if there's absolutely no
1044  * non-negated content present in the sig */
1045  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
1046  continue;
1047  if (cd->content_len != max_len) {
1048  SCLogDebug("content_len %u != max_len %u", cd->content_len, max_len);
1049  continue;
1050  }
1051  if (mpm_sm == NULL) {
1052  mpm_sm = sm;
1053  } else {
1054  DetectContentData *data1 = (DetectContentData *)sm->ctx;
1055  DetectContentData *data2 = (DetectContentData *)mpm_sm->ctx;
1056  uint32_t ls = PatternStrength(data1->content, data1->content_len);
1057  uint32_t ss = PatternStrength(data2->content, data2->content_len);
1058  if (ls > ss) {
1059  mpm_sm = sm;
1060  } else if (ls == ss) {
1061  /* if 2 patterns are of equal strength, we pick the longest */
1062  if (data1->content_len > data2->content_len)
1063  mpm_sm = sm;
1064  } else {
1065  SCLogDebug("sticking with mpm_sm");
1066  }
1067  }
1068  }
1069  return mpm_sm;
1070 }
1071 
1073 
1075 {
1076  if (g_skip_prefilter)
1077  return;
1078 
1079  if (s->init_data->mpm_sm != NULL)
1080  return;
1081 
1082  const int nlists = s->init_data->max_content_list_id + 1;
1083  int pos_sm_list[nlists];
1084  int neg_sm_list[nlists];
1085  memset(pos_sm_list, 0, nlists * sizeof(int));
1086  memset(neg_sm_list, 0, nlists * sizeof(int));
1087  int pos_sm_list_cnt = 0;
1088  int neg_sm_list_cnt = 0;
1089 
1090  /* inspect rule to see if we have the fast_pattern reg to
1091  * force using a sig, otherwise keep stats about the patterns */
1092  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL) {
1094  for (SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH]; sm != NULL;
1095  sm = sm->next) {
1096  if (sm->type != DETECT_CONTENT)
1097  continue;
1098 
1099  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1100  /* fast_pattern set in rule, so using this pattern */
1101  if ((cd->flags & DETECT_CONTENT_FAST_PATTERN)) {
1102  SetMpm(s, sm, DETECT_SM_LIST_PMATCH);
1103  return;
1104  }
1105 
1106  if (cd->flags & DETECT_CONTENT_NEGATED) {
1107  neg_sm_list[DETECT_SM_LIST_PMATCH] = 1;
1108  neg_sm_list_cnt++;
1109  } else {
1110  pos_sm_list[DETECT_SM_LIST_PMATCH] = 1;
1111  pos_sm_list_cnt++;
1112  }
1113  }
1114  }
1115  }
1116  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1117  const int list_id = s->init_data->buffers[x].id;
1118 
1119  SCLogDebug("%u: list_id %d: %s", s->id, list_id,
1121 
1123  SCLogDebug("skip");
1124  continue;
1125  }
1126 
1127  for (SigMatch *sm = s->init_data->buffers[x].head; sm != NULL; sm = sm->next) {
1128  if (sm->type != DETECT_CONTENT)
1129  continue;
1130 
1131  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1132  /* fast_pattern set in rule, so using this pattern */
1133  if ((cd->flags & DETECT_CONTENT_FAST_PATTERN)) {
1134  SetMpm(s, sm, list_id);
1135  return;
1136  }
1137 
1138  if (cd->flags & DETECT_CONTENT_NEGATED) {
1139  neg_sm_list[list_id] = 1;
1140  neg_sm_list_cnt++;
1141  } else {
1142  pos_sm_list[list_id] = 1;
1143  pos_sm_list_cnt++;
1144  SCLogDebug("pos added for %d", list_id);
1145  }
1146  }
1147  SCLogDebug("ok");
1148  }
1149 
1150  SCLogDebug("neg_sm_list_cnt %d pos_sm_list_cnt %d", neg_sm_list_cnt, pos_sm_list_cnt);
1151 
1152  /* prefer normal not-negated over negated */
1153  int *curr_sm_list = NULL;
1154  int skip_negated_content = 1;
1155  if (pos_sm_list_cnt > 0) {
1156  curr_sm_list = pos_sm_list;
1157  } else if (neg_sm_list_cnt > 0) {
1158  curr_sm_list = neg_sm_list;
1159  skip_negated_content = 0;
1160  } else {
1161  return;
1162  }
1163 
1164  int final_sm_list[nlists];
1165  memset(&final_sm_list, 0, (nlists * sizeof(int)));
1166 
1167  int count_final_sm_list = 0;
1168  int priority;
1169 
1171  while (tmp != NULL) {
1172  for (priority = tmp->priority;
1173  tmp != NULL && priority == tmp->priority;
1174  tmp = tmp->next)
1175  {
1176  SCLogDebug("tmp->list_id %d tmp->priority %d", tmp->list_id, tmp->priority);
1177  if (tmp->list_id >= nlists)
1178  continue;
1179  if (curr_sm_list[tmp->list_id] == 0)
1180  continue;
1181  final_sm_list[count_final_sm_list++] = tmp->list_id;
1182  SCLogDebug("tmp->list_id %d", tmp->list_id);
1183  }
1184  if (count_final_sm_list != 0)
1185  break;
1186  }
1187 
1188  BUG_ON(count_final_sm_list == 0);
1189  SCLogDebug("count_final_sm_list %d skip_negated_content %d", count_final_sm_list,
1190  skip_negated_content);
1191 
1192  uint16_t max_len = 0;
1193  for (int i = 0; i < count_final_sm_list; i++) {
1194  SCLogDebug("i %d final_sm_list[i] %d", i, final_sm_list[i]);
1195 
1196  if (final_sm_list[i] == DETECT_SM_LIST_PMATCH) {
1197  for (SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH]; sm != NULL;
1198  sm = sm->next) {
1199  if (sm->type != DETECT_CONTENT)
1200  continue;
1201 
1202  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1203  /* skip_negated_content is only set if there's absolutely no
1204  * non-negated content present in the sig */
1205  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
1206  continue;
1207  max_len = MAX(max_len, cd->content_len);
1208  }
1209  } else {
1210  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1211  const int list_id = s->init_data->buffers[x].id;
1212  if (final_sm_list[i] == list_id) {
1213  SCLogDebug("%u: list_id %d: %s", s->id, list_id,
1215 
1216  for (SigMatch *sm = s->init_data->buffers[x].head; sm != NULL; sm = sm->next) {
1217  if (sm->type != DETECT_CONTENT)
1218  continue;
1219 
1220  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1221  /* skip_negated_content is only set if there's absolutely no
1222  * non-negated content present in the sig */
1223  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
1224  continue;
1225  max_len = MAX(max_len, cd->content_len);
1226  }
1227  }
1228  }
1229  }
1230  }
1231 
1232  SigMatch *mpm_sm = NULL;
1233  int mpm_sm_list = -1;
1234  for (int i = 0; i < count_final_sm_list; i++) {
1235  SCLogDebug("i %d", i);
1236  if (final_sm_list[i] == DETECT_SM_LIST_PMATCH) {
1237  /* GetMpmForList may keep `mpm_sm` the same, so track if it changed */
1238  SigMatch *prev_mpm_sm = mpm_sm;
1239  mpm_sm = GetMpmForList(s, s->init_data->smlists[DETECT_SM_LIST_PMATCH], mpm_sm, max_len,
1240  skip_negated_content);
1241  if (mpm_sm != prev_mpm_sm) {
1242  mpm_sm_list = final_sm_list[i];
1243  }
1244  } else {
1245  SCLogDebug(
1246  "%u: %s", s->id, DetectEngineBufferTypeGetNameById(de_ctx, final_sm_list[i]));
1247  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1248  const int list_id = s->init_data->buffers[x].id;
1249  if (final_sm_list[i] == list_id) {
1250  SCLogDebug("%u: list_id %d: %s", s->id, list_id,
1252  /* GetMpmForList may keep `mpm_sm` the same, so track if it changed */
1253  SigMatch *prev_mpm_sm = mpm_sm;
1254  mpm_sm = GetMpmForList(s, s->init_data->buffers[x].head, mpm_sm, max_len,
1255  skip_negated_content);
1256  SCLogDebug("mpm_sm %p from %p", mpm_sm, s->init_data->buffers[x].head);
1257  if (mpm_sm != prev_mpm_sm) {
1258  mpm_sm_list = list_id;
1259  }
1260  }
1261  }
1262  }
1263  }
1264 
1265 #ifdef DEBUG
1266  if (mpm_sm != NULL) {
1267  BUG_ON(mpm_sm_list == -1);
1268  int check_list = SigMatchListSMBelongsTo(s, mpm_sm);
1269  BUG_ON(check_list != mpm_sm_list);
1270  }
1271 #endif
1272  /* assign to signature */
1273  SetMpm(s, mpm_sm, mpm_sm_list);
1274 }
1275 
1276 /** \internal
1277  * \brief The hash function for MpmStore
1278  *
1279  * \param ht Pointer to the hash table.
1280  * \param data Pointer to the MpmStore.
1281  * \param datalen Not used in our case.
1282  *
1283  * \retval hash The generated hash value.
1284  */
1285 static uint32_t MpmStoreHashFunc(HashListTable *ht, void *data, uint16_t datalen)
1286 {
1287  const MpmStore *ms = (MpmStore *)data;
1288  uint32_t hash = ms->alproto;
1289 
1290  for (uint32_t b = 0; b < ms->sid_array_size; b++)
1291  hash += ms->sid_array[b];
1292 
1293  return hash % ht->array_size;
1294 }
1295 
1296 /**
1297  * \brief The Compare function for MpmStore
1298  *
1299  * \param data1 Pointer to the first MpmStore.
1300  * \param len1 Not used.
1301  * \param data2 Pointer to the second MpmStore.
1302  * \param len2 Not used.
1303  *
1304  * \retval 1 If the 2 MpmStores sent as args match.
1305  * \retval 0 If the 2 MpmStores sent as args do not match.
1306  */
1307 static char MpmStoreCompareFunc(void *data1, uint16_t len1, void *data2,
1308  uint16_t len2)
1309 {
1310  const MpmStore *ms1 = (MpmStore *)data1;
1311  const MpmStore *ms2 = (MpmStore *)data2;
1312 
1313  if (ms1->alproto != ms2->alproto)
1314  return 0;
1315 
1316  if (ms1->sid_array_size != ms2->sid_array_size)
1317  return 0;
1318 
1319  if (ms1->buffer != ms2->buffer)
1320  return 0;
1321 
1322  if (ms1->direction != ms2->direction)
1323  return 0;
1324 
1325  if (ms1->sm_list != ms2->sm_list)
1326  return 0;
1327 
1328  if (SCMemcmp(ms1->sid_array, ms2->sid_array,
1329  ms1->sid_array_size) != 0)
1330  {
1331  return 0;
1332  }
1333 
1334  return 1;
1335 }
1336 
1337 static void MpmStoreFreeFunc(void *ptr)
1338 {
1339  MpmStore *ms = ptr;
1340  if (ms != NULL) {
1341  if (ms->mpm_ctx != NULL && !(ms->mpm_ctx->flags & MPMCTX_FLAGS_GLOBAL))
1342  {
1343  SCLogDebug("destroying mpm_ctx %p", ms->mpm_ctx);
1345  SCFree(ms->mpm_ctx);
1346  }
1347  ms->mpm_ctx = NULL;
1348 
1349  SCFree(ms->sid_array);
1350  SCFree(ms);
1351  }
1352 }
1353 
1354 /**
1355  * \brief Initializes the MpmStore mpm hash table to be used by the detection
1356  * engine context.
1357  *
1358  * \param de_ctx Pointer to the detection engine context.
1359  *
1360  * \retval 0 On success.
1361  * \retval -1 On failure.
1362  */
1364 {
1366  MpmStoreHashFunc,
1367  MpmStoreCompareFunc,
1368  MpmStoreFreeFunc);
1369  if (de_ctx->mpm_hash_table == NULL)
1370  goto error;
1371 
1372  return 0;
1373 
1374 error:
1375  return -1;
1376 }
1377 
1378 /**
1379  * \brief Adds a MpmStore to the detection engine context MpmStore
1380  *
1381  * \param de_ctx Pointer to the detection engine context.
1382  * \param sgh Pointer to the MpmStore.
1383  *
1384  * \retval ret 0 on Successfully adding the argument sgh; -1 on failure.
1385  */
1386 static int MpmStoreAdd(DetectEngineCtx *de_ctx, MpmStore *s)
1387 {
1388  int ret = HashListTableAdd(de_ctx->mpm_hash_table, (void *)s, 0);
1389  return ret;
1390 }
1391 
1392 /**
1393  * \brief Used to lookup a MpmStore from the MpmStore
1394  *
1395  * \param de_ctx Pointer to the detection engine context.
1396  * \param sgh Pointer to the MpmStore.
1397  *
1398  * \retval rsgh On success a pointer to the MpmStore if the MpmStore is
1399  * found in the hash table; NULL on failure.
1400  */
1401 static MpmStore *MpmStoreLookup(DetectEngineCtx *de_ctx, MpmStore *s)
1402 {
1404  (void *)s, 0);
1405  return rs;
1406 }
1407 
1408 static const DetectBufferMpmRegistry *GetByMpmStore(
1409  const DetectEngineCtx *de_ctx, const MpmStore *ms)
1410 {
1412  while (am != NULL) {
1413  if (ms->sm_list == am->sm_list &&
1414  ms->direction == am->direction) {
1415  return am;
1416  }
1417  am = am->next;
1418  }
1419  am = de_ctx->pkt_mpms_list;
1420  while (am != NULL) {
1421  if (ms->sm_list == am->sm_list) {
1422  return am;
1423  }
1424  am = am->next;
1425  }
1426  return NULL;
1427 }
1428 
1430 {
1431  HashListTableBucket *htb = NULL;
1432 
1433  uint32_t stats[MPMB_MAX] = {0};
1434  int app_mpms_cnt = de_ctx->buffer_type_id;
1435  uint32_t appstats[app_mpms_cnt + 1]; // +1 to silence scan-build
1436  memset(&appstats, 0x00, sizeof(appstats));
1437  int pkt_mpms_cnt = de_ctx->buffer_type_id;
1438  uint32_t pktstats[pkt_mpms_cnt + 1]; // +1 to silence scan-build
1439  memset(&pktstats, 0x00, sizeof(pktstats));
1440  int frame_mpms_cnt = de_ctx->buffer_type_id;
1441  uint32_t framestats[frame_mpms_cnt + 1]; // +1 to silence scan-build
1442  memset(&framestats, 0x00, sizeof(framestats));
1443 
1445  htb != NULL;
1446  htb = HashListTableGetListNext(htb))
1447  {
1448  const MpmStore *ms = (MpmStore *)HashListTableGetListData(htb);
1449  if (ms == NULL || ms->mpm_ctx == NULL) {
1450  continue;
1451  }
1452  if (ms->buffer < MPMB_MAX)
1453  stats[ms->buffer]++;
1454  else if (ms->sm_list != DETECT_SM_LIST_PMATCH) {
1455  const DetectBufferMpmRegistry *am = GetByMpmStore(de_ctx, ms);
1456  if (am != NULL) {
1457  switch (am->type) {
1459  SCLogDebug("%s: %u patterns. Min %u, Max %u. Ctx %p",
1460  am->name,
1461  ms->mpm_ctx->pattern_cnt,
1462  ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1463  ms->mpm_ctx);
1464  pktstats[am->sm_list]++;
1465  break;
1467  SCLogDebug("%s %s %s: %u patterns. Min %u, Max %u. Ctx %p",
1468  AppProtoToString(ms->alproto), am->name,
1469  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1470  ms->mpm_ctx->pattern_cnt, ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1471  ms->mpm_ctx);
1472  appstats[am->sm_list]++;
1473  break;
1475  SCLogDebug("%s: %u patterns. Min %u, Max %u. Ctx %p", am->name,
1476  ms->mpm_ctx->pattern_cnt, ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1477  ms->mpm_ctx);
1478  framestats[am->sm_list]++;
1479  break;
1481  break;
1482  }
1483  }
1484  }
1485  }
1486 
1487  if (!(de_ctx->flags & DE_QUIET)) {
1488  for (int x = 0; x < MPMB_MAX; x++) {
1489  SCLogPerf("Builtin MPM \"%s\": %u", builtin_mpms[x], stats[x]);
1490  }
1492  while (am != NULL) {
1493  if (appstats[am->sm_list] > 0) {
1494  const char *name = am->name;
1495  const char *direction = am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient";
1496  SCLogPerf("AppLayer MPM \"%s %s (%s)\": %u", direction, name,
1497  AppProtoToString(am->app_v2.alproto), appstats[am->sm_list]);
1498  }
1499  am = am->next;
1500  }
1502  while (pm != NULL) {
1503  if (pktstats[pm->sm_list] > 0) {
1504  const char *name = pm->name;
1505  SCLogPerf("Pkt MPM \"%s\": %u", name, pktstats[pm->sm_list]);
1506  }
1507  pm = pm->next;
1508  }
1510  while (um != NULL) {
1511  if (framestats[um->sm_list] > 0) {
1512  const char *name = um->name;
1513  SCLogPerf("Frame MPM \"%s\": %u", name, framestats[um->sm_list]);
1514  }
1515  um = um->next;
1516  }
1517  }
1518 }
1519 
1520 /**
1521  * \brief Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by
1522  * MpmStoreInit() function.
1523  *
1524  * \param de_ctx Pointer to the detection engine context.
1525  */
1527 {
1528  if (de_ctx->mpm_hash_table == NULL)
1529  return;
1530 
1532  de_ctx->mpm_hash_table = NULL;
1533 }
1534 
1535 static void MpmStoreSetup(const DetectEngineCtx *de_ctx, MpmStore *ms)
1536 {
1537  const Signature *s = NULL;
1538  uint32_t sig;
1539  int dir = 0;
1540 
1541  if (ms->buffer != MPMB_MAX) {
1543 
1544  switch (ms->buffer) {
1545  /* TS is 1 */
1546  case MPMB_TCP_PKT_TS:
1547  case MPMB_TCP_STREAM_TS:
1548  case MPMB_UDP_TS:
1549  dir = 1;
1550  break;
1551 
1552  /* TC is 0 */
1553  default:
1554  case MPMB_UDP_TC:
1555  case MPMB_TCP_STREAM_TC:
1556  case MPMB_TCP_PKT_TC:
1557  case MPMB_OTHERIP: /**< use 0 for other */
1558  dir = 0;
1559  break;
1560  }
1561  } else {
1563 
1564  if (ms->direction == SIG_FLAG_TOSERVER)
1565  dir = 1;
1566  else
1567  dir = 0;
1568  }
1569 
1571  if (ms->mpm_ctx == NULL) {
1572  return;
1573  }
1574 
1576 
1577  const bool mpm_supports_endswith =
1579 
1580  /* add the patterns */
1581  for (sig = 0; sig < (ms->sid_array_size * 8); sig++) {
1582  if (ms->sid_array[sig / 8] & (1 << (sig % 8))) {
1583  s = de_ctx->sig_array[sig];
1584  DEBUG_VALIDATE_BUG_ON(s == NULL);
1585  if (s == NULL)
1586  continue;
1587 
1588  SCLogDebug("%p: direction %d adding %u", ms, ms->direction, s->id);
1589 
1591 
1592  int skip = 0;
1593  /* negated logic: if mpm match can't be used to be sure about this
1594  * pattern, we have to inspect the rule fully regardless of mpm
1595  * match. So in this case there is no point of adding it at all.
1596  * The non-mpm list entry for the sig will make sure the sig is
1597  * inspected. */
1598  if ((cd->flags & DETECT_CONTENT_NEGATED) &&
1600  {
1601  skip = 1;
1602  SCLogDebug("not adding negated mpm as it's not 'single'");
1603  }
1604 
1605  if (!skip) {
1606  uint8_t flags = 0;
1607  if ((cd->flags & DETECT_CONTENT_ENDS_WITH) && mpm_supports_endswith)
1609  PopulateMpmHelperAddPattern(
1610  ms->mpm_ctx, cd, s, flags, (cd->flags & DETECT_CONTENT_FAST_PATTERN_CHOP));
1611  }
1612  }
1613  }
1614 
1615  if (ms->mpm_ctx->pattern_cnt == 0) {
1617  ms->mpm_ctx = NULL;
1618  } else {
1620  if (mpm_table[ms->mpm_ctx->mpm_type].Prepare != NULL) {
1622  }
1623  }
1624  }
1625 }
1626 
1627 
1628 /** \brief Get MpmStore for a built-in buffer type
1629  *
1630  */
1632  enum MpmBuiltinBuffers buf)
1633 {
1634  const Signature *s = NULL;
1635  uint32_t sig;
1636  uint32_t cnt = 0;
1637  int direction = 0;
1638  uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
1639  uint8_t sids_array[max_sid];
1640  memset(sids_array, 0x00, max_sid);
1641  int sgh_mpm_context = 0;
1642  int sm_list = DETECT_SM_LIST_PMATCH;
1643 
1644  switch (buf) {
1645  case MPMB_TCP_PKT_TS:
1646  case MPMB_TCP_PKT_TC:
1647  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_tcp_packet;
1648  break;
1649  case MPMB_TCP_STREAM_TS:
1650  case MPMB_TCP_STREAM_TC:
1651  sgh_mpm_context = de_ctx->sgh_mpm_context_stream;
1652  break;
1653  case MPMB_UDP_TS:
1654  case MPMB_UDP_TC:
1655  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_udp_packet;
1656  break;
1657  case MPMB_OTHERIP:
1658  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_other_packet;
1659  break;
1660  default:
1661  break;
1662  }
1663 
1664  switch(buf) {
1665  case MPMB_TCP_PKT_TS:
1666  case MPMB_TCP_STREAM_TS:
1667  case MPMB_UDP_TS:
1668  direction = SIG_FLAG_TOSERVER;
1669  break;
1670 
1671  case MPMB_TCP_PKT_TC:
1672  case MPMB_TCP_STREAM_TC:
1673  case MPMB_UDP_TC:
1674  direction = SIG_FLAG_TOCLIENT;
1675  break;
1676 
1677  case MPMB_OTHERIP:
1678  direction = (SIG_FLAG_TOCLIENT|SIG_FLAG_TOSERVER);
1679  break;
1680 
1681  case MPMB_MAX:
1682  BUG_ON(1);
1683  break;
1684  }
1685 
1686  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
1687  s = sgh->init->match_array[sig];
1688  if (s == NULL)
1689  continue;
1690 
1691  if (s->init_data->mpm_sm == NULL)
1692  continue;
1693 
1694  int list = s->init_data->mpm_sm_list;
1695  if (list < 0)
1696  continue;
1697 
1698  if (list != DETECT_SM_LIST_PMATCH)
1699  continue;
1700 
1701  switch (buf) {
1702  case MPMB_TCP_PKT_TS:
1703  case MPMB_TCP_PKT_TC:
1704  if (SignatureHasPacketContent(s) == 1)
1705  {
1706  sids_array[s->num / 8] |= 1 << (s->num % 8);
1707  cnt++;
1708  }
1709  break;
1710  case MPMB_TCP_STREAM_TS:
1711  case MPMB_TCP_STREAM_TC:
1712  if (SignatureHasStreamContent(s) == 1)
1713  {
1714  sids_array[s->num / 8] |= 1 << (s->num % 8);
1715  cnt++;
1716  }
1717  break;
1718  case MPMB_UDP_TS:
1719  case MPMB_UDP_TC:
1720  sids_array[s->num / 8] |= 1 << (s->num % 8);
1721  cnt++;
1722  break;
1723  case MPMB_OTHERIP:
1724  sids_array[s->num / 8] |= 1 << (s->num % 8);
1725  cnt++;
1726  break;
1727  default:
1728  break;
1729  }
1730  }
1731 
1732  if (cnt == 0)
1733  return NULL;
1734 
1735  MpmStore lookup = { sids_array, max_sid, direction, buf, sm_list, 0, 0, NULL };
1736 
1737  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1738  if (result == NULL) {
1739  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1740  if (copy == NULL)
1741  return NULL;
1742  uint8_t *sids = SCCalloc(1, max_sid);
1743  if (sids == NULL) {
1744  SCFree(copy);
1745  return NULL;
1746  }
1747 
1748  memcpy(sids, sids_array, max_sid);
1749  copy->sid_array = sids;
1750  copy->sid_array_size = max_sid;
1751  copy->buffer = buf;
1752  copy->direction = direction;
1753  copy->sm_list = sm_list;
1754  copy->sgh_mpm_context = sgh_mpm_context;
1755 
1756  MpmStoreSetup(de_ctx, copy);
1757  MpmStoreAdd(de_ctx, copy);
1758  return copy;
1759  } else {
1760  return result;
1761  }
1762 }
1763 
1764 struct SidsArray {
1765  uint8_t *sids_array;
1767  /* indicates this has an active engine */
1768  bool active;
1769 
1771 };
1772 
1773 static MpmStore *MpmStorePrepareBufferAppLayer(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1774  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1775 {
1776  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1777  return NULL;
1778 
1779  SCLogDebug("handling %s direction %s for list %d", am->name,
1780  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1781  am->sm_list);
1782 
1783  MpmStore lookup = { sa->sids_array, sa->sids_array_size, am->direction, MPMB_MAX, am->sm_list,
1784  0, am->app_v2.alproto, NULL };
1785  SCLogDebug("am->direction %d am->sm_list %d sgh_mpm_context %d", am->direction, am->sm_list,
1786  am->sgh_mpm_context);
1787 
1788  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1789  if (result == NULL) {
1790  SCLogDebug("new unique mpm for %s %s", am->name,
1791  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient");
1792 
1793  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1794  if (copy == NULL)
1795  return NULL;
1796  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1797  if (sids == NULL) {
1798  SCFree(copy);
1799  return NULL;
1800  }
1801 
1802  memcpy(sids, sa->sids_array, sa->sids_array_size);
1803  copy->sid_array = sids;
1804  copy->sid_array_size = sa->sids_array_size;
1805  copy->buffer = MPMB_MAX;
1806  copy->direction = am->direction;
1807  copy->sm_list = am->sm_list;
1808  copy->sgh_mpm_context = am->sgh_mpm_context;
1809  copy->alproto = am->app_v2.alproto;
1810 
1811  MpmStoreSetup(de_ctx, copy);
1812  MpmStoreAdd(de_ctx, copy);
1813  return copy;
1814  } else {
1815  SCLogDebug("using existing mpm %p", result);
1816  return result;
1817  }
1818  return NULL;
1819 }
1820 
1821 static MpmStore *MpmStorePrepareBufferPkt(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1822  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1823 {
1824  SCLogDebug("handling %s for list %d", am->name,
1825  am->sm_list);
1826 
1827  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1828  return NULL;
1829 
1831  MPMB_MAX, am->sm_list, 0, 0, NULL };
1832  SCLogDebug("am->sm_list %d", am->sm_list);
1833 
1834  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1835  if (result == NULL) {
1836  SCLogDebug("new unique mpm for %s", am->name);
1837 
1838  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1839  if (copy == NULL)
1840  return NULL;
1841  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1842  if (sids == NULL) {
1843  SCFree(copy);
1844  return NULL;
1845  }
1846 
1847  memcpy(sids, sa->sids_array, sa->sids_array_size);
1848  copy->sid_array = sids;
1849  copy->sid_array_size = sa->sids_array_size;
1850  copy->buffer = MPMB_MAX;
1852  copy->sm_list = am->sm_list;
1853  copy->sgh_mpm_context = am->sgh_mpm_context;
1854 
1855  MpmStoreSetup(de_ctx, copy);
1856  MpmStoreAdd(de_ctx, copy);
1857  return copy;
1858  } else {
1859  SCLogDebug("using existing mpm %p", result);
1860  return result;
1861  }
1862  return NULL;
1863 }
1864 
1865 static MpmStore *MpmStorePrepareBufferFrame(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1866  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1867 {
1868  SCLogDebug("handling %s for list %d", am->name, am->sm_list);
1869 
1870  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1871  return NULL;
1872 
1873  MpmStore lookup = { sa->sids_array, sa->sids_array_size, am->direction, MPMB_MAX, am->sm_list,
1874  0, am->frame_v1.alproto, NULL };
1875  SCLogDebug("am->sm_list %d", am->sm_list);
1876 
1877  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1878  if (result == NULL) {
1879  SCLogDebug("new unique mpm for %s", am->name);
1880 
1881  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1882  if (copy == NULL)
1883  return NULL;
1884  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1885  if (sids == NULL) {
1886  SCFree(copy);
1887  return NULL;
1888  }
1889 
1890  memcpy(sids, sa->sids_array, sa->sids_array_size);
1891  copy->sid_array = sids;
1892  copy->sid_array_size = sa->sids_array_size;
1893  copy->buffer = MPMB_MAX;
1894  copy->direction = am->direction;
1895  copy->sm_list = am->sm_list;
1896  copy->sgh_mpm_context = am->sgh_mpm_context;
1897  copy->alproto = am->frame_v1.alproto;
1898 
1899  MpmStoreSetup(de_ctx, copy);
1900  MpmStoreAdd(de_ctx, copy);
1901  return copy;
1902  } else {
1903  SCLogDebug("using existing mpm %p", result);
1904  return result;
1905  }
1906  return NULL;
1907 }
1908 
1909 static void SetRawReassemblyFlag(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
1910 {
1911  const Signature *s = NULL;
1912  uint32_t sig;
1913 
1914  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
1915  s = sgh->init->match_array[sig];
1916  if (s == NULL)
1917  continue;
1918 
1919  if (SignatureHasStreamContent(s) == 1) {
1921  SCLogDebug("rule group %p has SIG_GROUP_HEAD_HAVERAWSTREAM set", sgh);
1922  return;
1923  }
1924  }
1925  SCLogDebug("rule group %p does NOT have SIG_GROUP_HEAD_HAVERAWSTREAM set", sgh);
1926 }
1927 
1928 typedef struct DetectBufferInstance {
1929  // key
1930  int list;
1932 
1933  struct SidsArray ts;
1934  struct SidsArray tc;
1936 
1937 static uint32_t DetectBufferInstanceHashFunc(HashListTable *ht, void *data, uint16_t datalen)
1938 {
1939  const DetectBufferInstance *ms = (const DetectBufferInstance *)data;
1940  uint32_t hash = ms->list + ms->alproto;
1941  return hash % ht->array_size;
1942 }
1943 
1944 static char DetectBufferInstanceCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
1945 {
1946  const DetectBufferInstance *ms1 = (DetectBufferInstance *)data1;
1947  const DetectBufferInstance *ms2 = (DetectBufferInstance *)data2;
1948  return (ms1->list == ms2->list && ms1->alproto == ms2->alproto);
1949 }
1950 
1951 static void DetectBufferInstanceFreeFunc(void *ptr)
1952 {
1953  DetectBufferInstance *ms = ptr;
1954  if (ms->ts.sids_array != NULL)
1955  SCFree(ms->ts.sids_array);
1956  if (ms->tc.sids_array != NULL)
1957  SCFree(ms->tc.sids_array);
1958  SCFree(ms);
1959 }
1960 
1961 static HashListTable *DetectBufferInstanceInit(void)
1962 {
1963  return HashListTableInit(4096, DetectBufferInstanceHashFunc, DetectBufferInstanceCompareFunc,
1964  DetectBufferInstanceFreeFunc);
1965 }
1966 
1967 static void PrepareMpms(DetectEngineCtx *de_ctx, SigGroupHead *sh)
1968 {
1969  HashListTable *bufs = DetectBufferInstanceInit();
1970  BUG_ON(bufs == NULL);
1971 
1972  const int max_buffer_id = de_ctx->buffer_type_id + 1;
1973  const uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
1974 
1975  AppProto engines[max_buffer_id][ALPROTO_MAX];
1976  memset(engines, 0, sizeof(engines));
1977  int engines_idx[max_buffer_id];
1978  memset(engines_idx, 0, sizeof(engines_idx));
1979  int types[max_buffer_id];
1980  memset(types, 0, sizeof(types));
1981 
1982  /* flag the list+directions we have engines for as active */
1983  for (DetectBufferMpmRegistry *a = de_ctx->pkt_mpms_list; a != NULL; a = a->next) {
1984  types[a->sm_list] = a->type;
1985 
1986  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = ALPROTO_UNKNOWN };
1987  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
1988  if (instance == NULL) {
1989  instance = SCCalloc(1, sizeof(*instance));
1990  BUG_ON(instance == NULL);
1991  instance->list = a->sm_list;
1992  instance->alproto = ALPROTO_UNKNOWN;
1993  HashListTableAdd(bufs, instance, 0);
1994  }
1995  instance->ts.active = true;
1996  instance->tc.active = true;
1997  }
1998  for (DetectBufferMpmRegistry *a = de_ctx->frame_mpms_list; a != NULL; a = a->next) {
1999  const bool add_ts = ((a->direction == SIG_FLAG_TOSERVER) && SGH_DIRECTION_TS(sh));
2000  const bool add_tc = ((a->direction == SIG_FLAG_TOCLIENT) && SGH_DIRECTION_TC(sh));
2001  if (add_ts || add_tc) {
2002  types[a->sm_list] = a->type;
2003  engines[a->sm_list][engines_idx[a->sm_list]++] = a->frame_v1.alproto;
2004 
2005  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->frame_v1.alproto };
2006  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2007  if (instance == NULL) {
2008  instance = SCCalloc(1, sizeof(*instance));
2009  BUG_ON(instance == NULL);
2010  instance->list = a->sm_list;
2011  instance->alproto = a->frame_v1.alproto;
2012  HashListTableAdd(bufs, instance, 0);
2013  }
2014  instance->ts.active |= add_ts;
2015  instance->tc.active |= add_tc;
2016  }
2017  }
2018  for (DetectBufferMpmRegistry *a = de_ctx->app_mpms_list; a != NULL; a = a->next) {
2019  const bool add_ts = ((a->direction == SIG_FLAG_TOSERVER) && SGH_DIRECTION_TS(sh));
2020  const bool add_tc = ((a->direction == SIG_FLAG_TOCLIENT) && SGH_DIRECTION_TC(sh));
2021  if (add_ts || add_tc) {
2022  types[a->sm_list] = a->type;
2023  engines[a->sm_list][engines_idx[a->sm_list]++] = a->app_v2.alproto;
2024 
2025  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->app_v2.alproto };
2026  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2027  if (instance == NULL) {
2028  instance = SCCalloc(1, sizeof(*instance));
2029  BUG_ON(instance == NULL);
2030  instance->list = a->sm_list;
2031  instance->alproto = a->app_v2.alproto;
2032  HashListTableAdd(bufs, instance, 0);
2033  }
2034  instance->ts.active |= add_ts;
2035  instance->tc.active |= add_tc;
2036  }
2037  }
2038 
2039  for (uint32_t sig = 0; sig < sh->init->sig_cnt; sig++) {
2040  const Signature *s = sh->init->match_array[sig];
2041  if (s == NULL)
2042  continue;
2043  if (s->init_data->mpm_sm == NULL)
2044  continue;
2045  const int list = s->init_data->mpm_sm_list;
2046  if (list < 0)
2047  continue;
2048  if (list == DETECT_SM_LIST_PMATCH)
2049  continue;
2050 
2051  switch (types[list]) {
2052  /* app engines are direction aware */
2055  for (int e = 0; e < engines_idx[list]; e++) {
2056  const AppProto alproto = engines[list][e];
2057  if (!(AppProtoEquals(s->alproto, alproto) || s->alproto == 0))
2058  continue;
2059 
2060  DetectBufferInstance lookup = { .list = list, .alproto = alproto };
2061  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2062  if (instance == NULL)
2063  continue;
2064  if (s->flags & SIG_FLAG_TOSERVER) {
2065  struct SidsArray *sa = &instance->ts;
2066  if (sa->active) {
2067  if (sa->sids_array == NULL) {
2068  sa->sids_array = SCCalloc(1, max_sid);
2069  sa->sids_array_size = max_sid;
2070  BUG_ON(sa->sids_array == NULL); // TODO
2071  }
2072  sa->sids_array[s->num / 8] |= 1 << (s->num % 8);
2073  SCLogDebug("instance %p: stored %u/%u ts", instance, s->id, s->num);
2074  }
2075  }
2076  if (s->flags & SIG_FLAG_TOCLIENT) {
2077  struct SidsArray *sa = &instance->tc;
2078  if (sa->active) {
2079  if (sa->sids_array == NULL) {
2080  sa->sids_array = SCCalloc(1, max_sid);
2081  sa->sids_array_size = max_sid;
2082  BUG_ON(sa->sids_array == NULL); // TODO
2083  }
2084  sa->sids_array[s->num / 8] |= 1 << (s->num % 8);
2085  SCLogDebug("instance %p: stored %u/%u tc", instance, s->id, s->num);
2086  }
2087  }
2088  }
2089  break;
2090  }
2091  /* pkt engines are directionless, so only use ts */
2093  DetectBufferInstance lookup = { .list = list, .alproto = ALPROTO_UNKNOWN };
2094  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2095  if (instance == NULL)
2096  continue;
2097  struct SidsArray *sa = &instance->ts;
2098  if (sa->active) {
2099  if (sa->sids_array == NULL) {
2100  sa->sids_array = SCCalloc(1, max_sid);
2101  sa->sids_array_size = max_sid;
2102  BUG_ON(sa->sids_array == NULL); // TODO
2103  }
2104  sa->sids_array[s->num / 8] |= 1 << (s->num % 8);
2105  }
2106  break;
2107  }
2108  default:
2109  abort();
2110  break;
2111  }
2112  }
2113 
2114  sh->init->app_mpms = SCCalloc(de_ctx->app_mpms_list_cnt, sizeof(MpmCtx *));
2115  BUG_ON(sh->init->app_mpms == NULL);
2116 
2117  sh->init->pkt_mpms = SCCalloc(de_ctx->pkt_mpms_list_cnt, sizeof(MpmCtx *));
2118  BUG_ON(sh->init->pkt_mpms == NULL);
2119 
2121  BUG_ON(sh->init->frame_mpms == NULL);
2122 
2123  for (DetectBufferMpmRegistry *a = de_ctx->pkt_mpms_list; a != NULL; a = a->next) {
2124  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = ALPROTO_UNKNOWN };
2125  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2126  if (instance == NULL) {
2127  continue;
2128  }
2129  struct SidsArray *sa = &instance->ts;
2130  if (!sa->active)
2131  continue;
2132 
2133  MpmStore *mpm_store = MpmStorePrepareBufferPkt(de_ctx, sh, a, sa);
2134  if (mpm_store != NULL) {
2135  sh->init->pkt_mpms[a->id] = mpm_store->mpm_ctx;
2136 
2137  SCLogDebug("a %p a->name %s a->reg->PrefilterRegisterWithListId %p "
2138  "mpm_store->mpm_ctx %p", a, a->name,
2139  a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2140 
2141  /* if we have just certain types of negated patterns,
2142  * mpm_ctx can be NULL */
2143  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2144  BUG_ON(a->PrefilterRegisterWithListId(de_ctx,
2145  sh, mpm_store->mpm_ctx,
2146  a, a->sm_list) != 0);
2147  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2148  }
2149  }
2150  }
2151  for (DetectBufferMpmRegistry *a = de_ctx->frame_mpms_list; a != NULL; a = a->next) {
2152  if ((a->direction == SIG_FLAG_TOSERVER && SGH_DIRECTION_TS(sh)) ||
2153  (a->direction == SIG_FLAG_TOCLIENT && SGH_DIRECTION_TC(sh))) {
2154  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->frame_v1.alproto };
2155  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2156  if (instance == NULL) {
2157  continue;
2158  }
2159  struct SidsArray *sa =
2160  (a->direction == SIG_FLAG_TOSERVER) ? &instance->ts : &instance->tc;
2161  if (!sa->active)
2162  continue;
2163 
2164  SCLogDebug("a %s direction %d PrefilterRegisterWithListId %p", a->name, a->direction,
2165  a->PrefilterRegisterWithListId);
2166  MpmStore *mpm_store = MpmStorePrepareBufferFrame(de_ctx, sh, a, sa);
2167  if (mpm_store != NULL) {
2168  sh->init->frame_mpms[a->id] = mpm_store->mpm_ctx;
2169 
2170  SCLogDebug("a %p a->name %s a->reg->PrefilterRegisterWithListId %p "
2171  "mpm_store->mpm_ctx %p",
2172  a, a->name, a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2173 
2174  /* if we have just certain types of negated patterns,
2175  * mpm_ctx can be NULL */
2176  SCLogDebug("mpm_store %p mpm_ctx %p", mpm_store, mpm_store->mpm_ctx);
2177  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2178  BUG_ON(a->PrefilterRegisterWithListId(
2179  de_ctx, sh, mpm_store->mpm_ctx, a, a->sm_list) != 0);
2180  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2181  }
2182  }
2183  }
2184  }
2185  for (DetectBufferMpmRegistry *a = de_ctx->app_mpms_list; a != NULL; a = a->next) {
2186  if ((a->direction == SIG_FLAG_TOSERVER && SGH_DIRECTION_TS(sh)) ||
2187  (a->direction == SIG_FLAG_TOCLIENT && SGH_DIRECTION_TC(sh))) {
2188 
2189  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->app_v2.alproto };
2190  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2191  if (instance == NULL) {
2192  continue;
2193  }
2194  struct SidsArray *sa =
2195  (a->direction == SIG_FLAG_TOSERVER) ? &instance->ts : &instance->tc;
2196  if (!sa->active)
2197  continue;
2198 
2199  MpmStore *mpm_store = MpmStorePrepareBufferAppLayer(de_ctx, sh, a, sa);
2200  if (mpm_store != NULL) {
2201  sh->init->app_mpms[a->id] = mpm_store->mpm_ctx;
2202 
2203  SCLogDebug("a %p a->name %s a->PrefilterRegisterWithListId %p "
2204  "mpm_store->mpm_ctx %p",
2205  a, a->name, a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2206 
2207  /* if we have just certain types of negated patterns,
2208  * mpm_ctx can be NULL */
2209  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2210  BUG_ON(a->PrefilterRegisterWithListId(
2211  de_ctx, sh, mpm_store->mpm_ctx, a, a->sm_list) != 0);
2212  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2213  }
2214  }
2215  }
2216  }
2217  HashListTableFree(bufs);
2218 }
2219 
2220 /** \brief Prepare the pattern matcher ctx in a sig group head.
2221  *
2222  */
2224 {
2225  MpmStore *mpm_store = NULL;
2226  if (SGH_PROTO(sh, IPPROTO_TCP)) {
2227  if (SGH_DIRECTION_TS(sh)) {
2228  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_PKT_TS);
2229  if (mpm_store != NULL) {
2230  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2231  }
2232 
2234  if (mpm_store != NULL) {
2235  PrefilterPktStreamRegister(de_ctx, sh, mpm_store->mpm_ctx);
2236  }
2237 
2238  SetRawReassemblyFlag(de_ctx, sh);
2239  }
2240  if (SGH_DIRECTION_TC(sh)) {
2241  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_PKT_TC);
2242  if (mpm_store != NULL) {
2243  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2244  }
2245 
2247  if (mpm_store != NULL) {
2248  PrefilterPktStreamRegister(de_ctx, sh, mpm_store->mpm_ctx);
2249  }
2250 
2251  SetRawReassemblyFlag(de_ctx, sh);
2252  }
2253  } else if (SGH_PROTO(sh, IPPROTO_UDP)) {
2254  if (SGH_DIRECTION_TS(sh)) {
2255  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_UDP_TS);
2256  if (mpm_store != NULL) {
2257  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2258  }
2259  }
2260  if (SGH_DIRECTION_TC(sh)) {
2261  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_UDP_TC);
2262  if (mpm_store != NULL) {
2263  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2264  }
2265  }
2266  } else {
2267  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_OTHERIP);
2268  if (mpm_store != NULL) {
2269  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2270  }
2271  }
2272 
2273  PrepareMpms(de_ctx, sh);
2274  return 0;
2275 }
2276 
2277 static inline uint32_t ContentFlagsForHash(const DetectContentData *cd)
2278 {
2281 }
2282 
2283 /** \internal
2284  * \brief The hash function for Pattern. Hashes pattern after chop is applied.
2285  *
2286  * \param ht Pointer to the hash table.
2287  * \param data Pointer to the Pattern.
2288  * \param datalen Not used in our case.
2289  *
2290  * \retval hash The generated hash value.
2291  */
2292 static uint32_t PatternChopHashFunc(HashListTable *ht, void *data, uint16_t datalen)
2293 {
2294  const DetectPatternTracker *p = (DetectPatternTracker *)data;
2295  uint32_t hash = p->sm_list + ContentFlagsForHash(p->cd);
2296  uint16_t content_len = p->cd->content_len;
2297  const uint8_t *content = p->cd->content;
2299  content += p->cd->fp_chop_offset;
2300  content_len = p->cd->fp_chop_len;
2301  }
2302  hash += StringHashDjb2(content, content_len);
2303  return hash % ht->array_size;
2304 }
2305 
2306 /** \internal
2307  * \brief The hash function for Pattern. Ignores chop.
2308  *
2309  * \param ht Pointer to the hash table.
2310  * \param data Pointer to the Pattern.
2311  * \param datalen Not used in our case.
2312  *
2313  * \retval hash The generated hash value.
2314  */
2315 static uint32_t PatternNoChopHashFunc(HashListTable *ht, void *data, uint16_t datalen)
2316 {
2317  const DetectPatternTracker *p = (DetectPatternTracker *)data;
2318  uint32_t hash = p->sm_list + ContentFlagsForHash(p->cd);
2319  hash += StringHashDjb2(p->cd->content, p->cd->content_len);
2320  return hash % ht->array_size;
2321 }
2322 
2323 /**
2324  * \brief The Compare function for Pattern. Compares patterns after chop is applied.
2325  *
2326  * \param data1 Pointer to the first Pattern.
2327  * \param len1 Not used.
2328  * \param data2 Pointer to the second Pattern.
2329  * \param len2 Not used.
2330  *
2331  * \retval 1 If the 2 Patterns sent as args match.
2332  * \retval 0 If the 2 Patterns sent as args do not match.
2333  */
2334 static char PatternChopCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
2335 {
2336  const DetectPatternTracker *p1 = (DetectPatternTracker *)data1;
2337  const DetectPatternTracker *p2 = (DetectPatternTracker *)data2;
2338 
2339  if (p1->sm_list != p2->sm_list)
2340  return 0;
2341 
2342  if (ContentFlagsForHash(p1->cd) != ContentFlagsForHash(p2->cd))
2343  return 0;
2344 
2345  uint16_t p1_content_len = p1->cd->content_len;
2346  uint8_t *p1_content = p1->cd->content;
2348  p1_content += p1->cd->fp_chop_offset;
2349  p1_content_len = p1->cd->fp_chop_len;
2350  }
2351  uint16_t p2_content_len = p2->cd->content_len;
2352  uint8_t *p2_content = p2->cd->content;
2354  p2_content += p2->cd->fp_chop_offset;
2355  p2_content_len = p2->cd->fp_chop_len;
2356  }
2357 
2358  if (p1_content_len != p2_content_len)
2359  return 0;
2360 
2361  if (memcmp(p1_content, p2_content, p1_content_len) != 0) {
2362  return 0;
2363  }
2364 
2365  return 1;
2366 }
2367 
2368 /**
2369  * \brief The Compare function for Pattern. Ignores chop settings
2370  *
2371  * \param data1 Pointer to the first Pattern.
2372  * \param len1 Not used.
2373  * \param data2 Pointer to the second Pattern.
2374  * \param len2 Not used.
2375  *
2376  * \retval 1 If the 2 Patterns sent as args match.
2377  * \retval 0 If the 2 Patterns sent as args do not match.
2378  */
2379 static char PatternNoChopCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
2380 {
2381  const DetectPatternTracker *p1 = (DetectPatternTracker *)data1;
2382  const DetectPatternTracker *p2 = (DetectPatternTracker *)data2;
2383 
2384  if (p1->sm_list != p2->sm_list)
2385  return 0;
2386 
2387  if (ContentFlagsForHash(p1->cd) != ContentFlagsForHash(p2->cd))
2388  return 0;
2389 
2390  if (p1->cd->content_len != p2->cd->content_len)
2391  return 0;
2392 
2393  if (memcmp(p1->cd->content, p2->cd->content, p1->cd->content_len) != 0) {
2394  return 0;
2395  }
2396 
2397  return 1;
2398 }
2399 
2400 static void PatternFreeFunc(void *ptr)
2401 {
2402  SCFree(ptr);
2403 }
2404 
2405 /**
2406  * \brief Figure out the FP and their respective content ids for all the
2407  * sigs in the engine.
2408  *
2409  * \param de_ctx Detection engine context.
2410  *
2411  * \retval 0 On success.
2412  * \retval -1 On failure.
2413  */
2415 {
2416  uint32_t cnt = 0;
2417  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
2418  if (s->flags & SIG_FLAG_PREFILTER)
2419  continue;
2420 
2422  if (s->init_data->mpm_sm != NULL) {
2423  s->flags |= SIG_FLAG_PREFILTER;
2424  cnt++;
2425  }
2426  }
2427  /* no mpm rules */
2428  if (cnt == 0)
2429  return 0;
2430 
2431  HashListTable *ht =
2432  HashListTableInit(4096, PatternChopHashFunc, PatternChopCompareFunc, PatternFreeFunc);
2433  BUG_ON(ht == NULL);
2434  PatIntId max_id = 0;
2435 
2436  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
2437  if (s->init_data->mpm_sm == NULL)
2438  continue;
2439 
2440  const int sm_list = s->init_data->mpm_sm_list;
2441  BUG_ON(sm_list == -1);
2442 
2444 
2445  DetectPatternTracker lookup = { .cd = cd, .sm_list = sm_list, .cnt = 0, .mpm = 0 };
2446  DetectPatternTracker *res = HashListTableLookup(ht, &lookup, 0);
2447  if (res) {
2448  res->cnt++;
2449  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2450 
2451  cd->id = res->cd->id;
2452  SCLogDebug("%u: res id %u cnt %u", s->id, res->cd->id, res->cnt);
2453  } else {
2454  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2455  BUG_ON(add == NULL);
2456  add->cd = cd;
2457  add->sm_list = sm_list;
2458  add->cnt = 1;
2459  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2460  HashListTableAdd(ht, (void *)add, 0);
2461 
2462  cd->id = max_id++;
2463  SCLogDebug("%u: add id %u cnt %u", s->id, add->cd->id, add->cnt);
2464  }
2465  }
2466 
2467  HashListTableFree(ht);
2468 
2469  return 0;
2470 }
2471 
2472 /** \brief add all patterns on our stats hash
2473  * Used to fill the hash later used by DumpPatterns()
2474  * \note sets up the hash table on first call
2475  */
2477 {
2478  if (de_ctx->pattern_hash_table == NULL) {
2480  4096, PatternNoChopHashFunc, PatternNoChopCompareFunc, PatternFreeFunc);
2481  BUG_ON(de_ctx->pattern_hash_table == NULL);
2482  }
2483  if (s->sm_arrays[DETECT_SM_LIST_PMATCH]) {
2485  do {
2486  switch (smd->type) {
2487  case DETECT_CONTENT: {
2488  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2489  DetectPatternTracker lookup = {
2490  .cd = cd, .sm_list = DETECT_SM_LIST_PMATCH, .cnt = 0, .mpm = 0
2491  };
2492  DetectPatternTracker *res =
2494  if (res) {
2495  res->cnt++;
2496  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2497  } else {
2498  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2499  BUG_ON(add == NULL);
2500  add->cd = cd;
2502  add->cnt = 1;
2503  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2504  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2505  }
2506  break;
2507  }
2508  }
2509  if (smd->is_last)
2510  break;
2511  smd++;
2512  } while (1);
2513  }
2514 
2516  for (; app != NULL; app = app->next) {
2517  DEBUG_VALIDATE_BUG_ON(app->smd == NULL);
2518  SigMatchData *smd = app->smd;
2519  do {
2520  switch (smd->type) {
2521  case DETECT_CONTENT: {
2522  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2523 
2524  DetectPatternTracker lookup = {
2525  .cd = cd, .sm_list = app->sm_list, .cnt = 0, .mpm = 0
2526  };
2527  DetectPatternTracker *res =
2529  if (res) {
2530  res->cnt++;
2531  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2532  } else {
2533  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2534  BUG_ON(add == NULL);
2535  add->cd = cd;
2536  add->sm_list = app->sm_list;
2537  add->cnt = 1;
2538  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2539  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2540  }
2541  break;
2542  }
2543  }
2544  if (smd->is_last)
2545  break;
2546  smd++;
2547  } while (1);
2548  }
2550  for (; pkt != NULL; pkt = pkt->next) {
2551  SigMatchData *smd = pkt->smd;
2552  do {
2553  if (smd == NULL) {
2555  smd = s->sm_arrays[pkt->sm_list];
2556  }
2557  switch (smd->type) {
2558  case DETECT_CONTENT: {
2559  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2560 
2561  DetectPatternTracker lookup = {
2562  .cd = cd, .sm_list = pkt->sm_list, .cnt = 0, .mpm = 0
2563  };
2564  DetectPatternTracker *res =
2566  if (res) {
2567  res->cnt++;
2568  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2569  } else {
2570  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2571  BUG_ON(add == NULL);
2572  add->cd = cd;
2573  add->sm_list = pkt->sm_list;
2574  add->cnt = 1;
2575  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2576  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2577  }
2578  break;
2579  }
2580  }
2581  if (smd->is_last)
2582  break;
2583  smd++;
2584  } while (1);
2585  }
2587  for (; frame != NULL; frame = frame->next) {
2588  SigMatchData *smd = frame->smd;
2589  do {
2590  if (smd == NULL) {
2592  smd = s->sm_arrays[frame->sm_list];
2593  }
2594  switch (smd->type) {
2595  case DETECT_CONTENT: {
2596  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2597 
2598  DetectPatternTracker lookup = {
2599  .cd = cd, .sm_list = frame->sm_list, .cnt = 0, .mpm = 0
2600  };
2601  DetectPatternTracker *res =
2603  if (res) {
2604  res->cnt++;
2605  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2606  } else {
2607  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2608  BUG_ON(add == NULL);
2609  add->cd = cd;
2610  add->sm_list = frame->sm_list;
2611  add->cnt = 1;
2612  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2613  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2614  }
2615  break;
2616  }
2617  }
2618  if (smd->is_last)
2619  break;
2620  smd++;
2621  } while (1);
2622  }
2623 }
MpmInitThreadCtx
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t matcher)
Definition: util-mpm.c:196
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
SignatureHasPacketContent
int SignatureHasPacketContent(const Signature *s)
check if a signature has patterns that are to be inspected against a packets payload (as opposed to t...
Definition: detect-engine-mpm.c:796
DetectEngineCtx_::pkt_mpms_list_cnt
uint32_t pkt_mpms_list_cnt
Definition: detect.h:996
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:56
DetectEngineCtx_::frame_mpms_list_cnt
uint32_t frame_mpms_list_cnt
Definition: detect.h:999
DetectContentData_::offset
uint16_t offset
Definition: detect-content.h:107
SignatureInitData_::max_content_list_id
uint32_t max_content_list_id
Definition: detect.h:597
DetectAppLayerMpmMultiRegister
void DetectAppLayerMpmMultiRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionMultiBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
Definition: detect-engine-mpm.c:159
DetectPatternTracker
Definition: detect.h:730
SCFPSupportSMList_
Definition: detect.h:760
DetectEngineAppInspectionEngine_
Definition: detect.h:429
util-hash-string.h
MPMB_UDP_TS
@ MPMB_UDP_TS
Definition: detect.h:1341
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:535
detect-content.h
SGH_DIRECTION_TC
#define SGH_DIRECTION_TC(sgh)
Definition: detect-engine-mpm.c:1006
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
detect-engine.h
MPM_TABLE_SIZE
@ MPM_TABLE_SIZE
Definition: util-mpm.h:40
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:116
DetectBufferMpmRegistry_::direction
int direction
Definition: detect.h:688
DETECT_CONTENT_FAST_PATTERN_CHOP
#define DETECT_CONTENT_FAST_PATTERN_CHOP
Definition: detect-content.h:36
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:586
PatternMatchDestroy
void PatternMatchDestroy(MpmCtx *mpm_ctx, uint16_t mpm_matcher)
Definition: detect-engine-mpm.c:900
MpmStore_::sid_array_size
uint32_t sid_array_size
Definition: detect.h:1349
DetectContentData_::fp_chop_len
uint16_t fp_chop_len
Definition: detect-content.h:98
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
MpmStore_::sid_array
uint8_t * sid_array
Definition: detect.h:1348
DetectEngineCtx_::sgh_mpm_context_proto_tcp_packet
int32_t sgh_mpm_context_proto_tcp_packet
Definition: detect.h:910
PatternMatchPrepareGroup
int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh)
Prepare the pattern matcher ctx in a sig group head.
Definition: detect-engine-mpm.c:2223
SCFPSupportSMList_::next
struct SCFPSupportSMList_ * next
Definition: detect.h:763
DetectEnginePktInspectionEngine
Definition: detect.h:486
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:41
DetectEngineAppInspectionEngine_::next
struct DetectEngineAppInspectionEngine_ * next
Definition: detect.h:451
detect-engine-siggroup.h
SigGroupHead_::flags
uint16_t flags
Definition: detect.h:1458
SCFPSupportSMList_::list_id
int list_id
Definition: detect.h:761
SigTableElmt_::name
const char * name
Definition: detect.h:1301
MpmStoreFree
void MpmStoreFree(DetectEngineCtx *de_ctx)
Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by MpmStoreInit() function.
Definition: detect-engine-mpm.c:1526
DetectFrameMpmRegisterByParentId
void DetectFrameMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id, DetectEngineTransforms *transforms)
copy a mpm engine from parent_id, add in transforms
Definition: detect-engine-mpm.c:359
MpmThreadCtx_
Definition: util-mpm.h:46
DetectPatternTracker::mpm
uint32_t mpm
Definition: detect.h:734
Signature_::num
SigIntId num
Definition: detect.h:613
ConfGetBool
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition: conf.c:482
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1457
DetectEngineCtx_::pattern_hash_table
HashListTable * pattern_hash_table
Definition: detect.h:877
DetectEngineTransforms
Definition: detect.h:408
DetectBufferMpmRegistry_::sm_list_base
int16_t sm_list_base
Definition: detect.h:690
MpmFactoryReClaimMpmCtx
void MpmFactoryReClaimMpmCtx(const DetectEngineCtx *de_ctx, MpmCtx *mpm_ctx)
Definition: util-mpm.c:157
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:70
MpmStoreReportStats
void MpmStoreReportStats(const DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:1429
Signature_::alproto
AppProto alproto
Definition: detect.h:606
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
MPMB_OTHERIP
@ MPMB_OTHERIP
Definition: detect.h:1343
DetectPktMpmRegister
void DetectPktMpmRegister(const char *name, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id), InspectionBufferGetPktDataPtr GetData)
register a MPM engine
Definition: detect-engine-mpm.c:541
SigMatchData_::is_last
bool is_last
Definition: detect.h:360
DetectBufferTypeSupportsFrames
void DetectBufferTypeSupportsFrames(const char *name)
Definition: detect-engine.c:1051
DetectMpmInitializeFrameMpms
void DetectMpmInitializeFrameMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:461
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:84
DETECT_SM_LIST_DYNAMIC_START
@ DETECT_SM_LIST_DYNAMIC_START
Definition: detect.h:135
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:361
DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED
#define DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED
Definition: detect-content.h:55
InspectionMultiBufferGetDataPtr
InspectionBuffer *(* InspectionMultiBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id, const uint32_t local_id)
Definition: detect.h:419
MpmStore_::sm_list
int sm_list
Definition: detect.h:1353
PatternStrength
uint32_t PatternStrength(uint8_t *pat, uint16_t patlen)
Predict a strength value for patterns.
Definition: detect-engine-mpm.c:930
DetectEngineCtx_::pkt_mpms_list
DetectBufferMpmRegistry * pkt_mpms_list
Definition: detect.h:995
DETECT_BUFFER_MPM_TYPE_FRAME
@ DETECT_BUFFER_MPM_TYPE_FRAME
Definition: detect.h:679
DetectSetFastPatternAndItsId
int DetectSetFastPatternAndItsId(DetectEngineCtx *de_ctx)
Figure out the FP and their respective content ids for all the sigs in the engine.
Definition: detect-engine-mpm.c:2414
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:414
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:78
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
DetectEnginePktInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:487
util-memcpy.h
DETECT_CONTENT_MPM_IS_CONCLUSIVE
#define DETECT_CONTENT_MPM_IS_CONCLUSIVE(c)
Definition: detect-content.h:78
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@85::@87 app_v2
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:287
DETECT_CONTENT_DEPTH_VAR
#define DETECT_CONTENT_DEPTH_VAR
Definition: detect-content.h:46
InspectionBufferGetPktDataPtr
InspectionBuffer *(* InspectionBufferGetPktDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Packet *p, const int list_id)
Definition: detect.h:481
DetectEngineBufferTypeGetNameById
const char * DetectEngineBufferTypeGetNameById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1121
DetectMpmInitializeBuiltinMpms
void DetectMpmInitializeBuiltinMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:726
DetectBufferMpmRegistry_::next
struct DetectBufferMpmRegistry_ * next
Definition: detect.h:726
SIG_FLAG_REQUIRE_STREAM
#define SIG_FLAG_REQUIRE_STREAM
Definition: detect.h:251
DetectPatternTracker::cnt
uint32_t cnt
Definition: detect.h:733
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:685
DE_QUIET
#define DE_QUIET
Definition: detect.h:323
MpmCtx_::maxlen
uint16_t maxlen
Definition: util-mpm.h:100
MPMB_TCP_STREAM_TS
@ MPMB_TCP_STREAM_TS
Definition: detect.h:1339
DetectPatternTracker::cd
const struct DetectContentData_ * cd
Definition: detect.h:731
PatIntId
#define PatIntId
Definition: suricata-common.h:318
SIG_GROUP_HEAD_HAVERAWSTREAM
#define SIG_GROUP_HEAD_HAVERAWSTREAM
Definition: detect.h:1327
MpmTableElmt_::feature_flags
uint8_t feature_flags
Definition: util-mpm.h:174
mpm_default_matcher
uint8_t mpm_default_matcher
Definition: util-mpm.c:48
Signature_::sm_arrays
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:654
DetectContentData_
Definition: detect-content.h:93
DetectContentData_::fp_chop_offset
uint16_t fp_chop_offset
Definition: detect-content.h:100
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:245
detect-engine-payload.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
MAX
#define MAX(x, y)
Definition: suricata-common.h:395
ALPROTO_MAX
@ ALPROTO_MAX
Definition: app-layer-protos.h:79
MPMB_MAX
@ MPMB_MAX
Definition: detect.h:1344
SigMatchData_
Data needed for Match()
Definition: detect.h:358
DetectEngineCtx_::sgh_mpm_context_proto_udp_packet
int32_t sgh_mpm_context_proto_udp_packet
Definition: detect.h:911
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:698
ShortenString
void ShortenString(const char *input, char *output, size_t output_size, char c)
Definition: util-misc.c:207
SigMatchData_::type
uint16_t type
Definition: detect.h:359
DetectEngineRegisterFastPatternForId
void DetectEngineRegisterFastPatternForId(DetectEngineCtx *de_ctx, int list_id, int priority)
Definition: detect-fast-pattern.c:134
SidsArray
Definition: detect-engine-mpm.c:1764
MPMB_TCP_STREAM_TC
@ MPMB_TCP_STREAM_TC
Definition: detect.h:1340
detect-engine-prefilter.h
EngineAnalysisAddAllRulePatterns
void EngineAnalysisAddAllRulePatterns(DetectEngineCtx *de_ctx, const Signature *s)
add all patterns on our stats hash Used to fill the hash later used by DumpPatterns()
Definition: detect-engine-mpm.c:2476
Signature_::frame_inspect
DetectEngineFrameInspectionEngine * frame_inspect
Definition: detect.h:650
MpmBuiltinBuffers
MpmBuiltinBuffers
Definition: detect.h:1336
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1091
HashListTableAdd
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:114
detect-udphdr.h
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
HashListTable_::array_size
uint32_t array_size
Definition: util-hashlist.h:41
DetectAppLayerMpmRegisterByParentId
void DetectAppLayerMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id, DetectEngineTransforms *transforms)
copy a mpm engine from parent_id, add in transforms
Definition: detect-engine-mpm.c:168
util-memcmp.h
SigGroupHeadInitData_::pkt_mpms
MpmCtx ** pkt_mpms
Definition: detect.h:1441
MpmInitCtx
void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher)
Definition: util-mpm.c:210
Signature_::next
struct Signature_ * next
Definition: detect.h:673
DetectEngineCtx_::sgh_mpm_context_proto_other_packet
int32_t sgh_mpm_context_proto_other_packet
Definition: detect.h:912
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:435
DetectBufferInstance::alproto
AppProto alproto
Definition: detect-engine-mpm.c:1931
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:335
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:55
DetectBufferMpmType
DetectBufferMpmType
Definition: detect.h:676
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
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
decode.h
MpmDestroyThreadCtx
void MpmDestroyThreadCtx(MpmThreadCtx *mpm_thread_ctx, const uint16_t matcher)
Definition: util-mpm.c:203
util-debug.h
DetectBufferInstance::ts
struct SidsArray ts
Definition: detect-engine-mpm.c:1933
SigGroupHeadInitData_::sig_cnt
SigIntId sig_cnt
Definition: detect.h:1450
SidsArray::sids_array_size
uint32_t sids_array_size
Definition: detect-engine-mpm.c:1766
DETECT_CONTENT_ENDS_WITH
#define DETECT_CONTENT_ENDS_WITH
Definition: detect-content.h:42
DetectBufferMpmRegistry_::sgh_mpm_context
int sgh_mpm_context
Definition: detect.h:694
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
MpmFactoryGetMpmCtxForProfile
MpmCtx * MpmFactoryGetMpmCtxForProfile(const DetectEngineCtx *de_ctx, int32_t id, int direction)
Definition: util-mpm.c:132
DetectEnginePktInspectionEngine::sm_list
uint16_t sm_list
Definition: detect.h:489
DetectMpmInitializePktMpms
void DetectMpmInitializePktMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:628
MPM_PATTERN_CTX_OWNS_ID
#define MPM_PATTERN_CTX_OWNS_ID
Definition: util-mpm.h:139
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
MpmStore_
Definition: detect.h:1347
SignatureInitData_::mpm_sm
SigMatch * mpm_sm
Definition: detect.h:565
DetectBufferMpmRegistry_::sm_list
int16_t sm_list
Definition: detect.h:689
DetectEngineGetMaxSigId
#define DetectEngineGetMaxSigId(de_ctx)
Definition: detect-engine.h:105
DetectBufferMpmRegistry_::pkt_v1
struct DetectBufferMpmRegistry_::@85::@88 pkt_v1
SignatureInitData_::mpm_sm_list
int mpm_sm_list
Definition: detect.h:563
SidsArray::sids_array
uint8_t * sids_array
Definition: detect-engine-mpm.c:1765
DETECT_CONTENT_DEPTH
#define DETECT_CONTENT_DEPTH
Definition: detect-content.h:33
Signature_::pkt_inspect
DetectEnginePktInspectionEngine * pkt_inspect
Definition: detect.h:649
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
DetectEngineFrameInspectionEngine::sm_list
uint16_t sm_list
Definition: detect.h:516
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:353
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:844
DETECT_CONTENT_IS_SINGLE
#define DETECT_CONTENT_IS_SINGLE(c)
Definition: detect-content.h:68
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
DetectBufferMpmRegistry_::priority
int priority
Definition: detect.h:691
PatternMatchThreadPrepare
void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
Definition: detect-engine-mpm.c:911
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
DetectEngineBufferTypeRegister
int DetectEngineBufferTypeRegister(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1178
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1480
DetectContentData_::id
PatIntId id
Definition: detect-content.h:105
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
Signature_::app_inspect
DetectEngineAppInspectionEngine * app_inspect
Definition: detect.h:648
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:352
DetectProto_::proto
uint8_t proto[256/8]
Definition: detect-engine-proto.h:37
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:300
MpmStore_::direction
int direction
Definition: detect.h:1351
MPMCTX_FLAGS_GLOBAL
#define MPMCTX_FLAGS_GLOBAL
Definition: util-mpm.h:85
PrefilterRegisterFunc
int(* PrefilterRegisterFunc)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-mpm.h:75
Signature_::flags
uint32_t flags
Definition: detect.h:602
DetectContentData_::depth
uint16_t depth
Definition: detect-content.h:106
stream.h
MpmFactoryRegisterMpmCtxProfile
int32_t MpmFactoryRegisterMpmCtxProfile(DetectEngineCtx *de_ctx, const char *name, const int sm_list, const AppProto alproto)
Register a new Mpm Context.
Definition: util-mpm.c:59
ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE
@ ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE
Definition: detect.h:1059
DetectEngineCtx_::sgh_mpm_context_stream
int32_t sgh_mpm_context_stream
Definition: detect.h:913
type
uint16_t type
Definition: decode-vlan.c:107
DetectEngineBufferTypeSupportsFrames
void DetectEngineBufferTypeSupportsFrames(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1217
DetectEngineCtx_::frame_mpms_list
DetectBufferMpmRegistry * frame_mpms_list
Definition: detect.h:998
conf.h
DetectEngineCtx_::sgh_mpm_ctx_cnf
uint8_t sgh_mpm_ctx_cnf
Definition: detect.h:939
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
PrefilterPktPayloadRegister
int PrefilterPktPayloadRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx)
Definition: detect-engine-payload.c:132
MpmStore_::alproto
AppProto alproto
Definition: detect.h:1355
DetectEngineFrameInspectionEngine
Definition: detect.h:511
DETECT_BUFFER_MPM_TYPE_PKT
@ DETECT_BUFFER_MPM_TYPE_PKT
Definition: detect.h:677
MpmTableElmt_::Prepare
int(* Prepare)(struct MpmCtx_ *)
Definition: util-mpm.h:166
ALPROTO_DOH2
@ ALPROTO_DOH2
Definition: app-layer-protos.h:61
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:670
FastPatternSupportEnabledForSigMatchList
int FastPatternSupportEnabledForSigMatchList(const DetectEngineCtx *de_ctx, const int list_id)
Checks if a particular buffer is in the list of lists that need to be searched for a keyword that has...
Definition: detect-fast-pattern.c:64
SCFPSupportSMList_::priority
int priority
Definition: detect.h:762
HashListTable_
Definition: util-hashlist.h:37
DetectEngineTransforms::transforms
TransformData transforms[DETECT_TRANSFORMS_MAX]
Definition: detect.h:409
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:64
SidsArray::type
enum DetectBufferMpmType type
Definition: detect-engine-mpm.c:1770
SigGroupHeadInitData_::app_mpms
MpmCtx ** app_mpms
Definition: detect.h:1440
MpmAddPatternCS
int MpmAddPatternCS(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:250
DetectEngineBufferTypeSupportsTransformations
void DetectEngineBufferTypeSupportsTransformations(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1241
SGH_DIRECTION_TS
#define SGH_DIRECTION_TS(sgh)
Definition: detect-engine-mpm.c:1005
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:873
DetectPatternTracker::sm_list
int sm_list
Definition: detect.h:732
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:449
DetectBufferInstance
Definition: detect-engine-mpm.c:1928
MpmStore_::mpm_ctx
MpmCtx * mpm_ctx
Definition: detect.h:1356
MPM_FEATURE_FLAG_ENDSWITH
#define MPM_FEATURE_FLAG_ENDSWITH
Definition: util-mpm.h:144
detect-fast-pattern.h
MpmStorePrepareBuffer
MpmStore * MpmStorePrepareBuffer(DetectEngineCtx *de_ctx, SigGroupHead *sgh, enum MpmBuiltinBuffers buf)
Get MpmStore for a built-in buffer type.
Definition: detect-engine-mpm.c:1631
MPMB_TCP_PKT_TC
@ MPMB_TCP_PKT_TC
Definition: detect.h:1338
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
DetectMpmPrepareFrameMpms
int DetectMpmPrepareFrameMpms(DetectEngineCtx *de_ctx)
initialize mpm contexts for applayer buffers that are in "single or "shared" mode.
Definition: detect-engine-mpm.c:513
DetectEngineFrameMpmRegister
void DetectEngineFrameMpmRegister(DetectEngineCtx *de_ctx, const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id), AppProto alproto, uint8_t type)
Definition: detect-engine-mpm.c:398
SignatureHasStreamContent
int SignatureHasStreamContent(const Signature *s)
check if a signature has patterns that are to be inspected against the stream payload (as opposed to ...
Definition: detect-engine-mpm.c:826
util-mpm.h
flags
uint8_t flags
Definition: decode-gre.h:0
Signature_::proto
DetectProto proto
Definition: detect.h:620
DetectBufferMpmRegistry_::type
enum DetectBufferMpmType type
Definition: detect.h:693
MpmStoreInit
int MpmStoreInit(DetectEngineCtx *de_ctx)
Initializes the MpmStore mpm hash table to be used by the detection engine context.
Definition: detect-engine-mpm.c:1363
DetectEngineCtx_::app_mpms_list
DetectBufferMpmRegistry * app_mpms_list
Definition: detect.h:990
suricata-common.h
MpmCtx_::pattern_cnt
uint32_t pattern_cnt
Definition: util-mpm.h:97
DETECT_BUFFER_MPM_TYPE_APP
@ DETECT_BUFFER_MPM_TYPE_APP
Definition: detect.h:678
DetectBufferTypeSupportsMpm
void DetectBufferTypeSupportsMpm(const char *name)
Definition: detect-engine.c:1071
DetectBufferInstance::list
int list
Definition: detect-engine-mpm.c:1930
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:88
SigGroupHeadInitData_::match_array
Signature ** match_array
Definition: detect.h:1453
DetectBufferMpmRegistry_::frame_v1
struct DetectBufferMpmRegistry_::@85::@89 frame_v1
DetectBufferMpmRegistry_::name
const char * name
Definition: detect.h:686
SupportFastPatternForSigMatchList
void SupportFastPatternForSigMatchList(int list_id, int priority)
Lets one add a sm list id to be searched for potential fp supported keywords later.
Definition: detect-fast-pattern.c:129
DetectEngineFrameInspectionEngine::next
struct DetectEngineFrameInspectionEngine * next
Definition: detect.h:524
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:230
DetectEnginePktInspectionEngine::next
struct DetectEnginePktInspectionEngine * next
Definition: detect.h:497
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
PatternMatchDefaultMatcher
uint8_t PatternMatchDefaultMatcher(void)
Function to return the multi pattern matcher algorithm to be used by the engine, based on the mpm-alg...
Definition: detect-engine-mpm.c:854
FatalError
#define FatalError(...)
Definition: util-debug.h:502
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:849
DetectMpmPreparePktMpms
int DetectMpmPreparePktMpms(DetectEngineCtx *de_ctx)
initialize mpm contexts for applayer buffers that are in "single or "shared" mode.
Definition: detect-engine-mpm.c:680
TransformData_::transform
int transform
Definition: detect.h:404
DetectEngineBufferTypeSupportsMpm
void DetectEngineBufferTypeSupportsMpm(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1233
util-validate.h
detect-flow.h
DetectEngineCtx_::app_mpms_list_cnt
uint32_t app_mpms_list_cnt
Definition: detect.h:989
DetectBufferTypeSupportsTransformations
void DetectBufferTypeSupportsTransformations(const char *name)
Definition: detect-engine.c:1081
MPM_PATTERN_FLAG_ENDSWITH
#define MPM_PATTERN_FLAG_ENDSWITH
Definition: util-mpm.h:140
builtin_mpms
const char * builtin_mpms[]
Definition: detect-engine-mpm.c:66
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:591
g_skip_prefilter
int g_skip_prefilter
Definition: detect-engine-mpm.c:1072
DetectEngineCtx_::mpm_hash_table
HashListTable * mpm_hash_table
Definition: detect.h:876
MPMB_UDP_TC
@ MPMB_UDP_TC
Definition: detect.h:1342
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:150
DetectBufferMpmRegistry_::id
int id
Definition: detect.h:692
SigMatchListSMBelongsTo
int SigMatchListSMBelongsTo(const Signature *s, const SigMatch *key_sm)
Definition: detect-parse.c:805
SCFree
#define SCFree(p)
Definition: util-mem.h:61
MPM_CTX_FACTORY_UNIQUE_CONTEXT
#define MPM_CTX_FACTORY_UNIQUE_CONTEXT
Definition: util-mpm.h:113
DetectMpmPrepareBuiltinMpms
int DetectMpmPrepareBuiltinMpms(DetectEngineCtx *de_ctx)
initialize mpm contexts for builtin buffers that are in "single or "shared" mode.
Definition: detect-engine-mpm.c:739
Signature_::id
uint32_t id
Definition: detect.h:636
DETECT_CONTENT_OFFSET
#define DETECT_CONTENT_OFFSET
Definition: detect-content.h:32
HashListTableBucket_
Definition: util-hashlist.h:28
DetectBufferMpmRegistry_::PrefilterRegisterWithListId
int(* PrefilterRegisterWithListId)(struct DetectEngineCtx_ *de_ctx, struct SigGroupHead_ *sgh, MpmCtx *mpm_ctx, const struct DetectBufferMpmRegistry_ *mpm_reg, int list_id)
Definition: detect.h:696
detect-tcphdr.h
DETECT_CONTENT_MPM
#define DETECT_CONTENT_MPM
Definition: detect-content.h:61
detect-engine-iponly.h
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:528
Signature_
Signature container.
Definition: detect.h:601
SigMatch_
a single match condition for a signature
Definition: detect.h:349
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
MPMB_TCP_PKT_TS
@ MPMB_TCP_PKT_TS
Definition: detect.h:1337
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
app-layer-protos.h
DetectMpmInitializeAppMpms
void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:236
DetectEngineTransforms::cnt
int cnt
Definition: detect.h:410
suricata.h
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:858
DETECT_BUFFER_MPM_TYPE_SIZE
@ DETECT_BUFFER_MPM_TYPE_SIZE
Definition: detect.h:681
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DetectBufferInstance::tc
struct SidsArray tc
Definition: detect-engine-mpm.c:1934
DetectEngineCtx_::buffer_type_id
uint32_t buffer_type_id
Definition: detect.h:987
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:843
MpmCtx_
Definition: util-mpm.h:88
SGH_PROTO
#define SGH_PROTO(sgh, p)
Definition: detect-engine-mpm.c:1004
DETECT_CONTENT_REPLACE
#define DETECT_CONTENT_REPLACE
Definition: detect-content.h:51
util-misc.h
flow.h
MpmCtx_::flags
uint8_t flags
Definition: util-mpm.h:92
DetectPktMpmRegisterByParentId
void DetectPktMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id, DetectEngineTransforms *transforms)
copy a mpm engine from parent_id, add in transforms
Definition: detect-engine-mpm.c:590
DETECT_CONTENT_FAST_PATTERN
#define DETECT_CONTENT_FAST_PATTERN
Definition: detect-content.h:34
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectFrameMpmRegister
void DetectFrameMpmRegister(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id), AppProto alproto, uint8_t type)
register a MPM engine
Definition: detect-engine-mpm.c:311
util-enum.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
DetectBufferInstance
struct DetectBufferInstance DetectBufferInstance
SidsArray::active
bool active
Definition: detect-engine-mpm.c:1768
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:592
MpmAddPatternCI
int MpmAddPatternCI(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:259
flow-var.h
SCMemcmp
#define SCMemcmp(a, b, c)
Definition: util-memcmp.h:290
DetectMpmPrepareAppMpms
int DetectMpmPrepareAppMpms(DetectEngineCtx *de_ctx)
initialize mpm contexts for applayer buffers that are in "single or "shared" mode.
Definition: detect-engine-mpm.c:286
DetectEngineCtx_::fp_support_smlist_list
SCFPSupportSMList * fp_support_smlist_list
Definition: detect.h:1012
MpmStore_::sgh_mpm_context
int32_t sgh_mpm_context
Definition: detect.h:1354
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
SIG_FLAG_PREFILTER
#define SIG_FLAG_PREFILTER
Definition: detect.h:274
PatternMatchThreadDestroy
void PatternMatchThreadDestroy(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
Definition: detect-engine-mpm.c:906
MpmStore_::buffer
enum MpmBuiltinBuffers buffer
Definition: detect.h:1352
RetrieveFPForSig
void RetrieveFPForSig(const DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine-mpm.c:1074
StringHashDjb2
uint32_t StringHashDjb2(const uint8_t *data, uint32_t datalen)
Definition: util-hash-string.c:22
PrefilterPktStreamRegister
int PrefilterPktStreamRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx)
Definition: detect-engine-payload.c:109
DETECT_CONTENT_OFFSET_VAR
#define DETECT_CONTENT_OFFSET_VAR
Definition: detect-content.h:45
SigGroupHeadInitData_::frame_mpms
MpmCtx ** frame_mpms
Definition: detect.h:1442
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:687
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:250
DetectEngineFrameInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:523