suricata
detect.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2017 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Basic detection engine
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 #include "conf.h"
29 
30 #include "decode.h"
31 #include "flow.h"
32 #include "stream-tcp.h"
33 #include "app-layer.h"
34 #include "app-layer-parser.h"
35 
36 #include "detect.h"
37 #include "detect-dsize.h"
38 #include "detect-engine.h"
39 #include "detect-engine-profile.h"
40 
41 #include "detect-engine-alert.h"
42 #include "detect-engine-siggroup.h"
43 #include "detect-engine-address.h"
44 #include "detect-engine-proto.h"
45 #include "detect-engine-port.h"
46 #include "detect-engine-mpm.h"
47 #include "detect-engine-iponly.h"
50 #include "detect-engine-state.h"
51 #include "detect-engine-analyzer.h"
52 
53 #include "detect-engine-payload.h"
54 #include "detect-engine-event.h"
55 
56 #include "detect-filestore.h"
57 #include "detect-flowvar.h"
58 #include "detect-replace.h"
59 
60 #include "util-validate.h"
61 #include "util-detect.h"
62 
63 typedef struct DetectRunScratchpad {
65  const uint8_t flow_flags; /* flow/state flags: STREAM_* */
66  const bool app_decoder_events;
67  const SigGroupHead *sgh;
70 
71 /* prototypes */
72 static DetectRunScratchpad DetectRunSetup(const DetectEngineCtx *de_ctx,
73  DetectEngineThreadCtx *det_ctx, Packet * const p, Flow * const pflow);
74 static void DetectRunInspectIPOnly(ThreadVars *tv, const DetectEngineCtx *de_ctx,
75  DetectEngineThreadCtx *det_ctx, Flow * const pflow, Packet * const p);
76 static inline void DetectRunGetRuleGroup(const DetectEngineCtx *de_ctx,
77  Packet * const p, Flow * const pflow, DetectRunScratchpad *scratch);
78 static inline void DetectRunPrefilterPkt(ThreadVars *tv,
80  DetectRunScratchpad *scratch);
81 static inline void DetectRulePacketRules(ThreadVars * const tv,
82  DetectEngineCtx * const de_ctx, DetectEngineThreadCtx * const det_ctx,
83  Packet * const p, Flow * const pflow, const DetectRunScratchpad *scratch);
84 static void DetectRunTx(ThreadVars *tv, DetectEngineCtx *de_ctx,
85  DetectEngineThreadCtx *det_ctx, Packet *p,
86  Flow *f, DetectRunScratchpad *scratch);
87 static void DetectRunFrames(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
88  Packet *p, Flow *f, DetectRunScratchpad *scratch);
89 static inline void DetectRunPostRules(ThreadVars *tv, DetectEngineCtx *de_ctx,
90  DetectEngineThreadCtx *det_ctx, Packet * const p, Flow * const pflow,
91  DetectRunScratchpad *scratch);
92 static void DetectRunCleanup(DetectEngineThreadCtx *det_ctx,
93  Packet *p, Flow * const pflow);
94 
95 /** \internal
96  */
97 static void DetectRun(ThreadVars *th_v,
99  Packet *p)
100 {
101  SCEnter();
102  SCLogDebug("p->pcap_cnt %" PRIu64 " direction %s flow %p", p->pcap_cnt,
103  p->flow ? (FlowGetPacketDirection(p->flow, p) == TOSERVER ? "toserver" : "toclient")
104  : "noflow",
105  p->flow);
106 
107  /* bail early if packet should not be inspected */
108  if (p->flags & PKT_NOPACKET_INSPECTION) {
109  /* nothing to do */
110  SCReturn;
111  }
112 
113  /* Load the Packet's flow early, even though it might not be needed.
114  * Mark as a constant pointer, although the flow itself can change. */
115  Flow * const pflow = p->flow;
116 
117  DetectRunScratchpad scratch = DetectRunSetup(de_ctx, det_ctx, p, pflow);
118 
119  /* run the IPonly engine */
120  DetectRunInspectIPOnly(th_v, de_ctx, det_ctx, pflow, p);
121 
122  /* get our rule group */
123  DetectRunGetRuleGroup(de_ctx, p, pflow, &scratch);
124  /* if we didn't get a sig group head, we
125  * have nothing to do.... */
126  if (scratch.sgh == NULL) {
127  SCLogDebug("no sgh for this packet, nothing to match against");
128  goto end;
129  }
130 
131  /* run the prefilters for packets */
132  DetectRunPrefilterPkt(th_v, de_ctx, det_ctx, p, &scratch);
133 
135  /* inspect the rules against the packet */
136  DetectRulePacketRules(th_v, de_ctx, det_ctx, p, pflow, &scratch);
138 
139  /* run tx/state inspection. Don't call for ICMP error msgs. */
140  if (pflow && pflow->alstate && likely(pflow->proto == p->proto)) {
141  if (p->proto == IPPROTO_TCP) {
142  const TcpSession *ssn = p->flow->protoctx;
143  if (ssn && (ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED) == 0) {
144  // PACKET_PROFILING_DETECT_START(p, PROF_DETECT_TX);
145  DetectRunFrames(th_v, de_ctx, det_ctx, p, pflow, &scratch);
146  // PACKET_PROFILING_DETECT_END(p, PROF_DETECT_TX);
147  }
148  }
149 
151  DetectRunTx(th_v, de_ctx, det_ctx, p, pflow, &scratch);
153  }
154 
155 end:
156  DetectRunPostRules(th_v, de_ctx, det_ctx, p, pflow, &scratch);
157 
158  DetectRunCleanup(det_ctx, p, pflow);
159  SCReturn;
160 }
161 
162 static void DetectRunPostMatch(ThreadVars *tv,
163  DetectEngineThreadCtx *det_ctx, Packet *p,
164  const Signature *s)
165 {
166  /* run the packet match functions */
168  if (smd != NULL) {
170 
171  SCLogDebug("running match functions, sm %p", smd);
172 
173  while (1) {
175  (void)sigmatch_table[smd->type].Match(det_ctx, p, s, smd->ctx);
176  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
177  if (smd->is_last)
178  break;
179  smd++;
180  }
181  }
182 }
183 
184 /**
185  * \brief Get the SigGroupHead for a packet.
186  *
187  * \param de_ctx detection engine context
188  * \param p packet
189  *
190  * \retval sgh the SigGroupHead or NULL if non applies to the packet
191  */
193  const Packet *p)
194 {
195  SCEnter();
196 
197  int f;
198  SigGroupHead *sgh = NULL;
199 
200  /* if the packet proto is 0 (not set), we're inspecting it against
201  * the decoder events sgh we have. */
202  if (p->proto == 0 && p->events.cnt > 0) {
203  SCReturnPtr(de_ctx->decoder_event_sgh, "SigGroupHead");
204  } else if (p->proto == 0) {
205  if (!(PKT_IS_IPV4(p) || PKT_IS_IPV6(p))) {
206  /* not IP, so nothing to do */
207  SCReturnPtr(NULL, "SigGroupHead");
208  }
209  }
210 
211  /* select the flow_gh */
212  if (p->flowflags & FLOW_PKT_TOCLIENT)
213  f = 0;
214  else
215  f = 1;
216 
217  int proto = IP_GET_IPPROTO(p);
218  if (proto == IPPROTO_TCP) {
219  DetectPort *list = de_ctx->flow_gh[f].tcp;
220  SCLogDebug("tcp toserver %p, tcp toclient %p: going to use %p",
222  uint16_t port = f ? p->dp : p->sp;
223  SCLogDebug("tcp port %u -> %u:%u", port, p->sp, p->dp);
224  DetectPort *sghport = DetectPortLookupGroup(list, port);
225  if (sghport != NULL)
226  sgh = sghport->sh;
227  SCLogDebug("TCP list %p, port %u, direction %s, sghport %p, sgh %p",
228  list, port, f ? "toserver" : "toclient", sghport, sgh);
229  } else if (proto == IPPROTO_UDP) {
230  DetectPort *list = de_ctx->flow_gh[f].udp;
231  uint16_t port = f ? p->dp : p->sp;
232  DetectPort *sghport = DetectPortLookupGroup(list, port);
233  if (sghport != NULL)
234  sgh = sghport->sh;
235  SCLogDebug("UDP list %p, port %u, direction %s, sghport %p, sgh %p",
236  list, port, f ? "toserver" : "toclient", sghport, sgh);
237  } else {
238  sgh = de_ctx->flow_gh[f].sgh[proto];
239  }
240 
241  SCReturnPtr(sgh, "SigGroupHead");
242 }
243 
244 static inline void DetectPrefilterMergeSort(DetectEngineCtx *de_ctx,
245  DetectEngineThreadCtx *det_ctx)
246 {
247  SigIntId mpm, nonmpm;
248  SigIntId *mpm_ptr = det_ctx->pmq.rule_id_array;
249  SigIntId *nonmpm_ptr = det_ctx->non_pf_id_array;
250  uint32_t m_cnt = det_ctx->pmq.rule_id_array_cnt;
251  uint32_t n_cnt = det_ctx->non_pf_id_cnt;
252  SigIntId *final_ptr;
253  uint32_t final_cnt;
254  SigIntId id;
255  SigIntId previous_id = (SigIntId)-1;
256  Signature **sig_array = de_ctx->sig_array;
257  Signature **match_array = det_ctx->match_array;
258  Signature *s;
259 
260  SCLogDebug("PMQ rule id array count %d", det_ctx->pmq.rule_id_array_cnt);
261 
262  /* Load first values. */
263  if (likely(m_cnt)) {
264  mpm = *mpm_ptr;
265  } else {
266  /* mpm list is empty */
267  final_ptr = nonmpm_ptr;
268  final_cnt = n_cnt;
269  goto final;
270  }
271  if (likely(n_cnt)) {
272  nonmpm = *nonmpm_ptr;
273  } else {
274  /* non-mpm list is empty. */
275  final_ptr = mpm_ptr;
276  final_cnt = m_cnt;
277  goto final;
278  }
279  while (1) {
280  if (mpm < nonmpm) {
281  /* Take from mpm list */
282  id = mpm;
283 
284  s = sig_array[id];
285  /* As the mpm list can contain duplicates, check for that here. */
286  if (likely(id != previous_id)) {
287  *match_array++ = s;
288  previous_id = id;
289  }
290  if (unlikely(--m_cnt == 0)) {
291  /* mpm list is now empty */
292  final_ptr = nonmpm_ptr;
293  final_cnt = n_cnt;
294  goto final;
295  }
296  mpm_ptr++;
297  mpm = *mpm_ptr;
298  } else if (mpm > nonmpm) {
299  id = nonmpm;
300 
301  s = sig_array[id];
302  /* As the mpm list can contain duplicates, check for that here. */
303  if (likely(id != previous_id)) {
304  *match_array++ = s;
305  previous_id = id;
306  }
307  if (unlikely(--n_cnt == 0)) {
308  final_ptr = mpm_ptr;
309  final_cnt = m_cnt;
310  goto final;
311  }
312  nonmpm_ptr++;
313  nonmpm = *nonmpm_ptr;
314 
315  } else { /* implied mpm == nonmpm */
316  /* special case: if on both lists, it's a negated mpm pattern */
317 
318  /* mpm list may have dups, so skip past them here */
319  while (--m_cnt != 0) {
320  mpm_ptr++;
321  mpm = *mpm_ptr;
322  if (mpm != nonmpm)
323  break;
324  }
325  /* if mpm is done, update nonmpm_ptrs and jump to final */
326  if (unlikely(m_cnt == 0)) {
327  n_cnt--;
328 
329  /* mpm list is now empty */
330  final_ptr = ++nonmpm_ptr;
331  final_cnt = n_cnt;
332  goto final;
333  }
334  /* otherwise, if nonmpm is done jump to final for mpm
335  * mpm ptrs alrady updated */
336  if (unlikely(--n_cnt == 0)) {
337  final_ptr = mpm_ptr;
338  final_cnt = m_cnt;
339  goto final;
340  }
341 
342  /* not at end of the lists, update nonmpm. Mpm already
343  * updated in while loop above. */
344  nonmpm_ptr++;
345  nonmpm = *nonmpm_ptr;
346  }
347  }
348 
349  final: /* Only one list remaining. Just walk that list. */
350 
351  while (final_cnt-- > 0) {
352  id = *final_ptr++;
353  s = sig_array[id];
354 
355  /* As the mpm list can contain duplicates, check for that here. */
356  if (likely(id != previous_id)) {
357  *match_array++ = s;
358  previous_id = id;
359  }
360  }
361 
362  det_ctx->match_array_cnt = match_array - det_ctx->match_array;
363  DEBUG_VALIDATE_BUG_ON((det_ctx->pmq.rule_id_array_cnt + det_ctx->non_pf_id_cnt) < det_ctx->match_array_cnt);
364  PMQ_RESET(&det_ctx->pmq);
365 }
366 
367 /** \internal
368  * \brief build non-prefilter list based on the rule group list we've set.
369  */
370 static inline void
371 DetectPrefilterBuildNonPrefilterList(DetectEngineThreadCtx *det_ctx,
372  const SignatureMask mask, const uint8_t alproto)
373 {
374  for (uint32_t x = 0; x < det_ctx->non_pf_store_cnt; x++) {
375  /* only if the mask matches this rule can possibly match,
376  * so build the non_mpm array only for match candidates */
377  const SignatureMask rule_mask = det_ctx->non_pf_store_ptr[x].mask;
378  const uint8_t rule_alproto = det_ctx->non_pf_store_ptr[x].alproto;
379  if ((rule_mask & mask) == rule_mask &&
380  (rule_alproto == 0 || AppProtoEquals(rule_alproto, alproto))) {
381  det_ctx->non_pf_id_array[det_ctx->non_pf_id_cnt++] = det_ctx->non_pf_store_ptr[x].id;
382  }
383  }
384 }
385 
386 /** \internal
387  * \brief select non-mpm list
388  * Based on the packet properties, select the non-mpm list to use
389  * \todo move non_pf_store* into scratchpad */
390 static inline void
391 DetectPrefilterSetNonPrefilterList(const Packet *p, DetectEngineThreadCtx *det_ctx, DetectRunScratchpad *scratch)
392 {
393  if ((p->proto == IPPROTO_TCP) && (p->tcph != NULL) && (p->tcph->th_flags & TH_SYN)) {
394  det_ctx->non_pf_store_ptr = scratch->sgh->non_pf_syn_store_array;
395  det_ctx->non_pf_store_cnt = scratch->sgh->non_pf_syn_store_cnt;
396  } else {
397  det_ctx->non_pf_store_ptr = scratch->sgh->non_pf_other_store_array;
398  det_ctx->non_pf_store_cnt = scratch->sgh->non_pf_other_store_cnt;
399  }
400  SCLogDebug("sgh non_pf ptr %p cnt %u (syn %p/%u, other %p/%u)",
401  det_ctx->non_pf_store_ptr, det_ctx->non_pf_store_cnt,
402  scratch->sgh->non_pf_syn_store_array, scratch->sgh->non_pf_syn_store_cnt,
404 }
405 
406 /** \internal
407  * \brief update flow's file tracking flags based on the detection engine
408  * A set of flags is prepared that is sent to the File API. The
409  File API may reject one or more based on the global force settings.
410  */
411 static inline void
412 DetectPostInspectFileFlagsUpdate(Flow *f, const SigGroupHead *sgh, uint8_t direction)
413 {
414  uint16_t flow_file_flags = FLOWFILE_INIT;
415 
416  if (sgh == NULL) {
417  SCLogDebug("requesting disabling all file features for flow");
418  flow_file_flags = FLOWFILE_NONE;
419  } else {
420  if (sgh->filestore_cnt == 0) {
421  SCLogDebug("requesting disabling filestore for flow");
422  flow_file_flags |= (FLOWFILE_NO_STORE_TS|FLOWFILE_NO_STORE_TC);
423  }
424 #ifdef HAVE_MAGIC
425  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILEMAGIC)) {
426  SCLogDebug("requesting disabling magic for flow");
427  flow_file_flags |= (FLOWFILE_NO_MAGIC_TS|FLOWFILE_NO_MAGIC_TC);
428  }
429 #endif
430  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILEMD5)) {
431  SCLogDebug("requesting disabling md5 for flow");
432  flow_file_flags |= (FLOWFILE_NO_MD5_TS|FLOWFILE_NO_MD5_TC);
433  }
434  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILESHA1)) {
435  SCLogDebug("requesting disabling sha1 for flow");
436  flow_file_flags |= (FLOWFILE_NO_SHA1_TS|FLOWFILE_NO_SHA1_TC);
437  }
438  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILESHA256)) {
439  SCLogDebug("requesting disabling sha256 for flow");
440  flow_file_flags |= (FLOWFILE_NO_SHA256_TS|FLOWFILE_NO_SHA256_TC);
441  }
442  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILESIZE)) {
443  SCLogDebug("requesting disabling filesize for flow");
444  flow_file_flags |= (FLOWFILE_NO_SIZE_TS|FLOWFILE_NO_SIZE_TC);
445  }
446  }
447  if (flow_file_flags != 0) {
448  FileUpdateFlowFileFlags(f, flow_file_flags, direction);
449  }
450 }
451 
452 static inline void
453 DetectRunPostGetFirstRuleGroup(const Packet *p, Flow *pflow, const SigGroupHead *sgh)
454 {
455  if ((p->flowflags & FLOW_PKT_TOSERVER) && !(pflow->flags & FLOW_SGH_TOSERVER)) {
456  /* first time we see this toserver sgh, store it */
457  pflow->sgh_toserver = sgh;
458  pflow->flags |= FLOW_SGH_TOSERVER;
459 
460  if (p->proto == IPPROTO_TCP && (sgh == NULL || !(sgh->flags & SIG_GROUP_HEAD_HAVERAWSTREAM))) {
461  if (pflow->protoctx != NULL) {
462  TcpSession *ssn = pflow->protoctx;
463  SCLogDebug("STREAMTCP_STREAM_FLAG_DISABLE_RAW ssn.client");
465  }
466  }
467 
468  DetectPostInspectFileFlagsUpdate(pflow,
469  pflow->sgh_toserver, STREAM_TOSERVER);
470 
471  } else if ((p->flowflags & FLOW_PKT_TOCLIENT) && !(pflow->flags & FLOW_SGH_TOCLIENT)) {
472  pflow->sgh_toclient = sgh;
473  pflow->flags |= FLOW_SGH_TOCLIENT;
474 
475  if (p->proto == IPPROTO_TCP && (sgh == NULL || !(sgh->flags & SIG_GROUP_HEAD_HAVERAWSTREAM))) {
476  if (pflow->protoctx != NULL) {
477  TcpSession *ssn = pflow->protoctx;
478  SCLogDebug("STREAMTCP_STREAM_FLAG_DISABLE_RAW ssn.server");
480  }
481  }
482 
483  DetectPostInspectFileFlagsUpdate(pflow,
484  pflow->sgh_toclient, STREAM_TOCLIENT);
485  }
486 }
487 
488 static inline void DetectRunGetRuleGroup(
489  const DetectEngineCtx *de_ctx,
490  Packet * const p, Flow * const pflow,
491  DetectRunScratchpad *scratch)
492 {
493  const SigGroupHead *sgh = NULL;
494 
495  if (pflow) {
496  bool use_flow_sgh = false;
497  /* Get the stored sgh from the flow (if any). Make sure we're not using
498  * the sgh for icmp error packets part of the same stream. */
499  if (IP_GET_IPPROTO(p) == pflow->proto) { /* filter out icmp */
501  if ((p->flowflags & FLOW_PKT_TOSERVER) && (pflow->flags & FLOW_SGH_TOSERVER)) {
502  sgh = pflow->sgh_toserver;
503  SCLogDebug("sgh = pflow->sgh_toserver; => %p", sgh);
504  use_flow_sgh = true;
505  } else if ((p->flowflags & FLOW_PKT_TOCLIENT) && (pflow->flags & FLOW_SGH_TOCLIENT)) {
506  sgh = pflow->sgh_toclient;
507  SCLogDebug("sgh = pflow->sgh_toclient; => %p", sgh);
508  use_flow_sgh = true;
509  }
511  }
512 
513  if (!(use_flow_sgh)) {
517 
518  /* HACK: prevent the wrong sgh (or NULL) from being stored in the
519  * flow's sgh pointers */
521  ; /* no-op */
522  } else {
523  /* store the found sgh (or NULL) in the flow to save us
524  * from looking it up again for the next packet.
525  * Also run other tasks */
526  DetectRunPostGetFirstRuleGroup(p, pflow, sgh);
527  }
528  }
529  } else { /* p->flags & PKT_HAS_FLOW */
530  /* no flow */
531 
535  }
536 
537  scratch->sgh = sgh;
538 }
539 
540 static void DetectRunInspectIPOnly(ThreadVars *tv, const DetectEngineCtx *de_ctx,
541  DetectEngineThreadCtx *det_ctx,
542  Flow * const pflow, Packet * const p)
543 {
544  if (pflow) {
545  /* set the iponly stuff */
546  if (pflow->flags & FLOW_TOCLIENT_IPONLY_SET)
548  if (pflow->flags & FLOW_TOSERVER_IPONLY_SET)
550 
553  {
554  SCLogDebug("testing against \"ip-only\" signatures");
555 
557  IPOnlyMatchPacket(tv, de_ctx, det_ctx, &de_ctx->io_ctx, &det_ctx->io_ctx, p);
559 
560  /* save in the flow that we scanned this direction... */
561  FlowSetIPOnlyFlag(pflow, p->flowflags & FLOW_PKT_TOSERVER ? 1 : 0);
562  }
563  } else { /* p->flags & PKT_HAS_FLOW */
564  /* no flow */
565 
566  /* Even without flow we should match the packet src/dst */
568  IPOnlyMatchPacket(tv, de_ctx, det_ctx, &de_ctx->io_ctx,
569  &det_ctx->io_ctx, p);
571  }
572 }
573 
574 /* returns 0 if no match, 1 if match */
575 static inline int DetectRunInspectRuleHeader(
576  const Packet *p,
577  const Flow *f,
578  const Signature *s,
579  const uint32_t sflags,
580  const uint8_t s_proto_flags)
581 {
582  /* check if this signature has a requirement for flowvars of some type
583  * and if so, if we actually have any in the flow. If not, the sig
584  * can't match and we skip it. */
585  if ((p->flags & PKT_HAS_FLOW) && (sflags & SIG_FLAG_REQUIRE_FLOWVAR)) {
586  DEBUG_VALIDATE_BUG_ON(f == NULL);
587 
588  int m = f->flowvar ? 1 : 0;
589 
590  /* no flowvars? skip this sig */
591  if (m == 0) {
592  SCLogDebug("skipping sig as the flow has no flowvars and sig "
593  "has SIG_FLAG_REQUIRE_FLOWVAR flag set.");
594  return 0;
595  }
596  }
597 
598  if ((s_proto_flags & DETECT_PROTO_IPV4) && !PKT_IS_IPV4(p)) {
599  SCLogDebug("ip version didn't match");
600  return 0;
601  }
602  if ((s_proto_flags & DETECT_PROTO_IPV6) && !PKT_IS_IPV6(p)) {
603  SCLogDebug("ip version didn't match");
604  return 0;
605  }
606 
607  if (DetectProtoContainsProto(&s->proto, IP_GET_IPPROTO(p)) == 0) {
608  SCLogDebug("proto didn't match");
609  return 0;
610  }
611 
612  /* check the source & dst port in the sig */
613  if (p->proto == IPPROTO_TCP || p->proto == IPPROTO_UDP || p->proto == IPPROTO_SCTP) {
614  if (!(sflags & SIG_FLAG_DP_ANY)) {
615  if (p->flags & PKT_IS_FRAGMENT)
616  return 0;
617  DetectPort *dport = DetectPortLookupGroup(s->dp,p->dp);
618  if (dport == NULL) {
619  SCLogDebug("dport didn't match.");
620  return 0;
621  }
622  }
623  if (!(sflags & SIG_FLAG_SP_ANY)) {
624  if (p->flags & PKT_IS_FRAGMENT)
625  return 0;
626  DetectPort *sport = DetectPortLookupGroup(s->sp,p->sp);
627  if (sport == NULL) {
628  SCLogDebug("sport didn't match.");
629  return 0;
630  }
631  }
632  } else if ((sflags & (SIG_FLAG_DP_ANY|SIG_FLAG_SP_ANY)) != (SIG_FLAG_DP_ANY|SIG_FLAG_SP_ANY)) {
633  SCLogDebug("port-less protocol and sig needs ports");
634  return 0;
635  }
636 
637  /* check the destination address */
638  if (!(sflags & SIG_FLAG_DST_ANY)) {
639  if (PKT_IS_IPV4(p)) {
641  return 0;
642  } else if (PKT_IS_IPV6(p)) {
644  return 0;
645  }
646  }
647  /* check the source address */
648  if (!(sflags & SIG_FLAG_SRC_ANY)) {
649  if (PKT_IS_IPV4(p)) {
651  return 0;
652  } else if (PKT_IS_IPV6(p)) {
654  return 0;
655  }
656  }
657 
658  return 1;
659 }
660 
661 /** \internal
662  * \brief run packet/stream prefilter engines
663  */
664 static inline void DetectRunPrefilterPkt(
665  ThreadVars *tv,
667  DetectEngineThreadCtx *det_ctx,
668  Packet *p,
669  DetectRunScratchpad *scratch
670 )
671 {
672  DetectPrefilterSetNonPrefilterList(p, det_ctx, scratch);
673 
674  /* create our prefilter mask */
675  PacketCreateMask(p, &scratch->pkt_mask, scratch->alproto, scratch->app_decoder_events);
676 
677  /* build and prefilter non_pf list against the mask of the packet */
679  det_ctx->non_pf_id_cnt = 0;
680  if (likely(det_ctx->non_pf_store_cnt > 0)) {
681  DetectPrefilterBuildNonPrefilterList(det_ctx, scratch->pkt_mask, scratch->alproto);
682  }
684 
685  /* run the prefilter engines */
686  Prefilter(det_ctx, scratch->sgh, p, scratch->flow_flags);
687  /* create match list if we have non-pf and/or pf */
688  if (det_ctx->non_pf_store_cnt || det_ctx->pmq.rule_id_array_cnt) {
689 #ifdef PROFILING
690  if (tv) {
691  StatsAddUI64(tv, det_ctx->counter_mpm_list, (uint64_t)det_ctx->pmq.rule_id_array_cnt);
692  }
693 #endif
695  DetectPrefilterMergeSort(de_ctx, det_ctx);
697  }
698 
699 #ifdef PROFILING
700  if (tv) {
702  (uint64_t)det_ctx->non_pf_store_cnt);
703  /* non mpm sigs after mask prefilter */
705  (uint64_t)det_ctx->non_pf_id_cnt);
706  }
707 #endif
708 }
709 
710 static inline void DetectRulePacketRules(
711  ThreadVars * const tv,
712  DetectEngineCtx * const de_ctx,
713  DetectEngineThreadCtx * const det_ctx,
714  Packet * const p,
715  Flow * const pflow,
716  const DetectRunScratchpad *scratch
717 )
718 {
719  const Signature *s = NULL;
720  const Signature *next_s = NULL;
721 
722  /* inspect the sigs against the packet */
723  /* Prefetch the next signature. */
724  SigIntId match_cnt = det_ctx->match_array_cnt;
725 #ifdef PROFILING
726  if (tv) {
728  (uint64_t)match_cnt);
729  }
730 #endif
731  Signature **match_array = det_ctx->match_array;
732 
733  SGH_PROFILING_RECORD(det_ctx, scratch->sgh);
734 #ifdef PROFILING
735  if (match_cnt >= de_ctx->profile_match_logging_threshold)
736  RulesDumpMatchArray(det_ctx, scratch->sgh, p);
737 #endif
738 
739  uint32_t sflags, next_sflags = 0;
740  if (match_cnt) {
741  next_s = *match_array++;
742  next_sflags = next_s->flags;
743  }
744  while (match_cnt--) {
746  uint8_t alert_flags = 0;
747 #ifdef PROFILING
748  bool smatch = false; /* signature match */
749 #endif
750  s = next_s;
751  sflags = next_sflags;
752  if (match_cnt) {
753  next_s = *match_array++;
754  next_sflags = next_s->flags;
755  }
756  const uint8_t s_proto_flags = s->proto.flags;
757 
758  SCLogDebug("inspecting signature id %"PRIu32"", s->id);
759 
760  if (s->app_inspect != NULL) {
761  goto next; // handle sig in DetectRunTx
762  }
763  if (s->frame_inspect != NULL) {
764  goto next; // handle sig in DetectRunFrame
765  }
766 
767  /* don't run mask check for stateful rules.
768  * There we depend on prefilter */
769  if ((s->mask & scratch->pkt_mask) != s->mask) {
770  SCLogDebug("mask mismatch %x & %x != %x", s->mask, scratch->pkt_mask, s->mask);
771  goto next;
772  }
773 
774  if (SigDsizePrefilter(p, s, sflags))
775  goto next;
776 
777  /* if the sig has alproto and the session as well they should match */
778  if (likely(sflags & SIG_FLAG_APPLAYER)) {
779  if (s->alproto != ALPROTO_UNKNOWN && !AppProtoEquals(s->alproto, scratch->alproto)) {
780  if (s->alproto == ALPROTO_DCERPC) {
781  if (scratch->alproto != ALPROTO_SMB) {
782  SCLogDebug("DCERPC sig, alproto not SMB");
783  goto next;
784  }
785  } else {
786  SCLogDebug("alproto mismatch");
787  goto next;
788  }
789  }
790  }
791 
792  if (DetectRunInspectRuleHeader(p, pflow, s, sflags, s_proto_flags) == 0) {
793  goto next;
794  }
795 
796  if (DetectEnginePktInspectionRun(tv, det_ctx, s, pflow, p, &alert_flags) == false) {
797  goto next;
798  }
799 
800 #ifdef PROFILING
801  smatch = true;
802 #endif
803  DetectRunPostMatch(tv, det_ctx, p, s);
804 
805  PacketAlertAppend(det_ctx, s, p, 0, alert_flags);
806 next:
807  DetectVarProcessList(det_ctx, pflow, p);
808  DetectReplaceFree(det_ctx);
809  RULE_PROFILING_END(det_ctx, s, smatch, p);
810 
811  det_ctx->flags = 0;
812  continue;
813  }
814 }
815 
816 static DetectRunScratchpad DetectRunSetup(
817  const DetectEngineCtx *de_ctx,
818  DetectEngineThreadCtx *det_ctx,
819  Packet * const p, Flow * const pflow)
820 {
821  AppProto alproto = ALPROTO_UNKNOWN;
822  uint8_t flow_flags = 0; /* flow/state flags */
823  bool app_decoder_events = false;
824 
826 
827 #ifdef UNITTESTS
828  p->alerts.cnt = 0;
829 #endif
830  det_ctx->filestore_cnt = 0;
831  det_ctx->base64_decoded_len = 0;
832  det_ctx->raw_stream_progress = 0;
833  det_ctx->match_array_cnt = 0;
834 
835 #ifdef DEBUG
836  if (p->flags & PKT_STREAM_ADD) {
837  det_ctx->pkt_stream_add_cnt++;
838  }
839 #endif
840 
841  /* grab the protocol state we will detect on */
842  if (p->flags & PKT_HAS_FLOW) {
843  DEBUG_VALIDATE_BUG_ON(pflow == NULL);
844 
845  if (p->flowflags & FLOW_PKT_TOSERVER) {
846  flow_flags = STREAM_TOSERVER;
847  SCLogDebug("flag STREAM_TOSERVER set");
848  } else if (p->flowflags & FLOW_PKT_TOCLIENT) {
849  flow_flags = STREAM_TOCLIENT;
850  SCLogDebug("flag STREAM_TOCLIENT set");
851  }
852  SCLogDebug("p->flowflags 0x%02x", p->flowflags);
853 
854  if (p->flags & PKT_STREAM_EOF) {
855  flow_flags |= STREAM_EOF;
856  SCLogDebug("STREAM_EOF set");
857  }
858 
859  /* store tenant_id in the flow so that we can use it
860  * for creating pseudo packets */
861  if (p->tenant_id > 0 && pflow->tenant_id == 0) {
862  pflow->tenant_id = p->tenant_id;
863  }
864 
865  /* live ruleswap check for flow updates */
866  if (pflow->de_ctx_version == 0) {
867  /* first time this flow is inspected, set id */
868  pflow->de_ctx_version = de_ctx->version;
869  } else if (pflow->de_ctx_version != de_ctx->version) {
870  /* first time we inspect flow with this de_ctx, reset */
871  pflow->flags &= ~FLOW_SGH_TOSERVER;
872  pflow->flags &= ~FLOW_SGH_TOCLIENT;
873  pflow->sgh_toserver = NULL;
874  pflow->sgh_toclient = NULL;
875 
876  pflow->de_ctx_version = de_ctx->version;
877  GenericVarFree(pflow->flowvar);
878  pflow->flowvar = NULL;
879 
881  }
882 
883  /* Retrieve the app layer state and protocol and the tcp reassembled
884  * stream chunks. */
885  if ((p->proto == IPPROTO_TCP && (p->flags & PKT_STREAM_EST)) ||
886  (p->proto == IPPROTO_UDP) ||
887  (p->proto == IPPROTO_SCTP && (p->flowflags & FLOW_PKT_ESTABLISHED)))
888  {
889  /* update flow flags with knowledge on disruptions */
890  flow_flags = FlowGetDisruptionFlags(pflow, flow_flags);
891  alproto = FlowGetAppProtocol(pflow);
892  if (p->proto == IPPROTO_TCP && pflow->protoctx &&
895  flow_flags |= STREAM_FLUSH;
896  }
897  SCLogDebug("alproto %u", alproto);
898  } else {
899  SCLogDebug("packet doesn't have established flag set (proto %d)", p->proto);
900  }
901 
902  app_decoder_events = AppLayerParserHasDecoderEvents(pflow->alparser);
903  }
904 
905  DetectRunScratchpad pad = { alproto, flow_flags, app_decoder_events, NULL, 0 };
907  return pad;
908 }
909 
910 static inline void DetectRunPostRules(
911  ThreadVars *tv,
913  DetectEngineThreadCtx *det_ctx,
914  Packet * const p,
915  Flow * const pflow,
916  DetectRunScratchpad *scratch)
917 {
918  /* see if we need to increment the inspect_id and reset the de_state */
919  if (pflow && pflow->alstate) {
921  DeStateUpdateInspectTransactionId(pflow, scratch->flow_flags, (scratch->sgh == NULL));
923  }
924 
925  /* so now let's iterate the alerts and remove the ones after a pass rule
926  * matched (if any). This is done inside PacketAlertFinalize() */
927  /* PR: installed "tag" keywords are handled after the threshold inspection */
928 
930  PacketAlertFinalize(de_ctx, det_ctx, p);
931  if (p->alerts.cnt > 0) {
932  StatsAddUI64(tv, det_ctx->counter_alerts, (uint64_t)p->alerts.cnt);
933  }
935 }
936 
937 static void DetectRunCleanup(DetectEngineThreadCtx *det_ctx,
938  Packet *p, Flow * const pflow)
939 {
941  InspectionBufferClean(det_ctx);
942 
943  if (pflow != NULL) {
944  /* update inspected tracker for raw reassembly */
945  if (p->proto == IPPROTO_TCP && pflow->protoctx != NULL &&
948  det_ctx->raw_stream_progress);
949  }
950  }
952  SCReturn;
953 }
954 
956 {
958  det_ctx->tx_candidates = SCCalloc(size, sizeof(RuleMatchCandidateTx));
959  if (det_ctx->tx_candidates == NULL) {
960  FatalError(SC_ERR_MEM_ALLOC, "failed to allocate %"PRIu64" bytes",
961  (uint64_t)(size * sizeof(RuleMatchCandidateTx)));
962  }
963  det_ctx->tx_candidates_size = size;
964  SCLogDebug("array initialized to %u elements (%"PRIu64" bytes)",
965  size, (uint64_t)(size * sizeof(RuleMatchCandidateTx)));
966 }
967 
969 {
970  SCFree(det_ctx->tx_candidates);
971  det_ctx->tx_candidates_size = 0;
972 }
973 
974 /* if size >= cur_space */
975 static inline bool RuleMatchCandidateTxArrayHasSpace(const DetectEngineThreadCtx *det_ctx,
976  const uint32_t need)
977 {
978  if (det_ctx->tx_candidates_size >= need)
979  return 1;
980  return 0;
981 }
982 
983 /* realloc */
984 static int RuleMatchCandidateTxArrayExpand(DetectEngineThreadCtx *det_ctx, const uint32_t needed)
985 {
986  const uint32_t old_size = det_ctx->tx_candidates_size;
987  uint32_t new_size = needed;
988  void *ptmp = SCRealloc(det_ctx->tx_candidates, (new_size * sizeof(RuleMatchCandidateTx)));
989  if (ptmp == NULL) {
990  FatalError(SC_ERR_MEM_ALLOC, "failed to expand to %"PRIu64" bytes",
991  (uint64_t)(new_size * sizeof(RuleMatchCandidateTx)));
992  // TODO can this be handled more gracefully?
993  }
994  det_ctx->tx_candidates = ptmp;
995  det_ctx->tx_candidates_size = new_size;
996  SCLogDebug("array expanded from %u to %u elements (%"PRIu64" bytes -> %"PRIu64" bytes)",
997  old_size, new_size, (uint64_t)(old_size * sizeof(RuleMatchCandidateTx)),
998  (uint64_t)(new_size * sizeof(RuleMatchCandidateTx))); (void)old_size;
999  return 1;
1000 }
1001 
1002 /** \internal
1003  * \brief sort helper for sorting match candidates by id: ascending
1004  *
1005  * The id field is set from Signature::num, so we sort the candidates to match the signature
1006  * sort order (ascending).
1007  *
1008  * \todo maybe let one with flags win if equal? */
1009 static int
1010 DetectRunTxSortHelper(const void *a, const void *b)
1011 {
1012  const RuleMatchCandidateTx *s0 = a;
1013  const RuleMatchCandidateTx *s1 = b;
1014  if (s1->id == s0->id)
1015  return 0;
1016  else
1017  return s0->id > s1->id ? 1 : -1;
1018 }
1019 
1020 #if 0
1021 #define TRACE_SID_TXS(sid,txs,...) \
1022  do { \
1023  char _trace_buf[2048]; \
1024  snprintf(_trace_buf, sizeof(_trace_buf), __VA_ARGS__); \
1025  SCLogNotice("%p/%"PRIu64"/%u: %s", txs->tx_ptr, txs->tx_id, sid, _trace_buf); \
1026  } while(0)
1027 #else
1028 #define TRACE_SID_TXS(sid,txs,...)
1029 #endif
1030 
1031 /** \internal
1032  * \brief inspect a rule against a transaction
1033  *
1034  * Inspect a rule. New detection or continued stateful
1035  * detection.
1036  *
1037  * \param stored_flags pointer to stored flags or NULL.
1038  * If stored_flags is set it means we're continueing
1039  * inspection from an earlier run.
1040  *
1041  * \retval bool true sig matched, false didn't match
1042  */
1043 static bool DetectRunTxInspectRule(ThreadVars *tv,
1045  DetectEngineThreadCtx *det_ctx,
1046  Packet *p,
1047  Flow *f,
1048  const uint8_t in_flow_flags, // direction, EOF, etc
1049  void *alstate,
1050  DetectTransaction *tx,
1051  const Signature *s,
1052  uint32_t *stored_flags,
1053  RuleMatchCandidateTx *can,
1054  DetectRunScratchpad *scratch)
1055 {
1056  uint8_t flow_flags = in_flow_flags;
1057  const int direction = (flow_flags & STREAM_TOSERVER) ? 0 : 1;
1058  uint32_t inspect_flags = stored_flags ? *stored_flags : 0;
1059  int total_matches = 0;
1060  int file_no_match = 0;
1061  bool retval = false;
1062  bool mpm_before_progress = false; // is mpm engine before progress?
1063  bool mpm_in_progress = false; // is mpm engine in a buffer we will revisit?
1064 
1065  /* see if we want to pass on the FLUSH flag */
1066  if ((s->flags & SIG_FLAG_FLUSH) == 0)
1067  flow_flags &=~ STREAM_FLUSH;
1068 
1069  TRACE_SID_TXS(s->id, tx, "starting %s", direction ? "toclient" : "toserver");
1070  TRACE_SID_TXS(s->id, tx, "FLUSH? %s", (flow_flags & STREAM_FLUSH)?"true":"false");
1071 
1072  /* for a new inspection we inspect pkt header and packet matches */
1073  if (likely(stored_flags == NULL)) {
1074  TRACE_SID_TXS(s->id, tx, "first inspect, run packet matches");
1075  if (DetectRunInspectRuleHeader(p, f, s, s->flags, s->proto.flags) == 0) {
1076  TRACE_SID_TXS(s->id, tx, "DetectRunInspectRuleHeader() no match");
1077  return false;
1078  }
1079  if (DetectEnginePktInspectionRun(tv, det_ctx, s, f, p, NULL) == false) {
1080  TRACE_SID_TXS(s->id, tx, "DetectEnginePktInspectionRun no match");
1081  return false;
1082  }
1083  /* stream mpm and negated mpm sigs can end up here with wrong proto */
1084  if (!(AppProtoEquals(s->alproto, f->alproto) || s->alproto == ALPROTO_UNKNOWN)) {
1085  TRACE_SID_TXS(s->id, tx, "alproto mismatch");
1086  return false;
1087  }
1088  }
1089 
1090  const DetectEngineAppInspectionEngine *engine = s->app_inspect;
1091  while (engine != NULL) { // TODO could be do {} while as s->app_inspect cannot be null
1092  TRACE_SID_TXS(s->id, tx, "engine %p inspect_flags %x", engine, inspect_flags);
1093  if (!(inspect_flags & BIT_U32(engine->id)) &&
1094  direction == engine->dir)
1095  {
1096  const bool skip_engine = (engine->alproto != 0 && engine->alproto != f->alproto);
1097  /* special case: file_data on 'alert tcp' will have engines
1098  * in the list that are not for us. */
1099  if (unlikely(skip_engine)) {
1100  engine = engine->next;
1101  continue;
1102  }
1103 
1104  /* engines are sorted per progress, except that the one with
1105  * mpm/prefilter enabled is first */
1106  if (tx->tx_progress < engine->progress) {
1107  SCLogDebug("tx progress %d < engine progress %d",
1108  tx->tx_progress, engine->progress);
1109  break;
1110  }
1111  if (engine->mpm) {
1112  if (tx->tx_progress > engine->progress) {
1113  TRACE_SID_TXS(s->id, tx,
1114  "engine->mpm: t->tx_progress %u > engine->progress %u, so set "
1115  "mpm_before_progress",
1116  tx->tx_progress, engine->progress);
1117  mpm_before_progress = true;
1118  } else if (tx->tx_progress == engine->progress) {
1119  TRACE_SID_TXS(s->id, tx,
1120  "engine->mpm: t->tx_progress %u == engine->progress %u, so set "
1121  "mpm_in_progress",
1122  tx->tx_progress, engine->progress);
1123  mpm_in_progress = true;
1124  }
1125  }
1126 
1127  /* run callback: but bypass stream callback if we can */
1128  int match;
1129  if (unlikely(engine->stream && can->stream_stored)) {
1130  match = can->stream_result;
1131  TRACE_SID_TXS(s->id, tx, "stream skipped, stored result %d used instead", match);
1132  } else {
1133  KEYWORD_PROFILING_SET_LIST(det_ctx, engine->sm_list);
1134  DEBUG_VALIDATE_BUG_ON(engine->v2.Callback == NULL);
1135  match = engine->v2.Callback(
1136  de_ctx, det_ctx, engine, s, f, flow_flags, alstate, tx->tx_ptr, tx->tx_id);
1137  TRACE_SID_TXS(s->id, tx, "engine %p match %d", engine, match);
1138  if (engine->stream) {
1139  can->stream_stored = true;
1140  can->stream_result = match;
1141  TRACE_SID_TXS(s->id, tx, "stream ran, store result %d for next tx (if any)", match);
1142  }
1143  }
1144  if (match == DETECT_ENGINE_INSPECT_SIG_MATCH) {
1145  inspect_flags |= BIT_U32(engine->id);
1146  engine = engine->next;
1147  total_matches++;
1148  continue;
1149  } else if (match == DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES) {
1150  /* if the file engine matched, but indicated more
1151  * files are still in progress, we don't set inspect
1152  * flags as these would end inspection for this tx */
1153  engine = engine->next;
1154  total_matches++;
1155  continue;
1156  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH) {
1157  inspect_flags |= DE_STATE_FLAG_SIG_CANT_MATCH;
1158  inspect_flags |= BIT_U32(engine->id);
1159  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES) {
1160  inspect_flags |= DE_STATE_FLAG_SIG_CANT_MATCH;
1161  inspect_flags |= BIT_U32(engine->id);
1162  file_no_match = 1;
1163  }
1164  /* implied DETECT_ENGINE_INSPECT_SIG_NO_MATCH */
1165  if (engine->mpm && mpm_before_progress) {
1166  inspect_flags |= DE_STATE_FLAG_SIG_CANT_MATCH;
1167  inspect_flags |= BIT_U32(engine->id);
1168  }
1169  break;
1170  }
1171  engine = engine->next;
1172  }
1173  TRACE_SID_TXS(s->id, tx, "inspect_flags %x, total_matches %u, engine %p",
1174  inspect_flags, total_matches, engine);
1175 
1176  if (engine == NULL && total_matches) {
1177  inspect_flags |= DE_STATE_FLAG_FULL_INSPECT;
1178  TRACE_SID_TXS(s->id, tx, "MATCH");
1179  retval = true;
1180  }
1181 
1182  if (stored_flags) {
1183  *stored_flags = inspect_flags;
1184  TRACE_SID_TXS(s->id, tx, "continue inspect flags %08x", inspect_flags);
1185  } else {
1186  // store... or? If tx is done we might not want to come back to this tx
1187 
1188  // also... if mpmid tracking is enabled, we won't do a sig again for this tx...
1189  TRACE_SID_TXS(s->id, tx, "start inspect flags %08x", inspect_flags);
1190  if (inspect_flags & DE_STATE_FLAG_SIG_CANT_MATCH) {
1191  if (file_no_match) {
1192  /* if we have a mismatch on a file sig, we need to keep state.
1193  * We may get another file on the same tx (for http and smtp
1194  * at least), so for a new file we need to re-eval the sig.
1195  * Thoughts / TODO:
1196  * - not for some protos that have 1 file per tx (e.g. nfs)
1197  * - maybe we only need this for file sigs that mix with
1198  * other matches? E.g. 'POST + filename', is different than
1199  * just 'filename'.
1200  */
1201  DetectRunStoreStateTx(scratch->sgh, f, tx->tx_ptr, tx->tx_id, s,
1202  inspect_flags, flow_flags, file_no_match);
1203  }
1204  } else if ((inspect_flags & DE_STATE_FLAG_FULL_INSPECT) && mpm_before_progress) {
1205  TRACE_SID_TXS(s->id, tx, "no need to store match sig, "
1206  "mpm won't trigger for it anymore");
1207 
1208  if (inspect_flags & DE_STATE_FLAG_FILE_INSPECT) {
1209  TRACE_SID_TXS(s->id, tx, "except that for new files, "
1210  "we may have to revisit anyway");
1211  DetectRunStoreStateTx(scratch->sgh, f, tx->tx_ptr, tx->tx_id, s,
1212  inspect_flags, flow_flags, file_no_match);
1213  }
1214  } else if ((inspect_flags & DE_STATE_FLAG_FULL_INSPECT) == 0 && mpm_in_progress) {
1215  TRACE_SID_TXS(s->id, tx, "no need to store no-match sig, "
1216  "mpm will revisit it");
1217  } else {
1218  TRACE_SID_TXS(s->id, tx, "storing state: flags %08x", inspect_flags);
1219  DetectRunStoreStateTx(scratch->sgh, f, tx->tx_ptr, tx->tx_id, s,
1220  inspect_flags, flow_flags, file_no_match);
1221  }
1222  }
1223 
1224  return retval;
1225 }
1226 
1227 /** \internal
1228  * \brief get a DetectTransaction object
1229  * \retval struct filled with relevant info or all nulls/0s
1230  */
1231 static DetectTransaction GetDetectTx(const uint8_t ipproto, const AppProto alproto,
1232  void *alstate, const uint64_t tx_id, void *tx_ptr, const int tx_end_state,
1233  const uint8_t flow_flags)
1234 {
1235  uint64_t detect_flags;
1236  AppLayerTxData *txd = AppLayerParserGetTxData(ipproto, alproto, tx_ptr);
1237  if (likely(txd != NULL)) {
1238  detect_flags = (flow_flags & STREAM_TOSERVER) ? txd->detect_flags_ts : txd->detect_flags_tc;
1239  } else {
1240  detect_flags = 0;
1241  }
1242  if (detect_flags & APP_LAYER_TX_INSPECTED_FLAG) {
1243  SCLogDebug("%"PRIu64" tx already fully inspected for %s. Flags %016"PRIx64,
1244  tx_id, flow_flags & STREAM_TOSERVER ? "toserver" : "toclient",
1245  detect_flags);
1246  DetectTransaction no_tx = { NULL, 0, NULL, NULL, 0, 0, 0, 0, 0, };
1247  return no_tx;
1248  }
1249 
1250  const int tx_progress = AppLayerParserGetStateProgress(ipproto, alproto, tx_ptr, flow_flags);
1251  const int dir_int = (flow_flags & STREAM_TOSERVER) ? 0 : 1;
1252  DetectEngineState *tx_de_state = txd->de_state;
1253  DetectEngineStateDirection *tx_dir_state = tx_de_state ? &tx_de_state->dir_state[dir_int] : NULL;
1254  uint64_t prefilter_flags = detect_flags & APP_LAYER_TX_PREFILTER_MASK;
1256 
1257  DetectTransaction tx = {
1258  .tx_ptr = tx_ptr,
1259  .tx_id = tx_id,
1260  .tx_data_ptr = (struct AppLayerTxData *)txd,
1261  .de_state = tx_dir_state,
1262  .detect_flags = detect_flags,
1263  .prefilter_flags = prefilter_flags,
1264  .prefilter_flags_orig = prefilter_flags,
1265  .tx_progress = tx_progress,
1266  .tx_end_state = tx_end_state,
1267  };
1268  return tx;
1269 }
1270 
1271 static inline void StoreDetectFlags(DetectTransaction *tx, const uint8_t flow_flags,
1272  const uint8_t ipproto, const AppProto alproto, const uint64_t detect_flags)
1273 {
1274  AppLayerTxData *txd = (AppLayerTxData *)tx->tx_data_ptr;
1275  if (likely(txd != NULL)) {
1276  if (flow_flags & STREAM_TOSERVER) {
1277  txd->detect_flags_ts = detect_flags;
1278  } else {
1279  txd->detect_flags_tc = detect_flags;
1280  }
1281  }
1282 }
1283 
1284 static void DetectRunTx(ThreadVars *tv,
1286  DetectEngineThreadCtx *det_ctx,
1287  Packet *p,
1288  Flow *f,
1289  DetectRunScratchpad *scratch)
1290 {
1291  const uint8_t flow_flags = scratch->flow_flags;
1292  const SigGroupHead * const sgh = scratch->sgh;
1293  void * const alstate = f->alstate;
1294  const uint8_t ipproto = f->proto;
1295  const AppProto alproto = f->alproto;
1296 
1297  const uint64_t total_txs = AppLayerParserGetTxCnt(f, alstate);
1298  uint64_t tx_id_min = AppLayerParserGetTransactionInspectId(f->alparser, flow_flags);
1299  const int tx_end_state = AppLayerParserGetStateProgressCompletionStatus(alproto, flow_flags);
1300 
1301  AppLayerGetTxIteratorFunc IterFunc = AppLayerGetTxIterator(ipproto, alproto);
1302  AppLayerGetTxIterState state;
1303  memset(&state, 0, sizeof(state));
1304 
1305  while (1) {
1306  AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, tx_id_min, total_txs, &state);
1307  if (ires.tx_ptr == NULL)
1308  break;
1309 
1310  DetectTransaction tx = GetDetectTx(ipproto, alproto,
1311  alstate, ires.tx_id, ires.tx_ptr, tx_end_state, flow_flags);
1312  if (tx.tx_ptr == NULL) {
1313  SCLogDebug("%p/%"PRIu64" no transaction to inspect",
1314  tx.tx_ptr, tx_id_min);
1315 
1316  tx_id_min++; // next (if any) run look for +1
1317  goto next;
1318  }
1319  tx_id_min = tx.tx_id + 1; // next look for cur + 1
1320 
1321  uint32_t array_idx = 0;
1322  uint32_t total_rules = det_ctx->match_array_cnt;
1323  total_rules += (tx.de_state ? tx.de_state->cnt : 0);
1324 
1325  /* run prefilter engines and merge results into a candidates array */
1326  if (sgh->tx_engines) {
1328  DetectRunPrefilterTx(det_ctx, sgh, p, ipproto, flow_flags, alproto,
1329  alstate, &tx);
1331  SCLogDebug("%p/%"PRIu64" rules added from prefilter: %u candidates",
1332  tx.tx_ptr, tx.tx_id, det_ctx->pmq.rule_id_array_cnt);
1333 
1334  total_rules += det_ctx->pmq.rule_id_array_cnt;
1335  if (!(RuleMatchCandidateTxArrayHasSpace(det_ctx, total_rules))) {
1336  RuleMatchCandidateTxArrayExpand(det_ctx, total_rules);
1337  }
1338 
1339  for (uint32_t i = 0; i < det_ctx->pmq.rule_id_array_cnt; i++) {
1340  const Signature *s = de_ctx->sig_array[det_ctx->pmq.rule_id_array[i]];
1341  const SigIntId id = s->num;
1342  det_ctx->tx_candidates[array_idx].s = s;
1343  det_ctx->tx_candidates[array_idx].id = id;
1344  det_ctx->tx_candidates[array_idx].flags = NULL;
1345  det_ctx->tx_candidates[array_idx].stream_reset = 0;
1346  array_idx++;
1347  }
1348  PMQ_RESET(&det_ctx->pmq);
1349  } else {
1350  if (!(RuleMatchCandidateTxArrayHasSpace(det_ctx, total_rules))) {
1351  RuleMatchCandidateTxArrayExpand(det_ctx, total_rules);
1352  }
1353  }
1354 
1355  /* merge 'state' rules from the regular prefilter */
1356  uint32_t x = array_idx;
1357  for (uint32_t i = 0; i < det_ctx->match_array_cnt; i++) {
1358  const Signature *s = det_ctx->match_array[i];
1359  if (s->app_inspect != NULL) {
1360  const SigIntId id = s->num;
1361  det_ctx->tx_candidates[array_idx].s = s;
1362  det_ctx->tx_candidates[array_idx].id = id;
1363  det_ctx->tx_candidates[array_idx].flags = NULL;
1364  det_ctx->tx_candidates[array_idx].stream_reset = 0;
1365  array_idx++;
1366 
1367  SCLogDebug("%p/%"PRIu64" rule %u (%u) added from 'match' list",
1368  tx.tx_ptr, tx.tx_id, s->id, id);
1369  }
1370  }
1371  SCLogDebug("%p/%"PRIu64" rules added from 'match' list: %u",
1372  tx.tx_ptr, tx.tx_id, array_idx - x); (void)x;
1373 
1374  /* merge stored state into results */
1375  if (tx.de_state != NULL) {
1376  const uint32_t old = array_idx;
1377 
1378  /* if tx.de_state->flags has 'new file' set and sig below has
1379  * 'file inspected' flag, reset the file part of the state */
1380  const bool have_new_file = (tx.de_state->flags & DETECT_ENGINE_STATE_FLAG_FILE_NEW);
1381  if (have_new_file) {
1382  SCLogDebug("%p/%"PRIu64" destate: need to consider new file",
1383  tx.tx_ptr, tx.tx_id);
1385  }
1386 
1387  SigIntId state_cnt = 0;
1388  DeStateStore *tx_store = tx.de_state->head;
1389  for (; tx_store != NULL; tx_store = tx_store->next) {
1390  SCLogDebug("tx_store %p", tx_store);
1391 
1392  SigIntId store_cnt = 0;
1393  for (store_cnt = 0;
1394  store_cnt < DE_STATE_CHUNK_SIZE && state_cnt < tx.de_state->cnt;
1395  store_cnt++, state_cnt++)
1396  {
1397  DeStateStoreItem *item = &tx_store->store[store_cnt];
1398  SCLogDebug("rule id %u, inspect_flags %u", item->sid, item->flags);
1399  if (have_new_file && (item->flags & DE_STATE_FLAG_FILE_INSPECT)) {
1400  /* remove part of the state. File inspect engine will now
1401  * be able to run again */
1403  SCLogDebug("rule id %u, post file reset inspect_flags %u", item->sid, item->flags);
1404  }
1405  det_ctx->tx_candidates[array_idx].s = de_ctx->sig_array[item->sid];
1406  det_ctx->tx_candidates[array_idx].id = item->sid;
1407  det_ctx->tx_candidates[array_idx].flags = &item->flags;
1408  det_ctx->tx_candidates[array_idx].stream_reset = 0;
1409  array_idx++;
1410  }
1411  }
1412  if (old && old != array_idx) {
1413  qsort(det_ctx->tx_candidates, array_idx, sizeof(RuleMatchCandidateTx),
1414  DetectRunTxSortHelper);
1415 
1416  SCLogDebug("%p/%"PRIu64" rules added from 'continue' list: %u",
1417  tx.tx_ptr, tx.tx_id, array_idx - old);
1418  }
1419  }
1420 #ifdef PROFILING
1421  if (array_idx >= de_ctx->profile_match_logging_threshold)
1422  RulesDumpTxMatchArray(det_ctx, scratch->sgh, p, tx.tx_id, array_idx, x);
1423 #endif
1424  det_ctx->tx_id = tx.tx_id;
1425  det_ctx->tx_id_set = true;
1426  det_ctx->p = p;
1427 
1428  /* run rules: inspect the match candidates */
1429  for (uint32_t i = 0; i < array_idx; i++) {
1430  RuleMatchCandidateTx *can = &det_ctx->tx_candidates[i];
1431  const Signature *s = det_ctx->tx_candidates[i].s;
1432  uint32_t *inspect_flags = det_ctx->tx_candidates[i].flags;
1433 
1434  /* deduplicate: rules_array is sorted, but not deduplicated:
1435  * both mpm and stored state could give us the same sid.
1436  * As they are back to back in that case we can check for it
1437  * here. We select the stored state one. */
1438  if ((i + 1) < array_idx) {
1439  if (det_ctx->tx_candidates[i].s == det_ctx->tx_candidates[i+1].s) {
1440  if (det_ctx->tx_candidates[i].flags != NULL) {
1441  i++;
1442  SCLogDebug("%p/%"PRIu64" inspecting SKIP NEXT: sid %u (%u), flags %08x",
1443  tx.tx_ptr, tx.tx_id, s->id, s->num, inspect_flags ? *inspect_flags : 0);
1444  } else if (det_ctx->tx_candidates[i+1].flags != NULL) {
1445  SCLogDebug("%p/%"PRIu64" inspecting SKIP CURRENT: sid %u (%u), flags %08x",
1446  tx.tx_ptr, tx.tx_id, s->id, s->num, inspect_flags ? *inspect_flags : 0);
1447  continue;
1448  } else {
1449  // if it's all the same, inspect the current one and skip next.
1450  i++;
1451  SCLogDebug("%p/%"PRIu64" inspecting SKIP NEXT: sid %u (%u), flags %08x",
1452  tx.tx_ptr, tx.tx_id, s->id, s->num, inspect_flags ? *inspect_flags : 0);
1453  }
1454  }
1455  }
1456 
1457  SCLogDebug("%p/%"PRIu64" inspecting: sid %u (%u), flags %08x",
1458  tx.tx_ptr, tx.tx_id, s->id, s->num, inspect_flags ? *inspect_flags : 0);
1459 
1460  if (inspect_flags) {
1462  SCLogDebug("%p/%"PRIu64" inspecting: sid %u (%u), flags %08x ALREADY COMPLETE",
1463  tx.tx_ptr, tx.tx_id, s->id, s->num, *inspect_flags);
1464  continue;
1465  }
1466  }
1467 
1468  if (inspect_flags) {
1469  /* continue previous inspection */
1470  SCLogDebug("%p/%"PRIu64" Continueing sid %u", tx.tx_ptr, tx.tx_id, s->id);
1471  } else {
1472  /* start new inspection */
1473  SCLogDebug("%p/%"PRIu64" Start sid %u", tx.tx_ptr, tx.tx_id, s->id);
1474  }
1475 
1476  /* call individual rule inspection */
1478  const int r = DetectRunTxInspectRule(tv, de_ctx, det_ctx, p, f, flow_flags,
1479  alstate, &tx, s, inspect_flags, can, scratch);
1480  if (r == 1) {
1481  /* match */
1482  DetectRunPostMatch(tv, det_ctx, p, s);
1483 
1484  const uint8_t alert_flags = (PACKET_ALERT_FLAG_STATE_MATCH | PACKET_ALERT_FLAG_TX);
1485  SCLogDebug("%p/%"PRIu64" sig %u (%u) matched", tx.tx_ptr, tx.tx_id, s->id, s->num);
1486  PacketAlertAppend(det_ctx, s, p, tx.tx_id, alert_flags);
1487  }
1488  DetectVarProcessList(det_ctx, p->flow, p);
1489  RULE_PROFILING_END(det_ctx, s, r, p);
1490  }
1491 
1492  det_ctx->tx_id = 0;
1493  det_ctx->tx_id_set = false;
1494  det_ctx->p = NULL;
1495 
1496  /* see if we have any updated state to store in the tx */
1497 
1498  uint64_t new_detect_flags = 0;
1499  /* this side of the tx is done */
1500  if (tx.tx_progress >= tx.tx_end_state) {
1501  new_detect_flags |= APP_LAYER_TX_INSPECTED_FLAG;
1502  SCLogDebug("%p/%"PRIu64" tx is done for direction %s. Flag %016"PRIx64,
1503  tx.tx_ptr, tx.tx_id,
1504  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient",
1505  new_detect_flags);
1506  }
1507  if (tx.prefilter_flags != tx.prefilter_flags_orig) {
1508  new_detect_flags |= tx.prefilter_flags;
1510  SCLogDebug("%p/%"PRIu64" updated prefilter flags %016"PRIx64" "
1511  "(was: %016"PRIx64") for direction %s. Flag %016"PRIx64,
1513  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient",
1514  new_detect_flags);
1515  }
1516  if (new_detect_flags != 0 &&
1517  (new_detect_flags | tx.detect_flags) != tx.detect_flags)
1518  {
1519  new_detect_flags |= tx.detect_flags;
1521  SCLogDebug("%p/%"PRIu64" Storing new flags %016"PRIx64" (was %016"PRIx64")",
1522  tx.tx_ptr, tx.tx_id, new_detect_flags, tx.detect_flags);
1523 
1524  StoreDetectFlags(&tx, flow_flags, ipproto, alproto, new_detect_flags);
1525  }
1526 next:
1527  InspectionBufferClean(det_ctx);
1528 
1529  if (!ires.has_next)
1530  break;
1531  }
1532 }
1533 
1534 static void DetectRunFrames(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
1535  Packet *p, Flow *f, DetectRunScratchpad *scratch)
1536 {
1537  const SigGroupHead *const sgh = scratch->sgh;
1538  const AppProto alproto = f->alproto;
1539 
1540  FramesContainer *frames_container = AppLayerFramesGetContainer(f);
1541  if (frames_container == NULL) {
1542  return;
1543  }
1544  Frames *frames;
1545  if (PKT_IS_TOSERVER(p)) {
1546  frames = &frames_container->toserver;
1547  } else {
1548  frames = &frames_container->toclient;
1549  }
1550 
1551  for (uint32_t idx = 0; idx < frames->cnt; idx++) {
1552  SCLogDebug("frame %u", idx);
1553  const Frame *frame = FrameGetByIndex(frames, idx);
1554  if (frame == NULL) {
1555  continue;
1556  }
1557 
1558  uint32_t array_idx = 0;
1559  uint32_t total_rules = det_ctx->match_array_cnt;
1560 
1561  /* run prefilter engines and merge results into a candidates array */
1562  if (sgh->frame_engines) {
1563  // PACKET_PROFILING_DETECT_START(p, PROF_DETECT_PF_TX);
1564  DetectRunPrefilterFrame(det_ctx, sgh, p, frames, frame, alproto, idx);
1565  // PACKET_PROFILING_DETECT_END(p, PROF_DETECT_PF_TX);
1566  SCLogDebug("%p/%" PRIi64 " rules added from prefilter: %u candidates", frame, frame->id,
1567  det_ctx->pmq.rule_id_array_cnt);
1568 
1569  total_rules += det_ctx->pmq.rule_id_array_cnt;
1570 
1571  if (!(RuleMatchCandidateTxArrayHasSpace(
1572  det_ctx, total_rules))) { // TODO is it safe to overload?
1573  RuleMatchCandidateTxArrayExpand(det_ctx, total_rules);
1574  }
1575 
1576  for (uint32_t i = 0; i < det_ctx->pmq.rule_id_array_cnt; i++) {
1577  const Signature *s = de_ctx->sig_array[det_ctx->pmq.rule_id_array[i]];
1578  const SigIntId id = s->num;
1579  det_ctx->tx_candidates[array_idx].s = s;
1580  det_ctx->tx_candidates[array_idx].id = id;
1581  det_ctx->tx_candidates[array_idx].flags = NULL;
1582  det_ctx->tx_candidates[array_idx].stream_reset = 0;
1583  array_idx++;
1584  }
1585  PMQ_RESET(&det_ctx->pmq);
1586  }
1587  /* merge 'state' rules from the regular prefilter */
1588  uint32_t x = array_idx;
1589  for (uint32_t i = 0; i < det_ctx->match_array_cnt; i++) {
1590  const Signature *s = det_ctx->match_array[i];
1591  if (s->frame_inspect != NULL) {
1592  const SigIntId id = s->num;
1593  det_ctx->tx_candidates[array_idx].s = s;
1594  det_ctx->tx_candidates[array_idx].id = id;
1595  det_ctx->tx_candidates[array_idx].flags = NULL;
1596  det_ctx->tx_candidates[array_idx].stream_reset = 0;
1597  array_idx++;
1598 
1599  SCLogDebug("%p/%" PRIi64 " rule %u (%u) added from 'match' list", frame, frame->id,
1600  s->id, id);
1601  }
1602  }
1603  SCLogDebug("%p/%" PRIi64 " rules added from 'match' list: %u", frame, frame->id,
1604  array_idx - x);
1605  (void)x;
1606 
1607  /* run rules: inspect the match candidates */
1608  for (uint32_t i = 0; i < array_idx; i++) {
1609  const Signature *s = det_ctx->tx_candidates[i].s;
1610 
1611  SCLogDebug("%p/%" PRIi64 " inspecting: sid %u (%u)", frame, frame->id, s->id, s->num);
1612 
1613  /* start new inspection */
1614  SCLogDebug("%p/%" PRIi64 " Start sid %u", frame, frame->id, s->id);
1615 
1616  /* call individual rule inspection */
1618  int r = DetectRunInspectRuleHeader(p, f, s, s->flags, s->proto.flags);
1619  if (r == 1) {
1620  r = DetectRunFrameInspectRule(tv, det_ctx, s, f, p, frames, frame, idx);
1621  if (r == 1) {
1622  /* match */
1623  DetectRunPostMatch(tv, det_ctx, p, s);
1624 
1625  const uint8_t alert_flags =
1627  // TODO set tx id if the frame has it
1629  det_ctx->frame_id = frame->id;
1630  SCLogDebug(
1631  "%p/%" PRIi64 " sig %u (%u) matched", frame, frame->id, s->id, s->num);
1632  PacketAlertAppend(det_ctx, s, p, 0, alert_flags); // TODO tx id frame field
1633  }
1634  }
1635  DetectVarProcessList(det_ctx, p->flow, p);
1636  RULE_PROFILING_END(det_ctx, s, r, p);
1637  }
1638  InspectionBufferClean(det_ctx);
1639  }
1640 }
1641 
1642 static DetectEngineThreadCtx *GetTenantById(HashTable *h, uint32_t id)
1643 {
1644  /* technically we need to pass a DetectEngineThreadCtx struct with the
1645  * tentant_id member. But as that member is the first in the struct, we
1646  * can use the id directly. */
1647  return HashTableLookup(h, &id, 0);
1648 }
1649 
1650 static void DetectFlow(ThreadVars *tv,
1652  Packet *p)
1653 {
1654  if (p->flags & PKT_NOPACKET_INSPECTION) {
1655  /* hack: if we are in pass the entire flow mode, we need to still
1656  * update the inspect_id forward. So test for the condition here,
1657  * and call the update code if necessary. */
1658  const int pass = ((p->flow->flags & FLOW_NOPACKET_INSPECTION));
1659  if (pass) {
1660  uint8_t flags;
1661  if (p->flowflags & FLOW_PKT_TOSERVER) {
1662  flags = STREAM_TOSERVER;
1663  } else {
1664  flags = STREAM_TOCLIENT;
1665  }
1668  }
1669  SCLogDebug("p->pcap %"PRIu64": no detection on packet, "
1670  "PKT_NOPACKET_INSPECTION is set", p->pcap_cnt);
1671  return;
1672  }
1673 
1674  /* if flow is set to drop, we enforce that here */
1675  if (p->flow->flags & FLOW_ACTION_DROP) {
1676  PACKET_DROP(p);
1677  SCReturn;
1678  }
1679 
1680  /* see if the packet matches one or more of the sigs */
1681  (void)DetectRun(tv, de_ctx, det_ctx, p);
1682 }
1683 
1684 
1685 static void DetectNoFlow(ThreadVars *tv,
1687  Packet *p)
1688 {
1689  /* No need to perform any detection on this packet, if the the given flag is set.*/
1690  if ((p->flags & PKT_NOPACKET_INSPECTION) || (PacketTestAction(p, ACTION_DROP))) {
1691  return;
1692  }
1693 
1694  /* see if the packet matches one or more of the sigs */
1695  DetectRun(tv, de_ctx, det_ctx, p);
1696  return;
1697 }
1698 
1699 /** \brief Detection engine thread wrapper.
1700  * \param tv thread vars
1701  * \param p packet to inspect
1702  * \param data thread specific data
1703  * \param pq packet queue
1704  * \retval TM_ECODE_FAILED error
1705  * \retval TM_ECODE_OK ok
1706  */
1707 TmEcode Detect(ThreadVars *tv, Packet *p, void *data)
1708 {
1710 
1711  DetectEngineCtx *de_ctx = NULL;
1712  DetectEngineThreadCtx *det_ctx = (DetectEngineThreadCtx *)data;
1713  if (det_ctx == NULL) {
1714  printf("ERROR: Detect has no thread ctx\n");
1715  goto error;
1716  }
1717 
1718  if (unlikely(SC_ATOMIC_GET(det_ctx->so_far_used_by_detect) == 0)) {
1719  (void)SC_ATOMIC_SET(det_ctx->so_far_used_by_detect, 1);
1720  SCLogDebug("Detect Engine using new det_ctx - %p",
1721  det_ctx);
1722  }
1723 
1724  /* if in MT mode _and_ we have tenants registered, use
1725  * MT logic. */
1726  if (det_ctx->mt_det_ctxs_cnt > 0 && det_ctx->TenantGetId != NULL)
1727  {
1728  uint32_t tenant_id = p->tenant_id;
1729  if (tenant_id == 0)
1730  tenant_id = det_ctx->TenantGetId(det_ctx, p);
1731  if (tenant_id > 0 && tenant_id < det_ctx->mt_det_ctxs_cnt) {
1732  p->tenant_id = tenant_id;
1733  det_ctx = GetTenantById(det_ctx->mt_det_ctxs_hash, tenant_id);
1734  if (det_ctx == NULL)
1735  return TM_ECODE_OK;
1736  de_ctx = det_ctx->de_ctx;
1737  if (de_ctx == NULL)
1738  return TM_ECODE_OK;
1739 
1740  if (unlikely(SC_ATOMIC_GET(det_ctx->so_far_used_by_detect) == 0)) {
1741  (void)SC_ATOMIC_SET(det_ctx->so_far_used_by_detect, 1);
1742  SCLogDebug("MT de_ctx %p det_ctx %p (tenant %u)", de_ctx, det_ctx, tenant_id);
1743  }
1744  } else {
1745  /* use default if no tenants are registered for this packet */
1746  de_ctx = det_ctx->de_ctx;
1747  }
1748  } else {
1749  de_ctx = det_ctx->de_ctx;
1750  }
1751 
1752  if (p->flow) {
1753  DetectFlow(tv, de_ctx, det_ctx, p);
1754  } else {
1755  DetectNoFlow(tv, de_ctx, det_ctx, p);
1756  }
1757  return TM_ECODE_OK;
1758 error:
1759  return TM_ECODE_FAILED;
1760 }
1761 
1762 /** \brief disable file features we don't need
1763  * Called if we have no detection engine.
1764  */
1766 {
1767  DetectPostInspectFileFlagsUpdate(f, NULL /* no sgh */, STREAM_TOSERVER);
1768  DetectPostInspectFileFlagsUpdate(f, NULL /* no sgh */, STREAM_TOCLIENT);
1769 }
1770 
1771 #ifdef UNITTESTS
1772 /**
1773  * \brief wrapper for old tests
1774  */
1777 {
1778  if (p->flow) {
1779  DetectFlow(tv, de_ctx, det_ctx, p);
1780  } else {
1781  DetectNoFlow(tv, de_ctx, det_ctx, p);
1782  }
1783 }
1784 #endif
1785 
1786 /*
1787  * TESTS
1788  */
1789 
1790 #ifdef UNITTESTS
1791 #include "tests/detect.c"
1792 #endif
1793 
DetectEngineAppInspectionEngine_::stream
bool stream
Definition: detect.h:403
SigGroupHead_::non_pf_syn_store_cnt
uint32_t non_pf_syn_store_cnt
Definition: detect.h:1413
FLOWFILE_NO_MD5_TS
#define FLOWFILE_NO_MD5_TS
Definition: flow.h:129
DetectEngineThreadCtx_::non_pf_store_ptr
SignatureNonPrefilterStore * non_pf_store_ptr
Definition: detect.h:1140
RulesDumpTxMatchArray
void RulesDumpTxMatchArray(const DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, const Packet *p, const uint64_t tx_id, const uint32_t rule_cnt, const uint32_t pkt_prefilter_cnt)
Definition: detect-engine-profile.c:38
SIG_GROUP_HEAD_HAVEFILEMD5
#define SIG_GROUP_HEAD_HAVEFILEMD5
Definition: detect.h:1283
FLOWFILE_NO_MD5_TC
#define FLOWFILE_NO_MD5_TC
Definition: flow.h:130
SigGroupHead_::tx_engines
PrefilterEngine * tx_engines
Definition: detect.h:1426
DetectEngineAppInspectionEngine_
Definition: detect.h:398
Packet_::proto
uint8_t proto
Definition: decode.h:442
DetectTransaction_::tx_data_ptr
struct AppLayerTxData * tx_data_ptr
Definition: detect-engine-state.h:101
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:402
RULE_PROFILING_END
#define RULE_PROFILING_END(ctx, r, m, p)
Definition: util-profiling.h:54
DE_STATE_CHUNK_SIZE
#define DE_STATE_CHUNK_SIZE
Definition: detect-engine-state.h:53
RuleMatchCandidateTx::stream_stored
bool stream_stored
Definition: detect.h:1038
FLOWFILE_NO_SIZE_TS
#define FLOWFILE_NO_SIZE_TS
Definition: flow.h:141
PROF_DETECT_GETSGH
@ PROF_DETECT_GETSGH
Definition: suricata-common.h:418
FLOWFILE_NO_SIZE_TC
#define FLOWFILE_NO_SIZE_TC
Definition: flow.h:142
detect-engine.h
detect-engine-proto.h
DetectEngineThreadCtx_::match_array_cnt
SigIntId match_array_cnt
Definition: detect.h:1135
DetectEngineStateDirection_::flags
uint8_t flags
Definition: detect-engine-state.h:89
detect-dsize.h
Signature_::addr_src_match6
DetectMatchAddressIPv6 * addr_src_match6
Definition: detect.h:578
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1161
ALPROTO_DCERPC
@ ALPROTO_DCERPC
Definition: app-layer-protos.h:38
PACKET_ALERT_FLAG_TX
#define PACKET_ALERT_FLAG_TX
Definition: decode.h:296
DetectEngineCtx_::decoder_event_sgh
struct SigGroupHead_ * decoder_event_sgh
Definition: detect.h:900
FlowGetPacketDirection
int FlowGetPacketDirection(const Flow *f, const Packet *p)
determine the direction of the packet compared to the flow
Definition: flow.c:292
DetectEngineCtx_::flow_gh
DetectEngineLookupFlow flow_gh[FLOW_STATES]
Definition: detect.h:835
DE_STATE_FLAG_SIG_CANT_MATCH
#define DE_STATE_FLAG_SIG_CANT_MATCH
Definition: detect-engine-state.h:57
DetectEngineThreadCtx_::counter_match_list
uint16_t counter_match_list
Definition: detect.h:1091
DetectEngineAppInspectionEngine_::next
struct DetectEngineAppInspectionEngine_ * next
Definition: detect.h:417
DETECT_PROTO_IPV6
#define DETECT_PROTO_IPV6
Definition: detect-engine-proto.h:34
detect-engine-siggroup.h
IPOnlyMatchPacket
void IPOnlyMatchPacket(ThreadVars *tv, const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineIPOnlyCtx *io_ctx, DetectEngineIPOnlyThreadCtx *io_tctx, Packet *p)
Match a packet against the IP Only detection engine contexts.
Definition: detect-engine-iponly.c:1000
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition: decode.h:263
DetectEngineState_
Definition: detect-engine-state.h:93
Signature_::num
SigIntId num
Definition: detect.h:557
stream-tcp.h
Prefilter
void Prefilter(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p, const uint8_t flags)
Definition: detect-engine-prefilter.c:143
PrefilterRuleStore_::rule_id_array_cnt
uint32_t rule_id_array_cnt
Definition: util-prefilter.h:38
detect-engine-event.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1407
SIG_GROUP_HEAD_HAVEFILESIZE
#define SIG_GROUP_HEAD_HAVEFILESIZE
Definition: detect.h:1284
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
detect.c
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:387
KEYWORD_PROFILING_SET_LIST
#define KEYWORD_PROFILING_SET_LIST(ctx, list)
Definition: util-profiling.h:65
DetectAddressMatchIPv4
int DetectAddressMatchIPv4(const DetectMatchAddressIPv4 *addrs, uint16_t addrs_cnt, const Address *a)
Match a packets address against a signatures addrs array.
Definition: detect-engine-address.c:1638
Signature_::alproto
AppProto alproto
Definition: detect.h:550
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
SignatureNonPrefilterStore_::id
SigIntId id
Definition: detect.h:1027
FLOW_SGH_TOCLIENT
#define FLOW_SGH_TOCLIENT
Definition: flow.h:73
Packet_::pcap_cnt
uint64_t pcap_cnt
Definition: decode.h:581
DetectEngineStateDirection_::cnt
SigIntId cnt
Definition: detect-engine-state.h:87
AppLayerGetTxIterator
AppLayerGetTxIteratorFunc AppLayerGetTxIterator(const uint8_t ipproto, const AppProto alproto)
Definition: app-layer-parser.c:709
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
Flow_::proto
uint8_t proto
Definition: flow.h:375
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:78
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:305
AppLayerParserGetStateProgress
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *alstate, uint8_t flags)
get the progress value for a tx/protocol
Definition: app-layer-parser.c:1103
DetectEngineThreadCtx_::tx_id
uint64_t tx_id
Definition: detect.h:1119
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:333
FLOWFILE_NO_MAGIC_TS
#define FLOWFILE_NO_MAGIC_TS
Definition: flow.h:122
FramesContainer::toserver
Frames toserver
Definition: app-layer-frames.h:77
Packet_::flags
uint32_t flags
Definition: decode.h:455
ICMPV4_DEST_UNREACH_IS_VALID
#define ICMPV4_DEST_UNREACH_IS_VALID(p)
Definition: decode-icmpv4.h:269
Frame
Definition: app-layer-frames.h:45
Flow_
Flow data structure.
Definition: flow.h:353
DetectTransaction_::tx_end_state
const int tx_end_state
Definition: detect-engine-state.h:107
PROF_DETECT_ALERT
@ PROF_DETECT_ALERT
Definition: suricata-common.h:429
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1149
SIG_GROUP_HEAD_HAVEFILESHA1
#define SIG_GROUP_HEAD_HAVEFILESHA1
Definition: detect.h:1285
DetectRunPrefilterFrame
void DetectRunPrefilterFrame(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p, const Frames *frames, const Frame *frame, const AppProto alproto, const uint32_t idx)
Definition: detect-engine-frame.c:43
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:801
AppLayerParserGetStateProgressCompletionStatus
int AppLayerParserGetStateProgressCompletionStatus(AppProto alproto, uint8_t direction)
Definition: app-layer-parser.c:1131
DetectRunScratchpad
struct DetectRunScratchpad DetectRunScratchpad
PROF_DETECT_PF_TX
@ PROF_DETECT_PF_TX
Definition: suricata-common.h:424
Frames::cnt
uint16_t cnt
Definition: app-layer-frames.h:62
Frame::id
int64_t id
Definition: app-layer-frames.h:54
DetectEngineThreadCtx_::p
Packet * p
Definition: detect.h:1121
DetectEngineState_::dir_state
DetectEngineStateDirection dir_state[2]
Definition: detect-engine-state.h:94
FrameGetByIndex
Frame * FrameGetByIndex(Frames *frames, const uint32_t idx)
Definition: app-layer-frames.c:63
StreamReassembleRawUpdateProgress
void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, uint64_t progress)
update stream engine after detection
Definition: stream-tcp-reassemble.c:1384
RuleMatchCandidateTx::id
SigIntId id
Definition: detect.h:1034
PROF_DETECT_CLEANUP
@ PROF_DETECT_CLEANUP
Definition: suricata-common.h:431
SIG_FLAG_DST_ANY
#define SIG_FLAG_DST_ANY
Definition: detect.h:212
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:225
HashTable_
Definition: util-hash.h:35
DetectEngineThreadCtx_::flags
uint16_t flags
Definition: detect.h:1114
Frames
Definition: app-layer-frames.h:61
FramesContainer
Definition: app-layer-frames.h:76
APP_LAYER_TX_INSPECTED_FLAG
#define APP_LAYER_TX_INSPECTED_FLAG
Definition: app-layer-parser.h:80
DetectRunScratchpad
Definition: detect.c:63
SIG_GROUP_HEAD_HAVERAWSTREAM
#define SIG_GROUP_HEAD_HAVERAWSTREAM
Definition: detect.h:1279
FLOW_ACTION_DROP
#define FLOW_ACTION_DROP
Definition: flow.h:68
TcpStream_::flags
uint16_t flags
Definition: stream-tcp-private.h:95
FLOWFILE_NONE
#define FLOWFILE_NONE
Definition: flow.h:156
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1775
Signature_::sm_arrays
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:601
proto
uint8_t proto
Definition: decode-template.h:0
m
SCMutex m
Definition: flow-hash.h:6
DetectPort_::sh
struct SigGroupHead_ * sh
Definition: detect.h:202
PACKET_PROFILING_DETECT_END
#define PACKET_PROFILING_DETECT_END(p, id)
Definition: util-profiling.h:241
Detect
TmEcode Detect(ThreadVars *tv, Packet *p, void *data)
Detection engine thread wrapper.
Definition: detect.c:1707
FLOW_PKT_TOCLIENT_IPONLY_SET
#define FLOW_PKT_TOCLIENT_IPONLY_SET
Definition: flow.h:229
detect-engine-payload.h
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:451
SIG_FLAG_SRC_ANY
#define SIG_FLAG_SRC_ANY
Definition: detect.h:211
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
Flow_::protoctx
void * protoctx
Definition: flow.h:451
SigMatchData_
Data needed for Match()
Definition: detect.h:330
DeStateStoreItem_::sid
SigIntId sid
Definition: detect-engine-state.h:75
RuleMatchCandidateTx::s
const Signature * s
Definition: detect.h:1044
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:69
DetectEngineThreadCtx_::counter_fnonmpm_list
uint16_t counter_fnonmpm_list
Definition: detect.h:1090
SigMatchData_::type
uint16_t type
Definition: detect.h:331
DetectEngineCtx_::version
uint32_t version
Definition: detect.h:896
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@84 v2
DisableDetectFlowFileFlags
void DisableDetectFlowFileFlags(Flow *f)
disable file features we don't need Called if we have no detection engine.
Definition: detect.c:1765
AppLayerParserGetTransactionInspectId
uint64_t AppLayerParserGetTransactionInspectId(AppLayerParserState *pstate, uint8_t direction)
Definition: app-layer-parser.c:734
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:575
DetectTransaction_::tx_progress
const int tx_progress
Definition: detect-engine-state.h:106
DetectEngineThreadCtx_::counter_nonmpm_list
uint16_t counter_nonmpm_list
Definition: detect.h:1089
detect-engine-prefilter.h
Packet_::events
PacketEngineEvents events
Definition: decode.h:585
Signature_::frame_inspect
DetectEngineFrameInspectionEngine * frame_inspect
Definition: detect.h:597
DetectTransaction_::prefilter_flags_orig
const uint64_t prefilter_flags_orig
Definition: detect-engine-state.h:105
PROF_DETECT_PF_SORT2
@ PROF_DETECT_PF_SORT2
Definition: suricata-common.h:427
pad
uint16_t pad
Definition: source-erf-file.c:60
TcpSession_::flags
uint16_t flags
Definition: stream-tcp-private.h:269
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:82
PacketCreateMask
void PacketCreateMask(Packet *p, SignatureMask *mask, AppProto alproto, bool app_decoder_events)
Definition: detect-engine-build.c:397
SIG_FLAG_APPLAYER
#define SIG_FLAG_APPLAYER
Definition: detect.h:218
KEYWORD_PROFILING_END
#define KEYWORD_PROFILING_END(ctx, type, m)
Definition: util-profiling.h:83
detect-flowvar.h
DetectEngineThreadCtx_::mt_det_ctxs_hash
HashTable * mt_det_ctxs_hash
Definition: detect.h:1065
DetectEnginePktInspectionRun
bool DetectEnginePktInspectionRun(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, const Signature *s, Flow *f, Packet *p, uint8_t *alert_flags)
Definition: detect-engine.c:1806
Flow_::sgh_toserver
const struct SigGroupHead_ * sgh_toserver
Definition: flow.h:493
PROF_DETECT_TX_UPDATE
@ PROF_DETECT_TX_UPDATE
Definition: suricata-common.h:430
DetectEngineAppInspectionEngine_::id
uint8_t id
Definition: detect.h:401
DetectRunStoreStateTx
void DetectRunStoreStateTx(const SigGroupHead *sgh, Flow *f, void *tx, uint64_t tx_id, const Signature *s, uint32_t inspect_flags, uint8_t flow_flags, const uint16_t file_no_match)
Definition: detect-engine-state.c:218
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:404
FLOWFILE_INIT
#define FLOWFILE_INIT
Definition: flow.h:119
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:485
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
Definition: detect-engine-state.h:44
FLOWFILE_NO_SHA1_TC
#define FLOWFILE_NO_SHA1_TC
Definition: flow.h:134
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:98
FramesContainer::toclient
Frames toclient
Definition: app-layer-frames.h:78
DetectEngineThreadCtx_::tx_candidates
RuleMatchCandidateTx * tx_candidates
Definition: detect.h:1137
Signature_::addr_src_match4
DetectMatchAddressIPv4 * addr_src_match4
Definition: detect.h:575
decode.h
TOSERVER
#define TOSERVER
Definition: flow.h:45
DETECT_ENGINE_THREAD_CTX_FRAME_ID_SET
#define DETECT_ENGINE_THREAD_CTX_FRAME_ID_SET
Definition: detect.h:282
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
PKT_IS_TOSERVER
#define PKT_IS_TOSERVER(p)
Definition: decode.h:268
DetectEngineThreadCtx_
Definition: detect.h:1050
DEBUG_VALIDATE_PACKET
#define DEBUG_VALIDATE_PACKET(p)
Definition: util-validate.h:110
DeStateStoreItem_::flags
uint32_t flags
Definition: detect-engine-state.h:74
PKT_STREAM_ADD
#define PKT_STREAM_ADD
Definition: decode.h:1156
DeStateUpdateInspectTransactionId
void DeStateUpdateInspectTransactionId(Flow *f, const uint8_t flags, const bool tag_txs_as_inspected)
update flow's inspection id's
Definition: detect-engine-state.c:251
FLOWFILE_NO_STORE_TS
#define FLOWFILE_NO_STORE_TS
Definition: flow.h:126
BIT_U32
#define BIT_U32(n)
Definition: suricata-common.h:381
DetectEngineThreadCtx_::tx_candidates_size
uint32_t tx_candidates_size
Definition: detect.h:1138
FLOW_PKT_TOSERVER_IPONLY_SET
#define FLOW_PKT_TOSERVER_IPONLY_SET
Definition: flow.h:228
SIG_FLAG_FLUSH
#define SIG_FLAG_FLUSH
Definition: detect.h:228
PKT_IS_FRAGMENT
#define PKT_IS_FRAGMENT
Definition: decode.h:1185
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
PMQ_RESET
#define PMQ_RESET(pmq)
Definition: util-prefilter.h:44
detect-engine-mpm.h
DetectEngineLookupFlow_::sgh
struct SigGroupHead_ * sgh[256]
Definition: detect.h:740
HashTableLookup
void * HashTableLookup(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:193
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DeStateStore_::next
struct DeStateStore_ * next
Definition: detect-engine-state.h:80
Packet_::sp
Port sp
Definition: decode.h:427
FLOWFILE_NO_SHA256_TS
#define FLOWFILE_NO_SHA256_TS
Definition: flow.h:137
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:39
PKT_DETECT_HAS_STREAMDATA
#define PKT_DETECT_HAS_STREAMDATA
Definition: decode.h:1200
StreamReassembleRawHasDataReady
bool StreamReassembleRawHasDataReady(TcpSession *ssn, Packet *p)
does the stream engine have data to inspect?
Definition: stream-tcp-reassemble.c:1340
detect-engine-port.h
DetectEngineThreadCtx_::non_pf_id_cnt
uint32_t non_pf_id_cnt
Definition: detect.h:1061
DetectPort_
Port structure for detection engine.
Definition: detect.h:191
app-layer-parser.h
Signature_::app_inspect
DetectEngineAppInspectionEngine * app_inspect
Definition: detect.h:595
util-detect.h
detect-engine-profile.h
Flow_::sgh_toclient
const struct SigGroupHead_ * sgh_toclient
Definition: flow.h:490
DetectEngineThreadCtx_::base64_decoded_len
int base64_decoded_len
Definition: detect.h:1183
SIG_FLAG_REQUIRE_FLOWVAR
#define SIG_FLAG_REQUIRE_FLOWVAR
Definition: detect.h:232
RuleMatchCandidateTx::stream_result
uint8_t stream_result
Definition: detect.h:1039
DetectTransaction_::tx_id
const uint64_t tx_id
Definition: detect-engine-state.h:100
DetectEngineLookupFlow_::udp
DetectPort * udp
Definition: detect.h:739
DetectEngineThreadCtx_::raw_stream_progress
uint64_t raw_stream_progress
Definition: detect.h:1074
FileUpdateFlowFileFlags
void FileUpdateFlowFileFlags(Flow *f, uint16_t set_file_flags, uint8_t direction)
set a flow's file flags
Definition: util-file.c:1049
SCReturn
#define SCReturn
Definition: util-debug.h:302
Signature_::flags
uint32_t flags
Definition: detect.h:547
RuleMatchCandidateTxArrayFree
void RuleMatchCandidateTxArrayFree(DetectEngineThreadCtx *det_ctx)
Definition: detect.c:968
AppLayerGetTxIterState
Definition: app-layer-parser.h:143
DetectTransaction_::prefilter_flags
uint64_t prefilter_flags
Definition: detect-engine-state.h:104
Packet_
Definition: decode.h:420
DetectRunFrameInspectRule
int DetectRunFrameInspectRule(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, const Signature *s, Flow *f, Packet *p, const Frames *frames, const Frame *frame, const uint32_t idx)
Definition: detect-engine-frame.c:132
AppLayerFramesGetContainer
FramesContainer * AppLayerFramesGetContainer(Flow *f)
Definition: app-layer-parser.c:189
DetectEngineThreadCtx_::frame_id
int64_t frame_id
Definition: detect.h:1120
DetectEngineAppInspectionEngine_::Callback
InspectEngineFuncPtr2 Callback
Definition: detect.h:410
APP_LAYER_TX_PREFILTER_MASK
#define APP_LAYER_TX_PREFILTER_MASK
Definition: app-layer-parser.h:83
detect-engine-alert.h
conf.h
DetectRunScratchpad::pkt_mask
SignatureMask pkt_mask
Definition: detect.c:68
FlowSetIPOnlyFlag
void FlowSetIPOnlyFlag(Flow *f, int direction)
Set the IPOnly scanned flag for 'direction'.
Definition: flow.c:158
FLOW_TOSERVER_IPONLY_SET
#define FLOW_TOSERVER_IPONLY_SET
Definition: flow.h:58
DetectEngineThreadCtx_::filestore_cnt
uint16_t filestore_cnt
Definition: detect.h:1083
PROF_DETECT_IPONLY
@ PROF_DETECT_IPONLY
Definition: suricata-common.h:419
TmEcode
TmEcode
Definition: tm-threads-common.h:81
RULE_PROFILING_START
#define RULE_PROFILING_START(p)
Definition: util-profiling.h:42
PKT_STREAM_EOF
#define PKT_STREAM_EOF
Definition: decode.h:1160
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:316
DetectTransaction_::de_state
DetectEngineStateDirection * de_state
Definition: detect-engine-state.h:102
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
detect-filestore.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1217
detect-replace.h
RulesDumpMatchArray
void RulesDumpMatchArray(const DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, const Packet *p)
Definition: detect-engine-profile.c:83
Signature_::addr_dst_match6_cnt
uint16_t addr_dst_match6_cnt
Definition: detect.h:572
FLOW_PKT_TOCLIENT
#define FLOW_PKT_TOCLIENT
Definition: flow.h:226
SigGroupHead_::frame_engines
PrefilterEngine * frame_engines
Definition: detect.h:1427
Signature_::sp
DetectPort * sp
Definition: detect.h:586
DetectRunScratchpad::sgh
const SigGroupHead * sgh
Definition: detect.c:67
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
Definition: detect-engine-state.h:40
DETECT_ENGINE_STATE_FLAG_FILE_NEW
#define DETECT_ENGINE_STATE_FLAG_FILE_NEW
Definition: detect-engine-state.h:71
Flow_::flowvar
GenericVar * flowvar
Definition: flow.h:496
RuleMatchCandidateTx::flags
uint32_t * flags
Definition: detect.h:1035
DetectEngineAppInspectionEngine_::alproto
AppProto alproto
Definition: detect.h:399
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
detect-engine-analyzer.h
FLOWFILE_NO_SHA256_TC
#define FLOWFILE_NO_SHA256_TC
Definition: flow.h:138
DetectEngineStateDirection_::head
DeStateStore * head
Definition: detect-engine-state.h:84
DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
#define DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
Definition: detect-engine-state.h:50
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:38
DETECT_PROTO_IPV4
#define DETECT_PROTO_IPV4
Definition: detect-engine-proto.h:33
DetectAddressMatchIPv6
int DetectAddressMatchIPv6(const DetectMatchAddressIPv6 *addrs, uint16_t addrs_cnt, const Address *a)
Match a packets address against a signatures addrs array.
Definition: detect-engine-address.c:1671
Packet_::tenant_id
uint32_t tenant_id
Definition: decode.h:614
Packet_::flow
struct Flow_ * flow
Definition: decode.h:457
DetectEngineStateResetTxs
void DetectEngineStateResetTxs(Flow *f)
Reset de state for active tx' To be used on detect engine reload.
Definition: detect-engine-state.c:283
FLOWFILE_NO_MAGIC_TC
#define FLOWFILE_NO_MAGIC_TC
Definition: flow.h:123
TH_SYN
#define TH_SYN
Definition: decode-tcp.h:35
flags
uint8_t flags
Definition: decode-gre.h:0
Signature_::proto
DetectProto proto
Definition: detect.h:564
SigGroupHead_::non_pf_other_store_array
SignatureNonPrefilterStore * non_pf_other_store_array
Definition: detect.h:1414
FLOW_TOCLIENT_IPONLY_SET
#define FLOW_TOCLIENT_IPONLY_SET
Definition: flow.h:60
SigMatchData_::is_last
uint8_t is_last
Definition: detect.h:332
suricata-common.h
SIG_FLAG_SP_ANY
#define SIG_FLAG_SP_ANY
Definition: detect.h:213
APP_LAYER_TX_RESERVED_FLAGS
#define APP_LAYER_TX_RESERVED_FLAGS
Definition: app-layer-parser.h:70
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:540
SigGroupHead_::non_pf_other_store_cnt
uint32_t non_pf_other_store_cnt
Definition: detect.h:1412
DeStateStore_
Definition: detect-engine-state.h:78
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
DetectTransaction_
Definition: detect-engine-state.h:98
PROF_DETECT_SETUP
@ PROF_DETECT_SETUP
Definition: suricata-common.h:417
DetectEngineStateDirection_
Definition: detect-engine-state.h:83
DetectEngineCtx_::profile_match_logging_threshold
uint32_t profile_match_logging_threshold
Definition: detect.h:929
AppLayerParserGetTxData
AppLayerTxData * AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx)
Definition: app-layer-parser.c:1196
FatalError
#define FatalError(x,...)
Definition: util-debug.h:532
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:272
FLOWFILE_NO_STORE_TC
#define FLOWFILE_NO_STORE_TC
Definition: flow.h:127
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
DetectEngineThreadCtx_::non_pf_id_array
SigIntId * non_pf_id_array
Definition: detect.h:1060
DetectEngineAppInspectionEngine_::progress
int16_t progress
Definition: detect.h:406
PKT_IS_ICMPV4
#define PKT_IS_ICMPV4(p)
Definition: decode.h:266
util-validate.h
FlowGetAppProtocol
AppProto FlowGetAppProtocol(const Flow *f)
Definition: flow.c:1104
DetectTransaction_::tx_ptr
void * tx_ptr
Definition: detect-engine-state.h:99
Signature_::addr_src_match6_cnt
uint16_t addr_src_match6_cnt
Definition: detect.h:573
StatsAddUI64
void StatsAddUI64(ThreadVars *tv, uint16_t id, uint64_t x)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:148
FLOWFILE_NO_SHA1_TS
#define FLOWFILE_NO_SHA1_TS
Definition: flow.h:133
PROF_DETECT_RULES
@ PROF_DETECT_RULES
Definition: suricata-common.h:420
DetectRunScratchpad::app_decoder_events
const bool app_decoder_events
Definition: detect.c:66
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:271
PROF_DETECT_NONMPMLIST
@ PROF_DETECT_NONMPMLIST
Definition: suricata-common.h:428
Signature_::dp
DetectPort * dp
Definition: detect.h:586
PACKET_PROFILING_DETECT_START
#define PACKET_PROFILING_DETECT_START(p, id)
Definition: util-profiling.h:234
PACKET_ALERT_FLAG_FRAME
#define PACKET_ALERT_FLAG_FRAME
Definition: decode.h:300
DetectEngineThreadCtx_::non_pf_store_cnt
uint32_t non_pf_store_cnt
Definition: detect.h:1141
FLOW_SGH_TOSERVER
#define FLOW_SGH_TOSERVER
Definition: flow.h:71
DetectEngineThreadCtx_::counter_alerts
uint16_t counter_alerts
Definition: detect.h:1086
SigGroupHead_::flags
uint32_t flags
Definition: detect.h:1408
SCFree
#define SCFree(p)
Definition: util-mem.h:61
PACKET_DROP
#define PACKET_DROP(p)
Definition: decode.h:889
SGH_PROFILING_RECORD
#define SGH_PROFILING_RECORD(det_ctx, sgh)
Definition: util-profiling.h:272
PacketAlertFinalize
void PacketAlertFinalize(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
Check the threshold of the sigs that match, set actions, break on pass action This function iterate t...
Definition: detect-engine-alert.c:287
Signature_::addr_dst_match6
DetectMatchAddressIPv6 * addr_dst_match6
Definition: detect.h:577
Flow_::alstate
void * alstate
Definition: flow.h:486
Signature_::id
uint32_t id
Definition: detect.h:580
DeStateStore_::store
DeStateStoreItem store[DE_STATE_CHUNK_SIZE]
Definition: detect-engine-state.h:79
Flow_::flags
uint32_t flags
Definition: flow.h:431
RuleMatchCandidateTx::stream_reset
uint32_t stream_reset
Definition: detect.h:1041
detect-engine-iponly.h
Signature_
Signature container.
Definition: detect.h:546
IP_GET_IPPROTO
#define IP_GET_IPPROTO(p)
Definition: decode.h:274
InspectionBufferClean
void InspectionBufferClean(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine.c:1340
SignatureMask
#define SignatureMask
Definition: detect.h:280
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
TRACE_SID_TXS
#define TRACE_SID_TXS(sid, txs,...)
Definition: detect.c:1028
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:227
DetectEngineThreadCtx_::tx_id_set
bool tx_id_set
Definition: detect.h:1117
SC_ERR_MEM_ALLOC
@ SC_ERR_MEM_ALLOC
Definition: util-error.h:31
STREAMTCP_FLAG_APP_LAYER_DISABLED
#define STREAMTCP_FLAG_APP_LAYER_DISABLED
Definition: stream-tcp-private.h:188
DetectRunScratchpad::flow_flags
const uint8_t flow_flags
Definition: detect.c:65
DetectEngineThreadCtx_::de_ctx
DetectEngineCtx * de_ctx
Definition: detect.h:1174
suricata.h
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:816
Packet_::dst
Address dst
Definition: decode.h:425
SigGroupHead_::non_pf_syn_store_array
SignatureNonPrefilterStore * non_pf_syn_store_array
Definition: detect.h:1416
PROF_DETECT_TX
@ PROF_DETECT_TX
Definition: suricata-common.h:421
DetectEngineAppInspectionEngine_::dir
uint8_t dir
Definition: detect.h:400
PKT_NOPACKET_INSPECTION
#define PKT_NOPACKET_INSPECTION
Definition: decode.h:1146
SignatureNonPrefilterStore_::mask
SignatureMask mask
Definition: detect.h:1028
PacketAlertAppend
int PacketAlertAppend(DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, uint64_t tx_id, uint8_t flags)
append a signature match to a packet
Definition: detect-engine-alert.c:188
DetectRunScratchpad::alproto
const AppProto alproto
Definition: detect.c:64
DE_STATE_FLAG_FULL_INSPECT
#define DE_STATE_FLAG_FULL_INSPECT
Definition: detect-engine-state.h:56
STREAMTCP_STREAM_FLAG_DISABLE_RAW
#define STREAMTCP_STREAM_FLAG_DISABLE_RAW
Definition: stream-tcp-private.h:219
ALPROTO_SMB
@ ALPROTO_SMB
Definition: app-layer-protos.h:37
DetectEngineThreadCtx_::mt_det_ctxs_cnt
uint32_t mt_det_ctxs_cnt
Definition: detect.h:1063
AppLayerGetTxIteratorFunc
AppLayerGetTxIterTuple(* AppLayerGetTxIteratorFunc)(const uint8_t ipproto, const AppProto alproto, void *alstate, uint64_t min_tx_id, uint64_t max_tx_id, AppLayerGetTxIterState *state)
tx iterator prototype
Definition: app-layer-parser.h:152
likely
#define likely(expr)
Definition: util-optimize.h:32
DE_STATE_FLAG_FILE_INSPECT
#define DE_STATE_FLAG_FILE_INSPECT
Definition: detect-engine-state.h:61
FLOW_NOPACKET_INSPECTION
#define FLOW_NOPACKET_INSPECTION
Definition: flow.h:63
DetectEngineCtx_::io_ctx
DetectEngineIPOnlyCtx io_ctx
Definition: detect.h:848
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:376
Signature_::addr_dst_match4
DetectMatchAddressIPv4 * addr_dst_match4
Definition: detect.h:574
FlowGetDisruptionFlags
uint8_t FlowGetDisruptionFlags(const Flow *f, uint8_t flags)
get 'disruption' flags: GAP/DEPTH/PASS
Definition: flow.c:1121
TcpSession_
Definition: stream-tcp-private.h:260
flow.h
Signature_::addr_src_match4_cnt
uint16_t addr_src_match4_cnt
Definition: detect.h:571
SigIntId
#define SigIntId
Definition: suricata-common.h:296
DeStateStoreItem_
Definition: detect-engine-state.h:73
GenericVarFree
void GenericVarFree(GenericVar *gv)
Definition: util-var.c:47
Signature_::addr_dst_match4_cnt
uint16_t addr_dst_match4_cnt
Definition: detect.h:570
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:460
Packet_::dp
Port dp
Definition: decode.h:435
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SIG_GROUP_HEAD_HAVEFILESHA256
#define SIG_GROUP_HEAD_HAVEFILESHA256
Definition: detect.h:1286
SignatureNonPrefilterStore_::alproto
uint8_t alproto
Definition: detect.h:1029
SigGroupHead_::filestore_cnt
uint16_t filestore_cnt
Definition: detect.h:1420
AppLayerParserGetTxCnt
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
Definition: app-layer-parser.c:1117
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:262
PACKET_ALERT_FLAG_STATE_MATCH
#define PACKET_ALERT_FLAG_STATE_MATCH
Definition: decode.h:292
AppLayerParserHasDecoderEvents
bool AppLayerParserHasDecoderEvents(AppLayerParserState *pstate)
Definition: app-layer-parser.c:1429
DetectEngineLookupFlow_::tcp
DetectPort * tcp
Definition: detect.h:738
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:111
DetectTransaction_::detect_flags
const uint64_t detect_flags
Definition: detect-engine-state.h:103
detect-engine-address.h
Flow_::tenant_id
uint32_t tenant_id
Definition: flow.h:426
Packet_::src
Address src
Definition: decode.h:424
DetectEngineThreadCtx_::counter_mpm_list
uint16_t counter_mpm_list
Definition: detect.h:1088
SigMatchSignaturesGetSgh
const SigGroupHead * SigMatchSignaturesGetSgh(const DetectEngineCtx *de_ctx, const Packet *p)
Get the SigGroupHead for a packet.
Definition: detect.c:192
DetectRunPrefilterTx
void DetectRunPrefilterTx(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p, const uint8_t ipproto, const uint8_t flow_flags, const AppProto alproto, void *alstate, DetectTransaction *tx)
run prefilter engines on a transaction
Definition: detect-engine-prefilter.c:93
RuleMatchCandidateTxArrayInit
void RuleMatchCandidateTxArrayInit(DetectEngineThreadCtx *det_ctx, uint32_t size)
Definition: detect.c:955
SIG_FLAG_DP_ANY
#define SIG_FLAG_DP_ANY
Definition: detect.h:214
DetectPortLookupGroup
DetectPort * DetectPortLookupGroup(DetectPort *dp, uint16_t port)
Function that find the group matching address in a group head.
Definition: detect-engine-port.c:670
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1158
detect-engine-threshold.h
DetectEngineThreadCtx_::io_ctx
DetectEngineIPOnlyThreadCtx io_ctx
Definition: detect.h:1156
Signature_::mask
SignatureMask mask
Definition: detect.h:556
app-layer.h
RuleMatchCandidateTx
Definition: detect.h:1033
DetectEngineThreadCtx_::TenantGetId
uint32_t(* TenantGetId)(const void *, const Packet *p)
Definition: detect.h:1070
PrefilterRuleStore_::rule_id_array
SigIntId * rule_id_array
Definition: util-prefilter.h:36
PacketEngineEvents_::cnt
uint8_t cnt
Definition: decode.h:318
Flow_::de_ctx_version
uint32_t de_ctx_version
Definition: flow.h:474
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:135
DetectEngineThreadCtx_::match_array
Signature ** match_array
Definition: detect.h:1130