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