]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect: added support for protocol-aliases
authorfrank honza <frank.honza@dcso.de>
Sun, 18 Oct 2020 20:00:48 +0000 (22:00 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 5 Mar 2021 13:47:10 +0000 (14:47 +0100)
rust/src/applayer.rs
src/app-layer-detect-proto.c
src/app-layer-detect-proto.h
src/app-layer-register.c
src/app-layer-register.h

index 35bb1865085341398775cd8d35b89a39d9008088..31c18c77af263526c4de94818e17fd30c1644618 100644 (file)
@@ -290,6 +290,7 @@ pub type TruncateFn = unsafe extern "C" fn (*mut c_void, u8);
 // Defined in app-layer-register.h
 extern {
     pub fn AppLayerRegisterProtocolDetection(parser: *const RustParser, enable_default: c_int) -> AppProto;
+    pub fn AppLayerRegisterParserAlias(parser_name: *const c_char, alias_name: *const c_char);
 }
 
 #[allow(non_snake_case)]
index 91080a15d43ca122096375ef088648c55bc4a05d..41ff5673d59481d7acef1fe5fae3ae04323af124 100644 (file)
@@ -163,6 +163,12 @@ typedef struct AppLayerProtoDetectCtx_ {
     const char *alproto_names[ALPROTO_MAX];
 } AppLayerProtoDetectCtx;
 
+typedef struct AppLayerProtoDetectAliases_ {
+    const char *proto_name;
+    const char *proto_alias;
+    struct AppLayerProtoDetectAliases_ *next;
+} AppLayerProtoDetectAliases;
+
 /**
  * \brief The app layer protocol detection thread context.
  */
@@ -175,6 +181,7 @@ struct AppLayerProtoDetectThreadCtx_ {
 
 /* The global app layer proto detection context. */
 static AppLayerProtoDetectCtx alpd_ctx;
+static AppLayerProtoDetectAliases *alpda_ctx = NULL;
 
 static void AppLayerProtoDetectPEGetIpprotos(AppProto alproto,
                                              uint8_t *ipprotos);
@@ -1600,6 +1607,27 @@ static void AppLayerProtoDetectFreeProbingParsers(AppLayerProtoDetectProbingPars
     SCReturn;
 }
 
+static void AppLayerProtoDetectFreeAliases(void)
+{
+    SCEnter();
+
+    AppLayerProtoDetectAliases *cur_alias = alpda_ctx;
+    if (cur_alias == NULL)
+        goto end;
+
+    AppLayerProtoDetectAliases *next_alias = NULL;
+    while (cur_alias != NULL) {
+        next_alias = cur_alias->next;
+        SCFree(cur_alias);
+        cur_alias = next_alias;
+    }
+
+    alpda_ctx = NULL;
+
+end:
+    SCReturn;
+}
+
 /***** State Preparation *****/
 
 int AppLayerProtoDetectPrepareState(void)
@@ -1851,6 +1879,8 @@ int AppLayerProtoDetectDeSetup(void)
 
     SpmDestroyGlobalThreadCtx(alpd_ctx.spm_global_thread_ctx);
 
+    AppLayerProtoDetectFreeAliases();
+
     AppLayerProtoDetectFreeProbingParsers(alpd_ctx.ctx_pp);
 
     SCReturnInt(0);
@@ -1866,6 +1896,32 @@ void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_n
     SCReturn;
 }
 
+void AppLayerProtoDetectRegisterAlias(const char *proto_name, const char *proto_alias)
+{
+    SCEnter();
+
+    AppLayerProtoDetectAliases *new_alias = SCMalloc(sizeof(AppLayerProtoDetectAliases));
+    if (unlikely(new_alias == NULL)) {
+        exit(EXIT_FAILURE);
+    }
+
+    new_alias->proto_name = proto_name;
+    new_alias->proto_alias = proto_alias;
+    new_alias->next = NULL;
+
+    if (alpda_ctx == NULL) {
+        alpda_ctx = new_alias;
+    } else {
+        AppLayerProtoDetectAliases *cur_alias = alpda_ctx;
+        while (cur_alias->next != NULL) {
+            cur_alias = cur_alias->next;
+        }
+        cur_alias->next = new_alias;
+    }
+
+    SCReturn;
+}
+
 /** \brief request applayer to wrap up this protocol and rerun protocol
  *         detection.
  *
@@ -2090,6 +2146,15 @@ AppProto AppLayerProtoDetectGetProtoByName(const char *alproto_name)
 {
     SCEnter();
 
+    AppLayerProtoDetectAliases *cur_alias = alpda_ctx;
+    while (cur_alias != NULL) {
+        if (strcasecmp(alproto_name, cur_alias->proto_alias) == 0) {
+            alproto_name = cur_alias->proto_name;
+        }
+
+        cur_alias = cur_alias->next;
+    }
+
     AppProto a;
     AppProto b = StringToAppProto(alproto_name);
     for (a = 0; a < ALPROTO_MAX; a++) {
index 4a29919739ccfc780240485b03c87b675d8486fe..7f3fef0bd609526a57ccb3032bf32af43c0b93b9 100644 (file)
@@ -148,6 +148,8 @@ int AppLayerProtoDetectDeSetup(void);
  */
 void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name);
 
+void AppLayerProtoDetectRegisterAlias(const char *proto_name, const char *proto_alias);
+
 /**
  * \brief Given a protocol name, checks if proto detection is enabled in
  *        the conf file.
index 5cef99e7d2102da3ea87eb5d4e0a4b5b1a8f5eae..dcf82aa8b76b5ba82898db6a3199eb89b6dfc876 100644 (file)
@@ -189,6 +189,13 @@ int AppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto)
     return 0;
 }
 
+int AppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias)
+{
+    AppLayerProtoDetectRegisterAlias(proto_name, proto_alias);
+
+    return 0;
+}
+
 static const char * IpProtoToString(int ip_proto)
 {
     switch (ip_proto) {
index 6f317cca6562a027122fa5a328437ec50dbff0b4..ee58dc3b97f989022f66f95a5603eda473d3f838 100644 (file)
@@ -96,4 +96,6 @@ AppProto AppLayerRegisterProtocolDetection(const struct AppLayerParser *parser,
  */
 int AppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto);
 
+int AppLayerRegisterParserAlias(const char *proto_name, const char *proto_alias);
+
 #endif /* __APP_LAYER_REGISTER_H__ */