4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
30 #include "audio/audio.h"
41 #include <sys/times.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
51 #include <net/if_tap.h>
54 #include <linux/if_tun.h>
56 #include <arpa/inet.h>
59 #include <sys/select.h>
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 #include <freebsd/stdlib.h>
73 #include <linux/rtc.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #include "qemu_socket.h"
103 #if defined(CONFIG_SLIRP)
104 #include "libslirp.h"
107 #if defined(__OpenBSD__)
111 #if defined(CONFIG_VDE)
112 #include <libvdeplug.h>
117 #include <sys/timeb.h>
118 #include <mmsystem.h>
119 #define getopt_long_only getopt_long
120 #define memalign(align, size) malloc(size)
123 static VLANState
*first_vlan
;
125 /***********************************************************/
126 /* network device redirectors */
128 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
129 static void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
133 for(i
=0;i
<size
;i
+=16) {
137 fprintf(f
, "%08x ", i
);
140 fprintf(f
, " %02x", buf
[i
+j
]);
147 if (c
< ' ' || c
> '~')
156 static int parse_macaddr(uint8_t *macaddr
, const char *p
)
163 offset
= strtol(p
, &last_char
, 0);
164 if (0 == errno
&& '\0' == *last_char
&&
165 offset
>= 0 && offset
<= 0xFFFFFF) {
166 macaddr
[3] = (offset
& 0xFF0000) >> 16;
167 macaddr
[4] = (offset
& 0xFF00) >> 8;
168 macaddr
[5] = offset
& 0xFF;
171 for(i
= 0; i
< 6; i
++) {
172 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
177 if (*p
!= ':' && *p
!= '-')
188 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
199 if (len
> buf_size
- 1)
208 int parse_host_src_port(struct sockaddr_in
*haddr
,
209 struct sockaddr_in
*saddr
,
210 const char *input_str
)
212 char *str
= strdup(input_str
);
213 char *host_str
= str
;
215 const char *src_str2
;
219 * Chop off any extra arguments at the end of the string which
220 * would start with a comma, then fill in the src port information
221 * if it was provided else use the "any address" and "any port".
223 if ((ptr
= strchr(str
,',')))
226 if ((src_str
= strchr(input_str
,'@'))) {
231 if (parse_host_port(haddr
, host_str
) < 0)
235 if (!src_str
|| *src_str
== '\0')
238 if (parse_host_port(saddr
, src_str2
) < 0)
249 int parse_host_port(struct sockaddr_in
*saddr
, const char *str
)
257 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
259 saddr
->sin_family
= AF_INET
;
260 if (buf
[0] == '\0') {
261 saddr
->sin_addr
.s_addr
= 0;
263 if (qemu_isdigit(buf
[0])) {
264 if (!inet_aton(buf
, &saddr
->sin_addr
))
267 if ((he
= gethostbyname(buf
)) == NULL
)
269 saddr
->sin_addr
= *(struct in_addr
*)he
->h_addr
;
272 port
= strtol(p
, (char **)&r
, 0);
275 saddr
->sin_port
= htons(port
);
279 #if !defined(_WIN32) && 0
280 static int parse_unix_path(struct sockaddr_un
*uaddr
, const char *str
)
285 len
= MIN(108, strlen(str
));
286 p
= strchr(str
, ',');
288 len
= MIN(len
, p
- str
);
290 memset(uaddr
, 0, sizeof(*uaddr
));
292 uaddr
->sun_family
= AF_UNIX
;
293 memcpy(uaddr
->sun_path
, str
, len
);
299 void qemu_format_nic_info_str(VLANClientState
*vc
, uint8_t macaddr
[6])
301 snprintf(vc
->info_str
, sizeof(vc
->info_str
),
302 "macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
303 macaddr
[0], macaddr
[1], macaddr
[2],
304 macaddr
[3], macaddr
[4], macaddr
[5]);
307 static char *assign_name(VLANClientState
*vc1
, const char *model
)
313 for (vlan
= first_vlan
; vlan
; vlan
= vlan
->next
) {
316 for (vc
= vlan
->first_client
; vc
; vc
= vc
->next
)
317 if (vc
!= vc1
&& strcmp(vc
->model
, model
) == 0)
321 snprintf(buf
, sizeof(buf
), "%s.%d", model
, id
);
326 VLANClientState
*qemu_new_vlan_client(VLANState
*vlan
,
329 IOReadHandler
*fd_read
,
330 IOCanRWHandler
*fd_can_read
,
333 VLANClientState
*vc
, **pvc
;
334 vc
= qemu_mallocz(sizeof(VLANClientState
));
337 vc
->model
= strdup(model
);
339 vc
->name
= strdup(name
);
341 vc
->name
= assign_name(vc
, model
);
342 vc
->fd_read
= fd_read
;
343 vc
->fd_can_read
= fd_can_read
;
348 pvc
= &vlan
->first_client
;
355 void qemu_del_vlan_client(VLANClientState
*vc
)
357 VLANClientState
**pvc
= &vc
->vlan
->first_client
;
370 int qemu_can_send_packet(VLANClientState
*vc1
)
372 VLANState
*vlan
= vc1
->vlan
;
375 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
377 if (vc
->fd_can_read
&& vc
->fd_can_read(vc
->opaque
))
384 void qemu_send_packet(VLANClientState
*vc1
, const uint8_t *buf
, int size
)
386 VLANState
*vlan
= vc1
->vlan
;
390 printf("vlan %d send:\n", vlan
->id
);
391 hex_dump(stdout
, buf
, size
);
393 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
395 vc
->fd_read(vc
->opaque
, buf
, size
);
400 static ssize_t
vc_sendv_compat(VLANClientState
*vc
, const struct iovec
*iov
,
403 uint8_t buffer
[4096];
407 for (i
= 0; i
< iovcnt
; i
++) {
410 len
= MIN(sizeof(buffer
) - offset
, iov
[i
].iov_len
);
411 memcpy(buffer
+ offset
, iov
[i
].iov_base
, len
);
415 vc
->fd_read(vc
->opaque
, buffer
, offset
);
420 ssize_t
qemu_sendv_packet(VLANClientState
*vc1
, const struct iovec
*iov
,
423 VLANState
*vlan
= vc1
->vlan
;
427 for (vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
434 len
= vc
->fd_readv(vc
->opaque
, iov
, iovcnt
);
435 else if (vc
->fd_read
)
436 len
= vc_sendv_compat(vc
, iov
, iovcnt
);
438 max_len
= MAX(max_len
, len
);
444 #if defined(CONFIG_SLIRP)
446 /* slirp network adapter */
448 static int slirp_inited
;
449 static VLANClientState
*slirp_vc
;
451 int slirp_can_output(void)
453 return !slirp_vc
|| qemu_can_send_packet(slirp_vc
);
456 void slirp_output(const uint8_t *pkt
, int pkt_len
)
459 printf("slirp output:\n");
460 hex_dump(stdout
, pkt
, pkt_len
);
464 qemu_send_packet(slirp_vc
, pkt
, pkt_len
);
467 int slirp_is_inited(void)
472 static void slirp_receive(void *opaque
, const uint8_t *buf
, int size
)
475 printf("slirp input:\n");
476 hex_dump(stdout
, buf
, size
);
478 slirp_input(buf
, size
);
481 static int net_slirp_init(VLANState
*vlan
, const char *model
, const char *name
)
487 slirp_vc
= qemu_new_vlan_client(vlan
, model
, name
,
488 slirp_receive
, NULL
, NULL
);
489 slirp_vc
->info_str
[0] = '\0';
493 void net_slirp_redir(const char *redir_str
)
498 struct in_addr guest_addr
;
499 int host_port
, guest_port
;
507 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
509 if (!strcmp(buf
, "tcp")) {
511 } else if (!strcmp(buf
, "udp")) {
517 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
519 host_port
= strtol(buf
, &r
, 0);
523 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
525 if (buf
[0] == '\0') {
526 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
528 if (!inet_aton(buf
, &guest_addr
))
531 guest_port
= strtol(p
, &r
, 0);
535 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
536 fprintf(stderr
, "qemu: could not set up redirection\n");
541 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
547 static char smb_dir
[1024];
549 static void erase_dir(char *dir_name
)
555 /* erase all the files in the directory */
556 if ((d
= opendir(dir_name
)) != 0) {
561 if (strcmp(de
->d_name
, ".") != 0 &&
562 strcmp(de
->d_name
, "..") != 0) {
563 snprintf(filename
, sizeof(filename
), "%s/%s",
564 smb_dir
, de
->d_name
);
565 if (unlink(filename
) != 0) /* is it a directory? */
574 /* automatic user mode samba server configuration */
575 static void smb_exit(void)
580 /* automatic user mode samba server configuration */
581 void net_slirp_smb(const char *exported_dir
)
584 char smb_cmdline
[1024];
592 /* XXX: better tmp dir construction */
593 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
594 if (mkdir(smb_dir
, 0700) < 0) {
595 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
598 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
600 f
= fopen(smb_conf
, "w");
602 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
609 "socket address=127.0.0.1\n"
611 "lock directory=%s\n"
612 "log file=%s/log.smbd\n"
613 "smb passwd file=%s/smbpasswd\n"
629 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "%s -s %s",
630 SMBD_COMMAND
, smb_conf
);
632 slirp_add_exec(0, smb_cmdline
, 4, 139);
635 #endif /* !defined(_WIN32) */
636 void do_info_slirp(void)
641 #endif /* CONFIG_SLIRP */
645 typedef struct TAPState
{
648 char down_script
[1024];
652 static ssize_t
tap_receive_iov(void *opaque
, const struct iovec
*iov
,
655 TAPState
*s
= opaque
;
659 len
= writev(s
->fd
, iov
, iovcnt
);
660 } while (len
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
666 static void tap_receive(void *opaque
, const uint8_t *buf
, int size
)
668 TAPState
*s
= opaque
;
671 ret
= write(s
->fd
, buf
, size
);
672 if (ret
< 0 && (errno
== EINTR
|| errno
== EAGAIN
)) {
679 static void tap_send(void *opaque
)
681 TAPState
*s
= opaque
;
688 sbuf
.maxlen
= sizeof(buf
);
690 size
= getmsg(s
->fd
, NULL
, &sbuf
, &f
) >=0 ? sbuf
.len
: -1;
692 size
= read(s
->fd
, buf
, sizeof(buf
));
695 qemu_send_packet(s
->vc
, buf
, size
);
701 static TAPState
*net_tap_fd_init(VLANState
*vlan
,
708 s
= qemu_mallocz(sizeof(TAPState
));
712 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, tap_receive
, NULL
, s
);
714 s
->vc
->fd_readv
= tap_receive_iov
;
716 qemu_set_fd_handler(s
->fd
, tap_send
, NULL
, s
);
717 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "fd=%d", fd
);
721 #if defined (_BSD) || defined (__FreeBSD_kernel__)
722 static int tap_open(char *ifname
, int ifname_size
)
728 TFR(fd
= open("/dev/tap", O_RDWR
));
730 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
735 dev
= devname(s
.st_rdev
, S_IFCHR
);
736 pstrcpy(ifname
, ifname_size
, dev
);
738 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
741 #elif defined(__sun__)
742 #define TUNNEWPPA (('T'<<16) | 0x0001)
744 * Allocate TAP device, returns opened fd.
745 * Stores dev name in the first arg(must be large enough).
747 int tap_alloc(char *dev
, size_t dev_size
)
749 int tap_fd
, if_fd
, ppa
= -1;
750 static int ip_fd
= 0;
753 static int arp_fd
= 0;
754 int ip_muxid
, arp_muxid
;
755 struct strioctl strioc_if
, strioc_ppa
;
756 int link_type
= I_PLINK
;;
758 char actual_name
[32] = "";
760 memset(&ifr
, 0x0, sizeof(ifr
));
764 while( *ptr
&& !qemu_isdigit((int)*ptr
) ) ptr
++;
768 /* Check if IP device was opened */
772 TFR(ip_fd
= open("/dev/udp", O_RDWR
, 0));
774 syslog(LOG_ERR
, "Can't open /dev/ip (actually /dev/udp)");
778 TFR(tap_fd
= open("/dev/tap", O_RDWR
, 0));
780 syslog(LOG_ERR
, "Can't open /dev/tap");
784 /* Assign a new PPA and get its unit number. */
785 strioc_ppa
.ic_cmd
= TUNNEWPPA
;
786 strioc_ppa
.ic_timout
= 0;
787 strioc_ppa
.ic_len
= sizeof(ppa
);
788 strioc_ppa
.ic_dp
= (char *)&ppa
;
789 if ((ppa
= ioctl (tap_fd
, I_STR
, &strioc_ppa
)) < 0)
790 syslog (LOG_ERR
, "Can't assign new interface");
792 TFR(if_fd
= open("/dev/tap", O_RDWR
, 0));
794 syslog(LOG_ERR
, "Can't open /dev/tap (2)");
797 if(ioctl(if_fd
, I_PUSH
, "ip") < 0){
798 syslog(LOG_ERR
, "Can't push IP module");
802 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) < 0)
803 syslog(LOG_ERR
, "Can't get flags\n");
805 snprintf (actual_name
, 32, "tap%d", ppa
);
806 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
809 /* Assign ppa according to the unit number returned by tun device */
811 if (ioctl (if_fd
, SIOCSLIFNAME
, &ifr
) < 0)
812 syslog (LOG_ERR
, "Can't set PPA %d", ppa
);
813 if (ioctl(if_fd
, SIOCGLIFFLAGS
, &ifr
) <0)
814 syslog (LOG_ERR
, "Can't get flags\n");
815 /* Push arp module to if_fd */
816 if (ioctl (if_fd
, I_PUSH
, "arp") < 0)
817 syslog (LOG_ERR
, "Can't push ARP module (2)");
819 /* Push arp module to ip_fd */
820 if (ioctl (ip_fd
, I_POP
, NULL
) < 0)
821 syslog (LOG_ERR
, "I_POP failed\n");
822 if (ioctl (ip_fd
, I_PUSH
, "arp") < 0)
823 syslog (LOG_ERR
, "Can't push ARP module (3)\n");
825 TFR(arp_fd
= open ("/dev/tap", O_RDWR
, 0));
827 syslog (LOG_ERR
, "Can't open %s\n", "/dev/tap");
829 /* Set ifname to arp */
830 strioc_if
.ic_cmd
= SIOCSLIFNAME
;
831 strioc_if
.ic_timout
= 0;
832 strioc_if
.ic_len
= sizeof(ifr
);
833 strioc_if
.ic_dp
= (char *)&ifr
;
834 if (ioctl(arp_fd
, I_STR
, &strioc_if
) < 0){
835 syslog (LOG_ERR
, "Can't set ifname to arp\n");
838 if((ip_muxid
= ioctl(ip_fd
, I_LINK
, if_fd
)) < 0){
839 syslog(LOG_ERR
, "Can't link TAP device to IP");
843 if ((arp_muxid
= ioctl (ip_fd
, link_type
, arp_fd
)) < 0)
844 syslog (LOG_ERR
, "Can't link TAP device to ARP");
848 memset(&ifr
, 0x0, sizeof(ifr
));
849 pstrcpy(ifr
.lifr_name
, sizeof(ifr
.lifr_name
), actual_name
);
850 ifr
.lifr_ip_muxid
= ip_muxid
;
851 ifr
.lifr_arp_muxid
= arp_muxid
;
853 if (ioctl (ip_fd
, SIOCSLIFMUXID
, &ifr
) < 0)
855 ioctl (ip_fd
, I_PUNLINK
, arp_muxid
);
856 ioctl (ip_fd
, I_PUNLINK
, ip_muxid
);
857 syslog (LOG_ERR
, "Can't set multiplexor id");
860 snprintf(dev
, dev_size
, "tap%d", ppa
);
864 static int tap_open(char *ifname
, int ifname_size
)
868 if( (fd
= tap_alloc(dev
, sizeof(dev
))) < 0 ){
869 fprintf(stderr
, "Cannot allocate TAP device\n");
872 pstrcpy(ifname
, ifname_size
, dev
);
873 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
877 static int tap_open(char *ifname
, int ifname_size
)
879 fprintf (stderr
, "no tap on AIX\n");
883 static int tap_open(char *ifname
, int ifname_size
)
888 TFR(fd
= open("/dev/net/tun", O_RDWR
));
890 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
893 memset(&ifr
, 0, sizeof(ifr
));
894 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
895 if (ifname
[0] != '\0')
896 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, ifname
);
898 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tap%d");
899 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
901 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
905 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
906 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
911 static int launch_script(const char *setup_script
, const char *ifname
, int fd
)
917 /* try to launch network script */
921 int open_max
= sysconf (_SC_OPEN_MAX
), i
;
922 for (i
= 0; i
< open_max
; i
++)
923 if (i
!= STDIN_FILENO
&&
924 i
!= STDOUT_FILENO
&&
925 i
!= STDERR_FILENO
&&
930 *parg
++ = (char *)setup_script
;
931 *parg
++ = (char *)ifname
;
933 execv(setup_script
, args
);
936 while (waitpid(pid
, &status
, 0) != pid
);
937 if (!WIFEXITED(status
) ||
938 WEXITSTATUS(status
) != 0) {
939 fprintf(stderr
, "%s: could not launch network script\n",
947 static int net_tap_init(VLANState
*vlan
, const char *model
,
948 const char *name
, const char *ifname1
,
949 const char *setup_script
, const char *down_script
)
956 pstrcpy(ifname
, sizeof(ifname
), ifname1
);
959 TFR(fd
= tap_open(ifname
, sizeof(ifname
)));
963 if (!setup_script
|| !strcmp(setup_script
, "no"))
965 if (setup_script
[0] != '\0') {
966 if (launch_script(setup_script
, ifname
, fd
))
969 s
= net_tap_fd_init(vlan
, model
, name
, fd
);
972 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
973 "ifname=%s,script=%s,downscript=%s",
974 ifname
, setup_script
, down_script
);
975 if (down_script
&& strcmp(down_script
, "no"))
976 snprintf(s
->down_script
, sizeof(s
->down_script
), "%s", down_script
);
982 #if defined(CONFIG_VDE)
983 typedef struct VDEState
{
988 static void vde_to_qemu(void *opaque
)
990 VDEState
*s
= opaque
;
994 size
= vde_recv(s
->vde
, buf
, sizeof(buf
), 0);
996 qemu_send_packet(s
->vc
, buf
, size
);
1000 static void vde_from_qemu(void *opaque
, const uint8_t *buf
, int size
)
1002 VDEState
*s
= opaque
;
1005 ret
= vde_send(s
->vde
, buf
, size
, 0);
1006 if (ret
< 0 && errno
== EINTR
) {
1013 static int net_vde_init(VLANState
*vlan
, const char *model
,
1014 const char *name
, const char *sock
,
1015 int port
, const char *group
, int mode
)
1018 char *init_group
= strlen(group
) ? (char *)group
: NULL
;
1019 char *init_sock
= strlen(sock
) ? (char *)sock
: NULL
;
1021 struct vde_open_args args
= {
1023 .group
= init_group
,
1027 s
= qemu_mallocz(sizeof(VDEState
));
1030 s
->vde
= vde_open(init_sock
, "QEMU", &args
);
1035 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, vde_from_qemu
, NULL
, s
);
1036 qemu_set_fd_handler(vde_datafd(s
->vde
), vde_to_qemu
, NULL
, s
);
1037 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
), "sock=%s,fd=%d",
1038 sock
, vde_datafd(s
->vde
));
1043 /* network connection */
1044 typedef struct NetSocketState
{
1045 VLANClientState
*vc
;
1047 int state
; /* 0 = getting length, 1 = getting data */
1051 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1054 typedef struct NetSocketListenState
{
1059 } NetSocketListenState
;
1061 /* XXX: we consider we can send the whole packet without blocking */
1062 static void net_socket_receive(void *opaque
, const uint8_t *buf
, int size
)
1064 NetSocketState
*s
= opaque
;
1068 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
1069 send_all(s
->fd
, buf
, size
);
1072 static void net_socket_receive_dgram(void *opaque
, const uint8_t *buf
, int size
)
1074 NetSocketState
*s
= opaque
;
1075 sendto(s
->fd
, buf
, size
, 0,
1076 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
1079 static void net_socket_send(void *opaque
)
1081 NetSocketState
*s
= opaque
;
1086 size
= recv(s
->fd
, buf1
, sizeof(buf1
), 0);
1088 err
= socket_error();
1089 if (err
!= EWOULDBLOCK
)
1091 } else if (size
== 0) {
1092 /* end of connection */
1094 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1100 /* reassemble a packet from the network */
1106 memcpy(s
->buf
+ s
->index
, buf
, l
);
1110 if (s
->index
== 4) {
1112 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
1118 l
= s
->packet_len
- s
->index
;
1121 memcpy(s
->buf
+ s
->index
, buf
, l
);
1125 if (s
->index
>= s
->packet_len
) {
1126 qemu_send_packet(s
->vc
, s
->buf
, s
->packet_len
);
1135 static void net_socket_send_dgram(void *opaque
)
1137 NetSocketState
*s
= opaque
;
1140 size
= recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
1144 /* end of connection */
1145 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1148 qemu_send_packet(s
->vc
, s
->buf
, size
);
1151 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
)
1156 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
1157 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1158 inet_ntoa(mcastaddr
->sin_addr
),
1159 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
1163 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1165 perror("socket(PF_INET, SOCK_DGRAM)");
1170 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
1171 (const char *)&val
, sizeof(val
));
1173 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1177 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
1183 /* Add host to multicast group */
1184 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
1185 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
1187 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
1188 (const char *)&imr
, sizeof(struct ip_mreq
));
1190 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1194 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1196 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
1197 (const char *)&val
, sizeof(val
));
1199 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1203 socket_set_nonblock(fd
);
1211 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
1214 int fd
, int is_connected
)
1216 struct sockaddr_in saddr
;
1218 socklen_t saddr_len
;
1221 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1222 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1223 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1227 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
1229 if (saddr
.sin_addr
.s_addr
==0) {
1230 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1234 /* clone dgram socket */
1235 newfd
= net_socket_mcast_create(&saddr
);
1237 /* error already reported by net_socket_mcast_create() */
1241 /* clone newfd to fd, close newfd */
1246 fprintf(stderr
, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1247 fd
, strerror(errno
));
1252 s
= qemu_mallocz(sizeof(NetSocketState
));
1257 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
, net_socket_receive_dgram
, NULL
, s
);
1258 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
1260 /* mcast: save bound address as dst */
1261 if (is_connected
) s
->dgram_dst
=saddr
;
1263 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1264 "socket: fd=%d (%s mcast=%s:%d)",
1265 fd
, is_connected
? "cloned" : "",
1266 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1270 static void net_socket_connect(void *opaque
)
1272 NetSocketState
*s
= opaque
;
1273 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
1276 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
1279 int fd
, int is_connected
)
1282 s
= qemu_mallocz(sizeof(NetSocketState
));
1286 s
->vc
= qemu_new_vlan_client(vlan
, model
, name
,
1287 net_socket_receive
, NULL
, s
);
1288 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1289 "socket: fd=%d", fd
);
1291 net_socket_connect(s
);
1293 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
1298 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
1299 const char *model
, const char *name
,
1300 int fd
, int is_connected
)
1302 int so_type
=-1, optlen
=sizeof(so_type
);
1304 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
1305 (socklen_t
*)&optlen
)< 0) {
1306 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd
);
1311 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
1313 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1315 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1316 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
1317 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
1322 static void net_socket_accept(void *opaque
)
1324 NetSocketListenState
*s
= opaque
;
1326 struct sockaddr_in saddr
;
1331 len
= sizeof(saddr
);
1332 fd
= accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
1333 if (fd
< 0 && errno
!= EINTR
) {
1335 } else if (fd
>= 0) {
1339 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
1343 snprintf(s1
->vc
->info_str
, sizeof(s1
->vc
->info_str
),
1344 "socket: connection from %s:%d",
1345 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1349 static int net_socket_listen_init(VLANState
*vlan
,
1352 const char *host_str
)
1354 NetSocketListenState
*s
;
1356 struct sockaddr_in saddr
;
1358 if (parse_host_port(&saddr
, host_str
) < 0)
1361 s
= qemu_mallocz(sizeof(NetSocketListenState
));
1365 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1370 socket_set_nonblock(fd
);
1372 /* allow fast reuse */
1374 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
1376 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1381 ret
= listen(fd
, 0);
1387 s
->model
= strdup(model
);
1388 s
->name
= strdup(name
);
1390 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
1394 static int net_socket_connect_init(VLANState
*vlan
,
1397 const char *host_str
)
1400 int fd
, connected
, ret
, err
;
1401 struct sockaddr_in saddr
;
1403 if (parse_host_port(&saddr
, host_str
) < 0)
1406 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1411 socket_set_nonblock(fd
);
1415 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
1417 err
= socket_error();
1418 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
1419 } else if (err
== EINPROGRESS
) {
1422 } else if (err
== WSAEALREADY
) {
1435 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
1438 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1439 "socket: connect to %s:%d",
1440 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1444 static int net_socket_mcast_init(VLANState
*vlan
,
1447 const char *host_str
)
1451 struct sockaddr_in saddr
;
1453 if (parse_host_port(&saddr
, host_str
) < 0)
1457 fd
= net_socket_mcast_create(&saddr
);
1461 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
1465 s
->dgram_dst
= saddr
;
1467 snprintf(s
->vc
->info_str
, sizeof(s
->vc
->info_str
),
1468 "socket: mcast=%s:%d",
1469 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
1474 /* find or alloc a new VLAN */
1475 VLANState
*qemu_find_vlan(int id
)
1477 VLANState
**pvlan
, *vlan
;
1478 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1482 vlan
= qemu_mallocz(sizeof(VLANState
));
1487 pvlan
= &first_vlan
;
1488 while (*pvlan
!= NULL
)
1489 pvlan
= &(*pvlan
)->next
;
1494 int net_client_init(const char *device
, const char *p
)
1502 if (get_param_value(buf
, sizeof(buf
), "vlan", p
)) {
1503 vlan_id
= strtol(buf
, NULL
, 0);
1505 vlan
= qemu_find_vlan(vlan_id
);
1507 fprintf(stderr
, "Could not create vlan %d\n", vlan_id
);
1510 if (get_param_value(buf
, sizeof(buf
), "name", p
)) {
1513 if (!strcmp(device
, "nic")) {
1517 if (nb_nics
>= MAX_NICS
) {
1518 fprintf(stderr
, "Too Many NICs\n");
1521 nd
= &nd_table
[nb_nics
];
1522 macaddr
= nd
->macaddr
;
1528 macaddr
[5] = 0x56 + nb_nics
;
1530 if (get_param_value(buf
, sizeof(buf
), "macaddr", p
)) {
1531 if (parse_macaddr(macaddr
, buf
) < 0) {
1532 fprintf(stderr
, "invalid syntax for ethernet address\n");
1536 if (get_param_value(buf
, sizeof(buf
), "model", p
)) {
1537 nd
->model
= strdup(buf
);
1543 vlan
->nb_guest_devs
++;
1546 if (!strcmp(device
, "none")) {
1547 /* does nothing. It is needed to signal that no network cards
1552 if (!strcmp(device
, "user")) {
1553 if (get_param_value(buf
, sizeof(buf
), "hostname", p
)) {
1554 pstrcpy(slirp_hostname
, sizeof(slirp_hostname
), buf
);
1556 vlan
->nb_host_devs
++;
1557 ret
= net_slirp_init(vlan
, device
, name
);
1561 if (!strcmp(device
, "tap")) {
1563 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1564 fprintf(stderr
, "tap: no interface name\n");
1567 vlan
->nb_host_devs
++;
1568 ret
= tap_win32_init(vlan
, device
, name
, ifname
);
1570 #elif defined (_AIX)
1572 if (!strcmp(device
, "tap")) {
1574 char setup_script
[1024], down_script
[1024];
1576 vlan
->nb_host_devs
++;
1577 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1578 fd
= strtol(buf
, NULL
, 0);
1579 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1581 if (net_tap_fd_init(vlan
, device
, name
, fd
))
1584 if (get_param_value(ifname
, sizeof(ifname
), "ifname", p
) <= 0) {
1587 if (get_param_value(setup_script
, sizeof(setup_script
), "script", p
) == 0) {
1588 pstrcpy(setup_script
, sizeof(setup_script
), DEFAULT_NETWORK_SCRIPT
);
1590 if (get_param_value(down_script
, sizeof(down_script
), "downscript", p
) == 0) {
1591 pstrcpy(down_script
, sizeof(down_script
), DEFAULT_NETWORK_DOWN_SCRIPT
);
1593 ret
= net_tap_init(vlan
, device
, name
, ifname
, setup_script
, down_script
);
1597 if (!strcmp(device
, "socket")) {
1598 if (get_param_value(buf
, sizeof(buf
), "fd", p
) > 0) {
1600 fd
= strtol(buf
, NULL
, 0);
1602 if (net_socket_fd_init(vlan
, device
, name
, fd
, 1))
1604 } else if (get_param_value(buf
, sizeof(buf
), "listen", p
) > 0) {
1605 ret
= net_socket_listen_init(vlan
, device
, name
, buf
);
1606 } else if (get_param_value(buf
, sizeof(buf
), "connect", p
) > 0) {
1607 ret
= net_socket_connect_init(vlan
, device
, name
, buf
);
1608 } else if (get_param_value(buf
, sizeof(buf
), "mcast", p
) > 0) {
1609 ret
= net_socket_mcast_init(vlan
, device
, name
, buf
);
1611 fprintf(stderr
, "Unknown socket options: %s\n", p
);
1614 vlan
->nb_host_devs
++;
1617 if (!strcmp(device
, "vde")) {
1618 char vde_sock
[1024], vde_group
[512];
1619 int vde_port
, vde_mode
;
1620 vlan
->nb_host_devs
++;
1621 if (get_param_value(vde_sock
, sizeof(vde_sock
), "sock", p
) <= 0) {
1624 if (get_param_value(buf
, sizeof(buf
), "port", p
) > 0) {
1625 vde_port
= strtol(buf
, NULL
, 10);
1629 if (get_param_value(vde_group
, sizeof(vde_group
), "group", p
) <= 0) {
1630 vde_group
[0] = '\0';
1632 if (get_param_value(buf
, sizeof(buf
), "mode", p
) > 0) {
1633 vde_mode
= strtol(buf
, NULL
, 8);
1637 ret
= net_vde_init(vlan
, device
, name
, vde_sock
, vde_port
, vde_group
, vde_mode
);
1641 fprintf(stderr
, "Unknown network device: %s\n", device
);
1647 fprintf(stderr
, "Could not initialize device '%s'\n", device
);
1654 int net_client_parse(const char *str
)
1662 while (*p
!= '\0' && *p
!= ',') {
1663 if ((q
- device
) < sizeof(device
) - 1)
1671 return net_client_init(device
, p
);
1674 void do_info_network(void)
1677 VLANClientState
*vc
;
1679 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1680 term_printf("VLAN %d devices:\n", vlan
->id
);
1681 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
)
1682 term_printf(" %s: %s\n", vc
->name
, vc
->info_str
);
1686 void net_cleanup(void)
1690 #if !defined(_WIN32)
1691 /* close network clients */
1692 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1693 VLANClientState
*vc
;
1695 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
1696 if (vc
->fd_read
== tap_receive
) {
1698 TAPState
*s
= vc
->opaque
;
1700 if (strcmp(vc
->model
, "tap") == 0 &&
1701 sscanf(vc
->info_str
, "ifname=%63s ", ifname
) == 1 &&
1703 launch_script(s
->down_script
, ifname
, s
->fd
);
1705 #if defined(CONFIG_VDE)
1706 if (vc
->fd_read
== vde_from_qemu
) {
1707 VDEState
*s
= vc
->opaque
;
1716 void net_client_check(void)
1720 for(vlan
= first_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1721 if (vlan
->nb_guest_devs
== 0 && vlan
->nb_host_devs
== 0)
1723 if (vlan
->nb_guest_devs
== 0)
1724 fprintf(stderr
, "Warning: vlan %d with no nics\n", vlan
->id
);
1725 if (vlan
->nb_host_devs
== 0)
1727 "Warning: vlan %d is not connected to host network\n",