migration: handle the error condition properly
[qemu/ar7.git] / net / slirp.c
blobc18060f7781b16b867ff2a87dbea76c9dfecc4d4
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 Error **errp)
155 /* default settings according to historic slirp */
156 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
157 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
158 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
159 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
160 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
161 struct in6_addr ip6_prefix;
162 struct in6_addr ip6_host;
163 struct in6_addr ip6_dns;
164 #ifndef _WIN32
165 struct in_addr smbsrv = { .s_addr = 0 };
166 #endif
167 NetClientState *nc;
168 SlirpState *s;
169 char buf[20];
170 uint32_t addr;
171 int shift;
172 char *end;
173 struct slirp_config_str *config;
175 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
176 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
177 return -1;
180 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
181 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
182 return -1;
185 if (!ipv4 && !ipv6) {
186 /* It doesn't make sense to disable both */
187 error_setg(errp, "IPv4 and IPv6 disabled");
188 return -1;
191 if (vnetwork) {
192 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
193 if (!inet_aton(vnetwork, &net)) {
194 error_setg(errp, "Failed to parse netmask");
195 return -1;
197 addr = ntohl(net.s_addr);
198 if (!(addr & 0x80000000)) {
199 mask.s_addr = htonl(0xff000000); /* class A */
200 } else if ((addr & 0xfff00000) == 0xac100000) {
201 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
202 } else if ((addr & 0xc0000000) == 0x80000000) {
203 mask.s_addr = htonl(0xffff0000); /* class B */
204 } else if ((addr & 0xffff0000) == 0xc0a80000) {
205 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
206 } else if ((addr & 0xffff0000) == 0xc6120000) {
207 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
208 } else if ((addr & 0xe0000000) == 0xe0000000) {
209 mask.s_addr = htonl(0xffffff00); /* class C */
210 } else {
211 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
213 } else {
214 if (!inet_aton(buf, &net)) {
215 error_setg(errp, "Failed to parse netmask");
216 return -1;
218 shift = strtol(vnetwork, &end, 10);
219 if (*end != '\0') {
220 if (!inet_aton(vnetwork, &mask)) {
221 error_setg(errp,
222 "Failed to parse netmask (trailing chars)");
223 return -1;
225 } else if (shift < 4 || shift > 32) {
226 error_setg(errp,
227 "Invalid netmask provided (must be in range 4-32)");
228 return -1;
229 } else {
230 mask.s_addr = htonl(0xffffffff << (32 - shift));
233 net.s_addr &= mask.s_addr;
234 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
235 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
236 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
239 if (vhost && !inet_aton(vhost, &host)) {
240 error_setg(errp, "Failed to parse host");
241 return -1;
243 if ((host.s_addr & mask.s_addr) != net.s_addr) {
244 error_setg(errp, "Host doesn't belong to network");
245 return -1;
248 if (vnameserver && !inet_aton(vnameserver, &dns)) {
249 error_setg(errp, "Failed to parse DNS");
250 return -1;
252 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
253 error_setg(errp, "DNS doesn't belong to network");
254 return -1;
256 if (dns.s_addr == host.s_addr) {
257 error_setg(errp, "DNS must be different from host");
258 return -1;
261 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
262 error_setg(errp, "Failed to parse DHCP start address");
263 return -1;
265 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
266 error_setg(errp, "DHCP doesn't belong to network");
267 return -1;
269 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
270 error_setg(errp, "DNS must be different from host and DNS");
271 return -1;
274 #ifndef _WIN32
275 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
276 error_setg(errp, "Failed to parse SMB address");
277 return -1;
279 #endif
281 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
282 /* No inet_pton helper before Vista... */
283 if (vprefix6) {
284 /* Unsupported */
285 error_setg(errp, "IPv6 prefix not supported");
286 return -1;
288 memset(&ip6_prefix, 0, sizeof(ip6_prefix));
289 ip6_prefix.s6_addr[0] = 0xfe;
290 ip6_prefix.s6_addr[1] = 0xc0;
291 #else
292 if (!vprefix6) {
293 vprefix6 = "fec0::";
295 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
296 error_setg(errp, "Failed to parse IPv6 prefix");
297 return -1;
299 #endif
301 if (!vprefix6_len) {
302 vprefix6_len = 64;
304 if (vprefix6_len < 0 || vprefix6_len > 126) {
305 error_setg(errp,
306 "Invalid prefix provided (prefix len must be in range 0-126");
307 return -1;
310 if (vhost6) {
311 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
312 error_setg(errp, "IPv6 host not supported");
313 return -1;
314 #else
315 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
316 error_setg(errp, "Failed to parse IPv6 host");
317 return -1;
319 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
320 error_setg(errp, "IPv6 Host doesn't belong to network");
321 return -1;
323 #endif
324 } else {
325 ip6_host = ip6_prefix;
326 ip6_host.s6_addr[15] |= 2;
329 if (vnameserver6) {
330 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
331 error_setg(errp, "IPv6 DNS not supported");
332 return -1;
333 #else
334 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
335 error_setg(errp, "Failed to parse IPv6 DNS");
336 return -1;
338 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
339 error_setg(errp, "IPv6 DNS doesn't belong to network");
340 return -1;
342 #endif
343 } else {
344 ip6_dns = ip6_prefix;
345 ip6_dns.s6_addr[15] |= 3;
348 if (vdomainname && !*vdomainname) {
349 error_setg(errp, "'domainname' parameter cannot be empty");
350 return -1;
354 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
356 snprintf(nc->info_str, sizeof(nc->info_str),
357 "net=%s,restrict=%s", inet_ntoa(net),
358 restricted ? "on" : "off");
360 s = DO_UPCAST(SlirpState, nc, nc);
362 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
363 ipv6, ip6_prefix, vprefix6_len, ip6_host,
364 vhostname, tftp_export, bootfile, dhcp,
365 dns, ip6_dns, dnssearch, vdomainname, s);
366 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
368 for (config = slirp_configs; config; config = config->next) {
369 if (config->flags & SLIRP_CFG_HOSTFWD) {
370 if (slirp_hostfwd(s, config->str, errp) < 0) {
371 goto error;
373 } else {
374 if (slirp_guestfwd(s, config->str, errp) < 0) {
375 goto error;
379 #ifndef _WIN32
380 if (smb_export) {
381 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
382 goto error;
385 #endif
387 s->exit_notifier.notify = slirp_smb_exit;
388 qemu_add_exit_notifier(&s->exit_notifier);
389 return 0;
391 error:
392 qemu_del_net_client(nc);
393 return -1;
396 static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
397 const char *name)
399 if (name) {
400 NetClientState *nc;
401 if (hub_id) {
402 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
403 if (!nc) {
404 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
405 return NULL;
407 } else {
408 nc = qemu_find_netdev(name);
409 if (!nc) {
410 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
411 return NULL;
414 if (strcmp(nc->model, "user")) {
415 monitor_printf(mon, "invalid device specified\n");
416 return NULL;
418 return DO_UPCAST(SlirpState, nc, nc);
419 } else {
420 if (QTAILQ_EMPTY(&slirp_stacks)) {
421 monitor_printf(mon, "user mode network stack not in use\n");
422 return NULL;
424 return QTAILQ_FIRST(&slirp_stacks);
428 void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
430 struct in_addr host_addr = { .s_addr = INADDR_ANY };
431 int host_port;
432 char buf[256];
433 const char *src_str, *p;
434 SlirpState *s;
435 int is_udp = 0;
436 int err;
437 const char *arg1 = qdict_get_str(qdict, "arg1");
438 const char *arg2 = qdict_get_try_str(qdict, "arg2");
439 const char *arg3 = qdict_get_try_str(qdict, "arg3");
441 if (arg3) {
442 s = slirp_lookup(mon, arg1, arg2);
443 src_str = arg3;
444 } else if (arg2) {
445 s = slirp_lookup(mon, NULL, arg1);
446 src_str = arg2;
447 } else {
448 s = slirp_lookup(mon, NULL, NULL);
449 src_str = arg1;
451 if (!s) {
452 return;
455 p = src_str;
456 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
457 goto fail_syntax;
460 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
461 is_udp = 0;
462 } else if (!strcmp(buf, "udp")) {
463 is_udp = 1;
464 } else {
465 goto fail_syntax;
468 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
469 goto fail_syntax;
471 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
472 goto fail_syntax;
475 if (qemu_strtoi(p, NULL, 10, &host_port)) {
476 goto fail_syntax;
479 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
481 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
482 err ? "not found" : "removed");
483 return;
485 fail_syntax:
486 monitor_printf(mon, "invalid format\n");
489 static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
491 struct in_addr host_addr = { .s_addr = INADDR_ANY };
492 struct in_addr guest_addr = { .s_addr = 0 };
493 int host_port, guest_port;
494 const char *p;
495 char buf[256];
496 int is_udp;
497 char *end;
498 const char *fail_reason = "Unknown reason";
500 p = redir_str;
501 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
502 fail_reason = "No : separators";
503 goto fail_syntax;
505 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
506 is_udp = 0;
507 } else if (!strcmp(buf, "udp")) {
508 is_udp = 1;
509 } else {
510 fail_reason = "Bad protocol name";
511 goto fail_syntax;
514 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
515 fail_reason = "Missing : separator";
516 goto fail_syntax;
518 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
519 fail_reason = "Bad host address";
520 goto fail_syntax;
523 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
524 fail_reason = "Bad host port separator";
525 goto fail_syntax;
527 host_port = strtol(buf, &end, 0);
528 if (*end != '\0' || host_port < 0 || host_port > 65535) {
529 fail_reason = "Bad host port";
530 goto fail_syntax;
533 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
534 fail_reason = "Missing guest address";
535 goto fail_syntax;
537 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
538 fail_reason = "Bad guest address";
539 goto fail_syntax;
542 guest_port = strtol(p, &end, 0);
543 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
544 fail_reason = "Bad guest port";
545 goto fail_syntax;
548 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
549 guest_port) < 0) {
550 error_setg(errp, "Could not set up host forwarding rule '%s'",
551 redir_str);
552 return -1;
554 return 0;
556 fail_syntax:
557 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
558 fail_reason);
559 return -1;
562 void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
564 const char *redir_str;
565 SlirpState *s;
566 const char *arg1 = qdict_get_str(qdict, "arg1");
567 const char *arg2 = qdict_get_try_str(qdict, "arg2");
568 const char *arg3 = qdict_get_try_str(qdict, "arg3");
570 if (arg3) {
571 s = slirp_lookup(mon, arg1, arg2);
572 redir_str = arg3;
573 } else if (arg2) {
574 s = slirp_lookup(mon, NULL, arg1);
575 redir_str = arg2;
576 } else {
577 s = slirp_lookup(mon, NULL, NULL);
578 redir_str = arg1;
580 if (s) {
581 Error *err = NULL;
582 if (slirp_hostfwd(s, redir_str, &err) < 0) {
583 error_report_err(err);
589 #ifndef _WIN32
591 /* automatic user mode samba server configuration */
592 static void slirp_smb_cleanup(SlirpState *s)
594 int ret;
596 if (s->smb_dir) {
597 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
598 ret = system(cmd);
599 if (ret == -1 || !WIFEXITED(ret)) {
600 error_report("'%s' failed.", cmd);
601 } else if (WEXITSTATUS(ret)) {
602 error_report("'%s' failed. Error code: %d",
603 cmd, WEXITSTATUS(ret));
605 g_free(cmd);
606 g_free(s->smb_dir);
607 s->smb_dir = NULL;
611 static int slirp_smb(SlirpState* s, const char *exported_dir,
612 struct in_addr vserver_addr, Error **errp)
614 char *smb_conf;
615 char *smb_cmdline;
616 struct passwd *passwd;
617 FILE *f;
619 passwd = getpwuid(geteuid());
620 if (!passwd) {
621 error_setg(errp, "Failed to retrieve user name");
622 return -1;
625 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
626 error_setg(errp, "Could not find '%s', please install it",
627 CONFIG_SMBD_COMMAND);
628 return -1;
631 if (access(exported_dir, R_OK | X_OK)) {
632 error_setg(errp, "Error accessing shared directory '%s': %s",
633 exported_dir, strerror(errno));
634 return -1;
637 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
638 if (!s->smb_dir) {
639 error_setg(errp, "Could not create samba server dir");
640 return -1;
642 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
644 f = fopen(smb_conf, "w");
645 if (!f) {
646 slirp_smb_cleanup(s);
647 error_setg(errp,
648 "Could not create samba server configuration file '%s'",
649 smb_conf);
650 g_free(smb_conf);
651 return -1;
653 fprintf(f,
654 "[global]\n"
655 "private dir=%s\n"
656 "interfaces=127.0.0.1\n"
657 "bind interfaces only=yes\n"
658 "pid directory=%s\n"
659 "lock directory=%s\n"
660 "state directory=%s\n"
661 "cache directory=%s\n"
662 "ncalrpc dir=%s/ncalrpc\n"
663 "log file=%s/log.smbd\n"
664 "smb passwd file=%s/smbpasswd\n"
665 "security = user\n"
666 "map to guest = Bad User\n"
667 "load printers = no\n"
668 "printing = bsd\n"
669 "disable spoolss = yes\n"
670 "usershare max shares = 0\n"
671 "[qemu]\n"
672 "path=%s\n"
673 "read only=no\n"
674 "guest ok=yes\n"
675 "force user=%s\n",
676 s->smb_dir,
677 s->smb_dir,
678 s->smb_dir,
679 s->smb_dir,
680 s->smb_dir,
681 s->smb_dir,
682 s->smb_dir,
683 s->smb_dir,
684 exported_dir,
685 passwd->pw_name
687 fclose(f);
689 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
690 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
691 g_free(smb_conf);
693 if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0 ||
694 slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 445) < 0) {
695 slirp_smb_cleanup(s);
696 g_free(smb_cmdline);
697 error_setg(errp, "Conflicting/invalid smbserver address");
698 return -1;
700 g_free(smb_cmdline);
701 return 0;
704 #endif /* !defined(_WIN32) */
706 struct GuestFwd {
707 CharBackend hd;
708 struct in_addr server;
709 int port;
710 Slirp *slirp;
713 static int guestfwd_can_read(void *opaque)
715 struct GuestFwd *fwd = opaque;
716 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
719 static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
721 struct GuestFwd *fwd = opaque;
722 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
725 static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
727 struct in_addr server = { .s_addr = 0 };
728 struct GuestFwd *fwd;
729 const char *p;
730 char buf[128];
731 char *end;
732 int port;
734 p = config_str;
735 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
736 goto fail_syntax;
738 if (strcmp(buf, "tcp") && buf[0] != '\0') {
739 goto fail_syntax;
741 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
742 goto fail_syntax;
744 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
745 goto fail_syntax;
747 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
748 goto fail_syntax;
750 port = strtol(buf, &end, 10);
751 if (*end != '\0' || port < 1 || port > 65535) {
752 goto fail_syntax;
755 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
757 if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
758 if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
759 error_setg(errp, "Conflicting/invalid host:port in guest "
760 "forwarding rule '%s'", config_str);
761 return -1;
763 } else {
764 Error *err = NULL;
765 Chardev *chr = qemu_chr_new(buf, p);
767 if (!chr) {
768 error_setg(errp, "Could not open guest forwarding device '%s'",
769 buf);
770 return -1;
773 fwd = g_new(struct GuestFwd, 1);
774 qemu_chr_fe_init(&fwd->hd, chr, &err);
775 if (err) {
776 error_propagate(errp, err);
777 g_free(fwd);
778 return -1;
781 if (slirp_add_exec(s->slirp, 3, &fwd->hd, &server, port) < 0) {
782 error_setg(errp, "Conflicting/invalid host:port in guest "
783 "forwarding rule '%s'", config_str);
784 g_free(fwd);
785 return -1;
787 fwd->server = server;
788 fwd->port = port;
789 fwd->slirp = s->slirp;
791 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
792 NULL, NULL, fwd, NULL, true);
794 return 0;
796 fail_syntax:
797 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
798 return -1;
801 void hmp_info_usernet(Monitor *mon, const QDict *qdict)
803 SlirpState *s;
805 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
806 int id;
807 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
808 monitor_printf(mon, "Hub %d (%s):\n",
809 got_hub_id ? id : -1,
810 s->nc.name);
811 slirp_connection_info(s->slirp, mon);
815 static void
816 net_init_slirp_configs(const StringList *fwd, int flags)
818 while (fwd) {
819 struct slirp_config_str *config;
821 config = g_malloc0(sizeof(*config));
822 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
823 config->flags = flags;
824 config->next = slirp_configs;
825 slirp_configs = config;
827 fwd = fwd->next;
831 static const char **slirp_dnssearch(const StringList *dnsname)
833 const StringList *c = dnsname;
834 size_t i = 0, num_opts = 0;
835 const char **ret;
837 while (c) {
838 num_opts++;
839 c = c->next;
842 if (num_opts == 0) {
843 return NULL;
846 ret = g_malloc((num_opts + 1) * sizeof(*ret));
847 c = dnsname;
848 while (c) {
849 ret[i++] = c->value->str;
850 c = c->next;
852 ret[i] = NULL;
853 return ret;
856 int net_init_slirp(const Netdev *netdev, const char *name,
857 NetClientState *peer, Error **errp)
859 struct slirp_config_str *config;
860 char *vnet;
861 int ret;
862 const NetdevUserOptions *user;
863 const char **dnssearch;
864 bool ipv4 = true, ipv6 = true;
866 assert(netdev->type == NET_CLIENT_DRIVER_USER);
867 user = &netdev->u.user;
869 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
870 (user->has_ipv4 && !user->ipv4)) {
871 ipv4 = 0;
873 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
874 (user->has_ipv6 && !user->ipv6)) {
875 ipv6 = 0;
878 vnet = user->has_net ? g_strdup(user->net) :
879 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
880 NULL;
882 dnssearch = slirp_dnssearch(user->dnssearch);
884 /* all optional fields are initialized to "all bits zero" */
886 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
887 net_init_slirp_configs(user->guestfwd, 0);
889 ret = net_slirp_init(peer, "user", name, user->q_restrict,
890 ipv4, vnet, user->host,
891 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
892 user->ipv6_host, user->hostname, user->tftp,
893 user->bootfile, user->dhcpstart,
894 user->dns, user->ipv6_dns, user->smb,
895 user->smbserver, dnssearch, user->domainname, errp);
897 while (slirp_configs) {
898 config = slirp_configs;
899 slirp_configs = config->next;
900 g_free(config);
903 g_free(vnet);
904 g_free(dnssearch);
906 return ret;