suricata
detect-engine-build.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2025 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 #include "suricata-common.h"
19 #include "detect.h"
20 #include "detect-engine.h"
21 #include "detect-parse.h"
22 #include "detect-content.h"
23 
24 #include "detect-engine-build.h"
25 #include "detect-engine-address.h"
26 #include "detect-engine-analyzer.h"
27 #include "detect-engine-iponly.h"
28 #include "detect-engine-mpm.h"
29 #include "detect-engine-siggroup.h"
30 #include "detect-engine-port.h"
32 #include "detect-engine-proto.h"
34 
35 #include "detect-dsize.h"
36 #include "detect-tcp-flags.h"
37 #include "detect-flow.h"
38 #include "detect-config.h"
39 #include "detect-flowbits.h"
40 
41 #include "app-layer-events.h"
42 
44 #include "util-profiling.h"
45 #include "util-validate.h"
46 #include "util-var-name.h"
47 #include "util-conf.h"
48 
49 /* Magic numbers to make the rules of a certain order fall in the same group */
50 #define DETECT_PGSCORE_RULE_PORT_PRIORITIZED 111 /* Rule port group contains a priority port */
51 #define DETECT_PGSCORE_RULE_MPM_FAST_PATTERN 99 /* Rule contains an MPM fast pattern */
52 #define DETECT_PGSCORE_RULE_MPM_NEGATED 77 /* Rule contains a negated MPM */
53 #define DETECT_PGSCORE_RULE_NO_MPM 55 /* Rule does not contain MPM */
54 #define DETECT_PGSCORE_RULE_SYN_ONLY 33 /* Rule needs SYN check */
55 
57 {
58  if (de_ctx == NULL)
59  return;
60 
61  for (Signature *s = de_ctx->sig_list; s != NULL;) {
62  Signature *ns = s->next;
63  SigFree(de_ctx, s);
64  s = ns;
65  }
66  de_ctx->sig_list = NULL;
67 
69  de_ctx->sig_list = NULL;
70 }
71 
72 /** \brief Find a specific signature by sid and gid
73  * \param de_ctx detection engine ctx
74  * \param sid the signature id
75  * \param gid the signature group id
76  *
77  * \retval s sig found
78  * \retval NULL sig not found
79  */
81 {
82  if (de_ctx == NULL)
83  return NULL;
84 
85  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
86  if (s->id == sid && s->gid == gid)
87  return s;
88  }
89 
90  return NULL;
91 }
92 
93 /**
94  * \brief Check if a signature contains the filestore keyword.
95  *
96  * \param s signature
97  *
98  * \retval 0 no
99  * \retval 1 yes
100  */
102 {
103  if (s == NULL)
104  return 0;
105 
106  if (s->flags & SIG_FLAG_FILESTORE)
107  return 1;
108 
109  return 0;
110 }
111 
112 /**
113  * \brief Check if a signature contains the filemagic keyword.
114  *
115  * \param s signature
116  *
117  * \retval 0 no
118  * \retval 1 yes
119  */
121 {
122  if (s == NULL)
123  return 0;
124 
126  return 1;
127 
128  return 0;
129 }
130 
131 /**
132  * \brief Check if a signature contains the filemd5 keyword.
133  *
134  * \param s signature
135  *
136  * \retval 0 no
137  * \retval 1 yes
138  */
140 {
141  if ((s != NULL) && (s->file_flags & FILE_SIG_NEED_MD5))
142  return 1;
143 
144  return 0;
145 }
146 
147 /**
148  * \brief Check if a signature contains the filesha1 keyword.
149  *
150  * \param s signature
151  *
152  * \retval 0 no
153  * \retval 1 yes
154  */
156 {
157  if ((s != NULL) && (s->file_flags & FILE_SIG_NEED_SHA1))
158  return 1;
159 
160  return 0;
161 }
162 
163 /**
164  * \brief Check if a signature contains the filesha256 keyword.
165  *
166  * \param s signature
167  *
168  * \retval 0 no
169  * \retval 1 yes
170  */
172 {
173  if ((s != NULL) && (s->file_flags & FILE_SIG_NEED_SHA256))
174  return 1;
175 
176  return 0;
177 }
178 
179 static bool SignatureInspectsBuffers(const Signature *s)
180 {
181  return (s->init_data->buffer_index > 0);
182 }
183 
184 /** \brief Test is a initialized signature is IP only
185  * \param de_ctx detection engine ctx
186  * \param s the signature
187  * \retval 1 sig is ip only
188  * \retval 2 sig is like ip only
189  * \retval 0 sig is not ip only
190  */
192 {
193  /* explicit hook means no IP-only */
195  return 0;
196 
197  if (s->alproto != ALPROTO_UNKNOWN)
198  return 0;
199 
200  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL)
201  return 0;
202 
203  // may happen for 'config' keyword, postmatch
204  if (s->flags & SIG_FLAG_APPLAYER)
205  return 0;
206 
207  /* if flow dir is set we can't process it in ip-only */
208  if (!(((s->flags & (SIG_FLAG_TOSERVER|SIG_FLAG_TOCLIENT)) == 0) ||
211  return 0;
212 
213  /* for now assume that all registered buffer types are incompatible */
214  if (SignatureInspectsBuffers(s)) {
215  SCReturnInt(0);
216  }
217 
218  /* TMATCH list can be ignored, it contains TAGs and
219  * tags are compatible to IP-only. */
220 
222  for (; sm != NULL; sm = sm->next) {
224  return 0;
225  /* we have enabled flowbits to be compatible with ip only sigs, as long
226  * as the sig only has a "set" flowbits */
227  if (sm->type == DETECT_FLOWBITS &&
228  (((DetectFlowbitsData *)sm->ctx)->cmd != DETECT_FLOWBITS_CMD_SET)) {
229  return 0;
230  }
231  }
233  for ( ; sm != NULL; sm = sm->next) {
235  return 0;
236  /* we have enabled flowbits to be compatible with ip only sigs, as long
237  * as the sig only has a "set" flowbits */
238  if (sm->type == DETECT_FLOWBITS &&
239  (((DetectFlowbitsData *)sm->ctx)->cmd != DETECT_FLOWBITS_CMD_SET) ) {
240  return 0;
241  }
242  }
243 
245  /* Rule is IP only, but contains negated addresses. */
246  return 2;
247  }
248  if (!(de_ctx->flags & DE_QUIET)) {
249  SCLogDebug("IP-ONLY (%" PRIu32 "): source %s, dest %s", s->id,
250  s->flags & SIG_FLAG_SRC_ANY ? "ANY" : "SET",
251  s->flags & SIG_FLAG_DST_ANY ? "ANY" : "SET");
252  }
253  return 1;
254 }
255 
256 /** \internal
257  * \brief Test is a initialized signature is inspecting protocol detection only
258  * \param de_ctx detection engine ctx
259  * \param s the signature
260  * \retval 1 sig is dp only
261  * \retval 0 sig is not dp only
262  */
263 static int SignatureIsPDOnly(const DetectEngineCtx *de_ctx, const Signature *s)
264 {
265  /* explicit hook means no PD-only */
267  return 0;
268 
269  if (s->alproto != ALPROTO_UNKNOWN)
270  return 0;
271 
272  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL)
273  return 0;
274 
275  /* for now assume that all registered buffer types are incompatible */
276  if (SignatureInspectsBuffers(s)) {
277  SCReturnInt(0);
278  }
279 
280  /* TMATCH list can be ignored, it contains TAGs and
281  * tags are compatible to DP-only. */
282 
283  /* match list matches may be compatible to DP only. We follow the same
284  * logic as IP-only so we can use that flag */
285 
287  if (sm == NULL)
288  return 0;
289 
290  int pd = 0;
291  for ( ; sm != NULL; sm = sm->next) {
292  if (sm->type == DETECT_APP_LAYER_PROTOCOL) {
293  pd = 1;
294  } else {
295  /* flowbits are supported for dp only sigs, as long
296  * as the sig only has a "set" flowbits */
297  if (sm->type == DETECT_FLOWBITS) {
298  if ((((DetectFlowbitsData *)sm->ctx)->cmd != DETECT_FLOWBITS_CMD_SET) ) {
299  SCLogDebug("%u: not PD-only: flowbit settings other than 'set'", s->id);
300  return 0;
301  }
302  } else if (sm->type == DETECT_FLOW) {
304  SCLogDebug("%u: not PD-only: flow settings other than toserver/toclient", s->id);
305  return 0;
306  }
307  } else if ( !(sigmatch_table[sm->type].flags & SIGMATCH_IPONLY_COMPAT)) {
308  SCLogDebug("%u: not PD-only: %s not PD/IP-only compat", s->id, sigmatch_table[sm->type].name);
309  return 0;
310  }
311  }
312  }
313 
314  if (pd) {
315  SCLogDebug("PD-ONLY (%" PRIu32 ")", s->id);
316  }
317  return pd;
318 }
319 
320 /**
321  * \internal
322  * \brief Check if the initialized signature is inspecting the packet payload
323  * \param de_ctx detection engine ctx
324  * \param s the signature
325  * \retval 1 sig is inspecting the payload
326  * \retval 0 sig is not inspecting the payload
327  */
328 static int SignatureIsInspectingPayload(DetectEngineCtx *de_ctx, const Signature *s)
329 {
330 
331  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL) {
332  return 1;
333  }
334  return 0;
335 }
336 
337 /**
338  * \internal
339  * \brief check if a signature is decoder event matching only
340  * \param de_ctx detection engine
341  * \param s the signature to test
342  * \retval 0 not a DEOnly sig
343  * \retval 1 DEOnly sig
344  */
345 static int SignatureIsDEOnly(DetectEngineCtx *de_ctx, const Signature *s)
346 {
347  /* explicit hook means no DE-only */
349  SCReturnInt(0);
350 
351  if (s->alproto != ALPROTO_UNKNOWN) {
352  SCReturnInt(0);
353  }
354 
355  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL)
356  {
357  SCReturnInt(0);
358  }
359 
360  /* for now assume that all registered buffer types are incompatible */
361  if (SignatureInspectsBuffers(s)) {
362  SCReturnInt(0);
363  }
364 
365  /* check for conflicting keywords */
367  for ( ;sm != NULL; sm = sm->next) {
369  SCReturnInt(0);
370  }
371 
372  /* need at least one decode event keyword to be considered decode event. */
374  for ( ;sm != NULL; sm = sm->next) {
375  if (sm->type == DETECT_DECODE_EVENT)
376  goto deonly;
377  if (sm->type == DETECT_ENGINE_EVENT)
378  goto deonly;
379  if (sm->type == DETECT_STREAM_EVENT)
380  goto deonly;
381  }
382 
383  SCReturnInt(0);
384 
385 deonly:
386  if (!(de_ctx->flags & DE_QUIET)) {
387  SCLogDebug("DE-ONLY (%" PRIu32 "): source %s, dest %s", s->id,
388  s->flags & SIG_FLAG_SRC_ANY ? "ANY" : "SET",
389  s->flags & SIG_FLAG_DST_ANY ? "ANY" : "SET");
390  }
391 
392  SCReturnInt(1);
393 }
394 
395 #define MASK_TCP_INITDEINIT_FLAGS (TH_SYN|TH_RST|TH_FIN)
396 #define MASK_TCP_UNUSUAL_FLAGS (TH_URG|TH_ECN|TH_CWR)
397 
398 /* Create mask for this packet + it's flow if it has one
399  */
400 void
402  bool app_decoder_events)
403 {
404  if (!(PKT_IS_PSEUDOPKT(p))) {
405  (*mask) |= SIG_MASK_REQUIRE_REAL_PKT;
406  }
407  if (!(p->flags & PKT_NOPAYLOAD_INSPECTION) && p->payload_len > 0) {
408  SCLogDebug("packet has payload");
409  (*mask) |= SIG_MASK_REQUIRE_PAYLOAD;
410  } else if (p->flags & PKT_DETECT_HAS_STREAMDATA) {
411  SCLogDebug("stream data available");
412  (*mask) |= SIG_MASK_REQUIRE_PAYLOAD;
413  } else {
414  SCLogDebug("packet has no payload");
415  (*mask) |= SIG_MASK_REQUIRE_NO_PAYLOAD;
416  }
417 
418  if (p->events.cnt > 0 || app_decoder_events != 0 ||
419  (p->app_layer_events != NULL && p->app_layer_events->cnt)) {
420  SCLogDebug("packet/flow has events set");
422  }
423 
424  if (!(PKT_IS_PSEUDOPKT(p)) && PacketIsTCP(p)) {
425  const TCPHdr *tcph = PacketGetTCP(p);
426  if ((tcph->th_flags & MASK_TCP_INITDEINIT_FLAGS) != 0) {
428  }
429  if ((tcph->th_flags & MASK_TCP_UNUSUAL_FLAGS) != 0) {
431  }
432  }
433 
434  if (p->flags & PKT_HAS_FLOW) {
435  SCLogDebug("packet has flow");
436  (*mask) |= SIG_MASK_REQUIRE_FLOW;
437  }
438 }
439 
440 static int SignatureCreateMask(Signature *s)
441 {
442  SCEnter();
443 
447  }
448  if (s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL) {
450  SCLogDebug("sig requires payload");
451  }
452 
453  SigMatch *sm;
454  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
455  switch(sm->type) {
456  case DETECT_FLOWBITS:
457  {
458  /* figure out what flowbit action */
460  if (fb->cmd == DETECT_FLOWBITS_CMD_ISSET) {
461  /* not a mask flag, but still set it here */
463 
464  SCLogDebug("SIG_FLAG_REQUIRE_FLOWVAR set as sig has "
465  "flowbit isset option.");
466  }
467 
468  /* flow is required for any flowbit manipulation */
470  SCLogDebug("sig requires flow to be able to manipulate "
471  "flowbit(s)");
472  break;
473  }
474  case DETECT_FLOWINT:
475  /* flow is required for any flowint manipulation */
477  SCLogDebug("sig requires flow to be able to manipulate "
478  "flowint(s)");
479  break;
480  case DETECT_FLAGS:
481  {
482  DetectU8Data *fl = (DetectU8Data *)sm->ctx;
483 
484  uint8_t arg = 0;
485  if (fl->mode == DetectUintModeBitmask) {
486  arg = fl->arg2;
487  } else if (fl->mode == DetectUintModeNegBitmask && fl->arg2 == 0) {
488  arg = fl->arg1;
489  } else if (fl->mode == DetectUintModeEqual) {
490  arg = fl->arg1;
491  }
492  if (arg & MASK_TCP_INITDEINIT_FLAGS) {
494  SCLogDebug("sig requires SIG_MASK_REQUIRE_FLAGS_INITDEINIT");
495  }
496  if (arg & MASK_TCP_UNUSUAL_FLAGS) {
498  SCLogDebug("sig requires SIG_MASK_REQUIRE_FLAGS_UNUSUAL");
499  }
500 
501  break;
502  }
503  case DETECT_DSIZE:
504  {
505  DetectU16Data *ds = (DetectU16Data *)sm->ctx;
506  /* LT will include 0, so no payload.
507  * if GT is used in the same rule the
508  * flag will be set anyway. */
509  if (ds->mode == DETECT_UINT_RA || ds->mode == DETECT_UINT_GT ||
510  ds->mode == DETECT_UINT_NE || ds->mode == DETECT_UINT_GTE) {
511 
513  SCLogDebug("sig requires payload");
514 
515  } else if (ds->mode == DETECT_UINT_EQ) {
516  if (ds->arg1 > 0) {
518  SCLogDebug("sig requires payload");
519  } else {
521  SCLogDebug("sig requires no payload");
522  }
523  }
524  break;
525  }
526  case DETECT_DECODE_EVENT:
527  // fallthrough
528  case DETECT_STREAM_EVENT:
529  // fallthrough
531  // fallthrough
532  case DETECT_ENGINE_EVENT:
534  break;
535  }
536  }
537 
538  for (sm = s->init_data->smlists[DETECT_SM_LIST_POSTMATCH]; sm != NULL; sm = sm->next) {
539  switch (sm->type) {
540  case DETECT_CONFIG: {
542  if (fd->scope == CONFIG_SCOPE_FLOW) {
544  }
545  break;
546  }
547  }
548  }
549 
552  SCLogDebug("sig requires flow");
553  }
554 
555  if (s->flags & SIG_FLAG_APPLAYER) {
557  SCLogDebug("sig requires flow");
558  }
559 
560  SCLogDebug("mask %02X", s->mask);
561  SCReturnInt(0);
562 }
563 
564 static void SigInitStandardMpmFactoryContexts(DetectEngineCtx *de_ctx)
565 {
567 }
568 
569 /** \brief Pure-PCRE or bytetest rule */
570 static bool RuleInspectsPayloadHasNoMpm(const Signature *s)
571 {
572  return s->init_data->mpm_sm == NULL && s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL;
573 }
574 
575 static int RuleGetMpmPatternSize(const Signature *s)
576 {
577  if (s->init_data->mpm_sm == NULL)
578  return -1;
579  int mpm_list = s->init_data->mpm_sm_list;
580  if (mpm_list < 0)
581  return -1;
582  const DetectContentData *cd = (const DetectContentData *)s->init_data->mpm_sm->ctx;
583  if (cd == NULL)
584  return -1;
585  return (int)cd->content_len;
586 }
587 
588 static bool RuleMpmIsNegated(const Signature *s)
589 {
590  if (s->flags & SIG_FLAG_MPM_NEG)
591  return true;
592  if (s->init_data->mpm_sm == NULL)
593  return false;
594  int mpm_list = s->init_data->mpm_sm_list;
595  if (mpm_list < 0)
596  return false;
597  const DetectContentData *cd = (const DetectContentData *)s->init_data->mpm_sm->ctx;
598  if (cd == NULL)
599  return false;
600  return (cd->flags & DETECT_CONTENT_NEGATED) != 0;
601 }
602 
603 static SCJsonBuilder *RulesGroupPrintSghStats(const DetectEngineCtx *de_ctx,
604  const SigGroupHead *sgh, const int add_rules, const int add_mpm_stats)
605 {
606  uint32_t prefilter_cnt = 0;
607  uint32_t mpm_cnt = 0;
608  uint32_t nonmpm_cnt = 0;
609  uint32_t mpm_depth_cnt = 0;
610  uint32_t mpm_endswith_cnt = 0;
611  uint32_t negmpm_cnt = 0;
612  uint32_t any5_cnt = 0;
613  uint32_t payload_no_mpm_cnt = 0;
614  uint32_t syn_cnt = 0;
615 
616  uint32_t mpms_min = 0;
617  uint32_t mpms_max = 0;
618 
619  int max_buffer_type_id = de_ctx->buffer_type_id;
620 
621  struct {
622  uint32_t total;
623  uint32_t cnt;
624  uint32_t min;
625  uint32_t max;
626  } mpm_stats[max_buffer_type_id];
627  memset(mpm_stats, 0x00, sizeof(mpm_stats));
628 
629  uint32_t alstats[g_alproto_max];
630  memset(alstats, 0, g_alproto_max * sizeof(uint32_t));
631  uint32_t mpm_sizes[max_buffer_type_id][256];
632  memset(mpm_sizes, 0, sizeof(mpm_sizes));
633  uint32_t alproto_mpm_bufs[g_alproto_max][max_buffer_type_id];
634  memset(alproto_mpm_bufs, 0, sizeof(alproto_mpm_bufs));
635 
636  DEBUG_VALIDATE_BUG_ON(sgh->init == NULL);
637  if (sgh->init == NULL)
638  return NULL;
639 
640  SCJsonBuilder *js = SCJbNewObject();
641  if (unlikely(js == NULL))
642  return NULL;
643 
644  SCJbSetUint(js, "id", sgh->id);
645 
646  SCJbOpenArray(js, "rules");
647  for (uint32_t x = 0; x < sgh->init->sig_cnt; x++) {
648  const Signature *s = sgh->init->match_array[x];
649  if (s == NULL)
650  continue;
651 
652  int any = 0;
653  if (s->proto == NULL || s->proto->flags & DETECT_PROTO_ANY) {
654  any++;
655  }
656  if (s->flags & SIG_FLAG_DST_ANY) {
657  any++;
658  }
659  if (s->flags & SIG_FLAG_SRC_ANY) {
660  any++;
661  }
662  if (s->flags & SIG_FLAG_DP_ANY) {
663  any++;
664  }
665  if (s->flags & SIG_FLAG_SP_ANY) {
666  any++;
667  }
668  if (any == 5) {
669  any5_cnt++;
670  }
671 
672  prefilter_cnt += (s->init_data->prefilter_sm != NULL);
673  if (s->init_data->mpm_sm == NULL) {
674  nonmpm_cnt++;
675 
676  if (s->sm_arrays[DETECT_SM_LIST_MATCH] != NULL) {
677  SCLogDebug("SGH %p Non-MPM inspecting only packets. Rule %u", sgh, s->id);
678  }
679 
680  DetectPort *sp = s->sp;
681  DetectPort *dp = s->dp;
682 
683  if (s->flags & SIG_FLAG_TOSERVER && (dp->port == 0 && dp->port2 == 65535)) {
684  SCLogDebug("SGH %p Non-MPM toserver and to 'any'. Rule %u", sgh, s->id);
685  }
686  if (s->flags & SIG_FLAG_TOCLIENT && (sp->port == 0 && sp->port2 == 65535)) {
687  SCLogDebug("SGH %p Non-MPM toclient and to 'any'. Rule %u", sgh, s->id);
688  }
689 
691  syn_cnt++;
692  }
693 
694  } else {
695  int mpm_list = s->init_data->mpm_sm_list;
696  BUG_ON(mpm_list < 0);
697  const DetectContentData *cd = (const DetectContentData *)s->init_data->mpm_sm->ctx;
698  uint32_t size = cd->content_len < 256 ? cd->content_len : 255;
699 
700  mpm_sizes[mpm_list][size]++;
701  alproto_mpm_bufs[s->alproto][mpm_list]++;
702 
703  if (mpm_list == DETECT_SM_LIST_PMATCH) {
704  if (size == 1) {
705  DetectPort *sp = s->sp;
706  DetectPort *dp = s->dp;
707  if (s->flags & SIG_FLAG_TOSERVER) {
708  if (dp->port == 0 && dp->port2 == 65535) {
709  SCLogDebug("SGH %p toserver 1byte fast_pattern to ANY. Rule %u", sgh, s->id);
710  } else {
711  SCLogDebug("SGH %p toserver 1byte fast_pattern to port(s) %u-%u. Rule %u", sgh, dp->port, dp->port2, s->id);
712  }
713  }
714  if (s->flags & SIG_FLAG_TOCLIENT) {
715  if (sp->port == 0 && sp->port2 == 65535) {
716  SCLogDebug("SGH %p toclient 1byte fast_pattern to ANY. Rule %u", sgh, s->id);
717  } else {
718  SCLogDebug("SGH %p toclient 1byte fast_pattern to port(s) %u-%u. Rule %u", sgh, sp->port, sp->port2, s->id);
719  }
720  }
721  }
722  }
723 
724  uint32_t w = PatternStrength(cd->content, cd->content_len);
725  if (mpms_min == 0)
726  mpms_min = w;
727  if (w < mpms_min)
728  mpms_min = w;
729  if (w > mpms_max)
730  mpms_max = w;
731 
732  BUG_ON(mpm_list >= max_buffer_type_id);
733  mpm_stats[mpm_list].total += w;
734  mpm_stats[mpm_list].cnt++;
735  if (mpm_stats[mpm_list].min == 0 || w < mpm_stats[mpm_list].min)
736  mpm_stats[mpm_list].min = w;
737  if (w > mpm_stats[mpm_list].max)
738  mpm_stats[mpm_list].max = w;
739 
740  mpm_cnt++;
741 
742  if (w < 10) {
743  SCLogDebug("SGH %p Weak MPM Pattern on %s. Rule %u", sgh, DetectListToString(mpm_list), s->id);
744  }
745  if (w < 10 && any == 5) {
746  SCLogDebug("SGH %p Weak MPM Pattern on %s, rule is 5xAny. Rule %u", sgh, DetectListToString(mpm_list), s->id);
747  }
748 
749  if (cd->flags & DETECT_CONTENT_NEGATED) {
750  SCLogDebug("SGH %p MPM Pattern on %s, is negated. Rule %u", sgh, DetectListToString(mpm_list), s->id);
751  negmpm_cnt++;
752  }
753  if (cd->flags & DETECT_CONTENT_ENDS_WITH) {
754  mpm_endswith_cnt++;
755  }
756  if (cd->flags & DETECT_CONTENT_DEPTH) {
757  mpm_depth_cnt++;
758  }
759  }
760 
761  if (RuleInspectsPayloadHasNoMpm(s)) {
762  SCLogDebug("SGH %p No MPM. Payload inspecting. Rule %u", sgh, s->id);
763  payload_no_mpm_cnt++;
764  }
765 
766  alstats[s->alproto]++;
767 
768  if (add_rules) {
769  SCJsonBuilder *e = SCJbNewObject();
770  if (e != NULL) {
771  SCJbSetUint(e, "sig_id", s->id);
772  SCJbClose(e);
773  SCJbAppendObject(js, e);
774  SCJbFree(e);
775  }
776  }
777  }
778  SCJbClose(js);
779 
780  SCJbOpenObject(js, "stats");
781  SCJbSetUint(js, "total", sgh->init->sig_cnt);
782 
783  SCJbOpenObject(js, "types");
784  SCJbSetUint(js, "mpm", mpm_cnt);
785  SCJbSetUint(js, "non_mpm", nonmpm_cnt);
786  SCJbSetUint(js, "mpm_depth", mpm_depth_cnt);
787  SCJbSetUint(js, "mpm_endswith", mpm_endswith_cnt);
788  SCJbSetUint(js, "negated_mpm", negmpm_cnt);
789  SCJbSetUint(js, "payload_but_no_mpm", payload_no_mpm_cnt);
790  SCJbSetUint(js, "prefilter", prefilter_cnt);
791  SCJbSetUint(js, "syn", syn_cnt);
792  SCJbSetUint(js, "any5", any5_cnt);
793  SCJbClose(js);
794 
795  for (AppProto i = 0; i < g_alproto_max; i++) {
796  if (alstats[i] > 0) {
797  const char *proto_name = (i == ALPROTO_UNKNOWN) ? "payload" : AppProtoToString(i);
798  SCJbOpenObject(js, proto_name);
799  SCJbSetUint(js, "total", alstats[i]);
800 
801  for (int y = 0; y < max_buffer_type_id; y++) {
802  if (alproto_mpm_bufs[i][y] == 0)
803  continue;
804 
805  const char *name;
808  else
810 
811  SCJbSetUint(js, name, alproto_mpm_bufs[i][y]);
812  }
813  SCJbClose(js);
814  }
815  }
816 
817  if (add_mpm_stats) {
818  SCJbOpenObject(js, "mpm");
819 
820  for (int i = 0; i < max_buffer_type_id; i++) {
821  if (mpm_stats[i].cnt > 0) {
822  const char *name;
825  else
827 
828  SCJbOpenArray(js, name);
829 
830  for (int y = 0; y < 256; y++) {
831  if (mpm_sizes[i][y] == 0)
832  continue;
833 
834  SCJsonBuilder *e = SCJbNewObject();
835  if (e != NULL) {
836  SCJbSetUint(e, "size", y);
837  SCJbSetUint(e, "count", mpm_sizes[i][y]);
838  SCJbClose(e);
839  SCJbAppendObject(js, e);
840  SCJbFree(e);
841  }
842  }
843 
844  SCJsonBuilder *e = SCJbNewObject();
845  if (e != NULL) {
846  SCJbSetUint(e, "total", mpm_stats[i].cnt);
847  SCJbSetUint(e, "avg_strength", mpm_stats[i].total / mpm_stats[i].cnt);
848  SCJbSetUint(e, "min_strength", mpm_stats[i].min);
849  SCJbSetUint(e, "max_strength", mpm_stats[i].max);
850  SCJbClose(e);
851  SCJbAppendObject(js, e);
852  SCJbFree(e);
853  }
854 
855  SCJbClose(js);
856  }
857  }
858  SCJbClose(js);
859  }
860  SCJbClose(js);
861 
862  SCJbSetUint(js, "score", sgh->init->score);
863  SCJbClose(js);
864 
865  return js;
866 }
867 
868 static void RulesDumpGrouping(const DetectEngineCtx *de_ctx,
869  const int add_rules, const int add_mpm_stats)
870 {
871  SCJsonBuilder *js = SCJbNewObject();
872  if (unlikely(js == NULL))
873  return;
874 
875  for (int p = 0; p < 256; p++) {
876  if (p == IPPROTO_TCP || p == IPPROTO_UDP) {
877  const char *name = (p == IPPROTO_TCP) ? "tcp" : "udp";
878 
879  SCJbOpenObject(js, name);
880  SCJbOpenArray(js, "toserver");
881  const DetectPort *list =
882  (p == IPPROTO_TCP) ? de_ctx->flow_gh[1].tcp : de_ctx->flow_gh[1].udp;
883  while (list != NULL) {
884  SCJsonBuilder *port = SCJbNewObject();
885  SCJbSetUint(port, "port", list->port);
886  SCJbSetUint(port, "port2", list->port2);
887 
888  SCJsonBuilder *stats =
889  RulesGroupPrintSghStats(de_ctx, list->sh, add_rules, add_mpm_stats);
890  SCJbSetObject(port, "rulegroup", stats);
891  SCJbFree(stats);
892  SCJbClose(port);
893  SCJbAppendObject(js, port);
894  SCJbFree(port);
895 
896  list = list->next;
897  }
898  SCJbClose(js); // toserver array
899 
900  SCJbOpenArray(js, "toclient");
901  list = (p == IPPROTO_TCP) ? de_ctx->flow_gh[0].tcp :
902  de_ctx->flow_gh[0].udp;
903  while (list != NULL) {
904  SCJsonBuilder *port = SCJbNewObject();
905  SCJbSetUint(port, "port", list->port);
906  SCJbSetUint(port, "port2", list->port2);
907 
908  SCJsonBuilder *stats =
909  RulesGroupPrintSghStats(de_ctx, list->sh, add_rules, add_mpm_stats);
910  SCJbSetObject(port, "rulegroup", stats);
911  SCJbFree(stats);
912  SCJbClose(port);
913  SCJbAppendObject(js, port);
914  SCJbFree(port);
915 
916  list = list->next;
917  }
918  SCJbClose(js); // toclient array
919  SCJbClose(js);
920  } else if (p == IPPROTO_ICMP || p == IPPROTO_ICMPV6) {
921  const char *name = (p == IPPROTO_ICMP) ? "icmpv4" : "icmpv6";
922  SCJbOpenObject(js, name);
923  if (de_ctx->flow_gh[1].sgh[p]) {
924  SCJbOpenObject(js, "toserver");
925  SCJsonBuilder *stats = RulesGroupPrintSghStats(
926  de_ctx, de_ctx->flow_gh[1].sgh[p], add_rules, add_mpm_stats);
927  SCJbSetObject(js, "rulegroup", stats);
928  SCJbFree(stats);
929  SCJbClose(js);
930  }
931  if (de_ctx->flow_gh[0].sgh[p]) {
932  SCJbOpenObject(js, "toclient");
933  SCJsonBuilder *stats = RulesGroupPrintSghStats(
934  de_ctx, de_ctx->flow_gh[0].sgh[p], add_rules, add_mpm_stats);
935  SCJbSetObject(js, "rulegroup", stats);
936  SCJbFree(stats);
937  SCJbClose(js);
938  }
939  SCJbClose(js);
940  }
941  }
942  SCJbClose(js);
943 
944  const char *filename = "rule_group.json";
945  const char *log_dir = SCConfigGetLogDirectory();
946  char log_path[PATH_MAX] = "";
947  snprintf(log_path, sizeof(log_path), "%s/%s", log_dir, filename);
948 
949  FILE *fp = fopen(log_path, "w");
950  if (fp != NULL) {
951  fwrite(SCJbPtr(js), SCJbLen(js), 1, fp);
952  (void)fclose(fp);
953  }
954  SCJbFree(js);
955 }
956 
957 static int RulesGroupByIPProto(DetectEngineCtx *de_ctx)
958 {
959  Signature *s = de_ctx->sig_list;
960 
961  SigGroupHead *sgh_ts[256] = {NULL};
962  SigGroupHead *sgh_tc[256] = {NULL};
963 
964  for ( ; s != NULL; s = s->next) {
965  if (s->type == SIG_TYPE_IPONLY)
966  continue;
967 
968  /* traverse over IP protocol list from libc */
969  for (int p = 0; p < 256; p++) {
970  if (p == IPPROTO_TCP || p == IPPROTO_UDP) {
971  continue;
972  }
973 
974  if (!DetectProtoContainsProto(&s->init_data->proto, p)) {
975  continue;
976  }
977 
978  /* Signatures that are ICMP, SCTP, not IP only are handled here */
979  if (s->flags & SIG_FLAG_TOCLIENT) {
980  SigGroupHeadAppendSig(de_ctx, &sgh_tc[p], s);
981  }
982  if (s->flags & SIG_FLAG_TOSERVER) {
983  SigGroupHeadAppendSig(de_ctx, &sgh_ts[p], s);
984  }
985  }
986  }
987 
988  /* lets look at deduplicating this list */
991 
992  uint32_t cnt = 0;
993  uint32_t own = 0;
994  uint32_t ref = 0;
995  int p;
996  for (p = 0; p < 256; p++) {
997  if (p == IPPROTO_TCP || p == IPPROTO_UDP)
998  continue;
999  if (sgh_ts[p] == NULL)
1000  continue;
1001 
1002  cnt++;
1003 
1004  SigGroupHead *lookup_sgh = SigGroupHeadHashLookup(de_ctx, sgh_ts[p]);
1005  if (lookup_sgh == NULL) {
1006  SCLogDebug("proto group %d sgh %p is the original", p, sgh_ts[p]);
1007 
1008  SigGroupHeadSetSigCnt(sgh_ts[p], 0);
1009  SigGroupHeadBuildMatchArray(de_ctx, sgh_ts[p], 0);
1010 
1011  SigGroupHeadHashAdd(de_ctx, sgh_ts[p]);
1012  SigGroupHeadStore(de_ctx, sgh_ts[p]);
1013  own++;
1014  } else {
1015  SCLogDebug("proto group %d sgh %p is a copy", p, sgh_ts[p]);
1016 
1017  SigGroupHeadFree(de_ctx, sgh_ts[p]);
1018  sgh_ts[p] = lookup_sgh;
1019  ref++;
1020  }
1021  }
1022  SCLogPerf("OTHER %s: %u proto groups, %u unique SGH's, %u copies",
1023  "toserver", cnt, own, ref);
1024 
1025  cnt = 0;
1026  own = 0;
1027  ref = 0;
1028  for (p = 0; p < 256; p++) {
1029  if (p == IPPROTO_TCP || p == IPPROTO_UDP)
1030  continue;
1031  if (sgh_tc[p] == NULL)
1032  continue;
1033 
1034  cnt++;
1035 
1036  SigGroupHead *lookup_sgh = SigGroupHeadHashLookup(de_ctx, sgh_tc[p]);
1037  if (lookup_sgh == NULL) {
1038  SCLogDebug("proto group %d sgh %p is the original", p, sgh_tc[p]);
1039 
1040  SigGroupHeadSetSigCnt(sgh_tc[p], 0);
1041  SigGroupHeadBuildMatchArray(de_ctx, sgh_tc[p], 0);
1042 
1043  SigGroupHeadHashAdd(de_ctx, sgh_tc[p]);
1044  SigGroupHeadStore(de_ctx, sgh_tc[p]);
1045  own++;
1046 
1047  } else {
1048  SCLogDebug("proto group %d sgh %p is a copy", p, sgh_tc[p]);
1049 
1050  SigGroupHeadFree(de_ctx, sgh_tc[p]);
1051  sgh_tc[p] = lookup_sgh;
1052  ref++;
1053  }
1054  }
1055  SCLogPerf("OTHER %s: %u proto groups, %u unique SGH's, %u copies",
1056  "toclient", cnt, own, ref);
1057 
1058  for (p = 0; p < 256; p++) {
1059  if (p == IPPROTO_TCP || p == IPPROTO_UDP)
1060  continue;
1061 
1062  de_ctx->flow_gh[0].sgh[p] = sgh_tc[p];
1063  de_ctx->flow_gh[1].sgh[p] = sgh_ts[p];
1064  }
1065 
1066  return 0;
1067 }
1068 
1069 static int PortIsPriority(const DetectEngineCtx *de_ctx, const DetectPort *a, int ipproto)
1070 {
1072  if (ipproto == IPPROTO_UDP)
1074 
1075  while (w) {
1076  /* Make sure the priority port falls in the port range of a */
1077  DEBUG_VALIDATE_BUG_ON(a->port > a->port2);
1078  if (a->port == w->port && w->port2 == a->port2) {
1079  return 1;
1080  }
1081  w = w->next;
1082  }
1083 
1084  return 0;
1085 }
1086 
1087 static int RuleSetScore(Signature *s)
1088 {
1089  DetectPort *p = NULL;
1090  if (s->flags & SIG_FLAG_TOSERVER)
1091  p = s->dp;
1092  else if (s->flags & SIG_FLAG_TOCLIENT)
1093  p = s->sp;
1094  else
1095  return 0;
1096 
1097  /* for sigs that don't use 'any' as port, see if we want to
1098  * prioritize poor sigs */
1099  int wl = 0;
1100  if (!(p->port == 0 && p->port2 == 65535)) {
1101  /* pure pcre, bytetest, etc rules */
1102  if (RuleInspectsPayloadHasNoMpm(s)) {
1103  SCLogDebug("Rule %u MPM has 1 byte fast_pattern. Prioritizing SGH's.", s->id);
1105 
1106  } else if (RuleMpmIsNegated(s)) {
1107  SCLogDebug("Rule %u MPM is negated. Prioritizing SGH's.", s->id);
1109 
1110  /* one byte pattern in packet/stream payloads */
1111  } else if (s->init_data->mpm_sm != NULL &&
1113  RuleGetMpmPatternSize(s) == 1) {
1114  SCLogDebug("Rule %u No MPM. Payload inspecting. Prioritizing SGH's.", s->id);
1116 
1118  SCLogDebug("Rule %u Needs SYN, so inspected often. Prioritizing SGH's.", s->id);
1120  }
1121  }
1122 
1123  s->init_data->score = wl;
1124  return wl;
1125 }
1126 
1127 static int SortCompare(const void *a, const void *b)
1128 {
1129  const DetectPort *pa = *(const DetectPort **)a;
1130  const DetectPort *pb = *(const DetectPort **)b;
1131 
1132  if (pa->sh->init->score < pb->sh->init->score) {
1133  return 1;
1134  } else if (pa->sh->init->score > pb->sh->init->score) {
1135  return -1;
1136  }
1137 
1138  if (pa->sh->init->sig_cnt < pb->sh->init->sig_cnt) {
1139  return 1;
1140  } else if (pa->sh->init->sig_cnt > pb->sh->init->sig_cnt) {
1141  return -1;
1142  }
1143 
1144  /* Hack to make the qsort output deterministic across platforms.
1145  * This had to be done because the order of equal elements sorted
1146  * by qsort is undeterministic and showed different output on BSD,
1147  * MacOS and Windows. Sorting based on id makes it deterministic. */
1148  if (pa->sh->id < pb->sh->id)
1149  return -1;
1150 
1151  return 1;
1152 }
1153 
1154 static inline void SortGroupList(
1155  uint32_t *groups, DetectPort **list, int (*CompareFunc)(const void *, const void *))
1156 {
1157  int cnt = 0;
1158  for (DetectPort *x = *list; x != NULL; x = x->next) {
1159  DEBUG_VALIDATE_BUG_ON(x->port > x->port2);
1160  cnt++;
1161  }
1162  if (cnt <= 1)
1163  return;
1164 
1165  /* build temporary array to sort with qsort */
1166  DetectPort **array = (DetectPort **)SCCalloc(cnt, sizeof(DetectPort *));
1167  if (array == NULL)
1168  return;
1169 
1170  int idx = 0;
1171  for (DetectPort *x = *list; x != NULL;) {
1172  /* assign a temporary id to resolve otherwise equal groups */
1173  x->sh->id = idx + 1;
1174  SigGroupHeadSetSigCnt(x->sh, 0);
1175  DetectPort *next = x->next;
1176  x->next = x->prev = x->last = NULL;
1177  DEBUG_VALIDATE_BUG_ON(x->port > x->port2);
1178  array[idx++] = x;
1179  x = next;
1180  }
1181  DEBUG_VALIDATE_BUG_ON(cnt != idx);
1182 
1183  qsort(array, idx, sizeof(DetectPort *), SortCompare);
1184 
1185  /* rebuild the list based on the qsort-ed array */
1186  DetectPort *new_list = NULL, *tail = NULL;
1187  for (int i = 0; i < idx; i++) {
1188  DetectPort *p = array[i];
1189  /* unset temporary group id */
1190  p->sh->id = 0;
1191 
1192  if (new_list == NULL) {
1193  new_list = p;
1194  }
1195  if (tail != NULL) {
1196  tail->next = p;
1197  }
1198  p->prev = tail;
1199  tail = p;
1200  }
1201 
1202  *list = new_list;
1203  *groups = idx;
1204 
1205 #if DEBUG
1206  int dbgcnt = 0;
1207  SCLogDebug("SORTED LIST:");
1208  for (DetectPort *tmp = *list; tmp != NULL; tmp = tmp->next) {
1209  SCLogDebug("item:= [%u:%u]; score: %d; sig_cnt: %d", tmp->port, tmp->port2,
1210  tmp->sh->init->score, tmp->sh->init->sig_cnt);
1211  dbgcnt++;
1212  BUG_ON(dbgcnt > cnt);
1213  }
1214 #endif
1215  SCFree(array);
1216 }
1217 /** \internal
1218  * \brief Create a list of DetectPort objects sorted based on CompareFunc's
1219  * logic.
1220  *
1221  * List can limit the number of groups. In this case an extra "join" group
1222  * is created that contains the sigs belonging to that. It's *appended* to
1223  * the list, meaning that if the list is walked linearly it's found last.
1224  * The joingr is meant to be a catch all.
1225  *
1226  */
1227 static int CreateGroupedPortList(DetectEngineCtx *de_ctx, DetectPort *port_list,
1228  DetectPort **newhead, uint32_t unique_groups,
1229  int (*CompareFunc)(const void *, const void *))
1230 {
1231  DetectPort *tmplist = NULL, *joingr = NULL;
1232  uint32_t groups = 0;
1233 
1234  /* insert the ports into the tmplist, where it will
1235  * be sorted descending on 'cnt' and on whether a group
1236  * is prioritized. */
1237  tmplist = port_list;
1238  SortGroupList(&groups, &tmplist, SortCompare);
1239  uint32_t left = unique_groups;
1240  if (left == 0)
1241  left = groups;
1242 
1243  /* create another list: take the port groups from above
1244  * and add them to the 2nd list until we have met our
1245  * count. The rest is added to the 'join' group. */
1246  DetectPort *tmplist2 = NULL, *tmplist2_tail = NULL;
1247  DetectPort *gr, *next_gr;
1248  for (gr = tmplist; gr != NULL;) {
1249  next_gr = gr->next;
1250 
1251  SCLogDebug("temp list gr %p %u:%u", gr, gr->port, gr->port2);
1252  DetectPortPrint(gr);
1253 
1254  /* if we've set up all the unique groups, add the rest to the
1255  * catch-all joingr */
1256  if (left == 0) {
1257  if (joingr == NULL) {
1258  DetectPortParse(de_ctx, &joingr, "0:65535");
1259  if (joingr == NULL) {
1260  goto error;
1261  }
1262  SCLogDebug("joingr => %u-%u", joingr->port, joingr->port2);
1263  joingr->next = NULL;
1264  }
1265  SigGroupHeadCopySigs(de_ctx, gr->sh, &joingr->sh);
1266 
1267  /* when a group's sigs are added to the joingr, we can free it */
1268  gr->next = NULL;
1269  DetectPortFree(de_ctx, gr);
1270  /* append */
1271  } else {
1272  gr->next = NULL;
1273 
1274  if (tmplist2 == NULL) {
1275  tmplist2 = gr;
1276  tmplist2_tail = gr;
1277  } else {
1278  tmplist2_tail->next = gr;
1279  tmplist2_tail = gr;
1280  }
1281  }
1282 
1283  if (left > 0)
1284  left--;
1285 
1286  gr = next_gr;
1287  }
1288 
1289  /* if present, append the joingr that covers the rest */
1290  if (joingr != NULL) {
1291  SCLogDebug("appending joingr %p %u:%u", joingr, joingr->port, joingr->port2);
1292 
1293  if (tmplist2 == NULL) {
1294  tmplist2 = joingr;
1295  // tmplist2_tail = joingr;
1296  } else {
1297  tmplist2_tail->next = joingr;
1298  // tmplist2_tail = joingr;
1299  }
1300  } else {
1301  SCLogDebug("no joingr");
1302  }
1303 
1304  /* pass back our new list to the caller */
1305  *newhead = tmplist2;
1306  DetectPortPrintList(*newhead);
1307 
1308  return 0;
1309 error:
1310  return -1;
1311 }
1312 
1313 #define UNDEFINED_PORT 0
1314 #define RANGE_PORT 1
1315 #define SINGLE_PORT 2
1317 typedef struct UniquePortPoint_ {
1318  uint16_t port; /* value of the port */
1319  bool single; /* is the port single or part of a range */
1321 
1322 /**
1323  * \brief Function to set unique port points. Consider all the ports
1324  * flattened out on one line, set the points that correspond
1325  * to a valid port. Also store whether the port point stored
1326  * was a single port or part of a range.
1327  *
1328  * \param p Port object to be set
1329  * \param unique_list List of unique port points to be updated
1330  * \param size_list Current size of the list
1331  *
1332  * \return Updated size of the list
1333  */
1334 static inline uint32_t SetUniquePortPoints(
1335  const DetectPort *p, uint8_t *unique_list, uint32_t size_list)
1336 {
1337  if (unique_list[p->port] == UNDEFINED_PORT) {
1338  if (p->port == p->port2) {
1339  unique_list[p->port] = SINGLE_PORT;
1340  } else {
1341  unique_list[p->port] = RANGE_PORT;
1342  }
1343  size_list++;
1344  } else if (((unique_list[p->port] == SINGLE_PORT) && (p->port != p->port2)) ||
1345  ((unique_list[p->port] == RANGE_PORT) && (p->port == p->port2))) {
1346  if ((p->port != UINT16_MAX) && (unique_list[p->port + 1] == UNDEFINED_PORT)) {
1347  unique_list[p->port + 1] = RANGE_PORT;
1348  size_list++;
1349  }
1350  }
1351 
1352  /* Treat right boundary as single point to avoid creating unneeded
1353  * ranges later on */
1354  if (unique_list[p->port2] == UNDEFINED_PORT) {
1355  size_list++;
1356  }
1357  unique_list[p->port2] = SINGLE_PORT;
1358  return size_list;
1359 }
1360 
1361 /**
1362  * \brief Function to set the *final* unique port points and save them
1363  * for later use. The points are already sorted because of the way
1364  * they have been retrieved and saved earlier for use at this point.
1365  *
1366  * \param unique_list List of the unique port points to be used
1367  * \param size_unique_arr Number of unique port points
1368  * \param final_arr List of the final unique port points to be created
1369  */
1370 static inline void SetFinalUniquePortPoints(
1371  const uint8_t *unique_list, const uint32_t size_unique_arr, UniquePortPoint *final_arr)
1372 {
1373  for (uint32_t i = 0, j = 0; i < (UINT16_MAX + 1); i++) {
1374  DEBUG_VALIDATE_BUG_ON(j > size_unique_arr);
1375  if (unique_list[i] == RANGE_PORT) {
1376  final_arr[j].port = (uint16_t)i;
1377  final_arr[j++].single = false;
1378  } else if (unique_list[i] == SINGLE_PORT) {
1379  final_arr[j].port = (uint16_t)i;
1380  final_arr[j++].single = true;
1381  }
1382  }
1383 }
1384 
1385 /**
1386  * \brief Function to create the list of ports with the smallest ranges
1387  * by resolving overlaps and end point conditions. These contain the
1388  * correct SGHs as well after going over the interval tree to find
1389  * any range overlaps.
1390  *
1391  * \param de_ctx Detection Engine Context
1392  * \param unique_list Final list of unique port points
1393  * \param size_list Size of the unique_list
1394  * \param it Pointer to the interval tree
1395  * \param list Pointer to the list where final ports will be stored
1396  *
1397  * \return 0 on success, -1 otherwise
1398  */
1399 static inline int CreatePortList(DetectEngineCtx *de_ctx, const uint8_t *unique_list,
1400  const uint32_t size_list, SCPortIntervalTree *it, DetectPort **list)
1401 {
1402  /* Only do the operations if there is at least one unique port */
1403  if (size_list == 0)
1404  return 0;
1405  UniquePortPoint *final_unique_points =
1406  (UniquePortPoint *)SCCalloc(size_list, sizeof(UniquePortPoint));
1407  if (final_unique_points == NULL)
1408  return -1;
1409  SetFinalUniquePortPoints(unique_list, size_list, final_unique_points);
1410  /* Handle edge case when there is just one unique port */
1411  if (size_list == 1) {
1413  de_ctx, final_unique_points[0].port, final_unique_points[0].port, &it->tree, list);
1414  } else {
1415  UniquePortPoint *p1 = &final_unique_points[0];
1416  UniquePortPoint *p2 = &final_unique_points[1];
1417  uint16_t port = p1 ? p1->port : 0; // just for cppcheck
1418  uint16_t port2 = p2->port;
1419  for (uint32_t i = 1; i < size_list; i++) {
1420  DEBUG_VALIDATE_BUG_ON(port > port2);
1421  if ((p1 && p1->single) && p2->single) {
1422  SCPortIntervalFindOverlappingRanges(de_ctx, port, port, &it->tree, list);
1423  SCPortIntervalFindOverlappingRanges(de_ctx, port2, port2, &it->tree, list);
1424  port = port2 + 1;
1425  } else if (p1 && p1->single) {
1426  SCPortIntervalFindOverlappingRanges(de_ctx, port, port, &it->tree, list);
1427  if ((port2 > port + 1)) {
1429  de_ctx, port + 1, port2 - 1, &it->tree, list);
1430  port = port2;
1431  } else {
1432  port = port + 1;
1433  }
1434  } else if (p2->single) {
1435  /* If port2 is boundary and less or equal to port + 1, create a range
1436  * keeping the boundary away as it is single port */
1437  if ((port2 >= port + 1)) {
1438  SCPortIntervalFindOverlappingRanges(de_ctx, port, port2 - 1, &it->tree, list);
1439  }
1440  /* Deal with port2 as it is a single port */
1441  SCPortIntervalFindOverlappingRanges(de_ctx, port2, port2, &it->tree, list);
1442  port = port2 + 1;
1443  } else {
1444  if ((port2 > port + 1)) {
1445  SCPortIntervalFindOverlappingRanges(de_ctx, port, port2 - 1, &it->tree, list);
1446  port = port2;
1447  } else {
1448  SCPortIntervalFindOverlappingRanges(de_ctx, port, port2, &it->tree, list);
1449  port = port2 + 1;
1450  }
1451  }
1452  /* if the current port matches the p2->port, assign it to p1 so that
1453  * there is a UniquePortPoint object to check other info like whether
1454  * the port with this value is single */
1455  if (port == p2->port) {
1456  p1 = p2;
1457  } else {
1458  p1 = NULL;
1459  }
1460  if (i + 1 < size_list) {
1461  p2 = &final_unique_points[i + 1];
1462  port2 = p2->port;
1463  }
1464  }
1465  }
1466  /* final_unique_points array is no longer needed */
1467  SCFree(final_unique_points);
1468  return 0;
1469 }
1470 
1471 static bool SigIsEthernet(const Signature *s)
1472 {
1473  return ((s->init_data->proto.flags & DETECT_PROTO_ETHERNET));
1474 }
1475 
1476 static bool SigIsEthernetAddToIP(const Signature *s)
1477 {
1478  /* ARP and IP are mutually exclusive, so don't add an ARP rule
1479  * to IP groups. */
1480  if (s->init_data->proto.flags & DETECT_PROTO_ARP) {
1481  SCLogDebug("rule %u: ARP is not for IP", s->id);
1482  return false;
1483  }
1484 
1485  /* all other Ethernet may be IP as well, so add to these groups. */
1486  return true;
1487 }
1488 
1489 static bool SigIsEthernetAddToNonIP(const Signature *s)
1490 {
1491  /* add all Ethernet sigs to the Non IP group as there isn't enough
1492  * info to know for sure if they are looking for IP traffic or not. */
1493  return (SigIsEthernet(s));
1494 }
1495 
1496 static DetectPort *RulesGroupByPorts(DetectEngineCtx *de_ctx, uint8_t ipproto, uint32_t direction)
1497 {
1498  /* step 1: create a hash of 'DetectPort' objects based on all the
1499  * rules. Each object will have a SGH with the sigs added
1500  * that belong to the SGH. */
1502 
1503  uint32_t size_unique_port_arr = 0;
1504  const Signature *s = de_ctx->sig_list;
1505  DetectPort *list = NULL;
1506 
1507  uint8_t *unique_port_points = (uint8_t *)SCCalloc(UINT16_MAX + 1, sizeof(uint8_t));
1508  if (unique_port_points == NULL)
1509  return NULL;
1510 
1511  while (s) {
1512  /* IP Only rules are handled separately */
1513  if (s->type == SIG_TYPE_IPONLY)
1514  goto next;
1515 
1516  if (SigIsEthernet(s)) {
1517  if (!SigIsEthernetAddToIP(s)) {
1518  SCLogDebug("rule %u: not for IP", s->id);
1519  goto next;
1520  }
1521  SCLogDebug("rule %u: add ethernet rule to IP group", s->id);
1522  } else {
1523  /* Protocol does not match the Signature protocol and is non of IP, pkthdr */
1524  if (!DetectProtoContainsProto(&s->init_data->proto, ipproto)) {
1525  SCLogDebug("skip");
1526  goto next;
1527  }
1528  /* Direction does not match Signature direction */
1529  if (direction == SIG_FLAG_TOSERVER) {
1530  if (!(s->flags & SIG_FLAG_TOSERVER))
1531  goto next;
1532  } else if (direction == SIG_FLAG_TOCLIENT) {
1533  if (!(s->flags & SIG_FLAG_TOCLIENT))
1534  goto next;
1535  }
1536 
1537  /* see if we want to exclude directionless sigs that really care only for
1538  * to_server syn scans/floods */
1542  (!(s->dp->port == 0 && s->dp->port2 == 65535))) {
1543  SCLogWarning("rule %u: SYN-only to port(s) %u:%u "
1544  "w/o direction specified, disabling for toclient direction",
1545  s->id, s->dp->port, s->dp->port2);
1546  goto next;
1547  }
1548  }
1549 
1550  DetectPort *p = NULL;
1551  if (direction == SIG_FLAG_TOSERVER)
1552  p = s->dp;
1553  else if (direction == SIG_FLAG_TOCLIENT)
1554  p = s->sp;
1555  else
1556  BUG_ON(1);
1557 
1558  int wl = s->init_data->score;
1559  while (p) {
1560  int pwl = PortIsPriority(de_ctx, p, ipproto) ? DETECT_PGSCORE_RULE_PORT_PRIORITIZED : 0;
1561  pwl = MAX(wl,pwl);
1562 
1563  DetectPort *lookup = DetectPortHashLookup(de_ctx, p);
1564  if (lookup) {
1565  SigGroupHeadAppendSig(de_ctx, &lookup->sh, s);
1566  lookup->sh->init->score = MAX(lookup->sh->init->score, pwl);
1567  } else {
1569  BUG_ON(tmp2 == NULL);
1570  SigGroupHeadAppendSig(de_ctx, &tmp2->sh, s);
1571  tmp2->sh->init->score = pwl;
1572  DetectPortHashAdd(de_ctx, tmp2);
1573  size_unique_port_arr =
1574  SetUniquePortPoints(tmp2, unique_port_points, size_unique_port_arr);
1575  }
1576 
1577  p = p->next;
1578  }
1579  next:
1580  s = s->next;
1581  }
1582 
1583  /* step 2: create a list of the smallest port ranges with
1584  * appropriate SGHs */
1585 
1586  /* Create an interval tree of all the given ports to make the search
1587  * for overlaps later on easier */
1589  if (it == NULL)
1590  goto error;
1591 
1592  HashListTableBucket *htb = NULL;
1593  for (htb = HashListTableGetListHead(de_ctx->dport_hash_table); htb != NULL;
1594  htb = HashListTableGetListNext(htb)) {
1596  if (SCPortIntervalInsert(de_ctx, it, p) != SC_OK) {
1597  SCLogDebug("Port was not inserted in the tree");
1598  goto error;
1599  }
1600  }
1601 
1602  /* Create a sorted list of ports in ascending order after resolving overlaps
1603  * and corresponding SGHs */
1604  if (CreatePortList(de_ctx, unique_port_points, size_unique_port_arr, it, &list) < 0)
1605  goto error;
1606 
1607  /* unique_port_points array is no longer needed */
1608  SCFree(unique_port_points);
1609 
1610  /* Port hashes are no longer needed */
1612 
1613  SCLogDebug("rules analyzed");
1614 
1615  /* step 3: group the list and shrink it if necessary */
1616  DetectPort *newlist = NULL;
1617  uint16_t groupmax = (direction == SIG_FLAG_TOCLIENT) ? de_ctx->max_uniq_toclient_groups :
1619  CreateGroupedPortList(de_ctx, list, &newlist, groupmax, SortCompare);
1620  list = newlist;
1621 
1622  /* step 4: deduplicate the SGH's */
1625 
1626  uint32_t cnt = 0;
1627  uint32_t own = 0;
1628  uint32_t ref = 0;
1629  DetectPort *iter;
1630  for (iter = list ; iter != NULL; iter = iter->next) {
1631  BUG_ON (iter->sh == NULL);
1632  DEBUG_VALIDATE_BUG_ON(own + ref != cnt);
1633  cnt++;
1634 
1635  SigGroupHead *lookup_sgh = SigGroupHeadHashLookup(de_ctx, iter->sh);
1636  if (lookup_sgh == NULL) {
1637  SCLogDebug("port group %p sgh %p is the original", iter, iter->sh);
1638 
1639  SigGroupHeadSetSigCnt(iter->sh, 0);
1641  SigGroupHeadSetProtoAndDirection(iter->sh, ipproto, direction);
1642  SigGroupHeadHashAdd(de_ctx, iter->sh);
1643  SigGroupHeadStore(de_ctx, iter->sh);
1644  iter->flags |= PORT_SIGGROUPHEAD_COPY;
1645  own++;
1646  } else {
1647  SCLogDebug("port group %p sgh %p is a copy", iter, iter->sh);
1648 
1649  SigGroupHeadFree(de_ctx, iter->sh);
1650  iter->sh = lookup_sgh;
1651  iter->flags |= PORT_SIGGROUPHEAD_COPY;
1652  ref++;
1653  }
1654  }
1655 #if 0
1656  for (iter = list ; iter != NULL; iter = iter->next) {
1657  SCLogInfo("PORT %u-%u %p (sgh=%s, prioritized=%s/%d)",
1658  iter->port, iter->port2, iter->sh,
1659  iter->flags & PORT_SIGGROUPHEAD_COPY ? "ref" : "own",
1660  iter->sh->init->score ? "true" : "false",
1661  iter->sh->init->score);
1662  }
1663 #endif
1664  SCLogPerf("%s %s: %u port groups, %u unique SGH's, %u copies",
1665  ipproto == 6 ? "TCP" : "UDP",
1666  direction == SIG_FLAG_TOSERVER ? "toserver" : "toclient",
1667  cnt, own, ref);
1669  return list;
1670 
1671 error:
1672  if (unique_port_points != NULL)
1673  SCFree(unique_port_points);
1674  if (it != NULL)
1676 
1677  return NULL;
1678 }
1679 
1681 {
1682  BUG_ON(s->type != SIG_TYPE_NOT_SET);
1683  int iponly = 0;
1684 
1686  s->type = SIG_TYPE_APP_TX;
1687  SCLogDebug("%u: set to app_tx due to hook type app", s->id);
1688  SCReturn;
1689  }
1690 
1692  s->type = SIG_TYPE_PKT;
1693 
1694  /* see if the sig is dp only */
1695  } else if (SignatureIsPDOnly(de_ctx, s) == 1) {
1696  s->type = SIG_TYPE_PDONLY;
1697 
1698  /* see if the sig is ip only */
1699  } else if ((iponly = SignatureIsIPOnly(de_ctx, s)) > 0) {
1700  if (iponly == 1) {
1701  s->type = SIG_TYPE_IPONLY;
1702  } else if (iponly == 2) {
1704  }
1705  } else if (SignatureIsDEOnly(de_ctx, s) == 1) {
1706  s->type = SIG_TYPE_DEONLY;
1707 
1708  } else {
1709  const bool has_match = s->init_data->smlists[DETECT_SM_LIST_MATCH] != NULL;
1710  const bool has_pmatch = s->init_data->smlists[DETECT_SM_LIST_PMATCH] != NULL;
1711  bool has_buffer_frame_engine = false;
1712  bool has_buffer_packet_engine = false;
1713  bool has_buffer_app_engine = false;
1714 
1715  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1716  const uint32_t id = s->init_data->buffers[x].id;
1717 
1719  has_buffer_packet_engine = true;
1721  has_buffer_frame_engine = true;
1722  } else {
1723  has_buffer_app_engine = true;
1724  }
1725  }
1726 
1727  if (has_buffer_packet_engine) {
1728  s->type = SIG_TYPE_PKT;
1729  } else if (has_buffer_frame_engine || has_buffer_app_engine) {
1730  s->type = SIG_TYPE_APP_TX;
1731  } else if (has_pmatch) {
1734  s->type = SIG_TYPE_PKT;
1735  } else if ((s->flags & (SIG_FLAG_REQUIRE_PACKET | SIG_FLAG_REQUIRE_STREAM)) ==
1737  s->type = SIG_TYPE_STREAM;
1738  } else {
1740  }
1741  } else if (has_match) {
1742  s->type = SIG_TYPE_PKT;
1743 
1744  /* app-layer but no inspect engines */
1745  } else if (s->flags & SIG_FLAG_APPLAYER) {
1746  s->type = SIG_TYPE_APPLAYER;
1747  } else {
1748  s->type = SIG_TYPE_PKT;
1749  }
1750  }
1751 }
1752 
1753 /**
1754  * \brief Preprocess signature, classify ip-only, etc, build sig array
1755  *
1756  * \param de_ctx Pointer to the Detection Engine Context
1757  *
1758  * \retval 0 on success
1759  * \retval -1 on failure
1760  */
1762 {
1763  uint32_t cnt_iponly = 0;
1764  uint32_t cnt_payload = 0;
1765  uint32_t cnt_packet = 0;
1766  uint32_t cnt_packet_stream = 0;
1767  uint32_t cnt_applayer = 0;
1768  uint32_t cnt_deonly = 0;
1769 
1770  if (!(de_ctx->flags & DE_QUIET)) {
1771  SCLogDebug("building signature grouping structure, stage 1: "
1772  "preprocessing rules...");
1773  }
1774 
1777  if (de_ctx->sig_array == NULL)
1778  goto error;
1779 
1780  /* now for every rule add the source group */
1781  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
1782  de_ctx->sig_array[s->iid] = s;
1783 
1784  SCLogDebug("Signature %" PRIu32 ", internal id %" PRIu32 ", ptrs %p %p ", s->id, s->iid, s,
1785  de_ctx->sig_array[s->iid]);
1786 
1787  if (s->type == SIG_TYPE_PDONLY) {
1788  SCLogDebug("Signature %"PRIu32" is considered \"PD only\"", s->id);
1789  } else if (s->type == SIG_TYPE_IPONLY) {
1790  SCLogDebug("Signature %"PRIu32" is considered \"IP only\"", s->id);
1791  cnt_iponly++;
1792  } else if (SignatureIsInspectingPayload(de_ctx, s) == 1) {
1793  SCLogDebug("Signature %"PRIu32" is considered \"Payload inspecting\"", s->id);
1794  cnt_payload++;
1795  } else if (s->type == SIG_TYPE_PKT) {
1796  SCLogDebug("Signature %" PRIu32 " is considered \"Packet inspecting\"", s->id);
1797  cnt_packet++;
1798  } else if (s->type == SIG_TYPE_PKT_STREAM) {
1799  SCLogDebug("Signature %" PRIu32 " is considered \"Packet-stream inspecting\"", s->id);
1800  cnt_packet_stream++;
1801  } else if (s->type == SIG_TYPE_DEONLY) {
1802  SCLogDebug("Signature %"PRIu32" is considered \"Decoder Event only\"", s->id);
1803  cnt_deonly++;
1804  } else if (s->flags & SIG_FLAG_APPLAYER) {
1805  SCLogDebug("Signature %"PRIu32" is considered \"Applayer inspecting\"", s->id);
1806  cnt_applayer++;
1807  }
1808 
1809 #ifdef DEBUG
1810  if (SCLogDebugEnabled()) {
1811  uint16_t colen = 0;
1812  char copresent = 0;
1813  SigMatch *sm;
1814  DetectContentData *co;
1815  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH]; sm != NULL; sm = sm->next) {
1816  if (sm->type != DETECT_CONTENT)
1817  continue;
1818 
1819  copresent = 1;
1820  co = (DetectContentData *)sm->ctx;
1821  if (co->content_len > colen)
1822  colen = co->content_len;
1823  }
1824 
1825  if (copresent && colen == 1) {
1826  SCLogDebug("signature %8u content maxlen 1", s->id);
1827  for (int proto = 0; proto < 256; proto++) {
1828  if (s->init_data->proto.proto[(proto / 8)] & (1 << (proto % 8)))
1829  SCLogDebug("=> proto %" PRId32 "", proto);
1830  }
1831  }
1832  }
1833 #endif /* DEBUG */
1834 
1835  if (RuleMpmIsNegated(s)) {
1836  s->flags |= SIG_FLAG_MPM_NEG;
1837  }
1838 
1839  SignatureCreateMask(s);
1842 
1843  RuleSetScore(s);
1844 
1845  /* run buffer type callbacks if any */
1846  for (int x = 0; x < DETECT_SM_LIST_MAX; x++) {
1847  if (s->init_data->smlists[x])
1849  }
1850  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
1852  }
1853 
1854  de_ctx->sig_cnt++;
1855  }
1856 
1857  if (!(de_ctx->flags & DE_QUIET)) {
1858  if (strlen(de_ctx->config_prefix) > 0)
1859  SCLogInfo("tenant id %d: %" PRIu32 " signatures processed. %" PRIu32 " are IP-only "
1860  "rules, %" PRIu32 " are inspecting packet payload, %" PRIu32
1861  " inspect application layer, %" PRIu32 " are decoder event only, %" PRIu32
1862  " are packet inspecting,"
1863  " %" PRIu32 " are packet-stream inspecting",
1864  de_ctx->tenant_id, de_ctx->sig_cnt, cnt_iponly, cnt_payload, cnt_applayer,
1865  cnt_deonly, cnt_packet, cnt_packet_stream);
1866  else
1867  SCLogInfo("%" PRIu32 " signatures processed. %" PRIu32 " are IP-only "
1868  "rules, %" PRIu32 " are inspecting packet payload, %" PRIu32
1869  " inspect application layer, %" PRIu32 " are decoder event only %" PRIu32
1870  " are packet inspecting,"
1871  " %" PRIu32 " are packet-stream inspecting",
1872  de_ctx->sig_cnt, cnt_iponly, cnt_payload, cnt_applayer, cnt_deonly, cnt_packet,
1873  cnt_packet_stream);
1874 
1875  SCLogConfig("building signature grouping structure, stage 1: "
1876  "preprocessing rules... complete");
1877  }
1878 
1879  if (DetectFlowbitsAnalyze(de_ctx) != 0)
1880  goto error;
1881 
1882  return 0;
1883 
1884 error:
1885  return -1;
1886 }
1887 
1888 /**
1889  * \internal
1890  * \brief add a decoder event signature to the detection engine ctx
1891  */
1892 static void DetectEngineAddDecoderEventSig(DetectEngineCtx *de_ctx, Signature *s)
1893 {
1894  SCLogDebug("adding signature %"PRIu32" to the decoder event sgh", s->id);
1896 }
1897 
1898 static void DetectEngineAddEthernetSig(DetectEngineCtx *de_ctx, Signature *s)
1899 {
1900  SCLogNotice("adding signature %" PRIu32 " to the eth non ip sgh", s->id);
1902 }
1903 
1904 static void DetectEngineAddSigToPreStreamHook(DetectEngineCtx *de_ctx, Signature *s)
1905 {
1906  SCLogDebug("adding signature %" PRIu32 " to the pre_stream hook sgh", s->id);
1907 
1908  if ((s->flags & (SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT)) ==
1912  } else if (s->flags & SIG_FLAG_TOSERVER) {
1914  } else if (s->flags & SIG_FLAG_TOCLIENT) {
1916  }
1917 }
1918 
1919 static void DetectEngineAddSigToPreFlowHook(DetectEngineCtx *de_ctx, Signature *s)
1920 {
1921  SCLogDebug("adding signature %" PRIu32 " to the pre_flow hook sgh", s->id);
1923 }
1924 
1925 /**
1926  * \brief Fill the global src group head, with the sigs included
1927  *
1928  * \param de_ctx Pointer to the Detection Engine Context whose Signatures have
1929  * to be processed
1930  *
1931  * \retval 0 On success
1932  * \retval -1 On failure
1933  */
1935 {
1936  SCLogDebug("building signature grouping structure, stage 2: "
1937  "building source address lists...");
1938 
1940 
1941  de_ctx->flow_gh[1].tcp = RulesGroupByPorts(de_ctx, IPPROTO_TCP, SIG_FLAG_TOSERVER);
1942  de_ctx->flow_gh[0].tcp = RulesGroupByPorts(de_ctx, IPPROTO_TCP, SIG_FLAG_TOCLIENT);
1943  de_ctx->flow_gh[1].udp = RulesGroupByPorts(de_ctx, IPPROTO_UDP, SIG_FLAG_TOSERVER);
1944  de_ctx->flow_gh[0].udp = RulesGroupByPorts(de_ctx, IPPROTO_UDP, SIG_FLAG_TOCLIENT);
1945 
1946  /* Setup the other IP Protocols (so not TCP/UDP) */
1947  RulesGroupByIPProto(de_ctx);
1948 
1949  /* now for every rule add the source group to our temp lists */
1950  for (Signature *s = de_ctx->sig_list; s != NULL; s = s->next) {
1951  SCLogDebug("s->id %"PRIu32, s->id);
1952  if (s->type == SIG_TYPE_IPONLY) {
1954  } else if (s->type == SIG_TYPE_DEONLY) {
1955  DetectEngineAddDecoderEventSig(de_ctx, s);
1956  } else if (s->type == SIG_TYPE_PKT && s->init_data->hook.type == SIGNATURE_HOOK_TYPE_PKT &&
1958  DetectEngineAddSigToPreStreamHook(de_ctx, s);
1959  } else if (s->type == SIG_TYPE_PKT && s->init_data->hook.type == SIGNATURE_HOOK_TYPE_PKT &&
1961  DetectEngineAddSigToPreFlowHook(de_ctx, s);
1962  }
1963 
1964  /* add ethernet sigs and decoder events to the ethernet sgh */
1965  if ((s->type == SIG_TYPE_PKT && SigIsEthernetAddToNonIP(s)) || s->type == SIG_TYPE_DEONLY ||
1967  // ethernet
1968  SCLogNotice("rule: %u: add to non-IP", s->id);
1969  DetectEngineAddEthernetSig(de_ctx, s);
1970  }
1971  }
1972 
1975  return 0;
1976 }
1977 
1978 static void DetectEngineBuildDecoderEventSgh(DetectEngineCtx *de_ctx)
1979 {
1980  if (de_ctx->decoder_event_sgh == NULL)
1981  return;
1982 
1983  uint32_t max_idx = DetectEngineGetMaxSigId(de_ctx);
1986 }
1987 
1988 static void DetectEngineBuildPreStreamHookSghs(DetectEngineCtx *de_ctx)
1989 {
1990  uint32_t max_idx = DetectEngineGetMaxSigId(de_ctx);
1991  if (de_ctx->pre_stream_sgh[0] != NULL) {
1995  }
1996  if (de_ctx->pre_stream_sgh[1] != NULL) {
2000  }
2001 
2002  if (de_ctx->pre_stream_sgh[0] != NULL || de_ctx->pre_stream_sgh[1] != NULL) {
2004  }
2005 }
2006 
2007 static void DetectEngineBuildPreFlowHookSghs(DetectEngineCtx *de_ctx)
2008 {
2009  if (de_ctx->pre_flow_sgh != NULL) {
2010  uint32_t max_idx = DetectEngineGetMaxSigId(de_ctx);
2015  }
2016 }
2017 
2018 static void DetectEngineBuildEthernetNonIPSgh(DetectEngineCtx *de_ctx)
2019 {
2020  if (de_ctx->eth_non_ip_sgh != NULL) {
2021  const uint32_t max_idx = DetectEngineGetMaxSigId(de_ctx);
2025  }
2026 }
2027 
2029 {
2030  /* prepare the decoder event sgh */
2031  DetectEngineBuildDecoderEventSgh(de_ctx);
2032 
2033  /* pre_flow hook sgh */
2034  DetectEngineBuildPreFlowHookSghs(de_ctx);
2035 
2036  /* pre_stream hook sghs */
2037  DetectEngineBuildPreStreamHookSghs(de_ctx);
2038 
2039  /* Ethernet Non IP */
2040  DetectEngineBuildEthernetNonIPSgh(de_ctx);
2041 
2042  return 0;
2043 }
2044 
2046 {
2047  BUG_ON(de_ctx == NULL);
2048 
2049  SCLogDebug("cleaning up signature grouping structure...");
2050 
2051  if (de_ctx->eth_non_ip_sgh)
2055  de_ctx->decoder_event_sgh = NULL;
2056  if (de_ctx->pre_flow_sgh)
2058  de_ctx->pre_flow_sgh = NULL;
2059  if (de_ctx->pre_stream_sgh[0])
2061  de_ctx->pre_stream_sgh[0] = NULL;
2062  if (de_ctx->pre_stream_sgh[1])
2064  de_ctx->pre_stream_sgh[1] = NULL;
2065 
2066  for (int f = 0; f < FLOW_STATES; f++) {
2067  for (int p = 0; p < 256; p++) {
2068  de_ctx->flow_gh[f].sgh[p] = NULL;
2069  }
2070 
2071  /* free lookup lists */
2073  de_ctx->flow_gh[f].tcp = NULL;
2075  de_ctx->flow_gh[f].udp = NULL;
2076  }
2077 
2078  for (uint32_t idx = 0; idx < de_ctx->sgh_array_cnt; idx++) {
2079  SigGroupHead *sgh = de_ctx->sgh_array[idx];
2080  if (sgh == NULL)
2081  continue;
2082 
2083  SCLogDebug("sgh %p", sgh);
2084  SigGroupHeadFree(de_ctx, sgh);
2085  }
2087  de_ctx->sgh_array = NULL;
2088  de_ctx->sgh_array_cnt = 0;
2089  de_ctx->sgh_array_size = 0;
2090 
2092 
2093  SCLogDebug("cleaning up signature grouping structure... complete");
2094  return 0;
2095 }
2096 
2097 #if 0
2098 static void DbgPrintSigs(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
2099 {
2100  if (sgh == NULL) {
2101  printf("\n");
2102  return;
2103  }
2104 
2105  uint32_t sig;
2106  for (sig = 0; sig < sgh->sig_cnt; sig++) {
2107  printf("%" PRIu32 " ", sgh->match_array[sig]->id);
2108  }
2109  printf("\n");
2110 }
2111 
2112 static void DbgPrintSigs2(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
2113 {
2114  if (sgh == NULL || sgh->init == NULL) {
2115  printf("\n");
2116  return;
2117  }
2118 
2119  uint32_t sig;
2120  for (sig = 0; sig < DetectEngineGetMaxSigId(de_ctx); sig++) {
2121  if (sgh->init->sig_array[(sig/8)] & (1<<(sig%8))) {
2122  printf("%" PRIu32 " ", de_ctx->sig_array[sig]->id);
2123  }
2124  }
2125  printf("\n");
2126 }
2127 #endif
2128 
2129 /** \brief finalize preparing sgh's */
2131 {
2132  SCEnter();
2133 
2134  //SCLogInfo("sgh's %"PRIu32, de_ctx->sgh_array_cnt);
2135 
2136  uint32_t cnt = 0;
2137  for (uint32_t idx = 0; idx < de_ctx->sgh_array_cnt; idx++) {
2138  SigGroupHead *sgh = de_ctx->sgh_array[idx];
2139  if (sgh == NULL)
2140  continue;
2141 
2142  SCLogDebug("sgh %p", sgh);
2143 
2145  SCLogDebug("filestore count %u", sgh->filestore_cnt);
2146 
2148 
2149  sgh->id = idx;
2150  cnt++;
2151  }
2152  SCLogPerf("Unique rule groups: %u", cnt);
2153 
2155 
2156  if (de_ctx->decoder_event_sgh != NULL) {
2157  /* no need to set filestore count here as that would make a
2158  * signature not decode event only. */
2160  }
2161 
2162  int dump_grouping = 0;
2163  (void)SCConfGetBool("detect.profiling.grouping.dump-to-disk", &dump_grouping);
2164 
2165  if (dump_grouping) {
2166  int add_rules = 0;
2167  (void)SCConfGetBool("detect.profiling.grouping.include-rules", &add_rules);
2168  int add_mpm_stats = 0;
2169  (void)SCConfGetBool("detect.profiling.grouping.include-mpm-stats", &add_mpm_stats);
2170 
2171  RulesDumpGrouping(de_ctx, add_rules, add_mpm_stats);
2172  }
2173 
2174  for (uint32_t idx = 0; idx < de_ctx->sgh_array_cnt; idx++) {
2175  SigGroupHead *sgh = de_ctx->sgh_array[idx];
2176  if (sgh == NULL)
2177  continue;
2179  sgh->init = NULL;
2180  }
2181  /* cleanup the hashes now since we won't need them
2182  * after the initialization phase. */
2184 
2185 #ifdef PROFILING
2187 #endif
2188  SCReturnInt(0);
2189 }
2190 
2191 extern bool rule_engine_analysis_set;
2192 /** \internal
2193  * \brief perform final per signature setup tasks
2194  *
2195  * - Create SigMatchData arrays from the init only SigMatch lists
2196  * - Setup per signature inspect engines
2197  * - remove signature init data.
2198  */
2199 static int SigMatchPrepare(DetectEngineCtx *de_ctx)
2200 {
2201  SCEnter();
2202 
2203  Signature *s = de_ctx->sig_list;
2204  for (; s != NULL; s = s->next) {
2205  /* set up inspect engines */
2207 
2208  /* built-ins */
2209  for (int type = 0; type < DETECT_SM_LIST_MAX; type++) {
2210  /* skip PMATCH if it is used in a stream 'app engine' instead */
2212  continue;
2213  SigMatch *sm = s->init_data->smlists[type];
2215  }
2216  /* set up the pkt inspection engines */
2218 
2222  }
2223  /* free lists. Ctx' are xferred to sm_arrays so won't get freed */
2224  for (uint32_t i = 0; i < DETECT_SM_LIST_MAX; i++) {
2225  SigMatch *sm = s->init_data->smlists[i];
2226  while (sm != NULL) {
2227  SigMatch *nsm = sm->next;
2228  SigMatchFree(de_ctx, sm);
2229  sm = nsm;
2230  }
2231  }
2232  for (uint32_t i = 0; i < (uint32_t)s->init_data->transforms.cnt; i++) {
2233  if (s->init_data->transforms.transforms[i].options) {
2234  int transform = s->init_data->transforms.transforms[i].transform;
2235  sigmatch_table[transform].Free(
2237  s->init_data->transforms.transforms[i].options = NULL;
2238  }
2239  }
2240  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
2241  SigMatch *sm = s->init_data->buffers[x].head;
2242  while (sm != NULL) {
2243  SigMatch *nsm = sm->next;
2244  SigMatchFree(de_ctx, sm);
2245  sm = nsm;
2246  }
2247  }
2248  if (s->init_data->cidr_dst != NULL)
2250 
2251  if (s->init_data->cidr_src != NULL)
2253 
2254  SCFree(s->init_data->buffers);
2257  SCFree(s->init_data);
2258  s->init_data = NULL;
2259  }
2260 
2262  SCReturnInt(0);
2263 }
2264 
2265 /**
2266  * \brief Convert the signature list into the runtime match structure.
2267  *
2268  * \param de_ctx Pointer to the Detection Engine Context whose Signatures have
2269  * to be processed
2270  *
2271  * \retval 0 On Success.
2272  * \retval -1 On failure.
2273  */
2275 {
2276  Signature *s = de_ctx->sig_list;
2277 
2278  /* Assign the unique order id of signatures after sorting,
2279  * so the IP Only engine process them in order too. Also
2280  * reset the old signums and assign new signums. We would
2281  * have experienced Sig reordering by now, hence the new
2282  * signums. */
2283  de_ctx->signum = 0;
2284  while (s != NULL) {
2285  s->iid = de_ctx->signum++;
2286 
2287  s = s->next;
2288  }
2289 
2291  return -1;
2292 
2293  SigInitStandardMpmFactoryContexts(de_ctx);
2294 
2295  if (SigPrepareStage1(de_ctx) != 0) {
2296  FatalError("initializing the detection engine failed");
2297  }
2298 
2299  if (SigPrepareStage2(de_ctx) != 0) {
2300  FatalError("initializing the detection engine failed");
2301  }
2302 
2303  if (SigPrepareStage3(de_ctx) != 0) {
2304  FatalError("initializing the detection engine failed");
2305  }
2306  if (SigPrepareStage4(de_ctx) != 0) {
2307  FatalError("initializing the detection engine failed");
2308  }
2309 
2314  if (r != 0) {
2315  FatalError("initializing the detection engine failed");
2316  }
2317 
2318  if (SigMatchPrepare(de_ctx) != 0) {
2319  FatalError("initializing the detection engine failed");
2320  }
2321 
2322 #ifdef PROFILING
2325  de_ctx->profile_match_logging_threshold = UINT_MAX; // disabled
2326 
2327  intmax_t v = 0;
2328  if (SCConfGetInt("detect.profiling.inspect-logging-threshold", &v) == 1)
2329  de_ctx->profile_match_logging_threshold = (uint32_t)v;
2330 #endif
2331 #ifdef PROFILE_RULES
2332  SCProfilingRuleInitCounters(de_ctx);
2333 #endif
2334 
2337  }
2338 
2339  if (EngineModeIsFirewall()) {
2341  }
2342  return 0;
2343 }
2344 
2346 {
2348 
2349  return 0;
2350 }
detect-tcp-flags.h
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:56
SIG_TYPE_STREAM
@ SIG_TYPE_STREAM
Definition: detect.h:74
DETECT_FLOW_FLAG_TOCLIENT
#define DETECT_FLOW_FLAG_TOCLIENT
Definition: detect-flow.h:28
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:535
detect-content.h
DETECT_APP_LAYER_EVENT
@ DETECT_APP_LAYER_EVENT
Definition: detect-engine-register.h:199
util-port-interval-tree.h
detect-engine.h
DetectEngineResetMaxSigId
void DetectEngineResetMaxSigId(DetectEngineCtx *de_ctx)
Definition: detect-engine.c:3064
DetectEngineBufferTypeSupportsPacketGetById
bool DetectEngineBufferTypeSupportsPacketGetById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1436
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:119
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:316
MASK_TCP_UNUSUAL_FLAGS
#define MASK_TCP_UNUSUAL_FLAGS
Definition: detect-engine-build.c:396
DetectFlagsSignatureNeedsSynOnlyPackets
int DetectFlagsSignatureNeedsSynOnlyPackets(const Signature *s)
Definition: detect-tcp-flags.c:170
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:646
detect-engine-proto.h
detect-dsize.h
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1293
UniquePortPoint_
Definition: detect-engine-build.c:1317
SIG_TYPE_APP_TX
@ SIG_TYPE_APP_TX
Definition: detect.h:77
SigMatchFree
void SigMatchFree(DetectEngineCtx *de_ctx, SigMatch *sm)
free a SigMatch
Definition: detect-parse.c:288
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1455
DetectEngineCtx_::decoder_event_sgh
struct SigGroupHead_ * decoder_event_sgh
Definition: detect.h:1022
DetectPortHashInit
int DetectPortHashInit(DetectEngineCtx *de_ctx)
Initializes the hash table in the detection engine context to hold the DetectPort hash.
Definition: detect-engine-port.c:1319
DetectEngineCtx_::flow_gh
DetectEngineLookupFlow flow_gh[FLOW_STATES]
Definition: detect.h:964
FILE_SIG_NEED_SHA1
#define FILE_SIG_NEED_SHA1
Definition: detect.h:325
SigPrepareStage4
int SigPrepareStage4(DetectEngineCtx *de_ctx)
finalize preparing sgh's
Definition: detect-engine-build.c:2130
detect-engine-siggroup.h
DetectFlowData_
Definition: detect-flow.h:37
SigTableElmt_::name
const char * name
Definition: detect.h:1468
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1346
DetectEngineBufferRunSetupCallback
void DetectEngineBufferRunSetupCallback(const DetectEngineCtx *de_ctx, const int id, Signature *s)
Definition: detect-engine.c:1473
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1638
DetectListToHumanString
const char * DetectListToHumanString(int list)
Definition: detect-parse.c:112
SIG_FLAG_INIT_FLOW
#define SIG_FLAG_INIT_FLOW
Definition: detect.h:291
SigFree
void SigFree(DetectEngineCtx *, Signature *)
Definition: detect-parse.c:2059
DumpPatterns
void DumpPatterns(DetectEngineCtx *de_ctx)
Definition: detect-engine-analyzer.c:1557
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SigGroupHeadInitData_::sig_array
uint8_t * sig_array
Definition: detect.h:1612
DetectPortFree
void DetectPortFree(const DetectEngineCtx *de_ctx, DetectPort *dp)
Free a DetectPort and its members.
Definition: detect-engine-port.c:80
DetectEngineCtx_::PreFlowHook
DetectPacketHookFunc PreFlowHook
Definition: detect.h:1166
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:71
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1459
DetectEngineCtx_::max_uniq_toclient_groups
uint16_t max_uniq_toclient_groups
Definition: detect.h:995
MpmStoreReportStats
void MpmStoreReportStats(const DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:1523
SignatureInitData_::prefilter_sm
SigMatch * prefilter_sm
Definition: detect.h:626
SignatureInitData_::src_contains_negation
bool src_contains_negation
Definition: detect.h:602
DETECT_FLOW
@ DETECT_FLOW
Definition: detect-engine-register.h:56
Signature_::alproto
AppProto alproto
Definition: detect.h:677
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DETECT_UINT_NE
#define DETECT_UINT_NE
Definition: detect-engine-uint.h:36
IPOnlyDeinit
void IPOnlyDeinit(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx)
Deinitialize the IP Only detection engine context.
Definition: detect-engine-iponly.c:954
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
DetectPort_::port
uint16_t port
Definition: detect.h:220
name
const char * name
Definition: detect-engine-proto.c:48
SCPortIntervalFindOverlappingRanges
void SCPortIntervalFindOverlappingRanges(DetectEngineCtx *de_ctx, const uint16_t port, const uint16_t port2, const struct PI *head, DetectPort **list)
Callee function to find all overlapping port ranges as asked by the detection engine during Stage 2 o...
Definition: util-port-interval-tree.c:315
PORT_SIGGROUPHEAD_COPY
#define PORT_SIGGROUPHEAD_COPY
Definition: detect.h:216
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:86
DETECT_DECODE_EVENT
@ DETECT_DECODE_EVENT
Definition: detect-engine-register.h:125
DETECT_SM_LIST_DYNAMIC_START
@ DETECT_SM_LIST_DYNAMIC_START
Definition: detect.h:138
SignatureHook_::t
union SignatureHook_::@87 t
Packet_::flags
uint32_t flags
Definition: decode.h:551
DetectEngineCtx_::PreStreamHook
DetectPacketHookFunc PreStreamHook
Definition: detect.h:1161
UNDEFINED_PORT
#define UNDEFINED_PORT
Definition: detect-engine-build.c:1313
PatternStrength
uint32_t PatternStrength(uint8_t *pat, uint16_t patlen)
Predict a strength value for patterns.
Definition: detect-engine-mpm.c:987
DetectFlowbitsAnalyze
int DetectFlowbitsAnalyze(DetectEngineCtx *de_ctx)
Definition: detect-flowbits.c:673
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
SCProfilingSghInitCounters
void SCProfilingSghInitCounters(DetectEngineCtx *de_ctx)
Register the keyword profiling counters.
Definition: util-profiling-rulegroups.c:348
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:937
DETECT_PROTO_ANY
#define DETECT_PROTO_ANY
Definition: detect-engine-proto.h:28
SIG_TYPE_PKT_STREAM
@ SIG_TYPE_PKT_STREAM
Definition: detect.h:73
DetectEngineCtx_::pre_stream_sgh
struct SigGroupHead_ * pre_stream_sgh[2]
Definition: detect.h:1163
DETECT_PROTO_ETHERNET
#define DETECT_PROTO_ETHERNET
Definition: detect-engine-proto.h:33
DetectFlagsSignatureNeedsSynPackets
int DetectFlagsSignatureNeedsSynPackets(const Signature *s)
Definition: detect-tcp-flags.c:151
TransformData_::options
void * options
Definition: detect.h:388
DetectFlowbitsData_::cmd
uint8_t cmd
Definition: detect-flowbits.h:37
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:287
SigGroupHeadSetProtoAndDirection
void SigGroupHeadSetProtoAndDirection(SigGroupHead *sgh, uint8_t ipproto, int dir)
Definition: detect-engine-siggroup.c:483
DETECT_UINT_EQ
#define DETECT_UINT_EQ
Definition: detect-engine-uint.h:35
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
SIG_FLAG_DST_ANY
#define SIG_FLAG_DST_ANY
Definition: detect.h:241
util-var-name.h
SIG_FLAG_REQUIRE_STREAM
#define SIG_FLAG_REQUIRE_STREAM
Definition: detect.h:254
rule_engine_analysis_set
bool rule_engine_analysis_set
Definition: detect-engine-loader.c:56
SCConfGetBool
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition: conf.c:498
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
DetectPort_::next
struct DetectPort_ * next
Definition: detect.h:233
DetectEngineCtx_::tcp_priorityports
DetectPort * tcp_priorityports
Definition: detect.h:1076
DetectEngineCtx_::dport_hash_table
HashListTable * dport_hash_table
Definition: detect.h:1074
Signature_::sm_arrays
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:735
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:609
proto
uint8_t proto
Definition: decode-template.h:0
DetectPort_::sh
struct SigGroupHead_ * sh
Definition: detect.h:230
DetectEngineCtx_::udp_priorityports
DetectPort * udp_priorityports
Definition: detect.h:1077
DetectContentData_
Definition: detect-content.h:93
DETECT_FLOWBITS_CMD_ISSET
#define DETECT_FLOWBITS_CMD_ISSET
Definition: detect-flowbits.h:32
PKT_NOPAYLOAD_INSPECTION
#define PKT_NOPAYLOAD_INSPECTION
Definition: decode.h:1279
SigCleanSignatures
void SigCleanSignatures(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:56
DetectPortPrintList
void DetectPortPrintList(DetectPort *head)
Helper function used to print the list of ports present in this DetectPort list.
Definition: detect-engine-port.c:100
DETECT_UINT_GT
#define DETECT_UINT_GT
Definition: detect-engine-uint.h:32
SigPrepareStage1
int SigPrepareStage1(DetectEngineCtx *de_ctx)
Preprocess signature, classify ip-only, etc, build sig array.
Definition: detect-engine-build.c:1761
DETECT_PGSCORE_RULE_SYN_ONLY
#define DETECT_PGSCORE_RULE_SYN_ONLY
Definition: detect-engine-build.c:54
EngineModeIsFirewall
bool EngineModeIsFirewall(void)
Definition: suricata.c:239
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
MAX
#define MAX(x, y)
Definition: suricata-common.h:412
SIG_FLAG_SRC_ANY
#define SIG_FLAG_SRC_ANY
Definition: detect.h:240
SigGroupHeadSetupFiles
void SigGroupHeadSetupFiles(const DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Set the need hash flag in the sgh.
Definition: detect-engine-siggroup.c:570
SIG_TYPE_APPLAYER
@ SIG_TYPE_APPLAYER
Definition: detect.h:76
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:610
DetectPort_::port2
uint16_t port2
Definition: detect.h:221
detect-engine-prefilter.h
Packet_::app_layer_events
AppLayerDecoderEvents * app_layer_events
Definition: decode.h:632
Packet_::events
PacketEngineEvents events
Definition: decode.h:630
FLOW_STATES
#define FLOW_STATES
Definition: detect.h:917
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
DetectPort_::flags
uint8_t flags
Definition: detect.h:223
DetectPortPrint
void DetectPortPrint(DetectPort *dp)
Helper function that print the DetectPort info.
Definition: detect-engine-port.c:568
FirewallAnalyzer
int FirewallAnalyzer(const DetectEngineCtx *de_ctx)
Definition: detect-engine-analyzer.c:2140
PacketCreateMask
void PacketCreateMask(Packet *p, SignatureMask *mask, AppProto alproto, bool app_decoder_events)
Definition: detect-engine-build.c:401
SIG_FLAG_APPLAYER
#define SIG_FLAG_APPLAYER
Definition: detect.h:248
SigGroupHeadHashInit
int SigGroupHeadHashInit(DetectEngineCtx *de_ctx)
Initializes the hash table in the detection engine context to hold the SigGroupHeads.
Definition: detect-engine-siggroup.c:244
FILE_SIG_NEED_MD5
#define FILE_SIG_NEED_MD5
Definition: detect.h:324
DetectEngineCtx_::sgh_array_size
uint32_t sgh_array_size
Definition: detect.h:1007
SIGNATURE_HOOK_TYPE_APP
@ SIGNATURE_HOOK_TYPE_APP
Definition: detect.h:550
SignatureIsFileSha256Inspecting
int SignatureIsFileSha256Inspecting(const Signature *s)
Check if a signature contains the filesha256 keyword.
Definition: detect-engine-build.c:171
Signature_::next
struct Signature_ * next
Definition: detect.h:754
DetectFlowbitsData_
Definition: detect-flowbits.h:35
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:55
DETECT_APP_LAYER_PROTOCOL
@ DETECT_APP_LAYER_PROTOCOL
Definition: detect-engine-register.h:35
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:127
SigGroupHeadInitData_::score
int score
Definition: detect.h:1617
DetectPortCopySingle
DetectPort * DetectPortCopySingle(DetectEngineCtx *de_ctx, DetectPort *src)
Function that return a copy of DetectPort src sigs.
Definition: detect-engine-port.c:528
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
DetectPortParse
int DetectPortParse(const DetectEngineCtx *de_ctx, DetectPort **head, const char *str)
Function for parsing port strings.
Definition: detect-engine-port.c:1134
SigAddressCleanupStage1
int SigAddressCleanupStage1(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2045
SCPortIntervalInsert
int SCPortIntervalInsert(DetectEngineCtx *de_ctx, SCPortIntervalTree *it, const DetectPort *p)
Function to insert a node in the interval tree.
Definition: util-port-interval-tree.c:109
DetectEngineCtx_::pre_flow_sgh
struct SigGroupHead_ * pre_flow_sgh
Definition: detect.h:1168
SIG_TYPE_PKT
@ SIG_TYPE_PKT
Definition: detect.h:72
TCPHdr_::th_flags
uint8_t th_flags
Definition: decode-tcp.h:155
SigGroupHeadBuildMatchArray
int SigGroupHeadBuildMatchArray(DetectEngineCtx *de_ctx, SigGroupHead *sgh, uint32_t max_idx)
Create an array with all the internal ids of the sigs that this sig group head will check for.
Definition: detect-engine-siggroup.c:532
DETECT_PROTO_ARP
#define DETECT_PROTO_ARP
Definition: detect-engine-proto.h:34
SigGroupHeadInitData_::sig_cnt
SigIntId sig_cnt
Definition: detect.h:1631
DETECT_CONTENT_ENDS_WITH
#define DETECT_CONTENT_ENDS_WITH
Definition: detect-content.h:42
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
g_alproto_max
AppProto g_alproto_max
Definition: app-layer-protos.c:30
DETECT_FLOWINT
@ DETECT_FLOWINT
Definition: detect-engine-register.h:64
IPOnlyInit
void IPOnlyInit(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx)
Setup the IP Only detection engine context.
Definition: detect-engine-iponly.c:915
SIG_MASK_REQUIRE_ENGINE_EVENT
#define SIG_MASK_REQUIRE_ENGINE_EVENT
Definition: detect.h:318
SIG_TYPE_IPONLY
@ SIG_TYPE_IPONLY
Definition: detect.h:66
SignatureInitData_::mpm_sm
SigMatch * mpm_sm
Definition: detect.h:624
SigFindSignatureBySidGid
Signature * SigFindSignatureBySidGid(DetectEngineCtx *de_ctx, uint32_t sid, uint32_t gid)
Find a specific signature by sid and gid.
Definition: detect-engine-build.c:80
DetectConfigData_::scope
enum ConfigScope scope
Definition: detect-config.h:32
DetectEngineGetMaxSigId
#define DetectEngineGetMaxSigId(de_ctx)
Definition: detect-engine.h:93
DETECT_FLOW_FLAG_TOSERVER
#define DETECT_FLOW_FLAG_TOSERVER
Definition: detect-flow.h:27
FILE_SIG_NEED_MAGIC
#define FILE_SIG_NEED_MAGIC
Definition: detect.h:322
SignatureInitData_::mpm_sm_list
int mpm_sm_list
Definition: detect.h:622
SIG_MASK_REQUIRE_FLOW
#define SIG_MASK_REQUIRE_FLOW
Definition: detect.h:312
SCConfGetInt
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:415
SignatureInitData_::cidr_dst
IPOnlyCIDRItem * cidr_dst
Definition: detect.h:619
DETECT_CONTENT_DEPTH
#define DETECT_CONTENT_DEPTH
Definition: detect-content.h:33
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
DetectEngineLookupFlow_::sgh
struct SigGroupHead_ * sgh[256]
Definition: detect.h:869
detect.h
SIG_MASK_REQUIRE_FLAGS_INITDEINIT
#define SIG_MASK_REQUIRE_FLAGS_INITDEINIT
Definition: detect.h:313
SCProfilingPrefilterInitCounters
void SCProfilingPrefilterInitCounters(DetectEngineCtx *de_ctx)
Register the prefilter profiling counters.
Definition: util-profiling-prefilter.c:300
SigMatchList2DataArray
SigMatchData * SigMatchList2DataArray(SigMatch *head)
convert SigMatch list to SigMatchData array
Definition: detect-parse.c:2370
PKT_DETECT_HAS_STREAMDATA
#define PKT_DETECT_HAS_STREAMDATA
Definition: decode.h:1332
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:360
DETECT_CONTENT_NEGATED
#define DETECT_CONTENT_NEGATED
Definition: detect-content.h:40
detect-engine-port.h
SignatureInitData_::proto
DetectProto proto
Definition: detect.h:635
DETECT_ENGINE_EVENT
@ DETECT_ENGINE_EVENT
Definition: detect-engine-register.h:216
DetectU8Data
DetectUintData_u8 DetectU8Data
Definition: detect-engine-uint.h:43
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
DetectContentPropagateLimits
void DetectContentPropagateLimits(Signature *s)
Definition: detect-content.c:715
DetectPort_
Port structure for detection engine.
Definition: detect.h:219
SigGroupHead_::init
SigGroupHeadInitData * init
Definition: detect.h:1655
DetectEngineCtx_::sig_cnt
uint32_t sig_cnt
Definition: detect.h:947
SignatureHook_::pkt
struct SignatureHook_::@87::@89 pkt
DETECT_PGSCORE_RULE_NO_MPM
#define DETECT_PGSCORE_RULE_NO_MPM
Definition: detect-engine-build.c:53
UniquePortPoint_::single
bool single
Definition: detect-engine-build.c:1319
SignatureInitData_::cidr_src
IPOnlyCIDRItem * cidr_src
Definition: detect.h:619
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:359
SignatureInitData_::hook
SignatureHook hook
Definition: detect.h:591
SIGNATURE_HOOK_TYPE_NOT_SET
@ SIGNATURE_HOOK_TYPE_NOT_SET
Definition: detect.h:548
DetectProto_::proto
uint8_t proto[256/8]
Definition: detect-engine-proto.h:39
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
AppLayerDecoderEvents_::cnt
uint8_t cnt
Definition: app-layer-events.h:37
IPOnlyPrepare
void IPOnlyPrepare(DetectEngineCtx *de_ctx)
Build the radix trees from the lists of parsed addresses in CIDR format the result should be 4 radix ...
Definition: detect-engine-iponly.c:1142
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2345
DETECT_PGSCORE_RULE_MPM_FAST_PATTERN
#define DETECT_PGSCORE_RULE_MPM_FAST_PATTERN
Definition: detect-engine-build.c:51
SIG_FLAG_REQUIRE_FLOWVAR
#define SIG_FLAG_REQUIRE_FLOWVAR
Definition: detect.h:266
util-profiling.h
FILE_SIG_NEED_SHA256
#define FILE_SIG_NEED_SHA256
Definition: detect.h:326
DetectEngineLookupFlow_::udp
DetectPort * udp
Definition: detect.h:868
SCReturn
#define SCReturn
Definition: util-debug.h:286
Signature_::flags
uint32_t flags
Definition: detect.h:673
Packet_
Definition: decode.h:505
SIGMATCH_IPONLY_COMPAT
#define SIGMATCH_IPONLY_COMPAT
Definition: detect-engine-register.h:313
detect-engine-build.h
type
uint16_t type
Definition: decode-vlan.c:106
DETECT_UINT_GTE
#define DETECT_UINT_GTE
Definition: detect-engine-uint.h:33
SCConfigGetLogDirectory
const char * SCConfigGetLogDirectory(void)
Definition: util-conf.c:38
IPOnlyPrint
void IPOnlyPrint(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx)
Print stats of the IP Only engine.
Definition: detect-engine-iponly.c:943
DetectConfigData_
Definition: detect-config.h:29
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
DetectEngineCtx_::max_uniq_toserver_groups
uint16_t max_uniq_toserver_groups
Definition: detect.h:996
IPOnlyAddSignature
void IPOnlyAddSignature(DetectEngineCtx *de_ctx, DetectEngineIPOnlyCtx *io_ctx, Signature *s)
Add a signature to the lists of Addresses in CIDR format (sorted) this step is necessary to build the...
Definition: detect-engine-iponly.c:1414
SC_OK
@ SC_OK
Definition: util-error.h:27
DETECT_CONFIG
@ DETECT_CONFIG
Definition: detect-engine-register.h:219
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:751
DetectPreFlow
uint8_t DetectPreFlow(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p)
Definition: detect.c:2326
SignatureInitData_::rule_state_dependant_sids_array
uint32_t * rule_state_dependant_sids_array
Definition: detect.h:661
DetectEngineCtx_::sgh_array_cnt
uint32_t sgh_array_cnt
Definition: detect.h:1006
SigGroupHeadAppendSig
int SigGroupHeadAppendSig(const DetectEngineCtx *de_ctx, SigGroupHead **sgh, const Signature *s)
Add a Signature to a SigGroupHead.
Definition: detect-engine-siggroup.c:319
DetectEngineCtx_::sgh_array
struct SigGroupHead_ ** sgh_array
Definition: detect.h:1005
UniquePortPoint_::port
uint16_t port
Definition: detect-engine-build.c:1318
SignatureInitData_::dst_contains_negation
bool dst_contains_negation
Definition: detect.h:603
SigGroupHeadFree
void SigGroupHeadFree(const DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Free a SigGroupHead and its members.
Definition: detect-engine-siggroup.c:163
DetectEngineTransforms::transforms
TransformData transforms[DETECT_TRANSFORMS_MAX]
Definition: detect.h:392
SIG_TYPE_DEONLY
@ SIG_TYPE_DEONLY
Definition: detect.h:71
SIGNATURE_HOOK_PKT_PRE_STREAM
@ SIGNATURE_HOOK_PKT_PRE_STREAM
Definition: detect.h:543
DetectEngineMultiTenantEnabled
bool DetectEngineMultiTenantEnabled(void)
Definition: detect-engine.c:3899
detect-flowbits.h
SIG_MASK_REQUIRE_PAYLOAD
#define SIG_MASK_REQUIRE_PAYLOAD
Definition: detect.h:311
SIGMATCH_DEONLY_COMPAT
#define SIGMATCH_DEONLY_COMPAT
Definition: detect-engine-register.h:315
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
DETECT_DSIZE
@ DETECT_DSIZE
Definition: detect-engine-register.h:54
Signature_::sp
DetectPort * sp
Definition: detect.h:723
SIG_TYPE_NOT_SET
@ SIG_TYPE_NOT_SET
Definition: detect.h:65
SignatureIsFileSha1Inspecting
int SignatureIsFileSha1Inspecting(const Signature *s)
Check if a signature contains the filesha1 keyword.
Definition: detect-engine-build.c:155
SCPortIntervalTreeInit
SCPortIntervalTree * SCPortIntervalTreeInit(void)
Function to initialize the interval tree.
Definition: util-port-interval-tree.c:58
IPOnlyCIDRListFree
void IPOnlyCIDRListFree(IPOnlyCIDRItem *tmphead)
This function free a IPOnlyCIDRItem list.
Definition: detect-engine-iponly.c:482
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2274
DetectEngineCtx_::config_prefix
char config_prefix[64]
Definition: detect.h:1059
SIG_FLAG_MPM_NEG
#define SIG_FLAG_MPM_NEG
Definition: detect.h:256
SCPortIntervalTree_::tree
struct PI tree
Definition: util-port-interval-tree.h:30
detect-engine-analyzer.h
SIG_FLAG_INIT_STATE_MATCH
#define SIG_FLAG_INIT_STATE_MATCH
Definition: detect.h:296
DetectEngineBufferTypeSupportsFramesGetById
bool DetectEngineBufferTypeSupportsFramesGetById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1454
EngineAnalysisRules2
void EngineAnalysisRules2(const DetectEngineCtx *de_ctx, const Signature *s)
Definition: detect-engine-analyzer.c:1110
SigGroupHeadSetSigCnt
void SigGroupHeadSetSigCnt(SigGroupHead *sgh, uint32_t max_idx)
Updates the SigGroupHead->sig_cnt with the total count of all the Signatures present in this SigGroup...
Definition: detect-engine-siggroup.c:446
DetectEngineCtx_::eth_non_ip_sgh
struct SigGroupHead_ * eth_non_ip_sgh
Definition: detect.h:1025
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:40
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
util-conf.h
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
SINGLE_PORT
#define SINGLE_PORT
Definition: detect-engine-build.c:1315
DetectEnginePktInspectionSetup
int DetectEnginePktInspectionSetup(Signature *s)
Definition: detect-engine.c:1852
tail
Host * tail
Definition: host.h:2
suricata-common.h
SIG_MASK_REQUIRE_NO_PAYLOAD
#define SIG_MASK_REQUIRE_NO_PAYLOAD
Definition: detect.h:315
SIG_FLAG_SP_ANY
#define SIG_FLAG_SP_ANY
Definition: detect.h:242
SigGroupHeadStore
void SigGroupHeadStore(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Definition: detect-engine-siggroup.c:109
SigMatch_::type
uint16_t type
Definition: detect.h:357
SigGroupHeadInitData_::match_array
Signature ** match_array
Definition: detect.h:1634
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:688
SigPrepareStage3
int SigPrepareStage3(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2028
SignatureHook_::type
enum SignatureHookType type
Definition: detect.h:573
DetectPort_::prev
struct DetectPort_ * prev
Definition: detect.h:232
SCLogPerf
#define SCLogPerf(...)
Definition: util-debug.h:241
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
MASK_TCP_INITDEINIT_FLAGS
#define MASK_TCP_INITDEINIT_FLAGS
Definition: detect-engine-build.c:395
DetectListToString
const char * DetectListToString(int list)
Definition: detect-parse.c:130
SIGNATURE_HOOK_PKT_PRE_FLOW
@ SIGNATURE_HOOK_PKT_PRE_FLOW
Definition: detect.h:542
DetectEngineCtx_::profile_match_logging_threshold
uint32_t profile_match_logging_threshold
Definition: detect.h:1057
Signature_::proto
DetectProto * proto
Definition: detect.h:691
FatalError
#define FatalError(...)
Definition: util-debug.h:517
SIGNATURE_HOOK_TYPE_PKT
@ SIGNATURE_HOOK_TYPE_PKT
Definition: detect.h:549
DetectEngineCtx_::sig_list
Signature * sig_list
Definition: detect.h:946
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
SigGroupHeadHashFree
void SigGroupHeadHashFree(DetectEngineCtx *de_ctx)
Frees the hash table - DetectEngineCtx->sgh_hash_table, allocated by SigGroupHeadHashInit() function.
Definition: detect-engine-siggroup.c:299
SIG_MASK_REQUIRE_FLAGS_UNUSUAL
#define SIG_MASK_REQUIRE_FLAGS_UNUSUAL
Definition: detect.h:314
TransformData_::transform
int transform
Definition: detect.h:387
DETECT_FLOWBITS
@ DETECT_FLOWBITS
Definition: detect-engine-register.h:61
app-layer-events.h
SigGroupHeadHashAdd
int SigGroupHeadHashAdd(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Adds a SigGroupHead to the detection engine context SigGroupHead hash table.
Definition: detect-engine-siggroup.c:266
util-validate.h
detect-flow.h
SCPortIntervalTree_
Definition: util-port-interval-tree.h:29
SigPrepareStage2
int SigPrepareStage2(DetectEngineCtx *de_ctx)
Fill the global src group head, with the sigs included.
Definition: detect-engine-build.c:1934
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:651
HtpBodyChunk_::next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:124
SigGroupHeadHashLookup
SigGroupHead * SigGroupHeadHashLookup(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Used to lookup a SigGroupHead hash from the detection engine context SigGroupHead hash table.
Definition: detect-engine-siggroup.c:283
Signature_::dp
DetectPort * dp
Definition: detect.h:723
DETECT_STREAM_EVENT
@ DETECT_STREAM_EVENT
Definition: detect-engine-register.h:217
Signature_::iid
SigIntId iid
Definition: detect.h:684
DETECT_PGSCORE_RULE_PORT_PRIORITIZED
#define DETECT_PGSCORE_RULE_PORT_PRIORITIZED
Definition: detect-engine-build.c:50
SignatureIsFilestoring
int SignatureIsFilestoring(const Signature *s)
Check if a signature contains the filestore keyword.
Definition: detect-engine-build.c:101
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SignatureIsIPOnly
int SignatureIsIPOnly(DetectEngineCtx *de_ctx, const Signature *s)
Test is a initialized signature is IP only.
Definition: detect-engine-build.c:191
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:717
HashListTableBucket_
Definition: util-hashlist.h:28
detect-engine-iponly.h
SignatureInitData_::score
int score
Definition: detect.h:640
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:526
Signature_
Signature container.
Definition: detect.h:672
SigMatch_
a single match condition for a signature
Definition: detect.h:356
DETECT_SM_LIST_MAX
@ DETECT_SM_LIST_MAX
Definition: detect.h:135
SigGroupHeadInitDataFree
void SigGroupHeadInitDataFree(SigGroupHeadInitData *sghid)
Definition: detect-engine-siggroup.c:60
SignatureIsFileMd5Inspecting
int SignatureIsFileMd5Inspecting(const Signature *s)
Check if a signature contains the filemd5 keyword.
Definition: detect-engine-build.c:139
DETECT_PGSCORE_RULE_MPM_NEGATED
#define DETECT_PGSCORE_RULE_MPM_NEGATED
Definition: detect-engine-build.c:52
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
SignatureSetType
void SignatureSetType(DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine-build.c:1680
RANGE_PORT
#define RANGE_PORT
Definition: detect-engine-build.c:1314
PrefilterSetupRuleGroup
int PrefilterSetupRuleGroup(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
Definition: detect-engine-prefilter.c:1176
DETECT_PROTO_L2_ANY
#define DETECT_PROTO_L2_ANY
Definition: detect-engine-proto.h:35
SCProfilingKeywordInitCounters
void SCProfilingKeywordInitCounters(DetectEngineCtx *de_ctx)
Register the keyword profiling counters.
Definition: util-profiling-keywords.c:365
DetectEngineTransforms::cnt
int cnt
Definition: detect.h:393
DetectPortHashAdd
int DetectPortHashAdd(DetectEngineCtx *de_ctx, DetectPort *dp)
Adds a DetectPort to the detection engine context DetectPort hash table.
Definition: detect-engine-port.c:1339
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:955
CONFIG_SCOPE_FLOW
@ CONFIG_SCOPE_FLOW
Definition: util-config.h:51
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
DetectEngineCtx_::buffer_type_id
uint32_t buffer_type_id
Definition: detect.h:1089
DetectPreStream
uint8_t DetectPreStream(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p)
Definition: detect.c:2336
SigParseApplyDsizeToContent
void SigParseApplyDsizeToContent(Signature *s)
Apply dsize as depth to content matches in the rule.
Definition: detect-dsize.c:320
SignatureInitData_::rule_state_flowbits_ids_array
uint32_t * rule_state_flowbits_ids_array
Definition: detect.h:664
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:939
UniquePortPoint
struct UniquePortPoint_ UniquePortPoint
SignatureInitData_::transforms
DetectEngineTransforms transforms
Definition: detect.h:632
DetectEngineCtx_::io_ctx
DetectEngineIPOnlyCtx io_ctx
Definition: detect.h:975
DetectU16Data
DetectUintData_u16 DetectU16Data
Definition: detect-engine-uint.h:42
DetectPortHashFree
void DetectPortHashFree(DetectEngineCtx *de_ctx)
Frees the hash table - DetectEngineCtx->sgh_hash_table, allocated by DetectPortInit() function.
Definition: detect-engine-port.c:1370
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:250
DetectPortHashLookup
DetectPort * DetectPortHashLookup(DetectEngineCtx *de_ctx, DetectPort *dp)
Used to lookup a DetectPort hash from the detection engine context DetectPort hash table.
Definition: detect-engine-port.c:1355
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineCtx_::sig_array_len
uint32_t sig_array_len
Definition: detect.h:956
detect-config.h
DETECT_FLAGS
@ DETECT_FLAGS
Definition: detect-engine-register.h:42
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
Signature_::type
enum SignatureType type
Definition: detect.h:675
SignatureIsFilemagicInspecting
int SignatureIsFilemagicInspecting(const Signature *s)
Check if a signature contains the filemagic keyword.
Definition: detect-engine-build.c:120
DetectEngineCtx_::signum
uint32_t signum
Definition: detect.h:958
DetectEngineCtx_::tenant_id
uint32_t tenant_id
Definition: detect.h:944
SigGroupHead_::filestore_cnt
uint16_t filestore_cnt
Definition: detect.h:1644
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:652
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
DetectEngineLookupFlow_::tcp
DetectPort * tcp
Definition: detect.h:867
DetectPortCleanupList
void DetectPortCleanupList(const DetectEngineCtx *de_ctx, DetectPort *head)
Free a DetectPort list and each of its members.
Definition: detect-engine-port.c:124
SIG_TYPE_PDONLY
@ SIG_TYPE_PDONLY
Definition: detect.h:70
SCLogDebugEnabled
int SCLogDebugEnabled(void)
Returns whether debug messages are enabled to be logged or not.
Definition: util-debug.c:768
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
DetectEngineAppInspectionEngine2Signature
int DetectEngineAppInspectionEngine2Signature(DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine.c:801
SCPortIntervalTreeFree
void SCPortIntervalTreeFree(DetectEngineCtx *de_ctx, SCPortIntervalTree *it)
Function to free an entire interval tree.
Definition: util-port-interval-tree.c:92
TCPHdr_
Definition: decode-tcp.h:149
detect-engine-address.h
SigGroupHead_::id
uint32_t id
Definition: detect.h:1646
VarNameStoreActivate
int VarNameStoreActivate(void)
Definition: util-var-name.c:221
SIG_FLAG_FILESTORE
#define SIG_FLAG_FILESTORE
Definition: detect.h:268
DETECT_UINT_RA
#define DETECT_UINT_RA
Definition: detect-engine-uint.h:34
SIG_FLAG_DP_ANY
#define SIG_FLAG_DP_ANY
Definition: detect.h:243
DETECT_FLOWBITS_CMD_SET
#define DETECT_FLOWBITS_CMD_SET
Definition: detect-flowbits.h:28
detect-engine-threshold.h
SignatureMask
#define SignatureMask
Definition: decode.h:100
SigGroupHeadCopySigs
int SigGroupHeadCopySigs(DetectEngineCtx *de_ctx, SigGroupHead *src, SigGroupHead **dst)
Copies the bitarray holding the sids from the source SigGroupHead to the destination SigGroupHead.
Definition: detect-engine-siggroup.c:390
Signature_::mask
SignatureMask mask
Definition: detect.h:683
SIG_TYPE_LIKE_IPONLY
@ SIG_TYPE_LIKE_IPONLY
Definition: detect.h:67
PacketEngineEvents_::cnt
uint8_t cnt
Definition: decode.h:308
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:115
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:253