2017-05-09 22:09:55 -04:00
|
|
|
/*
|
|
|
|
|
* 86Box A hypervisor and IBM PC system emulator that specializes in
|
|
|
|
|
* running old operating systems and software designed for IBM
|
|
|
|
|
* PC systems and compatibles from 1981 through fairly recent
|
|
|
|
|
* system designs based on the PCI bus.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the 86Box distribution.
|
|
|
|
|
*
|
|
|
|
|
* Implementation of the network module.
|
|
|
|
|
*
|
2017-05-12 05:05:20 -04:00
|
|
|
* NOTE The definition of the netcard_t is currently not optimal;
|
|
|
|
|
* it should be malloc'ed and then linked to the NETCARD def.
|
|
|
|
|
* Will be done later.
|
|
|
|
|
*
|
2017-10-17 01:59:09 -04:00
|
|
|
* Version: @(#)network.c 1.0.15 2017/10/16
|
2017-05-09 22:09:55 -04:00
|
|
|
*
|
2017-06-04 02:11:19 -04:00
|
|
|
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
|
2017-10-10 03:07:29 -04:00
|
|
|
*
|
|
|
|
|
* Copyright 2017 Fred N. van Kempen.
|
2017-05-09 22:09:55 -04:00
|
|
|
*/
|
2017-05-06 17:48:33 +02:00
|
|
|
#include <stdio.h>
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdint.h>
|
2017-05-06 17:48:33 +02:00
|
|
|
#include <string.h>
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <wchar.h>
|
2017-10-17 01:59:09 -04:00
|
|
|
#include "../86box.h"
|
2017-06-14 07:21:01 +02:00
|
|
|
#include "../ibm.h"
|
|
|
|
|
#include "../device.h"
|
2017-10-16 21:19:51 +02:00
|
|
|
#include "../plat.h"
|
2017-10-10 03:07:29 -04:00
|
|
|
#include "../ui.h"
|
2017-05-09 22:09:55 -04:00
|
|
|
#include "network.h"
|
2017-05-06 17:48:33 +02:00
|
|
|
#include "net_ne2000.h"
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
static netcard_t net_cards[] = {
|
|
|
|
|
{ "None", "none", NULL,
|
|
|
|
|
NULL, NULL },
|
2017-10-10 03:07:29 -04:00
|
|
|
#if defined(DEV_BRANCH) && defined(USE_NE1000)
|
|
|
|
|
{ "Novell NE1000", "ne1k", &ne1000_device,
|
2017-05-12 05:05:20 -04:00
|
|
|
NULL, NULL },
|
2017-10-10 00:14:15 +02:00
|
|
|
#endif
|
2017-10-10 03:07:29 -04:00
|
|
|
{ "Novell NE2000", "ne2k", &ne2000_device,
|
2017-05-12 05:05:20 -04:00
|
|
|
NULL, NULL },
|
2017-10-10 03:07:29 -04:00
|
|
|
{ "Realtek RTL8029AS", "ne2kpci", &rtl8029as_device,
|
2017-05-12 05:05:20 -04:00
|
|
|
NULL, NULL },
|
|
|
|
|
{ "", "", NULL,
|
|
|
|
|
NULL, NULL }
|
|
|
|
|
};
|
2017-05-06 17:48:33 +02:00
|
|
|
|
|
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
/* Global variables. */
|
|
|
|
|
int network_type;
|
|
|
|
|
int network_ndev;
|
2017-10-07 00:46:54 -04:00
|
|
|
int network_card;
|
2017-05-18 01:57:16 -04:00
|
|
|
netdev_t network_devs[32];
|
|
|
|
|
char network_pcap[512];
|
2017-10-07 00:46:54 -04:00
|
|
|
int nic_do_log;
|
2017-10-17 05:15:53 +02:00
|
|
|
static volatile
|
|
|
|
|
mutex_t *netMutex;
|
2017-10-16 21:19:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
static struct
|
|
|
|
|
{
|
2017-10-17 05:15:53 +02:00
|
|
|
volatile int
|
|
|
|
|
busy,
|
|
|
|
|
queue_in_use;
|
|
|
|
|
|
|
|
|
|
volatile event_t
|
|
|
|
|
*wake_poll_thread,
|
|
|
|
|
*poll_complete,
|
|
|
|
|
*queue_not_in_use;
|
2017-10-16 21:19:51 +02:00
|
|
|
} poll_data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-17 05:15:53 +02:00
|
|
|
network_mutex_wait(uint8_t wait)
|
2017-10-16 21:19:51 +02:00
|
|
|
{
|
2017-10-17 05:15:53 +02:00
|
|
|
if (wait)
|
|
|
|
|
thread_wait_mutex((mutex_t *) netMutex);
|
|
|
|
|
else
|
|
|
|
|
thread_release_mutex((mutex_t *) netMutex);
|
2017-10-16 21:19:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
network_wait_for_poll()
|
|
|
|
|
{
|
|
|
|
|
while (poll_data.busy)
|
2017-10-17 05:15:53 +02:00
|
|
|
thread_wait_event((event_t *) poll_data.poll_complete, -1);
|
|
|
|
|
thread_reset_event((event_t *) poll_data.poll_complete);
|
2017-10-16 21:19:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-17 05:15:53 +02:00
|
|
|
network_thread_init(void)
|
2017-10-16 21:19:51 +02:00
|
|
|
{
|
|
|
|
|
poll_data.wake_poll_thread = thread_create_event();
|
|
|
|
|
poll_data.poll_complete = thread_create_event();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-17 05:15:53 +02:00
|
|
|
network_busy(uint8_t set)
|
2017-10-16 21:19:51 +02:00
|
|
|
{
|
2017-10-17 05:15:53 +02:00
|
|
|
poll_data.busy = !!set;
|
|
|
|
|
if (!set)
|
|
|
|
|
thread_set_event((event_t *) poll_data.poll_complete);
|
2017-10-16 21:19:51 +02:00
|
|
|
}
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Initialize the configured network cards.
|
|
|
|
|
*
|
|
|
|
|
* This function gets called only once, from the System
|
|
|
|
|
* Platform initialization code (currently in pc.c) to
|
|
|
|
|
* set our local stuff to a known state.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
network_init(void)
|
|
|
|
|
{
|
2017-05-22 00:21:22 -04:00
|
|
|
int i;
|
|
|
|
|
|
2017-06-04 02:11:19 -04:00
|
|
|
#if ENABLE_NIC_LOG
|
|
|
|
|
nic_do_log = ENABLE_NIC_LOG;
|
|
|
|
|
#else
|
|
|
|
|
nic_do_log = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-10-07 00:46:54 -04:00
|
|
|
/* Initialize to a known state. */
|
2017-05-18 01:57:16 -04:00
|
|
|
network_type = NET_TYPE_NONE;
|
2017-05-12 05:05:20 -04:00
|
|
|
network_card = 0;
|
2017-05-18 01:57:16 -04:00
|
|
|
|
2017-05-22 00:21:22 -04:00
|
|
|
/* Create a first device entry that's always there, as needed by UI. */
|
|
|
|
|
strcpy(network_devs[0].device, "none");
|
|
|
|
|
strcpy(network_devs[0].description, "None");
|
|
|
|
|
network_ndev = 1;
|
|
|
|
|
|
|
|
|
|
/* Initialize the Pcap system module, if present. */
|
|
|
|
|
i = network_pcap_init(&network_devs[network_ndev]);
|
|
|
|
|
if (i > 0)
|
|
|
|
|
network_ndev += i;
|
2017-05-29 01:18:32 +02:00
|
|
|
|
2017-05-29 21:57:31 -04:00
|
|
|
if (network_type != NET_TYPE_PCAP)
|
|
|
|
|
network_pcap_close();
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
2017-05-06 17:48:33 +02:00
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/*
|
|
|
|
|
* Attach a network card to the system.
|
|
|
|
|
*
|
|
|
|
|
* This function is called by a hardware driver ("card") after it has
|
|
|
|
|
* finished initializing itself, to link itself to the platform support
|
|
|
|
|
* modules.
|
|
|
|
|
*/
|
|
|
|
|
int
|
|
|
|
|
network_attach(void *dev, uint8_t *mac, NETRXCB rx)
|
2017-05-06 17:48:33 +02:00
|
|
|
{
|
2017-05-12 05:05:20 -04:00
|
|
|
int ret = -1;
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2017-05-22 00:21:22 -04:00
|
|
|
if (network_card == 0) return(ret);
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Save the card's callback info. */
|
2017-10-16 04:54:41 -04:00
|
|
|
net_cards[network_card].priv = dev;
|
2017-05-12 05:05:20 -04:00
|
|
|
net_cards[network_card].rx = rx;
|
|
|
|
|
|
2017-10-17 05:15:53 +02:00
|
|
|
netMutex = thread_create_mutex(L"86Box.NetMutex");
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Start the platform module. */
|
|
|
|
|
switch(network_type) {
|
2017-05-18 01:57:16 -04:00
|
|
|
case NET_TYPE_PCAP:
|
2017-05-12 05:05:20 -04:00
|
|
|
ret = network_pcap_setup(mac, rx, dev);
|
2017-05-22 00:21:22 -04:00
|
|
|
if (ret < 0) {
|
2017-10-10 03:07:29 -04:00
|
|
|
ui_msgbox(MBX_ERROR, (wchar_t *)IDS_2139);
|
2017-05-22 00:21:22 -04:00
|
|
|
network_type = NET_TYPE_NONE;
|
|
|
|
|
}
|
2017-05-12 05:05:20 -04:00
|
|
|
break;
|
|
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
case NET_TYPE_SLIRP:
|
2017-05-12 05:05:20 -04:00
|
|
|
ret = network_slirp_setup(mac, rx, dev);
|
|
|
|
|
break;
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
return(ret);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Stop any network activity. */
|
2017-05-06 17:48:33 +02:00
|
|
|
void
|
2017-05-12 05:05:20 -04:00
|
|
|
network_close(void)
|
2017-05-06 17:48:33 +02:00
|
|
|
{
|
2017-05-12 05:05:20 -04:00
|
|
|
switch(network_type) {
|
2017-05-18 01:57:16 -04:00
|
|
|
case NET_TYPE_PCAP:
|
2017-05-12 05:05:20 -04:00
|
|
|
network_pcap_close();
|
|
|
|
|
break;
|
|
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
case NET_TYPE_SLIRP:
|
2017-05-12 05:05:20 -04:00
|
|
|
network_slirp_close();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-10-17 05:15:53 +02:00
|
|
|
|
|
|
|
|
thread_close_mutex((event_t *) netMutex);
|
2017-08-22 02:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Test the network. */
|
|
|
|
|
int
|
|
|
|
|
network_test(void)
|
|
|
|
|
{
|
|
|
|
|
switch(network_type) {
|
|
|
|
|
case NET_TYPE_PCAP:
|
|
|
|
|
return network_pcap_test();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NET_TYPE_SLIRP:
|
|
|
|
|
return network_slirp_test();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-08-22 02:16:15 +02:00
|
|
|
return 0;
|
2017-05-09 22:09:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/*
|
|
|
|
|
* Reset the network card(s).
|
|
|
|
|
*
|
|
|
|
|
* This function is called each time the system is reset,
|
|
|
|
|
* either a hard reset (including power-up) or a soft reset
|
|
|
|
|
* including C-A-D reset.) It is responsible for connecting
|
|
|
|
|
* everything together.
|
|
|
|
|
*/
|
2017-05-09 22:09:55 -04:00
|
|
|
void
|
|
|
|
|
network_reset(void)
|
|
|
|
|
{
|
2017-05-22 00:21:22 -04:00
|
|
|
pclog("NETWORK: reset (type=%d, card=%d)\n", network_type, network_card);
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
/* Just in case.. */
|
|
|
|
|
network_close();
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* If no active card, we're done. */
|
2017-05-18 01:57:16 -04:00
|
|
|
if ((network_type==NET_TYPE_NONE) || (network_card==0)) return;
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2017-05-29 01:18:32 +02:00
|
|
|
if (network_type==NET_TYPE_PCAP) network_pcap_reset();
|
2017-05-27 03:53:32 +02:00
|
|
|
|
2017-05-22 00:21:22 -04:00
|
|
|
pclog("NETWORK: set up for %s, card='%s'\n",
|
2017-05-18 01:57:16 -04:00
|
|
|
(network_type==NET_TYPE_SLIRP)?"SLiRP":"WinPcap",
|
|
|
|
|
net_cards[network_card].name);
|
2017-05-12 17:33:28 -04:00
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Add the (new?) card to the I/O system. */
|
|
|
|
|
if (net_cards[network_card].device) {
|
2017-05-09 22:09:55 -04:00
|
|
|
pclog("NETWORK: adding device '%s'\n",
|
2017-05-12 05:05:20 -04:00
|
|
|
net_cards[network_card].name);
|
|
|
|
|
device_add(net_cards[network_card].device);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Transmit a packet to one of the network providers. */
|
2017-05-06 17:48:33 +02:00
|
|
|
void
|
2017-05-12 05:05:20 -04:00
|
|
|
network_tx(uint8_t *bufp, int len)
|
2017-05-06 17:48:33 +02:00
|
|
|
{
|
2017-05-12 05:05:20 -04:00
|
|
|
switch(network_type) {
|
2017-05-18 01:57:16 -04:00
|
|
|
case NET_TYPE_PCAP:
|
2017-05-12 05:05:20 -04:00
|
|
|
network_pcap_in(bufp, len);
|
|
|
|
|
break;
|
|
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
case NET_TYPE_SLIRP:
|
2017-05-12 05:05:20 -04:00
|
|
|
network_slirp_in(bufp, len);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
int
|
|
|
|
|
network_card_available(int card)
|
|
|
|
|
{
|
|
|
|
|
if (net_cards[card].device)
|
|
|
|
|
return(device_available(net_cards[card].device));
|
|
|
|
|
|
|
|
|
|
return(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
char *
|
|
|
|
|
network_card_getname(int card)
|
|
|
|
|
{
|
|
|
|
|
return(net_cards[card].name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
device_t *
|
|
|
|
|
network_card_getdevice(int card)
|
|
|
|
|
{
|
|
|
|
|
return(net_cards[card].device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
int
|
|
|
|
|
network_card_has_config(int card)
|
|
|
|
|
{
|
|
|
|
|
if (! net_cards[card].device) return(0);
|
|
|
|
|
|
|
|
|
|
return(net_cards[card].device->config ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
char *
|
|
|
|
|
network_card_get_internal_name(int card)
|
|
|
|
|
{
|
|
|
|
|
return(net_cards[card].internal_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
int
|
|
|
|
|
network_card_get_from_internal_name(char *s)
|
|
|
|
|
{
|
|
|
|
|
int c = 0;
|
|
|
|
|
|
|
|
|
|
while (strlen(net_cards[c].internal_name)) {
|
|
|
|
|
if (! strcmp(net_cards[c].internal_name, s))
|
|
|
|
|
return(c);
|
|
|
|
|
c++;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 21:56:31 +02:00
|
|
|
return(-1);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
2017-05-18 01:57:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
network_dev_to_id(char *dev)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
for (i=0; i<network_ndev; i++) {
|
|
|
|
|
if (! strcmp(network_devs[i].device, dev)) {
|
|
|
|
|
return(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If no match found, assume "none". */
|
|
|
|
|
return(0);
|
|
|
|
|
}
|