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"
26 #include "net/slirp.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
)
58 if (len
> buf_size
- 1)
67 /* slirp network adapter */
69 #define SLIRP_CFG_HOSTFWD 1
71 struct slirp_config_str
{
72 struct slirp_config_str
*next
;
77 typedef struct SlirpState
{
79 QTAILQ_ENTRY(SlirpState
) entry
;
81 Notifier exit_notifier
;
87 static struct slirp_config_str
*slirp_configs
;
88 static QTAILQ_HEAD(, 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
);
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
);
99 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
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
);
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
,
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
,
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
;
166 struct in_addr smbsrv
= { .s_addr
= 0 };
174 struct slirp_config_str
*config
;
176 if (!ipv4
&& (vnetwork
|| vhost
|| vnameserver
)) {
177 error_setg(errp
, "IPv4 disabled but netmask/host/dns provided");
181 if (!ipv6
&& (vprefix6
|| vhost6
|| vnameserver6
)) {
182 error_setg(errp
, "IPv6 disabled but prefix/host6/dns6 provided");
186 if (!ipv4
&& !ipv6
) {
187 /* It doesn't make sense to disable both */
188 error_setg(errp
, "IPv4 and IPv6 disabled");
193 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
194 if (!inet_aton(vnetwork
, &net
)) {
195 error_setg(errp
, "Failed to parse netmask");
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 */
212 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
215 if (!inet_aton(buf
, &net
)) {
216 error_setg(errp
, "Failed to parse netmask");
219 shift
= strtol(vnetwork
, &end
, 10);
221 if (!inet_aton(vnetwork
, &mask
)) {
223 "Failed to parse netmask (trailing chars)");
226 } else if (shift
< 4 || shift
> 32) {
228 "Invalid netmask provided (must be in range 4-32)");
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");
244 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
245 error_setg(errp
, "Host doesn't belong to network");
249 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
250 error_setg(errp
, "Failed to parse DNS");
253 if ((dns
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
254 error_setg(errp
, "DNS doesn't belong to network");
257 if (dns
.s_addr
== host
.s_addr
) {
258 error_setg(errp
, "DNS must be different from host");
262 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
263 error_setg(errp
, "Failed to parse DHCP start address");
266 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
267 error_setg(errp
, "DHCP doesn't belong to network");
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");
276 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
277 error_setg(errp
, "Failed to parse SMB address");
282 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
283 /* No inet_pton helper before Vista... */
286 error_setg(errp
, "IPv6 prefix not supported");
289 memset(&ip6_prefix
, 0, sizeof(ip6_prefix
));
290 ip6_prefix
.s6_addr
[0] = 0xfe;
291 ip6_prefix
.s6_addr
[1] = 0xc0;
296 if (!inet_pton(AF_INET6
, vprefix6
, &ip6_prefix
)) {
297 error_setg(errp
, "Failed to parse IPv6 prefix");
305 if (vprefix6_len
< 0 || vprefix6_len
> 126) {
307 "Invalid prefix provided (prefix len must be in range 0-126");
312 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
313 error_setg(errp
, "IPv6 host not supported");
316 if (!inet_pton(AF_INET6
, vhost6
, &ip6_host
)) {
317 error_setg(errp
, "Failed to parse IPv6 host");
320 if (!in6_equal_net(&ip6_prefix
, &ip6_host
, vprefix6_len
)) {
321 error_setg(errp
, "IPv6 Host doesn't belong to network");
326 ip6_host
= ip6_prefix
;
327 ip6_host
.s6_addr
[15] |= 2;
331 #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
332 error_setg(errp
, "IPv6 DNS not supported");
335 if (!inet_pton(AF_INET6
, vnameserver6
, &ip6_dns
)) {
336 error_setg(errp
, "Failed to parse IPv6 DNS");
339 if (!in6_equal_net(&ip6_prefix
, &ip6_dns
, vprefix6_len
)) {
340 error_setg(errp
, "IPv6 DNS doesn't belong to network");
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");
354 if (vdomainname
&& strlen(vdomainname
) > 255) {
355 error_setg(errp
, "'domainname' parameter cannot exceed 255 bytes");
359 if (vhostname
&& strlen(vhostname
) > 255) {
360 error_setg(errp
, "'vhostname' parameter cannot exceed 255 bytes");
364 if (tftp_server_name
&& strlen(tftp_server_name
) > 255) {
365 error_setg(errp
, "'tftp-server-name' parameter cannot exceed 255 bytes");
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) {
390 if (slirp_guestfwd(s
, config
->str
, errp
) < 0) {
397 if (slirp_smb(s
, smb_export
, smbsrv
, errp
) < 0) {
403 s
->exit_notifier
.notify
= slirp_smb_exit
;
404 qemu_add_exit_notifier(&s
->exit_notifier
);
408 qemu_del_net_client(nc
);
412 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *hub_id
,
418 nc
= net_hub_find_client_by_name(strtol(hub_id
, NULL
, 0), name
);
420 monitor_printf(mon
, "unrecognized (hub-id, stackname) pair\n");
423 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
426 nc
= qemu_find_netdev(name
);
428 monitor_printf(mon
, "unrecognized netdev id '%s'\n", name
);
432 if (strcmp(nc
->model
, "user")) {
433 monitor_printf(mon
, "invalid device specified\n");
436 return DO_UPCAST(SlirpState
, nc
, nc
);
438 if (QTAILQ_EMPTY(&slirp_stacks
)) {
439 monitor_printf(mon
, "user mode network stack not in use\n");
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
};
451 const char *src_str
, *p
;
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");
460 s
= slirp_lookup(mon
, arg1
, arg2
);
463 s
= slirp_lookup(mon
, NULL
, arg1
);
466 s
= slirp_lookup(mon
, NULL
, NULL
);
474 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
478 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
480 } else if (!strcmp(buf
, "udp")) {
486 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
489 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
493 if (qemu_strtoi(p
, NULL
, 10, &host_port
)) {
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");
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
;
516 const char *fail_reason
= "Unknown reason";
519 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
520 fail_reason
= "No : separators";
523 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
525 } else if (!strcmp(buf
, "udp")) {
528 fail_reason
= "Bad protocol name";
532 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
533 fail_reason
= "Missing : separator";
536 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
537 fail_reason
= "Bad host address";
541 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
542 fail_reason
= "Bad host port separator";
545 host_port
= strtol(buf
, &end
, 0);
546 if (*end
!= '\0' || host_port
< 0 || host_port
> 65535) {
547 fail_reason
= "Bad host port";
551 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
552 fail_reason
= "Missing guest address";
555 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
)) {
556 fail_reason
= "Bad guest address";
560 guest_port
= strtol(p
, &end
, 0);
561 if (*end
!= '\0' || guest_port
< 1 || guest_port
> 65535) {
562 fail_reason
= "Bad guest port";
566 if (slirp_add_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
, guest_addr
,
568 error_setg(errp
, "Could not set up host forwarding rule '%s'",
575 error_setg(errp
, "Invalid host forwarding rule '%s' (%s)", redir_str
,
580 void hmp_hostfwd_add(Monitor
*mon
, const QDict
*qdict
)
582 const char *redir_str
;
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");
589 s
= slirp_lookup(mon
, arg1
, arg2
);
592 s
= slirp_lookup(mon
, NULL
, arg1
);
595 s
= slirp_lookup(mon
, NULL
, NULL
);
600 if (slirp_hostfwd(s
, redir_str
, &err
) < 0) {
601 error_report_err(err
);
609 /* automatic user mode samba server configuration */
610 static void slirp_smb_cleanup(SlirpState
*s
)
615 gchar
*cmd
= g_strdup_printf("rm -rf %s", s
->smb_dir
);
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
));
629 static int slirp_smb(SlirpState
* s
, const char *exported_dir
,
630 struct in_addr vserver_addr
, Error
**errp
)
634 struct passwd
*passwd
;
637 passwd
= getpwuid(geteuid());
639 error_setg(errp
, "Failed to retrieve user name");
643 if (access(CONFIG_SMBD_COMMAND
, F_OK
)) {
644 error_setg(errp
, "Could not find '%s', please install it",
645 CONFIG_SMBD_COMMAND
);
649 if (access(exported_dir
, R_OK
| X_OK
)) {
650 error_setg(errp
, "Error accessing shared directory '%s': %s",
651 exported_dir
, strerror(errno
));
655 s
->smb_dir
= g_dir_make_tmp("qemu-smb.XXXXXX", NULL
);
657 error_setg(errp
, "Could not create samba server dir");
660 smb_conf
= g_strdup_printf("%s/%s", s
->smb_dir
, "smb.conf");
662 f
= fopen(smb_conf
, "w");
664 slirp_smb_cleanup(s
);
666 "Could not create samba server configuration file '%s'",
674 "interfaces=127.0.0.1\n"
675 "bind interfaces only=yes\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"
684 "map to guest = Bad User\n"
685 "load printers = no\n"
687 "disable spoolss = yes\n"
688 "usershare max shares = 0\n"
707 smb_cmdline
= g_strdup_printf("%s -l %s -s %s",
708 CONFIG_SMBD_COMMAND
, s
->smb_dir
, 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
);
715 error_setg(errp
, "Conflicting/invalid smbserver address");
722 #endif /* !defined(_WIN32) */
726 struct in_addr server
;
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
;
753 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
756 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
759 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
762 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
765 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
768 port
= strtol(buf
, &end
, 10);
769 if (*end
!= '\0' || port
< 1 || port
> 65535) {
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
);
784 * FIXME: sure we want to support implicit
785 * muxed monitors here?
787 Chardev
*chr
= qemu_chr_new_mux_mon(buf
, p
);
790 error_setg(errp
, "Could not open guest forwarding device '%s'",
795 fwd
= g_new(struct GuestFwd
, 1);
796 qemu_chr_fe_init(&fwd
->hd
, chr
, &err
);
798 error_propagate(errp
, err
);
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
);
809 fwd
->server
= server
;
811 fwd
->slirp
= s
->slirp
;
813 qemu_chr_fe_set_handlers(&fwd
->hd
, guestfwd_can_read
, guestfwd_read
,
814 NULL
, NULL
, fwd
, NULL
, true);
819 error_setg(errp
, "Invalid guest forwarding rule '%s'", config_str
);
823 void hmp_info_usernet(Monitor
*mon
, const QDict
*qdict
)
827 QTAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
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,
833 slirp_connection_info(s
->slirp
, mon
);
838 net_init_slirp_configs(const StringList
*fwd
, int flags
)
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
;
853 static const char **slirp_dnssearch(const StringList
*dnsname
)
855 const StringList
*c
= dnsname
;
856 size_t i
= 0, num_opts
= 0;
868 ret
= g_malloc((num_opts
+ 1) * sizeof(*ret
));
871 ret
[i
++] = c
->value
->str
;
878 int net_init_slirp(const Netdev
*netdev
, const char *name
,
879 NetClientState
*peer
, Error
**errp
)
881 struct slirp_config_str
*config
;
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
)) {
895 if ((user
->has_ipv4
&& user
->ipv4
&& !user
->has_ipv6
) ||
896 (user
->has_ipv6
&& !user
->ipv6
)) {
900 vnet
= user
->has_net
? g_strdup(user
->net
) :
901 user
->has_ip
? g_strdup_printf("%s/24", user
->ip
) :
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
;