Go to the documentation of this file.
57 static bool fp_engine_analysis_set =
false;
60 static char *DetectLoadCompleteSigPathWithKey(
63 const char *defaultpath = NULL;
67 if (sig_file == NULL) {
68 SCLogError(
"invalid sig_file argument - NULL");
80 if (default_rule_path) {
81 defaultpath = default_rule_path->
val;
110 return DetectLoadCompleteSigPathWithKey(
de_ctx,
"default-rule-path", sig_file);
122 int *badsigs,
int *skippedsigs,
const bool firewall_rule)
124 int good = 0, bad = 0, skipped = 0;
127 int lineno = 0, multiline = 0;
133 FILE *fp = fopen(sig_file,
"r");
137 sig_file, strerror(errno));
145 char *res = fgets(line +
offset, (
int)(
sizeof(line) -
offset), fp);
150 size_t len = strlen(line);
153 if (line[0] ==
'\n' || line [0] ==
'\r' || line[0] ==
' ' || line[0] ==
'#' || line[0] ==
'\t')
157 while (
len > 0 && isspace((
unsigned char)line[--
len]));
158 if (line[
len] ==
'\\') {
161 if (
offset <
sizeof(line) - 1) {
171 if (
len > 0 && (line[
len - 1] ==
'\n' || line[
len - 1] ==
'\r')) {
172 line[
len - 1] =
'\0';
188 if (fp_engine_analysis_set) {
199 SCLogError(
"error parsing signature \"%s\" from "
200 "file %s at line %" PRId32
"",
201 line, sig_file, lineno - multiline);
205 "file %s at line %" PRId32
"",
206 line, sig_file, lineno - multiline);
219 SCLogInfo(
"Skipping signature due to missing requirements: %s from file %s at line "
221 line, sig_file, lineno - multiline);
231 *skippedsigs = skipped;
242 int *good_sigs,
int *bad_sigs,
int *skipped_sigs)
246 if (pattern == NULL) {
253 r = glob(pattern, 0, NULL, &files);
255 if (r == GLOB_NOMATCH) {
256 SCLogWarning(
"No rule files match the pattern %s", pattern);
261 SCLogError(
"error expanding template %s: %s", pattern, strerror(errno));
265 for (
size_t i = 0; i < (size_t)files.gl_pathc; i++) {
266 char *fname = files.gl_pathv[i];
267 if (strcmp(
"/dev/null", fname) == 0)
270 char *fname = pattern;
271 if (strcmp(
"/dev/null", fname) == 0)
279 r = DetectLoadSigFile(
de_ctx, fname, good_sigs, bad_sigs, skipped_sigs,
false);
300 SCLogError(
"initializing firewall policies failed");
304 SCLogError(
"loading firewall policies failed");
309 int32_t good_sigs = 0;
310 int32_t bad_sigs = 0;
311 int32_t skipped_sigs = 0;
316 &bad_sigs, &skipped_sigs,
true);
319 if (ret != 0 || bad_sigs != 0 || skipped_sigs != 0) {
325 if (good_sigs == 0) {
328 SCLogNotice(
"fw: %d rules loaded from %s.", good_sigs,
337 if (default_fw_rule_path == NULL) {
338 SCLogNotice(
"fw: firewall.rule-path not defined, skip loading firewall rules");
342 if (rule_files == NULL) {
343 SCLogNotice(
"fw: firewall.rule-files not defined, skip loading firewall rules");
349 int32_t good_sigs = 0;
350 int32_t bad_sigs = 0;
351 int32_t skipped_sigs = 0;
353 char *sfile = DetectLoadCompleteSigPathWithKey(
de_ctx,
"firewall.rule-path", file->
val);
354 SCLogNotice(
"fw: rule file full path \"%s\"", sfile);
356 int ret = DetectLoadSigFile(
de_ctx, sfile, &good_sigs, &bad_sigs, &skipped_sigs,
true);
360 if (ret != 0 || bad_sigs != 0 || skipped_sigs != 0) {
366 if (good_sigs == 0) {
369 SCLogNotice(
"fw: %d rules loaded from %s.", good_sigs, file->
val);
392 char varname[128] =
"rule-files";
395 int skipped_sigs = 0;
406 if (LoadFirewallRuleFiles(
de_ctx) < 0) {
417 goto skip_regular_rules;
422 if (!(sig_file != NULL && sig_file_exclusive)) {
424 if (rule_files != NULL) {
426 SCLogWarning(
"Invalid rule-files configuration section: "
427 "expected a list of filenames.");
431 good_sigs = bad_sigs = skipped_sigs = 0;
432 ret = ProcessSigFiles(
433 de_ctx, sfile, sig_stat, &good_sigs, &bad_sigs, &skipped_sigs);
442 if (good_sigs == 0) {
451 if (sig_file != NULL) {
452 ret = ProcessSigFiles(
de_ctx, sig_file, sig_stat, &good_sigs, &bad_sigs, &skipped_sigs);
460 if (good_sigs == 0) {
470 "%d rule files specified, but no rules were loaded!", sig_stat->
total_files);
478 SCLogInfo(
"tenant id %d: %" PRId32
" rule files processed. %" PRId32
479 " rules successfully loaded, %" PRId32
" rules failed, %" PRId32
484 SCLogInfo(
"%" PRId32
" rule files processed. %" PRId32
485 " rules successfully loaded, %" PRId32
" rules failed, %" PRId32
528 static int cur_loader = 0;
529 static void TmThreadWakeupDetectLoaderThreads(
void);
536 if (loader_id == -1) {
537 loader_id = cur_loader;
539 if (cur_loader >= num_loaders)
542 if (loader_id >= num_loaders || loader_id < 0) {
560 TmThreadWakeupDetectLoaderThreads();
562 SCLogDebug(
"%d %p %p", loader_id, Func, func_ctx);
572 for (
int i = 0; i < num_loaders; i++) {
590 if (loader->
result != 0) {
597 SCLogError(
"%d loaders reported errors", errors);
606 memset(loader, 0x00,
sizeof(*loader));
616 if (setting < 1 || setting > 1024) {
617 FatalError(
"invalid multi-detect.loaders setting %" PRIdMAX, setting);
620 num_loaders = (int32_t)setting;
621 SCLogInfo(
"using %d detect loader threads", num_loaders);
627 for (
int i = 0; i < num_loaders; i++) {
628 DetectLoaderInit(&loaders[i]);
635 static void TmThreadWakeupDetectLoaderThreads(
void)
638 for (
int i = 0; i <
TVT_MAX; i++) {
641 if (strncmp(
tv->
name,
"DL#",3) == 0) {
659 for (
int i = 0; i <
TVT_MAX; i++) {
662 if (strncmp(
tv->
name,
"DL#",3) == 0)
677 static TmEcode DetectLoaderThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
736 bool has_work = loader->task_list.tqh_first != NULL;
758 for (
int i = 0; i < num_loaders; i++) {
763 if (tv_loader == NULL) {
@ RUNMODE_ENGINE_ANALYSIS
TmEcode TmThreadSpawn(ThreadVars *tv)
Spawns a thread associated with the ThreadVars instance tv.
void DetectLoaderThreadSpawn(void)
spawn the detect loader manager thread
char * PathMergeAlloc(const char *const dir, const char *const fname)
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
int SigLoadSignatures(DetectEngineCtx *de_ctx, char *sig_file, bool sig_file_exclusive)
Load signatures.
const char * firewall_rule_file_exclusive
void SetupEngineAnalysis(DetectEngineCtx *de_ctx, bool *fp_analysis, bool *rule_analysis)
void TmThreadsSetFlag(ThreadVars *tv, uint32_t flag)
Set a thread flag.
void TmThreadWaitForFlag(ThreadVars *tv, uint32_t flags)
Waits till the specified flag(s) is(are) set. We don't bother if the kill flag has been set or not on...
SC_ATOMIC_DECLARE(int, detect_loader_cnt)
struct HtpBodyChunk_ * next
void TmThreadContinueDetectLoaderThreads(void)
Unpauses all threads present in tv_root.
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
main detection engine ctx
#define TAILQ_EMPTY(head)
#define TAILQ_FOREACH(var, head, field)
void(* LoaderFreeFunc)(void *ctx)
void SCSigSignatureOrderingModuleCleanup(DetectEngineCtx *de_ctx)
De-registers all the signature ordering functions registered.
ThreadVars * tv_root[TVT_MAX]
int(* LoaderFunc)(void *ctx, int loader_id)
#define TAILQ_INSERT_TAIL(head, elm, field)
int DetectFirewallInitDefaultPolicies(DetectEngineCtx *de_ctx)
allocate and initialize to default values the policies table
void DetectParseDupSigHashFree(DetectEngineCtx *de_ctx)
Frees the hash table that is used to cull duplicate sigs.
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
bool EngineModeIsFirewall(void)
int SCThresholdConfInitContext(DetectEngineCtx *de_ctx)
Inits the context to be used by the Threshold Config parsing API.
void EngineAnalysisRulesFailure(const DetectEngineCtx *de_ctx, const char *line, const char *file, int lineno)
#define TM_THREAD_NAME_MAX
int SCConfNodeIsSequence(const SCConfNode *node)
Check if a node is a sequence or node.
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
void TmThreadContinue(ThreadVars *tv)
Unpauses a thread.
SCDetectRequiresStatus * requirements
#define TAILQ_REMOVE(head, elm, field)
SCRunMode SCRunmodeGet(void)
Get the current run mode.
bool rule_engine_analysis_set
int SigStringAppend(SigFileLoaderStat *sig_stats, const char *sig_file, const char *sig_str, const char *sig_error, int line)
Append a new list member to SigString list.
struct DetectFirewallPolicies * fw_policies
void DetectLoadersInit(void)
void SCSigOrderSignatures(DetectEngineCtx *de_ctx)
Orders the signatures.
#define SCMutexUnlock(mut)
int SCConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
struct timeval last_reload
Per thread variable structure.
TmEcode(* Management)(ThreadVars *, void *)
void SCSigRegisterSignatureOrderingFuncs(DetectEngineCtx *de_ctx)
Lets you register the Signature ordering functions. The order in which the functions are registered s...
#define SCLogWarning(...)
Macro used to log WARNING messages.
void EngineAnalysisRules(const DetectEngineCtx *de_ctx, const Signature *s, const char *line)
Prints analysis of loaded rules.
struct ThreadVars_ * next
const char * thread_name_detect_loader
void TmModuleDetectLoaderRegister(void)
#define SCCtrlMutexLock(mut)
TmModule tmm_modules[TMM_SIZE]
struct DetectLoaderThreadData_ DetectLoaderThreadData
#define DETECT_MAX_RULE_SIZE
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
#define SCMutexInit(mut, mutattrs)
int DetectLoaderQueueTask(int loader_id, LoaderFunc Func, void *func_ctx, LoaderFreeFunc FreeFunc)
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
#define SCCtrlMutexUnlock(mut)
SigFileLoaderStat sig_stat
int DetectFirewallLoadDefaultPolicies(DetectEngineCtx *de_ctx)
bool TmThreadsWaitForUnpause(ThreadVars *tv)
Wait for a thread to become unpaused.
void CleanupEngineAnalysis(DetectEngineCtx *de_ctx)
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
ThreadVars * TmThreadCreateCmdThreadByName(const char *name, const char *module, int mucond)
Creates and returns the TV instance for a Command thread (CMD). This function supports only custom sl...
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
void EngineAnalysisFP(const DetectEngineCtx *de_ctx, const Signature *s, const char *line)
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
#define SCLogError(...)
Macro used to log ERROR messages.
int PathIsRelative(const char *path)
Check if a path is relative.
int DetectLoadersSync(void)
wait for loader tasks to complete
char * DetectLoadCompleteSigPath(const DetectEngineCtx *de_ctx, const char *sig_file)
Create the path if default-rule-path was specified.
int TmThreadsCheckFlag(ThreadVars *tv, uint32_t flag)
Check if a thread flag is set.
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Signature loader statistics.
Signature * DetectFirewallRuleAppendNew(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
#define TM_FLAG_MANAGEMENT_TM