2009-11-25 21:48:54 +03:00
|
|
|
/*
|
|
|
|
* QEMU System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "net/slirp.h"
|
|
|
|
|
|
|
|
#include "config-host.h"
|
|
|
|
|
2010-01-27 20:47:33 +03:00
|
|
|
#ifndef _WIN32
|
2012-07-05 21:35:57 +04:00
|
|
|
#include <pwd.h>
|
2010-01-27 20:47:33 +03:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
2012-10-24 10:43:34 +04:00
|
|
|
#include "net/net.h"
|
2012-09-17 20:43:51 +04:00
|
|
|
#include "clients.h"
|
|
|
|
#include "hub.h"
|
2012-12-17 21:19:49 +04:00
|
|
|
#include "monitor/monitor.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/sockets.h"
|
2009-11-25 21:48:54 +03:00
|
|
|
#include "slirp/libslirp.h"
|
2013-04-08 18:55:25 +04:00
|
|
|
#include "sysemu/char.h"
|
2009-11-25 21:48:54 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* slirp network adapter */
|
|
|
|
|
|
|
|
#define SLIRP_CFG_HOSTFWD 1
|
|
|
|
#define SLIRP_CFG_LEGACY 2
|
|
|
|
|
|
|
|
struct slirp_config_str {
|
|
|
|
struct slirp_config_str *next;
|
|
|
|
int flags;
|
|
|
|
char str[1024];
|
|
|
|
int legacy_format;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct SlirpState {
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState nc;
|
2009-11-25 21:48:54 +03:00
|
|
|
QTAILQ_ENTRY(SlirpState) entry;
|
|
|
|
Slirp *slirp;
|
|
|
|
#ifndef _WIN32
|
|
|
|
char smb_dir[128];
|
|
|
|
#endif
|
|
|
|
} SlirpState;
|
|
|
|
|
|
|
|
static struct slirp_config_str *slirp_configs;
|
|
|
|
const char *legacy_tftp_prefix;
|
|
|
|
const char *legacy_bootp_filename;
|
|
|
|
static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
|
|
|
|
QTAILQ_HEAD_INITIALIZER(slirp_stacks);
|
|
|
|
|
|
|
|
static int slirp_hostfwd(SlirpState *s, const char *redir_str,
|
|
|
|
int legacy_format);
|
|
|
|
static int slirp_guestfwd(SlirpState *s, const char *config_str,
|
|
|
|
int legacy_format);
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
static const char *legacy_smb_export;
|
|
|
|
|
|
|
|
static int slirp_smb(SlirpState *s, const char *exported_dir,
|
|
|
|
struct in_addr vserver_addr);
|
|
|
|
static void slirp_smb_cleanup(SlirpState *s);
|
|
|
|
#else
|
|
|
|
static inline void slirp_smb_cleanup(SlirpState *s) { }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
|
|
|
|
{
|
|
|
|
SlirpState *s = opaque;
|
|
|
|
|
2009-11-25 21:49:06 +03:00
|
|
|
qemu_send_packet(&s->nc, pkt, pkt_len);
|
2009-11-25 21:48:54 +03:00
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
|
2009-11-25 21:48:54 +03:00
|
|
|
{
|
2009-11-25 21:49:06 +03:00
|
|
|
SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
|
2009-11-25 21:48:54 +03:00
|
|
|
|
|
|
|
slirp_input(s->slirp, buf, size);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static void net_slirp_cleanup(NetClientState *nc)
|
2009-11-25 21:48:54 +03:00
|
|
|
{
|
2009-11-25 21:49:06 +03:00
|
|
|
SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
|
2009-11-25 21:48:54 +03:00
|
|
|
|
|
|
|
slirp_cleanup(s->slirp);
|
|
|
|
slirp_smb_cleanup(s);
|
|
|
|
QTAILQ_REMOVE(&slirp_stacks, s, entry);
|
|
|
|
}
|
|
|
|
|
2009-11-25 21:49:06 +03:00
|
|
|
static NetClientInfo net_slirp_info = {
|
2012-07-17 18:17:12 +04:00
|
|
|
.type = NET_CLIENT_OPTIONS_KIND_USER,
|
2009-11-25 21:49:06 +03:00
|
|
|
.size = sizeof(SlirpState),
|
|
|
|
.receive = net_slirp_receive,
|
|
|
|
.cleanup = net_slirp_cleanup,
|
|
|
|
};
|
|
|
|
|
2012-07-24 19:35:13 +04:00
|
|
|
static int net_slirp_init(NetClientState *peer, const char *model,
|
2009-11-25 21:48:54 +03:00
|
|
|
const char *name, int restricted,
|
|
|
|
const char *vnetwork, const char *vhost,
|
|
|
|
const char *vhostname, const char *tftp_export,
|
|
|
|
const char *bootfile, const char *vdhcp_start,
|
|
|
|
const char *vnameserver, const char *smb_export,
|
2012-10-27 21:53:39 +04:00
|
|
|
const char *vsmbserver, const char **dnssearch)
|
2009-11-25 21:48:54 +03:00
|
|
|
{
|
|
|
|
/* default settings according to historic slirp */
|
|
|
|
struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
|
|
|
|
struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
|
|
|
|
struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
|
|
|
|
struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
|
|
|
|
struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
|
|
|
|
#ifndef _WIN32
|
|
|
|
struct in_addr smbsrv = { .s_addr = 0 };
|
|
|
|
#endif
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *nc;
|
2009-11-25 21:48:54 +03:00
|
|
|
SlirpState *s;
|
|
|
|
char buf[20];
|
|
|
|
uint32_t addr;
|
|
|
|
int shift;
|
|
|
|
char *end;
|
|
|
|
struct slirp_config_str *config;
|
|
|
|
|
|
|
|
if (!tftp_export) {
|
|
|
|
tftp_export = legacy_tftp_prefix;
|
|
|
|
}
|
|
|
|
if (!bootfile) {
|
|
|
|
bootfile = legacy_bootp_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vnetwork) {
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
|
|
|
|
if (!inet_aton(vnetwork, &net)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
addr = ntohl(net.s_addr);
|
|
|
|
if (!(addr & 0x80000000)) {
|
|
|
|
mask.s_addr = htonl(0xff000000); /* class A */
|
|
|
|
} else if ((addr & 0xfff00000) == 0xac100000) {
|
|
|
|
mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
|
|
|
|
} else if ((addr & 0xc0000000) == 0x80000000) {
|
|
|
|
mask.s_addr = htonl(0xffff0000); /* class B */
|
|
|
|
} else if ((addr & 0xffff0000) == 0xc0a80000) {
|
|
|
|
mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
|
|
|
|
} else if ((addr & 0xffff0000) == 0xc6120000) {
|
|
|
|
mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
|
|
|
|
} else if ((addr & 0xe0000000) == 0xe0000000) {
|
|
|
|
mask.s_addr = htonl(0xffffff00); /* class C */
|
|
|
|
} else {
|
|
|
|
mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!inet_aton(buf, &net)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
shift = strtol(vnetwork, &end, 10);
|
|
|
|
if (*end != '\0') {
|
|
|
|
if (!inet_aton(vnetwork, &mask)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (shift < 4 || shift > 32) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
mask.s_addr = htonl(0xffffffff << (32 - shift));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
net.s_addr &= mask.s_addr;
|
|
|
|
host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
|
|
|
|
dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
|
|
|
|
dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vhost && !inet_aton(vhost, &host)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((host.s_addr & mask.s_addr) != net.s_addr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-03 17:11:49 +04:00
|
|
|
if (vnameserver && !inet_aton(vnameserver, &dns)) {
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2013-06-03 17:11:49 +04:00
|
|
|
if ((dns.s_addr & mask.s_addr) != net.s_addr ||
|
|
|
|
dns.s_addr == host.s_addr) {
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-03 17:11:49 +04:00
|
|
|
if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2013-06-03 17:11:49 +04:00
|
|
|
if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
|
|
|
|
dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-24 19:35:08 +04:00
|
|
|
nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
|
2009-11-25 21:49:06 +03:00
|
|
|
|
|
|
|
snprintf(nc->info_str, sizeof(nc->info_str),
|
2011-07-20 14:20:14 +04:00
|
|
|
"net=%s,restrict=%s", inet_ntoa(net),
|
|
|
|
restricted ? "on" : "off");
|
2009-11-25 21:49:06 +03:00
|
|
|
|
|
|
|
s = DO_UPCAST(SlirpState, nc, nc);
|
|
|
|
|
2009-11-25 21:48:54 +03:00
|
|
|
s->slirp = slirp_init(restricted, net, mask, host, vhostname,
|
2012-10-27 21:53:39 +04:00
|
|
|
tftp_export, bootfile, dhcp, dns, dnssearch, s);
|
2009-11-25 21:48:54 +03:00
|
|
|
QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
|
|
|
|
|
|
|
|
for (config = slirp_configs; config; config = config->next) {
|
|
|
|
if (config->flags & SLIRP_CFG_HOSTFWD) {
|
|
|
|
if (slirp_hostfwd(s, config->str,
|
|
|
|
config->flags & SLIRP_CFG_LEGACY) < 0)
|
2009-11-25 21:49:06 +03:00
|
|
|
goto error;
|
2009-11-25 21:48:54 +03:00
|
|
|
} else {
|
|
|
|
if (slirp_guestfwd(s, config->str,
|
|
|
|
config->flags & SLIRP_CFG_LEGACY) < 0)
|
2009-11-25 21:49:06 +03:00
|
|
|
goto error;
|
2009-11-25 21:48:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (!smb_export) {
|
|
|
|
smb_export = legacy_smb_export;
|
|
|
|
}
|
|
|
|
if (smb_export) {
|
|
|
|
if (slirp_smb(s, smb_export, smbsrv) < 0)
|
2009-11-25 21:49:06 +03:00
|
|
|
goto error;
|
2009-11-25 21:48:54 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
2009-11-25 21:49:06 +03:00
|
|
|
|
|
|
|
error:
|
2012-07-24 19:35:15 +04:00
|
|
|
qemu_del_net_client(nc);
|
2009-11-25 21:49:06 +03:00
|
|
|
return -1;
|
2009-11-25 21:48:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static SlirpState *slirp_lookup(Monitor *mon, const char *vlan,
|
|
|
|
const char *stack)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (vlan) {
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *nc;
|
2012-07-24 19:35:06 +04:00
|
|
|
nc = net_hub_find_client_by_name(strtol(vlan, NULL, 0), stack);
|
2009-11-25 21:49:06 +03:00
|
|
|
if (!nc) {
|
2014-06-16 19:47:11 +04:00
|
|
|
monitor_printf(mon, "unrecognized (vlan-id, stackname) pair\n");
|
2009-11-25 21:48:54 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-25 21:49:06 +03:00
|
|
|
if (strcmp(nc->model, "user")) {
|
2009-11-25 21:48:54 +03:00
|
|
|
monitor_printf(mon, "invalid device specified\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-11-25 21:49:06 +03:00
|
|
|
return DO_UPCAST(SlirpState, nc, nc);
|
2009-11-25 21:48:54 +03:00
|
|
|
} else {
|
|
|
|
if (QTAILQ_EMPTY(&slirp_stacks)) {
|
|
|
|
monitor_printf(mon, "user mode network stack not in use\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return QTAILQ_FIRST(&slirp_stacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict)
|
|
|
|
{
|
|
|
|
struct in_addr host_addr = { .s_addr = INADDR_ANY };
|
|
|
|
int host_port;
|
2011-11-16 18:45:59 +04:00
|
|
|
char buf[256];
|
2009-11-25 21:48:54 +03:00
|
|
|
const char *src_str, *p;
|
|
|
|
SlirpState *s;
|
|
|
|
int is_udp = 0;
|
|
|
|
int err;
|
|
|
|
const char *arg1 = qdict_get_str(qdict, "arg1");
|
|
|
|
const char *arg2 = qdict_get_try_str(qdict, "arg2");
|
|
|
|
const char *arg3 = qdict_get_try_str(qdict, "arg3");
|
|
|
|
|
|
|
|
if (arg2) {
|
|
|
|
s = slirp_lookup(mon, arg1, arg2);
|
|
|
|
src_str = arg3;
|
|
|
|
} else {
|
|
|
|
s = slirp_lookup(mon, NULL, NULL);
|
|
|
|
src_str = arg1;
|
|
|
|
}
|
|
|
|
if (!s) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = src_str;
|
2011-11-16 18:45:59 +04:00
|
|
|
if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
2009-11-25 21:48:54 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
|
|
|
|
host_port = atoi(p);
|
|
|
|
|
2014-06-16 19:47:49 +04:00
|
|
|
err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
|
2009-11-25 21:48:54 +03:00
|
|
|
|
|
|
|
monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
|
2011-12-17 13:23:59 +04:00
|
|
|
err ? "not found" : "removed");
|
2009-11-25 21:48:54 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail_syntax:
|
|
|
|
monitor_printf(mon, "invalid format\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int slirp_hostfwd(SlirpState *s, const char *redir_str,
|
|
|
|
int legacy_format)
|
|
|
|
{
|
|
|
|
struct in_addr host_addr = { .s_addr = INADDR_ANY };
|
|
|
|
struct in_addr guest_addr = { .s_addr = 0 };
|
|
|
|
int host_port, guest_port;
|
|
|
|
const char *p;
|
|
|
|
char buf[256];
|
|
|
|
int is_udp;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
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 (!legacy_format) {
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 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;
|
|
|
|
}
|
|
|
|
if (buf[0] != '\0' && !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_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
|
|
|
|
guest_port) < 0) {
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("could not set up host forwarding rule '%s'",
|
|
|
|
redir_str);
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_syntax:
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("invalid host forwarding rule '%s'", redir_str);
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict)
|
|
|
|
{
|
|
|
|
const char *redir_str;
|
|
|
|
SlirpState *s;
|
|
|
|
const char *arg1 = qdict_get_str(qdict, "arg1");
|
|
|
|
const char *arg2 = qdict_get_try_str(qdict, "arg2");
|
|
|
|
const char *arg3 = qdict_get_try_str(qdict, "arg3");
|
|
|
|
|
|
|
|
if (arg2) {
|
|
|
|
s = slirp_lookup(mon, arg1, arg2);
|
|
|
|
redir_str = arg3;
|
|
|
|
} else {
|
|
|
|
s = slirp_lookup(mon, NULL, NULL);
|
|
|
|
redir_str = arg1;
|
|
|
|
}
|
|
|
|
if (s) {
|
|
|
|
slirp_hostfwd(s, redir_str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_slirp_redir(const char *redir_str)
|
|
|
|
{
|
|
|
|
struct slirp_config_str *config;
|
|
|
|
|
|
|
|
if (QTAILQ_EMPTY(&slirp_stacks)) {
|
2011-08-21 07:09:37 +04:00
|
|
|
config = g_malloc(sizeof(*config));
|
2009-11-25 21:48:54 +03:00
|
|
|
pstrcpy(config->str, sizeof(config->str), redir_str);
|
|
|
|
config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
|
|
|
|
config->next = slirp_configs;
|
|
|
|
slirp_configs = config;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
/* automatic user mode samba server configuration */
|
|
|
|
static void slirp_smb_cleanup(SlirpState *s)
|
|
|
|
{
|
|
|
|
char cmd[128];
|
2010-01-20 02:56:16 +03:00
|
|
|
int ret;
|
2009-11-25 21:48:54 +03:00
|
|
|
|
|
|
|
if (s->smb_dir[0] != '\0') {
|
|
|
|
snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir);
|
2010-01-20 02:56:16 +03:00
|
|
|
ret = system(cmd);
|
2010-03-04 12:00:31 +03:00
|
|
|
if (ret == -1 || !WIFEXITED(ret)) {
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("'%s' failed.", cmd);
|
2010-01-20 02:56:16 +03:00
|
|
|
} else if (WEXITSTATUS(ret)) {
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("'%s' failed. Error code: %d",
|
|
|
|
cmd, WEXITSTATUS(ret));
|
2010-01-20 02:56:16 +03:00
|
|
|
}
|
2009-11-25 21:48:54 +03:00
|
|
|
s->smb_dir[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int slirp_smb(SlirpState* s, const char *exported_dir,
|
|
|
|
struct in_addr vserver_addr)
|
|
|
|
{
|
|
|
|
static int instance;
|
|
|
|
char smb_conf[128];
|
|
|
|
char smb_cmdline[128];
|
2012-07-05 21:35:57 +04:00
|
|
|
struct passwd *passwd;
|
2009-11-25 21:48:54 +03:00
|
|
|
FILE *f;
|
|
|
|
|
2012-07-05 21:35:57 +04:00
|
|
|
passwd = getpwuid(geteuid());
|
|
|
|
if (!passwd) {
|
|
|
|
error_report("failed to retrieve user name");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-07-06 10:04:43 +04:00
|
|
|
if (access(CONFIG_SMBD_COMMAND, F_OK)) {
|
|
|
|
error_report("could not find '%s', please install it",
|
|
|
|
CONFIG_SMBD_COMMAND);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access(exported_dir, R_OK | X_OK)) {
|
2012-07-06 10:40:48 +04:00
|
|
|
error_report("error accessing shared directory '%s': %s",
|
|
|
|
exported_dir, strerror(errno));
|
2012-07-06 10:04:43 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-11-25 21:48:54 +03:00
|
|
|
snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
|
|
|
|
(long)getpid(), instance++);
|
|
|
|
if (mkdir(s->smb_dir, 0700) < 0) {
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("could not create samba server dir '%s'", s->smb_dir);
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf");
|
|
|
|
|
|
|
|
f = fopen(smb_conf, "w");
|
|
|
|
if (!f) {
|
|
|
|
slirp_smb_cleanup(s);
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("could not create samba server configuration file '%s'",
|
|
|
|
smb_conf);
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fprintf(f,
|
|
|
|
"[global]\n"
|
|
|
|
"private dir=%s\n"
|
|
|
|
"socket address=127.0.0.1\n"
|
|
|
|
"pid directory=%s\n"
|
|
|
|
"lock directory=%s\n"
|
2012-04-25 17:57:19 +04:00
|
|
|
"state directory=%s\n"
|
2014-04-27 14:54:12 +04:00
|
|
|
"ncalrpc dir=%s/ncalrpc\n"
|
2009-11-25 21:48:54 +03:00
|
|
|
"log file=%s/log.smbd\n"
|
|
|
|
"smb passwd file=%s/smbpasswd\n"
|
2013-11-01 15:23:49 +04:00
|
|
|
"security = user\n"
|
|
|
|
"map to guest = Bad User\n"
|
2009-11-25 21:48:54 +03:00
|
|
|
"[qemu]\n"
|
|
|
|
"path=%s\n"
|
|
|
|
"read only=no\n"
|
2012-07-05 21:35:57 +04:00
|
|
|
"guest ok=yes\n"
|
|
|
|
"force user=%s\n",
|
2009-11-25 21:48:54 +03:00
|
|
|
s->smb_dir,
|
|
|
|
s->smb_dir,
|
|
|
|
s->smb_dir,
|
|
|
|
s->smb_dir,
|
|
|
|
s->smb_dir,
|
2012-04-25 17:57:19 +04:00
|
|
|
s->smb_dir,
|
2014-04-27 14:54:12 +04:00
|
|
|
s->smb_dir,
|
2012-07-05 21:35:57 +04:00
|
|
|
exported_dir,
|
|
|
|
passwd->pw_name
|
2009-11-25 21:48:54 +03:00
|
|
|
);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
|
2011-09-03 00:53:28 +04:00
|
|
|
CONFIG_SMBD_COMMAND, smb_conf);
|
2009-11-25 21:48:54 +03:00
|
|
|
|
slirp smb with modern win guests when samba is also running on host
After numerous reports that -smb (or -netdev user,smb=foo) not working
with modern windows (win7 and vista are reported as non-working), I
started digging myself. And found that indeed it doesn't work, and
why.
The thing is that modern win tries to connect to port 445 (microsoft-ds)
first, and if that fails, it falls back to old port 139 (netbios-ssn).
slirp code in qemu only redirects port 139, it does not touch port 445.
So the prob is that if samba is also running on the host, guest will try
to communicate using port 445, and that will succed, but ofcourse guest
will not talk with our samba but with samba running on the host.
If samba is not running on the host, guest will fall back to port 139,
and will reach the redirecting rule and qemu will spawn smbd correctly.
The solution is to redirect both ports (139 and 445), and the fix is
a one-liner, adding second call to slirp_add_exec() at the end of
net/slirp.c:slirp_smb() function (provided below).
But it looks like that is not a proper fix really, since in theory
we should redirect both ports to the SAME, single samba instance,
but I'm not sure this is possible with slirp. Well, even if two
smbd processes will be run on the same config dir, it should not
be a problem.
The one-liner (not exactly 1 since it touches previous line too) is like
this:
Signed-off-By: Michael Tokarev <mjt@tls.msk.ru>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
2013-11-28 23:32:55 +04:00
|
|
|
if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0 ||
|
|
|
|
slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 445) < 0) {
|
2009-11-25 21:48:54 +03:00
|
|
|
slirp_smb_cleanup(s);
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("conflicting/invalid smbserver address");
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* automatic user mode samba server configuration (legacy interface) */
|
|
|
|
int net_slirp_smb(const char *exported_dir)
|
|
|
|
{
|
|
|
|
struct in_addr vserver_addr = { .s_addr = 0 };
|
|
|
|
|
|
|
|
if (legacy_smb_export) {
|
|
|
|
fprintf(stderr, "-smb given twice\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
legacy_smb_export = exported_dir;
|
|
|
|
if (!QTAILQ_EMPTY(&slirp_stacks)) {
|
|
|
|
return slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir,
|
|
|
|
vserver_addr);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined(_WIN32) */
|
|
|
|
|
|
|
|
struct GuestFwd {
|
|
|
|
CharDriverState *hd;
|
|
|
|
struct in_addr server;
|
|
|
|
int port;
|
|
|
|
Slirp *slirp;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int guestfwd_can_read(void *opaque)
|
|
|
|
{
|
|
|
|
struct GuestFwd *fwd = opaque;
|
|
|
|
return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
|
|
|
|
{
|
|
|
|
struct GuestFwd *fwd = opaque;
|
|
|
|
slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int slirp_guestfwd(SlirpState *s, const char *config_str,
|
|
|
|
int legacy_format)
|
|
|
|
{
|
|
|
|
struct in_addr server = { .s_addr = 0 };
|
|
|
|
struct GuestFwd *fwd;
|
|
|
|
const char *p;
|
|
|
|
char buf[128];
|
|
|
|
char *end;
|
|
|
|
int port;
|
|
|
|
|
|
|
|
p = config_str;
|
|
|
|
if (legacy_format) {
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
if (strcmp(buf, "tcp") && buf[0] != '\0') {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
if (buf[0] != '\0' && !inet_aton(buf, &server)) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
port = strtol(buf, &end, 10);
|
|
|
|
if (*end != '\0' || port < 1 || port > 65535) {
|
|
|
|
goto fail_syntax;
|
|
|
|
}
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
fwd = g_malloc(sizeof(struct GuestFwd));
|
2011-06-04 09:25:59 +04:00
|
|
|
snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-06-03 11:45:01 +04:00
|
|
|
if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
|
|
|
|
if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
|
|
|
|
error_report("conflicting/invalid host:port in guest forwarding "
|
|
|
|
"rule '%s'", config_str);
|
|
|
|
g_free(fwd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fwd->hd = qemu_chr_new(buf, p, NULL);
|
|
|
|
if (!fwd->hd) {
|
|
|
|
error_report("could not open guest forwarding device '%s'", buf);
|
|
|
|
g_free(fwd);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-06-03 11:45:01 +04:00
|
|
|
if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
|
|
|
|
error_report("conflicting/invalid host:port in guest forwarding "
|
|
|
|
"rule '%s'", config_str);
|
|
|
|
g_free(fwd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fwd->server = server;
|
|
|
|
fwd->port = port;
|
|
|
|
fwd->slirp = s->slirp;
|
|
|
|
|
2013-03-27 23:29:40 +04:00
|
|
|
qemu_chr_fe_claim_no_fail(fwd->hd);
|
2012-06-03 11:45:01 +04:00
|
|
|
qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
|
|
|
|
NULL, fwd);
|
|
|
|
}
|
2009-11-25 21:48:54 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_syntax:
|
2010-02-18 19:25:24 +03:00
|
|
|
error_report("invalid guest forwarding rule '%s'", config_str);
|
2009-11-25 21:48:54 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-14 10:06:25 +04:00
|
|
|
void do_info_usernet(Monitor *mon, const QDict *qdict)
|
2009-11-25 21:48:54 +03:00
|
|
|
{
|
|
|
|
SlirpState *s;
|
|
|
|
|
|
|
|
QTAILQ_FOREACH(s, &slirp_stacks, entry) {
|
2012-07-24 19:35:06 +04:00
|
|
|
int id;
|
|
|
|
bool got_vlan_id = net_hub_id_for_client(&s->nc, &id) == 0;
|
2009-11-25 21:49:06 +03:00
|
|
|
monitor_printf(mon, "VLAN %d (%s):\n",
|
2012-07-24 19:35:06 +04:00
|
|
|
got_vlan_id ? id : -1,
|
2009-11-25 21:49:06 +03:00
|
|
|
s->nc.name);
|
2009-11-25 21:48:54 +03:00
|
|
|
slirp_connection_info(s->slirp, mon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:16 +04:00
|
|
|
static void
|
|
|
|
net_init_slirp_configs(const StringList *fwd, int flags)
|
2009-11-25 21:48:54 +03:00
|
|
|
{
|
2012-07-17 18:17:16 +04:00
|
|
|
while (fwd) {
|
|
|
|
struct slirp_config_str *config;
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-07-17 18:17:16 +04:00
|
|
|
config = g_malloc0(sizeof(*config));
|
|
|
|
pstrcpy(config->str, sizeof(config->str), fwd->value->str);
|
|
|
|
config->flags = flags;
|
|
|
|
config->next = slirp_configs;
|
|
|
|
slirp_configs = config;
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-07-17 18:17:16 +04:00
|
|
|
fwd = fwd->next;
|
2009-11-25 21:48:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-27 21:53:39 +04:00
|
|
|
static const char **slirp_dnssearch(const StringList *dnsname)
|
|
|
|
{
|
|
|
|
const StringList *c = dnsname;
|
|
|
|
size_t i = 0, num_opts = 0;
|
|
|
|
const char **ret;
|
|
|
|
|
|
|
|
while (c) {
|
|
|
|
num_opts++;
|
|
|
|
c = c->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_opts == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = g_malloc((num_opts + 1) * sizeof(*ret));
|
|
|
|
c = dnsname;
|
|
|
|
while (c) {
|
|
|
|
ret[i++] = c->value->str;
|
|
|
|
c = c->next;
|
|
|
|
}
|
|
|
|
ret[i] = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-07-17 18:17:21 +04:00
|
|
|
int net_init_slirp(const NetClientOptions *opts, const char *name,
|
2012-07-24 19:35:13 +04:00
|
|
|
NetClientState *peer)
|
2009-11-25 21:48:54 +03:00
|
|
|
{
|
|
|
|
struct slirp_config_str *config;
|
2012-07-17 18:17:16 +04:00
|
|
|
char *vnet;
|
2009-11-25 21:48:54 +03:00
|
|
|
int ret;
|
2012-07-17 18:17:16 +04:00
|
|
|
const NetdevUserOptions *user;
|
2012-10-27 21:53:39 +04:00
|
|
|
const char **dnssearch;
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-07-17 18:17:16 +04:00
|
|
|
assert(opts->kind == NET_CLIENT_OPTIONS_KIND_USER);
|
|
|
|
user = opts->user;
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-07-17 18:17:16 +04:00
|
|
|
vnet = user->has_net ? g_strdup(user->net) :
|
|
|
|
user->has_ip ? g_strdup_printf("%s/24", user->ip) :
|
|
|
|
NULL;
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-10-27 21:53:39 +04:00
|
|
|
dnssearch = slirp_dnssearch(user->dnssearch);
|
|
|
|
|
2012-07-17 18:17:16 +04:00
|
|
|
/* all optional fields are initialized to "all bits zero" */
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-07-17 18:17:16 +04:00
|
|
|
net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
|
|
|
|
net_init_slirp_configs(user->guestfwd, 0);
|
2009-11-25 21:48:54 +03:00
|
|
|
|
2012-07-30 19:46:55 +04:00
|
|
|
ret = net_slirp_init(peer, "user", name, user->q_restrict, vnet,
|
|
|
|
user->host, user->hostname, user->tftp,
|
|
|
|
user->bootfile, user->dhcpstart, user->dns, user->smb,
|
2012-10-27 21:53:39 +04:00
|
|
|
user->smbserver, dnssearch);
|
2009-11-25 21:48:54 +03:00
|
|
|
|
|
|
|
while (slirp_configs) {
|
|
|
|
config = slirp_configs;
|
|
|
|
slirp_configs = config->next;
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(config);
|
2009-11-25 21:48:54 +03:00
|
|
|
}
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free(vnet);
|
2012-10-27 21:53:39 +04:00
|
|
|
g_free(dnssearch);
|
2009-11-25 21:48:54 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_slirp_parse_legacy(QemuOptsList *opts_list, const char *optarg, int *ret)
|
|
|
|
{
|
|
|
|
if (strcmp(opts_list->name, "net") != 0 ||
|
|
|
|
strncmp(optarg, "channel,", strlen("channel,")) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle legacy -net channel,port:chr */
|
|
|
|
optarg += strlen("channel,");
|
|
|
|
|
|
|
|
if (QTAILQ_EMPTY(&slirp_stacks)) {
|
|
|
|
struct slirp_config_str *config;
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
config = g_malloc(sizeof(*config));
|
2009-11-25 21:48:54 +03:00
|
|
|
pstrcpy(config->str, sizeof(config->str), optarg);
|
|
|
|
config->flags = SLIRP_CFG_LEGACY;
|
|
|
|
config->next = slirp_configs;
|
|
|
|
slirp_configs = config;
|
|
|
|
*ret = 0;
|
|
|
|
} else {
|
|
|
|
*ret = slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), optarg, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|