suricata
detect-engine-mpm.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2014 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 #include "detect-engine-dns.h"
57 
58 #include "stream.h"
59 
60 #include "util-misc.h"
61 #include "util-enum.h"
62 #include "util-debug.h"
63 #include "util-print.h"
64 #include "util-validate.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 /* Registery for mpm keywords
78  *
79  * Keywords are registered at engine start up
80  */
81 
82 static DetectBufferMpmRegistery *g_mpm_list[DETECT_BUFFER_MPM_TYPE_SIZE] = { NULL, NULL };
83 static int g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_SIZE] = { 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 void DetectAppLayerMpmRegister2(const char *name,
90  int direction, int priority,
91  int (*PrefilterRegister)(DetectEngineCtx *de_ctx,
92  SigGroupHead *sgh, MpmCtx *mpm_ctx,
93  const DetectBufferMpmRegistery *mpm_reg, int list_id),
95  AppProto alproto, int tx_min_progress)
96 {
97  SCLogDebug("registering %s/%d/%d/%p/%p/%u/%d", name, direction, priority,
98  PrefilterRegister, GetData, alproto, tx_min_progress);
99 
100  if (PrefilterRegister == PrefilterGenericMpmRegister && GetData == NULL) {
101  // must register GetData with PrefilterGenericMpmRegister
102  abort();
103  }
104 
107  int sm_list = DetectBufferTypeGetByName(name);
108  if (sm_list == -1) {
110  "MPM engine registration for %s failed", name);
111  }
112 
113  DetectBufferMpmRegistery *am = SCCalloc(1, sizeof(*am));
114  BUG_ON(am == NULL);
115  am->name = name;
116  snprintf(am->pname, sizeof(am->pname), "%s", am->name);
117  am->direction = direction;
118  am->sm_list = sm_list;
119  am->priority = priority;
121 
122  am->PrefilterRegisterWithListId = PrefilterRegister;
123  am->app_v2.GetData = GetData;
124  am->app_v2.alproto = alproto;
125  am->app_v2.tx_min_progress = tx_min_progress;
126 
127  if (g_mpm_list[DETECT_BUFFER_MPM_TYPE_APP] == NULL) {
128  g_mpm_list[DETECT_BUFFER_MPM_TYPE_APP] = am;
129  } else {
131  while (t->next != NULL) {
132  t = t->next;
133  }
134 
135  t->next = am;
136  am->id = t->id + 1;
137  }
138  g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_APP]++;
139 
140  SupportFastPatternForSigMatchList(sm_list, priority);
141 }
142 
143 /** \brief copy a mpm engine from parent_id, add in transforms */
145  const int id, const int parent_id,
146  DetectEngineTransforms *transforms)
147 {
148  SCLogDebug("registering %d/%d", id, parent_id);
149 
151  while (t) {
152  if (t->sm_list == parent_id) {
153  DetectBufferMpmRegistery *am = SCCalloc(1, sizeof(*am));
154  BUG_ON(am == NULL);
155  am->name = t->name;
156  am->direction = t->direction;
157  am->sm_list = id; // use new id
160  am->app_v2.GetData = t->app_v2.GetData;
161  am->app_v2.alproto = t->app_v2.alproto;
162  am->app_v2.tx_min_progress = t->app_v2.tx_min_progress;
163  am->priority = t->priority;
165  am->next = t->next;
166  if (transforms) {
167  memcpy(&am->transforms, transforms, sizeof(*transforms));
168 
169  /* create comma separated string of the names of the
170  * transforms and then shorten it if necessary. Finally
171  * use it to construct the 'profile' name for the engine */
172  char xforms[1024] = "";
173  for (int i = 0; i < transforms->cnt; i++) {
174  char ttstr[64];
175  (void)snprintf(ttstr,sizeof(ttstr), "%s,",
176  sigmatch_table[transforms->transforms[i]].name);
177  strlcat(xforms, ttstr, sizeof(xforms));
178  }
179  xforms[strlen(xforms)-1] = '\0';
180 
181  size_t space = sizeof(am->pname) - strlen(am->name) - 3;
182  char toprint[space + 1];
183  memset(toprint, 0x00, space + 1);
184  if (space < strlen(xforms)) {
185  ShortenString(xforms, toprint, space, '~');
186  } else {
187  strlcpy(toprint, xforms,sizeof(toprint));
188  }
189  (void)snprintf(am->pname, sizeof(am->pname), "%s#%d (%s)",
190  am->name, id, toprint);
191  } else {
192  (void)snprintf(am->pname, sizeof(am->pname), "%s#%d",
193  am->name, id);
194  }
195  am->id = de_ctx->app_mpms_list_cnt++;
196 
198  t->next = am;
199  SCLogDebug("copied mpm registration for %s id %u "
200  "with parent %u and GetData %p",
201  t->name, id, parent_id, am->app_v2.GetData);
202  t = am;
203  }
204  t = t->next;
205  }
206 }
207 
209 {
210  const DetectBufferMpmRegistery *list = g_mpm_list[DETECT_BUFFER_MPM_TYPE_APP];
211  while (list != NULL) {
212  DetectBufferMpmRegistery *n = SCCalloc(1, sizeof(*n));
213  BUG_ON(n == NULL);
214 
215  *n = *list;
216  n->next = NULL;
217 
218  if (de_ctx->app_mpms_list == NULL) {
219  de_ctx->app_mpms_list = n;
220  } else {
222  while (t->next != NULL) {
223  t = t->next;
224  }
225  t->next = n;
226  }
227 
228  /* default to whatever the global setting is */
229  int shared = (de_ctx->sgh_mpm_context == ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE);
230 
231  /* see if we use a unique or shared mpm ctx for this type */
232  int confshared = 0;
233  char confstring[256] = "detect.mpm.";
234  strlcat(confstring, n->name, sizeof(confstring));
235  strlcat(confstring, ".shared", sizeof(confstring));
236  if (ConfGetBool(confstring, &confshared) == 1)
237  shared = confshared;
238 
239  if (shared == 0) {
240  if (!(de_ctx->flags & DE_QUIET)) {
241  SCLogPerf("using unique mpm ctx' for %s", n->name);
242  }
244  } else {
245  if (!(de_ctx->flags & DE_QUIET)) {
246  SCLogPerf("using shared mpm ctx' for %s", n->name);
247  }
249  }
250 
251  list = list->next;
252  }
253  de_ctx->app_mpms_list_cnt = g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_APP];
254  SCLogDebug("mpm: de_ctx app_mpms_list %p %u",
255  de_ctx->app_mpms_list, de_ctx->app_mpms_list_cnt);
256 }
257 
258 /**
259  * \brief initialize mpm contexts for applayer buffers that are in
260  * "single or "shared" mode.
261  */
263 {
264  int r = 0;
265  const DetectBufferMpmRegistery *am = de_ctx->app_mpms_list;
266  while (am != NULL) {
267  int dir = (am->direction == SIG_FLAG_TOSERVER) ? 1 : 0;
268 
270  {
271  MpmCtx *mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, am->sgh_mpm_context, dir);
272  if (mpm_ctx != NULL) {
273  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
274  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
275  }
276  }
277  }
278  am = am->next;
279  }
280  return r;
281 }
282 
283 /** \brief register a MPM engine
284  *
285  * \note to be used at start up / registration only. Errors are fatal.
286  */
287 void DetectPktMpmRegister(const char *name,
288  int priority,
289  int (*PrefilterRegister)(DetectEngineCtx *de_ctx,
290  SigGroupHead *sgh, MpmCtx *mpm_ctx,
291  const DetectBufferMpmRegistery *mpm_reg, int list_id),
293 {
294  SCLogDebug("registering %s/%d/%p/%p", name, priority,
295  PrefilterRegister, GetData);
296 
297  if (PrefilterRegister == PrefilterGenericMpmPktRegister && GetData == NULL) {
298  // must register GetData with PrefilterGenericMpmRegister
299  abort();
300  }
301 
304  int sm_list = DetectBufferTypeGetByName(name);
305  if (sm_list == -1) {
307  "MPM engine registration for %s failed", name);
308  }
309 
310  DetectBufferMpmRegistery *am = SCCalloc(1, sizeof(*am));
311  BUG_ON(am == NULL);
312  am->name = name;
313  snprintf(am->pname, sizeof(am->pname), "%s", am->name);
314  am->sm_list = sm_list;
315  am->priority = priority;
317 
318  am->PrefilterRegisterWithListId = PrefilterRegister;
319  am->pkt_v1.GetData = GetData;
320 
321  if (g_mpm_list[DETECT_BUFFER_MPM_TYPE_PKT] == NULL) {
322  g_mpm_list[DETECT_BUFFER_MPM_TYPE_PKT] = am;
323  } else {
325  while (t->next != NULL) {
326  t = t->next;
327  }
328  t->next = am;
329  am->id = t->id + 1;
330  }
331  g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_PKT]++;
332 
333  SupportFastPatternForSigMatchList(sm_list, priority);
334  SCLogDebug("%s/%d done", name, sm_list);
335 }
336 
337 /** \brief copy a mpm engine from parent_id, add in transforms */
339  const int id, const int parent_id,
340  DetectEngineTransforms *transforms)
341 {
342  SCLogDebug("registering %d/%d", id, parent_id);
343 
345  while (t) {
346  if (t->sm_list == parent_id) {
347  DetectBufferMpmRegistery *am = SCCalloc(1, sizeof(*am));
348  BUG_ON(am == NULL);
349  am->name = t->name;
350  snprintf(am->pname, sizeof(am->pname), "%s#%d", am->name, id);
351  am->sm_list = id; // use new id
354  am->pkt_v1.GetData = t->pkt_v1.GetData;
355  am->priority = t->priority;
357  am->next = t->next;
358  if (transforms) {
359  memcpy(&am->transforms, transforms, sizeof(*transforms));
360  }
361  am->id = de_ctx->pkt_mpms_list_cnt++;
362 
364  t->next = am;
365  SCLogDebug("copied mpm registration for %s id %u "
366  "with parent %u and GetData %p",
367  t->name, id, parent_id, am->pkt_v1.GetData);
368  t = am;
369  }
370  t = t->next;
371  }
372 }
373 
375 {
376  const DetectBufferMpmRegistery *list = g_mpm_list[DETECT_BUFFER_MPM_TYPE_PKT];
377  while (list != NULL) {
378  DetectBufferMpmRegistery *n = SCCalloc(1, sizeof(*n));
379  BUG_ON(n == NULL);
380 
381  *n = *list;
382  n->next = NULL;
383 
384  if (de_ctx->pkt_mpms_list == NULL) {
385  de_ctx->pkt_mpms_list = n;
386  } else {
388  while (t->next != NULL) {
389  t = t->next;
390  }
391 
392  t->next = n;
393  }
394 
395  /* default to whatever the global setting is */
396  int shared = (de_ctx->sgh_mpm_context == ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE);
397 
398  /* see if we use a unique or shared mpm ctx for this type */
399  int confshared = 0;
400  char confstring[256] = "detect.mpm.";
401  strlcat(confstring, n->name, sizeof(confstring));
402  strlcat(confstring, ".shared", sizeof(confstring));
403  if (ConfGetBool(confstring, &confshared) == 1)
404  shared = confshared;
405 
406  if (shared == 0) {
407  if (!(de_ctx->flags & DE_QUIET)) {
408  SCLogPerf("using unique mpm ctx' for %s", n->name);
409  }
411  } else {
412  if (!(de_ctx->flags & DE_QUIET)) {
413  SCLogPerf("using shared mpm ctx' for %s", n->name);
414  }
416  }
417 
418  list = list->next;
419  }
420  de_ctx->pkt_mpms_list_cnt = g_mpm_list_cnt[DETECT_BUFFER_MPM_TYPE_PKT];
421  SCLogDebug("mpm: de_ctx pkt_mpms_list %p %u",
422  de_ctx->pkt_mpms_list, de_ctx->pkt_mpms_list_cnt);
423 }
424 
425 /**
426  * \brief initialize mpm contexts for applayer buffers that are in
427  * "single or "shared" mode.
428  */
430 {
431  SCLogDebug("preparing pkt mpm");
432  int r = 0;
433  const DetectBufferMpmRegistery *am = de_ctx->pkt_mpms_list;
434  while (am != NULL) {
435  SCLogDebug("%s", am->name);
437  {
438  MpmCtx *mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, am->sgh_mpm_context, 0);
439  if (mpm_ctx != NULL) {
440  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
441  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
442  SCLogDebug("%s: %d", am->name, r);
443  }
444  }
445  }
446  am = am->next;
447  }
448  return r;
449 }
450 
451 static int32_t SetupBuiltinMpm(DetectEngineCtx *de_ctx, const char *name)
452 {
453  /* default to whatever the global setting is */
454  int shared = (de_ctx->sgh_mpm_context == ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE);
455 
456  /* see if we use a unique or shared mpm ctx for this type */
457  int confshared = 0;
458  char confstring[256] = "detect.mpm.";
459  strlcat(confstring, name, sizeof(confstring));
460  strlcat(confstring, ".shared", sizeof(confstring));
461  if (ConfGetBool(confstring, &confshared) == 1)
462  shared = confshared;
463 
464  int32_t ctx;
465  if (shared == 0) {
467  SCLogPerf("using unique mpm ctx' for %s", name);
468  } else {
469  ctx = MpmFactoryRegisterMpmCtxProfile(de_ctx, name);
470  SCLogPerf("using shared mpm ctx' for %s", name);
471  }
472  return ctx;
473 }
474 
476 {
477  de_ctx->sgh_mpm_context_proto_tcp_packet = SetupBuiltinMpm(de_ctx, "tcp-packet");
478  de_ctx->sgh_mpm_context_stream = SetupBuiltinMpm(de_ctx, "tcp-stream");
479 
480  de_ctx->sgh_mpm_context_proto_udp_packet = SetupBuiltinMpm(de_ctx, "udp-packet");
481  de_ctx->sgh_mpm_context_proto_other_packet = SetupBuiltinMpm(de_ctx, "other-ip");
482 }
483 
484 /**
485  * \brief initialize mpm contexts for builtin buffers that are in
486  * "single or "shared" mode.
487  */
489 {
490  int r = 0;
491  MpmCtx *mpm_ctx = NULL;
492 
495  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
496  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
497  }
499  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
500  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
501  }
502  }
503 
506  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
507  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
508  }
510  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
511  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
512  }
513  }
514 
517  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
518  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
519  }
520  }
521 
523  mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_stream, 0);
524  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
525  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
526  }
527  mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_stream, 1);
528  if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
529  r |= mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
530  }
531  }
532 
533  return r;
534 }
535 
536 /**
537  * \brief check if a signature has patterns that are to be inspected
538  * against a packets payload (as opposed to the stream payload)
539  *
540  * \param s signature
541  *
542  * \retval 1 true
543  * \retval 0 false
544  */
546 {
547  SCEnter();
548 
549  if (s == NULL) {
550  SCReturnInt(0);
551  }
552 
553  if (!(s->proto.proto[IPPROTO_TCP / 8] & 1 << (IPPROTO_TCP % 8))) {
554  SCReturnInt(1);
555  }
556 
557  if ((s->init_data != NULL && s->init_data->smlists[DETECT_SM_LIST_PMATCH] == NULL) ||
558  (s->init_data == NULL && s->sm_arrays[DETECT_SM_LIST_PMATCH] == NULL))
559  {
560  SCLogDebug("no mpm");
561  SCReturnInt(0);
562  }
563 
564  if (!(s->flags & SIG_FLAG_REQUIRE_PACKET)) {
565  SCReturnInt(0);
566  }
567 
568  SCReturnInt(1);
569 }
570 
571 /**
572  * \brief check if a signature has patterns that are to be inspected
573  * against the stream payload (as opposed to the individual packets
574  * payload(s))
575  *
576  * \param s signature
577  *
578  * \retval 1 true
579  * \retval 0 false
580  */
582 {
583  SCEnter();
584 
585  if (s == NULL) {
586  SCReturnInt(0);
587  }
588 
589  if (!(s->proto.proto[IPPROTO_TCP / 8] & 1 << (IPPROTO_TCP % 8))) {
590  SCReturnInt(0);
591  }
592 
593  if ((s->init_data != NULL && s->init_data->smlists[DETECT_SM_LIST_PMATCH] == NULL) ||
594  (s->init_data == NULL && s->sm_arrays[DETECT_SM_LIST_PMATCH] == NULL))
595  {
596  SCLogDebug("no mpm");
597  SCReturnInt(0);
598  }
599 
600  if (!(s->flags & SIG_FLAG_REQUIRE_STREAM)) {
601  SCReturnInt(0);
602  }
603 
604  SCReturnInt(1);
605 }
606 
607 
608 /**
609  * \brief Function to return the multi pattern matcher algorithm to be
610  * used by the engine, based on the mpm-algo setting in yaml
611  * Use the default mpm if none is specified in the yaml file.
612  *
613  * \retval mpm algo value
614  */
616 {
617  const char *mpm_algo;
618  uint16_t mpm_algo_val = mpm_default_matcher;
619 
620  /* Get the mpm algo defined in config file by the user */
621  if ((ConfGet("mpm-algo", &mpm_algo)) == 1) {
622  uint16_t u;
623 
624  if (mpm_algo != NULL) {
625 #if __BYTE_ORDER == __BIG_ENDIAN
626  if (strcmp(mpm_algo, "ac-ks") == 0) {
628  "not work on big endian systems at this time.");
629  exit(EXIT_FAILURE);
630  }
631 #endif
632  if (strcmp("auto", mpm_algo) == 0) {
633  goto done;
634  }
635  for (u = 0; u < MPM_TABLE_SIZE; u++) {
636  if (mpm_table[u].name == NULL)
637  continue;
638 
639  if (strcmp(mpm_table[u].name, mpm_algo) == 0) {
640  mpm_algo_val = u;
641  goto done;
642  }
643  }
644  }
645 
646  SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY, "Invalid mpm algo supplied "
647  "in the yaml conf file: \"%s\"", mpm_algo);
648  exit(EXIT_FAILURE);
649  }
650 
651  done:
652  return mpm_algo_val;
653 }
654 
655 /** \brief cleans up the mpm instance after a match */
657 {
658  PmqReset(&det_ctx->pmq);
659 }
660 
661 void PatternMatchDestroy(MpmCtx *mpm_ctx, uint16_t mpm_matcher)
662 {
663  SCLogDebug("mpm_ctx %p, mpm_matcher %"PRIu16"", mpm_ctx, mpm_matcher);
664  mpm_table[mpm_matcher].DestroyCtx(mpm_ctx);
665 }
666 
667 void PatternMatchThreadPrint(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
668 {
669  SCLogDebug("mpm_thread_ctx %p, mpm_matcher %"PRIu16" defunct", mpm_thread_ctx, mpm_matcher);
670  //mpm_table[mpm_matcher].PrintThreadCtx(mpm_thread_ctx);
671 }
672 void PatternMatchThreadDestroy(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
673 {
674  SCLogDebug("mpm_thread_ctx %p, mpm_matcher %"PRIu16"", mpm_thread_ctx, mpm_matcher);
675  if (mpm_table[mpm_matcher].DestroyThreadCtx != NULL)
676  mpm_table[mpm_matcher].DestroyThreadCtx(NULL, mpm_thread_ctx);
677 }
678 void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
679 {
680  SCLogDebug("mpm_thread_ctx %p, type %"PRIu16, mpm_thread_ctx, mpm_matcher);
681  MpmInitThreadCtx(mpm_thread_ctx, mpm_matcher);
682 }
683 
684 /** \brief Predict a strength value for patterns
685  *
686  * Patterns with high character diversity score higher.
687  * Alpha chars score not so high
688  * Other printable + a few common codes a little higher
689  * Everything else highest.
690  * Longer patterns score better than short patters.
691  *
692  * \param pat pattern
693  * \param patlen length of the patternn
694  *
695  * \retval s pattern score
696  */
697 uint32_t PatternStrength(uint8_t *pat, uint16_t patlen)
698 {
699  uint8_t a[256];
700  memset(&a, 0 ,sizeof(a));
701 
702  uint32_t s = 0;
703  uint16_t u = 0;
704  for (u = 0; u < patlen; u++) {
705  if (a[pat[u]] == 0) {
706  if (isalpha(pat[u]))
707  s += 3;
708  else if (isprint(pat[u]) || pat[u] == 0x00 || pat[u] == 0x01 || pat[u] == 0xFF)
709  s += 4;
710  else
711  s += 6;
712 
713  a[pat[u]] = 1;
714  } else {
715  s++;
716  }
717  }
718 
719  return s;
720 }
721 
722 static void PopulateMpmHelperAddPattern(MpmCtx *mpm_ctx,
723  const DetectContentData *cd,
724  const Signature *s, uint8_t flags,
725  int chop)
726 {
727  uint16_t pat_offset = cd->offset;
728  uint16_t pat_depth = cd->depth;
729 
730  /* recompute offset/depth to cope with chop */
731  if (chop && (pat_depth || pat_offset)) {
732  pat_offset += cd->fp_chop_offset;
733  if (pat_depth) {
734  pat_depth -= cd->content_len;
735  pat_depth += cd->fp_chop_offset + cd->fp_chop_len;
736  }
737  }
738 
739  if (cd->flags & DETECT_CONTENT_NOCASE) {
740  if (chop) {
741  MpmAddPatternCI(mpm_ctx,
742  cd->content + cd->fp_chop_offset, cd->fp_chop_len,
743  pat_offset, pat_depth,
744  cd->id, s->num, flags|MPM_PATTERN_CTX_OWNS_ID);
745  } else {
746  MpmAddPatternCI(mpm_ctx,
747  cd->content, cd->content_len,
748  pat_offset, pat_depth,
749  cd->id, s->num, flags|MPM_PATTERN_CTX_OWNS_ID);
750  }
751  } else {
752  if (chop) {
753  MpmAddPatternCS(mpm_ctx,
754  cd->content + cd->fp_chop_offset, cd->fp_chop_len,
755  pat_offset, pat_depth,
756  cd->id, s->num, flags|MPM_PATTERN_CTX_OWNS_ID);
757  } else {
758  MpmAddPatternCS(mpm_ctx,
759  cd->content, cd->content_len,
760  pat_offset, pat_depth,
761  cd->id, s->num, flags|MPM_PATTERN_CTX_OWNS_ID);
762  }
763  }
764 
765  return;
766 }
767 
768 #define SGH_PROTO(sgh, p) ((sgh)->init->protos[(p)] == 1)
769 #define SGH_DIRECTION_TS(sgh) ((sgh)->init->direction & SIG_FLAG_TOSERVER)
770 #define SGH_DIRECTION_TC(sgh) ((sgh)->init->direction & SIG_FLAG_TOCLIENT)
771 
772 static void SetMpm(Signature *s, SigMatch *mpm_sm)
773 {
774  if (s == NULL || mpm_sm == NULL)
775  return;
776 
777  DetectContentData *cd = (DetectContentData *)mpm_sm->ctx;
779  if (DETECT_CONTENT_IS_SINGLE(cd) &&
780  !(cd->flags & DETECT_CONTENT_NEGATED) &&
781  !(cd->flags & DETECT_CONTENT_REPLACE) &&
782  cd->content_len == cd->fp_chop_len)
783  {
785  }
786  } else {
787  if (DETECT_CONTENT_IS_SINGLE(cd) &&
788  !(cd->flags & DETECT_CONTENT_NEGATED) &&
789  !(cd->flags & DETECT_CONTENT_REPLACE))
790  {
792  }
793  }
794  cd->flags |= DETECT_CONTENT_MPM;
795  s->init_data->mpm_sm = mpm_sm;
796  return;
797 }
798 
799 static SigMatch *GetMpmForList(const Signature *s, const int list, SigMatch *mpm_sm,
800  uint16_t max_len, bool skip_negated_content)
801 {
802  for (SigMatch *sm = s->init_data->smlists[list]; sm != NULL; sm = sm->next) {
803  if (sm->type != DETECT_CONTENT)
804  continue;
805 
806  const DetectContentData *cd = (DetectContentData *)sm->ctx;
807  /* skip_negated_content is only set if there's absolutely no
808  * non-negated content present in the sig */
809  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
810  continue;
811  if (cd->content_len != max_len)
812  continue;
813 
814  if (mpm_sm == NULL) {
815  mpm_sm = sm;
816  } else {
817  DetectContentData *data1 = (DetectContentData *)sm->ctx;
818  DetectContentData *data2 = (DetectContentData *)mpm_sm->ctx;
819  uint32_t ls = PatternStrength(data1->content, data1->content_len);
820  uint32_t ss = PatternStrength(data2->content, data2->content_len);
821  if (ls > ss) {
822  mpm_sm = sm;
823  } else if (ls == ss) {
824  /* if 2 patterns are of equal strength, we pick the longest */
825  if (data1->content_len > data2->content_len)
826  mpm_sm = sm;
827  } else {
828  SCLogDebug("sticking with mpm_sm");
829  }
830  }
831  }
832  return mpm_sm;
833 }
834 
836 {
837  if (s->init_data->mpm_sm != NULL)
838  return;
839 
840  SigMatch *mpm_sm = NULL, *sm = NULL;
841  const int nlists = s->init_data->smlists_array_size;
842  int nn_sm_list[nlists];
843  int n_sm_list[nlists];
844  memset(nn_sm_list, 0, nlists * sizeof(int));
845  memset(n_sm_list, 0, nlists * sizeof(int));
846  int count_nn_sm_list = 0;
847  int count_n_sm_list = 0;
848 
849  /* inspect rule to see if we have the fast_pattern reg to
850  * force using a sig, otherwise keep stats about the patterns */
851  for (int list_id = 0; list_id < nlists; list_id++) {
852  if (s->init_data->smlists[list_id] == NULL)
853  continue;
854 
855  if (!FastPatternSupportEnabledForSigMatchList(de_ctx, list_id))
856  continue;
857 
858  for (sm = s->init_data->smlists[list_id]; sm != NULL; sm = sm->next) {
859  if (sm->type != DETECT_CONTENT)
860  continue;
861 
862  const DetectContentData *cd = (DetectContentData *)sm->ctx;
863  /* fast_pattern set in rule, so using this pattern */
864  if ((cd->flags & DETECT_CONTENT_FAST_PATTERN)) {
865  SetMpm(s, sm);
866  return;
867  }
868 
869  if (cd->flags & DETECT_CONTENT_NEGATED) {
870  n_sm_list[list_id] = 1;
871  count_n_sm_list++;
872  } else {
873  nn_sm_list[list_id] = 1;
874  count_nn_sm_list++;
875  }
876  }
877  }
878 
879  /* prefer normal not-negated over negated */
880  int *curr_sm_list = NULL;
881  int skip_negated_content = 1;
882  if (count_nn_sm_list > 0) {
883  curr_sm_list = nn_sm_list;
884  } else if (count_n_sm_list > 0) {
885  curr_sm_list = n_sm_list;
886  skip_negated_content = 0;
887  } else {
888  return;
889  }
890 
891  int final_sm_list[nlists];
892  memset(&final_sm_list, 0, (nlists * sizeof(int)));
893 
894  int count_final_sm_list = 0;
895  int priority;
896 
898  while (tmp != NULL) {
899  for (priority = tmp->priority;
900  tmp != NULL && priority == tmp->priority;
901  tmp = tmp->next)
902  {
903  if (tmp->list_id >= nlists)
904  continue;
905  if (curr_sm_list[tmp->list_id] == 0)
906  continue;
907  final_sm_list[count_final_sm_list++] = tmp->list_id;
908  }
909  if (count_final_sm_list != 0)
910  break;
911  }
912 
913  BUG_ON(count_final_sm_list == 0);
914 
915  uint16_t max_len = 0;
916  for (int i = 0; i < count_final_sm_list; i++) {
917  if (final_sm_list[i] >= (int)s->init_data->smlists_array_size)
918  continue;
919 
920  for (sm = s->init_data->smlists[final_sm_list[i]]; sm != NULL; sm = sm->next) {
921  if (sm->type != DETECT_CONTENT)
922  continue;
923 
924  const DetectContentData *cd = (DetectContentData *)sm->ctx;
925  /* skip_negated_content is only set if there's absolutely no
926  * non-negated content present in the sig */
927  if ((cd->flags & DETECT_CONTENT_NEGATED) && skip_negated_content)
928  continue;
929  if (max_len < cd->content_len)
930  max_len = cd->content_len;
931  }
932  }
933 
934  for (int i = 0; i < count_final_sm_list; i++) {
935  if (final_sm_list[i] >= (int)s->init_data->smlists_array_size)
936  continue;
937 
938  mpm_sm = GetMpmForList(s, final_sm_list[i], mpm_sm, max_len, skip_negated_content);
939  }
940 
941  /* assign to signature */
942  SetMpm(s, mpm_sm);
943  return;
944 }
945 
946 /** \internal
947  * \brief The hash function for MpmStore
948  *
949  * \param ht Pointer to the hash table.
950  * \param data Pointer to the MpmStore.
951  * \param datalen Not used in our case.
952  *
953  * \retval hash The generated hash value.
954  */
955 static uint32_t MpmStoreHashFunc(HashListTable *ht, void *data, uint16_t datalen)
956 {
957  const MpmStore *ms = (MpmStore *)data;
958  uint32_t hash = 0;
959  uint32_t b = 0;
960 
961  for (b = 0; b < ms->sid_array_size; b++)
962  hash += ms->sid_array[b];
963 
964  return hash % ht->array_size;
965 }
966 
967 /**
968  * \brief The Compare function for MpmStore
969  *
970  * \param data1 Pointer to the first MpmStore.
971  * \param len1 Not used.
972  * \param data2 Pointer to the second MpmStore.
973  * \param len2 Not used.
974  *
975  * \retval 1 If the 2 MpmStores sent as args match.
976  * \retval 0 If the 2 MpmStores sent as args do not match.
977  */
978 static char MpmStoreCompareFunc(void *data1, uint16_t len1, void *data2,
979  uint16_t len2)
980 {
981  const MpmStore *ms1 = (MpmStore *)data1;
982  const MpmStore *ms2 = (MpmStore *)data2;
983 
984  if (ms1->sid_array_size != ms2->sid_array_size)
985  return 0;
986 
987  if (ms1->buffer != ms2->buffer)
988  return 0;
989 
990  if (ms1->direction != ms2->direction)
991  return 0;
992 
993  if (ms1->sm_list != ms2->sm_list)
994  return 0;
995 
996  if (SCMemcmp(ms1->sid_array, ms2->sid_array,
997  ms1->sid_array_size) != 0)
998  {
999  return 0;
1000  }
1001 
1002  return 1;
1003 }
1004 
1005 static void MpmStoreFreeFunc(void *ptr)
1006 {
1007  MpmStore *ms = ptr;
1008  if (ms != NULL) {
1009  if (ms->mpm_ctx != NULL && !(ms->mpm_ctx->flags & MPMCTX_FLAGS_GLOBAL))
1010  {
1011  SCLogDebug("destroying mpm_ctx %p", ms->mpm_ctx);
1013  SCFree(ms->mpm_ctx);
1014  }
1015  ms->mpm_ctx = NULL;
1016 
1017  SCFree(ms->sid_array);
1018  SCFree(ms);
1019  }
1020 }
1021 
1022 /**
1023  * \brief Initializes the MpmStore mpm hash table to be used by the detection
1024  * engine context.
1025  *
1026  * \param de_ctx Pointer to the detection engine context.
1027  *
1028  * \retval 0 On success.
1029  * \retval -1 On failure.
1030  */
1032 {
1033  de_ctx->mpm_hash_table = HashListTableInit(4096,
1034  MpmStoreHashFunc,
1035  MpmStoreCompareFunc,
1036  MpmStoreFreeFunc);
1037  if (de_ctx->mpm_hash_table == NULL)
1038  goto error;
1039 
1040  return 0;
1041 
1042 error:
1043  return -1;
1044 }
1045 
1046 /**
1047  * \brief Adds a MpmStore to the detection engine context MpmStore
1048  *
1049  * \param de_ctx Pointer to the detection engine context.
1050  * \param sgh Pointer to the MpmStore.
1051  *
1052  * \retval ret 0 on Successfully adding the argument sgh; -1 on failure.
1053  */
1054 static int MpmStoreAdd(DetectEngineCtx *de_ctx, MpmStore *s)
1055 {
1056  int ret = HashListTableAdd(de_ctx->mpm_hash_table, (void *)s, 0);
1057  return ret;
1058 }
1059 
1060 /**
1061  * \brief Used to lookup a MpmStore from the MpmStore
1062  *
1063  * \param de_ctx Pointer to the detection engine context.
1064  * \param sgh Pointer to the MpmStore.
1065  *
1066  * \retval rsgh On success a pointer to the MpmStore if the MpmStore is
1067  * found in the hash table; NULL on failure.
1068  */
1069 static MpmStore *MpmStoreLookup(DetectEngineCtx *de_ctx, MpmStore *s)
1070 {
1072  (void *)s, 0);
1073  return rs;
1074 }
1075 
1076 static const DetectBufferMpmRegistery *GetByMpmStore(const DetectEngineCtx *de_ctx,
1077  const MpmStore *ms)
1078 {
1079  const DetectBufferMpmRegistery *am = de_ctx->app_mpms_list;
1080  while (am != NULL) {
1081  if (ms->sm_list == am->sm_list &&
1082  ms->direction == am->direction) {
1083  return am;
1084  }
1085  am = am->next;
1086  }
1087  am = de_ctx->pkt_mpms_list;
1088  while (am != NULL) {
1089  if (ms->sm_list == am->sm_list) {
1090  return am;
1091  }
1092  am = am->next;
1093  }
1094  return NULL;
1095 }
1096 
1098 {
1099  HashListTableBucket *htb = NULL;
1100 
1101  uint32_t stats[MPMB_MAX] = {0};
1102  int app_mpms_cnt = de_ctx->buffer_type_map_elements;
1103  uint32_t appstats[app_mpms_cnt + 1]; // +1 to silence scan-build
1104  memset(&appstats, 0x00, sizeof(appstats));
1105  int pkt_mpms_cnt = de_ctx->buffer_type_map_elements;
1106  uint32_t pktstats[pkt_mpms_cnt + 1]; // +1 to silence scan-build
1107  memset(&pktstats, 0x00, sizeof(pktstats));
1108 
1109  for (htb = HashListTableGetListHead(de_ctx->mpm_hash_table);
1110  htb != NULL;
1111  htb = HashListTableGetListNext(htb))
1112  {
1113  const MpmStore *ms = (MpmStore *)HashListTableGetListData(htb);
1114  if (ms == NULL) {
1115  continue;
1116  }
1117  if (ms->buffer < MPMB_MAX)
1118  stats[ms->buffer]++;
1119  else if (ms->sm_list != DETECT_SM_LIST_PMATCH) {
1120  const DetectBufferMpmRegistery *am = GetByMpmStore(de_ctx, ms);
1121  if (am != NULL) {
1122  switch (am->type) {
1124  SCLogDebug("%s: %u patterns. Min %u, Max %u. Ctx %p",
1125  am->name,
1126  ms->mpm_ctx->pattern_cnt,
1127  ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1128  ms->mpm_ctx);
1129  pktstats[am->sm_list]++;
1130  break;
1132  SCLogDebug("%s %s: %u patterns. Min %u, Max %u. Ctx %p",
1133  am->name,
1134  am->direction == SIG_FLAG_TOSERVER ? "toserver":"toclient",
1135  ms->mpm_ctx->pattern_cnt,
1136  ms->mpm_ctx->minlen, ms->mpm_ctx->maxlen,
1137  ms->mpm_ctx);
1138  appstats[am->sm_list]++;
1139  break;
1141  break;
1142  }
1143  }
1144  }
1145  }
1146 
1147  if (!(de_ctx->flags & DE_QUIET)) {
1148  for (int x = 0; x < MPMB_MAX; x++) {
1149  SCLogPerf("Builtin MPM \"%s\": %u", builtin_mpms[x], stats[x]);
1150  }
1151  const DetectBufferMpmRegistery *am = de_ctx->app_mpms_list;
1152  while (am != NULL) {
1153  if (appstats[am->sm_list] > 0) {
1154  const char *name = am->name;
1155  const char *direction = am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient";
1156  SCLogPerf("AppLayer MPM \"%s %s (%s)\": %u", direction, name,
1157  AppProtoToString(am->app_v2.alproto), appstats[am->sm_list]);
1158  }
1159  am = am->next;
1160  }
1161  const DetectBufferMpmRegistery *pm = de_ctx->pkt_mpms_list;
1162  while (pm != NULL) {
1163  if (pktstats[pm->sm_list] > 0) {
1164  const char *name = pm->name;
1165  SCLogPerf("Pkt MPM \"%s\": %u", name, pktstats[pm->sm_list]);
1166  }
1167  pm = pm->next;
1168  }
1169  }
1170 }
1171 
1172 /**
1173  * \brief Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by
1174  * MpmStoreInit() function.
1175  *
1176  * \param de_ctx Pointer to the detection engine context.
1177  */
1179 {
1180  if (de_ctx->mpm_hash_table == NULL)
1181  return;
1182 
1184  de_ctx->mpm_hash_table = NULL;
1185  return;
1186 }
1187 
1188 static void MpmStoreSetup(const DetectEngineCtx *de_ctx, MpmStore *ms)
1189 {
1190  const Signature *s = NULL;
1191  uint32_t sig;
1192  int dir = 0;
1193 
1194  if (ms->buffer != MPMB_MAX) {
1196 
1197  switch (ms->buffer) {
1198  /* TS is 1 */
1199  case MPMB_TCP_PKT_TS:
1200  case MPMB_TCP_STREAM_TS:
1201  case MPMB_UDP_TS:
1202  dir = 1;
1203  break;
1204 
1205  /* TC is 0 */
1206  default:
1207  case MPMB_UDP_TC:
1208  case MPMB_TCP_STREAM_TC:
1209  case MPMB_TCP_PKT_TC:
1210  case MPMB_OTHERIP: /**< use 0 for other */
1211  dir = 0;
1212  break;
1213  }
1214  } else {
1216 
1217  if (ms->direction == SIG_FLAG_TOSERVER)
1218  dir = 1;
1219  else
1220  dir = 0;
1221  }
1222 
1223  ms->mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, ms->sgh_mpm_context, dir);
1224  if (ms->mpm_ctx == NULL)
1225  return;
1226 
1227  MpmInitCtx(ms->mpm_ctx, de_ctx->mpm_matcher);
1228 
1229  /* add the patterns */
1230  for (sig = 0; sig < (ms->sid_array_size * 8); sig++) {
1231  if (ms->sid_array[sig / 8] & (1 << (sig % 8))) {
1232  s = de_ctx->sig_array[sig];
1233  if (s == NULL)
1234  continue;
1235  if ((s->flags & ms->direction) == 0)
1236  continue;
1237  if (s->init_data->mpm_sm == NULL)
1238  continue;
1239  int list = SigMatchListSMBelongsTo(s, s->init_data->mpm_sm);
1240  if (list < 0)
1241  continue;
1242  if (list != ms->sm_list)
1243  continue;
1244 
1245  SCLogDebug("adding %u", s->id);
1246 
1248 
1249  int skip = 0;
1250  /* negated logic: if mpm match can't be used to be sure about this
1251  * pattern, we have to inspect the rule fully regardless of mpm
1252  * match. So in this case there is no point of adding it at all.
1253  * The non-mpm list entry for the sig will make sure the sig is
1254  * inspected. */
1255  if ((cd->flags & DETECT_CONTENT_NEGATED) &&
1257  {
1258  skip = 1;
1259  SCLogDebug("not adding negated mpm as it's not 'single'");
1260  }
1261 
1262  if (!skip) {
1263  PopulateMpmHelperAddPattern(ms->mpm_ctx,
1264  cd, s, 0, (cd->flags & DETECT_CONTENT_FAST_PATTERN_CHOP));
1265  }
1266  }
1267  }
1268 
1269  if (ms->mpm_ctx->pattern_cnt == 0) {
1270  MpmFactoryReClaimMpmCtx(de_ctx, ms->mpm_ctx);
1271  ms->mpm_ctx = NULL;
1272  } else {
1274  if (mpm_table[ms->mpm_ctx->mpm_type].Prepare != NULL) {
1276  }
1277  }
1278  }
1279 }
1280 
1281 
1282 /** \brief Get MpmStore for a built-in buffer type
1283  *
1284  */
1286  enum MpmBuiltinBuffers buf)
1287 {
1288  const Signature *s = NULL;
1289  uint32_t sig;
1290  uint32_t cnt = 0;
1291  int direction = 0;
1292  uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
1293  uint8_t sids_array[max_sid];
1294  memset(sids_array, 0x00, max_sid);
1295  int sgh_mpm_context = 0;
1296  int sm_list = DETECT_SM_LIST_PMATCH;
1297 
1298  switch (buf) {
1299  case MPMB_TCP_PKT_TS:
1300  case MPMB_TCP_PKT_TC:
1301  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_tcp_packet;
1302  break;
1303  case MPMB_TCP_STREAM_TS:
1304  case MPMB_TCP_STREAM_TC:
1305  sgh_mpm_context = de_ctx->sgh_mpm_context_stream;
1306  break;
1307  case MPMB_UDP_TS:
1308  case MPMB_UDP_TC:
1309  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_udp_packet;
1310  break;
1311  case MPMB_OTHERIP:
1312  sgh_mpm_context = de_ctx->sgh_mpm_context_proto_other_packet;
1313  break;
1314  default:
1315  break;
1316  }
1317 
1318  switch(buf) {
1319  case MPMB_TCP_PKT_TS:
1320  case MPMB_TCP_STREAM_TS:
1321  case MPMB_UDP_TS:
1322  direction = SIG_FLAG_TOSERVER;
1323  break;
1324 
1325  case MPMB_TCP_PKT_TC:
1326  case MPMB_TCP_STREAM_TC:
1327  case MPMB_UDP_TC:
1328  direction = SIG_FLAG_TOCLIENT;
1329  break;
1330 
1331  case MPMB_OTHERIP:
1332  direction = (SIG_FLAG_TOCLIENT|SIG_FLAG_TOSERVER);
1333  break;
1334 
1335  case MPMB_MAX:
1336  BUG_ON(1);
1337  break;
1338  }
1339 
1340  for (sig = 0; sig < sgh->sig_cnt; sig++) {
1341  s = sgh->match_array[sig];
1342  if (s == NULL)
1343  continue;
1344 
1345  if (s->init_data->mpm_sm == NULL)
1346  continue;
1347 
1348  int list = SigMatchListSMBelongsTo(s, s->init_data->mpm_sm);
1349  if (list < 0)
1350  continue;
1351 
1352  if (list != DETECT_SM_LIST_PMATCH)
1353  continue;
1354 
1355  switch (buf) {
1356  case MPMB_TCP_PKT_TS:
1357  case MPMB_TCP_PKT_TC:
1358  if (SignatureHasPacketContent(s) == 1)
1359  {
1360  sids_array[s->num / 8] |= 1 << (s->num % 8);
1361  cnt++;
1362  }
1363  break;
1364  case MPMB_TCP_STREAM_TS:
1365  case MPMB_TCP_STREAM_TC:
1366  if (SignatureHasStreamContent(s) == 1)
1367  {
1368  sids_array[s->num / 8] |= 1 << (s->num % 8);
1369  cnt++;
1370  }
1371  break;
1372  case MPMB_UDP_TS:
1373  case MPMB_UDP_TC:
1374  sids_array[s->num / 8] |= 1 << (s->num % 8);
1375  cnt++;
1376  break;
1377  case MPMB_OTHERIP:
1378  sids_array[s->num / 8] |= 1 << (s->num % 8);
1379  cnt++;
1380  break;
1381  default:
1382  break;
1383  }
1384  }
1385 
1386  if (cnt == 0)
1387  return NULL;
1388 
1389  MpmStore lookup = { sids_array, max_sid, direction, buf, sm_list, 0, NULL};
1390 
1391  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1392  if (result == NULL) {
1393  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1394  if (copy == NULL)
1395  return NULL;
1396  uint8_t *sids = SCCalloc(1, max_sid);
1397  if (sids == NULL) {
1398  SCFree(copy);
1399  return NULL;
1400  }
1401 
1402  memcpy(sids, sids_array, max_sid);
1403  copy->sid_array = sids;
1404  copy->sid_array_size = max_sid;
1405  copy->buffer = buf;
1406  copy->direction = direction;
1407  copy->sm_list = sm_list;
1408  copy->sgh_mpm_context = sgh_mpm_context;
1409 
1410  MpmStoreSetup(de_ctx, copy);
1411  MpmStoreAdd(de_ctx, copy);
1412  return copy;
1413  } else {
1414  return result;
1415  }
1416 }
1417 
1418 static MpmStore *MpmStorePrepareBufferAppLayer(DetectEngineCtx *de_ctx,
1419  SigGroupHead *sgh, const DetectBufferMpmRegistery *am)
1420 {
1421  const Signature *s = NULL;
1422  uint32_t sig;
1423  uint32_t cnt = 0;
1424  uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
1425  uint8_t sids_array[max_sid];
1426  memset(sids_array, 0x00, max_sid);
1427 
1428  SCLogDebug("handling %s direction %s for list %d", am->name,
1429  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1430  am->sm_list);
1431 
1432  for (sig = 0; sig < sgh->sig_cnt; sig++) {
1433  s = sgh->match_array[sig];
1434  if (s == NULL)
1435  continue;
1436 
1437  if (s->init_data->mpm_sm == NULL)
1438  continue;
1439 
1440  int list = SigMatchListSMBelongsTo(s, s->init_data->mpm_sm);
1441  if (list < 0)
1442  continue;
1443 
1444  if ((s->flags & am->direction) == 0)
1445  continue;
1446 
1447  if (list != am->sm_list)
1448  continue;
1449 
1450  sids_array[s->num / 8] |= 1 << (s->num % 8);
1451  cnt++;
1452  }
1453 
1454  if (cnt == 0)
1455  return NULL;
1456 
1457  MpmStore lookup = { sids_array, max_sid, am->direction,
1458  MPMB_MAX, am->sm_list, 0, NULL};
1459  SCLogDebug("am->direction %d am->sm_list %d",
1460  am->direction, am->sm_list);
1461 
1462  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1463  if (result == NULL) {
1464  SCLogDebug("new unique mpm for %s %s: %u patterns",
1465  am->name,
1466  am->direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1467  cnt);
1468 
1469  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1470  if (copy == NULL)
1471  return NULL;
1472  uint8_t *sids = SCCalloc(1, max_sid);
1473  if (sids == NULL) {
1474  SCFree(copy);
1475  return NULL;
1476  }
1477 
1478  memcpy(sids, sids_array, max_sid);
1479  copy->sid_array = sids;
1480  copy->sid_array_size = max_sid;
1481  copy->buffer = MPMB_MAX;
1482  copy->direction = am->direction;
1483  copy->sm_list = am->sm_list;
1484  copy->sgh_mpm_context = am->sgh_mpm_context;
1485 
1486  MpmStoreSetup(de_ctx, copy);
1487  MpmStoreAdd(de_ctx, copy);
1488  return copy;
1489  } else {
1490  SCLogDebug("using existing mpm %p", result);
1491  return result;
1492  }
1493  return NULL;
1494 }
1495 
1496 static MpmStore *MpmStorePrepareBufferPkt(DetectEngineCtx *de_ctx,
1497  SigGroupHead *sgh, const DetectBufferMpmRegistery *am)
1498 {
1499  const Signature *s = NULL;
1500  uint32_t sig;
1501  uint32_t cnt = 0;
1502  uint32_t max_sid = DetectEngineGetMaxSigId(de_ctx) / 8 + 1;
1503  uint8_t sids_array[max_sid];
1504  memset(sids_array, 0x00, max_sid);
1505 
1506  SCLogDebug("handling %s for list %d", am->name,
1507  am->sm_list);
1508 
1509  for (sig = 0; sig < sgh->sig_cnt; sig++) {
1510  s = sgh->match_array[sig];
1511  if (s == NULL)
1512  continue;
1513 
1514  if (s->init_data->mpm_sm == NULL)
1515  continue;
1516 
1517  int list = SigMatchListSMBelongsTo(s, s->init_data->mpm_sm);
1518  if (list < 0)
1519  continue;
1520 
1521  if (list != am->sm_list)
1522  continue;
1523 
1524  sids_array[s->num / 8] |= 1 << (s->num % 8);
1525  cnt++;
1526  }
1527 
1528  if (cnt == 0)
1529  return NULL;
1530 
1531  MpmStore lookup = { sids_array, max_sid, SIG_FLAG_TOSERVER|SIG_FLAG_TOCLIENT,
1532  MPMB_MAX, am->sm_list, 0, NULL};
1533  SCLogDebug("am->sm_list %d", am->sm_list);
1534 
1535  MpmStore *result = MpmStoreLookup(de_ctx, &lookup);
1536  if (result == NULL) {
1537  SCLogDebug("new unique mpm for %s: %u patterns",
1538  am->name, cnt);
1539 
1540  MpmStore *copy = SCCalloc(1, sizeof(MpmStore));
1541  if (copy == NULL)
1542  return NULL;
1543  uint8_t *sids = SCCalloc(1, max_sid);
1544  if (sids == NULL) {
1545  SCFree(copy);
1546  return NULL;
1547  }
1548 
1549  memcpy(sids, sids_array, max_sid);
1550  copy->sid_array = sids;
1551  copy->sid_array_size = max_sid;
1552  copy->buffer = MPMB_MAX;
1554  copy->sm_list = am->sm_list;
1555  copy->sgh_mpm_context = am->sgh_mpm_context;
1556 
1557  MpmStoreSetup(de_ctx, copy);
1558  MpmStoreAdd(de_ctx, copy);
1559  return copy;
1560  } else {
1561  SCLogDebug("using existing mpm %p", result);
1562  return result;
1563  }
1564  return NULL;
1565 }
1566 
1567 static void SetRawReassemblyFlag(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
1568 {
1569  const Signature *s = NULL;
1570  uint32_t sig;
1571 
1572  for (sig = 0; sig < sgh->sig_cnt; sig++) {
1573  s = sgh->match_array[sig];
1574  if (s == NULL)
1575  continue;
1576 
1577  if (SignatureHasStreamContent(s) == 1) {
1579  SCLogDebug("rule group %p has SIG_GROUP_HEAD_HAVERAWSTREAM set", sgh);
1580  return;
1581  }
1582  }
1583  SCLogDebug("rule group %p does NOT have SIG_GROUP_HEAD_HAVERAWSTREAM set", sgh);
1584 }
1585 
1586 static void PrepareAppMpms(DetectEngineCtx *de_ctx, SigGroupHead *sh)
1587 {
1588  if (de_ctx->app_mpms_list_cnt == 0)
1589  return;
1590 
1591  sh->init->app_mpms = SCCalloc(de_ctx->app_mpms_list_cnt, sizeof(MpmCtx *));
1592  BUG_ON(sh->init->app_mpms == NULL);
1593 
1595  while (a != NULL) {
1596  if ((a->direction == SIG_FLAG_TOSERVER && SGH_DIRECTION_TS(sh)) ||
1598  {
1599  MpmStore *mpm_store = MpmStorePrepareBufferAppLayer(de_ctx, sh, a);
1600  if (mpm_store != NULL) {
1601  sh->init->app_mpms[a->id] = mpm_store->mpm_ctx;
1602 
1603  SCLogDebug("a %p a->name %s a->PrefilterRegisterWithListId %p "
1604  "mpm_store->mpm_ctx %p", a, a->name,
1605  a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
1606 
1607  /* if we have just certain types of negated patterns,
1608  * mpm_ctx can be NULL */
1609  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
1611  sh, mpm_store->mpm_ctx,
1612  a, a->sm_list) != 0);
1613  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
1614  }
1615  }
1616  }
1617  a = a->next;
1618  }
1619 }
1620 
1621 static void PreparePktMpms(DetectEngineCtx *de_ctx, SigGroupHead *sh)
1622 {
1623  if (de_ctx->pkt_mpms_list_cnt == 0)
1624  return;
1625 
1626  sh->init->pkt_mpms = SCCalloc(de_ctx->pkt_mpms_list_cnt, sizeof(MpmCtx *));
1627  BUG_ON(sh->init->pkt_mpms == NULL);
1628 
1630  while (a != NULL) {
1631  MpmStore *mpm_store = MpmStorePrepareBufferPkt(de_ctx, sh, a);
1632  if (mpm_store != NULL) {
1633  sh->init->pkt_mpms[a->id] = mpm_store->mpm_ctx;
1634 
1635  SCLogDebug("a %p a->name %s a->reg->PrefilterRegisterWithListId %p "
1636  "mpm_store->mpm_ctx %p", a, a->name,
1637  a->PrefilterRegisterWithListId, mpm_store->mpm_ctx);
1638 
1639  /* if we have just certain types of negated patterns,
1640  * mpm_ctx can be NULL */
1641  if (a->PrefilterRegisterWithListId && mpm_store->mpm_ctx) {
1643  sh, mpm_store->mpm_ctx,
1644  a, a->sm_list) != 0);
1645  SCLogDebug("mpm %s %d set up", a->name, a->sm_list);
1646  }
1647  }
1648  a = a->next;
1649  }
1650 }
1651 
1652 /** \brief Prepare the pattern matcher ctx in a sig group head.
1653  *
1654  */
1656 {
1657  MpmStore *mpm_store = NULL;
1658  if (SGH_PROTO(sh, IPPROTO_TCP)) {
1659  if (SGH_DIRECTION_TS(sh)) {
1660  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_PKT_TS);
1661  if (mpm_store != NULL) {
1662  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
1663  }
1664 
1665  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_STREAM_TS);
1666  if (mpm_store != NULL) {
1667  PrefilterPktStreamRegister(de_ctx, sh, mpm_store->mpm_ctx);
1668  }
1669 
1670  SetRawReassemblyFlag(de_ctx, sh);
1671  }
1672  if (SGH_DIRECTION_TC(sh)) {
1673  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_PKT_TC);
1674  if (mpm_store != NULL) {
1675  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
1676  }
1677 
1678  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_TCP_STREAM_TC);
1679  if (mpm_store != NULL) {
1680  PrefilterPktStreamRegister(de_ctx, sh, mpm_store->mpm_ctx);
1681  }
1682 
1683  SetRawReassemblyFlag(de_ctx, sh);
1684  }
1685  } else if (SGH_PROTO(sh, IPPROTO_UDP)) {
1686  if (SGH_DIRECTION_TS(sh)) {
1687  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_UDP_TS);
1688  if (mpm_store != NULL) {
1689  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
1690  }
1691  }
1692  if (SGH_DIRECTION_TC(sh)) {
1693  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_UDP_TC);
1694  if (mpm_store != NULL) {
1695  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
1696  }
1697  }
1698  } else {
1699  mpm_store = MpmStorePrepareBuffer(de_ctx, sh, MPMB_OTHERIP);
1700  if (mpm_store != NULL) {
1701  PrefilterPktPayloadRegister(de_ctx, sh, mpm_store->mpm_ctx);
1702  }
1703  }
1704 
1705  PrepareAppMpms(de_ctx, sh);
1706  PreparePktMpms(de_ctx, sh);
1707  return 0;
1708 }
1709 
1710 typedef struct DetectFPAndItsId_ {
1712  uint16_t content_len;
1713  uint32_t flags;
1714  int sm_list;
1715 
1716  uint8_t *content;
1718 
1719 /**
1720  * \brief Figured out the FP and their respective content ids for all the
1721  * sigs in the engine.
1722  *
1723  * \param de_ctx Detection engine context.
1724  *
1725  * \retval 0 On success.
1726  * \retval -1 On failure.
1727  */
1729 {
1730  uint32_t struct_total_size = 0;
1731  uint32_t content_total_size = 0;
1732  Signature *s = NULL;
1733 
1734  /* Count the amount of memory needed to store all the structures
1735  * and the content of those structures. This will over estimate the
1736  * true size, since duplicates are removed below, but counted here.
1737  */
1738  for (s = de_ctx->sig_list; s != NULL; s = s->next) {
1739  if (s->flags & SIG_FLAG_PREFILTER)
1740  continue;
1741 
1742  RetrieveFPForSig(de_ctx, s);
1743  if (s->init_data->mpm_sm != NULL) {
1745  struct_total_size += sizeof(DetectFPAndItsId);
1746  content_total_size += cd->content_len;
1747 
1748  s->flags |= SIG_FLAG_PREFILTER;
1749  }
1750  }
1751  /* no rules */
1752  if (struct_total_size + content_total_size == 0)
1753  return 0;
1754 
1755  /* array hash buffer - i've run out of ideas to name it */
1756  uint8_t *ahb = SCMalloc(sizeof(uint8_t) * (struct_total_size + content_total_size));
1757  if (unlikely(ahb == NULL))
1758  return -1;
1759 
1760  uint8_t *content = NULL;
1761  uint16_t content_len = 0;
1762  PatIntId max_id = 0;
1763  DetectFPAndItsId *struct_offset = (DetectFPAndItsId *)ahb;
1764  uint8_t *content_offset = ahb + struct_total_size;
1765 
1766  for (s = de_ctx->sig_list; s != NULL; s = s->next) {
1767  if (s->init_data->mpm_sm != NULL) {
1769  BUG_ON(sm_list == -1);
1770 
1772  DetectFPAndItsId *dup = (DetectFPAndItsId *)ahb;
1774  content = cd->content + cd->fp_chop_offset;
1775  content_len = cd->fp_chop_len;
1776  } else {
1777  content = cd->content;
1778  content_len = cd->content_len;
1779  }
1780  uint32_t flags = cd->flags & DETECT_CONTENT_NOCASE;
1781  /* Check for content already found on the same list */
1782  for (; dup != struct_offset; dup++) {
1783  if (dup->content_len != content_len)
1784  continue;
1785  if (dup->sm_list != sm_list)
1786  continue;
1787  if (dup->flags != flags)
1788  continue;
1789  /* Check for pattern matching a duplicate. Use case insensitive matching
1790  * for case insensitive patterns. */
1791  if (flags & DETECT_CONTENT_NOCASE) {
1792  if (SCMemcmpLowercase(dup->content, content, content_len) != 0)
1793  continue;
1794  } else {
1795  /* Case sensitive matching */
1796  if (SCMemcmp(dup->content, content, content_len) != 0)
1797  continue;
1798  }
1799  /* Found a match with a previous pattern. */
1800  break;
1801  }
1802  if (dup != struct_offset) {
1803  /* Exited for-loop before the end, so found an existing match.
1804  * Use its ID. */
1805  cd->id = dup->id;
1806  continue;
1807  }
1808 
1809  /* Not found, so new content. Give it a new ID and add it
1810  * to the array. Copy the content at the end of the
1811  * content array.
1812  */
1813  struct_offset->id = max_id++;
1814  cd->id = struct_offset->id;
1815  struct_offset->content_len = content_len;
1816  struct_offset->sm_list = sm_list;
1817  struct_offset->content = content_offset;
1818  struct_offset->flags = flags;
1819 
1820  content_offset += content_len;
1821 
1822  if (flags & DETECT_CONTENT_NOCASE) {
1823  /* Need to store case-insensitive patterns as lower case
1824  * because SCMemcmpLowercase() above assumes that all
1825  * patterns are stored lower case so that it doesn't
1826  * need to relower its first argument.
1827  */
1828  memcpy_tolower(struct_offset->content, content, content_len);
1829  } else {
1830  memcpy(struct_offset->content, content, content_len);
1831  }
1832 
1833  struct_offset++;
1834  } /* if (s->mpm_sm != NULL) */
1835  } /* for */
1836 
1837  de_ctx->max_fp_id = max_id;
1838 
1839  SCFree(ahb);
1840 
1841  return 0;
1842 }
void MpmFactoryReClaimMpmCtx(const DetectEngineCtx *de_ctx, MpmCtx *mpm_ctx)
Definition: util-mpm.c:215
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1439
#define PatIntId
uint32_t PatternStrength(uint8_t *pat, uint16_t patlen)
Predict a strength value for patterns.
DetectProto proto
Definition: detect.h:534
SigIntId sig_cnt
Definition: detect.h:1334
SignatureInitData * init_data
Definition: detect.h:586
uint16_t flags
#define SCMemcmp(a, b, c)
Definition: util-memcmp.h:369
#define DETECT_CONTENT_MPM_IS_CONCLUSIVE(c)
#define SCLogDebug(...)
Definition: util-debug.h:335
const char * builtin_mpms[]
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
uint8_t flags
Definition: util-mpm.h:92
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:302
Signature ** sig_array
Definition: detect.h:771
uint16_t minlen
Definition: util-mpm.h:99
#define SIG_FLAG_PREFILTER
Definition: detect.h:239
int DetectSetFastPatternAndItsId(DetectEngineCtx *de_ctx)
Figured out the FP and their respective content ids for all the sigs in the engine.
void PacketPatternCleanup(DetectEngineThreadCtx *det_ctx)
cleans up the mpm instance after a match
int32_t sgh_mpm_context
Definition: detect.h:1248
InspectionBuffer *(* InspectionBufferGetPktDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Packet *p, const int list_id)
Definition: detect.h:444
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
#define BUG_ON(x)
uint32_t pattern_cnt
Definition: util-mpm.h:97
uint32_t flags
Definition: detect.h:518
uint32_t id
Definition: detect.h:550
void RetrieveFPForSig(const DetectEngineCtx *de_ctx, Signature *s)
Signature ** match_array
Definition: detect.h:1354
#define DETECT_CONTENT_FAST_PATTERN
uint32_t sid_array_size
Definition: detect.h:1243
#define unlikely(expr)
Definition: util-optimize.h:35
void MpmStoreReportStats(const DetectEngineCtx *de_ctx)
DetectEngineTransforms transforms
Definition: detect.h:613
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:59
int transforms[DETECT_TRANSFORMS_MAX]
Definition: detect.h:365
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
Definition: conf.c:517
uint16_t PatternMatchDefaultMatcher(void)
Function to return the multi pattern matcher algorithm to be used by the engine, based on the mpm-alg...
Signature * sig_list
Definition: detect.h:762
int32_t sgh_mpm_context_proto_tcp_packet
Definition: detect.h:841
uint8_t * sid_array
Definition: detect.h:1242
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
#define SIG_FLAG_REQUIRE_STREAM
Definition: detect.h:220
void MpmInitThreadCtx(MpmThreadCtx *mpm_thread_ctx, uint16_t matcher)
Definition: util-mpm.c:256
uint8_t proto[256/8]
void DetectPktMpmRegister(const char *name, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetPktDataPtr GetData)
register a MPM engine
void(* DestroyThreadCtx)(struct MpmCtx_ *, struct MpmThreadCtx_ *)
Definition: util-mpm.h:146
struct DetectFPAndItsId_ DetectFPAndItsId
Container for matching data for a signature group.
Definition: detect.h:1329
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
#define SGH_PROTO(sgh, p)
void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx)
DetectBufferMpmRegistery * pkt_mpms_list
Definition: detect.h:925
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:219
void PatternMatchThreadDestroy(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
const char * name
Definition: detect.h:1193
uint16_t AppProto
Signature container.
Definition: detect.h:517
int DetectMpmPreparePktMpms(DetectEngineCtx *de_ctx)
initialize mpm contexts for applayer buffers that are in "single or "shared" mode.
int32_t sgh_mpm_context_proto_other_packet
Definition: detect.h:843
HashListTable * mpm_hash_table
Definition: detect.h:797
int sm_list
Definition: detect.h:1247
void DetectAppLayerMpmRegister2(const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register a MPM engine
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:331
int32_t MpmFactoryRegisterMpmCtxProfile(DetectEngineCtx *de_ctx, const char *name)
Register a new Mpm Context.
Definition: util-mpm.c:55
#define DETECT_CONTENT_IS_SINGLE(c)
#define SGH_DIRECTION_TS(sgh)
SigIntId num
Definition: detect.h:527
void DetectPktMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id, DetectEngineTransforms *transforms)
copy a mpm engine from parent_id, add in transforms
struct SigMatch_ * next
Definition: detect.h:317
main detection engine ctx
Definition: detect.h:756
uint32_t max_fp_id
Definition: detect.h:823
#define MPMCTX_FLAGS_GLOBAL
Definition: util-mpm.h:85
MpmBuiltinBuffers
Definition: detect.h:1230
SCFPSupportSMList * sm_fp_support_smlist_list
#define DETECT_CONTENT_NO_DOUBLE_INSPECTION_REQUIRED
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
uint32_t pkt_mpms_list_cnt
Definition: detect.h:926
#define DE_QUIET
Definition: detect.h:287
MpmCtx ** pkt_mpms
Definition: detect.h:1318
int(* Prepare)(struct MpmCtx_ *)
Definition: util-mpm.h:161
#define DetectEngineGetMaxSigId(de_ctx)
Definition: detect-engine.h:81
int DetectBufferTypeGetByName(const char *name)
#define SCCalloc(nm, a)
Definition: util-mem.h:253
#define SIG_FLAG_TOCLIENT
Definition: detect.h:233
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:58
int32_t sgh_mpm_context_stream
Definition: detect.h:844
void PatternMatchDestroy(MpmCtx *mpm_ctx, uint16_t mpm_matcher)
uint8_t flags
Definition: detect.h:757
int direction
Definition: detect.h:1245
MpmCtx ** app_mpms
Definition: detect.h:1317
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
struct SCFPSupportSMList_ * next
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
uint8_t mpm_type
Definition: util-mpm.h:90
uint16_t mpm_matcher
Definition: detect.h:805
one time registration of keywords at start up
Definition: detect.h:600
#define SIG_FLAG_TOSERVER
Definition: detect.h:232
int PrefilterPktStreamRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx)
#define SCEnter(...)
Definition: util-debug.h:337
PrefilterRuleStore pmq
Definition: detect.h:1095
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:570
enum DetectBufferMpmType type
Definition: detect.h:607
uint32_t buffer_type_map_elements
Definition: detect.h:912
int SignatureHasPacketContent(const Signature *s)
check if a signature has patterns that are to be inspected against a packets payload (as opposed to t...
void ShortenString(const char *input, char *output, size_t output_size, char c)
Definition: util-misc.c:220
void MpmStoreFree(DetectEngineCtx *de_ctx)
Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by MpmStoreInit() function...
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
void(* DestroyCtx)(struct MpmCtx_ *)
Definition: util-mpm.h:145
uint32_t flags
Definition: detect.h:1330
#define MPM_PATTERN_CTX_OWNS_ID
Definition: util-mpm.h:139
struct Signature_ * next
Definition: detect.h:589
#define SCReturnInt(x)
Definition: util-debug.h:341
int DetectMpmPrepareAppMpms(DetectEngineCtx *de_ctx)
initialize mpm contexts for applayer buffers that are in "single or "shared" mode.
int32_t sgh_mpm_context_proto_udp_packet
Definition: detect.h:842
#define SIG_GROUP_HEAD_HAVERAWSTREAM
Definition: detect.h:1221
void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
struct SigMatch_ ** smlists
Definition: detect.h:511
void DetectBufferTypeSupportsMpm(const char *name)
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
uint32_t smlists_array_size
Definition: detect.h:509
#define DETECT_CONTENT_MPM
SigMatchCtx * ctx
Definition: detect.h:316
#define SCMalloc(a)
Definition: util-mem.h:222
#define MPM_CTX_FACTORY_UNIQUE_CONTEXT
Definition: util-mpm.h:113
int mpm_default_matcher
Definition: util-mpm.h:170
uint16_t maxlen
Definition: util-mpm.h:100
void MpmInitCtx(MpmCtx *mpm_ctx, uint16_t matcher)
Definition: util-mpm.c:261
#define DETECT_CONTENT_NEGATED
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.h:169
#define SCFree(a)
Definition: util-mem.h:322
#define DETECT_CONTENT_REPLACE
void DetectMpmInitializePktMpms(DetectEngineCtx *de_ctx)
void DetectBufferTypeSupportsTransformations(const char *name)
void DetectAppLayerMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id, DetectEngineTransforms *transforms)
copy a mpm engine from parent_id, add in transforms
MpmCtx * mpm_ctx
Definition: detect.h:1250
struct DetectBufferMpmRegistery_::@94::@96 app_v2
int FastPatternSupportEnabledForSigMatchList(const DetectEngineCtx *de_ctx, const int list_id)
Checks if a particular list(Signature->sm_lists[]) is in the list of lists that need to be searched f...
int DetectMpmPrepareBuiltinMpms(DetectEngineCtx *de_ctx)
initialize mpm contexts for builtin buffers that are in "single or "shared" mode. ...
void SupportFastPatternForSigMatchList(int list_id, int priority)
Lets one add a sm list id to be searched for potential fp supported keywords later.
int MpmStoreInit(DetectEngineCtx *de_ctx)
Initializes the MpmStore mpm hash table to be used by the detection engine context.
void DetectMpmInitializeBuiltinMpms(DetectEngineCtx *de_ctx)
#define SCLogPerf(...)
Definition: util-debug.h:261
MpmCtx * MpmFactoryGetMpmCtxForProfile(const DetectEngineCtx *de_ctx, int32_t id, int direction)
Definition: util-mpm.c:192
#define FatalError(x,...)
Definition: util-debug.h:539
int PrefilterPktPayloadRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx)
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
DetectBufferMpmRegistery * app_mpms_list
Definition: detect.h:922
uint32_t array_size
Definition: util-hashlist.h:41
int SignatureHasStreamContent(const Signature *s)
check if a signature has patterns that are to be inspected against the stream payload (as opposed to ...
uint8_t sgh_mpm_context
Definition: detect.h:818
int MpmAddPatternCI(struct MpmCtx_ *mpm_ctx, uint8_t *pat, uint16_t patlen, uint16_t offset, uint16_t depth, uint32_t pid, SigIntId sid, uint8_t flags)
Definition: util-mpm.c:311
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:82
#define DETECT_CONTENT_FAST_PATTERN_CHOP
#define DETECT_CONTENT_NOCASE
enum MpmBuiltinBuffers buffer
Definition: detect.h:1246
void PatternMatchThreadPrint(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
int(* PrefilterRegisterWithListId)(struct DetectEngineCtx_ *de_ctx, struct SigGroupHead_ *sgh, MpmCtx *mpm_ctx, const struct DetectBufferMpmRegistery_ *mpm_reg, int list_id)
Definition: detect.h:610
uint32_t app_mpms_list_cnt
Definition: detect.h:923
int SigMatchListSMBelongsTo(const Signature *s, const SigMatch *key_sm)
Definition: detect-parse.c:561
InspectionBuffer *(* InspectionBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id)
Definition: detect.h:370
#define SGH_DIRECTION_TC(sgh)
struct DetectBufferMpmRegistery_ * next
Definition: detect.h:632
SigMatch * mpm_sm
Definition: detect.h:488
int PatternMatchPrepareGroup(DetectEngineCtx *de_ctx, SigGroupHead *sh)
Prepare the pattern matcher ctx in a sig group head.
const char * name
Definition: detect.h:601
MpmStore * MpmStorePrepareBuffer(DetectEngineCtx *de_ctx, SigGroupHead *sgh, enum MpmBuiltinBuffers buf)
Get MpmStore for a built-in buffer type.
struct DetectBufferMpmRegistery_::@94::@97 pkt_v1
a single match condition for a signature
Definition: detect.h:313
void PmqReset(PrefilterRuleStore *)
Reset a Pmq for reusage. Meant to be called after a single search.
SigGroupHeadInitData * init
Definition: detect.h:1357