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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 #include <sys/select.h>
51 #if defined(__FreeBSD__) || defined(__DragonFly__)
56 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
57 #include <freebsd/stdlib.h>
62 #include <linux/rtc.h>
64 /* For the benefit of older linux systems which don't supply it,
65 we use a local copy of hpet.h. */
66 /* #include <linux/hpet.h> */
69 #include <linux/ppdev.h>
70 #include <linux/parport.h>
74 #include <sys/ethernet.h>
75 #include <sys/sockio.h>
76 #include <netinet/arp.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/ip_icmp.h> // must come after ip.h
81 #include <netinet/udp.h>
82 #include <netinet/tcp.h>
90 #if defined(__OpenBSD__)
94 #if defined(CONFIG_VDE)
95 #include <libvdeplug.h>
98 #include "qemu-common.h"
103 #include "qemu-timer.h"
104 #include "qemu-char.h"
105 #include "audio/audio.h"
106 #include "qemu_socket.h"
107 #include "qemu-log.h"
108 #include "qemu-config.h"
110 #include "slirp/libslirp.h"
112 static QTAILQ_HEAD(, VLANState
) vlans
;
113 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
115 /***********************************************************/
116 /* network device redirectors */
118 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
119 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
123 for(i
=0;i
<size
;i
+=16) {
127 fprintf(f
, "%08x ", i
);
130 fprintf(f
, " %02x", buf
[i
+j
]);
137 if (c
< ' ' || c
> '~')
146 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
153 offset
= strtol(p
, &last_char
, 0);
154 if (0 == errno
&& '\0' == *last_char
&&
155 offset
>= 0 && offset
<= 0xFFFFFF) {
156 macaddr
[3] = (offset
& 0xFF0000) >> 16;
157 macaddr
[4] = (offset
& 0xFF00) >> 8;
158 macaddr
[5] = offset
& 0xFF;
161 for(i
= 0; i
< 6; i
++) {
162 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
167 if (*p
!= ':' && *p
!= '-')
178 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
189 if (len
> buf_size
- 1)
198 int parse_host_src_port(struct sockaddr_in
*haddr
,
199 struct sockaddr_in
*saddr
,
200 const char *input_str
)
202 char *str
= strdup(input_str
);
203 char *host_str
= str
;
205 const char *src_str2
;
209 * Chop off any extra arguments at the end of the string which
210 * would start with a comma, then fill in the src port information
211 * if it was provided else use the "any address" and "any port".
213 if ((ptr
= strchr(str
,',')))
216 if ((src_str
= strchr(input_str
,'@'))) {
221 if (parse_host_port(haddr
, host_str
) < 0)
225 if (!src_str
|| *src_str
== '\0')
228 if (parse_host_port(saddr
, src_str2
) < 0)
239 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
247 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
249 saddr
->sin_family
= AF_INET
;
250 if (buf
[0] == '\0') {
251 saddr
->sin_addr
.s_addr
= 0;
253 if (qemu_isdigit(buf
[0])) {
254 if (!inet_aton(buf
, &saddr
->sin_addr
))
257 if ((he
= gethostbyname(buf
)) == NULL
)
259 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
262 port
= strtol(p
, (char **)&r
, 0);
265 saddr
->sin_port
= htons(port
);
269 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
271 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
272 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
274 macaddr
[0], macaddr
[1], macaddr
[2],
275 macaddr
[3], macaddr
[4], macaddr
[5]);
278 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
280 static int index
= 0;
281 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
283 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
285 macaddr
->a
[0] = 0x52;
286 macaddr
->a
[1] = 0x54;
287 macaddr
->a
[2] = 0x00;
288 macaddr
->a
[3] = 0x12;
289 macaddr
->a
[4] = 0x34;
290 macaddr
->a
[5] = 0x56 + index
++;
293 static char *assign_name(VLANClientState
*vc1
, const char *model
)
299 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
302 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
303 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
309 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
311 return qemu_strdup(buf
);
314 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
319 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
321 const struct iovec
*iov
,
325 VLANClientState
*qemu_new_vlan_client(net_client_type type
,
327 VLANClientState
*peer
,
330 NetCanReceive
*can_receive
,
332 NetReceive
*receive_raw
,
333 NetReceiveIOV
*receive_iov
,
339 vc
= qemu_mallocz(sizeof(VLANClientState
));
342 vc
->model
= qemu_strdup(model
);
344 vc
->name
= qemu_strdup(name
);
346 vc
->name
= assign_name(vc
, model
);
347 vc
->can_receive
= can_receive
;
348 vc
->receive
= receive
;
349 vc
->receive_raw
= receive_raw
;
350 vc
->receive_iov
= receive_iov
;
351 vc
->cleanup
= cleanup
;
357 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
363 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
365 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
366 qemu_deliver_packet_iov
,
373 void qemu_del_vlan_client(VLANClientState
*vc
)
376 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
378 if (vc
->send_queue
) {
379 qemu_del_net_queue(vc
->send_queue
);
381 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
383 vc
->peer
->peer
= NULL
;
392 qemu_free(vc
->model
);
396 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
400 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
401 if (vc
->opaque
== opaque
) {
409 static VLANClientState
*
410 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
411 const char *client_str
)
416 vlan
= qemu_find_vlan(vlan_id
, 0);
418 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
422 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
423 if (!strcmp(vc
->name
, client_str
)) {
428 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
429 client_str
, vlan_id
);
435 int qemu_can_send_packet(VLANClientState
*sender
)
437 VLANState
*vlan
= sender
->vlan
;
441 if (!sender
->peer
->can_receive
||
442 sender
->peer
->can_receive(sender
->peer
)) {
453 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
458 /* no can_receive() handler, they can always receive */
459 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
466 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
472 VLANClientState
*vc
= opaque
;
478 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
)
479 return vc
->receive_raw(vc
, data
, size
);
481 return vc
->receive(vc
, data
, size
);
484 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
490 VLANState
*vlan
= opaque
;
494 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
506 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
)
507 len
= vc
->receive_raw(vc
, buf
, size
);
509 len
= vc
->receive(vc
, buf
, size
);
511 ret
= (ret
>= 0) ? ret
: len
;
517 void qemu_purge_queued_packets(VLANClientState
*vc
)
521 if (!vc
->peer
&& !vc
->vlan
) {
526 queue
= vc
->peer
->send_queue
;
528 queue
= vc
->vlan
->send_queue
;
531 qemu_net_queue_purge(queue
, vc
);
534 void qemu_flush_queued_packets(VLANClientState
*vc
)
539 queue
= vc
->vlan
->send_queue
;
541 queue
= vc
->send_queue
;
544 qemu_net_queue_flush(queue
);
547 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
549 const uint8_t *buf
, int size
,
550 NetPacketSent
*sent_cb
)
555 printf("qemu_send_packet_async:\n");
556 hex_dump(stdout
, buf
, size
);
559 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
564 queue
= sender
->peer
->send_queue
;
566 queue
= sender
->vlan
->send_queue
;
569 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
572 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
573 const uint8_t *buf
, int size
,
574 NetPacketSent
*sent_cb
)
576 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
580 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
582 qemu_send_packet_async(vc
, buf
, size
, NULL
);
585 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
587 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
591 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
594 uint8_t buffer
[4096];
598 for (i
= 0; i
< iovcnt
; i
++) {
601 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
602 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
606 return vc
->receive(vc
, buffer
, offset
);
609 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
614 for (i
= 0; i
< iovcnt
; i
++)
615 offset
+= iov
[i
].iov_len
;
619 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
621 const struct iovec
*iov
,
625 VLANClientState
*vc
= opaque
;
628 return calc_iov_length(iov
, iovcnt
);
631 if (vc
->receive_iov
) {
632 return vc
->receive_iov(vc
, iov
, iovcnt
);
634 return vc_sendv_compat(vc
, iov
, iovcnt
);
638 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
640 const struct iovec
*iov
,
644 VLANState
*vlan
= opaque
;
648 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
656 ret
= calc_iov_length(iov
, iovcnt
);
660 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
662 if (vc
->receive_iov
) {
663 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
665 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
668 ret
= (ret
>= 0) ? ret
: len
;
674 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
675 const struct iovec
*iov
, int iovcnt
,
676 NetPacketSent
*sent_cb
)
680 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
681 return calc_iov_length(iov
, iovcnt
);
685 queue
= sender
->peer
->send_queue
;
687 queue
= sender
->vlan
->send_queue
;
690 return qemu_net_queue_send_iov(queue
, sender
,
691 QEMU_NET_PACKET_FLAG_NONE
,
692 iov
, iovcnt
, sent_cb
);
696 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
698 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
701 #if defined(CONFIG_SLIRP)
703 /* slirp network adapter */
705 #define SLIRP_CFG_HOSTFWD 1
706 #define SLIRP_CFG_LEGACY 2
708 struct slirp_config_str
{
709 struct slirp_config_str
*next
;
715 typedef struct SlirpState
{
716 QTAILQ_ENTRY(SlirpState
) entry
;
724 static struct slirp_config_str
*slirp_configs
;
725 const char *legacy_tftp_prefix
;
726 const char *legacy_bootp_filename
;
727 static QTAILQ_HEAD(slirp_stacks
, SlirpState
) slirp_stacks
=
728 QTAILQ_HEAD_INITIALIZER(slirp_stacks
);
730 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
,
732 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
,
736 static const char *legacy_smb_export
;
738 static int slirp_smb(SlirpState
*s
, const char *exported_dir
,
739 struct in_addr vserver_addr
);
740 static void slirp_smb_cleanup(SlirpState
*s
);
742 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
745 int slirp_can_output(void *opaque
)
747 SlirpState
*s
= opaque
;
749 return qemu_can_send_packet(s
->vc
);
752 void slirp_output(void *opaque
, const uint8_t *pkt
, int pkt_len
)
754 SlirpState
*s
= opaque
;
757 printf("slirp output:\n");
758 hex_dump(stdout
, pkt
, pkt_len
);
760 qemu_send_packet(s
->vc
, pkt
, pkt_len
);
763 static ssize_t
slirp_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
765 SlirpState
*s
= vc
->opaque
;
768 printf("slirp input:\n");
769 hex_dump(stdout
, buf
, size
);
771 slirp_input(s
->slirp
, buf
, size
);
775 static void net_slirp_cleanup(VLANClientState
*vc
)
777 SlirpState
*s
= vc
->opaque
;
779 slirp_cleanup(s
->slirp
);
780 slirp_smb_cleanup(s
);
781 QTAILQ_REMOVE(&slirp_stacks
, s
, entry
);
785 static int net_slirp_init(VLANState
*vlan
, const char *model
,
786 const char *name
, int restricted
,
787 const char *vnetwork
, const char *vhost
,
788 const char *vhostname
, const char *tftp_export
,
789 const char *bootfile
, const char *vdhcp_start
,
790 const char *vnameserver
, const char *smb_export
,
791 const char *vsmbserver
)
793 /* default settings according to historic slirp */
794 struct in_addr net
= { .s_addr
= htonl(0x0a000200) }; /* 10.0.2.0 */
795 struct in_addr mask
= { .s_addr
= htonl(0xffffff00) }; /* 255.255.255.0 */
796 struct in_addr host
= { .s_addr
= htonl(0x0a000202) }; /* 10.0.2.2 */
797 struct in_addr dhcp
= { .s_addr
= htonl(0x0a00020f) }; /* 10.0.2.15 */
798 struct in_addr dns
= { .s_addr
= htonl(0x0a000203) }; /* 10.0.2.3 */
800 struct in_addr smbsrv
= { .s_addr
= 0 };
807 struct slirp_config_str
*config
;
810 tftp_export
= legacy_tftp_prefix
;
813 bootfile
= legacy_bootp_filename
;
817 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
818 if (!inet_aton(vnetwork
, &net
)) {
821 addr
= ntohl(net
.s_addr
);
822 if (!(addr
& 0x80000000)) {
823 mask
.s_addr
= htonl(0xff000000); /* class A */
824 } else if ((addr
& 0xfff00000) == 0xac100000) {
825 mask
.s_addr
= htonl(0xfff00000); /* priv. 172.16.0.0/12 */
826 } else if ((addr
& 0xc0000000) == 0x80000000) {
827 mask
.s_addr
= htonl(0xffff0000); /* class B */
828 } else if ((addr
& 0xffff0000) == 0xc0a80000) {
829 mask
.s_addr
= htonl(0xffff0000); /* priv. 192.168.0.0/16 */
830 } else if ((addr
& 0xffff0000) == 0xc6120000) {
831 mask
.s_addr
= htonl(0xfffe0000); /* tests 198.18.0.0/15 */
832 } else if ((addr
& 0xe0000000) == 0xe0000000) {
833 mask
.s_addr
= htonl(0xffffff00); /* class C */
835 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
838 if (!inet_aton(buf
, &net
)) {
841 shift
= strtol(vnetwork
, &end
, 10);
843 if (!inet_aton(vnetwork
, &mask
)) {
846 } else if (shift
< 4 || shift
> 32) {
849 mask
.s_addr
= htonl(0xffffffff << (32 - shift
));
852 net
.s_addr
&= mask
.s_addr
;
853 host
.s_addr
= net
.s_addr
| (htonl(0x0202) & ~mask
.s_addr
);
854 dhcp
.s_addr
= net
.s_addr
| (htonl(0x020f) & ~mask
.s_addr
);
855 dns
.s_addr
= net
.s_addr
| (htonl(0x0203) & ~mask
.s_addr
);
858 if (vhost
&& !inet_aton(vhost
, &host
)) {
861 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
865 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
868 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
||
869 dhcp
.s_addr
== host
.s_addr
|| dhcp
.s_addr
== dns
.s_addr
) {
873 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
876 if ((dns
.s_addr
& mask
.s_addr
) != net
.s_addr
||
877 dns
.s_addr
== host
.s_addr
) {
882 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
887 s
= qemu_mallocz(sizeof(SlirpState
));
888 s
->slirp
= slirp_init(restricted
, net
, mask
, host
, vhostname
,
889 tftp_export
, bootfile
, dhcp
, dns
, s
);
890 QTAILQ_INSERT_TAIL(&slirp_stacks
, s
, entry
);
892 for (config
= slirp_configs
; config
; config
= config
->next
) {
893 if (config
->flags
& SLIRP_CFG_HOSTFWD
) {
894 if (slirp_hostfwd(s
, config
->str
,
895 config
->flags
& SLIRP_CFG_LEGACY
) < 0)
898 if (slirp_guestfwd(s
, config
->str
,
899 config
->flags
& SLIRP_CFG_LEGACY
) < 0)
905 smb_export
= legacy_smb_export
;
908 if (slirp_smb(s
, smb_export
, smbsrv
) < 0)
913 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_SLIRP
,
914 vlan
, NULL
, model
, name
, NULL
,
915 slirp_receive
, NULL
, NULL
,
916 net_slirp_cleanup
, s
);
917 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
918 "net=%s, restricted=%c", inet_ntoa(net
), restricted
? 'y' : 'n');
922 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *vlan
,
928 vc
= qemu_find_vlan_client_by_name(mon
, strtol(vlan
, NULL
, 0), stack
);
932 if (strcmp(vc
->model
, "user")) {
933 monitor_printf(mon
, "invalid device specified\n");
938 if (QTAILQ_EMPTY(&slirp_stacks
)) {
939 monitor_printf(mon
, "user mode network stack not in use\n");
942 return QTAILQ_FIRST(&slirp_stacks
);
946 void net_slirp_hostfwd_remove(Monitor
*mon
, const QDict
*qdict
)
948 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
951 const char *src_str
, *p
;
955 const char *arg1
= qdict_get_str(qdict
, "arg1");
956 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
957 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
960 s
= slirp_lookup(mon
, arg1
, arg2
);
963 s
= slirp_lookup(mon
, NULL
, NULL
);
970 if (!src_str
|| !src_str
[0])
974 get_str_sep(buf
, sizeof(buf
), &p
, ':');
976 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
978 } else if (!strcmp(buf
, "udp")) {
984 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
987 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
993 err
= slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks
)->slirp
, is_udp
,
994 host_addr
, host_port
);
996 monitor_printf(mon
, "host forwarding rule for %s %s\n", src_str
,
997 err
? "removed" : "not found");
1001 monitor_printf(mon
, "invalid format\n");
1004 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
,
1007 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
1008 struct in_addr guest_addr
= { .s_addr
= 0 };
1009 int host_port
, guest_port
;
1016 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1019 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
1021 } else if (!strcmp(buf
, "udp")) {
1027 if (!legacy_format
) {
1028 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1031 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
1036 if (get_str_sep(buf
, sizeof(buf
), &p
, legacy_format
? ':' : '-') < 0) {
1039 host_port
= strtol(buf
, &end
, 0);
1040 if (*end
!= '\0' || host_port
< 1 || host_port
> 65535) {
1044 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1047 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
)) {
1051 guest_port
= strtol(p
, &end
, 0);
1052 if (*end
!= '\0' || guest_port
< 1 || guest_port
> 65535) {
1056 if (slirp_add_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
, guest_addr
,
1058 qemu_error("could not set up host forwarding rule '%s'\n",
1065 qemu_error("invalid host forwarding rule '%s'\n", redir_str
);
1069 void net_slirp_hostfwd_add(Monitor
*mon
, const QDict
*qdict
)
1071 const char *redir_str
;
1073 const char *arg1
= qdict_get_str(qdict
, "arg1");
1074 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
1075 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
1078 s
= slirp_lookup(mon
, arg1
, arg2
);
1081 s
= slirp_lookup(mon
, NULL
, NULL
);
1085 slirp_hostfwd(s
, redir_str
, 0);
1090 int net_slirp_redir(const char *redir_str
)
1092 struct slirp_config_str
*config
;
1094 if (QTAILQ_EMPTY(&slirp_stacks
)) {
1095 config
= qemu_malloc(sizeof(*config
));
1096 pstrcpy(config
->str
, sizeof(config
->str
), redir_str
);
1097 config
->flags
= SLIRP_CFG_HOSTFWD
| SLIRP_CFG_LEGACY
;
1098 config
->next
= slirp_configs
;
1099 slirp_configs
= config
;
1103 return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks
), redir_str
, 1);
1108 /* automatic user mode samba server configuration */
1109 static void slirp_smb_cleanup(SlirpState
*s
)
1113 if (s
->smb_dir
[0] != '\0') {
1114 snprintf(cmd
, sizeof(cmd
), "rm -rf %s", s
->smb_dir
);
1116 s
->smb_dir
[0] = '\0';
1120 static int slirp_smb(SlirpState
* s
, const char *exported_dir
,
1121 struct in_addr vserver_addr
)
1123 static int instance
;
1125 char smb_cmdline
[128];
1128 snprintf(s
->smb_dir
, sizeof(s
->smb_dir
), "/tmp/qemu-smb.%ld-%d",
1129 (long)getpid(), instance
++);
1130 if (mkdir(s
->smb_dir
, 0700) < 0) {
1131 qemu_error("could not create samba server dir '%s'\n", s
->smb_dir
);
1134 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", s
->smb_dir
, "smb.conf");
1136 f
= fopen(smb_conf
, "w");
1138 slirp_smb_cleanup(s
);
1139 qemu_error("could not create samba server configuration file '%s'\n",
1147 "socket address=127.0.0.1\n"
1148 "pid directory=%s\n"
1149 "lock directory=%s\n"
1150 "log file=%s/log.smbd\n"
1151 "smb passwd file=%s/smbpasswd\n"
1152 "security = share\n"
1166 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
1167 SMBD_COMMAND
, smb_conf
);
1169 if (slirp_add_exec(s
->slirp
, 0, smb_cmdline
, &vserver_addr
, 139) < 0) {
1170 slirp_smb_cleanup(s
);
1171 qemu_error("conflicting/invalid smbserver address\n");
1177 /* automatic user mode samba server configuration (legacy interface) */
1178 int net_slirp_smb(const char *exported_dir
)
1180 struct in_addr vserver_addr
= { .s_addr
= 0 };
1182 if (legacy_smb_export
) {
1183 fprintf(stderr
, "-smb given twice\n");
1186 legacy_smb_export
= exported_dir
;
1187 if (!QTAILQ_EMPTY(&slirp_stacks
)) {
1188 return slirp_smb(QTAILQ_FIRST(&slirp_stacks
), exported_dir
,
1194 #endif /* !defined(_WIN32) */
1197 CharDriverState
*hd
;
1198 struct in_addr server
;
1203 static int guestfwd_can_read(void *opaque
)
1205 struct GuestFwd
*fwd
= opaque
;
1206 return slirp_socket_can_recv(fwd
->slirp
, fwd
->server
, fwd
->port
);
1209 static void guestfwd_read(void *opaque
, const uint8_t *buf
, int size
)
1211 struct GuestFwd
*fwd
= opaque
;
1212 slirp_socket_recv(fwd
->slirp
, fwd
->server
, fwd
->port
, buf
, size
);
1215 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
,
1218 struct in_addr server
= { .s_addr
= 0 };
1219 struct GuestFwd
*fwd
;
1226 if (legacy_format
) {
1227 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1231 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1234 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
1237 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1240 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
1243 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
1247 port
= strtol(buf
, &end
, 10);
1248 if (*end
!= '\0' || port
< 1 || port
> 65535) {
1252 fwd
= qemu_malloc(sizeof(struct GuestFwd
));
1253 snprintf(buf
, sizeof(buf
), "guestfwd.tcp:%d", port
);
1254 fwd
->hd
= qemu_chr_open(buf
, p
, NULL
);
1256 qemu_error("could not open guest forwarding device '%s'\n", buf
);
1261 if (slirp_add_exec(s
->slirp
, 3, fwd
->hd
, &server
, port
) < 0) {
1262 qemu_error("conflicting/invalid host:port in guest forwarding "
1263 "rule '%s'\n", config_str
);
1267 fwd
->server
= server
;
1269 fwd
->slirp
= s
->slirp
;
1271 qemu_chr_add_handlers(fwd
->hd
, guestfwd_can_read
, guestfwd_read
,
1276 qemu_error("invalid guest forwarding rule '%s'\n", config_str
);
1280 void do_info_usernet(Monitor
*mon
)
1284 QTAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
1285 monitor_printf(mon
, "VLAN %d (%s):\n", s
->vc
->vlan
->id
, s
->vc
->name
);
1286 slirp_connection_info(s
->slirp
, mon
);
1290 #endif /* CONFIG_SLIRP */
1292 #if defined(CONFIG_VDE)
1293 typedef struct VDEState
{
1294 VLANClientState
*vc
;
1298 static void vde_to_qemu(void *opaque
)
1300 VDEState
*s
= opaque
;
1304 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1306 qemu_send_packet(s
->vc
, buf
, size
);
1310 static ssize_t
vde_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1312 VDEState
*s
= vc
->opaque
;
1316 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1317 } while (ret
< 0 && errno
== EINTR
);
1322 static void vde_cleanup(VLANClientState
*vc
)
1324 VDEState
*s
= vc
->opaque
;
1325 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1330 static int net_vde_init(VLANState
*vlan
, const char *model
,
1331 const char *name
, const char *sock
,
1332 int port
, const char *group
, int mode
)
1335 char *init_group
= (char *)group
;
1336 char *init_sock
= (char *)sock
;
1338 struct vde_open_args args
= {
1340 .group
= init_group
,
1344 s
= qemu_mallocz(sizeof(VDEState
));
1345 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1350 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_VDE
,
1351 vlan
, NULL
, model
, name
, NULL
,
1352 vde_receive
, NULL
, NULL
,
1354 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1355 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1356 sock
, vde_datafd(s
->vde
));
1361 /* network connection */
1362 typedef struct NetSocketState
{
1363 VLANClientState
*vc
;
1365 int state
; /* 0 = getting length, 1 = getting data */
1367 unsigned int packet_len
;
1369 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1372 typedef struct NetSocketListenState
{
1377 } NetSocketListenState
;
1379 /* XXX: we consider we can send the whole packet without blocking */
1380 static ssize_t
net_socket_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1382 NetSocketState
*s
= vc
->opaque
;
1386 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1387 return send_all(s
->fd
, buf
, size
);
1390 static ssize_t
net_socket_receive_dgram(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1392 NetSocketState
*s
= vc
->opaque
;
1394 return sendto(s
->fd
, (const void *)buf
, size
, 0,
1395 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1398 static void net_socket_send(void *opaque
)
1400 NetSocketState
*s
= opaque
;
1406 size
= recv(s
->fd
, (void *)buf1
, sizeof(buf1
), 0);
1408 err
= socket_error();
1409 if (err
!= EWOULDBLOCK
)
1411 } else if (size
== 0) {
1412 /* end of connection */
1414 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1420 /* reassemble a packet from the network */
1426 memcpy(s
->buf
+ s
->index
, buf
, l
);
1430 if (s
->index
== 4) {
1432 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1438 l
= s
->packet_len
- s
->index
;
1441 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1442 memcpy(s
->buf
+ s
->index
, buf
, l
);
1444 fprintf(stderr
, "serious error: oversized packet received,"
1445 "connection terminated.\n");
1453 if (s
->index
>= s
->packet_len
) {
1454 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1463 static void net_socket_send_dgram(void *opaque
)
1465 NetSocketState
*s
= opaque
;
1468 size
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1472 /* end of connection */
1473 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1476 qemu_send_packet(s
->vc
, s
->buf
, size
);
1479 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1484 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1485 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1486 inet_ntoa(mcastaddr
->sin_addr
),
1487 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1491 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1493 perror("socket(PF_INET, SOCK_DGRAM)");
1498 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1499 (const char *)&val
, sizeof(val
));
1501 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1505 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1511 /* Add host to multicast group */
1512 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1513 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1515 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1516 (const char *)&imr
, sizeof(struct ip_mreq
));
1518 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1522 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1524 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1525 (const char *)&val
, sizeof(val
));
1527 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1531 socket_set_nonblock(fd
);
1539 static void net_socket_cleanup(VLANClientState
*vc
)
1541 NetSocketState
*s
= vc
->opaque
;
1542 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1547 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1550 int fd
, int is_connected
)
1552 struct sockaddr_in saddr
;
1554 socklen_t saddr_len
;
1557 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1558 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1559 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1563 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1565 if (saddr
.sin_addr
.s_addr
==0) {
1566 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1570 /* clone dgram socket */
1571 newfd
= net_socket_mcast_create(&saddr
);
1573 /* error already reported by net_socket_mcast_create() */
1577 /* clone newfd to fd, close newfd */
1582 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1583 fd
, strerror(errno
));
1588 s
= qemu_mallocz(sizeof(NetSocketState
));
1591 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET
,
1592 vlan
, NULL
, model
, name
, NULL
,
1593 net_socket_receive_dgram
, NULL
, NULL
,
1594 net_socket_cleanup
, s
);
1595 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1597 /* mcast: save bound address as dst */
1598 if (is_connected
) s
->dgram_dst
=saddr
;
1600 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1601 "socket: fd=%d (%s mcast=%s:%d)",
1602 fd
, is_connected
? "cloned" : "",
1603 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1607 static void net_socket_connect(void *opaque
)
1609 NetSocketState
*s
= opaque
;
1610 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1613 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1616 int fd
, int is_connected
)
1619 s
= qemu_mallocz(sizeof(NetSocketState
));
1621 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET
,
1622 vlan
, NULL
, model
, name
, NULL
,
1623 net_socket_receive
, NULL
, NULL
,
1624 net_socket_cleanup
, s
);
1625 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1626 "socket: fd=%d", fd
);
1628 net_socket_connect(s
);
1630 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1635 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1636 const char *model
, const char *name
,
1637 int fd
, int is_connected
)
1639 int so_type
= -1, optlen
=sizeof(so_type
);
1641 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1642 (socklen_t
*)&optlen
)< 0) {
1643 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1648 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1650 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1652 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1653 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1654 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1659 static void net_socket_accept(void *opaque
)
1661 NetSocketListenState
*s
= opaque
;
1663 struct sockaddr_in saddr
;
1668 len
= sizeof(saddr
);
1669 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1670 if (fd
< 0 && errno
!= EINTR
) {
1672 } else if (fd
>= 0) {
1676 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1680 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1681 "socket: connection from %s:%d",
1682 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1686 static int net_socket_listen_init(VLANState
*vlan
,
1689 const char *host_str
)
1691 NetSocketListenState
*s
;
1693 struct sockaddr_in saddr
;
1695 if (parse_host_port(&saddr
, host_str
) < 0)
1698 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1700 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1705 socket_set_nonblock(fd
);
1707 /* allow fast reuse */
1709 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1711 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1716 ret
= listen(fd
, 0);
1722 s
->model
= qemu_strdup(model
);
1723 s
->name
= name
? qemu_strdup(name
) : NULL
;
1725 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1729 static int net_socket_connect_init(VLANState
*vlan
,
1732 const char *host_str
)
1735 int fd
, connected
, ret
, err
;
1736 struct sockaddr_in saddr
;
1738 if (parse_host_port(&saddr
, host_str
) < 0)
1741 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1746 socket_set_nonblock(fd
);
1750 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1752 err
= socket_error();
1753 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1754 } else if (err
== EINPROGRESS
) {
1757 } else if (err
== WSAEALREADY
) {
1770 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1773 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1774 "socket: connect to %s:%d",
1775 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1779 static int net_socket_mcast_init(VLANState
*vlan
,
1782 const char *host_str
)
1786 struct sockaddr_in saddr
;
1788 if (parse_host_port(&saddr
, host_str
) < 0)
1792 fd
= net_socket_mcast_create(&saddr
);
1796 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1800 s
->dgram_dst
= saddr
;
1802 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1803 "socket: mcast=%s:%d",
1804 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1809 typedef struct DumpState
{
1810 VLANClientState
*pcap_vc
;
1815 #define PCAP_MAGIC 0xa1b2c3d4
1817 struct pcap_file_hdr
{
1819 uint16_t version_major
;
1820 uint16_t version_minor
;
1827 struct pcap_sf_pkthdr
{
1836 static ssize_t
dump_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1838 DumpState
*s
= vc
->opaque
;
1839 struct pcap_sf_pkthdr hdr
;
1843 /* Early return in case of previous error. */
1848 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, get_ticks_per_sec());
1849 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
1851 hdr
.ts
.tv_sec
= ts
/ 1000000;
1852 hdr
.ts
.tv_usec
= ts
% 1000000;
1853 hdr
.caplen
= caplen
;
1855 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
1856 write(s
->fd
, buf
, caplen
) != caplen
) {
1857 qemu_log("-net dump write error - stop dump\n");
1865 static void net_dump_cleanup(VLANClientState
*vc
)
1867 DumpState
*s
= vc
->opaque
;
1873 static int net_dump_init(VLANState
*vlan
, const char *device
,
1874 const char *name
, const char *filename
, int len
)
1876 struct pcap_file_hdr hdr
;
1879 s
= qemu_malloc(sizeof(DumpState
));
1881 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
| O_BINARY
, 0644);
1883 qemu_error("-net dump: can't open %s\n", filename
);
1887 s
->pcap_caplen
= len
;
1889 hdr
.magic
= PCAP_MAGIC
;
1890 hdr
.version_major
= 2;
1891 hdr
.version_minor
= 4;
1894 hdr
.snaplen
= s
->pcap_caplen
;
1897 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
1898 qemu_error("-net dump write error: %s\n", strerror(errno
));
1904 s
->pcap_vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_DUMP
,
1905 vlan
, NULL
, device
, name
, NULL
,
1906 dump_receive
, NULL
, NULL
,
1907 net_dump_cleanup
, s
);
1908 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
1909 "dump to %s (len=%d)", filename
, len
);
1913 /* find or alloc a new VLAN */
1914 VLANState
*qemu_find_vlan(int id
, int allocate
)
1918 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1919 if (vlan
->id
== id
) {
1928 vlan
= qemu_mallocz(sizeof(VLANState
));
1930 QTAILQ_INIT(&vlan
->clients
);
1932 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
1933 qemu_vlan_deliver_packet_iov
,
1936 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
1941 VLANClientState
*qemu_find_netdev(const char *id
)
1943 VLANClientState
*vc
;
1945 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1946 if (!strcmp(vc
->name
, id
)) {
1954 static int nic_get_free_idx(void)
1958 for (index
= 0; index
< MAX_NICS
; index
++)
1959 if (!nd_table
[index
].used
)
1964 int qemu_show_nic_models(const char *arg
, const char *const *models
)
1968 if (!arg
|| strcmp(arg
, "?"))
1971 fprintf(stderr
, "qemu: Supported NIC models: ");
1972 for (i
= 0 ; models
[i
]; i
++)
1973 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1977 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1979 const char *models
[2];
1984 if (qemu_show_nic_models(nd
->model
, models
))
1986 if (qemu_find_nic_model(nd
, models
, model
) < 0)
1990 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
1991 const char *default_model
)
1996 nd
->model
= qemu_strdup(default_model
);
1998 for (i
= 0 ; models
[i
]; i
++) {
1999 if (strcmp(nd
->model
, models
[i
]) == 0)
2003 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
2007 int net_handle_fd_param(Monitor
*mon
, const char *param
)
2009 if (!qemu_isdigit(param
[0])) {
2012 fd
= monitor_get_fd(mon
, param
);
2014 qemu_error("No file descriptor named %s found", param
);
2020 return strtol(param
, NULL
, 0);
2024 static int net_init_nic(QemuOpts
*opts
,
2033 idx
= nic_get_free_idx();
2034 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2035 qemu_error("Too Many NICs\n");
2039 nd
= &nd_table
[idx
];
2041 memset(nd
, 0, sizeof(*nd
));
2043 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
2044 nd
->netdev
= qemu_find_netdev(netdev
);
2046 qemu_error("netdev '%s' not found\n", netdev
);
2054 nd
->name
= qemu_strdup(name
);
2056 if (qemu_opt_get(opts
, "model")) {
2057 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
2059 if (qemu_opt_get(opts
, "addr")) {
2060 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
2063 nd
->macaddr
[0] = 0x52;
2064 nd
->macaddr
[1] = 0x54;
2065 nd
->macaddr
[2] = 0x00;
2066 nd
->macaddr
[3] = 0x12;
2067 nd
->macaddr
[4] = 0x34;
2068 nd
->macaddr
[5] = 0x56 + idx
;
2070 if (qemu_opt_get(opts
, "macaddr") &&
2071 parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
2072 qemu_error("invalid syntax for ethernet address\n");
2076 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors", NIC_NVECTORS_UNSPECIFIED
);
2077 if (nd
->nvectors
!= NIC_NVECTORS_UNSPECIFIED
&&
2078 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
2079 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
2085 nd
->vlan
->nb_guest_devs
++;
2092 #if defined(CONFIG_SLIRP)
2093 static int net_init_slirp_configs(const char *name
, const char *value
, void *opaque
)
2095 struct slirp_config_str
*config
;
2097 if (strcmp(name
, "hostfwd") != 0 && strcmp(name
, "guestfwd") != 0) {
2101 config
= qemu_mallocz(sizeof(*config
));
2103 pstrcpy(config
->str
, sizeof(config
->str
), value
);
2105 if (!strcmp(name
, "hostfwd")) {
2106 config
->flags
= SLIRP_CFG_HOSTFWD
;
2109 config
->next
= slirp_configs
;
2110 slirp_configs
= config
;
2115 static int net_init_slirp(QemuOpts
*opts
,
2120 struct slirp_config_str
*config
;
2122 const char *vhostname
;
2123 const char *vdhcp_start
;
2124 const char *vnamesrv
;
2125 const char *tftp_export
;
2126 const char *bootfile
;
2127 const char *smb_export
;
2128 const char *vsmbsrv
;
2133 vhost
= qemu_opt_get(opts
, "host");
2134 vhostname
= qemu_opt_get(opts
, "hostname");
2135 vdhcp_start
= qemu_opt_get(opts
, "dhcpstart");
2136 vnamesrv
= qemu_opt_get(opts
, "dns");
2137 tftp_export
= qemu_opt_get(opts
, "tftp");
2138 bootfile
= qemu_opt_get(opts
, "bootfile");
2139 smb_export
= qemu_opt_get(opts
, "smb");
2140 vsmbsrv
= qemu_opt_get(opts
, "smbserver");
2142 if (qemu_opt_get(opts
, "ip")) {
2143 const char *ip
= qemu_opt_get(opts
, "ip");
2144 int l
= strlen(ip
) + strlen("/24") + 1;
2146 vnet
= qemu_malloc(l
);
2148 /* emulate legacy ip= parameter */
2149 pstrcpy(vnet
, l
, ip
);
2150 pstrcat(vnet
, l
, "/24");
2153 if (qemu_opt_get(opts
, "net")) {
2157 vnet
= qemu_strdup(qemu_opt_get(opts
, "net"));
2160 if (qemu_opt_get(opts
, "restrict") &&
2161 qemu_opt_get(opts
, "restrict")[0] == 'y') {
2165 qemu_opt_foreach(opts
, net_init_slirp_configs
, NULL
, 0);
2167 ret
= net_slirp_init(vlan
, "user", name
, restricted
, vnet
, vhost
,
2168 vhostname
, tftp_export
, bootfile
, vdhcp_start
,
2169 vnamesrv
, smb_export
, vsmbsrv
);
2171 while (slirp_configs
) {
2172 config
= slirp_configs
;
2173 slirp_configs
= config
->next
;
2177 if (ret
!= -1 && vlan
) {
2178 vlan
->nb_host_devs
++;
2185 #endif /* CONFIG_SLIRP */
2187 static int net_init_socket(QemuOpts
*opts
,
2192 if (qemu_opt_get(opts
, "fd")) {
2195 if (qemu_opt_get(opts
, "listen") ||
2196 qemu_opt_get(opts
, "connect") ||
2197 qemu_opt_get(opts
, "mcast")) {
2198 qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
2202 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
2207 if (!net_socket_fd_init(vlan
, "socket", name
, fd
, 1)) {
2211 } else if (qemu_opt_get(opts
, "listen")) {
2214 if (qemu_opt_get(opts
, "fd") ||
2215 qemu_opt_get(opts
, "connect") ||
2216 qemu_opt_get(opts
, "mcast")) {
2217 qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
2221 listen
= qemu_opt_get(opts
, "listen");
2223 if (net_socket_listen_init(vlan
, "socket", name
, listen
) == -1) {
2226 } else if (qemu_opt_get(opts
, "connect")) {
2227 const char *connect
;
2229 if (qemu_opt_get(opts
, "fd") ||
2230 qemu_opt_get(opts
, "listen") ||
2231 qemu_opt_get(opts
, "mcast")) {
2232 qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
2236 connect
= qemu_opt_get(opts
, "connect");
2238 if (net_socket_connect_init(vlan
, "socket", name
, connect
) == -1) {
2241 } else if (qemu_opt_get(opts
, "mcast")) {
2244 if (qemu_opt_get(opts
, "fd") ||
2245 qemu_opt_get(opts
, "connect") ||
2246 qemu_opt_get(opts
, "listen")) {
2247 qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
2251 mcast
= qemu_opt_get(opts
, "mcast");
2253 if (net_socket_mcast_init(vlan
, "socket", name
, mcast
) == -1) {
2257 qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
2262 vlan
->nb_host_devs
++;
2269 static int net_init_vde(QemuOpts
*opts
,
2278 sock
= qemu_opt_get(opts
, "sock");
2279 group
= qemu_opt_get(opts
, "group");
2281 port
= qemu_opt_get_number(opts
, "port", 0);
2282 mode
= qemu_opt_get_number(opts
, "mode", 0700);
2284 if (net_vde_init(vlan
, "vde", name
, sock
, port
, group
, mode
) == -1) {
2289 vlan
->nb_host_devs
++;
2296 static int net_init_dump(QemuOpts
*opts
,
2307 file
= qemu_opt_get(opts
, "file");
2309 snprintf(def_file
, sizeof(def_file
), "qemu-vlan%d.pcap", vlan
->id
);
2313 len
= qemu_opt_get_size(opts
, "len", 65536);
2315 return net_dump_init(vlan
, "dump", name
, file
, len
);
2318 #define NET_COMMON_PARAMS_DESC \
2321 .type = QEMU_OPT_STRING, \
2322 .help = "net client type (nic, tap etc.)", \
2325 .type = QEMU_OPT_NUMBER, \
2326 .help = "vlan number", \
2329 .type = QEMU_OPT_STRING, \
2330 .help = "identifier for monitor commands", \
2333 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
2338 /* magic number, but compiler will warn if too small */
2339 #define NET_MAX_DESC 20
2343 net_client_init_func init
;
2344 QemuOptDesc desc
[NET_MAX_DESC
];
2345 } net_client_types
[] = {
2349 NET_COMMON_PARAMS_DESC
,
2350 { /* end of list */ }
2354 .init
= net_init_nic
,
2356 NET_COMMON_PARAMS_DESC
,
2359 .type
= QEMU_OPT_STRING
,
2360 .help
= "id of -netdev to connect to",
2364 .type
= QEMU_OPT_STRING
,
2365 .help
= "MAC address",
2368 .type
= QEMU_OPT_STRING
,
2369 .help
= "device model (e1000, rtl8139, virtio etc.)",
2372 .type
= QEMU_OPT_STRING
,
2373 .help
= "PCI device address",
2376 .type
= QEMU_OPT_NUMBER
,
2377 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
2379 { /* end of list */ }
2384 .init
= net_init_slirp
,
2386 NET_COMMON_PARAMS_DESC
,
2389 .type
= QEMU_OPT_STRING
,
2390 .help
= "client hostname reported by the builtin DHCP server",
2393 .type
= QEMU_OPT_STRING
,
2394 .help
= "isolate the guest from the host (y|yes|n|no)",
2397 .type
= QEMU_OPT_STRING
,
2398 .help
= "legacy parameter, use net= instead",
2401 .type
= QEMU_OPT_STRING
,
2402 .help
= "IP address and optional netmask",
2405 .type
= QEMU_OPT_STRING
,
2406 .help
= "guest-visible address of the host",
2409 .type
= QEMU_OPT_STRING
,
2410 .help
= "root directory of the built-in TFTP server",
2413 .type
= QEMU_OPT_STRING
,
2414 .help
= "BOOTP filename, for use with tftp=",
2416 .name
= "dhcpstart",
2417 .type
= QEMU_OPT_STRING
,
2418 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
2421 .type
= QEMU_OPT_STRING
,
2422 .help
= "guest-visible address of the virtual nameserver",
2425 .type
= QEMU_OPT_STRING
,
2426 .help
= "root directory of the built-in SMB server",
2428 .name
= "smbserver",
2429 .type
= QEMU_OPT_STRING
,
2430 .help
= "IP address of the built-in SMB server",
2433 .type
= QEMU_OPT_STRING
,
2434 .help
= "guest port number to forward incoming TCP or UDP connections",
2437 .type
= QEMU_OPT_STRING
,
2438 .help
= "IP address and port to forward guest TCP connections",
2440 { /* end of list */ }
2445 .init
= net_init_tap
,
2447 NET_COMMON_PARAMS_DESC
,
2450 .type
= QEMU_OPT_STRING
,
2451 .help
= "interface name",
2456 .type
= QEMU_OPT_STRING
,
2457 .help
= "file descriptor of an already opened tap",
2460 .type
= QEMU_OPT_STRING
,
2461 .help
= "script to initialize the interface",
2463 .name
= "downscript",
2464 .type
= QEMU_OPT_STRING
,
2465 .help
= "script to shut down the interface",
2468 .type
= QEMU_OPT_SIZE
,
2469 .help
= "send buffer limit"
2472 .type
= QEMU_OPT_BOOL
,
2473 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
2476 { /* end of list */ }
2480 .init
= net_init_socket
,
2482 NET_COMMON_PARAMS_DESC
,
2485 .type
= QEMU_OPT_STRING
,
2486 .help
= "file descriptor of an already opened socket",
2489 .type
= QEMU_OPT_STRING
,
2490 .help
= "port number, and optional hostname, to listen on",
2493 .type
= QEMU_OPT_STRING
,
2494 .help
= "port number, and optional hostname, to connect to",
2497 .type
= QEMU_OPT_STRING
,
2498 .help
= "UDP multicast address and port number",
2500 { /* end of list */ }
2505 .init
= net_init_vde
,
2507 NET_COMMON_PARAMS_DESC
,
2510 .type
= QEMU_OPT_STRING
,
2511 .help
= "socket path",
2514 .type
= QEMU_OPT_NUMBER
,
2515 .help
= "port number",
2518 .type
= QEMU_OPT_STRING
,
2519 .help
= "group owner of socket",
2522 .type
= QEMU_OPT_NUMBER
,
2523 .help
= "permissions for socket",
2525 { /* end of list */ }
2530 .init
= net_init_dump
,
2532 NET_COMMON_PARAMS_DESC
,
2535 .type
= QEMU_OPT_SIZE
,
2536 .help
= "per-packet size limit (64k default)",
2539 .type
= QEMU_OPT_STRING
,
2540 .help
= "dump file path (default is qemu-vlan0.pcap)",
2542 { /* end of list */ }
2545 { /* end of list */ }
2548 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
2554 type
= qemu_opt_get(opts
, "type");
2556 qemu_error("No type specified for -net\n");
2561 if (strcmp(type
, "tap") != 0 &&
2563 strcmp(type
, "user") != 0 &&
2566 strcmp(type
, "vde") != 0 &&
2568 strcmp(type
, "socket") != 0) {
2569 qemu_error("The '%s' network backend type is not valid with -netdev\n",
2574 if (qemu_opt_get(opts
, "vlan")) {
2575 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
2578 if (qemu_opt_get(opts
, "name")) {
2579 qemu_error("The 'name' parameter is not valid with -netdev\n");
2582 if (!qemu_opts_id(opts
)) {
2583 qemu_error("The id= parameter is required with -netdev\n");
2588 name
= qemu_opts_id(opts
);
2590 name
= qemu_opt_get(opts
, "name");
2593 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
2594 if (!strcmp(net_client_types
[i
].type
, type
)) {
2595 VLANState
*vlan
= NULL
;
2597 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
2601 /* Do not add to a vlan if it's a -netdev or a nic with a
2602 * netdev= parameter. */
2604 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
2605 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2608 if (net_client_types
[i
].init
) {
2609 return net_client_types
[i
].init(opts
, mon
, name
, vlan
);
2616 qemu_error("Invalid -net type '%s'\n", type
);
2620 void net_client_uninit(NICInfo
*nd
)
2623 nd
->vlan
->nb_guest_devs
--;
2627 qemu_free(nd
->model
);
2628 qemu_free(nd
->name
);
2629 qemu_free(nd
->devaddr
);
2634 static int net_host_check_device(const char *device
)
2637 const char *valid_param_list
[] = { "tap", "socket", "dump"
2645 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2646 if (!strncmp(valid_param_list
[i
], device
,
2647 strlen(valid_param_list
[i
])))
2654 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
2656 const char *device
= qdict_get_str(qdict
, "device");
2657 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
2660 if (!net_host_check_device(device
)) {
2661 monitor_printf(mon
, "invalid host network device %s\n", device
);
2665 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
2667 monitor_printf(mon
, "parsing network options '%s' failed\n",
2668 opts_str
? opts_str
: "");
2672 qemu_opt_set(opts
, "type", device
);
2674 if (net_client_init(mon
, opts
, 0) < 0) {
2675 monitor_printf(mon
, "adding host network device %s failed\n", device
);
2679 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
2681 VLANClientState
*vc
;
2682 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
2683 const char *device
= qdict_get_str(qdict
, "device");
2685 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
2689 if (!net_host_check_device(vc
->model
)) {
2690 monitor_printf(mon
, "invalid host network device %s\n", device
);
2693 qemu_del_vlan_client(vc
);
2696 void net_set_boot_mask(int net_boot_mask
)
2700 /* Only the first four NICs may be bootable */
2701 net_boot_mask
= net_boot_mask
& 0xF;
2703 for (i
= 0; i
< nb_nics
; i
++) {
2704 if (net_boot_mask
& (1 << i
)) {
2705 nd_table
[i
].bootable
= 1;
2706 net_boot_mask
&= ~(1 << i
);
2710 if (net_boot_mask
) {
2711 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
2716 void do_info_network(Monitor
*mon
)
2720 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2721 VLANClientState
*vc
;
2723 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2725 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
2726 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2731 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
2734 VLANClientState
*vc
= NULL
;
2735 const char *name
= qdict_get_str(qdict
, "name");
2736 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
2738 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2739 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
2740 if (strcmp(vc
->name
, name
) == 0) {
2748 monitor_printf(mon
, "could not find network device '%s'\n", name
);
2752 if (strcmp(up_or_down
, "up") == 0)
2754 else if (strcmp(up_or_down
, "down") == 0)
2757 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2758 "valid\n", up_or_down
);
2760 if (vc
->link_status_changed
)
2761 vc
->link_status_changed(vc
);
2764 void net_cleanup(void)
2767 VLANClientState
*vc
, *next_vc
;
2769 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2770 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
2771 qemu_del_vlan_client(vc
);
2775 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
2776 qemu_del_vlan_client(vc
);
2780 static void net_check_clients(void)
2784 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2785 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2787 if (vlan
->nb_guest_devs
== 0)
2788 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2789 if (vlan
->nb_host_devs
== 0)
2791 "Warning: vlan %d is not connected to host network\n",
2796 static int net_init_client(QemuOpts
*opts
, void *dummy
)
2798 if (net_client_init(NULL
, opts
, 0) < 0)
2803 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
2805 return net_client_init(NULL
, opts
, 1);
2808 int net_init_clients(void)
2810 if (QTAILQ_EMPTY(&qemu_net_opts
.head
)) {
2811 /* if no clients, we use a default config */
2812 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
2814 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
2818 QTAILQ_INIT(&vlans
);
2819 QTAILQ_INIT(&non_vlan_clients
);
2821 if (qemu_opts_foreach(&qemu_netdev_opts
, net_init_netdev
, NULL
, 1) == -1)
2824 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
2828 net_check_clients();
2833 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
2835 #if defined(CONFIG_SLIRP)
2836 /* handle legacy -net channel,port:chr */
2837 if (!strcmp(opts_list
->name
, "net") &&
2838 !strncmp(optarg
, "channel,", strlen("channel,"))) {
2841 optarg
+= strlen("channel,");
2843 if (QTAILQ_EMPTY(&slirp_stacks
)) {
2844 struct slirp_config_str
*config
;
2846 config
= qemu_malloc(sizeof(*config
));
2847 pstrcpy(config
->str
, sizeof(config
->str
), optarg
);
2848 config
->flags
= SLIRP_CFG_LEGACY
;
2849 config
->next
= slirp_configs
;
2850 slirp_configs
= config
;
2853 ret
= slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks
), optarg
, 1);
2859 if (!qemu_opts_parse(opts_list
, optarg
, "type")) {