25 #include "ns3/ipv4-click-routing.h"
27 #include "ns3/socket.h"
28 #include "ns3/ethernet-header.h"
29 #include "ns3/llc-snap-header.h"
30 #include "ns3/net-device.h"
31 #include "ns3/uinteger.h"
32 #include "ns3/object-vector.h"
34 #include "ns3/ipv4-raw-socket-impl.h"
35 #include "ns3/arp-l3-protocol.h"
36 #include "ns3/ip-l4-protocol.h"
37 #include "ns3/icmpv4-l4-protocol.h"
38 #include "ns3/loopback-net-device.h"
44 const uint16_t Ipv4L3ClickProtocol::PROT_NUMBER = 0x0800;
52 static TypeId tid = TypeId (
"ns3::Ipv4L3ClickProtocol")
54 .AddConstructor<Ipv4L3ClickProtocol> ()
55 .AddAttribute (
"DefaultTtl",
"The TTL value set by default on all outgoing packets generated on this node.",
57 MakeUintegerAccessor (&Ipv4L3ClickProtocol::m_defaultTtl),
58 MakeUintegerChecker<uint8_t> ())
59 .AddAttribute (
"InterfaceList",
"The set of Ipv4 interfaces associated to this Ipv4 stack.",
62 MakeObjectVectorChecker<Ipv4Interface> ())
67 Ipv4L3ClickProtocol::Ipv4L3ClickProtocol ()
68 : m_identification (0)
72 Ipv4L3ClickProtocol::~Ipv4L3ClickProtocol ()
80 for (L4List_t::iterator i = m_protocols.begin (); i != m_protocols.end (); ++i)
86 for (Ipv4InterfaceList::iterator i = m_interfaces.begin (); i != m_interfaces.end (); ++i)
90 m_interfaces.clear ();
93 m_routingProtocol = 0;
102 Ptr<Node>node = this->GetObject<Node> ();
107 this->SetNode (node);
117 m_routingProtocol = routingProtocol;
118 m_routingProtocol->SetIpv4 (
this);
122 Ptr<Ipv4RoutingProtocol>
125 return m_routingProtocol;
129 Ipv4L3ClickProtocol::GetInterface (uint32_t index)
const
132 if (index < m_interfaces.size ())
134 return m_interfaces[index];
143 return m_interfaces.size ();
152 int32_t
interface = 0;
153 for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin ();
154 i != m_interfaces.end ();
157 for (uint32_t j = 0; j < (*i)->GetNAddresses (); j++)
159 if ((*i)->GetAddress (j).GetLocal () ==
address)
176 int32_t
interface = 0;
177 for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin ();
178 i != m_interfaces.end ();
181 for (uint32_t j = 0; j < (*i)->GetNAddresses (); j++)
183 if ((*i)->GetAddress (j).GetLocal ().CombineMask (mask) == address.CombineMask (mask))
195 Ptr<const NetDevice> device)
const
199 int32_t
interface = 0;
200 for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin ();
201 i != m_interfaces.end ();
204 if ((*i)->GetDevice () == device)
221 Ipv4InterfaceAddress iaddr =
GetAddress (iif, i);
222 if (address == iaddr.GetLocal ())
224 NS_LOG_LOGIC (
"For me (destination " << address <<
" match)");
227 if (address == iaddr.GetBroadcast ())
234 if (address.IsMulticast ())
237 if (MulticastCheckGroup (iif, address ))
246 if (address.IsBroadcast ())
256 if (j == uint32_t (iif))
262 Ipv4InterfaceAddress iaddr =
GetAddress (j, i);
263 if (address == iaddr.GetLocal ())
265 NS_LOG_LOGIC (
"For me (destination " << address <<
" match) on another interface");
269 if (address == iaddr.GetBroadcast ())
271 NS_LOG_LOGIC (
"For me (interface broadcast address on another interface)");
284 m_ipForward = forward;
285 for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin (); i != m_interfaces.end (); i++)
287 (*i)->SetForwarding (forward);
300 m_weakEsModel = model;
306 return m_weakEsModel;
313 return GetInterface (i)->GetDevice ();
317 Ipv4L3ClickProtocol::SetDefaultTtl (uint8_t ttl)
324 Ipv4L3ClickProtocol::SetupLoopback (
void)
328 Ptr<Ipv4Interface>
interface =
CreateObject<Ipv4Interface> ();
329 Ptr<LoopbackNetDevice> device = 0;
331 for (uint32_t i = 0; i < m_node->GetNDevices (); i++)
333 if ((device = DynamicCast<LoopbackNetDevice> (m_node->GetDevice (i))))
340 device = CreateObject<LoopbackNetDevice> ();
341 m_node->AddDevice (device);
343 interface->SetDevice (device);
344 interface->SetNode (m_node);
346 interface->AddAddress (ifaceAddr);
347 uint32_t index = AddIpv4Interface (interface);
348 Ptr<Node> node = GetObject<Node> ();
349 node->RegisterProtocolHandler (
MakeCallback (&Ipv4L3ClickProtocol::Receive,
this),
350 Ipv4L3ClickProtocol::PROT_NUMBER, device);
352 if (m_routingProtocol != 0)
354 m_routingProtocol->NotifyInterfaceUp (index);
362 Ptr<Ipv4RawSocketImpl> socket = CreateObject<Ipv4RawSocketImpl> ();
363 socket->SetNode (m_node);
364 m_sockets.push_back (socket);
371 for (SocketList::iterator i = m_sockets.begin (); i != m_sockets.end (); ++i)
384 Ipv4L3ClickProtocol::SetNode (Ptr<Node> node)
395 Ptr<Ipv4Interface>
interface = GetInterface (i);
396 bool retVal = interface->AddAddress (address);
397 if (m_routingProtocol != 0)
399 m_routingProtocol->NotifyAddAddress (i, address);
408 Ptr<Ipv4Interface>
interface = GetInterface (interfaceIndex);
409 return interface->GetAddress (addressIndex);
416 Ptr<Ipv4Interface> iface = GetInterface (interface);
417 return iface->GetNAddresses ();
424 Ptr<Ipv4Interface>
interface = GetInterface (i);
425 Ipv4InterfaceAddress address = interface->RemoveAddress (addressIndex);
426 if (address != Ipv4InterfaceAddress ())
428 if (m_routingProtocol != 0)
430 m_routingProtocol->NotifyRemoveAddress (i, address);
447 Ptr<Ipv4Interface>
interface = GetInterface (i);
448 Ipv4InterfaceAddress ifAddr = interface->RemoveAddress (address);
449 if (ifAddr != Ipv4InterfaceAddress ())
451 if (m_routingProtocol != 0)
453 m_routingProtocol->NotifyRemoveAddress (i, ifAddr);
465 Ipv4Address addr (
"0.0.0.0");
466 Ipv4InterfaceAddress iaddr;
476 if (iaddr.IsSecondary ())
480 if (iaddr.GetScope () > scope)
484 if (dst.CombineMask (iaddr.GetMask ()) == iaddr.GetLocal ().CombineMask (iaddr.GetMask ()) )
486 return iaddr.GetLocal ();
490 addr = iaddr.GetLocal ();
506 if (iaddr.IsSecondary ())
511 && iaddr.GetScope () <= scope)
513 return iaddr.GetLocal ();
517 NS_LOG_WARN (
"Could not find source address for " << dst <<
" and scope "
518 << scope <<
", returning 0");
526 Ptr<Ipv4Interface>
interface = GetInterface (i);
527 interface->SetMetric (metric);
534 Ptr<Ipv4Interface>
interface = GetInterface (i);
535 return interface->GetMetric ();
542 Ptr<Ipv4Interface>
interface = GetInterface (i);
543 return interface->GetDevice ()->GetMtu ();
550 Ptr<Ipv4Interface>
interface = GetInterface (i);
551 return interface->IsUp ();
558 Ptr<Ipv4Interface>
interface = GetInterface (i);
561 if (m_routingProtocol != 0)
563 m_routingProtocol->NotifyInterfaceUp (i);
571 Ptr<Ipv4Interface>
interface = GetInterface (ifaceIndex);
572 interface->SetDown ();
574 if (m_routingProtocol != 0)
576 m_routingProtocol->NotifyInterfaceDown (ifaceIndex);
584 Ptr<Ipv4Interface>
interface = GetInterface (i);
585 NS_LOG_LOGIC (
"Forwarding state: " << interface->IsForwarding ());
586 return interface->IsForwarding ();
593 Ptr<Ipv4Interface>
interface = GetInterface (i);
594 interface->SetForwarding (val);
598 Ipv4L3ClickProtocol::SetPromisc (uint32_t i)
603 Ptr<Node> node = GetObject<Node> ();
605 node->RegisterProtocolHandler (
MakeCallback (&Ipv4L3ClickProtocol::Receive,
this),
613 Ptr<Node> node = GetObject<Node> ();
614 node->RegisterProtocolHandler (
MakeCallback (&Ipv4L3ClickProtocol::Receive,
this),
615 Ipv4L3ClickProtocol::PROT_NUMBER, device);
616 node->RegisterProtocolHandler (
MakeCallback (&Ipv4L3ClickProtocol::Receive,
this),
619 Ptr<Ipv4Interface>
interface =
CreateObject<Ipv4Interface> ();
620 interface->SetNode (m_node);
621 interface->SetDevice (device);
622 interface->SetForwarding (m_ipForward);
623 return AddIpv4Interface (interface);
627 Ipv4L3ClickProtocol::AddIpv4Interface (Ptr<Ipv4Interface>interface)
630 uint32_t index = m_interfaces.size ();
631 m_interfaces.push_back (interface);
639 Ipv4L3ClickProtocol::BuildHeader (
641 Ipv4Address destination,
643 uint16_t payloadSize,
649 ipHeader.SetSource (source);
650 ipHeader.SetDestination (destination);
651 ipHeader.SetProtocol (protocol);
652 ipHeader.SetPayloadSize (payloadSize);
653 ipHeader.SetTtl (ttl);
654 if (mayFragment ==
true)
656 ipHeader.SetMayFragment ();
657 ipHeader.SetIdentification (m_identification);
662 ipHeader.SetDontFragment ();
664 ipHeader.SetIdentification (m_identification);
669 ipHeader.EnableChecksum ();
677 Ipv4Address destination,
679 Ptr<Ipv4Route> route)
681 NS_LOG_FUNCTION (
this << packet << source << destination << uint32_t (protocol) << route);
684 bool mayFragment =
true;
685 uint8_t ttl = m_defaultTtl;
687 bool found = packet->RemovePacketTag (tag);
693 ipHeader = BuildHeader (source, destination, protocol, packet->GetSize (), ttl, mayFragment);
694 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting> (m_routingProtocol);
697 ipHeader.EnableChecksum ();
699 packet->AddHeader (ipHeader);
700 click->Send (packet->Copy (), source, destination);
707 Ptr<Ipv4Route> route)
711 Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting> (m_routingProtocol);
714 ipHeader.EnableChecksum ();
716 packet->AddHeader (ipHeader);
717 click->Send (packet->Copy (), ipHeader.GetSource (), ipHeader.GetDestination ());
721 Ipv4L3ClickProtocol::SendDown (Ptr<Packet> p,
int ifid)
732 EthernetHeader header;
733 p->RemoveHeader (header);
737 if (header.GetLengthType () <= 1500)
740 p->RemoveHeader (llc);
741 protocol = llc.GetType ();
745 protocol = header.GetLengthType ();
750 netdev->Send (p, header.GetDestination (), protocol);
754 Ipv4L3ClickProtocol::Receive ( Ptr<NetDevice> device, Ptr<const Packet> p, uint16_t protocol,
const Address &from,
760 if (protocol == Ipv4L3ClickProtocol::PROT_NUMBER && m_sockets.size () > 0)
762 Ptr<Packet> packetForRawSocket = p->Copy ();
763 uint32_t
interface = 0;
764 Ptr<Ipv4Interface> ipv4Interface;
765 for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin ();
766 i != m_interfaces.end ();
770 if (ipv4Interface->GetDevice () == device)
772 if (ipv4Interface->IsUp ())
778 NS_LOG_LOGIC (
"Dropping received packet -- interface is down");
787 ipHeader.EnableChecksum ();
789 packetForRawSocket->RemoveHeader (ipHeader);
792 for (SocketList::iterator i = m_sockets.begin (); i != m_sockets.end (); ++i)
795 Ptr<Ipv4RawSocketImpl> socket = *i;
796 socket->ForwardUp (packetForRawSocket, ipHeader, ipv4Interface);
800 Ptr<Packet> packet = p->Copy ();
807 hdr.SetLengthType (protocol);
808 packet->AddHeader (hdr);
815 Ipv4L3ClickProtocol::LocalDeliver (Ptr<const Packet> packet, Ipv4Header
const&ip, uint32_t iif)
818 Ptr<Packet> p = packet->Copy ();
820 m_localDeliverTrace (ip, packet, iif);
822 Ptr<IpL4Protocol> protocol =
GetProtocol (ip.GetProtocol ());
827 Ptr<Packet> copy = p->Copy ();
829 protocol->Receive (p, ip, GetInterface (iif));
839 if (ip.GetDestination ().IsBroadcast () ==
true
840 || ip.GetDestination ().IsMulticast () ==
true)
845 bool subnetDirected =
false;
848 Ipv4InterfaceAddress addr =
GetAddress (iif, i);
849 if (addr.GetLocal ().CombineMask (addr.GetMask ()) == ip.GetDestination ().CombineMask (addr.GetMask ())
850 && ip.GetDestination ().IsSubnetDirectedBroadcast (addr.GetMask ()))
852 subnetDirected =
true;
855 if (subnetDirected ==
false)
857 GetIcmp ()->SendDestUnreachPort (ip, copy);
863 Ptr<Icmpv4L4Protocol>
864 Ipv4L3ClickProtocol::GetIcmp (
void)
const
869 return prot->GetObject<Icmpv4L4Protocol> ();
880 m_protocols.push_back (protocol);
886 for (L4List_t::const_iterator i = m_protocols.begin (); i != m_protocols.end (); ++i)
888 if ((*i)->GetProtocolNumber () == protocolNumber)
static TypeId GetTypeId(void)
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberContainer)
virtual uint32_t AddInterface(Ptr< NetDevice > device)=0
virtual int32_t GetInterfaceForPrefix(Ipv4Address address, Ipv4Mask mask) const =0
Return the interface number of first interface found that has an Ipv4 address within the prefix speci...
#define NS_LOG_FUNCTION(parameters)
virtual void SetWeakEsModel(bool model)=0
static bool ChecksumEnabled(void)
#define NS_ASSERT(condition)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual void DoDispose(void)
#define NS_LOG_FUNCTION_NOARGS()
virtual Ptr< NetDevice > GetNetDevice(uint32_t interface)=0
virtual void SetForwarding(uint32_t interface, bool val)=0
virtual Ptr< Ipv4RoutingProtocol > GetRoutingProtocol(void) const =0
Get the routing protocol to be used by this Ipv4 stack.
Ptr< T > CreateObject(void)
virtual bool IsDestinationAddress(Ipv4Address address, uint32_t iif) const =0
Determine whether address and interface corresponding to received packet can be accepted for local de...
virtual bool GetIpForward(void) const =0
virtual void DeleteRawSocket(Ptr< Socket > socket)=0
Deletes a particular raw socket.
virtual Ptr< IpL4Protocol > GetProtocol(int protocolNumber) const =0
virtual void SetUp(uint32_t interface)=0
virtual uint16_t GetMetric(uint32_t interface) const =0
NS_OBJECT_ENSURE_REGISTERED(AntennaModel)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
virtual void SendWithHeader(Ptr< Packet > packet, Ipv4Header ipHeader, Ptr< Ipv4Route > route)=0
#define NS_LOG_LOGIC(msg)
static Mac48Address ConvertFrom(const Address &address)
virtual void NotifyNewAggregate(void)
virtual bool GetWeakEsModel(void) const =0
static const uint16_t PROT_NUMBER
virtual bool IsForwarding(uint32_t interface) const =0
virtual uint16_t GetMtu(uint32_t interface) const =0
virtual void Send(Ptr< Packet > packet, Ipv4Address source, Ipv4Address destination, uint8_t protocol, Ptr< Ipv4Route > route)=0
static Ipv4Address GetLoopback(void)
static Ipv4Mask GetLoopback(void)
virtual Ptr< Socket > CreateRawSocket(void)=0
Creates a raw socket.
#define NS_ASSERT_MSG(condition, message)
virtual bool IsUp(uint32_t interface) const =0
virtual void SetIpForward(bool forward)=0
virtual void Insert(Ptr< IpL4Protocol > protocol)=0
virtual Ipv4InterfaceAddress GetAddress(uint32_t interface, uint32_t addressIndex) const =0
static uint16_t GetStaticProtocolNumber(void)
virtual uint32_t GetNAddresses(uint32_t interface) const =0
virtual Ipv4Address SelectSourceAddress(Ptr< const NetDevice > device, Ipv4Address dst, Ipv4InterfaceAddress::InterfaceAddressScope_e scope)=0
Return the first primary source address with scope less than or equal to the requested scope...
virtual void SetMetric(uint32_t interface, uint16_t metric)=0
virtual int32_t GetInterfaceForDevice(Ptr< const NetDevice > device) const =0
virtual bool AddAddress(uint32_t interface, Ipv4InterfaceAddress address)=0
virtual int32_t GetInterfaceForAddress(Ipv4Address address) const =0
Return the interface number of the interface that has been assigned the specified IP address...
ObjectPtrContainerValue ObjectVectorValue
virtual void SetDown(uint32_t interface)=0
virtual bool RemoveAddress(uint32_t interface, uint32_t addressIndex)=0
TypeId SetParent(TypeId tid)
virtual uint32_t GetNInterfaces(void) const =0
virtual void SetRoutingProtocol(Ptr< Ipv4RoutingProtocol > routingProtocol)=0
Register a new routing protocol to be used by this Ipv4 stack.