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 
40 NS_OBJECT_ENSURE_REGISTERED (Ipv6Interface)
41  ;
42 
44 {
45  static TypeId tid = TypeId ("ns3::Ipv6Interface")
46  .SetParent<Object> ()
47  ;
48  return tid;
49 }
50 
52  : m_ifup (false),
53  m_forwarding (true),
54  m_metric (1),
55  m_node (0),
56  m_device (0),
57  m_ndCache (0),
58  m_curHopLimit (0),
59  m_baseReachableTime (0),
60  m_reachableTime (0),
61  m_retransTimer (0)
62 {
63  NS_LOG_FUNCTION (this);
64 }
65 
67 {
69 }
70 
72 {
74  m_node = 0;
75  m_device = 0;
76  m_ndCache = 0;
78 }
79 
81 {
83 
84  if (m_node == 0 || m_device == 0)
85  {
86  return;
87  }
88 
89  /* set up link-local address */
90  if (!DynamicCast<LoopbackNetDevice> (m_device)) /* no autoconf for ip6-localhost */
91  {
92  Address addr = GetDevice ()->GetAddress ();
93 
95  {
97  AddAddress (ifaddr);
98  }
99  else if (Mac48Address::IsMatchingType (addr))
100  {
102  AddAddress (ifaddr);
103  }
104  else if (Mac16Address::IsMatchingType (addr))
105  {
107  AddAddress (ifaddr);
108  }
109  else
110  {
111  NS_ASSERT_MSG (false, "IPv6 autoconf for this kind of address not implemented.");
112  }
113  }
114  else
115  {
116  return; /* no NDISC cache for ip6-localhost */
117  }
118 
119  Ptr<Icmpv6L4Protocol> icmpv6 = m_node->GetObject<Ipv6L3Protocol> ()->GetIcmpv6 ();
120  if (m_device->NeedsArp ())
121  {
122  m_ndCache = icmpv6->CreateCache (m_device, this);
123  }
124 }
125 
127 {
128  NS_LOG_FUNCTION (this << node);
129  m_node = node;
130  DoSetup ();
131 }
132 
134 {
135  NS_LOG_FUNCTION (this << device);
136  m_device = device;
137  DoSetup ();
138 }
139 
141 {
143  return m_device;
144 }
145 
146 void Ipv6Interface::SetMetric (uint16_t metric)
147 {
148  NS_LOG_FUNCTION (this << metric);
149  m_metric = metric;
150 }
151 
152 uint16_t Ipv6Interface::GetMetric () const
153 {
155  return m_metric;
156 }
157 
158 bool Ipv6Interface::IsUp () const
159 {
161  return m_ifup;
162 }
163 
165 {
167  return !m_ifup;
168 }
169 
171 {
173 
174  if (m_ifup)
175  {
176  return;
177  }
178  m_ifup = true;
179 }
180 
182 {
184  m_ifup = false;
185  m_addresses.clear ();
186 }
187 
189 {
191  return m_forwarding;
192 }
193 
194 void Ipv6Interface::SetForwarding (bool forwarding)
195 {
196  NS_LOG_FUNCTION (this << forwarding);
197  m_forwarding = forwarding;
198 }
199 
201 {
203  Ipv6Address addr = iface.GetAddress ();
204 
205  /* DAD handling */
206  if (!addr.IsAny ())
207  {
208  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
209  {
210  if ((*it).GetAddress () == addr)
211  {
212  return false;
213  }
214  }
215 
216  m_addresses.push_back (iface);
217 
218  if (!addr.IsAny () || !addr.IsLocalhost ())
219  {
220  /* DAD handling */
221  Ptr<Icmpv6L4Protocol> icmpv6 = m_node->GetObject<Ipv6L3Protocol> ()->GetIcmpv6 ();
222 
223  if (icmpv6 && icmpv6->IsAlwaysDad ())
224  {
225  Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
226  Simulator::Schedule (Seconds (1.), &Icmpv6L4Protocol::FunctionDadTimeout, icmpv6, this, addr);
227  }
228  }
229  return true;
230  }
231 
232  /* bad address */
233  return false;
234 }
235 
237 {
238  /* IPv6 interface has always at least one IPv6 link-local address */
240 
241  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
242  {
243  if ((*it).GetAddress ().IsLinkLocal ())
244  {
245  return (*it);
246  }
247  }
248  NS_ASSERT_MSG (false, "No link-local address on interface " << this);
250  return addr; /* quiet compiler */
251 }
252 
254 {
255  NS_LOG_FUNCTION (this << index);
256  uint32_t i = 0;
257 
258  if (m_addresses.size () > index)
259  {
260  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
261  {
262  if (i == index)
263  {
264  return (*it);
265  }
266  i++;
267  }
268  }
269 
270  NS_ASSERT_MSG (false, "Address " << index << " not found");
272  return addr; /* quiet compiler */
273 }
274 
276 {
278  return m_addresses.size ();
279 }
280 
282 {
283  NS_LOG_FUNCTION (this << index);
284  uint32_t i = 0;
285 
286  if (m_addresses.size () < index)
287  {
288  NS_ASSERT_MSG (false, "Try to remove index that don't exist in Ipv6Interface::RemoveAddress");
289  }
290 
291  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
292  {
293  if (i == index)
294  {
295  Ipv6InterfaceAddress iface = (*it);
296  m_addresses.erase (it);
297  return iface;
298  }
299 
300  i++;
301  }
302 
303  NS_ASSERT_MSG (false, "Address " << index << " not found");
305  return addr; /* quiet compiler */
306 }
307 
310 {
311  NS_LOG_FUNCTION(this << address);
312 
313  if (address == address.GetLoopback())
314  {
315  NS_LOG_WARN ("Cannot remove loopback address.");
316  return Ipv6InterfaceAddress();
317  }
318 
319  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
320  {
321  if((*it).GetAddress() == address)
322  {
323  Ipv6InterfaceAddress iface = (*it);
324  m_addresses.erase(it);
325  return iface;
326  }
327  }
328  return Ipv6InterfaceAddress();
329 }
330 
332 {
333  NS_LOG_FUNCTION (this << dst);
334 
335  for (Ipv6InterfaceAddressList::const_iterator it = m_addresses.begin (); it != m_addresses.end (); ++it)
336  {
337  Ipv6InterfaceAddress ifaddr = (*it);
338 
339  if (ifaddr.GetPrefix ().IsMatch (ifaddr.GetAddress (), dst))
340  {
341  return ifaddr;
342  }
343  }
344 
345  /* NS_ASSERT_MSG (false, "Not matching address."); */
347  return ret; /* quiet compiler */
348 }
349 
351 {
352  NS_LOG_FUNCTION (this << p << dest);
354 
355  if (!IsUp ())
356  {
357  return;
358  }
359 
360  /* check if destination is localhost (::1) */
361  if (DynamicCast<LoopbackNetDevice> (m_device))
362  {
366  m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
367  return;
368  }
369 
370  /* check if destination is for one of our interface */
371  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
372  {
373  if (dest == (*it).GetAddress ())
374  {
375  ipv6->Receive (m_device, p, Ipv6L3Protocol::PROT_NUMBER,
376  m_device->GetBroadcast (),
377  m_device->GetBroadcast (),
378  NetDevice::PACKET_HOST // note: linux uses PACKET_LOOPBACK here
379  );
380  return;
381  }
382  }
383 
384  /* other address */
385  if (m_device->NeedsArp ())
386  {
387  NS_LOG_LOGIC ("Needs ARP" << " " << dest);
388  Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
389  Address hardwareDestination;
390  bool found = false;
391 
392  NS_ASSERT (icmpv6);
393 
394  if (dest.IsMulticast ())
395  {
396  NS_LOG_LOGIC ("IsMulticast");
397  NS_ASSERT_MSG (m_device->IsMulticast (), "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
398 
399  hardwareDestination = m_device->GetMulticast (dest);
400  found = true;
401  }
402  else
403  {
404  NS_LOG_LOGIC ("NDISC Lookup");
405  found = icmpv6->Lookup (p, dest, GetDevice (), m_ndCache, &hardwareDestination);
406  }
407 
408  if (found)
409  {
410  NS_LOG_LOGIC ("Address Resolved. Send.");
411  m_device->Send (p, hardwareDestination, Ipv6L3Protocol::PROT_NUMBER);
412  }
413  }
414  else
415  {
416  NS_LOG_LOGIC ("Doesn't need ARP");
417  m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
418  }
419 }
420 
421 void Ipv6Interface::SetCurHopLimit (uint8_t curHopLimit)
422 {
423  NS_LOG_FUNCTION (this << curHopLimit);
424  m_curHopLimit = curHopLimit;
425 }
426 
428 {
430  return m_curHopLimit;
431 }
432 
433 void Ipv6Interface::SetBaseReachableTime (uint16_t baseReachableTime)
434 {
435  NS_LOG_FUNCTION (this << baseReachableTime);
436  m_baseReachableTime = baseReachableTime;
437 }
438 
440 {
442  return m_baseReachableTime;
443 }
444 
445 void Ipv6Interface::SetReachableTime (uint16_t reachableTime)
446 {
447  NS_LOG_FUNCTION (this << reachableTime);
448  m_reachableTime = reachableTime;
449 }
450 
452 {
454  return m_reachableTime;
455 }
456 
457 void Ipv6Interface::SetRetransTimer (uint16_t retransTimer)
458 {
459  NS_LOG_FUNCTION (this << retransTimer);
460  m_retransTimer = retransTimer;
461 }
462 
464 {
466  return m_retransTimer;
467 }
468 
470 {
471  NS_LOG_FUNCTION (this << address << state);
472 
473  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
474  {
475  if ((*it).GetAddress () == address)
476  {
477  (*it).SetState (state);
478  return;
479  }
480  }
481  /* not found, maybe address has expired */
482 }
483 
485 {
486  NS_LOG_FUNCTION (this << address << uid);
487 
488  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
489  {
490  if ((*it).GetAddress () == address)
491  {
492  (*it).SetNsDadUid (uid);
493  return;
494  }
495  }
496  /* not found, maybe address has expired */
497 }
498 
499 } /* namespace ns3 */
500 
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.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
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.
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
static Mac16Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
IPv6 layer implementation.
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
Ipv6InterfaceAddress GetAddress(uint32_t index) const
Get an address from IPv6 interface.
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:336
std::list< Ipv6InterfaceAddress >::iterator Ipv6InterfaceAddressListI
Container Iterator for the Ipv6InterfaceAddresses.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
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:824
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.
uint16_t m_retransTimer
Retransmission timer (in millisecond).
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)
Definition: log.h:368
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.
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)
Definition: assert.h:86
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:272
void DoSetup()
Initialize interface.
#define NS_LOG_WARN(msg)
Definition: log.h:280
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:63
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:361
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:611
Ptr< NdiscCache > m_ndCache
Neighbor cache.
static const uint16_t PROT_NUMBER
The protocol number for IPv6 (0x86DD).
Ipv6Interface()
Constructs an Ipv6Interface.