]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
eliminate packet manager thread local for dst mac
authorRuss Combs <rucombs@cisco.com>
Sun, 23 Oct 2016 18:48:20 +0000 (14:48 -0400)
committerRuss Combs <rucombs@cisco.com>
Wed, 18 Jan 2017 14:51:25 +0000 (09:51 -0500)
move packet manager thread local for encode packet to detection context
using codec max for packet buffers

src/codecs/root/cd_eth.cc
src/detection/detection_engine.cc
src/detection/detection_engine.h
src/detection/ips_context.cc
src/detection/ips_context.h
src/main/snort.cc
src/packet_io/active.cc
src/protocols/packet_manager.cc
src/protocols/packet_manager.h
src/stream/ip/ip_defrag.cc

index e54760f65749dd3cefd5c318fe69f073305c47d9..e8d6dbfab24a9412fb24753dde823bc4905ef09d 100644 (file)
@@ -164,25 +164,24 @@ bool EthCodec::encode(const uint8_t* const raw_in, const uint16_t /*raw_len*/,
             return false;
 
         eth::EtherHdr* ho = reinterpret_cast<eth::EtherHdr*>(buf.data());
-        ho->ether_type = enc.ethertype_set() ? htons(to_utype(enc.next_ethertype)) : hi->ether_type;
-
-        uint8_t* dst_mac = PacketManager::encode_get_dst_mac();
+        ho->ether_type = enc.ethertype_set() ?
+            htons(to_utype(enc.next_ethertype)) : hi->ether_type;
 
         if ( enc.forward() )
         {
             memcpy(ho->ether_src, hi->ether_src, sizeof(ho->ether_src));
-            /*If user configured remote MAC address, use it*/
-            if (nullptr != dst_mac)
-                memcpy(ho->ether_dst, dst_mac, sizeof(ho->ether_dst));
+
+            if ( snort_conf->eth_dst )
+                memcpy(ho->ether_dst, snort_conf->eth_dst, sizeof(ho->ether_dst));
             else
                 memcpy(ho->ether_dst, hi->ether_dst, sizeof(ho->ether_dst));
         }
         else
         {
             memcpy(ho->ether_src, hi->ether_dst, sizeof(ho->ether_src));
-            /*If user configured remote MAC address, use it*/
-            if (nullptr != dst_mac)
-                memcpy(ho->ether_dst, dst_mac, sizeof(ho->ether_dst));
+
+            if ( snort_conf->eth_dst )
+                memcpy(ho->ether_dst, snort_conf->eth_dst, sizeof(ho->ether_dst));
             else
                 memcpy(ho->ether_dst, hi->ether_src, sizeof(ho->ether_dst));
         }
index 2c39a32e4e7db3c2d1630d4ec18e87d7d261ade1..db1c64237f0e19ef93765318d2d7d84d3bb27b8f 100644 (file)
@@ -63,6 +63,12 @@ Packet* DetectionEngine::get_current_packet()
 Packet* DetectionEngine::get_packet()
 { return get_current_packet(); }
 
+void DetectionEngine::set_encode_packet(Packet* p)
+{ Snort::get_switcher()->get_context()->encode_packet = p; }
+
+Packet* DetectionEngine::get_encode_packet()
+{ return Snort::get_switcher()->get_context()->encode_packet; }
+
 MpseStash* DetectionEngine::get_stash()
 { return Snort::get_switcher()->get_context()->stash; }
 
index b8330cbdac566e52d85b5c37ca0ab3a401058b72..12fd50cf8c50aead225a56e51280cd174acd0bfb 100644 (file)
@@ -47,6 +47,9 @@ public:
     static Packet* set_packet();
     static void clear_packet();
 
+    static void set_encode_packet(Packet*);
+    static Packet* get_encode_packet();
+
     static class MpseStash* get_stash();
     static uint8_t* get_buffer(unsigned& max);
 
index 277d69b3d040096f14af0ad4f33fdcfa49994dc6..90b169cc854fc53ed6e8a0d7a06ccf67a7671922 100644 (file)
@@ -57,8 +57,10 @@ IpsContext::IpsContext(unsigned size) :
     data(size ? size : IpsContextData::get_max_id() + 1, nullptr)
 {
     packet = new Packet(false);
+    encode_packet = nullptr;
+
     pkth = new DAQ_PktHdr_t;
-    buf = new uint8_t[buf_size];  // FIXIT-H use codec max or let pkt do it?
+    buf = new uint8_t[buf_size];
 
     const EventQueueConfig* qc = snort_conf->event_queue_config;
     equeue = sfeventq_new(qc->max_events, qc->log_events, sizeof(EventNode));
index cdd5be86b8289c44246eea56ece8aa14e1c7a1b3..8ba9ecf8029b81711d78d94954759ce090c17c3d 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <vector>
 #include "main/snort_types.h"
+#include "framework/codec.h"
 
 // required to get a decent decl of pkth
 #include "protocols/packet.h"
@@ -64,6 +65,7 @@ public:
 
 public:
     Packet* packet;
+    Packet* encode_packet;
     DAQ_PktHdr_t* pkth;
     uint8_t* buf;
 
@@ -73,7 +75,7 @@ public:
 
     struct SF_EVENTQ* equeue;
 
-    static const unsigned buf_size = 65536;
+    static const unsigned buf_size = Codec::PKT_MAX;
 
 private:
     std::vector<IpsContextData*> data;
index 50c78adaa6bf19b921b702d26ad07339425e7000..e22281c5d388bd70041f9b514bd77f3023cdfafa 100644 (file)
@@ -866,7 +866,6 @@ DAQ_Verdict Snort::packet_callback(
     HighAvailabilityManager::process_update(s_packet->flow, pkthdr);
 
     Active::reset();
-    PacketManager::encode_reset();
     Stream::timeout_flows(pkthdr->ts.tv_sec);
     HighAvailabilityManager::process_receive();
 
index 41fa8f30b11047722e7a30c3cacd0035cec449cc..2d18031d97ed09df81b7a38df1a1f3a54ab1af23 100644 (file)
@@ -173,9 +173,6 @@ bool Active::init(SnortConfig* sc)
             s_attempts = 0;
 #endif
         }
-
-        if (NULL != sc->eth_dst)
-            PacketManager::encode_set_dst_mac(sc->eth_dst);
     }
     return true;
 }
index 07c94f64b5d0a94166dbf0187fbd2eb5b3055d5d..c370e8b6e4515638bf8f0acacc4c45e00a3a5220 100644 (file)
@@ -65,12 +65,8 @@ const std::array<const char*, PacketManager::stat_offset> PacketManager::stat_na
 };
 
 // Encoder Foo
-static THREAD_LOCAL Packet* encode_pkt = nullptr;
 static THREAD_LOCAL PegCount total_rebuilt_pkts = 0;
-static THREAD_LOCAL std::array<uint8_t, Codec::PKT_MAX> s_pkt {
-    { 0 }
-};
-static THREAD_LOCAL uint8_t* dst_mac = nullptr;
+static THREAD_LOCAL std::array<uint8_t, Codec::PKT_MAX> s_pkt { { 0 } };
 
 //-------------------------------------------------------------------------
 // Private helper functions
@@ -351,8 +347,8 @@ bool PacketManager::encode(const Packet* p,
     IpProtocol next_prot,
     Buffer& buf)
 {
-    if ( encode_pkt )
-        p = encode_pkt;
+    if ( Packet* pe = DetectionEngine::get_encode_packet() )
+        p = pe;
 
     uint8_t ttl = GetTTL(p, (flags & ENC_FLAG_FWD));
     if ( ttl )
@@ -812,18 +808,9 @@ void PacketManager::encode_update(Packet* p)
 // codec support and statistics
 //-------------------------------------------------------------------------
 
-void PacketManager::encode_set_dst_mac(uint8_t* mac)
-{ dst_mac = mac; }
-
-uint8_t* PacketManager::encode_get_dst_mac()
-{ return dst_mac; }
-
 uint64_t PacketManager::get_rebuilt_packet_count()
 { return total_rebuilt_pkts; }
 
-void PacketManager::encode_set_pkt(Packet* p)
-{ encode_pkt = p; }
-
 uint16_t PacketManager::encode_get_max_payload(const Packet* p)
 {
     if ( !p->num_layers )
index b1fca5b989581bb5a500ad77b70120d5f4a0ce69..191dda7387249e5fb5fedfea4a7741c769871cad 100644 (file)
@@ -57,14 +57,6 @@ public:
     // decode this packet and set all relevent packet fields.
     static void decode(Packet*, const struct _daq_pkthdr*, const uint8_t*, bool cooked = false);
 
-    // when encoding, rather than copy the destination MAC address from the
-    // inbound packet, manually set the MAC address.
-    static void encode_set_dst_mac(uint8_t*);
-
-    // get the MAC address which has been set using encode_set_dst_mac().
-    // Useful for root decoders setting the MAC address
-    static uint8_t* encode_get_dst_mac();
-
     // update the packet's checksums and length variables. Call this function
     // after Snort has changed any data in this packet
     static void encode_update(Packet*);
@@ -104,16 +96,9 @@ public:
     // get the number of packets which have been rebuilt by this thread
     static PegCount get_rebuilt_packet_count();
 
-    // set the packet to be encoded.
-    static void encode_set_pkt(Packet* p);
-
     // get the max payload for the current packet
     static uint16_t encode_get_max_payload(const Packet*);
 
-    // reset the current 'clone' packet
-    static void encode_reset()
-    { encode_set_pkt(NULL); }
-
     // print codec information.  MUST be called after thread_term.
     static void dump_stats();
 
index f1f71dbb84aa6e2d4ddc37331ad453d0b82cd6a9..5824eb25369c5225fe39d5748e89436f8e8f6e0d 100644 (file)
@@ -738,13 +738,13 @@ static void FragRebuild(FragTracker* ft, Packet* p)
 #endif
 
     DetectionEngine de;
-    PacketManager::encode_set_pkt(p);
+    de.set_encode_packet(p);
     Snort::process_packet(dpkt, dpkt->pkth, dpkt->pkt, true);
+    de.set_encode_packet(nullptr);
 
-    trace_log(stream_ip,
-        "Done with rebuilt packet, marking rebuilt...\n");
+    trace_log(stream_ip, "Done with rebuilt packet, marking rebuilt...\n");
 
-    ft->frag_flags = ft->frag_flags | FRAG_REBUILT;
+    ft->frag_flags |= FRAG_REBUILT;
 }
 
 /**