PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/src/internet/model/ipv6-interface.cc

https://bitbucket.org/binhngq/lena-local
C++ | 460 lines | 357 code | 70 blank | 33 comment | 49 complexity | b1768d6fe84b2a1d3c9ddd2fd5468ed2 MD5 | raw 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. */
  20. #include "ns3/log.h"
  21. #include "ns3/node.h"
  22. #include "ns3/packet.h"
  23. #include "ipv6-interface.h"
  24. #include "ns3/net-device.h"
  25. #include "loopback-net-device.h"
  26. #include "ipv6-l3-protocol.h"
  27. #include "icmpv6-l4-protocol.h"
  28. #include "ndisc-cache.h"
  29. namespace ns3
  30. {
  31. NS_LOG_COMPONENT_DEFINE ("Ipv6Interface");
  32. NS_OBJECT_ENSURE_REGISTERED (Ipv6Interface);
  33. TypeId Ipv6Interface::GetTypeId ()
  34. {
  35. static TypeId tid = TypeId ("ns3::Ipv6Interface")
  36. .SetParent<Object> ()
  37. ;
  38. return tid;
  39. }
  40. Ipv6Interface::Ipv6Interface ()
  41. : m_ifup (false),
  42. m_forwarding (true),
  43. m_metric (1),
  44. m_node (0),
  45. m_device (0),
  46. m_ndCache (0),
  47. m_curHopLimit (0),
  48. m_baseReachableTime (0),
  49. m_reachableTime (0),
  50. m_retransTimer (0)
  51. {
  52. NS_LOG_FUNCTION (this);
  53. }
  54. Ipv6Interface::~Ipv6Interface ()
  55. {
  56. NS_LOG_FUNCTION_NOARGS ();
  57. }
  58. void Ipv6Interface::DoDispose ()
  59. {
  60. NS_LOG_FUNCTION_NOARGS ();
  61. m_node = 0;
  62. m_device = 0;
  63. m_ndCache = 0;
  64. Object::DoDispose ();
  65. }
  66. void Ipv6Interface::DoSetup ()
  67. {
  68. NS_LOG_FUNCTION_NOARGS ();
  69. if (m_node == 0 || m_device == 0)
  70. {
  71. return;
  72. }
  73. /* set up link-local address */
  74. if (!DynamicCast<LoopbackNetDevice> (m_device)) /* no autoconf for ip6-localhost */
  75. {
  76. Address addr = GetDevice ()->GetAddress ();
  77. if (Mac48Address::IsMatchingType (addr))
  78. {
  79. Ipv6InterfaceAddress ifaddr = Ipv6InterfaceAddress (Ipv6Address::MakeAutoconfiguredLinkLocalAddress (Mac48Address::ConvertFrom (addr)), Ipv6Prefix (64));
  80. AddAddress (ifaddr);
  81. }
  82. else
  83. {
  84. NS_ASSERT_MSG (false, "IPv6 autoconf for this kind of address not implemented.");
  85. }
  86. }
  87. else
  88. {
  89. return; /* no NDISC cache for ip6-localhost */
  90. }
  91. Ptr<Icmpv6L4Protocol> icmpv6 = m_node->GetObject<Ipv6L3Protocol> ()->GetIcmpv6 ();
  92. m_ndCache = icmpv6->CreateCache (m_device, this);
  93. }
  94. void Ipv6Interface::SetNode (Ptr<Node> node)
  95. {
  96. NS_LOG_FUNCTION (this << node);
  97. m_node = node;
  98. DoSetup ();
  99. }
  100. void Ipv6Interface::SetDevice (Ptr<NetDevice> device)
  101. {
  102. NS_LOG_FUNCTION (this << device);
  103. m_device = device;
  104. DoSetup ();
  105. }
  106. Ptr<NetDevice> Ipv6Interface::GetDevice () const
  107. {
  108. NS_LOG_FUNCTION_NOARGS ();
  109. return m_device;
  110. }
  111. void Ipv6Interface::SetMetric (uint16_t metric)
  112. {
  113. NS_LOG_FUNCTION (this << metric);
  114. m_metric = metric;
  115. }
  116. uint16_t Ipv6Interface::GetMetric () const
  117. {
  118. NS_LOG_FUNCTION_NOARGS ();
  119. return m_metric;
  120. }
  121. bool Ipv6Interface::IsUp () const
  122. {
  123. NS_LOG_FUNCTION_NOARGS ();
  124. return m_ifup;
  125. }
  126. bool Ipv6Interface::IsDown () const
  127. {
  128. NS_LOG_FUNCTION_NOARGS ();
  129. return !m_ifup;
  130. }
  131. void Ipv6Interface::SetUp ()
  132. {
  133. NS_LOG_FUNCTION_NOARGS ();
  134. if (m_ifup)
  135. {
  136. return;
  137. }
  138. m_ifup = true;
  139. }
  140. void Ipv6Interface::SetDown ()
  141. {
  142. NS_LOG_FUNCTION_NOARGS ();
  143. m_ifup = false;
  144. m_addresses.clear ();
  145. }
  146. bool Ipv6Interface::IsForwarding () const
  147. {
  148. NS_LOG_FUNCTION_NOARGS ();
  149. return m_forwarding;
  150. }
  151. void Ipv6Interface::SetForwarding (bool forwarding)
  152. {
  153. NS_LOG_FUNCTION (this << forwarding);
  154. m_forwarding = forwarding;
  155. }
  156. bool Ipv6Interface::AddAddress (Ipv6InterfaceAddress iface)
  157. {
  158. NS_LOG_FUNCTION_NOARGS ();
  159. Ipv6Address addr = iface.GetAddress ();
  160. /* DAD handling */
  161. if (!addr.IsAny ())
  162. {
  163. for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
  164. {
  165. if ((*it).GetAddress () == addr)
  166. {
  167. return false;
  168. }
  169. }
  170. m_addresses.push_back (iface);
  171. if (!addr.IsAny () || !addr.IsLocalhost ())
  172. {
  173. /* DAD handling */
  174. Ptr<Icmpv6L4Protocol> icmpv6 = m_node->GetObject<Ipv6L3Protocol> ()->GetIcmpv6 ();
  175. if (icmpv6 && icmpv6->IsAlwaysDad ())
  176. {
  177. Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
  178. Simulator::Schedule (Seconds (1.), &Icmpv6L4Protocol::FunctionDadTimeout, icmpv6, this, addr);
  179. }
  180. }
  181. return true;
  182. }
  183. /* bad address */
  184. return false;
  185. }
  186. Ipv6InterfaceAddress Ipv6Interface::GetLinkLocalAddress () const
  187. {
  188. /* IPv6 interface has always at least one IPv6 link-local address */
  189. NS_LOG_FUNCTION_NOARGS ();
  190. for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
  191. {
  192. if ((*it).GetAddress ().IsLinkLocal ())
  193. {
  194. return (*it);
  195. }
  196. }
  197. NS_ASSERT_MSG (false, "No link-local address on interface " << this);
  198. Ipv6InterfaceAddress addr;
  199. return addr; /* quiet compiler */
  200. }
  201. Ipv6InterfaceAddress Ipv6Interface::GetAddress (uint32_t index) const
  202. {
  203. NS_LOG_FUNCTION (this << index);
  204. uint32_t i = 0;
  205. if (m_addresses.size () > index)
  206. {
  207. for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
  208. {
  209. if (i == index)
  210. {
  211. return (*it);
  212. }
  213. i++;
  214. }
  215. }
  216. NS_ASSERT_MSG (false, "Address " << index << " not found");
  217. Ipv6InterfaceAddress addr;
  218. return addr; /* quiet compiler */
  219. }
  220. uint32_t Ipv6Interface::GetNAddresses () const
  221. {
  222. NS_LOG_FUNCTION_NOARGS ();
  223. return m_addresses.size ();
  224. }
  225. Ipv6InterfaceAddress Ipv6Interface::RemoveAddress (uint32_t index)
  226. {
  227. NS_LOG_FUNCTION (this << index);
  228. uint32_t i = 0;
  229. if (m_addresses.size () < index)
  230. {
  231. NS_ASSERT_MSG (false, "Try to remove index that don't exist in Ipv6Interface::RemoveAddress");
  232. }
  233. for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
  234. {
  235. if (i == index)
  236. {
  237. Ipv6InterfaceAddress iface = (*it);
  238. m_addresses.erase (it);
  239. return iface;
  240. }
  241. i++;
  242. }
  243. NS_ASSERT_MSG (false, "Address " << index << " not found");
  244. Ipv6InterfaceAddress addr;
  245. return addr; /* quiet compiler */
  246. }
  247. Ipv6InterfaceAddress Ipv6Interface::GetAddressMatchingDestination (Ipv6Address dst)
  248. {
  249. NS_LOG_FUNCTION (this << dst);
  250. for (Ipv6InterfaceAddressList::const_iterator it = m_addresses.begin (); it != m_addresses.end (); ++it)
  251. {
  252. Ipv6InterfaceAddress ifaddr = (*it);
  253. if (ifaddr.GetPrefix ().IsMatch (ifaddr.GetAddress (), dst))
  254. {
  255. return ifaddr;
  256. }
  257. }
  258. /* NS_ASSERT_MSG (false, "Not matching address."); */
  259. Ipv6InterfaceAddress ret;
  260. return ret; /* quiet compiler */
  261. }
  262. void Ipv6Interface::Send (Ptr<Packet> p, Ipv6Address dest)
  263. {
  264. NS_LOG_FUNCTION (this << p << dest);
  265. Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol> ();
  266. if (!IsUp ())
  267. {
  268. return;
  269. }
  270. /* check if destination is localhost (::1) */
  271. if (DynamicCast<LoopbackNetDevice> (m_device))
  272. {
  273. /* XXX additional checks needed here (such as whether multicast
  274. * goes to loopback)?
  275. */
  276. m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
  277. return;
  278. }
  279. /* check if destination is for one of our interface */
  280. for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
  281. {
  282. if (dest == (*it).GetAddress ())
  283. {
  284. ipv6->Receive (m_device, p, Ipv6L3Protocol::PROT_NUMBER,
  285. m_device->GetBroadcast (),
  286. m_device->GetBroadcast (),
  287. NetDevice::PACKET_HOST // note: linux uses PACKET_LOOPBACK here
  288. );
  289. return;
  290. }
  291. }
  292. /* other address */
  293. if (m_device->NeedsArp ())
  294. {
  295. NS_LOG_LOGIC ("Needs ARP" << " " << dest);
  296. Ptr<Icmpv6L4Protocol> icmpv6 = ipv6->GetIcmpv6 ();
  297. Address hardwareDestination;
  298. bool found = false;
  299. NS_ASSERT (icmpv6);
  300. if (dest.IsMulticast ())
  301. {
  302. NS_LOG_LOGIC ("IsMulticast");
  303. NS_ASSERT_MSG (m_device->IsMulticast (), "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
  304. hardwareDestination = m_device->GetMulticast (dest);
  305. found = true;
  306. }
  307. else
  308. {
  309. NS_LOG_LOGIC ("NDISC Lookup");
  310. found = icmpv6->Lookup (p, dest, GetDevice (), m_ndCache, &hardwareDestination);
  311. }
  312. if (found)
  313. {
  314. NS_LOG_LOGIC ("Address Resolved. Send.");
  315. m_device->Send (p, hardwareDestination, Ipv6L3Protocol::PROT_NUMBER);
  316. }
  317. }
  318. else
  319. {
  320. NS_LOG_LOGIC ("Doesn't need ARP");
  321. m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
  322. }
  323. }
  324. void Ipv6Interface::SetCurHopLimit (uint8_t curHopLimit)
  325. {
  326. NS_LOG_FUNCTION (this << curHopLimit);
  327. m_curHopLimit = curHopLimit;
  328. }
  329. uint8_t Ipv6Interface::GetCurHopLimit () const
  330. {
  331. NS_LOG_FUNCTION_NOARGS ();
  332. return m_curHopLimit;
  333. }
  334. void Ipv6Interface::SetBaseReachableTime (uint16_t baseReachableTime)
  335. {
  336. NS_LOG_FUNCTION (this << baseReachableTime);
  337. m_baseReachableTime = baseReachableTime;
  338. }
  339. uint16_t Ipv6Interface::GetBaseReachableTime () const
  340. {
  341. NS_LOG_FUNCTION_NOARGS ();
  342. return m_baseReachableTime;
  343. }
  344. void Ipv6Interface::SetReachableTime (uint16_t reachableTime)
  345. {
  346. NS_LOG_FUNCTION (this << reachableTime);
  347. m_reachableTime = reachableTime;
  348. }
  349. uint16_t Ipv6Interface::GetReachableTime () const
  350. {
  351. NS_LOG_FUNCTION_NOARGS ();
  352. return m_reachableTime;
  353. }
  354. void Ipv6Interface::SetRetransTimer (uint16_t retransTimer)
  355. {
  356. NS_LOG_FUNCTION (this << retransTimer);
  357. m_retransTimer = retransTimer;
  358. }
  359. uint16_t Ipv6Interface::GetRetransTimer () const
  360. {
  361. NS_LOG_FUNCTION_NOARGS ();
  362. return m_retransTimer;
  363. }
  364. void Ipv6Interface::SetState (Ipv6Address address, Ipv6InterfaceAddress::State_e state)
  365. {
  366. NS_LOG_FUNCTION (this << address << state);
  367. for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
  368. {
  369. if ((*it).GetAddress () == address)
  370. {
  371. (*it).SetState (state);
  372. return;
  373. }
  374. }
  375. /* not found, maybe address has expired */
  376. }
  377. void Ipv6Interface::SetNsDadUid (Ipv6Address address, uint32_t uid)
  378. {
  379. NS_LOG_FUNCTION (this << address << uid);
  380. for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
  381. {
  382. if ((*it).GetAddress () == address)
  383. {
  384. (*it).SetNsDadUid (uid);
  385. return;
  386. }
  387. }
  388. /* not found, maybe address has expired */
  389. }
  390. } /* namespace ns3 */