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
;
78 typedef struct SlirpState
{
80 QTAILQ_ENTRY(SlirpState
) entry
;
82 Notifier exit_notifier
;
88 static struct slirp_config_str
*slirp_configs
;
89 static QTAILQ_HEAD(, SlirpState
) slirp_stacks
=
90 QTAILQ_HEAD_INITIALIZER(slirp_stacks
);
92 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
, Error
**errp
);
93 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
, Error
**errp
);
96 static int slirp_smb(SlirpState
*s
, const char *exported_dir
,
97 struct in_addr vserver_addr
, Error
**errp
);
98 static void slirp_smb_cleanup(SlirpState
*s
);
100 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
103 static void net_slirp_output(void *opaque
, const uint8_t *pkt
, int pkt_len
)
105 SlirpState
*s
= opaque
;
107 qemu_send_packet(&s
->nc
, pkt
, pkt_len
);
110 static ssize_t
net_slirp_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
112 SlirpState
*s
= DO_UPCAST(SlirpState
, nc
, nc
);
114 slirp_input(s
->slirp
, buf
, size
);
119 static void slirp_smb_exit(Notifier
*n
, void *data
)
121 SlirpState
*s
= container_of(n
, SlirpState
, exit_notifier
);
122 slirp_smb_cleanup(s
);
125 static void net_slirp_cleanup(NetClientState
*nc
)
127 SlirpState
*s
= DO_UPCAST(SlirpState
, nc
, nc
);
129 slirp_cleanup(s
->slirp
);
130 if (s
->exit_notifier
.notify
) {
131 qemu_remove_exit_notifier(&s
->exit_notifier
);
133 slirp_smb_cleanup(s
);
134 QTAILQ_REMOVE(&slirp_stacks
, s
, entry
);
137 static NetClientInfo net_slirp_info
= {
138 .type
= NET_CLIENT_DRIVER_USER
,
139 .size
= sizeof(SlirpState
),
140 .receive
= net_slirp_receive
,
141 .cleanup
= net_slirp_cleanup
,
144 static void net_slirp_guest_error(const char *msg
)
146 qemu_log_mask(LOG_GUEST_ERROR
, "%s", msg
);
149 static int64_t net_slirp_clock_get_ns(void)
151 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
154 static const SlirpCb slirp_cb
= {
155 .output
= net_slirp_output
,
156 .guest_error
= net_slirp_guest_error
,
157 .clock_get_ns
= net_slirp_clock_get_ns
,
160 static int net_slirp_init(NetClientState
*peer
, const char *model
,
161 const char *name
, int restricted
,
162 bool ipv4
, const char *vnetwork
, const char *vhost
,
163 bool ipv6
, const char *vprefix6
, int vprefix6_len
,
165 const char *vhostname
, const char *tftp_export
,
166 const char *bootfile
, const char *vdhcp_start
,
167 const char *vnameserver
, const char *vnameserver6
,
168 const char *smb_export
, const char *vsmbserver
,
169 const char **dnssearch
, const char *vdomainname
,
170 const char *tftp_server_name
,
173 /* default settings according to historic slirp */
174 struct in_addr net
= { .s_addr
= htonl(0x0a000200) }; /* 10.0.2.0 */
175 struct in_addr mask
= { .s_addr
= htonl(0xffffff00) }; /* 255.255.255.0 */
176 struct in_addr host
= { .s_addr
= htonl(0x0a000202) }; /* 10.0.2.2 */
177 struct in_addr dhcp
= { .s_addr
= htonl(0x0a00020f) }; /* 10.0.2.15 */
178 struct in_addr dns
= { .s_addr
= htonl(0x0a000203) }; /* 10.0.2.3 */
179 struct in6_addr ip6_prefix
;
180 struct in6_addr ip6_host
;
181 struct in6_addr ip6_dns
;
183 struct in_addr smbsrv
= { .s_addr
= 0 };
191 struct slirp_config_str
*config
;
193 if (!ipv4
&& (vnetwork
|| vhost
|| vnameserver
)) {
194 error_setg(errp
, "IPv4 disabled but netmask/host/dns provided");
198 if (!ipv6
&& (vprefix6
|| vhost6
|| vnameserver6
)) {
199 error_setg(errp
, "IPv6 disabled but prefix/host6/dns6 provided");
203 if (!ipv4
&& !ipv6
) {
204 /* It doesn't make sense to disable both */
205 error_setg(errp
, "IPv4 and IPv6 disabled");
210 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
211 if (!inet_aton(vnetwork
, &net
)) {
212 error_setg(errp
, "Failed to parse netmask");
215 addr
= ntohl(net
.s_addr
);
216 if (!(addr
& 0x80000000)) {
217 mask
.s_addr
= htonl(0xff000000); /* class A */
218 } else if ((addr
& 0xfff00000) == 0xac100000) {
219 mask
.s_addr
= htonl(0xfff00000); /* priv. 172.16.0.0/12 */
220 } else if ((addr
& 0xc0000000) == 0x80000000) {
221 mask
.s_addr
= htonl(0xffff0000); /* class B */
222 } else if ((addr
& 0xffff0000) == 0xc0a80000) {
223 mask
.s_addr
= htonl(0xffff0000); /* priv. 192.168.0.0/16 */
224 } else if ((addr
& 0xffff0000) == 0xc6120000) {
225 mask
.s_addr
= htonl(0xfffe0000); /* tests 198.18.0.0/15 */
226 } else if ((addr
& 0xe0000000) == 0xe0000000) {
227 mask
.s_addr
= htonl(0xffffff00); /* class C */
229 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
232 if (!inet_aton(buf
, &net
)) {
233 error_setg(errp
, "Failed to parse netmask");
236 shift
= strtol(vnetwork
, &end
, 10);
238 if (!inet_aton(vnetwork
, &mask
)) {
240 "Failed to parse netmask (trailing chars)");
243 } else if (shift
< 4 || shift
> 32) {
245 "Invalid netmask provided (must be in range 4-32)");
248 mask
.s_addr
= htonl(0xffffffff << (32 - shift
));
251 net
.s_addr
&= mask
.s_addr
;
252 host
.s_addr
= net
.s_addr
| (htonl(0x0202) & ~mask
.s_addr
);
253 dhcp
.s_addr
= net
.s_addr
| (htonl(0x020f) & ~mask
.s_addr
);
254 dns
.s_addr
= net
.s_addr
| (htonl(0x0203) & ~mask
.s_addr
);
257 if (vhost
&& !inet_aton(vhost
, &host
)) {
258 error_setg(errp
, "Failed to parse host");
261 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
262 error_setg(errp
, "Host doesn't belong to network");
266 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
267 error_setg(errp
, "Failed to parse DNS");
270 if ((dns
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
271 error_setg(errp
, "DNS doesn't belong to network");
274 if (dns
.s_addr
== host
.s_addr
) {
275 error_setg(errp
, "DNS must be different from host");
279 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
280 error_setg(errp
, "Failed to parse DHCP start address");
283 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
284 error_setg(errp
, "DHCP doesn't belong to network");
287 if (dhcp
.s_addr
== host
.s_addr
|| dhcp
.s_addr
== dns
.s_addr
) {
288 error_setg(errp
, "DNS must be different from host and DNS");
293 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
294 error_setg(errp
, "Failed to parse SMB address");
302 if (!inet_pton(AF_INET6
, vprefix6
, &ip6_prefix
)) {
303 error_setg(errp
, "Failed to parse IPv6 prefix");
310 if (vprefix6_len
< 0 || vprefix6_len
> 126) {
312 "Invalid prefix provided (prefix len must be in range 0-126");
317 if (!inet_pton(AF_INET6
, vhost6
, &ip6_host
)) {
318 error_setg(errp
, "Failed to parse IPv6 host");
321 if (!in6_equal_net(&ip6_prefix
, &ip6_host
, vprefix6_len
)) {
322 error_setg(errp
, "IPv6 Host doesn't belong to network");
326 ip6_host
= ip6_prefix
;
327 ip6_host
.s6_addr
[15] |= 2;
331 if (!inet_pton(AF_INET6
, vnameserver6
, &ip6_dns
)) {
332 error_setg(errp
, "Failed to parse IPv6 DNS");
335 if (!in6_equal_net(&ip6_prefix
, &ip6_dns
, vprefix6_len
)) {
336 error_setg(errp
, "IPv6 DNS doesn't belong to network");
340 ip6_dns
= ip6_prefix
;
341 ip6_dns
.s6_addr
[15] |= 3;
344 if (vdomainname
&& !*vdomainname
) {
345 error_setg(errp
, "'domainname' parameter cannot be empty");
349 if (vdomainname
&& strlen(vdomainname
) > 255) {
350 error_setg(errp
, "'domainname' parameter cannot exceed 255 bytes");
354 if (vhostname
&& strlen(vhostname
) > 255) {
355 error_setg(errp
, "'vhostname' parameter cannot exceed 255 bytes");
359 if (tftp_server_name
&& strlen(tftp_server_name
) > 255) {
360 error_setg(errp
, "'tftp-server-name' parameter cannot exceed 255 bytes");
364 nc
= qemu_new_net_client(&net_slirp_info
, peer
, model
, name
);
366 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
367 "net=%s,restrict=%s", inet_ntoa(net
),
368 restricted
? "on" : "off");
370 s
= DO_UPCAST(SlirpState
, nc
, nc
);
372 s
->slirp
= slirp_init(restricted
, ipv4
, net
, mask
, host
,
373 ipv6
, ip6_prefix
, vprefix6_len
, ip6_host
,
374 vhostname
, tftp_server_name
,
375 tftp_export
, bootfile
, dhcp
,
376 dns
, ip6_dns
, dnssearch
, vdomainname
,
378 QTAILQ_INSERT_TAIL(&slirp_stacks
, s
, entry
);
380 for (config
= slirp_configs
; config
; config
= config
->next
) {
381 if (config
->flags
& SLIRP_CFG_HOSTFWD
) {
382 if (slirp_hostfwd(s
, config
->str
, errp
) < 0) {
386 if (slirp_guestfwd(s
, config
->str
, errp
) < 0) {
393 if (slirp_smb(s
, smb_export
, smbsrv
, errp
) < 0) {
399 s
->exit_notifier
.notify
= slirp_smb_exit
;
400 qemu_add_exit_notifier(&s
->exit_notifier
);
404 qemu_del_net_client(nc
);
408 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *hub_id
,
414 nc
= net_hub_find_client_by_name(strtol(hub_id
, NULL
, 0), name
);
416 monitor_printf(mon
, "unrecognized (hub-id, stackname) pair\n");
419 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
422 nc
= qemu_find_netdev(name
);
424 monitor_printf(mon
, "unrecognized netdev id '%s'\n", name
);
428 if (strcmp(nc
->model
, "user")) {
429 monitor_printf(mon
, "invalid device specified\n");
432 return DO_UPCAST(SlirpState
, nc
, nc
);
434 if (QTAILQ_EMPTY(&slirp_stacks
)) {
435 monitor_printf(mon
, "user mode network stack not in use\n");
438 return QTAILQ_FIRST(&slirp_stacks
);
442 void hmp_hostfwd_remove(Monitor
*mon
, const QDict
*qdict
)
444 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
447 const char *src_str
, *p
;
451 const char *arg1
= qdict_get_str(qdict
, "arg1");
452 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
453 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
456 s
= slirp_lookup(mon
, arg1
, arg2
);
459 s
= slirp_lookup(mon
, NULL
, arg1
);
462 s
= slirp_lookup(mon
, NULL
, NULL
);
470 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
474 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
476 } else if (!strcmp(buf
, "udp")) {
482 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
485 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
489 if (qemu_strtoi(p
, NULL
, 10, &host_port
)) {
493 err
= slirp_remove_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
);
495 monitor_printf(mon
, "host forwarding rule for %s %s\n", src_str
,
496 err
? "not found" : "removed");
500 monitor_printf(mon
, "invalid format\n");
503 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
, Error
**errp
)
505 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
506 struct in_addr guest_addr
= { .s_addr
= 0 };
507 int host_port
, guest_port
;
512 const char *fail_reason
= "Unknown reason";
515 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
516 fail_reason
= "No : separators";
519 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
521 } else if (!strcmp(buf
, "udp")) {
524 fail_reason
= "Bad protocol name";
528 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
529 fail_reason
= "Missing : separator";
532 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
533 fail_reason
= "Bad host address";
537 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
538 fail_reason
= "Bad host port separator";
541 host_port
= strtol(buf
, &end
, 0);
542 if (*end
!= '\0' || host_port
< 0 || host_port
> 65535) {
543 fail_reason
= "Bad host port";
547 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
548 fail_reason
= "Missing guest address";
551 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
)) {
552 fail_reason
= "Bad guest address";
556 guest_port
= strtol(p
, &end
, 0);
557 if (*end
!= '\0' || guest_port
< 1 || guest_port
> 65535) {
558 fail_reason
= "Bad guest port";
562 if (slirp_add_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
, guest_addr
,
564 error_setg(errp
, "Could not set up host forwarding rule '%s'",
571 error_setg(errp
, "Invalid host forwarding rule '%s' (%s)", redir_str
,
576 void hmp_hostfwd_add(Monitor
*mon
, const QDict
*qdict
)
578 const char *redir_str
;
580 const char *arg1
= qdict_get_str(qdict
, "arg1");
581 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
582 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
585 s
= slirp_lookup(mon
, arg1
, arg2
);
588 s
= slirp_lookup(mon
, NULL
, arg1
);
591 s
= slirp_lookup(mon
, NULL
, NULL
);
596 if (slirp_hostfwd(s
, redir_str
, &err
) < 0) {
597 error_report_err(err
);
605 /* automatic user mode samba server configuration */
606 static void slirp_smb_cleanup(SlirpState
*s
)
611 gchar
*cmd
= g_strdup_printf("rm -rf %s", s
->smb_dir
);
613 if (ret
== -1 || !WIFEXITED(ret
)) {
614 error_report("'%s' failed.", cmd
);
615 } else if (WEXITSTATUS(ret
)) {
616 error_report("'%s' failed. Error code: %d",
617 cmd
, WEXITSTATUS(ret
));
625 static int slirp_smb(SlirpState
* s
, const char *exported_dir
,
626 struct in_addr vserver_addr
, Error
**errp
)
630 struct passwd
*passwd
;
633 passwd
= getpwuid(geteuid());
635 error_setg(errp
, "Failed to retrieve user name");
639 if (access(CONFIG_SMBD_COMMAND
, F_OK
)) {
640 error_setg(errp
, "Could not find '%s', please install it",
641 CONFIG_SMBD_COMMAND
);
645 if (access(exported_dir
, R_OK
| X_OK
)) {
646 error_setg(errp
, "Error accessing shared directory '%s': %s",
647 exported_dir
, strerror(errno
));
651 s
->smb_dir
= g_dir_make_tmp("qemu-smb.XXXXXX", NULL
);
653 error_setg(errp
, "Could not create samba server dir");
656 smb_conf
= g_strdup_printf("%s/%s", s
->smb_dir
, "smb.conf");
658 f
= fopen(smb_conf
, "w");
660 slirp_smb_cleanup(s
);
662 "Could not create samba server configuration file '%s'",
670 "interfaces=127.0.0.1\n"
671 "bind interfaces only=yes\n"
673 "lock directory=%s\n"
674 "state directory=%s\n"
675 "cache directory=%s\n"
676 "ncalrpc dir=%s/ncalrpc\n"
677 "log file=%s/log.smbd\n"
678 "smb passwd file=%s/smbpasswd\n"
680 "map to guest = Bad User\n"
681 "load printers = no\n"
683 "disable spoolss = yes\n"
684 "usershare max shares = 0\n"
703 smb_cmdline
= g_strdup_printf("%s -l %s -s %s",
704 CONFIG_SMBD_COMMAND
, s
->smb_dir
, smb_conf
);
707 if (slirp_add_exec(s
->slirp
, NULL
, smb_cmdline
, &vserver_addr
, 139) < 0 ||
708 slirp_add_exec(s
->slirp
, NULL
, smb_cmdline
, &vserver_addr
, 445) < 0) {
709 slirp_smb_cleanup(s
);
711 error_setg(errp
, "Conflicting/invalid smbserver address");
718 #endif /* !defined(_WIN32) */
722 struct in_addr server
;
727 static int guestfwd_can_read(void *opaque
)
729 struct GuestFwd
*fwd
= opaque
;
730 return slirp_socket_can_recv(fwd
->slirp
, fwd
->server
, fwd
->port
);
733 static void guestfwd_read(void *opaque
, const uint8_t *buf
, int size
)
735 struct GuestFwd
*fwd
= opaque
;
736 slirp_socket_recv(fwd
->slirp
, fwd
->server
, fwd
->port
, buf
, size
);
739 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
, Error
**errp
)
741 struct in_addr server
= { .s_addr
= 0 };
742 struct GuestFwd
*fwd
;
749 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
752 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
755 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
758 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
761 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
764 port
= strtol(buf
, &end
, 10);
765 if (*end
!= '\0' || port
< 1 || port
> 65535) {
769 snprintf(buf
, sizeof(buf
), "guestfwd.tcp.%d", port
);
771 if ((strlen(p
) > 4) && !strncmp(p
, "cmd:", 4)) {
772 if (slirp_add_exec(s
->slirp
, NULL
, &p
[4], &server
, port
) < 0) {
773 error_setg(errp
, "Conflicting/invalid host:port in guest "
774 "forwarding rule '%s'", config_str
);
780 * FIXME: sure we want to support implicit
781 * muxed monitors here?
783 Chardev
*chr
= qemu_chr_new_mux_mon(buf
, p
);
786 error_setg(errp
, "Could not open guest forwarding device '%s'",
791 fwd
= g_new(struct GuestFwd
, 1);
792 qemu_chr_fe_init(&fwd
->hd
, chr
, &err
);
794 error_propagate(errp
, err
);
799 if (slirp_add_exec(s
->slirp
, &fwd
->hd
, NULL
, &server
, port
) < 0) {
800 error_setg(errp
, "Conflicting/invalid host:port in guest "
801 "forwarding rule '%s'", config_str
);
805 fwd
->server
= server
;
807 fwd
->slirp
= s
->slirp
;
809 qemu_chr_fe_set_handlers(&fwd
->hd
, guestfwd_can_read
, guestfwd_read
,
810 NULL
, NULL
, fwd
, NULL
, true);
815 error_setg(errp
, "Invalid guest forwarding rule '%s'", config_str
);
819 void hmp_info_usernet(Monitor
*mon
, const QDict
*qdict
)
823 QTAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
825 bool got_hub_id
= net_hub_id_for_client(&s
->nc
, &id
) == 0;
826 char *info
= slirp_connection_info(s
->slirp
);
827 monitor_printf(mon
, "Hub %d (%s):\n%s",
828 got_hub_id
? id
: -1,
835 net_init_slirp_configs(const StringList
*fwd
, int flags
)
838 struct slirp_config_str
*config
;
840 config
= g_malloc0(sizeof(*config
));
841 pstrcpy(config
->str
, sizeof(config
->str
), fwd
->value
->str
);
842 config
->flags
= flags
;
843 config
->next
= slirp_configs
;
844 slirp_configs
= config
;
850 static const char **slirp_dnssearch(const StringList
*dnsname
)
852 const StringList
*c
= dnsname
;
853 size_t i
= 0, num_opts
= 0;
865 ret
= g_malloc((num_opts
+ 1) * sizeof(*ret
));
868 ret
[i
++] = c
->value
->str
;
875 int net_init_slirp(const Netdev
*netdev
, const char *name
,
876 NetClientState
*peer
, Error
**errp
)
878 struct slirp_config_str
*config
;
881 const NetdevUserOptions
*user
;
882 const char **dnssearch
;
883 bool ipv4
= true, ipv6
= true;
885 assert(netdev
->type
== NET_CLIENT_DRIVER_USER
);
886 user
= &netdev
->u
.user
;
888 if ((user
->has_ipv6
&& user
->ipv6
&& !user
->has_ipv4
) ||
889 (user
->has_ipv4
&& !user
->ipv4
)) {
892 if ((user
->has_ipv4
&& user
->ipv4
&& !user
->has_ipv6
) ||
893 (user
->has_ipv6
&& !user
->ipv6
)) {
897 vnet
= user
->has_net
? g_strdup(user
->net
) :
898 user
->has_ip
? g_strdup_printf("%s/24", user
->ip
) :
901 dnssearch
= slirp_dnssearch(user
->dnssearch
);
903 /* all optional fields are initialized to "all bits zero" */
905 net_init_slirp_configs(user
->hostfwd
, SLIRP_CFG_HOSTFWD
);
906 net_init_slirp_configs(user
->guestfwd
, 0);
908 ret
= net_slirp_init(peer
, "user", name
, user
->q_restrict
,
909 ipv4
, vnet
, user
->host
,
910 ipv6
, user
->ipv6_prefix
, user
->ipv6_prefixlen
,
911 user
->ipv6_host
, user
->hostname
, user
->tftp
,
912 user
->bootfile
, user
->dhcpstart
,
913 user
->dns
, user
->ipv6_dns
, user
->smb
,
914 user
->smbserver
, dnssearch
, user
->domainname
,
915 user
->tftp_server_name
, errp
);
917 while (slirp_configs
) {
918 config
= slirp_configs
;
919 slirp_configs
= config
->next
;