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/socket.h>
58 #include <netinet/in.h>
61 #include <sys/select.h>
62 #include <arpa/inet.h>
65 #if !defined(__APPLE__) && !defined(__OpenBSD__)
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
76 #include <linux/if_tun.h>
79 #include <linux/rtc.h>
81 /* For the benefit of older linux systems which don't supply it,
82 we use a local copy of hpet.h. */
83 /* #include <linux/hpet.h> */
86 #include <linux/ppdev.h>
87 #include <linux/parport.h>
91 #include <sys/ethernet.h>
92 #include <sys/sockio.h>
93 #include <netinet/arp.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_icmp.h> // must come after ip.h
98 #include <netinet/udp.h>
99 #include <netinet/tcp.h>
107 #include "qemu_socket.h"
109 #if defined(CONFIG_SLIRP)
110 #include "libslirp.h"
113 #if defined(__OpenBSD__)
117 #if defined(CONFIG_VDE)
118 #include <libvdeplug.h>
123 #include <sys/timeb.h>
124 #include <mmsystem.h>
125 #define getopt_long_only getopt_long
126 #define memalign(align, size) malloc(size)
129 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
130 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
132 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
134 #define SMBD_COMMAND "/usr/sbin/smbd"
137 static VLANState
*first_vlan
;
139 /***********************************************************/
140 /* network device redirectors */
142 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
143 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
147 for(i
=0;i
<size
;i
+=16) {
151 fprintf(f
, "%08x ", i
);
154 fprintf(f
, " %02x", buf
[i
+j
]);
161 if (c
< ' ' || c
> '~')
170 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
177 offset
= strtol(p
, &last_char
, 0);
178 if (0 == errno
&& '\0' == *last_char
&&
179 offset
>= 0 && offset
<= 0xFFFFFF) {
180 macaddr
[3] = (offset
& 0xFF0000) >> 16;
181 macaddr
[4] = (offset
& 0xFF00) >> 8;
182 macaddr
[5] = offset
& 0xFF;
185 for(i
= 0; i
< 6; i
++) {
186 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
191 if (*p
!= ':' && *p
!= '-')
202 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
213 if (len
> buf_size
- 1)
222 int parse_host_src_port(struct sockaddr_in
*haddr
,
223 struct sockaddr_in
*saddr
,
224 const char *input_str
)
226 char *str
= strdup(input_str
);
227 char *host_str
= str
;
229 const char *src_str2
;
233 * Chop off any extra arguments at the end of the string which
234 * would start with a comma, then fill in the src port information
235 * if it was provided else use the "any address" and "any port".
237 if ((ptr
= strchr(str
,',')))
240 if ((src_str
= strchr(input_str
,'@'))) {
245 if (parse_host_port(haddr
, host_str
) < 0)
249 if (!src_str
|| *src_str
== '\0')
252 if (parse_host_port(saddr
, src_str2
) < 0)
263 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
271 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
273 saddr
->sin_family
= AF_INET
;
274 if (buf
[0] == '\0') {
275 saddr
->sin_addr
.s_addr
= 0;
277 if (isdigit(buf
[0])) {
278 if (!inet_aton(buf
, &saddr
->sin_addr
))
281 if ((he
= gethostbyname(buf
)) == NULL
)
283 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
286 port
= strtol(p
, (char **)&r
, 0);
289 saddr
->sin_port
= htons(port
);
294 int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
299 len
= MIN(108, strlen(str
));
300 p
= strchr(str
, ',');
302 len
= MIN(len
, p
- str
);
304 memset(uaddr
, 0, sizeof(*uaddr
));
306 uaddr
->sun_family
= AF_UNIX
;
307 memcpy(uaddr
->sun_path
, str
, len
);
313 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
314 IOReadHandler
*fd_read
,
315 IOCanRWHandler
*fd_can_read
,
318 VLANClientState
*vc
, **pvc
;
319 vc
= qemu_mallocz(sizeof(VLANClientState
));
322 vc
->fd_read
= fd_read
;
323 vc
->fd_can_read
= fd_can_read
;
328 pvc
= &vlan
->first_client
;
335 void qemu_del_vlan_client(VLANClientState
*vc
)
337 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
348 int qemu_can_send_packet(VLANClientState
*vc1
)
350 VLANState
*vlan
= vc1
->vlan
;
353 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
355 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
362 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
364 VLANState
*vlan
= vc1
->vlan
;
368 printf("vlan %d send:\n", vlan
->id
);
369 hex_dump(stdout
, buf
, size
);
371 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
373 vc
->fd_read(vc
->opaque
, buf
, size
);
378 #if defined(CONFIG_SLIRP)
380 /* slirp network adapter */
382 static int slirp_inited
;
383 static VLANClientState
*slirp_vc
;
385 int slirp_can_output(void)
387 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
390 void slirp_output(const uint8_t *pkt
, int pkt_len
)
393 printf("slirp output:\n");
394 hex_dump(stdout
, pkt
, pkt_len
);
398 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
401 int slirp_is_inited(void)
406 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
409 printf("slirp input:\n");
410 hex_dump(stdout
, buf
, size
);
412 slirp_input(buf
, size
);
415 static int net_slirp_init(VLANState
*vlan
)
421 slirp_vc
= qemu_new_vlan_client(vlan
,
422 slirp_receive
, NULL
, NULL
);
423 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
427 void net_slirp_redir(const char *redir_str
)
432 struct in_addr guest_addr
;
433 int host_port
, guest_port
;
441 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
443 if (!strcmp(buf
, "tcp")) {
445 } else if (!strcmp(buf
, "udp")) {
451 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
453 host_port
= strtol(buf
, &r
, 0);
457 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
459 if (buf
[0] == '\0') {
460 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
462 if (!inet_aton(buf
, &guest_addr
))
465 guest_port
= strtol(p
, &r
, 0);
469 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
470 fprintf(stderr
, "qemu: could not set up redirection\n");
475 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
481 static char smb_dir
[1024];
483 static void erase_dir(char *dir_name
)
489 /* erase all the files in the directory */
490 if ((d
= opendir(dir_name
)) != 0) {
495 if (strcmp(de
->d_name
, ".") != 0 &&
496 strcmp(de
->d_name
, "..") != 0) {
497 snprintf(filename
, sizeof(filename
), "%s/%s",
498 smb_dir
, de
->d_name
);
499 if (unlink(filename
) != 0) /* is it a directory? */
508 /* automatic user mode samba server configuration */
509 static void smb_exit(void)
514 /* automatic user mode samba server configuration */
515 void net_slirp_smb(const char *exported_dir
)
518 char smb_cmdline
[1024];
526 /* XXX: better tmp dir construction */
527 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
528 if (mkdir(smb_dir
, 0700) < 0) {
529 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
532 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
534 f
= fopen(smb_conf
, "w");
536 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
543 "socket address=127.0.0.1\n"
545 "lock directory=%s\n"
546 "log file=%s/log.smbd\n"
547 "smb passwd file=%s/smbpasswd\n"
563 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
564 SMBD_COMMAND
, smb_conf
);
566 slirp_add_exec(0, smb_cmdline
, 4, 139);
569 #endif /* !defined(_WIN32) */
570 void do_info_slirp(void)
575 #endif /* CONFIG_SLIRP */
579 typedef struct TAPState
{
582 char down_script
[1024];
585 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
587 TAPState
*s
= opaque
;
590 ret
= write(s
->fd
, buf
, size
);
591 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
598 static void tap_send(void *opaque
)
600 TAPState
*s
= opaque
;
607 sbuf
.maxlen
= sizeof(buf
);
609 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
611 size
= read(s
->fd
, buf
, sizeof(buf
));
614 qemu_send_packet(s
->vc
, buf
, size
);
620 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
624 s
= qemu_mallocz(sizeof(TAPState
));
628 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
629 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
630 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
634 #if defined (_BSD) || defined (__FreeBSD_kernel__)
635 static int tap_open(char *ifname
, int ifname_size
)
641 TFR(fd
= open("/dev/tap", O_RDWR
));
643 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
648 dev
= devname(s
.st_rdev
, S_IFCHR
);
649 pstrcpy(ifname
, ifname_size
, dev
);
651 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
654 #elif defined(__sun__)
655 #define TUNNEWPPA (('T'<<16) | 0x0001)
657 * Allocate TAP device, returns opened fd.
658 * Stores dev name in the first arg(must be large enough).
660 int tap_alloc(char *dev
, size_t dev_size
)
662 int tap_fd
, if_fd
, ppa
= -1;
663 static int ip_fd
= 0;
666 static int arp_fd
= 0;
667 int ip_muxid
, arp_muxid
;
668 struct strioctl strioc_if
, strioc_ppa
;
669 int link_type
= I_PLINK
;;
671 char actual_name
[32] = "";
673 memset(&ifr
, 0x0, sizeof(ifr
));
677 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
681 /* Check if IP device was opened */
685 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
687 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
691 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
693 syslog(LOG_ERR
, "Can't open /dev/tap");
697 /* Assign a new PPA and get its unit number. */
698 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
699 strioc_ppa
.ic_timout
= 0;
700 strioc_ppa
.ic_len
= sizeof(ppa
);
701 strioc_ppa
.ic_dp
= (char *)&ppa
;
702 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
703 syslog (LOG_ERR
, "Can't assign new interface");
705 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
707 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
710 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
711 syslog(LOG_ERR
, "Can't push IP module");
715 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
716 syslog(LOG_ERR
, "Can't get flags\n");
718 snprintf (actual_name
, 32, "tap%d", ppa
);
719 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
722 /* Assign ppa according to the unit number returned by tun device */
724 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
725 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
726 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
727 syslog (LOG_ERR
, "Can't get flags\n");
728 /* Push arp module to if_fd */
729 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
730 syslog (LOG_ERR
, "Can't push ARP module (2)");
732 /* Push arp module to ip_fd */
733 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
734 syslog (LOG_ERR
, "I_POP failed\n");
735 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
736 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
738 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
740 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
742 /* Set ifname to arp */
743 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
744 strioc_if
.ic_timout
= 0;
745 strioc_if
.ic_len
= sizeof(ifr
);
746 strioc_if
.ic_dp
= (char *)&ifr
;
747 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
748 syslog (LOG_ERR
, "Can't set ifname to arp\n");
751 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
752 syslog(LOG_ERR
, "Can't link TAP device to IP");
756 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
757 syslog (LOG_ERR
, "Can't link TAP device to ARP");
761 memset(&ifr
, 0x0, sizeof(ifr
));
762 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
763 ifr
.lifr_ip_muxid
= ip_muxid
;
764 ifr
.lifr_arp_muxid
= arp_muxid
;
766 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
768 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
769 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
770 syslog (LOG_ERR
, "Can't set multiplexor id");
773 snprintf(dev
, dev_size
, "tap%d", ppa
);
777 static int tap_open(char *ifname
, int ifname_size
)
781 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
782 fprintf(stderr
, "Cannot allocate TAP device\n");
785 pstrcpy(ifname
, ifname_size
, dev
);
786 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
790 static int tap_open(char *ifname
, int ifname_size
)
795 TFR(fd
= open("/dev/net/tun", O_RDWR
));
797 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
800 memset(&ifr
, 0, sizeof(ifr
));
801 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
802 if (ifname
[0] != '\0')
803 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
805 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
806 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
808 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
812 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
813 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
818 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
824 /* try to launch network script */
828 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
829 for (i
= 0; i
< open_max
; i
++)
830 if (i
!= STDIN_FILENO
&&
831 i
!= STDOUT_FILENO
&&
832 i
!= STDERR_FILENO
&&
837 *parg
++ = (char *)setup_script
;
838 *parg
++ = (char *)ifname
;
840 execv(setup_script
, args
);
843 while (waitpid(pid
, &status
, 0) != pid
);
844 if (!WIFEXITED(status
) ||
845 WEXITSTATUS(status
) != 0) {
846 fprintf(stderr
, "%s: could not launch network script\n",
854 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
855 const char *setup_script
, const char *down_script
)
862 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
865 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
869 if (!setup_script
|| !strcmp(setup_script
, "no"))
871 if (setup_script
[0] != '\0') {
872 if (launch_script(setup_script
, ifname
, fd
))
875 s
= net_tap_fd_init(vlan
, fd
);
878 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
879 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
880 if (down_script
&& strcmp(down_script
, "no"))
881 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
887 #if defined(CONFIG_VDE)
888 typedef struct VDEState
{
893 static void vde_to_qemu(void *opaque
)
895 VDEState
*s
= opaque
;
899 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
901 qemu_send_packet(s
->vc
, buf
, size
);
905 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
907 VDEState
*s
= opaque
;
910 ret
= vde_send(s
->vde
, buf
, size
, 0);
911 if (ret
< 0 && errno
== EINTR
) {
918 static int net_vde_init(VLANState
*vlan
, const char *sock
, int port
,
919 const char *group
, int mode
)
922 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
923 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
925 struct vde_open_args args
= {
931 s
= qemu_mallocz(sizeof(VDEState
));
934 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
939 s
->vc
= qemu_new_vlan_client(vlan
, vde_from_qemu
, NULL
, s
);
940 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
941 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "vde: sock=%s fd=%d",
942 sock
, vde_datafd(s
->vde
));
947 /* network connection */
948 typedef struct NetSocketState
{
951 int state
; /* 0 = getting length, 1 = getting data */
955 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
958 typedef struct NetSocketListenState
{
961 } NetSocketListenState
;
963 /* XXX: we consider we can send the whole packet without blocking */
964 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
966 NetSocketState
*s
= opaque
;
970 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
971 send_all(s
->fd
, buf
, size
);
974 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
976 NetSocketState
*s
= opaque
;
977 sendto(s
->fd
, buf
, size
, 0,
978 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
981 static void net_socket_send(void *opaque
)
983 NetSocketState
*s
= opaque
;
988 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
990 err
= socket_error();
991 if (err
!= EWOULDBLOCK
)
993 } else if (size
== 0) {
994 /* end of connection */
996 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1002 /* reassemble a packet from the network */
1008 memcpy(s
->buf
+ s
->index
, buf
, l
);
1012 if (s
->index
== 4) {
1014 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1020 l
= s
->packet_len
- s
->index
;
1023 memcpy(s
->buf
+ s
->index
, buf
, l
);
1027 if (s
->index
>= s
->packet_len
) {
1028 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1037 static void net_socket_send_dgram(void *opaque
)
1039 NetSocketState
*s
= opaque
;
1042 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1046 /* end of connection */
1047 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1050 qemu_send_packet(s
->vc
, s
->buf
, size
);
1053 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1058 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1059 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1060 inet_ntoa(mcastaddr
->sin_addr
),
1061 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1065 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1067 perror("socket(PF_INET, SOCK_DGRAM)");
1072 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1073 (const char *)&val
, sizeof(val
));
1075 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1079 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1085 /* Add host to multicast group */
1086 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1087 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1089 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1090 (const char *)&imr
, sizeof(struct ip_mreq
));
1092 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1096 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1098 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1099 (const char *)&val
, sizeof(val
));
1101 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1105 socket_set_nonblock(fd
);
1113 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
1116 struct sockaddr_in saddr
;
1118 socklen_t saddr_len
;
1121 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1122 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1123 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1127 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1129 if (saddr
.sin_addr
.s_addr
==0) {
1130 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1134 /* clone dgram socket */
1135 newfd
= net_socket_mcast_create(&saddr
);
1137 /* error already reported by net_socket_mcast_create() */
1141 /* clone newfd to fd, close newfd */
1146 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1147 fd
, strerror(errno
));
1152 s
= qemu_mallocz(sizeof(NetSocketState
));
1157 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
1158 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1160 /* mcast: save bound address as dst */
1161 if (is_connected
) s
->dgram_dst
=saddr
;
1163 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1164 "socket: fd=%d (%s mcast=%s:%d)",
1165 fd
, is_connected
? "cloned" : "",
1166 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1170 static void net_socket_connect(void *opaque
)
1172 NetSocketState
*s
= opaque
;
1173 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1176 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
1180 s
= qemu_mallocz(sizeof(NetSocketState
));
1184 s
->vc
= qemu_new_vlan_client(vlan
,
1185 net_socket_receive
, NULL
, s
);
1186 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1187 "socket: fd=%d", fd
);
1189 net_socket_connect(s
);
1191 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1196 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
1199 int so_type
=-1, optlen
=sizeof(so_type
);
1201 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1202 (socklen_t
*)&optlen
)< 0) {
1203 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1208 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
1210 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
1212 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1213 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1214 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
1219 static void net_socket_accept(void *opaque
)
1221 NetSocketListenState
*s
= opaque
;
1223 struct sockaddr_in saddr
;
1228 len
= sizeof(saddr
);
1229 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1230 if (fd
< 0 && errno
!= EINTR
) {
1232 } else if (fd
>= 0) {
1236 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
1240 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1241 "socket: connection from %s:%d",
1242 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1246 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
1248 NetSocketListenState
*s
;
1250 struct sockaddr_in saddr
;
1252 if (parse_host_port(&saddr
, host_str
) < 0)
1255 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1259 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1264 socket_set_nonblock(fd
);
1266 /* allow fast reuse */
1268 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1270 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1275 ret
= listen(fd
, 0);
1282 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1286 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
1289 int fd
, connected
, ret
, err
;
1290 struct sockaddr_in saddr
;
1292 if (parse_host_port(&saddr
, host_str
) < 0)
1295 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1300 socket_set_nonblock(fd
);
1304 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1306 err
= socket_error();
1307 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1308 } else if (err
== EINPROGRESS
) {
1311 } else if (err
== WSAEALREADY
) {
1324 s
= net_socket_fd_init(vlan
, fd
, connected
);
1327 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1328 "socket: connect to %s:%d",
1329 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1333 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
1337 struct sockaddr_in saddr
;
1339 if (parse_host_port(&saddr
, host_str
) < 0)
1343 fd
= net_socket_mcast_create(&saddr
);
1347 s
= net_socket_fd_init(vlan
, fd
, 0);
1351 s
->dgram_dst
= saddr
;
1353 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1354 "socket: mcast=%s:%d",
1355 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1360 /* find or alloc a new VLAN */
1361 VLANState
*qemu_find_vlan(int id
)
1363 VLANState
**pvlan
, *vlan
;
1364 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1368 vlan
= qemu_mallocz(sizeof(VLANState
));
1373 pvlan
= &first_vlan
;
1374 while (*pvlan
!= NULL
)
1375 pvlan
= &(*pvlan
)->next
;
1380 int net_client_init(const char *device
, const char *p
)
1387 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1388 vlan_id
= strtol(buf
, NULL
, 0);
1390 vlan
= qemu_find_vlan(vlan_id
);
1392 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1395 if (!strcmp(device
, "nic")) {
1399 if (nb_nics
>= MAX_NICS
) {
1400 fprintf(stderr
, "Too Many NICs\n");
1403 nd
= &nd_table
[nb_nics
];
1404 macaddr
= nd
->macaddr
;
1410 macaddr
[5] = 0x56 + nb_nics
;
1412 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1413 if (parse_macaddr(macaddr
, buf
) < 0) {
1414 fprintf(stderr
, "invalid syntax for ethernet address\n");
1418 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1419 nd
->model
= strdup(buf
);
1423 vlan
->nb_guest_devs
++;
1426 if (!strcmp(device
, "none")) {
1427 /* does nothing. It is needed to signal that no network cards
1432 if (!strcmp(device
, "user")) {
1433 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1434 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1436 vlan
->nb_host_devs
++;
1437 ret
= net_slirp_init(vlan
);
1441 if (!strcmp(device
, "tap")) {
1443 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1444 fprintf(stderr
, "tap: no interface name\n");
1447 vlan
->nb_host_devs
++;
1448 ret
= tap_win32_init(vlan
, ifname
);
1451 if (!strcmp(device
, "tap")) {
1453 char setup_script
[1024], down_script
[1024];
1455 vlan
->nb_host_devs
++;
1456 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1457 fd
= strtol(buf
, NULL
, 0);
1458 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1460 if (net_tap_fd_init(vlan
, fd
))
1463 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1466 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1467 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1469 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1470 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1472 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
1476 if (!strcmp(device
, "socket")) {
1477 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1479 fd
= strtol(buf
, NULL
, 0);
1481 if (net_socket_fd_init(vlan
, fd
, 1))
1483 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1484 ret
= net_socket_listen_init(vlan
, buf
);
1485 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1486 ret
= net_socket_connect_init(vlan
, buf
);
1487 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1488 ret
= net_socket_mcast_init(vlan
, buf
);
1490 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1493 vlan
->nb_host_devs
++;
1496 if (!strcmp(device
, "vde")) {
1497 char vde_sock
[1024], vde_group
[512];
1498 int vde_port
, vde_mode
;
1499 vlan
->nb_host_devs
++;
1500 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1503 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1504 vde_port
= strtol(buf
, NULL
, 10);
1508 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1509 vde_group
[0] = '\0';
1511 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1512 vde_mode
= strtol(buf
, NULL
, 8);
1516 ret
= net_vde_init(vlan
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1520 fprintf(stderr
, "Unknown network device: %s\n", device
);
1524 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1530 int net_client_parse(const char *str
)
1538 while (*p
!= '\0' && *p
!= ',') {
1539 if ((q
- device
) < sizeof(device
) - 1)
1547 return net_client_init(device
, p
);
1550 void do_info_network(void)
1553 VLANClientState
*vc
;
1555 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1556 term_printf("VLAN %d devices:\n", vlan
->id
);
1557 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1558 term_printf(" %s\n", vc
->info_str
);
1562 void net_cleanup(void)
1566 #if !defined(_WIN32)
1567 /* close network clients */
1568 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1569 VLANClientState
*vc
;
1571 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1572 if (vc
->fd_read
== tap_receive
) {
1574 TAPState
*s
= vc
->opaque
;
1576 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
1578 launch_script(s
->down_script
, ifname
, s
->fd
);
1580 #if defined(CONFIG_VDE)
1581 if (vc
->fd_read
== vde_from_qemu
) {
1582 VDEState
*s
= vc
->opaque
;
1591 void net_client_check(void)
1595 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1596 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1598 if (vlan
->nb_guest_devs
== 0)
1599 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1600 if (vlan
->nb_host_devs
== 0)
1602 "Warning: vlan %d is not connected to host network\n",