A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 "ipv4-header.h"
25#include "ipv4.h"
26
27#include "ns3/ipv4-address.h"
28#include "ns3/net-device.h"
29#include "ns3/nstime.h"
30#include "ns3/ptr.h"
31#include "ns3/simulator.h"
32#include "ns3/traced-callback.h"
33
34#include <list>
35#include <map>
36#include <stdint.h>
37#include <vector>
38
40
41namespace ns3
42{
43
44class Packet;
45class NetDevice;
46class Ipv4Interface;
47class Ipv4Address;
48class Ipv4Header;
49class Ipv4RoutingTableEntry;
50class Ipv4Route;
51class Node;
52class Socket;
53class Ipv4RawSocketImpl;
54class IpL4Protocol;
55class Icmpv4L4Protocol;
56
82class Ipv4L3Protocol : public Ipv4
83{
84 public:
89 static TypeId GetTypeId();
90 static const uint16_t PROT_NUMBER;
91
93 ~Ipv4L3Protocol() override;
94
95 // Delete copy constructor and assignment operator to avoid misuse
98
104 {
112 };
113
118 void SetNode(Ptr<Node> node);
119
120 // functions defined in base class Ipv4
121
122 void SetRoutingProtocol(Ptr<Ipv4RoutingProtocol> routingProtocol) override;
124
125 Ptr<Socket> CreateRawSocket() override;
126 void DeleteRawSocket(Ptr<Socket> socket) override;
127
128 void Insert(Ptr<IpL4Protocol> protocol) override;
129 void Insert(Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex) override;
130
131 void Remove(Ptr<IpL4Protocol> protocol) override;
132 void Remove(Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex) override;
133
134 Ptr<IpL4Protocol> GetProtocol(int protocolNumber) const override;
135 Ptr<IpL4Protocol> GetProtocol(int protocolNumber, int32_t interfaceIndex) const override;
136
137 Ipv4Address SourceAddressSelection(uint32_t interface, Ipv4Address dest) override;
138
145 void SetDefaultTtl(uint8_t ttl);
146
160 void Receive(Ptr<NetDevice> device,
162 uint16_t protocol,
163 const Address& from,
164 const Address& to,
165 NetDevice::PacketType packetType);
166
177 void Send(Ptr<Packet> packet,
178 Ipv4Address source,
179 Ipv4Address destination,
180 uint8_t protocol,
181 Ptr<Ipv4Route> route) override;
190 void SendWithHeader(Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route) override;
191
192 uint32_t AddInterface(Ptr<NetDevice> device) override;
199 uint32_t GetNInterfaces() const override;
200
201 int32_t GetInterfaceForAddress(Ipv4Address addr) const override;
202 int32_t GetInterfaceForPrefix(Ipv4Address addr, Ipv4Mask mask) const override;
204 bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const override;
205
206 bool AddAddress(uint32_t i, Ipv4InterfaceAddress address) override;
207 Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const override;
208 uint32_t GetNAddresses(uint32_t interface) const override;
209 bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex) override;
210 bool RemoveAddress(uint32_t interface, Ipv4Address address) override;
212 Ipv4Address dst,
214
215 void SetMetric(uint32_t i, uint16_t metric) override;
216 uint16_t GetMetric(uint32_t i) const override;
217 uint16_t GetMtu(uint32_t i) const override;
218 bool IsUp(uint32_t i) const override;
219 void SetUp(uint32_t i) override;
220 void SetDown(uint32_t i) override;
221 bool IsForwarding(uint32_t i) const override;
222 void SetForwarding(uint32_t i, bool val) override;
223
225
236 bool IsUnicast(Ipv4Address ad) const;
237
245 typedef void (*SentTracedCallback)(const Ipv4Header& header,
246 Ptr<const Packet> packet,
247 uint32_t interface);
248
258 typedef void (*TxRxTracedCallback)(Ptr<const Packet> packet,
259 Ptr<Ipv4> ipv4,
260 uint32_t interface);
261
273 typedef void (*DropTracedCallback)(const Ipv4Header& header,
274 Ptr<const Packet> packet,
275 DropReason reason,
276 Ptr<Ipv4> ipv4,
277 uint32_t interface);
278
279 protected:
280 void DoDispose() override;
286 void NotifyNewAggregate() override;
287
288 private:
293 friend class ::Ipv4L3ProtocolTestCase;
294
295 // class Ipv4 attributes
296 void SetIpForward(bool forward) override;
297 bool GetIpForward() const override;
298 void SetWeakEsModel(bool model) override;
299 bool GetWeakEsModel() const override;
300
307 void DecreaseIdentification(Ipv4Address source, Ipv4Address destination, uint8_t protocol);
308
321 Ipv4Address destination,
322 uint8_t protocol,
323 uint16_t payloadSize,
324 uint8_t ttl,
325 uint8_t tos,
326 bool mayFragment);
327
334 void SendRealOut(Ptr<Ipv4Route> route, Ptr<Packet> packet, const Ipv4Header& ipHeader);
335
342 void IpForward(Ptr<Ipv4Route> rtentry, Ptr<const Packet> p, const Ipv4Header& header);
343
352 const Ipv4Header& header);
353
360 void LocalDeliver(Ptr<const Packet> p, const Ipv4Header& ip, uint32_t iif);
361
369 const Ipv4Header& ipHeader,
370 Socket::SocketErrno sockErrno);
371
378
382 void SetupLoopback();
383
389
396 bool IsUnicast(Ipv4Address ad, Ipv4Mask interfaceMask) const;
397
401 typedef std::pair<Ptr<Packet>, Ipv4Header> Ipv4PayloadHeaderPair;
402
410 void DoFragmentation(Ptr<Packet> packet,
411 const Ipv4Header& ipv4Header,
412 uint32_t outIfaceMtu,
413 std::list<Ipv4PayloadHeaderPair>& listFragments);
414
422 bool ProcessFragment(Ptr<Packet>& packet, Ipv4Header& ipHeader, uint32_t iif);
423
434 void CallTxTrace(const Ipv4Header& ipHeader,
435 Ptr<Packet> packet,
436 Ptr<Ipv4> ipv4,
437 uint32_t interface);
438
442 typedef std::vector<Ptr<Ipv4Interface>> Ipv4InterfaceList;
446 typedef std::map<Ptr<const NetDevice>, uint32_t> Ipv4InterfaceReverseContainer;
450 typedef std::list<Ptr<Ipv4RawSocketImpl>> SocketList;
451
455 typedef std::pair<int, int32_t> L4ListKey_t;
456
460 typedef std::map<L4ListKey_t, Ptr<IpL4Protocol>> L4List_t;
461
468 uint8_t m_defaultTtl;
469 std::map<std::pair<uint64_t, uint8_t>, uint16_t>
472
481
482 // The following two traces pass a packet with an IP header
491 // <ip-header, payload, reason, ifindex> (ifindex not valid if reason is DROP_NO_ROUTE)
497
499
501
503 typedef std::pair<uint64_t, uint32_t> FragmentKey_t;
504
506 typedef std::list<std::tuple<Time, FragmentKey_t, Ipv4Header, uint32_t>>
509 typedef std::list<std::tuple<Time, FragmentKey_t, Ipv4Header, uint32_t>>::iterator
511
519
528
532 void HandleTimeout();
533
535
537
541 class Fragments : public SimpleRefCount<Fragments>
542 {
543 public:
547 Fragments();
548
555 void AddFragment(Ptr<Packet> fragment, uint16_t fragmentOffset, bool moreFragment);
556
561 bool IsEntire() const;
562
567 Ptr<Packet> GetPacket() const;
568
574
580
586
587 private:
592
596 std::list<std::pair<Ptr<Packet>, uint16_t>> m_fragments;
597
602 };
603
605 typedef std::map<FragmentKey_t, Ptr<Fragments>> MapFragments_t;
606
609
613 typedef std::tuple<uint64_t, uint8_t, Ipv4Address, Ipv4Address> DupTuple_t;
615 typedef std::map<DupTuple_t, Time> DupMap_t;
616
623 bool UpdateDuplicate(Ptr<const Packet> p, const Ipv4Header& header);
627 void RemoveDuplicates();
628
634
639};
640
641} // Namespace ns3
642
643#endif /* IPV4_L3_PROTOCOL_H */
a polymophic address class
Definition: address.h:101
An identifier for simulation events.
Definition: event-id.h:55
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
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
Ipv4RoutingProtocol::ErrorCallback m_ecb
Error callback.
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
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.
Ipv4RoutingProtocol::UnicastForwardCallback m_ucb
Unicast forward callback.
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
Ipv4L3Protocol & operator=(const Ipv4L3Protocol &)=delete
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.
Ipv4RoutingProtocol::MulticastForwardCallback m_mcb
Multicast forward callback.
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
std::list< std::tuple< Time, FragmentKey_t, Ipv4Header, uint32_t > >::iterator FragmentsTimeoutsListI_t
Container Iterator for fragment timeouts..
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.
Ipv4RoutingProtocol::LocalDeliverCallback m_lcb
Local delivery callback.
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:257
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.