detect: move mpm engines into detect engine ctx

This allows safe registration at runtime.
pull/3246/head
Victor Julien 8 years ago
parent ad16925bc9
commit efbd901385

@ -554,7 +554,7 @@ static int SignatureCreateMask(Signature *s)
static void SigInitStandardMpmFactoryContexts(DetectEngineCtx *de_ctx)
{
DetectMpmInitializeBuiltinMpms(de_ctx);
DetectMpmInitializeAppMpms(de_ctx);
DetectMpmSetupAppMpms(de_ctx);
return;
}

@ -183,12 +183,14 @@ void DetectAppLayerMpmRegister(const char *name,
SupportFastPatternForSigMatchList(sm_list, priority);
}
void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
/** \brief copy a mpm engine from parent_id, add in transforms */
void DetectAppLayerMpmRegisterByParentId(DetectEngineCtx *de_ctx,
const int id, const int parent_id,
DetectEngineTransforms *transforms)
{
SCLogDebug("registering %d/%d", id, parent_id);
DetectMpmAppLayerRegistery *t = g_app_mpms_list;
DetectMpmAppLayerRegistery *t = de_ctx->app_mpms_list;
while (t) {
if (t->sm_list == parent_id) {
DetectMpmAppLayerRegistery *am = SCCalloc(1, sizeof(*am));
@ -196,7 +198,7 @@ void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
am->name = t->name;
snprintf(am->pname, sizeof(am->pname), "%s#%d", am->name, id);
am->direction = t->direction;
am->sm_list = id; // use new id
am->sm_list = id; // use new id
am->PrefilterRegister = t->PrefilterRegister;
am->v2.PrefilterRegisterWithListId = t->v2.PrefilterRegisterWithListId;
am->v2.GetData = t->v2.GetData;
@ -207,11 +209,12 @@ void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
if (transforms) {
memcpy(&am->v2.transforms, transforms, sizeof(*transforms));
}
am->id = g_app_mpms_list_cnt++;
am->id = de_ctx->app_mpms_list_cnt++;
SupportFastPatternForSigMatchList(am->sm_list, am->priority);
t->next = am;
SCLogDebug("copied mpm registration for %s id %u with parent %u and GetData %p",
SCLogDebug("copied mpm registration for %s id %u "
"with parent %u and GetData %p",
t->name, id, parent_id, am->v2.GetData);
t = am;
}
@ -221,12 +224,40 @@ void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx)
{
BUG_ON(g_app_mpms_list_cnt == 0);
const DetectMpmAppLayerRegistery *list = g_app_mpms_list;
while (list != NULL) {
DetectMpmAppLayerRegistery *n = SCCalloc(1, sizeof(*n));
BUG_ON(n == NULL);
*n = *list;
n->next = NULL;
if (de_ctx->app_mpms_list == NULL) {
de_ctx->app_mpms_list = n;
} else {
DetectMpmAppLayerRegistery *t = de_ctx->app_mpms_list;
while (t->next != NULL) {
t = t->next;
}
t->next = n;
}
list = list->next;
}
de_ctx->app_mpms_list_cnt = g_app_mpms_list_cnt;
SCLogDebug("mpm: de_ctx app_mpms_list %p %u",
de_ctx->app_mpms_list, de_ctx->app_mpms_list_cnt);
}
void DetectMpmSetupAppMpms(DetectEngineCtx *de_ctx)
{
BUG_ON(de_ctx->app_mpms_list_cnt == 0);
de_ctx->app_mpms = SCCalloc(g_app_mpms_list_cnt + 1, sizeof(DetectMpmAppLayerKeyword));
de_ctx->app_mpms = SCCalloc(de_ctx->app_mpms_list_cnt + 1, sizeof(DetectMpmAppLayerKeyword));
BUG_ON(de_ctx->app_mpms == NULL);
DetectMpmAppLayerRegistery *list = g_app_mpms_list;
DetectMpmAppLayerRegistery *list = de_ctx->app_mpms_list;
while (list != NULL) {
DetectMpmAppLayerKeyword *am = &de_ctx->app_mpms[list->id];
am->reg = list;

@ -33,6 +33,7 @@
#include "stream.h"
void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx);
void DetectMpmSetupAppMpms(DetectEngineCtx *de_ctx);
int DetectMpmPrepareAppMpms(DetectEngineCtx *de_ctx);
void DetectMpmInitializeBuiltinMpms(DetectEngineCtx *de_ctx);
int DetectMpmPrepareBuiltinMpms(DetectEngineCtx *de_ctx);
@ -95,7 +96,9 @@ void DetectAppLayerMpmRegister2(const char *name,
const DetectMpmAppLayerRegistery *mpm_reg, int list_id),
InspectionBufferGetDataPtr GetData,
AppProto alproto, int tx_min_progress);
void DetectAppLayerMpmRegisterByParentId(const int id, const int parent_id,
void DetectAppLayerMpmRegisterByParentId(
DetectEngineCtx *de_ctx,
const int id, const int parent_id,
DetectEngineTransforms *transforms);
#endif /* __DETECT_ENGINE_MPM_H__ */

@ -940,6 +940,7 @@ static void DetectBufferTypeSetupDetectEngine(DetectEngineCtx *de_ctx)
}
de_ctx->buffer_type_id = g_buffer_type_id;
DetectMpmInitializeAppMpms(de_ctx);
DetectAppLayerInspectEngineCopyListToDetectCtx(de_ctx);
}
@ -950,6 +951,19 @@ static void DetectBufferTypeFreeDetectEngine(DetectEngineCtx *de_ctx)
SCFree(de_ctx->buffer_type_map);
if (de_ctx->buffer_type_hash)
HashListTableFree(de_ctx->buffer_type_hash);
DetectEngineAppInspectionEngine *ilist = de_ctx->app_inspect_engines;
while (ilist) {
DetectEngineAppInspectionEngine *next = ilist->next;
SCFree(ilist);
ilist = next;
}
DetectMpmAppLayerRegistery *mlist = de_ctx->app_mpms_list;
while (mlist) {
DetectMpmAppLayerRegistery *next = mlist->next;
SCFree(mlist);
mlist = next;
}
}
}
@ -999,7 +1013,8 @@ int DetectBufferTypeGetByIdTransforms(DetectEngineCtx *de_ctx, const int id,
map->mpm = base_map->mpm;
map->SetupCallback = base_map->SetupCallback;
map->ValidateCallback = base_map->ValidateCallback;
DetectAppLayerMpmRegisterByParentId(map->id, map->parent_id, &map->transforms);
DetectAppLayerMpmRegisterByParentId(de_ctx,
map->id, map->parent_id, &map->transforms);
BUG_ON(HashListTableAdd(de_ctx->buffer_type_hash, (void *)map, 0) != 0);
SCLogDebug("buffer %s registered with id %d, parent %d", map->string, map->id, map->parent_id);

@ -861,6 +861,8 @@ typedef struct DetectEngineCtx_ {
/* list with app inspect engines. Both the start-time registered ones and
* the rule-time registered ones. */
DetectEngineAppInspectionEngine *app_inspect_engines;
DetectMpmAppLayerRegistery *app_mpms_list;
uint32_t app_mpms_list_cnt;
/** table with mpms and their registration function
* \todo we only need this at init, so perhaps this

Loading…
Cancel
Save