decode_module.cc
codec_api.h
codec_api.cc
+ cd_stats.h
+ cd_stats.cc
)
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
-#if 0
-
-#ifdef HAVE_DUMBNET_H
-#include <dumbnet.h>
-#else
-#include <dnet.h>
-#endif
-#endif
#include "framework/codec.h"
#include "codecs/codec_events.h"
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<uint16_t>&);
};
return true;
}
-
-
-void EspCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ESP_PROT_ID);
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ NULL, // get_dlt()
+ get_protocol_ids,
sum, // sum
stats // stats
};
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
virtual bool decode(const uint8_t *raw_pkt, const uint32_t len,
Packet *p, uint16_t &p_hdr_len, int &next_prot_id);
- virtual void get_protocol_ids(std::vector<uint16_t>&);
- virtual void get_data_link_type(std::vector<int>&);
// DELETE
#include "codecs/sf_protocols.h"
virtual inline PROTO_ID get_proto_id() { return PROTO_ETH; };
} // anonymous namespace
-
//--------------------------------------------------------------------
// decode.c::Ethernet
//--------------------------------------------------------------------
#endif
-void EthCodec::get_data_link_type(std::vector<int>&v)
+static void get_data_link_type(std::vector<int>&v)
{
v.push_back(DLT_EN10MB);
}
-void EthCodec::get_protocol_ids(std::vector<uint16_t>& v)
-{
-}
static Codec* ctor()
{
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ get_data_link_type,
+ NULL,
+ NULL,
NULL,
- NULL
};
const BaseApi* cd_eth = ð_api.base;
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
--- /dev/null
+/*
+** Copyright (C) 2002-2013 Sourcefire, Inc.
+** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License Version 2 as
+** published by the Free Software Foundation. You may not use, modify or
+** distribute this program under any other version of the GNU General
+** Public License.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#include "cd_stats.h"
+#include "src/thread.h"
+
+namespace codec_statistics
+{
+
+static THREAD_LOCAL ProtType curr_type;
+
+
+void set_state(ProtType ct)
+{
+ curr_type = ct;
+}
+
+ProtType get_state(){
+ return curr_type;
+}
+
+} //namespace codec_statistics
--- /dev/null
+/*
+** Copyright (C) 2002-2013 Sourcefire, Inc.
+** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License Version 2 as
+** published by the Free Software Foundation. You may not use, modify or
+** distribute this program under any other version of the GNU General
+** Public License.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#ifndef CD_STATS_H
+#define CD_STATS_H
+
+namespace codec_statistics
+{
+
+enum class ProtType{
+ PROT_IPV4,
+ PROT_IPV6,
+ PROT_IPV6_EXT,
+ PROT_ICMP4,
+ PROT_ICMP6,
+ PROT_UDP,
+ PROT_TCP,
+ PROT_GRE,
+};
+
+void set_state(ProtType);
+ProtType get_state();
+
+}; // namespace codec_statistics
+
+#endif
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<uint16_t>&);
// DELETE from here and below
-void AhCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(AH_PROT_ID);
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr,
+ get_protocol_ids,
sum, // sum
stats // stats
};
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<uint16_t>&);
// DELETE from here and below
-void ArpCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERNET_TYPE_ARP);
v.push_back(ETHERNET_TYPE_REVARP);
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr, // get_dlt
+ get_protocol_ids,
sum, // sum
stats // stats
};
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<uint16_t>&);
// DELETE from here and below
#include "codecs/sf_protocols.h"
-void Erspan2Codec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERTYPE_ERSPAN_TYPE2);
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr,
+ get_protocol_ids,
sum, // sum
stats // stats
};
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
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<uint16_t>&);
// DELETE from here and below
#include "codecs/sf_protocols.h"
}
-void Erspan3Codec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERTYPE_ERSPAN_TYPE3);
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr, // get_dlt
+ get_protocol_ids,
sum, // sum
stats // stats
};
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
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<uint16_t>&);
- virtual void get_data_link_type(std::vector<int>&){};
};
//-------------------------------------------------------------------------
-void EthLoopbackCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERNET_TYPE_LOOP);
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr, // get_dlt
+ get_protocol_ids,
sum, // sum
stats // stats
};
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<uint16_t>&);
- virtual void get_data_link_type(std::vector<int>&){};
-
-
// DELETE from here and below
#include "codecs/sf_protocols.h"
virtual inline PROTO_ID get_proto_id() { return PROTO_GRE; };
-void GreCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(GRE_PROT_ID);
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr,
+ get_protocol_ids,
sum, // sum
stats // stats
};
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
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<uint16_t>&);
-
+ Packet *, uint16_t &p_hdr_len, int &next_prot_id);
// DELETE from here and below
#include "codecs/sf_protocols.h"
}
-void MplsCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERNET_TYPE_MPLS_UNICAST);
v.push_back(ETHERNET_TYPE_MPLS_MULTICAST);
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr, // get_dlt
+ get_protocol_ids,
sum, // sum
stats // stats
};
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
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<uint16_t>&);
-
// DELETE from here and below
#include "codecs/sf_protocols.h"
return true;
}
-
-
-
-void PppEncap::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERTYPE_PPP);
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr, // get_dlt
+ get_protocol_ids,
sum, // sum
stats // stats
};
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
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<uint16_t>&);
// DELETE from here and below
#include "codecs/sf_protocols.h"
}
#endif
-void PPPoEPkt::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERNET_TYPE_PPPoE_DISC);
v.push_back(ETHERNET_TYPE_PPPoE_SESS);
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr,
+ get_protocol_ids,
sum, // sum
stats // stats
};
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
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<uint16_t>&);
};
-void TransbridgeCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERTYPE_TRANS_ETHER_BRIDGING); // defined in ethertypes.h"
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ nullptr,
+ get_protocol_ids,
sum, // sum
stats // stats
};
-/* $Id: decode.c,v 1.285 2013-06-29 03:03:00 rcombs Exp $ */
-
/*
** Copyright (C) 2002-2013 Sourcefire, Inc.
** Copyright (C) 1998-2002 Martin Roesch <roesch@sourcefire.com>
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<uint16_t>&);
- virtual void get_data_link_type(std::vector<int>&){};
-
// DELETE from here and below
#include "codecs/sf_protocols.h"
-void VlanCodec::get_protocol_ids(std::vector<uint16_t>& v)
+static void get_protocol_ids(std::vector<uint16_t>& v)
{
v.push_back(ETHERNET_TYPE_8021Q);
}
NULL, // tterm
ctor, // ctor
dtor, // dtor
+ NULL, // get_dlt
+ get_protocol_ids,
sum, // sum
stats // stats
};
// do nothing unless methods overridden.
// ONE OF THESE METHODS MUST BE IMPLEMENTED!!
- virtual void get_protocol_ids(std::vector<uint16_t>&){};
- virtual void get_data_link_type(std::vector<int>&){};
virtual inline const char* get_name(){return name; };
typedef void (*cd_del_f)(Codec *);
typedef void (*cd_aux_f)();
-typedef void (*cd_get_protos)(std::vector<uint16_t>&);
-typedef void (*cd_get_dlt)(std::vector<int>&);
typedef bool (*decode_f)(const uint8_t *, const uint32_t, Packet *, uint16_t &, uint16_t &);
-
+typedef void (*cd_dlt_f)(std::vector<int>&v);
+typedef void (*cd_prot_id_f)(std::vector<uint16_t>&);
// add every protocol id, included IP protocols and
// ethertypes, to the passed in vector
cd_new_f ctor; // get eval optional instance data
cd_del_f dtor; // clean up instance data
+ cd_dlt_f dlt; // get the data link type
+ cd_prot_id_f proto_id; // get the protocol ids
+
cd_aux_f sum;
cd_aux_f stats;
};
+/*
+** Copyright (C) 2013-2013 Sourcefire, Inc.
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License Version 2 as
+** published by the Free Software Foundation. You may not use, modify or
+** distribute this program under any other version of the GNU General
+** Public License.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
#include <list>
+#include <vector>
using namespace std;
#include "packet_manager.h"
#include "protocols/packet.h"
#include "protocols/undefined_protocols.h"
+#include "time/profiler.h"
-#if 0
-#include "codecs/decode.h"
-#include "codecs/essential/root_eth.h"
-#include "codecs/root/root_raw4.h"
-#include "codecs/root/root_raw6.h"
-#include "codecs/root/root_null.h"
-#endif
-#include "time/profiler.h"
+namespace
+{
+struct CdGenPegs{
+ PegCount total_processed = 0;
+ PegCount other_codecs = 0;
+ PegCount discards = 0;
+};
+
+std::vector<const char*> CdGenPegNames =
+{
+ "total",
+ "other",
+ "discards"
+};
+} // anonymous namespace
-//static list<const CodecApi*> s_codecs;
-//static THREAD_LOCAL decode_f grinder;
#ifdef PERF_PROFILING
THREAD_LOCAL PreprocStats decodePerfStats;
#endif
+static const uint16_t max_protocol_id = 65535;
+static std::array<Codec*, max_protocol_id> s_protocols;
+static list<const CodecApi*> s_codecs;
+static THREAD_LOCAL CdGenPegs pkt_cnt;
+static CdGenPegs gpkt_cnt;
-//namespace
-//{
- static const uint16_t max_protocol_id = 65535;
- static std::array<Codec*, max_protocol_id> s_protocols;
- static list<const CodecApi*> s_codecs;
-
-//} // namespace
//-------------------------------------------------------------------------
// plugins
void PacketManager::release_plugins()
{
+ for ( auto* p : s_codecs )
+ {
+ p->gterm();
+ p->tterm();
+// p->dtor();
+ }
s_codecs.clear();
}
p->pkt = pkt;
len = pkthdr->caplen;
curr_prot_id = GRINDER_ID;
+ pkt_cnt.total_processed++;
- // 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))
+ // loop until the protocol id is no longer valid
+ while(curr_prot_id >= 0 && curr_prot_id < max_protocol_id)
{
+ if (s_protocols[curr_prot_id] == 0)
+ {
+ pkt_cnt.other_codecs++;
+ break;
+ }
+ else if( !s_protocols[curr_prot_id]->decode(pkt, len, p, p_hdr_len, next_prot_id))
+ {
+ pkt_cnt.discards++;
+ break;
+ }
-
- // if we have succesfully decoded this layer, push the layer
PacketClass::PushLayer(p, s_protocols[curr_prot_id], pkt, p_hdr_len);
curr_prot_id = next_prot_id;
len -= p_hdr_len;
pkt += p_hdr_len;
-
}
p->dsize = len;
p->data = pkt;
-
PREPROC_PROFILE_END(decodePerfStats);
}
-#if 0
-const CodecApi *PacketManager::get_data_link_type(int dlt)
-{
- vector<int> dlt_vec;
-
- for ( auto* p : s_codecs )
- {
- dlt_vec.clear();
-// p->get_dlt(dlt_vec);
-
- for (auto *it = dlt_vec.begin(); it != dlt_vec.end(); ++it)
- {
- if (*it == dlt)
- return p;
- }
- }
-
- return nullptr;
-}
-
-void PacketManager::set_grinder(void)
-{
- const char* slink = NULL;
- const char* extra = NULL;
-
- // initialize values
-
- int dlt = DAQ_GetBaseProtocol();
- const CodecApi *cd_api = get_data_link_type(dlt);
-
- if(cd_api != nullptr)
- {
- grinder = cd_api->ctor();
-
- if ( !ScReadMode() || ScPcapShow() )
- LogMessage("Decoding %s\n", slink);
- }
-
-
- FatalError("%s(%d) Could not find codec for Data Link Type %d.\n",
- __FILE__, __LINE__, dlt);
-}
-#endif
void PacketManager::set_grinder(void)
{
proto.clear();
- cd->get_protocol_ids(proto);
+ p->proto_id(proto);
for (auto proto_id : proto)
{
if(s_protocols[proto_id] != NULL)
dlt.clear();
- cd->get_data_link_type(dlt);
+ p->dlt(dlt);
// set the grinder if the data link types match
for (auto curr_dlt : dlt )
{
if (!codec_registered)
WarningMessage("The Codec %s is never used\n", cd->get_name());
- // ERRRO: If multiple correct grinders found.
}
-
-
-
-// FatalError("Codec installation checking!!");
}
void PacketManager::dump_stats()
{
-// for ( auto* cd : s_codecs )
-// cd->sum();
+ sum_stats((PegCount*)&gpkt_cnt, (PegCount*)&pkt_cnt, array_size(CdGenPegNames));
+
+ for ( auto* cd : s_codecs )
+ if (cd->sum != nullptr)
+ cd->sum();
+
+ std::vector<const char*> pegNames(CdGenPegNames);
+ std::vector<PegCount> pegs;
+ pegs.push_back(gpkt_cnt.total_processed);
+ pegs.push_back(gpkt_cnt.other_codecs);
+ pegs.push_back(gpkt_cnt.discards);
+
+ // using two temporary vectors to ensure codecs cannot
+ // see any other codecs statistics
+ std::vector<const char*> tmpNames;
+ std::vector<PegCount> tmpPegs;
+
+ for ( auto* cd : s_codecs )
+ {
+ if (cd->stats != nullptr)
+ {
+ tmpPegs.clear();
+ tmpNames.clear();
+// cd->stats(tmpPegs, tmpNames);
+ if (tmpNames.size() == tmpPegs.size())
+ {
+ pegs.insert(pegs.end(), tmpPegs.begin(), tmpPegs.end());
+ pegNames.insert(pegNames.end(), tmpNames.begin(), tmpNames.end());
+ }
+ else
+ {
+ WarningMessage("The %s Codecs stats function returned a "
+ "different %d PegCounts and %d PegNames. the two "
+ "values must be equal\n",
+ cd->base.name, pegs.size(), pegNames.size());
+ }
+ }
+ }
+ show_percent_stats(&pegs[0], &pegNames[0], pegNames.size(),
+ "codecs");
}
+
bool PacketManager::has_codec(uint16_t cd_id)
{
return s_protocols[cd_id] != 0;
#include "snort_types.h"
#include "framework/codec.h"
#include "time/profiler.h"
+#include "utils/stats.h"
#include <array>
#include <list>
static void init_codecs();
static void dump_stats();
-
static bool has_codec(uint16_t);
// static void encode_update(Packet *);
// static void encode_format(Packet *);
private:
- static const CodecApi *get_data_link_type(int dlt);
-
};