A Discrete-Event Network Simulator
API
radvd.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 Telecom Bretagne
4  * Copyright (c) 2009 Strasbourg University
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
20  * Mehdi Benamor <benamor.mehdi@ensi.rnu.tn>
21  */
22 
23 #include "ns3/log.h"
24 #include "ns3/abort.h"
25 #include "ns3/ipv6-address.h"
26 #include "ns3/nstime.h"
27 #include "ns3/simulator.h"
28 #include "ns3/packet.h"
29 #include "ns3/net-device.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/inet6-socket-address.h"
32 #include "ns3/ipv6.h"
33 #include "ns3/ipv6-l3-protocol.h"
34 #include "ns3/ipv6-interface.h"
35 #include "ns3/ipv6-raw-socket-factory.h"
36 #include "ns3/ipv6-packet-info-tag.h"
37 #include "ns3/ipv6-header.h"
38 #include "ns3/icmpv6-header.h"
39 #include "ns3/string.h"
40 #include "ns3/pointer.h"
41 
42 #include "radvd.h"
43 
44 namespace ns3
45 {
46 
47 NS_LOG_COMPONENT_DEFINE ("RadvdApplication");
48 
50 
52 {
53  static TypeId tid = TypeId ("ns3::Radvd")
55  .AddConstructor<Radvd> ()
56  .AddAttribute ("AdvertisementJitter",
57  "Uniform variable to provide jitter between min and max values of AdvInterval",
58  StringValue("ns3::UniformRandomVariable"),
60  MakePointerChecker<UniformRandomVariable> ());
61  ;
62  return tid;
63 }
64 
66 {
67  NS_LOG_FUNCTION (this);
68 }
69 
71 {
72  NS_LOG_FUNCTION (this);
73  for (RadvdInterfaceListI it = m_configurations.begin (); it != m_configurations.end (); ++it)
74  {
75  *it = 0;
76  }
77  m_configurations.clear ();
78  m_recvSocket = 0;
79 }
80 
82 {
83  NS_LOG_FUNCTION (this);
84 
85  m_recvSocket->Close ();
86  m_recvSocket = 0;
87 
88  for (SocketMapI it = m_sendSockets.begin (); it != m_sendSockets.end (); ++it)
89  {
90  it->second->Close ();
91  it->second = 0;
92  }
93 
95 }
96 
98 {
99  NS_LOG_FUNCTION (this);
100 
101  TypeId tid = TypeId::LookupByName ("ns3::Ipv6RawSocketFactory");
102 
103  if (!m_recvSocket)
104  {
106 
108 
114  }
115 
116  for (RadvdInterfaceListCI it = m_configurations.begin (); it != m_configurations.end (); it++)
117  {
118  if ((*it)->IsSendAdvert ())
119  {
120  m_unsolicitedEventIds[(*it)->GetInterface ()] = Simulator::Schedule (Seconds (0.), &Radvd::Send,
121  this, (*it), Ipv6Address::GetAllNodesMulticast (), true);
122  }
123 
124  if (m_sendSockets.find ((*it)->GetInterface ()) == m_sendSockets.end ())
125  {
127  Ptr<Ipv6Interface> iFace = ipv6->GetInterface ((*it)->GetInterface ());
128 
129  m_sendSockets[(*it)->GetInterface ()] = Socket::CreateSocket (GetNode (), tid);
130  m_sendSockets[(*it)->GetInterface ()]->Bind (Inet6SocketAddress (iFace->GetLinkLocalAddress ().GetAddress (), 0));
131  m_sendSockets[(*it)->GetInterface ()]->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
132  m_sendSockets[(*it)->GetInterface ()]->ShutdownRecv ();
133  }
134  }
135 }
136 
138 {
139  NS_LOG_FUNCTION (this);
140 
141  if (m_recvSocket)
142  {
144  }
145 
146  for (EventIdMapI it = m_unsolicitedEventIds.begin (); it != m_unsolicitedEventIds.end (); ++it)
147  {
148  Simulator::Cancel ((*it).second);
149  }
150  m_unsolicitedEventIds.clear ();
151 
152  for (EventIdMapI it = m_solicitedEventIds.begin (); it != m_solicitedEventIds.end (); ++it)
153  {
154  Simulator::Cancel ((*it).second);
155  }
156  m_solicitedEventIds.clear ();
157 }
158 
160 {
161  NS_LOG_FUNCTION (this << routerInterface);
162  m_configurations.push_back (routerInterface);
163 }
164 
165 int64_t
166 Radvd:: AssignStreams (int64_t stream)
167 {
168  NS_LOG_FUNCTION (this << stream);
169  m_jitter->SetStream (stream);
170  return 1;
171 }
172 
173 void Radvd::Send (Ptr<RadvdInterface> config, Ipv6Address dst, bool reschedule)
174 {
175  NS_LOG_FUNCTION (this << dst << reschedule);
176 
177  if (reschedule == true)
178  {
179  config->SetLastRaTxTime (Simulator::Now ());
180  }
181 
182  Icmpv6RA raHdr;
184  Icmpv6OptionMtu mtuHdr;
186 
187  std::list<Ptr<RadvdPrefix> > prefixes = config->GetPrefixes ();
188  Ptr<Packet> p = Create<Packet> ();
189  Ptr<Ipv6> ipv6 = GetNode ()->GetObject<Ipv6> ();
190 
191  /* set RA header information */
192  raHdr.SetFlagM (config->IsManagedFlag ());
193  raHdr.SetFlagO (config->IsOtherConfigFlag ());
194  raHdr.SetFlagH (config->IsHomeAgentFlag ());
195  raHdr.SetCurHopLimit (config->GetCurHopLimit ());
196  raHdr.SetLifeTime (config->GetDefaultLifeTime ());
197  raHdr.SetReachableTime (config->GetReachableTime ());
198  raHdr.SetRetransmissionTime (config->GetRetransTimer ());
199 
200  if (config->IsSourceLLAddress ())
201  {
202  /* Get L2 address from NetDevice */
203  Address addr = ipv6->GetNetDevice (config->GetInterface ())->GetAddress ();
204  llaHdr = Icmpv6OptionLinkLayerAddress (true, addr);
205  p->AddHeader (llaHdr);
206  }
207 
208  if (config->GetLinkMtu ())
209  {
210  NS_ASSERT (config->GetLinkMtu () >= 1280);
211  mtuHdr = Icmpv6OptionMtu (config->GetLinkMtu ());
212  p->AddHeader (mtuHdr);
213  }
214 
215  /* add list of prefixes */
216  for (std::list<Ptr<RadvdPrefix> >::const_iterator jt = prefixes.begin (); jt != prefixes.end (); jt++)
217  {
218  uint8_t flags = 0;
219  prefixHdr = Icmpv6OptionPrefixInformation ();
220  prefixHdr.SetPrefix ((*jt)->GetNetwork ());
221  prefixHdr.SetPrefixLength ((*jt)->GetPrefixLength ());
222  prefixHdr.SetValidTime ((*jt)->GetValidLifeTime ());
223  prefixHdr.SetPreferredTime ((*jt)->GetPreferredLifeTime ());
224 
225  if ((*jt)->IsOnLinkFlag ())
226  {
227  flags += 1 << 7;
228  }
229 
230  if ((*jt)->IsAutonomousFlag ())
231  {
232  flags += 1 << 6;
233  }
234 
235  if ((*jt)->IsRouterAddrFlag ())
236  {
237  flags += 1 << 5;
238  }
239 
240  prefixHdr.SetFlags (flags);
241 
242  p->AddHeader (prefixHdr);
243  }
244 
245  Address sockAddr;
246  m_sendSockets[config->GetInterface ()]->GetSockName (sockAddr);
248 
249  /* as we know interface index that will be used to send RA and
250  * we always send RA with router's link-local address, we can
251  * calculate checksum here.
252  */
253  raHdr.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + raHdr.GetSerializedSize (), 58 /* ICMPv6 */);
254  p->AddHeader (raHdr);
255 
256  /* Router advertisements MUST always have a ttl of 255
257  * The ttl value should be set as a socket option, but this is not yet implemented
258  */
259  SocketIpTtlTag ttl;
260  ttl.SetTtl (255);
261  p->AddPacketTag (ttl);
262 
263  /* send RA */
264  NS_LOG_LOGIC ("Send RA to " << dst);
265  m_sendSockets[config->GetInterface ()]->SendTo (p, 0, Inet6SocketAddress (dst, 0));
266 
267  if (reschedule)
268  {
269  uint64_t delay = static_cast<uint64_t> (m_jitter->GetValue (config->GetMinRtrAdvInterval (), config->GetMaxRtrAdvInterval ()) + 0.5);
270  if (config->IsInitialRtrAdv ())
271  {
274  }
275 
276  NS_LOG_INFO ("Reschedule in " << delay << " milliseconds");
277  Time t = MilliSeconds (delay);
279  }
280 }
281 
283 {
284  NS_LOG_FUNCTION (this << socket);
285  Ptr<Packet> packet = 0;
286  Address from;
287 
288  while ((packet = socket->RecvFrom (from)))
289  {
291  {
292  Ipv6PacketInfoTag interfaceInfo;
293  if (!packet->RemovePacketTag (interfaceInfo))
294  {
295  NS_ABORT_MSG ("No incoming interface on RADVD message, aborting.");
296  }
297  uint32_t incomingIf = interfaceInfo.GetRecvIf ();
298  Ptr<NetDevice> dev = GetNode ()->GetDevice (incomingIf);
299  Ptr<Ipv6> ipv6 = GetNode ()->GetObject<Ipv6> ();
300  uint32_t ipInterfaceIndex = ipv6->GetInterfaceForDevice (dev);
301 
302  Ipv6Header hdr;
303  Icmpv6RS rsHdr;
304  uint64_t delay = 0;
305  Time t;
306 
307  packet->RemoveHeader (hdr);
308  uint8_t type;
309  packet->CopyData (&type, sizeof(type));
310 
311  switch (type)
312  {
314  packet->RemoveHeader (rsHdr);
315  NS_LOG_INFO ("Received ICMPv6 Router Solicitation from " << hdr.GetSourceAddress () << " code = " << (uint32_t)rsHdr.GetCode ());
316 
317  for (RadvdInterfaceListCI it = m_configurations.begin (); it != m_configurations.end (); it++)
318  {
319  if (ipInterfaceIndex == (*it)->GetInterface ())
320  {
321  /* calculate minimum delay between RA */
322  delay = static_cast<uint64_t> (m_jitter->GetValue (0, MAX_RA_DELAY_TIME) + 0.5);
323  t = Simulator::Now () + MilliSeconds (delay); /* absolute time of solicited RA */
324 
325  if (Simulator::Now () < (*it)->GetLastRaTxTime () + MilliSeconds (MIN_DELAY_BETWEEN_RAS) )
326  {
328  }
329 
330  /* if our solicited RA is before the next periodic RA, we schedule it */
331  bool scheduleSingle = true;
332 
333  if (m_solicitedEventIds.find ((*it)->GetInterface ()) != m_solicitedEventIds.end ())
334  {
335  if (m_solicitedEventIds[(*it)->GetInterface ()].IsRunning ())
336  {
337  scheduleSingle = false;
338  }
339  }
340 
341  if (m_unsolicitedEventIds.find ((*it)->GetInterface ()) != m_unsolicitedEventIds.end ())
342  {
343  if (t.GetTimeStep () > static_cast<int64_t> (m_unsolicitedEventIds[(*it)->GetInterface ()].GetTs ()))
344  {
345  scheduleSingle = false;
346  }
347  }
348 
349  if (scheduleSingle)
350  {
351  NS_LOG_INFO ("schedule new RA");
352  m_solicitedEventIds[(*it)->GetInterface ()] = Simulator::Schedule (MilliSeconds (delay), &Radvd::Send,
353  this, (*it), Ipv6Address::GetAllNodesMulticast (), false);
354  }
355  }
356  }
357  break;
358  default:
359  break;
360  }
361  }
362  }
363 }
364 
365 } /* namespace ns3 */
366 
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
RadvdPrefixList GetPrefixes() const
Get list of prefixes advertised for this interface.
void CalculatePseudoHeaderChecksum(Ipv6Address src, Ipv6Address dst, uint16_t length, uint8_t protocol)
Calculate pseudo header checksum for IPv6.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
uint32_t GetMinRtrAdvInterval() const
Get minimum RA interval.
Introspection did not find any typical Config paths.
Definition: ipv6-header.h:33
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 SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
uint32_t GetLinkMtu() const
Get link MTU.
Introspection did not find any typical Config paths.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
virtual void StartApplication()
Start the application.
Definition: radvd.cc:97
RadvdInterfaceList m_configurations
List of configuration for interface.
Definition: radvd.h:164
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:80
Hold variables of type string.
Definition: string.h:41
uint32_t GetInterface() const
Get interface index for this configuration.
std::list< Ptr< RadvdInterface > >::const_iterator RadvdInterfaceListCI
Container Const Iterator: Ptr to RadvdInterface.
Definition: radvd.h:111
virtual int ShutdownSend(void)=0
IPv6 layer implementation.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:836
Introspection did not find any typical Config paths.
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:867
std::list< Ptr< RadvdInterface > >::iterator RadvdInterfaceListI
Container Iterator: Ptr to RadvdInterface.
Definition: radvd.h:109
uint32_t GetRetransTimer() const
Get retransmission timer.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
void SetRetransmissionTime(uint32_t r)
Set the node Retransmission time (Neighbor Discovery).
std::map< uint32_t, Ptr< Socket > >::iterator SocketMapI
Container Iterator: interface number, Socket.
Definition: radvd.h:123
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:311
Callback< R > MakeNullCallback(void)
Definition: callback.h:1436
SocketMap m_sendSockets
Raw socket to send RA.
Definition: radvd.h:159
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:819
uint8_t GetCurHopLimit() const
Get current hop limit.
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer...
Definition: socket.h:999
a polymophic address class
Definition: address.h:90
bool IsManagedFlag() const
Is managed flag enabled ?
void SetLifeTime(uint16_t l)
Set the node Life time (Neighbor Discovery).
void SetValidTime(uint32_t validTime)
Set the valid time of the information.
void SetRecvPktInfo(bool flag)
Enable/Disable receive packet information to socket.
Definition: socket.cc:357
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: radvd.cc:166
virtual ~Radvd()
Destructor.
Definition: radvd.cc:70
void SetCurHopLimit(uint8_t m)
Set the IPv6 maximum number of jumps.
void SetPreferredTime(uint32_t preferredTime)
Set the preferred time of the information.
bool IsInitialRtrAdv()
Checks if the interface is subject to the initial Rtr Advertisements rule.
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:210
The base class for all ns3 applications.
Definition: application.h:60
void SetTtl(uint8_t ttl)
Set the tag's TTL.
Definition: socket.cc:582
bool IsOtherConfigFlag() const
Is "other config" flag enabled ?
Hold an unsigned integer type.
Definition: uinteger.h:44
void SetPrefixLength(uint8_t prefixLength)
Set the prefix length.
Introspection did not find any typical Config paths.
EventIdMap m_unsolicitedEventIds
Event ID map for unsolicited RAs.
Definition: radvd.h:169
Ptr< Node > GetNode() const
Definition: application.cc:103
An Inet6 address class.
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:134
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1290
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:70
static const uint32_t MIN_DELAY_BETWEEN_RAS
Default value for minimum delay between RA advertisements (ms)
Definition: radvd.h:81
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
#define list
virtual void DoDispose()
Dispose the instance.
Definition: radvd.cc:81
void HandleRead(Ptr< Socket > socket)
Handle received packet, especially router solicitation.
Definition: radvd.cc:282
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:82
static const uint32_t MAX_RA_DELAY_TIME
Default value for maximum delay of RA (ms)
Definition: radvd.h:69
void SetLastRaTxTime(Time now)
Set the last RA send time.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Introspection did not find any typical Config paths.
uint8_t GetCode() const
Get the code field.
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
std::map< uint32_t, EventId >::iterator EventIdMapI
Container Iterator: interface number, EventId.
Definition: radvd.h:116
EventIdMap m_solicitedEventIds
Event ID map for solicited RAs.
Definition: radvd.h:174
static Ipv6Address GetAllNodesMulticast()
Get the "all nodes multicast" address.
int64_t GetTimeStep(void) const
Definition: nstime.h:357
void SetFlagO(bool o)
Set the O flag.
void SetFlagH(bool h)
Set the H flag.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
void Send(Ptr< RadvdInterface > config, Ipv6Address dst=Ipv6Address::GetAllNodesMulticast(), bool reschedule=false)
Send a packet.
Definition: radvd.cc:173
virtual void StopApplication()
Stop the application.
Definition: radvd.cc:137
void SetFlagM(bool m)
Set the M flag.
void SetFlags(uint8_t flags)
Set the flags.
void SetReachableTime(uint32_t r)
Set the node Reachable time (Neighbor Discovery).
Describes an IPv6 address.
Definition: ipv6-address.h:47
Radvd()
Constructor.
Definition: radvd.cc:65
void AddConfiguration(Ptr< RadvdInterface > routerInterface)
Add configuration for an interface;.
Definition: radvd.cc:159
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:843
Ptr< UniformRandomVariable > m_jitter
Variable to provide jitter in advertisement interval.
Definition: radvd.h:179
Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:101
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:859
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
uint32_t GetRecvIf(void) const
Get the tag's receiving interface.
virtual uint32_t GetSerializedSize() const
Get the serialized size.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:368
This class implements a tag that carries socket ancillary data to the socket interface.
uint32_t GetMaxRtrAdvInterval() const
Get maximum RA interval.
uint32_t GetDefaultLifeTime() const
Get default lifetime.
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
Ptr< Socket > m_recvSocket
Raw socket to receive RS.
Definition: radvd.h:154
bool IsSourceLLAddress() const
Is source LLA option should be included in RA ?
static TypeId GetTypeId(void)
Get the type ID.
Definition: radvd.cc:51
virtual int Close(void)=0
Close a socket.
static const uint32_t MAX_INITIAL_RTR_ADVERT_INTERVAL
Default value for maximum initial RA advertisements interval (ms)
Definition: radvd.h:77
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:190
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void SetPrefix(Ipv6Address prefix)
Set the IPv6 prefix.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
uint32_t GetReachableTime() const
Get reachable time.
bool IsHomeAgentFlag() const
Is "home agent" flag enabled ?
static TypeId LookupByName(std::string name)
Definition: type-id.cc:556
static Ipv6Address GetAllRoutersMulticast()
Get the "all routers multicast" address.