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)
748 NS_LOG_LOGIC (
"Send via NetDevice ifIndex " << outDev->GetIfIndex () <<
" ipv4InterfaceIndex " << interface);
752 if (outInterface->
IsUp ())
757 std::list<Ptr<Packet> > listFragments;
759 for (
std::list<
Ptr<Packet> >::iterator it = listFragments.begin (); it != listFragments.end (); it++ )
781 if (outInterface->
IsUp ())
786 std::list<Ptr<Packet> > 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);
999 if (iaddr.
GetScope () > scope)
continue;
1030 NS_LOG_WARN (
"Could not find source address for " << dst <<
" and scope "
1031 << scope <<
", returning 0");
1064 return interface->
IsUp ();
1072 interface->
SetUp ();
1098 NS_LOG_LOGIC (
"Forwarding state: " << interface->IsForwarding ());
1099 return interface->IsForwarding ();
1124 (*i)->SetForwarding (forward);
1153 NS_LOG_LOGIC (
"Route input failure-- dropping packet to " << ipHeader <<
" with errno " << sockErrno);
1173 "IPv4 fragmentation implementation only works without option headers." );
1175 uint16_t offset = 0;
1176 bool moreFragment =
true;
1177 uint16_t originalOffset = 0;
1178 bool alreadyFragmented =
false;
1179 uint32_t currentFragmentablePartSize = 0;
1183 alreadyFragmented =
true;
1190 uint32_t fragmentSize = (outIfaceMtu - ipv4Header.
GetSerializedSize () ) & ~uint32_t (0x7);
1192 NS_LOG_LOGIC (
"Fragmenting - Target Size: " << fragmentSize );
1198 if (p->
GetSize () > offset + fragmentSize )
1200 moreFragment =
true;
1201 currentFragmentablePartSize = fragmentSize;
1206 moreFragment =
false;
1207 currentFragmentablePartSize = p->
GetSize () - offset;
1208 if (alreadyFragmented)
1218 NS_LOG_LOGIC (
"Fragment creation - " << offset <<
", " << currentFragmentablePartSize );
1232 NS_LOG_LOGIC (
"New fragment Header " << fragmentHeader);
1235 std::ostringstream oss;
1236 fragment->
Print (oss);
1240 listFragments.push_back (fragment);
1242 offset += currentFragmentablePartSize;
1245 while (moreFragment);
1257 std::pair<uint64_t, uint32_t> key;
1261 key.first = addressCombination;
1262 key.second = idProto;
1266 MapFragments_t::iterator it =
m_fragments.find (key);
1269 fragments = Create<Fragments> ();
1270 m_fragments.insert (std::make_pair (key, fragments));
1273 key, ipHeader, iif);
1277 fragments = it->second;
1302 : m_moreFragment (0)
1317 std::list<std::pair<Ptr<Packet>, uint16_t> >::iterator it;
1321 if (it->second > fragmentOffset)
1329 m_moreFragment = moreFragment;
1340 bool ret = !m_moreFragment &&
m_fragments.size () > 0;
1344 uint16_t lastEndOffset = 0;
1349 NS_LOG_LOGIC (
"Checking overlaps " << lastEndOffset <<
" - " << it->second );
1351 if (lastEndOffset < it->second)
1357 uint16_t fragmentEnd = it->first->GetSize () + it->second;
1358 lastEndOffset = std::max ( lastEndOffset, fragmentEnd );
1370 std::list<std::pair<Ptr<Packet>, uint16_t> >::const_iterator it =
m_fragments.begin ();
1373 uint16_t lastEndOffset = 0;
1377 if ( lastEndOffset > it->second )
1383 uint32_t newStart = lastEndOffset - it->second;
1384 if ( it->first->GetSize () > newStart )
1386 uint32_t newSize = it->first->GetSize () - newStart;
1387 Ptr<Packet> tempFragment = it->first->CreateFragment (newStart, newSize);
1396 lastEndOffset = p->
GetSize ();
1407 std::list<std::pair<Ptr<Packet>, uint16_t> >::const_iterator it =
m_fragments.begin ();
1410 uint16_t lastEndOffset = 0;
1419 if ( lastEndOffset > it->second )
1421 uint32_t newStart = lastEndOffset - it->second;
1422 uint32_t newSize = it->first->GetSize () - newStart;
1423 Ptr<Packet> tempFragment = it->first->CreateFragment (newStart, newSize);
1426 else if ( lastEndOffset == it->second )
1431 lastEndOffset = p->
GetSize ();
1442 MapFragments_t::iterator it =
m_fragments.find (key);
1443 Ptr<Packet> packet = it->second->GetPartialPacket ();
1449 icmp->SendTimeExceededTtl (ipHeader, packet);