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
25 #include "qemu/osdep.h"
27 #include "net/slirp.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"
48 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
59 if (len
> buf_size
- 1)
68 /* slirp network adapter */
70 #define SLIRP_CFG_HOSTFWD 1
72 struct slirp_config_str
{
73 struct slirp_config_str
*next
;
80 struct in_addr server
;
85 typedef struct SlirpState
{
87 QTAILQ_ENTRY(SlirpState
) entry
;
89 Notifier exit_notifier
;
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
);
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
);
108 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
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
);
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);
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
,
178 static void net_slirp_timer_free(void *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
,
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
,
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
;
234 struct in_addr smbsrv
= { .s_addr
= 0 };
242 struct slirp_config_str
*config
;
244 if (!ipv4
&& (vnetwork
|| vhost
|| vnameserver
)) {
245 error_setg(errp
, "IPv4 disabled but netmask/host/dns provided");
249 if (!ipv6
&& (vprefix6
|| vhost6
|| vnameserver6
)) {
250 error_setg(errp
, "IPv6 disabled but prefix/host6/dns6 provided");
254 if (!ipv4
&& !ipv6
) {
255 /* It doesn't make sense to disable both */
256 error_setg(errp
, "IPv4 and IPv6 disabled");
261 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
262 if (!inet_aton(vnetwork
, &net
)) {
263 error_setg(errp
, "Failed to parse netmask");
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 */
280 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
283 if (!inet_aton(buf
, &net
)) {
284 error_setg(errp
, "Failed to parse netmask");
287 shift
= strtol(vnetwork
, &end
, 10);
289 if (!inet_aton(vnetwork
, &mask
)) {
291 "Failed to parse netmask (trailing chars)");
294 } else if (shift
< 4 || shift
> 32) {
296 "Invalid netmask provided (must be in range 4-32)");
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");
312 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
313 error_setg(errp
, "Host doesn't belong to network");
317 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
318 error_setg(errp
, "Failed to parse DNS");
321 if ((dns
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
322 error_setg(errp
, "DNS doesn't belong to network");
325 if (dns
.s_addr
== host
.s_addr
) {
326 error_setg(errp
, "DNS must be different from host");
330 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
331 error_setg(errp
, "Failed to parse DHCP start address");
334 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
335 error_setg(errp
, "DHCP doesn't belong to network");
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");
344 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
345 error_setg(errp
, "Failed to parse SMB address");
353 if (!inet_pton(AF_INET6
, vprefix6
, &ip6_prefix
)) {
354 error_setg(errp
, "Failed to parse IPv6 prefix");
361 if (vprefix6_len
< 0 || vprefix6_len
> 126) {
363 "Invalid prefix provided (prefix len must be in range 0-126");
368 if (!inet_pton(AF_INET6
, vhost6
, &ip6_host
)) {
369 error_setg(errp
, "Failed to parse IPv6 host");
372 if (!in6_equal_net(&ip6_prefix
, &ip6_host
, vprefix6_len
)) {
373 error_setg(errp
, "IPv6 Host doesn't belong to network");
377 ip6_host
= ip6_prefix
;
378 ip6_host
.s6_addr
[15] |= 2;
382 if (!inet_pton(AF_INET6
, vnameserver6
, &ip6_dns
)) {
383 error_setg(errp
, "Failed to parse IPv6 DNS");
386 if (!in6_equal_net(&ip6_prefix
, &ip6_dns
, vprefix6_len
)) {
387 error_setg(errp
, "IPv6 DNS doesn't belong to network");
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");
400 if (vdomainname
&& strlen(vdomainname
) > 255) {
401 error_setg(errp
, "'domainname' parameter cannot exceed 255 bytes");
405 if (vhostname
&& strlen(vhostname
) > 255) {
406 error_setg(errp
, "'vhostname' parameter cannot exceed 255 bytes");
410 if (tftp_server_name
&& strlen(tftp_server_name
) > 255) {
411 error_setg(errp
, "'tftp-server-name' parameter cannot exceed 255 bytes");
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
,
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) {
437 if (slirp_guestfwd(s
, config
->str
, errp
) < 0) {
444 if (slirp_smb(s
, smb_export
, smbsrv
, errp
) < 0) {
450 s
->exit_notifier
.notify
= slirp_smb_exit
;
451 qemu_add_exit_notifier(&s
->exit_notifier
);
455 qemu_del_net_client(nc
);
459 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *hub_id
,
465 nc
= net_hub_find_client_by_name(strtol(hub_id
, NULL
, 0), name
);
467 monitor_printf(mon
, "unrecognized (hub-id, stackname) pair\n");
470 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
473 nc
= qemu_find_netdev(name
);
475 monitor_printf(mon
, "unrecognized netdev id '%s'\n", name
);
479 if (strcmp(nc
->model
, "user")) {
480 monitor_printf(mon
, "invalid device specified\n");
483 return DO_UPCAST(SlirpState
, nc
, nc
);
485 if (QTAILQ_EMPTY(&slirp_stacks
)) {
486 monitor_printf(mon
, "user mode network stack not in use\n");
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
};
498 const char *src_str
, *p
;
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");
507 s
= slirp_lookup(mon
, arg1
, arg2
);
510 s
= slirp_lookup(mon
, NULL
, arg1
);
513 s
= slirp_lookup(mon
, NULL
, NULL
);
521 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
525 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
527 } else if (!strcmp(buf
, "udp")) {
533 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
536 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
540 if (qemu_strtoi(p
, NULL
, 10, &host_port
)) {
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");
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
;
563 const char *fail_reason
= "Unknown reason";
566 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
567 fail_reason
= "No : separators";
570 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
572 } else if (!strcmp(buf
, "udp")) {
575 fail_reason
= "Bad protocol name";
579 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
580 fail_reason
= "Missing : separator";
583 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
584 fail_reason
= "Bad host address";
588 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
589 fail_reason
= "Bad host port separator";
592 host_port
= strtol(buf
, &end
, 0);
593 if (*end
!= '\0' || host_port
< 0 || host_port
> 65535) {
594 fail_reason
= "Bad host port";
598 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
599 fail_reason
= "Missing guest address";
602 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
)) {
603 fail_reason
= "Bad guest address";
607 guest_port
= strtol(p
, &end
, 0);
608 if (*end
!= '\0' || guest_port
< 1 || guest_port
> 65535) {
609 fail_reason
= "Bad guest port";
613 if (slirp_add_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
, guest_addr
,
615 error_setg(errp
, "Could not set up host forwarding rule '%s'",
622 error_setg(errp
, "Invalid host forwarding rule '%s' (%s)", redir_str
,
627 void hmp_hostfwd_add(Monitor
*mon
, const QDict
*qdict
)
629 const char *redir_str
;
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");
636 s
= slirp_lookup(mon
, arg1
, arg2
);
639 s
= slirp_lookup(mon
, NULL
, arg1
);
642 s
= slirp_lookup(mon
, NULL
, NULL
);
647 if (slirp_hostfwd(s
, redir_str
, &err
) < 0) {
648 error_report_err(err
);
656 /* automatic user mode samba server configuration */
657 static void slirp_smb_cleanup(SlirpState
*s
)
662 gchar
*cmd
= g_strdup_printf("rm -rf %s", s
->smb_dir
);
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
));
676 static int slirp_smb(SlirpState
* s
, const char *exported_dir
,
677 struct in_addr vserver_addr
, Error
**errp
)
681 struct passwd
*passwd
;
684 passwd
= getpwuid(geteuid());
686 error_setg(errp
, "Failed to retrieve user name");
690 if (access(CONFIG_SMBD_COMMAND
, F_OK
)) {
691 error_setg(errp
, "Could not find '%s', please install it",
692 CONFIG_SMBD_COMMAND
);
696 if (access(exported_dir
, R_OK
| X_OK
)) {
697 error_setg(errp
, "Error accessing shared directory '%s': %s",
698 exported_dir
, strerror(errno
));
702 s
->smb_dir
= g_dir_make_tmp("qemu-smb.XXXXXX", NULL
);
704 error_setg(errp
, "Could not create samba server dir");
707 smb_conf
= g_strdup_printf("%s/%s", s
->smb_dir
, "smb.conf");
709 f
= fopen(smb_conf
, "w");
711 slirp_smb_cleanup(s
);
713 "Could not create samba server configuration file '%s'",
721 "interfaces=127.0.0.1\n"
722 "bind interfaces only=yes\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"
731 "map to guest = Bad User\n"
732 "load printers = no\n"
734 "disable spoolss = yes\n"
735 "usershare max shares = 0\n"
754 smb_cmdline
= g_strdup_printf("%s -l %s -s %s",
755 CONFIG_SMBD_COMMAND
, s
->smb_dir
, 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
);
762 error_setg(errp
, "Conflicting/invalid smbserver address");
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
;
798 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
801 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
804 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
807 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
810 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
813 port
= strtol(buf
, &end
, 10);
814 if (*end
!= '\0' || port
< 1 || port
> 65535) {
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
);
829 * FIXME: sure we want to support implicit
830 * muxed monitors here?
832 Chardev
*chr
= qemu_chr_new_mux_mon(buf
, p
);
835 error_setg(errp
, "Could not open guest forwarding device '%s'",
840 fwd
= g_new(struct GuestFwd
, 1);
841 qemu_chr_fe_init(&fwd
->hd
, chr
, &err
);
843 error_propagate(errp
, err
);
844 object_unparent(OBJECT(chr
));
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);
857 fwd
->server
= server
;
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
);
868 error_setg(errp
, "Invalid guest forwarding rule '%s'", config_str
);
872 void hmp_info_usernet(Monitor
*mon
, const QDict
*qdict
)
876 QTAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
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,
888 net_init_slirp_configs(const StringList
*fwd
, int flags
)
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
;
903 static const char **slirp_dnssearch(const StringList
*dnsname
)
905 const StringList
*c
= dnsname
;
906 size_t i
= 0, num_opts
= 0;
918 ret
= g_malloc((num_opts
+ 1) * sizeof(*ret
));
921 ret
[i
++] = c
->value
->str
;
928 int net_init_slirp(const Netdev
*netdev
, const char *name
,
929 NetClientState
*peer
, Error
**errp
)
931 struct slirp_config_str
*config
;
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
)) {
945 if ((user
->has_ipv4
&& user
->ipv4
&& !user
->has_ipv6
) ||
946 (user
->has_ipv6
&& !user
->ipv6
)) {
950 vnet
= user
->has_net
? g_strdup(user
->net
) :
951 user
->has_ip
? g_strdup_printf("%s/24", user
->ip
) :
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
;