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
24 #include "qemu-common.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
30 #include "audio/audio.h"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
51 #include <net/if_tap.h>
54 #include <linux/if_tun.h>
56 #include <arpa/inet.h>
59 #include <sys/select.h>
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 #include <freebsd/stdlib.h>
73 #include <linux/rtc.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #include "qemu_socket.h"
103 #if defined(CONFIG_SLIRP)
104 #include "libslirp.h"
107 #if defined(__OpenBSD__)
111 #if defined(CONFIG_VDE)
112 #include <libvdeplug.h>
117 #include <sys/timeb.h>
118 #include <mmsystem.h>
119 #define getopt_long_only getopt_long
120 #define memalign(align, size) malloc(size)
123 static VLANState
*first_vlan
;
125 /***********************************************************/
126 /* network device redirectors */
128 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
129 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
133 for(i
=0;i
<size
;i
+=16) {
137 fprintf(f
, "%08x ", i
);
140 fprintf(f
, " %02x", buf
[i
+j
]);
147 if (c
< ' ' || c
> '~')
156 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
163 offset
= strtol(p
, &last_char
, 0);
164 if (0 == errno
&& '\0' == *last_char
&&
165 offset
>= 0 && offset
<= 0xFFFFFF) {
166 macaddr
[3] = (offset
& 0xFF0000) >> 16;
167 macaddr
[4] = (offset
& 0xFF00) >> 8;
168 macaddr
[5] = offset
& 0xFF;
171 for(i
= 0; i
< 6; i
++) {
172 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
177 if (*p
!= ':' && *p
!= '-')
188 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
199 if (len
> buf_size
- 1)
208 int parse_host_src_port(struct sockaddr_in
*haddr
,
209 struct sockaddr_in
*saddr
,
210 const char *input_str
)
212 char *str
= strdup(input_str
);
213 char *host_str
= str
;
215 const char *src_str2
;
219 * Chop off any extra arguments at the end of the string which
220 * would start with a comma, then fill in the src port information
221 * if it was provided else use the "any address" and "any port".
223 if ((ptr
= strchr(str
,',')))
226 if ((src_str
= strchr(input_str
,'@'))) {
231 if (parse_host_port(haddr
, host_str
) < 0)
235 if (!src_str
|| *src_str
== '\0')
238 if (parse_host_port(saddr
, src_str2
) < 0)
249 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
257 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
259 saddr
->sin_family
= AF_INET
;
260 if (buf
[0] == '\0') {
261 saddr
->sin_addr
.s_addr
= 0;
263 if (qemu_isdigit(buf
[0])) {
264 if (!inet_aton(buf
, &saddr
->sin_addr
))
267 if ((he
= gethostbyname(buf
)) == NULL
)
269 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
272 port
= strtol(p
, (char **)&r
, 0);
275 saddr
->sin_port
= htons(port
);
279 #if !defined(_WIN32) && 0
280 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
285 len
= MIN(108, strlen(str
));
286 p
= strchr(str
, ',');
288 len
= MIN(len
, p
- str
);
290 memset(uaddr
, 0, sizeof(*uaddr
));
292 uaddr
->sun_family
= AF_UNIX
;
293 memcpy(uaddr
->sun_path
, str
, len
);
299 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
301 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
302 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
304 macaddr
[0], macaddr
[1], macaddr
[2],
305 macaddr
[3], macaddr
[4], macaddr
[5]);
308 static char *assign_name(VLANClientState
*vc1
, const char *model
)
314 for (vlan
= first_vlan
; vlan
; vlan
= vlan
->next
) {
317 for (vc
= vlan
->first_client
; vc
; vc
= vc
->next
)
318 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0)
322 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
327 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
330 IOReadHandler
*fd_read
,
331 IOCanRWHandler
*fd_can_read
,
334 VLANClientState
*vc
, **pvc
;
335 vc
= qemu_mallocz(sizeof(VLANClientState
));
338 vc
->model
= strdup(model
);
340 vc
->name
= strdup(name
);
342 vc
->name
= assign_name(vc
, model
);
343 vc
->fd_read
= fd_read
;
344 vc
->fd_can_read
= fd_can_read
;
349 pvc
= &vlan
->first_client
;
356 void qemu_del_vlan_client(VLANClientState
*vc
)
358 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
371 int qemu_can_send_packet(VLANClientState
*vc1
)
373 VLANState
*vlan
= vc1
->vlan
;
376 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
378 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
385 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
387 VLANState
*vlan
= vc1
->vlan
;
394 printf("vlan %d send:\n", vlan
->id
);
395 hex_dump(stdout
, buf
, size
);
397 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
398 if (vc
!= vc1
&& !vc
->link_down
) {
399 vc
->fd_read(vc
->opaque
, buf
, size
);
404 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
407 uint8_t buffer
[4096];
411 for (i
= 0; i
< iovcnt
; i
++) {
414 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
415 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
419 vc
->fd_read(vc
->opaque
, buffer
, offset
);
424 static ssize_t
calc_iov_length(const struct iovec
*iov
, int iovcnt
)
429 for (i
= 0; i
< iovcnt
; i
++)
430 offset
+= iov
[i
].iov_len
;
434 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
437 VLANState
*vlan
= vc1
->vlan
;
442 return calc_iov_length(iov
, iovcnt
);
444 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
451 len
= calc_iov_length(iov
, iovcnt
);
453 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
454 else if (vc
->fd_read
)
455 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
457 max_len
= MAX(max_len
, len
);
463 #if defined(CONFIG_SLIRP)
465 /* slirp network adapter */
467 static int slirp_inited
;
468 static int slirp_restrict
;
469 static char *slirp_ip
;
470 static VLANClientState
*slirp_vc
;
472 int slirp_can_output(void)
474 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
477 void slirp_output(const uint8_t *pkt
, int pkt_len
)
480 printf("slirp output:\n");
481 hex_dump(stdout
, pkt
, pkt_len
);
485 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
488 int slirp_is_inited(void)
493 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
496 printf("slirp input:\n");
497 hex_dump(stdout
, buf
, size
);
499 slirp_input(buf
, size
);
502 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
506 slirp_init(slirp_restrict
, slirp_ip
);
508 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
509 slirp_receive
, NULL
, NULL
);
510 slirp_vc
->info_str
[0] = '\0';
514 void net_slirp_redir(const char *redir_str
)
519 struct in_addr guest_addr
;
520 int host_port
, guest_port
;
524 slirp_init(slirp_restrict
, slirp_ip
);
528 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
530 if (!strcmp(buf
, "tcp")) {
532 } else if (!strcmp(buf
, "udp")) {
538 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
540 host_port
= strtol(buf
, &r
, 0);
544 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
546 if (buf
[0] == '\0') {
547 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
549 if (!inet_aton(buf
, &guest_addr
))
552 guest_port
= strtol(p
, &r
, 0);
556 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
557 fprintf(stderr
, "qemu: could not set up redirection\n");
562 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
568 static char smb_dir
[1024];
570 static void erase_dir(char *dir_name
)
576 /* erase all the files in the directory */
577 if ((d
= opendir(dir_name
)) != 0) {
582 if (strcmp(de
->d_name
, ".") != 0 &&
583 strcmp(de
->d_name
, "..") != 0) {
584 snprintf(filename
, sizeof(filename
), "%s/%s",
585 smb_dir
, de
->d_name
);
586 if (unlink(filename
) != 0) /* is it a directory? */
595 /* automatic user mode samba server configuration */
596 static void smb_exit(void)
601 /* automatic user mode samba server configuration */
602 void net_slirp_smb(const char *exported_dir
)
605 char smb_cmdline
[1024];
610 slirp_init(slirp_restrict
, slirp_ip
);
613 /* XXX: better tmp dir construction */
614 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
615 if (mkdir(smb_dir
, 0700) < 0) {
616 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
619 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
621 f
= fopen(smb_conf
, "w");
623 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
630 "socket address=127.0.0.1\n"
632 "lock directory=%s\n"
633 "log file=%s/log.smbd\n"
634 "smb passwd file=%s/smbpasswd\n"
650 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
651 SMBD_COMMAND
, smb_conf
);
653 slirp_add_exec(0, smb_cmdline
, 4, 139);
656 #endif /* !defined(_WIN32) */
657 void do_info_slirp(void)
662 #endif /* CONFIG_SLIRP */
666 typedef struct TAPState
{
669 char down_script
[1024];
670 char down_script_arg
[128];
674 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
677 TAPState
*s
= opaque
;
681 len
= writev(s
->fd
, iov
, iovcnt
);
682 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
688 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
690 TAPState
*s
= opaque
;
693 ret
= write(s
->fd
, buf
, size
);
694 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
701 static void tap_send(void *opaque
)
703 TAPState
*s
= opaque
;
710 sbuf
.maxlen
= sizeof(buf
);
712 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
714 size
= read(s
->fd
, buf
, sizeof(buf
));
717 qemu_send_packet(s
->vc
, buf
, size
);
723 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
730 s
= qemu_mallocz(sizeof(TAPState
));
734 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
, NULL
, s
);
736 s
->vc
->fd_readv
= tap_receive_iov
;
738 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
739 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
743 #if defined (_BSD) || defined (__FreeBSD_kernel__)
744 static int tap_open(char *ifname
, int ifname_size
)
750 TFR(fd
= open("/dev/tap", O_RDWR
));
752 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
757 dev
= devname(s
.st_rdev
, S_IFCHR
);
758 pstrcpy(ifname
, ifname_size
, dev
);
760 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
763 #elif defined(__sun__)
764 #define TUNNEWPPA (('T'<<16) | 0x0001)
766 * Allocate TAP device, returns opened fd.
767 * Stores dev name in the first arg(must be large enough).
769 int tap_alloc(char *dev
, size_t dev_size
)
771 int tap_fd
, if_fd
, ppa
= -1;
772 static int ip_fd
= 0;
775 static int arp_fd
= 0;
776 int ip_muxid
, arp_muxid
;
777 struct strioctl strioc_if
, strioc_ppa
;
778 int link_type
= I_PLINK
;;
780 char actual_name
[32] = "";
782 memset(&ifr
, 0x0, sizeof(ifr
));
786 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
790 /* Check if IP device was opened */
794 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
796 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
800 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
802 syslog(LOG_ERR
, "Can't open /dev/tap");
806 /* Assign a new PPA and get its unit number. */
807 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
808 strioc_ppa
.ic_timout
= 0;
809 strioc_ppa
.ic_len
= sizeof(ppa
);
810 strioc_ppa
.ic_dp
= (char *)&ppa
;
811 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
812 syslog (LOG_ERR
, "Can't assign new interface");
814 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
816 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
819 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
820 syslog(LOG_ERR
, "Can't push IP module");
824 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
825 syslog(LOG_ERR
, "Can't get flags\n");
827 snprintf (actual_name
, 32, "tap%d", ppa
);
828 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
831 /* Assign ppa according to the unit number returned by tun device */
833 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
834 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
835 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
836 syslog (LOG_ERR
, "Can't get flags\n");
837 /* Push arp module to if_fd */
838 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
839 syslog (LOG_ERR
, "Can't push ARP module (2)");
841 /* Push arp module to ip_fd */
842 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
843 syslog (LOG_ERR
, "I_POP failed\n");
844 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
845 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
847 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
849 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
851 /* Set ifname to arp */
852 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
853 strioc_if
.ic_timout
= 0;
854 strioc_if
.ic_len
= sizeof(ifr
);
855 strioc_if
.ic_dp
= (char *)&ifr
;
856 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
857 syslog (LOG_ERR
, "Can't set ifname to arp\n");
860 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
861 syslog(LOG_ERR
, "Can't link TAP device to IP");
865 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
866 syslog (LOG_ERR
, "Can't link TAP device to ARP");
870 memset(&ifr
, 0x0, sizeof(ifr
));
871 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
872 ifr
.lifr_ip_muxid
= ip_muxid
;
873 ifr
.lifr_arp_muxid
= arp_muxid
;
875 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
877 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
878 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
879 syslog (LOG_ERR
, "Can't set multiplexor id");
882 snprintf(dev
, dev_size
, "tap%d", ppa
);
886 static int tap_open(char *ifname
, int ifname_size
)
890 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
891 fprintf(stderr
, "Cannot allocate TAP device\n");
894 pstrcpy(ifname
, ifname_size
, dev
);
895 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
899 static int tap_open(char *ifname
, int ifname_size
)
901 fprintf (stderr
, "no tap on AIX\n");
905 static int tap_open(char *ifname
, int ifname_size
)
910 TFR(fd
= open("/dev/net/tun", O_RDWR
));
912 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
915 memset(&ifr
, 0, sizeof(ifr
));
916 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
917 if (ifname
[0] != '\0')
918 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
920 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
921 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
923 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
927 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
928 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
933 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
939 /* try to launch network script */
943 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
944 for (i
= 0; i
< open_max
; i
++)
945 if (i
!= STDIN_FILENO
&&
946 i
!= STDOUT_FILENO
&&
947 i
!= STDERR_FILENO
&&
952 *parg
++ = (char *)setup_script
;
953 *parg
++ = (char *)ifname
;
955 execv(setup_script
, args
);
958 while (waitpid(pid
, &status
, 0) != pid
);
959 if (!WIFEXITED(status
) ||
960 WEXITSTATUS(status
) != 0) {
961 fprintf(stderr
, "%s: could not launch network script\n",
969 static int net_tap_init(VLANState
*vlan
, const char *model
,
970 const char *name
, const char *ifname1
,
971 const char *setup_script
, const char *down_script
)
978 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
981 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
985 if (!setup_script
|| !strcmp(setup_script
, "no"))
987 if (setup_script
[0] != '\0') {
988 if (launch_script(setup_script
, ifname
, fd
))
991 s
= net_tap_fd_init(vlan
, model
, name
, fd
);
994 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
995 "ifname=%s,script=%s,downscript=%s",
996 ifname
, setup_script
, down_script
);
997 if (down_script
&& strcmp(down_script
, "no")) {
998 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
999 snprintf(s
->down_script_arg
, sizeof(s
->down_script_arg
), "%s", ifname
);
1004 #endif /* !_WIN32 */
1006 #if defined(CONFIG_VDE)
1007 typedef struct VDEState
{
1008 VLANClientState
*vc
;
1012 static void vde_to_qemu(void *opaque
)
1014 VDEState
*s
= opaque
;
1018 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
1020 qemu_send_packet(s
->vc
, buf
, size
);
1024 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1026 VDEState
*s
= opaque
;
1029 ret
= vde_send(s
->vde
, buf
, size
, 0);
1030 if (ret
< 0 && errno
== EINTR
) {
1037 static int net_vde_init(VLANState
*vlan
, const char *model
,
1038 const char *name
, const char *sock
,
1039 int port
, const char *group
, int mode
)
1042 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1043 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1045 struct vde_open_args args
= {
1047 .group
= init_group
,
1051 s
= qemu_mallocz(sizeof(VDEState
));
1054 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
1059 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
, NULL
, s
);
1060 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1061 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1062 sock
, vde_datafd(s
->vde
));
1067 /* network connection */
1068 typedef struct NetSocketState
{
1069 VLANClientState
*vc
;
1071 int state
; /* 0 = getting length, 1 = getting data */
1075 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1078 typedef struct NetSocketListenState
{
1083 } NetSocketListenState
;
1085 /* XXX: we consider we can send the whole packet without blocking */
1086 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1088 NetSocketState
*s
= opaque
;
1092 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1093 send_all(s
->fd
, buf
, size
);
1096 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1098 NetSocketState
*s
= opaque
;
1099 sendto(s
->fd
, buf
, size
, 0,
1100 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1103 static void net_socket_send(void *opaque
)
1105 NetSocketState
*s
= opaque
;
1110 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1112 err
= socket_error();
1113 if (err
!= EWOULDBLOCK
)
1115 } else if (size
== 0) {
1116 /* end of connection */
1118 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1124 /* reassemble a packet from the network */
1130 memcpy(s
->buf
+ s
->index
, buf
, l
);
1134 if (s
->index
== 4) {
1136 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1142 l
= s
->packet_len
- s
->index
;
1145 memcpy(s
->buf
+ s
->index
, buf
, l
);
1149 if (s
->index
>= s
->packet_len
) {
1150 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1159 static void net_socket_send_dgram(void *opaque
)
1161 NetSocketState
*s
= opaque
;
1164 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1168 /* end of connection */
1169 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1172 qemu_send_packet(s
->vc
, s
->buf
, size
);
1175 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1180 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1181 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1182 inet_ntoa(mcastaddr
->sin_addr
),
1183 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1187 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1189 perror("socket(PF_INET, SOCK_DGRAM)");
1194 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1195 (const char *)&val
, sizeof(val
));
1197 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1201 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1207 /* Add host to multicast group */
1208 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1209 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1211 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1212 (const char *)&imr
, sizeof(struct ip_mreq
));
1214 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1218 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1220 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1221 (const char *)&val
, sizeof(val
));
1223 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1227 socket_set_nonblock(fd
);
1235 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1238 int fd
, int is_connected
)
1240 struct sockaddr_in saddr
;
1242 socklen_t saddr_len
;
1245 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1246 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1247 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1251 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1253 if (saddr
.sin_addr
.s_addr
==0) {
1254 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1258 /* clone dgram socket */
1259 newfd
= net_socket_mcast_create(&saddr
);
1261 /* error already reported by net_socket_mcast_create() */
1265 /* clone newfd to fd, close newfd */
1270 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1271 fd
, strerror(errno
));
1276 s
= qemu_mallocz(sizeof(NetSocketState
));
1281 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
, NULL
, s
);
1282 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1284 /* mcast: save bound address as dst */
1285 if (is_connected
) s
->dgram_dst
=saddr
;
1287 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1288 "socket: fd=%d (%s mcast=%s:%d)",
1289 fd
, is_connected
? "cloned" : "",
1290 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1294 static void net_socket_connect(void *opaque
)
1296 NetSocketState
*s
= opaque
;
1297 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1300 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1303 int fd
, int is_connected
)
1306 s
= qemu_mallocz(sizeof(NetSocketState
));
1310 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
,
1311 net_socket_receive
, NULL
, s
);
1312 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1313 "socket: fd=%d", fd
);
1315 net_socket_connect(s
);
1317 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1322 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1323 const char *model
, const char *name
,
1324 int fd
, int is_connected
)
1326 int so_type
=-1, optlen
=sizeof(so_type
);
1328 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1329 (socklen_t
*)&optlen
)< 0) {
1330 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1335 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1337 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1339 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1340 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1341 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1346 static void net_socket_accept(void *opaque
)
1348 NetSocketListenState
*s
= opaque
;
1350 struct sockaddr_in saddr
;
1355 len
= sizeof(saddr
);
1356 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1357 if (fd
< 0 && errno
!= EINTR
) {
1359 } else if (fd
>= 0) {
1363 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1367 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1368 "socket: connection from %s:%d",
1369 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1373 static int net_socket_listen_init(VLANState
*vlan
,
1376 const char *host_str
)
1378 NetSocketListenState
*s
;
1380 struct sockaddr_in saddr
;
1382 if (parse_host_port(&saddr
, host_str
) < 0)
1385 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1389 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1394 socket_set_nonblock(fd
);
1396 /* allow fast reuse */
1398 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1400 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1405 ret
= listen(fd
, 0);
1411 s
->model
= strdup(model
);
1412 s
->name
= strdup(name
);
1414 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1418 static int net_socket_connect_init(VLANState
*vlan
,
1421 const char *host_str
)
1424 int fd
, connected
, ret
, err
;
1425 struct sockaddr_in saddr
;
1427 if (parse_host_port(&saddr
, host_str
) < 0)
1430 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1435 socket_set_nonblock(fd
);
1439 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1441 err
= socket_error();
1442 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1443 } else if (err
== EINPROGRESS
) {
1446 } else if (err
== WSAEALREADY
) {
1459 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1462 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1463 "socket: connect to %s:%d",
1464 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1468 static int net_socket_mcast_init(VLANState
*vlan
,
1471 const char *host_str
)
1475 struct sockaddr_in saddr
;
1477 if (parse_host_port(&saddr
, host_str
) < 0)
1481 fd
= net_socket_mcast_create(&saddr
);
1485 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1489 s
->dgram_dst
= saddr
;
1491 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1492 "socket: mcast=%s:%d",
1493 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1498 /* find or alloc a new VLAN */
1499 VLANState
*qemu_find_vlan(int id
)
1501 VLANState
**pvlan
, *vlan
;
1502 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1506 vlan
= qemu_mallocz(sizeof(VLANState
));
1511 pvlan
= &first_vlan
;
1512 while (*pvlan
!= NULL
)
1513 pvlan
= &(*pvlan
)->next
;
1518 void qemu_check_nic_model(NICInfo
*nd
, const char *model
)
1520 const char *models
[2];
1525 qemu_check_nic_model_list(nd
, models
, model
);
1528 void qemu_check_nic_model_list(NICInfo
*nd
, const char * const *models
,
1529 const char *default_model
)
1531 int i
, exit_status
= 0;
1534 nd
->model
= strdup(default_model
);
1536 if (strcmp(nd
->model
, "?") != 0) {
1537 for (i
= 0 ; models
[i
]; i
++)
1538 if (strcmp(nd
->model
, models
[i
]) == 0)
1541 fprintf(stderr
, "qemu: Unsupported NIC model: %s\n", nd
->model
);
1545 fprintf(stderr
, "qemu: Supported NIC models: ");
1546 for (i
= 0 ; models
[i
]; i
++)
1547 fprintf(stderr
, "%s%c", models
[i
], models
[i
+1] ? ',' : '\n');
1552 int net_client_init(const char *device
, const char *p
)
1560 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1561 vlan_id
= strtol(buf
, NULL
, 0);
1563 vlan
= qemu_find_vlan(vlan_id
);
1565 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1568 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1571 if (!strcmp(device
, "nic")) {
1575 if (nb_nics
>= MAX_NICS
) {
1576 fprintf(stderr
, "Too Many NICs\n");
1579 nd
= &nd_table
[nb_nics
];
1580 macaddr
= nd
->macaddr
;
1586 macaddr
[5] = 0x56 + nb_nics
;
1588 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1589 if (parse_macaddr(macaddr
, buf
) < 0) {
1590 fprintf(stderr
, "invalid syntax for ethernet address\n");
1594 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1595 nd
->model
= strdup(buf
);
1601 vlan
->nb_guest_devs
++;
1604 if (!strcmp(device
, "none")) {
1605 /* does nothing. It is needed to signal that no network cards
1610 if (!strcmp(device
, "user")) {
1611 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1612 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1614 if (get_param_value(buf
, sizeof(buf
), "restrict", p
)) {
1615 slirp_restrict
= (buf
[0] == 'y') ? 1 : 0;
1617 if (get_param_value(buf
, sizeof(buf
), "ip", p
)) {
1618 slirp_ip
= strdup(buf
);
1620 vlan
->nb_host_devs
++;
1621 ret
= net_slirp_init(vlan
, device
, name
);
1625 if (!strcmp(device
, "tap")) {
1627 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1628 fprintf(stderr
, "tap: no interface name\n");
1631 vlan
->nb_host_devs
++;
1632 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
1634 #elif defined (_AIX)
1636 if (!strcmp(device
, "tap")) {
1638 char setup_script
[1024], down_script
[1024];
1640 vlan
->nb_host_devs
++;
1641 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1642 fd
= strtol(buf
, NULL
, 0);
1643 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1645 if (net_tap_fd_init(vlan
, device
, name
, fd
))
1648 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1651 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1652 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1654 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1655 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1657 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
1661 if (!strcmp(device
, "socket")) {
1662 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1664 fd
= strtol(buf
, NULL
, 0);
1666 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
1668 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1669 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
1670 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1671 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
1672 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1673 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
1675 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1678 vlan
->nb_host_devs
++;
1681 if (!strcmp(device
, "vde")) {
1682 char vde_sock
[1024], vde_group
[512];
1683 int vde_port
, vde_mode
;
1684 vlan
->nb_host_devs
++;
1685 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1688 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1689 vde_port
= strtol(buf
, NULL
, 10);
1693 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1694 vde_group
[0] = '\0';
1696 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1697 vde_mode
= strtol(buf
, NULL
, 8);
1701 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1705 fprintf(stderr
, "Unknown network device: %s\n", device
);
1711 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1718 int net_client_parse(const char *str
)
1726 while (*p
!= '\0' && *p
!= ',') {
1727 if ((q
- device
) < sizeof(device
) - 1)
1735 return net_client_init(device
, p
);
1738 void do_info_network(void)
1741 VLANClientState
*vc
;
1743 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1744 term_printf("VLAN %d devices:\n", vlan
->id
);
1745 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1746 term_printf(" %s: %s\n", vc
->name
, vc
->info_str
);
1750 int do_set_link(const char *name
, const char *up_or_down
)
1753 VLANClientState
*vc
= NULL
;
1755 for (vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
)
1756 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1757 if (strcmp(vc
->name
, name
) == 0)
1762 term_printf("could not find network device '%s'", name
);
1766 if (strcmp(up_or_down
, "up") == 0)
1768 else if (strcmp(up_or_down
, "down") == 0)
1771 term_printf("invalid link status '%s'; only 'up' or 'down' valid\n",
1774 if (vc
->link_status_changed
)
1775 vc
->link_status_changed(vc
);
1780 void net_cleanup(void)
1784 #if !defined(_WIN32)
1785 /* close network clients */
1786 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1787 VLANClientState
*vc
;
1789 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1790 if (vc
->fd_read
== tap_receive
) {
1791 TAPState
*s
= vc
->opaque
;
1793 if (s
->down_script
[0])
1794 launch_script(s
->down_script
, s
->down_script_arg
, s
->fd
);
1796 #if defined(CONFIG_VDE)
1797 if (vc
->fd_read
== vde_from_qemu
) {
1798 VDEState
*s
= vc
->opaque
;
1807 void net_client_check(void)
1811 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1812 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1814 if (vlan
->nb_guest_devs
== 0)
1815 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1816 if (vlan
->nb_host_devs
== 0)
1818 "Warning: vlan %d is not connected to host network\n",