From: Josh Date: Mon, 21 Apr 2014 21:07:25 +0000 (-0400) Subject: Adding encoder backwards compatability X-Git-Tag: 3.0.0-233~1559^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=559c1f4da8e2f87e91be37a4421742a44fcead08;p=thirdparty%2Fsnort3.git Adding encoder backwards compatability --- diff --git a/src/codecs/basic/cd_esp.cc b/src/codecs/basic/cd_esp.cc index d6fa3edf3..cea7def6a 100644 --- a/src/codecs/basic/cd_esp.cc +++ b/src/codecs/basic/cd_esp.cc @@ -93,7 +93,6 @@ bool EspCodec::decode(const uint8_t *raw_pkt, const uint32_t len, { const uint8_t *esp_payload; uint8_t pad_length; - uint8_t save_layer = p->next_layer; if (!ScESPDecoding()) return false; @@ -143,13 +142,13 @@ bool EspCodec::decode(const uint8_t *raw_pkt, const uint32_t len, // set the data pointers and pretend this is an ip datagram. if (!PacketManager::has_codec(next_prot_id)) { - p->packet_flags |= PKT_TRUST; - p->data = esp_payload; - p->dsize = (u_short) len - p_hdr_len; + p->packet_flags |= PKT_UNSURE_ENCAP; } else { - p->packet_flags |= PKT_UNSURE_ENCAP; + p->packet_flags |= PKT_TRUST; + p->data = esp_payload; + p->dsize = (u_short) len - p_hdr_len; } return true; diff --git a/src/codecs/basic/cd_eth.cc b/src/codecs/basic/cd_eth.cc index e55d9a8d9..4ea76e65f 100644 --- a/src/codecs/basic/cd_eth.cc +++ b/src/codecs/basic/cd_eth.cc @@ -54,6 +54,9 @@ public: virtual void get_protocol_ids(std::vector&); virtual void get_data_link_type(std::vector&); + // DELETE + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_ETH; }; }; diff --git a/src/codecs/basic/cd_icmp4.cc b/src/codecs/basic/cd_icmp4.cc index 01c29b2a9..16ebc2316 100644 --- a/src/codecs/basic/cd_icmp4.cc +++ b/src/codecs/basic/cd_icmp4.cc @@ -62,11 +62,13 @@ public: virtual bool decode(const uint8_t* raw_packet, const uint32_t raw_len, Packet *p, uint16_t &p_hdr_len, int &next_prot_id); - - virtual void get_protocol_ids(std::vector&); + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_ICMP4; }; + private: PegCount packets; PegCount discards; diff --git a/src/codecs/basic/cd_icmp6.cc b/src/codecs/basic/cd_icmp6.cc index adbf732c3..df1d610ff 100644 --- a/src/codecs/basic/cd_icmp6.cc +++ b/src/codecs/basic/cd_icmp6.cc @@ -47,8 +47,11 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_ICMP6; }; }; diff --git a/src/codecs/basic/cd_ipv4.cc b/src/codecs/basic/cd_ipv4.cc index 0658b1576..2aa1d791e 100644 --- a/src/codecs/basic/cd_ipv4.cc +++ b/src/codecs/basic/cd_ipv4.cc @@ -62,7 +62,10 @@ public: virtual void get_protocol_ids(std::vector&); // used in random classes throughout Snort++ - virtual inline bool is_ipv4(){ return true; }; + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_IP4; }; private: @@ -126,7 +129,6 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, Packet *p, uint16_t &p_hdr_len, int &next_prot_id) { uint32_t ip_len; /* length from the start of the ip hdr to the pkt end */ - uint32_t hlen; /* ip header length */ // dc.ip++; @@ -197,14 +199,13 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, ip_len = ntohs(p->iph->ip_len); /* get the IP header length */ - hlen = ipv4::get_pkt_hdr_len(p->iph) << 2; - p_hdr_len = hlen; + p_hdr_len = ipv4::get_pkt_hdr_len(p->iph) << 2; /* header length sanity check */ - if(hlen < ipv4::hdr_len()) + if(p_hdr_len < ipv4::hdr_len()) { DEBUG_WRAP(DebugMessage(DEBUG_DECODE, - "Bogus IP header length of %i bytes\n", hlen);); + "Bogus IP header length of %i bytes\n", p_hdr_len);); DecoderEvent(p, DECODE_IPV4_INVALID_HEADER_LEN); @@ -247,11 +248,11 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, } #endif - if(ip_len < hlen) + if(ip_len < p_hdr_len) { DEBUG_WRAP(DebugMessage(DEBUG_DECODE, "IP dgm len (%d bytes) < IP hdr " - "len (%d bytes), packet discarded\n", ip_len, hlen);); + "len (%d bytes), packet discarded\n", ip_len, p_hdr_len);); DecoderEvent(p, DECODE_IPV4_DGRAM_LT_IPHDR); @@ -274,7 +275,7 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, * need to check them (should make this a command line/config * option */ - int16_t csum = in_chksum_ip((u_short *)p->iph, hlen); + int16_t csum = in_chksum_ip((u_short *)p->iph, p_hdr_len); if(csum) { @@ -293,7 +294,7 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, } /* test for IP options */ - p->ip_options_len = (uint16_t)(hlen - ipv4::hdr_len()); + p->ip_options_len = (uint16_t)(p_hdr_len - ipv4::hdr_len()); if(p->ip_options_len > 0) { @@ -320,7 +321,7 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, p->actual_ip_len = (uint16_t) ip_len; /* set the remaining packet length */ - ip_len -= hlen; + ip_len -= p_hdr_len; /* check for fragmented packets */ p->frag_offset = ntohs(p->iph->ip_off); @@ -353,7 +354,7 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, { /* set the packet fragment flag */ p->frag_flag = 1; - p->ip_frag_start = raw_packet + hlen; + p->ip_frag_start = raw_packet + p_hdr_len; p->ip_frag_len = (uint16_t)ip_len; // dc.frags++; } @@ -369,7 +370,7 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, } /* Set some convienience pointers */ - p->ip_data = raw_packet + hlen; + p->ip_data = raw_packet + p_hdr_len; p->ip_dsize = (u_short) ip_len; /* See if there are any ip_proto only rules that match */ @@ -386,20 +387,18 @@ bool Ipv4Codec::decode(const uint8_t *raw_packet, const uint32_t len, (p->iph->ip_proto == IPPROTO_UDP))) { DEBUG_WRAP(DebugMessage(DEBUG_DECODE, "IP header length: %lu\n", - (unsigned long)hlen);); + (unsigned long)p_hdr_len);); next_prot_id = p->iph->ip_proto; - p_hdr_len = hlen; return true; } else { /* set the payload pointer and payload size */ - p->data = raw_packet + hlen; + p->data = raw_packet + p_hdr_len; p->dsize = (u_short) ip_len; } - p_hdr_len = hlen; return true; } diff --git a/src/codecs/basic/cd_ipv6.cc b/src/codecs/basic/cd_ipv6.cc index 96e89e29c..b745ef319 100644 --- a/src/codecs/basic/cd_ipv6.cc +++ b/src/codecs/basic/cd_ipv6.cc @@ -58,10 +58,10 @@ public: virtual void get_protocol_ids(std::vector&); - // used in random classes throughout Snort++ - virtual inline bool is_ipv6(){ return true; }; + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_IP6; }; - }; @@ -484,8 +484,8 @@ void DecodeIPV6Extensions(uint8_t next, const uint8_t *pkt, const uint32_t len, DecoderEvent(p, DECODE_IPV6_BAD_NEXT_HEADER); // dc.other++; - p->data = pkt; - p->dsize = (uint16_t)len; +// p->data = pkt; +// p->dsize = (uint16_t)len; break; }; } diff --git a/src/codecs/basic/cd_tcp.cc b/src/codecs/basic/cd_tcp.cc index ad23b4bcc..443e34e00 100644 --- a/src/codecs/basic/cd_tcp.cc +++ b/src/codecs/basic/cd_tcp.cc @@ -62,8 +62,11 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); + + // DELETE + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_TCP; }; }; static IpAddrSet *SynToMulticastDstIp = NULL; diff --git a/src/codecs/basic/cd_udp.cc b/src/codecs/basic/cd_udp.cc index 705df9290..8d1801ae1 100644 --- a/src/codecs/basic/cd_udp.cc +++ b/src/codecs/basic/cd_udp.cc @@ -60,6 +60,11 @@ public: Packet *, uint16_t &p_hdr_len, int &next_prot_id); virtual void get_protocol_ids(std::vector&); + + // DELETE + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_UDP; }; + }; @@ -234,8 +239,9 @@ bool UdpCodec::decode(const uint8_t *raw_pkt, const uint32_t len, // PushLayer(PROTO_UDP, p, raw_pkt, udp::header_len()); - p->data = (uint8_t *) (raw_pkt + udp::header_len()); - p->dsize = uhlen - udp::header_len(); // length validated above + // set in packet manager +// p->data = (uint8_t *) (raw_pkt + udp::header_len()); +// p->dsize = uhlen - udp::header_len(); // length validated above p->proto_bits |= PROTO_BIT__UDP; /* Drop packet if we ignore this port */ diff --git a/src/codecs/plugins/cd_ah.cc b/src/codecs/plugins/cd_ah.cc index f591c2f70..99408e288 100644 --- a/src/codecs/plugins/cd_ah.cc +++ b/src/codecs/plugins/cd_ah.cc @@ -43,8 +43,12 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); + + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_AH; }; }; diff --git a/src/codecs/plugins/cd_arp.cc b/src/codecs/plugins/cd_arp.cc index 65e655978..130720dd6 100644 --- a/src/codecs/plugins/cd_arp.cc +++ b/src/codecs/plugins/cd_arp.cc @@ -42,9 +42,12 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_ARP; }; }; diff --git a/src/codecs/plugins/cd_erspan2.cc b/src/codecs/plugins/cd_erspan2.cc index dbea43c88..5e310f334 100644 --- a/src/codecs/plugins/cd_erspan2.cc +++ b/src/codecs/plugins/cd_erspan2.cc @@ -41,6 +41,9 @@ public: virtual void get_protocol_ids(std::vector&); + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_ERSPAN; }; }; diff --git a/src/codecs/plugins/cd_erspan3.cc b/src/codecs/plugins/cd_erspan3.cc index 71014aa02..9df6c0bcf 100644 --- a/src/codecs/plugins/cd_erspan3.cc +++ b/src/codecs/plugins/cd_erspan3.cc @@ -43,6 +43,9 @@ public: virtual void get_protocol_ids(std::vector&); + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_ERSPAN; }; }; diff --git a/src/codecs/plugins/cd_gre.cc b/src/codecs/plugins/cd_gre.cc index 486e4e7ce..87634d3ce 100644 --- a/src/codecs/plugins/cd_gre.cc +++ b/src/codecs/plugins/cd_gre.cc @@ -43,7 +43,12 @@ public: virtual void get_protocol_ids(std::vector&); virtual void get_data_link_type(std::vector&){}; - + + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_GRE; }; + }; static const uint16_t GRE_PROT_ID = 47; diff --git a/src/codecs/plugins/cd_gtp.cc b/src/codecs/plugins/cd_gtp.cc index ded90aa18..fb85e3357 100644 --- a/src/codecs/plugins/cd_gtp.cc +++ b/src/codecs/plugins/cd_gtp.cc @@ -51,8 +51,12 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); + virtual void get_protocol_ids(std::vector&); - virtual void get_protocol_ids(std::vector&); + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_GTP; }; }; } // anonymous namespace diff --git a/src/codecs/plugins/cd_mpls.cc b/src/codecs/plugins/cd_mpls.cc index 05822c073..281b1fbed 100644 --- a/src/codecs/plugins/cd_mpls.cc +++ b/src/codecs/plugins/cd_mpls.cc @@ -46,9 +46,12 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_MPLS; }; }; diff --git a/src/codecs/plugins/cd_pppencap.cc b/src/codecs/plugins/cd_pppencap.cc index d4996de93..775446578 100644 --- a/src/codecs/plugins/cd_pppencap.cc +++ b/src/codecs/plugins/cd_pppencap.cc @@ -45,8 +45,12 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); + + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_PPP_ENCAP; }; }; diff --git a/src/codecs/plugins/cd_pppoepkt.cc b/src/codecs/plugins/cd_pppoepkt.cc index e24c6ff30..b2a4dfea6 100644 --- a/src/codecs/plugins/cd_pppoepkt.cc +++ b/src/codecs/plugins/cd_pppoepkt.cc @@ -39,9 +39,11 @@ public: virtual bool decode(const uint8_t *raw_pkt, const uint32_t len, Packet *, uint16_t &p_hdr_len, int &next_prot_id); - virtual void get_protocol_ids(std::vector&); + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_PPPOE; }; }; diff --git a/src/codecs/plugins/cd_vlan.cc b/src/codecs/plugins/cd_vlan.cc index b89574508..555518392 100644 --- a/src/codecs/plugins/cd_vlan.cc +++ b/src/codecs/plugins/cd_vlan.cc @@ -56,7 +56,11 @@ public: virtual void get_protocol_ids(std::vector&); virtual void get_data_link_type(std::vector&){}; + + // DELETE from here and below + #include "codecs/sf_protocols.h" + virtual inline PROTO_ID get_proto_id() { return PROTO_VLAN; }; }; } // anonymous namespace diff --git a/src/framework/codec.h b/src/framework/codec.h index da951c539..8abecb9f9 100644 --- a/src/framework/codec.h +++ b/src/framework/codec.h @@ -26,6 +26,10 @@ #include "framework/base_api.h" +// REMOVE WHEN POSSIBLE!!! +#include "codecs/sf_protocols.h" + + struct Packet; // this is the current version of the api @@ -53,12 +57,12 @@ public: // ONE OF THESE METHODS MUST BE IMPLEMENTED!! virtual void get_protocol_ids(std::vector&){}; virtual void get_data_link_type(std::vector&){}; - - virtual inline bool is_ipv4(){ return false; }; - virtual inline bool is_ipv6(){ return false; }; virtual inline const char* get_name(){return name; }; + virtual inline PROTO_ID get_proto_id() { return PROTO_MAX; }; + + protected: Codec(const char* s) { name = s; }; diff --git a/src/managers/packet_manager.cc b/src/managers/packet_manager.cc index 4b3bc9be3..8a9e7091a 100644 --- a/src/managers/packet_manager.cc +++ b/src/managers/packet_manager.cc @@ -34,7 +34,7 @@ THREAD_LOCAL PreprocStats decodePerfStats; //namespace //{ static const uint16_t max_protocol_id = 65535; - static std::array s_protocols; + static std::array s_protocols; static list s_codecs; //} // namespace @@ -100,6 +100,7 @@ void PacketManager::decode( // The boolean check in this order so while(curr_prot_id >= 0 && + curr_prot_id < max_protocol_id && s_protocols[curr_prot_id] != 0 && s_protocols[curr_prot_id]->decode(pkt, len, p, p_hdr_len, next_prot_id)) { @@ -229,8 +230,6 @@ void PacketManager::set_grinder(void) // FatalError("Codec installation checking!!"); } - static void init_codecs(); - void PacketManager::dump_stats() { diff --git a/src/protocols/packet.cc b/src/protocols/packet.cc index 15afd3bb5..4c70706dd 100644 --- a/src/protocols/packet.cc +++ b/src/protocols/packet.cc @@ -24,19 +24,20 @@ #include "codecs/sf_protocols.h" #include "log/messages.h" -void PacketClass::PushLayer(Packet *p, const Codec *cd, const uint8_t *hdr_start, uint32_t len) +void PacketClass::PushLayer(Packet *p, Codec* const cd, const uint8_t *hdr_start, uint32_t len) { if ( p->next_layer < LAYER_MAX ) { - Layer* lyr = p->layers + p->next_layer++; - lyr->proto = PROTO_TCP; - lyr->cd = cd; - lyr->start = (uint8_t*)hdr_start; - lyr->length = (uint16_t)len; + Layer lyr = p->layers[p->next_layer]; + lyr.proto = cd->get_proto_id(); + lyr.cd = cd; + lyr.start = (uint8_t*)hdr_start; + lyr.length = (uint16_t)len; } else { LogMessage("(snort_decoder) WARNING: decoder got too many layers;" " next proto is something.\n"); } -} \ No newline at end of file +} + diff --git a/src/protocols/packet.h b/src/protocols/packet.h index e3f69ec24..783e2941b 100644 --- a/src/protocols/packet.h +++ b/src/protocols/packet.h @@ -898,8 +898,7 @@ typedef struct class PacketClass{ public: - static - void PushLayer(Packet *p, const Codec *cd, const uint8_t *hdr_start, uint32_t len); + static void PushLayer(Packet *p, Codec* const cd, const uint8_t *hdr_start, uint32_t len); private: