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 #include "ns3/traffic-control-layer.h"
28 
29 #include "ipv6-interface.h"
30 #include "ipv6-queue-disc-item.h"
31 #include "loopback-net-device.h"
32 #include "ipv6-l3-protocol.h"
33 #include "icmpv6-l4-protocol.h"
34 #include "ipv6-header.h"
35 #include "ndisc-cache.h"
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE ("Ipv6Interface");
41 
42 NS_OBJECT_ENSURE_REGISTERED (Ipv6Interface);
43 
45 {
46  static TypeId tid = TypeId ("ns3::Ipv6Interface")
47  .SetParent<Object> ()
48  .SetGroupName ("Internet")
49  ;
50  return tid;
51 }
52 
54  : m_ifup (false),
55  m_forwarding (true),
56  m_metric (1),
57  m_node (0),
58  m_device (0),
59  m_tc (0),
60  m_ndCache (0),
61  m_curHopLimit (0),
62  m_baseReachableTime (0),
63  m_reachableTime (0),
64  m_retransTimer (0)
65 {
66  NS_LOG_FUNCTION (this);
67 }
68 
70 {
72 }
73 
75 {
77  m_node = 0;
78  m_device = 0;
79  m_tc = 0;
80  m_ndCache = 0;
82 }
83 
85 {
87 
88  if (m_node == 0 || m_device == 0)
89  {
90  return;
91  }
92 
93  /* set up link-local address */
94  if (!DynamicCast<LoopbackNetDevice> (m_device)) /* no autoconf for ip6-localhost */
95  {
96  Address addr = GetDevice ()->GetAddress ();
97 
99  {
101  AddAddress (ifaddr);
102  m_linkLocalAddress = ifaddr;
103  }
104  else if (Mac48Address::IsMatchingType (addr))
105  {
107  AddAddress (ifaddr);
108  m_linkLocalAddress = ifaddr;
109  }
110  else if (Mac16Address::IsMatchingType (addr))
111  {
113  AddAddress (ifaddr);
114  m_linkLocalAddress = ifaddr;
115  }
116  else if (Mac8Address::IsMatchingType (addr))
117  {
119  AddAddress (ifaddr);
120  m_linkLocalAddress = ifaddr;
121  }
122  else
123  {
124  NS_FATAL_ERROR ("IPv6 autoconf for this kind of address not implemented.");
125  }
126  }
127  else
128  {
129  return; /* no NDISC cache for ip6-localhost */
130  }
131  int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
132  Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
133 
134  if (icmpv6 && !m_ndCache)
135  {
136  m_ndCache = icmpv6->CreateCache (m_device, this);
137  }
138 }
139 
141 {
142  NS_LOG_FUNCTION (this << node);
143  m_node = node;
144 }
145 
147 {
148  NS_LOG_FUNCTION (this << device);
149  m_device = device;
150 }
151 
152 void
154 {
155  NS_LOG_FUNCTION (this << tc);
156  m_tc = tc;
157 }
158 
160 {
162  return m_device;
163 }
164 
165 void Ipv6Interface::SetMetric (uint16_t metric)
166 {
167  NS_LOG_FUNCTION (this << metric);
168  m_metric = metric;
169 }
170 
171 uint16_t Ipv6Interface::GetMetric () const
172 {
174  return m_metric;
175 }
176 
177 bool Ipv6Interface::IsUp () const
178 {
180  return m_ifup;
181 }
182 
184 {
186  return !m_ifup;
187 }
188 
190 {
192 
193  if (m_ifup)
194  {
195  return;
196  }
197  DoSetup ();
198  m_ifup = true;
199 }
200 
202 {
204  m_ifup = false;
205  m_addresses.clear ();
206  m_ndCache->Flush ();
207 }
208 
210 {
212  return m_forwarding;
213 }
214 
215 void Ipv6Interface::SetForwarding (bool forwarding)
216 {
217  NS_LOG_FUNCTION (this << forwarding);
218  m_forwarding = forwarding;
219 }
220 
222 {
224  Ipv6Address addr = iface.GetAddress ();
225 
226  /* DAD handling */
227  if (!addr.IsAny ())
228  {
229  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
230  {
231  if (it->first.GetAddress () == addr)
232  {
233  return false;
234  }
235  }
236 
238  m_addresses.push_back (std::make_pair (iface, solicited));
239 
240  if (!addr.IsAny () || !addr.IsLocalhost ())
241  {
242  /* DAD handling */
243 
244  int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
245  Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
246 
247  if (icmpv6 && icmpv6->IsAlwaysDad ())
248  {
249  if (icmpv6->IsAlwaysDad ())
250  {
251  Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
253  }
254  else
255  {
257  }
258  }
259  }
260  return true;
261  }
262 
263  /* bad address */
264  return false;
265 }
266 
268 {
269  /* IPv6 interface has always at least one IPv6 link-local address */
271 
272  return m_linkLocalAddress;
273 }
274 
276 {
277  /* IPv6 interface has always at least one IPv6 Solicited Multicast address */
278  NS_LOG_FUNCTION (this << address);
279 
280  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
281  {
282  if (it->second == address)
283  {
284  return true;
285  }
286  }
287 
288  return false;
289 }
290 
292 {
293  NS_LOG_FUNCTION (this << index);
294  uint32_t i = 0;
295 
296  if (m_addresses.size () > index)
297  {
298  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
299  {
300  if (i == index)
301  {
302  return it->first;
303  }
304  i++;
305  }
306  }
307  else
308  {
309  NS_FATAL_ERROR ("index " << index << " out of bounds");
310  }
312  return addr; /* quiet compiler */
313 }
314 
316 {
318  return m_addresses.size ();
319 }
320 
322 {
323  NS_LOG_FUNCTION (this << index);
324  uint32_t i = 0;
325 
326  if (m_addresses.size () < index)
327  {
328  NS_FATAL_ERROR ("Removing index that does not exist in Ipv6Interface::RemoveAddress");
329  }
330 
331  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
332  {
333  if (i == index)
334  {
335  Ipv6InterfaceAddress iface = it->first;
336  m_addresses.erase (it);
337  return iface;
338  }
339 
340  i++;
341  }
342  NS_FATAL_ERROR ("Address " << index << " not found");
344  return addr; /* quiet compiler */
345 }
346 
349 {
350  NS_LOG_FUNCTION(this << address);
351 
352  if (address == address.GetLoopback())
353  {
354  NS_LOG_WARN ("Cannot remove loopback address.");
355  return Ipv6InterfaceAddress();
356  }
357 
358  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
359  {
360  if(it->first.GetAddress () == address)
361  {
362  Ipv6InterfaceAddress iface = it->first;
363  m_addresses.erase(it);
364  return iface;
365  }
366  }
367  return Ipv6InterfaceAddress();
368 }
369 
371 {
372  NS_LOG_FUNCTION (this << dst);
373 
374  for (Ipv6InterfaceAddressList::const_iterator it = m_addresses.begin (); it != m_addresses.end (); ++it)
375  {
376  Ipv6InterfaceAddress ifaddr = it->first;
377 
378  if (ifaddr.GetPrefix ().IsMatch (ifaddr.GetAddress (), dst))
379  {
380  return ifaddr;
381  }
382  }
383 
384  /* NS_ASSERT_MSG (false, "Not matching address."); */
386  return ret; /* quiet compiler */
387 }
388 
390 {
391  NS_LOG_FUNCTION (this << p << dest);
392 
393  if (!IsUp ())
394  {
395  return;
396  }
397 
399 
400  /* check if destination is localhost (::1), if yes we don't pass through
401  * traffic control layer */
402  if (DynamicCast<LoopbackNetDevice> (m_device))
403  {
407  p->AddHeader (hdr);
408  m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
409  return;
410  }
411 
412  NS_ASSERT (m_tc != 0);
413 
414  /* check if destination is for one of our interface */
415  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
416  {
417  if (dest == it->first.GetAddress ())
418  {
419  p->AddHeader (hdr);
421  m_device->GetBroadcast (),
422  m_device->GetBroadcast (),
424  return;
425  }
426  }
427 
428  /* other address */
429  if (m_device->NeedsArp ())
430  {
431  NS_LOG_LOGIC ("Needs NDISC " << dest);
432 
433  int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
434  Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
435 
436  Address hardwareDestination;
437  bool found = false;
438 
439  NS_ASSERT (icmpv6);
440 
441  if (dest.IsMulticast ())
442  {
443  NS_LOG_LOGIC ("IsMulticast");
444  NS_ASSERT_MSG (m_device->IsMulticast (), "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
445 
446  hardwareDestination = m_device->GetMulticast (dest);
447  found = true;
448  }
449  else
450  {
451  NS_LOG_LOGIC ("NDISC Lookup");
452  found = icmpv6->Lookup (p, hdr, dest, GetDevice (), m_ndCache, &hardwareDestination);
453  }
454 
455  if (found)
456  {
457  NS_LOG_LOGIC ("Address Resolved. Send.");
458  m_tc->Send (m_device, Create<Ipv6QueueDiscItem> (p, hardwareDestination, Ipv6L3Protocol::PROT_NUMBER, hdr));
459  }
460  }
461  else
462  {
463  NS_LOG_LOGIC ("Doesn't need NDISC");
464  m_tc->Send (m_device, Create<Ipv6QueueDiscItem> (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER, hdr));
465  }
466 }
467 
468 void Ipv6Interface::SetCurHopLimit (uint8_t curHopLimit)
469 {
470  NS_LOG_FUNCTION (this << curHopLimit);
471  m_curHopLimit = curHopLimit;
472 }
473 
475 {
477  return m_curHopLimit;
478 }
479 
480 void Ipv6Interface::SetBaseReachableTime (uint16_t baseReachableTime)
481 {
482  NS_LOG_FUNCTION (this << baseReachableTime);
483  m_baseReachableTime = baseReachableTime;
484 }
485 
487 {
489  return m_baseReachableTime;
490 }
491 
492 void Ipv6Interface::SetReachableTime (uint16_t reachableTime)
493 {
494  NS_LOG_FUNCTION (this << reachableTime);
495  m_reachableTime = reachableTime;
496 }
497 
499 {
501  return m_reachableTime;
502 }
503 
504 void Ipv6Interface::SetRetransTimer (uint16_t retransTimer)
505 {
506  NS_LOG_FUNCTION (this << retransTimer);
507  m_retransTimer = retransTimer;
508 }
509 
511 {
513  return m_retransTimer;
514 }
515 
517 {
518  NS_LOG_FUNCTION (this << address << state);
519 
520  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
521  {
522  if (it->first.GetAddress () == address)
523  {
524  it->first.SetState (state);
525  return;
526  }
527  }
528  /* not found, maybe address has expired */
529 }
530 
532 {
533  NS_LOG_FUNCTION (this << address << uid);
534 
535  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
536  {
537  if (it->first.GetAddress () == address)
538  {
539  it->first.SetNsDadUid (uid);
540  return;
541  }
542  }
543  /* not found, maybe address has expired */
544 }
545 
547 {
548  NS_LOG_FUNCTION (this);
549  return m_ndCache;
550 }
551 
552 } /* namespace ns3 */
553 
Ipv6InterfaceAddressList m_addresses
The addresses assigned to this interface.
static bool IsMatchingType(const Address &address)
Ipv6Address GetAddress() const
Get the IPv6 address.
bool IsLocalhost() const
If the IPv6 address is localhost (::1).
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#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.
Ipv6InterfaceAddress GetLinkLocalAddress() const
Get link-local address from IPv6 interface.
bool m_forwarding
Forwarding state.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
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)
static Ipv6Address MakeAutoconfiguredLinkLocalAddress(Address mac)
Make the autoconfigured link-local IPv6 address from a Mac 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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
IPv6 address associated with an interface.
Ptr< NetDevice > m_device
NetDevice associated with this interface.
void SetDevice(Ptr< NetDevice > device)
Set the NetDevice.
bool IsForwarding() const
If the interface allows forwarding packets.
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 Ptr< NetDevice > GetDevice() const
Get the NetDevice.
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
static bool IsMatchingType(const Address &address)
Check that a generic Address is compatible with Mac8Address.
Definition: mac8-address.cc:63
bool IsSolicitedMulticastAddress(Ipv6Address address) const
Checks if the address is a Solicited Multicast address for this interface.
a polymophic address class
Definition: address.h:90
void SetBaseReachableTime(uint16_t baseReachableTime)
Set the base reachable time.
bool IsMatch(Ipv6Address a, Ipv6Address b) const
If the Address match the type.
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).
void Send(Ptr< Packet > p, const Ipv6Header &hdr, Ipv6Address dest)
Send a packet through this 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:1389
virtual ~Ipv6Interface()
Destructor.
void SetRetransTimer(uint16_t retransTimer)
Set the retransmission timer.
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:152
uint16_t m_retransTimer
Retransmission timer (in millisecond).
bool IsUp() const
Is the interface UP ?
static TypeId GetTypeId()
Get the type ID.
Ipv6InterfaceAddress m_linkLocalAddress
The link-local addresses assigned to this interface.
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.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t m_curHopLimit
Current hop limit.
uint16_t GetReachableTime() const
Get the reachable time.
address
Definition: first.py:37
static uint16_t GetStaticProtocolNumber()
Get ICMPv6 protocol number.
void SetCurHopLimit(uint8_t curHopLimit)
Set the current hop limit.
uint32_t GetNAddresses(void) const
Get number of addresses on this IPv6 interface.
std::list< std::pair< Ipv6InterfaceAddress, Ipv6Address > >::const_iterator Ipv6InterfaceAddressListCI
Const Container Iterator for the Ipv6InterfaceAddresses.
Ipv6InterfaceAddress GetAddress(uint32_t index) const
Get an address from IPv6 interface.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
std::list< std::pair< Ipv6InterfaceAddress, Ipv6Address > >::iterator Ipv6InterfaceAddressListI
Container Iterator for the Ipv6InterfaceAddresses.
void DoDAD(Ipv6Address target, Ptr< Ipv6Interface > interface)
Do the Duplication Address Detection (DAD).
Describes an IPv6 address.
Definition: ipv6-address.h:49
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:298
void DoSetup()
Initialize interface.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:264
uint16_t m_reachableTime
Reachable time (in millisecond).
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
uint16_t GetBaseReachableTime() const
Get the base reachable time.
uint8_t GetCurHopLimit() const
Get the current hop limit value.
Describes an IPv6 prefix.
Definition: ipv6-address.h:449
Ipv6InterfaceAddress RemoveAddress(uint32_t index)
Remove an address from interface.
Ptr< NdiscCache > GetNdiscCache() const
A base class which provides memory management and object aggregation.
Definition: object.h:87
bool IsDown() const
Is the interface DOWN ?
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.
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:915
Ptr< NdiscCache > m_ndCache
Neighbor cache.
static Mac8Address ConvertFrom(const Address &address)
Convert a generic address to a Mac8Address.
Definition: mac8-address.cc:54
static const uint16_t PROT_NUMBER
The protocol number for IPv6 (0x86DD).
Ipv6Prefix GetPrefix() const
Get the IPv6 prefix.
uint16_t GetRetransTimer() const
Get the retransmission timer.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
bool IsAny() const
If the IPv6 address is the "Any" address.
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
Ipv6Interface()
Constructs an Ipv6Interface.