A Discrete-Event Network Simulator
API
ipv6-interface.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
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: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/node.h"
23 #include "ns3/packet.h"
24 #include "ns3/net-device.h"
25 #include "ns3/mac16-address.h"
26 #include "ns3/mac64-address.h"
27 
28 #include "ipv6-interface.h"
29 #include "ipv6-queue-disc-item.h"
30 #include "loopback-net-device.h"
31 #include "ipv6-l3-protocol.h"
32 #include "icmpv6-l4-protocol.h"
33 #include "ipv6-header.h"
34 #include "ndisc-cache.h"
35 
36 namespace ns3
37 {
38 
39 NS_LOG_COMPONENT_DEFINE ("Ipv6Interface");
40 
41 NS_OBJECT_ENSURE_REGISTERED (Ipv6Interface);
42 
44 {
45  static TypeId tid = TypeId ("ns3::Ipv6Interface")
46  .SetParent<Object> ()
47  .SetGroupName ("Internet")
48  ;
49  return tid;
50 }
51 
53  : m_ifup (false),
54  m_forwarding (true),
55  m_metric (1),
56  m_node (0),
57  m_device (0),
58  m_tc (0),
59  m_ndCache (0),
60  m_curHopLimit (0),
61  m_baseReachableTime (0),
62  m_reachableTime (0),
63  m_retransTimer (0)
64 {
65  NS_LOG_FUNCTION (this);
66 }
67 
69 {
71 }
72 
74 {
76  m_node = 0;
77  m_device = 0;
78  m_tc = 0;
79  m_ndCache = 0;
81 }
82 
84 {
86 
87  if (m_node == 0 || m_device == 0)
88  {
89  return;
90  }
91 
92  /* set up link-local address */
93  if (!DynamicCast<LoopbackNetDevice> (m_device)) /* no autoconf for ip6-localhost */
94  {
95  Address addr = GetDevice ()->GetAddress ();
96 
98  {
100  AddAddress (ifaddr);
101  m_linkLocalAddress = ifaddr;
102  }
103  else if (Mac48Address::IsMatchingType (addr))
104  {
106  AddAddress (ifaddr);
107  m_linkLocalAddress = ifaddr;
108  }
109  else if (Mac16Address::IsMatchingType (addr))
110  {
112  AddAddress (ifaddr);
113  m_linkLocalAddress = ifaddr;
114  }
115  else
116  {
117  NS_FATAL_ERROR ("IPv6 autoconf for this kind of address not implemented.");
118  }
119  }
120  else
121  {
122  return; /* no NDISC cache for ip6-localhost */
123  }
124 
126  Ptr<Icmpv6L4Protocol> icmpv6;
127  if (proto)
128  {
129  icmpv6 = proto->GetObject <Icmpv6L4Protocol> ();
130  }
131  if (icmpv6 && !m_ndCache)
132  {
133  m_ndCache = icmpv6->CreateCache (m_device, this);
134  }
135 }
136 
138 {
139  NS_LOG_FUNCTION (this << node);
140  m_node = node;
141  DoSetup ();
142 }
143 
145 {
146  NS_LOG_FUNCTION (this << device);
147  m_device = device;
148  DoSetup ();
149 }
150 
151 void
153 {
154  NS_LOG_FUNCTION (this << tc);
155  m_tc = tc;
156 }
157 
159 {
161  return m_device;
162 }
163 
164 void Ipv6Interface::SetMetric (uint16_t metric)
165 {
166  NS_LOG_FUNCTION (this << metric);
167  m_metric = metric;
168 }
169 
170 uint16_t Ipv6Interface::GetMetric () const
171 {
173  return m_metric;
174 }
175 
176 bool Ipv6Interface::IsUp () const
177 {
179  return m_ifup;
180 }
181 
183 {
185  return !m_ifup;
186 }
187 
189 {
191 
192  if (m_ifup)
193  {
194  return;
195  }
196  DoSetup ();
197  m_ifup = true;
198 }
199 
201 {
203  m_ifup = false;
204  m_addresses.clear ();
205  m_ndCache->Flush ();
206 }
207 
209 {
211  return m_forwarding;
212 }
213 
214 void Ipv6Interface::SetForwarding (bool forwarding)
215 {
216  NS_LOG_FUNCTION (this << forwarding);
217  m_forwarding = forwarding;
218 }
219 
221 {
223  Ipv6Address addr = iface.GetAddress ();
224 
225  /* DAD handling */
226  if (!addr.IsAny ())
227  {
228  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
229  {
230  if (it->first.GetAddress () == addr)
231  {
232  return false;
233  }
234  }
235 
237  m_addresses.push_back (std::make_pair (iface, solicited));
238 
239  if (!addr.IsAny () || !addr.IsLocalhost ())
240  {
241  /* DAD handling */
243  Ptr<Icmpv6L4Protocol> icmpv6;
244  if (proto)
245  {
246  icmpv6 = proto->GetObject <Icmpv6L4Protocol> ();
247  }
248 
249  if (icmpv6 && icmpv6->IsAlwaysDad ())
250  {
251  Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
253  }
254  }
255  return true;
256  }
257 
258  /* bad address */
259  return false;
260 }
261 
263 {
264  /* IPv6 interface has always at least one IPv6 link-local address */
266 
267  return m_linkLocalAddress;
268 }
269 
271 {
272  /* IPv6 interface has always at least one IPv6 Solicited Multicast address */
273  NS_LOG_FUNCTION (this << address);
274 
275  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
276  {
277  if (it->second == address)
278  {
279  return true;
280  }
281  }
282 
283  return false;
284 }
285 
287 {
288  NS_LOG_FUNCTION (this << index);
289  uint32_t i = 0;
290 
291  if (m_addresses.size () > index)
292  {
293  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
294  {
295  if (i == index)
296  {
297  return it->first;
298  }
299  i++;
300  }
301  }
302  else
303  {
304  NS_FATAL_ERROR ("index " << index << " out of bounds");
305  }
307  return addr; /* quiet compiler */
308 }
309 
311 {
313  return m_addresses.size ();
314 }
315 
317 {
318  NS_LOG_FUNCTION (this << index);
319  uint32_t i = 0;
320 
321  if (m_addresses.size () < index)
322  {
323  NS_FATAL_ERROR ("Removing index that does not exist in Ipv6Interface::RemoveAddress");
324  }
325 
326  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
327  {
328  if (i == index)
329  {
330  Ipv6InterfaceAddress iface = it->first;
331  m_addresses.erase (it);
332  return iface;
333  }
334 
335  i++;
336  }
337  NS_FATAL_ERROR ("Address " << index << " not found");
339  return addr; /* quiet compiler */
340 }
341 
344 {
345  NS_LOG_FUNCTION(this << address);
346 
347  if (address == address.GetLoopback())
348  {
349  NS_LOG_WARN ("Cannot remove loopback address.");
350  return Ipv6InterfaceAddress();
351  }
352 
353  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
354  {
355  if(it->first.GetAddress () == address)
356  {
357  Ipv6InterfaceAddress iface = it->first;
358  m_addresses.erase(it);
359  return iface;
360  }
361  }
362  return Ipv6InterfaceAddress();
363 }
364 
366 {
367  NS_LOG_FUNCTION (this << dst);
368 
369  for (Ipv6InterfaceAddressList::const_iterator it = m_addresses.begin (); it != m_addresses.end (); ++it)
370  {
371  Ipv6InterfaceAddress ifaddr = it->first;
372 
373  if (ifaddr.GetPrefix ().IsMatch (ifaddr.GetAddress (), dst))
374  {
375  return ifaddr;
376  }
377  }
378 
379  /* NS_ASSERT_MSG (false, "Not matching address."); */
381  return ret; /* quiet compiler */
382 }
383 
385 {
386  NS_LOG_FUNCTION (this << p << dest);
387 
388  if (!IsUp ())
389  {
390  return;
391  }
392 
394 
395  /* check if destination is localhost (::1), if yes we don't pass through
396  * traffic control layer */
397  if (DynamicCast<LoopbackNetDevice> (m_device))
398  {
402  p->AddHeader (hdr);
403  m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
404  return;
405  }
406 
407  NS_ASSERT (m_tc != 0);
408 
409  /* check if destination is for one of our interface */
410  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
411  {
412  if (dest == it->first.GetAddress ())
413  {
414  p->AddHeader (hdr);
416  m_device->GetBroadcast (),
417  m_device->GetBroadcast (),
419  return;
420  }
421  }
422 
423  /* other address */
424  if (m_device->NeedsArp ())
425  {
426  NS_LOG_LOGIC ("Needs ARP" << " " << dest);
427  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
428  Address hardwareDestination;
429  bool found = false;
430 
431  NS_ASSERT (icmpv6);
432 
433  if (dest.IsMulticast ())
434  {
435  NS_LOG_LOGIC ("IsMulticast");
436  NS_ASSERT_MSG (m_device->IsMulticast (), "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
437 
438  hardwareDestination = m_device->GetMulticast (dest);
439  found = true;
440  }
441  else
442  {
443  NS_LOG_LOGIC ("NDISC Lookup");
444  found = icmpv6->Lookup (p, hdr, dest, GetDevice (), m_ndCache, &hardwareDestination);
445  }
446 
447  if (found)
448  {
449  NS_LOG_LOGIC ("Address Resolved. Send.");
450  m_tc->Send (m_device, Create<Ipv6QueueDiscItem> (p, hardwareDestination, Ipv6L3Protocol::PROT_NUMBER, hdr));
451  }
452  }
453  else
454  {
455  NS_LOG_LOGIC ("Doesn't need ARP");
456  m_tc->Send (m_device, Create<Ipv6QueueDiscItem> (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER, hdr));
457  }
458 }
459 
460 void Ipv6Interface::SetCurHopLimit (uint8_t curHopLimit)
461 {
462  NS_LOG_FUNCTION (this << curHopLimit);
463  m_curHopLimit = curHopLimit;
464 }
465 
467 {
469  return m_curHopLimit;
470 }
471 
472 void Ipv6Interface::SetBaseReachableTime (uint16_t baseReachableTime)
473 {
474  NS_LOG_FUNCTION (this << baseReachableTime);
475  m_baseReachableTime = baseReachableTime;
476 }
477 
479 {
481  return m_baseReachableTime;
482 }
483 
484 void Ipv6Interface::SetReachableTime (uint16_t reachableTime)
485 {
486  NS_LOG_FUNCTION (this << reachableTime);
487  m_reachableTime = reachableTime;
488 }
489 
491 {
493  return m_reachableTime;
494 }
495 
496 void Ipv6Interface::SetRetransTimer (uint16_t retransTimer)
497 {
498  NS_LOG_FUNCTION (this << retransTimer);
499  m_retransTimer = retransTimer;
500 }
501 
503 {
505  return m_retransTimer;
506 }
507 
509 {
510  NS_LOG_FUNCTION (this << address << state);
511 
512  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
513  {
514  if (it->first.GetAddress () == address)
515  {
516  it->first.SetState (state);
517  return;
518  }
519  }
520  /* not found, maybe address has expired */
521 }
522 
524 {
525  NS_LOG_FUNCTION (this << address << uid);
526 
527  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
528  {
529  if (it->first.GetAddress () == address)
530  {
531  it->first.SetNsDadUid (uid);
532  return;
533  }
534  }
535  /* not found, maybe address has expired */
536 }
537 
539 {
540  NS_LOG_FUNCTION (this);
541  return m_ndCache;
542 }
543 
544 } /* namespace ns3 */
545 
bool IsMatch(Ipv6Address a, Ipv6Address b) const
If the Address match the type.
Ipv6InterfaceAddressList m_addresses
The addresses assigned to this interface.
bool IsAny() const
If the IPv6 address is the "Any" address.
static bool IsMatchingType(const Address &address)
static Ipv6Address GetLoopback()
Get the loopback address.
bool m_ifup
The state of this interface.
Packet header for IPv6.
Definition: ipv6-header.h:34
bool AddAddress(Ipv6InterfaceAddress iface)
Add an IPv6 address.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetState(Ipv6Address address, Ipv6InterfaceAddress::State_e state)
Update state of an interface address.
void SetUp()
Enable this interface.
uint16_t GetRetransTimer() const
Get the retransmission timer.
bool m_forwarding
Forwarding state.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
static Mac16Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
IPv6 layer implementation.
#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
Ipv6InterfaceAddress GetAddress(uint32_t index) const
Get an address from IPv6 interface.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
IPv6 address associated with an interface.
Ptr< NetDevice > m_device
NetDevice associated with this interface.
void SetDevice(Ptr< NetDevice > device)
Set the NetDevice.
void SetReachableTime(uint16_t reachableTime)
Set the reachable time.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Packet addressed oo us.
Definition: net-device.h:608
bool IsUp() const
Is the interface UP ?
bool IsDown() const
Is the interface DOWN ?
a polymophic address class
Definition: address.h:90
void SetBaseReachableTime(uint16_t baseReachableTime)
Set the base reachable time.
bool IsSolicitedMulticastAddress(Ipv6Address address) const
Checks if the address is a Solicited Multicast address for this interface.
static void FunctionDadTimeout(Ptr< Icmpv6L4Protocol > icmpv6, Ipv6Interface *interface, Ipv6Address addr)
Function called when DAD timeout.
Ptr< NdiscCache > GetNdiscCache() const
uint16_t m_baseReachableTime
Base value used for computing the random reachable time value (in millisecond).
uint16_t GetBaseReachableTime() const
Get the base reachable time.
Ipv6Address GetAddress() const
Get the IPv6 address.
void Send(Ptr< Packet > p, const Ipv6Header &hdr, Ipv6Address dest)
Send a packet through this interface.
uint32_t GetNAddresses(void) const
Get number of addresses on this IPv6 interface.
void SetMetric(uint16_t metric)
Set the metric.
Ptr< Node > m_node
Node associated with this interface.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
virtual ~Ipv6Interface()
Destructor.
void SetRetransTimer(uint16_t retransTimer)
Set the retransmission timer.
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:147
uint16_t m_retransTimer
Retransmission timer (in millisecond).
An implementation of the ICMPv6 protocol.
static TypeId GetTypeId()
Get the type ID.
virtual Ptr< NetDevice > GetDevice() const
Get the NetDevice.
Ipv6Prefix GetPrefix() const
Get the IPv6 prefix.
Ipv6InterfaceAddress m_linkLocalAddress
The link-local addresses assigned to this interface.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
void SetTrafficControl(Ptr< TrafficControlLayer > tc)
Set the TrafficControlLayer.
void SetNsDadUid(Ipv6Address address, uint32_t uid)
Update NS DAD packet UID of an interface address.
static Mac48Address ConvertFrom(const Address &address)
void SetNode(Ptr< Node > node)
Set node associated with interface.
void SetForwarding(bool forward)
Set forwarding enabled or not.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t m_curHopLimit
Current hop limit.
static uint16_t GetStaticProtocolNumber()
Get ICMPv6 protocol number.
void SetCurHopLimit(uint8_t curHopLimit)
Set the current hop limit.
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
State_e
State of an address associated with an interface.
std::list< std::pair< Ipv6InterfaceAddress, Ipv6Address > >::const_iterator Ipv6InterfaceAddressListCI
Const Container Iterator for the Ipv6InterfaceAddresses.
std::list< std::pair< Ipv6InterfaceAddress, Ipv6Address > >::iterator Ipv6InterfaceAddressListI
Container Iterator for the Ipv6InterfaceAddresses.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
void DoDAD(Ipv6Address target, Ptr< Ipv6Interface > interface)
Do the Duplication Address Detection (DAD).
static Ipv6Address MakeAutoconfiguredLinkLocalAddress(Mac16Address mac)
Make the autoconfigured link-local IPv6 address with Mac16Address.
Ipv6InterfaceAddress GetLinkLocalAddress() const
Get link-local address from IPv6 interface.
uint8_t GetCurHopLimit() const
Get the current hop limit value.
Describes an IPv6 address.
Definition: ipv6-address.h:48
uint16_t GetMetric() const
Get the metric.
static Mac64Address ConvertFrom(const Address &address)
void DoSetup()
Initialize interface.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
uint16_t m_reachableTime
Reachable time (in millisecond).
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
bool IsForwarding() const
If the interface allows forwarding packets.
Describes an IPv6 prefix.
Definition: ipv6-address.h:394
uint16_t GetReachableTime() const
Get the reachable time.
Ipv6InterfaceAddress RemoveAddress(uint32_t index)
Remove an address from interface.
A base class which provides memory management and object aggregation.
Definition: object.h:87
tuple address
Definition: first.py:37
virtual void DoDispose()
Dispose this object.
static bool IsMatchingType(const Address &address)
uint16_t m_metric
The metric.
Ptr< TrafficControlLayer > m_tc
TrafficControlLayer associated with this interface.
bool IsLocalhost() const
If the IPv6 address is localhost (::1).
void SetDown()
Disable this interface.
a unique identifier for an interface.
Definition: type-id.h:58
Ipv6InterfaceAddress GetAddressMatchingDestination(Ipv6Address dst)
Get an address which is in the same network prefix as destination.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
Ptr< NdiscCache > m_ndCache
Neighbor cache.
static const uint16_t PROT_NUMBER
The protocol number for IPv6 (0x86DD).
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
Ipv6Interface()
Constructs an Ipv6Interface.