INET Framework for OMNeT++/OMNEST
inet::tcp::TcpCrcInsertionHook Class Reference

#include <TcpCrcInsertionHook.h>

Inheritance diagram for inet::tcp::TcpCrcInsertionHook:
inet::NetfilterBase::HookBase inet::INetfilter::IHook

Public Member Functions

virtual Result datagramPreRoutingHook (Packet *packet) override
 This is the first hook called by the network protocol before it routes a datagram that was received from the lower layer. More...
 
virtual Result datagramForwardHook (Packet *packet) override
 This is the second hook called by the network protocol before it sends a datagram to the lower layer. More...
 
virtual Result datagramPostRoutingHook (Packet *packet) override
 This is the last hook called by the network protocol before it sends a datagram to the lower layer. More...
 
virtual Result datagramLocalInHook (Packet *packet) override
 This is the last hook called by the network protocol before it sends a datagram to the upper layer. More...
 
virtual Result datagramLocalOutHook (Packet *packet) override
 This is the first hook called by the network protocol before it routes a datagram that was received from the upper layer. More...
 
- Public Member Functions inherited from inet::NetfilterBase::HookBase
virtual ~HookBase ()
 
void registeredTo (INetfilter *nf)
 
void unregisteredFrom (INetfilter *nf)
 
bool isRegisteredHook (INetfilter *nf)
 
- Public Member Functions inherited from inet::INetfilter::IHook
virtual ~IHook ()
 

Static Public Member Functions

static void insertCrc (const Protocol *networkProtocol, const L3Address &srcAddress, const L3Address &destAddress, const Ptr< TcpHeader > &tcpHeader, Packet *tcpPayload)
 
static uint16_t computeCrc (const Protocol *networkProtocol, const L3Address &srcAddress, const L3Address &destAddress, const Ptr< const TcpHeader > &tcpHeader, const Ptr< const Chunk > &tcpData)
 

Additional Inherited Members

- Public Types inherited from inet::INetfilter::IHook
enum  Type {
  PREROUTING, LOCALIN, FORWARD, POSTROUTING,
  LOCALOUT
}
 
enum  Result { ACCEPT, DROP, QUEUE, STOLEN }
 
- Protected Attributes inherited from inet::NetfilterBase::HookBase
std::vector< INetfilter * > netfilters
 

Member Function Documentation

◆ computeCrc()

uint16_t inet::tcp::TcpCrcInsertionHook::computeCrc ( const Protocol networkProtocol,
const L3Address srcAddress,
const L3Address destAddress,
const Ptr< const TcpHeader > &  tcpHeader,
const Ptr< const Chunk > &  tcpData 
)
static
74 {
75  auto pseudoHeader = makeShared<TransportPseudoHeader>();
76  pseudoHeader->setSrcAddress(srcAddress);
77  pseudoHeader->setDestAddress(destAddress);
78  pseudoHeader->setNetworkProtocolId(networkProtocol->getId());
79  pseudoHeader->setProtocolId(IP_PROT_TCP);
80  pseudoHeader->setPacketLength(tcpHeader->getChunkLength() + tcpData->getChunkLength());
81  // pseudoHeader length: ipv4: 12 bytes, ipv6: 40 bytes, generic: ???
82  if (networkProtocol == &Protocol::ipv4)
83  pseudoHeader->setChunkLength(B(12));
84  else if (networkProtocol == &Protocol::ipv6)
85  pseudoHeader->setChunkLength(B(40));
86  else
87  throw cRuntimeError("Unknown network protocol: %s", networkProtocol->getName());
88  MemoryOutputStream stream;
89  Chunk::serialize(stream, pseudoHeader);
90  Chunk::serialize(stream, tcpHeader);
91  Chunk::serialize(stream, tcpData);
92  uint16_t crc = TcpIpChecksum::checksum(stream.getData());
93  return crc;
94 }

Referenced by insertCrc().

◆ datagramForwardHook()

virtual Result inet::tcp::TcpCrcInsertionHook::datagramForwardHook ( Packet datagram)
inlineoverridevirtual

This is the second hook called by the network protocol before it sends a datagram to the lower layer.

This is done after the datagramPreRoutingHook or the datagramLocalInHook is called and the datagram is routed.

Implements inet::INetfilter::IHook.

27 { return ACCEPT; }

◆ datagramLocalInHook()

virtual Result inet::tcp::TcpCrcInsertionHook::datagramLocalInHook ( Packet datagram)
inlineoverridevirtual

This is the last hook called by the network protocol before it sends a datagram to the upper layer.

This is done after the datagramPreRoutingHook is called and the datagram is routed.

Implements inet::INetfilter::IHook.

29 { return ACCEPT; }

◆ datagramLocalOutHook()

virtual Result inet::tcp::TcpCrcInsertionHook::datagramLocalOutHook ( Packet datagram)
inlineoverridevirtual

This is the first hook called by the network protocol before it routes a datagram that was received from the upper layer.

The nextHopAddress is ignored when the outputNetworkInterface is a nullptr. After this is done

Implements inet::INetfilter::IHook.

30 { return ACCEPT; }

◆ datagramPostRoutingHook()

INetfilter::IHook::Result inet::tcp::TcpCrcInsertionHook::datagramPostRoutingHook ( Packet datagram)
overridevirtual

This is the last hook called by the network protocol before it sends a datagram to the lower layer.

Implements inet::INetfilter::IHook.

26 {
27  Enter_Method("datagramPostRoutingHook");
28 
29  if (packet->findTag<InterfaceInd>())
30  return ACCEPT; // FORWARD
31  auto networkProtocol = packet->getTag<PacketProtocolTag>()->getProtocol();
32  const auto& networkHeader = getNetworkProtocolHeader(packet);
33  if (networkHeader->getProtocol() == &Protocol::tcp) {
34  ASSERT(!networkHeader->isFragment());
35  packet->eraseAtFront(networkHeader->getChunkLength());
36  auto tcpHeader = packet->removeAtFront<TcpHeader>();
37  ASSERT(tcpHeader->getCrcMode() == CRC_COMPUTED);
38  const L3Address& srcAddress = networkHeader->getSourceAddress();
39  const L3Address& destAddress = networkHeader->getDestinationAddress();
40  insertCrc(networkProtocol, srcAddress, destAddress, tcpHeader, packet);
41  packet->insertAtFront(tcpHeader);
42  packet->insertAtFront(networkHeader);
43  }
44  return ACCEPT;
45 }

◆ datagramPreRoutingHook()

virtual Result inet::tcp::TcpCrcInsertionHook::datagramPreRoutingHook ( Packet datagram)
inlineoverridevirtual

This is the first hook called by the network protocol before it routes a datagram that was received from the lower layer.

The nextHopAddress is ignored when the outputNetworkInterface is nullptr.

Implements inet::INetfilter::IHook.

26 { return ACCEPT; }

◆ insertCrc()

void inet::tcp::TcpCrcInsertionHook::insertCrc ( const Protocol networkProtocol,
const L3Address srcAddress,
const L3Address destAddress,
const Ptr< TcpHeader > &  tcpHeader,
Packet tcpPayload 
)
static
48 {
49  auto crcMode = tcpHeader->getCrcMode();
50  switch (crcMode) {
52  // if the CRC mode is declared to be correct, then set the CRC to an easily recognizable value
53  tcpHeader->setCrc(0xC00D);
54  break;
56  // if the CRC mode is declared to be incorrect, then set the CRC to an easily recognizable value
57  tcpHeader->setCrc(0xBAAD);
58  break;
59  case CRC_COMPUTED: {
60  // if the CRC mode is computed, then compute the CRC and set it
61  // this computation is delayed after the routing decision, see INetfilter hook
62  tcpHeader->setCrc(0x0000); // make sure that the CRC is 0 in the TCP header before computing the CRC
63  auto tcpData = packet->peekData(Chunk::PF_ALLOW_EMPTY);
64  auto crc = computeCrc(networkProtocol, srcAddress, destAddress, tcpHeader, tcpData);
65  tcpHeader->setCrc(crc);
66  break;
67  }
68  default:
69  throw cRuntimeError("Unknown CRC mode");
70  }
71 }

Referenced by datagramPostRoutingHook(), and inet::Ipv4NatTable::processPacket().


The documentation for this class was generated from the following files:
inet::tcp::TcpCrcInsertionHook::insertCrc
static void insertCrc(const Protocol *networkProtocol, const L3Address &srcAddress, const L3Address &destAddress, const Ptr< TcpHeader > &tcpHeader, Packet *tcpPayload)
Definition: TcpCrcInsertionHook.cc:47
inet::Protocol::tcp
static const Protocol tcp
Definition: Protocol.h:112
inet::Protocol::ipv4
static const Protocol ipv4
Definition: Protocol.h:93
inet::Protocol::ipv6
static const Protocol ipv6
Definition: Protocol.h:94
inet::CRC_COMPUTED
@ CRC_COMPUTED
Definition: CrcMode_m.h:59
inet::Chunk::serialize
static void serialize(MemoryOutputStream &stream, const Ptr< const Chunk > &chunk, b offset=b(0), b length=b(-1))
Serializes a chunk into the given stream.
Definition: Chunk.cc:175
inet::tcp::TcpCrcInsertionHook::computeCrc
static uint16_t computeCrc(const Protocol *networkProtocol, const L3Address &srcAddress, const L3Address &destAddress, const Ptr< const TcpHeader > &tcpHeader, const Ptr< const Chunk > &tcpData)
Definition: TcpCrcInsertionHook.cc:73
PacketProtocolTag
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down PacketProtocolTag
Definition: IUdp-gates.txt:25
inet::units::units::B
intscale< b, 1, 8 > B
Definition: Units.h:1168
inet::getNetworkProtocolHeader
const Ptr< const NetworkHeaderBase > getNetworkProtocolHeader(Packet *packet)
Definition: L3Tools.cc:43
inet::Chunk::PF_ALLOW_EMPTY
@ PF_ALLOW_EMPTY
Definition: Chunk.h:279
inet::TcpIpChecksum::checksum
static uint16_t checksum(const void *addr, unsigned int count)
Definition: TcpIpChecksum.h:33
inet::CRC_DECLARED_CORRECT
@ CRC_DECLARED_CORRECT
Definition: CrcMode_m.h:57
inet::IP_PROT_TCP
@ IP_PROT_TCP
Definition: IpProtocolId_m.h:94
inet::CRC_DECLARED_INCORRECT
@ CRC_DECLARED_INCORRECT
Definition: CrcMode_m.h:58
Enter_Method
#define Enter_Method(...)
Definition: SelfDoc.h:71
inet::INetfilter::IHook::ACCEPT
@ ACCEPT
allows the datagram to pass to the next hook
Definition: INetfilter.h:40