Bug 1319 - Fix Ipv6RawSocketImpl Icmpv6 filter

This commit is contained in:
Tommaso Pecorella
2012-01-25 19:13:11 +01:00
parent 251ddfc332
commit 43de6789ed
2 changed files with 85 additions and 7 deletions

View File

@@ -51,10 +51,6 @@ TypeId Ipv6RawSocketImpl::GetTypeId ()
UintegerValue (0),
MakeUintegerAccessor (&Ipv6RawSocketImpl::m_protocol),
MakeUintegerChecker<uint16_t> ())
.AddAttribute ("IcmpFilter", "Any ICMPv6 header whose type field matches a bit in this filter is dropped.",
UintegerValue (0),
MakeUintegerAccessor (&Ipv6RawSocketImpl::m_icmpFilter),
MakeUintegerChecker<uint32_t> ())
;
return tid;
}
@@ -69,6 +65,7 @@ Ipv6RawSocketImpl::Ipv6RawSocketImpl ()
m_protocol = 0;
m_shutdownSend = false;
m_shutdownRecv = false;
Icmpv6FilterSetPassAll();
}
Ipv6RawSocketImpl::~Ipv6RawSocketImpl ()
@@ -328,7 +325,7 @@ bool Ipv6RawSocketImpl::ForwardUp (Ptr<const Packet> p, Ipv6Header hdr, Ptr<NetD
copy->PeekHeader (icmpHeader);
uint8_t type = icmpHeader.GetType ();
if ((1 << type) & m_icmpFilter)
if (Icmpv6FilterWillBlock(type))
{
/* packet filtered */
return false;
@@ -372,5 +369,41 @@ Ipv6RawSocketImpl::GetAllowBroadcast () const
return true;
}
void
Ipv6RawSocketImpl::Icmpv6FilterSetPassAll()
{
memset(&m_icmpFilter, 0xff, sizeof(icmpv6Filter));
}
void
Ipv6RawSocketImpl::Icmpv6FilterSetBlockAll()
{
memset(&m_icmpFilter, 0x00, sizeof(icmpv6Filter));
}
void
Ipv6RawSocketImpl::Icmpv6FilterSetPass(uint8_t type)
{
(m_icmpFilter.icmpv6Filt[(type) >> 5]) |= (uint32_t(1) << ((type) & 31));
}
void
Ipv6RawSocketImpl::Icmpv6FilterSetBlock(uint8_t type)
{
(m_icmpFilter.icmpv6Filt[(type) >> 5]) &= ~(uint32_t(1) << ((type) & 31));
}
bool
Ipv6RawSocketImpl::Icmpv6FilterWillPass(uint8_t type)
{
return (((m_icmpFilter.icmpv6Filt[(type) >> 5]) & (uint32_t(1) << ((type) & 31))) != 0);
}
bool
Ipv6RawSocketImpl::Icmpv6FilterWillBlock(uint8_t type)
{
return (((m_icmpFilter.icmpv6Filt[(type) >> 5]) & (uint32_t(1) << ((type) & 31))) == 0);
}
} /* namespace ns3 */

View File

@@ -219,10 +219,47 @@ public:
virtual bool SetAllowBroadcast (bool allowBroadcast);
virtual bool GetAllowBroadcast () const;
/**
* \brief Clean the ICMPv6 filter structure
*/
void Icmpv6FilterSetPassAll();
/**
* \brief Set the filter to block all the ICMPv6 types
*/
void Icmpv6FilterSetBlockAll();
/**
* \brief Set the filter to pass one ICMPv6 type
* \param the ICMPv6 type to pass
*/
void Icmpv6FilterSetPass(uint8_t type);
/**
* \brief Set the filter to block one ICMPv6 type
* \param the ICMPv6 type to block
*/
void Icmpv6FilterSetBlock(uint8_t type);
/**
* \brief Ask the filter about the status of one ICMPv6 type
* \param the ICMPv6 type
* \return true if the ICMP type is passing through
*/
bool Icmpv6FilterWillPass(uint8_t type);
/**
* \brief Ask the filter about the status of one ICMPv6 type
* \param the ICMPv6 type
* \return true if the ICMP type is being blocked
*/
bool Icmpv6FilterWillBlock(uint8_t type);
private:
/**
* \struct Data
* \brief IPv6 raw data and additionnal information.
* \brief IPv6 raw data and additional information.
*/
struct Data
{
@@ -276,10 +313,18 @@ private:
*/
bool m_shutdownRecv;
/**
* \brief Struct to hold the ICMPv6 filter
*/
typedef struct
{
uint32_t icmpv6Filt[8];
} icmpv6Filter;
/**
* \brief ICMPv6 filter.
*/
uint32_t m_icmpFilter;
icmpv6Filter m_icmpFilter;
};
} /* namespace ns3 */