2004-08-27 12:23:50 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2004-08-27 12:23:50 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2004-01-16 18:53:44 +03:00
|
|
|
//
|
2011-01-24 23:35:51 +03:00
|
|
|
// Copyright (C) 2011 The Bochs Project
|
2009-02-08 12:05:52 +03:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
2004-01-16 18:53:44 +03:00
|
|
|
// virtual Ethernet locator
|
|
|
|
//
|
2005-06-12 12:59:32 +04:00
|
|
|
// An implementation of ARP, ping(ICMP-echo), DHCP and read/write TFTP.
|
2004-01-16 18:53:44 +03:00
|
|
|
// Virtual host acts as a DHCP server for guest.
|
|
|
|
// There are no connections between the virtual host and real ethernets.
|
|
|
|
//
|
2004-09-11 15:26:41 +04:00
|
|
|
// Virtual host name: vnet
|
2004-01-16 18:53:44 +03:00
|
|
|
// Virtual host IP: 192.168.10.1
|
|
|
|
// Guest IP: 192.168.10.2
|
|
|
|
// Guest netmask: 255.255.255.0
|
|
|
|
// Guest broadcast: 192.168.10.255
|
2005-06-12 12:59:32 +04:00
|
|
|
// TFTP server uses ethdev value for the root directory and doesn't overwrite files
|
2004-01-16 18:53:44 +03:00
|
|
|
|
|
|
|
#define BX_PLUGGABLE
|
|
|
|
|
2004-06-19 19:20:15 +04:00
|
|
|
#include "iodev.h"
|
2005-12-10 21:37:35 +03:00
|
|
|
|
2004-10-04 00:06:12 +04:00
|
|
|
#if BX_NETWORKING
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2004-09-05 14:30:19 +04:00
|
|
|
#include "eth.h"
|
|
|
|
|
2009-04-13 17:33:11 +04:00
|
|
|
#define LOG_THIS netdev->
|
2004-01-16 18:53:44 +03:00
|
|
|
|
|
|
|
#define BX_ETH_VNET_LOGGING 1
|
2007-07-01 11:28:14 +04:00
|
|
|
#define BX_ETH_VNET_PCAP_LOGGING 0
|
|
|
|
|
|
|
|
#if BX_ETH_VNET_PCAP_LOGGING
|
|
|
|
#include <pcap.h>
|
|
|
|
#endif
|
2004-01-16 18:53:44 +03:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// handler to send/receive packets
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static const Bit8u default_host_ipv4addr[4] = {192,168,10,1};
|
|
|
|
static const Bit8u subnetmask_ipv4addr[4] = {0xff,0xff,0xff,0x00};
|
|
|
|
static const Bit8u default_guest_ipv4addr[4] = {192,168,10,2};
|
|
|
|
static const Bit8u broadcast_ipv4addr[3][4] =
|
|
|
|
{
|
|
|
|
{ 0, 0, 0, 0},
|
|
|
|
{255,255,255,255},
|
|
|
|
{192,168, 10,255},
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ICMP_ECHO_PACKET_MAX 128
|
|
|
|
#define LAYER4_LISTEN_MAX 128
|
2004-08-27 12:23:50 +04:00
|
|
|
|
2004-10-07 21:38:03 +04:00
|
|
|
static Bit8u packet_buffer[BX_PACKET_BUFSIZE];
|
2004-08-27 12:23:50 +04:00
|
|
|
static unsigned packet_len;
|
|
|
|
|
2004-01-16 18:53:44 +03:00
|
|
|
typedef void (*layer4_handler_t)(
|
|
|
|
void *this_ptr,
|
|
|
|
const Bit8u *ipheader,
|
|
|
|
unsigned ipheader_len,
|
|
|
|
unsigned sourceport,
|
|
|
|
unsigned targetport,
|
|
|
|
const Bit8u *data,
|
|
|
|
unsigned data_len
|
|
|
|
);
|
|
|
|
|
|
|
|
#define INET_PORT_FTPDATA 20
|
|
|
|
#define INET_PORT_FTP 21
|
|
|
|
#define INET_PORT_TIME 37
|
|
|
|
#define INET_PORT_NAME 42
|
|
|
|
#define INET_PORT_DOMAIN 53
|
|
|
|
#define INET_PORT_BOOTP_SERVER 67
|
|
|
|
#define INET_PORT_BOOTP_CLIENT 68
|
|
|
|
#define INET_PORT_HTTP 80
|
|
|
|
#define INET_PORT_NTP 123
|
|
|
|
|
2005-05-13 22:10:58 +04:00
|
|
|
// TFTP server support by EaseWay <easeway@123.com>
|
|
|
|
|
|
|
|
#define INET_PORT_TFTP_SERVER 69
|
|
|
|
|
|
|
|
#define TFTP_RRQ 1
|
|
|
|
#define TFTP_WRQ 2
|
|
|
|
#define TFTP_DATA 3
|
|
|
|
#define TFTP_ACK 4
|
|
|
|
#define TFTP_ERROR 5
|
2007-07-01 11:28:14 +04:00
|
|
|
#define TFTP_OPTACK 6
|
2005-05-13 22:10:58 +04:00
|
|
|
|
2005-05-21 23:33:25 +04:00
|
|
|
#define TFTP_BUFFER_SIZE 512
|
2005-05-13 22:10:58 +04:00
|
|
|
|
2004-01-16 18:53:44 +03:00
|
|
|
class bx_vnet_pktmover_c : public eth_pktmover_c {
|
|
|
|
public:
|
|
|
|
bx_vnet_pktmover_c();
|
|
|
|
void pktmover_init(
|
|
|
|
const char *netif, const char *macaddr,
|
2009-04-13 17:33:11 +04:00
|
|
|
eth_rx_handler_t rxh, bx_devmodel_c *dev, const char *script);
|
2004-01-16 18:53:44 +03:00
|
|
|
void sendpkt(void *buf, unsigned io_len);
|
|
|
|
private:
|
|
|
|
void guest_to_host(const Bit8u *buf, unsigned io_len);
|
|
|
|
void host_to_guest(Bit8u *buf, unsigned io_len);
|
|
|
|
void process_arp(const Bit8u *buf, unsigned io_len);
|
|
|
|
void host_to_guest_arp(Bit8u *buf, unsigned io_len);
|
|
|
|
void process_ipv4(const Bit8u *buf, unsigned io_len);
|
|
|
|
void host_to_guest_ipv4(Bit8u *buf, unsigned io_len);
|
|
|
|
|
|
|
|
layer4_handler_t get_layer4_handler(
|
|
|
|
unsigned ipprotocol, unsigned port);
|
|
|
|
bx_bool register_layer4_handler(
|
|
|
|
unsigned ipprotocol, unsigned port,layer4_handler_t func);
|
|
|
|
bx_bool unregister_layer4_handler(
|
|
|
|
unsigned ipprotocol, unsigned port);
|
|
|
|
|
|
|
|
void process_icmpipv4(
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
const Bit8u *l4pkt, unsigned l4pkt_len);
|
|
|
|
void process_tcpipv4(
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
const Bit8u *l4pkt, unsigned l4pkt_len);
|
|
|
|
void process_udpipv4(
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
const Bit8u *l4pkt, unsigned l4pkt_len);
|
|
|
|
void host_to_guest_udpipv4_packet(
|
|
|
|
unsigned target_port, unsigned source_port,
|
|
|
|
const Bit8u *udpdata, unsigned udpdata_len);
|
|
|
|
|
|
|
|
void process_icmpipv4_echo(
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
const Bit8u *l4pkt, unsigned l4pkt_len);
|
|
|
|
|
|
|
|
static void udpipv4_dhcp_handler(
|
|
|
|
void *this_ptr,
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
const Bit8u *data, unsigned data_len);
|
|
|
|
void udpipv4_dhcp_handler_ns(
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
const Bit8u *data, unsigned data_len);
|
2005-05-13 22:10:58 +04:00
|
|
|
static void udpipv4_tftp_handler(
|
|
|
|
void *this_ptr,
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
const Bit8u *data, unsigned data_len);
|
|
|
|
void udpipv4_tftp_handler_ns(
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
const Bit8u *data, unsigned data_len);
|
|
|
|
void tftp_send_error(
|
2005-06-12 12:59:32 +04:00
|
|
|
Bit8u *buffer,
|
2005-05-13 22:10:58 +04:00
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
unsigned code, const char *msg);
|
|
|
|
void tftp_send_data(
|
2005-06-12 12:59:32 +04:00
|
|
|
Bit8u *buffer,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
2008-01-27 01:24:03 +03:00
|
|
|
unsigned block_nr);
|
2005-06-12 12:59:32 +04:00
|
|
|
void tftp_send_ack(
|
|
|
|
Bit8u *buffer,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
unsigned block_nr);
|
2007-07-01 11:28:14 +04:00
|
|
|
void tftp_send_optack(
|
|
|
|
Bit8u *buffer,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
2007-09-04 11:45:31 +04:00
|
|
|
size_t tsize_option, unsigned blksize_option);
|
2007-07-01 11:28:14 +04:00
|
|
|
|
2005-06-09 01:11:54 +04:00
|
|
|
char tftp_filename[BX_PATHNAME_LEN];
|
|
|
|
char tftp_rootdir[BX_PATHNAME_LEN];
|
2005-06-12 12:59:32 +04:00
|
|
|
bx_bool tftp_write;
|
2005-05-21 23:33:25 +04:00
|
|
|
Bit16u tftp_tid;
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2011-03-01 23:47:12 +03:00
|
|
|
dhcp_cfg_t dhcp;
|
2004-01-16 18:53:44 +03:00
|
|
|
|
|
|
|
struct {
|
|
|
|
unsigned ipprotocol;
|
|
|
|
unsigned port;
|
|
|
|
layer4_handler_t func;
|
|
|
|
} l4data[LAYER4_LISTEN_MAX];
|
|
|
|
unsigned l4data_used;
|
|
|
|
|
2004-08-27 12:23:50 +04:00
|
|
|
static void rx_timer_handler(void *);
|
|
|
|
void rx_timer(void);
|
|
|
|
int rx_timer_index;
|
2004-09-11 15:26:41 +04:00
|
|
|
unsigned tx_time;
|
2004-08-27 12:23:50 +04:00
|
|
|
|
2004-01-16 18:53:44 +03:00
|
|
|
#if BX_ETH_VNET_LOGGING
|
|
|
|
FILE *pktlog_txt;
|
|
|
|
#endif // BX_ETH_VNET_LOGGING
|
2007-07-01 11:28:14 +04:00
|
|
|
#if BX_ETH_VNET_PCAP_LOGGING
|
|
|
|
pcap_t *pcapp;
|
|
|
|
pcap_dumper_t *pktlog_pcap;
|
|
|
|
struct pcap_pkthdr pcaphdr;
|
|
|
|
#endif // BX_ETH_VNET_PCAP_LOGGING
|
2004-01-16 18:53:44 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class bx_vnet_locator_c : public eth_locator_c {
|
|
|
|
public:
|
|
|
|
bx_vnet_locator_c(void) : eth_locator_c("vnet") {}
|
|
|
|
protected:
|
|
|
|
eth_pktmover_c *allocate(
|
|
|
|
const char *netif, const char *macaddr,
|
|
|
|
eth_rx_handler_t rxh,
|
2009-04-13 17:33:11 +04:00
|
|
|
bx_devmodel_c *dev, const char *script) {
|
2004-01-16 18:53:44 +03:00
|
|
|
bx_vnet_pktmover_c *pktmover;
|
|
|
|
pktmover = new bx_vnet_pktmover_c();
|
2009-04-13 17:33:11 +04:00
|
|
|
pktmover->pktmover_init(netif, macaddr, rxh, dev, script);
|
2004-01-16 18:53:44 +03:00
|
|
|
return pktmover;
|
|
|
|
}
|
|
|
|
} bx_vnet_match;
|
|
|
|
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
// duplicate the part of tftp_send_data() that constructs the filename
|
|
|
|
// but ignore errors since tftp_send_data() will respond for us
|
2009-04-13 17:33:11 +04:00
|
|
|
static size_t get_file_size(bx_devmodel_c *netdev, const char *tpath, const char *tname)
|
2007-07-01 11:28:14 +04:00
|
|
|
{
|
|
|
|
struct stat stbuf;
|
|
|
|
char path[BX_PATHNAME_LEN];
|
|
|
|
|
|
|
|
if (strlen(tname) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((strlen(tpath) + strlen(tname)) > BX_PATHNAME_LEN)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sprintf(path, "%s/%s", tpath, tname);
|
|
|
|
if (stat(path, &stbuf) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BX_INFO(("tftp filesize: %lu", (unsigned long)stbuf.st_size));
|
2009-04-13 17:33:11 +04:00
|
|
|
return (size_t)stbuf.st_size;
|
2007-07-01 11:28:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-16 18:53:44 +03:00
|
|
|
bx_vnet_pktmover_c::bx_vnet_pktmover_c()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::pktmover_init(
|
2004-01-16 18:53:44 +03:00
|
|
|
const char *netif, const char *macaddr,
|
2009-04-13 17:33:11 +04:00
|
|
|
eth_rx_handler_t rxh, bx_devmodel_c *dev, const char *script)
|
2004-01-16 18:53:44 +03:00
|
|
|
{
|
2009-04-13 17:33:11 +04:00
|
|
|
this->netdev = dev;
|
|
|
|
BX_INFO(("vnet network driver"));
|
2004-01-16 18:53:44 +03:00
|
|
|
this->rxh = rxh;
|
2005-06-09 01:11:54 +04:00
|
|
|
strcpy(this->tftp_rootdir, netif);
|
2005-05-21 23:33:25 +04:00
|
|
|
this->tftp_tid = 0;
|
2005-06-12 12:59:32 +04:00
|
|
|
this->tftp_write = 0;
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2011-03-01 23:47:12 +03:00
|
|
|
memcpy(&dhcp.host_macaddr[0], macaddr, 6);
|
|
|
|
memcpy(&dhcp.guest_macaddr[0], macaddr, 6);
|
|
|
|
dhcp.host_macaddr[5] ^= 0x03;
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2011-03-01 23:47:12 +03:00
|
|
|
memcpy(&dhcp.host_ipv4addr[0], &default_host_ipv4addr[0], 4);
|
|
|
|
memcpy(&dhcp.guest_ipv4addr[0], &broadcast_ipv4addr[1][0], 4);
|
|
|
|
dhcp.default_guest_ipv4addr = default_guest_ipv4addr;
|
|
|
|
memcpy(&dhcp.dns_ipv4addr[0], &broadcast_ipv4addr[0][0], 4);
|
2004-01-16 18:53:44 +03:00
|
|
|
|
|
|
|
l4data_used = 0;
|
|
|
|
|
|
|
|
register_layer4_handler(0x11,INET_PORT_BOOTP_SERVER,udpipv4_dhcp_handler);
|
2005-05-13 22:10:58 +04:00
|
|
|
register_layer4_handler(0x11,INET_PORT_TFTP_SERVER,udpipv4_tftp_handler);
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2008-01-27 01:24:03 +03:00
|
|
|
this->rx_timer_index =
|
2004-09-11 15:26:41 +04:00
|
|
|
bx_pc_system.register_timer(this, this->rx_timer_handler, 1000,
|
2007-07-01 11:28:14 +04:00
|
|
|
0, 0, "eth_vnet");
|
2004-08-27 12:23:50 +04:00
|
|
|
|
2004-01-16 18:53:44 +03:00
|
|
|
#if BX_ETH_VNET_LOGGING
|
2009-04-13 17:33:11 +04:00
|
|
|
pktlog_txt = fopen("ne2k-pktlog.txt", "wb");
|
|
|
|
if (!pktlog_txt) BX_PANIC(("ne2k-pktlog.txt failed"));
|
|
|
|
fprintf(pktlog_txt, "vnet packetmover readable log file\n");
|
|
|
|
fprintf(pktlog_txt, "TFTP root = %s\n", netif);
|
|
|
|
fprintf(pktlog_txt, "host MAC address = ");
|
2005-01-19 21:21:40 +03:00
|
|
|
int i;
|
2008-01-27 01:24:03 +03:00
|
|
|
for (i=0; i<6; i++)
|
2011-03-01 23:47:12 +03:00
|
|
|
fprintf(pktlog_txt, "%02x%s", 0xff & dhcp.host_macaddr[i], i<5?":" : "\n");
|
2009-04-13 17:33:11 +04:00
|
|
|
fprintf(pktlog_txt, "guest MAC address = ");
|
2008-01-27 01:24:03 +03:00
|
|
|
for (i=0; i<6; i++)
|
2011-03-01 23:47:12 +03:00
|
|
|
fprintf(pktlog_txt, "%02x%s", 0xff & dhcp.guest_macaddr[i], i<5?":" : "\n");
|
2009-04-13 17:33:11 +04:00
|
|
|
fprintf(pktlog_txt, "--\n");
|
|
|
|
fflush(pktlog_txt);
|
2004-01-16 18:53:44 +03:00
|
|
|
#endif
|
2007-07-01 11:28:14 +04:00
|
|
|
#if BX_ETH_VNET_PCAP_LOGGING
|
2009-04-13 17:33:11 +04:00
|
|
|
pcapp = pcap_open_dead(DLT_EN10MB, BX_PACKET_BUFSIZE);
|
|
|
|
pktlog_pcap = pcap_dump_open(pcapp, "ne2k-pktlog.pcap");
|
|
|
|
if (pktlog_pcap == NULL) BX_PANIC(("ne2k-pktlog.pcap failed"));
|
2007-07-01 11:28:14 +04:00
|
|
|
#endif
|
2004-01-16 18:53:44 +03:00
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::sendpkt(void *buf, unsigned io_len)
|
2004-01-16 18:53:44 +03:00
|
|
|
{
|
|
|
|
guest_to_host((const Bit8u *)buf,io_len);
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::guest_to_host(const Bit8u *buf, unsigned io_len)
|
2004-01-16 18:53:44 +03:00
|
|
|
{
|
|
|
|
#if BX_ETH_VNET_LOGGING
|
2009-04-19 21:25:40 +04:00
|
|
|
write_pktlog_txt(pktlog_txt, buf, io_len, 0);
|
2004-01-16 18:53:44 +03:00
|
|
|
#endif
|
2007-07-01 11:28:14 +04:00
|
|
|
#if BX_ETH_VNET_PCAP_LOGGING
|
|
|
|
if (pktlog_pcap && !ferror((FILE *)pktlog_pcap)) {
|
|
|
|
Bit64u time = bx_pc_system.time_usec();
|
|
|
|
pcaphdr.ts.tv_usec = time % 1000000;
|
|
|
|
pcaphdr.ts.tv_sec = time / 1000000;
|
|
|
|
pcaphdr.caplen = io_len;
|
|
|
|
pcaphdr.len = io_len;
|
|
|
|
pcap_dump((u_char *)pktlog_pcap, &pcaphdr, buf);
|
|
|
|
fflush((FILE *)pktlog_pcap);
|
2008-01-27 01:24:03 +03:00
|
|
|
}
|
2007-07-01 11:28:14 +04:00
|
|
|
#endif
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2004-09-11 15:26:41 +04:00
|
|
|
this->tx_time = (64 + 96 + 4 * 8 + io_len * 8) / 10;
|
2004-01-16 18:53:44 +03:00
|
|
|
if ((io_len >= 14) &&
|
2011-03-01 23:47:12 +03:00
|
|
|
(!memcmp(&buf[6],&dhcp.guest_macaddr[0],6)) &&
|
|
|
|
(!memcmp(&buf[0],&dhcp.host_macaddr[0],6) ||
|
2004-01-16 18:53:44 +03:00
|
|
|
!memcmp(&buf[0],&broadcast_macaddr[0],6))) {
|
|
|
|
switch (get_net2(&buf[12])) {
|
|
|
|
case 0x0800: // IPv4.
|
|
|
|
process_ipv4(buf, io_len);
|
|
|
|
break;
|
|
|
|
case 0x0806: // ARP.
|
|
|
|
process_arp(buf, io_len);
|
|
|
|
break;
|
|
|
|
default: // unknown packet type.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-27 12:23:50 +04:00
|
|
|
// The receive poll process
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::rx_timer_handler(void *this_ptr)
|
2004-08-27 12:23:50 +04:00
|
|
|
{
|
|
|
|
bx_vnet_pktmover_c *class_ptr = (bx_vnet_pktmover_c *) this_ptr;
|
|
|
|
|
|
|
|
class_ptr->rx_timer();
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::rx_timer(void)
|
2004-08-27 12:23:50 +04:00
|
|
|
{
|
2009-04-13 17:33:11 +04:00
|
|
|
this->rxh(this->netdev, (void *)packet_buffer, packet_len);
|
2004-08-27 12:23:50 +04:00
|
|
|
#if BX_ETH_VNET_LOGGING
|
2009-04-19 21:25:40 +04:00
|
|
|
write_pktlog_txt(pktlog_txt, packet_buffer, packet_len, 1);
|
2004-08-27 12:23:50 +04:00
|
|
|
#endif
|
2007-07-01 11:28:14 +04:00
|
|
|
#if BX_ETH_VNET_PCAP_LOGGING
|
|
|
|
if (pktlog_pcap && !ferror((FILE *)pktlog_pcap)) {
|
|
|
|
Bit64u time = bx_pc_system.time_usec();
|
|
|
|
pcaphdr.ts.tv_usec = time % 1000000;
|
|
|
|
pcaphdr.ts.tv_sec = time / 1000000;
|
|
|
|
pcaphdr.caplen = packet_len;
|
|
|
|
pcaphdr.len = packet_len;
|
|
|
|
pcap_dump((u_char *)pktlog_pcap, &pcaphdr, packet_buffer);
|
|
|
|
fflush((FILE *)pktlog_pcap);
|
2008-01-27 01:24:03 +03:00
|
|
|
}
|
2007-07-01 11:28:14 +04:00
|
|
|
#endif
|
2004-08-27 12:23:50 +04:00
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::host_to_guest(Bit8u *buf, unsigned io_len)
|
2004-01-16 18:53:44 +03:00
|
|
|
{
|
|
|
|
Bit8u localbuf[60];
|
|
|
|
|
|
|
|
if (io_len < 14) {
|
|
|
|
BX_PANIC(("host_to_guest: io_len < 14!"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (io_len < 60) {
|
|
|
|
memcpy(&localbuf[0],&buf[0],io_len);
|
|
|
|
memset(&localbuf[io_len],0,60-io_len);
|
|
|
|
buf=localbuf;
|
|
|
|
io_len=60;
|
|
|
|
}
|
|
|
|
|
2004-08-27 12:23:50 +04:00
|
|
|
packet_len = io_len;
|
|
|
|
memcpy(&packet_buffer, &buf[0], io_len);
|
2004-09-11 15:26:41 +04:00
|
|
|
unsigned rx_time = (64 + 96 + 4 * 8 + io_len * 8) / 10;
|
|
|
|
bx_pc_system.activate_timer(this->rx_timer_index, this->tx_time + rx_time + 100, 0);
|
2004-01-16 18:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// ARP
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::process_arp(const Bit8u *buf, unsigned io_len)
|
2004-01-16 18:53:44 +03:00
|
|
|
{
|
|
|
|
unsigned opcode;
|
|
|
|
unsigned protocol;
|
|
|
|
Bit8u replybuf[60];
|
|
|
|
|
|
|
|
if (io_len < 22) return;
|
2004-09-05 14:30:19 +04:00
|
|
|
if (io_len < (unsigned)(22+buf[18]*2+buf[19]*2)) return;
|
2004-01-16 18:53:44 +03:00
|
|
|
// hardware:Ethernet
|
|
|
|
if (buf[14] != 0x00 || buf[15] != 0x01 || buf[18] != 0x06) return;
|
|
|
|
opcode = get_net2(&buf[20]);
|
|
|
|
protocol = get_net2(&buf[16]);
|
|
|
|
memset(&replybuf[0],0,60);
|
|
|
|
|
|
|
|
// protocol
|
|
|
|
switch (protocol) {
|
|
|
|
case 0x0800: // IPv4
|
|
|
|
if (buf[19] == 0x04) {
|
|
|
|
switch (opcode) {
|
|
|
|
case 0x0001: // ARP REQUEST
|
2011-03-01 23:47:12 +03:00
|
|
|
if (!memcmp(&buf[22],&dhcp.guest_macaddr[0],6)) {
|
|
|
|
memcpy(&dhcp.guest_ipv4addr[0],&buf[28],4);
|
|
|
|
if (!memcmp(&buf[38],&dhcp.host_ipv4addr[0],4)) {
|
2004-01-16 18:53:44 +03:00
|
|
|
memcpy(&replybuf[14],&buf[14],6);
|
|
|
|
replybuf[20]=0x00;
|
|
|
|
replybuf[21]=0x02;
|
2011-03-01 23:47:12 +03:00
|
|
|
memcpy(&replybuf[22],&dhcp.host_macaddr[0],6);
|
|
|
|
memcpy(&replybuf[28],&dhcp.host_ipv4addr[0],4);
|
|
|
|
memcpy(&replybuf[32],&dhcp.guest_macaddr[0],6);
|
|
|
|
memcpy(&replybuf[38],&dhcp.guest_ipv4addr[0],4);
|
2004-01-16 18:53:44 +03:00
|
|
|
|
|
|
|
host_to_guest_arp(replybuf,60);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x0002: // ARP REPLY
|
2005-05-13 22:10:58 +04:00
|
|
|
BX_INFO(("unexpected ARP REPLY"));
|
2004-01-16 18:53:44 +03:00
|
|
|
break;
|
|
|
|
case 0x0003: // RARP REQUEST
|
2005-05-13 22:10:58 +04:00
|
|
|
BX_ERROR(("RARP is not implemented"));
|
2004-01-16 18:53:44 +03:00
|
|
|
break;
|
|
|
|
case 0x0004: // RARP REPLY
|
2005-05-13 22:10:58 +04:00
|
|
|
BX_INFO(("unexpected RARP REPLY"));
|
2004-01-16 18:53:44 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BX_INFO(("arp: unknown ARP opcode %04x",opcode));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BX_INFO(("arp: unknown address length %u",(unsigned)buf[19]));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BX_INFO(("arp: unknown protocol 0x%04x",protocol));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::host_to_guest_arp(Bit8u *buf, unsigned io_len)
|
2004-01-16 18:53:44 +03:00
|
|
|
{
|
2011-03-01 23:47:12 +03:00
|
|
|
memcpy(&buf[0],&dhcp.guest_macaddr[0],6);
|
|
|
|
memcpy(&buf[6],&dhcp.host_macaddr[0],6);
|
2004-01-16 18:53:44 +03:00
|
|
|
buf[12]=0x08;
|
|
|
|
buf[13]=0x06;
|
|
|
|
host_to_guest(buf,io_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// IPv4
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::process_ipv4(const Bit8u *buf, unsigned io_len)
|
2004-01-16 18:53:44 +03:00
|
|
|
{
|
|
|
|
unsigned total_len;
|
|
|
|
unsigned packet_id;
|
|
|
|
unsigned fragment_flags;
|
|
|
|
unsigned fragment_offset;
|
|
|
|
unsigned ipproto;
|
|
|
|
unsigned l3header_len;
|
|
|
|
const Bit8u *l4pkt;
|
|
|
|
unsigned l4pkt_len;
|
|
|
|
|
|
|
|
if (io_len < (14U+20U)) {
|
2005-05-13 22:10:58 +04:00
|
|
|
BX_INFO(("ip packet - too small packet"));
|
2004-01-16 18:53:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((buf[14+0] & 0xf0) != 0x40) {
|
2005-05-13 22:10:58 +04:00
|
|
|
BX_INFO(("ipv%u packet - not implemented",((unsigned)buf[14+0] >> 4)));
|
2004-01-16 18:53:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
l3header_len = ((unsigned)(buf[14+0] & 0x0f) << 2);
|
|
|
|
if (l3header_len != 20) {
|
|
|
|
BX_ERROR(("ip: option header is not implemented"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (io_len < (14U+l3header_len)) return;
|
|
|
|
if (ip_checksum(&buf[14],l3header_len) != (Bit16u)0xffff) {
|
|
|
|
BX_INFO(("ip: invalid checksum"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_len = get_net2(&buf[14+2]);
|
2005-05-13 22:10:58 +04:00
|
|
|
// FIXED By EaseWay
|
|
|
|
// Ignore this check to tolerant some cases
|
|
|
|
//if (io_len > (14U+total_len)) return;
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2011-03-01 23:47:12 +03:00
|
|
|
if (memcmp(&buf[14+16],dhcp.host_ipv4addr,4) &&
|
2004-01-16 18:53:44 +03:00
|
|
|
memcmp(&buf[14+16],broadcast_ipv4addr[0],4) &&
|
|
|
|
memcmp(&buf[14+16],broadcast_ipv4addr[1],4) &&
|
|
|
|
memcmp(&buf[14+16],broadcast_ipv4addr[2],4))
|
|
|
|
{
|
|
|
|
BX_INFO(("target IP address %u.%u.%u.%u is unknown",
|
|
|
|
(unsigned)buf[14+16],(unsigned)buf[14+17],
|
|
|
|
(unsigned)buf[14+18],(unsigned)buf[14+19]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
packet_id = get_net2(&buf[14+4]);
|
|
|
|
fragment_flags = (unsigned)buf[14+6] >> 5;
|
|
|
|
fragment_offset = ((unsigned)get_net2(&buf[14+6]) & 0x1fff) << 3;
|
|
|
|
ipproto = buf[14+9];
|
|
|
|
|
|
|
|
if ((fragment_flags & 0x1) || (fragment_offset != 0)) {
|
2005-05-13 22:10:58 +04:00
|
|
|
BX_INFO(("ignore fragmented packet!"));
|
2004-01-16 18:53:44 +03:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
l4pkt = &buf[14 + l3header_len];
|
|
|
|
l4pkt_len = total_len - l3header_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ipproto) {
|
|
|
|
case 0x01: // ICMP
|
|
|
|
process_icmpipv4(&buf[14],l3header_len,l4pkt,l4pkt_len);
|
|
|
|
break;
|
|
|
|
case 0x06: // TCP
|
|
|
|
process_tcpipv4(&buf[14],l3header_len,l4pkt,l4pkt_len);
|
|
|
|
break;
|
|
|
|
case 0x11: // UDP
|
|
|
|
process_udpipv4(&buf[14],l3header_len,l4pkt,l4pkt_len);
|
|
|
|
break;
|
|
|
|
default:
|
2005-05-13 22:10:58 +04:00
|
|
|
BX_INFO(("unknown IP protocol %02x",ipproto));
|
2004-01-16 18:53:44 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::host_to_guest_ipv4(Bit8u *buf, unsigned io_len)
|
2004-01-16 18:53:44 +03:00
|
|
|
{
|
|
|
|
unsigned l3header_len;
|
|
|
|
|
2011-03-01 23:47:12 +03:00
|
|
|
memcpy(&buf[0],&dhcp.guest_macaddr[0],6);
|
|
|
|
memcpy(&buf[6],&dhcp.host_macaddr[0],6);
|
2004-01-16 18:53:44 +03:00
|
|
|
buf[12]=0x08;
|
|
|
|
buf[13]=0x00;
|
|
|
|
buf[14+0] = (buf[14+0] & 0x0f) | 0x40;
|
|
|
|
l3header_len = ((unsigned)(buf[14+0] & 0x0f) << 2);
|
2011-03-01 23:47:12 +03:00
|
|
|
memcpy(&buf[14+12],&dhcp.host_ipv4addr[0],4);
|
|
|
|
memcpy(&buf[14+16],&dhcp.guest_ipv4addr[0],4);
|
2004-01-16 18:53:44 +03:00
|
|
|
put_net2(&buf[14+10], 0);
|
|
|
|
put_net2(&buf[14+10], ip_checksum(&buf[14],l3header_len) ^ (Bit16u)0xffff);
|
|
|
|
|
|
|
|
host_to_guest(buf,io_len);
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
layer4_handler_t bx_vnet_pktmover_c::get_layer4_handler(
|
2004-01-16 18:53:44 +03:00
|
|
|
unsigned ipprotocol, unsigned port)
|
|
|
|
{
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
for (n = 0; n < l4data_used; n++) {
|
|
|
|
if (l4data[n].ipprotocol == ipprotocol && l4data[n].port == port)
|
|
|
|
return l4data[n].func;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (layer4_handler_t)NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
bx_bool bx_vnet_pktmover_c::register_layer4_handler(
|
2004-01-16 18:53:44 +03:00
|
|
|
unsigned ipprotocol, unsigned port,layer4_handler_t func)
|
|
|
|
{
|
|
|
|
if (get_layer4_handler(ipprotocol,port) != (layer4_handler_t)NULL) {
|
|
|
|
BX_INFO(("IP protocol 0x%02x port %u is already in use",
|
|
|
|
ipprotocol,port));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
for (n = 0; n < l4data_used; n++) {
|
|
|
|
if (l4data[n].func == (layer4_handler_t)NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == l4data_used) {
|
|
|
|
if (n >= LAYER4_LISTEN_MAX) {
|
|
|
|
BX_ERROR(("vnet: LAYER4_LISTEN_MAX is too small"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
l4data_used++;
|
|
|
|
}
|
|
|
|
|
|
|
|
l4data[n].ipprotocol = ipprotocol;
|
|
|
|
l4data[n].port = port;
|
|
|
|
l4data[n].func = func;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
bx_bool bx_vnet_pktmover_c::unregister_layer4_handler(
|
2004-01-16 18:53:44 +03:00
|
|
|
unsigned ipprotocol, unsigned port)
|
|
|
|
{
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
for (n = 0; n < l4data_used; n++) {
|
|
|
|
if (l4data[n].ipprotocol == ipprotocol && l4data[n].port == port) {
|
|
|
|
l4data[n].func = (layer4_handler_t)NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BX_ERROR(("IP protocol 0x%02x port %u is not registered",
|
|
|
|
ipprotocol,port));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::process_icmpipv4(
|
2004-01-16 18:53:44 +03:00
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
const Bit8u *l4pkt, unsigned l4pkt_len)
|
|
|
|
{
|
|
|
|
unsigned icmptype;
|
|
|
|
unsigned icmpcode;
|
|
|
|
|
|
|
|
if (l4pkt_len < 8) return;
|
|
|
|
icmptype = l4pkt[0];
|
|
|
|
icmpcode = l4pkt[1];
|
|
|
|
if (ip_checksum(l4pkt,l4pkt_len) != (Bit16u)0xffff) {
|
|
|
|
BX_INFO(("icmp: invalid checksum"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (icmptype) {
|
|
|
|
case 0x08: // ECHO
|
|
|
|
if (icmpcode == 0) {
|
|
|
|
process_icmpipv4_echo(ipheader,ipheader_len,l4pkt,l4pkt_len);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BX_INFO(("unhandled icmp packet: type=%u code=%u",
|
|
|
|
icmptype, icmpcode));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::process_tcpipv4(
|
2004-01-16 18:53:44 +03:00
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
const Bit8u *l4pkt, unsigned l4pkt_len)
|
|
|
|
{
|
|
|
|
if (l4pkt_len < 20) return;
|
|
|
|
|
|
|
|
BX_INFO(("tcp packet - not implemented"));
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::process_udpipv4(
|
2004-01-16 18:53:44 +03:00
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
const Bit8u *l4pkt, unsigned l4pkt_len)
|
|
|
|
{
|
|
|
|
unsigned udp_targetport;
|
|
|
|
unsigned udp_sourceport;
|
|
|
|
unsigned udp_len;
|
|
|
|
layer4_handler_t func;
|
|
|
|
|
|
|
|
if (l4pkt_len < 8) return;
|
2008-01-27 01:24:03 +03:00
|
|
|
udp_sourceport = get_net2(&l4pkt[0]);
|
|
|
|
udp_targetport = get_net2(&l4pkt[2]);
|
|
|
|
udp_len = get_net2(&l4pkt[4]);
|
2004-01-16 18:53:44 +03:00
|
|
|
|
|
|
|
func = get_layer4_handler(0x11,udp_targetport);
|
|
|
|
if (func != (layer4_handler_t)NULL) {
|
|
|
|
(*func)((void *)this,ipheader,ipheader_len,
|
|
|
|
udp_sourceport,udp_targetport,&l4pkt[8],l4pkt_len-8);
|
|
|
|
} else {
|
|
|
|
BX_INFO(("udp - unhandled port %u",udp_targetport));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::host_to_guest_udpipv4_packet(
|
2004-01-16 18:53:44 +03:00
|
|
|
unsigned target_port, unsigned source_port,
|
|
|
|
const Bit8u *udpdata, unsigned udpdata_len)
|
|
|
|
{
|
2004-10-07 21:38:03 +04:00
|
|
|
Bit8u ipbuf[BX_PACKET_BUFSIZE];
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2004-10-07 21:38:03 +04:00
|
|
|
if ((udpdata_len + 42U) > BX_PACKET_BUFSIZE) {
|
2004-01-16 18:53:44 +03:00
|
|
|
BX_PANIC(("generated udp data is too long"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// udp pseudo-header
|
2004-10-13 23:42:25 +04:00
|
|
|
ipbuf[34U-12U]=0;
|
|
|
|
ipbuf[34U-11U]=0x11; // UDP
|
|
|
|
put_net2(&ipbuf[34U-10U],8U+udpdata_len);
|
2011-03-01 23:47:12 +03:00
|
|
|
memcpy(&ipbuf[34U-8U],dhcp.host_ipv4addr,4);
|
|
|
|
memcpy(&ipbuf[34U-4U],dhcp.guest_ipv4addr,4);
|
2004-01-16 18:53:44 +03:00
|
|
|
// udp header
|
|
|
|
put_net2(&ipbuf[34U+0],source_port);
|
|
|
|
put_net2(&ipbuf[34U+2],target_port);
|
|
|
|
put_net2(&ipbuf[34U+4],8U+udpdata_len);
|
|
|
|
put_net2(&ipbuf[34U+6],0);
|
2004-10-13 23:42:25 +04:00
|
|
|
memcpy(&ipbuf[42U],udpdata,udpdata_len);
|
|
|
|
put_net2(&ipbuf[34U+6], ip_checksum(&ipbuf[34U-12U],12U+8U+udpdata_len) ^ (Bit16u)0xffff);
|
2004-01-16 18:53:44 +03:00
|
|
|
// ip header
|
|
|
|
memset(&ipbuf[14U],0,20U);
|
|
|
|
ipbuf[14U+0] = 0x45;
|
|
|
|
ipbuf[14U+1] = 0x00;
|
|
|
|
put_net2(&ipbuf[14U+2],20U+8U+udpdata_len);
|
|
|
|
put_net2(&ipbuf[14U+4],1);
|
|
|
|
ipbuf[14U+6] = 0x00;
|
|
|
|
ipbuf[14U+7] = 0x00;
|
|
|
|
ipbuf[14U+8] = 0x07; // TTL
|
|
|
|
ipbuf[14U+9] = 0x11; // UDP
|
|
|
|
|
|
|
|
host_to_guest_ipv4(ipbuf,udpdata_len + 42U);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// ICMP/IPv4
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::process_icmpipv4_echo(
|
2004-01-16 18:53:44 +03:00
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
const Bit8u *l4pkt, unsigned l4pkt_len)
|
|
|
|
{
|
|
|
|
Bit8u replybuf[ICMP_ECHO_PACKET_MAX];
|
|
|
|
|
|
|
|
if ((14U+ipheader_len+l4pkt_len) > ICMP_ECHO_PACKET_MAX) {
|
|
|
|
BX_ERROR(("icmp echo: size of an echo packet is too long"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&replybuf[14],ipheader,ipheader_len);
|
|
|
|
memcpy(&replybuf[14+ipheader_len],l4pkt,l4pkt_len);
|
|
|
|
|
|
|
|
replybuf[14+ipheader_len+0] = 0x00; // echo reply
|
|
|
|
put_net2(&replybuf[14+ipheader_len+2],0);
|
|
|
|
put_net2(&replybuf[14+ipheader_len+2],
|
|
|
|
ip_checksum(&replybuf[14+ipheader_len],l4pkt_len) ^ (Bit16u)0xffff);
|
|
|
|
|
|
|
|
host_to_guest_ipv4(replybuf,14U+ipheader_len+l4pkt_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// DHCP/UDP/IPv4
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::udpipv4_dhcp_handler(
|
2004-01-16 18:53:44 +03:00
|
|
|
void *this_ptr,
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
const Bit8u *data, unsigned data_len)
|
|
|
|
{
|
|
|
|
((bx_vnet_pktmover_c *)this_ptr)->udpipv4_dhcp_handler_ns(
|
|
|
|
ipheader,ipheader_len,sourceport,targetport,data,data_len);
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::udpipv4_dhcp_handler_ns(
|
2004-01-16 18:53:44 +03:00
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
const Bit8u *data, unsigned data_len)
|
|
|
|
{
|
|
|
|
Bit8u replybuf[576];
|
2011-03-01 23:47:12 +03:00
|
|
|
unsigned opts_len;
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2011-03-01 23:47:12 +03:00
|
|
|
opts_len = process_dhcp(netdev, data, data_len, replybuf, &dhcp);
|
2011-03-02 20:51:52 +03:00
|
|
|
if (opts_len > 0) {
|
|
|
|
host_to_guest_udpipv4_packet(sourceport, targetport, replybuf, opts_len);
|
|
|
|
}
|
2004-01-16 18:53:44 +03:00
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::udpipv4_tftp_handler(
|
2005-05-13 22:10:58 +04:00
|
|
|
void *this_ptr,
|
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
const Bit8u *data, unsigned data_len)
|
|
|
|
{
|
|
|
|
((bx_vnet_pktmover_c *)this_ptr)->udpipv4_tftp_handler_ns(
|
|
|
|
ipheader,ipheader_len,sourceport,targetport,data,data_len);
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::udpipv4_tftp_handler_ns(
|
2005-05-13 22:10:58 +04:00
|
|
|
const Bit8u *ipheader, unsigned ipheader_len,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
const Bit8u *data, unsigned data_len)
|
|
|
|
{
|
2005-05-21 23:33:25 +04:00
|
|
|
Bit8u buffer[TFTP_BUFFER_SIZE + 4];
|
2005-06-12 12:59:32 +04:00
|
|
|
char path[BX_PATHNAME_LEN];
|
|
|
|
FILE *fp;
|
|
|
|
unsigned block_nr;
|
|
|
|
unsigned tftp_len;
|
|
|
|
|
2005-05-13 22:10:58 +04:00
|
|
|
switch (get_net2(data)) {
|
|
|
|
case TFTP_RRQ:
|
2005-05-21 23:33:25 +04:00
|
|
|
if (tftp_tid == 0) {
|
|
|
|
strncpy((char*)buffer, (const char*)data + 2, data_len - 2);
|
|
|
|
buffer[data_len - 4] = 0;
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
// options
|
|
|
|
size_t tsize_option = 0;
|
|
|
|
int blksize_option = 0;
|
2005-05-21 23:33:25 +04:00
|
|
|
if (strlen((char*)buffer) < data_len - 2) {
|
|
|
|
const char *mode = (const char*)data + 2 + strlen((char*)buffer) + 1;
|
2007-07-01 11:28:14 +04:00
|
|
|
int octet_option = 0;
|
|
|
|
while (mode < (const char*)data + data_len) {
|
|
|
|
if (memcmp(mode, "octet\0", 6) == 0) {
|
|
|
|
mode += 6;
|
|
|
|
octet_option = 1;
|
|
|
|
} else if (memcmp(mode, "tsize\0", 6) == 0) {
|
|
|
|
mode += 6;
|
|
|
|
tsize_option = 1; // size needed
|
|
|
|
mode += strlen(mode)+1;
|
|
|
|
} else if (memcmp(mode, "blksize\0", 8) == 0) {
|
|
|
|
mode += 8;
|
|
|
|
blksize_option = atoi(mode);
|
|
|
|
mode += strlen(mode)+1;
|
|
|
|
} else {
|
|
|
|
BX_INFO(("tftp req: unknown option %s", mode));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!octet_option) {
|
2005-05-21 23:33:25 +04:00
|
|
|
tftp_send_error(buffer, sourceport, targetport, 4, "Unsupported transfer mode");
|
|
|
|
return;
|
|
|
|
}
|
2005-05-13 22:10:58 +04:00
|
|
|
}
|
2005-05-21 23:33:25 +04:00
|
|
|
|
2005-06-09 01:11:54 +04:00
|
|
|
strcpy(tftp_filename, (char*)buffer);
|
2007-07-01 11:28:14 +04:00
|
|
|
BX_INFO(("tftp req: %s", tftp_filename));
|
|
|
|
if (tsize_option) {
|
2009-04-13 17:33:11 +04:00
|
|
|
tsize_option = get_file_size(netdev, tftp_rootdir, tftp_filename);
|
2007-07-01 11:28:14 +04:00
|
|
|
if (tsize_option > 0) {
|
|
|
|
// if tsize requested and file exists, send optack and return
|
|
|
|
// optack ack will pick up where we leave off here.
|
|
|
|
// if blksize_option is less than TFTP_BUFFER_SIZE should
|
|
|
|
// probably use blksize_option...
|
|
|
|
tftp_send_optack(buffer, sourceport, targetport, tsize_option, TFTP_BUFFER_SIZE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-05-21 23:33:25 +04:00
|
|
|
tftp_tid = sourceport;
|
2005-06-12 12:59:32 +04:00
|
|
|
tftp_write = 0;
|
2005-05-21 23:33:25 +04:00
|
|
|
tftp_send_data(buffer, sourceport, targetport, 1);
|
|
|
|
} else {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 4, "Illegal request");
|
2005-05-13 22:10:58 +04:00
|
|
|
}
|
|
|
|
break;
|
2005-06-09 01:11:54 +04:00
|
|
|
case TFTP_WRQ:
|
|
|
|
if (tftp_tid == 0) {
|
|
|
|
strncpy((char*)buffer, (const char*)data + 2, data_len - 2);
|
|
|
|
buffer[data_len - 4] = 0;
|
|
|
|
|
|
|
|
// transfer mode
|
|
|
|
if (strlen((char*)buffer) < data_len - 2) {
|
|
|
|
const char *mode = (const char*)data + 2 + strlen((char*)buffer) + 1;
|
|
|
|
if (memcmp(mode, "octet\0", 6) != 0) {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 4, "Unsupported transfer mode");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-12 12:59:32 +04:00
|
|
|
strcpy(tftp_filename, (char*)buffer);
|
|
|
|
sprintf(path, "%s/%s", tftp_rootdir, tftp_filename);
|
|
|
|
fp = fopen(path, "rb");
|
|
|
|
if (fp) {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 6, "File exists");
|
|
|
|
fclose(fp);
|
2007-07-01 11:28:14 +04:00
|
|
|
return;
|
2005-06-12 12:59:32 +04:00
|
|
|
}
|
|
|
|
fp = fopen(path, "wb");
|
|
|
|
if (!fp) {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 2, "Access violation");
|
2007-07-01 11:28:14 +04:00
|
|
|
return;
|
2005-06-12 12:59:32 +04:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
tftp_tid = sourceport;
|
|
|
|
tftp_write = 1;
|
|
|
|
|
|
|
|
tftp_send_ack(buffer, sourceport, targetport, 0);
|
|
|
|
} else {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 4, "Illegal request");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TFTP_DATA:
|
|
|
|
if ((tftp_tid == sourceport) && (tftp_write == 1)) {
|
|
|
|
block_nr = get_net2(data + 2);
|
|
|
|
strncpy((char*)buffer, (const char*)data + 4, data_len - 4);
|
|
|
|
tftp_len = data_len - 4;
|
|
|
|
buffer[tftp_len] = 0;
|
|
|
|
if (tftp_len <= 512) {
|
|
|
|
sprintf(path, "%s/%s", tftp_rootdir, tftp_filename);
|
|
|
|
fp = fopen(path, "ab");
|
|
|
|
if (!fp) {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 2, "Access violation");
|
2007-07-01 11:28:14 +04:00
|
|
|
return;
|
2005-06-12 12:59:32 +04:00
|
|
|
}
|
|
|
|
if (fseek(fp, (block_nr - 1) * TFTP_BUFFER_SIZE, SEEK_SET) < 0) {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 3, "Block not seekable");
|
2007-07-01 11:28:14 +04:00
|
|
|
return;
|
2005-06-12 12:59:32 +04:00
|
|
|
}
|
|
|
|
fwrite(buffer, 1, tftp_len, fp);
|
|
|
|
fclose(fp);
|
|
|
|
tftp_send_ack(buffer, sourceport, targetport, block_nr);
|
|
|
|
if (tftp_len < 512) {
|
|
|
|
tftp_tid = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 4, "Illegal request");
|
|
|
|
}
|
2005-06-09 01:11:54 +04:00
|
|
|
} else {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 4, "Illegal request");
|
|
|
|
}
|
|
|
|
break;
|
2005-05-13 22:10:58 +04:00
|
|
|
case TFTP_ACK:
|
|
|
|
tftp_send_data(buffer, sourceport, targetport, get_net2(data + 2) + 1);
|
|
|
|
break;
|
2005-05-21 23:33:25 +04:00
|
|
|
case TFTP_ERROR:
|
|
|
|
// silently ignore error packets
|
|
|
|
break;
|
2005-05-13 22:10:58 +04:00
|
|
|
default:
|
|
|
|
BX_ERROR(("TFTP unknown opt %d", get_net2(data)));
|
2008-01-27 01:24:03 +03:00
|
|
|
}
|
2005-05-13 22:10:58 +04:00
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::tftp_send_error(
|
2005-05-13 22:10:58 +04:00
|
|
|
Bit8u *buffer,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
unsigned code, const char *msg)
|
|
|
|
{
|
|
|
|
put_net2(buffer, TFTP_ERROR);
|
|
|
|
put_net2(buffer + 2, code);
|
|
|
|
strcpy((char*)buffer + 4, msg);
|
2008-01-27 01:24:03 +03:00
|
|
|
host_to_guest_udpipv4_packet(sourceport, targetport, buffer, strlen(msg) + 5);
|
2005-05-21 23:33:25 +04:00
|
|
|
tftp_tid = 0;
|
2005-05-13 22:10:58 +04:00
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::tftp_send_data(
|
2005-05-13 22:10:58 +04:00
|
|
|
Bit8u *buffer,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
unsigned block_nr)
|
|
|
|
{
|
2005-06-09 01:11:54 +04:00
|
|
|
char path[BX_PATHNAME_LEN];
|
|
|
|
char msg[BX_PATHNAME_LEN];
|
2005-05-13 22:10:58 +04:00
|
|
|
int rd;
|
|
|
|
|
2005-06-09 01:11:54 +04:00
|
|
|
if (strlen(tftp_filename) == 0) {
|
2005-05-13 22:10:58 +04:00
|
|
|
tftp_send_error(buffer, sourceport, targetport, 1, "File not found");
|
|
|
|
return;
|
|
|
|
}
|
2005-06-09 01:11:54 +04:00
|
|
|
|
|
|
|
if ((strlen(tftp_rootdir) + strlen(tftp_filename)) > BX_PATHNAME_LEN) {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 1, "Path name too long");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(path, "%s/%s", tftp_rootdir, tftp_filename);
|
|
|
|
FILE *fp = fopen(path, "rb");
|
2005-05-13 22:10:58 +04:00
|
|
|
if (!fp) {
|
2005-06-09 01:11:54 +04:00
|
|
|
sprintf(msg, "File not found: %s", tftp_filename);
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 1, msg);
|
2007-07-01 11:28:14 +04:00
|
|
|
return;
|
2005-05-13 22:10:58 +04:00
|
|
|
}
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2005-05-21 23:33:25 +04:00
|
|
|
if (fseek(fp, (block_nr - 1) * TFTP_BUFFER_SIZE, SEEK_SET) < 0) {
|
2005-05-13 22:10:58 +04:00
|
|
|
tftp_send_error(buffer, sourceport, targetport, 3, "Block not seekable");
|
2007-07-01 11:28:14 +04:00
|
|
|
return;
|
2005-05-13 22:10:58 +04:00
|
|
|
}
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2005-05-21 23:33:25 +04:00
|
|
|
rd = fread(buffer + 4, 1, TFTP_BUFFER_SIZE, fp);
|
2005-05-13 22:10:58 +04:00
|
|
|
fclose(fp);
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2005-05-13 22:10:58 +04:00
|
|
|
if (rd < 0) {
|
|
|
|
tftp_send_error(buffer, sourceport, targetport, 3, "Block not readable");
|
2007-07-01 11:28:14 +04:00
|
|
|
return;
|
2005-05-13 22:10:58 +04:00
|
|
|
}
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2005-05-13 22:10:58 +04:00
|
|
|
put_net2(buffer, TFTP_DATA);
|
|
|
|
put_net2(buffer + 2, block_nr);
|
|
|
|
host_to_guest_udpipv4_packet(sourceport, targetport, buffer, rd + 4);
|
2005-05-21 23:33:25 +04:00
|
|
|
if (rd < TFTP_BUFFER_SIZE) {
|
|
|
|
tftp_tid = 0;
|
|
|
|
}
|
2005-05-13 22:10:58 +04:00
|
|
|
}
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::tftp_send_ack(
|
2005-06-12 12:59:32 +04:00
|
|
|
Bit8u *buffer,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
|
|
|
unsigned block_nr)
|
|
|
|
{
|
|
|
|
put_net2(buffer, TFTP_ACK);
|
|
|
|
put_net2(buffer + 2, block_nr);
|
|
|
|
host_to_guest_udpipv4_packet(sourceport, targetport, buffer, 4);
|
|
|
|
}
|
|
|
|
|
2007-07-01 11:28:14 +04:00
|
|
|
void bx_vnet_pktmover_c::tftp_send_optack(
|
|
|
|
Bit8u *buffer,
|
|
|
|
unsigned sourceport, unsigned targetport,
|
2007-09-04 11:45:31 +04:00
|
|
|
size_t tsize_option, unsigned blksize_option)
|
2007-07-01 11:28:14 +04:00
|
|
|
{
|
|
|
|
Bit8u *p = buffer;
|
|
|
|
put_net2(p, TFTP_OPTACK);
|
|
|
|
p += 2;
|
|
|
|
if (tsize_option > 0) {
|
|
|
|
*p++='t'; *p++='s'; *p++='i'; *p++='z'; *p++='e'; *p++='\0';
|
|
|
|
sprintf((char *)p, "%lu", (unsigned long)tsize_option);
|
|
|
|
p += strlen((const char *)p) + 1;
|
|
|
|
}
|
|
|
|
if (blksize_option > 0) {
|
|
|
|
*p++='b'; *p++='l'; *p++='k'; *p++='s'; *p++='i'; *p++='z'; *p++='e'; *p++='\0';
|
|
|
|
sprintf((char *)p, "%d", blksize_option); p += strlen((const char *)p) + 1;
|
|
|
|
}
|
|
|
|
host_to_guest_udpipv4_packet(sourceport, targetport, buffer, p - buffer);
|
|
|
|
}
|
2004-01-16 18:53:44 +03:00
|
|
|
|
2004-10-04 00:06:12 +04:00
|
|
|
#endif /* if BX_NETWORKING */
|