src/internet-stack/arp-l3-protocol.cc
author Pavel Boyko <boyko@iitp.ru>
Thu Aug 06 10:23:12 2009 +0400 (2009-08-06)
changeset 4696 5ef92ccda11a
parent 4669 8aaa5e83939e
child 4764 e90e1ef585b0
child 5671 b1fce73037f8
permissions -rw-r--r--
Route lookup removed in ARP request (see bug 606)
mathieu@242
     1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
mathieu@242
     2
/*
mathieu@242
     3
 * Copyright (c) 2006 INRIA
mathieu@242
     4
 *
mathieu@242
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@242
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@242
     7
 * published by the Free Software Foundation;
mathieu@242
     8
 *
mathieu@242
     9
 * This program is distributed in the hope that it will be useful,
mathieu@242
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@242
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@242
    12
 * GNU General Public License for more details.
mathieu@242
    13
 *
mathieu@242
    14
 * You should have received a copy of the GNU General Public License
mathieu@242
    15
 * along with this program; if not, write to the Free Software
mathieu@242
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@242
    17
 *
mathieu@242
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@242
    19
 */
mathieu@242
    20
#include "ns3/packet.h"
craigdo@1505
    21
#include "ns3/log.h"
mathieu@729
    22
#include "ns3/node.h"
mathieu@524
    23
#include "ns3/net-device.h"
mathieu@3146
    24
#include "ns3/object-vector.h"
mathieu@3152
    25
#include "ns3/trace-source-accessor.h"
tomh@4571
    26
#include "ns3/ipv4-route.h"
mathieu@524
    27
mathieu@1176
    28
#include "ipv4-l3-protocol.h"
mathieu@735
    29
#include "arp-l3-protocol.h"
mathieu@242
    30
#include "arp-header.h"
mathieu@242
    31
#include "arp-cache.h"
mathieu@242
    32
#include "ipv4-interface.h"
mathieu@242
    33
craigdo@1505
    34
NS_LOG_COMPONENT_DEFINE ("ArpL3Protocol");
mathieu@242
    35
mathieu@242
    36
namespace ns3 {
mathieu@242
    37
mathieu@736
    38
const uint16_t ArpL3Protocol::PROT_NUMBER = 0x0806;
mathieu@294
    39
mathieu@2249
    40
NS_OBJECT_ENSURE_REGISTERED (ArpL3Protocol);
mathieu@2249
    41
mathieu@2250
    42
TypeId 
mathieu@2251
    43
ArpL3Protocol::GetTypeId (void)
mathieu@2232
    44
{
mathieu@2602
    45
  static TypeId tid = TypeId ("ns3::ArpL3Protocol")
mathieu@2498
    46
    .SetParent<Object> ()
tomh@4472
    47
    .AddConstructor<ArpL3Protocol> ()
mathieu@3146
    48
    .AddAttribute ("CacheList",
mathieu@3146
    49
                   "The list of ARP caches",
mathieu@3146
    50
                   ObjectVectorValue (),
mathieu@3146
    51
                   MakeObjectVectorAccessor (&ArpL3Protocol::m_cacheList),
mathieu@3146
    52
                   MakeObjectVectorChecker<ArpCache> ())
mathieu@3152
    53
    .AddTraceSource ("Drop",
mathieu@3152
    54
                     "Packet dropped because not enough room in pending queue for a specific cache entry.",
mathieu@3152
    55
                     MakeTraceSourceAccessor (&ArpL3Protocol::m_dropTrace))
mathieu@2498
    56
    ;
mathieu@2252
    57
  return tid;
mathieu@2232
    58
}
mathieu@2232
    59
mathieu@2498
    60
ArpL3Protocol::ArpL3Protocol ()
mathieu@1178
    61
{
mathieu@3151
    62
  NS_LOG_FUNCTION (this);
mathieu@1178
    63
}
mathieu@242
    64
mathieu@736
    65
ArpL3Protocol::~ArpL3Protocol ()
craigdo@1505
    66
{
mathieu@3151
    67
  NS_LOG_FUNCTION (this);
craigdo@1505
    68
}
mathieu@496
    69
mathieu@496
    70
void 
mathieu@2592
    71
ArpL3Protocol::SetNode (Ptr<Node> node)
mathieu@2592
    72
{
tomh@3430
    73
  NS_LOG_FUNCTION (this);
mathieu@2592
    74
  m_node = node;
mathieu@2592
    75
}
mathieu@2592
    76
tomh@4472
    77
/*
tomh@4472
    78
 * This method is called by AddAgregate and completes the aggregation
tomh@4472
    79
 * by setting the node in the ipv4 stack
tomh@4472
    80
 */
tomh@4472
    81
void
tomh@4472
    82
ArpL3Protocol::NotifyNewAggregate ()
tomh@4472
    83
{
mathieu@4669
    84
  if (m_node == 0)
tomh@4472
    85
    {
mathieu@4669
    86
      Ptr<Node>node = this->GetObject<Node> ();
mathieu@4669
    87
      //verify that it's a valid node and that
mathieu@4669
    88
      //the node was not set before
mathieu@4669
    89
      if (node != 0)
mathieu@4669
    90
        {
mathieu@4669
    91
          this->SetNode (node);
mathieu@4669
    92
        }
tomh@4472
    93
    }
tomh@4472
    94
  Object::NotifyNewAggregate ();
tomh@4472
    95
}
tomh@4472
    96
mathieu@2592
    97
void 
mathieu@736
    98
ArpL3Protocol::DoDispose (void)
mathieu@496
    99
{
mathieu@3151
   100
  NS_LOG_FUNCTION (this);
mathieu@3151
   101
  for (CacheList::iterator i = m_cacheList.begin (); i != m_cacheList.end (); ++i)
mathieu@3151
   102
    {
mathieu@3151
   103
      Ptr<ArpCache> cache = *i;
mathieu@3151
   104
      cache->Dispose ();
mathieu@3151
   105
    }
mathieu@496
   106
  m_cacheList.clear ();
mathieu@552
   107
  m_node = 0;
mathieu@1176
   108
  Object::DoDispose ();
mathieu@248
   109
}
mathieu@248
   110
mathieu@3147
   111
Ptr<ArpCache> 
mathieu@3147
   112
ArpL3Protocol::CreateCache (Ptr<NetDevice> device, Ptr<Ipv4Interface> interface)
mathieu@3147
   113
{
mathieu@3765
   114
  NS_LOG_FUNCTION (this << device << interface);
mathieu@3147
   115
  Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
mathieu@3147
   116
  Ptr<ArpCache> cache = CreateObject<ArpCache> ();
mathieu@3147
   117
  cache->SetDevice (device, interface);
mathieu@3147
   118
  NS_ASSERT (device->IsBroadcast ());
mathieu@3147
   119
  device->SetLinkChangeCallback (MakeCallback (&ArpCache::Flush, cache));
tomh@3499
   120
  cache->SetArpRequestCallback (MakeCallback (&ArpL3Protocol::SendArpRequest, this));
mathieu@3147
   121
  m_cacheList.push_back (cache);
mathieu@3147
   122
  return cache;
mathieu@3147
   123
}
mathieu@3147
   124
mathieu@3146
   125
Ptr<ArpCache>
mathieu@736
   126
ArpL3Protocol::FindCache (Ptr<NetDevice> device)
mathieu@242
   127
{
mathieu@3765
   128
  NS_LOG_FUNCTION (this << device);
mathieu@242
   129
  for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++)
mathieu@242
   130
    {
mathieu@568
   131
      if ((*i)->GetDevice () == device)
mathieu@242
   132
	{
mathieu@242
   133
	  return *i;
mathieu@242
   134
	}
mathieu@242
   135
    }
mathieu@3147
   136
  NS_ASSERT (false);
mathieu@3147
   137
  // quiet compiler
mathieu@3147
   138
  return 0;
mathieu@242
   139
}
mathieu@242
   140
mathieu@242
   141
void 
mathieu@3548
   142
ArpL3Protocol::Receive(Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol, const Address &from,
gjc@3448
   143
                       const Address &to, NetDevice::PacketType packetType)
mathieu@242
   144
{
mathieu@3765
   145
  NS_LOG_FUNCTION (this << device << p->GetSize () << protocol << from << to << packetType);
gjc@3448
   146
mathieu@3548
   147
  Ptr<Packet> packet = p->Copy ();
mathieu@3548
   148
craigdo@4170
   149
  NS_LOG_LOGIC ("ARP: received packet of size "<< packet->GetSize ());
craigdo@4170
   150
mathieu@3146
   151
  Ptr<ArpCache> cache = FindCache (device);
craigdo@4170
   152
craigdo@4170
   153
  // 
craigdo@4170
   154
  // If we're connected to a real world network, then some of the fields sizes 
craigdo@4170
   155
  // in an ARP packet can vary in ways not seen in simulations.  We need to be
craigdo@4170
   156
  // able to detect ARP packets with headers we don't recongnize and not process
craigdo@4170
   157
  // them instead of crashing.  The ArpHeader will return 0 if it can't deal
craigdo@4170
   158
  // with the received header.
craigdo@4170
   159
  //
mathieu@242
   160
  ArpHeader arp;
craigdo@4170
   161
  uint32_t size = packet->RemoveHeader (arp);
craigdo@4170
   162
  if (size == 0)
craigdo@4170
   163
    {
craigdo@4170
   164
      NS_LOG_LOGIC ("ARP: Cannot remove ARP header");
craigdo@4170
   165
      return;
craigdo@4170
   166
    }
craigdo@1505
   167
  NS_LOG_LOGIC ("ARP: received "<< (arp.IsRequest ()? "request" : "reply") <<
gjc@933
   168
            " node="<<m_node->GetId ()<<", got request from " <<
gjc@933
   169
            arp.GetSourceIpv4Address () << " for address " <<
tomh@4571
   170
            arp.GetDestinationIpv4Address () << "; we have addresses: ");
tomh@4571
   171
  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
tomh@4571
   172
    {
tomh@4571
   173
      NS_LOG_LOGIC (cache->GetInterface ()->GetAddress (i).GetLocal () << ", ");
tomh@4571
   174
    }
gjc@933
   175
mathieu@2919
   176
  /**
mathieu@2919
   177
   * Note: we do not update the ARP cache when we receive an ARP request
mathieu@2919
   178
   * from an unknown node. See bug #107
mathieu@2919
   179
   */
tomh@4571
   180
  bool found = false;
tomh@4571
   181
  for (uint32_t i = 0; i < cache->GetInterface ()->GetNAddresses (); i++)
mathieu@242
   182
    {
tomh@4571
   183
      if (arp.IsRequest () && arp.GetDestinationIpv4Address () == 
tomh@4571
   184
        cache->GetInterface ()->GetAddress (i).GetLocal ()) 
tomh@4571
   185
        {
tomh@4571
   186
          found = true;
tomh@4571
   187
          NS_LOG_LOGIC ("node="<<m_node->GetId () <<", got request from " << 
mathieu@321
   188
                arp.GetSourceIpv4Address () << " -- send reply");
boyko@4696
   189
          SendArpReply (cache, arp.GetDestinationIpv4Address (), arp.GetSourceIpv4Address (),
mathieu@242
   190
                    arp.GetSourceHardwareAddress ());
tomh@4571
   191
          break;
tomh@4571
   192
        } 
tomh@4571
   193
      else if (arp.IsReply () && 
tomh@4571
   194
        arp.GetDestinationIpv4Address ().IsEqual (cache->GetInterface ()->GetAddress (i).GetLocal ()) &&
tomh@4571
   195
        arp.GetDestinationHardwareAddress () == device->GetAddress ()) 
mathieu@242
   196
        {
tomh@4571
   197
          found = true;
tomh@4571
   198
          Ipv4Address from = arp.GetSourceIpv4Address ();
tomh@4571
   199
          ArpCache::Entry *entry = cache->Lookup (from);
tomh@4571
   200
          if (entry != 0)
mathieu@242
   201
            {
tomh@4571
   202
              if (entry->IsWaitReply ()) 
tomh@4571
   203
                {
tomh@4571
   204
                  NS_LOG_LOGIC ("node="<< m_node->GetId () << 
tomh@4571
   205
                    ", got reply from " << arp.GetSourceIpv4Address ()
mathieu@242
   206
                     << " for waiting entry -- flush");
tomh@4571
   207
                  Address from_mac = arp.GetSourceHardwareAddress ();
tomh@4571
   208
                  entry->MarkAlive (from_mac);
tomh@4571
   209
                  Ptr<Packet> pending = entry->DequeuePending();
tomh@4571
   210
                  while (pending != 0)
tomh@4571
   211
                    {
tomh@4571
   212
                      cache->GetInterface ()->Send (pending,
tomh@4571
   213
                                                arp.GetSourceIpv4Address ());
tomh@4571
   214
                      pending = entry->DequeuePending();
tomh@4571
   215
                    }
tomh@4571
   216
                } 
tomh@4571
   217
              else 
mathieu@3151
   218
                {
tomh@4571
   219
                  // ignore this reply which might well be an attempt 
tomh@4571
   220
                  // at poisening my arp cache.
tomh@4571
   221
                  NS_LOG_LOGIC("node="<<m_node->GetId ()<<", got reply from " << 
tomh@4571
   222
                        arp.GetSourceIpv4Address () << 
tomh@4571
   223
                        " for non-waiting entry -- drop");
tomh@4571
   224
                  m_dropTrace (packet);
mathieu@3151
   225
                }
mathieu@242
   226
            } 
mathieu@242
   227
          else 
mathieu@242
   228
            {
tomh@4571
   229
              NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got reply for unknown entry -- drop");
mathieu@3152
   230
              m_dropTrace (packet);
mathieu@242
   231
            }
tomh@4571
   232
          break;
mathieu@242
   233
        }
mathieu@242
   234
    }
tomh@4571
   235
  if (found == false)
gjc@933
   236
    {
craigdo@1505
   237
      NS_LOG_LOGIC ("node="<<m_node->GetId ()<<", got request from " <<
gjc@933
   238
                arp.GetSourceIpv4Address () << " for unknown address " <<
gjc@933
   239
                arp.GetDestinationIpv4Address () << " -- drop");
gjc@933
   240
    }
mathieu@242
   241
}
tomh@4571
   242
mathieu@242
   243
bool 
mathieu@1866
   244
ArpL3Protocol::Lookup (Ptr<Packet> packet, Ipv4Address destination, 
mathieu@1161
   245
                       Ptr<NetDevice> device,
mathieu@3147
   246
                       Ptr<ArpCache> cache,
mathieu@1161
   247
                       Address *hardwareDestination)
mathieu@242
   248
{
mathieu@3765
   249
  NS_LOG_FUNCTION (this << packet << destination << device << cache);
mathieu@242
   250
  ArpCache::Entry *entry = cache->Lookup (destination);
mathieu@242
   251
  if (entry != 0)
mathieu@242
   252
    {
mathieu@242
   253
      if (entry->IsExpired ()) 
mathieu@242
   254
        {
mathieu@242
   255
          if (entry->IsDead ()) 
mathieu@242
   256
            {
craigdo@1505
   257
              NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
mathieu@321
   258
                        ", dead entry for " << destination << " expired -- send arp request");
mathieu@242
   259
              entry->MarkWaitReply (packet);
mathieu@242
   260
              SendArpRequest (cache, destination);
mathieu@242
   261
            } 
mathieu@242
   262
          else if (entry->IsAlive ()) 
mathieu@242
   263
            {
craigdo@1505
   264
              NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
mathieu@321
   265
                        ", alive entry for " << destination << " expired -- send arp request");
mathieu@242
   266
              entry->MarkWaitReply (packet);
mathieu@242
   267
              SendArpRequest (cache, destination);
mathieu@242
   268
            } 
mathieu@242
   269
          else if (entry->IsWaitReply ()) 
mathieu@242
   270
            {
tomh@3823
   271
              NS_FATAL_ERROR ("Test for possibly unreachable code-- please file a bug report, with a test case, if this is ever hit");
mathieu@242
   272
            }
mathieu@242
   273
        } 
mathieu@242
   274
      else 
mathieu@242
   275
        {
mathieu@242
   276
          if (entry->IsDead ()) 
mathieu@242
   277
            {
craigdo@1505
   278
              NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
mathieu@3151
   279
                            ", dead entry for " << destination << " valid -- drop");
mathieu@3152
   280
              m_dropTrace (packet);
mathieu@242
   281
            } 
mathieu@242
   282
          else if (entry->IsAlive ()) 
mathieu@242
   283
            {
craigdo@1505
   284
              NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
mathieu@3151
   285
                            ", alive entry for " << destination << " valid -- send");
mathieu@242
   286
	      *hardwareDestination = entry->GetMacAddress ();
mathieu@242
   287
              return true;
mathieu@242
   288
            } 
mathieu@242
   289
          else if (entry->IsWaitReply ()) 
mathieu@242
   290
            {
craigdo@1505
   291
              NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
mathieu@3151
   292
                            ", wait reply for " << destination << " valid -- drop previous");
mathieu@3152
   293
              if (!entry->UpdateWaitReply (packet))
mathieu@3152
   294
                {
mathieu@3152
   295
                  m_dropTrace (packet);
mathieu@3152
   296
                }
mathieu@242
   297
            }
mathieu@242
   298
        }
mathieu@242
   299
    }
mathieu@242
   300
  else
mathieu@242
   301
    {
mathieu@242
   302
      // This is our first attempt to transmit data to this destination.
craigdo@1505
   303
      NS_LOG_LOGIC ("node="<<m_node->GetId ()<<
mathieu@321
   304
                ", no entry for " << destination << " -- send arp request");
mathieu@242
   305
      entry = cache->Add (destination);
mathieu@242
   306
      entry->MarkWaitReply (packet);
mathieu@242
   307
      SendArpRequest (cache, destination);
mathieu@242
   308
    }
mathieu@242
   309
  return false;
mathieu@242
   310
}
mathieu@242
   311
mathieu@242
   312
void
mathieu@3146
   313
ArpL3Protocol::SendArpRequest (Ptr<const ArpCache> cache, Ipv4Address to)
mathieu@242
   314
{
mathieu@3765
   315
  NS_LOG_FUNCTION (this << cache << to);
mathieu@242
   316
  ArpHeader arp;
tomh@4571
   317
  // need to pick a source address; use routing implementation to select
tomh@4571
   318
  Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
tomh@4571
   319
  int32_t interface = ipv4->GetInterfaceForDevice (cache->GetDevice ());
tomh@4571
   320
  NS_ASSERT (interface >= 0);
tomh@4571
   321
  Ipv4Header header;
tomh@4571
   322
  header.SetDestination (to);
tomh@4571
   323
  Socket::SocketErrno errno_;
joshpelkey@4603
   324
  Ptr<Packet> packet = Create<Packet> ();
joshpelkey@4603
   325
  Ptr<Ipv4Route> route = ipv4->GetRoutingProtocol ()->RouteOutput (packet, header, interface, errno_);
tomh@4571
   326
  NS_ASSERT (route != 0);
craigdo@1505
   327
  NS_LOG_LOGIC ("ARP: sending request from node "<<m_node->GetId ()<<
gjc@933
   328
            " || src: " << cache->GetDevice ()->GetAddress () <<
tomh@4571
   329
            " / " << route->GetSource () <<
gjc@933
   330
            " || dst: " << cache->GetDevice ()->GetBroadcast () <<
gjc@933
   331
            " / " << to);
mathieu@568
   332
  arp.SetRequest (cache->GetDevice ()->GetAddress (),
tomh@4571
   333
		  route->GetSource (),
mathieu@568
   334
                  cache->GetDevice ()->GetBroadcast (),
mathieu@242
   335
                  to);
mathieu@1866
   336
  packet->AddHeader (arp);
mathieu@568
   337
  cache->GetDevice ()->Send (packet, cache->GetDevice ()->GetBroadcast (), PROT_NUMBER);
mathieu@242
   338
}
mathieu@242
   339
mathieu@242
   340
void
boyko@4696
   341
ArpL3Protocol::SendArpReply (Ptr<const ArpCache> cache, Ipv4Address myIp, Ipv4Address toIp, Address toMac)
mathieu@242
   342
{
mathieu@3765
   343
  NS_LOG_FUNCTION (this << cache << toIp << toMac);
mathieu@242
   344
  ArpHeader arp;
craigdo@1505
   345
  NS_LOG_LOGIC ("ARP: sending reply from node "<<m_node->GetId ()<<
gjc@933
   346
            "|| src: " << cache->GetDevice ()->GetAddress () << 
boyko@4696
   347
            " / " << myIp <<
gjc@933
   348
            " || dst: " << toMac << " / " << toIp);
boyko@4696
   349
  arp.SetReply (cache->GetDevice ()->GetAddress (), myIp, toMac, toIp);
boyko@4696
   350
  Ptr<Packet> packet = Create<Packet> ();
mathieu@1866
   351
  packet->AddHeader (arp);
mathieu@568
   352
  cache->GetDevice ()->Send (packet, toMac, PROT_NUMBER);
mathieu@242
   353
}
mathieu@242
   354
mathieu@242
   355
}//namespace ns3