|
|
|
|
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/ptr.h" |
| 26 |
#include "ns3/assert.h" |
| 27 |
#include "ns3/packet.h" |
| 28 |
#include "ns3/node.h" |
| 29 |
#include "ns3/boolean.h" |
| 30 |
#include "ns3/ipv6-routing-protocol.h" |
| 31 |
#include "ns3/ipv6-route.h" |
| 32 |
#include "ns3/mac64-address.h" |
| 33 |
#include "ns3/mac48-address.h" |
| 34 |
#include "ns3/mac16-address.h" |
| 35 |
#include "ns3/pointer.h" |
| 36 |
#include "ns3/uinteger.h" |
| 37 |
#include "ns3/string.h" |
| 38 |
#include "ns3/sgi-hashmap.h" |
| 39 |
#include "ns3/ipv6-l3-protocol.h" |
| 40 |
#include "ns3/ipv6-interface.h" |
| 41 |
|
| 42 |
#include "sixlowpan-nd-protocol.h" |
| 43 |
#include "sixlowpan-ndisc-cache.h" |
| 44 |
#include "sixlowpan-ndisc-ra-options.h" |
| 45 |
#include "sixlowpan-nd-header.h" |
| 46 |
#include "sixlowpan-net-device.h" |
| 47 |
#include "src/network/model/packet.h" |
| 48 |
#include "src/core/model/integer.h" |
| 49 |
#include "src/core/model/nstime.h" |
| 50 |
|
| 51 |
namespace ns3 { |
| 52 |
|
| 53 |
NS_LOG_COMPONENT_DEFINE ("SixLowPanNdProtocol"); |
| 54 |
|
| 55 |
NS_OBJECT_ENSURE_REGISTERED (SixLowPanNdProtocol); |
| 56 |
|
| 57 |
const uint16_t SixLowPanNdProtocol::MIN_CONTEXT_CHANGE_DELAY = 300; |
| 58 |
|
| 59 |
const uint8_t SixLowPanNdProtocol::MAX_RTR_ADVERTISEMENTS = 3; |
| 60 |
const uint8_t SixLowPanNdProtocol::MIN_DELAY_BETWEEN_RAS = 10; |
| 61 |
const uint8_t SixLowPanNdProtocol::MAX_RA_DELAY_TIME = 2; |
| 62 |
const uint8_t SixLowPanNdProtocol::TENTATIVE_NCE_LIFETIME = 20; |
| 63 |
|
| 64 |
const uint8_t SixLowPanNdProtocol::MULTIHOP_HOPLIMIT = 64; |
| 65 |
|
| 66 |
const uint8_t SixLowPanNdProtocol::RTR_SOLICITATION_INTERVAL = 10; |
| 67 |
const uint8_t SixLowPanNdProtocol::MAX_RTR_SOLICITATIONS = 3; |
| 68 |
const uint8_t SixLowPanNdProtocol::MAX_RTR_SOLICITATION_INTERVAL = 60; |
| 69 |
|
| 70 |
SixLowPanNdProtocol::SixLowPanNdProtocol () |
| 71 |
: m_rsRetransmit (0), |
| 72 |
m_aroRetransmit (0) |
| 73 |
{ |
| 74 |
NS_LOG_FUNCTION (this); |
| 75 |
} |
| 76 |
|
| 77 |
SixLowPanNdProtocol::~SixLowPanNdProtocol () |
| 78 |
{ |
| 79 |
NS_LOG_FUNCTION (this); |
| 80 |
} |
| 81 |
|
| 82 |
TypeId SixLowPanNdProtocol::GetTypeId () |
| 83 |
{ |
| 84 |
static TypeId tid = TypeId ("ns3::SixLowPanNdProtocol") |
| 85 |
.SetParent<Icmpv6L4Protocol> () |
| 86 |
.SetGroupName ("Internet") |
| 87 |
.AddConstructor<SixLowPanNdProtocol> () |
| 88 |
.AddAttribute ("Border", "Is the node a 6LoWPAN Border Router.", |
| 89 |
BooleanValue (false), |
| 90 |
MakeBooleanAccessor (&SixLowPanNdProtocol::m_border), |
| 91 |
MakeBooleanChecker ()) |
| 92 |
.AddAttribute ("RegistrationLifeTime", "The amount of time (units of 60 seconds) that the router should retain the NCE for the node.", |
| 93 |
UintegerValue (60), |
| 94 |
MakeUintegerAccessor (&SixLowPanNdProtocol::m_regTime), |
| 95 |
MakeUintegerChecker<uint16_t> ()) |
| 96 |
.AddAttribute ("AdvanceTime", "The advance to perform maintaining of RA's information and registration.", |
| 97 |
UintegerValue (5), |
| 98 |
MakeUintegerAccessor (&SixLowPanNdProtocol::m_advance), |
| 99 |
MakeUintegerChecker<uint16_t> ()) |
| 100 |
; |
| 101 |
return tid; |
| 102 |
} |
| 103 |
|
| 104 |
Ptr<Packet> SixLowPanNdProtocol::ForgeNA (Ipv6Address src, Ipv6Address dst, Ipv6Address target, Address hardwareAddress, uint8_t flags) |
| 105 |
{ |
| 106 |
NS_LOG_FUNCTION (this << src << dst << hardwareAddress << (uint32_t)flags); |
| 107 |
Ptr<Packet> p = Create<Packet> (); |
| 108 |
Ipv6Header ipHeader; |
| 109 |
Icmpv6NA na; |
| 110 |
Icmpv6OptionLinkLayerAddress llOption (0, hardwareAddress); /* we give our mac address in response */ |
| 111 |
|
| 112 |
NS_LOG_LOGIC ("Send NA ( from " << src << " to " << dst << " target " << target << ")"); |
| 113 |
|
| 114 |
/* forge the entire NA packet from IPv6 header to ICMPv6 link-layer option, so that the packet does not pass by Icmpv6L4Protocol::Lookup again */ |
| 115 |
|
| 116 |
p->AddHeader (llOption); |
| 117 |
na.SetIpv6Target (target); |
| 118 |
|
| 119 |
if ((flags & 1)) |
| 120 |
{ |
| 121 |
na.SetFlagO (true); |
| 122 |
} |
| 123 |
if ((flags & 2) && src != Ipv6Address::GetAny ()) |
| 124 |
{ |
| 125 |
na.SetFlagS (true); |
| 126 |
} |
| 127 |
if ((flags & 4)) |
| 128 |
{ |
| 129 |
na.SetFlagR (true); |
| 130 |
} |
| 131 |
|
| 132 |
na.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + na.GetSerializedSize (), PROT_NUMBER); |
| 133 |
p->AddHeader (na); |
| 134 |
|
| 135 |
ipHeader.SetSourceAddress (src); |
| 136 |
ipHeader.SetDestinationAddress (dst); |
| 137 |
ipHeader.SetNextHeader (PROT_NUMBER); |
| 138 |
ipHeader.SetPayloadLength (p->GetSize ()); |
| 139 |
ipHeader.SetHopLimit (255); |
| 140 |
|
| 141 |
p->AddHeader (ipHeader); |
| 142 |
|
| 143 |
return p; |
| 144 |
} |
| 145 |
|
| 146 |
void SixLowPanNdProtocol::SendNS (Ipv6Address src, Ipv6Address dst, Ipv6Address target, |
| 147 |
Address linkAddr) |
| 148 |
{ |
| 149 |
NS_LOG_FUNCTION (this << src << dst << target << linkAddr); |
| 150 |
|
| 151 |
Ptr<Ipv6L3Protocol> ipv6 = GetNode ()->GetObject<Ipv6L3Protocol> (); |
| 152 |
uint32_t interfaceId = ipv6->GetInterfaceForAddress (src); |
| 153 |
Ptr<Ipv6Interface> interface = ipv6->GetInterface (interfaceId); |
| 154 |
Ptr<NdiscCache> cache = FindCache (interface->GetDevice ()); |
| 155 |
|
| 156 |
if (src != Ipv6Address::GetAny ()) |
| 157 |
{ |
| 158 |
NS_LOG_WARN ("An unspecified source address MUST NOT be used in NS messages."); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
// only send NS if the target is a router. |
| 163 |
NdiscCache::Entry *entry = cache->Lookup (dst); |
| 164 |
if (entry) |
| 165 |
{ |
| 166 |
if (entry->IsRouter ()) |
| 167 |
{ |
| 168 |
Icmpv6L4Protocol::SendNS (interface->GetLinkLocalAddress ().GetAddress (), dst, |
| 169 |
target, linkAddr); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void SixLowPanNdProtocol::SendSixLowPanARO (Ipv6Address src, Ipv6Address dst, uint16_t time, |
| 175 |
Mac64Address eui, Address linkAddr) |
| 176 |
{ |
| 177 |
NS_LOG_FUNCTION (this << src << dst << time << eui << linkAddr); |
| 178 |
|
| 179 |
/* an unspecified src MUST NOT be used in NS */ |
| 180 |
|
| 181 |
NS_ASSERT_MSG (src != Ipv6Address::GetAny (), "An unspecified source address MUST NOT be used in ARO messages."); |
| 182 |
NS_ASSERT_MSG (!dst.IsMulticast (), "Destination address must not be a multicast address in ARO messages."); |
| 183 |
|
| 184 |
Ptr<Packet> p = Create<Packet> (); |
| 185 |
Icmpv6NS ns (Ipv6Address::GetZero ()); |
| 186 |
|
| 187 |
/* ARO (request) + SLLAO */ |
| 188 |
Icmpv6OptionAddressRegistration arOption (time, eui); |
| 189 |
Icmpv6OptionLinkLayerAddress llOption (1, linkAddr); |
| 190 |
|
| 191 |
NS_LOG_LOGIC ("Send NS ( from " << src << " to " << dst << ")"); |
| 192 |
|
| 193 |
p->AddHeader (arOption); |
| 194 |
p->AddHeader (llOption); |
| 195 |
ns.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + ns.GetSerializedSize (), PROT_NUMBER); |
| 196 |
p->AddHeader (ns); |
| 197 |
|
| 198 |
SendMessage (p, src, dst, 255); |
| 199 |
} |
| 200 |
|
| 201 |
void SixLowPanNdProtocol::SendSixLowPanARO (Ipv6Address src, Ipv6Address dst, uint8_t status, |
| 202 |
uint16_t time, Mac64Address eui) |
| 203 |
{ |
| 204 |
NS_LOG_FUNCTION (this << src << dst << static_cast<uint32_t> (status) << time << eui); |
| 205 |
Ptr<Packet> p = Create<Packet> (); |
| 206 |
Icmpv6NA na; |
| 207 |
|
| 208 |
/* ARO (response) */ |
| 209 |
Icmpv6OptionAddressRegistration arOption (status, time, eui); |
| 210 |
|
| 211 |
NS_LOG_LOGIC ("Send NA ( from " << src << " to " << dst << ")"); |
| 212 |
|
| 213 |
na.SetIpv6Target (Ipv6Address::GetZero ()); |
| 214 |
na.SetFlagO (false); |
| 215 |
na.SetFlagS (true); |
| 216 |
na.SetFlagR (true); |
| 217 |
|
| 218 |
p->AddHeader (arOption); |
| 219 |
na.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + na.GetSerializedSize (), PROT_NUMBER); |
| 220 |
p->AddHeader (na); |
| 221 |
|
| 222 |
SendMessage (p, src, dst, 255); |
| 223 |
} |
| 224 |
|
| 225 |
void SixLowPanNdProtocol::SendSixLowPanRA (Ipv6Address src, Ipv6Address dst, Ptr<Ipv6Interface> interface) |
| 226 |
{ |
| 227 |
NS_LOG_FUNCTION (this << src << dst << interface); |
| 228 |
|
| 229 |
Ptr<SixLowPanNdiscCache> sixCache = FindSixLowPanCache (interface->GetDevice ()); |
| 230 |
std::map<Ipv6Address, SixLowPanNdiscCache::SixLowPanRaEntry *> raCache = sixCache->GetRaCache (); |
| 231 |
|
| 232 |
for (std::map<Ipv6Address, SixLowPanNdiscCache::SixLowPanRaEntry *>::iterator it = raCache.begin (); |
| 233 |
it != raCache.end (); it++) |
| 234 |
{ |
| 235 |
Icmpv6RA raHdr; |
| 236 |
Ipv6Header ipHeader; |
| 237 |
|
| 238 |
Ptr<Packet> p = Create<Packet> (); |
| 239 |
|
| 240 |
/* set RA header information */ |
| 241 |
raHdr.SetFlagM (it->second->IsManagedFlag ()); |
| 242 |
raHdr.SetFlagO (it->second->IsOtherConfigFlag ()); |
| 243 |
raHdr.SetFlagH (it->second->IsHomeAgentFlag ()); |
| 244 |
raHdr.SetCurHopLimit (it->second->GetCurHopLimit ()); |
| 245 |
raHdr.SetLifeTime (it->second->GetRouterLifeTime ()); |
| 246 |
raHdr.SetReachableTime (it->second->GetReachableTime ()); |
| 247 |
raHdr.SetRetransmissionTime (it->second->GetRetransTimer ()); |
| 248 |
|
| 249 |
/* Add ABRO */ |
| 250 |
Icmpv6OptionAuthoritativeBorderRouter abroHdr; |
| 251 |
abroHdr.SetVersion (it->second->GetVersion ()); |
| 252 |
abroHdr.SetValidTime (it->second->GetValidTime ()); |
| 253 |
abroHdr.SetRouterAddress (it->second->GetBorderAddress ()); |
| 254 |
p->AddHeader (abroHdr); |
| 255 |
|
| 256 |
/* Add SLLAO */ |
| 257 |
Address addr = interface->GetDevice ()->GetAddress (); |
| 258 |
Icmpv6OptionLinkLayerAddress llaHdr (1, addr); |
| 259 |
p->AddHeader (llaHdr); |
| 260 |
|
| 261 |
/* Add PIOs */ |
| 262 |
std::map<Ipv6Address, Ptr<SixLowPanPrefix> > prefixes = it->second->GetPrefixes (); |
| 263 |
for (std::map<Ipv6Address, Ptr<SixLowPanPrefix> >::iterator jt = prefixes.begin (); jt != prefixes.end (); jt++) |
| 264 |
{ |
| 265 |
Icmpv6OptionPrefixInformation prefixHdr; |
| 266 |
prefixHdr.SetPrefixLength (jt->second->GetPrefixLength ()); |
| 267 |
prefixHdr.SetFlags (jt->second->GetFlags ()); |
| 268 |
prefixHdr.SetValidTime (jt->second->GetValidLifeTime ()); |
| 269 |
prefixHdr.SetPreferredTime (jt->second->GetPreferredLifeTime ()); |
| 270 |
prefixHdr.SetPrefix (jt->second->GetPrefix ()); |
| 271 |
p->AddHeader (prefixHdr); |
| 272 |
} |
| 273 |
|
| 274 |
/* Add 6COs */ |
| 275 |
std::map<uint8_t, Ptr<SixLowPanContext> > contexts = it->second->GetContexts (); |
| 276 |
for (std::map<uint8_t, Ptr<SixLowPanContext> >::iterator i = contexts.begin (); i != contexts.end (); i++) |
| 277 |
{ |
| 278 |
Icmpv6OptionSixLowPanContext sixHdr; |
| 279 |
sixHdr.SetContextLen (i->second->GetContextLen ()); |
| 280 |
sixHdr.SetFlagC (i->second->IsFlagC ()); |
| 281 |
sixHdr.SetCid (i->second->GetCid ()); |
| 282 |
sixHdr.SetValidTime (i->second->GetValidTime ()); |
| 283 |
sixHdr.SetContextPrefix (i->second->GetContextPrefix ()); |
| 284 |
p->AddHeader (sixHdr); |
| 285 |
} |
| 286 |
|
| 287 |
raHdr.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + raHdr.GetSerializedSize (), PROT_NUMBER); |
| 288 |
p->AddHeader (raHdr); |
| 289 |
|
| 290 |
ipHeader.SetSourceAddress (src); |
| 291 |
ipHeader.SetDestinationAddress (dst); |
| 292 |
ipHeader.SetNextHeader (PROT_NUMBER); |
| 293 |
ipHeader.SetPayloadLength (p->GetSize ()); |
| 294 |
ipHeader.SetHopLimit (255); |
| 295 |
|
| 296 |
/* send RA */ |
| 297 |
NS_LOG_LOGIC ("Send RA to " << dst); |
| 298 |
interface->Send (p, ipHeader, dst); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
void SixLowPanNdProtocol::SendSixLowPanDAR (Ipv6Address src, Ipv6Address dst, uint16_t time, Mac64Address eui, |
| 303 |
Ipv6Address registered) |
| 304 |
{ |
| 305 |
NS_LOG_FUNCTION (this << src << dst << time << eui << registered); |
| 306 |
Ptr<Packet> p = Create<Packet> (); |
| 307 |
Icmpv6DuplicateAddress dar (time, eui, registered); |
| 308 |
|
| 309 |
NS_LOG_LOGIC ("Send DAR ( from " << src << " to " << dst << ")"); |
| 310 |
|
| 311 |
dar.CalculatePseudoHeaderChecksum (src, dst, p->GetSize () + dar.GetSerializedSize (), PROT_NUMBER); |
| 312 |
p->AddHeader (dar); |
| 313 |
|
| 314 |
SendMessage (p, src, dst, MULTIHOP_HOPLIMIT); |
| 315 |
} |
| 316 |
|
| 317 |
enum IpL4Protocol::RxStatus SixLowPanNdProtocol::Receive (Ptr<Packet> packet, Ipv6Header const &header, |
| 318 |
Ptr<Ipv6Interface> interface) |
| 319 |
{ |
| 320 |
NS_LOG_FUNCTION (this << packet << header.GetSourceAddress () << header.GetDestinationAddress () << |
| 321 |
interface); |
| 322 |
Ptr<Ipv6> ipv6 = GetNode ()->GetObject<Ipv6> (); |
| 323 |
Ptr<SixLowPanNetDevice> sixDevice = DynamicCast<SixLowPanNetDevice> (interface->GetDevice()); |
| 324 |
NS_ASSERT_MSG (sixDevice != NULL, "SixLowPanNdProtocol cannot be installed on device different from SixLowPanNetDevice"); |
| 325 |
|
| 326 |
uint8_t type; |
| 327 |
packet->CopyData (&type, sizeof(type)); |
| 328 |
|
| 329 |
switch (type) |
| 330 |
{ |
| 331 |
case Icmpv6Header::ICMPV6_ND_ROUTER_SOLICITATION: |
| 332 |
if (ipv6->IsForwarding (ipv6->GetInterfaceForDevice (sixDevice))) |
| 333 |
{ |
| 334 |
HandleSixLowPanRS (packet, header.GetSourceAddress (), header.GetDestinationAddress (), interface); |
| 335 |
} |
| 336 |
break; |
| 337 |
case Icmpv6Header::ICMPV6_ND_ROUTER_ADVERTISEMENT: |
| 338 |
HandleSixLowPanRA (packet, header.GetSourceAddress (), header.GetDestinationAddress (), interface); |
| 339 |
break; |
| 340 |
case Icmpv6Header::ICMPV6_ND_NEIGHBOR_SOLICITATION: |
| 341 |
if (ipv6->IsForwarding (ipv6->GetInterfaceForDevice (sixDevice))) |
| 342 |
{ |
| 343 |
HandleSixLowPanNS (packet, header.GetSourceAddress (), header.GetDestinationAddress (), interface); |
| 344 |
} |
| 345 |
break; |
| 346 |
case Icmpv6Header::ICMPV6_ND_NEIGHBOR_ADVERTISEMENT: |
| 347 |
HandleSixLowPanNA (packet, header.GetSourceAddress (), header.GetDestinationAddress (), interface); |
| 348 |
break; |
| 349 |
case Icmpv6Header::ICMPV6_ND_DUPLICATE_ADDRESS_CONFIRM: |
| 350 |
if (ipv6->IsForwarding (ipv6->GetInterfaceForDevice (sixDevice))) |
| 351 |
{ |
| 352 |
HandleSixLowPanDAC (packet, header.GetSourceAddress (), header.GetDestinationAddress (), interface); |
| 353 |
} |
| 354 |
break; |
| 355 |
default: |
| 356 |
return Icmpv6L4Protocol::Receive (packet, header, interface); |
| 357 |
break; |
| 358 |
} |
| 359 |
|
| 360 |
return IpL4Protocol::RX_OK; |
| 361 |
} |
| 362 |
|
| 363 |
void SixLowPanNdProtocol::DoDispose () |
| 364 |
{ |
| 365 |
NS_LOG_FUNCTION (this); |
| 366 |
|
| 367 |
for (SixLowPanCacheList::const_iterator it = m_sixlowCList.begin (); it != m_sixlowCList.end (); it++) |
| 368 |
{ |
| 369 |
Ptr<SixLowPanNdiscCache> cache = *it; |
| 370 |
cache->Dispose (); |
| 371 |
cache = 0; |
| 372 |
} |
| 373 |
m_sixlowCList.clear (); |
| 374 |
|
| 375 |
Icmpv6L4Protocol::DoDispose (); |
| 376 |
} |
| 377 |
|
| 378 |
void SixLowPanNdProtocol::HandleSixLowPanNS (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, |
| 379 |
Ptr<Ipv6Interface> interface) |
| 380 |
{ |
| 381 |
NS_LOG_FUNCTION (this << packet << src << dst << interface); |
| 382 |
|
| 383 |
Icmpv6NS nsHdr; |
| 384 |
packet->RemoveHeader (nsHdr); |
| 385 |
Ipv6Address target = nsHdr.GetIpv6Target (); |
| 386 |
|
| 387 |
Ptr<SixLowPanNetDevice> sixDevice = DynamicCast<SixLowPanNetDevice> (interface->GetDevice()); |
| 388 |
NS_ASSERT_MSG (sixDevice != NULL, "SixLowPanNdProtocol cannot be installed on device different from SixLowPanNetDevice"); |
| 389 |
|
| 390 |
Icmpv6OptionLinkLayerAddress llaHdr (1); /* SLLAO */ |
| 391 |
Icmpv6OptionAddressRegistration aroHdr; |
| 392 |
|
| 393 |
bool next = true; |
| 394 |
bool hasLla = false; |
| 395 |
bool hasAro = false; |
| 396 |
|
| 397 |
if (src != Ipv6Address::GetAny ()) |
| 398 |
{ |
| 399 |
/* search all options following the NS header */ |
| 400 |
while (next == true) |
| 401 |
{ |
| 402 |
uint8_t type; |
| 403 |
packet->CopyData (&type, sizeof(type)); |
| 404 |
|
| 405 |
switch (type) |
| 406 |
{ |
| 407 |
case Icmpv6Header::ICMPV6_OPT_LINK_LAYER_SOURCE: |
| 408 |
if (!hasLla) |
| 409 |
{ |
| 410 |
packet->RemoveHeader (llaHdr); |
| 411 |
hasLla = true; |
| 412 |
} |
| 413 |
break; |
| 414 |
case Icmpv6Header::ICMPV6_OPT_ADDRESS_REGISTRATION: |
| 415 |
if (!hasAro) |
| 416 |
{ |
| 417 |
packet->RemoveHeader (aroHdr); |
| 418 |
hasAro = true; |
| 419 |
} |
| 420 |
break; |
| 421 |
default: |
| 422 |
/* unknow option, quit */ |
| 423 |
next = false; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
if (!hasLla) /* error! */ |
| 428 |
{ |
| 429 |
NS_LOG_ERROR ("NS message MUST have source link layer option."); |
| 430 |
return; |
| 431 |
} |
| 432 |
else /* NS + SLLAO */ |
| 433 |
{ |
| 434 |
/* Update "ARPv6" table with information of src */ |
| 435 |
Ptr<NdiscCache> cache = FindCache (sixDevice); |
| 436 |
|
| 437 |
NdiscCache::Entry* entry = 0; |
| 438 |
entry = cache->Lookup (src); |
| 439 |
|
| 440 |
if (!entry && src.IsLinkLocal()) |
| 441 |
{ |
| 442 |
entry = cache->Add (src); |
| 443 |
uint8_t buf[16]; |
| 444 |
src.GetBytes (buf); |
| 445 |
Mac16Address address; |
| 446 |
address.CopyFrom (buf+14); |
| 447 |
entry->SetRouter (false); |
| 448 |
entry->SetMacAddress (llaHdr.GetAddress ()); |
| 449 |
entry->MarkReachable (); |
| 450 |
} |
| 451 |
else if (!entry) |
| 452 |
{ |
| 453 |
entry = cache->Add (src); |
| 454 |
entry->SetRouter (false); |
| 455 |
entry->MarkStale (llaHdr.GetAddress ()); |
| 456 |
} |
| 457 |
else if (entry->GetMacAddress () != llaHdr.GetAddress ()) |
| 458 |
{ |
| 459 |
entry->MarkStale (llaHdr.GetAddress ()); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
if (!hasAro) /* NS + SLLAO */ |
| 464 |
{ |
| 465 |
/* Send NA to src with information requested about target (in "ARPv6" table) */ |
| 466 |
Ptr<NdiscCache> cache = FindCache (sixDevice); |
| 467 |
|
| 468 |
NdiscCache::Entry* entry = 0; |
| 469 |
entry = cache->Lookup (target); |
| 470 |
uint8_t flags; |
| 471 |
|
| 472 |
if (!entry) |
| 473 |
{ |
| 474 |
NS_LOG_LOGIC ("No entry with address " << target); |
| 475 |
return; |
| 476 |
} |
| 477 |
else |
| 478 |
{ |
| 479 |
if (entry->IsRouter ()) |
| 480 |
{ |
| 481 |
flags = 7; /* R + S + O flag */ |
| 482 |
} |
| 483 |
else |
| 484 |
{ |
| 485 |
flags = 3; /* S + O flags */ |
| 486 |
} |
| 487 |
|
| 488 |
/* NA + TLLAO */ |
| 489 |
Ptr<Packet> p = ForgeNA (interface->GetLinkLocalAddress ().GetAddress (), src, target, |
| 490 |
entry->GetMacAddress (), flags); |
| 491 |
Ipv6Header header; |
| 492 |
p->RemoveHeader (header); |
| 493 |
interface->Send (p, header, src); |
| 494 |
} |
| 495 |
} |
| 496 |
else /* NS + SLLAO + ARO */ |
| 497 |
{ |
| 498 |
/* Update NCE with information of src and ARO */ |
| 499 |
Ptr<SixLowPanNdiscCache> nCache = FindSixLowPanCache (sixDevice); |
| 500 |
|
| 501 |
SixLowPanNdiscCache::SixLowPanEntry* nEntry = 0; |
| 502 |
nEntry = nCache->SixLowPanLookup (src); |
| 503 |
|
| 504 |
std::map<Ipv6Address, SixLowPanNdiscCache::SixLowPanRaEntry *> raCache = nCache->GetRaCache (); |
| 505 |
|
| 506 |
if (!nEntry) /* no entry, creating */ |
| 507 |
{ |
| 508 |
if (aroHdr.GetRegTime () != 0) |
| 509 |
{ |
| 510 |
nEntry = nCache->SixLowPanAdd (src); |
| 511 |
nEntry->SetRouter (false); |
| 512 |
nEntry->SetMacAddress (llaHdr.GetAddress ()); |
| 513 |
nEntry->SetEui64 (aroHdr.GetEui64 ()); |
| 514 |
nEntry->MarkReachable (); |
| 515 |
|
| 516 |
if (m_multihopDad) /* multihop DAD used */ |
| 517 |
{ |
| 518 |
nEntry->MarkTentative (); |
| 519 |
for (std::map<Ipv6Address, SixLowPanNdiscCache::SixLowPanRaEntry *>::iterator it = raCache.begin (); |
| 520 |
it != raCache.end (); it++) |
| 521 |
{ |
| 522 |
Ipv6Address destination = it->second->GetBorderAddress (); |
| 523 |
SendSixLowPanDAR (interface->GetAddressMatchingDestination (destination).GetAddress (), destination, |
| 524 |
aroHdr.GetRegTime (), aroHdr.GetEui64 (), src); |
| 525 |
} |
| 526 |
return; |
| 527 |
} |
| 528 |
else /* multihop DAD NOT used */ |
| 529 |
{ |
| 530 |
nEntry->MarkRegistered (aroHdr.GetRegTime ()); |
| 531 |
SendSixLowPanARO (interface->GetLinkLocalAddress ().GetAddress (), src, 0, aroHdr.GetRegTime (), |
| 532 |
aroHdr.GetEui64 ()); |
| 533 |
return; |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
else if (nEntry->GetEui64 () == aroHdr.GetEui64 ()) /* entry found, same EUI-64, updating */ |
| 538 |
{ |
| 539 |
if (aroHdr.GetRegTime () != 0) |
| 540 |
{ |
| 541 |
nEntry->SetRouter (false); |
| 542 |
nEntry->SetMacAddress (llaHdr.GetAddress ()); |
| 543 |
nEntry->MarkReachable (); |
| 544 |
nEntry->MarkRegistered (aroHdr.GetRegTime ()); |
| 545 |
SendSixLowPanARO (interface->GetLinkLocalAddress ().GetAddress (), src, 0, aroHdr.GetRegTime (), |
| 546 |
aroHdr.GetEui64 ()); |
| 547 |
|
| 548 |
if (m_multihopDad) |
| 549 |
{ |
| 550 |
for (std::map<Ipv6Address, SixLowPanNdiscCache::SixLowPanRaEntry *>::iterator it = raCache.begin (); |
| 551 |
it != raCache.end (); it++) |
| 552 |
{ |
| 553 |
Ipv6Address destination = it->second->GetBorderAddress (); |
| 554 |
|
| 555 |
/* Send request to update entry from DAD table */ |
| 556 |
SendSixLowPanDAR (interface->GetAddressMatchingDestination (destination).GetAddress (), destination, |
| 557 |
aroHdr.GetRegTime (), aroHdr.GetEui64 (), src); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
return; |
| 562 |
} |
| 563 |
else /* ARO's Registration Lifetime = 0 */ |
| 564 |
{ |
| 565 |
if (m_multihopDad) |
| 566 |
{ |
| 567 |
for (std::map<Ipv6Address, SixLowPanNdiscCache::SixLowPanRaEntry *>::iterator it = raCache.begin (); |
| 568 |
it != raCache.end (); it++) |
| 569 |
{ |
| 570 |
Ipv6Address destination = it->second->GetBorderAddress (); |
| 571 |
|
| 572 |
/* Send request to remove entry from DAD table */ |
| 573 |
SendSixLowPanDAR (interface->GetAddressMatchingDestination (destination).GetAddress (), destination, |
| 574 |
aroHdr.GetRegTime (), aroHdr.GetEui64 (), src); |
| 575 |
} |
| 576 |
} |
| 577 |
/* Remove the entry from Neighbor Cache */ |
| 578 |
nCache->SixLowPanRemove (nEntry); |
| 579 |
|
| 580 |
/* Remove the entry from "ARPv6" table */ |
| 581 |
Ptr<NdiscCache> cache = FindCache (sixDevice); |
| 582 |
|
| 583 |
NdiscCache::Entry* entry = 0; |
| 584 |
entry = cache->Lookup (src); |
| 585 |
cache->Remove (entry); |
| 586 |
return; |
| 587 |
} |
| 588 |
} |
| 589 |
else /* entry found, different EUI-64 */ |
| 590 |
{ |
| 591 |
if (m_multihopDad && nEntry->IsRegistered ()) |
| 592 |
{ |
| 593 |
SendSixLowPanARO (interface->GetLinkLocalAddress ().GetAddress (), |
| 594 |
sixDevice->MakeLinkLocalAddressFromMac (aroHdr.GetEui64 ()), 1, |
| 595 |
aroHdr.GetRegTime (), aroHdr.GetEui64 ()); |
| 596 |
} |
| 597 |
else if (!m_multihopDad) |
| 598 |
{ |
| 599 |
SendSixLowPanARO (interface->GetLinkLocalAddress ().GetAddress (), |
| 600 |
sixDevice->MakeLinkLocalAddressFromMac (aroHdr.GetEui64 ()), 1, |
| 601 |
aroHdr.GetRegTime (), aroHdr.GetEui64 ()); |
| 602 |
} |
| 603 |
return; |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
else |
| 608 |
{ |
| 609 |
NS_LOG_ERROR ("An unspecified source address MUST NOT be used in NS messages."); |
| 610 |
return; |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
void SixLowPanNdProtocol::HandleSixLowPanNA (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, |
| 615 |
Ptr<Ipv6Interface> interface) |
| 616 |
{ |
| 617 |
NS_LOG_FUNCTION (this << packet << src << dst << interface); |
| 618 |
|
| 619 |
Ptr<Packet> p = packet->Copy (); |
| 620 |
Icmpv6NS naHdr; |
| 621 |
packet->RemoveHeader (naHdr); |
| 622 |
Ipv6Address target = naHdr.GetIpv6Target (); |
| 623 |
|
| 624 |
Icmpv6OptionLinkLayerAddress llaHdr (0); /* TLLAO */ |
| 625 |
Icmpv6OptionAddressRegistration aroHdr; |
| 626 |
|
| 627 |
uint8_t type; |
| 628 |
packet->CopyData (&type, sizeof(type)); |
| 629 |
|
| 630 |
switch (type) |
| 631 |
{ |
| 632 |
case Icmpv6Header::ICMPV6_OPT_LINK_LAYER_TARGET: /* NA + TLLAO */ |
| 633 |
|
| 634 |
HandleNA (p, target, dst, interface); /* Handle response of Address Resolution */ |
| 635 |
|
| 636 |
break; |
| 637 |
case Icmpv6Header::ICMPV6_OPT_ADDRESS_REGISTRATION: /* NA + ARO */ |
| 638 |
|
| 639 |
packet->RemoveHeader (aroHdr); |
| 640 |
|
| 641 |
m_aroRetransmit = 0; |
| 642 |
|
| 643 |
if (aroHdr.GetStatus () == 0) /* status=0, success! */ |
| 644 |
{ |
| 645 |
/* schedule a new ARO to maintain NCE in routers */ |
| 646 |
|
| 647 |
Simulator::Schedule (Time (Minutes (aroHdr.GetRegTime () - m_advance)), &SixLowPanNdProtocol::RetransmitARO, this, |
| 648 |
dst, src, aroHdr.GetRegTime (), aroHdr.GetEui64 (), interface->GetDevice ()->GetAddress ()); |
| 649 |
} |
| 650 |
else /* status NOT 0, fail! */ |
| 651 |
{ |
| 652 |
NS_LOG_LOGIC ("ARO status is NOT 0, registration failed!"); |
| 653 |
/// \todo implement method to remove address that generated error. |
| 654 |
return; |
| 655 |
} |
| 656 |
break; |
| 657 |
default: |
| 658 |
NS_LOG_ERROR ("NA message MUST have option."); |
| 659 |
return; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
void SixLowPanNdProtocol::HandleSixLowPanRS (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, |
| 664 |
Ptr<Ipv6Interface> interface) |
| 665 |
{ |
| 666 |
NS_LOG_FUNCTION (this << packet << src << dst << interface); |
| 667 |
|
| 668 |
Ptr<SixLowPanNetDevice> sixDevice = DynamicCast<SixLowPanNetDevice> (interface->GetDevice()); |
| 669 |
NS_ASSERT_MSG (sixDevice != NULL, "SixLowPanNdProtocol cannot be installed on device different from SixLowPanNetDevice"); |
| 670 |
|
| 671 |
Icmpv6RS rsHeader; |
| 672 |
Icmpv6OptionLinkLayerAddress lla (1); |
| 673 |
|
| 674 |
packet->RemoveHeader (rsHeader); |
| 675 |
|
| 676 |
if (src != Ipv6Address::GetAny ()) |
| 677 |
{ |
| 678 |
uint8_t type; |
| 679 |
packet->CopyData (&type, sizeof(type)); |
| 680 |
|
| 681 |
if (type != Icmpv6Header::ICMPV6_OPT_LINK_LAYER_SOURCE) |
| 682 |
{ |
| 683 |
NS_LOG_ERROR ("RS message MUST have source link layer option."); |
| 684 |
return; |
| 685 |
} |
| 686 |
|
| 687 |
packet->RemoveHeader (lla); |
| 688 |
|
| 689 |
/* Update Neighbor Cache */ |
| 690 |
Ptr<SixLowPanNdiscCache> sixCache = FindSixLowPanCache (sixDevice); |
| 691 |
SixLowPanNdiscCache::SixLowPanEntry* sixEntry = 0; |
| 692 |
sixEntry = sixCache->SixLowPanLookup (src); |
| 693 |
if (!sixEntry) |
| 694 |
{ |
| 695 |
sixEntry = sixCache->SixLowPanAdd (src); |
| 696 |
sixEntry->SetRouter (false); |
| 697 |
sixEntry->MarkStale (lla.GetAddress ()); |
| 698 |
sixEntry->MarkTentative (); |
| 699 |
NS_LOG_LOGIC ("Tentative entry created from RS"); |
| 700 |
} |
| 701 |
else if (sixEntry->GetMacAddress () != lla.GetAddress ()) |
| 702 |
{ |
| 703 |
sixEntry->MarkStale (lla.GetAddress ()); |
| 704 |
} |
| 705 |
|
| 706 |
/* Update "ARPv6" table */ |
| 707 |
Ptr<NdiscCache> cache = FindCache (sixDevice); |
| 708 |
NdiscCache::Entry* entry = 0; |
| 709 |
entry = cache->Lookup (src); |
| 710 |
if (!entry) |
| 711 |
{ |
| 712 |
entry = cache->Add (src); |
| 713 |
entry->SetRouter (false); |
| 714 |
entry->MarkStale (lla.GetAddress ()); |
| 715 |
} |
| 716 |
else if (entry->GetMacAddress () != lla.GetAddress ()) |
| 717 |
{ |
| 718 |
entry->MarkStale (lla.GetAddress ()); |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
if (!m_border) |
| 723 |
{ |
| 724 |
Address addr = lla.GetAddress (); |
| 725 |
Ipv6Address destination = sixDevice->MakeLinkLocalAddressFromMac(addr); |
| 726 |
|
| 727 |
SendSixLowPanRA (interface->GetLinkLocalAddress ().GetAddress (), destination, interface); |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
void SixLowPanNdProtocol::HandleSixLowPanRA (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, |
| 732 |
Ptr<Ipv6Interface> interface) |
| 733 |
{ |
| 734 |
NS_LOG_FUNCTION (this << packet << src << dst << interface); |
| 735 |
|
| 736 |
m_rsRetransmit = 0; |
| 737 |
m_receivedRA = true; |
| 738 |
|
| 739 |
Ptr<SixLowPanNetDevice> sixDevice = DynamicCast<SixLowPanNetDevice> (interface->GetDevice()); |
| 740 |
NS_ASSERT_MSG (sixDevice != NULL, "SixLowPanNdProtocol cannot be installed on device different from SixLowPanNetDevice"); |
| 741 |
|
| 742 |
Address addr = sixDevice->GetAddress (); |
| 743 |
|
| 744 |
Icmpv6RA raHeader; |
| 745 |
Ptr<Ipv6L3Protocol> ipv6 = GetNode ()->GetObject<Ipv6L3Protocol> (); |
| 746 |
|
| 747 |
Icmpv6OptionAuthoritativeBorderRouter abrHdr; /* ABRO */ |
| 748 |
Icmpv6OptionLinkLayerAddress llaHdr (1); /* SLLAO */ |
| 749 |
|
| 750 |
bool next = true; |
| 751 |
Ptr<SixLowPanNdiscCache> sixCache = FindSixLowPanCache (sixDevice); |
| 752 |
SixLowPanNdiscCache::SixLowPanRaEntry* ra = 0; |
| 753 |
|
| 754 |
Ptr<NdiscCache> cache = FindCache (sixDevice); |
| 755 |
// sgi::hash_map<Ipv6Address, NdiscCache::Entry *, Ipv6AddressHash> ndiscCache = cache->GetNdiscCache (); |
| 756 |
|
| 757 |
uint32_t version; |
| 758 |
Ipv6Address border; |
| 759 |
|
| 760 |
packet->RemoveHeader (raHeader); |
| 761 |
|
| 762 |
Ipv6Address defaultRouter = Ipv6Address::GetZero (); |
| 763 |
|
| 764 |
if (raHeader.GetLifeTime()) |
| 765 |
{ |
| 766 |
defaultRouter = src; |
| 767 |
} |
| 768 |
|
| 769 |
std::list<Icmpv6OptionPrefixInformation> prefixList; |
| 770 |
std::list<Icmpv6OptionSixLowPanContext> contextList; |
| 771 |
Icmpv6OptionPrefixInformation prefixHdr; |
| 772 |
Icmpv6OptionSixLowPanContext contextHdr; |
| 773 |
|
| 774 |
// \todo check the stop condiiton |
| 775 |
while (next == true) |
| 776 |
{ |
| 777 |
uint8_t type = 0; |
| 778 |
packet->CopyData (&type, sizeof(type)); |
| 779 |
|
| 780 |
switch (type) |
| 781 |
{ |
| 782 |
case Icmpv6Header::ICMPV6_OPT_PREFIX: |
| 783 |
packet->RemoveHeader (prefixHdr); |
| 784 |
prefixList.push_back (prefixHdr); |
| 785 |
break; |
| 786 |
case Icmpv6Header::ICMPV6_OPT_SIXLOWPAN_CONTEXT: |
| 787 |
packet->RemoveHeader (contextHdr); |
| 788 |
contextList.push_back (contextHdr); |
| 789 |
break; |
| 790 |
case Icmpv6Header::ICMPV6_OPT_AUTHORITATIVE_BORDER_ROUTER: |
| 791 |
packet->RemoveHeader (abrHdr); |
| 792 |
version = abrHdr.GetVersion (); |
| 793 |
border = abrHdr.GetRouterAddress (); |
| 794 |
break; |
| 795 |
case Icmpv6Header::ICMPV6_OPT_LINK_LAYER_SOURCE: |
| 796 |
packet->RemoveHeader (llaHdr); |
| 797 |
ReceiveLLA (llaHdr, src, dst, interface); // generates an entry in NDISC table with m_router = true |
| 798 |
break; |
| 799 |
default: |
| 800 |
/* Unknown option, quit */ |
| 801 |
next = false; |
| 802 |
break; |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
ra = sixCache->RaEntryLookup (border); |
| 807 |
if (!ra) // Create a new entry |
| 808 |
{ |
| 809 |
ra = sixCache->AddRaEntry (border); |
| 810 |
ra->SetManagedFlag (raHeader.GetFlagM ()); |
| 811 |
ra->SetOtherConfigFlag (raHeader.GetFlagO ()); |
| 812 |
ra->SetHomeAgentFlag (raHeader.GetFlagH ()); |
| 813 |
ra->SetReachableTime (raHeader.GetReachableTime ()); |
| 814 |
ra->SetRouterLifeTime (raHeader.GetLifeTime ()); |
| 815 |
ra->SetRetransTimer (raHeader.GetRetransmissionTime ()); |
| 816 |
ra->SetCurHopLimit (raHeader.GetCurHopLimit ()); |
| 817 |
ra->SetVersion (version); |
| 818 |
ra->SetValidTime (abrHdr.GetValidTime ()); |
| 819 |
|
| 820 |
// for (std::list<Icmpv6OptionPrefixInformation>::iterator it = prefixList.begin (); it != prefixList.end (); it++) |
| 821 |
// { |
| 822 |
// Ptr<SixLowPanPrefix> prefix = new SixLowPanPrefix; |
| 823 |
// prefix->SetPrefixLength ((*it).GetPrefixLength ()); |
| 824 |
// prefix->SetFlags ((*it).GetFlags ()); |
| 825 |
// prefix->SetValidLifeTime ((*it).GetValidTime ()); |
| 826 |
// prefix->SetPreferredLifeTime ((*it).GetPreferredTime ()); |
| 827 |
// prefix->SetPrefix ((*it).GetPrefix ()); |
| 828 |
// |
| 829 |
// ra->AddPrefix (prefix); |
| 830 |
// |
| 831 |
// ipv6->AddAutoconfiguredAddress (ipv6->GetInterfaceForDevice (sixDevice), |
| 832 |
// (*it).GetPrefix (),(*it).GetPrefixLength (), |
| 833 |
// (*it).GetFlags (), (*it).GetValidTime (), |
| 834 |
// (*it).GetPreferredTime (), defaultRouter); |
| 835 |
// |
| 836 |
// for (sgi::hash_map<Ipv6Address, NdiscCache::Entry *, Ipv6AddressHash>::iterator k = ndiscCache.begin (); |
| 837 |
// k != ndiscCache.end (); k++) |
| 838 |
// { |
| 839 |
// if (k->second->IsRouter ()) |
| 840 |
// { |
| 841 |
// SendSixLowPanARO (sixDevice->MakeGlobalAddressFromMac(addr, (*it).GetPrefix ()), |
| 842 |
// k->second->GetIpv6Address (), m_regTime, Mac64Address::ConvertFrom (addr), |
| 843 |
// addr); |
| 844 |
// |
| 845 |
// Simulator::Schedule (Time (Minutes (m_regTime - m_advance)), &SixLowPanNdProtocol::RetransmitARO, this, |
| 846 |
// sixDevice->MakeGlobalAddressFromMac(addr, (*it).GetPrefix ()), |
| 847 |
// k->second->GetIpv6Address (), m_regTime, Mac64Address::ConvertFrom (addr), |
| 848 |
// addr); |
| 849 |
// } |
| 850 |
// } |
| 851 |
// } |
| 852 |
// \todo quiet compiler |
| 853 |
NS_ASSERT (m_regTime); |
| 854 |
|
| 855 |
for (std::list<Icmpv6OptionSixLowPanContext>::iterator jt = contextList.begin (); jt != contextList.end (); jt++) |
| 856 |
{ |
| 857 |
Ptr<SixLowPanContext> context = new SixLowPanContext; |
| 858 |
context->SetCid ((*jt).GetCid ()); |
| 859 |
context->SetFlagC ((*jt).IsFlagC ()); |
| 860 |
context->SetValidTime ((*jt).GetValidTime ()); |
| 861 |
context->SetContextPrefix ((*jt).GetContextPrefix ()); |
| 862 |
|
| 863 |
ra->AddContext (context); |
| 864 |
} |
| 865 |
} |
| 866 |
else if (ra && version > (ra->GetVersion ())) // Update existing entry from 6LBR with new information |
| 867 |
{ |
| 868 |
ra->SetManagedFlag (raHeader.GetFlagM ()); |
| 869 |
ra->SetOtherConfigFlag (raHeader.GetFlagO ()); |
| 870 |
ra->SetHomeAgentFlag (raHeader.GetFlagH ()); |
| 871 |
ra->SetReachableTime (raHeader.GetReachableTime ()); |
| 872 |
ra->SetRouterLifeTime (raHeader.GetLifeTime ()); |
| 873 |
ra->SetRetransTimer (raHeader.GetRetransmissionTime ()); |
| 874 |
ra->SetCurHopLimit (raHeader.GetCurHopLimit ()); |
| 875 |
ra->SetVersion (version); |
| 876 |
ra->SetValidTime (abrHdr.GetValidTime ()); |
| 877 |
|
| 878 |
for (std::list<Icmpv6OptionPrefixInformation>::iterator it = prefixList.begin (); it != prefixList.end (); it++) |
| 879 |
{ |
| 880 |
if (ra->GetPrefixes ().find ((*it).GetPrefix ()) == ra->GetPrefixes ().end ()) /* prefix NOT found */ |
| 881 |
{ |
| 882 |
Ptr<SixLowPanPrefix> prefix = new SixLowPanPrefix; |
| 883 |
prefix->SetPrefixLength ((*it).GetPrefixLength ()); |
| 884 |
prefix->SetFlags ((*it).GetFlags ()); |
| 885 |
prefix->SetValidLifeTime ((*it).GetValidTime ()); |
| 886 |
prefix->SetPreferredLifeTime ((*it).GetPreferredTime ()); |
| 887 |
prefix->SetPrefix ((*it).GetPrefix ()); |
| 888 |
|
| 889 |
ra->AddPrefix (prefix); |
| 890 |
|
| 891 |
ipv6->AddAutoconfiguredAddress (ipv6->GetInterfaceForDevice (sixDevice), |
| 892 |
(*it).GetPrefix (), (*it).GetPrefixLength (), |
| 893 |
(*it).GetFlags (), (*it).GetValidTime (), |
| 894 |
(*it).GetPreferredTime (), defaultRouter); |
| 895 |
// for (sgi::hash_map<Ipv6Address, NdiscCache::Entry *, Ipv6AddressHash>::iterator kt = ndiscCache.begin (); |
| 896 |
// kt != ndiscCache.end (); kt++) |
| 897 |
// { |
| 898 |
// if (kt->second->IsRouter ()) |
| 899 |
// { |
| 900 |
// SendSixLowPanARO (sixDevice->MakeGlobalAddressFromMac(addr, (*it).GetPrefix ()), |
| 901 |
// kt->second->GetIpv6Address (), m_regTime, |
| 902 |
// Mac64Address::ConvertFrom (addr), addr); |
| 903 |
// |
| 904 |
// Simulator::Schedule (Time (Minutes (m_regTime - m_advance)), &SixLowPanNdProtocol::RetransmitARO, this, |
| 905 |
// sixDevice->MakeGlobalAddressFromMac(addr, (*it).GetPrefix ()), |
| 906 |
// kt->second->GetIpv6Address (), m_regTime, Mac64Address::ConvertFrom (addr), |
| 907 |
// addr); |
| 908 |
// } |
| 909 |
// } |
| 910 |
} |
| 911 |
else /* prefix found, updating! */ |
| 912 |
{ |
| 913 |
Ptr<SixLowPanPrefix> prefix = (ra->GetPrefixes().find((*it).GetPrefix ()))->second; |
| 914 |
|
| 915 |
prefix->SetPrefixLength ((*it).GetPrefixLength ()); |
| 916 |
prefix->SetFlags ((*it).GetFlags ()); |
| 917 |
prefix->SetValidLifeTime ((*it).GetValidTime ()); |
| 918 |
prefix->SetPreferredLifeTime ((*it).GetPreferredTime ()); |
| 919 |
|
| 920 |
ipv6->AddAutoconfiguredAddress (ipv6->GetInterfaceForDevice (sixDevice), |
| 921 |
(*it).GetPrefix (), (*it).GetPrefixLength (), |
| 922 |
(*it).GetFlags (), (*it).GetValidTime (), |
| 923 |
(*it).GetPreferredTime (), defaultRouter); |
| 924 |
// for (sgi::hash_map<Ipv6Address, NdiscCache::Entry *, Ipv6AddressHash>::iterator lt = ndiscCache.begin (); |
| 925 |
// lt != ndiscCache.end (); lt++) |
| 926 |
// { |
| 927 |
// if (lt->second->IsRouter ()) |
| 928 |
// { |
| 929 |
// SendSixLowPanARO (sixDevice->MakeGlobalAddressFromMac(addr, (*it).GetPrefix ()), |
| 930 |
// lt->second->GetIpv6Address (), m_regTime, Mac64Address::ConvertFrom (addr), |
| 931 |
// addr); |
| 932 |
// |
| 933 |
// Simulator::Schedule (Time (Minutes (m_regTime - m_advance)), &SixLowPanNdProtocol::RetransmitARO, this, |
| 934 |
// sixDevice->MakeGlobalAddressFromMac(addr, (*it).GetPrefix ()), |
| 935 |
// lt->second->GetIpv6Address (), m_regTime, Mac64Address::ConvertFrom (addr), |
| 936 |
// addr); |
| 937 |
// } |
| 938 |
// } |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
for (std::list<Icmpv6OptionSixLowPanContext>::iterator jt = contextList.begin (); jt != contextList.end (); jt++) |
| 943 |
{ |
| 944 |
if (ra->GetContexts ().find ((*jt).GetCid ()) == ra->GetContexts ().end ()) /* context NOT found */ |
| 945 |
{ |
| 946 |
Ptr<SixLowPanContext> context = new SixLowPanContext; |
| 947 |
context->SetCid ((*jt).GetCid ()); |
| 948 |
context->SetFlagC ((*jt).IsFlagC ()); |
| 949 |
context->SetValidTime ((*jt).GetValidTime ()); |
| 950 |
context->SetContextPrefix ((*jt).GetContextPrefix ()); |
| 951 |
|
| 952 |
ra->AddContext (context); |
| 953 |
} |
| 954 |
else |
| 955 |
{ |
| 956 |
Ptr<SixLowPanContext> context = (ra->GetContexts ().find ((*jt).GetCid ()))->second; |
| 957 |
|
| 958 |
context->SetFlagC ((*jt).IsFlagC ()); |
| 959 |
context->SetValidTime ((*jt).GetValidTime ()); |
| 960 |
context->SetContextPrefix ((*jt).GetContextPrefix ()); |
| 961 |
} |
| 962 |
} |
| 963 |
} |
| 964 |
else // Old information, not updating |
| 965 |
{ |
| 966 |
return; |
| 967 |
} |
| 968 |
|
| 969 |
uint32_t t = raHeader.GetLifeTime (); |
| 970 |
|
| 971 |
for (std::list<Icmpv6OptionPrefixInformation>::iterator it = prefixList.begin (); it != prefixList.end (); it++) |
| 972 |
{ |
| 973 |
t = t < ((*it).GetValidTime ()) ? t : ((*it).GetValidTime ()); |
| 974 |
} |
| 975 |
for (std::list<Icmpv6OptionSixLowPanContext>::iterator jt = contextList.begin (); jt != contextList.end (); jt++) |
| 976 |
{ |
| 977 |
t = (60 * ((*jt).GetValidTime ())) < t ? (60 * ((*jt).GetValidTime ())) : t; |
| 978 |
} |
| 979 |
|
| 980 |
t -= (60 * m_advance); |
| 981 |
|
| 982 |
Simulator::Schedule (Time (Seconds (t - 1)), &SixLowPanNdProtocol::SetReceivedRA, this, false); |
| 983 |
Simulator::Schedule (Time (Seconds (t)), &SixLowPanNdProtocol::RetransmitRS, this, |
| 984 |
interface->GetLinkLocalAddress ().GetAddress (), src, addr); |
| 985 |
} |
| 986 |
/// \todo da finire!! (controlla codice, controlla regTime) |
| 987 |
|
| 988 |
void SixLowPanNdProtocol::HandleSixLowPanDAC (Ptr<Packet> packet, Ipv6Address const &src, Ipv6Address const &dst, |
| 989 |
Ptr<Ipv6Interface> interface) |
| 990 |
{ |
| 991 |
NS_LOG_FUNCTION (this << packet << src << dst << interface); |
| 992 |
|
| 993 |
Ptr<SixLowPanNetDevice> sixDevice = DynamicCast<SixLowPanNetDevice> (interface->GetDevice()); |
| 994 |
NS_ASSERT_MSG (sixDevice != NULL, "SixLowPanNdProtocol cannot be installed on device different from SixLowPanNetDevice"); |
| 995 |
|
| 996 |
Icmpv6DuplicateAddress dacHdr (0); |
| 997 |
packet->RemoveHeader (dacHdr); |
| 998 |
|
| 999 |
if (m_multihopDad) |
| 1000 |
{ |
| 1001 |
Ipv6Address reg = dacHdr.GetRegAddress (); |
| 1002 |
|
| 1003 |
if (!reg.IsMulticast () && src != Ipv6Address::GetAny () && !src.IsMulticast ()) |
| 1004 |
{ |
| 1005 |
Ptr<SixLowPanNdiscCache> cache = FindSixLowPanCache (sixDevice); |
| 1006 |
SixLowPanNdiscCache::SixLowPanEntry* entry = 0; |
| 1007 |
entry = cache->SixLowPanLookup (reg); |
| 1008 |
|
| 1009 |
if (dacHdr.GetStatus () == 0) /* mark the entry as registered, send ARO with status=0 */ |
| 1010 |
{ |
| 1011 |
entry->MarkRegistered (dacHdr.GetRegTime ()); |
| 1012 |
|
| 1013 |
SendSixLowPanARO (dst, dacHdr.GetRegAddress (), dacHdr.GetStatus (), dacHdr.GetRegTime (), |
| 1014 |
dacHdr.GetEui64 ()); |
| 1015 |
} |
| 1016 |
else /* remove the tentative entry, send ARO with error code */ |
| 1017 |
{ |
| 1018 |
cache->SixLowPanRemove (entry); |
| 1019 |
|
| 1020 |
Ipv6Address address = sixDevice->MakeLinkLocalAddressFromMac (dacHdr.GetEui64 ()); |
| 1021 |
|
| 1022 |
SendSixLowPanARO (dst, address, dacHdr.GetStatus (), dacHdr.GetRegTime (), dacHdr.GetEui64 ()); |
| 1023 |
} |
| 1024 |
} |
| 1025 |
else |
| 1026 |
{ |
| 1027 |
NS_LOG_ERROR ("Validity checks for DAR not satisfied."); |
| 1028 |
return; |
| 1029 |
} |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
Ptr<SixLowPanNdiscCache> SixLowPanNdProtocol::FindSixLowPanCache (Ptr<NetDevice> device) |
| 1034 |
{ |
| 1035 |
NS_LOG_FUNCTION (this << device); |
| 1036 |
|
| 1037 |
for (SixLowPanCacheList::const_iterator it = m_sixlowCList.begin (); it != m_sixlowCList.end (); it++) |
| 1038 |
{ |
| 1039 |
if ((*it)->GetDevice () == device) |
| 1040 |
{ |
| 1041 |
return *it; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
/* quiet compiler */ |
| 1046 |
return 0; |
| 1047 |
} |
| 1048 |
|
| 1049 |
void SixLowPanNdProtocol::RetransmitARO (Ipv6Address src, Ipv6Address dst, uint16_t time, |
| 1050 |
Mac64Address eui, Address linkAddr) |
| 1051 |
{ |
| 1052 |
NS_LOG_FUNCTION (this << src << dst << time << eui << linkAddr); |
| 1053 |
|
| 1054 |
IntegerValue maxUnicastSolicit; |
| 1055 |
GetAttribute ("MaxUnicastSolicit", maxUnicastSolicit); |
| 1056 |
if (m_aroRetransmit < maxUnicastSolicit.Get ()) |
| 1057 |
{ |
| 1058 |
m_aroRetransmit++; |
| 1059 |
|
| 1060 |
SendSixLowPanARO (src, dst, time, eui, linkAddr); |
| 1061 |
|
| 1062 |
TimeValue retransmissionTime; |
| 1063 |
GetAttribute ("RetransmissionTime", retransmissionTime); |
| 1064 |
|
| 1065 |
Simulator::Schedule (retransmissionTime.Get (), &SixLowPanNdProtocol::RetransmitARO, this, |
| 1066 |
src, dst, time, eui, linkAddr); |
| 1067 |
return; |
| 1068 |
} |
| 1069 |
else |
| 1070 |
{ |
| 1071 |
return; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
void SixLowPanNdProtocol::RetransmitRS (Ipv6Address src, Ipv6Address dst, Address linkAddr) |
| 1076 |
{ |
| 1077 |
NS_LOG_FUNCTION (this << src << dst << linkAddr); |
| 1078 |
|
| 1079 |
/* if the source is NOT unspec, send a RS message + SLLA option */ |
| 1080 |
if (src != Ipv6Address::GetAny ()) |
| 1081 |
{ |
| 1082 |
if (!m_receivedRA && m_rsRetransmit < MAX_RTR_SOLICITATIONS) |
| 1083 |
{ |
| 1084 |
m_rsRetransmit++; |
| 1085 |
|
| 1086 |
SendRS (src, dst, linkAddr); |
| 1087 |
|
| 1088 |
Simulator::Schedule (Time (Seconds (RTR_SOLICITATION_INTERVAL)), &SixLowPanNdProtocol::RetransmitRS, this, |
| 1089 |
src, dst, linkAddr); |
| 1090 |
return; |
| 1091 |
} |
| 1092 |
else if (!m_receivedRA) |
| 1093 |
{ |
| 1094 |
m_rsRetransmit++; |
| 1095 |
|
| 1096 |
Ipv6Address destination = Ipv6Address::GetAllRoutersMulticast (); |
| 1097 |
|
| 1098 |
SendRS (src, destination, linkAddr); |
| 1099 |
|
| 1100 |
Simulator::Schedule (Time (Seconds (MAX_RTR_SOLICITATION_INTERVAL)), &SixLowPanNdProtocol::RetransmitRS, this, |
| 1101 |
src, destination, linkAddr); |
| 1102 |
/* inserire truncated binary exponential backoff */ |
| 1103 |
return; |
| 1104 |
} |
| 1105 |
else |
| 1106 |
{ |
| 1107 |
return; |
| 1108 |
} |
| 1109 |
} |
| 1110 |
else |
| 1111 |
{ |
| 1112 |
NS_LOG_ERROR ("An unspecified source address MUST NOT be used in RS messages."); |
| 1113 |
return; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
/// \todo da finire!! (truncated binary exponential backoff) |
| 1117 |
|
| 1118 |
void SixLowPanNdProtocol::SetReceivedRA (bool received) |
| 1119 |
{ |
| 1120 |
NS_LOG_FUNCTION (this << received); |
| 1121 |
m_receivedRA = received; |
| 1122 |
} |
| 1123 |
|
| 1124 |
} /* namespace ns3 */ |