23#include "ns3/boolean.h"
24#include "ns3/channel.h"
26#include "ns3/ethernet-header.h"
27#include "ns3/fd-reader.h"
29#include "ns3/llc-snap-header.h"
32#include "ns3/packet.h"
33#include "ns3/realtime-simulator-impl.h"
34#include "ns3/simulator.h"
35#include "ns3/string.h"
36#include "ns3/uinteger.h"
43#include <sys/socket.h>
60 uint8_t* buf = (uint8_t*)std::malloc(bufferSize);
64 ssize_t len = read(
m_fd, buf, bufferSize);
76#define TAP_MAGIC 95549
86 .SetGroupName(
"TapBridge")
89 "The MAC-level Maximum Transmission Unit",
92 MakeUintegerChecker<uint16_t>())
93 .AddAttribute(
"DeviceName",
94 "The name of the tap device to create.",
98 .AddAttribute(
"Gateway",
99 "The IP address of the default gateway to assign to the host machine, "
100 "when in ConfigureLocal mode.",
103 MakeIpv4AddressChecker())
106 "The IP address to assign to the tap device, when in ConfigureLocal mode. "
107 "This address will override the discovered IP address of the simulated device.",
110 MakeIpv4AddressChecker())
113 "The MAC address to assign to the tap device, when in ConfigureLocal mode. "
114 "This address will override the discovered MAC address of the simulated device.",
117 MakeMac48AddressChecker())
120 "The network mask to assign to the tap device, when in ConfigureLocal mode. "
121 "This address will override the discovered MAC address of the simulated device.",
124 MakeIpv4MaskChecker())
125 .AddAttribute(
"Start",
126 "The simulation time at which to spin up the tap device read thread.",
130 .AddAttribute(
"Stop",
131 "The simulation time at which to tear down the tap device read thread.",
135 .AddAttribute(
"Mode",
136 "The operating and configuration mode to use.",
145 .AddAttribute(
"Verbose",
146 "Enable verbose output from tap-creator child process",
160 m_ns3AddressRewritten(false)
324 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
327 "TapBridge::CreateTap(): Unix socket creation error, errno = " << std::strerror(errno));
332 struct sockaddr_un un;
333 memset(&un, 0,
sizeof(un));
334 un.sun_family = AF_UNIX;
335 int status = bind(sock, (
struct sockaddr*)&un,
sizeof(sa_family_t));
337 "TapBridge::CreateTap(): Could not bind(): errno = " << std::strerror(errno));
348 socklen_t len =
sizeof(un);
349 status = getsockname(sock, (
struct sockaddr*)&un, &len);
352 "TapBridge::CreateTap(): Could not getsockname(): errno = " << std::strerror(errno));
358 NS_LOG_INFO(
"Encoded Unix socket as \"" << path <<
"\"");
376 pid_t pid = ::fork();
413 NS_FATAL_ERROR(
"TapBridge::CreateTap(): Tap device IP configuration requested but "
414 "neither IP address nor IP netmask is provided");
419 Ipv4Mask ipv4Mask(
"255.255.255.255");
423 uint32_t index = ipv4->GetInterfaceForDevice(nd);
424 if (ipv4->GetNAddresses(index) > 1)
427 "Underlying bridged NetDevice has multiple IP addresses; using first one.");
429 ipv4Address = ipv4->GetAddress(index, 0).GetLocal();
434 ipv4Mask = ipv4->GetAddress(index, 0).GetMask();
441 Address address = nd->GetAddress();
450 std::ostringstream ossDeviceName;
457 std::ostringstream ossGateway;
465 std::ostringstream ossIp;
468 ossIp <<
"-i" << ipv4Address;
475 std::ostringstream ossMac;
478 ossMac <<
"-m" << mac48Address;
485 std::ostringstream ossNetmask;
488 ossNetmask <<
"-n" << ipv4Mask;
495 std::ostringstream ossMode;
510 std::ostringstream ossVerbose;
516 std::ostringstream ossPath;
517 ossPath <<
"-p" << path;
519 NS_LOG_DEBUG(
"Executing: " << TAP_CREATOR <<
" " << ossDeviceName.str() <<
" "
520 << ossGateway.str() <<
" " << ossIp.str() <<
" " << ossMac.str()
521 <<
" " << ossNetmask.str() <<
" " << ossMode.str() <<
" "
522 << ossPath.str() <<
" " << ossVerbose.str());
527 status = ::execlp(TAP_CREATOR,
529 ossDeviceName.str().c_str(),
530 ossGateway.str().c_str(),
532 ossMac.str().c_str(),
533 ossNetmask.str().c_str(),
534 ossMode.str().c_str(),
535 ossPath.str().c_str(),
536 ossVerbose.str().c_str(),
543 NS_FATAL_ERROR(
"TapBridge::CreateTap(): Back from execlp(), status = "
544 << status <<
" errno = " << ::strerror(errno));
554 pid_t waited = waitpid(pid, &st, 0);
557 "TapBridge::CreateTap(): waitpid() fails, errno = " << std::strerror(errno));
558 NS_ASSERT_MSG(pid == waited,
"TapBridge::CreateTap(): pid mismatch");
567 int exitStatus = WEXITSTATUS(st);
569 "TapBridge::CreateTap(): socket creator exited normally with status "
572 else if (WIFSIGNALED(st))
574 NS_FATAL_ERROR(
"TapBridge::CreateTap(): socket creator exited with signal "
579 NS_FATAL_ERROR(
"TapBridge::CreateTap(): socket creator exited abnormally");
597 iov.iov_base = &magic;
598 iov.iov_len =
sizeof(magic);
611 size_t msg_size =
sizeof(int);
612 char control[CMSG_SPACE(msg_size)];
625 msg.msg_name =
nullptr;
629 msg.msg_control = control;
630 msg.msg_controllen =
sizeof(control);
637 ssize_t bytesRead = recvmsg(sock, &msg, 0);
639 "TapBridge::CreateTap(): Wrong byte count from socket creator");
646 struct cmsghdr* cmsg;
647 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg !=
nullptr; cmsg = CMSG_NXTHDR(&msg, cmsg))
649 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
658 NS_LOG_INFO(
"Got SCM_RIGHTS with correct magic " << magic);
659 int* rawSocket = (
int*)CMSG_DATA(cmsg);
660 NS_LOG_INFO(
"Got the socket from the socket creator = " << *rawSocket);
666 NS_LOG_INFO(
"Got SCM_RIGHTS, but with bad magic " << magic);
672 NS_FATAL_ERROR(
"Did not get the raw socket from the socket creator");
682 memset(&s, 0,
sizeof(
struct ifreq));
686 int ioctlResult = ioctl(sock, SIOCGIFHWADDR, &s);
687 if (ioctlResult == 0)
690 learnedMac.
CopyFrom((uint8_t*)s.ifr_hwaddr.sa_data);
692 << learnedMac <<
": setting ns-3 device to use this address");
701 <<
" while in USE_LOCAL/USE_BRIDGE mode: " << std::strerror(errno));
702 NS_LOG_INFO(
"Underlying ns-3 device will continue to use default address, what can "
703 "lead to connectivity errors");
732 NS_LOG_INFO(
"TapBridge::ReadCallback(): Scheduling handler");
770 Ptr<Packet> packet = Create<Packet>(
reinterpret_cast<const uint8_t*
>(buf), len);
790 "TapBridge::ForwardToBridgedDevice: Discarding packet as unfit for ns-3 consumption");
803 "TapBridge::ForwardToBridgedDevice: Source addr is broadcast");
812 <<
": setting ns-3 device to use this address");
821 NS_LOG_LOGIC(
"Forwarding packet to ns-3 device via Send()");
864 "TapBridge::ForwardToBridgedDevice(): Internal error");
888 uint32_t headerSize = p->PeekHeader(header);
889 p->RemoveAtStart(headerSize);
913 p->RemoveHeader(llc);
944 NS_ASSERT_MSG(bridgedDevice !=
this,
"TapBridge::SetBridgedDevice: Cannot bridge to self");
949 NS_FATAL_ERROR(
"TapBridge::SetBridgedDevice: Device does not support eui 48 addresses: "
950 "cannot be added to bridge.");
955 NS_FATAL_ERROR(
"TapBridge::SetBridgedDevice: Device does not support SendFrom: cannot be "
969 bridgedDevice->SetPromiscReceiveCallback(
981 NS_LOG_LOGIC(
"Discarding packet stolen from bridged device " << device);
993 NS_LOG_FUNCTION(
this << device << packet << protocol << src << dst << packetType);
995 "TapBridge::SetBridgedDevice: Received packet from unexpected device");
1039 p->AddHeader(header);
1048 "TapBridge::ReceiveFromBridgedDevice: Packet too big " << p->GetSize());
1053 "TapBridge::ReceiveFromBridgedDevice(): Write error.");
1201 NS_FATAL_ERROR(
"TapBridge::Send: You may not call Send on a TapBridge directly");
1209 NS_FATAL_ERROR(
"TapBridge::Send: You may not call SendFrom on a TapBridge directly");
a polymophic address class
AttributeValue implementation for Boolean.
Hold variables of type enum.
int m_fd
The file descriptor to read from.
Ipv4 addresses are stored in host order in this class.
AttributeValue implementation for Ipv4Address.
Access to the IPv4 forwarding table, interfaces, and configuration.
a class to represent an Ipv4 address mask
static Ipv4Mask GetOnes()
AttributeValue implementation for Ipv4Mask.
Describes an IPv6 address.
static Mac48Address GetMulticast(Ipv4Address address)
static bool IsMatchingType(const Address &address)
void CopyFrom(const uint8_t buffer[6])
static Mac48Address ConvertFrom(const Address &address)
AttributeValue implementation for Mac48Address.
Network layer to device interface.
PacketType
Packet types are used as they are in Linux.
@ PACKET_OTHERHOST
Packet addressed to someone else.
virtual void DoDispose()
Destructor implementation.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Hold variables of type string.
FdReader::Data DoRead() override
The read implementation.
A bridge to make it appear that a real host process is connected to an ns-3 net device.
void SetBridgedNetDevice(Ptr< NetDevice > bridgedDevice)
Set the ns-3 net device to bridge.
bool ReceiveFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &src, const Address &dst, PacketType packetType)
Receives a packet from a bridged Device.
void CreateTap()
Call out to a separate process running as suid root in order to get our tap device created.
void SetIfIndex(const uint32_t index) override
uint32_t m_nodeId
a copy of the node id so the read thread doesn't have to GetNode() in in order to find the node ID.
static TypeId GetTypeId()
Get the type ID.
bool m_ns3AddressRewritten
Whether the MAC address of the underlying ns-3 device has already been rewritten is stored in this va...
void AddLinkChangeCallback(Callback< void > callback) override
uint8_t * m_packetBuffer
A 64K buffer to hold packet data while it is being sent.
bool m_linkUp
Flag indicating whether or not the link is up.
Address GetAddress() const override
TracedCallback m_linkChangeCallbacks
Callbacks to fire if the link changes state (up or down).
void SetMode(TapBridge::Mode mode)
Set the operating mode of this device.
NetDevice::PromiscReceiveCallback m_promiscRxCallback
Callback used to hook the promiscuous packet receive callback of the TapBridge ns-3 net device.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
int m_sock
The socket (actually interpreted as fd) to use to talk to the Tap device on the real internet host.
void StopTapDevice()
Tear down the device.
Address GetBroadcast() const override
uint32_t m_ifIndex
The ns-3 interface index of this TapBridge net device.
bool DiscardFromBridgedDevice(Ptr< NetDevice > device, Ptr< const Packet > packet, uint16_t protocol, const Address &src)
Receives a packet from a bridged Device.
bool NeedsArp() const override
void SetPromiscReceiveCallback(NetDevice::PromiscReceiveCallback cb) override
NetDevice::ReceiveCallback m_rxCallback
Callback used to hook the standard packet receive callback of the TapBridge ns-3 net device.
Mac48Address m_address
The (unused) MAC address of the TapBridge net device.
void Start(Time tStart)
Set a start time for the device.
bool SetMtu(const uint16_t mtu) override
bool IsLinkUp() const override
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
Ptr< TapBridgeFdReader > m_fdReader
Includes the ns-3 read thread used to do blocking reads on the fd corresponding to the host device.
uint16_t m_mtu
The common mtu to use for the net devices.
void ReadCallback(uint8_t *buf, ssize_t len)
Callback to process packets that are read.
Ipv4Address m_tapIp
The IP address to use as the device IP on the host.
Ipv4Mask m_tapNetmask
The network mask to assign to the device created on the host.
bool m_verbose
Flag indicating whether or not the link is up.
bool SupportsSendFrom() const override
void StartTapDevice()
Spin up the device.
Ptr< Packet > Filter(Ptr< Packet > packet, Address *src, Address *dst, uint16_t *type)
The host we are bridged to is in the evil real world.
Mode m_mode
The operating mode of the bridge.
Ptr< Node > m_node
Pointer to the (ghost) Node to which we are connected.
EventId m_stopEvent
The ID of the ns-3 event used to schedule the tear down of the underlying host Tap device and ns-3 re...
void ForwardToBridgedDevice(uint8_t *buf, ssize_t len)
Forward a packet received from the tap device to the bridged ns-3 device.
void Stop(Time tStop)
Set a stop time for the device.
void NotifyLinkUp()
Notifies that the link is up and ready.
Ptr< NetDevice > m_bridgedDevice
The ns-3 net device to which we are bridging.
bool IsBroadcast() const override
Mode
Enumeration of the operating modes supported in the class.
@ USE_BRIDGE
ns-3 uses a pre-created tap, and bridges to a bridging net device
@ USE_LOCAL
ns-3 uses a pre-created tap, without configuring it
@ CONFIGURE_LOCAL
ns-3 creates and configures tap device
void SetAddress(Address address) override
Set the address of this interface.
Ptr< Node > GetNode() const override
EventId m_startEvent
The ID of the ns-3 event used to schedule the start up of the underlying host Tap device and ns-3 rea...
bool IsMulticast() const override
Time m_tStart
Time to start spinning up the device.
Ipv4Address m_tapGateway
The IP address to use as the device default gateway on the host.
TapBridge::Mode GetMode()
Get the operating mode of this device.
Ptr< NetDevice > GetBridgedNetDevice()
Get the bridged net device.
Time m_tStop
Time to start tearing down the device.
void SetNode(Ptr< Node > node) override
std::string m_tapDeviceName
The name of the device to create on the host.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
void DoDispose() override
Call out to a separate process running as suid root in order to get our tap device created.
uint16_t GetMtu() const override
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Ptr< Channel > GetChannel() const override
uint32_t GetIfIndex() const override
Mac48Address m_tapMac
The MAC address to use as the hardware address on the host; only used in UseLocal mode.
Simulation virtual time values and global simulation resolution.
AttributeValue implementation for Time.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Hold an unsigned integer type.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Ptr< const AttributeChecker > MakeBooleanChecker()
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Ptr< const AttributeChecker > MakeStringChecker()
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
EventImpl * MakeEvent(void(*f)())
Make an EventImpl from a function pointer taking varying numbers of arguments.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Time Seconds(double value)
Construct a Time in the indicated unit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
std::string TapBufferToString(uint8_t *buffer, uint32_t len)
Convert a byte buffer to a string containing a hex representation of the buffer.
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
A structure representing data read.
uint32_t pktSize
packet size used for the simulation (in bytes)