4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
40 #include "audio/audio.h"
41 #include "migration.h"
52 #include <sys/times.h>
56 #include <sys/ioctl.h>
57 #include <sys/resource.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
62 #include <net/if_tap.h>
65 #include <linux/if_tun.h>
67 #include <arpa/inet.h>
70 #include <sys/select.h>
78 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
79 #include <freebsd/stdlib.h>
84 #include <linux/rtc.h>
86 /* For the benefit of older linux systems which don't supply it,
87 we use a local copy of hpet.h. */
88 /* #include <linux/hpet.h> */
91 #include <linux/ppdev.h>
92 #include <linux/parport.h>
96 #include <sys/ethernet.h>
97 #include <sys/sockio.h>
98 #include <netinet/arp.h>
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
102 #include <netinet/ip_icmp.h> // must come after ip.h
103 #include <netinet/udp.h>
104 #include <netinet/tcp.h>
112 #include "qemu_socket.h"
114 #if defined(CONFIG_SLIRP)
115 #include "libslirp.h"
118 #if defined(__OpenBSD__)
122 #if defined(CONFIG_VDE)
123 #include <libvdeplug.h>
128 #include <sys/timeb.h>
129 #include <mmsystem.h>
130 #define getopt_long_only getopt_long
131 #define memalign(align, size) malloc(size)
134 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
135 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
137 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
139 #define SMBD_COMMAND "/usr/sbin/smbd"
142 static VLANState
*first_vlan
;
144 /***********************************************************/
145 /* network device redirectors */
147 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
148 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
152 for(i
=0;i
<size
;i
+=16) {
156 fprintf(f
, "%08x ", i
);
159 fprintf(f
, " %02x", buf
[i
+j
]);
166 if (c
< ' ' || c
> '~')
175 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
182 offset
= strtol(p
, &last_char
, 0);
183 if (0 == errno
&& '\0' == *last_char
&&
184 offset
>= 0 && offset
<= 0xFFFFFF) {
185 macaddr
[3] = (offset
& 0xFF0000) >> 16;
186 macaddr
[4] = (offset
& 0xFF00) >> 8;
187 macaddr
[5] = offset
& 0xFF;
190 for(i
= 0; i
< 6; i
++) {
191 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
196 if (*p
!= ':' && *p
!= '-')
207 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
218 if (len
> buf_size
- 1)
227 int parse_host_src_port(struct sockaddr_in
*haddr
,
228 struct sockaddr_in
*saddr
,
229 const char *input_str
)
231 char *str
= strdup(input_str
);
232 char *host_str
= str
;
234 const char *src_str2
;
238 * Chop off any extra arguments at the end of the string which
239 * would start with a comma, then fill in the src port information
240 * if it was provided else use the "any address" and "any port".
242 if ((ptr
= strchr(str
,',')))
245 if ((src_str
= strchr(input_str
,'@'))) {
250 if (parse_host_port(haddr
, host_str
) < 0)
254 if (!src_str
|| *src_str
== '\0')
257 if (parse_host_port(saddr
, src_str2
) < 0)
268 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
276 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
278 saddr
->sin_family
= AF_INET
;
279 if (buf
[0] == '\0') {
280 saddr
->sin_addr
.s_addr
= 0;
282 if (isdigit(buf
[0])) {
283 if (!inet_aton(buf
, &saddr
->sin_addr
))
286 if ((he
= gethostbyname(buf
)) == NULL
)
288 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
291 port
= strtol(p
, (char **)&r
, 0);
294 saddr
->sin_port
= htons(port
);
299 int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
304 len
= MIN(108, strlen(str
));
305 p
= strchr(str
, ',');
307 len
= MIN(len
, p
- str
);
309 memset(uaddr
, 0, sizeof(*uaddr
));
311 uaddr
->sun_family
= AF_UNIX
;
312 memcpy(uaddr
->sun_path
, str
, len
);
318 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
319 IOReadHandler
*fd_read
,
320 IOCanRWHandler
*fd_can_read
,
323 VLANClientState
*vc
, **pvc
;
324 vc
= qemu_mallocz(sizeof(VLANClientState
));
327 vc
->fd_read
= fd_read
;
328 vc
->fd_can_read
= fd_can_read
;
333 pvc
= &vlan
->first_client
;
340 void qemu_del_vlan_client(VLANClientState
*vc
)
342 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
353 int qemu_can_send_packet(VLANClientState
*vc1
)
355 VLANState
*vlan
= vc1
->vlan
;
358 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
360 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
367 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
369 VLANState
*vlan
= vc1
->vlan
;
373 printf("vlan %d send:\n", vlan
->id
);
374 hex_dump(stdout
, buf
, size
);
376 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
378 vc
->fd_read(vc
->opaque
, buf
, size
);
383 #if defined(CONFIG_SLIRP)
385 /* slirp network adapter */
387 static int slirp_inited
;
388 static VLANClientState
*slirp_vc
;
390 int slirp_can_output(void)
392 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
395 void slirp_output(const uint8_t *pkt
, int pkt_len
)
398 printf("slirp output:\n");
399 hex_dump(stdout
, pkt
, pkt_len
);
403 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
406 int slirp_is_inited(void)
411 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
414 printf("slirp input:\n");
415 hex_dump(stdout
, buf
, size
);
417 slirp_input(buf
, size
);
420 static int net_slirp_init(VLANState
*vlan
)
426 slirp_vc
= qemu_new_vlan_client(vlan
,
427 slirp_receive
, NULL
, NULL
);
428 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
432 void net_slirp_redir(const char *redir_str
)
437 struct in_addr guest_addr
;
438 int host_port
, guest_port
;
446 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
448 if (!strcmp(buf
, "tcp")) {
450 } else if (!strcmp(buf
, "udp")) {
456 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
458 host_port
= strtol(buf
, &r
, 0);
462 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
464 if (buf
[0] == '\0') {
465 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
467 if (!inet_aton(buf
, &guest_addr
))
470 guest_port
= strtol(p
, &r
, 0);
474 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
475 fprintf(stderr
, "qemu: could not set up redirection\n");
480 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
486 static char smb_dir
[1024];
488 static void erase_dir(char *dir_name
)
494 /* erase all the files in the directory */
495 if ((d
= opendir(dir_name
)) != 0) {
500 if (strcmp(de
->d_name
, ".") != 0 &&
501 strcmp(de
->d_name
, "..") != 0) {
502 snprintf(filename
, sizeof(filename
), "%s/%s",
503 smb_dir
, de
->d_name
);
504 if (unlink(filename
) != 0) /* is it a directory? */
513 /* automatic user mode samba server configuration */
514 static void smb_exit(void)
519 /* automatic user mode samba server configuration */
520 void net_slirp_smb(const char *exported_dir
)
523 char smb_cmdline
[1024];
531 /* XXX: better tmp dir construction */
532 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
533 if (mkdir(smb_dir
, 0700) < 0) {
534 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
537 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
539 f
= fopen(smb_conf
, "w");
541 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
548 "socket address=127.0.0.1\n"
550 "lock directory=%s\n"
551 "log file=%s/log.smbd\n"
552 "smb passwd file=%s/smbpasswd\n"
568 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
569 SMBD_COMMAND
, smb_conf
);
571 slirp_add_exec(0, smb_cmdline
, 4, 139);
574 #endif /* !defined(_WIN32) */
575 void do_info_slirp(void)
580 #endif /* CONFIG_SLIRP */
584 typedef struct TAPState
{
587 char down_script
[1024];
590 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
592 TAPState
*s
= opaque
;
595 ret
= write(s
->fd
, buf
, size
);
596 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
603 static void tap_send(void *opaque
)
605 TAPState
*s
= opaque
;
612 sbuf
.maxlen
= sizeof(buf
);
614 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
616 size
= read(s
->fd
, buf
, sizeof(buf
));
619 qemu_send_packet(s
->vc
, buf
, size
);
625 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
629 s
= qemu_mallocz(sizeof(TAPState
));
633 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
634 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
635 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
639 #if defined (_BSD) || defined (__FreeBSD_kernel__)
640 static int tap_open(char *ifname
, int ifname_size
)
646 TFR(fd
= open("/dev/tap", O_RDWR
));
648 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
653 dev
= devname(s
.st_rdev
, S_IFCHR
);
654 pstrcpy(ifname
, ifname_size
, dev
);
656 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
659 #elif defined(__sun__)
660 #define TUNNEWPPA (('T'<<16) | 0x0001)
662 * Allocate TAP device, returns opened fd.
663 * Stores dev name in the first arg(must be large enough).
665 int tap_alloc(char *dev
, size_t dev_size
)
667 int tap_fd
, if_fd
, ppa
= -1;
668 static int ip_fd
= 0;
671 static int arp_fd
= 0;
672 int ip_muxid
, arp_muxid
;
673 struct strioctl strioc_if
, strioc_ppa
;
674 int link_type
= I_PLINK
;;
676 char actual_name
[32] = "";
678 memset(&ifr
, 0x0, sizeof(ifr
));
682 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
686 /* Check if IP device was opened */
690 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
692 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
696 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
698 syslog(LOG_ERR
, "Can't open /dev/tap");
702 /* Assign a new PPA and get its unit number. */
703 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
704 strioc_ppa
.ic_timout
= 0;
705 strioc_ppa
.ic_len
= sizeof(ppa
);
706 strioc_ppa
.ic_dp
= (char *)&ppa
;
707 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
708 syslog (LOG_ERR
, "Can't assign new interface");
710 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
712 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
715 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
716 syslog(LOG_ERR
, "Can't push IP module");
720 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
721 syslog(LOG_ERR
, "Can't get flags\n");
723 snprintf (actual_name
, 32, "tap%d", ppa
);
724 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
727 /* Assign ppa according to the unit number returned by tun device */
729 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
730 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
731 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
732 syslog (LOG_ERR
, "Can't get flags\n");
733 /* Push arp module to if_fd */
734 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
735 syslog (LOG_ERR
, "Can't push ARP module (2)");
737 /* Push arp module to ip_fd */
738 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
739 syslog (LOG_ERR
, "I_POP failed\n");
740 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
741 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
743 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
745 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
747 /* Set ifname to arp */
748 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
749 strioc_if
.ic_timout
= 0;
750 strioc_if
.ic_len
= sizeof(ifr
);
751 strioc_if
.ic_dp
= (char *)&ifr
;
752 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
753 syslog (LOG_ERR
, "Can't set ifname to arp\n");
756 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
757 syslog(LOG_ERR
, "Can't link TAP device to IP");
761 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
762 syslog (LOG_ERR
, "Can't link TAP device to ARP");
766 memset(&ifr
, 0x0, sizeof(ifr
));
767 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
768 ifr
.lifr_ip_muxid
= ip_muxid
;
769 ifr
.lifr_arp_muxid
= arp_muxid
;
771 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
773 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
774 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
775 syslog (LOG_ERR
, "Can't set multiplexor id");
778 snprintf(dev
, dev_size
, "tap%d", ppa
);
782 static int tap_open(char *ifname
, int ifname_size
)
786 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
787 fprintf(stderr
, "Cannot allocate TAP device\n");
790 pstrcpy(ifname
, ifname_size
, dev
);
791 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
795 static int tap_open(char *ifname
, int ifname_size
)
800 TFR(fd
= open("/dev/net/tun", O_RDWR
));
802 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
805 memset(&ifr
, 0, sizeof(ifr
));
806 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
807 if (ifname
[0] != '\0')
808 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
810 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
811 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
813 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
817 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
818 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
823 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
829 /* try to launch network script */
833 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
834 for (i
= 0; i
< open_max
; i
++)
835 if (i
!= STDIN_FILENO
&&
836 i
!= STDOUT_FILENO
&&
837 i
!= STDERR_FILENO
&&
842 *parg
++ = (char *)setup_script
;
843 *parg
++ = (char *)ifname
;
845 execv(setup_script
, args
);
848 while (waitpid(pid
, &status
, 0) != pid
);
849 if (!WIFEXITED(status
) ||
850 WEXITSTATUS(status
) != 0) {
851 fprintf(stderr
, "%s: could not launch network script\n",
859 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
860 const char *setup_script
, const char *down_script
)
867 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
870 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
874 if (!setup_script
|| !strcmp(setup_script
, "no"))
876 if (setup_script
[0] != '\0') {
877 if (launch_script(setup_script
, ifname
, fd
))
880 s
= net_tap_fd_init(vlan
, fd
);
883 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
884 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
885 if (down_script
&& strcmp(down_script
, "no"))
886 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
892 #if defined(CONFIG_VDE)
893 typedef struct VDEState
{
898 static void vde_to_qemu(void *opaque
)
900 VDEState
*s
= opaque
;
904 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
906 qemu_send_packet(s
->vc
, buf
, size
);
910 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
912 VDEState
*s
= opaque
;
915 ret
= vde_send(s
->vde
, buf
, size
, 0);
916 if (ret
< 0 && errno
== EINTR
) {
923 static int net_vde_init(VLANState
*vlan
, const char *sock
, int port
,
924 const char *group
, int mode
)
927 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
928 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
930 struct vde_open_args args
= {
936 s
= qemu_mallocz(sizeof(VDEState
));
939 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
944 s
->vc
= qemu_new_vlan_client(vlan
, vde_from_qemu
, NULL
, s
);
945 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
946 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "vde: sock=%s fd=%d",
947 sock
, vde_datafd(s
->vde
));
952 /* network connection */
953 typedef struct NetSocketState
{
956 int state
; /* 0 = getting length, 1 = getting data */
960 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
963 typedef struct NetSocketListenState
{
966 } NetSocketListenState
;
968 /* XXX: we consider we can send the whole packet without blocking */
969 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
971 NetSocketState
*s
= opaque
;
975 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
976 send_all(s
->fd
, buf
, size
);
979 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
981 NetSocketState
*s
= opaque
;
982 sendto(s
->fd
, buf
, size
, 0,
983 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
986 static void net_socket_send(void *opaque
)
988 NetSocketState
*s
= opaque
;
993 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
995 err
= socket_error();
996 if (err
!= EWOULDBLOCK
)
998 } else if (size
== 0) {
999 /* end of connection */
1001 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1007 /* reassemble a packet from the network */
1013 memcpy(s
->buf
+ s
->index
, buf
, l
);
1017 if (s
->index
== 4) {
1019 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1025 l
= s
->packet_len
- s
->index
;
1028 memcpy(s
->buf
+ s
->index
, buf
, l
);
1032 if (s
->index
>= s
->packet_len
) {
1033 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1042 static void net_socket_send_dgram(void *opaque
)
1044 NetSocketState
*s
= opaque
;
1047 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1051 /* end of connection */
1052 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1055 qemu_send_packet(s
->vc
, s
->buf
, size
);
1058 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1063 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1064 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1065 inet_ntoa(mcastaddr
->sin_addr
),
1066 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1070 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1072 perror("socket(PF_INET, SOCK_DGRAM)");
1077 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1078 (const char *)&val
, sizeof(val
));
1080 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1084 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1090 /* Add host to multicast group */
1091 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1092 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1094 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1095 (const char *)&imr
, sizeof(struct ip_mreq
));
1097 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1101 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1103 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1104 (const char *)&val
, sizeof(val
));
1106 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1110 socket_set_nonblock(fd
);
1118 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
1121 struct sockaddr_in saddr
;
1123 socklen_t saddr_len
;
1126 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1127 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1128 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1132 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1134 if (saddr
.sin_addr
.s_addr
==0) {
1135 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1139 /* clone dgram socket */
1140 newfd
= net_socket_mcast_create(&saddr
);
1142 /* error already reported by net_socket_mcast_create() */
1146 /* clone newfd to fd, close newfd */
1151 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1152 fd
, strerror(errno
));
1157 s
= qemu_mallocz(sizeof(NetSocketState
));
1162 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
1163 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1165 /* mcast: save bound address as dst */
1166 if (is_connected
) s
->dgram_dst
=saddr
;
1168 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1169 "socket: fd=%d (%s mcast=%s:%d)",
1170 fd
, is_connected
? "cloned" : "",
1171 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1175 static void net_socket_connect(void *opaque
)
1177 NetSocketState
*s
= opaque
;
1178 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1181 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
1185 s
= qemu_mallocz(sizeof(NetSocketState
));
1189 s
->vc
= qemu_new_vlan_client(vlan
,
1190 net_socket_receive
, NULL
, s
);
1191 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1192 "socket: fd=%d", fd
);
1194 net_socket_connect(s
);
1196 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1201 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
1204 int so_type
=-1, optlen
=sizeof(so_type
);
1206 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1207 (socklen_t
*)&optlen
)< 0) {
1208 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1213 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
1215 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
1217 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1218 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1219 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
1224 static void net_socket_accept(void *opaque
)
1226 NetSocketListenState
*s
= opaque
;
1228 struct sockaddr_in saddr
;
1233 len
= sizeof(saddr
);
1234 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1235 if (fd
< 0 && errno
!= EINTR
) {
1237 } else if (fd
>= 0) {
1241 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
1245 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1246 "socket: connection from %s:%d",
1247 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1251 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
1253 NetSocketListenState
*s
;
1255 struct sockaddr_in saddr
;
1257 if (parse_host_port(&saddr
, host_str
) < 0)
1260 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1264 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1269 socket_set_nonblock(fd
);
1271 /* allow fast reuse */
1273 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1275 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1280 ret
= listen(fd
, 0);
1287 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1291 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
1294 int fd
, connected
, ret
, err
;
1295 struct sockaddr_in saddr
;
1297 if (parse_host_port(&saddr
, host_str
) < 0)
1300 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1305 socket_set_nonblock(fd
);
1309 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1311 err
= socket_error();
1312 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1313 } else if (err
== EINPROGRESS
) {
1316 } else if (err
== WSAEALREADY
) {
1329 s
= net_socket_fd_init(vlan
, fd
, connected
);
1332 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1333 "socket: connect to %s:%d",
1334 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1338 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
1342 struct sockaddr_in saddr
;
1344 if (parse_host_port(&saddr
, host_str
) < 0)
1348 fd
= net_socket_mcast_create(&saddr
);
1352 s
= net_socket_fd_init(vlan
, fd
, 0);
1356 s
->dgram_dst
= saddr
;
1358 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1359 "socket: mcast=%s:%d",
1360 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1365 /* find or alloc a new VLAN */
1366 VLANState
*qemu_find_vlan(int id
)
1368 VLANState
**pvlan
, *vlan
;
1369 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1373 vlan
= qemu_mallocz(sizeof(VLANState
));
1378 pvlan
= &first_vlan
;
1379 while (*pvlan
!= NULL
)
1380 pvlan
= &(*pvlan
)->next
;
1385 int net_client_init(const char *device
, const char *p
)
1392 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1393 vlan_id
= strtol(buf
, NULL
, 0);
1395 vlan
= qemu_find_vlan(vlan_id
);
1397 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1400 if (!strcmp(device
, "nic")) {
1404 if (nb_nics
>= MAX_NICS
) {
1405 fprintf(stderr
, "Too Many NICs\n");
1408 nd
= &nd_table
[nb_nics
];
1409 macaddr
= nd
->macaddr
;
1415 macaddr
[5] = 0x56 + nb_nics
;
1417 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1418 if (parse_macaddr(macaddr
, buf
) < 0) {
1419 fprintf(stderr
, "invalid syntax for ethernet address\n");
1423 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1424 nd
->model
= strdup(buf
);
1428 vlan
->nb_guest_devs
++;
1431 if (!strcmp(device
, "none")) {
1432 /* does nothing. It is needed to signal that no network cards
1437 if (!strcmp(device
, "user")) {
1438 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1439 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1441 vlan
->nb_host_devs
++;
1442 ret
= net_slirp_init(vlan
);
1446 if (!strcmp(device
, "tap")) {
1448 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1449 fprintf(stderr
, "tap: no interface name\n");
1452 vlan
->nb_host_devs
++;
1453 ret
= tap_win32_init(vlan
, ifname
);
1456 if (!strcmp(device
, "tap")) {
1458 char setup_script
[1024], down_script
[1024];
1460 vlan
->nb_host_devs
++;
1461 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1462 fd
= strtol(buf
, NULL
, 0);
1463 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1465 if (net_tap_fd_init(vlan
, fd
))
1468 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1471 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1472 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1474 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1475 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1477 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
1481 if (!strcmp(device
, "socket")) {
1482 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1484 fd
= strtol(buf
, NULL
, 0);
1486 if (net_socket_fd_init(vlan
, fd
, 1))
1488 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1489 ret
= net_socket_listen_init(vlan
, buf
);
1490 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1491 ret
= net_socket_connect_init(vlan
, buf
);
1492 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1493 ret
= net_socket_mcast_init(vlan
, buf
);
1495 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1498 vlan
->nb_host_devs
++;
1501 if (!strcmp(device
, "vde")) {
1502 char vde_sock
[1024], vde_group
[512];
1503 int vde_port
, vde_mode
;
1504 vlan
->nb_host_devs
++;
1505 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1508 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1509 vde_port
= strtol(buf
, NULL
, 10);
1513 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1514 vde_group
[0] = '\0';
1516 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1517 vde_mode
= strtol(buf
, NULL
, 8);
1521 ret
= net_vde_init(vlan
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1525 fprintf(stderr
, "Unknown network device: %s\n", device
);
1529 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1535 int net_client_parse(const char *str
)
1543 while (*p
!= '\0' && *p
!= ',') {
1544 if ((q
- device
) < sizeof(device
) - 1)
1552 return net_client_init(device
, p
);
1555 void do_info_network(void)
1558 VLANClientState
*vc
;
1560 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1561 term_printf("VLAN %d devices:\n", vlan
->id
);
1562 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1563 term_printf(" %s\n", vc
->info_str
);
1567 void net_cleanup(void)
1571 #if !defined(_WIN32)
1572 /* close network clients */
1573 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1574 VLANClientState
*vc
;
1576 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1577 if (vc
->fd_read
== tap_receive
) {
1579 TAPState
*s
= vc
->opaque
;
1581 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
1583 launch_script(s
->down_script
, ifname
, s
->fd
);
1585 #if defined(CONFIG_VDE)
1586 if (vc
->fd_read
== vde_from_qemu
) {
1587 VDEState
*s
= vc
->opaque
;
1596 void net_client_check(void)
1600 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1601 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1603 if (vlan
->nb_guest_devs
== 0)
1604 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1605 if (vlan
->nb_host_devs
== 0)
1607 "Warning: vlan %d is not connected to host network\n",