A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-interface.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007-2009 Strasbourg University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
7 */
8
9#include "ipv6-interface.h"
10
11#include "icmpv6-l4-protocol.h"
12#include "ipv6-header.h"
13#include "ipv6-l3-protocol.h"
15#include "loopback-net-device.h"
16#include "ndisc-cache.h"
17
18#include "ns3/iana-ieee802-numbers.h"
19#include "ns3/log.h"
20#include "ns3/mac16-address.h"
21#include "ns3/mac64-address.h"
22#include "ns3/net-device.h"
23#include "ns3/node.h"
24#include "ns3/packet.h"
25#include "ns3/traffic-control-layer.h"
26
27namespace ns3
28{
29
30NS_LOG_COMPONENT_DEFINE("Ipv6Interface");
31
33
36{
37 static TypeId tid =
38 TypeId("ns3::Ipv6Interface")
40 .SetGroupName("Internet")
41 .AddTraceSource("InterfaceStatus",
42 "Interface has been set up or down.",
44 "ns3::Ipv6Address::TracedCallback");
45 return tid;
46}
47
49 : m_ifup(false),
50 m_forwarding(true),
51 m_metric(1),
52 m_node(nullptr),
53 m_device(nullptr),
54 m_tc(nullptr),
55 m_ndCache(nullptr),
60{
61 NS_LOG_FUNCTION(this);
62}
63
67
68void
70{
71 NS_LOG_FUNCTION(this);
72 m_node = nullptr;
73 m_device = nullptr;
74 m_tc = nullptr;
75 m_ndCache = nullptr;
77}
78
79void
81{
82 NS_LOG_FUNCTION(this);
83
84 if (!m_node || !m_device)
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();
95 Ipv6Prefix(64));
96 AddAddress(ifaddr);
97 m_linkLocalAddress = ifaddr;
98 }
99 else
100 {
101 return; /* no NDISC cache for ip6-localhost */
102 }
103 int32_t interfaceId = m_node->GetObject<Ipv6>()->GetInterfaceForDevice(m_device);
106 interfaceId));
107
108 if (icmpv6 && !m_ndCache)
109 {
110 m_ndCache = icmpv6->CreateCache(m_device, this);
111 }
112}
113
114void
116{
117 NS_LOG_FUNCTION(this << node);
118 m_node = node;
119}
120
121void
123{
124 NS_LOG_FUNCTION(this << device);
125 m_device = device;
126}
127
128void
134
137{
138 NS_LOG_FUNCTION(this);
139 return m_device;
140}
141
142void
144{
145 NS_LOG_FUNCTION(this << metric);
146 m_metric = metric;
147}
148
149uint16_t
151{
152 NS_LOG_FUNCTION(this);
153 return m_metric;
154}
155
156bool
158{
159 NS_LOG_FUNCTION(this);
160 return m_ifup;
161}
162
163bool
165{
166 NS_LOG_FUNCTION(this);
167 return !m_ifup;
168}
169
170void
172{
173 NS_LOG_FUNCTION(this);
174
175 if (m_ifup)
176 {
177 return;
178 }
179 DoSetup();
180
181 m_ifup = true;
182
183 Ptr<Ipv6> ip = m_node->GetObject<Ipv6>();
184 NS_ASSERT_MSG(ip, "IPv6 not installed on node.");
185 auto ifIndex = ip->GetInterfaceForDevice(m_device);
186 m_interfaceStatus(m_ifup, ifIndex);
187}
188
189void
191{
192 NS_LOG_FUNCTION(this);
193 m_ifup = false;
194 m_addresses.clear();
195 m_ndCache->Flush();
196
197 Ptr<Ipv6> ip = m_node->GetObject<Ipv6>();
198 NS_ASSERT_MSG(ip, "IPv6 not installed on node.");
199 auto ifIndex = ip->GetInterfaceForDevice(m_device);
200 m_interfaceStatus(m_ifup, ifIndex);
201}
202
203bool
205{
206 NS_LOG_FUNCTION(this);
207 return m_forwarding;
208}
209
210void
212{
213 NS_LOG_FUNCTION(this << forwarding);
214 m_forwarding = forwarding;
215}
216
217bool
219{
220 NS_LOG_FUNCTION(this << iface);
221 Ipv6Address addr = iface.GetAddress();
222
223 /* DAD handling */
224 if (!addr.IsAny())
225 {
226 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
227 {
228 if (it->first.GetAddress() == addr)
229 {
230 return false;
231 }
232 }
233
235 m_addresses.emplace_back(iface, solicited);
236 if (!m_addAddressCallback.IsNull())
237 {
238 m_addAddressCallback(this, addr);
239 }
240
241 if (!addr.IsAny() || !addr.IsLocalhost())
242 {
243 /* DAD handling */
244
245 int32_t interfaceId = m_node->GetObject<Ipv6>()->GetInterfaceForDevice(m_device);
248 interfaceId));
249
250 if (icmpv6)
251 {
252 if (icmpv6->IsAlwaysDad())
253 {
254 Simulator::Schedule(Seconds(0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
255 Simulator::Schedule(icmpv6->GetDadTimeout(),
257 icmpv6,
258 this,
259 addr);
260 }
261 else
262 {
265 icmpv6,
266 this,
267 addr);
268 }
269 }
270 }
271 return true;
272 }
273
274 /* bad address */
275 return false;
276}
277
280{
281 /* IPv6 interface has always at least one IPv6 link-local address */
282 NS_LOG_FUNCTION(this);
283
284 return m_linkLocalAddress;
285}
286
287bool
289{
290 /* IPv6 interface has always at least one IPv6 Solicited Multicast address */
291 NS_LOG_FUNCTION(this << address);
292
293 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
294 {
295 if (it->second == address)
296 {
297 return true;
298 }
299 }
300
301 return false;
302}
303
306{
307 NS_LOG_FUNCTION(this << index);
308 uint32_t i = 0;
309
310 if (m_addresses.size() > index)
311 {
312 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
313 {
314 if (i == index)
315 {
316 return it->first;
317 }
318 i++;
319 }
320 }
321 else
322 {
323 NS_FATAL_ERROR("index " << index << " out of bounds");
324 }
326 return addr; /* quiet compiler */
327}
328
331{
332 NS_LOG_FUNCTION(this);
333 return m_addresses.size();
334}
335
338{
339 NS_LOG_FUNCTION(this << index);
340 uint32_t i = 0;
341
342 if (m_addresses.size() < index)
343 {
344 NS_FATAL_ERROR("Removing index that does not exist in Ipv6Interface::RemoveAddress");
345 }
346
347 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
348 {
349 if (i == index)
350 {
351 Ipv6InterfaceAddress iface = it->first;
352 m_addresses.erase(it);
353 if (!m_removeAddressCallback.IsNull())
354 {
355 m_removeAddressCallback(this, iface);
356 }
357 return iface;
358 }
359
360 i++;
361 }
362 NS_FATAL_ERROR("Address " << index << " not found");
364 return addr; /* quiet compiler */
365}
366
369{
370 NS_LOG_FUNCTION(this << address);
371
372 if (address == Ipv6Address::GetLoopback())
373 {
374 NS_LOG_WARN("Cannot remove loopback address.");
375 return Ipv6InterfaceAddress();
376 }
377
378 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
379 {
380 if (it->first.GetAddress() == address)
381 {
382 Ipv6InterfaceAddress iface = it->first;
383 m_addresses.erase(it);
384 if (!m_removeAddressCallback.IsNull())
385 {
386 m_removeAddressCallback(this, iface);
387 }
388 return iface;
389 }
390 }
391 return Ipv6InterfaceAddress();
392}
393
396{
397 NS_LOG_FUNCTION(this << dst);
398
399 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
400 {
401 Ipv6InterfaceAddress ifaddr = it->first;
402
403 if (ifaddr.GetPrefix().IsMatch(ifaddr.GetAddress(), dst))
404 {
405 return ifaddr;
406 }
407 }
408
409 /* NS_ASSERT_MSG (false, "Not matching address."); */
411 return ret; /* quiet compiler */
412}
413
414void
416{
417 NS_LOG_FUNCTION(this << p << dest);
418
419 if (!IsUp())
420 {
421 return;
422 }
423
424 Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol>();
425
426 /* check if destination is localhost (::1), if yes we don't pass through
427 * traffic control layer */
429 {
430 /** @todo additional checks needed here (such as whether multicast
431 * goes to loopback)?
432 */
433 p->AddHeader(hdr);
434 m_device->Send(p, m_device->GetBroadcast(), iana::ieee802numbers::IPV6);
435 return;
436 }
437
439
440 /* check if destination is for one of our interface */
441 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
442 {
443 if (dest == it->first.GetAddress())
444 {
445 p->AddHeader(hdr);
447 m_tc,
448 m_device,
449 p,
451 m_device->GetBroadcast(),
452 m_device->GetBroadcast(),
454 return;
455 }
456 }
457
458 /* other address */
459 if (m_device->NeedsArp())
460 {
461 NS_LOG_LOGIC("Needs NDISC " << dest);
462
463 int32_t interfaceId = m_node->GetObject<Ipv6>()->GetInterfaceForDevice(m_device);
466 interfaceId));
467
468 Address hardwareDestination;
469 bool found = false;
470
471 NS_ASSERT(icmpv6);
472
473 if (dest.IsMulticast())
474 {
475 NS_LOG_LOGIC("IsMulticast");
477 m_device->IsMulticast(),
478 "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
479
480 hardwareDestination = m_device->GetMulticast(dest);
481 found = true;
482 }
483 else
484 {
485 NS_LOG_LOGIC("NDISC Lookup");
486 found = icmpv6->Lookup(p, hdr, dest, GetDevice(), m_ndCache, &hardwareDestination);
487 }
488
489 if (found)
490 {
491 NS_LOG_LOGIC("Address Resolved. Send.");
492 m_tc->Send(
493 m_device,
494 Create<Ipv6QueueDiscItem>(p, hardwareDestination, iana::ieee802numbers::IPV6, hdr));
495 }
496 }
497 else
498 {
499 NS_LOG_LOGIC("Doesn't need NDISC");
500 m_tc->Send(m_device,
502 m_device->GetBroadcast(),
504 hdr));
505 }
506}
507
508void
510{
511 NS_LOG_FUNCTION(this << curHopLimit);
512 m_curHopLimit = curHopLimit;
513}
514
515uint8_t
517{
518 NS_LOG_FUNCTION(this);
519 return m_curHopLimit;
520}
521
522void
523Ipv6Interface::SetBaseReachableTime(uint16_t baseReachableTime)
524{
525 NS_LOG_FUNCTION(this << baseReachableTime);
526 m_baseReachableTime = baseReachableTime;
527}
528
529uint16_t
535
536void
537Ipv6Interface::SetReachableTime(uint16_t reachableTime)
538{
539 NS_LOG_FUNCTION(this << reachableTime);
540 m_reachableTime = reachableTime;
541}
542
543uint16_t
545{
546 NS_LOG_FUNCTION(this);
547 return m_reachableTime;
548}
549
550void
551Ipv6Interface::SetRetransTimer(uint16_t retransTimer)
552{
553 NS_LOG_FUNCTION(this << retransTimer);
554 m_retransTimer = retransTimer;
555}
556
557uint16_t
559{
560 NS_LOG_FUNCTION(this);
561 return m_retransTimer;
562}
563
564void
566{
567 NS_LOG_FUNCTION(this << address << state);
568
569 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
570 {
571 if (it->first.GetAddress() == address)
572 {
573 it->first.SetState(state);
574 return;
575 }
576 }
577 /* not found, maybe address has expired */
578}
579
580void
582{
583 NS_LOG_FUNCTION(this << address << uid);
584
585 for (auto it = m_addresses.begin(); it != m_addresses.end(); ++it)
586 {
587 if (it->first.GetAddress() == address)
588 {
589 it->first.SetNsDadUid(uid);
590 return;
591 }
592 }
593 /* not found, maybe address has expired */
594}
595
598{
599 NS_LOG_FUNCTION(this);
600 return m_ndCache;
601}
602
603void
605 Callback<void, Ptr<Ipv6Interface>, Ipv6InterfaceAddress> removeAddressCallback)
606{
607 NS_LOG_FUNCTION(this << &removeAddressCallback);
608 m_removeAddressCallback = removeAddressCallback;
609}
610
611void
613 Callback<void, Ptr<Ipv6Interface>, Ipv6InterfaceAddress> addAddressCallback)
614{
615 NS_LOG_FUNCTION(this << &addAddressCallback);
616 m_addAddressCallback = addAddressCallback;
617}
618
619} /* namespace ns3 */
a polymophic address class
Definition address.h:114
Callback template class.
Definition callback.h:428
virtual 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.
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.
static Ipv6Address GetLoopback()
Get the loopback address.
Packet header for IPv6.
Definition ipv6-header.h:24
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition ipv6.h:71
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.
The IPv6 representation of a network 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 ?
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.
void RemoveAddressCallback(Callback< void, Ptr< Ipv6Interface >, Ipv6InterfaceAddress > removeAddressCallback)
This callback is set when an address is removed from an interface with auto-generated Ndisc cache and...
uint16_t GetReachableTime() const
Get the reachable time.
bool IsUp() const
Is the interface UP ?
bool m_forwarding
Forwarding state.
ns3::TracedCallback< bool, int32_t > m_interfaceStatus
The trace fired when the interface state changes.
uint16_t m_metric
The metric.
uint16_t m_reachableTime
Reachable time (in millisecond).
uint32_t GetNAddresses() const
Get number of addresses on this IPv6 interface.
void DoDispose() override
Dispose this object.
Callback< void, Ptr< Ipv6Interface >, Ipv6InterfaceAddress > m_removeAddressCallback
remove address callback
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.
~Ipv6Interface() override
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.
Callback< void, Ptr< Ipv6Interface >, Ipv6InterfaceAddress > m_addAddressCallback
add address callback
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 AddAddressCallback(Callback< void, Ptr< Ipv6Interface >, Ipv6InterfaceAddress > addAddressCallback)
This callback is set when an address is added from an interface with auto-generated Ndisc cache and i...
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.
Describes an IPv6 prefix.
bool IsMatch(Ipv6Address a, Ipv6Address b) const
Check whether two addresses have the same bits in the prefix portion of their addresses.
@ PACKET_HOST
Packet addressed to us.
Definition net-device.h:315
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
virtual void DoDispose()
Destructor implementation.
Definition object.cc:431
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:614
virtual void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Called by NetDevices, incoming packet.
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:76
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:281
#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:260
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:492
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
constexpr uint16_t IPV6
IPv6 EtherType [RFC2464].
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:643