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 (qemu_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
);
287 #if !defined(_WIN32) && 0
288 static 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 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
375 uint8_t buffer
[4096];
379 for (i
= 0; i
< iovcnt
; i
++) {
382 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
383 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
387 vc
->fd_read(vc
->opaque
, buffer
, offset
);
392 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
395 VLANState
*vlan
= vc1
->vlan
;
399 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
406 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
407 else if (vc
->fd_read
)
408 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
410 max_len
= MAX(max_len
, len
);
416 #if defined(CONFIG_SLIRP)
418 /* slirp network adapter */
420 static int slirp_inited
;
421 static VLANClientState
*slirp_vc
;
423 int slirp_can_output(void)
425 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
428 void slirp_output(const uint8_t *pkt
, int pkt_len
)
431 printf("slirp output:\n");
432 hex_dump(stdout
, pkt
, pkt_len
);
436 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
439 int slirp_is_inited(void)
444 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
447 printf("slirp input:\n");
448 hex_dump(stdout
, buf
, size
);
450 slirp_input(buf
, size
);
453 static int net_slirp_init(VLANState
*vlan
)
459 slirp_vc
= qemu_new_vlan_client(vlan
,
460 slirp_receive
, NULL
, NULL
);
461 snprintf(slirp_vc
->info_str
, sizeof(slirp_vc
->info_str
), "user redirector");
465 void net_slirp_redir(const char *redir_str
)
470 struct in_addr guest_addr
;
471 int host_port
, guest_port
;
479 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
481 if (!strcmp(buf
, "tcp")) {
483 } else if (!strcmp(buf
, "udp")) {
489 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
491 host_port
= strtol(buf
, &r
, 0);
495 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
497 if (buf
[0] == '\0') {
498 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
500 if (!inet_aton(buf
, &guest_addr
))
503 guest_port
= strtol(p
, &r
, 0);
507 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
508 fprintf(stderr
, "qemu: could not set up redirection\n");
513 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
519 static char smb_dir
[1024];
521 static void erase_dir(char *dir_name
)
527 /* erase all the files in the directory */
528 if ((d
= opendir(dir_name
)) != 0) {
533 if (strcmp(de
->d_name
, ".") != 0 &&
534 strcmp(de
->d_name
, "..") != 0) {
535 snprintf(filename
, sizeof(filename
), "%s/%s",
536 smb_dir
, de
->d_name
);
537 if (unlink(filename
) != 0) /* is it a directory? */
546 /* automatic user mode samba server configuration */
547 static void smb_exit(void)
552 /* automatic user mode samba server configuration */
553 void net_slirp_smb(const char *exported_dir
)
556 char smb_cmdline
[1024];
564 /* XXX: better tmp dir construction */
565 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
566 if (mkdir(smb_dir
, 0700) < 0) {
567 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
570 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
572 f
= fopen(smb_conf
, "w");
574 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
581 "socket address=127.0.0.1\n"
583 "lock directory=%s\n"
584 "log file=%s/log.smbd\n"
585 "smb passwd file=%s/smbpasswd\n"
601 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
602 SMBD_COMMAND
, smb_conf
);
604 slirp_add_exec(0, smb_cmdline
, 4, 139);
607 #endif /* !defined(_WIN32) */
608 void do_info_slirp(void)
613 #endif /* CONFIG_SLIRP */
617 typedef struct TAPState
{
620 char down_script
[1024];
623 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
625 TAPState
*s
= opaque
;
628 ret
= write(s
->fd
, buf
, size
);
629 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
636 static void tap_send(void *opaque
)
638 TAPState
*s
= opaque
;
645 sbuf
.maxlen
= sizeof(buf
);
647 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
649 size
= read(s
->fd
, buf
, sizeof(buf
));
652 qemu_send_packet(s
->vc
, buf
, size
);
658 static TAPState
*net_tap_fd_init(VLANState
*vlan
, int fd
)
662 s
= qemu_mallocz(sizeof(TAPState
));
666 s
->vc
= qemu_new_vlan_client(vlan
, tap_receive
, NULL
, s
);
667 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
668 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "tap: fd=%d", fd
);
672 #if defined (_BSD) || defined (__FreeBSD_kernel__)
673 static int tap_open(char *ifname
, int ifname_size
)
679 TFR(fd
= open("/dev/tap", O_RDWR
));
681 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
686 dev
= devname(s
.st_rdev
, S_IFCHR
);
687 pstrcpy(ifname
, ifname_size
, dev
);
689 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
692 #elif defined(__sun__)
693 #define TUNNEWPPA (('T'<<16) | 0x0001)
695 * Allocate TAP device, returns opened fd.
696 * Stores dev name in the first arg(must be large enough).
698 int tap_alloc(char *dev
, size_t dev_size
)
700 int tap_fd
, if_fd
, ppa
= -1;
701 static int ip_fd
= 0;
704 static int arp_fd
= 0;
705 int ip_muxid
, arp_muxid
;
706 struct strioctl strioc_if
, strioc_ppa
;
707 int link_type
= I_PLINK
;;
709 char actual_name
[32] = "";
711 memset(&ifr
, 0x0, sizeof(ifr
));
715 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
719 /* Check if IP device was opened */
723 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
725 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
729 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
731 syslog(LOG_ERR
, "Can't open /dev/tap");
735 /* Assign a new PPA and get its unit number. */
736 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
737 strioc_ppa
.ic_timout
= 0;
738 strioc_ppa
.ic_len
= sizeof(ppa
);
739 strioc_ppa
.ic_dp
= (char *)&ppa
;
740 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
741 syslog (LOG_ERR
, "Can't assign new interface");
743 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
745 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
748 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
749 syslog(LOG_ERR
, "Can't push IP module");
753 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
754 syslog(LOG_ERR
, "Can't get flags\n");
756 snprintf (actual_name
, 32, "tap%d", ppa
);
757 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
760 /* Assign ppa according to the unit number returned by tun device */
762 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
763 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
764 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
765 syslog (LOG_ERR
, "Can't get flags\n");
766 /* Push arp module to if_fd */
767 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
768 syslog (LOG_ERR
, "Can't push ARP module (2)");
770 /* Push arp module to ip_fd */
771 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
772 syslog (LOG_ERR
, "I_POP failed\n");
773 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
774 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
776 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
778 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
780 /* Set ifname to arp */
781 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
782 strioc_if
.ic_timout
= 0;
783 strioc_if
.ic_len
= sizeof(ifr
);
784 strioc_if
.ic_dp
= (char *)&ifr
;
785 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
786 syslog (LOG_ERR
, "Can't set ifname to arp\n");
789 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
790 syslog(LOG_ERR
, "Can't link TAP device to IP");
794 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
795 syslog (LOG_ERR
, "Can't link TAP device to ARP");
799 memset(&ifr
, 0x0, sizeof(ifr
));
800 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
801 ifr
.lifr_ip_muxid
= ip_muxid
;
802 ifr
.lifr_arp_muxid
= arp_muxid
;
804 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
806 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
807 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
808 syslog (LOG_ERR
, "Can't set multiplexor id");
811 snprintf(dev
, dev_size
, "tap%d", ppa
);
815 static int tap_open(char *ifname
, int ifname_size
)
819 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
820 fprintf(stderr
, "Cannot allocate TAP device\n");
823 pstrcpy(ifname
, ifname_size
, dev
);
824 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
828 static int tap_open(char *ifname
, int ifname_size
)
830 fprintf (stderr
, "no tap on AIX\n");
834 static int tap_open(char *ifname
, int ifname_size
)
839 TFR(fd
= open("/dev/net/tun", O_RDWR
));
841 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
844 memset(&ifr
, 0, sizeof(ifr
));
845 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
846 if (ifname
[0] != '\0')
847 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
849 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
850 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
852 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
856 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
857 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
862 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
868 /* try to launch network script */
872 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
873 for (i
= 0; i
< open_max
; i
++)
874 if (i
!= STDIN_FILENO
&&
875 i
!= STDOUT_FILENO
&&
876 i
!= STDERR_FILENO
&&
881 *parg
++ = (char *)setup_script
;
882 *parg
++ = (char *)ifname
;
884 execv(setup_script
, args
);
887 while (waitpid(pid
, &status
, 0) != pid
);
888 if (!WIFEXITED(status
) ||
889 WEXITSTATUS(status
) != 0) {
890 fprintf(stderr
, "%s: could not launch network script\n",
898 static int net_tap_init(VLANState
*vlan
, const char *ifname1
,
899 const char *setup_script
, const char *down_script
)
906 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
909 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
913 if (!setup_script
|| !strcmp(setup_script
, "no"))
915 if (setup_script
[0] != '\0') {
916 if (launch_script(setup_script
, ifname
, fd
))
919 s
= net_tap_fd_init(vlan
, fd
);
922 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
923 "tap: ifname=%s setup_script=%s", ifname
, setup_script
);
924 if (down_script
&& strcmp(down_script
, "no"))
925 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
931 #if defined(CONFIG_VDE)
932 typedef struct VDEState
{
937 static void vde_to_qemu(void *opaque
)
939 VDEState
*s
= opaque
;
943 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
945 qemu_send_packet(s
->vc
, buf
, size
);
949 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
951 VDEState
*s
= opaque
;
954 ret
= vde_send(s
->vde
, buf
, size
, 0);
955 if (ret
< 0 && errno
== EINTR
) {
962 static int net_vde_init(VLANState
*vlan
, const char *sock
, int port
,
963 const char *group
, int mode
)
966 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
967 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
969 struct vde_open_args args
= {
975 s
= qemu_mallocz(sizeof(VDEState
));
978 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
983 s
->vc
= qemu_new_vlan_client(vlan
, vde_from_qemu
, NULL
, s
);
984 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
985 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "vde: sock=%s fd=%d",
986 sock
, vde_datafd(s
->vde
));
991 /* network connection */
992 typedef struct NetSocketState
{
995 int state
; /* 0 = getting length, 1 = getting data */
999 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1002 typedef struct NetSocketListenState
{
1005 } NetSocketListenState
;
1007 /* XXX: we consider we can send the whole packet without blocking */
1008 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1010 NetSocketState
*s
= opaque
;
1014 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1015 send_all(s
->fd
, buf
, size
);
1018 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1020 NetSocketState
*s
= opaque
;
1021 sendto(s
->fd
, buf
, size
, 0,
1022 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1025 static void net_socket_send(void *opaque
)
1027 NetSocketState
*s
= opaque
;
1032 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1034 err
= socket_error();
1035 if (err
!= EWOULDBLOCK
)
1037 } else if (size
== 0) {
1038 /* end of connection */
1040 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1046 /* reassemble a packet from the network */
1052 memcpy(s
->buf
+ s
->index
, buf
, l
);
1056 if (s
->index
== 4) {
1058 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1064 l
= s
->packet_len
- s
->index
;
1067 memcpy(s
->buf
+ s
->index
, buf
, l
);
1071 if (s
->index
>= s
->packet_len
) {
1072 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1081 static void net_socket_send_dgram(void *opaque
)
1083 NetSocketState
*s
= opaque
;
1086 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1090 /* end of connection */
1091 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1094 qemu_send_packet(s
->vc
, s
->buf
, size
);
1097 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1102 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1103 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1104 inet_ntoa(mcastaddr
->sin_addr
),
1105 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1109 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1111 perror("socket(PF_INET, SOCK_DGRAM)");
1116 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1117 (const char *)&val
, sizeof(val
));
1119 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1123 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1129 /* Add host to multicast group */
1130 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1131 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1133 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1134 (const char *)&imr
, sizeof(struct ip_mreq
));
1136 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1140 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1142 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1143 (const char *)&val
, sizeof(val
));
1145 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1149 socket_set_nonblock(fd
);
1157 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
, int fd
,
1160 struct sockaddr_in saddr
;
1162 socklen_t saddr_len
;
1165 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1166 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1167 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1171 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1173 if (saddr
.sin_addr
.s_addr
==0) {
1174 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1178 /* clone dgram socket */
1179 newfd
= net_socket_mcast_create(&saddr
);
1181 /* error already reported by net_socket_mcast_create() */
1185 /* clone newfd to fd, close newfd */
1190 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1191 fd
, strerror(errno
));
1196 s
= qemu_mallocz(sizeof(NetSocketState
));
1201 s
->vc
= qemu_new_vlan_client(vlan
, net_socket_receive_dgram
, NULL
, s
);
1202 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1204 /* mcast: save bound address as dst */
1205 if (is_connected
) s
->dgram_dst
=saddr
;
1207 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1208 "socket: fd=%d (%s mcast=%s:%d)",
1209 fd
, is_connected
? "cloned" : "",
1210 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1214 static void net_socket_connect(void *opaque
)
1216 NetSocketState
*s
= opaque
;
1217 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1220 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
, int fd
,
1224 s
= qemu_mallocz(sizeof(NetSocketState
));
1228 s
->vc
= qemu_new_vlan_client(vlan
,
1229 net_socket_receive
, NULL
, s
);
1230 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1231 "socket: fd=%d", fd
);
1233 net_socket_connect(s
);
1235 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1240 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
, int fd
,
1243 int so_type
=-1, optlen
=sizeof(so_type
);
1245 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1246 (socklen_t
*)&optlen
)< 0) {
1247 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1252 return net_socket_fd_init_dgram(vlan
, fd
, is_connected
);
1254 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
1256 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1257 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1258 return net_socket_fd_init_stream(vlan
, fd
, is_connected
);
1263 static void net_socket_accept(void *opaque
)
1265 NetSocketListenState
*s
= opaque
;
1267 struct sockaddr_in saddr
;
1272 len
= sizeof(saddr
);
1273 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1274 if (fd
< 0 && errno
!= EINTR
) {
1276 } else if (fd
>= 0) {
1280 s1
= net_socket_fd_init(s
->vlan
, fd
, 1);
1284 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1285 "socket: connection from %s:%d",
1286 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1290 static int net_socket_listen_init(VLANState
*vlan
, const char *host_str
)
1292 NetSocketListenState
*s
;
1294 struct sockaddr_in saddr
;
1296 if (parse_host_port(&saddr
, host_str
) < 0)
1299 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1303 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1308 socket_set_nonblock(fd
);
1310 /* allow fast reuse */
1312 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1314 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1319 ret
= listen(fd
, 0);
1326 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1330 static int net_socket_connect_init(VLANState
*vlan
, const char *host_str
)
1333 int fd
, connected
, ret
, err
;
1334 struct sockaddr_in saddr
;
1336 if (parse_host_port(&saddr
, host_str
) < 0)
1339 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1344 socket_set_nonblock(fd
);
1348 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1350 err
= socket_error();
1351 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1352 } else if (err
== EINPROGRESS
) {
1355 } else if (err
== WSAEALREADY
) {
1368 s
= net_socket_fd_init(vlan
, fd
, connected
);
1371 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1372 "socket: connect to %s:%d",
1373 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1377 static int net_socket_mcast_init(VLANState
*vlan
, const char *host_str
)
1381 struct sockaddr_in saddr
;
1383 if (parse_host_port(&saddr
, host_str
) < 0)
1387 fd
= net_socket_mcast_create(&saddr
);
1391 s
= net_socket_fd_init(vlan
, fd
, 0);
1395 s
->dgram_dst
= saddr
;
1397 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1398 "socket: mcast=%s:%d",
1399 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1404 /* find or alloc a new VLAN */
1405 VLANState
*qemu_find_vlan(int id
)
1407 VLANState
**pvlan
, *vlan
;
1408 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1412 vlan
= qemu_mallocz(sizeof(VLANState
));
1417 pvlan
= &first_vlan
;
1418 while (*pvlan
!= NULL
)
1419 pvlan
= &(*pvlan
)->next
;
1424 int net_client_init(const char *device
, const char *p
)
1431 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1432 vlan_id
= strtol(buf
, NULL
, 0);
1434 vlan
= qemu_find_vlan(vlan_id
);
1436 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1439 if (!strcmp(device
, "nic")) {
1443 if (nb_nics
>= MAX_NICS
) {
1444 fprintf(stderr
, "Too Many NICs\n");
1447 nd
= &nd_table
[nb_nics
];
1448 macaddr
= nd
->macaddr
;
1454 macaddr
[5] = 0x56 + nb_nics
;
1456 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1457 if (parse_macaddr(macaddr
, buf
) < 0) {
1458 fprintf(stderr
, "invalid syntax for ethernet address\n");
1462 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1463 nd
->model
= strdup(buf
);
1467 vlan
->nb_guest_devs
++;
1470 if (!strcmp(device
, "none")) {
1471 /* does nothing. It is needed to signal that no network cards
1476 if (!strcmp(device
, "user")) {
1477 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1478 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1480 vlan
->nb_host_devs
++;
1481 ret
= net_slirp_init(vlan
);
1485 if (!strcmp(device
, "tap")) {
1487 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1488 fprintf(stderr
, "tap: no interface name\n");
1491 vlan
->nb_host_devs
++;
1492 ret
= tap_win32_init(vlan
, ifname
);
1494 #elif defined (_AIX)
1496 if (!strcmp(device
, "tap")) {
1498 char setup_script
[1024], down_script
[1024];
1500 vlan
->nb_host_devs
++;
1501 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1502 fd
= strtol(buf
, NULL
, 0);
1503 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1505 if (net_tap_fd_init(vlan
, fd
))
1508 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1511 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1512 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1514 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1515 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1517 ret
= net_tap_init(vlan
, ifname
, setup_script
, down_script
);
1521 if (!strcmp(device
, "socket")) {
1522 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1524 fd
= strtol(buf
, NULL
, 0);
1526 if (net_socket_fd_init(vlan
, fd
, 1))
1528 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1529 ret
= net_socket_listen_init(vlan
, buf
);
1530 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1531 ret
= net_socket_connect_init(vlan
, buf
);
1532 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1533 ret
= net_socket_mcast_init(vlan
, buf
);
1535 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1538 vlan
->nb_host_devs
++;
1541 if (!strcmp(device
, "vde")) {
1542 char vde_sock
[1024], vde_group
[512];
1543 int vde_port
, vde_mode
;
1544 vlan
->nb_host_devs
++;
1545 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1548 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1549 vde_port
= strtol(buf
, NULL
, 10);
1553 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1554 vde_group
[0] = '\0';
1556 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1557 vde_mode
= strtol(buf
, NULL
, 8);
1561 ret
= net_vde_init(vlan
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1565 fprintf(stderr
, "Unknown network device: %s\n", device
);
1569 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1575 int net_client_parse(const char *str
)
1583 while (*p
!= '\0' && *p
!= ',') {
1584 if ((q
- device
) < sizeof(device
) - 1)
1592 return net_client_init(device
, p
);
1595 void do_info_network(void)
1598 VLANClientState
*vc
;
1600 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1601 term_printf("VLAN %d devices:\n", vlan
->id
);
1602 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1603 term_printf(" %s\n", vc
->info_str
);
1607 void net_cleanup(void)
1611 #if !defined(_WIN32)
1612 /* close network clients */
1613 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1614 VLANClientState
*vc
;
1616 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1617 if (vc
->fd_read
== tap_receive
) {
1619 TAPState
*s
= vc
->opaque
;
1621 if (sscanf(vc
->info_str
, "tap: ifname=%63s ", ifname
) == 1 &&
1623 launch_script(s
->down_script
, ifname
, s
->fd
);
1625 #if defined(CONFIG_VDE)
1626 if (vc
->fd_read
== vde_from_qemu
) {
1627 VDEState
*s
= vc
->opaque
;
1636 void net_client_check(void)
1640 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1641 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1643 if (vlan
->nb_guest_devs
== 0)
1644 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1645 if (vlan
->nb_host_devs
== 0)
1647 "Warning: vlan %d is not connected to host network\n",