qtest: log QEMU command line
[qemu/ar7.git] / net / slirp.c
blobf6dc03963af77afb30a0665f671c9ddf923d0f3f
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 "net/slirp.h"
29 #ifndef _WIN32
30 #include <pwd.h>
31 #include <sys/wait.h>
32 #endif
33 #include "net/net.h"
34 #include "clients.h"
35 #include "hub.h"
36 #include "monitor/monitor.h"
37 #include "qemu/error-report.h"
38 #include "qemu/sockets.h"
39 #include "slirp/libslirp.h"
40 #include "slirp/ip6.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"
47 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
49 const char *p, *p1;
50 int len;
51 p = *pp;
52 p1 = strchr(p, sep);
53 if (!p1)
54 return -1;
55 len = p1 - p;
56 p1++;
57 if (buf_size > 0) {
58 if (len > buf_size - 1)
59 len = buf_size - 1;
60 memcpy(buf, p, len);
61 buf[len] = '\0';
63 *pp = p1;
64 return 0;
67 /* slirp network adapter */
69 #define SLIRP_CFG_HOSTFWD 1
71 struct slirp_config_str {
72 struct slirp_config_str *next;
73 int flags;
74 char str[1024];
77 typedef struct SlirpState {
78 NetClientState nc;
79 QTAILQ_ENTRY(SlirpState) entry;
80 Slirp *slirp;
81 Notifier exit_notifier;
82 #ifndef _WIN32
83 gchar *smb_dir;
84 #endif
85 } SlirpState;
87 static struct slirp_config_str *slirp_configs;
88 static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
89 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
91 static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
92 static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
94 #ifndef _WIN32
95 static int slirp_smb(SlirpState *s, const char *exported_dir,
96 struct in_addr vserver_addr, Error **errp);
97 static void slirp_smb_cleanup(SlirpState *s);
98 #else
99 static inline void slirp_smb_cleanup(SlirpState *s) { }
100 #endif
102 void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
104 SlirpState *s = opaque;
106 qemu_send_packet(&s->nc, pkt, pkt_len);
109 static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
111 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
113 slirp_input(s->slirp, buf, size);
115 return size;
118 static void slirp_smb_exit(Notifier *n, void *data)
120 SlirpState *s = container_of(n, SlirpState, exit_notifier);
121 slirp_smb_cleanup(s);
124 static void net_slirp_cleanup(NetClientState *nc)
126 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
128 slirp_cleanup(s->slirp);
129 if (s->exit_notifier.notify) {
130 qemu_remove_exit_notifier(&s->exit_notifier);
132 slirp_smb_cleanup(s);
133 QTAILQ_REMOVE(&slirp_stacks, s, entry);
136 static NetClientInfo net_slirp_info = {
137 .type = NET_CLIENT_DRIVER_USER,
138 .size = sizeof(SlirpState),
139 .receive = net_slirp_receive,
140 .cleanup = net_slirp_cleanup,
143 static int net_slirp_init(NetClientState *peer, const char *model,
144 const char *name, int restricted,
145 bool ipv4, const char *vnetwork, const char *vhost,
146 bool ipv6, const char *vprefix6, int vprefix6_len,
147 const char *vhost6,
148 const char *vhostname, const char *tftp_export,
149 const char *bootfile, const char *vdhcp_start,
150 const char *vnameserver, const char *vnameserver6,
151 const char *smb_export, const char *vsmbserver,
152 const char **dnssearch, const char *vdomainname,
153 const char *tftp_server_name,
154 Error **errp)
156 /* default settings according to historic slirp */
157 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
158 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
159 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
160 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
161 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
162 struct in6_addr ip6_prefix;
163 struct in6_addr ip6_host;
164 struct in6_addr ip6_dns;
165 #ifndef _WIN32
166 struct in_addr smbsrv = { .s_addr = 0 };
167 #endif
168 NetClientState *nc;
169 SlirpState *s;
170 char buf[20];
171 uint32_t addr;
172 int shift;
173 char *end;
174 struct slirp_config_str *config;
176 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
177 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
178 return -1;
181 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
182 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
183 return -1;
186 if (!ipv4 && !ipv6) {
187 /* It doesn't make sense to disable both */
188 error_setg(errp, "IPv4 and IPv6 disabled");
189 return -1;
192 if (vnetwork) {
193 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
194 if (!inet_aton(vnetwork, &net)) {
195 error_setg(errp, "Failed to parse netmask");
196 return -1;
198 addr = ntohl(net.s_addr);
199 if (!(addr & 0x80000000)) {
200 mask.s_addr = htonl(0xff000000); /* class A */
201 } else if ((addr & 0xfff00000) == 0xac100000) {
202 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
203 } else if ((addr & 0xc0000000) == 0x80000000) {
204 mask.s_addr = htonl(0xffff0000); /* class B */
205 } else if ((addr & 0xffff0000) == 0xc0a80000) {
206 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
207 } else if ((addr & 0xffff0000) == 0xc6120000) {
208 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
209 } else if ((addr & 0xe0000000) == 0xe0000000) {
210 mask.s_addr = htonl(0xffffff00); /* class C */
211 } else {
212 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
214 } else {
215 if (!inet_aton(buf, &net)) {
216 error_setg(errp, "Failed to parse netmask");
217 return -1;
219 shift = strtol(vnetwork, &end, 10);
220 if (*end != '\0') {
221 if (!inet_aton(vnetwork, &mask)) {
222 error_setg(errp,
223 "Failed to parse netmask (trailing chars)");
224 return -1;
226 } else if (shift < 4 || shift > 32) {
227 error_setg(errp,
228 "Invalid netmask provided (must be in range 4-32)");
229 return -1;
230 } else {
231 mask.s_addr = htonl(0xffffffff << (32 - shift));
234 net.s_addr &= mask.s_addr;
235 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
236 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
237 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
240 if (vhost && !inet_aton(vhost, &host)) {
241 error_setg(errp, "Failed to parse host");
242 return -1;
244 if ((host.s_addr & mask.s_addr) != net.s_addr) {
245 error_setg(errp, "Host doesn't belong to network");
246 return -1;
249 if (vnameserver && !inet_aton(vnameserver, &dns)) {
250 error_setg(errp, "Failed to parse DNS");
251 return -1;
253 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
254 error_setg(errp, "DNS doesn't belong to network");
255 return -1;
257 if (dns.s_addr == host.s_addr) {
258 error_setg(errp, "DNS must be different from host");
259 return -1;
262 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
263 error_setg(errp, "Failed to parse DHCP start address");
264 return -1;
266 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
267 error_setg(errp, "DHCP doesn't belong to network");
268 return -1;
270 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
271 error_setg(errp, "DNS must be different from host and DNS");
272 return -1;
275 #ifndef _WIN32
276 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
277 error_setg(errp, "Failed to parse SMB address");
278 return -1;
280 #endif
282 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
283 /* No inet_pton helper before Vista... */
284 if (vprefix6) {
285 /* Unsupported */
286 error_setg(errp, "IPv6 prefix not supported");
287 return -1;
289 memset(&ip6_prefix, 0, sizeof(ip6_prefix));
290 ip6_prefix.s6_addr[0] = 0xfe;
291 ip6_prefix.s6_addr[1] = 0xc0;
292 #else
293 if (!vprefix6) {
294 vprefix6 = "fec0::";
296 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
297 error_setg(errp, "Failed to parse IPv6 prefix");
298 return -1;
300 #endif
302 if (!vprefix6_len) {
303 vprefix6_len = 64;
305 if (vprefix6_len < 0 || vprefix6_len > 126) {
306 error_setg(errp,
307 "Invalid prefix provided (prefix len must be in range 0-126");
308 return -1;
311 if (vhost6) {
312 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
313 error_setg(errp, "IPv6 host not supported");
314 return -1;
315 #else
316 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
317 error_setg(errp, "Failed to parse IPv6 host");
318 return -1;
320 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
321 error_setg(errp, "IPv6 Host doesn't belong to network");
322 return -1;
324 #endif
325 } else {
326 ip6_host = ip6_prefix;
327 ip6_host.s6_addr[15] |= 2;
330 if (vnameserver6) {
331 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
332 error_setg(errp, "IPv6 DNS not supported");
333 return -1;
334 #else
335 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
336 error_setg(errp, "Failed to parse IPv6 DNS");
337 return -1;
339 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
340 error_setg(errp, "IPv6 DNS doesn't belong to network");
341 return -1;
343 #endif
344 } else {
345 ip6_dns = ip6_prefix;
346 ip6_dns.s6_addr[15] |= 3;
349 if (vdomainname && !*vdomainname) {
350 error_setg(errp, "'domainname' parameter cannot be empty");
351 return -1;
354 if (vdomainname && strlen(vdomainname) > 255) {
355 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
356 return -1;
359 if (vhostname && strlen(vhostname) > 255) {
360 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
361 return -1;
364 if (tftp_server_name && strlen(tftp_server_name) > 255) {
365 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
366 return -1;
369 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
371 snprintf(nc->info_str, sizeof(nc->info_str),
372 "net=%s,restrict=%s", inet_ntoa(net),
373 restricted ? "on" : "off");
375 s = DO_UPCAST(SlirpState, nc, nc);
377 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
378 ipv6, ip6_prefix, vprefix6_len, ip6_host,
379 vhostname, tftp_server_name,
380 tftp_export, bootfile, dhcp,
381 dns, ip6_dns, dnssearch, vdomainname, s);
382 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
384 for (config = slirp_configs; config; config = config->next) {
385 if (config->flags & SLIRP_CFG_HOSTFWD) {
386 if (slirp_hostfwd(s, config->str, errp) < 0) {
387 goto error;
389 } else {
390 if (slirp_guestfwd(s, config->str, errp) < 0) {
391 goto error;
395 #ifndef _WIN32
396 if (smb_export) {
397 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
398 goto error;
401 #endif
403 s->exit_notifier.notify = slirp_smb_exit;
404 qemu_add_exit_notifier(&s->exit_notifier);
405 return 0;
407 error:
408 qemu_del_net_client(nc);
409 return -1;
412 static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
413 const char *name)
415 if (name) {
416 NetClientState *nc;
417 if (hub_id) {
418 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
419 if (!nc) {
420 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
421 return NULL;
423 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
424 "directly instead");
425 } else {
426 nc = qemu_find_netdev(name);
427 if (!nc) {
428 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
429 return NULL;
432 if (strcmp(nc->model, "user")) {
433 monitor_printf(mon, "invalid device specified\n");
434 return NULL;
436 return DO_UPCAST(SlirpState, nc, nc);
437 } else {
438 if (QTAILQ_EMPTY(&slirp_stacks)) {
439 monitor_printf(mon, "user mode network stack not in use\n");
440 return NULL;
442 return QTAILQ_FIRST(&slirp_stacks);
446 void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
448 struct in_addr host_addr = { .s_addr = INADDR_ANY };
449 int host_port;
450 char buf[256];
451 const char *src_str, *p;
452 SlirpState *s;
453 int is_udp = 0;
454 int err;
455 const char *arg1 = qdict_get_str(qdict, "arg1");
456 const char *arg2 = qdict_get_try_str(qdict, "arg2");
457 const char *arg3 = qdict_get_try_str(qdict, "arg3");
459 if (arg3) {
460 s = slirp_lookup(mon, arg1, arg2);
461 src_str = arg3;
462 } else if (arg2) {
463 s = slirp_lookup(mon, NULL, arg1);
464 src_str = arg2;
465 } else {
466 s = slirp_lookup(mon, NULL, NULL);
467 src_str = arg1;
469 if (!s) {
470 return;
473 p = src_str;
474 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
475 goto fail_syntax;
478 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
479 is_udp = 0;
480 } else if (!strcmp(buf, "udp")) {
481 is_udp = 1;
482 } else {
483 goto fail_syntax;
486 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
487 goto fail_syntax;
489 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
490 goto fail_syntax;
493 if (qemu_strtoi(p, NULL, 10, &host_port)) {
494 goto fail_syntax;
497 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
499 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
500 err ? "not found" : "removed");
501 return;
503 fail_syntax:
504 monitor_printf(mon, "invalid format\n");
507 static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
509 struct in_addr host_addr = { .s_addr = INADDR_ANY };
510 struct in_addr guest_addr = { .s_addr = 0 };
511 int host_port, guest_port;
512 const char *p;
513 char buf[256];
514 int is_udp;
515 char *end;
516 const char *fail_reason = "Unknown reason";
518 p = redir_str;
519 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
520 fail_reason = "No : separators";
521 goto fail_syntax;
523 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
524 is_udp = 0;
525 } else if (!strcmp(buf, "udp")) {
526 is_udp = 1;
527 } else {
528 fail_reason = "Bad protocol name";
529 goto fail_syntax;
532 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
533 fail_reason = "Missing : separator";
534 goto fail_syntax;
536 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
537 fail_reason = "Bad host address";
538 goto fail_syntax;
541 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
542 fail_reason = "Bad host port separator";
543 goto fail_syntax;
545 host_port = strtol(buf, &end, 0);
546 if (*end != '\0' || host_port < 0 || host_port > 65535) {
547 fail_reason = "Bad host port";
548 goto fail_syntax;
551 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
552 fail_reason = "Missing guest address";
553 goto fail_syntax;
555 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
556 fail_reason = "Bad guest address";
557 goto fail_syntax;
560 guest_port = strtol(p, &end, 0);
561 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
562 fail_reason = "Bad guest port";
563 goto fail_syntax;
566 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
567 guest_port) < 0) {
568 error_setg(errp, "Could not set up host forwarding rule '%s'",
569 redir_str);
570 return -1;
572 return 0;
574 fail_syntax:
575 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
576 fail_reason);
577 return -1;
580 void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
582 const char *redir_str;
583 SlirpState *s;
584 const char *arg1 = qdict_get_str(qdict, "arg1");
585 const char *arg2 = qdict_get_try_str(qdict, "arg2");
586 const char *arg3 = qdict_get_try_str(qdict, "arg3");
588 if (arg3) {
589 s = slirp_lookup(mon, arg1, arg2);
590 redir_str = arg3;
591 } else if (arg2) {
592 s = slirp_lookup(mon, NULL, arg1);
593 redir_str = arg2;
594 } else {
595 s = slirp_lookup(mon, NULL, NULL);
596 redir_str = arg1;
598 if (s) {
599 Error *err = NULL;
600 if (slirp_hostfwd(s, redir_str, &err) < 0) {
601 error_report_err(err);
607 #ifndef _WIN32
609 /* automatic user mode samba server configuration */
610 static void slirp_smb_cleanup(SlirpState *s)
612 int ret;
614 if (s->smb_dir) {
615 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
616 ret = system(cmd);
617 if (ret == -1 || !WIFEXITED(ret)) {
618 error_report("'%s' failed.", cmd);
619 } else if (WEXITSTATUS(ret)) {
620 error_report("'%s' failed. Error code: %d",
621 cmd, WEXITSTATUS(ret));
623 g_free(cmd);
624 g_free(s->smb_dir);
625 s->smb_dir = NULL;
629 static int slirp_smb(SlirpState* s, const char *exported_dir,
630 struct in_addr vserver_addr, Error **errp)
632 char *smb_conf;
633 char *smb_cmdline;
634 struct passwd *passwd;
635 FILE *f;
637 passwd = getpwuid(geteuid());
638 if (!passwd) {
639 error_setg(errp, "Failed to retrieve user name");
640 return -1;
643 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
644 error_setg(errp, "Could not find '%s', please install it",
645 CONFIG_SMBD_COMMAND);
646 return -1;
649 if (access(exported_dir, R_OK | X_OK)) {
650 error_setg(errp, "Error accessing shared directory '%s': %s",
651 exported_dir, strerror(errno));
652 return -1;
655 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
656 if (!s->smb_dir) {
657 error_setg(errp, "Could not create samba server dir");
658 return -1;
660 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
662 f = fopen(smb_conf, "w");
663 if (!f) {
664 slirp_smb_cleanup(s);
665 error_setg(errp,
666 "Could not create samba server configuration file '%s'",
667 smb_conf);
668 g_free(smb_conf);
669 return -1;
671 fprintf(f,
672 "[global]\n"
673 "private dir=%s\n"
674 "interfaces=127.0.0.1\n"
675 "bind interfaces only=yes\n"
676 "pid directory=%s\n"
677 "lock directory=%s\n"
678 "state directory=%s\n"
679 "cache directory=%s\n"
680 "ncalrpc dir=%s/ncalrpc\n"
681 "log file=%s/log.smbd\n"
682 "smb passwd file=%s/smbpasswd\n"
683 "security = user\n"
684 "map to guest = Bad User\n"
685 "load printers = no\n"
686 "printing = bsd\n"
687 "disable spoolss = yes\n"
688 "usershare max shares = 0\n"
689 "[qemu]\n"
690 "path=%s\n"
691 "read only=no\n"
692 "guest ok=yes\n"
693 "force user=%s\n",
694 s->smb_dir,
695 s->smb_dir,
696 s->smb_dir,
697 s->smb_dir,
698 s->smb_dir,
699 s->smb_dir,
700 s->smb_dir,
701 s->smb_dir,
702 exported_dir,
703 passwd->pw_name
705 fclose(f);
707 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
708 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
709 g_free(smb_conf);
711 if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0 ||
712 slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 445) < 0) {
713 slirp_smb_cleanup(s);
714 g_free(smb_cmdline);
715 error_setg(errp, "Conflicting/invalid smbserver address");
716 return -1;
718 g_free(smb_cmdline);
719 return 0;
722 #endif /* !defined(_WIN32) */
724 struct GuestFwd {
725 CharBackend hd;
726 struct in_addr server;
727 int port;
728 Slirp *slirp;
731 static int guestfwd_can_read(void *opaque)
733 struct GuestFwd *fwd = opaque;
734 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
737 static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
739 struct GuestFwd *fwd = opaque;
740 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
743 static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
745 struct in_addr server = { .s_addr = 0 };
746 struct GuestFwd *fwd;
747 const char *p;
748 char buf[128];
749 char *end;
750 int port;
752 p = config_str;
753 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
754 goto fail_syntax;
756 if (strcmp(buf, "tcp") && buf[0] != '\0') {
757 goto fail_syntax;
759 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
760 goto fail_syntax;
762 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
763 goto fail_syntax;
765 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
766 goto fail_syntax;
768 port = strtol(buf, &end, 10);
769 if (*end != '\0' || port < 1 || port > 65535) {
770 goto fail_syntax;
773 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
775 if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
776 if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
777 error_setg(errp, "Conflicting/invalid host:port in guest "
778 "forwarding rule '%s'", config_str);
779 return -1;
781 } else {
782 Error *err = NULL;
784 * FIXME: sure we want to support implicit
785 * muxed monitors here?
787 Chardev *chr = qemu_chr_new_mux_mon(buf, p);
789 if (!chr) {
790 error_setg(errp, "Could not open guest forwarding device '%s'",
791 buf);
792 return -1;
795 fwd = g_new(struct GuestFwd, 1);
796 qemu_chr_fe_init(&fwd->hd, chr, &err);
797 if (err) {
798 error_propagate(errp, err);
799 g_free(fwd);
800 return -1;
803 if (slirp_add_exec(s->slirp, 3, &fwd->hd, &server, port) < 0) {
804 error_setg(errp, "Conflicting/invalid host:port in guest "
805 "forwarding rule '%s'", config_str);
806 g_free(fwd);
807 return -1;
809 fwd->server = server;
810 fwd->port = port;
811 fwd->slirp = s->slirp;
813 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
814 NULL, NULL, fwd, NULL, true);
816 return 0;
818 fail_syntax:
819 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
820 return -1;
823 void hmp_info_usernet(Monitor *mon, const QDict *qdict)
825 SlirpState *s;
827 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
828 int id;
829 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
830 monitor_printf(mon, "Hub %d (%s):\n",
831 got_hub_id ? id : -1,
832 s->nc.name);
833 slirp_connection_info(s->slirp, mon);
837 static void
838 net_init_slirp_configs(const StringList *fwd, int flags)
840 while (fwd) {
841 struct slirp_config_str *config;
843 config = g_malloc0(sizeof(*config));
844 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
845 config->flags = flags;
846 config->next = slirp_configs;
847 slirp_configs = config;
849 fwd = fwd->next;
853 static const char **slirp_dnssearch(const StringList *dnsname)
855 const StringList *c = dnsname;
856 size_t i = 0, num_opts = 0;
857 const char **ret;
859 while (c) {
860 num_opts++;
861 c = c->next;
864 if (num_opts == 0) {
865 return NULL;
868 ret = g_malloc((num_opts + 1) * sizeof(*ret));
869 c = dnsname;
870 while (c) {
871 ret[i++] = c->value->str;
872 c = c->next;
874 ret[i] = NULL;
875 return ret;
878 int net_init_slirp(const Netdev *netdev, const char *name,
879 NetClientState *peer, Error **errp)
881 struct slirp_config_str *config;
882 char *vnet;
883 int ret;
884 const NetdevUserOptions *user;
885 const char **dnssearch;
886 bool ipv4 = true, ipv6 = true;
888 assert(netdev->type == NET_CLIENT_DRIVER_USER);
889 user = &netdev->u.user;
891 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
892 (user->has_ipv4 && !user->ipv4)) {
893 ipv4 = 0;
895 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
896 (user->has_ipv6 && !user->ipv6)) {
897 ipv6 = 0;
900 vnet = user->has_net ? g_strdup(user->net) :
901 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
902 NULL;
904 dnssearch = slirp_dnssearch(user->dnssearch);
906 /* all optional fields are initialized to "all bits zero" */
908 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
909 net_init_slirp_configs(user->guestfwd, 0);
911 ret = net_slirp_init(peer, "user", name, user->q_restrict,
912 ipv4, vnet, user->host,
913 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
914 user->ipv6_host, user->hostname, user->tftp,
915 user->bootfile, user->dhcpstart,
916 user->dns, user->ipv6_dns, user->smb,
917 user->smbserver, dnssearch, user->domainname,
918 user->tftp_server_name, errp);
920 while (slirp_configs) {
921 config = slirp_configs;
922 slirp_configs = config->next;
923 g_free(config);
926 g_free(vnet);
927 g_free(dnssearch);
929 return ret;