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
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
46 #include <net/if_tap.h>
49 #include <linux/if_tun.h>
51 #include <arpa/inet.h>
54 #include <sys/select.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
70 /* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72 /* #include <linux/hpet.h> */
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
96 #if defined(__OpenBSD__)
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
107 #include <sys/timeb.h>
108 #include <mmsystem.h>
109 #define getopt_long_only getopt_long
110 #define memalign(align, size) malloc(size)
113 #include "qemu-common.h"
117 #include "qemu-timer.h"
118 #include "qemu-char.h"
119 #include "audio/audio.h"
120 #include "qemu_socket.h"
122 #if defined(CONFIG_SLIRP)
123 #include "libslirp.h"
127 static VLANState *first_vlan;
129 /***********************************************************/
130 /* network device redirectors */
132 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
133 static void hex_dump(FILE *f, const uint8_t *buf, int size)
137 for(i=0;i<size;i+=16) {
141 fprintf(f, "%08x ", i);
144 fprintf(f, " %02x", buf[i+j]);
151 if (c < ' ' || c > '~')
160 static int parse_macaddr(uint8_t *macaddr, const char *p)
167 offset = strtol(p, &last_char, 0);
168 if (0 == errno && '\0' == *last_char &&
169 offset >= 0 && offset <= 0xFFFFFF) {
170 macaddr[3] = (offset & 0xFF0000) >> 16;
171 macaddr[4] = (offset & 0xFF00) >> 8;
172 macaddr[5] = offset & 0xFF;
175 for(i = 0; i < 6; i++) {
176 macaddr[i] = strtol(p, (char **)&p, 16);
181 if (*p != ':' && *p != '-')
192 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
203 if (len > buf_size - 1)
212 int parse_host_src_port(struct sockaddr_in *haddr,
213 struct sockaddr_in *saddr,
214 const char *input_str)
216 char *str = strdup(input_str);
217 char *host_str = str;
219 const char *src_str2;
223 * Chop off any extra arguments at the end of the string which
224 * would start with a comma, then fill in the src port information
225 * if it was provided else use the "any address" and "any port".
227 if ((ptr = strchr(str,',')))
230 if ((src_str = strchr(input_str,'@'))) {
235 if (parse_host_port(haddr, host_str) < 0)
239 if (!src_str || *src_str == '\0')
242 if (parse_host_port(saddr, src_str2) < 0)
253 int parse_host_port(struct sockaddr_in *saddr, const char *str)
261 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
263 saddr->sin_family = AF_INET;
264 if (buf[0] == '\0') {
265 saddr->sin_addr.s_addr = 0;
267 if (qemu_isdigit(buf[0])) {
268 if (!inet_aton(buf, &saddr->sin_addr))
271 if ((he = gethostbyname(buf)) == NULL)
273 saddr->sin_addr = *(struct in_addr *)he->h_addr;
276 port = strtol(p, (char **)&r, 0);
279 saddr->sin_port = htons(port);
283 #if !defined(_WIN32) && 0
284 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
289 len = MIN(108, strlen(str));
290 p = strchr(str, ',');
292 len = MIN(len, p - str);
294 memset(uaddr, 0, sizeof(*uaddr));
296 uaddr->sun_family = AF_UNIX;
297 memcpy(uaddr->sun_path, str, len);
303 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
305 snprintf(vc->info_str, sizeof(vc->info_str),
306 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
308 macaddr[0], macaddr[1], macaddr[2],
309 macaddr[3], macaddr[4], macaddr[5]);
312 static char *assign_name(VLANClientState *vc1, const char *model)
318 for (vlan = first_vlan; vlan; vlan = vlan->next) {
321 for (vc = vlan->first_client; vc; vc = vc->next)
322 if (vc != vc1 && strcmp(vc->model, model) == 0)
326 snprintf(buf, sizeof(buf), "%s.%d", model, id);
331 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
334 IOReadHandler *fd_read,
335 IOCanRWHandler *fd_can_read,
339 VLANClientState *vc, **pvc;
340 vc = qemu_mallocz(sizeof(VLANClientState));
341 vc->model = strdup(model);
343 vc->name = strdup(name);
345 vc->name = assign_name(vc, model);
346 vc->fd_read = fd_read;
347 vc->fd_can_read = fd_can_read;
348 vc->cleanup = cleanup;
353 pvc = &vlan->first_client;
360 void qemu_del_vlan_client(VLANClientState *vc)
362 VLANClientState **pvc = &vc->vlan->first_client;
378 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
380 VLANClientState **pvc = &vlan->first_client;
383 if ((*pvc)->opaque == opaque)
391 int qemu_can_send_packet(VLANClientState *vc1)
393 VLANState *vlan = vc1->vlan;
396 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
398 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
405 void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
407 VLANState *vlan = vc1->vlan;
414 printf("vlan %d send:\n", vlan->id);
415 hex_dump(stdout, buf, size);
417 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
418 if (vc != vc1 && !vc->link_down) {
419 vc->fd_read(vc->opaque, buf, size);
424 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
427 uint8_t buffer[4096];
431 for (i = 0; i < iovcnt; i++) {
434 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
435 memcpy(buffer + offset, iov[i].iov_base, len);
439 vc->fd_read(vc->opaque, buffer, offset);
444 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
449 for (i = 0; i < iovcnt; i++)
450 offset += iov[i].iov_len;
454 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
457 VLANState *vlan = vc1->vlan;
462 return calc_iov_length(iov, iovcnt);
464 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
471 len = calc_iov_length(iov, iovcnt);
473 len = vc->fd_readv(vc->opaque, iov, iovcnt);
474 else if (vc->fd_read)
475 len = vc_sendv_compat(vc, iov, iovcnt);
477 max_len = MAX(max_len, len);
483 #if defined(CONFIG_SLIRP)
485 /* slirp network adapter */
487 static int slirp_inited;
488 static int slirp_restrict;
489 static char *slirp_ip;
490 static VLANClientState *slirp_vc;
492 int slirp_can_output(void)
494 return !slirp_vc || qemu_can_send_packet(slirp_vc);
497 void slirp_output(const uint8_t *pkt, int pkt_len)
500 printf("slirp output:\n");
501 hex_dump(stdout, pkt, pkt_len);
505 qemu_send_packet(slirp_vc, pkt, pkt_len);
508 int slirp_is_inited(void)
513 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
516 printf("slirp input:\n");
517 hex_dump(stdout, buf, size);
519 slirp_input(buf, size);
522 static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
526 slirp_init(slirp_restrict, slirp_ip);
528 slirp_vc = qemu_new_vlan_client(vlan, model, name,
529 slirp_receive, NULL, NULL, NULL);
530 slirp_vc->info_str[0] = '\0';
534 void net_slirp_redir(const char *redir_str)
539 struct in_addr guest_addr;
540 int host_port, guest_port;
544 slirp_init(slirp_restrict, slirp_ip);
548 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
550 if (!strcmp(buf, "tcp")) {
552 } else if (!strcmp(buf, "udp")) {
558 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
560 host_port = strtol(buf, &r, 0);
564 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
566 if (buf[0] == '\0') {
567 pstrcpy(buf, sizeof(buf), "10.0.2.15");
569 if (!inet_aton(buf, &guest_addr))
572 guest_port = strtol(p, &r, 0);
576 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
577 fprintf(stderr, "qemu: could not set up redirection\n");
582 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
588 static char smb_dir[1024];
590 static void erase_dir(char *dir_name)
596 /* erase all the files in the directory */
597 if ((d = opendir(dir_name)) != NULL) {
602 if (strcmp(de->d_name, ".") != 0 &&
603 strcmp(de->d_name, "..") != 0) {
604 snprintf(filename, sizeof(filename), "%s/%s",
605 smb_dir, de->d_name);
606 if (unlink(filename) != 0) /* is it a directory? */
615 /* automatic user mode samba server configuration */
616 static void smb_exit(void)
621 /* automatic user mode samba server configuration */
622 void net_slirp_smb(const char *exported_dir)
625 char smb_cmdline[1024];
630 slirp_init(slirp_restrict, slirp_ip);
633 /* XXX: better tmp dir construction */
634 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
635 if (mkdir(smb_dir, 0700) < 0) {
636 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
639 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
641 f = fopen(smb_conf, "w");
643 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
650 "socket address=127.0.0.1\n"
652 "lock directory=%s\n"
653 "log file=%s/log.smbd\n"
654 "smb passwd file=%s/smbpasswd\n"
670 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
671 SMBD_COMMAND, smb_conf);
673 slirp_add_exec(0, smb_cmdline, 4, 139);
676 #endif /* !defined(_WIN32) */
677 void do_info_slirp(Monitor *mon)
687 static int vmchannel_can_read(void *opaque)
689 struct VMChannel *vmc = (struct VMChannel*)opaque;
690 return slirp_socket_can_recv(4, vmc->port);
693 static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
695 struct VMChannel *vmc = (struct VMChannel*)opaque;
696 slirp_socket_recv(4, vmc->port, buf, size);
699 #endif /* CONFIG_SLIRP */
703 typedef struct TAPState {
706 char down_script[1024];
707 char down_script_arg[128];
710 static int launch_script(const char *setup_script, const char *ifname, int fd);
712 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
715 TAPState *s = opaque;
719 len = writev(s->fd, iov, iovcnt);
720 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
725 static void tap_receive(void *opaque, const uint8_t *buf, int size)
727 TAPState *s = opaque;
730 ret = write(s->fd, buf, size);
731 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
738 static void tap_send(void *opaque)
740 TAPState *s = opaque;
747 sbuf.maxlen = sizeof(buf);
748 sbuf.buf = (char *)buf;
749 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
751 size = read(s->fd, buf, sizeof(buf));
754 qemu_send_packet(s->vc, buf, size);
758 static void tap_cleanup(VLANClientState *vc)
760 TAPState *s = vc->opaque;
762 if (s->down_script[0])
763 launch_script(s->down_script, s->down_script_arg, s->fd);
765 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
772 static TAPState *net_tap_fd_init(VLANState *vlan,
779 s = qemu_mallocz(sizeof(TAPState));
781 s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive,
782 NULL, tap_cleanup, s);
783 s->vc->fd_readv = tap_receive_iov;
784 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
785 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
789 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
790 static int tap_open(char *ifname, int ifname_size)
796 TFR(fd = open("/dev/tap", O_RDWR));
798 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
803 dev = devname(s.st_rdev, S_IFCHR);
804 pstrcpy(ifname, ifname_size, dev);
806 fcntl(fd, F_SETFL, O_NONBLOCK);
809 #elif defined(__sun__)
810 #define TUNNEWPPA (('T'<<16) | 0x0001)
812 * Allocate TAP device, returns opened fd.
813 * Stores dev name in the first arg(must be large enough).
815 static int tap_alloc(char *dev, size_t dev_size)
817 int tap_fd, if_fd, ppa = -1;
818 static int ip_fd = 0;
821 static int arp_fd = 0;
822 int ip_muxid, arp_muxid;
823 struct strioctl strioc_if, strioc_ppa;
824 int link_type = I_PLINK;;
826 char actual_name[32] = "";
828 memset(&ifr, 0x0, sizeof(ifr));
832 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
836 /* Check if IP device was opened */
840 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
842 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
846 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
848 syslog(LOG_ERR, "Can't open /dev/tap");
852 /* Assign a new PPA and get its unit number. */
853 strioc_ppa.ic_cmd = TUNNEWPPA;
854 strioc_ppa.ic_timout = 0;
855 strioc_ppa.ic_len = sizeof(ppa);
856 strioc_ppa.ic_dp = (char *)&ppa;
857 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
858 syslog (LOG_ERR, "Can't assign new interface");
860 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
862 syslog(LOG_ERR, "Can't open /dev/tap (2)");
865 if(ioctl(if_fd, I_PUSH, "ip") < 0){
866 syslog(LOG_ERR, "Can't push IP module");
870 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
871 syslog(LOG_ERR, "Can't get flags\n");
873 snprintf (actual_name, 32, "tap%d", ppa);
874 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
877 /* Assign ppa according to the unit number returned by tun device */
879 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
880 syslog (LOG_ERR, "Can't set PPA %d", ppa);
881 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
882 syslog (LOG_ERR, "Can't get flags\n");
883 /* Push arp module to if_fd */
884 if (ioctl (if_fd, I_PUSH, "arp") < 0)
885 syslog (LOG_ERR, "Can't push ARP module (2)");
887 /* Push arp module to ip_fd */
888 if (ioctl (ip_fd, I_POP, NULL) < 0)
889 syslog (LOG_ERR, "I_POP failed\n");
890 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
891 syslog (LOG_ERR, "Can't push ARP module (3)\n");
893 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
895 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
897 /* Set ifname to arp */
898 strioc_if.ic_cmd = SIOCSLIFNAME;
899 strioc_if.ic_timout = 0;
900 strioc_if.ic_len = sizeof(ifr);
901 strioc_if.ic_dp = (char *)𝔦
902 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
903 syslog (LOG_ERR, "Can't set ifname to arp\n");
906 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
907 syslog(LOG_ERR, "Can't link TAP device to IP");
911 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
912 syslog (LOG_ERR, "Can't link TAP device to ARP");
916 memset(&ifr, 0x0, sizeof(ifr));
917 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
918 ifr.lifr_ip_muxid = ip_muxid;
919 ifr.lifr_arp_muxid = arp_muxid;
921 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
923 ioctl (ip_fd, I_PUNLINK , arp_muxid);
924 ioctl (ip_fd, I_PUNLINK, ip_muxid);
925 syslog (LOG_ERR, "Can't set multiplexor id");
928 snprintf(dev, dev_size, "tap%d", ppa);
932 static int tap_open(char *ifname, int ifname_size)
936 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
937 fprintf(stderr, "Cannot allocate TAP device\n");
940 pstrcpy(ifname, ifname_size, dev);
941 fcntl(fd, F_SETFL, O_NONBLOCK);
945 static int tap_open(char *ifname, int ifname_size)
947 fprintf (stderr, "no tap on AIX\n");
951 static int tap_open(char *ifname, int ifname_size)
956 TFR(fd = open("/dev/net/tun", O_RDWR));
958 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
961 memset(&ifr, 0, sizeof(ifr));
962 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
963 if (ifname[0] != '\0')
964 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
966 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
967 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
969 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
973 pstrcpy(ifname, ifname_size, ifr.ifr_name);
974 fcntl(fd, F_SETFL, O_NONBLOCK);
979 static int launch_script(const char *setup_script, const char *ifname, int fd)
985 /* try to launch network script */
989 int open_max = sysconf (_SC_OPEN_MAX), i;
990 for (i = 0; i < open_max; i++)
991 if (i != STDIN_FILENO &&
992 i != STDOUT_FILENO &&
993 i != STDERR_FILENO &&
998 *parg++ = (char *)setup_script;
999 *parg++ = (char *)ifname;
1001 execv(setup_script, args);
1004 while (waitpid(pid, &status, 0) != pid);
1005 if (!WIFEXITED(status) ||
1006 WEXITSTATUS(status) != 0) {
1007 fprintf(stderr, "%s: could not launch network script\n",
1015 static int net_tap_init(VLANState *vlan, const char *model,
1016 const char *name, const char *ifname1,
1017 const char *setup_script, const char *down_script)
1023 if (ifname1 != NULL)
1024 pstrcpy(ifname, sizeof(ifname), ifname1);
1027 TFR(fd = tap_open(ifname, sizeof(ifname)));
1031 if (!setup_script || !strcmp(setup_script, "no"))
1033 if (setup_script[0] != '\0') {
1034 if (launch_script(setup_script, ifname, fd))
1037 s = net_tap_fd_init(vlan, model, name, fd);
1038 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1039 "ifname=%s,script=%s,downscript=%s",
1040 ifname, setup_script, down_script);
1041 if (down_script && strcmp(down_script, "no")) {
1042 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1043 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1048 #endif /* !_WIN32 */
1050 #if defined(CONFIG_VDE)
1051 typedef struct VDEState {
1052 VLANClientState *vc;
1056 static void vde_to_qemu(void *opaque)
1058 VDEState *s = opaque;
1062 size = vde_recv(s->vde, buf, sizeof(buf), 0);
1064 qemu_send_packet(s->vc, buf, size);
1068 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1070 VDEState *s = opaque;
1073 ret = vde_send(s->vde, buf, size, 0);
1074 if (ret < 0 && errno == EINTR) {
1081 static void vde_cleanup(VLANClientState *vc)
1083 VDEState *s = vc->opaque;
1084 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1089 static int net_vde_init(VLANState *vlan, const char *model,
1090 const char *name, const char *sock,
1091 int port, const char *group, int mode)
1094 char *init_group = strlen(group) ? (char *)group : NULL;
1095 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1097 struct vde_open_args args = {
1099 .group = init_group,
1103 s = qemu_mallocz(sizeof(VDEState));
1104 s->vde = vde_open(init_sock, "QEMU", &args);
1109 s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu,
1110 NULL, vde_cleanup, s);
1111 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1112 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1113 sock, vde_datafd(s->vde));
1118 /* network connection */
1119 typedef struct NetSocketState {
1120 VLANClientState *vc;
1122 int state; /* 0 = getting length, 1 = getting data */
1124 unsigned int packet_len;
1126 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1129 typedef struct NetSocketListenState {
1134 } NetSocketListenState;
1136 /* XXX: we consider we can send the whole packet without blocking */
1137 static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1139 NetSocketState *s = opaque;
1143 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1144 send_all(s->fd, buf, size);
1147 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1149 NetSocketState *s = opaque;
1150 sendto(s->fd, buf, size, 0,
1151 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1154 static void net_socket_send(void *opaque)
1156 NetSocketState *s = opaque;
1162 size = recv(s->fd, buf1, sizeof(buf1), 0);
1164 err = socket_error();
1165 if (err != EWOULDBLOCK)
1167 } else if (size == 0) {
1168 /* end of connection */
1170 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1176 /* reassemble a packet from the network */
1182 memcpy(s->buf + s->index, buf, l);
1186 if (s->index == 4) {
1188 s->packet_len = ntohl(*(uint32_t *)s->buf);
1194 l = s->packet_len - s->index;
1197 if (s->index + l <= sizeof(s->buf)) {
1198 memcpy(s->buf + s->index, buf, l);
1200 fprintf(stderr, "serious error: oversized packet received,"
1201 "connection terminated.\n");
1209 if (s->index >= s->packet_len) {
1210 qemu_send_packet(s->vc, s->buf, s->packet_len);
1219 static void net_socket_send_dgram(void *opaque)
1221 NetSocketState *s = opaque;
1224 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1228 /* end of connection */
1229 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1232 qemu_send_packet(s->vc, s->buf, size);
1235 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1240 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1241 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1242 inet_ntoa(mcastaddr->sin_addr),
1243 (int)ntohl(mcastaddr->sin_addr.s_addr));
1247 fd = socket(PF_INET, SOCK_DGRAM, 0);
1249 perror("socket(PF_INET, SOCK_DGRAM)");
1254 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1255 (const char *)&val, sizeof(val));
1257 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1261 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1267 /* Add host to multicast group */
1268 imr.imr_multiaddr = mcastaddr->sin_addr;
1269 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1271 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1272 (const char *)&imr, sizeof(struct ip_mreq));
1274 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1278 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1280 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1281 (const char *)&val, sizeof(val));
1283 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1287 socket_set_nonblock(fd);
1295 static void net_socket_cleanup(VLANClientState *vc)
1297 NetSocketState *s = vc->opaque;
1298 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1303 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1306 int fd, int is_connected)
1308 struct sockaddr_in saddr;
1310 socklen_t saddr_len;
1313 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1314 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1315 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1319 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1321 if (saddr.sin_addr.s_addr==0) {
1322 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1326 /* clone dgram socket */
1327 newfd = net_socket_mcast_create(&saddr);
1329 /* error already reported by net_socket_mcast_create() */
1333 /* clone newfd to fd, close newfd */
1338 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1339 fd, strerror(errno));
1344 s = qemu_mallocz(sizeof(NetSocketState));
1347 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram,
1348 NULL, net_socket_cleanup, s);
1349 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1351 /* mcast: save bound address as dst */
1352 if (is_connected) s->dgram_dst=saddr;
1354 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1355 "socket: fd=%d (%s mcast=%s:%d)",
1356 fd, is_connected? "cloned" : "",
1357 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1361 static void net_socket_connect(void *opaque)
1363 NetSocketState *s = opaque;
1364 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1367 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1370 int fd, int is_connected)
1373 s = qemu_mallocz(sizeof(NetSocketState));
1375 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive,
1376 NULL, net_socket_cleanup, s);
1377 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1378 "socket: fd=%d", fd);
1380 net_socket_connect(s);
1382 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1387 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1388 const char *model, const char *name,
1389 int fd, int is_connected)
1391 int so_type=-1, optlen=sizeof(so_type);
1393 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1394 (socklen_t *)&optlen)< 0) {
1395 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1400 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1402 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1404 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1405 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1406 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1411 static void net_socket_accept(void *opaque)
1413 NetSocketListenState *s = opaque;
1415 struct sockaddr_in saddr;
1420 len = sizeof(saddr);
1421 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1422 if (fd < 0 && errno != EINTR) {
1424 } else if (fd >= 0) {
1428 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1432 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1433 "socket: connection from %s:%d",
1434 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1438 static int net_socket_listen_init(VLANState *vlan,
1441 const char *host_str)
1443 NetSocketListenState *s;
1445 struct sockaddr_in saddr;
1447 if (parse_host_port(&saddr, host_str) < 0)
1450 s = qemu_mallocz(sizeof(NetSocketListenState));
1452 fd = socket(PF_INET, SOCK_STREAM, 0);
1457 socket_set_nonblock(fd);
1459 /* allow fast reuse */
1461 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1463 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1468 ret = listen(fd, 0);
1474 s->model = strdup(model);
1475 s->name = strdup(name);
1477 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1481 static int net_socket_connect_init(VLANState *vlan,
1484 const char *host_str)
1487 int fd, connected, ret, err;
1488 struct sockaddr_in saddr;
1490 if (parse_host_port(&saddr, host_str) < 0)
1493 fd = socket(PF_INET, SOCK_STREAM, 0);
1498 socket_set_nonblock(fd);
1502 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1504 err = socket_error();
1505 if (err == EINTR || err == EWOULDBLOCK) {
1506 } else if (err == EINPROGRESS) {
1509 } else if (err == WSAEALREADY) {
1522 s = net_socket_fd_init(vlan, model, name, fd, connected);
1525 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1526 "socket: connect to %s:%d",
1527 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1531 static int net_socket_mcast_init(VLANState *vlan,
1534 const char *host_str)
1538 struct sockaddr_in saddr;
1540 if (parse_host_port(&saddr, host_str) < 0)
1544 fd = net_socket_mcast_create(&saddr);
1548 s = net_socket_fd_init(vlan, model, name, fd, 0);
1552 s->dgram_dst = saddr;
1554 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1555 "socket: mcast=%s:%d",
1556 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1561 /* find or alloc a new VLAN */
1562 VLANState *qemu_find_vlan(int id)
1564 VLANState **pvlan, *vlan;
1565 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1569 vlan = qemu_mallocz(sizeof(VLANState));
1572 pvlan = &first_vlan;
1573 while (*pvlan != NULL)
1574 pvlan = &(*pvlan)->next;
1579 static int nic_get_free_idx(void)
1583 for (index = 0; index < MAX_NICS; index++)
1584 if (!nd_table[index].used)
1589 void qemu_check_nic_model(NICInfo *nd, const char *model)
1591 const char *models[2];
1596 qemu_check_nic_model_list(nd, models, model);
1599 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
1600 const char *default_model)
1602 int i, exit_status = 0;
1605 nd->model = strdup(default_model);
1607 if (strcmp(nd->model, "?") != 0) {
1608 for (i = 0 ; models[i]; i++)
1609 if (strcmp(nd->model, models[i]) == 0)
1612 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
1616 fprintf(stderr, "qemu: Supported NIC models: ");
1617 for (i = 0 ; models[i]; i++)
1618 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
1623 int net_client_init(const char *device, const char *p)
1631 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1632 vlan_id = strtol(buf, NULL, 0);
1634 vlan = qemu_find_vlan(vlan_id);
1636 if (get_param_value(buf, sizeof(buf), "name", p)) {
1639 if (!strcmp(device, "nic")) {
1642 int idx = nic_get_free_idx();
1644 if (idx == -1 || nb_nics >= MAX_NICS) {
1645 fprintf(stderr, "Too Many NICs\n");
1649 nd = &nd_table[idx];
1650 macaddr = nd->macaddr;
1656 macaddr[5] = 0x56 + idx;
1658 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1659 if (parse_macaddr(macaddr, buf) < 0) {
1660 fprintf(stderr, "invalid syntax for ethernet address\n");
1665 if (get_param_value(buf, sizeof(buf), "model", p)) {
1666 nd->model = strdup(buf);
1673 vlan->nb_guest_devs++;
1676 if (!strcmp(device, "none")) {
1677 /* does nothing. It is needed to signal that no network cards
1682 if (!strcmp(device, "user")) {
1683 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1684 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1686 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
1687 slirp_restrict = (buf[0] == 'y') ? 1 : 0;
1689 if (get_param_value(buf, sizeof(buf), "ip", p)) {
1690 slirp_ip = strdup(buf);
1692 vlan->nb_host_devs++;
1693 ret = net_slirp_init(vlan, device, name);
1694 } else if (!strcmp(device, "channel")) {
1696 char name[20], *devname;
1697 struct VMChannel *vmc;
1699 port = strtol(p, &devname, 10);
1701 if (port < 1 || port > 65535) {
1702 fprintf(stderr, "vmchannel wrong port number\n");
1706 vmc = malloc(sizeof(struct VMChannel));
1707 snprintf(name, 20, "vmchannel%ld", port);
1708 vmc->hd = qemu_chr_open(name, devname, NULL);
1710 fprintf(stderr, "qemu: could not open vmchannel device"
1716 slirp_add_exec(3, vmc->hd, 4, port);
1717 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
1723 if (!strcmp(device, "tap")) {
1725 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1726 fprintf(stderr, "tap: no interface name\n");
1730 vlan->nb_host_devs++;
1731 ret = tap_win32_init(vlan, device, name, ifname);
1733 #elif defined (_AIX)
1735 if (!strcmp(device, "tap")) {
1737 char setup_script[1024], down_script[1024];
1739 vlan->nb_host_devs++;
1740 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1741 fd = strtol(buf, NULL, 0);
1742 fcntl(fd, F_SETFL, O_NONBLOCK);
1743 net_tap_fd_init(vlan, device, name, fd);
1746 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1749 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1750 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1752 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1753 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1755 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
1759 if (!strcmp(device, "socket")) {
1760 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1762 fd = strtol(buf, NULL, 0);
1764 if (net_socket_fd_init(vlan, device, name, fd, 1))
1766 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
1767 ret = net_socket_listen_init(vlan, device, name, buf);
1768 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
1769 ret = net_socket_connect_init(vlan, device, name, buf);
1770 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
1771 ret = net_socket_mcast_init(vlan, device, name, buf);
1773 fprintf(stderr, "Unknown socket options: %s\n", p);
1777 vlan->nb_host_devs++;
1780 if (!strcmp(device, "vde")) {
1781 char vde_sock[1024], vde_group[512];
1782 int vde_port, vde_mode;
1783 vlan->nb_host_devs++;
1784 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
1787 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1788 vde_port = strtol(buf, NULL, 10);
1792 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1793 vde_group[0] = '\0';
1795 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1796 vde_mode = strtol(buf, NULL, 8);
1800 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
1804 fprintf(stderr, "Unknown network device: %s\n", device);
1809 fprintf(stderr, "Could not initialize device '%s'\n", device);
1817 void net_client_uninit(NICInfo *nd)
1819 nd->vlan->nb_guest_devs--;
1822 free((void *)nd->model);
1825 static int net_host_check_device(const char *device)
1828 const char *valid_param_list[] = { "tap", "socket"
1836 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1837 if (!strncmp(valid_param_list[i], device,
1838 strlen(valid_param_list[i])))
1845 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
1847 if (!net_host_check_device(device)) {
1848 monitor_printf(mon, "invalid host network device %s\n", device);
1851 net_client_init(device, opts);
1854 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
1857 VLANClientState *vc;
1859 vlan = qemu_find_vlan(vlan_id);
1861 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1862 if (!strcmp(vc->name, device))
1866 monitor_printf(mon, "can't find device %s\n", device);
1869 qemu_del_vlan_client(vc);
1872 int net_client_parse(const char *str)
1880 while (*p != '\0' && *p != ',') {
1881 if ((q - device) < sizeof(device) - 1)
1889 return net_client_init(device, p);
1892 void do_info_network(Monitor *mon)
1895 VLANClientState *vc;
1897 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1898 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1899 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1900 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
1904 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
1907 VLANClientState *vc = NULL;
1909 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
1910 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
1911 if (strcmp(vc->name, name) == 0)
1916 monitor_printf(mon, "could not find network device '%s'", name);
1920 if (strcmp(up_or_down, "up") == 0)
1922 else if (strcmp(up_or_down, "down") == 0)
1925 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
1926 "valid\n", up_or_down);
1928 if (vc->link_status_changed)
1929 vc->link_status_changed(vc);
1934 void net_cleanup(void)
1938 /* close network clients */
1939 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1940 VLANClientState *vc = vlan->first_client;
1943 VLANClientState *next = vc->next;
1945 qemu_del_vlan_client(vc);
1952 void net_client_check(void)
1956 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1957 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1959 if (vlan->nb_guest_devs == 0)
1960 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1961 if (vlan->nb_host_devs == 0)
1963 "Warning: vlan %d is not connected to host network\n",