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