21 #include "ns3/packet.h"
23 #include "ns3/callback.h"
24 #include "ns3/ipv4-address.h"
25 #include "ns3/ipv4-route.h"
27 #include "ns3/socket.h"
28 #include "ns3/net-device.h"
29 #include "ns3/uinteger.h"
30 #include "ns3/trace-source-accessor.h"
31 #include "ns3/object-vector.h"
32 #include "ns3/ipv4-header.h"
33 #include "ns3/boolean.h"
34 #include "ns3/ipv4-routing-table-entry.h"
56 .AddConstructor<Ipv4L3Protocol> ()
57 .AddAttribute (
"DefaultTos",
"The TOS value set by default on all outgoing packets generated on this node.",
60 MakeUintegerChecker<uint8_t> ())
61 .AddAttribute (
"DefaultTtl",
"The TTL value set by default on all outgoing packets generated on this node.",
64 MakeUintegerChecker<uint8_t> ())
65 .AddAttribute (
"FragmentExpirationTimeout",
66 "When this timeout expires, the fragments will be cleared from the buffer.",
70 .AddTraceSource (
"Tx",
"Send ipv4 packet to outgoing interface.",
72 .AddTraceSource (
"Rx",
"Receive ipv4 packet from incoming interface.",
74 .AddTraceSource (
"Drop",
"Drop ipv4 packet",
76 .AddAttribute (
"InterfaceList",
"The set of Ipv4 interfaces associated to this Ipv4 stack.",
79 MakeObjectVectorChecker<Ipv4Interface> ())
81 .AddTraceSource (
"SendOutgoing",
"A newly-generated packet by this node is about to be queued for transmission",
83 .AddTraceSource (
"UnicastForward",
"A unicast IPv4 packet was received by this node and is being forwarded to another node",
85 .AddTraceSource (
"LocalDeliver",
"An IPv4 packet was received by/for this node, and it is being forward up the stack",
93 : m_identification (0)
116 if ((*i)->GetProtocolNumber () == protocolNumber)
225 if (it->second.IsRunning ())
227 it->second.Cancel ();
254 device = CreateObject<LoopbackNetDevice> ();
257 interface->SetDevice (device);
258 interface->SetNode (
m_node);
260 interface->AddAddress (ifaceAddr);
291 interface->SetNode (
m_node);
292 interface->SetDevice (device);
329 int32_t
interface = 0;
330 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
334 for (uint32_t j = 0; j < (*i)->GetNAddresses (); j++)
336 if ((*i)->GetAddress (j).GetLocal () ==
address)
352 int32_t
interface = 0;
353 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
357 for (uint32_t j = 0; j < (*i)->GetNAddresses (); j++)
359 if ((*i)->GetAddress (j).GetLocal ().CombineMask (mask) == address.
CombineMask (mask))
374 int32_t
interface = 0;
375 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
379 if ((*i)->GetDevice () == device)
398 NS_LOG_LOGIC (
"For me (destination " << address <<
" match)");
411 if (MulticastCheckGroup (iif, address ))
430 if (j == uint32_t (iif))
continue;
436 NS_LOG_LOGIC (
"For me (destination " << address <<
" match) on another interface");
442 NS_LOG_LOGIC (
"For me (interface broadcast address on another interface)");
455 NS_LOG_FUNCTION (
this << device << p << protocol << from << to << packetType);
457 NS_LOG_LOGIC (
"Packet from " << from <<
" received on node " <<
460 uint32_t
interface = 0;
464 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
469 if (ipv4Interface->
GetDevice () == device)
471 if (ipv4Interface->
IsUp ())
478 NS_LOG_LOGIC (
"Dropping received packet -- interface is down");
480 packet->RemoveHeader (ipHeader);
492 packet->RemoveHeader (ipHeader);
497 packet->RemoveAtEnd (packet->GetSize () - ipHeader.
GetPayloadSize ());
502 NS_LOG_LOGIC (
"Dropping received packet -- checksum not ok");
511 socket->
ForwardUp (packet, ipHeader, ipv4Interface);
522 NS_LOG_WARN (
"No route found for forwarding packet. Drop.");
569 NS_LOG_FUNCTION (
this << packet << source << destination << uint32_t (protocol) << route);
572 bool mayFragment =
true;
599 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 1: limited broadcast");
600 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, tos, mayFragment);
601 uint32_t ifaceIndex = 0;
602 for (Ipv4InterfaceList::iterator ifaceIter =
m_interfaces.begin ();
603 ifaceIter !=
m_interfaces.end (); ifaceIter++, ifaceIndex++)
613 outInterface->
Send (packetCopy, destination);
619 uint32_t ifaceIndex = 0;
620 for (Ipv4InterfaceList::iterator ifaceIter =
m_interfaces.begin ();
621 ifaceIter !=
m_interfaces.end (); ifaceIter++, ifaceIndex++)
632 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, tos, mayFragment);
635 packetCopy->AddHeader (ipHeader);
637 outInterface->
Send (packetCopy, destination);
647 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 3: passed in with route");
648 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, tos, mayFragment);
661 NS_FATAL_ERROR (
"Ipv4L3Protocol::Send case 4: This case not yet implemented");
664 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 5: passed in with no route " << destination);
667 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, tos, mayFragment);
675 NS_LOG_ERROR (
"Ipv4L3Protocol::Send: m_routingProtocol == 0");
698 uint16_t payloadSize,
703 NS_LOG_FUNCTION (
this << source << destination << (uint16_t)protocol << payloadSize << (uint16_t)ttl << (uint16_t)tos << mayFragment);
711 if (mayFragment ==
true)
745 int32_t
interface = GetInterfaceForDevice (outDev);
748 NS_LOG_LOGIC (
"Send via NetDevice ifIndex " << outDev->GetIfIndex () <<
" ipv4InterfaceIndex " << interface);
752 if (outInterface->IsUp ())
755 if ( packet->
GetSize () > outInterface->GetDevice ()->GetMtu () )
757 std::list<Ptr<Packet> > listFragments;
758 DoFragmentation (packet, outInterface->GetDevice ()->GetMtu (), listFragments);
759 for (
std::list<
Ptr<Packet> >::iterator it = listFragments.begin (); it != listFragments.end (); it++ )
762 outInterface->Send (*it, route->
GetGateway ());
768 outInterface->Send (packet, route->
GetGateway ());
781 if (outInterface->IsUp ())
784 if ( packet->
GetSize () > outInterface->GetDevice ()->GetMtu () )
786 std::list<Ptr<Packet> > listFragments;
787 DoFragmentation (packet, outInterface->GetDevice ()->GetMtu (), listFragments);
788 for (
std::list<
Ptr<Packet> >::iterator it = listFragments.begin (); it != listFragments.end (); it++ )
818 std::map<uint32_t, uint32_t> ttlMap = mrtentry->GetOutputTtlMap ();
819 std::map<uint32_t, uint32_t>::iterator mapIter;
821 for (mapIter = ttlMap.begin (); mapIter != ttlMap.end (); mapIter++)
823 uint32_t interfaceId = mapIter->first;
835 NS_LOG_LOGIC (
"Forward multicast via interface " << interfaceId);
856 ipHeader.SetTtl (ipHeader.GetTtl () - 1);
857 if (ipHeader.GetTtl () == 0)
861 ipHeader.GetDestination ().IsBroadcast () ==
false &&
862 ipHeader.GetDestination ().IsMulticast () ==
false)
865 icmp->SendTimeExceededTtl (ipHeader, packet);
884 NS_LOG_LOGIC (
"Received a fragment, processing " << *p );
885 bool isPacketComplete;
887 if ( isPacketComplete ==
false)
891 NS_LOG_LOGIC (
"Got last fragment, Packet is complete " << *p );
918 bool subnetDirected =
false;
925 subnetDirected =
true;
928 if (subnetDirected ==
false)
930 GetIcmp ()->SendDestUnreachPort (ip, copy);
941 bool retVal = interface->
AddAddress (address);
1022 if (iaddr.
GetScope () > scope)
continue;
1053 NS_LOG_WARN (
"Could not find source address for " << dst <<
" and scope "
1054 << scope <<
", returning 0");
1079 return interface->
GetDevice ()->GetMtu ();
1087 return interface->
IsUp ();
1095 interface->
SetUp ();
1121 NS_LOG_LOGIC (
"Forwarding state: " << interface->IsForwarding ());
1122 return interface->IsForwarding ();
1147 (*i)->SetForwarding (forward);
1176 NS_LOG_LOGIC (
"Route input failure-- dropping packet to " << ipHeader <<
" with errno " << sockErrno);
1196 "IPv4 fragmentation implementation only works without option headers." );
1198 uint16_t offset = 0;
1199 bool moreFragment =
true;
1200 uint16_t originalOffset = 0;
1201 bool alreadyFragmented =
false;
1202 uint32_t currentFragmentablePartSize = 0;
1206 alreadyFragmented =
true;
1213 uint32_t fragmentSize = (outIfaceMtu - ipv4Header.
GetSerializedSize () ) & ~uint32_t (0x7);
1215 NS_LOG_LOGIC (
"Fragmenting - Target Size: " << fragmentSize );
1221 if (p->
GetSize () > offset + fragmentSize )
1223 moreFragment =
true;
1224 currentFragmentablePartSize = fragmentSize;
1229 moreFragment =
false;
1230 currentFragmentablePartSize = p->
GetSize () - offset;
1231 if (alreadyFragmented)
1241 NS_LOG_LOGIC (
"Fragment creation - " << offset <<
", " << currentFragmentablePartSize );
1255 NS_LOG_LOGIC (
"New fragment Header " << fragmentHeader);
1258 std::ostringstream oss;
1259 fragment->
Print (oss);
1263 listFragments.push_back (fragment);
1265 offset += currentFragmentablePartSize;
1268 while (moreFragment);
1280 std::pair<uint64_t, uint32_t> key;
1284 key.first = addressCombination;
1285 key.second = idProto;
1289 MapFragments_t::iterator it =
m_fragments.find (key);
1292 fragments = Create<Fragments> ();
1293 m_fragments.insert (std::make_pair (key, fragments));
1296 key, ipHeader, iif);
1300 fragments = it->second;
1325 : m_moreFragment (0)
1340 std::list<std::pair<Ptr<Packet>, uint16_t> >::iterator it;
1344 if (it->second > fragmentOffset)
1352 m_moreFragment = moreFragment;
1363 bool ret = !m_moreFragment &&
m_fragments.size () > 0;
1367 uint16_t lastEndOffset = 0;
1372 NS_LOG_LOGIC (
"Checking overlaps " << lastEndOffset <<
" - " << it->second );
1374 if (lastEndOffset < it->second)
1380 uint16_t fragmentEnd = it->first->GetSize () + it->second;
1381 lastEndOffset = std::max ( lastEndOffset, fragmentEnd );
1393 std::list<std::pair<Ptr<Packet>, uint16_t> >::const_iterator it =
m_fragments.begin ();
1396 uint16_t lastEndOffset = 0;
1400 if ( lastEndOffset > it->second )
1406 uint32_t newStart = lastEndOffset - it->second;
1407 if ( it->first->GetSize () > newStart )
1409 uint32_t newSize = it->first->GetSize () - newStart;
1410 Ptr<Packet> tempFragment = it->first->CreateFragment (newStart, newSize);
1419 lastEndOffset = p->
GetSize ();
1430 std::list<std::pair<Ptr<Packet>, uint16_t> >::const_iterator it =
m_fragments.begin ();
1433 uint16_t lastEndOffset = 0;
1442 if ( lastEndOffset > it->second )
1444 uint32_t newStart = lastEndOffset - it->second;
1445 uint32_t newSize = it->first->GetSize () - newStart;
1446 Ptr<Packet> tempFragment = it->first->CreateFragment (newStart, newSize);
1449 else if ( lastEndOffset == it->second )
1454 lastEndOffset = p->
GetSize ();
1465 MapFragments_t::iterator it =
m_fragments.find (key);
1466 Ptr<Packet> packet = it->second->GetPartialPacket ();
1472 icmp->SendTimeExceededTtl (ipHeader, packet);
uint32_t RemoveHeader(Header &header)
void SetForwarding(bool val)
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberContainer)
void SetDefaultTtl(uint8_t ttl)
virtual void SetIpForward(bool forward)
void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Receive a packet.
Ptr< Packet > GetPacket() const
Get the entire packet.
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
virtual void NotifyNewAggregate()
Time m_fragmentExpirationTimeout
Ipv4Mask GetMask(void) const
Ipv4Address GetLocal(void) const
uint8_t GetTos(void) const
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_sendOutgoingTrace
a class to represent an Ipv4 address mask
void Send(Ptr< Packet > p, Ipv4Address dest)
static bool ChecksumEnabled(void)
bool IsLocalMulticast(void) const
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual bool GetWeakEsModel(void) const
void SetNode(Ptr< Node > node)
uint32_t GetSize(void) const
bool IsMulticast(void) const
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
virtual void DoDispose(void)
int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const
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...
bool AddAddress(Ipv4InterfaceAddress address)
uint32_t AddInterface(Ptr< NetDevice > device)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
TracedCallback< const Ipv4Header &, Ptr< const Packet >, DropReason, Ptr< Ipv4 >, uint32_t > m_dropTrace
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer...
void Print(std::ostream &os) const
Ptr< Ipv4RoutingProtocol > m_routingProtocol
#define NS_FATAL_ERROR(msg)
fatal error handling
a polymophic address class
Ipv4InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const
void SetSource(Ipv4Address src)
Ptr< T > CreateObject(void)
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
Ptr< NetDevice > GetOutputDevice(void) const
Ipv4InterfaceList m_interfaces
bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const
Determine whether address and interface corresponding to received packet can be accepted for local de...
Ptr< Packet > GetPartialPacket() const
Get the complete part of the packet.
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_rxTrace
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
void AddAtEnd(Ptr< const Packet > packet)
hold objects of type ns3::Time
Ptr< Icmpv4L4Protocol > GetIcmp(void) const
Get ICMPv4 protocol.
void SetGateway(Ipv4Address gw)
TracedCallback< Ptr< const Packet >, Ptr< Ipv4 >, uint32_t > m_txTrace
Hold an unsigned integer type.
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_localDeliverTrace
bool IsEntire() const
If all fragments have been added.
void SetMetric(uint32_t i, uint16_t metric)
NS_OBJECT_ENSURE_REGISTERED(AntennaModel)
virtual void DoDispose(void)
bool IsBroadcast(void) const
T * PeekPointer(const Ptr< T > &p)
Ptr< NetDevice > GetDevice(uint32_t index) const
Ptr< Socket > CreateRawSocket(void)
Creates a raw socket.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
virtual enum RxStatus Receive(Ptr< Packet > p, Ipv4Header const &header, Ptr< Ipv4Interface > incomingInterface)=0
void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)
static TypeId GetTypeId(void)
bool IsUp(uint32_t i) const
MapFragments_t m_fragments
The hash of fragmented packets.
void SendWithHeader(Ptr< Packet > packet, Ipv4Header ipHeader, Ptr< Ipv4Route > route)
void IpForward(Ptr< Ipv4Route > rtentry, Ptr< const Packet > p, const Ipv4Header &header)
#define NS_LOG_LOGIC(msg)
Ipv4Address GetGateway(void) const
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex)
bool IsSecondary(void) const
void AddFragment(Ptr< Packet > fragment, uint16_t fragmentOffset, bool moreFragment)
Add a fragment.
Ipv4InterfaceAddress GetAddress(uint32_t index) const
virtual void NotifyNewAggregate(void)
Access to the Ipv4 forwarding table, interfaces, and configuration.
uint32_t GetNDevices(void) const
void SetForwarding(uint32_t i, bool val)
Ptr< Packet > Copy(void) const
static const uint16_t PROT_NUMBER
int32_t GetInterfaceForAddress(Ipv4Address addr) const
Return the interface number of the interface that has been assigned the specified IP address...
bool IsEqual(const Ipv4Address &other) const
Comparison operation between two Ipv4Addresses.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
uint16_t m_identification
void DoFragmentation(Ptr< Packet > packet, uint32_t outIfaceMtu, std::list< Ptr< Packet > > &listFragments)
Fragment a packet.
void RouteInputError(Ptr< const Packet > p, const Ipv4Header &ipHeader, Socket::SocketErrno sockErrno)
Ipv4Header BuildHeader(Ipv4Address source, Ipv4Address destination, uint8_t protocol, uint16_t payloadSize, uint8_t ttl, uint8_t tos, bool mayFragment)
void SendRealOut(Ptr< Ipv4Route > route, Ptr< Packet > packet, Ipv4Header const &ipHeader)
void SetOutputDevice(Ptr< NetDevice > outputDevice)
static Ipv4Address GetLoopback(void)
static Ipv4Mask GetLoopback(void)
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
Ptr< NetDevice > GetNetDevice(uint32_t i)
uint16_t GetMetric(void) const
#define NS_ASSERT_MSG(condition, message)
bool ProcessFragment(Ptr< Packet > &packet, Ipv4Header &ipHeader, uint32_t iif)
Process a packet fragment.
uint32_t AddIpv4Interface(Ptr< Ipv4Interface > interface)
void Remove(Ptr< IpL4Protocol > protocol)
Ipv4 addresses are stored in host order in this class.
Ipv4Address GetBroadcast(void) const
virtual void SetWeakEsModel(bool model)
void SetNode(Ptr< Node > node)
uint32_t AddDevice(Ptr< NetDevice > device)
uint32_t GetId(void) const
a class to store IPv4 address information on an interface
void DeleteRawSocket(Ptr< Socket > socket)
Deletes a particular raw socket.
bool AddAddress(uint32_t i, Ipv4InterfaceAddress address)
Ptr< NetDevice > GetDevice(void) const
Ptr< Ipv4Interface > GetInterface(uint32_t i) const
static uint16_t GetStaticProtocolNumber(void)
void Insert(Ptr< IpL4Protocol > protocol)
bool RemovePacketTag(Tag &tag)
void LocalDeliver(Ptr< const Packet > p, Ipv4Header const &ip, uint32_t iif)
void IpMulticastForward(Ptr< Ipv4MulticastRoute > mrtentry, Ptr< const Packet > p, const Ipv4Header &header)
uint32_t GetNAddresses(uint32_t interface) const
MapFragmentsTimers_t m_fragmentsTimers
uint16_t GetMtu(uint32_t i) const
void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
bool ForwardUp(Ptr< const Packet > p, Ipv4Header ipHeader, Ptr< Ipv4Interface > incomingInterface)
Ipv4InterfaceAddress::InterfaceAddressScope_e GetScope(void) const
uint32_t GetNInterfaces(void) const
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range. Both limits are inclusive.
Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const
void SetMetric(uint16_t metric)
#define NS_LOG_ERROR(msg)
bool IsUnicast(Ipv4Address ad, Ipv4Mask interfaceMask) const
uint8_t GetTtl(void) const
TracedCallback< const Ipv4Header &, Ptr< const Packet >, uint32_t > m_unicastForwardTrace
contain a set of ns3::Object pointers.
uint16_t GetMetric(uint32_t i) const
Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const
Get the routing protocol to be used by this Ipv4 stack.
Ptr< T > GetObject(void) const
uint32_t GetNAddresses(void) const
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
virtual ~Ipv4L3Protocol()
void HandleFragmentsTimeout(std::pair< uint64_t, uint32_t > key, Ipv4Header &ipHeader, uint32_t iif)
Process the timeout for packet fragments.
virtual bool GetIpForward(void) const
void AddHeader(const Header &header)
static const uint8_t PROT_NUMBER
static const uint16_t PROT_NUMBER
void SetDestination(Ipv4Address dest)
bool IsForwarding(uint32_t i) const
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...
void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)
Register a new routing protocol to be used by this Ipv4 stack.