A Discrete-Event Network Simulator
API
ipv4-l3-protocol.h
Go to the documentation of this file.
1 // -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*-
2 //
3 // Copyright (c) 2006 Georgia Tech Research Corporation
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Author: George F. Riley<riley@ece.gatech.edu>
19 //
20 
21 #ifndef IPV4_L3_PROTOCOL_H
22 #define IPV4_L3_PROTOCOL_H
23 
24 #include <list>
25 #include <map>
26 #include <vector>
27 #include <stdint.h>
28 #include "ns3/ipv4-address.h"
29 #include "ns3/ptr.h"
30 #include "ns3/net-device.h"
31 #include "ns3/ipv4.h"
32 #include "ns3/traced-callback.h"
33 #include "ns3/ipv4-header.h"
34 #include "ns3/ipv4-routing-protocol.h"
35 #include "ns3/nstime.h"
36 #include "ns3/simulator.h"
37 
39 
40 namespace ns3 {
41 
42 class Packet;
43 class NetDevice;
44 class Ipv4Interface;
45 class Ipv4Address;
46 class Ipv4Header;
47 class Ipv4RoutingTableEntry;
48 class Ipv4Route;
49 class Node;
50 class Socket;
51 class Ipv4RawSocketImpl;
52 class IpL4Protocol;
53 class Icmpv4L4Protocol;
54 
80 class Ipv4L3Protocol : public Ipv4
81 {
82 public:
87  static TypeId GetTypeId (void);
88  static const uint16_t PROT_NUMBER;
89 
91  virtual ~Ipv4L3Protocol ();
92 
93  // Delete copy constructor and assignment operator to avoid misuse
94  Ipv4L3Protocol (const Ipv4L3Protocol &) = delete;
96 
102  {
110  };
111 
116  void SetNode (Ptr<Node> node);
117 
118  // functions defined in base class Ipv4
119 
120  void SetRoutingProtocol (Ptr<Ipv4RoutingProtocol> routingProtocol);
122 
124  void DeleteRawSocket (Ptr<Socket> socket);
125 
126  virtual void Insert (Ptr<IpL4Protocol> protocol);
127  virtual void Insert (Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex);
128 
129  virtual void Remove (Ptr<IpL4Protocol> protocol);
130  virtual void Remove (Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex);
131 
132  virtual Ptr<IpL4Protocol> GetProtocol (int protocolNumber) const;
133  virtual Ptr<IpL4Protocol> GetProtocol (int protocolNumber, int32_t interfaceIndex) const;
134 
135  virtual Ipv4Address SourceAddressSelection (uint32_t interface, Ipv4Address dest);
136 
143  void SetDefaultTtl (uint8_t ttl);
144 
158  void Receive ( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
159  const Address &to, NetDevice::PacketType packetType);
160 
171  void Send (Ptr<Packet> packet, Ipv4Address source,
172  Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);
181  void SendWithHeader (Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route);
182 
183  uint32_t AddInterface (Ptr<NetDevice> device);
189  Ptr<Ipv4Interface> GetInterface (uint32_t i) const;
190  uint32_t GetNInterfaces (void) const;
191 
192  int32_t GetInterfaceForAddress (Ipv4Address addr) const;
193  int32_t GetInterfaceForPrefix (Ipv4Address addr, Ipv4Mask mask) const;
194  int32_t GetInterfaceForDevice (Ptr<const NetDevice> device) const;
195  bool IsDestinationAddress (Ipv4Address address, uint32_t iif) const;
196 
197  bool AddAddress (uint32_t i, Ipv4InterfaceAddress address);
198  Ipv4InterfaceAddress GetAddress (uint32_t interfaceIndex, uint32_t addressIndex) const;
199  uint32_t GetNAddresses (uint32_t interface) const;
200  bool RemoveAddress (uint32_t interfaceIndex, uint32_t addressIndex);
201  bool RemoveAddress (uint32_t interface, Ipv4Address address);
204 
205 
206  void SetMetric (uint32_t i, uint16_t metric);
207  uint16_t GetMetric (uint32_t i) const;
208  uint16_t GetMtu (uint32_t i) const;
209  bool IsUp (uint32_t i) const;
210  void SetUp (uint32_t i);
211  void SetDown (uint32_t i);
212  bool IsForwarding (uint32_t i) const;
213  void SetForwarding (uint32_t i, bool val);
214 
215  Ptr<NetDevice> GetNetDevice (uint32_t i);
216 
227  bool IsUnicast (Ipv4Address ad) const;
228 
236  typedef void (* SentTracedCallback)
237  (const Ipv4Header & header, Ptr<const Packet> packet, uint32_t interface);
238 
248  typedef void (* TxRxTracedCallback)
249  (Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface);
250 
262  typedef void (* DropTracedCallback)
263  (const Ipv4Header & header, Ptr<const Packet> packet,
264  DropReason reason, Ptr<Ipv4> ipv4,
265  uint32_t interface);
266 
267 protected:
268 
269  virtual void DoDispose (void);
274  virtual void NotifyNewAggregate ();
275 private:
280  friend class ::Ipv4L3ProtocolTestCase;
281 
282  // class Ipv4 attributes
283  virtual void SetIpForward (bool forward);
284  virtual bool GetIpForward (void) const;
285  virtual void SetWeakEsModel (bool model);
286  virtual bool GetWeakEsModel (void) const;
287 
294  void DecreaseIdentification (Ipv4Address source,
295  Ipv4Address destination,
296  uint8_t protocol);
297 
310  Ipv4Address source,
311  Ipv4Address destination,
312  uint8_t protocol,
313  uint16_t payloadSize,
314  uint8_t ttl,
315  uint8_t tos,
316  bool mayFragment);
317 
324  void
326  Ptr<Packet> packet,
327  Ipv4Header const &ipHeader);
328 
335  void
336  IpForward (Ptr<Ipv4Route> rtentry,
338  const Ipv4Header &header);
339 
346  void
349  const Ipv4Header &header);
350 
357  void LocalDeliver (Ptr<const Packet> p, Ipv4Header const&ip, uint32_t iif);
358 
365  void RouteInputError (Ptr<const Packet> p, const Ipv4Header & ipHeader, Socket::SocketErrno sockErrno);
366 
372  uint32_t AddIpv4Interface (Ptr<Ipv4Interface> interface);
373 
377  void SetupLoopback (void);
378 
383  Ptr<Icmpv4L4Protocol> GetIcmp (void) const;
384 
391  bool IsUnicast (Ipv4Address ad, Ipv4Mask interfaceMask) const;
392 
396  typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
397 
405  void DoFragmentation (Ptr<Packet> packet, const Ipv4Header& ipv4Header, uint32_t outIfaceMtu, std::list<Ipv4PayloadHeaderPair>& listFragments);
406 
414  bool ProcessFragment (Ptr<Packet>& packet, Ipv4Header & ipHeader, uint32_t iif);
415 
426  void CallTxTrace (const Ipv4Header & ipHeader, Ptr<Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface);
427 
431  typedef std::vector<Ptr<Ipv4Interface> > Ipv4InterfaceList;
435  typedef std::map<Ptr<const NetDevice>, uint32_t > Ipv4InterfaceReverseContainer;
439  typedef std::list<Ptr<Ipv4RawSocketImpl> > SocketList;
440 
444  typedef std::pair<int, int32_t> L4ListKey_t;
445 
449  typedef std::map<L4ListKey_t, Ptr<IpL4Protocol> > L4List_t;
450 
451  bool m_ipForward;
456  uint8_t m_defaultTtl;
457  std::map<std::pair<uint64_t, uint8_t>, uint16_t> m_identification;
459 
468 
469  // The following two traces pass a packet with an IP header
478  // <ip-header, payload, reason, ifindex> (ifindex not valid if reason is DROP_NO_ROUTE)
483 
485 
487 
489  typedef std::pair<uint64_t, uint32_t> FragmentKey_t;
490 
492  typedef std::list< std::tuple <Time, FragmentKey_t, Ipv4Header, uint32_t > > FragmentsTimeoutsList_t;
494  typedef std::list< std::tuple <Time, FragmentKey_t, Ipv4Header, uint32_t > >::iterator FragmentsTimeoutsListI_t;
495 
502  void HandleFragmentsTimeout (FragmentKey_t key, Ipv4Header & ipHeader, uint32_t iif);
503 
511  FragmentsTimeoutsListI_t SetTimeout (FragmentKey_t key, Ipv4Header ipHeader, uint32_t iif);
512 
516  void HandleTimeout (void);
517 
519 
521 
525  class Fragments : public SimpleRefCount<Fragments>
526  {
527  public:
531  Fragments ();
532 
536  ~Fragments ();
537 
544  void AddFragment (Ptr<Packet> fragment, uint16_t fragmentOffset, bool moreFragment);
545 
550  bool IsEntire () const;
551 
556  Ptr<Packet> GetPacket () const;
557 
562  Ptr<Packet> GetPartialPacket () const;
563 
569 
575 
576  private:
581 
585  std::list<std::pair<Ptr<Packet>, uint16_t> > m_fragments;
586 
591  };
592 
594  typedef std::map< FragmentKey_t, Ptr<Fragments> > MapFragments_t;
595 
598 
601  typedef std::tuple <uint64_t, uint8_t, Ipv4Address, Ipv4Address> DupTuple_t;
603  typedef std::map<DupTuple_t, Time> DupMap_t;
604 
611  bool UpdateDuplicate (Ptr<const Packet> p, const Ipv4Header &header);
615  void RemoveDuplicates (void);
616 
617  bool m_enableDpd;
622 };
623 
624 } // Namespace ns3
625 
626 #endif /* IPV4_L3_PROTOCOL_H */
a polymophic address class
Definition: address.h:91
An identifier for simulation events.
Definition: event-id.h:54
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
Packet header for IPv4.
Definition: ipv4-header.h:34
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
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.
Ipv4InterfaceList m_interfaces
List of IPv4 interfaces.
bool IsUp(uint32_t i) const
MapFragments_t m_fragments
Fragmented packets.
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex)
Remove the address at addressIndex on named interface.
std::pair< uint64_t, uint32_t > FragmentKey_t
Key identifying a fragmented packet.
void DeleteRawSocket(Ptr< Socket > socket)
Deletes a particular raw socket.
int32_t GetInterfaceForPrefix(Ipv4Address addr, Ipv4Mask mask) const
Return the interface number of first interface found that has an Ipv4 address within the prefix speci...
Time m_expire
duplicate entry expiration delay
virtual bool GetIpForward(void) const
Get the IP forwarding state.
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...
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 SetForwarding(uint32_t i, bool val)
virtual void NotifyNewAggregate()
This function will notify other components connected to the node that a new stack member is now conne...
uint32_t GetNAddresses(uint32_t interface) const
uint16_t GetMtu(uint32_t i) const
virtual void SetIpForward(bool forward)
Set or unset the IP forwarding state.
virtual void SetWeakEsModel(bool model)
Set or unset the Weak Es Model.
virtual Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const
Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const
Get the routing protocol to be used by this Ipv4 stack.
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.
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.
virtual Ipv4Address SourceAddressSelection(uint32_t interface, Ipv4Address dest)
Choose the source address to use with destination address.
bool m_enableDpd
Enable multicast duplicate packet detection.
bool AddAddress(uint32_t i, Ipv4InterfaceAddress address)
std::map< std::pair< uint64_t, uint8_t >, uint16_t > m_identification
Identification (for each {src, dst, proto} tuple)
Time m_fragmentExpirationTimeout
Expiration timeout.
static TypeId GetTypeId(void)
Get the type ID.
std::map< Ptr< const NetDevice >, uint32_t > Ipv4InterfaceReverseContainer
Container of NetDevices registered to IPv4 and their interface indexes.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_localDeliverTrace
Trace of locally delivered packets.
virtual void Remove(Ptr< IpL4Protocol > protocol)
void HandleTimeout(void)
Handles a fragmented packet timeout.
Ptr< NetDevice > GetNetDevice(uint32_t i)
EventId m_timeoutEvent
Event for the next scheduled timeout.
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.
Time m_purge
time between purging expired duplicate entries
TracedCallback< const Ipv4Header &, Ptr< const Packet >, DropReason, Ptr< Ipv4 >, uint32_t > m_dropTrace
Trace of dropped packets.
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
void RemoveDuplicates(void)
Remove expired duplicates packet entry.
FragmentsTimeoutsList_t m_timeoutEventList
Timeout "events" container.
bool IsForwarding(uint32_t i) const
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_sendOutgoingTrace
Trace of sent packets.
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_txTrace
Trace of transmitted packets.
void SendWithHeader(Ptr< Packet > packet, Ipv4Header ipHeader, Ptr< Ipv4Route > route)
bool ProcessFragment(Ptr< Packet > &packet, Ipv4Header &ipHeader, uint32_t iif)
Process a packet fragment.
void SetUp(uint32_t i)
Ipv4InterfaceReverseContainer m_reverseInterfacesContainer
Container of NetDevice / Interface index associations.
void SetupLoopback(void)
Setup loopback interface.
void LocalDeliver(Ptr< const Packet > p, Ipv4Header const &ip, uint32_t iif)
Deliver a packet.
Ipv4Address SelectSourceAddress(Ptr< const NetDevice > device, Ipv4Address dst, Ipv4InterfaceAddress::InterfaceAddressScope_e scope)
Return the first primary source address with scope less than or equal to the requested scope,...
EventId m_cleanDpd
event to cleanup expired duplicate entries
void SetDown(uint32_t i)
L4List_t m_protocols
List of transport protocol.
void(* TxRxTracedCallback)(Ptr< const Packet > packet, Ptr< Ipv4 > ipv4, uint32_t interface)
TracedCallback signature for packet transmission or reception events.
std::map< FragmentKey_t, Ptr< Fragments > > MapFragments_t
Container of fragments, stored as pairs(src+dst addr, src+dst port) / fragment.
Ptr< Socket > CreateRawSocket(void)
Creates a raw socket.
Ptr< Ipv4RoutingProtocol > m_routingProtocol
Routing protocol associated with the stack.
void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)
void SendRealOut(Ptr< Ipv4Route > route, Ptr< Packet > packet, Ipv4Header const &ipHeader)
Send packet with route.
Ptr< Icmpv4L4Protocol > GetIcmp(void) const
Get ICMPv4 protocol.
uint32_t AddInterface(Ptr< NetDevice > device)
virtual void DoDispose(void)
Destructor implementation.
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.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_unicastForwardTrace
Trace of unicast forwarded packets.
void SetMetric(uint32_t i, uint16_t metric)
DupMap_t m_dups
map of packet duplicate tuples to expiry event
Ptr< Node > m_node
Node attached to stack.
bool UpdateDuplicate(Ptr< const Packet > p, const Ipv4Header &header)
Registers duplicate entry, return false if new.
std::vector< Ptr< Ipv4Interface > > Ipv4InterfaceList
Container of the IPv4 Interfaces.
virtual void Insert(Ptr< IpL4Protocol > protocol)
Ipv4L3Protocol(const Ipv4L3Protocol &)=delete
int32_t GetInterfaceForAddress(Ipv4Address addr) const
Return the interface number of the interface that has been assigned the specified IP address.
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(* 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)
std::map< DupTuple_t, Time > DupMap_t
Maps packet duplicate tuple to expiration time.
int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const
std::list< std::tuple< Time, FragmentKey_t, Ipv4Header, uint32_t > > FragmentsTimeoutsList_t
Container for fragment timeouts.
uint32_t GetNInterfaces(void) const
FragmentsTimeoutsListI_t SetTimeout(FragmentKey_t key, Ipv4Header ipHeader, uint32_t iif)
Set a new timeout "event" for a fragmented packet.
virtual bool GetWeakEsModel(void) const
Get the Weak Es Model status.
Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const
Because addresses can be removed, the addressIndex is not guaranteed to be static across calls to thi...
std::pair< int, int32_t > L4ListKey_t
Container of the IPv4 L4 keys: protocol number, interface index.
std::list< std::tuple< Time, FragmentKey_t, Ipv4Header, uint32_t > >::iterator FragmentsTimeoutsListI_t
Container Iterator for fragment timeouts..
bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const
Determine whether address and interface corresponding to received packet can be accepted for local de...
uint32_t AddIpv4Interface(Ptr< Ipv4Interface > interface)
Add an IPv4 interface to the stack.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_multicastForwardTrace
Trace of multicast forwarded packets.
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:297
A template-based reference counting class.
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.