suricata
detect-engine.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 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 
24 #include "suricata-common.h"
25 #include "suricata.h"
26 #include "detect.h"
27 #include "flow.h"
28 #include "flow-private.h"
29 #include "flow-util.h"
30 #include "flow-worker.h"
31 #include "conf.h"
32 #include "conf-yaml-loader.h"
33 #include "datasets.h"
34 
35 #include "app-layer-parser.h"
36 #include "app-layer-events.h"
37 #include "app-layer-htp.h"
38 
39 #include "detect-parse.h"
40 #include "detect-engine-sigorder.h"
41 
42 #include "detect-engine-build.h"
43 #include "detect-engine-buffer.h"
44 #include "detect-engine-siggroup.h"
45 #include "detect-engine-address.h"
46 #include "detect-engine-port.h"
48 #include "detect-engine-mpm.h"
49 #include "detect-engine-iponly.h"
50 #include "detect-engine-tag.h"
51 #include "detect-engine-frame.h"
52 
53 #include "detect-engine-file.h"
54 
55 #include "detect-engine.h"
56 #include "detect-engine-state.h"
57 #include "detect-engine-payload.h"
58 #include "detect-fast-pattern.h"
59 #include "detect-byte-extract.h"
60 #include "detect-content.h"
61 #include "detect-uricontent.h"
62 #include "detect-tcphdr.h"
65 
66 #include "detect-engine-loader.h"
67 
68 #include "detect-engine-alert.h"
69 
71 #include "util-reference-config.h"
72 #include "util-threshold-config.h"
73 #include "util-error.h"
74 #include "util-hash.h"
75 #include "util-byte.h"
76 #include "util-debug.h"
77 #include "util-unittest.h"
78 #include "util-action.h"
79 #include "util-magic.h"
80 #include "util-signal.h"
81 #include "util-spm.h"
82 #include "util-device-private.h"
83 #include "util-var-name.h"
84 #include "util-path.h"
85 #include "util-profiling.h"
86 #include "util-validate.h"
87 #include "util-hash-string.h"
88 #include "util-enum.h"
89 #include "util-conf.h"
90 
91 #include "tm-threads.h"
92 #include "runmodes.h"
93 
94 #include "reputation.h"
95 
96 #define DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT 3000
97 
98 #define DEFAULT_MAX_FLOWBITS_PER_SIGNATURE 8
99 
100 static int DetectEngineCtxLoadConf(DetectEngineCtx *);
101 
102 static DetectEngineMasterCtx g_master_de_ctx = { SCMUTEX_INITIALIZER,
103  0, 99, NULL, NULL, TENANT_SELECTOR_UNKNOWN, NULL, NULL, 0};
104 
105 static uint32_t TenantIdHash(HashTable *h, void *data, uint16_t data_len);
106 static char TenantIdCompare(void *d1, uint16_t d1_len, void *d2, uint16_t d2_len);
107 static void TenantIdFree(void *d);
108 static uint32_t DetectEngineTenantGetIdFromLivedev(const void *ctx, const Packet *p);
109 static uint32_t DetectEngineTenantGetIdFromVlanId(const void *ctx, const Packet *p);
110 static uint32_t DetectEngineTenantGetIdFromPcap(const void *ctx, const Packet *p);
111 
112 static bool DetectEngineMultiTenantEnabledWithLock(void);
113 static DetectEngineAppInspectionEngine *g_app_inspect_engines = NULL;
114 static DetectEnginePktInspectionEngine *g_pkt_inspect_engines = NULL;
115 static DetectEngineFrameInspectionEngine *g_frame_inspect_engines = NULL;
116 
117 // clang-format off
118 // rule types documentation tag start: SignatureProperties
120  /* SIG_TYPE_NOT_SET */ { SIG_PROP_FLOW_ACTION_PACKET, },
121  /* SIG_TYPE_IPONLY */ { SIG_PROP_FLOW_ACTION_FLOW, },
122  /* SIG_TYPE_LIKE_IPONLY */ { SIG_PROP_FLOW_ACTION_FLOW, },
123  /* SIG_TYPE_PDONLY */ { SIG_PROP_FLOW_ACTION_FLOW, },
124  /* SIG_TYPE_DEONLY */ { SIG_PROP_FLOW_ACTION_PACKET, },
125  /* SIG_TYPE_PKT */ { SIG_PROP_FLOW_ACTION_PACKET, },
126  /* SIG_TYPE_PKT_STREAM */ { SIG_PROP_FLOW_ACTION_FLOW_IF_STATEFUL, },
127  /* SIG_TYPE_STREAM */ { SIG_PROP_FLOW_ACTION_FLOW_IF_STATEFUL, },
128  /* SIG_TYPE_APPLAYER */ { SIG_PROP_FLOW_ACTION_FLOW, },
129  /* SIG_TYPE_APP_TX */ { SIG_PROP_FLOW_ACTION_FLOW, },
130 };
131 // rule types documentation tag end: SignatureProperties
132 // clang-format on
133 
134 const char *DetectTableToString(enum DetectTable table)
135 {
136  switch (table) {
138  return "not_set";
140  return "pre_flow";
142  return "pre_stream";
144  return "packet_filter";
146  return "packet_td";
148  return "app_filter";
149  case DETECT_TABLE_APP_TD:
150  return "app_td";
151  default:
152  return "unknown";
153  }
154 }
155 
156 /** \brief register inspect engine at start up time
157  *
158  * \note errors are fatal */
162 {
164  const int sm_list = DetectBufferTypeGetByName(name);
165  if (sm_list == -1) {
166  FatalError("failed to register inspect engine %s", name);
167  }
168 
169  if ((sm_list < DETECT_SM_LIST_MATCH) || (sm_list >= SHRT_MAX) ||
170  (Callback == NULL))
171  {
172  SCLogError("Invalid arguments");
173  BUG_ON(1);
174  }
175 
176  DetectEnginePktInspectionEngine *new_engine = SCCalloc(1, sizeof(*new_engine));
177  if (unlikely(new_engine == NULL)) {
178  FatalError("failed to register inspect engine %s: %s", name, strerror(errno));
179  }
180  new_engine->sm_list = (uint16_t)sm_list;
181  new_engine->sm_list_base = (uint16_t)sm_list;
182  new_engine->v1.Callback = Callback;
183  new_engine->v1.GetData = GetPktData;
184 
185  if (g_pkt_inspect_engines == NULL) {
186  g_pkt_inspect_engines = new_engine;
187  } else {
188  DetectEnginePktInspectionEngine *t = g_pkt_inspect_engines;
189  while (t->next != NULL) {
190  t = t->next;
191  }
192 
193  t->next = new_engine;
194  }
195 }
196 
197 /** \brief register inspect engine at start up time
198  *
199  * \note errors are fatal */
200 static void AppLayerInspectEngineRegisterInternal(const char *name, AppProto alproto, uint32_t dir,
201  uint8_t sub_state, uint8_t progress, InspectEngineFuncPtr Callback,
203  InspectionMultiBufferGetDataPtr GetMultiData)
204 {
205  /* ignore special case unknown */
206  if (alproto != ALPROTO_UNKNOWN && AppLayerParserIsEnabled(alproto)) {
207  DEBUG_VALIDATE_BUG_ON(AppLayerParserSupportsSubStates(alproto) && sub_state == 0);
208  DEBUG_VALIDATE_BUG_ON(!AppLayerParserSupportsSubStates(alproto) && sub_state != 0);
209  }
210  BUG_ON(progress >= APP_LAYER_MAX_PROGRESS);
211 
213  const int sm_list = DetectBufferTypeGetByName(name);
214  if (sm_list == -1) {
215  FatalError("failed to register inspect engine %s", name);
216  }
217  SCLogDebug("name %s id %d", name, sm_list);
218 
219  if ((alproto == ALPROTO_FAILED) || (!(dir == SIG_FLAG_TOSERVER || dir == SIG_FLAG_TOCLIENT)) ||
220  (sm_list < DETECT_SM_LIST_MATCH) || (sm_list >= SHRT_MAX) || (progress >= 48) ||
221  (Callback == NULL)) {
222  SCLogError("Invalid arguments");
223  BUG_ON(1);
224  } else if (Callback == DetectEngineInspectBufferGeneric && GetData == NULL) {
225  SCLogError("Invalid arguments: must register "
226  "GetData with DetectEngineInspectBufferGeneric");
227  BUG_ON(1);
228  } else if (Callback == DetectEngineInspectBufferSingle && GetDataSingle == NULL) {
229  SCLogError("Invalid arguments: must register "
230  "GetData with DetectEngineInspectBufferGeneric");
231  BUG_ON(1);
232  } else if (Callback == DetectEngineInspectMultiBufferGeneric && GetMultiData == NULL) {
233  SCLogError("Invalid arguments: must register "
234  "GetData with DetectEngineInspectMultiBufferGeneric");
235  BUG_ON(1);
236  }
237 
238  uint8_t direction;
239  if (dir == SIG_FLAG_TOSERVER) {
240  direction = 0;
241  } else {
242  direction = 1;
243  }
244  DetectEngineAppInspectionEngine *new_engine =
246  if (unlikely(new_engine == NULL)) {
247  exit(EXIT_FAILURE);
248  }
249  new_engine->alproto = alproto;
250  new_engine->dir = direction;
251  new_engine->sm_list = (uint16_t)sm_list;
252  new_engine->sm_list_base = (uint16_t)sm_list;
253  new_engine->progress = progress;
254  new_engine->sub_state = sub_state;
255  new_engine->v2.Callback = Callback;
256  if (Callback == DetectEngineInspectBufferGeneric) {
257  new_engine->v2.GetData = GetData;
258  } else if (Callback == DetectEngineInspectBufferSingle) {
259  new_engine->v2.GetDataSingle = GetDataSingle;
260  } else if (Callback == DetectEngineInspectMultiBufferGeneric) {
261  new_engine->v2.GetMultiData = GetMultiData;
262  }
263 
264  if (g_app_inspect_engines == NULL) {
265  g_app_inspect_engines = new_engine;
266  } else {
267  DetectEngineAppInspectionEngine *t = g_app_inspect_engines;
268  while (t->next != NULL) {
269  t = t->next;
270  }
271 
272  t->next = new_engine;
273  }
274 }
275 
276 void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir,
277  uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
278 {
279  /* before adding, check that we don't add a duplicate entry, which will
280  * propagate all the way into the packet runtime if allowed. */
281  DetectEngineAppInspectionEngine *t = g_app_inspect_engines;
282  while (t != NULL) {
283  const uint32_t t_direction = t->dir == 0 ? SIG_FLAG_TOSERVER : SIG_FLAG_TOCLIENT;
284  const int sm_list = DetectBufferTypeGetByName(name);
285 
286  if (t->sm_list == sm_list && t->alproto == alproto && t_direction == dir &&
287  t->sub_state == 0 && t->progress == progress && t->v2.Callback == Callback &&
288  t->v2.GetData == GetData) {
290  return;
291  }
292  t = t->next;
293  }
294 
295  AppLayerInspectEngineRegisterInternal(
296  name, alproto, dir, 0, (uint8_t)progress, Callback, GetData, NULL, NULL);
297 }
298 
299 void DetectAppLayerInspectEngineRegisterSubState(const char *name, AppProto alproto, uint32_t dir,
300  uint8_t sub_state, uint8_t progress, InspectEngineFuncPtr Callback,
302 {
303  /* before adding, check that we don't add a duplicate entry, which will
304  * propagate all the way into the packet runtime if allowed. */
305  DetectEngineAppInspectionEngine *t = g_app_inspect_engines;
306  while (t != NULL) {
307  const uint32_t t_direction = t->dir == 0 ? SIG_FLAG_TOSERVER : SIG_FLAG_TOCLIENT;
308  const int sm_list = DetectBufferTypeGetByName(name);
309 
310  if (t->sm_list == sm_list && t->alproto == alproto && t_direction == dir &&
311  t->sub_state == sub_state && t->progress == progress &&
312  t->v2.Callback == Callback && t->v2.GetData == GetData) {
314  return;
315  }
316  t = t->next;
317  }
318 
319  AppLayerInspectEngineRegisterInternal(
320  name, alproto, dir, sub_state, progress, Callback, GetData, NULL, NULL);
321 }
322 void DetectAppLayerInspectEngineRegisterSingle(const char *name, AppProto alproto, uint32_t dir,
323  uint8_t progress, InspectEngineFuncPtr Callback, InspectionSingleBufferGetDataPtr GetData)
324 {
325  /* before adding, check that we don't add a duplicate entry, which will
326  * propagate all the way into the packet runtime if allowed. */
327  DetectEngineAppInspectionEngine *t = g_app_inspect_engines;
328  while (t != NULL) {
329  const uint32_t t_direction = t->dir == 0 ? SIG_FLAG_TOSERVER : SIG_FLAG_TOCLIENT;
330  const int sm_list = DetectBufferTypeGetByName(name);
331 
332  if (t->sm_list == sm_list && t->alproto == alproto && t_direction == dir &&
333  t->progress == progress && t->v2.Callback == Callback &&
334  t->v2.GetDataSingle == GetData) {
336  return;
337  }
338  t = t->next;
339  }
340 
341  AppLayerInspectEngineRegisterInternal(
342  name, alproto, dir, 0, (uint8_t)progress, Callback, NULL, GetData, NULL);
343 }
344 
345 /* copy an inspect engine with transforms to a new list id. */
346 static void DetectAppLayerInspectEngineCopy(
348  int sm_list, int new_list,
349  const DetectEngineTransforms *transforms)
350 {
351  const DetectEngineAppInspectionEngine *t = g_app_inspect_engines;
352  while (t) {
353  if (t->sm_list == sm_list) {
355  if (unlikely(new_engine == NULL)) {
356  exit(EXIT_FAILURE);
357  }
358  new_engine->alproto = t->alproto;
359  new_engine->dir = t->dir;
360  DEBUG_VALIDATE_BUG_ON(new_list < 0 || new_list > UINT16_MAX);
361  new_engine->sm_list = (uint16_t)new_list; /* use new list id */
362  DEBUG_VALIDATE_BUG_ON(sm_list < 0 || sm_list > UINT16_MAX);
363  new_engine->sm_list_base = (uint16_t)sm_list;
364  new_engine->progress = t->progress;
365  new_engine->sub_state = t->sub_state;
366  new_engine->v2 = t->v2;
367  new_engine->v2.transforms = transforms; /* assign transforms */
368 
369  if (de_ctx->app_inspect_engines == NULL) {
370  de_ctx->app_inspect_engines = new_engine;
371  } else {
373  while (list->next != NULL) {
374  list = list->next;
375  }
376 
377  list->next = new_engine;
378  }
379  }
380  t = t->next;
381  }
382 }
383 
384 /* copy inspect engines from global registrations to de_ctx list */
385 static void DetectAppLayerInspectEngineCopyListToDetectCtx(DetectEngineCtx *de_ctx)
386 {
387  const DetectEngineAppInspectionEngine *t = g_app_inspect_engines;
389  while (t) {
391  if (unlikely(new_engine == NULL)) {
392  exit(EXIT_FAILURE);
393  }
394  new_engine->alproto = t->alproto;
395  new_engine->dir = t->dir;
396  new_engine->sm_list = t->sm_list;
397  new_engine->sm_list_base = t->sm_list;
398  new_engine->progress = t->progress;
399  new_engine->sub_state = t->sub_state;
400  new_engine->v2 = t->v2;
401 
402  if (list == NULL) {
403  de_ctx->app_inspect_engines = new_engine;
404  } else {
405  list->next = new_engine;
406  }
407  list = new_engine;
408 
409  t = t->next;
410  }
411 }
412 
413 /* copy an inspect engine with transforms to a new list id. */
414 static void DetectPktInspectEngineCopy(
416  int sm_list, int new_list,
417  const DetectEngineTransforms *transforms)
418 {
419  const DetectEnginePktInspectionEngine *t = g_pkt_inspect_engines;
420  while (t) {
421  if (t->sm_list == sm_list) {
423  if (unlikely(new_engine == NULL)) {
424  exit(EXIT_FAILURE);
425  }
426  DEBUG_VALIDATE_BUG_ON(new_list < 0 || new_list > UINT16_MAX);
427  new_engine->sm_list = (uint16_t)new_list; /* use new list id */
428  DEBUG_VALIDATE_BUG_ON(sm_list < 0 || sm_list > UINT16_MAX);
429  new_engine->sm_list_base = (uint16_t)sm_list;
430  new_engine->v1 = t->v1;
431  new_engine->v1.transforms = transforms; /* assign transforms */
432 
433  if (de_ctx->pkt_inspect_engines == NULL) {
434  de_ctx->pkt_inspect_engines = new_engine;
435  } else {
437  while (list->next != NULL) {
438  list = list->next;
439  }
440 
441  list->next = new_engine;
442  }
443  }
444  t = t->next;
445  }
446 }
447 
448 /* copy inspect engines from global registrations to de_ctx list */
449 static void DetectPktInspectEngineCopyListToDetectCtx(DetectEngineCtx *de_ctx)
450 {
451  const DetectEnginePktInspectionEngine *t = g_pkt_inspect_engines;
452  while (t) {
453  SCLogDebug("engine %p", t);
455  if (unlikely(new_engine == NULL)) {
456  exit(EXIT_FAILURE);
457  }
458  new_engine->sm_list = t->sm_list;
459  new_engine->sm_list_base = t->sm_list;
460  new_engine->v1 = t->v1;
461 
462  if (de_ctx->pkt_inspect_engines == NULL) {
463  de_ctx->pkt_inspect_engines = new_engine;
464  } else {
466  while (list->next != NULL) {
467  list = list->next;
468  }
469 
470  list->next = new_engine;
471  }
472 
473  t = t->next;
474  }
475 }
476 
477 /** \brief register inspect engine at start up time
478  *
479  * \note errors are fatal */
481  InspectionBufferFrameInspectFunc Callback, AppProto alproto, uint8_t type)
482 {
483  const int sm_list = DetectEngineBufferTypeRegister(de_ctx, name);
484  if (sm_list < 0) {
485  FatalError("failed to register inspect engine %s", name);
486  }
487 
488  if ((sm_list < DETECT_SM_LIST_MATCH) || (sm_list >= SHRT_MAX) || (Callback == NULL)) {
489  SCLogError("Invalid arguments");
490  BUG_ON(1);
491  }
492 
493  uint8_t direction;
494  if (dir == SIG_FLAG_TOSERVER) {
495  direction = 0;
496  } else {
497  direction = 1;
498  }
499 
500  DetectEngineFrameInspectionEngine *new_engine = SCCalloc(1, sizeof(*new_engine));
501  if (unlikely(new_engine == NULL)) {
502  FatalError("failed to register inspect engine %s: %s", name, strerror(errno));
503  }
504  new_engine->sm_list = (uint16_t)sm_list;
505  new_engine->sm_list_base = (uint16_t)sm_list;
506  new_engine->dir = direction;
507  new_engine->v1.Callback = Callback;
508  new_engine->alproto = alproto;
509  new_engine->type = type;
510 
511  if (de_ctx->frame_inspect_engines == NULL) {
512  de_ctx->frame_inspect_engines = new_engine;
513  } else {
515  while (list->next != NULL) {
516  list = list->next;
517  }
518 
519  list->next = new_engine;
520  }
521 }
522 
523 /* copy an inspect engine with transforms to a new list id. */
524 static void DetectFrameInspectEngineCopy(DetectEngineCtx *de_ctx, int sm_list, int new_list,
525  const DetectEngineTransforms *transforms)
526 {
527  /* take the list from the detect engine as the buffers can be registered
528  * dynamically. */
530  while (t) {
531  if (t->sm_list == sm_list) {
534  if (unlikely(new_engine == NULL)) {
535  exit(EXIT_FAILURE);
536  }
537  DEBUG_VALIDATE_BUG_ON(new_list < 0 || new_list > UINT16_MAX);
538  new_engine->sm_list = (uint16_t)new_list; /* use new list id */
539  DEBUG_VALIDATE_BUG_ON(sm_list < 0 || sm_list > UINT16_MAX);
540  new_engine->sm_list_base = (uint16_t)sm_list;
541  new_engine->dir = t->dir;
542  new_engine->alproto = t->alproto;
543  new_engine->type = t->type;
544  new_engine->v1 = t->v1;
545  new_engine->v1.transforms = transforms; /* assign transforms */
546 
547  /* append to the list */
549  while (list->next != NULL) {
550  list = list->next;
551  }
552 
553  list->next = new_engine;
554  }
555  t = t->next;
556  }
557 }
558 
559 /* copy inspect engines from global registrations to de_ctx list */
560 static void DetectFrameInspectEngineCopyListToDetectCtx(DetectEngineCtx *de_ctx)
561 {
562  const DetectEngineFrameInspectionEngine *t = g_frame_inspect_engines;
563  while (t) {
564  SCLogDebug("engine %p", t);
567  if (unlikely(new_engine == NULL)) {
568  exit(EXIT_FAILURE);
569  }
570  new_engine->sm_list = t->sm_list;
571  new_engine->sm_list_base = t->sm_list;
572  new_engine->dir = t->dir;
573  new_engine->alproto = t->alproto;
574  new_engine->type = t->type;
575  new_engine->v1 = t->v1;
576 
577  if (de_ctx->frame_inspect_engines == NULL) {
578  de_ctx->frame_inspect_engines = new_engine;
579  } else {
581  while (list->next != NULL) {
582  list = list->next;
583  }
584 
585  list->next = new_engine;
586  }
587 
588  t = t->next;
589  }
590 }
591 
592 /** \internal
593  * \brief append the stream inspection
594  *
595  * If stream inspection is MPM, then prepend it.
596  */
597 static void AppendStreamInspectEngine(
598  Signature *s, SigMatchData *stream, uint8_t direction, uint8_t id)
599 {
600  bool prepend = false;
601 
603  if (unlikely(new_engine == NULL)) {
604  exit(EXIT_FAILURE);
605  }
607  SCLogDebug("stream is mpm");
608  prepend = true;
609  new_engine->mpm = true;
610  }
611  new_engine->alproto = ALPROTO_UNKNOWN; /* all */
612  new_engine->dir = direction;
613  new_engine->stream = true;
614  new_engine->sm_list = DETECT_SM_LIST_PMATCH;
615  new_engine->sm_list_base = DETECT_SM_LIST_PMATCH;
616  new_engine->smd = stream;
617  new_engine->v2.Callback = DetectEngineInspectStream;
618  new_engine->progress = 0;
619 
620  /* append */
621  if (s->app_inspect == NULL) {
622  s->app_inspect = new_engine;
623  new_engine->id = DE_STATE_FLAG_BASE; /* id is used as flag in stateful detect */
624  } else if (prepend) {
625  new_engine->next = s->app_inspect;
626  s->app_inspect = new_engine;
627  new_engine->id = id;
628 
629  } else {
631  while (a->next != NULL) {
632  a = a->next;
633  }
634 
635  a->next = new_engine;
636  new_engine->id = id;
637  }
638  SCLogDebug("sid %u: engine %p/%u added", s->id, new_engine, new_engine->id);
639 }
640 
641 static void AppendFrameInspectEngine(DetectEngineCtx *de_ctx,
643  const int mpm_list)
644 {
645  bool prepend = false;
646 
647  if (u->alproto == ALPROTO_UNKNOWN) {
648  /* special case, inspect engine applies to all protocols */
649  } else if (s->alproto != ALPROTO_UNKNOWN && !AppProtoEquals(s->alproto, u->alproto))
650  return;
651 
652  if (s->flags & SIG_FLAG_TOSERVER && !(s->flags & SIG_FLAG_TOCLIENT)) {
653  if (u->dir == 1)
654  return;
655  } else if (s->flags & SIG_FLAG_TOCLIENT && !(s->flags & SIG_FLAG_TOSERVER)) {
656  if (u->dir == 0)
657  return;
658  }
659 
662  if (unlikely(new_engine == NULL)) {
663  exit(EXIT_FAILURE);
664  }
665  if (mpm_list == u->sm_list) {
667  prepend = true;
668  new_engine->mpm = true;
669  }
670 
671  new_engine->type = u->type;
672  new_engine->sm_list = u->sm_list;
673  new_engine->sm_list_base = u->sm_list_base;
674  new_engine->smd = smd;
675  new_engine->v1 = u->v1;
676  SCLogDebug("sm_list %d new_engine->v1 %p/%p", new_engine->sm_list, new_engine->v1.Callback,
677  new_engine->v1.transforms);
678 
679  if (s->frame_inspect == NULL) {
680  s->frame_inspect = new_engine;
681  } else if (prepend) {
682  new_engine->next = s->frame_inspect;
683  s->frame_inspect = new_engine;
684  } else {
686  while (a->next != NULL) {
687  a = a->next;
688  }
689  new_engine->next = a->next;
690  a->next = new_engine;
691  }
692 }
693 
694 static void AppendPacketInspectEngine(DetectEngineCtx *de_ctx,
696  const int mpm_list)
697 {
698  bool prepend = false;
699 
700  DetectEnginePktInspectionEngine *new_engine =
702  if (unlikely(new_engine == NULL)) {
703  exit(EXIT_FAILURE);
704  }
705  if (mpm_list == e->sm_list) {
707  prepend = true;
708  new_engine->mpm = true;
709  }
710 
711  new_engine->sm_list = e->sm_list;
712  new_engine->sm_list_base = e->sm_list_base;
713  new_engine->smd = smd;
714  new_engine->v1 = e->v1;
715  SCLogDebug("sm_list %d new_engine->v1 %p/%p/%p", new_engine->sm_list, new_engine->v1.Callback,
716  new_engine->v1.GetData, new_engine->v1.transforms);
717 
718  if (s->pkt_inspect == NULL) {
719  s->pkt_inspect = new_engine;
720  } else if (prepend) {
721  new_engine->next = s->pkt_inspect;
722  s->pkt_inspect = new_engine;
723  } else {
725  while (a->next != NULL) {
726  a = a->next;
727  }
728  new_engine->next = a->next;
729  a->next = new_engine;
730  }
731 }
732 
733 static void AppendAppInspectEngine(DetectEngineCtx *de_ctx,
735  const int mpm_list, const int files_id, uint8_t *last_id, bool *head_is_mpm)
736 {
737  if (t->alproto == ALPROTO_UNKNOWN) {
738  /* special case, inspect engine applies to all protocols */
739  } else if (s->alproto != ALPROTO_UNKNOWN) {
741  /* SIGNATURE_HOOK_TYPE_APP rules are exact about their protocol */
742  if (!(AppProtoEqualsStrict(s->alproto, t->alproto))) {
743  return;
744  }
745 
746  /* skip engines not for us */
747  if (s->init_data->hook.t.app.sub_state != t->sub_state) {
748  return;
749  }
750  } else {
751  /* other rules use the more relax AppProtoEquals logic */
752  if (!AppProtoEquals(s->alproto, t->alproto)) {
753  return;
754  }
755  }
756  }
757 
758  if (s->flags & SIG_FLAG_TOSERVER && !(s->flags & SIG_FLAG_TOCLIENT)) {
759  if (t->dir == 1)
760  return;
761  } else if (s->flags & SIG_FLAG_TOCLIENT && !(s->flags & SIG_FLAG_TOSERVER)) {
762  if (t->dir == 0)
763  return;
764  }
765  SCLogDebug("app engine: t %p t->id %u => alproto:%s files:%s", t, t->id,
766  AppProtoToString(t->alproto), BOOL2STR(t->sm_list == files_id));
767 
768  DetectEngineAppInspectionEngine *new_engine =
770  if (unlikely(new_engine == NULL)) {
771  exit(EXIT_FAILURE);
772  }
773  bool prepend = false;
774  if (mpm_list == t->sm_list) {
776  prepend = true;
777  *head_is_mpm = true;
778  new_engine->mpm = true;
779  }
780 
781  new_engine->alproto = t->alproto;
782  new_engine->dir = t->dir;
783  new_engine->sm_list = t->sm_list;
784  new_engine->sm_list_base = t->sm_list_base;
785  new_engine->smd = smd;
786  new_engine->match_on_null = smd ? DetectContentInspectionMatchOnAbsentBuffer(smd) : false;
787  new_engine->progress = t->progress;
788  new_engine->sub_state = t->sub_state;
789  new_engine->v2 = t->v2;
790  SCLogDebug("sm_list %d new_engine->v2 %p/%p/%p", new_engine->sm_list, new_engine->v2.Callback,
791  new_engine->v2.GetData, new_engine->v2.transforms);
792 
793  if (s->app_inspect == NULL) {
794  s->app_inspect = new_engine;
795  if (new_engine->sm_list == files_id) {
796  new_engine->id = DE_STATE_ID_FILE_INSPECT;
797  SCLogDebug("sid %u: engine %p/%u is FILE ENGINE", s->id, new_engine, new_engine->id);
798  } else {
799  new_engine->id = DE_STATE_FLAG_BASE; /* id is used as flag in stateful detect */
800  SCLogDebug("sid %u: engine %p/%u %s", s->id, new_engine, new_engine->id,
802  }
803 
804  /* prepend engine if forced or if our engine has a lower progress. */
805  } else if (prepend || (!(*head_is_mpm) && s->app_inspect->progress > new_engine->progress)) {
806  new_engine->next = s->app_inspect;
807  s->app_inspect = new_engine;
808  if (new_engine->sm_list == files_id) {
809  new_engine->id = DE_STATE_ID_FILE_INSPECT;
810  SCLogDebug("sid %u: engine %p/%u is FILE ENGINE", s->id, new_engine, new_engine->id);
811  } else {
812  new_engine->id = ++(*last_id);
813  SCLogDebug("sid %u: engine %p/%u %s", s->id, new_engine, new_engine->id,
815  }
816 
817  } else {
819  while (a->next != NULL) {
820  if (a->next && a->next->progress > new_engine->progress) {
821  break;
822  }
823  a = a->next;
824  }
825 
826  new_engine->next = a->next;
827  a->next = new_engine;
828  if (new_engine->sm_list == files_id) {
829  new_engine->id = DE_STATE_ID_FILE_INSPECT;
830  SCLogDebug("sid %u: engine %p/%u is FILE ENGINE", s->id, new_engine, new_engine->id);
831  } else {
832  new_engine->id = ++(*last_id);
833  SCLogDebug("sid %u: engine %p/%u %s", s->id, new_engine, new_engine->id,
835  }
836  }
837 
838  SCLogDebug("sid %u: engine %p/%u added", s->id, new_engine, new_engine->id);
839 
841 }
842 
843 /**
844  * \param direction STREAM_TOSERVER or STREAM_TOCLIENT
845  */
847  const AppProto p, const uint8_t sub_state, const uint8_t state, const uint8_t direction)
848 {
849  if (!((direction & (STREAM_TOSERVER | STREAM_TOCLIENT)) == STREAM_TOSERVER) &&
850  !((direction & (STREAM_TOSERVER | STREAM_TOCLIENT)) == STREAM_TOCLIENT))
851  return NULL;
852 
853  if (sub_state == 0) {
854  const char *pname = AppLayerParserGetStateNameById(IPPROTO_TCP, // TODO
855  p, state, direction);
856  if (pname == NULL) {
857  if (state == 0) {
858  if (direction == STREAM_TOSERVER) {
859  pname = "request_started";
860  } else {
861  pname = "response_started";
862  }
863  } else {
864  const int complete = AppLayerParserGetStateProgressCompletionStatus(p, direction);
865  if (state == complete) {
866  if (direction == STREAM_TOSERVER) {
867  pname = "request_complete";
868  } else {
869  pname = "response_complete";
870  }
871  }
872  }
873  }
874  return pname;
875  } else {
877  const char *name = AppLayerParserGetSubStateProgressName(p, sub_state, state, direction);
878  return name;
879  }
880 }
881 
882 /** \brief get the sm_list for a app hook
883  * \param sub_state sub_state to use or 0 if not in use
884  * */
886  const AppProto p, const uint8_t sub_state, const uint8_t state, const uint8_t direction)
887 {
888  const char *app_proto = AppProtoToStringRaw(p);
889  if (app_proto == NULL) {
890  SCLogError("unknown app_proto %u", p);
891  return -1;
892  }
893 
894  char generic_hook_name[256];
895  if (sub_state == 0) {
896  const char *name = DetectEngineAppHookToName(
897  p, 0, state, direction & (STREAM_TOSERVER | STREAM_TOCLIENT));
898  if (name == NULL) {
899  return -1;
900  }
901 
902  snprintf(generic_hook_name, sizeof(generic_hook_name), "%s:%s:generic", app_proto, name);
903 
904  int list = DetectBufferTypeGetByName(generic_hook_name);
905  if (list < 0) {
906  SCLogError(
907  "no list registered as %s for %s hook %s", generic_hook_name, app_proto, name);
908  return -1;
909  }
910  return list;
911  } else {
913 
914  const char *sname = AppLayerParserGetSubStateName(p, sub_state);
915  if (sname == NULL)
916  return -1;
917 
918  const char *name = AppLayerParserGetSubStateProgressName(p, sub_state, state, direction);
919  if (name == NULL)
920  return -1;
921 
922  snprintf(generic_hook_name, sizeof(generic_hook_name), "%s:%s:%s:generic", app_proto, sname,
923  name);
924 
925  int list = DetectBufferTypeGetByName(generic_hook_name);
926  if (list < 0) {
927  SCLogError("no list registered as %s for %s sub_state %s hook %s", generic_hook_name,
928  app_proto, sname, name);
929  return -1;
930  }
931  return list;
932  }
933 }
934 
935 /**
936  * \note for the file inspect engine, the id DE_STATE_ID_FILE_INSPECT
937  * is assigned.
938  */
940 {
941  const int mpm_list = s->init_data->mpm_sm ? s->init_data->mpm_sm_list : -1;
942  const int files_id = DetectBufferTypeGetByName("files");
943  bool head_is_mpm = false;
944  uint8_t last_id = DE_STATE_FLAG_BASE;
945  SCLogDebug("%u: setup app inspect engines. %u buffers", s->id, s->init_data->buffer_index);
946 
947  if (s->flags & SIG_FLAG_FW_HOOK_LTE) {
948  SCLogDebug("need an inspect engine per state, range 0-%u", s->app_progress_hook);
949  for (uint8_t state = 0; state < s->app_progress_hook; state++) {
950  uint8_t dir = 0;
951  uint8_t direction = 0;
955  if (s->flags & SIG_FLAG_TOSERVER) {
956  direction = STREAM_TOSERVER;
957  dir = 0;
958  } else if (s->flags & SIG_FLAG_TOCLIENT) {
959  direction = STREAM_TOCLIENT;
960  dir = 1;
961  }
962 
963  int sm_list = DetectEngineAppHookToSmlist(s->init_data->hook.t.app.alproto,
964  s->init_data->hook.t.app.sub_state, 0, direction);
965  if (sm_list < 0)
966  return -1;
967 
969  .alproto = s->init_data->hook.t.app.alproto,
970  .progress = state,
971  .sub_state = s->init_data->hook.t.app.sub_state,
972  .sm_list = (uint16_t)sm_list,
973  .sm_list_base = (uint16_t)sm_list,
974  .dir = dir,
975  };
976  AppendAppInspectEngine(de_ctx, &t, s, NULL, mpm_list, files_id, &last_id, &head_is_mpm);
977  SCLogDebug("sid %u: appended pass-tru engine at hook:%u sm_list:%d for "
978  "SIG_FLAG_INIT_HOOK_LTE",
979  s->id, state, sm_list);
980  }
981  }
982 
983  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
985  SCLogDebug("smd %p, id %u", smd, s->init_data->buffers[x].id);
986 
987  const DetectBufferType *b =
989  if (b == NULL)
990  FatalError("unknown buffer");
991 
992  if (b->frame) {
994  u != NULL; u = u->next) {
995  if (u->sm_list == s->init_data->buffers[x].id) {
996  AppendFrameInspectEngine(de_ctx, u, s, smd, mpm_list);
997  }
998  }
999  } else if (b->packet) {
1000  /* set up pkt inspect engines */
1001  for (const DetectEnginePktInspectionEngine *e = de_ctx->pkt_inspect_engines; e != NULL;
1002  e = e->next) {
1003  SCLogDebug("e %p sm_list %u", e, e->sm_list);
1004  if (e->sm_list == s->init_data->buffers[x].id) {
1005  AppendPacketInspectEngine(de_ctx, e, s, smd, mpm_list);
1006  }
1007  }
1008  } else {
1009  SCLogDebug("app %s id %u parent %u rule %u xforms %u", b->name, b->id, b->parent_id,
1010  s->init_data->buffers[x].id, b->transforms.cnt);
1011  for (const DetectEngineAppInspectionEngine *t = de_ctx->app_inspect_engines; t != NULL;
1012  t = t->next) {
1013  if (t->sm_list == s->init_data->buffers[x].id) {
1014  if (s->flags & SIG_FLAG_TXBOTHDIR) {
1015  // ambiguous keywords have app engines in both directions
1016  // so we skip the wrong direction for this buffer
1017  if (s->init_data->buffers[x].only_tc && t->dir == 0) {
1018  continue;
1019  } else if (s->init_data->buffers[x].only_ts && t->dir == 1) {
1020  continue;
1021  }
1022  }
1023  AppendAppInspectEngine(
1024  de_ctx, t, s, smd, mpm_list, files_id, &last_id, &head_is_mpm);
1025  }
1026  }
1027  }
1028  }
1029 
1030  /* handle rules that have an app-layer hook w/o bringing their own app inspect engine,
1031  * e.g. `alert dns:request_complete ... (sid:1;)`
1032  *
1033  * Here we use a minimal stub inspect engine in which we set:
1034  * - alproto
1035  * - progress
1036  * - sm_list/sm_list_base to get the mapping to the hook name
1037  * - dir based on sig direction
1038  *
1039  * The inspect engine has no callback and is thus considered a straight match.
1040  */
1042  uint8_t dir = 0;
1046  if (s->flags & SIG_FLAG_TOSERVER)
1047  dir = 0;
1048  else if (s->flags & SIG_FLAG_TOCLIENT)
1049  dir = 1;
1050 
1052  .alproto = s->init_data->hook.t.app.alproto,
1053  .progress = s->init_data->hook.t.app.app_progress,
1054  .sub_state = s->init_data->hook.t.app.sub_state,
1055  .sm_list = (uint16_t)s->init_data->hook.sm_list,
1056  .sm_list_base = (uint16_t)s->init_data->hook.sm_list,
1057  .dir = dir,
1058  };
1059  AppendAppInspectEngine(de_ctx, &t, s, NULL, mpm_list, files_id, &last_id, &head_is_mpm);
1060  }
1061 
1064  {
1065  /* if engine is added multiple times, we pass it the same list */
1067  BUG_ON(stream == NULL);
1068  if (s->flags & SIG_FLAG_TOSERVER && !(s->flags & SIG_FLAG_TOCLIENT)) {
1069  AppendStreamInspectEngine(s, stream, 0, last_id + 1);
1070  } else if (s->flags & SIG_FLAG_TOCLIENT && !(s->flags & SIG_FLAG_TOSERVER)) {
1071  AppendStreamInspectEngine(s, stream, 1, last_id + 1);
1072  } else {
1073  AppendStreamInspectEngine(s, stream, 0, last_id + 1);
1074  AppendStreamInspectEngine(s, stream, 1, last_id + 1);
1075  }
1076 
1078  SCLogDebug("set SIG_FLAG_FLUSH on %u", s->id);
1079  s->flags |= SIG_FLAG_FLUSH;
1080  }
1081  }
1082 
1083 #ifdef DEBUG
1085  while (iter) {
1086  SCLogDebug("%u: engine %s id %u progress %d %s", s->id,
1088  iter->sm_list == mpm_list ? "MPM" : "");
1089  iter = iter->next;
1090  }
1091 #endif
1092  return 0;
1093 }
1094 
1095 /** \brief free app inspect engines for a signature
1096  *
1097  * For lists that are registered multiple times, like http_header and
1098  * http_cookie, making the engines owner of the lists is complicated.
1099  * Multiple engines in a sig may be pointing to the same list. To
1100  * address this the 'free' code needs to be extra careful about not
1101  * double freeing, so it takes an approach to first fill an array
1102  * of the to-free pointers before freeing them.
1103  */
1105 {
1106  int engines = 0;
1107 
1109  while (ie) {
1110  ie = ie->next;
1111  engines++;
1112  }
1114  while (e) {
1115  e = e->next;
1116  engines++;
1117  }
1119  while (u) {
1120  u = u->next;
1121  engines++;
1122  }
1123  if (engines == 0) {
1124  BUG_ON(s->pkt_inspect);
1125  BUG_ON(s->frame_inspect);
1126  return;
1127  }
1128 
1129  SigMatchData *bufs[engines];
1130  memset(&bufs, 0, (engines * sizeof(SigMatchData *)));
1131  int arrays = 0;
1132 
1133  /* free engines and put smd in the array */
1134  ie = s->app_inspect;
1135  while (ie) {
1137 
1138  bool skip = false;
1139  for (int i = 0; i < arrays; i++) {
1140  if (bufs[i] == ie->smd) {
1141  skip = true;
1142  break;
1143  }
1144  }
1145  if (!skip) {
1146  bufs[arrays++] = ie->smd;
1147  }
1148  SCFree(ie);
1149  ie = next;
1150  }
1151  e = s->pkt_inspect;
1152  while (e) {
1154 
1155  bool skip = false;
1156  for (int i = 0; i < arrays; i++) {
1157  if (bufs[i] == e->smd) {
1158  skip = true;
1159  break;
1160  }
1161  }
1162  if (!skip) {
1163  bufs[arrays++] = e->smd;
1164  }
1165  SCFree(e);
1166  e = next;
1167  }
1168  u = s->frame_inspect;
1169  while (u) {
1171 
1172  bool skip = false;
1173  for (int i = 0; i < arrays; i++) {
1174  if (bufs[i] == u->smd) {
1175  skip = true;
1176  break;
1177  }
1178  }
1179  if (!skip) {
1180  bufs[arrays++] = u->smd;
1181  }
1182  SCFree(u);
1183  u = next;
1184  }
1185 
1186  for (int i = 0; i < engines; i++) {
1187  if (bufs[i] == NULL)
1188  continue;
1189  SigMatchData *smd = bufs[i];
1190  while (1) {
1191  if (sigmatch_table[smd->type].Free != NULL) {
1192  sigmatch_table[smd->type].Free(de_ctx, smd->ctx);
1193  }
1194  if (smd->is_last)
1195  break;
1196  smd++;
1197  }
1198  SCFree(bufs[i]);
1199  }
1200 }
1201 
1202 /* code for registering buffers */
1203 
1204 #include "util-hash-lookup3.h"
1205 
1206 static HashListTable *g_buffer_type_hash = NULL;
1207 static int g_buffer_type_id = DETECT_SM_LIST_DYNAMIC_START;
1208 static int g_buffer_type_reg_closed = 0;
1209 
1211 {
1212  return g_buffer_type_id;
1213 }
1214 
1215 static void DetectBufferAddTransformData(DetectBufferType *map)
1216 {
1217  for (int i = 0; i < map->transforms.cnt; i++) {
1218  const TransformData *t = &map->transforms.transforms[i];
1221  &map->xform_id[i].id_data, &map->xform_id[i].id_data_len, t->options);
1222  SCLogDebug("transform identity data: [%p] \"%s\" [%d]", map->xform_id[i].id_data,
1223  (char *)map->xform_id[i].id_data, map->xform_id[i].id_data_len);
1224  }
1225  }
1226 }
1227 
1228 static uint32_t DetectBufferTypeHashNameFunc(HashListTable *ht, void *data, uint16_t datalen)
1229 {
1230  const DetectBufferType *map = (DetectBufferType *)data;
1231  uint32_t hash = hashlittle_safe(map->name, strlen(map->name), 0);
1232 
1233  // Add the transform data
1234  // - Collect transform id and position
1235  // - Collect identity data, if any
1236  hash += hashlittle_safe((uint8_t *)&map->transforms.cnt, sizeof(map->transforms.cnt), 0);
1237  for (int i = 0; i < map->transforms.cnt; i++) {
1238  const TransformData *t = &map->transforms.transforms[i];
1239  int tval = t->transform;
1240  hash += hashlittle_safe((uint8_t *)&tval, sizeof(tval), 0);
1241  if (map->xform_id[i].id_data) {
1242  hash += hashlittle_safe(
1243  &map->xform_id[i].id_data_len, sizeof(map->xform_id[i].id_data_len), 0);
1244  hash += hashlittle_safe(map->xform_id[i].id_data, map->xform_id[i].id_data_len, 0);
1245  }
1246  }
1247  hash %= ht->array_size;
1248  SCLogDebug("map->name %s, hash %d", map->name, hash);
1249  return hash;
1250 }
1251 
1252 static uint32_t DetectBufferTypeHashIdFunc(HashListTable *ht, void *data, uint16_t datalen)
1253 {
1254  const DetectBufferType *map = (DetectBufferType *)data;
1255  uint32_t hash = map->id;
1256  hash %= ht->array_size;
1257  return hash;
1258 }
1259 
1260 static char DetectBufferTypeCompareNameFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
1261 {
1262  DetectBufferType *map1 = (DetectBufferType *)data1;
1263  DetectBufferType *map2 = (DetectBufferType *)data2;
1264 
1265  char r = (strcmp(map1->name, map2->name) == 0);
1266 
1267  // Compare the transforms
1268  // the transform supports identity, that data will also be added.
1269  r &= map1->transforms.cnt == map2->transforms.cnt;
1270  if (r && map1->transforms.cnt) {
1271  for (int i = 0; i < map1->transforms.cnt; i++) {
1272  if (map1->transforms.transforms[i].transform !=
1273  map2->transforms.transforms[i].transform) {
1274  r = 0;
1275  break;
1276  }
1277 
1278  SCLogDebug("%s: transform ids match; checking specialized data", map1->name);
1279  // Checks
1280  // - Both NULL: --> ok, continue
1281  // - One NULL: --> no match, break?
1282  // - identity data lengths match: --> ok, continue
1283  // - identity data matches: ok
1284 
1285  // Stop if only one is NULL
1286  if ((map1->xform_id[i].id_data == NULL) ^ (map2->xform_id[i].id_data == NULL)) {
1287  SCLogDebug("identity data: only one is null");
1288  r = 0;
1289  break;
1290  } else if (map1->xform_id[i].id_data == NULL) { /* continue when both are null */
1291  SCLogDebug("identity data: both null");
1292  r = 1;
1293  continue;
1294  } else if (map1->xform_id[i].id_data_len != map2->xform_id[i].id_data_len) {
1295  // Stop when id data lengths aren't equal
1296  SCLogDebug("id data: unequal lengths");
1297  r = 0;
1298  break;
1299  }
1300 
1301  // stop if the identity data is different
1302  r &= memcmp(map1->xform_id[i].id_data, map2->xform_id[i].id_data,
1303  map1->xform_id[i].id_data_len) == 0;
1304  if (r == 0)
1305  break;
1306  SCLogDebug("identity data: data matches");
1307  }
1308  }
1309  return r;
1310 }
1311 
1312 static char DetectBufferTypeCompareIdFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
1313 {
1314  DetectBufferType *map1 = (DetectBufferType *)data1;
1315  DetectBufferType *map2 = (DetectBufferType *)data2;
1316  return map1->id == map2->id;
1317 }
1318 
1319 static void DetectBufferTypeFreeFunc(void *data)
1320 {
1321  DetectBufferType *map = (DetectBufferType *)data;
1322 
1323  if (map == NULL) {
1324  return;
1325  }
1326 
1327  /* Release transformation option memory, if any */
1328  for (int i = 0; i < map->transforms.cnt; i++) {
1329  if (map->transforms.transforms[i].options == NULL)
1330  continue;
1331 
1332  if (sigmatch_table[map->transforms.transforms[i].transform].Free == NULL) {
1333  SCLogError("%s allocates transform option memory but has no free routine",
1335  continue;
1336  }
1338  }
1339 
1340  SCFree(map);
1341 }
1342 
1343 static int DetectBufferTypeInit(void)
1344 {
1345  BUG_ON(g_buffer_type_hash);
1346  g_buffer_type_hash = HashListTableInit(256, DetectBufferTypeHashNameFunc,
1347  DetectBufferTypeCompareNameFunc, DetectBufferTypeFreeFunc);
1348  if (g_buffer_type_hash == NULL)
1349  return -1;
1350 
1351  return 0;
1352 }
1353 #if 0
1354 static void DetectBufferTypeFree(void)
1355 {
1356  if (g_buffer_type_hash == NULL)
1357  return;
1358 
1359  HashListTableFree(g_buffer_type_hash);
1360  g_buffer_type_hash = NULL;
1361 }
1362 #endif
1363 static int DetectBufferTypeAdd(const char *string)
1364 {
1365  BUG_ON(string == NULL || strlen(string) >= 64);
1366 
1367  DetectBufferType *map = SCCalloc(1, sizeof(*map));
1368  if (map == NULL)
1369  return -1;
1370 
1371  strlcpy(map->name, string, sizeof(map->name));
1372  map->id = g_buffer_type_id++;
1373 
1374  BUG_ON(HashListTableAdd(g_buffer_type_hash, (void *)map, 0) != 0);
1375  SCLogDebug("buffer %s registered with id %d", map->name, map->id);
1376  return map->id;
1377 }
1378 
1379 static DetectBufferType *DetectBufferTypeLookupByName(const char *string)
1380 {
1381  DetectBufferType map;
1382  memset(&map, 0, sizeof(map));
1383  strlcpy(map.name, string, sizeof(map.name));
1384 
1385  DetectBufferType *res = HashListTableLookup(g_buffer_type_hash, &map, 0);
1386  return res;
1387 }
1388 
1390 {
1391  BUG_ON(g_buffer_type_reg_closed);
1392  if (g_buffer_type_hash == NULL)
1393  DetectBufferTypeInit();
1394 
1395  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1396  if (!exists) {
1397  return DetectBufferTypeAdd(name);
1398  } else {
1399  return exists->id;
1400  }
1401 }
1402 
1404 {
1405  BUG_ON(g_buffer_type_reg_closed);
1407  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1408  BUG_ON(!exists);
1409  exists->multi_instance = true;
1410  SCLogDebug("%p %s -- %d supports multi instance", exists, name, exists->id);
1411 }
1412 
1414 {
1415  BUG_ON(g_buffer_type_reg_closed);
1417  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1418  BUG_ON(!exists);
1419  exists->frame = true;
1420  SCLogDebug("%p %s -- %d supports frame inspection", exists, name, exists->id);
1421 }
1422 
1424 {
1425  BUG_ON(g_buffer_type_reg_closed);
1427  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1428  BUG_ON(!exists);
1429  exists->packet = true;
1430  SCLogDebug("%p %s -- %d supports packet inspection", exists, name, exists->id);
1431 }
1432 
1434 {
1435  BUG_ON(g_buffer_type_reg_closed);
1437  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1438  BUG_ON(!exists);
1439  exists->mpm = true;
1440  SCLogDebug("%p %s -- %d supports mpm", exists, name, exists->id);
1441 }
1442 
1444 {
1445  BUG_ON(g_buffer_type_reg_closed);
1447  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1448  BUG_ON(!exists);
1449  exists->supports_transforms = true;
1450  SCLogDebug("%p %s -- %d supports transformations", exists, name, exists->id);
1451 }
1452 
1454 {
1455  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1456  if (!exists) {
1457  return -1;
1458  }
1459  return exists->id;
1460 }
1461 
1462 static DetectBufferType *DetectEngineBufferTypeLookupByName(
1463  const DetectEngineCtx *de_ctx, const char *string)
1464 {
1465  DetectBufferType map;
1466  memset(&map, 0, sizeof(map));
1467  strlcpy(map.name, string, sizeof(map.name));
1468 
1470  return res;
1471 }
1472 
1474 {
1475  DetectBufferType lookup;
1476  memset(&lookup, 0, sizeof(lookup));
1477  lookup.id = id;
1478  const DetectBufferType *res =
1479  HashListTableLookup(de_ctx->buffer_type_hash_id, (void *)&lookup, 0);
1480  return res;
1481 }
1482 
1484 {
1486  return res ? res->name : NULL;
1487 }
1488 
1489 static int DetectEngineBufferTypeAdd(DetectEngineCtx *de_ctx, const char *string)
1490 {
1491  BUG_ON(string == NULL || strlen(string) >= 32);
1492 
1493  DetectBufferType *map = SCCalloc(1, sizeof(*map));
1494  if (map == NULL)
1495  return -1;
1496 
1497  strlcpy(map->name, string, sizeof(map->name));
1498  map->id = de_ctx->buffer_type_id++;
1499 
1500  BUG_ON(HashListTableAdd(de_ctx->buffer_type_hash_name, (void *)map, 0) != 0);
1501  BUG_ON(HashListTableAdd(de_ctx->buffer_type_hash_id, (void *)map, 0) != 0);
1502  SCLogDebug("buffer %s registered with id %d", map->name, map->id);
1503  return map->id;
1504 }
1505 
1507  const int direction, const AppProto alproto, const uint8_t frame_type)
1508 {
1509  DetectBufferType *exists = DetectEngineBufferTypeLookupByName(de_ctx, name);
1510  if (exists) {
1511  return exists->id;
1512  }
1513 
1514  const int buffer_id = DetectEngineBufferTypeAdd(de_ctx, name);
1515  if (buffer_id < 0) {
1516  return -1;
1517  }
1518 
1519  /* TODO hack we need the map to get the name. Should we return the map at reg? */
1520  const DetectBufferType *map = DetectEngineBufferTypeGetById(de_ctx, buffer_id);
1521  BUG_ON(!map);
1522 
1523  /* register MPM/inspect engines */
1524  if (direction & SIG_FLAG_TOSERVER) {
1526  PrefilterGenericMpmFrameRegister, alproto, frame_type);
1528  DetectEngineInspectFrameBufferGeneric, alproto, frame_type);
1529  }
1530  if (direction & SIG_FLAG_TOCLIENT) {
1532  PrefilterGenericMpmFrameRegister, alproto, frame_type);
1534  DetectEngineInspectFrameBufferGeneric, alproto, frame_type);
1535  }
1536 
1537  return buffer_id;
1538 }
1539 
1541 {
1542  DetectBufferType *exists = DetectEngineBufferTypeLookupByName(de_ctx, name);
1543  if (!exists) {
1544  return DetectEngineBufferTypeAdd(de_ctx, name);
1545  } else {
1546  return exists->id;
1547  }
1548 }
1549 
1550 void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
1551 {
1552  BUG_ON(desc == NULL || strlen(desc) >= 128);
1553 
1554  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1555  if (!exists) {
1556  return;
1557  }
1558  strlcpy(exists->description, desc, sizeof(exists->description));
1559 }
1560 
1562 {
1564  if (!exists) {
1565  return NULL;
1566  }
1567  return exists->description;
1568 }
1569 
1571 {
1572  DetectBufferType *exists = DetectEngineBufferTypeLookupByName(de_ctx, name);
1573  BUG_ON(!exists);
1574  exists->frame = true;
1575  SCLogDebug("%p %s -- %d supports frame inspection", exists, name, exists->id);
1576 }
1577 
1579 {
1580  DetectBufferType *exists = DetectEngineBufferTypeLookupByName(de_ctx, name);
1581  BUG_ON(!exists);
1582  exists->packet = true;
1583  SCLogDebug("%p %s -- %d supports packet inspection", exists, name, exists->id);
1584 }
1585 
1587 {
1588  DetectBufferType *exists = DetectEngineBufferTypeLookupByName(de_ctx, name);
1589  BUG_ON(!exists);
1590  exists->mpm = true;
1591  SCLogDebug("%p %s -- %d supports mpm", exists, name, exists->id);
1592 }
1593 
1595 {
1596  DetectBufferType *exists = DetectEngineBufferTypeLookupByName(de_ctx, name);
1597  BUG_ON(!exists);
1598  exists->supports_transforms = true;
1599  SCLogDebug("%p %s -- %d supports transformations", exists, name, exists->id);
1600 }
1601 
1603 {
1605  if (map == NULL)
1606  return false;
1607  SCLogDebug("map %p id %d multi_instance? %s", map, id, BOOL2STR(map->multi_instance));
1608  return map->multi_instance;
1609 }
1610 
1612 {
1614  if (map == NULL)
1615  return false;
1616  SCLogDebug("map %p id %d packet? %d", map, id, map->packet);
1617  return map->packet;
1618 }
1619 
1621 {
1623  if (map == NULL)
1624  return false;
1625  SCLogDebug("map %p id %d mpm? %d", map, id, map->mpm);
1626  return map->mpm;
1627 }
1628 
1630 {
1632  if (map == NULL)
1633  return false;
1634  SCLogDebug("map %p id %d frame? %d", map, id, map->frame);
1635  return map->frame;
1636 }
1637 
1639  void (*SetupCallback)(const DetectEngineCtx *, Signature *, const DetectBufferType *))
1640 {
1641  BUG_ON(g_buffer_type_reg_closed);
1643  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1644  BUG_ON(!exists);
1645  exists->SetupCallback = SetupCallback;
1646 }
1647 
1649 {
1651  if (map && map->SetupCallback) {
1652  map->SetupCallback(de_ctx, s, map);
1653  }
1654 }
1655 
1657  const char *name, bool (*ValidateCallback)(const Signature *, const char **sigerror,
1658  const DetectBufferType *))
1659 {
1660  BUG_ON(g_buffer_type_reg_closed);
1662  DetectBufferType *exists = DetectBufferTypeLookupByName(name);
1663  BUG_ON(!exists);
1664  exists->ValidateCallback = ValidateCallback;
1665 }
1666 
1668  const DetectEngineCtx *de_ctx, const int id, const Signature *s, const char **sigerror)
1669 {
1671  // only run validation if the buffer is not transformed
1672  if (map && map->ValidateCallback && map->transforms.cnt == 0) {
1673  return map->ValidateCallback(s, sigerror, map);
1674  }
1675  return true;
1676 }
1677 
1678 bool DetectBufferIsPresent(const Signature *s, const uint32_t buf_id)
1679 {
1680  for (uint32_t i = 0; i < s->init_data->buffer_index; i++) {
1681  if (buf_id == s->init_data->buffers[i].id) {
1682  return true;
1683  }
1684  }
1685  return false;
1686 }
1687 
1688 /** \brief Check content byte array compatibility with transforms
1689  *
1690  * The "content" array is presented to the transforms so that each
1691  * transform may validate that it's compatible with the transform.
1692  *
1693  * When a transform indicates the byte array is incompatible, none of the
1694  * subsequent transforms, if any, are invoked. This means the first validation
1695  * failure terminates the loop.
1696  *
1697  * \param de_ctx Detection engine context.
1698  * \param sm_list The SM list id.
1699  * \param content The byte array being validated
1700  * \param namestr returns the name of the transform that is incompatible with
1701  * content.
1702  *
1703  * \retval true (false) If any of the transforms indicate the byte array is
1704  * (is not) compatible.
1705  **/
1707  const uint8_t *content, uint16_t content_len, const char **namestr)
1708 {
1709  const DetectBufferType *dbt = DetectEngineBufferTypeGetById(de_ctx, sm_list);
1710  BUG_ON(dbt == NULL);
1711 
1712  for (int i = 0; i < dbt->transforms.cnt; i++) {
1713  const TransformData *t = &dbt->transforms.transforms[i];
1715  continue;
1716 
1717  if (sigmatch_table[t->transform].TransformValidate(content, content_len, t->options)) {
1718  continue;
1719  }
1720 
1721  if (namestr) {
1722  *namestr = sigmatch_table[t->transform].name;
1723  }
1724 
1725  return false;
1726  }
1727 
1728  return true;
1729 }
1730 
1731 static void DetectBufferTypeSetupDetectEngine(DetectEngineCtx *de_ctx)
1732 {
1733  const int size = g_buffer_type_id;
1734  BUG_ON(!(size > 0));
1735 
1736  de_ctx->buffer_type_hash_name = HashListTableInit(256, DetectBufferTypeHashNameFunc,
1737  DetectBufferTypeCompareNameFunc, DetectBufferTypeFreeFunc);
1740  HashListTableInit(256, DetectBufferTypeHashIdFunc, DetectBufferTypeCompareIdFunc,
1741  NULL); // entries owned by buffer_type_hash_name
1742  BUG_ON(de_ctx->buffer_type_hash_id == NULL);
1743  de_ctx->buffer_type_id = g_buffer_type_id;
1744 
1745  SCLogDebug("DETECT_SM_LIST_DYNAMIC_START %u", DETECT_SM_LIST_DYNAMIC_START);
1746  HashListTableBucket *b = HashListTableGetListHead(g_buffer_type_hash);
1747  while (b) {
1749 
1750  DetectBufferType *copy = SCCalloc(1, sizeof(*copy));
1751  BUG_ON(!copy);
1752  memcpy(copy, map, sizeof(*copy));
1753  int r = HashListTableAdd(de_ctx->buffer_type_hash_name, (void *)copy, 0);
1754  BUG_ON(r != 0);
1755  r = HashListTableAdd(de_ctx->buffer_type_hash_id, (void *)copy, 0);
1756  BUG_ON(r != 0);
1757 
1758  SCLogDebug("name %s id %d mpm %s packet %s -- %s. "
1759  "Callbacks: Setup %p Validate %p",
1760  map->name, map->id, map->mpm ? "true" : "false", map->packet ? "true" : "false",
1761  map->description, map->SetupCallback, map->ValidateCallback);
1762  b = HashListTableGetListNext(b);
1763  }
1764 
1767  DetectAppLayerInspectEngineCopyListToDetectCtx(de_ctx);
1769  DetectFrameInspectEngineCopyListToDetectCtx(de_ctx);
1771  DetectPktInspectEngineCopyListToDetectCtx(de_ctx);
1772 }
1773 
1774 static void DetectBufferTypeFreeDetectEngine(DetectEngineCtx *de_ctx)
1775 {
1776  if (de_ctx) {
1781 
1783  while (ilist) {
1785  SCFree(ilist);
1786  ilist = next;
1787  }
1789  while (mlist) {
1790  DetectBufferMpmRegistry *next = mlist->next;
1791  SCFree(mlist);
1792  mlist = next;
1793  }
1795  while (plist) {
1797  SCFree(plist);
1798  plist = next;
1799  }
1801  while (pmlist) {
1802  DetectBufferMpmRegistry *next = pmlist->next;
1803  SCFree(pmlist);
1804  pmlist = next;
1805  }
1807  while (framelist) {
1809  SCFree(framelist);
1810  framelist = next;
1811  }
1813  while (framemlist) {
1814  DetectBufferMpmRegistry *next = framemlist->next;
1815  SCFree(framemlist);
1816  framemlist = next;
1817  }
1819  }
1820 }
1821 
1823 {
1824  BUG_ON(g_buffer_type_hash == NULL);
1825 
1826  g_buffer_type_reg_closed = 1;
1827 }
1828 
1830  DetectEngineCtx *de_ctx, const int id, TransformData *transforms, uint8_t transform_cnt)
1831 {
1832  const DetectBufferType *base_map = DetectEngineBufferTypeGetById(de_ctx, id);
1833  if (!base_map) {
1834  return -1;
1835  }
1836  if (!base_map->supports_transforms) {
1837  SCLogError("buffer '%s' does not support transformations", base_map->name);
1838  return -1;
1839  }
1840 
1841  SCLogDebug("base_map %s", base_map->name);
1842 
1844  memset(&t, 0, sizeof(t));
1845  for (int i = 0; i < transform_cnt; i++) {
1846  t.transforms[i] = transforms[i];
1847  }
1848  t.cnt = transform_cnt;
1849 
1850  DetectBufferType lookup_map;
1851  memset(&lookup_map, 0, sizeof(lookup_map));
1852  strlcpy(lookup_map.name, base_map->name, sizeof(lookup_map.name));
1853  lookup_map.transforms = t;
1854 
1855  /* Add transform identity data from transforms */
1856  if (t.cnt) {
1857  DetectBufferAddTransformData(&lookup_map);
1858  }
1860 
1861  SCLogDebug("res %p", res);
1862  if (res != NULL) {
1863  return res->id;
1864  }
1865 
1866  DetectBufferType *map = SCCalloc(1, sizeof(*map));
1867  if (map == NULL)
1868  return -1;
1869 
1870  strlcpy(map->name, base_map->name, sizeof(map->name));
1871  map->id = de_ctx->buffer_type_id++;
1872  map->parent_id = base_map->id;
1873  map->transforms = t;
1874  map->mpm = base_map->mpm;
1875  map->packet = base_map->packet;
1876  map->frame = base_map->frame;
1877  map->SetupCallback = base_map->SetupCallback;
1878  map->ValidateCallback = base_map->ValidateCallback;
1879  if (map->frame) {
1881  } else if (map->packet) {
1883  map->id, map->parent_id, &map->transforms);
1884  } else {
1886  map->id, map->parent_id, &map->transforms);
1887  }
1888 
1889  BUG_ON(HashListTableAdd(de_ctx->buffer_type_hash_name, (void *)map, 0) != 0);
1890  BUG_ON(HashListTableAdd(de_ctx->buffer_type_hash_id, (void *)map, 0) != 0);
1891  SCLogDebug("buffer %s registered with id %d, parent %d", map->name, map->id, map->parent_id);
1892 
1893  if (map->frame) {
1894  DetectFrameInspectEngineCopy(de_ctx, map->parent_id, map->id, &map->transforms);
1895  } else if (map->packet) {
1896  DetectPktInspectEngineCopy(de_ctx, map->parent_id, map->id, &map->transforms);
1897  } else {
1898  DetectAppLayerInspectEngineCopy(de_ctx, map->parent_id, map->id, &map->transforms);
1899  }
1900  return map->id;
1901 }
1902 
1903 /* returns false if no match, true if match */
1904 static int DetectEngineInspectRulePacketMatches(
1905  DetectEngineThreadCtx *det_ctx,
1906  const DetectEnginePktInspectionEngine *engine,
1907  const Signature *s,
1908  Packet *p, uint8_t *_alert_flags)
1909 {
1910  SCEnter();
1911 
1912  /* run the packet match functions */
1914  const SigMatchData *smd = s->sm_arrays[DETECT_SM_LIST_MATCH];
1915 
1916  SCLogDebug("running match functions, sm %p", smd);
1917  while (1) {
1919  if (sigmatch_table[smd->type].Match(det_ctx, p, s, smd->ctx) <= 0) {
1920  KEYWORD_PROFILING_END(det_ctx, smd->type, 0);
1921  SCLogDebug("no match");
1923  }
1924  KEYWORD_PROFILING_END(det_ctx, smd->type, 1);
1925  if (smd->is_last) {
1926  SCLogDebug("match and is_last");
1927  break;
1928  }
1929  smd++;
1930  }
1932 }
1933 
1934 static int DetectEngineInspectRulePayloadMatches(
1935  DetectEngineThreadCtx *det_ctx,
1936  const DetectEnginePktInspectionEngine *engine,
1937  const Signature *s, Packet *p, uint8_t *alert_flags)
1938 {
1939  SCEnter();
1940 
1941  DetectEngineCtx *de_ctx = det_ctx->de_ctx;
1942 
1944  /* if we have stream msgs, inspect against those first,
1945  * but not for a "dsize" signature */
1946  if (s->flags & SIG_FLAG_REQUIRE_STREAM) {
1947  int pmatch = 0;
1949  pmatch = DetectEngineInspectStreamPayload(de_ctx, det_ctx, s, p->flow, p);
1950  if (pmatch) {
1951  *alert_flags |= PACKET_ALERT_FLAG_STREAM_MATCH;
1952  }
1953  }
1954  /* no match? then inspect packet payload */
1955  if (pmatch == 0) {
1956  SCLogDebug("no match in stream, fall back to packet payload");
1957 
1958  /* skip if we don't have to inspect the packet and segment was
1959  * added to stream */
1960  if (!(s->flags & SIG_FLAG_REQUIRE_PACKET) && (p->flags & PKT_STREAM_ADD)) {
1962  }
1964  SCLogDebug("SIG_FLAG_REQUIRE_STREAM_ONLY, so no match");
1966  }
1967  if (DetectEngineInspectPacketPayload(de_ctx, det_ctx, s, p->flow, p) != 1) {
1969  }
1970  }
1971  } else {
1972  if (DetectEngineInspectPacketPayload(de_ctx, det_ctx, s, p->flow, p) != 1) {
1974  }
1975  }
1977 }
1978 
1980  DetectEngineThreadCtx *det_ctx, const Signature *s,
1981  Flow *f, Packet *p,
1982  uint8_t *alert_flags)
1983 {
1984  SCEnter();
1985 
1986  for (DetectEnginePktInspectionEngine *e = s->pkt_inspect; e != NULL; e = e->next) {
1987  if (e->v1.Callback(det_ctx, e, s, p, alert_flags) != DETECT_ENGINE_INSPECT_SIG_MATCH) {
1988  SCLogDebug("sid %u: e %p Callback returned no match", s->id, e);
1989  return false;
1990  }
1991  SCLogDebug("sid %u: e %p Callback returned true", s->id, e);
1992  }
1993 
1994  SCLogDebug("sid %u: returning true", s->id);
1995  return true;
1996 }
1997 
1998 /**
1999  * \param data pointer to SigMatchData. Allowed to be NULL.
2000  */
2001 static int DetectEnginePktInspectionAppend(Signature *s, InspectionBufferPktInspectFunc Callback,
2002  SigMatchData *data, const int list_id)
2003 {
2004  DetectEnginePktInspectionEngine *e = SCCalloc(1, sizeof(*e));
2005  if (e == NULL)
2006  return -1;
2007 
2008  e->mpm = s->init_data->mpm_sm_list == list_id;
2009  DEBUG_VALIDATE_BUG_ON(list_id < 0 || list_id > UINT16_MAX);
2010  e->sm_list = (uint16_t)list_id;
2011  e->sm_list_base = (uint16_t)list_id;
2012  e->v1.Callback = Callback;
2013  e->smd = data;
2014 
2015  if (s->pkt_inspect == NULL) {
2016  s->pkt_inspect = e;
2017  } else {
2019  while (a->next != NULL) {
2020  a = a->next;
2021  }
2022  a->next = e;
2023  }
2024  return 0;
2025 }
2026 
2028 {
2029  /* only handle PMATCH here if we're not an app inspect rule */
2031  if (DetectEnginePktInspectionAppend(
2032  s, DetectEngineInspectRulePayloadMatches, NULL, DETECT_SM_LIST_PMATCH) < 0)
2033  return -1;
2034  SCLogDebug("sid %u: DetectEngineInspectRulePayloadMatches appended", s->id);
2035  }
2036 
2037  if (s->sm_arrays[DETECT_SM_LIST_MATCH]) {
2038  if (DetectEnginePktInspectionAppend(
2039  s, DetectEngineInspectRulePacketMatches, NULL, DETECT_SM_LIST_MATCH) < 0)
2040  return -1;
2041  SCLogDebug("sid %u: DetectEngineInspectRulePacketMatches appended", s->id);
2042  }
2043 
2044  return 0;
2045 }
2046 
2047 /* code to control the main thread to do a reload */
2048 
2050  IDLE, /**< ready to start a reload */
2051  RELOAD, /**< command main thread to do the reload */
2052 };
2053 
2054 
2055 typedef struct DetectEngineSyncer_ {
2059 
2060 static DetectEngineSyncer detect_sync = { SCMUTEX_INITIALIZER, IDLE };
2061 
2062 /* tell main to start reloading */
2064 {
2065  int r = 0;
2066  SCMutexLock(&detect_sync.m);
2067  if (detect_sync.state == IDLE) {
2068  detect_sync.state = RELOAD;
2069  } else {
2070  r = -1;
2071  }
2072  SCMutexUnlock(&detect_sync.m);
2073  return r;
2074 }
2075 
2076 /* main thread checks this to see if it should start */
2078 {
2079  int r = 0;
2080  SCMutexLock(&detect_sync.m);
2081  if (detect_sync.state == RELOAD) {
2082  r = 1;
2083  }
2084  SCMutexUnlock(&detect_sync.m);
2085  return r;
2086 }
2087 
2088 /* main thread sets done when it's done */
2090 {
2091  SCMutexLock(&detect_sync.m);
2092  detect_sync.state = IDLE;
2093  SCMutexUnlock(&detect_sync.m);
2094 }
2095 
2096 /* caller loops this until it returns 1 */
2098 {
2099  int r = 0;
2100  SCMutexLock(&detect_sync.m);
2101  if (detect_sync.state == IDLE) {
2102  r = 1;
2103  }
2104  SCMutexUnlock(&detect_sync.m);
2105  return r;
2106 }
2107 
2108 /** \brief Do the content inspection & validation for a signature
2109  *
2110  * \param de_ctx Detection engine context
2111  * \param det_ctx Detection engine thread context
2112  * \param s Signature to inspect
2113  * \param sm SigMatch to inspect
2114  * \param f Flow
2115  * \param flags app layer flags
2116  * \param state App layer state
2117  *
2118  * \retval 0 no match
2119  * \retval 1 match
2120  */
2122  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
2123  uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
2124 {
2125  SigMatchData *smd = engine->smd;
2126  SCLogDebug("running match functions, sm %p", smd);
2127  if (smd != NULL) {
2128  while (1) {
2129  int match = 0;
2131  match = sigmatch_table[smd->type].
2132  AppLayerTxMatch(det_ctx, f, flags, alstate, txv, s, smd->ctx);
2133  KEYWORD_PROFILING_END(det_ctx, smd->type, (match == 1));
2134  if (match == 0)
2136  if (match == 2) {
2138  }
2139 
2140  if (smd->is_last)
2141  break;
2142  smd++;
2143  }
2144  }
2145 
2147 }
2148 
2149 static bool DetectTxCompleted(
2150  Flow *f, void *txv, uint8_t flags, const DetectEngineAppInspectionEngine *engine)
2151 {
2152  if (f->alproto == ALPROTO_DOH2 && engine->alproto == ALPROTO_DOH2) {
2153  // the DNS tx from DetectGetInnerTx is always complete
2154  return true;
2155  } // else
2156  return AppLayerParserGetStateProgress(f->proto, f->alproto, txv, flags) > engine->progress;
2157 }
2158 
2159 /**
2160  * \brief Do the content inspection & validation for a signature
2161  *
2162  * \param de_ctx Detection engine context
2163  * \param det_ctx Detection engine thread context
2164  * \param s Signature to inspect
2165  * \param f Flow
2166  * \param flags app layer flags
2167  * \param state App layer state
2168  *
2169  * \retval 0 no match.
2170  * \retval 1 match.
2171  * \retval 2 Sig can't match.
2172  */
2174  const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
2175  void *alstate, void *txv, uint64_t tx_id)
2176 {
2177  const int list_id = engine->sm_list;
2178  SCLogDebug("running inspect on %d", list_id);
2179 
2180  const bool eof = DetectTxCompleted(f, txv, flags, engine);
2181 
2182  SCLogDebug("list %d mpm? %s transforms %p", engine->sm_list, engine->mpm ? "true" : "false",
2183  engine->v2.transforms);
2184 
2185  /* if prefilter didn't already run, we need to consider transformations */
2186  const DetectEngineTransforms *transforms = NULL;
2187  if (!engine->mpm) {
2188  transforms = engine->v2.transforms;
2189  }
2190 
2191  const InspectionBuffer *buffer = DetectGetSingleData(
2192  det_ctx, transforms, f, flags, txv, list_id, engine->v2.GetDataSingle);
2193  if (unlikely(buffer == NULL)) {
2194  if (eof && engine->match_on_null) {
2196  }
2198  }
2199 
2200  const uint32_t data_len = buffer->inspect_len;
2201  const uint8_t *data = buffer->inspect;
2202  const uint64_t offset = buffer->inspect_offset;
2203 
2204  uint8_t ci_flags = eof ? DETECT_CI_FLAGS_END : 0;
2205  ci_flags |= (offset == 0 ? DETECT_CI_FLAGS_START : 0);
2206  ci_flags |= buffer->flags;
2207 
2208  /* Inspect all the uricontents fetched on each
2209  * transaction at the app layer */
2210  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f, data,
2212  if (match) {
2214  } else {
2216  }
2217 }
2218 
2219 /**
2220  * \brief Do the content inspection & validation for a signature
2221  *
2222  * \param de_ctx Detection engine context
2223  * \param det_ctx Detection engine thread context
2224  * \param s Signature to inspect
2225  * \param f Flow
2226  * \param flags app layer flags
2227  * \param state App layer state
2228  *
2229  * \retval 0 no match.
2230  * \retval 1 match.
2231  * \retval 2 Sig can't match.
2232  */
2234  const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
2235  void *alstate, void *txv, uint64_t tx_id)
2236 {
2237  const int list_id = engine->sm_list;
2238  SCLogDebug("running inspect on %d", list_id);
2239 
2240  const bool eof = DetectTxCompleted(f, txv, flags, engine);
2241 
2242  SCLogDebug("list %d mpm? %s transforms %p",
2243  engine->sm_list, engine->mpm ? "true" : "false", engine->v2.transforms);
2244 
2245  /* if prefilter didn't already run, we need to consider transformations */
2246  const DetectEngineTransforms *transforms = NULL;
2247  if (!engine->mpm) {
2248  transforms = engine->v2.transforms;
2249  }
2250 
2251  const InspectionBuffer *buffer = engine->v2.GetData(det_ctx, transforms,
2252  f, flags, txv, list_id);
2253  if (unlikely(buffer == NULL)) {
2254  if (eof && engine->match_on_null) {
2256  }
2259  }
2260 
2261  const uint32_t data_len = buffer->inspect_len;
2262  const uint8_t *data = buffer->inspect;
2263  const uint64_t offset = buffer->inspect_offset;
2264 
2265  uint8_t ci_flags = eof ? DETECT_CI_FLAGS_END : 0;
2266  ci_flags |= (offset == 0 ? DETECT_CI_FLAGS_START : 0);
2267  ci_flags |= buffer->flags;
2268 
2269  /* Inspect all the uricontents fetched on each
2270  * transaction at the app layer */
2271  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f, data,
2273  if (match) {
2275  } else {
2278  }
2279 }
2280 
2281 // wrapper for both DetectAppLayerInspectEngineRegister and DetectAppLayerMpmRegister
2282 // with cast of callback function
2283 void DetectAppLayerMultiRegisterSubState(const char *name, AppProto alproto, uint32_t dir,
2284  uint8_t sub_state, uint8_t progress, InspectionMultiBufferGetDataPtr GetData, int priority)
2285 {
2286  BUG_ON(AppLayerParserSupportsSubStates(alproto) && sub_state == 0);
2287  AppLayerInspectEngineRegisterInternal(name, alproto, dir, sub_state, progress,
2288  DetectEngineInspectMultiBufferGeneric, NULL, NULL, GetData);
2290  GetData, alproto, sub_state, progress);
2291 }
2292 
2293 // wrapper for both DetectAppLayerInspectEngineRegister and DetectAppLayerMpmRegister
2294 // with cast of callback function
2295 void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress,
2296  InspectionMultiBufferGetDataPtr GetData, int priority)
2297 {
2299  AppLayerInspectEngineRegisterInternal(name, alproto, dir, 0, (uint8_t)progress,
2300  DetectEngineInspectMultiBufferGeneric, NULL, NULL, GetData);
2302  name, dir, priority, PrefilterMultiGenericMpmRegister, GetData, alproto, progress);
2303 }
2304 
2306  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
2307  const int list_id, InspectionSingleBufferGetDataPtr GetBuf)
2308 {
2309  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
2310  if (buffer->inspect == NULL) {
2311  const uint8_t *b = NULL;
2312  uint32_t b_len = 0;
2313 
2314  if (!GetBuf(txv, flow_flags, &b, &b_len))
2315  return NULL;
2316 
2317  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
2318  }
2319  return buffer;
2320 }
2321 
2323  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
2324  const int list_id, uint32_t index, InspectionMultiBufferGetDataPtr GetBuf)
2325 {
2326  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, index);
2327  if (buffer == NULL) {
2328  return NULL;
2329  }
2330  if (buffer->initialized) {
2331  return buffer;
2332  }
2333 
2334  const uint8_t *data = NULL;
2335  uint32_t data_len = 0;
2336 
2337  if (!GetBuf(det_ctx, txv, flow_flags, index, &data, &data_len)) {
2339  return NULL;
2340  }
2341  InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
2342  buffer->flags = DETECT_CI_FLAGS_SINGLE;
2343  return buffer;
2344 }
2345 
2348  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
2349 {
2350  uint32_t local_id = 0;
2351  const DetectEngineTransforms *transforms = NULL;
2352  if (!engine->mpm) {
2353  transforms = engine->v2.transforms;
2354  }
2355 
2356  do {
2357  InspectionBuffer *buffer = DetectGetMultiData(det_ctx, transforms, f, flags, txv,
2358  engine->sm_list, local_id, engine->v2.GetMultiData);
2359 
2360  if (buffer == NULL || buffer->inspect == NULL)
2361  break;
2362 
2363  // The GetData functions set buffer->flags to DETECT_CI_FLAGS_SINGLE
2364  // This is not meant for streaming buffers
2365  const bool match = DetectEngineContentInspectionBuffer(de_ctx, det_ctx, s, engine->smd,
2367  if (match) {
2369  }
2370  local_id++;
2371  } while (1);
2372  if (local_id == 0) {
2373  // That means we did not get even one buffer value from the multi-buffer
2374  const bool eof = DetectTxCompleted(f, txv, flags, engine);
2375  if (eof && engine->match_on_null) {
2377  }
2378  }
2380 }
2381 
2382 /**
2383  * \brief Do the content inspection & validation for a signature
2384  *
2385  * \param de_ctx Detection engine context
2386  * \param det_ctx Detection engine thread context
2387  * \param s Signature to inspect
2388  * \param p Packet
2389  *
2390  * \retval 0 no match.
2391  * \retval 1 match.
2392  */
2394  DetectEngineThreadCtx *det_ctx,
2395  const DetectEnginePktInspectionEngine *engine,
2396  const Signature *s, Packet *p, uint8_t *_alert_flags)
2397 {
2398  const int list_id = engine->sm_list;
2399  SCLogDebug("running inspect on %d", list_id);
2400 
2401  SCLogDebug("list %d transforms %p",
2402  engine->sm_list, engine->v1.transforms);
2403 
2404  /* if prefilter didn't already run, we need to consider transformations */
2405  const DetectEngineTransforms *transforms = NULL;
2406  if (!engine->mpm) {
2407  transforms = engine->v1.transforms;
2408  }
2409 
2410  const InspectionBuffer *buffer = engine->v1.GetData(det_ctx, transforms, p,
2411  list_id);
2412  if (unlikely(buffer == NULL)) {
2414  }
2415 
2416  uint8_t ci_flags = DETECT_CI_FLAGS_START|DETECT_CI_FLAGS_END;
2417  ci_flags |= buffer->flags;
2418 
2419  /* Inspect all the uricontents fetched on each
2420  * transaction at the app layer */
2421  const bool match = DetectEngineContentInspection(det_ctx->de_ctx, det_ctx, s, engine->smd, p,
2422  p->flow, buffer->inspect, buffer->inspect_len, 0, ci_flags,
2424  if (match) {
2426  } else {
2428  }
2429 }
2430 
2431 /** \internal
2432  * \brief inject a pseudo packet into each detect thread
2433  * -that doesn't use the new det_ctx yet
2434  * -*or*, if the thread should flush its output logs.
2435  */
2436 static void InjectPackets(
2437  ThreadVars **detect_tvs, DetectEngineThreadCtx **new_det_ctx, int no_of_detect_tvs)
2438 {
2439  /* inject a fake packet if the detect thread that needs it. This function
2440  * is called if
2441  * - A thread isn't using a DE ctx and should
2442  * - Or, it should process a pseudo packet and flush its output logs.
2443  * to speed the process. */
2444  for (int i = 0; i < no_of_detect_tvs; i++) {
2445  if (SC_ATOMIC_GET(new_det_ctx[i]->so_far_used_by_detect) != 1) {
2446  if (detect_tvs[i]->inq != NULL) {
2448  if (p != NULL) {
2451  PacketQueue *q = detect_tvs[i]->inq->pq;
2452  SCMutexLock(&q->mutex_q);
2453  PacketEnqueue(q, p);
2454  SCCondSignal(&q->cond_q);
2455  SCMutexUnlock(&q->mutex_q);
2456  }
2457  }
2458  }
2459  }
2460 }
2461 
2462 static void DetectEngineLoadFlowbitSettings(DetectEngineCtx *de_ctx)
2463 {
2465  char varname[128] = "detect.flowbits.max-per-signature";
2466  if (strlen(de_ctx->config_prefix) > 0) {
2467  snprintf(varname, sizeof(varname), "%s.detect.flowbits.max-per-signature",
2469  }
2470  const char *str;
2471  if (SCConfGet(varname, &str) == 1) {
2472  uint8_t val = 0;
2473  int ret = StringParseUint8(&val, 10, 0, str);
2474  if (ret > 0) {
2475  if (val > 0) {
2476  de_ctx->max_flowbits = val;
2477  } else {
2478  SCLogWarning("Invalid setting for flowbits.max-per-signature %d, resetting to the "
2479  "default",
2480  val);
2481  }
2482  } else {
2483  SCLogWarning(
2484  "Invalid setting for flowbits.max-per-signature, resetting to the default");
2485  }
2486  }
2487  SCLogConfig("Setting flowbits.max-per-signature to %d", de_ctx->max_flowbits);
2488 }
2489 
2490 /** \internal
2491  * \brief Update detect threads with new detect engine
2492  *
2493  * Atomically update each detect thread with a new thread context
2494  * that is associated to the new detection engine(s).
2495  *
2496  * If called in unix socket mode, it's possible that we don't have
2497  * detect threads yet.
2498  * NOTE: master MUST be locked before calling this
2499  *
2500  * \retval -1 error
2501  * \retval 0 no detection threads
2502  * \retval 1 successful reload
2503  */
2504 static int DetectEngineReloadThreads(DetectEngineCtx *new_de_ctx)
2505 {
2506  SCEnter();
2507  uint32_t i = 0;
2508 
2509  /* count detect threads in use */
2510  const uint32_t no_of_detect_tvs = TmThreadCountThreadsByTmmFlags(TM_FLAG_FLOWWORKER_TM);
2511  /* can be zero in unix socket mode */
2512  if (no_of_detect_tvs == 0) {
2513  return 0;
2514  }
2515 
2516  /* prepare swap structures */
2517  DetectEngineThreadCtx *old_det_ctx[no_of_detect_tvs];
2518  DetectEngineThreadCtx *new_det_ctx[no_of_detect_tvs];
2519  ThreadVars *detect_tvs[no_of_detect_tvs];
2520  memset(old_det_ctx, 0x00, (no_of_detect_tvs * sizeof(DetectEngineThreadCtx *)));
2521  memset(new_det_ctx, 0x00, (no_of_detect_tvs * sizeof(DetectEngineThreadCtx *)));
2522  memset(detect_tvs, 0x00, (no_of_detect_tvs * sizeof(ThreadVars *)));
2523 
2524  /* start the process of swapping detect threads ctxs */
2525 
2526  /* get reference to tv's and setup new_det_ctx array */
2528  for (ThreadVars *tv = tv_root[TVT_PPT]; tv != NULL; tv = tv->next) {
2529  if ((tv->tmm_flags & TM_FLAG_FLOWWORKER_TM) == 0) {
2530  continue;
2531  }
2532  for (TmSlot *s = tv->tm_slots; s != NULL; s = s->slot_next) {
2533  TmModule *tm = TmModuleGetById(s->tm_id);
2534  if (!(tm->flags & TM_FLAG_FLOWWORKER_TM)) {
2535  continue;
2536  }
2537 
2538  if (suricata_ctl_flags != 0) {
2540  goto error;
2541  }
2542 
2543  old_det_ctx[i] = FlowWorkerGetDetectCtxPtr(SC_ATOMIC_GET(s->slot_data));
2544  detect_tvs[i] = tv;
2545 
2546  new_det_ctx[i] = DetectEngineThreadCtxInitForReload(tv, new_de_ctx, 1);
2547  if (new_det_ctx[i] == NULL) {
2548  SCLogError("Detect engine thread init "
2549  "failure in live rule swap. Let's get out of here");
2551  goto error;
2552  }
2553  SCLogDebug("live rule swap created new det_ctx - %p and de_ctx "
2554  "- %p\n", new_det_ctx[i], new_de_ctx);
2555  i++;
2556  break;
2557  }
2558  }
2559  BUG_ON(i != no_of_detect_tvs);
2560 
2561  /* atomically replace the det_ctx data */
2562  i = 0;
2563  for (ThreadVars *tv = tv_root[TVT_PPT]; tv != NULL; tv = tv->next) {
2564  if ((tv->tmm_flags & TM_FLAG_FLOWWORKER_TM) == 0) {
2565  continue;
2566  }
2567  for (TmSlot *s = tv->tm_slots; s != NULL; s = s->slot_next) {
2568  TmModule *tm = TmModuleGetById(s->tm_id);
2569  if (!(tm->flags & TM_FLAG_FLOWWORKER_TM)) {
2570  continue;
2571  }
2572  SCLogDebug("swapping new det_ctx - %p with older one - %p",
2573  new_det_ctx[i], SC_ATOMIC_GET(s->slot_data));
2574  DEBUG_VALIDATE_BUG_ON(i >= no_of_detect_tvs); // help scan-build
2575  FlowWorkerReplaceDetectCtx(SC_ATOMIC_GET(s->slot_data), new_det_ctx[i]);
2576  i++;
2577  break;
2578  }
2579  }
2581 
2582  /* threads now all have new data, however they may not have started using
2583  * it and may still use the old data */
2584 
2585  SCLogDebug("Live rule swap has swapped %d old det_ctx's with new ones, "
2586  "along with the new de_ctx", no_of_detect_tvs);
2587 
2588  InjectPackets(detect_tvs, new_det_ctx, no_of_detect_tvs);
2589 
2590  /* loop waiting for detect threads to switch to the new det_ctx. Try to
2591  * wake up capture if needed (break loop). */
2592  uint32_t threads_done = 0;
2593 retry:
2594  for (i = 0; i < no_of_detect_tvs; i++) {
2595  if (suricata_ctl_flags != 0) {
2596  threads_done = no_of_detect_tvs;
2597  break;
2598  }
2599  SleepMsec(1);
2600  if (SC_ATOMIC_GET(new_det_ctx[i]->so_far_used_by_detect) == 1) {
2601  SCLogDebug("new_det_ctx - %p used by detect engine", new_det_ctx[i]);
2602  threads_done++;
2603  } else {
2604  TmThreadsCaptureBreakLoop(detect_tvs[i]);
2605  }
2606  }
2607  if (threads_done < no_of_detect_tvs) {
2608  threads_done = 0;
2609  SleepMsec(250);
2610  goto retry;
2611  }
2612 
2613  /* this is to make sure that if someone initiated shutdown during a live
2614  * rule swap, the live rule swap won't clean up the old det_ctx and
2615  * de_ctx, till all detect threads have stopped working and sitting
2616  * silently after setting RUNNING_DONE flag and while waiting for
2617  * THV_DEINIT flag */
2618  if (i != no_of_detect_tvs) { // not all threads we swapped
2619  for (ThreadVars *tv = tv_root[TVT_PPT]; tv != NULL; tv = tv->next) {
2620  if ((tv->tmm_flags & TM_FLAG_FLOWWORKER_TM) == 0) {
2621  continue;
2622  }
2623 
2625  SleepUsec(100);
2626  }
2627  }
2628  }
2629 
2630  /* free all the ctxs */
2631  for (i = 0; i < no_of_detect_tvs; i++) {
2632  SCLogDebug("Freeing old_det_ctx - %p used by detect",
2633  old_det_ctx[i]);
2634  DetectEngineThreadCtxDeinit(NULL, old_det_ctx[i]);
2635  }
2636 
2638 
2639  return 1;
2640 
2641  error:
2642  for (i = 0; i < no_of_detect_tvs; i++) {
2643  if (new_det_ctx[i] != NULL)
2644  DetectEngineThreadCtxDeinit(NULL, new_det_ctx[i]);
2645  }
2646  return -1;
2647 }
2648 
2650 {
2651  int sgh_mpm_caching = 0;
2652  if (SCConfGetBool("detect.sgh-mpm-caching", &sgh_mpm_caching) != 1) {
2653  return false;
2654  }
2655  return (bool)sgh_mpm_caching;
2656 }
2657 
2659 {
2660  if (DetectEngineMpmCachingEnabled() == false) {
2661  return NULL;
2662  }
2663 
2664  char yamlpath[] = "detect.sgh-mpm-caching-path";
2665  const char *strval = NULL;
2666  if (SCConfGet(yamlpath, &strval) == 1 && strval != NULL) {
2667  return strval;
2668  }
2669 
2670  static bool notified = false;
2671  if (!notified) {
2672  SCLogInfo("%s has no path specified, using %s", yamlpath, SGH_CACHE_DIR);
2673  notified = true;
2674  }
2675  return SGH_CACHE_DIR;
2676 }
2677 
2678 void DetectEngineMpmCacheService(uint32_t op_flags)
2679 {
2681  if (!de_ctx) {
2682  return;
2683  }
2684 
2685  if (!de_ctx->mpm_cfg || !de_ctx->mpm_cfg->cache_dir_path) {
2686  goto error;
2687  }
2688 
2689  if (mpm_table[de_ctx->mpm_matcher].CacheStatsInit != NULL) {
2691  if (de_ctx->mpm_cfg->cache_stats == NULL) {
2692  goto error;
2693  }
2694  }
2695 
2696  if (op_flags & DETECT_ENGINE_MPM_CACHE_OP_SAVE) {
2697  if (mpm_table[de_ctx->mpm_matcher].CacheRuleset != NULL) {
2699  }
2700  }
2701 
2702  if (op_flags & DETECT_ENGINE_MPM_CACHE_OP_PRUNE) {
2703  if (mpm_table[de_ctx->mpm_matcher].CachePrune != NULL) {
2705  }
2706  }
2707 
2708  if (mpm_table[de_ctx->mpm_matcher].CacheStatsPrint != NULL) {
2710  }
2711 
2712  if (mpm_table[de_ctx->mpm_matcher].CacheStatsDeinit != NULL) {
2714  de_ctx->mpm_cfg->cache_stats = NULL;
2715  }
2716 
2717 error:
2719 }
2720 
2721 static DetectEngineCtx *DetectEngineCtxInitReal(
2722  enum DetectEngineType type, const char *prefix, uint32_t tenant_id)
2723 {
2725  if (unlikely(de_ctx == NULL))
2726  goto error;
2727 
2728  memset(&de_ctx->sig_stat, 0, sizeof(SigFileLoaderStat));
2729  TAILQ_INIT(&de_ctx->sig_stat.failed_sigs);
2730  de_ctx->sigerror = NULL;
2731  de_ctx->type = type;
2733  de_ctx->tenant_id = tenant_id;
2734 
2737 
2740  if (de_ctx->mpm_cfg == NULL) {
2741  goto error;
2742  }
2743 
2747 
2749  if (SCConfGetTime("detect.sgh-mpm-caching-max-age",
2751  de_ctx->mpm_cfg->cache_max_age_seconds = 7ULL * 24ULL * 60ULL * 60ULL;
2752  }
2753  }
2754  }
2755  }
2756 
2759  SCLogDebug("stub %u with version %u", type, de_ctx->version);
2760  return de_ctx;
2761  }
2762 
2763  if (prefix != NULL) {
2764  strlcpy(de_ctx->config_prefix, prefix, sizeof(de_ctx->config_prefix));
2765  }
2766 
2767  int failure_fatal = 0;
2768  if (SCConfGetBool("engine.init-failure-fatal", (int *)&failure_fatal) != 1) {
2769  SCLogDebug("ConfGetBool could not load the value.");
2770  }
2771  de_ctx->failure_fatal = (failure_fatal == 1);
2772 
2773  SCLogConfig("pattern matchers: MPM: %s, SPM: %s", mpm_table[de_ctx->mpm_matcher].name,
2776  if (de_ctx->spm_global_thread_ctx == NULL) {
2777  SCLogDebug("Unable to alloc SpmGlobalThreadCtx.");
2778  goto error;
2779  }
2780 
2782  if (de_ctx->sm_types_prefilter == NULL) {
2783  goto error;
2784  }
2786  if (de_ctx->sm_types_silent_error == NULL) {
2787  goto error;
2788  }
2789  if (DetectEngineCtxLoadConf(de_ctx) == -1) {
2790  goto error;
2791  }
2792 
2798  DetectBufferTypeSetupDetectEngine(de_ctx);
2800 
2801  /* init iprep... ignore errors for now */
2802  (void)SRepInit(de_ctx);
2803 
2807  goto error;
2808  }
2809 
2810  if (ActionInitConfig() < 0) {
2811  goto error;
2812  }
2814  if (SCRConfLoadReferenceConfigFile(de_ctx, NULL) < 0) {
2816  goto error;
2817  }
2818 
2820  SCLogDebug("dectx with version %u", de_ctx->version);
2821  return de_ctx;
2822 error:
2823  if (de_ctx != NULL) {
2825  }
2826  return NULL;
2827 }
2828 
2830 {
2831  return DetectEngineCtxInitReal(DETECT_ENGINE_TYPE_MT_STUB, NULL, 0);
2832 }
2833 
2835 {
2836  return DetectEngineCtxInitReal(DETECT_ENGINE_TYPE_DD_STUB, NULL, 0);
2837 }
2838 
2840 {
2841  return DetectEngineCtxInitReal(DETECT_ENGINE_TYPE_NORMAL, NULL, 0);
2842 }
2843 
2844 DetectEngineCtx *DetectEngineCtxInitWithPrefix(const char *prefix, uint32_t tenant_id)
2845 {
2846  if (prefix == NULL || strlen(prefix) == 0)
2847  return DetectEngineCtxInit();
2848  else
2849  return DetectEngineCtxInitReal(DETECT_ENGINE_TYPE_NORMAL, prefix, tenant_id);
2850 }
2851 
2852 static void DetectEngineCtxFreeThreadKeywordData(DetectEngineCtx *de_ctx)
2853 {
2855 }
2856 
2857 static void DetectEngineCtxFreeFailedSigs(DetectEngineCtx *de_ctx)
2858 {
2859  SigString *item = NULL;
2860  SigString *sitem;
2861 
2862  TAILQ_FOREACH_SAFE(item, &de_ctx->sig_stat.failed_sigs, next, sitem) {
2863  SCFree(item->filename);
2864  SCFree(item->sig_str);
2865  if (item->sig_error) {
2866  SCFree(item->sig_error);
2867  }
2868  TAILQ_REMOVE(&de_ctx->sig_stat.failed_sigs, item, next);
2869  SCFree(item);
2870  }
2871 }
2872 
2873 /**
2874  * \brief Free a DetectEngineCtx::
2875  *
2876  * \param de_ctx DetectEngineCtx:: to be freed
2877  */
2879 {
2880 
2881  if (de_ctx == NULL)
2882  return;
2883 
2884 #ifdef PROFILE_RULES
2885  if (de_ctx->profile_ctx != NULL) {
2886  SCProfilingRuleDestroyCtx(de_ctx->profile_ctx);
2887  de_ctx->profile_ctx = NULL;
2888  }
2889 #endif
2890 #ifdef PROFILING
2891  if (de_ctx->profile_keyword_ctx != NULL) {
2892  SCProfilingKeywordDestroyCtx(de_ctx);//->profile_keyword_ctx);
2893 // de_ctx->profile_keyword_ctx = NULL;
2894  }
2895  if (de_ctx->profile_sgh_ctx != NULL) {
2897  }
2899 #endif
2900 
2903  }
2904  /* Normally the hashes are freed elsewhere, but
2905  * to be sure look at them again here.
2906  */
2912  if (de_ctx->sig_array)
2914 
2915  if (de_ctx->filedata_config)
2917 
2921 
2923 
2927 
2929 
2930  DetectEngineCtxFreeThreadKeywordData(de_ctx);
2932  DetectEngineCtxFreeFailedSigs(de_ctx);
2933 
2936 
2937  /* if we have a config prefix, remove the config from the tree */
2938  if (strlen(de_ctx->config_prefix) > 0) {
2939  /* remove config */
2941  if (node != NULL) {
2942  SCConfNodeRemove(node); /* frees node */
2943  }
2944 #if 0
2945  SCConfDump();
2946 #endif
2947  }
2948 
2951 
2952  DetectBufferTypeFreeDetectEngine(de_ctx);
2955 
2956  if (de_ctx->tenant_path) {
2958  }
2959 
2960  if (de_ctx->requirements) {
2961  SCDetectRequiresStatusFree(de_ctx->requirements);
2962  }
2963 
2964  if (de_ctx->non_pf_engine_names) {
2966  }
2967  if (de_ctx->fw_policies) {
2968  for (uint32_t i = 0; i < DETECT_FIREWALL_POLICY_SIZE; i++) {
2972  }
2973  }
2975  }
2977  SCFree(de_ctx);
2978  //DetectAddressGroupPrintMemory();
2979  //DetectSigGroupPrintMemory();
2980  //DetectPortPrintMemory();
2981 }
2982 
2983 /** \brief Function that load DetectEngineCtx config for grouping sigs
2984  * used by the engine
2985  * \retval 0 if no config provided, 1 if config was provided
2986  * and loaded successfully
2987  */
2988 static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx)
2989 {
2990  uint8_t profile = ENGINE_PROFILE_MEDIUM;
2991  const char *max_uniq_toclient_groups_str = NULL;
2992  const char *max_uniq_toserver_groups_str = NULL;
2993  const char *sgh_mpm_context = NULL;
2994  const char *de_ctx_profile = NULL;
2995 
2996  (void)SCConfGet("detect.profile", &de_ctx_profile);
2997  (void)SCConfGet("detect.sgh-mpm-context", &sgh_mpm_context);
2998 
2999  SCConfNode *de_ctx_custom = SCConfGetNode("detect-engine");
3000  SCConfNode *opt = NULL;
3001 
3002  if (de_ctx_custom != NULL) {
3003  TAILQ_FOREACH(opt, &de_ctx_custom->head, next) {
3004  if (de_ctx_profile == NULL) {
3005  if (opt->val && strcmp(opt->val, "profile") == 0) {
3006  de_ctx_profile = opt->head.tqh_first->val;
3007  }
3008  }
3009 
3010  if (sgh_mpm_context == NULL) {
3011  if (opt->val && strcmp(opt->val, "sgh-mpm-context") == 0) {
3012  sgh_mpm_context = opt->head.tqh_first->val;
3013  }
3014  }
3015  }
3016  }
3017 
3018  if (de_ctx_profile != NULL) {
3019  if (strcmp(de_ctx_profile, "low") == 0 ||
3020  strcmp(de_ctx_profile, "lowest") == 0) { // legacy
3021  profile = ENGINE_PROFILE_LOW;
3022  } else if (strcmp(de_ctx_profile, "medium") == 0) {
3023  profile = ENGINE_PROFILE_MEDIUM;
3024  } else if (strcmp(de_ctx_profile, "high") == 0 ||
3025  strcmp(de_ctx_profile, "highest") == 0) { // legacy
3026  profile = ENGINE_PROFILE_HIGH;
3027  } else if (strcmp(de_ctx_profile, "custom") == 0) {
3028  profile = ENGINE_PROFILE_CUSTOM;
3029  } else {
3030  SCLogError("invalid value for detect.profile: '%s'. "
3031  "Valid options: low, medium, high and custom.",
3032  de_ctx_profile);
3033  return -1;
3034  }
3035 
3036  SCLogDebug("Profile for detection engine groups is \"%s\"", de_ctx_profile);
3037  } else {
3038  SCLogDebug("Profile for detection engine groups not provided "
3039  "at suricata.yaml. Using default (\"medium\").");
3040  }
3041 
3042  /* detect-engine.sgh-mpm-context option parsing */
3043  if (sgh_mpm_context == NULL || strcmp(sgh_mpm_context, "auto") == 0) {
3044  /* for now, since we still haven't implemented any intelligence into
3045  * understanding the patterns and distributing mpm_ctx across sgh */
3047  de_ctx->mpm_matcher == MPM_HS) {
3049  } else {
3051  }
3052  } else {
3053  if (strcmp(sgh_mpm_context, "single") == 0) {
3055  } else if (strcmp(sgh_mpm_context, "full") == 0) {
3057  } else {
3058  SCLogError("You have supplied an "
3059  "invalid conf value for detect-engine.sgh-mpm-context-"
3060  "%s",
3061  sgh_mpm_context);
3062  exit(EXIT_FAILURE);
3063  }
3064  }
3065 
3066  if (RunmodeIsUnittests()) {
3068  }
3069 
3070  /* parse profile custom-values */
3071  opt = NULL;
3072  switch (profile) {
3073  case ENGINE_PROFILE_LOW:
3076  break;
3077 
3078  case ENGINE_PROFILE_HIGH:
3081  break;
3082 
3083  case ENGINE_PROFILE_CUSTOM:
3084  (void)SCConfGet("detect.custom-values.toclient-groups", &max_uniq_toclient_groups_str);
3085  (void)SCConfGet("detect.custom-values.toserver-groups", &max_uniq_toserver_groups_str);
3086 
3087  if (de_ctx_custom != NULL) {
3088  TAILQ_FOREACH(opt, &de_ctx_custom->head, next) {
3089  if (opt->val && strcmp(opt->val, "custom-values") == 0) {
3090  if (max_uniq_toclient_groups_str == NULL) {
3091  max_uniq_toclient_groups_str = (char *)SCConfNodeLookupChildValue(
3092  opt->head.tqh_first, "toclient-sp-groups");
3093  }
3094  if (max_uniq_toclient_groups_str == NULL) {
3095  max_uniq_toclient_groups_str = (char *)SCConfNodeLookupChildValue(
3096  opt->head.tqh_first, "toclient-groups");
3097  }
3098  if (max_uniq_toserver_groups_str == NULL) {
3099  max_uniq_toserver_groups_str = (char *)SCConfNodeLookupChildValue(
3100  opt->head.tqh_first, "toserver-dp-groups");
3101  }
3102  if (max_uniq_toserver_groups_str == NULL) {
3103  max_uniq_toserver_groups_str = (char *)SCConfNodeLookupChildValue(
3104  opt->head.tqh_first, "toserver-groups");
3105  }
3106  }
3107  }
3108  }
3109  if (max_uniq_toclient_groups_str != NULL) {
3111  (uint16_t)strlen(max_uniq_toclient_groups_str),
3112  (const char *)max_uniq_toclient_groups_str) <= 0) {
3114 
3115  SCLogWarning("parsing '%s' for "
3116  "toclient-groups failed, using %u",
3117  max_uniq_toclient_groups_str, de_ctx->max_uniq_toclient_groups);
3118  }
3119  } else {
3121  }
3122  SCLogConfig("toclient-groups %u", de_ctx->max_uniq_toclient_groups);
3123 
3124  if (max_uniq_toserver_groups_str != NULL) {
3126  (uint16_t)strlen(max_uniq_toserver_groups_str),
3127  (const char *)max_uniq_toserver_groups_str) <= 0) {
3129 
3130  SCLogWarning("parsing '%s' for "
3131  "toserver-groups failed, using %u",
3132  max_uniq_toserver_groups_str, de_ctx->max_uniq_toserver_groups);
3133  }
3134  } else {
3136  }
3137  SCLogConfig("toserver-groups %u", de_ctx->max_uniq_toserver_groups);
3138  break;
3139 
3140  /* Default (or no config provided) is profile medium */
3141  case ENGINE_PROFILE_MEDIUM:
3143  default:
3146  break;
3147  }
3148 
3149  intmax_t value = 0;
3151  if (SCConfGetInt("detect.inspection-recursion-limit", &value) == 1) {
3152  if (value >= 0 && value <= INT_MAX) {
3153  de_ctx->inspection_recursion_limit = (int)value;
3154  }
3155 
3156  /* fall back to old config parsing */
3157  } else {
3158  SCConfNode *insp_recursion_limit_node = NULL;
3159  char *insp_recursion_limit = NULL;
3160 
3161  if (de_ctx_custom != NULL) {
3162  opt = NULL;
3163  TAILQ_FOREACH(opt, &de_ctx_custom->head, next) {
3164  if (opt->val && strcmp(opt->val, "inspection-recursion-limit") != 0)
3165  continue;
3166 
3167  insp_recursion_limit_node = SCConfNodeLookupChild(opt, opt->val);
3168  if (insp_recursion_limit_node == NULL) {
3169  SCLogError("Error retrieving conf "
3170  "entry for detect-engine:inspection-recursion-limit");
3171  break;
3172  }
3173  insp_recursion_limit = insp_recursion_limit_node->val;
3174  SCLogDebug("Found detect-engine.inspection-recursion-limit - %s:%s",
3175  insp_recursion_limit_node->name, insp_recursion_limit_node->val);
3176  break;
3177  }
3178 
3179  if (insp_recursion_limit != NULL) {
3181  0, (const char *)insp_recursion_limit) < 0) {
3182  SCLogWarning("Invalid value for "
3183  "detect-engine.inspection-recursion-limit: %s "
3184  "resetting to %d",
3185  insp_recursion_limit, DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT);
3188  }
3189  }
3190  }
3191  }
3192 
3195 
3196  SCLogDebug("de_ctx->inspection_recursion_limit: %d",
3198 
3199  // default value is 4
3201  if (SCConfGetInt("detect.stream-tx-log-limit", &value) == 1) {
3202  if (value >= 0 && value <= UINT8_MAX) {
3203  de_ctx->guess_applayer_log_limit = (uint8_t)value;
3204  } else {
3205  SCLogWarning("Invalid value for detect-engine.stream-tx-log-limit: must be between 0 "
3206  "and 255, will default to 4");
3207  }
3208  }
3209  int guess_applayer = 0;
3210  if ((SCConfGetBool("detect.guess-applayer-tx", &guess_applayer)) == 1) {
3211  if (guess_applayer == 1) {
3212  de_ctx->guess_applayer = true;
3213  }
3214  }
3215 
3216  /* parse port grouping priority settings */
3217 
3218  const char *ports = NULL;
3219  (void)SCConfGet("detect.grouping.tcp-priority-ports", &ports);
3220  if (ports) {
3221  SCLogConfig("grouping: tcp-priority-ports %s", ports);
3222  } else {
3223  (void)SCConfGet("detect.grouping.tcp-whitelist", &ports);
3224  if (ports) {
3225  SCLogConfig(
3226  "grouping: tcp-priority-ports from legacy 'tcp-whitelist' setting: %s", ports);
3227  } else {
3228  ports = "53, 80, 139, 443, 445, 1433, 3306, 3389, 6666, 6667, 8080";
3229  SCLogConfig("grouping: tcp-priority-ports (default) %s", ports);
3230  }
3231  }
3232  if (DetectPortParse(de_ctx, &de_ctx->tcp_priorityports, ports) != 0) {
3233  SCLogWarning("'%s' is not a valid value "
3234  "for detect.grouping.tcp-priority-ports",
3235  ports);
3236  }
3238  for ( ; x != NULL; x = x->next) {
3239  if (x->port != x->port2) {
3240  SCLogWarning("'%s' is not a valid value "
3241  "for detect.grouping.tcp-priority-ports: only single ports allowed",
3242  ports);
3244  de_ctx->tcp_priorityports = NULL;
3245  break;
3246  }
3247  }
3248 
3249  ports = NULL;
3250  (void)SCConfGet("detect.grouping.udp-priority-ports", &ports);
3251  if (ports) {
3252  SCLogConfig("grouping: udp-priority-ports %s", ports);
3253  } else {
3254  (void)SCConfGet("detect.grouping.udp-whitelist", &ports);
3255  if (ports) {
3256  SCLogConfig(
3257  "grouping: udp-priority-ports from legacy 'udp-whitelist' setting: %s", ports);
3258  } else {
3259  ports = "53, 135, 5060";
3260  SCLogConfig("grouping: udp-priority-ports (default) %s", ports);
3261  }
3262  }
3263  if (DetectPortParse(de_ctx, &de_ctx->udp_priorityports, ports) != 0) {
3264  SCLogWarning("'%s' is not a valid value "
3265  "for detect.grouping.udp-priority-ports",
3266  ports);
3267  }
3268  for (x = de_ctx->udp_priorityports; x != NULL; x = x->next) {
3269  if (x->port != x->port2) {
3270  SCLogWarning("'%s' is not a valid value "
3271  "for detect.grouping.udp-priority-ports: only single ports allowed",
3272  ports);
3274  de_ctx->udp_priorityports = NULL;
3275  break;
3276  }
3277  }
3278 
3279  DetectEngineLoadFlowbitSettings(de_ctx);
3280 
3282  const char *pf_setting = NULL;
3283  if (SCConfGet("detect.prefilter.default", &pf_setting) == 1 && pf_setting) {
3284  if (strcasecmp(pf_setting, "mpm") == 0) {
3286  } else if (strcasecmp(pf_setting, "auto") == 0) {
3288  }
3289  }
3290  switch (de_ctx->prefilter_setting) {
3291  case DETECT_PREFILTER_MPM:
3292  SCLogConfig("prefilter engines: MPM");
3293  break;
3294  case DETECT_PREFILTER_AUTO:
3295  SCLogConfig("prefilter engines: MPM and keywords");
3296  break;
3297  }
3298 
3299  return 0;
3300 }
3301 
3303 {
3304  de_ctx->signum = 0;
3305 }
3306 
3307 static int DetectEngineThreadCtxInitGlobalKeywords(DetectEngineThreadCtx *det_ctx)
3308 {
3309  const DetectEngineMasterCtx *master = &g_master_de_ctx;
3310 
3311  if (master->keyword_id > 0) {
3312  // coverity[suspicious_sizeof : FALSE]
3313  det_ctx->global_keyword_ctxs_array = (void **)SCCalloc(master->keyword_id, sizeof(void *));
3314  if (det_ctx->global_keyword_ctxs_array == NULL) {
3315  SCLogError("setting up thread local detect ctx");
3316  return TM_ECODE_FAILED;
3317  }
3318  det_ctx->global_keyword_ctxs_size = master->keyword_id;
3319 
3320  const DetectEngineThreadKeywordCtxItem *item = master->keyword_list;
3321  while (item) {
3322  det_ctx->global_keyword_ctxs_array[item->id] = item->InitFunc(item->data);
3323  if (det_ctx->global_keyword_ctxs_array[item->id] == NULL) {
3324  SCLogError("setting up thread local detect ctx "
3325  "for keyword \"%s\" failed",
3326  item->name);
3327  return TM_ECODE_FAILED;
3328  }
3329  item = item->next;
3330  }
3331  }
3332  return TM_ECODE_OK;
3333 }
3334 
3335 static void DetectEngineThreadCtxDeinitGlobalKeywords(DetectEngineThreadCtx *det_ctx)
3336 {
3337  if (det_ctx->global_keyword_ctxs_array == NULL ||
3338  det_ctx->global_keyword_ctxs_size == 0) {
3339  return;
3340  }
3341 
3342  const DetectEngineMasterCtx *master = &g_master_de_ctx;
3343  if (master->keyword_id > 0) {
3344  const DetectEngineThreadKeywordCtxItem *item = master->keyword_list;
3345  while (item) {
3346  if (det_ctx->global_keyword_ctxs_array[item->id] != NULL)
3347  item->FreeFunc(det_ctx->global_keyword_ctxs_array[item->id]);
3348 
3349  item = item->next;
3350  }
3351  det_ctx->global_keyword_ctxs_size = 0;
3353  det_ctx->global_keyword_ctxs_array = NULL;
3354  }
3355 }
3356 
3357 static int DetectEngineThreadCtxInitKeywords(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx)
3358 {
3359  if (de_ctx->keyword_id > 0) {
3360  // coverity[suspicious_sizeof : FALSE]
3361  det_ctx->keyword_ctxs_array = SCCalloc(de_ctx->keyword_id, sizeof(void *));
3362  if (det_ctx->keyword_ctxs_array == NULL) {
3363  SCLogError("setting up thread local detect ctx");
3364  return TM_ECODE_FAILED;
3365  }
3366 
3367  det_ctx->keyword_ctxs_size = de_ctx->keyword_id;
3368 
3370  for (; hb != NULL; hb = HashListTableGetListNext(hb)) {
3372 
3373  det_ctx->keyword_ctxs_array[item->id] = item->InitFunc(item->data);
3374  if (det_ctx->keyword_ctxs_array[item->id] == NULL) {
3375  SCLogError("setting up thread local detect ctx "
3376  "for keyword \"%s\" failed",
3377  item->name);
3378  return TM_ECODE_FAILED;
3379  }
3380  }
3381  }
3382  return TM_ECODE_OK;
3383 }
3384 
3385 static void DetectEngineThreadCtxDeinitKeywords(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx)
3386 {
3387  if (de_ctx->keyword_id > 0) {
3389  for (; hb != NULL; hb = HashListTableGetListNext(hb)) {
3391 
3392  if (det_ctx->keyword_ctxs_array[item->id] != NULL)
3393  item->FreeFunc(det_ctx->keyword_ctxs_array[item->id]);
3394  }
3395  det_ctx->keyword_ctxs_size = 0;
3396  SCFree(det_ctx->keyword_ctxs_array);
3397  det_ctx->keyword_ctxs_array = NULL;
3398  }
3399 }
3400 
3401 /** NOTE: master MUST be locked before calling this */
3402 static TmEcode DetectEngineThreadCtxInitForMT(ThreadVars *tv, DetectEngineThreadCtx *det_ctx)
3403 {
3404  DetectEngineMasterCtx *master = &g_master_de_ctx;
3405 
3406  DetectEngineTenantMapping *map_array = NULL;
3407  uint32_t map_array_size = 0;
3408  uint32_t map_cnt = 0;
3409  uint32_t max_tenant_id = 0;
3410  DetectEngineCtx *list = master->list;
3411 
3413 
3414  /* coverity[missing_lock] */
3415  if (master->tenant_selector == TENANT_SELECTOR_UNKNOWN) {
3416  SCLogError("no tenant selector set: "
3417  "set using multi-detect.selector");
3418  return TM_ECODE_FAILED;
3419  }
3420 
3421  uint32_t tcnt = 0;
3422  while (list) {
3423  if (list->tenant_id > max_tenant_id)
3424  max_tenant_id = list->tenant_id;
3425 
3426  list = list->next;
3427  tcnt++;
3428  }
3429 
3430  HashTable *mt_det_ctxs_hash =
3431  HashTableInit(tcnt * 2, TenantIdHash, TenantIdCompare, TenantIdFree);
3432  if (mt_det_ctxs_hash == NULL) {
3433  goto error;
3434  }
3435 
3436  if (tcnt == 0) {
3437  SCLogInfo("no tenants left, or none registered yet");
3438  } else {
3439  max_tenant_id++;
3440 
3442  while (map) {
3443  map_cnt++;
3444  map = map->next;
3445  }
3446 
3447  if (map_cnt > 0) {
3448  map_array_size = map_cnt + 1;
3449 
3450  map_array = SCCalloc(map_array_size, sizeof(*map_array));
3451  if (map_array == NULL)
3452  goto error;
3453 
3454  /* fill the array */
3455  map_cnt = 0;
3456  map = master->tenant_mapping_list;
3457  while (map) {
3458  if (map_cnt >= map_array_size) {
3459  goto error;
3460  }
3461  map_array[map_cnt].traffic_id = map->traffic_id;
3462  map_array[map_cnt].tenant_id = map->tenant_id;
3463  map_cnt++;
3464  map = map->next;
3465  }
3466 
3467  }
3468 
3469  /* set up hash for tenant lookup */
3470  list = master->list;
3471  while (list) {
3472  SCLogDebug("tenant-id %u", list->tenant_id);
3473  if (list->tenant_id != 0) {
3475  if (mt_det_ctx == NULL)
3476  goto error;
3477  if (HashTableAdd(mt_det_ctxs_hash, mt_det_ctx, 0) != 0) {
3478  goto error;
3479  }
3480  }
3481  list = list->next;
3482  }
3483  }
3484 
3485  det_ctx->mt_det_ctxs_hash = mt_det_ctxs_hash;
3486  mt_det_ctxs_hash = NULL;
3487 
3488  det_ctx->mt_det_ctxs_cnt = max_tenant_id;
3489 
3490  det_ctx->tenant_array = map_array;
3491  det_ctx->tenant_array_size = map_array_size;
3492 
3493  switch (master->tenant_selector) {
3495  SCLogDebug("TENANT_SELECTOR_UNKNOWN");
3496  break;
3497  case TENANT_SELECTOR_VLAN:
3498  det_ctx->TenantGetId = DetectEngineTenantGetIdFromVlanId;
3499  SCLogDebug("TENANT_SELECTOR_VLAN");
3500  break;
3502  det_ctx->TenantGetId = DetectEngineTenantGetIdFromLivedev;
3503  SCLogDebug("TENANT_SELECTOR_LIVEDEV");
3504  break;
3506  det_ctx->TenantGetId = DetectEngineTenantGetIdFromPcap;
3507  SCLogDebug("TENANT_SELECTOR_DIRECT");
3508  break;
3509  }
3510 
3511  return TM_ECODE_OK;
3512 error:
3513  if (map_array != NULL)
3514  SCFree(map_array);
3515  if (mt_det_ctxs_hash != NULL)
3516  HashTableFree(mt_det_ctxs_hash);
3517 
3518  return TM_ECODE_FAILED;
3519 }
3520 
3521 /** \internal
3522  * \brief Helper for DetectThread setup functions
3523  */
3524 static TmEcode ThreadCtxDoInit (DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx)
3525 {
3526  PatternMatchThreadPrepare(&det_ctx->mtc, de_ctx);
3527 
3528  PmqSetup(&det_ctx->pmq);
3529 
3531  if (det_ctx->spm_thread_ctx == NULL) {
3532  return TM_ECODE_FAILED;
3533  }
3534 
3535  /* DeState */
3536  if (de_ctx->sig_array_len > 0) {
3537  det_ctx->match_array_len = de_ctx->sig_array_len;
3538  det_ctx->match_array = SCCalloc(det_ctx->match_array_len, sizeof(Signature *));
3539  if (det_ctx->match_array == NULL) {
3540  return TM_ECODE_FAILED;
3541  }
3542  det_ctx->replace = SCCalloc(de_ctx->sig_array_len, sizeof(Signature *));
3543  if (det_ctx->replace == NULL) {
3544  return TM_ECODE_FAILED;
3545  }
3546 
3548  }
3549 
3550  /* Alert processing queue */
3551  AlertQueueInit(det_ctx);
3552 
3553  /* byte_extract storage */
3554  det_ctx->byte_values = SCMalloc(sizeof(*det_ctx->byte_values) *
3556  if (det_ctx->byte_values == NULL) {
3557  return TM_ECODE_FAILED;
3558  }
3559 
3560  /* Allocate space for base64 decoded data. */
3563  if (det_ctx->base64_decoded == NULL) {
3564  return TM_ECODE_FAILED;
3565  }
3566  det_ctx->base64_decoded_len = 0;
3567  }
3568 
3570  det_ctx->inspect.buffers = SCCalloc(det_ctx->inspect.buffers_size, sizeof(InspectionBuffer));
3571  if (det_ctx->inspect.buffers == NULL) {
3572  return TM_ECODE_FAILED;
3573  }
3574  det_ctx->inspect.to_clear_queue = SCCalloc(det_ctx->inspect.buffers_size, sizeof(uint32_t));
3575  if (det_ctx->inspect.to_clear_queue == NULL) {
3576  return TM_ECODE_FAILED;
3577  }
3578  det_ctx->inspect.to_clear_idx = 0;
3579 
3582  if (det_ctx->multi_inspect.buffers == NULL) {
3583  return TM_ECODE_FAILED;
3584  }
3585  det_ctx->multi_inspect.to_clear_queue = SCCalloc(det_ctx->multi_inspect.buffers_size, sizeof(uint32_t));
3586  if (det_ctx->multi_inspect.to_clear_queue == NULL) {
3587  return TM_ECODE_FAILED;
3588  }
3589  det_ctx->multi_inspect.to_clear_idx = 0;
3590 
3591  if (DetectEngineThreadCtxInitKeywords(de_ctx, det_ctx) != TM_ECODE_OK)
3592  return TM_ECODE_FAILED;
3593  DetectEngineThreadCtxInitGlobalKeywords(det_ctx);
3594 #ifdef PROFILE_RULES
3595  SCProfilingRuleThreadSetup(de_ctx->profile_ctx, det_ctx);
3596 #endif
3597 #ifdef PROFILING
3601 #endif
3602  SC_ATOMIC_INIT(det_ctx->so_far_used_by_detect);
3603 
3604  if (ThresholdCacheThreadInit(det_ctx) != 0)
3605  return TM_ECODE_FAILED;
3606  return TM_ECODE_OK;
3607 }
3608 
3609 /** \brief initialize thread specific detection engine context
3610  *
3611  * \note there is a special case when using delayed detect. In this case the
3612  * function is called twice per thread. The first time the rules are not
3613  * yet loaded. de_ctx->delayed_detect_initialized will be 0. The 2nd
3614  * time they will be loaded. de_ctx->delayed_detect_initialized will be 1.
3615  * This is needed to do the per thread counter registration before the
3616  * packet runtime starts. In delayed detect mode, the first call will
3617  * return a NULL ptr through the data ptr.
3618  *
3619  * \param tv ThreadVars for this thread
3620  * \param initdata pointer to de_ctx
3621  * \param data[out] pointer to store our thread detection ctx
3622  *
3623  * \retval TM_ECODE_OK if all went well
3624  * \retval TM_ECODE_FAILED on serious errors
3625  */
3626 TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
3627 {
3628  DetectEngineThreadCtx *det_ctx = SCCalloc(1, sizeof(DetectEngineThreadCtx));
3629  if (unlikely(det_ctx == NULL))
3630  return TM_ECODE_FAILED;
3631 
3632  det_ctx->tv = tv;
3633  det_ctx->de_ctx = DetectEngineGetCurrent();
3634  if (det_ctx->de_ctx == NULL) {
3635 #ifdef UNITTESTS
3636  if (RunmodeIsUnittests()) {
3637  det_ctx->de_ctx = (DetectEngineCtx *)initdata;
3638  } else {
3639  DetectEngineThreadCtxDeinit(tv, det_ctx);
3640  return TM_ECODE_FAILED;
3641  }
3642 #else
3643  DetectEngineThreadCtxDeinit(tv, det_ctx);
3644  return TM_ECODE_FAILED;
3645 #endif
3646  }
3647 
3648  if (det_ctx->de_ctx->type == DETECT_ENGINE_TYPE_NORMAL ||
3649  det_ctx->de_ctx->type == DETECT_ENGINE_TYPE_TENANT)
3650  {
3651  if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
3652  DetectEngineThreadCtxDeinit(tv, det_ctx);
3653  return TM_ECODE_FAILED;
3654  }
3655  }
3656 
3657  /** alert counter setup */
3658  det_ctx->counter_alerts = StatsRegisterCounter("detect.alert", &tv->stats);
3659  det_ctx->counter_alerts_overflow =
3660  StatsRegisterCounter("detect.alert_queue_overflow", &tv->stats);
3661  if (EngineModeIsFirewall()) {
3663  StatsRegisterCounter("firewall.discarded_alerts", &tv->stats);
3664  }
3665  det_ctx->counter_alerts_suppressed =
3666  StatsRegisterCounter("detect.alerts_suppressed", &tv->stats);
3667 
3668  /* Register counter for Lua rule errors. */
3669  det_ctx->lua_rule_errors = StatsRegisterCounter("detect.lua.errors", &tv->stats);
3670 
3671  /* Register a counter for Lua blocked function attempts. */
3672  det_ctx->lua_blocked_function_errors =
3673  StatsRegisterCounter("detect.lua.blocked_function_errors", &tv->stats);
3674 
3675  /* Register a counter for Lua instruction limit errors. */
3676  det_ctx->lua_instruction_limit_errors =
3677  StatsRegisterCounter("detect.lua.instruction_limit_errors", &tv->stats);
3678 
3679  /* Register a counter for Lua memory limit errors. */
3680  det_ctx->lua_memory_limit_errors =
3681  StatsRegisterCounter("detect.lua.memory_limit_errors", &tv->stats);
3682 
3683  det_ctx->json_content = NULL;
3684  det_ctx->json_content_capacity = 0;
3685  det_ctx->json_content_len = 0;
3686 
3687 #ifdef PROFILING
3688  det_ctx->counter_mpm_list = StatsRegisterAvgCounter("detect.mpm_list", &tv->stats);
3689  det_ctx->counter_match_list = StatsRegisterAvgCounter("detect.match_list", &tv->stats);
3690 #endif
3691 
3693  DetectEngineMasterCtx *master = &g_master_de_ctx;
3694  SCMutexLock(&master->lock);
3695  if (DetectEngineThreadCtxInitForMT(tv, det_ctx) != TM_ECODE_OK) {
3696  DetectEngineThreadCtxDeinit(tv, det_ctx);
3697  SCMutexUnlock(&master->lock);
3698  return TM_ECODE_FAILED;
3699  }
3700  SCMutexUnlock(&master->lock);
3701  }
3702 
3703  /* pass thread data back to caller */
3704  *data = (void *)det_ctx;
3705 
3706  return TM_ECODE_OK;
3707 }
3708 
3709 /**
3710  * \internal
3711  * \brief initialize a det_ctx for reload cases
3712  * \param new_de_ctx the new detection engine
3713  * \param mt flag to indicate if MT should be set up for this det_ctx
3714  * this should only be done for the 'root' det_ctx
3715  *
3716  * \retval det_ctx detection engine thread ctx or NULL in case of error
3717  */
3719  ThreadVars *tv, DetectEngineCtx *new_de_ctx, int mt)
3720 {
3721  DetectEngineThreadCtx *det_ctx = SCCalloc(1, sizeof(DetectEngineThreadCtx));
3722  if (unlikely(det_ctx == NULL))
3723  return NULL;
3724 
3725  det_ctx->tenant_id = new_de_ctx->tenant_id;
3726  det_ctx->tv = tv;
3727  det_ctx->de_ctx = DetectEngineReference(new_de_ctx);
3728  if (det_ctx->de_ctx == NULL) {
3729  SCFree(det_ctx);
3730  return NULL;
3731  }
3732 
3733  /* most of the init happens here */
3734  if (det_ctx->de_ctx->type == DETECT_ENGINE_TYPE_NORMAL ||
3735  det_ctx->de_ctx->type == DETECT_ENGINE_TYPE_TENANT)
3736  {
3737  if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
3738  DetectEngineDeReference(&det_ctx->de_ctx);
3739  SCFree(det_ctx);
3740  return NULL;
3741  }
3742  }
3743 
3744  /** alert counter setup */
3745  det_ctx->counter_alerts = StatsRegisterCounter("detect.alert", &tv->stats);
3746  det_ctx->counter_alerts_overflow =
3747  StatsRegisterCounter("detect.alert_queue_overflow", &tv->stats);
3748  if (EngineModeIsFirewall()) {
3750  StatsRegisterCounter("firewall.discarded_alerts", &tv->stats);
3751  }
3752  det_ctx->counter_alerts_suppressed =
3753  StatsRegisterCounter("detect.alerts_suppressed", &tv->stats);
3754 #ifdef PROFILING
3755  det_ctx->counter_mpm_list = StatsRegisterAvgCounter("detect.mpm_list", &tv->stats);
3756  det_ctx->counter_match_list = StatsRegisterAvgCounter("detect.match_list", &tv->stats);
3757 #endif
3758 
3759  if (mt && DetectEngineMultiTenantEnabledWithLock()) {
3760  if (DetectEngineThreadCtxInitForMT(tv, det_ctx) != TM_ECODE_OK) {
3761  DetectEngineDeReference(&det_ctx->de_ctx);
3762  SCFree(det_ctx);
3763  return NULL;
3764  }
3765  }
3766 
3767  return det_ctx;
3768 }
3769 
3770 static void DetectEngineThreadCtxFree(DetectEngineThreadCtx *det_ctx)
3771 {
3772 #if DEBUG
3773  SCLogDebug("PACKET PKT_STREAM_ADD: %"PRIu64, det_ctx->pkt_stream_add_cnt);
3774 
3775  SCLogDebug("PAYLOAD MPM %"PRIu64"/%"PRIu64, det_ctx->payload_mpm_cnt, det_ctx->payload_mpm_size);
3776  SCLogDebug("STREAM MPM %"PRIu64"/%"PRIu64, det_ctx->stream_mpm_cnt, det_ctx->stream_mpm_size);
3777 
3778  SCLogDebug("PAYLOAD SIG %"PRIu64"/%"PRIu64, det_ctx->payload_persig_cnt, det_ctx->payload_persig_size);
3779  SCLogDebug("STREAM SIG %"PRIu64"/%"PRIu64, det_ctx->stream_persig_cnt, det_ctx->stream_persig_size);
3780 #endif
3781 
3782  if (det_ctx->tenant_array != NULL) {
3783  SCFree(det_ctx->tenant_array);
3784  det_ctx->tenant_array = NULL;
3785  }
3786 
3787 #ifdef PROFILE_RULES
3788  SCProfilingRuleThreadCleanup(det_ctx);
3789 #endif
3790 #ifdef PROFILING
3793  SCProfilingSghThreadCleanup(det_ctx);
3794 #endif
3795 
3796  /** \todo get rid of this static */
3797  if (det_ctx->de_ctx != NULL) {
3798  PatternMatchThreadDestroy(&det_ctx->mtc, det_ctx->de_ctx->mpm_matcher);
3799  }
3800 
3801  PmqFree(&det_ctx->pmq);
3802 
3803  if (det_ctx->spm_thread_ctx != NULL) {
3805  }
3806  if (det_ctx->match_array != NULL)
3807  SCFree(det_ctx->match_array);
3808  if (det_ctx->replace != NULL)
3809  SCFree(det_ctx->replace);
3810 
3812 
3813  AlertQueueFree(det_ctx);
3814 
3815  if (det_ctx->post_rule_work_queue.q)
3816  SCFree(det_ctx->post_rule_work_queue.q);
3817 
3818  if (det_ctx->byte_values != NULL)
3819  SCFree(det_ctx->byte_values);
3820 
3821  /* Decoded base64 data. */
3822  if (det_ctx->base64_decoded != NULL) {
3823  SCFree(det_ctx->base64_decoded);
3824  }
3825 
3826  if (det_ctx->inspect.buffers) {
3827  for (uint32_t i = 0; i < det_ctx->inspect.buffers_size; i++) {
3828  InspectionBufferFree(&det_ctx->inspect.buffers[i]);
3829  }
3830  SCFree(det_ctx->inspect.buffers);
3831  }
3832  if (det_ctx->inspect.to_clear_queue) {
3833  SCFree(det_ctx->inspect.to_clear_queue);
3834  }
3835  if (det_ctx->multi_inspect.buffers) {
3836  for (uint32_t i = 0; i < det_ctx->multi_inspect.buffers_size; i++) {
3838  for (uint32_t x = 0; x < fb->size; x++) {
3840  }
3842  }
3843  SCFree(det_ctx->multi_inspect.buffers);
3844  }
3845  if (det_ctx->multi_inspect.to_clear_queue) {
3847  }
3848 
3849  DetectEngineThreadCtxDeinitGlobalKeywords(det_ctx);
3850  if (det_ctx->de_ctx != NULL) {
3851  DetectEngineThreadCtxDeinitKeywords(det_ctx->de_ctx, det_ctx);
3852 #ifdef UNITTESTS
3853  if (!RunmodeIsUnittests() || det_ctx->de_ctx->ref_cnt > 0)
3854  DetectEngineDeReference(&det_ctx->de_ctx);
3855 #else
3856  DetectEngineDeReference(&det_ctx->de_ctx);
3857 #endif
3858  }
3859 
3860  if (det_ctx->json_content) {
3861  SCFree(det_ctx->json_content);
3862  det_ctx->json_content = NULL;
3863  det_ctx->json_content_capacity = 0;
3864  }
3865 
3868  SCFree(det_ctx);
3869 }
3870 
3872 {
3873  DetectEngineThreadCtx *det_ctx = (DetectEngineThreadCtx *)data;
3874 
3875  if (det_ctx == NULL) {
3876  SCLogWarning("argument \"data\" NULL");
3877  return TM_ECODE_OK;
3878  }
3879 
3880  if (det_ctx->mt_det_ctxs_hash != NULL) {
3881  HashTableFree(det_ctx->mt_det_ctxs_hash);
3882  det_ctx->mt_det_ctxs_hash = NULL;
3883  }
3884  DetectEngineThreadCtxFree(det_ctx);
3885 
3886  return TM_ECODE_OK;
3887 }
3888 
3889 static uint32_t DetectKeywordCtxHashFunc(HashListTable *ht, void *data, uint16_t datalen)
3890 {
3892  const char *name = ctx->name;
3893  uint64_t hash =
3894  StringHashDjb2((const uint8_t *)name, (uint32_t)strlen(name)) + (ptrdiff_t)ctx->data;
3895  hash %= ht->array_size;
3896  return (uint32_t)hash;
3897 }
3898 
3899 static char DetectKeywordCtxCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
3900 {
3901  DetectEngineThreadKeywordCtxItem *ctx1 = data1;
3902  DetectEngineThreadKeywordCtxItem *ctx2 = data2;
3903  const char *name1 = ctx1->name;
3904  const char *name2 = ctx2->name;
3905  return (strcmp(name1, name2) == 0 && ctx1->data == ctx2->data);
3906 }
3907 
3908 static void DetectKeywordCtxFreeFunc(void *ptr)
3909 {
3910  SCFree(ptr);
3911 }
3912 
3913 /** \brief Register Thread keyword context Funcs
3914  *
3915  * \param de_ctx detection engine to register in
3916  * \param name keyword name for error printing
3917  * \param InitFunc function ptr
3918  * \param data keyword init data to pass to Func. Can be NULL.
3919  * \param FreeFunc function ptr
3920  * \param mode 0 normal (ctx per keyword instance) 1 shared (one ctx per det_ct)
3921  *
3922  * \retval id for retrieval of ctx at runtime
3923  * \retval -1 on error
3924  *
3925  * \note make sure "data" remains valid and it free'd elsewhere. It's
3926  * recommended to store it in the keywords global ctx so that
3927  * it's freed when the de_ctx is freed.
3928  */
3929 int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void (*FreeFunc)(void *), int mode)
3930 {
3931  BUG_ON(de_ctx == NULL || InitFunc == NULL || FreeFunc == NULL);
3932 
3933  if (de_ctx->keyword_hash == NULL) {
3934  de_ctx->keyword_hash = HashListTableInit(4096, // TODO
3935  DetectKeywordCtxHashFunc, DetectKeywordCtxCompareFunc, DetectKeywordCtxFreeFunc);
3936  BUG_ON(de_ctx->keyword_hash == NULL);
3937  }
3938 
3939  if (mode) {
3940  DetectEngineThreadKeywordCtxItem search = { .data = data, .name = name };
3941 
3943  HashListTableLookup(de_ctx->keyword_hash, (void *)&search, 0);
3944  if (item)
3945  return item->id;
3946 
3947  /* fall through */
3948  }
3949 
3951  if (unlikely(item == NULL))
3952  return -1;
3953 
3954  item->InitFunc = InitFunc;
3955  item->FreeFunc = FreeFunc;
3956  item->data = data;
3957  item->name = name;
3958  item->id = de_ctx->keyword_id++;
3959 
3960  if (HashListTableAdd(de_ctx->keyword_hash, (void *)item, 0) < 0) {
3961  SCFree(item);
3962  return -1;
3963  }
3964  return item->id;
3965 }
3966 
3967 /** \brief Remove Thread keyword context registration
3968  *
3969  * \param de_ctx detection engine to deregister from
3970  * \param det_ctx detection engine thread context to deregister from
3971  * \param data keyword init data to pass to Func. Can be NULL.
3972  * \param name keyword name for error printing
3973  *
3974  * \retval 1 Item unregistered
3975  * \retval 0 otherwise
3976  *
3977  * \note make sure "data" remains valid and it free'd elsewhere. It's
3978  * recommended to store it in the keywords global ctx so that
3979  * it's freed when the de_ctx is freed.
3980  */
3982 {
3983  /* might happen if we call this before a call to *Register* */
3984  if (de_ctx->keyword_hash == NULL)
3985  return 1;
3986  DetectEngineThreadKeywordCtxItem remove = { .data = data, .name = name };
3987  if (HashListTableRemove(de_ctx->keyword_hash, (void *)&remove, 0) == 0)
3988  return 1;
3989  return 0;
3990 }
3991 /** \brief Retrieve thread local keyword ctx by id
3992  *
3993  * \param det_ctx detection engine thread ctx to retrieve the ctx from
3994  * \param id id of the ctx returned by DetectRegisterThreadCtxInitFunc at
3995  * keyword init.
3996  *
3997  * \retval ctx or NULL on error
3998  */
4000 {
4001  if (id < 0 || id > det_ctx->keyword_ctxs_size || det_ctx->keyword_ctxs_array == NULL)
4002  return NULL;
4003 
4004  return det_ctx->keyword_ctxs_array[id];
4005 }
4006 
4007 
4008 /** \brief Register Thread keyword context Funcs (Global)
4009  *
4010  * IDs stay static over reloads and between tenants
4011  *
4012  * \param name keyword name for error printing
4013  * \param InitFunc function ptr
4014  * \param FreeFunc function ptr
4015  *
4016  * \retval id for retrieval of ctx at runtime
4017  * \retval -1 on error
4018  */
4020  const char *name, void *(*InitFunc)(void *), void *data, void (*FreeFunc)(void *))
4021 {
4022  int id;
4023  BUG_ON(InitFunc == NULL || FreeFunc == NULL);
4024 
4025  DetectEngineMasterCtx *master = &g_master_de_ctx;
4026 
4027  /* if already registered, return existing id */
4029  while (item != NULL) {
4030  if (strcmp(name, item->name) == 0) {
4031  id = item->id;
4032  return id;
4033  }
4034 
4035  item = item->next;
4036  }
4037 
4038  item = SCCalloc(1, sizeof(*item));
4039  if (unlikely(item == NULL)) {
4040  return -1;
4041  }
4042  item->InitFunc = InitFunc;
4043  item->FreeFunc = FreeFunc;
4044  item->name = name;
4045  item->data = data;
4046 
4047  item->next = master->keyword_list;
4048  master->keyword_list = item;
4049  item->id = master->keyword_id++;
4050 
4051  id = item->id;
4052  return id;
4053 }
4054 
4055 /** \brief Retrieve thread local keyword ctx by id
4056  *
4057  * \param det_ctx detection engine thread ctx to retrieve the ctx from
4058  * \param id id of the ctx returned by DetectRegisterThreadCtxInitFunc at
4059  * keyword init.
4060  *
4061  * \retval ctx or NULL on error
4062  */
4064 {
4065  if (id < 0 || id > det_ctx->global_keyword_ctxs_size ||
4066  det_ctx->global_keyword_ctxs_array == NULL) {
4067  return NULL;
4068  }
4069 
4070  return det_ctx->global_keyword_ctxs_array[id];
4071 }
4072 
4073 /** \brief Check if detection is enabled
4074  * \retval bool true or false */
4076 {
4077  DetectEngineMasterCtx *master = &g_master_de_ctx;
4078  SCMutexLock(&master->lock);
4079 
4080  if (master->list == NULL) {
4081  SCMutexUnlock(&master->lock);
4082  return 0;
4083  }
4084 
4085  SCMutexUnlock(&master->lock);
4086  return 1;
4087 }
4088 
4090 {
4091  uint32_t version;
4092  DetectEngineMasterCtx *master = &g_master_de_ctx;
4093  SCMutexLock(&master->lock);
4094  version = master->version;
4095  SCMutexUnlock(&master->lock);
4096  return version;
4097 }
4098 
4100 {
4101  DetectEngineMasterCtx *master = &g_master_de_ctx;
4102  SCMutexLock(&master->lock);
4103  master->version++;
4104  SCLogDebug("master version now %u", master->version);
4105  SCMutexUnlock(&master->lock);
4106 }
4107 
4109 {
4110  DetectEngineMasterCtx *master = &g_master_de_ctx;
4111  SCMutexLock(&master->lock);
4112 
4113  DetectEngineCtx *de_ctx = master->list;
4114  while (de_ctx) {
4118  {
4119  de_ctx->ref_cnt++;
4120  SCLogDebug("de_ctx %p ref_cnt %u", de_ctx, de_ctx->ref_cnt);
4121  SCMutexUnlock(&master->lock);
4122  return de_ctx;
4123  }
4124  de_ctx = de_ctx->next;
4125  }
4126 
4127  SCMutexUnlock(&master->lock);
4128  return NULL;
4129 }
4130 
4132 {
4133  if (de_ctx == NULL)
4134  return NULL;
4135  de_ctx->ref_cnt++;
4136  return de_ctx;
4137 }
4138 
4139 static bool DetectEngineMultiTenantEnabledWithLock(void)
4140 {
4141  DetectEngineMasterCtx *master = &g_master_de_ctx;
4142  return master->multi_tenant_enabled;
4143 }
4144 
4146 {
4147  DetectEngineMasterCtx *master = &g_master_de_ctx;
4148  SCMutexLock(&master->lock);
4149  bool enabled = DetectEngineMultiTenantEnabledWithLock();
4150  SCMutexUnlock(&master->lock);
4151  return enabled;
4152 }
4153 
4154 /** \internal
4155  * \brief load a tenant from a yaml file
4156  *
4157  * \param tenant_id the tenant id by which the config is known
4158  * \param filename full path of a yaml file
4159  * \param loader_id id of loader thread or -1
4160  *
4161  * \retval 0 ok
4162  * \retval -1 failed
4163  */
4164 static int DetectEngineMultiTenantLoadTenant(uint32_t tenant_id, const char *filename, int loader_id)
4165 {
4166  DetectEngineCtx *de_ctx = NULL;
4167  char prefix[64];
4168 
4169  snprintf(prefix, sizeof(prefix), "multi-detect.%u", tenant_id);
4170 
4171  SCStat st;
4172  if (SCStatFn(filename, &st) != 0) {
4173  SCLogError("failed to stat file %s", filename);
4174  goto error;
4175  }
4176 
4177  de_ctx = DetectEngineGetByTenantId(tenant_id);
4178  if (de_ctx != NULL) {
4179  SCLogError("tenant %u already registered", tenant_id);
4181  goto error;
4182  }
4183 
4184  SCConfNode *node = SCConfGetNode(prefix);
4185  if (node == NULL) {
4186  SCLogError("failed to properly setup yaml %s", filename);
4187  goto error;
4188  }
4189 
4190  de_ctx = DetectEngineCtxInitWithPrefix(prefix, tenant_id);
4191  if (de_ctx == NULL) {
4192  SCLogError("initializing detection engine "
4193  "context failed.");
4194  goto error;
4195  }
4196  SCLogDebug("de_ctx %p with prefix %s", de_ctx, de_ctx->config_prefix);
4197 
4199  de_ctx->tenant_id = tenant_id;
4200  de_ctx->loader_id = loader_id;
4201  de_ctx->tenant_path = SCStrdup(filename);
4202  if (de_ctx->tenant_path == NULL) {
4203  SCLogError("Failed to duplicate path");
4204  goto error;
4205  }
4206 
4207  if (SigLoadSignatures(de_ctx, NULL, false) < 0) {
4208  SCLogError("Loading signatures failed.");
4209  goto error;
4210  }
4212 
4213  return 0;
4214 
4215 error:
4216  if (de_ctx != NULL) {
4218  }
4219  return -1;
4220 }
4221 
4222 static int DetectEngineMultiTenantReloadTenant(uint32_t tenant_id, const char *filename, int reload_cnt)
4223 {
4224  DetectEngineCtx *old_de_ctx = DetectEngineGetByTenantId(tenant_id);
4225  if (old_de_ctx == NULL) {
4226  SCLogError("tenant detect engine not found");
4227  return -1;
4228  }
4229 
4230  if (filename == NULL)
4231  filename = old_de_ctx->tenant_path;
4232 
4233  char prefix[64];
4234  snprintf(prefix, sizeof(prefix), "multi-detect.%u.reload.%d", tenant_id, reload_cnt);
4235  reload_cnt++;
4236  SCLogDebug("prefix %s", prefix);
4237 
4238  if (SCConfYamlLoadFileWithPrefix(filename, prefix) != 0) {
4239  SCLogError("failed to load yaml");
4240  goto error;
4241  }
4242 
4243  SCConfNode *node = SCConfGetNode(prefix);
4244  if (node == NULL) {
4245  SCLogError("failed to properly setup yaml %s", filename);
4246  goto error;
4247  }
4248 
4249  DetectEngineCtx *new_de_ctx = DetectEngineCtxInitWithPrefix(prefix, tenant_id);
4250  if (new_de_ctx == NULL) {
4251  SCLogError("initializing detection engine "
4252  "context failed.");
4253  goto error;
4254  }
4255  SCLogDebug("de_ctx %p with prefix %s", new_de_ctx, new_de_ctx->config_prefix);
4256 
4257  new_de_ctx->type = DETECT_ENGINE_TYPE_TENANT;
4258  new_de_ctx->tenant_id = tenant_id;
4259  new_de_ctx->loader_id = old_de_ctx->loader_id;
4260  new_de_ctx->tenant_path = SCStrdup(filename);
4261  if (new_de_ctx->tenant_path == NULL) {
4262  SCLogError("Failed to duplicate path");
4263  goto new_de_ctx_error;
4264  }
4265 
4266  if (SigLoadSignatures(new_de_ctx, NULL, false) < 0) {
4267  SCLogError("Loading signatures failed.");
4268  goto new_de_ctx_error;
4269  }
4270 
4271  DetectEngineAddToMaster(new_de_ctx);
4272 
4273  /* move to free list */
4274  DetectEngineMoveToFreeList(old_de_ctx);
4275  DetectEngineDeReference(&old_de_ctx);
4276  return 0;
4277 
4278 new_de_ctx_error:
4279  DetectEngineCtxFree(new_de_ctx);
4280 
4281 error:
4282  DetectEngineDeReference(&old_de_ctx);
4283  return -1;
4284 }
4285 
4286 
4287 typedef struct TenantLoaderCtx_ {
4288  uint32_t tenant_id;
4289  int reload_cnt; /**< used by reload */
4290  char *yaml; /**< heap alloc'd copy of file path for the yaml */
4292 
4293 static void DetectLoaderFreeTenant(void *ctx)
4294 {
4296  if (t->yaml != NULL) {
4297  SCFree(t->yaml);
4298  }
4299  SCFree(t);
4300 }
4301 
4302 static int DetectLoaderFuncLoadTenant(void *vctx, int loader_id)
4303 {
4304  TenantLoaderCtx *ctx = (TenantLoaderCtx *)vctx;
4305 
4306  SCLogDebug("loader %d", loader_id);
4307  if (DetectEngineMultiTenantLoadTenant(ctx->tenant_id, ctx->yaml, loader_id) != 0) {
4308  return -1;
4309  }
4310  return 0;
4311 }
4312 
4313 static int DetectLoaderSetupLoadTenant(uint32_t tenant_id, const char *yaml)
4314 {
4315  TenantLoaderCtx *t = SCCalloc(1, sizeof(*t));
4316  if (t == NULL)
4317  return -ENOMEM;
4318 
4319  t->tenant_id = tenant_id;
4320  t->yaml = SCStrdup(yaml);
4321  if (t->yaml == NULL) {
4322  SCFree(t);
4323  return -ENOMEM;
4324  }
4325 
4326  return DetectLoaderQueueTask(-1, DetectLoaderFuncLoadTenant, t, DetectLoaderFreeTenant);
4327 }
4328 
4329 static int DetectLoaderFuncReloadTenant(void *vctx, int loader_id)
4330 {
4331  TenantLoaderCtx *ctx = (TenantLoaderCtx *)vctx;
4332 
4333  SCLogDebug("loader_id %d", loader_id);
4334 
4335  if (DetectEngineMultiTenantReloadTenant(ctx->tenant_id, ctx->yaml, ctx->reload_cnt) != 0) {
4336  return -1;
4337  }
4338  return 0;
4339 }
4340 
4341 static int DetectLoaderSetupReloadTenants(const int reload_cnt)
4342 {
4343  int ret = 0;
4344  DetectEngineMasterCtx *master = &g_master_de_ctx;
4345  SCMutexLock(&master->lock);
4346 
4347  DetectEngineCtx *de_ctx = master->list;
4348  while (de_ctx) {
4350  TenantLoaderCtx *t = SCCalloc(1, sizeof(*t));
4351  if (t == NULL) {
4352  ret = -1;
4353  goto error;
4354  }
4355  t->tenant_id = de_ctx->tenant_id;
4356  t->reload_cnt = reload_cnt;
4357  int loader_id = de_ctx->loader_id;
4358 
4359  int r = DetectLoaderQueueTask(
4360  loader_id, DetectLoaderFuncReloadTenant, t, DetectLoaderFreeTenant);
4361  if (r < 0) {
4362  ret = -2;
4363  goto error;
4364  }
4365  }
4366 
4367  de_ctx = de_ctx->next;
4368  }
4369 error:
4370  SCMutexUnlock(&master->lock);
4371  return ret;
4372 }
4373 
4374 static int DetectLoaderSetupReloadTenant(uint32_t tenant_id, const char *yaml, int reload_cnt)
4375 {
4376  DetectEngineCtx *old_de_ctx = DetectEngineGetByTenantId(tenant_id);
4377  if (old_de_ctx == NULL)
4378  return -ENOENT;
4379  int loader_id = old_de_ctx->loader_id;
4380  DetectEngineDeReference(&old_de_ctx);
4381 
4382  TenantLoaderCtx *t = SCCalloc(1, sizeof(*t));
4383  if (t == NULL)
4384  return -ENOMEM;
4385 
4386  t->tenant_id = tenant_id;
4387  if (yaml != NULL) {
4388  t->yaml = SCStrdup(yaml);
4389  if (t->yaml == NULL) {
4390  SCFree(t);
4391  return -ENOMEM;
4392  }
4393  }
4394  t->reload_cnt = reload_cnt;
4395 
4396  SCLogDebug("loader_id %d", loader_id);
4397 
4398  return DetectLoaderQueueTask(
4399  loader_id, DetectLoaderFuncReloadTenant, t, DetectLoaderFreeTenant);
4400 }
4401 
4402 /** \brief Load a tenant and wait for loading to complete
4403  */
4404 int DetectEngineLoadTenantBlocking(uint32_t tenant_id, const char *yaml)
4405 {
4406  int r = DetectLoaderSetupLoadTenant(tenant_id, yaml);
4407  if (r < 0)
4408  return r;
4409 
4410  if (DetectLoadersSync() != 0)
4411  return -1;
4412 
4413  return 0;
4414 }
4415 
4416 /** \brief Reload a tenant and wait for loading to complete
4417  */
4418 int DetectEngineReloadTenantBlocking(uint32_t tenant_id, const char *yaml, int reload_cnt)
4419 {
4420  int r = DetectLoaderSetupReloadTenant(tenant_id, yaml, reload_cnt);
4421  if (r < 0)
4422  return r;
4423 
4424  if (DetectLoadersSync() != 0)
4425  return -1;
4426 
4427  return 0;
4428 }
4429 
4430 /** \brief Reload all tenants and wait for loading to complete
4431  */
4432 int DetectEngineReloadTenantsBlocking(const int reload_cnt)
4433 {
4434  int r = DetectLoaderSetupReloadTenants(reload_cnt);
4435  if (r < 0)
4436  return r;
4437 
4438  if (DetectLoadersSync() != 0)
4439  return -1;
4440 
4441  return 0;
4442 }
4443 
4444 static int DetectEngineMultiTenantSetupLoadLivedevMappings(
4445  const SCConfNode *mappings_root_node, bool failure_fatal)
4446 {
4447  SCConfNode *mapping_node = NULL;
4448 
4449  int mapping_cnt = 0;
4450  if (mappings_root_node != NULL) {
4451  TAILQ_FOREACH(mapping_node, &mappings_root_node->head, next) {
4452  SCConfNode *tenant_id_node = SCConfNodeLookupChild(mapping_node, "tenant-id");
4453  if (tenant_id_node == NULL)
4454  goto bad_mapping;
4455  SCConfNode *device_node = SCConfNodeLookupChild(mapping_node, "device");
4456  if (device_node == NULL)
4457  goto bad_mapping;
4458 
4459  uint32_t tenant_id = 0;
4460  if (StringParseUint32(&tenant_id, 10, (uint16_t)strlen(tenant_id_node->val),
4461  tenant_id_node->val) < 0) {
4462  SCLogError("tenant-id "
4463  "of %s is invalid",
4464  tenant_id_node->val);
4465  goto bad_mapping;
4466  }
4467 
4468  const char *dev = device_node->val;
4469  LiveDevice *ld = LiveGetDevice(dev);
4470  if (ld == NULL) {
4471  SCLogWarning("device %s not found", dev);
4472  goto bad_mapping;
4473  }
4474 
4475  if (ld->tenant_id_set) {
4476  SCLogWarning("device %s already mapped to tenant-id %u", dev, ld->tenant_id);
4477  goto bad_mapping;
4478  }
4479 
4480  ld->tenant_id = tenant_id;
4481  ld->tenant_id_set = true;
4482 
4483  if (DetectEngineTenantRegisterLivedev(tenant_id, ld->id) != 0) {
4484  goto error;
4485  }
4486 
4487  SCLogConfig("device %s connected to tenant-id %u", dev, tenant_id);
4488  mapping_cnt++;
4489  continue;
4490 
4491  bad_mapping:
4492  if (failure_fatal)
4493  goto error;
4494  }
4495  }
4496  SCLogConfig("%d device - tenant-id mappings defined", mapping_cnt);
4497  return mapping_cnt;
4498 
4499 error:
4500  return 0;
4501 }
4502 
4503 static int DetectEngineMultiTenantSetupLoadVlanMappings(
4504  const SCConfNode *mappings_root_node, bool failure_fatal)
4505 {
4506  SCConfNode *mapping_node = NULL;
4507 
4508  int mapping_cnt = 0;
4509  if (mappings_root_node != NULL) {
4510  TAILQ_FOREACH(mapping_node, &mappings_root_node->head, next) {
4511  SCConfNode *tenant_id_node = SCConfNodeLookupChild(mapping_node, "tenant-id");
4512  if (tenant_id_node == NULL)
4513  goto bad_mapping;
4514  SCConfNode *vlan_id_node = SCConfNodeLookupChild(mapping_node, "vlan-id");
4515  if (vlan_id_node == NULL)
4516  goto bad_mapping;
4517 
4518  uint32_t tenant_id = 0;
4519  if (StringParseUint32(&tenant_id, 10, (uint16_t)strlen(tenant_id_node->val),
4520  tenant_id_node->val) < 0) {
4521  SCLogError("tenant-id "
4522  "of %s is invalid",
4523  tenant_id_node->val);
4524  goto bad_mapping;
4525  }
4526 
4527  uint16_t vlan_id = 0;
4528  if (StringParseUint16(
4529  &vlan_id, 10, (uint16_t)strlen(vlan_id_node->val), vlan_id_node->val) < 0) {
4530  SCLogError("vlan-id "
4531  "of %s is invalid",
4532  vlan_id_node->val);
4533  goto bad_mapping;
4534  }
4535  if (vlan_id == 0 || vlan_id >= 4095) {
4536  SCLogError("vlan-id "
4537  "of %s is invalid. Valid range 1-4094.",
4538  vlan_id_node->val);
4539  goto bad_mapping;
4540  }
4541 
4542  if (DetectEngineTenantRegisterVlanId(tenant_id, vlan_id) != 0) {
4543  goto error;
4544  }
4545  SCLogConfig("vlan %u connected to tenant-id %u", vlan_id, tenant_id);
4546  mapping_cnt++;
4547  continue;
4548 
4549  bad_mapping:
4550  if (failure_fatal)
4551  goto error;
4552  }
4553  }
4554  return mapping_cnt;
4555 
4556 error:
4557  return 0;
4558 }
4559 
4560 /**
4561  * \brief setup multi-detect / multi-tenancy
4562  *
4563  * See if MT is enabled. If so, setup the selector, tenants and mappings.
4564  * Tenants and mappings are optional, and can also dynamically be added
4565  * and removed from the unix socket.
4566  */
4567 int DetectEngineMultiTenantSetup(const bool unix_socket)
4568 {
4570  DetectEngineMasterCtx *master = &g_master_de_ctx;
4571  int failure_fatal = 0;
4572  (void)SCConfGetBool("engine.init-failure-fatal", &failure_fatal);
4573 
4574  int enabled = 0;
4575  (void)SCConfGetBool("multi-detect.enabled", &enabled);
4576  if (enabled == 1) {
4581 
4582  SCMutexLock(&master->lock);
4583  master->multi_tenant_enabled = 1;
4584 
4585  const char *handler = NULL;
4586  if (SCConfGetNonNull("multi-detect.selector", &handler) == 1) {
4587  SCLogConfig("multi-tenant selector type %s", handler);
4588 
4589  if (strcmp(handler, "vlan") == 0) {
4590  tenant_selector = master->tenant_selector = TENANT_SELECTOR_VLAN;
4591 
4592  int vlanbool = 0;
4593  if ((SCConfGetBool("vlan.use-for-tracking", &vlanbool)) == 1 && vlanbool == 0) {
4594  SCLogError("vlan tracking is disabled, "
4595  "can't use multi-detect selector 'vlan'");
4596  SCMutexUnlock(&master->lock);
4597  goto error;
4598  }
4599 
4600  } else if (strcmp(handler, "direct") == 0) {
4601  tenant_selector = master->tenant_selector = TENANT_SELECTOR_DIRECT;
4602  } else if (strcmp(handler, "device") == 0) {
4603  tenant_selector = master->tenant_selector = TENANT_SELECTOR_LIVEDEV;
4604  if (EngineModeIsIPS()) {
4605  SCLogWarning("multi-tenant 'device' mode not supported for IPS");
4606  SCMutexUnlock(&master->lock);
4607  goto error;
4608  }
4609 
4610  } else {
4611  SCLogError("unknown value %s "
4612  "multi-detect.selector",
4613  handler);
4614  SCMutexUnlock(&master->lock);
4615  goto error;
4616  }
4617  }
4618  SCMutexUnlock(&master->lock);
4619  SCLogConfig("multi-detect is enabled (multi tenancy). Selector: %s", handler);
4620 
4621  /* traffic -- tenant mappings */
4622  SCConfNode *mappings_root_node = SCConfGetNode("multi-detect.mappings");
4623 
4624  if (tenant_selector == TENANT_SELECTOR_VLAN) {
4625  int mapping_cnt = DetectEngineMultiTenantSetupLoadVlanMappings(mappings_root_node,
4626  failure_fatal);
4627  if (mapping_cnt == 0) {
4628  /* no mappings are valid when we're in unix socket mode,
4629  * they can be added on the fly. Otherwise warn/error
4630  * depending on failure_fatal */
4631 
4632  if (unix_socket) {
4633  SCLogNotice("no tenant traffic mappings defined, "
4634  "tenants won't be used until mappings are added");
4635  } else {
4636  if (failure_fatal) {
4637  SCLogError("no multi-detect mappings defined");
4638  goto error;
4639  } else {
4640  SCLogWarning("no multi-detect mappings defined");
4641  }
4642  }
4643  }
4644  } else if (tenant_selector == TENANT_SELECTOR_LIVEDEV) {
4645  int mapping_cnt = DetectEngineMultiTenantSetupLoadLivedevMappings(mappings_root_node,
4646  failure_fatal);
4647  if (mapping_cnt == 0) {
4648  if (failure_fatal) {
4649  SCLogError("no multi-detect mappings defined");
4650  goto error;
4651  } else {
4652  SCLogWarning("no multi-detect mappings defined");
4653  }
4654  }
4655  }
4656 
4657  /* tenants */
4658  SCConfNode *tenants_root_node = SCConfGetNode("multi-detect.tenants");
4659  SCConfNode *tenant_node = NULL;
4660 
4661  if (tenants_root_node != NULL) {
4662  const char *path = NULL;
4663  SCConfNode *path_node = SCConfGetNode("multi-detect.config-path");
4664  if (path_node) {
4665  path = path_node->val;
4666  SCLogConfig("tenants config path: %s", path);
4667  }
4668 
4669  TAILQ_FOREACH(tenant_node, &tenants_root_node->head, next) {
4670  SCConfNode *id_node = SCConfNodeLookupChild(tenant_node, "id");
4671  if (id_node == NULL) {
4672  goto bad_tenant;
4673  }
4674  SCConfNode *yaml_node = SCConfNodeLookupChild(tenant_node, "yaml");
4675  if (yaml_node == NULL) {
4676  goto bad_tenant;
4677  }
4678 
4679  uint32_t tenant_id = 0;
4680  if (StringParseUint32(
4681  &tenant_id, 10, (uint16_t)strlen(id_node->val), id_node->val) < 0) {
4682  SCLogError("tenant_id "
4683  "of %s is invalid",
4684  id_node->val);
4685  goto bad_tenant;
4686  }
4687  SCLogDebug("tenant id: %u, %s", tenant_id, yaml_node->val);
4688 
4689  char yaml_path[PATH_MAX] = "";
4690  if (path) {
4691  if (PathMerge(yaml_path, PATH_MAX, path, yaml_node->val) < 0)
4692  goto bad_tenant;
4693  } else {
4694  size_t r = strlcpy(yaml_path, yaml_node->val, sizeof(yaml_path));
4695  if (r >= sizeof(yaml_path))
4696  goto bad_tenant;
4697  }
4698  SCLogDebug("tenant path: %s", yaml_path);
4699 
4700  /* setup the yaml in this loop so that it's not done by the loader
4701  * threads. SCConfYamlLoadFileWithPrefix is not thread safe. */
4702  char prefix[64];
4703  snprintf(prefix, sizeof(prefix), "multi-detect.%u", tenant_id);
4704  if (SCConfYamlLoadFileWithPrefix(yaml_path, prefix) != 0) {
4705  SCLogError("failed to load yaml %s", yaml_path);
4706  goto bad_tenant;
4707  }
4708 
4709  int r = DetectLoaderSetupLoadTenant(tenant_id, yaml_path);
4710  if (r < 0) {
4711  /* error logged already */
4712  goto bad_tenant;
4713  }
4714  continue;
4715 
4716  bad_tenant:
4717  if (failure_fatal)
4718  goto error;
4719  }
4720  }
4721 
4722  /* wait for our loaders to complete their tasks */
4723  if (DetectLoadersSync() != 0) {
4724  goto error;
4725  }
4726 
4728 
4729  } else {
4730  SCLogDebug("multi-detect not enabled (multi tenancy)");
4731  }
4732  return 0;
4733 error:
4734  return -1;
4735 }
4736 
4737 static uint32_t DetectEngineTenantGetIdFromVlanId(const void *ctx, const Packet *p)
4738 {
4739  const DetectEngineThreadCtx *det_ctx = ctx;
4740  uint32_t x = 0;
4741  uint32_t vlan_id = 0;
4742 
4743  if (p->vlan_idx == 0)
4744  return 0;
4745 
4746  vlan_id = p->vlan_id[0];
4747 
4748  if (det_ctx == NULL || det_ctx->tenant_array == NULL || det_ctx->tenant_array_size == 0)
4749  return 0;
4750 
4751  /* not very efficient, but for now we're targeting only limited amounts.
4752  * Can use hash/tree approach later. */
4753  for (x = 0; x < det_ctx->tenant_array_size; x++) {
4754  if (det_ctx->tenant_array[x].traffic_id == vlan_id)
4755  return det_ctx->tenant_array[x].tenant_id;
4756  }
4757 
4758  return 0;
4759 }
4760 
4761 static uint32_t DetectEngineTenantGetIdFromLivedev(const void *ctx, const Packet *p)
4762 {
4763  const DetectEngineThreadCtx *det_ctx = ctx;
4764  const LiveDevice *ld = LiveDeviceGetById(p->livedev_id);
4765 
4766  if (ld == NULL || det_ctx == NULL)
4767  return 0;
4768 
4769  SCLogDebug("using tenant-id %u for packet on device %s", ld->tenant_id, ld->dev);
4770  return ld->tenant_id;
4771 }
4772 
4773 static int DetectEngineTenantRegisterSelector(
4774  enum DetectEngineTenantSelectors selector, uint32_t tenant_id, uint32_t traffic_id)
4775 {
4776  DetectEngineMasterCtx *master = &g_master_de_ctx;
4777  SCMutexLock(&master->lock);
4778 
4779  if (!(master->tenant_selector == TENANT_SELECTOR_UNKNOWN || master->tenant_selector == selector)) {
4780  SCLogInfo("conflicting selector already set");
4781  SCMutexUnlock(&master->lock);
4782  return -1;
4783  }
4784 
4786  while (m) {
4787  if (m->traffic_id == traffic_id) {
4788  SCLogInfo("traffic id already registered");
4789  SCMutexUnlock(&master->lock);
4790  return -1;
4791  }
4792  m = m->next;
4793  }
4794 
4795  DetectEngineTenantMapping *map = SCCalloc(1, sizeof(*map));
4796  if (map == NULL) {
4797  SCLogInfo("memory fail");
4798  SCMutexUnlock(&master->lock);
4799  return -1;
4800  }
4801  map->traffic_id = traffic_id;
4802  map->tenant_id = tenant_id;
4803 
4804  map->next = master->tenant_mapping_list;
4805  master->tenant_mapping_list = map;
4806 
4807  master->tenant_selector = selector;
4808 
4809  SCLogDebug("tenant handler %u %u %u registered", selector, tenant_id, traffic_id);
4810  SCMutexUnlock(&master->lock);
4811  return 0;
4812 }
4813 
4814 static int DetectEngineTenantUnregisterSelector(
4815  enum DetectEngineTenantSelectors selector, uint32_t tenant_id, uint32_t traffic_id)
4816 {
4817  DetectEngineMasterCtx *master = &g_master_de_ctx;
4818  SCMutexLock(&master->lock);
4819 
4820  if (master->tenant_mapping_list == NULL) {
4821  SCMutexUnlock(&master->lock);
4822  return -1;
4823  }
4824 
4825  DetectEngineTenantMapping *prev = NULL;
4827  while (map) {
4828  if (map->traffic_id == traffic_id &&
4829  map->tenant_id == tenant_id)
4830  {
4831  if (prev != NULL)
4832  prev->next = map->next;
4833  else
4834  master->tenant_mapping_list = map->next;
4835 
4836  map->next = NULL;
4837  SCFree(map);
4838  SCLogInfo("tenant handler %u %u %u unregistered", selector, tenant_id, traffic_id);
4839  SCMutexUnlock(&master->lock);
4840  return 0;
4841  }
4842  prev = map;
4843  map = map->next;
4844  }
4845 
4846  SCMutexUnlock(&master->lock);
4847  return -1;
4848 }
4849 
4850 int DetectEngineTenantRegisterLivedev(uint32_t tenant_id, int device_id)
4851 {
4852  return DetectEngineTenantRegisterSelector(
4853  TENANT_SELECTOR_LIVEDEV, tenant_id, (uint32_t)device_id);
4854 }
4855 
4856 int DetectEngineTenantRegisterVlanId(uint32_t tenant_id, uint16_t vlan_id)
4857 {
4858  return DetectEngineTenantRegisterSelector(TENANT_SELECTOR_VLAN, tenant_id, (uint32_t)vlan_id);
4859 }
4860 
4861 int DetectEngineTenantUnregisterVlanId(uint32_t tenant_id, uint16_t vlan_id)
4862 {
4863  return DetectEngineTenantUnregisterSelector(TENANT_SELECTOR_VLAN, tenant_id, (uint32_t)vlan_id);
4864 }
4865 
4866 int DetectEngineTenantRegisterPcapFile(uint32_t tenant_id)
4867 {
4868  SCLogInfo("registering %u %d 0", TENANT_SELECTOR_DIRECT, tenant_id);
4869  return DetectEngineTenantRegisterSelector(TENANT_SELECTOR_DIRECT, tenant_id, 0);
4870 }
4871 
4873 {
4874  SCLogInfo("unregistering %u %d 0", TENANT_SELECTOR_DIRECT, tenant_id);
4875  return DetectEngineTenantUnregisterSelector(TENANT_SELECTOR_DIRECT, tenant_id, 0);
4876 }
4877 
4878 static uint32_t DetectEngineTenantGetIdFromPcap(const void *ctx, const Packet *p)
4879 {
4880  return p->pcap_v.tenant_id;
4881 }
4882 
4884 {
4885  DetectEngineMasterCtx *master = &g_master_de_ctx;
4886  SCMutexLock(&master->lock);
4887 
4888  if (master->list == NULL) {
4889  SCMutexUnlock(&master->lock);
4890  return NULL;
4891  }
4892 
4893  DetectEngineCtx *de_ctx = master->list;
4894  while (de_ctx) {
4896  de_ctx->tenant_id == tenant_id)
4897  {
4898  de_ctx->ref_cnt++;
4899  break;
4900  }
4901 
4902  de_ctx = de_ctx->next;
4903  }
4904 
4905  SCMutexUnlock(&master->lock);
4906  return de_ctx;
4907 }
4908 
4910 {
4911  DEBUG_VALIDATE_BUG_ON((*de_ctx)->ref_cnt == 0);
4912  (*de_ctx)->ref_cnt--;
4913  *de_ctx = NULL;
4914 }
4915 
4916 static int DetectEngineAddToList(DetectEngineCtx *instance)
4917 {
4918  DetectEngineMasterCtx *master = &g_master_de_ctx;
4919 
4920  if (instance == NULL)
4921  return -1;
4922 
4923  if (master->list == NULL) {
4924  master->list = instance;
4925  } else {
4926  instance->next = master->list;
4927  master->list = instance;
4928  }
4929 
4930  return 0;
4931 }
4932 
4934 {
4935  int r;
4936 
4937  if (de_ctx == NULL)
4938  return -1;
4939 
4940  SCLogDebug("adding de_ctx %p to master", de_ctx);
4941 
4942  DetectEngineMasterCtx *master = &g_master_de_ctx;
4943  SCMutexLock(&master->lock);
4944  r = DetectEngineAddToList(de_ctx);
4945  SCMutexUnlock(&master->lock);
4946  return r;
4947 }
4948 
4949 static int DetectEngineMoveToFreeListNoLock(DetectEngineMasterCtx *master, DetectEngineCtx *de_ctx)
4950 {
4951  DetectEngineCtx *instance = master->list;
4952  if (instance == NULL) {
4953  return -1;
4954  }
4955 
4956  /* remove from active list */
4957  if (instance == de_ctx) {
4958  master->list = instance->next;
4959  } else {
4960  DetectEngineCtx *prev = instance;
4961  instance = instance->next; /* already checked first element */
4962 
4963  while (instance) {
4964  DetectEngineCtx *next = instance->next;
4965 
4966  if (instance == de_ctx) {
4967  prev->next = instance->next;
4968  break;
4969  }
4970 
4971  prev = instance;
4972  instance = next;
4973  }
4974  if (instance == NULL) {
4975  return -1;
4976  }
4977  }
4978 
4979  /* instance is now detached from list */
4980  instance->next = NULL;
4981 
4982  /* add to free list */
4983  if (master->free_list == NULL) {
4984  master->free_list = instance;
4985  } else {
4986  instance->next = master->free_list;
4987  master->free_list = instance;
4988  }
4989  SCLogDebug("detect engine %p moved to free list (%u refs)", de_ctx, de_ctx->ref_cnt);
4990  return 0;
4991 }
4992 
4994 {
4995  int ret = 0;
4996  DetectEngineMasterCtx *master = &g_master_de_ctx;
4997  SCMutexLock(&master->lock);
4998  ret = DetectEngineMoveToFreeListNoLock(master, de_ctx);
4999  SCMutexUnlock(&master->lock);
5000  return ret;
5001 }
5002 
5004 {
5005  DetectEngineMasterCtx *master = &g_master_de_ctx;
5006  SCMutexLock(&master->lock);
5007 
5008  DetectEngineCtx *prev = NULL;
5009  DetectEngineCtx *instance = master->free_list;
5010  while (instance) {
5011  DetectEngineCtx *next = instance->next;
5012 
5013  SCLogDebug("detect engine %p has %u ref(s)", instance, instance->ref_cnt);
5014 
5015  if (instance->ref_cnt == 0) {
5016  if (prev == NULL) {
5017  master->free_list = next;
5018  } else {
5019  prev->next = next;
5020  }
5021 
5022  SCLogDebug("freeing detect engine %p", instance);
5023  DetectEngineCtxFree(instance);
5024  instance = NULL;
5025  }
5026 
5027  prev = instance;
5028  instance = next;
5029  }
5030  SCMutexUnlock(&master->lock);
5031 }
5032 
5034 {
5035  DetectEngineMasterCtx *master = &g_master_de_ctx;
5036  SCMutexLock(&master->lock);
5037 
5038  DetectEngineCtx *instance = master->list;
5039  while (instance) {
5040  DetectEngineCtx *next = instance->next;
5041  DEBUG_VALIDATE_BUG_ON(instance->ref_cnt);
5042  SCLogDebug("detect engine %p has %u ref(s)", instance, instance->ref_cnt);
5043  instance->ref_cnt = 0;
5044  DetectEngineMoveToFreeListNoLock(master, instance);
5045  instance = next;
5046  }
5047  SCMutexUnlock(&master->lock);
5049 }
5050 
5051 static int reloads = 0;
5052 
5053 /** \brief Reload the detection engine
5054  *
5055  * \param filename YAML file to load for the detect config
5056  *
5057  * \retval -1 error
5058  * \retval 0 ok
5059  */
5061 {
5062  DetectEngineCtx *new_de_ctx = NULL;
5063  DetectEngineCtx *old_de_ctx = NULL;
5064 
5065  char prefix[128];
5066  memset(prefix, 0, sizeof(prefix));
5067 
5068  SCLogNotice("rule reload starting");
5069 
5070  if (suri->conf_filename != NULL) {
5071  snprintf(prefix, sizeof(prefix), "detect-engine-reloads.%d", reloads++);
5072  SCLogConfig("Reloading %s", suri->conf_filename);
5073  if (SCConfYamlLoadFileWithPrefix(suri->conf_filename, prefix) != 0) {
5074  SCLogError("failed to load yaml %s", suri->conf_filename);
5075  return -1;
5076  }
5077 
5078  SCConfNode *node = SCConfGetNode(prefix);
5079  if (node == NULL) {
5080  SCLogError("failed to properly setup yaml %s", suri->conf_filename);
5081  return -1;
5082  }
5083 
5084  if (suri->additional_configs) {
5085  for (int i = 0; suri->additional_configs[i] != NULL; i++) {
5086  SCLogConfig("Reloading %s", suri->additional_configs[i]);
5088  }
5089  }
5090 
5091 #if 0
5092  SCConfDump();
5093 #endif
5094  }
5095 
5096  /* get a reference to the current de_ctx */
5097  old_de_ctx = DetectEngineGetCurrent();
5098  if (old_de_ctx == NULL)
5099  return -1;
5100  SCLogDebug("get ref to old_de_ctx %p", old_de_ctx);
5101  DatasetReload();
5102 
5103  /* only reload a regular 'normal' and 'delayed detect stub' detect engines */
5104  if (!(old_de_ctx->type == DETECT_ENGINE_TYPE_NORMAL ||
5105  old_de_ctx->type == DETECT_ENGINE_TYPE_DD_STUB))
5106  {
5107  DetectEngineDeReference(&old_de_ctx);
5108  SCLogNotice("rule reload complete");
5109  return -1;
5110  }
5111 
5112  /* get new detection engine */
5113  new_de_ctx = DetectEngineCtxInitWithPrefix(prefix, old_de_ctx->tenant_id);
5114  if (new_de_ctx == NULL) {
5115  SCLogError("initializing detection engine "
5116  "context failed.");
5117  DetectEngineDeReference(&old_de_ctx);
5118  return -1;
5119  }
5120 
5121  if (SigLoadSignatures(new_de_ctx,
5122  suri->sig_file, suri->sig_file_exclusive) != 0) {
5123  DetectEngineCtxFree(new_de_ctx);
5124  DetectEngineDeReference(&old_de_ctx);
5125  return -1;
5126  }
5127  SCLogDebug("set up new_de_ctx %p", new_de_ctx);
5128 
5129  /* Copy over callbacks. */
5130  new_de_ctx->RateFilterCallback = old_de_ctx->RateFilterCallback;
5131  new_de_ctx->rate_filter_callback_arg = old_de_ctx->rate_filter_callback_arg;
5132 
5133  /* add to master */
5134  DetectEngineAddToMaster(new_de_ctx);
5135 
5136  /* move to old free list */
5137  DetectEngineMoveToFreeList(old_de_ctx);
5138  DetectEngineDeReference(&old_de_ctx);
5139 
5140  SCLogDebug("going to reload the threads to use new_de_ctx %p", new_de_ctx);
5141 
5142  DetectEngineMasterCtx *master = &g_master_de_ctx;
5143  SCMutexLock(&master->lock);
5144  /* update the threads */
5145  DetectEngineReloadThreads(new_de_ctx);
5146  SCMutexUnlock(&master->lock);
5147 
5148  SCLogDebug("threads now run new_de_ctx %p", new_de_ctx);
5149 
5150  /* walk free list, freeing the old_de_ctx */
5152 
5154 
5156 
5157  SCLogDebug("old_de_ctx should have been freed");
5158 
5160 
5161  SCLogNotice("rule reload complete");
5162 
5163 #ifdef HAVE_MALLOC_TRIM
5164  /* The reload process potentially frees up large amounts of memory.
5165  * Encourage the memory management system to reclaim as much as it
5166  * can.
5167  */
5168  malloc_trim(0);
5169 #endif
5170 
5171  return 0;
5172 }
5173 
5174 static uint32_t TenantIdHash(HashTable *h, void *data, uint16_t data_len)
5175 {
5176  DetectEngineThreadCtx *det_ctx = (DetectEngineThreadCtx *)data;
5177  return det_ctx->tenant_id % h->array_size;
5178 }
5179 
5180 static char TenantIdCompare(void *d1, uint16_t d1_len, void *d2, uint16_t d2_len)
5181 {
5184  return (det1->tenant_id == det2->tenant_id);
5185 }
5186 
5187 static void TenantIdFree(void *d)
5188 {
5189  DetectEngineThreadCtxFree(d);
5190 }
5191 
5193 {
5194  DetectEngineMasterCtx *master = &g_master_de_ctx;
5195  SCMutexLock(&master->lock);
5196 
5197  if (master->tenant_selector == TENANT_SELECTOR_UNKNOWN) {
5198  SCLogInfo("error, no tenant selector");
5199  SCMutexUnlock(&master->lock);
5200  return -1;
5201  }
5202 
5203  DetectEngineCtx *stub_de_ctx = NULL;
5204  DetectEngineCtx *list = master->list;
5205  for ( ; list != NULL; list = list->next) {
5206  SCLogDebug("list %p tenant %u", list, list->tenant_id);
5207 
5208  if (list->type == DETECT_ENGINE_TYPE_NORMAL ||
5209  list->type == DETECT_ENGINE_TYPE_MT_STUB ||
5211  {
5212  stub_de_ctx = list;
5213  break;
5214  }
5215  }
5216  if (stub_de_ctx == NULL) {
5217  stub_de_ctx = DetectEngineCtxInitStubForMT();
5218  if (stub_de_ctx == NULL) {
5219  SCMutexUnlock(&master->lock);
5220  return -1;
5221  }
5222 
5223  if (master->list == NULL) {
5224  master->list = stub_de_ctx;
5225  } else {
5226  stub_de_ctx->next = master->list;
5227  master->list = stub_de_ctx;
5228  }
5229  }
5230 
5231  /* update the threads */
5232  SCLogDebug("MT reload starting");
5233  DetectEngineReloadThreads(stub_de_ctx);
5234  SCLogDebug("MT reload done");
5235 
5236  SCMutexUnlock(&master->lock);
5237 
5238  /* walk free list, freeing the old_de_ctx */
5240  // needed for VarNameStoreFree
5242 
5243  SCLogDebug("old_de_ctx should have been freed");
5244  return 0;
5245 }
5246 
5247 static int g_parse_metadata = 0;
5248 
5250 {
5251  g_parse_metadata = 1;
5252 }
5253 
5255 {
5256  g_parse_metadata = 0;
5257 }
5258 
5260 {
5261  return g_parse_metadata;
5262 }
5263 
5265 {
5266  switch (type) {
5267  case DETECT_SM_LIST_MATCH:
5268  return "packet";
5269  case DETECT_SM_LIST_PMATCH:
5270  return "packet/stream payload";
5271 
5272  case DETECT_SM_LIST_TMATCH:
5273  return "tag";
5274 
5276  return "base64_data";
5277 
5279  return "post-match";
5280 
5282  return "suppress";
5284  return "threshold";
5285 
5286  case DETECT_SM_LIST_MAX:
5287  return "max (internal)";
5288  }
5289  return "error";
5290 }
5291 
5292 /* events api */
5294 {
5296  det_ctx->events++;
5297 }
5298 
5300  const Signature *s, const char **sigerror, const DetectBufferType *map)
5301 {
5302  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
5303  if (s->init_data->buffers[x].id != (uint32_t)map->id)
5304  continue;
5305  const SigMatch *sm = s->init_data->buffers[x].head;
5306  for (; sm != NULL; sm = sm->next) {
5307  if (sm->type != DETECT_CONTENT)
5308  continue;
5309 
5310  const DetectContentData *cd = (DetectContentData *)sm->ctx;
5311  if (cd->flags & DETECT_CONTENT_NOCASE) {
5312  *sigerror = "md5-like keyword should not be used together with "
5313  "nocase, since the rule is automatically "
5314  "lowercased anyway which makes nocase redundant.";
5315  SCLogWarning("rule %u: buffer %s: %s", s->id, map->name, *sigerror);
5316  }
5317 
5318  if (cd->content_len != SC_MD5_HEX_LEN) {
5319  *sigerror = "Invalid length for md5-like keyword (should "
5320  "be 32 characters long). This rule will therefore "
5321  "never match.";
5322  SCLogError("rule %u: buffer %s: %s", s->id, map->name, *sigerror);
5323  return false;
5324  }
5325 
5326  for (size_t i = 0; i < cd->content_len; ++i) {
5327  if (!isxdigit(cd->content[i])) {
5328  *sigerror =
5329  "Invalid md5-like string (should be string of hexadecimal characters)."
5330  "This rule will therefore never match.";
5331  SCLogWarning("rule %u: buffer %s: %s", s->id, map->name, *sigerror);
5332  return false;
5333  }
5334  }
5335  }
5336  }
5337  return true;
5338 }
5339 
5341  const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
5342 {
5343  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
5344  if (s->init_data->buffers[x].id != (uint32_t)map->id)
5345  continue;
5346  SigMatch *sm = s->init_data->buffers[x].head;
5347  for (; sm != NULL; sm = sm->next) {
5348  if (sm->type != DETECT_CONTENT)
5349  continue;
5350 
5352 
5353  bool changed = false;
5354  uint32_t u;
5355  for (u = 0; u < cd->content_len; u++) {
5356  if (isupper(cd->content[u])) {
5357  cd->content[u] = u8_tolower(cd->content[u]);
5358  changed = true;
5359  }
5360  }
5361 
5362  if (changed) {
5363  SpmDestroyCtx(cd->spm_ctx);
5364  cd->spm_ctx =
5366  }
5367  }
5368  }
5369 }
5370 
5372 {
5374  if (de_ctx == NULL) {
5375  SCLogError("no detection engine available for rate filter callback registration");
5376  return false;
5377  }
5378  de_ctx->RateFilterCallback = fn;
5381  return true;
5382 }
5383 
5385 {
5386  if (det_ctx->json_content_len > SIG_JSON_CONTENT_ARRAY_LEN - 1) {
5387  SCLogDebug("json content length %u exceeds maximum %u", det_ctx->json_content_len,
5389  return -1;
5390  }
5391  if (det_ctx->json_content_len >= det_ctx->json_content_capacity) {
5392  if (det_ctx->json_content_capacity == 0) {
5393  det_ctx->json_content_capacity = 1;
5394  } else {
5395  det_ctx->json_content_capacity *= 2;
5396  }
5397  void *tmp = SCRealloc(
5398  det_ctx->json_content, det_ctx->json_content_capacity * sizeof(SigJsonContent));
5399  if (unlikely(tmp == NULL)) {
5400  return -1;
5401  }
5402  SCLogDebug("reallocated json content array to %u items", det_ctx->json_content_capacity);
5403  det_ctx->json_content = tmp;
5404  }
5405  return 0;
5406 }
5407 
5408 /*************************************Unittest*********************************/
5409 
5410 #ifdef UNITTESTS
5411 
5412 static int DetectEngineInitYamlConf(const char *conf)
5413 {
5415  SCConfInit();
5416  return SCConfYamlLoadString(conf, strlen(conf));
5417 }
5418 
5419 static void DetectEngineDeInitYamlConf(void)
5420 {
5421  SCConfDeInit();
5423 }
5424 
5425 static int DetectEngineTest01(void)
5426 {
5427  const char *conf =
5428  "%YAML 1.1\n"
5429  "---\n"
5430  "detect-engine:\n"
5431  " - profile: medium\n"
5432  " - custom-values:\n"
5433  " toclient_src_groups: 2\n"
5434  " toclient_dst_groups: 2\n"
5435  " toclient_sp_groups: 2\n"
5436  " toclient_dp_groups: 3\n"
5437  " toserver_src_groups: 2\n"
5438  " toserver_dst_groups: 4\n"
5439  " toserver_sp_groups: 2\n"
5440  " toserver_dp_groups: 25\n"
5441  " - inspection-recursion-limit: 0\n";
5442 
5443  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5444 
5447 
5449 
5451 
5452  DetectEngineDeInitYamlConf();
5453 
5454  PASS;
5455 }
5456 
5457 static int DetectEngineTest02(void)
5458 {
5459  const char *conf =
5460  "%YAML 1.1\n"
5461  "---\n"
5462  "detect-engine:\n"
5463  " - profile: medium\n"
5464  " - custom-values:\n"
5465  " toclient_src_groups: 2\n"
5466  " toclient_dst_groups: 2\n"
5467  " toclient_sp_groups: 2\n"
5468  " toclient_dp_groups: 3\n"
5469  " toserver_src_groups: 2\n"
5470  " toserver_dst_groups: 4\n"
5471  " toserver_sp_groups: 2\n"
5472  " toserver_dp_groups: 25\n"
5473  " - inspection-recursion-limit:\n";
5474 
5475  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5476 
5479 
5480  FAIL_IF_NOT(
5482 
5484 
5485  DetectEngineDeInitYamlConf();
5486 
5487  PASS;
5488 }
5489 
5490 static int DetectEngineTest03(void)
5491 {
5492  const char *conf =
5493  "%YAML 1.1\n"
5494  "---\n"
5495  "detect-engine:\n"
5496  " - profile: medium\n"
5497  " - custom-values:\n"
5498  " toclient_src_groups: 2\n"
5499  " toclient_dst_groups: 2\n"
5500  " toclient_sp_groups: 2\n"
5501  " toclient_dp_groups: 3\n"
5502  " toserver_src_groups: 2\n"
5503  " toserver_dst_groups: 4\n"
5504  " toserver_sp_groups: 2\n"
5505  " toserver_dp_groups: 25\n";
5506 
5507  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5508 
5511 
5512  FAIL_IF_NOT(
5514 
5516 
5517  DetectEngineDeInitYamlConf();
5518 
5519  PASS;
5520 }
5521 
5522 static int DetectEngineTest04(void)
5523 {
5524  const char *conf =
5525  "%YAML 1.1\n"
5526  "---\n"
5527  "detect-engine:\n"
5528  " - profile: medium\n"
5529  " - custom-values:\n"
5530  " toclient_src_groups: 2\n"
5531  " toclient_dst_groups: 2\n"
5532  " toclient_sp_groups: 2\n"
5533  " toclient_dp_groups: 3\n"
5534  " toserver_src_groups: 2\n"
5535  " toserver_dst_groups: 4\n"
5536  " toserver_sp_groups: 2\n"
5537  " toserver_dp_groups: 25\n"
5538  " - inspection-recursion-limit: 10\n";
5539 
5540  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5541 
5544 
5546 
5548 
5549  DetectEngineDeInitYamlConf();
5550 
5551  PASS;
5552 }
5553 
5554 static int DetectEngineTest08(void)
5555 {
5556  const char *conf =
5557  "%YAML 1.1\n"
5558  "---\n"
5559  "detect-engine:\n"
5560  " - profile: custom\n"
5561  " - custom-values:\n"
5562  " toclient-groups: 23\n"
5563  " toserver-groups: 27\n";
5564 
5565  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5566 
5569 
5572 
5574 
5575  DetectEngineDeInitYamlConf();
5576 
5577  PASS;
5578 }
5579 
5580 /** \test bug 892 bad values */
5581 static int DetectEngineTest09(void)
5582 {
5583  const char *conf =
5584  "%YAML 1.1\n"
5585  "---\n"
5586  "detect-engine:\n"
5587  " - profile: custom\n"
5588  " - custom-values:\n"
5589  " toclient-groups: BA\n"
5590  " toserver-groups: BA\n"
5591  " - inspection-recursion-limit: 10\n";
5592 
5593  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5594 
5597 
5600 
5602 
5603  DetectEngineDeInitYamlConf();
5604 
5605  PASS;
5606 }
5607 
5608 /** \brief keyword thread-context init stub that always fails, used to mimic a
5609  * failing per-thread keyword init such as DetectFilemagicThreadInit. */
5610 static void *DetectEngineFailingThreadKeywordInit(void *data)
5611 {
5612  (void)data;
5613  return NULL;
5614 }
5615 
5616 static void DetectEngineNoopThreadKeywordFree(void *ctx)
5617 {
5618  (void)ctx;
5619 }
5620 
5621 /** \test Ticket #8237: a failing per-thread keyword init must make the detect
5622  * thread context init fail rather than silently continuing with a partially
5623  * initialized keyword context array. */
5624 static int DetectEngineThreadCtxInitKeywordFailTest(void)
5625 {
5626  ThreadVars th_v;
5627  memset(&th_v, 0, sizeof(th_v));
5628  DetectEngineThreadCtx *det_ctx = NULL;
5629 
5632  de_ctx->flags |= DE_QUIET;
5633 
5634  Signature *s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (sid:1;)");
5635  FAIL_IF_NULL(s);
5636 
5638 
5639  /* Register a keyword whose per-thread init fails (returns NULL). */
5640  int id = DetectRegisterThreadCtxFuncs(de_ctx, "test_failing_keyword",
5641  DetectEngineFailingThreadKeywordInit, NULL, DetectEngineNoopThreadKeywordFree, 0);
5642  FAIL_IF(id < 0);
5643 
5644  /* Thread context init must report failure and not hand back a context. */
5645  TmEcode r = DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
5646  FAIL_IF(r != TM_ECODE_FAILED);
5647  FAIL_IF_NOT_NULL(det_ctx);
5648 
5650 
5651  PASS;
5652 }
5653 
5654 #endif
5655 
5657 {
5658 #ifdef UNITTESTS
5659  UtRegisterTest("DetectEngineTest01", DetectEngineTest01);
5660  UtRegisterTest("DetectEngineTest02", DetectEngineTest02);
5661  UtRegisterTest("DetectEngineTest03", DetectEngineTest03);
5662  UtRegisterTest("DetectEngineTest04", DetectEngineTest04);
5663  UtRegisterTest("DetectEngineTest08", DetectEngineTest08);
5664  UtRegisterTest("DetectEngineTest09", DetectEngineTest09);
5666  "DetectEngineThreadCtxInitKeywordFailTest", DetectEngineThreadCtxInitKeywordFailTest);
5667 #endif
5668 }
DetectEngineThreadCtx_::byte_values
uint64_t * byte_values
Definition: detect.h:1341
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
DETECT_TABLE_APP_TD
@ DETECT_TABLE_APP_TD
Definition: detect.h:567
DetectEngineAppInspectionEngine_::stream
bool stream
Definition: detect.h:424
HashListTableGetListData
#define HashListTableGetListData(hb)
Definition: util-hashlist.h:56
DE_STATE_ID_FILE_INSPECT
#define DE_STATE_ID_FILE_INSPECT
Definition: detect-engine-state.h:62
SCProfilingSghThreadCleanup
void SCProfilingSghThreadCleanup(DetectEngineThreadCtx *det_ctx)
Definition: util-profiling-rulegroups.c:330
DetectEngineCtxInitWithPrefix
DetectEngineCtx * DetectEngineCtxInitWithPrefix(const char *prefix, uint32_t tenant_id)
Definition: detect-engine.c:2844
DetectEngineTenantMapping_
Definition: detect.h:1728
DEFAULT_MAX_FLOWBITS_PER_SIGNATURE
#define DEFAULT_MAX_FLOWBITS_PER_SIGNATURE
Definition: detect-engine.c:98
util-device-private.h
util-byte.h
SCConfYamlLoadString
int SCConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:536
tm-threads.h
DetectEngineAppInspectionEngine_
Definition: detect.h:419
DetectGetMultiData
InspectionBuffer * DetectGetMultiData(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id, uint32_t index, InspectionMultiBufferGetDataPtr GetBuf)
Definition: detect-engine.c:2322
DetectEngineThreadCtx_::inspect
struct DetectEngineThreadCtx_::@101 inspect
util-hash-string.h
DetectBufferType_::supports_transforms
bool supports_transforms
Definition: detect.h:461
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:539
DetectEngineSyncer_::m
SCMutex m
Definition: detect-engine.c:2056
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:423
DETECT_ENGINE_MPM_CACHE_OP_PRUNE
#define DETECT_ENGINE_MPM_CACHE_OP_PRUNE
Definition: detect.h:1773
DetectBufferType_::mpm
bool mpm
Definition: detect.h:458
detect-content.h
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@82 v2
detect-engine.h
DetectEngineResetMaxSigId
void DetectEngineResetMaxSigId(DetectEngineCtx *de_ctx)
Definition: detect-engine.c:3302
DetectEngineMTApply
int DetectEngineMTApply(void)
Definition: detect-engine.c:5192
SCProfilingKeywordDestroyCtx
void SCProfilingKeywordDestroyCtx(DetectEngineCtx *de_ctx)
Definition: util-profiling-keywords.c:268
DetectEngineBufferTypeSupportsPacketGetById
bool DetectEngineBufferTypeSupportsPacketGetById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1611
DETECT_SM_LIST_PMATCH
@ DETECT_SM_LIST_PMATCH
Definition: detect.h:119
DetectEngineThreadCtxInitForReload
DetectEngineThreadCtx * DetectEngineThreadCtxInitForReload(ThreadVars *tv, DetectEngineCtx *new_de_ctx, int mt)
Definition: detect-engine.c:3718
DetectEngineThreadCtx_::to_clear_idx
uint32_t to_clear_idx
Definition: detect.h:1366
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SCConfNodeRemove
void SCConfNodeRemove(SCConfNode *node)
Remove (and SCFree) the provided configuration node.
Definition: conf.c:710
SignatureInitData_::smlists
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition: detect.h:662
SCProfilingKeywordThreadCleanup
void SCProfilingKeywordThreadCleanup(DetectEngineThreadCtx *det_ctx)
Definition: util-profiling-keywords.c:338
THashDataGetResult::data
THashData * data
Definition: util-thash.h:192
SCConfGetTime
int SCConfGetTime(const char *name, uint64_t *val)
Retrieve a configuration value as a time duration in seconds.
Definition: conf.c:691
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *, const DetectBufferType *))
Definition: detect-engine.c:1638
AlertQueueFree
void AlertQueueFree(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine-alert.c:282
DetectEngineAppHookToName
const char * DetectEngineAppHookToName(const AppProto p, const uint8_t sub_state, const uint8_t state, const uint8_t direction)
Definition: detect-engine.c:846
SCProfilingSghThreadSetup
void SCProfilingSghThreadSetup(SCProfileSghDetectCtx *ctx, DetectEngineThreadCtx *det_ctx)
Definition: util-profiling-rulegroups.c:295
DetectEngineThreadCtx_::counter_alerts
StatsCounterId counter_alerts
Definition: detect.h:1351
DetectParseDupSigHashInit
int DetectParseDupSigHashInit(DetectEngineCtx *de_ctx)
Initializes the hash table that is used to cull duplicate sigs.
Definition: detect-parse.c:3587
RELOAD
@ RELOAD
Definition: detect-engine.c:2051
SIG_FLAG_FW_HOOK_LTE
#define SIG_FLAG_FW_HOOK_LTE
Definition: detect.h:254
DetectGetSingleData
InspectionBuffer * DetectGetSingleData(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id, InspectionSingleBufferGetDataPtr GetBuf)
Definition: detect-engine.c:2305
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SIG_JSON_CONTENT_ARRAY_LEN
#define SIG_JSON_CONTENT_ARRAY_LEN
Definition: detect.h:1293
DetectLoaderThreadSpawn
void DetectLoaderThreadSpawn(void)
spawn the detect loader manager thread
Definition: detect-engine-loader.c:760
AppLayerParserIsEnabled
int AppLayerParserIsEnabled(AppProto alproto)
simple way to globally test if a alproto is registered and fully enabled in the configuration.
Definition: app-layer-parser.c:1787
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
SinglePatternMatchDefaultMatcher
uint8_t SinglePatternMatchDefaultMatcher(void)
Returns the single pattern matcher algorithm to be used, based on the spm-algo setting in yaml.
Definition: util-spm.c:69
DetectEngineDeReference
void DetectEngineDeReference(DetectEngineCtx **de_ctx)
Definition: detect-engine.c:4909
DETECT_CI_FLAGS_START
#define DETECT_CI_FLAGS_START
Definition: detect-engine-content-inspection.h:40
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2233
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1511
DetectEngineMustParseMetadata
int DetectEngineMustParseMetadata(void)
Definition: detect-engine.c:5259
AppLayerParserGetStateNameById
const char * AppLayerParserGetStateNameById(uint8_t ipproto, AppProto alproto, const int id, const uint8_t direction)
Definition: app-layer-parser.c:1873
TAILQ_INIT
#define TAILQ_INIT(head)
Definition: queue.h:262
SignatureHook_::sm_list
int sm_list
Definition: detect.h:581
MpmFactoryDeRegisterAllMpmCtxProfiles
void MpmFactoryDeRegisterAllMpmCtxProfiles(DetectEngineCtx *de_ctx)
Definition: util-mpm.c:168
DETECT_TABLE_APP_FILTER
@ DETECT_TABLE_APP_FILTER
Definition: detect.h:566
flow-util.h
DetectEnginePktInspectionEngine
Definition: detect.h:488
DetectEngineMasterCtx_::tenant_mapping_list
DetectEngineTenantMapping * tenant_mapping_list
Definition: detect.h:1759
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
Definition: util-atomic.h:314
DetectEngineAppInspectionEngine_::next
struct DetectEngineAppInspectionEngine_ * next
Definition: detect.h:445
SCInspectionBufferGet
InspectionBuffer * SCInspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
DetectEngineInspectMultiBufferGeneric
uint8_t DetectEngineInspectMultiBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Definition: detect-engine.c:2346
detect-engine-siggroup.h
SigTableElmt_::name
const char * name
Definition: detect.h:1524
DetectEngineMasterCtx_::list
DetectEngineCtx * list
Definition: detect.h:1748
InspectionBuffer::initialized
bool initialized
Definition: detect-engine-inspect-buffer.h:38
MpmStoreFree
void MpmStoreFree(DetectEngineCtx *de_ctx)
Frees the hash table - DetectEngineCtx->mpm_hash_table, allocated by MpmStoreInit() function.
Definition: detect-engine-mpm.c:1671
DetectFrameMpmRegisterByParentId
void DetectFrameMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id, DetectEngineTransforms *transforms)
copy a mpm engine from parent_id, add in transforms
Definition: detect-engine-mpm.c:439
DetectEngineBufferTypeSupportsPacket
void DetectEngineBufferTypeSupportsPacket(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1578
DetectEngineBufferRunSetupCallback
void DetectEngineBufferRunSetupCallback(const DetectEngineCtx *de_ctx, const int id, Signature *s)
Definition: detect-engine.c:1648
DetectEngineCtx_::type
enum DetectEngineType type
Definition: detect.h:1115
DetectEnginePruneFreeList
void DetectEnginePruneFreeList(void)
Definition: detect-engine.c:5003
SigLoadSignatures
int SigLoadSignatures(DetectEngineCtx *de_ctx, char *sig_file, bool sig_file_exclusive)
Load signatures.
Definition: detect-engine-loader.c:384
DetectEngineInspectBufferSingle
uint8_t DetectEngineInspectBufferSingle(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2173
DetectThreadCtxGetKeywordThreadCtx
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3999
DetectEngineCtx_::guess_applayer
bool guess_applayer
Definition: detect.h:1038
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DetectEngineTransforms
Definition: detect.h:394
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SIG_FLAG_INIT_NEED_FLUSH
#define SIG_FLAG_INIT_NEED_FLUSH
Definition: detect.h:300
MpmTableElmt_::name
const char * name
Definition: util-mpm.h:169
SCClassSCConfInit
void SCClassSCConfInit(DetectEngineCtx *de_ctx)
Definition: util-classification-config.c:61
Signature_::app_progress_hook
uint8_t app_progress_hook
Definition: detect.h:725
DetectEngineCtxInitStubForDD
DetectEngineCtx * DetectEngineCtxInitStubForDD(void)
Definition: detect-engine.c:2834
KEYWORD_PROFILING_SET_LIST
#define KEYWORD_PROFILING_SET_LIST(ctx, list)
Definition: util-profiling.h:46
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:78
DetectEngineCtx_::max_uniq_toclient_groups
uint16_t max_uniq_toclient_groups
Definition: detect.h:1049
ActionInitConfig
int ActionInitConfig(void)
Load the action order from config. If none is provided, it will be default to ACTION_PASS,...
Definition: util-action.c:105
IDLE
@ IDLE
Definition: detect-engine.c:2050
DetectEngineAppHookToSmlist
int DetectEngineAppHookToSmlist(const AppProto p, const uint8_t sub_state, const uint8_t state, const uint8_t direction)
get the sm_list for a app hook
Definition: detect-engine.c:885
DetectEngineCtx_::ref_cnt
uint32_t ref_cnt
Definition: detect.h:1118
DetectEngineAppInspectionEngine_::Callback
InspectEngineFuncPtr Callback
Definition: detect.h:438
PathMerge
int PathMerge(char *out_buf, size_t buf_size, const char *const dir, const char *const fname)
Definition: util-path.c:74
Signature_::alproto
AppProto alproto
Definition: detect.h:693
DETECT_TABLE_PACKET_PRE_STREAM
@ DETECT_TABLE_PACKET_PRE_STREAM
Definition: detect.h:563
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
SigString_
Definition: detect.h:889
StatsRegisterCounter
StatsCounterId StatsRegisterCounter(const char *name, StatsThreadContext *stats)
Registers a normal, unqualified counter.
Definition: counters.c:1039
PacketEnqueue
void PacketEnqueue(PacketQueue *q, Packet *p)
Definition: packet-queue.c:175
MPM_HS
@ MPM_HS
Definition: util-mpm.h:54
DetectEngineCtx_::filedata_config
DetectFileDataCfg * filedata_config
Definition: detect.h:1101
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
DetectEngineFrameInspectionEngine::sm_list_base
uint16_t sm_list_base
Definition: detect.h:519
DetectPort_::port
uint16_t port
Definition: detect.h:223
SigMatchData_::is_last
bool is_last
Definition: detect.h:370
SignatureHook_::app
struct SignatureHook_::@87::@88 app
DetectBufferTypeSupportsFrames
void DetectBufferTypeSupportsFrames(const char *name)
Definition: detect-engine.c:1413
name
const char * name
Definition: detect-engine-proto.c:48
DetectEngineMpmCacheService
void DetectEngineMpmCacheService(uint32_t op_flags)
Definition: detect-engine.c:2678
DetectMpmInitializeFrameMpms
void DetectMpmInitializeFrameMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:543
PacketQueue_
simple fifo queue for packets with mutex and cond Calling the mutex or triggering the cond is respons...
Definition: packet-queue.h:49
Flow_::proto
uint8_t proto
Definition: flow.h:377
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
DETECT_SM_LIST_DYNAMIC_START
@ DETECT_SM_LIST_DYNAMIC_START
Definition: detect.h:138
DetectEngineThreadCtx_::decoder_events
AppLayerDecoderEvents * decoder_events
Definition: detect.h:1437
SigMatchData_::ctx
SigMatchCtx * ctx
Definition: detect.h:371
AppLayerParserGetStateProgressCompletionStatus
uint8_t AppLayerParserGetStateProgressCompletionStatus(AppProto alproto, uint8_t direction)
Definition: app-layer-parser.c:1252
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
SignatureHook_::t
union SignatureHook_::@87 t
DetectEngineThreadKeywordCtxItem_::InitFunc
void *(* InitFunc)(void *)
Definition: detect.h:908
Packet_::flags
uint32_t flags
Definition: decode.h:562
type
uint8_t type
Definition: decode-sctp.h:0
SCMutexIsLocked
#define SCMutexIsLocked(mut)
Definition: threads-debug.h:119
DetectEngineAppInspectionEngine_::GetData
InspectionBufferGetDataPtr GetData
Definition: detect.h:434
DetectEngineThreadKeywordCtxItem_
Definition: detect.h:907
DetectTable
DetectTable
Definition: detect.h:560
DetectEngineCtx_::pkt_mpms_list
DetectBufferMpmRegistry * pkt_mpms_list
Definition: detect.h:1151
Tmq_::pq
PacketQueue * pq
Definition: tm-queues.h:35
Packet_::vlan_idx
uint8_t vlan_idx
Definition: decode.h:544
flow-private.h
Flow_
Flow data structure.
Definition: flow.h:355
TmThreadContinueDetectLoaderThreads
void TmThreadContinueDetectLoaderThreads(void)
Unpauses all threads present in tv_root.
Definition: detect-engine-loader.c:660
DETECT_SM_LIST_THRESHOLD
@ DETECT_SM_LIST_THRESHOLD
Definition: detect.h:133
SCConfYamlHandleInclude
int SCConfYamlHandleInclude(SCConfNode *parent, const char *filename)
Include a file in the configuration.
Definition: conf-yaml-loader.c:115
DetectEngineThreadKeywordCtxItem_::data
void * data
Definition: detect.h:910
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1414
util-hash.h
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its normalized string equivalent.
Definition: app-layer-protos.c:51
DetectEngineReloadTenantBlocking
int DetectEngineReloadTenantBlocking(uint32_t tenant_id, const char *yaml, int reload_cnt)
Reload a tenant and wait for loading to complete.
Definition: detect-engine.c:4418
ctx
struct Thresholds ctx
LiveDevice_
Definition: util-device-private.h:32
DetectEngineCtx_::inspection_recursion_limit
int inspection_recursion_limit
Definition: detect.h:1032
SpmTableElmt_::name
const char * name
Definition: util-spm.h:61
LiveDevice_::tenant_id_set
bool tenant_id_set
Definition: util-device-private.h:36
DetectAddressMapFree
void DetectAddressMapFree(DetectEngineCtx *de_ctx)
Definition: detect-engine-address.c:1340
DetectEngineFrameInspectionEngine::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:523
th_v
ThreadVars * th_v
Definition: fuzz_iprep.c:20
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:987
StringParseUint16
int StringParseUint16(uint16_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:296
DetectEngineReloadSetIdle
void DetectEngineReloadSetIdle(void)
Definition: detect-engine.c:2089
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1656
LiveDevice_::id
uint16_t id
Definition: util-device-private.h:38
DetectEnginePktInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:489
DetectMetadataHashInit
int DetectMetadataHashInit(DetectEngineCtx *de_ctx)
Definition: detect-metadata.c:69
SCConfGet
int SCConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:353
DetectEngineThreadCtx_::global_keyword_ctxs_array
void ** global_keyword_ctxs_array
Definition: detect.h:1435
DetectEngineGetCurrent
DetectEngineCtx * DetectEngineGetCurrent(void)
Definition: detect-engine.c:4108
TransformData_::options
void * options
Definition: detect.h:391
SCInspectionBufferSetupAndApplyTransforms
void SCInspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine-inspect-buffer.c:197
DetectEngineFrameInspectionEngine::mpm
bool mpm
Definition: detect.h:517
SCDetectRateFilterFunc
uint8_t(* SCDetectRateFilterFunc)(const Packet *p, uint32_t sid, uint32_t gid, uint32_t rev, uint8_t original_action, uint8_t new_action, void *arg)
Function type for rate filter callback.
Definition: detect.h:983
DetectFirewallPolicies::pkt_policy_signatures
Signature * pkt_policy_signatures[DETECT_FIREWALL_POLICY_SIZE]
Definition: detect.h:957
TmThreadCountThreadsByTmmFlags
uint32_t TmThreadCountThreadsByTmmFlags(uint8_t flags)
returns a count of all the threads that match the flag
Definition: tm-threads.c:2072
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1403
HashListTableGetListHead
HashListTableBucket * HashListTableGetListHead(HashListTable *ht)
Definition: util-hashlist.c:287
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
LiveDeviceGetById
LiveDevice * LiveDeviceGetById(const int id)
Definition: util-device.c:460
DetectEngineInspectPacketPayload
uint8_t DetectEngineInspectPacketPayload(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, Flow *f, Packet *p)
Do the content inspection & validation for a signature.
Definition: detect-engine-payload.c:152
DetectEngineAddToMaster
int DetectEngineAddToMaster(DetectEngineCtx *de_ctx)
Definition: detect-engine.c:4933
DetectEngineCtx_::mpm_cfg
MpmConfig * mpm_cfg
Definition: detect.h:994
InspectionBufferGetPktDataPtr
InspectionBuffer *(* InspectionBufferGetPktDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Packet *p, const int list_id)
Definition: detect.h:483
DetectAppLayerMultiRegisterSubState
void DetectAppLayerMultiRegisterSubState(const char *name, AppProto alproto, uint32_t dir, uint8_t sub_state, uint8_t progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2283
SCSigSignatureOrderingModuleCleanup
void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx)
De-registers all the signature ordering functions registered.
Definition: detect-engine-sigorder.c:946
DetectEngineBufferTypeGetNameById
const char * DetectEngineBufferTypeGetNameById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1483
DetectEngineCtx_::keyword_id
int keyword_id
Definition: detect.h:1097
MpmTableElmt_::CacheStatsPrint
void(* CacheStatsPrint)(void *data)
Definition: util-mpm.h:195
DetectEngineBufferTypeGetByIdTransforms
int DetectEngineBufferTypeGetByIdTransforms(DetectEngineCtx *de_ctx, const int id, TransformData *transforms, uint8_t transform_cnt)
Definition: detect-engine.c:1829
DetectEngineMultiTenantSetup
int DetectEngineMultiTenantSetup(const bool unix_socket)
setup multi-detect / multi-tenancy
Definition: detect-engine.c:4567
PrefilterDeinit
void PrefilterDeinit(DetectEngineCtx *de_ctx)
Definition: detect-engine-prefilter.c:1517
SignatureProperties
Definition: detect.h:89
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
DetectBufferMpmRegistry_::next
struct DetectBufferMpmRegistry_ * next
Definition: detect.h:826
util-var-name.h
SIG_FLAG_REQUIRE_STREAM
#define SIG_FLAG_REQUIRE_STREAM
Definition: detect.h:257
HashTable_
Definition: util-hash.h:35
DetectEngineTenantMapping_::next
struct DetectEngineTenantMapping_ * next
Definition: detect.h:1734
DetectEngineSetEvent
void DetectEngineSetEvent(DetectEngineThreadCtx *det_ctx, uint8_t e)
Definition: detect-engine.c:5293
SIG_FLAG_TXBOTHDIR
#define SIG_FLAG_TXBOTHDIR
Definition: detect.h:252
DetectEngineThreadCtx_::buffers_size
uint32_t buffers_size
Definition: detect.h:1365
u8_tolower
#define u8_tolower(c)
Definition: suricata-common.h:461
DetectContentInspectionMatchOnAbsentBuffer
bool DetectContentInspectionMatchOnAbsentBuffer(const SigMatchData *smd)
tells if we should match on absent buffer, because there is an absent keyword being used
Definition: detect-engine-content-inspection.c:789
SCConfGetBool
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition: conf.c:524
DetectEngineCtx_::profile_sgh_ctx
struct SCProfileSghDetectCtx_ * profile_sgh_ctx
Definition: detect.h:1110
tv_root
ThreadVars * tv_root[TVT_MAX]
Definition: tm-threads.c:84
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:783
SCReferenceConfDeinit
void SCReferenceConfDeinit(DetectEngineCtx *de_ctx)
Definition: util-reference-config.c:72
DE_QUIET
#define DE_QUIET
Definition: detect.h:333
DetectPort_::next
struct DetectPort_ * next
Definition: detect.h:236
DetectEngineCtx_::tcp_priorityports
DetectPort * tcp_priorityports
Definition: detect.h:1130
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *de_ctx)
Free a DetectEngineCtx::
Definition: detect-engine.c:2878
DetectEngineThreadCtx_::counter_alerts_overflow
StatsCounterId counter_alerts_overflow
Definition: detect.h:1353
DetectEngineAppInspectionEngine_::sm_list_base
uint16_t sm_list_base
Definition: detect.h:428
SigTableElmt_::TransformId
void(* TransformId)(const uint8_t **data, uint32_t *length, const void *context)
Definition: detect.h:1503
DetectEngineThreadCtx_::spm_thread_ctx
SpmThreadCtx * spm_thread_ctx
Definition: detect.h:1338
DetectEngineReloadTenantsBlocking
int DetectEngineReloadTenantsBlocking(const int reload_cnt)
Reload all tenants and wait for loading to complete.
Definition: detect-engine.c:4432
InspectionBuffer::flags
uint8_t flags
Definition: detect-engine-inspect-buffer.h:39
detect-engine-frame.h
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:122
Signature_::sm_arrays
SigMatchData * sm_arrays[DETECT_SM_LIST_MAX]
Definition: detect.h:751
SCInstance_::conf_filename
const char * conf_filename
Definition: suricata.h:178
DetectEngineCtx_::prefilter_setting
enum DetectEnginePrefilterSetting prefilter_setting
Definition: detect.h:1126
SignatureInitData_::init_flags
uint32_t init_flags
Definition: detect.h:621
DetectParseDupSigHashFree
void DetectParseDupSigHashFree(DetectEngineCtx *de_ctx)
Frees the hash table that is used to cull duplicate sigs.
Definition: detect-parse.c:3604
DetectBufferType_
Definition: detect.h:453
m
SCMutex m
Definition: flow-hash.h:6
DetectEngineCtx_::udp_priorityports
DetectPort * udp_priorityports
Definition: detect.h:1131
DetectContentData_
Definition: detect-content.h:93
SCConfYamlLoadFileWithPrefix
int SCConfYamlLoadFileWithPrefix(const char *filename, const char *prefix)
Load configuration from a YAML file, insert in tree at 'prefix'.
Definition: conf-yaml-loader.c:566
p
Packet * p
Definition: fuzz_iprep.c:21
StringParseInt32
int StringParseInt32(int32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:587
PrefilterGenericMpmFrameRegister
int PrefilterGenericMpmFrameRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-frame.c:209
ENGINE_SGH_MPM_FACTORY_CONTEXT_FULL
@ ENGINE_SGH_MPM_FACTORY_CONTEXT_FULL
Definition: detect.h:1245
DetectEngineGetVersion
uint32_t DetectEngineGetVersion(void)
Definition: detect-engine.c:4089
DetectEngineThreadCtx_::counter_alerts_suppressed
StatsCounterId counter_alerts_suppressed
Definition: detect.h:1357
SCConfNodeLookupChildValue
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:878
SleepUsec
#define SleepUsec(usec)
Definition: tm-threads.h:44
SigCleanSignatures
void SigCleanSignatures(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:56
DetectEngineThreadCtx_::lua_instruction_limit_errors
StatsCounterId lua_instruction_limit_errors
Definition: detect.h:1447
HashListTableLookup
void * HashListTableLookup(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:245
detect-engine-payload.h
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3859
DetectEngineThreadCtx_::counter_firewall_discarded_alerts
StatsCounterId counter_firewall_discarded_alerts
Definition: detect.h:1355
EngineModeIsFirewall
bool EngineModeIsFirewall(void)
Definition: suricata.c:239
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:274
DetectMd5ValidateCallback
bool DetectMd5ValidateCallback(const Signature *s, const char **sigerror, const DetectBufferType *map)
Definition: detect-engine.c:5299
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
DetectEngineMpmCachingEnabled
bool DetectEngineMpmCachingEnabled(void)
Definition: detect-engine.c:2649
DetectBufferIsPresent
bool DetectBufferIsPresent(const Signature *s, const uint32_t buf_id)
Definition: detect-engine.c:1678
SigMatchData_
Data needed for Match()
Definition: detect.h:368
KEYWORD_PROFILING_START
#define KEYWORD_PROFILING_START
Definition: util-profiling.h:50
SigMatchData_::type
uint16_t type
Definition: detect.h:369
DetectEngineCtx_::version
uint32_t version
Definition: detect.h:1072
DetectPort_::port2
uint16_t port2
Definition: detect.h:224
StatsRegisterAvgCounter
StatsCounterAvgId StatsRegisterAvgCounter(const char *name, StatsThreadContext *stats)
Registers a counter, whose value holds the average of all the values assigned to it.
Definition: counters.c:1058
DetectEngineCtx_::non_pf_engine_names
HashTable * non_pf_engine_names
Definition: detect.h:1204
SCDetectThreadCtxGetGlobalKeywordThreadCtx
void * SCDetectThreadCtxGetGlobalKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:4063
DetectEngineThreadCtx_::events
uint16_t events
Definition: detect.h:1438
detect-engine-prefilter.h
util-unittest.h
TransformData_
Definition: detect.h:389
PrefilterMultiGenericMpmRegister
int PrefilterMultiGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1751
Signature_::frame_inspect
DetectEngineFrameInspectionEngine * frame_inspect
Definition: detect.h:747
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:276
DetectBufferTypeCloseRegistration
void DetectBufferTypeCloseRegistration(void)
Definition: detect-engine.c:1822
DetectEnginePktInspectionEngine::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:497
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
DetectBufferType_::SetupCallback
void(* SetupCallback)(const struct DetectEngineCtx_ *, struct Signature_ *, const struct DetectBufferType_ *)
Definition: detect.h:463
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
HashTableFree
void HashTableFree(HashTable *ht)
Free a HashTable and all its contents.
Definition: util-hash.c:112
MpmConfig_::cache_dir_path
const char * cache_dir_path
Definition: util-mpm.h:106
TenantLoaderCtx_
Definition: detect-engine.c:4287
SCAppLayerDecoderEventsSetEventRaw
void SCAppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
Set an app layer decoder event.
Definition: app-layer-events.c:96
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1453
KEYWORD_PROFILING_END
#define KEYWORD_PROFILING_END(ctx, type, m)
Definition: util-profiling.h:64
DetectBufferTypeSupportsPacket
void DetectBufferTypeSupportsPacket(const char *name)
Definition: detect-engine.c:1423
DetectEngineFrameInspectionEngine::Callback
InspectionBufferFrameInspectFunc Callback
Definition: detect.h:521
HashListTableAdd
int HashListTableAdd(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:114
HashTable_::array_size
uint32_t array_size
Definition: util-hash.h:37
SigGroupHeadHashInit
int SigGroupHeadHashInit(DetectEngineCtx *de_ctx)
Initializes the hash table in the detection engine context to hold the SigGroupHeads.
Definition: detect-engine-siggroup.c:244
InspectionBufferMultipleForList::size
uint32_t size
Definition: detect.h:384
SCRConfDeInitContext
void SCRConfDeInitContext(DetectEngineCtx *de_ctx)
Releases de_ctx resources related to Reference Config API.
Definition: util-reference-config.c:180
DetectEngineThreadCtx_::mt_det_ctxs_hash
HashTable * mt_det_ctxs_hash
Definition: detect.h:1318
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
DetectEnginePktInspectionRun
bool DetectEnginePktInspectionRun(ThreadVars *tv, DetectEngineThreadCtx *det_ctx, const Signature *s, Flow *f, Packet *p, uint8_t *alert_flags)
Definition: detect-engine.c:1979
DETECT_PREFILTER_AUTO
@ DETECT_PREFILTER_AUTO
Definition: detect.h:919
DetectEngineThreadCtx_::keyword_ctxs_size
int keyword_ctxs_size
Definition: detect.h:1432
HashListTable_::array_size
uint32_t array_size
Definition: util-hashlist.h:41
DetectAppLayerMpmRegisterByParentId
void DetectAppLayerMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id, DetectEngineTransforms *transforms)
copy a mpm engine from parent_id, add in transforms
Definition: detect-engine-mpm.c:269
AlertQueueInit
void AlertQueueInit(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine-alert.c:269
SigString_::sig_error
char * sig_error
Definition: detect.h:892
DetectEngineAppInspectionEngine_::id
uint8_t id
Definition: detect.h:422
PacketQueue_::mutex_q
SCMutex mutex_q
Definition: packet-queue.h:56
DetectEngineCtx_::base64_decode_max_len
uint16_t base64_decode_max_len
Definition: detect.h:1082
SIGNATURE_HOOK_TYPE_APP
@ SIGNATURE_HOOK_TYPE_APP
Definition: detect.h:554
THV_RUNNING_DONE
#define THV_RUNNING_DONE
Definition: threadvars.h:46
PKT_SET_SRC
#define PKT_SET_SRC(p, src_val)
Definition: decode.h:1366
SCConfInit
void SCConfInit(void)
Initialize the configuration system.
Definition: conf.c:121
DetectEngineThreadCtx_::multi_inspect
struct DetectEngineThreadCtx_::@102 multi_inspect
util-signal.h
SCConfDump
void SCConfDump(void)
Dump configuration to stdout.
Definition: conf.c:818
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:427
TENANT_SELECTOR_UNKNOWN
@ TENANT_SELECTOR_UNKNOWN
Definition: detect.h:1722
HashListTableGetListNext
#define HashListTableGetListNext(hb)
Definition: util-hashlist.h:55
InspectionBufferMultipleForList
Definition: detect.h:382
DetectEngineTenantMapping_::tenant_id
uint32_t tenant_id
Definition: detect.h:1729
ThreadVars_::tmm_flags
uint8_t tmm_flags
Definition: threadvars.h:78
StringParseUint8
int StringParseUint8(uint8_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:323
DETECT_SM_LIST_POSTMATCH
@ DETECT_SM_LIST_POSTMATCH
Definition: detect.h:127
MPM_AC_KS
@ MPM_AC_KS
Definition: util-mpm.h:53
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:273
app-layer-htp.h
DetectEngineTenantSelectors
DetectEngineTenantSelectors
Definition: detect.h:1721
DetectPortParse
int DetectPortParse(const DetectEngineCtx *de_ctx, DetectPort **head, const char *str)
Function for parsing port strings.
Definition: detect-engine-port.c:1135
InspectionBufferPktInspectFunc
int(* InspectionBufferPktInspectFunc)(struct DetectEngineThreadCtx_ *, const struct DetectEnginePktInspectionEngine *engine, const struct Signature_ *s, Packet *p, uint8_t *alert_flags)
Definition: detect.h:476
datasets.h
InspectionBufferFree
void InspectionBufferFree(InspectionBuffer *buffer)
Definition: detect-engine-inspect-buffer.c:205
HashListTableInit
HashListTable * HashListTableInit(uint32_t size, uint32_t(*Hash)(struct HashListTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hashlist.c:35
DetectEngineCtx_::requirements
SCDetectRequiresStatus * requirements
Definition: detect.h:1197
TAILQ_REMOVE
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:312
SCRunmodeGet
SCRunMode SCRunmodeGet(void)
Get the current run mode.
Definition: suricata.c:301
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
DetectBufferType_::ValidateCallback
bool(* ValidateCallback)(const struct Signature_ *, const char **sigerror, const struct DetectBufferType_ *)
Definition: detect.h:465
DetectEngineMoveToFreeList
int DetectEngineMoveToFreeList(DetectEngineCtx *de_ctx)
Definition: detect-engine.c:4993
DetectEngineCtx_::fw_policies
struct DetectFirewallPolicies * fw_policies
Definition: detect.h:1018
SigTableElmt_::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, const void *context)
Definition: detect.h:1500
DetectEngineThreadCtx_::counter_mpm_list
StatsCounterAvgId counter_mpm_list
Definition: detect.h:1359
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
util-error.h
DetectTableToString
const char * DetectTableToString(enum DetectTable table)
Definition: detect-engine.c:134
SpmInitGlobalThreadCtx
SpmGlobalThreadCtx * SpmInitGlobalThreadCtx(uint8_t matcher)
Definition: util-spm.c:148
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
SCProfilingPrefilterDestroyCtx
void SCProfilingPrefilterDestroyCtx(DetectEngineCtx *de_ctx)
Definition: util-profiling-prefilter.c:236
DetectEnginePktInspectionEngine::sm_list
uint16_t sm_list
Definition: detect.h:491
DetectEngineThreadCtx_::match_array_len
uint32_t match_array_len
Definition: detect.h:1403
DetectMpmInitializePktMpms
void DetectMpmInitializePktMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:710
DetectAppLayerMpmMultiRegisterSubState
void DetectAppLayerMpmMultiRegisterSubState(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionMultiBufferGetDataPtr GetData, AppProto alproto, uint8_t sub_state, uint8_t tx_min_progress)
Definition: detect-engine-mpm.c:192
DetectEngineThreadCtx_
Definition: detect.h:1306
DetectEngineThreadCtx_::lua_memory_limit_errors
StatsCounterId lua_memory_limit_errors
Definition: detect.h:1450
PKT_STREAM_ADD
#define PKT_STREAM_ADD
Definition: decode.h:1305
DetectLoadersInit
void DetectLoadersInit(void)
Definition: detect-engine-loader.c:615
ThreadVars_::tm_slots
struct TmSlot_ * tm_slots
Definition: threadvars.h:95
SignatureInitData_::mpm_sm
SigMatch * mpm_sm
Definition: detect.h:636
DetectEngineBufferTypeGetDescriptionById
const char * DetectEngineBufferTypeGetDescriptionById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1561
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
DETECT_SM_LIST_BASE64_DATA
@ DETECT_SM_LIST_BASE64_DATA
Definition: detect.h:124
SIG_FLAG_FLUSH
#define SIG_FLAG_FLUSH
Definition: detect.h:261
DetectEngineThreadKeywordCtxItem_::id
int id
Definition: detect.h:912
ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE
@ ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE
Definition: detect.h:1246
PKT_PSEUDO_STREAM_END
#define PKT_PSEUDO_STREAM_END
Definition: decode.h:1313
DetectEngineThreadCtx_::buffers
InspectionBuffer * buffers
Definition: detect.h:1364
detect-engine-file.h
DetectEngineMasterCtx_::keyword_list
DetectEngineThreadKeywordCtxItem * keyword_list
Definition: detect.h:1764
SignatureInitData_::mpm_sm_list
int mpm_sm_list
Definition: detect.h:634
LiveGetDevice
LiveDevice * LiveGetDevice(const char *name)
Get a pointer to the device at idx.
Definition: util-device.c:269
flow-worker.h
SCConfGetInt
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:441
DetectEngineMasterCtx_::tenant_selector
enum DetectEngineTenantSelectors tenant_selector
Definition: detect.h:1755
DetectEngineCtx_::keyword_hash
HashListTable * keyword_hash
Definition: detect.h:1099
BOOL2STR
#define BOOL2STR(b)
Definition: util-debug.h:542
DetectPktInspectEngineRegister
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
Definition: detect-engine.c:159
DetectEngineInspectStreamPayload
int DetectEngineInspectStreamPayload(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, Flow *f, Packet *p)
Do the content inspection & validation for a signature on the raw stream.
Definition: detect-engine-payload.c:248
util-reference-config.h
Packet_::pcap_v
PcapPacketVars pcap_v
Definition: decode.h:602
DetectEngineCtx_::failure_fatal
bool failure_fatal
Definition: detect.h:988
Signature_::pkt_inspect
DetectEnginePktInspectionEngine * pkt_inspect
Definition: detect.h:746
DetectEngineAppInspectionEngine_::GetMultiData
InspectionMultiBufferGetDataPtr GetMultiData
Definition: detect.h:436
DetectEngineCtx_::max_flowbits
uint8_t max_flowbits
Definition: detect.h:991
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
DetectMetadataHashFree
void DetectMetadataHashFree(DetectEngineCtx *de_ctx)
Definition: detect-metadata.c:80
DetectBufferTypeMaxId
int DetectBufferTypeMaxId(void)
Definition: detect-engine.c:1210
DatasetPostReloadCleanup
void DatasetPostReloadCleanup(void)
Definition: datasets.c:565
SCConfGetNonNull
int SCConfGetNonNull(const char *name, const char **vptr)
Retrieve the non-null value of a configuration node.
Definition: conf.c:381
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DetectEngineBufferTypeValidateTransform
bool DetectEngineBufferTypeValidateTransform(DetectEngineCtx *de_ctx, int sm_list, const uint8_t *content, uint16_t content_len, const char **namestr)
Check content byte array compatibility with transforms.
Definition: detect-engine.c:1706
DetectEngineCtx_::sm_types_prefilter
bool * sm_types_prefilter
Definition: detect.h:1173
DetectEngineEnabled
int DetectEngineEnabled(void)
Check if detection is enabled.
Definition: detect-engine.c:4075
SigMatchList2DataArray
SigMatchData * SigMatchList2DataArray(SigMatch *head)
convert SigMatch list to SigMatchData array
Definition: detect-parse.c:2654
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3626
MpmTableElmt_::CacheStatsDeinit
void(* CacheStatsDeinit)(void *data)
Definition: util-mpm.h:196
DetectEngineReloadIsIdle
int DetectEngineReloadIsIdle(void)
Definition: detect-engine.c:2097
DetectEngineFrameInspectionEngine::sm_list
uint16_t sm_list
Definition: detect.h:518
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:1350
InspectionSingleBufferGetDataPtr
bool(* InspectionSingleBufferGetDataPtr)(const void *txv, const uint8_t flow_flags, const uint8_t **buf, uint32_t *buf_len)
Definition: detect-engine-helper.h:45
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:363
DetectEngineCtx_::mpm_matcher
uint8_t mpm_matcher
Definition: detect.h:990
DETECT_TABLE_PACKET_PRE_FLOW
@ DETECT_TABLE_PACKET_PRE_FLOW
Definition: detect.h:562
detect-engine-port.h
LiveDevice_::tenant_id
uint32_t tenant_id
Definition: util-device-private.h:46
SCClassConfDeinit
void SCClassConfDeinit(DetectEngineCtx *de_ctx)
Definition: util-classification-config.c:81
InspectionBuffer::inspect_offset
uint64_t inspect_offset
Definition: detect-engine-inspect-buffer.h:36
DETECT_TABLE_PACKET_FILTER
@ DETECT_TABLE_PACKET_FILTER
Definition: detect.h:564
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
DetectEngineCtx_::frame_inspect_engines
DetectEngineFrameInspectionEngine * frame_inspect_engines
Definition: detect.h:1153
DetectEngineMasterCtx_::free_list
DetectEngineCtx * free_list
Definition: detect.h:1753
StringParseUint32
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:269
DetectEngineSyncState
DetectEngineSyncState
Definition: detect-engine.c:2049
LiveDevice_::dev
char * dev
Definition: util-device-private.h:33
DetectEngineThreadKeywordCtxItem_::next
struct DetectEngineThreadKeywordCtxItem_ * next
Definition: detect.h:911
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
HashTableAdd
int HashTableAdd(HashTable *ht, void *data, uint16_t datalen)
Definition: util-hash.c:132
DetectEngineBufferTypeRegister
int DetectEngineBufferTypeRegister(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1540
DetectPort_
Port structure for detection engine.
Definition: detect.h:222
DetectEngineThreadCtx_::json_content_capacity
uint8_t json_content_capacity
Definition: detect.h:1344
app-layer-parser.h
Signature_::app_inspect
DetectEngineAppInspectionEngine * app_inspect
Definition: detect.h:745
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:362
ThreadVars_::next
struct ThreadVars_ * next
Definition: threadvars.h:124
DetectEngineContentInspection
bool DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, const uint32_t buffer_len, const uint64_t stream_start_offset, const uint8_t flags, const enum DetectContentInspectionType inspection_mode)
wrapper around DetectEngineContentInspectionInternal to return true/false only
Definition: detect-engine-content-inspection.c:751
SignatureInitData_::hook
SignatureHook hook
Definition: detect.h:600
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:1225
DetectEngineThreadCtx_::lua_rule_errors
StatsCounterId lua_rule_errors
Definition: detect.h:1441
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:325
hashlittle_safe
uint32_t hashlittle_safe(const void *key, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:482
DetectEngineThreadCtx_::base64_decoded_len
int base64_decoded_len
Definition: detect.h:1389
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2371
detect-engine-tag.h
util-profiling.h
tv_root_lock
SCMutex tv_root_lock
Definition: tm-threads.c:87
DetectEngineInspectStream
uint8_t DetectEngineInspectStream(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
inspect engine for stateful rules
Definition: detect-engine-payload.c:298
DetectEngineLoadTenantBlocking
int DetectEngineLoadTenantBlocking(uint32_t tenant_id, const char *yaml)
Load a tenant and wait for loading to complete.
Definition: detect-engine.c:4404
TmModuleDetectLoaderRegister
void TmModuleDetectLoaderRegister(void)
Definition: detect-engine-loader.c:776
Signature_::flags
uint32_t flags
Definition: detect.h:689
DetectEngineFrameInspectionEngine::v1
struct DetectEngineFrameInspectionEngine::@86 v1
DetectBufferType_::xform_id
TransformIdData xform_id[DETECT_TRANSFORMS_MAX]
Definition: detect.h:468
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2839
RuleMatchCandidateTxArrayFree
void RuleMatchCandidateTxArrayFree(DetectEngineThreadCtx *det_ctx)
Definition: detect.c:1193
DetectEngineInitializeFastPatternList
void DetectEngineInitializeFastPatternList(DetectEngineCtx *de_ctx)
Definition: detect-fast-pattern.c:149
Packet_
Definition: decode.h:516
DetectEngineFrameInspectionEngine::alproto
AppProto alproto
Definition: detect.h:514
DE_STATE_FLAG_BASE
#define DE_STATE_FLAG_BASE
Definition: detect-engine-state.h:66
detect-engine-build.h
PatternMatchThreadPrepare
void PatternMatchThreadPrepare(MpmThreadCtx *mpm_thread_ctx, DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:993
conf-yaml-loader.h
SCRConfLoadReferenceConfigFile
int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Reference info from the reference.config file.
Definition: util-reference-config.c:481
DetectEngineTenantRegisterLivedev
int DetectEngineTenantRegisterLivedev(uint32_t tenant_id, int device_id)
Definition: detect-engine.c:4850
PcapPacketVars_::tenant_id
uint32_t tenant_id
Definition: source-pcap.h:39
DETECT_CI_FLAGS_END
#define DETECT_CI_FLAGS_END
Definition: detect-engine-content-inspection.h:42
DetectEngineBufferTypeSupportsFrames
void DetectEngineBufferTypeSupportsFrames(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1570
InspectionBufferFrameInspectFunc
int(* InspectionBufferFrameInspectFunc)(struct DetectEngineThreadCtx_ *, const struct DetectEngineFrameInspectionEngine *engine, const struct Signature_ *s, Packet *p, const struct Frames *frames, const struct Frame *frame)
Definition: detect.h:509
MpmTableElmt_::CacheStatsInit
void *(* CacheStatsInit)(void)
Definition: util-mpm.h:194
DetectEngineCtx_::frame_mpms_list
DetectBufferMpmRegistry * frame_mpms_list
Definition: detect.h:1154
MPM_AC
@ MPM_AC
Definition: util-mpm.h:52
detect-engine-alert.h
conf.h
DetectEngineCtx_::sgh_mpm_ctx_cnf
uint8_t sgh_mpm_ctx_cnf
Definition: detect.h:1095
DetectEngineAppInspectionEngine_::match_on_null
bool match_on_null
Definition: detect.h:426
DetectBufferType_::packet
bool packet
Definition: detect.h:459
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
DetectEngineMasterCtx_::multi_tenant_enabled
int multi_tenant_enabled
Definition: detect.h:1741
TmModuleGetById
TmModule * TmModuleGetById(int id)
Returns a TM Module by its id.
Definition: tm-modules.c:69
TmSlot_
Definition: tm-threads.h:53
DetectEngineFrameInspectionEngine
Definition: detect.h:513
TenantLoaderCtx
struct TenantLoaderCtx_ TenantLoaderCtx
ENGINE_PROFILE_MEDIUM
@ ENGINE_PROFILE_MEDIUM
Definition: detect.h:1238
util-magic.h
DetectEngineCtx_::max_uniq_toserver_groups
uint16_t max_uniq_toserver_groups
Definition: detect.h:1050
MpmTableElmt_::CacheRuleset
int(* CacheRuleset)(MpmConfig *)
Definition: util-mpm.h:197
DetectEngineBufferTypeGetById
const DetectBufferType * DetectEngineBufferTypeGetById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1473
DETECT_TABLE_PACKET_TD
@ DETECT_TABLE_PACKET_TD
Definition: detect.h:565
TmEcode
TmEcode
Definition: tm-threads-common.h:80
DetectEnginePktInspectionEngine::Callback
InspectionBufferPktInspectFunc Callback
Definition: detect.h:495
ThresholdCacheThreadInit
int ThresholdCacheThreadInit(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine-threshold.c:702
DetectBufferType_::name
char name[64]
Definition: detect.h:454
ALPROTO_DOH2
@ ALPROTO_DOH2
Definition: app-layer-protos.h:66
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:767
DetectEngineSyncer_::state
enum DetectEngineSyncState state
Definition: detect-engine.c:2057
SpmMakeThreadCtx
SpmThreadCtx * SpmMakeThreadCtx(const SpmGlobalThreadCtx *global_thread_ctx)
Definition: util-spm.c:163
SCReferenceSCConfInit
void SCReferenceSCConfInit(DetectEngineCtx *de_ctx)
Definition: util-reference-config.c:52
DetectEngineCtx_::profile_keyword_ctx
struct SCProfileKeywordDetectCtx_ * profile_keyword_ctx
Definition: detect.h:1107
DetectEngineAppInspectionEngine_::GetDataSingle
InspectionSingleBufferGetDataPtr GetDataSingle
Definition: detect.h:435
SCInstance_::additional_configs
const char ** additional_configs
Definition: suricata.h:179
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1486
reputation.h
SCProfilingPrefilterThreadCleanup
void SCProfilingPrefilterThreadCleanup(DetectEngineThreadCtx *det_ctx)
Definition: util-profiling-prefilter.c:281
SCConfCreateContextBackup
void SCConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
Definition: conf.c:741
util-action.h
HashListTable_
Definition: util-hashlist.h:37
DetectEngineThreadCtxGetJsonContext
int DetectEngineThreadCtxGetJsonContext(DetectEngineThreadCtx *det_ctx)
Definition: detect-engine.c:5384
DetectEngineCtx_::byte_extract_max_local_id
int32_t byte_extract_max_local_id
Definition: detect.h:1069
DetectEngineTransforms::transforms
TransformData transforms[DETECT_TRANSFORMS_MAX]
Definition: detect.h:395
DetectEnginePktInspectionEngine::sm_list_base
uint16_t sm_list_base
Definition: detect.h:492
SIG_PROP_FLOW_ACTION_PACKET
@ SIG_PROP_FLOW_ACTION_PACKET
Definition: detect.h:84
DetectEngineMultiTenantEnabled
bool DetectEngineMultiTenantEnabled(void)
Definition: detect-engine.c:4145
signature_properties
const struct SignatureProperties signature_properties[SIG_TYPE_MAX]
Definition: detect-engine.c:119
DetectEngineCtx_::RateFilterCallback
SCDetectRateFilterFunc RateFilterCallback
Definition: detect.h:1209
runmodes.h
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:292
PacketQueue_::cond_q
SCCondT cond_q
Definition: packet-queue.h:57
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
TAILQ_FOREACH_SAFE
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:329
DetectEngineFrameInspectionEngine::dir
uint8_t dir
Definition: detect.h:515
SIG_FLAG_REQUIRE_STREAM_ONLY
#define SIG_FLAG_REQUIRE_STREAM_ONLY
Definition: detect.h:263
SpmDestroyGlobalThreadCtx
void SpmDestroyGlobalThreadCtx(SpmGlobalThreadCtx *global_thread_ctx)
Definition: util-spm.c:154
DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
#define DETECT_ENGINE_INSPECT_SIG_CANT_MATCH
Definition: detect-engine-state.h:42
DetectEngineThreadCtx_::mtc
MpmThreadCtx mtc
Definition: detect.h:1410
DetectEngineRegisterTests
void DetectEngineRegisterTests(void)
Definition: detect-engine.c:5656
SigString_::filename
char * filename
Definition: detect.h:890
DetectBufferType_::multi_instance
bool multi_instance
Definition: detect.h:462
DetectLoaderQueueTask
int DetectLoaderQueueTask(int loader_id, LoaderFunc Func, void *func_ctx, LoaderFreeFunc FreeFunc)
Definition: detect-engine-loader.c:538
DETECT_ENGINE_TYPE_TENANT
@ DETECT_ENGINE_TYPE_TENANT
Definition: detect.h:927
DetectEngineBufferTypeSupportsTransformations
void DetectEngineBufferTypeSupportsTransformations(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1594
PACKET_ALERT_FLAG_STREAM_MATCH
#define PACKET_ALERT_FLAG_STREAM_MATCH
Definition: decode.h:272
InspectionMultiBufferGetDataPtr
bool(* InspectionMultiBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const void *txv, const uint8_t flow_flags, uint32_t local_id, const uint8_t **buf, uint32_t *buf_len)
Definition: detect-engine-helper.h:42
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2300
DetectEngineBufferTypeSupportsMpmGetById
bool DetectEngineBufferTypeSupportsMpmGetById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1620
DetectAppLayerMpmMultiRegister
void DetectAppLayerMpmMultiRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionMultiBufferGetDataPtr GetData, AppProto alproto, uint8_t tx_min_progress)
Definition: detect-engine-mpm.c:184
DetectEngineCtx_::config_prefix
char config_prefix[64]
Definition: detect.h:1113
DetectSigmatchListEnumToString
const char * DetectSigmatchListEnumToString(enum DetectSigmatchListEnum type)
Definition: detect-engine.c:5264
TmModule_
Definition: tm-modules.h:47
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
DetectEngineAppInspectionEngine_::alproto
AppProto alproto
Definition: detect.h:420
TVT_PPT
@ TVT_PPT
Definition: tm-threads-common.h:88
MpmConfig_::cache_max_age_seconds
uint64_t cache_max_age_seconds
Definition: util-mpm.h:107
SRepReloadComplete
void SRepReloadComplete(void)
Increment effective reputation version after a rule/reputation reload is complete.
Definition: reputation.c:161
SIG_FLAG_INIT_STATE_MATCH
#define SIG_FLAG_INIT_STATE_MATCH
Definition: detect.h:299
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:443
DetectEngineBufferTypeSupportsFramesGetById
bool DetectEngineBufferTypeSupportsFramesGetById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1629
DetectBufferType_::id
int id
Definition: detect.h:456
DetectEngineCtx_::spm_matcher
uint8_t spm_matcher
Definition: detect.h:995
DetectEngineBufferTypeRegisterWithFrameEngines
int DetectEngineBufferTypeRegisterWithFrameEngines(DetectEngineCtx *de_ctx, const char *name, const int direction, const AppProto alproto, const uint8_t frame_type)
Definition: detect-engine.c:1506
DetectEnginePktInspectionEngine::GetData
InspectionBufferGetPktDataPtr GetData
Definition: detect.h:494
SCConfNodeLookupChild
SCConfNode * SCConfNodeLookupChild(const SCConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:850
ENGINE_PROFILE_UNKNOWN
@ ENGINE_PROFILE_UNKNOWN
Definition: detect.h:1236
DETECT_ENGINE_TYPE_NORMAL
@ DETECT_ENGINE_TYPE_NORMAL
Definition: detect.h:924
FlowWorkerGetDetectCtxPtr
void * FlowWorkerGetDetectCtxPtr(void *flow_worker)
Definition: flow-worker.c:743
detect-fast-pattern.h
APP_LAYER_MAX_PROGRESS
#define APP_LAYER_MAX_PROGRESS
Definition: app-layer-parser.h:75
DetectBufferType_::frame
bool frame
Definition: detect.h:460
DetectEngineBufferTypeSupportsMultiInstanceGetById
bool DetectEngineBufferTypeSupportsMultiInstanceGetById(const DetectEngineCtx *de_ctx, const int id)
Definition: detect-engine.c:1602
SCCondSignal
#define SCCondSignal
Definition: threads-debug.h:140
ThreadVars_::inq
Tmq * inq
Definition: threadvars.h:89
util-conf.h
DetectEngineCtx_::sig_stat
SigFileLoaderStat sig_stat
Definition: detect.h:1164
Packet_::flow
struct Flow_ * flow
Definition: decode.h:564
DetectEngineFrameMpmRegister
void DetectEngineFrameMpmRegister(DetectEngineCtx *de_ctx, const char *name, int direction, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id), AppProto alproto, uint8_t type)
Definition: detect-engine-mpm.c:480
DetectEngineThreadCtx_::to_clear_queue
uint32_t * to_clear_queue
Definition: detect.h:1367
SpmDestroyThreadCtx
void SpmDestroyThreadCtx(SpmThreadCtx *thread_ctx)
Definition: util-spm.c:173
DetectEngineCtx_::profile_prefilter_ctx
struct SCProfilePrefilterDetectCtx_ * profile_prefilter_ctx
Definition: detect.h:1108
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
TENANT_SELECTOR_DIRECT
@ TENANT_SELECTOR_DIRECT
Definition: detect.h:1723
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:50
DetectEnginePktInspectionSetup
int DetectEnginePktInspectionSetup(Signature *s)
Definition: detect-engine.c:2027
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1389
InspectEngineFuncPtr
uint8_t(* InspectEngineFuncPtr)(struct DetectEngineCtx_ *de_ctx, struct DetectEngineThreadCtx_ *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const struct Signature_ *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Definition: detect.h:414
SleepMsec
#define SleepMsec(msec)
Definition: tm-threads.h:45
flags
uint8_t flags
Definition: decode-gre.h:0
DetectRegisterThreadCtxFuncs
int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *), int mode)
Register Thread keyword context Funcs.
Definition: detect-engine.c:3929
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2295
MpmStoreInit
int MpmStoreInit(DetectEngineCtx *de_ctx)
Initializes the MpmStore mpm hash table to be used by the detection engine context.
Definition: detect-engine-mpm.c:1495
DetectEngineMasterCtx_::keyword_id
int keyword_id
Definition: detect.h:1765
DetectEngineCtx_::app_mpms_list
DetectBufferMpmRegistry * app_mpms_list
Definition: detect.h:1146
suricata-common.h
DetectEnginePktInspectionEngine::v1
struct DetectEnginePktInspectionEngine::@85 v1
SIG_PROP_FLOW_ACTION_FLOW_IF_STATEFUL
@ SIG_PROP_FLOW_ACTION_FLOW_IF_STATEFUL
Definition: detect.h:86
DetectEngineCtxInitStubForMT
DetectEngineCtx * DetectEngineCtxInitStubForMT(void)
Definition: detect-engine.c:2829
SigMatch_::type
uint16_t type
Definition: detect.h:360
DetectBufferTypeSupportsMpm
void DetectBufferTypeSupportsMpm(const char *name)
Definition: detect-engine.c:1433
util-path.h
DetectEngineThreadCtx_::tenant_id
uint32_t tenant_id
Definition: detect.h:1309
HashListTableFree
void HashListTableFree(HashListTable *ht)
Definition: util-hashlist.c:88
DETECT_FIREWALL_POLICY_SIZE
#define DETECT_FIREWALL_POLICY_SIZE
Definition: detect.h:934
detect-byte-extract.h
DetectEngineCtx_::buffer_type_hash_name
HashListTable * buffer_type_hash_name
Definition: detect.h:1141
DetectEngineCtx_::next
struct DetectEngineCtx_ * next
Definition: detect.h:1120
DETECT_TABLE_NOT_SET
@ DETECT_TABLE_NOT_SET
Definition: detect.h:561
AppProtoToStringRaw
const char * AppProtoToStringRaw(AppProto alproto)
Maps the ALPROTO_*, to its registered string equivalent.
Definition: app-layer-protos.c:41
DETECT_ENGINE_TYPE_DD_STUB
@ DETECT_ENGINE_TYPE_DD_STUB
Definition: detect.h:925
TENANT_SELECTOR_VLAN
@ TENANT_SELECTOR_VLAN
Definition: detect.h:1724
DetectEngineBumpVersion
void DetectEngineBumpVersion(void)
Definition: detect-engine.c:4099
DetectEngineFrameInspectionEngine::next
struct DetectEngineFrameInspectionEngine * next
Definition: detect.h:526
SignatureHook_::type
enum SignatureHookType type
Definition: detect.h:580
DetectEngineInspectFrameBufferGeneric
int DetectEngineInspectFrameBufferGeneric(DetectEngineThreadCtx *det_ctx, const DetectEngineFrameInspectionEngine *engine, const Signature *s, Packet *p, const Frames *frames, const Frame *frame)
Do the content inspection & validation for a signature.
Definition: detect-engine-frame.c:563
Packet_::livedev_id
uint16_t livedev_id
Definition: decode.h:633
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3871
util-spm.h
DetectEnginePktInspectionEngine::next
struct DetectEnginePktInspectionEngine * next
Definition: detect.h:499
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
DetectEngineCtx_::rate_filter_callback_arg
void * rate_filter_callback_arg
Definition: detect.h:1212
DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT
#define DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT
Definition: detect-engine.c:96
version
uint8_t version
Definition: decode-gre.h:1
MpmConfig_::cache_stats
void * cache_stats
Definition: util-mpm.h:108
detect-engine-buffer.h
PostRuleMatchWorkQueue::q
PostRuleMatchWorkQueueItem * q
Definition: detect.h:1288
TransformIdData_::id_data_len
uint32_t id_data_len
Definition: detect.h:450
PatternMatchDefaultMatcher
uint8_t PatternMatchDefaultMatcher(void)
Function to return the multi pattern matcher algorithm to be used by the engine, based on the mpm-alg...
Definition: detect-engine-mpm.c:936
AppLayerParserGetSubStateProgressName
const char * AppLayerParserGetSubStateProgressName(const AppProto alproto, const uint8_t sub_state, const uint8_t state, const uint8_t dir_flag)
Definition: app-layer-parser.c:1303
util-classification-config.h
SCConfDeInit
void SCConfDeInit(void)
De-initializes the configuration system.
Definition: conf.c:760
DetectEngineTenantRegisterPcapFile
int DetectEngineTenantRegisterPcapFile(uint32_t tenant_id)
Definition: detect-engine.c:4866
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:517
SigGroupHeadHashFree
void SigGroupHeadHashFree(DetectEngineCtx *de_ctx)
Frees the hash table - DetectEngineCtx->sgh_hash_table, allocated by SigGroupHeadHashInit() function.
Definition: detect-engine-siggroup.c:299
SCAppLayerDecoderEventsFreeEvents
void SCAppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
Definition: app-layer-events.c:137
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2121
DetectEngineAppInspectionEngineSignatureFree
void DetectEngineAppInspectionEngineSignatureFree(DetectEngineCtx *de_ctx, Signature *s)
free app inspect engines for a signature
Definition: detect-engine.c:1104
DETECT_ENGINE_CONTENT_INSPECTION_MODE_HEADER
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_HEADER
Definition: detect-engine-content-inspection.h:33
util-hash-lookup3.h
DETECT_SM_LIST_TMATCH
@ DETECT_SM_LIST_TMATCH
Definition: detect.h:129
DetectEngineCtx_::loader_id
int loader_id
Definition: detect.h:1123
SCInstance_::sig_file
char * sig_file
Definition: suricata.h:139
TransformData_::transform
int transform
Definition: detect.h:390
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:40
DetectEngineCtx_::pkt_inspect_engines
DetectEnginePktInspectionEngine * pkt_inspect_engines
Definition: detect.h:1150
DetectEngineBufferTypeSupportsMpm
void DetectEngineBufferTypeSupportsMpm(DetectEngineCtx *de_ctx, const char *name)
Definition: detect-engine.c:1586
SIG_TYPE_MAX
@ SIG_TYPE_MAX
Definition: detect.h:79
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
DetectEngineCtx_::sm_types_silent_error
bool * sm_types_silent_error
Definition: detect.h:1174
DetectEngineThreadKeywordCtxItem_::FreeFunc
void(* FreeFunc)(void *)
Definition: detect.h:909
DetectAppLayerInspectEngineRegisterSubState
void DetectAppLayerInspectEngineRegisterSubState(const char *name, AppProto alproto, uint32_t dir, uint8_t sub_state, uint8_t progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register an app inspection engine for a tx type
Definition: detect-engine.c:299
app-layer-events.h
SignatureInitDataBuffer_::only_ts
bool only_ts
Definition: detect.h:537
util-validate.h
PacketGetFromAlloc
Packet * PacketGetFromAlloc(void)
Get a malloced packet.
Definition: decode.c:260
DetectEngineFreeFastPatternList
void DetectEngineFreeFastPatternList(DetectEngineCtx *de_ctx)
Definition: detect-fast-pattern.c:171
DetectBufferTypeSupportsTransformations
void DetectBufferTypeSupportsTransformations(const char *name)
Definition: detect-engine.c:1443
detect-engine-sigorder.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
DetectEngineMpmCachingGetPath
const char * DetectEngineMpmCachingGetPath(void)
Definition: detect-engine.c:2658
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
DETECT_ENGINE_MPM_CACHE_OP_SAVE
#define DETECT_ENGINE_MPM_CACHE_OP_SAVE
Definition: detect.h:1774
DetectAddressMapInit
int DetectAddressMapInit(DetectEngineCtx *de_ctx)
Definition: detect-engine-address.c:1329
DetectContentData_::spm_ctx
SpmCtx * spm_ctx
Definition: detect-content.h:111
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect-engine-inspect-buffer.h:37
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:667
DetectEngineCtx_::app_inspect_engines
DetectEngineAppInspectionEngine * app_inspect_engines
Definition: detect.h:1149
DetectEngineCtx_::filemagic_thread_ctx_id
int filemagic_thread_ctx_id
Definition: detect.h:1041
SigJsonContent
Definition: detect.h:1298
DetectEngineSyncer_
Definition: detect-engine.c:2055
DetectEngineThreadCtx_::global_keyword_ctxs_size
int global_keyword_ctxs_size
Definition: detect.h:1434
DetectEngineThreadCtx_::json_content
SigJsonContent * json_content
Definition: detect.h:1343
PrefilterInit
void PrefilterInit(DetectEngineCtx *de_ctx)
Definition: detect-engine-prefilter.c:1524
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
str
#define str(s)
Definition: suricata-common.h:316
DetectEngineThreadCtx_::replace
const Signature ** replace
Definition: detect.h:1400
DetectEngineThreadCtx_::tv
ThreadVars * tv
Definition: detect.h:1314
SCConfGetNode
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
Definition: conf.c:184
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
DETECT_TBLSIZE
int DETECT_TBLSIZE
Definition: detect-engine-register.c:263
SRepInit
int SRepInit(DetectEngineCtx *de_ctx)
init reputation
Definition: reputation.c:557
DetectEngineReloadStart
int DetectEngineReloadStart(void)
Definition: detect-engine.c:2063
DetectEngineUnsetParseMetadata
void DetectEngineUnsetParseMetadata(void)
Definition: detect-engine.c:5254
PKT_SRC_DETECT_RELOAD_FLUSH
@ PKT_SRC_DETECT_RELOAD_FLUSH
Definition: decode.h:61
DetectEngineThreadCtx_::keyword_ctxs_array
void ** keyword_ctxs_array
Definition: detect.h:1431
SCFree
#define SCFree(p)
Definition: util-mem.h:61
AppLayerParserGetSubStateName
const char * AppLayerParserGetSubStateName(const AppProto alproto, const uint8_t sub_state)
Definition: app-layer-parser.c:1352
DetectEngineAppInspectionEngine_::sub_state
uint8_t sub_state
Definition: detect.h:430
DetectEngineMasterCtx_
Definition: detect.h:1737
Signature_::id
uint32_t id
Definition: detect.h:733
ENGINE_PROFILE_CUSTOM
@ ENGINE_PROFILE_CUSTOM
Definition: detect.h:1240
HashListTableBucket_
Definition: util-hashlist.h:28
DetectEngineThreadKeywordCtxItem_::name
const char * name
Definition: detect.h:913
detect-tcphdr.h
DetectEngineCtx_::guess_applayer_log_limit
uint8_t guess_applayer_log_limit
Definition: detect.h:1035
HashListTableRemove
int HashListTableRemove(HashListTable *ht, void *data, uint16_t datalen)
Definition: util-hashlist.c:154
DatasetReload
void DatasetReload(void)
Definition: datasets.c:539
ENGINE_PROFILE_LOW
@ ENGINE_PROFILE_LOW
Definition: detect.h:1237
DetectBufferType_::transforms
DetectEngineTransforms transforms
Definition: detect.h:467
detect-engine-iponly.h
SCConfRestoreContextBackup
void SCConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
Definition: conf.c:751
DetectEngineType
DetectEngineType
Definition: detect.h:923
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:530
Signature_
Signature container.
Definition: detect.h:688
SigMatch_
a single match condition for a signature
Definition: detect.h:359
DetectEngineCtx_::tenant_path
char * tenant_path
Definition: detect.h:1194
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:440
DETECT_SM_LIST_MAX
@ DETECT_SM_LIST_MAX
Definition: detect.h:135
DetectBufferType_::parent_id
int parent_id
Definition: detect.h:457
DETECT_PREFILTER_MPM
@ DETECT_PREFILTER_MPM
Definition: detect.h:918
TenantLoaderCtx_::yaml
char * yaml
Definition: detect-engine.c:4290
TransformIdData_::id_data
const uint8_t * id_data
Definition: detect.h:449
DetectLoadersSync
int DetectLoadersSync(void)
wait for loader tasks to complete
Definition: detect-engine-loader.c:572
SCProfilingSghDestroyCtx
void SCProfilingSghDestroyCtx(DetectEngineCtx *de_ctx)
Definition: util-profiling-rulegroups.c:286
HashTableInit
HashTable * HashTableInit(uint32_t size, uint32_t(*Hash)(struct HashTable_ *, void *, uint16_t), char(*Compare)(void *, uint16_t, void *, uint16_t), void(*Free)(void *))
Definition: util-hash.c:35
DetectEngineTenantMapping_::traffic_id
uint32_t traffic_id
Definition: detect.h:1732
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
ALPROTO_FAILED
@ ALPROTO_FAILED
Definition: app-layer-protos.h:33
spm_table
SpmTableElmt spm_table[SPM_TABLE_SIZE]
Definition: util-spm.c:63
DetectEngineReference
DetectEngineCtx * DetectEngineReference(DetectEngineCtx *de_ctx)
Definition: detect-engine.c:4131
SCDetectEngineRegisterRateFilterCallback
bool SCDetectEngineRegisterRateFilterCallback(SCDetectRateFilterFunc fn, void *arg)
Register a callback when a rate_filter has been applied to an alert.
Definition: detect-engine.c:5371
DetectEngineThreadCtx_::base64_decoded
uint8_t * base64_decoded
Definition: detect.h:1388
TenantLoaderCtx_::reload_cnt
int reload_cnt
Definition: detect-engine.c:4289
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
DetectEngineSyncer
struct DetectEngineSyncer_ DetectEngineSyncer
DetectMpmInitializeAppMpms
void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx)
Definition: detect-engine-mpm.c:316
EngineModeIsIPS
int EngineModeIsIPS(void)
Definition: suricata.c:246
MpmTableElmt_::ConfigDeinit
void(* ConfigDeinit)(MpmConfig **)
Definition: util-mpm.h:176
DetectEngineThreadCtx_::de_ctx
DetectEngineCtx * de_ctx
Definition: detect.h:1429
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine-inspect-buffer.c:144
suricata.h
DetectEngineCtx_::sig_array
Signature ** sig_array
Definition: detect.h:1006
MpmTableElmt_::ConfigCacheDirSet
void(* ConfigCacheDirSet)(MpmConfig *, const char *dir_path)
Definition: util-mpm.h:177
InspectionBufferGetDataPtr
InspectionBuffer *(* InspectionBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv, const int list_id)
Definition: detect-engine-helper.h:39
DetectEngineAppInspectionEngine_::dir
uint8_t dir
Definition: detect.h:421
PmqFree
void PmqFree(PrefilterRuleStore *pmq)
Cleanup and free a Pmq.
Definition: util-prefilter.c:126
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
MpmTableElmt_::CachePrune
int(* CachePrune)(MpmConfig *)
Definition: util-mpm.h:198
DETECT_ENGINE_TYPE_MT_STUB
@ DETECT_ENGINE_TYPE_MT_STUB
Definition: detect.h:926
SCConfNode_::name
char * name
Definition: conf.h:38
SignatureInitDataBuffer_::only_tc
bool only_tc
Definition: detect.h:536
detect-uricontent.h
DetectEngineCtx_::buffer_type_id
uint32_t buffer_type_id
Definition: detect.h:1143
Packet_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: decode.h:543
RUNMODE_CONF_TEST
@ RUNMODE_CONF_TEST
Definition: runmodes.h:56
ENGINE_PROFILE_HIGH
@ ENGINE_PROFILE_HIGH
Definition: detect.h:1239
DetectEngineReload
int DetectEngineReload(const SCInstance *suri)
Reload the detection engine.
Definition: detect-engine.c:5060
DetectEngineBufferRunValidateCallback
bool DetectEngineBufferRunValidateCallback(const DetectEngineCtx *de_ctx, const int id, const Signature *s, const char **sigerror)
Definition: detect-engine.c:1667
DetectEngineCtx_::sigerror
const char * sigerror
Definition: detect.h:1087
DetectEngineThreadCtx_::json_content_len
uint8_t json_content_len
Definition: detect.h:1345
DetectEngineThreadCtx_::mt_det_ctxs_cnt
uint32_t mt_det_ctxs_cnt
Definition: detect.h:1316
DetectEngineContentInspectionBuffer
bool DetectEngineContentInspectionBuffer(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const InspectionBuffer *b, const enum DetectContentInspectionType inspection_mode)
wrapper around DetectEngineContentInspectionInternal to return true/false only
Definition: detect-engine-content-inspection.c:772
DetectEngineMasterCtx_::lock
SCMutex lock
Definition: detect.h:1738
DetectEnginePktInspectionEngine::mpm
bool mpm
Definition: detect.h:490
SCInstance_
Definition: suricata.h:134
DetectEngineCtx_::spm_global_thread_ctx
SpmGlobalThreadCtx * spm_global_thread_ctx
Definition: detect.h:1045
DetectEngineClearMaster
void DetectEngineClearMaster(void)
Definition: detect-engine.c:5033
DetectEngineThreadCtx_::counter_match_list
StatsCounterAvgId counter_match_list
Definition: detect.h:1360
SRepDestroy
void SRepDestroy(DetectEngineCtx *de_ctx)
Definition: reputation.c:640
MpmTableElmt_::ConfigInit
MpmConfig *(* ConfigInit)(void)
Definition: util-mpm.h:175
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:989
AppLayerParserSupportsSubStates
bool AppLayerParserSupportsSubStates(const AppProto alproto)
Definition: app-layer-parser.c:1382
SIG_PROP_FLOW_ACTION_FLOW
@ SIG_PROP_FLOW_ACTION_FLOW
Definition: detect.h:85
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1550
DetectFirewallPolicies::app_policies
HashTable * app_policies
Definition: detect.h:960
SigString_::sig_str
char * sig_str
Definition: detect.h:891
SCStatFn
#define SCStatFn(pathname, statbuf)
Definition: util-path.h:35
DetectUnregisterThreadCtxFuncs
int DetectUnregisterThreadCtxFuncs(DetectEngineCtx *de_ctx, void *data, const char *name)
Remove Thread keyword context registration.
Definition: detect-engine.c:3981
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
SCInstance_::sig_file_exclusive
bool sig_file_exclusive
Definition: suricata.h:140
DetectAppLayerInspectEngineRegisterSingle
void DetectAppLayerInspectEngineRegisterSingle(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectEngineFuncPtr Callback, InspectionSingleBufferGetDataPtr GetData)
Definition: detect-engine.c:322
DetectEngineFrameInspectEngineRegister
void DetectEngineFrameInspectEngineRegister(DetectEngineCtx *de_ctx, const char *name, int dir, InspectionBufferFrameInspectFunc Callback, AppProto alproto, uint8_t type)
register inspect engine at start up time
Definition: detect-engine.c:480
Signature_::msg
char * msg
Definition: detect.h:756
DetectEngineTenantRegisterVlanId
int DetectEngineTenantRegisterVlanId(uint32_t tenant_id, uint16_t vlan_id)
Definition: detect-engine.c:4856
flow.h
DetectEngineSetParseMetadata
void DetectEngineSetParseMetadata(void)
Definition: detect-engine.c:5249
TmThreadsCheckFlag
int TmThreadsCheckFlag(ThreadVars *tv, uint32_t flag)
Check if a thread flag is set.
Definition: tm-threads.c:95
SCDetectRegisterThreadCtxGlobalFuncs
int SCDetectRegisterThreadCtxGlobalFuncs(const char *name, void *(*InitFunc)(void *), void *data, void(*FreeFunc)(void *))
Register Thread keyword context Funcs (Global)
Definition: detect-engine.c:4019
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:250
DetectPktMpmRegisterByParentId
void DetectPktMpmRegisterByParentId(DetectEngineCtx *de_ctx, const int id, const int parent_id, DetectEngineTransforms *transforms)
copy a mpm engine from parent_id, add in transforms
Definition: detect-engine-mpm.c:670
SpmDestroyCtx
void SpmDestroyCtx(SpmCtx *ctx)
Definition: util-spm.c:193
TENANT_SELECTOR_LIVEDEV
@ TENANT_SELECTOR_LIVEDEV
Definition: detect.h:1725
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:451
DetectEngineFrameInspectionEngine::type
uint8_t type
Definition: detect.h:516
DetectSigmatchListEnum
DetectSigmatchListEnum
Definition: detect.h:115
DetectEngineMasterCtx_::version
uint32_t version
Definition: detect.h:1744
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DetectEngineCtx_::sig_array_len
uint32_t sig_array_len
Definition: detect.h:1007
util-enum.h
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
SCConfNode_
Definition: conf.h:37
DetectEngineTenantUnregisterPcapFile
int DetectEngineTenantUnregisterPcapFile(uint32_t tenant_id)
Definition: detect-engine.c:4872
TenantLoaderCtx_::tenant_id
uint32_t tenant_id
Definition: detect-engine.c:4288
DetectEngineCtx_::signum
uint32_t signum
Definition: detect.h:1009
DetectEngineCtx_::tenant_id
uint32_t tenant_id
Definition: detect.h:992
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:668
detect-engine-loader.h
SigFileLoaderStat_
Signature loader statistics.
Definition: detect.h:898
SCConfNode_::val
char * val
Definition: conf.h:39
DetectEngineInspectPktBufferGeneric
int DetectEngineInspectPktBufferGeneric(DetectEngineThreadCtx *det_ctx, const DetectEnginePktInspectionEngine *engine, const Signature *s, Packet *p, uint8_t *_alert_flags)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2393
DetectEngineCtx_::buffer_type_hash_id
HashListTable * buffer_type_hash_id
Definition: detect.h:1142
DetectEngineGetByTenantId
DetectEngineCtx * DetectEngineGetByTenantId(uint32_t tenant_id)
Definition: detect-engine.c:4883
SpmInitCtx
SpmCtx * SpmInitCtx(const uint8_t *needle, uint16_t needle_len, int nocase, SpmGlobalThreadCtx *global_thread_ctx)
Definition: util-spm.c:183
DetectPortCleanupList
void DetectPortCleanupList(const DetectEngineCtx *de_ctx, DetectPort *head)
Free a DetectPort list and each of its members.
Definition: detect-engine-port.c:124
DETECT_SM_LIST_SUPPRESS
@ DETECT_SM_LIST_SUPPRESS
Definition: detect.h:132
SCMutex
#define SCMutex
Definition: threads-debug.h:114
DetectEngineTransforms::cnt
uint8_t cnt
Definition: detect.h:396
InspectionBufferMultipleForList::inspection_buffers
InspectionBuffer * inspection_buffers
Definition: detect.h:383
DetectLowerSetupCallback
void DetectLowerSetupCallback(const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
Definition: detect-engine.c:5340
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
DetectEngineAppInspectionEngine2Signature
int DetectEngineAppInspectionEngine2Signature(DetectEngineCtx *de_ctx, Signature *s)
Definition: detect-engine.c:939
SCClassConfLoadClassificationConfigFile
bool SCClassConfLoadClassificationConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
Loads the Classtype info from the classification.config file.
Definition: util-classification-config.c:520
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer, const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine-inspect-buffer.c:157
DetectEngineThreadCtx_::lua_blocked_function_errors
StatsCounterId lua_blocked_function_errors
Definition: detect.h:1444
DetectEngineTenantUnregisterVlanId
int DetectEngineTenantUnregisterVlanId(uint32_t tenant_id, uint16_t vlan_id)
Definition: detect-engine.c:4861
SCStat
struct stat SCStat
Definition: util-path.h:33
detect-engine-address.h
PmqSetup
int PmqSetup(PrefilterRuleStore *pmq)
Setup a pmq.
Definition: util-prefilter.c:37
PatternMatchThreadDestroy
void PatternMatchThreadDestroy(MpmThreadCtx *mpm_thread_ctx, uint16_t mpm_matcher)
Definition: detect-engine-mpm.c:988
TmModule_::flags
uint8_t flags
Definition: tm-modules.h:80
VarNameStoreActivate
int VarNameStoreActivate(void)
Definition: util-var-name.c:222
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine-inspect-buffer.c:76
RuleMatchCandidateTxArrayInit
void RuleMatchCandidateTxArrayInit(DetectEngineThreadCtx *det_ctx, uint32_t size)
Definition: detect.c:1180
SCProfilingKeywordThreadSetup
void SCProfilingKeywordThreadSetup(SCProfileKeywordDetectCtx *ctx, DetectEngineThreadCtx *det_ctx)
Definition: util-profiling-keywords.c:284
util-threshold-config.h
DetectEngineReloadIsStart
int DetectEngineReloadIsStart(void)
Definition: detect-engine.c:2077
DetectEngineThreadCtx_::post_rule_work_queue
PostRuleMatchWorkQueue post_rule_work_queue
Definition: detect.h:1412
DetectEngineThreadCtx_::tenant_array_size
uint32_t tenant_array_size
Definition: detect.h:1321
DetectEngineThreadCtx_::tenant_array
struct DetectEngineTenantMapping_ * tenant_array
Definition: detect.h:1320
suricata_ctl_flags
volatile uint8_t suricata_ctl_flags
Definition: suricata.c:176
detect-engine-threshold.h
TM_FLAG_FLOWWORKER_TM
#define TM_FLAG_FLOWWORKER_TM
Definition: tm-modules.h:34
FlowWorkerReplaceDetectCtx
void FlowWorkerReplaceDetectCtx(void *flow_worker, void *detect_ctx)
Definition: flow-worker.c:736
StringHashDjb2
uint32_t StringHashDjb2(const uint8_t *data, uint32_t datalen)
Definition: util-hash-string.c:22
DetectEngineThreadCtx_::TenantGetId
uint32_t(* TenantGetId)(const void *, const Packet *p)
Definition: detect.h:1323
DetectEngineAppInspectionEngine_::progress
uint8_t progress
Definition: detect.h:429
SCProfilingPrefilterThreadSetup
void SCProfilingPrefilterThreadSetup(SCProfilePrefilterDetectCtx *ctx, DetectEngineThreadCtx *det_ctx)
Definition: util-profiling-prefilter.c:245
SCClassConfDeInitContext
void SCClassConfDeInitContext(DetectEngineCtx *de_ctx)
Releases resources used by the Classification Config API.
Definition: util-classification-config.c:191
PrefilterPktNonPFStatsDump
void PrefilterPktNonPFStatsDump(void)
Definition: detect-engine-prefilter.c:625
DetectBufferType_::description
char description[128]
Definition: detect.h:455
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:256
DetectEngineFrameInspectionEngine::smd
SigMatchData * smd
Definition: detect.h:525
DetectEngineThreadCtx_::match_array
Signature ** match_array
Definition: detect.h:1397