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
21 #include "ns3/log.h"
22 #include "ns3/node.h"
23 #include "ns3/packet.h"
24 #include "ns3/net-device.h"
25 #include "ns3/mac16-address.h"
26 #include "ns3/mac64-address.h"
27 #include "ns3/traffic-control-layer.h"
28
29 #include "ipv6-interface.h"
30 #include "ipv6-queue-disc-item.h"
31 #include "loopback-net-device.h"
32 #include "ipv6-l3-protocol.h"
33 #include "icmpv6-l4-protocol.h"
34 #include "ipv6-header.h"
35 #include "ndisc-cache.h"
36
37 namespace ns3
38 {
39
40 NS_LOG_COMPONENT_DEFINE ("Ipv6Interface");
41
42 NS_OBJECT_ENSURE_REGISTERED (Ipv6Interface);
43
GetTypeId()44 TypeId Ipv6Interface::GetTypeId ()
45 {
46 static TypeId tid = TypeId ("ns3::Ipv6Interface")
47 .SetParent<Object> ()
48 .SetGroupName ("Internet")
49 ;
50 return tid;
51 }
52
Ipv6Interface()53 Ipv6Interface::Ipv6Interface ()
54 : m_ifup (false),
55 m_forwarding (true),
56 m_metric (1),
57 m_node (0),
58 m_device (0),
59 m_tc (0),
60 m_ndCache (0),
61 m_curHopLimit (0),
62 m_baseReachableTime (0),
63 m_reachableTime (0),
64 m_retransTimer (0)
65 {
66 NS_LOG_FUNCTION (this);
67 }
68
~Ipv6Interface()69 Ipv6Interface::~Ipv6Interface ()
70 {
71 }
72
DoDispose()73 void Ipv6Interface::DoDispose ()
74 {
75 NS_LOG_FUNCTION (this);
76 m_node = 0;
77 m_device = 0;
78 m_tc = 0;
79 m_ndCache = 0;
80 Object::DoDispose ();
81 }
82
DoSetup()83 void Ipv6Interface::DoSetup ()
84 {
85 NS_LOG_FUNCTION (this);
86
87 if (m_node == 0 || m_device == 0)
88 {
89 return;
90 }
91
92 /* set up link-local address */
93 if (!DynamicCast<LoopbackNetDevice> (m_device)) /* no autoconf for ip6-localhost */
94 {
95 Address addr = GetDevice ()->GetAddress ();
96 Ipv6InterfaceAddress ifaddr = Ipv6InterfaceAddress (Ipv6Address::MakeAutoconfiguredLinkLocalAddress (addr), Ipv6Prefix (64));
97 AddAddress (ifaddr);
98 m_linkLocalAddress = ifaddr;
99 }
100 else
101 {
102 return; /* no NDISC cache for ip6-localhost */
103 }
104 int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
105 Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
106
107 if (icmpv6 && !m_ndCache)
108 {
109 m_ndCache = icmpv6->CreateCache (m_device, this);
110 }
111 }
112
SetNode(Ptr<Node> node)113 void Ipv6Interface::SetNode (Ptr<Node> node)
114 {
115 NS_LOG_FUNCTION (this << node);
116 m_node = node;
117 }
118
SetDevice(Ptr<NetDevice> device)119 void Ipv6Interface::SetDevice (Ptr<NetDevice> device)
120 {
121 NS_LOG_FUNCTION (this << device);
122 m_device = device;
123 }
124
125 void
SetTrafficControl(Ptr<TrafficControlLayer> tc)126 Ipv6Interface::SetTrafficControl (Ptr<TrafficControlLayer> tc)
127 {
128 NS_LOG_FUNCTION (this << tc);
129 m_tc = tc;
130 }
131
GetDevice() const132 Ptr<NetDevice> Ipv6Interface::GetDevice () const
133 {
134 NS_LOG_FUNCTION (this);
135 return m_device;
136 }
137
SetMetric(uint16_t metric)138 void Ipv6Interface::SetMetric (uint16_t metric)
139 {
140 NS_LOG_FUNCTION (this << metric);
141 m_metric = metric;
142 }
143
GetMetric() const144 uint16_t Ipv6Interface::GetMetric () const
145 {
146 NS_LOG_FUNCTION (this);
147 return m_metric;
148 }
149
IsUp() const150 bool Ipv6Interface::IsUp () const
151 {
152 NS_LOG_FUNCTION (this);
153 return m_ifup;
154 }
155
IsDown() const156 bool Ipv6Interface::IsDown () const
157 {
158 NS_LOG_FUNCTION (this);
159 return !m_ifup;
160 }
161
SetUp()162 void Ipv6Interface::SetUp ()
163 {
164 NS_LOG_FUNCTION (this);
165
166 if (m_ifup)
167 {
168 return;
169 }
170 DoSetup ();
171 m_ifup = true;
172 }
173
SetDown()174 void Ipv6Interface::SetDown ()
175 {
176 NS_LOG_FUNCTION (this);
177 m_ifup = false;
178 m_addresses.clear ();
179 m_ndCache->Flush ();
180 }
181
IsForwarding() const182 bool Ipv6Interface::IsForwarding () const
183 {
184 NS_LOG_FUNCTION (this);
185 return m_forwarding;
186 }
187
SetForwarding(bool forwarding)188 void Ipv6Interface::SetForwarding (bool forwarding)
189 {
190 NS_LOG_FUNCTION (this << forwarding);
191 m_forwarding = forwarding;
192 }
193
AddAddress(Ipv6InterfaceAddress iface)194 bool Ipv6Interface::AddAddress (Ipv6InterfaceAddress iface)
195 {
196 NS_LOG_FUNCTION (this << iface);
197 Ipv6Address addr = iface.GetAddress ();
198
199 /* DAD handling */
200 if (!addr.IsAny ())
201 {
202 for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
203 {
204 if (it->first.GetAddress () == addr)
205 {
206 return false;
207 }
208 }
209
210 Ipv6Address solicited = Ipv6Address::MakeSolicitedAddress (iface.GetAddress ());
211 m_addresses.push_back (std::make_pair (iface, solicited));
212
213 if (!addr.IsAny () || !addr.IsLocalhost ())
214 {
215 /* DAD handling */
216
217 int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
218 Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
219
220 if (icmpv6)
221 {
222 if (icmpv6->IsAlwaysDad ())
223 {
224 Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::DoDAD, icmpv6, addr, this);
225 Simulator::Schedule (Seconds (1.), &Icmpv6L4Protocol::FunctionDadTimeout, icmpv6, this, addr);
226 }
227 else
228 {
229 Simulator::Schedule (Seconds (0.), &Icmpv6L4Protocol::FunctionDadTimeout, icmpv6, this, addr);
230 }
231 }
232 }
233 return true;
234 }
235
236 /* bad address */
237 return false;
238 }
239
GetLinkLocalAddress() const240 Ipv6InterfaceAddress Ipv6Interface::GetLinkLocalAddress () const
241 {
242 /* IPv6 interface has always at least one IPv6 link-local address */
243 NS_LOG_FUNCTION (this);
244
245 return m_linkLocalAddress;
246 }
247
IsSolicitedMulticastAddress(Ipv6Address address) const248 bool Ipv6Interface::IsSolicitedMulticastAddress (Ipv6Address address) const
249 {
250 /* IPv6 interface has always at least one IPv6 Solicited Multicast address */
251 NS_LOG_FUNCTION (this << address);
252
253 for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
254 {
255 if (it->second == address)
256 {
257 return true;
258 }
259 }
260
261 return false;
262 }
263
GetAddress(uint32_t index) const264 Ipv6InterfaceAddress Ipv6Interface::GetAddress (uint32_t index) const
265 {
266 NS_LOG_FUNCTION (this << index);
267 uint32_t i = 0;
268
269 if (m_addresses.size () > index)
270 {
271 for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
272 {
273 if (i == index)
274 {
275 return it->first;
276 }
277 i++;
278 }
279 }
280 else
281 {
282 NS_FATAL_ERROR ("index " << index << " out of bounds");
283 }
284 Ipv6InterfaceAddress addr;
285 return addr; /* quiet compiler */
286 }
287
GetNAddresses() const288 uint32_t Ipv6Interface::GetNAddresses () const
289 {
290 NS_LOG_FUNCTION (this);
291 return m_addresses.size ();
292 }
293
RemoveAddress(uint32_t index)294 Ipv6InterfaceAddress Ipv6Interface::RemoveAddress (uint32_t index)
295 {
296 NS_LOG_FUNCTION (this << index);
297 uint32_t i = 0;
298
299 if (m_addresses.size () < index)
300 {
301 NS_FATAL_ERROR ("Removing index that does not exist in Ipv6Interface::RemoveAddress");
302 }
303
304 for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
305 {
306 if (i == index)
307 {
308 Ipv6InterfaceAddress iface = it->first;
309 m_addresses.erase (it);
310 return iface;
311 }
312
313 i++;
314 }
315 NS_FATAL_ERROR ("Address " << index << " not found");
316 Ipv6InterfaceAddress addr;
317 return addr; /* quiet compiler */
318 }
319
320 Ipv6InterfaceAddress
RemoveAddress(Ipv6Address address)321 Ipv6Interface::RemoveAddress(Ipv6Address address)
322 {
323 NS_LOG_FUNCTION(this << address);
324
325 if (address == address.GetLoopback())
326 {
327 NS_LOG_WARN ("Cannot remove loopback address.");
328 return Ipv6InterfaceAddress();
329 }
330
331 for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
332 {
333 if(it->first.GetAddress () == address)
334 {
335 Ipv6InterfaceAddress iface = it->first;
336 m_addresses.erase(it);
337 return iface;
338 }
339 }
340 return Ipv6InterfaceAddress();
341 }
342
GetAddressMatchingDestination(Ipv6Address dst)343 Ipv6InterfaceAddress Ipv6Interface::GetAddressMatchingDestination (Ipv6Address dst)
344 {
345 NS_LOG_FUNCTION (this << dst);
346
347 for (Ipv6InterfaceAddressList::const_iterator it = m_addresses.begin (); it != m_addresses.end (); ++it)
348 {
349 Ipv6InterfaceAddress ifaddr = it->first;
350
351 if (ifaddr.GetPrefix ().IsMatch (ifaddr.GetAddress (), dst))
352 {
353 return ifaddr;
354 }
355 }
356
357 /* NS_ASSERT_MSG (false, "Not matching address."); */
358 Ipv6InterfaceAddress ret;
359 return ret; /* quiet compiler */
360 }
361
Send(Ptr<Packet> p,const Ipv6Header & hdr,Ipv6Address dest)362 void Ipv6Interface::Send (Ptr<Packet> p, const Ipv6Header & hdr, Ipv6Address dest)
363 {
364 NS_LOG_FUNCTION (this << p << dest);
365
366 if (!IsUp ())
367 {
368 return;
369 }
370
371 Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol> ();
372
373 /* check if destination is localhost (::1), if yes we don't pass through
374 * traffic control layer */
375 if (DynamicCast<LoopbackNetDevice> (m_device))
376 {
377 /** \todo additional checks needed here (such as whether multicast
378 * goes to loopback)?
379 */
380 p->AddHeader (hdr);
381 m_device->Send (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER);
382 return;
383 }
384
385 NS_ASSERT (m_tc != 0);
386
387 /* check if destination is for one of our interface */
388 for (Ipv6InterfaceAddressListCI it = m_addresses.begin (); it != m_addresses.end (); ++it)
389 {
390 if (dest == it->first.GetAddress ())
391 {
392 p->AddHeader (hdr);
393 m_tc->Receive (m_device, p, Ipv6L3Protocol::PROT_NUMBER,
394 m_device->GetBroadcast (),
395 m_device->GetBroadcast (),
396 NetDevice::PACKET_HOST);
397 return;
398 }
399 }
400
401 /* other address */
402 if (m_device->NeedsArp ())
403 {
404 NS_LOG_LOGIC ("Needs NDISC " << dest);
405
406 int32_t interfaceId = m_node->GetObject<Ipv6> ()->GetInterfaceForDevice (m_device);
407 Ptr<Icmpv6L4Protocol> icmpv6 = DynamicCast<Icmpv6L4Protocol> (m_node->GetObject<Ipv6> ()->GetProtocol (Icmpv6L4Protocol::GetStaticProtocolNumber (), interfaceId));
408
409 Address hardwareDestination;
410 bool found = false;
411
412 NS_ASSERT (icmpv6);
413
414 if (dest.IsMulticast ())
415 {
416 NS_LOG_LOGIC ("IsMulticast");
417 NS_ASSERT_MSG (m_device->IsMulticast (), "Ipv6Interface::SendTo (): Sending multicast packet over non-multicast device");
418
419 hardwareDestination = m_device->GetMulticast (dest);
420 found = true;
421 }
422 else
423 {
424 NS_LOG_LOGIC ("NDISC Lookup");
425 found = icmpv6->Lookup (p, hdr, dest, GetDevice (), m_ndCache, &hardwareDestination);
426 }
427
428 if (found)
429 {
430 NS_LOG_LOGIC ("Address Resolved. Send.");
431 m_tc->Send (m_device, Create<Ipv6QueueDiscItem> (p, hardwareDestination, Ipv6L3Protocol::PROT_NUMBER, hdr));
432 }
433 }
434 else
435 {
436 NS_LOG_LOGIC ("Doesn't need NDISC");
437 m_tc->Send (m_device, Create<Ipv6QueueDiscItem> (p, m_device->GetBroadcast (), Ipv6L3Protocol::PROT_NUMBER, hdr));
438 }
439 }
440
SetCurHopLimit(uint8_t curHopLimit)441 void Ipv6Interface::SetCurHopLimit (uint8_t curHopLimit)
442 {
443 NS_LOG_FUNCTION (this << curHopLimit);
444 m_curHopLimit = curHopLimit;
445 }
446
GetCurHopLimit() const447 uint8_t Ipv6Interface::GetCurHopLimit () const
448 {
449 NS_LOG_FUNCTION (this);
450 return m_curHopLimit;
451 }
452
SetBaseReachableTime(uint16_t baseReachableTime)453 void Ipv6Interface::SetBaseReachableTime (uint16_t baseReachableTime)
454 {
455 NS_LOG_FUNCTION (this << baseReachableTime);
456 m_baseReachableTime = baseReachableTime;
457 }
458
GetBaseReachableTime() const459 uint16_t Ipv6Interface::GetBaseReachableTime () const
460 {
461 NS_LOG_FUNCTION (this);
462 return m_baseReachableTime;
463 }
464
SetReachableTime(uint16_t reachableTime)465 void Ipv6Interface::SetReachableTime (uint16_t reachableTime)
466 {
467 NS_LOG_FUNCTION (this << reachableTime);
468 m_reachableTime = reachableTime;
469 }
470
GetReachableTime() const471 uint16_t Ipv6Interface::GetReachableTime () const
472 {
473 NS_LOG_FUNCTION (this);
474 return m_reachableTime;
475 }
476
SetRetransTimer(uint16_t retransTimer)477 void Ipv6Interface::SetRetransTimer (uint16_t retransTimer)
478 {
479 NS_LOG_FUNCTION (this << retransTimer);
480 m_retransTimer = retransTimer;
481 }
482
GetRetransTimer() const483 uint16_t Ipv6Interface::GetRetransTimer () const
484 {
485 NS_LOG_FUNCTION (this);
486 return m_retransTimer;
487 }
488
SetState(Ipv6Address address,Ipv6InterfaceAddress::State_e state)489 void Ipv6Interface::SetState (Ipv6Address address, Ipv6InterfaceAddress::State_e state)
490 {
491 NS_LOG_FUNCTION (this << address << state);
492
493 for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
494 {
495 if (it->first.GetAddress () == address)
496 {
497 it->first.SetState (state);
498 return;
499 }
500 }
501 /* not found, maybe address has expired */
502 }
503
SetNsDadUid(Ipv6Address address,uint32_t uid)504 void Ipv6Interface::SetNsDadUid (Ipv6Address address, uint32_t uid)
505 {
506 NS_LOG_FUNCTION (this << address << uid);
507
508 for (Ipv6InterfaceAddressListI it = m_addresses.begin (); it != m_addresses.end (); ++it)
509 {
510 if (it->first.GetAddress () == address)
511 {
512 it->first.SetNsDadUid (uid);
513 return;
514 }
515 }
516 /* not found, maybe address has expired */
517 }
518
GetNdiscCache() const519 Ptr<NdiscCache> Ipv6Interface::GetNdiscCache () const
520 {
521 NS_LOG_FUNCTION (this);
522 return m_ndCache;
523 }
524
525 } /* namespace ns3 */
526
527