23 #include "ns3/uinteger.h"
24 #include "ns3/vector.h"
25 #include "ns3/boolean.h"
26 #include "ns3/callback.h"
27 #include "ns3/trace-source-accessor.h"
28 #include "ns3/object-vector.h"
29 #include "ns3/ipv6-routing-protocol.h"
30 #include "ns3/ipv6-route.h"
31 #include "ns3/mac16-address.h"
32 #include "ns3/mac64-address.h"
48 #define IPV6_MIN_MTU 1280
62 .AddConstructor<Ipv6L3Protocol> ()
63 .AddAttribute (
"DefaultTtl",
"The TTL value set by default on all outgoing packets generated on this node.",
66 MakeUintegerChecker<uint8_t> ())
67 .AddAttribute (
"DefaultTclass",
"The TCLASS value set by default on all outgoing packets generated on this node.",
70 MakeUintegerChecker<uint8_t> ())
71 .AddAttribute (
"InterfaceList",
"The set of IPv6 interfaces associated to this IPv6 stack.",
74 MakeObjectVectorChecker<Ipv6Interface> ())
75 .AddAttribute (
"SendIcmpv6Redirect",
"Send the ICMPv6 Redirect when appropriate.",
79 MakeBooleanChecker ())
80 .AddTraceSource (
"Tx",
"Send IPv6 packet to outgoing interface.",
82 .AddTraceSource (
"Rx",
"Receive IPv6 packet from incoming interface.",
84 .AddTraceSource (
"Drop",
"Drop IPv6 packet",
87 .AddTraceSource (
"SendOutgoing",
"A newly-generated packet by this node is about to be queued for transmission",
89 .AddTraceSource (
"UnicastForward",
"A unicast IPv6 packet was received by this node and is being forwarded to another node",
91 .AddTraceSource (
"LocalDeliver",
"An IPv6 packet was received by/for this node, and it is being forward up the stack",
137 (*it)->StopValidTimer ();
138 (*it)->StopPreferredTimer ();
169 interface->SetNode (
m_node);
170 interface->SetDevice (device);
215 uint32_t max = (*it)->GetNAddresses ();
217 for (j = 0; j < max; j++)
219 if ((*it)->GetAddress (j).GetAddress () ==
address)
237 for (j = 0; j < (*it)->GetNAddresses (); j++)
239 if ((*it)->GetAddress (j).GetAddress ().CombinePrefix (mask) == address.
CombinePrefix (mask))
262 if ((*it)->GetDevice () == device)
273 NS_LOG_FUNCTION (
this << interface << network << mask << (uint32_t)flags << validTime << preferredTime);
278 if (flags & (1 << 6))
295 NS_FATAL_ERROR (
"Unknown method to make autoconfigured address for this kind of device.");
302 if ((*it)->GetInterface () ==
interface && (*it)->GetPrefix () == network && (*it)->GetMask () == mask)
304 (*it)->StopPreferredTimer ();
305 (*it)->StopValidTimer ();
306 (*it)->StartPreferredTimer ();
318 if (!defaultRouter.
IsAny())
353 NS_FATAL_ERROR (
"Unknown method to make autoconfigured address for this kind of device.");
357 for (i = 0; i < max; i++)
369 if ((*it)->GetInterface () ==
interface && (*it)->GetPrefix () == network && (*it)->GetMask () == mask)
472 return interface->
GetDevice ()->GetMtu ();
486 return interface->
IsUp ();
499 if (interface->GetDevice ()->GetMtu () >= 1280)
510 NS_LOG_LOGIC (
"Interface " <<
int(i) <<
" is set to be down for IPv6. Reason: not respecting minimum IPv6 MTU (1280 octects)");
545 device = CreateObject<LoopbackNetDevice> ();
549 interface->SetDevice (device);
550 interface->SetNode (
m_node);
552 interface->AddAddress (ifaceAddr);
569 NS_LOG_LOGIC (
"Forwarding state: " << interface->IsForwarding ());
570 return interface->IsForwarding ();
595 NS_ASSERT_MSG (
false,
"No link-local address found on interface " << interface);
626 (*it)->SetForwarding (forward);
666 Ptr<Node> node = this->GetObject<Node> ();
703 if ((*i)->GetProtocolNumber () == protocolNumber)
763 NS_LOG_FUNCTION (
this << packet << source << destination << (uint32_t)protocol << route);
792 NS_LOG_LOGIC (
"Ipv6L3Protocol::Send case 1: passed in with a route");
803 NS_LOG_LOGIC (
"Ipv6L3Protocol::Send case 1: probably sent to machine on same IPv6 network");
813 NS_LOG_LOGIC (
"Ipv6L3Protocol::Send case 3: passed in with no route " << destination);
826 NS_ASSERT_MSG (index >= 0,
"Can not find an outgoing interface for a packet with src " << source <<
" and dst " << destination);
847 NS_LOG_FUNCTION (
this << device << p << protocol << from << to << packetType);
849 uint32_t
interface = 0;
857 if (ipv6Interface->
GetDevice () == device)
859 if (ipv6Interface->
IsUp ())
866 NS_LOG_LOGIC (
"Dropping received packet-- interface is down");
868 packet->RemoveHeader (hdr);
877 packet->RemoveHeader (hdr);
895 bool stopProcessing =
false;
896 bool isDropped =
false;
901 ipv6Extension = ipv6ExtensionDemux->GetExtension (nextHeader);
905 ipv6Extension->Process (packet, 0, hdr, hdr.
GetDestinationAddress (), (uint8_t *)0, stopProcessing, isDropped, dropReason);
925 NS_LOG_WARN (
"No route found for forwarding packet. Drop.");
942 int32_t
interface = GetInterfaceForDevice (dev);
946 NS_LOG_LOGIC (
"Send via NetDevice ifIndex " << dev->GetIfIndex () <<
" Ipv6InterfaceIndex " << interface);
949 std::list<Ptr<Packet> > fragments;
955 targetMtu = dev->GetMtu ();
958 if (packet->
GetSize () > targetMtu + 40)
980 icmpv6->SendErrorTooBig (packet, ipHeader.
GetSourceAddress (), dev->GetMtu ());
992 ipv6Fragment->
GetFragments (packet, targetMtu, fragments);
997 if (outInterface->IsUp ())
999 NS_LOG_LOGIC (
"Send to gateway " << route->GetGateway ());
1001 if (fragments.size () != 0)
1003 std::ostringstream oss;
1006 for (
std::list<
Ptr<Packet> >::const_iterator it = fragments.begin (); it != fragments.end (); it++)
1009 outInterface->Send (*it, route->GetGateway ());
1016 outInterface->Send (packet, route->GetGateway ());
1021 NS_LOG_LOGIC (
"Dropping-- outgoing interface is down: " << route->GetGateway ());
1027 if (outInterface->IsUp ())
1031 if (fragments.size () != 0)
1033 std::ostringstream oss;
1036 for (
std::list<
Ptr<Packet> >::const_iterator it = fragments.begin (); it != fragments.end (); it++)
1065 NS_LOG_WARN (
"Received a packet for 2001:db8::/32 (documentation class). Drop.");
1116 if (target.
IsAny ())
1124 if (icmpv6->Lookup (target, rtentry->GetOutputDevice (), 0, &hardwareTarget))
1126 icmpv6->SendRedirection (copy, linkLocal, src, target, dst, hardwareTarget);
1130 icmpv6->SendRedirection (copy, linkLocal, src, target, dst,
Address ());
1143 std::map<uint32_t, uint32_t> ttlMap = mrtentry->GetOutputTtlMap ();
1144 std::map<uint32_t, uint32_t>::iterator mapIter;
1146 for (mapIter = ttlMap.begin (); mapIter != ttlMap.end (); mapIter++)
1148 uint32_t interfaceId = mapIter->first;
1159 NS_LOG_LOGIC (
"Forward multicast via interface " << interfaceId);
1180 uint8_t nextHeaderPosition = 0;
1181 bool isDropped =
false;
1182 bool stopProcessing =
false;
1193 NS_LOG_WARN(
"Double Ipv6Header::IPV6_EXT_HOP_BY_HOP in packet, dropping packet");
1202 ipv6Extension = ipv6ExtensionDemux->GetExtension (nextHeader);
1206 uint8_t nextHeaderStep = 0;
1207 uint8_t curHeader = nextHeader;
1208 nextHeaderStep = ipv6Extension->Process (p, nextHeaderPosition, ip, dst, &nextHeader, stopProcessing, isDropped, dropReason);
1209 nextHeaderPosition += nextHeaderStep;
1221 "Zero-size IPv6 Option Header, aborting" << *packet );
1234 if (nextHeaderPosition == 0)
1278 while (ipv6Extension);
1284 NS_LOG_LOGIC (
"Route input failure-- dropping packet to " << ipHeader <<
" with errno " << sockErrno);
1290 NS_LOG_FUNCTION (
this << src << dst << (uint32_t)protocol << (uint32_t)payloadSize << (uint32_t)ttl << (uint32_t)tclass);
1305 ipv6ExtensionDemux->SetNode (
m_node);
1308 hopbyhopExtension->SetNode (
m_node);
1310 destinationExtension->SetNode (
m_node);
1312 fragmentExtension->SetNode (
m_node);
1318 ipv6ExtensionDemux->Insert (hopbyhopExtension);
1319 ipv6ExtensionDemux->Insert (destinationExtension);
1320 ipv6ExtensionDemux->Insert (fragmentExtension);
1321 ipv6ExtensionDemux->Insert (routingExtension);
1326 routingExtensionDemux->SetNode (
m_node);
1328 looseRoutingExtension->SetNode (
m_node);
1329 routingExtensionDemux->Insert (looseRoutingExtension);
1338 ipv6OptionDemux->SetNode (
m_node);
1341 pad1Option->SetNode (
m_node);
1343 padnOption->SetNode (
m_node);
1345 jumbogramOption->SetNode (
m_node);
1347 routerAlertOption->SetNode (
m_node);
1349 ipv6OptionDemux->Insert (pad1Option);
1350 ipv6OptionDemux->Insert (padnOption);
1351 ipv6OptionDemux->Insert (jumbogramOption);
1352 ipv6OptionDemux->Insert (routerAlertOption);
void SetNode(Ptr< Node > node)
Set the node associated with this socket.
bool IsAny() const
If the IPv6 address is the "Any" address.
void GetFragments(Ptr< Packet > packet, uint32_t fragmentSize, std::list< Ptr< Packet > > &listFragments)
Fragment a packet.
static bool IsMatchingType(const Address &address)
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberContainer)
static Ipv6Address GetLoopback()
Get the loopback address.
Ipv6Header BuildHeader(Ipv6Address src, Ipv6Address dst, uint8_t protocol, uint16_t payloadSize, uint8_t hopLimit, uint8_t tclass)
Construct an IPv6 header.
void SetForwarding(uint32_t i, bool val)
Enable or disable forwarding on interface.
bool AddAddress(Ipv6InterfaceAddress iface)
Add an IPv6 address.
smart pointer class similar to boost::intrusive_ptr
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual ~Ipv6L3Protocol()
Destructor.
void SetUp()
Enable this interface.
Ipv6L3Protocol()
Constructor.
bool IsLinkLocalMulticast() const
If the IPv6 address is link-local multicast (ff02::/16).
virtual void SetPmtu(Ipv6Address dst, uint32_t pmtu)
Set the Path MTU for the specified IPv6 destination address.
This class implements a tag that carries the socket-specific HOPLIMIT of a packet to the IPv6 layer...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Ptr< NetDevice > GetNetDevice(uint32_t i)
Get device by index.
bool AddAddress(uint32_t i, Ipv6InterfaceAddress address)
Add an address on interface.
Ptr< Ipv6RoutingProtocol > m_routingProtocol
Routing protocol.
Access to the IPv6 forwarding table, interfaces, and configuration.
static Mac16Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
Ptr< Ipv6RoutingProtocol > GetRoutingProtocol() const
Get current routing protocol used.
uint32_t AddIpv6Interface(Ptr< Ipv6Interface > interface)
Add an IPv6 interface to the stack.
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.
bool IsLinkLocal() const
If the IPv6 address is a link-local address (fe80::/64).
Interface is down so can not send packet.
void SetNode(Ptr< Node > node)
Set the node.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Ipv6InterfaceAddress GetAddress(uint32_t index) const
Get an address from IPv6 interface.
static Ipv6Address MakeAutoconfiguredAddress(Mac16Address addr, Ipv6Address prefix)
Make the autoconfigured IPv6 address with Mac16Address.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
IPv6 address associated with an interface.
uint32_t GetNAddresses(uint32_t interface) const
Get number of address for an interface.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Link-local address (fe80::/64)
void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Receive method when a packet arrive in the stack.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
bool IsDocumentation() const
If the IPv6 address is a documentation address (2001:DB8::/32).
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_FATAL_ERROR(msg)
fatal error handling
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual bool GetSendIcmpv6Redirect() const
Get the ICMPv6 Redirect sending state.
virtual void DoDispose()
Dispose object.
void SetMetric(uint32_t i, uint16_t metric)
Set metric for an interface.
bool ForwardUp(Ptr< const Packet > p, Ipv6Header hdr, Ptr< NetDevice > device)
Forward up to receive method.
uint32_t AddInterface(Ptr< NetDevice > device)
Add IPv6 interface for a device.
SocketErrno
Enumeration of the possible errors returned by a socket.
Ptr< Node > m_node
Node attached to stack.
TracedCallback< const Ipv6Header &, Ptr< const Packet >, uint32_t > m_localDeliverTrace
Trace of locally delivered packets.
bool IsUp() const
Is the interface UP ?
a polymophic address class
virtual void SetIpForward(bool forward)
Set IPv6 forwarding state.
virtual void NotifyNewAggregate()
Notify other components connected to the node that a new stack member is now connected.
Ptr< T > CreateObject(void)
bool IsUp(uint32_t i) const
Is specified interface up ?
uint32_t m_nInterfaces
Number of IPv6 interfaces managed by the stack.
void SetDefaultTclass(uint8_t tclass)
Set the default TCLASS.
Ipv6Address GetAddress() const
Get the IPv6 address.
uint32_t GetNAddresses(void) const
Get number of addresses on this IPv6 interface.
void SetMetric(uint16_t metric)
Set the metric.
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
int32_t GetInterfaceForPrefix(Ipv6Address addr, Ipv6Prefix mask) const
Get interface index which match specified address/prefix.
uint8_t GetTclass(void) const
Get the tag's Tclass.
Ptr< Socket > CreateRawSocket()
Create raw IPv6 socket.
#define IPV6_MIN_MTU
Minimum IPv6 MTU, as defined by RFC 2460
Hold an unsigned integer type.
L4List_t m_protocols
List of transport protocol.
void SetDefaultTtl(uint8_t ttl)
Set the default TTL.
static Ipv6Address GetZero()
Get the 0 (::) Ipv6Address.
void IpMulticastForward(Ptr< const NetDevice > idev, Ptr< Ipv6MulticastRoute > mrtentry, Ptr< const Packet > p, const Ipv6Header &header)
Forward a multicast packet.
T * PeekPointer(const Ptr< T > &p)
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
void SetUp(uint32_t i)
Set an interface up.
An implementation of the ICMPv6 protocol.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
virtual enum RxStatus Receive(Ptr< Packet > p, Ipv4Header const &header, Ptr< Ipv4Interface > incomingInterface)=0
Called from lower-level layers to send the packet up in the stack.
virtual void ReportDrop(Ipv6Header ipHeader, Ptr< Packet > p, DropReason dropReason)
Report a packet drop.
virtual Ptr< NetDevice > GetDevice() const
Get the NetDevice.
static TypeId GetTypeId()
Get the type ID of this class.
Ipv6InterfaceList m_interfaces
List of IPv6 interfaces.
void AggregateObject(Ptr< Object > other)
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
bool m_ipForward
Forwarding packets (i.e.
uint32_t GetNInterfaces() const
Get current number of interface on this stack.
TracedCallback< const Ipv6Header &, Ptr< const Packet >, uint32_t > m_unicastForwardTrace
Trace of unicast forwarded packets.
static Mac48Address ConvertFrom(const Address &address)
uint16_t GetMetric(uint32_t i) const
Get metric for an interface.
virtual void NotifyNewAggregate(void)
This method is invoked whenever two sets of objects are aggregated together.
uint32_t GetNDevices(void) const
void SetForwarding(bool forward)
Set forwarding enabled or not.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
bool IsInSameSubnet(Ipv6Address b) const
Checks if the address is in the same subnet.
std::list< Ptr< Ipv6AutoconfiguredPrefix > >::iterator Ipv6AutoconfiguredPrefixListI
Iterator of the container of the IPv6 Autoconfigured addresses.
Ipv6AutoconfiguredPrefixList m_prefixes
List of IPv6 prefix received from RA.
void LocalDeliver(Ptr< const Packet > p, Ipv6Header const &ip, uint32_t iif)
Deliver a packet.
void DeleteRawSocket(Ptr< Socket > socket)
Remove raw IPv6 socket.
Global address (2000::/3)
virtual bool GetIpForward() const
Get IPv6 forwarding state.
static uint16_t GetStaticProtocolNumber()
Get ICMPv6 protocol number.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Ipv6Address SourceAddressSelection(uint32_t interface, Ipv6Address dest)
Choose the source address to use with destination address.
void IpForward(Ptr< const NetDevice > idev, Ptr< Ipv6Route > rtentry, Ptr< const Packet > p, const Ipv6Header &header)
Forward a packet.
Ipv6InterfaceAddress GetAddress(uint32_t interfaceIndex, uint32_t addressIndex) const
Get an address.
uint8_t m_defaultTtl
Default TTL for outgoing packets.
indicates whether the socket has IPV6_TCLASS set.
TracedCallback< const Ipv6Header &, Ptr< const Packet >, DropReason, Ptr< Ipv6 >, uint32_t > m_dropTrace
Callback to trace drop packets.
bool IsMulticast() const
If the IPv6 address is multicast (ff00::/8).
TracedCallback< Ptr< const Packet >, Ptr< Ipv6 >, uint32_t > m_rxTrace
Callback to trace RX (reception) packets.
int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const
Get interface index which is on a specified net device.
void SetNode(Ptr< Node > node)
Set node associated with this stack.
bool IsForwarding(uint32_t i) const
Is interface allows forwarding ?
void RemoveAutoconfiguredAddress(uint32_t interface, Ipv6Address network, Ipv6Prefix mask, Ipv6Address defaultRouter)
Remove an autoconfigured address.
void SendRealOut(Ptr< Ipv6Route > route, Ptr< Packet > packet, Ipv6Header const &ipHeader)
Send packet with route.
void RegisterProtocolHandler(ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device, bool promiscuous=false)
void SetupLoopback()
Setup loopback interface.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Doxygen introspection did not find any typical Config paths.
virtual void RegisterExtensions()
Register the IPv6 Extensions.
uint8_t GetHopLimit(void) const
Get the tag's Hop Limit.
Ipv6InterfaceAddress GetLinkLocalAddress() const
Get link-local address from IPv6 interface.
Describes an IPv6 address.
uint16_t GetMetric() const
Get the metric.
void SetRoutingProtocol(Ptr< Ipv6RoutingProtocol > routingProtocol)
Set routing protocol for this stack.
Ipv6InterfaceAddress::Scope_e GetScope() const
Get address scope.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
virtual Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const
Get L4 protocol by protocol number.
uint32_t GetId(void) const
Ptr< Icmpv6L4Protocol > GetIcmpv6() const
Get ICMPv6 protocol.
static Mac64Address ConvertFrom(const Address &address)
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
void Insert(Ptr< IpL4Protocol > protocol)
Add an L4 protocol.
uint16_t GetMtu(uint32_t i) const
Get MTU for an interface.
bool m_sendIcmpv6Redirect
Allow ICMPv6 Redirect sending state.
virtual void RegisterOptions()
Register the IPv6 Options.
TracedCallback< const Ipv6Header &, Ptr< const Packet >, uint32_t > m_sendOutgoingTrace
Trace of sent packets.
Describes an IPv6 prefix.
void SetDown(uint32_t i)
set an interface down.
uint32_t CopyData(uint8_t *buffer, uint32_t size) const
Copy the packet contents to a byte buffer.
bool m_mtuDiscover
MTU Discover (i.e.
int32_t GetInterfaceForAddress(Ipv6Address addr) const
Get interface index which has specified IPv6 address.
bool RemoveAddress(uint32_t interfaceIndex, uint32_t addressIndex)
Remove an address from an interface.
PacketType
Packet types are used as they are in Linux.
contain a set of ns3::Object pointers.
Ptr< Ipv6PmtuCache > m_pmtuCache
Path MTU Cache.
static bool IsMatchingType(const Address &address)
virtual bool GetMtuDiscover(void) const
Get IPv6 MTU discover state.
TracedCallback< Ptr< const Packet >, Ptr< Ipv6 >, uint32_t > m_txTrace
Callback to trace TX (transmission) packets.
virtual void SetMtuDiscover(bool mtuDiscover)
Set IPv6 MTU discover state.
Ipv6Address CombinePrefix(Ipv6Prefix const &prefix)
Combine this address with a prefix.
uint8_t m_defaultTclass
Default TCLASS for outgoing packets.
Ptr< T > GetObject(void) const
void SetDown()
Disable this interface.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
SocketList m_sockets
List of IPv6 raw sockets.
static const uint16_t PROT_NUMBER
The protocol number for IPv6 (0x86DD).
Ptr< Ipv6Interface > GetInterface(uint32_t i) const
Get an interface.
void AddHeader(const Header &header)
Add header to this packet.
void Remove(Ptr< IpL4Protocol > protocol)
Remove an L4 protocol.
void AddAutoconfiguredAddress(uint32_t interface, Ipv6Address network, Ipv6Prefix mask, uint8_t flags, uint32_t validTime, uint32_t preferredTime, Ipv6Address defaultRouter=Ipv6Address::GetZero())
Add an autoconfigured address with RA information.
DropReason
Reason why a packet has been dropped.
virtual void SetSendIcmpv6Redirect(bool sendIcmpv6Redirect)
Set the ICMPv6 Redirect sending state.
void RouteInputError(Ptr< const Packet > p, const Ipv6Header &ipHeader, Socket::SocketErrno sockErrno)
Fallback when no route is found.