tcg/aarch64: Support vector absolute value
[qemu/ar7.git] / net / slirp.c
blob95934fb36d61025ef45afa35a7a2ad29411f4717
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/log.h"
27 #include "net/slirp.h"
30 #ifndef _WIN32
31 #include <pwd.h>
32 #include <sys/wait.h>
33 #endif
34 #include "net/net.h"
35 #include "clients.h"
36 #include "hub.h"
37 #include "monitor/monitor.h"
38 #include "qemu/error-report.h"
39 #include "qemu/sockets.h"
40 #include <libslirp.h>
41 #include "chardev/char-fe.h"
42 #include "sysemu/sysemu.h"
43 #include "qemu/cutils.h"
44 #include "qapi/error.h"
45 #include "qapi/qmp/qdict.h"
46 #include "util.h"
47 #include "migration/register.h"
48 #include "migration/qemu-file-types.h"
50 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
52 const char *p, *p1;
53 int len;
54 p = *pp;
55 p1 = strchr(p, sep);
56 if (!p1)
57 return -1;
58 len = p1 - p;
59 p1++;
60 if (buf_size > 0) {
61 if (len > buf_size - 1)
62 len = buf_size - 1;
63 memcpy(buf, p, len);
64 buf[len] = '\0';
66 *pp = p1;
67 return 0;
70 /* slirp network adapter */
72 #define SLIRP_CFG_HOSTFWD 1
74 struct slirp_config_str {
75 struct slirp_config_str *next;
76 int flags;
77 char str[1024];
80 struct GuestFwd {
81 CharBackend hd;
82 struct in_addr server;
83 int port;
84 Slirp *slirp;
87 typedef struct SlirpState {
88 NetClientState nc;
89 QTAILQ_ENTRY(SlirpState) entry;
90 Slirp *slirp;
91 Notifier poll_notifier;
92 Notifier exit_notifier;
93 #ifndef _WIN32
94 gchar *smb_dir;
95 #endif
96 GSList *fwd;
97 } SlirpState;
99 static struct slirp_config_str *slirp_configs;
100 static QTAILQ_HEAD(, SlirpState) slirp_stacks =
101 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
103 static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
104 static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
106 #ifndef _WIN32
107 static int slirp_smb(SlirpState *s, const char *exported_dir,
108 struct in_addr vserver_addr, Error **errp);
109 static void slirp_smb_cleanup(SlirpState *s);
110 #else
111 static inline void slirp_smb_cleanup(SlirpState *s) { }
112 #endif
114 static ssize_t net_slirp_send_packet(const void *pkt, size_t pkt_len,
115 void *opaque)
117 SlirpState *s = opaque;
119 return qemu_send_packet(&s->nc, pkt, pkt_len);
122 static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
124 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
126 slirp_input(s->slirp, buf, size);
128 return size;
131 static void slirp_smb_exit(Notifier *n, void *data)
133 SlirpState *s = container_of(n, SlirpState, exit_notifier);
134 slirp_smb_cleanup(s);
137 static void slirp_free_fwd(gpointer data)
139 struct GuestFwd *fwd = data;
141 qemu_chr_fe_deinit(&fwd->hd, true);
142 g_free(data);
145 static void net_slirp_cleanup(NetClientState *nc)
147 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
149 g_slist_free_full(s->fwd, slirp_free_fwd);
150 main_loop_poll_remove_notifier(&s->poll_notifier);
151 unregister_savevm(NULL, "slirp", s->slirp);
152 slirp_cleanup(s->slirp);
153 if (s->exit_notifier.notify) {
154 qemu_remove_exit_notifier(&s->exit_notifier);
156 slirp_smb_cleanup(s);
157 QTAILQ_REMOVE(&slirp_stacks, s, entry);
160 static NetClientInfo net_slirp_info = {
161 .type = NET_CLIENT_DRIVER_USER,
162 .size = sizeof(SlirpState),
163 .receive = net_slirp_receive,
164 .cleanup = net_slirp_cleanup,
167 static void net_slirp_guest_error(const char *msg, void *opaque)
169 qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
172 static int64_t net_slirp_clock_get_ns(void *opaque)
174 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
177 static void *net_slirp_timer_new(SlirpTimerCb cb,
178 void *cb_opaque, void *opaque)
180 return timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
181 SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
182 cb, cb_opaque);
185 static void net_slirp_timer_free(void *timer, void *opaque)
187 timer_del(timer);
188 timer_free(timer);
191 static void net_slirp_timer_mod(void *timer, int64_t expire_timer,
192 void *opaque)
194 timer_mod(timer, expire_timer);
197 static void net_slirp_register_poll_fd(int fd, void *opaque)
199 qemu_fd_register(fd);
202 static void net_slirp_unregister_poll_fd(int fd, void *opaque)
204 /* no qemu_fd_unregister */
207 static void net_slirp_notify(void *opaque)
209 qemu_notify_event();
212 static const SlirpCb slirp_cb = {
213 .send_packet = net_slirp_send_packet,
214 .guest_error = net_slirp_guest_error,
215 .clock_get_ns = net_slirp_clock_get_ns,
216 .timer_new = net_slirp_timer_new,
217 .timer_free = net_slirp_timer_free,
218 .timer_mod = net_slirp_timer_mod,
219 .register_poll_fd = net_slirp_register_poll_fd,
220 .unregister_poll_fd = net_slirp_unregister_poll_fd,
221 .notify = net_slirp_notify,
224 static int slirp_poll_to_gio(int events)
226 int ret = 0;
228 if (events & SLIRP_POLL_IN) {
229 ret |= G_IO_IN;
231 if (events & SLIRP_POLL_OUT) {
232 ret |= G_IO_OUT;
234 if (events & SLIRP_POLL_PRI) {
235 ret |= G_IO_PRI;
237 if (events & SLIRP_POLL_ERR) {
238 ret |= G_IO_ERR;
240 if (events & SLIRP_POLL_HUP) {
241 ret |= G_IO_HUP;
244 return ret;
247 static int net_slirp_add_poll(int fd, int events, void *opaque)
249 GArray *pollfds = opaque;
250 GPollFD pfd = {
251 .fd = fd,
252 .events = slirp_poll_to_gio(events),
254 int idx = pollfds->len;
255 g_array_append_val(pollfds, pfd);
256 return idx;
259 static int slirp_gio_to_poll(int events)
261 int ret = 0;
263 if (events & G_IO_IN) {
264 ret |= SLIRP_POLL_IN;
266 if (events & G_IO_OUT) {
267 ret |= SLIRP_POLL_OUT;
269 if (events & G_IO_PRI) {
270 ret |= SLIRP_POLL_PRI;
272 if (events & G_IO_ERR) {
273 ret |= SLIRP_POLL_ERR;
275 if (events & G_IO_HUP) {
276 ret |= SLIRP_POLL_HUP;
279 return ret;
282 static int net_slirp_get_revents(int idx, void *opaque)
284 GArray *pollfds = opaque;
286 return slirp_gio_to_poll(g_array_index(pollfds, GPollFD, idx).revents);
289 static void net_slirp_poll_notify(Notifier *notifier, void *data)
291 MainLoopPoll *poll = data;
292 SlirpState *s = container_of(notifier, SlirpState, poll_notifier);
294 switch (poll->state) {
295 case MAIN_LOOP_POLL_FILL:
296 slirp_pollfds_fill(s->slirp, &poll->timeout,
297 net_slirp_add_poll, poll->pollfds);
298 break;
299 case MAIN_LOOP_POLL_OK:
300 case MAIN_LOOP_POLL_ERR:
301 slirp_pollfds_poll(s->slirp, poll->state == MAIN_LOOP_POLL_ERR,
302 net_slirp_get_revents, poll->pollfds);
303 break;
304 default:
305 g_assert_not_reached();
309 static ssize_t
310 net_slirp_stream_read(void *buf, size_t size, void *opaque)
312 QEMUFile *f = opaque;
314 return qemu_get_buffer(f, buf, size);
317 static ssize_t
318 net_slirp_stream_write(const void *buf, size_t size, void *opaque)
320 QEMUFile *f = opaque;
322 qemu_put_buffer(f, buf, size);
323 if (qemu_file_get_error(f)) {
324 return -1;
327 return size;
330 static int net_slirp_state_load(QEMUFile *f, void *opaque, int version_id)
332 Slirp *slirp = opaque;
334 return slirp_state_load(slirp, version_id, net_slirp_stream_read, f);
337 static void net_slirp_state_save(QEMUFile *f, void *opaque)
339 Slirp *slirp = opaque;
341 slirp_state_save(slirp, net_slirp_stream_write, f);
344 static SaveVMHandlers savevm_slirp_state = {
345 .save_state = net_slirp_state_save,
346 .load_state = net_slirp_state_load,
349 static int net_slirp_init(NetClientState *peer, const char *model,
350 const char *name, int restricted,
351 bool ipv4, const char *vnetwork, const char *vhost,
352 bool ipv6, const char *vprefix6, int vprefix6_len,
353 const char *vhost6,
354 const char *vhostname, const char *tftp_export,
355 const char *bootfile, const char *vdhcp_start,
356 const char *vnameserver, const char *vnameserver6,
357 const char *smb_export, const char *vsmbserver,
358 const char **dnssearch, const char *vdomainname,
359 const char *tftp_server_name,
360 Error **errp)
362 /* default settings according to historic slirp */
363 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
364 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
365 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
366 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
367 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
368 struct in6_addr ip6_prefix;
369 struct in6_addr ip6_host;
370 struct in6_addr ip6_dns;
371 #ifndef _WIN32
372 struct in_addr smbsrv = { .s_addr = 0 };
373 #endif
374 NetClientState *nc;
375 SlirpState *s;
376 char buf[20];
377 uint32_t addr;
378 int shift;
379 char *end;
380 struct slirp_config_str *config;
382 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
383 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
384 return -1;
387 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
388 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
389 return -1;
392 if (!ipv4 && !ipv6) {
393 /* It doesn't make sense to disable both */
394 error_setg(errp, "IPv4 and IPv6 disabled");
395 return -1;
398 if (vnetwork) {
399 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
400 if (!inet_aton(vnetwork, &net)) {
401 error_setg(errp, "Failed to parse netmask");
402 return -1;
404 addr = ntohl(net.s_addr);
405 if (!(addr & 0x80000000)) {
406 mask.s_addr = htonl(0xff000000); /* class A */
407 } else if ((addr & 0xfff00000) == 0xac100000) {
408 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
409 } else if ((addr & 0xc0000000) == 0x80000000) {
410 mask.s_addr = htonl(0xffff0000); /* class B */
411 } else if ((addr & 0xffff0000) == 0xc0a80000) {
412 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
413 } else if ((addr & 0xffff0000) == 0xc6120000) {
414 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
415 } else if ((addr & 0xe0000000) == 0xe0000000) {
416 mask.s_addr = htonl(0xffffff00); /* class C */
417 } else {
418 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
420 } else {
421 if (!inet_aton(buf, &net)) {
422 error_setg(errp, "Failed to parse netmask");
423 return -1;
425 shift = strtol(vnetwork, &end, 10);
426 if (*end != '\0') {
427 if (!inet_aton(vnetwork, &mask)) {
428 error_setg(errp,
429 "Failed to parse netmask (trailing chars)");
430 return -1;
432 } else if (shift < 4 || shift > 32) {
433 error_setg(errp,
434 "Invalid netmask provided (must be in range 4-32)");
435 return -1;
436 } else {
437 mask.s_addr = htonl(0xffffffff << (32 - shift));
440 net.s_addr &= mask.s_addr;
441 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
442 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
443 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
446 if (vhost && !inet_aton(vhost, &host)) {
447 error_setg(errp, "Failed to parse host");
448 return -1;
450 if ((host.s_addr & mask.s_addr) != net.s_addr) {
451 error_setg(errp, "Host doesn't belong to network");
452 return -1;
455 if (vnameserver && !inet_aton(vnameserver, &dns)) {
456 error_setg(errp, "Failed to parse DNS");
457 return -1;
459 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
460 error_setg(errp, "DNS doesn't belong to network");
461 return -1;
463 if (dns.s_addr == host.s_addr) {
464 error_setg(errp, "DNS must be different from host");
465 return -1;
468 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
469 error_setg(errp, "Failed to parse DHCP start address");
470 return -1;
472 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
473 error_setg(errp, "DHCP doesn't belong to network");
474 return -1;
476 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
477 error_setg(errp, "DNS must be different from host and DNS");
478 return -1;
481 #ifndef _WIN32
482 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
483 error_setg(errp, "Failed to parse SMB address");
484 return -1;
486 #endif
488 if (!vprefix6) {
489 vprefix6 = "fec0::";
491 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
492 error_setg(errp, "Failed to parse IPv6 prefix");
493 return -1;
496 if (!vprefix6_len) {
497 vprefix6_len = 64;
499 if (vprefix6_len < 0 || vprefix6_len > 126) {
500 error_setg(errp,
501 "Invalid prefix provided (prefix len must be in range 0-126");
502 return -1;
505 if (vhost6) {
506 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
507 error_setg(errp, "Failed to parse IPv6 host");
508 return -1;
510 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
511 error_setg(errp, "IPv6 Host doesn't belong to network");
512 return -1;
514 } else {
515 ip6_host = ip6_prefix;
516 ip6_host.s6_addr[15] |= 2;
519 if (vnameserver6) {
520 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
521 error_setg(errp, "Failed to parse IPv6 DNS");
522 return -1;
524 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
525 error_setg(errp, "IPv6 DNS doesn't belong to network");
526 return -1;
528 } else {
529 ip6_dns = ip6_prefix;
530 ip6_dns.s6_addr[15] |= 3;
533 if (vdomainname && !*vdomainname) {
534 error_setg(errp, "'domainname' parameter cannot be empty");
535 return -1;
538 if (vdomainname && strlen(vdomainname) > 255) {
539 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
540 return -1;
543 if (vhostname && strlen(vhostname) > 255) {
544 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
545 return -1;
548 if (tftp_server_name && strlen(tftp_server_name) > 255) {
549 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
550 return -1;
553 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
555 snprintf(nc->info_str, sizeof(nc->info_str),
556 "net=%s,restrict=%s", inet_ntoa(net),
557 restricted ? "on" : "off");
559 s = DO_UPCAST(SlirpState, nc, nc);
561 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
562 ipv6, ip6_prefix, vprefix6_len, ip6_host,
563 vhostname, tftp_server_name,
564 tftp_export, bootfile, dhcp,
565 dns, ip6_dns, dnssearch, vdomainname,
566 &slirp_cb, s);
567 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
570 * Make sure the current bitstream version of slirp is 4, to avoid
571 * QEMU migration incompatibilities, if upstream slirp bumped the
572 * version.
574 * FIXME: use bitfields of features? teach libslirp to save with
575 * specific version?
577 g_assert(slirp_state_version() == 4);
578 register_savevm_live(NULL, "slirp", 0, slirp_state_version(),
579 &savevm_slirp_state, s->slirp);
581 s->poll_notifier.notify = net_slirp_poll_notify;
582 main_loop_poll_add_notifier(&s->poll_notifier);
584 for (config = slirp_configs; config; config = config->next) {
585 if (config->flags & SLIRP_CFG_HOSTFWD) {
586 if (slirp_hostfwd(s, config->str, errp) < 0) {
587 goto error;
589 } else {
590 if (slirp_guestfwd(s, config->str, errp) < 0) {
591 goto error;
595 #ifndef _WIN32
596 if (smb_export) {
597 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
598 goto error;
601 #endif
603 s->exit_notifier.notify = slirp_smb_exit;
604 qemu_add_exit_notifier(&s->exit_notifier);
605 return 0;
607 error:
608 qemu_del_net_client(nc);
609 return -1;
612 static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
613 const char *name)
615 if (name) {
616 NetClientState *nc;
617 if (hub_id) {
618 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
619 if (!nc) {
620 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
621 return NULL;
623 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
624 "directly instead");
625 } else {
626 nc = qemu_find_netdev(name);
627 if (!nc) {
628 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
629 return NULL;
632 if (strcmp(nc->model, "user")) {
633 monitor_printf(mon, "invalid device specified\n");
634 return NULL;
636 return DO_UPCAST(SlirpState, nc, nc);
637 } else {
638 if (QTAILQ_EMPTY(&slirp_stacks)) {
639 monitor_printf(mon, "user mode network stack not in use\n");
640 return NULL;
642 return QTAILQ_FIRST(&slirp_stacks);
646 void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
648 struct in_addr host_addr = { .s_addr = INADDR_ANY };
649 int host_port;
650 char buf[256];
651 const char *src_str, *p;
652 SlirpState *s;
653 int is_udp = 0;
654 int err;
655 const char *arg1 = qdict_get_str(qdict, "arg1");
656 const char *arg2 = qdict_get_try_str(qdict, "arg2");
657 const char *arg3 = qdict_get_try_str(qdict, "arg3");
659 if (arg3) {
660 s = slirp_lookup(mon, arg1, arg2);
661 src_str = arg3;
662 } else if (arg2) {
663 s = slirp_lookup(mon, NULL, arg1);
664 src_str = arg2;
665 } else {
666 s = slirp_lookup(mon, NULL, NULL);
667 src_str = arg1;
669 if (!s) {
670 return;
673 p = src_str;
674 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
675 goto fail_syntax;
678 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
679 is_udp = 0;
680 } else if (!strcmp(buf, "udp")) {
681 is_udp = 1;
682 } else {
683 goto fail_syntax;
686 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
687 goto fail_syntax;
689 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
690 goto fail_syntax;
693 if (qemu_strtoi(p, NULL, 10, &host_port)) {
694 goto fail_syntax;
697 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
699 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
700 err ? "not found" : "removed");
701 return;
703 fail_syntax:
704 monitor_printf(mon, "invalid format\n");
707 static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
709 struct in_addr host_addr = { .s_addr = INADDR_ANY };
710 struct in_addr guest_addr = { .s_addr = 0 };
711 int host_port, guest_port;
712 const char *p;
713 char buf[256];
714 int is_udp;
715 char *end;
716 const char *fail_reason = "Unknown reason";
718 p = redir_str;
719 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
720 fail_reason = "No : separators";
721 goto fail_syntax;
723 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
724 is_udp = 0;
725 } else if (!strcmp(buf, "udp")) {
726 is_udp = 1;
727 } else {
728 fail_reason = "Bad protocol name";
729 goto fail_syntax;
732 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
733 fail_reason = "Missing : separator";
734 goto fail_syntax;
736 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
737 fail_reason = "Bad host address";
738 goto fail_syntax;
741 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
742 fail_reason = "Bad host port separator";
743 goto fail_syntax;
745 host_port = strtol(buf, &end, 0);
746 if (*end != '\0' || host_port < 0 || host_port > 65535) {
747 fail_reason = "Bad host port";
748 goto fail_syntax;
751 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
752 fail_reason = "Missing guest address";
753 goto fail_syntax;
755 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
756 fail_reason = "Bad guest address";
757 goto fail_syntax;
760 guest_port = strtol(p, &end, 0);
761 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
762 fail_reason = "Bad guest port";
763 goto fail_syntax;
766 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
767 guest_port) < 0) {
768 error_setg(errp, "Could not set up host forwarding rule '%s'",
769 redir_str);
770 return -1;
772 return 0;
774 fail_syntax:
775 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
776 fail_reason);
777 return -1;
780 void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
782 const char *redir_str;
783 SlirpState *s;
784 const char *arg1 = qdict_get_str(qdict, "arg1");
785 const char *arg2 = qdict_get_try_str(qdict, "arg2");
786 const char *arg3 = qdict_get_try_str(qdict, "arg3");
788 if (arg3) {
789 s = slirp_lookup(mon, arg1, arg2);
790 redir_str = arg3;
791 } else if (arg2) {
792 s = slirp_lookup(mon, NULL, arg1);
793 redir_str = arg2;
794 } else {
795 s = slirp_lookup(mon, NULL, NULL);
796 redir_str = arg1;
798 if (s) {
799 Error *err = NULL;
800 if (slirp_hostfwd(s, redir_str, &err) < 0) {
801 error_report_err(err);
807 #ifndef _WIN32
809 /* automatic user mode samba server configuration */
810 static void slirp_smb_cleanup(SlirpState *s)
812 int ret;
814 if (s->smb_dir) {
815 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
816 ret = system(cmd);
817 if (ret == -1 || !WIFEXITED(ret)) {
818 error_report("'%s' failed.", cmd);
819 } else if (WEXITSTATUS(ret)) {
820 error_report("'%s' failed. Error code: %d",
821 cmd, WEXITSTATUS(ret));
823 g_free(cmd);
824 g_free(s->smb_dir);
825 s->smb_dir = NULL;
829 static int slirp_smb(SlirpState* s, const char *exported_dir,
830 struct in_addr vserver_addr, Error **errp)
832 char *smb_conf;
833 char *smb_cmdline;
834 struct passwd *passwd;
835 FILE *f;
837 passwd = getpwuid(geteuid());
838 if (!passwd) {
839 error_setg(errp, "Failed to retrieve user name");
840 return -1;
843 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
844 error_setg(errp, "Could not find '%s', please install it",
845 CONFIG_SMBD_COMMAND);
846 return -1;
849 if (access(exported_dir, R_OK | X_OK)) {
850 error_setg(errp, "Error accessing shared directory '%s': %s",
851 exported_dir, strerror(errno));
852 return -1;
855 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
856 if (!s->smb_dir) {
857 error_setg(errp, "Could not create samba server dir");
858 return -1;
860 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
862 f = fopen(smb_conf, "w");
863 if (!f) {
864 slirp_smb_cleanup(s);
865 error_setg(errp,
866 "Could not create samba server configuration file '%s'",
867 smb_conf);
868 g_free(smb_conf);
869 return -1;
871 fprintf(f,
872 "[global]\n"
873 "private dir=%s\n"
874 "interfaces=127.0.0.1\n"
875 "bind interfaces only=yes\n"
876 "pid directory=%s\n"
877 "lock directory=%s\n"
878 "state directory=%s\n"
879 "cache directory=%s\n"
880 "ncalrpc dir=%s/ncalrpc\n"
881 "log file=%s/log.smbd\n"
882 "smb passwd file=%s/smbpasswd\n"
883 "security = user\n"
884 "map to guest = Bad User\n"
885 "load printers = no\n"
886 "printing = bsd\n"
887 "disable spoolss = yes\n"
888 "usershare max shares = 0\n"
889 "[qemu]\n"
890 "path=%s\n"
891 "read only=no\n"
892 "guest ok=yes\n"
893 "force user=%s\n",
894 s->smb_dir,
895 s->smb_dir,
896 s->smb_dir,
897 s->smb_dir,
898 s->smb_dir,
899 s->smb_dir,
900 s->smb_dir,
901 s->smb_dir,
902 exported_dir,
903 passwd->pw_name
905 fclose(f);
907 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
908 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
909 g_free(smb_conf);
911 if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 ||
912 slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) {
913 slirp_smb_cleanup(s);
914 g_free(smb_cmdline);
915 error_setg(errp, "Conflicting/invalid smbserver address");
916 return -1;
918 g_free(smb_cmdline);
919 return 0;
922 #endif /* !defined(_WIN32) */
924 static int guestfwd_can_read(void *opaque)
926 struct GuestFwd *fwd = opaque;
927 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
930 static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
932 struct GuestFwd *fwd = opaque;
933 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
936 static ssize_t guestfwd_write(const void *buf, size_t len, void *chr)
938 return qemu_chr_fe_write_all(chr, buf, len);
941 static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
943 /* TODO: IPv6 */
944 struct in_addr server = { .s_addr = 0 };
945 struct GuestFwd *fwd;
946 const char *p;
947 char buf[128];
948 char *end;
949 int port;
951 p = config_str;
952 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
953 goto fail_syntax;
955 if (strcmp(buf, "tcp") && buf[0] != '\0') {
956 goto fail_syntax;
958 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
959 goto fail_syntax;
961 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
962 goto fail_syntax;
964 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
965 goto fail_syntax;
967 port = strtol(buf, &end, 10);
968 if (*end != '\0' || port < 1 || port > 65535) {
969 goto fail_syntax;
972 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
974 if (g_str_has_prefix(p, "cmd:")) {
975 if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
976 error_setg(errp, "Conflicting/invalid host:port in guest "
977 "forwarding rule '%s'", config_str);
978 return -1;
980 } else {
981 Error *err = NULL;
983 * FIXME: sure we want to support implicit
984 * muxed monitors here?
986 Chardev *chr = qemu_chr_new_mux_mon(buf, p, NULL);
988 if (!chr) {
989 error_setg(errp, "Could not open guest forwarding device '%s'",
990 buf);
991 return -1;
994 fwd = g_new(struct GuestFwd, 1);
995 qemu_chr_fe_init(&fwd->hd, chr, &err);
996 if (err) {
997 error_propagate(errp, err);
998 object_unparent(OBJECT(chr));
999 g_free(fwd);
1000 return -1;
1003 if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd,
1004 &server, port) < 0) {
1005 error_setg(errp, "Conflicting/invalid host:port in guest "
1006 "forwarding rule '%s'", config_str);
1007 qemu_chr_fe_deinit(&fwd->hd, true);
1008 g_free(fwd);
1009 return -1;
1011 fwd->server = server;
1012 fwd->port = port;
1013 fwd->slirp = s->slirp;
1015 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
1016 NULL, NULL, fwd, NULL, true);
1017 s->fwd = g_slist_append(s->fwd, fwd);
1019 return 0;
1021 fail_syntax:
1022 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
1023 return -1;
1026 void hmp_info_usernet(Monitor *mon, const QDict *qdict)
1028 SlirpState *s;
1030 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
1031 int id;
1032 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
1033 char *info = slirp_connection_info(s->slirp);
1034 monitor_printf(mon, "Hub %d (%s):\n%s",
1035 got_hub_id ? id : -1,
1036 s->nc.name, info);
1037 g_free(info);
1041 static void
1042 net_init_slirp_configs(const StringList *fwd, int flags)
1044 while (fwd) {
1045 struct slirp_config_str *config;
1047 config = g_malloc0(sizeof(*config));
1048 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
1049 config->flags = flags;
1050 config->next = slirp_configs;
1051 slirp_configs = config;
1053 fwd = fwd->next;
1057 static const char **slirp_dnssearch(const StringList *dnsname)
1059 const StringList *c = dnsname;
1060 size_t i = 0, num_opts = 0;
1061 const char **ret;
1063 while (c) {
1064 num_opts++;
1065 c = c->next;
1068 if (num_opts == 0) {
1069 return NULL;
1072 ret = g_malloc((num_opts + 1) * sizeof(*ret));
1073 c = dnsname;
1074 while (c) {
1075 ret[i++] = c->value->str;
1076 c = c->next;
1078 ret[i] = NULL;
1079 return ret;
1082 int net_init_slirp(const Netdev *netdev, const char *name,
1083 NetClientState *peer, Error **errp)
1085 struct slirp_config_str *config;
1086 char *vnet;
1087 int ret;
1088 const NetdevUserOptions *user;
1089 const char **dnssearch;
1090 bool ipv4 = true, ipv6 = true;
1092 assert(netdev->type == NET_CLIENT_DRIVER_USER);
1093 user = &netdev->u.user;
1095 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
1096 (user->has_ipv4 && !user->ipv4)) {
1097 ipv4 = 0;
1099 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
1100 (user->has_ipv6 && !user->ipv6)) {
1101 ipv6 = 0;
1104 vnet = user->has_net ? g_strdup(user->net) :
1105 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
1106 NULL;
1108 dnssearch = slirp_dnssearch(user->dnssearch);
1110 /* all optional fields are initialized to "all bits zero" */
1112 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
1113 net_init_slirp_configs(user->guestfwd, 0);
1115 ret = net_slirp_init(peer, "user", name, user->q_restrict,
1116 ipv4, vnet, user->host,
1117 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
1118 user->ipv6_host, user->hostname, user->tftp,
1119 user->bootfile, user->dhcpstart,
1120 user->dns, user->ipv6_dns, user->smb,
1121 user->smbserver, dnssearch, user->domainname,
1122 user->tftp_server_name, errp);
1124 while (slirp_configs) {
1125 config = slirp_configs;
1126 slirp_configs = config->next;
1127 g_free(config);
1130 g_free(vnet);
1131 g_free(dnssearch);
1133 return ret;