Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/limit tcp reassembly #662

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RFCs/2021-04-16-76-collection-policies.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ visor:
input_type: pcap
filter:
bpf: "port 53"
config:
tcp_packet_reassembly_cache_limit: 100000
# stream handlers to attach to this input stream
# these decide exactly which data to summarize and expose for collection
handlers:
Expand Down
33 changes: 33 additions & 0 deletions src/handlers/dns/v2/tests/test_dns_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,39 @@ TEST_CASE("Parse DNS TCP IPv4 tests", "[pcap][ipv4][tcp][dns]")
CHECK(j["unknown"]["top_qname2_xacts"][0]["estimate"] == 210);
}

TEST_CASE("Parse DNS TCP tests with limit", "[pcap][ipv4][tcp][dns]")
{
PcapInputStream stream{"pcap-test"};
stream.config_set("pcap_file", "tests/fixtures/dns_ipv4_tcp.pcap");
stream.config_set("bpf", "");
stream.config_set<uint64_t>("tcp_packet_reassembly_cache_limit", 10);

visor::Config c;
auto stream_proxy = stream.add_event_proxy(c);
c.config_set<uint64_t>("num_periods", 1);
DnsStreamHandler dns_handler{"dns-test", stream_proxy, &c};

dns_handler.start();
stream.start();
dns_handler.stop();
stream.stop();

auto counters = dns_handler.metrics()->bucket(0)->counters(TransactionDirection::unknown);
auto event_data = dns_handler.metrics()->bucket(0)->event_data_locked();
json j;
dns_handler.metrics()->bucket(0)->to_json(j);

CHECK(event_data.num_events->value() == 140);
CHECK(counters.TCP.value() == 70);
CHECK(counters.IPv4.value() == 70);
CHECK(counters.IPv6.value() == 0);
CHECK(counters.xacts.value() == 70);
CHECK(counters.timeout.value() == 0);
CHECK(counters.orphan.value() == 0);
CHECK(j["unknown"]["top_qname2_xacts"][0]["name"] == ".test.com");
CHECK(j["unknown"]["top_qname2_xacts"][0]["estimate"] == 70);
}

TEST_CASE("Parse DNS UDP IPv6 tests", "[pcap][ipv6][udp][dns]")
{

Expand Down
27 changes: 21 additions & 6 deletions src/inputs/pcap/PcapInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static void _pcap_stats_update(pcpp::IPcapDevice::PcapStats &stats, void *cookie

PcapInputStream::PcapInputStream(const std::string &name)
: visor::InputStream(name)
, _lru_list(std::make_unique<LRUList<uint32_t, timeval>>(DEFAULT_LRULIST_SIZE))
, _pcapDevice(nullptr)
, _tcp_reassembly(_tcp_message_ready_cb,
this,
Expand All @@ -93,6 +94,10 @@ void PcapInputStream::start()

validate_configs(_config_defs);

if (config_exists("tcp_packet_reassembly_cache_limit")) {
auto limit = config_get<uint64_t>("tcp_packet_reassembly_cache_limit");
_lru_list = std::make_unique<LRUList<uint32_t, timeval>>(limit);
}
if (config_exists("pcap_file")) {
// read from pcap file. this is a special case from a command line utility
if (!config_exists("bpf")) {
Expand Down Expand Up @@ -252,7 +257,9 @@ void PcapInputStream::tcp_message_ready(int8_t side, const pcpp::TcpStreamData &
for (auto &proxy : _event_proxies) {
dynamic_cast<PcapInputEventProxy *>(proxy.get())->tcp_message_ready_cb(side, tcpData, _packet_dir_cache);
}
_lru_list.put(tcpData.getConnectionData().flowKey, tcpData.getConnectionData().endTime);
if (_lru_list->put(tcpData.getConnectionData().flowKey, tcpData.getConnectionData().endTime, &_deleted_data)){
_lru_overflow.push_back(_deleted_data.first);
}
}

void PcapInputStream::tcp_connection_start(const pcpp::ConnectionData &connectionData)
Expand All @@ -261,7 +268,9 @@ void PcapInputStream::tcp_connection_start(const pcpp::ConnectionData &connectio
for (auto &proxy : _event_proxies) {
dynamic_cast<PcapInputEventProxy *>(proxy.get())->tcp_connection_start_cb(connectionData, _packet_dir_cache);
}
_lru_list.put(connectionData.flowKey, connectionData.startTime);
if (_lru_list->put(connectionData.flowKey, connectionData.startTime, &_deleted_data)) {
_lru_overflow.push_back(_deleted_data.first);
}
}

void PcapInputStream::tcp_connection_end(const pcpp::ConnectionData &connectionData, pcpp::TcpReassembly::ConnectionEndReason reason)
Expand All @@ -270,7 +279,7 @@ void PcapInputStream::tcp_connection_end(const pcpp::ConnectionData &connectionD
for (auto &proxy : _event_proxies) {
static_cast<PcapInputEventProxy *>(proxy.get())->tcp_connection_end_cb(connectionData, reason);
}
_lru_list.eraseElement(connectionData.flowKey);
_lru_list->eraseElement(connectionData.flowKey);
}

void PcapInputStream::process_pcap_stats(const pcpp::IPcapDevice::PcapStats &stats)
Expand Down Expand Up @@ -438,15 +447,21 @@ void PcapInputStream::process_raw_packet(pcpp::RawPacket *rawPacket)
}

for (uint8_t counter = 0; counter < MAX_TCP_CLEANUPS; counter++) {
if (_lru_list.getSize() == 0) {
if (_lru_list->getSize() == 0) {
break;
}
auto connection = _lru_list.getLRUElement();
auto connection = _lru_list->getLRUElement();
if (timestamp.tv_sec < connection.second.tv_sec + TCP_TIMEOUT) {
break;
}
_tcp_reassembly.closeConnection(connection.first);
_lru_list.eraseElement(connection.first);
_lru_list->eraseElement(connection.first);
}
if (_lru_overflow.size() > 0) {
for (const auto &fKey : _lru_overflow) {
_tcp_reassembly.closeConnection(fKey);
}
_lru_overflow.clear();
}
} else {
// unsupported layer3 protocol
Expand Down
8 changes: 6 additions & 2 deletions src/inputs/pcap/PcapInputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ class PcapInputStream : public visor::InputStream
private:
static constexpr uint8_t TCP_TIMEOUT = 30;
static constexpr uint8_t MAX_TCP_CLEANUPS = 100;
static constexpr size_t DEFAULT_LRULIST_SIZE = TCP_TIMEOUT * 10000;

static const PcapSource DefaultPcapSource = PcapSource::libpcap;
LRUList<uint32_t, timeval> _lru_list;
std::unique_ptr<LRUList<uint32_t, timeval>> _lru_list;
std::pair<uint32_t, timeval> _deleted_data;
std::vector<uint32_t> _lru_overflow;
lib::utils::IPv4subnetList _hostIPv4;
lib::utils::IPv6subnetList _hostIPv6;
PacketDirection _packet_dir_cache{PacketDirection::unknown};
Expand Down Expand Up @@ -127,7 +130,8 @@ class PcapInputStream : public visor::InputStream
"debug",
"host_spec",
"pcap_file",
"pcap_source"};
"pcap_source",
"tcp_packet_reassembly_cache_limit"};

protected:
void _open_pcap(const std::string &fileName, const std::string &bpfFilter);
Expand Down