A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
icmpv6-l4-protocol.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  * David Gross <gdavid.devel@gmail.com>
20  * Mehdi Benamor <benamor.mehdi@ensi.rnu.tn>
21  * Tommaso Pecorella <tommaso.pecorella@unifi.it>
22  */
23 
24 #include "ns3/log.h"
25 #include "ns3/assert.h"
26 #include "ns3/packet.h"
27 #include "ns3/node.h"
28 #include "ns3/boolean.h"
29 #include "ns3/ipv6-routing-protocol.h"
30 #include "ns3/ipv6-route.h"
31 #include "ns3/pointer.h"
32 #include "ns3/string.h"
33 
35 #include "ipv6-l3-protocol.h"
36 #include "ipv6-interface.h"
37 #include "icmpv6-l4-protocol.h"
38 #include "ndisc-cache.h"
39 
40 namespace ns3 {
41 
42 NS_OBJECT_ENSURE_REGISTERED (Icmpv6L4Protocol);
43 
44 NS_LOG_COMPONENT_DEFINE ("Icmpv6L4Protocol");
45 
46 const uint8_t Icmpv6L4Protocol::PROT_NUMBER = 58;
47 
52 const uint32_t Icmpv6L4Protocol::MAX_RA_DELAY_TIME = 500; /* millisecond */
53 
57 
62 const uint32_t Icmpv6L4Protocol::REACHABLE_TIME = 30000;
63 const uint32_t Icmpv6L4Protocol::RETRANS_TIMER = 1000;
65 const double Icmpv6L4Protocol::MIN_RANDOM_FACTOR = 0.5;
66 const double Icmpv6L4Protocol::MAX_RANDOM_FACTOR = 1.5;
67 
69 {
70  static TypeId tid = TypeId ("ns3::Icmpv6L4Protocol")
72  .AddConstructor<Icmpv6L4Protocol> ()
73  .AddAttribute ("DAD", "Always do DAD check.",
74  BooleanValue (true),
75  MakeBooleanAccessor (&Icmpv6L4Protocol::m_alwaysDad),
76  MakeBooleanChecker ())
77  .AddAttribute ("SolicitationJitter", "The jitter in ms a node is allowed to wait before sending any solicitation . Some jitter aims to prevent collisions. By default, the model will wait for a duration in ms defined by a uniform random-variable between 0 and SolicitationJitter",
78  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=10.0]"),
79  MakePointerAccessor (&Icmpv6L4Protocol::m_solicitationJitter),
80  MakePointerChecker<RandomVariableStream> ())
81 
82  ;
83  return tid;
84 }
85 
87  : m_node (0)
88 {
89  NS_LOG_FUNCTION (this);
90 }
91 
93 {
94  NS_LOG_FUNCTION (this);
95 }
96 
98 {
99  NS_LOG_FUNCTION (this);
100  for (CacheList::const_iterator it = m_cacheList.begin (); it != m_cacheList.end (); it++)
101  {
102  Ptr<NdiscCache> cache = *it;
103  cache->Dispose ();
104  cache = 0;
105  }
106  m_cacheList.clear ();
108 
109  m_node = 0;
111 }
112 
113 int64_t Icmpv6L4Protocol::AssignStreams (int64_t stream)
114 {
115  NS_LOG_FUNCTION (this << stream);
117  return 1;
118 }
119 
121 {
122  NS_LOG_FUNCTION (this);
123  if (m_node == 0)
124  {
125  Ptr<Node> node = this->GetObject<Node> ();
126  if (node != 0)
127  {
128  Ptr<Ipv6L3Protocol> ipv6 = this->GetObject<Ipv6L3Protocol> ();
129  if (ipv6 != 0 && m_downTarget.IsNull ())
130  {
131  SetNode (node);
132  ipv6->Insert (this);
133  Ptr<Ipv6RawSocketFactoryImpl> rawFactory = CreateObject<Ipv6RawSocketFactoryImpl> ();
134  ipv6->AggregateObject (rawFactory);
136  }
137  }
138  }
140 }
141 
143 {
144  NS_LOG_FUNCTION (this << node);
145  m_node = node;
146 }
147 
149 {
151  return PROT_NUMBER;
152 }
153 
155 {
156  NS_LOG_FUNCTION (this);
157  return PROT_NUMBER;
158 }
159 
161 {
162  NS_LOG_FUNCTION (this);
163  return 1;
164 }
165 
167 {
168  NS_LOG_FUNCTION (this);
169  return m_alwaysDad;
170 }
171 
173 {
174  NS_LOG_FUNCTION (this << target << interface);
175  Ipv6Address addr;
177 
178  NS_ASSERT (ipv6);
179 
180  if (!m_alwaysDad)
181  {
182  return;
183  }
184 
187  Ptr<Packet> p = ForgeNS ("::",Ipv6Address::MakeSolicitedAddress (target), target, interface->GetDevice ()->GetAddress ());
188 
189  /* update last packet UID */
190  interface->SetNsDadUid (target, p->GetUid ());
192 }
193 
195 {
196  NS_LOG_FUNCTION (this << packet << header);
198 }
199 
201 {
202  NS_LOG_FUNCTION (this << packet << header.GetSourceAddress () << header.GetDestinationAddress () << interface);
203  Ptr<Packet> p = packet->Copy ();
204  Ptr<Ipv6> ipv6 = m_node->GetObject<Ipv6> ();
205 
206  /* very ugly! try to find something better in the future */
207  uint8_t type;
208  p->CopyData (&type, sizeof(type));
209 
210  switch (type)
211  {
213  if (ipv6->IsForwarding (ipv6->GetInterfaceForDevice (interface->GetDevice ())))
214  {
215  HandleRS (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
216  }
217  break;
219  if (!ipv6->IsForwarding (ipv6->GetInterfaceForDevice (interface->GetDevice ())))
220  {
221  HandleRA (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
222  }
223  break;
225  HandleNS (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
226  break;
228  HandleNA (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
229  break;
231  HandleRedirection (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
232  break;
234  HandleEchoRequest (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
235  break;
237  // EchoReply does not contain any info about L4
238  // so we can not forward it up.
240  break;
242  HandleDestinationUnreachable (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
243  break;
245  HandlePacketTooBig (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
246  break;
248  HandleTimeExceeded (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
249  break;
251  HandleParameterError (p, header.GetSourceAddress (), header.GetDestinationAddress (), interface);
252  break;
253  default:
254  NS_LOG_LOGIC ("Unknown ICMPv6 message type=" << type);
255  break;
256  }
257 
258  return IpL4Protocol::RX_OK;
259 }
260 
262  uint32_t info, Ipv6Header ipHeader,
263  const uint8_t payload[8])
264 {
265  NS_LOG_FUNCTION (this << source << icmp << info << ipHeader << payload);
266 
268 
270 
271  uint8_t nextHeader = ipHeader.GetNextHeader ();
272 
273  if (nextHeader != Icmpv6L4Protocol::PROT_NUMBER)
274  {
275  Ptr<IpL4Protocol> l4 = ipv6->GetProtocol (nextHeader);
276  if (l4 != 0)
277  {
278  l4->ReceiveIcmp (source, ipHeader.GetHopLimit (), icmp.GetType (), icmp.GetCode (),
279  info, ipHeader.GetSourceAddress (), ipHeader.GetDestinationAddress (), payload);
280  }
281  }
282 }
283 
285 {
286  NS_LOG_FUNCTION (this << packet << src << dst << interface);
287  Icmpv6Echo request;
288  uint8_t* buf = new uint8_t[packet->GetSize ()];
289 
290  packet->RemoveHeader (request);
291  /* XXX IPv6 extension: obtain a fresh copy of data otherwise it crash... */
292  packet->CopyData (buf, packet->GetSize ());
293  Ptr<Packet> p = Create<Packet> (buf, packet->GetSize ());
294 
295  /* if we send message from ff02::* (link-local multicast), we use our link-local address */
296  SendEchoReply (dst.IsMulticast () ? interface->GetLinkLocalAddress ().GetAddress () : dst, src, request.GetId (), request.GetSeq (), p);
297  delete[] buf;
298 }
299 
300 void Icmpv6L4Protocol::HandleRA (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, Ptr<Ipv6Interface> interface)
301 {
302  NS_LOG_FUNCTION (this << packet << src << dst << interface);
303  Ptr<Packet> p = packet->Copy ();
304  Icmpv6RA raHeader;
307  Icmpv6OptionMtu mtuHdr;
309  bool next = true;
310  bool hasLla = false;
311  bool hasMtu = false;
312  Ipv6Address defaultRouter = Ipv6Address::GetZero ();
313 
314  p->RemoveHeader (raHeader);
315 
316  if (raHeader.GetLifeTime())
317  {
318  defaultRouter = src;
319  }
320 
321  while (next == true)
322  {
323  uint8_t type = 0;
324  p->CopyData (&type, sizeof(type));
325 
326  switch (type)
327  {
329  p->RemoveHeader (prefixHdr);
330  ipv6->AddAutoconfiguredAddress (ipv6->GetInterfaceForDevice (interface->GetDevice ()), prefixHdr.GetPrefix (), prefixHdr.GetPrefixLength (),
331  prefixHdr.GetFlags (), prefixHdr.GetValidTime (), prefixHdr.GetPreferredTime (), defaultRouter);
332  break;
334  /* take in account the first MTU option */
335  if (!hasMtu)
336  {
337  p->RemoveHeader (mtuHdr);
338  hasMtu = true;
340  /* interface->GetDevice ()->SetMtu (m.GetMtu ()); */
341  }
342  break;
344  /* take in account the first LLA option */
345  if (!hasLla)
346  {
347  p->RemoveHeader (llaHdr);
348  ReceiveLLA (llaHdr, src, dst, interface);
349  hasLla = true;
350  }
351  break;
352  default:
353  /* unknow option, quit */
354  next = false;
355  }
356  }
357 }
358 
360 {
361  NS_LOG_FUNCTION (this << lla << src << dst << interface);
362  Address hardwareAddress;
363  NdiscCache::Entry* entry = 0;
364  Ptr<NdiscCache> cache = FindCache (interface->GetDevice ());
365 
366  /* check if we have this address in our cache */
367  entry = cache->Lookup (src);
368 
369  if (!entry)
370  {
371  entry = cache->Add (src);
372  entry->SetRouter (true);
373  entry->SetMacAddress (lla.GetAddress ());
374  entry->MarkReachable ();
375  entry->StartReachableTimer ();
376  }
377  else
378  {
379  std::list<Ptr<Packet> > waiting;
380  if (entry->IsIncomplete ())
381  {
382  entry->StopRetransmitTimer ();
383  // mark it to reachable
384  waiting = entry->MarkReachable (lla.GetAddress ());
385  entry->StopReachableTimer ();
386  entry->StartReachableTimer ();
387  // send out waiting packet
388  for (std::list<Ptr<Packet> >::const_iterator it = waiting.begin (); it != waiting.end (); it++)
389  {
390  cache->GetInterface ()->Send (*it, src);
391  }
392  entry->ClearWaitingPacket ();
393  }
394  else
395  {
396  if (entry->GetMacAddress () != lla.GetAddress ())
397  {
398  entry->SetMacAddress (lla.GetAddress ());
399  entry->MarkStale ();
400  entry->SetRouter (true);
401  }
402  else
403  {
404  if (!entry->IsReachable ())
405  {
406  entry->StopProbeTimer ();
407  entry->StopDelayTimer ();
408  waiting = entry->MarkReachable (lla.GetAddress ());
409  if (entry->IsProbe ())
410  {
411  for (std::list<Ptr<Packet> >::const_iterator it = waiting.begin (); it != waiting.end (); it++)
412  {
413  cache->GetInterface ()->Send (*it, src);
414  }
415  }
416  entry->StopReachableTimer ();
417  entry->StartReachableTimer ();
418  }
419  }
420  }
421  }
422 }
423 
424 void Icmpv6L4Protocol::HandleRS (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, Ptr<Ipv6Interface> interface)
425 {
426  NS_LOG_FUNCTION (this << packet << src << dst << interface);
428  Icmpv6RS rsHeader;
429  packet->RemoveHeader (rsHeader);
430  Address hardwareAddress;
432  NdiscCache::Entry* entry = 0;
433  Ptr<NdiscCache> cache = FindCache (interface->GetDevice ());
434 
435  if (src != Ipv6Address::GetAny ())
436  {
437  /* XXX search all options following the RS header */
438  /* test if the next option is SourceLinkLayerAddress */
439  uint8_t type;
440  packet->CopyData (&type, sizeof(type));
441 
443  {
444  return;
445  }
446  packet->RemoveHeader (lla);
447  NS_LOG_LOGIC ("Cache updated by RS");
448 
449  entry = cache->Lookup (src);
450  if (!entry)
451  {
452  entry = cache->Add (src);
453  entry->SetRouter (false);
454  entry->MarkStale (lla.GetAddress ());
455  }
456  else if (entry->GetMacAddress () != lla.GetAddress ())
457  {
458  entry->MarkStale (lla.GetAddress ());
459  }
460  }
461 }
462 
463 void Icmpv6L4Protocol::HandleNS (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, Ptr<Ipv6Interface> interface)
464 {
465  NS_LOG_FUNCTION (this << packet << src << dst << interface);
466  Icmpv6NS nsHeader ("::");
467  Ipv6InterfaceAddress ifaddr;
468  uint32_t nb = interface->GetNAddresses ();
469  uint32_t i = 0;
470  bool found = false;
471 
472  packet->RemoveHeader (nsHeader);
473 
474  Ipv6Address target = nsHeader.GetIpv6Target ();
475 
476  for (i = 0; i < nb; i++)
477  {
478  ifaddr = interface->GetAddress (i);
479 
480  if (ifaddr.GetAddress () == target)
481  {
482  found = true;
483  break;
484  }
485  }
486 
487  if (!found)
488  {
489  NS_LOG_LOGIC ("Not a NS for us");
490  return;
491  }
492 
493  if (packet->GetUid () == ifaddr.GetNsDadUid ())
494  {
495  /* don't process our own DAD probe */
496  NS_LOG_LOGIC ("Hey we receive our DAD probe!");
497  return;
498  }
499 
501  Address hardwareAddress;
502  NdiscCache::Entry* entry = 0;
503  Ptr<NdiscCache> cache = FindCache (interface->GetDevice ());
504  uint8_t flags = 0;
505 
506  /* XXX search all options following the NS header */
507 
508  if (src != Ipv6Address::GetAny ())
509  {
510  uint8_t type;
511  packet->CopyData (&type, sizeof(type));
512 
514  {
515  return;
516  }
517 
518  /* Get LLA */
519  packet->RemoveHeader (lla);
520 
521  entry = cache->Lookup (src);
522  if (!entry)
523  {
524  entry = cache->Add (src);
525  entry->SetRouter (false);
526  entry->MarkStale (lla.GetAddress ());
527  }
528  else if (entry->GetMacAddress () != lla.GetAddress ())
529  {
530  entry->MarkStale (lla.GetAddress ());
531  }
532 
533  flags = 3; /* S + O flags */
534  }
535  else
536  {
537  /* it means someone do a DAD */
538  flags = 1; /* O flag */
539  }
540 
541  /* send a NA to src */
543 
544  if (ipv6->IsForwarding (ipv6->GetInterfaceForDevice (interface->GetDevice ())))
545  {
546  flags += 4; /* R flag */
547  }
548 
549  hardwareAddress = interface->GetDevice ()->GetAddress ();
550  Ptr<Packet> p = ForgeNA (target.IsLinkLocal () ? interface->GetLinkLocalAddress ().GetAddress () : ifaddr.GetAddress (), src.IsAny () ? Ipv6Address::GetAllNodesMulticast () : src, &hardwareAddress, flags );
551  interface->Send (p, src.IsAny () ? Ipv6Address::GetAllNodesMulticast () : src);
552 
553  /* not a NS for us discard it */
554 }
555 
557 {
558  NS_LOG_FUNCTION (this << src << dst << hardwareAddress);
559  Ptr<Packet> p = Create<Packet> ();
560  Ipv6Header ipHeader;
561  Icmpv6RS rs;
562  Icmpv6OptionLinkLayerAddress llOption (1, hardwareAddress); /* we give our mac address in response */
563 
564  NS_LOG_LOGIC ("Send RS ( from " << src << " to " << dst << ")");
565  p->AddHeader (llOption);
566 
568  p->AddHeader (rs);
569 
570  ipHeader.SetSourceAddress (src);
571  ipHeader.SetDestinationAddress (dst);
572  ipHeader.SetNextHeader (PROT_NUMBER);
573  ipHeader.SetPayloadLength (p->GetSize ());
574  ipHeader.SetHopLimit (255);
575 
576  p->AddHeader (ipHeader);
577 
578  return p;
579 }
580 
582 {
583  NS_LOG_FUNCTION (this << src << dst << id << seq << data);
584  Ptr<Packet> p = data->Copy ();
585  Ipv6Header ipHeader;
586  Icmpv6Echo req (1);
587 
588  req.SetId (id);
589  req.SetSeq (seq);
590 
591  p->AddHeader (req);
592 
593  return p;
594 }
595 
596 void Icmpv6L4Protocol::HandleNA (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, Ptr<Ipv6Interface> interface)
597 {
598  NS_LOG_FUNCTION (this << packet << src << dst << interface);
599  Icmpv6NA naHeader;
601 
602  packet->RemoveHeader (naHeader);
603  Ipv6Address target = naHeader.GetIpv6Target ();
604 
605  Address hardwareAddress;
606  NdiscCache::Entry* entry = 0;
607  Ptr<NdiscCache> cache = FindCache (interface->GetDevice ());
608  std::list<Ptr<Packet> > waiting;
609 
610  /* check if we have something in our cache */
611  entry = cache->Lookup (target);
612 
613  if (!entry)
614  {
615  /* ouch!! we might be victim of a DAD */
616 
617  Ipv6InterfaceAddress ifaddr;
618  bool found = false;
619  uint32_t i = 0;
620  uint32_t nb = interface->GetNAddresses ();
621 
622  for (i = 0; i < nb; i++)
623  {
624  ifaddr = interface->GetAddress (i);
625  if (ifaddr.GetAddress () == target)
626  {
627  found = true;
628  break;
629  }
630  }
631 
632  if (found)
633  {
635  {
636  interface->SetState (ifaddr.GetAddress (), Ipv6InterfaceAddress::INVALID);
637  }
638  }
639 
640  /* we have not initiated any communication with the target so... discard the NA */
641  return;
642  }
643 
644  /* XXX search all options following the NA header */
645  /* Get LLA */
646  uint8_t type;
647  packet->CopyData (&type, sizeof(type));
648 
650  {
651  return;
652  }
653  packet->RemoveHeader (lla);
654 
655  if (entry->IsIncomplete ())
656  {
657  /* we receive a NA so stop the retransmission timer */
658  entry->StopRetransmitTimer ();
659 
660  if (naHeader.GetFlagS ())
661  {
662  /* mark it to reachable */
663  waiting = entry->MarkReachable (lla.GetAddress ());
664  entry->StopReachableTimer ();
665  entry->StartReachableTimer ();
666  /* send out waiting packet */
667  for (std::list<Ptr<Packet> >::const_iterator it = waiting.begin (); it != waiting.end (); it++)
668  {
669  cache->GetInterface ()->Send (*it, src);
670  }
671  entry->ClearWaitingPacket ();
672  }
673  else
674  {
675  entry->MarkStale (lla.GetAddress ());
676  }
677 
678  if (naHeader.GetFlagR ())
679  {
680  entry->SetRouter (true);
681  }
682  }
683  else
684  {
685  /* we receive a NA so stop the probe timer or delay timer if any */
686  entry->StopProbeTimer ();
687  entry->StopDelayTimer ();
688 
689  /* if the Flag O is clear and mac address differs from the cache */
690  if (!naHeader.GetFlagO () && lla.GetAddress () != entry->GetMacAddress ())
691  {
692  if (entry->IsReachable ())
693  {
694  entry->MarkStale ();
695  }
696  return;
697  }
698  else
699  {
700  if ((!naHeader.GetFlagO () && lla.GetAddress () == entry->GetMacAddress ()) || naHeader.GetFlagO ()) /* XXX lake "no target link-layer address option supplied" */
701  {
702  entry->SetMacAddress (lla.GetAddress ());
703 
704  if (naHeader.GetFlagS ())
705  {
706  if (!entry->IsReachable ())
707  {
708  if (entry->IsProbe ())
709  {
710  waiting = entry->MarkReachable (lla.GetAddress ());
711  for (std::list<Ptr<Packet> >::const_iterator it = waiting.begin (); it != waiting.end (); it++)
712  {
713  cache->GetInterface ()->Send (*it, src);
714  }
715  entry->ClearWaitingPacket ();
716  }
717  else
718  {
719  entry->MarkReachable (lla.GetAddress ());
720  }
721  }
722  entry->StopReachableTimer ();
723  entry->StartReachableTimer ();
724  }
725  else if (lla.GetAddress () != entry->GetMacAddress ())
726  {
727  entry->MarkStale ();
728  }
729  entry->SetRouter (naHeader.GetFlagR ());
730  }
731  }
732  }
733 }
734 
736 {
737  NS_LOG_FUNCTION (this << packet << src << dst << interface);
738  bool hasLla = false;
739  Ptr<Packet> p = packet->Copy ();
740  Icmpv6OptionLinkLayerAddress llOptionHeader (0);
741 
742  Icmpv6Redirection redirectionHeader;
743  p->RemoveHeader (redirectionHeader);
744 
745  /* little ugly try to find a better way */
746  uint8_t type;
747  p->CopyData (&type, sizeof(type));
749  {
750  hasLla = true;
751  p->RemoveHeader (llOptionHeader);
752  }
753 
754  Icmpv6OptionRedirected redirectedOptionHeader;
755  p->RemoveHeader (redirectedOptionHeader);
756 
757  Ipv6Address redirTarget = redirectionHeader.GetTarget ();
758  Ipv6Address redirDestination = redirectionHeader.GetDestination ();
759 
760  if (hasLla)
761  {
762  /* update the cache if needed */
763  NdiscCache::Entry* entry = 0;
764  Ptr<NdiscCache> cache = FindCache (interface->GetDevice ());
765 
766  entry = cache->Lookup (redirTarget);
767  if (!entry)
768  {
769  entry = cache->Add (redirTarget);
770  /* destination and target different => necessarily a router */
771  entry->SetRouter (!redirTarget.IsEqual (redirDestination) ? true : false);
772  entry->SetMacAddress (llOptionHeader.GetAddress ());
773  entry->MarkStale ();
774  }
775  else
776  {
777  if (entry->IsIncomplete () || entry->GetMacAddress () != llOptionHeader.GetAddress ())
778  {
779  /* update entry to STALE */
780  if (entry->GetMacAddress () != llOptionHeader.GetAddress ())
781  {
782  entry->SetMacAddress (llOptionHeader.GetAddress ());
783  entry->MarkStale ();
784  }
785  }
786  else
787  {
788  /* stay unchanged */
789  }
790  }
791  }
792 
793  /* add redirection in routing table */
794  Ptr<Ipv6> ipv6 = m_node->GetObject<Ipv6> ();
795 
796  if (redirTarget.IsEqual (redirDestination))
797  {
798  ipv6->GetRoutingProtocol ()->NotifyAddRoute (redirDestination, Ipv6Prefix (128), Ipv6Address ("::"), ipv6->GetInterfaceForAddress (dst));
799  }
800  else
801  {
802  uint32_t ifIndex = ipv6->GetInterfaceForAddress (dst);
803  ipv6->GetRoutingProtocol ()->NotifyAddRoute (redirDestination, Ipv6Prefix (128), redirTarget, ifIndex);
804  }
805 }
806 
808 {
809  NS_LOG_FUNCTION (this << *p << src << dst << interface);
810  Ptr<Packet> pkt = p->Copy ();
811 
813  pkt->RemoveHeader (unreach);
814  Ptr<Packet> origPkt = unreach.GetPacket ();
815 
816  Ipv6Header ipHeader;
817  if ( origPkt->GetSerializedSize () > ipHeader.GetSerializedSize () )
818  {
819  origPkt->RemoveHeader (ipHeader);
820  uint8_t payload[8];
821  origPkt->CopyData (payload, 8);
822  Forward (src, unreach, unreach.GetCode (), ipHeader, payload);
823  }
824 }
825 
827 {
828  NS_LOG_FUNCTION (this << *p << src << dst << interface);
829  Ptr<Packet> pkt = p->Copy ();
830 
831  Icmpv6TimeExceeded timeexceeded;
832  pkt->RemoveHeader (timeexceeded);
833  Ptr<Packet> origPkt = timeexceeded.GetPacket ();
834  Ipv6Header ipHeader;
835  uint8_t payload[8];
836  origPkt->RemoveHeader (ipHeader);
837  origPkt->CopyData (payload, 8);
838 
839  Forward (src, timeexceeded, timeexceeded.GetCode (), ipHeader, payload);
840 }
841 
843 {
844  NS_LOG_FUNCTION (this << *p << src << dst << interface);
845  Ptr<Packet> pkt = p->Copy ();
846 
847  Icmpv6TooBig tooBig;
848  pkt->RemoveHeader (tooBig);
849  Ptr<Packet> origPkt = tooBig.GetPacket ();
850 
851  Ipv6Header ipHeader;
852  origPkt->RemoveHeader (ipHeader);
853  uint8_t payload[8];
854  origPkt->CopyData (payload, 8);
855 
857  ipv6->SetPmtu(ipHeader.GetDestinationAddress(), tooBig.GetMtu ());
858 
859  Forward (src, tooBig, tooBig.GetMtu (), ipHeader, payload);
860 }
861 
863 {
864  NS_LOG_FUNCTION (this << *p << src << dst << interface);
865  Ptr<Packet> pkt = p->Copy ();
866 
867  Icmpv6ParameterError paramErr;
868  pkt->RemoveHeader (paramErr);
869  Ptr<Packet> origPkt = paramErr.GetPacket ();
870 
871  Ipv6Header ipHeader;
872  origPkt->RemoveHeader (ipHeader);
873  uint8_t payload[8];
874  origPkt->CopyData (payload, 8);
875  Forward (src, paramErr, paramErr.GetCode (), ipHeader, payload);
876 }
877 
879 {
880  NS_LOG_FUNCTION (this << packet << src << dst << (uint32_t)ttl);
882  SocketIpTtlTag tag;
883  NS_ASSERT (ipv6 != 0);
884 
885  tag.SetTtl (ttl);
886  packet->AddPacketTag (tag);
887  m_downTarget (packet, src, dst, PROT_NUMBER, 0);
888 }
889 
891 {
892  NS_LOG_FUNCTION (this << packet << src << dst << (uint32_t)ttl);
893  SendMessage (packet, src, dst, ttl);
894 }
895 
896 void Icmpv6L4Protocol::SendMessage (Ptr<Packet> packet, Ipv6Address dst, Icmpv6Header& icmpv6Hdr, uint8_t ttl)
897 {
898  NS_LOG_FUNCTION (this << packet << dst << icmpv6Hdr << (uint32_t)ttl);
900  NS_ASSERT (ipv6 != 0 && ipv6->GetRoutingProtocol () != 0);
901  Ipv6Header header;
902  SocketIpTtlTag tag;
904  Ptr<Ipv6Route> route;
905  Ptr<NetDevice> oif (0); //specify non-zero if bound to a source address
906 
907  header.SetDestinationAddress (dst);
908  route = ipv6->GetRoutingProtocol ()->RouteOutput (packet, header, oif, err);
909 
910  if (route != 0)
911  {
912  NS_LOG_LOGIC ("Route exists");
913  tag.SetTtl (ttl);
914  packet->AddPacketTag (tag);
915  Ipv6Address src = route->GetSource ();
916 
917  icmpv6Hdr.CalculatePseudoHeaderChecksum (src, dst, packet->GetSize () + icmpv6Hdr.GetSerializedSize (), PROT_NUMBER);
918  packet->AddHeader (icmpv6Hdr);
919  m_downTarget (packet, src, dst, PROT_NUMBER, route);
920  }
921  else
922  {
923  NS_LOG_WARN ("drop icmp message");
924  }
925 }
926 
927 void Icmpv6L4Protocol::SendNA (Ipv6Address src, Ipv6Address dst, Address* hardwareAddress, uint8_t flags)
928 {
929  NS_LOG_FUNCTION (this << src << dst << hardwareAddress << static_cast<uint32_t> (flags));
930  Ptr<Packet> p = Create<Packet> ();
931  Icmpv6NA na;
932  Icmpv6OptionLinkLayerAddress llOption (0, *hardwareAddress); /* not a source link layer */
933 
934  NS_LOG_LOGIC ("Send NA ( from " << src << " to " << dst << " target " << src << ")");
935  na.SetIpv6Target (src);
936 
937  if ((flags & 1))
938  {
939  na.SetFlagO (true);
940  }
941  if ((flags & 2) && src != Ipv6Address::GetAny ())
942  {
943  na.SetFlagS (true);
944  }
945  if ((flags & 4))
946  {
947  na.SetFlagR (true);
948  }
949 
950  p->AddHeader (llOption);
951  na.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + na.GetSerializedSize (), PROT_NUMBER);
952  p->AddHeader (na);
953 
954  SendMessage (p, src, dst, 255);
955 }
956 
957 void Icmpv6L4Protocol::SendEchoReply (Ipv6Address src, Ipv6Address dst, uint16_t id, uint16_t seq, Ptr<Packet> data)
958 {
959  NS_LOG_FUNCTION (this << src << dst << id << seq << data);
960  Ptr<Packet> p = data->Copy ();
961  Icmpv6Echo reply (0); /* echo reply */
962 
963  reply.SetId (id);
964  reply.SetSeq (seq);
965 
966  reply.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + reply.GetSerializedSize (), PROT_NUMBER);
967  p->AddHeader (reply);
968  SendMessage (p, src, dst, 64);
969 }
970 
971 void Icmpv6L4Protocol::SendNS (Ipv6Address src, Ipv6Address dst, Ipv6Address target, Address hardwareAddress)
972 {
973  NS_LOG_FUNCTION (this << src << dst << target << hardwareAddress);
974  Ptr<Packet> p = Create<Packet> ();
975  /* Ipv6Header ipHeader; */
976  Icmpv6NS ns (target);
977  Icmpv6OptionLinkLayerAddress llOption (1, hardwareAddress); /* we give our mac address in response */
978 
979  /* if the source is unspec, multicast the NA to all-nodes multicast */
980  if (src == Ipv6Address::GetAny ())
981  {
983  }
984 
985  NS_LOG_LOGIC ("Send NS ( from " << src << " to " << dst << " target " << target << ")");
986 
987  p->AddHeader (llOption);
988  ns.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + ns.GetSerializedSize (), PROT_NUMBER);
989  p->AddHeader (ns);
990  if (!dst.IsMulticast ())
991  {
992  SendMessage (p, src, dst, 255);
993  }
994  else
995  {
996  NS_LOG_LOGIC ("Destination is Multicast, using DelayedSendMessage");
997  Simulator::Schedule (Time (MilliSeconds (m_solicitationJitter->GetValue ())), &Icmpv6L4Protocol::DelayedSendMessage, this, p, src, dst, 255);
998  }
999 }
1000 
1002 {
1003  NS_LOG_FUNCTION (this << src << dst << hardwareAddress);
1004  Ptr<Packet> p = Create<Packet> ();
1005  Icmpv6RS rs;
1006  Icmpv6OptionLinkLayerAddress llOption (1, hardwareAddress); /* we give our mac address in response */
1007 
1008  /* if the source is unspec, multicast the NA to all-nodes multicast */
1009  if (src != Ipv6Address::GetAny ())
1010  {
1011  p->AddHeader (llOption);
1012  }
1013 
1014  NS_LOG_LOGIC ("Send RS ( from " << src << " to " << dst << ")");
1015 
1016  rs.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + rs.GetSerializedSize (), PROT_NUMBER);
1017  p->AddHeader (rs);
1018  if (!dst.IsMulticast ())
1019  {
1020  SendMessage (p, src, dst, 255);
1021  }
1022  else
1023  {
1024  NS_LOG_LOGIC ("Destination is Multicast, using DelayedSendMessage");
1025  Simulator::Schedule (Time (MilliSeconds (m_solicitationJitter->GetValue ())), &Icmpv6L4Protocol::DelayedSendMessage, this, p, src, dst, 255);
1026  }
1027 }
1028 
1030 {
1031  NS_LOG_FUNCTION (this << malformedPacket << dst << (uint32_t)code);
1032  Ptr<Packet> p = Create<Packet> ();
1033  uint32_t malformedPacketSize = malformedPacket->GetSize ();
1035 
1036  NS_LOG_LOGIC ("Send Destination Unreachable ( to " << dst << " code " << (uint32_t)code << " )");
1037 
1038  /* 48 = sizeof IPv6 header + sizeof ICMPv6 error header */
1039  if (malformedPacketSize <= 1280 - 48)
1040  {
1041  header.SetPacket (malformedPacket);
1042  }
1043  else
1044  {
1045  Ptr<Packet> fragment = malformedPacket->CreateFragment (0, 1280 - 48);
1046  header.SetPacket (fragment);
1047  }
1048 
1049  header.SetCode (code);
1050  SendMessage (p, dst, header, 255);
1051 }
1052 
1053 void Icmpv6L4Protocol::SendErrorTooBig (Ptr<Packet> malformedPacket, Ipv6Address dst, uint32_t mtu)
1054 {
1055  NS_LOG_FUNCTION (this << malformedPacket << dst << mtu);
1056  Ptr<Packet> p = Create<Packet> ();
1057  uint32_t malformedPacketSize = malformedPacket->GetSize ();
1058  Icmpv6TooBig header;
1059 
1060  NS_LOG_LOGIC ("Send Too Big ( to " << dst << " )");
1061 
1062  /* 48 = sizeof IPv6 header + sizeof ICMPv6 error header */
1063  if (malformedPacketSize <= 1280 - 48)
1064  {
1065  header.SetPacket (malformedPacket);
1066  }
1067  else
1068  {
1069  Ptr<Packet> fragment = malformedPacket->CreateFragment (0, 1280 - 48);
1070  header.SetPacket (fragment);
1071  }
1072 
1073  header.SetCode (0);
1074  header.SetMtu (mtu);
1075  SendMessage (p, dst, header, 255);
1076 }
1077 
1078 void Icmpv6L4Protocol::SendErrorTimeExceeded (Ptr<Packet> malformedPacket, Ipv6Address dst, uint8_t code)
1079 {
1080  NS_LOG_FUNCTION (this << malformedPacket << dst << static_cast<uint32_t> (code));
1081  Ptr<Packet> p = Create<Packet> ();
1082  uint32_t malformedPacketSize = malformedPacket->GetSize ();
1083  Icmpv6TimeExceeded header;
1084 
1085  NS_LOG_LOGIC ("Send Time Exceeded ( to " << dst << " code " << (uint32_t)code << " )");
1086 
1087  /* 48 = sizeof IPv6 header + sizeof ICMPv6 error header */
1088  if (malformedPacketSize <= 1280 - 48)
1089  {
1090  header.SetPacket (malformedPacket);
1091  }
1092  else
1093  {
1094  Ptr<Packet> fragment = malformedPacket->CreateFragment (0, 1280 - 48);
1095  header.SetPacket (fragment);
1096  }
1097 
1098  header.SetCode (code);
1099  SendMessage (p, dst, header, 255);
1100 }
1101 
1102 void Icmpv6L4Protocol::SendErrorParameterError (Ptr<Packet> malformedPacket, Ipv6Address dst, uint8_t code, uint32_t ptr)
1103 {
1104  NS_LOG_FUNCTION (this << malformedPacket << dst << static_cast<uint32_t> (code) << ptr);
1105  Ptr<Packet> p = Create<Packet> ();
1106  uint32_t malformedPacketSize = malformedPacket->GetSize ();
1107  Icmpv6ParameterError header;
1108 
1109  NS_LOG_LOGIC ("Send Parameter Error ( to " << dst << " code " << (uint32_t)code << " )");
1110 
1111  /* 48 = sizeof IPv6 header + sizeof ICMPv6 error header */
1112  if (malformedPacketSize <= 1280 - 48 )
1113  {
1114  header.SetPacket (malformedPacket);
1115  }
1116  else
1117  {
1118  Ptr<Packet> fragment = malformedPacket->CreateFragment (0, 1280 - 48);
1119  header.SetPacket (fragment);
1120  }
1121 
1122  header.SetCode (code);
1123  header.SetPtr (ptr);
1124  SendMessage (p, dst, header, 255);
1125 }
1126 
1127 void Icmpv6L4Protocol::SendRedirection (Ptr<Packet> redirectedPacket, Ipv6Address src, Ipv6Address dst, Ipv6Address redirTarget, Ipv6Address redirDestination, Address redirHardwareTarget)
1128 {
1129  NS_LOG_FUNCTION (this << redirectedPacket << dst << redirTarget << redirDestination << redirHardwareTarget);
1130  uint32_t llaSize = 0;
1131  Ptr<Packet> p = Create<Packet> ();
1132  uint32_t redirectedPacketSize = redirectedPacket->GetSize ();
1133  Icmpv6OptionLinkLayerAddress llOption (0);
1134 
1135  NS_LOG_LOGIC ("Send Redirection ( to " << dst << " target " << redirTarget << " destination " << redirDestination << " )");
1136 
1137  Icmpv6OptionRedirected redirectedOptionHeader;
1138 
1139  if ((redirectedPacketSize % 8) != 0)
1140  {
1141  Ptr<Packet> pad = Create<Packet> (8 - (redirectedPacketSize % 8));
1142  redirectedPacket->AddAtEnd (pad);
1143  }
1144 
1145  if (redirHardwareTarget.GetLength ())
1146  {
1147  llOption.SetAddress (redirHardwareTarget);
1148  llaSize = llOption.GetSerializedSize ();
1149  }
1150 
1151  /* 56 = sizeof IPv6 header + sizeof ICMPv6 error header + sizeof redirected option */
1152  if (redirectedPacketSize <= (1280 - 56 - llaSize))
1153  {
1154  redirectedOptionHeader.SetPacket (redirectedPacket);
1155  }
1156  else
1157  {
1158  Ptr<Packet> fragment = redirectedPacket->CreateFragment (0, 1280 - 56 - llaSize);
1159  redirectedOptionHeader.SetPacket (fragment);
1160  }
1161 
1162  p->AddHeader (redirectedOptionHeader);
1163 
1164  if (llaSize)
1165  {
1166  p->AddHeader (llOption);
1167  }
1168 
1169  Icmpv6Redirection redirectionHeader;
1170  redirectionHeader.SetTarget (redirTarget);
1171  redirectionHeader.SetDestination (redirDestination);
1172  redirectionHeader.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + redirectionHeader.GetSerializedSize (), PROT_NUMBER);
1173  p->AddHeader (redirectionHeader);
1174 
1175  SendMessage (p, src, dst, 64);
1176 }
1177 
1178 Ptr<Packet> Icmpv6L4Protocol::ForgeNA (Ipv6Address src, Ipv6Address dst, Address* hardwareAddress, uint8_t flags)
1179 {
1180  NS_LOG_FUNCTION (this << src << dst << hardwareAddress << (uint32_t)flags);
1181  Ptr<Packet> p = Create<Packet> ();
1182  Ipv6Header ipHeader;
1183  Icmpv6NA na;
1184  Icmpv6OptionLinkLayerAddress llOption (0, *hardwareAddress); /* we give our mac address in response */
1185 
1186  NS_LOG_LOGIC ("Send NA ( from " << src << " to " << dst << ")");
1187 
1188  /* forge the entire NA packet from IPv6 header to ICMPv6 link-layer option, so that the packet does not pass by Icmpv6L4Protocol::Lookup again */
1189 
1190  p->AddHeader (llOption);
1191  na.SetIpv6Target (src);
1192 
1193  if ((flags & 1))
1194  {
1195  na.SetFlagO (true);
1196  }
1197  if ((flags & 2) && src != Ipv6Address::GetAny ())
1198  {
1199  na.SetFlagS (true);
1200  }
1201  if ((flags & 4))
1202  {
1203  na.SetFlagR (true);
1204  }
1205 
1207  p->AddHeader (na);
1208 
1209  ipHeader.SetSourceAddress (src);
1210  ipHeader.SetDestinationAddress (dst);
1211  ipHeader.SetNextHeader (PROT_NUMBER);
1212  ipHeader.SetPayloadLength (p->GetSize ());
1213  ipHeader.SetHopLimit (255);
1214 
1215  p->AddHeader (ipHeader);
1216 
1217  return p;
1218 }
1219 
1221 {
1222  NS_LOG_FUNCTION (this << src << dst << target << hardwareAddress);
1223  Ptr<Packet> p = Create<Packet> ();
1224  Ipv6Header ipHeader;
1225  Icmpv6NS ns (target);
1226  Icmpv6OptionLinkLayerAddress llOption (1, hardwareAddress); /* we give our mac address in response */
1227 
1228  /* if the source is unspec, multicast the NA to all-nodes multicast */
1229  if (src == Ipv6Address::GetAny ())
1230  {
1232  }
1233 
1234  NS_LOG_LOGIC ("Send NS ( from " << src << " to " << dst << " target " << target << ")");
1235 
1236  p->AddHeader (llOption);
1238  p->AddHeader (ns);
1239 
1240  ipHeader.SetSourceAddress (src);
1241  ipHeader.SetDestinationAddress (dst);
1242  ipHeader.SetNextHeader (PROT_NUMBER);
1243  ipHeader.SetPayloadLength (p->GetSize ());
1244  ipHeader.SetHopLimit (255);
1245 
1246  p->AddHeader (ipHeader);
1247 
1248  return p;
1249 }
1250 
1252 {
1253  NS_LOG_FUNCTION (this << device);
1254 
1255  for (CacheList::const_iterator i = m_cacheList.begin (); i != m_cacheList.end (); i++)
1256  {
1257  if ((*i)->GetDevice () == device)
1258  {
1259  return *i;
1260  }
1261  }
1262 
1263  NS_ASSERT (false);
1264  /* quiet compiler */
1265  return 0;
1266 }
1267 
1269 {
1270  NS_LOG_FUNCTION (this << device << interface);
1271 
1272  Ptr<NdiscCache> cache = CreateObject<NdiscCache> ();
1273 
1274  cache->SetDevice (device, interface);
1275  device->AddLinkChangeCallback (MakeCallback (&NdiscCache::Flush, cache));
1276  m_cacheList.push_back (cache);
1277  return cache;
1278 }
1279 
1280 bool Icmpv6L4Protocol::Lookup (Ipv6Address dst, Ptr<NetDevice> device, Ptr<NdiscCache> cache, Address* hardwareDestination)
1281 {
1282  NS_LOG_FUNCTION (this << dst << device << cache << hardwareDestination);
1283 
1284  if (!cache)
1285  {
1286  /* try to find the cache */
1287  cache = FindCache (device);
1288  }
1289  if (cache)
1290  {
1291  NdiscCache::Entry* entry = cache->Lookup (dst);
1292  if (entry)
1293  {
1294  if (entry->IsReachable () || entry->IsDelay ())
1295  {
1296  *hardwareDestination = entry->GetMacAddress ();
1297  return true;
1298  }
1299  else if (entry->IsStale ())
1300  {
1301  entry->StartDelayTimer ();
1302  entry->MarkDelay ();
1303  *hardwareDestination = entry->GetMacAddress ();
1304  return true;
1305  }
1306  }
1307  }
1308  return false;
1309 }
1310 
1311 bool Icmpv6L4Protocol::Lookup (Ptr<Packet> p, Ipv6Address dst, Ptr<NetDevice> device, Ptr<NdiscCache> cache, Address* hardwareDestination)
1312 {
1313  NS_LOG_FUNCTION (this << p << dst << device << cache << hardwareDestination);
1314 
1315  if (!cache)
1316  {
1317  /* try to find the cache */
1318  cache = FindCache (device);
1319  }
1320  if (!cache)
1321  {
1322  return false;
1323  }
1324 
1325  NdiscCache::Entry* entry = cache->Lookup (dst);
1326  if (entry)
1327  {
1328  if (entry->IsReachable () || entry->IsDelay ())
1329  {
1330  /* XXX check reachability time */
1331  /* send packet */
1332  *hardwareDestination = entry->GetMacAddress ();
1333  return true;
1334  }
1335  else if (entry->IsStale ())
1336  {
1337  /* start delay timer */
1338  entry->StartDelayTimer ();
1339  entry->MarkDelay ();
1340  *hardwareDestination = entry->GetMacAddress ();
1341  return true;
1342  }
1343  else /* PROBE */
1344  {
1345  /* queue packet */
1346  entry->AddWaitingPacket (p);
1347  return false;
1348  }
1349  }
1350  else
1351  {
1352  /* we contact this node for the first time
1353  * add it to the cache and send an NS
1354  */
1355  Ipv6Address addr;
1356  NdiscCache::Entry* entry = cache->Add (dst);
1357  entry->MarkIncomplete (p);
1358  entry->SetRouter (false);
1359 
1360  if (dst.IsLinkLocal ())
1361  {
1362  addr = cache->GetInterface ()->GetLinkLocalAddress ().GetAddress ();
1363  }
1364  else if (cache->GetInterface ()->GetNAddresses () == 1) /* an interface have at least one address (link-local) */
1365  {
1366  /* try to resolve global address without having global address so return! */
1367  cache->Remove (entry);
1368  return false;
1369  }
1370  else
1371  {
1372  /* find source address that match destination */
1373  addr = cache->GetInterface ()->GetAddressMatchingDestination (dst).GetAddress ();
1374  }
1375 
1376  SendNS (addr, Ipv6Address::MakeSolicitedAddress (dst), dst, cache->GetDevice ()->GetAddress ());
1377 
1378  /* start retransmit timer */
1379  entry->StartRetransmitTimer ();
1380  return false;
1381  }
1382 
1383  return false;
1384 }
1385 
1387 {
1389  NS_LOG_LOGIC (interface << " " << addr);
1390  Ipv6InterfaceAddress ifaddr;
1391  bool found = false;
1392  uint32_t i = 0;
1393  uint32_t nb = interface->GetNAddresses ();
1394 
1395  for (i = 0; i < nb; i++)
1396  {
1397  ifaddr = interface->GetAddress (i);
1398 
1399  if (ifaddr.GetAddress () == addr)
1400  {
1401  found = true;
1402  break;
1403  }
1404  }
1405 
1406  /* for the moment, this function is always called, if we was victim of a DAD the address is INVALID
1407  * and we do not set it to PREFERRED
1408  */
1409  if (found && ifaddr.GetState () != Ipv6InterfaceAddress::INVALID)
1410  {
1411  interface->SetState (ifaddr.GetAddress (), Ipv6InterfaceAddress::PREFERRED);
1412  NS_LOG_LOGIC ("DAD OK, interface in state PREFERRED");
1413 
1414  /* send an RS if our interface is not forwarding (router) and if address is a link-local ones
1415  * (because we will send RS with it)
1416  */
1417  Ptr<Ipv6> ipv6 = icmpv6->m_node->GetObject<Ipv6> ();
1418 
1419  if (!ipv6->IsForwarding (ipv6->GetInterfaceForDevice (interface->GetDevice ())) && addr.IsLinkLocal ())
1420  {
1421  /* \todo Add random delays before sending RS
1422  * because all nodes start at the same time, there will be many of RS arround 1 second of simulation time
1423  */
1424  Simulator::Schedule (Seconds (0.0), &Icmpv6L4Protocol::SendRS, PeekPointer (icmpv6), ifaddr.GetAddress (), Ipv6Address::GetAllRoutersMulticast (), interface->GetDevice ()->GetAddress ());
1425  }
1426  }
1427 }
1428 
1429 void
1431 {
1432  NS_LOG_FUNCTION (this << &callback);
1433 }
1434 
1435 void
1437 {
1438  NS_LOG_FUNCTION (this << &callback);
1439  m_downTarget = callback;
1440 }
1441 
1444 {
1445  NS_LOG_FUNCTION (this);
1446  return (IpL4Protocol::DownTargetCallback)NULL;
1447 }
1448 
1451 {
1452  NS_LOG_FUNCTION (this);
1453  return m_downTarget;
1454 }
1455 
1456 } /* namespace ns3 */
1457 
void DelayedSendMessage(Ptr< Packet > packet, Ipv6Address src, Ipv6Address dst, uint8_t ttl)
Helper function used during delayed solicitation.
bool IsAny() const
If the IPv6 address is the "Any" address.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
virtual uint32_t GetSerializedSize(void) const
Get the serialized size of the packet.
Definition: ipv6-header.cc:143
uint8_t GetNextHeader(void) const
Get the next header.
Definition: ipv6-header.cc:81
void SetPacket(Ptr< Packet > packet)
Set the redirected packet.
void CalculatePseudoHeaderChecksum(Ipv6Address src, Ipv6Address dst, uint16_t length, uint8_t protocol)
Calculate pseudo header checksum for IPv6.
Doxygen introspection did not find any typical Config paths.
uint16_t GetId() const
Get the ID of the packet.
static const uint8_t MAX_ANYCAST_DELAY_TIME
Neighbor Discovery node constants : max anycast delay.
uint32_t GetNsDadUid() const
Get the latest DAD probe packet UID.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
Doxygen introspection did not find any typical Config paths.
Definition: ipv6-header.h:33
bool Lookup(Ipv6Address dst, Ptr< NetDevice > device, Ptr< NdiscCache > cache, Address *hardwareDestination)
Lookup in the ND cache for the IPv6 address.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void MarkIncomplete(Ptr< Packet > p)
Changes the state to this entry to INCOMPLETE.
Definition: ndisc-cache.cc:427
void SetStream(int64_t stream)
Specifies the stream number for this RNG stream.
Doxygen introspection did not find any typical Config paths.
void SetState(Ipv6Address address, Ipv6InterfaceAddress::State_e state)
Update state of an interface address.
Hold a bool native type.
Definition: boolean.h:38
Doxygen introspection did not find any typical Config paths.
Doxygen introspection did not find any typical Config paths.
void SendNA(Ipv6Address src, Ipv6Address dst, Address *hardwareAddress, uint8_t flags)
Send a Neighbor Adverstisement.
void HandleNS(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Neighbor Solicitation method.
virtual uint32_t GetSerializedSize() const
Get the serialized size.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
Address GetMacAddress() const
Get the MAC address of this entry.
Definition: ndisc-cache.cc:508
NdiscCache::Entry * Add(Ipv6Address to)
Add an entry.
Definition: ndisc-cache.cc:101
static const uint8_t MAX_INITIAL_RTR_ADVERTISEMENTS
Neighbor Discovery router constants : max initial RA transmission.
Ptr< Packet > GetPacket() const
Get the incorrect packet.
Ipv6Address GetTarget() const
Get the IPv6 target address.
Access to the IPv6 forwarding table, interfaces, and configuration.
Definition: ipv6.h:80
hold variables of type string
Definition: string.h:18
virtual uint32_t GetSerializedSize() const
Get the serialized size.
static const uint8_t MAX_FINAL_RTR_ADVERTISEMENTS
Neighbor Discovery router constants : max final RA transmission.
void Send(Ptr< Packet > packet, Ipv6Address source, Ipv6Address destination, uint8_t protocol, Ptr< Ipv6Route > route)
Higher-level layers call this method to send a packet down the stack to the MAC and PHY layers...
Doxygen introspection did not find any typical Config paths.
IPv6 layer implementation.
Ptr< Node > m_node
The node.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:841
Doxygen introspection did not find any typical Config paths.
void SendRS(Ipv6Address src, Ipv6Address dst, Address hardwareAddress)
Send a Router Solicitation.
Doxygen introspection did not find any typical Config paths.
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
uint64_t GetUid(void) const
Returns the packet's Uid.
Definition: packet.cc:393
uint8_t GetType() const
Get the type field.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1018
#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
virtual void ReceiveIcmp(Ipv4Address icmpSource, uint8_t icmpTtl, uint8_t icmpType, uint8_t icmpCode, uint32_t icmpInfo, Ipv4Address payloadSource, Ipv4Address payloadDestination, const uint8_t payload[8])
Called from lower-level layers to send the ICMP packet up in the stack.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Ipv6InterfaceAddress GetAddress(uint32_t index) const
Get an address from IPv6 interface.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
IPv6 address associated with an interface.
void SetPtr(uint32_t ptr)
Set the pointer field.
static const uint8_t PROT_NUMBER
ICMPv6 protocol number (58).
virtual IpL4Protocol::DownTargetCallback6 GetDownTarget6(void) const
This method allows a caller to get the current down target callback set for this L4 protocol (IPv6 ca...
bool GetFlagS() const
Get the S flag.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:744
Invalid state (after a DAD failed)
bool IsEqual(const Ipv6Address &other) const
Comparison operation between two Ipv6Addresses.
virtual ~Icmpv6L4Protocol()
Destructor.
static const uint8_t RTR_SOLICITATION_INTERVAL
Neighbor Discovery host constants : RS interval.
void StopReachableTimer()
Stop the reachable timer.
Definition: ndisc-cache.cc:375
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
static const uint8_t MAX_RTR_SOLICITATIONS
Neighbor Discovery host constants : max RS transmission.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
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:825
void SetMtu(uint32_t mtu)
Set the MTU.
void SetNode(Ptr< Node > node)
Set the node.
void SetPacket(Ptr< Packet > p)
Set the incorrect packet.
This class implements a tag that carries the socket-specific TTL of a packet to the IP layer...
Definition: socket.h:996
void SetTarget(Ipv6Address target)
Set the IPv6 target address.
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition: socket.h:82
virtual int GetProtocolNumber() const
Get the protocol number.
std::list< Ptr< Packet > > MarkReachable(Address mac)
Changes the state to this entry to REACHABLE.
Definition: ndisc-cache.cc:438
Icmpv6L4Protocol()
Constructor.
uint16_t GetLifeTime() const
Get the node Life time (Neighbor Discovery).
virtual double GetValue(void)=0
Returns a random double from the underlying distribution.
a polymophic address class
Definition: address.h:86
void SetPacket(Ptr< Packet > p)
Set the incorrect packet.
static void FunctionDadTimeout(Ptr< Icmpv6L4Protocol > icmpv6, Ipv6Interface *interface, Ipv6Address addr)
Function called when DAD timeout.
virtual enum IpL4Protocol::RxStatus Receive(Ptr< Packet > p, Ipv4Header const &header, Ptr< Ipv4Interface > interface)
Receive method.
void SendErrorTooBig(Ptr< Packet > malformedPacket, Ipv6Address dst, uint32_t mtu)
Send an error Too Big.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Ptr< Ipv6Interface > GetInterface() const
Get the Ipv6Interface associated with this cache.
Definition: ndisc-cache.cc:77
void NotifyNewAggregate()
This method is called by AddAgregate and completes the aggregation by setting the node in the ICMPv6 ...
bool IsDelay() const
Is the entry DELAY.
Definition: ndisc-cache.cc:490
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
Definition: packet.cc:228
Packet header for IPv4.
Definition: ipv4-header.h:31
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:317
bool GetFlagR() const
Get the R flag.
The IPv6 representation of a network interface.
Doxygen introspection did not find any typical Config paths.
void HandleRedirection(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Redirection method.
Ipv6Address GetAddress() const
Get the IPv6 address.
Ipv6Address GetIpv6Target() const
Get the IPv6 target field.
Doxygen introspection did not find any typical Config paths.
Definition: icmpv6-header.h:37
uint32_t GetNAddresses(void) const
Get number of addresses on this IPv6 interface.
static const uint32_t MAX_RA_DELAY_TIME
Neighbor Discovery router constants : max delay between RA.
void Forward(Ipv6Address source, Icmpv6Header icmp, uint32_t info, Ipv6Header ipHeader, const uint8_t payload[8])
Notify an ICMPv6 reception to upper layers (if requested).
void SendErrorParameterError(Ptr< Packet > malformedPacket, Ipv6Address dst, uint8_t code, uint32_t ptr)
Send an error Parameter Error.
Ptr< Packet > GetPacket() const
Get the incorrect packet.
void SetTtl(uint8_t ttl)
Set the tag's TTL.
Definition: socket.cc:582
void SetIpv6Target(Ipv6Address target)
Set the IPv6 target field.
void HandleTimeExceeded(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Time Exceeded method.
void Flush()
Flush the cache.
Definition: ndisc-cache.cc:128
void SendErrorTimeExceeded(Ptr< Packet > malformedPacket, Ipv6Address dst, uint8_t code)
Send an error Time Exceeded.
Doxygen introspection did not find any typical Config paths.
static Ipv6Address GetZero()
Get the 0 (::) Ipv6Address.
uint8_t data[writeSize]
bool IsAlwaysDad() const
Is the node must do DAD.
T * PeekPointer(const Ptr< T > &p)
Definition: ptr.h:280
uint8_t GetLength(void) const
Get the length of the underlying address.
Definition: address.cc:75
static const uint32_t REACHABLE_TIME
Neighbor Discovery node constants : reachable time.
Doxygen introspection did not find any typical Config paths.
Ptr< Packet > GetPacket() const
Get the incorrect packet.
static const double MIN_RANDOM_FACTOR
Neighbor Discovery node constants : min random factor.
Ptr< NdiscCache > FindCache(Ptr< NetDevice > device)
Get the cache corresponding to the device.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
virtual Ptr< NetDevice > GetDevice() const
Get the NetDevice.
Ptr< Packet > ForgeNA(Ipv6Address src, Ipv6Address dst, Address *hardwareAddress, uint8_t flags)
Forge a Neighbor Advertisement.
void SetPacket(Ptr< Packet > p)
Set the incorrect packet.
Ptr< RandomVariableStream > m_solicitationJitter
Random jitter before sending solicitations.
bool GetFlagO() const
Get the O flag.
void HandleRS(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Router Solicitation method.
void Send(Ptr< Packet > p, Ipv6Address dest)
Send a packet through this interface.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:233
void StopProbeTimer()
Stop probe timer.
Definition: ndisc-cache.cc:389
Ptr< Packet > ForgeNS(Ipv6Address src, Ipv6Address dst, Ipv6Address target, Address hardwareAddress)
Forge a Neighbor Solicitation.
void StartRetransmitTimer()
Start retransmit timer.
Definition: ndisc-cache.cc:412
#define list
uint16_t GetSeq() const
Get the sequence number.
void SetNsDadUid(Ipv6Address address, uint32_t uid)
Update NS DAD packet UID of an interface address.
void ClearWaitingPacket()
Clear the waiting packet list.
Definition: ndisc-cache.cc:191
virtual uint32_t GetSerializedSize() const
Get the serialized size.
void HandleEchoRequest(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Echo Request method.
void HandleParameterError(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Parameter Error method.
virtual void NotifyNewAggregate(void)
This method is invoked whenever two sets of objects are aggregated together.
Definition: object.cc:314
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
static const uint8_t MAX_MULTICAST_SOLICIT
Neighbor Discovery node constants : max multicast solicitations.
bool IsProbe() const
Is the entry PROBE.
Definition: ndisc-cache.cc:502
uint8_t GetHopLimit(void) const
Get the "Hop limit" field (TTL).
Definition: ipv6-header.cc:91
void SendErrorDestinationUnreachable(Ptr< Packet > malformedPacket, Ipv6Address dst, uint8_t code)
Send an error Destination Unreachable.
void SendEchoReply(Ipv6Address src, Ipv6Address dst, uint16_t id, uint16_t seq, Ptr< Packet > data)
Send a Echo Reply.
static TypeId GetTypeId()
Interface ID.
static const uint8_t MAX_NEIGHBOR_ADVERTISEMENT
Neighbor Discovery node constants : max NA transmission.
virtual IpL4Protocol::DownTargetCallback GetDownTarget(void) const
This method allows a caller to get the current down target callback set for this L4 protocol (IPv4 ca...
static uint16_t GetStaticProtocolNumber()
Get ICMPv6 protocol number.
void SetCode(uint8_t code)
Set the code field.
Ipv6InterfaceAddress::State_e GetState() const
Get the address state.
Doxygen introspection did not find any typical Config paths.
uint8_t GetCode() const
Get the code field.
void AddWaitingPacket(Ptr< Packet > p)
Add a packet (or replace old value) in the queue.
Definition: ndisc-cache.cc:178
void StartDelayTimer()
Start delay timer.
Definition: ndisc-cache.cc:397
IpL4Protocol::DownTargetCallback6 m_downTarget
callback to Ipv6::Send
static const uint8_t MAX_INITIAL_RTR_ADVERT_INTERVAL
Neighbor Discovery router constants : max initial RA initial interval.
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
static Ipv6Address GetAllNodesMulticast()
Get the "all nodes multicast" address.
void StartReachableTimer()
Start the reachable timer.
Definition: ndisc-cache.cc:367
bool m_alwaysDad
Always do DAD ?
bool IsIncomplete() const
Is the entry INCOMPLETE.
Definition: ndisc-cache.cc:496
L4 Protocol abstract base class.
void SendMessage(Ptr< Packet > packet, Ipv6Address src, Ipv6Address dst, uint8_t ttl)
Send a packet via ICMPv6, note that packet already contains ICMPv6 header.
virtual void SetDownTarget(IpL4Protocol::DownTargetCallback cb)
This method allows a caller to set the current down target callback set for this L4 protocol (IPv4 ca...
Ptr< Packet > ForgeRS(Ipv6Address src, Ipv6Address dst, Address hardwareAddress)
Forge a Router Solicitation.
void Remove(NdiscCache::Entry *entry)
Delete an entry.
Definition: ndisc-cache.cc:112
static const uint8_t MIN_DELAY_BETWEEN_RAS
Neighbor Discovery router constants : min delay between RA.
void SetFlagR(bool r)
Set the R flag.
virtual uint32_t GetSerializedSize() const
Get the serialized size.
void SendRedirection(Ptr< Packet > redirectedPacket, Ipv6Address src, Ipv6Address dst, Ipv6Address redirTarget, Ipv6Address redirDestination, Address redirHardwareTarget)
Send an ICMPv6 Redirection.
std::list< Ptr< Packet > > MarkStale(Address mac)
Changes the state to this entry to STALE.
Definition: ndisc-cache.cc:464
void SetMacAddress(Address mac)
Set the MAC address of this entry.
Definition: ndisc-cache.cc:514
static const double MAX_RANDOM_FACTOR
Neighbor Discovery node constants : max random factor.
Doxygen introspection did not find any typical Config paths.
void SetFlagS(bool s)
Set the S flag.
CacheList m_cacheList
A list of cache by device.
void DoDAD(Ipv6Address target, Ptr< Ipv6Interface > interface)
Do the Duplication Address Detection (DAD).
virtual void SetDownTarget6(IpL4Protocol::DownTargetCallback6 cb)
This method allows a caller to set the current down target callback set for this L4 protocol (IPv6 ca...
Ipv6InterfaceAddress GetLinkLocalAddress() const
Get link-local address from IPv6 interface.
Describes an IPv6 address.
Definition: ipv6-address.h:46
NdiscCache::Entry * Lookup(Ipv6Address dst)
Lookup in the cache.
Definition: ndisc-cache.cc:89
void HandleRA(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Router Advertisement method.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:203
void ReceiveLLA(Icmpv6OptionLinkLayerAddress lla, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Link layer address option processing.
void MarkDelay()
Change the state to this entry to DELAY.
Definition: ndisc-cache.cc:472
virtual int GetVersion() const
Get the version of the protocol.
uint32_t GetSerializedSize(void) const
Returns number of bytes required for packet serialization.
Definition: packet.cc:565
static const uint32_t RETRANS_TIMER
Neighbor Discovery node constants : retransmission timer.
void SetDestination(Ipv6Address destination)
Set the IPv6 destination address.
void StopRetransmitTimer()
Stop retransmit timer.
Definition: ndisc-cache.cc:420
Ptr< Packet > ForgeEchoRequest(Ipv6Address src, Ipv6Address dst, uint16_t id, uint16_t seq, Ptr< Packet > data)
Forge an Echo Request.
Ipv6Address GetSourceAddress(void) const
Get the "Source address" field.
Definition: ipv6-header.cc:101
void StopDelayTimer()
Stop delay timer.
Definition: ndisc-cache.cc:405
void SetDevice(Ptr< NetDevice > device, Ptr< Ipv6Interface > interface)
Set the device and interface.
Definition: ndisc-cache.cc:70
Describes an IPv6 prefix.
Definition: ipv6-address.h:387
virtual void DoDispose()
Dispose this object.
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1022
Ipv6Address GetDestination() const
Get the IPv6 destination address.
Ipv6Address GetIpv6Target() const
Get the IPv6 target field.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
Definition: packet.cc:381
static const uint8_t MAX_RTR_SOLICITATION_DELAY
Neighbor Discovery host constants : max RS delay.
A record that holds information about an NdiscCache entry.
Definition: ndisc-cache.h:131
RxStatus
Rx status codes.
Address is tentative but we are optimistic so we can send packet even if DAD is not yet finished...
void HandleNA(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Neighbor Advertisement method.
void SetRouter(bool router)
Set the node type.
Definition: ndisc-cache.cc:166
static const uint8_t DELAY_FIRST_PROBE_TIME
Neighbor Discovery node constants : delay for the first probe.
virtual uint32_t GetSerializedSize() const
Get the serialized size.
void SetSeq(uint16_t seq)
Set the sequence number.
void HandlePacketTooBig(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Packet Too Big method.
bool IsReachable() const
Is the entry REACHABLE.
Definition: ndisc-cache.cc:484
void SetPacket(Ptr< Packet > p)
Set the incorrect packet.
uint32_t GetMtu() const
Get the MTU field.
Ptr< T > GetObject(void) const
Definition: object.h:362
Doxygen introspection did not find any typical Config paths.
a unique identifier for an interface.
Definition: type-id.h:49
Ptr< Packet > GetPacket() const
Get the incorrect packet.
Ipv6InterfaceAddress GetAddressMatchingDestination(Ipv6Address dst)
Get an address which is in the same network prefix as destination.
virtual uint32_t GetSerializedSize() const
Get the serialized size.
void SetFlagO(bool o)
Set the O flag.
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated with this cache.
Definition: ndisc-cache.cc:83
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
void Dispose(void)
Run the DoDispose methods of this object and all the objects aggregated to it.
Definition: object.cc:204
void SendNS(Ipv6Address src, Ipv6Address dst, Ipv6Address target, Address hardwareAddress)
Send a Neighbor Solicitation.
Address is tentative, no packet can be sent unless DAD finished.
bool IsStale() const
Is the entry STALE.
Definition: ndisc-cache.cc:478
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
Ipv6Address GetDestinationAddress(void) const
Get the "Destination address" field.
Definition: ipv6-header.cc:111
Ptr< NdiscCache > CreateCache(Ptr< NetDevice > device, Ptr< Ipv6Interface > interface)
Create a neighbor cache.
void SetId(uint16_t id)
Set the ID of the packet.
void HandleDestinationUnreachable(Ptr< Packet > p, Ipv6Address const &src, Ipv6Address const &dst, Ptr< Ipv6Interface > interface)
Receive Destination Unreachable method.
static Ipv6Address MakeSolicitedAddress(Ipv6Address addr)
Make the solicited IPv6 address.
static Ipv6Address GetAllRoutersMulticast()
Get the "all routers multicast" address.
static const uint8_t MAX_UNICAST_SOLICIT
Neighbor Discovery node constants : max unicast solicitations.