detect/prefilter: move hash into detect engine ctx

pull/3246/head
Victor Julien 9 years ago
parent 91296d1eec
commit d64785274e

@ -1468,7 +1468,7 @@ void AppLayerProtoDetectPPRegister(uint8_t ipproto,
} }
temp_dp = temp_dp->next; temp_dp = temp_dp->next;
} }
DetectPortCleanupList(head); DetectPortCleanupList(NULL,head);
SCReturn; SCReturn;
} }

@ -974,7 +974,7 @@ static int RulesGroupByProto(DetectEngineCtx *de_ctx)
} else { } else {
SCLogDebug("proto group %d sgh %p is a copy", p, sgh_ts[p]); 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; sgh_ts[p] = lookup_sgh;
de_ctx->gh_reuse++; de_ctx->gh_reuse++;
@ -1011,7 +1011,7 @@ static int RulesGroupByProto(DetectEngineCtx *de_ctx)
} else { } else {
SCLogDebug("proto group %d sgh %p is a copy", p, sgh_tc[p]); 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; sgh_tc[p] = lookup_sgh;
de_ctx->gh_reuse++; de_ctx->gh_reuse++;
@ -1216,7 +1216,7 @@ static DetectPort *RulesGroupByPorts(DetectEngineCtx *de_ctx, int ipproto, uint3
} else { } else {
SCLogDebug("port group %p sgh %p is a copy", iter, iter->sh); 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->sh = lookup_sgh;
iter->flags |= PORT_SIGGROUPHEAD_COPY; 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 */ /* when a group's sigs are added to the joingr, we can free it */
gr->next = NULL; gr->next = NULL;
DetectPortFree(gr); DetectPortFree(de_ctx, gr);
gr = NULL; gr = NULL;
/* append */ /* append */
@ -1666,7 +1666,7 @@ int SigAddressCleanupStage1(DetectEngineCtx *de_ctx)
SCLogDebug("cleaning up signature grouping structure..."); SCLogDebug("cleaning up signature grouping structure...");
} }
if (de_ctx->decoder_event_sgh) 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; de_ctx->decoder_event_sgh = NULL;
int f; int f;
@ -1677,9 +1677,9 @@ int SigAddressCleanupStage1(DetectEngineCtx *de_ctx)
} }
/* free lookup lists */ /* 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; 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; de_ctx->flow_gh[f].udp = NULL;
} }
@ -1690,7 +1690,7 @@ int SigAddressCleanupStage1(DetectEngineCtx *de_ctx)
continue; continue;
SCLogDebug("sgh %p", sgh); SCLogDebug("sgh %p", sgh);
SigGroupHeadFree(sgh); SigGroupHeadFree(de_ctx, sgh);
} }
SCFree(de_ctx->sgh_array); SCFree(de_ctx->sgh_array);
de_ctx->sgh_array = NULL; de_ctx->sgh_array = NULL;

@ -80,14 +80,14 @@ static DetectPort *DetectPortInit(void)
* *
* \param dp Pointer to the DetectPort that has to be freed. * \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) if (dp == NULL)
return; return;
/* only free the head if we have the original */ /* only free the head if we have the original */
if (dp->sh != NULL && !(dp->flags & PORT_SIGGROUPHEAD_COPY)) { if (dp->sh != NULL && !(dp->flags & PORT_SIGGROUPHEAD_COPY)) {
SigGroupHeadFree(dp->sh); SigGroupHeadFree(de_ctx, dp->sh);
} }
dp->sh = NULL; dp->sh = NULL;
@ -121,7 +121,7 @@ void DetectPortPrintList(DetectPort *head)
* *
* \param head Pointer to the DetectPort list head * \param head Pointer to the DetectPort list head
*/ */
void DetectPortCleanupList (DetectPort *head) void DetectPortCleanupList (const DetectEngineCtx *de_ctx, DetectPort *head)
{ {
if (head == NULL) if (head == NULL)
return; return;
@ -131,7 +131,7 @@ void DetectPortCleanupList (DetectPort *head)
for (cur = head; cur != NULL; ) { for (cur = head; cur != NULL; ) {
next = cur->next; next = cur->next;
cur->next = NULL; cur->next = NULL;
DetectPortFree(cur); DetectPortFree(de_ctx, cur);
cur = next; cur = next;
} }
} }
@ -191,7 +191,7 @@ int DetectPortInsert(DetectEngineCtx *de_ctx, DetectPort **head,
/* exact overlap/match */ /* exact overlap/match */
if (cur != new) { if (cur != new) {
SigGroupHeadCopySigs(de_ctx, new->sh, &cur->sh); SigGroupHeadCopySigs(de_ctx, new->sh, &cur->sh);
DetectPortFree(new); DetectPortFree(de_ctx, new);
return 0; return 0;
} }
return 1; return 1;
@ -510,13 +510,13 @@ static int DetectPortCut(DetectEngineCtx *de_ctx, DetectPort *a,
} }
if (tmp != NULL) { if (tmp != NULL) {
DetectPortFree(tmp); DetectPortFree(de_ctx, tmp);
} }
return 0; return 0;
error: error:
if (tmp != NULL) if (tmp != NULL)
DetectPortFree(tmp); DetectPortFree(de_ctx, tmp);
return -1; return -1;
} }
@ -805,7 +805,8 @@ static int DetectPortParseInsert(DetectPort **head, DetectPort *new)
* \retval 0 on success * \retval 0 on success
* \retval -1 on error * \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; DetectPort *ad = NULL, *ad_any = NULL;
int r = 0; int r = 0;
@ -864,9 +865,9 @@ static int DetectPortParseInsertString(DetectPort **head, const char *s)
error: error:
SCLogError(SC_ERR_PORT_PARSE_INSERT_STRING,"DetectPortParseInsertString error"); SCLogError(SC_ERR_PORT_PARSE_INSERT_STRING,"DetectPortParseInsertString error");
if (ad != NULL) if (ad != NULL)
DetectPortCleanupList(ad); DetectPortCleanupList(de_ctx, ad);
if (ad_any != NULL) if (ad_any != NULL)
DetectPortCleanupList(ad_any); DetectPortCleanupList(de_ctx, ad_any);
return -1; return -1;
} }
@ -989,9 +990,9 @@ static int DetectPortParseDo(const DetectEngineCtx *de_ctx,
SCLogDebug("Parsed port from DetectPortParseDo - %s", address); SCLogDebug("Parsed port from DetectPortParseDo - %s", address);
if (negate == 0 && n_set == 0) { if (negate == 0 && n_set == 0) {
r = DetectPortParseInsertString(head, address); r = DetectPortParseInsertString(de_ctx, head, address);
} else { } else {
r = DetectPortParseInsertString(nhead, address); r = DetectPortParseInsertString(de_ctx, nhead, address);
} }
if (r == -1) if (r == -1)
goto error; goto error;
@ -1054,9 +1055,9 @@ static int DetectPortParseDo(const DetectEngineCtx *de_ctx,
d_set = 0; d_set = 0;
} else { } else {
if (!((negate + n_set) % 2)) { if (!((negate + n_set) % 2)) {
r = DetectPortParseInsertString(head,address); r = DetectPortParseInsertString(de_ctx, head,address);
} else { } else {
r = DetectPortParseInsertString(nhead,address); r = DetectPortParseInsertString(de_ctx, nhead,address);
} }
if (r == -1) if (r == -1)
goto error; goto error;
@ -1129,7 +1130,8 @@ static int DetectPortIsCompletePortSpace(DetectPort *p)
* \retval 0 on success * \retval 0 on success
* \retval -1 on error * \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 *ad = NULL;
DetectPort *ag, *ag2; DetectPort *ag, *ag2;
@ -1148,7 +1150,7 @@ static int DetectPortParseMergeNotPorts(DetectPort **head, DetectPort **nhead)
*/ */
if (*head == NULL && *nhead != NULL) { if (*head == NULL && *nhead != NULL) {
SCLogDebug("inserting 0:65535 into head"); SCLogDebug("inserting 0:65535 into head");
r = DetectPortParseInsertString(head,"0:65535"); r = DetectPortParseInsertString(de_ctx, head,"0:65535");
if (r < 0) { if (r < 0) {
goto error; goto error;
} }
@ -1192,7 +1194,7 @@ static int DetectPortParseMergeNotPorts(DetectPort **head, DetectPort **nhead)
} }
/** store the next ptr and remove the group */ /** store the next ptr and remove the group */
DetectPort *next_ag2 = ag2->next; DetectPort *next_ag2 = ag2->next;
DetectPortFree(ag2); DetectPortFree(de_ctx,ag2);
ag2 = next_ag2; ag2 = next_ag2;
} else { } else {
ag2 = ag2->next; ag2 = ag2->next;
@ -1213,7 +1215,7 @@ static int DetectPortParseMergeNotPorts(DetectPort **head, DetectPort **nhead)
return 0; return 0;
error: error:
if (ad != NULL) if (ad != NULL)
DetectPortFree(ad); DetectPortFree(de_ctx, ad);
return -1; return -1;
} }
@ -1243,7 +1245,7 @@ int DetectPortTestConfVars(void)
"Port var \"%s\" probably has a sequence(something " "Port var \"%s\" probably has a sequence(something "
"in brackets) value set without any quotes. Please " "in brackets) value set without any quotes. Please "
"quote it using \"..\".", seq_node->name); "quote it using \"..\".", seq_node->name);
DetectPortCleanupList(gh); DetectPortCleanupList(NULL, gh);
goto error; goto error;
} }
@ -1252,7 +1254,7 @@ int DetectPortTestConfVars(void)
CleanVariableResolveList(&var_list); CleanVariableResolveList(&var_list);
if (r < 0) { if (r < 0) {
DetectPortCleanupList(gh); DetectPortCleanupList(NULL, gh);
SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY, SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY,
"failed to parse port var \"%s\" with value \"%s\". " "failed to parse port var \"%s\" with value \"%s\". "
"Please check it's syntax", seq_node->name, seq_node->val); "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. " "with it's value \"%s\". Port space range is NIL. "
"Probably have a !any or a port range that supplies " "Probably have a !any or a port range that supplies "
"a NULL address range", seq_node->name, seq_node->val); "a NULL address range", seq_node->name, seq_node->val);
DetectPortCleanupList(gh); DetectPortCleanupList(NULL, gh);
DetectPortCleanupList(ghn); DetectPortCleanupList(NULL, ghn);
goto error; goto error;
} }
if (gh != NULL) if (gh != NULL)
DetectPortCleanupList(gh); DetectPortCleanupList(NULL, gh);
if (ghn != NULL) if (ghn != NULL)
DetectPortCleanupList(ghn); DetectPortCleanupList(NULL, ghn);
} }
return 0; return 0;
@ -1308,15 +1310,15 @@ int DetectPortParse(const DetectEngineCtx *de_ctx,
SCLogDebug("head %p %p, nhead %p", head, *head, nhead); SCLogDebug("head %p %p, nhead %p", head, *head, nhead);
/* merge the 'not' address groups */ /* merge the 'not' address groups */
if (DetectPortParseMergeNotPorts(head, &nhead) < 0) if (DetectPortParseMergeNotPorts(de_ctx, head, &nhead) < 0)
goto error; goto error;
/* free the temp negate head */ /* free the temp negate head */
DetectPortCleanupList(nhead); DetectPortCleanupList(de_ctx, nhead);
return 0; return 0;
error: error:
DetectPortCleanupList(nhead); DetectPortCleanupList(de_ctx, nhead);
return -1; return -1;
} }
@ -1389,7 +1391,7 @@ DetectPort *PortParse(const char *str)
error: error:
if (dp != NULL) if (dp != NULL)
DetectPortCleanupList(dp); DetectPortCleanupList(NULL, dp);
return NULL; return NULL;
} }
@ -1467,7 +1469,7 @@ static char DetectPortCompareFunc(void *data1, uint16_t len1,
static void DetectPortHashFreeFunc(void *ptr) static void DetectPortHashFreeFunc(void *ptr)
{ {
DetectPort *p = ptr; DetectPort *p = ptr;
DetectPortFree(p); DetectPortFree(NULL, p);
} }
/** /**
@ -1601,7 +1603,7 @@ static int PortTestParse01 (void)
int r = DetectPortParse(NULL,&dd,"80"); int r = DetectPortParse(NULL,&dd,"80");
if (r == 0) { if (r == 0) {
DetectPortFree(dd); DetectPortFree(NULL, dd);
return 1; return 1;
} }
@ -1623,7 +1625,7 @@ static int PortTestParse02 (void)
result = 1; result = 1;
} }
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
return result; return result;
} }
@ -1645,7 +1647,7 @@ static int PortTestParse03 (void)
result = 1; result = 1;
} }
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
return result; return result;
} }
@ -1662,7 +1664,7 @@ static int PortTestParse04 (void)
int r = DetectPortParse(NULL,&dd,"!80:81"); int r = DetectPortParse(NULL,&dd,"!80:81");
if (r == 0) { if (r == 0) {
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
return 1; return 1;
} }
@ -1691,7 +1693,7 @@ static int PortTestParse05 (void)
if (dd->next->port != 82 || dd->next->port2 != 65535) if (dd->next->port != 82 || dd->next->port2 != 65535)
goto end; goto end;
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
result = 1; result = 1;
end: end:
return result; return result;
@ -1744,9 +1746,9 @@ static int PortTestParse06 (void)
end: end:
if (copy != NULL) if (copy != NULL)
DetectPortCleanupList(copy); DetectPortCleanupList(NULL, copy);
if (dd != NULL) if (dd != NULL)
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
return result; return result;
} }
@ -1772,7 +1774,7 @@ static int PortTestParse07 (void)
if (dd->next->port != 903 || dd->next->port2 != 65535) if (dd->next->port != 903 || dd->next->port2 != 65535)
goto end; goto end;
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
result = 1; result = 1;
end: end:
return result; return result;
@ -1790,7 +1792,7 @@ static int PortTestParse08 (void)
if (r == 0) if (r == 0)
goto end; goto end;
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
result = 1; result = 1;
end: end:
return result; return result;
@ -1814,7 +1816,7 @@ static int PortTestParse09 (void)
if (dd->port != 1024 || dd->port2 != 0xffff) if (dd->port != 1024 || dd->port2 != 0xffff)
goto end; goto end;
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
result = 1; result = 1;
end: end:
return result; return result;
@ -1834,7 +1836,7 @@ static int PortTestParse10 (void)
goto end; goto end;
} }
DetectPortFree(dd); DetectPortFree(NULL, dd);
end: end:
return result; return result;
@ -1854,7 +1856,7 @@ static int PortTestParse11 (void)
goto end; goto end;
} }
DetectPortFree(dd); DetectPortFree(NULL, dd);
end: end:
return result; return result;
@ -1873,7 +1875,7 @@ static int PortTestParse12 (void)
goto end; goto end;
} }
DetectPortFree(dd); DetectPortFree(NULL, dd);
result = 1 ; result = 1 ;
end: end:
@ -1894,7 +1896,7 @@ static int PortTestParse13 (void)
goto end; goto end;
} }
DetectPortFree(dd); DetectPortFree(NULL, dd);
end: end:
return result; return result;
@ -1908,10 +1910,10 @@ static int PortTestParse14 (void)
DetectPort *dd = NULL; DetectPort *dd = NULL;
int result = 0; int result = 0;
int r = DetectPortParseInsertString(&dd, "0:100"); int r = DetectPortParseInsertString(NULL, &dd, "0:100");
if (r != 0) if (r != 0)
goto end; goto end;
r = DetectPortParseInsertString(&dd, "1000:65535"); r = DetectPortParseInsertString(NULL, &dd, "1000:65535");
if (r != 0 || dd->next == NULL) if (r != 0 || dd->next == NULL)
goto end; goto end;
@ -1921,7 +1923,7 @@ static int PortTestParse14 (void)
result &= (dd->next->port == 1000) ? 1 : 0; result &= (dd->next->port == 1000) ? 1 : 0;
result &= (dd->next->port2 == 65535) ? 1 : 0; result &= (dd->next->port2 == 65535) ? 1 : 0;
DetectPortFree(dd); DetectPortFree(NULL, dd);
end: end:
return result; return result;
@ -1945,7 +1947,7 @@ static int PortTestParse15 (void)
result &= (dd->next->port == 3001) ? 1 : 0; result &= (dd->next->port == 3001) ? 1 : 0;
result &= (dd->next->port2 == 65535) ? 1 : 0; result &= (dd->next->port2 == 65535) ? 1 : 0;
DetectPortFree(dd); DetectPortFree(NULL, dd);
end: end:
return result; return result;
@ -2000,9 +2002,9 @@ static int PortTestParse16 (void)
end: end:
if (copy != NULL) if (copy != NULL)
DetectPortCleanupList(copy); DetectPortCleanupList(NULL, copy);
if (dd != NULL) if (dd != NULL)
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
return result; return result;
} }
@ -2062,9 +2064,9 @@ static int PortTestFunctions01(void)
result = 1; result = 1;
end: end:
if (dp1 != NULL) if (dp1 != NULL)
DetectPortFree(dp1); DetectPortFree(NULL, dp1);
if (head != NULL) if (head != NULL)
DetectPortFree(head); DetectPortFree(NULL, head);
return result; return result;
} }
@ -2088,7 +2090,7 @@ static int PortTestFunctions02(void)
goto end; goto end;
/* Merge Nots */ /* Merge Nots */
r = DetectPortParseMergeNotPorts(&head, &dp1); r = DetectPortParseMergeNotPorts(NULL, &head, &dp1);
if (r != 0 || head->next != NULL) if (r != 0 || head->next != NULL)
goto end; goto end;
@ -2097,7 +2099,7 @@ static int PortTestFunctions02(void)
goto end; goto end;
/* Merge Nots */ /* Merge Nots */
r = DetectPortParseMergeNotPorts(&head, &dp2); r = DetectPortParseMergeNotPorts(NULL, &head, &dp2);
if (r != 0 || head->next != NULL) if (r != 0 || head->next != NULL)
goto end; goto end;
@ -2110,11 +2112,11 @@ static int PortTestFunctions02(void)
end: end:
if (dp1 != NULL) if (dp1 != NULL)
DetectPortFree(dp1); DetectPortFree(NULL, dp1);
if (dp2 != NULL) if (dp2 != NULL)
DetectPortFree(dp2); DetectPortFree(NULL, dp2);
if (head != NULL) if (head != NULL)
DetectPortFree(head); DetectPortFree(NULL, head);
return result; return result;
} }
@ -2176,11 +2178,11 @@ static int PortTestFunctions03(void)
end: end:
if (dp1 != NULL) if (dp1 != NULL)
DetectPortFree(dp1); DetectPortFree(NULL, dp1);
if (dp2 != NULL) if (dp2 != NULL)
DetectPortFree(dp2); DetectPortFree(NULL, dp2);
if (dp3 != NULL) if (dp3 != NULL)
DetectPortFree(dp3); DetectPortFree(NULL, dp3);
return result; return result;
} }
@ -2216,9 +2218,9 @@ static int PortTestFunctions04(void)
result = 1; result = 1;
end: end:
if (dp1 != NULL) if (dp1 != NULL)
DetectPortFree(dp1); DetectPortFree(NULL, dp1);
if (dp2 != NULL) if (dp2 != NULL)
DetectPortFree(dp2); DetectPortFree(NULL, dp2);
return result; return result;
} }
@ -2294,9 +2296,9 @@ static int PortTestFunctions05(void)
result = 1; result = 1;
end: end:
if (dp1 != NULL) if (dp1 != NULL)
DetectPortFree(dp1); DetectPortFree(NULL, dp1);
if (dp2 != NULL) if (dp2 != NULL)
DetectPortFree(dp2); DetectPortFree(NULL, dp2);
return result; return result;
} }
@ -2372,9 +2374,9 @@ static int PortTestFunctions06(void)
result = 1; result = 1;
end: end:
if (dp1 != NULL) if (dp1 != NULL)
DetectPortFree(dp1); DetectPortFree(NULL, dp1);
if (dp2 != NULL) if (dp2 != NULL)
DetectPortFree(dp2); DetectPortFree(NULL, dp2);
return result; return result;
} }
@ -2402,7 +2404,7 @@ static int PortTestFunctions07(void)
FAIL_IF_NOT_NULL(DetectPortLookupGroup(dd, 2)); FAIL_IF_NOT_NULL(DetectPortLookupGroup(dd, 2));
FAIL_IF_NULL(DetectPortLookupGroup(dd, 80)); FAIL_IF_NULL(DetectPortLookupGroup(dd, 80));
DetectPortCleanupList(dd); DetectPortCleanupList(NULL, dd);
PASS; PASS;
} }

@ -31,7 +31,7 @@ DetectPort *DetectPortCopy(DetectEngineCtx *, DetectPort *);
DetectPort *DetectPortCopySingle(DetectEngineCtx *, DetectPort *); DetectPort *DetectPortCopySingle(DetectEngineCtx *, DetectPort *);
int DetectPortInsertCopy(DetectEngineCtx *,DetectPort **, DetectPort *); int DetectPortInsertCopy(DetectEngineCtx *,DetectPort **, DetectPort *);
int DetectPortInsert(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); DetectPort *DetectPortLookupGroup(DetectPort *dp, uint16_t port);
@ -40,7 +40,7 @@ int DetectPortJoin(DetectEngineCtx *,DetectPort *target, DetectPort *source);
void DetectPortPrint(DetectPort *); void DetectPortPrint(DetectPort *);
void DetectPortPrintList(DetectPort *head); void DetectPortPrintList(DetectPort *head);
int DetectPortCmp(DetectPort *, DetectPort *); int DetectPortCmp(DetectPort *, DetectPort *);
void DetectPortFree(DetectPort *); void DetectPortFree(const DetectEngineCtx *de_ctx, DetectPort *);
int DetectPortTestConfVars(void); int DetectPortTestConfVars(void);

@ -57,7 +57,8 @@
static int PrefilterStoreGetId(DetectEngineCtx *de_ctx, static int PrefilterStoreGetId(DetectEngineCtx *de_ctx,
const char *name, void (*FreeFunc)(void *)); 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) 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; PrefilterEngine *t = list;
while (1) { while (1) {
const PrefilterStore *s = PrefilterStoreGetStore(t->gid); const PrefilterStore *s = PrefilterStoreGetStore(de_ctx, t->gid);
if (s && s->FreeFunc && t->pectx) { if (s && s->FreeFunc && t->pectx) {
s->FreeFunc(t->pectx); s->FreeFunc(t->pectx);
} }
@ -327,18 +328,18 @@ static void PrefilterFreeEngines(PrefilterEngine *list)
SCFreeAligned(list); SCFreeAligned(list);
} }
void PrefilterCleanupRuleGroup(SigGroupHead *sgh) void PrefilterCleanupRuleGroup(const DetectEngineCtx *de_ctx, SigGroupHead *sgh)
{ {
if (sgh->pkt_engines) { if (sgh->pkt_engines) {
PrefilterFreeEngines(sgh->pkt_engines); PrefilterFreeEngines(de_ctx, sgh->pkt_engines);
sgh->pkt_engines = NULL; sgh->pkt_engines = NULL;
} }
if (sgh->payload_engines) { if (sgh->payload_engines) {
PrefilterFreeEngines(sgh->payload_engines); PrefilterFreeEngines(de_ctx, sgh->payload_engines);
sgh->payload_engines = NULL; sgh->payload_engines = NULL;
} }
if (sgh->tx_engines) { if (sgh->tx_engines) {
PrefilterFreeEngines(sgh->tx_engines); PrefilterFreeEngines(de_ctx, sgh->tx_engines);
sgh->tx_engines = NULL; sgh->tx_engines = NULL;
} }
} }
@ -471,30 +472,22 @@ static void PrefilterStoreFreeFunc(void *ptr)
SCFree(ptr); SCFree(ptr);
} }
static SCMutex g_prefilter_mutex = SCMUTEX_INITIALIZER; void PrefilterDeinit(DetectEngineCtx *de_ctx)
uint32_t g_prefilter_id = 0;
HashListTable *g_prefilter_hash_table = NULL;
static void PrefilterDeinit(void)
{ {
SCMutexLock(&g_prefilter_mutex); if (de_ctx->prefilter_hash_table != NULL) {
BUG_ON(g_prefilter_hash_table == NULL); HashListTableFree(de_ctx->prefilter_hash_table);
HashListTableFree(g_prefilter_hash_table); }
SCMutexUnlock(&g_prefilter_mutex);
} }
static void PrefilterInit(void) void PrefilterInit(DetectEngineCtx *de_ctx)
{ {
SCMutexLock(&g_prefilter_mutex); BUG_ON(de_ctx->prefilter_hash_table != NULL);
BUG_ON(g_prefilter_hash_table != NULL);
g_prefilter_hash_table = HashListTableInit(256, de_ctx->prefilter_hash_table = HashListTableInit(256,
PrefilterStoreHashFunc, PrefilterStoreHashFunc,
PrefilterStoreCompareFunc, PrefilterStoreCompareFunc,
PrefilterStoreFreeFunc); PrefilterStoreFreeFunc);
BUG_ON(g_prefilter_hash_table == NULL); BUG_ON(de_ctx->prefilter_hash_table == NULL);
atexit(PrefilterDeinit);
SCMutexUnlock(&g_prefilter_mutex);
} }
static int PrefilterStoreGetId(DetectEngineCtx *de_ctx, static int PrefilterStoreGetId(DetectEngineCtx *de_ctx,
@ -502,49 +495,43 @@ static int PrefilterStoreGetId(DetectEngineCtx *de_ctx,
{ {
PrefilterStore ctx = { name, FreeFunc, 0 }; PrefilterStore ctx = { name, FreeFunc, 0 };
if (g_prefilter_hash_table == NULL) { BUG_ON(de_ctx->prefilter_hash_table == NULL);
PrefilterInit();
}
SCLogDebug("looking up %s", name); SCLogDebug("looking up %s", name);
SCMutexLock(&g_prefilter_mutex); PrefilterStore *rctx = HashListTableLookup(de_ctx->prefilter_hash_table, (void *)&ctx, 0);
PrefilterStore *rctx = HashListTableLookup(g_prefilter_hash_table, (void *)&ctx, 0);
if (rctx != NULL) { if (rctx != NULL) {
SCMutexUnlock(&g_prefilter_mutex);
return rctx->id; return rctx->id;
} }
PrefilterStore *actx = SCCalloc(1, sizeof(*actx)); PrefilterStore *actx = SCCalloc(1, sizeof(*actx));
if (actx == NULL) { if (actx == NULL) {
SCMutexUnlock(&g_prefilter_mutex);
return -1; return -1;
} }
actx->name = name; actx->name = name;
actx->FreeFunc = FreeFunc; 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); 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) { if (ret != 0) {
SCMutexUnlock(&g_prefilter_mutex);
SCFree(actx); SCFree(actx);
return -1; return -1;
} }
int r = actx->id; int r = actx->id;
SCMutexUnlock(&g_prefilter_mutex);
return r; return r;
} }
/** \warning slow */ /** \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; const PrefilterStore *store = NULL;
SCMutexLock(&g_prefilter_mutex); if (de_ctx->prefilter_hash_table != NULL) {
if (g_prefilter_hash_table != NULL) { HashListTableBucket *hb = HashListTableGetListHead(de_ctx->prefilter_hash_table);
HashListTableBucket *hb = HashListTableGetListHead(g_prefilter_hash_table);
for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) { for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) {
PrefilterStore *ctx = HashListTableGetListData(hb); PrefilterStore *ctx = HashListTableGetListData(hb);
if (ctx->id == id) { if (ctx->id == id) {
@ -553,28 +540,13 @@ static const PrefilterStore *PrefilterStoreGetStore(const uint32_t id)
} }
} }
} }
SCMutexUnlock(&g_prefilter_mutex);
return store; return store;
} }
#ifdef PROFILING #ifdef PROFILING
/** \warning slow */
const char *PrefilterStoreGetName(const uint32_t id) const char *PrefilterStoreGetName(const uint32_t id)
{ {
const char *name = NULL; return 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;
} }
#endif #endif

@ -63,12 +63,15 @@ void DetectRunPrefilterTx(DetectEngineThreadCtx *det_ctx,
void PrefilterFreeEnginesList(PrefilterEngineList *list); void PrefilterFreeEnginesList(PrefilterEngineList *list);
void PrefilterSetupRuleGroup(DetectEngineCtx *de_ctx, SigGroupHead *sgh); void PrefilterSetupRuleGroup(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
void PrefilterCleanupRuleGroup(SigGroupHead *sgh); void PrefilterCleanupRuleGroup(const DetectEngineCtx *de_ctx, SigGroupHead *sgh);
#ifdef PROFILING #ifdef PROFILING
const char *PrefilterStoreGetName(const uint32_t id); const char *PrefilterStoreGetName(const uint32_t id);
#endif #endif
void PrefilterInit(DetectEngineCtx *de_ctx);
void PrefilterDeinit(DetectEngineCtx *de_ctx);
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx,
SigGroupHead *sgh, MpmCtx *mpm_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
const DetectMpmAppLayerRegistery *mpm_reg, int list_id); const DetectMpmAppLayerRegistery *mpm_reg, int list_id);

@ -139,7 +139,7 @@ static SigGroupHead *SigGroupHeadAlloc(const DetectEngineCtx *de_ctx, uint32_t s
return sgh; return sgh;
error: error:
SigGroupHeadFree(sgh); SigGroupHeadFree(de_ctx, sgh);
return NULL; return NULL;
} }
@ -148,7 +148,7 @@ error:
* *
* \param sgh Pointer to the SigGroupHead that has to be freed. * \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) if (sgh == NULL)
return; return;
@ -179,7 +179,7 @@ void SigGroupHeadFree(SigGroupHead *sgh)
sgh->init = NULL; sgh->init = NULL;
} }
PrefilterCleanupRuleGroup(sgh); PrefilterCleanupRuleGroup(de_ctx, sgh);
SCFree(sgh); SCFree(sgh);
return; return;
@ -869,7 +869,7 @@ static int SigGroupHeadTest06(void)
result &= (SigGroupHeadContainsSigId(de_ctx, sh, 4) == 0); result &= (SigGroupHeadContainsSigId(de_ctx, sh, 4) == 0);
result &= (SigGroupHeadContainsSigId(de_ctx, sh, 5) == 1); result &= (SigGroupHeadContainsSigId(de_ctx, sh, 5) == 1);
SigGroupHeadFree(sh); SigGroupHeadFree(de_ctx, sh);
end: end:
SigCleanSignatures(de_ctx); SigCleanSignatures(de_ctx);
@ -962,7 +962,7 @@ static int SigGroupHeadTest07(void)
result &= (SigGroupHeadContainsSigId(de_ctx, sh, 4) == 0); result &= (SigGroupHeadContainsSigId(de_ctx, sh, 4) == 0);
result &= (SigGroupHeadContainsSigId(de_ctx, sh, 5) == 0); result &= (SigGroupHeadContainsSigId(de_ctx, sh, 5) == 0);
SigGroupHeadFree(sh); SigGroupHeadFree(de_ctx, sh);
end: end:
SigCleanSignatures(de_ctx); 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, 4) == 0);
result &= (SigGroupHeadContainsSigId(de_ctx, dst_sh, 5) == 1); result &= (SigGroupHeadContainsSigId(de_ctx, dst_sh, 5) == 1);
SigGroupHeadFree(src_sh); SigGroupHeadFree(de_ctx, src_sh);
SigGroupHeadFree(dst_sh); SigGroupHeadFree(de_ctx, dst_sh);
end: end:
SigCleanSignatures(de_ctx); 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[1] == de_ctx->sig_list->next->next);
result &= (sh->match_array[2] == de_ctx->sig_list->next->next->next->next); result &= (sh->match_array[2] == de_ctx->sig_list->next->next->next->next);
SigGroupHeadFree(sh); SigGroupHeadFree(de_ctx, sh);
end: end:
SigCleanSignatures(de_ctx); SigCleanSignatures(de_ctx);

@ -28,7 +28,7 @@ int SigGroupHeadAppendSig(const DetectEngineCtx *, SigGroupHead **, const Signat
int SigGroupHeadClearSigs(SigGroupHead *); int SigGroupHeadClearSigs(SigGroupHead *);
int SigGroupHeadCopySigs(DetectEngineCtx *, SigGroupHead *, SigGroupHead **); int SigGroupHeadCopySigs(DetectEngineCtx *, SigGroupHead *, SigGroupHead **);
void SigGroupHeadFree(SigGroupHead *); void SigGroupHeadFree(const DetectEngineCtx *de_ctx, SigGroupHead *);
void SigGroupHeadFreeMpmArrays(DetectEngineCtx *); void SigGroupHeadFreeMpmArrays(DetectEngineCtx *);

@ -41,6 +41,7 @@
#include "detect-engine-siggroup.h" #include "detect-engine-siggroup.h"
#include "detect-engine-address.h" #include "detect-engine-address.h"
#include "detect-engine-port.h" #include "detect-engine-port.h"
#include "detect-engine-prefilter.h"
#include "detect-engine-mpm.h" #include "detect-engine-mpm.h"
#include "detect-engine-iponly.h" #include "detect-engine-iponly.h"
#include "detect-engine-tag.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; de_ctx->buffer_type_id = g_buffer_type_id;
PrefilterInit(de_ctx);
DetectMpmInitializeAppMpms(de_ctx); DetectMpmInitializeAppMpms(de_ctx);
DetectAppLayerInspectEngineCopyListToDetectCtx(de_ctx); DetectAppLayerInspectEngineCopyListToDetectCtx(de_ctx);
} }
@ -964,6 +966,7 @@ static void DetectBufferTypeFreeDetectEngine(DetectEngineCtx *de_ctx)
SCFree(mlist); SCFree(mlist);
mlist = next; mlist = next;
} }
PrefilterDeinit(de_ctx);
} }
} }
@ -1632,7 +1635,6 @@ void DetectEngineCtxFree(DetectEngineCtx *de_ctx)
SCClassConfDeInitContext(de_ctx); SCClassConfDeInitContext(de_ctx);
SCRConfDeInitContext(de_ctx); SCRConfDeInitContext(de_ctx);
DetectBufferTypeFreeDetectEngine(de_ctx);
SigGroupCleanup(de_ctx); SigGroupCleanup(de_ctx);
@ -1659,9 +1661,10 @@ void DetectEngineCtxFree(DetectEngineCtx *de_ctx)
#endif #endif
} }
DetectPortCleanupList(de_ctx->tcp_whitelist); DetectPortCleanupList(de_ctx, de_ctx->tcp_whitelist);
DetectPortCleanupList(de_ctx->udp_whitelist); DetectPortCleanupList(de_ctx, de_ctx->udp_whitelist);
DetectBufferTypeFreeDetectEngine(de_ctx);
/* freed our var name hash */ /* freed our var name hash */
VarNameStoreFree(de_ctx->version); VarNameStoreFree(de_ctx->version);
@ -1911,7 +1914,7 @@ static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx)
if (x->port != x->port2) { if (x->port != x->port2) {
SCLogWarning(SC_ERR_INVALID_YAML_CONF_ENTRY, "'%s' is not a valid value " SCLogWarning(SC_ERR_INVALID_YAML_CONF_ENTRY, "'%s' is not a valid value "
"for detect.grouping.tcp-whitelist: only single ports allowed", ports); "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; de_ctx->tcp_whitelist = NULL;
break; break;
} }
@ -1934,7 +1937,7 @@ static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx)
if (x->port != x->port2) { if (x->port != x->port2) {
SCLogWarning(SC_ERR_INVALID_YAML_CONF_ENTRY, "'%s' is not a valid value " SCLogWarning(SC_ERR_INVALID_YAML_CONF_ENTRY, "'%s' is not a valid value "
"for detect.grouping.udp-whitelist: only single ports allowed", ports); "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; de_ctx->udp_whitelist = NULL;
break; break;
} }

@ -1319,10 +1319,10 @@ void SigFree(Signature *s)
} }
if (s->sp != NULL) { if (s->sp != NULL) {
DetectPortCleanupList(s->sp); DetectPortCleanupList(NULL, s->sp);
} }
if (s->dp != NULL) { if (s->dp != NULL) {
DetectPortCleanupList(s->dp); DetectPortCleanupList(NULL, s->dp);
} }
if (s->msg != NULL) if (s->msg != NULL)
@ -2317,9 +2317,12 @@ static int SigParseTest02 (void)
} }
end: end:
if (port != NULL) DetectPortCleanupList(port); if (port != NULL)
if (sig != NULL) SigFree(sig); DetectPortCleanupList(de_ctx, port);
if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); if (sig != NULL)
SigFree(sig);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
return result; return result;
} }

@ -866,6 +866,9 @@ typedef struct DetectEngineCtx_ {
DetectMpmAppLayerRegistery *app_mpms_list; DetectMpmAppLayerRegistery *app_mpms_list;
uint32_t app_mpms_list_cnt; uint32_t app_mpms_list_cnt;
uint32_t prefilter_id;
HashListTable *prefilter_hash_table;
/** table with mpms and their registration function /** table with mpms and their registration function
* \todo we only need this at init, so perhaps this * \todo we only need this at init, so perhaps this
* can move to a DetectEngineCtx 'init' struct */ * can move to a DetectEngineCtx 'init' struct */

@ -40,9 +40,6 @@
#ifdef PROFILING #ifdef PROFILING
extern uint32_t g_prefilter_id;
extern HashListTable *g_prefilter_hash_table;
typedef struct SCProfilePrefilterData_ { typedef struct SCProfilePrefilterData_ {
uint64_t called; uint64_t called;
uint64_t total; uint64_t total;
@ -186,7 +183,7 @@ void
SCProfilingPrefilterUpdateCounter(DetectEngineThreadCtx *det_ctx, int id, uint64_t ticks) SCProfilingPrefilterUpdateCounter(DetectEngineThreadCtx *det_ctx, int id, uint64_t ticks)
{ {
if (det_ctx != NULL && det_ctx->prefilter_perf_data != NULL && 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]; SCProfilePrefilterData *p = &det_ctx->prefilter_perf_data[id];
@ -237,7 +234,7 @@ void SCProfilingPrefilterThreadSetup(SCProfilePrefilterDetectCtx *ctx, DetectEng
if (ctx == NULL) if (ctx == NULL)
return; return;
const uint32_t size = g_prefilter_id; const uint32_t size = det_ctx->de_ctx->prefilter_id;
SCProfilePrefilterData *a = SCMalloc(sizeof(SCProfilePrefilterData) * size); SCProfilePrefilterData *a = SCMalloc(sizeof(SCProfilePrefilterData) * size);
if (a != NULL) { if (a != NULL) {
@ -253,7 +250,7 @@ static void SCProfilingPrefilterThreadMerge(DetectEngineCtx *de_ctx, DetectEngin
det_ctx->prefilter_perf_data == NULL) det_ctx->prefilter_perf_data == NULL)
return; 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].called += det_ctx->prefilter_perf_data[i].called;
de_ctx->profile_prefilter_ctx->data[i].total += det_ctx->prefilter_perf_data[i].total; 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) 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) if (profiling_prefilter_enabled == 0)
return; return;
const uint32_t size = g_prefilter_id; const uint32_t size = de_ctx->prefilter_id;
if (g_prefilter_id == 0) if (size == 0)
return; return;
de_ctx->profile_prefilter_ctx = SCProfilingPrefilterInitCtx(); de_ctx->profile_prefilter_ctx = SCProfilingPrefilterInitCtx();
@ -297,7 +294,7 @@ SCProfilingPrefilterInitCounters(DetectEngineCtx *de_ctx)
BUG_ON(de_ctx->profile_prefilter_ctx->data == NULL); BUG_ON(de_ctx->profile_prefilter_ctx->data == NULL);
memset(de_ctx->profile_prefilter_ctx->data, 0x00, sizeof(SCProfilePrefilterData) * size); 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)) { for ( ; hb != NULL; hb = HashListTableGetListNext(hb)) {
PrefilterStore *ctx = HashListTableGetListData(hb); PrefilterStore *ctx = HashListTableGetListData(hb);
de_ctx->profile_prefilter_ctx->data[ctx->id].name = ctx->name; de_ctx->profile_prefilter_ctx->data[ctx->id].name = ctx->name;

Loading…
Cancel
Save