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(de_ctx->mpm_cfg, 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(de_ctx->mpm_cfg, 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(de_ctx->mpm_cfg, 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(de_ctx->mpm_cfg, mpm_ctx);
748  }
750  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
751  r |= mpm_table[de_ctx->mpm_matcher].Prepare(de_ctx->mpm_cfg, mpm_ctx);
752  }
753  }
754 
757  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
758  r |= mpm_table[de_ctx->mpm_matcher].Prepare(de_ctx->mpm_cfg, mpm_ctx);
759  }
761  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
762  r |= mpm_table[de_ctx->mpm_matcher].Prepare(de_ctx->mpm_cfg, mpm_ctx);
763  }
764  }
765 
768  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
769  r |= mpm_table[de_ctx->mpm_matcher].Prepare(de_ctx->mpm_cfg, mpm_ctx);
770  }
771  }
772 
775  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
776  r |= mpm_table[de_ctx->mpm_matcher].Prepare(de_ctx->mpm_cfg, mpm_ctx);
777  }
779  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
780  r |= mpm_table[de_ctx->mpm_matcher].Prepare(de_ctx->mpm_cfg, 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  // a buffer with absent keyword cannot be used as fast_pattern
1129  if (sm->type == DETECT_ABSENT)
1130  break;
1131  if (sm->type != DETECT_CONTENT)
1132  continue;
1133 
1134  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1135  /* fast_pattern set in rule, so using this pattern */
1136  if ((cd->flags & DETECT_CONTENT_FAST_PATTERN)) {
1137  SetMpm(s, sm, list_id);
1138  return;
1139  }
1140 
1141  if (cd->flags & DETECT_CONTENT_NEGATED) {
1142  neg_sm_list[list_id] = 1;
1143  neg_sm_list_cnt++;
1144  } else {
1145  pos_sm_list[list_id] = 1;
1146  pos_sm_list_cnt++;
1147  SCLogDebug("pos added for %d", list_id);
1148  }
1149  }
1150  SCLogDebug("ok");
1151  }
1152 
1153  SCLogDebug("neg_sm_list_cnt %d pos_sm_list_cnt %d", neg_sm_list_cnt, pos_sm_list_cnt);
1154 
1155  /* prefer normal not-negated over negated */
1156  int *curr_sm_list = NULL;
1157  int skip_negated_content = 1;
1158  if (pos_sm_list_cnt > 0) {
1159  curr_sm_list = pos_sm_list;
1160  } else if (neg_sm_list_cnt > 0) {
1161  curr_sm_list = neg_sm_list;
1162  skip_negated_content = 0;
1163  } else {
1164  return;
1165  }
1166 
1167  int final_sm_list[nlists];
1168  memset(&final_sm_list, 0, (nlists * sizeof(int)));
1169 
1170  int count_final_sm_list = 0;
1171  int priority;
1172 
1174  while (tmp != NULL) {
1175  for (priority = tmp->priority;
1176  tmp != NULL && priority == tmp->priority;
1177  tmp = tmp->next)
1178  {
1179  SCLogDebug("tmp->list_id %d tmp->priority %d", tmp->list_id, tmp->priority);
1180  if (tmp->list_id >= nlists)
1181  continue;
1182  if (curr_sm_list[tmp->list_id] == 0)
1183  continue;
1184  final_sm_list[count_final_sm_list++] = tmp->list_id;
1185  SCLogDebug("tmp->list_id %d", tmp->list_id);
1186  }
1187  if (count_final_sm_list != 0)
1188  break;
1189  }
1190 
1191  BUG_ON(count_final_sm_list == 0);
1192  SCLogDebug("count_final_sm_list %d skip_negated_content %d", count_final_sm_list,
1193  skip_negated_content);
1194 
1195  uint16_t max_len = 0;
1196  for (int i = 0; i < count_final_sm_list; i++) {
1197  SCLogDebug("i %d final_sm_list[i] %d", i, final_sm_list[i]);
1198 
1199  if (final_sm_list[i] == DETECT_SM_LIST_PMATCH) {
1200  for (SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH]; sm != NULL;
1201  sm = sm->next) {
1202  if (sm->type != DETECT_CONTENT)
1203  continue;
1204 
1205  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1206  /* skip_negated_content is only set if there's absolutely no
1207  * non-negated content present in the sig */
1208  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
1209  continue;
1210  max_len = MAX(max_len, cd->content_len);
1211  }
1212  } else {
1213  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1214  const int list_id = s->init_data->buffers[x].id;
1215  if (final_sm_list[i] == list_id) {
1216  SCLogDebug("%u: list_id %d: %s", s->id, list_id,
1218 
1219  for (SigMatch *sm = s->init_data->buffers[x].head; sm != NULL; sm = sm->next) {
1220  if (sm->type != DETECT_CONTENT)
1221  continue;
1222 
1223  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1224  /* skip_negated_content is only set if there's absolutely no
1225  * non-negated content present in the sig */
1226  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
1227  continue;
1228  max_len = MAX(max_len, cd->content_len);
1229  }
1230  }
1231  }
1232  }
1233  }
1234 
1235  SigMatch *mpm_sm = NULL;
1236  int mpm_sm_list = -1;
1237  for (int i = 0; i < count_final_sm_list; i++) {
1238  SCLogDebug("i %d", i);
1239  if (final_sm_list[i] == DETECT_SM_LIST_PMATCH) {
1240  /* GetMpmForList may keep `mpm_sm` the same, so track if it changed */
1241  SigMatch *prev_mpm_sm = mpm_sm;
1242  mpm_sm = GetMpmForList(s, s->init_data->smlists[DETECT_SM_LIST_PMATCH], mpm_sm, max_len,
1243  skip_negated_content);
1244  if (mpm_sm != prev_mpm_sm) {
1245  mpm_sm_list = final_sm_list[i];
1246  }
1247  } else {
1248  SCLogDebug(
1249  "%u: %s", s->id, DetectEngineBufferTypeGetNameById(de_ctx, final_sm_list[i]));
1250  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1251  const int list_id = s->init_data->buffers[x].id;
1252  if (final_sm_list[i] == list_id) {
1253  SCLogDebug("%u: list_id %d: %s", s->id, list_id,
1255  /* GetMpmForList may keep `mpm_sm` the same, so track if it changed */
1256  SigMatch *prev_mpm_sm = mpm_sm;
1257  mpm_sm = GetMpmForList(s, s->init_data->buffers[x].head, mpm_sm, max_len,
1258  skip_negated_content);
1259  SCLogDebug("mpm_sm %p from %p", mpm_sm, s->init_data->buffers[x].head);
1260  if (mpm_sm != prev_mpm_sm) {
1261  mpm_sm_list = list_id;
1262  }
1263  }
1264  }
1265  }
1266  }
1267 
1268 #ifdef DEBUG
1269  if (mpm_sm != NULL) {
1270  BUG_ON(mpm_sm_list == -1);
1271  int check_list = SigMatchListSMBelongsTo(s, mpm_sm);
1272  BUG_ON(check_list != mpm_sm_list);
1273  }
1274 #endif
1275  /* assign to signature */
1276  SetMpm(s, mpm_sm, mpm_sm_list);
1277 }
1278 
1279 /** \internal
1280  * \brief The hash function for MpmStore
1281  *
1282  * \param ht Pointer to the hash table.
1283  * \param data Pointer to the MpmStore.
1284  * \param datalen Not used in our case.
1285  *
1286  * \retval hash The generated hash value.
1287  */
1288 static uint32_t MpmStoreHashFunc(HashListTable *ht, void *data, uint16_t datalen)
1289 {
1290  const MpmStore *ms = (MpmStore *)data;
1291  uint32_t hash = ms->alproto;
1292 
1293  for (uint32_t b = 0; b < ms->sid_array_size; b++)
1294  hash += ms->sid_array[b];
1295 
1296  return hash % ht->array_size;
1297 }
1298 
1299 /**
1300  * \brief The Compare function for MpmStore
1301  *
1302  * \param data1 Pointer to the first MpmStore.
1303  * \param len1 Not used.
1304  * \param data2 Pointer to the second MpmStore.
1305  * \param len2 Not used.
1306  *
1307  * \retval 1 If the 2 MpmStores sent as args match.
1308  * \retval 0 If the 2 MpmStores sent as args do not match.
1309  */
1310 static char MpmStoreCompareFunc(void *data1, uint16_t len1, void *data2,
1311  uint16_t len2)
1312 {
1313  const MpmStore *ms1 = (MpmStore *)data1;
1314  const MpmStore *ms2 = (MpmStore *)data2;
1315 
1316  if (ms1->alproto != ms2->alproto)
1317  return 0;
1318 
1319  if (ms1->sid_array_size != ms2->sid_array_size)
1320  return 0;
1321 
1322  if (ms1->buffer != ms2->buffer)
1323  return 0;
1324 
1325  if (ms1->direction != ms2->direction)
1326  return 0;
1327 
1328  if (ms1->sm_list != ms2->sm_list)
1329  return 0;
1330 
1331  if (SCMemcmp(ms1->sid_array, ms2->sid_array,
1332  ms1->sid_array_size) != 0)
1333  {
1334  return 0;
1335  }
1336 
1337  return 1;
1338 }
1339 
1340 static void MpmStoreFreeFunc(void *ptr)
1341 {
1342  MpmStore *ms = ptr;
1343  if (ms != NULL) {
1344  if (ms->mpm_ctx != NULL && !(ms->mpm_ctx->flags & MPMCTX_FLAGS_GLOBAL))
1345  {
1346  SCLogDebug("destroying mpm_ctx %p", ms->mpm_ctx);
1348  SCFree(ms->mpm_ctx);
1349  }
1350  ms->mpm_ctx = NULL;
1351 
1352  SCFree(ms->sid_array);
1353  SCFree(ms);
1354  }
1355 }
1356 
1357 /**
1358  * \brief Initializes the MpmStore mpm hash table to be used by the detection
1359  * engine context.
1360  *
1361  * \param de_ctx Pointer to the detection engine context.
1362  *
1363  * \retval 0 On success.
1364  * \retval -1 On failure.
1365  */
1367 {
1369  MpmStoreHashFunc,
1370  MpmStoreCompareFunc,
1371  MpmStoreFreeFunc);
1372  if (de_ctx->mpm_hash_table == NULL)
1373  goto error;
1374 
1375  return 0;
1376 
1377 error:
1378  return -1;
1379 }
1380 
1381 /**
1382  * \brief Adds a MpmStore to the detection engine context MpmStore
1383  *
1384  * \param de_ctx Pointer to the detection engine context.
1385  * \param sgh Pointer to the MpmStore.
1386  *
1387  * \retval ret 0 on Successfully adding the argument sgh; -1 on failure.
1388  */
1389 static int MpmStoreAdd(DetectEngineCtx *de_ctx, MpmStore *s)
1390 {
1391  int ret = HashListTableAdd(de_ctx->mpm_hash_table, (void *)s, 0);
1392  return ret;
1393 }
1394 
1395 /**
1396  * \brief Used to lookup a MpmStore from the MpmStore
1397  *
1398  * \param de_ctx Pointer to the detection engine context.
1399  * \param sgh Pointer to the MpmStore.
1400  *
1401  * \retval rsgh On success a pointer to the MpmStore if the MpmStore is
1402  * found in the hash table; NULL on failure.
1403  */
1404 static MpmStore *MpmStoreLookup(DetectEngineCtx *de_ctx, MpmStore *s)
1405 {
1407  (void *)s, 0);
1408  return rs;
1409 }
1410 
1411 static const DetectBufferMpmRegistry *GetByMpmStore(
1412  const DetectEngineCtx *de_ctx, const MpmStore *ms)
1413 {
1415  while (am != NULL) {
1416  if (ms->sm_list == am->sm_list &&
1417  ms->direction == am->direction) {
1418  return am;
1419  }
1420  am = am->next;
1421  }
1422  am = de_ctx->pkt_mpms_list;
1423  while (am != NULL) {
1424  if (ms->sm_list == am->sm_list) {
1425  return am;
1426  }
1427  am = am->next;
1428  }
1429  return NULL;
1430 }
1431 
1433 {
1434  HashListTableBucket *htb = NULL;
1435 
1436  uint32_t stats[MPMB_MAX] = {0};
1437  int app_mpms_cnt = de_ctx->buffer_type_id;
1438  uint32_t appstats[app_mpms_cnt + 1]; // +1 to silence scan-build
1439  memset(&appstats, 0x00, sizeof(appstats));
1440  int pkt_mpms_cnt = de_ctx->buffer_type_id;
1441  uint32_t pktstats[pkt_mpms_cnt + 1]; // +1 to silence scan-build
1442  memset(&pktstats, 0x00, sizeof(pktstats));
1443  int frame_mpms_cnt = de_ctx->buffer_type_id;
1444  uint32_t framestats[frame_mpms_cnt + 1]; // +1 to silence scan-build
1445  memset(&framestats, 0x00, sizeof(framestats));
1446 
1448  htb != NULL;
1449  htb = HashListTableGetListNext(htb))
1450  {
1451  const MpmStore *ms = (MpmStore *)HashListTableGetListData(htb);
1452  if (ms == NULL || ms->mpm_ctx == NULL) {
1453  continue;
1454  }
1455  if (ms->buffer < MPMB_MAX)
1456  stats[ms->buffer]++;
1457  else if (ms->sm_list != DETECT_SM_LIST_PMATCH) {
1458  const DetectBufferMpmRegistry *am = GetByMpmStore(de_ctx, ms);
1459  if (am != NULL) {
1460  switch (am->type) {
1462  SCLogDebug("%s: %u patterns. Min %u, Max %u. Ctx %p",
1463  am->name,
1464  ms->mpm_ctx->pattern_cnt,
1465  ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1466  ms->mpm_ctx);
1467  pktstats[am->sm_list]++;
1468  break;
1470  SCLogDebug("%s %s %s: %u patterns. Min %u, Max %u. Ctx %p",
1471  AppProtoToString(ms->alproto), am->name,
1472  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1473  ms->mpm_ctx->pattern_cnt, ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1474  ms->mpm_ctx);
1475  appstats[am->sm_list]++;
1476  break;
1478  SCLogDebug("%s: %u patterns. Min %u, Max %u. Ctx %p", am->name,
1479  ms->mpm_ctx->pattern_cnt, ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1480  ms->mpm_ctx);
1481  framestats[am->sm_list]++;
1482  break;
1484  break;
1485  }
1486  }
1487  }
1488  }
1489 
1490  if (!(de_ctx->flags & DE_QUIET)) {
1491  for (int x = 0; x < MPMB_MAX; x++) {
1492  SCLogPerf("Builtin MPM \"%s\": %u", builtin_mpms[x], stats[x]);
1493  }
1495  while (am != NULL) {
1496  if (appstats[am->sm_list] > 0) {
1497  const char *name = am->name;
1498  const char *direction = am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient";
1499  SCLogPerf("AppLayer MPM \"%s %s (%s)\": %u", direction, name,
1500  AppProtoToString(am->app_v2.alproto), appstats[am->sm_list]);
1501  }
1502  am = am->next;
1503  }
1505  while (pm != NULL) {
1506  if (pktstats[pm->sm_list] > 0) {
1507  const char *name = pm->name;
1508  SCLogPerf("Pkt MPM \"%s\": %u", name, pktstats[pm->sm_list]);
1509  }
1510  pm = pm->next;
1511  }
1513  while (um != NULL) {
1514  if (framestats[um->sm_list] > 0) {
1515  const char *name = um->name;
1516  SCLogPerf("Frame MPM \"%s\": %u", name, framestats[um->sm_list]);
1517  }
1518  um = um->next;
1519  }
1520  }
1521 }
1522 
1523 /**
1524  * \brief Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by
1525  * MpmStoreInit() function.
1526  *
1527  * \param de_ctx Pointer to the detection engine context.
1528  */
1530 {
1531  if (de_ctx->mpm_hash_table == NULL)
1532  return;
1533 
1535  de_ctx->mpm_hash_table = NULL;
1536 }
1537 
1538 static void MpmStoreSetup(const DetectEngineCtx *de_ctx, MpmStore *ms)
1539 {
1540  const Signature *s = NULL;
1541  uint32_t sig;
1542  int dir = 0;
1543 
1544  if (ms->buffer != MPMB_MAX) {
1546 
1547  switch (ms->buffer) {
1548  /* TS is 1 */
1549  case MPMB_TCP_PKT_TS:
1550  case MPMB_TCP_STREAM_TS:
1551  case MPMB_UDP_TS:
1552  dir = 1;
1553  break;
1554 
1555  /* TC is 0 */
1556  default:
1557  case MPMB_UDP_TC:
1558  case MPMB_TCP_STREAM_TC:
1559  case MPMB_TCP_PKT_TC:
1560  case MPMB_OTHERIP: /**< use 0 for other */
1561  dir = 0;
1562  break;
1563  }
1564  } else {
1566 
1567  if (ms->direction == SIG_FLAG_TOSERVER)
1568  dir = 1;
1569  else
1570  dir = 0;
1571  }
1572 
1574  if (ms->mpm_ctx == NULL) {
1575  return;
1576  }
1577 
1579 
1582 
1583  const bool mpm_supports_endswith =
1585 
1586  /* add the patterns */
1587  for (sig = 0; sig < (ms->sid_array_size * 8); sig++) {
1588  if (ms->sid_array[sig / 8] & (1 << (sig % 8))) {
1589  s = de_ctx->sig_array[sig];
1590  DEBUG_VALIDATE_BUG_ON(s == NULL);
1591  if (s == NULL)
1592  continue;
1593 
1594  SCLogDebug("%p: direction %d adding %u", ms, ms->direction, s->id);
1595 
1597 
1598  int skip = 0;
1599  /* negated logic: if mpm match can't be used to be sure about this
1600  * pattern, we have to inspect the rule fully regardless of mpm
1601  * match. So in this case there is no point of adding it at all.
1602  * The non-mpm list entry for the sig will make sure the sig is
1603  * inspected. */
1604  if ((cd->flags & DETECT_CONTENT_NEGATED) &&
1606  {
1607  skip = 1;
1608  SCLogDebug("not adding negated mpm as it's not 'single'");
1609  }
1610 
1611  if (!skip) {
1612  uint8_t flags = 0;
1613  if ((cd->flags & DETECT_CONTENT_ENDS_WITH) && mpm_supports_endswith)
1615  PopulateMpmHelperAddPattern(ms->mpm_ctx, cd, s, flags,
1617  }
1618  }
1619  }
1620 
1621  if (ms->mpm_ctx->pattern_cnt == 0) {
1623  ms->mpm_ctx = NULL;
1624  } else {
1626  if (mpm_table[ms->mpm_ctx->mpm_type].Prepare != NULL) {
1628  }
1629  }
1630  }
1631 }
1632 
1633 
1634 /** \brief Get MpmStore for a built-in buffer type
1635  *
1636  */
1638  enum MpmBuiltinBuffers buf)
1639 {
1640  const Signature *s = NULL;
1641  uint32_t sig;
1642  uint32_t cnt = 0;
1643  int direction = 0;
1644  uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
1645  uint8_t sids_array[max_sid];
1646  memset(sids_array, 0x00, max_sid);
1647  int sgh_mpm_context = 0;
1648  int sm_list = DETECT_SM_LIST_PMATCH;
1649 
1650  switch (buf) {
1651  case MPMB_TCP_PKT_TS:
1652  case MPMB_TCP_PKT_TC:
1653  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_tcp_packet;
1654  break;
1655  case MPMB_TCP_STREAM_TS:
1656  case MPMB_TCP_STREAM_TC:
1657  sgh_mpm_context = de_ctx->sgh_mpm_context_stream;
1658  break;
1659  case MPMB_UDP_TS:
1660  case MPMB_UDP_TC:
1661  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_udp_packet;
1662  break;
1663  case MPMB_OTHERIP:
1664  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_other_packet;
1665  break;
1666  default:
1667  break;
1668  }
1669 
1670  switch(buf) {
1671  case MPMB_TCP_PKT_TS:
1672  case MPMB_TCP_STREAM_TS:
1673  case MPMB_UDP_TS:
1674  direction = SIG_FLAG_TOSERVER;
1675  break;
1676 
1677  case MPMB_TCP_PKT_TC:
1678  case MPMB_TCP_STREAM_TC:
1679  case MPMB_UDP_TC:
1680  direction = SIG_FLAG_TOCLIENT;
1681  break;
1682 
1683  case MPMB_OTHERIP:
1684  direction = (SIG_FLAG_TOCLIENT|SIG_FLAG_TOSERVER);
1685  break;
1686 
1687  case MPMB_MAX:
1688  BUG_ON(1);
1689  break;
1690  }
1691 
1692  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
1693  s = sgh->init->match_array[sig];
1694  if (s == NULL)
1695  continue;
1696 
1697  if (s->init_data->mpm_sm == NULL)
1698  continue;
1699 
1700  int list = s->init_data->mpm_sm_list;
1701  if (list < 0)
1702  continue;
1703 
1704  if (list != DETECT_SM_LIST_PMATCH)
1705  continue;
1706 
1707  switch (buf) {
1708  case MPMB_TCP_PKT_TS:
1709  case MPMB_TCP_PKT_TC:
1710  if (SignatureHasPacketContent(s) == 1)
1711  {
1712  sids_array[s->num / 8] |= 1 << (s->num % 8);
1713  cnt++;
1714  }
1715  break;
1716  case MPMB_TCP_STREAM_TS:
1717  case MPMB_TCP_STREAM_TC:
1718  if (SignatureHasStreamContent(s) == 1)
1719  {
1720  sids_array[s->num / 8] |= 1 << (s->num % 8);
1721  cnt++;
1722  }
1723  break;
1724  case MPMB_UDP_TS:
1725  case MPMB_UDP_TC:
1726  sids_array[s->num / 8] |= 1 << (s->num % 8);
1727  cnt++;
1728  break;
1729  case MPMB_OTHERIP:
1730  sids_array[s->num / 8] |= 1 << (s->num % 8);
1731  cnt++;
1732  break;
1733  default:
1734  break;
1735  }
1736  }
1737 
1738  if (cnt == 0)
1739  return NULL;
1740 
1741  MpmStore lookup = { sids_array, max_sid, direction, buf, sm_list, 0, 0, NULL };
1742 
1743  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1744  if (result == NULL) {
1745  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1746  if (copy == NULL)
1747  return NULL;
1748  uint8_t *sids = SCCalloc(1, max_sid);
1749  if (sids == NULL) {
1750  SCFree(copy);
1751  return NULL;
1752  }
1753 
1754  memcpy(sids, sids_array, max_sid);
1755  copy->sid_array = sids;
1756  copy->sid_array_size = max_sid;
1757  copy->buffer = buf;
1758  copy->direction = direction;
1759  copy->sm_list = sm_list;
1760  copy->sgh_mpm_context = sgh_mpm_context;
1761 
1762  MpmStoreSetup(de_ctx, copy);
1763  MpmStoreAdd(de_ctx, copy);
1764  return copy;
1765  } else {
1766  return result;
1767  }
1768 }
1769 
1770 struct SidsArray {
1771  uint8_t *sids_array;
1773  /* indicates this has an active engine */
1774  bool active;
1775 
1777 };
1778 
1779 static MpmStore *MpmStorePrepareBufferAppLayer(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1780  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1781 {
1782  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1783  return NULL;
1784 
1785  SCLogDebug("handling %s direction %s for list %d", am->name,
1786  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1787  am->sm_list);
1788 
1789  MpmStore lookup = { sa->sids_array, sa->sids_array_size, am->direction, MPMB_MAX, am->sm_list,
1790  0, am->app_v2.alproto, NULL };
1791  SCLogDebug("am->direction %d am->sm_list %d sgh_mpm_context %d", am->direction, am->sm_list,
1792  am->sgh_mpm_context);
1793 
1794  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1795  if (result == NULL) {
1796  SCLogDebug("new unique mpm for %s %s", am->name,
1797  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient");
1798 
1799  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1800  if (copy == NULL)
1801  return NULL;
1802  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1803  if (sids == NULL) {
1804  SCFree(copy);
1805  return NULL;
1806  }
1807 
1808  memcpy(sids, sa->sids_array, sa->sids_array_size);
1809  copy->sid_array = sids;
1810  copy->sid_array_size = sa->sids_array_size;
1811  copy->buffer = MPMB_MAX;
1812  copy->direction = am->direction;
1813  copy->sm_list = am->sm_list;
1814  copy->sgh_mpm_context = am->sgh_mpm_context;
1815  copy->alproto = am->app_v2.alproto;
1816 
1817  MpmStoreSetup(de_ctx, copy);
1818  MpmStoreAdd(de_ctx, copy);
1819  return copy;
1820  } else {
1821  SCLogDebug("using existing mpm %p", result);
1822  return result;
1823  }
1824  return NULL;
1825 }
1826 
1827 static MpmStore *MpmStorePrepareBufferPkt(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1828  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1829 {
1830  SCLogDebug("handling %s for list %d", am->name,
1831  am->sm_list);
1832 
1833  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1834  return NULL;
1835 
1837  MPMB_MAX, am->sm_list, 0, 0, NULL };
1838  SCLogDebug("am->sm_list %d", am->sm_list);
1839 
1840  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1841  if (result == NULL) {
1842  SCLogDebug("new unique mpm for %s", am->name);
1843 
1844  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1845  if (copy == NULL)
1846  return NULL;
1847  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1848  if (sids == NULL) {
1849  SCFree(copy);
1850  return NULL;
1851  }
1852 
1853  memcpy(sids, sa->sids_array, sa->sids_array_size);
1854  copy->sid_array = sids;
1855  copy->sid_array_size = sa->sids_array_size;
1856  copy->buffer = MPMB_MAX;
1858  copy->sm_list = am->sm_list;
1859  copy->sgh_mpm_context = am->sgh_mpm_context;
1860 
1861  MpmStoreSetup(de_ctx, copy);
1862  MpmStoreAdd(de_ctx, copy);
1863  return copy;
1864  } else {
1865  SCLogDebug("using existing mpm %p", result);
1866  return result;
1867  }
1868  return NULL;
1869 }
1870 
1871 static MpmStore *MpmStorePrepareBufferFrame(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1872  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1873 {
1874  SCLogDebug("handling %s for list %d", am->name, am->sm_list);
1875 
1876  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1877  return NULL;
1878 
1879  MpmStore lookup = { sa->sids_array, sa->sids_array_size, am->direction, MPMB_MAX, am->sm_list,
1880  0, am->frame_v1.alproto, NULL };
1881  SCLogDebug("am->sm_list %d", am->sm_list);
1882 
1883  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1884  if (result == NULL) {
1885  SCLogDebug("new unique mpm for %s", am->name);
1886 
1887  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1888  if (copy == NULL)
1889  return NULL;
1890  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1891  if (sids == NULL) {
1892  SCFree(copy);
1893  return NULL;
1894  }
1895 
1896  memcpy(sids, sa->sids_array, sa->sids_array_size);
1897  copy->sid_array = sids;
1898  copy->sid_array_size = sa->sids_array_size;
1899  copy->buffer = MPMB_MAX;
1900  copy->direction = am->direction;
1901  copy->sm_list = am->sm_list;
1902  copy->sgh_mpm_context = am->sgh_mpm_context;
1903  copy->alproto = am->frame_v1.alproto;
1904 
1905  MpmStoreSetup(de_ctx, copy);
1906  MpmStoreAdd(de_ctx, copy);
1907  return copy;
1908  } else {
1909  SCLogDebug("using existing mpm %p", result);
1910  return result;
1911  }
1912  return NULL;
1913 }
1914 
1915 static void SetRawReassemblyFlag(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
1916 {
1917  const Signature *s = NULL;
1918  uint32_t sig;
1919 
1920  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
1921  s = sgh->init->match_array[sig];
1922  if (s == NULL)
1923  continue;
1924 
1925  if (SignatureHasStreamContent(s) == 1) {
1927  SCLogDebug("rule group %p has SIG_GROUP_HEAD_HAVERAWSTREAM set", sgh);
1928  return;
1929  }
1930  }
1931  SCLogDebug("rule group %p does NOT have SIG_GROUP_HEAD_HAVERAWSTREAM set", sgh);
1932 }
1933 
1934 typedef struct DetectBufferInstance {
1935  // key
1936  int list;
1938 
1939  struct SidsArray ts;
1940  struct SidsArray tc;
1942 
1943 static uint32_t DetectBufferInstanceHashFunc(HashListTable *ht, void *data, uint16_t datalen)
1944 {
1945  const DetectBufferInstance *ms = (const DetectBufferInstance *)data;
1946  uint32_t hash = ms->list + ms->alproto;
1947  return hash % ht->array_size;
1948 }
1949 
1950 static char DetectBufferInstanceCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
1951 {
1952  const DetectBufferInstance *ms1 = (DetectBufferInstance *)data1;
1953  const DetectBufferInstance *ms2 = (DetectBufferInstance *)data2;
1954  return (ms1->list == ms2->list && ms1->alproto == ms2->alproto);
1955 }
1956 
1957 static void DetectBufferInstanceFreeFunc(void *ptr)
1958 {
1959  DetectBufferInstance *ms = ptr;
1960  if (ms->ts.sids_array != NULL)
1961  SCFree(ms->ts.sids_array);
1962  if (ms->tc.sids_array != NULL)
1963  SCFree(ms->tc.sids_array);
1964  SCFree(ms);
1965 }
1966 
1967 static HashListTable *DetectBufferInstanceInit(void)
1968 {
1969  return HashListTableInit(4096, DetectBufferInstanceHashFunc, DetectBufferInstanceCompareFunc,
1970  DetectBufferInstanceFreeFunc);
1971 }
1972 
1973 static void PrepareMpms(DetectEngineCtx *de_ctx, SigGroupHead *sh)
1974 {
1975  HashListTable *bufs = DetectBufferInstanceInit();
1976  BUG_ON(bufs == NULL);
1977 
1978  const int max_buffer_id = de_ctx->buffer_type_id + 1;
1979  const uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
1980 
1981  AppProto engines[max_buffer_id][g_alproto_max];
1982  memset(engines, 0, sizeof(engines));
1983  int engines_idx[max_buffer_id];
1984  memset(engines_idx, 0, sizeof(engines_idx));
1985  int types[max_buffer_id];
1986  memset(types, 0, sizeof(types));
1987 
1988  /* flag the list+directions we have engines for as active */
1989  for (DetectBufferMpmRegistry *a = de_ctx->pkt_mpms_list; a != NULL; a = a->next) {
1990  types[a->sm_list] = a->type;
1991 
1992  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = ALPROTO_UNKNOWN };
1993  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
1994  if (instance == NULL) {
1995  instance = SCCalloc(1, sizeof(*instance));
1996  BUG_ON(instance == NULL);
1997  instance->list = a->sm_list;
1998  instance->alproto = ALPROTO_UNKNOWN;
1999  HashListTableAdd(bufs, instance, 0);
2000  }
2001  instance->ts.active = true;
2002  instance->tc.active = true;
2003  }
2004  for (DetectBufferMpmRegistry *a = de_ctx->frame_mpms_list; a != NULL; a = a->next) {
2005  const bool add_ts = ((a->direction == SIG_FLAG_TOSERVER) && SGH_DIRECTION_TS(sh));
2006  const bool add_tc = ((a->direction == SIG_FLAG_TOCLIENT) && SGH_DIRECTION_TC(sh));
2007  if (add_ts || add_tc) {
2008  types[a->sm_list] = a->type;
2009  engines[a->sm_list][engines_idx[a->sm_list]++] = a->frame_v1.alproto;
2010 
2011  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->frame_v1.alproto };
2012  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2013  if (instance == NULL) {
2014  instance = SCCalloc(1, sizeof(*instance));
2015  BUG_ON(instance == NULL);
2016  instance->list = a->sm_list;
2017  instance->alproto = a->frame_v1.alproto;
2018  HashListTableAdd(bufs, instance, 0);
2019  }
2020  instance->ts.active |= add_ts;
2021  instance->tc.active |= add_tc;
2022  }
2023  }
2024  for (DetectBufferMpmRegistry *a = de_ctx->app_mpms_list; a != NULL; a = a->next) {
2025  const bool add_ts = ((a->direction == SIG_FLAG_TOSERVER) && SGH_DIRECTION_TS(sh));
2026  const bool add_tc = ((a->direction == SIG_FLAG_TOCLIENT) && SGH_DIRECTION_TC(sh));
2027  if (add_ts || add_tc) {
2028  types[a->sm_list] = a->type;
2029  engines[a->sm_list][engines_idx[a->sm_list]++] = a->app_v2.alproto;
2030 
2031  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->app_v2.alproto };
2032  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2033  if (instance == NULL) {
2034  instance = SCCalloc(1, sizeof(*instance));
2035  BUG_ON(instance == NULL);
2036  instance->list = a->sm_list;
2037  instance->alproto = a->app_v2.alproto;
2038  HashListTableAdd(bufs, instance, 0);
2039  }
2040  instance->ts.active |= add_ts;
2041  instance->tc.active |= add_tc;
2042  }
2043  }
2044 
2045  for (uint32_t sig = 0; sig < sh->init->sig_cnt; sig++) {
2046  const Signature *s = sh->init->match_array[sig];
2047  if (s == NULL)
2048  continue;
2049  if (s->init_data->mpm_sm == NULL)
2050  continue;
2051  const int list = s->init_data->mpm_sm_list;
2052  if (list < 0)
2053  continue;
2054  if (list == DETECT_SM_LIST_PMATCH)
2055  continue;
2056 
2057  switch (types[list]) {
2058  /* app engines are direction aware */
2061  for (int e = 0; e < engines_idx[list]; e++) {
2062  const AppProto alproto = engines[list][e];
2063  if (!(AppProtoEquals(s->alproto, alproto) || s->alproto == 0))
2064  continue;
2065 
2066  DetectBufferInstance lookup = { .list = list, .alproto = alproto };
2067  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2068  if (instance == NULL)
2069  continue;
2070  if (s->flags & SIG_FLAG_TOSERVER) {
2071  struct SidsArray *sa = &instance->ts;
2072  if (sa->active) {
2073  if (sa->sids_array == NULL) {
2074  sa->sids_array = SCCalloc(1, max_sid);
2075  sa->sids_array_size = max_sid;
2076  BUG_ON(sa->sids_array == NULL); // TODO
2077  }
2078  sa->sids_array[s->num / 8] |= 1 << (s->num % 8);
2079  SCLogDebug("instance %p: stored %u/%u ts", instance, s->id, s->num);
2080  }
2081  }
2082  if (s->flags & SIG_FLAG_TOCLIENT) {
2083  struct SidsArray *sa = &instance->tc;
2084  if (sa->active) {
2085  if (sa->sids_array == NULL) {
2086  sa->sids_array = SCCalloc(1, max_sid);
2087  sa->sids_array_size = max_sid;
2088  BUG_ON(sa->sids_array == NULL); // TODO
2089  }
2090  sa->sids_array[s->num / 8] |= 1 << (s->num % 8);
2091  SCLogDebug("instance %p: stored %u/%u tc", instance, s->id, s->num);
2092  }
2093  }
2094  }
2095  break;
2096  }
2097  /* pkt engines are directionless, so only use ts */
2099  DetectBufferInstance lookup = { .list = list, .alproto = ALPROTO_UNKNOWN };
2100  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2101  if (instance == NULL)
2102  continue;
2103  struct SidsArray *sa = &instance->ts;
2104  if (sa->active) {
2105  if (sa->sids_array == NULL) {
2106  sa->sids_array = SCCalloc(1, max_sid);
2107  sa->sids_array_size = max_sid;
2108  BUG_ON(sa->sids_array == NULL); // TODO
2109  }
2110  sa->sids_array[s->num / 8] |= 1 << (s->num % 8);
2111  }
2112  break;
2113  }
2114  default:
2115  abort();
2116  break;
2117  }
2118  }
2119 
2120  sh->init->app_mpms = SCCalloc(de_ctx->app_mpms_list_cnt, sizeof(MpmCtx *));
2121  BUG_ON(sh->init->app_mpms == NULL);
2122 
2123  sh->init->pkt_mpms = SCCalloc(de_ctx->pkt_mpms_list_cnt, sizeof(MpmCtx *));
2124  BUG_ON(sh->init->pkt_mpms == NULL);
2125 
2127  BUG_ON(sh->init->frame_mpms == NULL);
2128 
2129  for (DetectBufferMpmRegistry *a = de_ctx->pkt_mpms_list; a != NULL; a = a->next) {
2130  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = ALPROTO_UNKNOWN };
2131  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2132  if (instance == NULL) {
2133  continue;
2134  }
2135  struct SidsArray *sa = &instance->ts;
2136  if (!sa->active)
2137  continue;
2138 
2139  MpmStore *mpm_store = MpmStorePrepareBufferPkt(de_ctx, sh, a, sa);
2140  if (mpm_store != NULL) {
2141  sh->init->pkt_mpms[a->id] = mpm_store->mpm_ctx;
2142 
2143  SCLogDebug("a %p a->name %s a->reg->PrefilterRegisterWithListId %p "
2144  "mpm_store->mpm_ctx %p", a, a->name,
2145  a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2146 
2147  /* if we have just certain types of negated patterns,
2148  * mpm_ctx can be NULL */
2149  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2150  BUG_ON(a->PrefilterRegisterWithListId(de_ctx,
2151  sh, mpm_store->mpm_ctx,
2152  a, a->sm_list) != 0);
2153  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2154  }
2155  }
2156  }
2157  for (DetectBufferMpmRegistry *a = de_ctx->frame_mpms_list; a != NULL; a = a->next) {
2158  if ((a->direction == SIG_FLAG_TOSERVER && SGH_DIRECTION_TS(sh)) ||
2159  (a->direction == SIG_FLAG_TOCLIENT && SGH_DIRECTION_TC(sh))) {
2160  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->frame_v1.alproto };
2161  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2162  if (instance == NULL) {
2163  continue;
2164  }
2165  struct SidsArray *sa =
2166  (a->direction == SIG_FLAG_TOSERVER) ? &instance->ts : &instance->tc;
2167  if (!sa->active)
2168  continue;
2169 
2170  SCLogDebug("a %s direction %d PrefilterRegisterWithListId %p", a->name, a->direction,
2171  a->PrefilterRegisterWithListId);
2172  MpmStore *mpm_store = MpmStorePrepareBufferFrame(de_ctx, sh, a, sa);
2173  if (mpm_store != NULL) {
2174  sh->init->frame_mpms[a->id] = mpm_store->mpm_ctx;
2175 
2176  SCLogDebug("a %p a->name %s a->reg->PrefilterRegisterWithListId %p "
2177  "mpm_store->mpm_ctx %p",
2178  a, a->name, a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2179 
2180  /* if we have just certain types of negated patterns,
2181  * mpm_ctx can be NULL */
2182  SCLogDebug("mpm_store %p mpm_ctx %p", mpm_store, mpm_store->mpm_ctx);
2183  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2184  BUG_ON(a->PrefilterRegisterWithListId(
2185  de_ctx, sh, mpm_store->mpm_ctx, a, a->sm_list) != 0);
2186  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2187  }
2188  }
2189  }
2190  }
2191  for (DetectBufferMpmRegistry *a = de_ctx->app_mpms_list; a != NULL; a = a->next) {
2192  if ((a->direction == SIG_FLAG_TOSERVER && SGH_DIRECTION_TS(sh)) ||
2193  (a->direction == SIG_FLAG_TOCLIENT && SGH_DIRECTION_TC(sh))) {
2194 
2195  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->app_v2.alproto };
2196  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2197  if (instance == NULL) {
2198  continue;
2199  }
2200  struct SidsArray *sa =
2201  (a->direction == SIG_FLAG_TOSERVER) ? &instance->ts : &instance->tc;
2202  if (!sa->active)
2203  continue;
2204 
2205  MpmStore *mpm_store = MpmStorePrepareBufferAppLayer(de_ctx, sh, a, sa);
2206  if (mpm_store != NULL) {
2207  sh->init->app_mpms[a->id] = mpm_store->mpm_ctx;
2208 
2209  SCLogDebug("a %p a->name %s a->PrefilterRegisterWithListId %p "
2210  "mpm_store->mpm_ctx %p",
2211  a, a->name, a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2212 
2213  /* if we have just certain types of negated patterns,
2214  * mpm_ctx can be NULL */
2215  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2216  BUG_ON(a->PrefilterRegisterWithListId(
2217  de_ctx, sh, mpm_store->mpm_ctx, a, a->sm_list) != 0);
2218  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2219  }
2220  }
2221  }
2222  }
2223  HashListTableFree(bufs);
2224 }
2225 
2226 /** \brief Prepare the pattern matcher ctx in a sig group head.
2227  *
2228  */
2230 {
2231  MpmStore *mpm_store = NULL;
2232  if (SGH_PROTO(sh, IPPROTO_TCP)) {
2233  if (SGH_DIRECTION_TS(sh)) {
2234  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_PKT_TS);
2235  if (mpm_store != NULL) {
2236  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2237  }
2238 
2240  if (mpm_store != NULL) {
2241  PrefilterPktStreamRegister(de_ctx, sh, mpm_store->mpm_ctx);
2242  }
2243 
2244  SetRawReassemblyFlag(de_ctx, sh);
2245  }
2246  if (SGH_DIRECTION_TC(sh)) {
2247  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_PKT_TC);
2248  if (mpm_store != NULL) {
2249  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2250  }
2251 
2253  if (mpm_store != NULL) {
2254  PrefilterPktStreamRegister(de_ctx, sh, mpm_store->mpm_ctx);
2255  }
2256 
2257  SetRawReassemblyFlag(de_ctx, sh);
2258  }
2259  } else if (SGH_PROTO(sh, IPPROTO_UDP)) {
2260  if (SGH_DIRECTION_TS(sh)) {
2261  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_UDP_TS);
2262  if (mpm_store != NULL) {
2263  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2264  }
2265  }
2266  if (SGH_DIRECTION_TC(sh)) {
2267  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_UDP_TC);
2268  if (mpm_store != NULL) {
2269  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2270  }
2271  }
2272  } else {
2273  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_OTHERIP);
2274  if (mpm_store != NULL) {
2275  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2276  }
2277  }
2278 
2279  PrepareMpms(de_ctx, sh);
2280  return 0;
2281 }
2282 
2283 static inline uint32_t ContentFlagsForHash(const DetectContentData *cd)
2284 {
2287 }
2288 
2289 /** \internal
2290  * \brief The hash function for Pattern. Hashes pattern after chop is applied.
2291  *
2292  * \param ht Pointer to the hash table.
2293  * \param data Pointer to the Pattern.
2294  * \param datalen Not used in our case.
2295  *
2296  * \retval hash The generated hash value.
2297  */
2298 static uint32_t PatternChopHashFunc(HashListTable *ht, void *data, uint16_t datalen)
2299 {
2300  const DetectPatternTracker *p = (DetectPatternTracker *)data;
2301  uint32_t hash = p->sm_list + ContentFlagsForHash(p->cd);
2302  uint16_t content_len = p->cd->content_len;
2303  const uint8_t *content = p->cd->content;
2305  content += p->cd->fp_chop_offset;
2306  content_len = p->cd->fp_chop_len;
2307  }
2308  hash += StringHashDjb2(content, content_len);
2309  return hash % ht->array_size;
2310 }
2311 
2312 /** \internal
2313  * \brief The hash function for Pattern. Ignores chop.
2314  *
2315  * \param ht Pointer to the hash table.
2316  * \param data Pointer to the Pattern.
2317  * \param datalen Not used in our case.
2318  *
2319  * \retval hash The generated hash value.
2320  */
2321 static uint32_t PatternNoChopHashFunc(HashListTable *ht, void *data, uint16_t datalen)
2322 {
2323  const DetectPatternTracker *p = (DetectPatternTracker *)data;
2324  uint32_t hash = p->sm_list + ContentFlagsForHash(p->cd);
2325  hash += StringHashDjb2(p->cd->content, p->cd->content_len);
2326  return hash % ht->array_size;
2327 }
2328 
2329 /**
2330  * \brief The Compare function for Pattern. Compares patterns after chop is applied.
2331  *
2332  * \param data1 Pointer to the first Pattern.
2333  * \param len1 Not used.
2334  * \param data2 Pointer to the second Pattern.
2335  * \param len2 Not used.
2336  *
2337  * \retval 1 If the 2 Patterns sent as args match.
2338  * \retval 0 If the 2 Patterns sent as args do not match.
2339  */
2340 static char PatternChopCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
2341 {
2342  const DetectPatternTracker *p1 = (DetectPatternTracker *)data1;
2343  const DetectPatternTracker *p2 = (DetectPatternTracker *)data2;
2344 
2345  if (p1->sm_list != p2->sm_list)
2346  return 0;
2347 
2348  if (ContentFlagsForHash(p1->cd) != ContentFlagsForHash(p2->cd))
2349  return 0;
2350 
2351  uint16_t p1_content_len = p1->cd->content_len;
2352  uint8_t *p1_content = p1->cd->content;
2354  p1_content += p1->cd->fp_chop_offset;
2355  p1_content_len = p1->cd->fp_chop_len;
2356  }
2357  uint16_t p2_content_len = p2->cd->content_len;
2358  uint8_t *p2_content = p2->cd->content;
2360  p2_content += p2->cd->fp_chop_offset;
2361  p2_content_len = p2->cd->fp_chop_len;
2362  }
2363 
2364  if (p1_content_len != p2_content_len)
2365  return 0;
2366 
2367  if (memcmp(p1_content, p2_content, p1_content_len) != 0) {
2368  return 0;
2369  }
2370 
2371  return 1;
2372 }
2373 
2374 /**
2375  * \brief The Compare function for Pattern. Ignores chop settings
2376  *
2377  * \param data1 Pointer to the first Pattern.
2378  * \param len1 Not used.
2379  * \param data2 Pointer to the second Pattern.
2380  * \param len2 Not used.
2381  *
2382  * \retval 1 If the 2 Patterns sent as args match.
2383  * \retval 0 If the 2 Patterns sent as args do not match.
2384  */
2385 static char PatternNoChopCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
2386 {
2387  const DetectPatternTracker *p1 = (DetectPatternTracker *)data1;
2388  const DetectPatternTracker *p2 = (DetectPatternTracker *)data2;
2389 
2390  if (p1->sm_list != p2->sm_list)
2391  return 0;
2392 
2393  if (ContentFlagsForHash(p1->cd) != ContentFlagsForHash(p2->cd))
2394  return 0;
2395 
2396  if (p1->cd->content_len != p2->cd->content_len)
2397  return 0;
2398 
2399  if (memcmp(p1->cd->content, p2->cd->content, p1->cd->content_len) != 0) {
2400  return 0;
2401  }
2402 
2403  return 1;
2404 }
2405 
2406 static void PatternFreeFunc(void *ptr)
2407 {
2408  SCFree(ptr);
2409 }
2410 
2411 /**
2412  * \brief Figure out the FP and their respective content ids for all the
2413  * sigs in the engine.
2414  *
2415  * \param de_ctx Detection engine context.
2416  *
2417  * \retval 0 On success.
2418  * \retval -1 On failure.
2419  */
2421 {
2422  uint32_t cnt = 0;
2423  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
2424  if (s->init_data->mpm_sm != NULL) {
2425  cnt++;
2426  }
2427  }
2428  /* no mpm rules */
2429  if (cnt == 0)
2430  return 0;
2431 
2432  HashListTable *ht =
2433  HashListTableInit(4096, PatternChopHashFunc, PatternChopCompareFunc, PatternFreeFunc);
2434  BUG_ON(ht == NULL);
2435  PatIntId max_id = 0;
2436 
2437  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
2438  if (s->init_data->mpm_sm == NULL)
2439  continue;
2440 
2441  const int sm_list = s->init_data->mpm_sm_list;
2442  BUG_ON(sm_list == -1);
2443 
2445 
2446  DetectPatternTracker lookup = { .cd = cd, .sm_list = sm_list, .cnt = 0, .mpm = 0 };
2447  DetectPatternTracker *res = HashListTableLookup(ht, &lookup, 0);
2448  if (res) {
2449  res->cnt++;
2450  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2451 
2452  cd->id = res->cd->id;
2453  SCLogDebug("%u: res id %u cnt %u", s->id, res->cd->id, res->cnt);
2454  } else {
2455  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2456  BUG_ON(add == NULL);
2457  add->cd = cd;
2458  add->sm_list = sm_list;
2459  add->cnt = 1;
2460  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2461  HashListTableAdd(ht, (void *)add, 0);
2462 
2463  cd->id = max_id++;
2464  SCLogDebug("%u: add id %u cnt %u", s->id, add->cd->id, add->cnt);
2465  }
2466  }
2467 
2468  HashListTableFree(ht);
2469 
2470  return 0;
2471 }
2472 
2473 /** \brief add all patterns on our stats hash
2474  * Used to fill the hash later used by DumpPatterns()
2475  * \note sets up the hash table on first call
2476  */
2478 {
2479  if (de_ctx->pattern_hash_table == NULL) {
2481  4096, PatternNoChopHashFunc, PatternNoChopCompareFunc, PatternFreeFunc);
2482  BUG_ON(de_ctx->pattern_hash_table == NULL);
2483  }
2484  if (s->sm_arrays[DETECT_SM_LIST_PMATCH]) {
2486  do {
2487  switch (smd->type) {
2488  case DETECT_CONTENT: {
2489  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2490  DetectPatternTracker lookup = {
2491  .cd = cd, .sm_list = DETECT_SM_LIST_PMATCH, .cnt = 0, .mpm = 0
2492  };
2493  DetectPatternTracker *res =
2495  if (res) {
2496  res->cnt++;
2497  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2498  } else {
2499  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2500  BUG_ON(add == NULL);
2501  add->cd = cd;
2503  add->cnt = 1;
2504  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2505  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2506  }
2507  break;
2508  }
2509  }
2510  if (smd->is_last)
2511  break;
2512  smd++;
2513  } while (1);
2514  }
2515 
2517  for (; app != NULL; app = app->next) {
2518  DEBUG_VALIDATE_BUG_ON(app->smd == NULL);
2519  SigMatchData *smd = app->smd;
2520  do {
2521  switch (smd->type) {
2522  case DETECT_CONTENT: {
2523  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2524 
2525  DetectPatternTracker lookup = {
2526  .cd = cd, .sm_list = app->sm_list, .cnt = 0, .mpm = 0
2527  };
2528  DetectPatternTracker *res =
2530  if (res) {
2531  res->cnt++;
2532  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2533  } else {
2534  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2535  BUG_ON(add == NULL);
2536  add->cd = cd;
2537  add->sm_list = app->sm_list;
2538  add->cnt = 1;
2539  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2540  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2541  }
2542  break;
2543  }
2544  }
2545  if (smd->is_last)
2546  break;
2547  smd++;
2548  } while (1);
2549  }
2551  for (; pkt != NULL; pkt = pkt->next) {
2552  SigMatchData *smd = pkt->smd;
2553  do {
2554  if (smd == NULL) {
2556  smd = s->sm_arrays[pkt->sm_list];
2557  }
2558  switch (smd->type) {
2559  case DETECT_CONTENT: {
2560  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2561 
2562  DetectPatternTracker lookup = {
2563  .cd = cd, .sm_list = pkt->sm_list, .cnt = 0, .mpm = 0
2564  };
2565  DetectPatternTracker *res =
2567  if (res) {
2568  res->cnt++;
2569  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2570  } else {
2571  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2572  BUG_ON(add == NULL);
2573  add->cd = cd;
2574  add->sm_list = pkt->sm_list;
2575  add->cnt = 1;
2576  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2577  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2578  }
2579  break;
2580  }
2581  }
2582  if (smd->is_last)
2583  break;
2584  smd++;
2585  } while (1);
2586  }
2588  for (; frame != NULL; frame = frame->next) {
2589  SigMatchData *smd = frame->smd;
2590  do {
2591  if (smd == NULL) {
2593  smd = s->sm_arrays[frame->sm_list];
2594  }
2595  switch (smd->type) {
2596  case DETECT_CONTENT: {
2597  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2598 
2599  DetectPatternTracker lookup = {
2600  .cd = cd, .sm_list = frame->sm_list, .cnt = 0, .mpm = 0
2601  };
2602  DetectPatternTracker *res =
2604  if (res) {
2605  res->cnt++;
2606  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2607  } else {
2608  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2609  BUG_ON(add == NULL);
2610  add->cd = cd;
2611  add->sm_list = frame->sm_list;
2612  add->cnt = 1;
2613  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2614  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2615  }
2616  break;
2617  }
2618  }
2619  if (smd->is_last)
2620  break;
2621  smd++;
2622  } while (1);
2623  }
2624 }
MpmInitThreadCtx
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t matcher)
Definition: util-mpm.c:195
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:1022
DetectBufferMpmRegistry_::pkt_v1
struct DetectBufferMpmRegistry_::@82::@85 pkt_v1
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:56
DetectEngineCtx_::frame_mpms_list_cnt
uint32_t frame_mpms_list_cnt
Definition: detect.h:1025
DetectContentData_::offset
uint16_t offset
Definition: detect-content.h:107
SignatureInitData_::max_content_list_id
uint32_t max_content_list_id
Definition: detect.h:606
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:884
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:748
SCFPSupportSMList_
Definition: detect.h:779
DetectEngineAppInspectionEngine_
Definition: detect.h:430
util-hash-string.h
MPMB_UDP_TS
@ MPMB_UDP_TS
Definition: detect.h:1366
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:538
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:95
detect-engine.h
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:118
DetectBufferMpmRegistry_::direction
int direction
Definition: detect.h:706
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:595
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:1374
DetectContentData_::fp_chop_len
uint16_t fp_chop_len
Definition: detect-content.h:98
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
MpmStore_::sid_array
uint8_t * sid_array
Definition: detect.h:1373
DetectEngineCtx_::sgh_mpm_context_proto_tcp_packet
int32_t sgh_mpm_context_proto_tcp_packet
Definition: detect.h:936
PatternMatchPrepareGroup
int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh)
Prepare the pattern matcher ctx in a sig group head.
Definition: detect-engine-mpm.c:2229
SCFPSupportSMList_::next
struct SCFPSupportSMList_ * next
Definition: detect.h:782
DetectEnginePktInspectionEngine
Definition: detect.h:489
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:47
DetectEngineAppInspectionEngine_::next
struct DetectEngineAppInspectionEngine_ * next
Definition: detect.h:454
detect-engine-siggroup.h
SigGroupHead_::flags
uint16_t flags
Definition: detect.h:1483
SCFPSupportSMList_::list_id
int list_id
Definition: detect.h:780
SigTableElmt_::name
const char * name
Definition: detect.h:1326
MpmStoreFree
void MpmStoreFree(DetectEngineCtx *de_ctx)
Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by MpmStoreInit() function.
Definition: detect-engine-mpm.c:1529
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:752
Signature_::num
SigIntId num
Definition: detect.h:630
ConfGetBool
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition: conf.c:482
DETECT_ABSENT
@ DETECT_ABSENT
Definition: detect-engine-register.h:97
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1482
DetectEngineCtx_::pattern_hash_table
HashListTable * pattern_hash_table
Definition: detect.h:897
DetectEngineTransforms
Definition: detect.h:409
DetectBufferMpmRegistry_::sm_list_base
int16_t sm_list_base
Definition: detect.h:708
MpmFactoryReClaimMpmCtx
void MpmFactoryReClaimMpmCtx(const DetectEngineCtx *de_ctx, MpmCtx *mpm_ctx)
Definition: util-mpm.c:156
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:71
MpmStoreReportStats
void MpmStoreReportStats(const DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:1432
Signature_::alproto
AppProto alproto
Definition: detect.h:623
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
MPMB_OTHERIP
@ MPMB_OTHERIP
Definition: detect.h:1368
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:361
DetectBufferTypeSupportsFrames
void DetectBufferTypeSupportsFrames(const char *name)
Definition: detect-engine.c:1074
DetectMpmInitializeFrameMpms
void DetectMpmInitializeFrameMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:461
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:85
DETECT_SM_LIST_DYNAMIC_START
@ DETECT_SM_LIST_DYNAMIC_START
Definition: detect.h:137
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:362
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:420
MpmStore_::sm_list
int sm_list
Definition: detect.h:1378
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:1021
DETECT_BUFFER_MPM_TYPE_FRAME
@ DETECT_BUFFER_MPM_TYPE_FRAME
Definition: detect.h:697
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:2420
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:415
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:40
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:860
DetectEnginePktInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:490
util-memcpy.h
DETECT_CONTENT_MPM_IS_CONCLUSIVE
#define DETECT_CONTENT_MPM_IS_CONCLUSIVE(c)
Definition: detect-content.h:78
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:287
DETECT_CONTENT_DEPTH_VAR
#define DETECT_CONTENT_DEPTH_VAR
Definition: detect-content.h:46
DetectEngineCtx_::mpm_cfg
MpmConfig * mpm_cfg
Definition: detect.h:864
InspectionBufferGetPktDataPtr
InspectionBuffer *(* InspectionBufferGetPktDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Packet *p, const int list_id)
Definition: detect.h:484
DetectEngineBufferTypeGetNameById
const char * DetectEngineBufferTypeGetNameById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1144
DetectMpmInitializeBuiltinMpms
void DetectMpmInitializeBuiltinMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:726
DetectBufferMpmRegistry_::next
struct DetectBufferMpmRegistry_ * next
Definition: detect.h:744
SIG_FLAG_REQUIRE_STREAM
#define SIG_FLAG_REQUIRE_STREAM
Definition: detect.h:253
DetectPatternTracker::cnt
uint32_t cnt
Definition: detect.h:751
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:703
DE_QUIET
#define DE_QUIET
Definition: detect.h:324
MpmCtx_::maxlen
uint16_t maxlen
Definition: util-mpm.h:105
MPMB_TCP_STREAM_TS
@ MPMB_TCP_STREAM_TS
Definition: detect.h:1364
DetectPatternTracker::cd
const struct DetectContentData_ * cd
Definition: detect.h:749
PatIntId
#define PatIntId
Definition: suricata-common.h:327
SIG_GROUP_HEAD_HAVERAWSTREAM
#define SIG_GROUP_HEAD_HAVERAWSTREAM
Definition: detect.h:1352
MpmTableElmt_::feature_flags
uint8_t feature_flags
Definition: util-mpm.h:183
MPM_TABLE_SIZE
@ MPM_TABLE_SIZE
Definition: util-mpm.h:40
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:672
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:270
MAX
#define MAX(x, y)
Definition: suricata-common.h:404
MPMB_MAX
@ MPMB_MAX
Definition: detect.h:1369
SigMatchData_
Data needed for Match()
Definition: detect.h:359
DetectEngineCtx_::sgh_mpm_context_proto_udp_packet
int32_t sgh_mpm_context_proto_udp_packet
Definition: detect.h:937
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:716
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:360
DetectEngineRegisterFastPatternForId
void DetectEngineRegisterFastPatternForId(DetectEngineCtx *de_ctx, int list_id, int priority)
Definition: detect-fast-pattern.c:134
SidsArray
Definition: detect-engine-mpm.c:1770
MPMB_TCP_STREAM_TC
@ MPMB_TCP_STREAM_TC
Definition: detect.h:1365
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:2477
Signature_::frame_inspect
DetectEngineFrameInspectionEngine * frame_inspect
Definition: detect.h:668
MpmBuiltinBuffers
MpmBuiltinBuffers
Definition: detect.h:1361
MpmConfig_::cache_dir_path
const char * cache_dir_path
Definition: util-mpm.h:90
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1114
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
ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE
@ ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE
Definition: detect.h:1085
util-memcmp.h
SigGroupHeadInitData_::pkt_mpms
MpmCtx ** pkt_mpms
Definition: detect.h:1466
MpmInitCtx
void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher)
Definition: util-mpm.c:209
Signature_::next
struct Signature_ * next
Definition: detect.h:691
DetectEngineCtx_::sgh_mpm_context_proto_other_packet
int32_t sgh_mpm_context_proto_other_packet
Definition: detect.h:938
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:438
DetectBufferInstance::alproto
AppProto alproto
Definition: detect-engine-mpm.c:1937
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:694
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:269
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:202
util-debug.h
DetectBufferInstance::ts
struct SidsArray ts
Definition: detect-engine-mpm.c:1939
SigGroupHeadInitData_::sig_cnt
SigIntId sig_cnt
Definition: detect.h:1475
SidsArray::sids_array_size
uint32_t sids_array_size
Definition: detect-engine-mpm.c:1772
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:712
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
MpmFactoryGetMpmCtxForProfile
MpmCtx * MpmFactoryGetMpmCtxForProfile(const DetectEngineCtx *de_ctx, int32_t id, int direction)
Definition: util-mpm.c:131
DetectEnginePktInspectionEngine::sm_list
uint16_t sm_list
Definition: detect.h:492
g_alproto_max
AppProto g_alproto_max
Definition: app-layer-protos.c:29
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:143
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
MpmStore_
Definition: detect.h:1372
SignatureInitData_::mpm_sm
SigMatch * mpm_sm
Definition: detect.h:576
DetectBufferMpmRegistry_::sm_list
int16_t sm_list
Definition: detect.h:707
DetectEngineGetMaxSigId
#define DetectEngineGetMaxSigId(de_ctx)
Definition: detect-engine.h:109
SignatureInitData_::mpm_sm_list
int mpm_sm_list
Definition: detect.h:574
SidsArray::sids_array
uint8_t * sids_array
Definition: detect-engine-mpm.c:1771
DETECT_CONTENT_DEPTH
#define DETECT_CONTENT_DEPTH
Definition: detect-content.h:33
Signature_::pkt_inspect
DetectEnginePktInspectionEngine * pkt_inspect
Definition: detect.h:667
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:519
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:354
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:863
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:761
DetectBufferMpmRegistry_::priority
int priority
Definition: detect.h:709
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:1201
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1505
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:666
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:104
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:353
DetectProto_::proto
uint8_t proto[256/8]
Definition: detect-engine-proto.h:36
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:309
MpmStore_::direction
int direction
Definition: detect.h:1376
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:72
Signature_::flags
uint32_t flags
Definition: detect.h:619
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
DetectEngineCtx_::sgh_mpm_context_stream
int32_t sgh_mpm_context_stream
Definition: detect.h:939
type
uint16_t type
Definition: decode-vlan.c:106
DetectEngineBufferTypeSupportsFrames
void DetectEngineBufferTypeSupportsFrames(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1240
DetectEngineCtx_::frame_mpms_list
DetectBufferMpmRegistry * frame_mpms_list
Definition: detect.h:1024
conf.h
DetectEngineCtx_::sgh_mpm_ctx_cnf
uint8_t sgh_mpm_ctx_cnf
Definition: detect.h:965
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:1380
DetectEngineFrameInspectionEngine
Definition: detect.h:514
DETECT_BUFFER_MPM_TYPE_PKT
@ DETECT_BUFFER_MPM_TYPE_PKT
Definition: detect.h:695
ALPROTO_DOH2
@ ALPROTO_DOH2
Definition: app-layer-protos.h:67
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:688
name
const char * name
Definition: tm-threads.c:2081
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:781
HashListTable_
Definition: util-hashlist.h:37
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@82::@84 app_v2
DetectEngineTransforms::transforms
TransformData transforms[DETECT_TRANSFORMS_MAX]
Definition: detect.h:410
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:70
SidsArray::type
enum DetectBufferMpmType type
Definition: detect-engine-mpm.c:1776
SigGroupHeadInitData_::app_mpms
MpmCtx ** app_mpms
Definition: detect.h:1465
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:249
DetectEngineBufferTypeSupportsTransformations
void DetectEngineBufferTypeSupportsTransformations(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1264
SGH_DIRECTION_TS
#define SGH_DIRECTION_TS(sgh)
Definition: detect-engine-mpm.c:1005
DetectPatternTracker::sm_list
int sm_list
Definition: detect.h:750
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:452
DetectBufferInstance
Definition: detect-engine-mpm.c:1934
MpmStore_::mpm_ctx
MpmCtx * mpm_ctx
Definition: detect.h:1381
MPM_FEATURE_FLAG_ENDSWITH
#define MPM_FEATURE_FLAG_ENDSWITH
Definition: util-mpm.h:148
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:1637
MPMB_TCP_PKT_TC
@ MPMB_TCP_PKT_TC
Definition: detect.h:1363
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:637
DetectBufferMpmRegistry_::type
enum DetectBufferMpmType type
Definition: detect.h:711
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:1366
DetectEngineCtx_::app_mpms_list
DetectBufferMpmRegistry * app_mpms_list
Definition: detect.h:1016
suricata-common.h
MpmCtx_::pattern_cnt
uint32_t pattern_cnt
Definition: util-mpm.h:102
DETECT_BUFFER_MPM_TYPE_APP
@ DETECT_BUFFER_MPM_TYPE_APP
Definition: detect.h:696
DetectBufferTypeSupportsMpm
void DetectBufferTypeSupportsMpm(const char *name)
Definition: detect-engine.c:1094
DetectBufferInstance::list
int list
Definition: detect-engine-mpm.c:1936
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:88
SigGroupHeadInitData_::match_array
Signature ** match_array
Definition: detect.h:1478
DetectBufferMpmRegistry_::name
const char * name
Definition: detect.h:704
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:527
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:230
DetectEnginePktInspectionEngine::next
struct DetectEnginePktInspectionEngine * next
Definition: detect.h:500
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:869
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:405
DetectEngineBufferTypeSupportsMpm
void DetectEngineBufferTypeSupportsMpm(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1256
util-validate.h
detect-flow.h
DetectEngineCtx_::app_mpms_list_cnt
uint32_t app_mpms_list_cnt
Definition: detect.h:1015
DetectBufferTypeSupportsTransformations
void DetectBufferTypeSupportsTransformations(const char *name)
Definition: detect-engine.c:1104
MPM_PATTERN_FLAG_ENDSWITH
#define MPM_PATTERN_FLAG_ENDSWITH
Definition: util-mpm.h:144
builtin_mpms
const char * builtin_mpms[]
Definition: detect-engine-mpm.c:66
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:600
g_skip_prefilter
int g_skip_prefilter
Definition: detect-engine-mpm.c:1072
DetectEngineCtx_::mpm_hash_table
HashListTable * mpm_hash_table
Definition: detect.h:896
MpmTableElmt_::Prepare
int(* Prepare)(MpmConfig *, struct MpmCtx_ *)
Definition: util-mpm.h:174
MPMB_UDP_TC
@ MPMB_UDP_TC
Definition: detect.h:1367
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:154
DetectBufferMpmRegistry_::id
int id
Definition: detect.h:710
SigMatchListSMBelongsTo
int SigMatchListSMBelongsTo(const Signature *s, const SigMatch *key_sm)
Definition: detect-parse.c:831
SCFree
#define SCFree(p)
Definition: util-mem.h:61
MPM_CTX_FACTORY_UNIQUE_CONTEXT
#define MPM_CTX_FACTORY_UNIQUE_CONTEXT
Definition: util-mpm.h:118
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:654
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:714
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:531
Signature_
Signature container.
Definition: detect.h:618
SigMatch_
a single match condition for a signature
Definition: detect.h:350
DetectBufferMpmRegistry_::frame_v1
struct DetectBufferMpmRegistry_::@82::@86 frame_v1
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
MPMB_TCP_PKT_TS
@ MPMB_TCP_PKT_TS
Definition: detect.h:1362
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:411
suricata.h
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:878
DETECT_BUFFER_MPM_TYPE_SIZE
@ DETECT_BUFFER_MPM_TYPE_SIZE
Definition: detect.h:699
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DetectBufferInstance::tc
struct SidsArray tc
Definition: detect-engine-mpm.c:1940
DetectEngineCtx_::buffer_type_id
uint32_t buffer_type_id
Definition: detect.h:1013
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:862
MpmCtx_
Definition: util-mpm.h:93
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:97
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
MPMCTX_FLAGS_CACHE_TO_DISK
#define MPMCTX_FLAGS_CACHE_TO_DISK
Definition: util-mpm.h:87
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:1774
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:601
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:258
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:1038
MpmStore_::sgh_mpm_context
int32_t sgh_mpm_context
Definition: detect.h:1379
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
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:1377
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:1467
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:705
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:252
DetectEngineFrameInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:526