A Discrete-Event Network Simulator
API
ipv4-l3-protocol.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006 Georgia Tech Research Corporation
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 2 as
6 // published by the Free Software Foundation;
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // Author: George F. Riley<riley@ece.gatech.edu>
18 //
19 
20 #ifndef IPV4_L3_PROTOCOL_H
21 #define IPV4_L3_PROTOCOL_H
22 
23 #include "ns3/ipv4-address.h"
24 #include "ns3/ipv4-header.h"
25 #include "ns3/ipv4-routing-protocol.h"
26 #include "ns3/ipv4.h"
27 #include "ns3/net-device.h"
28 #include "ns3/nstime.h"
29 #include "ns3/ptr.h"
30 #include "ns3/simulator.h"
31 #include "ns3/traced-callback.h"
32 
33 #include <list>
34 #include <map>
35 #include <stdint.h>
36 #include <vector>
37 
39 
40 namespace ns3
41 {
42 
43 class Packet;
44 class NetDevice;
45 class Ipv4Interface;
46 class Ipv4Address;
47 class Ipv4Header;
48 class Ipv4RoutingTableEntry;
49 class Ipv4Route;
50 class Node;
51 class Socket;
52 class Ipv4RawSocketImpl;
53 class IpL4Protocol;
54 class Icmpv4L4Protocol;
55 
81 class Ipv4L3Protocol : public Ipv4
82 {
83  public:
88  static TypeId GetTypeId();
89  static const uint16_t PROT_NUMBER;
90 
92  ~Ipv4L3Protocol() override;
93 
94  // Delete copy constructor and assignment operator to avoid misuse
95  Ipv4L3Protocol(const Ipv4L3Protocol&) = delete;
97 
103  {
111  };
112 
117  void SetNode(Ptr<Node> node);
118 
119  // functions defined in base class Ipv4
120 
121  void SetRoutingProtocol(Ptr<Ipv4RoutingProtocol> routingProtocol) override;
123 
124  Ptr<Socket> CreateRawSocket() override;
125  void DeleteRawSocket(Ptr<Socket> socket) override;
126 
127  void Insert(Ptr<IpL4Protocol> protocol) override;
128  void Insert(Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex) override;
129 
130  void Remove(Ptr<IpL4Protocol> protocol) override;
131  void Remove(Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex) override;
132 
133  Ptr<IpL4Protocol> GetProtocol(int protocolNumber) const override;
134  Ptr<IpL4Protocol> GetProtocol(int protocolNumber, int32_t interfaceIndex) const override;
135 
136  Ipv4Address SourceAddressSelection(uint32_t interface, Ipv4Address dest) override;
137 
144  void SetDefaultTtl(uint8_t ttl);
145 
159  void Receive(Ptr<NetDevice> device,
161  uint16_t protocol,
162  const Address& from,
163  const Address& to,
164  NetDevice::PacketType packetType);
165 
176  void Send(Ptr<Packet> packet,
177  Ipv4Address source,
178  Ipv4Address destination,
179  uint8_t protocol,
180  Ptr<Ipv4Route> route) override;
189  void SendWithHeader(Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route) override;
190 
191  uint32_t AddInterface(Ptr<NetDevice> device) override;
197  Ptr<Ipv4Interface> GetInterface(uint32_t i) const;
198  uint32_t GetNInterfaces() const override;
199 
200  int32_t GetInterfaceForAddress(Ipv4Address addr) const override;
201  int32_t GetInterfaceForPrefix(Ipv4Address addr, Ipv4Mask mask) const override;
202  int32_t GetInterfaceForDevice(Ptr<const NetDevice> device) const override;
203  bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const override;
204 
205  bool AddAddress(uint32_t i, Ipv4InterfaceAddress address) override;
206  Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const override;
207  uint32_t GetNAddresses(uint32_t interface) const override;
208  bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex) override;
209  bool RemoveAddress(uint32_t interface, Ipv4Address address) override;
211  Ipv4Address dst,
213 
214  void SetMetric(uint32_t i, uint16_t metric) override;
215  uint16_t GetMetric(uint32_t i) const override;
216  uint16_t GetMtu(uint32_t i) const override;
217  bool IsUp(uint32_t i) const override;
218  void SetUp(uint32_t i) override;
219  void SetDown(uint32_t i) override;
220  bool IsForwarding(uint32_t i) const override;
221  void SetForwarding(uint32_t i, bool val) override;
222 
223  Ptr<NetDevice> GetNetDevice(uint32_t i) override;
224 
235  bool IsUnicast(Ipv4Address ad) const;
236 
244  typedef void (*SentTracedCallback)(const Ipv4Header& header,
245  Ptr<const Packet> packet,
246  uint32_t interface);
247 
257  typedef void (*TxRxTracedCallback)(Ptr<const Packet> packet,
258  Ptr<Ipv4> ipv4,
259  uint32_t interface);
260 
272  typedef void (*DropTracedCallback)(const Ipv4Header& header,
273  Ptr<const Packet> packet,
274  DropReason reason,
275  Ptr<Ipv4> ipv4,
276  uint32_t interface);
277 
278  protected:
279  void DoDispose() override;
285  void NotifyNewAggregate() override;
286 
287  private:
292  friend class ::Ipv4L3ProtocolTestCase;
293 
294  // class Ipv4 attributes
295  void SetIpForward(bool forward) override;
296  bool GetIpForward() const override;
297  void SetWeakEsModel(bool model) override;
298  bool GetWeakEsModel() const override;
299 
306  void DecreaseIdentification(Ipv4Address source, Ipv4Address destination, uint8_t protocol);
307 
320  Ipv4Address destination,
321  uint8_t protocol,
322  uint16_t payloadSize,
323  uint8_t ttl,
324  uint8_t tos,
325  bool mayFragment);
326 
333  void SendRealOut(Ptr<Ipv4Route> route, Ptr<Packet> packet, const Ipv4Header& ipHeader);
334 
341  void IpForward(Ptr<Ipv4Route> rtentry, Ptr<const Packet> p, const Ipv4Header& header);
342 
351  const Ipv4Header& header);
352 
359  void LocalDeliver(Ptr<const Packet> p, const Ipv4Header& ip, uint32_t iif);
360 
368  const Ipv4Header& ipHeader,
369  Socket::SocketErrno sockErrno);
370 
376  uint32_t AddIpv4Interface(Ptr<Ipv4Interface> interface);
377 
381  void SetupLoopback();
382 
388 
395  bool IsUnicast(Ipv4Address ad, Ipv4Mask interfaceMask) const;
396 
400  typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
401 
409  void DoFragmentation(Ptr<Packet> packet,
410  const Ipv4Header& ipv4Header,
411  uint32_t outIfaceMtu,
412  std::list<Ipv4PayloadHeaderPair>& listFragments);
413 
421  bool ProcessFragment(Ptr<Packet>& packet, Ipv4Header& ipHeader, uint32_t iif);
422 
433  void CallTxTrace(const Ipv4Header& ipHeader,
434  Ptr<Packet> packet,
435  Ptr<Ipv4> ipv4,
436  uint32_t interface);
437 
441  typedef std::vector<Ptr<Ipv4Interface>> Ipv4InterfaceList;
445  typedef std::map<Ptr<const NetDevice>, uint32_t> Ipv4InterfaceReverseContainer;
449  typedef std::list<Ptr<Ipv4RawSocketImpl>> SocketList;
450 
454  typedef std::pair<int, int32_t> L4ListKey_t;
455 
459  typedef std::map<L4ListKey_t, Ptr<IpL4Protocol>> L4List_t;
460 
461  bool m_ipForward;
467  uint8_t m_defaultTtl;
468  std::map<std::pair<uint64_t, uint8_t>, uint16_t>
471 
480 
481  // The following two traces pass a packet with an IP header
490  // <ip-header, payload, reason, ifindex> (ifindex not valid if reason is DROP_NO_ROUTE)
496 
498 
500 
502  typedef std::pair<uint64_t, uint32_t> FragmentKey_t;
503 
505  typedef std::list<std::tuple<Time, FragmentKey_t, Ipv4Header, uint32_t>>
508  typedef std::list<std::tuple<Time, FragmentKey_t, Ipv4Header, uint32_t>>::iterator
510 
517  void HandleFragmentsTimeout(FragmentKey_t key, Ipv4Header& ipHeader, uint32_t iif);
518 
526  FragmentsTimeoutsListI_t SetTimeout(FragmentKey_t key, Ipv4Header ipHeader, uint32_t iif);
527 
531  void HandleTimeout();
532 
534 
536 
540  class Fragments : public SimpleRefCount<Fragments>
541  {
542  public:
546  Fragments();
547 
554  void AddFragment(Ptr<Packet> fragment, uint16_t fragmentOffset, bool moreFragment);
555 
560  bool IsEntire() const;
561 
566  Ptr<Packet> GetPacket() const;
567 
573 
579 
585 
586  private:
591 
595  std::list<std::pair<Ptr<Packet>, uint16_t>> m_fragments;
596 
601  };
602 
604  typedef std::map<FragmentKey_t, Ptr<Fragments>> MapFragments_t;
605 
608 
612  typedef std::tuple<uint64_t, uint8_t, Ipv4Address, Ipv4Address> DupTuple_t;
614  typedef std::map<DupTuple_t, Time> DupMap_t;
615 
622  bool UpdateDuplicate(Ptr<const Packet> p, const Ipv4Header& header);
626  void RemoveDuplicates();
627 
628  bool m_enableDpd;
633 };
634 
635 } // Namespace ns3
636 
637 #endif /* IPV4_L3_PROTOCOL_H */
a polymophic address class
Definition: address.h:100
An identifier for simulation events.
Definition: event-id.h:55
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Packet header for IPv4.
Definition: ipv4-header.h:34
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
a class to store IPv4 address information on an interface
A Set of Fragment belonging to the same packet (src, dst, identification and proto)
bool IsEntire() const
If all fragments have been added.
Ptr< Packet > GetPartialPacket() const
Get the complete part of the packet.
FragmentsTimeoutsListI_t GetTimeoutIter()
Get the Timeout iterator.
std::list< std::pair< Ptr< Packet >, uint16_t > > m_fragments
The current fragments.
void AddFragment(Ptr< Packet > fragment, uint16_t fragmentOffset, bool moreFragment)
Add a fragment.
bool m_moreFragment
True if other fragments will be sent.
FragmentsTimeoutsListI_t m_timeoutIter
Timeout iterator to "event" handler.
Ptr< Packet > GetPacket() const
Get the entire packet.
void SetTimeoutIter(FragmentsTimeoutsListI_t iter)
Set the Timeout iterator.
Implement the IPv4 layer.
static const uint16_t PROT_NUMBER
Protocol number (0x0800)
std::tuple< uint64_t, uint8_t, Ipv4Address, Ipv4Address > DupTuple_t
IETF RFC 6621, Section 6.2 de-duplication w/o IPSec RFC 6621 recommended duplicate packet tuple: {IPV...
void CallTxTrace(const Ipv4Header &ipHeader, Ptr< Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
Make a copy of the packet, add the header and invoke the TX trace callback.
DropReason
Reason why a packet has been dropped.
@ DROP_BAD_CHECKSUM
Bad checksum.
@ DROP_NO_ROUTE
No route to host.
@ DROP_INTERFACE_DOWN
Interface is down so can not send packet.
@ DROP_DUPLICATE
Duplicate packet received.
@ DROP_TTL_EXPIRED
Packet TTL has expired.
@ DROP_ROUTE_ERROR
Route error.
@ DROP_FRAGMENT_TIMEOUT
Fragment timeout exceeded.
void DecreaseIdentification(Ipv4Address source, Ipv4Address destination, uint8_t protocol)
Decrease the identification value for a dropped or recursed packet.
std::map< FragmentKey_t, Ptr< Fragments > > MapFragments_t
Container of fragments, stored as pairs(src+dst addr, src+dst port) / fragment.
Ipv4InterfaceList m_interfaces
List of IPv4 interfaces.
void DeleteRawSocket(Ptr< Socket > socket) override
Deletes a particular raw socket.
MapFragments_t m_fragments
Fragmented packets.
void LocalDeliver(Ptr< const Packet > p, const Ipv4Header &ip, uint32_t iif)
Deliver a packet.
bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const override
Determine whether address and interface corresponding to received packet can be accepted for local de...
std::pair< uint64_t, uint32_t > FragmentKey_t
Key identifying a fragmented packet.
std::list< std::tuple< Time, FragmentKey_t, Ipv4Header, uint32_t > > FragmentsTimeoutsList_t
Container for fragment timeouts.
Time m_expire
duplicate entry expiration delay
Ipv4L3Protocol & operator=(const Ipv4L3Protocol &)=delete
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
bool m_ipForward
Forwarding packets (i.e.
void(* DropTracedCallback)(const Ipv4Header &header, Ptr< const Packet > packet, DropReason reason, Ptr< Ipv4 > ipv4, uint32_t interface)
TracedCallback signature for packet drop events.
void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Lower layer calls this method after calling L3Demux::Lookup The ARP subclass needs to know from which...
void SetUp(uint32_t i) override
uint32_t GetNInterfaces() const override
Ipv4Header BuildHeader(Ipv4Address source, Ipv4Address destination, uint8_t protocol, uint16_t payloadSize, uint8_t ttl, uint8_t tos, bool mayFragment)
Construct an IPv4 header.
void RouteInputError(Ptr< const Packet > p, const Ipv4Header &ipHeader, Socket::SocketErrno sockErrno)
Fallback when no route is found.
std::list< Ptr< Ipv4RawSocketImpl > > SocketList
Container of the IPv4 Raw Sockets.
void SetMetric(uint32_t i, uint16_t metric) override
std::list< std::tuple< Time, FragmentKey_t, Ipv4Header, uint32_t > >::iterator FragmentsTimeoutsListI_t
Container Iterator for fragment timeouts..
void DoDispose() override
Destructor implementation.
void Remove(Ptr< IpL4Protocol > protocol) override
void SetIpForward(bool forward) override
Set or unset the IP forwarding state.
bool AddAddress(uint32_t i, Ipv4InterfaceAddress address) override
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_unicastForwardTrace
Trace of unicast forwarded packets.
bool GetWeakEsModel() const override
Get the Weak Es Model status.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_localDeliverTrace
Trace of locally delivered packets.
bool IsUnicast(Ipv4Address ad) const
Check if an IPv4 address is unicast according to the node.
void HandleFragmentsTimeout(FragmentKey_t key, Ipv4Header &ipHeader, uint32_t iif)
Process the timeout for packet fragments.
uint16_t GetMtu(uint32_t i) const override
Ptr< Icmpv4L4Protocol > GetIcmp() const
Get ICMPv4 protocol.
Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const override
void RemoveDuplicates()
Remove expired duplicates packet entry.
void DoFragmentation(Ptr< Packet > packet, const Ipv4Header &ipv4Header, uint32_t outIfaceMtu, std::list< Ipv4PayloadHeaderPair > &listFragments)
Fragment a packet.
SocketList m_sockets
List of IPv4 raw sockets.
bool m_enableDpd
Enable multicast duplicate packet detection.
std::map< std::pair< uint64_t, uint8_t >, uint16_t > m_identification
Identification (for each {src, dst, proto} tuple)
bool IsUp(uint32_t i) const override
Time m_fragmentExpirationTimeout
Expiration timeout.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_sendOutgoingTrace
Trace of sent packets.
EventId m_timeoutEvent
Event for the next scheduled timeout.
int32_t GetInterfaceForPrefix(Ipv4Address addr, Ipv4Mask mask) const override
Return the interface number of first interface found that has an Ipv4 address within the prefix speci...
void SetWeakEsModel(bool model) override
Set or unset the Weak Es Model.
Time m_purge
time between purging expired duplicate entries
std::map< Ptr< const NetDevice >, uint32_t > Ipv4InterfaceReverseContainer
Container of NetDevices registered to IPv4 and their interface indexes.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
void IpMulticastForward(Ptr< Ipv4MulticastRoute > mrtentry, Ptr< const Packet > p, const Ipv4Header &header)
Forward a multicast packet.
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_rxTrace
Trace of received packets.
uint16_t GetMetric(uint32_t i) const override
FragmentsTimeoutsList_t m_timeoutEventList
Timeout "events" container.
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_txTrace
Trace of transmitted packets.
Ptr< Socket > CreateRawSocket() override
Creates a raw socket.
bool ProcessFragment(Ptr< Packet > &packet, Ipv4Header &ipHeader, uint32_t iif)
Process a packet fragment.
static TypeId GetTypeId()
Get the type ID.
void HandleTimeout()
Handles a fragmented packet timeout.
Ipv4Address SourceAddressSelection(uint32_t interface, Ipv4Address dest) override
Choose the source address to use with destination address.
void NotifyNewAggregate() override
This function will notify other components connected to the node that a new stack member is now conne...
Ipv4InterfaceReverseContainer m_reverseInterfacesContainer
Container of NetDevice / Interface index associations.
uint32_t AddInterface(Ptr< NetDevice > device) override
EventId m_cleanDpd
event to cleanup expired duplicate entries
void SendWithHeader(Ptr< Packet > packet, Ipv4Header ipHeader, Ptr< Ipv4Route > route) override
bool IsForwarding(uint32_t i) const override
void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route) override
Ptr< NetDevice > GetNetDevice(uint32_t i) override
L4List_t m_protocols
List of transport protocol.
uint32_t GetNAddresses(uint32_t interface) const override
void SetDown(uint32_t i) override
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol) override
Register a new routing protocol to be used by this Ipv4 stack.
void(* TxRxTracedCallback)(Ptr< const Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
TracedCallback signature for packet transmission or reception events.
Ptr< Ipv4RoutingProtocol > GetRoutingProtocol() const override
Get the routing protocol to be used by this Ipv4 stack.
Ptr< Ipv4RoutingProtocol > m_routingProtocol
Routing protocol associated with the stack.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_multicastForwardTrace
Trace of multicast forwarded packets.
uint8_t m_defaultTtl
Default TTL.
std::map< L4ListKey_t, Ptr< IpL4Protocol > > L4List_t
Container of the IPv4 L4 instances.
bool m_weakEsModel
Weak ES model state.
DupMap_t m_dups
map of packet duplicate tuples to expiry event
void SendRealOut(Ptr< Ipv4Route > route, Ptr< Packet > packet, const Ipv4Header &ipHeader)
Send packet with route.
Ptr< Node > m_node
Node attached to stack.
bool UpdateDuplicate(Ptr< const Packet > p, const Ipv4Header &header)
Registers duplicate entry, return false if new.
Ipv4Address SelectSourceAddress(Ptr< const NetDevice > device, Ipv4Address dst, Ipv4InterfaceAddress::InterfaceAddressScope_e scope) override
Return the first primary source address with scope less than or equal to the requested scope,...
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex) override
Remove the address at addressIndex on named interface.
std::vector< Ptr< Ipv4Interface > > Ipv4InterfaceList
Container of the IPv4 Interfaces.
Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const override
Because addresses can be removed, the addressIndex is not guaranteed to be static across calls to thi...
int32_t GetInterfaceForAddress(Ipv4Address addr) const override
Return the interface number of the interface that has been assigned the specified IP address.
Ipv4L3Protocol(const Ipv4L3Protocol &)=delete
Ptr< Ipv4Interface > GetInterface(uint32_t i) const
Get an interface.
void IpForward(Ptr< Ipv4Route > rtentry, Ptr< const Packet > p, const Ipv4Header &header)
Forward a packet.
void SetForwarding(uint32_t i, bool val) override
void SetupLoopback()
Setup loopback interface.
void(* SentTracedCallback)(const Ipv4Header &header, Ptr< const Packet > packet, uint32_t interface)
TracedCallback signature for packet send, forward, or local deliver events.
void SetDefaultTtl(uint8_t ttl)
int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const override
std::map< DupTuple_t, Time > DupMap_t
Maps packet duplicate tuple to expiration time.
void Insert(Ptr< IpL4Protocol > protocol) override
TracedCallback< const Ipv4Header &, Ptr< const Packet >, DropReason, Ptr< Ipv4 >, uint32_t > m_dropTrace
Trace of dropped packets.
FragmentsTimeoutsListI_t SetTimeout(FragmentKey_t key, Ipv4Header ipHeader, uint32_t iif)
Set a new timeout "event" for a fragmented packet.
bool GetIpForward() const override
Get the IP forwarding state.
std::pair< int, int32_t > L4ListKey_t
Container of the IPv4 L4 keys: protocol number, interface index.
uint32_t AddIpv4Interface(Ptr< Ipv4Interface > interface)
Add an IPv4 interface to the stack.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
A template-based reference counting class.
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:84
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:60
address
Definition: first.py:40
Every class exported by the ns3 library is enclosed in the ns3 namespace.