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"
30 #if defined(CONFIG_SMBD_COMMAND)
38 #include "monitor/monitor.h"
39 #include "qemu/error-report.h"
40 #include "qemu/sockets.h"
42 #include "chardev/char-fe.h"
43 #include "sysemu/sysemu.h"
44 #include "qemu/cutils.h"
45 #include "qapi/error.h"
46 #include "qapi/qmp/qdict.h"
48 #include "migration/register.h"
49 #include "migration/vmstate.h"
50 #include "migration/qemu-file-types.h"
52 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
63 if (len
> buf_size
- 1)
72 /* slirp network adapter */
74 #define SLIRP_CFG_HOSTFWD 1
76 struct slirp_config_str
{
77 struct slirp_config_str
*next
;
84 struct in_addr server
;
89 typedef struct SlirpState
{
91 QTAILQ_ENTRY(SlirpState
) entry
;
93 Notifier poll_notifier
;
94 Notifier exit_notifier
;
95 #if defined(CONFIG_SMBD_COMMAND)
101 static struct slirp_config_str
*slirp_configs
;
102 static QTAILQ_HEAD(, SlirpState
) slirp_stacks
=
103 QTAILQ_HEAD_INITIALIZER(slirp_stacks
);
105 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
, Error
**errp
);
106 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
, Error
**errp
);
108 #if defined(CONFIG_SMBD_COMMAND)
109 static int slirp_smb(SlirpState
*s
, const char *exported_dir
,
110 struct in_addr vserver_addr
, Error
**errp
);
111 static void slirp_smb_cleanup(SlirpState
*s
);
113 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
116 static ssize_t
net_slirp_send_packet(const void *pkt
, size_t pkt_len
,
119 SlirpState
*s
= opaque
;
120 uint8_t min_pkt
[ETH_ZLEN
];
121 size_t min_pktsz
= sizeof(min_pkt
);
123 if (net_peer_needs_padding(&s
->nc
)) {
124 if (eth_pad_short_frame(min_pkt
, &min_pktsz
, pkt
, pkt_len
)) {
130 return qemu_send_packet(&s
->nc
, pkt
, pkt_len
);
133 static ssize_t
net_slirp_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
135 SlirpState
*s
= DO_UPCAST(SlirpState
, nc
, nc
);
137 slirp_input(s
->slirp
, buf
, size
);
142 static void slirp_smb_exit(Notifier
*n
, void *data
)
144 SlirpState
*s
= container_of(n
, SlirpState
, exit_notifier
);
145 slirp_smb_cleanup(s
);
148 static void slirp_free_fwd(gpointer data
)
150 struct GuestFwd
*fwd
= data
;
152 qemu_chr_fe_deinit(&fwd
->hd
, true);
156 static void net_slirp_cleanup(NetClientState
*nc
)
158 SlirpState
*s
= DO_UPCAST(SlirpState
, nc
, nc
);
160 g_slist_free_full(s
->fwd
, slirp_free_fwd
);
161 main_loop_poll_remove_notifier(&s
->poll_notifier
);
162 unregister_savevm(NULL
, "slirp", s
->slirp
);
163 slirp_cleanup(s
->slirp
);
164 if (s
->exit_notifier
.notify
) {
165 qemu_remove_exit_notifier(&s
->exit_notifier
);
167 slirp_smb_cleanup(s
);
168 QTAILQ_REMOVE(&slirp_stacks
, s
, entry
);
171 static NetClientInfo net_slirp_info
= {
172 .type
= NET_CLIENT_DRIVER_USER
,
173 .size
= sizeof(SlirpState
),
174 .receive
= net_slirp_receive
,
175 .cleanup
= net_slirp_cleanup
,
178 static void net_slirp_guest_error(const char *msg
, void *opaque
)
180 qemu_log_mask(LOG_GUEST_ERROR
, "%s", msg
);
183 static int64_t net_slirp_clock_get_ns(void *opaque
)
185 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
188 typedef struct SlirpTimer SlirpTimer
;
191 #if SLIRP_CHECK_VERSION(4,7,0)
198 #if SLIRP_CHECK_VERSION(4,7,0)
199 static void net_slirp_init_completed(Slirp
*slirp
, void *opaque
)
201 SlirpState
*s
= opaque
;
205 static void net_slirp_timer_cb(void *opaque
)
207 SlirpTimer
*t
= opaque
;
208 slirp_handle_timer(t
->slirp
, t
->id
, t
->cb_opaque
);
211 static void *net_slirp_timer_new_opaque(SlirpTimerId id
,
212 void *cb_opaque
, void *opaque
)
214 SlirpState
*s
= opaque
;
215 SlirpTimer
*t
= g_new(SlirpTimer
, 1);
218 t
->cb_opaque
= cb_opaque
;
219 timer_init_full(&t
->timer
, NULL
, QEMU_CLOCK_VIRTUAL
,
220 SCALE_MS
, QEMU_TIMER_ATTR_EXTERNAL
,
221 net_slirp_timer_cb
, t
);
225 static void *net_slirp_timer_new(SlirpTimerCb cb
,
226 void *cb_opaque
, void *opaque
)
228 SlirpTimer
*t
= g_new(SlirpTimer
, 1);
229 timer_init_full(&t
->timer
, NULL
, QEMU_CLOCK_VIRTUAL
,
230 SCALE_MS
, QEMU_TIMER_ATTR_EXTERNAL
,
236 static void net_slirp_timer_free(void *timer
, void *opaque
)
238 SlirpTimer
*t
= timer
;
239 timer_del(&t
->timer
);
243 static void net_slirp_timer_mod(void *timer
, int64_t expire_timer
,
246 SlirpTimer
*t
= timer
;
247 timer_mod(&t
->timer
, expire_timer
);
250 static void net_slirp_register_poll_fd(int fd
, void *opaque
)
253 AioContext
*ctxt
= qemu_get_aio_context();
255 if (WSAEventSelect(fd
, event_notifier_get_handle(&ctxt
->notifier
),
256 FD_READ
| FD_ACCEPT
| FD_CLOSE
|
257 FD_CONNECT
| FD_WRITE
| FD_OOB
) != 0) {
258 error_setg_win32(&error_warn
, WSAGetLastError(), "failed to WSAEventSelect()");
263 static void net_slirp_unregister_poll_fd(int fd
, void *opaque
)
266 if (WSAEventSelect(fd
, NULL
, 0) != 0) {
267 error_setg_win32(&error_warn
, WSAGetLastError(), "failed to WSAEventSelect()");
272 static void net_slirp_notify(void *opaque
)
277 static const SlirpCb slirp_cb
= {
278 .send_packet
= net_slirp_send_packet
,
279 .guest_error
= net_slirp_guest_error
,
280 .clock_get_ns
= net_slirp_clock_get_ns
,
281 #if SLIRP_CHECK_VERSION(4,7,0)
282 .init_completed
= net_slirp_init_completed
,
283 .timer_new_opaque
= net_slirp_timer_new_opaque
,
285 .timer_new
= net_slirp_timer_new
,
287 .timer_free
= net_slirp_timer_free
,
288 .timer_mod
= net_slirp_timer_mod
,
289 .register_poll_fd
= net_slirp_register_poll_fd
,
290 .unregister_poll_fd
= net_slirp_unregister_poll_fd
,
291 .notify
= net_slirp_notify
,
294 static int slirp_poll_to_gio(int events
)
298 if (events
& SLIRP_POLL_IN
) {
301 if (events
& SLIRP_POLL_OUT
) {
304 if (events
& SLIRP_POLL_PRI
) {
307 if (events
& SLIRP_POLL_ERR
) {
310 if (events
& SLIRP_POLL_HUP
) {
317 static int net_slirp_add_poll(int fd
, int events
, void *opaque
)
319 GArray
*pollfds
= opaque
;
322 .events
= slirp_poll_to_gio(events
),
324 int idx
= pollfds
->len
;
325 g_array_append_val(pollfds
, pfd
);
329 static int slirp_gio_to_poll(int events
)
333 if (events
& G_IO_IN
) {
334 ret
|= SLIRP_POLL_IN
;
336 if (events
& G_IO_OUT
) {
337 ret
|= SLIRP_POLL_OUT
;
339 if (events
& G_IO_PRI
) {
340 ret
|= SLIRP_POLL_PRI
;
342 if (events
& G_IO_ERR
) {
343 ret
|= SLIRP_POLL_ERR
;
345 if (events
& G_IO_HUP
) {
346 ret
|= SLIRP_POLL_HUP
;
352 static int net_slirp_get_revents(int idx
, void *opaque
)
354 GArray
*pollfds
= opaque
;
356 return slirp_gio_to_poll(g_array_index(pollfds
, GPollFD
, idx
).revents
);
359 static void net_slirp_poll_notify(Notifier
*notifier
, void *data
)
361 MainLoopPoll
*poll
= data
;
362 SlirpState
*s
= container_of(notifier
, SlirpState
, poll_notifier
);
364 switch (poll
->state
) {
365 case MAIN_LOOP_POLL_FILL
:
366 slirp_pollfds_fill(s
->slirp
, &poll
->timeout
,
367 net_slirp_add_poll
, poll
->pollfds
);
369 case MAIN_LOOP_POLL_OK
:
370 case MAIN_LOOP_POLL_ERR
:
371 slirp_pollfds_poll(s
->slirp
, poll
->state
== MAIN_LOOP_POLL_ERR
,
372 net_slirp_get_revents
, poll
->pollfds
);
375 g_assert_not_reached();
380 net_slirp_stream_read(void *buf
, size_t size
, void *opaque
)
382 QEMUFile
*f
= opaque
;
384 return qemu_get_buffer(f
, buf
, size
);
388 net_slirp_stream_write(const void *buf
, size_t size
, void *opaque
)
390 QEMUFile
*f
= opaque
;
392 qemu_put_buffer(f
, buf
, size
);
393 if (qemu_file_get_error(f
)) {
400 static int net_slirp_state_load(QEMUFile
*f
, void *opaque
, int version_id
)
402 Slirp
*slirp
= opaque
;
404 return slirp_state_load(slirp
, version_id
, net_slirp_stream_read
, f
);
407 static void net_slirp_state_save(QEMUFile
*f
, void *opaque
)
409 Slirp
*slirp
= opaque
;
411 slirp_state_save(slirp
, net_slirp_stream_write
, f
);
414 static SaveVMHandlers savevm_slirp_state
= {
415 .save_state
= net_slirp_state_save
,
416 .load_state
= net_slirp_state_load
,
419 static int net_slirp_init(NetClientState
*peer
, const char *model
,
420 const char *name
, int restricted
,
421 bool ipv4
, const char *vnetwork
, const char *vhost
,
422 bool ipv6
, const char *vprefix6
, int vprefix6_len
,
424 const char *vhostname
, const char *tftp_export
,
425 const char *bootfile
, const char *vdhcp_start
,
426 const char *vnameserver
, const char *vnameserver6
,
427 const char *smb_export
, const char *vsmbserver
,
428 const char **dnssearch
, const char *vdomainname
,
429 const char *tftp_server_name
,
432 /* default settings according to historic slirp */
433 struct in_addr net
= { .s_addr
= htonl(0x0a000200) }; /* 10.0.2.0 */
434 struct in_addr mask
= { .s_addr
= htonl(0xffffff00) }; /* 255.255.255.0 */
435 struct in_addr host
= { .s_addr
= htonl(0x0a000202) }; /* 10.0.2.2 */
436 struct in_addr dhcp
= { .s_addr
= htonl(0x0a00020f) }; /* 10.0.2.15 */
437 struct in_addr dns
= { .s_addr
= htonl(0x0a000203) }; /* 10.0.2.3 */
438 struct in6_addr ip6_prefix
;
439 struct in6_addr ip6_host
;
440 struct in6_addr ip6_dns
;
441 #if defined(CONFIG_SMBD_COMMAND)
442 struct in_addr smbsrv
= { .s_addr
= 0 };
444 SlirpConfig cfg
= { 0 };
451 struct slirp_config_str
*config
;
453 if (!ipv4
&& (vnetwork
|| vhost
|| vnameserver
)) {
454 error_setg(errp
, "IPv4 disabled but netmask/host/dns provided");
458 if (!ipv6
&& (vprefix6
|| vhost6
|| vnameserver6
)) {
459 error_setg(errp
, "IPv6 disabled but prefix/host6/dns6 provided");
463 if (!ipv4
&& !ipv6
) {
464 /* It doesn't make sense to disable both */
465 error_setg(errp
, "IPv4 and IPv6 disabled");
470 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
471 if (!inet_aton(vnetwork
, &net
)) {
472 error_setg(errp
, "Failed to parse netmask");
475 addr
= ntohl(net
.s_addr
);
476 if (!(addr
& 0x80000000)) {
477 mask
.s_addr
= htonl(0xff000000); /* class A */
478 } else if ((addr
& 0xfff00000) == 0xac100000) {
479 mask
.s_addr
= htonl(0xfff00000); /* priv. 172.16.0.0/12 */
480 } else if ((addr
& 0xc0000000) == 0x80000000) {
481 mask
.s_addr
= htonl(0xffff0000); /* class B */
482 } else if ((addr
& 0xffff0000) == 0xc0a80000) {
483 mask
.s_addr
= htonl(0xffff0000); /* priv. 192.168.0.0/16 */
484 } else if ((addr
& 0xffff0000) == 0xc6120000) {
485 mask
.s_addr
= htonl(0xfffe0000); /* tests 198.18.0.0/15 */
486 } else if ((addr
& 0xe0000000) == 0xe0000000) {
487 mask
.s_addr
= htonl(0xffffff00); /* class C */
489 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
492 if (!inet_aton(buf
, &net
)) {
493 error_setg(errp
, "Failed to parse netmask");
496 shift
= strtol(vnetwork
, &end
, 10);
498 if (!inet_aton(vnetwork
, &mask
)) {
500 "Failed to parse netmask (trailing chars)");
503 } else if (shift
< 4 || shift
> 32) {
505 "Invalid netmask provided (must be in range 4-32)");
508 mask
.s_addr
= htonl(0xffffffff << (32 - shift
));
511 net
.s_addr
&= mask
.s_addr
;
512 host
.s_addr
= net
.s_addr
| (htonl(0x0202) & ~mask
.s_addr
);
513 dhcp
.s_addr
= net
.s_addr
| (htonl(0x020f) & ~mask
.s_addr
);
514 dns
.s_addr
= net
.s_addr
| (htonl(0x0203) & ~mask
.s_addr
);
517 if (vhost
&& !inet_aton(vhost
, &host
)) {
518 error_setg(errp
, "Failed to parse host");
521 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
522 error_setg(errp
, "Host doesn't belong to network");
526 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
527 error_setg(errp
, "Failed to parse DNS");
530 if (restricted
&& (dns
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
531 error_setg(errp
, "DNS doesn't belong to network");
534 if (dns
.s_addr
== host
.s_addr
) {
535 error_setg(errp
, "DNS must be different from host");
539 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
540 error_setg(errp
, "Failed to parse DHCP start address");
543 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
544 error_setg(errp
, "DHCP doesn't belong to network");
547 if (dhcp
.s_addr
== host
.s_addr
|| dhcp
.s_addr
== dns
.s_addr
) {
548 error_setg(errp
, "DHCP must be different from host and DNS");
552 #if defined(CONFIG_SMBD_COMMAND)
553 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
554 error_setg(errp
, "Failed to parse SMB address");
562 if (!inet_pton(AF_INET6
, vprefix6
, &ip6_prefix
)) {
563 error_setg(errp
, "Failed to parse IPv6 prefix");
570 if (vprefix6_len
< 0 || vprefix6_len
> 126) {
572 "Invalid IPv6 prefix provided "
573 "(IPv6 prefix length must be between 0 and 126)");
578 if (!inet_pton(AF_INET6
, vhost6
, &ip6_host
)) {
579 error_setg(errp
, "Failed to parse IPv6 host");
582 if (!in6_equal_net(&ip6_prefix
, &ip6_host
, vprefix6_len
)) {
583 error_setg(errp
, "IPv6 Host doesn't belong to network");
587 ip6_host
= ip6_prefix
;
588 ip6_host
.s6_addr
[15] |= 2;
592 if (!inet_pton(AF_INET6
, vnameserver6
, &ip6_dns
)) {
593 error_setg(errp
, "Failed to parse IPv6 DNS");
596 if (restricted
&& !in6_equal_net(&ip6_prefix
, &ip6_dns
, vprefix6_len
)) {
597 error_setg(errp
, "IPv6 DNS doesn't belong to network");
601 ip6_dns
= ip6_prefix
;
602 ip6_dns
.s6_addr
[15] |= 3;
605 if (vdomainname
&& !*vdomainname
) {
606 error_setg(errp
, "'domainname' parameter cannot be empty");
610 if (vdomainname
&& strlen(vdomainname
) > 255) {
611 error_setg(errp
, "'domainname' parameter cannot exceed 255 bytes");
615 if (vhostname
&& strlen(vhostname
) > 255) {
616 error_setg(errp
, "'vhostname' parameter cannot exceed 255 bytes");
620 if (tftp_server_name
&& strlen(tftp_server_name
) > 255) {
621 error_setg(errp
, "'tftp-server-name' parameter cannot exceed 255 bytes");
625 nc
= qemu_new_net_client(&net_slirp_info
, peer
, model
, name
);
627 qemu_set_info_str(nc
, "net=%s,restrict=%s", inet_ntoa(net
),
628 restricted
? "on" : "off");
630 s
= DO_UPCAST(SlirpState
, nc
, nc
);
632 cfg
.version
= SLIRP_CHECK_VERSION(4,7,0) ? 4 : 1;
633 cfg
.restricted
= restricted
;
634 cfg
.in_enabled
= ipv4
;
638 cfg
.in6_enabled
= ipv6
;
639 cfg
.vprefix_addr6
= ip6_prefix
;
640 cfg
.vprefix_len
= vprefix6_len
;
641 cfg
.vhost6
= ip6_host
;
642 cfg
.vhostname
= vhostname
;
643 cfg
.tftp_server_name
= tftp_server_name
;
644 cfg
.tftp_path
= tftp_export
;
645 cfg
.bootfile
= bootfile
;
646 cfg
.vdhcp_start
= dhcp
;
647 cfg
.vnameserver
= dns
;
648 cfg
.vnameserver6
= ip6_dns
;
649 cfg
.vdnssearch
= dnssearch
;
650 cfg
.vdomainname
= vdomainname
;
651 s
->slirp
= slirp_new(&cfg
, &slirp_cb
, s
);
652 QTAILQ_INSERT_TAIL(&slirp_stacks
, s
, entry
);
655 * Make sure the current bitstream version of slirp is 4, to avoid
656 * QEMU migration incompatibilities, if upstream slirp bumped the
659 * FIXME: use bitfields of features? teach libslirp to save with
662 g_assert(slirp_state_version() == 4);
663 register_savevm_live("slirp", VMSTATE_INSTANCE_ID_ANY
,
664 slirp_state_version(), &savevm_slirp_state
, s
->slirp
);
666 s
->poll_notifier
.notify
= net_slirp_poll_notify
;
667 main_loop_poll_add_notifier(&s
->poll_notifier
);
669 for (config
= slirp_configs
; config
; config
= config
->next
) {
670 if (config
->flags
& SLIRP_CFG_HOSTFWD
) {
671 if (slirp_hostfwd(s
, config
->str
, errp
) < 0) {
675 if (slirp_guestfwd(s
, config
->str
, errp
) < 0) {
680 #if defined(CONFIG_SMBD_COMMAND)
682 if (slirp_smb(s
, smb_export
, smbsrv
, errp
) < 0) {
688 s
->exit_notifier
.notify
= slirp_smb_exit
;
689 qemu_add_exit_notifier(&s
->exit_notifier
);
693 qemu_del_net_client(nc
);
697 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *id
)
700 NetClientState
*nc
= qemu_find_netdev(id
);
702 monitor_printf(mon
, "unrecognized netdev id '%s'\n", id
);
705 if (strcmp(nc
->model
, "user")) {
706 monitor_printf(mon
, "invalid device specified\n");
709 return DO_UPCAST(SlirpState
, nc
, nc
);
711 if (QTAILQ_EMPTY(&slirp_stacks
)) {
712 monitor_printf(mon
, "user mode network stack not in use\n");
715 return QTAILQ_FIRST(&slirp_stacks
);
719 void hmp_hostfwd_remove(Monitor
*mon
, const QDict
*qdict
)
721 struct sockaddr_in host_addr
= {
722 .sin_family
= AF_INET
,
724 .s_addr
= INADDR_ANY
,
729 const char *src_str
, *p
;
733 const char *arg1
= qdict_get_str(qdict
, "arg1");
734 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
737 s
= slirp_lookup(mon
, arg1
);
740 s
= slirp_lookup(mon
, NULL
);
748 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
752 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
754 } else if (!strcmp(buf
, "udp")) {
760 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
763 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
.sin_addr
)) {
767 if (qemu_strtoi(p
, NULL
, 10, &host_port
)) {
770 host_addr
.sin_port
= htons(host_port
);
772 #if SLIRP_CHECK_VERSION(4, 5, 0)
773 err
= slirp_remove_hostxfwd(s
->slirp
, (struct sockaddr
*) &host_addr
,
774 sizeof(host_addr
), is_udp
? SLIRP_HOSTFWD_UDP
: 0);
776 err
= slirp_remove_hostfwd(s
->slirp
, is_udp
, host_addr
.sin_addr
, host_port
);
779 monitor_printf(mon
, "host forwarding rule for %s %s\n", src_str
,
780 err
? "not found" : "removed");
784 monitor_printf(mon
, "invalid format\n");
787 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
, Error
**errp
)
789 struct sockaddr_in host_addr
= {
790 .sin_family
= AF_INET
,
792 .s_addr
= INADDR_ANY
,
795 struct sockaddr_in guest_addr
= {
796 .sin_family
= AF_INET
,
802 int host_port
, guest_port
;
807 const char *fail_reason
= "Unknown reason";
810 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
811 fail_reason
= "No : separators";
814 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
816 } else if (!strcmp(buf
, "udp")) {
819 fail_reason
= "Bad protocol name";
823 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
824 fail_reason
= "Missing : separator";
827 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
.sin_addr
)) {
828 fail_reason
= "Bad host address";
832 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
833 fail_reason
= "Bad host port separator";
836 err
= qemu_strtoi(buf
, &end
, 0, &host_port
);
837 if (err
|| host_port
< 0 || host_port
> 65535) {
838 fail_reason
= "Bad host port";
841 host_addr
.sin_port
= htons(host_port
);
843 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
844 fail_reason
= "Missing guest address";
847 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
.sin_addr
)) {
848 fail_reason
= "Bad guest address";
852 err
= qemu_strtoi(p
, &end
, 0, &guest_port
);
853 if (err
|| guest_port
< 1 || guest_port
> 65535) {
854 fail_reason
= "Bad guest port";
857 guest_addr
.sin_port
= htons(guest_port
);
859 #if SLIRP_CHECK_VERSION(4, 5, 0)
860 err
= slirp_add_hostxfwd(s
->slirp
,
861 (struct sockaddr
*) &host_addr
, sizeof(host_addr
),
862 (struct sockaddr
*) &guest_addr
, sizeof(guest_addr
),
863 is_udp
? SLIRP_HOSTFWD_UDP
: 0);
865 err
= slirp_add_hostfwd(s
->slirp
, is_udp
,
866 host_addr
.sin_addr
, host_port
,
867 guest_addr
.sin_addr
, guest_port
);
871 error_setg(errp
, "Could not set up host forwarding rule '%s'",
878 error_setg(errp
, "Invalid host forwarding rule '%s' (%s)", redir_str
,
883 void hmp_hostfwd_add(Monitor
*mon
, const QDict
*qdict
)
885 const char *redir_str
;
887 const char *arg1
= qdict_get_str(qdict
, "arg1");
888 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
891 s
= slirp_lookup(mon
, arg1
);
894 s
= slirp_lookup(mon
, NULL
);
899 if (slirp_hostfwd(s
, redir_str
, &err
) < 0) {
900 error_report_err(err
);
906 #if defined(CONFIG_SMBD_COMMAND)
908 /* automatic user mode samba server configuration */
909 static void slirp_smb_cleanup(SlirpState
*s
)
914 gchar
*cmd
= g_strdup_printf("rm -rf %s", s
->smb_dir
);
916 if (ret
== -1 || !WIFEXITED(ret
)) {
917 error_report("'%s' failed.", cmd
);
918 } else if (WEXITSTATUS(ret
)) {
919 error_report("'%s' failed. Error code: %d",
920 cmd
, WEXITSTATUS(ret
));
928 static int slirp_smb(SlirpState
* s
, const char *exported_dir
,
929 struct in_addr vserver_addr
, Error
**errp
)
933 struct passwd
*passwd
;
936 passwd
= getpwuid(geteuid());
938 error_setg(errp
, "Failed to retrieve user name");
942 if (access(CONFIG_SMBD_COMMAND
, F_OK
)) {
943 error_setg(errp
, "Could not find '%s', please install it",
944 CONFIG_SMBD_COMMAND
);
948 if (access(exported_dir
, R_OK
| X_OK
)) {
949 error_setg(errp
, "Error accessing shared directory '%s': %s",
950 exported_dir
, strerror(errno
));
954 s
->smb_dir
= g_dir_make_tmp("qemu-smb.XXXXXX", NULL
);
956 error_setg(errp
, "Could not create samba server dir");
959 smb_conf
= g_strdup_printf("%s/%s", s
->smb_dir
, "smb.conf");
961 f
= fopen(smb_conf
, "w");
963 slirp_smb_cleanup(s
);
965 "Could not create samba server configuration file '%s'",
973 "interfaces=127.0.0.1\n"
974 "bind interfaces only=yes\n"
976 "lock directory=%s\n"
977 "state directory=%s\n"
978 "cache directory=%s\n"
979 "ncalrpc dir=%s/ncalrpc\n"
980 "log file=%s/log.smbd\n"
981 "smb passwd file=%s/smbpasswd\n"
983 "map to guest = Bad User\n"
984 "load printers = no\n"
986 "disable spoolss = yes\n"
987 "usershare max shares = 0\n"
1006 smb_cmdline
= g_strdup_printf("%s -l %s -s %s",
1007 CONFIG_SMBD_COMMAND
, s
->smb_dir
, smb_conf
);
1010 if (slirp_add_exec(s
->slirp
, smb_cmdline
, &vserver_addr
, 139) < 0 ||
1011 slirp_add_exec(s
->slirp
, smb_cmdline
, &vserver_addr
, 445) < 0) {
1012 slirp_smb_cleanup(s
);
1013 g_free(smb_cmdline
);
1014 error_setg(errp
, "Conflicting/invalid smbserver address");
1017 g_free(smb_cmdline
);
1021 #endif /* defined(CONFIG_SMBD_COMMAND) */
1023 static int guestfwd_can_read(void *opaque
)
1025 struct GuestFwd
*fwd
= opaque
;
1026 return slirp_socket_can_recv(fwd
->slirp
, fwd
->server
, fwd
->port
);
1029 static void guestfwd_read(void *opaque
, const uint8_t *buf
, int size
)
1031 struct GuestFwd
*fwd
= opaque
;
1032 slirp_socket_recv(fwd
->slirp
, fwd
->server
, fwd
->port
, buf
, size
);
1035 static ssize_t
guestfwd_write(const void *buf
, size_t len
, void *chr
)
1037 return qemu_chr_fe_write_all(chr
, buf
, len
);
1040 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
, Error
**errp
)
1043 struct in_addr server
= { .s_addr
= 0 };
1044 struct GuestFwd
*fwd
;
1051 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1054 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
1057 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1060 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
1063 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
1066 port
= strtol(buf
, &end
, 10);
1067 if (*end
!= '\0' || port
< 1 || port
> 65535) {
1071 snprintf(buf
, sizeof(buf
), "guestfwd.tcp.%d", port
);
1073 if (g_str_has_prefix(p
, "cmd:")) {
1074 if (slirp_add_exec(s
->slirp
, &p
[4], &server
, port
) < 0) {
1075 error_setg(errp
, "Conflicting/invalid host:port in guest "
1076 "forwarding rule '%s'", config_str
);
1082 * FIXME: sure we want to support implicit
1083 * muxed monitors here?
1085 Chardev
*chr
= qemu_chr_new_mux_mon(buf
, p
, NULL
);
1088 error_setg(errp
, "Could not open guest forwarding device '%s'",
1093 fwd
= g_new(struct GuestFwd
, 1);
1094 qemu_chr_fe_init(&fwd
->hd
, chr
, &err
);
1096 error_propagate(errp
, err
);
1097 object_unparent(OBJECT(chr
));
1102 if (slirp_add_guestfwd(s
->slirp
, guestfwd_write
, &fwd
->hd
,
1103 &server
, port
) < 0) {
1104 error_setg(errp
, "Conflicting/invalid host:port in guest "
1105 "forwarding rule '%s'", config_str
);
1106 qemu_chr_fe_deinit(&fwd
->hd
, true);
1110 fwd
->server
= server
;
1112 fwd
->slirp
= s
->slirp
;
1114 qemu_chr_fe_set_handlers(&fwd
->hd
, guestfwd_can_read
, guestfwd_read
,
1115 NULL
, NULL
, fwd
, NULL
, true);
1116 s
->fwd
= g_slist_append(s
->fwd
, fwd
);
1121 error_setg(errp
, "Invalid guest forwarding rule '%s'", config_str
);
1125 void hmp_info_usernet(Monitor
*mon
, const QDict
*qdict
)
1129 QTAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
1131 bool got_hub_id
= net_hub_id_for_client(&s
->nc
, &id
) == 0;
1132 char *info
= slirp_connection_info(s
->slirp
);
1133 monitor_printf(mon
, "Hub %d (%s):\n%s",
1134 got_hub_id
? id
: -1,
1141 net_init_slirp_configs(const StringList
*fwd
, int flags
)
1144 struct slirp_config_str
*config
;
1146 config
= g_malloc0(sizeof(*config
));
1147 pstrcpy(config
->str
, sizeof(config
->str
), fwd
->value
->str
);
1148 config
->flags
= flags
;
1149 config
->next
= slirp_configs
;
1150 slirp_configs
= config
;
1156 static const char **slirp_dnssearch(const StringList
*dnsname
)
1158 const StringList
*c
= dnsname
;
1159 size_t i
= 0, num_opts
= 0;
1167 if (num_opts
== 0) {
1171 ret
= g_malloc((num_opts
+ 1) * sizeof(*ret
));
1174 ret
[i
++] = c
->value
->str
;
1181 int net_init_slirp(const Netdev
*netdev
, const char *name
,
1182 NetClientState
*peer
, Error
**errp
)
1184 struct slirp_config_str
*config
;
1187 const NetdevUserOptions
*user
;
1188 const char **dnssearch
;
1189 bool ipv4
= true, ipv6
= true;
1191 assert(netdev
->type
== NET_CLIENT_DRIVER_USER
);
1192 user
= &netdev
->u
.user
;
1194 if ((user
->has_ipv6
&& user
->ipv6
&& !user
->has_ipv4
) ||
1195 (user
->has_ipv4
&& !user
->ipv4
)) {
1198 if ((user
->has_ipv4
&& user
->ipv4
&& !user
->has_ipv6
) ||
1199 (user
->has_ipv6
&& !user
->ipv6
)) {
1203 vnet
= user
->net
? g_strdup(user
->net
) :
1204 user
->ip
? g_strdup_printf("%s/24", user
->ip
) :
1207 dnssearch
= slirp_dnssearch(user
->dnssearch
);
1209 /* all optional fields are initialized to "all bits zero" */
1211 net_init_slirp_configs(user
->hostfwd
, SLIRP_CFG_HOSTFWD
);
1212 net_init_slirp_configs(user
->guestfwd
, 0);
1214 ret
= net_slirp_init(peer
, "user", name
, user
->q_restrict
,
1215 ipv4
, vnet
, user
->host
,
1216 ipv6
, user
->ipv6_prefix
, user
->ipv6_prefixlen
,
1217 user
->ipv6_host
, user
->hostname
, user
->tftp
,
1218 user
->bootfile
, user
->dhcpstart
,
1219 user
->dns
, user
->ipv6_dns
, user
->smb
,
1220 user
->smbserver
, dnssearch
, user
->domainname
,
1221 user
->tftp_server_name
, errp
);
1223 while (slirp_configs
) {
1224 config
= slirp_configs
;
1225 slirp_configs
= config
->next
;