Merge branch 'etherslave'

Conflicts:
	BasiliskII/src/Unix/configure.ac
This commit is contained in:
Dan Sumorok 2013-05-14 06:39:11 -04:00
commit 531b1d7df4
7 changed files with 606 additions and 1 deletions

View File

@ -0,0 +1,304 @@
/*
* etherslavetool.c - Reads and writes raw ethernet packets usng bpf
* interface.
*
* Copyright (C) 2010, Daniel Sumorok
*
* Basilisk II (C) 1997-2008 Christian Bauer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <errno.h>
#include <sys/select.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <net/bpf.h>
#include <fcntl.h>
#include <strings.h>
#include <Carbon/Carbon.h>
static int open_bpf(char *ifname);
static int retreive_auth_info(void);
static int main_loop(int sd);
int main(int argc, char **argv)
{
char *if_name;
int ret;
int sd;
if (argc != 2) {
return 255;
}
if_name = argv[1];
ret = retreive_auth_info();
if (ret != 0) {
return 254;
}
fflush(stdout);
sd = open_bpf(if_name);
if (sd < 0) {
return 253;
}
fflush(stdout);
ret = main_loop(sd);
close(sd);
if (ret < 0) {
return 252;
}
return 0;
}
static int main_loop(int sd)
{
fd_set readSet;
char *outgoing, *incoming;
unsigned short *out_len;
unsigned short *in_len;
int in_index, out_index;
u_int blen = 0;
int ret;
int fret = 0;
struct bpf_hdr *hdr;
int pkt_len;
int frame_len;
int pad;
if (ioctl(sd, BIOCGBLEN, &blen) < 0) {
return -1;
}
incoming = malloc(blen);
if (incoming == NULL) {
return -2;
}
outgoing = malloc(blen);
if (outgoing == NULL) {
free(outgoing);
return -3;
}
in_index = 0;
out_index = 0;
out_len = (unsigned short *)outgoing;
while (1) {
int i;
FD_ZERO(&readSet);
FD_SET(0, &readSet);
FD_SET(sd, &readSet);
ret = select(sd + 1, &readSet, NULL, NULL, NULL);
if (ret < 0) {
fret = -4;
break;
}
if (FD_ISSET(0, &readSet)) {
if (out_index < 2) {
ret = read(0, outgoing + out_index, 2-out_index);
} else {
ret = read(0, outgoing + out_index, *out_len - out_index + 2);
}
if (ret < 1) {
fret = -5;
break;
}
out_index += ret;
if (out_index > 1) {
fflush(stdout);
if ((*out_len + 2) > blen) {
fret = -6;
break;
}
if (out_index == (*out_len + 2)) {
ret = write(sd, out_len + 1, *out_len);
if (ret != *out_len) {
fret = -7;
break;
}
out_index = 0;
}
}
}
if (FD_ISSET(sd, &readSet)) {
int i;
ret = read(sd, incoming, blen);
if (ret < 1) {
fret = -8;
break;
}
hdr = (struct bpf_hdr *)incoming;
in_len = (unsigned short *)(incoming + 16);
do {
pkt_len = hdr->bh_caplen;
frame_len = pkt_len + 18;
if ((pkt_len < 0) || (frame_len > ret) || (frame_len < 0)) {
fret = -9;
break;
}
*in_len = pkt_len;
write(0, in_len, pkt_len + 2);
if ((frame_len & 0x03) == 0) {
pad = 0;
} else {
pad = 4 - (frame_len & 0x03);
}
ret -= (frame_len + pad);
hdr = (struct bpf_hdr *)((unsigned char *)hdr + frame_len + pad);
in_len = (unsigned short *)((unsigned char *)hdr + 16);
} while (ret > 0);
if (fret != 0) {
break;
}
}
}
free(incoming);
free(outgoing);
return fret;
}
static int retreive_auth_info(void)
{
AuthorizationRef aRef;
OSStatus status;
AuthorizationRights myRights;
AuthorizationRights *newRights;
AuthorizationItem *myItem;
AuthorizationItem myItems[1];
AuthorizationItemSet *mySet;
int i;
status = AuthorizationCopyPrivilegedReference(&aRef, kAuthorizationFlagDefaults);
if (status != errAuthorizationSuccess) {
return -1;
}
status = AuthorizationCopyInfo(aRef, NULL, &mySet);
if (status != errAuthorizationSuccess) {
AuthorizationFree(aRef, kAuthorizationFlagDestroyRights);
return -1;
}
myItems[0].name = "system.privilege.admin";
myItems[0].valueLength = 0;
myItems[0].value = NULL;
myItems[0].flags = 0;
myRights.count = sizeof (myItems) / sizeof (myItems[0]);
myRights.items = myItems;
status = AuthorizationCopyRights(aRef, &myRights, NULL,
kAuthorizationFlagExtendRights,
&newRights);
if (status != errAuthorizationSuccess) {
AuthorizationFreeItemSet(mySet);
AuthorizationFree(aRef, kAuthorizationFlagDestroyRights);
return -2;
}
AuthorizationFreeItemSet(newRights);
AuthorizationFreeItemSet(mySet);
AuthorizationFree(aRef, kAuthorizationFlagDestroyRights);
return 0;
}
static int open_bpf(char *ifname)
{
u_int blen = 0;
struct ifreq ifreq;
u_int arg;
int sd = open("/dev/bpf2", O_RDWR);
if (sd < 0) {
return -1;
}
if (ioctl(sd, BIOCGBLEN, &blen) < 0) {
close(sd);
return -2;
}
bzero(&ifreq, sizeof(ifreq));
strncpy(ifreq.ifr_name, ifname, IFNAMSIZ);
arg = 0;
if (ioctl(sd, BIOCSETIF, &ifreq) < 0) {
close(sd);
return -3;
}
arg = 0;
if (ioctl(sd, BIOCSSEESENT, &arg) < 0) {
close(sd);
return -4;
}
arg = 1;
if (ioctl(sd, BIOCPROMISC, &arg) < 0) {
close(sd);
return -5;
}
arg = 1;
if (ioctl(sd, BIOCIMMEDIATE, &arg) < 0) {
close(sd);
return -6;
}
return sd;
}

View File

@ -0,0 +1,100 @@
/*
* runtool.m - Run an external program as root for networking
* Copyright (C) 2010, Daniel Sumorok
*
* Basilisk II (C) 1997-2008 Christian Bauer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <errno.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <net/bpf.h>
#include <fcntl.h>
#include <strings.h>
#include <Carbon/Carbon.h>
FILE * run_tool(const char *ifName);
FILE * run_tool(const char *ifName)
{
OSStatus auth_status;
FILE *fp;
char *args[] = {"etherslavetool", NULL, NULL};
int ret;
const char *path;
AuthorizationFlags auth_flags;
AuthorizationRef auth_ref;
AuthorizationItem auth_items[1];
AuthorizationRights auth_rights;
path = [[[NSBundle mainBundle]
pathForResource:@"etherslavetool" ofType: nil] UTF8String];
if (path == NULL) {
return NULL;
}
args[1] = (char *)ifName;
auth_flags = kAuthorizationFlagExtendRights |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize;
auth_items[0].name = "system.privilege.admin";
auth_items[0].valueLength = 0;
auth_items[0].value = NULL;
auth_items[0].flags = 0;
auth_rights.count = sizeof (auth_items) / sizeof (auth_items[0]);
auth_rights.items = auth_items;
auth_status = AuthorizationCreate(&auth_rights,
kAuthorizationEmptyEnvironment,
auth_flags,
&auth_ref);
if (auth_status != errAuthorizationSuccess) {
fprintf(stderr, "%s: AuthorizationCreate() failed.\n", __func__);
return NULL;
}
auth_status = AuthorizationExecuteWithPrivileges(auth_ref,
path,
kAuthorizationFlagDefaults,
args + 1,
&fp);
if (auth_status != errAuthorizationSuccess) {
fprintf(stderr, "%s: AuthorizationExecWithPrivileges() failed.\n", __func__);
return NULL;
}
return fp;
}

View File

@ -1,6 +1,7 @@
# Object files
obj/*
BasiliskII
etherslavetool
# Autotools generated files
Makefile

View File

@ -74,6 +74,10 @@ CXXFLAGS += $(GUI_CFLAGS)
LIBS += $(GUI_LIBS)
endif
ifeq (@MACOSX_ETHERSLAVE@,yes)
PROGS += etherslavetool
endif
## Rules
.PHONY: modules install installdirs uninstall mostlyclean clean distclean depend dep
.SUFFIXES:
@ -138,6 +142,9 @@ $(GUI_APP)_app: $(GUI_APP) ../MacOSX/Info.plist ../MacOSX/$(APP).icns
mkdir -p $(GUI_APP_APP)/Contents/Resources
./cpr.sh ../MacOSX/$(APP).icns $(GUI_APP_APP)/Contents/Resources/$(GUI_APP).icns
etherslavetool: ../MacOSX/etherslavetool.c
$(CC) $(CPPFLAGS) $(DEFS) $(CFLAGS) $(LIBS) $< -o $@
modules:
cd Linux/NetDriver; make
@ -168,6 +175,7 @@ mostlyclean:
clean: mostlyclean
rm -f cpuemu.cpp cpudefs.cpp cputmp*.s cpufast*.s cpustbl.cpp cputbl.h compemu.cpp compstbl.cpp comptbl.h
rm -rf etherslavetool.dSYM
distclean: clean
rm -rf $(OBJ_DIR)

View File

@ -24,6 +24,9 @@ AC_ARG_ENABLE(macosx-gui, [ --enable-macosx-gui enable Mac OS X GUI [def
dnl Mac OS X Sound
AC_ARG_ENABLE(macosx-sound, [ --enable-macosx-sound enable Mac OS X Sound [default=no]], [WANT_MACOSX_SOUND=$enableval], [WANT_MACOSX_SOUND=no])
dnl Mac OS X etherslave support
AC_ARG_ENABLE(macosx-etherslave, [ --enable-macosx-etherslave enable Mac OS X Sound [default=no]], [WANT_MACOSX_ETHERSLAVE=$enableval], [WANT_MACOSX_ETHERSLAVE=no])
dnl Video options.
AC_ARG_ENABLE(xf86-dga, [ --enable-xf86-dga use the XFree86 DGA extension [default=yes]], [WANT_XF86_DGA=$enableval], [WANT_XF86_DGA=yes])
AC_ARG_ENABLE(xf86-vidmode, [ --enable-xf86-vidmode use the XFree86 VidMode extension [default=yes]], [WANT_XF86_VIDMODE=$enableval], [WANT_XF86_VIDMODE=yes])
@ -732,6 +735,14 @@ if [[ "x$WANT_MACOSX_SOUND" = "xyes" ]]; then
LIBS="$LIBS -framework AudioToolbox -framework AudioUnit -framework CoreAudio"
fi
if [[ "x$WANT_MACOSX_ETHERSLAVE" = "xyes" ]]; then
EXTRASYSSRCS="$EXTRASYSSRCS ../MacOSX/runtool.m"
LIBS="$LIBS -framework Security"
AC_DEFINE(ENABLE_MACOSX_ETHERSLAVE, 1, [Define if supporting "etherslave" network device.])
fi
AC_SUBST(MACOSX_ETHERSLAVE, $WANT_MACOSX_ETHERSLAVE)
dnl SDL overrides
if [[ "x$WANT_SDL" = "xyes" ]]; then
AC_DEFINE(USE_SDL, 1, [Define to enble SDL support])

View File

@ -41,6 +41,12 @@
#endif
#include <sys/ioctl.h>
#include <sys/socket.h>
#ifdef ENABLE_MACOSX_ETHERSLAVE
#include <net/if_dl.h>
#include <ifaddrs.h>
#endif
#include <sys/wait.h>
#include <netinet/in.h>
#include <pthread.h>
@ -93,9 +99,17 @@ enum {
NET_IF_SHEEPNET,
NET_IF_ETHERTAP,
NET_IF_TUNTAP,
NET_IF_SLIRP
NET_IF_SLIRP,
NET_IF_ETHERSLAVE
};
#ifdef ENABLE_MACOSX_ETHERSLAVE
extern "C" {
extern FILE * run_tool(const char *if_name);
}
#endif
// Constants
#if ENABLE_TUNTAP
static const char ETHERCONFIG_FILE_NAME[] = DATADIR "/tunconfig";
@ -122,6 +136,11 @@ static uint8 ether_addr[6]; // Our Ethernet address
const bool ether_driver_opened = true; // Flag: is the MacOS driver opened?
#endif
#ifdef ENABLE_MACOSX_ETHERSLAVE
static uint8 packet_buffer[2048];
#endif
// Attached network protocols, maps protocol type to MacOS handler address
static map<uint16, uint32> net_protocols;
@ -135,6 +154,11 @@ static void ether_do_interrupt(void);
static void slirp_add_redirs();
static int slirp_add_redir(const char *redir_str);
#ifdef ENABLE_MACOSX_ETHERSLAVE
static int get_mac_address(const char* dev, unsigned char *addr);
static bool open_ether_slave(const char *if_name);
static int read_packet(void);
#endif
/*
* Start packet reception thread
@ -235,6 +259,9 @@ bool ether_init(void)
// Do nothing if no Ethernet device specified
const char *name = PrefsFindString("ether");
#ifdef ENABLE_MACOSX_ETHERSLAVE
const char *slave_dev = PrefsFindString("etherslavedev");
#endif
if (name == NULL)
return false;
@ -249,6 +276,10 @@ bool ether_init(void)
#ifdef HAVE_SLIRP
else if (strcmp(name, "slirp") == 0)
net_if_type = NET_IF_SLIRP;
#endif
#ifdef ENABLE_MACOSX_ETHERSLAVE
else if (strcmp(name, "etherslave") == 0)
net_if_type = NET_IF_ETHERSLAVE;
#endif
else
net_if_type = NET_IF_SHEEPNET;
@ -300,6 +331,14 @@ bool ether_init(void)
case NET_IF_SHEEPNET:
strcpy(dev_name, "/dev/sheep_net");
break;
#ifdef ENABLE_MACOSX_ETHERSLAVE
case NET_IF_ETHERSLAVE:
if (slave_dev == NULL) {
WarningAlert("etherslavedev not defined in preferences.");
return false;
}
return open_ether_slave(slave_dev);
#endif
}
if (net_if_type != NET_IF_SLIRP) {
fd = open(dev_name, O_RDWR);
@ -750,6 +789,21 @@ static int16 ether_do_write(uint32 arg)
write(slirp_input_fd, packet, len);
return noErr;
} else
#endif
#ifdef ENABLE_MACOSX_ETHERSLAVE
if (net_if_type == NET_IF_ETHERSLAVE) {
unsigned short pkt_len;
pkt_len = len;
if (write(fd, &pkt_len, 2) < 2) {
return excessCollsns;
}
if (write(fd, packet, len) < len) {
return excessCollsns;
}
return noErr;
} else
#endif
if (write(fd, packet, len) < 0) {
D(bug("WARNING: Couldn't transmit packet\n"));
@ -884,6 +938,13 @@ static void *receive_func(void *arg)
if (res <= 0)
break;
#ifdef ENABLE_MACOSX_ETHERSLAVE
if (net_if_type == NET_IF_ETHERSLAVE) {
if (read_packet() < 1) {
break;
}
}
#endif
if (ether_driver_opened) {
// Trigger Ethernet interrupt
D(bug(" packet received, triggering Ethernet interrupt\n"));
@ -923,6 +984,18 @@ void ether_do_interrupt(void)
ether_udp_read(packet, length, &from);
} else
#endif
#ifdef ENABLE_MACOSX_ETHERSLAVE
if (net_if_type == NET_IF_ETHERSLAVE) {
unsigned short *pkt_len;
uint32 p = packet;
pkt_len = (unsigned short *)packet_buffer;
length = *pkt_len;
memcpy(Mac2HostAddr(packet), pkt_len + 1, length);
ether_dispatch_packet(p, length);
break;
} else
#endif
{
@ -1049,3 +1122,108 @@ static int slirp_add_redir(const char *redir_str)
WarningAlert(str);
return -1;
}
#ifdef ENABLE_MACOSX_ETHERSLAVE
static int get_mac_address(const char* dev, unsigned char *addr)
{
struct ifaddrs *ifaddrs, *next;
int ret = -1;
struct sockaddr_dl *sa;
if (getifaddrs(&ifaddrs) != 0) {
perror("getifaddrs");
return -1;
}
next = ifaddrs;
while (next != NULL) {
switch (next->ifa_addr->sa_family) {
case AF_LINK:
if (!strcmp(dev, next->ifa_name)) {
sa = (struct sockaddr_dl *)next->ifa_addr;
memcpy(addr, LLADDR(sa), 6);
ret = 0;
}
break;
default:
break;
}
next = next->ifa_next;
}
freeifaddrs(ifaddrs);
return ret;
}
static bool open_ether_slave(const char *if_name)
{
FILE *fp;
char str[64];
if (get_mac_address(if_name, ether_addr) != 0) {
snprintf(str, sizeof(str), "Unable to find interface %s.",
if_name);
WarningAlert(str);
return false;
}
fp = run_tool(if_name);
if (fp == NULL) {
snprintf(str, sizeof(str), "Unable to run ether slave helper tool.");
WarningAlert(str);
return false;
}
fd = dup(fileno(fp));
fclose(fp);
if (start_thread() == false) {
close(fd);
fd = -1;
return false;
}
return true;
}
static int read_packet()
{
int index;
unsigned short *pkt_len;
int ret = -1;
pkt_len = (unsigned short *)packet_buffer;
index = 0;
while (1) {
if (index < 2) {
ret = read(fd, packet_buffer + index, 2 - index);
} else {
ret = read(fd, packet_buffer + index, *pkt_len - index + 2);
}
if (ret < 1) {
fprintf(stderr, "%s: read() returned %d.\n", __func__, ret);
break;
}
index += ret;
if (index > 1) {
if (*pkt_len > (sizeof(packet_buffer) + 2)) {
fprintf(stderr, "%s: pkt_len (%d) too large.\n", __func__, *pkt_len);
break;
}
if (index == (*pkt_len + 2)) {
ret = *pkt_len;
break;
}
}
}
return ret;
}
#endif

View File

@ -40,6 +40,9 @@ prefs_desc platform_prefs_items[] = {
{"mixer", TYPE_STRING, false, "audio mixer device name"},
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
{"ignoresegv", TYPE_BOOLEAN, false, "ignore illegal memory accesses"},
#endif
#ifdef ENABLE_MACOSX_ETHERSLAVE
{"etherslavedev", TYPE_STRING, false, "ethernet device for etherslave ethernet"},
#endif
{"idlewait", TYPE_BOOLEAN, false, "sleep when idle"},
{NULL, TYPE_END, false, NULL} // End of list