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(__FreeBSD_kernel__) || defined(__DragonFly__)
59 #include <linux/rtc.h>
61 /* For the benefit of older linux systems which don't supply it,
62 we use a local copy of hpet.h. */
63 /* #include <linux/hpet.h> */
66 #include <linux/ppdev.h>
67 #include <linux/parport.h>
71 #include <sys/ethernet.h>
72 #include <sys/sockio.h>
73 #include <netinet/arp.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
87 #if defined(__OpenBSD__)
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
95 #include "qemu-common.h"
100 #include "qemu-timer.h"
101 #include "qemu-char.h"
102 #include "audio/audio.h"
103 #include "qemu_socket.h"
104 #include "qemu-log.h"
105 #include "qemu-config.h"
107 #include "slirp/libslirp.h"
109 static QTAILQ_HEAD(, VLANState
) vlans
;
110 static QTAILQ_HEAD(, VLANClientState
) non_vlan_clients
;
112 /***********************************************************/
113 /* network device redirectors */
115 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
116 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
120 for(i
=0;i
<size
;i
+=16) {
124 fprintf(f
, "%08x ", i
);
127 fprintf(f
, " %02x", buf
[i
+j
]);
134 if (c
< ' ' || c
> '~')
143 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
150 offset
= strtol(p
, &last_char
, 0);
151 if (0 == errno
&& '\0' == *last_char
&&
152 offset
>= 0 && offset
<= 0xFFFFFF) {
153 macaddr
[3] = (offset
& 0xFF0000) >> 16;
154 macaddr
[4] = (offset
& 0xFF00) >> 8;
155 macaddr
[5] = offset
& 0xFF;
158 for(i
= 0; i
< 6; i
++) {
159 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
164 if (*p
!= ':' && *p
!= '-')
175 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
186 if (len
> buf_size
- 1)
195 int parse_host_src_port(struct sockaddr_in
*haddr
,
196 struct sockaddr_in
*saddr
,
197 const char *input_str
)
199 char *str
= strdup(input_str
);
200 char *host_str
= str
;
202 const char *src_str2
;
206 * Chop off any extra arguments at the end of the string which
207 * would start with a comma, then fill in the src port information
208 * if it was provided else use the "any address" and "any port".
210 if ((ptr
= strchr(str
,',')))
213 if ((src_str
= strchr(input_str
,'@'))) {
218 if (parse_host_port(haddr
, host_str
) < 0)
222 if (!src_str
|| *src_str
== '\0')
225 if (parse_host_port(saddr
, src_str2
) < 0)
236 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
244 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
246 saddr
->sin_family
= AF_INET
;
247 if (buf
[0] == '\0') {
248 saddr
->sin_addr
.s_addr
= 0;
250 if (qemu_isdigit(buf
[0])) {
251 if (!inet_aton(buf
, &saddr
->sin_addr
))
254 if ((he
= gethostbyname(buf
)) == NULL
)
256 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
259 port
= strtol(p
, (char **)&r
, 0);
262 saddr
->sin_port
= htons(port
);
266 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
268 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
269 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
271 macaddr
[0], macaddr
[1], macaddr
[2],
272 macaddr
[3], macaddr
[4], macaddr
[5]);
275 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
)
277 static int index
= 0;
278 static const MACAddr zero
= { .a
= { 0,0,0,0,0,0 } };
280 if (memcmp(macaddr
, &zero
, sizeof(zero
)) != 0)
282 macaddr
->a
[0] = 0x52;
283 macaddr
->a
[1] = 0x54;
284 macaddr
->a
[2] = 0x00;
285 macaddr
->a
[3] = 0x12;
286 macaddr
->a
[4] = 0x34;
287 macaddr
->a
[5] = 0x56 + index
++;
290 static char *assign_name(VLANClientState
*vc1
, const char *model
)
296 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
299 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
300 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0) {
306 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
308 return qemu_strdup(buf
);
311 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
316 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
318 const struct iovec
*iov
,
322 VLANClientState
*qemu_new_vlan_client(net_client_type type
,
324 VLANClientState
*peer
,
327 NetCanReceive
*can_receive
,
329 NetReceive
*receive_raw
,
330 NetReceiveIOV
*receive_iov
,
336 vc
= qemu_mallocz(sizeof(VLANClientState
));
339 vc
->model
= qemu_strdup(model
);
341 vc
->name
= qemu_strdup(name
);
343 vc
->name
= assign_name(vc
, model
);
344 vc
->can_receive
= can_receive
;
345 vc
->receive
= receive
;
346 vc
->receive_raw
= receive_raw
;
347 vc
->receive_iov
= receive_iov
;
348 vc
->cleanup
= cleanup
;
354 QTAILQ_INSERT_TAIL(&vc
->vlan
->clients
, vc
, next
);
360 QTAILQ_INSERT_TAIL(&non_vlan_clients
, vc
, next
);
362 vc
->send_queue
= qemu_new_net_queue(qemu_deliver_packet
,
363 qemu_deliver_packet_iov
,
370 void qemu_del_vlan_client(VLANClientState
*vc
)
373 QTAILQ_REMOVE(&vc
->vlan
->clients
, vc
, next
);
375 if (vc
->send_queue
) {
376 qemu_del_net_queue(vc
->send_queue
);
378 QTAILQ_REMOVE(&non_vlan_clients
, vc
, next
);
380 vc
->peer
->peer
= NULL
;
389 qemu_free(vc
->model
);
393 VLANClientState
*qemu_find_vlan_client(VLANState
*vlan
, void *opaque
)
397 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
398 if (vc
->opaque
== opaque
) {
406 static VLANClientState
*
407 qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
408 const char *client_str
)
413 vlan
= qemu_find_vlan(vlan_id
, 0);
415 monitor_printf(mon
, "unknown VLAN %d\n", vlan_id
);
419 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
420 if (!strcmp(vc
->name
, client_str
)) {
425 monitor_printf(mon
, "can't find device %s on VLAN %d\n",
426 client_str
, vlan_id
);
432 int qemu_can_send_packet(VLANClientState
*sender
)
434 VLANState
*vlan
= sender
->vlan
;
438 if (sender
->peer
->receive_disabled
) {
440 } else if (sender
->peer
->can_receive
&&
441 !sender
->peer
->can_receive(sender
->peer
)) {
452 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
457 /* no can_receive() handler, they can always receive */
458 if (!vc
->can_receive
|| vc
->can_receive(vc
)) {
465 static ssize_t
qemu_deliver_packet(VLANClientState
*sender
,
471 VLANClientState
*vc
= opaque
;
478 if (vc
->receive_disabled
) {
482 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
) {
483 ret
= vc
->receive_raw(vc
, data
, size
);
485 ret
= vc
->receive(vc
, data
, size
);
489 vc
->receive_disabled
= 1;
495 static ssize_t
qemu_vlan_deliver_packet(VLANClientState
*sender
,
501 VLANState
*vlan
= opaque
;
505 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
517 if (vc
->receive_disabled
) {
522 if (flags
& QEMU_NET_PACKET_FLAG_RAW
&& vc
->receive_raw
) {
523 len
= vc
->receive_raw(vc
, buf
, size
);
525 len
= vc
->receive(vc
, buf
, size
);
529 vc
->receive_disabled
= 1;
532 ret
= (ret
>= 0) ? ret
: len
;
539 void qemu_purge_queued_packets(VLANClientState
*vc
)
543 if (!vc
->peer
&& !vc
->vlan
) {
548 queue
= vc
->peer
->send_queue
;
550 queue
= vc
->vlan
->send_queue
;
553 qemu_net_queue_purge(queue
, vc
);
556 void qemu_flush_queued_packets(VLANClientState
*vc
)
560 vc
->receive_disabled
= 0;
563 queue
= vc
->vlan
->send_queue
;
565 queue
= vc
->send_queue
;
568 qemu_net_queue_flush(queue
);
571 static ssize_t
qemu_send_packet_async_with_flags(VLANClientState
*sender
,
573 const uint8_t *buf
, int size
,
574 NetPacketSent
*sent_cb
)
579 printf("qemu_send_packet_async:\n");
580 hex_dump(stdout
, buf
, size
);
583 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
588 queue
= sender
->peer
->send_queue
;
590 queue
= sender
->vlan
->send_queue
;
593 return qemu_net_queue_send(queue
, sender
, flags
, buf
, size
, sent_cb
);
596 ssize_t
qemu_send_packet_async(VLANClientState
*sender
,
597 const uint8_t *buf
, int size
,
598 NetPacketSent
*sent_cb
)
600 return qemu_send_packet_async_with_flags(sender
, QEMU_NET_PACKET_FLAG_NONE
,
604 void qemu_send_packet(VLANClientState
*vc
, const uint8_t *buf
, int size
)
606 qemu_send_packet_async(vc
, buf
, size
, NULL
);
609 ssize_t
qemu_send_packet_raw(VLANClientState
*vc
, const uint8_t *buf
, int size
)
611 return qemu_send_packet_async_with_flags(vc
, QEMU_NET_PACKET_FLAG_RAW
,
615 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
618 uint8_t buffer
[4096];
622 for (i
= 0; i
< iovcnt
; i
++) {
625 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
626 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
630 return vc
->receive(vc
, buffer
, offset
);
633 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
638 for (i
= 0; i
< iovcnt
; i
++)
639 offset
+= iov
[i
].iov_len
;
643 static ssize_t
qemu_deliver_packet_iov(VLANClientState
*sender
,
645 const struct iovec
*iov
,
649 VLANClientState
*vc
= opaque
;
652 return calc_iov_length(iov
, iovcnt
);
655 if (vc
->receive_iov
) {
656 return vc
->receive_iov(vc
, iov
, iovcnt
);
658 return vc_sendv_compat(vc
, iov
, iovcnt
);
662 static ssize_t
qemu_vlan_deliver_packet_iov(VLANClientState
*sender
,
664 const struct iovec
*iov
,
668 VLANState
*vlan
= opaque
;
672 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
680 ret
= calc_iov_length(iov
, iovcnt
);
684 assert(!(flags
& QEMU_NET_PACKET_FLAG_RAW
));
686 if (vc
->receive_iov
) {
687 len
= vc
->receive_iov(vc
, iov
, iovcnt
);
689 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
692 ret
= (ret
>= 0) ? ret
: len
;
698 ssize_t
qemu_sendv_packet_async(VLANClientState
*sender
,
699 const struct iovec
*iov
, int iovcnt
,
700 NetPacketSent
*sent_cb
)
704 if (sender
->link_down
|| (!sender
->peer
&& !sender
->vlan
)) {
705 return calc_iov_length(iov
, iovcnt
);
709 queue
= sender
->peer
->send_queue
;
711 queue
= sender
->vlan
->send_queue
;
714 return qemu_net_queue_send_iov(queue
, sender
,
715 QEMU_NET_PACKET_FLAG_NONE
,
716 iov
, iovcnt
, sent_cb
);
720 qemu_sendv_packet(VLANClientState
*vc
, const struct iovec
*iov
, int iovcnt
)
722 return qemu_sendv_packet_async(vc
, iov
, iovcnt
, NULL
);
725 #if defined(CONFIG_SLIRP)
727 /* slirp network adapter */
729 #define SLIRP_CFG_HOSTFWD 1
730 #define SLIRP_CFG_LEGACY 2
732 struct slirp_config_str
{
733 struct slirp_config_str
*next
;
739 typedef struct SlirpState
{
740 QTAILQ_ENTRY(SlirpState
) entry
;
748 static struct slirp_config_str
*slirp_configs
;
749 const char *legacy_tftp_prefix
;
750 const char *legacy_bootp_filename
;
751 static QTAILQ_HEAD(slirp_stacks
, SlirpState
) slirp_stacks
=
752 QTAILQ_HEAD_INITIALIZER(slirp_stacks
);
754 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
,
756 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
,
760 static const char *legacy_smb_export
;
762 static int slirp_smb(SlirpState
*s
, const char *exported_dir
,
763 struct in_addr vserver_addr
);
764 static void slirp_smb_cleanup(SlirpState
*s
);
766 static inline void slirp_smb_cleanup(SlirpState
*s
) { }
769 int slirp_can_output(void *opaque
)
771 SlirpState
*s
= opaque
;
773 return qemu_can_send_packet(s
->vc
);
776 void slirp_output(void *opaque
, const uint8_t *pkt
, int pkt_len
)
778 SlirpState
*s
= opaque
;
781 printf("slirp output:\n");
782 hex_dump(stdout
, pkt
, pkt_len
);
784 qemu_send_packet(s
->vc
, pkt
, pkt_len
);
787 static ssize_t
slirp_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
789 SlirpState
*s
= vc
->opaque
;
792 printf("slirp input:\n");
793 hex_dump(stdout
, buf
, size
);
795 slirp_input(s
->slirp
, buf
, size
);
799 static void net_slirp_cleanup(VLANClientState
*vc
)
801 SlirpState
*s
= vc
->opaque
;
803 slirp_cleanup(s
->slirp
);
804 slirp_smb_cleanup(s
);
805 QTAILQ_REMOVE(&slirp_stacks
, s
, entry
);
809 static int net_slirp_init(VLANState
*vlan
, const char *model
,
810 const char *name
, int restricted
,
811 const char *vnetwork
, const char *vhost
,
812 const char *vhostname
, const char *tftp_export
,
813 const char *bootfile
, const char *vdhcp_start
,
814 const char *vnameserver
, const char *smb_export
,
815 const char *vsmbserver
)
817 /* default settings according to historic slirp */
818 struct in_addr net
= { .s_addr
= htonl(0x0a000200) }; /* 10.0.2.0 */
819 struct in_addr mask
= { .s_addr
= htonl(0xffffff00) }; /* 255.255.255.0 */
820 struct in_addr host
= { .s_addr
= htonl(0x0a000202) }; /* 10.0.2.2 */
821 struct in_addr dhcp
= { .s_addr
= htonl(0x0a00020f) }; /* 10.0.2.15 */
822 struct in_addr dns
= { .s_addr
= htonl(0x0a000203) }; /* 10.0.2.3 */
824 struct in_addr smbsrv
= { .s_addr
= 0 };
831 struct slirp_config_str
*config
;
834 tftp_export
= legacy_tftp_prefix
;
837 bootfile
= legacy_bootp_filename
;
841 if (get_str_sep(buf
, sizeof(buf
), &vnetwork
, '/') < 0) {
842 if (!inet_aton(vnetwork
, &net
)) {
845 addr
= ntohl(net
.s_addr
);
846 if (!(addr
& 0x80000000)) {
847 mask
.s_addr
= htonl(0xff000000); /* class A */
848 } else if ((addr
& 0xfff00000) == 0xac100000) {
849 mask
.s_addr
= htonl(0xfff00000); /* priv. 172.16.0.0/12 */
850 } else if ((addr
& 0xc0000000) == 0x80000000) {
851 mask
.s_addr
= htonl(0xffff0000); /* class B */
852 } else if ((addr
& 0xffff0000) == 0xc0a80000) {
853 mask
.s_addr
= htonl(0xffff0000); /* priv. 192.168.0.0/16 */
854 } else if ((addr
& 0xffff0000) == 0xc6120000) {
855 mask
.s_addr
= htonl(0xfffe0000); /* tests 198.18.0.0/15 */
856 } else if ((addr
& 0xe0000000) == 0xe0000000) {
857 mask
.s_addr
= htonl(0xffffff00); /* class C */
859 mask
.s_addr
= htonl(0xfffffff0); /* multicast/reserved */
862 if (!inet_aton(buf
, &net
)) {
865 shift
= strtol(vnetwork
, &end
, 10);
867 if (!inet_aton(vnetwork
, &mask
)) {
870 } else if (shift
< 4 || shift
> 32) {
873 mask
.s_addr
= htonl(0xffffffff << (32 - shift
));
876 net
.s_addr
&= mask
.s_addr
;
877 host
.s_addr
= net
.s_addr
| (htonl(0x0202) & ~mask
.s_addr
);
878 dhcp
.s_addr
= net
.s_addr
| (htonl(0x020f) & ~mask
.s_addr
);
879 dns
.s_addr
= net
.s_addr
| (htonl(0x0203) & ~mask
.s_addr
);
882 if (vhost
&& !inet_aton(vhost
, &host
)) {
885 if ((host
.s_addr
& mask
.s_addr
) != net
.s_addr
) {
889 if (vdhcp_start
&& !inet_aton(vdhcp_start
, &dhcp
)) {
892 if ((dhcp
.s_addr
& mask
.s_addr
) != net
.s_addr
||
893 dhcp
.s_addr
== host
.s_addr
|| dhcp
.s_addr
== dns
.s_addr
) {
897 if (vnameserver
&& !inet_aton(vnameserver
, &dns
)) {
900 if ((dns
.s_addr
& mask
.s_addr
) != net
.s_addr
||
901 dns
.s_addr
== host
.s_addr
) {
906 if (vsmbserver
&& !inet_aton(vsmbserver
, &smbsrv
)) {
911 s
= qemu_mallocz(sizeof(SlirpState
));
912 s
->slirp
= slirp_init(restricted
, net
, mask
, host
, vhostname
,
913 tftp_export
, bootfile
, dhcp
, dns
, s
);
914 QTAILQ_INSERT_TAIL(&slirp_stacks
, s
, entry
);
916 for (config
= slirp_configs
; config
; config
= config
->next
) {
917 if (config
->flags
& SLIRP_CFG_HOSTFWD
) {
918 if (slirp_hostfwd(s
, config
->str
,
919 config
->flags
& SLIRP_CFG_LEGACY
) < 0)
922 if (slirp_guestfwd(s
, config
->str
,
923 config
->flags
& SLIRP_CFG_LEGACY
) < 0)
929 smb_export
= legacy_smb_export
;
932 if (slirp_smb(s
, smb_export
, smbsrv
) < 0)
937 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_SLIRP
,
938 vlan
, NULL
, model
, name
, NULL
,
939 slirp_receive
, NULL
, NULL
,
940 net_slirp_cleanup
, s
);
941 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
942 "net=%s, restricted=%c", inet_ntoa(net
), restricted
? 'y' : 'n');
946 static SlirpState
*slirp_lookup(Monitor
*mon
, const char *vlan
,
952 vc
= qemu_find_vlan_client_by_name(mon
, strtol(vlan
, NULL
, 0), stack
);
956 if (strcmp(vc
->model
, "user")) {
957 monitor_printf(mon
, "invalid device specified\n");
962 if (QTAILQ_EMPTY(&slirp_stacks
)) {
963 monitor_printf(mon
, "user mode network stack not in use\n");
966 return QTAILQ_FIRST(&slirp_stacks
);
970 void net_slirp_hostfwd_remove(Monitor
*mon
, const QDict
*qdict
)
972 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
975 const char *src_str
, *p
;
979 const char *arg1
= qdict_get_str(qdict
, "arg1");
980 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
981 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
984 s
= slirp_lookup(mon
, arg1
, arg2
);
987 s
= slirp_lookup(mon
, NULL
, NULL
);
994 if (!src_str
|| !src_str
[0])
998 get_str_sep(buf
, sizeof(buf
), &p
, ':');
1000 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
1002 } else if (!strcmp(buf
, "udp")) {
1008 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1011 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
1015 host_port
= atoi(p
);
1017 err
= slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks
)->slirp
, is_udp
,
1018 host_addr
, host_port
);
1020 monitor_printf(mon
, "host forwarding rule for %s %s\n", src_str
,
1021 err
? "removed" : "not found");
1025 monitor_printf(mon
, "invalid format\n");
1028 static int slirp_hostfwd(SlirpState
*s
, const char *redir_str
,
1031 struct in_addr host_addr
= { .s_addr
= INADDR_ANY
};
1032 struct in_addr guest_addr
= { .s_addr
= 0 };
1033 int host_port
, guest_port
;
1040 if (!p
|| get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1043 if (!strcmp(buf
, "tcp") || buf
[0] == '\0') {
1045 } else if (!strcmp(buf
, "udp")) {
1051 if (!legacy_format
) {
1052 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1055 if (buf
[0] != '\0' && !inet_aton(buf
, &host_addr
)) {
1060 if (get_str_sep(buf
, sizeof(buf
), &p
, legacy_format
? ':' : '-') < 0) {
1063 host_port
= strtol(buf
, &end
, 0);
1064 if (*end
!= '\0' || host_port
< 1 || host_port
> 65535) {
1068 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1071 if (buf
[0] != '\0' && !inet_aton(buf
, &guest_addr
)) {
1075 guest_port
= strtol(p
, &end
, 0);
1076 if (*end
!= '\0' || guest_port
< 1 || guest_port
> 65535) {
1080 if (slirp_add_hostfwd(s
->slirp
, is_udp
, host_addr
, host_port
, guest_addr
,
1082 qemu_error("could not set up host forwarding rule '%s'\n",
1089 qemu_error("invalid host forwarding rule '%s'\n", redir_str
);
1093 void net_slirp_hostfwd_add(Monitor
*mon
, const QDict
*qdict
)
1095 const char *redir_str
;
1097 const char *arg1
= qdict_get_str(qdict
, "arg1");
1098 const char *arg2
= qdict_get_try_str(qdict
, "arg2");
1099 const char *arg3
= qdict_get_try_str(qdict
, "arg3");
1102 s
= slirp_lookup(mon
, arg1
, arg2
);
1105 s
= slirp_lookup(mon
, NULL
, NULL
);
1109 slirp_hostfwd(s
, redir_str
, 0);
1114 int net_slirp_redir(const char *redir_str
)
1116 struct slirp_config_str
*config
;
1118 if (QTAILQ_EMPTY(&slirp_stacks
)) {
1119 config
= qemu_malloc(sizeof(*config
));
1120 pstrcpy(config
->str
, sizeof(config
->str
), redir_str
);
1121 config
->flags
= SLIRP_CFG_HOSTFWD
| SLIRP_CFG_LEGACY
;
1122 config
->next
= slirp_configs
;
1123 slirp_configs
= config
;
1127 return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks
), redir_str
, 1);
1132 /* automatic user mode samba server configuration */
1133 static void slirp_smb_cleanup(SlirpState
*s
)
1137 if (s
->smb_dir
[0] != '\0') {
1138 snprintf(cmd
, sizeof(cmd
), "rm -rf %s", s
->smb_dir
);
1140 s
->smb_dir
[0] = '\0';
1144 static int slirp_smb(SlirpState
* s
, const char *exported_dir
,
1145 struct in_addr vserver_addr
)
1147 static int instance
;
1149 char smb_cmdline
[128];
1152 snprintf(s
->smb_dir
, sizeof(s
->smb_dir
), "/tmp/qemu-smb.%ld-%d",
1153 (long)getpid(), instance
++);
1154 if (mkdir(s
->smb_dir
, 0700) < 0) {
1155 qemu_error("could not create samba server dir '%s'\n", s
->smb_dir
);
1158 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", s
->smb_dir
, "smb.conf");
1160 f
= fopen(smb_conf
, "w");
1162 slirp_smb_cleanup(s
);
1163 qemu_error("could not create samba server configuration file '%s'\n",
1171 "socket address=127.0.0.1\n"
1172 "pid directory=%s\n"
1173 "lock directory=%s\n"
1174 "log file=%s/log.smbd\n"
1175 "smb passwd file=%s/smbpasswd\n"
1176 "security = share\n"
1190 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
1191 SMBD_COMMAND
, smb_conf
);
1193 if (slirp_add_exec(s
->slirp
, 0, smb_cmdline
, &vserver_addr
, 139) < 0) {
1194 slirp_smb_cleanup(s
);
1195 qemu_error("conflicting/invalid smbserver address\n");
1201 /* automatic user mode samba server configuration (legacy interface) */
1202 int net_slirp_smb(const char *exported_dir
)
1204 struct in_addr vserver_addr
= { .s_addr
= 0 };
1206 if (legacy_smb_export
) {
1207 fprintf(stderr
, "-smb given twice\n");
1210 legacy_smb_export
= exported_dir
;
1211 if (!QTAILQ_EMPTY(&slirp_stacks
)) {
1212 return slirp_smb(QTAILQ_FIRST(&slirp_stacks
), exported_dir
,
1218 #endif /* !defined(_WIN32) */
1221 CharDriverState
*hd
;
1222 struct in_addr server
;
1227 static int guestfwd_can_read(void *opaque
)
1229 struct GuestFwd
*fwd
= opaque
;
1230 return slirp_socket_can_recv(fwd
->slirp
, fwd
->server
, fwd
->port
);
1233 static void guestfwd_read(void *opaque
, const uint8_t *buf
, int size
)
1235 struct GuestFwd
*fwd
= opaque
;
1236 slirp_socket_recv(fwd
->slirp
, fwd
->server
, fwd
->port
, buf
, size
);
1239 static int slirp_guestfwd(SlirpState
*s
, const char *config_str
,
1242 struct in_addr server
= { .s_addr
= 0 };
1243 struct GuestFwd
*fwd
;
1250 if (legacy_format
) {
1251 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1255 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1258 if (strcmp(buf
, "tcp") && buf
[0] != '\0') {
1261 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0) {
1264 if (buf
[0] != '\0' && !inet_aton(buf
, &server
)) {
1267 if (get_str_sep(buf
, sizeof(buf
), &p
, '-') < 0) {
1271 port
= strtol(buf
, &end
, 10);
1272 if (*end
!= '\0' || port
< 1 || port
> 65535) {
1276 fwd
= qemu_malloc(sizeof(struct GuestFwd
));
1277 snprintf(buf
, sizeof(buf
), "guestfwd.tcp:%d", port
);
1278 fwd
->hd
= qemu_chr_open(buf
, p
, NULL
);
1280 qemu_error("could not open guest forwarding device '%s'\n", buf
);
1285 if (slirp_add_exec(s
->slirp
, 3, fwd
->hd
, &server
, port
) < 0) {
1286 qemu_error("conflicting/invalid host:port in guest forwarding "
1287 "rule '%s'\n", config_str
);
1291 fwd
->server
= server
;
1293 fwd
->slirp
= s
->slirp
;
1295 qemu_chr_add_handlers(fwd
->hd
, guestfwd_can_read
, guestfwd_read
,
1300 qemu_error("invalid guest forwarding rule '%s'\n", config_str
);
1304 void do_info_usernet(Monitor
*mon
)
1308 QTAILQ_FOREACH(s
, &slirp_stacks
, entry
) {
1309 monitor_printf(mon
, "VLAN %d (%s):\n", s
->vc
->vlan
->id
, s
->vc
->name
);
1310 slirp_connection_info(s
->slirp
, mon
);
1314 #endif /* CONFIG_SLIRP */
1316 #if defined(CONFIG_VDE)
1317 typedef struct VDEState
{
1318 VLANClientState
*vc
;
1322 static void vde_to_qemu(void *opaque
)
1324 VDEState
*s
= opaque
;
1328 size
= vde_recv(s
->vde
, (char *)buf
, sizeof(buf
), 0);
1330 qemu_send_packet(s
->vc
, buf
, size
);
1334 static ssize_t
vde_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1336 VDEState
*s
= vc
->opaque
;
1340 ret
= vde_send(s
->vde
, (const char *)buf
, size
, 0);
1341 } while (ret
< 0 && errno
== EINTR
);
1346 static void vde_cleanup(VLANClientState
*vc
)
1348 VDEState
*s
= vc
->opaque
;
1349 qemu_set_fd_handler(vde_datafd(s
->vde
), NULL
, NULL
, NULL
);
1354 static int net_vde_init(VLANState
*vlan
, const char *model
,
1355 const char *name
, const char *sock
,
1356 int port
, const char *group
, int mode
)
1359 char *init_group
= (char *)group
;
1360 char *init_sock
= (char *)sock
;
1362 struct vde_open_args args
= {
1364 .group
= init_group
,
1368 s
= qemu_mallocz(sizeof(VDEState
));
1369 s
->vde
= vde_open(init_sock
, (char *)"QEMU", &args
);
1374 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_VDE
,
1375 vlan
, NULL
, model
, name
, NULL
,
1376 vde_receive
, NULL
, NULL
,
1378 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1379 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1380 sock
, vde_datafd(s
->vde
));
1385 /* network connection */
1386 typedef struct NetSocketState
{
1387 VLANClientState
*vc
;
1389 int state
; /* 0 = getting length, 1 = getting data */
1391 unsigned int packet_len
;
1393 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1396 typedef struct NetSocketListenState
{
1401 } NetSocketListenState
;
1403 /* XXX: we consider we can send the whole packet without blocking */
1404 static ssize_t
net_socket_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1406 NetSocketState
*s
= vc
->opaque
;
1410 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1411 return send_all(s
->fd
, buf
, size
);
1414 static ssize_t
net_socket_receive_dgram(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1416 NetSocketState
*s
= vc
->opaque
;
1418 return sendto(s
->fd
, (const void *)buf
, size
, 0,
1419 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1422 static void net_socket_send(void *opaque
)
1424 NetSocketState
*s
= opaque
;
1430 size
= recv(s
->fd
, (void *)buf1
, sizeof(buf1
), 0);
1432 err
= socket_error();
1433 if (err
!= EWOULDBLOCK
)
1435 } else if (size
== 0) {
1436 /* end of connection */
1438 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1444 /* reassemble a packet from the network */
1450 memcpy(s
->buf
+ s
->index
, buf
, l
);
1454 if (s
->index
== 4) {
1456 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1462 l
= s
->packet_len
- s
->index
;
1465 if (s
->index
+ l
<= sizeof(s
->buf
)) {
1466 memcpy(s
->buf
+ s
->index
, buf
, l
);
1468 fprintf(stderr
, "serious error: oversized packet received,"
1469 "connection terminated.\n");
1477 if (s
->index
>= s
->packet_len
) {
1478 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1487 static void net_socket_send_dgram(void *opaque
)
1489 NetSocketState
*s
= opaque
;
1492 size
= recv(s
->fd
, (void *)s
->buf
, sizeof(s
->buf
), 0);
1496 /* end of connection */
1497 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1500 qemu_send_packet(s
->vc
, s
->buf
, size
);
1503 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1508 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1509 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1510 inet_ntoa(mcastaddr
->sin_addr
),
1511 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1515 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1517 perror("socket(PF_INET, SOCK_DGRAM)");
1522 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1523 (const char *)&val
, sizeof(val
));
1525 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1529 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1535 /* Add host to multicast group */
1536 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1537 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1539 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1540 (const char *)&imr
, sizeof(struct ip_mreq
));
1542 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1546 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1548 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1549 (const char *)&val
, sizeof(val
));
1551 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1555 socket_set_nonblock(fd
);
1563 static void net_socket_cleanup(VLANClientState
*vc
)
1565 NetSocketState
*s
= vc
->opaque
;
1566 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1571 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1574 int fd
, int is_connected
)
1576 struct sockaddr_in saddr
;
1578 socklen_t saddr_len
;
1581 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1582 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1583 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1587 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1589 if (saddr
.sin_addr
.s_addr
==0) {
1590 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1594 /* clone dgram socket */
1595 newfd
= net_socket_mcast_create(&saddr
);
1597 /* error already reported by net_socket_mcast_create() */
1601 /* clone newfd to fd, close newfd */
1606 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1607 fd
, strerror(errno
));
1612 s
= qemu_mallocz(sizeof(NetSocketState
));
1615 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET
,
1616 vlan
, NULL
, model
, name
, NULL
,
1617 net_socket_receive_dgram
, NULL
, NULL
,
1618 net_socket_cleanup
, s
);
1619 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1621 /* mcast: save bound address as dst */
1622 if (is_connected
) s
->dgram_dst
=saddr
;
1624 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1625 "socket: fd=%d (%s mcast=%s:%d)",
1626 fd
, is_connected
? "cloned" : "",
1627 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1631 static void net_socket_connect(void *opaque
)
1633 NetSocketState
*s
= opaque
;
1634 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1637 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1640 int fd
, int is_connected
)
1643 s
= qemu_mallocz(sizeof(NetSocketState
));
1645 s
->vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET
,
1646 vlan
, NULL
, model
, name
, NULL
,
1647 net_socket_receive
, NULL
, NULL
,
1648 net_socket_cleanup
, s
);
1649 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1650 "socket: fd=%d", fd
);
1652 net_socket_connect(s
);
1654 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1659 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1660 const char *model
, const char *name
,
1661 int fd
, int is_connected
)
1663 int so_type
= -1, optlen
=sizeof(so_type
);
1665 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1666 (socklen_t
*)&optlen
)< 0) {
1667 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1672 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1674 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1676 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1677 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1678 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1683 static void net_socket_accept(void *opaque
)
1685 NetSocketListenState
*s
= opaque
;
1687 struct sockaddr_in saddr
;
1692 len
= sizeof(saddr
);
1693 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1694 if (fd
< 0 && errno
!= EINTR
) {
1696 } else if (fd
>= 0) {
1700 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1704 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1705 "socket: connection from %s:%d",
1706 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1710 static int net_socket_listen_init(VLANState
*vlan
,
1713 const char *host_str
)
1715 NetSocketListenState
*s
;
1717 struct sockaddr_in saddr
;
1719 if (parse_host_port(&saddr
, host_str
) < 0)
1722 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1724 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1729 socket_set_nonblock(fd
);
1731 /* allow fast reuse */
1733 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1735 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1740 ret
= listen(fd
, 0);
1746 s
->model
= qemu_strdup(model
);
1747 s
->name
= name
? qemu_strdup(name
) : NULL
;
1749 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1753 static int net_socket_connect_init(VLANState
*vlan
,
1756 const char *host_str
)
1759 int fd
, connected
, ret
, err
;
1760 struct sockaddr_in saddr
;
1762 if (parse_host_port(&saddr
, host_str
) < 0)
1765 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1770 socket_set_nonblock(fd
);
1774 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1776 err
= socket_error();
1777 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1778 } else if (err
== EINPROGRESS
) {
1781 } else if (err
== WSAEALREADY
) {
1794 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1797 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1798 "socket: connect to %s:%d",
1799 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1803 static int net_socket_mcast_init(VLANState
*vlan
,
1806 const char *host_str
)
1810 struct sockaddr_in saddr
;
1812 if (parse_host_port(&saddr
, host_str
) < 0)
1816 fd
= net_socket_mcast_create(&saddr
);
1820 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1824 s
->dgram_dst
= saddr
;
1826 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1827 "socket: mcast=%s:%d",
1828 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1833 typedef struct DumpState
{
1834 VLANClientState
*pcap_vc
;
1839 #define PCAP_MAGIC 0xa1b2c3d4
1841 struct pcap_file_hdr
{
1843 uint16_t version_major
;
1844 uint16_t version_minor
;
1851 struct pcap_sf_pkthdr
{
1860 static ssize_t
dump_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
1862 DumpState
*s
= vc
->opaque
;
1863 struct pcap_sf_pkthdr hdr
;
1867 /* Early return in case of previous error. */
1872 ts
= muldiv64(qemu_get_clock(vm_clock
), 1000000, get_ticks_per_sec());
1873 caplen
= size
> s
->pcap_caplen
? s
->pcap_caplen
: size
;
1875 hdr
.ts
.tv_sec
= ts
/ 1000000;
1876 hdr
.ts
.tv_usec
= ts
% 1000000;
1877 hdr
.caplen
= caplen
;
1879 if (write(s
->fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
) ||
1880 write(s
->fd
, buf
, caplen
) != caplen
) {
1881 qemu_log("-net dump write error - stop dump\n");
1889 static void net_dump_cleanup(VLANClientState
*vc
)
1891 DumpState
*s
= vc
->opaque
;
1897 static int net_dump_init(VLANState
*vlan
, const char *device
,
1898 const char *name
, const char *filename
, int len
)
1900 struct pcap_file_hdr hdr
;
1903 s
= qemu_malloc(sizeof(DumpState
));
1905 s
->fd
= open(filename
, O_CREAT
| O_WRONLY
| O_BINARY
, 0644);
1907 qemu_error("-net dump: can't open %s\n", filename
);
1911 s
->pcap_caplen
= len
;
1913 hdr
.magic
= PCAP_MAGIC
;
1914 hdr
.version_major
= 2;
1915 hdr
.version_minor
= 4;
1918 hdr
.snaplen
= s
->pcap_caplen
;
1921 if (write(s
->fd
, &hdr
, sizeof(hdr
)) < sizeof(hdr
)) {
1922 qemu_error("-net dump write error: %s\n", strerror(errno
));
1928 s
->pcap_vc
= qemu_new_vlan_client(NET_CLIENT_TYPE_DUMP
,
1929 vlan
, NULL
, device
, name
, NULL
,
1930 dump_receive
, NULL
, NULL
,
1931 net_dump_cleanup
, s
);
1932 snprintf(s
->pcap_vc
->info_str
, sizeof(s
->pcap_vc
->info_str
),
1933 "dump to %s (len=%d)", filename
, len
);
1937 /* find or alloc a new VLAN */
1938 VLANState
*qemu_find_vlan(int id
, int allocate
)
1942 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
1943 if (vlan
->id
== id
) {
1952 vlan
= qemu_mallocz(sizeof(VLANState
));
1954 QTAILQ_INIT(&vlan
->clients
);
1956 vlan
->send_queue
= qemu_new_net_queue(qemu_vlan_deliver_packet
,
1957 qemu_vlan_deliver_packet_iov
,
1960 QTAILQ_INSERT_TAIL(&vlans
, vlan
, next
);
1965 VLANClientState
*qemu_find_netdev(const char *id
)
1967 VLANClientState
*vc
;
1969 QTAILQ_FOREACH(vc
, &non_vlan_clients
, next
) {
1970 if (!strcmp(vc
->name
, id
)) {
1978 static int nic_get_free_idx(void)
1982 for (index
= 0; index
< MAX_NICS
; index
++)
1983 if (!nd_table
[index
].used
)
1988 int qemu_show_nic_models(const char *arg
, const char *const *models
)
1992 if (!arg
|| strcmp(arg
, "?"))
1995 fprintf(stderr
, "qemu: Supported NIC models: ");
1996 for (i
= 0 ; models
[i
]; i
++)
1997 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
2001 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
2003 const char *models
[2];
2008 if (qemu_show_nic_models(nd
->model
, models
))
2010 if (qemu_find_nic_model(nd
, models
, model
) < 0)
2014 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
2015 const char *default_model
)
2020 nd
->model
= qemu_strdup(default_model
);
2022 for (i
= 0 ; models
[i
]; i
++) {
2023 if (strcmp(nd
->model
, models
[i
]) == 0)
2027 qemu_error("qemu: Unsupported NIC model: %s\n", nd
->model
);
2031 int net_handle_fd_param(Monitor
*mon
, const char *param
)
2033 if (!qemu_isdigit(param
[0])) {
2036 fd
= monitor_get_fd(mon
, param
);
2038 qemu_error("No file descriptor named %s found", param
);
2044 return strtol(param
, NULL
, 0);
2048 static int net_init_nic(QemuOpts
*opts
,
2057 idx
= nic_get_free_idx();
2058 if (idx
== -1 || nb_nics
>= MAX_NICS
) {
2059 qemu_error("Too Many NICs\n");
2063 nd
= &nd_table
[idx
];
2065 memset(nd
, 0, sizeof(*nd
));
2067 if ((netdev
= qemu_opt_get(opts
, "netdev"))) {
2068 nd
->netdev
= qemu_find_netdev(netdev
);
2070 qemu_error("netdev '%s' not found\n", netdev
);
2078 nd
->name
= qemu_strdup(name
);
2080 if (qemu_opt_get(opts
, "model")) {
2081 nd
->model
= qemu_strdup(qemu_opt_get(opts
, "model"));
2083 if (qemu_opt_get(opts
, "addr")) {
2084 nd
->devaddr
= qemu_strdup(qemu_opt_get(opts
, "addr"));
2087 nd
->macaddr
[0] = 0x52;
2088 nd
->macaddr
[1] = 0x54;
2089 nd
->macaddr
[2] = 0x00;
2090 nd
->macaddr
[3] = 0x12;
2091 nd
->macaddr
[4] = 0x34;
2092 nd
->macaddr
[5] = 0x56 + idx
;
2094 if (qemu_opt_get(opts
, "macaddr") &&
2095 parse_macaddr(nd
->macaddr
, qemu_opt_get(opts
, "macaddr")) < 0) {
2096 qemu_error("invalid syntax for ethernet address\n");
2100 nd
->nvectors
= qemu_opt_get_number(opts
, "vectors", NIC_NVECTORS_UNSPECIFIED
);
2101 if (nd
->nvectors
!= NIC_NVECTORS_UNSPECIFIED
&&
2102 (nd
->nvectors
< 0 || nd
->nvectors
> 0x7ffffff)) {
2103 qemu_error("invalid # of vectors: %d\n", nd
->nvectors
);
2109 nd
->vlan
->nb_guest_devs
++;
2116 #if defined(CONFIG_SLIRP)
2117 static int net_init_slirp_configs(const char *name
, const char *value
, void *opaque
)
2119 struct slirp_config_str
*config
;
2121 if (strcmp(name
, "hostfwd") != 0 && strcmp(name
, "guestfwd") != 0) {
2125 config
= qemu_mallocz(sizeof(*config
));
2127 pstrcpy(config
->str
, sizeof(config
->str
), value
);
2129 if (!strcmp(name
, "hostfwd")) {
2130 config
->flags
= SLIRP_CFG_HOSTFWD
;
2133 config
->next
= slirp_configs
;
2134 slirp_configs
= config
;
2139 static int net_init_slirp(QemuOpts
*opts
,
2144 struct slirp_config_str
*config
;
2146 const char *vhostname
;
2147 const char *vdhcp_start
;
2148 const char *vnamesrv
;
2149 const char *tftp_export
;
2150 const char *bootfile
;
2151 const char *smb_export
;
2152 const char *vsmbsrv
;
2157 vhost
= qemu_opt_get(opts
, "host");
2158 vhostname
= qemu_opt_get(opts
, "hostname");
2159 vdhcp_start
= qemu_opt_get(opts
, "dhcpstart");
2160 vnamesrv
= qemu_opt_get(opts
, "dns");
2161 tftp_export
= qemu_opt_get(opts
, "tftp");
2162 bootfile
= qemu_opt_get(opts
, "bootfile");
2163 smb_export
= qemu_opt_get(opts
, "smb");
2164 vsmbsrv
= qemu_opt_get(opts
, "smbserver");
2166 if (qemu_opt_get(opts
, "ip")) {
2167 const char *ip
= qemu_opt_get(opts
, "ip");
2168 int l
= strlen(ip
) + strlen("/24") + 1;
2170 vnet
= qemu_malloc(l
);
2172 /* emulate legacy ip= parameter */
2173 pstrcpy(vnet
, l
, ip
);
2174 pstrcat(vnet
, l
, "/24");
2177 if (qemu_opt_get(opts
, "net")) {
2181 vnet
= qemu_strdup(qemu_opt_get(opts
, "net"));
2184 if (qemu_opt_get(opts
, "restrict") &&
2185 qemu_opt_get(opts
, "restrict")[0] == 'y') {
2189 qemu_opt_foreach(opts
, net_init_slirp_configs
, NULL
, 0);
2191 ret
= net_slirp_init(vlan
, "user", name
, restricted
, vnet
, vhost
,
2192 vhostname
, tftp_export
, bootfile
, vdhcp_start
,
2193 vnamesrv
, smb_export
, vsmbsrv
);
2195 while (slirp_configs
) {
2196 config
= slirp_configs
;
2197 slirp_configs
= config
->next
;
2201 if (ret
!= -1 && vlan
) {
2202 vlan
->nb_host_devs
++;
2209 #endif /* CONFIG_SLIRP */
2211 static int net_init_socket(QemuOpts
*opts
,
2216 if (qemu_opt_get(opts
, "fd")) {
2219 if (qemu_opt_get(opts
, "listen") ||
2220 qemu_opt_get(opts
, "connect") ||
2221 qemu_opt_get(opts
, "mcast")) {
2222 qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
2226 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
2231 if (!net_socket_fd_init(vlan
, "socket", name
, fd
, 1)) {
2235 } else if (qemu_opt_get(opts
, "listen")) {
2238 if (qemu_opt_get(opts
, "fd") ||
2239 qemu_opt_get(opts
, "connect") ||
2240 qemu_opt_get(opts
, "mcast")) {
2241 qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
2245 listen
= qemu_opt_get(opts
, "listen");
2247 if (net_socket_listen_init(vlan
, "socket", name
, listen
) == -1) {
2250 } else if (qemu_opt_get(opts
, "connect")) {
2251 const char *connect
;
2253 if (qemu_opt_get(opts
, "fd") ||
2254 qemu_opt_get(opts
, "listen") ||
2255 qemu_opt_get(opts
, "mcast")) {
2256 qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
2260 connect
= qemu_opt_get(opts
, "connect");
2262 if (net_socket_connect_init(vlan
, "socket", name
, connect
) == -1) {
2265 } else if (qemu_opt_get(opts
, "mcast")) {
2268 if (qemu_opt_get(opts
, "fd") ||
2269 qemu_opt_get(opts
, "connect") ||
2270 qemu_opt_get(opts
, "listen")) {
2271 qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
2275 mcast
= qemu_opt_get(opts
, "mcast");
2277 if (net_socket_mcast_init(vlan
, "socket", name
, mcast
) == -1) {
2281 qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
2286 vlan
->nb_host_devs
++;
2293 static int net_init_vde(QemuOpts
*opts
,
2302 sock
= qemu_opt_get(opts
, "sock");
2303 group
= qemu_opt_get(opts
, "group");
2305 port
= qemu_opt_get_number(opts
, "port", 0);
2306 mode
= qemu_opt_get_number(opts
, "mode", 0700);
2308 if (net_vde_init(vlan
, "vde", name
, sock
, port
, group
, mode
) == -1) {
2313 vlan
->nb_host_devs
++;
2320 static int net_init_dump(QemuOpts
*opts
,
2331 file
= qemu_opt_get(opts
, "file");
2333 snprintf(def_file
, sizeof(def_file
), "qemu-vlan%d.pcap", vlan
->id
);
2337 len
= qemu_opt_get_size(opts
, "len", 65536);
2339 return net_dump_init(vlan
, "dump", name
, file
, len
);
2342 #define NET_COMMON_PARAMS_DESC \
2345 .type = QEMU_OPT_STRING, \
2346 .help = "net client type (nic, tap etc.)", \
2349 .type = QEMU_OPT_NUMBER, \
2350 .help = "vlan number", \
2353 .type = QEMU_OPT_STRING, \
2354 .help = "identifier for monitor commands", \
2357 typedef int (*net_client_init_func
)(QemuOpts
*opts
,
2362 /* magic number, but compiler will warn if too small */
2363 #define NET_MAX_DESC 20
2367 net_client_init_func init
;
2368 QemuOptDesc desc
[NET_MAX_DESC
];
2369 } net_client_types
[] = {
2373 NET_COMMON_PARAMS_DESC
,
2374 { /* end of list */ }
2378 .init
= net_init_nic
,
2380 NET_COMMON_PARAMS_DESC
,
2383 .type
= QEMU_OPT_STRING
,
2384 .help
= "id of -netdev to connect to",
2388 .type
= QEMU_OPT_STRING
,
2389 .help
= "MAC address",
2392 .type
= QEMU_OPT_STRING
,
2393 .help
= "device model (e1000, rtl8139, virtio etc.)",
2396 .type
= QEMU_OPT_STRING
,
2397 .help
= "PCI device address",
2400 .type
= QEMU_OPT_NUMBER
,
2401 .help
= "number of MSI-x vectors, 0 to disable MSI-X",
2403 { /* end of list */ }
2408 .init
= net_init_slirp
,
2410 NET_COMMON_PARAMS_DESC
,
2413 .type
= QEMU_OPT_STRING
,
2414 .help
= "client hostname reported by the builtin DHCP server",
2417 .type
= QEMU_OPT_STRING
,
2418 .help
= "isolate the guest from the host (y|yes|n|no)",
2421 .type
= QEMU_OPT_STRING
,
2422 .help
= "legacy parameter, use net= instead",
2425 .type
= QEMU_OPT_STRING
,
2426 .help
= "IP address and optional netmask",
2429 .type
= QEMU_OPT_STRING
,
2430 .help
= "guest-visible address of the host",
2433 .type
= QEMU_OPT_STRING
,
2434 .help
= "root directory of the built-in TFTP server",
2437 .type
= QEMU_OPT_STRING
,
2438 .help
= "BOOTP filename, for use with tftp=",
2440 .name
= "dhcpstart",
2441 .type
= QEMU_OPT_STRING
,
2442 .help
= "the first of the 16 IPs the built-in DHCP server can assign",
2445 .type
= QEMU_OPT_STRING
,
2446 .help
= "guest-visible address of the virtual nameserver",
2449 .type
= QEMU_OPT_STRING
,
2450 .help
= "root directory of the built-in SMB server",
2452 .name
= "smbserver",
2453 .type
= QEMU_OPT_STRING
,
2454 .help
= "IP address of the built-in SMB server",
2457 .type
= QEMU_OPT_STRING
,
2458 .help
= "guest port number to forward incoming TCP or UDP connections",
2461 .type
= QEMU_OPT_STRING
,
2462 .help
= "IP address and port to forward guest TCP connections",
2464 { /* end of list */ }
2469 .init
= net_init_tap
,
2471 NET_COMMON_PARAMS_DESC
,
2474 .type
= QEMU_OPT_STRING
,
2475 .help
= "interface name",
2480 .type
= QEMU_OPT_STRING
,
2481 .help
= "file descriptor of an already opened tap",
2484 .type
= QEMU_OPT_STRING
,
2485 .help
= "script to initialize the interface",
2487 .name
= "downscript",
2488 .type
= QEMU_OPT_STRING
,
2489 .help
= "script to shut down the interface",
2492 .type
= QEMU_OPT_SIZE
,
2493 .help
= "send buffer limit"
2496 .type
= QEMU_OPT_BOOL
,
2497 .help
= "enable the IFF_VNET_HDR flag on the tap interface"
2500 { /* end of list */ }
2504 .init
= net_init_socket
,
2506 NET_COMMON_PARAMS_DESC
,
2509 .type
= QEMU_OPT_STRING
,
2510 .help
= "file descriptor of an already opened socket",
2513 .type
= QEMU_OPT_STRING
,
2514 .help
= "port number, and optional hostname, to listen on",
2517 .type
= QEMU_OPT_STRING
,
2518 .help
= "port number, and optional hostname, to connect to",
2521 .type
= QEMU_OPT_STRING
,
2522 .help
= "UDP multicast address and port number",
2524 { /* end of list */ }
2529 .init
= net_init_vde
,
2531 NET_COMMON_PARAMS_DESC
,
2534 .type
= QEMU_OPT_STRING
,
2535 .help
= "socket path",
2538 .type
= QEMU_OPT_NUMBER
,
2539 .help
= "port number",
2542 .type
= QEMU_OPT_STRING
,
2543 .help
= "group owner of socket",
2546 .type
= QEMU_OPT_NUMBER
,
2547 .help
= "permissions for socket",
2549 { /* end of list */ }
2554 .init
= net_init_dump
,
2556 NET_COMMON_PARAMS_DESC
,
2559 .type
= QEMU_OPT_SIZE
,
2560 .help
= "per-packet size limit (64k default)",
2563 .type
= QEMU_OPT_STRING
,
2564 .help
= "dump file path (default is qemu-vlan0.pcap)",
2566 { /* end of list */ }
2569 { /* end of list */ }
2572 int net_client_init(Monitor
*mon
, QemuOpts
*opts
, int is_netdev
)
2578 type
= qemu_opt_get(opts
, "type");
2580 qemu_error("No type specified for -net\n");
2585 if (strcmp(type
, "tap") != 0 &&
2587 strcmp(type
, "user") != 0 &&
2590 strcmp(type
, "vde") != 0 &&
2592 strcmp(type
, "socket") != 0) {
2593 qemu_error("The '%s' network backend type is not valid with -netdev\n",
2598 if (qemu_opt_get(opts
, "vlan")) {
2599 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
2602 if (qemu_opt_get(opts
, "name")) {
2603 qemu_error("The 'name' parameter is not valid with -netdev\n");
2606 if (!qemu_opts_id(opts
)) {
2607 qemu_error("The id= parameter is required with -netdev\n");
2612 name
= qemu_opts_id(opts
);
2614 name
= qemu_opt_get(opts
, "name");
2617 for (i
= 0; net_client_types
[i
].type
!= NULL
; i
++) {
2618 if (!strcmp(net_client_types
[i
].type
, type
)) {
2619 VLANState
*vlan
= NULL
;
2621 if (qemu_opts_validate(opts
, &net_client_types
[i
].desc
[0]) == -1) {
2625 /* Do not add to a vlan if it's a -netdev or a nic with a
2626 * netdev= parameter. */
2628 (strcmp(type
, "nic") == 0 && qemu_opt_get(opts
, "netdev")))) {
2629 vlan
= qemu_find_vlan(qemu_opt_get_number(opts
, "vlan", 0), 1);
2632 if (net_client_types
[i
].init
) {
2633 return net_client_types
[i
].init(opts
, mon
, name
, vlan
);
2640 qemu_error("Invalid -net type '%s'\n", type
);
2644 void net_client_uninit(NICInfo
*nd
)
2647 nd
->vlan
->nb_guest_devs
--;
2651 qemu_free(nd
->model
);
2652 qemu_free(nd
->name
);
2653 qemu_free(nd
->devaddr
);
2658 static int net_host_check_device(const char *device
)
2661 const char *valid_param_list
[] = { "tap", "socket", "dump"
2669 for (i
= 0; i
< sizeof(valid_param_list
) / sizeof(char *); i
++) {
2670 if (!strncmp(valid_param_list
[i
], device
,
2671 strlen(valid_param_list
[i
])))
2678 void net_host_device_add(Monitor
*mon
, const QDict
*qdict
)
2680 const char *device
= qdict_get_str(qdict
, "device");
2681 const char *opts_str
= qdict_get_try_str(qdict
, "opts");
2684 if (!net_host_check_device(device
)) {
2685 monitor_printf(mon
, "invalid host network device %s\n", device
);
2689 opts
= qemu_opts_parse(&qemu_net_opts
, opts_str
? opts_str
: "", NULL
);
2691 monitor_printf(mon
, "parsing network options '%s' failed\n",
2692 opts_str
? opts_str
: "");
2696 qemu_opt_set(opts
, "type", device
);
2698 if (net_client_init(mon
, opts
, 0) < 0) {
2699 monitor_printf(mon
, "adding host network device %s failed\n", device
);
2703 void net_host_device_remove(Monitor
*mon
, const QDict
*qdict
)
2705 VLANClientState
*vc
;
2706 int vlan_id
= qdict_get_int(qdict
, "vlan_id");
2707 const char *device
= qdict_get_str(qdict
, "device");
2709 vc
= qemu_find_vlan_client_by_name(mon
, vlan_id
, device
);
2713 if (!net_host_check_device(vc
->model
)) {
2714 monitor_printf(mon
, "invalid host network device %s\n", device
);
2717 qemu_del_vlan_client(vc
);
2720 void net_set_boot_mask(int net_boot_mask
)
2724 /* Only the first four NICs may be bootable */
2725 net_boot_mask
= net_boot_mask
& 0xF;
2727 for (i
= 0; i
< nb_nics
; i
++) {
2728 if (net_boot_mask
& (1 << i
)) {
2729 nd_table
[i
].bootable
= 1;
2730 net_boot_mask
&= ~(1 << i
);
2734 if (net_boot_mask
) {
2735 fprintf(stderr
, "Cannot boot from non-existent NIC\n");
2740 void do_info_network(Monitor
*mon
)
2744 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2745 VLANClientState
*vc
;
2747 monitor_printf(mon
, "VLAN %d devices:\n", vlan
->id
);
2749 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
2750 monitor_printf(mon
, " %s: %s\n", vc
->name
, vc
->info_str
);
2755 void do_set_link(Monitor
*mon
, const QDict
*qdict
)
2758 VLANClientState
*vc
= NULL
;
2759 const char *name
= qdict_get_str(qdict
, "name");
2760 const char *up_or_down
= qdict_get_str(qdict
, "up_or_down");
2762 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2763 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
2764 if (strcmp(vc
->name
, name
) == 0) {
2772 monitor_printf(mon
, "could not find network device '%s'\n", name
);
2776 if (strcmp(up_or_down
, "up") == 0)
2778 else if (strcmp(up_or_down
, "down") == 0)
2781 monitor_printf(mon
, "invalid link status '%s'; only 'up' or 'down' "
2782 "valid\n", up_or_down
);
2784 if (vc
->link_status_changed
)
2785 vc
->link_status_changed(vc
);
2788 void net_cleanup(void)
2791 VLANClientState
*vc
, *next_vc
;
2793 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2794 QTAILQ_FOREACH_SAFE(vc
, &vlan
->clients
, next
, next_vc
) {
2795 qemu_del_vlan_client(vc
);
2799 QTAILQ_FOREACH_SAFE(vc
, &non_vlan_clients
, next
, next_vc
) {
2800 qemu_del_vlan_client(vc
);
2804 static void net_check_clients(void)
2808 QTAILQ_FOREACH(vlan
, &vlans
, next
) {
2809 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
2811 if (vlan
->nb_guest_devs
== 0)
2812 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
2813 if (vlan
->nb_host_devs
== 0)
2815 "Warning: vlan %d is not connected to host network\n",
2820 static int net_init_client(QemuOpts
*opts
, void *dummy
)
2822 if (net_client_init(NULL
, opts
, 0) < 0)
2827 static int net_init_netdev(QemuOpts
*opts
, void *dummy
)
2829 return net_client_init(NULL
, opts
, 1);
2832 int net_init_clients(void)
2834 if (QTAILQ_EMPTY(&qemu_net_opts
.head
)) {
2835 /* if no clients, we use a default config */
2836 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "nic");
2838 qemu_opts_set(&qemu_net_opts
, NULL
, "type", "user");
2842 QTAILQ_INIT(&vlans
);
2843 QTAILQ_INIT(&non_vlan_clients
);
2845 if (qemu_opts_foreach(&qemu_netdev_opts
, net_init_netdev
, NULL
, 1) == -1)
2848 if (qemu_opts_foreach(&qemu_net_opts
, net_init_client
, NULL
, 1) == -1) {
2852 net_check_clients();
2857 int net_client_parse(QemuOptsList
*opts_list
, const char *optarg
)
2859 #if defined(CONFIG_SLIRP)
2860 /* handle legacy -net channel,port:chr */
2861 if (!strcmp(opts_list
->name
, "net") &&
2862 !strncmp(optarg
, "channel,", strlen("channel,"))) {
2865 optarg
+= strlen("channel,");
2867 if (QTAILQ_EMPTY(&slirp_stacks
)) {
2868 struct slirp_config_str
*config
;
2870 config
= qemu_malloc(sizeof(*config
));
2871 pstrcpy(config
->str
, sizeof(config
->str
), optarg
);
2872 config
->flags
= SLIRP_CFG_LEGACY
;
2873 config
->next
= slirp_configs
;
2874 slirp_configs
= config
;
2877 ret
= slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks
), optarg
, 1);
2883 if (!qemu_opts_parse(opts_list
, optarg
, "type")) {