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 "radvd.h"
24 #include "ns3/log.h"
25 #include "ns3/abort.h"
26 #include "ns3/ipv6-address.h"
27 #include "ns3/nstime.h"
28 #include "ns3/simulator.h"
29 #include "ns3/packet.h"
30 #include "ns3/net-device.h"
31 #include "ns3/uinteger.h"
32 #include "ns3/inet6-socket-address.h"
33 #include "ns3/ipv6.h"
34 #include "ns3/ipv6-l3-protocol.h"
35 #include "ns3/ipv6-interface.h"
36 #include "ns3/ipv6-raw-socket-factory.h"
37 #include "ns3/ipv6-packet-info-tag.h"
38 #include "ns3/ipv6-header.h"
39 #include "ns3/icmpv6-header.h"
40 #include "ns3/string.h"
41 #include "ns3/pointer.h"
42 
43 
44 namespace ns3
45 {
46 
47 NS_LOG_COMPONENT_DEFINE ("RadvdApplication");
48 
50 
52 {
53  static TypeId tid = TypeId ("ns3::Radvd")
55  .SetGroupName("Internet-Apps")
56  .AddConstructor<Radvd> ()
57  .AddAttribute ("AdvertisementJitter",
58  "Uniform variable to provide jitter between min and max values of AdvInterval",
59  StringValue("ns3::UniformRandomVariable"),
61  MakePointerChecker<UniformRandomVariable> ());
62  ;
63  return tid;
64 }
65 
67 {
68  NS_LOG_FUNCTION (this);
69 }
70 
72 {
73  NS_LOG_FUNCTION (this);
74  for (RadvdInterfaceListI it = m_configurations.begin (); it != m_configurations.end (); ++it)
75  {
76  *it = 0;
77  }
78  m_configurations.clear ();
79  m_recvSocket = 0;
80 }
81 
83 {
84  NS_LOG_FUNCTION (this);
85 
86  m_recvSocket->Close ();
87  m_recvSocket = 0;
88 
89  for (SocketMapI it = m_sendSockets.begin (); it != m_sendSockets.end (); ++it)
90  {
91  it->second->Close ();
92  it->second = 0;
93  }
94 
96 }
97 
99 {
100  NS_LOG_FUNCTION (this);
101 
102  TypeId tid = TypeId::LookupByName ("ns3::Ipv6RawSocketFactory");
103 
104  if (!m_recvSocket)
105  {
107 
109 
115  }
116 
117  for (RadvdInterfaceListCI it = m_configurations.begin (); it != m_configurations.end (); it++)
118  {
119  if ((*it)->IsSendAdvert ())
120  {
121  m_unsolicitedEventIds[(*it)->GetInterface ()] = Simulator::Schedule (Seconds (0.), &Radvd::Send,
122  this, (*it), Ipv6Address::GetAllNodesMulticast (), true);
123  }
124 
125  if (m_sendSockets.find ((*it)->GetInterface ()) == m_sendSockets.end ())
126  {
128  Ptr<Ipv6Interface> iFace = ipv6->GetInterface ((*it)->GetInterface ());
129 
130  m_sendSockets[(*it)->GetInterface ()] = Socket::CreateSocket (GetNode (), tid);
131  m_sendSockets[(*it)->GetInterface ()]->Bind (Inet6SocketAddress (iFace->GetLinkLocalAddress ().GetAddress (), 0));
132  m_sendSockets[(*it)->GetInterface ()]->SetAttribute ("Protocol", UintegerValue (Ipv6Header::IPV6_ICMPV6));
133  m_sendSockets[(*it)->GetInterface ()]->ShutdownRecv ();
134  }
135  }
136 }
137 
139 {
140  NS_LOG_FUNCTION (this);
141 
142  if (m_recvSocket)
143  {
145  }
146 
147  for (EventIdMapI it = m_unsolicitedEventIds.begin (); it != m_unsolicitedEventIds.end (); ++it)
148  {
149  Simulator::Cancel ((*it).second);
150  }
151  m_unsolicitedEventIds.clear ();
152 
153  for (EventIdMapI it = m_solicitedEventIds.begin (); it != m_solicitedEventIds.end (); ++it)
154  {
155  Simulator::Cancel ((*it).second);
156  }
157  m_solicitedEventIds.clear ();
158 }
159 
161 {
162  NS_LOG_FUNCTION (this << routerInterface);
163  m_configurations.push_back (routerInterface);
164 }
165 
166 int64_t
167 Radvd:: AssignStreams (int64_t stream)
168 {
169  NS_LOG_FUNCTION (this << stream);
170  m_jitter->SetStream (stream);
171  return 1;
172 }
173 
174 void Radvd::Send (Ptr<RadvdInterface> config, Ipv6Address dst, bool reschedule)
175 {
176  NS_LOG_FUNCTION (this << dst << reschedule);
177 
178  if (reschedule == true)
179  {
180  config->SetLastRaTxTime (Simulator::Now ());
181  }
182 
183  Icmpv6RA raHdr;
185  Icmpv6OptionMtu mtuHdr;
187 
188  std::list<Ptr<RadvdPrefix> > prefixes = config->GetPrefixes ();
189  Ptr<Packet> p = Create<Packet> ();
190  Ptr<Ipv6> ipv6 = GetNode ()->GetObject<Ipv6> ();
191 
192  /* set RA header information */
193  raHdr.SetFlagM (config->IsManagedFlag ());
194  raHdr.SetFlagO (config->IsOtherConfigFlag ());
195  raHdr.SetFlagH (config->IsHomeAgentFlag ());
196  raHdr.SetCurHopLimit (config->GetCurHopLimit ());
197  raHdr.SetLifeTime (config->GetDefaultLifeTime ());
198  raHdr.SetReachableTime (config->GetReachableTime ());
199  raHdr.SetRetransmissionTime (config->GetRetransTimer ());
200 
201  if (config->IsSourceLLAddress ())
202  {
203  /* Get L2 address from NetDevice */
204  Address addr = ipv6->GetNetDevice (config->GetInterface ())->GetAddress ();
205  llaHdr = Icmpv6OptionLinkLayerAddress (true, addr);
206  p->AddHeader (llaHdr);
207  }
208 
209  if (config->GetLinkMtu ())
210  {
211  NS_ASSERT (config->GetLinkMtu () >= 1280);
212  mtuHdr = Icmpv6OptionMtu (config->GetLinkMtu ());
213  p->AddHeader (mtuHdr);
214  }
215 
216  /* add list of prefixes */
217  for (std::list<Ptr<RadvdPrefix> >::const_iterator jt = prefixes.begin (); jt != prefixes.end (); jt++)
218  {
219  uint8_t flags = 0;
220  prefixHdr = Icmpv6OptionPrefixInformation ();
221  prefixHdr.SetPrefix ((*jt)->GetNetwork ());
222  prefixHdr.SetPrefixLength ((*jt)->GetPrefixLength ());
223  prefixHdr.SetValidTime ((*jt)->GetValidLifeTime ());
224  prefixHdr.SetPreferredTime ((*jt)->GetPreferredLifeTime ());
225 
226  if ((*jt)->IsOnLinkFlag ())
227  {
228  flags += 1 << 7;
229  }
230 
231  if ((*jt)->IsAutonomousFlag ())
232  {
233  flags += 1 << 6;
234  }
235 
236  if ((*jt)->IsRouterAddrFlag ())
237  {
238  flags += 1 << 5;
239  }
240 
241  prefixHdr.SetFlags (flags);
242 
243  p->AddHeader (prefixHdr);
244  }
245 
246  Address sockAddr;
247  m_sendSockets[config->GetInterface ()]->GetSockName (sockAddr);
249 
250  /* as we know interface index that will be used to send RA and
251  * we always send RA with router's link-local address, we can
252  * calculate checksum here.
253  */
254  raHdr.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + raHdr.GetSerializedSize (), 58 /* ICMPv6 */);
255  p->AddHeader (raHdr);
256 
257  /* Router advertisements MUST always have a ttl of 255
258  * The ttl value should be set as a socket option, but this is not yet implemented
259  */
260  SocketIpTtlTag ttl;
261  ttl.SetTtl (255);
262  p->AddPacketTag (ttl);
263 
264  /* send RA */
265  NS_LOG_LOGIC ("Send RA to " << dst);
266  m_sendSockets[config->GetInterface ()]->SendTo (p, 0, Inet6SocketAddress (dst, 0));
267 
268  if (reschedule)
269  {
270  uint64_t delay = static_cast<uint64_t> (m_jitter->GetValue (config->GetMinRtrAdvInterval (), config->GetMaxRtrAdvInterval ()) + 0.5);
271  if (config->IsInitialRtrAdv ())
272  {
275  }
276 
277  NS_LOG_INFO ("Reschedule in " << delay << " milliseconds");
278  Time t = MilliSeconds (delay);
280  }
281 }
282 
284 {
285  NS_LOG_FUNCTION (this << socket);
286  Ptr<Packet> packet = 0;
287  Address from;
288 
289  while ((packet = socket->RecvFrom (from)))
290  {
292  {
293  Ipv6PacketInfoTag interfaceInfo;
294  if (!packet->RemovePacketTag (interfaceInfo))
295  {
296  NS_ABORT_MSG ("No incoming interface on RADVD message, aborting.");
297  }
298  uint32_t incomingIf = interfaceInfo.GetRecvIf ();
299  Ptr<NetDevice> dev = GetNode ()->GetDevice (incomingIf);
300  Ptr<Ipv6> ipv6 = GetNode ()->GetObject<Ipv6> ();
301  uint32_t ipInterfaceIndex = ipv6->GetInterfaceForDevice (dev);
302 
303  Ipv6Header hdr;
304  Icmpv6RS rsHdr;
305  uint64_t delay = 0;
306  Time t;
307 
308  packet->RemoveHeader (hdr);
309  uint8_t type;
310  packet->CopyData (&type, sizeof(type));
311 
312  switch (type)
313  {
315  packet->RemoveHeader (rsHdr);
316  NS_LOG_INFO ("Received ICMPv6 Router Solicitation from " << hdr.GetSourceAddress () << " code = " << (uint32_t)rsHdr.GetCode ());
317 
318  for (RadvdInterfaceListCI it = m_configurations.begin (); it != m_configurations.end (); it++)
319  {
320  if (ipInterfaceIndex == (*it)->GetInterface ())
321  {
322  /* calculate minimum delay between RA */
323  delay = static_cast<uint64_t> (m_jitter->GetValue (0, MAX_RA_DELAY_TIME) + 0.5);
324  t = Simulator::Now () + MilliSeconds (delay); /* absolute time of solicited RA */
325 
326  if (Simulator::Now () < (*it)->GetLastRaTxTime () + MilliSeconds (MIN_DELAY_BETWEEN_RAS) )
327  {
329  }
330 
331  /* if our solicited RA is before the next periodic RA, we schedule it */
332  bool scheduleSingle = true;
333 
334  if (m_solicitedEventIds.find ((*it)->GetInterface ()) != m_solicitedEventIds.end ())
335  {
336  if (m_solicitedEventIds[(*it)->GetInterface ()].IsRunning ())
337  {
338  scheduleSingle = false;
339  }
340  }
341 
342  if (m_unsolicitedEventIds.find ((*it)->GetInterface ()) != m_unsolicitedEventIds.end ())
343  {
344  if (t.GetTimeStep () > static_cast<int64_t> (m_unsolicitedEventIds[(*it)->GetInterface ()].GetTs ()))
345  {
346  scheduleSingle = false;
347  }
348  }
349 
350  if (scheduleSingle)
351  {
352  NS_LOG_INFO ("schedule new RA");
353  m_solicitedEventIds[(*it)->GetInterface ()] = Simulator::Schedule (MilliSeconds (delay), &Radvd::Send,
354  this, (*it), Ipv6Address::GetAllNodesMulticast (), false);
355  }
356  }
357  }
358  break;
359  default:
360  break;
361  }
362  }
363  }
364 }
365 
366 } /* namespace ns3 */
367 
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void CalculatePseudoHeaderChecksum(Ipv6Address src, Ipv6Address dst, uint16_t length, uint8_t protocol)
Calculate pseudo header checksum for IPv6.
uint32_t GetInterface() const
Get interface index for this configuration.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Packet header for IPv6.
Definition: ipv6-header.h:34
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 the RngStream.
ICMPv6 Router Advertisement header.
virtual uint32_t GetSerializedSize() const
Get the serialized size.
#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:98
RadvdInterfaceList m_configurations
List of configuration for interface.
Definition: radvd.h:163
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:81
Hold variables of type string.
Definition: string.h:41
Router advertisement daemon.
Definition: radvd.h:46
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:142
std::list< Ptr< RadvdInterface > >::const_iterator RadvdInterfaceListCI
Container Const Iterator: Ptr to RadvdInterface.
Definition: radvd.h:110
virtual int ShutdownSend(void)=0
IPv6 layer implementation.
ICMPv6 Router Solicitation header.
#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:202
uint32_t GetMinRtrAdvInterval() const
Get minimum RA interval.
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
std::list< Ptr< RadvdInterface > >::iterator RadvdInterfaceListI
Container Iterator: Ptr to RadvdInterface.
Definition: radvd.h:108
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:122
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event&#39;s associated function will not be invoked when it expires...
Definition: simulator.cc:346
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
SocketMap m_sendSockets
Raw socket to send RA.
Definition: radvd.h:158
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer...
Definition: socket.h:1115
uint32_t GetRetransTimer() const
Get retransmission timer.
uint32_t GetRecvIf(void) const
Get the tag&#39;s receiving interface.
bool IsManagedFlag() const
Is managed flag enabled ?
a polymophic address class
Definition: address.h:90
void SetLifeTime(uint16_t l)
Set the node Life time (Neighbor Discovery).
uint32_t GetDefaultLifeTime() const
Get default lifetime.
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:358
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: radvd.cc:167
virtual ~Radvd()
Destructor.
Definition: radvd.cc:71
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:220
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
The base class for all ns3 applications.
Definition: application.h:60
void SetTtl(uint8_t ttl)
Set the tag&#39;s TTL.
Definition: socket.cc:604
uint32_t GetLinkMtu() const
Get link MTU.
bool IsSourceLLAddress() const
Is source LLA option should be included in RA ?
Hold an unsigned integer type.
Definition: uinteger.h:44
void SetPrefixLength(uint8_t prefixLength)
Set the prefix length.
ICMPv6 Option Prefix Information.
EventIdMap m_unsolicitedEventIds
Event ID map for unsolicited RAs.
Definition: radvd.h:168
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
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:71
static const uint32_t MIN_DELAY_BETWEEN_RAS
Default value for minimum delay between RA advertisements (ms)
Definition: radvd.h:80
Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:100
#define list
Ptr< Node > GetNode() const
Definition: application.cc:104
RadvdPrefixList GetPrefixes() const
Get list of prefixes advertised for this interface.
virtual void DoDispose()
Dispose the instance.
Definition: radvd.cc:82
uint8_t GetCode() const
Get the code field.
void HandleRead(Ptr< Socket > socket)
Handle received packet, especially router solicitation.
Definition: radvd.cc:283
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
static const uint32_t MAX_RA_DELAY_TIME
Default value for maximum delay of RA (ms)
Definition: radvd.h:68
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.
bool IsHomeAgentFlag() const
Is "home agent" flag enabled ?
ICMPv6 MTU option.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
std::map< uint32_t, EventId >::iterator EventIdMapI
Container Iterator: interface number, EventId.
Definition: radvd.h:115
EventIdMap m_solicitedEventIds
Event ID map for solicited RAs.
Definition: radvd.h:173
static Ipv6Address GetAllNodesMulticast()
Get the "all nodes multicast" address.
void SetFlagO(bool o)
Set the O flag.
bool IsOtherConfigFlag() const
Is "other config" flag enabled ?
void SetFlagH(bool h)
Set the H flag.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
void Send(Ptr< RadvdInterface > config, Ipv6Address dst=Ipv6Address::GetAllNodesMulticast(), bool reschedule=false)
Send a packet.
Definition: radvd.cc:174
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
virtual void StopApplication()
Stop the application.
Definition: radvd.cc:138
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:49
Radvd()
Constructor.
Definition: radvd.cc:66
void AddConfiguration(Ptr< RadvdInterface > routerInterface)
Add configuration for an interface;.
Definition: radvd.cc:160
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:852
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:378
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:859
Ptr< UniformRandomVariable > m_jitter
Variable to provide jitter in advertisement interval.
Definition: radvd.h:178
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
uint32_t GetReachableTime() const
Get reachable time.
uint8_t GetCurHopLimit() const
Get current hop limit.
static bool IsMatchingType(const Address &addr)
If the address match.
This class implements a tag that carries socket ancillary data to the socket interface.
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.
uint32_t GetMaxRtrAdvInterval() const
Get maximum RA interval.
Ptr< Socket > m_recvSocket
Raw socket to receive RS.
Definition: radvd.h:153
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:76
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void SetPrefix(Ipv6Address prefix)
Set the IPv6 prefix.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:824
static Ipv6Address GetAllRoutersMulticast()
Get the "all routers multicast" address.
int64_t GetTimeStep(void) const
Get the raw time value, in the current resolution unit.
Definition: nstime.h:391