From: Josh Date: Mon, 22 Sep 2014 19:47:15 +0000 (-0400) Subject: Main Packet struct is now allocated using PacketManager::encode_new() X-Git-Tag: 3.0.0-233~1404^2~10^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b71707bf4b9547ef87b6b3f69a1fc5f1ec632bae;p=thirdparty%2Fsnort3.git Main Packet struct is now allocated using PacketManager::encode_new() --- b71707bf4b9547ef87b6b3f69a1fc5f1ec632bae diff --cc src/main/snort.cc index 5923c36a0,cabc87c0c..e1087ec9b --- a/src/main/snort.cc +++ b/src/main/snort.cc @@@ -117,7 -117,6 +117,7 @@@ using namespace std //------------------------------------------------------------------------- - static THREAD_LOCAL Packet s_packet; // runtime variable. ++static THREAD_LOCAL Packet* s_packet; // runtime variable. THREAD_LOCAL SnortConfig* snort_conf = nullptr; static SnortConfig* snort_cmd_line_conf = nullptr; @@@ -702,7 -673,7 +674,7 @@@ void set_main_hook(MainHook_f f { main_hook = f; } Packet* get_current_packet() --{ return &s_packet; } ++{ return s_packet; } // FIXIT-J for multiple packet threads // using thread locals for s_pkth and s_data won't work @@@ -710,15 -681,12 +682,15 @@@ // capture all if it is not clear which thread crashed void CapturePacket() { -- if ( s_packet.pkth ) ++ if ( s_packet->pkth ) { -- s_pkth = *s_packet.pkth; ++ s_pkth = *(s_packet->pkth); -- if ( s_packet.pkt ) - memcpy(s_data, s_packet.pkt, 0xFFFF & s_packet.pkth->caplen); ++ if ( s_packet->pkt ) + { - memcpy(s_data, s_packet.pkt, 0xFFFF & s_packet.pkth->caplen); - s_packet.pkt = s_data; ++ memcpy(s_data, s_packet->pkt, 0xFFFF & s_packet->pkth->caplen); ++ s_packet->pkt = s_data; + } } } @@@ -873,9 -821,9 +825,9 @@@ DAQ_Verdict packet_callback ActionManager::reset_queue(); -- verdict = ProcessPacket(&s_packet, pkthdr, pkt); ++ verdict = ProcessPacket(s_packet, pkthdr, pkt); -- ActionManager::execute(&s_packet); ++ ActionManager::execute(s_packet); if ( Active_PacketWasDropped() ) { @@@ -884,19 -832,19 +836,18 @@@ } else { -- Packet* p = &s_packet; -- if ( s_packet.packet_flags & PKT_MODIFIED ) ++ if ( s_packet->packet_flags & PKT_MODIFIED ) { // this packet was normalized and/or has replacements -- PacketManager::encode_update(&s_packet); ++ PacketManager::encode_update(s_packet); verdict = DAQ_VERDICT_REPLACE; } -- else if ( p->packet_flags & PKT_RESIZED ) ++ else if ( s_packet->packet_flags & PKT_RESIZED ) { -- printf("packet flags = 0x%X\n", p->packet_flags); ++ printf("packet flags = 0x%X\n", s_packet->packet_flags); // we never increase, only trim, but // daq doesn't support resizing wire packet -- if ( !DAQ_Inject(s_packet.pkth, 0, s_packet.pkt, s_packet.pkth->pktlen) ) ++ if ( !DAQ_Inject(s_packet->pkth, 0, s_packet->pkt, s_packet->pkth->pktlen) ) { verdict = DAQ_VERDICT_BLOCK; inject = 1; @@@ -904,8 -852,8 +855,8 @@@ } else { -- if ( (s_packet.packet_flags & PKT_IGNORE) || -- (stream.get_ignore_direction(s_packet.flow) == SSN_DIR_BOTH) ) ++ if ( (s_packet->packet_flags & PKT_IGNORE) || ++ (stream.get_ignore_direction(s_packet->flow) == SSN_DIR_BOTH) ) { if ( !Active_GetTunnelBypass() ) { @@@ -917,9 -865,9 +868,9 @@@ pc.internal_whitelist++; } } -- else if ( s_packet.ptrs.decode_flags & DECODE_PKT_TRUST ) ++ else if ( s_packet->ptrs.decode_flags & DECODE_PKT_TRUST ) { -- stream.set_ignore_direction(s_packet.flow, SSN_DIR_BOTH); ++ stream.set_ignore_direction(s_packet->flow, SSN_DIR_BOTH); verdict = DAQ_VERDICT_WHITELIST; } @@@ -940,7 -888,7 +891,7 @@@ flow_con->timeout_flows(4, pkthdr->ts.tv_sec); } -- s_packet.pkth = NULL; // no longer avail on segv ++ s_packet->pkth = NULL; // no longer avail on segv if ( snort_conf->pkt_cnt && pc.total_from_daq >= snort_conf->pkt_cnt ) DAQ_BreakLoop(-1); @@@ -967,7 -915,7 +918,8 @@@ void snort_thread_init(const char* intf DAQ_New(snort_conf, intf); DAQ_Start(); - CodecManager::thread_init(snort_conf, s_packet); ++ s_packet = PacketManager::encode_new(); + CodecManager::thread_init(); FileAPIPostInit(); // this depends on instantiated daq capabilities @@@ -999,7 -947,6 +951,13 @@@ void snort_thread_term( ActionManager::thread_term(snort_conf); IpsManager::clear_options(); EventManager::close_outputs(); - CodecManager::thread_term(s_packet); ++ CodecManager::thread_term(); ++ ++ if (s_packet) ++ { ++ PacketManager::encode_delete(s_packet); ++ s_packet = nullptr; ++ } if ( DAQ_WasStarted() ) DAQ_Stop(); diff --cc src/main/snort_config.h index 71009394c,d293253a9..333d63050 --- a/src/main/snort_config.h +++ b/src/main/snort_config.h @@@ -284,23 -281,14 +284,23 @@@ struct SnortConfi bool unit_test; #endif - InspectionPolicy* get_inspection_policy() const - { return policy_map->get_inspection_policy(); }; + InspectionPolicy* get_inspection_policy() + { return policy_map->inspection_policy[0]; }; - IpsPolicy* get_ips_policy() const - { return policy_map->get_ips_policy(); }; + IpsPolicy* get_ips_policy() + { return policy_map->ips_policy[0]; }; - NetworkPolicy* get_network_policy() const - { return policy_map->get_network_policy(); }; + NetworkPolicy* get_network_policy() + { return policy_map->network_policy[0]; }; + + inline uint8_t get_num_layers() const + { return num_layers; } + + inline uint8_t get_ip6_maxopts() const + { return max_ip6_options; } + + inline uint8_t get_ip_maxlayers() const + { return max_ip_layers; } }; SnortConfig* SnortConfNew(void); diff --cc src/managers/codec_manager.cc index 310f8e918,b2637e17a..000c319a7 --- a/src/managers/codec_manager.cc +++ b/src/managers/codec_manager.cc @@@ -197,11 -196,8 +197,8 @@@ void CodecManager::instantiate( instantiate(wrap, nullptr, nullptr); } - void CodecManager::thread_init(const SnortConfig* const sc, Packet& p) -void CodecManager::thread_init(void) ++void CodecManager::thread_init() { - max_layers = sc->get_num_layers(); - p.layers = new Layer[max_layers]; - for ( CodecApiWrapper& wrap : s_codecs ) if (wrap.api->tinit) wrap.api->tinit(); diff --cc src/managers/codec_manager.h index deb10efc3,a29467bda..13f8636b9 --- a/src/managers/codec_manager.h +++ b/src/managers/codec_manager.h @@@ -62,10 -62,10 +62,10 @@@ public static void instantiate(); // destroy all global codec related information static void release_plugins(); - // initialize the current threads codecs + // initialize the current threads DLT and Packet struct - static void thread_init(const SnortConfig* const, Packet&); + static void thread_init(); // destroy thread_local data - static void thread_term(Packet&); + static void thread_term(); // print all of the codec plugins static void dump_plugins(); diff --cc src/protocols/packet_manager.cc index 9ec1df09b,38d44c89d..60554bd88 --- a/src/protocols/packet_manager.cc +++ b/src/protocols/packet_manager.cc @@@ -105,9 -103,8 +105,9 @@@ Packet* PacketManager::encode_new( { Packet* p = (Packet*)SnortAlloc(sizeof(*p)); uint8_t* b = (uint8_t*)SnortAlloc(sizeof(*p->pkth) + Codec::PKT_MAX + SPARC_TWIDDLE); - Layer* lyr = (Layer*)SnortAlloc(sizeof(Layer) * CodecManager::max_layers); ++ Layer* lyr = new Layer[CodecManager::max_layers]; - if ( !p || !b ) + if ( !p || !b || !lyr) FatalError("encode_new() => Failed to allocate packet\n"); p->pkth = (DAQ_PktHdr_t*)b; @@@ -123,14 -119,9 +123,14 @@@ void PacketManager::encode_delete(Packe { if (p) { - if (p->pkth) + if(p->pkth) free((void*)p->pkth); // cast away const! + if(p->layers) - free(p->layers); ++ delete[] p->layers; + + p->pkth = nullptr; + p->layers = nullptr; free(p); } } diff --cc src/protocols/packet_manager.h index c472bcd32,e100fb4d1..bf1828096 --- a/src/protocols/packet_manager.h +++ b/src/protocols/packet_manager.h @@@ -143,10 -126,8 +143,10 @@@ private // STATISTICS!! // The only time we should accumulate is when CodecManager tells us too - friend void CodecManager::thread_term(Packet&); + friend void CodecManager::thread_term(); static void accumulate(); + static bool encode(const Packet* p, EncodeFlags, + uint8_t lyr_start, uint8_t next_prot, Buffer& buf); // constant offsets into the s_stats array. Notice the stat_offset // constant which is used when adding a protocol specific codec