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 (
"DefaultTtl",
"The TTL value set by default on all outgoing packets generated on this node.",
60 MakeUintegerChecker<uint8_t> ())
61 .AddAttribute (
"FragmentExpirationTimeout",
62 "When this timeout expires, the fragments will be cleared from the buffer.",
66 .AddTraceSource (
"Tx",
"Send ipv4 packet to outgoing interface.",
68 .AddTraceSource (
"Rx",
"Receive ipv4 packet from incoming interface.",
70 .AddTraceSource (
"Drop",
"Drop ipv4 packet",
72 .AddAttribute (
"InterfaceList",
"The set of Ipv4 interfaces associated to this Ipv4 stack.",
75 MakeObjectVectorChecker<Ipv4Interface> ())
77 .AddTraceSource (
"SendOutgoing",
"A newly-generated packet by this node is about to be queued for transmission",
79 .AddTraceSource (
"UnicastForward",
"A unicast IPv4 packet was received by this node and is being forwarded to another node",
81 .AddTraceSource (
"LocalDeliver",
"An IPv4 packet was received by/for this node, and it is being forward up the stack",
89 : m_identification (0)
109 if ((*i)->GetProtocolNumber () == protocolNumber)
214 if (it->second.IsRunning ())
216 it->second.Cancel ();
243 device = CreateObject<LoopbackNetDevice> ();
246 interface->SetDevice (device);
247 interface->SetNode (
m_node);
249 interface->AddAddress (ifaceAddr);
279 interface->SetNode (
m_node);
280 interface->SetDevice (device);
314 int32_t
interface = 0;
315 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
319 for (uint32_t j = 0; j < (*i)->GetNAddresses (); j++)
321 if ((*i)->GetAddress (j).GetLocal () == address)
336 int32_t
interface = 0;
337 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
341 for (uint32_t j = 0; j < (*i)->GetNAddresses (); j++)
343 if ((*i)->GetAddress (j).GetLocal ().CombineMask (mask) == address.
CombineMask (mask))
357 int32_t
interface = 0;
358 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
362 if ((*i)->GetDevice () == device)
380 NS_LOG_LOGIC (
"For me (destination " << address <<
" match)");
393 if (MulticastCheckGroup (iif, address ))
412 if (j == uint32_t (iif))
continue;
418 NS_LOG_LOGIC (
"For me (destination " << address <<
" match) on another interface");
424 NS_LOG_LOGIC (
"For me (interface broadcast address on another interface)");
437 NS_LOG_FUNCTION (
this << device << p << protocol << from << to << packetType);
439 NS_LOG_LOGIC (
"Packet from " << from <<
" received on node " <<
442 uint32_t
interface = 0;
446 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
451 if (ipv4Interface->
GetDevice () == device)
453 if (ipv4Interface->
IsUp ())
460 NS_LOG_LOGIC (
"Dropping received packet -- interface is down");
462 packet->RemoveHeader (ipHeader);
474 packet->RemoveHeader (ipHeader);
479 packet->RemoveAtEnd (packet->GetSize () - ipHeader.
GetPayloadSize ());
484 NS_LOG_LOGIC (
"Dropping received packet -- checksum not ok");
493 socket->
ForwardUp (packet, ipHeader, ipv4Interface);
504 NS_LOG_WARN (
"No route found for forwarding packet. Drop.");
545 NS_LOG_FUNCTION (
this << packet << source << destination << uint32_t (protocol) << route);
548 bool mayFragment =
true;
567 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 1: limited broadcast");
568 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, mayFragment);
569 uint32_t ifaceIndex = 0;
570 for (Ipv4InterfaceList::iterator ifaceIter =
m_interfaces.begin ();
571 ifaceIter !=
m_interfaces.end (); ifaceIter++, ifaceIndex++)
581 outInterface->
Send (packetCopy, destination);
587 uint32_t ifaceIndex = 0;
588 for (Ipv4InterfaceList::iterator ifaceIter =
m_interfaces.begin ();
589 ifaceIter !=
m_interfaces.end (); ifaceIter++, ifaceIndex++)
600 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, mayFragment);
603 packetCopy->AddHeader (ipHeader);
605 outInterface->
Send (packetCopy, destination);
615 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 3: passed in with route");
616 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, mayFragment);
629 NS_FATAL_ERROR (
"Ipv4L3Protocol::Send case 4: This case not yet implemented");
632 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 5: passed in with no route " << destination);
635 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, mayFragment);
643 NS_LOG_ERROR (
"Ipv4L3Protocol::Send: m_routingProtocol == 0");
666 uint16_t payloadSize,
670 NS_LOG_FUNCTION (
this << source << destination << (uint16_t)protocol << payloadSize << (uint16_t)ttl << mayFragment);
677 if (mayFragment ==
true)
715 NS_LOG_LOGIC (
"Send via NetDevice ifIndex " << outDev->GetIfIndex () <<
" ipv4InterfaceIndex " << interface);
719 if (outInterface->
IsUp ())
724 std::list<Ptr<Packet> > listFragments;
726 for (
std::list<
Ptr<Packet> >::iterator it = listFragments.begin (); it != listFragments.end (); it++ )
748 if (outInterface->
IsUp ())
753 std::list<Ptr<Packet> > listFragments;
755 for (
std::list<
Ptr<Packet> >::iterator it = listFragments.begin (); it != listFragments.end (); it++ )
785 std::map<uint32_t, uint32_t> ttlMap = mrtentry->GetOutputTtlMap ();
786 std::map<uint32_t, uint32_t>::iterator mapIter;
788 for (mapIter = ttlMap.begin (); mapIter != ttlMap.end (); mapIter++)
790 uint32_t interfaceId = mapIter->first;
802 NS_LOG_LOGIC (
"Forward multicast via interface " << interfaceId);
823 ipHeader.SetTtl (ipHeader.GetTtl () - 1);
824 if (ipHeader.GetTtl () == 0)
828 ipHeader.GetDestination ().IsBroadcast () ==
false &&
829 ipHeader.GetDestination ().IsMulticast () ==
false)
832 icmp->SendTimeExceededTtl (ipHeader, packet);
851 NS_LOG_LOGIC (
"Received a fragment, processing " << *p );
852 bool isPacketComplete;
854 if ( isPacketComplete ==
false)
858 NS_LOG_LOGIC (
"Got last fragment, Packet is complete " << *p );
885 bool subnetDirected =
false;
892 subnetDirected =
true;
895 if (subnetDirected ==
false)
897 GetIcmp ()->SendDestUnreachPort (ip, copy);
908 bool retVal = interface->
AddAddress (address);
964 if (iaddr.
GetScope () > scope)
continue;
995 NS_LOG_WARN (
"Could not find source address for " << dst <<
" and scope "
996 << scope <<
", returning 0");
1026 return interface->
IsUp ();
1034 interface->
SetUp ();
1060 NS_LOG_LOGIC (
"Forwarding state: " << interface->IsForwarding ());
1061 return interface->IsForwarding ();
1085 (*i)->SetForwarding (forward);
1111 NS_LOG_LOGIC (
"Route input failure-- dropping packet to " << ipHeader <<
" with errno " << sockErrno);
1131 "IPv4 fragmentation implementation only works without option headers." );
1133 uint16_t offset = 0;
1134 bool moreFragment =
true;
1135 uint16_t originalOffset = 0;
1136 bool alreadyFragmented =
false;
1137 uint32_t currentFragmentablePartSize = 0;
1141 alreadyFragmented =
true;
1148 uint32_t fragmentSize = (outIfaceMtu - ipv4Header.
GetSerializedSize () ) & ~uint32_t (0x7);
1150 NS_LOG_LOGIC (
"Fragmenting - Target Size: " << fragmentSize );
1156 if (p->
GetSize () > offset + fragmentSize )
1158 moreFragment =
true;
1159 currentFragmentablePartSize = fragmentSize;
1164 moreFragment =
false;
1165 currentFragmentablePartSize = p->
GetSize () - offset;
1166 if (alreadyFragmented)
1176 NS_LOG_LOGIC (
"Fragment creation - " << offset <<
", " << currentFragmentablePartSize );
1190 NS_LOG_LOGIC (
"New fragment Header " << fragmentHeader);
1193 std::ostringstream oss;
1194 fragment->
Print (oss);
1198 listFragments.push_back (fragment);
1200 offset += currentFragmentablePartSize;
1203 while (moreFragment);
1215 std::pair<uint64_t, uint32_t> key;
1219 key.first = addressCombination;
1220 key.second = idProto;
1224 MapFragments_t::iterator it =
m_fragments.find (key);
1227 fragments = Create<Fragments> ();
1228 m_fragments.insert (std::make_pair (key, fragments));
1231 key, ipHeader, iif);
1235 fragments = it->second;
1260 : m_moreFragment (0)
1271 NS_LOG_FUNCTION (
this << fragment <<
" " << fragmentOffset <<
" " << moreFragment);
1273 std::list<std::pair<Ptr<Packet>, uint16_t> >::iterator it;
1277 if (it->second > fragmentOffset)
1285 m_moreFragment = moreFragment;
1296 bool ret = !m_moreFragment &&
m_fragments.size () > 0;
1300 uint16_t lastEndOffset = 0;
1305 NS_LOG_LOGIC (
"Checking overlaps " << lastEndOffset <<
" - " << it->second );
1307 if (lastEndOffset < it->second)
1313 uint16_t fragmentEnd = it->first->GetSize () + it->second;
1314 lastEndOffset = std::max ( lastEndOffset, fragmentEnd );
1326 std::list<std::pair<Ptr<Packet>, uint16_t> >::const_iterator it =
m_fragments.begin ();
1329 uint16_t lastEndOffset = 0;
1333 if ( lastEndOffset > it->second )
1339 uint32_t newStart = lastEndOffset - it->second;
1340 if ( it->first->GetSize () > newStart )
1342 uint32_t newSize = it->first->GetSize () - newStart;
1343 Ptr<Packet> tempFragment = it->first->CreateFragment (newStart, newSize);
1352 lastEndOffset = p->
GetSize ();
1361 std::list<std::pair<Ptr<Packet>, uint16_t> >::const_iterator it =
m_fragments.begin ();
1364 uint16_t lastEndOffset = 0;
1373 if ( lastEndOffset > it->second )
1375 uint32_t newStart = lastEndOffset - it->second;
1376 uint32_t newSize = it->first->GetSize () - newStart;
1377 Ptr<Packet> tempFragment = it->first->CreateFragment (newStart, newSize);
1380 else if ( lastEndOffset == it->second )
1385 lastEndOffset = p->
GetSize ();
1396 MapFragments_t::iterator it =
m_fragments.find (key);
1397 Ptr<Packet> packet = it->second->GetPartialPacket ();
1403 icmp->SendTimeExceededTtl (ipHeader, packet);