A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  ;
46  return tid;
47 }
48 
50  : m_ifup (false),
51  m_forwarding (true),
52  m_metric (1),
53  m_node (0),
54  m_device (0),
55  m_ndCache (0),
56  m_curHopLimit (0),
57  m_baseReachableTime (0),
58  m_reachableTime (0),
59  m_retransTimer (0)
60 {
61  NS_LOG_FUNCTION (this);
62 }
63 
65 {
67 }
68 
70 {
72  m_node = 0;
73  m_device = 0;
74  m_ndCache = 0;
76 }
77 
79 {
81 
82  if (m_node == 0 || m_device == 0)
83  {
84  return;
85  }
86 
87  /* set up link-local address */
88  if (!DynamicCast<LoopbackNetDevice> (m_device)) /* no autoconf for ip6-localhost */
89  {
90  Address addr = GetDevice ()->GetAddress ();
91 
93  {
95  AddAddress (ifaddr);
96  }
97  else if (Mac48Address::IsMatchingType (addr))
98  {
100  AddAddress (ifaddr);
101  }
102  else if (Mac16Address::IsMatchingType (addr))
103  {
105  AddAddress (ifaddr);
106  }
107  else
108  {
109  NS_ASSERT_MSG (false, "IPv6 autoconf for this kind of address not implemented.");
110  }
111  }
112  else
113  {
114  return; /* no NDISC cache for ip6-localhost */
115  }
116 
118  Ptr<Icmpv6L4Protocol> icmpv6;
119  if (proto)
120  {
121  icmpv6 = proto->GetObject <Icmpv6L4Protocol> ();
122  }
123  if (icmpv6)
124  {
125  m_ndCache = icmpv6->CreateCache (m_device, this);
126  }
127 }
128 
130 {
131  NS_LOG_FUNCTION (this << node);
132  m_node = node;
133  DoSetup ();
134 }
135 
137 {
138  NS_LOG_FUNCTION (this << device);
139  m_device = device;
140  DoSetup ();
141 }
142 
144 {
146  return m_device;
147 }
148 
149 void Ipv6Interface::SetMetric (uint16_t metric)
150 {
151  NS_LOG_FUNCTION (this << metric);
152  m_metric = metric;
153 }
154 
155 uint16_t Ipv6Interface::GetMetric () const
156 {
158  return m_metric;
159 }
160 
161 bool Ipv6Interface::IsUp () const
162 {
164  return m_ifup;
165 }
166 
168 {
170  return !m_ifup;
171 }
172 
174 {
176 
177  if (m_ifup)
178  {
179  return;
180  }
181  m_ifup = true;
182 }
183 
185 {
187  m_ifup = false;
188  m_addresses.clear ();
189  m_ndCache->Flush ();
190 }
191 
193 {
195  return m_forwarding;
196 }
197 
198 void Ipv6Interface::SetForwarding (bool forwarding)
199 {
200  NS_LOG_FUNCTION (this << forwarding);
201  m_forwarding = forwarding;
202 }
203 
205 {
207  Ipv6Address addr = iface.GetAddress ();
208 
209  /* DAD handling */
210  if (!addr.IsAny ())
211  {
212  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
213  {
214  if ((*it).GetAddress () == addr)
215  {
216  return false;
217  }
218  }
219 
220  m_addresses.push_back (iface);
221 
222  if (!addr.IsAny () || !addr.IsLocalhost ())
223  {
224  /* DAD handling */
226  Ptr<Icmpv6L4Protocol> icmpv6;
227  if (proto)
228  {
229  icmpv6 = proto->GetObject <Icmpv6L4Protocol> ();
230  }
231 
232  if (icmpv6 && icmpv6->IsAlwaysDad ())
233  {
234  Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
235  Simulator::Schedule (Seconds (1.), &Icmpv6L4Protocol::FunctionDadTimeout, icmpv6, this, addr);
236  }
237  }
238  return true;
239  }
240 
241  /* bad address */
242  return false;
243 }
244 
246 {
247  /* IPv6 interface has always at least one IPv6 link-local address */
249 
250  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
251  {
252  if ((*it).GetAddress ().IsLinkLocal ())
253  {
254  return (*it);
255  }
256  }
257  NS_ASSERT_MSG (false, "No link-local address on interface " << this);
259  return addr; /* quiet compiler */
260 }
261 
263 {
264  NS_LOG_FUNCTION (this << index);
265  uint32_t i = 0;
266 
267  if (m_addresses.size () > index)
268  {
269  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
270  {
271  if (i == index)
272  {
273  return (*it);
274  }
275  i++;
276  }
277  }
278 
279  NS_ASSERT_MSG (false, "Address " << index << " not found");
281  return addr; /* quiet compiler */
282 }
283 
285 {
287  return m_addresses.size ();
288 }
289 
291 {
292  NS_LOG_FUNCTION (this << index);
293  uint32_t i = 0;
294 
295  if (m_addresses.size () < index)
296  {
297  NS_ASSERT_MSG (false, "Try to remove index that don't exist in Ipv6Interface::RemoveAddress");
298  }
299 
300  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
301  {
302  if (i == index)
303  {
304  Ipv6InterfaceAddress iface = (*it);
305  m_addresses.erase (it);
306  return iface;
307  }
308 
309  i++;
310  }
311 
312  NS_ASSERT_MSG (false, "Address " << index << " not found");
314  return addr; /* quiet compiler */
315 }
316 
319 {
320  NS_LOG_FUNCTION(this << address);
321 
322  if (address == address.GetLoopback())
323  {
324  NS_LOG_WARN ("Cannot remove loopback address.");
325  return Ipv6InterfaceAddress();
326  }
327 
328  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
329  {
330  if((*it).GetAddress() == address)
331  {
332  Ipv6InterfaceAddress iface = (*it);
333  m_addresses.erase(it);
334  return iface;
335  }
336  }
337  return Ipv6InterfaceAddress();
338 }
339 
341 {
342  NS_LOG_FUNCTION (this << dst);
343 
344  for (Ipv6InterfaceAddressList::const_iterator it = m_addresses.begin (); it != m_addresses.end (); ++it)
345  {
346  Ipv6InterfaceAddress ifaddr = (*it);
347 
348  if (ifaddr.GetPrefix ().IsMatch (ifaddr.GetAddress (), dst))
349  {
350  return ifaddr;
351  }
352  }
353 
354  /* NS_ASSERT_MSG (false, "Not matching address."); */
356  return ret; /* quiet compiler */
357 }
358 
360 {
361  NS_LOG_FUNCTION (this << p << dest);
363 
364  if (!IsUp ())
365  {
366  return;
367  }
368 
369  /* check if destination is localhost (::1) */
370  if (DynamicCast<LoopbackNetDevice> (m_device))
371  {
375  m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
376  return;
377  }
378 
379  /* check if destination is for one of our interface */
380  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
381  {
382  if (dest == (*it).GetAddress ())
383  {
384  ipv6->Receive (m_device, p, Ipv6L3Protocol::PROT_NUMBER,
385  m_device->GetBroadcast (),
386  m_device->GetBroadcast (),
387  NetDevice::PACKET_HOST // note: linux uses PACKET_LOOPBACK here
388  );
389  return;
390  }
391  }
392 
393  /* other address */
394  if (m_device->NeedsArp ())
395  {
396  NS_LOG_LOGIC ("Needs ARP" << " " << dest);
397  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
398  Address hardwareDestination;
399  bool found = false;
400 
401  NS_ASSERT (icmpv6);
402 
403  if (dest.IsMulticast ())
404  {
405  NS_LOG_LOGIC ("IsMulticast");
406  NS_ASSERT_MSG (m_device->IsMulticast (), "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
407 
408  hardwareDestination = m_device->GetMulticast (dest);
409  found = true;
410  }
411  else
412  {
413  NS_LOG_LOGIC ("NDISC Lookup");
414  found = icmpv6->Lookup (p, dest, GetDevice (), m_ndCache, &hardwareDestination);
415  }
416 
417  if (found)
418  {
419  NS_LOG_LOGIC ("Address Resolved. Send.");
420  m_device->Send (p, hardwareDestination, Ipv6L3Protocol::PROT_NUMBER);
421  }
422  }
423  else
424  {
425  NS_LOG_LOGIC ("Doesn't need ARP");
426  m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
427  }
428 }
429 
430 void Ipv6Interface::SetCurHopLimit (uint8_t curHopLimit)
431 {
432  NS_LOG_FUNCTION (this << curHopLimit);
433  m_curHopLimit = curHopLimit;
434 }
435 
437 {
439  return m_curHopLimit;
440 }
441 
442 void Ipv6Interface::SetBaseReachableTime (uint16_t baseReachableTime)
443 {
444  NS_LOG_FUNCTION (this << baseReachableTime);
445  m_baseReachableTime = baseReachableTime;
446 }
447 
449 {
451  return m_baseReachableTime;
452 }
453 
454 void Ipv6Interface::SetReachableTime (uint16_t reachableTime)
455 {
456  NS_LOG_FUNCTION (this << reachableTime);
457  m_reachableTime = reachableTime;
458 }
459 
461 {
463  return m_reachableTime;
464 }
465 
466 void Ipv6Interface::SetRetransTimer (uint16_t retransTimer)
467 {
468  NS_LOG_FUNCTION (this << retransTimer);
469  m_retransTimer = retransTimer;
470 }
471 
473 {
475  return m_retransTimer;
476 }
477 
479 {
480  NS_LOG_FUNCTION (this << address << state);
481 
482  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
483  {
484  if ((*it).GetAddress () == address)
485  {
486  (*it).SetState (state);
487  return;
488  }
489  }
490  /* not found, maybe address has expired */
491 }
492 
494 {
495  NS_LOG_FUNCTION (this << address << uid);
496 
497  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
498  {
499  if ((*it).GetAddress () == address)
500  {
501  (*it).SetNsDadUid (uid);
502  return;
503  }
504  }
505  /* not found, maybe address has expired */
506 }
507 
508 } /* namespace ns3 */
509 
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 the class in the ns-3 factory.
Definition: object-base.h:38
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:61
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:170
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.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
std::list< Ipv6InterfaceAddress >::iterator Ipv6InterfaceAddressListI
Container Iterator for the Ipv6InterfaceAddresses.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:825
bool IsUp() const
Is the interface UP ?
bool IsDown() const
Is the interface DOWN ?
a polymophic address class
Definition: address.h:86
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.
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.
virtual ~Ipv6Interface()
Destructor.
void SetRetransTimer(uint16_t retransTimer)
Set the retransmission timer.
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:128
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:233
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.
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:84
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:46
uint16_t GetMetric() const
Get the metric.
State_e
State of an address associated with an interface.
static Mac64Address ConvertFrom(const Address &address)
Packet addressed oo us.
Definition: net-device.h:276
void DoSetup()
Initialize interface.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:203
uint16_t m_reachableTime
Reachable time (in millisecond).
bool IsForwarding() const
If the interface allows forwarding packets.
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
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:64
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).
Ptr< T > GetObject(void) const
Definition: object.h:362
void SetDown()
Disable this interface.
a unique identifier for an interface.
Definition: type-id.h:49
Ipv6InterfaceAddress GetAddressMatchingDestination(Ipv6Address dst)
Get an address which is in the same network prefix as destination.
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
Ptr< NdiscCache > m_ndCache
Neighbor cache.
static const uint16_t PROT_NUMBER
The protocol number for IPv6 (0x86DD).
Ipv6Interface()
Constructs an Ipv6Interface.