2020-08-28 14:18:02 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SCSI Target Emulator RaSCSI (*^..^*)
|
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Powered by XM6 TypeG Technology.
|
|
|
|
// Copyright (C) 2016-2020 GIMONS
|
2021-02-07 19:00:48 +00:00
|
|
|
// Copyright (C) akuker
|
2020-08-28 14:18:02 +00:00
|
|
|
//
|
2021-02-07 19:00:48 +00:00
|
|
|
// Imported NetBSD support and some optimisation patches by Rin Okuyama.
|
2020-08-28 14:18:02 +00:00
|
|
|
//
|
|
|
|
// [ TAP Driver ]
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2021-02-07 19:00:48 +00:00
|
|
|
#include <zlib.h> // For crc32()
|
2020-08-28 14:18:02 +00:00
|
|
|
#include "os.h"
|
|
|
|
#include "xm6.h"
|
|
|
|
#include "ctapdriver.h"
|
2021-02-07 19:00:48 +00:00
|
|
|
#include "log.h"
|
2020-08-28 14:18:02 +00:00
|
|
|
|
2021-02-07 22:46:59 +00:00
|
|
|
const char rascsi_bridge_string[] = "rascsi_bridge";
|
|
|
|
|
2020-08-28 14:18:02 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2020-12-28 02:13:21 +00:00
|
|
|
// Constructor
|
2020-08-28 14:18:02 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
CTapDriver::CTapDriver()
|
|
|
|
{
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("%s",__PRETTY_FUNCTION__);
|
2020-12-28 02:13:21 +00:00
|
|
|
// Initialization
|
2020-08-28 14:18:02 +00:00
|
|
|
m_hTAP = -1;
|
|
|
|
memset(&m_MacAddr, 0, sizeof(m_MacAddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2020-12-28 02:13:21 +00:00
|
|
|
// Initialization
|
2020-08-28 14:18:02 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
#ifdef __linux__
|
|
|
|
BOOL FASTCALL CTapDriver::Init()
|
|
|
|
{
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("%s",__PRETTY_FUNCTION__);
|
|
|
|
|
2020-08-28 14:18:02 +00:00
|
|
|
char dev[IFNAMSIZ] = "ras0";
|
2021-02-07 22:46:59 +00:00
|
|
|
char cmd_output[256] = "";
|
2020-08-28 14:18:02 +00:00
|
|
|
struct ifreq ifr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ASSERT(this);
|
|
|
|
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("Opening Tap device");
|
2020-12-28 02:13:21 +00:00
|
|
|
// TAP device initilization
|
2020-08-28 14:18:02 +00:00
|
|
|
if ((m_hTAP = open("/dev/net/tun", O_RDWR)) < 0) {
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGERROR("Error: can't open tun. Errno: %d %s", errno, strerror(errno));
|
2020-08-28 14:18:02 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("Opened tap device %d",m_hTAP);
|
2020-08-28 14:18:02 +00:00
|
|
|
|
2021-02-07 19:00:48 +00:00
|
|
|
|
2020-08-28 14:18:02 +00:00
|
|
|
// IFF_NO_PI for no extra packet information
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
|
|
|
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("Going to open %s", ifr.ifr_name);
|
2020-08-28 14:18:02 +00:00
|
|
|
if ((ret = ioctl(m_hTAP, TUNSETIFF, (void *)&ifr)) < 0) {
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGERROR("Error: can't ioctl TUNSETIFF. Errno: %d %s", errno, strerror(errno));
|
2020-08-28 14:18:02 +00:00
|
|
|
close(m_hTAP);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("return code from ioctl was %d", ret);
|
2020-08-28 14:18:02 +00:00
|
|
|
|
2021-02-07 22:46:59 +00:00
|
|
|
LOGTRACE("Going to see if the bridge is created");
|
|
|
|
|
|
|
|
ret = run_system_cmd_with_output("brctl show | grep rascsi_bridge", cmd_output, sizeof(cmd_output));
|
|
|
|
if(ret != EXIT_SUCCESS){
|
|
|
|
LOGTRACE("Unable to run brctl show command");
|
|
|
|
}
|
|
|
|
LOGTRACE("%s brctl show returned %s", __PRETTY_FUNCTION__, cmd_output);
|
|
|
|
|
|
|
|
// Check if the bridge is already created
|
|
|
|
if(strncmp(rascsi_bridge_string, cmd_output, strlen(rascsi_bridge_string)) != 0){
|
|
|
|
LOGINFO("Creating the rascsi_bridge...");
|
|
|
|
LOGDEBUG("brctl addbr rascsi_bridge");
|
|
|
|
ret = run_system_cmd("brctl addbr rascsi_bridge");
|
|
|
|
LOGDEBUG("brctl addif rascsi_bridge eth0");
|
|
|
|
ret = run_system_cmd("brctl addif rascsi_bridge eth0");
|
|
|
|
LOGDEBUG("ip link set dev rascsi_bridge up");
|
|
|
|
ret = run_system_cmd("ip link set dev rascsi_bridge up");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGINFO("Note: rascsi_bridge already created");
|
|
|
|
}
|
|
|
|
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGDEBUG("ip link set ras0 up");
|
|
|
|
ret = run_system_cmd("ip link set ras0 up");
|
|
|
|
LOGTRACE("return code from ip link set ras0 up was %d", ret);
|
|
|
|
|
|
|
|
LOGDEBUG("brctl addif rascsi_bridge ras0");
|
|
|
|
ret = run_system_cmd("brctl addif rascsi_bridge ras0");
|
|
|
|
LOGTRACE("return code from brctl addif rascsi_bridge ras0 was %d", ret);
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Get MAC address
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("Getting the MAC address");
|
2020-08-28 14:18:02 +00:00
|
|
|
ifr.ifr_addr.sa_family = AF_INET;
|
|
|
|
if ((ret = ioctl(m_hTAP, SIOCGIFHWADDR, &ifr)) < 0) {
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGERROR("Error: can't ioctl SIOCGIFHWADDR. Errno: %d %s", errno, strerror(errno));
|
2020-08-28 14:18:02 +00:00
|
|
|
close(m_hTAP);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("got the mac");
|
2020-08-28 14:18:02 +00:00
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Save MAC address
|
2020-08-28 14:18:02 +00:00
|
|
|
memcpy(m_MacAddr, ifr.ifr_hwaddr.sa_data, sizeof(m_MacAddr));
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGINFO("Tap device %s created", ifr.ifr_name);
|
2020-08-28 14:18:02 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif // __linux__
|
|
|
|
|
|
|
|
#ifdef __NetBSD__
|
|
|
|
BOOL FASTCALL CTapDriver::Init()
|
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
struct ifaddrs *ifa, *a;
|
|
|
|
|
|
|
|
ASSERT(this);
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// TAP Device Initialization
|
2020-08-28 14:18:02 +00:00
|
|
|
if ((m_hTAP = open("/dev/tap", O_RDWR)) < 0) {
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGERROR("Error: can't open tap. Errno: %d %s", errno, strerror(errno));
|
2020-08-28 14:18:02 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Get device name
|
2020-08-28 14:18:02 +00:00
|
|
|
if (ioctl(m_hTAP, TAPGIFNAME, (void *)&ifr) < 0) {
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGERROR("Error: can't ioctl TAPGIFNAME. Errno: %d %s", errno, strerror(errno));
|
2020-08-28 14:18:02 +00:00
|
|
|
close(m_hTAP);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Get MAC address
|
2020-08-28 14:18:02 +00:00
|
|
|
if (getifaddrs(&ifa) == -1) {
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGERROR("Error: can't getifaddrs. Errno: %d %s", errno, strerror(errno));
|
2020-08-28 14:18:02 +00:00
|
|
|
close(m_hTAP);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
for (a = ifa; a != NULL; a = a->ifa_next)
|
|
|
|
if (strcmp(ifr.ifr_name, a->ifa_name) == 0 &&
|
|
|
|
a->ifa_addr->sa_family == AF_LINK)
|
|
|
|
break;
|
|
|
|
if (a == NULL) {
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGERROR("Error: can't get MAC addressErrno: %d %s", errno, strerror(errno));
|
2020-08-28 14:18:02 +00:00
|
|
|
close(m_hTAP);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Save MAC address
|
2020-08-28 14:18:02 +00:00
|
|
|
memcpy(m_MacAddr, LLADDR((struct sockaddr_dl *)a->ifa_addr),
|
|
|
|
sizeof(m_MacAddr));
|
|
|
|
freeifaddrs(ifa);
|
|
|
|
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGINFO("Tap device : %s\n", ifr.ifr_name);
|
2020-08-28 14:18:02 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif // __NetBSD__
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2020-12-28 02:13:21 +00:00
|
|
|
// Cleanup
|
2020-08-28 14:18:02 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
void FASTCALL CTapDriver::Cleanup()
|
|
|
|
{
|
|
|
|
ASSERT(this);
|
2021-02-07 19:00:48 +00:00
|
|
|
int result;
|
2021-01-27 21:16:52 +00:00
|
|
|
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGDEBUG("brctl delif rascsi_bridge ras0");
|
|
|
|
result = run_system_cmd("brctl delif rascsi_bridge ras0");
|
|
|
|
if(result != EXIT_SUCCESS){
|
|
|
|
LOGWARN("Warning: The brctl delif command failed.");
|
|
|
|
LOGWARN("You may need to manually remove the ras0 tap device from the bridge");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release TAP defice
|
2020-08-28 14:18:02 +00:00
|
|
|
if (m_hTAP != -1) {
|
|
|
|
close(m_hTAP);
|
|
|
|
m_hTAP = -1;
|
|
|
|
}
|
2021-01-27 21:16:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 19:00:48 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Enable
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
BOOL FASTCALL CTapDriver::Enable(){
|
|
|
|
int result;
|
|
|
|
LOGDEBUG("%s: ip link set ras0 up", __PRETTY_FUNCTION__);
|
|
|
|
result = run_system_cmd("ip link set ras0 up");
|
|
|
|
return (result == EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Disable
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
BOOL FASTCALL CTapDriver::Disable(){
|
|
|
|
int result;
|
|
|
|
LOGDEBUG("%s: ip link set ras0 down", __PRETTY_FUNCTION__);
|
|
|
|
result = run_system_cmd("ip link set ras0 down");
|
|
|
|
return (result == EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Flush
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
BOOL FASTCALL CTapDriver::Flush(){
|
|
|
|
LOGTRACE("%s", __PRETTY_FUNCTION__);
|
|
|
|
while(PendingPackets()){
|
|
|
|
(void)Rx(m_garbage_buffer);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-08-28 14:18:02 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2020-12-28 02:13:21 +00:00
|
|
|
// MGet MAC Address
|
2020-08-28 14:18:02 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
void FASTCALL CTapDriver::GetMacAddr(BYTE *mac)
|
|
|
|
{
|
|
|
|
ASSERT(this);
|
|
|
|
ASSERT(mac);
|
|
|
|
|
|
|
|
memcpy(mac, m_MacAddr, sizeof(m_MacAddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2020-12-28 02:13:21 +00:00
|
|
|
// Receive
|
2020-08-28 14:18:02 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
2021-02-07 19:00:48 +00:00
|
|
|
BOOL FASTCALL CTapDriver::PendingPackets()
|
2020-08-28 14:18:02 +00:00
|
|
|
{
|
|
|
|
struct pollfd fds;
|
|
|
|
|
|
|
|
ASSERT(this);
|
|
|
|
ASSERT(m_hTAP != -1);
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Check if there is data that can be received
|
2020-08-28 14:18:02 +00:00
|
|
|
fds.fd = m_hTAP;
|
|
|
|
fds.events = POLLIN | POLLERR;
|
|
|
|
fds.revents = 0;
|
|
|
|
poll(&fds, 1, 0);
|
2021-02-07 19:00:48 +00:00
|
|
|
LOGTRACE("%s %u revents", __PRETTY_FUNCTION__, fds.revents);
|
2020-08-28 14:18:02 +00:00
|
|
|
if (!(fds.revents & POLLIN)) {
|
2021-02-07 19:00:48 +00:00
|
|
|
return FALSE;
|
|
|
|
}else {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Receive
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
int FASTCALL CTapDriver::Rx(BYTE *buf)
|
|
|
|
{
|
|
|
|
DWORD dwReceived;
|
|
|
|
DWORD crc;
|
|
|
|
|
|
|
|
ASSERT(this);
|
|
|
|
ASSERT(m_hTAP != -1);
|
|
|
|
|
|
|
|
// Check if there is data that can be received
|
|
|
|
if(!PendingPackets()){
|
2020-08-28 14:18:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Receive
|
2020-08-28 14:18:02 +00:00
|
|
|
dwReceived = read(m_hTAP, buf, ETH_FRAME_LEN);
|
|
|
|
if (dwReceived == (DWORD)-1) {
|
2021-02-26 17:40:16 +00:00
|
|
|
LOGWARN("%s Error occured while receiving an packet", __PRETTY_FUNCTION__);
|
2020-08-28 14:18:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// If reception is enabled
|
2020-08-28 14:18:02 +00:00
|
|
|
if (dwReceived > 0) {
|
2021-02-07 19:00:48 +00:00
|
|
|
// We need to add the Frame Check Status (FCS) CRC back onto the end of the packet.
|
|
|
|
// The Linux network subsystem removes it, since most software apps shouldn't ever
|
|
|
|
// need it.
|
|
|
|
|
|
|
|
// Initialize the CRC
|
|
|
|
crc = crc32(0L, Z_NULL, 0);
|
|
|
|
// Calculate the CRC
|
|
|
|
crc = crc32(crc, buf, dwReceived);
|
|
|
|
|
|
|
|
buf[dwReceived + 0] = (BYTE)((crc >> 0) & 0xFF);
|
|
|
|
buf[dwReceived + 1] = (BYTE)((crc >> 8) & 0xFF);
|
|
|
|
buf[dwReceived + 2] = (BYTE)((crc >> 16) & 0xFF);
|
|
|
|
buf[dwReceived + 3] = (BYTE)((crc >> 24) & 0xFF);
|
|
|
|
|
|
|
|
LOGDEBUG("%s CRC is %08lX - %02X %02X %02X %02X\n", __PRETTY_FUNCTION__, crc, buf[dwReceived+0], buf[dwReceived+1], buf[dwReceived+2], buf[dwReceived+3]);
|
|
|
|
|
|
|
|
// Add FCS size to the received message size
|
2020-08-28 14:18:02 +00:00
|
|
|
dwReceived += 4;
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Return the number of bytes
|
2020-08-28 14:18:02 +00:00
|
|
|
return dwReceived;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2020-12-28 02:13:21 +00:00
|
|
|
// Send
|
2020-08-28 14:18:02 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
2021-02-07 19:00:48 +00:00
|
|
|
int FASTCALL CTapDriver::Tx(const BYTE *buf, int len)
|
2020-08-28 14:18:02 +00:00
|
|
|
{
|
|
|
|
ASSERT(this);
|
|
|
|
ASSERT(m_hTAP != -1);
|
|
|
|
|
2020-12-28 02:13:21 +00:00
|
|
|
// Start sending
|
2020-08-28 14:18:02 +00:00
|
|
|
return write(m_hTAP, buf, len);
|
|
|
|
}
|