From 0c55cce6195612ce6ad5f3aee99ed31501595e3d Mon Sep 17 00:00:00 2001 From: Levente Meszaros Date: Wed, 11 Dec 2024 17:01:00 +0100 Subject: [PATCH] all: Fixed #932. Changed the Gptp header lookup to use a packet dissector to avoid mishandling fragmented packets. --- .../packet/dissector/PacketDissector.cc | 8 +++++++ .../common/packet/dissector/PacketDissector.h | 17 +++++++++++++ src/inet/linklayer/ieee8021as/Gptp.cc | 24 +++++++------------ 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/inet/common/packet/dissector/PacketDissector.cc b/src/inet/common/packet/dissector/PacketDissector.cc index c82bf93d306..eb388a4e67a 100644 --- a/src/inet/common/packet/dissector/PacketDissector.cc +++ b/src/inet/common/packet/dissector/PacketDissector.cc @@ -83,6 +83,14 @@ void PacketDissector::ChunkBuilder::visitChunk(const Ptr& chunk, co } } +// ChunkFinder + +void PacketDissector::ChunkFinder::visitChunk(const Ptr& chunk, const Protocol *protocol) +{ + if (this->protocol == protocol) + this->chunk = chunk; +} + // PduTreeBuilder void PacketDissector::PduTreeBuilder::startProtocolDataUnit(const Protocol *protocol) diff --git a/src/inet/common/packet/dissector/PacketDissector.h b/src/inet/common/packet/dissector/PacketDissector.h index 9c59bb88e59..a819735c66a 100644 --- a/src/inet/common/packet/dissector/PacketDissector.h +++ b/src/inet/common/packet/dissector/PacketDissector.h @@ -112,6 +112,23 @@ class INET_API PacketDissector virtual void visitChunk(const Ptr& chunk, const Protocol *protocol) override; }; + class INET_API ChunkFinder : public PacketDissector::ICallback { + protected: + const Protocol *protocol; + Ptr chunk; + + public: + ChunkFinder(const Protocol *protocol) : protocol(protocol) { } + + const Ptr getChunk() { return chunk; } + + virtual bool shouldDissectProtocolDataUnit(const Protocol *protocol) override { return true; } + virtual void startProtocolDataUnit(const Protocol *protocol) override {} + virtual void endProtocolDataUnit(const Protocol *protocol) override {} + virtual void markIncorrect() override {} + virtual void visitChunk(const Ptr& chunk, const Protocol *protocol) override; + }; + class INET_API PduTreeBuilder : public PacketDissector::ICallback { protected: bool isEndProtocolDataUnitCalled = false; diff --git a/src/inet/linklayer/ieee8021as/Gptp.cc b/src/inet/linklayer/ieee8021as/Gptp.cc index f2e93f83df3..92a8cd1b3da 100644 --- a/src/inet/linklayer/ieee8021as/Gptp.cc +++ b/src/inet/linklayer/ieee8021as/Gptp.cc @@ -5,18 +5,18 @@ // University of Rostock, Germany // -#include "Gptp.h" - -#include "GptpPacket_m.h" +#include "inet/linklayer/ieee8021as/Gptp.h" #include "inet/clock/model/SettableClock.h" -#include "inet/common/IProtocolRegistrationListener.h" #include "inet/common/clock/ClockUserModuleBase.h" +#include "inet/common/IProtocolRegistrationListener.h" +#include "inet/common/packet/dissector/PacketDissector.h" #include "inet/linklayer/common/InterfaceTag_m.h" #include "inet/linklayer/common/MacAddress.h" #include "inet/linklayer/common/MacAddressTag_m.h" #include "inet/linklayer/ethernet/common/Ethernet.h" #include "inet/linklayer/ethernet/common/EthernetMacHeader_m.h" +#include "inet/linklayer/ieee8021as/GptpPacket_m.h" #include "inet/networklayer/common/NetworkInterface.h" #include "inet/physicallayer/wired/ethernet/EthernetPhyHeader_m.h" @@ -493,17 +493,11 @@ void Gptp::processPdelayRespFollowUp(Packet *packet, const GptpPdelayRespFollowU const GptpBase *Gptp::extractGptpHeader(Packet *packet) { - auto protocol = packet->getTag()->getProtocol(); - if (*protocol != Protocol::ethernetPhy) - return nullptr; - - const auto& ethPhyHeader = packet->peekAtFront(); - const auto& ethMacHeader = packet->peekDataAt(ethPhyHeader->getChunkLength()); - if (ethMacHeader->getTypeOrLength() != ETHERTYPE_GPTP) - return nullptr; - - b offset = ethPhyHeader->getChunkLength() + ethMacHeader->getChunkLength(); - return packet->peekDataAt(offset).get(); + PacketDissector::ChunkFinder chunkFinder(&Protocol::gptp); + PacketDissector packetDissector(ProtocolDissectorRegistry::getInstance(), chunkFinder); + packetDissector.dissectPacket(packet); + const auto& chunk = staticPtrCast(chunkFinder.getChunk()); + return chunk != nullptr ? chunk.get() : nullptr; } void Gptp::receiveSignal(cComponent *source, simsignal_t simSignal, cObject *obj, cObject *details)