slirp: Move g_spawn_async_with_fds_qemu compatibility to slirp/
[qemu/ar7.git] / net / slirp.c
blob7b4f9f5c5ee31c3208d4e8d8b8362fb8c1264555
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 "slirp/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"
48 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
50 const char *p, *p1;
51 int len;
52 p = *pp;
53 p1 = strchr(p, sep);
54 if (!p1)
55 return -1;
56 len = p1 - p;
57 p1++;
58 if (buf_size > 0) {
59 if (len > buf_size - 1)
60 len = buf_size - 1;
61 memcpy(buf, p, len);
62 buf[len] = '\0';
64 *pp = p1;
65 return 0;
68 /* slirp network adapter */
70 #define SLIRP_CFG_HOSTFWD 1
72 struct slirp_config_str {
73 struct slirp_config_str *next;
74 int flags;
75 char str[1024];
78 struct GuestFwd {
79 CharBackend hd;
80 struct in_addr server;
81 int port;
82 Slirp *slirp;
85 typedef struct SlirpState {
86 NetClientState nc;
87 QTAILQ_ENTRY(SlirpState) entry;
88 Slirp *slirp;
89 Notifier exit_notifier;
90 #ifndef _WIN32
91 gchar *smb_dir;
92 #endif
93 GSList *fwd;
94 } SlirpState;
96 static struct slirp_config_str *slirp_configs;
97 static QTAILQ_HEAD(, SlirpState) slirp_stacks =
98 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
100 static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
101 static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
103 #ifndef _WIN32
104 static int slirp_smb(SlirpState *s, const char *exported_dir,
105 struct in_addr vserver_addr, Error **errp);
106 static void slirp_smb_cleanup(SlirpState *s);
107 #else
108 static inline void slirp_smb_cleanup(SlirpState *s) { }
109 #endif
111 static void net_slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
113 SlirpState *s = opaque;
115 qemu_send_packet(&s->nc, pkt, pkt_len);
118 static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
120 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
122 slirp_input(s->slirp, buf, size);
124 return size;
127 static void slirp_smb_exit(Notifier *n, void *data)
129 SlirpState *s = container_of(n, SlirpState, exit_notifier);
130 slirp_smb_cleanup(s);
133 static void slirp_free_fwd(gpointer data)
135 struct GuestFwd *fwd = data;
137 qemu_chr_fe_deinit(&fwd->hd, true);
138 g_free(data);
141 static void net_slirp_cleanup(NetClientState *nc)
143 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
145 g_slist_free_full(s->fwd, slirp_free_fwd);
146 slirp_cleanup(s->slirp);
147 if (s->exit_notifier.notify) {
148 qemu_remove_exit_notifier(&s->exit_notifier);
150 slirp_smb_cleanup(s);
151 QTAILQ_REMOVE(&slirp_stacks, s, entry);
154 static NetClientInfo net_slirp_info = {
155 .type = NET_CLIENT_DRIVER_USER,
156 .size = sizeof(SlirpState),
157 .receive = net_slirp_receive,
158 .cleanup = net_slirp_cleanup,
161 static void net_slirp_guest_error(const char *msg)
163 qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
166 static int64_t net_slirp_clock_get_ns(void)
168 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
171 static void *net_slirp_timer_new(SlirpTimerCb cb, void *opaque)
173 return timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
174 SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
175 cb, opaque);
178 static void net_slirp_timer_free(void *timer)
180 timer_del(timer);
181 timer_free(timer);
184 static void net_slirp_timer_mod(void *timer, int64_t expire_timer)
186 timer_mod(timer, expire_timer);
189 static void net_slirp_register_poll_fd(int fd)
191 qemu_fd_register(fd);
194 static void net_slirp_unregister_poll_fd(int fd)
196 /* no qemu_fd_unregister */
199 static const SlirpCb slirp_cb = {
200 .output = net_slirp_output,
201 .guest_error = net_slirp_guest_error,
202 .clock_get_ns = net_slirp_clock_get_ns,
203 .timer_new = net_slirp_timer_new,
204 .timer_free = net_slirp_timer_free,
205 .timer_mod = net_slirp_timer_mod,
206 .register_poll_fd = net_slirp_register_poll_fd,
207 .unregister_poll_fd = net_slirp_unregister_poll_fd,
208 .notify = qemu_notify_event,
211 static int net_slirp_init(NetClientState *peer, const char *model,
212 const char *name, int restricted,
213 bool ipv4, const char *vnetwork, const char *vhost,
214 bool ipv6, const char *vprefix6, int vprefix6_len,
215 const char *vhost6,
216 const char *vhostname, const char *tftp_export,
217 const char *bootfile, const char *vdhcp_start,
218 const char *vnameserver, const char *vnameserver6,
219 const char *smb_export, const char *vsmbserver,
220 const char **dnssearch, const char *vdomainname,
221 const char *tftp_server_name,
222 Error **errp)
224 /* default settings according to historic slirp */
225 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
226 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
227 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
228 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
229 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
230 struct in6_addr ip6_prefix;
231 struct in6_addr ip6_host;
232 struct in6_addr ip6_dns;
233 #ifndef _WIN32
234 struct in_addr smbsrv = { .s_addr = 0 };
235 #endif
236 NetClientState *nc;
237 SlirpState *s;
238 char buf[20];
239 uint32_t addr;
240 int shift;
241 char *end;
242 struct slirp_config_str *config;
244 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
245 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
246 return -1;
249 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
250 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
251 return -1;
254 if (!ipv4 && !ipv6) {
255 /* It doesn't make sense to disable both */
256 error_setg(errp, "IPv4 and IPv6 disabled");
257 return -1;
260 if (vnetwork) {
261 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
262 if (!inet_aton(vnetwork, &net)) {
263 error_setg(errp, "Failed to parse netmask");
264 return -1;
266 addr = ntohl(net.s_addr);
267 if (!(addr & 0x80000000)) {
268 mask.s_addr = htonl(0xff000000); /* class A */
269 } else if ((addr & 0xfff00000) == 0xac100000) {
270 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
271 } else if ((addr & 0xc0000000) == 0x80000000) {
272 mask.s_addr = htonl(0xffff0000); /* class B */
273 } else if ((addr & 0xffff0000) == 0xc0a80000) {
274 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
275 } else if ((addr & 0xffff0000) == 0xc6120000) {
276 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
277 } else if ((addr & 0xe0000000) == 0xe0000000) {
278 mask.s_addr = htonl(0xffffff00); /* class C */
279 } else {
280 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
282 } else {
283 if (!inet_aton(buf, &net)) {
284 error_setg(errp, "Failed to parse netmask");
285 return -1;
287 shift = strtol(vnetwork, &end, 10);
288 if (*end != '\0') {
289 if (!inet_aton(vnetwork, &mask)) {
290 error_setg(errp,
291 "Failed to parse netmask (trailing chars)");
292 return -1;
294 } else if (shift < 4 || shift > 32) {
295 error_setg(errp,
296 "Invalid netmask provided (must be in range 4-32)");
297 return -1;
298 } else {
299 mask.s_addr = htonl(0xffffffff << (32 - shift));
302 net.s_addr &= mask.s_addr;
303 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
304 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
305 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
308 if (vhost && !inet_aton(vhost, &host)) {
309 error_setg(errp, "Failed to parse host");
310 return -1;
312 if ((host.s_addr & mask.s_addr) != net.s_addr) {
313 error_setg(errp, "Host doesn't belong to network");
314 return -1;
317 if (vnameserver && !inet_aton(vnameserver, &dns)) {
318 error_setg(errp, "Failed to parse DNS");
319 return -1;
321 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
322 error_setg(errp, "DNS doesn't belong to network");
323 return -1;
325 if (dns.s_addr == host.s_addr) {
326 error_setg(errp, "DNS must be different from host");
327 return -1;
330 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
331 error_setg(errp, "Failed to parse DHCP start address");
332 return -1;
334 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
335 error_setg(errp, "DHCP doesn't belong to network");
336 return -1;
338 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
339 error_setg(errp, "DNS must be different from host and DNS");
340 return -1;
343 #ifndef _WIN32
344 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
345 error_setg(errp, "Failed to parse SMB address");
346 return -1;
348 #endif
350 if (!vprefix6) {
351 vprefix6 = "fec0::";
353 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
354 error_setg(errp, "Failed to parse IPv6 prefix");
355 return -1;
358 if (!vprefix6_len) {
359 vprefix6_len = 64;
361 if (vprefix6_len < 0 || vprefix6_len > 126) {
362 error_setg(errp,
363 "Invalid prefix provided (prefix len must be in range 0-126");
364 return -1;
367 if (vhost6) {
368 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
369 error_setg(errp, "Failed to parse IPv6 host");
370 return -1;
372 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
373 error_setg(errp, "IPv6 Host doesn't belong to network");
374 return -1;
376 } else {
377 ip6_host = ip6_prefix;
378 ip6_host.s6_addr[15] |= 2;
381 if (vnameserver6) {
382 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
383 error_setg(errp, "Failed to parse IPv6 DNS");
384 return -1;
386 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
387 error_setg(errp, "IPv6 DNS doesn't belong to network");
388 return -1;
390 } else {
391 ip6_dns = ip6_prefix;
392 ip6_dns.s6_addr[15] |= 3;
395 if (vdomainname && !*vdomainname) {
396 error_setg(errp, "'domainname' parameter cannot be empty");
397 return -1;
400 if (vdomainname && strlen(vdomainname) > 255) {
401 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
402 return -1;
405 if (vhostname && strlen(vhostname) > 255) {
406 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
407 return -1;
410 if (tftp_server_name && strlen(tftp_server_name) > 255) {
411 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
412 return -1;
415 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
417 snprintf(nc->info_str, sizeof(nc->info_str),
418 "net=%s,restrict=%s", inet_ntoa(net),
419 restricted ? "on" : "off");
421 s = DO_UPCAST(SlirpState, nc, nc);
423 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
424 ipv6, ip6_prefix, vprefix6_len, ip6_host,
425 vhostname, tftp_server_name,
426 tftp_export, bootfile, dhcp,
427 dns, ip6_dns, dnssearch, vdomainname,
428 &slirp_cb, s);
429 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
431 for (config = slirp_configs; config; config = config->next) {
432 if (config->flags & SLIRP_CFG_HOSTFWD) {
433 if (slirp_hostfwd(s, config->str, errp) < 0) {
434 goto error;
436 } else {
437 if (slirp_guestfwd(s, config->str, errp) < 0) {
438 goto error;
442 #ifndef _WIN32
443 if (smb_export) {
444 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
445 goto error;
448 #endif
450 s->exit_notifier.notify = slirp_smb_exit;
451 qemu_add_exit_notifier(&s->exit_notifier);
452 return 0;
454 error:
455 qemu_del_net_client(nc);
456 return -1;
459 static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
460 const char *name)
462 if (name) {
463 NetClientState *nc;
464 if (hub_id) {
465 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
466 if (!nc) {
467 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
468 return NULL;
470 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
471 "directly instead");
472 } else {
473 nc = qemu_find_netdev(name);
474 if (!nc) {
475 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
476 return NULL;
479 if (strcmp(nc->model, "user")) {
480 monitor_printf(mon, "invalid device specified\n");
481 return NULL;
483 return DO_UPCAST(SlirpState, nc, nc);
484 } else {
485 if (QTAILQ_EMPTY(&slirp_stacks)) {
486 monitor_printf(mon, "user mode network stack not in use\n");
487 return NULL;
489 return QTAILQ_FIRST(&slirp_stacks);
493 void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
495 struct in_addr host_addr = { .s_addr = INADDR_ANY };
496 int host_port;
497 char buf[256];
498 const char *src_str, *p;
499 SlirpState *s;
500 int is_udp = 0;
501 int err;
502 const char *arg1 = qdict_get_str(qdict, "arg1");
503 const char *arg2 = qdict_get_try_str(qdict, "arg2");
504 const char *arg3 = qdict_get_try_str(qdict, "arg3");
506 if (arg3) {
507 s = slirp_lookup(mon, arg1, arg2);
508 src_str = arg3;
509 } else if (arg2) {
510 s = slirp_lookup(mon, NULL, arg1);
511 src_str = arg2;
512 } else {
513 s = slirp_lookup(mon, NULL, NULL);
514 src_str = arg1;
516 if (!s) {
517 return;
520 p = src_str;
521 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
522 goto fail_syntax;
525 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
526 is_udp = 0;
527 } else if (!strcmp(buf, "udp")) {
528 is_udp = 1;
529 } else {
530 goto fail_syntax;
533 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
534 goto fail_syntax;
536 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
537 goto fail_syntax;
540 if (qemu_strtoi(p, NULL, 10, &host_port)) {
541 goto fail_syntax;
544 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
546 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
547 err ? "not found" : "removed");
548 return;
550 fail_syntax:
551 monitor_printf(mon, "invalid format\n");
554 static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
556 struct in_addr host_addr = { .s_addr = INADDR_ANY };
557 struct in_addr guest_addr = { .s_addr = 0 };
558 int host_port, guest_port;
559 const char *p;
560 char buf[256];
561 int is_udp;
562 char *end;
563 const char *fail_reason = "Unknown reason";
565 p = redir_str;
566 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
567 fail_reason = "No : separators";
568 goto fail_syntax;
570 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
571 is_udp = 0;
572 } else if (!strcmp(buf, "udp")) {
573 is_udp = 1;
574 } else {
575 fail_reason = "Bad protocol name";
576 goto fail_syntax;
579 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
580 fail_reason = "Missing : separator";
581 goto fail_syntax;
583 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
584 fail_reason = "Bad host address";
585 goto fail_syntax;
588 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
589 fail_reason = "Bad host port separator";
590 goto fail_syntax;
592 host_port = strtol(buf, &end, 0);
593 if (*end != '\0' || host_port < 0 || host_port > 65535) {
594 fail_reason = "Bad host port";
595 goto fail_syntax;
598 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
599 fail_reason = "Missing guest address";
600 goto fail_syntax;
602 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
603 fail_reason = "Bad guest address";
604 goto fail_syntax;
607 guest_port = strtol(p, &end, 0);
608 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
609 fail_reason = "Bad guest port";
610 goto fail_syntax;
613 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
614 guest_port) < 0) {
615 error_setg(errp, "Could not set up host forwarding rule '%s'",
616 redir_str);
617 return -1;
619 return 0;
621 fail_syntax:
622 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
623 fail_reason);
624 return -1;
627 void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
629 const char *redir_str;
630 SlirpState *s;
631 const char *arg1 = qdict_get_str(qdict, "arg1");
632 const char *arg2 = qdict_get_try_str(qdict, "arg2");
633 const char *arg3 = qdict_get_try_str(qdict, "arg3");
635 if (arg3) {
636 s = slirp_lookup(mon, arg1, arg2);
637 redir_str = arg3;
638 } else if (arg2) {
639 s = slirp_lookup(mon, NULL, arg1);
640 redir_str = arg2;
641 } else {
642 s = slirp_lookup(mon, NULL, NULL);
643 redir_str = arg1;
645 if (s) {
646 Error *err = NULL;
647 if (slirp_hostfwd(s, redir_str, &err) < 0) {
648 error_report_err(err);
654 #ifndef _WIN32
656 /* automatic user mode samba server configuration */
657 static void slirp_smb_cleanup(SlirpState *s)
659 int ret;
661 if (s->smb_dir) {
662 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
663 ret = system(cmd);
664 if (ret == -1 || !WIFEXITED(ret)) {
665 error_report("'%s' failed.", cmd);
666 } else if (WEXITSTATUS(ret)) {
667 error_report("'%s' failed. Error code: %d",
668 cmd, WEXITSTATUS(ret));
670 g_free(cmd);
671 g_free(s->smb_dir);
672 s->smb_dir = NULL;
676 static int slirp_smb(SlirpState* s, const char *exported_dir,
677 struct in_addr vserver_addr, Error **errp)
679 char *smb_conf;
680 char *smb_cmdline;
681 struct passwd *passwd;
682 FILE *f;
684 passwd = getpwuid(geteuid());
685 if (!passwd) {
686 error_setg(errp, "Failed to retrieve user name");
687 return -1;
690 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
691 error_setg(errp, "Could not find '%s', please install it",
692 CONFIG_SMBD_COMMAND);
693 return -1;
696 if (access(exported_dir, R_OK | X_OK)) {
697 error_setg(errp, "Error accessing shared directory '%s': %s",
698 exported_dir, strerror(errno));
699 return -1;
702 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
703 if (!s->smb_dir) {
704 error_setg(errp, "Could not create samba server dir");
705 return -1;
707 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
709 f = fopen(smb_conf, "w");
710 if (!f) {
711 slirp_smb_cleanup(s);
712 error_setg(errp,
713 "Could not create samba server configuration file '%s'",
714 smb_conf);
715 g_free(smb_conf);
716 return -1;
718 fprintf(f,
719 "[global]\n"
720 "private dir=%s\n"
721 "interfaces=127.0.0.1\n"
722 "bind interfaces only=yes\n"
723 "pid directory=%s\n"
724 "lock directory=%s\n"
725 "state directory=%s\n"
726 "cache directory=%s\n"
727 "ncalrpc dir=%s/ncalrpc\n"
728 "log file=%s/log.smbd\n"
729 "smb passwd file=%s/smbpasswd\n"
730 "security = user\n"
731 "map to guest = Bad User\n"
732 "load printers = no\n"
733 "printing = bsd\n"
734 "disable spoolss = yes\n"
735 "usershare max shares = 0\n"
736 "[qemu]\n"
737 "path=%s\n"
738 "read only=no\n"
739 "guest ok=yes\n"
740 "force user=%s\n",
741 s->smb_dir,
742 s->smb_dir,
743 s->smb_dir,
744 s->smb_dir,
745 s->smb_dir,
746 s->smb_dir,
747 s->smb_dir,
748 s->smb_dir,
749 exported_dir,
750 passwd->pw_name
752 fclose(f);
754 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
755 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
756 g_free(smb_conf);
758 if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 ||
759 slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) {
760 slirp_smb_cleanup(s);
761 g_free(smb_cmdline);
762 error_setg(errp, "Conflicting/invalid smbserver address");
763 return -1;
765 g_free(smb_cmdline);
766 return 0;
769 #endif /* !defined(_WIN32) */
771 static int guestfwd_can_read(void *opaque)
773 struct GuestFwd *fwd = opaque;
774 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
777 static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
779 struct GuestFwd *fwd = opaque;
780 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
783 static int guestfwd_write(const void *buf, size_t len, void *chr)
785 return qemu_chr_fe_write_all(chr, buf, len);
788 static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
790 struct in_addr server = { .s_addr = 0 };
791 struct GuestFwd *fwd;
792 const char *p;
793 char buf[128];
794 char *end;
795 int port;
797 p = config_str;
798 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
799 goto fail_syntax;
801 if (strcmp(buf, "tcp") && buf[0] != '\0') {
802 goto fail_syntax;
804 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
805 goto fail_syntax;
807 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
808 goto fail_syntax;
810 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
811 goto fail_syntax;
813 port = strtol(buf, &end, 10);
814 if (*end != '\0' || port < 1 || port > 65535) {
815 goto fail_syntax;
818 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
820 if (g_str_has_prefix(p, "cmd:")) {
821 if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
822 error_setg(errp, "Conflicting/invalid host:port in guest "
823 "forwarding rule '%s'", config_str);
824 return -1;
826 } else {
827 Error *err = NULL;
829 * FIXME: sure we want to support implicit
830 * muxed monitors here?
832 Chardev *chr = qemu_chr_new_mux_mon(buf, p);
834 if (!chr) {
835 error_setg(errp, "Could not open guest forwarding device '%s'",
836 buf);
837 return -1;
840 fwd = g_new(struct GuestFwd, 1);
841 qemu_chr_fe_init(&fwd->hd, chr, &err);
842 if (err) {
843 error_propagate(errp, err);
844 object_unparent(OBJECT(chr));
845 g_free(fwd);
846 return -1;
849 if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd,
850 &server, port) < 0) {
851 error_setg(errp, "Conflicting/invalid host:port in guest "
852 "forwarding rule '%s'", config_str);
853 qemu_chr_fe_deinit(&fwd->hd, true);
854 g_free(fwd);
855 return -1;
857 fwd->server = server;
858 fwd->port = port;
859 fwd->slirp = s->slirp;
861 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
862 NULL, NULL, fwd, NULL, true);
863 s->fwd = g_slist_append(s->fwd, fwd);
865 return 0;
867 fail_syntax:
868 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
869 return -1;
872 void hmp_info_usernet(Monitor *mon, const QDict *qdict)
874 SlirpState *s;
876 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
877 int id;
878 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
879 char *info = slirp_connection_info(s->slirp);
880 monitor_printf(mon, "Hub %d (%s):\n%s",
881 got_hub_id ? id : -1,
882 s->nc.name, info);
883 g_free(info);
887 static void
888 net_init_slirp_configs(const StringList *fwd, int flags)
890 while (fwd) {
891 struct slirp_config_str *config;
893 config = g_malloc0(sizeof(*config));
894 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
895 config->flags = flags;
896 config->next = slirp_configs;
897 slirp_configs = config;
899 fwd = fwd->next;
903 static const char **slirp_dnssearch(const StringList *dnsname)
905 const StringList *c = dnsname;
906 size_t i = 0, num_opts = 0;
907 const char **ret;
909 while (c) {
910 num_opts++;
911 c = c->next;
914 if (num_opts == 0) {
915 return NULL;
918 ret = g_malloc((num_opts + 1) * sizeof(*ret));
919 c = dnsname;
920 while (c) {
921 ret[i++] = c->value->str;
922 c = c->next;
924 ret[i] = NULL;
925 return ret;
928 int net_init_slirp(const Netdev *netdev, const char *name,
929 NetClientState *peer, Error **errp)
931 struct slirp_config_str *config;
932 char *vnet;
933 int ret;
934 const NetdevUserOptions *user;
935 const char **dnssearch;
936 bool ipv4 = true, ipv6 = true;
938 assert(netdev->type == NET_CLIENT_DRIVER_USER);
939 user = &netdev->u.user;
941 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
942 (user->has_ipv4 && !user->ipv4)) {
943 ipv4 = 0;
945 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
946 (user->has_ipv6 && !user->ipv6)) {
947 ipv6 = 0;
950 vnet = user->has_net ? g_strdup(user->net) :
951 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
952 NULL;
954 dnssearch = slirp_dnssearch(user->dnssearch);
956 /* all optional fields are initialized to "all bits zero" */
958 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
959 net_init_slirp_configs(user->guestfwd, 0);
961 ret = net_slirp_init(peer, "user", name, user->q_restrict,
962 ipv4, vnet, user->host,
963 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
964 user->ipv6_host, user->hostname, user->tftp,
965 user->bootfile, user->dhcpstart,
966 user->dns, user->ipv6_dns, user->smb,
967 user->smbserver, dnssearch, user->domainname,
968 user->tftp_server_name, errp);
970 while (slirp_configs) {
971 config = slirp_configs;
972 slirp_configs = config->next;
973 g_free(config);
976 g_free(vnet);
977 g_free(dnssearch);
979 return ret;