]> git.ipfire.org Git - people/ms/suricata.git/commitdiff
app-layer: add ApplyTxConfig API
authorVictor Julien <victor@inliniac.net>
Sun, 7 Jun 2020 11:42:09 +0000 (13:42 +0200)
committerVictor Julien <victor@inliniac.net>
Sat, 11 Jul 2020 06:37:40 +0000 (08:37 +0200)
Optional callback a parser can register for applying configuration
to the 'transaction'. Most parsers have a bidirectional tx. For those
parsers that have different types of transaction handling, this new
callback can be used to properly apply the config.

16 files changed:
rust/src/applayer.rs
rust/src/applayertemplate/template.rs
rust/src/dhcp/dhcp.rs
rust/src/dns/dns.rs
rust/src/ikev2/ikev2.rs
rust/src/krb/krb5.rs
rust/src/ntp/ntp.rs
rust/src/rdp/rdp.rs
rust/src/rfb/rfb.rs
rust/src/sip/sip.rs
rust/src/snmp/snmp.rs
rust/src/ssh/ssh.rs
src/app-layer-parser.c
src/app-layer-parser.h
src/app-layer-register.c
src/app-layer-register.h

index 1ed11b36e1b5e625fcf5544ee713b462566d6a8a..0feab3ebe0050f9d3e5085335b5dfd6e588ea55e 100644 (file)
@@ -234,6 +234,12 @@ pub struct RustParser {
     pub get_tx_detect_flags: Option<GetTxDetectFlagsFn>,
 
     pub get_tx_data: Option<GetTxDataFn>,
+
+    // Function to apply config to a TX. Optional. Normal (bidirectional)
+    // transactions don't need to set this. It is meant for cases where
+    // the requests and responses are not sharing tx. It is then up to
+    // the implementation to make sure the config is applied correctly.
+    pub apply_tx_config: Option<ApplyTxConfigFn>,
 }
 
 /// Create a slice, given a buffer and a length
@@ -286,6 +292,7 @@ pub type GetTxIteratorFn    = extern "C" fn (ipproto: u8, alproto: AppProto,
 pub type GetTxDetectFlagsFn = unsafe extern "C" fn(*mut c_void, u8) -> u64;
 pub type SetTxDetectFlagsFn = unsafe extern "C" fn(*mut c_void, u8, u64);
 pub type GetTxDataFn = unsafe extern "C" fn(*mut c_void) -> *mut AppLayerTxData;
+pub type ApplyTxConfigFn = unsafe extern "C" fn (*mut c_void, *mut c_void, c_int, AppLayerTxConfig);
 
 // Defined in app-layer-register.h
 extern {
index 6b241bfbdfef85b769b98fc90c89f4a4435a886e..f59e474816ba7514e1d28794d918fc9bf4568333 100644 (file)
@@ -538,6 +538,7 @@ pub unsafe extern "C" fn rs_template_register_parser() {
         get_tx_detect_flags: None,
         set_tx_detect_flags: None,
         get_tx_data: None,
+        apply_tx_config: None,
     };
 
     let ip_proto_str = CString::new("tcp").unwrap();
index d38cf932e1f477206bd9c46c299c585e42fb836f..094565eebf1f3f7c6194b50cb4726f772d57cff3 100644 (file)
@@ -452,6 +452,7 @@ pub unsafe extern "C" fn rs_dhcp_register_parser() {
         set_tx_detect_flags: None,
         get_tx_detect_flags: None,
         get_tx_data        : None,
+        apply_tx_config    : None,
     };
 
     let ip_proto_str = CString::new("udp").unwrap();
index c95d9df3ca928f75a7098e2b6c1314616ca62d3c..72d2e37593a6fdd200dc3bed951330c368db7388 100644 (file)
@@ -1033,6 +1033,7 @@ pub unsafe extern "C" fn rs_dns_udp_register_parser() {
         get_de_state: rs_dns_state_get_tx_detect_state,
         set_de_state: rs_dns_state_set_tx_detect_state,
         get_tx_data: Some(rs_dns_state_get_tx_data),
+        apply_tx_config: None,
     };
 
     let ip_proto_str = CString::new("udp").unwrap();
@@ -1079,6 +1080,7 @@ pub unsafe extern "C" fn rs_dns_tcp_register_parser() {
         get_de_state: rs_dns_state_get_tx_detect_state,
         set_de_state: rs_dns_state_set_tx_detect_state,
         get_tx_data: Some(rs_dns_state_get_tx_data),
+        apply_tx_config: None,
     };
 
     let ip_proto_str = CString::new("tcp").unwrap();
index 86ac3a778ff709c961e6a8337c86f837fa310369..fae04be1d5bbcf41e71837a74fb5535120a50244 100644 (file)
@@ -736,6 +736,7 @@ pub unsafe extern "C" fn rs_register_ikev2_parser() {
         get_tx_detect_flags: None,
         set_tx_detect_flags: None,
         get_tx_data        : None,
+        apply_tx_config    : None,
     };
 
     let ip_proto_str = CString::new("udp").unwrap();
index 9602a0a4def8ea18edb9be3fde4887c8cd31fb98..b52cc93d388b048c93a2741c18d56525604f32d5 100644 (file)
@@ -683,6 +683,7 @@ pub unsafe extern "C" fn rs_register_krb5_parser() {
         get_tx_detect_flags: Some(rs_krb5_tx_detect_flags_get),
         set_tx_detect_flags: Some(rs_krb5_tx_detect_flags_set),
         get_tx_data        : None,
+        apply_tx_config    : None,
     };
     // register UDP parser
     let ip_proto_str = CString::new("udp").unwrap();
index 4a73f9e1c7d445ff631dcc0f738662334a5de361..71d8277f4287ca786f26cb801225506b9bd9a289 100644 (file)
@@ -435,6 +435,7 @@ pub unsafe extern "C" fn rs_register_ntp_parser() {
         get_tx_detect_flags: None,
         set_tx_detect_flags: None,
         get_tx_data        : None,
+        apply_tx_config    : None,
     };
 
     let ip_proto_str = CString::new("udp").unwrap();
index d4a4d327da0171d1b083ba45a67c59365c462dff..25a678e2061504924b3fe8f8168d972d920ca474 100644 (file)
@@ -533,6 +533,7 @@ pub unsafe extern "C" fn rs_rdp_register_parser() {
         get_tx_detect_flags: None,
         set_tx_detect_flags: None,
         get_tx_data: None,
+        apply_tx_config: None,
     };
 
     let ip_proto_str = std::ffi::CString::new("tcp").unwrap();
index 5abbf7b6ca616e50907c60e90731fbccedb3d289..85cb1982fa92de4fc32d06328d8b831eb4347fca 100644 (file)
@@ -725,6 +725,7 @@ pub unsafe extern "C" fn rs_rfb_register_parser() {
         get_tx_detect_flags: Some(rs_rfb_get_tx_detect_flags),
         set_tx_detect_flags: Some(rs_rfb_set_tx_detect_flags),
         get_tx_data: None,
+        apply_tx_config: None,
     };
 
     let ip_proto_str = CString::new("tcp").unwrap();
index fb3b21a6495b808bcbf60f1c14a19d3e1fc5055d..00be0a8bcc8f98a9a34a2e38de41a1bbd6078b4c 100755 (executable)
@@ -413,6 +413,7 @@ pub unsafe extern "C" fn rs_sip_register_parser() {
         get_tx_detect_flags: None,
         set_tx_detect_flags: None,
         get_tx_data: None,
+        apply_tx_config: None,
     };
 
     let ip_proto_str = CString::new("udp").unwrap();
index 5eb45a9b7eef2734673e3ff8a08ebee36f00a394..4d11d9e8717d9a35c164faf2d0db5cd42e88146b 100644 (file)
@@ -615,6 +615,7 @@ pub unsafe extern "C" fn rs_register_snmp_parser() {
         get_tx_detect_flags: Some(rs_snmp_get_tx_detect_flags),
         set_tx_detect_flags: Some(rs_snmp_set_tx_detect_flags),
         get_tx_data        : None,
+        apply_tx_config    : None,
     };
     let ip_proto_str = CString::new("udp").unwrap();
     if AppLayerProtoDetectConfProtoDetectionEnabled(ip_proto_str.as_ptr(), parser.name) != 0 {
index 7d20b04cd6fbe203cfd6faa7816ae1b59b4c37fd..5c5545b4376df95f05a72aae2a3c272d4a6a0c92 100644 (file)
@@ -582,6 +582,7 @@ pub unsafe extern "C" fn rs_ssh_register_parser() {
         get_tx_detect_flags: Some(rs_ssh_get_tx_detect_flags),
         set_tx_detect_flags: Some(rs_ssh_set_tx_detect_flags),
         get_tx_data: None,
+        apply_tx_config: None,
     };
 
     let ip_proto_str = CString::new("tcp").unwrap();
index e96c827dab5cf463150c26bd85dbcbe9ff02abe6..fd6b24c6ab481e20ee770b7243386f9f930e784d 100644 (file)
@@ -126,6 +126,7 @@ typedef struct AppLayerParserProtoCtx_
     uint64_t (*GetTxDetectFlags)(void *tx, uint8_t dir);
     void (*SetTxDetectFlags)(void *tx, uint8_t dir, uint64_t);
     AppLayerTxData *(*GetTxData)(void *tx);
+    bool (*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig);
 
     void (*SetStreamDepthFlag)(void *tx, uint8_t flags);
 
@@ -605,6 +606,16 @@ void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto,
     SCReturn;
 }
 
+void AppLayerParserRegisterApplyTxConfigFunc(uint8_t ipproto, AppProto alproto,
+        bool (*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig))
+{
+    SCEnter();
+
+    alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].ApplyTxConfig = ApplyTxConfig;
+
+    SCReturn;
+}
+
 void AppLayerParserRegisterSetStreamDepthFlag(uint8_t ipproto, AppProto alproto,
         void (*SetStreamDepthFlag)(void *tx, uint8_t flags))
 {
@@ -1219,6 +1230,16 @@ AppLayerTxData *AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void
     SCReturnPtr(NULL, "AppLayerTxData");
 }
 
+void AppLayerParserApplyTxConfig(uint8_t ipproto, AppProto alproto,
+        void *state, void *tx, enum ConfigAction mode, AppLayerTxConfig config)
+{
+    SCEnter();
+    if (alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].ApplyTxConfig) {
+        alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].ApplyTxConfig(state, tx, mode, config);
+    }
+    SCReturn;
+}
+
 /***** General *****/
 
 /** \retval int -1 in case of unrecoverable error. App-layer tracking stops for this flow.
index 7b7e44e13f5643e4d7b9b296ebe311bb01d7ed8c..733a819324d803b72a0803a8d32299339bb9f22f 100644 (file)
@@ -30,6 +30,7 @@
 #include "util-file.h"
 #include "stream-tcp-private.h"
 #include "rust.h"
+#include "util-config.h"
 
 /* Flags for AppLayerParserState. */
 #define APP_LAYER_PARSER_EOF                    BIT_U8(0)
@@ -189,6 +190,8 @@ void AppLayerParserRegisterSetStreamDepthFlag(uint8_t ipproto, AppProto alproto,
 
 void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto,
         AppLayerTxData *(*GetTxData)(void *tx));
+void AppLayerParserRegisterApplyTxConfigFunc(uint8_t ipproto, AppProto alproto,
+        bool (*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig));
 
 /***** Get and transaction functions *****/
 
@@ -240,6 +243,8 @@ void AppLayerParserSetTxDetectFlags(uint8_t ipproto, AppProto alproto, void *tx,
 bool AppLayerParserSupportsTxDetectFlags(AppProto alproto);
 
 AppLayerTxData *AppLayerParserGetTxData(uint8_t ipproto, AppProto alproto, void *tx);
+void AppLayerParserApplyTxConfig(uint8_t ipproto, AppProto alproto,
+        void *state, void *tx, enum ConfigAction mode, AppLayerTxConfig);
 
 /***** General *****/
 
index 06c6d026e96b556dbbfb763ab4b2ebe1f0885414..5ac182e964aa13e24cdf1b93cfe23b5257eb76fd 100644 (file)
@@ -181,6 +181,11 @@ int AppLayerRegisterParser(const struct AppLayerParser *p, AppProto alproto)
                 p->GetTxData);
     }
 
+    if (p->ApplyTxConfig) {
+        AppLayerParserRegisterApplyTxConfigFunc(p->ip_proto, alproto,
+                p->ApplyTxConfig);
+    }
+
     return 0;
 }
 
index ea6190ee107cfae075ce8419e2c050f354f49c8f..a6d5f7530015d9d7e6338db8665742e1afcce963 100644 (file)
@@ -73,6 +73,7 @@ typedef struct AppLayerParser {
     uint64_t (*GetTxDetectFlags)(void *, uint8_t);
 
     AppLayerTxData *(*GetTxData)(void *tx);
+    bool (*ApplyTxConfig)(void *state, void *tx, int mode, AppLayerTxConfig);
 } AppLayerParser;
 
 /**