1999-10-03 14:16:26 +00:00
|
|
|
/*
|
2001-03-29 14:20:55 +00:00
|
|
|
* ether_unix.cpp - Ethernet device driver, Unix specific stuff (Linux and FreeBSD)
|
1999-10-03 14:16:26 +00:00
|
|
|
*
|
2008-01-01 09:40:36 +00:00
|
|
|
* Basilisk II (C) 1997-2008 Christian Bauer
|
1999-10-03 14:16:26 +00:00
|
|
|
*
|
|
|
|
* 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 "sysdeps.h"
|
|
|
|
|
2018-09-28 14:14:14 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <libvdeplug.h>
|
|
|
|
}
|
2006-01-21 16:19:47 +00:00
|
|
|
/*
|
|
|
|
* NOTES concerning MacOS X issues:
|
|
|
|
* - poll() does not exist in 10.2.8, but is available in 10.4.4
|
|
|
|
* - select(), and very likely poll(), are not cancellation points. So
|
|
|
|
* the ethernet thread doesn't stop on exit. An explicit check is
|
|
|
|
* performed to workaround this problem.
|
|
|
|
*/
|
|
|
|
#if (defined __APPLE__ && defined __MACH__) || ! defined HAVE_POLL
|
|
|
|
#define USE_POLL 0
|
|
|
|
#else
|
|
|
|
#define USE_POLL 1
|
|
|
|
#endif
|
|
|
|
|
2006-04-02 21:06:50 +00:00
|
|
|
// Define to let the slirp library determine the right timeout for select()
|
|
|
|
#define USE_SLIRP_TIMEOUT 1
|
|
|
|
|
2005-05-13 17:43:38 +00:00
|
|
|
#ifdef HAVE_SYS_POLL_H
|
1999-10-03 14:16:26 +00:00
|
|
|
#include <sys/poll.h>
|
2005-05-13 17:43:38 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/ioctl.h>
|
2001-07-13 15:39:25 +00:00
|
|
|
#include <sys/socket.h>
|
2004-05-09 16:16:05 +00:00
|
|
|
#include <sys/wait.h>
|
2001-07-13 15:39:25 +00:00
|
|
|
#include <netinet/in.h>
|
2000-07-13 16:12:33 +00:00
|
|
|
#include <pthread.h>
|
1999-10-03 14:16:26 +00:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2007-11-03 11:31:36 +00:00
|
|
|
#include <signal.h>
|
2001-07-13 15:39:25 +00:00
|
|
|
#include <map>
|
2001-07-12 19:48:28 +00:00
|
|
|
|
2005-05-13 17:43:38 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(sgi) || (defined(__APPLE__) && defined(__MACH__))
|
2001-03-29 14:20:55 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#endif
|
|
|
|
|
2004-05-09 16:16:05 +00:00
|
|
|
#if defined(HAVE_LINUX_IF_H) && defined(HAVE_LINUX_IF_TUN_H)
|
|
|
|
#include <linux/if.h>
|
|
|
|
#include <linux/if_tun.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_NET_IF_H) && defined(HAVE_NET_IF_TUN_H)
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_tun.h>
|
|
|
|
#endif
|
|
|
|
|
2005-05-14 08:10:49 +00:00
|
|
|
#ifdef HAVE_SLIRP
|
2005-05-13 14:02:37 +00:00
|
|
|
#include "libslirp.h"
|
2012-10-30 05:32:38 +00:00
|
|
|
#include "ctl.h"
|
2005-05-14 08:10:49 +00:00
|
|
|
#endif
|
2005-05-13 09:21:21 +00:00
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
#include "cpu_emulation.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "macos_util.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "user_strings.h"
|
|
|
|
#include "ether.h"
|
|
|
|
#include "ether_defs.h"
|
|
|
|
|
2001-07-13 15:39:25 +00:00
|
|
|
#ifndef NO_STD_NAMESPACE
|
|
|
|
using std::map;
|
|
|
|
#endif
|
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
#include "debug.h"
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
#define STATISTICS 0
|
1999-10-03 14:16:26 +00:00
|
|
|
#define MONITOR 0
|
|
|
|
|
|
|
|
|
2004-05-09 16:16:05 +00:00
|
|
|
// Ethernet device types
|
|
|
|
enum {
|
|
|
|
NET_IF_SHEEPNET,
|
|
|
|
NET_IF_ETHERTAP,
|
|
|
|
NET_IF_TUNTAP,
|
2018-09-28 14:14:14 +00:00
|
|
|
NET_IF_SLIRP,
|
|
|
|
NET_IF_VDE
|
2004-05-09 16:16:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Constants
|
2012-09-02 11:52:28 +00:00
|
|
|
#if ENABLE_TUNTAP
|
2004-05-09 16:16:05 +00:00
|
|
|
static const char ETHERCONFIG_FILE_NAME[] = DATADIR "/tunconfig";
|
2012-09-02 11:52:28 +00:00
|
|
|
#endif
|
2004-05-09 16:16:05 +00:00
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
// Global variables
|
|
|
|
static int fd = -1; // fd of sheep_net device
|
|
|
|
static pthread_t ether_thread; // Packet reception thread
|
|
|
|
static pthread_attr_t ether_thread_attr; // Packet reception thread attributes
|
|
|
|
static bool thread_active = false; // Flag: Packet reception thread installed
|
|
|
|
static sem_t int_ack; // Interrupt acknowledge semaphore
|
2001-07-12 19:48:28 +00:00
|
|
|
static bool udp_tunnel; // Flag: UDP tunnelling active, fd is the socket descriptor
|
2004-05-09 16:16:05 +00:00
|
|
|
static int net_if_type = -1; // Ethernet device type
|
2004-05-09 16:32:12 +00:00
|
|
|
static char *net_if_name = NULL; // TUN/TAP device name
|
2004-05-09 16:16:05 +00:00
|
|
|
static const char *net_if_script = NULL; // Network config script
|
2005-05-13 09:21:21 +00:00
|
|
|
static pthread_t slirp_thread; // Slirp reception thread
|
|
|
|
static bool slirp_thread_active = false; // Flag: Slirp reception threadinstalled
|
|
|
|
static int slirp_output_fd = -1; // fd of slirp output pipe
|
2005-05-21 17:48:21 +00:00
|
|
|
static int slirp_input_fds[2] = { -1, -1 }; // fds of slirp input pipe
|
2018-10-20 09:18:08 +00:00
|
|
|
static VDECONN *vde_conn;
|
2005-07-03 08:21:32 +00:00
|
|
|
#ifdef SHEEPSHAVER
|
|
|
|
static bool net_open = false; // Flag: initialization succeeded, network device open
|
|
|
|
static uint8 ether_addr[6]; // Our Ethernet address
|
|
|
|
#else
|
|
|
|
const bool ether_driver_opened = true; // Flag: is the MacOS driver opened?
|
|
|
|
#endif
|
1999-10-03 14:16:26 +00:00
|
|
|
|
2001-07-13 15:39:25 +00:00
|
|
|
// Attached network protocols, maps protocol type to MacOS handler address
|
|
|
|
static map<uint16, uint32> net_protocols;
|
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
// Prototypes
|
|
|
|
static void *receive_func(void *arg);
|
2005-05-13 09:21:21 +00:00
|
|
|
static void *slirp_receive_func(void *arg);
|
2005-07-03 08:21:32 +00:00
|
|
|
static int16 ether_do_add_multicast(uint8 *addr);
|
|
|
|
static int16 ether_do_del_multicast(uint8 *addr);
|
|
|
|
static int16 ether_do_write(uint32 arg);
|
|
|
|
static void ether_do_interrupt(void);
|
2012-10-30 05:32:38 +00:00
|
|
|
static void slirp_add_redirs();
|
|
|
|
static int slirp_add_redir(const char *redir_str);
|
1999-10-03 14:16:26 +00:00
|
|
|
|
|
|
|
|
2001-07-12 19:48:28 +00:00
|
|
|
/*
|
|
|
|
* Start packet reception thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool start_thread(void)
|
|
|
|
{
|
|
|
|
if (sem_init(&int_ack, 0, 0) < 0) {
|
|
|
|
printf("WARNING: Cannot init semaphore");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-02-07 16:10:57 +00:00
|
|
|
Set_pthread_attr(ðer_thread_attr, 1);
|
2001-07-12 19:48:28 +00:00
|
|
|
thread_active = (pthread_create(ðer_thread, ðer_thread_attr, receive_func, NULL) == 0);
|
|
|
|
if (!thread_active) {
|
|
|
|
printf("WARNING: Cannot start Ethernet thread");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-05-13 09:21:21 +00:00
|
|
|
#ifdef HAVE_SLIRP
|
|
|
|
if (net_if_type == NET_IF_SLIRP) {
|
|
|
|
slirp_thread_active = (pthread_create(&slirp_thread, NULL, slirp_receive_func, NULL) == 0);
|
|
|
|
if (!slirp_thread_active) {
|
|
|
|
printf("WARNING: Cannot start slirp reception thread\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-07-12 19:48:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop packet reception thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void stop_thread(void)
|
|
|
|
{
|
2005-05-13 09:21:21 +00:00
|
|
|
#ifdef HAVE_SLIRP
|
|
|
|
if (slirp_thread_active) {
|
2005-05-15 17:22:12 +00:00
|
|
|
#ifdef HAVE_PTHREAD_CANCEL
|
2005-05-13 09:21:21 +00:00
|
|
|
pthread_cancel(slirp_thread);
|
2005-05-15 17:22:12 +00:00
|
|
|
#endif
|
2005-05-13 09:21:21 +00:00
|
|
|
pthread_join(slirp_thread, NULL);
|
|
|
|
slirp_thread_active = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-07-12 19:48:28 +00:00
|
|
|
if (thread_active) {
|
2005-05-15 17:22:12 +00:00
|
|
|
#ifdef HAVE_PTHREAD_CANCEL
|
2001-07-12 19:48:28 +00:00
|
|
|
pthread_cancel(ether_thread);
|
2005-05-15 17:22:12 +00:00
|
|
|
#endif
|
2001-07-12 19:48:28 +00:00
|
|
|
pthread_join(ether_thread, NULL);
|
|
|
|
sem_destroy(&int_ack);
|
|
|
|
thread_active = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-09 16:16:05 +00:00
|
|
|
/*
|
|
|
|
* Execute network script up|down
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool execute_network_script(const char *action)
|
|
|
|
{
|
|
|
|
if (net_if_script == NULL || net_if_name == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int pid = fork();
|
|
|
|
if (pid >= 0) {
|
|
|
|
if (pid == 0) {
|
|
|
|
char *args[4];
|
|
|
|
args[0] = (char *)net_if_script;
|
2004-05-09 16:32:12 +00:00
|
|
|
args[1] = net_if_name;
|
2004-05-09 16:16:05 +00:00
|
|
|
args[2] = (char *)action;
|
|
|
|
args[3] = NULL;
|
|
|
|
execv(net_if_script, args);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
int status;
|
|
|
|
while (waitpid(pid, &status, 0) != pid);
|
|
|
|
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
/*
|
|
|
|
* Initialization
|
|
|
|
*/
|
|
|
|
|
2001-07-12 19:48:28 +00:00
|
|
|
bool ether_init(void)
|
1999-10-03 14:16:26 +00:00
|
|
|
{
|
2012-03-01 04:27:42 +00:00
|
|
|
int val;
|
1999-10-03 14:16:26 +00:00
|
|
|
char str[256];
|
|
|
|
|
|
|
|
// Do nothing if no Ethernet device specified
|
|
|
|
const char *name = PrefsFindString("ether");
|
|
|
|
if (name == NULL)
|
2001-07-12 19:48:28 +00:00
|
|
|
return false;
|
1999-10-03 14:16:26 +00:00
|
|
|
|
2004-05-09 16:16:05 +00:00
|
|
|
// Determine Ethernet device type
|
|
|
|
net_if_type = -1;
|
2018-10-20 09:18:08 +00:00
|
|
|
if (strncmp(name, "tap", 3) == 0) {
|
2004-05-09 16:16:05 +00:00
|
|
|
net_if_type = NET_IF_ETHERTAP;
|
2018-10-20 09:18:08 +00:00
|
|
|
printf("selected Ethernet device type tap\n");
|
|
|
|
}
|
2004-05-09 16:16:05 +00:00
|
|
|
#if ENABLE_TUNTAP
|
2018-10-20 09:18:08 +00:00
|
|
|
else if (strcmp(name, "tun") == 0) {
|
2004-05-09 16:16:05 +00:00
|
|
|
net_if_type = NET_IF_TUNTAP;
|
2018-10-20 09:18:08 +00:00
|
|
|
printf("selected Ethernet device type tun\n");
|
|
|
|
}
|
2005-05-13 09:21:21 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SLIRP
|
2018-10-20 09:18:08 +00:00
|
|
|
else if (strcmp(name, "slirp") == 0) {
|
2005-05-13 09:21:21 +00:00
|
|
|
net_if_type = NET_IF_SLIRP;
|
2018-10-20 09:18:08 +00:00
|
|
|
printf("selected Ethernet device type slirp\n");
|
|
|
|
}
|
2004-05-09 16:16:05 +00:00
|
|
|
#endif
|
2018-10-20 09:18:08 +00:00
|
|
|
else if (strcmp(name, "vde") == 0) {
|
2018-09-28 14:14:14 +00:00
|
|
|
net_if_type = NET_IF_VDE;
|
2018-10-20 09:18:08 +00:00
|
|
|
printf("selected Ethernet device type VDE\n");
|
2018-09-28 14:14:14 +00:00
|
|
|
}
|
2018-10-20 09:18:08 +00:00
|
|
|
else {
|
2004-05-09 16:16:05 +00:00
|
|
|
net_if_type = NET_IF_SHEEPNET;
|
2018-10-20 09:18:08 +00:00
|
|
|
printf("selected Ethernet device type sheep_net\n");
|
|
|
|
}
|
1999-10-03 14:16:26 +00:00
|
|
|
|
2007-11-03 11:31:36 +00:00
|
|
|
// Don't raise SIGPIPE, let errno be set to EPIPE
|
|
|
|
struct sigaction sigpipe_sa;
|
|
|
|
if (sigaction(SIGPIPE, NULL, &sigpipe_sa) == 0) {
|
|
|
|
assert(sigpipe_sa.sa_handler == SIG_DFL || sigpipe_sa.sa_handler == SIG_IGN);
|
|
|
|
sigfillset(&sigpipe_sa.sa_mask);
|
|
|
|
sigpipe_sa.sa_flags = 0;
|
|
|
|
sigpipe_sa.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &sigpipe_sa, NULL);
|
|
|
|
}
|
|
|
|
|
2005-05-13 09:21:21 +00:00
|
|
|
#ifdef HAVE_SLIRP
|
|
|
|
// Initialize slirp library
|
|
|
|
if (net_if_type == NET_IF_SLIRP) {
|
2005-06-12 22:48:48 +00:00
|
|
|
if (slirp_init() < 0) {
|
2012-04-01 15:05:55 +00:00
|
|
|
sprintf(str, "%s", GetString(STR_SLIRP_NO_DNS_FOUND_WARN));
|
2005-06-12 22:48:48 +00:00
|
|
|
WarningAlert(str);
|
|
|
|
return false;
|
|
|
|
}
|
2005-05-13 09:21:21 +00:00
|
|
|
|
|
|
|
// Open slirp output pipe
|
|
|
|
int fds[2];
|
|
|
|
if (pipe(fds) < 0)
|
|
|
|
return false;
|
|
|
|
fd = fds[0];
|
|
|
|
slirp_output_fd = fds[1];
|
2005-05-21 17:48:21 +00:00
|
|
|
|
|
|
|
// Open slirp input pipe
|
|
|
|
if (pipe(slirp_input_fds) < 0)
|
|
|
|
return false;
|
2012-10-30 05:32:38 +00:00
|
|
|
|
|
|
|
// Set up port redirects
|
|
|
|
slirp_add_redirs();
|
2005-05-13 09:21:21 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-12 11:46:34 +00:00
|
|
|
// Open sheep_net or ethertap or TUN/TAP device
|
1999-10-03 14:16:26 +00:00
|
|
|
char dev_name[16];
|
2004-05-09 16:16:05 +00:00
|
|
|
switch (net_if_type) {
|
|
|
|
case NET_IF_ETHERTAP:
|
1999-10-03 14:16:26 +00:00
|
|
|
sprintf(dev_name, "/dev/%s", name);
|
2004-05-09 16:16:05 +00:00
|
|
|
break;
|
|
|
|
case NET_IF_TUNTAP:
|
2004-05-12 11:46:34 +00:00
|
|
|
strcpy(dev_name, "/dev/net/tun");
|
2004-05-09 16:16:05 +00:00
|
|
|
break;
|
|
|
|
case NET_IF_SHEEPNET:
|
1999-10-03 14:16:26 +00:00
|
|
|
strcpy(dev_name, "/dev/sheep_net");
|
2004-05-09 16:16:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-28 14:14:14 +00:00
|
|
|
|
|
|
|
//vde switch information
|
|
|
|
int port = 0;
|
|
|
|
char *init_group = NULL;
|
|
|
|
mode_t mode = 0700;
|
|
|
|
|
|
|
|
struct vde_open_args args = {
|
|
|
|
.port = port,
|
|
|
|
.group = init_group,
|
|
|
|
.mode = mode,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (net_if_type == NET_IF_VDE) {
|
|
|
|
/* calling vde open to open the vde connection to the vde switch */
|
|
|
|
vde_conn = vde_open(vde_sock, (char *)"macemu", &args);
|
|
|
|
|
|
|
|
if (!vde_conn) {
|
|
|
|
D(bug("VDE open failed\n"));
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
/* for select/poll when this fd receive data, there are
|
|
|
|
* packets to recv(call vde_recv) */
|
|
|
|
fd = vde_datafd(vde_conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (net_if_type != NET_IF_SLIRP && net_if_type != NET_IF_VDE) {
|
2005-05-13 09:21:21 +00:00
|
|
|
fd = open(dev_name, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno));
|
|
|
|
WarningAlert(str);
|
|
|
|
goto open_error;
|
|
|
|
}
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
|
2004-05-09 16:16:05 +00:00
|
|
|
#if ENABLE_TUNTAP
|
|
|
|
// Open TUN/TAP interface
|
|
|
|
if (net_if_type == NET_IF_TUNTAP) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
|
|
|
|
strcpy(ifr.ifr_name, "tun%d");
|
|
|
|
if (ioctl(fd, TUNSETIFF, (void *) &ifr) != 0) {
|
|
|
|
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno));
|
|
|
|
WarningAlert(str);
|
|
|
|
goto open_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get network config script file path
|
|
|
|
net_if_script = PrefsFindString("etherconfig");
|
|
|
|
if (net_if_script == NULL)
|
|
|
|
net_if_script = ETHERCONFIG_FILE_NAME;
|
|
|
|
|
|
|
|
// Start network script up
|
|
|
|
if (net_if_script == NULL) {
|
|
|
|
sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script not found");
|
|
|
|
WarningAlert(str);
|
|
|
|
goto open_error;
|
|
|
|
}
|
2004-05-09 16:32:12 +00:00
|
|
|
net_if_name = strdup(ifr.ifr_name);
|
2004-05-09 16:16:05 +00:00
|
|
|
if (!execute_network_script("up")) {
|
|
|
|
sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script execute error");
|
|
|
|
WarningAlert(str);
|
|
|
|
goto open_error;
|
|
|
|
}
|
|
|
|
D(bug("Connected to host network interface: %s\n", net_if_name));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-03-29 14:20:55 +00:00
|
|
|
#if defined(__linux__)
|
1999-10-03 14:16:26 +00:00
|
|
|
// Attach sheep_net to selected Ethernet card
|
2004-05-09 16:16:05 +00:00
|
|
|
if (net_if_type == NET_IF_SHEEPNET && ioctl(fd, SIOCSIFLINK, name) < 0) {
|
1999-10-03 14:16:26 +00:00
|
|
|
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno));
|
|
|
|
WarningAlert(str);
|
|
|
|
goto open_error;
|
|
|
|
}
|
2001-03-29 14:20:55 +00:00
|
|
|
#endif
|
1999-10-03 14:16:26 +00:00
|
|
|
|
|
|
|
// Set nonblocking I/O
|
2006-01-24 23:46:19 +00:00
|
|
|
#ifdef USE_FIONBIO
|
2012-03-01 04:27:42 +00:00
|
|
|
int nonblock = 1;
|
2006-01-24 23:46:19 +00:00
|
|
|
if (ioctl(fd, FIONBIO, &nonblock) < 0) {
|
|
|
|
sprintf(str, GetString(STR_BLOCKING_NET_SOCKET_WARN), strerror(errno));
|
|
|
|
WarningAlert(str);
|
|
|
|
goto open_error;
|
|
|
|
}
|
|
|
|
#else
|
2006-02-27 13:39:29 +00:00
|
|
|
val = fcntl(fd, F_GETFL, 0);
|
2006-01-24 23:46:19 +00:00
|
|
|
if (val < 0 || fcntl(fd, F_SETFL, val | O_NONBLOCK) < 0) {
|
|
|
|
sprintf(str, GetString(STR_BLOCKING_NET_SOCKET_WARN), strerror(errno));
|
|
|
|
WarningAlert(str);
|
|
|
|
goto open_error;
|
|
|
|
}
|
|
|
|
#endif
|
1999-10-03 14:16:26 +00:00
|
|
|
|
|
|
|
// Get Ethernet address
|
2017-12-28 04:41:38 +00:00
|
|
|
if (net_if_type == NET_IF_ETHERTAP || net_if_type == NET_IF_TUNTAP) {
|
1999-10-03 14:16:26 +00:00
|
|
|
pid_t p = getpid(); // If configured for multicast, ethertap requires that the lower 32 bit of the Ethernet address are our PID
|
|
|
|
ether_addr[0] = 0xfe;
|
|
|
|
ether_addr[1] = 0xfd;
|
|
|
|
ether_addr[2] = p >> 24;
|
|
|
|
ether_addr[3] = p >> 16;
|
|
|
|
ether_addr[4] = p >> 8;
|
|
|
|
ether_addr[5] = p;
|
2005-05-13 09:21:21 +00:00
|
|
|
#ifdef HAVE_SLIRP
|
|
|
|
} else if (net_if_type == NET_IF_SLIRP) {
|
|
|
|
ether_addr[0] = 0x52;
|
|
|
|
ether_addr[1] = 0x54;
|
|
|
|
ether_addr[2] = 0x00;
|
|
|
|
ether_addr[3] = 0x12;
|
|
|
|
ether_addr[4] = 0x34;
|
|
|
|
ether_addr[5] = 0x56;
|
|
|
|
#endif
|
2018-09-28 14:14:14 +00:00
|
|
|
} else if (net_if_type == NET_IF_VDE) {
|
|
|
|
ether_addr[0] = 0x52;
|
|
|
|
ether_addr[1] = 0x54;
|
|
|
|
ether_addr[2] = 0x00;
|
|
|
|
ether_addr[3] = 0x12;
|
|
|
|
ether_addr[4] = 0x34;
|
|
|
|
ether_addr[5] = 0x56;
|
1999-10-03 14:16:26 +00:00
|
|
|
} else
|
|
|
|
ioctl(fd, SIOCGIFADDR, ether_addr);
|
|
|
|
D(bug("Ethernet address %02x %02x %02x %02x %02x %02x\n", ether_addr[0], ether_addr[1], ether_addr[2], ether_addr[3], ether_addr[4], ether_addr[5]));
|
|
|
|
|
|
|
|
// Start packet reception thread
|
2001-07-12 19:48:28 +00:00
|
|
|
if (!start_thread())
|
1999-10-03 14:16:26 +00:00
|
|
|
goto open_error;
|
|
|
|
|
|
|
|
// Everything OK
|
2001-07-12 19:48:28 +00:00
|
|
|
return true;
|
1999-10-03 14:16:26 +00:00
|
|
|
|
|
|
|
open_error:
|
2001-07-12 19:48:28 +00:00
|
|
|
stop_thread();
|
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
if (fd > 0) {
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
2005-05-21 17:48:21 +00:00
|
|
|
if (slirp_input_fds[0] >= 0) {
|
|
|
|
close(slirp_input_fds[0]);
|
|
|
|
slirp_input_fds[0] = -1;
|
|
|
|
}
|
|
|
|
if (slirp_input_fds[1] >= 0) {
|
|
|
|
close(slirp_input_fds[1]);
|
|
|
|
slirp_input_fds[1] = -1;
|
|
|
|
}
|
2005-05-13 09:21:21 +00:00
|
|
|
if (slirp_output_fd >= 0) {
|
|
|
|
close(slirp_output_fd);
|
|
|
|
slirp_output_fd = -1;
|
|
|
|
}
|
2001-07-12 19:48:28 +00:00
|
|
|
return false;
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deinitialization
|
|
|
|
*/
|
|
|
|
|
2001-07-12 19:48:28 +00:00
|
|
|
void ether_exit(void)
|
1999-10-03 14:16:26 +00:00
|
|
|
{
|
2005-05-15 17:22:12 +00:00
|
|
|
// Stop reception threads
|
|
|
|
stop_thread();
|
1999-10-03 14:16:26 +00:00
|
|
|
|
2004-05-09 16:32:12 +00:00
|
|
|
// Shut down TUN/TAP interface
|
|
|
|
if (net_if_type == NET_IF_TUNTAP)
|
|
|
|
execute_network_script("down");
|
|
|
|
|
|
|
|
// Free TUN/TAP device name
|
|
|
|
if (net_if_name)
|
|
|
|
free(net_if_name);
|
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
// Close sheep_net device
|
|
|
|
if (fd > 0)
|
|
|
|
close(fd);
|
2005-05-13 09:21:21 +00:00
|
|
|
|
2005-05-21 17:48:21 +00:00
|
|
|
// Close slirp input buffer
|
|
|
|
if (slirp_input_fds[0] >= 0)
|
|
|
|
close(slirp_input_fds[0]);
|
|
|
|
if (slirp_input_fds[1] >= 0)
|
|
|
|
close(slirp_input_fds[1]);
|
|
|
|
|
2005-05-13 09:21:21 +00:00
|
|
|
// Close slirp output buffer
|
|
|
|
if (slirp_output_fd > 0)
|
|
|
|
close(slirp_output_fd);
|
2005-07-03 08:21:32 +00:00
|
|
|
|
2018-09-28 14:14:14 +00:00
|
|
|
// Close vde_connection
|
|
|
|
if (net_if_type == NET_IF_VDE)
|
|
|
|
vde_close(vde_conn);
|
2005-07-03 08:21:32 +00:00
|
|
|
#if STATISTICS
|
|
|
|
// Show statistics
|
|
|
|
printf("%ld messages put on write queue\n", num_wput);
|
|
|
|
printf("%ld error acks\n", num_error_acks);
|
|
|
|
printf("%ld packets transmitted (%ld raw, %ld normal)\n", num_tx_packets, num_tx_raw_packets, num_tx_normal_packets);
|
|
|
|
printf("%ld tx packets dropped because buffer full\n", num_tx_buffer_full);
|
|
|
|
printf("%ld packets received\n", num_rx_packets);
|
|
|
|
printf("%ld packets passed upstream (%ld Fast Path, %ld normal)\n", num_rx_fastpath + num_unitdata_ind, num_rx_fastpath, num_unitdata_ind);
|
|
|
|
printf("EtherIRQ called %ld times\n", num_ether_irq);
|
|
|
|
printf("%ld rx packets dropped due to low memory\n", num_rx_no_mem);
|
|
|
|
printf("%ld rx packets dropped because no stream found\n", num_rx_dropped);
|
|
|
|
printf("%ld rx packets dropped because stream not ready\n", num_rx_stream_not_ready);
|
|
|
|
printf("%ld rx packets dropped because no memory for unitdata_ind\n", num_rx_no_unitdata_mem);
|
|
|
|
#endif
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
/*
|
|
|
|
* Glue around low-level implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef SHEEPSHAVER
|
|
|
|
// Error codes
|
|
|
|
enum {
|
|
|
|
eMultiErr = -91,
|
|
|
|
eLenErr = -92,
|
|
|
|
lapProtErr = -94,
|
|
|
|
excessCollsns = -95
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize ethernet
|
|
|
|
void EtherInit(void)
|
|
|
|
{
|
|
|
|
net_open = false;
|
|
|
|
|
|
|
|
// Do nothing if the user disabled the network
|
|
|
|
if (PrefsFindBool("nonet"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
net_open = ether_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit ethernet
|
|
|
|
void EtherExit(void)
|
|
|
|
{
|
|
|
|
ether_exit();
|
|
|
|
net_open = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get ethernet hardware address
|
|
|
|
void AO_get_ethernet_address(uint32 arg)
|
|
|
|
{
|
|
|
|
uint8 *addr = Mac2HostAddr(arg);
|
|
|
|
if (net_open)
|
|
|
|
OTCopy48BitAddress(ether_addr, addr);
|
|
|
|
else {
|
|
|
|
addr[0] = 0x12;
|
|
|
|
addr[1] = 0x34;
|
|
|
|
addr[2] = 0x56;
|
|
|
|
addr[3] = 0x78;
|
|
|
|
addr[4] = 0x9a;
|
|
|
|
addr[5] = 0xbc;
|
|
|
|
}
|
|
|
|
D(bug("AO_get_ethernet_address: got address %02x%02x%02x%02x%02x%02x\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add multicast address
|
|
|
|
void AO_enable_multicast(uint32 addr)
|
|
|
|
{
|
|
|
|
if (net_open)
|
|
|
|
ether_do_add_multicast(Mac2HostAddr(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable multicast address
|
|
|
|
void AO_disable_multicast(uint32 addr)
|
|
|
|
{
|
|
|
|
if (net_open)
|
|
|
|
ether_do_del_multicast(Mac2HostAddr(addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transmit one packet
|
|
|
|
void AO_transmit_packet(uint32 mp)
|
|
|
|
{
|
|
|
|
if (net_open) {
|
|
|
|
switch (ether_do_write(mp)) {
|
|
|
|
case noErr:
|
|
|
|
num_tx_packets++;
|
|
|
|
break;
|
|
|
|
case excessCollsns:
|
|
|
|
num_tx_buffer_full++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy packet data from message block to linear buffer
|
|
|
|
static inline int ether_arg_to_buffer(uint32 mp, uint8 *p)
|
|
|
|
{
|
|
|
|
return ether_msgb_to_buffer(mp, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ethernet interrupt
|
|
|
|
void EtherIRQ(void)
|
|
|
|
{
|
|
|
|
D(bug("EtherIRQ\n"));
|
|
|
|
num_ether_irq++;
|
|
|
|
|
|
|
|
OTEnterInterrupt();
|
|
|
|
ether_do_interrupt();
|
|
|
|
OTLeaveInterrupt();
|
|
|
|
|
|
|
|
// Acknowledge interrupt to reception thread
|
|
|
|
D(bug(" EtherIRQ done\n"));
|
|
|
|
sem_post(&int_ack);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Add multicast address
|
|
|
|
int16 ether_add_multicast(uint32 pb)
|
|
|
|
{
|
|
|
|
return ether_do_add_multicast(Mac2HostAddr(pb + eMultiAddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable multicast address
|
|
|
|
int16 ether_del_multicast(uint32 pb)
|
|
|
|
{
|
|
|
|
return ether_do_del_multicast(Mac2HostAddr(pb + eMultiAddr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transmit one packet
|
|
|
|
int16 ether_write(uint32 wds)
|
|
|
|
{
|
|
|
|
return ether_do_write(wds);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy packet data from WDS to linear buffer
|
|
|
|
static inline int ether_arg_to_buffer(uint32 wds, uint8 *p)
|
|
|
|
{
|
|
|
|
return ether_wds_to_buffer(wds, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch packet to protocol handler
|
|
|
|
static void ether_dispatch_packet(uint32 p, uint32 length)
|
|
|
|
{
|
|
|
|
// Get packet type
|
|
|
|
uint16 type = ReadMacInt16(p + 12);
|
|
|
|
|
|
|
|
// Look for protocol
|
|
|
|
uint16 search_type = (type <= 1500 ? 0 : type);
|
|
|
|
if (net_protocols.find(search_type) == net_protocols.end())
|
|
|
|
return;
|
|
|
|
uint32 handler = net_protocols[search_type];
|
|
|
|
|
|
|
|
// No default handler
|
|
|
|
if (handler == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Copy header to RHA
|
|
|
|
Mac2Mac_memcpy(ether_data + ed_RHA, p, 14);
|
|
|
|
D(bug(" header %08x%04x %08x%04x %04x\n", ReadMacInt32(ether_data + ed_RHA), ReadMacInt16(ether_data + ed_RHA + 4), ReadMacInt32(ether_data + ed_RHA + 6), ReadMacInt16(ether_data + ed_RHA + 10), ReadMacInt16(ether_data + ed_RHA + 12)));
|
|
|
|
|
|
|
|
// Call protocol handler
|
|
|
|
M68kRegisters r;
|
|
|
|
r.d[0] = type; // Packet type
|
|
|
|
r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket)
|
|
|
|
r.a[0] = p + 14; // Pointer to packet (Mac address, for ReadPacket)
|
|
|
|
r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA
|
|
|
|
r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines
|
|
|
|
D(bug(" calling protocol handler %08x, type %08x, length %08x, data %08x, rha %08x, read_packet %08x\n", handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4]));
|
|
|
|
Execute68k(handler, &r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ethernet interrupt
|
|
|
|
void EtherInterrupt(void)
|
|
|
|
{
|
|
|
|
D(bug("EtherIRQ\n"));
|
|
|
|
ether_do_interrupt();
|
|
|
|
|
|
|
|
// Acknowledge interrupt to reception thread
|
|
|
|
D(bug(" EtherIRQ done\n"));
|
|
|
|
sem_post(&int_ack);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
/*
|
|
|
|
* Reset
|
|
|
|
*/
|
|
|
|
|
2001-07-13 15:39:25 +00:00
|
|
|
void ether_reset(void)
|
1999-10-03 14:16:26 +00:00
|
|
|
{
|
2001-07-13 15:39:25 +00:00
|
|
|
net_protocols.clear();
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add multicast address
|
|
|
|
*/
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
static int16 ether_do_add_multicast(uint8 *addr)
|
1999-10-03 14:16:26 +00:00
|
|
|
{
|
2005-05-13 09:21:21 +00:00
|
|
|
switch (net_if_type) {
|
|
|
|
case NET_IF_ETHERTAP:
|
|
|
|
case NET_IF_SHEEPNET:
|
2005-07-03 08:21:32 +00:00
|
|
|
if (ioctl(fd, SIOCADDMULTI, addr) < 0) {
|
2005-05-13 09:21:21 +00:00
|
|
|
D(bug("WARNING: Couldn't enable multicast address\n"));
|
2012-09-02 11:52:28 +00:00
|
|
|
if (net_if_type == NET_IF_ETHERTAP) {
|
2005-05-13 09:21:21 +00:00
|
|
|
return noErr;
|
2012-09-02 11:52:28 +00:00
|
|
|
} else {
|
2005-05-13 09:21:21 +00:00
|
|
|
return eMultiErr;
|
2012-09-02 11:52:28 +00:00
|
|
|
}
|
2005-05-13 09:21:21 +00:00
|
|
|
}
|
2012-09-02 11:52:28 +00:00
|
|
|
return noErr;
|
2005-05-13 09:21:21 +00:00
|
|
|
default:
|
1999-10-03 14:16:26 +00:00
|
|
|
return noErr;
|
2005-05-13 09:21:21 +00:00
|
|
|
}
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete multicast address
|
|
|
|
*/
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
static int16 ether_do_del_multicast(uint8 *addr)
|
1999-10-03 14:16:26 +00:00
|
|
|
{
|
2005-05-13 09:21:21 +00:00
|
|
|
switch (net_if_type) {
|
|
|
|
case NET_IF_ETHERTAP:
|
|
|
|
case NET_IF_SHEEPNET:
|
2005-07-03 08:21:32 +00:00
|
|
|
if (ioctl(fd, SIOCDELMULTI, addr) < 0) {
|
2005-05-13 09:21:21 +00:00
|
|
|
D(bug("WARNING: Couldn't disable multicast address\n"));
|
|
|
|
return eMultiErr;
|
|
|
|
}
|
2012-09-02 11:52:28 +00:00
|
|
|
return noErr;
|
2005-05-13 09:21:21 +00:00
|
|
|
default:
|
1999-10-03 14:16:26 +00:00
|
|
|
return noErr;
|
2005-05-13 09:21:21 +00:00
|
|
|
}
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach protocol handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
int16 ether_attach_ph(uint16 type, uint32 handler)
|
|
|
|
{
|
2001-07-13 15:39:25 +00:00
|
|
|
if (net_protocols.find(type) != net_protocols.end())
|
1999-10-03 14:16:26 +00:00
|
|
|
return lapProtErr;
|
2001-07-13 15:39:25 +00:00
|
|
|
net_protocols[type] = handler;
|
|
|
|
return noErr;
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach protocol handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
int16 ether_detach_ph(uint16 type)
|
|
|
|
{
|
2001-07-13 15:39:25 +00:00
|
|
|
if (net_protocols.erase(type) == 0)
|
1999-10-03 14:16:26 +00:00
|
|
|
return lapProtErr;
|
2001-07-13 15:39:25 +00:00
|
|
|
return noErr;
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Transmit raw ethernet packet
|
|
|
|
*/
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
static int16 ether_do_write(uint32 arg)
|
1999-10-03 14:16:26 +00:00
|
|
|
{
|
|
|
|
// Copy packet to buffer
|
|
|
|
uint8 packet[1516], *p = packet;
|
|
|
|
int len = 0;
|
2001-03-29 14:20:55 +00:00
|
|
|
#if defined(__linux__)
|
2004-05-09 16:16:05 +00:00
|
|
|
if (net_if_type == NET_IF_ETHERTAP) {
|
2001-03-29 14:20:55 +00:00
|
|
|
*p++ = 0; // Linux ethertap discards the first 2 bytes
|
1999-10-03 14:16:26 +00:00
|
|
|
*p++ = 0;
|
|
|
|
len += 2;
|
|
|
|
}
|
2001-03-29 14:20:55 +00:00
|
|
|
#endif
|
2005-07-03 08:21:32 +00:00
|
|
|
len += ether_arg_to_buffer(arg, p);
|
1999-10-03 14:16:26 +00:00
|
|
|
|
|
|
|
#if MONITOR
|
|
|
|
bug("Sending Ethernet packet:\n");
|
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
bug("%02x ", packet[i]);
|
|
|
|
}
|
|
|
|
bug("\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Transmit packet
|
2005-05-13 09:21:21 +00:00
|
|
|
#ifdef HAVE_SLIRP
|
|
|
|
if (net_if_type == NET_IF_SLIRP) {
|
2005-05-21 17:48:21 +00:00
|
|
|
const int slirp_input_fd = slirp_input_fds[1];
|
|
|
|
write(slirp_input_fd, &len, sizeof(len));
|
|
|
|
write(slirp_input_fd, packet, len);
|
2005-05-13 09:21:21 +00:00
|
|
|
return noErr;
|
2018-10-23 13:37:09 +00:00
|
|
|
} else
|
2005-05-13 09:21:21 +00:00
|
|
|
#endif
|
2018-09-28 14:14:14 +00:00
|
|
|
if (net_if_type == NET_IF_VDE) {
|
|
|
|
if (fd == -1) { // which means vde service is not running
|
|
|
|
D(bug("WARNING: Couldn't transmit VDE packet\n"));
|
|
|
|
return excessCollsns;
|
|
|
|
}
|
|
|
|
|
2018-10-20 13:07:38 +00:00
|
|
|
if (vde_conn == NULL) {
|
2018-09-28 14:14:14 +00:00
|
|
|
D(bug("WARNING: vde_conn is NULL\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2018-10-20 13:07:38 +00:00
|
|
|
len = vde_send(vde_conn, packet, sizeof(packet), 0);
|
2018-09-28 14:14:14 +00:00
|
|
|
} while (len < 0);
|
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
else if (write(fd, packet, len) < 0) {
|
1999-10-03 14:16:26 +00:00
|
|
|
D(bug("WARNING: Couldn't transmit packet\n"));
|
|
|
|
return excessCollsns;
|
|
|
|
} else
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-12 19:48:28 +00:00
|
|
|
/*
|
|
|
|
* Start UDP packet reception thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool ether_start_udp_thread(int socket_fd)
|
|
|
|
{
|
|
|
|
fd = socket_fd;
|
|
|
|
udp_tunnel = true;
|
|
|
|
return start_thread();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop UDP packet reception thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
void ether_stop_udp_thread(void)
|
|
|
|
{
|
|
|
|
stop_thread();
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-13 09:21:21 +00:00
|
|
|
/*
|
|
|
|
* SLIRP output buffer glue
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_SLIRP
|
|
|
|
int slirp_can_output(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void slirp_output(const uint8 *packet, int len)
|
|
|
|
{
|
|
|
|
write(slirp_output_fd, packet, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *slirp_receive_func(void *arg)
|
|
|
|
{
|
2005-05-21 17:48:21 +00:00
|
|
|
const int slirp_input_fd = slirp_input_fds[0];
|
|
|
|
|
2005-05-13 09:21:21 +00:00
|
|
|
for (;;) {
|
|
|
|
// Wait for packets to arrive
|
|
|
|
fd_set rfds, wfds, xfds;
|
|
|
|
int nfds;
|
|
|
|
struct timeval tv;
|
|
|
|
|
2005-05-21 17:48:21 +00:00
|
|
|
// ... in the input queue
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(slirp_input_fd, &rfds);
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (select(slirp_input_fd + 1, &rfds, NULL, NULL, &tv) > 0) {
|
|
|
|
int len;
|
|
|
|
read(slirp_input_fd, &len, sizeof(len));
|
|
|
|
uint8 packet[1516];
|
|
|
|
assert(len <= sizeof(packet));
|
|
|
|
read(slirp_input_fd, packet, len);
|
|
|
|
slirp_input(packet, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... in the output queue
|
2005-05-13 09:21:21 +00:00
|
|
|
nfds = -1;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_ZERO(&xfds);
|
2006-04-02 21:06:50 +00:00
|
|
|
int timeout = slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
|
|
|
|
#if ! USE_SLIRP_TIMEOUT
|
|
|
|
timeout = 10000;
|
|
|
|
#endif
|
2005-05-13 09:21:21 +00:00
|
|
|
tv.tv_sec = 0;
|
2006-04-02 21:06:50 +00:00
|
|
|
tv.tv_usec = timeout;
|
2005-05-13 09:21:21 +00:00
|
|
|
if (select(nfds + 1, &rfds, &wfds, &xfds, &tv) >= 0)
|
|
|
|
slirp_select_poll(&rfds, &wfds, &xfds);
|
2005-05-15 17:22:12 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_PTHREAD_TESTCANCEL
|
|
|
|
// Explicit cancellation point if select() was not covered
|
|
|
|
// This seems to be the case on MacOS X 10.2
|
|
|
|
pthread_testcancel();
|
|
|
|
#endif
|
2005-05-13 09:21:21 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int slirp_can_output(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void slirp_output(const uint8 *packet, int len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-10-03 14:16:26 +00:00
|
|
|
/*
|
|
|
|
* Packet reception thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void *receive_func(void *arg)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
// Wait for packets to arrive
|
2006-01-21 16:19:47 +00:00
|
|
|
#if USE_POLL
|
2005-05-15 17:22:12 +00:00
|
|
|
struct pollfd pf = {fd, POLLIN, 0};
|
|
|
|
int res = poll(&pf, 1, -1);
|
|
|
|
#else
|
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(fd, &rfds);
|
|
|
|
// A NULL timeout could cause select() to block indefinitely,
|
|
|
|
// even if it is supposed to be a cancellation point [MacOS X]
|
|
|
|
struct timeval tv = { 0, 20000 };
|
|
|
|
int res = select(fd + 1, &rfds, NULL, NULL, &tv);
|
|
|
|
#ifdef HAVE_PTHREAD_TESTCANCEL
|
|
|
|
pthread_testcancel();
|
|
|
|
#endif
|
|
|
|
if (res == 0 || (res == -1 && errno == EINTR))
|
|
|
|
continue;
|
|
|
|
#endif
|
1999-10-03 14:16:26 +00:00
|
|
|
if (res <= 0)
|
|
|
|
break;
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
if (ether_driver_opened) {
|
|
|
|
// Trigger Ethernet interrupt
|
|
|
|
D(bug(" packet received, triggering Ethernet interrupt\n"));
|
|
|
|
SetInterruptFlag(INTFLAG_ETHER);
|
|
|
|
TriggerInterrupt();
|
1999-10-03 14:16:26 +00:00
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
// Wait for interrupt acknowledge by EtherInterrupt()
|
|
|
|
sem_wait(&int_ack);
|
|
|
|
} else
|
2006-04-02 21:06:50 +00:00
|
|
|
Delay_usec(20000);
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers
|
|
|
|
*/
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
void ether_do_interrupt(void)
|
1999-10-03 14:16:26 +00:00
|
|
|
{
|
|
|
|
// Call protocol handler for received packets
|
2005-03-19 17:43:03 +00:00
|
|
|
EthernetPacket ether_packet;
|
|
|
|
uint32 packet = ether_packet.addr();
|
2001-07-12 19:48:28 +00:00
|
|
|
ssize_t length;
|
1999-10-03 14:16:26 +00:00
|
|
|
for (;;) {
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
#ifndef SHEEPSHAVER
|
2001-07-12 19:48:28 +00:00
|
|
|
if (udp_tunnel) {
|
|
|
|
|
|
|
|
// Read packet from socket
|
|
|
|
struct sockaddr_in from;
|
|
|
|
socklen_t from_len = sizeof(from);
|
2005-03-19 17:43:03 +00:00
|
|
|
length = recvfrom(fd, Mac2HostAddr(packet), 1514, 0, (struct sockaddr *)&from, &from_len);
|
2001-07-12 19:48:28 +00:00
|
|
|
if (length < 14)
|
|
|
|
break;
|
|
|
|
ether_udp_read(packet, length, &from);
|
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2018-09-28 14:14:14 +00:00
|
|
|
if (net_if_type == NET_IF_VDE) {
|
|
|
|
length = vde_recv(vde_conn, Mac2HostAddr(packet), 1514, 0);
|
|
|
|
} else {
|
2018-10-28 18:42:04 +00:00
|
|
|
// Read packet from sheep_net device
|
2001-03-29 14:20:55 +00:00
|
|
|
#if defined(__linux__)
|
2018-10-28 18:40:08 +00:00
|
|
|
length = read(fd, Mac2HostAddr(packet), net_if_type == NET_IF_ETHERTAP ? 1516 : 1514);
|
2001-03-29 14:20:55 +00:00
|
|
|
#else
|
2018-10-28 18:40:08 +00:00
|
|
|
length = read(fd, Mac2HostAddr(packet), 1514);
|
2001-03-29 14:20:55 +00:00
|
|
|
#endif
|
2018-10-28 18:40:08 +00:00
|
|
|
if (length < 14)
|
|
|
|
break;
|
2018-09-28 14:14:14 +00:00
|
|
|
}
|
1999-10-03 14:16:26 +00:00
|
|
|
|
|
|
|
#if MONITOR
|
2001-07-12 19:48:28 +00:00
|
|
|
bug("Receiving Ethernet packet:\n");
|
|
|
|
for (int i=0; i<length; i++) {
|
2005-03-19 17:43:03 +00:00
|
|
|
bug("%02x ", ReadMacInt8(packet + i));
|
2001-07-12 19:48:28 +00:00
|
|
|
}
|
|
|
|
bug("\n");
|
1999-10-03 14:16:26 +00:00
|
|
|
#endif
|
|
|
|
|
2001-07-12 19:48:28 +00:00
|
|
|
// Pointer to packet data (Ethernet header)
|
2005-03-19 17:43:03 +00:00
|
|
|
uint32 p = packet;
|
2001-03-29 14:20:55 +00:00
|
|
|
#if defined(__linux__)
|
2004-05-09 16:16:05 +00:00
|
|
|
if (net_if_type == NET_IF_ETHERTAP) {
|
2001-07-12 19:48:28 +00:00
|
|
|
p += 2; // Linux ethertap has two random bytes before the packet
|
|
|
|
length -= 2;
|
|
|
|
}
|
2001-03-29 14:20:55 +00:00
|
|
|
#endif
|
1999-10-03 14:16:26 +00:00
|
|
|
|
2005-07-03 08:21:32 +00:00
|
|
|
// Dispatch packet
|
|
|
|
ether_dispatch_packet(p, length);
|
2001-07-12 19:48:28 +00:00
|
|
|
}
|
1999-10-03 14:16:26 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-30 05:32:38 +00:00
|
|
|
|
|
|
|
// Helper function for port forwarding
|
|
|
|
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
|
|
|
|
{
|
|
|
|
const char *p, *p1;
|
|
|
|
int len;
|
|
|
|
p = *pp;
|
|
|
|
p1 = strchr(p, sep);
|
|
|
|
if (!p1)
|
|
|
|
return -1;
|
|
|
|
len = p1 - p;
|
|
|
|
p1++;
|
|
|
|
if (buf_size > 0) {
|
|
|
|
if (len > buf_size - 1)
|
|
|
|
len = buf_size - 1;
|
|
|
|
memcpy(buf, p, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
}
|
|
|
|
*pp = p1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up port forwarding for slirp
|
|
|
|
static void slirp_add_redirs()
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
const char *str;
|
|
|
|
while ((str = PrefsFindString("redir", index++)) != NULL) {
|
|
|
|
slirp_add_redir(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a port forward/redirection for slirp
|
|
|
|
static int slirp_add_redir(const char *redir_str)
|
|
|
|
{
|
|
|
|
// code adapted from qemu source
|
2012-10-30 13:34:10 +00:00
|
|
|
struct in_addr guest_addr = {0};
|
2012-10-30 05:32:38 +00:00
|
|
|
int host_port, guest_port;
|
|
|
|
const char *p;
|
|
|
|
char buf[256];
|
|
|
|
int is_udp;
|
|
|
|
char *end;
|
|
|
|
char str[256];
|
|
|
|
|
|
|
|
p = redir_str;
|
|
|
|
if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
if (!strcmp(buf, "tcp") || buf[0] == '\0') {
|
|
|
|
is_udp = 0;
|
|
|
|
} else if (!strcmp(buf, "udp")) {
|
|
|
|
is_udp = 1;
|
|
|
|
} else {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
host_port = strtol(buf, &end, 0);
|
|
|
|
if (*end != '\0' || host_port < 1 || host_port > 65535) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
// 0.0.0.0 doesn't seem to work, so default to a client address
|
|
|
|
// if none is specified
|
|
|
|
if (buf[0] == '\0' ?
|
|
|
|
!inet_aton(CTL_LOCAL, &guest_addr) :
|
|
|
|
!inet_aton(buf, &guest_addr)) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
|
|
|
|
guest_port = strtol(p, &end, 0);
|
|
|
|
if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
|
|
|
|
sprintf(str, "could not set up host forwarding rule '%s'", redir_str);
|
|
|
|
WarningAlert(str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_syntax:
|
|
|
|
sprintf(str, "invalid host forwarding rule '%s'", redir_str);
|
|
|
|
WarningAlert(str);
|
|
|
|
return -1;
|
|
|
|
}
|