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 {
71 }
72 
74 {
75  NS_LOG_FUNCTION (this);
76  m_node = 0;
77  m_device = 0;
78  m_tc = 0;
79  m_ndCache = 0;
81 }
82 
84 {
85  NS_LOG_FUNCTION (this);
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 ();
97  AddAddress (ifaddr);
98  m_linkLocalAddress = ifaddr;
99  }
100  else
101  {
102  return; /* no NDISC cache for ip6-localhost */
103  }
104  int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
105  Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
106 
107  if (icmpv6 && !m_ndCache)
108  {
109  m_ndCache = icmpv6->CreateCache (m_device, this);
110  }
111 }
112 
114 {
115  NS_LOG_FUNCTION (this << node);
116  m_node = node;
117 }
118 
120 {
121  NS_LOG_FUNCTION (this << device);
122  m_device = device;
123 }
124 
125 void
127 {
128  NS_LOG_FUNCTION (this << tc);
129  m_tc = tc;
130 }
131 
133 {
134  NS_LOG_FUNCTION (this);
135  return m_device;
136 }
137 
138 void Ipv6Interface::SetMetric (uint16_t metric)
139 {
140  NS_LOG_FUNCTION (this << metric);
141  m_metric = metric;
142 }
143 
144 uint16_t Ipv6Interface::GetMetric () const
145 {
146  NS_LOG_FUNCTION (this);
147  return m_metric;
148 }
149 
150 bool Ipv6Interface::IsUp () const
151 {
152  NS_LOG_FUNCTION (this);
153  return m_ifup;
154 }
155 
157 {
158  NS_LOG_FUNCTION (this);
159  return !m_ifup;
160 }
161 
163 {
164  NS_LOG_FUNCTION (this);
165 
166  if (m_ifup)
167  {
168  return;
169  }
170  DoSetup ();
171  m_ifup = true;
172 }
173 
175 {
176  NS_LOG_FUNCTION (this);
177  m_ifup = false;
178  m_addresses.clear ();
179  m_ndCache->Flush ();
180 }
181 
183 {
184  NS_LOG_FUNCTION (this);
185  return m_forwarding;
186 }
187 
188 void Ipv6Interface::SetForwarding (bool forwarding)
189 {
190  NS_LOG_FUNCTION (this << forwarding);
191  m_forwarding = forwarding;
192 }
193 
195 {
196  NS_LOG_FUNCTION (this << iface);
197  Ipv6Address addr = iface.GetAddress ();
198 
199  /* DAD handling */
200  if (!addr.IsAny ())
201  {
202  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
203  {
204  if (it->first.GetAddress () == addr)
205  {
206  return false;
207  }
208  }
209 
211  m_addresses.push_back (std::make_pair (iface, solicited));
212 
213  if (!addr.IsAny () || !addr.IsLocalhost ())
214  {
215  /* DAD handling */
216 
217  int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
218  Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
219 
220  if (icmpv6)
221  {
222  if (icmpv6->IsAlwaysDad ())
223  {
224  Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
226  }
227  else
228  {
230  }
231  }
232  }
233  return true;
234  }
235 
236  /* bad address */
237  return false;
238 }
239 
241 {
242  /* IPv6 interface has always at least one IPv6 link-local address */
243  NS_LOG_FUNCTION (this);
244 
245  return m_linkLocalAddress;
246 }
247 
249 {
250  /* IPv6 interface has always at least one IPv6 Solicited Multicast address */
251  NS_LOG_FUNCTION (this << address);
252 
253  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
254  {
255  if (it->second == address)
256  {
257  return true;
258  }
259  }
260 
261  return false;
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->first;
276  }
277  i++;
278  }
279  }
280  else
281  {
282  NS_FATAL_ERROR ("index " << index << " out of bounds");
283  }
285  return addr; /* quiet compiler */
286 }
287 
289 {
290  NS_LOG_FUNCTION (this);
291  return m_addresses.size ();
292 }
293 
295 {
296  NS_LOG_FUNCTION (this << index);
297  uint32_t i = 0;
298 
299  if (m_addresses.size () < index)
300  {
301  NS_FATAL_ERROR ("Removing index that does not exist in Ipv6Interface::RemoveAddress");
302  }
303 
304  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
305  {
306  if (i == index)
307  {
308  Ipv6InterfaceAddress iface = it->first;
309  m_addresses.erase (it);
310  return iface;
311  }
312 
313  i++;
314  }
315  NS_FATAL_ERROR ("Address " << index << " not found");
317  return addr; /* quiet compiler */
318 }
319 
322 {
323  NS_LOG_FUNCTION(this << address);
324 
325  if (address == address.GetLoopback())
326  {
327  NS_LOG_WARN ("Cannot remove loopback address.");
328  return Ipv6InterfaceAddress();
329  }
330 
331  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
332  {
333  if(it->first.GetAddress () == address)
334  {
335  Ipv6InterfaceAddress iface = it->first;
336  m_addresses.erase(it);
337  return iface;
338  }
339  }
340  return Ipv6InterfaceAddress();
341 }
342 
344 {
345  NS_LOG_FUNCTION (this << dst);
346 
347  for (Ipv6InterfaceAddressList::const_iterator it = m_addresses.begin (); it != m_addresses.end (); ++it)
348  {
349  Ipv6InterfaceAddress ifaddr = it->first;
350 
351  if (ifaddr.GetPrefix ().IsMatch (ifaddr.GetAddress (), dst))
352  {
353  return ifaddr;
354  }
355  }
356 
357  /* NS_ASSERT_MSG (false, "Not matching address."); */
359  return ret; /* quiet compiler */
360 }
361 
363 {
364  NS_LOG_FUNCTION (this << p << dest);
365 
366  if (!IsUp ())
367  {
368  return;
369  }
370 
372 
373  /* check if destination is localhost (::1), if yes we don't pass through
374  * traffic control layer */
375  if (DynamicCast<LoopbackNetDevice> (m_device))
376  {
380  p->AddHeader (hdr);
382  return;
383  }
384 
385  NS_ASSERT (m_tc != 0);
386 
387  /* check if destination is for one of our interface */
388  for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
389  {
390  if (dest == it->first.GetAddress ())
391  {
392  p->AddHeader (hdr);
397  return;
398  }
399  }
400 
401  /* other address */
402  if (m_device->NeedsArp ())
403  {
404  NS_LOG_LOGIC ("Needs NDISC " << dest);
405 
406  int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
407  Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
408 
409  Address hardwareDestination;
410  bool found = false;
411 
412  NS_ASSERT (icmpv6);
413 
414  if (dest.IsMulticast ())
415  {
416  NS_LOG_LOGIC ("IsMulticast");
417  NS_ASSERT_MSG (m_device->IsMulticast (), "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
418 
419  hardwareDestination = m_device->GetMulticast (dest);
420  found = true;
421  }
422  else
423  {
424  NS_LOG_LOGIC ("NDISC Lookup");
425  found = icmpv6->Lookup (p, hdr, dest, GetDevice (), m_ndCache, &hardwareDestination);
426  }
427 
428  if (found)
429  {
430  NS_LOG_LOGIC ("Address Resolved. Send.");
431  m_tc->Send (m_device, Create<Ipv6QueueDiscItem> (p, hardwareDestination, Ipv6L3Protocol::PROT_NUMBER, hdr));
432  }
433  }
434  else
435  {
436  NS_LOG_LOGIC ("Doesn't need NDISC");
437  m_tc->Send (m_device, Create<Ipv6QueueDiscItem> (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER, hdr));
438  }
439 }
440 
441 void Ipv6Interface::SetCurHopLimit (uint8_t curHopLimit)
442 {
443  NS_LOG_FUNCTION (this << curHopLimit);
444  m_curHopLimit = curHopLimit;
445 }
446 
448 {
449  NS_LOG_FUNCTION (this);
450  return m_curHopLimit;
451 }
452 
453 void Ipv6Interface::SetBaseReachableTime (uint16_t baseReachableTime)
454 {
455  NS_LOG_FUNCTION (this << baseReachableTime);
456  m_baseReachableTime = baseReachableTime;
457 }
458 
460 {
461  NS_LOG_FUNCTION (this);
462  return m_baseReachableTime;
463 }
464 
465 void Ipv6Interface::SetReachableTime (uint16_t reachableTime)
466 {
467  NS_LOG_FUNCTION (this << reachableTime);
468  m_reachableTime = reachableTime;
469 }
470 
472 {
473  NS_LOG_FUNCTION (this);
474  return m_reachableTime;
475 }
476 
477 void Ipv6Interface::SetRetransTimer (uint16_t retransTimer)
478 {
479  NS_LOG_FUNCTION (this << retransTimer);
480  m_retransTimer = retransTimer;
481 }
482 
484 {
485  NS_LOG_FUNCTION (this);
486  return m_retransTimer;
487 }
488 
490 {
491  NS_LOG_FUNCTION (this << address << state);
492 
493  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
494  {
495  if (it->first.GetAddress () == address)
496  {
497  it->first.SetState (state);
498  return;
499  }
500  }
501  /* not found, maybe address has expired */
502 }
503 
505 {
506  NS_LOG_FUNCTION (this << address << uid);
507 
508  for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
509  {
510  if (it->first.GetAddress () == address)
511  {
512  it->first.SetNsDadUid (uid);
513  return;
514  }
515  }
516  /* not found, maybe address has expired */
517 }
518 
520 {
521  NS_LOG_FUNCTION (this);
522  return m_ndCache;
523 }
524 
525 } /* namespace ns3 */
526 
a polymophic address class
Definition: address.h:91
void FunctionDadTimeout(Ipv6Interface *interface, Ipv6Address addr)
Function called when DAD timeout.
void DoDAD(Ipv6Address target, Ptr< Ipv6Interface > interface)
Do the Duplication Address Detection (DAD).
static uint16_t GetStaticProtocolNumber()
Get ICMPv6 protocol number.
Describes an IPv6 address.
Definition: ipv6-address.h:50
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
bool IsAny() const
If the IPv6 address is the "Any" address.
bool IsLocalhost() const
If the IPv6 address is localhost (::1).
static Ipv6Address MakeAutoconfiguredLinkLocalAddress(Address mac)
Make the autoconfigured link-local IPv6 address from a Mac address.
Packet header for IPv6.
Definition: ipv6-header.h:36
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:82
virtual Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const =0
Get L4 protocol by protocol number.
IPv6 address associated with an interface.
Ipv6Address GetAddress() const
Get the IPv6 address.
Ipv6Prefix GetPrefix() const
Get the IPv6 prefix.
State_e
State of an address associated with an interface.
Ipv6InterfaceAddress RemoveAddress(uint32_t index)
Remove an address from interface.
uint16_t GetBaseReachableTime() const
Get the base reachable time.
uint8_t m_curHopLimit
Current hop limit.
Ptr< NetDevice > m_device
NetDevice associated with this interface.
bool IsDown() const
Is the interface DOWN ?
virtual void DoDispose()
Dispose this object.
Ptr< NdiscCache > m_ndCache
Neighbor cache.
Ipv6InterfaceAddress GetLinkLocalAddress() const
Get link-local address from IPv6 interface.
bool m_ifup
The state of this interface.
uint8_t GetCurHopLimit() const
Get the current hop limit value.
static TypeId GetTypeId()
Get the type ID.
Ipv6Interface()
Constructs an Ipv6Interface.
void SetRetransTimer(uint16_t retransTimer)
Set the retransmission timer.
void DoSetup()
Initialize interface.
void SetForwarding(bool forward)
Set forwarding enabled or not.
void SetReachableTime(uint16_t reachableTime)
Set the reachable time.
Ipv6InterfaceAddress GetAddressMatchingDestination(Ipv6Address dst)
Get an address which is in the same network prefix as destination.
void SetNode(Ptr< Node > node)
Set node associated with interface.
void SetDown()
Disable this interface.
bool IsForwarding() const
If the interface allows forwarding packets.
uint16_t GetReachableTime() const
Get the reachable time.
bool IsUp() const
Is the interface UP ?
bool m_forwarding
Forwarding state.
uint16_t m_metric
The metric.
uint16_t m_reachableTime
Reachable time (in millisecond).
std::list< std::pair< Ipv6InterfaceAddress, Ipv6Address > >::const_iterator Ipv6InterfaceAddressListCI
Const Container Iterator for the Ipv6InterfaceAddresses.
bool IsSolicitedMulticastAddress(Ipv6Address address) const
Checks if the address is a Solicited Multicast address for this interface.
void SetCurHopLimit(uint8_t curHopLimit)
Set the current hop limit.
Ipv6InterfaceAddress GetAddress(uint32_t index) const
Get an address from IPv6 interface.
virtual ~Ipv6Interface()
Destructor.
bool AddAddress(Ipv6InterfaceAddress iface)
Add an IPv6 address.
void SetUp()
Enable this interface.
void SetNsDadUid(Ipv6Address address, uint32_t uid)
Update NS DAD packet UID of an interface address.
uint16_t m_retransTimer
Retransmission timer (in millisecond).
void Send(Ptr< Packet > p, const Ipv6Header &hdr, Ipv6Address dest)
Send a packet through this interface.
Ipv6InterfaceAddressList m_addresses
The addresses assigned to this interface.
uint16_t GetRetransTimer() const
Get the retransmission timer.
uint16_t GetMetric() const
Get the metric.
void SetBaseReachableTime(uint16_t baseReachableTime)
Set the base reachable time.
Ptr< NdiscCache > GetNdiscCache() const
Ipv6InterfaceAddress m_linkLocalAddress
The link-local addresses assigned to this interface.
Ptr< Node > m_node
Node associated with this interface.
std::list< std::pair< Ipv6InterfaceAddress, Ipv6Address > >::iterator Ipv6InterfaceAddressListI
Container Iterator for the Ipv6InterfaceAddresses.
uint32_t GetNAddresses(void) const
Get number of addresses on this IPv6 interface.
void SetTrafficControl(Ptr< TrafficControlLayer > tc)
Set the TrafficControlLayer.
void SetMetric(uint16_t metric)
Set the metric.
uint16_t m_baseReachableTime
Base value used for computing the random reachable time value (in millisecond).
Ptr< TrafficControlLayer > m_tc
TrafficControlLayer associated with this interface.
void SetDevice(Ptr< NetDevice > device)
Set the NetDevice.
virtual Ptr< NetDevice > GetDevice() const
Get the NetDevice.
void SetState(Ipv6Address address, Ipv6InterfaceAddress::State_e state)
Update state of an interface address.
IPv6 layer implementation.
static const uint16_t PROT_NUMBER
The protocol number for IPv6 (0x86DD).
Describes an IPv6 prefix.
Definition: ipv6-address.h:456
bool IsMatch(Ipv6Address a, Ipv6Address b) const
If the Address match the type.
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:153
virtual bool IsMulticast(void) const =0
virtual Address GetBroadcast(void) const =0
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)=0
virtual Address GetAddress(void) const =0
virtual Address GetMulticast(Ipv4Address multicastGroup) const =0
Make and return a MAC multicast address using the provided multicast group.
virtual bool NeedsArp(void) const =0
@ PACKET_HOST
Packet addressed oo us.
Definition: net-device.h:298
A base class which provides memory management and object aggregation.
Definition: object.h:88
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#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_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:88
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.