Skip to content

Commit

Permalink
Bug 1477: Click doesn't work with raw sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
lalithsuresh committed Jul 26, 2012
1 parent b75cfdb commit db3b602
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 8 deletions.
60 changes: 59 additions & 1 deletion src/click/model/ipv4-l3-click-protocol.cc
Expand Up @@ -574,7 +574,7 @@ Ipv4L3ClickProtocol::SetForwarding (uint32_t i, bool val)
void
Ipv4L3ClickProtocol::SetPromisc (uint32_t i)
{
NS_ASSERT(i <= m_node->GetNDevices ());
NS_ASSERT (i <= m_node->GetNDevices ());
Ptr<NetDevice> netdev = GetNetDevice (i);
NS_ASSERT (netdev);
Ptr<Node> node = GetObject<Node> ();
Expand Down Expand Up @@ -678,6 +678,22 @@ Ipv4L3ClickProtocol::Send (Ptr<Packet> packet,
return;
}

void
Ipv4L3ClickProtocol::SendWithHeader (Ptr<Packet> packet,
Ipv4Header ipHeader,
Ptr<Ipv4Route> route)
{
NS_LOG_FUNCTION (this << packet << ipHeader << route);

Ptr<Ipv4ClickRouting> click = DynamicCast<Ipv4ClickRouting> (m_routingProtocol);
if (Node::ChecksumEnabled ())
{
ipHeader.EnableChecksum ();
}
packet->AddHeader (ipHeader);
click->Send (packet->Copy (), ipHeader.GetSource (), ipHeader.GetDestination ());
}

void
Ipv4L3ClickProtocol::SendDown (Ptr<Packet> p, int ifid)
{
Expand Down Expand Up @@ -716,6 +732,48 @@ Ipv4L3ClickProtocol::Receive ( Ptr<NetDevice> device, Ptr<const Packet> p, uint1
const Address &to, NetDevice::PacketType packetType)
{
NS_LOG_FUNCTION (this << device << p << from << to);

// Forward packet to raw sockets, if any
if (protocol == Ipv4L3ClickProtocol::PROT_NUMBER && m_sockets.size () > 0)
{
Ptr<Packet> packetForRawSocket = p->Copy ();
uint32_t interface = 0;
Ptr<Ipv4Interface> ipv4Interface;
for (Ipv4InterfaceList::const_iterator i = m_interfaces.begin ();
i != m_interfaces.end ();
i++, interface++)
{
ipv4Interface = *i;
if (ipv4Interface->GetDevice () == device)
{
if (ipv4Interface->IsUp ())
{
break;
}
else
{
NS_LOG_LOGIC ("Dropping received packet -- interface is down");
return;
}
}
}

Ipv4Header ipHeader;
if (Node::ChecksumEnabled ())
{
ipHeader.EnableChecksum ();
}
packetForRawSocket->RemoveHeader (ipHeader);


for (SocketList::iterator i = m_sockets.begin (); i != m_sockets.end (); ++i)
{
NS_LOG_LOGIC ("Forwarding to raw socket");
Ptr<Ipv4RawSocketImpl> socket = *i;
socket->ForwardUp (packetForRawSocket, ipHeader, ipv4Interface);
}
}

Ptr<Packet> packet = p->Copy ();

// Add an ethernet frame. This allows
Expand Down
10 changes: 10 additions & 0 deletions src/click/model/ipv4-l3-click-protocol.h
Expand Up @@ -116,6 +116,16 @@ class Ipv4L3ClickProtocol : public Ipv4
void Send (Ptr<Packet> packet, Ipv4Address source,
Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route);

/**
* \param packet packet to send
* \param ipHeader IP Header
* \param route route entry
*
* Higher-level layers call this method to send a packet with IPv4 Header
* (Intend to be used with IpHeaderInclude attribute.)
*/
void SendWithHeader (Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route);

/**
* \param packet packet to send down the stack
* \param ifid interface to be used for sending down packet
Expand Down
8 changes: 4 additions & 4 deletions src/internet/model/icmpv4-l4-protocol.cc
Expand Up @@ -56,14 +56,14 @@ Icmpv4L4Protocol::NotifyNewAggregate ()
Ptr<Node> node = this->GetObject<Node> ();
if (node != 0)
{
Ptr<Ipv4L3Protocol> ipv4 = this->GetObject<Ipv4L3Protocol> ();
Ptr<Ipv4> ipv4 = this->GetObject<Ipv4> ();
if (ipv4 != 0 && m_downTarget.IsNull ())
{
this->SetNode (node);
ipv4->Insert (this);
Ptr<Ipv4RawSocketFactoryImpl> rawFactory = CreateObject<Ipv4RawSocketFactoryImpl> ();
ipv4->AggregateObject (rawFactory);
this->SetDownTarget (MakeCallback (&Ipv4L3Protocol::Send, ipv4));
this->SetDownTarget (MakeCallback (&Ipv4::Send, ipv4));
}
}
}
Expand All @@ -84,7 +84,7 @@ Icmpv4L4Protocol::GetProtocolNumber (void) const
void
Icmpv4L4Protocol::SendMessage (Ptr<Packet> packet, Ipv4Address dest, uint8_t type, uint8_t code)
{
Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
NS_ASSERT (ipv4 != 0 && ipv4->GetRoutingProtocol () != 0);
Ipv4Header header;
header.SetDestination (dest);
Expand Down Expand Up @@ -179,7 +179,7 @@ Icmpv4L4Protocol::Forward (Ipv4Address source, Icmpv4Header icmp,
uint32_t info, Ipv4Header ipHeader,
const uint8_t payload[8])
{
Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
Ptr<IpL4Protocol> l4 = ipv4->GetProtocol (ipHeader.GetProtocol ());
if (l4 != 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/internet/model/ipv4-raw-socket-factory-impl.cc
Expand Up @@ -28,7 +28,7 @@ namespace ns3 {
Ptr<Socket>
Ipv4RawSocketFactoryImpl::CreateSocket (void)
{
Ptr<Ipv4L3Protocol> ipv4 = GetObject<Ipv4L3Protocol> ();
Ptr<Ipv4> ipv4 = GetObject<Ipv4> ();
Ptr<Socket> socket = ipv4->CreateRawSocket ();
return socket;
}
Expand Down
4 changes: 2 additions & 2 deletions src/internet/model/ipv4-raw-socket-impl.cc
Expand Up @@ -130,7 +130,7 @@ int
Ipv4RawSocketImpl::Close (void)
{
NS_LOG_FUNCTION (this);
Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
if (ipv4 != 0)
{
ipv4->DeleteRawSocket (this);
Expand Down Expand Up @@ -199,7 +199,7 @@ Ipv4RawSocketImpl::SendTo (Ptr<Packet> p, uint32_t flags,
return 0;
}
InetSocketAddress ad = InetSocketAddress::ConvertFrom (toAddress);
Ptr<Ipv4L3Protocol> ipv4 = m_node->GetObject<Ipv4L3Protocol> ();
Ptr<Ipv4> ipv4 = m_node->GetObject<Ipv4> ();
Ipv4Address dst = ad.GetIpv4 ();
Ipv4Address src = m_src;
if (ipv4->GetRoutingProtocol ())
Expand Down
35 changes: 35 additions & 0 deletions src/internet/model/ipv4.h
Expand Up @@ -143,6 +143,16 @@ class Ipv4 : public Object
virtual void Send (Ptr<Packet> packet, Ipv4Address source,
Ipv4Address destination, uint8_t protocol, Ptr<Ipv4Route> route) = 0;

/**
* \param packet packet to send
* \param ipHeader IP Header
* \param route route entry
*
* Higher-level layers call this method to send a packet with IPv4 Header
* (Intend to be used with IpHeaderInclude attribute.)
*/
virtual void SendWithHeader (Ptr<Packet> packet, Ipv4Header ipHeader, Ptr<Ipv4Route> route) = 0;

/**
* \param protocol a pointer to the protocol to add to this L4 Demux.
*
Expand Down Expand Up @@ -334,6 +344,31 @@ class Ipv4 : public Object
*/
virtual void SetForwarding (uint32_t interface, bool val) = 0;

/**
* \param protocolNumber number of protocol to lookup
* in this L4 Demux
* \returns a matching L4 Protocol
*
* This method is typically called by lower layers
* to forward packets up the stack to the right protocol.
*/
virtual Ptr<IpL4Protocol> GetProtocol (int protocolNumber) const = 0;

/**
* \brief Creates a raw socket
*
* \returns a smart pointer to the instantiated raw socket
*/
virtual Ptr<Socket> CreateRawSocket (void) = 0;

/**
* \brief Deletes a particular raw socket
*
* \param socket Smart pointer to the raw socket to be deleted
*/
virtual void DeleteRawSocket (Ptr<Socket> socket) = 0;


static const uint32_t IF_ANY = 0xffffffff;

private:
Expand Down

0 comments on commit db3b602

Please sign in to comment.