From: Victor Julien Date: Tue, 31 Oct 2017 12:49:42 +0000 (+0100) Subject: detect/prefilter: move hash into detect engine ctx X-Git-Tag: suricata-4.1.0-beta1~145 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d64785274e28814963f12c04b462ca2721a99d4a;p=thirdparty%2Fsuricata.git detect/prefilter: move hash into detect engine ctx --- diff --git a/src/app-layer-detect-proto.c b/src/app-layer-detect-proto.c index e3d6292f2e..3f91ffef32 100644 --- a/src/app-layer-detect-proto.c +++ b/src/app-layer-detect-proto.c @@ -1468,7 +1468,7 @@ void AppLayerProtoDetectPPRegister(uint8_t ipproto, } temp_dp = temp_dp->next; } - DetectPortCleanupList(head); + DetectPortCleanupList(NULL,head); SCReturn; } diff --git a/src/detect-engine-build.c b/src/detect-engine-build.c index 39f78f27f5..681ce4d9a1 100644 --- a/src/detect-engine-build.c +++ b/src/detect-engine-build.c @@ -974,7 +974,7 @@ static int RulesGroupByProto(DetectEngineCtx *de_ctx) } else { SCLogDebug("proto group %d sgh %p is a copy", p, sgh_ts[p]); - SigGroupHeadFree(sgh_ts[p]); + SigGroupHeadFree(de_ctx, sgh_ts[p]); sgh_ts[p] = lookup_sgh; de_ctx->gh_reuse++; @@ -1011,7 +1011,7 @@ static int RulesGroupByProto(DetectEngineCtx *de_ctx) } else { SCLogDebug("proto group %d sgh %p is a copy", p, sgh_tc[p]); - SigGroupHeadFree(sgh_tc[p]); + SigGroupHeadFree(de_ctx, sgh_tc[p]); sgh_tc[p] = lookup_sgh; de_ctx->gh_reuse++; @@ -1216,7 +1216,7 @@ static DetectPort *RulesGroupByPorts(DetectEngineCtx *de_ctx, int ipproto, uint3 } else { SCLogDebug("port group %p sgh %p is a copy", iter, iter->sh); - SigGroupHeadFree(iter->sh); + SigGroupHeadFree(de_ctx, iter->sh); iter->sh = lookup_sgh; iter->flags |= PORT_SIGGROUPHEAD_COPY; @@ -1536,7 +1536,7 @@ int CreateGroupedPortList(DetectEngineCtx *de_ctx, DetectPort *port_list, Detect /* when a group's sigs are added to the joingr, we can free it */ gr->next = NULL; - DetectPortFree(gr); + DetectPortFree(de_ctx, gr); gr = NULL; /* append */ @@ -1666,7 +1666,7 @@ int SigAddressCleanupStage1(DetectEngineCtx *de_ctx) SCLogDebug("cleaning up signature grouping structure..."); } if (de_ctx->decoder_event_sgh) - SigGroupHeadFree(de_ctx->decoder_event_sgh); + SigGroupHeadFree(de_ctx, de_ctx->decoder_event_sgh); de_ctx->decoder_event_sgh = NULL; int f; @@ -1677,9 +1677,9 @@ int SigAddressCleanupStage1(DetectEngineCtx *de_ctx) } /* free lookup lists */ - DetectPortCleanupList(de_ctx->flow_gh[f].tcp); + DetectPortCleanupList(de_ctx, de_ctx->flow_gh[f].tcp); de_ctx->flow_gh[f].tcp = NULL; - DetectPortCleanupList(de_ctx->flow_gh[f].udp); + DetectPortCleanupList(de_ctx, de_ctx->flow_gh[f].udp); de_ctx->flow_gh[f].udp = NULL; } @@ -1690,7 +1690,7 @@ int SigAddressCleanupStage1(DetectEngineCtx *de_ctx) continue; SCLogDebug("sgh %p", sgh); - SigGroupHeadFree(sgh); + SigGroupHeadFree(de_ctx, sgh); } SCFree(de_ctx->sgh_array); de_ctx->sgh_array = NULL; diff --git a/src/detect-engine-port.c b/src/detect-engine-port.c index 0ea5b73b1e..216fc296e7 100644 --- a/src/detect-engine-port.c +++ b/src/detect-engine-port.c @@ -80,14 +80,14 @@ static DetectPort *DetectPortInit(void) * * \param dp Pointer to the DetectPort that has to be freed. */ -void DetectPortFree(DetectPort *dp) +void DetectPortFree(const DetectEngineCtx *de_ctx, DetectPort *dp) { if (dp == NULL) return; /* only free the head if we have the original */ if (dp->sh != NULL && !(dp->flags & PORT_SIGGROUPHEAD_COPY)) { - SigGroupHeadFree(dp->sh); + SigGroupHeadFree(de_ctx, dp->sh); } dp->sh = NULL; @@ -121,7 +121,7 @@ void DetectPortPrintList(DetectPort *head) * * \param head Pointer to the DetectPort list head */ -void DetectPortCleanupList (DetectPort *head) +void DetectPortCleanupList (const DetectEngineCtx *de_ctx, DetectPort *head) { if (head == NULL) return; @@ -131,7 +131,7 @@ void DetectPortCleanupList (DetectPort *head) for (cur = head; cur != NULL; ) { next = cur->next; cur->next = NULL; - DetectPortFree(cur); + DetectPortFree(de_ctx, cur); cur = next; } } @@ -191,7 +191,7 @@ int DetectPortInsert(DetectEngineCtx *de_ctx, DetectPort **head, /* exact overlap/match */ if (cur != new) { SigGroupHeadCopySigs(de_ctx, new->sh, &cur->sh); - DetectPortFree(new); + DetectPortFree(de_ctx, new); return 0; } return 1; @@ -510,13 +510,13 @@ static int DetectPortCut(DetectEngineCtx *de_ctx, DetectPort *a, } if (tmp != NULL) { - DetectPortFree(tmp); + DetectPortFree(de_ctx, tmp); } return 0; error: if (tmp != NULL) - DetectPortFree(tmp); + DetectPortFree(de_ctx, tmp); return -1; } @@ -805,7 +805,8 @@ static int DetectPortParseInsert(DetectPort **head, DetectPort *new) * \retval 0 on success * \retval -1 on error */ -static int DetectPortParseInsertString(DetectPort **head, const char *s) +static int DetectPortParseInsertString(const DetectEngineCtx *de_ctx, + DetectPort **head, const char *s) { DetectPort *ad = NULL, *ad_any = NULL; int r = 0; @@ -864,9 +865,9 @@ static int DetectPortParseInsertString(DetectPort **head, const char *s) error: SCLogError(SC_ERR_PORT_PARSE_INSERT_STRING,"DetectPortParseInsertString error"); if (ad != NULL) - DetectPortCleanupList(ad); + DetectPortCleanupList(de_ctx, ad); if (ad_any != NULL) - DetectPortCleanupList(ad_any); + DetectPortCleanupList(de_ctx, ad_any); return -1; } @@ -989,9 +990,9 @@ static int DetectPortParseDo(const DetectEngineCtx *de_ctx, SCLogDebug("Parsed port from DetectPortParseDo - %s", address); if (negate == 0 && n_set == 0) { - r = DetectPortParseInsertString(head, address); + r = DetectPortParseInsertString(de_ctx, head, address); } else { - r = DetectPortParseInsertString(nhead, address); + r = DetectPortParseInsertString(de_ctx, nhead, address); } if (r == -1) goto error; @@ -1054,9 +1055,9 @@ static int DetectPortParseDo(const DetectEngineCtx *de_ctx, d_set = 0; } else { if (!((negate + n_set) % 2)) { - r = DetectPortParseInsertString(head,address); + r = DetectPortParseInsertString(de_ctx, head,address); } else { - r = DetectPortParseInsertString(nhead,address); + r = DetectPortParseInsertString(de_ctx, nhead,address); } if (r == -1) goto error; @@ -1129,7 +1130,8 @@ static int DetectPortIsCompletePortSpace(DetectPort *p) * \retval 0 on success * \retval -1 on error */ -static int DetectPortParseMergeNotPorts(DetectPort **head, DetectPort **nhead) +static int DetectPortParseMergeNotPorts(const DetectEngineCtx *de_ctx, + DetectPort **head, DetectPort **nhead) { DetectPort *ad = NULL; DetectPort *ag, *ag2; @@ -1148,7 +1150,7 @@ static int DetectPortParseMergeNotPorts(DetectPort **head, DetectPort **nhead) */ if (*head == NULL && *nhead != NULL) { SCLogDebug("inserting 0:65535 into head"); - r = DetectPortParseInsertString(head,"0:65535"); + r = DetectPortParseInsertString(de_ctx, head,"0:65535"); if (r < 0) { goto error; } @@ -1192,7 +1194,7 @@ static int DetectPortParseMergeNotPorts(DetectPort **head, DetectPort **nhead) } /** store the next ptr and remove the group */ DetectPort *next_ag2 = ag2->next; - DetectPortFree(ag2); + DetectPortFree(de_ctx,ag2); ag2 = next_ag2; } else { ag2 = ag2->next; @@ -1213,7 +1215,7 @@ static int DetectPortParseMergeNotPorts(DetectPort **head, DetectPort **nhead) return 0; error: if (ad != NULL) - DetectPortFree(ad); + DetectPortFree(de_ctx, ad); return -1; } @@ -1243,7 +1245,7 @@ int DetectPortTestConfVars(void) "Port var \"%s\" probably has a sequence(something " "in brackets) value set without any quotes. Please " "quote it using \"..\".", seq_node->name); - DetectPortCleanupList(gh); + DetectPortCleanupList(NULL, gh); goto error; } @@ -1252,7 +1254,7 @@ int DetectPortTestConfVars(void) CleanVariableResolveList(&var_list); if (r < 0) { - DetectPortCleanupList(gh); + DetectPortCleanupList(NULL, gh); SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY, "failed to parse port var \"%s\" with value \"%s\". " "Please check it's syntax", seq_node->name, seq_node->val); @@ -1265,15 +1267,15 @@ int DetectPortTestConfVars(void) "with it's value \"%s\". Port space range is NIL. " "Probably have a !any or a port range that supplies " "a NULL address range", seq_node->name, seq_node->val); - DetectPortCleanupList(gh); - DetectPortCleanupList(ghn); + DetectPortCleanupList(NULL, gh); + DetectPortCleanupList(NULL, ghn); goto error; } if (gh != NULL) - DetectPortCleanupList(gh); + DetectPortCleanupList(NULL, gh); if (ghn != NULL) - DetectPortCleanupList(ghn); + DetectPortCleanupList(NULL, ghn); } return 0; @@ -1308,15 +1310,15 @@ int DetectPortParse(const DetectEngineCtx *de_ctx, SCLogDebug("head %p %p, nhead %p", head, *head, nhead); /* merge the 'not' address groups */ - if (DetectPortParseMergeNotPorts(head, &nhead) < 0) + if (DetectPortParseMergeNotPorts(de_ctx, head, &nhead) < 0) goto error; /* free the temp negate head */ - DetectPortCleanupList(nhead); + DetectPortCleanupList(de_ctx, nhead); return 0; error: - DetectPortCleanupList(nhead); + DetectPortCleanupList(de_ctx, nhead); return -1; } @@ -1389,7 +1391,7 @@ DetectPort *PortParse(const char *str) error: if (dp != NULL) - DetectPortCleanupList(dp); + DetectPortCleanupList(NULL, dp); return NULL; } @@ -1467,7 +1469,7 @@ static char DetectPortCompareFunc(void *data1, uint16_t len1, static void DetectPortHashFreeFunc(void *ptr) { DetectPort *p = ptr; - DetectPortFree(p); + DetectPortFree(NULL, p); } /** @@ -1601,7 +1603,7 @@ static int PortTestParse01 (void) int r = DetectPortParse(NULL,&dd,"80"); if (r == 0) { - DetectPortFree(dd); + DetectPortFree(NULL, dd); return 1; } @@ -1623,7 +1625,7 @@ static int PortTestParse02 (void) result = 1; } - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); return result; } @@ -1645,7 +1647,7 @@ static int PortTestParse03 (void) result = 1; } - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); return result; } @@ -1662,7 +1664,7 @@ static int PortTestParse04 (void) int r = DetectPortParse(NULL,&dd,"!80:81"); if (r == 0) { - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); return 1; } @@ -1691,7 +1693,7 @@ static int PortTestParse05 (void) if (dd->next->port != 82 || dd->next->port2 != 65535) goto end; - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); result = 1; end: return result; @@ -1744,9 +1746,9 @@ static int PortTestParse06 (void) end: if (copy != NULL) - DetectPortCleanupList(copy); + DetectPortCleanupList(NULL, copy); if (dd != NULL) - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); return result; } @@ -1772,7 +1774,7 @@ static int PortTestParse07 (void) if (dd->next->port != 903 || dd->next->port2 != 65535) goto end; - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); result = 1; end: return result; @@ -1790,7 +1792,7 @@ static int PortTestParse08 (void) if (r == 0) goto end; - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); result = 1; end: return result; @@ -1814,7 +1816,7 @@ static int PortTestParse09 (void) if (dd->port != 1024 || dd->port2 != 0xffff) goto end; - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); result = 1; end: return result; @@ -1834,7 +1836,7 @@ static int PortTestParse10 (void) goto end; } - DetectPortFree(dd); + DetectPortFree(NULL, dd); end: return result; @@ -1854,7 +1856,7 @@ static int PortTestParse11 (void) goto end; } - DetectPortFree(dd); + DetectPortFree(NULL, dd); end: return result; @@ -1873,7 +1875,7 @@ static int PortTestParse12 (void) goto end; } - DetectPortFree(dd); + DetectPortFree(NULL, dd); result = 1 ; end: @@ -1894,7 +1896,7 @@ static int PortTestParse13 (void) goto end; } - DetectPortFree(dd); + DetectPortFree(NULL, dd); end: return result; @@ -1908,10 +1910,10 @@ static int PortTestParse14 (void) DetectPort *dd = NULL; int result = 0; - int r = DetectPortParseInsertString(&dd, "0:100"); + int r = DetectPortParseInsertString(NULL, &dd, "0:100"); if (r != 0) goto end; - r = DetectPortParseInsertString(&dd, "1000:65535"); + r = DetectPortParseInsertString(NULL, &dd, "1000:65535"); if (r != 0 || dd->next == NULL) goto end; @@ -1921,7 +1923,7 @@ static int PortTestParse14 (void) result &= (dd->next->port == 1000) ? 1 : 0; result &= (dd->next->port2 == 65535) ? 1 : 0; - DetectPortFree(dd); + DetectPortFree(NULL, dd); end: return result; @@ -1945,7 +1947,7 @@ static int PortTestParse15 (void) result &= (dd->next->port == 3001) ? 1 : 0; result &= (dd->next->port2 == 65535) ? 1 : 0; - DetectPortFree(dd); + DetectPortFree(NULL, dd); end: return result; @@ -2000,9 +2002,9 @@ static int PortTestParse16 (void) end: if (copy != NULL) - DetectPortCleanupList(copy); + DetectPortCleanupList(NULL, copy); if (dd != NULL) - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); return result; } @@ -2062,9 +2064,9 @@ static int PortTestFunctions01(void) result = 1; end: if (dp1 != NULL) - DetectPortFree(dp1); + DetectPortFree(NULL, dp1); if (head != NULL) - DetectPortFree(head); + DetectPortFree(NULL, head); return result; } @@ -2088,7 +2090,7 @@ static int PortTestFunctions02(void) goto end; /* Merge Nots */ - r = DetectPortParseMergeNotPorts(&head, &dp1); + r = DetectPortParseMergeNotPorts(NULL, &head, &dp1); if (r != 0 || head->next != NULL) goto end; @@ -2097,7 +2099,7 @@ static int PortTestFunctions02(void) goto end; /* Merge Nots */ - r = DetectPortParseMergeNotPorts(&head, &dp2); + r = DetectPortParseMergeNotPorts(NULL, &head, &dp2); if (r != 0 || head->next != NULL) goto end; @@ -2110,11 +2112,11 @@ static int PortTestFunctions02(void) end: if (dp1 != NULL) - DetectPortFree(dp1); + DetectPortFree(NULL, dp1); if (dp2 != NULL) - DetectPortFree(dp2); + DetectPortFree(NULL, dp2); if (head != NULL) - DetectPortFree(head); + DetectPortFree(NULL, head); return result; } @@ -2176,11 +2178,11 @@ static int PortTestFunctions03(void) end: if (dp1 != NULL) - DetectPortFree(dp1); + DetectPortFree(NULL, dp1); if (dp2 != NULL) - DetectPortFree(dp2); + DetectPortFree(NULL, dp2); if (dp3 != NULL) - DetectPortFree(dp3); + DetectPortFree(NULL, dp3); return result; } @@ -2216,9 +2218,9 @@ static int PortTestFunctions04(void) result = 1; end: if (dp1 != NULL) - DetectPortFree(dp1); + DetectPortFree(NULL, dp1); if (dp2 != NULL) - DetectPortFree(dp2); + DetectPortFree(NULL, dp2); return result; } @@ -2294,9 +2296,9 @@ static int PortTestFunctions05(void) result = 1; end: if (dp1 != NULL) - DetectPortFree(dp1); + DetectPortFree(NULL, dp1); if (dp2 != NULL) - DetectPortFree(dp2); + DetectPortFree(NULL, dp2); return result; } @@ -2372,9 +2374,9 @@ static int PortTestFunctions06(void) result = 1; end: if (dp1 != NULL) - DetectPortFree(dp1); + DetectPortFree(NULL, dp1); if (dp2 != NULL) - DetectPortFree(dp2); + DetectPortFree(NULL, dp2); return result; } @@ -2402,7 +2404,7 @@ static int PortTestFunctions07(void) FAIL_IF_NOT_NULL(DetectPortLookupGroup(dd, 2)); FAIL_IF_NULL(DetectPortLookupGroup(dd, 80)); - DetectPortCleanupList(dd); + DetectPortCleanupList(NULL, dd); PASS; } diff --git a/src/detect-engine-port.h b/src/detect-engine-port.h index ed0b950c7a..f82e1c360e 100644 --- a/src/detect-engine-port.h +++ b/src/detect-engine-port.h @@ -31,7 +31,7 @@ DetectPort *DetectPortCopy(DetectEngineCtx *, DetectPort *); DetectPort *DetectPortCopySingle(DetectEngineCtx *, DetectPort *); int DetectPortInsertCopy(DetectEngineCtx *,DetectPort **, DetectPort *); int DetectPortInsert(DetectEngineCtx *,DetectPort **, DetectPort *); -void DetectPortCleanupList (DetectPort *head); +void DetectPortCleanupList (const DetectEngineCtx *de_ctx, DetectPort *head); DetectPort *DetectPortLookupGroup(DetectPort *dp, uint16_t port); @@ -40,7 +40,7 @@ int DetectPortJoin(DetectEngineCtx *,DetectPort *target, DetectPort *source); void DetectPortPrint(DetectPort *); void DetectPortPrintList(DetectPort *head); int DetectPortCmp(DetectPort *, DetectPort *); -void DetectPortFree(DetectPort *); +void DetectPortFree(const DetectEngineCtx *de_ctx, DetectPort *); int DetectPortTestConfVars(void); diff --git a/src/detect-engine-prefilter.c b/src/detect-engine-prefilter.c index 20acdb8ad1..f9075ba8d4 100644 --- a/src/detect-engine-prefilter.c +++ b/src/detect-engine-prefilter.c @@ -57,7 +57,8 @@ static int PrefilterStoreGetId(DetectEngineCtx *de_ctx, const char *name, void (*FreeFunc)(void *)); -static const PrefilterStore *PrefilterStoreGetStore(const uint32_t id); +static const PrefilterStore *PrefilterStoreGetStore(const DetectEngineCtx *de_ctx, + const uint32_t id); static inline void QuickSortSigIntId(SigIntId *sids, uint32_t n) { @@ -310,12 +311,12 @@ void PrefilterFreeEnginesList(PrefilterEngineList *list) } } -static void PrefilterFreeEngines(PrefilterEngine *list) +static void PrefilterFreeEngines(const DetectEngineCtx *de_ctx, PrefilterEngine *list) { PrefilterEngine *t = list; while (1) { - const PrefilterStore *s = PrefilterStoreGetStore(t->gid); + const PrefilterStore *s = PrefilterStoreGetStore(de_ctx, t->gid); if (s && s->FreeFunc && t->pectx) { s->FreeFunc(t->pectx); } @@ -327,18 +328,18 @@ static void PrefilterFreeEngines(PrefilterEngine *list) SCFreeAligned(list); } -void PrefilterCleanupRuleGroup(SigGroupHead *sgh) +void PrefilterCleanupRuleGroup(const DetectEngineCtx *de_ctx, SigGroupHead *sgh) { if (sgh->pkt_engines) { - PrefilterFreeEngines(sgh->pkt_engines); + PrefilterFreeEngines(de_ctx, sgh->pkt_engines); sgh->pkt_engines = NULL; } if (sgh->payload_engines) { - PrefilterFreeEngines(sgh->payload_engines); + PrefilterFreeEngines(de_ctx, sgh->payload_engines); sgh->payload_engines = NULL; } if (sgh->tx_engines) { - PrefilterFreeEngines(sgh->tx_engines); + PrefilterFreeEngines(de_ctx, sgh->tx_engines); sgh->tx_engines = NULL; } } @@ -471,30 +472,22 @@ static void PrefilterStoreFreeFunc(void *ptr) SCFree(ptr); } -static SCMutex g_prefilter_mutex = SCMUTEX_INITIALIZER; -uint32_t g_prefilter_id = 0; -HashListTable *g_prefilter_hash_table = NULL; - -static void PrefilterDeinit(void) +void PrefilterDeinit(DetectEngineCtx *de_ctx) { - SCMutexLock(&g_prefilter_mutex); - BUG_ON(g_prefilter_hash_table == NULL); - HashListTableFree(g_prefilter_hash_table); - SCMutexUnlock(&g_prefilter_mutex); + if (de_ctx->prefilter_hash_table != NULL) { + HashListTableFree(de_ctx->prefilter_hash_table); + } } -static void PrefilterInit(void) +void PrefilterInit(DetectEngineCtx *de_ctx) { - SCMutexLock(&g_prefilter_mutex); - BUG_ON(g_prefilter_hash_table != NULL); + BUG_ON(de_ctx->prefilter_hash_table != NULL); - g_prefilter_hash_table = HashListTableInit(256, + de_ctx->prefilter_hash_table = HashListTableInit(256, PrefilterStoreHashFunc, PrefilterStoreCompareFunc, PrefilterStoreFreeFunc); - BUG_ON(g_prefilter_hash_table == NULL); - atexit(PrefilterDeinit); - SCMutexUnlock(&g_prefilter_mutex); + BUG_ON(de_ctx->prefilter_hash_table == NULL); } static int PrefilterStoreGetId(DetectEngineCtx *de_ctx, @@ -502,49 +495,43 @@ static int PrefilterStoreGetId(DetectEngineCtx *de_ctx, { PrefilterStore ctx = { name, FreeFunc, 0 }; - if (g_prefilter_hash_table == NULL) { - PrefilterInit(); - } + BUG_ON(de_ctx->prefilter_hash_table == NULL); SCLogDebug("looking up %s", name); - SCMutexLock(&g_prefilter_mutex); - PrefilterStore *rctx = HashListTableLookup(g_prefilter_hash_table, (void *)&ctx, 0); + PrefilterStore *rctx = HashListTableLookup(de_ctx->prefilter_hash_table, (void *)&ctx, 0); if (rctx != NULL) { - SCMutexUnlock(&g_prefilter_mutex); return rctx->id; } PrefilterStore *actx = SCCalloc(1, sizeof(*actx)); if (actx == NULL) { - SCMutexUnlock(&g_prefilter_mutex); return -1; } actx->name = name; actx->FreeFunc = FreeFunc; - actx->id = g_prefilter_id++; + actx->id = de_ctx->prefilter_id++; SCLogDebug("prefilter engine %s has profile id %u", actx->name, actx->id); - int ret = HashListTableAdd(g_prefilter_hash_table, actx, 0); + int ret = HashListTableAdd(de_ctx->prefilter_hash_table, actx, 0); if (ret != 0) { - SCMutexUnlock(&g_prefilter_mutex); SCFree(actx); return -1; } int r = actx->id; - SCMutexUnlock(&g_prefilter_mutex); return r; } /** \warning slow */ -static const PrefilterStore *PrefilterStoreGetStore(const uint32_t id) +static const PrefilterStore *PrefilterStoreGetStore(const DetectEngineCtx *de_ctx, + const uint32_t id) { + const PrefilterStore *store = NULL; - SCMutexLock(&g_prefilter_mutex); - if (g_prefilter_hash_table != NULL) { - HashListTableBucket *hb = HashListTableGetListHead(g_prefilter_hash_table); + if (de_ctx->prefilter_hash_table != NULL) { + HashListTableBucket *hb = HashListTableGetListHead(de_ctx->prefilter_hash_table); for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) { PrefilterStore *ctx = HashListTableGetListData(hb); if (ctx->id == id) { @@ -553,28 +540,13 @@ static const PrefilterStore *PrefilterStoreGetStore(const uint32_t id) } } } - SCMutexUnlock(&g_prefilter_mutex); return store; } #ifdef PROFILING -/** \warning slow */ const char *PrefilterStoreGetName(const uint32_t id) { - const char *name = NULL; - SCMutexLock(&g_prefilter_mutex); - if (g_prefilter_hash_table != NULL) { - HashListTableBucket *hb = HashListTableGetListHead(g_prefilter_hash_table); - for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) { - PrefilterStore *ctx = HashListTableGetListData(hb); - if (ctx->id == id) { - name = ctx->name; - break; - } - } - } - SCMutexUnlock(&g_prefilter_mutex); - return name; + return NULL; } #endif diff --git a/src/detect-engine-prefilter.h b/src/detect-engine-prefilter.h index 6469883eaf..33f62a66de 100644 --- a/src/detect-engine-prefilter.h +++ b/src/detect-engine-prefilter.h @@ -63,12 +63,15 @@ void DetectRunPrefilterTx(DetectEngineThreadCtx *det_ctx, void PrefilterFreeEnginesList(PrefilterEngineList *list); void PrefilterSetupRuleGroup(DetectEngineCtx *de_ctx, SigGroupHead *sgh); -void PrefilterCleanupRuleGroup(SigGroupHead *sgh); +void PrefilterCleanupRuleGroup(const DetectEngineCtx *de_ctx, SigGroupHead *sgh); #ifdef PROFILING const char *PrefilterStoreGetName(const uint32_t id); #endif +void PrefilterInit(DetectEngineCtx *de_ctx); +void PrefilterDeinit(DetectEngineCtx *de_ctx); + int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectMpmAppLayerRegistery *mpm_reg, int list_id); diff --git a/src/detect-engine-siggroup.c b/src/detect-engine-siggroup.c index 3a839dc4c1..9502ef80f1 100644 --- a/src/detect-engine-siggroup.c +++ b/src/detect-engine-siggroup.c @@ -139,7 +139,7 @@ static SigGroupHead *SigGroupHeadAlloc(const DetectEngineCtx *de_ctx, uint32_t s return sgh; error: - SigGroupHeadFree(sgh); + SigGroupHeadFree(de_ctx, sgh); return NULL; } @@ -148,7 +148,7 @@ error: * * \param sgh Pointer to the SigGroupHead that has to be freed. */ -void SigGroupHeadFree(SigGroupHead *sgh) +void SigGroupHeadFree(const DetectEngineCtx *de_ctx, SigGroupHead *sgh) { if (sgh == NULL) return; @@ -179,7 +179,7 @@ void SigGroupHeadFree(SigGroupHead *sgh) sgh->init = NULL; } - PrefilterCleanupRuleGroup(sgh); + PrefilterCleanupRuleGroup(de_ctx, sgh); SCFree(sgh); return; @@ -869,7 +869,7 @@ static int SigGroupHeadTest06(void) result &= (SigGroupHeadContainsSigId(de_ctx, sh, 4) == 0); result &= (SigGroupHeadContainsSigId(de_ctx, sh, 5) == 1); - SigGroupHeadFree(sh); + SigGroupHeadFree(de_ctx, sh); end: SigCleanSignatures(de_ctx); @@ -962,7 +962,7 @@ static int SigGroupHeadTest07(void) result &= (SigGroupHeadContainsSigId(de_ctx, sh, 4) == 0); result &= (SigGroupHeadContainsSigId(de_ctx, sh, 5) == 0); - SigGroupHeadFree(sh); + SigGroupHeadFree(de_ctx, sh); end: SigCleanSignatures(de_ctx); @@ -1056,8 +1056,8 @@ static int SigGroupHeadTest08(void) result &= (SigGroupHeadContainsSigId(de_ctx, dst_sh, 4) == 0); result &= (SigGroupHeadContainsSigId(de_ctx, dst_sh, 5) == 1); - SigGroupHeadFree(src_sh); - SigGroupHeadFree(dst_sh); + SigGroupHeadFree(de_ctx, src_sh); + SigGroupHeadFree(de_ctx, dst_sh); end: SigCleanSignatures(de_ctx); @@ -1137,7 +1137,7 @@ static int SigGroupHeadTest09(void) result &= (sh->match_array[1] == de_ctx->sig_list->next->next); result &= (sh->match_array[2] == de_ctx->sig_list->next->next->next->next); - SigGroupHeadFree(sh); + SigGroupHeadFree(de_ctx, sh); end: SigCleanSignatures(de_ctx); diff --git a/src/detect-engine-siggroup.h b/src/detect-engine-siggroup.h index a5b2480826..1c5c3357cf 100644 --- a/src/detect-engine-siggroup.h +++ b/src/detect-engine-siggroup.h @@ -28,7 +28,7 @@ int SigGroupHeadAppendSig(const DetectEngineCtx *, SigGroupHead **, const Signat int SigGroupHeadClearSigs(SigGroupHead *); int SigGroupHeadCopySigs(DetectEngineCtx *, SigGroupHead *, SigGroupHead **); -void SigGroupHeadFree(SigGroupHead *); +void SigGroupHeadFree(const DetectEngineCtx *de_ctx, SigGroupHead *); void SigGroupHeadFreeMpmArrays(DetectEngineCtx *); diff --git a/src/detect-engine.c b/src/detect-engine.c index 62978dfc35..647205c1d5 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -41,6 +41,7 @@ #include "detect-engine-siggroup.h" #include "detect-engine-address.h" #include "detect-engine-port.h" +#include "detect-engine-prefilter.h" #include "detect-engine-mpm.h" #include "detect-engine-iponly.h" #include "detect-engine-tag.h" @@ -940,6 +941,7 @@ static void DetectBufferTypeSetupDetectEngine(DetectEngineCtx *de_ctx) } de_ctx->buffer_type_id = g_buffer_type_id; + PrefilterInit(de_ctx); DetectMpmInitializeAppMpms(de_ctx); DetectAppLayerInspectEngineCopyListToDetectCtx(de_ctx); } @@ -964,6 +966,7 @@ static void DetectBufferTypeFreeDetectEngine(DetectEngineCtx *de_ctx) SCFree(mlist); mlist = next; } + PrefilterDeinit(de_ctx); } } @@ -1632,7 +1635,6 @@ void DetectEngineCtxFree(DetectEngineCtx *de_ctx) SCClassConfDeInitContext(de_ctx); SCRConfDeInitContext(de_ctx); - DetectBufferTypeFreeDetectEngine(de_ctx); SigGroupCleanup(de_ctx); @@ -1659,9 +1661,10 @@ void DetectEngineCtxFree(DetectEngineCtx *de_ctx) #endif } - DetectPortCleanupList(de_ctx->tcp_whitelist); - DetectPortCleanupList(de_ctx->udp_whitelist); + DetectPortCleanupList(de_ctx, de_ctx->tcp_whitelist); + DetectPortCleanupList(de_ctx, de_ctx->udp_whitelist); + DetectBufferTypeFreeDetectEngine(de_ctx); /* freed our var name hash */ VarNameStoreFree(de_ctx->version); @@ -1911,7 +1914,7 @@ static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx) if (x->port != x->port2) { SCLogWarning(SC_ERR_INVALID_YAML_CONF_ENTRY, "'%s' is not a valid value " "for detect.grouping.tcp-whitelist: only single ports allowed", ports); - DetectPortCleanupList(de_ctx->tcp_whitelist); + DetectPortCleanupList(de_ctx, de_ctx->tcp_whitelist); de_ctx->tcp_whitelist = NULL; break; } @@ -1934,7 +1937,7 @@ static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx) if (x->port != x->port2) { SCLogWarning(SC_ERR_INVALID_YAML_CONF_ENTRY, "'%s' is not a valid value " "for detect.grouping.udp-whitelist: only single ports allowed", ports); - DetectPortCleanupList(de_ctx->udp_whitelist); + DetectPortCleanupList(de_ctx, de_ctx->udp_whitelist); de_ctx->udp_whitelist = NULL; break; } diff --git a/src/detect-parse.c b/src/detect-parse.c index 26db89b8e7..5b3cd3243b 100644 --- a/src/detect-parse.c +++ b/src/detect-parse.c @@ -1319,10 +1319,10 @@ void SigFree(Signature *s) } if (s->sp != NULL) { - DetectPortCleanupList(s->sp); + DetectPortCleanupList(NULL, s->sp); } if (s->dp != NULL) { - DetectPortCleanupList(s->dp); + DetectPortCleanupList(NULL, s->dp); } if (s->msg != NULL) @@ -2317,9 +2317,12 @@ static int SigParseTest02 (void) } end: - if (port != NULL) DetectPortCleanupList(port); - if (sig != NULL) SigFree(sig); - if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); + if (port != NULL) + DetectPortCleanupList(de_ctx, port); + if (sig != NULL) + SigFree(sig); + if (de_ctx != NULL) + DetectEngineCtxFree(de_ctx); return result; } diff --git a/src/detect.h b/src/detect.h index 878f08afbd..64efe593b4 100644 --- a/src/detect.h +++ b/src/detect.h @@ -866,6 +866,9 @@ typedef struct DetectEngineCtx_ { DetectMpmAppLayerRegistery *app_mpms_list; uint32_t app_mpms_list_cnt; + uint32_t prefilter_id; + HashListTable *prefilter_hash_table; + /** table with mpms and their registration function * \todo we only need this at init, so perhaps this * can move to a DetectEngineCtx 'init' struct */ diff --git a/src/util-profiling-prefilter.c b/src/util-profiling-prefilter.c index baac3edd0e..2ae498557f 100644 --- a/src/util-profiling-prefilter.c +++ b/src/util-profiling-prefilter.c @@ -40,9 +40,6 @@ #ifdef PROFILING -extern uint32_t g_prefilter_id; -extern HashListTable *g_prefilter_hash_table; - typedef struct SCProfilePrefilterData_ { uint64_t called; uint64_t total; @@ -186,7 +183,7 @@ void SCProfilingPrefilterUpdateCounter(DetectEngineThreadCtx *det_ctx, int id, uint64_t ticks) { if (det_ctx != NULL && det_ctx->prefilter_perf_data != NULL && - id < (int)g_prefilter_id) + id < (int)det_ctx->de_ctx->prefilter_id) { SCProfilePrefilterData *p = &det_ctx->prefilter_perf_data[id]; @@ -237,7 +234,7 @@ void SCProfilingPrefilterThreadSetup(SCProfilePrefilterDetectCtx *ctx, DetectEng if (ctx == NULL) return; - const uint32_t size = g_prefilter_id; + const uint32_t size = det_ctx->de_ctx->prefilter_id; SCProfilePrefilterData *a = SCMalloc(sizeof(SCProfilePrefilterData) * size); if (a != NULL) { @@ -253,7 +250,7 @@ static void SCProfilingPrefilterThreadMerge(DetectEngineCtx *de_ctx, DetectEngin det_ctx->prefilter_perf_data == NULL) return; - for (uint32_t i = 0; i < g_prefilter_id; i++) { + for (uint32_t i = 0; i < de_ctx->prefilter_id; i++) { de_ctx->profile_prefilter_ctx->data[i].called += det_ctx->prefilter_perf_data[i].called; de_ctx->profile_prefilter_ctx->data[i].total += det_ctx->prefilter_perf_data[i].total; if (det_ctx->prefilter_perf_data[i].max > de_ctx->profile_prefilter_ctx->data[i].max) @@ -285,8 +282,8 @@ SCProfilingPrefilterInitCounters(DetectEngineCtx *de_ctx) if (profiling_prefilter_enabled == 0) return; - const uint32_t size = g_prefilter_id; - if (g_prefilter_id == 0) + const uint32_t size = de_ctx->prefilter_id; + if (size == 0) return; de_ctx->profile_prefilter_ctx = SCProfilingPrefilterInitCtx(); @@ -297,7 +294,7 @@ SCProfilingPrefilterInitCounters(DetectEngineCtx *de_ctx) BUG_ON(de_ctx->profile_prefilter_ctx->data == NULL); memset(de_ctx->profile_prefilter_ctx->data, 0x00, sizeof(SCProfilePrefilterData) * size); - HashListTableBucket *hb = HashListTableGetListHead(g_prefilter_hash_table); + HashListTableBucket *hb = HashListTableGetListHead(de_ctx->prefilter_hash_table); for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) { PrefilterStore *ctx = HashListTableGetListData(hb); de_ctx->profile_prefilter_ctx->data[ctx->id].name = ctx->name;