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 (!(s->proto.proto[IPPROTO_TCP / 8] & 1 << (IPPROTO_TCP % 8))) {
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 (!(s->proto.proto[IPPROTO_TCP / 8] & 1 << (IPPROTO_TCP % 8))) {
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 }
968 void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
969 {
970  SCLogDebug("mpm_thread_ctx %p, type %"PRIu16, mpm_thread_ctx, mpm_matcher);
971  MpmInitThreadCtx(mpm_thread_ctx, mpm_matcher);
972 }
973 
974 /** \brief Predict a strength value for patterns
975  *
976  * Patterns with high character diversity score higher.
977  * Alpha chars score not so high
978  * Other printable + a few common codes a little higher
979  * Everything else highest.
980  * Longer patterns score better than short patters.
981  *
982  * \param pat pattern
983  * \param patlen length of the pattern
984  *
985  * \retval s pattern score
986  */
987 uint32_t PatternStrength(uint8_t *pat, uint16_t patlen)
988 {
989  uint8_t a[256];
990  memset(&a, 0 ,sizeof(a));
991 
992  uint32_t s = 0;
993  uint16_t u = 0;
994  for (u = 0; u < patlen; u++) {
995  if (a[pat[u]] == 0) {
996  if (isalpha(pat[u]))
997  s += 3;
998  else if (isprint(pat[u]) || pat[u] == 0x00 || pat[u] == 0x01 || pat[u] == 0xFF)
999  s += 4;
1000  else
1001  s += 6;
1002 
1003  a[pat[u]] = 1;
1004  } else {
1005  s++;
1006  }
1007  }
1008 
1009  return s;
1010 }
1011 
1012 static void PopulateMpmHelperAddPattern(MpmCtx *mpm_ctx, const DetectContentData *cd,
1013  const Signature *s, const uint8_t flags, const int chop)
1014 {
1015  uint16_t pat_offset = cd->offset;
1016  uint16_t pat_depth = cd->depth;
1017 
1018  /* recompute offset/depth to cope with chop */
1019  if (chop && (pat_depth || pat_offset)) {
1020  pat_offset += cd->fp_chop_offset;
1021  if (pat_depth) {
1022  pat_depth -= cd->content_len;
1023  pat_depth += cd->fp_chop_offset + cd->fp_chop_len;
1024  }
1025  }
1026 
1027  /* We have to effectively "wild card" values that will be coming from
1028  * byte_extract variables
1029  */
1031  pat_depth = pat_offset = 0;
1032  }
1033 
1034  if (cd->flags & DETECT_CONTENT_NOCASE) {
1035  if (chop) {
1036  SCMpmAddPatternCI(mpm_ctx, cd->content + cd->fp_chop_offset, cd->fp_chop_len,
1037  pat_offset, pat_depth, cd->id, s->iid, flags | MPM_PATTERN_CTX_OWNS_ID);
1038  } else {
1039  SCMpmAddPatternCI(mpm_ctx, cd->content, cd->content_len, pat_offset, pat_depth, cd->id,
1041  }
1042  } else {
1043  if (chop) {
1044  MpmAddPatternCS(mpm_ctx, cd->content + cd->fp_chop_offset, cd->fp_chop_len, pat_offset,
1045  pat_depth, cd->id, s->iid, flags | MPM_PATTERN_CTX_OWNS_ID);
1046  } else {
1047  MpmAddPatternCS(mpm_ctx, cd->content, cd->content_len, pat_offset, pat_depth, cd->id,
1049  }
1050  }
1051 }
1052 
1053 #define SGH_PROTO(sgh, p) ((sgh)->init->protos[(p)] == 1)
1054 #define SGH_DIRECTION_TS(sgh) ((sgh)->init->direction & SIG_FLAG_TOSERVER)
1055 #define SGH_DIRECTION_TC(sgh) ((sgh)->init->direction & SIG_FLAG_TOCLIENT)
1057 static void SetMpm(Signature *s, SigMatch *mpm_sm, const int mpm_sm_list)
1058 {
1059  if (s == NULL || mpm_sm == NULL)
1060  return;
1061 
1062  DetectContentData *cd = (DetectContentData *)mpm_sm->ctx;
1064  if (DETECT_CONTENT_IS_SINGLE(cd) &&
1065  !(cd->flags & DETECT_CONTENT_NEGATED) &&
1066  !(cd->flags & DETECT_CONTENT_REPLACE) &&
1067  cd->content_len == cd->fp_chop_len)
1068  {
1070  }
1071  } else {
1072  if (DETECT_CONTENT_IS_SINGLE(cd) &&
1073  !(cd->flags & DETECT_CONTENT_NEGATED) &&
1074  !(cd->flags & DETECT_CONTENT_REPLACE))
1075  {
1077  }
1078  }
1079  cd->flags |= DETECT_CONTENT_MPM;
1080  s->init_data->mpm_sm_list = mpm_sm_list;
1081  s->init_data->mpm_sm = mpm_sm;
1082 }
1083 
1084 static SigMatch *GetMpmForList(const Signature *s, SigMatch *list, SigMatch *mpm_sm,
1085  uint16_t max_len, bool skip_negated_content)
1086 {
1087  for (SigMatch *sm = list; sm != NULL; sm = sm->next) {
1088  if (sm->type != DETECT_CONTENT)
1089  continue;
1090 
1091  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1092  /* skip_negated_content is only set if there's absolutely no
1093  * non-negated content present in the sig */
1094  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
1095  continue;
1096  if (cd->content_len != max_len) {
1097  SCLogDebug("content_len %u != max_len %u", cd->content_len, max_len);
1098  continue;
1099  }
1100  if (mpm_sm == NULL) {
1101  mpm_sm = sm;
1102  } else {
1103  DetectContentData *data1 = (DetectContentData *)sm->ctx;
1104  DetectContentData *data2 = (DetectContentData *)mpm_sm->ctx;
1105  uint32_t ls = PatternStrength(data1->content, data1->content_len);
1106  uint32_t ss = PatternStrength(data2->content, data2->content_len);
1107  if (ls > ss) {
1108  mpm_sm = sm;
1109  } else if (ls == ss) {
1110  /* if 2 patterns are of equal strength, we pick the longest */
1111  if (data1->content_len > data2->content_len)
1112  mpm_sm = sm;
1113  } else {
1114  SCLogDebug("sticking with mpm_sm");
1115  }
1116  }
1117  }
1118  return mpm_sm;
1119 }
1120 
1122 
1123 // tells if a buffer id is only used to client
1124 bool DetectBufferToClient(const DetectEngineCtx *de_ctx, int buf_id, AppProto alproto)
1125 {
1126  bool r = false;
1128  for (; app != NULL; app = app->next) {
1129  if (app->sm_list == buf_id &&
1130  (AppProtoEquals(alproto, app->alproto) || alproto == ALPROTO_UNKNOWN)) {
1131  if (app->dir == 1) {
1132  // do not return yet in case we have app engines on both sides
1133  r = true;
1134  } else {
1135  // ambiguous keywords have a app-engine to server
1136  return false;
1137  }
1138  }
1139  }
1140  return r;
1141 }
1142 
1144 {
1145  if (g_skip_prefilter)
1146  return;
1147 
1148  if (s->init_data->mpm_sm != NULL)
1149  return;
1150 
1151  const int nlists = s->init_data->max_content_list_id + 1;
1152  DEBUG_VALIDATE_BUG_ON(nlists > UINT16_MAX);
1153  int pos_sm_list[nlists];
1154  int neg_sm_list[nlists];
1155  memset(pos_sm_list, 0, nlists * sizeof(int));
1156  memset(neg_sm_list, 0, nlists * sizeof(int));
1157  int pos_sm_list_cnt = 0;
1158  int neg_sm_list_cnt = 0;
1159 
1160  /* inspect rule to see if we have the fast_pattern reg to
1161  * force using a sig, otherwise keep stats about the patterns */
1162  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL) {
1164  for (SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH]; sm != NULL;
1165  sm = sm->next) {
1166  if (sm->type != DETECT_CONTENT)
1167  continue;
1168 
1169  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1170  /* fast_pattern set in rule, so using this pattern */
1171  if ((cd->flags & DETECT_CONTENT_FAST_PATTERN)) {
1172  SetMpm(s, sm, DETECT_SM_LIST_PMATCH);
1173  return;
1174  }
1175 
1176  if (cd->flags & DETECT_CONTENT_NEGATED) {
1177  neg_sm_list[DETECT_SM_LIST_PMATCH] = 1;
1178  neg_sm_list_cnt++;
1179  } else {
1180  pos_sm_list[DETECT_SM_LIST_PMATCH] = 1;
1181  pos_sm_list_cnt++;
1182  }
1183  }
1184  }
1185  }
1186  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1187  const int list_id = s->init_data->buffers[x].id;
1188 
1189  SCLogDebug("%u: list_id %d: %s", s->id, list_id,
1191 
1193  SCLogDebug("skip");
1194  continue;
1195  }
1196 
1197  for (SigMatch *sm = s->init_data->buffers[x].head; sm != NULL; sm = sm->next) {
1198  // a buffer with absent keyword cannot be used as fast_pattern
1199  if (sm->type == DETECT_ABSENT)
1200  break;
1201  if (sm->type != DETECT_CONTENT)
1202  continue;
1203 
1204  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1205  /* fast_pattern set in rule, so using this pattern */
1206  if ((cd->flags & DETECT_CONTENT_FAST_PATTERN)) {
1207  SetMpm(s, sm, list_id);
1208  return;
1209  }
1210 
1211  if (cd->flags & DETECT_CONTENT_NEGATED) {
1212  neg_sm_list[list_id] = 1;
1213  neg_sm_list_cnt++;
1214  } else {
1215  pos_sm_list[list_id] = 1;
1216  pos_sm_list_cnt++;
1217  SCLogDebug("pos added for %d", list_id);
1218  }
1219  }
1220  SCLogDebug("ok");
1221  }
1222 
1223  SCLogDebug("neg_sm_list_cnt %d pos_sm_list_cnt %d", neg_sm_list_cnt, pos_sm_list_cnt);
1224 
1225  /* prefer normal not-negated over negated */
1226  int *curr_sm_list = NULL;
1227  int skip_negated_content = 1;
1228  if (pos_sm_list_cnt > 0) {
1229  curr_sm_list = pos_sm_list;
1230  } else if (neg_sm_list_cnt > 0) {
1231  curr_sm_list = neg_sm_list;
1232  skip_negated_content = 0;
1233  } else {
1234  return;
1235  }
1236 
1237  int final_sm_list[nlists];
1238  memset(&final_sm_list, 0, (nlists * sizeof(int)));
1239 
1240  int count_final_sm_list = 0;
1241  int count_txbidir_toclient_sm_list = 0;
1242  int priority;
1243 
1245  while (tmp != NULL) {
1246  for (priority = tmp->priority;
1247  tmp != NULL && priority == tmp->priority;
1248  tmp = tmp->next)
1249  {
1250  SCLogDebug("tmp->list_id %d tmp->priority %d", tmp->list_id, tmp->priority);
1251  if (tmp->list_id >= nlists)
1252  continue;
1253  if (curr_sm_list[tmp->list_id] == 0)
1254  continue;
1255  if (s->flags & SIG_FLAG_TXBOTHDIR) {
1256  // prefer to choose a fast_pattern to server by default
1257  if (DetectBufferToClient(de_ctx, tmp->list_id, s->alproto)) {
1258  if (count_final_sm_list == 0) {
1259  // still put it in in the case we do not have toserver buffer
1260  final_sm_list[count_txbidir_toclient_sm_list++] = tmp->list_id;
1261  }
1262  continue;
1263  }
1264  }
1265  // we may erase tx bidir toclient buffers here as intended if we have a better choice
1266  final_sm_list[count_final_sm_list++] = tmp->list_id;
1267  SCLogDebug("tmp->list_id %d", tmp->list_id);
1268  }
1269  if (count_final_sm_list != 0)
1270  break;
1271  }
1272 
1273  if ((s->flags & SIG_FLAG_TXBOTHDIR) && count_final_sm_list == 0) {
1274  // forced to pick a fast_pattern to client for tx bidir signature
1275  count_final_sm_list = count_txbidir_toclient_sm_list;
1276  }
1277  BUG_ON(count_final_sm_list == 0);
1278  SCLogDebug("count_final_sm_list %d skip_negated_content %d", count_final_sm_list,
1279  skip_negated_content);
1280 
1281  uint16_t max_len = 0;
1282  for (int i = 0; i < count_final_sm_list; i++) {
1283  SCLogDebug("i %d final_sm_list[i] %d", i, final_sm_list[i]);
1284 
1285  if (final_sm_list[i] == DETECT_SM_LIST_PMATCH) {
1286  for (SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH]; sm != NULL;
1287  sm = sm->next) {
1288  if (sm->type != DETECT_CONTENT)
1289  continue;
1290 
1291  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1292  /* skip_negated_content is only set if there's absolutely no
1293  * non-negated content present in the sig */
1294  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
1295  continue;
1296  max_len = MAX(max_len, cd->content_len);
1297  }
1298  } else {
1299  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1300  if (s->init_data->buffers[x].only_tc) {
1301  // prefer to choose a fast_pattern to server by default
1302  continue;
1303  }
1304  const int list_id = s->init_data->buffers[x].id;
1305 
1306  if (final_sm_list[i] == list_id) {
1307  SCLogDebug("%u: list_id %d: %s", s->id, list_id,
1309 
1310  for (SigMatch *sm = s->init_data->buffers[x].head; sm != NULL; sm = sm->next) {
1311  if (sm->type != DETECT_CONTENT)
1312  continue;
1313 
1314  const DetectContentData *cd = (DetectContentData *)sm->ctx;
1315  /* skip_negated_content is only set if there's absolutely no
1316  * non-negated content present in the sig */
1317  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
1318  continue;
1319  max_len = MAX(max_len, cd->content_len);
1320  }
1321  }
1322  }
1323  }
1324  }
1325 
1326  SigMatch *mpm_sm = NULL;
1327  int mpm_sm_list = -1;
1328  for (int i = 0; i < count_final_sm_list; i++) {
1329  SCLogDebug("i %d", i);
1330  if (final_sm_list[i] == DETECT_SM_LIST_PMATCH) {
1331  /* GetMpmForList may keep `mpm_sm` the same, so track if it changed */
1332  SigMatch *prev_mpm_sm = mpm_sm;
1333  mpm_sm = GetMpmForList(s, s->init_data->smlists[DETECT_SM_LIST_PMATCH], mpm_sm, max_len,
1334  skip_negated_content);
1335  if (mpm_sm != prev_mpm_sm) {
1336  mpm_sm_list = final_sm_list[i];
1337  }
1338  } else {
1339  SCLogDebug(
1340  "%u: %s", s->id, DetectEngineBufferTypeGetNameById(de_ctx, final_sm_list[i]));
1341  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1342  const int list_id = s->init_data->buffers[x].id;
1343  if (final_sm_list[i] == list_id) {
1344  SCLogDebug("%u: list_id %d: %s", s->id, list_id,
1346  /* GetMpmForList may keep `mpm_sm` the same, so track if it changed */
1347  SigMatch *prev_mpm_sm = mpm_sm;
1348  mpm_sm = GetMpmForList(s, s->init_data->buffers[x].head, mpm_sm, max_len,
1349  skip_negated_content);
1350  SCLogDebug("mpm_sm %p from %p", mpm_sm, s->init_data->buffers[x].head);
1351  if (mpm_sm != prev_mpm_sm) {
1352  mpm_sm_list = list_id;
1353  }
1354  }
1355  }
1356  }
1357  }
1358 
1359 #ifdef DEBUG
1360  if (mpm_sm != NULL) {
1361  BUG_ON(mpm_sm_list == -1);
1362  int check_list = SigMatchListSMBelongsTo(s, mpm_sm);
1363  BUG_ON(check_list != mpm_sm_list);
1364  }
1365 #endif
1366  /* assign to signature */
1367  SetMpm(s, mpm_sm, mpm_sm_list);
1368 }
1369 
1370 /** \internal
1371  * \brief The hash function for MpmStore
1372  *
1373  * \param ht Pointer to the hash table.
1374  * \param data Pointer to the MpmStore.
1375  * \param datalen Not used in our case.
1376  *
1377  * \retval hash The generated hash value.
1378  */
1379 static uint32_t MpmStoreHashFunc(HashListTable *ht, void *data, uint16_t datalen)
1380 {
1381  const MpmStore *ms = (MpmStore *)data;
1382  uint32_t hash = ms->alproto;
1383 
1384  for (uint32_t b = 0; b < ms->sid_array_size; b++)
1385  hash += ms->sid_array[b];
1386 
1387  return hash % ht->array_size;
1388 }
1389 
1390 /**
1391  * \brief The Compare function for MpmStore
1392  *
1393  * \param data1 Pointer to the first MpmStore.
1394  * \param len1 Not used.
1395  * \param data2 Pointer to the second MpmStore.
1396  * \param len2 Not used.
1397  *
1398  * \retval 1 If the 2 MpmStores sent as args match.
1399  * \retval 0 If the 2 MpmStores sent as args do not match.
1400  */
1401 static char MpmStoreCompareFunc(void *data1, uint16_t len1, void *data2,
1402  uint16_t len2)
1403 {
1404  const MpmStore *ms1 = (MpmStore *)data1;
1405  const MpmStore *ms2 = (MpmStore *)data2;
1406 
1407  if (ms1->alproto != ms2->alproto)
1408  return 0;
1409 
1410  if (ms1->sid_array_size != ms2->sid_array_size)
1411  return 0;
1412 
1413  if (ms1->buffer != ms2->buffer)
1414  return 0;
1415 
1416  if (ms1->direction != ms2->direction)
1417  return 0;
1418 
1419  if (ms1->sm_list != ms2->sm_list)
1420  return 0;
1421 
1422  if (SCMemcmp(ms1->sid_array, ms2->sid_array,
1423  ms1->sid_array_size) != 0)
1424  {
1425  return 0;
1426  }
1427 
1428  return 1;
1429 }
1430 
1431 static void MpmStoreFreeFunc(void *ptr)
1432 {
1433  MpmStore *ms = ptr;
1434  if (ms != NULL) {
1435  if (ms->mpm_ctx != NULL && !(ms->mpm_ctx->flags & MPMCTX_FLAGS_GLOBAL))
1436  {
1437  SCLogDebug("destroying mpm_ctx %p", ms->mpm_ctx);
1439  SCFree(ms->mpm_ctx);
1440  }
1441  ms->mpm_ctx = NULL;
1442 
1443  SCFree(ms->sid_array);
1444  SCFree(ms);
1445  }
1446 }
1447 
1448 /**
1449  * \brief Initializes the MpmStore mpm hash table to be used by the detection
1450  * engine context.
1451  *
1452  * \param de_ctx Pointer to the detection engine context.
1453  *
1454  * \retval 0 On success.
1455  * \retval -1 On failure.
1456  */
1458 {
1460  MpmStoreHashFunc,
1461  MpmStoreCompareFunc,
1462  MpmStoreFreeFunc);
1463  if (de_ctx->mpm_hash_table == NULL)
1464  goto error;
1465 
1466  return 0;
1467 
1468 error:
1469  return -1;
1470 }
1471 
1472 /**
1473  * \brief Adds a MpmStore to the detection engine context MpmStore
1474  *
1475  * \param de_ctx Pointer to the detection engine context.
1476  * \param sgh Pointer to the MpmStore.
1477  *
1478  * \retval ret 0 on Successfully adding the argument sgh; -1 on failure.
1479  */
1480 static int MpmStoreAdd(DetectEngineCtx *de_ctx, MpmStore *s)
1481 {
1482  int ret = HashListTableAdd(de_ctx->mpm_hash_table, (void *)s, 0);
1483  return ret;
1484 }
1485 
1486 /**
1487  * \brief Used to lookup a MpmStore from the MpmStore
1488  *
1489  * \param de_ctx Pointer to the detection engine context.
1490  * \param sgh Pointer to the MpmStore.
1491  *
1492  * \retval rsgh On success a pointer to the MpmStore if the MpmStore is
1493  * found in the hash table; NULL on failure.
1494  */
1495 static MpmStore *MpmStoreLookup(DetectEngineCtx *de_ctx, MpmStore *s)
1496 {
1498  (void *)s, 0);
1499  return rs;
1500 }
1501 
1502 static const DetectBufferMpmRegistry *GetByMpmStore(
1503  const DetectEngineCtx *de_ctx, const MpmStore *ms)
1504 {
1506  while (am != NULL) {
1507  if (ms->sm_list == am->sm_list &&
1508  ms->direction == am->direction) {
1509  return am;
1510  }
1511  am = am->next;
1512  }
1513  am = de_ctx->pkt_mpms_list;
1514  while (am != NULL) {
1515  if (ms->sm_list == am->sm_list) {
1516  return am;
1517  }
1518  am = am->next;
1519  }
1520  return NULL;
1521 }
1522 
1524 {
1525  HashListTableBucket *htb = NULL;
1526 
1527  uint32_t stats[MPMB_MAX] = {0};
1529  int app_mpms_cnt = de_ctx->buffer_type_id;
1530  uint32_t appstats[app_mpms_cnt + 1]; // +1 to silence scan-build
1531  memset(&appstats, 0x00, sizeof(appstats));
1532  int pkt_mpms_cnt = de_ctx->buffer_type_id;
1533  uint32_t pktstats[pkt_mpms_cnt + 1]; // +1 to silence scan-build
1534  memset(&pktstats, 0x00, sizeof(pktstats));
1535  int frame_mpms_cnt = de_ctx->buffer_type_id;
1536  uint32_t framestats[frame_mpms_cnt + 1]; // +1 to silence scan-build
1537  memset(&framestats, 0x00, sizeof(framestats));
1538 
1540  htb != NULL;
1541  htb = HashListTableGetListNext(htb))
1542  {
1543  const MpmStore *ms = (MpmStore *)HashListTableGetListData(htb);
1544  if (ms == NULL || ms->mpm_ctx == NULL) {
1545  continue;
1546  }
1547  if (ms->buffer < MPMB_MAX)
1548  stats[ms->buffer]++;
1549  else if (ms->sm_list != DETECT_SM_LIST_PMATCH) {
1550  const DetectBufferMpmRegistry *am = GetByMpmStore(de_ctx, ms);
1551  if (am != NULL) {
1552  switch (am->type) {
1554  SCLogDebug("%s: %u patterns. Min %u, Max %u. Ctx %p",
1555  am->name,
1556  ms->mpm_ctx->pattern_cnt,
1557  ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1558  ms->mpm_ctx);
1559  pktstats[am->sm_list]++;
1560  break;
1562  SCLogDebug("%s %s %s: %u patterns. Min %u, Max %u. Ctx %p",
1563  AppProtoToString(ms->alproto), am->name,
1564  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1565  ms->mpm_ctx->pattern_cnt, ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1566  ms->mpm_ctx);
1567  appstats[am->sm_list]++;
1568  break;
1570  SCLogDebug("%s: %u patterns. Min %u, Max %u. Ctx %p", am->name,
1571  ms->mpm_ctx->pattern_cnt, ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1572  ms->mpm_ctx);
1573  framestats[am->sm_list]++;
1574  break;
1576  break;
1577  }
1578  }
1579  }
1580  }
1581 
1582  if (!(de_ctx->flags & DE_QUIET)) {
1583  for (int x = 0; x < MPMB_MAX; x++) {
1584  SCLogPerf("Builtin MPM \"%s\": %u", builtin_mpms[x], stats[x]);
1585  }
1587  while (am != NULL) {
1588  if (appstats[am->sm_list] > 0) {
1589  const char *name = am->name;
1590  const char *direction = am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient";
1591  SCLogPerf("AppLayer MPM \"%s %s (%s)\": %u", direction, name,
1592  AppProtoToString(am->app_v2.alproto), appstats[am->sm_list]);
1593  }
1594  am = am->next;
1595  }
1597  while (pm != NULL) {
1598  if (pktstats[pm->sm_list] > 0) {
1599  const char *name = pm->name;
1600  SCLogPerf("Pkt MPM \"%s\": %u", name, pktstats[pm->sm_list]);
1601  }
1602  pm = pm->next;
1603  }
1605  while (um != NULL) {
1606  if (framestats[um->sm_list] > 0) {
1607  const char *name = um->name;
1608  SCLogPerf("Frame MPM \"%s\": %u", name, framestats[um->sm_list]);
1609  }
1610  um = um->next;
1611  }
1612  }
1613 }
1614 
1615 /**
1616  * \brief Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by
1617  * MpmStoreInit() function.
1618  *
1619  * \param de_ctx Pointer to the detection engine context.
1620  */
1622 {
1623  if (de_ctx->mpm_hash_table == NULL)
1624  return;
1625 
1627  de_ctx->mpm_hash_table = NULL;
1628 }
1629 
1630 static void MpmStoreSetup(const DetectEngineCtx *de_ctx, MpmStore *ms)
1631 {
1632  const Signature *s = NULL;
1633  uint32_t sig;
1634  int dir = 0;
1635 
1636  if (ms->buffer != MPMB_MAX) {
1638 
1639  switch (ms->buffer) {
1640  /* TS is 1 */
1641  case MPMB_TCP_PKT_TS:
1642  case MPMB_TCP_STREAM_TS:
1643  case MPMB_UDP_TS:
1644  dir = 1;
1645  break;
1646 
1647  /* TC is 0 */
1648  default:
1649  case MPMB_UDP_TC:
1650  case MPMB_TCP_STREAM_TC:
1651  case MPMB_TCP_PKT_TC:
1652  case MPMB_OTHERIP: /**< use 0 for other */
1653  dir = 0;
1654  break;
1655  }
1656  } else {
1658 
1659  if (ms->direction == SIG_FLAG_TOSERVER)
1660  dir = 1;
1661  else
1662  dir = 0;
1663  }
1664 
1666  if (ms->mpm_ctx == NULL) {
1667  return;
1668  }
1669 
1671 
1674 
1675  const bool mpm_supports_endswith =
1677 
1678  /* add the patterns */
1679  for (sig = 0; sig < (ms->sid_array_size * 8); sig++) {
1680  if (ms->sid_array[sig / 8] & (1 << (sig % 8))) {
1681  s = de_ctx->sig_array[sig];
1682  DEBUG_VALIDATE_BUG_ON(s == NULL);
1683  if (s == NULL)
1684  continue;
1685 
1686  SCLogDebug("%p: direction %d adding %u", ms, ms->direction, s->id);
1687 
1689 
1690  int skip = 0;
1691  /* negated logic: if mpm match can't be used to be sure about this
1692  * pattern, we have to inspect the rule fully regardless of mpm
1693  * match. So in this case there is no point of adding it at all.
1694  * The non-mpm list entry for the sig will make sure the sig is
1695  * inspected. */
1696  if ((cd->flags & DETECT_CONTENT_NEGATED) &&
1698  {
1699  skip = 1;
1700  SCLogDebug("not adding negated mpm as it's not 'single'");
1701  }
1702 
1703  if (!skip) {
1704  uint8_t flags = 0;
1705  if ((cd->flags & DETECT_CONTENT_ENDS_WITH) && mpm_supports_endswith)
1707  PopulateMpmHelperAddPattern(ms->mpm_ctx, cd, s, flags,
1709  }
1710  }
1711  }
1712 
1713  if (ms->mpm_ctx->pattern_cnt == 0) {
1715  ms->mpm_ctx = NULL;
1716  } else {
1718  if (mpm_table[ms->mpm_ctx->mpm_type].Prepare != NULL) {
1720  }
1721  }
1722  }
1723 }
1724 
1725 
1726 /** \brief Get MpmStore for a built-in buffer type
1727  *
1728  */
1730  enum MpmBuiltinBuffers buf)
1731 {
1732  const Signature *s = NULL;
1733  uint32_t sig;
1734  uint32_t cnt = 0;
1735  int direction = 0;
1736  uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
1737  int sgh_mpm_context = 0;
1738  int sm_list = DETECT_SM_LIST_PMATCH;
1739  uint8_t *sids_array = SCCalloc(1, max_sid);
1740  if (sids_array == NULL) {
1741  return NULL;
1742  }
1743  switch (buf) {
1744  case MPMB_TCP_PKT_TS:
1745  case MPMB_TCP_PKT_TC:
1746  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_tcp_packet;
1747  break;
1748  case MPMB_TCP_STREAM_TS:
1749  case MPMB_TCP_STREAM_TC:
1750  sgh_mpm_context = de_ctx->sgh_mpm_context_stream;
1751  break;
1752  case MPMB_UDP_TS:
1753  case MPMB_UDP_TC:
1754  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_udp_packet;
1755  break;
1756  case MPMB_OTHERIP:
1757  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_other_packet;
1758  break;
1759  default:
1760  break;
1761  }
1762 
1763  switch(buf) {
1764  case MPMB_TCP_PKT_TS:
1765  case MPMB_TCP_STREAM_TS:
1766  case MPMB_UDP_TS:
1767  direction = SIG_FLAG_TOSERVER;
1768  break;
1769 
1770  case MPMB_TCP_PKT_TC:
1771  case MPMB_TCP_STREAM_TC:
1772  case MPMB_UDP_TC:
1773  direction = SIG_FLAG_TOCLIENT;
1774  break;
1775 
1776  case MPMB_OTHERIP:
1777  direction = (SIG_FLAG_TOCLIENT|SIG_FLAG_TOSERVER);
1778  break;
1779 
1780  case MPMB_MAX:
1781  BUG_ON(1);
1782  break;
1783  }
1784 
1785  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
1786  s = sgh->init->match_array[sig];
1787  if (s == NULL)
1788  continue;
1789 
1790  if (s->init_data->mpm_sm == NULL)
1791  continue;
1792 
1793  int list = s->init_data->mpm_sm_list;
1794  if (list < 0)
1795  continue;
1796 
1797  if (list != DETECT_SM_LIST_PMATCH)
1798  continue;
1799 
1800  switch (buf) {
1801  case MPMB_TCP_PKT_TS:
1802  case MPMB_TCP_PKT_TC:
1803  if (SignatureHasPacketContent(s) == 1)
1804  {
1805  sids_array[s->iid / 8] |= 1 << (s->iid % 8);
1806  cnt++;
1807  }
1808  break;
1809  case MPMB_TCP_STREAM_TS:
1810  case MPMB_TCP_STREAM_TC:
1811  if (SignatureHasStreamContent(s) == 1)
1812  {
1813  sids_array[s->iid / 8] |= 1 << (s->iid % 8);
1814  cnt++;
1815  }
1816  break;
1817  case MPMB_UDP_TS:
1818  case MPMB_UDP_TC:
1819  sids_array[s->iid / 8] |= 1 << (s->iid % 8);
1820  cnt++;
1821  break;
1822  case MPMB_OTHERIP:
1823  sids_array[s->iid / 8] |= 1 << (s->iid % 8);
1824  cnt++;
1825  break;
1826  default:
1827  break;
1828  }
1829  }
1830 
1831  if (cnt == 0) {
1832  SCFree(sids_array);
1833  return NULL;
1834  }
1835 
1836  MpmStore lookup = { sids_array, max_sid, direction, buf, sm_list, 0, 0, NULL };
1837 
1838  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1839  if (result == NULL) {
1840  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1841  if (copy == NULL)
1842  return NULL;
1843  uint8_t *sids = SCCalloc(1, max_sid);
1844  if (sids == NULL) {
1845  SCFree(copy);
1846  SCFree(sids_array);
1847  return NULL;
1848  }
1849 
1850  memcpy(sids, sids_array, max_sid);
1851  copy->sid_array = sids;
1852  copy->sid_array_size = max_sid;
1853  copy->buffer = buf;
1854  copy->direction = direction;
1855  copy->sm_list = sm_list;
1856  copy->sgh_mpm_context = sgh_mpm_context;
1857 
1858  MpmStoreSetup(de_ctx, copy);
1859  MpmStoreAdd(de_ctx, copy);
1860  SCFree(sids_array);
1861  return copy;
1862  } else {
1863  SCFree(sids_array);
1864  return result;
1865  }
1866 }
1867 
1868 struct SidsArray {
1869  uint8_t *sids_array;
1871  /* indicates this has an active engine */
1872  bool active;
1873 
1875 };
1876 
1877 static MpmStore *MpmStorePrepareBufferAppLayer(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1878  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1879 {
1880  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1881  return NULL;
1882 
1883  SCLogDebug("handling %s direction %s for list %d", am->name,
1884  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1885  am->sm_list);
1886 
1887  MpmStore lookup = { sa->sids_array, sa->sids_array_size, am->direction, MPMB_MAX, am->sm_list,
1888  0, am->app_v2.alproto, NULL };
1889  SCLogDebug("am->direction %d am->sm_list %d sgh_mpm_context %d", am->direction, am->sm_list,
1890  am->sgh_mpm_context);
1891 
1892  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1893  if (result == NULL) {
1894  SCLogDebug("new unique mpm for %s %s", am->name,
1895  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient");
1896 
1897  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1898  if (copy == NULL)
1899  return NULL;
1900  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1901  if (sids == NULL) {
1902  SCFree(copy);
1903  return NULL;
1904  }
1905 
1906  memcpy(sids, sa->sids_array, sa->sids_array_size);
1907  copy->sid_array = sids;
1908  copy->sid_array_size = sa->sids_array_size;
1909  copy->buffer = MPMB_MAX;
1910  copy->direction = am->direction;
1911  copy->sm_list = am->sm_list;
1912  copy->sgh_mpm_context = am->sgh_mpm_context;
1913  copy->alproto = am->app_v2.alproto;
1914 
1915  MpmStoreSetup(de_ctx, copy);
1916  MpmStoreAdd(de_ctx, copy);
1917  return copy;
1918  } else {
1919  SCLogDebug("using existing mpm %p", result);
1920  return result;
1921  }
1922  return NULL;
1923 }
1924 
1925 static MpmStore *MpmStorePrepareBufferPkt(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1926  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1927 {
1928  SCLogDebug("handling %s for list %d", am->name,
1929  am->sm_list);
1930 
1931  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1932  return NULL;
1933 
1935  MPMB_MAX, am->sm_list, 0, 0, NULL };
1936  SCLogDebug("am->sm_list %d", am->sm_list);
1937 
1938  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1939  if (result == NULL) {
1940  SCLogDebug("new unique mpm for %s", am->name);
1941 
1942  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1943  if (copy == NULL)
1944  return NULL;
1945  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1946  if (sids == NULL) {
1947  SCFree(copy);
1948  return NULL;
1949  }
1950 
1951  memcpy(sids, sa->sids_array, sa->sids_array_size);
1952  copy->sid_array = sids;
1953  copy->sid_array_size = sa->sids_array_size;
1954  copy->buffer = MPMB_MAX;
1956  copy->sm_list = am->sm_list;
1957  copy->sgh_mpm_context = am->sgh_mpm_context;
1958 
1959  MpmStoreSetup(de_ctx, copy);
1960  MpmStoreAdd(de_ctx, copy);
1961  return copy;
1962  } else {
1963  SCLogDebug("using existing mpm %p", result);
1964  return result;
1965  }
1966  return NULL;
1967 }
1968 
1969 static MpmStore *MpmStorePrepareBufferFrame(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
1970  const DetectBufferMpmRegistry *am, const struct SidsArray *sa)
1971 {
1972  SCLogDebug("handling %s for list %d", am->name, am->sm_list);
1973 
1974  if (sa->sids_array_size == 0 || sa->sids_array == NULL)
1975  return NULL;
1976 
1977  MpmStore lookup = { sa->sids_array, sa->sids_array_size, am->direction, MPMB_MAX, am->sm_list,
1978  0, am->frame_v1.alproto, NULL };
1979  SCLogDebug("am->sm_list %d", am->sm_list);
1980 
1981  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1982  if (result == NULL) {
1983  SCLogDebug("new unique mpm for %s", am->name);
1984 
1985  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1986  if (copy == NULL)
1987  return NULL;
1988  uint8_t *sids = SCCalloc(1, sa->sids_array_size);
1989  if (sids == NULL) {
1990  SCFree(copy);
1991  return NULL;
1992  }
1993 
1994  memcpy(sids, sa->sids_array, sa->sids_array_size);
1995  copy->sid_array = sids;
1996  copy->sid_array_size = sa->sids_array_size;
1997  copy->buffer = MPMB_MAX;
1998  copy->direction = am->direction;
1999  copy->sm_list = am->sm_list;
2000  copy->sgh_mpm_context = am->sgh_mpm_context;
2001  copy->alproto = am->frame_v1.alproto;
2002 
2003  MpmStoreSetup(de_ctx, copy);
2004  MpmStoreAdd(de_ctx, copy);
2005  return copy;
2006  } else {
2007  SCLogDebug("using existing mpm %p", result);
2008  return result;
2009  }
2010  return NULL;
2011 }
2012 
2013 static void SetRawReassemblyFlag(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
2014 {
2015  const Signature *s = NULL;
2016  uint32_t sig;
2017 
2018  for (sig = 0; sig < sgh->init->sig_cnt; sig++) {
2019  s = sgh->init->match_array[sig];
2020  if (s == NULL)
2021  continue;
2022 
2023  if (SignatureHasStreamContent(s) == 1) {
2025  SCLogDebug("rule group %p has SIG_GROUP_HEAD_HAVERAWSTREAM set", sgh);
2026  return;
2027  }
2028  }
2029  SCLogDebug("rule group %p does NOT have SIG_GROUP_HEAD_HAVERAWSTREAM set", sgh);
2030 }
2031 
2032 typedef struct DetectBufferInstance {
2033  // key
2034  int list;
2036 
2037  struct SidsArray ts;
2038  struct SidsArray tc;
2040 
2041 static uint32_t DetectBufferInstanceHashFunc(HashListTable *ht, void *data, uint16_t datalen)
2042 {
2043  const DetectBufferInstance *ms = (const DetectBufferInstance *)data;
2044  uint32_t hash = ms->list + ms->alproto;
2045  return hash % ht->array_size;
2046 }
2047 
2048 static char DetectBufferInstanceCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
2049 {
2050  const DetectBufferInstance *ms1 = (DetectBufferInstance *)data1;
2051  const DetectBufferInstance *ms2 = (DetectBufferInstance *)data2;
2052  return (ms1->list == ms2->list && ms1->alproto == ms2->alproto);
2053 }
2054 
2055 static void DetectBufferInstanceFreeFunc(void *ptr)
2056 {
2057  DetectBufferInstance *ms = ptr;
2058  if (ms->ts.sids_array != NULL)
2059  SCFree(ms->ts.sids_array);
2060  if (ms->tc.sids_array != NULL)
2061  SCFree(ms->tc.sids_array);
2062  SCFree(ms);
2063 }
2064 
2065 static HashListTable *DetectBufferInstanceInit(void)
2066 {
2067  return HashListTableInit(4096, DetectBufferInstanceHashFunc, DetectBufferInstanceCompareFunc,
2068  DetectBufferInstanceFreeFunc);
2069 }
2070 
2071 static void PrepareMpms(DetectEngineCtx *de_ctx, SigGroupHead *sh)
2072 {
2073  HashListTable *bufs = DetectBufferInstanceInit();
2074  BUG_ON(bufs == NULL);
2075 
2076  const int max_buffer_id = de_ctx->buffer_type_id + 1;
2077  const uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
2078 
2079  AppProto engines[max_buffer_id][g_alproto_max];
2080  memset(engines, 0, sizeof(engines));
2081  int engines_idx[max_buffer_id];
2082  memset(engines_idx, 0, sizeof(engines_idx));
2083  int types[max_buffer_id];
2084  memset(types, 0, sizeof(types));
2085 
2086  /* flag the list+directions we have engines for as active */
2087  for (DetectBufferMpmRegistry *a = de_ctx->pkt_mpms_list; a != NULL; a = a->next) {
2088  types[a->sm_list] = a->type;
2089 
2090  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = ALPROTO_UNKNOWN };
2091  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2092  if (instance == NULL) {
2093  instance = SCCalloc(1, sizeof(*instance));
2094  BUG_ON(instance == NULL);
2095  instance->list = a->sm_list;
2096  instance->alproto = ALPROTO_UNKNOWN;
2097  HashListTableAdd(bufs, instance, 0);
2098  }
2099  instance->ts.active = true;
2100  instance->tc.active = true;
2101  }
2102  for (DetectBufferMpmRegistry *a = de_ctx->frame_mpms_list; a != NULL; a = a->next) {
2103  const bool add_ts = ((a->direction == SIG_FLAG_TOSERVER) && SGH_DIRECTION_TS(sh));
2104  const bool add_tc = ((a->direction == SIG_FLAG_TOCLIENT) && SGH_DIRECTION_TC(sh));
2105  if (add_ts || add_tc) {
2106  types[a->sm_list] = a->type;
2107  engines[a->sm_list][engines_idx[a->sm_list]++] = a->frame_v1.alproto;
2108 
2109  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->frame_v1.alproto };
2110  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2111  if (instance == NULL) {
2112  instance = SCCalloc(1, sizeof(*instance));
2113  BUG_ON(instance == NULL);
2114  instance->list = a->sm_list;
2115  instance->alproto = a->frame_v1.alproto;
2116  HashListTableAdd(bufs, instance, 0);
2117  }
2118  instance->ts.active |= add_ts;
2119  instance->tc.active |= add_tc;
2120  }
2121  }
2122  for (DetectBufferMpmRegistry *a = de_ctx->app_mpms_list; a != NULL; a = a->next) {
2123  const bool add_ts = ((a->direction == SIG_FLAG_TOSERVER) && SGH_DIRECTION_TS(sh));
2124  const bool add_tc = ((a->direction == SIG_FLAG_TOCLIENT) && SGH_DIRECTION_TC(sh));
2125  if (add_ts || add_tc) {
2126  types[a->sm_list] = a->type;
2127  engines[a->sm_list][engines_idx[a->sm_list]++] = a->app_v2.alproto;
2128 
2129  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->app_v2.alproto };
2130  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2131  if (instance == NULL) {
2132  instance = SCCalloc(1, sizeof(*instance));
2133  BUG_ON(instance == NULL);
2134  instance->list = a->sm_list;
2135  instance->alproto = a->app_v2.alproto;
2136  HashListTableAdd(bufs, instance, 0);
2137  }
2138  instance->ts.active |= add_ts;
2139  instance->tc.active |= add_tc;
2140  }
2141  }
2142 
2143  for (uint32_t sig = 0; sig < sh->init->sig_cnt; sig++) {
2144  const Signature *s = sh->init->match_array[sig];
2145  if (s == NULL)
2146  continue;
2147  if (s->init_data->mpm_sm == NULL)
2148  continue;
2149  const int list = s->init_data->mpm_sm_list;
2150  if (list < 0)
2151  continue;
2152  if (list == DETECT_SM_LIST_PMATCH)
2153  continue;
2154 
2155  switch (types[list]) {
2156  /* app engines are direction aware */
2159  for (int e = 0; e < engines_idx[list]; e++) {
2160  const AppProto alproto = engines[list][e];
2161  if (!(AppProtoEquals(s->alproto, alproto) || s->alproto == 0))
2162  continue;
2163 
2164  DetectBufferInstance lookup = { .list = list, .alproto = alproto };
2165  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2166  if (instance == NULL)
2167  continue;
2168  if (s->flags & SIG_FLAG_TOSERVER) {
2169  struct SidsArray *sa = &instance->ts;
2170  if (sa->active) {
2171  if (sa->sids_array == NULL) {
2172  sa->sids_array = SCCalloc(1, max_sid);
2173  sa->sids_array_size = max_sid;
2174  BUG_ON(sa->sids_array == NULL); // TODO
2175  }
2176  sa->sids_array[s->iid / 8] |= 1 << (s->iid % 8);
2177  SCLogDebug("instance %p: stored %u/%u ts", instance, s->id, s->iid);
2178  }
2179  }
2180  if (s->flags & SIG_FLAG_TOCLIENT) {
2181  struct SidsArray *sa = &instance->tc;
2182  if (sa->active) {
2183  if (sa->sids_array == NULL) {
2184  sa->sids_array = SCCalloc(1, max_sid);
2185  sa->sids_array_size = max_sid;
2186  BUG_ON(sa->sids_array == NULL); // TODO
2187  }
2188  sa->sids_array[s->iid / 8] |= 1 << (s->iid % 8);
2189  SCLogDebug("instance %p: stored %u/%u tc", instance, s->id, s->iid);
2190  }
2191  }
2192  }
2193  break;
2194  }
2195  /* pkt engines are directionless, so only use ts */
2197  DetectBufferInstance lookup = { .list = list, .alproto = ALPROTO_UNKNOWN };
2198  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2199  if (instance == NULL)
2200  continue;
2201  struct SidsArray *sa = &instance->ts;
2202  if (sa->active) {
2203  if (sa->sids_array == NULL) {
2204  sa->sids_array = SCCalloc(1, max_sid);
2205  sa->sids_array_size = max_sid;
2206  BUG_ON(sa->sids_array == NULL); // TODO
2207  }
2208  sa->sids_array[s->iid / 8] |= 1 << (s->iid % 8);
2209  }
2210  break;
2211  }
2212  default:
2213  BUG_ON(1);
2214  }
2215  }
2216 
2217  sh->init->app_mpms = SCCalloc(de_ctx->app_mpms_list_cnt, sizeof(MpmCtx *));
2218  BUG_ON(sh->init->app_mpms == NULL);
2219 
2220  sh->init->pkt_mpms = SCCalloc(de_ctx->pkt_mpms_list_cnt, sizeof(MpmCtx *));
2221  BUG_ON(sh->init->pkt_mpms == NULL);
2222 
2224  BUG_ON(sh->init->frame_mpms == NULL);
2225 
2226  for (DetectBufferMpmRegistry *a = de_ctx->pkt_mpms_list; a != NULL; a = a->next) {
2227  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = ALPROTO_UNKNOWN };
2228  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2229  if (instance == NULL) {
2230  continue;
2231  }
2232  struct SidsArray *sa = &instance->ts;
2233  if (!sa->active)
2234  continue;
2235 
2236  MpmStore *mpm_store = MpmStorePrepareBufferPkt(de_ctx, sh, a, sa);
2237  if (mpm_store != NULL) {
2238  sh->init->pkt_mpms[a->id] = mpm_store->mpm_ctx;
2239 
2240  SCLogDebug("a %p a->name %s a->reg->PrefilterRegisterWithListId %p "
2241  "mpm_store->mpm_ctx %p", a, a->name,
2242  a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2243 
2244  /* if we have just certain types of negated patterns,
2245  * mpm_ctx can be NULL */
2246  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2247  BUG_ON(a->PrefilterRegisterWithListId(de_ctx,
2248  sh, mpm_store->mpm_ctx,
2249  a, a->sm_list) != 0);
2250  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2251  }
2252  }
2253  }
2254  for (DetectBufferMpmRegistry *a = de_ctx->frame_mpms_list; a != NULL; a = a->next) {
2255  if ((a->direction == SIG_FLAG_TOSERVER && SGH_DIRECTION_TS(sh)) ||
2256  (a->direction == SIG_FLAG_TOCLIENT && SGH_DIRECTION_TC(sh))) {
2257  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->frame_v1.alproto };
2258  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2259  if (instance == NULL) {
2260  continue;
2261  }
2262  struct SidsArray *sa =
2263  (a->direction == SIG_FLAG_TOSERVER) ? &instance->ts : &instance->tc;
2264  if (!sa->active)
2265  continue;
2266 
2267  SCLogDebug("a %s direction %d PrefilterRegisterWithListId %p", a->name, a->direction,
2268  a->PrefilterRegisterWithListId);
2269  MpmStore *mpm_store = MpmStorePrepareBufferFrame(de_ctx, sh, a, sa);
2270  if (mpm_store != NULL) {
2271  sh->init->frame_mpms[a->id] = mpm_store->mpm_ctx;
2272 
2273  SCLogDebug("a %p a->name %s a->reg->PrefilterRegisterWithListId %p "
2274  "mpm_store->mpm_ctx %p",
2275  a, a->name, a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2276 
2277  /* if we have just certain types of negated patterns,
2278  * mpm_ctx can be NULL */
2279  SCLogDebug("mpm_store %p mpm_ctx %p", mpm_store, mpm_store->mpm_ctx);
2280  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2281  BUG_ON(a->PrefilterRegisterWithListId(
2282  de_ctx, sh, mpm_store->mpm_ctx, a, a->sm_list) != 0);
2283  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2284  }
2285  }
2286  }
2287  }
2288  for (DetectBufferMpmRegistry *a = de_ctx->app_mpms_list; a != NULL; a = a->next) {
2289  if ((a->direction == SIG_FLAG_TOSERVER && SGH_DIRECTION_TS(sh)) ||
2290  (a->direction == SIG_FLAG_TOCLIENT && SGH_DIRECTION_TC(sh))) {
2291 
2292  DetectBufferInstance lookup = { .list = a->sm_list, .alproto = a->app_v2.alproto };
2293  DetectBufferInstance *instance = HashListTableLookup(bufs, &lookup, 0);
2294  if (instance == NULL) {
2295  continue;
2296  }
2297  struct SidsArray *sa =
2298  (a->direction == SIG_FLAG_TOSERVER) ? &instance->ts : &instance->tc;
2299  if (!sa->active)
2300  continue;
2301 
2302  MpmStore *mpm_store = MpmStorePrepareBufferAppLayer(de_ctx, sh, a, sa);
2303  if (mpm_store != NULL) {
2304  sh->init->app_mpms[a->id] = mpm_store->mpm_ctx;
2305 
2306  SCLogDebug("a %p a->name %s a->PrefilterRegisterWithListId %p "
2307  "mpm_store->mpm_ctx %p",
2308  a, a->name, a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
2309 
2310  /* if we have just certain types of negated patterns,
2311  * mpm_ctx can be NULL */
2312  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
2313  BUG_ON(a->PrefilterRegisterWithListId(
2314  de_ctx, sh, mpm_store->mpm_ctx, a, a->sm_list) != 0);
2315  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
2316  }
2317  }
2318  }
2319  }
2320  HashListTableFree(bufs);
2321 }
2322 
2323 /** \brief Prepare the pattern matcher ctx in a sig group head.
2324  *
2325  */
2327 {
2328  MpmStore *mpm_store = NULL;
2329  if (SGH_PROTO(sh, IPPROTO_TCP)) {
2330  if (SGH_DIRECTION_TS(sh)) {
2331  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_PKT_TS);
2332  if (mpm_store != NULL) {
2333  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2334  }
2335 
2337  if (mpm_store != NULL) {
2338  PrefilterPktStreamRegister(de_ctx, sh, mpm_store->mpm_ctx);
2339  }
2340 
2341  SetRawReassemblyFlag(de_ctx, sh);
2342  }
2343  if (SGH_DIRECTION_TC(sh)) {
2344  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_PKT_TC);
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  } else if (SGH_PROTO(sh, IPPROTO_UDP)) {
2357  if (SGH_DIRECTION_TS(sh)) {
2358  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_UDP_TS);
2359  if (mpm_store != NULL) {
2360  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2361  }
2362  }
2363  if (SGH_DIRECTION_TC(sh)) {
2364  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_UDP_TC);
2365  if (mpm_store != NULL) {
2366  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2367  }
2368  }
2369  } else {
2370  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_OTHERIP);
2371  if (mpm_store != NULL) {
2372  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
2373  }
2374  }
2375 
2376  PrepareMpms(de_ctx, sh);
2377  return 0;
2378 }
2379 
2380 static inline uint32_t ContentFlagsForHash(const DetectContentData *cd)
2381 {
2384 }
2385 
2386 /** \internal
2387  * \brief The hash function for Pattern. Hashes pattern after chop is applied.
2388  *
2389  * \param ht Pointer to the hash table.
2390  * \param data Pointer to the Pattern.
2391  * \param datalen Not used in our case.
2392  *
2393  * \retval hash The generated hash value.
2394  */
2395 static uint32_t PatternChopHashFunc(HashListTable *ht, void *data, uint16_t datalen)
2396 {
2397  const DetectPatternTracker *p = (DetectPatternTracker *)data;
2398  uint32_t hash = p->sm_list + ContentFlagsForHash(p->cd);
2399  uint16_t content_len = p->cd->content_len;
2400  const uint8_t *content = p->cd->content;
2402  content += p->cd->fp_chop_offset;
2403  content_len = p->cd->fp_chop_len;
2404  }
2405  hash += StringHashDjb2(content, content_len);
2406  return hash % ht->array_size;
2407 }
2408 
2409 /** \internal
2410  * \brief The hash function for Pattern. Ignores chop.
2411  *
2412  * \param ht Pointer to the hash table.
2413  * \param data Pointer to the Pattern.
2414  * \param datalen Not used in our case.
2415  *
2416  * \retval hash The generated hash value.
2417  */
2418 static uint32_t PatternNoChopHashFunc(HashListTable *ht, void *data, uint16_t datalen)
2419 {
2420  const DetectPatternTracker *p = (DetectPatternTracker *)data;
2421  uint32_t hash = p->sm_list + ContentFlagsForHash(p->cd);
2422  hash += StringHashDjb2(p->cd->content, p->cd->content_len);
2423  return hash % ht->array_size;
2424 }
2425 
2426 /**
2427  * \brief The Compare function for Pattern. Compares patterns after chop is applied.
2428  *
2429  * \param data1 Pointer to the first Pattern.
2430  * \param len1 Not used.
2431  * \param data2 Pointer to the second Pattern.
2432  * \param len2 Not used.
2433  *
2434  * \retval 1 If the 2 Patterns sent as args match.
2435  * \retval 0 If the 2 Patterns sent as args do not match.
2436  */
2437 static char PatternChopCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
2438 {
2439  const DetectPatternTracker *p1 = (DetectPatternTracker *)data1;
2440  const DetectPatternTracker *p2 = (DetectPatternTracker *)data2;
2441 
2442  if (p1->sm_list != p2->sm_list)
2443  return 0;
2444 
2445  if (ContentFlagsForHash(p1->cd) != ContentFlagsForHash(p2->cd))
2446  return 0;
2447 
2448  uint16_t p1_content_len = p1->cd->content_len;
2449  uint8_t *p1_content = p1->cd->content;
2451  p1_content += p1->cd->fp_chop_offset;
2452  p1_content_len = p1->cd->fp_chop_len;
2453  }
2454  uint16_t p2_content_len = p2->cd->content_len;
2455  uint8_t *p2_content = p2->cd->content;
2457  p2_content += p2->cd->fp_chop_offset;
2458  p2_content_len = p2->cd->fp_chop_len;
2459  }
2460 
2461  if (p1_content_len != p2_content_len)
2462  return 0;
2463 
2464  if (memcmp(p1_content, p2_content, p1_content_len) != 0) {
2465  return 0;
2466  }
2467 
2468  return 1;
2469 }
2470 
2471 /**
2472  * \brief The Compare function for Pattern. Ignores chop settings
2473  *
2474  * \param data1 Pointer to the first Pattern.
2475  * \param len1 Not used.
2476  * \param data2 Pointer to the second Pattern.
2477  * \param len2 Not used.
2478  *
2479  * \retval 1 If the 2 Patterns sent as args match.
2480  * \retval 0 If the 2 Patterns sent as args do not match.
2481  */
2482 static char PatternNoChopCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
2483 {
2484  const DetectPatternTracker *p1 = (DetectPatternTracker *)data1;
2485  const DetectPatternTracker *p2 = (DetectPatternTracker *)data2;
2486 
2487  if (p1->sm_list != p2->sm_list)
2488  return 0;
2489 
2490  if (ContentFlagsForHash(p1->cd) != ContentFlagsForHash(p2->cd))
2491  return 0;
2492 
2493  if (p1->cd->content_len != p2->cd->content_len)
2494  return 0;
2495 
2496  if (memcmp(p1->cd->content, p2->cd->content, p1->cd->content_len) != 0) {
2497  return 0;
2498  }
2499 
2500  return 1;
2501 }
2502 
2503 static void PatternFreeFunc(void *ptr)
2504 {
2505  SCFree(ptr);
2506 }
2507 
2508 /**
2509  * \brief Figure out the FP and their respective content ids for all the
2510  * sigs in the engine.
2511  *
2512  * \param de_ctx Detection engine context.
2513  *
2514  * \retval 0 On success.
2515  * \retval -1 On failure.
2516  */
2518 {
2519  uint32_t cnt = 0;
2520  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
2521  if (s->init_data->mpm_sm != NULL) {
2522  cnt++;
2523  }
2524  }
2525  /* no mpm rules */
2526  if (cnt == 0)
2527  return 0;
2528 
2529  HashListTable *ht =
2530  HashListTableInit(4096, PatternChopHashFunc, PatternChopCompareFunc, PatternFreeFunc);
2531  BUG_ON(ht == NULL);
2532  PatIntId max_id = 0;
2533 
2534  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
2535  if (s->init_data->mpm_sm == NULL)
2536  continue;
2537 
2538  const int sm_list = s->init_data->mpm_sm_list;
2539  BUG_ON(sm_list == -1);
2540 
2542 
2543  DetectPatternTracker lookup = { .cd = cd, .sm_list = sm_list, .cnt = 0, .mpm = 0 };
2544  DetectPatternTracker *res = HashListTableLookup(ht, &lookup, 0);
2545  if (res) {
2546  res->cnt++;
2547  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2548 
2549  cd->id = res->cd->id;
2550  SCLogDebug("%u: res id %u cnt %u", s->id, res->cd->id, res->cnt);
2551  } else {
2552  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2553  BUG_ON(add == NULL);
2554  add->cd = cd;
2555  add->sm_list = sm_list;
2556  add->cnt = 1;
2557  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2558  HashListTableAdd(ht, (void *)add, 0);
2559 
2560  cd->id = max_id++;
2561  SCLogDebug("%u: add id %u cnt %u", s->id, add->cd->id, add->cnt);
2562  }
2563  }
2564 
2565  HashListTableFree(ht);
2566 
2567  return 0;
2568 }
2569 
2570 /** \brief add all patterns on our stats hash
2571  * Used to fill the hash later used by DumpPatterns()
2572  * \note sets up the hash table on first call
2573  */
2575 {
2576  if (de_ctx->pattern_hash_table == NULL) {
2578  4096, PatternNoChopHashFunc, PatternNoChopCompareFunc, PatternFreeFunc);
2579  BUG_ON(de_ctx->pattern_hash_table == NULL);
2580  }
2581  if (s->sm_arrays[DETECT_SM_LIST_PMATCH]) {
2583  do {
2584  switch (smd->type) {
2585  case DETECT_CONTENT: {
2586  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2587  DetectPatternTracker lookup = {
2588  .cd = cd, .sm_list = DETECT_SM_LIST_PMATCH, .cnt = 0, .mpm = 0
2589  };
2590  DetectPatternTracker *res =
2592  if (res) {
2593  res->cnt++;
2594  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2595  } else {
2596  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2597  BUG_ON(add == NULL);
2598  add->cd = cd;
2600  add->cnt = 1;
2601  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2602  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2603  }
2604  break;
2605  }
2606  }
2607  if (smd->is_last)
2608  break;
2609  smd++;
2610  } while (1);
2611  }
2612 
2614  for (; app != NULL; app = app->next) {
2615  SigMatchData *smd = app->smd;
2616  while (smd) {
2617  switch (smd->type) {
2618  case DETECT_CONTENT: {
2619  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2620 
2621  DetectPatternTracker lookup = {
2622  .cd = cd, .sm_list = app->sm_list, .cnt = 0, .mpm = 0
2623  };
2624  DetectPatternTracker *res =
2626  if (res) {
2627  res->cnt++;
2628  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2629  } else {
2630  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2631  BUG_ON(add == NULL);
2632  add->cd = cd;
2633  add->sm_list = app->sm_list;
2634  add->cnt = 1;
2635  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2636  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2637  }
2638  break;
2639  }
2640  }
2641  if (smd->is_last)
2642  break;
2643  smd++;
2644  }
2645  }
2647  for (; pkt != NULL; pkt = pkt->next) {
2648  SigMatchData *smd = pkt->smd;
2649  do {
2650  if (smd == NULL) {
2652  smd = s->sm_arrays[pkt->sm_list];
2653  }
2654  switch (smd->type) {
2655  case DETECT_CONTENT: {
2656  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2657 
2658  DetectPatternTracker lookup = {
2659  .cd = cd, .sm_list = pkt->sm_list, .cnt = 0, .mpm = 0
2660  };
2661  DetectPatternTracker *res =
2663  if (res) {
2664  res->cnt++;
2665  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2666  } else {
2667  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2668  BUG_ON(add == NULL);
2669  add->cd = cd;
2670  add->sm_list = pkt->sm_list;
2671  add->cnt = 1;
2672  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2673  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2674  }
2675  break;
2676  }
2677  }
2678  if (smd->is_last)
2679  break;
2680  smd++;
2681  } while (1);
2682  }
2684  for (; frame != NULL; frame = frame->next) {
2685  SigMatchData *smd = frame->smd;
2686  do {
2687  if (smd == NULL) {
2689  smd = s->sm_arrays[frame->sm_list];
2690  }
2691  switch (smd->type) {
2692  case DETECT_CONTENT: {
2693  const DetectContentData *cd = (const DetectContentData *)smd->ctx;
2694 
2695  DetectPatternTracker lookup = {
2696  .cd = cd, .sm_list = frame->sm_list, .cnt = 0, .mpm = 0
2697  };
2698  DetectPatternTracker *res =
2700  if (res) {
2701  res->cnt++;
2702  res->mpm += ((cd->flags & DETECT_CONTENT_MPM) != 0);
2703  } else {
2704  DetectPatternTracker *add = SCCalloc(1, sizeof(*add));
2705  BUG_ON(add == NULL);
2706  add->cd = cd;
2707  add->sm_list = frame->sm_list;
2708  add->cnt = 1;
2709  add->mpm = ((cd->flags & DETECT_CONTENT_MPM) != 0);
2710  HashListTableAdd(de_ctx->pattern_hash_table, (void *)add, 0);
2711  }
2712  break;
2713  }
2714  }
2715  if (smd->is_last)
2716  break;
2717  smd++;
2718  } while (1);
2719  }
2720 }
MpmInitThreadCtx
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t matcher)
Definition: util-mpm.c:195
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
SignatureHasPacketContent
int SignatureHasPacketContent(const Signature *s)
check if a signature has patterns that are to be inspected against a packets payload (as opposed to t...
Definition: detect-engine-mpm.c:853
DetectEngineCtx_::pkt_mpms_list_cnt
uint32_t pkt_mpms_list_cnt
Definition: detect.h:1091
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:56
DetectEngineCtx_::frame_mpms_list_cnt
uint32_t frame_mpms_list_cnt
Definition: detect.h:1094
DetectContentData_::offset
uint16_t offset
Definition: detect-content.h:107
SignatureInitData_::max_content_list_id
uint32_t max_content_list_id
Definition: detect.h:653
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1720
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:809
SCFPSupportSMList_
Definition: detect.h:840
DetectEngineAppInspectionEngine_
Definition: detect.h:415
util-hash-string.h
MPMB_UDP_TS
@ MPMB_UDP_TS
Definition: detect.h:1501
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:534
detect-content.h
SGH_DIRECTION_TC
#define SGH_DIRECTION_TC(sgh)
Definition: detect-engine-mpm.c:1055
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:99
DETECT_PROFILE_NAME_LEN
#define DETECT_PROFILE_NAME_LEN
Definition: detect.h:761
detect-engine.h
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:119
DetectBufferMpmRegistry_::direction
int direction
Definition: detect.h:766
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:642
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:1509
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:1508
DetectEngineCtx_::sgh_mpm_context_proto_tcp_packet
int32_t sgh_mpm_context_proto_tcp_packet
Definition: detect.h:1005
PatternMatchPrepareGroup
int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh)
Prepare the pattern matcher ctx in a sig group head.
Definition: detect-engine-mpm.c:2326
SCFPSupportSMList_::next
struct SCFPSupportSMList_ * next
Definition: detect.h:843
DetectEnginePktInspectionEngine
Definition: detect.h:483
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:47
DetectEngineAppInspectionEngine_::next
struct DetectEngineAppInspectionEngine_ * next
Definition: detect.h:440
detect-engine-siggroup.h
SigGroupHead_::flags
uint16_t flags
Definition: detect.h:1629
SCFPSupportSMList_::list_id
int list_id
Definition: detect.h:841
SigTableElmt_::name
const char * name
Definition: detect.h:1458
MpmStoreFree
void MpmStoreFree(DetectEngineCtx *de_ctx)
Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by MpmStoreInit() function.
Definition: detect-engine-mpm.c:1621
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:813
DETECT_ABSENT
@ DETECT_ABSENT
Definition: detect-engine-register.h:95
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1628
DetectEngineCtx_::pattern_hash_table
HashListTable * pattern_hash_table
Definition: detect.h:966
DetectEngineTransforms
Definition: detect.h:390
DetectBufferMpmRegistry_::sm_list_base
int16_t sm_list_base
Definition: detect.h:768
MpmFactoryReClaimMpmCtx
void MpmFactoryReClaimMpmCtx(const DetectEngineCtx *de_ctx, MpmCtx *mpm_ctx)
Definition: util-mpm.c:156
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:69
MpmStoreReportStats
void MpmStoreReportStats(const DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:1523
Signature_::alproto
AppProto alproto
Definition: detect.h:673
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
MPMB_OTHERIP
@ MPMB_OTHERIP
Definition: detect.h:1503
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
MPM_TABLE_SIZE
@ MPM_TABLE_SIZE
Definition: util-mpm.h:42
SigMatchData_::is_last
bool is_last
Definition: detect.h:366
DetectBufferTypeSupportsFrames
void DetectBufferTypeSupportsFrames(const char *name)
Definition: detect-engine.c:1238
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:86
DETECT_SM_LIST_DYNAMIC_START
@ DETECT_SM_LIST_DYNAMIC_START
Definition: detect.h:138
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:367
DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED
#define DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED
Definition: detect-content.h:55
MpmStore_::sm_list
int sm_list
Definition: detect.h:1513
PatternStrength
uint32_t PatternStrength(uint8_t *pat, uint16_t patlen)
Predict a strength value for patterns.
Definition: detect-engine-mpm.c:987
DetectEngineCtx_::pkt_mpms_list
DetectBufferMpmRegistry * pkt_mpms_list
Definition: detect.h:1090
DETECT_BUFFER_MPM_TYPE_FRAME
@ DETECT_BUFFER_MPM_TYPE_FRAME
Definition: detect.h:756
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:2517
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:933
DetectEnginePktInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:484
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:937
InspectionBufferGetPktDataPtr
InspectionBuffer *(* InspectionBufferGetPktDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Packet *p, const int list_id)
Definition: detect.h:478
DetectEngineBufferTypeGetNameById
const char * DetectEngineBufferTypeGetNameById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1308
DetectMpmInitializeBuiltinMpms
void DetectMpmInitializeBuiltinMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:783
DetectBufferMpmRegistry_::next
struct DetectBufferMpmRegistry_ * next
Definition: detect.h:805
SIG_FLAG_REQUIRE_STREAM
#define SIG_FLAG_REQUIRE_STREAM
Definition: detect.h:254
DetectPatternTracker::cnt
uint32_t cnt
Definition: detect.h:812
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:763
DE_QUIET
#define DE_QUIET
Definition: detect.h:329
MpmCtx_::maxlen
uint16_t maxlen
Definition: util-mpm.h:109
MPMB_TCP_STREAM_TS
@ MPMB_TCP_STREAM_TS
Definition: detect.h:1499
DetectPatternTracker::cd
const struct DetectContentData_ * cd
Definition: detect.h:810
PatIntId
#define PatIntId
Definition: suricata-common.h:335
SIG_GROUP_HEAD_HAVERAWSTREAM
#define SIG_GROUP_HEAD_HAVERAWSTREAM
Definition: detect.h:1487
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:731
DetectContentData_
Definition: detect-content.h:93
DetectContentData_::fp_chop_offset
uint16_t fp_chop_offset
Definition: detect-content.h:100
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:245
detect-engine-payload.h
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
MAX
#define MAX(x, y)
Definition: suricata-common.h:412
MPMB_MAX
@ MPMB_MAX
Definition: detect.h:1504
SigMatchData_
Data needed for Match()
Definition: detect.h:364
DetectEngineCtx_::sgh_mpm_context_proto_udp_packet
int32_t sgh_mpm_context_proto_udp_packet
Definition: detect.h:1006
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:776
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:365
DetectEngineRegisterFastPatternForId
void DetectEngineRegisterFastPatternForId(DetectEngineCtx *de_ctx, int list_id, int priority)
Definition: detect-fast-pattern.c:134
SidsArray
Definition: detect-engine-mpm.c:1868
MPMB_TCP_STREAM_TC
@ MPMB_TCP_STREAM_TC
Definition: detect.h:1500
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:2574
Signature_::frame_inspect
DetectEngineFrameInspectionEngine * frame_inspect
Definition: detect.h:727
MpmBuiltinBuffers
MpmBuiltinBuffers
Definition: detect.h:1496
MpmConfig_::cache_dir_path
const char * cache_dir_path
Definition: util-mpm.h:92
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1278
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:1611
MpmInitCtx
void MpmInitCtx(MpmCtx *mpm_ctx, uint8_t matcher)
Definition: util-mpm.c:209
Signature_::next
struct Signature_ * next
Definition: detect.h:750
DetectEngineCtx_::sgh_mpm_context_proto_other_packet
int32_t sgh_mpm_context_proto_other_packet
Definition: detect.h:1007
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:423
DetectBufferInstance::alproto
AppProto alproto
Definition: detect-engine-mpm.c:2035
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:55
DetectBufferMpmType
DetectBufferMpmType
Definition: detect.h:753
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:765
util-debug.h
DetectBufferMpmRegistry_::frame_v1
struct DetectBufferMpmRegistry_::@90::@94 frame_v1
DetectBufferInstance::ts
struct SidsArray ts
Definition: detect-engine-mpm.c:2037
SigGroupHeadInitData_::sig_cnt
SigIntId sig_cnt
Definition: detect.h:1621
SidsArray::sids_array_size
uint32_t sids_array_size
Definition: detect-engine-mpm.c:1870
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:772
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
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:486
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:1507
SignatureInitData_::mpm_sm
SigMatch * mpm_sm
Definition: detect.h:623
ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE
@ ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE
Definition: detect.h:1185
DetectBufferMpmRegistry_::sm_list
int16_t sm_list
Definition: detect.h:767
DetectEngineGetMaxSigId
#define DetectEngineGetMaxSigId(de_ctx)
Definition: detect-engine.h:93
SignatureInitData_::mpm_sm_list
int mpm_sm_list
Definition: detect.h:621
SidsArray::sids_array
uint8_t * sids_array
Definition: detect-engine-mpm.c:1869
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:726
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:513
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:359
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:936
DETECT_CONTENT_IS_SINGLE
#define DETECT_CONTENT_IS_SINGLE(c)
Definition: detect-content.h:68
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1577
DetectBufferMpmRegistry_::priority
int priority
Definition: detect.h:769
PatternMatchThreadPrepare
void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
Definition: detect-engine-mpm.c:968
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:1365
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1645
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:725
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:108
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:358
DetectProto_::proto
uint8_t proto[256/8]
Definition: detect-engine-proto.h:36
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
MpmStore_::direction
int direction
Definition: detect.h:1511
DetectBufferToClient
bool DetectBufferToClient(const DetectEngineCtx *de_ctx, int buf_id, AppProto alproto)
Definition: detect-engine-mpm.c:1124
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:669
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:1008
type
uint16_t type
Definition: decode-vlan.c:106
DetectEngineBufferTypeSupportsFrames
void DetectEngineBufferTypeSupportsFrames(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1395
DetectEngineCtx_::frame_mpms_list
DetectBufferMpmRegistry * frame_mpms_list
Definition: detect.h:1093
conf.h
DetectEngineCtx_::sgh_mpm_ctx_cnf
uint8_t sgh_mpm_ctx_cnf
Definition: detect.h:1034
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:1515
DetectEngineFrameInspectionEngine
Definition: detect.h:508
DETECT_BUFFER_MPM_TYPE_PKT
@ DETECT_BUFFER_MPM_TYPE_PKT
Definition: detect.h:754
ALPROTO_DOH2
@ ALPROTO_DOH2
Definition: app-layer-protos.h:66
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:747
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:842
HashListTable_
Definition: util-hashlist.h:37
DetectEngineTransforms::transforms
TransformData transforms[DETECT_TRANSFORMS_MAX]
Definition: detect.h:391
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
SidsArray::type
enum DetectBufferMpmType type
Definition: detect-engine-mpm.c:1874
SigGroupHeadInitData_::app_mpms
MpmCtx ** app_mpms
Definition: detect.h:1610
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:1419
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:1054
DetectEngineAppInspectionEngine_::alproto
AppProto alproto
Definition: detect.h:416
DetectPatternTracker::sm_list
int sm_list
Definition: detect.h:811
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:438
DetectBufferInstance
Definition: detect-engine-mpm.c:2032
MpmStore_::mpm_ctx
MpmCtx * mpm_ctx
Definition: detect.h:1516
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:1729
MPMB_TCP_PKT_TC
@ MPMB_TCP_PKT_TC
Definition: detect.h:1498
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
Signature_::proto
DetectProto proto
Definition: detect.h:687
DetectBufferMpmRegistry_::type
enum DetectBufferMpmType type
Definition: detect.h:771
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:1457
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:1085
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:755
DetectBufferTypeSupportsMpm
void DetectBufferTypeSupportsMpm(const char *name)
Definition: detect-engine.c:1258
DetectBufferInstance::list
int list
Definition: detect-engine-mpm.c:2034
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:88
SigGroupHeadInitData_::match_array
Signature ** match_array
Definition: detect.h:1624
DetectBufferMpmRegistry_::name
const char * name
Definition: detect.h:764
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:521
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:241
DetectEnginePktInspectionEngine::next
struct DetectEnginePktInspectionEngine * next
Definition: detect.h:494
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:942
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:386
DetectEngineBufferTypeSupportsMpm
void DetectEngineBufferTypeSupportsMpm(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1411
util-validate.h
detect-flow.h
DetectEngineCtx_::app_mpms_list_cnt
uint32_t app_mpms_list_cnt
Definition: detect.h:1084
DetectBufferTypeSupportsTransformations
void DetectBufferTypeSupportsTransformations(const char *name)
Definition: detect-engine.c:1268
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:647
DetectEngineCtx_::app_inspect_engines
DetectEngineAppInspectionEngine * app_inspect_engines
Definition: detect.h:1088
g_skip_prefilter
int g_skip_prefilter
Definition: detect-engine-mpm.c:1121
DetectEngineCtx_::mpm_hash_table
HashListTable * mpm_hash_table
Definition: detect.h:965
MpmTableElmt_::Prepare
int(* Prepare)(MpmConfig *, struct MpmCtx_ *)
Definition: util-mpm.h:179
MPMB_UDP_TC
@ MPMB_UDP_TC
Definition: detect.h:1502
MpmTableElmt_::DestroyCtx
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:158
Signature_::iid
SigIntId iid
Definition: detect.h:680
DetectBufferMpmRegistry_::id
int id
Definition: detect.h:770
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:713
DETECT_CONTENT_OFFSET
#define DETECT_CONTENT_OFFSET
Definition: detect-content.h:32
HashListTableBucket_
Definition: util-hashlist.h:28
DetectBufferMpmRegistry_::PrefilterRegisterWithListId
int(* PrefilterRegisterWithListId)(struct DetectEngineCtx_ *de_ctx, struct SigGroupHead_ *sgh, MpmCtx *mpm_ctx, const struct DetectBufferMpmRegistry_ *mpm_reg, int list_id)
Definition: detect.h:774
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:525
Signature_
Signature container.
Definition: detect.h:668
SigMatch_
a single match condition for a signature
Definition: detect.h:355
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
MPMB_TCP_PKT_TS
@ MPMB_TCP_PKT_TS
Definition: detect.h:1497
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:392
suricata.h
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:951
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:417
DETECT_BUFFER_MPM_TYPE_SIZE
@ DETECT_BUFFER_MPM_TYPE_SIZE
Definition: detect.h:758
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DetectBufferInstance::tc
struct SidsArray tc
Definition: detect-engine-mpm.c:2038
SignatureInitDataBuffer_::only_tc
bool only_tc
Definition: detect.h:531
DetectEngineCtx_::buffer_type_id
uint32_t buffer_type_id
Definition: detect.h:1082
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:935
MpmCtx_
Definition: util-mpm.h:97
SGH_PROTO
#define SGH_PROTO(sgh, p)
Definition: detect-engine-mpm.c:1053
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:1872
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:648
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:1107
MpmStore_::sgh_mpm_context
int32_t sgh_mpm_context
Definition: detect.h:1514
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
PatternMatchThreadDestroy
void PatternMatchThreadDestroy(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
Definition: detect-engine-mpm.c:963
MpmStore_::buffer
enum MpmBuiltinBuffers buffer
Definition: detect.h:1512
RetrieveFPForSig
void RetrieveFPForSig(const DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine-mpm.c:1143
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:1612
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253
DetectEngineFrameInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:520