]> git.proxmox.com Git - qemu.git/blob - net.c
virtio-net support
[qemu.git] / net.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24 #include "qemu-common.h"
25 #include "net.h"
26 #include "console.h"
27 #include "sysemu.h"
28 #include "qemu-timer.h"
29 #include "qemu-char.h"
30 #include "audio/audio.h"
31
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <signal.h>
35 #include <time.h>
36 #include <errno.h>
37 #include <sys/time.h>
38 #include <zlib.h>
39
40 #ifndef _WIN32
41 #include <sys/times.h>
42 #include <sys/wait.h>
43 #include <termios.h>
44 #include <sys/mman.h>
45 #include <sys/ioctl.h>
46 #include <sys/resource.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <net/if.h>
50 #ifdef __NetBSD__
51 #include <net/if_tap.h>
52 #endif
53 #ifdef __linux__
54 #include <linux/if_tun.h>
55 #endif
56 #include <arpa/inet.h>
57 #include <dirent.h>
58 #include <netdb.h>
59 #include <sys/select.h>
60 #ifdef _BSD
61 #include <sys/stat.h>
62 #ifdef __FreeBSD__
63 #include <libutil.h>
64 #else
65 #include <util.h>
66 #endif
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 #include <freebsd/stdlib.h>
69 #else
70 #ifdef __linux__
71 #include <pty.h>
72 #include <malloc.h>
73 #include <linux/rtc.h>
74
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> */
78 #include "hpet.h"
79
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
82 #endif
83 #ifdef __sun__
84 #include <sys/stat.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>
94 #include <net/if.h>
95 #include <syslog.h>
96 #include <stropts.h>
97 #endif
98 #endif
99 #endif
100
101 #include "qemu_socket.h"
102
103 #if defined(CONFIG_SLIRP)
104 #include "libslirp.h"
105 #endif
106
107 #if defined(__OpenBSD__)
108 #include <util.h>
109 #endif
110
111 #if defined(CONFIG_VDE)
112 #include <libvdeplug.h>
113 #endif
114
115 #ifdef _WIN32
116 #include <malloc.h>
117 #include <sys/timeb.h>
118 #include <mmsystem.h>
119 #define getopt_long_only getopt_long
120 #define memalign(align, size) malloc(size)
121 #endif
122
123 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
124 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
125 #ifdef __sun__
126 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
127 #else
128 #define SMBD_COMMAND "/usr/sbin/smbd"
129 #endif
130
131 static VLANState *first_vlan;
132
133 /***********************************************************/
134 /* network device redirectors */
135
136 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
137 static void hex_dump(FILE *f, const uint8_t *buf, int size)
138 {
139 int len, i, j, c;
140
141 for(i=0;i<size;i+=16) {
142 len = size - i;
143 if (len > 16)
144 len = 16;
145 fprintf(f, "%08x ", i);
146 for(j=0;j<16;j++) {
147 if (j < len)
148 fprintf(f, " %02x", buf[i+j]);
149 else
150 fprintf(f, " ");
151 }
152 fprintf(f, " ");
153 for(j=0;j<len;j++) {
154 c = buf[i+j];
155 if (c < ' ' || c > '~')
156 c = '.';
157 fprintf(f, "%c", c);
158 }
159 fprintf(f, "\n");
160 }
161 }
162 #endif
163
164 static int parse_macaddr(uint8_t *macaddr, const char *p)
165 {
166 int i;
167 char *last_char;
168 long int offset;
169
170 errno = 0;
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;
177 return 0;
178 } else {
179 for(i = 0; i < 6; i++) {
180 macaddr[i] = strtol(p, (char **)&p, 16);
181 if (i == 5) {
182 if (*p != '\0')
183 return -1;
184 } else {
185 if (*p != ':' && *p != '-')
186 return -1;
187 p++;
188 }
189 }
190 return 0;
191 }
192
193 return -1;
194 }
195
196 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
197 {
198 const char *p, *p1;
199 int len;
200 p = *pp;
201 p1 = strchr(p, sep);
202 if (!p1)
203 return -1;
204 len = p1 - p;
205 p1++;
206 if (buf_size > 0) {
207 if (len > buf_size - 1)
208 len = buf_size - 1;
209 memcpy(buf, p, len);
210 buf[len] = '\0';
211 }
212 *pp = p1;
213 return 0;
214 }
215
216 int parse_host_src_port(struct sockaddr_in *haddr,
217 struct sockaddr_in *saddr,
218 const char *input_str)
219 {
220 char *str = strdup(input_str);
221 char *host_str = str;
222 char *src_str;
223 const char *src_str2;
224 char *ptr;
225
226 /*
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".
230 */
231 if ((ptr = strchr(str,',')))
232 *ptr = '\0';
233
234 if ((src_str = strchr(input_str,'@'))) {
235 *src_str = '\0';
236 src_str++;
237 }
238
239 if (parse_host_port(haddr, host_str) < 0)
240 goto fail;
241
242 src_str2 = src_str;
243 if (!src_str || *src_str == '\0')
244 src_str2 = ":0";
245
246 if (parse_host_port(saddr, src_str2) < 0)
247 goto fail;
248
249 free(str);
250 return(0);
251
252 fail:
253 free(str);
254 return -1;
255 }
256
257 int parse_host_port(struct sockaddr_in *saddr, const char *str)
258 {
259 char buf[512];
260 struct hostent *he;
261 const char *p, *r;
262 int port;
263
264 p = str;
265 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
266 return -1;
267 saddr->sin_family = AF_INET;
268 if (buf[0] == '\0') {
269 saddr->sin_addr.s_addr = 0;
270 } else {
271 if (qemu_isdigit(buf[0])) {
272 if (!inet_aton(buf, &saddr->sin_addr))
273 return -1;
274 } else {
275 if ((he = gethostbyname(buf)) == NULL)
276 return - 1;
277 saddr->sin_addr = *(struct in_addr *)he->h_addr;
278 }
279 }
280 port = strtol(p, (char **)&r, 0);
281 if (r == p)
282 return -1;
283 saddr->sin_port = htons(port);
284 return 0;
285 }
286
287 #if !defined(_WIN32) && 0
288 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
289 {
290 const char *p;
291 int len;
292
293 len = MIN(108, strlen(str));
294 p = strchr(str, ',');
295 if (p)
296 len = MIN(len, p - str);
297
298 memset(uaddr, 0, sizeof(*uaddr));
299
300 uaddr->sun_family = AF_UNIX;
301 memcpy(uaddr->sun_path, str, len);
302
303 return 0;
304 }
305 #endif
306
307 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
308 IOReadHandler *fd_read,
309 IOCanRWHandler *fd_can_read,
310 void *opaque)
311 {
312 VLANClientState *vc, **pvc;
313 vc = qemu_mallocz(sizeof(VLANClientState));
314 if (!vc)
315 return NULL;
316 vc->fd_read = fd_read;
317 vc->fd_can_read = fd_can_read;
318 vc->opaque = opaque;
319 vc->vlan = vlan;
320
321 vc->next = NULL;
322 pvc = &vlan->first_client;
323 while (*pvc != NULL)
324 pvc = &(*pvc)->next;
325 *pvc = vc;
326 return vc;
327 }
328
329 void qemu_del_vlan_client(VLANClientState *vc)
330 {
331 VLANClientState **pvc = &vc->vlan->first_client;
332
333 while (*pvc != NULL)
334 if (*pvc == vc) {
335 *pvc = vc->next;
336 free(vc);
337 break;
338 } else
339 pvc = &(*pvc)->next;
340 }
341
342 int qemu_can_send_packet(VLANClientState *vc1)
343 {
344 VLANState *vlan = vc1->vlan;
345 VLANClientState *vc;
346
347 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
348 if (vc != vc1) {
349 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
350 return 1;
351 }
352 }
353 return 0;
354 }
355
356 void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
357 {
358 VLANState *vlan = vc1->vlan;
359 VLANClientState *vc;
360
361 #ifdef DEBUG_NET
362 printf("vlan %d send:\n", vlan->id);
363 hex_dump(stdout, buf, size);
364 #endif
365 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
366 if (vc != vc1) {
367 vc->fd_read(vc->opaque, buf, size);
368 }
369 }
370 }
371
372 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
373 int iovcnt)
374 {
375 uint8_t buffer[4096];
376 size_t offset = 0;
377 int i;
378
379 for (i = 0; i < iovcnt; i++) {
380 size_t len;
381
382 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
383 memcpy(buffer + offset, iov[i].iov_base, len);
384 offset += len;
385 }
386
387 vc->fd_read(vc->opaque, buffer, offset);
388
389 return offset;
390 }
391
392 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
393 int iovcnt)
394 {
395 VLANState *vlan = vc1->vlan;
396 VLANClientState *vc;
397 ssize_t max_len = 0;
398
399 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
400 ssize_t len = 0;
401
402 if (vc == vc1)
403 continue;
404
405 if (vc->fd_readv)
406 len = vc->fd_readv(vc->opaque, iov, iovcnt);
407 else if (vc->fd_read)
408 len = vc_sendv_compat(vc, iov, iovcnt);
409
410 max_len = MAX(max_len, len);
411 }
412
413 return max_len;
414 }
415
416 #if defined(CONFIG_SLIRP)
417
418 /* slirp network adapter */
419
420 static int slirp_inited;
421 static VLANClientState *slirp_vc;
422
423 int slirp_can_output(void)
424 {
425 return !slirp_vc || qemu_can_send_packet(slirp_vc);
426 }
427
428 void slirp_output(const uint8_t *pkt, int pkt_len)
429 {
430 #ifdef DEBUG_SLIRP
431 printf("slirp output:\n");
432 hex_dump(stdout, pkt, pkt_len);
433 #endif
434 if (!slirp_vc)
435 return;
436 qemu_send_packet(slirp_vc, pkt, pkt_len);
437 }
438
439 int slirp_is_inited(void)
440 {
441 return slirp_inited;
442 }
443
444 static void slirp_receive(void *opaque, const uint8_t *buf, int size)
445 {
446 #ifdef DEBUG_SLIRP
447 printf("slirp input:\n");
448 hex_dump(stdout, buf, size);
449 #endif
450 slirp_input(buf, size);
451 }
452
453 static int net_slirp_init(VLANState *vlan)
454 {
455 if (!slirp_inited) {
456 slirp_inited = 1;
457 slirp_init();
458 }
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");
462 return 0;
463 }
464
465 void net_slirp_redir(const char *redir_str)
466 {
467 int is_udp;
468 char buf[256], *r;
469 const char *p;
470 struct in_addr guest_addr;
471 int host_port, guest_port;
472
473 if (!slirp_inited) {
474 slirp_inited = 1;
475 slirp_init();
476 }
477
478 p = redir_str;
479 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
480 goto fail;
481 if (!strcmp(buf, "tcp")) {
482 is_udp = 0;
483 } else if (!strcmp(buf, "udp")) {
484 is_udp = 1;
485 } else {
486 goto fail;
487 }
488
489 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
490 goto fail;
491 host_port = strtol(buf, &r, 0);
492 if (r == buf)
493 goto fail;
494
495 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
496 goto fail;
497 if (buf[0] == '\0') {
498 pstrcpy(buf, sizeof(buf), "10.0.2.15");
499 }
500 if (!inet_aton(buf, &guest_addr))
501 goto fail;
502
503 guest_port = strtol(p, &r, 0);
504 if (r == p)
505 goto fail;
506
507 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
508 fprintf(stderr, "qemu: could not set up redirection\n");
509 exit(1);
510 }
511 return;
512 fail:
513 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
514 exit(1);
515 }
516
517 #ifndef _WIN32
518
519 static char smb_dir[1024];
520
521 static void erase_dir(char *dir_name)
522 {
523 DIR *d;
524 struct dirent *de;
525 char filename[1024];
526
527 /* erase all the files in the directory */
528 if ((d = opendir(dir_name)) != 0) {
529 for(;;) {
530 de = readdir(d);
531 if (!de)
532 break;
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? */
538 erase_dir(filename);
539 }
540 }
541 closedir(d);
542 rmdir(dir_name);
543 }
544 }
545
546 /* automatic user mode samba server configuration */
547 static void smb_exit(void)
548 {
549 erase_dir(smb_dir);
550 }
551
552 /* automatic user mode samba server configuration */
553 void net_slirp_smb(const char *exported_dir)
554 {
555 char smb_conf[1024];
556 char smb_cmdline[1024];
557 FILE *f;
558
559 if (!slirp_inited) {
560 slirp_inited = 1;
561 slirp_init();
562 }
563
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);
568 exit(1);
569 }
570 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
571
572 f = fopen(smb_conf, "w");
573 if (!f) {
574 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
575 exit(1);
576 }
577 fprintf(f,
578 "[global]\n"
579 "private dir=%s\n"
580 "smb ports=0\n"
581 "socket address=127.0.0.1\n"
582 "pid directory=%s\n"
583 "lock directory=%s\n"
584 "log file=%s/log.smbd\n"
585 "smb passwd file=%s/smbpasswd\n"
586 "security = share\n"
587 "[qemu]\n"
588 "path=%s\n"
589 "read only=no\n"
590 "guest ok=yes\n",
591 smb_dir,
592 smb_dir,
593 smb_dir,
594 smb_dir,
595 smb_dir,
596 exported_dir
597 );
598 fclose(f);
599 atexit(smb_exit);
600
601 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
602 SMBD_COMMAND, smb_conf);
603
604 slirp_add_exec(0, smb_cmdline, 4, 139);
605 }
606
607 #endif /* !defined(_WIN32) */
608 void do_info_slirp(void)
609 {
610 slirp_stats();
611 }
612
613 #endif /* CONFIG_SLIRP */
614
615 #if !defined(_WIN32)
616
617 typedef struct TAPState {
618 VLANClientState *vc;
619 int fd;
620 char down_script[1024];
621 } TAPState;
622
623 static void tap_receive(void *opaque, const uint8_t *buf, int size)
624 {
625 TAPState *s = opaque;
626 int ret;
627 for(;;) {
628 ret = write(s->fd, buf, size);
629 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
630 } else {
631 break;
632 }
633 }
634 }
635
636 static void tap_send(void *opaque)
637 {
638 TAPState *s = opaque;
639 uint8_t buf[4096];
640 int size;
641
642 #ifdef __sun__
643 struct strbuf sbuf;
644 int f = 0;
645 sbuf.maxlen = sizeof(buf);
646 sbuf.buf = buf;
647 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
648 #else
649 size = read(s->fd, buf, sizeof(buf));
650 #endif
651 if (size > 0) {
652 qemu_send_packet(s->vc, buf, size);
653 }
654 }
655
656 /* fd support */
657
658 static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
659 {
660 TAPState *s;
661
662 s = qemu_mallocz(sizeof(TAPState));
663 if (!s)
664 return NULL;
665 s->fd = fd;
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);
669 return s;
670 }
671
672 #if defined (_BSD) || defined (__FreeBSD_kernel__)
673 static int tap_open(char *ifname, int ifname_size)
674 {
675 int fd;
676 char *dev;
677 struct stat s;
678
679 TFR(fd = open("/dev/tap", O_RDWR));
680 if (fd < 0) {
681 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
682 return -1;
683 }
684
685 fstat(fd, &s);
686 dev = devname(s.st_rdev, S_IFCHR);
687 pstrcpy(ifname, ifname_size, dev);
688
689 fcntl(fd, F_SETFL, O_NONBLOCK);
690 return fd;
691 }
692 #elif defined(__sun__)
693 #define TUNNEWPPA (('T'<<16) | 0x0001)
694 /*
695 * Allocate TAP device, returns opened fd.
696 * Stores dev name in the first arg(must be large enough).
697 */
698 int tap_alloc(char *dev, size_t dev_size)
699 {
700 int tap_fd, if_fd, ppa = -1;
701 static int ip_fd = 0;
702 char *ptr;
703
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;;
708 struct lifreq ifr;
709 char actual_name[32] = "";
710
711 memset(&ifr, 0x0, sizeof(ifr));
712
713 if( *dev ){
714 ptr = dev;
715 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
716 ppa = atoi(ptr);
717 }
718
719 /* Check if IP device was opened */
720 if( ip_fd )
721 close(ip_fd);
722
723 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
724 if (ip_fd < 0) {
725 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
726 return -1;
727 }
728
729 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
730 if (tap_fd < 0) {
731 syslog(LOG_ERR, "Can't open /dev/tap");
732 return -1;
733 }
734
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");
742
743 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
744 if (if_fd < 0) {
745 syslog(LOG_ERR, "Can't open /dev/tap (2)");
746 return -1;
747 }
748 if(ioctl(if_fd, I_PUSH, "ip") < 0){
749 syslog(LOG_ERR, "Can't push IP module");
750 return -1;
751 }
752
753 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
754 syslog(LOG_ERR, "Can't get flags\n");
755
756 snprintf (actual_name, 32, "tap%d", ppa);
757 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
758
759 ifr.lifr_ppa = ppa;
760 /* Assign ppa according to the unit number returned by tun device */
761
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)");
769
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");
775 /* Open arp_fd */
776 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
777 if (arp_fd < 0)
778 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
779
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");
787 }
788
789 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
790 syslog(LOG_ERR, "Can't link TAP device to IP");
791 return -1;
792 }
793
794 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
795 syslog (LOG_ERR, "Can't link TAP device to ARP");
796
797 close (if_fd);
798
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;
803
804 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
805 {
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");
809 }
810
811 snprintf(dev, dev_size, "tap%d", ppa);
812 return tap_fd;
813 }
814
815 static int tap_open(char *ifname, int ifname_size)
816 {
817 char dev[10]="";
818 int fd;
819 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
820 fprintf(stderr, "Cannot allocate TAP device\n");
821 return -1;
822 }
823 pstrcpy(ifname, ifname_size, dev);
824 fcntl(fd, F_SETFL, O_NONBLOCK);
825 return fd;
826 }
827 #elif defined (_AIX)
828 static int tap_open(char *ifname, int ifname_size)
829 {
830 fprintf (stderr, "no tap on AIX\n");
831 return -1;
832 }
833 #else
834 static int tap_open(char *ifname, int ifname_size)
835 {
836 struct ifreq ifr;
837 int fd, ret;
838
839 TFR(fd = open("/dev/net/tun", O_RDWR));
840 if (fd < 0) {
841 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
842 return -1;
843 }
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);
848 else
849 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
850 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
851 if (ret != 0) {
852 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
853 close(fd);
854 return -1;
855 }
856 pstrcpy(ifname, ifname_size, ifr.ifr_name);
857 fcntl(fd, F_SETFL, O_NONBLOCK);
858 return fd;
859 }
860 #endif
861
862 static int launch_script(const char *setup_script, const char *ifname, int fd)
863 {
864 int pid, status;
865 char *args[3];
866 char **parg;
867
868 /* try to launch network script */
869 pid = fork();
870 if (pid >= 0) {
871 if (pid == 0) {
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 &&
877 i != fd)
878 close(i);
879
880 parg = args;
881 *parg++ = (char *)setup_script;
882 *parg++ = (char *)ifname;
883 *parg++ = NULL;
884 execv(setup_script, args);
885 _exit(1);
886 }
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",
891 setup_script);
892 return -1;
893 }
894 }
895 return 0;
896 }
897
898 static int net_tap_init(VLANState *vlan, const char *ifname1,
899 const char *setup_script, const char *down_script)
900 {
901 TAPState *s;
902 int fd;
903 char ifname[128];
904
905 if (ifname1 != NULL)
906 pstrcpy(ifname, sizeof(ifname), ifname1);
907 else
908 ifname[0] = '\0';
909 TFR(fd = tap_open(ifname, sizeof(ifname)));
910 if (fd < 0)
911 return -1;
912
913 if (!setup_script || !strcmp(setup_script, "no"))
914 setup_script = "";
915 if (setup_script[0] != '\0') {
916 if (launch_script(setup_script, ifname, fd))
917 return -1;
918 }
919 s = net_tap_fd_init(vlan, fd);
920 if (!s)
921 return -1;
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);
926 return 0;
927 }
928
929 #endif /* !_WIN32 */
930
931 #if defined(CONFIG_VDE)
932 typedef struct VDEState {
933 VLANClientState *vc;
934 VDECONN *vde;
935 } VDEState;
936
937 static void vde_to_qemu(void *opaque)
938 {
939 VDEState *s = opaque;
940 uint8_t buf[4096];
941 int size;
942
943 size = vde_recv(s->vde, buf, sizeof(buf), 0);
944 if (size > 0) {
945 qemu_send_packet(s->vc, buf, size);
946 }
947 }
948
949 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
950 {
951 VDEState *s = opaque;
952 int ret;
953 for(;;) {
954 ret = vde_send(s->vde, buf, size, 0);
955 if (ret < 0 && errno == EINTR) {
956 } else {
957 break;
958 }
959 }
960 }
961
962 static int net_vde_init(VLANState *vlan, const char *sock, int port,
963 const char *group, int mode)
964 {
965 VDEState *s;
966 char *init_group = strlen(group) ? (char *)group : NULL;
967 char *init_sock = strlen(sock) ? (char *)sock : NULL;
968
969 struct vde_open_args args = {
970 .port = port,
971 .group = init_group,
972 .mode = mode,
973 };
974
975 s = qemu_mallocz(sizeof(VDEState));
976 if (!s)
977 return -1;
978 s->vde = vde_open(init_sock, "QEMU", &args);
979 if (!s->vde){
980 free(s);
981 return -1;
982 }
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));
987 return 0;
988 }
989 #endif
990
991 /* network connection */
992 typedef struct NetSocketState {
993 VLANClientState *vc;
994 int fd;
995 int state; /* 0 = getting length, 1 = getting data */
996 int index;
997 int packet_len;
998 uint8_t buf[4096];
999 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1000 } NetSocketState;
1001
1002 typedef struct NetSocketListenState {
1003 VLANState *vlan;
1004 int fd;
1005 } NetSocketListenState;
1006
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)
1009 {
1010 NetSocketState *s = opaque;
1011 uint32_t len;
1012 len = htonl(size);
1013
1014 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1015 send_all(s->fd, buf, size);
1016 }
1017
1018 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1019 {
1020 NetSocketState *s = opaque;
1021 sendto(s->fd, buf, size, 0,
1022 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1023 }
1024
1025 static void net_socket_send(void *opaque)
1026 {
1027 NetSocketState *s = opaque;
1028 int l, size, err;
1029 uint8_t buf1[4096];
1030 const uint8_t *buf;
1031
1032 size = recv(s->fd, buf1, sizeof(buf1), 0);
1033 if (size < 0) {
1034 err = socket_error();
1035 if (err != EWOULDBLOCK)
1036 goto eoc;
1037 } else if (size == 0) {
1038 /* end of connection */
1039 eoc:
1040 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1041 closesocket(s->fd);
1042 return;
1043 }
1044 buf = buf1;
1045 while (size > 0) {
1046 /* reassemble a packet from the network */
1047 switch(s->state) {
1048 case 0:
1049 l = 4 - s->index;
1050 if (l > size)
1051 l = size;
1052 memcpy(s->buf + s->index, buf, l);
1053 buf += l;
1054 size -= l;
1055 s->index += l;
1056 if (s->index == 4) {
1057 /* got length */
1058 s->packet_len = ntohl(*(uint32_t *)s->buf);
1059 s->index = 0;
1060 s->state = 1;
1061 }
1062 break;
1063 case 1:
1064 l = s->packet_len - s->index;
1065 if (l > size)
1066 l = size;
1067 memcpy(s->buf + s->index, buf, l);
1068 s->index += l;
1069 buf += l;
1070 size -= l;
1071 if (s->index >= s->packet_len) {
1072 qemu_send_packet(s->vc, s->buf, s->packet_len);
1073 s->index = 0;
1074 s->state = 0;
1075 }
1076 break;
1077 }
1078 }
1079 }
1080
1081 static void net_socket_send_dgram(void *opaque)
1082 {
1083 NetSocketState *s = opaque;
1084 int size;
1085
1086 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1087 if (size < 0)
1088 return;
1089 if (size == 0) {
1090 /* end of connection */
1091 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1092 return;
1093 }
1094 qemu_send_packet(s->vc, s->buf, size);
1095 }
1096
1097 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1098 {
1099 struct ip_mreq imr;
1100 int fd;
1101 int val, ret;
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));
1106 return -1;
1107
1108 }
1109 fd = socket(PF_INET, SOCK_DGRAM, 0);
1110 if (fd < 0) {
1111 perror("socket(PF_INET, SOCK_DGRAM)");
1112 return -1;
1113 }
1114
1115 val = 1;
1116 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1117 (const char *)&val, sizeof(val));
1118 if (ret < 0) {
1119 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1120 goto fail;
1121 }
1122
1123 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1124 if (ret < 0) {
1125 perror("bind");
1126 goto fail;
1127 }
1128
1129 /* Add host to multicast group */
1130 imr.imr_multiaddr = mcastaddr->sin_addr;
1131 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1132
1133 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1134 (const char *)&imr, sizeof(struct ip_mreq));
1135 if (ret < 0) {
1136 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1137 goto fail;
1138 }
1139
1140 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1141 val = 1;
1142 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1143 (const char *)&val, sizeof(val));
1144 if (ret < 0) {
1145 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1146 goto fail;
1147 }
1148
1149 socket_set_nonblock(fd);
1150 return fd;
1151 fail:
1152 if (fd >= 0)
1153 closesocket(fd);
1154 return -1;
1155 }
1156
1157 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
1158 int is_connected)
1159 {
1160 struct sockaddr_in saddr;
1161 int newfd;
1162 socklen_t saddr_len;
1163 NetSocketState *s;
1164
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
1168 */
1169
1170 if (is_connected) {
1171 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1172 /* must be bound */
1173 if (saddr.sin_addr.s_addr==0) {
1174 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1175 fd);
1176 return NULL;
1177 }
1178 /* clone dgram socket */
1179 newfd = net_socket_mcast_create(&saddr);
1180 if (newfd < 0) {
1181 /* error already reported by net_socket_mcast_create() */
1182 close(fd);
1183 return NULL;
1184 }
1185 /* clone newfd to fd, close newfd */
1186 dup2(newfd, fd);
1187 close(newfd);
1188
1189 } else {
1190 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1191 fd, strerror(errno));
1192 return NULL;
1193 }
1194 }
1195
1196 s = qemu_mallocz(sizeof(NetSocketState));
1197 if (!s)
1198 return NULL;
1199 s->fd = fd;
1200
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);
1203
1204 /* mcast: save bound address as dst */
1205 if (is_connected) s->dgram_dst=saddr;
1206
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));
1211 return s;
1212 }
1213
1214 static void net_socket_connect(void *opaque)
1215 {
1216 NetSocketState *s = opaque;
1217 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1218 }
1219
1220 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
1221 int is_connected)
1222 {
1223 NetSocketState *s;
1224 s = qemu_mallocz(sizeof(NetSocketState));
1225 if (!s)
1226 return NULL;
1227 s->fd = fd;
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);
1232 if (is_connected) {
1233 net_socket_connect(s);
1234 } else {
1235 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1236 }
1237 return s;
1238 }
1239
1240 static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
1241 int is_connected)
1242 {
1243 int so_type=-1, optlen=sizeof(so_type);
1244
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);
1248 return NULL;
1249 }
1250 switch(so_type) {
1251 case SOCK_DGRAM:
1252 return net_socket_fd_init_dgram(vlan, fd, is_connected);
1253 case SOCK_STREAM:
1254 return net_socket_fd_init_stream(vlan, fd, is_connected);
1255 default:
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);
1259 }
1260 return NULL;
1261 }
1262
1263 static void net_socket_accept(void *opaque)
1264 {
1265 NetSocketListenState *s = opaque;
1266 NetSocketState *s1;
1267 struct sockaddr_in saddr;
1268 socklen_t len;
1269 int fd;
1270
1271 for(;;) {
1272 len = sizeof(saddr);
1273 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1274 if (fd < 0 && errno != EINTR) {
1275 return;
1276 } else if (fd >= 0) {
1277 break;
1278 }
1279 }
1280 s1 = net_socket_fd_init(s->vlan, fd, 1);
1281 if (!s1) {
1282 closesocket(fd);
1283 } else {
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));
1287 }
1288 }
1289
1290 static int net_socket_listen_init(VLANState *vlan, const char *host_str)
1291 {
1292 NetSocketListenState *s;
1293 int fd, val, ret;
1294 struct sockaddr_in saddr;
1295
1296 if (parse_host_port(&saddr, host_str) < 0)
1297 return -1;
1298
1299 s = qemu_mallocz(sizeof(NetSocketListenState));
1300 if (!s)
1301 return -1;
1302
1303 fd = socket(PF_INET, SOCK_STREAM, 0);
1304 if (fd < 0) {
1305 perror("socket");
1306 return -1;
1307 }
1308 socket_set_nonblock(fd);
1309
1310 /* allow fast reuse */
1311 val = 1;
1312 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1313
1314 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1315 if (ret < 0) {
1316 perror("bind");
1317 return -1;
1318 }
1319 ret = listen(fd, 0);
1320 if (ret < 0) {
1321 perror("listen");
1322 return -1;
1323 }
1324 s->vlan = vlan;
1325 s->fd = fd;
1326 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1327 return 0;
1328 }
1329
1330 static int net_socket_connect_init(VLANState *vlan, const char *host_str)
1331 {
1332 NetSocketState *s;
1333 int fd, connected, ret, err;
1334 struct sockaddr_in saddr;
1335
1336 if (parse_host_port(&saddr, host_str) < 0)
1337 return -1;
1338
1339 fd = socket(PF_INET, SOCK_STREAM, 0);
1340 if (fd < 0) {
1341 perror("socket");
1342 return -1;
1343 }
1344 socket_set_nonblock(fd);
1345
1346 connected = 0;
1347 for(;;) {
1348 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1349 if (ret < 0) {
1350 err = socket_error();
1351 if (err == EINTR || err == EWOULDBLOCK) {
1352 } else if (err == EINPROGRESS) {
1353 break;
1354 #ifdef _WIN32
1355 } else if (err == WSAEALREADY) {
1356 break;
1357 #endif
1358 } else {
1359 perror("connect");
1360 closesocket(fd);
1361 return -1;
1362 }
1363 } else {
1364 connected = 1;
1365 break;
1366 }
1367 }
1368 s = net_socket_fd_init(vlan, fd, connected);
1369 if (!s)
1370 return -1;
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));
1374 return 0;
1375 }
1376
1377 static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
1378 {
1379 NetSocketState *s;
1380 int fd;
1381 struct sockaddr_in saddr;
1382
1383 if (parse_host_port(&saddr, host_str) < 0)
1384 return -1;
1385
1386
1387 fd = net_socket_mcast_create(&saddr);
1388 if (fd < 0)
1389 return -1;
1390
1391 s = net_socket_fd_init(vlan, fd, 0);
1392 if (!s)
1393 return -1;
1394
1395 s->dgram_dst = saddr;
1396
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));
1400 return 0;
1401
1402 }
1403
1404 /* find or alloc a new VLAN */
1405 VLANState *qemu_find_vlan(int id)
1406 {
1407 VLANState **pvlan, *vlan;
1408 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1409 if (vlan->id == id)
1410 return vlan;
1411 }
1412 vlan = qemu_mallocz(sizeof(VLANState));
1413 if (!vlan)
1414 return NULL;
1415 vlan->id = id;
1416 vlan->next = NULL;
1417 pvlan = &first_vlan;
1418 while (*pvlan != NULL)
1419 pvlan = &(*pvlan)->next;
1420 *pvlan = vlan;
1421 return vlan;
1422 }
1423
1424 int net_client_init(const char *device, const char *p)
1425 {
1426 char buf[1024];
1427 int vlan_id, ret;
1428 VLANState *vlan;
1429
1430 vlan_id = 0;
1431 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1432 vlan_id = strtol(buf, NULL, 0);
1433 }
1434 vlan = qemu_find_vlan(vlan_id);
1435 if (!vlan) {
1436 fprintf(stderr, "Could not create vlan %d\n", vlan_id);
1437 return -1;
1438 }
1439 if (!strcmp(device, "nic")) {
1440 NICInfo *nd;
1441 uint8_t *macaddr;
1442
1443 if (nb_nics >= MAX_NICS) {
1444 fprintf(stderr, "Too Many NICs\n");
1445 return -1;
1446 }
1447 nd = &nd_table[nb_nics];
1448 macaddr = nd->macaddr;
1449 macaddr[0] = 0x52;
1450 macaddr[1] = 0x54;
1451 macaddr[2] = 0x00;
1452 macaddr[3] = 0x12;
1453 macaddr[4] = 0x34;
1454 macaddr[5] = 0x56 + nb_nics;
1455
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");
1459 return -1;
1460 }
1461 }
1462 if (get_param_value(buf, sizeof(buf), "model", p)) {
1463 nd->model = strdup(buf);
1464 }
1465 nd->vlan = vlan;
1466 nb_nics++;
1467 vlan->nb_guest_devs++;
1468 ret = 0;
1469 } else
1470 if (!strcmp(device, "none")) {
1471 /* does nothing. It is needed to signal that no network cards
1472 are wanted */
1473 ret = 0;
1474 } else
1475 #ifdef CONFIG_SLIRP
1476 if (!strcmp(device, "user")) {
1477 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1478 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1479 }
1480 vlan->nb_host_devs++;
1481 ret = net_slirp_init(vlan);
1482 } else
1483 #endif
1484 #ifdef _WIN32
1485 if (!strcmp(device, "tap")) {
1486 char ifname[64];
1487 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1488 fprintf(stderr, "tap: no interface name\n");
1489 return -1;
1490 }
1491 vlan->nb_host_devs++;
1492 ret = tap_win32_init(vlan, ifname);
1493 } else
1494 #elif defined (_AIX)
1495 #else
1496 if (!strcmp(device, "tap")) {
1497 char ifname[64];
1498 char setup_script[1024], down_script[1024];
1499 int fd;
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);
1504 ret = -1;
1505 if (net_tap_fd_init(vlan, fd))
1506 ret = 0;
1507 } else {
1508 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1509 ifname[0] = '\0';
1510 }
1511 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1512 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1513 }
1514 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1515 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1516 }
1517 ret = net_tap_init(vlan, ifname, setup_script, down_script);
1518 }
1519 } else
1520 #endif
1521 if (!strcmp(device, "socket")) {
1522 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1523 int fd;
1524 fd = strtol(buf, NULL, 0);
1525 ret = -1;
1526 if (net_socket_fd_init(vlan, fd, 1))
1527 ret = 0;
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);
1534 } else {
1535 fprintf(stderr, "Unknown socket options: %s\n", p);
1536 return -1;
1537 }
1538 vlan->nb_host_devs++;
1539 } else
1540 #ifdef CONFIG_VDE
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) {
1546 vde_sock[0] = '\0';
1547 }
1548 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1549 vde_port = strtol(buf, NULL, 10);
1550 } else {
1551 vde_port = 0;
1552 }
1553 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1554 vde_group[0] = '\0';
1555 }
1556 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1557 vde_mode = strtol(buf, NULL, 8);
1558 } else {
1559 vde_mode = 0700;
1560 }
1561 ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode);
1562 } else
1563 #endif
1564 {
1565 fprintf(stderr, "Unknown network device: %s\n", device);
1566 return -1;
1567 }
1568 if (ret < 0) {
1569 fprintf(stderr, "Could not initialize device '%s'\n", device);
1570 }
1571
1572 return ret;
1573 }
1574
1575 int net_client_parse(const char *str)
1576 {
1577 const char *p;
1578 char *q;
1579 char device[64];
1580
1581 p = str;
1582 q = device;
1583 while (*p != '\0' && *p != ',') {
1584 if ((q - device) < sizeof(device) - 1)
1585 *q++ = *p;
1586 p++;
1587 }
1588 *q = '\0';
1589 if (*p == ',')
1590 p++;
1591
1592 return net_client_init(device, p);
1593 }
1594
1595 void do_info_network(void)
1596 {
1597 VLANState *vlan;
1598 VLANClientState *vc;
1599
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);
1604 }
1605 }
1606
1607 void net_cleanup(void)
1608 {
1609 VLANState *vlan;
1610
1611 #if !defined(_WIN32)
1612 /* close network clients */
1613 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1614 VLANClientState *vc;
1615
1616 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
1617 if (vc->fd_read == tap_receive) {
1618 char ifname[64];
1619 TAPState *s = vc->opaque;
1620
1621 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
1622 s->down_script[0])
1623 launch_script(s->down_script, ifname, s->fd);
1624 }
1625 #if defined(CONFIG_VDE)
1626 if (vc->fd_read == vde_from_qemu) {
1627 VDEState *s = vc->opaque;
1628 vde_close(s->vde);
1629 }
1630 #endif
1631 }
1632 }
1633 #endif
1634 }
1635
1636 void net_client_check(void)
1637 {
1638 VLANState *vlan;
1639
1640 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1641 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1642 continue;
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)
1646 fprintf(stderr,
1647 "Warning: vlan %d is not connected to host network\n",
1648 vlan->id);
1649 }
1650 }