A Discrete-Event Network Simulator
API
arp-l3-protocol.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "ns3/packet.h"
21 #include "ns3/log.h"
22 #include "ns3/node.h"
23 #include "ns3/net-device.h"
24 #include "ns3/object-vector.h"
25 #include "ns3/trace-source-accessor.h"
26 #include "ns3/pointer.h"
27 #include "ns3/string.h"
28 
29 #include "ipv4-l3-protocol.h"
30 #include "arp-l3-protocol.h"
31 #include "arp-header.h"
32 #include "arp-cache.h"
33 #include "arp-queue-disc-item.h"
34 #include "ipv4-interface.h"
35 #include "ns3/traffic-control-layer.h"
36 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("ArpL3Protocol");
40 
41 const uint16_t ArpL3Protocol::PROT_NUMBER = 0x0806;
42 
43 NS_OBJECT_ENSURE_REGISTERED (ArpL3Protocol);
44 
45 TypeId
47 {
48  static TypeId tid = TypeId ("ns3::ArpL3Protocol")
49  .SetParent<Object> ()
50  .AddConstructor<ArpL3Protocol> ()
51  .SetGroupName ("Internet")
52  .AddAttribute ("CacheList",
53  "The list of ARP caches",
56  MakeObjectVectorChecker<ArpCache> ())
57  .AddAttribute ("RequestJitter",
58  "The jitter in ms a node is allowed to wait "
59  "before sending an ARP request. Some jitter aims "
60  "to prevent collisions. By default, the model "
61  "will wait for a duration in ms defined by "
62  "a uniform random-variable between 0 and RequestJitter",
63  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=10.0]"),
65  MakePointerChecker<RandomVariableStream> ())
66  .AddTraceSource ("Drop",
67  "Packet dropped because not enough room "
68  "in pending queue for a specific cache entry.",
70  "ns3::Packet::TracedCallback")
71  ;
72  return tid;
73 }
74 
76  : m_tc (0)
77 {
78  NS_LOG_FUNCTION (this);
79 }
80 
82 {
83  NS_LOG_FUNCTION (this);
84 }
85 
86 int64_t
88 {
89  NS_LOG_FUNCTION (this << stream);
90  m_requestJitter->SetStream (stream);
91  return 1;
92 }
93 
94 void
96 {
97  NS_LOG_FUNCTION (this << node);
98  m_node = node;
99 }
100 
101 void
103 {
104  NS_LOG_FUNCTION (this << tc);
105  m_tc = tc;
106 }
107 
108 /*
109  * This method is called by AddAgregate and completes the aggregation
110  * by setting the node in the ipv4 stack
111  */
112 void
114 {
115  NS_LOG_FUNCTION (this);
116  if (m_node == 0)
117  {
118  Ptr<Node>node = this->GetObject<Node> ();
119  //verify that it's a valid node and that
120  //the node was not set before
121  if (node != 0)
122  {
123  this->SetNode (node);
124  }
125  }
127 }
128 
129 void
131 {
132  NS_LOG_FUNCTION (this);
133  for (CacheList::iterator i = m_cacheList.begin (); i != m_cacheList.end (); ++i)
134  {
135  Ptr<ArpCache> cache = *i;
136  cache->Dispose ();
137  }
138  m_cacheList.clear ();
139  m_node = 0;
140  m_tc = 0;
142 }
143 
146 {
147  NS_LOG_FUNCTION (this << device << interface);
149  Ptr<ArpCache> cache = CreateObject<ArpCache> ();
150  cache->SetDevice (device, interface);
151  NS_ASSERT (device->IsBroadcast ());
152  device->AddLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache));
153  cache->SetArpRequestCallback (MakeCallback (&ArpL3Protocol::SendArpRequest, this));
154  m_cacheList.push_back (cache);
155  return cache;
156 }
157 
160 {
161  NS_LOG_FUNCTION (this << device);
162  for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++)
163  {
164  if ((*i)->GetDevice () == device)
165  {
166  return *i;
167  }
168  }
169  NS_ASSERT (false);
170  // quiet compiler
171  return 0;
172 }
173 
174 void
175 ArpL3Protocol::Receive (Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
176  const Address &to, NetDevice::PacketType packetType)
177 {
178  NS_LOG_FUNCTION (this << device << p->GetSize () << protocol << from << to << packetType);
179 
180  Ptr<Packet> packet = p->Copy ();
181 
182  NS_LOG_LOGIC ("ARP: received packet of size "<< packet->GetSize ());
183 
184  Ptr<ArpCache> cache = FindCache (device);
185 
186  //
187  // If we're connected to a real world network, then some of the fields sizes
188  // in an ARP packet can vary in ways not seen in simulations. We need to be
189  // able to detect ARP packets with headers we don't recongnize and not process
190  // them instead of crashing. The ArpHeader will return 0 if it can't deal
191  // with the received header.
192  //
193  ArpHeader arp;
194  uint32_t size = packet->RemoveHeader (arp);
195  if (size == 0)
196  {
197  NS_LOG_LOGIC ("ARP: Cannot remove ARP header");
198  return;
199  }
200  NS_LOG_LOGIC ("ARP: received " << (arp.IsRequest () ? "request" : "reply") <<
201  " node=" << m_node->GetId () <<
202  ", got " <<
203  (arp.IsRequest () ? "request" : "reply") <<
204  " from " << arp.GetSourceIpv4Address () <<
205  " for address " << arp.GetDestinationIpv4Address () <<
206  "; we have addresses: ");
207  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
208  {
209  NS_LOG_LOGIC (cache->GetInterface ()->GetAddress (i).GetLocal () << ", ");
210  }
211 
217  bool found = false;
218  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
219  {
220  if (arp.IsRequest () && arp.GetDestinationIpv4Address () ==
221  cache->GetInterface ()->GetAddress (i).GetLocal ())
222  {
223  found = true;
224  NS_LOG_LOGIC ("node="<<m_node->GetId () <<", got request from " <<
225  arp.GetSourceIpv4Address () << " -- send reply");
227  arp.GetSourceHardwareAddress ());
228  break;
229  }
230  else if (arp.IsReply () &&
231  arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress (i).GetLocal ()) &&
232  arp.GetDestinationHardwareAddress () == device->GetAddress ())
233  {
234  found = true;
235  Ipv4Address from = arp.GetSourceIpv4Address ();
236  ArpCache::Entry *entry = cache->Lookup (from);
237  if (entry != 0)
238  {
239  if (entry->IsWaitReply ())
240  {
241  NS_LOG_LOGIC ("node="<< m_node->GetId () <<
242  ", got reply from " << arp.GetSourceIpv4Address ()
243  << " for waiting entry -- flush");
244  Address from_mac = arp.GetSourceHardwareAddress ();
245  entry->MarkAlive (from_mac);
247  while (pending.first != 0)
248  {
249  cache->GetInterface ()->Send (pending.first, pending.second,
250  arp.GetSourceIpv4Address ());
251  pending = entry->DequeuePending ();
252  }
253  }
254  else
255  {
256  // ignore this reply which might well be an attempt
257  // at poisening my arp cache.
258  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply from " <<
259  arp.GetSourceIpv4Address () <<
260  " for non-waiting entry -- drop");
261  m_dropTrace (packet);
262  }
263  }
264  else
265  {
266  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply for unknown entry -- drop");
267  m_dropTrace (packet);
268  }
269  break;
270  }
271  }
272  if (found == false)
273  {
274  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got request from " <<
275  arp.GetSourceIpv4Address () << " for unknown address " <<
276  arp.GetDestinationIpv4Address () << " -- drop");
277  }
278 }
279 
280 bool
281 ArpL3Protocol::Lookup (Ptr<Packet> packet, const Ipv4Header & ipHeader, Ipv4Address destination,
282  Ptr<NetDevice> device,
283  Ptr<ArpCache> cache,
284  Address *hardwareDestination)
285 {
286  NS_LOG_FUNCTION (this << packet << destination << device << cache << hardwareDestination);
287  ArpCache::Entry *entry = cache->Lookup (destination);
288  if (entry != 0)
289  {
290  if (entry->IsExpired ())
291  {
292  if (entry->IsDead ())
293  {
294  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
295  ", dead entry for " << destination << " expired -- send arp request");
296  entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
298  }
299  else if (entry->IsAlive ())
300  {
301  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
302  ", alive entry for " << destination << " expired -- send arp request");
303  entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
305  }
306  else
307  {
308  NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
309  }
310  }
311  else
312  {
313  if (entry->IsDead ())
314  {
315  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
316  ", dead entry for " << destination << " valid -- drop");
317  // add the Ipv4 header for tracing purposes
318  packet->AddHeader (ipHeader);
319  m_dropTrace (packet);
320  }
321  else if (entry->IsAlive ())
322  {
323  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
324  ", alive entry for " << destination << " valid -- send");
325  *hardwareDestination = entry->GetMacAddress ();
326  return true;
327  }
328  else if (entry->IsWaitReply ())
329  {
330  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
331  ", wait reply for " << destination << " valid -- drop previous");
332  if (!entry->UpdateWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader)))
333  {
334  // add the Ipv4 header for tracing purposes
335  packet->AddHeader (ipHeader);
336  m_dropTrace (packet);
337  }
338  }
339  else if (entry-> IsPermanent ())
340  {
341  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
342  ", permanent for " << destination << "valid -- send");
343  *hardwareDestination = entry->GetMacAddress ();
344  return true;
345  }
346  else
347  {
348  NS_LOG_LOGIC ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
349  }
350  }
351  }
352  else
353  {
354  // This is our first attempt to transmit data to this destination.
355  NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
356  ", no entry for " << destination << " -- send arp request");
357  entry = cache->Add (destination);
358  entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (packet, ipHeader));
360  }
361  return false;
362 }
363 
364 void
366 {
367  NS_LOG_FUNCTION (this << cache << to);
368  ArpHeader arp;
369  // need to pick a source address; use routing implementation to select
371  Ptr<NetDevice> device = cache->GetDevice ();
372  NS_ASSERT (device != 0);
373  Ipv4Header header;
374  header.SetDestination (to);
375  Ptr<Packet> packet = Create<Packet> ();
376  Ipv4Address source = ipv4->SelectSourceAddress (device, to, Ipv4InterfaceAddress::GLOBAL);
377  NS_LOG_LOGIC ("ARP: sending request from node "<<m_node->GetId ()<<
378  " || src: " << device->GetAddress () << " / " << source <<
379  " || dst: " << device->GetBroadcast () << " / " << to);
380  arp.SetRequest (device->GetAddress (), source, device->GetBroadcast (), to);
381  NS_ASSERT (m_tc != 0);
382  m_tc->Send (device, Create<ArpQueueDiscItem> (packet, device->GetBroadcast (), PROT_NUMBER, arp));
383 }
384 
385 void
387 {
388  NS_LOG_FUNCTION (this << cache << myIp << toIp << toMac);
389  ArpHeader arp;
390  NS_LOG_LOGIC ("ARP: sending reply from node "<<m_node->GetId ()<<
391  "|| src: " << cache->GetDevice ()->GetAddress () <<
392  " / " << myIp <<
393  " || dst: " << toMac << " / " << toIp);
394  arp.SetReply (cache->GetDevice ()->GetAddress (), myIp, toMac, toIp);
395  Ptr<Packet> packet = Create<Packet> ();
396  NS_ASSERT (m_tc != 0);
397  m_tc->Send (cache->GetDevice (), Create<ArpQueueDiscItem> (packet, toMac, PROT_NUMBER, arp));
398 }
399 
400 } // namespace ns3
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Set the NetDevice and Ipv4Interface associated with the ArpCache.
Definition: arp-cache.cc:114
void SetReply(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP reply parameters.
Definition: arp-header.cc:46
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Receive a packet.
void SetDestination(Ipv4Address destination)
Definition: ipv4-header.cc:298
void SendArpReply(Ptr< const ArpCache > cache, Ipv4Address myIp, Ipv4Address toIp, Address toMac)
Send an ARP reply to an host.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
ArpCache::Entry * Add(Ipv4Address to)
Add an Ipv4Address to this ARP cache.
Definition: arp-cache.cc:330
uint32_t GetId(void) const
Definition: node.cc:107
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
Hold variables of type string.
Definition: string.h:41
Ipv4Address GetDestinationIpv4Address(void) const
Returns the destination IP address.
Definition: arp-header.cc:89
ArpCache::Entry * Lookup(Ipv4Address destination)
Do lookup in the ARP cache against an IP address.
Definition: arp-cache.cc:318
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:81
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
Ptr< TrafficControlLayer > m_tc
The associated TrafficControlLayer.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
void SetRequest(Address sourceHardwareAddress, Ipv4Address sourceProtocolAddress, Address destinationHardwareAddress, Ipv4Address destinationProtocolAddress)
Set the ARP request parameters.
Definition: arp-header.cc:33
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
The packet header for an ARP packet.
Definition: arp-header.h:34
Ptr< ArpCache > CreateCache(Ptr< NetDevice > device, Ptr< Ipv4Interface > interface)
Create an ARP cache for the device/interface.
TracedCallback< Ptr< const Packet > > m_dropTrace
trace for packets dropped by ARP
bool IsAlive(void)
Definition: arp-cache.cc:375
Address GetDestinationHardwareAddress(void) const
Returns the destination hardware address.
Definition: arp-header.cc:77
void SendArpRequest(Ptr< const ArpCache >cache, Ipv4Address to)
Send an ARP request to an host.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Address GetSourceHardwareAddress(void) const
Returns the source hardware address.
Definition: arp-header.cc:71
Packet header for IPv4.
Definition: ipv4-header.h:33
virtual void NotifyNewAggregate()
Notify all Objects aggregated to this one of a new Object being aggregated.
Ipv4Address GetSourceIpv4Address(void) const
Returns the source IP address.
Definition: arp-header.cc:83
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
Address GetMacAddress(void) const
Definition: arp-cache.cc:454
virtual void DoDispose(void)
Destructor implementation.
bool IsDead(void)
Definition: arp-cache.cc:369
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void MarkWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:440
bool IsRequest(void) const
Check if the ARP is a request.
Definition: arp-header.cc:59
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Implement the IPv4 layer.
bool IsReply(void) const
Check if the ARP is a reply.
Definition: arp-header.cc:65
static const uint16_t PROT_NUMBER
ARP protocol number (0x0806)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< Node > m_node
node the ARP L3 protocol is associated with
void MarkAlive(Address macAddress)
Definition: arp-cache.cc:404
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
bool IsExpired(void) const
Definition: arp-cache.cc:503
A record that that holds information about an ArpCache entry.
Definition: arp-cache.h:188
void Flush(void)
Clear the ArpCache of all entries.
Definition: arp-cache.cc:244
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
bool UpdateWaitReply(Ipv4PayloadHeaderPair waiting)
Definition: arp-cache.cc:424
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4PayloadHeaderPair DequeuePending(void)
Definition: arp-cache.cc:516
void SetNode(Ptr< Node > node)
Set the node the ARP L3 protocol is associated with.
std::pair< Ptr< Packet >, Ipv4Header > Ipv4PayloadHeaderPair
Pair of a packet and an Ipv4 header.
Definition: arp-cache.h:183
Ptr< RandomVariableStream > m_requestJitter
jitter to de-sync ARP requests
bool IsWaitReply(void)
Definition: arp-cache.cc:381
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:325
bool IsEqual(const Ipv4Address &other) const
Comparison operation between two Ipv4Addresses.
Definition: ipv4-address.h:83
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:296
Container for a set of ns3::Object pointers.
void SetTrafficControl(Ptr< TrafficControlLayer > tc)
Set the TrafficControlLayer.
CacheList m_cacheList
ARP cache container.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
bool Lookup(Ptr< Packet > p, const Ipv4Header &ipHeader, Ipv4Address destination, Ptr< NetDevice > device, Ptr< ArpCache > cache, Address *hardwareDestination)
Perform an ARP lookup.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
Ptr< ArpCache > FindCache(Ptr< NetDevice > device)
Finds the cache associated with a NetDevice.
static TypeId GetTypeId(void)
Get the type ID.