suricata
detect.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 /**
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 
29 #include "decode.h"
30 #include "packet.h"
31 #include "flow.h"
32 #include "stream-tcp.h"
33 #include "app-layer.h"
34 #include "app-layer-parser.h"
35 #include "app-layer-frames.h"
36 
37 #include "detect.h"
38 #include "detect-parse.h"
39 #include "detect-dsize.h"
40 #include "detect-engine.h"
41 #include "detect-engine-build.h"
42 #include "detect-engine-frame.h"
43 #include "detect-engine-profile.h"
44 
45 #include "detect-engine-alert.h"
46 #include "detect-engine-siggroup.h"
47 #include "detect-engine-address.h"
48 #include "detect-engine-proto.h"
49 #include "detect-engine-port.h"
50 #include "detect-engine-mpm.h"
51 #include "detect-engine-iponly.h"
54 #include "detect-engine-state.h"
55 #include "detect-engine-analyzer.h"
56 
57 #include "detect-engine-payload.h"
58 #include "detect-engine-event.h"
59 
60 #include "detect-filestore.h"
61 #include "detect-flowvar.h"
62 #include "detect-replace.h"
63 
64 #include "util-validate.h"
65 #include "util-detect.h"
66 #include "util-profiling.h"
67 
68 #include "action-globals.h"
69 
70 typedef struct DetectRunScratchpad {
72  const uint8_t flow_flags; /* flow/state flags: STREAM_* */
73  const bool app_decoder_events;
75  const SigGroupHead *sgh;
77 
78 /* prototypes */
79 static DetectRunScratchpad DetectRunSetup(const DetectEngineCtx *de_ctx,
80  DetectEngineThreadCtx *det_ctx, Packet *const p, Flow *const pflow,
81  const enum DetectFirewallPacketPolicies fw_pkt_policy);
82 static void DetectRunInspectIPOnly(ThreadVars *tv, const DetectEngineCtx *de_ctx,
83  DetectEngineThreadCtx *det_ctx, Flow * const pflow, Packet * const p);
84 static inline void DetectRunGetRuleGroup(const DetectEngineCtx *de_ctx,
85  Packet * const p, Flow * const pflow, DetectRunScratchpad *scratch);
86 static inline void DetectRunPrefilterPkt(ThreadVars *tv, const DetectEngineCtx *de_ctx,
87  DetectEngineThreadCtx *det_ctx, Packet *p, DetectRunScratchpad *scratch);
88 static inline uint8_t DetectRulePacketRules(ThreadVars *const tv,
89  const DetectEngineCtx *const de_ctx, DetectEngineThreadCtx *const det_ctx, Packet *const p,
90  Flow *const pflow, const DetectRunScratchpad *scratch);
91 static void DetectRunTx(ThreadVars *tv, DetectEngineCtx *de_ctx,
92  DetectEngineThreadCtx *det_ctx, Packet *p,
93  Flow *f, DetectRunScratchpad *scratch);
94 static void DetectRunFrames(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
95  Packet *p, Flow *f, DetectRunScratchpad *scratch);
96 static inline void DetectRunPostRules(ThreadVars *tv, const DetectEngineCtx *de_ctx,
97  DetectEngineThreadCtx *det_ctx, Packet *const p, Flow *const pflow,
98  DetectRunScratchpad *scratch);
99 static void DetectRunCleanup(DetectEngineThreadCtx *det_ctx,
100  Packet *p, Flow * const pflow);
101 static inline void DetectRunAppendDefaultAccept(DetectEngineThreadCtx *det_ctx, Packet *p);
102 
103 /** \internal
104  */
105 static void DetectRun(ThreadVars *th_v,
107  Packet *p)
108 {
109  SCEnter();
110  SCLogDebug("pcap_cnt %" PRIu64 " direction %s pkt_src %s", PcapPacketCntGet(p),
111  p->flow ? (FlowGetPacketDirection(p->flow, p) == TOSERVER ? "toserver" : "toclient")
112  : "noflow",
114 
115  /* Load the Packet's flow early, even though it might not be needed.
116  * Mark as a constant pointer, although the flow itself can change. */
117  Flow * const pflow = p->flow;
118 
119  DetectRunScratchpad scratch =
120  DetectRunSetup(de_ctx, det_ctx, p, pflow, DETECT_FIREWALL_POLICY_PACKET_FILTER);
121 
122  /* run the IPonly engine */
123  DetectRunInspectIPOnly(th_v, de_ctx, det_ctx, pflow, p);
124 
125  /* get our rule group */
126  DetectRunGetRuleGroup(de_ctx, p, pflow, &scratch);
127  /* if we didn't get a sig group head, we
128  * have nothing to do.... */
129  if (scratch.sgh == NULL) {
130  if (!EngineModeIsFirewall()) {
131  SCLogDebug("no sgh for this packet, nothing to match against");
132  goto end;
133  }
134  SCLogDebug(
135  "packet %" PRIu64 ": no sgh, need to apply default policies", PcapPacketCntGet(p));
136  } else {
137  /* run the prefilters for packets */
138  DetectRunPrefilterPkt(th_v, de_ctx, det_ctx, p, &scratch);
139  }
141  /* inspect the rules against the packet */
142  const uint8_t pkt_policy = DetectRulePacketRules(th_v, de_ctx, det_ctx, p, pflow, &scratch);
144  SCLogDebug("packet %" PRIu64 ": pkt_policy %02x (p->action %02x)", PcapPacketCntGet(p),
145  pkt_policy, p->action);
146 
147  /* Only FW rules will already have set the action, IDS rules go through PacketAlertFinalize
148  *
149  * If rules told us to drop or accept:packet/accept:flow, we skip app_filter and app_td.
150  *
151  * accept:hook won't have set the pkt_policy, so we simply continue.
152  *
153  * TODO what about app state progression, cleanup and such? */
154  if (pkt_policy & (ACTION_DROP | ACTION_ACCEPT)) {
155  goto end;
156  }
157 
158  /* run tx/state inspection. Don't call for ICMP error msgs. */
159  if (pflow && pflow->alstate && likely(pflow->proto == p->proto)) {
160  if (p->proto == IPPROTO_TCP) {
161  if ((p->flags & PKT_STREAM_EST) == 0) {
162  SCLogDebug("packet %" PRIu64 ": skip tcp non-established", PcapPacketCntGet(p));
163  if (EngineModeIsFirewall()) {
164  SCLogDebug("default accept: no PKT_STREAM_EST");
165  DetectRunAppendDefaultAccept(det_ctx, p);
166  }
167  goto end;
168  }
169  const TcpSession *ssn = p->flow->protoctx;
170  bool setting_nopayload = p->flow->alparser &&
172  p->flow->alparser, APP_LAYER_PARSER_NO_INSPECTION) &&
174  // we may be right after disabling app-layer (ssh)
175  if (ssn &&
176  ((ssn->flags & STREAMTCP_FLAG_APP_LAYER_DISABLED) == 0 || setting_nopayload)) {
177  // PACKET_PROFILING_DETECT_START(p, PROF_DETECT_TX);
178  DetectRunFrames(th_v, de_ctx, det_ctx, p, pflow, &scratch);
179  // PACKET_PROFILING_DETECT_END(p, PROF_DETECT_TX);
180  }
181  // no update to transactions
182  if (!PKT_IS_PSEUDOPKT(p) && p->app_update_direction == 0 &&
183  ((PKT_IS_TOSERVER(p) && (p->flow->flags & FLOW_TS_APP_UPDATED) == 0) ||
184  (PKT_IS_TOCLIENT(p) && (p->flow->flags & FLOW_TC_APP_UPDATED) == 0))) {
185  SCLogDebug("packet %" PRIu64 ": no app-layer update", PcapPacketCntGet(p));
186  if (EngineModeIsFirewall()) {
187  SCLogDebug("default accept: no app update");
188  DetectRunAppendDefaultAccept(det_ctx, p);
189  }
190  goto end;
191  }
192  } else if (p->proto == IPPROTO_UDP) {
193  DetectRunFrames(th_v, de_ctx, det_ctx, p, pflow, &scratch);
194  }
195 
197  DetectRunTx(th_v, de_ctx, det_ctx, p, pflow, &scratch);
199  /* see if we need to increment the inspect_id and reset the de_state */
202  pflow, pflow->alparser, pflow->alstate, scratch.flow_flags, (scratch.sgh == NULL));
204  } else {
205  SCLogDebug("packet %" PRIu64 ": no flow / app-layer", PcapPacketCntGet(p));
206  if (EngineModeIsFirewall()) {
207  SCLogDebug("default accept: no flow/app");
208  DetectRunAppendDefaultAccept(det_ctx, p);
209  }
210  }
211 
212 end:
213  DetectRunPostRules(th_v, de_ctx, det_ctx, p, pflow, &scratch);
214 
215  DetectRunCleanup(det_ctx, p, pflow);
216  SCReturn;
217 }
218 
219 /** \internal
220  */
221 static void DetectRunPacketHook(ThreadVars *th_v, const DetectEngineCtx *de_ctx,
222  DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p,
223  enum DetectFirewallPacketPolicies fw_pkt_policy)
224 {
225  SCEnter();
226  SCLogDebug("pcap_cnt %" PRIu64 " direction %s pkt_src %s", PcapPacketCntGet(p),
227  p->flow ? (FlowGetPacketDirection(p->flow, p) == TOSERVER ? "toserver" : "toclient")
228  : "noflow",
230 
231  /* Load the Packet's flow early, even though it might not be needed.
232  * Mark as a constant pointer, although the flow itself can change. */
233  Flow *const pflow = p->flow;
234 
235  DetectRunScratchpad scratch = DetectRunSetup(de_ctx, det_ctx, p, pflow, fw_pkt_policy);
236  scratch.sgh = sgh;
237 
238  /* if we didn't get a sig group head, we
239  * have nothing to do.... */
240  if (scratch.sgh == NULL) {
241  SCLogDebug("no sgh for this packet, nothing to match against");
242  goto end;
243  }
244 
245  /* run the prefilters for packets */
246  DetectRunPrefilterPkt(th_v, de_ctx, det_ctx, p, &scratch);
247 
248  // PACKET_PROFILING_DETECT_START(p, PROF_DETECT_RULES); // TODO
249  /* inspect the rules against the packet */
250  const uint8_t pkt_policy = DetectRulePacketRules(th_v, de_ctx, det_ctx, p, pflow, &scratch);
251  // PACKET_PROFILING_DETECT_END(p, PROF_DETECT_RULES);
252  if (pkt_policy & (ACTION_DROP | ACTION_ACCEPT)) {
253  goto end;
254  }
255 
256 end:
257  DetectRunPostRules(th_v, de_ctx, det_ctx, p, pflow, &scratch);
258 
259  DetectRunCleanup(det_ctx, p, pflow);
260  SCReturn;
261 }
262 
263 static void DetectRunPostMatch(ThreadVars *tv,
264  DetectEngineThreadCtx *det_ctx, Packet *p,
265  const Signature *s)
266 {
267  /* run the packet match functions */
269  if (smd != NULL) {
271 
272  SCLogDebug("running match functions, sm %p", smd);
273 
274  while (1) {
276  (void)sigmatch_table[smd->type].Match(det_ctx, p, s, smd->ctx);
277  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
278  if (smd->is_last)
279  break;
280  smd++;
281  }
282  }
283 }
284 
285 /**
286  * \brief Get the SigGroupHead for a packet.
287  *
288  * \param de_ctx detection engine context
289  * \param p packet
290  *
291  * \retval sgh the SigGroupHead or NULL if non applies to the packet
292  */
294  const Packet *p)
295 {
296  SCEnter();
297  SigGroupHead *sgh = NULL;
298 
299  /* use ethernet non-IP sgh if we're ethernet but have no (valid) IP layer on top of it. */
300  if (PacketIsEthernet(p) && p->proto == 0 && de_ctx->eth_non_ip_sgh != NULL) {
301  SCLogDebug("using eth_non_ip_sgh %p", de_ctx->eth_non_ip_sgh);
302  SCReturnPtr(de_ctx->eth_non_ip_sgh, "SigGroupHead");
303  }
304 
305  /* if the packet proto is 0 (not set), we're inspecting it against
306  * the decoder events sgh we have. */
307  if (p->proto == 0 && p->events.cnt > 0) {
308  SCReturnPtr(de_ctx->decoder_event_sgh, "SigGroupHead");
309  } else if (p->proto == 0) {
310  if (!(PacketIsIPv4(p) || PacketIsIPv6(p))) {
311  /* not IP, so nothing to do */
312  SCReturnPtr(NULL, "SigGroupHead");
313  }
314  }
315 
316  /* select the flow_gh */
317  const int dir = (p->flowflags & FLOW_PKT_TOCLIENT) == 0;
318 
319  int proto = PacketGetIPProto(p);
320  if (proto == IPPROTO_TCP) {
321  DetectPort *list = de_ctx->flow_gh[dir].tcp;
322  SCLogDebug("tcp toserver %p, tcp toclient %p: going to use %p", de_ctx->flow_gh[1].tcp,
323  de_ctx->flow_gh[0].tcp, de_ctx->flow_gh[dir].tcp);
324  const uint16_t port = dir ? p->dp : p->sp;
325  SCLogDebug("tcp port %u -> %u:%u", port, p->sp, p->dp);
326  DetectPort *sghport = DetectPortLookupGroup(list, port);
327  if (sghport != NULL)
328  sgh = sghport->sh;
329  SCLogDebug("TCP list %p, port %u, direction %s, sghport %p, sgh %p", list, port,
330  dir ? "toserver" : "toclient", sghport, sgh);
331  } else if (proto == IPPROTO_UDP) {
332  DetectPort *list = de_ctx->flow_gh[dir].udp;
333  uint16_t port = dir ? p->dp : p->sp;
334  DetectPort *sghport = DetectPortLookupGroup(list, port);
335  if (sghport != NULL)
336  sgh = sghport->sh;
337  SCLogDebug("UDP list %p, port %u, direction %s, sghport %p, sgh %p", list, port,
338  dir ? "toserver" : "toclient", sghport, sgh);
339  } else {
340  sgh = de_ctx->flow_gh[dir].sgh[proto];
341  }
342 
343  SCReturnPtr(sgh, "SigGroupHead");
344 }
345 
346 static inline void DetectPrefilterCopyDeDup(
348 {
349  SigIntId *pf_ptr = det_ctx->pmq.rule_id_array;
350  uint32_t final_cnt = det_ctx->pmq.rule_id_array_cnt;
351  Signature **sig_array = de_ctx->sig_array;
352  Signature **match_array = det_ctx->match_array;
353  SigIntId previous_id = (SigIntId)-1;
354  while (final_cnt-- > 0) {
355  SigIntId id = *pf_ptr++;
356  Signature *s = sig_array[id];
357 
358  /* As the prefilter list can contain duplicates, check for that here. */
359  if (likely(id != previous_id)) {
360  *match_array++ = s;
361  previous_id = id;
362  }
363  }
364 
365  det_ctx->match_array_cnt = (uint32_t)(match_array - det_ctx->match_array);
367  PMQ_RESET(&det_ctx->pmq);
368 }
369 
370 /** \internal
371  * \brief update flow's file tracking flags based on the detection engine
372  * A set of flags is prepared that is sent to the File API. The
373  File API may reject one or more based on the global force settings.
374  */
375 static inline void
376 DetectPostInspectFileFlagsUpdate(Flow *f, const SigGroupHead *sgh, uint8_t direction)
377 {
378  uint16_t flow_file_flags = FLOWFILE_INIT;
379 
380  if (sgh == NULL) {
381  SCLogDebug("requesting disabling all file features for flow");
382  flow_file_flags = FLOWFILE_NONE;
383  } else {
384  if (sgh->filestore_cnt == 0) {
385  SCLogDebug("requesting disabling filestore for flow");
386  flow_file_flags |= (FLOWFILE_NO_STORE_TS|FLOWFILE_NO_STORE_TC);
387  }
388 #ifdef HAVE_MAGIC
389  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILEMAGIC)) {
390  SCLogDebug("requesting disabling magic for flow");
391  flow_file_flags |= (FLOWFILE_NO_MAGIC_TS|FLOWFILE_NO_MAGIC_TC);
392  }
393 #endif
394  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILEMD5)) {
395  SCLogDebug("requesting disabling md5 for flow");
396  flow_file_flags |= (FLOWFILE_NO_MD5_TS|FLOWFILE_NO_MD5_TC);
397  }
398  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILESHA1)) {
399  SCLogDebug("requesting disabling sha1 for flow");
400  flow_file_flags |= (FLOWFILE_NO_SHA1_TS|FLOWFILE_NO_SHA1_TC);
401  }
402  if (!(sgh->flags & SIG_GROUP_HEAD_HAVEFILESHA256)) {
403  SCLogDebug("requesting disabling sha256 for flow");
404  flow_file_flags |= (FLOWFILE_NO_SHA256_TS|FLOWFILE_NO_SHA256_TC);
405  }
406  }
407  if (flow_file_flags != 0) {
408  FileUpdateFlowFileFlags(f, flow_file_flags, direction);
409  }
410 }
411 
412 static inline void
413 DetectRunPostGetFirstRuleGroup(const Packet *p, Flow *pflow, const SigGroupHead *sgh)
414 {
415  if ((p->flowflags & FLOW_PKT_TOSERVER) && !(pflow->flags & FLOW_SGH_TOSERVER)) {
416  /* first time we see this toserver sgh, store it */
417  pflow->sgh_toserver = sgh;
418  pflow->flags |= FLOW_SGH_TOSERVER;
419 
420  if (p->proto == IPPROTO_TCP && (sgh == NULL || !(sgh->flags & SIG_GROUP_HEAD_HAVERAWSTREAM))) {
421  if (pflow->protoctx != NULL) {
422  TcpSession *ssn = pflow->protoctx;
423  SCLogDebug("STREAMTCP_STREAM_FLAG_DISABLE_RAW ssn.client");
425  }
426  }
427 
428  DetectPostInspectFileFlagsUpdate(pflow,
429  pflow->sgh_toserver, STREAM_TOSERVER);
430 
431  } else if ((p->flowflags & FLOW_PKT_TOCLIENT) && !(pflow->flags & FLOW_SGH_TOCLIENT)) {
432  pflow->sgh_toclient = sgh;
433  pflow->flags |= FLOW_SGH_TOCLIENT;
434 
435  if (p->proto == IPPROTO_TCP && (sgh == NULL || !(sgh->flags & SIG_GROUP_HEAD_HAVERAWSTREAM))) {
436  if (pflow->protoctx != NULL) {
437  TcpSession *ssn = pflow->protoctx;
438  SCLogDebug("STREAMTCP_STREAM_FLAG_DISABLE_RAW ssn.server");
440  }
441  }
442 
443  DetectPostInspectFileFlagsUpdate(pflow,
444  pflow->sgh_toclient, STREAM_TOCLIENT);
445  }
446 }
447 
448 static inline void DetectRunGetRuleGroup(
449  const DetectEngineCtx *de_ctx,
450  Packet * const p, Flow * const pflow,
451  DetectRunScratchpad *scratch)
452 {
453  const SigGroupHead *sgh = NULL;
454 
455  if (pflow) {
456  bool use_flow_sgh = false;
457  /* Get the stored sgh from the flow (if any). Make sure we're not using
458  * the sgh for icmp error packets part of the same stream. */
459  if (PacketGetIPProto(p) == pflow->proto) { /* filter out icmp */
461  if ((p->flowflags & FLOW_PKT_TOSERVER) && (pflow->flags & FLOW_SGH_TOSERVER)) {
462  sgh = pflow->sgh_toserver;
463  SCLogDebug("sgh = pflow->sgh_toserver; => %p", sgh);
464  use_flow_sgh = true;
465  } else if ((p->flowflags & FLOW_PKT_TOCLIENT) && (pflow->flags & FLOW_SGH_TOCLIENT)) {
466  sgh = pflow->sgh_toclient;
467  SCLogDebug("sgh = pflow->sgh_toclient; => %p", sgh);
468  use_flow_sgh = true;
469  }
471  }
472 
473  if (!(use_flow_sgh)) {
477 
478  /* HACK: prevent the wrong sgh (or NULL) from being stored in the
479  * flow's sgh pointers */
480  if (PacketIsICMPv4(p) && ICMPV4_DEST_UNREACH_IS_VALID(p)) {
481  ; /* no-op */
482  } else {
483  /* store the found sgh (or NULL) in the flow to save us
484  * from looking it up again for the next packet.
485  * Also run other tasks */
486  DetectRunPostGetFirstRuleGroup(p, pflow, sgh);
487  }
488  }
489  } else { /* p->flags & PKT_HAS_FLOW */
490  /* no flow */
491 
495  }
496 
497  scratch->sgh = sgh;
498 }
499 
500 static void DetectRunInspectIPOnly(ThreadVars *tv, const DetectEngineCtx *de_ctx,
501  DetectEngineThreadCtx *det_ctx,
502  Flow * const pflow, Packet * const p)
503 {
504  if (pflow) {
506  SCLogDebug("testing against \"ip-only\" signatures");
507 
509  IPOnlyMatchPacket(tv, de_ctx, det_ctx, &de_ctx->io_ctx, p);
511  }
512  } else { /* p->flags & PKT_HAS_FLOW */
513  /* no flow */
514 
515  /* Even without flow we should match the packet src/dst */
517  IPOnlyMatchPacket(tv, de_ctx, det_ctx, &de_ctx->io_ctx, p);
519  }
520 }
521 
522 /** \internal
523  * \brief inspect the rule header: protocol, ports, etc
524  * \retval bool false if no match, true if match */
525 static inline bool DetectRunInspectRuleHeader(
526  const Packet *p, const Flow *f, const Signature *s, const uint32_t sflags)
527 {
528  /* check if this signature has a requirement for flowvars of some type
529  * and if so, if we actually have any in the flow. If not, the sig
530  * can't match and we skip it. */
531  if ((p->flags & PKT_HAS_FLOW) && (sflags & SIG_FLAG_REQUIRE_FLOWVAR)) {
532  DEBUG_VALIDATE_BUG_ON(f == NULL);
533 
534  /* no flowvars? skip this sig */
535  const bool fv = f->flowvar != NULL;
536  if (!fv) {
537  SCLogDebug("skipping sig as the flow has no flowvars and sig "
538  "has SIG_FLAG_REQUIRE_FLOWVAR flag set.");
539  return false;
540  }
541  }
542 
543  if (!(s->proto == NULL)) {
544  const uint8_t s_proto_flags = s->proto->flags;
545  /* TODO does it make sense to move these flags to s->flags? */
546  if ((s_proto_flags & DETECT_PROTO_IPV4) && !PacketIsIPv4(p)) {
547  SCLogDebug("ip version didn't match");
548  return false;
549  }
550  if ((s_proto_flags & DETECT_PROTO_IPV6) && !PacketIsIPv6(p)) {
551  SCLogDebug("ip version didn't match");
552  return false;
553  }
554  if (DetectProtoContainsProto(s->proto, PacketGetIPProto(p)) == 0) {
555  SCLogDebug("proto didn't match");
556  if (PacketIsEthernet(p) &&
557  (s_proto_flags & (DETECT_PROTO_ETHERNET | DETECT_PROTO_ARP))) {
558  SCLogDebug("checking ether/arp protocol");
559  if ((s_proto_flags & DETECT_PROTO_ARP) && !PacketIsARP(p)) {
560  return false;
561  }
562  SCLogDebug("checking eth protocol: match!");
563  } else {
564  return false;
565  }
566  }
567  }
568 
569  /* check the source & dst port in the sig */
570  if (p->proto == IPPROTO_TCP || p->proto == IPPROTO_UDP || p->proto == IPPROTO_SCTP) {
571  if (!(sflags & SIG_FLAG_DP_ANY)) {
572  if (p->flags & PKT_IS_FRAGMENT)
573  return false;
574  const DetectPort *dport = DetectPortLookupGroup(s->dp, p->dp);
575  if (dport == NULL) {
576  SCLogDebug("dport didn't match.");
577  return false;
578  }
579  }
580  if (!(sflags & SIG_FLAG_SP_ANY)) {
581  if (p->flags & PKT_IS_FRAGMENT)
582  return false;
583  const DetectPort *sport = DetectPortLookupGroup(s->sp, p->sp);
584  if (sport == NULL) {
585  SCLogDebug("sport didn't match.");
586  return false;
587  }
588  }
589  } else if ((sflags & (SIG_FLAG_DP_ANY|SIG_FLAG_SP_ANY)) != (SIG_FLAG_DP_ANY|SIG_FLAG_SP_ANY)) {
590  SCLogDebug("port-less protocol and sig needs ports");
591  return false;
592  }
593 
594  /* check the destination address */
595  if (!(sflags & SIG_FLAG_DST_ANY)) {
596  if (PacketIsIPv4(p)) {
598  return false;
599  } else if (PacketIsIPv6(p)) {
601  return false;
602  }
603  }
604  /* check the source address */
605  if (!(sflags & SIG_FLAG_SRC_ANY)) {
606  if (PacketIsIPv4(p)) {
608  return false;
609  } else if (PacketIsIPv6(p)) {
611  return false;
612  }
613  }
614 
615  return true;
616 }
617 
618 /** \internal
619  * \brief run packet/stream prefilter engines
620  */
621 static inline void DetectRunPrefilterPkt(ThreadVars *tv, const DetectEngineCtx *de_ctx,
622  DetectEngineThreadCtx *det_ctx, Packet *p, DetectRunScratchpad *scratch)
623 {
624  /* create our prefilter mask */
625  PacketCreateMask(p, &p->sig_mask, scratch->alproto, scratch->app_decoder_events);
626  /* run the prefilter engines */
627  Prefilter(det_ctx, scratch->sgh, p, scratch->flow_flags, p->sig_mask);
628  /* create match list if we have non-pf and/or pf */
629  if (det_ctx->pmq.rule_id_array_cnt) {
630 #ifdef PROFILING
631  if (tv) {
633  &tv->stats, det_ctx->counter_mpm_list, (int64_t)det_ctx->pmq.rule_id_array_cnt);
634  }
635 #endif
637  DetectPrefilterCopyDeDup(de_ctx, det_ctx);
639  }
640 }
641 
642 /** \internal
643  * \brief check if the tx whose id is given is the only one
644  * live transaction for the flow in the given direction
645  *
646  * \param f flow
647  * \param txid transaction id
648  * \param dir direction
649  *
650  * \retval bool true if we are sure this tx is the only one live in said direction
651  */
652 static bool IsOnlyTxInDirection(Flow *f, uint64_t txid, uint8_t dir)
653 {
654  uint64_t tx_cnt = AppLayerParserGetTxCnt(f, f->alstate);
655  if (tx_cnt == txid + 1) {
656  // only live tx
657  return true;
658  }
659  if (tx_cnt == txid + 2) {
660  // 2 live txs, one after us
661  void *tx = AppLayerParserGetTx(f->proto, f->alproto, f->alstate, txid + 1);
662  if (tx) {
664  // test if the other tx is unidirectional in the other way
665  if ((dir == STREAM_TOSERVER && (txd->flags & APP_LAYER_TX_SKIP_INSPECT_TS)) ||
666  (dir == STREAM_TOCLIENT && (txd->flags & APP_LAYER_TX_SKIP_INSPECT_TC))) {
667  return true;
668  }
669  }
670  }
671  return false;
672 }
673 
674 static int SortHelper(const void *a, const void *b)
675 {
676  const Signature *sa = *(const Signature **)a;
677  const Signature *sb = *(const Signature **)b;
678  if (sa->iid == sb->iid)
679  return 0;
680  return sa->iid > sb->iid ? 1 : -1;
681 }
682 
683 static inline bool SkipFwRules(const Packet *p)
684 {
685  if (p->flow != NULL) {
686  return (p->flow->flags & FLOW_ACTION_ACCEPT) != 0;
687  }
688  return false;
689 }
690 
691 /**
692  * \internal
693  * \brief apply packet default policy
694  * \param[in] de_ctx detect engine, for looking up the policy
695  * \param[in] policy policy to apply
696  * \param[in] p packet to apply policy to
697  * \param[in] final see if we need to apply accept:hook to the packet
698  * \retval action action to immediately apply, accept:hook will not set this unless final is true
699  *
700  * If this is run from the post-match final check, we need to apply a
701  * packet:filter accept:hook to the packet as well.
702  */
703 static uint8_t DetectRunApplyPacketPolicy(const DetectEngineCtx *de_ctx,
704  DetectEngineThreadCtx *det_ctx, const enum DetectFirewallPacketPolicies policy, Packet *p,
705  const bool final)
706 {
708  const struct DetectFirewallPolicy *pol = &de_ctx->fw_policies->pkt[policy];
709  if (pol->action & ACTION_DROP) {
710  SCLogDebug("packet %" PRIu64 ": drop PKT_DROP_REASON_FW_DEFAULT_PACKET_POLICY",
713  if (p->flow && pol->action_scope == ACTION_SCOPE_FLOW) {
714  FlowSetDropAction(p->flow, FLOW_ACTION_BY_FIREWALL);
715  SCLogDebug("packet %" PRIu64 ": drop scope flow", PcapPacketCntGet(p));
716  }
717  } else if (pol->action & ACTION_ACCEPT) {
718  SCLogDebug("packet %" PRIu64 ": accept", PcapPacketCntGet(p));
719  if (pol->action_scope == ACTION_SCOPE_PACKET) {
720  p->action |= pol->action;
721  SCLogDebug("packet %" PRIu64 ": accept scope packet", PcapPacketCntGet(p));
722  } else if (pol->action_scope == ACTION_SCOPE_HOOK) {
723  SCLogDebug("packet %" PRIu64 ": accept scope hook", PcapPacketCntGet(p));
724  if (final) {
725  p->action |= pol->action;
726  SCLogDebug("packet %" PRIu64 ": accept scope hook upgraded to packet",
728  }
729  } else if (pol->action_scope == ACTION_SCOPE_FLOW) {
730  p->action |= pol->action;
731  SCLogDebug("packet %" PRIu64 ": accept scope flow", PcapPacketCntGet(p));
732  if (p->flow) {
734  }
735  } else {
736  /* should be unreachable */
738  }
739  } else {
740  /* should be unreachable */
742  }
744  if (s != NULL) {
746  }
747  return p->action;
748 }
749 
750 /** \internal
751  * \brief helper for appending a packet alert
752  * Tries to find (guess) a TX to add to the alert.
753  */
754 static void DetectRulePacketAppendAlert(const DetectEngineCtx *de_ctx,
755  DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, Flow *f,
756  const uint8_t alert_flags_in)
757 {
759 
760  if (f && f->alstate) {
761  const uint8_t dir = (p->flowflags & FLOW_PKT_TOCLIENT) ? STREAM_TOCLIENT : STREAM_TOSERVER;
762  const uint64_t tx_id = AppLayerParserGetTransactionInspectId(f->alparser, dir);
763  if ((s->alproto != ALPROTO_UNKNOWN && f->proto == IPPROTO_UDP) ||
764  (de_ctx->guess_applayer && IsOnlyTxInDirection(f, tx_id, dir))) {
765  // if there is a UDP specific app-layer signature,
766  // or only one live transaction
767  // try to use the good tx for the packet direction
768  void *tx_ptr = AppLayerParserGetTx(f->proto, f->alproto, f->alstate, tx_id);
769  AppLayerTxData *txd =
770  tx_ptr ? AppLayerParserGetTxData(f->proto, f->alproto, tx_ptr) : NULL;
772  uint8_t alert_flags = alert_flags_in;
773  if (f->proto != IPPROTO_UDP) {
774  alert_flags |= PACKET_ALERT_FLAG_TX_GUESSED;
775  }
777  AlertQueueAppendAppTxFromPacket(det_ctx, s, p, tx_id, txd->tx_type, alert_flags);
778  return;
779  }
780  }
781  }
782  AlertQueueAppendPacket(det_ctx, s, p, alert_flags_in);
783 }
784 
785 static inline uint8_t DetectRulePacketRules(ThreadVars *const tv,
786  const DetectEngineCtx *const de_ctx, DetectEngineThreadCtx *const det_ctx, Packet *const p,
787  Flow *const pflow, const DetectRunScratchpad *scratch)
788 {
789  uint8_t action = 0;
790  bool fw_verdict = false;
791  const bool have_fw_rules = EngineModeIsFirewall();
792  const Signature *next_s = NULL;
793 
794  /* inspect the sigs against the packet */
795  /* Prefetch the next signature. */
796  SigIntId match_cnt = det_ctx->match_array_cnt;
797 #ifdef PROFILING
798  if (tv) {
799  StatsCounterAvgAddI64(&tv->stats, det_ctx->counter_match_list, (int64_t)match_cnt);
800  }
801 #endif
802  Signature **match_array = det_ctx->match_array;
803 
804  SGH_PROFILING_RECORD(det_ctx, scratch->sgh);
805 #ifdef PROFILING
806  if (match_cnt >= de_ctx->profile_match_logging_threshold)
807  RulesDumpMatchArray(det_ctx, scratch->sgh, p);
808 #endif
809 
810  bool skip_fw = SkipFwRules(p);
811  uint32_t sflags, next_sflags = 0;
812  if (match_cnt) {
813  next_s = *match_array++;
814  next_sflags = next_s->flags;
815  }
816  while (match_cnt--) {
818  bool break_out_of_packet_filter = false;
819  uint8_t alert_flags = 0;
820 #ifdef PROFILE_RULES
821  bool smatch = false; /* signature match */
822 #endif
823  const Signature *s = next_s;
824  sflags = next_sflags;
825  if (match_cnt) {
826  next_s = *match_array++;
827  next_sflags = next_s->flags;
828  }
829 
830  SCLogDebug("packet %" PRIu64 ": inspecting signature id %" PRIu32 "", PcapPacketCntGet(p),
831  s->id);
832 
833  /* if we accept:hook'd the `packet_filter` hook, we skip the rest of the firewall rules. */
834  if (s->flags & SIG_FLAG_FIREWALL) {
835  if (skip_fw) {
836  SCLogDebug("skipping firewall rule %u", s->id);
837  goto next;
838  }
839  } else if (have_fw_rules) {
840  /* fw mode, we skip anything after the fw rules if:
841  * - flow pass is set
842  * - packet pass (e.g. exception policy) */
844  (pflow != NULL && pflow->flags & (FLOW_ACTION_PASS))) {
845  SCLogDebug("skipping firewall rule %u", s->id);
846  break_out_of_packet_filter = true;
847  goto next;
848  }
849  }
850 
851  if (s->app_inspect != NULL) {
852  goto next; // handle sig in DetectRunTx
853  }
854  if (s->frame_inspect != NULL) {
855  goto next; // handle sig in DetectRunFrame
856  }
857 
858  /* skip pkt sigs for flow end packets */
859  if ((p->flags & PKT_PSEUDO_STREAM_END) != 0 && s->type == SIG_TYPE_PKT)
860  goto next;
861 
862  /* don't run mask check for stateful rules.
863  * There we depend on prefilter */
864  if ((s->mask & p->sig_mask) != s->mask) {
865  SCLogDebug("mask mismatch %x & %x != %x", s->mask, p->sig_mask, s->mask);
866  goto next;
867  }
868 
869  if (SigDsizePrefilter(p, s, sflags))
870  goto next;
871 
872  /* if the sig has alproto and the session as well they should match */
873  if (likely(sflags & SIG_FLAG_APPLAYER)) {
874  if (s->alproto != ALPROTO_UNKNOWN && !AppProtoEquals(s->alproto, scratch->alproto)) {
875  SCLogDebug("alproto mismatch");
876  goto next;
877  }
878  }
879 
880  if (DetectRunInspectRuleHeader(p, pflow, s, sflags) == false) {
881  goto next;
882  }
883 
884  if (!DetectEnginePktInspectionRun(tv, det_ctx, s, pflow, p, &alert_flags)) {
885  goto next;
886  }
887 
888 #ifdef PROFILE_RULES
889  smatch = true;
890 #endif
891  DetectRunPostMatch(tv, det_ctx, p, s);
892 
893  DetectRulePacketAppendAlert(de_ctx, det_ctx, s, p, pflow, alert_flags);
894 
895  if (det_ctx->post_rule_work_queue.len > 0) {
896  /* run post match prefilter engines on work queue */
897  PrefilterPostRuleMatch(det_ctx, scratch->sgh, p, pflow);
898 
899  if (det_ctx->pmq.rule_id_array_cnt > 0) {
900  /* undo "prefetch" */
901  match_array--;
902  SCLogDebug("sig_array_len %u det_ctx->pmq.rule_id_array_cnt %u",
904  const Signature **r = det_ctx->replace;
905  for (uint32_t x = 0; x < match_cnt; x++) {
906  *r++ = match_array[x];
907  SCLogDebug("appended %u", match_array[x]->id);
908  }
909  /* Append the prefilter results, then sort it. The iids are
910  * sorted, so duplicates are adjacent: the engines can report the
911  * same rule for more than one queued item, and det_ctx->replace
912  * only has room for the total number of rules. */
913  SigIntId previous_id = (SigIntId)-1;
914  for (uint32_t x = 0; x < det_ctx->pmq.rule_id_array_cnt; x++) {
915  const SigIntId iid = det_ctx->pmq.rule_id_array[x];
916  SCLogDebug("adding iid %u", iid);
917  if (iid == previous_id) {
918  continue;
919  }
920  previous_id = iid;
921  Signature *ts = de_ctx->sig_array[iid];
922  SCLogDebug("adding id %u", ts->id);
923  if (ts->app_inspect == NULL) {
924  /* det_ctx->replace has one slot per rule: a result that
925  * is already in the list would be written past its end. */
926  if (unlikely(match_cnt >= de_ctx->sig_array_len)) {
927  break;
928  }
929  *r++ = ts;
930  match_cnt++;
931  }
932  }
933  if (match_cnt > 1) {
934  qsort(det_ctx->replace, match_cnt, sizeof(Signature *), SortHelper);
935  }
936  /* rewrite match_array to include the new additions, and deduplicate
937  * while at it. */
938  Signature **m = match_array;
939  Signature *last_sig = NULL;
940  uint32_t skipped = 0;
941  for (uint32_t x = 0; x < match_cnt; x++) {
942  /* de-duplicate */
943  if (last_sig == *m) {
944  skipped++;
945  continue;
946  }
947  last_sig = *m;
948  *m++ = (Signature *)det_ctx->replace[x];
949  }
950  match_cnt -= skipped;
951  /* prefetch next */
952  next_s = *match_array++;
953  next_sflags = next_s->flags;
954  SCLogDebug("%u rules added", det_ctx->pmq.rule_id_array_cnt);
955  }
956  /* Consume the queue in all cases: a rule group without post-rule
957  * match engines leaves pmq empty, and then the work of earlier
958  * packets would pile up until it no longer fits. */
959  det_ctx->post_rule_work_queue.len = 0;
960  PMQ_RESET(&det_ctx->pmq);
961  }
962 
963  /* firewall logic in the packet:filter table:
964  * 1. firewall rules preceed the packet:td rules in the list
965  * 2. if no rule issues an accept, we drop
966  * 3. drop is immediate
967  * 4. accept:
968  * - hook: skip rest of fw rules, inspect packet:td rules
969  * - packet: immediate accept, no packet:td or app:* inspect
970  * - flow: as packet, but applied to all future packets in the
971  * flow as well
972  */
973  if (s->flags & SIG_FLAG_FIREWALL) {
974  if (s->action & (ACTION_ACCEPT)) {
975  fw_verdict = true;
976 
977  enum ActionScope as = s->action_scope;
978  if (as == ACTION_SCOPE_HOOK) {
979  /* accept:hook: jump to first TD. Implemented as:
980  * skip until the first TD rule.
981  * Don't update action as we're just continuing to the next hook. */
982  skip_fw = true;
983 
984  } else if (as == ACTION_SCOPE_PACKET) {
985  /* accept:packet: break loop, return accept */
986  action |= s->action;
987  skip_fw = true;
988 
989  } else if (as == ACTION_SCOPE_FLOW) {
990  /* accept:flow: break loop, return accept */
991  action |= s->action;
992  skip_fw = true;
993 
994  /* set immediately, as we're in hook "packet_filter" */
995  if (pflow) {
996  pflow->flags |= FLOW_ACTION_ACCEPT;
997  }
998  }
999  } else if (s->action & ACTION_DROP) {
1000  /* apply a drop immediately here */
1001  fw_verdict = true;
1002  action |= s->action;
1003  break_out_of_packet_filter = true;
1004  }
1005  }
1006 next:
1007  DetectVarProcessList(det_ctx, pflow, p);
1008  DetectReplaceFree(det_ctx);
1009  RULE_PROFILING_END(det_ctx, s, smatch, p);
1010 
1011  /* fw drop means we're done here */
1012  if (break_out_of_packet_filter)
1013  break;
1014 
1015  continue;
1016  }
1017 
1018  /* if no rule told us to accept, and no rule explicitly dropped, we invoke the default drop
1019  * policy
1020  */
1021  if (have_fw_rules) {
1022  if (skip_fw || fw_verdict) {
1023  /* apply fw action */
1024  p->action |= action;
1025  } else {
1027  /* non-final call as we may have to consider app-layer still */
1028  action |= DetectRunApplyPacketPolicy(de_ctx, det_ctx, scratch->fw_pkt_policy, p, false);
1029  }
1030  }
1031  return action;
1032 }
1033 
1034 /** \internal
1035  * \param fw_pkt_policy policy to apply to packet rules
1036  */
1037 static DetectRunScratchpad DetectRunSetup(const DetectEngineCtx *de_ctx,
1038  DetectEngineThreadCtx *det_ctx, Packet *const p, Flow *const pflow,
1039  const enum DetectFirewallPacketPolicies fw_pkt_policy)
1040 {
1041  AppProto alproto = ALPROTO_UNKNOWN;
1042  uint8_t flow_flags = 0; /* flow/state flags */
1043  bool app_decoder_events = false;
1044 
1046 
1047 #ifdef UNITTESTS
1048  if (RunmodeIsUnittests()) {
1049  p->alerts.cnt = 0;
1050  p->alerts.discarded = 0;
1051  p->alerts.suppressed = 0;
1052  }
1053 #endif
1054  det_ctx->filestore_cnt = 0;
1055  det_ctx->base64_decoded_len = 0;
1056  det_ctx->raw_stream_progress = 0;
1057  det_ctx->match_array_cnt = 0;
1058  det_ctx->json_content_len = 0;
1059  /* a fresh (zeroed) context has byte_values_max == 0, which would
1060  * walk a bogus slot 0 in the per-run clear: start with none written */
1061  det_ctx->byte_values_max = -1;
1062 
1063  det_ctx->alert_queue_size = 0;
1066 
1067 #ifdef DEBUG
1068  if (p->flags & PKT_STREAM_ADD) {
1069  det_ctx->pkt_stream_add_cnt++;
1070  }
1071 #endif
1072 
1073  /* grab the protocol state we will detect on */
1074  if (p->flags & PKT_HAS_FLOW) {
1075  DEBUG_VALIDATE_BUG_ON(pflow == NULL);
1076 
1077  if (p->flowflags & FLOW_PKT_TOSERVER) {
1078  flow_flags = STREAM_TOSERVER;
1079  SCLogDebug("flag STREAM_TOSERVER set");
1080  } else if (p->flowflags & FLOW_PKT_TOCLIENT) {
1081  flow_flags = STREAM_TOCLIENT;
1082  SCLogDebug("flag STREAM_TOCLIENT set");
1083  }
1084  SCLogDebug("p->flowflags 0x%02x", p->flowflags);
1085 
1086  if (p->flags & PKT_PSEUDO_STREAM_END) {
1087  flow_flags |= STREAM_EOF;
1088  SCLogDebug("STREAM_EOF set");
1089  }
1090 
1091  /* store tenant_id in the flow so that we can use it
1092  * for creating pseudo packets */
1093  if (p->tenant_id > 0 && pflow->tenant_id == 0) {
1094  pflow->tenant_id = p->tenant_id;
1095  }
1096 
1097  /* live ruleswap check for flow updates */
1098  if (pflow->de_ctx_version == 0) {
1099  /* first time this flow is inspected, set id */
1100  pflow->de_ctx_version = de_ctx->version;
1101  } else if (pflow->de_ctx_version != de_ctx->version) {
1102  /* first time we inspect flow with this de_ctx, reset */
1103  pflow->flags &= ~FLOW_SGH_TOSERVER;
1104  pflow->flags &= ~FLOW_SGH_TOCLIENT;
1105  pflow->sgh_toserver = NULL;
1106  pflow->sgh_toclient = NULL;
1107 
1108  pflow->de_ctx_version = de_ctx->version;
1109  SCGenericVarFree(pflow->flowvar);
1110  pflow->flowvar = NULL;
1111 
1113  }
1114 
1115  /* Retrieve the app layer state and protocol and the tcp reassembled
1116  * stream chunks. */
1117  if ((p->proto == IPPROTO_TCP && (p->flags & PKT_STREAM_EST)) ||
1118  (p->proto == IPPROTO_UDP) ||
1120  {
1121  /* update flow flags with knowledge on disruptions */
1122  flow_flags = FlowGetDisruptionFlags(pflow, flow_flags);
1123  alproto = SCFlowGetAppProtocol(pflow);
1124  if (p->proto == IPPROTO_TCP && pflow->protoctx &&
1127  }
1128  SCLogDebug("alproto %u", alproto);
1129  } else {
1130  SCLogDebug("packet doesn't have established flag set (proto %d)", p->proto);
1131  }
1132 
1133  app_decoder_events = AppLayerParserHasDecoderEvents(pflow->alparser);
1134  }
1135 
1136  DetectRunScratchpad pad = { alproto, flow_flags, app_decoder_events, fw_pkt_policy, NULL };
1138  return pad;
1139 }
1140 
1141 static inline void DetectRunPostRules(ThreadVars *tv, const DetectEngineCtx *de_ctx,
1142  DetectEngineThreadCtx *det_ctx, Packet *const p, Flow *const pflow,
1143  DetectRunScratchpad *scratch)
1144 {
1145  /* so now let's iterate the alerts and remove the ones after a pass rule
1146  * matched (if any). This is done inside PacketAlertFinalize() */
1147  /* PR: installed "tag" keywords are handled after the threshold inspection */
1148 
1150  PacketAlertFinalize(de_ctx, det_ctx, p);
1151  if (p->alerts.cnt > 0) {
1152  StatsCounterAddI64(&tv->stats, det_ctx->counter_alerts, (uint64_t)p->alerts.cnt);
1153  }
1154  if (p->alerts.discarded > 0) {
1156  &tv->stats, det_ctx->counter_alerts_overflow, (uint64_t)p->alerts.discarded);
1157  }
1158  if (p->alerts.firewall_discarded > 0) {
1160  (uint64_t)p->alerts.firewall_discarded);
1161  }
1162  if (p->alerts.suppressed > 0) {
1164  &tv->stats, det_ctx->counter_alerts_suppressed, (uint64_t)p->alerts.suppressed);
1165  }
1167 
1168  /* firewall: "fail" closed if we don't have an ACCEPT. This can happen
1169  * if there was no rule group. */
1170  // TODO review packet src types here
1171  if (EngineModeIsFirewall() && ((p->action & (ACTION_ACCEPT | ACTION_DROP)) == 0) &&
1172  p->pkt_src == PKT_SRC_WIRE) {
1173  SCLogDebug("packet %" PRIu64 ": default action as no verdict set %02x (pkt %s)",
1175  (void)DetectRunApplyPacketPolicy(de_ctx, det_ctx, scratch->fw_pkt_policy, p, true);
1177  }
1178 }
1179 
1180 static void DetectByteValuesClear(DetectEngineThreadCtx *det_ctx)
1181 {
1182  /* clear the "written this run" marker of the byte_extract slots
1183  * this run wrote, so the next run's scripts read them as unset;
1184  * the value itself persists - the content modifiers (depth,
1185  * offset, ...) read it without the marker, as before the marker
1186  * existed. Only slots up to the run's highest written index are
1187  * walked, and only set slots are touched. A set slot without a
1188  * writer means a write site is buggy. NULL: stub multi-tenant
1189  * thread contexts never allocate the array (and never run
1190  * extractors). */
1191  if (det_ctx->byte_values != NULL && det_ctx->byte_values_max >= 0) {
1192  for (int32_t i = 0; i <= det_ctx->byte_values_max; i++) {
1193  if (det_ctx->byte_values[i].set) {
1194  DEBUG_VALIDATE_BUG_ON(det_ctx->byte_values[i].writer == NULL);
1195  det_ctx->byte_values[i].set = 0;
1196  det_ctx->byte_values[i].writer = NULL;
1197  }
1198  }
1199  det_ctx->byte_values_max = -1;
1200  }
1201 }
1202 
1203 static void DetectRunCleanup(DetectEngineThreadCtx *det_ctx,
1204  Packet *p, Flow * const pflow)
1205 {
1207  InspectionBufferClean(det_ctx);
1208 
1209  DetectByteValuesClear(det_ctx);
1210 
1211  if (pflow != NULL) {
1212  /* update inspected tracker for raw reassembly */
1213  if (p->proto == IPPROTO_TCP && pflow->protoctx != NULL &&
1216  det_ctx->raw_stream_progress);
1217  }
1218  }
1220  SCReturn;
1221 }
1222 
1224  DETECT_TX_FW_FC_OK = 0, /**< continue to next rule */
1225  DETECT_TX_FW_FC_SKIP = 1, /**< skip this rule, continue to next */
1226  DETECT_TX_FW_FC_BREAK = 2, /**< break rule loop */
1227 };
1228 
1230 {
1232  det_ctx->tx_candidates = SCCalloc(size, sizeof(RuleMatchCandidateTx));
1233  if (det_ctx->tx_candidates == NULL) {
1234  FatalError("failed to allocate %" PRIu64 " bytes",
1235  (uint64_t)(size * sizeof(RuleMatchCandidateTx)));
1236  }
1237  det_ctx->tx_candidates_size = size;
1238  SCLogDebug("array initialized to %u elements (%"PRIu64" bytes)",
1239  size, (uint64_t)(size * sizeof(RuleMatchCandidateTx)));
1240 }
1241 
1243 {
1244  SCFree(det_ctx->tx_candidates);
1245  det_ctx->tx_candidates_size = 0;
1246 }
1247 
1248 /* if size >= cur_space */
1249 static inline bool RuleMatchCandidateTxArrayHasSpace(const DetectEngineThreadCtx *det_ctx,
1250  const uint32_t need)
1251 {
1252  if (det_ctx->tx_candidates_size >= need)
1253  return 1;
1254  return 0;
1255 }
1256 
1257 /* realloc */
1258 static int RuleMatchCandidateTxArrayExpand(DetectEngineThreadCtx *det_ctx, const uint32_t needed)
1259 {
1260  const uint32_t old_size = det_ctx->tx_candidates_size;
1261  uint32_t new_size = needed;
1262  void *ptmp = SCRealloc(det_ctx->tx_candidates, (new_size * sizeof(RuleMatchCandidateTx)));
1263  if (ptmp == NULL) {
1264  FatalError("failed to expand to %" PRIu64 " bytes",
1265  (uint64_t)(new_size * sizeof(RuleMatchCandidateTx)));
1266  // TODO can this be handled more gracefully?
1267  }
1268  det_ctx->tx_candidates = ptmp;
1269  det_ctx->tx_candidates_size = new_size;
1270  SCLogDebug("array expanded from %u to %u elements (%"PRIu64" bytes -> %"PRIu64" bytes)",
1271  old_size, new_size, (uint64_t)(old_size * sizeof(RuleMatchCandidateTx)),
1272  (uint64_t)(new_size * sizeof(RuleMatchCandidateTx))); (void)old_size;
1273  return 1;
1274 }
1275 
1276 /** \internal
1277  * \brief sort helper for sorting match candidates by id: ascending
1278  *
1279  * The id field is set from Signature::num, so we sort the candidates to match the signature
1280  * sort order (ascending), where candidates that have flags go first.
1281  */
1282 static int
1283 DetectRunTxSortHelper(const void *a, const void *b)
1284 {
1285  const RuleMatchCandidateTx *s0 = a;
1286  const RuleMatchCandidateTx *s1 = b;
1287  if (s1->id == s0->id) {
1288  if (s1->flags && !s0->flags)
1289  return 1;
1290  else if (!s1->flags && s0->flags)
1291  return -1;
1292  return 0;
1293  } else
1294  return s0->id > s1->id ? 1 : -1;
1295 }
1296 
1297 #if 0
1298 #define TRACE_SID_TXS(sid,txs,...) \
1299  do { \
1300  char _trace_buf[2048]; \
1301  snprintf(_trace_buf, sizeof(_trace_buf), __VA_ARGS__); \
1302  SCLogNotice("%p/%"PRIu64"/%u: %s", txs->tx_ptr, txs->tx_id, sid, _trace_buf); \
1303  } while(0)
1304 #else
1305 #define TRACE_SID_TXS(sid,txs,...)
1306 #endif
1307 
1308 /** \internal
1309  * \brief get correct transaction pointer
1310  *
1311  * Gets an encapsulated DNS transaction in the DOH2 case.
1312  *
1313  * Returns NULL is the TX is not to be inspected by this engine.
1314  */
1315 void *DetectGetInnerTx(void *tx_ptr, AppProto alproto, AppProto engine_alproto, uint8_t flow_flags)
1316 {
1317  SCLogDebug("pre: tx_ptr %p flow::alproto %s engine::alproto %s", tx_ptr,
1318  AppProtoToString(alproto), AppProtoToString(engine_alproto));
1319  if (unlikely(alproto == ALPROTO_DOH2)) {
1320  switch (engine_alproto) {
1321  case ALPROTO_DOH2:
1322  /* need to get the dns tx pointer */
1323  tx_ptr = SCDoH2GetDnsTx(tx_ptr, flow_flags);
1324  break;
1325  case ALPROTO_HTTP2:
1326  case ALPROTO_UNKNOWN:
1327  /* tx_ptr is untouched, so use outer (HTTP/2) layer */
1328  break;
1329  default:
1330  /* any other protocol is a mismatch with DOH2 */
1331  tx_ptr = NULL;
1332  break;
1333  }
1334  } else if (engine_alproto != alproto && engine_alproto != ALPROTO_UNKNOWN) {
1335  /* incompatible engine->alproto with flow alproto */
1336  tx_ptr = NULL;
1337  }
1338  return tx_ptr;
1339 }
1340 
1341 /** \internal
1342  * \brief inspect a rule against a transaction
1343  *
1344  * Inspect a rule. New detection or continued stateful
1345  * detection.
1346  *
1347  * \param stored_flags pointer to stored flags or NULL.
1348  * If stored_flags is set it means we're continuing
1349  * inspection from an earlier run.
1350  *
1351  * \retval 1 sig matched
1352  * \retval 0 partial incomplete match
1353  * \retval -1 failed to match
1354  * \retval -2 no match yet, but not final: the MPM engine will revisit the
1355  * tx as it progresses. The caller must not apply a hook default
1356  * policy for it: that could decide the packet before the revisit
1357  * can turn it into a match.
1358  */
1359 static int DetectRunTxInspectRule(ThreadVars *tv, DetectEngineCtx *de_ctx,
1360  DetectEngineThreadCtx *det_ctx, Packet *p, Flow *f,
1361  const uint8_t in_flow_flags, // direction, EOF, etc
1362  void *alstate, DetectTransaction *tx, const Signature *s, uint32_t *stored_flags,
1364 {
1365  const uint8_t flow_flags = in_flow_flags;
1366  const int direction = (flow_flags & STREAM_TOSERVER) ? 0 : 1;
1367  uint32_t inspect_flags = stored_flags ? *stored_flags : 0;
1368  int total_matches = 0;
1369  uint16_t file_no_match = 0;
1370  bool mpm_before_progress = false; // is mpm engine before progress?
1371  bool mpm_in_progress = false; // is mpm engine in a buffer we will revisit?
1372 
1373  TRACE_SID_TXS(s->id, tx, "starting %s", direction ? "toclient" : "toserver");
1374 
1375  /* for a new inspection we inspect pkt header and packet matches */
1376  if (likely(stored_flags == NULL)) {
1377  TRACE_SID_TXS(s->id, tx, "first inspect, run packet matches");
1378  /* the firewall LTE coverage build already ran the same header check for
1379  * this candidate, so skip repeating it */
1380  if (!can->fw_lte_header_ok && DetectRunInspectRuleHeader(p, f, s, s->flags) == false) {
1381  TRACE_SID_TXS(s->id, tx, "DetectRunInspectRuleHeader() no match");
1382  return -1;
1383  }
1384  if (!DetectEnginePktInspectionRun(tv, det_ctx, s, f, p, NULL)) {
1385  TRACE_SID_TXS(s->id, tx, "DetectEnginePktInspectionRun no match");
1386  return -1;
1387  }
1388  /* stream mpm and negated mpm sigs can end up here with wrong proto */
1389  if (!(AppProtoEquals(s->alproto, f->alproto) || s->alproto == ALPROTO_UNKNOWN)) {
1390  TRACE_SID_TXS(s->id, tx, "alproto mismatch");
1391  return -1;
1392  }
1393  } else {
1394  TRACE_SID_TXS(s->id, tx, "continue, inspect_flags %x", inspect_flags);
1395  }
1396 
1397  const DetectEngineAppInspectionEngine *engine = s->app_inspect;
1398  do {
1399  TRACE_SID_TXS(s->id, tx, "engine %p inspect_flags %x", engine, inspect_flags);
1400 
1401  // also if it is not the same direction, but
1402  // this is a transactional signature, and we are toclient
1403  if (!(inspect_flags & BIT_U32(engine->id)) &&
1404  (direction == engine->dir || ((s->flags & SIG_FLAG_TXBOTHDIR) && direction == 1))) {
1405 
1407  AppLayerParserSupportsSubStates(engine->alproto) && engine->sub_state == 0);
1409  !AppLayerParserSupportsSubStates(engine->alproto) && engine->sub_state != 0);
1410 
1411  if (engine->alproto != ALPROTO_UNKNOWN && // app-layer-events is registered for each
1412  // proto this way
1413  tx->tx_type != engine->sub_state) {
1414  TRACE_SID_TXS(s->id, tx,
1415  "skip because engine alproto %s sub_state %u != tx_type %u (engine "
1416  "progress %u)",
1417  AppProtoToString(engine->alproto), engine->sub_state, tx->tx_type,
1418  engine->progress);
1419  engine = engine->next;
1420  continue;
1421  }
1422  TRACE_SID_TXS(s->id, tx,
1423  "inspecting engine alproto %s sub_state %u == tx_type %u (engine progress %u)",
1424  AppProtoToString(engine->alproto), engine->sub_state, tx->tx_type,
1425  engine->progress);
1426 
1427  void *tx_ptr = DetectGetInnerTx(tx->tx_ptr, f->alproto, engine->alproto, flow_flags);
1428  if (tx_ptr == NULL) {
1429  TRACE_SID_TXS(s->id, tx, "no tx_ptr after DetectGetInnerTx");
1430  if (engine->alproto != ALPROTO_UNKNOWN) {
1431  TRACE_SID_TXS(s->id, tx, "no tx_ptr skip engine");
1432  /* special case: file_data on 'alert tcp' will have engines
1433  * in the list that are not for us. */
1434  engine = engine->next;
1435  continue;
1436  } else {
1437  tx_ptr = tx->tx_ptr;
1438  }
1439  }
1440  TRACE_SID_TXS(s->id, tx, "tx_ptr %p", tx_ptr);
1441 
1442  /* engines are sorted per progress, except that the one with
1443  * mpm/prefilter enabled is first */
1444  if (tx->tx_progress < engine->progress) {
1445  SCLogDebug("tx progress %d < engine progress %d",
1446  tx->tx_progress, engine->progress);
1447  break;
1448  }
1449  if (engine->mpm) {
1450  if (tx->tx_progress > engine->progress) {
1451  TRACE_SID_TXS(s->id, tx,
1452  "engine->mpm: t->tx_progress %u > engine->progress %u, so set "
1453  "mpm_before_progress",
1454  tx->tx_progress, engine->progress);
1455  mpm_before_progress = true;
1456  } else if (tx->tx_progress == engine->progress) {
1457  TRACE_SID_TXS(s->id, tx,
1458  "engine->mpm: t->tx_progress %u == engine->progress %u, so set "
1459  "mpm_in_progress",
1460  tx->tx_progress, engine->progress);
1461  if ((p->flags & PKT_PSEUDO_DETECTLOG_FLUSH) == 0) {
1462  mpm_in_progress = true;
1463  }
1464  }
1465  }
1466 
1467  uint8_t engine_flags = flow_flags;
1468  if (direction != engine->dir) {
1469  engine_flags = flow_flags ^ (STREAM_TOCLIENT | STREAM_TOSERVER);
1470  }
1471  /* run callback: but bypass stream callback if we can */
1472  uint8_t match;
1473  if (unlikely(engine->stream && can->stream_stored)) {
1474  match = can->stream_result;
1475  TRACE_SID_TXS(s->id, tx, "stream skipped, stored result %d used instead", match);
1476  } else if (engine->v2.Callback == NULL) {
1477  /* TODO is this the cleanest way to support a non-app sig on a app hook? */
1478 
1479  if (tx->tx_progress > engine->progress) {
1480  mpm_before_progress = true; // TODO needs a new name now
1481  }
1482 
1483  /* we don't have to store a "hook" match, also don't want to keep any state to make
1484  * sure the hook gets invoked again until tx progress progresses. */
1485  if ((s->flags & SIG_FLAG_FW_HOOK_LTE) == 0 && tx->tx_progress <= engine->progress) {
1486  return 1; // DETECT_ENGINE_INSPECT_SIG_MATCH;
1487  }
1488 
1489  /* if progress > engine progress, track state to avoid additional matches */
1491  } else {
1492  KEYWORD_PROFILING_SET_LIST(det_ctx, engine->sm_list);
1493  DEBUG_VALIDATE_BUG_ON(engine->v2.Callback == NULL);
1494  match = engine->v2.Callback(
1495  de_ctx, det_ctx, engine, s, f, engine_flags, alstate, tx_ptr, tx->tx_id);
1496  TRACE_SID_TXS(s->id, tx, "engine %p match %d", engine, match);
1497  if (engine->stream) {
1498  can->stream_stored = true;
1499  can->stream_result = match;
1500  TRACE_SID_TXS(s->id, tx, "stream ran, store result %d for next tx (if any)", match);
1501  }
1502  }
1503  if (match == DETECT_ENGINE_INSPECT_SIG_MATCH) {
1504  inspect_flags |= BIT_U32(engine->id);
1505  engine = engine->next;
1506  total_matches++;
1507  continue;
1508  } else if (match == DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES) {
1509  /* if the file engine matched, but indicated more
1510  * files are still in progress, we don't set inspect
1511  * flags as these would end inspection for this tx */
1512  engine = engine->next;
1513  total_matches++;
1514  continue;
1515  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH) {
1516  inspect_flags |= DE_STATE_FLAG_SIG_CANT_MATCH;
1517  inspect_flags |= BIT_U32(engine->id);
1518  } else if (match == DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES) {
1519  inspect_flags |= DE_STATE_FLAG_SIG_CANT_MATCH;
1520  inspect_flags |= BIT_U32(engine->id);
1521  file_no_match = 1;
1522  }
1523  /* implied DETECT_ENGINE_INSPECT_SIG_NO_MATCH */
1524  if (engine->mpm && mpm_before_progress) {
1525  inspect_flags |= DE_STATE_FLAG_SIG_CANT_MATCH;
1526  inspect_flags |= BIT_U32(engine->id);
1527  }
1528  break;
1529  } else if (!(inspect_flags & BIT_U32(engine->id)) && s->flags & SIG_FLAG_TXBOTHDIR &&
1530  direction != engine->dir) {
1531  TRACE_SID_TXS(s->id, tx, "handle bidir engine");
1532 
1533  // for transactional rules, the engines on the opposite direction
1534  // are ordered by progress on the different side
1535  // so we have a two mixed-up lists, and we skip the elements
1536  if (direction == 0 && engine->next == NULL) {
1537  // do not match yet on request only
1538  break;
1539  }
1540  engine = engine->next;
1541  continue;
1542  }
1543 
1544  engine = engine->next;
1545  } while (engine != NULL);
1546  TRACE_SID_TXS(s->id, tx, "inspect_flags %x, total_matches %u, engine %p",
1547  inspect_flags, total_matches, engine);
1548 
1549  bool full_match = false;
1550  if (engine == NULL && total_matches) {
1551  inspect_flags |= DE_STATE_FLAG_FULL_INSPECT;
1552  TRACE_SID_TXS(s->id, tx, "MATCH");
1553  full_match = true;
1554  }
1555 
1556  if (stored_flags) {
1557  *stored_flags = inspect_flags;
1558  TRACE_SID_TXS(s->id, tx, "continue inspect flags %08x", inspect_flags);
1559  } else {
1560  // store... or? If tx is done we might not want to come back to this tx
1561 
1562  // also... if mpmid tracking is enabled, we won't do a sig again for this tx...
1563  TRACE_SID_TXS(s->id, tx, "start inspect flags %08x", inspect_flags);
1564  if (inspect_flags & DE_STATE_FLAG_SIG_CANT_MATCH) {
1565  if (file_no_match) {
1566  /* if we have a mismatch on a file sig, we need to keep state.
1567  * We may get another file on the same tx (for http and smtp
1568  * at least), so for a new file we need to re-eval the sig.
1569  * Thoughts / TODO:
1570  * - not for some protos that have 1 file per tx (e.g. nfs)
1571  * - maybe we only need this for file sigs that mix with
1572  * other matches? E.g. 'POST + filename', is different than
1573  * just 'filename'.
1574  */
1575  DetectRunStoreStateTx(scratch->sgh, f, tx->tx_ptr, tx->tx_id, s,
1576  inspect_flags, flow_flags, file_no_match);
1577  }
1578  } else if ((inspect_flags & DE_STATE_FLAG_FULL_INSPECT) && mpm_before_progress) {
1579  TRACE_SID_TXS(s->id, tx, "no need to store match sig, "
1580  "mpm won't trigger for it anymore");
1581 
1582  if (inspect_flags & DE_STATE_FLAG_FILE_INSPECT) {
1583  TRACE_SID_TXS(s->id, tx, "except that for new files, "
1584  "we may have to revisit anyway");
1585  DetectRunStoreStateTx(scratch->sgh, f, tx->tx_ptr, tx->tx_id, s,
1586  inspect_flags, flow_flags, file_no_match);
1587  }
1588  } else if ((inspect_flags & DE_STATE_FLAG_FULL_INSPECT) == 0 && mpm_in_progress) {
1589  TRACE_SID_TXS(s->id, tx, "no need to store no-match sig, "
1590  "mpm will revisit it");
1591  return -2; /* no match, but not final: mpm will revisit it */
1592  } else if (inspect_flags != 0 || file_no_match != 0) {
1593  TRACE_SID_TXS(s->id, tx, "storing state: flags %08x", inspect_flags);
1594  DetectRunStoreStateTx(scratch->sgh, f, tx->tx_ptr, tx->tx_id, s,
1595  inspect_flags, flow_flags, file_no_match);
1596  } else {
1597  if (inspect_flags == 0) {
1598  TRACE_SID_TXS(s->id, tx, "no match: inspect_flags %08x", inspect_flags);
1599  return -1;
1600  }
1601  }
1602  }
1603  if (full_match) {
1604  return 1;
1605  /* can't be a partial match if we're at the end state */
1606  } else if ((inspect_flags & DE_STATE_FLAG_SIG_CANT_MATCH) == 0 &&
1607  tx->tx_progress < tx->tx_end_state) {
1608  return 0;
1609  } else {
1610  return -1;
1611  }
1612 }
1613 
1614 #define NO_TX \
1615  { \
1616  NULL, 0, NULL, NULL, 0, 0, 0, 0, false, 0, \
1617  }
1618 
1619 /** \internal
1620  * \brief get a DetectTransaction object
1621  * \retval struct filled with relevant info or all nulls/0s
1622  */
1623 static DetectTransaction GetDetectTx(const uint8_t ipproto, const AppProto alproto,
1624  const uint64_t tx_id, void *tx_ptr, const uint8_t flow_flags)
1625 {
1626  AppLayerTxData *txd = AppLayerParserGetTxData(ipproto, alproto, tx_ptr);
1627  const uint8_t tx_progress =
1628  (uint8_t)AppLayerParserGetStateProgress(ipproto, alproto, tx_ptr, flow_flags);
1630 
1631  const uint8_t e_tx_end_state =
1632  AppLayerParserGetTxEndState(ipproto, alproto, tx_ptr, flow_flags);
1633  DEBUG_VALIDATE_BUG_ON(e_tx_end_state >= APP_LAYER_MAX_PROGRESS);
1634 
1635  bool updated = (flow_flags & STREAM_TOSERVER) ? txd->updated_ts : txd->updated_tc;
1636  if (!updated && tx_progress < e_tx_end_state && ((flow_flags & STREAM_EOF) == 0)) {
1637  DetectTransaction no_tx = NO_TX;
1638  return no_tx;
1639  }
1640  const uint8_t inspected_flag =
1641  (flow_flags & STREAM_TOSERVER) ? APP_LAYER_TX_INSPECTED_TS : APP_LAYER_TX_INSPECTED_TC;
1642  if (unlikely(txd->flags & inspected_flag)) {
1643  SCLogDebug("%" PRIu64 " tx already fully inspected for %s. Flags %02x", tx_id,
1644  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient", txd->flags);
1645  DetectTransaction no_tx = NO_TX;
1646  return no_tx;
1647  }
1648  const uint8_t skip_flag = (flow_flags & STREAM_TOSERVER) ? APP_LAYER_TX_SKIP_INSPECT_TS
1649  : APP_LAYER_TX_SKIP_INSPECT_TC;
1650  if (unlikely(txd->flags & skip_flag)) {
1651  SCLogDebug("%" PRIu64 " tx should not be inspected in direction %s. Flags %02x", tx_id,
1652  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient", txd->flags);
1653  DetectTransaction no_tx = NO_TX;
1654  return no_tx;
1655  }
1656 
1657  if (txd->tx_type != 0) {
1658  SCLogDebug("using tx_type %u", txd->tx_type);
1659  }
1660 
1661  const uint8_t detect_progress =
1662  (flow_flags & STREAM_TOSERVER) ? txd->detect_progress_ts : txd->detect_progress_tc;
1663 
1664  const int dir_int = (flow_flags & STREAM_TOSERVER) ? 0 : 1;
1665  DetectEngineState *tx_de_state = txd->de_state;
1666  DetectEngineStateDirection *tx_dir_state =
1667  tx_de_state ? &tx_de_state->dir_state[dir_int] : NULL;
1668  DetectTransaction tx = {
1669  .tx_ptr = tx_ptr,
1670  .tx_id = tx_id,
1671  .tx_data_ptr = (struct AppLayerTxData *)txd,
1672  .de_state = tx_dir_state,
1673  .detect_progress = detect_progress,
1674  .detect_progress_orig = detect_progress,
1675  .tx_progress = (uint8_t)tx_progress,
1676  .tx_end_state = e_tx_end_state,
1677  .is_last = false,
1678  .tx_type = txd->tx_type,
1679  };
1680  return tx;
1681 }
1682 
1683 static inline void StoreDetectProgress(
1684  DetectTransaction *tx, const uint8_t flow_flags, const uint8_t progress)
1685 {
1687  if (flow_flags & STREAM_TOSERVER) {
1688  txd->detect_progress_ts = progress;
1689  } else {
1690  txd->detect_progress_tc = progress;
1691  }
1692 }
1693 
1694 // Merge 'state' rules from the regular prefilter
1695 // updates array_idx on the way
1696 static inline void RuleMatchCandidateMergeStateRules(
1697  DetectEngineThreadCtx *det_ctx, uint32_t *array_idx, bool *fw_lte)
1698 {
1699  // Now, we will merge 2 sorted lists :
1700  // the one in det_ctx->tx_candidates
1701  // and the one in det_ctx->match_array
1702  // For match_array, we take only the relevant elements where s->app_inspect != NULL
1703 
1704  // Basically, we iterate at the same time over the 2 lists
1705  // comparing and taking an element from either.
1706 
1707  // Trick is to do so in place in det_ctx->tx_candidates,
1708  // so as to minimize the number of moves in det_ctx->tx_candidates.
1709  // For this, the algorithm traverses the lists in reverse order.
1710  // Otherwise, if the first element of match_array was to be put before
1711  // all tx_candidates, we would need to shift all tx_candidates
1712 
1713  // Retain the number of elements sorted in tx_candidates before merge
1714  uint32_t j = *array_idx;
1715  // First loop only counting the number of elements to add
1716  for (uint32_t i = 0; i < det_ctx->match_array_cnt; i++) {
1717  const Signature *s = det_ctx->match_array[i];
1718  if (s->app_inspect != NULL) {
1719  if (s->flags & SIG_FLAG_FW_HOOK_LTE) {
1720  *fw_lte = true;
1721  }
1722  (*array_idx)++;
1723  }
1724  }
1725  // Future number of elements in tx_candidates after merge
1726  uint32_t k = *array_idx;
1727 
1728  if (k == j) {
1729  // no new element from match_array to merge in tx_candidates
1730  return;
1731  }
1732 
1733  // variable i is for all elements of match_array (even not relevant ones)
1734  // variable j is for elements of tx_candidates before merge
1735  // variable k is for elements of tx_candidates after merge
1736  for (uint32_t i = det_ctx->match_array_cnt; i > 0;) {
1737  const Signature *s = det_ctx->match_array[i - 1];
1738  if (s->app_inspect == NULL) {
1739  // no relevant element, get the next one from match_array
1740  i--;
1741  continue;
1742  }
1743  // we have one element from match_array to merge in tx_candidates
1744  k--;
1745  if (j > 0) {
1746  // j > 0 means there is still at least one element in tx_candidates to merge
1747  const RuleMatchCandidateTx *s0 = &det_ctx->tx_candidates[j - 1];
1748  if (s->iid <= s0->id) {
1749  // get next element from previous tx_candidates
1750  j--;
1751  // take the element from tx_candidates before merge
1752  det_ctx->tx_candidates[k].s = det_ctx->tx_candidates[j].s;
1753  det_ctx->tx_candidates[k].id = det_ctx->tx_candidates[j].id;
1754  det_ctx->tx_candidates[k].fw_lte_counted = det_ctx->tx_candidates[j].fw_lte_counted;
1755  det_ctx->tx_candidates[k].fw_lte_header_ok =
1756  det_ctx->tx_candidates[j].fw_lte_header_ok;
1757  det_ctx->tx_candidates[k].flags = det_ctx->tx_candidates[j].flags;
1758  det_ctx->tx_candidates[k].stream_reset = det_ctx->tx_candidates[j].stream_reset;
1759  continue;
1760  }
1761  } // otherwise
1762  // get next element from match_array
1763  i--;
1764  // take the element from match_array
1765  det_ctx->tx_candidates[k].s = s;
1766  det_ctx->tx_candidates[k].id = s->iid;
1767  det_ctx->tx_candidates[k].fw_lte_counted = false;
1768  det_ctx->tx_candidates[k].fw_lte_header_ok = false;
1769  det_ctx->tx_candidates[k].flags = NULL;
1770  det_ctx->tx_candidates[k].stream_reset = 0;
1771  }
1772  // Even if k > 0 or j > 0, the loop is over. (Note that j == k now)
1773  // The remaining elements in tx_candidates up to k were already sorted
1774  // and come before any other element later in the list
1775 }
1776 
1783  bool last_fw_rule; /**< processing the last fw rule, so we need to eval all hooks after it. */
1784  /** det_ctx->fw_lte_cover holds the counts for this walk. The counts answer
1785  * "does another pending rule cover hook H": per hook, how many pending LTE
1786  * rules cover it (their own hook and every prior hook). Progress values
1787  * stay under APP_LAYER_MAX_PROGRESS. */
1789 };
1790 
1791 /** \internal
1792  * \brief initialize the per-thread LTE hook coverage for this walk.
1793  *
1794  * The counts live in the thread ctx so IDS traffic does not pay to clear them.
1795  * They are cleared lazily, on the first LTE rule of the walk.
1796  */
1797 static inline void DetectFwEnsureLteCoverage(
1798  DetectEngineThreadCtx *det_ctx, struct DetectFirewallAppTxState *fw_state)
1799 {
1800  if (!fw_state->fw_lte_cover_active) {
1801  memset(det_ctx->fw_lte_cover, 0, sizeof(det_ctx->fw_lte_cover));
1802  fw_state->fw_lte_cover_active = true;
1803  }
1804 }
1805 
1806 /** \internal
1807  * \brief build the per hook coverage of the pending LTE rules.
1808  *
1809  * Counts, per hook, how many pending LTE rules cover it (their own hook and
1810  * every prior hook). The candidate list is sorted, so duplicates - the state
1811  * store and the per-state registrations can both add a rule - are adjacent.
1812  */
1813 static void DetectFwBuildLteCoverage(DetectEngineThreadCtx *det_ctx, Packet *p, Flow *f,
1814  struct DetectFirewallAppTxState *fw_state, const uint32_t array_idx)
1815 {
1816  DetectFwEnsureLteCoverage(det_ctx, fw_state);
1817  for (uint32_t i = 0; i < array_idx; i++) {
1818  const Signature *s = det_ctx->tx_candidates[i].s;
1819  /* only firewall rules implement the per-state policy: a threat
1820  * detection rule using the <hook header must not count as coverage */
1821  if (!(s->flags & SIG_FLAG_FW_HOOK_LTE) || !(s->flags & SIG_FLAG_FIREWALL)) {
1822  continue;
1823  }
1824  RuleMatchCandidateTx *can = &det_ctx->tx_candidates[i];
1825  can->fw_lte_counted = false;
1826  const uint32_t *flags = can->flags;
1827  if (flags == NULL) {
1828  if (!DetectRunInspectRuleHeader(p, f, s, s->flags)) {
1829  /* an out of scope rule never covered anything */
1830  continue;
1831  }
1832  /* the walk inspects this candidate with the same arguments, so it
1833  * can skip repeating the header check */
1834  can->fw_lte_header_ok = true;
1835  }
1836  if (flags != NULL &&
1838  /* already resolved in an earlier pass */
1839  continue;
1840  }
1841  /* duplicates of the same signature (state store + per-state
1842  * registrations) are adjacent: the candidate list is sorted by id with
1843  * the flagged entry first */
1845  i > 0 && det_ctx->tx_candidates[i].id < det_ctx->tx_candidates[i - 1].id);
1846  if (i > 0 && det_ctx->tx_candidates[i - 1].s == s) {
1847  continue;
1848  }
1850  /* defensive: parse rejects this; skip it so the arrays stay in
1851  * bounds and the rule applies its own default policy */
1853  continue;
1854  }
1855  can->fw_lte_counted = true;
1856  for (uint16_t h = 0; h <= s->app_progress_hook; h++) {
1857  det_ctx->fw_lte_cover[h]++;
1858  }
1859  }
1860 }
1861 
1862 /** \internal
1863  * \brief check if a pending LTE rule other than the current one covers its hook.
1864  */
1865 static inline bool DetectFwOtherLteCoversHook(DetectEngineThreadCtx *det_ctx,
1866  const struct DetectFirewallAppTxState *fw_state, const Signature *s, const bool counted)
1867 {
1868  if (!fw_state->fw_lte_cover_active) {
1869  /* no LTE candidate in this walk, so nothing covers the hook */
1870  return false;
1871  }
1874  return false;
1875  }
1876  const uint32_t covered = det_ctx->fw_lte_cover[s->app_progress_hook];
1877  if (s->flags & SIG_FLAG_FW_HOOK_LTE) {
1878  /* an uncounted candidate (header mismatch, already retired) carries
1879  * no own count in the coverage */
1880  return covered > (counted ? 1 : 0);
1881  }
1882  return covered > 0;
1883 }
1884 
1885 /** \internal
1886  * \brief count an LTE rule appended mid-walk into the hook coverage.
1887  *
1888  * Appended candidates are the tail of the list, so duplicates cannot be
1889  * detected by adjacency like in DetectFwBuildLteCoverage().
1890  */
1891 static inline void DetectFwCountAppendedLteRule(DetectEngineThreadCtx *det_ctx, Packet *p, Flow *f,
1892  struct DetectFirewallAppTxState *fw_state, const Signature *s, const uint32_t can_idx)
1893 {
1894  if (!(s->flags & SIG_FLAG_FW_HOOK_LTE) || !(s->flags & SIG_FLAG_FIREWALL) ||
1896  !DetectRunInspectRuleHeader(p, f, s, s->flags)) {
1897  return;
1898  }
1899  for (uint32_t k = 0; k < can_idx; k++) {
1900  if (det_ctx->tx_candidates[k].s == s && det_ctx->tx_candidates[k].fw_lte_counted) {
1901  return;
1902  }
1903  }
1904  DetectFwEnsureLteCoverage(det_ctx, fw_state);
1905  for (uint16_t h = 0; h <= s->app_progress_hook; h++) {
1906  det_ctx->fw_lte_cover[h]++;
1907  }
1908  det_ctx->tx_candidates[can_idx].fw_lte_counted = true;
1909  det_ctx->tx_candidates[can_idx].fw_lte_header_ok = true;
1910 }
1911 
1912 /** \internal
1913  * \brief remove an LTE rule that resolved definitively from the hook coverage.
1914  */
1915 static void DetectFwRetireLteRule(DetectEngineThreadCtx *det_ctx, RuleMatchCandidateTx *can)
1916 {
1917  const Signature *s = can->s;
1920  return;
1921  }
1922  for (uint16_t h = 0; h <= s->app_progress_hook; h++) {
1923  /* the caller only retires counted candidates: a zero here means the
1924  * accounting is broken, not a legitimate case */
1925  DEBUG_VALIDATE_BUG_ON(det_ctx->fw_lte_cover[h] == 0);
1926  if (det_ctx->fw_lte_cover[h] > 0) {
1927  det_ctx->fw_lte_cover[h]--;
1928  }
1929  }
1930  /* the candidate no longer contributes to the counts */
1931  can->fw_lte_counted = false;
1932 }
1933 
1934 static inline void DetectRunAppendDefaultAppPolicyAlert(DetectEngineThreadCtx *det_ctx, Packet *p,
1935  const bool apply_to_packet, const DetectTransaction *tx,
1936  const struct DetectFirewallAppPolicy *ap)
1937 {
1938  if (EngineModeIsFirewall()) {
1939  const Signature *s = ap->alert_signature;
1940  BUG_ON(s == NULL);
1941  uint8_t alert_flags = apply_to_packet ? PACKET_ALERT_FLAG_APPLY_ACTION_TO_PACKET : 0;
1942  AlertQueueAppendAppTxFromPacket(det_ctx, s, p, tx->tx_id, tx->tx_type, alert_flags);
1943  }
1944 }
1945 
1946 /** \internal
1947  * \brief apply default policy
1948  * \param p packet to apply policy to
1949  * \param alproto app proto
1950  * \param progress hook / progress value to apply the policy to
1951  *
1952  * \note alproto and progress are unused right now, will be used
1953  * to look up configurable default policies later
1954  */
1955 static struct DetectFirewallPolicy DetectFirewallApplyDefaultAppPolicy(
1956  DetectEngineThreadCtx *det_ctx, const struct DetectFirewallPolicies *policies,
1957  const DetectTransaction *tx, Packet *p, const AppProto alproto, const uint8_t direction,
1958  const uint8_t progress)
1959 {
1960  const uint8_t dir_flags = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
1961 
1962  SCLogDebug("packet %" PRIu64 ": tx type %u", PcapPacketCntGet(p), tx->tx_type);
1963 
1964  const struct DetectFirewallPolicy drop_policy = { .action = ACTION_DROP,
1965  .action_scope = ACTION_SCOPE_FLOW };
1966  const struct DetectFirewallAppPolicy lookup = {
1967  .alproto = alproto, .sub_state = tx->tx_type, .progress = progress, .direction = dir_flags
1968  };
1969  const struct DetectFirewallPolicy *policy = NULL;
1970  const struct DetectFirewallAppPolicy *ap =
1971  HashTableLookup(policies->app_policies, (void *)&lookup, 0);
1972  /* table should be fully populated, so this should not be able to fail.
1973  * However as it continues to confuse tooling, at a fallback. */
1974  DEBUG_VALIDATE_BUG_ON(ap == NULL);
1975  if (likely(ap != NULL)) {
1976  policy = &ap->policy;
1977  } else {
1978  policy = &drop_policy;
1979  }
1980  if (policy->action & ACTION_DROP) {
1981  SCLogDebug("dropping packet PKT_DROP_REASON_FW_DEFAULT_APP_POLICY");
1984  SCLogDebug("dropping flow");
1985  FlowSetDropAction(p->flow, FLOW_ACTION_BY_FIREWALL);
1986  }
1987  if (policy->action & ACTION_ALERT) {
1988  DetectRunAppendDefaultAppPolicyAlert(det_ctx, p, true, tx, ap);
1989  }
1990  } else if (policy->action & ACTION_ACCEPT) {
1991  /* should the accept be applied to the packet?
1992  * ACTION_SCOPE_FLOW: yes
1993  * ACTION_SCOPE_TX: only if last_tx
1994  * ACTION_SCOPE_HOOK: only if last_tx and hook is highest available hook
1995  */
1996  const bool last_hook = progress == tx->tx_progress;
1997  bool apply_to_packet = false;
1998 
1999  switch (policy->action_scope) {
2000  case ACTION_SCOPE_FLOW:
2002  apply_to_packet = true;
2003  break;
2004  case ACTION_SCOPE_TX:
2005  tx->tx_data_ptr->flags |= APP_LAYER_TX_ACCEPT;
2006  apply_to_packet = tx->is_last;
2007  break;
2008  case ACTION_SCOPE_HOOK:
2009  apply_to_packet = tx->is_last && last_hook;
2010  break;
2011  default:
2012  /* should be unreachable */
2014  break;
2015  }
2016  SCLogDebug("packet %" PRIu64 " hook %u default policy ACCEPT, apply_to_packet:%s",
2017  PcapPacketCntGet(p), progress, BOOL2STR(apply_to_packet));
2018 
2019  if (policy->action & ACTION_ALERT) {
2020  SCLogDebug("policy alert, do the append");
2021  DetectRunAppendDefaultAppPolicyAlert(det_ctx, p, apply_to_packet, tx, ap);
2022  } else if (apply_to_packet) {
2023  SCLogDebug("default accept: last_tx");
2024  DetectRunAppendDefaultAccept(det_ctx, p);
2025  }
2026  } else {
2027  /* should be unreachable */
2029  }
2030  return *policy;
2031 }
2032 
2033 /** \internal
2034  * \brief run default policies for hook(s)
2035  *
2036  * For a range of hooks look up the policy and apply it.
2037  *
2038  * \param is_last is this tx the last we have? Used to check if an action needs to be applied to
2039  * the packet.
2040  *
2041  * \retval DETECT_TX_FW_FC_BREAK rest of rules shouldn't be inspected
2042  * \retval DETECT_TX_FW_FC_SKIP skip current firewall rule
2043  * \retval DETECT_TX_FW_FC_OK no action needed
2044  */
2045 static enum DetectTxFirewallFlowControl DetectFirewallApplyDefaultPolicies(
2046  DetectEngineThreadCtx *det_ctx, const struct DetectFirewallPolicies *policies,
2047  DetectTransaction *tx, Packet *p, const AppProto alproto, const uint8_t direction,
2048  const uint8_t start_hook, const uint8_t end_hook)
2049 {
2050  DEBUG_VALIDATE_BUG_ON(start_hook > end_hook);
2051 
2052  const bool need_verdict =
2053  tx->is_last && (end_hook == tx->tx_end_state || end_hook == tx->tx_progress);
2054  SCLogDebug("need_verdict:%s is_last:%s end_hook:%u tx->tx_end_state:%u tx->progress: %u",
2055  BOOL2STR(need_verdict), BOOL2STR(tx->is_last), end_hook, tx->tx_end_state,
2056  tx->tx_progress);
2057 
2058  for (uint8_t hook = start_hook; hook <= end_hook; hook++) {
2059  const bool apply_to_packet =
2060  tx->is_last && (hook == tx->tx_end_state || hook == tx->tx_progress);
2061 
2062  SCLogDebug("%" PRIu64 ": %s default policy for hook %u, apply_to_packet %s",
2063  PcapPacketCntGet(p), direction & STREAM_TOSERVER ? "toserver" : "toclient", hook,
2064  BOOL2STR(apply_to_packet));
2065 
2066  const struct DetectFirewallPolicy policy = DetectFirewallApplyDefaultAppPolicy(
2067  det_ctx, policies, tx, p, alproto, direction, hook);
2068  SCLogDebug("fw: hook:%u policy:%02x apply_to_packet:%s", hook, policy.action,
2069  BOOL2STR(apply_to_packet));
2070  if (policy.action & ACTION_DROP) {
2071  SCLogDebug("fw: action %02x", policy.action);
2072  return DETECT_TX_FW_FC_BREAK;
2073 
2074  } else if (policy.action & ACTION_ACCEPT) {
2075  SCLogDebug("fw: accept hook %u action %02x", hook, policy.action);
2076 
2077  /* accepting flow, so skip rest of the fw rules */
2078  if (policy.action_scope == ACTION_SCOPE_FLOW) {
2079  SCLogDebug("fw: accept flow");
2080  return DETECT_TX_FW_FC_SKIP;
2081 
2082  /* accepting flow, so skip rest of the fw rules for this tx */
2083  } else if (policy.action_scope == ACTION_SCOPE_TX) {
2084  return DETECT_TX_FW_FC_SKIP;
2085 
2086  } else if (policy.action_scope == ACTION_SCOPE_HOOK) {
2087  /* we're done */
2088  if (apply_to_packet) {
2089  return DETECT_TX_FW_FC_SKIP;
2090  }
2091  } else {
2093  }
2094  } else {
2096  }
2097  }
2098 
2099  /* if the tx progress is at end_hook state, it means the rule that called us cannot
2100  * match: it's app_progress_hook is not yet available. In this can we need to set
2101  * a default accept. This happens if we got only `accept:hook` policies. */
2102  if (need_verdict) {
2103  SCLogDebug("default accept: last tx and progress at end_hook %u", end_hook);
2104  DetectRunAppendDefaultAccept(det_ctx, p);
2105  /* break as we can't match the calling signature */
2106  return DETECT_TX_FW_FC_BREAK;
2107  }
2108 
2109  return DETECT_TX_FW_FC_OK;
2110 }
2111 
2112 /** \internal
2113  * \brief run pre-rule inspection firewall policy checks
2114  *
2115  * Check for:
2116  * - check if we're in accept:tx mode
2117  * - check for missing accept hooks
2118  * -
2119  *
2120  * \retval DETECT_TX_FW_FC_OK no action needed
2121  * \retval DETECT_TX_FW_FC_BREAK rest of rules shouldn't be inspected
2122  * \retval DETECT_TX_FW_FC_SKIP skip this rule
2123  */
2124 static enum DetectTxFirewallFlowControl DetectRunTxPreCheckFirewallPolicy(
2125  DetectEngineThreadCtx *det_ctx, Packet *p, DetectTransaction *tx, const uint8_t direction,
2126  const Signature *s, const uint32_t can_idx, const bool lte_counted,
2127  struct DetectFirewallAppTxState *fw_state)
2128 {
2129  SCLogDebug("packet %" PRIu64 ": running pre-checks before sid %u", PcapPacketCntGet(p), s->id);
2130 
2131  /* enforce skip app filter. If a prior rule caused a fw_skip_app_filter set, we will skip
2132  * each fw rule from now. Non-FW rules will just be inspected. Non-FW need to hit this
2133  * path to help put into effect the FLOW_ACTION_ACCEPT/APP_LAYER_TX_ACCEPT and call
2134  * the default policy enforcement. */
2135  if (fw_state->fw_skip_app_filter) {
2136  if ((s->flags & SIG_FLAG_FIREWALL) != 0) {
2137  return DETECT_TX_FW_FC_SKIP;
2138  } else {
2139  return DETECT_TX_FW_FC_OK;
2140  }
2141  }
2142  if (p->flow->flags & FLOW_ACTION_ACCEPT) {
2143  fw_state->fw_skip_app_filter = true;
2144  SCLogDebug("default accept due to flow accept");
2145  DetectRunAppendDefaultAccept(det_ctx, p);
2146 
2147  if (s->flags & SIG_FLAG_FIREWALL) {
2148  return DETECT_TX_FW_FC_SKIP;
2149  }
2150  }
2151  /* skip fw rules if we're in accept:tx mode */
2152  if (tx->tx_data_ptr->flags & APP_LAYER_TX_ACCEPT) {
2153  /* append a blank accept:packet action for the APP_LAYER_TX_ACCEPT,
2154  * if this is the last tx */
2155  fw_state->fw_skip_app_filter = true;
2156  const bool accept_tx_applies_to_packet = tx->is_last;
2157  if (accept_tx_applies_to_packet) {
2158  SCLogDebug("accept:tx: should be applied to the packet");
2159  DetectRunAppendDefaultAccept(det_ctx, p);
2160  }
2161 
2162  if (s->flags & SIG_FLAG_FIREWALL) {
2163  SCLogDebug("APP_LAYER_TX_ACCEPT, so skip rule");
2164  return DETECT_TX_FW_FC_SKIP;
2165  }
2166 
2167  /* threat detect rules will be inspected */
2168  return DETECT_TX_FW_FC_OK;
2169  }
2170 
2171  /* handle missing rules case */
2172  if (s->flags & SIG_FLAG_FW_HOOK_LTE) {
2173  SCLogDebug("SIG_FLAG_FW_HOOK_LTE");
2174  return DETECT_TX_FW_FC_OK; // TODO check for other cases
2175  }
2176 
2177  if (can_idx == 0) {
2178  SCLogDebug("check if prior hooks are satisfied: s->app_progress_hook %u, "
2179  "tx->detect_progress_orig %u",
2181 
2182  if (DetectFwOtherLteCoversHook(det_ctx, fw_state, s, lte_counted)) {
2183  SCLogDebug("later LTE in-progress rule brought us here: us:%u covered:%u",
2185  return DETECT_TX_FW_FC_OK;
2186  }
2187 
2188  /* if our first rule is beyond the starting state, we need to check if
2189  * there are rules missing for states in between. */
2190  if (s->app_progress_hook > tx->detect_progress_orig) {
2191  SCLogDebug("missing fw rules at list start: sid %u, progress %u (%u:%u)", s->id,
2193  /* if this rule was after the state we expected meaning that there are
2194  * no rules for that state. Invoke the default policies. */
2195  enum DetectTxFirewallFlowControl r = DetectFirewallApplyDefaultPolicies(det_ctx,
2196  det_ctx->de_ctx->fw_policies, tx, p, s->alproto, direction,
2198  if (r != DETECT_TX_FW_FC_OK) {
2199  /* both SKIP and BREAK mean: no more fw rules to inspect.
2200  * SKIP applies to just this TX.
2201  * DROP applies to everything. */
2202  fw_state->fw_skip_app_filter = true;
2203  }
2204  return r;
2205  }
2206  }
2207  return DETECT_TX_FW_FC_OK;
2208 }
2209 
2210 /**
2211  * \internal
2212  * \brief Check and update firewall rules state.
2213  *
2214  * \param fw_state pointer to flow control state
2215  *
2216  * \retval DETECT_TX_FW_FC_OK no action needed
2217  * \retval DETECT_TX_FW_FC_BREAK rest of rules shouldn't inspected
2218  * \retval DETECT_TX_FW_FC_SKIP skip this rule
2219  */
2220 static enum DetectTxFirewallFlowControl DetectRunTxCheckRuleState(DetectEngineThreadCtx *det_ctx,
2221  Packet *p, Flow *f, DetectTransaction *tx, const Signature *s, const uint32_t can_idx,
2222  const uint32_t can_size, const bool lte_counted, struct DetectFirewallAppTxState *fw_state)
2223 {
2224  if (s->flags & SIG_FLAG_FIREWALL) {
2225  /* check if the next sig is on the same progress hook. If not, we need to apply our
2226  * default policy in case the current sig doesn't apply one. If the next sig has a
2227  * progress beyond our progress + 1, it means the next progress has no rules and needs
2228  * the default policy applied. But only after we evaluate the current rule first, as
2229  * that may override it. */
2230 
2231  if (can_idx + 1 < can_size) {
2232  const Signature *next_s = det_ctx->tx_candidates[can_idx + 1].s;
2233  SCLogDebug(
2234  "peek: peeking at sid %u / progress %u", next_s->id, next_s->app_progress_hook);
2235  if (next_s->flags & SIG_FLAG_FIREWALL) {
2236  if (s->app_progress_hook != next_s->app_progress_hook) {
2237  if (DetectFwOtherLteCoversHook(det_ctx, fw_state, s, lte_counted)) {
2238  SCLogDebug("we have another LTE in-progress. Us:%u covered:%u",
2240  } else {
2241  SCLogDebug("peek: next sid progress %u != current progress %u, so current "
2242  "is last for progress",
2243  next_s->app_progress_hook, s->app_progress_hook);
2244  fw_state->fw_last_for_progress = true;
2245 
2246  if (next_s->app_progress_hook - s->app_progress_hook > 1) {
2247  SCLogDebug("peek: missing progress, so we'll drop that unless we get a "
2248  "sweeping accept first");
2249  fw_state->fw_next_progress_missing = true;
2250  }
2251  }
2252  }
2253  } else {
2254  SCLogDebug("peek: next sid not a fw rule, so current is last for progress");
2255  fw_state->fw_last_for_progress = true;
2256  fw_state->last_fw_rule = true;
2257  }
2258  } else {
2259  SCLogDebug("peek: no peek beyond last rule");
2260  if (!DetectFwOtherLteCoversHook(det_ctx, fw_state, s, lte_counted)) {
2261  if (s->app_progress_hook < tx->tx_progress) {
2262  SCLogDebug("peek: there are no rules to allow the state after this rule");
2263  fw_state->fw_next_progress_missing = true;
2264  }
2265  fw_state->fw_last_for_progress = true;
2266  fw_state->last_fw_rule = true;
2267  }
2268  }
2269 
2270  if (fw_state->skip_fw_hook == true) {
2271  if (s->app_progress_hook <= fw_state->skip_before_progress) {
2272  return DETECT_TX_FW_FC_SKIP;
2273  }
2274  fw_state->skip_fw_hook = false;
2275  }
2276  } else {
2277  /* fw mode, we skip anything after the fw rules if:
2278  * - flow pass is set
2279  * - packet pass (e.g. exception policy) */
2281  SCLogDebug("skipping firewall rule %u", s->id);
2282  return DETECT_TX_FW_FC_BREAK;
2283  }
2284  }
2285  return DETECT_TX_FW_FC_OK;
2286 }
2287 
2288 // TODO move into det_ctx?
2290 static void DetectRunAppendDefaultAccept(DetectEngineThreadCtx *det_ctx, Packet *p)
2291 {
2293  SCLogDebug("packet %" PRIu64 ": appending default firewall accept", PcapPacketCntGet(p));
2294  memset(&default_accept, 0, sizeof(default_accept));
2297  default_accept.iid = UINT32_MAX;
2301  DETECT_TABLE_APP_FILTER; // TODO review, hope this makes it last in sorting
2303 }
2304 
2305 /** \internal
2306  * \brief see if the accept rule needs to apply to the packet
2307  */
2308 static inline bool ApplyAcceptToPacket(const DetectTransaction *tx, const Signature *s)
2309 {
2310  if ((s->flags & SIG_FLAG_FIREWALL) == 0) {
2311  return false;
2312  }
2313  if ((s->action & ACTION_ACCEPT) == 0) {
2314  return false;
2315  }
2316 
2317  /* for accept:tx we need:
2318  * - packet will only be accepted if this is set on the last tx
2319  */
2320  if (s->action_scope == ACTION_SCOPE_TX) {
2321  if (tx->is_last) {
2322  return true;
2323  }
2324  }
2325  /* for accept:hook we need a bit more checking:
2326  * - packet will only be accepted if this is set on the last tx
2327  * - the hook accepted should be the last progress available. */
2328  if (s->action_scope == ACTION_SCOPE_HOOK) {
2329  if (tx->is_last && (s->app_progress_hook == tx->tx_progress)) {
2330  return true;
2331  }
2332  }
2333  return false;
2334 }
2335 
2336 /** \internal
2337  * \brief apply an accept, but do check policies when needed
2338  *
2339  * Updates flow control where needed.
2340  *
2341  * */
2342 static void DetectRunTxFirewallApplyAccept(DetectEngineThreadCtx *det_ctx, Packet *p,
2343  const uint8_t direction, const Signature *s, DetectTransaction *tx,
2344  struct DetectFirewallAppTxState *fw_state)
2345 {
2346  const enum ActionScope as = s->action_scope;
2347  /* accept:hook: jump to first rule of next state.
2348  * Implemented as skip until the first rule of next state. */
2349  if (as == ACTION_SCOPE_HOOK) {
2350  fw_state->skip_fw_hook = true;
2351  fw_state->skip_before_progress = s->app_progress_hook;
2352 
2353  SCLogDebug("fw match sid:%u hook:%u", s->id, s->app_progress_hook);
2354  SCLogDebug("fw fw_skip_app_filter:%s skip_fw_hook:%s "
2355  "skip_before_progress:%u fw_last_for_progress:%s fw_next_progress_missing:%s",
2356  BOOL2STR(fw_state->fw_skip_app_filter), BOOL2STR(fw_state->skip_fw_hook),
2357  fw_state->skip_before_progress, BOOL2STR(fw_state->fw_last_for_progress),
2358  BOOL2STR(fw_state->fw_next_progress_missing));
2359  /* if there is no fw rule for the next progress value,
2360  * we invoke the defaul policies for the remaining available hooks.
2361  * Only when the tx walked over this rule's hook: at progress == hook
2362  * the sweep range would start above its end. */
2363  if (fw_state->fw_next_progress_missing && tx->tx_progress > s->app_progress_hook) {
2364  /* only states the packet actually walked over: states beyond the
2365  * current progress have not been reached, their defaults must not
2366  * decide this packet */
2367  const uint8_t last_hook = fw_state->last_fw_rule
2368  ? tx->tx_progress
2369  : MIN(tx->tx_progress, s->app_progress_hook + 1);
2371  DetectFirewallApplyDefaultPolicies(det_ctx, det_ctx->de_ctx->fw_policies, tx, p,
2372  s->alproto, direction, s->app_progress_hook + 1, last_hook);
2373  if (r == DETECT_TX_FW_FC_BREAK) {
2374  fw_state->fw_skip_app_filter = true;
2375  return;
2376  }
2377  }
2378  } else if (as == ACTION_SCOPE_TX) {
2380  fw_state->skip_fw_hook = true;
2381  fw_state->skip_before_progress = tx->tx_end_state + 1; // skip all hooks
2382  SCLogDebug("accept:tx applied, skip_fw_hook, skip_before_progress %u",
2383  fw_state->skip_before_progress);
2384  } else if (as == ACTION_SCOPE_PACKET) {
2385  fw_state->fw_skip_app_filter = true;
2386  } else if (as == ACTION_SCOPE_FLOW) {
2387  SCLogDebug("sid %u: ACTION_ACCEPT with ACTION_SCOPE_FLOW", s->id);
2388  fw_state->fw_skip_app_filter = true;
2389  }
2390 }
2391 
2392 /**
2393  * \internal
2394  * \brief check if there are no (fw) rules, and apply the default policies if so
2395  *
2396  * \retval 3 policies handled, continue with inspection
2397  * \retval 2 continue with next tx
2398  * \retval 1 done with inspection
2399  * \retval 0 ok, continue as normal. No policies applied.
2400  */
2401 static int DetectTxFirewallNoRulesApplyPolicies(DetectEngineThreadCtx *det_ctx, Packet *p, Flow *f,
2402  DetectTransaction *tx, const AppProto alproto, const uint8_t flow_flags, const int rule_cnt)
2403 {
2404  /* if there are no rules / rule candidates, handling invoking the default
2405  * policy. */
2406  if (rule_cnt == 0 || (det_ctx->tx_candidates[0].s->flags & SIG_FLAG_FIREWALL) == 0) {
2407  /* if there are no rules, make sure to handle accept:flow and accept:tx */
2408  if (rule_cnt == 0) {
2409  if (f->flags & FLOW_ACTION_ACCEPT) {
2410  SCLogDebug("default accept:flow: no rules");
2411  DetectRunAppendDefaultAccept(det_ctx, p);
2412  return 1;
2413  }
2414  if (tx->tx_data_ptr->flags & APP_LAYER_TX_ACCEPT) {
2415  /* current tx is the last we have, append a blank accept:packet */
2416  if (tx->is_last) {
2417  SCLogDebug("default accept:tx: no rules");
2418  DetectRunAppendDefaultAccept(det_ctx, p);
2419  return 1;
2420  }
2421  return 2;
2422  }
2423  }
2424 
2425  /* if there are no fw rules, handle default policies */
2426  if ((f->flags & FLOW_ACTION_ACCEPT) == 0 &&
2427  (tx->tx_data_ptr->flags & APP_LAYER_TX_ACCEPT) == 0) {
2428  /* No rules to eval, so we need to see if there are default policies to apply.
2429  * Start at last inspected progress and check each hook. If all hooks accepted,
2430  * apply the accept to the packet. */
2431  SCLogDebug("tx.detect_progress_orig %u tx.tx_progress %u", tx->detect_progress_orig,
2432  tx->tx_progress);
2434  DetectFirewallApplyDefaultPolicies(det_ctx, det_ctx->de_ctx->fw_policies, tx, p,
2435  alproto, flow_flags & (STREAM_TOSERVER | STREAM_TOCLIENT),
2437  SCLogDebug("r %u", r);
2438  if (r == DETECT_TX_FW_FC_BREAK)
2439  return 1;
2440  if (r == DETECT_TX_FW_FC_SKIP)
2441  return 2;
2442  /* continue with TD rules */
2443  SCLogDebug("continue with TD rules");
2444  return 3;
2445  }
2446  }
2447  return 0;
2448 }
2449 
2450 /** \internal
2451  * \brief handle a full rule match for a firewall rule
2452  *
2453  * For a drop/reject rule, excute action immediately.
2454  * For an accept rule, add an alert even to the queue. This will
2455  * allow TD rules to override the accept.
2456  */
2457 static void DetectRunTxFirewallRuleFullMatch(DetectEngineThreadCtx *det_ctx, const Signature *s,
2458  DetectTransaction *tx, struct DetectFirewallAppTxState *fw_state, Flow *f, Packet *p,
2459  const uint8_t flow_flags)
2460 {
2461  if (s->action & ACTION_ACCEPT) {
2462  /* add alert now, as ApplyAccept may also trigger
2463  * policy matches that could add alerts. */
2464  SCLogDebug("append alert");
2465  /* see if we need to apply tx/hook accept to the packet. This can be needed
2466  * when we've completed the inspection so far for an incomplete tx, and an
2467  * accept:tx or accept:hook is the last match.*/
2468  const bool fw_accept_to_packet = ApplyAcceptToPacket(tx, s);
2469  if (fw_accept_to_packet) {
2470  SCLogDebug("packet %" PRIu64 ": apply accept to packet", PcapPacketCntGet(p));
2471  SCLogDebug("accept:(tx|hook): should be applied to the packet");
2472  AlertQueueAppendAppTx(det_ctx, s, p, tx->tx_id, tx->tx_type,
2474  } else {
2475  AlertQueueAppendAppTx(det_ctx, s, p, tx->tx_id, tx->tx_type, 0);
2476  }
2477  DetectRunTxFirewallApplyAccept(det_ctx, p, flow_flags, s, tx, fw_state);
2478  } else if (s->action & ACTION_DROP) {
2479  SCLogDebug("drop packet because of rule with drop action");
2481  if (s->action_scope == ACTION_SCOPE_FLOW) {
2482  SCLogDebug("drop flow because of rule with drop action");
2483  FlowSetDropAction(f, FLOW_ACTION_BY_FIREWALL);
2484  }
2485  SCLogDebug("append alert");
2486  AlertQueueAppendAppTx(det_ctx, s, p, tx->tx_id, tx->tx_type, 0);
2487  } else {
2488  SCLogDebug("append alert");
2489  AlertQueueAppendAppTx(det_ctx, s, p, tx->tx_id, tx->tx_type, 0);
2490  }
2491 }
2492 
2493 /** \internal
2494  * \brief handle a partial match for firewall rules
2495  *
2496  * Currently only used for LTE mode. Regardless of the final action,
2497  * the partial match acts as a `accept:hook`.
2498  *
2499  * A default accept is appended. TD has a chance to override this accept.
2500  *
2501  * \retval 1 accept partial, caller must break loop
2502  * \retval 0 ok, caller must continue as normal
2503  */
2504 static int DetectRunTxFirewallRulePartialMatch(
2505  DetectEngineThreadCtx *det_ctx, const Signature *s, const DetectTransaction *tx, Packet *p)
2506 {
2507  if ((s->flags & SIG_FLAG_FIREWALL) && (s->action & ACTION_ACCEPT)) {
2508  /* partial match always uses ACTION_SCOPE_HOOK. Final action only on the full
2509  * match */
2510  if (tx->is_last) {
2511  SCLogDebug("need to apply accept to packet");
2512  DetectRunAppendDefaultAccept(det_ctx, p);
2513  }
2514  if (s->action_scope == ACTION_SCOPE_FLOW) {
2515  SCLogDebug("only applying accept:flow on full match, downgrading to "
2516  "accept:hook");
2517  }
2518  return 1;
2519  }
2520  return 0;
2521 }
2522 
2523 /** \internal
2524  * \brief handle the no-match case for a firewall rule
2525  *
2526  * If the rule did not match we need to see if we need invoke the default
2527  * policy for this hook. If that is the case, we handle a drop by telling the
2528  * caller about it. Flow control for various accept options is handled by the
2529  * next rule.
2530  *
2531  * \retval 1 dropped by policy, caller must return
2532  * \retval 0 ok, caller must continue as normal
2533  */
2534 static int DetectRunTxFirewallRuleNoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s,
2535  DetectTransaction *tx, struct DetectFirewallAppTxState *fw_state, Packet *p,
2536  const uint8_t flow_flags)
2537 {
2538  if (fw_state->fw_last_for_progress && (s->flags & SIG_FLAG_FIREWALL)) {
2539  SCLogDebug("%" PRIu64 ": %s default policy for progress %u", PcapPacketCntGet(p),
2540  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient", s->app_progress_hook);
2541  /* if this rule was the last for our progress state, and it didn't match,
2542  * we have to invoke the default policy. We only check the current rule hook.
2543  * DROP is immediate, flow control for various accept options is handled by
2544  * the DetectRunTxPreCheckFirewallPolicy function for the next rule. */
2545  const struct DetectFirewallPolicy policy = DetectFirewallApplyDefaultAppPolicy(det_ctx,
2546  det_ctx->de_ctx->fw_policies, tx, p, s->alproto, flow_flags, s->app_progress_hook);
2547  SCLogDebug("fw_last_for_progress policy %02x", policy.action);
2548  if (policy.action & ACTION_DROP) {
2549  return 1;
2550  }
2551  /* an accept with tx or flow scope already carries the verdict. For an
2552  * accept:hook (which does not apply to this packet) the states walked
2553  * over must still be decided: if the tx progressed beyond this rule's
2554  * hook and there is no rule for the states in between, their default
2555  * policies must be applied too - same as the match path does.
2556  * Without this a packet that crosses two states (e.g. a hello
2557  * completing into the certificate phase) would be decided by the
2558  * packet default policy even though the app policy for the current
2559  * state says accept. */
2560  if ((policy.action & ACTION_ACCEPT) && policy.action_scope == ACTION_SCOPE_HOOK &&
2561  fw_state->fw_next_progress_missing && tx->tx_progress > s->app_progress_hook) {
2562  /* only states the packet actually walked over (see the match
2563  * path) */
2564  const uint8_t last_hook = fw_state->last_fw_rule
2565  ? tx->tx_progress
2566  : MIN(tx->tx_progress, s->app_progress_hook + 1);
2568  DetectFirewallApplyDefaultPolicies(det_ctx, det_ctx->de_ctx->fw_policies, tx, p,
2569  s->alproto, flow_flags, s->app_progress_hook + 1, last_hook);
2570  if (r == DETECT_TX_FW_FC_BREAK) {
2571  /* the sweep dropped the packet: same as a current-hook
2572  * policy drop, the caller must stop the walk */
2573  fw_state->fw_skip_app_filter = true;
2574  return 1;
2575  }
2576  }
2577  }
2578  return 0;
2579 }
2580 
2581 /** \internal
2582  * \brief handle full match on rule when state didn't progress yet
2583  *
2584  * For a full rule match on a certain hook, we need to continue to enforce the
2585  * match as long as the tx progress doesn't move beyond that hook.
2586  */
2587 static void DetectRunTxFirewallRuleStatefulReApplyMatch(DetectEngineThreadCtx *det_ctx,
2588  const Signature *s, DetectTransaction *tx, struct DetectFirewallAppTxState *fw_state,
2589  Packet *p, const uint8_t flow_flags)
2590 {
2591  /* if we're still in the same progress state as an earlier full
2592  * match, we need to apply the same accept */
2593  if ((s->flags & SIG_FLAG_FIREWALL) && (s->action & ACTION_ACCEPT) &&
2594  s->app_progress_hook == tx->tx_progress) {
2595  const bool fw_accept_to_packet = ApplyAcceptToPacket(tx, s);
2596  DetectRunTxFirewallApplyAccept(det_ctx, p, flow_flags, s, tx, fw_state);
2597  if (fw_accept_to_packet) {
2598  SCLogDebug("packet %" PRIu64 ": apply accept to packet", PcapPacketCntGet(p));
2599  DetectRunAppendDefaultAccept(det_ctx, p);
2600  }
2601  }
2602 }
2603 
2604 /** \brief Called on every way out of an inspected transaction.
2605  */
2606 static inline void DetectRunTxClearInspected(DetectEngineThreadCtx *det_ctx)
2607 {
2608  det_ctx->tx_id = 0;
2609  det_ctx->tx_id_set = false;
2610  det_ctx->tx_data = NULL;
2611  det_ctx->p = NULL;
2612 }
2613 
2614 static void DetectRunTx(ThreadVars *tv,
2616  DetectEngineThreadCtx *det_ctx,
2617  Packet *p,
2618  Flow *f,
2619  DetectRunScratchpad *scratch)
2620 {
2621  const uint8_t flow_flags = scratch->flow_flags;
2622  const SigGroupHead * const sgh = scratch->sgh;
2623  void * const alstate = f->alstate;
2624  const uint8_t ipproto = f->proto;
2625  const AppProto alproto = f->alproto;
2626 
2627  const uint64_t total_txs = AppLayerParserGetTxCnt(f, alstate);
2628  uint64_t tx_id_min = AppLayerParserGetTransactionInspectId(f->alparser, flow_flags);
2629  AppLayerGetTxIteratorFunc IterFunc = AppLayerGetTxIterator(ipproto, alproto);
2630  AppLayerGetTxIterState state = { 0 };
2631 
2632  uint32_t tx_inspected = 0;
2633  const bool have_fw_rules = EngineModeIsFirewall();
2634  /* if we skipped the last tx, we did not have a chance to apply a fw accept to the packet. Since
2635  * the tx is skipped, we should consider it accepted. */
2636  bool last_tx_skipped = false;
2637 
2638  SCLogDebug("packet %" PRIu64, PcapPacketCntGet(p));
2639  SCLogDebug("total_txs %" PRIu64, total_txs);
2640 
2641  while (1) {
2642  AppLayerGetTxIterTuple ires = IterFunc(ipproto, alproto, alstate, tx_id_min, total_txs, &state);
2643  if (ires.tx_ptr == NULL) {
2644  SCLogDebug("%p/%" PRIu64 " no transaction to inspect", ires.tx_ptr, tx_id_min);
2645  break;
2646  }
2647 
2648  DetectTransaction tx = GetDetectTx(ipproto, alproto, ires.tx_id, ires.tx_ptr, flow_flags);
2649  if (tx.tx_ptr == NULL) {
2650  SCLogDebug("%p/%"PRIu64" no transaction to inspect",
2651  tx.tx_ptr, tx_id_min);
2652  last_tx_skipped = !ires.has_next;
2653  tx_id_min++; // next (if any) run look for +1
2654  goto next;
2655  }
2656  tx.is_last = !ires.has_next;
2657  tx_id_min = tx.tx_id + 1; // next look for cur + 1
2658  tx_inspected++;
2659 
2660  SCLogDebug("%p/%" PRIu64 " txd flags %02x", tx.tx_ptr, tx.tx_id, tx.tx_data_ptr->flags);
2661  SCLogDebug("%p/%" PRIu64 " is_last %s", tx.tx_ptr, tx.tx_id, BOOL2STR(tx.is_last));
2662 
2663  det_ctx->tx_id = tx.tx_id;
2664  det_ctx->tx_id_set = true;
2665  det_ctx->tx_data = tx.tx_data_ptr;
2666  det_ctx->p = p;
2667 
2668  bool do_sort = false; // do we need to sort the tx candidate list?
2669  bool fw_lte_candidates = false; // does the list have a pending LTE rule?
2670  uint32_t array_idx = 0;
2671  uint32_t total_rules = det_ctx->match_array_cnt;
2672  total_rules += (tx.de_state ? tx.de_state->cnt : 0);
2673 
2674  /* run prefilter engines and merge results into a candidates array */
2675  if (sgh && sgh->tx_engines) {
2677  DetectRunPrefilterTx(det_ctx, sgh, p, ipproto, flow_flags, alproto,
2678  alstate, &tx);
2680  SCLogDebug("%p/%"PRIu64" rules added from prefilter: %u candidates",
2681  tx.tx_ptr, tx.tx_id, det_ctx->pmq.rule_id_array_cnt);
2682 
2683  total_rules += det_ctx->pmq.rule_id_array_cnt;
2684  if (!(RuleMatchCandidateTxArrayHasSpace(det_ctx, total_rules))) {
2685  RuleMatchCandidateTxArrayExpand(det_ctx, total_rules);
2686  }
2687 
2688  for (uint32_t i = 0; i < det_ctx->pmq.rule_id_array_cnt; i++) {
2689  const Signature *s = de_ctx->sig_array[det_ctx->pmq.rule_id_array[i]];
2690  const SigIntId id = s->iid;
2691  det_ctx->tx_candidates[array_idx].s = s;
2692  det_ctx->tx_candidates[array_idx].id = id;
2693  det_ctx->tx_candidates[array_idx].flags = NULL;
2694  det_ctx->tx_candidates[array_idx].fw_lte_counted = false;
2695  det_ctx->tx_candidates[array_idx].fw_lte_header_ok = false;
2696  det_ctx->tx_candidates[array_idx].stream_reset = 0;
2697  if (s->flags & SIG_FLAG_FW_HOOK_LTE) {
2698  fw_lte_candidates = true;
2699  }
2700  array_idx++;
2701  }
2702  PMQ_RESET(&det_ctx->pmq);
2703  } else {
2704  if (!(RuleMatchCandidateTxArrayHasSpace(det_ctx, total_rules))) {
2705  RuleMatchCandidateTxArrayExpand(det_ctx, total_rules);
2706  }
2707  }
2708 
2709  /* merge 'state' rules from the regular prefilter */
2710 #ifdef PROFILING
2711  uint32_t x = array_idx;
2712 #endif
2713  RuleMatchCandidateMergeStateRules(det_ctx, &array_idx, &fw_lte_candidates);
2714 
2715  /* merge stored state into results */
2716  if (tx.de_state != NULL) {
2717  const uint32_t old = array_idx;
2718 
2719  /* if tx.de_state->flags has 'new file' set and sig below has
2720  * 'file inspected' flag, reset the file part of the state */
2721  const bool have_new_file = (tx.de_state->flags & DETECT_ENGINE_STATE_FLAG_FILE_NEW);
2722  if (have_new_file) {
2723  SCLogDebug("%p/%"PRIu64" destate: need to consider new file",
2724  tx.tx_ptr, tx.tx_id);
2726  }
2727 
2728  SigIntId state_cnt = 0;
2729  DeStateStore *tx_store = tx.de_state->head;
2730  for (; tx_store != NULL; tx_store = tx_store->next) {
2731  SCLogDebug("tx_store %p", tx_store);
2732 
2733  SigIntId store_cnt = 0;
2734  for (store_cnt = 0;
2735  store_cnt < DE_STATE_CHUNK_SIZE && state_cnt < tx.de_state->cnt;
2736  store_cnt++, state_cnt++)
2737  {
2738  DeStateStoreItem *item = &tx_store->store[store_cnt];
2739  SCLogDebug("rule id %u, inspect_flags %u", item->sid, item->flags);
2740  if (have_new_file && (item->flags & DE_STATE_FLAG_FILE_INSPECT)) {
2741  /* remove part of the state. File inspect engine will now
2742  * be able to run again */
2744  SCLogDebug("rule id %u, post file reset inspect_flags %u", item->sid, item->flags);
2745  }
2746 
2747  const Signature *ss = de_ctx->sig_array[item->sid];
2748  det_ctx->tx_candidates[array_idx].s = ss;
2749  det_ctx->tx_candidates[array_idx].id = item->sid;
2750  det_ctx->tx_candidates[array_idx].flags = &item->flags;
2751  det_ctx->tx_candidates[array_idx].fw_lte_counted = false;
2752  det_ctx->tx_candidates[array_idx].fw_lte_header_ok = false;
2753  det_ctx->tx_candidates[array_idx].stream_reset = 0;
2754  if (ss->flags & SIG_FLAG_FW_HOOK_LTE) {
2755  fw_lte_candidates = true;
2756  }
2757  array_idx++;
2758  }
2759  }
2760  do_sort |= (array_idx > old); // sort if continue list adds sids
2761  SCLogDebug("%p/%" PRIu64 " rules added from 'continue' list: %u", tx.tx_ptr, tx.tx_id,
2762  array_idx - old);
2763  }
2764  if (do_sort) {
2765  qsort(det_ctx->tx_candidates, array_idx, sizeof(RuleMatchCandidateTx),
2766  DetectRunTxSortHelper);
2767  }
2768 
2769 #ifdef PROFILING
2770  if (array_idx >= de_ctx->profile_match_logging_threshold)
2771  RulesDumpTxMatchArray(det_ctx, scratch->sgh, p, tx.tx_id, array_idx, x);
2772 #endif
2773 
2774 #ifdef DEBUG
2775  for (uint32_t i = 0; i < array_idx; i++) {
2776  RuleMatchCandidateTx *can = &det_ctx->tx_candidates[i];
2777  const Signature *s = det_ctx->tx_candidates[i].s;
2778  SCLogDebug("%u: sid %u flags %p", i, s->id, can->flags);
2779  }
2780 #endif
2781 
2782  /* Only firewall mode uses the per-tx firewall state. The LTE flag is
2783  * tracked where the candidates are added: the list mixes fw and TD
2784  * rules across tables in iid order, so a scan cannot stop early. */
2785  struct DetectFirewallAppTxState fw_state = { 0 };
2786  if (have_fw_rules && fw_lte_candidates) {
2787  DetectFwBuildLteCoverage(det_ctx, p, f, &fw_state, array_idx);
2788  }
2789 
2790  SCLogDebug("%s: tx_progress %u tx %p have_fw_rules %s array_idx %u detect_progress_orig %u "
2791  "cur detect_progress %u",
2792  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient", tx.tx_progress,
2793  tx.tx_data_ptr, BOOL2STR(have_fw_rules), array_idx, tx.detect_progress_orig,
2794  tx.detect_progress);
2795 
2796  if (have_fw_rules) {
2797  /* if there are no firewall rules to consider, handle invoking the default
2798  * policies. */
2799  const int r = DetectTxFirewallNoRulesApplyPolicies(
2800  det_ctx, p, f, &tx, alproto, flow_flags, array_idx);
2801  if (r == 1) {
2802  SCLogDebug("done");
2803  DetectRunTxClearInspected(det_ctx);
2804  return;
2805  } else if (r == 2) {
2806  goto next_tx_fw; /* next tx, need to clean up buffers */
2807  }
2808  }
2809 
2810  /* run rules: inspect the match candidates */
2811  for (uint32_t i = 0; i < array_idx; i++) {
2812  RuleMatchCandidateTx *can = &det_ctx->tx_candidates[i];
2813  const Signature *s = det_ctx->tx_candidates[i].s;
2814  uint32_t *inspect_flags = det_ctx->tx_candidates[i].flags;
2815 
2816  SCLogDebug("%" PRIu64 ": sid:%u: %s tx %u/%u/%u sig %u", PcapPacketCntGet(p), s->id,
2817  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient", tx.tx_progress,
2819 
2820  if (have_fw_rules) {
2821  const enum DetectTxFirewallFlowControl fw_r = DetectRunTxPreCheckFirewallPolicy(
2822  det_ctx, p, &tx, flow_flags & (STREAM_TOSERVER | STREAM_TOCLIENT), s, i,
2823  can->fw_lte_counted, &fw_state);
2824  SCLogDebug("fw fw_skip_app_filter:%s skip_fw_hook:%s "
2825  "skip_before_progress:%u fw_last_for_progress:%s "
2826  "fw_next_progress_missing:%s",
2827  BOOL2STR(fw_state.fw_skip_app_filter), BOOL2STR(fw_state.skip_fw_hook),
2830  if (fw_r == DETECT_TX_FW_FC_SKIP) {
2831  continue;
2832  } else if (fw_r == DETECT_TX_FW_FC_BREAK) {
2833  break;
2834  }
2835 
2836  fw_state.fw_last_for_progress = false;
2837  fw_state.fw_next_progress_missing = false; // reset
2838  }
2839 
2840  /* deduplicate: rules_array is sorted, but not deduplicated:
2841  * both mpm and stored state could give us the same sid.
2842  * As they are back to back in that case we can check for it
2843  * here. We select the stored state one as that comes first
2844  * in the array. */
2845  while ((i + 1) < array_idx &&
2846  det_ctx->tx_candidates[i].s == det_ctx->tx_candidates[i + 1].s) {
2847  SCLogDebug("%p/%" PRIu64 " inspecting SKIP NEXT: sid %u (%u), flags %08x",
2848  tx.tx_ptr, tx.tx_id, s->id, s->iid, inspect_flags ? *inspect_flags : 0);
2849  i++;
2850  }
2851 
2852  SCLogDebug("%p/%" PRIu64 " inspecting: sid %u (%u), flags %08x", tx.tx_ptr, tx.tx_id,
2853  s->id, s->iid, inspect_flags ? *inspect_flags : 0);
2854 
2855  if (inspect_flags) {
2856  if (*inspect_flags & DE_STATE_FLAG_FULL_INSPECT) {
2857  SCLogDebug("%p/%" PRIu64
2858  " inspecting: sid %u (%u), flags %08x DE_STATE_FLAG_FULL_INSPECT",
2859  tx.tx_ptr, tx.tx_id, s->id, s->iid, *inspect_flags);
2860 
2861  /* if we're still in the same progress state as an earlier full
2862  * match, we need to apply the same accept */
2863  if (have_fw_rules) {
2864  DetectRunTxFirewallRuleStatefulReApplyMatch(
2865  det_ctx, s, &tx, &fw_state, p, flow_flags);
2866  }
2867  continue;
2868  }
2869  if (*inspect_flags & DE_STATE_FLAG_SIG_CANT_MATCH) {
2870  SCLogDebug("%p/%" PRIu64
2871  " inspecting: sid %u (%u), flags %08x DE_STATE_FLAG_SIG_CANT_MATCH",
2872  tx.tx_ptr, tx.tx_id, s->id, s->iid, *inspect_flags);
2873  continue;
2874  }
2875 
2876  /* continue previous inspection */
2877  SCLogDebug("%p/%" PRIu64 " Continuing sid %u", tx.tx_ptr, tx.tx_id, s->id);
2878  } else {
2879  /* start new inspection */
2880  SCLogDebug("%p/%"PRIu64" Start sid %u", tx.tx_ptr, tx.tx_id, s->id);
2881  }
2882 
2883  if (have_fw_rules) {
2884  /* check if we should run this rule and update the firewall flow state */
2885  const enum DetectTxFirewallFlowControl fw_r = DetectRunTxCheckRuleState(
2886  det_ctx, p, f, &tx, s, i, array_idx, can->fw_lte_counted, &fw_state);
2887  SCLogDebug("fw fw_skip_app_filter:%s skip_fw_hook:%s "
2888  "skip_before_progress:%u fw_last_for_progress:%s "
2889  "fw_next_progress_missing:%s",
2890  BOOL2STR(fw_state.fw_skip_app_filter), BOOL2STR(fw_state.skip_fw_hook),
2893  if (fw_r == DETECT_TX_FW_FC_SKIP)
2894  continue;
2895  else if (fw_r == DETECT_TX_FW_FC_BREAK)
2896  break;
2897  }
2898 
2899  /* call individual rule inspection */
2901  const int r = DetectRunTxInspectRule(tv, de_ctx, det_ctx, p, f, flow_flags,
2902  alstate, &tx, s, inspect_flags, can, scratch);
2903  SCLogDebug("s %u r %d", s->id, r);
2904  if (have_fw_rules && r == -1 && (s->flags & SIG_FLAG_FW_HOOK_LTE) &&
2905  can->fw_lte_counted && tx.tx_progress >= s->app_progress_hook) {
2906  DetectFwRetireLteRule(det_ctx, can);
2907  }
2908  if (r == 1) {
2909  /* match */
2910  DetectRunPostMatch(tv, det_ctx, p, s);
2911 
2912  SCLogDebug(
2913  "%p/%" PRIu64 " sig %u (%u) matched", tx.tx_ptr, tx.tx_id, s->id, s->iid);
2914 
2915  if ((s->flags & SIG_FLAG_FIREWALL) == 0) {
2916  AlertQueueAppendAppTx(det_ctx, s, p, tx.tx_id, tx.tx_type, 0);
2917  } else {
2918  DetectRunTxFirewallRuleFullMatch(det_ctx, s, &tx, &fw_state, f, p, flow_flags);
2919  }
2920  } else if (r == 0) {
2921  SCLogDebug("sid %u partial match", s->id);
2922  if (DetectRunTxFirewallRulePartialMatch(det_ctx, s, &tx, p) == 1) {
2923  break;
2924  }
2925  } else if (r == -1) {
2926  if ((s->flags & SIG_FLAG_FIREWALL) != 0 &&
2927  DetectRunTxFirewallRuleNoMatch(det_ctx, s, &tx, &fw_state, p, flow_flags) ==
2928  1) {
2929  DetectRunTxClearInspected(det_ctx);
2930  return;
2931  }
2932  } /* r == -2: no match, but not final: the rule is revisited, so a
2933  * hook default now could decide the packet before the revisit can */
2934  DetectVarProcessList(det_ctx, p->flow, p);
2935  RULE_PROFILING_END(det_ctx, s, r == 1, p);
2936 
2937  if (det_ctx->post_rule_work_queue.len > 0) {
2938  SCLogDebug("%p/%" PRIu64 " post_rule_work_queue len %u", tx.tx_ptr, tx.tx_id,
2939  det_ctx->post_rule_work_queue.len);
2940  /* run post match prefilter engines on work queue */
2941  PrefilterPostRuleMatch(det_ctx, scratch->sgh, p, f);
2942 
2943  uint32_t prev_array_idx = array_idx;
2944 
2945  /* Check if there is room for the pmq results before appending them. */
2946  const uint32_t needed = array_idx + det_ctx->pmq.rule_id_array_cnt;
2947  if (!(RuleMatchCandidateTxArrayHasSpace(det_ctx, needed))) {
2948  RuleMatchCandidateTxArrayExpand(det_ctx, needed);
2949  }
2950 
2951  for (uint32_t j = 0; j < det_ctx->pmq.rule_id_array_cnt; j++) {
2952  const Signature *ts = de_ctx->sig_array[det_ctx->pmq.rule_id_array[j]];
2953  if (ts->app_inspect != NULL) {
2954  const SigIntId id = ts->iid;
2955  det_ctx->tx_candidates[array_idx].s = ts;
2956  det_ctx->tx_candidates[array_idx].id = id;
2957  det_ctx->tx_candidates[array_idx].fw_lte_counted = false;
2958  det_ctx->tx_candidates[array_idx].fw_lte_header_ok = false;
2959  det_ctx->tx_candidates[array_idx].flags = NULL;
2960  det_ctx->tx_candidates[array_idx].stream_reset = 0;
2961 
2962  /* a mid-walk enabled LTE rule must count as coverage
2963  * for the remaining checks of this walk */
2964  if (have_fw_rules) {
2965  DetectFwCountAppendedLteRule(det_ctx, p, f, &fw_state, ts, array_idx);
2966  }
2967  array_idx++;
2968 
2969  SCLogDebug("%p/%" PRIu64 " rule %u (%u) added from 'post match' prefilter",
2970  tx.tx_ptr, tx.tx_id, ts->id, id);
2971  }
2972  }
2973  SCLogDebug("%p/%" PRIu64 " rules added from 'post match' prefilter: %u", tx.tx_ptr,
2974  tx.tx_id, array_idx - prev_array_idx);
2975  if (prev_array_idx != array_idx) {
2976  /* sort, but only part of array we're still going to process */
2977  qsort(det_ctx->tx_candidates + i, array_idx - i, sizeof(RuleMatchCandidateTx),
2978  DetectRunTxSortHelper);
2979  }
2980  det_ctx->post_rule_work_queue.len = 0;
2981  PMQ_RESET(&det_ctx->pmq);
2982  }
2983  }
2984 
2985  /* see if we have any updated state to store in the tx */
2986 
2987  /* this side of the tx is done */
2988  if (tx.tx_progress >= tx.tx_end_state) {
2989  SCLogDebug("%" PRIu64 ": %s tx done", PcapPacketCntGet(p),
2990  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient");
2991  const uint8_t inspected_flag = (flow_flags & STREAM_TOSERVER)
2994  tx.tx_data_ptr->flags |= inspected_flag;
2995  SCLogDebug("%p/%" PRIu64 " tx is done for direction %s. Progress %02x", tx.tx_ptr,
2996  tx.tx_id, flow_flags & STREAM_TOSERVER ? "toserver" : "toclient",
2997  tx.detect_progress);
2998  }
2999 
3000  if (tx.detect_progress != tx.detect_progress_orig) {
3001  SCLogDebug("%" PRIu64 ": %s tx state change %u -> %u", PcapPacketCntGet(p),
3002  flow_flags & STREAM_TOSERVER ? "toserver" : "toclient", tx.detect_progress_orig,
3003  tx.detect_progress);
3004  SCLogDebug("%p/%" PRIu64 " Storing new progress %02x (was %02x)", tx.tx_ptr, tx.tx_id,
3006 
3007  StoreDetectProgress(&tx, flow_flags, tx.detect_progress);
3008  }
3009  next_tx_fw:
3010  DetectRunTxClearInspected(det_ctx);
3011  InspectionBufferClean(det_ctx);
3012 
3013  next:
3014  if (!ires.has_next)
3015  break;
3016  }
3017 
3018  SCLogDebug("packet %" PRIu64 ": tx_inspected %u", PcapPacketCntGet(p), tx_inspected);
3019  if (have_fw_rules) {
3020  if (tx_inspected == 0) {
3021  /* if all tables have been bypassed, we accept:packet */
3022  SCLogDebug("default accept: no app inspect performed");
3023  DetectRunAppendDefaultAccept(det_ctx, p);
3024  } else if (last_tx_skipped) {
3025  /* if the last tx was skipped, we need to apply accept:packet */
3026  // TODO should we check drops first?
3028  SCLogDebug("default accept: last tx skipped");
3029  DetectRunAppendDefaultAccept(det_ctx, p);
3030  }
3031  }
3032 }
3033 
3034 static void DetectRunFrames(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
3035  Packet *p, Flow *f, DetectRunScratchpad *scratch)
3036 {
3037  const SigGroupHead *const sgh = scratch->sgh;
3038  const AppProto alproto = f->alproto;
3039 
3040  /* for TCP, limit inspection to pseudo packets or real packet that did
3041  * an app-layer update. */
3042  if (p->proto == IPPROTO_TCP && !PKT_IS_PSEUDOPKT(p) &&
3043  ((PKT_IS_TOSERVER(p) && (f->flags & FLOW_TS_APP_UPDATED) == 0) ||
3044  (PKT_IS_TOCLIENT(p) && (f->flags & FLOW_TC_APP_UPDATED) == 0))) {
3045  SCLogDebug("pcap_cnt %" PRIu64 ": %s: skip frame inspection for TCP w/o APP UPDATE",
3046  PcapPacketCntGet(p), PKT_IS_TOSERVER(p) ? "toserver" : "toclient");
3047  return;
3048  }
3049  FramesContainer *frames_container = AppLayerFramesGetContainer(f);
3050  if (frames_container == NULL) {
3051  return;
3052  }
3053  Frames *frames;
3054  if (PKT_IS_TOSERVER(p)) {
3055  frames = &frames_container->toserver;
3056  } else {
3057  frames = &frames_container->toclient;
3058  }
3059 
3060  for (uint32_t idx = 0; idx < frames->cnt; idx++) {
3061  SCLogDebug("frame %u", idx);
3062  Frame *frame = FrameGetByIndex(frames, idx);
3063  if (frame == NULL) {
3064  continue;
3065  }
3066 
3067  det_ctx->frame_inspect_progress = 0;
3068  uint32_t array_idx = 0;
3069  uint32_t total_rules = det_ctx->match_array_cnt;
3070 
3071  /* run prefilter engines and merge results into a candidates array */
3072  if (sgh->frame_engines) {
3073  // PACKET_PROFILING_DETECT_START(p, PROF_DETECT_PF_TX);
3074  DetectRunPrefilterFrame(det_ctx, sgh, p, frames, frame, alproto);
3075  // PACKET_PROFILING_DETECT_END(p, PROF_DETECT_PF_TX);
3076  SCLogDebug("%p/%" PRIi64 " rules added from prefilter: %u candidates", frame, frame->id,
3077  det_ctx->pmq.rule_id_array_cnt);
3078 
3079  total_rules += det_ctx->pmq.rule_id_array_cnt;
3080 
3081  if (!(RuleMatchCandidateTxArrayHasSpace(
3082  det_ctx, total_rules))) { // TODO is it safe to overload?
3083  RuleMatchCandidateTxArrayExpand(det_ctx, total_rules);
3084  }
3085 
3086  for (uint32_t i = 0; i < det_ctx->pmq.rule_id_array_cnt; i++) {
3087  const Signature *s = de_ctx->sig_array[det_ctx->pmq.rule_id_array[i]];
3088  const SigIntId id = s->iid;
3089  det_ctx->tx_candidates[array_idx].s = s;
3090  det_ctx->tx_candidates[array_idx].id = id;
3091  det_ctx->tx_candidates[array_idx].flags = NULL;
3092  det_ctx->tx_candidates[array_idx].fw_lte_counted = false;
3093  det_ctx->tx_candidates[array_idx].fw_lte_header_ok = false;
3094  det_ctx->tx_candidates[array_idx].stream_reset = 0;
3095  array_idx++;
3096  }
3097  PMQ_RESET(&det_ctx->pmq);
3098  }
3099  /* merge 'state' rules from the regular prefilter */
3100  uint32_t x = array_idx;
3101  for (uint32_t i = 0; i < det_ctx->match_array_cnt; i++) {
3102  const Signature *s = det_ctx->match_array[i];
3103  if (s->frame_inspect != NULL) {
3104  const SigIntId id = s->iid;
3105  det_ctx->tx_candidates[array_idx].s = s;
3106  det_ctx->tx_candidates[array_idx].id = id;
3107  det_ctx->tx_candidates[array_idx].flags = NULL;
3108  det_ctx->tx_candidates[array_idx].fw_lte_counted = false;
3109  det_ctx->tx_candidates[array_idx].fw_lte_header_ok = false;
3110  det_ctx->tx_candidates[array_idx].stream_reset = 0;
3111  array_idx++;
3112 
3113  SCLogDebug("%p/%" PRIi64 " rule %u (%u) added from 'match' list", frame, frame->id,
3114  s->id, id);
3115  }
3116  }
3117  SCLogDebug("%p/%" PRIi64 " rules added from 'match' list: %u", frame, frame->id,
3118  array_idx - x);
3119  (void)x;
3120 
3121  /* run rules: inspect the match candidates */
3122  for (uint32_t i = 0; i < array_idx; i++) {
3123  const Signature *s = det_ctx->tx_candidates[i].s;
3124 
3125  /* deduplicate: rules_array is sorted, but not deduplicated.
3126  * As they are back to back in that case we can check for it
3127  * here. We select the stored state one as that comes first
3128  * in the array. */
3129  while ((i + 1) < array_idx &&
3130  det_ctx->tx_candidates[i].s == det_ctx->tx_candidates[i + 1].s) {
3131  i++;
3132  }
3133  SCLogDebug("%p/%" PRIi64 " inspecting: sid %u (%u)", frame, frame->id, s->id, s->iid);
3134 
3135  /* start new inspection */
3136  SCLogDebug("%p/%" PRIi64 " Start sid %u", frame, frame->id, s->id);
3137 
3138  /* call individual rule inspection */
3140  bool r = DetectRunInspectRuleHeader(p, f, s, s->flags);
3141  if (r) {
3142  r = DetectRunFrameInspectRule(tv, det_ctx, s, f, p, frames, frame);
3143  if (r) {
3144  /* match */
3145  DetectRunPostMatch(tv, det_ctx, p, s);
3146  det_ctx->frame_id = frame->id;
3147  SCLogDebug(
3148  "%p/%" PRIi64 " sig %u (%u) matched", frame, frame->id, s->id, s->iid);
3149  const uint8_t alert_flags =
3151  if (frame->flags & FRAME_FLAG_TX_ID_SET) {
3152  const uint8_t ipproto = p->proto;
3153  uint8_t sub_state = 0;
3154  void *tx = AppLayerParserGetTx(
3155  ipproto, alproto, p->flow->alstate, frame->tx_id);
3156  if (tx) {
3157  AppLayerTxData *txd = AppLayerParserGetTxData(ipproto, alproto, tx);
3158  sub_state = txd->tx_type;
3159  }
3160  AlertQueueAppendAppTx(det_ctx, s, p, frame->tx_id, sub_state, alert_flags);
3161  } else {
3162  AlertQueueAppendPacket(det_ctx, s, p, alert_flags);
3163  }
3164  }
3165  }
3166  DetectVarProcessList(det_ctx, p->flow, p);
3167  RULE_PROFILING_END(det_ctx, s, r == 1, p);
3168  }
3169 
3170  /* update Frame::inspect_progress here instead of in the code above. The reason is that a
3171  * frame might be used more than once in buffers with transforms. */
3172  if (frame->inspect_progress < det_ctx->frame_inspect_progress) {
3173  frame->inspect_progress = det_ctx->frame_inspect_progress;
3174  SCLogDebug("frame->inspect_progress: %" PRIu64 " -> updated", frame->inspect_progress);
3175  } else {
3176  SCLogDebug(
3177  "frame->inspect_progress: %" PRIu64 " -> not updated", frame->inspect_progress);
3178  }
3179 
3180  SCLogDebug("%p/%" PRIi64 " rules inspected, running cleanup", frame, frame->id);
3181  InspectionBufferClean(det_ctx);
3182  }
3183 }
3184 
3185 static DetectEngineThreadCtx *GetTenantById(HashTable *h, uint32_t id)
3186 {
3187  /* technically we need to pass a DetectEngineThreadCtx struct with the
3188  * tenant_id member. But as that member is the first in the struct, we
3189  * can use the id directly. */
3190  return HashTableLookup(h, &id, 0);
3191 }
3192 
3193 static void DetectFlow(ThreadVars *tv,
3195  Packet *p)
3196 {
3197  Flow *const f = p->flow;
3198 
3199  /* we check the flow drop here, and not the packet drop. This is
3200  * to allow stream engine "invalid" drop packets to still be
3201  * evaluated by the stream event rules. */
3202  if (f->flags & FLOW_ACTION_DROP) {
3204  SCReturn;
3205  }
3206 
3207  /* in firewall mode, we still need to run the fw rulesets even for exception policy pass */
3208  bool skip = (p->flags & PKT_NOPACKET_INSPECTION || f->flags & (FLOW_ACTION_PASS));
3209  if (EngineModeIsFirewall() && (f->flags & FLOW_ACTION_ACCEPT) == 0) {
3210  skip = false;
3211  }
3212  if (skip) {
3213  /* enforce prior accept:flow */
3214  if (f->flags & FLOW_ACTION_ACCEPT) {
3215  p->action |= ACTION_ACCEPT;
3216  }
3217  /* hack: if we are in pass the entire flow mode, we need to still
3218  * update the inspect_id forward. So test for the condition here,
3219  * and call the update code if necessary. */
3220  const int pass = (f->flags & (FLOW_ACTION_PASS | FLOW_ACTION_ACCEPT));
3221  if (pass) {
3222  uint8_t flags = STREAM_FLAGS_FOR_PACKET(p);
3224  if (f->alstate) {
3226  }
3227  }
3228  SCLogDebug("p->pcap %" PRIu64 ": no detection on packet, "
3229  "PKT_NOPACKET_INSPECTION is set",
3230  PcapPacketCntGet(p));
3231  return;
3232  }
3233 
3234  /* see if the packet matches one or more of the sigs */
3235  DetectRun(tv, de_ctx, det_ctx, p);
3236 }
3237 
3238 
3239 static void DetectNoFlow(ThreadVars *tv,
3241  Packet *p)
3242 {
3243  /* No need to perform any detection on this packet, if the given flag is set.*/
3245  return;
3246  }
3247 
3248  /* see if the packet matches one or more of the sigs */
3249  DetectRun(tv, de_ctx, det_ctx, p);
3250 }
3251 
3253 {
3254  const DetectEngineCtx *de_ctx = det_ctx->de_ctx;
3255  const SigGroupHead *sgh = de_ctx->pre_flow_sgh;
3256 
3257  SCLogDebug("thread id: %u, packet %" PRIu64 ", sgh %p", tv->id, PcapPacketCntGet(p), sgh);
3258  DetectRunPacketHook(tv, de_ctx, det_ctx, sgh, p, DETECT_FIREWALL_POLICY_PRE_FLOW);
3259  return p->action;
3260 }
3261 
3263 {
3264  const DetectEngineCtx *de_ctx = det_ctx->de_ctx;
3265  const int direction = (PKT_IS_TOCLIENT(p) != 0);
3266  const SigGroupHead *sgh = de_ctx->pre_stream_sgh[direction];
3267 
3268  SCLogDebug("thread id: %u, packet %" PRIu64 ", sgh %p", tv->id, PcapPacketCntGet(p), sgh);
3269  DetectRunPacketHook(tv, de_ctx, det_ctx, sgh, p, DETECT_FIREWALL_POLICY_PRE_STREAM);
3270  return p->action;
3271 }
3272 
3273 /** \brief Detection engine thread wrapper.
3274  * \param tv thread vars
3275  * \param p packet to inspect
3276  * \param data thread specific data
3277  * \param pq packet queue
3278  * \retval TM_ECODE_FAILED error
3279  * \retval TM_ECODE_OK ok
3280  */
3282 {
3284 
3285  DetectEngineCtx *de_ctx = NULL;
3286  DetectEngineThreadCtx *det_ctx = (DetectEngineThreadCtx *)data;
3287  if (det_ctx == NULL) {
3288  printf("ERROR: Detect has no thread ctx\n");
3289  goto error;
3290  }
3291 
3292  if (unlikely(SC_ATOMIC_GET(det_ctx->so_far_used_by_detect) == 0)) {
3293  (void)SC_ATOMIC_SET(det_ctx->so_far_used_by_detect, 1);
3294  SCLogDebug("Detect Engine using new det_ctx - %p",
3295  det_ctx);
3296  }
3297 
3298  /* if in MT mode _and_ we have tenants registered, use
3299  * MT logic. */
3300  if (det_ctx->mt_det_ctxs_cnt > 0 && det_ctx->TenantGetId != NULL)
3301  {
3302  uint32_t tenant_id = p->tenant_id;
3303  if (tenant_id == 0)
3304  tenant_id = det_ctx->TenantGetId(det_ctx, p);
3305  if (tenant_id > 0 && tenant_id < det_ctx->mt_det_ctxs_cnt) {
3306  p->tenant_id = tenant_id;
3307  det_ctx = GetTenantById(det_ctx->mt_det_ctxs_hash, tenant_id);
3308  if (det_ctx == NULL)
3309  return TM_ECODE_OK;
3310  de_ctx = det_ctx->de_ctx;
3311  if (de_ctx == NULL)
3312  return TM_ECODE_OK;
3313 
3314  if (unlikely(SC_ATOMIC_GET(det_ctx->so_far_used_by_detect) == 0)) {
3315  (void)SC_ATOMIC_SET(det_ctx->so_far_used_by_detect, 1);
3316  SCLogDebug("MT de_ctx %p det_ctx %p (tenant %u)", de_ctx, det_ctx, tenant_id);
3317  }
3318  } else {
3319  /* use default if no tenants are registered for this packet */
3320  de_ctx = det_ctx->de_ctx;
3321  }
3322  } else {
3323  de_ctx = det_ctx->de_ctx;
3324  }
3325 
3326  if (p->flow) {
3327  DetectFlow(tv, de_ctx, det_ctx, p);
3328  } else {
3329  DetectNoFlow(tv, de_ctx, det_ctx, p);
3330  }
3331 
3332 #ifdef PROFILE_RULES
3333  /* aggregate statistics */
3334  struct timeval ts;
3335  gettimeofday(&ts, NULL);
3336  if (ts.tv_sec != det_ctx->rule_perf_last_sync) {
3337  SCProfilingRuleThreatAggregate(det_ctx);
3338  det_ctx->rule_perf_last_sync = ts.tv_sec;
3339  }
3340 #endif
3341 
3342  return TM_ECODE_OK;
3343 error:
3344  return TM_ECODE_FAILED;
3345 }
3346 
3347 /** \brief disable file features we don't need
3348  * Called if we have no detection engine.
3349  */
3351 {
3352  DetectPostInspectFileFlagsUpdate(f, NULL /* no sgh */, STREAM_TOSERVER);
3353  DetectPostInspectFileFlagsUpdate(f, NULL /* no sgh */, STREAM_TOCLIENT);
3354 }
3355 
3356 #if defined(UNITTESTS) || defined(FUZZ)
3357 /**
3358  * \brief wrapper for old tests
3359  */
3362 {
3363  if (p->flow) {
3364  DetectFlow(tv, de_ctx, det_ctx, p);
3365  } else {
3366  DetectNoFlow(tv, de_ctx, det_ctx, p);
3367  }
3368 }
3369 #endif
3370 
3371 /*
3372  * TESTS
3373  */
3374 
3375 #ifdef UNITTESTS
3376 #include "tests/detect.c"
3377 #endif
PKT_IS_TOCLIENT
#define PKT_IS_TOCLIENT(p)
Definition: decode.h:257
default_accept
thread_local Signature default_accept
Definition: detect.c:2289
DetectEngineAppInspectionEngine_::stream
bool stream
Definition: detect.h:396
PacketCheckAction
bool PacketCheckAction(const Packet *p, const uint8_t a)
Definition: packet.c:51
FLOWFILE_NO_MD5_TS
#define FLOWFILE_NO_MD5_TS
Definition: flow.h:147
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:34
SIG_GROUP_HEAD_HAVEFILEMD5
#define SIG_GROUP_HEAD_HAVEFILEMD5
Definition: detect.h:1581
DETECT_TX_FW_FC_SKIP
@ DETECT_TX_FW_FC_SKIP
Definition: detect.c:1225
DetectFirewallPolicies
Definition: detect.h:933
PACKET_ALERT_FLAG_TX_GUESSED
#define PACKET_ALERT_FLAG_TX_GUESSED
Definition: decode.h:297
FLOWFILE_NO_MD5_TC
#define FLOWFILE_NO_MD5_TC
Definition: flow.h:148
SigGroupHead_::tx_engines
PrefilterEngine * tx_engines
Definition: detect.h:1745
DetectEngineAppInspectionEngine_
Definition: detect.h:391
Packet_::proto
uint8_t proto
Definition: decode.h:550
DetectTransaction_::tx_data_ptr
struct AppLayerTxData * tx_data_ptr
Definition: detect-engine-prefilter.h:34
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:395
DE_STATE_CHUNK_SIZE
#define DE_STATE_CHUNK_SIZE
Definition: detect-engine-state.h:55
RuleMatchCandidateTx::stream_stored
bool stream_stored
Definition: detect.h:1246
Frame::inspect_progress
uint64_t inspect_progress
Definition: app-layer-frames.h:59
DetectEngineThreadCtx_::alert_queue_size
uint16_t alert_queue_size
Definition: detect.h:1389
PROF_DETECT_GETSGH
@ PROF_DETECT_GETSGH
Definition: suricata-common.h:463
ts
uint64_t ts
Definition: source-erf-file.c:68
AppLayerTxData::flags
uint8_t flags
Definition: app-layer-parser.h:182
PacketAlerts_::firewall_discarded
uint16_t firewall_discarded
Definition: decode.h:308
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@82 v2
SCAppLayerParserStateIssetFlag
uint16_t SCAppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint16_t flag)
Definition: app-layer-parser.c:2079
FLOW_ACTION_BY_FIREWALL
#define FLOW_ACTION_BY_FIREWALL
Definition: flow.h:125
detect-engine.h
detect-engine-proto.h
DetectEngineThreadCtx_::match_array_cnt
SigIntId match_array_cnt
Definition: detect.h:1403
Frame::tx_id
uint64_t tx_id
Definition: app-layer-frames.h:58
DetectEngineStateDirection_::flags
uint8_t flags
Definition: detect-engine-state.h:91
DetectEngineThreadCtx_::counter_alerts
StatsCounterId counter_alerts
Definition: detect.h:1344
detect-dsize.h
Signature_::addr_src_match6
DetectMatchAddressIPv6 * addr_src_match6
Definition: detect.h:710
Flow_::flags
uint64_t flags
Definition: flow.h:410
DetectEngineThreadCtx_::byte_values
ByteVarSlot * byte_values
Definition: detect.h:1332
SIG_FLAG_FW_HOOK_LTE
#define SIG_FLAG_FW_HOOK_LTE
Definition: detect.h:226
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1320
RuleMatchCandidateTx::fw_lte_header_ok
bool fw_lte_header_ok
Definition: detect.h:1242
DetectEngineCtx_::decoder_event_sgh
struct SigGroupHead_ * decoder_event_sgh
Definition: detect.h:1055
FlowGetPacketDirection
int FlowGetPacketDirection(const Flow *f, const Packet *p)
determine the direction of the packet compared to the flow
Definition: flow.c:283
DetectEngineCtx_::flow_gh
DetectEngineLookupFlow flow_gh[FLOW_STATES]
Definition: detect.h:994
DETECT_TABLE_APP_FILTER
@ DETECT_TABLE_APP_FILTER
Definition: detect.h:541
DE_STATE_FLAG_SIG_CANT_MATCH
#define DE_STATE_FLAG_SIG_CANT_MATCH
Definition: detect-engine-state.h:59
DetectFirewallAppPolicy
Definition: detect.h:922
DetectEngineAppInspectionEngine_::next
struct DetectEngineAppInspectionEngine_ * next
Definition: detect.h:417
DETECT_PROTO_IPV6
#define DETECT_PROTO_IPV6
Definition: detect-engine-proto.h:32
detect-engine-siggroup.h
SigGroupHead_::flags
uint16_t flags
Definition: detect.h:1734
PostRuleMatchWorkQueue::len
uint32_t len
Definition: detect.h:1270
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1373
DetectEngineState_
Definition: detect-engine-state.h:95
stream-tcp.h
PrefilterRuleStore_::rule_id_array_cnt
uint32_t rule_id_array_cnt
Definition: util-prefilter.h:40
detect-engine-event.h
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1733
PACKET_ALERT_FLAG_STATE_MATCH
#define PACKET_ALERT_FLAG_STATE_MATCH
Definition: decode.h:287
DetectEngineCtx_::guess_applayer
bool guess_applayer
Definition: detect.h:1017
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
detect.c
AppLayerTxData::tx_type
uint8_t tx_type
Definition: app-layer-parser.h:215
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
PKT_DROP_REASON_FW_RULES
@ PKT_DROP_REASON_FW_RULES
Definition: decode.h:426
Signature_::app_progress_hook
uint8_t app_progress_hook
Definition: detect.h:700
AppLayerFramesGetContainer
FramesContainer * AppLayerFramesGetContainer(const Flow *f)
Definition: app-layer-parser.c:209
KEYWORD_PROFILING_SET_LIST
#define KEYWORD_PROFILING_SET_LIST(ctx, list)
Definition: util-profiling.h:46
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:1603
PcapPacketCntGet
uint64_t PcapPacketCntGet(const Packet *p)
Definition: decode.c:1244
DetectEngineAppInspectionEngine_::Callback
InspectEngineFuncPtr Callback
Definition: detect.h:410
Signature_::alproto
AppProto alproto
Definition: detect.h:668
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
FLOW_SGH_TOCLIENT
#define FLOW_SGH_TOCLIENT
Definition: flow.h:74
AppLayerParserSetTransactionInspectId
void AppLayerParserSetTransactionInspectId(const Flow *f, AppLayerParserState *pstate, void *alstate, const uint8_t flags, bool tag_txs_as_inspected)
Definition: app-layer-parser.c:844
DETECT_TX_FW_FC_OK
@ DETECT_TX_FW_FC_OK
Definition: detect.c:1224
DetectEngineStateDirection_::cnt
SigIntId cnt
Definition: detect-engine-state.h:89
AppLayerGetTxIterator
AppLayerGetTxIteratorFunc AppLayerGetTxIterator(const uint8_t ipproto, const AppProto alproto)
Definition: app-layer-parser.c:783
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
SigMatchData_::is_last
bool is_last
Definition: detect.h:342
AppLayerTxData::de_state
DetectEngineState * de_state
Definition: app-layer-parser.h:223
InspectionBufferClean
void InspectionBufferClean(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine-inspect-buffer.c:30
Flow_::proto
uint8_t proto
Definition: flow.h:382
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:306
DetectEngineThreadCtx_::tx_id
uint64_t tx_id
Definition: detect.h:1376
SCFlowGetAppProtocol
AppProto SCFlowGetAppProtocol(const Flow *f)
Definition: flow.c:1288
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:343
action-globals.h
FLOWFILE_NO_MAGIC_TS
#define FLOWFILE_NO_MAGIC_TS
Definition: flow.h:139
FramesContainer::toserver
Frames toserver
Definition: app-layer-frames.h:78
Packet_::flags
uint32_t flags
Definition: decode.h:582
ssn
TcpSession ssn
Definition: fuzz_dataset.c:31
Packet_::action
uint8_t action
Definition: decode.h:644
ICMPV4_DEST_UNREACH_IS_VALID
#define ICMPV4_DEST_UNREACH_IS_VALID(p)
Definition: decode-icmpv4.h:253
Frame
Definition: app-layer-frames.h:49
Flow_
Flow data structure.
Definition: flow.h:360
DetectFirewallPacketPolicies
DetectFirewallPacketPolicies
Definition: detect.h:909
PROF_DETECT_ALERT
@ PROF_DETECT_ALERT
Definition: suricata-common.h:474
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1420
SIG_GROUP_HEAD_HAVEFILESHA1
#define SIG_GROUP_HEAD_HAVEFILESHA1
Definition: detect.h:1583
ByteVarSlot_::writer
const Signature * writer
Definition: detect.h:1289
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its normalized string equivalent.
Definition: app-layer-protos.c:53
FLOW_TC_APP_UPDATED
#define FLOW_TC_APP_UPDATED
Definition: flow.h:119
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:966
DetectEngineCtx_::pre_stream_sgh
struct SigGroupHead_ * pre_stream_sgh[2]
Definition: detect.h:1196
DETECT_PROTO_ETHERNET
#define DETECT_PROTO_ETHERNET
Definition: detect-engine-proto.h:33
DetectRunScratchpad
struct DetectRunScratchpad DetectRunScratchpad
DetectFirewallAppPolicy::policy
struct DetectFirewallPolicy policy
Definition: detect.h:927
PROF_DETECT_PF_TX
@ PROF_DETECT_PF_TX
Definition: suricata-common.h:469
Frames::cnt
uint16_t cnt
Definition: app-layer-frames.h:65
Frame::id
int64_t id
Definition: app-layer-frames.h:57
DetectFirewallPolicies::pkt_policy_signatures
Signature * pkt_policy_signatures[DETECT_FIREWALL_POLICY_SIZE]
Definition: detect.h:936
DetectEngineThreadCtx_::p
Packet * p
Definition: detect.h:1384
DetectEngineState_::dir_state
DetectEngineStateDirection dir_state[2]
Definition: detect-engine-state.h:96
FrameGetByIndex
Frame * FrameGetByIndex(Frames *frames, const uint32_t idx)
Definition: app-layer-frames.c:146
RuleMatchCandidateTx::id
SigIntId id
Definition: detect.h:1240
PROF_DETECT_CLEANUP
@ PROF_DETECT_CLEANUP
Definition: suricata-common.h:476
SIG_FLAG_DST_ANY
#define SIG_FLAG_DST_ANY
Definition: detect.h:216
ACTION_SCOPE_FLOW
@ ACTION_SCOPE_FLOW
Definition: action-globals.h:45
NO_TX
#define NO_TX
Definition: detect.c:1614
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:236
HashTable_
Definition: util-hash.h:35
SIG_FLAG_TXBOTHDIR
#define SIG_FLAG_TXBOTHDIR
Definition: detect.h:224
MIN
#define MIN(x, y)
Definition: suricata-common.h:413
Packet_::sig_mask
SignatureMask sig_mask
Definition: decode.h:570
Frames
Definition: app-layer-frames.h:64
FramesContainer
Definition: app-layer-frames.h:77
DetectFirewallAppTxState::fw_skip_app_filter
bool fw_skip_app_filter
Definition: detect.c:1778
DetectEngineThreadCtx_::counter_alerts_overflow
StatsCounterId counter_alerts_overflow
Definition: detect.h:1346
DetectRunScratchpad
Definition: detect.c:70
SIG_GROUP_HEAD_HAVERAWSTREAM
#define SIG_GROUP_HEAD_HAVERAWSTREAM
Definition: detect.h:1577
FLOW_ACTION_DROP
#define FLOW_ACTION_DROP
Definition: flow.h:69
DetectFirewallAppTxState::fw_lte_cover_active
bool fw_lte_cover_active
Definition: detect.c:1788
TcpStream_::flags
uint16_t flags
Definition: stream-tcp-private.h:107
FLOWFILE_NONE
#define FLOWFILE_NONE
Definition: flow.h:171
detect-engine-frame.h
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:3360
p
Packet * p
Definition: fuzz_dataset.c:30
Signature_::sm_arrays
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:730
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:205
PKT_NOPAYLOAD_INSPECTION
#define PKT_NOPAYLOAD_INSPECTION
Definition: decode.h:1306
PACKET_PROFILING_DETECT_END
#define PACKET_PROFILING_DETECT_END(p, id)
Definition: util-profiling.h:221
DetectEngineThreadCtx_::counter_alerts_suppressed
StatsCounterId counter_alerts_suppressed
Definition: detect.h:1350
Detect
TmEcode Detect(ThreadVars *tv, Packet *p, void *data)
Detection engine thread wrapper.
Definition: detect.c:3281
detect-engine-payload.h
DetectEngineThreadCtx_::counter_firewall_discarded_alerts
StatsCounterId counter_firewall_discarded_alerts
Definition: detect.h:1348
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:564
EngineModeIsFirewall
bool EngineModeIsFirewall(void)
Definition: suricata.c:240
SIG_FLAG_SRC_ANY
#define SIG_FLAG_SRC_ANY
Definition: detect.h:215
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
DetectRunFrameInspectRule
bool DetectRunFrameInspectRule(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, const Signature *s, Flow *f, Packet *p, const Frames *frames, const Frame *frame)
Definition: detect-engine-frame.c:229
Flow_::protoctx
void * protoctx
Definition: flow.h:440
DetectFirewallAppTxState::skip_before_progress
uint8_t skip_before_progress
Definition: detect.c:1780
AppLayerGetTxIterTuple::tx_ptr
void * tx_ptr
Definition: app-layer-parser.h:160
SigMatchData_
Data needed for Match()
Definition: detect.h:340
DetectFirewallAppPolicy::alert_signature
Signature * alert_signature
Definition: detect.h:930
DeStateStoreItem_::sid
SigIntId sid
Definition: detect-engine-state.h:77
RuleMatchCandidateTx::s
const Signature * s
Definition: detect.h:1252
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:50
SigMatchData_::type
uint16_t type
Definition: detect.h:341
DetectEngineCtx_::version
uint32_t version
Definition: detect.h:1051
DetectFirewallPolicy::action
uint8_t action
Definition: detect.h:918
DisableDetectFlowFileFlags
void DisableDetectFlowFileFlags(Flow *f)
disable file features we don't need Called if we have no detection engine.
Definition: detect.c:3350
AppLayerParserGetTransactionInspectId
uint64_t AppLayerParserGetTransactionInspectId(AppLayerParserState *pstate, uint8_t direction)
Definition: app-layer-parser.c:813
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:657
detect-engine-prefilter.h
DetectRunPrefilterFrame
void DetectRunPrefilterFrame(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p, const Frames *frames, const Frame *frame, const AppProto alproto)
Definition: detect-engine-frame.c:74
Packet_::events
PacketEngineEvents events
Definition: decode.h:663
Signature_::frame_inspect
DetectEngineFrameInspectionEngine * frame_inspect
Definition: detect.h:726
PROF_DETECT_PF_SORT2
@ PROF_DETECT_PF_SORT2
Definition: suricata-common.h:472
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
PacketCreateMask
void PacketCreateMask(Packet *p, SignatureMask *mask, AppProto alproto, bool app_decoder_events)
Definition: detect-engine-build.c:406
SIG_FLAG_APPLAYER
#define SIG_FLAG_APPLAYER
Definition: detect.h:223
KEYWORD_PROFILING_END
#define KEYWORD_PROFILING_END(ctx, type, m)
Definition: util-profiling.h:64
detect-flowvar.h
RuleMatchCandidateTx::fw_lte_counted
bool fw_lte_counted
Definition: detect.h:1241
DetectEngineThreadCtx_::mt_det_ctxs_hash
HashTable * mt_det_ctxs_hash
Definition: detect.h:1307
TcpSession_::flags
uint32_t flags
Definition: stream-tcp-private.h:301
DetectEnginePktInspectionRun
bool DetectEnginePktInspectionRun(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, const Signature *s, Flow *f, Packet *p, uint8_t *alert_flags)
Definition: detect-engine.c:1999
SIG_FLAG_FIREWALL
#define SIG_FLAG_FIREWALL
Definition: detect.h:220
Flow_::sgh_toserver
const struct SigGroupHead_ * sgh_toserver
Definition: flow.h:493
PROF_DETECT_TX_UPDATE
@ PROF_DETECT_TX_UPDATE
Definition: suricata-common.h:475
DetectEngineAppInspectionEngine_::id
uint8_t id
Definition: detect.h:394
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:212
StatsCounterAvgAddI64
void StatsCounterAvgAddI64(StatsThreadContext *stats, StatsCounterAvgId id, int64_t x)
Definition: counters.c:238
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:399
FLOWFILE_INIT
#define FLOWFILE_INIT
Definition: flow.h:136
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:485
FLOW_ACTION_PASS
#define FLOW_ACTION_PASS
Definition: flow.h:116
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH_FILES
Definition: detect-engine-state.h:46
FLOWFILE_NO_SHA1_TC
#define FLOWFILE_NO_SHA1_TC
Definition: flow.h:152
FramesContainer::toclient
Frames toclient
Definition: app-layer-frames.h:79
DetectEngineCtx_::pre_flow_sgh
struct SigGroupHead_ * pre_flow_sgh
Definition: detect.h:1201
DetectEngineThreadCtx_::tx_candidates
RuleMatchCandidateTx * tx_candidates
Definition: detect.h:1405
SIG_TYPE_PKT
@ SIG_TYPE_PKT
Definition: detect.h:73
Signature_::addr_src_match4
DetectMatchAddressIPv4 * addr_src_match4
Definition: detect.h:707
decode.h
PKT_SRC_WIRE
@ PKT_SRC_WIRE
Definition: decode.h:69
DetectEngineCtx_::fw_policies
struct DetectFirewallPolicies * fw_policies
Definition: detect.h:997
DETECT_PROTO_ARP
#define DETECT_PROTO_ARP
Definition: detect-engine-proto.h:34
TOSERVER
#define TOSERVER
Definition: flow.h:45
DetectTransaction_::tx_end_state
const uint8_t tx_end_state
Definition: detect-engine-prefilter.h:44
DetectEngineThreadCtx_::counter_mpm_list
StatsCounterAvgId counter_mpm_list
Definition: detect.h:1352
AppLayerTxData
Definition: app-layer-parser.h:172
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
PKT_IS_TOSERVER
#define PKT_IS_TOSERVER(p)
Definition: decode.h:256
DetectFirewallAppTxState::skip_fw_hook
bool skip_fw_hook
Definition: detect.c:1779
Prefilter
void Prefilter(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p, const uint8_t flags, const SignatureMask mask)
Definition: detect-engine-prefilter.c:244
DetectTransaction_::detect_progress_orig
const uint8_t detect_progress_orig
Definition: detect-engine-prefilter.h:41
DetectEngineThreadCtx_
Definition: detect.h:1295
DEBUG_VALIDATE_PACKET
#define DEBUG_VALIDATE_PACKET(p)
Definition: util-validate.h:108
DeStateStoreItem_::flags
uint32_t flags
Definition: detect-engine-state.h:76
PKT_STREAM_ADD
#define PKT_STREAM_ADD
Definition: decode.h:1314
DetectTransaction_::detect_progress
uint8_t detect_progress
Definition: detect-engine-prefilter.h:39
AlertQueueAppendAppTxFromPacket
void AlertQueueAppendAppTxFromPacket(DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, uint64_t tx_id, const uint8_t sub_state, uint8_t alert_flags)
Append signature to local packet alert queue for later preprocessing This does not automatically set ...
Definition: detect-engine-alert.c:466
BIT_U32
#define BIT_U32(n)
Definition: suricata-common.h:422
DetectEngineThreadCtx_::tx_candidates_size
uint32_t tx_candidates_size
Definition: detect.h:1406
FRAME_FLAG_TX_ID_SET
#define FRAME_FLAG_TX_ID_SET
Definition: app-layer-frames.h:42
PKT_PSEUDO_STREAM_END
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1322
DETECT_TX_FW_FC_BREAK
@ DETECT_TX_FW_FC_BREAK
Definition: detect.c:1226
PKT_IS_FRAGMENT
#define PKT_IS_FRAGMENT
Definition: decode.h:1344
BOOL2STR
#define BOOL2STR(b)
Definition: util-debug.h:542
STREAM_FLAGS_FOR_PACKET
#define STREAM_FLAGS_FOR_PACKET(p)
Definition: stream.h:30
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
PMQ_RESET
#define PMQ_RESET(pmq)
Definition: util-prefilter.h:46
detect-engine-mpm.h
DetectEngineLookupFlow_::sgh
struct SigGroupHead_ * sgh[256]
Definition: detect.h:865
HashTableLookup
void * HashTableLookup(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:193
AppLayerGetTxIterTuple::tx_id
uint64_t tx_id
Definition: app-layer-parser.h:161
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
DeStateStore_::next
struct DeStateStore_ * next
Definition: detect-engine-state.h:82
DetectTransaction_::is_last
bool is_last
Definition: detect-engine-prefilter.h:45
Packet_::sp
Port sp
Definition: decode.h:535
FLOWFILE_NO_SHA256_TS
#define FLOWFILE_NO_SHA256_TS
Definition: flow.h:155
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:41
PKT_DETECT_HAS_STREAMDATA
#define PKT_DETECT_HAS_STREAMDATA
Definition: decode.h:1359
FLOW_PKT_TOCLIENT_FIRST
#define FLOW_PKT_TOCLIENT_FIRST
Definition: flow.h:240
DetectFirewallPolicies::pkt
struct DetectFirewallPolicy pkt[DETECT_FIREWALL_POLICY_SIZE]
Definition: detect.h:935
StreamReassembleRawHasDataReady
bool StreamReassembleRawHasDataReady(TcpSession *ssn, Packet *p)
does the stream engine have data to inspect?
Definition: stream-tcp-reassemble.c:1513
detect-engine-port.h
PktSrcToString
const char * PktSrcToString(enum PktSrcEnum pkt_src)
Definition: decode.c:941
DetectFirewallAppPolicy::progress
uint8_t progress
Definition: detect.h:925
DetectPort_
Port structure for detection engine.
Definition: detect.h:194
PacketAlerts_::discarded
uint16_t discarded
Definition: decode.h:307
app-layer-parser.h
Signature_::app_inspect
DetectEngineAppInspectionEngine * app_inspect
Definition: detect.h:724
ThreadVars_::id
int id
Definition: threadvars.h:85
AppLayerParserGetStateProgress
int AppLayerParserGetStateProgress(uint8_t ipproto, AppProto alproto, void *tx, uint8_t flags)
get the progress value for a tx/protocol
Definition: app-layer-parser.c:1199
util-detect.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:322
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:1387
SIG_FLAG_REQUIRE_FLOWVAR
#define SIG_FLAG_REQUIRE_FLOWVAR
Definition: detect.h:241
RuleMatchCandidateTx::stream_result
uint8_t stream_result
Definition: detect.h:1247
Signature_::action
uint8_t action
Definition: detect.h:678
util-profiling.h
DetectTransaction_::tx_id
const uint64_t tx_id
Definition: detect-engine-prefilter.h:33
DetectEngineLookupFlow_::udp
DetectPort * udp
Definition: detect.h:864
DetectEngineThreadCtx_::raw_stream_progress
uint64_t raw_stream_progress
Definition: detect.h:1316
SigIntId
#define SigIntId
Definition: detect-engine-state.h:38
FileUpdateFlowFileFlags
void FileUpdateFlowFileFlags(Flow *f, uint16_t set_file_flags, uint8_t direction)
set a flow's file flags
Definition: util-file.c:1091
SCReturn
#define SCReturn
Definition: util-debug.h:286
Signature_::flags
uint32_t flags
Definition: detect.h:664
RuleMatchCandidateTxArrayFree
void RuleMatchCandidateTxArrayFree(DetectEngineThreadCtx *det_ctx)
Definition: detect.c:1242
AppLayerGetTxIterState
Definition: app-layer-parser.h:148
ACTION_ALERT
#define ACTION_ALERT
Definition: action-globals.h:29
Packet_
Definition: decode.h:528
ByteVarSlot_::set
uint8_t set
Definition: detect.h:1288
detect-engine-build.h
ACTION_SCOPE_TX
@ ACTION_SCOPE_TX
Definition: action-globals.h:47
DetectEngineThreadCtx_::frame_id
int64_t frame_id
Definition: detect.h:1381
detect-engine-alert.h
AppLayerTxData::guessed_applayer_logged
uint8_t guessed_applayer_logged
Definition: app-layer-parser.h:202
DetectEngineThreadCtx_::filestore_cnt
uint16_t filestore_cnt
Definition: detect.h:1341
PROF_DETECT_IPONLY
@ PROF_DETECT_IPONLY
Definition: suricata-common.h:464
RULE_PROFILING_END
#define RULE_PROFILING_END(a, b, c, p)
Definition: util-profiling.h:422
TmEcode
TmEcode
Definition: tm-threads-common.h:80
RULE_PROFILING_START
#define RULE_PROFILING_START(p)
Definition: util-profiling.h:421
ALPROTO_DOH2
@ ALPROTO_DOH2
Definition: app-layer-protos.h:66
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:300
DetectPreFlow
uint8_t DetectPreFlow(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p)
Definition: detect.c:3252
DetectTransaction_::de_state
DetectEngineStateDirection * de_state
Definition: detect-engine-prefilter.h:35
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:1507
DetectTransaction_::tx_type
const uint8_t tx_type
Definition: detect-engine-prefilter.h:48
DetectFirewallAppTxState::last_fw_rule
bool last_fw_rule
Definition: detect.c:1783
detect-replace.h
AppLayerGetTxIterTuple
Definition: app-layer-parser.h:159
RulesDumpMatchArray
void RulesDumpMatchArray(const DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, const Packet *p)
Definition: detect-engine-profile.c:81
ALPROTO_HTTP2
@ ALPROTO_HTTP2
Definition: app-layer-protos.h:69
Signature_::addr_dst_match6_cnt
uint16_t addr_dst_match6_cnt
Definition: detect.h:690
AppLayerTxData::detect_progress_ts
uint8_t detect_progress_ts
Definition: app-layer-parser.h:210
FLOW_PKT_TOCLIENT
#define FLOW_PKT_TOCLIENT
Definition: flow.h:237
SigGroupHead_::frame_engines
PrefilterEngine * frame_engines
Definition: detect.h:1746
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:293
Signature_::sp
DetectPort * sp
Definition: detect.h:718
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1219
DetectRunScratchpad::sgh
const SigGroupHead * sgh
Definition: detect.c:75
PACKET_ALERT_FLAG_APPLY_ACTION_TO_PACKET
#define PACKET_ALERT_FLAG_APPLY_ACTION_TO_PACKET
Definition: decode.h:299
DETECT_FIREWALL_POLICY_PRE_STREAM
@ DETECT_FIREWALL_POLICY_PRE_STREAM
Definition: detect.h:912
PacketAlertFinalize
void PacketAlertFinalize(const 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:866
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
Definition: detect-engine-state.h:42
DETECT_ENGINE_STATE_FLAG_FILE_NEW
#define DETECT_ENGINE_STATE_FLAG_FILE_NEW
Definition: detect-engine-state.h:73
Flow_::flowvar
GenericVar * flowvar
Definition: flow.h:496
RuleMatchCandidateTx::flags
uint32_t * flags
Definition: detect.h:1243
PACKET_ALERT_FLAG_FRAME
#define PACKET_ALERT_FLAG_FRAME
Definition: decode.h:295
Frame::flags
uint8_t flags
Definition: app-layer-frames.h:51
DetectEngineAppInspectionEngine_::alproto
AppProto alproto
Definition: detect.h:392
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:156
DetectEngineStateDirection_::head
DeStateStore * head
Definition: detect-engine-state.h:86
DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
#define DETECT_ENGINE_INSPECT_SIG_MATCH_MORE_FILES
Definition: detect-engine-state.h:52
DetectEngineCtx_::eth_non_ip_sgh
struct SigGroupHead_ * eth_non_ip_sgh
Definition: detect.h:1058
APP_LAYER_MAX_PROGRESS
#define APP_LAYER_MAX_PROGRESS
Definition: app-layer-parser.h:81
DetectProto_::flags
uint8_t flags
Definition: detect-engine-proto.h:40
th_v
ThreadVars * th_v
Definition: fuzz_dataset.c:29
DETECT_PROTO_IPV4
#define DETECT_PROTO_IPV4
Definition: detect-engine-proto.h:31
app-layer-frames.h
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:1636
Packet_::tenant_id
uint32_t tenant_id
Definition: decode.h:698
Packet_::flow
struct Flow_ * flow
Definition: decode.h:584
DetectEngineStateResetTxs
void DetectEngineStateResetTxs(Flow *f)
Reset de state for active tx' To be used on detect engine reload.
Definition: detect-engine-state.c:258
PacketAlertDropReset
void PacketAlertDropReset(Packet *p)
Release the alert context the drop slot owns, then invalidate the slot. Not an alerts[] entry,...
Definition: decode.c:170
FLOWFILE_NO_MAGIC_TC
#define FLOWFILE_NO_MAGIC_TC
Definition: flow.h:140
flags
uint8_t flags
Definition: decode-gre.h:0
SCGenericVarFree
void SCGenericVarFree(GenericVar *gv)
Definition: util-var.c:48
suricata-common.h
SIG_FLAG_SP_ANY
#define SIG_FLAG_SP_ANY
Definition: detect.h:217
ActionScope
ActionScope
Definition: action-globals.h:42
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect-engine-register.h:316
DETECT_FIREWALL_POLICY_PACKET_FILTER
@ DETECT_FIREWALL_POLICY_PACKET_FILTER
Definition: detect.h:910
AppLayerTxData::updated_tc
bool updated_tc
Definition: app-layer-parser.h:179
ACTION_SCOPE_HOOK
@ ACTION_SCOPE_HOOK
Definition: action-globals.h:46
Signature_::action_scope
uint8_t action_scope
Definition: detect.h:685
packet.h
DeStateStore_
Definition: detect-engine-state.h:80
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
DetectTransaction_
Definition: detect-engine-prefilter.h:31
Packet_::app_update_direction
uint8_t app_update_direction
Definition: decode.h:567
PROF_DETECT_SETUP
@ PROF_DETECT_SETUP
Definition: suricata-common.h:462
DetectEngineStateDirection_
Definition: detect-engine-state.h:85
DetectEngineThreadCtx_::frame_inspect_progress
uint64_t frame_inspect_progress
Definition: detect.h:1382
DetectEngineCtx_::profile_match_logging_threshold
uint32_t profile_match_logging_threshold
Definition: detect.h:1090
FLOW_TS_APP_UPDATED
#define FLOW_TS_APP_UPDATED
Definition: flow.h:118
Signature_::proto
DetectProto * proto
Definition: detect.h:682
AppLayerParserGetTxData
AppLayerTxData * AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx)
Definition: app-layer-parser.c:1420
FatalError
#define FatalError(...)
Definition: util-debug.h:517
DetectFirewallAppTxState::fw_next_progress_missing
bool fw_next_progress_missing
Definition: detect.c:1782
AppLayerTxData::detect_progress_tc
uint8_t detect_progress_tc
Definition: app-layer-parser.h:211
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:304
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
DetectFirewallAppTxState
Definition: detect.c:1777
AlertQueueAppendPacket
void AlertQueueAppendPacket(DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, uint8_t alert_flags)
Append signature to local packet alert queue for later preprocessing.
Definition: detect-engine-alert.c:476
FLOW_ACTION_ACCEPT
#define FLOW_ACTION_ACCEPT
Definition: flow.h:63
util-validate.h
DetectTransaction_::tx_ptr
void * tx_ptr
Definition: detect-engine-prefilter.h:32
Signature_::addr_src_match6_cnt
uint16_t addr_src_match6_cnt
Definition: detect.h:691
StreamReassembleRawUpdateProgress
void StreamReassembleRawUpdateProgress(TcpSession *ssn, Packet *p, const uint64_t progress)
update stream engine after detection
Definition: stream-tcp-reassemble.c:1559
FLOWFILE_NO_SHA1_TS
#define FLOWFILE_NO_SHA1_TS
Definition: flow.h:151
PROF_DETECT_RULES
@ PROF_DETECT_RULES
Definition: suricata-common.h:465
APP_LAYER_TX_INSPECTED_TS
#define APP_LAYER_TX_INSPECTED_TS
Definition: app-layer-parser.h:51
DetectRunScratchpad::app_decoder_events
const bool app_decoder_events
Definition: detect.c:73
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:303
Signature_::dp
DetectPort * dp
Definition: detect.h:718
PacketAlerts_::suppressed
uint16_t suppressed
Definition: decode.h:309
PACKET_PROFILING_DETECT_START
#define PACKET_PROFILING_DETECT_START(p, id)
Definition: util-profiling.h:214
DetectEngineThreadCtx_::replace
const Signature ** replace
Definition: detect.h:1398
DetectFirewallPolicy
Definition: detect.h:917
PKT_PSEUDO_DETECTLOG_FLUSH
#define PKT_PSEUDO_DETECTLOG_FLUSH
Definition: decode.h:1362
DetectEngineThreadCtx_::byte_values_max
int32_t byte_values_max
Definition: detect.h:1409
Signature_::iid
SigIntId iid
Definition: detect.h:675
FLOW_SGH_TOSERVER
#define FLOW_SGH_TOSERVER
Definition: flow.h:72
APP_LAYER_TX_ACCEPT
#define APP_LAYER_TX_ACCEPT
Definition: app-layer-parser.h:54
SCFree
#define SCFree(p)
Definition: util-mem.h:61
Packet_::pkt_src
uint8_t pkt_src
Definition: decode.h:646
DetectEngineAppInspectionEngine_::sub_state
uint8_t sub_state
Definition: detect.h:402
SGH_PROFILING_RECORD
#define SGH_PROFILING_RECORD(det_ctx, sgh)
Definition: util-profiling.h:252
Signature_::addr_dst_match6
DetectMatchAddressIPv6 * addr_dst_match6
Definition: detect.h:709
Flow_::alstate
void * alstate
Definition: flow.h:486
Signature_::id
uint32_t id
Definition: detect.h:712
DeStateStore_::store
DeStateStoreItem store[DE_STATE_CHUNK_SIZE]
Definition: detect-engine-state.h:81
DetectEngineCtx_::guess_applayer_log_limit
uint8_t guess_applayer_log_limit
Definition: detect.h:1014
RuleMatchCandidateTx::stream_reset
uint32_t stream_reset
Definition: detect.h:1249
ACTION_SCOPE_PACKET
@ ACTION_SCOPE_PACKET
Definition: action-globals.h:44
detect-engine-iponly.h
DetectEngineThreadCtx_::fw_lte_cover
uint32_t fw_lte_cover[APP_LAYER_MAX_PROGRESS]
Definition: detect.h:1414
detect-parse.h
Signature_
Signature container.
Definition: detect.h:663
AppLayerGetTxIterTuple::has_next
bool has_next
Definition: app-layer-parser.h:162
PACKET_ALERT_FLAG_TX
#define PACKET_ALERT_FLAG_TX
Definition: decode.h:291
DetectFirewallAppTxState::fw_last_for_progress
bool fw_last_for_progress
Definition: detect.c:1781
AppLayerParserGetTxEndState
uint8_t AppLayerParserGetTxEndState(uint8_t ipproto, AppProto alproto, void *tx, uint8_t flags)
get the end state (progress) for a transaction.
Definition: app-layer-parser.c:1177
PKT_DROP_REASON_FW_DEFAULT_APP_POLICY
@ PKT_DROP_REASON_FW_DEFAULT_APP_POLICY
Definition: decode.h:428
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
TRACE_SID_TXS
#define TRACE_SID_TXS(sid, txs,...)
Definition: detect.c:1305
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:238
DetectEngineThreadCtx_::tx_id_set
bool tx_id_set
Definition: detect.h:1338
PacketDrop
void PacketDrop(Packet *p, const uint8_t action, enum PacketDropReason r)
issue drop action
Definition: packet.c:35
ACTION_ACCEPT
#define ACTION_ACCEPT
Definition: action-globals.h:36
STREAMTCP_FLAG_APP_LAYER_DISABLED
#define STREAMTCP_FLAG_APP_LAYER_DISABLED
Definition: stream-tcp-private.h:201
DetectRunScratchpad::flow_flags
const uint8_t flow_flags
Definition: detect.c:72
DetectEngineThreadCtx_::de_ctx
DetectEngineCtx * de_ctx
Definition: detect.h:1435
suricata.h
IPOnlyMatchPacket
void IPOnlyMatchPacket(ThreadVars *tv, const DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineIPOnlyCtx *io_ctx, Packet *p)
Match a packet against the IP Only detection engine contexts.
Definition: detect-engine-iponly.c:1000
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:985
Packet_::dst
Address dst
Definition: decode.h:533
DETECT_FIREWALL_POLICY_PRE_FLOW
@ DETECT_FIREWALL_POLICY_PRE_FLOW
Definition: detect.h:911
PROF_DETECT_TX
@ PROF_DETECT_TX
Definition: suricata-common.h:466
DetectEngineAppInspectionEngine_::dir
uint8_t dir
Definition: detect.h:393
Signature_::detect_table
uint8_t detect_table
Definition: detect.h:697
PKT_NOPACKET_INSPECTION
#define PKT_NOPACKET_INSPECTION
Definition: decode.h:1301
PrefilterPostRuleMatch
void PrefilterPostRuleMatch(DetectEngineThreadCtx *det_ctx, const SigGroupHead *sgh, Packet *p, Flow *f)
invoke post-rule match "prefilter" engines
Definition: detect-engine-prefilter.c:221
DetectGetInnerTx
void * DetectGetInnerTx(void *tx_ptr, AppProto alproto, AppProto engine_alproto, uint8_t flow_flags)
Definition: detect.c:1315
DetectPreStream
uint8_t DetectPreStream(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, Packet *p)
Definition: detect.c:3262
PKT_DROP_REASON_FW_DEFAULT_PACKET_POLICY
@ PKT_DROP_REASON_FW_DEFAULT_PACKET_POLICY
Definition: decode.h:427
DetectRunScratchpad::alproto
const AppProto alproto
Definition: detect.c:71
DE_STATE_FLAG_FULL_INSPECT
#define DE_STATE_FLAG_FULL_INSPECT
Definition: detect-engine-state.h:58
STREAMTCP_STREAM_FLAG_DISABLE_RAW
#define STREAMTCP_STREAM_FLAG_DISABLE_RAW
Definition: stream-tcp-private.h:238
DetectEngineThreadCtx_::json_content_len
uint8_t json_content_len
Definition: detect.h:1336
DetectEngineThreadCtx_::mt_det_ctxs_cnt
uint32_t mt_det_ctxs_cnt
Definition: detect.h:1305
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:232
likely
#define likely(expr)
Definition: util-optimize.h:32
IPPROTO_SCTP
#define IPPROTO_SCTP
Definition: decode.h:1282
DE_STATE_FLAG_FILE_INSPECT
#define DE_STATE_FLAG_FILE_INSPECT
Definition: detect-engine-state.h:63
DetectEngineThreadCtx_::counter_match_list
StatsCounterAvgId counter_match_list
Definition: detect.h:1353
AppLayerParserSupportsSubStates
bool AppLayerParserSupportsSubStates(const AppProto alproto)
Definition: app-layer-parser.c:1356
DetectFirewallAppPolicy::alproto
AppProto alproto
Definition: detect.h:923
DetectFirewallPolicies::app_policies
HashTable * app_policies
Definition: detect.h:939
DetectTransaction_::tx_progress
const uint8_t tx_progress
Definition: detect-engine-prefilter.h:43
DetectEngineCtx_::io_ctx
DetectEngineIPOnlyCtx io_ctx
Definition: detect.h:1008
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
Signature_::addr_dst_match4
DetectMatchAddressIPv4 * addr_dst_match4
Definition: detect.h:706
FlowGetDisruptionFlags
uint8_t FlowGetDisruptionFlags(const Flow *f, uint8_t flags)
get 'disruption' flags: GAP/DEPTH/PASS
Definition: flow.c:1168
TcpSession_
Definition: stream-tcp-private.h:290
DetectRunScratchpad::fw_pkt_policy
enum DetectFirewallPacketPolicies fw_pkt_policy
Definition: detect.c:74
flow.h
Signature_::addr_src_match4_cnt
uint16_t addr_src_match4_cnt
Definition: detect.h:689
DetectFirewallPolicy::action_scope
uint8_t action_scope
Definition: detect.h:919
DetectTxFirewallFlowControl
DetectTxFirewallFlowControl
Definition: detect.c:1223
DeStateStoreItem_
Definition: detect-engine-state.h:75
Signature_::addr_dst_match4_cnt
uint16_t addr_dst_match4_cnt
Definition: detect.h:688
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:457
Packet_::dp
Port dp
Definition: decode.h:543
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineCtx_::sig_array_len
uint32_t sig_array_len
Definition: detect.h:986
SIG_GROUP_HEAD_HAVEFILESHA256
#define SIG_GROUP_HEAD_HAVEFILESHA256
Definition: detect.h:1584
AlertQueueAppendAppTx
void AlertQueueAppendAppTx(DetectEngineThreadCtx *det_ctx, const Signature *s, Packet *p, uint64_t tx_id, uint8_t sub_state, uint8_t alert_flags)
Append signature to local packet alert queue for later preprocessing.
Definition: detect-engine-alert.c:454
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
Signature_::type
enum SignatureType type
Definition: detect.h:666
StatsCounterAddI64
void StatsCounterAddI64(StatsThreadContext *stats, StatsCounterId id, int64_t x)
Adds a value of type uint64_t to the local counter.
Definition: counters.c:144
SigGroupHead_::filestore_cnt
uint16_t filestore_cnt
Definition: detect.h:1739
AppLayerParserGetTxCnt
uint64_t AppLayerParserGetTxCnt(const Flow *f, void *alstate)
Definition: app-layer-parser.c:1212
DetectFirewallAppPolicy::direction
uint8_t direction
Definition: detect.h:926
AppLayerParserHasDecoderEvents
bool AppLayerParserHasDecoderEvents(AppLayerParserState *pstate)
Definition: app-layer-parser.c:1747
FLOW_PKT_TOSERVER_FIRST
#define FLOW_PKT_TOSERVER_FIRST
Definition: flow.h:239
DetectEngineThreadCtx_::tx_data
struct AppLayerTxData * tx_data
Definition: detect.h:1380
DetectEngineLookupFlow_::tcp
DetectPort * tcp
Definition: detect.h:863
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
detect-engine-address.h
Flow_::tenant_id
uint32_t tenant_id
Definition: flow.h:431
Packet_::src
Address src
Definition: decode.h:532
SigMatchSignaturesGetSgh
const SigGroupHead * SigMatchSignaturesGetSgh(const DetectEngineCtx *de_ctx, const Packet *p)
Get the SigGroupHead for a packet.
Definition: detect.c:293
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:95
RuleMatchCandidateTxArrayInit
void RuleMatchCandidateTxArrayInit(DetectEngineThreadCtx *det_ctx, uint32_t size)
Definition: detect.c:1229
APP_LAYER_TX_INSPECTED_TC
#define APP_LAYER_TX_INSPECTED_TC
Definition: app-layer-parser.h:52
SIG_FLAG_DP_ANY
#define SIG_FLAG_DP_ANY
Definition: detect.h:218
DetectPortLookupGroup
DetectPort * DetectPortLookupGroup(DetectPort *dp, uint16_t port)
Function that find the group matching port in a group head.
Definition: detect-engine-port.c:584
DetectEngineThreadCtx_::post_rule_work_queue
PostRuleMatchWorkQueue post_rule_work_queue
Definition: detect.h:1418
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1316
detect-engine-threshold.h
Signature_::mask
SignatureMask mask
Definition: detect.h:674
AppLayerTxData::updated_ts
bool updated_ts
Definition: app-layer-parser.h:180
app-layer.h
RuleMatchCandidateTx
Definition: detect.h:1239
DetectEngineThreadCtx_::TenantGetId
uint32_t(* TenantGetId)(const void *, const Packet *p)
Definition: detect.h:1312
DetectEngineAppInspectionEngine_::progress
uint8_t progress
Definition: detect.h:401
PrefilterRuleStore_::rule_id_array
SigIntId * rule_id_array
Definition: util-prefilter.h:38
PacketEngineEvents_::cnt
uint8_t cnt
Definition: decode.h:329
f
Flow f
Definition: fuzz_dataset.c:32
Flow_::de_ctx_version
uint32_t de_ctx_version
Definition: flow.h:471
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:114
DetectEngineThreadCtx_::match_array
Signature ** match_array
Definition: detect.h:1395