Add a NetDevice::SendFrom API, for sending packets with a custom source MAC address (a.k.a. MAC spoofing). Only implemented for CsmaNetDevice for now.
--- a/src/devices/csma/csma-net-device.cc Fri Jul 04 17:58:31 2008 +0100
+++ b/src/devices/csma/csma-net-device.cc Fri Jul 04 18:03:26 2008 +0100
@@ -173,6 +173,7 @@
void
CsmaNetDevice::AddHeader (
Ptr<Packet> p,
+ Mac48Address source,
Mac48Address dest,
uint16_t protocolNumber)
{
@@ -183,7 +184,6 @@
return;
}
- Mac48Address source = Mac48Address::ConvertFrom (GetAddress ());
EthernetHeader header (false);
header.SetSource (source);
header.SetDestination (dest);
@@ -492,17 +492,6 @@
NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ());
//
-// We never forward up packets that we sent. Real devices don't do this since
-// their receivers are disabled during send, so we don't. Drop the packet
-// silently (no tracing) since it would really never get here in a real device.
-//
- if (header.GetSource () == GetAddress ())
- {
- NS_LOG_LOGIC ("Ignoring packet sourced by this device");
- return;
- }
-
-//
// An IP host group address is mapped to an Ethernet multicast address
// by placing the low-order 23-bits of the IP address into the low-order
// 23 bits of the Ethernet multicast address 01-00-5E-00-00-00 (hex).
@@ -730,11 +719,19 @@
return false;
}
- bool
-CsmaNetDevice::Send(
- Ptr<Packet> packet,
- const Address& dest,
- uint16_t protocolNumber)
+bool
+CsmaNetDevice::Send (Ptr<Packet> packet,
+ const Address& dest,
+ uint16_t protocolNumber)
+{
+ return SendFrom (packet, m_address, dest, protocolNumber);
+}
+
+bool
+CsmaNetDevice::SendFrom (Ptr<Packet> packet,
+ const Address& src,
+ const Address& dest,
+ uint16_t protocolNumber)
{
NS_LOG_FUNCTION_NOARGS ();
NS_LOG_LOGIC ("p=" << packet);
@@ -751,7 +748,8 @@
}
Mac48Address destination = Mac48Address::ConvertFrom (dest);
- AddHeader (packet, destination, protocolNumber);
+ Mac48Address source = Mac48Address::ConvertFrom (src);
+ AddHeader (packet, source, destination, protocolNumber);
//
// Place the packet to be sent on the send queue
--- a/src/devices/csma/csma-net-device.h Fri Jul 04 17:58:31 2008 +0100
+++ b/src/devices/csma/csma-net-device.h Fri Jul 04 18:03:26 2008 +0100
@@ -251,6 +251,12 @@
uint16_t protocolNumber);
/**
+ * Start sending a packet down the channel, with MAC spoofing
+ */
+ virtual bool SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest,
+ uint16_t protocolNumber);
+
+ /**
* Get the node to which this device is attached.
*
* \returns Ptr to the Node to which the device is attached.
@@ -307,7 +313,7 @@
* \param protocolNumber In some protocols, identifies the type of
* payload contained in this packet.
*/
- void AddHeader (Ptr<Packet> p, Mac48Address dest, uint16_t protocolNumber);
+ void AddHeader (Ptr<Packet> p, Mac48Address source, Mac48Address dest, uint16_t protocolNumber);
/**
* Removes, from a packet of data, all headers and trailers that
--- a/src/devices/point-to-point/point-to-point-net-device.cc Fri Jul 04 17:58:31 2008 +0100
+++ b/src/devices/point-to-point/point-to-point-net-device.cc Fri Jul 04 18:03:26 2008 +0100
@@ -434,6 +434,15 @@
}
}
+bool
+PointToPointNetDevice::SendFrom (Ptr<Packet> packet,
+ const Address &source,
+ const Address &dest,
+ uint16_t protocolNumber)
+{
+ return Send (packet, dest, protocolNumber);
+}
+
Ptr<Node>
PointToPointNetDevice::GetNode (void) const
{
--- a/src/devices/point-to-point/point-to-point-net-device.h Fri Jul 04 17:58:31 2008 +0100
+++ b/src/devices/point-to-point/point-to-point-net-device.h Fri Jul 04 18:03:26 2008 +0100
@@ -167,8 +167,8 @@
virtual bool IsPointToPoint (void) const;
- virtual bool Send(Ptr<Packet> packet, const Address &dest,
- uint16_t protocolNumber);
+ virtual bool Send(Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber);
+ virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
virtual Ptr<Node> GetNode (void) const;
virtual void SetNode (Ptr<Node> node);
--- a/src/devices/wifi/wifi-net-device.cc Fri Jul 04 17:58:31 2008 +0100
+++ b/src/devices/wifi/wifi-net-device.cc Fri Jul 04 18:03:26 2008 +0100
@@ -283,6 +283,12 @@
m_mac->Enqueue (packet, realTo);
return true;
}
+bool
+WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
+{
+ NS_FATAL_ERROR ("WifiNetDevice::SendFrom not implemented.");
+ return false;
+}
Ptr<Node>
WifiNetDevice::GetNode (void) const
{
--- a/src/devices/wifi/wifi-net-device.h Fri Jul 04 17:58:31 2008 +0100
+++ b/src/devices/wifi/wifi-net-device.h Fri Jul 04 18:03:26 2008 +0100
@@ -96,6 +96,7 @@
virtual Address MakeMulticastAddress (Ipv4Address multicastGroup) const;
virtual bool IsPointToPoint (void) const;
virtual bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
+ virtual bool SendFrom (Ptr<Packet> packet, const Address& src, const Address& dest, uint16_t protocolNumber);
virtual Ptr<Node> GetNode (void) const;
virtual void SetNode (Ptr<Node> node);
virtual bool NeedsArp (void) const;
--- a/src/node/net-device.h Fri Jul 04 17:58:31 2008 +0100
+++ b/src/node/net-device.h Fri Jul 04 18:03:26 2008 +0100
@@ -216,6 +216,20 @@
*/
virtual bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber) = 0;
/**
+ * \param packet packet sent from above down to Network Device
+ * \param source source mac address (so called "MAC spoofing")
+ * \param dest mac address of the destination (already resolved)
+ * \param protocolNumber identifies the type of payload contained in
+ * this packet. Used to call the right L3Protocol when the packet
+ * is received.
+ *
+ * Called from higher layer to send packet into Network Device
+ * with the specified source and destination Addresses.
+ *
+ * \return whether the Send operation succeeded
+ */
+ virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber) = 0;
+ /**
* \returns the node base class which contains this network
* interface.
*
--- a/src/node/simple-net-device.cc Fri Jul 04 17:58:31 2008 +0100
+++ b/src/node/simple-net-device.cc Fri Jul 04 18:03:26 2008 +0100
@@ -151,6 +151,15 @@
m_channel->Send (packet, protocolNumber, to, m_address, this);
return true;
}
+bool
+SimpleNetDevice::SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
+{
+ Mac48Address to = Mac48Address::ConvertFrom (dest);
+ Mac48Address from = Mac48Address::ConvertFrom (source);
+ m_channel->Send (packet, protocolNumber, to, from, this);
+ return true;
+}
+
Ptr<Node>
SimpleNetDevice::GetNode (void) const
{
--- a/src/node/simple-net-device.h Fri Jul 04 17:58:31 2008 +0100
+++ b/src/node/simple-net-device.h Fri Jul 04 18:03:26 2008 +0100
@@ -63,6 +63,7 @@
virtual Address MakeMulticastAddress (Ipv4Address multicastGroup) const;
virtual bool IsPointToPoint (void) const;
virtual bool Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber);
+ virtual bool SendFrom(Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber);
virtual Ptr<Node> GetNode (void) const;
virtual void SetNode (Ptr<Node> node);
virtual bool NeedsArp (void) const;