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 
25 #include "ipv6-interface.h"
26 #include "ns3/net-device.h"
27 #include "loopback-net-device.h"
28 #include "ns3/mac16-address.h"
29 #include "ns3/mac64-address.h"
30 #include "ipv6-l3-protocol.h"
31 #include "icmpv6-l4-protocol.h"
32 #include "ndisc-cache.h"
33 
34 namespace ns3
35 {
36 
37 NS_LOG_COMPONENT_DEFINE ("Ipv6Interface");
38 
39 NS_OBJECT_ENSURE_REGISTERED (Ipv6Interface);
40 
42 {
43  static TypeId tid = TypeId ("ns3::Ipv6Interface")
44  .SetParent<Object> ()
45  .SetGroupName ("Internet")
46  ;
47  return tid;
48 }
49 
51  : m_ifup (false),
52  m_forwarding (true),
53  m_metric (1),
54  m_node (0),
55  m_device (0),
56  m_ndCache (0),
57  m_curHopLimit (0),
58  m_baseReachableTime (0),
59  m_reachableTime (0),
60  m_retransTimer (0)
61 {
62  NS_LOG_FUNCTION (this);
63 }
64 
66 {
68 }
69 
71 {
73  m_node = 0;
74  m_device = 0;
75  m_ndCache = 0;
77 }
78 
80 {
82 
83  if (m_node == 0 || m_device == 0)
84  {
85  return;
86  }
87 
88  /* set up link-local address */
89  if (!DynamicCast<LoopbackNetDevice> (m_device)) /* no autoconf for ip6-localhost */
90  {
91  Address addr = GetDevice ()->GetAddress ();
92 
94  {
96  AddAddress (ifaddr);
97  }
98  else if (Mac48Address::IsMatchingType (addr))
99  {
101  AddAddress (ifaddr);
102  }
103  else if (Mac16Address::IsMatchingType (addr))
104  {
106  AddAddress (ifaddr);
107  }
108  else
109  {
110  NS_ASSERT_MSG (false, "IPv6 autoconf for this kind of address not implemented.");
111  }
112  }
113  else
114  {
115  return; /* no NDISC cache for ip6-localhost */
116  }
117 
119  Ptr<Icmpv6L4Protocol> icmpv6;
120  if (proto)
121  {
122  icmpv6 = proto->GetObject <Icmpv6L4Protocol> ();
123  }
124  if (icmpv6 && !m_ndCache)
125  {
126  m_ndCache = icmpv6->CreateCache (m_device, this);
127  }
128 }
129 
131 {
132  NS_LOG_FUNCTION (this << node);
133  m_node = node;
134  DoSetup ();
135 }
136 
138 {
139  NS_LOG_FUNCTION (this << device);
140  m_device = device;
141  DoSetup ();
142 }
143 
145 {
147  return m_device;
148 }
149 
150 void Ipv6Interface::SetMetric (uint16_t metric)
151 {
152  NS_LOG_FUNCTION (this << metric);
153  m_metric = metric;
154 }
155 
156 uint16_t Ipv6Interface::GetMetric () const
157 {
159  return m_metric;
160 }
161 
162 bool Ipv6Interface::IsUp () const
163 {
165  return m_ifup;
166 }
167 
169 {
171  return !m_ifup;
172 }
173 
175 {
177 
178  if (m_ifup)
179  {
180  return;
181  }
182  DoSetup ();
183  m_ifup = true;
184 }
185 
187 {
189  m_ifup = false;
190  m_addresses.clear ();
191  m_ndCache->Flush ();
192 }
193 
195 {
197  return m_forwarding;
198 }
199 
200 void Ipv6Interface::SetForwarding (bool forwarding)
201 {
202  NS_LOG_FUNCTION (this << forwarding);
203  m_forwarding = forwarding;
204 }
205 
207 {
209  Ipv6Address addr = iface.GetAddress ();
210 
211  /* DAD handling */
212  if (!addr.IsAny ())
213  {
214  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
215  {
216  if ((*it).GetAddress () == addr)
217  {
218  return false;
219  }
220  }
221 
222  m_addresses.push_back (iface);
223 
224  if (!addr.IsAny () || !addr.IsLocalhost ())
225  {
226  /* DAD handling */
228  Ptr<Icmpv6L4Protocol> icmpv6;
229  if (proto)
230  {
231  icmpv6 = proto->GetObject <Icmpv6L4Protocol> ();
232  }
233 
234  if (icmpv6 && icmpv6->IsAlwaysDad ())
235  {
236  Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
238  }
239  }
240  return true;
241  }
242 
243  /* bad address */
244  return false;
245 }
246 
248 {
249  /* IPv6 interface has always at least one IPv6 link-local address */
251 
252  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
253  {
254  if ((*it).GetAddress ().IsLinkLocal ())
255  {
256  return (*it);
257  }
258  }
259  NS_ASSERT_MSG (false, "No link-local address on interface " << this);
261  return addr; /* quiet compiler */
262 }
263 
265 {
266  NS_LOG_FUNCTION (this << index);
267  uint32_t i = 0;
268 
269  if (m_addresses.size () > index)
270  {
271  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
272  {
273  if (i == index)
274  {
275  return (*it);
276  }
277  i++;
278  }
279  }
280 
281  NS_ASSERT_MSG (false, "Address " << index << " not found");
283  return addr; /* quiet compiler */
284 }
285 
287 {
289  return m_addresses.size ();
290 }
291 
293 {
294  NS_LOG_FUNCTION (this << index);
295  uint32_t i = 0;
296 
297  if (m_addresses.size () < index)
298  {
299  NS_ASSERT_MSG (false, "Try to remove index that don't exist in Ipv6Interface::RemoveAddress");
300  }
301 
302  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
303  {
304  if (i == index)
305  {
306  Ipv6InterfaceAddress iface = (*it);
307  m_addresses.erase (it);
308  return iface;
309  }
310 
311  i++;
312  }
313 
314  NS_ASSERT_MSG (false, "Address " << index << " not found");
316  return addr; /* quiet compiler */
317 }
318 
321 {
322  NS_LOG_FUNCTION(this << address);
323 
324  if (address == address.GetLoopback())
325  {
326  NS_LOG_WARN ("Cannot remove loopback address.");
327  return Ipv6InterfaceAddress();
328  }
329 
330  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
331  {
332  if((*it).GetAddress() == address)
333  {
334  Ipv6InterfaceAddress iface = (*it);
335  m_addresses.erase(it);
336  return iface;
337  }
338  }
339  return Ipv6InterfaceAddress();
340 }
341 
343 {
344  NS_LOG_FUNCTION (this << dst);
345 
346  for (Ipv6InterfaceAddressList::const_iterator it = m_addresses.begin (); it != m_addresses.end (); ++it)
347  {
348  Ipv6InterfaceAddress ifaddr = (*it);
349 
350  if (ifaddr.GetPrefix ().IsMatch (ifaddr.GetAddress (), dst))
351  {
352  return ifaddr;
353  }
354  }
355 
356  /* NS_ASSERT_MSG (false, "Not matching address."); */
358  return ret; /* quiet compiler */
359 }
360 
362 {
363  NS_LOG_FUNCTION (this << p << dest);
365 
366  if (!IsUp ())
367  {
368  return;
369  }
370 
371  /* check if destination is localhost (::1) */
372  if (DynamicCast<LoopbackNetDevice> (m_device))
373  {
377  m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
378  return;
379  }
380 
381  /* check if destination is for one of our interface */
382  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
383  {
384  if (dest == (*it).GetAddress ())
385  {
386  ipv6->Receive (m_device, p, Ipv6L3Protocol::PROT_NUMBER,
387  m_device->GetBroadcast (),
388  m_device->GetBroadcast (),
389  NetDevice::PACKET_HOST // note: linux uses PACKET_LOOPBACK here
390  );
391  return;
392  }
393  }
394 
395  /* other address */
396  if (m_device->NeedsArp ())
397  {
398  NS_LOG_LOGIC ("Needs ARP" << " " << dest);
399  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
400  Address hardwareDestination;
401  bool found = false;
402 
403  NS_ASSERT (icmpv6);
404 
405  if (dest.IsMulticast ())
406  {
407  NS_LOG_LOGIC ("IsMulticast");
408  NS_ASSERT_MSG (m_device->IsMulticast (), "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
409 
410  hardwareDestination = m_device->GetMulticast (dest);
411  found = true;
412  }
413  else
414  {
415  NS_LOG_LOGIC ("NDISC Lookup");
416  found = icmpv6->Lookup (p, dest, GetDevice (), m_ndCache, &hardwareDestination);
417  }
418 
419  if (found)
420  {
421  NS_LOG_LOGIC ("Address Resolved. Send.");
422  m_device->Send (p, hardwareDestination, Ipv6L3Protocol::PROT_NUMBER);
423  }
424  }
425  else
426  {
427  NS_LOG_LOGIC ("Doesn't need ARP");
428  m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
429  }
430 }
431 
432 void Ipv6Interface::SetCurHopLimit (uint8_t curHopLimit)
433 {
434  NS_LOG_FUNCTION (this << curHopLimit);
435  m_curHopLimit = curHopLimit;
436 }
437 
439 {
441  return m_curHopLimit;
442 }
443 
444 void Ipv6Interface::SetBaseReachableTime (uint16_t baseReachableTime)
445 {
446  NS_LOG_FUNCTION (this << baseReachableTime);
447  m_baseReachableTime = baseReachableTime;
448 }
449 
451 {
453  return m_baseReachableTime;
454 }
455 
456 void Ipv6Interface::SetReachableTime (uint16_t reachableTime)
457 {
458  NS_LOG_FUNCTION (this << reachableTime);
459  m_reachableTime = reachableTime;
460 }
461 
463 {
465  return m_reachableTime;
466 }
467 
468 void Ipv6Interface::SetRetransTimer (uint16_t retransTimer)
469 {
470  NS_LOG_FUNCTION (this << retransTimer);
471  m_retransTimer = retransTimer;
472 }
473 
475 {
477  return m_retransTimer;
478 }
479 
481 {
482  NS_LOG_FUNCTION (this << address << state);
483 
484  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
485  {
486  if ((*it).GetAddress () == address)
487  {
488  (*it).SetState (state);
489  return;
490  }
491  }
492  /* not found, maybe address has expired */
493 }
494 
496 {
497  NS_LOG_FUNCTION (this << address << uid);
498 
499  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
500  {
501  if ((*it).GetAddress () == address)
502  {
503  (*it).SetNsDadUid (uid);
504  return;
505  }
506  }
507  /* not found, maybe address has expired */
508 }
509 
511 {
512  NS_LOG_FUNCTION (this);
513  return m_ndCache;
514 }
515 
516 } /* namespace ns3 */
517 
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.
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:455
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:80
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.
std::list< Ipv6InterfaceAddress >::iterator Ipv6InterfaceAddressListI
Container Iterator for the Ipv6InterfaceAddresses.
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Packet addressed oo us.
Definition: net-device.h:278
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.
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.
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:1216
virtual ~Ipv6Interface()
Destructor.
void SetRetransTimer(uint16_t retransTimer)
Set the retransmission timer.
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:130
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.
void Send(Ptr< Packet > p, Ipv6Address dest)
Send a packet through this interface.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
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).
#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:47
uint16_t GetMetric() const
Get the metric.
State_e
State of an address associated with an interface.
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:389
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)
std::list< Ipv6InterfaceAddress >::const_iterator Ipv6InterfaceAddressListCI
Const Container Iterator for the Ipv6InterfaceAddresses.
uint16_t m_metric
The metric.
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:826
Ptr< NdiscCache > m_ndCache
Neighbor cache.
static const uint16_t PROT_NUMBER
The protocol number for IPv6 (0x86DD).
Ipv6Interface()
Constructs an Ipv6Interface.