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 /**
2150  * \brief Do the content inspection & validation for a signature
2151  *
2152  * \param de_ctx Detection engine context
2153  * \param det_ctx Detection engine thread context
2154  * \param s Signature to inspect
2155  * \param f Flow
2156  * \param flags app layer flags
2157  * \param state App layer state
2158  *
2159  * \retval 0 no match.
2160  * \retval 1 match.
2161  * \retval 2 Sig can't match.
2162  */
2164  const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
2165  void *alstate, void *txv, uint64_t tx_id)
2166 {
2167  const int list_id = engine->sm_list;
2168  SCLogDebug("running inspect on %d", list_id);
2169 
2170  const bool eof =
2171  (AppLayerParserGetStateProgress(f->proto, f->alproto, txv, flags) > engine->progress);
2172 
2173  SCLogDebug("list %d mpm? %s transforms %p", engine->sm_list, engine->mpm ? "true" : "false",
2174  engine->v2.transforms);
2175 
2176  /* if prefilter didn't already run, we need to consider transformations */
2177  const DetectEngineTransforms *transforms = NULL;
2178  if (!engine->mpm) {
2179  transforms = engine->v2.transforms;
2180  }
2181 
2182  const InspectionBuffer *buffer = DetectGetSingleData(
2183  det_ctx, transforms, f, flags, txv, list_id, engine->v2.GetDataSingle);
2184  if (unlikely(buffer == NULL)) {
2185  if (eof && engine->match_on_null) {
2187  }
2189  }
2190 
2191  const uint32_t data_len = buffer->inspect_len;
2192  const uint8_t *data = buffer->inspect;
2193  const uint64_t offset = buffer->inspect_offset;
2194 
2195  uint8_t ci_flags = eof ? DETECT_CI_FLAGS_END : 0;
2196  ci_flags |= (offset == 0 ? DETECT_CI_FLAGS_START : 0);
2197  ci_flags |= buffer->flags;
2198 
2199  /* Inspect all the uricontents fetched on each
2200  * transaction at the app layer */
2201  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f, data,
2203  if (match) {
2205  } else {
2207  }
2208 }
2209 
2210 /**
2211  * \brief Do the content inspection & validation for a signature
2212  *
2213  * \param de_ctx Detection engine context
2214  * \param det_ctx Detection engine thread context
2215  * \param s Signature to inspect
2216  * \param f Flow
2217  * \param flags app layer flags
2218  * \param state App layer state
2219  *
2220  * \retval 0 no match.
2221  * \retval 1 match.
2222  * \retval 2 Sig can't match.
2223  */
2225  const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags,
2226  void *alstate, void *txv, uint64_t tx_id)
2227 {
2228  const int list_id = engine->sm_list;
2229  SCLogDebug("running inspect on %d", list_id);
2230 
2231  const bool eof = (AppLayerParserGetStateProgress(f->proto, f->alproto, txv, flags) > engine->progress);
2232 
2233  SCLogDebug("list %d mpm? %s transforms %p",
2234  engine->sm_list, engine->mpm ? "true" : "false", engine->v2.transforms);
2235 
2236  /* if prefilter didn't already run, we need to consider transformations */
2237  const DetectEngineTransforms *transforms = NULL;
2238  if (!engine->mpm) {
2239  transforms = engine->v2.transforms;
2240  }
2241 
2242  const InspectionBuffer *buffer = engine->v2.GetData(det_ctx, transforms,
2243  f, flags, txv, list_id);
2244  if (unlikely(buffer == NULL)) {
2245  if (eof && engine->match_on_null) {
2247  }
2250  }
2251 
2252  const uint32_t data_len = buffer->inspect_len;
2253  const uint8_t *data = buffer->inspect;
2254  const uint64_t offset = buffer->inspect_offset;
2255 
2256  uint8_t ci_flags = eof ? DETECT_CI_FLAGS_END : 0;
2257  ci_flags |= (offset == 0 ? DETECT_CI_FLAGS_START : 0);
2258  ci_flags |= buffer->flags;
2259 
2260  /* Inspect all the uricontents fetched on each
2261  * transaction at the app layer */
2262  const bool match = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f, data,
2264  if (match) {
2266  } else {
2269  }
2270 }
2271 
2272 // wrapper for both DetectAppLayerInspectEngineRegister and DetectAppLayerMpmRegister
2273 // with cast of callback function
2274 void DetectAppLayerMultiRegisterSubState(const char *name, AppProto alproto, uint32_t dir,
2275  uint8_t sub_state, uint8_t progress, InspectionMultiBufferGetDataPtr GetData, int priority)
2276 {
2277  BUG_ON(AppLayerParserSupportsSubStates(alproto) && sub_state == 0);
2278  AppLayerInspectEngineRegisterInternal(name, alproto, dir, sub_state, progress,
2279  DetectEngineInspectMultiBufferGeneric, NULL, NULL, GetData);
2281  GetData, alproto, sub_state, progress);
2282 }
2283 
2284 // wrapper for both DetectAppLayerInspectEngineRegister and DetectAppLayerMpmRegister
2285 // with cast of callback function
2286 void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress,
2287  InspectionMultiBufferGetDataPtr GetData, int priority)
2288 {
2290  AppLayerInspectEngineRegisterInternal(name, alproto, dir, 0, (uint8_t)progress,
2291  DetectEngineInspectMultiBufferGeneric, NULL, NULL, GetData);
2293  name, dir, priority, PrefilterMultiGenericMpmRegister, GetData, alproto, progress);
2294 }
2295 
2297  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
2298  const int list_id, InspectionSingleBufferGetDataPtr GetBuf)
2299 {
2300  InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
2301  if (buffer->inspect == NULL) {
2302  const uint8_t *b = NULL;
2303  uint32_t b_len = 0;
2304 
2305  if (!GetBuf(txv, flow_flags, &b, &b_len))
2306  return NULL;
2307 
2308  SCInspectionBufferSetupAndApplyTransforms(det_ctx, list_id, buffer, b, b_len, transforms);
2309  }
2310  return buffer;
2311 }
2312 
2314  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
2315  const int list_id, uint32_t index, InspectionMultiBufferGetDataPtr GetBuf)
2316 {
2317  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, index);
2318  if (buffer == NULL) {
2319  return NULL;
2320  }
2321  if (buffer->initialized) {
2322  return buffer;
2323  }
2324 
2325  const uint8_t *data = NULL;
2326  uint32_t data_len = 0;
2327 
2328  if (!GetBuf(det_ctx, txv, flow_flags, index, &data, &data_len)) {
2330  return NULL;
2331  }
2332  InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
2333  buffer->flags = DETECT_CI_FLAGS_SINGLE;
2334  return buffer;
2335 }
2336 
2339  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
2340 {
2341  uint32_t local_id = 0;
2342  const DetectEngineTransforms *transforms = NULL;
2343  if (!engine->mpm) {
2344  transforms = engine->v2.transforms;
2345  }
2346 
2347  do {
2348  InspectionBuffer *buffer = DetectGetMultiData(det_ctx, transforms, f, flags, txv,
2349  engine->sm_list, local_id, engine->v2.GetMultiData);
2350 
2351  if (buffer == NULL || buffer->inspect == NULL)
2352  break;
2353 
2354  // The GetData functions set buffer->flags to DETECT_CI_FLAGS_SINGLE
2355  // This is not meant for streaming buffers
2356  const bool match = DetectEngineContentInspectionBuffer(de_ctx, det_ctx, s, engine->smd,
2358  if (match) {
2360  }
2361  local_id++;
2362  } while (1);
2363  if (local_id == 0) {
2364  // That means we did not get even one buffer value from the multi-buffer
2365  const bool eof = (AppLayerParserGetStateProgress(f->proto, f->alproto, txv, flags) >
2366  engine->progress);
2367  if (eof && engine->match_on_null) {
2369  }
2370  }
2372 }
2373 
2374 /**
2375  * \brief Do the content inspection & validation for a signature
2376  *
2377  * \param de_ctx Detection engine context
2378  * \param det_ctx Detection engine thread context
2379  * \param s Signature to inspect
2380  * \param p Packet
2381  *
2382  * \retval 0 no match.
2383  * \retval 1 match.
2384  */
2386  DetectEngineThreadCtx *det_ctx,
2387  const DetectEnginePktInspectionEngine *engine,
2388  const Signature *s, Packet *p, uint8_t *_alert_flags)
2389 {
2390  const int list_id = engine->sm_list;
2391  SCLogDebug("running inspect on %d", list_id);
2392 
2393  SCLogDebug("list %d transforms %p",
2394  engine->sm_list, engine->v1.transforms);
2395 
2396  /* if prefilter didn't already run, we need to consider transformations */
2397  const DetectEngineTransforms *transforms = NULL;
2398  if (!engine->mpm) {
2399  transforms = engine->v1.transforms;
2400  }
2401 
2402  const InspectionBuffer *buffer = engine->v1.GetData(det_ctx, transforms, p,
2403  list_id);
2404  if (unlikely(buffer == NULL)) {
2406  }
2407 
2408  uint8_t ci_flags = DETECT_CI_FLAGS_START|DETECT_CI_FLAGS_END;
2409  ci_flags |= buffer->flags;
2410 
2411  /* Inspect all the uricontents fetched on each
2412  * transaction at the app layer */
2413  const bool match = DetectEngineContentInspection(det_ctx->de_ctx, det_ctx, s, engine->smd, p,
2414  p->flow, buffer->inspect, buffer->inspect_len, 0, ci_flags,
2416  if (match) {
2418  } else {
2420  }
2421 }
2422 
2423 /** \internal
2424  * \brief inject a pseudo packet into each detect thread
2425  * -that doesn't use the new det_ctx yet
2426  * -*or*, if the thread should flush its output logs.
2427  */
2428 static void InjectPackets(
2429  ThreadVars **detect_tvs, DetectEngineThreadCtx **new_det_ctx, int no_of_detect_tvs)
2430 {
2431  /* inject a fake packet if the detect thread that needs it. This function
2432  * is called if
2433  * - A thread isn't using a DE ctx and should
2434  * - Or, it should process a pseudo packet and flush its output logs.
2435  * to speed the process. */
2436  for (int i = 0; i < no_of_detect_tvs; i++) {
2437  if (SC_ATOMIC_GET(new_det_ctx[i]->so_far_used_by_detect) != 1) {
2438  if (detect_tvs[i]->inq != NULL) {
2440  if (p != NULL) {
2443  PacketQueue *q = detect_tvs[i]->inq->pq;
2444  SCMutexLock(&q->mutex_q);
2445  PacketEnqueue(q, p);
2446  SCCondSignal(&q->cond_q);
2447  SCMutexUnlock(&q->mutex_q);
2448  }
2449  }
2450  }
2451  }
2452 }
2453 
2454 static void DetectEngineLoadFlowbitSettings(DetectEngineCtx *de_ctx)
2455 {
2457  char varname[128] = "detect.flowbits.max-per-signature";
2458  if (strlen(de_ctx->config_prefix) > 0) {
2459  snprintf(varname, sizeof(varname), "%s.detect.flowbits.max-per-signature",
2461  }
2462  const char *str;
2463  if (SCConfGet(varname, &str) == 1) {
2464  uint8_t val = 0;
2465  int ret = StringParseUint8(&val, 10, 0, str);
2466  if (ret > 0) {
2467  if (val > 0) {
2468  de_ctx->max_flowbits = val;
2469  } else {
2470  SCLogWarning("Invalid setting for flowbits.max-per-signature %d, resetting to the "
2471  "default",
2472  val);
2473  }
2474  } else {
2475  SCLogWarning(
2476  "Invalid setting for flowbits.max-per-signature, resetting to the default");
2477  }
2478  }
2479  SCLogConfig("Setting flowbits.max-per-signature to %d", de_ctx->max_flowbits);
2480 }
2481 
2482 /** \internal
2483  * \brief Update detect threads with new detect engine
2484  *
2485  * Atomically update each detect thread with a new thread context
2486  * that is associated to the new detection engine(s).
2487  *
2488  * If called in unix socket mode, it's possible that we don't have
2489  * detect threads yet.
2490  * NOTE: master MUST be locked before calling this
2491  *
2492  * \retval -1 error
2493  * \retval 0 no detection threads
2494  * \retval 1 successful reload
2495  */
2496 static int DetectEngineReloadThreads(DetectEngineCtx *new_de_ctx)
2497 {
2498  SCEnter();
2499  uint32_t i = 0;
2500 
2501  /* count detect threads in use */
2502  const uint32_t no_of_detect_tvs = TmThreadCountThreadsByTmmFlags(TM_FLAG_FLOWWORKER_TM);
2503  /* can be zero in unix socket mode */
2504  if (no_of_detect_tvs == 0) {
2505  return 0;
2506  }
2507 
2508  /* prepare swap structures */
2509  DetectEngineThreadCtx *old_det_ctx[no_of_detect_tvs];
2510  DetectEngineThreadCtx *new_det_ctx[no_of_detect_tvs];
2511  ThreadVars *detect_tvs[no_of_detect_tvs];
2512  memset(old_det_ctx, 0x00, (no_of_detect_tvs * sizeof(DetectEngineThreadCtx *)));
2513  memset(new_det_ctx, 0x00, (no_of_detect_tvs * sizeof(DetectEngineThreadCtx *)));
2514  memset(detect_tvs, 0x00, (no_of_detect_tvs * sizeof(ThreadVars *)));
2515 
2516  /* start the process of swapping detect threads ctxs */
2517 
2518  /* get reference to tv's and setup new_det_ctx array */
2520  for (ThreadVars *tv = tv_root[TVT_PPT]; tv != NULL; tv = tv->next) {
2521  if ((tv->tmm_flags & TM_FLAG_FLOWWORKER_TM) == 0) {
2522  continue;
2523  }
2524  for (TmSlot *s = tv->tm_slots; s != NULL; s = s->slot_next) {
2525  TmModule *tm = TmModuleGetById(s->tm_id);
2526  if (!(tm->flags & TM_FLAG_FLOWWORKER_TM)) {
2527  continue;
2528  }
2529 
2530  if (suricata_ctl_flags != 0) {
2532  goto error;
2533  }
2534 
2535  old_det_ctx[i] = FlowWorkerGetDetectCtxPtr(SC_ATOMIC_GET(s->slot_data));
2536  detect_tvs[i] = tv;
2537 
2538  new_det_ctx[i] = DetectEngineThreadCtxInitForReload(tv, new_de_ctx, 1);
2539  if (new_det_ctx[i] == NULL) {
2540  SCLogError("Detect engine thread init "
2541  "failure in live rule swap. Let's get out of here");
2543  goto error;
2544  }
2545  SCLogDebug("live rule swap created new det_ctx - %p and de_ctx "
2546  "- %p\n", new_det_ctx[i], new_de_ctx);
2547  i++;
2548  break;
2549  }
2550  }
2551  BUG_ON(i != no_of_detect_tvs);
2552 
2553  /* atomically replace the det_ctx data */
2554  i = 0;
2555  for (ThreadVars *tv = tv_root[TVT_PPT]; tv != NULL; tv = tv->next) {
2556  if ((tv->tmm_flags & TM_FLAG_FLOWWORKER_TM) == 0) {
2557  continue;
2558  }
2559  for (TmSlot *s = tv->tm_slots; s != NULL; s = s->slot_next) {
2560  TmModule *tm = TmModuleGetById(s->tm_id);
2561  if (!(tm->flags & TM_FLAG_FLOWWORKER_TM)) {
2562  continue;
2563  }
2564  SCLogDebug("swapping new det_ctx - %p with older one - %p",
2565  new_det_ctx[i], SC_ATOMIC_GET(s->slot_data));
2566  DEBUG_VALIDATE_BUG_ON(i >= no_of_detect_tvs); // help scan-build
2567  FlowWorkerReplaceDetectCtx(SC_ATOMIC_GET(s->slot_data), new_det_ctx[i]);
2568  i++;
2569  break;
2570  }
2571  }
2573 
2574  /* threads now all have new data, however they may not have started using
2575  * it and may still use the old data */
2576 
2577  SCLogDebug("Live rule swap has swapped %d old det_ctx's with new ones, "
2578  "along with the new de_ctx", no_of_detect_tvs);
2579 
2580  InjectPackets(detect_tvs, new_det_ctx, no_of_detect_tvs);
2581 
2582  /* loop waiting for detect threads to switch to the new det_ctx. Try to
2583  * wake up capture if needed (break loop). */
2584  uint32_t threads_done = 0;
2585 retry:
2586  for (i = 0; i < no_of_detect_tvs; i++) {
2587  if (suricata_ctl_flags != 0) {
2588  threads_done = no_of_detect_tvs;
2589  break;
2590  }
2591  SleepMsec(1);
2592  if (SC_ATOMIC_GET(new_det_ctx[i]->so_far_used_by_detect) == 1) {
2593  SCLogDebug("new_det_ctx - %p used by detect engine", new_det_ctx[i]);
2594  threads_done++;
2595  } else {
2596  TmThreadsCaptureBreakLoop(detect_tvs[i]);
2597  }
2598  }
2599  if (threads_done < no_of_detect_tvs) {
2600  threads_done = 0;
2601  SleepMsec(250);
2602  goto retry;
2603  }
2604 
2605  /* this is to make sure that if someone initiated shutdown during a live
2606  * rule swap, the live rule swap won't clean up the old det_ctx and
2607  * de_ctx, till all detect threads have stopped working and sitting
2608  * silently after setting RUNNING_DONE flag and while waiting for
2609  * THV_DEINIT flag */
2610  if (i != no_of_detect_tvs) { // not all threads we swapped
2611  for (ThreadVars *tv = tv_root[TVT_PPT]; tv != NULL; tv = tv->next) {
2612  if ((tv->tmm_flags & TM_FLAG_FLOWWORKER_TM) == 0) {
2613  continue;
2614  }
2615 
2617  SleepUsec(100);
2618  }
2619  }
2620  }
2621 
2622  /* free all the ctxs */
2623  for (i = 0; i < no_of_detect_tvs; i++) {
2624  SCLogDebug("Freeing old_det_ctx - %p used by detect",
2625  old_det_ctx[i]);
2626  DetectEngineThreadCtxDeinit(NULL, old_det_ctx[i]);
2627  }
2628 
2630 
2631  return 1;
2632 
2633  error:
2634  for (i = 0; i < no_of_detect_tvs; i++) {
2635  if (new_det_ctx[i] != NULL)
2636  DetectEngineThreadCtxDeinit(NULL, new_det_ctx[i]);
2637  }
2638  return -1;
2639 }
2640 
2642 {
2643  int sgh_mpm_caching = 0;
2644  if (SCConfGetBool("detect.sgh-mpm-caching", &sgh_mpm_caching) != 1) {
2645  return false;
2646  }
2647  return (bool)sgh_mpm_caching;
2648 }
2649 
2651 {
2652  if (DetectEngineMpmCachingEnabled() == false) {
2653  return NULL;
2654  }
2655 
2656  char yamlpath[] = "detect.sgh-mpm-caching-path";
2657  const char *strval = NULL;
2658  if (SCConfGet(yamlpath, &strval) == 1 && strval != NULL) {
2659  return strval;
2660  }
2661 
2662  static bool notified = false;
2663  if (!notified) {
2664  SCLogInfo("%s has no path specified, using %s", yamlpath, SGH_CACHE_DIR);
2665  notified = true;
2666  }
2667  return SGH_CACHE_DIR;
2668 }
2669 
2670 void DetectEngineMpmCacheService(uint32_t op_flags)
2671 {
2673  if (!de_ctx) {
2674  return;
2675  }
2676 
2677  if (!de_ctx->mpm_cfg || !de_ctx->mpm_cfg->cache_dir_path) {
2678  goto error;
2679  }
2680 
2681  if (mpm_table[de_ctx->mpm_matcher].CacheStatsInit != NULL) {
2683  if (de_ctx->mpm_cfg->cache_stats == NULL) {
2684  goto error;
2685  }
2686  }
2687 
2688  if (op_flags & DETECT_ENGINE_MPM_CACHE_OP_SAVE) {
2689  if (mpm_table[de_ctx->mpm_matcher].CacheRuleset != NULL) {
2691  }
2692  }
2693 
2694  if (op_flags & DETECT_ENGINE_MPM_CACHE_OP_PRUNE) {
2695  if (mpm_table[de_ctx->mpm_matcher].CachePrune != NULL) {
2697  }
2698  }
2699 
2700  if (mpm_table[de_ctx->mpm_matcher].CacheStatsPrint != NULL) {
2702  }
2703 
2704  if (mpm_table[de_ctx->mpm_matcher].CacheStatsDeinit != NULL) {
2706  de_ctx->mpm_cfg->cache_stats = NULL;
2707  }
2708 
2709 error:
2711 }
2712 
2713 static DetectEngineCtx *DetectEngineCtxInitReal(
2714  enum DetectEngineType type, const char *prefix, uint32_t tenant_id)
2715 {
2717  if (unlikely(de_ctx == NULL))
2718  goto error;
2719 
2720  memset(&de_ctx->sig_stat, 0, sizeof(SigFileLoaderStat));
2721  TAILQ_INIT(&de_ctx->sig_stat.failed_sigs);
2722  de_ctx->sigerror = NULL;
2723  de_ctx->type = type;
2725  de_ctx->tenant_id = tenant_id;
2726 
2729 
2732  if (de_ctx->mpm_cfg == NULL) {
2733  goto error;
2734  }
2735 
2739 
2741  if (SCConfGetTime("detect.sgh-mpm-caching-max-age",
2743  de_ctx->mpm_cfg->cache_max_age_seconds = 7ULL * 24ULL * 60ULL * 60ULL;
2744  }
2745  }
2746  }
2747  }
2748 
2751  SCLogDebug("stub %u with version %u", type, de_ctx->version);
2752  return de_ctx;
2753  }
2754 
2755  if (prefix != NULL) {
2756  strlcpy(de_ctx->config_prefix, prefix, sizeof(de_ctx->config_prefix));
2757  }
2758 
2759  int failure_fatal = 0;
2760  if (SCConfGetBool("engine.init-failure-fatal", (int *)&failure_fatal) != 1) {
2761  SCLogDebug("ConfGetBool could not load the value.");
2762  }
2763  de_ctx->failure_fatal = (failure_fatal == 1);
2764 
2765  SCLogConfig("pattern matchers: MPM: %s, SPM: %s", mpm_table[de_ctx->mpm_matcher].name,
2768  if (de_ctx->spm_global_thread_ctx == NULL) {
2769  SCLogDebug("Unable to alloc SpmGlobalThreadCtx.");
2770  goto error;
2771  }
2772 
2774  if (de_ctx->sm_types_prefilter == NULL) {
2775  goto error;
2776  }
2778  if (de_ctx->sm_types_silent_error == NULL) {
2779  goto error;
2780  }
2781  if (DetectEngineCtxLoadConf(de_ctx) == -1) {
2782  goto error;
2783  }
2784 
2790  DetectBufferTypeSetupDetectEngine(de_ctx);
2792 
2793  /* init iprep... ignore errors for now */
2794  (void)SRepInit(de_ctx);
2795 
2799  goto error;
2800  }
2801 
2802  if (ActionInitConfig() < 0) {
2803  goto error;
2804  }
2806  if (SCRConfLoadReferenceConfigFile(de_ctx, NULL) < 0) {
2808  goto error;
2809  }
2810 
2812  SCLogDebug("dectx with version %u", de_ctx->version);
2813  return de_ctx;
2814 error:
2815  if (de_ctx != NULL) {
2817  }
2818  return NULL;
2819 }
2820 
2822 {
2823  return DetectEngineCtxInitReal(DETECT_ENGINE_TYPE_MT_STUB, NULL, 0);
2824 }
2825 
2827 {
2828  return DetectEngineCtxInitReal(DETECT_ENGINE_TYPE_DD_STUB, NULL, 0);
2829 }
2830 
2832 {
2833  return DetectEngineCtxInitReal(DETECT_ENGINE_TYPE_NORMAL, NULL, 0);
2834 }
2835 
2836 DetectEngineCtx *DetectEngineCtxInitWithPrefix(const char *prefix, uint32_t tenant_id)
2837 {
2838  if (prefix == NULL || strlen(prefix) == 0)
2839  return DetectEngineCtxInit();
2840  else
2841  return DetectEngineCtxInitReal(DETECT_ENGINE_TYPE_NORMAL, prefix, tenant_id);
2842 }
2843 
2844 static void DetectEngineCtxFreeThreadKeywordData(DetectEngineCtx *de_ctx)
2845 {
2847 }
2848 
2849 static void DetectEngineCtxFreeFailedSigs(DetectEngineCtx *de_ctx)
2850 {
2851  SigString *item = NULL;
2852  SigString *sitem;
2853 
2854  TAILQ_FOREACH_SAFE(item, &de_ctx->sig_stat.failed_sigs, next, sitem) {
2855  SCFree(item->filename);
2856  SCFree(item->sig_str);
2857  if (item->sig_error) {
2858  SCFree(item->sig_error);
2859  }
2860  TAILQ_REMOVE(&de_ctx->sig_stat.failed_sigs, item, next);
2861  SCFree(item);
2862  }
2863 }
2864 
2865 /**
2866  * \brief Free a DetectEngineCtx::
2867  *
2868  * \param de_ctx DetectEngineCtx:: to be freed
2869  */
2871 {
2872 
2873  if (de_ctx == NULL)
2874  return;
2875 
2876 #ifdef PROFILE_RULES
2877  if (de_ctx->profile_ctx != NULL) {
2878  SCProfilingRuleDestroyCtx(de_ctx->profile_ctx);
2879  de_ctx->profile_ctx = NULL;
2880  }
2881 #endif
2882 #ifdef PROFILING
2883  if (de_ctx->profile_keyword_ctx != NULL) {
2884  SCProfilingKeywordDestroyCtx(de_ctx);//->profile_keyword_ctx);
2885 // de_ctx->profile_keyword_ctx = NULL;
2886  }
2887  if (de_ctx->profile_sgh_ctx != NULL) {
2889  }
2891 #endif
2892 
2895  }
2896  /* Normally the hashes are freed elsewhere, but
2897  * to be sure look at them again here.
2898  */
2904  if (de_ctx->sig_array)
2906 
2907  if (de_ctx->filedata_config)
2909 
2913 
2915 
2919 
2921 
2922  DetectEngineCtxFreeThreadKeywordData(de_ctx);
2924  DetectEngineCtxFreeFailedSigs(de_ctx);
2925 
2928 
2929  /* if we have a config prefix, remove the config from the tree */
2930  if (strlen(de_ctx->config_prefix) > 0) {
2931  /* remove config */
2933  if (node != NULL) {
2934  SCConfNodeRemove(node); /* frees node */
2935  }
2936 #if 0
2937  SCConfDump();
2938 #endif
2939  }
2940 
2943 
2944  DetectBufferTypeFreeDetectEngine(de_ctx);
2947 
2948  if (de_ctx->tenant_path) {
2950  }
2951 
2952  if (de_ctx->requirements) {
2953  SCDetectRequiresStatusFree(de_ctx->requirements);
2954  }
2955 
2956  if (de_ctx->non_pf_engine_names) {
2958  }
2959  if (de_ctx->fw_policies) {
2960  for (uint32_t i = 0; i < DETECT_FIREWALL_POLICY_SIZE; i++) {
2964  }
2965  }
2967  }
2969  SCFree(de_ctx);
2970  //DetectAddressGroupPrintMemory();
2971  //DetectSigGroupPrintMemory();
2972  //DetectPortPrintMemory();
2973 }
2974 
2975 /** \brief Function that load DetectEngineCtx config for grouping sigs
2976  * used by the engine
2977  * \retval 0 if no config provided, 1 if config was provided
2978  * and loaded successfully
2979  */
2980 static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx)
2981 {
2982  uint8_t profile = ENGINE_PROFILE_MEDIUM;
2983  const char *max_uniq_toclient_groups_str = NULL;
2984  const char *max_uniq_toserver_groups_str = NULL;
2985  const char *sgh_mpm_context = NULL;
2986  const char *de_ctx_profile = NULL;
2987 
2988  (void)SCConfGet("detect.profile", &de_ctx_profile);
2989  (void)SCConfGet("detect.sgh-mpm-context", &sgh_mpm_context);
2990 
2991  SCConfNode *de_ctx_custom = SCConfGetNode("detect-engine");
2992  SCConfNode *opt = NULL;
2993 
2994  if (de_ctx_custom != NULL) {
2995  TAILQ_FOREACH(opt, &de_ctx_custom->head, next) {
2996  if (de_ctx_profile == NULL) {
2997  if (opt->val && strcmp(opt->val, "profile") == 0) {
2998  de_ctx_profile = opt->head.tqh_first->val;
2999  }
3000  }
3001 
3002  if (sgh_mpm_context == NULL) {
3003  if (opt->val && strcmp(opt->val, "sgh-mpm-context") == 0) {
3004  sgh_mpm_context = opt->head.tqh_first->val;
3005  }
3006  }
3007  }
3008  }
3009 
3010  if (de_ctx_profile != NULL) {
3011  if (strcmp(de_ctx_profile, "low") == 0 ||
3012  strcmp(de_ctx_profile, "lowest") == 0) { // legacy
3013  profile = ENGINE_PROFILE_LOW;
3014  } else if (strcmp(de_ctx_profile, "medium") == 0) {
3015  profile = ENGINE_PROFILE_MEDIUM;
3016  } else if (strcmp(de_ctx_profile, "high") == 0 ||
3017  strcmp(de_ctx_profile, "highest") == 0) { // legacy
3018  profile = ENGINE_PROFILE_HIGH;
3019  } else if (strcmp(de_ctx_profile, "custom") == 0) {
3020  profile = ENGINE_PROFILE_CUSTOM;
3021  } else {
3022  SCLogError("invalid value for detect.profile: '%s'. "
3023  "Valid options: low, medium, high and custom.",
3024  de_ctx_profile);
3025  return -1;
3026  }
3027 
3028  SCLogDebug("Profile for detection engine groups is \"%s\"", de_ctx_profile);
3029  } else {
3030  SCLogDebug("Profile for detection engine groups not provided "
3031  "at suricata.yaml. Using default (\"medium\").");
3032  }
3033 
3034  /* detect-engine.sgh-mpm-context option parsing */
3035  if (sgh_mpm_context == NULL || strcmp(sgh_mpm_context, "auto") == 0) {
3036  /* for now, since we still haven't implemented any intelligence into
3037  * understanding the patterns and distributing mpm_ctx across sgh */
3039  de_ctx->mpm_matcher == MPM_HS) {
3041  } else {
3043  }
3044  } else {
3045  if (strcmp(sgh_mpm_context, "single") == 0) {
3047  } else if (strcmp(sgh_mpm_context, "full") == 0) {
3049  } else {
3050  SCLogError("You have supplied an "
3051  "invalid conf value for detect-engine.sgh-mpm-context-"
3052  "%s",
3053  sgh_mpm_context);
3054  exit(EXIT_FAILURE);
3055  }
3056  }
3057 
3058  if (RunmodeIsUnittests()) {
3060  }
3061 
3062  /* parse profile custom-values */
3063  opt = NULL;
3064  switch (profile) {
3065  case ENGINE_PROFILE_LOW:
3068  break;
3069 
3070  case ENGINE_PROFILE_HIGH:
3073  break;
3074 
3075  case ENGINE_PROFILE_CUSTOM:
3076  (void)SCConfGet("detect.custom-values.toclient-groups", &max_uniq_toclient_groups_str);
3077  (void)SCConfGet("detect.custom-values.toserver-groups", &max_uniq_toserver_groups_str);
3078 
3079  if (de_ctx_custom != NULL) {
3080  TAILQ_FOREACH(opt, &de_ctx_custom->head, next) {
3081  if (opt->val && strcmp(opt->val, "custom-values") == 0) {
3082  if (max_uniq_toclient_groups_str == NULL) {
3083  max_uniq_toclient_groups_str = (char *)SCConfNodeLookupChildValue(
3084  opt->head.tqh_first, "toclient-sp-groups");
3085  }
3086  if (max_uniq_toclient_groups_str == NULL) {
3087  max_uniq_toclient_groups_str = (char *)SCConfNodeLookupChildValue(
3088  opt->head.tqh_first, "toclient-groups");
3089  }
3090  if (max_uniq_toserver_groups_str == NULL) {
3091  max_uniq_toserver_groups_str = (char *)SCConfNodeLookupChildValue(
3092  opt->head.tqh_first, "toserver-dp-groups");
3093  }
3094  if (max_uniq_toserver_groups_str == NULL) {
3095  max_uniq_toserver_groups_str = (char *)SCConfNodeLookupChildValue(
3096  opt->head.tqh_first, "toserver-groups");
3097  }
3098  }
3099  }
3100  }
3101  if (max_uniq_toclient_groups_str != NULL) {
3103  (uint16_t)strlen(max_uniq_toclient_groups_str),
3104  (const char *)max_uniq_toclient_groups_str) <= 0) {
3106 
3107  SCLogWarning("parsing '%s' for "
3108  "toclient-groups failed, using %u",
3109  max_uniq_toclient_groups_str, de_ctx->max_uniq_toclient_groups);
3110  }
3111  } else {
3113  }
3114  SCLogConfig("toclient-groups %u", de_ctx->max_uniq_toclient_groups);
3115 
3116  if (max_uniq_toserver_groups_str != NULL) {
3118  (uint16_t)strlen(max_uniq_toserver_groups_str),
3119  (const char *)max_uniq_toserver_groups_str) <= 0) {
3121 
3122  SCLogWarning("parsing '%s' for "
3123  "toserver-groups failed, using %u",
3124  max_uniq_toserver_groups_str, de_ctx->max_uniq_toserver_groups);
3125  }
3126  } else {
3128  }
3129  SCLogConfig("toserver-groups %u", de_ctx->max_uniq_toserver_groups);
3130  break;
3131 
3132  /* Default (or no config provided) is profile medium */
3133  case ENGINE_PROFILE_MEDIUM:
3135  default:
3138  break;
3139  }
3140 
3141  intmax_t value = 0;
3143  if (SCConfGetInt("detect.inspection-recursion-limit", &value) == 1) {
3144  if (value >= 0 && value <= INT_MAX) {
3145  de_ctx->inspection_recursion_limit = (int)value;
3146  }
3147 
3148  /* fall back to old config parsing */
3149  } else {
3150  SCConfNode *insp_recursion_limit_node = NULL;
3151  char *insp_recursion_limit = NULL;
3152 
3153  if (de_ctx_custom != NULL) {
3154  opt = NULL;
3155  TAILQ_FOREACH(opt, &de_ctx_custom->head, next) {
3156  if (opt->val && strcmp(opt->val, "inspection-recursion-limit") != 0)
3157  continue;
3158 
3159  insp_recursion_limit_node = SCConfNodeLookupChild(opt, opt->val);
3160  if (insp_recursion_limit_node == NULL) {
3161  SCLogError("Error retrieving conf "
3162  "entry for detect-engine:inspection-recursion-limit");
3163  break;
3164  }
3165  insp_recursion_limit = insp_recursion_limit_node->val;
3166  SCLogDebug("Found detect-engine.inspection-recursion-limit - %s:%s",
3167  insp_recursion_limit_node->name, insp_recursion_limit_node->val);
3168  break;
3169  }
3170 
3171  if (insp_recursion_limit != NULL) {
3173  0, (const char *)insp_recursion_limit) < 0) {
3174  SCLogWarning("Invalid value for "
3175  "detect-engine.inspection-recursion-limit: %s "
3176  "resetting to %d",
3177  insp_recursion_limit, DETECT_ENGINE_DEFAULT_INSPECTION_RECURSION_LIMIT);
3180  }
3181  }
3182  }
3183  }
3184 
3187 
3188  SCLogDebug("de_ctx->inspection_recursion_limit: %d",
3190 
3191  // default value is 4
3193  if (SCConfGetInt("detect.stream-tx-log-limit", &value) == 1) {
3194  if (value >= 0 && value <= UINT8_MAX) {
3195  de_ctx->guess_applayer_log_limit = (uint8_t)value;
3196  } else {
3197  SCLogWarning("Invalid value for detect-engine.stream-tx-log-limit: must be between 0 "
3198  "and 255, will default to 4");
3199  }
3200  }
3201  int guess_applayer = 0;
3202  if ((SCConfGetBool("detect.guess-applayer-tx", &guess_applayer)) == 1) {
3203  if (guess_applayer == 1) {
3204  de_ctx->guess_applayer = true;
3205  }
3206  }
3207 
3208  /* parse port grouping priority settings */
3209 
3210  const char *ports = NULL;
3211  (void)SCConfGet("detect.grouping.tcp-priority-ports", &ports);
3212  if (ports) {
3213  SCLogConfig("grouping: tcp-priority-ports %s", ports);
3214  } else {
3215  (void)SCConfGet("detect.grouping.tcp-whitelist", &ports);
3216  if (ports) {
3217  SCLogConfig(
3218  "grouping: tcp-priority-ports from legacy 'tcp-whitelist' setting: %s", ports);
3219  } else {
3220  ports = "53, 80, 139, 443, 445, 1433, 3306, 3389, 6666, 6667, 8080";
3221  SCLogConfig("grouping: tcp-priority-ports (default) %s", ports);
3222  }
3223  }
3224  if (DetectPortParse(de_ctx, &de_ctx->tcp_priorityports, ports) != 0) {
3225  SCLogWarning("'%s' is not a valid value "
3226  "for detect.grouping.tcp-priority-ports",
3227  ports);
3228  }
3230  for ( ; x != NULL; x = x->next) {
3231  if (x->port != x->port2) {
3232  SCLogWarning("'%s' is not a valid value "
3233  "for detect.grouping.tcp-priority-ports: only single ports allowed",
3234  ports);
3236  de_ctx->tcp_priorityports = NULL;
3237  break;
3238  }
3239  }
3240 
3241  ports = NULL;
3242  (void)SCConfGet("detect.grouping.udp-priority-ports", &ports);
3243  if (ports) {
3244  SCLogConfig("grouping: udp-priority-ports %s", ports);
3245  } else {
3246  (void)SCConfGet("detect.grouping.udp-whitelist", &ports);
3247  if (ports) {
3248  SCLogConfig(
3249  "grouping: udp-priority-ports from legacy 'udp-whitelist' setting: %s", ports);
3250  } else {
3251  ports = "53, 135, 5060";
3252  SCLogConfig("grouping: udp-priority-ports (default) %s", ports);
3253  }
3254  }
3255  if (DetectPortParse(de_ctx, &de_ctx->udp_priorityports, ports) != 0) {
3256  SCLogWarning("'%s' is not a valid value "
3257  "for detect.grouping.udp-priority-ports",
3258  ports);
3259  }
3260  for (x = de_ctx->udp_priorityports; x != NULL; x = x->next) {
3261  if (x->port != x->port2) {
3262  SCLogWarning("'%s' is not a valid value "
3263  "for detect.grouping.udp-priority-ports: only single ports allowed",
3264  ports);
3266  de_ctx->udp_priorityports = NULL;
3267  break;
3268  }
3269  }
3270 
3271  DetectEngineLoadFlowbitSettings(de_ctx);
3272 
3274  const char *pf_setting = NULL;
3275  if (SCConfGet("detect.prefilter.default", &pf_setting) == 1 && pf_setting) {
3276  if (strcasecmp(pf_setting, "mpm") == 0) {
3278  } else if (strcasecmp(pf_setting, "auto") == 0) {
3280  }
3281  }
3282  switch (de_ctx->prefilter_setting) {
3283  case DETECT_PREFILTER_MPM:
3284  SCLogConfig("prefilter engines: MPM");
3285  break;
3286  case DETECT_PREFILTER_AUTO:
3287  SCLogConfig("prefilter engines: MPM and keywords");
3288  break;
3289  }
3290 
3291  return 0;
3292 }
3293 
3295 {
3296  de_ctx->signum = 0;
3297 }
3298 
3299 static int DetectEngineThreadCtxInitGlobalKeywords(DetectEngineThreadCtx *det_ctx)
3300 {
3301  const DetectEngineMasterCtx *master = &g_master_de_ctx;
3302 
3303  if (master->keyword_id > 0) {
3304  // coverity[suspicious_sizeof : FALSE]
3305  det_ctx->global_keyword_ctxs_array = (void **)SCCalloc(master->keyword_id, sizeof(void *));
3306  if (det_ctx->global_keyword_ctxs_array == NULL) {
3307  SCLogError("setting up thread local detect ctx");
3308  return TM_ECODE_FAILED;
3309  }
3310  det_ctx->global_keyword_ctxs_size = master->keyword_id;
3311 
3312  const DetectEngineThreadKeywordCtxItem *item = master->keyword_list;
3313  while (item) {
3314  det_ctx->global_keyword_ctxs_array[item->id] = item->InitFunc(item->data);
3315  if (det_ctx->global_keyword_ctxs_array[item->id] == NULL) {
3316  SCLogError("setting up thread local detect ctx "
3317  "for keyword \"%s\" failed",
3318  item->name);
3319  return TM_ECODE_FAILED;
3320  }
3321  item = item->next;
3322  }
3323  }
3324  return TM_ECODE_OK;
3325 }
3326 
3327 static void DetectEngineThreadCtxDeinitGlobalKeywords(DetectEngineThreadCtx *det_ctx)
3328 {
3329  if (det_ctx->global_keyword_ctxs_array == NULL ||
3330  det_ctx->global_keyword_ctxs_size == 0) {
3331  return;
3332  }
3333 
3334  const DetectEngineMasterCtx *master = &g_master_de_ctx;
3335  if (master->keyword_id > 0) {
3336  const DetectEngineThreadKeywordCtxItem *item = master->keyword_list;
3337  while (item) {
3338  if (det_ctx->global_keyword_ctxs_array[item->id] != NULL)
3339  item->FreeFunc(det_ctx->global_keyword_ctxs_array[item->id]);
3340 
3341  item = item->next;
3342  }
3343  det_ctx->global_keyword_ctxs_size = 0;
3345  det_ctx->global_keyword_ctxs_array = NULL;
3346  }
3347 }
3348 
3349 static int DetectEngineThreadCtxInitKeywords(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx)
3350 {
3351  if (de_ctx->keyword_id > 0) {
3352  // coverity[suspicious_sizeof : FALSE]
3353  det_ctx->keyword_ctxs_array = SCCalloc(de_ctx->keyword_id, sizeof(void *));
3354  if (det_ctx->keyword_ctxs_array == NULL) {
3355  SCLogError("setting up thread local detect ctx");
3356  return TM_ECODE_FAILED;
3357  }
3358 
3359  det_ctx->keyword_ctxs_size = de_ctx->keyword_id;
3360 
3362  for (; hb != NULL; hb = HashListTableGetListNext(hb)) {
3364 
3365  det_ctx->keyword_ctxs_array[item->id] = item->InitFunc(item->data);
3366  if (det_ctx->keyword_ctxs_array[item->id] == NULL) {
3367  SCLogError("setting up thread local detect ctx "
3368  "for keyword \"%s\" failed",
3369  item->name);
3370  return TM_ECODE_FAILED;
3371  }
3372  }
3373  }
3374  return TM_ECODE_OK;
3375 }
3376 
3377 static void DetectEngineThreadCtxDeinitKeywords(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx)
3378 {
3379  if (de_ctx->keyword_id > 0) {
3381  for (; hb != NULL; hb = HashListTableGetListNext(hb)) {
3383 
3384  if (det_ctx->keyword_ctxs_array[item->id] != NULL)
3385  item->FreeFunc(det_ctx->keyword_ctxs_array[item->id]);
3386  }
3387  det_ctx->keyword_ctxs_size = 0;
3388  SCFree(det_ctx->keyword_ctxs_array);
3389  det_ctx->keyword_ctxs_array = NULL;
3390  }
3391 }
3392 
3393 /** NOTE: master MUST be locked before calling this */
3394 static TmEcode DetectEngineThreadCtxInitForMT(ThreadVars *tv, DetectEngineThreadCtx *det_ctx)
3395 {
3396  DetectEngineMasterCtx *master = &g_master_de_ctx;
3397 
3398  DetectEngineTenantMapping *map_array = NULL;
3399  uint32_t map_array_size = 0;
3400  uint32_t map_cnt = 0;
3401  uint32_t max_tenant_id = 0;
3402  DetectEngineCtx *list = master->list;
3403 
3405 
3406  /* coverity[missing_lock] */
3407  if (master->tenant_selector == TENANT_SELECTOR_UNKNOWN) {
3408  SCLogError("no tenant selector set: "
3409  "set using multi-detect.selector");
3410  return TM_ECODE_FAILED;
3411  }
3412 
3413  uint32_t tcnt = 0;
3414  while (list) {
3415  if (list->tenant_id > max_tenant_id)
3416  max_tenant_id = list->tenant_id;
3417 
3418  list = list->next;
3419  tcnt++;
3420  }
3421 
3422  HashTable *mt_det_ctxs_hash =
3423  HashTableInit(tcnt * 2, TenantIdHash, TenantIdCompare, TenantIdFree);
3424  if (mt_det_ctxs_hash == NULL) {
3425  goto error;
3426  }
3427 
3428  if (tcnt == 0) {
3429  SCLogInfo("no tenants left, or none registered yet");
3430  } else {
3431  max_tenant_id++;
3432 
3434  while (map) {
3435  map_cnt++;
3436  map = map->next;
3437  }
3438 
3439  if (map_cnt > 0) {
3440  map_array_size = map_cnt + 1;
3441 
3442  map_array = SCCalloc(map_array_size, sizeof(*map_array));
3443  if (map_array == NULL)
3444  goto error;
3445 
3446  /* fill the array */
3447  map_cnt = 0;
3448  map = master->tenant_mapping_list;
3449  while (map) {
3450  if (map_cnt >= map_array_size) {
3451  goto error;
3452  }
3453  map_array[map_cnt].traffic_id = map->traffic_id;
3454  map_array[map_cnt].tenant_id = map->tenant_id;
3455  map_cnt++;
3456  map = map->next;
3457  }
3458 
3459  }
3460 
3461  /* set up hash for tenant lookup */
3462  list = master->list;
3463  while (list) {
3464  SCLogDebug("tenant-id %u", list->tenant_id);
3465  if (list->tenant_id != 0) {
3467  if (mt_det_ctx == NULL)
3468  goto error;
3469  if (HashTableAdd(mt_det_ctxs_hash, mt_det_ctx, 0) != 0) {
3470  goto error;
3471  }
3472  }
3473  list = list->next;
3474  }
3475  }
3476 
3477  det_ctx->mt_det_ctxs_hash = mt_det_ctxs_hash;
3478  mt_det_ctxs_hash = NULL;
3479 
3480  det_ctx->mt_det_ctxs_cnt = max_tenant_id;
3481 
3482  det_ctx->tenant_array = map_array;
3483  det_ctx->tenant_array_size = map_array_size;
3484 
3485  switch (master->tenant_selector) {
3487  SCLogDebug("TENANT_SELECTOR_UNKNOWN");
3488  break;
3489  case TENANT_SELECTOR_VLAN:
3490  det_ctx->TenantGetId = DetectEngineTenantGetIdFromVlanId;
3491  SCLogDebug("TENANT_SELECTOR_VLAN");
3492  break;
3494  det_ctx->TenantGetId = DetectEngineTenantGetIdFromLivedev;
3495  SCLogDebug("TENANT_SELECTOR_LIVEDEV");
3496  break;
3498  det_ctx->TenantGetId = DetectEngineTenantGetIdFromPcap;
3499  SCLogDebug("TENANT_SELECTOR_DIRECT");
3500  break;
3501  }
3502 
3503  return TM_ECODE_OK;
3504 error:
3505  if (map_array != NULL)
3506  SCFree(map_array);
3507  if (mt_det_ctxs_hash != NULL)
3508  HashTableFree(mt_det_ctxs_hash);
3509 
3510  return TM_ECODE_FAILED;
3511 }
3512 
3513 /** \internal
3514  * \brief Helper for DetectThread setup functions
3515  */
3516 static TmEcode ThreadCtxDoInit (DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx)
3517 {
3518  PatternMatchThreadPrepare(&det_ctx->mtc, de_ctx);
3519 
3520  PmqSetup(&det_ctx->pmq);
3521 
3523  if (det_ctx->spm_thread_ctx == NULL) {
3524  return TM_ECODE_FAILED;
3525  }
3526 
3527  /* DeState */
3528  if (de_ctx->sig_array_len > 0) {
3529  det_ctx->match_array_len = de_ctx->sig_array_len;
3530  det_ctx->match_array = SCCalloc(det_ctx->match_array_len, sizeof(Signature *));
3531  if (det_ctx->match_array == NULL) {
3532  return TM_ECODE_FAILED;
3533  }
3534  det_ctx->replace = SCCalloc(de_ctx->sig_array_len, sizeof(Signature *));
3535  if (det_ctx->replace == NULL) {
3536  return TM_ECODE_FAILED;
3537  }
3538 
3540  }
3541 
3542  /* Alert processing queue */
3543  AlertQueueInit(det_ctx);
3544 
3545  /* byte_extract storage */
3546  det_ctx->byte_values = SCMalloc(sizeof(*det_ctx->byte_values) *
3548  if (det_ctx->byte_values == NULL) {
3549  return TM_ECODE_FAILED;
3550  }
3551 
3552  /* Allocate space for base64 decoded data. */
3555  if (det_ctx->base64_decoded == NULL) {
3556  return TM_ECODE_FAILED;
3557  }
3558  det_ctx->base64_decoded_len = 0;
3559  }
3560 
3562  det_ctx->inspect.buffers = SCCalloc(det_ctx->inspect.buffers_size, sizeof(InspectionBuffer));
3563  if (det_ctx->inspect.buffers == NULL) {
3564  return TM_ECODE_FAILED;
3565  }
3566  det_ctx->inspect.to_clear_queue = SCCalloc(det_ctx->inspect.buffers_size, sizeof(uint32_t));
3567  if (det_ctx->inspect.to_clear_queue == NULL) {
3568  return TM_ECODE_FAILED;
3569  }
3570  det_ctx->inspect.to_clear_idx = 0;
3571 
3574  if (det_ctx->multi_inspect.buffers == NULL) {
3575  return TM_ECODE_FAILED;
3576  }
3577  det_ctx->multi_inspect.to_clear_queue = SCCalloc(det_ctx->multi_inspect.buffers_size, sizeof(uint32_t));
3578  if (det_ctx->multi_inspect.to_clear_queue == NULL) {
3579  return TM_ECODE_FAILED;
3580  }
3581  det_ctx->multi_inspect.to_clear_idx = 0;
3582 
3583  if (DetectEngineThreadCtxInitKeywords(de_ctx, det_ctx) != TM_ECODE_OK)
3584  return TM_ECODE_FAILED;
3585  DetectEngineThreadCtxInitGlobalKeywords(det_ctx);
3586 #ifdef PROFILE_RULES
3587  SCProfilingRuleThreadSetup(de_ctx->profile_ctx, det_ctx);
3588 #endif
3589 #ifdef PROFILING
3593 #endif
3594  SC_ATOMIC_INIT(det_ctx->so_far_used_by_detect);
3595 
3596  if (ThresholdCacheThreadInit(det_ctx) != 0)
3597  return TM_ECODE_FAILED;
3598  return TM_ECODE_OK;
3599 }
3600 
3601 /** \brief initialize thread specific detection engine context
3602  *
3603  * \note there is a special case when using delayed detect. In this case the
3604  * function is called twice per thread. The first time the rules are not
3605  * yet loaded. de_ctx->delayed_detect_initialized will be 0. The 2nd
3606  * time they will be loaded. de_ctx->delayed_detect_initialized will be 1.
3607  * This is needed to do the per thread counter registration before the
3608  * packet runtime starts. In delayed detect mode, the first call will
3609  * return a NULL ptr through the data ptr.
3610  *
3611  * \param tv ThreadVars for this thread
3612  * \param initdata pointer to de_ctx
3613  * \param data[out] pointer to store our thread detection ctx
3614  *
3615  * \retval TM_ECODE_OK if all went well
3616  * \retval TM_ECODE_FAILED on serious errors
3617  */
3618 TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
3619 {
3620  DetectEngineThreadCtx *det_ctx = SCCalloc(1, sizeof(DetectEngineThreadCtx));
3621  if (unlikely(det_ctx == NULL))
3622  return TM_ECODE_FAILED;
3623 
3624  det_ctx->tv = tv;
3625  det_ctx->de_ctx = DetectEngineGetCurrent();
3626  if (det_ctx->de_ctx == NULL) {
3627 #ifdef UNITTESTS
3628  if (RunmodeIsUnittests()) {
3629  det_ctx->de_ctx = (DetectEngineCtx *)initdata;
3630  } else {
3631  DetectEngineThreadCtxDeinit(tv, det_ctx);
3632  return TM_ECODE_FAILED;
3633  }
3634 #else
3635  DetectEngineThreadCtxDeinit(tv, det_ctx);
3636  return TM_ECODE_FAILED;
3637 #endif
3638  }
3639 
3640  if (det_ctx->de_ctx->type == DETECT_ENGINE_TYPE_NORMAL ||
3641  det_ctx->de_ctx->type == DETECT_ENGINE_TYPE_TENANT)
3642  {
3643  if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
3644  DetectEngineThreadCtxDeinit(tv, det_ctx);
3645  return TM_ECODE_FAILED;
3646  }
3647  }
3648 
3649  /** alert counter setup */
3650  det_ctx->counter_alerts = StatsRegisterCounter("detect.alert", &tv->stats);
3651  det_ctx->counter_alerts_overflow =
3652  StatsRegisterCounter("detect.alert_queue_overflow", &tv->stats);
3653  if (EngineModeIsFirewall()) {
3655  StatsRegisterCounter("firewall.discarded_alerts", &tv->stats);
3656  }
3657  det_ctx->counter_alerts_suppressed =
3658  StatsRegisterCounter("detect.alerts_suppressed", &tv->stats);
3659 
3660  /* Register counter for Lua rule errors. */
3661  det_ctx->lua_rule_errors = StatsRegisterCounter("detect.lua.errors", &tv->stats);
3662 
3663  /* Register a counter for Lua blocked function attempts. */
3664  det_ctx->lua_blocked_function_errors =
3665  StatsRegisterCounter("detect.lua.blocked_function_errors", &tv->stats);
3666 
3667  /* Register a counter for Lua instruction limit errors. */
3668  det_ctx->lua_instruction_limit_errors =
3669  StatsRegisterCounter("detect.lua.instruction_limit_errors", &tv->stats);
3670 
3671  /* Register a counter for Lua memory limit errors. */
3672  det_ctx->lua_memory_limit_errors =
3673  StatsRegisterCounter("detect.lua.memory_limit_errors", &tv->stats);
3674 
3675  det_ctx->json_content = NULL;
3676  det_ctx->json_content_capacity = 0;
3677  det_ctx->json_content_len = 0;
3678 
3679 #ifdef PROFILING
3680  det_ctx->counter_mpm_list = StatsRegisterAvgCounter("detect.mpm_list", &tv->stats);
3681  det_ctx->counter_match_list = StatsRegisterAvgCounter("detect.match_list", &tv->stats);
3682 #endif
3683 
3685  DetectEngineMasterCtx *master = &g_master_de_ctx;
3686  SCMutexLock(&master->lock);
3687  if (DetectEngineThreadCtxInitForMT(tv, det_ctx) != TM_ECODE_OK) {
3688  DetectEngineThreadCtxDeinit(tv, det_ctx);
3689  SCMutexUnlock(&master->lock);
3690  return TM_ECODE_FAILED;
3691  }
3692  SCMutexUnlock(&master->lock);
3693  }
3694 
3695  /* pass thread data back to caller */
3696  *data = (void *)det_ctx;
3697 
3698  return TM_ECODE_OK;
3699 }
3700 
3701 /**
3702  * \internal
3703  * \brief initialize a det_ctx for reload cases
3704  * \param new_de_ctx the new detection engine
3705  * \param mt flag to indicate if MT should be set up for this det_ctx
3706  * this should only be done for the 'root' det_ctx
3707  *
3708  * \retval det_ctx detection engine thread ctx or NULL in case of error
3709  */
3711  ThreadVars *tv, DetectEngineCtx *new_de_ctx, int mt)
3712 {
3713  DetectEngineThreadCtx *det_ctx = SCCalloc(1, sizeof(DetectEngineThreadCtx));
3714  if (unlikely(det_ctx == NULL))
3715  return NULL;
3716 
3717  det_ctx->tenant_id = new_de_ctx->tenant_id;
3718  det_ctx->tv = tv;
3719  det_ctx->de_ctx = DetectEngineReference(new_de_ctx);
3720  if (det_ctx->de_ctx == NULL) {
3721  SCFree(det_ctx);
3722  return NULL;
3723  }
3724 
3725  /* most of the init happens here */
3726  if (det_ctx->de_ctx->type == DETECT_ENGINE_TYPE_NORMAL ||
3727  det_ctx->de_ctx->type == DETECT_ENGINE_TYPE_TENANT)
3728  {
3729  if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
3730  DetectEngineDeReference(&det_ctx->de_ctx);
3731  SCFree(det_ctx);
3732  return NULL;
3733  }
3734  }
3735 
3736  /** alert counter setup */
3737  det_ctx->counter_alerts = StatsRegisterCounter("detect.alert", &tv->stats);
3738  det_ctx->counter_alerts_overflow =
3739  StatsRegisterCounter("detect.alert_queue_overflow", &tv->stats);
3740  if (EngineModeIsFirewall()) {
3742  StatsRegisterCounter("firewall.discarded_alerts", &tv->stats);
3743  }
3744  det_ctx->counter_alerts_suppressed =
3745  StatsRegisterCounter("detect.alerts_suppressed", &tv->stats);
3746 #ifdef PROFILING
3747  det_ctx->counter_mpm_list = StatsRegisterAvgCounter("detect.mpm_list", &tv->stats);
3748  det_ctx->counter_match_list = StatsRegisterAvgCounter("detect.match_list", &tv->stats);
3749 #endif
3750 
3751  if (mt && DetectEngineMultiTenantEnabledWithLock()) {
3752  if (DetectEngineThreadCtxInitForMT(tv, det_ctx) != TM_ECODE_OK) {
3753  DetectEngineDeReference(&det_ctx->de_ctx);
3754  SCFree(det_ctx);
3755  return NULL;
3756  }
3757  }
3758 
3759  return det_ctx;
3760 }
3761 
3762 static void DetectEngineThreadCtxFree(DetectEngineThreadCtx *det_ctx)
3763 {
3764 #if DEBUG
3765  SCLogDebug("PACKET PKT_STREAM_ADD: %"PRIu64, det_ctx->pkt_stream_add_cnt);
3766 
3767  SCLogDebug("PAYLOAD MPM %"PRIu64"/%"PRIu64, det_ctx->payload_mpm_cnt, det_ctx->payload_mpm_size);
3768  SCLogDebug("STREAM MPM %"PRIu64"/%"PRIu64, det_ctx->stream_mpm_cnt, det_ctx->stream_mpm_size);
3769 
3770  SCLogDebug("PAYLOAD SIG %"PRIu64"/%"PRIu64, det_ctx->payload_persig_cnt, det_ctx->payload_persig_size);
3771  SCLogDebug("STREAM SIG %"PRIu64"/%"PRIu64, det_ctx->stream_persig_cnt, det_ctx->stream_persig_size);
3772 #endif
3773 
3774  if (det_ctx->tenant_array != NULL) {
3775  SCFree(det_ctx->tenant_array);
3776  det_ctx->tenant_array = NULL;
3777  }
3778 
3779 #ifdef PROFILE_RULES
3780  SCProfilingRuleThreadCleanup(det_ctx);
3781 #endif
3782 #ifdef PROFILING
3785  SCProfilingSghThreadCleanup(det_ctx);
3786 #endif
3787 
3788  /** \todo get rid of this static */
3789  if (det_ctx->de_ctx != NULL) {
3790  PatternMatchThreadDestroy(&det_ctx->mtc, det_ctx->de_ctx->mpm_matcher);
3791  }
3792 
3793  PmqFree(&det_ctx->pmq);
3794 
3795  if (det_ctx->spm_thread_ctx != NULL) {
3797  }
3798  if (det_ctx->match_array != NULL)
3799  SCFree(det_ctx->match_array);
3800  if (det_ctx->replace != NULL)
3801  SCFree(det_ctx->replace);
3802 
3804 
3805  AlertQueueFree(det_ctx);
3806 
3807  if (det_ctx->post_rule_work_queue.q)
3808  SCFree(det_ctx->post_rule_work_queue.q);
3809 
3810  if (det_ctx->byte_values != NULL)
3811  SCFree(det_ctx->byte_values);
3812 
3813  /* Decoded base64 data. */
3814  if (det_ctx->base64_decoded != NULL) {
3815  SCFree(det_ctx->base64_decoded);
3816  }
3817 
3818  if (det_ctx->inspect.buffers) {
3819  for (uint32_t i = 0; i < det_ctx->inspect.buffers_size; i++) {
3820  InspectionBufferFree(&det_ctx->inspect.buffers[i]);
3821  }
3822  SCFree(det_ctx->inspect.buffers);
3823  }
3824  if (det_ctx->inspect.to_clear_queue) {
3825  SCFree(det_ctx->inspect.to_clear_queue);
3826  }
3827  if (det_ctx->multi_inspect.buffers) {
3828  for (uint32_t i = 0; i < det_ctx->multi_inspect.buffers_size; i++) {
3830  for (uint32_t x = 0; x < fb->size; x++) {
3832  }
3834  }
3835  SCFree(det_ctx->multi_inspect.buffers);
3836  }
3837  if (det_ctx->multi_inspect.to_clear_queue) {
3839  }
3840 
3841  DetectEngineThreadCtxDeinitGlobalKeywords(det_ctx);
3842  if (det_ctx->de_ctx != NULL) {
3843  DetectEngineThreadCtxDeinitKeywords(det_ctx->de_ctx, det_ctx);
3844 #ifdef UNITTESTS
3845  if (!RunmodeIsUnittests() || det_ctx->de_ctx->ref_cnt > 0)
3846  DetectEngineDeReference(&det_ctx->de_ctx);
3847 #else
3848  DetectEngineDeReference(&det_ctx->de_ctx);
3849 #endif
3850  }
3851 
3852  if (det_ctx->json_content) {
3853  SCFree(det_ctx->json_content);
3854  det_ctx->json_content = NULL;
3855  det_ctx->json_content_capacity = 0;
3856  }
3857 
3860  SCFree(det_ctx);
3861 }
3862 
3864 {
3865  DetectEngineThreadCtx *det_ctx = (DetectEngineThreadCtx *)data;
3866 
3867  if (det_ctx == NULL) {
3868  SCLogWarning("argument \"data\" NULL");
3869  return TM_ECODE_OK;
3870  }
3871 
3872  if (det_ctx->mt_det_ctxs_hash != NULL) {
3873  HashTableFree(det_ctx->mt_det_ctxs_hash);
3874  det_ctx->mt_det_ctxs_hash = NULL;
3875  }
3876  DetectEngineThreadCtxFree(det_ctx);
3877 
3878  return TM_ECODE_OK;
3879 }
3880 
3881 static uint32_t DetectKeywordCtxHashFunc(HashListTable *ht, void *data, uint16_t datalen)
3882 {
3884  const char *name = ctx->name;
3885  uint64_t hash =
3886  StringHashDjb2((const uint8_t *)name, (uint32_t)strlen(name)) + (ptrdiff_t)ctx->data;
3887  hash %= ht->array_size;
3888  return (uint32_t)hash;
3889 }
3890 
3891 static char DetectKeywordCtxCompareFunc(void *data1, uint16_t len1, void *data2, uint16_t len2)
3892 {
3893  DetectEngineThreadKeywordCtxItem *ctx1 = data1;
3894  DetectEngineThreadKeywordCtxItem *ctx2 = data2;
3895  const char *name1 = ctx1->name;
3896  const char *name2 = ctx2->name;
3897  return (strcmp(name1, name2) == 0 && ctx1->data == ctx2->data);
3898 }
3899 
3900 static void DetectKeywordCtxFreeFunc(void *ptr)
3901 {
3902  SCFree(ptr);
3903 }
3904 
3905 /** \brief Register Thread keyword context Funcs
3906  *
3907  * \param de_ctx detection engine to register in
3908  * \param name keyword name for error printing
3909  * \param InitFunc function ptr
3910  * \param data keyword init data to pass to Func. Can be NULL.
3911  * \param FreeFunc function ptr
3912  * \param mode 0 normal (ctx per keyword instance) 1 shared (one ctx per det_ct)
3913  *
3914  * \retval id for retrieval of ctx at runtime
3915  * \retval -1 on error
3916  *
3917  * \note make sure "data" remains valid and it free'd elsewhere. It's
3918  * recommended to store it in the keywords global ctx so that
3919  * it's freed when the de_ctx is freed.
3920  */
3921 int DetectRegisterThreadCtxFuncs(DetectEngineCtx *de_ctx, const char *name, void *(*InitFunc)(void *), void *data, void (*FreeFunc)(void *), int mode)
3922 {
3923  BUG_ON(de_ctx == NULL || InitFunc == NULL || FreeFunc == NULL);
3924 
3925  if (de_ctx->keyword_hash == NULL) {
3926  de_ctx->keyword_hash = HashListTableInit(4096, // TODO
3927  DetectKeywordCtxHashFunc, DetectKeywordCtxCompareFunc, DetectKeywordCtxFreeFunc);
3928  BUG_ON(de_ctx->keyword_hash == NULL);
3929  }
3930 
3931  if (mode) {
3932  DetectEngineThreadKeywordCtxItem search = { .data = data, .name = name };
3933 
3935  HashListTableLookup(de_ctx->keyword_hash, (void *)&search, 0);
3936  if (item)
3937  return item->id;
3938 
3939  /* fall through */
3940  }
3941 
3943  if (unlikely(item == NULL))
3944  return -1;
3945 
3946  item->InitFunc = InitFunc;
3947  item->FreeFunc = FreeFunc;
3948  item->data = data;
3949  item->name = name;
3950  item->id = de_ctx->keyword_id++;
3951 
3952  if (HashListTableAdd(de_ctx->keyword_hash, (void *)item, 0) < 0) {
3953  SCFree(item);
3954  return -1;
3955  }
3956  return item->id;
3957 }
3958 
3959 /** \brief Remove Thread keyword context registration
3960  *
3961  * \param de_ctx detection engine to deregister from
3962  * \param det_ctx detection engine thread context to deregister from
3963  * \param data keyword init data to pass to Func. Can be NULL.
3964  * \param name keyword name for error printing
3965  *
3966  * \retval 1 Item unregistered
3967  * \retval 0 otherwise
3968  *
3969  * \note make sure "data" remains valid and it free'd elsewhere. It's
3970  * recommended to store it in the keywords global ctx so that
3971  * it's freed when the de_ctx is freed.
3972  */
3974 {
3975  /* might happen if we call this before a call to *Register* */
3976  if (de_ctx->keyword_hash == NULL)
3977  return 1;
3978  DetectEngineThreadKeywordCtxItem remove = { .data = data, .name = name };
3979  if (HashListTableRemove(de_ctx->keyword_hash, (void *)&remove, 0) == 0)
3980  return 1;
3981  return 0;
3982 }
3983 /** \brief Retrieve thread local keyword ctx by id
3984  *
3985  * \param det_ctx detection engine thread ctx to retrieve the ctx from
3986  * \param id id of the ctx returned by DetectRegisterThreadCtxInitFunc at
3987  * keyword init.
3988  *
3989  * \retval ctx or NULL on error
3990  */
3992 {
3993  if (id < 0 || id > det_ctx->keyword_ctxs_size || det_ctx->keyword_ctxs_array == NULL)
3994  return NULL;
3995 
3996  return det_ctx->keyword_ctxs_array[id];
3997 }
3998 
3999 
4000 /** \brief Register Thread keyword context Funcs (Global)
4001  *
4002  * IDs stay static over reloads and between tenants
4003  *
4004  * \param name keyword name for error printing
4005  * \param InitFunc function ptr
4006  * \param FreeFunc function ptr
4007  *
4008  * \retval id for retrieval of ctx at runtime
4009  * \retval -1 on error
4010  */
4012  const char *name, void *(*InitFunc)(void *), void *data, void (*FreeFunc)(void *))
4013 {
4014  int id;
4015  BUG_ON(InitFunc == NULL || FreeFunc == NULL);
4016 
4017  DetectEngineMasterCtx *master = &g_master_de_ctx;
4018 
4019  /* if already registered, return existing id */
4021  while (item != NULL) {
4022  if (strcmp(name, item->name) == 0) {
4023  id = item->id;
4024  return id;
4025  }
4026 
4027  item = item->next;
4028  }
4029 
4030  item = SCCalloc(1, sizeof(*item));
4031  if (unlikely(item == NULL)) {
4032  return -1;
4033  }
4034  item->InitFunc = InitFunc;
4035  item->FreeFunc = FreeFunc;
4036  item->name = name;
4037  item->data = data;
4038 
4039  item->next = master->keyword_list;
4040  master->keyword_list = item;
4041  item->id = master->keyword_id++;
4042 
4043  id = item->id;
4044  return id;
4045 }
4046 
4047 /** \brief Retrieve thread local keyword ctx by id
4048  *
4049  * \param det_ctx detection engine thread ctx to retrieve the ctx from
4050  * \param id id of the ctx returned by DetectRegisterThreadCtxInitFunc at
4051  * keyword init.
4052  *
4053  * \retval ctx or NULL on error
4054  */
4056 {
4057  if (id < 0 || id > det_ctx->global_keyword_ctxs_size ||
4058  det_ctx->global_keyword_ctxs_array == NULL) {
4059  return NULL;
4060  }
4061 
4062  return det_ctx->global_keyword_ctxs_array[id];
4063 }
4064 
4065 /** \brief Check if detection is enabled
4066  * \retval bool true or false */
4068 {
4069  DetectEngineMasterCtx *master = &g_master_de_ctx;
4070  SCMutexLock(&master->lock);
4071 
4072  if (master->list == NULL) {
4073  SCMutexUnlock(&master->lock);
4074  return 0;
4075  }
4076 
4077  SCMutexUnlock(&master->lock);
4078  return 1;
4079 }
4080 
4082 {
4083  uint32_t version;
4084  DetectEngineMasterCtx *master = &g_master_de_ctx;
4085  SCMutexLock(&master->lock);
4086  version = master->version;
4087  SCMutexUnlock(&master->lock);
4088  return version;
4089 }
4090 
4092 {
4093  DetectEngineMasterCtx *master = &g_master_de_ctx;
4094  SCMutexLock(&master->lock);
4095  master->version++;
4096  SCLogDebug("master version now %u", master->version);
4097  SCMutexUnlock(&master->lock);
4098 }
4099 
4101 {
4102  DetectEngineMasterCtx *master = &g_master_de_ctx;
4103  SCMutexLock(&master->lock);
4104 
4105  DetectEngineCtx *de_ctx = master->list;
4106  while (de_ctx) {
4110  {
4111  de_ctx->ref_cnt++;
4112  SCLogDebug("de_ctx %p ref_cnt %u", de_ctx, de_ctx->ref_cnt);
4113  SCMutexUnlock(&master->lock);
4114  return de_ctx;
4115  }
4116  de_ctx = de_ctx->next;
4117  }
4118 
4119  SCMutexUnlock(&master->lock);
4120  return NULL;
4121 }
4122 
4124 {
4125  if (de_ctx == NULL)
4126  return NULL;
4127  de_ctx->ref_cnt++;
4128  return de_ctx;
4129 }
4130 
4131 static bool DetectEngineMultiTenantEnabledWithLock(void)
4132 {
4133  DetectEngineMasterCtx *master = &g_master_de_ctx;
4134  return master->multi_tenant_enabled;
4135 }
4136 
4138 {
4139  DetectEngineMasterCtx *master = &g_master_de_ctx;
4140  SCMutexLock(&master->lock);
4141  bool enabled = DetectEngineMultiTenantEnabledWithLock();
4142  SCMutexUnlock(&master->lock);
4143  return enabled;
4144 }
4145 
4146 /** \internal
4147  * \brief load a tenant from a yaml file
4148  *
4149  * \param tenant_id the tenant id by which the config is known
4150  * \param filename full path of a yaml file
4151  * \param loader_id id of loader thread or -1
4152  *
4153  * \retval 0 ok
4154  * \retval -1 failed
4155  */
4156 static int DetectEngineMultiTenantLoadTenant(uint32_t tenant_id, const char *filename, int loader_id)
4157 {
4158  DetectEngineCtx *de_ctx = NULL;
4159  char prefix[64];
4160 
4161  snprintf(prefix, sizeof(prefix), "multi-detect.%u", tenant_id);
4162 
4163  SCStat st;
4164  if (SCStatFn(filename, &st) != 0) {
4165  SCLogError("failed to stat file %s", filename);
4166  goto error;
4167  }
4168 
4169  de_ctx = DetectEngineGetByTenantId(tenant_id);
4170  if (de_ctx != NULL) {
4171  SCLogError("tenant %u already registered", tenant_id);
4173  goto error;
4174  }
4175 
4176  SCConfNode *node = SCConfGetNode(prefix);
4177  if (node == NULL) {
4178  SCLogError("failed to properly setup yaml %s", filename);
4179  goto error;
4180  }
4181 
4182  de_ctx = DetectEngineCtxInitWithPrefix(prefix, tenant_id);
4183  if (de_ctx == NULL) {
4184  SCLogError("initializing detection engine "
4185  "context failed.");
4186  goto error;
4187  }
4188  SCLogDebug("de_ctx %p with prefix %s", de_ctx, de_ctx->config_prefix);
4189 
4191  de_ctx->tenant_id = tenant_id;
4192  de_ctx->loader_id = loader_id;
4193  de_ctx->tenant_path = SCStrdup(filename);
4194  if (de_ctx->tenant_path == NULL) {
4195  SCLogError("Failed to duplicate path");
4196  goto error;
4197  }
4198 
4199  if (SigLoadSignatures(de_ctx, NULL, false) < 0) {
4200  SCLogError("Loading signatures failed.");
4201  goto error;
4202  }
4204 
4205  return 0;
4206 
4207 error:
4208  if (de_ctx != NULL) {
4210  }
4211  return -1;
4212 }
4213 
4214 static int DetectEngineMultiTenantReloadTenant(uint32_t tenant_id, const char *filename, int reload_cnt)
4215 {
4216  DetectEngineCtx *old_de_ctx = DetectEngineGetByTenantId(tenant_id);
4217  if (old_de_ctx == NULL) {
4218  SCLogError("tenant detect engine not found");
4219  return -1;
4220  }
4221 
4222  if (filename == NULL)
4223  filename = old_de_ctx->tenant_path;
4224 
4225  char prefix[64];
4226  snprintf(prefix, sizeof(prefix), "multi-detect.%u.reload.%d", tenant_id, reload_cnt);
4227  reload_cnt++;
4228  SCLogDebug("prefix %s", prefix);
4229 
4230  if (SCConfYamlLoadFileWithPrefix(filename, prefix) != 0) {
4231  SCLogError("failed to load yaml");
4232  goto error;
4233  }
4234 
4235  SCConfNode *node = SCConfGetNode(prefix);
4236  if (node == NULL) {
4237  SCLogError("failed to properly setup yaml %s", filename);
4238  goto error;
4239  }
4240 
4241  DetectEngineCtx *new_de_ctx = DetectEngineCtxInitWithPrefix(prefix, tenant_id);
4242  if (new_de_ctx == NULL) {
4243  SCLogError("initializing detection engine "
4244  "context failed.");
4245  goto error;
4246  }
4247  SCLogDebug("de_ctx %p with prefix %s", new_de_ctx, new_de_ctx->config_prefix);
4248 
4249  new_de_ctx->type = DETECT_ENGINE_TYPE_TENANT;
4250  new_de_ctx->tenant_id = tenant_id;
4251  new_de_ctx->loader_id = old_de_ctx->loader_id;
4252  new_de_ctx->tenant_path = SCStrdup(filename);
4253  if (new_de_ctx->tenant_path == NULL) {
4254  SCLogError("Failed to duplicate path");
4255  goto new_de_ctx_error;
4256  }
4257 
4258  if (SigLoadSignatures(new_de_ctx, NULL, false) < 0) {
4259  SCLogError("Loading signatures failed.");
4260  goto new_de_ctx_error;
4261  }
4262 
4263  DetectEngineAddToMaster(new_de_ctx);
4264 
4265  /* move to free list */
4266  DetectEngineMoveToFreeList(old_de_ctx);
4267  DetectEngineDeReference(&old_de_ctx);
4268  return 0;
4269 
4270 new_de_ctx_error:
4271  DetectEngineCtxFree(new_de_ctx);
4272 
4273 error:
4274  DetectEngineDeReference(&old_de_ctx);
4275  return -1;
4276 }
4277 
4278 
4279 typedef struct TenantLoaderCtx_ {
4280  uint32_t tenant_id;
4281  int reload_cnt; /**< used by reload */
4282  char *yaml; /**< heap alloc'd copy of file path for the yaml */
4284 
4285 static void DetectLoaderFreeTenant(void *ctx)
4286 {
4288  if (t->yaml != NULL) {
4289  SCFree(t->yaml);
4290  }
4291  SCFree(t);
4292 }
4293 
4294 static int DetectLoaderFuncLoadTenant(void *vctx, int loader_id)
4295 {
4296  TenantLoaderCtx *ctx = (TenantLoaderCtx *)vctx;
4297 
4298  SCLogDebug("loader %d", loader_id);
4299  if (DetectEngineMultiTenantLoadTenant(ctx->tenant_id, ctx->yaml, loader_id) != 0) {
4300  return -1;
4301  }
4302  return 0;
4303 }
4304 
4305 static int DetectLoaderSetupLoadTenant(uint32_t tenant_id, const char *yaml)
4306 {
4307  TenantLoaderCtx *t = SCCalloc(1, sizeof(*t));
4308  if (t == NULL)
4309  return -ENOMEM;
4310 
4311  t->tenant_id = tenant_id;
4312  t->yaml = SCStrdup(yaml);
4313  if (t->yaml == NULL) {
4314  SCFree(t);
4315  return -ENOMEM;
4316  }
4317 
4318  return DetectLoaderQueueTask(-1, DetectLoaderFuncLoadTenant, t, DetectLoaderFreeTenant);
4319 }
4320 
4321 static int DetectLoaderFuncReloadTenant(void *vctx, int loader_id)
4322 {
4323  TenantLoaderCtx *ctx = (TenantLoaderCtx *)vctx;
4324 
4325  SCLogDebug("loader_id %d", loader_id);
4326 
4327  if (DetectEngineMultiTenantReloadTenant(ctx->tenant_id, ctx->yaml, ctx->reload_cnt) != 0) {
4328  return -1;
4329  }
4330  return 0;
4331 }
4332 
4333 static int DetectLoaderSetupReloadTenants(const int reload_cnt)
4334 {
4335  int ret = 0;
4336  DetectEngineMasterCtx *master = &g_master_de_ctx;
4337  SCMutexLock(&master->lock);
4338 
4339  DetectEngineCtx *de_ctx = master->list;
4340  while (de_ctx) {
4342  TenantLoaderCtx *t = SCCalloc(1, sizeof(*t));
4343  if (t == NULL) {
4344  ret = -1;
4345  goto error;
4346  }
4347  t->tenant_id = de_ctx->tenant_id;
4348  t->reload_cnt = reload_cnt;
4349  int loader_id = de_ctx->loader_id;
4350 
4351  int r = DetectLoaderQueueTask(
4352  loader_id, DetectLoaderFuncReloadTenant, t, DetectLoaderFreeTenant);
4353  if (r < 0) {
4354  ret = -2;
4355  goto error;
4356  }
4357  }
4358 
4359  de_ctx = de_ctx->next;
4360  }
4361 error:
4362  SCMutexUnlock(&master->lock);
4363  return ret;
4364 }
4365 
4366 static int DetectLoaderSetupReloadTenant(uint32_t tenant_id, const char *yaml, int reload_cnt)
4367 {
4368  DetectEngineCtx *old_de_ctx = DetectEngineGetByTenantId(tenant_id);
4369  if (old_de_ctx == NULL)
4370  return -ENOENT;
4371  int loader_id = old_de_ctx->loader_id;
4372  DetectEngineDeReference(&old_de_ctx);
4373 
4374  TenantLoaderCtx *t = SCCalloc(1, sizeof(*t));
4375  if (t == NULL)
4376  return -ENOMEM;
4377 
4378  t->tenant_id = tenant_id;
4379  if (yaml != NULL) {
4380  t->yaml = SCStrdup(yaml);
4381  if (t->yaml == NULL) {
4382  SCFree(t);
4383  return -ENOMEM;
4384  }
4385  }
4386  t->reload_cnt = reload_cnt;
4387 
4388  SCLogDebug("loader_id %d", loader_id);
4389 
4390  return DetectLoaderQueueTask(
4391  loader_id, DetectLoaderFuncReloadTenant, t, DetectLoaderFreeTenant);
4392 }
4393 
4394 /** \brief Load a tenant and wait for loading to complete
4395  */
4396 int DetectEngineLoadTenantBlocking(uint32_t tenant_id, const char *yaml)
4397 {
4398  int r = DetectLoaderSetupLoadTenant(tenant_id, yaml);
4399  if (r < 0)
4400  return r;
4401 
4402  if (DetectLoadersSync() != 0)
4403  return -1;
4404 
4405  return 0;
4406 }
4407 
4408 /** \brief Reload a tenant and wait for loading to complete
4409  */
4410 int DetectEngineReloadTenantBlocking(uint32_t tenant_id, const char *yaml, int reload_cnt)
4411 {
4412  int r = DetectLoaderSetupReloadTenant(tenant_id, yaml, reload_cnt);
4413  if (r < 0)
4414  return r;
4415 
4416  if (DetectLoadersSync() != 0)
4417  return -1;
4418 
4419  return 0;
4420 }
4421 
4422 /** \brief Reload all tenants and wait for loading to complete
4423  */
4424 int DetectEngineReloadTenantsBlocking(const int reload_cnt)
4425 {
4426  int r = DetectLoaderSetupReloadTenants(reload_cnt);
4427  if (r < 0)
4428  return r;
4429 
4430  if (DetectLoadersSync() != 0)
4431  return -1;
4432 
4433  return 0;
4434 }
4435 
4436 static int DetectEngineMultiTenantSetupLoadLivedevMappings(
4437  const SCConfNode *mappings_root_node, bool failure_fatal)
4438 {
4439  SCConfNode *mapping_node = NULL;
4440 
4441  int mapping_cnt = 0;
4442  if (mappings_root_node != NULL) {
4443  TAILQ_FOREACH(mapping_node, &mappings_root_node->head, next) {
4444  SCConfNode *tenant_id_node = SCConfNodeLookupChild(mapping_node, "tenant-id");
4445  if (tenant_id_node == NULL)
4446  goto bad_mapping;
4447  SCConfNode *device_node = SCConfNodeLookupChild(mapping_node, "device");
4448  if (device_node == NULL)
4449  goto bad_mapping;
4450 
4451  uint32_t tenant_id = 0;
4452  if (StringParseUint32(&tenant_id, 10, (uint16_t)strlen(tenant_id_node->val),
4453  tenant_id_node->val) < 0) {
4454  SCLogError("tenant-id "
4455  "of %s is invalid",
4456  tenant_id_node->val);
4457  goto bad_mapping;
4458  }
4459 
4460  const char *dev = device_node->val;
4461  LiveDevice *ld = LiveGetDevice(dev);
4462  if (ld == NULL) {
4463  SCLogWarning("device %s not found", dev);
4464  goto bad_mapping;
4465  }
4466 
4467  if (ld->tenant_id_set) {
4468  SCLogWarning("device %s already mapped to tenant-id %u", dev, ld->tenant_id);
4469  goto bad_mapping;
4470  }
4471 
4472  ld->tenant_id = tenant_id;
4473  ld->tenant_id_set = true;
4474 
4475  if (DetectEngineTenantRegisterLivedev(tenant_id, ld->id) != 0) {
4476  goto error;
4477  }
4478 
4479  SCLogConfig("device %s connected to tenant-id %u", dev, tenant_id);
4480  mapping_cnt++;
4481  continue;
4482 
4483  bad_mapping:
4484  if (failure_fatal)
4485  goto error;
4486  }
4487  }
4488  SCLogConfig("%d device - tenant-id mappings defined", mapping_cnt);
4489  return mapping_cnt;
4490 
4491 error:
4492  return 0;
4493 }
4494 
4495 static int DetectEngineMultiTenantSetupLoadVlanMappings(
4496  const SCConfNode *mappings_root_node, bool failure_fatal)
4497 {
4498  SCConfNode *mapping_node = NULL;
4499 
4500  int mapping_cnt = 0;
4501  if (mappings_root_node != NULL) {
4502  TAILQ_FOREACH(mapping_node, &mappings_root_node->head, next) {
4503  SCConfNode *tenant_id_node = SCConfNodeLookupChild(mapping_node, "tenant-id");
4504  if (tenant_id_node == NULL)
4505  goto bad_mapping;
4506  SCConfNode *vlan_id_node = SCConfNodeLookupChild(mapping_node, "vlan-id");
4507  if (vlan_id_node == NULL)
4508  goto bad_mapping;
4509 
4510  uint32_t tenant_id = 0;
4511  if (StringParseUint32(&tenant_id, 10, (uint16_t)strlen(tenant_id_node->val),
4512  tenant_id_node->val) < 0) {
4513  SCLogError("tenant-id "
4514  "of %s is invalid",
4515  tenant_id_node->val);
4516  goto bad_mapping;
4517  }
4518 
4519  uint16_t vlan_id = 0;
4520  if (StringParseUint16(
4521  &vlan_id, 10, (uint16_t)strlen(vlan_id_node->val), vlan_id_node->val) < 0) {
4522  SCLogError("vlan-id "
4523  "of %s is invalid",
4524  vlan_id_node->val);
4525  goto bad_mapping;
4526  }
4527  if (vlan_id == 0 || vlan_id >= 4095) {
4528  SCLogError("vlan-id "
4529  "of %s is invalid. Valid range 1-4094.",
4530  vlan_id_node->val);
4531  goto bad_mapping;
4532  }
4533 
4534  if (DetectEngineTenantRegisterVlanId(tenant_id, vlan_id) != 0) {
4535  goto error;
4536  }
4537  SCLogConfig("vlan %u connected to tenant-id %u", vlan_id, tenant_id);
4538  mapping_cnt++;
4539  continue;
4540 
4541  bad_mapping:
4542  if (failure_fatal)
4543  goto error;
4544  }
4545  }
4546  return mapping_cnt;
4547 
4548 error:
4549  return 0;
4550 }
4551 
4552 /**
4553  * \brief setup multi-detect / multi-tenancy
4554  *
4555  * See if MT is enabled. If so, setup the selector, tenants and mappings.
4556  * Tenants and mappings are optional, and can also dynamically be added
4557  * and removed from the unix socket.
4558  */
4559 int DetectEngineMultiTenantSetup(const bool unix_socket)
4560 {
4562  DetectEngineMasterCtx *master = &g_master_de_ctx;
4563  int failure_fatal = 0;
4564  (void)SCConfGetBool("engine.init-failure-fatal", &failure_fatal);
4565 
4566  int enabled = 0;
4567  (void)SCConfGetBool("multi-detect.enabled", &enabled);
4568  if (enabled == 1) {
4573 
4574  SCMutexLock(&master->lock);
4575  master->multi_tenant_enabled = 1;
4576 
4577  const char *handler = NULL;
4578  if (SCConfGetNonNull("multi-detect.selector", &handler) == 1) {
4579  SCLogConfig("multi-tenant selector type %s", handler);
4580 
4581  if (strcmp(handler, "vlan") == 0) {
4582  tenant_selector = master->tenant_selector = TENANT_SELECTOR_VLAN;
4583 
4584  int vlanbool = 0;
4585  if ((SCConfGetBool("vlan.use-for-tracking", &vlanbool)) == 1 && vlanbool == 0) {
4586  SCLogError("vlan tracking is disabled, "
4587  "can't use multi-detect selector 'vlan'");
4588  SCMutexUnlock(&master->lock);
4589  goto error;
4590  }
4591 
4592  } else if (strcmp(handler, "direct") == 0) {
4593  tenant_selector = master->tenant_selector = TENANT_SELECTOR_DIRECT;
4594  } else if (strcmp(handler, "device") == 0) {
4595  tenant_selector = master->tenant_selector = TENANT_SELECTOR_LIVEDEV;
4596  if (EngineModeIsIPS()) {
4597  SCLogWarning("multi-tenant 'device' mode not supported for IPS");
4598  SCMutexUnlock(&master->lock);
4599  goto error;
4600  }
4601 
4602  } else {
4603  SCLogError("unknown value %s "
4604  "multi-detect.selector",
4605  handler);
4606  SCMutexUnlock(&master->lock);
4607  goto error;
4608  }
4609  }
4610  SCMutexUnlock(&master->lock);
4611  SCLogConfig("multi-detect is enabled (multi tenancy). Selector: %s", handler);
4612 
4613  /* traffic -- tenant mappings */
4614  SCConfNode *mappings_root_node = SCConfGetNode("multi-detect.mappings");
4615 
4616  if (tenant_selector == TENANT_SELECTOR_VLAN) {
4617  int mapping_cnt = DetectEngineMultiTenantSetupLoadVlanMappings(mappings_root_node,
4618  failure_fatal);
4619  if (mapping_cnt == 0) {
4620  /* no mappings are valid when we're in unix socket mode,
4621  * they can be added on the fly. Otherwise warn/error
4622  * depending on failure_fatal */
4623 
4624  if (unix_socket) {
4625  SCLogNotice("no tenant traffic mappings defined, "
4626  "tenants won't be used until mappings are added");
4627  } else {
4628  if (failure_fatal) {
4629  SCLogError("no multi-detect mappings defined");
4630  goto error;
4631  } else {
4632  SCLogWarning("no multi-detect mappings defined");
4633  }
4634  }
4635  }
4636  } else if (tenant_selector == TENANT_SELECTOR_LIVEDEV) {
4637  int mapping_cnt = DetectEngineMultiTenantSetupLoadLivedevMappings(mappings_root_node,
4638  failure_fatal);
4639  if (mapping_cnt == 0) {
4640  if (failure_fatal) {
4641  SCLogError("no multi-detect mappings defined");
4642  goto error;
4643  } else {
4644  SCLogWarning("no multi-detect mappings defined");
4645  }
4646  }
4647  }
4648 
4649  /* tenants */
4650  SCConfNode *tenants_root_node = SCConfGetNode("multi-detect.tenants");
4651  SCConfNode *tenant_node = NULL;
4652 
4653  if (tenants_root_node != NULL) {
4654  const char *path = NULL;
4655  SCConfNode *path_node = SCConfGetNode("multi-detect.config-path");
4656  if (path_node) {
4657  path = path_node->val;
4658  SCLogConfig("tenants config path: %s", path);
4659  }
4660 
4661  TAILQ_FOREACH(tenant_node, &tenants_root_node->head, next) {
4662  SCConfNode *id_node = SCConfNodeLookupChild(tenant_node, "id");
4663  if (id_node == NULL) {
4664  goto bad_tenant;
4665  }
4666  SCConfNode *yaml_node = SCConfNodeLookupChild(tenant_node, "yaml");
4667  if (yaml_node == NULL) {
4668  goto bad_tenant;
4669  }
4670 
4671  uint32_t tenant_id = 0;
4672  if (StringParseUint32(
4673  &tenant_id, 10, (uint16_t)strlen(id_node->val), id_node->val) < 0) {
4674  SCLogError("tenant_id "
4675  "of %s is invalid",
4676  id_node->val);
4677  goto bad_tenant;
4678  }
4679  SCLogDebug("tenant id: %u, %s", tenant_id, yaml_node->val);
4680 
4681  char yaml_path[PATH_MAX] = "";
4682  if (path) {
4683  if (PathMerge(yaml_path, PATH_MAX, path, yaml_node->val) < 0)
4684  goto bad_tenant;
4685  } else {
4686  size_t r = strlcpy(yaml_path, yaml_node->val, sizeof(yaml_path));
4687  if (r >= sizeof(yaml_path))
4688  goto bad_tenant;
4689  }
4690  SCLogDebug("tenant path: %s", yaml_path);
4691 
4692  /* setup the yaml in this loop so that it's not done by the loader
4693  * threads. SCConfYamlLoadFileWithPrefix is not thread safe. */
4694  char prefix[64];
4695  snprintf(prefix, sizeof(prefix), "multi-detect.%u", tenant_id);
4696  if (SCConfYamlLoadFileWithPrefix(yaml_path, prefix) != 0) {
4697  SCLogError("failed to load yaml %s", yaml_path);
4698  goto bad_tenant;
4699  }
4700 
4701  int r = DetectLoaderSetupLoadTenant(tenant_id, yaml_path);
4702  if (r < 0) {
4703  /* error logged already */
4704  goto bad_tenant;
4705  }
4706  continue;
4707 
4708  bad_tenant:
4709  if (failure_fatal)
4710  goto error;
4711  }
4712  }
4713 
4714  /* wait for our loaders to complete their tasks */
4715  if (DetectLoadersSync() != 0) {
4716  goto error;
4717  }
4718 
4720 
4721  } else {
4722  SCLogDebug("multi-detect not enabled (multi tenancy)");
4723  }
4724  return 0;
4725 error:
4726  return -1;
4727 }
4728 
4729 static uint32_t DetectEngineTenantGetIdFromVlanId(const void *ctx, const Packet *p)
4730 {
4731  const DetectEngineThreadCtx *det_ctx = ctx;
4732  uint32_t x = 0;
4733  uint32_t vlan_id = 0;
4734 
4735  if (p->vlan_idx == 0)
4736  return 0;
4737 
4738  vlan_id = p->vlan_id[0];
4739 
4740  if (det_ctx == NULL || det_ctx->tenant_array == NULL || det_ctx->tenant_array_size == 0)
4741  return 0;
4742 
4743  /* not very efficient, but for now we're targeting only limited amounts.
4744  * Can use hash/tree approach later. */
4745  for (x = 0; x < det_ctx->tenant_array_size; x++) {
4746  if (det_ctx->tenant_array[x].traffic_id == vlan_id)
4747  return det_ctx->tenant_array[x].tenant_id;
4748  }
4749 
4750  return 0;
4751 }
4752 
4753 static uint32_t DetectEngineTenantGetIdFromLivedev(const void *ctx, const Packet *p)
4754 {
4755  const DetectEngineThreadCtx *det_ctx = ctx;
4756  const LiveDevice *ld = LiveDeviceGetById(p->livedev_id);
4757 
4758  if (ld == NULL || det_ctx == NULL)
4759  return 0;
4760 
4761  SCLogDebug("using tenant-id %u for packet on device %s", ld->tenant_id, ld->dev);
4762  return ld->tenant_id;
4763 }
4764 
4765 static int DetectEngineTenantRegisterSelector(
4766  enum DetectEngineTenantSelectors selector, uint32_t tenant_id, uint32_t traffic_id)
4767 {
4768  DetectEngineMasterCtx *master = &g_master_de_ctx;
4769  SCMutexLock(&master->lock);
4770 
4771  if (!(master->tenant_selector == TENANT_SELECTOR_UNKNOWN || master->tenant_selector == selector)) {
4772  SCLogInfo("conflicting selector already set");
4773  SCMutexUnlock(&master->lock);
4774  return -1;
4775  }
4776 
4778  while (m) {
4779  if (m->traffic_id == traffic_id) {
4780  SCLogInfo("traffic id already registered");
4781  SCMutexUnlock(&master->lock);
4782  return -1;
4783  }
4784  m = m->next;
4785  }
4786 
4787  DetectEngineTenantMapping *map = SCCalloc(1, sizeof(*map));
4788  if (map == NULL) {
4789  SCLogInfo("memory fail");
4790  SCMutexUnlock(&master->lock);
4791  return -1;
4792  }
4793  map->traffic_id = traffic_id;
4794  map->tenant_id = tenant_id;
4795 
4796  map->next = master->tenant_mapping_list;
4797  master->tenant_mapping_list = map;
4798 
4799  master->tenant_selector = selector;
4800 
4801  SCLogDebug("tenant handler %u %u %u registered", selector, tenant_id, traffic_id);
4802  SCMutexUnlock(&master->lock);
4803  return 0;
4804 }
4805 
4806 static int DetectEngineTenantUnregisterSelector(
4807  enum DetectEngineTenantSelectors selector, uint32_t tenant_id, uint32_t traffic_id)
4808 {
4809  DetectEngineMasterCtx *master = &g_master_de_ctx;
4810  SCMutexLock(&master->lock);
4811 
4812  if (master->tenant_mapping_list == NULL) {
4813  SCMutexUnlock(&master->lock);
4814  return -1;
4815  }
4816 
4817  DetectEngineTenantMapping *prev = NULL;
4819  while (map) {
4820  if (map->traffic_id == traffic_id &&
4821  map->tenant_id == tenant_id)
4822  {
4823  if (prev != NULL)
4824  prev->next = map->next;
4825  else
4826  master->tenant_mapping_list = map->next;
4827 
4828  map->next = NULL;
4829  SCFree(map);
4830  SCLogInfo("tenant handler %u %u %u unregistered", selector, tenant_id, traffic_id);
4831  SCMutexUnlock(&master->lock);
4832  return 0;
4833  }
4834  prev = map;
4835  map = map->next;
4836  }
4837 
4838  SCMutexUnlock(&master->lock);
4839  return -1;
4840 }
4841 
4842 int DetectEngineTenantRegisterLivedev(uint32_t tenant_id, int device_id)
4843 {
4844  return DetectEngineTenantRegisterSelector(
4845  TENANT_SELECTOR_LIVEDEV, tenant_id, (uint32_t)device_id);
4846 }
4847 
4848 int DetectEngineTenantRegisterVlanId(uint32_t tenant_id, uint16_t vlan_id)
4849 {
4850  return DetectEngineTenantRegisterSelector(TENANT_SELECTOR_VLAN, tenant_id, (uint32_t)vlan_id);
4851 }
4852 
4853 int DetectEngineTenantUnregisterVlanId(uint32_t tenant_id, uint16_t vlan_id)
4854 {
4855  return DetectEngineTenantUnregisterSelector(TENANT_SELECTOR_VLAN, tenant_id, (uint32_t)vlan_id);
4856 }
4857 
4858 int DetectEngineTenantRegisterPcapFile(uint32_t tenant_id)
4859 {
4860  SCLogInfo("registering %u %d 0", TENANT_SELECTOR_DIRECT, tenant_id);
4861  return DetectEngineTenantRegisterSelector(TENANT_SELECTOR_DIRECT, tenant_id, 0);
4862 }
4863 
4865 {
4866  SCLogInfo("unregistering %u %d 0", TENANT_SELECTOR_DIRECT, tenant_id);
4867  return DetectEngineTenantUnregisterSelector(TENANT_SELECTOR_DIRECT, tenant_id, 0);
4868 }
4869 
4870 static uint32_t DetectEngineTenantGetIdFromPcap(const void *ctx, const Packet *p)
4871 {
4872  return p->pcap_v.tenant_id;
4873 }
4874 
4876 {
4877  DetectEngineMasterCtx *master = &g_master_de_ctx;
4878  SCMutexLock(&master->lock);
4879 
4880  if (master->list == NULL) {
4881  SCMutexUnlock(&master->lock);
4882  return NULL;
4883  }
4884 
4885  DetectEngineCtx *de_ctx = master->list;
4886  while (de_ctx) {
4888  de_ctx->tenant_id == tenant_id)
4889  {
4890  de_ctx->ref_cnt++;
4891  break;
4892  }
4893 
4894  de_ctx = de_ctx->next;
4895  }
4896 
4897  SCMutexUnlock(&master->lock);
4898  return de_ctx;
4899 }
4900 
4902 {
4903  DEBUG_VALIDATE_BUG_ON((*de_ctx)->ref_cnt == 0);
4904  (*de_ctx)->ref_cnt--;
4905  *de_ctx = NULL;
4906 }
4907 
4908 static int DetectEngineAddToList(DetectEngineCtx *instance)
4909 {
4910  DetectEngineMasterCtx *master = &g_master_de_ctx;
4911 
4912  if (instance == NULL)
4913  return -1;
4914 
4915  if (master->list == NULL) {
4916  master->list = instance;
4917  } else {
4918  instance->next = master->list;
4919  master->list = instance;
4920  }
4921 
4922  return 0;
4923 }
4924 
4926 {
4927  int r;
4928 
4929  if (de_ctx == NULL)
4930  return -1;
4931 
4932  SCLogDebug("adding de_ctx %p to master", de_ctx);
4933 
4934  DetectEngineMasterCtx *master = &g_master_de_ctx;
4935  SCMutexLock(&master->lock);
4936  r = DetectEngineAddToList(de_ctx);
4937  SCMutexUnlock(&master->lock);
4938  return r;
4939 }
4940 
4941 static int DetectEngineMoveToFreeListNoLock(DetectEngineMasterCtx *master, DetectEngineCtx *de_ctx)
4942 {
4943  DetectEngineCtx *instance = master->list;
4944  if (instance == NULL) {
4945  return -1;
4946  }
4947 
4948  /* remove from active list */
4949  if (instance == de_ctx) {
4950  master->list = instance->next;
4951  } else {
4952  DetectEngineCtx *prev = instance;
4953  instance = instance->next; /* already checked first element */
4954 
4955  while (instance) {
4956  DetectEngineCtx *next = instance->next;
4957 
4958  if (instance == de_ctx) {
4959  prev->next = instance->next;
4960  break;
4961  }
4962 
4963  prev = instance;
4964  instance = next;
4965  }
4966  if (instance == NULL) {
4967  return -1;
4968  }
4969  }
4970 
4971  /* instance is now detached from list */
4972  instance->next = NULL;
4973 
4974  /* add to free list */
4975  if (master->free_list == NULL) {
4976  master->free_list = instance;
4977  } else {
4978  instance->next = master->free_list;
4979  master->free_list = instance;
4980  }
4981  SCLogDebug("detect engine %p moved to free list (%u refs)", de_ctx, de_ctx->ref_cnt);
4982  return 0;
4983 }
4984 
4986 {
4987  int ret = 0;
4988  DetectEngineMasterCtx *master = &g_master_de_ctx;
4989  SCMutexLock(&master->lock);
4990  ret = DetectEngineMoveToFreeListNoLock(master, de_ctx);
4991  SCMutexUnlock(&master->lock);
4992  return ret;
4993 }
4994 
4996 {
4997  DetectEngineMasterCtx *master = &g_master_de_ctx;
4998  SCMutexLock(&master->lock);
4999 
5000  DetectEngineCtx *prev = NULL;
5001  DetectEngineCtx *instance = master->free_list;
5002  while (instance) {
5003  DetectEngineCtx *next = instance->next;
5004 
5005  SCLogDebug("detect engine %p has %u ref(s)", instance, instance->ref_cnt);
5006 
5007  if (instance->ref_cnt == 0) {
5008  if (prev == NULL) {
5009  master->free_list = next;
5010  } else {
5011  prev->next = next;
5012  }
5013 
5014  SCLogDebug("freeing detect engine %p", instance);
5015  DetectEngineCtxFree(instance);
5016  instance = NULL;
5017  }
5018 
5019  prev = instance;
5020  instance = next;
5021  }
5022  SCMutexUnlock(&master->lock);
5023 }
5024 
5026 {
5027  DetectEngineMasterCtx *master = &g_master_de_ctx;
5028  SCMutexLock(&master->lock);
5029 
5030  DetectEngineCtx *instance = master->list;
5031  while (instance) {
5032  DetectEngineCtx *next = instance->next;
5033  DEBUG_VALIDATE_BUG_ON(instance->ref_cnt);
5034  SCLogDebug("detect engine %p has %u ref(s)", instance, instance->ref_cnt);
5035  instance->ref_cnt = 0;
5036  DetectEngineMoveToFreeListNoLock(master, instance);
5037  instance = next;
5038  }
5039  SCMutexUnlock(&master->lock);
5041 }
5042 
5043 static int reloads = 0;
5044 
5045 /** \brief Reload the detection engine
5046  *
5047  * \param filename YAML file to load for the detect config
5048  *
5049  * \retval -1 error
5050  * \retval 0 ok
5051  */
5053 {
5054  DetectEngineCtx *new_de_ctx = NULL;
5055  DetectEngineCtx *old_de_ctx = NULL;
5056 
5057  char prefix[128];
5058  memset(prefix, 0, sizeof(prefix));
5059 
5060  SCLogNotice("rule reload starting");
5061 
5062  if (suri->conf_filename != NULL) {
5063  snprintf(prefix, sizeof(prefix), "detect-engine-reloads.%d", reloads++);
5064  SCLogConfig("Reloading %s", suri->conf_filename);
5065  if (SCConfYamlLoadFileWithPrefix(suri->conf_filename, prefix) != 0) {
5066  SCLogError("failed to load yaml %s", suri->conf_filename);
5067  return -1;
5068  }
5069 
5070  SCConfNode *node = SCConfGetNode(prefix);
5071  if (node == NULL) {
5072  SCLogError("failed to properly setup yaml %s", suri->conf_filename);
5073  return -1;
5074  }
5075 
5076  if (suri->additional_configs) {
5077  for (int i = 0; suri->additional_configs[i] != NULL; i++) {
5078  SCLogConfig("Reloading %s", suri->additional_configs[i]);
5080  }
5081  }
5082 
5083 #if 0
5084  SCConfDump();
5085 #endif
5086  }
5087 
5088  /* get a reference to the current de_ctx */
5089  old_de_ctx = DetectEngineGetCurrent();
5090  if (old_de_ctx == NULL)
5091  return -1;
5092  SCLogDebug("get ref to old_de_ctx %p", old_de_ctx);
5093  DatasetReload();
5094 
5095  /* only reload a regular 'normal' and 'delayed detect stub' detect engines */
5096  if (!(old_de_ctx->type == DETECT_ENGINE_TYPE_NORMAL ||
5097  old_de_ctx->type == DETECT_ENGINE_TYPE_DD_STUB))
5098  {
5099  DetectEngineDeReference(&old_de_ctx);
5100  SCLogNotice("rule reload complete");
5101  return -1;
5102  }
5103 
5104  /* get new detection engine */
5105  new_de_ctx = DetectEngineCtxInitWithPrefix(prefix, old_de_ctx->tenant_id);
5106  if (new_de_ctx == NULL) {
5107  SCLogError("initializing detection engine "
5108  "context failed.");
5109  DetectEngineDeReference(&old_de_ctx);
5110  return -1;
5111  }
5112 
5113  if (SigLoadSignatures(new_de_ctx,
5114  suri->sig_file, suri->sig_file_exclusive) != 0) {
5115  DetectEngineCtxFree(new_de_ctx);
5116  DetectEngineDeReference(&old_de_ctx);
5117  return -1;
5118  }
5119  SCLogDebug("set up new_de_ctx %p", new_de_ctx);
5120 
5121  /* Copy over callbacks. */
5122  new_de_ctx->RateFilterCallback = old_de_ctx->RateFilterCallback;
5123  new_de_ctx->rate_filter_callback_arg = old_de_ctx->rate_filter_callback_arg;
5124 
5125  /* add to master */
5126  DetectEngineAddToMaster(new_de_ctx);
5127 
5128  /* move to old free list */
5129  DetectEngineMoveToFreeList(old_de_ctx);
5130  DetectEngineDeReference(&old_de_ctx);
5131 
5132  SCLogDebug("going to reload the threads to use new_de_ctx %p", new_de_ctx);
5133 
5134  DetectEngineMasterCtx *master = &g_master_de_ctx;
5135  SCMutexLock(&master->lock);
5136  /* update the threads */
5137  DetectEngineReloadThreads(new_de_ctx);
5138  SCMutexUnlock(&master->lock);
5139 
5140  SCLogDebug("threads now run new_de_ctx %p", new_de_ctx);
5141 
5142  /* walk free list, freeing the old_de_ctx */
5144 
5146 
5148 
5149  SCLogDebug("old_de_ctx should have been freed");
5150 
5152 
5153  SCLogNotice("rule reload complete");
5154 
5155 #ifdef HAVE_MALLOC_TRIM
5156  /* The reload process potentially frees up large amounts of memory.
5157  * Encourage the memory management system to reclaim as much as it
5158  * can.
5159  */
5160  malloc_trim(0);
5161 #endif
5162 
5163  return 0;
5164 }
5165 
5166 static uint32_t TenantIdHash(HashTable *h, void *data, uint16_t data_len)
5167 {
5168  DetectEngineThreadCtx *det_ctx = (DetectEngineThreadCtx *)data;
5169  return det_ctx->tenant_id % h->array_size;
5170 }
5171 
5172 static char TenantIdCompare(void *d1, uint16_t d1_len, void *d2, uint16_t d2_len)
5173 {
5176  return (det1->tenant_id == det2->tenant_id);
5177 }
5178 
5179 static void TenantIdFree(void *d)
5180 {
5181  DetectEngineThreadCtxFree(d);
5182 }
5183 
5185 {
5186  DetectEngineMasterCtx *master = &g_master_de_ctx;
5187  SCMutexLock(&master->lock);
5188 
5189  if (master->tenant_selector == TENANT_SELECTOR_UNKNOWN) {
5190  SCLogInfo("error, no tenant selector");
5191  SCMutexUnlock(&master->lock);
5192  return -1;
5193  }
5194 
5195  DetectEngineCtx *stub_de_ctx = NULL;
5196  DetectEngineCtx *list = master->list;
5197  for ( ; list != NULL; list = list->next) {
5198  SCLogDebug("list %p tenant %u", list, list->tenant_id);
5199 
5200  if (list->type == DETECT_ENGINE_TYPE_NORMAL ||
5201  list->type == DETECT_ENGINE_TYPE_MT_STUB ||
5203  {
5204  stub_de_ctx = list;
5205  break;
5206  }
5207  }
5208  if (stub_de_ctx == NULL) {
5209  stub_de_ctx = DetectEngineCtxInitStubForMT();
5210  if (stub_de_ctx == NULL) {
5211  SCMutexUnlock(&master->lock);
5212  return -1;
5213  }
5214 
5215  if (master->list == NULL) {
5216  master->list = stub_de_ctx;
5217  } else {
5218  stub_de_ctx->next = master->list;
5219  master->list = stub_de_ctx;
5220  }
5221  }
5222 
5223  /* update the threads */
5224  SCLogDebug("MT reload starting");
5225  DetectEngineReloadThreads(stub_de_ctx);
5226  SCLogDebug("MT reload done");
5227 
5228  SCMutexUnlock(&master->lock);
5229 
5230  /* walk free list, freeing the old_de_ctx */
5232  // needed for VarNameStoreFree
5234 
5235  SCLogDebug("old_de_ctx should have been freed");
5236  return 0;
5237 }
5238 
5239 static int g_parse_metadata = 0;
5240 
5242 {
5243  g_parse_metadata = 1;
5244 }
5245 
5247 {
5248  g_parse_metadata = 0;
5249 }
5250 
5252 {
5253  return g_parse_metadata;
5254 }
5255 
5257 {
5258  switch (type) {
5259  case DETECT_SM_LIST_MATCH:
5260  return "packet";
5261  case DETECT_SM_LIST_PMATCH:
5262  return "packet/stream payload";
5263 
5264  case DETECT_SM_LIST_TMATCH:
5265  return "tag";
5266 
5268  return "base64_data";
5269 
5271  return "post-match";
5272 
5274  return "suppress";
5276  return "threshold";
5277 
5278  case DETECT_SM_LIST_MAX:
5279  return "max (internal)";
5280  }
5281  return "error";
5282 }
5283 
5284 /* events api */
5286 {
5288  det_ctx->events++;
5289 }
5290 
5292  const Signature *s, const char **sigerror, const DetectBufferType *map)
5293 {
5294  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
5295  if (s->init_data->buffers[x].id != (uint32_t)map->id)
5296  continue;
5297  const SigMatch *sm = s->init_data->buffers[x].head;
5298  for (; sm != NULL; sm = sm->next) {
5299  if (sm->type != DETECT_CONTENT)
5300  continue;
5301 
5302  const DetectContentData *cd = (DetectContentData *)sm->ctx;
5303  if (cd->flags & DETECT_CONTENT_NOCASE) {
5304  *sigerror = "md5-like keyword should not be used together with "
5305  "nocase, since the rule is automatically "
5306  "lowercased anyway which makes nocase redundant.";
5307  SCLogWarning("rule %u: buffer %s: %s", s->id, map->name, *sigerror);
5308  }
5309 
5310  if (cd->content_len != SC_MD5_HEX_LEN) {
5311  *sigerror = "Invalid length for md5-like keyword (should "
5312  "be 32 characters long). This rule will therefore "
5313  "never match.";
5314  SCLogError("rule %u: buffer %s: %s", s->id, map->name, *sigerror);
5315  return false;
5316  }
5317 
5318  for (size_t i = 0; i < cd->content_len; ++i) {
5319  if (!isxdigit(cd->content[i])) {
5320  *sigerror =
5321  "Invalid md5-like string (should be string of hexadecimal characters)."
5322  "This rule will therefore never match.";
5323  SCLogWarning("rule %u: buffer %s: %s", s->id, map->name, *sigerror);
5324  return false;
5325  }
5326  }
5327  }
5328  }
5329  return true;
5330 }
5331 
5333  const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
5334 {
5335  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
5336  if (s->init_data->buffers[x].id != (uint32_t)map->id)
5337  continue;
5338  SigMatch *sm = s->init_data->buffers[x].head;
5339  for (; sm != NULL; sm = sm->next) {
5340  if (sm->type != DETECT_CONTENT)
5341  continue;
5342 
5344 
5345  bool changed = false;
5346  uint32_t u;
5347  for (u = 0; u < cd->content_len; u++) {
5348  if (isupper(cd->content[u])) {
5349  cd->content[u] = u8_tolower(cd->content[u]);
5350  changed = true;
5351  }
5352  }
5353 
5354  if (changed) {
5355  SpmDestroyCtx(cd->spm_ctx);
5356  cd->spm_ctx =
5358  }
5359  }
5360  }
5361 }
5362 
5364 {
5366  if (de_ctx == NULL) {
5367  SCLogError("no detection engine available for rate filter callback registration");
5368  return false;
5369  }
5370  de_ctx->RateFilterCallback = fn;
5373  return true;
5374 }
5375 
5377 {
5378  if (det_ctx->json_content_len > SIG_JSON_CONTENT_ARRAY_LEN - 1) {
5379  SCLogDebug("json content length %u exceeds maximum %u", det_ctx->json_content_len,
5381  return -1;
5382  }
5383  if (det_ctx->json_content_len >= det_ctx->json_content_capacity) {
5384  if (det_ctx->json_content_capacity == 0) {
5385  det_ctx->json_content_capacity = 1;
5386  } else {
5387  det_ctx->json_content_capacity *= 2;
5388  }
5389  void *tmp = SCRealloc(
5390  det_ctx->json_content, det_ctx->json_content_capacity * sizeof(SigJsonContent));
5391  if (unlikely(tmp == NULL)) {
5392  return -1;
5393  }
5394  SCLogDebug("reallocated json content array to %u items", det_ctx->json_content_capacity);
5395  det_ctx->json_content = tmp;
5396  }
5397  return 0;
5398 }
5399 
5400 /*************************************Unittest*********************************/
5401 
5402 #ifdef UNITTESTS
5403 
5404 static int DetectEngineInitYamlConf(const char *conf)
5405 {
5407  SCConfInit();
5408  return SCConfYamlLoadString(conf, strlen(conf));
5409 }
5410 
5411 static void DetectEngineDeInitYamlConf(void)
5412 {
5413  SCConfDeInit();
5415 }
5416 
5417 static int DetectEngineTest01(void)
5418 {
5419  const char *conf =
5420  "%YAML 1.1\n"
5421  "---\n"
5422  "detect-engine:\n"
5423  " - profile: medium\n"
5424  " - custom-values:\n"
5425  " toclient_src_groups: 2\n"
5426  " toclient_dst_groups: 2\n"
5427  " toclient_sp_groups: 2\n"
5428  " toclient_dp_groups: 3\n"
5429  " toserver_src_groups: 2\n"
5430  " toserver_dst_groups: 4\n"
5431  " toserver_sp_groups: 2\n"
5432  " toserver_dp_groups: 25\n"
5433  " - inspection-recursion-limit: 0\n";
5434 
5435  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5436 
5439 
5441 
5443 
5444  DetectEngineDeInitYamlConf();
5445 
5446  PASS;
5447 }
5448 
5449 static int DetectEngineTest02(void)
5450 {
5451  const char *conf =
5452  "%YAML 1.1\n"
5453  "---\n"
5454  "detect-engine:\n"
5455  " - profile: medium\n"
5456  " - custom-values:\n"
5457  " toclient_src_groups: 2\n"
5458  " toclient_dst_groups: 2\n"
5459  " toclient_sp_groups: 2\n"
5460  " toclient_dp_groups: 3\n"
5461  " toserver_src_groups: 2\n"
5462  " toserver_dst_groups: 4\n"
5463  " toserver_sp_groups: 2\n"
5464  " toserver_dp_groups: 25\n"
5465  " - inspection-recursion-limit:\n";
5466 
5467  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5468 
5471 
5472  FAIL_IF_NOT(
5474 
5476 
5477  DetectEngineDeInitYamlConf();
5478 
5479  PASS;
5480 }
5481 
5482 static int DetectEngineTest03(void)
5483 {
5484  const char *conf =
5485  "%YAML 1.1\n"
5486  "---\n"
5487  "detect-engine:\n"
5488  " - profile: medium\n"
5489  " - custom-values:\n"
5490  " toclient_src_groups: 2\n"
5491  " toclient_dst_groups: 2\n"
5492  " toclient_sp_groups: 2\n"
5493  " toclient_dp_groups: 3\n"
5494  " toserver_src_groups: 2\n"
5495  " toserver_dst_groups: 4\n"
5496  " toserver_sp_groups: 2\n"
5497  " toserver_dp_groups: 25\n";
5498 
5499  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5500 
5503 
5504  FAIL_IF_NOT(
5506 
5508 
5509  DetectEngineDeInitYamlConf();
5510 
5511  PASS;
5512 }
5513 
5514 static int DetectEngineTest04(void)
5515 {
5516  const char *conf =
5517  "%YAML 1.1\n"
5518  "---\n"
5519  "detect-engine:\n"
5520  " - profile: medium\n"
5521  " - custom-values:\n"
5522  " toclient_src_groups: 2\n"
5523  " toclient_dst_groups: 2\n"
5524  " toclient_sp_groups: 2\n"
5525  " toclient_dp_groups: 3\n"
5526  " toserver_src_groups: 2\n"
5527  " toserver_dst_groups: 4\n"
5528  " toserver_sp_groups: 2\n"
5529  " toserver_dp_groups: 25\n"
5530  " - inspection-recursion-limit: 10\n";
5531 
5532  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5533 
5536 
5538 
5540 
5541  DetectEngineDeInitYamlConf();
5542 
5543  PASS;
5544 }
5545 
5546 static int DetectEngineTest08(void)
5547 {
5548  const char *conf =
5549  "%YAML 1.1\n"
5550  "---\n"
5551  "detect-engine:\n"
5552  " - profile: custom\n"
5553  " - custom-values:\n"
5554  " toclient-groups: 23\n"
5555  " toserver-groups: 27\n";
5556 
5557  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5558 
5561 
5564 
5566 
5567  DetectEngineDeInitYamlConf();
5568 
5569  PASS;
5570 }
5571 
5572 /** \test bug 892 bad values */
5573 static int DetectEngineTest09(void)
5574 {
5575  const char *conf =
5576  "%YAML 1.1\n"
5577  "---\n"
5578  "detect-engine:\n"
5579  " - profile: custom\n"
5580  " - custom-values:\n"
5581  " toclient-groups: BA\n"
5582  " toserver-groups: BA\n"
5583  " - inspection-recursion-limit: 10\n";
5584 
5585  FAIL_IF(DetectEngineInitYamlConf(conf) == -1);
5586 
5589 
5592 
5594 
5595  DetectEngineDeInitYamlConf();
5596 
5597  PASS;
5598 }
5599 
5600 /** \brief keyword thread-context init stub that always fails, used to mimic a
5601  * failing per-thread keyword init such as DetectFilemagicThreadInit. */
5602 static void *DetectEngineFailingThreadKeywordInit(void *data)
5603 {
5604  (void)data;
5605  return NULL;
5606 }
5607 
5608 static void DetectEngineNoopThreadKeywordFree(void *ctx)
5609 {
5610  (void)ctx;
5611 }
5612 
5613 /** \test Ticket #8237: a failing per-thread keyword init must make the detect
5614  * thread context init fail rather than silently continuing with a partially
5615  * initialized keyword context array. */
5616 static int DetectEngineThreadCtxInitKeywordFailTest(void)
5617 {
5618  ThreadVars th_v;
5619  memset(&th_v, 0, sizeof(th_v));
5620  DetectEngineThreadCtx *det_ctx = NULL;
5621 
5624  de_ctx->flags |= DE_QUIET;
5625 
5626  Signature *s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (sid:1;)");
5627  FAIL_IF_NULL(s);
5628 
5630 
5631  /* Register a keyword whose per-thread init fails (returns NULL). */
5632  int id = DetectRegisterThreadCtxFuncs(de_ctx, "test_failing_keyword",
5633  DetectEngineFailingThreadKeywordInit, NULL, DetectEngineNoopThreadKeywordFree, 0);
5634  FAIL_IF(id < 0);
5635 
5636  /* Thread context init must report failure and not hand back a context. */
5637  TmEcode r = DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
5638  FAIL_IF(r != TM_ECODE_FAILED);
5639  FAIL_IF_NOT_NULL(det_ctx);
5640 
5642 
5643  PASS;
5644 }
5645 
5646 #endif
5647 
5649 {
5650 #ifdef UNITTESTS
5651  UtRegisterTest("DetectEngineTest01", DetectEngineTest01);
5652  UtRegisterTest("DetectEngineTest02", DetectEngineTest02);
5653  UtRegisterTest("DetectEngineTest03", DetectEngineTest03);
5654  UtRegisterTest("DetectEngineTest04", DetectEngineTest04);
5655  UtRegisterTest("DetectEngineTest08", DetectEngineTest08);
5656  UtRegisterTest("DetectEngineTest09", DetectEngineTest09);
5658  "DetectEngineThreadCtxInitKeywordFailTest", DetectEngineThreadCtxInitKeywordFailTest);
5659 #endif
5660 }
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:2836
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:2313
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:3294
DetectEngineMTApply
int DetectEngineMTApply(void)
Definition: detect-engine.c:5184
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:3710
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:3575
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:2296
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:4901
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:2224
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1511
DetectEngineMustParseMetadata
int DetectEngineMustParseMetadata(void)
Definition: detect-engine.c:5251
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:2337
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
MPM_AC
@ MPM_AC
Definition: util-mpm.h:38
DetectEngineCtx_::type
enum DetectEngineType type
Definition: detect.h:1115
DetectEnginePruneFreeList
void DetectEnginePruneFreeList(void)
Definition: detect-engine.c:4995
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:2163
DetectThreadCtxGetKeywordThreadCtx
void * DetectThreadCtxGetKeywordThreadCtx(DetectEngineThreadCtx *det_ctx, int id)
Retrieve thread local keyword ctx by id.
Definition: detect-engine.c:3991
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:155
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:2826
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
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:2670
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:4410
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:4100
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:2070
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:4925
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:2274
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:181
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:4559
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:5285
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:2870
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:4424
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:3592
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:4081
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:3847
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:5291
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
DetectEngineMpmCachingEnabled
bool DetectEngineMpmCachingEnabled(void)
Definition: detect-engine.c:2641
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:4055
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:92
TenantLoaderCtx_
Definition: detect-engine.c:4279
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
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:4985
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
TVT_PPT
@ TVT_PPT
Definition: tm-threads-common.h:88
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:4067
SigMatchList2DataArray
SigMatchData * SigMatchList2DataArray(SigMatch *head)
convert SigMatch list to SigMatchData array
Definition: detect-parse.c:2642
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3618
MpmTableElmt_::CacheStatsDeinit
void(* CacheStatsDeinit)(void *data)
Definition: util-mpm.h:182
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:4396
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:2831
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:4842
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:180
DetectEngineCtx_::frame_mpms_list
DetectBufferMpmRegistry * frame_mpms_list
Definition: detect.h:1154
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:183
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:642
DetectBufferType_::name
char name[64]
Definition: detect.h:454
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:5376
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:4137
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:5648
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:5256
TmModule_
Definition: tm-modules.h:47
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
DetectEngineAppInspectionEngine_::alproto
AppProto alproto
Definition: detect.h:420
MpmConfig_::cache_max_age_seconds
uint64_t cache_max_age_seconds
Definition: util-mpm.h:93
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:3921
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2286
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:2821
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:4091
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:3863
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:94
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
MPM_HS
@ MPM_HS
Definition: util-mpm.h:40
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:4858
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:33
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:261
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:2650
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:5246
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:4282
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:4123
SCDetectEngineRegisterRateFilterCallback
bool SCDetectEngineRegisterRateFilterCallback(SCDetectRateFilterFunc fn, void *arg)
Register a callback when a rate_filter has been applied to an alert.
Definition: detect-engine.c:5363
DetectEngineThreadCtx_::base64_decoded
uint8_t * base64_decoded
Definition: detect.h:1388
TenantLoaderCtx_::reload_cnt
int reload_cnt
Definition: detect-engine.c:4281
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:162
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:163
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:184
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:5052
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:5025
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:161
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:3973
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:4848
flow.h
DetectEngineSetParseMetadata
void DetectEngineSetParseMetadata(void)
Definition: detect-engine.c:5241
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:4011
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:4864
TenantLoaderCtx_::tenant_id
uint32_t tenant_id
Definition: detect-engine.c:4280
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:2385
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:4875
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:5332
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:4853
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
MPM_AC_KS
@ MPM_AC_KS
Definition: util-mpm.h:39
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