]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
mpm: clean up builtin mpm setup, enable single/full
authorVictor Julien <victor@inliniac.net>
Wed, 28 Oct 2015 20:47:37 +0000 (21:47 +0100)
committerVictor Julien <victor@inliniac.net>
Tue, 5 Apr 2016 07:37:42 +0000 (09:37 +0200)
src/detect-engine-mpm.c
src/detect-engine-mpm.h
src/detect.c

index 179085603f70478a36205ead28a373a909f5d84b..89cc643b90b4f9b849864211bb244669cf2e53b5 100644 (file)
@@ -163,6 +163,88 @@ void DetectMpmPrepareAppMpms(DetectEngineCtx *de_ctx)
     }
 }
 
+static int32_t SetupBuiltinMpm(DetectEngineCtx *de_ctx, const char *name)
+{
+    /* default to whatever the global setting is */
+    int shared = (de_ctx->sgh_mpm_context == ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE);
+
+    /* see if we use a unique or shared mpm ctx for this type */
+    int confshared = 0;
+    char confstring[256] = "detect.mpm.";
+    strlcat(confstring, name, sizeof(confstring));
+    strlcat(confstring, ".shared", sizeof(confstring));
+    if (ConfGetBool(confstring, &confshared) == 1)
+        shared = confshared;
+
+    int32_t ctx;
+    if (shared == 0) {
+        ctx = MPM_CTX_FACTORY_UNIQUE_CONTEXT;
+        SCLogInfo("using unique mpm ctx' for %s", name);
+    } else {
+        ctx = MpmFactoryRegisterMpmCtxProfile(de_ctx, name);
+        SCLogInfo("using shared mpm ctx' for %s", name);
+    }
+    return ctx;
+}
+
+void DetectMpmInitializeBuiltinMpms(DetectEngineCtx *de_ctx)
+{
+    de_ctx->sgh_mpm_context_proto_tcp_packet = SetupBuiltinMpm(de_ctx, "tcp-packet");
+    de_ctx->sgh_mpm_context_stream = SetupBuiltinMpm(de_ctx, "tcp-stream");
+
+    de_ctx->sgh_mpm_context_proto_udp_packet = SetupBuiltinMpm(de_ctx, "udp-packet");
+    de_ctx->sgh_mpm_context_proto_other_packet = SetupBuiltinMpm(de_ctx, "other-ip");
+}
+
+/**
+ *  \brief initialize mpm contexts for builtin buffers that are in
+ *         "single or "shared" mode.
+ */
+void DetectMpmPrepareBuiltinMpms(DetectEngineCtx *de_ctx)
+{
+    MpmCtx *mpm_ctx = NULL;
+
+    if (de_ctx->sgh_mpm_context_proto_tcp_packet != MPM_CTX_FACTORY_UNIQUE_CONTEXT) {
+        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_tcp_packet, 0);
+        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
+            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
+        }
+        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_tcp_packet, 1);
+        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
+            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
+        }
+    }
+
+    if (de_ctx->sgh_mpm_context_proto_udp_packet != MPM_CTX_FACTORY_UNIQUE_CONTEXT) {
+        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_udp_packet, 0);
+        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
+            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
+        }
+        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_udp_packet, 1);
+        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
+            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
+        }
+    }
+
+    if (de_ctx->sgh_mpm_context_proto_other_packet != MPM_CTX_FACTORY_UNIQUE_CONTEXT) {
+        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_other_packet, 0);
+        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
+            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
+        }
+    }
+
+    if (de_ctx->sgh_mpm_context_stream != MPM_CTX_FACTORY_UNIQUE_CONTEXT) {
+        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_stream, 0);
+        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
+            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
+        }
+        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_stream, 1);
+        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
+            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
+        }
+    }
+}
+
 /**
  *  \brief check if a signature has patterns that are to be inspected
  *         against a packets payload (as opposed to the stream payload)
index 4bb7fe31a53c758cd6dc5b47864cebc362625998..5e83a2553987a03c8dafd5c650f0809f0465bc73 100644 (file)
 
 void DetectMpmInitializeAppMpms(DetectEngineCtx *de_ctx);
 void DetectMpmPrepareAppMpms(DetectEngineCtx *de_ctx);
-
-uint16_t PatternMatchDefaultMatcher(void);
+void DetectMpmInitializeBuiltinMpms(DetectEngineCtx *de_ctx);
+void DetectMpmPrepareBuiltinMpms(DetectEngineCtx *de_ctx);
 
 uint32_t PatternStrength(uint8_t *, uint16_t);
+
+uint16_t PatternMatchDefaultMatcher(void);
 uint32_t PacketPatternSearchWithStreamCtx(DetectEngineThreadCtx *, Packet *);
 uint32_t PacketPatternSearch(DetectEngineThreadCtx *, Packet *);
 uint32_t StreamPatternSearch(DetectEngineThreadCtx *, Packet *, StreamMsg *, uint8_t);
index a322bfc75561a1f10ee683b1659db722f7c9aaee..fd99ed761ae7e03768eb3c171bb1d6cea6ee0621 100644 (file)
@@ -2730,22 +2730,7 @@ static int SignatureCreateMask(Signature *s)
 
 static void SigInitStandardMpmFactoryContexts(DetectEngineCtx *de_ctx)
 {
-    if (de_ctx->sgh_mpm_context == ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE) {
-        de_ctx->sgh_mpm_context_proto_tcp_packet =
-            MpmFactoryRegisterMpmCtxProfile(de_ctx, "packet_proto_tcp");
-        de_ctx->sgh_mpm_context_proto_udp_packet =
-            MpmFactoryRegisterMpmCtxProfile(de_ctx, "packet_proto_udp");
-        de_ctx->sgh_mpm_context_proto_other_packet =
-            MpmFactoryRegisterMpmCtxProfile(de_ctx, "packet_proto_other");
-        de_ctx->sgh_mpm_context_stream =
-            MpmFactoryRegisterMpmCtxProfile(de_ctx, "stream");
-    } else {
-        de_ctx->sgh_mpm_context_proto_tcp_packet = MPM_CTX_FACTORY_UNIQUE_CONTEXT;
-        de_ctx->sgh_mpm_context_proto_udp_packet = MPM_CTX_FACTORY_UNIQUE_CONTEXT;
-        de_ctx->sgh_mpm_context_proto_other_packet = MPM_CTX_FACTORY_UNIQUE_CONTEXT;
-        de_ctx->sgh_mpm_context_stream = MPM_CTX_FACTORY_UNIQUE_CONTEXT;
-    }
-
+    DetectMpmInitializeBuiltinMpms(de_ctx);
     DetectMpmInitializeAppMpms(de_ctx);
 
     return;
@@ -4141,8 +4126,6 @@ int SigGroupBuild(DetectEngineCtx *de_ctx)
     }
 
     if (de_ctx->sgh_mpm_context == ENGINE_SGH_MPM_FACTORY_CONTEXT_SINGLE) {
-        MpmCtx *mpm_ctx = NULL;
-
 #ifdef __SC_CUDA_SUPPORT__
         if (PatternMatchDefaultMatcher() == MPM_AC_CUDA) {
             /* setting it to default.  You've gotta remove it once you fix the state table thing */
@@ -4161,42 +4144,6 @@ int SigGroupBuild(DetectEngineCtx *de_ctx)
             }
         }
 #endif
-        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_tcp_packet, 0);
-        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
-            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
-        }
-        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_tcp_packet, 1);
-        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
-            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
-        }
-        //printf("packet- %d\n", mpm_ctx->pattern_cnt);
-
-        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_udp_packet, 0);
-        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
-            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
-        }
-        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_udp_packet, 1);
-        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
-            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
-        }
-        //printf("packet- %d\n", mpm_ctx->pattern_cnt);
-
-        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_proto_other_packet, 0);
-        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
-            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
-        }
-        //printf("packet- %d\n", mpm_ctx->pattern_cnt);
-
-        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_stream, 0);
-        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
-            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
-        }
-        mpm_ctx = MpmFactoryGetMpmCtxForProfile(de_ctx, de_ctx->sgh_mpm_context_stream, 1);
-        if (mpm_table[de_ctx->mpm_matcher].Prepare != NULL) {
-            mpm_table[de_ctx->mpm_matcher].Prepare(mpm_ctx);
-        }
-        //printf("stream- %d\n", mpm_ctx->pattern_cnt);
-
 #ifdef __SC_CUDA_SUPPORT__
         if (PatternMatchDefaultMatcher() == MPM_AC_CUDA) {
             int r = SCCudaCtxPopCurrent(NULL);
@@ -4211,6 +4158,7 @@ int SigGroupBuild(DetectEngineCtx *de_ctx)
         DetermineCudaStateTableSize(de_ctx);
 #endif
     }
+    DetectMpmPrepareBuiltinMpms(de_ctx);
     DetectMpmPrepareAppMpms(de_ctx);
 
 //    DetectAddressPrintMemory();