INET Framework for OMNeT++/OMNEST
inet::Ipv4NatTable Class Reference

#include <Ipv4NatTable.h>

Inheritance diagram for inet::Ipv4NatTable:
inet::NetfilterBase::HookBase inet::INetfilter::IHook

Public Member Functions

virtual ~Ipv4NatTable ()
 
virtual Result datagramPreRoutingHook (Packet *datagram) 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 *datagram) 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 *datagram) 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 *datagram) 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 *datagram) 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 ()
 

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *message) override
 
virtual void parseConfig ()
 
virtual Result processPacket (Packet *packet, INetfilter::IHook::Type type)
 

Protected Attributes

cXMLElement * config = nullptr
 
ModuleRefByPar< INetfilternetworkProtocol
 
std::multimap< INetfilter::IHook::Type, std::pair< PacketFilter *, Ipv4NatEntry > > natEntries
 
- Protected Attributes inherited from inet::NetfilterBase::HookBase
std::vector< INetfilter * > netfilters
 

Additional Inherited Members

- Public Types inherited from inet::INetfilter::IHook
enum  Type {
  PREROUTING, LOCALIN, FORWARD, POSTROUTING,
  LOCALOUT
}
 
enum  Result { ACCEPT, DROP, QUEUE, STOLEN }
 

Constructor & Destructor Documentation

◆ ~Ipv4NatTable()

inet::Ipv4NatTable::~Ipv4NatTable ( )
virtual
31 {
32  for (auto& it : natEntries)
33  delete it.second.first;
34 }

Member Function Documentation

◆ datagramForwardHook()

virtual Result inet::Ipv4NatTable::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.

36 { return processPacket(datagram, FORWARD); }

◆ datagramLocalInHook()

virtual Result inet::Ipv4NatTable::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.

38 { return processPacket(datagram, LOCALIN); }

◆ datagramLocalOutHook()

virtual Result inet::Ipv4NatTable::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.

39 { return processPacket(datagram, LOCALOUT); }

◆ datagramPostRoutingHook()

virtual Result inet::Ipv4NatTable::datagramPostRoutingHook ( Packet datagram)
inlineoverridevirtual

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

Implements inet::INetfilter::IHook.

37 { return processPacket(datagram, POSTROUTING); }

◆ datagramPreRoutingHook()

virtual Result inet::Ipv4NatTable::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.

35 { return processPacket(datagram, PREROUTING); }

◆ handleMessage()

void inet::Ipv4NatTable::handleMessage ( cMessage *  message)
overrideprotectedvirtual
53 {
54  throw cRuntimeError("This module can not handle messages");
55 }

◆ initialize()

void inet::Ipv4NatTable::initialize ( int  stage)
overrideprotectedvirtual
37 {
38  cSimpleModule::initialize(stage);
39  if (stage == INITSTAGE_LOCAL) {
40  config = par("config");
41  networkProtocol.reference(this, "networkProtocolModule", true);
42  }
43  else if (stage == INITSTAGE_NETWORK_LAYER) {
44  parseConfig();
45  if (natEntries.size() != 0)
46  networkProtocol->registerHook(0, this);
47  auto text = std::to_string(natEntries.size()) + " entries";
48  getDisplayString().setTagArg("t", 0, text.c_str());
49  }
50 }

◆ numInitStages()

virtual int inet::Ipv4NatTable::numInitStages ( ) const
inlineoverrideprotectedvirtual
27 { return NUM_INIT_STAGES; }

◆ parseConfig()

void inet::Ipv4NatTable::parseConfig ( )
protectedvirtual
58 {
59  cXMLElementList xmlEntries = config->getChildrenByTagName("entry");
60  for (auto& xmlEntry : xmlEntries) {
61  // type
62  const char *typeAttr = xmlEntry->getAttribute("type");
64  if (!strcmp("prerouting", typeAttr))
65  type = PREROUTING;
66  else if (!strcmp("localin", typeAttr))
67  type = LOCALIN;
68  else if (!strcmp("forward", typeAttr))
69  type = FORWARD;
70  else if (!strcmp("postrouting", typeAttr))
71  type = POSTROUTING;
72  else if (!strcmp("localout", typeAttr))
73  type = LOCALOUT;
74  else
75  throw cRuntimeError("Unknown type");
76  // filter
77  PacketFilter *packetFilter = new PacketFilter();
78  const char *packetFilterAttr = xmlEntry->getAttribute("packetFilter");
79  packetFilter->setExpression(packetFilterAttr != nullptr ? packetFilterAttr : "*");
80  // NAT entry
81  Ipv4NatEntry natEntry;
82  const char *destAddressAttr = xmlEntry->getAttribute("destAddress");
83  if (destAddressAttr != nullptr && *destAddressAttr != '\0')
84  natEntry.setDestAddress(Ipv4Address(destAddressAttr));
85  const char *destPortAttr = xmlEntry->getAttribute("destPort");
86  if (destPortAttr != nullptr && *destPortAttr != '\0')
87  natEntry.setDestPort(atoi(destPortAttr));
88  const char *srcAddressAttr = xmlEntry->getAttribute("srcAddress");
89  if (srcAddressAttr != nullptr && *srcAddressAttr != '\0')
90  natEntry.setSrcAddress(Ipv4Address(srcAddressAttr));
91  const char *srcPortAttr = xmlEntry->getAttribute("srcPort");
92  if (srcPortAttr != nullptr && *srcPortAttr != '\0')
93  natEntry.setSrcPort(atoi(srcPortAttr));
94  // insert
95  natEntries.insert({type, {packetFilter, natEntry}});
96  }
97 }

Referenced by initialize().

◆ processPacket()

INetfilter::IHook::Result inet::Ipv4NatTable::processPacket ( Packet packet,
INetfilter::IHook::Type  type 
)
protectedvirtual
100 {
101  Enter_Method("processPacket");
102  auto lt = natEntries.lower_bound(type);
103  auto ut = natEntries.upper_bound(type);
104  for (; lt != ut; lt++) {
105  const auto& packetFilter = lt->second.first;
106  const auto& natEntry = lt->second.second;
107  // TODO this might be slow for too many filters
108  if (packetFilter->matches(packet)) {
109  auto& ipv4Header = removeNetworkProtocolHeader<Ipv4Header>(packet);
110  if (!natEntry.getDestAddress().isUnspecified())
111  ipv4Header->setDestAddress(natEntry.getDestAddress());
112  if (!natEntry.getSrcAddress().isUnspecified())
113  ipv4Header->setSrcAddress(natEntry.getSrcAddress());
114  auto transportProtocol = ipv4Header->getProtocol();
115 #ifdef INET_WITH_UDP
116  if (transportProtocol == &Protocol::udp) {
117  auto& udpHeader = removeTransportProtocolHeader<UdpHeader>(packet);
118  // TODO if (!Udp::verifyCrc(Protocol::ipv4, udpHeader, packet))
119  auto udpData = packet->peekData();
120  if (natEntry.getDestPort() != -1)
121  udpHeader->setDestPort(natEntry.getDestPort());
122  if (natEntry.getSrcPort() != -1)
123  udpHeader->setSrcPort(natEntry.getSrcPort());
124  Udp::insertCrc(&Protocol::ipv4, ipv4Header->getSrcAddress(), ipv4Header->getDestAddress(), udpHeader, packet);
125  insertTransportProtocolHeader(packet, Protocol::udp, udpHeader);
126  }
127  else
128 #endif
129 #ifdef INET_WITH_TCP_COMMON
130  if (transportProtocol == &Protocol::tcp) {
131  auto& tcpHeader = removeTransportProtocolHeader<tcp::TcpHeader>(packet);
132  // TODO if (!Tcp::verifyCrc(Protocol::ipv4, tcpHeader, packet))
133  if (natEntry.getDestPort() != -1)
134  tcpHeader->setDestPort(natEntry.getDestPort());
135  if (natEntry.getSrcPort() != -1)
136  tcpHeader->setSrcPort(natEntry.getSrcPort());
137  tcp::TcpCrcInsertionHook::insertCrc(&Protocol::ipv4, ipv4Header->getSrcAddress(), ipv4Header->getDestAddress(), tcpHeader, packet);
138  insertTransportProtocolHeader(packet, Protocol::tcp, tcpHeader);
139  }
140  else
141 #endif
142  throw cRuntimeError("Unknown protocol: '%s'", transportProtocol ? transportProtocol->getName() : std::to_string((int)ipv4Header->getProtocolId()).c_str());
143  insertNetworkProtocolHeader(packet, Protocol::ipv4, ipv4Header);
144  break;
145  }
146  }
147  return ACCEPT;
148 }

Member Data Documentation

◆ config

cXMLElement* inet::Ipv4NatTable::config = nullptr
protected

Referenced by initialize(), and parseConfig().

◆ natEntries

std::multimap<INetfilter::IHook::Type, std::pair<PacketFilter *, Ipv4NatEntry> > inet::Ipv4NatTable::natEntries
protected

◆ networkProtocol

ModuleRefByPar<INetfilter> inet::Ipv4NatTable::networkProtocol
protected

Referenced by initialize().


The documentation for this class was generated from the following files:
inet::INetfilter::IHook::Type
Type
Definition: INetfilter.h:31
inet::Ipv4NatTable::parseConfig
virtual void parseConfig()
Definition: Ipv4NatTable.cc:57
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::INITSTAGE_NETWORK_LAYER
INET_API InitStage INITSTAGE_NETWORK_LAYER
Initialization of network layer protocols.
inet::INetfilter::IHook::FORWARD
@ FORWARD
Definition: INetfilter.h:34
inet::insertNetworkProtocolHeader
void insertNetworkProtocolHeader(Packet *packet, const Protocol &protocol, const Ptr< NetworkHeaderBase > &header)
Definition: L3Tools.cc:70
inet::Udp::insertCrc
static void insertCrc(const Protocol *networkProtocol, const L3Address &srcAddress, const L3Address &destAddress, const Ptr< UdpHeader > &udpHeader, Packet *udpPayload)
Definition: Udp.cc:809
inet::Ipv4NatTable::processPacket
virtual Result processPacket(Packet *packet, INetfilter::IHook::Type type)
Definition: Ipv4NatTable.cc:99
inet::Protocol::udp
static const Protocol udp
Definition: Protocol.h:117
inet::insertTransportProtocolHeader
void insertTransportProtocolHeader(Packet *packet, const Protocol &protocol, const Ptr< TransportHeaderBase > &header)
Definition: L4Tools.cc:77
inet::Ipv4NatTable::networkProtocol
ModuleRefByPar< INetfilter > networkProtocol
Definition: Ipv4NatTable.h:22
type
removed type
Definition: IUdp-gates.txt:7
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::INetfilter::IHook::PREROUTING
@ PREROUTING
Definition: INetfilter.h:32
inet::Ipv4NatTable::config
cXMLElement * config
Definition: Ipv4NatTable.h:21
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
inet::INetfilter::IHook::LOCALIN
@ LOCALIN
Definition: INetfilter.h:33
inet::INetfilter::IHook::POSTROUTING
@ POSTROUTING
Definition: INetfilter.h:35
inet::INetfilter::IHook::LOCALOUT
@ LOCALOUT
Definition: INetfilter.h:36
inet::Ipv4NatTable::natEntries
std::multimap< INetfilter::IHook::Type, std::pair< PacketFilter *, Ipv4NatEntry > > natEntries
Definition: Ipv4NatTable.h:24