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 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
124 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
126 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
128 #define SMBD_COMMAND "/usr/sbin/smbd"
131 static VLANState
*first_vlan
;
133 /***********************************************************/
134 /* network device redirectors */
136 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
137 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
141 for(i
=0;i
<size
;i
+=16) {
145 fprintf(f
, "%08x ", i
);
148 fprintf(f
, " %02x", buf
[i
+j
]);
155 if (c
< ' ' || c
> '~')
164 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
171 offset
= strtol(p
, &last_char
, 0);
172 if (0 == errno
&& '\0' == *last_char
&&
173 offset
>= 0 && offset
<= 0xFFFFFF) {
174 macaddr
[3] = (offset
& 0xFF0000) >> 16;
175 macaddr
[4] = (offset
& 0xFF00) >> 8;
176 macaddr
[5] = offset
& 0xFF;
179 for(i
= 0; i
< 6; i
++) {
180 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
185 if (*p
!= ':' && *p
!= '-')
196 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
207 if (len
> buf_size
- 1)
216 int parse_host_src_port(struct sockaddr_in
*haddr
,
217 struct sockaddr_in
*saddr
,
218 const char *input_str
)
220 char *str
= strdup(input_str
);
221 char *host_str
= str
;
223 const char *src_str2
;
227 * Chop off any extra arguments at the end of the string which
228 * would start with a comma, then fill in the src port information
229 * if it was provided else use the "any address" and "any port".
231 if ((ptr
= strchr(str
,',')))
234 if ((src_str
= strchr(input_str
,'@'))) {
239 if (parse_host_port(haddr
, host_str
) < 0)
243 if (!src_str
|| *src_str
== '\0')
246 if (parse_host_port(saddr
, src_str2
) < 0)
257 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
265 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
267 saddr
->sin_family
= AF_INET
;
268 if (buf
[0] == '\0') {
269 saddr
->sin_addr
.s_addr
= 0;
271 if (isdigit(buf
[0])) {
272 if (!inet_aton(buf
, &saddr
->sin_addr
))
275 if ((he
= gethostbyname(buf
)) == NULL
)
277 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
280 port
= strtol(p
, (char **)&r
, 0);
283 saddr
->sin_port
= htons(port
);
288 int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
293 len
= MIN(108, strlen(str
));
294 p
= strchr(str
, ',');
296 len
= MIN(len
, p
- str
);
298 memset(uaddr
, 0, sizeof(*uaddr
));
300 uaddr
->sun_family
= AF_UNIX
;
301 memcpy(uaddr
->sun_path
, str
, len
);
307 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
308 IOReadHandler
*fd_read
,
309 IOCanRWHandler
*fd_can_read
,
312 VLANClientState
*vc
, **pvc
;
313 vc
= qemu_mallocz(sizeof(VLANClientState
));
316 vc
->fd_read
= fd_read
;
317 vc
->fd_can_read
= fd_can_read
;
322 pvc
= &vlan
->first_client
;
329 void qemu_del_vlan_client(VLANClientState
*vc
)
331 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
342 int qemu_can_send_packet(VLANClientState
*vc1
)
344 VLANState
*vlan
= vc1
->vlan
;
347 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
349 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
356 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
358 VLANState
*vlan
= vc1
->vlan
;
362 printf("vlan %d send:\n", vlan
->id
);
363 hex_dump(stdout
, buf
, size
);
365 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
367 vc
->fd_read(vc
->opaque
, buf
, size
);
372 #if defined(CONFIG_SLIRP)
374 /* slirp network adapter */
376 static int slirp_inited
;
377 static VLANClientState
*slirp_vc
;
379 int slirp_can_output(void)
381 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
384 void slirp_output(const uint8_t *pkt
, int pkt_len
)
387 printf("slirp output:\n");
388 hex_dump(stdout
, pkt
, pkt_len
);
392 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
395 int slirp_is_inited(void)
400 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
403 printf("slirp input:\n");
404 hex_dump(stdout
, buf
, size
);
406 slirp_input(buf
, size
);
409 static int net_slirp_init(VLANState
*vlan
)
415 slirp_vc
= qemu_new_vlan_client(vlan
,
416 slirp_receive
, NULL
, NULL
);
417 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
421 void net_slirp_redir(const char *redir_str
)
426 struct in_addr guest_addr
;
427 int host_port
, guest_port
;
435 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
437 if (!strcmp(buf
, "tcp")) {
439 } else if (!strcmp(buf
, "udp")) {
445 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
447 host_port
= strtol(buf
, &r
, 0);
451 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
453 if (buf
[0] == '\0') {
454 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
456 if (!inet_aton(buf
, &guest_addr
))
459 guest_port
= strtol(p
, &r
, 0);
463 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
464 fprintf(stderr
, "qemu: could not set up redirection\n");
469 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
475 static char smb_dir
[1024];
477 static void erase_dir(char *dir_name
)
483 /* erase all the files in the directory */
484 if ((d
= opendir(dir_name
)) != 0) {
489 if (strcmp(de
->d_name
, ".") != 0 &&
490 strcmp(de
->d_name
, "..") != 0) {
491 snprintf(filename
, sizeof(filename
), "%s/%s",
492 smb_dir
, de
->d_name
);
493 if (unlink(filename
) != 0) /* is it a directory? */
502 /* automatic user mode samba server configuration */
503 static void smb_exit(void)
508 /* automatic user mode samba server configuration */
509 void net_slirp_smb(const char *exported_dir
)
512 char smb_cmdline
[1024];
520 /* XXX: better tmp dir construction */
521 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
522 if (mkdir(smb_dir
, 0700) < 0) {
523 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
526 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
528 f
= fopen(smb_conf
, "w");
530 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
537 "socket address=127.0.0.1\n"
539 "lock directory=%s\n"
540 "log file=%s/log.smbd\n"
541 "smb passwd file=%s/smbpasswd\n"
557 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
558 SMBD_COMMAND
, smb_conf
);
560 slirp_add_exec(0, smb_cmdline
, 4, 139);
563 #endif /* !defined(_WIN32) */
564 void do_info_slirp(void)
569 #endif /* CONFIG_SLIRP */
573 typedef struct TAPState
{
576 char down_script
[1024];
579 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
581 TAPState
*s
= opaque
;
584 ret
= write(s
->fd
, buf
, size
);
585 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
592 static void tap_send(void *opaque
)
594 TAPState
*s
= opaque
;
601 sbuf
.maxlen
= sizeof(buf
);
603 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
605 size
= read(s
->fd
, buf
, sizeof(buf
));
608 qemu_send_packet(s
->vc
, buf
, size
);
614 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
618 s
= qemu_mallocz(sizeof(TAPState
));
622 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
623 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
624 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
628 #if defined (_BSD) || defined (__FreeBSD_kernel__)
629 static int tap_open(char *ifname
, int ifname_size
)
635 TFR(fd
= open("/dev/tap", O_RDWR
));
637 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
642 dev
= devname(s
.st_rdev
, S_IFCHR
);
643 pstrcpy(ifname
, ifname_size
, dev
);
645 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
648 #elif defined(__sun__)
649 #define TUNNEWPPA (('T'<<16) | 0x0001)
651 * Allocate TAP device, returns opened fd.
652 * Stores dev name in the first arg(must be large enough).
654 int tap_alloc(char *dev
, size_t dev_size
)
656 int tap_fd
, if_fd
, ppa
= -1;
657 static int ip_fd
= 0;
660 static int arp_fd
= 0;
661 int ip_muxid
, arp_muxid
;
662 struct strioctl strioc_if
, strioc_ppa
;
663 int link_type
= I_PLINK
;;
665 char actual_name
[32] = "";
667 memset(&ifr
, 0x0, sizeof(ifr
));
671 while( *ptr
&& !isdigit((int)*ptr
) ) ptr
++;
675 /* Check if IP device was opened */
679 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
681 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
685 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
687 syslog(LOG_ERR
, "Can't open /dev/tap");
691 /* Assign a new PPA and get its unit number. */
692 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
693 strioc_ppa
.ic_timout
= 0;
694 strioc_ppa
.ic_len
= sizeof(ppa
);
695 strioc_ppa
.ic_dp
= (char *)&ppa
;
696 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
697 syslog (LOG_ERR
, "Can't assign new interface");
699 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
701 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
704 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
705 syslog(LOG_ERR
, "Can't push IP module");
709 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
710 syslog(LOG_ERR
, "Can't get flags\n");
712 snprintf (actual_name
, 32, "tap%d", ppa
);
713 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
716 /* Assign ppa according to the unit number returned by tun device */
718 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
719 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
720 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
721 syslog (LOG_ERR
, "Can't get flags\n");
722 /* Push arp module to if_fd */
723 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
724 syslog (LOG_ERR
, "Can't push ARP module (2)");
726 /* Push arp module to ip_fd */
727 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
728 syslog (LOG_ERR
, "I_POP failed\n");
729 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
730 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
732 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
734 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
736 /* Set ifname to arp */
737 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
738 strioc_if
.ic_timout
= 0;
739 strioc_if
.ic_len
= sizeof(ifr
);
740 strioc_if
.ic_dp
= (char *)&ifr
;
741 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
742 syslog (LOG_ERR
, "Can't set ifname to arp\n");
745 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
746 syslog(LOG_ERR
, "Can't link TAP device to IP");
750 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
751 syslog (LOG_ERR
, "Can't link TAP device to ARP");
755 memset(&ifr
, 0x0, sizeof(ifr
));
756 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
757 ifr
.lifr_ip_muxid
= ip_muxid
;
758 ifr
.lifr_arp_muxid
= arp_muxid
;
760 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
762 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
763 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
764 syslog (LOG_ERR
, "Can't set multiplexor id");
767 snprintf(dev
, dev_size
, "tap%d", ppa
);
771 static int tap_open(char *ifname
, int ifname_size
)
775 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
776 fprintf(stderr
, "Cannot allocate TAP device\n");
779 pstrcpy(ifname
, ifname_size
, dev
);
780 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
784 static int tap_open(char *ifname
, int ifname_size
)
789 TFR(fd
= open("/dev/net/tun", O_RDWR
));
791 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
794 memset(&ifr
, 0, sizeof(ifr
));
795 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
796 if (ifname
[0] != '\0')
797 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
799 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
800 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
802 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
806 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
807 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
812 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
818 /* try to launch network script */
822 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
823 for (i
= 0; i
< open_max
; i
++)
824 if (i
!= STDIN_FILENO
&&
825 i
!= STDOUT_FILENO
&&
826 i
!= STDERR_FILENO
&&
831 *parg
++ = (char *)setup_script
;
832 *parg
++ = (char *)ifname
;
834 execv(setup_script
, args
);
837 while (waitpid(pid
, &status
, 0) != pid
);
838 if (!WIFEXITED(status
) ||
839 WEXITSTATUS(status
) != 0) {
840 fprintf(stderr
, "%s: could not launch network script\n",
848 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
849 const char *setup_script
, const char *down_script
)
856 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
859 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
863 if (!setup_script
|| !strcmp(setup_script
, "no"))
865 if (setup_script
[0] != '\0') {
866 if (launch_script(setup_script
, ifname
, fd
))
869 s
= net_tap_fd_init(vlan
, fd
);
872 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
873 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
874 if (down_script
&& strcmp(down_script
, "no"))
875 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
881 #if defined(CONFIG_VDE)
882 typedef struct VDEState
{
887 static void vde_to_qemu(void *opaque
)
889 VDEState
*s
= opaque
;
893 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
895 qemu_send_packet(s
->vc
, buf
, size
);
899 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
901 VDEState
*s
= opaque
;
904 ret
= vde_send(s
->vde
, buf
, size
, 0);
905 if (ret
< 0 && errno
== EINTR
) {
912 static int net_vde_init(VLANState
*vlan
, const char *sock
, int port
,
913 const char *group
, int mode
)
916 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
917 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
919 struct vde_open_args args
= {
925 s
= qemu_mallocz(sizeof(VDEState
));
928 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
933 s
->vc
= qemu_new_vlan_client(vlan
, vde_from_qemu
, NULL
, s
);
934 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
935 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "vde: sock=%s fd=%d",
936 sock
, vde_datafd(s
->vde
));
941 /* network connection */
942 typedef struct NetSocketState
{
945 int state
; /* 0 = getting length, 1 = getting data */
949 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
952 typedef struct NetSocketListenState
{
955 } NetSocketListenState
;
957 /* XXX: we consider we can send the whole packet without blocking */
958 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
960 NetSocketState
*s
= opaque
;
964 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
965 send_all(s
->fd
, buf
, size
);
968 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
970 NetSocketState
*s
= opaque
;
971 sendto(s
->fd
, buf
, size
, 0,
972 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
975 static void net_socket_send(void *opaque
)
977 NetSocketState
*s
= opaque
;
982 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
984 err
= socket_error();
985 if (err
!= EWOULDBLOCK
)
987 } else if (size
== 0) {
988 /* end of connection */
990 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
996 /* reassemble a packet from the network */
1002 memcpy(s
->buf
+ s
->index
, buf
, l
);
1006 if (s
->index
== 4) {
1008 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1014 l
= s
->packet_len
- s
->index
;
1017 memcpy(s
->buf
+ s
->index
, buf
, l
);
1021 if (s
->index
>= s
->packet_len
) {
1022 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1031 static void net_socket_send_dgram(void *opaque
)
1033 NetSocketState
*s
= opaque
;
1036 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1040 /* end of connection */
1041 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1044 qemu_send_packet(s
->vc
, s
->buf
, size
);
1047 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1052 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1053 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1054 inet_ntoa(mcastaddr
->sin_addr
),
1055 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1059 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1061 perror("socket(PF_INET, SOCK_DGRAM)");
1066 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1067 (const char *)&val
, sizeof(val
));
1069 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1073 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1079 /* Add host to multicast group */
1080 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1081 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1083 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1084 (const char *)&imr
, sizeof(struct ip_mreq
));
1086 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1090 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1092 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1093 (const char *)&val
, sizeof(val
));
1095 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1099 socket_set_nonblock(fd
);
1107 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
1110 struct sockaddr_in saddr
;
1112 socklen_t saddr_len
;
1115 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1116 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1117 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1121 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1123 if (saddr
.sin_addr
.s_addr
==0) {
1124 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1128 /* clone dgram socket */
1129 newfd
= net_socket_mcast_create(&saddr
);
1131 /* error already reported by net_socket_mcast_create() */
1135 /* clone newfd to fd, close newfd */
1140 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1141 fd
, strerror(errno
));
1146 s
= qemu_mallocz(sizeof(NetSocketState
));
1151 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
1152 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1154 /* mcast: save bound address as dst */
1155 if (is_connected
) s
->dgram_dst
=saddr
;
1157 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1158 "socket: fd=%d (%s mcast=%s:%d)",
1159 fd
, is_connected
? "cloned" : "",
1160 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1164 static void net_socket_connect(void *opaque
)
1166 NetSocketState
*s
= opaque
;
1167 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1170 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
1174 s
= qemu_mallocz(sizeof(NetSocketState
));
1178 s
->vc
= qemu_new_vlan_client(vlan
,
1179 net_socket_receive
, NULL
, s
);
1180 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1181 "socket: fd=%d", fd
);
1183 net_socket_connect(s
);
1185 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1190 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
1193 int so_type
=-1, optlen
=sizeof(so_type
);
1195 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1196 (socklen_t
*)&optlen
)< 0) {
1197 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1202 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
1204 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
1206 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1207 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1208 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
1213 static void net_socket_accept(void *opaque
)
1215 NetSocketListenState
*s
= opaque
;
1217 struct sockaddr_in saddr
;
1222 len
= sizeof(saddr
);
1223 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1224 if (fd
< 0 && errno
!= EINTR
) {
1226 } else if (fd
>= 0) {
1230 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
1234 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1235 "socket: connection from %s:%d",
1236 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1240 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
1242 NetSocketListenState
*s
;
1244 struct sockaddr_in saddr
;
1246 if (parse_host_port(&saddr
, host_str
) < 0)
1249 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1253 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1258 socket_set_nonblock(fd
);
1260 /* allow fast reuse */
1262 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1264 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1269 ret
= listen(fd
, 0);
1276 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1280 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
1283 int fd
, connected
, ret
, err
;
1284 struct sockaddr_in saddr
;
1286 if (parse_host_port(&saddr
, host_str
) < 0)
1289 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1294 socket_set_nonblock(fd
);
1298 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1300 err
= socket_error();
1301 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1302 } else if (err
== EINPROGRESS
) {
1305 } else if (err
== WSAEALREADY
) {
1318 s
= net_socket_fd_init(vlan
, fd
, connected
);
1321 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1322 "socket: connect to %s:%d",
1323 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1327 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
1331 struct sockaddr_in saddr
;
1333 if (parse_host_port(&saddr
, host_str
) < 0)
1337 fd
= net_socket_mcast_create(&saddr
);
1341 s
= net_socket_fd_init(vlan
, fd
, 0);
1345 s
->dgram_dst
= saddr
;
1347 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1348 "socket: mcast=%s:%d",
1349 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1354 /* find or alloc a new VLAN */
1355 VLANState
*qemu_find_vlan(int id
)
1357 VLANState
**pvlan
, *vlan
;
1358 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1362 vlan
= qemu_mallocz(sizeof(VLANState
));
1367 pvlan
= &first_vlan
;
1368 while (*pvlan
!= NULL
)
1369 pvlan
= &(*pvlan
)->next
;
1374 int net_client_init(const char *device
, const char *p
)
1381 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1382 vlan_id
= strtol(buf
, NULL
, 0);
1384 vlan
= qemu_find_vlan(vlan_id
);
1386 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1389 if (!strcmp(device
, "nic")) {
1393 if (nb_nics
>= MAX_NICS
) {
1394 fprintf(stderr
, "Too Many NICs\n");
1397 nd
= &nd_table
[nb_nics
];
1398 macaddr
= nd
->macaddr
;
1404 macaddr
[5] = 0x56 + nb_nics
;
1406 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1407 if (parse_macaddr(macaddr
, buf
) < 0) {
1408 fprintf(stderr
, "invalid syntax for ethernet address\n");
1412 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1413 nd
->model
= strdup(buf
);
1417 vlan
->nb_guest_devs
++;
1420 if (!strcmp(device
, "none")) {
1421 /* does nothing. It is needed to signal that no network cards
1426 if (!strcmp(device
, "user")) {
1427 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1428 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1430 vlan
->nb_host_devs
++;
1431 ret
= net_slirp_init(vlan
);
1435 if (!strcmp(device
, "tap")) {
1437 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1438 fprintf(stderr
, "tap: no interface name\n");
1441 vlan
->nb_host_devs
++;
1442 ret
= tap_win32_init(vlan
, ifname
);
1445 if (!strcmp(device
, "tap")) {
1447 char setup_script
[1024], down_script
[1024];
1449 vlan
->nb_host_devs
++;
1450 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1451 fd
= strtol(buf
, NULL
, 0);
1452 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1454 if (net_tap_fd_init(vlan
, fd
))
1457 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1460 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1461 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1463 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1464 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1466 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
1470 if (!strcmp(device
, "socket")) {
1471 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1473 fd
= strtol(buf
, NULL
, 0);
1475 if (net_socket_fd_init(vlan
, fd
, 1))
1477 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1478 ret
= net_socket_listen_init(vlan
, buf
);
1479 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1480 ret
= net_socket_connect_init(vlan
, buf
);
1481 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1482 ret
= net_socket_mcast_init(vlan
, buf
);
1484 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1487 vlan
->nb_host_devs
++;
1490 if (!strcmp(device
, "vde")) {
1491 char vde_sock
[1024], vde_group
[512];
1492 int vde_port
, vde_mode
;
1493 vlan
->nb_host_devs
++;
1494 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1497 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1498 vde_port
= strtol(buf
, NULL
, 10);
1502 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1503 vde_group
[0] = '\0';
1505 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1506 vde_mode
= strtol(buf
, NULL
, 8);
1510 ret
= net_vde_init(vlan
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1514 fprintf(stderr
, "Unknown network device: %s\n", device
);
1518 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1524 int net_client_parse(const char *str
)
1532 while (*p
!= '\0' && *p
!= ',') {
1533 if ((q
- device
) < sizeof(device
) - 1)
1541 return net_client_init(device
, p
);
1544 void do_info_network(void)
1547 VLANClientState
*vc
;
1549 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1550 term_printf("VLAN %d devices:\n", vlan
->id
);
1551 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1552 term_printf(" %s\n", vc
->info_str
);
1556 void net_cleanup(void)
1560 #if !defined(_WIN32)
1561 /* close network clients */
1562 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1563 VLANClientState
*vc
;
1565 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1566 if (vc
->fd_read
== tap_receive
) {
1568 TAPState
*s
= vc
->opaque
;
1570 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
1572 launch_script(s
->down_script
, ifname
, s
->fd
);
1574 #if defined(CONFIG_VDE)
1575 if (vc
->fd_read
== vde_from_qemu
) {
1576 VDEState
*s
= vc
->opaque
;
1585 void net_client_check(void)
1589 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1590 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1592 if (vlan
->nb_guest_devs
== 0)
1593 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1594 if (vlan
->nb_host_devs
== 0)
1596 "Warning: vlan %d is not connected to host network\n",