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/deprecated.h"
28#include "ns3/ipv4-address.h"
29#include "ns3/net-device.h"
30#include "ns3/nstime.h"
31#include "ns3/ptr.h"
32#include "ns3/simulator.h"
33#include "ns3/traced-callback.h"
34
35#include <list>
36#include <map>
37#include <stdint.h>
38#include <vector>
39
41
42namespace ns3
43{
44
45class Packet;
46class NetDevice;
47class Ipv4Interface;
48class Ipv4Address;
49class Ipv4Header;
50class Ipv4RoutingTableEntry;
51class Ipv4Route;
52class Node;
53class Socket;
54class Ipv4RawSocketImpl;
55class IpL4Protocol;
56class Icmpv4L4Protocol;
57
83class Ipv4L3Protocol : public Ipv4
84{
85 public:
90 static TypeId GetTypeId();
91 static const uint16_t PROT_NUMBER;
92
94 ~Ipv4L3Protocol() override;
95
96 // Delete copy constructor and assignment operator to avoid misuse
99
105 {
113 };
114
119 void SetNode(Ptr<Node> node);
120
121 // functions defined in base class Ipv4
122
123 void SetRoutingProtocol(Ptr<Ipv4RoutingProtocol> routingProtocol) override;
125
126 Ptr<Socket> CreateRawSocket() override;
127 void DeleteRawSocket(Ptr<Socket> socket) override;
128
129 void Insert(Ptr<IpL4Protocol> protocol) override;
130 void Insert(Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex) override;
131
132 void Remove(Ptr<IpL4Protocol> protocol) override;
133 void Remove(Ptr<IpL4Protocol> protocol, uint32_t interfaceIndex) override;
134
135 Ptr<IpL4Protocol> GetProtocol(int protocolNumber) const override;
136 Ptr<IpL4Protocol> GetProtocol(int protocolNumber, int32_t interfaceIndex) const override;
137
138 Ipv4Address SourceAddressSelection(uint32_t interface, Ipv4Address dest) override;
139
146 void SetDefaultTtl(uint8_t ttl);
147
161 void Receive(Ptr<NetDevice> device,
163 uint16_t protocol,
164 const Address& from,
165 const Address& to,
166 NetDevice::PacketType packetType);
167
178 void Send(Ptr<Packet> packet,
179 Ipv4Address source,
180 Ipv4Address destination,
181 uint8_t protocol,
182 Ptr<Ipv4Route> route) override;
191 void SendWithHeader(Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route) override;
192
193 uint32_t AddInterface(Ptr<NetDevice> device) override;
200 uint32_t GetNInterfaces() const override;
201
202 int32_t GetInterfaceForAddress(Ipv4Address addr) const override;
203 int32_t GetInterfaceForPrefix(Ipv4Address addr, Ipv4Mask mask) const override;
205 bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const override;
206
207 bool AddAddress(uint32_t i, Ipv4InterfaceAddress address) override;
208 Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const override;
209 uint32_t GetNAddresses(uint32_t interface) const override;
210 bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex) override;
211 bool RemoveAddress(uint32_t interface, Ipv4Address address) override;
213 Ipv4Address dst,
215
216 void SetMetric(uint32_t i, uint16_t metric) override;
217 uint16_t GetMetric(uint32_t i) const override;
218 uint16_t GetMtu(uint32_t i) const override;
219 bool IsUp(uint32_t i) const override;
220 void SetUp(uint32_t i) override;
221 void SetDown(uint32_t i) override;
222 bool IsForwarding(uint32_t i) const override;
223 void SetForwarding(uint32_t i, bool val) override;
224
226
237 bool IsUnicast(Ipv4Address ad) const;
238
246 typedef void (*SentTracedCallback)(const Ipv4Header& header,
247 Ptr<const Packet> packet,
248 uint32_t interface);
249
259 typedef void (*TxRxTracedCallback)(Ptr<const Packet> packet,
260 Ptr<Ipv4> ipv4,
261 uint32_t interface);
262
274 typedef void (*DropTracedCallback)(const Ipv4Header& header,
275 Ptr<const Packet> packet,
276 DropReason reason,
277 Ptr<Ipv4> ipv4,
278 uint32_t interface);
279
280 protected:
281 void DoDispose() override;
287 void NotifyNewAggregate() override;
288
289 private:
294 friend class ::Ipv4L3ProtocolTestCase;
295
296 // class Ipv4 attributes
297 void SetIpForward(bool forward) override;
298 bool GetIpForward() const override;
299
300 NS_DEPRECATED_3_41("Use SetStrongEndSystemModel instead")
301 void SetWeakEsModel(bool model) override;
303 bool GetWeakEsModel() const override;
304
305 void SetStrongEndSystemModel(bool model) override;
306 bool GetStrongEndSystemModel() const override;
307
314 void DecreaseIdentification(Ipv4Address source, Ipv4Address destination, uint8_t protocol);
315
328 Ipv4Address destination,
329 uint8_t protocol,
330 uint16_t payloadSize,
331 uint8_t ttl,
332 uint8_t tos,
333 bool mayFragment);
334
341 void SendRealOut(Ptr<Ipv4Route> route, Ptr<Packet> packet, const Ipv4Header& ipHeader);
342
349 void IpForward(Ptr<Ipv4Route> rtentry, Ptr<const Packet> p, const Ipv4Header& header);
350
358 Ptr<const Packet> p,
359 const Ipv4Header& header);
360
367 void LocalDeliver(Ptr<const Packet> p, const Ipv4Header& ip, uint32_t iif);
368
375 void RouteInputError(Ptr<const Packet> p,
376 const Ipv4Header& ipHeader,
377 Socket::SocketErrno sockErrno);
378
385
389 void SetupLoopback();
390
396
403 bool IsUnicast(Ipv4Address ad, Ipv4Mask interfaceMask) const;
404
409
417 void DoFragmentation(Ptr<Packet> packet,
418 const Ipv4Header& ipv4Header,
419 uint32_t outIfaceMtu,
420 std::list<Ipv4PayloadHeaderPair>& listFragments);
421
429 bool ProcessFragment(Ptr<Packet>& packet, Ipv4Header& ipHeader, uint32_t iif);
430
441 void CallTxTrace(const Ipv4Header& ipHeader,
442 Ptr<Packet> packet,
443 Ptr<Ipv4> ipv4,
444 uint32_t interface);
445
458
462 typedef std::pair<int, int32_t> L4ListKey_t;
463
468
475 uint8_t m_defaultTtl;
476 std::map<std::pair<uint64_t, uint8_t>, uint16_t>
479
488
489 // The following two traces pass a packet with an IP header
498 // <ip-header, payload, reason, ifindex> (ifindex not valid if reason is DROP_NO_ROUTE)
504
506
508
510 typedef std::pair<uint64_t, uint32_t> FragmentKey_t;
511
513 typedef std::list<std::tuple<Time, FragmentKey_t, Ipv4Header, uint32_t>>
516 typedef std::list<std::tuple<Time, FragmentKey_t, Ipv4Header, uint32_t>>::iterator
518
526
535
539 void HandleTimeout();
540
542
544
549 {
550 public:
554 Fragments();
555
562 void AddFragment(Ptr<Packet> fragment, uint16_t fragmentOffset, bool moreFragment);
563
568 bool IsEntire() const;
569
574 Ptr<Packet> GetPacket() const;
575
580 Ptr<Packet> GetPartialPacket() const;
581
586 void SetTimeoutIter(FragmentsTimeoutsListI_t iter);
587
592 FragmentsTimeoutsListI_t GetTimeoutIter();
593
594 private:
599
603 std::list<std::pair<Ptr<Packet>, uint16_t>> m_fragments;
604
609 };
610
612 typedef std::map<FragmentKey_t, Ptr<Fragments>> MapFragments_t;
613
616
620 typedef std::tuple<uint64_t, uint8_t, Ipv4Address, Ipv4Address> DupTuple_t;
622 typedef std::map<DupTuple_t, Time> DupMap_t;
623
630 bool UpdateDuplicate(Ptr<const Packet> p, const Ipv4Header& header);
634 void RemoveDuplicates();
635
641
646};
647
648} // Namespace ns3
649
650#endif /* IPV4_L3_PROTOCOL_H */
a polymophic address class
Definition: address.h:101
An identifier for simulation events.
Definition: event-id.h:55
This is the implementation of the ICMP protocol as described in RFC 792.
L4 Protocol abstract base class.
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:80
a class to store IPv4 address information on an interface
The IPv4 representation of a network interface.
A Set of Fragment belonging to the same packet (src, dst, identification and proto)
std::list< std::pair< Ptr< Packet >, uint16_t > > m_fragments
The current fragments.
bool m_moreFragment
True if other fragments will be sent.
FragmentsTimeoutsListI_t m_timeoutIter
Timeout iterator to "event" handler.
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
bool m_strongEndSystemModel
Strong End System Model state.
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.
bool GetStrongEndSystemModel() const override
Get the Strong End System Model status.
uint8_t m_defaultTtl
Default TTL.
std::map< L4ListKey_t, Ptr< IpL4Protocol > > L4List_t
Container of the IPv4 L4 instances.
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 SetStrongEndSystemModel(bool model) override
Set or unset the Strong End System Model.
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
Ipv4 multicast route cache entry (similar to Linux struct mfc_cache)
Definition: ipv4-route.h:115
IPv4 route cache entry (similar to Linux struct rtable)
Definition: ipv4-route.h:42
Abstract base class for IPv4 routing protocols.
Network layer to device interface.
Definition: net-device.h:98
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:300
A network Node.
Definition: node.h:57
network packets
Definition: packet.h:239
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
A template-based reference counting class.
A low-level Socket API based loosely on the BSD Socket API.
Definition: socket.h:68
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
#define NS_DEPRECATED_3_41(msg)
Tag for things deprecated in version ns-3.41.
Definition: deprecated.h:82
Every class exported by the ns3 library is enclosed in the ns3 namespace.
STL namespace.
#define list