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)
114 if ((*i)->GetProtocolNumber () == protocolNumber)
219 if (it->second.IsRunning ())
221 it->second.Cancel ();
248 device = CreateObject<LoopbackNetDevice> ();
251 interface->SetDevice (device);
252 interface->SetNode (
m_node);
254 interface->AddAddress (ifaceAddr);
284 interface->SetNode (
m_node);
285 interface->SetDevice (device);
319 int32_t
interface = 0;
320 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
324 for (uint32_t j = 0; j < (*i)->GetNAddresses (); j++)
326 if ((*i)->GetAddress (j).GetLocal () == address)
341 int32_t
interface = 0;
342 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
346 for (uint32_t j = 0; j < (*i)->GetNAddresses (); j++)
348 if ((*i)->GetAddress (j).GetLocal ().CombineMask (mask) == address.
CombineMask (mask))
362 int32_t
interface = 0;
363 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
367 if ((*i)->GetDevice () == device)
385 NS_LOG_LOGIC (
"For me (destination " << address <<
" match)");
398 if (MulticastCheckGroup (iif, address ))
417 if (j == uint32_t (iif))
continue;
423 NS_LOG_LOGIC (
"For me (destination " << address <<
" match) on another interface");
429 NS_LOG_LOGIC (
"For me (interface broadcast address on another interface)");
442 NS_LOG_FUNCTION (
this << device << p << protocol << from << to << packetType);
444 NS_LOG_LOGIC (
"Packet from " << from <<
" received on node " <<
447 uint32_t
interface = 0;
451 for (Ipv4InterfaceList::const_iterator i =
m_interfaces.begin ();
456 if (ipv4Interface->
GetDevice () == device)
458 if (ipv4Interface->
IsUp ())
465 NS_LOG_LOGIC (
"Dropping received packet -- interface is down");
467 packet->RemoveHeader (ipHeader);
479 packet->RemoveHeader (ipHeader);
484 packet->RemoveAtEnd (packet->GetSize () - ipHeader.
GetPayloadSize ());
489 NS_LOG_LOGIC (
"Dropping received packet -- checksum not ok");
498 socket->
ForwardUp (packet, ipHeader, ipv4Interface);
509 NS_LOG_WARN (
"No route found for forwarding packet. Drop.");
554 NS_LOG_FUNCTION (
this << packet << source << destination << uint32_t (protocol) << route);
557 bool mayFragment =
true;
584 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 1: limited broadcast");
585 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, tos, mayFragment);
586 uint32_t ifaceIndex = 0;
587 for (Ipv4InterfaceList::iterator ifaceIter =
m_interfaces.begin ();
588 ifaceIter !=
m_interfaces.end (); ifaceIter++, ifaceIndex++)
598 outInterface->
Send (packetCopy, destination);
604 uint32_t ifaceIndex = 0;
605 for (Ipv4InterfaceList::iterator ifaceIter =
m_interfaces.begin ();
606 ifaceIter !=
m_interfaces.end (); ifaceIter++, ifaceIndex++)
617 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, tos, mayFragment);
620 packetCopy->AddHeader (ipHeader);
622 outInterface->
Send (packetCopy, destination);
632 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 3: passed in with route");
633 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, tos, mayFragment);
646 NS_FATAL_ERROR (
"Ipv4L3Protocol::Send case 4: This case not yet implemented");
649 NS_LOG_LOGIC (
"Ipv4L3Protocol::Send case 5: passed in with no route " << destination);
652 ipHeader =
BuildHeader (source, destination, protocol, packet->
GetSize (), ttl, tos, mayFragment);
660 NS_LOG_ERROR (
"Ipv4L3Protocol::Send: m_routingProtocol == 0");
683 uint16_t payloadSize,
688 NS_LOG_FUNCTION (
this << source << destination << (uint16_t)protocol << payloadSize << (uint16_t)ttl << mayFragment);
696 if (mayFragment ==
true)
733 NS_LOG_LOGIC (
"Send via NetDevice ifIndex " << outDev->GetIfIndex () <<
" ipv4InterfaceIndex " << interface);
737 if (outInterface->
IsUp ())
742 std::list<Ptr<Packet> > listFragments;
744 for (
std::list<
Ptr<Packet> >::iterator it = listFragments.begin (); it != listFragments.end (); it++ )
766 if (outInterface->
IsUp ())
771 std::list<Ptr<Packet> > listFragments;
773 for (
std::list<
Ptr<Packet> >::iterator it = listFragments.begin (); it != listFragments.end (); it++ )
803 std::map<uint32_t, uint32_t> ttlMap = mrtentry->GetOutputTtlMap ();
804 std::map<uint32_t, uint32_t>::iterator mapIter;
806 for (mapIter = ttlMap.begin (); mapIter != ttlMap.end (); mapIter++)
808 uint32_t interfaceId = mapIter->first;
820 NS_LOG_LOGIC (
"Forward multicast via interface " << interfaceId);
841 ipHeader.SetTtl (ipHeader.GetTtl () - 1);
842 if (ipHeader.GetTtl () == 0)
846 ipHeader.GetDestination ().IsBroadcast () ==
false &&
847 ipHeader.GetDestination ().IsMulticast () ==
false)
850 icmp->SendTimeExceededTtl (ipHeader, packet);
869 NS_LOG_LOGIC (
"Received a fragment, processing " << *p );
870 bool isPacketComplete;
872 if ( isPacketComplete ==
false)
876 NS_LOG_LOGIC (
"Got last fragment, Packet is complete " << *p );
903 bool subnetDirected =
false;
910 subnetDirected =
true;
913 if (subnetDirected ==
false)
915 GetIcmp ()->SendDestUnreachPort (ip, copy);
926 bool retVal = interface->
AddAddress (address);
982 if (iaddr.
GetScope () > scope)
continue;
1013 NS_LOG_WARN (
"Could not find source address for " << dst <<
" and scope "
1014 << scope <<
", returning 0");
1044 return interface->
IsUp ();
1052 interface->
SetUp ();
1078 NS_LOG_LOGIC (
"Forwarding state: " << interface->IsForwarding ());
1079 return interface->IsForwarding ();
1103 (*i)->SetForwarding (forward);
1129 NS_LOG_LOGIC (
"Route input failure-- dropping packet to " << ipHeader <<
" with errno " << sockErrno);
1149 "IPv4 fragmentation implementation only works without option headers." );
1151 uint16_t offset = 0;
1152 bool moreFragment =
true;
1153 uint16_t originalOffset = 0;
1154 bool alreadyFragmented =
false;
1155 uint32_t currentFragmentablePartSize = 0;
1159 alreadyFragmented =
true;
1166 uint32_t fragmentSize = (outIfaceMtu - ipv4Header.
GetSerializedSize () ) & ~uint32_t (0x7);
1168 NS_LOG_LOGIC (
"Fragmenting - Target Size: " << fragmentSize );
1174 if (p->
GetSize () > offset + fragmentSize )
1176 moreFragment =
true;
1177 currentFragmentablePartSize = fragmentSize;
1182 moreFragment =
false;
1183 currentFragmentablePartSize = p->
GetSize () - offset;
1184 if (alreadyFragmented)
1194 NS_LOG_LOGIC (
"Fragment creation - " << offset <<
", " << currentFragmentablePartSize );
1208 NS_LOG_LOGIC (
"New fragment Header " << fragmentHeader);
1211 std::ostringstream oss;
1212 fragment->
Print (oss);
1216 listFragments.push_back (fragment);
1218 offset += currentFragmentablePartSize;
1221 while (moreFragment);
1233 std::pair<uint64_t, uint32_t> key;
1237 key.first = addressCombination;
1238 key.second = idProto;
1242 MapFragments_t::iterator it =
m_fragments.find (key);
1245 fragments = Create<Fragments> ();
1246 m_fragments.insert (std::make_pair (key, fragments));
1249 key, ipHeader, iif);
1253 fragments = it->second;
1278 : m_moreFragment (0)
1289 NS_LOG_FUNCTION (
this << fragment <<
" " << fragmentOffset <<
" " << moreFragment);
1291 std::list<std::pair<Ptr<Packet>, uint16_t> >::iterator it;
1295 if (it->second > fragmentOffset)
1303 m_moreFragment = moreFragment;
1314 bool ret = !m_moreFragment &&
m_fragments.size () > 0;
1318 uint16_t lastEndOffset = 0;
1323 NS_LOG_LOGIC (
"Checking overlaps " << lastEndOffset <<
" - " << it->second );
1325 if (lastEndOffset < it->second)
1331 uint16_t fragmentEnd = it->first->GetSize () + it->second;
1332 lastEndOffset = std::max ( lastEndOffset, fragmentEnd );
1344 std::list<std::pair<Ptr<Packet>, uint16_t> >::const_iterator it =
m_fragments.begin ();
1347 uint16_t lastEndOffset = 0;
1351 if ( lastEndOffset > it->second )
1357 uint32_t newStart = lastEndOffset - it->second;
1358 if ( it->first->GetSize () > newStart )
1360 uint32_t newSize = it->first->GetSize () - newStart;
1361 Ptr<Packet> tempFragment = it->first->CreateFragment (newStart, newSize);
1370 lastEndOffset = p->
GetSize ();
1379 std::list<std::pair<Ptr<Packet>, uint16_t> >::const_iterator it =
m_fragments.begin ();
1382 uint16_t lastEndOffset = 0;
1391 if ( lastEndOffset > it->second )
1393 uint32_t newStart = lastEndOffset - it->second;
1394 uint32_t newSize = it->first->GetSize () - newStart;
1395 Ptr<Packet> tempFragment = it->first->CreateFragment (newStart, newSize);
1398 else if ( lastEndOffset == it->second )
1403 lastEndOffset = p->
GetSize ();
1414 MapFragments_t::iterator it =
m_fragments.find (key);
1415 Ptr<Packet> packet = it->second->GetPartialPacket ();
1421 icmp->SendTimeExceededTtl (ipHeader, packet);