]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
convert stream splitters to detection engine buffer
authorRuss Combs <rucombs@cisco.com>
Sat, 22 Oct 2016 11:36:21 +0000 (07:36 -0400)
committerRuss Combs <rucombs@cisco.com>
Wed, 18 Jan 2017 14:41:32 +0000 (09:41 -0500)
19 files changed:
extra/src/inspectors/dpx/dpx.cc
extra/src/inspectors/http_server/hi_stream_splitter.cc
src/detection/detect.cc
src/detection/detection_engine.cc
src/detection/detection_engine.h
src/detection/ips_context.cc
src/detection/ips_context.h
src/flow/flow_control.cc
src/main/snort.cc
src/network_inspectors/binder/test/binder_test.cc
src/piglet_plugins/pp_stream_splitter_iface.cc
src/service_inspectors/http_inspect/http_stream_splitter.h
src/service_inspectors/http_inspect/http_stream_splitter_reassemble.cc
src/stream/stream_splitter.cc
src/stream/stream_splitter.h
src/stream/tcp/tcp_reassembler.cc
src/stream/tcp/tcp_session.cc
src/stream/user/user_session.cc
src/stream/user/user_session.h

index c54feb380d7cb751aa81222f1dc20023d9eac549..9800baa8e444d6afa6a283c392a4bdf997bae6d9 100644 (file)
@@ -21,6 +21,7 @@
 #include "config.h"
 #endif
 
+#include "detection/detection_engine.h"
 #include "events/event_queue.h"
 #include "framework/inspector.h"
 #include "framework/module.h"
index 17038bba1588ecc6677f62beb125b74446f623e4..263394e26e96c75a41cbd49355a0309a87ebec6f 100644 (file)
 
 #include "hi_stream_splitter.h"
 
-<<<<<<< HEAD
+#include "detection/detection_engine.h"
 #include "events/event_queue.h"
 #include "main/snort_debug.h"
-=======
-#include "hi_events.h"
-#include "main/snort_debug.h"
-#include "detection/detection_engine.h"
->>>>>>> refactor event queue into DetectionEngine
 #include "protocols/packet.h"
 #include "stream/stream.h"
 #include "utils/util.h"
index a56fcf7510de5ca45ca227dfacc9fa7b2c53fdde..786d237d888a609a9935092a2696aabcb0ee92ce 100644 (file)
@@ -47,6 +47,7 @@
 #include "detection_engine.h"
 #include "fp_detect.h"
 #include "tag.h"
+#include "treenodes.h"
 
 #define CHECK_SRC_IP         0x01
 #define CHECK_DST_IP         0x02
index a66c69519a189e5cc68429f4cc9b7162d2c71d2a..894aa3df47fff5ab8005c8ce710be34ebcbf7165 100644 (file)
@@ -94,6 +94,12 @@ void DetectionEngine::clear_packet()
     sw->complete();
 }
 
+uint8_t* DetectionEngine::get_buffer(unsigned& max)
+{
+    max = IpsContext::buf_size;
+    return Snort::get_switcher()->get_context()->buf;
+}
+
 DetectionEngine::ActiveRules DetectionEngine::get_detects()
 { return active_rules; }
 
index edcb74674a1159cee3c7a86099d8d8bce058f02e..16c72971547992e4b09e3505d5e9e086c9f8b208 100644 (file)
@@ -43,6 +43,8 @@ public:
     static Packet* set_packet();
     static void clear_packet();
 
+    static uint8_t* get_buffer(unsigned& max);
+
     static bool detect(Packet*);
     static void inspect(Packet*);
 
index 9a0fbf7138e220489b5788a8e76ece1f5ce5bcba..dbca66b2bfc8ecd490b37607e1bedcd5db80f56f 100644 (file)
@@ -55,7 +55,7 @@ IpsContext::IpsContext(unsigned size) : data(size, nullptr)
 {
     packet = new Packet(false);
     pkth = new DAQ_PktHdr_t;
-    buf = new uint8_t[65536];  // FIXIT-H use codec max or let pkt do it
+    buf = new uint8_t[buf_size];  // FIXIT-H use codec max or let pkt do it?
 
     const EventQueueConfig* qc = snort_conf->event_queue_config;
     equeue = sfeventq_new(qc->max_events, qc->log_events, sizeof(EventNode));
index d97364162f02c3560f6b1ac31acd40a28b9cd9fd..b5468964be6f7036e219659bbba8e8324e741f5e 100644 (file)
@@ -68,6 +68,8 @@ public:
     uint8_t* buf;
     struct SF_EVENTQ* equeue;
 
+    static const unsigned buf_size = 65536;
+
 private:
     std::vector<IpsContextData*> data;
     unsigned slot;
index c563fc00686b4ae9cae96ed8354039c544551301..fba3c91db5b0da1a35189d74827d9e0eb81179e6 100644 (file)
@@ -22,7 +22,7 @@
 
 #include "flow_control.h"
 
-#include "detection/detect.h"
+#include "detection/detection_engine.h"
 #include "main/snort_config.h"
 #include "main/snort_debug.h"
 #include "managers/inspector_manager.h"
index e4971c93d5cbe76a9de60f446804e3e6effe7bbe..4f0193d5c9bd1cbdf656fa73100d8dbea9aad594 100644 (file)
@@ -31,6 +31,7 @@
 #include "connectors/connectors.h"
 #include "decompress/file_decomp.h"
 #include "detection/context_switcher.h"
+#include "detection/detect.h"
 #include "detection/detection_engine.h"
 #include "detection/detection_util.h"
 #include "detection/fp_config.h"
index 33f56e3c09f5a785b2c9dfab8b2d478444a0cbf0..6e91e7bb85877e100cc1289817943ae2b45f6bec 100644 (file)
@@ -29,6 +29,7 @@
 #include <thread>
 #include <vector>
 
+#include "detection/detection_engine.h"
 #include "flow/flow.h"
 #include "framework/inspector.h"
 #include "managers/inspector_manager.h"
@@ -87,6 +88,7 @@ bool sfvar_ip_in(sfip_var_t*, const SfIp*) { return false; }
 SO_PUBLIC Inspector* InspectorManager::get_inspector(const char*, bool) { return s_inspector; }
 InspectorType InspectorManager::get_type(const char*) { return InspectorType::IT_BINDER; }
 Inspector* InspectorManager::get_binder() { return nullptr; }
+uint8_t* DetectionEngine::get_buffer(unsigned&) { return nullptr; }
 
 int16_t ProtocolReference::find(const char*) { return 0; }
 const char* ProtocolReference::get_name(uint16_t) { return ""; }
index 6e4794adbbf395a3c9e6ada080ad58f8eb0d09cd..d528aa24359e32ccaf9cb81086f326250a73e3df 100644 (file)
@@ -77,9 +77,9 @@ static const luaL_Reg methods[] =
 
             Lua::Stack<unsigned>::push(L, copied);
 
-            if ( sb )
+            if ( sb.data )
                 RawBufferIface.create(
-                    L, reinterpret_cast<const char*>(sb->data), sb->length);
+                    L, reinterpret_cast<const char*>(sb.data), sb.length);
             else
                 lua_pushnil(L);
 
index db4b3f81b93f3cb0587d685de6e71515e64fd5ff..c8b9e626979ecb8254c2ba7597ea52acc050489d 100644 (file)
@@ -38,7 +38,7 @@ public:
         my_inspector(my_inspector_) { }
     Status scan(Flow* flow, const uint8_t* data, uint32_t length, uint32_t not_used,
         uint32_t* flush_offset) override;
-    const StreamBuffer* reassemble(Flow* flow, unsigned total, unsigned, const
+    const StreamBuffer reassemble(Flow* flow, unsigned total, unsigned, const
         uint8_t* data, unsigned len, uint32_t flags, unsigned& copied) override;
     bool finish(Flow* flow) override;
     bool is_paf() override { return true; }
index 5ab4d75ce189508b4d460e0bc653a1ddc06423cb..4591d5bdc8957528c76a4378e3d29a65f8d1540e 100644 (file)
@@ -197,10 +197,10 @@ void HttpStreamSplitter::decompress_copy(uint8_t* buffer, uint32_t& offset, cons
     offset += length;
 }
 
-const StreamBuffer* HttpStreamSplitter::reassemble(Flow* flow, unsigned total, unsigned,
+const StreamBuffer HttpStreamSplitter::reassemble(Flow* flow, unsigned total, unsigned,
     const uint8_t* data, unsigned len, uint32_t flags, unsigned& copied)
 {
-    static THREAD_LOCAL StreamBuffer http_buf;
+    StreamBuffer http_buf { nullptr, 0 };
 
     copied = len;
 
@@ -214,7 +214,7 @@ const StreamBuffer* HttpStreamSplitter::reassemble(Flow* flow, unsigned total, u
         {
             if (!(flags & PKT_PDU_TAIL))
             {
-                return nullptr;
+                return http_buf;
             }
             bool tcp_close;
             uint8_t* test_buffer;
@@ -228,7 +228,7 @@ const StreamBuffer* HttpStreamSplitter::reassemble(Flow* flow, unsigned total, u
             {
                 // Source ID does not match test data, no test data was flushed, or there is no
                 // more test data
-                return nullptr;
+                return http_buf;
             }
             data = test_buffer;
             total = len;
@@ -245,7 +245,7 @@ const StreamBuffer* HttpStreamSplitter::reassemble(Flow* flow, unsigned total, u
     // FIXIT-H Workaround for TP Bug 149662
     if (session_data->section_type[source_id] == SEC__NOT_COMPUTE)
     {
-        return nullptr;
+        return { nullptr, 0 };
     }
 
     assert(session_data->section_type[source_id] != SEC__NOT_COMPUTE);
@@ -290,7 +290,7 @@ const StreamBuffer* HttpStreamSplitter::reassemble(Flow* flow, unsigned total, u
                 }
             }
         }
-        return nullptr;
+        return http_buf;
     }
 
     HttpModule::increment_peg_counts(PEG_REASSEMBLE);
@@ -377,10 +377,10 @@ const StreamBuffer* HttpStreamSplitter::reassemble(Flow* flow, unsigned total, u
                 fflush(HttpTestManager::get_output_file());
             }
 #endif
-            return &http_buf;
+            return http_buf;
         }
         my_inspector->clear(session_data, source_id);
     }
-    return nullptr;
+    return http_buf;
 }
 
index 0a3450a5ac44a49189bb6ecef9a276256760b7e3..80e839d1d0c5a8e5f3ae4e518fc4ee590dce611d 100644 (file)
 
 #include "stream_splitter.h"
 
+#include "detection/detection_engine.h"
 #include "main/snort_config.h"
 #include "protocols/packet.h"
 
 #include "flush_bucket.h"
 
-static THREAD_LOCAL uint8_t pdu_buf[StreamSplitter::max_buf];
-static THREAD_LOCAL StreamBuffer str_buf;
-
 unsigned StreamSplitter::max(Flow*)
 { return snort_conf->max_pdu; }
 
-const StreamBuffer* StreamSplitter::reassemble(
+const StreamBuffer StreamSplitter::reassemble(
     Flow*, unsigned, unsigned offset, const uint8_t* p,
     unsigned n, uint32_t flags, unsigned& copied)
 {
-    assert(offset + n < sizeof(pdu_buf));
+    unsigned max;
+    uint8_t* pdu_buf = DetectionEngine::get_buffer(max);
+
+    assert(offset + n < max);
     memcpy(pdu_buf+offset, p, n);
     copied = n;
 
     if ( flags & PKT_PDU_TAIL )
-    {
-        str_buf.data = pdu_buf;
-        str_buf.length = offset + n;
-        return &str_buf;
-    }
-    return nullptr;
+        return { pdu_buf, offset + n };
+
+    return { nullptr, 0 };
 }
 
 //--------------------------------------------------------------------------
index 1c5a7bb6fc86b86daaacb33643538602c2840375..d99b7a80e248898eca8a5726e3f32eeeaa94faea 100644 (file)
@@ -65,7 +65,7 @@ public:
 
     // the last call to reassemble() will be made with len == 0 if
     // finish() returned true as an opportunity for a final flush
-    virtual const StreamBuffer* reassemble(
+    virtual const StreamBuffer reassemble(
         Flow*,
         unsigned total,        // total amount to flush (sum of iterations)
         unsigned offset,       // data offset from start of reassembly
index 95c90e902737276bef9483d8ba4d0b2b576129ef..afba7476d42686eff672e91f710f15f75b85fa9f 100644 (file)
@@ -478,17 +478,17 @@ int TcpReassembler::flush_data_segments(Packet* p, uint32_t total)
             || SEQ_EQ(tsn->seq +  bytes_to_copy, to_seq) )
             flags |= PKT_PDU_TAIL;
 
-        const StreamBuffer* sb = tracker->splitter->reassemble(
+        const StreamBuffer sb = tracker->splitter->reassemble(
             session->flow, total, bytes_flushed, tsn->payload(),
             bytes_to_copy, flags, bytes_copied);
 
         flags = 0;
 
-        if ( sb )
+        if ( sb.data )
         {
-            s5_pkt->data = sb->data;
-            s5_pkt->dsize = sb->length;
-            assert(sb->length <= s5_pkt->max_dsize);
+            s5_pkt->data = sb.data;
+            s5_pkt->dsize = sb.length;
+            assert(sb.length <= s5_pkt->max_dsize);
 
             bytes_to_copy = bytes_copied;
         }
@@ -527,7 +527,7 @@ int TcpReassembler::flush_data_segments(Packet* p, uint32_t total)
             break;
         }
 
-        if ( sb || !seglist.next )
+        if ( sb.data || !seglist.next )
             break;
 
         if ( bytes_flushed + seglist.next->payload_size >= StreamSplitter::max_buf )
@@ -596,8 +596,6 @@ void TcpReassembler::prep_s5_pkt(Flow* flow, Packet* p, uint32_t pkt_flags)
 int TcpReassembler::_flush_to_seq(uint32_t bytes, Packet* p, uint32_t pkt_flags)
 {
     Profile profile(s5TcpFlushPerfStats);
-    s5_pkt = Snort::set_detect_packet();
-
     s5_pkt = DetectionEngine::set_packet();
 
     DAQ_PktHdr_t pkth;
@@ -627,7 +625,7 @@ int TcpReassembler::_flush_to_seq(uint32_t bytes, Packet* p, uint32_t pkt_flags)
 
         if ( footprint == 0 )
         {
-            Snort::clear_detect_packet();
+            DetectionEngine::clear_packet();
             return bytes_processed;
         }
 
index cc2b03f66b2112ef23f34572aab4ad0381b6ae84..14b09d3244d34476c7166c75c4d724a9327347a3 100644 (file)
@@ -49,6 +49,7 @@
 #include "tcp_session.h"
 
 #include "detection/detection_engine.h"
+#include "detection/rules.h"
 #include "log/log.h"
 #include "perf_monitor/flow_ip_tracker.h"
 #include "profiler/profiler.h"
index df7513e82fd568cb42bda386c8dd5db956b5d2d9..6cb8ad7030bf8dd524a0e8a516c7e2096b1e93bd 100644 (file)
@@ -131,15 +131,15 @@ void UserTracker::term()
     splitter = nullptr;
 }
 
-void UserTracker::detect(const Packet* p, const StreamBuffer* sb, uint32_t flags)
+void UserTracker::detect(const Packet* p, const StreamBuffer& sb, uint32_t flags)
 {
     Packet up(false);
 
     up.pkth = p->pkth;
     up.ptrs = p->ptrs;
     up.flow = p->flow;
-    up.data = sb->data;
-    up.dsize = sb->length;
+    up.data = sb.data;
+    up.dsize = sb.length;
 
     up.proto_bits = p->proto_bits;
     up.pseudo_type = PSEUDO_PKT_USER;
@@ -193,7 +193,7 @@ int UserTracker::scan(Packet* p, uint32_t& flags)
 void UserTracker::flush(Packet* p, unsigned flush_amt, uint32_t flags)
 {
     unsigned bytes_flushed = 0;
-    const StreamBuffer* sb = nullptr;
+    StreamBuffer sb = { nullptr, 0 };
     trace_logf(stream_user, "flush[%d]\n", flush_amt);
     uint32_t rflags = flags & ~PKT_PDU_TAIL;
 
@@ -219,7 +219,7 @@ void UserTracker::flush(Packet* p, unsigned flush_amt, uint32_t flags)
 
         rflags &= ~PKT_PDU_HEAD;
 
-        if ( sb )
+        if ( sb.data )
             detect(p, sb, flags);
 
         if ( bytes_copied == us->get_len() )
index 1047da108da406a719ab3c7907f12f6266e4e889..8d39a2dab9c4098efc1a9fe5df49a44b63b00ecd 100644 (file)
@@ -63,7 +63,7 @@ struct UserTracker
     void add_data(Packet*);
     int scan(Packet*, uint32_t&);
     void flush(struct Packet*, unsigned, uint32_t);
-    void detect(const struct Packet*, const struct StreamBuffer*, uint32_t);
+    void detect(const struct Packet*, const struct StreamBuffer&, uint32_t);
 
     std::list<UserSegment*> seg_list;
     StreamSplitter* splitter;