* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
+
#include "qemu/osdep.h"
+#include "qemu/log.h"
#include "net/slirp.h"
#include "qemu/error-report.h"
#include "qemu/sockets.h"
#include "slirp/libslirp.h"
-#include "slirp/ip6.h"
#include "chardev/char-fe.h"
#include "sysemu/sysemu.h"
#include "qemu/cutils.h"
#include "qapi/error.h"
+#include "qapi/qmp/qdict.h"
+#include "util.h"
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
{
/* 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 {
} 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 =
+static QTAILQ_HEAD(, SlirpState) slirp_stacks =
QTAILQ_HEAD_INITIALIZER(slirp_stacks);
-static int slirp_hostfwd(SlirpState *s, const char *redir_str,
- int legacy_format, Error **errp);
-static int slirp_guestfwd(SlirpState *s, const char *config_str,
- int legacy_format, Error **errp);
+static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
+static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
#ifndef _WIN32
-static const char *legacy_smb_export;
-
static int slirp_smb(SlirpState *s, const char *exported_dir,
struct in_addr vserver_addr, Error **errp);
static void slirp_smb_cleanup(SlirpState *s);
static inline void slirp_smb_cleanup(SlirpState *s) { }
#endif
-void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
+static void net_slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
{
SlirpState *s = opaque;
.cleanup = net_slirp_cleanup,
};
+static void net_slirp_guest_error(const char *msg)
+{
+ qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
+}
+
+static int64_t net_slirp_clock_get_ns(void)
+{
+ return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
+}
+
+static const SlirpCb slirp_cb = {
+ .output = net_slirp_output,
+ .guest_error = net_slirp_guest_error,
+ .clock_get_ns = net_slirp_clock_get_ns,
+};
+
static int net_slirp_init(NetClientState *peer, const char *model,
const char *name, int restricted,
bool ipv4, const char *vnetwork, const char *vhost,
const char *bootfile, const char *vdhcp_start,
const char *vnameserver, const char *vnameserver6,
const char *smb_export, const char *vsmbserver,
- const char **dnssearch, Error **errp)
+ const char **dnssearch, const char *vdomainname,
+ const char *tftp_server_name,
+ Error **errp)
{
/* default settings according to historic slirp */
struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
return -1;
}
- 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)) {
}
#endif
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
- /* No inet_pton helper before Vista... */
- if (vprefix6) {
- /* Unsupported */
- error_setg(errp, "IPv6 prefix not supported");
- return -1;
- }
- memset(&ip6_prefix, 0, sizeof(ip6_prefix));
- ip6_prefix.s6_addr[0] = 0xfe;
- ip6_prefix.s6_addr[1] = 0xc0;
-#else
if (!vprefix6) {
vprefix6 = "fec0::";
}
error_setg(errp, "Failed to parse IPv6 prefix");
return -1;
}
-#endif
if (!vprefix6_len) {
vprefix6_len = 64;
}
if (vhost6) {
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
- error_setg(errp, "IPv6 host not supported");
- return -1;
-#else
if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
error_setg(errp, "Failed to parse IPv6 host");
return -1;
error_setg(errp, "IPv6 Host doesn't belong to network");
return -1;
}
-#endif
} else {
ip6_host = ip6_prefix;
ip6_host.s6_addr[15] |= 2;
}
if (vnameserver6) {
-#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
- error_setg(errp, "IPv6 DNS not supported");
- return -1;
-#else
if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
error_setg(errp, "Failed to parse IPv6 DNS");
return -1;
error_setg(errp, "IPv6 DNS doesn't belong to network");
return -1;
}
-#endif
} else {
ip6_dns = ip6_prefix;
ip6_dns.s6_addr[15] |= 3;
}
+ if (vdomainname && !*vdomainname) {
+ error_setg(errp, "'domainname' parameter cannot be empty");
+ return -1;
+ }
+
+ if (vdomainname && strlen(vdomainname) > 255) {
+ error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
+ return -1;
+ }
+
+ if (vhostname && strlen(vhostname) > 255) {
+ error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
+ return -1;
+ }
+
+ if (tftp_server_name && strlen(tftp_server_name) > 255) {
+ error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
+ return -1;
+ }
nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
s->slirp = slirp_init(restricted, ipv4, net, mask, host,
ipv6, ip6_prefix, vprefix6_len, ip6_host,
- vhostname, tftp_export, bootfile, dhcp,
- dns, ip6_dns, dnssearch, s);
+ vhostname, tftp_server_name,
+ tftp_export, bootfile, dhcp,
+ dns, ip6_dns, dnssearch, vdomainname,
+ &slirp_cb, s);
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, errp) < 0) {
+ if (slirp_hostfwd(s, config->str, errp) < 0) {
goto error;
}
} else {
- if (slirp_guestfwd(s, config->str,
- config->flags & SLIRP_CFG_LEGACY, errp) < 0) {
+ if (slirp_guestfwd(s, config->str, errp) < 0) {
goto error;
}
}
}
#ifndef _WIN32
- if (!smb_export) {
- smb_export = legacy_smb_export;
- }
if (smb_export) {
if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
goto error;
return -1;
}
-static SlirpState *slirp_lookup(Monitor *mon, const char *vlan,
- const char *stack)
+static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
+ const char *name)
{
-
- if (vlan) {
+ if (name) {
NetClientState *nc;
- nc = net_hub_find_client_by_name(strtol(vlan, NULL, 0), stack);
- if (!nc) {
- monitor_printf(mon, "unrecognized (vlan-id, stackname) pair\n");
- return NULL;
+ if (hub_id) {
+ nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
+ if (!nc) {
+ monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
+ return NULL;
+ }
+ warn_report("Using 'hub-id' is deprecated, specify the netdev id "
+ "directly instead");
+ } else {
+ nc = qemu_find_netdev(name);
+ if (!nc) {
+ monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
+ return NULL;
+ }
}
if (strcmp(nc->model, "user")) {
monitor_printf(mon, "invalid device specified\n");
const char *arg2 = qdict_get_try_str(qdict, "arg2");
const char *arg3 = qdict_get_try_str(qdict, "arg3");
- if (arg2) {
+ if (arg3) {
s = slirp_lookup(mon, arg1, arg2);
src_str = arg3;
+ } else if (arg2) {
+ s = slirp_lookup(mon, NULL, arg1);
+ src_str = arg2;
} else {
s = slirp_lookup(mon, NULL, NULL);
src_str = arg1;
goto fail_syntax;
}
- host_port = atoi(p);
+ if (qemu_strtoi(p, NULL, 10, &host_port)) {
+ goto fail_syntax;
+ }
err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
monitor_printf(mon, "invalid format\n");
}
-static int slirp_hostfwd(SlirpState *s, const char *redir_str,
- int legacy_format, Error **errp)
+static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
{
struct in_addr host_addr = { .s_addr = INADDR_ANY };
struct in_addr guest_addr = { .s_addr = 0 };
goto fail_syntax;
}
- if (!legacy_format) {
- if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
- fail_reason = "Missing : separator";
- goto fail_syntax;
- }
- if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
- fail_reason = "Bad host address";
- goto fail_syntax;
- }
+ if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
+ fail_reason = "Missing : separator";
+ goto fail_syntax;
+ }
+ if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
+ fail_reason = "Bad host address";
+ goto fail_syntax;
}
- if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) {
+ if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
fail_reason = "Bad host port separator";
goto fail_syntax;
}
const char *arg2 = qdict_get_try_str(qdict, "arg2");
const char *arg3 = qdict_get_try_str(qdict, "arg3");
- if (arg2) {
+ if (arg3) {
s = slirp_lookup(mon, arg1, arg2);
redir_str = arg3;
+ } else if (arg2) {
+ s = slirp_lookup(mon, NULL, arg1);
+ redir_str = arg2;
} else {
s = slirp_lookup(mon, NULL, NULL);
redir_str = arg1;
}
if (s) {
Error *err = NULL;
- if (slirp_hostfwd(s, redir_str, 0, &err) < 0) {
+ if (slirp_hostfwd(s, redir_str, &err) < 0) {
error_report_err(err);
}
}
}
-int net_slirp_redir(const char *redir_str)
-{
- struct slirp_config_str *config;
- Error *err = NULL;
- int res;
-
- if (QTAILQ_EMPTY(&slirp_stacks)) {
- config = g_malloc(sizeof(*config));
- 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;
- }
-
- res = slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1, &err);
- if (res < 0) {
- error_report_err(err);
- }
- return res;
-}
-
#ifndef _WIN32
/* automatic user mode samba server configuration */
CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
g_free(smb_conf);
- 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) {
+ if (slirp_add_exec(s->slirp, NULL, smb_cmdline, &vserver_addr, 139) < 0 ||
+ slirp_add_exec(s->slirp, NULL, smb_cmdline, &vserver_addr, 445) < 0) {
slirp_smb_cleanup(s);
g_free(smb_cmdline);
error_setg(errp, "Conflicting/invalid smbserver address");
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)) {
- Error *err = NULL;
- int res = slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir,
- vserver_addr, &err);
- if (res < 0) {
- error_report_err(err);
- }
- return res;
- }
- return 0;
-}
-
#endif /* !defined(_WIN32) */
struct GuestFwd {
slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
}
-static int slirp_guestfwd(SlirpState *s, const char *config_str,
- int legacy_format, Error **errp)
+static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
{
struct in_addr server = { .s_addr = 0 };
struct GuestFwd *fwd;
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;
- }
+ 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) {
snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
- if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
+ if (slirp_add_exec(s->slirp, NULL, &p[4], &server, port) < 0) {
error_setg(errp, "Conflicting/invalid host:port in guest "
"forwarding rule '%s'", config_str);
return -1;
}
} else {
Error *err = NULL;
- Chardev *chr = qemu_chr_new(buf, p);
+ /*
+ * FIXME: sure we want to support implicit
+ * muxed monitors here?
+ */
+ Chardev *chr = qemu_chr_new_mux_mon(buf, p);
if (!chr) {
error_setg(errp, "Could not open guest forwarding device '%s'",
return -1;
}
- if (slirp_add_exec(s->slirp, 3, &fwd->hd, &server, port) < 0) {
+ if (slirp_add_exec(s->slirp, &fwd->hd, NULL, &server, port) < 0) {
error_setg(errp, "Conflicting/invalid host:port in guest "
"forwarding rule '%s'", config_str);
g_free(fwd);
QTAILQ_FOREACH(s, &slirp_stacks, entry) {
int id;
- bool got_vlan_id = net_hub_id_for_client(&s->nc, &id) == 0;
- monitor_printf(mon, "VLAN %d (%s):\n",
- got_vlan_id ? id : -1,
- s->nc.name);
- slirp_connection_info(s->slirp, mon);
+ bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
+ char *info = slirp_connection_info(s->slirp);
+ monitor_printf(mon, "Hub %d (%s):\n%s",
+ got_hub_id ? id : -1,
+ s->nc.name, info);
+ g_free(info);
}
}
user->ipv6_host, user->hostname, user->tftp,
user->bootfile, user->dhcpstart,
user->dns, user->ipv6_dns, user->smb,
- user->smbserver, dnssearch, errp);
+ user->smbserver, dnssearch, user->domainname,
+ user->tftp_server_name, errp);
while (slirp_configs) {
config = slirp_configs;
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;
- }
-
- error_report("The '-net channel' option is deprecated. "
- "Please use '-netdev user,guestfwd=...' instead.");
-
- /* handle legacy -net channel,port:chr */
- optarg += strlen("channel,");
-
- if (QTAILQ_EMPTY(&slirp_stacks)) {
- struct slirp_config_str *config;
-
- config = g_malloc(sizeof(*config));
- pstrcpy(config->str, sizeof(config->str), optarg);
- config->flags = SLIRP_CFG_LEGACY;
- config->next = slirp_configs;
- slirp_configs = config;
- *ret = 0;
- } else {
- Error *err = NULL;
- *ret = slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), optarg, 1, &err);
- if (*ret < 0) {
- error_report_err(err);
- }
- }
-
- return 1;
-}
-